From 7a894e97a0f18288503108b84b49d635c0073419 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Vladim=C3=ADr=20Vondru=C5=A1?= Date: Wed, 12 Dec 2012 19:26:58 +0100 Subject: [PATCH] Using ARB_explicit_uniform_location in shaders. The backwards compatibility fully kills the purpose, but at least I can test the feature somewhere :-) --- src/Shaders/FlatShader.cpp | 10 ++++++++-- src/Shaders/FlatShader.h | 4 ++-- src/Shaders/FlatShader2D.frag | 4 ++++ src/Shaders/FlatShader2D.vert | 4 ++++ src/Shaders/PhongShader.cpp | 24 +++++++++++++++--------- src/Shaders/PhongShader.frag | 19 ++++++++++++++----- src/Shaders/PhongShader.h | 18 +++++++++--------- src/Shaders/PhongShader.vert | 7 +++++++ src/Shaders/VertexColorShader.cpp | 8 +++++++- src/Shaders/VertexColorShader.h | 2 +- src/Shaders/VertexColorShader2D.vert | 4 ++++ src/Shaders/compatibility.glsl | 15 ++++++++++++--- 12 files changed, 87 insertions(+), 32 deletions(-) diff --git a/src/Shaders/FlatShader.cpp b/src/Shaders/FlatShader.cpp index d232e342b..aba54c0f3 100644 --- a/src/Shaders/FlatShader.cpp +++ b/src/Shaders/FlatShader.cpp @@ -65,8 +65,14 @@ template FlatShader::FlatShader() { link(); - transformationProjectionMatrixUniform = uniformLocation("transformationProjectionMatrix"); - colorUniform = uniformLocation("color"); + #ifndef MAGNUM_TARGET_GLES + if(!Context::current()->isExtensionSupported()) { + #else + { + #endif + transformationProjectionMatrixUniform = uniformLocation("transformationProjectionMatrix"); + colorUniform = uniformLocation("color"); + } } template class FlatShader<2>; diff --git a/src/Shaders/FlatShader.h b/src/Shaders/FlatShader.h index e8787cdd4..dc83f8d60 100644 --- a/src/Shaders/FlatShader.h +++ b/src/Shaders/FlatShader.h @@ -61,8 +61,8 @@ template class MAGNUM_SHADERS_EXPORT FlatShader: public } private: - GLint transformationProjectionMatrixUniform, - colorUniform; + GLint transformationProjectionMatrixUniform = 0, + colorUniform = 1; }; /** @brief 2D flat shader */ diff --git a/src/Shaders/FlatShader2D.frag b/src/Shaders/FlatShader2D.frag index 82c2d5a95..ce2c5867a 100644 --- a/src/Shaders/FlatShader2D.frag +++ b/src/Shaders/FlatShader2D.frag @@ -2,7 +2,11 @@ #define fragmentColor gl_FragColor #endif +#ifdef EXPLICIT_UNIFORM_LOCATION +layout(location = 1) uniform vec3 color; +#else uniform lowp vec3 color; +#endif #ifdef NEW_GLSL out lowp vec4 fragmentColor; diff --git a/src/Shaders/FlatShader2D.vert b/src/Shaders/FlatShader2D.vert index 78d041e8d..b976b595c 100644 --- a/src/Shaders/FlatShader2D.vert +++ b/src/Shaders/FlatShader2D.vert @@ -2,7 +2,11 @@ #define in attribute #endif +#ifdef EXPLICIT_UNIFORM_LOCATION +layout(location = 0) uniform mat3 transformationProjectionMatrix; +#else uniform highp mat3 transformationProjectionMatrix; +#endif #ifdef EXPLICIT_ATTRIB_LOCATION layout(location = 0) in highp vec3 position; diff --git a/src/Shaders/PhongShader.cpp b/src/Shaders/PhongShader.cpp index 8ca78b44d..638831d8e 100644 --- a/src/Shaders/PhongShader.cpp +++ b/src/Shaders/PhongShader.cpp @@ -52,15 +52,21 @@ PhongShader::PhongShader() { link(); - ambientColorUniform = uniformLocation("ambientColor"); - diffuseColorUniform = uniformLocation("diffuseColor"); - specularColorUniform = uniformLocation("specularColor"); - shininessUniform = uniformLocation("shininess"); - transformationMatrixUniform = uniformLocation("transformationMatrix"); - projectionMatrixUniform = uniformLocation("projectionMatrix"); - normalMatrixUniform = uniformLocation("normalMatrix"); - lightUniform = uniformLocation("light"); - lightColorUniform = uniformLocation("lightColor"); + #ifndef MAGNUM_TARGET_GLES + if(!Context::current()->isExtensionSupported()) { + #else + { + #endif + transformationMatrixUniform = uniformLocation("transformationMatrix"); + projectionMatrixUniform = uniformLocation("projectionMatrix"); + normalMatrixUniform = uniformLocation("normalMatrix"); + lightUniform = uniformLocation("light"); + diffuseColorUniform = uniformLocation("diffuseColor"); + ambientColorUniform = uniformLocation("ambientColor"); + specularColorUniform = uniformLocation("specularColor"); + lightColorUniform = uniformLocation("lightColor"); + shininessUniform = uniformLocation("shininess"); + } /* Set defaults in OpenGL ES (for desktop they are set in shader code itself) */ #ifdef MAGNUM_TARGET_GLES diff --git a/src/Shaders/PhongShader.frag b/src/Shaders/PhongShader.frag index 3bb1080ac..4b00f8cd5 100644 --- a/src/Shaders/PhongShader.frag +++ b/src/Shaders/PhongShader.frag @@ -3,13 +3,22 @@ #define color gl_FragColor #endif -uniform lowp vec3 diffuseColor; #ifndef GL_ES -uniform lowp vec3 ambientColor = vec3(0.0, 0.0, 0.0); -uniform lowp vec3 specularColor = vec3(1.0, 1.0, 1.0); -uniform lowp vec3 lightColor = vec3(1.0, 1.0, 1.0); -uniform mediump float shininess = 80.0; +#ifdef EXPLICIT_UNIFORM_LOCATION +layout(location = 4) uniform vec3 diffuseColor; +layout(location = 5) uniform vec3 ambientColor = vec3(0.0, 0.0, 0.0); +layout(location = 6) uniform vec3 specularColor = vec3(1.0, 1.0, 1.0); +layout(location = 7) uniform vec3 lightColor = vec3(1.0, 1.0, 1.0); +layout(location = 8) uniform float shininess = 80.0; +#else +uniform vec3 diffuseColor; +uniform vec3 ambientColor = vec3(0.0, 0.0, 0.0); +uniform vec3 specularColor = vec3(1.0, 1.0, 1.0); +uniform vec3 lightColor = vec3(1.0, 1.0, 1.0); +uniform float shininess = 80.0; +#endif #else +uniform lowp vec3 diffuseColor; uniform lowp vec3 ambientColor; uniform lowp vec3 specularColor; uniform lowp vec3 lightColor; diff --git a/src/Shaders/PhongShader.h b/src/Shaders/PhongShader.h index 6be38d84c..cbfc4eba6 100644 --- a/src/Shaders/PhongShader.h +++ b/src/Shaders/PhongShader.h @@ -123,15 +123,15 @@ class MAGNUM_SHADERS_EXPORT PhongShader: public AbstractShaderProgram { } private: - GLint ambientColorUniform, - diffuseColorUniform, - specularColorUniform, - shininessUniform, - transformationMatrixUniform, - projectionMatrixUniform, - normalMatrixUniform, - lightUniform, - lightColorUniform; + GLint transformationMatrixUniform = 0, + projectionMatrixUniform = 1, + normalMatrixUniform = 2, + lightUniform = 3, + diffuseColorUniform = 4, + ambientColorUniform = 5, + specularColorUniform = 6, + lightColorUniform = 7, + shininessUniform = 8; }; }} diff --git a/src/Shaders/PhongShader.vert b/src/Shaders/PhongShader.vert index 066afd94d..99d3a3fe7 100644 --- a/src/Shaders/PhongShader.vert +++ b/src/Shaders/PhongShader.vert @@ -3,10 +3,17 @@ #define out varying #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 uniform highp mat4 transformationMatrix; uniform highp mat4 projectionMatrix; uniform mediump mat3 normalMatrix; uniform highp vec3 light; +#endif #ifdef EXPLICIT_ATTRIB_LOCATION layout(location = 0) in highp vec4 position; diff --git a/src/Shaders/VertexColorShader.cpp b/src/Shaders/VertexColorShader.cpp index 5313da38c..b2a93b8e5 100644 --- a/src/Shaders/VertexColorShader.cpp +++ b/src/Shaders/VertexColorShader.cpp @@ -66,7 +66,13 @@ template VertexColorShader::VertexColorShad link(); - transformationProjectionMatrixUniform = uniformLocation("transformationProjectionMatrix"); + #ifndef MAGNUM_TARGET_GLES + if(!Context::current()->isExtensionSupported()) { + #else + { + #endif + transformationProjectionMatrixUniform = uniformLocation("transformationProjectionMatrix"); + } } template class VertexColorShader<2>; diff --git a/src/Shaders/VertexColorShader.h b/src/Shaders/VertexColorShader.h index 896fefdc6..c529521ff 100644 --- a/src/Shaders/VertexColorShader.h +++ b/src/Shaders/VertexColorShader.h @@ -55,7 +55,7 @@ template class MAGNUM_SHADERS_EXPORT VertexColorShader: } private: - GLint transformationProjectionMatrixUniform; + GLint transformationProjectionMatrixUniform = 0; }; /** @brief 2D vertex color shader */ diff --git a/src/Shaders/VertexColorShader2D.vert b/src/Shaders/VertexColorShader2D.vert index 88c8fc9a5..ff300a5b1 100644 --- a/src/Shaders/VertexColorShader2D.vert +++ b/src/Shaders/VertexColorShader2D.vert @@ -3,7 +3,11 @@ #define out varying #endif +#ifdef EXPLICIT_UNIFORM_LOCATION +layout(location = 0) uniform mat3 transformationProjectionMatrix; +#else uniform highp mat3 transformationProjectionMatrix; +#endif #ifdef EXPLICIT_ATTRIB_LOCATION layout(location = 0) in highp vec3 position; diff --git a/src/Shaders/compatibility.glsl b/src/Shaders/compatibility.glsl index 70f4cd6ba..8c67705d5 100644 --- a/src/Shaders/compatibility.glsl +++ b/src/Shaders/compatibility.glsl @@ -4,13 +4,22 @@ /* On NVidia and GLSL 1.20 layout qualifiers result in parsing error, even if the extension is defined as supported */ -#if !defined(GL_ES) && __VERSION__ >= 120 && defined(GL_ARB_explicit_attrib_location) -#extension GL_ARB_explicit_attrib_location: enable -#define EXPLICIT_ATTRIB_LOCATION +#if !defined(GL_ES) && __VERSION__ >= 120 + +#ifdef GL_ARB_explicit_attrib_location + #extension GL_ARB_explicit_attrib_location: enable + #define EXPLICIT_ATTRIB_LOCATION +#endif +#ifdef GL_ARB_explicit_uniform_location + #extension GL_ARB_explicit_uniform_location: enable + #define EXPLICIT_UNIFORM_LOCATION +#endif + #endif #if defined(GL_ES) && __VERSION__ >= 300 #define EXPLICIT_ATTRIB_LOCATION +/* EXPLICIT_UNIFORM_LOCATION is not available in OpenGL ES */ #endif /* Precision qualifiers are not supported in GLSL 1.20 */