From fff1b6e5cf486026e19245683341cd72f3888463 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Vladim=C3=ADr=20Vondru=C5=A1?= Date: Tue, 17 Mar 2020 12:09:58 +0100 Subject: [PATCH] Shaders: delete 8 kB of needless shader code. I get the best ideas on the shitter. I should go there more often. --- .../{Flat3D.vert => AbstractVector.vert} | 41 ++++++----- src/Magnum/Shaders/AbstractVector2D.vert | 72 ------------------- src/Magnum/Shaders/AbstractVector3D.vert | 72 ------------------- src/Magnum/Shaders/DistanceFieldVector.cpp | 9 +-- src/Magnum/Shaders/Flat.cpp | 7 +- src/Magnum/Shaders/{Flat2D.vert => Flat.vert} | 22 ++++++ src/Magnum/Shaders/Vector.cpp | 9 +-- src/Magnum/Shaders/VertexColor.cpp | 11 +-- .../{VertexColor2D.vert => VertexColor.vert} | 23 ++++++ src/Magnum/Shaders/VertexColor3D.vert | 55 -------------- src/Magnum/Shaders/resources.conf | 15 +--- 11 files changed, 79 insertions(+), 257 deletions(-) rename src/Magnum/Shaders/{Flat3D.vert => AbstractVector.vert} (84%) delete mode 100644 src/Magnum/Shaders/AbstractVector2D.vert delete mode 100644 src/Magnum/Shaders/AbstractVector3D.vert rename src/Magnum/Shaders/{Flat2D.vert => Flat.vert} (86%) rename src/Magnum/Shaders/{VertexColor2D.vert => VertexColor.vert} (81%) delete mode 100644 src/Magnum/Shaders/VertexColor3D.vert diff --git a/src/Magnum/Shaders/Flat3D.vert b/src/Magnum/Shaders/AbstractVector.vert similarity index 84% rename from src/Magnum/Shaders/Flat3D.vert rename to src/Magnum/Shaders/AbstractVector.vert index 0b8167293..44a9025c0 100644 --- a/src/Magnum/Shaders/Flat3D.vert +++ b/src/Magnum/Shaders/AbstractVector.vert @@ -31,11 +31,21 @@ #ifdef EXPLICIT_UNIFORM_LOCATION layout(location = 0) #endif +#ifdef TWO_DIMENSIONS +uniform highp mat3 transformationProjectionMatrix + #ifndef GL_ES + = mat3(1.0) + #endif + ; +#elif defined(THREE_DIMENSIONS) uniform highp mat4 transformationProjectionMatrix #ifndef GL_ES = mat4(1.0) #endif ; +#else +#error +#endif #ifdef TEXTURE_TRANSFORMATION #ifdef EXPLICIT_UNIFORM_LOCATION @@ -51,31 +61,30 @@ uniform mediump mat3 textureMatrix #ifdef EXPLICIT_ATTRIB_LOCATION layout(location = POSITION_ATTRIBUTE_LOCATION) #endif +#ifdef TWO_DIMENSIONS +in highp vec2 position; +#elif defined(THREE_DIMENSIONS) in highp vec4 position; +#else +#error +#endif -#ifdef TEXTURED #ifdef EXPLICIT_ATTRIB_LOCATION layout(location = TEXTURECOORDINATES_ATTRIBUTE_LOCATION) #endif in mediump vec2 textureCoordinates; out mediump vec2 interpolatedTextureCoordinates; -#endif - -#ifdef VERTEX_COLOR -#ifdef EXPLICIT_ATTRIB_LOCATION -layout(location = COLOR_ATTRIBUTE_LOCATION) -#endif -in lowp vec4 vertexColor; - -out lowp vec4 interpolatedVertexColor; -#endif void main() { + #ifdef TWO_DIMENSIONS + gl_Position.xywz = vec4(transformationProjectionMatrix*vec3(position, 1.0), 0.0); + #elif defined(THREE_DIMENSIONS) gl_Position = transformationProjectionMatrix*position; + #else + #error + #endif - #ifdef TEXTURED - /* Texture coordinates, if needed */ interpolatedTextureCoordinates = #ifdef TEXTURE_TRANSFORMATION (textureMatrix*vec3(textureCoordinates, 1.0)).xy @@ -83,10 +92,4 @@ void main() { textureCoordinates #endif ; - #endif - - #ifdef VERTEX_COLOR - /* Vertex colors, if enabled */ - interpolatedVertexColor = vertexColor; - #endif } diff --git a/src/Magnum/Shaders/AbstractVector2D.vert b/src/Magnum/Shaders/AbstractVector2D.vert deleted file mode 100644 index 9b6613050..000000000 --- a/src/Magnum/Shaders/AbstractVector2D.vert +++ /dev/null @@ -1,72 +0,0 @@ -/* - This file is part of Magnum. - - Copyright © 2010, 2011, 2012, 2013, 2014, 2015, 2016, 2017, 2018, 2019 - Vladimír Vondruš - - Permission is hereby granted, free of charge, to any person obtaining a - copy of this software and associated documentation files (the "Software"), - to deal in the Software without restriction, including without limitation - the rights to use, copy, modify, merge, publish, distribute, sublicense, - and/or sell copies of the Software, and to permit persons to whom the - Software is furnished to do so, subject to the following conditions: - - The above copyright notice and this permission notice shall be included - in all copies or substantial portions of the Software. - - THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR - IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, - FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL - THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER - LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING - FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER - DEALINGS IN THE SOFTWARE. -*/ - -#ifndef NEW_GLSL -#define in attribute -#define out varying -#endif - -#ifdef EXPLICIT_UNIFORM_LOCATION -layout(location = 0) -#endif -uniform highp mat3 transformationProjectionMatrix - #ifndef GL_ES - = mat3(1.0) - #endif - ; - -#ifdef TEXTURE_TRANSFORMATION -#ifdef EXPLICIT_UNIFORM_LOCATION -layout(location = 1) -#endif -uniform mediump mat3 textureMatrix - #ifndef GL_ES - = mat3(1.0) - #endif - ; -#endif - -#ifdef EXPLICIT_ATTRIB_LOCATION -layout(location = POSITION_ATTRIBUTE_LOCATION) -#endif -in highp vec2 position; - -#ifdef EXPLICIT_ATTRIB_LOCATION -layout(location = TEXTURECOORDINATES_ATTRIBUTE_LOCATION) -#endif -in mediump vec2 textureCoordinates; - -out mediump vec2 interpolatedTextureCoordinates; - -void main() { - gl_Position.xywz = vec4(transformationProjectionMatrix*vec3(position, 1.0), 0.0); - interpolatedTextureCoordinates = - #ifdef TEXTURE_TRANSFORMATION - (textureMatrix*vec3(textureCoordinates, 1.0)).xy - #else - textureCoordinates - #endif - ; -} diff --git a/src/Magnum/Shaders/AbstractVector3D.vert b/src/Magnum/Shaders/AbstractVector3D.vert deleted file mode 100644 index 6cc562032..000000000 --- a/src/Magnum/Shaders/AbstractVector3D.vert +++ /dev/null @@ -1,72 +0,0 @@ -/* - This file is part of Magnum. - - Copyright © 2010, 2011, 2012, 2013, 2014, 2015, 2016, 2017, 2018, 2019 - Vladimír Vondruš - - Permission is hereby granted, free of charge, to any person obtaining a - copy of this software and associated documentation files (the "Software"), - to deal in the Software without restriction, including without limitation - the rights to use, copy, modify, merge, publish, distribute, sublicense, - and/or sell copies of the Software, and to permit persons to whom the - Software is furnished to do so, subject to the following conditions: - - The above copyright notice and this permission notice shall be included - in all copies or substantial portions of the Software. - - THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR - IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, - FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL - THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER - LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING - FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER - DEALINGS IN THE SOFTWARE. -*/ - -#ifndef NEW_GLSL -#define in attribute -#define out varying -#endif - -#ifdef EXPLICIT_UNIFORM_LOCATION -layout(location = 0) -#endif -uniform highp mat4 transformationProjectionMatrix - #ifndef GL_ES - = mat4(1.0) - #endif - ; - -#ifdef TEXTURE_TRANSFORMATION -#ifdef EXPLICIT_UNIFORM_LOCATION -layout(location = 1) -#endif -uniform mediump mat3 textureMatrix - #ifndef GL_ES - = mat3(1.0) - #endif - ; -#endif - -#ifdef EXPLICIT_ATTRIB_LOCATION -layout(location = POSITION_ATTRIBUTE_LOCATION) -#endif -in highp vec4 position; - -#ifdef EXPLICIT_ATTRIB_LOCATION -layout(location = TEXTURECOORDINATES_ATTRIBUTE_LOCATION) -#endif -in mediump vec2 textureCoordinates; - -out mediump vec2 interpolatedTextureCoordinates; - -void main() { - gl_Position = transformationProjectionMatrix*position; - interpolatedTextureCoordinates = - #ifdef TEXTURE_TRANSFORMATION - (textureMatrix*vec3(textureCoordinates, 1.0)).xy - #else - textureCoordinates - #endif - ; -} diff --git a/src/Magnum/Shaders/DistanceFieldVector.cpp b/src/Magnum/Shaders/DistanceFieldVector.cpp index 7707e6456..6b167371e 100644 --- a/src/Magnum/Shaders/DistanceFieldVector.cpp +++ b/src/Magnum/Shaders/DistanceFieldVector.cpp @@ -40,12 +40,6 @@ namespace Magnum { namespace Shaders { -namespace { - template constexpr const char* vertexShaderName(); - template<> constexpr const char* vertexShaderName<2>() { return "AbstractVector2D.vert"; } - template<> constexpr const char* vertexShaderName<3>() { return "AbstractVector3D.vert"; } -} - template DistanceFieldVector::DistanceFieldVector(const Flags flags): _flags{flags} { #ifdef MAGNUM_BUILD_STATIC /* Import resources on static build, if not already */ @@ -64,8 +58,9 @@ template DistanceFieldVector::DistanceFieldV GL::Shader frag = Implementation::createCompatibilityShader(rs, version, GL::Shader::Type::Fragment); vert.addSource(flags & Flag::TextureTransformation ? "#define TEXTURE_TRANSFORMATION\n" : "") + .addSource(dimensions == 2 ? "#define TWO_DIMENSIONS\n" : "#define THREE_DIMENSIONS\n") .addSource(rs.get("generic.glsl")) - .addSource(rs.get(vertexShaderName())); + .addSource(rs.get("AbstractVector.vert")); frag.addSource(rs.get("generic.glsl")) .addSource(rs.get("DistanceFieldVector.frag")); diff --git a/src/Magnum/Shaders/Flat.cpp b/src/Magnum/Shaders/Flat.cpp index ac7dfdb05..7b923b48a 100644 --- a/src/Magnum/Shaders/Flat.cpp +++ b/src/Magnum/Shaders/Flat.cpp @@ -43,10 +43,6 @@ namespace Magnum { namespace Shaders { namespace { enum: Int { TextureLayer = 0 }; - - template constexpr const char* vertexShaderName(); - template<> constexpr const char* vertexShaderName<2>() { return "Flat2D.vert"; } - template<> constexpr const char* vertexShaderName<3>() { return "Flat3D.vert"; } } template Flat::Flat(const Flags flags): _flags(flags) { @@ -72,8 +68,9 @@ template Flat::Flat(const Flags flags): _fla vert.addSource(flags & Flag::Textured ? "#define TEXTURED\n" : "") .addSource(flags & Flag::VertexColor ? "#define VERTEX_COLOR\n" : "") .addSource(flags & Flag::TextureTransformation ? "#define TEXTURE_TRANSFORMATION\n" : "") + .addSource(dimensions == 2 ? "#define TWO_DIMENSIONS\n" : "#define THREE_DIMENSIONS\n") .addSource(rs.get("generic.glsl")) - .addSource(rs.get(vertexShaderName())); + .addSource(rs.get("Flat.vert")); frag.addSource(flags & Flag::Textured ? "#define TEXTURED\n" : "") .addSource(flags & Flag::AlphaMask ? "#define ALPHA_MASK\n" : "") .addSource(flags & Flag::VertexColor ? "#define VERTEX_COLOR\n" : "") diff --git a/src/Magnum/Shaders/Flat2D.vert b/src/Magnum/Shaders/Flat.vert similarity index 86% rename from src/Magnum/Shaders/Flat2D.vert rename to src/Magnum/Shaders/Flat.vert index 1f772f395..851e72325 100644 --- a/src/Magnum/Shaders/Flat2D.vert +++ b/src/Magnum/Shaders/Flat.vert @@ -31,11 +31,21 @@ #ifdef EXPLICIT_UNIFORM_LOCATION layout(location = 0) #endif +#ifdef TWO_DIMENSIONS uniform highp mat3 transformationProjectionMatrix #ifndef GL_ES = mat3(1.0) #endif ; +#elif defined(THREE_DIMENSIONS) +uniform highp mat4 transformationProjectionMatrix + #ifndef GL_ES + = mat4(1.0) + #endif + ; +#else +#error +#endif #ifdef TEXTURE_TRANSFORMATION #ifdef EXPLICIT_UNIFORM_LOCATION @@ -51,7 +61,13 @@ uniform mediump mat3 textureMatrix #ifdef EXPLICIT_ATTRIB_LOCATION layout(location = POSITION_ATTRIBUTE_LOCATION) #endif +#ifdef TWO_DIMENSIONS in highp vec2 position; +#elif defined(THREE_DIMENSIONS) +in highp vec4 position; +#else +#error +#endif #ifdef TEXTURED #ifdef EXPLICIT_ATTRIB_LOCATION @@ -72,7 +88,13 @@ out lowp vec4 interpolatedVertexColor; #endif void main() { + #ifdef TWO_DIMENSIONS gl_Position.xywz = vec4(transformationProjectionMatrix*vec3(position, 1.0), 0.0); + #elif defined(THREE_DIMENSIONS) + gl_Position = transformationProjectionMatrix*position; + #else + #error + #endif #ifdef TEXTURED /* Texture coordinates, if needed */ diff --git a/src/Magnum/Shaders/Vector.cpp b/src/Magnum/Shaders/Vector.cpp index f84c71ca4..da96dc009 100644 --- a/src/Magnum/Shaders/Vector.cpp +++ b/src/Magnum/Shaders/Vector.cpp @@ -40,12 +40,6 @@ namespace Magnum { namespace Shaders { -namespace { - template constexpr const char* vertexShaderName(); - template<> constexpr const char* vertexShaderName<2>() { return "AbstractVector2D.vert"; } - template<> constexpr const char* vertexShaderName<3>() { return "AbstractVector3D.vert"; } -} - template Vector::Vector(const Flags flags): _flags{flags} { #ifdef MAGNUM_BUILD_STATIC /* Import resources on static build, if not already */ @@ -64,8 +58,9 @@ template Vector::Vector(const Flags flags): GL::Shader frag = Implementation::createCompatibilityShader(rs, version, GL::Shader::Type::Fragment); vert.addSource(flags & Flag::TextureTransformation ? "#define TEXTURE_TRANSFORMATION\n" : "") + .addSource(dimensions == 2 ? "#define TWO_DIMENSIONS\n" : "#define THREE_DIMENSIONS\n") .addSource(rs.get("generic.glsl")) - .addSource(rs.get(vertexShaderName())); + .addSource(rs.get("AbstractVector.vert")); frag.addSource(rs.get("generic.glsl")) .addSource(rs.get("Vector.frag")); diff --git a/src/Magnum/Shaders/VertexColor.cpp b/src/Magnum/Shaders/VertexColor.cpp index 935fe37fb..f1e39ec7f 100644 --- a/src/Magnum/Shaders/VertexColor.cpp +++ b/src/Magnum/Shaders/VertexColor.cpp @@ -39,12 +39,6 @@ namespace Magnum { namespace Shaders { -namespace { - template constexpr const char* vertexShaderName(); - template<> constexpr const char* vertexShaderName<2>() { return "VertexColor2D.vert"; } - template<> constexpr const char* vertexShaderName<3>() { return "VertexColor3D.vert"; } -} - template VertexColor::VertexColor() { #ifdef MAGNUM_BUILD_STATIC /* Import resources on static build, if not already */ @@ -62,8 +56,9 @@ template VertexColor::VertexColor() { GL::Shader vert = Implementation::createCompatibilityShader(rs, version, GL::Shader::Type::Vertex); GL::Shader frag = Implementation::createCompatibilityShader(rs, version, GL::Shader::Type::Fragment); - vert.addSource(rs.get("generic.glsl")) - .addSource(rs.get(vertexShaderName())); + vert.addSource(dimensions == 2 ? "#define TWO_DIMENSIONS\n" : "#define THREE_DIMENSIONS\n") + .addSource(rs.get("generic.glsl")) + .addSource(rs.get("VertexColor.vert")); frag.addSource(rs.get("generic.glsl")) .addSource(rs.get("VertexColor.frag")); diff --git a/src/Magnum/Shaders/VertexColor2D.vert b/src/Magnum/Shaders/VertexColor.vert similarity index 81% rename from src/Magnum/Shaders/VertexColor2D.vert rename to src/Magnum/Shaders/VertexColor.vert index d67172d7f..3b8540edd 100644 --- a/src/Magnum/Shaders/VertexColor2D.vert +++ b/src/Magnum/Shaders/VertexColor.vert @@ -31,16 +31,32 @@ #ifdef EXPLICIT_UNIFORM_LOCATION layout(location = 0) #endif +#ifdef TWO_DIMENSIONS uniform highp mat3 transformationProjectionMatrix #ifndef GL_ES = mat3(1.0) #endif ; +#elif defined(THREE_DIMENSIONS) +uniform highp mat4 transformationProjectionMatrix + #ifndef GL_ES + = mat4(1.0) + #endif + ; +#else +#error +#endif #ifdef EXPLICIT_ATTRIB_LOCATION layout(location = POSITION_ATTRIBUTE_LOCATION) #endif +#ifdef TWO_DIMENSIONS in highp vec2 position; +#elif defined(THREE_DIMENSIONS) +in highp vec4 position; +#else +#error +#endif #ifdef EXPLICIT_ATTRIB_LOCATION layout(location = COLOR_ATTRIBUTE_LOCATION) @@ -50,6 +66,13 @@ in lowp vec4 color; out lowp vec4 interpolatedColor; void main() { + #ifdef TWO_DIMENSIONS gl_Position.xywz = vec4(transformationProjectionMatrix*vec3(position, 1.0), 0.0); + #elif defined(THREE_DIMENSIONS) + gl_Position = transformationProjectionMatrix*position; + #else + #error + #endif + interpolatedColor = color; } diff --git a/src/Magnum/Shaders/VertexColor3D.vert b/src/Magnum/Shaders/VertexColor3D.vert deleted file mode 100644 index 51ca839c5..000000000 --- a/src/Magnum/Shaders/VertexColor3D.vert +++ /dev/null @@ -1,55 +0,0 @@ -/* - This file is part of Magnum. - - Copyright © 2010, 2011, 2012, 2013, 2014, 2015, 2016, 2017, 2018, 2019 - Vladimír Vondruš - - Permission is hereby granted, free of charge, to any person obtaining a - copy of this software and associated documentation files (the "Software"), - to deal in the Software without restriction, including without limitation - the rights to use, copy, modify, merge, publish, distribute, sublicense, - and/or sell copies of the Software, and to permit persons to whom the - Software is furnished to do so, subject to the following conditions: - - The above copyright notice and this permission notice shall be included - in all copies or substantial portions of the Software. - - THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR - IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, - FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL - THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER - LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING - FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER - DEALINGS IN THE SOFTWARE. -*/ - -#ifndef NEW_GLSL -#define in attribute -#define out varying -#endif - -#ifdef EXPLICIT_UNIFORM_LOCATION -layout(location = 0) -#endif -uniform mat4 transformationProjectionMatrix - #ifndef GL_ES - = mat4(1.0) - #endif - ; - -#ifdef EXPLICIT_ATTRIB_LOCATION -layout(location = POSITION_ATTRIBUTE_LOCATION) -#endif -in highp vec4 position; - -#ifdef EXPLICIT_ATTRIB_LOCATION -layout(location = COLOR_ATTRIBUTE_LOCATION) -#endif -in lowp vec4 color; - -out lowp vec4 interpolatedColor; - -void main() { - gl_Position = transformationProjectionMatrix*position; - interpolatedColor = color; -} diff --git a/src/Magnum/Shaders/resources.conf b/src/Magnum/Shaders/resources.conf index a9aa28940..59924c314 100644 --- a/src/Magnum/Shaders/resources.conf +++ b/src/Magnum/Shaders/resources.conf @@ -1,16 +1,10 @@ group=MagnumShaders [file] -filename=AbstractVector2D.vert +filename=AbstractVector.vert [file] -filename=AbstractVector3D.vert - -[file] -filename=Flat2D.vert - -[file] -filename=Flat3D.vert +filename=Flat.vert [file] filename=Flat.frag @@ -43,10 +37,7 @@ filename=Vector.frag filename=DistanceFieldVector.frag [file] -filename=VertexColor2D.vert - -[file] -filename=VertexColor3D.vert +filename=VertexColor.vert [file] filename=VertexColor.frag