Skip to content

Commit de212cb

Browse files
committed
Changed uniforms attribute from unordered_map to vector
1 parent 0c1a368 commit de212cb

File tree

6 files changed

+48
-42
lines changed

6 files changed

+48
-42
lines changed

libopenage/renderer/opengl/renderer.cpp

Lines changed: 8 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -86,22 +86,21 @@ std::shared_ptr<RenderTarget> GlRenderer::get_display_target() {
8686

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

9595
// align offset to the size of the type
9696
offset += offset % size;
97-
98-
uniforms.emplace(
99-
std::make_pair(input.name,
100-
GlInBlockUniform{type,
101-
offset,
102-
resources::UniformBufferInfo::get_size(input, info.get_layout()),
103-
resources::UniformBufferInfo::get_stride_size(input.type, info.get_layout()),
104-
input.count}));
97+
uniforms.push_back(
98+
GlInBlockUniform{type,
99+
offset,
100+
resources::UniformBufferInfo::get_size(input, info.get_layout()),
101+
resources::UniformBufferInfo::get_stride_size(input.type, info.get_layout()),
102+
input.count,
103+
input.name});
105104

106105
offset += size;
107106
}

libopenage/renderer/opengl/shader_data.h

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
#include <optional>
66
#include <string>
77
#include <unordered_map>
8+
#include <vector>
89

910
#include <epoxy/gl.h>
1011

@@ -61,6 +62,11 @@ struct GlInBlockUniform {
6162
* Only relevant for arrays. The number of elements in the array.
6263
*/
6364
size_t count;
65+
66+
/**
67+
* Name of the block uniform.
68+
*/
69+
std::string name;
6470
};
6571

6672
/**
@@ -78,7 +84,7 @@ struct GlUniformBlock {
7884
/**
7985
* Maps uniform names within this block to their descriptions.
8086
*/
81-
std::unordered_map<std::string, GlInBlockUniform> uniforms;
87+
std::vector<GlInBlockUniform> uniforms;
8288

8389
/**
8490
* The binding point assigned to this block.

libopenage/renderer/opengl/shader_program.cpp

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -125,7 +125,7 @@ GlShaderProgram::GlShaderProgram(const std::shared_ptr<GlContext> &context,
125125
std::vector<GLint> uniform_indices(val);
126126
glGetActiveUniformBlockiv(handle, i_unif_block, GL_UNIFORM_BLOCK_ACTIVE_UNIFORM_INDICES, uniform_indices.data());
127127

128-
std::unordered_map<std::string, GlInBlockUniform> uniforms;
128+
std::vector<GlInBlockUniform> uniforms;
129129
for (GLuint const i_unif : uniform_indices) {
130130
in_block_unifs.insert(i_unif);
131131

@@ -152,14 +152,14 @@ GlShaderProgram::GlShaderProgram(const std::shared_ptr<GlContext> &context,
152152
// We do not need to handle sampler types here like in the uniform loop below,
153153
// because named blocks cannot contain samplers.
154154

155-
uniforms.insert(std::make_pair(
156-
name.data(),
155+
uniforms.push_back(
157156
GlInBlockUniform{
158157
type,
159158
size_t(offset),
160159
size_t(count) * GL_UNIFORM_TYPE_SIZE.get(type),
161160
size_t(stride),
162-
size_t(count)}));
161+
size_t(count),
162+
std::string(name.data())});
163163
}
164164

165165
// ENSURE(block_binding < caps.max_uniform_buffer_bindings,
@@ -257,10 +257,10 @@ GlShaderProgram::GlShaderProgram(const std::shared_ptr<GlContext> &context,
257257
for (auto const &pair : this->uniform_blocks) {
258258
log::log(MSG(dbg) << "(" << pair.second.index << ") " << pair.first
259259
<< " (size: " << pair.second.data_size << ") {");
260-
for (auto const &unif_pair : pair.second.uniforms) {
261-
log::log(MSG(dbg) << "\t+" << unif_pair.second.offset
262-
<< " " << unif_pair.first << ": "
263-
<< GLSL_TYPE_NAME.get(unif_pair.second.type));
260+
for (auto const &unif : pair.second.uniforms) {
261+
log::log(MSG(dbg) << "\t+" << unif.offset
262+
<< " " << unif.name << ": "
263+
<< GLSL_TYPE_NAME.get(unif.type));
264264
}
265265
log::log(MSG(dbg) << "}");
266266
}
@@ -478,9 +478,9 @@ void GlShaderProgram::bind_uniform_buffer(const char *block_name, std::shared_pt
478478
auto &block = this->uniform_blocks[block_name];
479479

480480
// Check if the uniform buffer matches the block definition
481-
for (auto const &pair : block.uniforms) {
482-
ENSURE(gl_buffer->has_uniform(pair.first.c_str()),
483-
"Uniform buffer does not contain uniform '" << pair.first << "' required by block " << block_name);
481+
for (auto const &unif : block.uniforms) {
482+
ENSURE(gl_buffer->has_uniform(unif.name.c_str()),
483+
"Uniform buffer does not contain uniform '" << unif.name << "' required by block " << block_name);
484484
}
485485

486486
block.binding_point = gl_buffer->get_binding_point();

libopenage/renderer/opengl/uniform_buffer.cpp

Lines changed: 17 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ namespace openage::renderer::opengl {
1616

1717
GlUniformBuffer::GlUniformBuffer(const std::shared_ptr<GlContext> &context,
1818
size_t size,
19-
std::unordered_map<std::string, GlInBlockUniform> uniforms,
19+
std::vector<GlInBlockUniform> uniforms,
2020
GLuint binding_point,
2121
GLenum usage) :
2222
GlSimpleObject(context,
@@ -35,7 +35,7 @@ GlUniformBuffer::GlUniformBuffer(const std::shared_ptr<GlContext> &context,
3535

3636
uniform_id_t unif_id = 0;
3737
for (auto &uniform : uniforms) {
38-
this->uniforms_by_name.insert(std::make_pair(uniform.first, unif_id));
38+
this->uniforms_by_name.insert(std::make_pair(uniform.name, unif_id));
3939
unif_id += 1;
4040
}
4141

@@ -62,25 +62,30 @@ void GlUniformBuffer::update_uniforms(std::shared_ptr<UniformBufferInput> const
6262
this->bind();
6363

6464
const auto &update_offs = glunif_in->update_offs;
65+
const auto &used_uniforms = glunif_in->used_uniforms;
66+
const auto &uniforms = this->uniforms;
6567
uint8_t const *data = glunif_in->update_data.data();
66-
for (auto const &pair : this->uniforms_by_name) {
67-
auto id = pair.second;
68-
auto offset = update_offs[id];
68+
69+
size_t unif_count = used_uniforms.size();
70+
for (size_t i = 0; i < unif_count; ++i) {
71+
uniform_id_t unif_id = used_uniforms[i];
72+
auto offset = update_offs[unif_id];
73+
6974
uint8_t const *ptr = data + offset.offset;
70-
auto unif_def = this->uniforms.find(pair.first)->second;
71-
auto loc = unif_def.offset;
72-
auto size = unif_def.size;
75+
auto &unif = uniforms[unif_id];
76+
auto loc = unif.offset;
77+
auto size = unif.size;
7378

7479
glBufferSubData(GL_UNIFORM_BUFFER, loc, size, ptr);
7580
}
7681
}
7782

78-
const std::unordered_map<std::string, GlInBlockUniform> &GlUniformBuffer::get_uniforms() const {
83+
const std::vector<GlInBlockUniform> &GlUniformBuffer::get_uniforms() const {
7984
return this->uniforms;
8085
}
8186

82-
bool GlUniformBuffer::has_uniform(const char *unif) {
83-
return this->uniforms.count(unif) != 0;
87+
bool GlUniformBuffer::has_uniform(const char *name) {
88+
return this->uniforms_by_name.contains(name);
8489
}
8590

8691
void GlUniformBuffer::bind() const {
@@ -100,11 +105,7 @@ void GlUniformBuffer::set_unif(UniformBufferInput &in, const char *unif, void co
100105
ENSURE(unif_id < this->uniforms.size(),
101106
"Tried to set uniform with invalid ID " << unif_id);
102107

103-
auto uniform = this->uniforms.find(unif);
104-
ENSURE(uniform != std::end(this->uniforms),
105-
"Tried to set uniform " << unif << " that does not exist in the shader program.");
106-
107-
auto const &unif_data = uniform->second;
108+
auto const &unif_data = this->uniforms[unif_id];
108109

109110
ENSURE(type == unif_data.type,
110111
"Tried to set uniform " << unif << " to a value of the wrong type.");

libopenage/renderer/opengl/uniform_buffer.h

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ class GlUniformBuffer final : public UniformBuffer
2222
public:
2323
GlUniformBuffer(const std::shared_ptr<GlContext> &context,
2424
size_t size,
25-
std::unordered_map<std::string, GlInBlockUniform> uniforms,
25+
std::vector<GlInBlockUniform> uniforms,
2626
GLuint binding_point = 0,
2727
GLenum usage = GL_DYNAMIC_DRAW);
2828

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

4343
/**
4444
* Set the binding point of the buffer.
@@ -95,7 +95,7 @@ class GlUniformBuffer final : public UniformBuffer
9595
/**
9696
* Uniform definitions inside the buffer.
9797
*/
98-
std::unordered_map<std::string, GlInBlockUniform> uniforms;
98+
std::vector<GlInBlockUniform> uniforms;
9999

100100
/**
101101
* Maps uniform names to their ID (the index in the uniform vector).

libopenage/renderer/opengl/uniform_input.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -42,8 +42,8 @@ GlUniformBufferInput::GlUniformBufferInput(const std::shared_ptr<UniformBuffer>
4242
size_t offset = 0;
4343
this->update_offs.reserve(uniforms.size());
4444
for (auto &uniform : uniforms) {
45-
this->update_offs.push_back({uniform.second.offset, false});
46-
offset += GL_UNIFORM_TYPE_SIZE.get(uniform.second.type);
45+
this->update_offs.push_back({uniform.offset, false});
46+
offset += GL_UNIFORM_TYPE_SIZE.get(uniform.type);
4747
}
4848

4949
// Resize the update data buffer to the total size of all uniforms.

0 commit comments

Comments
 (0)