From 837defa18290b85c02225194814005bfedda1dae Mon Sep 17 00:00:00 2001 From: Florian Goujeon Date: Sat, 28 Sep 2019 12:42:45 +0200 Subject: [PATCH] Fix DistanceFieldVector on WebGL iOS --- src/Magnum/Shaders/AbstractVector.h | 6 +++++- src/Magnum/Shaders/DistanceFieldVector.frag | 10 +++++++--- src/Magnum/Shaders/Vector.frag | 3 ++- src/Magnum/TextureTools/DistanceField.cpp | 4 +++- 4 files changed, 17 insertions(+), 6 deletions(-) diff --git a/src/Magnum/Shaders/AbstractVector.h b/src/Magnum/Shaders/AbstractVector.h index 19fb5d588..55f586cc2 100644 --- a/src/Magnum/Shaders/AbstractVector.h +++ b/src/Magnum/Shaders/AbstractVector.h @@ -100,7 +100,11 @@ template class AbstractVector: public GL::AbstractShader #else private: #endif - enum: Int { VectorTextureLayer = 15 }; + /* Those textures are quite specific (and likely reused multiple times + per frame for e.g. text rendering, so put them in a specific slot. + Older iOS (and iOS WebGL) has only 8 texture binding slots, so can't + go above that. Binding 7 is used by TextureTools::DistanceField. */ + enum: Int { VectorTextureLayer = 6 }; explicit AbstractVector(NoCreateT) noexcept: GL::AbstractShaderProgram{NoCreate} {} explicit AbstractVector() = default; diff --git a/src/Magnum/Shaders/DistanceFieldVector.frag b/src/Magnum/Shaders/DistanceFieldVector.frag index c00bc6fa3..9a68ebb39 100644 --- a/src/Magnum/Shaders/DistanceFieldVector.frag +++ b/src/Magnum/Shaders/DistanceFieldVector.frag @@ -62,7 +62,8 @@ uniform lowp float smoothness ; #ifdef EXPLICIT_TEXTURE_LAYER -layout(binding = 15) +/* See AbstractVector.h for details about the ID */ +layout(binding = 6) #endif uniform lowp sampler2D vectorTexture; @@ -83,8 +84,11 @@ void main() { /* Outline */ if(outlineRange.x > outlineRange.y) { - lowp float mid = (outlineRange.x + outlineRange.y)/2.0; - lowp float halfRange = (outlineRange.x - outlineRange.y)/2.0; + /* Doing *0.5 instead of /2.0 because the latter causes iOS / WebGL to + complain that "Overflow in implicit constant conversion, minimum + range for lowp float is (-2,2)" */ + lowp float mid = (outlineRange.x + outlineRange.y)*0.5; + lowp float halfRange = (outlineRange.x - outlineRange.y)*0.5; fragmentColor += smoothstep(halfRange+smoothness, halfRange-smoothness, distance(mid, intensity))*outlineColor; } } diff --git a/src/Magnum/Shaders/Vector.frag b/src/Magnum/Shaders/Vector.frag index 63ebb022b..e3253cd27 100644 --- a/src/Magnum/Shaders/Vector.frag +++ b/src/Magnum/Shaders/Vector.frag @@ -44,7 +44,8 @@ uniform lowp vec4 color ; #ifdef EXPLICIT_TEXTURE_LAYER -layout(binding = 15) +/* See AbstractVector.h for details about the ID */ +layout(binding = 6) #endif uniform lowp sampler2D vectorTexture; diff --git a/src/Magnum/TextureTools/DistanceField.cpp b/src/Magnum/TextureTools/DistanceField.cpp index 432a88184..415f34c73 100644 --- a/src/Magnum/TextureTools/DistanceField.cpp +++ b/src/Magnum/TextureTools/DistanceField.cpp @@ -73,7 +73,9 @@ class DistanceFieldShader: public GL::AbstractShaderProgram { private: /* ES2 on iOS (apparently independent on the device) has only 8 texture - units, so be careful to not step over that. ES3 on the same has 16. */ + units, so be careful to not step over that. ES3 on the same has 16. + Not using the default (0) because this shader is quite specific. + Unit 6 is used by Shaders::Vector and Shaders::DistanceFieldVector. */ enum: Int { TextureUnit = 7 }; Int scalingUniform{0},