Support setting uniform block indices in gfx::program

This commit is contained in:
Nikita Lisitsa 2022-06-22 18:03:04 +03:00
parent a9cb71629e
commit a439d33b0f
2 changed files with 34 additions and 4 deletions

View file

@ -115,9 +115,14 @@ namespace psemek::gfx
uniform_proxy operator[] (std::string_view name) const; uniform_proxy operator[] (std::string_view name) const;
uniform_proxy operator[] (GLint location) const; uniform_proxy operator[] (GLint location) const;
GLuint uniform_block_index(std::string_view name) const;
void uniform_block_binding(std::string_view name, GLuint binding) const;
void uniform_block_binding(GLuint index, GLuint binding) const;
private: private:
GLuint program_; GLuint program_;
mutable boost::container::flat_map<std::string, GLint, std::less<>> uniforms_; mutable boost::container::flat_map<std::string, GLint, std::less<>> uniforms_;
mutable boost::container::flat_map<std::string, GLuint, std::less<>> uniform_blocks_;
program(std::nullptr_t) program(std::nullptr_t)
: program_{0} : program_{0}

View file

@ -374,10 +374,10 @@ namespace psemek::gfx
auto it = uniforms_.find(name); auto it = uniforms_.find(name);
if (it == uniforms_.end()) if (it == uniforms_.end())
{ {
std::string name_str(name.begin(), name.end()); std::string name_str(name);
auto l = gl::GetUniformLocation(program_, name_str.c_str()); auto location = gl::GetUniformLocation(program_, name_str.data());
uniforms_[std::move(name_str)] = l; uniforms_[std::move(name_str)] = location;
return l; return location;
} }
return it->second; return it->second;
} }
@ -392,4 +392,29 @@ namespace psemek::gfx
return {location}; return {location};
} }
GLuint program::uniform_block_index(std::string_view name) const
{
auto it = uniform_blocks_.find(name);
if (it == uniform_blocks_.end())
{
std::string name_str(name);
auto index = gl::GetUniformBlockIndex(program_, name.data());
if (index == gl::INVALID_INDEX)
throw std::runtime_error("Unknown uniform block: " + name_str);
uniform_blocks_[std::move(name_str)] = index;
return index;
}
return it->second;
}
void program::uniform_block_binding(std::string_view name, GLuint binding) const
{
gl::UniformBlockBinding(program_, uniform_block_index(name), binding);
}
void program::uniform_block_binding(GLuint index, GLuint binding) const
{
gl::UniformBlockBinding(program_, index, binding);
}
} }