From d579e0e2c6cf11b3bc6067e1ede0f7020f146cef Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Vladim=C3=ADr=20Vondru=C5=A1?= Date: Thu, 29 Aug 2013 16:45:05 +0200 Subject: [PATCH] MeshTools: return pointer to buffer from fullScreenTriangle(). On systems without VAOs and without gl_VertexID the buffer needs to be bound before every draw call. This way the buffer reference would become dangling one, because the buffer is moved out somewhere else. Now creating it on heap and returning pointer instead. This multiple code path implementation is really starting to pay out, yay! --- src/MeshTools/FullScreenTriangle.cpp | 11 +++++++---- src/MeshTools/FullScreenTriangle.h | 6 +++--- 2 files changed, 10 insertions(+), 7 deletions(-) diff --git a/src/MeshTools/FullScreenTriangle.cpp b/src/MeshTools/FullScreenTriangle.cpp index b3e560445..c2a810eba 100644 --- a/src/MeshTools/FullScreenTriangle.cpp +++ b/src/MeshTools/FullScreenTriangle.cpp @@ -32,25 +32,28 @@ namespace Magnum { namespace MeshTools { -std::pair fullScreenTriangle() { +std::pair fullScreenTriangle() { Mesh mesh; mesh.setPrimitive(Mesh::Primitive::Triangles) .setVertexCount(3); - Buffer buffer; + Buffer* buffer = nullptr; #ifndef MAGNUM_TARGET_GLES if(!Context::current()->isVersionSupported(Version::GL300)) #else if(!Context::current()->isVersionSupported(Version::GLES300)) #endif { + buffer = new Buffer; constexpr Vector2 triangle[] = { Vector2(-1.0, 1.0), Vector2(-1.0, -3.0), Vector2( 3.0, 1.0) }; - buffer.setData(triangle, Buffer::Usage::StaticDraw); - mesh.addVertexBuffer(buffer, 0, AbstractShaderProgram::Attribute<0, Vector2>()); + buffer->setData(triangle, Buffer::Usage::StaticDraw); + /** @todo Is it possible to attach moveable buffer here to avoid heap + allocation? OTOH this is more effective in most (modern) cases */ + mesh.addVertexBuffer(*buffer, 0, AbstractShaderProgram::Attribute<0, Vector2>()); } return {std::move(buffer), std::move(mesh)}; diff --git a/src/MeshTools/FullScreenTriangle.h b/src/MeshTools/FullScreenTriangle.h index 02c041661..3067719ef 100644 --- a/src/MeshTools/FullScreenTriangle.h +++ b/src/MeshTools/FullScreenTriangle.h @@ -52,8 +52,8 @@ computed from them. The vertex positions are, in order: On OpenGL 2.1 and OpenGL ES 2.0 the vertex positions are passed explicitly as attribute `0`, contained in the buffer. On OpenGL 3.0+ and OpenGL ES 3.0+ the mesh is attribute-less and the vertex positions can be computed using -`gl_VertexID` builtin shader variable, thus the returned vertex buffer is empty -and basically useless. +`gl_VertexID` builtin shader variable, thus `nullptr` is returned instead of +vertex buffer. Computing positions in vertex shader in a portable way might be done like this. For OpenGL 2.1 and OpenGL ES 2.0 you then need to bind the location of `position` @@ -80,7 +80,7 @@ void main() { @attention The implementation needs to check OpenGL version, so it expects active context. */ -std::pair MAGNUM_MESHTOOLS_EXPORT fullScreenTriangle(); +std::pair MAGNUM_MESHTOOLS_EXPORT fullScreenTriangle(); }}