From 8ba2cac0eef024774cae2bac9cf213eaea5de2bb Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Vladim=C3=ADr=20Vondru=C5=A1?= Date: Sat, 2 Mar 2019 02:37:15 +0100 Subject: [PATCH] DebugTools, MeshTools, TextureTools: handle broken gl_VertexID. DebugTools::textureSubImage(), MeshTools::fullScreenTriangle() and TextureTools::distanceField() all plagued by this on SwiftShader. --- doc/changelog.dox | 12 +++++++++ src/Magnum/DebugTools/TextureImage.cpp | 26 ++++++++++++++++--- src/Magnum/DebugTools/TextureImage.vert | 8 ++++++ src/Magnum/MeshTools/FullScreenTriangle.cpp | 8 ++---- src/Magnum/Shaders/FullScreenTriangle.glsl | 7 +++-- .../CreateCompatibilityShader.h | 5 ++++ src/Magnum/TextureTools/DistanceField.cpp | 15 ++--------- 7 files changed, 56 insertions(+), 25 deletions(-) diff --git a/doc/changelog.dox b/doc/changelog.dox index 63f1b97b9..08a788105 100644 --- a/doc/changelog.dox +++ b/doc/changelog.dox @@ -101,6 +101,18 @@ See also: other variants are now usable directly through @ref CORRADE_COMPARE_AS(), no need to pass an explicit instance to @ref CORRADE_COMPARE_WITH() when just the default parameters are needed +- @ref DebugTools::textureSubImage() for float textures was updated to work + with SwiftShader (which has broken @glsl gl_VertexID @ce) + +@subsubsection changelog-latest-changes-meshtools MeshTools library + +- @ref MeshTools::fullScreenTriangle() was updated to work on ES3 SwiftShader + contexts (which have broken @glsl gl_VertexID @ce) + +@subsubsection changelog-latest-changes-texturetools TextureTools library + +- @ref TextureTools::distanceField() was updated to work on ES3 SwiftShader + contexts (which have broken @glsl gl_VertexID @ce) @subsubsection changelog-latest-changes-platform Platform libraries diff --git a/src/Magnum/DebugTools/TextureImage.cpp b/src/Magnum/DebugTools/TextureImage.cpp index 94d24b54e..cf7c72ff2 100644 --- a/src/Magnum/DebugTools/TextureImage.cpp +++ b/src/Magnum/DebugTools/TextureImage.cpp @@ -81,13 +81,19 @@ FloatReinterpretShader::FloatReinterpretShader() { GL::Shader vert{GL::Version::GLES300, GL::Shader::Type::Vertex}; GL::Shader frag{GL::Version::GLES300, GL::Shader::Type::Fragment}; + if(!GL::Context::current().isExtensionSupported()) + vert.addSource("#define DISABLE_GL_MAGNUM_shader_vertex_id\n"); vert.addSource(rs.get("TextureImage.vert")); frag.addSource(rs.get("TextureImage.frag")); - CORRADE_INTERNAL_ASSERT(GL::Shader::compile({vert, frag})); + CORRADE_INTERNAL_ASSERT_OUTPUT(GL::Shader::compile({vert, frag})); attachShaders({vert, frag}); - CORRADE_INTERNAL_ASSERT(link()); + if(!GL::Context::current().isExtensionSupported()) { + bindAttributeLocation(0, "position"); + } + + CORRADE_INTERNAL_ASSERT_OUTPUT(link()); levelUniform = uniformLocation("level"); setUniform(uniformLocation("textureData"), 0); @@ -145,8 +151,20 @@ void textureSubImage(GL::Texture2D& texture, const Int level, const Range2Di& ra shader.setTexture(texture, level); GL::Mesh mesh; - mesh.setCount(3) - .draw(shader); + mesh.setCount(3); + + if(!GL::Context::current().isExtensionSupported()) { + constexpr Vector2 triangle[]{ + {-1.0f, 1.0f}, + {-1.0f, -3.0f}, + { 3.0f, 1.0f} + }; + GL::Buffer buffer{GL::Buffer::TargetHint::Array}; + buffer.setData(triangle, GL::BufferUsage::StaticDraw); + mesh.addVertexBuffer(std::move(buffer), 0, GL::Attribute<0, Vector2>{}); + } + + mesh.draw(shader); /* release() needs to be called after querying the size to avoid zeroing it out */ const Vector2i imageSize = image.size(); diff --git a/src/Magnum/DebugTools/TextureImage.vert b/src/Magnum/DebugTools/TextureImage.vert index f33214f5a..4ddfb849e 100644 --- a/src/Magnum/DebugTools/TextureImage.vert +++ b/src/Magnum/DebugTools/TextureImage.vert @@ -23,7 +23,15 @@ DEALINGS IN THE SOFTWARE. */ +#ifdef DISABLE_GL_MAGNUM_shader_vertex_id +in lowp vec4 position; +#endif + void main() { + #ifndef DISABLE_GL_MAGNUM_shader_vertex_id gl_Position = vec4((gl_VertexID == 2) ? 3.0 : -1.0, (gl_VertexID == 1) ? -3.0 : 1.0, 0.0, 1.0); + #else + gl_Position = position; + #endif } diff --git a/src/Magnum/MeshTools/FullScreenTriangle.cpp b/src/Magnum/MeshTools/FullScreenTriangle.cpp index 1d025462f..7402fd67d 100644 --- a/src/Magnum/MeshTools/FullScreenTriangle.cpp +++ b/src/Magnum/MeshTools/FullScreenTriangle.cpp @@ -28,6 +28,7 @@ #include "Magnum/GL/Attribute.h" #include "Magnum/GL/Buffer.h" #include "Magnum/GL/Context.h" +#include "Magnum/GL/Extensions.h" #include "Magnum/GL/Mesh.h" #include "Magnum/GL/Version.h" #include "Magnum/Math/Vector2.h" @@ -39,12 +40,7 @@ GL::Mesh fullScreenTriangle(const GL::Version version) { mesh.setPrimitive(GL::MeshPrimitive::Triangles) .setCount(3); - #ifndef MAGNUM_TARGET_GLES - if(version < GL::Version::GL300) - #else - if(version < GL::Version::GLES300) - #endif - { + if(!GL::Context::current().isExtensionSupported(version)) { constexpr Vector2 triangle[]{ {-1.0f, 1.0f}, {-1.0f, -3.0f}, diff --git a/src/Magnum/Shaders/FullScreenTriangle.glsl b/src/Magnum/Shaders/FullScreenTriangle.glsl index e57d10bb8..e1ffa2db3 100644 --- a/src/Magnum/Shaders/FullScreenTriangle.glsl +++ b/src/Magnum/Shaders/FullScreenTriangle.glsl @@ -23,12 +23,15 @@ DEALINGS IN THE SOFTWARE. */ +#if !defined(NEW_GLSL) || defined(DISABLE_GL_MAGNUM_shader_vertex_id) #ifndef NEW_GLSL -attribute lowp vec4 position; +#define in attribute +#endif +in lowp vec4 position; #endif void fullScreenTriangle() { - #ifdef NEW_GLSL + #if defined(NEW_GLSL) && !defined(DISABLE_GL_MAGNUM_shader_vertex_id) gl_Position = vec4((gl_VertexID == 2) ? 3.0 : -1.0, (gl_VertexID == 1) ? -3.0 : 1.0, 0.0, 1.0); #else diff --git a/src/Magnum/Shaders/Implementation/CreateCompatibilityShader.h b/src/Magnum/Shaders/Implementation/CreateCompatibilityShader.h index ed65c877d..7458ecaae 100644 --- a/src/Magnum/Shaders/Implementation/CreateCompatibilityShader.h +++ b/src/Magnum/Shaders/Implementation/CreateCompatibilityShader.h @@ -53,6 +53,11 @@ inline GL::Shader createCompatibilityShader(const Utility::Resource& rs, GL::Ver shader.addSource("#define DISABLE_GL_ARB_explicit_uniform_location\n"); #endif + #ifndef MAGNUM_TARGET_GLES2 + if(type == GL::Shader::Type::Vertex && GL::Context::current().isExtensionDisabled(version)) + shader.addSource("#define DISABLE_GL_MAGNUM_shader_vertex_id\n"); + #endif + /* My Android emulator (running on NVidia) doesn't define GL_ES preprocessor macro, thus *all* the stock shaders fail to compile */ /** @todo remove this when Android emulator is sane */ diff --git a/src/Magnum/TextureTools/DistanceField.cpp b/src/Magnum/TextureTools/DistanceField.cpp index 83a4f63d8..1d7457553 100644 --- a/src/Magnum/TextureTools/DistanceField.cpp +++ b/src/Magnum/TextureTools/DistanceField.cpp @@ -106,13 +106,7 @@ DistanceFieldShader::DistanceFieldShader(const UnsignedInt radius) { attachShaders({vert, frag}); - /* Older GLSL doesn't have gl_VertexID, vertices must be supplied explicitly */ - #ifndef MAGNUM_TARGET_GLES - if(!GL::Context::current().isVersionSupported(GL::Version::GL300)) - #else - if(!GL::Context::current().isVersionSupported(GL::Version::GLES300)) - #endif - { + if(!GL::Context::current().isExtensionSupported()) { bindAttributeLocation(Position::Location, "position"); } @@ -160,12 +154,7 @@ DistanceField::DistanceField(const UnsignedInt radius): _state{new State{radius} _state->mesh.setPrimitive(GL::MeshPrimitive::Triangles) .setCount(3); - /* Older GLSL doesn't have gl_VertexID, vertices must be supplied explicitly */ - #ifndef MAGNUM_TARGET_GLES - if(!GL::Context::current().isVersionSupported(GL::Version::GL300)) - #else - if(!GL::Context::current().isVersionSupported(GL::Version::GLES300)) - #endif + if(!GL::Context::current().isExtensionSupported()) { constexpr Vector2 triangle[] = { Vector2(-1.0, 1.0),