From 8c2eaea406b35dcf9caa41cddc6eb845ca052f82 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Vladim=C3=ADr=20Vondru=C5=A1?= Date: Sun, 6 Sep 2015 11:28:28 +0200 Subject: [PATCH] Shaders: more shader code cleanup. Fixed random potential GLSL ES issues, reduced preprocessor madness. --- src/Magnum/Shaders/AbstractVector2D.vert | 17 ++++---- src/Magnum/Shaders/AbstractVector3D.vert | 17 ++++---- src/Magnum/Shaders/DistanceFieldVector.frag | 40 +++++++++++-------- src/Magnum/Shaders/Flat2D.vert | 16 ++++---- src/Magnum/Shaders/Flat3D.vert | 16 ++++---- src/Magnum/Shaders/MeshVisualizer.frag | 44 +++++++++++++++------ src/Magnum/Shaders/MeshVisualizer.geom | 5 +-- src/Magnum/Shaders/MeshVisualizer.vert | 15 +++---- src/Magnum/Shaders/Phong.vert | 36 +++++++++++------ src/Magnum/Shaders/Vector.frag | 15 +++---- src/Magnum/Shaders/VertexColor2D.vert | 23 +++++------ 11 files changed, 139 insertions(+), 105 deletions(-) diff --git a/src/Magnum/Shaders/AbstractVector2D.vert b/src/Magnum/Shaders/AbstractVector2D.vert index eaaf6e806..abf05e9f1 100644 --- a/src/Magnum/Shaders/AbstractVector2D.vert +++ b/src/Magnum/Shaders/AbstractVector2D.vert @@ -29,20 +29,21 @@ #endif #ifdef EXPLICIT_UNIFORM_LOCATION -layout(location = 0) uniform mat3 transformationProjectionMatrix; -#else -uniform highp mat3 transformationProjectionMatrix; +layout(location = 0) #endif +uniform highp mat3 transformationProjectionMatrix; #ifdef EXPLICIT_ATTRIB_LOCATION -layout(location = POSITION_ATTRIBUTE_LOCATION) in highp vec2 position; -layout(location = TEXTURECOORDINATES_ATTRIBUTE_LOCATION) in mediump vec2 textureCoordinates; -#else +layout(location = POSITION_ATTRIBUTE_LOCATION) +#endif in highp vec2 position; -in mediump vec2 textureCoordinates; + +#ifdef EXPLICIT_ATTRIB_LOCATION +layout(location = TEXTURECOORDINATES_ATTRIBUTE_LOCATION) #endif +in mediump vec2 textureCoordinates; -out vec2 fragmentTextureCoordinates; +out mediump vec2 fragmentTextureCoordinates; void main() { gl_Position.xywz = vec4(transformationProjectionMatrix*vec3(position, 1.0), 0.0); diff --git a/src/Magnum/Shaders/AbstractVector3D.vert b/src/Magnum/Shaders/AbstractVector3D.vert index c5c76b159..e30d1b3ee 100644 --- a/src/Magnum/Shaders/AbstractVector3D.vert +++ b/src/Magnum/Shaders/AbstractVector3D.vert @@ -29,20 +29,21 @@ #endif #ifdef EXPLICIT_UNIFORM_LOCATION -layout(location = 0) uniform mat4 transformationProjectionMatrix; -#else -uniform highp mat4 transformationProjectionMatrix; +layout(location = 0) #endif +uniform highp mat4 transformationProjectionMatrix; #ifdef EXPLICIT_ATTRIB_LOCATION -layout(location = POSITION_ATTRIBUTE_LOCATION) in highp vec4 position; -layout(location = TEXTURECOORDINATES_ATTRIBUTE_LOCATION) in mediump vec2 textureCoordinates; -#else +layout(location = POSITION_ATTRIBUTE_LOCATION) +#endif in highp vec4 position; -in mediump vec2 textureCoordinates; + +#ifdef EXPLICIT_ATTRIB_LOCATION +layout(location = TEXTURECOORDINATES_ATTRIBUTE_LOCATION) #endif +in mediump vec2 textureCoordinates; -out vec2 fragmentTextureCoordinates; +out mediump vec2 fragmentTextureCoordinates; void main() { gl_Position = transformationProjectionMatrix*position; diff --git a/src/Magnum/Shaders/DistanceFieldVector.frag b/src/Magnum/Shaders/DistanceFieldVector.frag index 23a67a6fb..02da7fa75 100644 --- a/src/Magnum/Shaders/DistanceFieldVector.frag +++ b/src/Magnum/Shaders/DistanceFieldVector.frag @@ -29,30 +29,38 @@ #define texture texture2D #endif -#ifndef GL_ES #ifdef EXPLICIT_UNIFORM_LOCATION -layout(location = 1) uniform lowp vec4 color; -layout(location = 2) uniform lowp vec4 outlineColor; -layout(location = 3) uniform lowp vec2 outlineRange = vec2(0.5, 1.0); -layout(location = 4) uniform lowp float smoothness = 0.04; -#else -uniform lowp vec4 color; -uniform lowp vec4 outlineColor; -uniform lowp vec2 outlineRange = vec2(0.5, 1.0); -uniform lowp float smoothness = 0.04; +layout(location = 1) #endif -#else uniform lowp vec4 color; + +#ifdef EXPLICIT_UNIFORM_LOCATION +layout(location = 2) +#endif uniform lowp vec4 outlineColor; -uniform lowp vec2 outlineRange; -uniform lowp float smoothness; + +#ifdef EXPLICIT_UNIFORM_LOCATION +layout(location = 3) +#endif +uniform lowp vec2 outlineRange + #ifndef GL_ES + = vec2(0.5, 1.0) + #endif + ; + +#ifdef EXPLICIT_UNIFORM_LOCATION +layout(location = 4) #endif +uniform lowp float smoothness + #ifndef GL_ES + = 0.04 + #endif + ; #ifdef EXPLICIT_TEXTURE_LAYER -layout(binding = 15) uniform sampler2D vectorTexture; -#else -uniform lowp sampler2D vectorTexture; +layout(binding = 15) #endif +uniform lowp sampler2D vectorTexture; in mediump vec2 fragmentTextureCoordinates; diff --git a/src/Magnum/Shaders/Flat2D.vert b/src/Magnum/Shaders/Flat2D.vert index 43544cf3b..dd9156847 100644 --- a/src/Magnum/Shaders/Flat2D.vert +++ b/src/Magnum/Shaders/Flat2D.vert @@ -29,23 +29,21 @@ #endif #ifdef EXPLICIT_UNIFORM_LOCATION -layout(location = 0) uniform mat3 transformationProjectionMatrix; -#else -uniform highp mat3 transformationProjectionMatrix; +layout(location = 0) #endif +uniform highp mat3 transformationProjectionMatrix; #ifdef EXPLICIT_ATTRIB_LOCATION -layout(location = POSITION_ATTRIBUTE_LOCATION) in highp vec2 position; -#else -in highp vec2 position; +layout(location = POSITION_ATTRIBUTE_LOCATION) #endif +in highp vec2 position; #ifdef TEXTURED #ifdef EXPLICIT_ATTRIB_LOCATION -layout(location = TEXTURECOORDINATES_ATTRIBUTE_LOCATION) in mediump vec2 textureCoordinates; -#else -in mediump vec2 textureCoordinates; +layout(location = TEXTURECOORDINATES_ATTRIBUTE_LOCATION) #endif +in mediump vec2 textureCoordinates; + out mediump vec2 interpolatedTextureCoordinates; #endif diff --git a/src/Magnum/Shaders/Flat3D.vert b/src/Magnum/Shaders/Flat3D.vert index 8e0163683..0e02a0867 100644 --- a/src/Magnum/Shaders/Flat3D.vert +++ b/src/Magnum/Shaders/Flat3D.vert @@ -29,23 +29,21 @@ #endif #ifdef EXPLICIT_UNIFORM_LOCATION -layout(location = 0) uniform mat4 transformationProjectionMatrix; -#else -uniform highp mat4 transformationProjectionMatrix; +layout(location = 0) #endif +uniform highp mat4 transformationProjectionMatrix; #ifdef EXPLICIT_ATTRIB_LOCATION -layout(location = POSITION_ATTRIBUTE_LOCATION) in highp vec4 position; -#else -in highp vec4 position; +layout(location = POSITION_ATTRIBUTE_LOCATION) #endif +in highp vec4 position; #ifdef TEXTURED #ifdef EXPLICIT_ATTRIB_LOCATION -layout(location = TEXTURECOORDINATES_ATTRIBUTE_LOCATION) in mediump vec2 textureCoordinates; -#else -in mediump vec2 textureCoordinates; +layout(location = TEXTURECOORDINATES_ATTRIBUTE_LOCATION) #endif +in mediump vec2 textureCoordinates; + out mediump vec2 interpolatedTextureCoordinates; #endif diff --git a/src/Magnum/Shaders/MeshVisualizer.frag b/src/Magnum/Shaders/MeshVisualizer.frag index 8c48eb079..bc9bf5460 100644 --- a/src/Magnum/Shaders/MeshVisualizer.frag +++ b/src/Magnum/Shaders/MeshVisualizer.frag @@ -44,22 +44,42 @@ #extension GL_NV_shader_noperspective_interpolation: require #endif -#ifndef GL_ES -layout(location = 2) uniform vec4 color = vec4(1.0, 1.0, 1.0, 1.0); -#else -uniform lowp vec4 color; +#ifdef EXPLICIT_UNIFORM_LOCATION +layout(location = 2) #endif +uniform lowp vec4 color + #ifndef GL_ES + = vec4(1.0, 1.0, 1.0, 1.0) + #endif + ; #ifdef WIREFRAME_RENDERING -#ifndef GL_ES -layout(location = 3) uniform vec4 wireframeColor = vec4(0.0, 0.0, 0.0, 1.0); -layout(location = 4) uniform float wireframeWidth = 1.0; -layout(location = 5) uniform float smoothness = 2.0; -#else -uniform lowp vec4 wireframeColor; -uniform lowp float wireframeWidth; -uniform lowp float smoothness; +#ifdef EXPLICIT_UNIFORM_LOCATION +layout(location = 3) +#endif +uniform lowp vec4 wireframeColor + #ifndef GL_ES + = vec4(0.0, 0.0, 0.0, 1.0) + #endif + ; + +#ifdef EXPLICIT_UNIFORM_LOCATION +layout(location = 4) #endif +uniform lowp float wireframeWidth + #ifndef GL_ES + = 1.0 + #endif + ; + +#ifdef EXPLICIT_UNIFORM_LOCATION +layout(location = 5) +#endif +uniform lowp float smoothness + #ifndef GL_ES + = 2.0 + #endif + ; #ifndef NO_GEOMETRY_SHADER #ifdef GL_NV_shader_noperspective_interpolation diff --git a/src/Magnum/Shaders/MeshVisualizer.geom b/src/Magnum/Shaders/MeshVisualizer.geom index 109e21fe2..1d22a3a31 100644 --- a/src/Magnum/Shaders/MeshVisualizer.geom +++ b/src/Magnum/Shaders/MeshVisualizer.geom @@ -35,10 +35,9 @@ #endif #ifdef EXPLICIT_UNIFORM_LOCATION -layout(location = 1) uniform vec2 viewportSize; -#else -uniform vec2 viewportSize; +layout(location = 1) #endif +uniform lowp vec2 viewportSize; layout(triangles) in; diff --git a/src/Magnum/Shaders/MeshVisualizer.vert b/src/Magnum/Shaders/MeshVisualizer.vert index 7a105aed2..4495b7108 100644 --- a/src/Magnum/Shaders/MeshVisualizer.vert +++ b/src/Magnum/Shaders/MeshVisualizer.vert @@ -29,24 +29,21 @@ #endif #ifdef EXPLICIT_UNIFORM_LOCATION -layout(location = 0) uniform mat4 transformationProjectionMatrix; -#else -uniform highp mat4 transformationProjectionMatrix; +layout(location = 0) #endif +uniform highp mat4 transformationProjectionMatrix; #ifdef EXPLICIT_ATTRIB_LOCATION -layout(location = POSITION_ATTRIBUTE_LOCATION) in highp vec4 position; -#else -in highp vec4 position; +layout(location = POSITION_ATTRIBUTE_LOCATION) #endif +in highp vec4 position; #if defined(WIREFRAME_RENDERING) && defined(NO_GEOMETRY_SHADER) #if (!defined(GL_ES) && __VERSION__ < 140) || (defined(GL_ES) && __VERSION__ < 300) #ifdef EXPLICIT_ATTRIB_LOCATION -layout(location = 3) in lowp float vertexIndex; -#else -in lowp float vertexIndex; +layout(location = 3) #endif +in lowp float vertexIndex; #define gl_VertexID int(vertexIndex) #endif diff --git a/src/Magnum/Shaders/Phong.vert b/src/Magnum/Shaders/Phong.vert index 8e93565f0..0a3321583 100644 --- a/src/Magnum/Shaders/Phong.vert +++ b/src/Magnum/Shaders/Phong.vert @@ -29,31 +29,41 @@ #endif #ifdef EXPLICIT_UNIFORM_LOCATION -layout(location = 0) uniform mat4 transformationMatrix; -layout(location = 1) uniform mat4 projectionMatrix; -layout(location = 2) uniform mat3 normalMatrix; -layout(location = 3) uniform vec3 light; -#else +layout(location = 0) +#endif uniform highp mat4 transformationMatrix; + +#ifdef EXPLICIT_UNIFORM_LOCATION +layout(location = 1) +#endif uniform highp mat4 projectionMatrix; + +#ifdef EXPLICIT_UNIFORM_LOCATION +layout(location = 2) +#endif uniform mediump mat3 normalMatrix; -uniform highp vec3 light; + +#ifdef EXPLICIT_UNIFORM_LOCATION +layout(location = 3) #endif +uniform highp vec3 light; #ifdef EXPLICIT_ATTRIB_LOCATION -layout(location = POSITION_ATTRIBUTE_LOCATION) in highp vec4 position; -layout(location = NORMAL_ATTRIBUTE_LOCATION) in mediump vec3 normal; -#else +layout(location = POSITION_ATTRIBUTE_LOCATION) +#endif in highp vec4 position; -in mediump vec3 normal; + +#ifdef EXPLICIT_ATTRIB_LOCATION +layout(location = NORMAL_ATTRIBUTE_LOCATION) #endif +in mediump vec3 normal; #ifdef TEXTURED #ifdef EXPLICIT_ATTRIB_LOCATION -layout(location = TEXTURECOORDINATES_ATTRIBUTE_LOCATION) in mediump vec2 textureCoords; -#else -in mediump vec2 textureCoords; +layout(location = TEXTURECOORDINATES_ATTRIBUTE_LOCATION) #endif +in mediump vec2 textureCoords; + out mediump vec2 interpolatedTextureCoords; #endif diff --git a/src/Magnum/Shaders/Vector.frag b/src/Magnum/Shaders/Vector.frag index 40c46af82..6e354d8c8 100644 --- a/src/Magnum/Shaders/Vector.frag +++ b/src/Magnum/Shaders/Vector.frag @@ -30,18 +30,19 @@ #endif #ifdef EXPLICIT_UNIFORM_LOCATION -layout(location = 1) uniform vec4 backgroundColor; -layout(location = 2) uniform vec4 color; -#else +layout(location = 1) +#endif uniform lowp vec4 backgroundColor; -uniform lowp vec4 color; + +#ifdef EXPLICIT_UNIFORM_LOCATION +layout(location = 2) #endif +uniform lowp vec4 color; #ifdef EXPLICIT_TEXTURE_LAYER -layout(binding = 15) uniform sampler2D vectorTexture; -#else -uniform lowp sampler2D vectorTexture; +layout(binding = 15) #endif +uniform lowp sampler2D vectorTexture; in mediump vec2 fragmentTextureCoordinates; diff --git a/src/Magnum/Shaders/VertexColor2D.vert b/src/Magnum/Shaders/VertexColor2D.vert index e3c5de102..ea9134fea 100644 --- a/src/Magnum/Shaders/VertexColor2D.vert +++ b/src/Magnum/Shaders/VertexColor2D.vert @@ -28,23 +28,24 @@ #define out varying #endif -#ifndef GL_ES #ifdef EXPLICIT_UNIFORM_LOCATION -layout(location = 0) uniform mat3 transformationProjectionMatrix = mat3(1.0); -#else -uniform mat3 transformationProjectionMatrix = mat3(1.0); -#endif -#else -uniform highp mat3 transformationProjectionMatrix; +layout(location = 0) #endif +uniform highp mat3 transformationProjectionMatrix + #ifndef GL_ES + = mat3(1.0) + #endif + ; #ifdef EXPLICIT_ATTRIB_LOCATION -layout(location = POSITION_ATTRIBUTE_LOCATION) in highp vec2 position; -layout(location = 3) in lowp vec3 color; -#else +layout(location = POSITION_ATTRIBUTE_LOCATION) +#endif in highp vec2 position; -in lowp vec3 color; + +#ifdef EXPLICIT_ATTRIB_LOCATION +layout(location = 3) #endif +in lowp vec3 color; out lowp vec3 interpolatedColor;