From 0f8d22235d59efdbfdea4981c52c7be8a0a74e9d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Vladim=C3=ADr=20Vondru=C5=A1?= Date: Wed, 19 Feb 2014 01:49:09 +0100 Subject: [PATCH] Shader: more robust extension presence checking. Propagate the information about whether given extension is disabled to GLSL instead of doing the guesswork again from scratch. --- src/Magnum/Shaders/CMakeLists.txt | 3 +++ src/Magnum/Shaders/DistanceFieldVector.cpp | 12 +++++----- src/Magnum/Shaders/Flat.cpp | 8 +++---- src/Magnum/Shaders/MeshVisualizer.cpp | 13 +++++------ src/Magnum/Shaders/Phong.cpp | 8 +++---- src/Magnum/Shaders/Vector.cpp | 12 +++++----- src/Magnum/Shaders/VertexColor.cpp | 12 +++++----- src/Magnum/Shaders/compatibility.glsl | 26 +++++++++------------- 8 files changed, 45 insertions(+), 49 deletions(-) diff --git a/src/Magnum/Shaders/CMakeLists.txt b/src/Magnum/Shaders/CMakeLists.txt index 0405f7529..8b2e16a16 100644 --- a/src/Magnum/Shaders/CMakeLists.txt +++ b/src/Magnum/Shaders/CMakeLists.txt @@ -33,6 +33,9 @@ set(MagnumShaders_SRCS Phong.cpp Vector.cpp VertexColor.cpp + + Implementation/CreateCompatibilityShader.cpp + ${MagnumShaders_RCS}) set(MagnumShaders_HEADERS diff --git a/src/Magnum/Shaders/DistanceFieldVector.cpp b/src/Magnum/Shaders/DistanceFieldVector.cpp index adf25521e..2b81b9d03 100644 --- a/src/Magnum/Shaders/DistanceFieldVector.cpp +++ b/src/Magnum/Shaders/DistanceFieldVector.cpp @@ -31,6 +31,8 @@ #include "Magnum/Extensions.h" #include "Magnum/Shader.h" +#include "Implementation/CreateCompatibilityShader.h" + namespace Magnum { namespace Shaders { namespace { @@ -48,16 +50,14 @@ template DistanceFieldVector::DistanceFieldV const Version version = Context::current()->supportedVersion({Version::GLES300, Version::GLES200}); #endif - Shader frag(version, Shader::Type::Vertex); - frag.addSource(rs.get("compatibility.glsl")) - .addSource(rs.get("generic.glsl")) + Shader frag = Implementation::createCompatibilityShader(version, Shader::Type::Vertex); + frag.addSource(rs.get("generic.glsl")) .addSource(rs.get(vertexShaderName())); CORRADE_INTERNAL_ASSERT_OUTPUT(frag.compile()); AbstractShaderProgram::attachShader(frag); - Shader vert(version, Shader::Type::Fragment); - vert.addSource(rs.get("compatibility.glsl")) - .addSource(rs.get("DistanceFieldVector.frag")); + Shader vert = Implementation::createCompatibilityShader(version, Shader::Type::Fragment); + vert.addSource(rs.get("DistanceFieldVector.frag")); CORRADE_INTERNAL_ASSERT_OUTPUT(vert.compile()); AbstractShaderProgram::attachShader(vert); diff --git a/src/Magnum/Shaders/Flat.cpp b/src/Magnum/Shaders/Flat.cpp index b70085413..49f5cd830 100644 --- a/src/Magnum/Shaders/Flat.cpp +++ b/src/Magnum/Shaders/Flat.cpp @@ -32,6 +32,8 @@ #include "Magnum/Shader.h" #include "Magnum/Texture.h" +#include "Implementation/CreateCompatibilityShader.h" + namespace Magnum { namespace Shaders { namespace { @@ -51,17 +53,15 @@ template Flat::Flat(const Flags flags): tran const Version version = Context::current()->supportedVersion({Version::GLES300, Version::GLES200}); #endif - Shader vert(version, Shader::Type::Vertex); + Shader vert = Implementation::createCompatibilityShader(version, Shader::Type::Vertex); vert.addSource(flags & Flag::Textured ? "#define TEXTURED\n" : "") - .addSource(rs.get("compatibility.glsl")) .addSource(rs.get("generic.glsl")) .addSource(rs.get(vertexShaderName())); CORRADE_INTERNAL_ASSERT_OUTPUT(vert.compile()); attachShader(vert); - Shader frag(version, Shader::Type::Fragment); + Shader frag = Implementation::createCompatibilityShader(version, Shader::Type::Fragment); frag.addSource(flags & Flag::Textured ? "#define TEXTURED\n" : "") - .addSource(rs.get("compatibility.glsl")) .addSource(rs.get("Flat.frag")); CORRADE_INTERNAL_ASSERT_OUTPUT(frag.compile()); attachShader(frag); diff --git a/src/Magnum/Shaders/MeshVisualizer.cpp b/src/Magnum/Shaders/MeshVisualizer.cpp index 2c2d6ab14..660929260 100644 --- a/src/Magnum/Shaders/MeshVisualizer.cpp +++ b/src/Magnum/Shaders/MeshVisualizer.cpp @@ -31,6 +31,8 @@ #include "Magnum/Extensions.h" #include "Magnum/Shader.h" +#include "Implementation/CreateCompatibilityShader.h" + namespace Magnum { namespace Shaders { MeshVisualizer::MeshVisualizer(const Flags flags): flags(flags), transformationProjectionMatrixUniform(0), viewportSizeUniform(1), colorUniform(2), wireframeColorUniform(3), wireframeWidthUniform(4), smoothnessUniform(5) { @@ -53,10 +55,9 @@ MeshVisualizer::MeshVisualizer(const Flags flags): flags(flags), transformationP const Version version = Context::current()->supportedVersion({Version::GLES300, Version::GLES200}); #endif - Shader vert(version, Shader::Type::Vertex); + Shader vert = Implementation::createCompatibilityShader(version, Shader::Type::Vertex); vert.addSource(flags & Flag::Wireframe ? "#define WIREFRAME_RENDERING\n" : "") .addSource(flags & Flag::NoGeometryShader ? "#define NO_GEOMETRY_SHADER\n" : "") - .addSource(rs.get("compatibility.glsl")) .addSource(rs.get("generic.glsl")) .addSource(rs.get("MeshVisualizer.vert")); CORRADE_INTERNAL_ASSERT_OUTPUT(vert.compile()); @@ -65,19 +66,17 @@ MeshVisualizer::MeshVisualizer(const Flags flags): flags(flags), transformationP #ifndef MAGNUM_TARGET_GLES if(flags & Flag::Wireframe && !(flags & Flag::NoGeometryShader)) { - Shader geom(version, Shader::Type::Geometry); - geom.addSource(rs.get("compatibility.glsl")) - .addSource(rs.get("MeshVisualizer.geom")); + Shader geom = Implementation::createCompatibilityShader(version, Shader::Type::Geometry); + geom.addSource(rs.get("MeshVisualizer.geom")); CORRADE_INTERNAL_ASSERT_OUTPUT(geom.compile()); geom.compile(); attachShader(geom); } #endif - Shader frag(version, Shader::Type::Fragment); + Shader frag = Implementation::createCompatibilityShader(version, Shader::Type::Fragment); frag.addSource(flags & Flag::Wireframe ? "#define WIREFRAME_RENDERING\n" : "") .addSource(flags & Flag::NoGeometryShader ? "#define NO_GEOMETRY_SHADER\n" : "") - .addSource(rs.get("compatibility.glsl")) .addSource(rs.get("MeshVisualizer.frag")); CORRADE_INTERNAL_ASSERT_OUTPUT(frag.compile()); frag.compile(); diff --git a/src/Magnum/Shaders/Phong.cpp b/src/Magnum/Shaders/Phong.cpp index bb88002c3..48f34597c 100644 --- a/src/Magnum/Shaders/Phong.cpp +++ b/src/Magnum/Shaders/Phong.cpp @@ -32,6 +32,8 @@ #include "Magnum/Shader.h" #include "Magnum/Texture.h" +#include "Implementation/CreateCompatibilityShader.h" + namespace Magnum { namespace Shaders { namespace { @@ -51,19 +53,17 @@ Phong::Phong(const Flags flags): transformationMatrixUniform(0), projectionMatri const Version version = Context::current()->supportedVersion({Version::GLES300, Version::GLES200}); #endif - Shader vert(version, Shader::Type::Vertex); + Shader vert = Implementation::createCompatibilityShader(version, Shader::Type::Vertex); vert.addSource(flags ? "#define TEXTURED\n" : "") - .addSource(rs.get("compatibility.glsl")) .addSource(rs.get("generic.glsl")) .addSource(rs.get("Phong.vert")); CORRADE_INTERNAL_ASSERT_OUTPUT(vert.compile()); attachShader(vert); - Shader frag(version, Shader::Type::Fragment); + Shader frag = Implementation::createCompatibilityShader(version, Shader::Type::Fragment); frag.addSource(flags & Flag::AmbientTexture ? "#define AMBIENT_TEXTURE\n" : "") .addSource(flags & Flag::DiffuseTexture ? "#define DIFFUSE_TEXTURE\n" : "") .addSource(flags & Flag::SpecularTexture ? "#define SPECULAR_TEXTURE\n" : "") - .addSource(rs.get("compatibility.glsl")) .addSource(rs.get("Phong.frag")); CORRADE_INTERNAL_ASSERT_OUTPUT(frag.compile()); attachShader(frag); diff --git a/src/Magnum/Shaders/Vector.cpp b/src/Magnum/Shaders/Vector.cpp index 86ce2aed2..a4337f26f 100644 --- a/src/Magnum/Shaders/Vector.cpp +++ b/src/Magnum/Shaders/Vector.cpp @@ -31,6 +31,8 @@ #include "Magnum/Extensions.h" #include "Magnum/Shader.h" +#include "Implementation/CreateCompatibilityShader.h" + namespace Magnum { namespace Shaders { namespace { @@ -48,16 +50,14 @@ template Vector::Vector(): transformationPro const Version version = Context::current()->supportedVersion({Version::GLES300, Version::GLES200}); #endif - Shader vert(version, Shader::Type::Vertex); - vert.addSource(rs.get("compatibility.glsl")) - .addSource(rs.get("generic.glsl")) + Shader vert = Implementation::createCompatibilityShader(version, Shader::Type::Vertex); + vert.addSource(rs.get("generic.glsl")) .addSource(rs.get(vertexShaderName())); CORRADE_INTERNAL_ASSERT_OUTPUT(vert.compile()); AbstractShaderProgram::attachShader(vert); - Shader frag(version, Shader::Type::Fragment); - frag.addSource(rs.get("compatibility.glsl")) - .addSource(rs.get("Vector.frag")); + Shader frag = Implementation::createCompatibilityShader(version, Shader::Type::Fragment); + frag.addSource(rs.get("Vector.frag")); CORRADE_INTERNAL_ASSERT_OUTPUT(frag.compile()); AbstractShaderProgram::attachShader(frag); diff --git a/src/Magnum/Shaders/VertexColor.cpp b/src/Magnum/Shaders/VertexColor.cpp index 52ece4678..ca08de6ce 100644 --- a/src/Magnum/Shaders/VertexColor.cpp +++ b/src/Magnum/Shaders/VertexColor.cpp @@ -31,6 +31,8 @@ #include "Magnum/Extensions.h" #include "Magnum/Shader.h" +#include "Implementation/CreateCompatibilityShader.h" + namespace Magnum { namespace Shaders { namespace { @@ -48,16 +50,14 @@ template VertexColor::VertexColor(): transfo const Version version = Context::current()->supportedVersion({Version::GLES300, Version::GLES200}); #endif - Shader vert(version, Shader::Type::Vertex); - vert.addSource(rs.get("compatibility.glsl")) - .addSource(rs.get("generic.glsl")) + Shader vert = Implementation::createCompatibilityShader(version, Shader::Type::Vertex); + vert.addSource(rs.get("generic.glsl")) .addSource(rs.get(vertexShaderName())); CORRADE_INTERNAL_ASSERT_OUTPUT(vert.compile()); attachShader(vert); - Shader frag(version, Shader::Type::Fragment); - frag.addSource(rs.get("compatibility.glsl")) - .addSource(rs.get("VertexColor.frag")); + Shader frag = Implementation::createCompatibilityShader(version, Shader::Type::Fragment); + frag.addSource(rs.get("VertexColor.frag")); CORRADE_INTERNAL_ASSERT_OUTPUT(frag.compile()); attachShader(frag); diff --git a/src/Magnum/Shaders/compatibility.glsl b/src/Magnum/Shaders/compatibility.glsl index 94eaf9440..e041903d6 100644 --- a/src/Magnum/Shaders/compatibility.glsl +++ b/src/Magnum/Shaders/compatibility.glsl @@ -27,26 +27,20 @@ #define NEW_GLSL #endif -#ifdef GL_ARB_shading_language_420pack +#if !defined(GL_ES) && defined(GL_ARB_explicit_attrib_location) && !defined(DISABLE_GL_ARB_explicit_attrib_location) + #extension GL_ARB_explicit_attrib_location: enable + #define EXPLICIT_ATTRIB_LOCATION +#endif + +#if !defined(GL_ES) && defined(GL_ARB_shading_language_420pack) && !defined(DISABLE_GL_ARB_shading_language_420pack) #extension GL_ARB_shading_language_420pack: enable #define RUNTIME_CONST + #define EXPLICIT_TEXTURE_LAYER #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__ >= 140 - #ifdef GL_ARB_explicit_attrib_location - #extension GL_ARB_explicit_attrib_location: enable - #define EXPLICIT_ATTRIB_LOCATION - #endif - #ifdef GL_ARB_shading_language_420pack - /* Already enabled */ - #define EXPLICIT_TEXTURE_LAYER - #endif - #ifdef GL_ARB_explicit_uniform_location - #extension GL_ARB_explicit_uniform_location: enable - #define EXPLICIT_UNIFORM_LOCATION - #endif +#if !defined(GL_ES) && defined(GL_ARB_explicit_uniform_location) && !defined(DISABLE_GL_ARB_explicit_uniform_location) + #extension GL_ARB_explicit_uniform_location: enable + #define EXPLICIT_UNIFORM_LOCATION #endif #if defined(GL_ES) && __VERSION__ >= 300