From 9f0a61e76ecc2c90c07310ebcca3bdeb53d4e84f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Vladim=C3=ADr=20Vondru=C5=A1?= Date: Sat, 17 Apr 2021 20:08:05 +0200 Subject: [PATCH] Shaders: group things together also in GLSL sources. It was also becoming quite a mess in there. --- src/Magnum/Shaders/AbstractVector.vert | 6 ++ src/Magnum/Shaders/DistanceFieldVector.frag | 8 +++ src/Magnum/Shaders/Flat.frag | 20 +++++-- src/Magnum/Shaders/Flat.vert | 24 ++++++-- src/Magnum/Shaders/FullScreenTriangle.glsl | 2 + src/Magnum/Shaders/MeshVisualizer.frag | 22 ++++--- src/Magnum/Shaders/MeshVisualizer.geom | 19 ++++++- src/Magnum/Shaders/MeshVisualizer.vert | 14 ++++- src/Magnum/Shaders/Phong.frag | 63 ++++++++++++--------- src/Magnum/Shaders/Phong.vert | 6 ++ src/Magnum/Shaders/Vector.frag | 6 ++ src/Magnum/Shaders/VertexColor.frag | 4 ++ src/Magnum/Shaders/VertexColor.vert | 6 ++ 13 files changed, 150 insertions(+), 50 deletions(-) diff --git a/src/Magnum/Shaders/AbstractVector.vert b/src/Magnum/Shaders/AbstractVector.vert index 5b4bb7498..17cb41708 100644 --- a/src/Magnum/Shaders/AbstractVector.vert +++ b/src/Magnum/Shaders/AbstractVector.vert @@ -28,6 +28,8 @@ #define out varying #endif +/* Uniforms */ + #ifdef EXPLICIT_UNIFORM_LOCATION layout(location = 0) #endif @@ -58,6 +60,8 @@ uniform mediump mat3 textureMatrix ; #endif +/* Inputs */ + #ifdef EXPLICIT_ATTRIB_LOCATION layout(location = POSITION_ATTRIBUTE_LOCATION) #endif @@ -74,6 +78,8 @@ layout(location = TEXTURECOORDINATES_ATTRIBUTE_LOCATION) #endif in mediump vec2 textureCoordinates; +/* Outputs */ + out mediump vec2 interpolatedTextureCoordinates; void main() { diff --git a/src/Magnum/Shaders/DistanceFieldVector.frag b/src/Magnum/Shaders/DistanceFieldVector.frag index d7c99d3af..78b1b89f0 100644 --- a/src/Magnum/Shaders/DistanceFieldVector.frag +++ b/src/Magnum/Shaders/DistanceFieldVector.frag @@ -29,6 +29,8 @@ #define texture texture2D #endif +/* Uniforms */ + #ifdef EXPLICIT_UNIFORM_LOCATION layout(location = 2) #endif @@ -61,14 +63,20 @@ uniform lowp float smoothness #endif ; +/* Textures */ + #ifdef EXPLICIT_TEXTURE_LAYER /* See AbstractVector.h for details about the ID */ layout(binding = 6) #endif uniform lowp sampler2D vectorTexture; +/* Inputs */ + in mediump vec2 interpolatedTextureCoordinates; +/* OUtput */ + #ifdef NEW_GLSL #ifdef EXPLICIT_ATTRIB_LOCATION layout(location = COLOR_OUTPUT_ATTRIBUTE_LOCATION) diff --git a/src/Magnum/Shaders/Flat.frag b/src/Magnum/Shaders/Flat.frag index c49a20078..21d3fbf45 100644 --- a/src/Magnum/Shaders/Flat.frag +++ b/src/Magnum/Shaders/Flat.frag @@ -33,12 +33,7 @@ #define in varying #endif -#ifdef TEXTURED -#ifdef EXPLICIT_TEXTURE_LAYER -layout(binding = 0) -#endif -uniform lowp sampler2D textureData; -#endif +/* Uniforms */ #ifdef EXPLICIT_UNIFORM_LOCATION layout(location = 2) @@ -68,6 +63,17 @@ layout(location = 4) uniform highp uint objectId; /* defaults to zero */ #endif +/* Textures */ + +#ifdef TEXTURED +#ifdef EXPLICIT_TEXTURE_LAYER +layout(binding = 0) +#endif +uniform lowp sampler2D textureData; +#endif + +/* Inputs */ + #ifdef TEXTURED in mediump vec2 interpolatedTextureCoordinates; #endif @@ -80,6 +86,8 @@ in lowp vec4 interpolatedVertexColor; flat in highp uint interpolatedInstanceObjectId; #endif +/* Outputs */ + #ifdef NEW_GLSL #ifdef EXPLICIT_ATTRIB_LOCATION layout(location = COLOR_OUTPUT_ATTRIBUTE_LOCATION) diff --git a/src/Magnum/Shaders/Flat.vert b/src/Magnum/Shaders/Flat.vert index 2611ff9ab..1c4305fc6 100644 --- a/src/Magnum/Shaders/Flat.vert +++ b/src/Magnum/Shaders/Flat.vert @@ -32,6 +32,8 @@ #define out varying #endif +/* Uniforms */ + #ifdef EXPLICIT_UNIFORM_LOCATION layout(location = 0) #endif @@ -62,6 +64,8 @@ uniform mediump mat3 textureMatrix ; #endif +/* Inputs */ + #ifdef EXPLICIT_ATTRIB_LOCATION layout(location = POSITION_ATTRIBUTE_LOCATION) #endif @@ -78,8 +82,6 @@ in highp vec4 position; layout(location = TEXTURECOORDINATES_ATTRIBUTE_LOCATION) #endif in mediump vec2 textureCoordinates; - -out mediump vec2 interpolatedTextureCoordinates; #endif #ifdef VERTEX_COLOR @@ -87,8 +89,6 @@ out mediump vec2 interpolatedTextureCoordinates; layout(location = COLOR_ATTRIBUTE_LOCATION) #endif in lowp vec4 vertexColor; - -out lowp vec4 interpolatedVertexColor; #endif #ifdef INSTANCED_OBJECT_ID @@ -96,8 +96,6 @@ out lowp vec4 interpolatedVertexColor; layout(location = OBJECT_ID_ATTRIBUTE_LOCATION) #endif in highp uint instanceObjectId; - -flat out highp uint interpolatedInstanceObjectId; #endif #ifdef INSTANCED_TRANSFORMATION @@ -120,6 +118,20 @@ layout(location = TEXTURE_OFFSET_ATTRIBUTE_LOCATION) in mediump vec2 instancedTextureOffset; #endif +/* Outputs */ + +#ifdef TEXTURED +out mediump vec2 interpolatedTextureCoordinates; +#endif + +#ifdef VERTEX_COLOR +out lowp vec4 interpolatedVertexColor; +#endif + +#ifdef INSTANCED_OBJECT_ID +flat out highp uint interpolatedInstanceObjectId; +#endif + void main() { #ifdef TWO_DIMENSIONS gl_Position.xywz = vec4(transformationProjectionMatrix* diff --git a/src/Magnum/Shaders/FullScreenTriangle.glsl b/src/Magnum/Shaders/FullScreenTriangle.glsl index a989d1e5a..e7b4d974c 100644 --- a/src/Magnum/Shaders/FullScreenTriangle.glsl +++ b/src/Magnum/Shaders/FullScreenTriangle.glsl @@ -23,6 +23,8 @@ DEALINGS IN THE SOFTWARE. */ +/* Inputs */ + #if !defined(NEW_GLSL) || defined(DISABLE_GL_MAGNUM_shader_vertex_id) #ifndef NEW_GLSL #define in attribute diff --git a/src/Magnum/Shaders/MeshVisualizer.frag b/src/Magnum/Shaders/MeshVisualizer.frag index d4dc84417..c206409bd 100644 --- a/src/Magnum/Shaders/MeshVisualizer.frag +++ b/src/Magnum/Shaders/MeshVisualizer.frag @@ -44,6 +44,8 @@ #extension GL_NV_shader_noperspective_interpolation: require #endif +/* Uniforms */ + #if (defined(WIREFRAME_RENDERING) || defined(INSTANCED_OBJECT_ID) || defined(VERTEX_ID) || defined(PRIMITIVE_ID) || defined(PRIMITIVE_ID_FROM_VERTEX_ID)) && !defined(TBN_DIRECTION) #ifdef EXPLICIT_UNIFORM_LOCATION layout(location = 1) @@ -95,13 +97,6 @@ uniform lowp float smoothness ; #endif -#if defined(INSTANCED_OBJECT_ID) || defined(VERTEX_ID) || defined(PRIMITIVE_ID) || defined(PRIMITIVE_ID_FROM_VERTEX_ID) -#ifdef EXPLICIT_TEXTURE_LAYER -layout(binding = 4) -#endif -uniform lowp sampler2D colorMapTexture; -#endif - #if defined(INSTANCED_OBJECT_ID) || defined(PRIMITIVE_ID) || defined(PRIMITIVE_ID_FROM_VERTEX_ID) #ifdef EXPLICIT_UNIFORM_LOCATION layout(location = 6) @@ -115,6 +110,17 @@ uniform lowp vec2 colorMapOffsetScale #define colorMapScale colorMapOffsetScale.y #endif +/* Textures */ + +#if defined(INSTANCED_OBJECT_ID) || defined(VERTEX_ID) || defined(PRIMITIVE_ID) || defined(PRIMITIVE_ID_FROM_VERTEX_ID) +#ifdef EXPLICIT_TEXTURE_LAYER +layout(binding = 4) +#endif +uniform lowp sampler2D colorMapTexture; +#endif + +/* Inputs */ + #if defined(WIREFRAME_RENDERING) || defined(TBN_DIRECTION) #ifndef NO_GEOMETRY_SHADER #if !defined(GL_ES) || defined(GL_NV_shader_noperspective_interpolation) @@ -141,6 +147,8 @@ in lowp vec4 backgroundColor; in lowp vec4 lineColor; #endif +/* Outputs */ + #ifdef NEW_GLSL #ifdef EXPLICIT_ATTRIB_LOCATION layout(location = COLOR_OUTPUT_ATTRIBUTE_LOCATION) diff --git a/src/Magnum/Shaders/MeshVisualizer.geom b/src/Magnum/Shaders/MeshVisualizer.geom index 71a581d2c..e5b65c426 100644 --- a/src/Magnum/Shaders/MeshVisualizer.geom +++ b/src/Magnum/Shaders/MeshVisualizer.geom @@ -34,6 +34,8 @@ #endif #endif +/* Uniforms */ + #ifdef EXPLICIT_UNIFORM_LOCATION layout(location = 5) #endif @@ -81,6 +83,8 @@ uniform lowp float smoothness ; #endif +/* Inputs */ + #ifdef TANGENT_DIRECTION in highp vec4 tangentEndpoint[]; #endif @@ -91,6 +95,18 @@ in highp vec4 bitangentEndpoint[]; in highp vec4 normalEndpoint[]; #endif +#ifdef INSTANCED_OBJECT_ID +flat in highp uint interpolatedVsInstanceObjectId[]; +#endif +#ifdef VERTEX_ID +in highp float interpolatedVsMappedVertexId[]; +#endif +#ifdef PRIMITIVE_ID_FROM_VERTEX_ID +flat in highp uint interpolatedVsPrimitiveId[]; +#endif + +/* Outputs */ + layout(triangle_strip, max_vertices = MAX_VERTICES) out; /* Interpolate in screen space (if possible) */ @@ -100,15 +116,12 @@ noperspective out lowp vec3 dist; #ifdef INSTANCED_OBJECT_ID -flat in highp uint interpolatedVsInstanceObjectId[]; flat out highp uint interpolatedInstanceObjectId; #endif #ifdef VERTEX_ID -in highp float interpolatedVsMappedVertexId[]; out highp float interpolatedMappedVertexId; #endif #ifdef PRIMITIVE_ID_FROM_VERTEX_ID -flat in highp uint interpolatedVsPrimitiveId[]; flat out highp uint interpolatedPrimitiveId; #endif diff --git a/src/Magnum/Shaders/MeshVisualizer.vert b/src/Magnum/Shaders/MeshVisualizer.vert index cbfe22518..a67ebc024 100644 --- a/src/Magnum/Shaders/MeshVisualizer.vert +++ b/src/Magnum/Shaders/MeshVisualizer.vert @@ -32,6 +32,8 @@ #define out varying #endif +/* Uniforms */ + #ifdef TWO_DIMENSIONS #ifdef EXPLICIT_UNIFORM_LOCATION layout(location = 0) @@ -95,6 +97,8 @@ uniform highp float lineLength ; #endif +/* Inputs */ + #ifdef EXPLICIT_ATTRIB_LOCATION layout(location = POSITION_ATTRIBUTE_LOCATION) #endif @@ -135,8 +139,6 @@ layout(location = 4) in lowp float vertexIndex; #define gl_VertexID int(vertexIndex) #endif - -out vec3 barycentric; #endif #ifdef INSTANCED_OBJECT_ID @@ -144,7 +146,15 @@ out vec3 barycentric; layout(location = OBJECT_ID_ATTRIBUTE_LOCATION) #endif in highp uint instanceObjectId; +#endif + +/* Outputs */ +#if defined(WIREFRAME_RENDERING) && defined(NO_GEOMETRY_SHADER) +out vec3 barycentric; +#endif + +#ifdef INSTANCED_OBJECT_ID #ifdef NO_GEOMETRY_SHADER flat out highp uint interpolatedInstanceObjectId; #else diff --git a/src/Magnum/Shaders/Phong.frag b/src/Magnum/Shaders/Phong.frag index 1201c8a2f..7f276f0a7 100644 --- a/src/Magnum/Shaders/Phong.frag +++ b/src/Magnum/Shaders/Phong.frag @@ -37,12 +37,7 @@ #define const #endif -#ifdef AMBIENT_TEXTURE -#ifdef EXPLICIT_TEXTURE_LAYER -layout(binding = 0) -#endif -uniform lowp sampler2D ambientTexture; -#endif +/* Uniforms */ #ifdef EXPLICIT_UNIFORM_LOCATION layout(location = 4) @@ -58,12 +53,6 @@ uniform lowp vec4 ambientColor ; #if LIGHT_COUNT -#ifdef DIFFUSE_TEXTURE -#ifdef EXPLICIT_TEXTURE_LAYER -layout(binding = 1) -#endif -uniform lowp sampler2D diffuseTexture; -#endif #ifdef EXPLICIT_UNIFORM_LOCATION layout(location = 5) @@ -74,20 +63,6 @@ uniform lowp vec4 diffuseColor #endif ; -#ifdef SPECULAR_TEXTURE -#ifdef EXPLICIT_TEXTURE_LAYER -layout(binding = 2) -#endif -uniform lowp sampler2D specularTexture; -#endif - -#ifdef NORMAL_TEXTURE -#ifdef EXPLICIT_TEXTURE_LAYER -layout(binding = 3) -#endif -uniform lowp sampler2D normalTexture; -#endif - #ifdef EXPLICIT_UNIFORM_LOCATION layout(location = 6) #endif @@ -170,6 +145,40 @@ uniform lowp float lightRanges[LIGHT_COUNT] ; #endif +/* Textures */ + +#ifdef AMBIENT_TEXTURE +#ifdef EXPLICIT_TEXTURE_LAYER +layout(binding = 0) +#endif +uniform lowp sampler2D ambientTexture; +#endif + +#if LIGHT_COUNT +#ifdef DIFFUSE_TEXTURE +#ifdef EXPLICIT_TEXTURE_LAYER +layout(binding = 1) +#endif +uniform lowp sampler2D diffuseTexture; +#endif + +#ifdef SPECULAR_TEXTURE +#ifdef EXPLICIT_TEXTURE_LAYER +layout(binding = 2) +#endif +uniform lowp sampler2D specularTexture; +#endif + +#ifdef NORMAL_TEXTURE +#ifdef EXPLICIT_TEXTURE_LAYER +layout(binding = 3) +#endif +uniform lowp sampler2D normalTexture; +#endif +#endif + +/* Inputs */ + #if LIGHT_COUNT in mediump vec3 transformedNormal; #ifdef NORMAL_TEXTURE @@ -196,6 +205,8 @@ in lowp vec4 interpolatedVertexColor; flat in highp uint interpolatedInstanceObjectId; #endif +/* Outputs */ + #ifdef NEW_GLSL #ifdef EXPLICIT_ATTRIB_LOCATION layout(location = COLOR_OUTPUT_ATTRIBUTE_LOCATION) diff --git a/src/Magnum/Shaders/Phong.vert b/src/Magnum/Shaders/Phong.vert index efcc5963c..80f48ce04 100644 --- a/src/Magnum/Shaders/Phong.vert +++ b/src/Magnum/Shaders/Phong.vert @@ -32,6 +32,8 @@ #define out varying #endif +/* Uniforms */ + #ifdef EXPLICIT_UNIFORM_LOCATION layout(location = 0) #endif @@ -84,6 +86,8 @@ uniform highp vec4 lightPositions[LIGHT_COUNT] ; #endif +/* Inputs */ + #ifdef EXPLICIT_ATTRIB_LOCATION layout(location = POSITION_ATTRIBUTE_LOCATION) #endif @@ -162,6 +166,8 @@ layout(location = TEXTURE_OFFSET_ATTRIBUTE_LOCATION) in mediump vec2 instancedTextureOffset; #endif +/* Outputs */ + #if LIGHT_COUNT out mediump vec3 transformedNormal; #ifdef NORMAL_TEXTURE diff --git a/src/Magnum/Shaders/Vector.frag b/src/Magnum/Shaders/Vector.frag index 8904a5611..a10ac4931 100644 --- a/src/Magnum/Shaders/Vector.frag +++ b/src/Magnum/Shaders/Vector.frag @@ -29,6 +29,8 @@ #define texture texture2D #endif +/* Uniforms */ + #ifdef EXPLICIT_UNIFORM_LOCATION layout(location = 2) #endif @@ -49,8 +51,12 @@ layout(binding = 6) #endif uniform lowp sampler2D vectorTexture; +/* Inputs */ + in mediump vec2 interpolatedTextureCoordinates; +/* Outputs */ + #ifdef NEW_GLSL #ifdef EXPLICIT_ATTRIB_LOCATION layout(location = COLOR_OUTPUT_ATTRIBUTE_LOCATION) diff --git a/src/Magnum/Shaders/VertexColor.frag b/src/Magnum/Shaders/VertexColor.frag index 31e796cd6..acf3a6ac7 100644 --- a/src/Magnum/Shaders/VertexColor.frag +++ b/src/Magnum/Shaders/VertexColor.frag @@ -28,8 +28,12 @@ #define fragmentColor gl_FragColor #endif +/* Inputs */ + in lowp vec4 interpolatedColor; +/* Outputs */ + #ifdef NEW_GLSL #ifdef EXPLICIT_ATTRIB_LOCATION layout(location = COLOR_OUTPUT_ATTRIBUTE_LOCATION) diff --git a/src/Magnum/Shaders/VertexColor.vert b/src/Magnum/Shaders/VertexColor.vert index 434c89fda..fda6574e3 100644 --- a/src/Magnum/Shaders/VertexColor.vert +++ b/src/Magnum/Shaders/VertexColor.vert @@ -28,6 +28,8 @@ #define out varying #endif +/* Uniforms */ + #ifdef EXPLICIT_UNIFORM_LOCATION layout(location = 0) #endif @@ -47,6 +49,8 @@ uniform highp mat4 transformationProjectionMatrix #error #endif +/* Inputs */ + #ifdef EXPLICIT_ATTRIB_LOCATION layout(location = POSITION_ATTRIBUTE_LOCATION) #endif @@ -63,6 +67,8 @@ layout(location = COLOR_ATTRIBUTE_LOCATION) #endif in lowp vec4 color; +/* Outputs */ + out lowp vec4 interpolatedColor; void main() {