From 218aef5016dfd764237d2c234cc9f7ff2ed293c0 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Vladim=C3=ADr=20Vondru=C5=A1?= Date: Mon, 13 Mar 2023 14:52:24 +0100 Subject: [PATCH] Shaders: don't define & format irrelevant skinning values for UBOs. The UBO use case doesn't need any initializers or uniform locations for these, it's just wasted allocations. --- src/Magnum/Shaders/FlatGL.cpp | 52 ++++++++++++++--------- src/Magnum/Shaders/MeshVisualizerGL.cpp | 52 ++++++++++++++--------- src/Magnum/Shaders/PhongGL.cpp | 56 +++++++++++++++---------- 3 files changed, 101 insertions(+), 59 deletions(-) diff --git a/src/Magnum/Shaders/FlatGL.cpp b/src/Magnum/Shaders/FlatGL.cpp index ea12471cc..2f144ca3e 100644 --- a/src/Magnum/Shaders/FlatGL.cpp +++ b/src/Magnum/Shaders/FlatGL.cpp @@ -179,27 +179,41 @@ template typename FlatGL::CompileState FlatG .addSource(configuration.flags() >= Flag::InstancedTextureOffset ? "#define INSTANCED_TEXTURE_OFFSET\n"_s : ""_s); #ifndef MAGNUM_TARGET_GLES2 if(configuration.perVertexJointCount() || configuration.secondaryPerVertexJointCount()) { - vert.addSource(Utility::format( - "#define JOINT_COUNT {}\n" - "#define PER_VERTEX_JOINT_COUNT {}u\n" - "#define SECONDARY_PER_VERTEX_JOINT_COUNT {}u\n" - #ifndef MAGNUM_TARGET_GLES - "#define JOINT_MATRIX_INITIALIZER {}\n" - #endif - "#define PER_INSTANCE_JOINT_COUNT_LOCATION {}\n", - configuration.jointCount(), - configuration.perVertexJointCount(), - configuration.secondaryPerVertexJointCount(), - #ifndef MAGNUM_TARGET_GLES - ((dimensions == 2 ? "mat3(1.0), "_s : "mat4(1.0), "_s)*configuration.jointCount()).exceptSuffix(2), - #endif - out._perInstanceJointCountUniform)); + if(!(configuration.flags() >= Flag::UniformBuffers)) { + vert.addSource(Utility::format( + "#define JOINT_COUNT {}\n" + "#define PER_VERTEX_JOINT_COUNT {}u\n" + "#define SECONDARY_PER_VERTEX_JOINT_COUNT {}u\n" + #ifndef MAGNUM_TARGET_GLES + "#define JOINT_MATRIX_INITIALIZER {}\n" + #endif + "#define PER_INSTANCE_JOINT_COUNT_LOCATION {}\n", + configuration.jointCount(), + configuration.perVertexJointCount(), + configuration.secondaryPerVertexJointCount(), + #ifndef MAGNUM_TARGET_GLES + ((dimensions == 2 ? "mat3(1.0), "_s : "mat4(1.0), "_s)*configuration.jointCount()).exceptSuffix(2), + #endif + out._perInstanceJointCountUniform)); + } else { + vert.addSource(Utility::format( + "#define JOINT_COUNT {}\n" + "#define PER_VERTEX_JOINT_COUNT {}u\n" + "#define SECONDARY_PER_VERTEX_JOINT_COUNT {}u\n", + configuration.jointCount(), + configuration.perVertexJointCount(), + configuration.secondaryPerVertexJointCount())); + } } if(configuration.flags() >= Flag::DynamicPerVertexJointCount) { - vert.addSource(Utility::format( - "#define DYNAMIC_PER_VERTEX_JOINT_COUNT\n" - "#define PER_VERTEX_JOINT_COUNT_LOCATION {}\n", - out._perVertexJointCountUniform)); + if(!(configuration.flags() >= Flag::UniformBuffers)) { + vert.addSource(Utility::format( + "#define DYNAMIC_PER_VERTEX_JOINT_COUNT\n" + "#define PER_VERTEX_JOINT_COUNT_LOCATION {}\n", + out._perVertexJointCountUniform)); + } else { + vert.addSource("#define DYNAMIC_PER_VERTEX_JOINT_COUNT\n"_s); + } } #endif #ifndef MAGNUM_TARGET_GLES2 diff --git a/src/Magnum/Shaders/MeshVisualizerGL.cpp b/src/Magnum/Shaders/MeshVisualizerGL.cpp index d7de27905..7f27bf5ac 100644 --- a/src/Magnum/Shaders/MeshVisualizerGL.cpp +++ b/src/Magnum/Shaders/MeshVisualizerGL.cpp @@ -198,27 +198,41 @@ GL::Version MeshVisualizerGLBase::setupShaders(GL::Shader& vert, GL::Shader& fra ; #ifndef MAGNUM_TARGET_GLES2 if(perVertexJointCount || secondaryPerVertexJointCount) { - vert.addSource(Utility::format( - "#define JOINT_COUNT {}\n" - "#define PER_VERTEX_JOINT_COUNT {}u\n" - "#define SECONDARY_PER_VERTEX_JOINT_COUNT {}u\n" - #ifndef MAGNUM_TARGET_GLES - "#define JOINT_MATRIX_INITIALIZER {}\n" - #endif - "#define PER_INSTANCE_JOINT_COUNT_LOCATION {}\n", - jointCount, - perVertexJointCount, - secondaryPerVertexJointCount, - #ifndef MAGNUM_TARGET_GLES - ((dimensions == 2 ? "mat3(1.0), "_s : "mat4(1.0), "_s)*jointCount).exceptSuffix(2), - #endif - perInstanceJointCountUniform)); + if(!(flags >= FlagBase::UniformBuffers)) { + vert.addSource(Utility::format( + "#define JOINT_COUNT {}\n" + "#define PER_VERTEX_JOINT_COUNT {}u\n" + "#define SECONDARY_PER_VERTEX_JOINT_COUNT {}u\n" + #ifndef MAGNUM_TARGET_GLES + "#define JOINT_MATRIX_INITIALIZER {}\n" + #endif + "#define PER_INSTANCE_JOINT_COUNT_LOCATION {}\n", + jointCount, + perVertexJointCount, + secondaryPerVertexJointCount, + #ifndef MAGNUM_TARGET_GLES + ((dimensions == 2 ? "mat3(1.0), "_s : "mat4(1.0), "_s)*jointCount).exceptSuffix(2), + #endif + perInstanceJointCountUniform)); + } else { + vert.addSource(Utility::format( + "#define JOINT_COUNT {}\n" + "#define PER_VERTEX_JOINT_COUNT {}u\n" + "#define SECONDARY_PER_VERTEX_JOINT_COUNT {}u\n", + jointCount, + perVertexJointCount, + secondaryPerVertexJointCount)); + } } if(flags >= FlagBase::DynamicPerVertexJointCount) { - vert.addSource(Utility::format( - "#define DYNAMIC_PER_VERTEX_JOINT_COUNT\n" - "#define PER_VERTEX_JOINT_COUNT_LOCATION {}\n", - perVertexJointCountUniform)); + if(!(flags >= FlagBase::UniformBuffers)) { + vert.addSource(Utility::format( + "#define DYNAMIC_PER_VERTEX_JOINT_COUNT\n" + "#define PER_VERTEX_JOINT_COUNT_LOCATION {}\n", + perVertexJointCountUniform)); + } else { + vert.addSource("#define DYNAMIC_PER_VERTEX_JOINT_COUNT\n"_s); + } } #endif #ifndef MAGNUM_TARGET_GLES2 diff --git a/src/Magnum/Shaders/PhongGL.cpp b/src/Magnum/Shaders/PhongGL.cpp index 1dd10d82f..58310ca4e 100644 --- a/src/Magnum/Shaders/PhongGL.cpp +++ b/src/Magnum/Shaders/PhongGL.cpp @@ -224,29 +224,43 @@ PhongGL::CompileState PhongGL::compile(const Configuration& configuration) { .addSource(configuration.flags() >= Flag::InstancedTextureOffset ? "#define INSTANCED_TEXTURE_OFFSET\n"_s : ""_s); #ifndef MAGNUM_TARGET_GLES2 if(configuration.perVertexJointCount() || configuration.secondaryPerVertexJointCount()) { - vert.addSource(Utility::format( - "#define JOINT_COUNT {}\n" - "#define PER_VERTEX_JOINT_COUNT {}u\n" - "#define SECONDARY_PER_VERTEX_JOINT_COUNT {}u\n" - "#define JOINT_MATRICES_LOCATION {}\n" - #ifndef MAGNUM_TARGET_GLES - "#define JOINT_MATRIX_INITIALIZER {}\n" - #endif - "#define PER_INSTANCE_JOINT_COUNT_LOCATION {}\n", - configuration.jointCount(), - configuration.perVertexJointCount(), - configuration.secondaryPerVertexJointCount(), - out._jointMatricesUniform, - #ifndef MAGNUM_TARGET_GLES - ("mat4(1.0), "_s*configuration.jointCount()).exceptSuffix(2), - #endif - out._perInstanceJointCountUniform)); + if(!(configuration.flags() >= Flag::UniformBuffers)) { + vert.addSource(Utility::format( + "#define JOINT_COUNT {}\n" + "#define PER_VERTEX_JOINT_COUNT {}u\n" + "#define SECONDARY_PER_VERTEX_JOINT_COUNT {}u\n" + "#define JOINT_MATRICES_LOCATION {}\n" + #ifndef MAGNUM_TARGET_GLES + "#define JOINT_MATRIX_INITIALIZER {}\n" + #endif + "#define PER_INSTANCE_JOINT_COUNT_LOCATION {}\n", + configuration.jointCount(), + configuration.perVertexJointCount(), + configuration.secondaryPerVertexJointCount(), + out._jointMatricesUniform, + #ifndef MAGNUM_TARGET_GLES + ("mat4(1.0), "_s*configuration.jointCount()).exceptSuffix(2), + #endif + out._perInstanceJointCountUniform)); + } else { + vert.addSource(Utility::format( + "#define JOINT_COUNT {}\n" + "#define PER_VERTEX_JOINT_COUNT {}u\n" + "#define SECONDARY_PER_VERTEX_JOINT_COUNT {}u\n", + configuration.jointCount(), + configuration.perVertexJointCount(), + configuration.secondaryPerVertexJointCount())); + } } if(configuration.flags() >= Flag::DynamicPerVertexJointCount) { - vert.addSource(Utility::format( - "#define DYNAMIC_PER_VERTEX_JOINT_COUNT\n" - "#define PER_VERTEX_JOINT_COUNT_LOCATION {}\n", - out._perVertexJointCountUniform)); + if(!(configuration.flags() >= Flag::UniformBuffers)) { + vert.addSource(Utility::format( + "#define DYNAMIC_PER_VERTEX_JOINT_COUNT\n" + "#define PER_VERTEX_JOINT_COUNT_LOCATION {}\n", + out._perVertexJointCountUniform)); + } else { + vert.addSource("#define DYNAMIC_PER_VERTEX_JOINT_COUNT\n"_s); + } } #endif #ifndef MAGNUM_TARGET_GLES2