mirror of
https://github.com/MaSzyna-EU07/maszyna.git
synced 2026-11-01 16:05:35 +01:00
Draw the recorded instance draws with the variant seed they were recorded with
This commit is contained in:
110
gl/query.cpp
110
gl/query.cpp
@@ -2,6 +2,8 @@
|
||||
#include "query.h"
|
||||
#include "utilities/Globals.h"
|
||||
|
||||
#include <cstring>
|
||||
|
||||
gl::query::query(targets target)
|
||||
: target(target)
|
||||
{
|
||||
@@ -122,3 +124,111 @@ void gl::gpu_timer::collect()
|
||||
m_pending[slot] = false;
|
||||
}
|
||||
}
|
||||
|
||||
gl::gpu_timeline::~gpu_timeline()
|
||||
{
|
||||
for (auto &frame : m_frames)
|
||||
{
|
||||
if (frame.queries[0] != 0)
|
||||
glDeleteQueries(static_cast<GLsizei>(frame.queries.size()), frame.queries.data());
|
||||
}
|
||||
}
|
||||
|
||||
void gl::gpu_timeline::begin(char const *Name)
|
||||
{
|
||||
if (m_active)
|
||||
end();
|
||||
|
||||
collect();
|
||||
auto &frame = m_frames[m_slot];
|
||||
m_active = !frame.pending;
|
||||
if (!m_active)
|
||||
return;
|
||||
|
||||
if (frame.queries[0] == 0)
|
||||
glCreateQueries(GL_TIMESTAMP, static_cast<GLsizei>(frame.queries.size()), frame.queries.data());
|
||||
frame.count = 0;
|
||||
next(Name);
|
||||
}
|
||||
|
||||
void gl::gpu_timeline::next(char const *Name)
|
||||
{
|
||||
if (!m_active)
|
||||
return;
|
||||
|
||||
auto &frame = m_frames[m_slot];
|
||||
// the last mark is kept for end(); past the limit the part under way goes on
|
||||
if (frame.count + 1 >= MARKS)
|
||||
return;
|
||||
|
||||
glQueryCounter(frame.queries[frame.count], GL_TIMESTAMP);
|
||||
frame.names[frame.count] = Name;
|
||||
++frame.count;
|
||||
}
|
||||
|
||||
void gl::gpu_timeline::end()
|
||||
{
|
||||
if (!m_active)
|
||||
return;
|
||||
|
||||
auto &frame = m_frames[m_slot];
|
||||
glQueryCounter(frame.queries[frame.count], GL_TIMESTAMP);
|
||||
frame.names[frame.count] = nullptr;
|
||||
++frame.count;
|
||||
frame.pending = true;
|
||||
m_slot = (m_slot + 1) % SLOTS;
|
||||
m_active = false;
|
||||
}
|
||||
|
||||
std::vector<gl::gpu_timeline::part> const &gl::gpu_timeline::result()
|
||||
{
|
||||
collect();
|
||||
return m_last;
|
||||
}
|
||||
|
||||
void gl::gpu_timeline::collect()
|
||||
{
|
||||
// frames finish in the order they were issued, oldest first
|
||||
for (size_t i = 0; i < SLOTS; ++i)
|
||||
{
|
||||
auto &frame = m_frames[(m_slot + i) % SLOTS];
|
||||
if (!frame.pending)
|
||||
continue;
|
||||
|
||||
auto ready = true;
|
||||
for (size_t mark = 0; (mark < frame.count) && ready; ++mark)
|
||||
{
|
||||
GLint available = GL_FALSE;
|
||||
glGetQueryObjectiv(frame.queries[mark], GL_QUERY_RESULT_AVAILABLE, &available);
|
||||
ready = (available != GL_FALSE);
|
||||
}
|
||||
if (!ready)
|
||||
break;
|
||||
|
||||
m_last.clear();
|
||||
GLuint64 first = 0;
|
||||
GLuint64 previous = 0;
|
||||
for (size_t mark = 0; mark < frame.count; ++mark)
|
||||
{
|
||||
GLuint64 time = 0;
|
||||
glGetQueryObjectui64v(frame.queries[mark], GL_QUERY_RESULT, &time);
|
||||
if (mark == 0)
|
||||
first = time;
|
||||
else
|
||||
{
|
||||
auto const length = (time >= previous ? time - previous : 0);
|
||||
auto const *name = frame.names[mark - 1];
|
||||
auto lookup = m_last.begin();
|
||||
while ((lookup != m_last.end()) && (std::strcmp(lookup->name, name) != 0))
|
||||
++lookup;
|
||||
if (lookup != m_last.end())
|
||||
lookup->time += length;
|
||||
else
|
||||
m_last.push_back({name, length});
|
||||
}
|
||||
previous = time;
|
||||
}
|
||||
m_total = (previous >= first ? previous - first : 0);
|
||||
frame.pending = false;
|
||||
}
|
||||
}
|
||||
|
||||
57
gl/query.h
57
gl/query.h
@@ -2,6 +2,7 @@
|
||||
|
||||
#include <array>
|
||||
#include <optional>
|
||||
#include <vector>
|
||||
|
||||
#include "object.h"
|
||||
|
||||
@@ -65,4 +66,60 @@ class gpu_timer
|
||||
bool m_active = false;
|
||||
std::optional<uint64_t> m_last;
|
||||
};
|
||||
|
||||
// gpu time of the parts of a frame, from GL_TIMESTAMP queries put one after another: a part lasts from its mark to the next
|
||||
// one, so the parts add up to the time from the first mark to the last, with nothing left out and nothing counted twice.
|
||||
// the first mark of a gpu_timer is put while the gpu may still be busy with what came before, so its time can hold the tail
|
||||
// of that work; in the run of marks that time goes to the part it belongs to. results are read a few frames later without
|
||||
// stalling; if the gpu falls further behind, the frame is skipped
|
||||
class gpu_timeline
|
||||
{
|
||||
public:
|
||||
struct part
|
||||
{
|
||||
char const *name;
|
||||
uint64_t time; // nanoseconds
|
||||
};
|
||||
|
||||
gpu_timeline() = default;
|
||||
~gpu_timeline();
|
||||
gpu_timeline(const gpu_timeline &) = delete;
|
||||
gpu_timeline &operator=(const gpu_timeline &) = delete;
|
||||
|
||||
// starts the frame with its first part. the names are kept as pointers: string literals
|
||||
void begin(char const *Name);
|
||||
// ends the part under way and starts the next one
|
||||
void next(char const *Name);
|
||||
void end();
|
||||
bool active() const
|
||||
{
|
||||
return m_active;
|
||||
}
|
||||
// parts of the most recent finished frame in the order they came first, the time of parts of the same name added up
|
||||
std::vector<part> const &result();
|
||||
// from the first mark of that frame to the last
|
||||
uint64_t total() const
|
||||
{
|
||||
return m_total;
|
||||
}
|
||||
|
||||
private:
|
||||
void collect();
|
||||
|
||||
static constexpr size_t SLOTS = 4;
|
||||
static constexpr size_t MARKS = 32; // per frame, the last one included
|
||||
struct frame
|
||||
{
|
||||
std::array<GLuint, MARKS> queries{};
|
||||
std::array<char const *, MARKS> names{};
|
||||
size_t count = 0; // marks put
|
||||
bool pending = false;
|
||||
};
|
||||
std::array<frame, SLOTS> m_frames;
|
||||
// slot written by the next begin(), also the oldest one still pending
|
||||
size_t m_slot = 0;
|
||||
bool m_active = false;
|
||||
std::vector<part> m_last;
|
||||
uint64_t m_total = 0;
|
||||
};
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user