Skip to content

Commit

Permalink
DebugTools, MeshTools, TextureTools: handle broken gl_VertexID.
Browse files Browse the repository at this point in the history
DebugTools::textureSubImage(), MeshTools::fullScreenTriangle() and
TextureTools::distanceField() all plagued by this on SwiftShader.
  • Loading branch information
mosra committed Mar 3, 2019
1 parent 5bf09f0 commit 8ba2cac
Show file tree
Hide file tree
Showing 7 changed files with 56 additions and 25 deletions.
12 changes: 12 additions & 0 deletions doc/changelog.dox
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
26 changes: 22 additions & 4 deletions src/Magnum/DebugTools/TextureImage.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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<GL::Extensions::MAGNUM::shader_vertex_id>())
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<GL::Extensions::MAGNUM::shader_vertex_id>()) {
bindAttributeLocation(0, "position");
}

CORRADE_INTERNAL_ASSERT_OUTPUT(link());

levelUniform = uniformLocation("level");
setUniform(uniformLocation("textureData"), 0);
Expand Down Expand Up @@ -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<GL::Extensions::MAGNUM::shader_vertex_id>()) {
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();
Expand Down
8 changes: 8 additions & 0 deletions src/Magnum/DebugTools/TextureImage.vert
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
8 changes: 2 additions & 6 deletions src/Magnum/MeshTools/FullScreenTriangle.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand All @@ -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<GL::Extensions::MAGNUM::shader_vertex_id>(version)) {
constexpr Vector2 triangle[]{
{-1.0f, 1.0f},
{-1.0f, -3.0f},
Expand Down
7 changes: 5 additions & 2 deletions src/Magnum/Shaders/FullScreenTriangle.glsl
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
5 changes: 5 additions & 0 deletions src/Magnum/Shaders/Implementation/CreateCompatibilityShader.h
Original file line number Diff line number Diff line change
Expand Up @@ -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<GL::Extensions::MAGNUM::shader_vertex_id>(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 */
Expand Down
15 changes: 2 additions & 13 deletions src/Magnum/TextureTools/DistanceField.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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<GL::Extensions::MAGNUM::shader_vertex_id>()) {
bindAttributeLocation(Position::Location, "position");
}

Expand Down Expand Up @@ -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<GL::Extensions::MAGNUM::shader_vertex_id>())
{
constexpr Vector2 triangle[] = {
Vector2(-1.0, 1.0),
Expand Down

0 comments on commit 8ba2cac

Please sign in to comment.