Browse Source

Using ARB_explicit_uniform_location in shaders.

The backwards compatibility fully kills the purpose, but at least I can
test the feature somewhere :-)
pull/7/head
Vladimír Vondruš 14 years ago
parent
commit
7a894e97a0
  1. 10
      src/Shaders/FlatShader.cpp
  2. 4
      src/Shaders/FlatShader.h
  3. 4
      src/Shaders/FlatShader2D.frag
  4. 4
      src/Shaders/FlatShader2D.vert
  5. 24
      src/Shaders/PhongShader.cpp
  6. 19
      src/Shaders/PhongShader.frag
  7. 18
      src/Shaders/PhongShader.h
  8. 7
      src/Shaders/PhongShader.vert
  9. 8
      src/Shaders/VertexColorShader.cpp
  10. 2
      src/Shaders/VertexColorShader.h
  11. 4
      src/Shaders/VertexColorShader2D.vert
  12. 15
      src/Shaders/compatibility.glsl

10
src/Shaders/FlatShader.cpp

@ -65,8 +65,14 @@ template<std::uint8_t dimensions> FlatShader<dimensions>::FlatShader() {
link();
transformationProjectionMatrixUniform = uniformLocation("transformationProjectionMatrix");
colorUniform = uniformLocation("color");
#ifndef MAGNUM_TARGET_GLES
if(!Context::current()->isExtensionSupported<Extensions::GL::ARB::explicit_uniform_location>()) {
#else
{
#endif
transformationProjectionMatrixUniform = uniformLocation("transformationProjectionMatrix");
colorUniform = uniformLocation("color");
}
}
template class FlatShader<2>;

4
src/Shaders/FlatShader.h

@ -61,8 +61,8 @@ template<std::uint8_t dimensions> class MAGNUM_SHADERS_EXPORT FlatShader: public
}
private:
GLint transformationProjectionMatrixUniform,
colorUniform;
GLint transformationProjectionMatrixUniform = 0,
colorUniform = 1;
};
/** @brief 2D flat shader */

4
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;

4
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;

24
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<Extensions::GL::ARB::explicit_uniform_location>()) {
#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

19
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;

18
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;
};
}}

7
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;

8
src/Shaders/VertexColorShader.cpp

@ -66,7 +66,13 @@ template<std::uint8_t dimensions> VertexColorShader<dimensions>::VertexColorShad
link();
transformationProjectionMatrixUniform = uniformLocation("transformationProjectionMatrix");
#ifndef MAGNUM_TARGET_GLES
if(!Context::current()->isExtensionSupported<Extensions::GL::ARB::explicit_uniform_location>()) {
#else
{
#endif
transformationProjectionMatrixUniform = uniformLocation("transformationProjectionMatrix");
}
}
template class VertexColorShader<2>;

2
src/Shaders/VertexColorShader.h

@ -55,7 +55,7 @@ template<std::uint8_t dimensions> class MAGNUM_SHADERS_EXPORT VertexColorShader:
}
private:
GLint transformationProjectionMatrixUniform;
GLint transformationProjectionMatrixUniform = 0;
};
/** @brief 2D vertex color shader */

4
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;

15
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 */

Loading…
Cancel
Save