From 81b69f17e1c3299aae9e762bd8dfc808449ba78d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Vladim=C3=ADr=20Vondru=C5=A1?= Date: Sat, 10 Nov 2012 21:26:00 +0100 Subject: [PATCH] Shaders: another iteration in porting of Phong and Flat shaders to GLES. Hope the reduced precision won't hurt. --- src/Shaders/FlatShader2D.frag | 4 ++-- src/Shaders/FlatShader2D.vert | 6 +++--- src/Shaders/PhongShader.frag | 28 ++++++++++++++-------------- src/Shaders/PhongShader.vert | 24 ++++++++++++------------ src/Shaders/compatibility.glsl | 7 +++++++ 5 files changed, 38 insertions(+), 31 deletions(-) diff --git a/src/Shaders/FlatShader2D.frag b/src/Shaders/FlatShader2D.frag index bdc52dd38..44051058d 100644 --- a/src/Shaders/FlatShader2D.frag +++ b/src/Shaders/FlatShader2D.frag @@ -2,10 +2,10 @@ #define fragmentColor gl_FragColor #endif -uniform vec3 color; +uniform lowp vec3 color; #ifndef NEW_GLSL -out vec4 fragmentColor; +out lowp vec4 fragmentColor; #endif void main() { diff --git a/src/Shaders/FlatShader2D.vert b/src/Shaders/FlatShader2D.vert index 60355d131..dc702bf98 100644 --- a/src/Shaders/FlatShader2D.vert +++ b/src/Shaders/FlatShader2D.vert @@ -2,12 +2,12 @@ #define in attribute #endif -uniform mat3 transformationProjection; +uniform highp mat3 transformationProjection; #ifdef EXPLICIT_ATTRIB_LOCATION -layout(location = 0) in vec3 position; +layout(location = 0) in highp vec3 position; #else -in vec3 position; +in highp vec3 position; #endif void main() { diff --git a/src/Shaders/PhongShader.frag b/src/Shaders/PhongShader.frag index 638a1e3fa..aa8125401 100644 --- a/src/Shaders/PhongShader.frag +++ b/src/Shaders/PhongShader.frag @@ -3,35 +3,35 @@ #define color gl_FragColor #endif -uniform vec3 ambientColor = vec3(0.0, 0.0, 0.0); -uniform vec3 diffuseColor; -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; +uniform lowp vec3 ambientColor = vec3(0.0, 0.0, 0.0); +uniform lowp vec3 diffuseColor; +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; -in vec3 transformedNormal; -in vec3 lightDirection; -in vec3 cameraDirection; +in mediump vec3 transformedNormal; +in highp vec3 lightDirection; +in highp vec3 cameraDirection; #ifdef NEW_GLSL -out vec4 color; +out lowp vec4 color; #endif void main() { /* Ambient color */ color.rgb = ambientColor; - vec3 normalizedTransformedNormal = normalize(transformedNormal); - vec3 normalizedLightDirection = normalize(lightDirection); + mediump vec3 normalizedTransformedNormal = normalize(transformedNormal); + highp vec3 normalizedLightDirection = normalize(lightDirection); /* Add diffuse color */ - float intensity = max(0.0, dot(normalizedTransformedNormal, normalizedLightDirection)); + lowp float intensity = max(0.0, dot(normalizedTransformedNormal, normalizedLightDirection)); color.rgb += diffuseColor*lightColor*intensity; /* Add specular color, if needed */ if(intensity != 0) { - vec3 reflection = reflect(-normalizedLightDirection, normalizedTransformedNormal); - float specularity = pow(max(0.0, dot(normalize(cameraDirection), reflection)), shininess); + highp vec3 reflection = reflect(-normalizedLightDirection, normalizedTransformedNormal); + mediump float specularity = pow(max(0.0, dot(normalize(cameraDirection), reflection)), shininess); color.rgb += specularColor*specularity; } diff --git a/src/Shaders/PhongShader.vert b/src/Shaders/PhongShader.vert index 1c794cbce..b544d63f8 100644 --- a/src/Shaders/PhongShader.vert +++ b/src/Shaders/PhongShader.vert @@ -3,26 +3,26 @@ #define out varying #endif -uniform mat4 transformationMatrix; -uniform mat4 projectionMatrix; -uniform vec3 light; +uniform highp mat4 transformationMatrix; +uniform highp mat4 projectionMatrix; +uniform highp vec3 light; #ifdef EXPLICIT_ATTRIB_LOCATION -layout(location = 0) in vec4 position; -layout(location = 1) in vec3 normal; +layout(location = 0) in highp vec4 position; +layout(location = 1) in mediump vec3 normal; #else -in vec4 position; -in vec3 normal; +in highp vec4 position; +in mediump vec3 normal; #endif -out vec3 transformedNormal; -out vec3 lightDirection; -out vec3 cameraDirection; +out mediump vec3 transformedNormal; +out highp vec3 lightDirection; +out highp vec3 cameraDirection; void main() { /* Transformed vertex position */ - vec4 transformedPosition4 = transformationMatrix*position; - vec3 transformedPosition = transformedPosition4.xyz/transformedPosition4.w; + highp vec4 transformedPosition4 = transformationMatrix*position; + highp vec3 transformedPosition = transformedPosition4.xyz/transformedPosition4.w; /* Transformed normal vector */ transformedNormal = normalize(mat3x3(transformationMatrix)*normal); diff --git a/src/Shaders/compatibility.glsl b/src/Shaders/compatibility.glsl index 2ccbfae6b..70f4cd6ba 100644 --- a/src/Shaders/compatibility.glsl +++ b/src/Shaders/compatibility.glsl @@ -12,3 +12,10 @@ #if defined(GL_ES) && __VERSION__ >= 300 #define EXPLICIT_ATTRIB_LOCATION #endif + +/* Precision qualifiers are not supported in GLSL 1.20 */ +#if !defined(GL_ES) && __VERSION__ == 120 +#define highp +#define mediump +#define lowp +#endif