17
0
mirror of https://github.com/MaSzyna-EU07/maszyna.git synced 2026-10-19 07:19:18 +02:00

Draw the recorded instance draws with the variant seed they were recorded with

This commit is contained in:
2026-10-18 18:41:33 +02:00
parent bc4df5567f
commit 2e30366235
5 changed files with 225 additions and 0 deletions

View File

@@ -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;
}
}