Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Replace rather than update Metal buffers #1842

Merged
merged 9 commits into from
Nov 20, 2023
25 changes: 23 additions & 2 deletions src/mbgl/mtl/buffer_resource.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -91,8 +91,29 @@ void BufferResource::update(const void* data, std::size_t updateSize, std::size_

if (updateSize > 0) {
if (buffer) {
if (auto* content = static_cast<uint8_t*>(buffer->contents())) {
std::memcpy(content + offset, data, updateSize);
if (auto* const content = static_cast<uint8_t*>(buffer->contents())) {
mwilsnd marked this conversation as resolved.
Show resolved Hide resolved
// Until we can be sure that the buffer is not still in use to render the
// previous frame, replace it with a new buffer instead of updating it.
auto& device = context.getBackend().getDevice();

const bool updateIsEntireBuffer = (offset == 0 && updateSize == size);
const auto copySource = updateIsEntireBuffer ? data : content;
auto newBuffer = NS::TransferPtr(device->newBuffer(copySource, size, usage));
mwilsnd marked this conversation as resolved.
Show resolved Hide resolved

assert(newBuffer);
if (newBuffer) {
buffer = std::move(newBuffer);
TimSylvester marked this conversation as resolved.
Show resolved Hide resolved

// Apply the update to the new buffer, if necessary
if (!updateIsEntireBuffer) {
auto* const newContent = static_cast<uint8_t*>(buffer->contents());

assert(newContent);
if (newContent) {
std::memcpy(newContent + offset, data, updateSize);
}
}
}
}
} else {
std::memcpy(raw.data() + offset, data, updateSize);
Expand Down
2 changes: 2 additions & 0 deletions src/mbgl/mtl/drawable.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -499,6 +499,8 @@ void Drawable::upload(gfx::UploadPass& uploadPass_) {
}

if (impl->indexes->getDirty()) {
// Create or update a buffer for the index data. We don't update any
// existing buffer because it may still be in use by the previous frame.
auto indexBufferResource{uploadPass.createIndexBufferResource(
impl->indexes->data(), impl->indexes->bytes(), usage, /*persistent=*/false)};
auto indexBuffer = std::make_unique<gfx::IndexBuffer>(impl->indexes->elements(),
Expand Down