From 6ebbc3dd6a0fad807e4e22dcfd6392a6fc73f303 Mon Sep 17 00:00:00 2001 From: Alex Cristici Date: Fri, 26 Apr 2024 19:15:58 +0300 Subject: [PATCH] UBO layer bind optimization (#2335) --- src/mbgl/gl/layer_group_gl.cpp | 24 ++++++++++++++++++------ src/mbgl/mtl/layer_group.cpp | 12 +++++++++--- src/mbgl/mtl/tile_layer_group.cpp | 12 +++++++++--- 3 files changed, 36 insertions(+), 12 deletions(-) diff --git a/src/mbgl/gl/layer_group_gl.cpp b/src/mbgl/gl/layer_group_gl.cpp index 3e452e6eb26..9f3a83b217d 100644 --- a/src/mbgl/gl/layer_group_gl.cpp +++ b/src/mbgl/gl/layer_group_gl.cpp @@ -93,8 +93,7 @@ void TileLayerGroupGL::render(RenderOrchestrator&, PaintParameters& parameters) const auto debugGroupRender = parameters.encoder->createDebugGroup(label_render.c_str()); #endif - bindUniformBuffers(); - + bool bindUBOs = false; visitDrawables([&](gfx::Drawable& drawable) { if (!drawable.getEnabled() || !drawable.hasRenderPass(parameters.pass)) { return; @@ -121,10 +120,17 @@ void TileLayerGroupGL::render(RenderOrchestrator&, PaintParameters& parameters) context.setStencilMode(drawable.getEnableStencil() ? stencilMode3d : gfx::StencilMode::disabled()); } + if (!bindUBOs) { + bindUniformBuffers(); + bindUBOs = true; + } + drawable.draw(parameters); }); - unbindUniformBuffers(); + if (bindUBOs) { + unbindUniformBuffers(); + } } void TileLayerGroupGL::bindUniformBuffers() const { @@ -178,8 +184,7 @@ void LayerGroupGL::render(RenderOrchestrator&, PaintParameters& parameters) { return; } - bindUniformBuffers(); - + bool bindUBOs = false; visitDrawables([&](gfx::Drawable& drawable) { if (!drawable.getEnabled() || !drawable.hasRenderPass(parameters.pass)) { return; @@ -193,10 +198,17 @@ void LayerGroupGL::render(RenderOrchestrator&, PaintParameters& parameters) { tweaker->execute(drawable, parameters); } + if (!bindUBOs) { + bindUniformBuffers(); + bindUBOs = true; + } + drawable.draw(parameters); }); - unbindUniformBuffers(); + if (bindUBOs) { + unbindUniformBuffers(); + } } void LayerGroupGL::bindUniformBuffers() const { diff --git a/src/mbgl/mtl/layer_group.cpp b/src/mbgl/mtl/layer_group.cpp index 962c8a91b32..c653334bd28 100644 --- a/src/mbgl/mtl/layer_group.cpp +++ b/src/mbgl/mtl/layer_group.cpp @@ -46,8 +46,7 @@ void LayerGroup::render(RenderOrchestrator&, PaintParameters& parameters) { auto& context = static_cast(parameters.context); auto& renderPass = static_cast(*parameters.renderPass); - bindUniformBuffers(renderPass); - + bool bindUBOs = false; visitDrawables([&](gfx::Drawable& drawable) { if (!drawable.getEnabled() || !drawable.hasRenderPass(parameters.pass)) { return; @@ -57,10 +56,17 @@ void LayerGroup::render(RenderOrchestrator&, PaintParameters& parameters) { tweaker->execute(drawable, parameters); } + if (!bindUBOs) { + bindUniformBuffers(renderPass); + bindUBOs = true; + } + drawable.draw(parameters); }); - unbindUniformBuffers(renderPass); + if (bindUBOs) { + unbindUniformBuffers(renderPass); + } } void LayerGroup::bindUniformBuffers(RenderPass& renderPass) const noexcept { diff --git a/src/mbgl/mtl/tile_layer_group.cpp b/src/mbgl/mtl/tile_layer_group.cpp index 174570f592f..0a8202415b2 100644 --- a/src/mbgl/mtl/tile_layer_group.cpp +++ b/src/mbgl/mtl/tile_layer_group.cpp @@ -120,8 +120,7 @@ void TileLayerGroup::render(RenderOrchestrator&, PaintParameters& parameters) { parameters.renderTileClippingMasks(stencilTiles); } - bindUniformBuffers(renderPass); - + bool bindUBOs = false; visitDrawables([&](gfx::Drawable& drawable) { if (!drawable.getEnabled() || !drawable.hasRenderPass(parameters.pass)) { return; @@ -139,10 +138,17 @@ void TileLayerGroup::render(RenderOrchestrator&, PaintParameters& parameters) { renderPass.setDepthStencilState(state); } + if (!bindUBOs) { + bindUniformBuffers(renderPass); + bindUBOs = true; + } + drawable.draw(parameters); }); - unbindUniformBuffers(renderPass); + if (bindUBOs) { + unbindUniformBuffers(renderPass); + } } void TileLayerGroup::bindUniformBuffers(RenderPass& renderPass) const noexcept {