Skip to content

Commit

Permalink
Changed uniforms attribute from unordered_map to vector
Browse files Browse the repository at this point in the history
  • Loading branch information
EdvinLndh committed Sep 22, 2024
1 parent 0c1a368 commit de212cb
Show file tree
Hide file tree
Showing 6 changed files with 48 additions and 42 deletions.
17 changes: 8 additions & 9 deletions libopenage/renderer/opengl/renderer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -86,22 +86,21 @@ std::shared_ptr<RenderTarget> GlRenderer::get_display_target() {

std::shared_ptr<UniformBuffer> GlRenderer::add_uniform_buffer(resources::UniformBufferInfo const &info) {
auto inputs = info.get_inputs();
std::unordered_map<std::string, GlInBlockUniform> uniforms{};
std::vector<GlInBlockUniform> uniforms{};
size_t offset = 0;
for (auto const &input : inputs) {
auto type = GL_UBO_INPUT_TYPE.get(input.type);
auto size = resources::UniformBufferInfo::get_size(input, info.get_layout());

// 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;
}
Expand Down
8 changes: 7 additions & 1 deletion libopenage/renderer/opengl/shader_data.h
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
#include <optional>
#include <string>
#include <unordered_map>
#include <vector>

#include <epoxy/gl.h>

Expand Down Expand Up @@ -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;
};

/**
Expand All @@ -78,7 +84,7 @@ struct GlUniformBlock {
/**
* Maps uniform names within this block to their descriptions.
*/
std::unordered_map<std::string, GlInBlockUniform> uniforms;
std::vector<GlInBlockUniform> uniforms;

/**
* The binding point assigned to this block.
Expand Down
22 changes: 11 additions & 11 deletions libopenage/renderer/opengl/shader_program.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -125,7 +125,7 @@ GlShaderProgram::GlShaderProgram(const std::shared_ptr<GlContext> &context,
std::vector<GLint> uniform_indices(val);
glGetActiveUniformBlockiv(handle, i_unif_block, GL_UNIFORM_BLOCK_ACTIVE_UNIFORM_INDICES, uniform_indices.data());

std::unordered_map<std::string, GlInBlockUniform> uniforms;
std::vector<GlInBlockUniform> uniforms;
for (GLuint const i_unif : uniform_indices) {
in_block_unifs.insert(i_unif);

Expand All @@ -152,14 +152,14 @@ GlShaderProgram::GlShaderProgram(const std::shared_ptr<GlContext> &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,
Expand Down Expand Up @@ -257,10 +257,10 @@ GlShaderProgram::GlShaderProgram(const std::shared_ptr<GlContext> &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) << "}");
}
Expand Down Expand Up @@ -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();
Expand Down
33 changes: 17 additions & 16 deletions libopenage/renderer/opengl/uniform_buffer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ namespace openage::renderer::opengl {

GlUniformBuffer::GlUniformBuffer(const std::shared_ptr<GlContext> &context,
size_t size,
std::unordered_map<std::string, GlInBlockUniform> uniforms,
std::vector<GlInBlockUniform> uniforms,
GLuint binding_point,
GLenum usage) :
GlSimpleObject(context,
Expand All @@ -35,7 +35,7 @@ GlUniformBuffer::GlUniformBuffer(const std::shared_ptr<GlContext> &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;
}

Expand All @@ -62,25 +62,30 @@ void GlUniformBuffer::update_uniforms(std::shared_ptr<UniformBufferInput> 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<std::string, GlInBlockUniform> &GlUniformBuffer::get_uniforms() const {
const std::vector<GlInBlockUniform> &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 {
Expand All @@ -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.");
Expand Down
6 changes: 3 additions & 3 deletions libopenage/renderer/opengl/uniform_buffer.h
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ class GlUniformBuffer final : public UniformBuffer
public:
GlUniformBuffer(const std::shared_ptr<GlContext> &context,
size_t size,
std::unordered_map<std::string, GlInBlockUniform> uniforms,
std::vector<GlInBlockUniform> uniforms,
GLuint binding_point = 0,
GLenum usage = GL_DYNAMIC_DRAW);

Expand All @@ -38,7 +38,7 @@ class GlUniformBuffer final : public UniformBuffer
*
* @return Uniforms in the shader program.
*/
const std::unordered_map<std::string, GlInBlockUniform> &get_uniforms() const;
const std::vector<GlInBlockUniform> &get_uniforms() const;

/**
* Set the binding point of the buffer.
Expand Down Expand Up @@ -95,7 +95,7 @@ class GlUniformBuffer final : public UniformBuffer
/**
* Uniform definitions inside the buffer.
*/
std::unordered_map<std::string, GlInBlockUniform> uniforms;
std::vector<GlInBlockUniform> uniforms;

/**
* Maps uniform names to their ID (the index in the uniform vector).
Expand Down
4 changes: 2 additions & 2 deletions libopenage/renderer/opengl/uniform_input.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -42,8 +42,8 @@ GlUniformBufferInput::GlUniformBufferInput(const std::shared_ptr<UniformBuffer>
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.
Expand Down

0 comments on commit de212cb

Please sign in to comment.