Browse Source

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.
pull/51/head
Vladimír Vondruš 12 years ago
parent
commit
0f8d22235d
  1. 3
      src/Magnum/Shaders/CMakeLists.txt
  2. 12
      src/Magnum/Shaders/DistanceFieldVector.cpp
  3. 8
      src/Magnum/Shaders/Flat.cpp
  4. 13
      src/Magnum/Shaders/MeshVisualizer.cpp
  5. 8
      src/Magnum/Shaders/Phong.cpp
  6. 12
      src/Magnum/Shaders/Vector.cpp
  7. 12
      src/Magnum/Shaders/VertexColor.cpp
  8. 26
      src/Magnum/Shaders/compatibility.glsl

3
src/Magnum/Shaders/CMakeLists.txt

@ -33,6 +33,9 @@ set(MagnumShaders_SRCS
Phong.cpp Phong.cpp
Vector.cpp Vector.cpp
VertexColor.cpp VertexColor.cpp
Implementation/CreateCompatibilityShader.cpp
${MagnumShaders_RCS}) ${MagnumShaders_RCS})
set(MagnumShaders_HEADERS set(MagnumShaders_HEADERS

12
src/Magnum/Shaders/DistanceFieldVector.cpp

@ -31,6 +31,8 @@
#include "Magnum/Extensions.h" #include "Magnum/Extensions.h"
#include "Magnum/Shader.h" #include "Magnum/Shader.h"
#include "Implementation/CreateCompatibilityShader.h"
namespace Magnum { namespace Shaders { namespace Magnum { namespace Shaders {
namespace { namespace {
@ -48,16 +50,14 @@ template<UnsignedInt dimensions> DistanceFieldVector<dimensions>::DistanceFieldV
const Version version = Context::current()->supportedVersion({Version::GLES300, Version::GLES200}); const Version version = Context::current()->supportedVersion({Version::GLES300, Version::GLES200});
#endif #endif
Shader frag(version, Shader::Type::Vertex); Shader frag = Implementation::createCompatibilityShader(version, Shader::Type::Vertex);
frag.addSource(rs.get("compatibility.glsl")) frag.addSource(rs.get("generic.glsl"))
.addSource(rs.get("generic.glsl"))
.addSource(rs.get(vertexShaderName<dimensions>())); .addSource(rs.get(vertexShaderName<dimensions>()));
CORRADE_INTERNAL_ASSERT_OUTPUT(frag.compile()); CORRADE_INTERNAL_ASSERT_OUTPUT(frag.compile());
AbstractShaderProgram::attachShader(frag); AbstractShaderProgram::attachShader(frag);
Shader vert(version, Shader::Type::Fragment); Shader vert = Implementation::createCompatibilityShader(version, Shader::Type::Fragment);
vert.addSource(rs.get("compatibility.glsl")) vert.addSource(rs.get("DistanceFieldVector.frag"));
.addSource(rs.get("DistanceFieldVector.frag"));
CORRADE_INTERNAL_ASSERT_OUTPUT(vert.compile()); CORRADE_INTERNAL_ASSERT_OUTPUT(vert.compile());
AbstractShaderProgram::attachShader(vert); AbstractShaderProgram::attachShader(vert);

8
src/Magnum/Shaders/Flat.cpp

@ -32,6 +32,8 @@
#include "Magnum/Shader.h" #include "Magnum/Shader.h"
#include "Magnum/Texture.h" #include "Magnum/Texture.h"
#include "Implementation/CreateCompatibilityShader.h"
namespace Magnum { namespace Shaders { namespace Magnum { namespace Shaders {
namespace { namespace {
@ -51,17 +53,15 @@ template<UnsignedInt dimensions> Flat<dimensions>::Flat(const Flags flags): tran
const Version version = Context::current()->supportedVersion({Version::GLES300, Version::GLES200}); const Version version = Context::current()->supportedVersion({Version::GLES300, Version::GLES200});
#endif #endif
Shader vert(version, Shader::Type::Vertex); Shader vert = Implementation::createCompatibilityShader(version, Shader::Type::Vertex);
vert.addSource(flags & Flag::Textured ? "#define TEXTURED\n" : "") vert.addSource(flags & Flag::Textured ? "#define TEXTURED\n" : "")
.addSource(rs.get("compatibility.glsl"))
.addSource(rs.get("generic.glsl")) .addSource(rs.get("generic.glsl"))
.addSource(rs.get(vertexShaderName<dimensions>())); .addSource(rs.get(vertexShaderName<dimensions>()));
CORRADE_INTERNAL_ASSERT_OUTPUT(vert.compile()); CORRADE_INTERNAL_ASSERT_OUTPUT(vert.compile());
attachShader(vert); attachShader(vert);
Shader frag(version, Shader::Type::Fragment); Shader frag = Implementation::createCompatibilityShader(version, Shader::Type::Fragment);
frag.addSource(flags & Flag::Textured ? "#define TEXTURED\n" : "") frag.addSource(flags & Flag::Textured ? "#define TEXTURED\n" : "")
.addSource(rs.get("compatibility.glsl"))
.addSource(rs.get("Flat.frag")); .addSource(rs.get("Flat.frag"));
CORRADE_INTERNAL_ASSERT_OUTPUT(frag.compile()); CORRADE_INTERNAL_ASSERT_OUTPUT(frag.compile());
attachShader(frag); attachShader(frag);

13
src/Magnum/Shaders/MeshVisualizer.cpp

@ -31,6 +31,8 @@
#include "Magnum/Extensions.h" #include "Magnum/Extensions.h"
#include "Magnum/Shader.h" #include "Magnum/Shader.h"
#include "Implementation/CreateCompatibilityShader.h"
namespace Magnum { namespace Shaders { namespace Magnum { namespace Shaders {
MeshVisualizer::MeshVisualizer(const Flags flags): flags(flags), transformationProjectionMatrixUniform(0), viewportSizeUniform(1), colorUniform(2), wireframeColorUniform(3), wireframeWidthUniform(4), smoothnessUniform(5) { 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}); const Version version = Context::current()->supportedVersion({Version::GLES300, Version::GLES200});
#endif #endif
Shader vert(version, Shader::Type::Vertex); Shader vert = Implementation::createCompatibilityShader(version, Shader::Type::Vertex);
vert.addSource(flags & Flag::Wireframe ? "#define WIREFRAME_RENDERING\n" : "") vert.addSource(flags & Flag::Wireframe ? "#define WIREFRAME_RENDERING\n" : "")
.addSource(flags & Flag::NoGeometryShader ? "#define NO_GEOMETRY_SHADER\n" : "") .addSource(flags & Flag::NoGeometryShader ? "#define NO_GEOMETRY_SHADER\n" : "")
.addSource(rs.get("compatibility.glsl"))
.addSource(rs.get("generic.glsl")) .addSource(rs.get("generic.glsl"))
.addSource(rs.get("MeshVisualizer.vert")); .addSource(rs.get("MeshVisualizer.vert"));
CORRADE_INTERNAL_ASSERT_OUTPUT(vert.compile()); CORRADE_INTERNAL_ASSERT_OUTPUT(vert.compile());
@ -65,19 +66,17 @@ MeshVisualizer::MeshVisualizer(const Flags flags): flags(flags), transformationP
#ifndef MAGNUM_TARGET_GLES #ifndef MAGNUM_TARGET_GLES
if(flags & Flag::Wireframe && !(flags & Flag::NoGeometryShader)) { if(flags & Flag::Wireframe && !(flags & Flag::NoGeometryShader)) {
Shader geom(version, Shader::Type::Geometry); Shader geom = Implementation::createCompatibilityShader(version, Shader::Type::Geometry);
geom.addSource(rs.get("compatibility.glsl")) geom.addSource(rs.get("MeshVisualizer.geom"));
.addSource(rs.get("MeshVisualizer.geom"));
CORRADE_INTERNAL_ASSERT_OUTPUT(geom.compile()); CORRADE_INTERNAL_ASSERT_OUTPUT(geom.compile());
geom.compile(); geom.compile();
attachShader(geom); attachShader(geom);
} }
#endif #endif
Shader frag(version, Shader::Type::Fragment); Shader frag = Implementation::createCompatibilityShader(version, Shader::Type::Fragment);
frag.addSource(flags & Flag::Wireframe ? "#define WIREFRAME_RENDERING\n" : "") frag.addSource(flags & Flag::Wireframe ? "#define WIREFRAME_RENDERING\n" : "")
.addSource(flags & Flag::NoGeometryShader ? "#define NO_GEOMETRY_SHADER\n" : "") .addSource(flags & Flag::NoGeometryShader ? "#define NO_GEOMETRY_SHADER\n" : "")
.addSource(rs.get("compatibility.glsl"))
.addSource(rs.get("MeshVisualizer.frag")); .addSource(rs.get("MeshVisualizer.frag"));
CORRADE_INTERNAL_ASSERT_OUTPUT(frag.compile()); CORRADE_INTERNAL_ASSERT_OUTPUT(frag.compile());
frag.compile(); frag.compile();

8
src/Magnum/Shaders/Phong.cpp

@ -32,6 +32,8 @@
#include "Magnum/Shader.h" #include "Magnum/Shader.h"
#include "Magnum/Texture.h" #include "Magnum/Texture.h"
#include "Implementation/CreateCompatibilityShader.h"
namespace Magnum { namespace Shaders { namespace Magnum { namespace Shaders {
namespace { namespace {
@ -51,19 +53,17 @@ Phong::Phong(const Flags flags): transformationMatrixUniform(0), projectionMatri
const Version version = Context::current()->supportedVersion({Version::GLES300, Version::GLES200}); const Version version = Context::current()->supportedVersion({Version::GLES300, Version::GLES200});
#endif #endif
Shader vert(version, Shader::Type::Vertex); Shader vert = Implementation::createCompatibilityShader(version, Shader::Type::Vertex);
vert.addSource(flags ? "#define TEXTURED\n" : "") vert.addSource(flags ? "#define TEXTURED\n" : "")
.addSource(rs.get("compatibility.glsl"))
.addSource(rs.get("generic.glsl")) .addSource(rs.get("generic.glsl"))
.addSource(rs.get("Phong.vert")); .addSource(rs.get("Phong.vert"));
CORRADE_INTERNAL_ASSERT_OUTPUT(vert.compile()); CORRADE_INTERNAL_ASSERT_OUTPUT(vert.compile());
attachShader(vert); 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" : "") frag.addSource(flags & Flag::AmbientTexture ? "#define AMBIENT_TEXTURE\n" : "")
.addSource(flags & Flag::DiffuseTexture ? "#define DIFFUSE_TEXTURE\n" : "") .addSource(flags & Flag::DiffuseTexture ? "#define DIFFUSE_TEXTURE\n" : "")
.addSource(flags & Flag::SpecularTexture ? "#define SPECULAR_TEXTURE\n" : "") .addSource(flags & Flag::SpecularTexture ? "#define SPECULAR_TEXTURE\n" : "")
.addSource(rs.get("compatibility.glsl"))
.addSource(rs.get("Phong.frag")); .addSource(rs.get("Phong.frag"));
CORRADE_INTERNAL_ASSERT_OUTPUT(frag.compile()); CORRADE_INTERNAL_ASSERT_OUTPUT(frag.compile());
attachShader(frag); attachShader(frag);

12
src/Magnum/Shaders/Vector.cpp

@ -31,6 +31,8 @@
#include "Magnum/Extensions.h" #include "Magnum/Extensions.h"
#include "Magnum/Shader.h" #include "Magnum/Shader.h"
#include "Implementation/CreateCompatibilityShader.h"
namespace Magnum { namespace Shaders { namespace Magnum { namespace Shaders {
namespace { namespace {
@ -48,16 +50,14 @@ template<UnsignedInt dimensions> Vector<dimensions>::Vector(): transformationPro
const Version version = Context::current()->supportedVersion({Version::GLES300, Version::GLES200}); const Version version = Context::current()->supportedVersion({Version::GLES300, Version::GLES200});
#endif #endif
Shader vert(version, Shader::Type::Vertex); Shader vert = Implementation::createCompatibilityShader(version, Shader::Type::Vertex);
vert.addSource(rs.get("compatibility.glsl")) vert.addSource(rs.get("generic.glsl"))
.addSource(rs.get("generic.glsl"))
.addSource(rs.get(vertexShaderName<dimensions>())); .addSource(rs.get(vertexShaderName<dimensions>()));
CORRADE_INTERNAL_ASSERT_OUTPUT(vert.compile()); CORRADE_INTERNAL_ASSERT_OUTPUT(vert.compile());
AbstractShaderProgram::attachShader(vert); AbstractShaderProgram::attachShader(vert);
Shader frag(version, Shader::Type::Fragment); Shader frag = Implementation::createCompatibilityShader(version, Shader::Type::Fragment);
frag.addSource(rs.get("compatibility.glsl")) frag.addSource(rs.get("Vector.frag"));
.addSource(rs.get("Vector.frag"));
CORRADE_INTERNAL_ASSERT_OUTPUT(frag.compile()); CORRADE_INTERNAL_ASSERT_OUTPUT(frag.compile());
AbstractShaderProgram::attachShader(frag); AbstractShaderProgram::attachShader(frag);

12
src/Magnum/Shaders/VertexColor.cpp

@ -31,6 +31,8 @@
#include "Magnum/Extensions.h" #include "Magnum/Extensions.h"
#include "Magnum/Shader.h" #include "Magnum/Shader.h"
#include "Implementation/CreateCompatibilityShader.h"
namespace Magnum { namespace Shaders { namespace Magnum { namespace Shaders {
namespace { namespace {
@ -48,16 +50,14 @@ template<UnsignedInt dimensions> VertexColor<dimensions>::VertexColor(): transfo
const Version version = Context::current()->supportedVersion({Version::GLES300, Version::GLES200}); const Version version = Context::current()->supportedVersion({Version::GLES300, Version::GLES200});
#endif #endif
Shader vert(version, Shader::Type::Vertex); Shader vert = Implementation::createCompatibilityShader(version, Shader::Type::Vertex);
vert.addSource(rs.get("compatibility.glsl")) vert.addSource(rs.get("generic.glsl"))
.addSource(rs.get("generic.glsl"))
.addSource(rs.get(vertexShaderName<dimensions>())); .addSource(rs.get(vertexShaderName<dimensions>()));
CORRADE_INTERNAL_ASSERT_OUTPUT(vert.compile()); CORRADE_INTERNAL_ASSERT_OUTPUT(vert.compile());
attachShader(vert); attachShader(vert);
Shader frag(version, Shader::Type::Fragment); Shader frag = Implementation::createCompatibilityShader(version, Shader::Type::Fragment);
frag.addSource(rs.get("compatibility.glsl")) frag.addSource(rs.get("VertexColor.frag"));
.addSource(rs.get("VertexColor.frag"));
CORRADE_INTERNAL_ASSERT_OUTPUT(frag.compile()); CORRADE_INTERNAL_ASSERT_OUTPUT(frag.compile());
attachShader(frag); attachShader(frag);

26
src/Magnum/Shaders/compatibility.glsl

@ -27,26 +27,20 @@
#define NEW_GLSL #define NEW_GLSL
#endif #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 #extension GL_ARB_shading_language_420pack: enable
#define RUNTIME_CONST #define RUNTIME_CONST
#define EXPLICIT_TEXTURE_LAYER
#endif #endif
/* On NVidia and GLSL 1.20 layout qualifiers result in parsing error, even if #if !defined(GL_ES) && defined(GL_ARB_explicit_uniform_location) && !defined(DISABLE_GL_ARB_explicit_uniform_location)
the extension is defined as supported */ #extension GL_ARB_explicit_uniform_location: enable
#if !defined(GL_ES) && __VERSION__ >= 140 #define EXPLICIT_UNIFORM_LOCATION
#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
#endif #endif
#if defined(GL_ES) && __VERSION__ >= 300 #if defined(GL_ES) && __VERSION__ >= 300

Loading…
Cancel
Save