From c6db4fbb304be2da72653c0a8c89092cac1c7962 Mon Sep 17 00:00:00 2001 From: edvin Date: Wed, 18 Sep 2024 14:13:55 +0200 Subject: [PATCH 1/4] renderer: Update syntax of GlUniformBuffer to match that of GlUniformInput --- libopenage/renderer/opengl/uniform_buffer.cpp | 51 +++++++++---------- libopenage/renderer/opengl/uniform_buffer.h | 34 ++++++------- libopenage/renderer/uniform_buffer.h | 32 ++++++------ libopenage/renderer/uniform_input.cpp | 34 ++++++------- 4 files changed, 75 insertions(+), 76 deletions(-) diff --git a/libopenage/renderer/opengl/uniform_buffer.cpp b/libopenage/renderer/opengl/uniform_buffer.cpp index 661f9e3173..3f16f1fdd6 100644 --- a/libopenage/renderer/opengl/uniform_buffer.cpp +++ b/libopenage/renderer/opengl/uniform_buffer.cpp @@ -80,8 +80,8 @@ std::shared_ptr GlUniformBuffer::new_unif_in() { return in; } -void GlUniformBuffer::set_unif(std::shared_ptr const &in, const char *unif, void const *val, GLenum type) { - auto unif_in = std::dynamic_pointer_cast(in); +void GlUniformBuffer::set_unif(UniformBufferInput &in, const char *unif, void const *val, GLenum type) { + auto &unif_in = dynamic_cast(in); auto uniform = this->uniforms.find(unif); ENSURE(uniform != std::end(this->uniforms), @@ -96,83 +96,82 @@ void GlUniformBuffer::set_unif(std::shared_ptr const &in, co ENSURE(size == unif_data.size, "Tried to set uniform " << unif << " to a value of the wrong size."); - auto update_off = unif_in->update_offs.find(unif); - if (update_off != std::end(unif_in->update_offs)) [[likely]] { // always used after the uniform value is written once - // already wrote to this uniform since last upload + auto update_off = unif_in.update_offs.find(unif); + if (update_off != std::end(unif_in.update_offs)) [[likely]] { // always used after the uniform value is written once // already wrote to this uniform since last upload size_t off = update_off->second; - memcpy(unif_in->update_data.data() + off, val, size); + memcpy(unif_in.update_data.data() + off, val, size); } else { // first time writing to this uniform since last upload, so // extend the buffer before storing the uniform value - size_t prev_size = unif_in->update_data.size(); - unif_in->update_data.resize(prev_size + size); - memcpy(unif_in->update_data.data() + prev_size, val, size); - unif_in->update_offs.emplace(unif, prev_size); + size_t prev_size = unif_in.update_data.size(); + unif_in.update_data.resize(prev_size + size); + memcpy(unif_in.update_data.data() + prev_size, val, size); + unif_in.update_offs.emplace(unif, prev_size); } } -void GlUniformBuffer::set_i32(std::shared_ptr const &in, const char *unif, int32_t val) { +void GlUniformBuffer::set_i32(UniformBufferInput &in, const char *unif, int32_t val) { this->set_unif(in, unif, &val, GL_INT); } -void GlUniformBuffer::set_u32(std::shared_ptr const &in, const char *unif, uint32_t val) { +void GlUniformBuffer::set_u32(UniformBufferInput &in, const char *unif, uint32_t val) { this->set_unif(in, unif, &val, GL_UNSIGNED_INT); } -void GlUniformBuffer::set_f32(std::shared_ptr const &in, const char *unif, float val) { +void GlUniformBuffer::set_f32(UniformBufferInput &in, const char *unif, float val) { this->set_unif(in, unif, &val, GL_FLOAT); } -void GlUniformBuffer::set_f64(std::shared_ptr const &in, const char *unif, double val) { +void GlUniformBuffer::set_f64(UniformBufferInput &in, const char *unif, double val) { this->set_unif(in, unif, &val, GL_DOUBLE); } -void GlUniformBuffer::set_bool(std::shared_ptr const &in, const char *unif, bool val) { +void GlUniformBuffer::set_bool(UniformBufferInput &in, const char *unif, bool val) { this->set_unif(in, unif, &val, GL_BOOL); } -void GlUniformBuffer::set_v2f32(std::shared_ptr const &in, const char *unif, Eigen::Vector2f const &val) { +void GlUniformBuffer::set_v2f32(UniformBufferInput &in, const char *unif, Eigen::Vector2f const &val) { this->set_unif(in, unif, val.data(), GL_FLOAT_VEC2); } -void GlUniformBuffer::set_v3f32(std::shared_ptr const &in, const char *unif, Eigen::Vector3f const &val) { +void GlUniformBuffer::set_v3f32(UniformBufferInput &in, const char *unif, Eigen::Vector3f const &val) { this->set_unif(in, unif, val.data(), GL_FLOAT_VEC3); } -void GlUniformBuffer::set_v4f32(std::shared_ptr const &in, const char *unif, Eigen::Vector4f const &val) { +void GlUniformBuffer::set_v4f32(UniformBufferInput &in, const char *unif, Eigen::Vector4f const &val) { this->set_unif(in, unif, val.data(), GL_FLOAT_VEC4); } -void GlUniformBuffer::set_v2i32(std::shared_ptr const &in, const char *unif, Eigen::Vector2i const &val) { +void GlUniformBuffer::set_v2i32(UniformBufferInput &in, const char *unif, Eigen::Vector2i const &val) { this->set_unif(in, unif, val.data(), GL_INT_VEC2); } -void GlUniformBuffer::set_v3i32(std::shared_ptr const &in, const char *unif, Eigen::Vector3i const &val) { +void GlUniformBuffer::set_v3i32(UniformBufferInput &in, const char *unif, Eigen::Vector3i const &val) { this->set_unif(in, unif, val.data(), GL_INT_VEC3); } -void GlUniformBuffer::set_v4i32(std::shared_ptr const &in, const char *unif, Eigen::Vector4i const &val) { +void GlUniformBuffer::set_v4i32(UniformBufferInput &in, const char *unif, Eigen::Vector4i const &val) { this->set_unif(in, unif, val.data(), GL_INT_VEC4); } -void GlUniformBuffer::set_v2ui32(std::shared_ptr const &in, const char *unif, Eigen::Vector2 const &val) { +void GlUniformBuffer::set_v2ui32(UniformBufferInput &in, const char *unif, Eigen::Vector2 const &val) { this->set_unif(in, unif, val.data(), GL_UNSIGNED_INT_VEC2); } -void GlUniformBuffer::set_v3ui32(std::shared_ptr const &in, const char *unif, Eigen::Vector3 const &val) { +void GlUniformBuffer::set_v3ui32(UniformBufferInput &in, const char *unif, Eigen::Vector3 const &val) { this->set_unif(in, unif, val.data(), GL_UNSIGNED_INT_VEC3); } -void GlUniformBuffer::set_v4ui32(std::shared_ptr const &in, const char *unif, Eigen::Vector4 const &val) { +void GlUniformBuffer::set_v4ui32(UniformBufferInput &in, const char *unif, Eigen::Vector4 const &val) { this->set_unif(in, unif, val.data(), GL_UNSIGNED_INT_VEC4); } -void GlUniformBuffer::set_m4f32(std::shared_ptr const &in, const char *unif, Eigen::Matrix4f const &val) { +void GlUniformBuffer::set_m4f32(UniformBufferInput &in, const char *unif, Eigen::Matrix4f const &val) { this->set_unif(in, unif, val.data(), GL_FLOAT_MAT4); } -void GlUniformBuffer::set_tex(std::shared_ptr const &in, const char *unif, std::shared_ptr const &val) { +void GlUniformBuffer::set_tex(UniformBufferInput &in, const char *unif, std::shared_ptr const &val) { auto tex = std::dynamic_pointer_cast(val); GLuint handle = tex->get_handle(); this->set_unif(in, unif, &handle, GL_SAMPLER_2D); diff --git a/libopenage/renderer/opengl/uniform_buffer.h b/libopenage/renderer/opengl/uniform_buffer.h index 6c7ca3d869..f1981fd19e 100644 --- a/libopenage/renderer/opengl/uniform_buffer.h +++ b/libopenage/renderer/opengl/uniform_buffer.h @@ -51,22 +51,22 @@ class GlUniformBuffer final : public UniformBuffer protected: std::shared_ptr new_unif_in() override; - void set_i32(std::shared_ptr const &, const char *, int32_t) override; - void set_u32(std::shared_ptr const &, const char *, uint32_t) override; - void set_f32(std::shared_ptr const &, const char *, float) override; - void set_f64(std::shared_ptr const &, const char *, double) override; - void set_bool(std::shared_ptr const &, const char *, bool) override; - void set_v2f32(std::shared_ptr const &, const char *, Eigen::Vector2f const &) override; - void set_v3f32(std::shared_ptr const &, const char *, Eigen::Vector3f const &) override; - void set_v4f32(std::shared_ptr const &, const char *, Eigen::Vector4f const &) override; - void set_v2i32(std::shared_ptr const &, const char *, Eigen::Vector2i const &) override; - void set_v3i32(std::shared_ptr const &, const char *, Eigen::Vector3i const &) override; - void set_v4i32(std::shared_ptr const &, const char *, Eigen::Vector4i const &) override; - void set_v2ui32(std::shared_ptr const &, const char *, Eigen::Vector2 const &) override; - void set_v3ui32(std::shared_ptr const &, const char *, Eigen::Vector3 const &) override; - void set_v4ui32(std::shared_ptr const &, const char *, Eigen::Vector4 const &) override; - void set_m4f32(std::shared_ptr const &, const char *, Eigen::Matrix4f const &) override; - void set_tex(std::shared_ptr const &, const char *, std::shared_ptr const &) override; + void set_i32(UniformBufferInput &in, const char *, int32_t) override; + void set_u32(UniformBufferInput &in, const char *, uint32_t) override; + void set_f32(UniformBufferInput &in, const char *, float) override; + void set_f64(UniformBufferInput &in, const char *, double) override; + void set_bool(UniformBufferInput &in, const char *, bool) override; + void set_v2f32(UniformBufferInput &in, const char *, Eigen::Vector2f const &) override; + void set_v3f32(UniformBufferInput &in, const char *, Eigen::Vector3f const &) override; + void set_v4f32(UniformBufferInput &in, const char *, Eigen::Vector4f const &) override; + void set_v2i32(UniformBufferInput &in, const char *, Eigen::Vector2i const &) override; + void set_v3i32(UniformBufferInput &in, const char *, Eigen::Vector3i const &) override; + void set_v4i32(UniformBufferInput &in, const char *, Eigen::Vector4i const &) override; + void set_v2ui32(UniformBufferInput &in, const char *, Eigen::Vector2 const &) override; + void set_v3ui32(UniformBufferInput &in, const char *, Eigen::Vector3 const &) override; + void set_v4ui32(UniformBufferInput &in, const char *, Eigen::Vector4 const &) override; + void set_m4f32(UniformBufferInput &in, const char *, Eigen::Matrix4f const &) override; + void set_tex(UniformBufferInput &in, const char *, std::shared_ptr const &) override; private: /** @@ -80,7 +80,7 @@ class GlUniformBuffer final : public UniformBuffer * @param val Pointer to the value to update the uniform with. * @param type Type of the uniform. */ - void set_unif(std::shared_ptr const &in, + void set_unif(UniformBufferInput &in, const char *name, void const *val, GLenum type); diff --git a/libopenage/renderer/uniform_buffer.h b/libopenage/renderer/uniform_buffer.h index 85040a4976..c91edf1132 100644 --- a/libopenage/renderer/uniform_buffer.h +++ b/libopenage/renderer/uniform_buffer.h @@ -50,22 +50,22 @@ class UniformBuffer : public std::enable_shared_from_this { protected: virtual std::shared_ptr new_unif_in() = 0; - virtual void set_i32(std::shared_ptr const &, const char *, int32_t) = 0; - virtual void set_u32(std::shared_ptr const &, const char *, uint32_t) = 0; - virtual void set_f32(std::shared_ptr const &, const char *, float) = 0; - virtual void set_f64(std::shared_ptr const &, const char *, double) = 0; - virtual void set_bool(std::shared_ptr const &, const char *, bool) = 0; - virtual void set_v2f32(std::shared_ptr const &, const char *, Eigen::Vector2f const &) = 0; - virtual void set_v3f32(std::shared_ptr const &, const char *, Eigen::Vector3f const &) = 0; - virtual void set_v4f32(std::shared_ptr const &, const char *, Eigen::Vector4f const &) = 0; - virtual void set_v2i32(std::shared_ptr const &, const char *, Eigen::Vector2i const &) = 0; - virtual void set_v3i32(std::shared_ptr const &, const char *, Eigen::Vector3i const &) = 0; - virtual void set_v4i32(std::shared_ptr const &, const char *, Eigen::Vector4i const &) = 0; - virtual void set_v2ui32(std::shared_ptr const &, const char *, Eigen::Vector2 const &) = 0; - virtual void set_v3ui32(std::shared_ptr const &, const char *, Eigen::Vector3 const &) = 0; - virtual void set_v4ui32(std::shared_ptr const &, const char *, Eigen::Vector4 const &) = 0; - virtual void set_m4f32(std::shared_ptr const &, const char *, Eigen::Matrix4f const &) = 0; - virtual void set_tex(std::shared_ptr const &, const char *, std::shared_ptr const &) = 0; + virtual void set_i32(UniformBufferInput &in, const char *, int32_t) = 0; + virtual void set_u32(UniformBufferInput &in, const char *, uint32_t) = 0; + virtual void set_f32(UniformBufferInput &in, const char *, float) = 0; + virtual void set_f64(UniformBufferInput &in, const char *, double) = 0; + virtual void set_bool(UniformBufferInput &in, const char *, bool) = 0; + virtual void set_v2f32(UniformBufferInput &in, const char *, Eigen::Vector2f const &) = 0; + virtual void set_v3f32(UniformBufferInput &in, const char *, Eigen::Vector3f const &) = 0; + virtual void set_v4f32(UniformBufferInput &in, const char *, Eigen::Vector4f const &) = 0; + virtual void set_v2i32(UniformBufferInput &in, const char *, Eigen::Vector2i const &) = 0; + virtual void set_v3i32(UniformBufferInput &in, const char *, Eigen::Vector3i const &) = 0; + virtual void set_v4i32(UniformBufferInput &in, const char *, Eigen::Vector4i const &) = 0; + virtual void set_v2ui32(UniformBufferInput &in, const char *, Eigen::Vector2 const &) = 0; + virtual void set_v3ui32(UniformBufferInput &in, const char *, Eigen::Vector3 const &) = 0; + virtual void set_v4ui32(UniformBufferInput &in, const char *, Eigen::Vector4 const &) = 0; + virtual void set_m4f32(UniformBufferInput &in, const char *, Eigen::Matrix4f const &) = 0; + virtual void set_tex(UniformBufferInput &in, const char *, std::shared_ptr const &) = 0; }; } // namespace openage::renderer diff --git a/libopenage/renderer/uniform_input.cpp b/libopenage/renderer/uniform_input.cpp index ee42202faf..b3abbe7ba8 100644 --- a/libopenage/renderer/uniform_input.cpp +++ b/libopenage/renderer/uniform_input.cpp @@ -157,71 +157,71 @@ UniformBufferInput::UniformBufferInput(std::shared_ptr const &buf void UniformBufferInput::update() {} void UniformBufferInput::update(const char *unif, int32_t val) { - this->buffer->set_i32(this->shared_from_this(), unif, val); + this->buffer->set_i32(*this, unif, val); } void UniformBufferInput::update(const char *unif, uint32_t val) { - this->buffer->set_u32(this->shared_from_this(), unif, val); + this->buffer->set_u32(*this, unif, val); } void UniformBufferInput::update(const char *unif, float val) { - this->buffer->set_f32(this->shared_from_this(), unif, val); + this->buffer->set_f32(*this, unif, val); } void UniformBufferInput::update(const char *unif, double val) { - this->buffer->set_f64(this->shared_from_this(), unif, val); + this->buffer->set_f64(*this, unif, val); } void UniformBufferInput::update(const char *unif, bool val) { - this->buffer->set_bool(this->shared_from_this(), unif, val); + this->buffer->set_bool(*this, unif, val); } void UniformBufferInput::update(const char *unif, Eigen::Vector2f const &val) { - this->buffer->set_v2f32(this->shared_from_this(), unif, val); + this->buffer->set_v2f32(*this, unif, val); } void UniformBufferInput::update(const char *unif, Eigen::Vector3f const &val) { - this->buffer->set_v3f32(this->shared_from_this(), unif, val); + this->buffer->set_v3f32(*this, unif, val); } void UniformBufferInput::update(const char *unif, Eigen::Vector4f const &val) { - this->buffer->set_v4f32(this->shared_from_this(), unif, val); + this->buffer->set_v4f32(*this, unif, val); } void UniformBufferInput::update(const char *unif, Eigen::Vector2i const &val) { - this->buffer->set_v2i32(this->shared_from_this(), unif, val); + this->buffer->set_v2i32(*this, unif, val); } void UniformBufferInput::update(const char *unif, Eigen::Vector3i const &val) { - this->buffer->set_v3i32(this->shared_from_this(), unif, val); + this->buffer->set_v3i32(*this, unif, val); } void UniformBufferInput::update(const char *unif, Eigen::Vector4i const &val) { - this->buffer->set_v4i32(this->shared_from_this(), unif, val); + this->buffer->set_v4i32(*this, unif, val); } void UniformBufferInput::update(const char *unif, Eigen::Vector2 const &val) { - this->buffer->set_v2ui32(this->shared_from_this(), unif, val); + this->buffer->set_v2ui32(*this, unif, val); } void UniformBufferInput::update(const char *unif, Eigen::Vector3 const &val) { - this->buffer->set_v3ui32(this->shared_from_this(), unif, val); + this->buffer->set_v3ui32(*this, unif, val); } void UniformBufferInput::update(const char *unif, Eigen::Vector4 const &val) { - this->buffer->set_v4ui32(this->shared_from_this(), unif, val); + this->buffer->set_v4ui32(*this, unif, val); } void UniformBufferInput::update(const char *unif, std::shared_ptr const &val) { - this->buffer->set_tex(this->shared_from_this(), unif, val); + this->buffer->set_tex(*this, unif, val); } void UniformBufferInput::update(const char *unif, std::shared_ptr &val) { - this->buffer->set_tex(this->shared_from_this(), unif, val); + this->buffer->set_tex(*this, unif, val); } void UniformBufferInput::update(const char *unif, Eigen::Matrix4f const &val) { - this->buffer->set_m4f32(this->shared_from_this(), unif, val); + this->buffer->set_m4f32(*this, unif, val); } } // namespace openage::renderer From 6256c66c6e35b9db7838012ea7d3a18a5b102dee Mon Sep 17 00:00:00 2001 From: Edvin Date: Wed, 18 Sep 2024 14:13:55 +0200 Subject: [PATCH 2/4] renderer: Update syntax of GlUniformBuffer to match that of GlUniformInput --- libopenage/renderer/opengl/uniform_buffer.cpp | 51 +++++++++---------- libopenage/renderer/opengl/uniform_buffer.h | 34 ++++++------- libopenage/renderer/uniform_buffer.h | 32 ++++++------ libopenage/renderer/uniform_input.cpp | 34 ++++++------- 4 files changed, 75 insertions(+), 76 deletions(-) diff --git a/libopenage/renderer/opengl/uniform_buffer.cpp b/libopenage/renderer/opengl/uniform_buffer.cpp index 661f9e3173..3f16f1fdd6 100644 --- a/libopenage/renderer/opengl/uniform_buffer.cpp +++ b/libopenage/renderer/opengl/uniform_buffer.cpp @@ -80,8 +80,8 @@ std::shared_ptr GlUniformBuffer::new_unif_in() { return in; } -void GlUniformBuffer::set_unif(std::shared_ptr const &in, const char *unif, void const *val, GLenum type) { - auto unif_in = std::dynamic_pointer_cast(in); +void GlUniformBuffer::set_unif(UniformBufferInput &in, const char *unif, void const *val, GLenum type) { + auto &unif_in = dynamic_cast(in); auto uniform = this->uniforms.find(unif); ENSURE(uniform != std::end(this->uniforms), @@ -96,83 +96,82 @@ void GlUniformBuffer::set_unif(std::shared_ptr const &in, co ENSURE(size == unif_data.size, "Tried to set uniform " << unif << " to a value of the wrong size."); - auto update_off = unif_in->update_offs.find(unif); - if (update_off != std::end(unif_in->update_offs)) [[likely]] { // always used after the uniform value is written once - // already wrote to this uniform since last upload + auto update_off = unif_in.update_offs.find(unif); + if (update_off != std::end(unif_in.update_offs)) [[likely]] { // always used after the uniform value is written once // already wrote to this uniform since last upload size_t off = update_off->second; - memcpy(unif_in->update_data.data() + off, val, size); + memcpy(unif_in.update_data.data() + off, val, size); } else { // first time writing to this uniform since last upload, so // extend the buffer before storing the uniform value - size_t prev_size = unif_in->update_data.size(); - unif_in->update_data.resize(prev_size + size); - memcpy(unif_in->update_data.data() + prev_size, val, size); - unif_in->update_offs.emplace(unif, prev_size); + size_t prev_size = unif_in.update_data.size(); + unif_in.update_data.resize(prev_size + size); + memcpy(unif_in.update_data.data() + prev_size, val, size); + unif_in.update_offs.emplace(unif, prev_size); } } -void GlUniformBuffer::set_i32(std::shared_ptr const &in, const char *unif, int32_t val) { +void GlUniformBuffer::set_i32(UniformBufferInput &in, const char *unif, int32_t val) { this->set_unif(in, unif, &val, GL_INT); } -void GlUniformBuffer::set_u32(std::shared_ptr const &in, const char *unif, uint32_t val) { +void GlUniformBuffer::set_u32(UniformBufferInput &in, const char *unif, uint32_t val) { this->set_unif(in, unif, &val, GL_UNSIGNED_INT); } -void GlUniformBuffer::set_f32(std::shared_ptr const &in, const char *unif, float val) { +void GlUniformBuffer::set_f32(UniformBufferInput &in, const char *unif, float val) { this->set_unif(in, unif, &val, GL_FLOAT); } -void GlUniformBuffer::set_f64(std::shared_ptr const &in, const char *unif, double val) { +void GlUniformBuffer::set_f64(UniformBufferInput &in, const char *unif, double val) { this->set_unif(in, unif, &val, GL_DOUBLE); } -void GlUniformBuffer::set_bool(std::shared_ptr const &in, const char *unif, bool val) { +void GlUniformBuffer::set_bool(UniformBufferInput &in, const char *unif, bool val) { this->set_unif(in, unif, &val, GL_BOOL); } -void GlUniformBuffer::set_v2f32(std::shared_ptr const &in, const char *unif, Eigen::Vector2f const &val) { +void GlUniformBuffer::set_v2f32(UniformBufferInput &in, const char *unif, Eigen::Vector2f const &val) { this->set_unif(in, unif, val.data(), GL_FLOAT_VEC2); } -void GlUniformBuffer::set_v3f32(std::shared_ptr const &in, const char *unif, Eigen::Vector3f const &val) { +void GlUniformBuffer::set_v3f32(UniformBufferInput &in, const char *unif, Eigen::Vector3f const &val) { this->set_unif(in, unif, val.data(), GL_FLOAT_VEC3); } -void GlUniformBuffer::set_v4f32(std::shared_ptr const &in, const char *unif, Eigen::Vector4f const &val) { +void GlUniformBuffer::set_v4f32(UniformBufferInput &in, const char *unif, Eigen::Vector4f const &val) { this->set_unif(in, unif, val.data(), GL_FLOAT_VEC4); } -void GlUniformBuffer::set_v2i32(std::shared_ptr const &in, const char *unif, Eigen::Vector2i const &val) { +void GlUniformBuffer::set_v2i32(UniformBufferInput &in, const char *unif, Eigen::Vector2i const &val) { this->set_unif(in, unif, val.data(), GL_INT_VEC2); } -void GlUniformBuffer::set_v3i32(std::shared_ptr const &in, const char *unif, Eigen::Vector3i const &val) { +void GlUniformBuffer::set_v3i32(UniformBufferInput &in, const char *unif, Eigen::Vector3i const &val) { this->set_unif(in, unif, val.data(), GL_INT_VEC3); } -void GlUniformBuffer::set_v4i32(std::shared_ptr const &in, const char *unif, Eigen::Vector4i const &val) { +void GlUniformBuffer::set_v4i32(UniformBufferInput &in, const char *unif, Eigen::Vector4i const &val) { this->set_unif(in, unif, val.data(), GL_INT_VEC4); } -void GlUniformBuffer::set_v2ui32(std::shared_ptr const &in, const char *unif, Eigen::Vector2 const &val) { +void GlUniformBuffer::set_v2ui32(UniformBufferInput &in, const char *unif, Eigen::Vector2 const &val) { this->set_unif(in, unif, val.data(), GL_UNSIGNED_INT_VEC2); } -void GlUniformBuffer::set_v3ui32(std::shared_ptr const &in, const char *unif, Eigen::Vector3 const &val) { +void GlUniformBuffer::set_v3ui32(UniformBufferInput &in, const char *unif, Eigen::Vector3 const &val) { this->set_unif(in, unif, val.data(), GL_UNSIGNED_INT_VEC3); } -void GlUniformBuffer::set_v4ui32(std::shared_ptr const &in, const char *unif, Eigen::Vector4 const &val) { +void GlUniformBuffer::set_v4ui32(UniformBufferInput &in, const char *unif, Eigen::Vector4 const &val) { this->set_unif(in, unif, val.data(), GL_UNSIGNED_INT_VEC4); } -void GlUniformBuffer::set_m4f32(std::shared_ptr const &in, const char *unif, Eigen::Matrix4f const &val) { +void GlUniformBuffer::set_m4f32(UniformBufferInput &in, const char *unif, Eigen::Matrix4f const &val) { this->set_unif(in, unif, val.data(), GL_FLOAT_MAT4); } -void GlUniformBuffer::set_tex(std::shared_ptr const &in, const char *unif, std::shared_ptr const &val) { +void GlUniformBuffer::set_tex(UniformBufferInput &in, const char *unif, std::shared_ptr const &val) { auto tex = std::dynamic_pointer_cast(val); GLuint handle = tex->get_handle(); this->set_unif(in, unif, &handle, GL_SAMPLER_2D); diff --git a/libopenage/renderer/opengl/uniform_buffer.h b/libopenage/renderer/opengl/uniform_buffer.h index 6c7ca3d869..f1981fd19e 100644 --- a/libopenage/renderer/opengl/uniform_buffer.h +++ b/libopenage/renderer/opengl/uniform_buffer.h @@ -51,22 +51,22 @@ class GlUniformBuffer final : public UniformBuffer protected: std::shared_ptr new_unif_in() override; - void set_i32(std::shared_ptr const &, const char *, int32_t) override; - void set_u32(std::shared_ptr const &, const char *, uint32_t) override; - void set_f32(std::shared_ptr const &, const char *, float) override; - void set_f64(std::shared_ptr const &, const char *, double) override; - void set_bool(std::shared_ptr const &, const char *, bool) override; - void set_v2f32(std::shared_ptr const &, const char *, Eigen::Vector2f const &) override; - void set_v3f32(std::shared_ptr const &, const char *, Eigen::Vector3f const &) override; - void set_v4f32(std::shared_ptr const &, const char *, Eigen::Vector4f const &) override; - void set_v2i32(std::shared_ptr const &, const char *, Eigen::Vector2i const &) override; - void set_v3i32(std::shared_ptr const &, const char *, Eigen::Vector3i const &) override; - void set_v4i32(std::shared_ptr const &, const char *, Eigen::Vector4i const &) override; - void set_v2ui32(std::shared_ptr const &, const char *, Eigen::Vector2 const &) override; - void set_v3ui32(std::shared_ptr const &, const char *, Eigen::Vector3 const &) override; - void set_v4ui32(std::shared_ptr const &, const char *, Eigen::Vector4 const &) override; - void set_m4f32(std::shared_ptr const &, const char *, Eigen::Matrix4f const &) override; - void set_tex(std::shared_ptr const &, const char *, std::shared_ptr const &) override; + void set_i32(UniformBufferInput &in, const char *, int32_t) override; + void set_u32(UniformBufferInput &in, const char *, uint32_t) override; + void set_f32(UniformBufferInput &in, const char *, float) override; + void set_f64(UniformBufferInput &in, const char *, double) override; + void set_bool(UniformBufferInput &in, const char *, bool) override; + void set_v2f32(UniformBufferInput &in, const char *, Eigen::Vector2f const &) override; + void set_v3f32(UniformBufferInput &in, const char *, Eigen::Vector3f const &) override; + void set_v4f32(UniformBufferInput &in, const char *, Eigen::Vector4f const &) override; + void set_v2i32(UniformBufferInput &in, const char *, Eigen::Vector2i const &) override; + void set_v3i32(UniformBufferInput &in, const char *, Eigen::Vector3i const &) override; + void set_v4i32(UniformBufferInput &in, const char *, Eigen::Vector4i const &) override; + void set_v2ui32(UniformBufferInput &in, const char *, Eigen::Vector2 const &) override; + void set_v3ui32(UniformBufferInput &in, const char *, Eigen::Vector3 const &) override; + void set_v4ui32(UniformBufferInput &in, const char *, Eigen::Vector4 const &) override; + void set_m4f32(UniformBufferInput &in, const char *, Eigen::Matrix4f const &) override; + void set_tex(UniformBufferInput &in, const char *, std::shared_ptr const &) override; private: /** @@ -80,7 +80,7 @@ class GlUniformBuffer final : public UniformBuffer * @param val Pointer to the value to update the uniform with. * @param type Type of the uniform. */ - void set_unif(std::shared_ptr const &in, + void set_unif(UniformBufferInput &in, const char *name, void const *val, GLenum type); diff --git a/libopenage/renderer/uniform_buffer.h b/libopenage/renderer/uniform_buffer.h index 85040a4976..c91edf1132 100644 --- a/libopenage/renderer/uniform_buffer.h +++ b/libopenage/renderer/uniform_buffer.h @@ -50,22 +50,22 @@ class UniformBuffer : public std::enable_shared_from_this { protected: virtual std::shared_ptr new_unif_in() = 0; - virtual void set_i32(std::shared_ptr const &, const char *, int32_t) = 0; - virtual void set_u32(std::shared_ptr const &, const char *, uint32_t) = 0; - virtual void set_f32(std::shared_ptr const &, const char *, float) = 0; - virtual void set_f64(std::shared_ptr const &, const char *, double) = 0; - virtual void set_bool(std::shared_ptr const &, const char *, bool) = 0; - virtual void set_v2f32(std::shared_ptr const &, const char *, Eigen::Vector2f const &) = 0; - virtual void set_v3f32(std::shared_ptr const &, const char *, Eigen::Vector3f const &) = 0; - virtual void set_v4f32(std::shared_ptr const &, const char *, Eigen::Vector4f const &) = 0; - virtual void set_v2i32(std::shared_ptr const &, const char *, Eigen::Vector2i const &) = 0; - virtual void set_v3i32(std::shared_ptr const &, const char *, Eigen::Vector3i const &) = 0; - virtual void set_v4i32(std::shared_ptr const &, const char *, Eigen::Vector4i const &) = 0; - virtual void set_v2ui32(std::shared_ptr const &, const char *, Eigen::Vector2 const &) = 0; - virtual void set_v3ui32(std::shared_ptr const &, const char *, Eigen::Vector3 const &) = 0; - virtual void set_v4ui32(std::shared_ptr const &, const char *, Eigen::Vector4 const &) = 0; - virtual void set_m4f32(std::shared_ptr const &, const char *, Eigen::Matrix4f const &) = 0; - virtual void set_tex(std::shared_ptr const &, const char *, std::shared_ptr const &) = 0; + virtual void set_i32(UniformBufferInput &in, const char *, int32_t) = 0; + virtual void set_u32(UniformBufferInput &in, const char *, uint32_t) = 0; + virtual void set_f32(UniformBufferInput &in, const char *, float) = 0; + virtual void set_f64(UniformBufferInput &in, const char *, double) = 0; + virtual void set_bool(UniformBufferInput &in, const char *, bool) = 0; + virtual void set_v2f32(UniformBufferInput &in, const char *, Eigen::Vector2f const &) = 0; + virtual void set_v3f32(UniformBufferInput &in, const char *, Eigen::Vector3f const &) = 0; + virtual void set_v4f32(UniformBufferInput &in, const char *, Eigen::Vector4f const &) = 0; + virtual void set_v2i32(UniformBufferInput &in, const char *, Eigen::Vector2i const &) = 0; + virtual void set_v3i32(UniformBufferInput &in, const char *, Eigen::Vector3i const &) = 0; + virtual void set_v4i32(UniformBufferInput &in, const char *, Eigen::Vector4i const &) = 0; + virtual void set_v2ui32(UniformBufferInput &in, const char *, Eigen::Vector2 const &) = 0; + virtual void set_v3ui32(UniformBufferInput &in, const char *, Eigen::Vector3 const &) = 0; + virtual void set_v4ui32(UniformBufferInput &in, const char *, Eigen::Vector4 const &) = 0; + virtual void set_m4f32(UniformBufferInput &in, const char *, Eigen::Matrix4f const &) = 0; + virtual void set_tex(UniformBufferInput &in, const char *, std::shared_ptr const &) = 0; }; } // namespace openage::renderer diff --git a/libopenage/renderer/uniform_input.cpp b/libopenage/renderer/uniform_input.cpp index ee42202faf..b3abbe7ba8 100644 --- a/libopenage/renderer/uniform_input.cpp +++ b/libopenage/renderer/uniform_input.cpp @@ -157,71 +157,71 @@ UniformBufferInput::UniformBufferInput(std::shared_ptr const &buf void UniformBufferInput::update() {} void UniformBufferInput::update(const char *unif, int32_t val) { - this->buffer->set_i32(this->shared_from_this(), unif, val); + this->buffer->set_i32(*this, unif, val); } void UniformBufferInput::update(const char *unif, uint32_t val) { - this->buffer->set_u32(this->shared_from_this(), unif, val); + this->buffer->set_u32(*this, unif, val); } void UniformBufferInput::update(const char *unif, float val) { - this->buffer->set_f32(this->shared_from_this(), unif, val); + this->buffer->set_f32(*this, unif, val); } void UniformBufferInput::update(const char *unif, double val) { - this->buffer->set_f64(this->shared_from_this(), unif, val); + this->buffer->set_f64(*this, unif, val); } void UniformBufferInput::update(const char *unif, bool val) { - this->buffer->set_bool(this->shared_from_this(), unif, val); + this->buffer->set_bool(*this, unif, val); } void UniformBufferInput::update(const char *unif, Eigen::Vector2f const &val) { - this->buffer->set_v2f32(this->shared_from_this(), unif, val); + this->buffer->set_v2f32(*this, unif, val); } void UniformBufferInput::update(const char *unif, Eigen::Vector3f const &val) { - this->buffer->set_v3f32(this->shared_from_this(), unif, val); + this->buffer->set_v3f32(*this, unif, val); } void UniformBufferInput::update(const char *unif, Eigen::Vector4f const &val) { - this->buffer->set_v4f32(this->shared_from_this(), unif, val); + this->buffer->set_v4f32(*this, unif, val); } void UniformBufferInput::update(const char *unif, Eigen::Vector2i const &val) { - this->buffer->set_v2i32(this->shared_from_this(), unif, val); + this->buffer->set_v2i32(*this, unif, val); } void UniformBufferInput::update(const char *unif, Eigen::Vector3i const &val) { - this->buffer->set_v3i32(this->shared_from_this(), unif, val); + this->buffer->set_v3i32(*this, unif, val); } void UniformBufferInput::update(const char *unif, Eigen::Vector4i const &val) { - this->buffer->set_v4i32(this->shared_from_this(), unif, val); + this->buffer->set_v4i32(*this, unif, val); } void UniformBufferInput::update(const char *unif, Eigen::Vector2 const &val) { - this->buffer->set_v2ui32(this->shared_from_this(), unif, val); + this->buffer->set_v2ui32(*this, unif, val); } void UniformBufferInput::update(const char *unif, Eigen::Vector3 const &val) { - this->buffer->set_v3ui32(this->shared_from_this(), unif, val); + this->buffer->set_v3ui32(*this, unif, val); } void UniformBufferInput::update(const char *unif, Eigen::Vector4 const &val) { - this->buffer->set_v4ui32(this->shared_from_this(), unif, val); + this->buffer->set_v4ui32(*this, unif, val); } void UniformBufferInput::update(const char *unif, std::shared_ptr const &val) { - this->buffer->set_tex(this->shared_from_this(), unif, val); + this->buffer->set_tex(*this, unif, val); } void UniformBufferInput::update(const char *unif, std::shared_ptr &val) { - this->buffer->set_tex(this->shared_from_this(), unif, val); + this->buffer->set_tex(*this, unif, val); } void UniformBufferInput::update(const char *unif, Eigen::Matrix4f const &val) { - this->buffer->set_m4f32(this->shared_from_this(), unif, val); + this->buffer->set_m4f32(*this, unif, val); } } // namespace openage::renderer From 623554ed6930a6f6ac76dac4b676a604bbbacb28 Mon Sep 17 00:00:00 2001 From: edvin Date: Thu, 19 Sep 2024 14:03:29 +0200 Subject: [PATCH 3/4] Added syntax changes to GlUniformBufferInput class. --- copying.md | 1 + libopenage/renderer/opengl/uniform_buffer.cpp | 47 ++++++++++++------- libopenage/renderer/opengl/uniform_buffer.h | 12 +++++ libopenage/renderer/opengl/uniform_input.cpp | 23 ++++++++- libopenage/renderer/opengl/uniform_input.h | 27 +++++++---- 5 files changed, 84 insertions(+), 26 deletions(-) diff --git a/copying.md b/copying.md index fb6501bab0..1002c8321a 100644 --- a/copying.md +++ b/copying.md @@ -154,6 +154,7 @@ _the openage authors_ are: | Haoyang Bi | AyiStar | ayistar à outlook dawt com | | Michael Seibt | RoboSchmied | github à roboschmie dawt de | | Nikhil Ghosh | NikhilGhosh75 | nghosh606 à gmail dawt com | +| Edvin Lindholm | EdvinLndh | edvinlndh à gmail dawt com | If you're a first-time committer, add yourself to the above list. This is not just for legal reasons, but also to keep an overview of all those nicknames. diff --git a/libopenage/renderer/opengl/uniform_buffer.cpp b/libopenage/renderer/opengl/uniform_buffer.cpp index 3f16f1fdd6..c153d1e080 100644 --- a/libopenage/renderer/opengl/uniform_buffer.cpp +++ b/libopenage/renderer/opengl/uniform_buffer.cpp @@ -33,6 +33,12 @@ GlUniformBuffer::GlUniformBuffer(const std::shared_ptr &context, this->bind(); glBufferData(GL_UNIFORM_BUFFER, this->data_size, NULL, usage); + uniform_id_t unif_id = 0; + for (auto &uniform : uniforms) { + this->uniforms_by_name.insert(std::make_pair(uniform.first, unif_id)); + unif_id += 1; + } + glBindBufferRange(GL_UNIFORM_BUFFER, this->binding_point, *this->handle, 0, this->data_size); log::log(MSG(dbg) << "Created OpenGL uniform buffer (size: " @@ -51,14 +57,17 @@ void GlUniformBuffer::set_binding_point(GLuint binding_point) { void GlUniformBuffer::update_uniforms(std::shared_ptr const &unif_in) { auto glunif_in = std::dynamic_pointer_cast(unif_in); - ENSURE(glunif_in->get_buffer() == this->shared_from_this(), "Uniform input passed to different buffer than it was created with."); + ENSURE(glunif_in->get_buffer().get() == this, "Uniform input passed to different buffer than it was created with."); this->bind(); + const auto &update_offs = glunif_in->update_offs; uint8_t const *data = glunif_in->update_data.data(); - for (auto const &pair : glunif_in->update_offs) { - uint8_t const *ptr = data + pair.second; - auto unif_def = this->uniforms[pair.first]; + for (auto const &pair : this->uniforms_by_name) { + auto id = pair.second; + auto offset = update_offs[id]; + uint8_t const *ptr = data + offset.offset; + auto unif_def = this->uniforms.find(pair.first)->second; auto loc = unif_def.offset; auto size = unif_def.size; @@ -66,6 +75,10 @@ void GlUniformBuffer::update_uniforms(std::shared_ptr const } } +const std::unordered_map &GlUniformBuffer::get_uniforms() const { + return this->uniforms; +} + bool GlUniformBuffer::has_uniform(const char *unif) { return this->uniforms.count(unif) != 0; } @@ -83,6 +96,10 @@ std::shared_ptr GlUniformBuffer::new_unif_in() { void GlUniformBuffer::set_unif(UniformBufferInput &in, const char *unif, void const *val, GLenum type) { auto &unif_in = dynamic_cast(in); + auto unif_id = this->uniforms_by_name.find(unif)->second; + ENSURE(unif_id < this->uniforms.size(), + "Tried to set uniform with invalid ID " << unif_id); + auto uniform = this->uniforms.find(unif); ENSURE(uniform != std::end(this->uniforms), "Tried to set uniform " << unif << " that does not exist in the shader program."); @@ -96,18 +113,16 @@ void GlUniformBuffer::set_unif(UniformBufferInput &in, const char *unif, void co ENSURE(size == unif_data.size, "Tried to set uniform " << unif << " to a value of the wrong size."); - auto update_off = unif_in.update_offs.find(unif); - if (update_off != std::end(unif_in.update_offs)) [[likely]] { // always used after the uniform value is written once // already wrote to this uniform since last upload - size_t off = update_off->second; - memcpy(unif_in.update_data.data() + off, val, size); - } - else { - // first time writing to this uniform since last upload, so - // extend the buffer before storing the uniform value - size_t prev_size = unif_in.update_data.size(); - unif_in.update_data.resize(prev_size + size); - memcpy(unif_in.update_data.data() + prev_size, val, size); - unif_in.update_offs.emplace(unif, prev_size); + auto &update_off = unif_in.update_offs[unif_id]; + auto offset = update_off.offset; + memcpy(unif_in.update_data.data() + offset, val, size); + if (not update_off.used) [[unlikely]] { // only true if the uniform value was not set before + auto lower_bound = std::lower_bound( + std::begin(unif_in.used_uniforms), + std::end(unif_in.used_uniforms), + unif_id); + unif_in.used_uniforms.insert(lower_bound, unif_id); + update_off.used = true; } } diff --git a/libopenage/renderer/opengl/uniform_buffer.h b/libopenage/renderer/opengl/uniform_buffer.h index f1981fd19e..0fdfc7309d 100644 --- a/libopenage/renderer/opengl/uniform_buffer.h +++ b/libopenage/renderer/opengl/uniform_buffer.h @@ -33,6 +33,13 @@ class GlUniformBuffer final : public UniformBuffer */ GLuint get_binding_point() const; + /** + * Get the uniform buffers uniforms. + * + * @return Uniforms in the shader program. + */ + const std::unordered_map &get_uniforms() const; + /** * Set the binding point of the buffer. * @@ -90,6 +97,11 @@ class GlUniformBuffer final : public UniformBuffer */ std::unordered_map uniforms; + /** + * Maps uniform names to their ID (the index in the uniform vector). + */ + std::unordered_map uniforms_by_name; + /** * Size of the buffer (in bytes). */ diff --git a/libopenage/renderer/opengl/uniform_input.cpp b/libopenage/renderer/opengl/uniform_input.cpp index 111b33b879..0121cc751a 100644 --- a/libopenage/renderer/opengl/uniform_input.cpp +++ b/libopenage/renderer/opengl/uniform_input.cpp @@ -4,6 +4,7 @@ #include "renderer/opengl/lookup.h" #include "renderer/opengl/shader_program.h" +#include "renderer/opengl/uniform_buffer.h" #include "renderer/opengl/util.h" @@ -28,7 +29,25 @@ GlUniformInput::GlUniformInput(const std::shared_ptr &prog) : this->update_data.resize(offset); } -GlUniformBufferInput::GlUniformBufferInput(std::shared_ptr const &buffer) : - UniformBufferInput{buffer} {} +GlUniformBufferInput::GlUniformBufferInput(const std::shared_ptr &buffer) : + UniformBufferInput{buffer} { + auto glBuf = std::dynamic_pointer_cast(buffer); + + auto uniforms = glBuf->get_uniforms(); + + // Reserve space for the used uniforms. + this->used_uniforms.reserve(uniforms.size()); + + // Calculate the byte-wise offsets of all uniforms. + size_t offset = 0; + this->update_offs.reserve(uniforms.size()); + for (auto &uniform : uniforms) { + this->update_offs.push_back({uniform.second.offset, false}); + offset += GL_UNIFORM_TYPE_SIZE.get(uniform.second.type); + } + + // Resize the update data buffer to the total size of all uniforms. + this->update_data.resize(offset); +} } // namespace openage::renderer::opengl diff --git a/libopenage/renderer/opengl/uniform_input.h b/libopenage/renderer/opengl/uniform_input.h index 8ca3ff02dd..961e12a2a5 100644 --- a/libopenage/renderer/opengl/uniform_input.h +++ b/libopenage/renderer/opengl/uniform_input.h @@ -18,6 +18,7 @@ class UniformBuffer; namespace opengl { class GlShaderProgram; +class GlUniformBuffer; /** * Describes OpenGL-specific uniform valuations. @@ -57,18 +58,28 @@ class GlUniformInput final : public UniformInput { * Describes OpenGL-specific uniform buffer valuations. */ class GlUniformBufferInput final : public UniformBufferInput { +private: + struct GlUniformOffset { + // Offset in the update_data buffer. + size_t offset; + /// Dtermine whether the uniform value has been set. + bool used; + }; + + public: - GlUniformBufferInput(std::shared_ptr const &); + GlUniformBufferInput(const std::shared_ptr &buffer); /** - * We store uniform updates lazily. They are only actually uploaded to GPU - * when a draw call is made. - * - * \p update_offs maps the uniform names to where their - * value is in \p update_data in terms of a byte-wise offset. This is only a partial - * valuation, so not all uniforms have to be present here. + * Store the IDs of the uniforms from the shader set by this uniform input. */ - std::unordered_map update_offs; + std::vector used_uniforms; + + /** + * Store offsets of uniforms in the update_data buffer and + * whether the uniform value has been set. + */ + std::vector update_offs; /** * Buffer containing untyped uniform update data. From de212cbe268df46d89ca11dd9c676c41b2772761 Mon Sep 17 00:00:00 2001 From: edvin Date: Sun, 22 Sep 2024 22:09:27 +0200 Subject: [PATCH 4/4] Changed uniforms attribute from unordered_map to vector --- libopenage/renderer/opengl/renderer.cpp | 17 +++++----- libopenage/renderer/opengl/shader_data.h | 8 ++++- libopenage/renderer/opengl/shader_program.cpp | 22 ++++++------- libopenage/renderer/opengl/uniform_buffer.cpp | 33 ++++++++++--------- libopenage/renderer/opengl/uniform_buffer.h | 6 ++-- libopenage/renderer/opengl/uniform_input.cpp | 4 +-- 6 files changed, 48 insertions(+), 42 deletions(-) diff --git a/libopenage/renderer/opengl/renderer.cpp b/libopenage/renderer/opengl/renderer.cpp index 48639dc368..33b734790f 100644 --- a/libopenage/renderer/opengl/renderer.cpp +++ b/libopenage/renderer/opengl/renderer.cpp @@ -86,7 +86,7 @@ std::shared_ptr GlRenderer::get_display_target() { std::shared_ptr GlRenderer::add_uniform_buffer(resources::UniformBufferInfo const &info) { auto inputs = info.get_inputs(); - std::unordered_map uniforms{}; + std::vector uniforms{}; size_t offset = 0; for (auto const &input : inputs) { auto type = GL_UBO_INPUT_TYPE.get(input.type); @@ -94,14 +94,13 @@ std::shared_ptr GlRenderer::add_uniform_buffer(resources::Uniform // align offset to the size of the type offset += offset % size; - - uniforms.emplace( - std::make_pair(input.name, - GlInBlockUniform{type, - offset, - resources::UniformBufferInfo::get_size(input, info.get_layout()), - resources::UniformBufferInfo::get_stride_size(input.type, info.get_layout()), - input.count})); + uniforms.push_back( + GlInBlockUniform{type, + offset, + resources::UniformBufferInfo::get_size(input, info.get_layout()), + resources::UniformBufferInfo::get_stride_size(input.type, info.get_layout()), + input.count, + input.name}); offset += size; } diff --git a/libopenage/renderer/opengl/shader_data.h b/libopenage/renderer/opengl/shader_data.h index 7b987fb6a7..d60d16cb85 100644 --- a/libopenage/renderer/opengl/shader_data.h +++ b/libopenage/renderer/opengl/shader_data.h @@ -5,6 +5,7 @@ #include #include #include +#include #include @@ -61,6 +62,11 @@ struct GlInBlockUniform { * Only relevant for arrays. The number of elements in the array. */ size_t count; + + /** + * Name of the block uniform. + */ + std::string name; }; /** @@ -78,7 +84,7 @@ struct GlUniformBlock { /** * Maps uniform names within this block to their descriptions. */ - std::unordered_map uniforms; + std::vector uniforms; /** * The binding point assigned to this block. diff --git a/libopenage/renderer/opengl/shader_program.cpp b/libopenage/renderer/opengl/shader_program.cpp index da24ac1596..395ee395c2 100644 --- a/libopenage/renderer/opengl/shader_program.cpp +++ b/libopenage/renderer/opengl/shader_program.cpp @@ -125,7 +125,7 @@ GlShaderProgram::GlShaderProgram(const std::shared_ptr &context, std::vector uniform_indices(val); glGetActiveUniformBlockiv(handle, i_unif_block, GL_UNIFORM_BLOCK_ACTIVE_UNIFORM_INDICES, uniform_indices.data()); - std::unordered_map uniforms; + std::vector uniforms; for (GLuint const i_unif : uniform_indices) { in_block_unifs.insert(i_unif); @@ -152,14 +152,14 @@ GlShaderProgram::GlShaderProgram(const std::shared_ptr &context, // We do not need to handle sampler types here like in the uniform loop below, // because named blocks cannot contain samplers. - uniforms.insert(std::make_pair( - name.data(), + uniforms.push_back( GlInBlockUniform{ type, size_t(offset), size_t(count) * GL_UNIFORM_TYPE_SIZE.get(type), size_t(stride), - size_t(count)})); + size_t(count), + std::string(name.data())}); } // ENSURE(block_binding < caps.max_uniform_buffer_bindings, @@ -257,10 +257,10 @@ GlShaderProgram::GlShaderProgram(const std::shared_ptr &context, for (auto const &pair : this->uniform_blocks) { log::log(MSG(dbg) << "(" << pair.second.index << ") " << pair.first << " (size: " << pair.second.data_size << ") {"); - for (auto const &unif_pair : pair.second.uniforms) { - log::log(MSG(dbg) << "\t+" << unif_pair.second.offset - << " " << unif_pair.first << ": " - << GLSL_TYPE_NAME.get(unif_pair.second.type)); + for (auto const &unif : pair.second.uniforms) { + log::log(MSG(dbg) << "\t+" << unif.offset + << " " << unif.name << ": " + << GLSL_TYPE_NAME.get(unif.type)); } log::log(MSG(dbg) << "}"); } @@ -478,9 +478,9 @@ void GlShaderProgram::bind_uniform_buffer(const char *block_name, std::shared_pt auto &block = this->uniform_blocks[block_name]; // Check if the uniform buffer matches the block definition - for (auto const &pair : block.uniforms) { - ENSURE(gl_buffer->has_uniform(pair.first.c_str()), - "Uniform buffer does not contain uniform '" << pair.first << "' required by block " << block_name); + for (auto const &unif : block.uniforms) { + ENSURE(gl_buffer->has_uniform(unif.name.c_str()), + "Uniform buffer does not contain uniform '" << unif.name << "' required by block " << block_name); } block.binding_point = gl_buffer->get_binding_point(); diff --git a/libopenage/renderer/opengl/uniform_buffer.cpp b/libopenage/renderer/opengl/uniform_buffer.cpp index c153d1e080..cc3aa3d3dc 100644 --- a/libopenage/renderer/opengl/uniform_buffer.cpp +++ b/libopenage/renderer/opengl/uniform_buffer.cpp @@ -16,7 +16,7 @@ namespace openage::renderer::opengl { GlUniformBuffer::GlUniformBuffer(const std::shared_ptr &context, size_t size, - std::unordered_map uniforms, + std::vector uniforms, GLuint binding_point, GLenum usage) : GlSimpleObject(context, @@ -35,7 +35,7 @@ GlUniformBuffer::GlUniformBuffer(const std::shared_ptr &context, uniform_id_t unif_id = 0; for (auto &uniform : uniforms) { - this->uniforms_by_name.insert(std::make_pair(uniform.first, unif_id)); + this->uniforms_by_name.insert(std::make_pair(uniform.name, unif_id)); unif_id += 1; } @@ -62,25 +62,30 @@ void GlUniformBuffer::update_uniforms(std::shared_ptr const this->bind(); const auto &update_offs = glunif_in->update_offs; + const auto &used_uniforms = glunif_in->used_uniforms; + const auto &uniforms = this->uniforms; uint8_t const *data = glunif_in->update_data.data(); - for (auto const &pair : this->uniforms_by_name) { - auto id = pair.second; - auto offset = update_offs[id]; + + size_t unif_count = used_uniforms.size(); + for (size_t i = 0; i < unif_count; ++i) { + uniform_id_t unif_id = used_uniforms[i]; + auto offset = update_offs[unif_id]; + uint8_t const *ptr = data + offset.offset; - auto unif_def = this->uniforms.find(pair.first)->second; - auto loc = unif_def.offset; - auto size = unif_def.size; + auto &unif = uniforms[unif_id]; + auto loc = unif.offset; + auto size = unif.size; glBufferSubData(GL_UNIFORM_BUFFER, loc, size, ptr); } } -const std::unordered_map &GlUniformBuffer::get_uniforms() const { +const std::vector &GlUniformBuffer::get_uniforms() const { return this->uniforms; } -bool GlUniformBuffer::has_uniform(const char *unif) { - return this->uniforms.count(unif) != 0; +bool GlUniformBuffer::has_uniform(const char *name) { + return this->uniforms_by_name.contains(name); } void GlUniformBuffer::bind() const { @@ -100,11 +105,7 @@ void GlUniformBuffer::set_unif(UniformBufferInput &in, const char *unif, void co ENSURE(unif_id < this->uniforms.size(), "Tried to set uniform with invalid ID " << unif_id); - auto uniform = this->uniforms.find(unif); - ENSURE(uniform != std::end(this->uniforms), - "Tried to set uniform " << unif << " that does not exist in the shader program."); - - auto const &unif_data = uniform->second; + auto const &unif_data = this->uniforms[unif_id]; ENSURE(type == unif_data.type, "Tried to set uniform " << unif << " to a value of the wrong type."); diff --git a/libopenage/renderer/opengl/uniform_buffer.h b/libopenage/renderer/opengl/uniform_buffer.h index 0fdfc7309d..24fd1e23a2 100644 --- a/libopenage/renderer/opengl/uniform_buffer.h +++ b/libopenage/renderer/opengl/uniform_buffer.h @@ -22,7 +22,7 @@ class GlUniformBuffer final : public UniformBuffer public: GlUniformBuffer(const std::shared_ptr &context, size_t size, - std::unordered_map uniforms, + std::vector uniforms, GLuint binding_point = 0, GLenum usage = GL_DYNAMIC_DRAW); @@ -38,7 +38,7 @@ class GlUniformBuffer final : public UniformBuffer * * @return Uniforms in the shader program. */ - const std::unordered_map &get_uniforms() const; + const std::vector &get_uniforms() const; /** * Set the binding point of the buffer. @@ -95,7 +95,7 @@ class GlUniformBuffer final : public UniformBuffer /** * Uniform definitions inside the buffer. */ - std::unordered_map uniforms; + std::vector uniforms; /** * Maps uniform names to their ID (the index in the uniform vector). diff --git a/libopenage/renderer/opengl/uniform_input.cpp b/libopenage/renderer/opengl/uniform_input.cpp index 0121cc751a..38c63f2d66 100644 --- a/libopenage/renderer/opengl/uniform_input.cpp +++ b/libopenage/renderer/opengl/uniform_input.cpp @@ -42,8 +42,8 @@ GlUniformBufferInput::GlUniformBufferInput(const std::shared_ptr size_t offset = 0; this->update_offs.reserve(uniforms.size()); for (auto &uniform : uniforms) { - this->update_offs.push_back({uniform.second.offset, false}); - offset += GL_UNIFORM_TYPE_SIZE.get(uniform.second.type); + this->update_offs.push_back({uniform.offset, false}); + offset += GL_UNIFORM_TYPE_SIZE.get(uniform.type); } // Resize the update data buffer to the total size of all uniforms.