From f0c6c97402152e093b55ed4151b1cefdc91f13c4 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Vladim=C3=ADr=20Vondru=C5=A1?= Date: Sun, 13 Apr 2025 12:46:49 +0200 Subject: [PATCH] GL: explicitly test creating a GL::Mesh from empty buffers. --- src/Magnum/GL/Test/MeshGLTest.cpp | 60 +++++++++++++++++++++++++++++-- 1 file changed, 58 insertions(+), 2 deletions(-) diff --git a/src/Magnum/GL/Test/MeshGLTest.cpp b/src/Magnum/GL/Test/MeshGLTest.cpp index 58f02519d..1a24e5400 100644 --- a/src/Magnum/GL/Test/MeshGLTest.cpp +++ b/src/Magnum/GL/Test/MeshGLTest.cpp @@ -143,6 +143,8 @@ struct MeshGLTest: OpenGLTester { template void setIndexBufferTransferOwnership(); template void setIndexBufferRangeTransferOwnership(); + void addEmptyBuffer(); + void setIndexOffset(); void indexTypeSetIndexOffsetNotIndexed(); @@ -235,6 +237,14 @@ struct MeshGLTest: OpenGLTester { #endif }; +const struct { + const char* name; + bool indexed; +} AddEmptyBufferData[]{ + {"", false}, + {"indexed", true}, +}; + const struct { const char* name; bool vertexId; @@ -637,9 +647,12 @@ MeshGLTest::MeshGLTest() { &MeshGLTest::setIndexBufferTransferOwnership, &MeshGLTest::setIndexBufferTransferOwnership, &MeshGLTest::setIndexBufferRangeTransferOwnership, - &MeshGLTest::setIndexBufferRangeTransferOwnership, + &MeshGLTest::setIndexBufferRangeTransferOwnership}); + + addInstancedTests({&MeshGLTest::addEmptyBuffer}, + Containers::arraySize(AddEmptyBufferData)); - &MeshGLTest::setIndexOffset, + addTests({&MeshGLTest::setIndexOffset, &MeshGLTest::indexTypeSetIndexOffsetNotIndexed, @@ -2683,6 +2696,49 @@ template void MeshGLTest::setIndexBufferRangeTransferOwnership() { CORRADE_VERIFY(!glIsBuffer(id)); } +void MeshGLTest::addEmptyBuffer() { + auto&& data = AddEmptyBufferData[testCaseInstanceId()]; + setTestCaseDescription(data.name); + + /* Tests that adding completely empty buffers (so, in some cases not even + created yet, just glGenBuffers()'d) works */ + + Buffer vertices{Buffer::TargetHint::Array}; + Buffer indices{NoCreate}; + if(data.indexed) + indices = Buffer{Buffer::TargetHint::ElementArray}; + + /* Verifying two separate attributes while just one is used to ensure the + handling isn't somehow special for the second one */ + Mesh mesh; + mesh.addVertexBuffer(vertices, 0, + Attribute<0, Float>{}, + Attribute<1, Float>{}) + .setCount(0); + + if(data.indexed) + mesh.setIndexBuffer(indices, 0, MeshIndexType::UnsignedInt); + + MAGNUM_VERIFY_NO_GL_ERROR(); + + /* This should do nothing */ + FloatShader shader{"float", "vec4(valueInterpolated, 0.0, 0.0, 0.0)"}; + shader.draw(mesh); + + MAGNUM_VERIFY_NO_GL_ERROR(); + + /* Filling the buffers and drawing again should also behave. Not interested + in the output, just that it doesn't generate any GL errors and doesn't + crash either. */ + vertices.setData({Vector2{}, Vector2{}, Vector2{}}); + if(data.indexed) + indices.setData({0u, 1u, 2u}); + mesh.setCount(3); + shader.draw(mesh); + + MAGNUM_VERIFY_NO_GL_ERROR(); +} + void MeshGLTest::setIndexOffset() { /* Like setIndexBuffer(), but with a four-byte index type and the Checker internals unwrapped to call setIndexOffset() on the Mesh directly