From 78d96946763a4e3bd18a7d7d39ddb2daa0f298d8 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Vladim=C3=ADr=20Vondru=C5=A1?= Date: Tue, 8 Jun 2021 13:39:25 +0200 Subject: [PATCH] Shaders: assume materialId is 0 if MATERIAL_COUNT is 1. Unlike the drawId optimization before, there's no possibility to check this anywhere, so the assumption is just documented. On an Intel 630 this resulted in further significant reduction for the single-draw single-material case, down to 260 from 440 in the previous commit, about a 45% reduction compared to the original 550 ms; multidraw case is still around the 550 there. --- src/Magnum/Shaders/DistanceFieldVector.frag | 4 ++++ src/Magnum/Shaders/DistanceFieldVector.h | 5 +++-- src/Magnum/Shaders/Flat.frag | 4 ++++ src/Magnum/Shaders/Flat.h | 6 ++++-- src/Magnum/Shaders/MeshVisualizer.frag | 4 ++++ src/Magnum/Shaders/MeshVisualizer.h | 5 +++-- src/Magnum/Shaders/Phong.frag | 4 ++++ src/Magnum/Shaders/Phong.h | 5 +++-- src/Magnum/Shaders/Vector.frag | 4 ++++ src/Magnum/Shaders/Vector.h | 5 +++-- 10 files changed, 36 insertions(+), 10 deletions(-) diff --git a/src/Magnum/Shaders/DistanceFieldVector.frag b/src/Magnum/Shaders/DistanceFieldVector.frag index 0421f17a1..16385febf 100644 --- a/src/Magnum/Shaders/DistanceFieldVector.frag +++ b/src/Magnum/Shaders/DistanceFieldVector.frag @@ -144,7 +144,11 @@ out lowp vec4 fragmentColor; void main() { #ifdef UNIFORM_BUFFERS + #if MATERIAL_COUNT > 1 mediump const uint materialId = draws[drawId].draw_materialIdReserved & 0xffffu; + #else + #define materialId 0u + #endif lowp const float smoothness = materials[materialId].material_smoothness; lowp const vec4 color = materials[materialId].color; lowp const vec4 outlineColor = materials[materialId].outlineColor; diff --git a/src/Magnum/Shaders/DistanceFieldVector.h b/src/Magnum/Shaders/DistanceFieldVector.h index 8964793d7..5bd8510ac 100644 --- a/src/Magnum/Shaders/DistanceFieldVector.h +++ b/src/Magnum/Shaders/DistanceFieldVector.h @@ -101,8 +101,9 @@ struct DistanceFieldVectorDrawUniform { * more than one material is supplied or in a multi-draw scenario. Should * be less than the material count passed to the * @ref DistanceFieldVectorGL::DistanceFieldVectorGL(Flags, UnsignedInt, UnsignedInt) - * constructor. Default value is @cpp 0 @ce, meaning the first material - * gets used. + * constructor, if material count is @cpp 1 @ce, this field is assumed to + * be @cpp 0 @ce and isn't even read by the shader. Default value is + * @cpp 0 @ce, meaning the first material gets used. */ /* This field is an UnsignedInt in the shader and materialId is extracted diff --git a/src/Magnum/Shaders/Flat.frag b/src/Magnum/Shaders/Flat.frag index 8f5e5a6d7..dff034e23 100644 --- a/src/Magnum/Shaders/Flat.frag +++ b/src/Magnum/Shaders/Flat.frag @@ -176,7 +176,11 @@ void main() { #ifdef OBJECT_ID highp const uint objectId = draws[drawId].draw_objectId; #endif + #if MATERIAL_COUNT > 1 mediump const uint materialId = draws[drawId].draw_materialIdReserved & 0xffffu; + #else + #define materialId 0u + #endif lowp const vec4 color = materials[materialId].color; #ifdef ALPHA_MASK lowp const float alphaMask = materials[materialId].material_alphaMask; diff --git a/src/Magnum/Shaders/Flat.h b/src/Magnum/Shaders/Flat.h index 4a894c173..8e22d887d 100644 --- a/src/Magnum/Shaders/Flat.h +++ b/src/Magnum/Shaders/Flat.h @@ -108,8 +108,10 @@ struct FlatDrawUniform { * References a particular material from a @ref FlatMaterialUniform array. * Useful when an UBO with more than one material is supplied or in a * multi-draw scenario. Should be less than the material count passed to - * the @ref FlatGL::FlatGL(Flags, UnsignedInt, UnsignedInt) constructor. - * Default value is @cpp 0 @ce, meaning the first material gets used. + * the @ref FlatGL::FlatGL(Flags, UnsignedInt, UnsignedInt) constructor, if + * material count is @cpp 1 @ce, this field is assumed to be @cpp 0 @ce and + * isn't even read by the shader. Default value is @cpp 0 @ce, meaning the + * first material gets used. */ /* This field is an UnsignedInt in the shader and materialId is extracted diff --git a/src/Magnum/Shaders/MeshVisualizer.frag b/src/Magnum/Shaders/MeshVisualizer.frag index 20bb89ec7..d1e467052 100644 --- a/src/Magnum/Shaders/MeshVisualizer.frag +++ b/src/Magnum/Shaders/MeshVisualizer.frag @@ -224,7 +224,11 @@ out lowp vec4 fragmentColor; void main() { #ifdef UNIFORM_BUFFERS + #if MATERIAL_COUNT > 1 mediump const uint materialId = draws[drawId].draw_materialIdReserved & 0xffffu; + #else + #define materialId 0u + #endif #if (defined(WIREFRAME_RENDERING) || defined(INSTANCED_OBJECT_ID) || defined(VERTEX_ID) || defined(PRIMITIVE_ID) || defined(PRIMITIVE_ID_FROM_VERTEX_ID)) && !defined(TBN_DIRECTION) lowp const vec4 color = materials[materialId].color; lowp const vec4 wireframeColor = materials[materialId].wireframeColor; diff --git a/src/Magnum/Shaders/MeshVisualizer.h b/src/Magnum/Shaders/MeshVisualizer.h index 2e4038412..9b0a540c4 100644 --- a/src/Magnum/Shaders/MeshVisualizer.h +++ b/src/Magnum/Shaders/MeshVisualizer.h @@ -100,8 +100,9 @@ struct MeshVisualizerDrawUniform2D { * more than one material is supplied or in a multi-draw scenario. Should * be less than the material count passed to the @ref MeshVisualizerGL2D::MeshVisualizerGL2D(Flags, UnsignedInt, UnsignedInt) * / @ref MeshVisualizerGL3D::MeshVisualizerGL3D(Flags, UnsignedInt, UnsignedInt) - * constructor. Default value is @cpp 0 @ce, meaning the first material - * gets used. + * constructor, if material count is @cpp 1 @ce, this field is assumed to + * be @cpp 0 @ce and isn't even read by the shader. Default value is + * @cpp 0 @ce, meaning the first material gets used. */ /* This field is an UnsignedInt in the shader and materialId is extracted diff --git a/src/Magnum/Shaders/Phong.frag b/src/Magnum/Shaders/Phong.frag index 57ab5b2d5..ccfb8371a 100644 --- a/src/Magnum/Shaders/Phong.frag +++ b/src/Magnum/Shaders/Phong.frag @@ -339,7 +339,11 @@ void main() { #ifdef OBJECT_ID highp const uint objectId = draws[drawId].draw_objectId; #endif + #if MATERIAL_COUNT > 1 mediump const uint materialId = draws[drawId].draw_materialIdReserved & 0xffffu; + #else + #define materialId 0u + #endif lowp const vec4 ambientColor = materials[materialId].ambientColor; #if LIGHT_COUNT lowp const vec4 diffuseColor = materials[materialId].diffuseColor; diff --git a/src/Magnum/Shaders/Phong.h b/src/Magnum/Shaders/Phong.h index c315e171d..8ac861067 100644 --- a/src/Magnum/Shaders/Phong.h +++ b/src/Magnum/Shaders/Phong.h @@ -141,8 +141,9 @@ struct PhongDrawUniform { * Useful when a UBO with more than one material is supplied or in a * multi-draw scenario. Should be less than the material count passed to * the @ref PhongGL::PhongGL(Flags, UnsignedInt, UnsignedInt, UnsignedInt) - * constructor. Default value is @cpp 0 @ce, meaning the first material - * gets used. + * constructor, if material count is @cpp 1 @ce, this field is assumed to + * be @cpp 0 @ce and isn't even read by the shader. Default value is + * @cpp 0 @ce, meaning the first material gets used. */ /* This field is an UnsignedInt in the shader and materialId is extracted diff --git a/src/Magnum/Shaders/Vector.frag b/src/Magnum/Shaders/Vector.frag index 1b78de18f..50477a9b2 100644 --- a/src/Magnum/Shaders/Vector.frag +++ b/src/Magnum/Shaders/Vector.frag @@ -123,7 +123,11 @@ out lowp vec4 fragmentColor; void main() { #ifdef UNIFORM_BUFFERS + #if MATERIAL_COUNT > 1 mediump const uint materialId = draws[drawId].draw_materialIdReserved & 0xffffu; + #else + #define materialId 0u + #endif lowp const vec4 color = materials[materialId].color; lowp const vec4 backgroundColor = materials[materialId].backgroundColor; #endif diff --git a/src/Magnum/Shaders/Vector.h b/src/Magnum/Shaders/Vector.h index 4d6b44675..daaf3d70a 100644 --- a/src/Magnum/Shaders/Vector.h +++ b/src/Magnum/Shaders/Vector.h @@ -96,8 +96,9 @@ struct VectorDrawUniform { * array. Useful when an UBO with more than one material is supplied or in * a multi-draw scenario. Should be less than the material count passed to * the @ref VectorGL::VectorGL(Flags, UnsignedInt, UnsignedInt) - * constructor. Default value is @cpp 0 @ce, meaning the first material - * gets used. + * constructor, if material count is @cpp 1 @ce, this field is assumed to + * be @cpp 0 @ce and isn't even read by the shader. Default value is + * @cpp 0 @ce, meaning the first material gets used. */ /* This field is an UnsignedInt in the shader and materialId is extracted