From b6dbc8fc585acf9f5439f69126e991d2cb1034a7 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Vladim=C3=ADr=20Vondru=C5=A1?= Date: Sun, 9 Oct 2016 22:22:11 +0200 Subject: [PATCH] Check for mesh emptiness even before binding the shader. Prevents unnecessary state touching. --- src/Magnum/Mesh.cpp | 6 +++--- src/Magnum/MeshView.cpp | 6 ++++++ src/Magnum/Test/MeshGLTest.cpp | 18 +++++++++++------- 3 files changed, 20 insertions(+), 10 deletions(-) diff --git a/src/Magnum/Mesh.cpp b/src/Magnum/Mesh.cpp index 938f1915e..507d58c5b 100644 --- a/src/Magnum/Mesh.cpp +++ b/src/Magnum/Mesh.cpp @@ -245,6 +245,9 @@ Mesh& Mesh::setIndexBuffer(Buffer& buffer, GLintptr offset, IndexType type, Unsi } void Mesh::draw(AbstractShaderProgram& shader) { + /* Nothing to draw, exit without touching any state */ + if(!_count || !_instanceCount) return; + shader.use(); #ifndef MAGNUM_TARGET_GLES @@ -266,9 +269,6 @@ void Mesh::drawInternal(Int count, Int baseVertex, Int instanceCount, GLintptr i { const Implementation::MeshState& state = *Context::current().state().mesh; - /* Nothing to draw */ - if(!count || !instanceCount) return; - (this->*state.bindImplementation)(); /* Non-instanced mesh */ diff --git a/src/Magnum/MeshView.cpp b/src/Magnum/MeshView.cpp index aa863dca9..e1a55c7a4 100644 --- a/src/Magnum/MeshView.cpp +++ b/src/Magnum/MeshView.cpp @@ -130,6 +130,9 @@ void MeshView::multiDrawImplementationDefault(std::initializer_list> meshes) { for(MeshView& mesh: meshes) { + /* Nothing to draw in this mesh */ + if(!mesh._count) continue; + #ifndef MAGNUM_TARGET_GLES2 mesh._original.get().drawInternal(mesh._count, mesh._baseVertex, mesh._instanceCount, mesh._indexOffset, mesh._indexStart, mesh._indexEnd); #else @@ -145,6 +148,9 @@ MeshView& MeshView::setIndexRange(Int first) { } void MeshView::draw(AbstractShaderProgram& shader) { + /* Nothing to draw, exit without touching any state */ + if(!_count || !_instanceCount) return; + shader.use(); #ifndef MAGNUM_TARGET_GLES diff --git a/src/Magnum/Test/MeshGLTest.cpp b/src/Magnum/Test/MeshGLTest.cpp index 41b4543cc..e798a7423 100644 --- a/src/Magnum/Test/MeshGLTest.cpp +++ b/src/Magnum/Test/MeshGLTest.cpp @@ -1853,19 +1853,23 @@ MultiChecker::MultiChecker(AbstractShaderProgram&& shader, Mesh& mesh): framebuf mesh.setPrimitive(MeshPrimitive::Points) .setCount(2); + /* Set zero count so we test mesh skipping */ + MeshView a{mesh}; + a.setCount(0); + /* Skip first vertex so we test also offsets */ - MeshView a(mesh); - a.setCount(1) + MeshView b{mesh}; + b.setCount(1) .setBaseVertex(mesh.baseVertex()); - MeshView b(mesh); - b.setCount(1); + MeshView c{mesh}; + c.setCount(1); if(mesh.isIndexed()) { - b.setBaseVertex(mesh.baseVertex()) + c.setBaseVertex(mesh.baseVertex()) .setIndexRange(1); - } else b.setBaseVertex(1); + } else c.setBaseVertex(1); - MeshView::draw(shader, {a, b}); + MeshView::draw(shader, {a, b, c}); } template T MultiChecker::get(PixelFormat format, PixelType type) {