Use timestamps for attribute updates (#2629)

This commit is contained in:
Tim Sylvester
2024-08-20 07:33:29 -07:00
committed by GitHub
parent c3a2fa247c
commit fceaa18caf
43 changed files with 439 additions and 163 deletions

View File

@@ -133,7 +133,7 @@ BufferResource& BufferResource::operator=(BufferResource&& other) noexcept {
}
void BufferResource::update(const void* newData, std::size_t updateSize, std::size_t offset) noexcept {
assert(size >= 0 && updateSize + offset <= size);
assert(updateSize + offset <= size);
updateSize = std::min(updateSize, size - offset);
if (updateSize <= 0) {
return;

View File

@@ -116,13 +116,14 @@ void Drawable::upload(gfx::UploadPass& uploadPass_) {
// We need either raw index data or a buffer already created from them.
// We can have a buffer and no indexes, but only if it's not marked dirty.
if (!impl->indexes || (impl->indexes->empty() && (!impl->indexes->getBuffer() || impl->indexes->getDirty()))) {
if (!impl->indexes || (impl->indexes->empty() &&
(!impl->indexes->getBuffer() || impl->indexes->isModifiedAfter(attributeUpdateTime)))) {
assert(!"Missing index data");
return;
}
if (impl->indexes->getDirty()) {
// Create or update a buffer for the index data. We don't update any
if (!impl->indexes->getBuffer() || impl->indexes->isModifiedAfter(attributeUpdateTime)) {
// Create 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)};
@@ -131,10 +132,9 @@ void Drawable::upload(gfx::UploadPass& uploadPass_) {
auto buffer = std::make_unique<IndexBuffer>(std::move(indexBuffer));
impl->indexes->setBuffer(std::move(buffer));
impl->indexes->setDirty(false);
}
const bool buildAttribs = !vertexAttributes || vertexAttributes->isDirty() ||
const bool buildAttribs = !vertexAttributes || vertexAttributes->isModifiedAfter(attributeUpdateTime) ||
impl->pipelineInfo.inputAttributes.empty();
if (buildAttribs) {
@@ -155,6 +155,7 @@ void Drawable::upload(gfx::UploadPass& uploadPass_) {
shader->getVertexAttributes(),
*vertexAttributes,
usage,
attributeUpdateTime,
vertexBuffers);
vertexAttributes->visitAttributes([](gfx::VertexAttribute& attrib) { attrib.setDirty(false); });
@@ -165,7 +166,7 @@ void Drawable::upload(gfx::UploadPass& uploadPass_) {
}
// build instance buffer
const bool buildInstanceBuffer = (instanceAttributes && instanceAttributes->isDirty());
const bool buildInstanceBuffer = (instanceAttributes && instanceAttributes->isModifiedAfter(attributeUpdateTime));
if (buildInstanceBuffer) {
// Build instance attribute buffers
@@ -177,6 +178,7 @@ void Drawable::upload(gfx::UploadPass& uploadPass_) {
shader->getInstanceAttributes(),
*instanceAttributes,
usage,
attributeUpdateTime,
instanceBuffers);
// clear dirty flag
@@ -185,6 +187,8 @@ void Drawable::upload(gfx::UploadPass& uploadPass_) {
if (impl->instanceBindings != instanceBindings_) {
impl->instanceBindings = std::move(instanceBindings_);
}
attributeUpdateTime = util::MonotonicTimer::now();
}
if (buildAttribs || buildInstanceBuffer) {
@@ -205,8 +209,8 @@ void Drawable::draw(PaintParameters& parameters) const {
}
auto& context = static_cast<Context&>(parameters.context);
auto& renderPass = static_cast<RenderPass&>(*parameters.renderPass);
auto& encoder = renderPass.getEncoder();
auto& renderPass_ = static_cast<RenderPass&>(*parameters.renderPass);
auto& encoder = renderPass_.getEncoder();
auto& commandBuffer = encoder.getCommandBuffer();
auto& shaderImpl = static_cast<mbgl::vulkan::ShaderProgram&>(*shader);
@@ -233,7 +237,7 @@ void Drawable::draw(PaintParameters& parameters) const {
}
}
impl->pipelineInfo.setRenderable(renderPass.getDescriptor().renderable);
impl->pipelineInfo.setRenderable(renderPass_.getDescriptor().renderable);
const uint32_t instances = instanceAttributes ? instanceAttributes->getMaxCount() : 1;
@@ -264,17 +268,17 @@ void Drawable::setIndexData(gfx::IndexVectorBasePtr indexes, std::vector<UniqueD
impl->segments = std::move(segments);
}
void Drawable::setVertices(std::vector<uint8_t>&& data, std::size_t count, gfx::AttributeDataType type) {
void Drawable::setVertices(std::vector<uint8_t>&& data, std::size_t count, gfx::AttributeDataType type_) {
impl->vertexCount = count;
impl->vertexType = type;
impl->vertexType = type_;
if (count && type != gfx::AttributeDataType::Invalid && !data.empty()) {
if (count && type_ != gfx::AttributeDataType::Invalid && !data.empty()) {
if (!vertexAttributes) {
vertexAttributes = std::make_shared<VertexAttributeArray>();
}
if (auto& attrib = vertexAttributes->set(impl->vertexAttrId, /*index=*/-1, type)) {
if (auto& attrib = vertexAttributes->set(impl->vertexAttrId, /*index=*/-1, type_)) {
attrib->setRawData(std::move(data));
attrib->setStride(VertexAttribute::getStrideOf(type));
attrib->setStride(VertexAttribute::getStrideOf(type_));
} else {
using namespace std::string_literals;
Log::Warning(Event::General,
@@ -352,9 +356,10 @@ bool Drawable::bindAttributes(CommandEncoder& encoder) const noexcept {
commandBuffer->bindVertexBuffers(0, impl->vulkanVertexBuffers, impl->vulkanVertexOffsets);
if (impl->indexes) {
const auto* indexBuffer = static_cast<const IndexBuffer*>(impl->indexes->getBuffer());
const auto& indexBufferResource = indexBuffer->buffer->getResource<IndexBufferResource>().get();
commandBuffer->bindIndexBuffer(indexBufferResource.getVulkanBuffer(), 0, vk::IndexType::eUint16);
if (const auto* indexBuffer = static_cast<const IndexBuffer*>(impl->indexes->getBuffer())) {
const auto& indexBufferResource = indexBuffer->buffer->getResource<IndexBufferResource>().get();
commandBuffer->bindIndexBuffer(indexBufferResource.getVulkanBuffer(), 0, vk::IndexType::eUint16);
}
}
return true;

View File

@@ -8,8 +8,8 @@
namespace mbgl {
namespace vulkan {
RenderPass::RenderPass(CommandEncoder& commandEncoder_, const char* name, const gfx::RenderPassDescriptor& descriptor)
: descriptor(descriptor),
RenderPass::RenderPass(CommandEncoder& commandEncoder_, const char* name, const gfx::RenderPassDescriptor& descriptor_)
: descriptor(descriptor_),
commandEncoder(commandEncoder_) {
auto& resource = descriptor.renderable.getResource<RenderableResource>();

View File

@@ -95,9 +95,9 @@ const gfx::UniqueVertexBufferResource& UploadPass::getBuffer(const gfx::VertexVe
// If it's changed, update it
if (rawBufSize <= resource.getSizeInBytes()) {
if (vec->getDirty()) {
if (vec->isModifiedAfter(resource.getLastUpdated())) {
updateVertexBufferResource(resource, rawBufPtr, rawBufSize);
vec->setDirty(false);
resource.setLastUpdated(vec->getLastModified());
}
return rawData->resource;
}
@@ -107,7 +107,6 @@ const gfx::UniqueVertexBufferResource& UploadPass::getBuffer(const gfx::VertexVe
auto buffer = std::make_unique<VertexBuffer>();
buffer->resource = createVertexBufferResource(rawBufPtr, rawBufSize, usage, /*persistent=*/false);
vec->setBuffer(std::move(buffer));
vec->setDirty(false);
return static_cast<VertexBuffer*>(vec->getBuffer())->resource;
}
}
@@ -122,6 +121,7 @@ gfx::AttributeBindingArray UploadPass::buildAttributeBindings(
const gfx::VertexAttributeArray& defaults,
const gfx::VertexAttributeArray& overrides,
const gfx::BufferUsageType usage,
const std::chrono::duration<double> /*lastUpdate*/,
/*out*/ std::vector<std::unique_ptr<gfx::VertexBufferResource>>&) {
gfx::AttributeBindingArray bindings;
@@ -175,8 +175,11 @@ gfx::AttributeBindingArray UploadPass::buildAttributeBindings(
assert(false);
};
defaults.resolve(overrides, resolveAttr);
// This version is called when the attribute is available, but isn't being used by the shader
const auto missingAttr = [&](const size_t, auto& attr_) -> void {
attr_->setDirty(false);
};
defaults.resolve(overrides, resolveAttr, missingAttr);
return bindings;
}

View File

@@ -25,6 +25,7 @@ const gfx::UniqueVertexBufferResource& VertexAttribute::getBuffer(gfx::VertexAtt
attrib.rawData.data(), attrib.rawData.size(), usage, false);
attrib.setBuffer(std::move(buffer));
attrib.setRawData({});
attrib_.setDirty(false);
} else {
assert(false);
}
@@ -33,18 +34,5 @@ const gfx::UniqueVertexBufferResource& VertexAttribute::getBuffer(gfx::VertexAtt
return attrib_.getBuffer();
}
bool VertexAttributeArray::isDirty() const {
return std::any_of(attrs.begin(), attrs.end(), [](const auto& attr) {
if (attr) {
// If we have shared data, the dirty flag from that overrides ours
const auto& attrib = static_cast<const VertexAttribute&>(*attr);
if (const auto& shared = attrib.getSharedRawData()) {
return shared->getDirty();
}
}
return attr && attr->isDirty();
});
}
} // namespace vulkan
} // namespace mbgl