From c99eb23869da2b80e3613a886aa1b99851367a3c Mon Sep 17 00:00:00 2001 From: Tom Englund Date: Sat, 17 Jan 2026 15:31:19 +0100 Subject: renderer: optimise shader usage further, split shaders and add more caching (#12992) * shader: split CM rgba/rgbx into discard ones make it branchless if we have no discards. * shader: ensure we dont stall on vbo uv buffer if we render a new texture before the previous was done gpu wise its going to stall until done, call glBufferData to orphan the data. this allows the driver to return a new memory block immediately if the GPU is still reading from the previous one * protocols: ensure we reset GL_PACK_ALIGNMENT reset GL_PACK_ALIGNMENT back to the default initial value of 4 * shader: use unsigned short in VAO loose a tiny bit of precision but gain massive bandwidth reductions. use GL_UNSIGNED_SHORT and set it as normalized. clamp and round the UV for uint16_t in customUv. * shader: interleave vertex buffers use std::array for fullverts, use a single interleaved buffer for position and uv, should in theory improve cache locality. and also remove the need to have two buffers around. * shader: revert precision drop we need the float precision because we might have 1.01 or similiar floats entering CM shader maths, and rounding/clamping those means the maths turns out wrong. so revert back to float, sadly higher bandwidth usage. * update doColorManagement api * convert primaries to XYZ on cpu * remove unused primaries uniform --------- Co-authored-by: UjinT34 --- src/render/Shader.cpp | 58 ++++++++++++++++++++++----------------------------- 1 file changed, 25 insertions(+), 33 deletions(-) (limited to 'src/render/Shader.cpp') diff --git a/src/render/Shader.cpp b/src/render/Shader.cpp index 635e1328..5f62232c 100644 --- a/src/render/Shader.cpp +++ b/src/render/Shader.cpp @@ -127,19 +127,19 @@ void CShader::getUniformLocations() { m_uniformLocations[SHADER_TEX_TYPE] = getUniform("texType"); // shader has #include "CM.glsl" - m_uniformLocations[SHADER_SKIP_CM] = getUniform("skipCM"); - m_uniformLocations[SHADER_SOURCE_TF] = getUniform("sourceTF"); - m_uniformLocations[SHADER_TARGET_TF] = getUniform("targetTF"); - m_uniformLocations[SHADER_SRC_TF_RANGE] = getUniform("srcTFRange"); - m_uniformLocations[SHADER_DST_TF_RANGE] = getUniform("dstTFRange"); - m_uniformLocations[SHADER_TARGET_PRIMARIES] = getUniform("targetPrimaries"); - m_uniformLocations[SHADER_MAX_LUMINANCE] = getUniform("maxLuminance"); - m_uniformLocations[SHADER_SRC_REF_LUMINANCE] = getUniform("srcRefLuminance"); - m_uniformLocations[SHADER_DST_MAX_LUMINANCE] = getUniform("dstMaxLuminance"); - m_uniformLocations[SHADER_DST_REF_LUMINANCE] = getUniform("dstRefLuminance"); - m_uniformLocations[SHADER_SDR_SATURATION] = getUniform("sdrSaturation"); - m_uniformLocations[SHADER_SDR_BRIGHTNESS] = getUniform("sdrBrightnessMultiplier"); - m_uniformLocations[SHADER_CONVERT_MATRIX] = getUniform("convertMatrix"); + m_uniformLocations[SHADER_SKIP_CM] = getUniform("skipCM"); + m_uniformLocations[SHADER_SOURCE_TF] = getUniform("sourceTF"); + m_uniformLocations[SHADER_TARGET_TF] = getUniform("targetTF"); + m_uniformLocations[SHADER_SRC_TF_RANGE] = getUniform("srcTFRange"); + m_uniformLocations[SHADER_DST_TF_RANGE] = getUniform("dstTFRange"); + m_uniformLocations[SHADER_TARGET_PRIMARIES_XYZ] = getUniform("targetPrimariesXYZ"); + m_uniformLocations[SHADER_MAX_LUMINANCE] = getUniform("maxLuminance"); + m_uniformLocations[SHADER_SRC_REF_LUMINANCE] = getUniform("srcRefLuminance"); + m_uniformLocations[SHADER_DST_MAX_LUMINANCE] = getUniform("dstMaxLuminance"); + m_uniformLocations[SHADER_DST_REF_LUMINANCE] = getUniform("dstRefLuminance"); + m_uniformLocations[SHADER_SDR_SATURATION] = getUniform("sdrSaturation"); + m_uniformLocations[SHADER_SDR_BRIGHTNESS] = getUniform("sdrBrightnessMultiplier"); + m_uniformLocations[SHADER_CONVERT_MATRIX] = getUniform("convertMatrix"); // m_uniformLocations[SHADER_TEX] = getUniform("tex"); m_uniformLocations[SHADER_ALPHA] = getUniform("alpha"); @@ -208,7 +208,7 @@ void CShader::getUniformLocations() { } void CShader::createVao() { - GLuint shaderVao = 0, shaderVbo = 0, shaderVboUv = 0; + GLuint shaderVao = 0, shaderVbo = 0; glGenVertexArrays(1, &shaderVao); glBindVertexArray(shaderVao); @@ -216,30 +216,26 @@ void CShader::createVao() { if (m_uniformLocations[SHADER_POS_ATTRIB] != -1) { glGenBuffers(1, &shaderVbo); glBindBuffer(GL_ARRAY_BUFFER, shaderVbo); - glBufferData(GL_ARRAY_BUFFER, sizeof(fullVerts), fullVerts, GL_STATIC_DRAW); + glBufferData(GL_ARRAY_BUFFER, sizeof(fullVerts), fullVerts.data(), GL_DYNAMIC_DRAW); glEnableVertexAttribArray(m_uniformLocations[SHADER_POS_ATTRIB]); - glVertexAttribPointer(m_uniformLocations[SHADER_POS_ATTRIB], 2, GL_FLOAT, GL_FALSE, 0, nullptr); + glVertexAttribPointer(m_uniformLocations[SHADER_POS_ATTRIB], 2, GL_FLOAT, GL_FALSE, sizeof(SVertex), (void*)offsetof(SVertex, x)); } // UV VBO (dynamic, may be updated per frame) - if (m_uniformLocations[SHADER_TEX_ATTRIB] != -1) { - glGenBuffers(1, &shaderVboUv); - glBindBuffer(GL_ARRAY_BUFFER, shaderVboUv); - glBufferData(GL_ARRAY_BUFFER, sizeof(fullVerts), fullVerts, GL_DYNAMIC_DRAW); // Initial dummy UVs + if (m_uniformLocations[SHADER_TEX_ATTRIB] != -1 && shaderVbo != 0) { + glBindBuffer(GL_ARRAY_BUFFER, shaderVbo); glEnableVertexAttribArray(m_uniformLocations[SHADER_TEX_ATTRIB]); - glVertexAttribPointer(m_uniformLocations[SHADER_TEX_ATTRIB], 2, GL_FLOAT, GL_FALSE, 0, nullptr); + glVertexAttribPointer(m_uniformLocations[SHADER_TEX_ATTRIB], 2, GL_FLOAT, GL_FALSE, sizeof(SVertex), (void*)offsetof(SVertex, u)); } glBindVertexArray(0); glBindBuffer(GL_ARRAY_BUFFER, 0); - m_uniformLocations[SHADER_SHADER_VAO] = shaderVao; - m_uniformLocations[SHADER_SHADER_VBO_POS] = shaderVbo; - m_uniformLocations[SHADER_SHADER_VBO_UV] = shaderVboUv; + m_uniformLocations[SHADER_SHADER_VAO] = shaderVao; + m_uniformLocations[SHADER_SHADER_VBO] = shaderVbo; RASSERT(m_uniformLocations[SHADER_SHADER_VAO] >= 0, "SHADER_SHADER_VAO could not be created"); - RASSERT(m_uniformLocations[SHADER_SHADER_VBO_POS] >= 0, "SHADER_SHADER_VBO_POS could not be created"); - RASSERT(m_uniformLocations[SHADER_SHADER_VBO_UV] >= 0, "SHADER_SHADER_VBO_UV could not be created"); + RASSERT(m_uniformLocations[SHADER_SHADER_VBO] >= 0, "SHADER_SHADER_VBO_POS could not be created"); } void CShader::setUniformInt(eShaderUniform location, GLint v0) { @@ -390,11 +386,10 @@ void CShader::destroy() { if (m_program == 0) return; - GLuint shaderVao, shaderVbo, shaderVboUv; + GLuint shaderVao, shaderVbo; - shaderVao = m_uniformLocations[SHADER_SHADER_VAO] == -1 ? 0 : m_uniformLocations[SHADER_SHADER_VAO]; - shaderVbo = m_uniformLocations[SHADER_SHADER_VBO_POS] == -1 ? 0 : m_uniformLocations[SHADER_SHADER_VBO_POS]; - shaderVboUv = m_uniformLocations[SHADER_SHADER_VBO_UV] == -1 ? 0 : m_uniformLocations[SHADER_SHADER_VBO_UV]; + shaderVao = m_uniformLocations[SHADER_SHADER_VAO] == -1 ? 0 : m_uniformLocations[SHADER_SHADER_VAO]; + shaderVbo = m_uniformLocations[SHADER_SHADER_VBO] == -1 ? 0 : m_uniformLocations[SHADER_SHADER_VBO]; if (shaderVao) glDeleteVertexArrays(1, &shaderVao); @@ -402,9 +397,6 @@ void CShader::destroy() { if (shaderVbo) glDeleteBuffers(1, &shaderVbo); - if (shaderVboUv) - glDeleteBuffers(1, &shaderVboUv); - glDeleteProgram(m_program); m_program = 0; } -- cgit v1.2.3