From 6527ec35c2c2ac76cedc0c75efb32420787f9f7f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Vladim=C3=ADr=20Vondru=C5=A1?= Date: Mon, 17 Jan 2022 17:44:33 +0100 Subject: [PATCH] MeshTools: assert in compile() if non-contiguous index buffers are used. --- src/Magnum/MeshTools/Compile.cpp | 3 +++ src/Magnum/MeshTools/Compile.h | 3 +++ src/Magnum/MeshTools/Test/CompileGLTest.cpp | 21 +++++++++++++++++++++ 3 files changed, 27 insertions(+) diff --git a/src/Magnum/MeshTools/Compile.cpp b/src/Magnum/MeshTools/Compile.cpp index 9ceee82f0..2603c4200 100644 --- a/src/Magnum/MeshTools/Compile.cpp +++ b/src/Magnum/MeshTools/Compile.cpp @@ -160,6 +160,9 @@ GL::Mesh compileInternal(const Trade::MeshData& meshData, GL::Buffer&& indices, } if(meshData.isIndexed()) { + CORRADE_ASSERT(Short(meshIndexTypeSize(meshData.indexType())) == meshData.indexStride(), + "MeshTools::compile():" << meshData.indexType() << "with stride of" << meshData.indexStride() << "bytes isn't supported by OpenGL", GL::Mesh{}); + mesh.setIndexBuffer(std::move(indices), meshData.indexOffset(), meshData.indexType()) .setCount(meshData.indexCount()); } else mesh.setCount(meshData.vertexCount()); diff --git a/src/Magnum/MeshTools/Compile.h b/src/Magnum/MeshTools/Compile.h index fac98b79f..0b694c23f 100644 --- a/src/Magnum/MeshTools/Compile.h +++ b/src/Magnum/MeshTools/Compile.h @@ -120,6 +120,9 @@ possibly also an index buffer, if the mesh is indexed. - Implementation-specific @ref Magnum::MeshPrimitive values are passed as-is with @ref meshPrimitiveUnwrap(). It's the user responsibility to ensure an implementation-specific value is valid in this context. +- The index buffer is expected to be contiguous (size of the index type equal + to @ref Trade::MeshData::indexStride()). OpenGL doesn't support interleaved + index buffers. - Stride of all attributes is expected to be positive. OpenGL doesn't support zero and negative strides. diff --git a/src/Magnum/MeshTools/Test/CompileGLTest.cpp b/src/Magnum/MeshTools/Test/CompileGLTest.cpp index 554283253..0ede0d7a7 100644 --- a/src/Magnum/MeshTools/Test/CompileGLTest.cpp +++ b/src/Magnum/MeshTools/Test/CompileGLTest.cpp @@ -104,6 +104,8 @@ struct CompileGLTest: GL::OpenGLTester { void multipleAttributes(); void packedAttributes(); + void unsupportedIndexStride(); + void customAttribute(); void unsupportedAttribute(); void unsupportedAttributeStride(); @@ -256,6 +258,8 @@ CompileGLTest::CompileGLTest() { &CompileGLTest::renderSetup, &CompileGLTest::renderTeardown); + addTests({&CompileGLTest::unsupportedIndexStride}); + addInstancedTests({&CompileGLTest::customAttribute, &CompileGLTest::unsupportedAttribute}, Containers::arraySize(CustomAttributeWarningData)); @@ -1153,6 +1157,23 @@ void CompileGLTest::packedAttributes() { #endif } +void CompileGLTest::unsupportedIndexStride() { + #ifdef CORRADE_NO_ASSERT + CORRADE_SKIP("CORRADE_NO_ASSERT defined, can't test assertions"); + #endif + + UnsignedShort indices[2]{}; + Trade::MeshData data{MeshPrimitive::Points, + {}, indices, Trade::MeshIndexData{Containers::stridedArrayView(indices).every(2)}, + 1}; + + std::ostringstream out; + Error redirectError{&out}; + compile(data); + CORRADE_COMPARE(out.str(), + "MeshTools::compile(): MeshIndexType::UnsignedShort with stride of 4 bytes isn't supported by OpenGL\n"); +} + void CompileGLTest::customAttribute() { auto&& instanceData = CustomAttributeWarningData[testCaseInstanceId()]; setTestCaseDescription(instanceData.name);