Skip to content

Commit 779b893

Browse files
AtlasProgramming“Malcolmpre-commit-ci[bot]
authored
Reduce duplicate GPU buffer uploads (maplibre#3577)
Co-authored-by: “Malcolm <“[email protected]”> Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com>
1 parent 753b7ae commit 779b893

File tree

4 files changed

+24
-3
lines changed

4 files changed

+24
-3
lines changed

include/mbgl/mtl/render_pass.hpp

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,9 @@ class RenderPass final : public gfx::RenderPass {
4141
/// Set the sampler for a texture binding
4242
void setFragmentSamplerState(const MTLSamplerStatePtr&, int32_t location);
4343

44+
/// Set the render pipeline state
45+
void setRenderPipelineState(const MTLRenderPipelineStatePtr&);
46+
4447
void endEncoding();
4548

4649
/// Resets the states and bindings of the render pass to deal
@@ -69,6 +72,8 @@ class RenderPass final : public gfx::RenderPass {
6972
mtl::CommandEncoder& commandEncoder;
7073
MTLRenderCommandEncoderPtr encoder;
7174
MTLDepthStencilStatePtr currentDepthStencilState;
75+
MTLRenderPipelineStatePtr currentPipelineState;
76+
7277
int32_t currentStencilReferenceValue = 0;
7378
std::vector<gfx::DebugGroup<gfx::RenderPass>> debugGroups;
7479

src/mbgl/mtl/buffer_resource.cpp

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -103,15 +103,22 @@ void BufferResource::update(const void* newData, std::size_t updateSize, std::si
103103
// Until we can be sure that the buffer is not still in use to render the
104104
// previous frame, replace it with a new buffer instead of updating it.
105105

106-
auto& device = context.getBackend().getDevice();
107106
const uint8_t* newBufferSource = nullptr;
108107
std::unique_ptr<uint8_t[]> tempBuffer;
108+
const bool updateIsEntireBuffer = (offset == 0 && updateSize == size);
109+
110+
// If the entire buffer is being updated, make sure it's changed
111+
if (updateIsEntireBuffer) {
112+
if (memcmp(buffer->contents(), newData, updateSize) == 0) {
113+
return;
114+
}
115+
}
109116

117+
auto& device = context.getBackend().getDevice();
110118
// `[MTLBuffer contents]` may involve memory mapping and/or synchronization. If the entire
111119
// buffer is being replaced, avoid accessing the old one by creating the new buffer directly
112120
// from the given data. If it's just being updated, apply the update to a local buffer to
113121
// avoid needing to access the new buffer.
114-
const bool updateIsEntireBuffer = (offset == 0 && updateSize == size);
115122
if (updateIsEntireBuffer) {
116123
newBufferSource = static_cast<const uint8_t*>(newData);
117124
} else {

src/mbgl/mtl/drawable.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -223,7 +223,7 @@ void Drawable::draw(PaintParameters& parameters) const {
223223
mbgl::util::hash(getColorMode().hash(), impl->vertexDescHash));
224224
}
225225
if (impl->pipelineState) {
226-
encoder->setRenderPipelineState(impl->pipelineState.get());
226+
renderPass.setRenderPipelineState(impl->pipelineState);
227227
} else {
228228
assert(!"Failed to create render pipeline state");
229229
return;

src/mbgl/mtl/render_pass.cpp

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,7 @@ void RenderPass::endEncoding() {
6666
}
6767

6868
void RenderPass::resetState() {
69+
currentPipelineState.reset();
6970
currentDepthStencilState.reset();
7071
currentStencilReferenceValue = 0;
7172
for (int i = 0; i < maxBinds; ++i) {
@@ -193,6 +194,14 @@ void RenderPass::setFragmentSamplerState(const MTLSamplerStatePtr& state, int32_
193194
}
194195
}
195196

197+
/// Set the render pipeline state
198+
void RenderPass::setRenderPipelineState(const MTLRenderPipelineStatePtr& pipelineState) {
199+
if (pipelineState != currentPipelineState) {
200+
currentPipelineState = pipelineState;
201+
encoder->setRenderPipelineState(currentPipelineState.get());
202+
}
203+
}
204+
196205
void RenderPass::setCullMode(const MTL::CullMode mode) {
197206
if (mode != currentCullMode) {
198207
encoder->setCullMode(mode);

0 commit comments

Comments
 (0)