From 084294e60b516656a2bb8b6157c59806abf8867f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Vladim=C3=ADr=20Vondru=C5=A1?= Date: Sun, 4 Nov 2012 16:09:09 +0100 Subject: [PATCH] Blind port of PhongShader to OpenGL ES 2.0. --- src/Shaders/CMakeLists.txt | 3 ++- src/Shaders/PhongShader.cpp | 23 ++++++++++++++++++++--- src/Shaders/PhongShader.frag | 4 ++-- src/Shaders/PhongShader.vert | 5 ++--- src/Shaders/compatibility.glsl | 14 ++++++++++++++ 5 files changed, 40 insertions(+), 9 deletions(-) create mode 100644 src/Shaders/compatibility.glsl diff --git a/src/Shaders/CMakeLists.txt b/src/Shaders/CMakeLists.txt index 53b99a0f2..926bc6238 100644 --- a/src/Shaders/CMakeLists.txt +++ b/src/Shaders/CMakeLists.txt @@ -1,6 +1,7 @@ corrade_add_resource(MagnumShaders_RCS MagnumShaders FlatShader2D.vert FlatShader2D.frag - PhongShader.frag PhongShader.vert) + PhongShader.frag PhongShader.vert + compatibility.glsl) set(MagnumShaders_SRCS FlatShader.cpp PhongShader.cpp diff --git a/src/Shaders/PhongShader.cpp b/src/Shaders/PhongShader.cpp index 8d79c69ba..394243dcb 100644 --- a/src/Shaders/PhongShader.cpp +++ b/src/Shaders/PhongShader.cpp @@ -24,11 +24,28 @@ namespace Magnum { namespace Shaders { PhongShader::PhongShader() { Corrade::Utility::Resource rs("MagnumShaders"); - Version v = Context::current()->isVersionSupported(Version::GL320) ? Version::GL320 : Version::GL210; - attachShader(Shader::fromData(v, Shader::Type::Vertex, rs.get("PhongShader.vert"))); - attachShader(Shader::fromData(v, Shader::Type::Fragment, rs.get("PhongShader.frag"))); + #ifndef MAGNUM_TARGET_GLES + Version v = Context::current()->supportedVersion({Version::GL320, Version::GL210}); + #else + Version v = Context::current()->supportedVersion({Version::GLES300, Version::GLES200}); + #endif + + Shader vertexShader(v, Shader::Type::Vertex); + vertexShader.addSource(rs.get("compatibility.glsl")); + vertexShader.addSource(rs.get("PhongShader.vert")); + attachShader(vertexShader); + + Shader fragmentShader(v, Shader::Type::Fragment); + fragmentShader.addSource(rs.get("compatibility.glsl")); + fragmentShader.addSource(rs.get("PhongShader.frag")); + attachShader(fragmentShader); + + #ifndef MAGNUM_TARGET_GLES if(!Context::current()->isExtensionSupported()) { + #else + if(!Context::current()->isVersionSupported(Version::GLES300)) { + #endif bindAttributeLocation(Position::Location, "position"); bindAttributeLocation(Normal::Location, "normal"); } diff --git a/src/Shaders/PhongShader.frag b/src/Shaders/PhongShader.frag index c6d9c9be5..638a1e3fa 100644 --- a/src/Shaders/PhongShader.frag +++ b/src/Shaders/PhongShader.frag @@ -1,4 +1,4 @@ -#if __VERSION__ == 120 +#ifndef NEW_GLSL #define in varying #define color gl_FragColor #endif @@ -13,7 +13,7 @@ in vec3 transformedNormal; in vec3 lightDirection; in vec3 cameraDirection; -#if __VERSION__ != 120 +#ifdef NEW_GLSL out vec4 color; #endif diff --git a/src/Shaders/PhongShader.vert b/src/Shaders/PhongShader.vert index 3872fcc74..1c794cbce 100644 --- a/src/Shaders/PhongShader.vert +++ b/src/Shaders/PhongShader.vert @@ -1,4 +1,4 @@ -#if __VERSION__ == 120 +#ifndef NEW_GLSL #define in attribute #define out varying #endif @@ -7,8 +7,7 @@ uniform mat4 transformationMatrix; uniform mat4 projectionMatrix; uniform vec3 light; -#if __VERSION__ != 120 && defined(GL_ARB_explicit_attrib_location) -#extension GL_ARB_explicit_attrib_location: enable +#ifdef EXPLICIT_ATTRIB_LOCATION layout(location = 0) in vec4 position; layout(location = 1) in vec3 normal; #else diff --git a/src/Shaders/compatibility.glsl b/src/Shaders/compatibility.glsl new file mode 100644 index 000000000..2ccbfae6b --- /dev/null +++ b/src/Shaders/compatibility.glsl @@ -0,0 +1,14 @@ +#if (!defined(GL_ES) && __VERSION__ >= 130) || (defined(GL_ES) && __VERSION__ >= 300) +#define NEW_GLSL +#endif + +/* 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 +#endif + +#if defined(GL_ES) && __VERSION__ >= 300 +#define EXPLICIT_ATTRIB_LOCATION +#endif