Skip to content

Commit

Permalink
[GL] Made VAOs multi-context aware.
Browse files Browse the repository at this point in the history
- Vertex array objects (VAOs) now work across multiple GL contexts.
- Replaced GL2XVertexArray with GLSharedContextVertexArray class to manage one VAO instance per GLContext on demand.
  • Loading branch information
LukasBanana committed Jul 4, 2024
1 parent 356207f commit 7734189
Show file tree
Hide file tree
Showing 24 changed files with 461 additions and 566 deletions.
128 changes: 0 additions & 128 deletions sources/Renderer/OpenGL/Buffer/GL2XVertexArray.cpp

This file was deleted.

69 changes: 0 additions & 69 deletions sources/Renderer/OpenGL/Buffer/GL2XVertexArray.h

This file was deleted.

83 changes: 14 additions & 69 deletions sources/Renderer/OpenGL/Buffer/GLBufferArrayWithVAO.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@

#include "GLBufferArrayWithVAO.h"
#include "GLBufferWithVAO.h"
#include "../GLObjectUtils.h"
#include "../RenderState/GLStateManager.h"
#include "../../CheckedCast.h"
#include "../../../Core/CoreUtils.h"
Expand All @@ -21,82 +20,28 @@ namespace LLGL
GLBufferArrayWithVAO::GLBufferArrayWithVAO(std::uint32_t numBuffers, Buffer* const * bufferArray) :
GLBufferArray { numBuffers, bufferArray }
{
#ifdef LLGL_GL_ENABLE_OPENGL2X
if (!HasNativeVAO())
/* Build vertex array and finalize afterwards as it references multiple buffers */
while (GLBuffer* bufferGL = NextArrayResource<GLBuffer>(numBuffers, bufferArray))
{
/* Build vertex array with emulator (for GL 2.x compatibility) */
BuildVertexArrayWithEmulator(numBuffers, bufferArray);
}
else
#endif // /LLGL_GL_ENABLE_OPENGL2X
{
/* Build vertex array with native VAO */
BuildVertexArrayWithVAO(numBuffers, bufferArray);
}
}

void GLBufferArrayWithVAO::SetDebugName(const char* name)
{
#ifdef LLGL_GL_ENABLE_OPENGL2X
if (HasNativeVAO())
#endif
{
/* Set label for VAO */
GLSetObjectLabel(GL_VERTEX_ARRAY, vao_.GetID(), name);
}
}


/*
* ======= Private: =======
*/

[[noreturn]]
static void ThrowNoVertexBufferErr()
{
throw std::invalid_argument(
"cannot build vertex array with buffer that was not created with the 'LLGL::BindFlags::VertexBuffer' flag"
);
}

void GLBufferArrayWithVAO::BuildVertexArrayWithVAO(std::uint32_t numBuffers, Buffer* const * bufferArray)
{
/* Bind VAO */
GLStateManager::Get().BindVertexArray(GetVaoID());
{
while (GLBuffer* bufferGL = NextArrayResource<GLBuffer>(numBuffers, bufferArray))
if ((bufferGL->GetBindFlags() & BindFlags::VertexBuffer) != 0)
{
if ((bufferGL->GetBindFlags() & BindFlags::VertexBuffer) != 0)
{
/* Bind VBO and build vertex layout */
auto* vertexBufferGL = LLGL_CAST(GLBufferWithVAO*, bufferGL);
GLStateManager::Get().BindBuffer(GLBufferTarget::ArrayBuffer, vertexBufferGL->GetID());
vao_.BuildVertexLayout(vertexBufferGL->GetVertexAttribs());
}
else
ThrowNoVertexBufferErr();
/* Bind VBO and build vertex layout */
auto* vertexBufferGL = LLGL_CAST(GLBufferWithVAO*, bufferGL);
GLStateManager::Get().BindBuffer(GLBufferTarget::ArrayBuffer, vertexBufferGL->GetID());
vertexArray_.BuildVertexLayout(vertexBufferGL->GetID(), vertexBufferGL->GetVertexAttribs());
}
}
GLStateManager::Get().BindVertexArray(0);
}

#ifdef LLGL_GL_ENABLE_OPENGL2X

void GLBufferArrayWithVAO::BuildVertexArrayWithEmulator(std::uint32_t numBuffers, Buffer* const * bufferArray)
{
while (numBuffers-- > 0)
{
if (((*bufferArray)->GetBindFlags() & BindFlags::VertexBuffer) != 0)
else
{
auto* vertexBufferGL = LLGL_CAST(GLBufferWithVAO*, (*bufferArray++));
vertexArrayGL2X_.BuildVertexLayout(vertexBufferGL->GetID(), vertexBufferGL->GetVertexAttribs());
LLGL_TRAP("cannot build vertex array with buffer that was not created with the 'LLGL::BindFlags::VertexBuffer' flag");
}
else
ThrowNoVertexBufferErr();
}
vertexArray_.Finalize();
}

#endif // /LLGL_GL_ENABLE_OPENGL2X
void GLBufferArrayWithVAO::SetDebugName(const char* name)
{
vertexArray_.SetDebugName(name);
}


} // /namespace LLGL
Expand Down
32 changes: 5 additions & 27 deletions sources/Renderer/OpenGL/Buffer/GLBufferArrayWithVAO.h
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,7 @@


#include "GLBufferArray.h"
#include "GLVertexArrayObject.h"
#ifdef LLGL_GL_ENABLE_OPENGL2X
# include "GL2XVertexArray.h"
#endif
#include "GLSharedContextVertexArray.h"


namespace LLGL
Expand All @@ -31,34 +28,15 @@ class GLBufferArrayWithVAO final : public GLBufferArray

GLBufferArrayWithVAO(std::uint32_t numBuffers, Buffer* const * bufferArray);

// Returns the ID of the vertex-array-object (VAO)
inline GLuint GetVaoID() const
{
return vao_.GetID();
}

#ifdef LLGL_GL_ENABLE_OPENGL2X
// Returns the GL 2.x compatible vertex-array emulator.
inline const GL2XVertexArray& GetVertexArrayGL2X() const
// Returns the vertex array which can be shared across multiple GL contexts.
inline GLSharedContextVertexArray* GetVertexArray()
{
return vertexArrayGL2X_;
return &vertexArray_;
}
#endif

private:

void BuildVertexArrayWithVAO(std::uint32_t numBuffers, Buffer* const * bufferArray);
#ifdef LLGL_GL_ENABLE_OPENGL2X
void BuildVertexArrayWithEmulator(std::uint32_t numBuffers, Buffer* const * bufferArray);
#endif

private:

GLVertexArrayObject vao_;

#ifdef LLGL_GL_ENABLE_OPENGL2X
GL2XVertexArray vertexArrayGL2X_;
#endif
GLSharedContextVertexArray vertexArray_;

};

Expand Down
Loading

0 comments on commit 7734189

Please sign in to comment.