Skip to content

A fast 2D and 3D game engine — TypeScript SDK, C++/WebAssembly core, visual editor. Ship one project to web, desktop, WeChat MiniGames, playable ads, and native Android / iOS.

feat(renderer): uniform reflection API on GfxDevice + Shader

esengine64405921b5dc
diff --git a/src/esengine/renderer/GLDevice.cpp b/src/esengine/renderer/GLDevice.cpp
index f3327e406..b34184984 100644
--- a/src/esengine/renderer/GLDevice.cpp
+++ b/src/esengine/renderer/GLDevice.cpp
@@ -311,6 +311,71 @@ void GLDevice::setUniformMat4(i32 location, const f32* data) {
if (location >= 0) glUniformMatrix4fv(location, 1, GL_FALSE, data);
}
+namespace {
+
+GfxUniformType fromGLUniformType(GLenum type) {
+ switch (type) {
+ case GL_FLOAT: return GfxUniformType::Float;
+ case GL_FLOAT_VEC2: return GfxUniformType::Vec2;
+ case GL_FLOAT_VEC3: return GfxUniformType::Vec3;
+ case GL_FLOAT_VEC4: return GfxUniformType::Vec4;
+ case GL_INT: return GfxUniformType::Int;
+ case GL_INT_VEC2: return GfxUniformType::IVec2;
+ case GL_INT_VEC3: return GfxUniformType::IVec3;
+ case GL_INT_VEC4: return GfxUniformType::IVec4;
+ case GL_BOOL: return GfxUniformType::Bool;
+ case GL_FLOAT_MAT2: return GfxUniformType::Mat2;
+ case GL_FLOAT_MAT3: return GfxUniformType::Mat3;
+ case GL_FLOAT_MAT4: return GfxUniformType::Mat4;
+ case GL_SAMPLER_2D: return GfxUniformType::Sampler2D;
+ case GL_SAMPLER_CUBE: return GfxUniformType::SamplerCube;
+ default: return GfxUniformType::Unknown;
+ }
+}
+
+} // namespace
+
+std::vector<GfxUniformInfo> GLDevice::getActiveUniforms(u32 programId) {
+ std::vector<GfxUniformInfo> result;
+ if (programId == 0) return result;
+
+ GLint count = 0;
+ glGetProgramiv(programId, GL_ACTIVE_UNIFORMS, &count);
+ if (count <= 0) return result;
+
+ GLint maxNameLen = 0;
+ glGetProgramiv(programId, GL_ACTIVE_UNIFORM_MAX_LENGTH, &maxNameLen);
+ if (maxNameLen <= 0) maxNameLen = 64;
+
+ std::string nameBuf(static_cast<size_t>(maxNameLen), '\0');
+ result.reserve(static_cast<size_t>(count));
+
+ for (GLint i = 0; i < count; ++i) {
+ GLsizei nameLen = 0;
+ GLint size = 0;
+ GLenum type = 0;
+ glGetActiveUniform(programId, static_cast<GLuint>(i),
+ static_cast<GLsizei>(maxNameLen), &nameLen,
+ &size, &type, nameBuf.data());
+
+ std::string name(nameBuf.data(), static_cast<size_t>(nameLen));
+ // Strip "[0]" suffix so callers look up arrays by their declared name.
+ const auto bracket = name.find('[');
+ if (bracket != std::string::npos) {
+ name.erase(bracket);
+ }
+
+ GfxUniformInfo info;
+ info.name = std::move(name);
+ info.type = fromGLUniformType(type);
+ info.location = glGetUniformLocation(programId, info.name.c_str());
+ info.arraySize = size > 0 ? static_cast<u32>(size) : 1u;
+ result.push_back(std::move(info));
+ }
+
+ return result;
+}
+
// =============================================================================
// Buffer Operations
// =============================================================================
diff --git a/src/esengine/renderer/GLDevice.hpp b/src/esengine/renderer/GLDevice.hpp
index a86cdc969..1661984a5 100644
--- a/src/esengine/renderer/GLDevice.hpp
+++ b/src/esengine/renderer/GLDevice.hpp
@@ -68,6 +68,8 @@ public:
void setUniformMat3(i32 location, const f32* data) override;
void setUniformMat4(i32 location, const f32* data) override;
+ std::vector<GfxUniformInfo> getActiveUniforms(u32 programId) override;
+
u32 createBuffer() override;
void deleteBuffer(u32 bufferId) override;
void bindVertexBuffer(u32 bufferId) override;
diff --git a/src/esengine/renderer/GfxDevice.hpp b/src/esengine/renderer/GfxDevice.hpp
index 4ab7da183..986fbc705 100644
--- a/src/esengine/renderer/GfxDevice.hpp
+++ b/src/esengine/renderer/GfxDevice.hpp
@@ -22,6 +22,8 @@
#include "GfxEnums.hpp"
#include "Texture.hpp"
+#include <vector>
+
namespace esengine {
// =============================================================================
@@ -158,6 +160,9 @@ public:
/** @brief Sets a mat4 uniform */
virtual void setUniformMat4(i32 location, const f32* data) = 0;
+ /** @brief Enumerates all active uniforms of a linked shader program */
+ virtual std::vector<GfxUniformInfo> getActiveUniforms(u32 programId) = 0;
+
// =========================================================================
// Buffer Operations
// =========================================================================
diff --git a/src/esengine/renderer/GfxEnums.hpp b/src/esengine/renderer/GfxEnums.hpp
index be4dbb260..d74121d3a 100644
--- a/src/esengine/renderer/GfxEnums.hpp
+++ b/src/esengine/renderer/GfxEnums.hpp
@@ -14,6 +14,8 @@
#include "../core/Types.hpp"
+#include <string>
+
namespace esengine {
// =============================================================================
@@ -86,4 +88,33 @@ enum class GfxAttachment : u8 {
Depth,
};
+// =============================================================================
+// Uniform Reflection
+// =============================================================================
+
+enum class GfxUniformType : u8 {
+ Unknown,
+ Float,
+ Vec2,
+ Vec3,
+ Vec4,
+ Int,
+ IVec2,
+ IVec3,
+ IVec4,
+ Bool,
+ Mat2,
+ Mat3,
+ Mat4,
+ Sampler2D,
+ SamplerCube,
+};
+
+struct GfxUniformInfo {
+ std::string name;
+ GfxUniformType type = GfxUniformType::Unknown;
+ i32 location = -1;
+ u32 arraySize = 1;
+};
+
} // namespace esengine
diff --git a/src/esengine/renderer/Shader.cpp b/src/esengine/renderer/Shader.cpp
index d360133f3..6c5fc4a50 100644
--- a/src/esengine/renderer/Shader.cpp
+++ b/src/esengine/renderer/Shader.cpp
@@ -36,7 +36,8 @@ Shader::~Shader() {
Shader::Shader(Shader&& other) noexcept
: programId_(other.programId_)
, uniformCache_(std::move(other.uniformCache_))
- , attribCache_(std::move(other.attribCache_)) {
+ , attribCache_(std::move(other.attribCache_))
+ , activeUniforms_(std::move(other.activeUniforms_)) {
other.programId_ = 0;
}
@@ -48,6 +49,7 @@ Shader& Shader::operator=(Shader&& other) noexcept {
programId_ = other.programId_;
uniformCache_ = std::move(other.uniformCache_);
attribCache_ = std::move(other.attribCache_);
+ activeUniforms_ = std::move(other.activeUniforms_);
other.programId_ = 0;
}
return *this;
@@ -187,14 +189,84 @@ bool Shader::compile(const std::string& vertexSrc, const std::string& fragmentSr
glDeleteShader(vertexShader);
glDeleteShader(fragmentShader);
- ES_LOG_DEBUG("Shader compiled successfully (program ID: {})", programId_);
+ reflectActiveUniforms();
+
+ ES_LOG_DEBUG("Shader compiled successfully (program ID: {}, active uniforms: {})",
+ programId_, activeUniforms_.size());
return true;
}
+namespace {
+
+GfxUniformType shaderUniformTypeFromGL(GLenum type) {
+ switch (type) {
+ case GL_FLOAT: return GfxUniformType::Float;
+ case GL_FLOAT_VEC2: return GfxUniformType::Vec2;
+ case GL_FLOAT_VEC3: return GfxUniformType::Vec3;
+ case GL_FLOAT_VEC4: return GfxUniformType::Vec4;
+ case GL_INT: return GfxUniformType::Int;
+ case GL_INT_VEC2: return GfxUniformType::IVec2;
+ case GL_INT_VEC3: return GfxUniformType::IVec3;
+ case GL_INT_VEC4: return GfxUniformType::IVec4;
+ case GL_BOOL: return GfxUniformType::Bool;
+ case GL_FLOAT_MAT2: return GfxUniformType::Mat2;
+ case GL_FLOAT_MAT3: return GfxUniformType::Mat3;
+ case GL_FLOAT_MAT4: return GfxUniformType::Mat4;
+ case GL_SAMPLER_2D: return GfxUniformType::Sampler2D;
+ case GL_SAMPLER_CUBE: return GfxUniformType::SamplerCube;
+ default: return GfxUniformType::Unknown;
+ }
+}
+
+} // namespace
+
+void Shader::reflectActiveUniforms() {
+ activeUniforms_.clear();
+ if (programId_ == 0) return;
+
+ GLint count = 0;
+ glGetProgramiv(programId_, GL_ACTIVE_UNIFORMS, &count);
+ if (count <= 0) return;
+
+ GLint maxNameLen = 0;
+ glGetProgramiv(programId_, GL_ACTIVE_UNIFORM_MAX_LENGTH, &maxNameLen);
+ if (maxNameLen <= 0) maxNameLen = 64;
+
+ std::string nameBuf(static_cast<size_t>(maxNameLen), '\0');
+ activeUniforms_.reserve(static_cast<size_t>(count));
+
+ for (GLint i = 0; i < count; ++i) {
+ GLsizei nameLen = 0;
+ GLint size = 0;
+ GLenum type = 0;
+ glGetActiveUniform(programId_, static_cast<GLuint>(i),
+ static_cast<GLsizei>(maxNameLen), &nameLen,
+ &size, &type, nameBuf.data());
+
+ std::string name(nameBuf.data(), static_cast<size_t>(nameLen));
+ // Strip "[0]" suffix so callers look up arrays by their declared name.
+ const auto bracket = name.find('[');
+ if (bracket != std::string::npos) {
+ name.erase(bracket);
+ }
+
+ GfxUniformInfo info;
+ info.type = shaderUniformTypeFromGL(type);
+ info.location = glGetUniformLocation(programId_, name.c_str());
+ info.arraySize = size > 0 ? static_cast<u32>(size) : 1u;
+ info.name = std::move(name);
+ activeUniforms_.push_back(std::move(info));
+ }
+}
+
i32 Shader::getUniformLocation(const std::string& name) const {
auto [it, inserted] = uniformCache_.emplace(name, -1);
if (inserted) {
it->second = glGetUniformLocation(programId_, name.c_str());
+ if (it->second < 0) {
+ ES_LOG_WARN("Shader {}: uniform '{}' not found (typo or optimized out)",
+ programId_, name);
+ }
}
return it->second;
}
diff --git a/src/esengine/renderer/Shader.hpp b/src/esengine/renderer/Shader.hpp
index 908fbb85b..281ea17b2 100644
--- a/src/esengine/renderer/Shader.hpp
+++ b/src/esengine/renderer/Shader.hpp
@@ -19,11 +19,13 @@
// Project includes
#include "../core/Types.hpp"
#include "../math/Math.hpp"
+#include "GfxEnums.hpp"
// Standard library
#include <initializer_list>
#include <string>
#include <unordered_map>
+#include <vector>
namespace esengine {
@@ -141,6 +143,9 @@ public:
i32 getUniformLocation(const std::string& name) const;
+ /** @brief Returns reflected metadata for every active uniform, populated at link time */
+ const std::vector<GfxUniformInfo>& getActiveUniforms() const { return activeUniforms_; }
+
// =========================================================================
// State
// =========================================================================
@@ -167,11 +172,14 @@ private:
bool compile(const std::string& vertexSrc, const std::string& fragmentSrc,
std::initializer_list<AttribBinding> bindings = {});
+ void reflectActiveUniforms();
+
u32 programId_ = 0;
/** @brief Cached uniform locations (mutable for const uniform setters) */
mutable std::unordered_map<std::string, i32> uniformCache_;
mutable std::unordered_map<std::string, i32> attribCache_;
+ std::vector<GfxUniformInfo> activeUniforms_;
};
// =============================================================================