feat(close #8): Implement uniform location caching

This commit is contained in:
Huseyn Ismayilov 2024-08-22 22:05:08 +04:00
parent 0ced6c452f
commit 9a866d16ef
3 changed files with 12 additions and 26 deletions

View File

@ -9,6 +9,8 @@
#include <sstream> #include <sstream>
#include <fstream> #include <fstream>
#include <iostream> #include <iostream>
#include <unordered_map>
class Shader class Shader
{ {
@ -19,15 +21,11 @@ public:
static Shader Create(const std::string& vPath, const std::string& fPath); static Shader Create(const std::string& vPath, const std::string& fPath);
unsigned int GetID() const;
void Use() const; void Use() const;
void Shutdown() const; void Shutdown() const;
void SetBool(const char* name, bool value) const; int GetUniformLocation(const std::string& name);
void SetInt(const char* name, int value) const;
void SetFloat(const char* name, float value) const;
void SetMat4(const char* name, glm::mat4 value) const;
private: private:
unsigned int m_ID; unsigned int m_ID;
std::unordered_map<std::string, int> m_Uniforms{};
}; };

View File

@ -78,8 +78,8 @@ void Renderer::Render() {
s_Data.m_Shader->Use(); s_Data.m_Shader->Use();
auto transform = glm::mat4(1.0f); auto transform = glm::mat4(1.0f);
transform = glm::translate(transform, glm::vec3(UI::GetData().m_Position[0], UI::GetData().m_Position[1], UI::GetData().m_Position[2])); transform = glm::translate(transform, glm::vec3(UI::GetData().m_Position[0], UI::GetData().m_Position[1], UI::GetData().m_Position[2]));
s_Data.m_Shader->SetMat4("transform", transform); glUniformMatrix4fv(s_Data.m_Shader->GetUniformLocation("transform"), 1, GL_FALSE, glm::value_ptr(transform));
glUniform3fv(glGetUniformLocation(s_Data.m_Shader->GetID(), "color"), 1, UI::GetData().m_Color); glUniform3fv(s_Data.m_Shader->GetUniformLocation("color"), 1, UI::GetData().m_Color);
s_Data.m_VAO->Bind(); s_Data.m_VAO->Bind();
glDrawArrays(GL_TRIANGLES, 0, 3); glDrawArrays(GL_TRIANGLES, 0, 3);
FrameBuffer::Unbind(); FrameBuffer::Unbind();

View File

@ -94,24 +94,12 @@ void Shader::Shutdown() const
glDeleteProgram(m_ID); glDeleteProgram(m_ID);
} }
unsigned int Shader::GetID() const int Shader::GetUniformLocation(const std::string& name)
{ {
return m_ID; if(m_Uniforms.find(name) == m_Uniforms.end())
} {
m_Uniforms[name] = glGetUniformLocation(m_ID, name.c_str());
}
void Shader::SetBool(const char* name, bool value) const return m_Uniforms.at(name);
{
glUniform1i(glGetUniformLocation(m_ID, name), (int)value);
}
void Shader::SetInt(const char* name, int value) const
{
glUniform1i(glGetUniformLocation(m_ID, name), value);
}
void Shader::SetFloat(const char* name, float value) const
{
glUniform1f(glGetUniformLocation(m_ID, name), value);
}
void Shader::SetMat4(const char* name, glm::mat4 value) const
{
glUniformMatrix4fv(glGetUniformLocation(m_ID, name), 1, GL_FALSE, glm::value_ptr(value));
} }