Refactors GL device report

It makes the report function easier to read.
This commit is contained in:
Vladislav Belov 2026-07-08 08:13:44 +02:00
parent 46af67b5bd
commit 0168f45a65
No known key found for this signature in database
GPG key ID: 353545E45DB9CCB3

View file

@ -29,6 +29,7 @@
#include "ps/CLogger.h" #include "ps/CLogger.h"
#include "ps/ConfigDB.h" #include "ps/ConfigDB.h"
#include "ps/Profile.h" #include "ps/Profile.h"
#include "ps/strings/StringBuilder.h"
#include "renderer/backend/Format.h" #include "renderer/backend/Format.h"
#include "renderer/backend/gl/Buffer.h" #include "renderer/backend/gl/Buffer.h"
#include "renderer/backend/gl/DeviceCommandContext.h" #include "renderer/backend/gl/DeviceCommandContext.h"
@ -204,6 +205,63 @@ void GLAD_API_PTR OnDebugMessage(
} }
} }
template<typename ParameterType, size_t parameterCount, bool doQueryCounterBits = false>
void ReportParameter(
const Script::Request& rq, JS::HandleValue settings,
const GLenum parameter, const char* paremeterName)
{
static_assert(parameterCount > 0);
const char* errorString = "(error)";
ParameterType values[parameterCount];
if constexpr (std::is_same_v<ParameterType, GLint>)
{
std::fill(std::begin(values), std::end(values), -1);
if constexpr (doQueryCounterBits)
glGetQueryivARB(parameter, GL_QUERY_COUNTER_BITS, values);
else
glGetIntegerv(parameter, values);
}
else if constexpr (std::is_same_v<ParameterType, GLfloat>)
{
std::fill(std::begin(values), std::end(values), std::numeric_limits<GLfloat>::quiet_NaN());
glGetFloatv(parameter, values);
}
else if constexpr (std::is_same_v<ParameterType, const char*>)
{
std::fill(std::begin(values), std::end(values), "");
// In the newer GL versions we have glGetStringi but currently we get
// only one string.
static_assert(parameterCount == 1);
values[0] = reinterpret_cast<const char*>(glGetString(parameter));
if (!values[0])
values[0] = errorString;
}
else
static_assert(false);
const bool errorHappened{ogl_SquelchError(GL_INVALID_ENUM)};
char buffer[1024];
for (size_t index{0}; index < parameterCount; ++index)
{
PS::StringBuilder stringBuilder{buffer};
stringBuilder.Append(paremeterName);
if constexpr (parameterCount > 1)
{
stringBuilder.Append('[');
stringBuilder.Append(index);
stringBuilder.Append(']');
}
if (errorHappened)
Script::SetProperty(rq, settings, stringBuilder.Str().data(), errorString);
else
Script::SetProperty(rq, settings, stringBuilder.Str().data(), values[index]);
}
}
} // anonymous namespace } // anonymous namespace
// static // static
@ -389,69 +447,19 @@ CDevice::~CDevice()
void CDevice::Report(const Script::Request& rq, JS::HandleValue settings) void CDevice::Report(const Script::Request& rq, JS::HandleValue settings)
{ {
const char* errstr = "(error)";
Script::SetProperty(rq, settings, "name", "gl"); Script::SetProperty(rq, settings, "name", "gl");
#define INTEGER(id) do { \ #define INTEGER(NAME) ReportParameter<GLint, 1>(rq, settings, GL_##NAME, "GL_" #NAME)
GLint i = -1; \ #define INTEGER2(NAME) ReportParameter<GLint, 2>(rq, settings, GL_##NAME, "GL_" #NAME)
glGetIntegerv(GL_##id, &i); \
if (ogl_SquelchError(GL_INVALID_ENUM)) \
Script::SetProperty(rq, settings, "GL_" #id, errstr); \
else \
Script::SetProperty(rq, settings, "GL_" #id, i); \
} while (false)
#define INTEGER2(id) do { \ #define BOOL(NAME) INTEGER(NAME)
GLint i[2] = { -1, -1 }; \
glGetIntegerv(GL_##id, i); \
if (ogl_SquelchError(GL_INVALID_ENUM)) { \
Script::SetProperty(rq, settings, "GL_" #id "[0]", errstr); \
Script::SetProperty(rq, settings, "GL_" #id "[1]", errstr); \
} else { \
Script::SetProperty(rq, settings, "GL_" #id "[0]", i[0]); \
Script::SetProperty(rq, settings, "GL_" #id "[1]", i[1]); \
} \
} while (false)
#define FLOAT(id) do { \ #define FLOAT(NAME) ReportParameter<GLfloat, 1>(rq, settings, GL_##NAME, "GL_" #NAME)
GLfloat f = std::numeric_limits<GLfloat>::quiet_NaN(); \ #define FLOAT2(NAME) ReportParameter<GLfloat, 2>(rq, settings, GL_##NAME, "GL_" #NAME)
glGetFloatv(GL_##id, &f); \
if (ogl_SquelchError(GL_INVALID_ENUM)) \
Script::SetProperty(rq, settings, "GL_" #id, errstr); \
else \
Script::SetProperty(rq, settings, "GL_" #id, f); \
} while (false)
#define FLOAT2(id) do { \ #define STRING(NAME) ReportParameter<const char*, 1>(rq, settings, GL_##NAME, "GL_" #NAME)
GLfloat f[2] = { std::numeric_limits<GLfloat>::quiet_NaN(), std::numeric_limits<GLfloat>::quiet_NaN() }; \
glGetFloatv(GL_##id, f); \
if (ogl_SquelchError(GL_INVALID_ENUM)) { \
Script::SetProperty(rq, settings, "GL_" #id "[0]", errstr); \
Script::SetProperty(rq, settings, "GL_" #id "[1]", errstr); \
} else { \
Script::SetProperty(rq, settings, "GL_" #id "[0]", f[0]); \
Script::SetProperty(rq, settings, "GL_" #id "[1]", f[1]); \
} \
} while (false)
#define STRING(id) do { \ #define QUERY_COUNTER_BITS(NAME) ReportParameter<GLint, 1, true>(rq, settings, GL_##NAME, "GL_" #NAME ".GL_QUERY_COUNTER_BITS")
const char* c = (const char*)glGetString(GL_##id); \
if (!c) c = ""; \
if (ogl_SquelchError(GL_INVALID_ENUM)) c = errstr; \
Script::SetProperty(rq, settings, "GL_" #id, std::string(c)); \
} while (false)
#define QUERY(target, pname) do { \
GLint i = -1; \
glGetQueryivARB(GL_##target, GL_##pname, &i); \
if (ogl_SquelchError(GL_INVALID_ENUM)) \
Script::SetProperty(rq, settings, "GL_" #target ".GL_" #pname, errstr); \
else \
Script::SetProperty(rq, settings, "GL_" #target ".GL_" #pname, i); \
} while (false)
#define BOOL(id) INTEGER(id)
ogl_WarnIfError(); ogl_WarnIfError();
@ -512,7 +520,7 @@ void CDevice::Report(const Script::Request& rq, JS::HandleValue settings)
if (ogl_HaveExtension("GL_ARB_occlusion_query")) if (ogl_HaveExtension("GL_ARB_occlusion_query"))
{ {
QUERY(SAMPLES_PASSED, QUERY_COUNTER_BITS); QUERY_COUNTER_BITS(SAMPLES_PASSED);
} }
if (ogl_HaveExtension("GL_ARB_shading_language_100")) if (ogl_HaveExtension("GL_ARB_shading_language_100"))
@ -574,12 +582,12 @@ void CDevice::Report(const Script::Request& rq, JS::HandleValue settings)
if (ogl_HaveExtension("GL_EXT_timer_query") || ogl_HaveExtension("GL_ARB_timer_query")) if (ogl_HaveExtension("GL_EXT_timer_query") || ogl_HaveExtension("GL_ARB_timer_query"))
{ {
QUERY(TIME_ELAPSED, QUERY_COUNTER_BITS); QUERY_COUNTER_BITS(TIME_ELAPSED);
} }
if (ogl_HaveExtension("GL_ARB_timer_query")) if (ogl_HaveExtension("GL_ARB_timer_query"))
{ {
QUERY(TIMESTAMP, QUERY_COUNTER_BITS); QUERY_COUNTER_BITS(TIMESTAMP);
} }
if (ogl_HaveExtension("GL_EXT_texture_filter_anisotropic")) if (ogl_HaveExtension("GL_EXT_texture_filter_anisotropic"))