From bda140ab732aa8e567514842aca9af4ed207bd5b Mon Sep 17 00:00:00 2001 From: Kae <80987908+Novaenia@users.noreply.github.com> Date: Sat, 1 Jul 2023 07:31:40 +1000 Subject: [PATCH] Renderer: Cache uniform and attribute locations --- source/application/StarRenderer_opengl20.cpp | 52 +++++++++++++++----- source/application/StarRenderer_opengl20.hpp | 11 ++++- 2 files changed, 48 insertions(+), 15 deletions(-) diff --git a/source/application/StarRenderer_opengl20.cpp b/source/application/StarRenderer_opengl20.cpp index 0584b51..b54361e 100644 --- a/source/application/StarRenderer_opengl20.cpp +++ b/source/application/StarRenderer_opengl20.cpp @@ -168,10 +168,12 @@ void OpenGl20Renderer::loadEffectConfig(String const& name, Json const& effectCo } glUseProgram(m_program = program); - setupGlUniforms(m_program); - auto& effect = m_effects.emplace(name, Effect{ program, effectConfig, {}, {} }).first->second; + auto& effect = m_effects.emplace(name, Effect()).first->second; + effect.program = m_program; + effect.config = effectConfig; m_currentEffect = &effect; + setupGlUniforms(effect); for (auto const& p : effectConfig.getObject("effectParameters", {})) { EffectParameter effectParameter; @@ -303,7 +305,7 @@ bool OpenGl20Renderer::switchEffectConfig(String const& name) { Effect& effect = find->second; glUseProgram(m_program = effect.program); - setupGlUniforms(m_program); + setupGlUniforms(effect); m_currentEffect = &effect; return true; @@ -825,21 +827,24 @@ void OpenGl20Renderer::renderGlBuffer(GlRenderBuffer const& renderBuffer, Mat3F } } -void OpenGl20Renderer::setupGlUniforms(GLuint program) { - m_positionAttribute = glGetAttribLocation(program, "vertexPosition"); - m_texCoordAttribute = glGetAttribLocation(program, "vertexTextureCoordinate"); - m_texIndexAttribute = glGetAttribLocation(program, "vertexTextureIndex"); - m_colorAttribute = glGetAttribLocation(program, "vertexColor"); - m_param1Attribute = glGetAttribLocation(program, "vertexParam1"); +//Assumes the passed effect program is currently in use. +void OpenGl20Renderer::setupGlUniforms(Effect& effect) { + GLuint program = effect.program; + + m_positionAttribute = effect.getAttribute("vertexPosition"); + m_texCoordAttribute = effect.getAttribute("vertexTextureCoordinate"); + m_texIndexAttribute = effect.getAttribute("vertexTextureIndex"); + m_colorAttribute = effect.getAttribute("vertexColor"); + m_param1Attribute = effect.getAttribute("vertexParam1"); m_textureUniforms.clear(); m_textureSizeUniforms.clear(); for (size_t i = 0; i < MultiTextureCount; ++i) { - m_textureUniforms.append(glGetUniformLocation(program, strf("texture{}", i).c_str())); - m_textureSizeUniforms.append(glGetUniformLocation(program, strf("textureSize{}", i).c_str())); + m_textureUniforms.append(effect.getUniform(strf("texture{}", i).c_str())); + m_textureSizeUniforms.append(effect.getUniform(strf("textureSize{}", i).c_str())); } - m_screenSizeUniform = glGetUniformLocation(program, "screenSize"); - m_vertexTransformUniform = glGetUniformLocation(program, "vertexTransform"); + m_screenSizeUniform = effect.getUniform("screenSize"); + m_vertexTransformUniform = effect.getUniform("vertexTransform"); for (size_t i = 0; i < MultiTextureCount; ++i) glUniform1i(m_textureUniforms[i], i); @@ -847,4 +852,25 @@ void OpenGl20Renderer::setupGlUniforms(GLuint program) { glUniform2f(m_screenSizeUniform, m_screenSize[0], m_screenSize[1]); } +GLuint OpenGl20Renderer::Effect::getAttribute(String const& name) { + auto find = attributes.find(name); + if (find == attributes.end()) { + GLuint attrib = glGetAttribLocation(program, name.utf8Ptr()); + attributes[name] = attrib; + return attrib; + } + return find->second; +} + +GLuint OpenGl20Renderer::Effect::getUniform(String const& name) { + auto find = uniforms.find(name); + if (find == uniforms.end()) { + GLuint uniform = glGetUniformLocation(program, name.utf8Ptr()); + uniforms[name] = uniform; + return uniform; + } + return find->second; +} + + } diff --git a/source/application/StarRenderer_opengl20.hpp b/source/application/StarRenderer_opengl20.hpp index 6946073..598f803 100644 --- a/source/application/StarRenderer_opengl20.hpp +++ b/source/application/StarRenderer_opengl20.hpp @@ -158,11 +158,18 @@ private: RefPtr textureValue; }; - struct Effect { + class Effect { + public: GLuint program; Json config; StringMap parameters; StringMap textures; + + StringMap attributes; + StringMap uniforms; + + GLuint getAttribute(String const& name); + GLuint getUniform(String const& name); }; static bool logGlErrorSummary(String prefix); @@ -176,7 +183,7 @@ private: void renderGlBuffer(GlRenderBuffer const& renderBuffer, Mat3F const& transformation); - void setupGlUniforms(GLuint program); + void setupGlUniforms(Effect& effect); Vec2U m_screenSize;