Skip to content

Commit

Permalink
Use timestamps for image descriptor updates (#3152)
Browse files Browse the repository at this point in the history
  • Loading branch information
adrian-cojocaru authored Jan 21, 2025
1 parent d960b11 commit 4ae61fb
Show file tree
Hide file tree
Showing 5 changed files with 25 additions and 6 deletions.
8 changes: 7 additions & 1 deletion include/mbgl/vulkan/descriptor_set.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ class DescriptorSet {

void allocate();

void markDirty(bool value = true);
virtual void markDirty();
void bind(CommandEncoder& encoder);

protected:
Expand Down Expand Up @@ -91,7 +91,13 @@ class ImageDescriptorSet : public DescriptorSet {
ImageDescriptorSet(Context& context_);
virtual ~ImageDescriptorSet() = default;

void markDirty() override;
const std::chrono::duration<double>& getLastModified() const { return lastModified; }

void update(const std::array<gfx::Texture2DPtr, shaders::maxTextureCountPerShader>& textures);

protected:
std::chrono::duration<double> lastModified;
};

} // namespace vulkan
Expand Down
2 changes: 2 additions & 0 deletions include/mbgl/vulkan/texture2d.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,7 @@ class Texture2D : public gfx::Texture2D {
size_t numChannels() const noexcept override;

bool isDirty() const { return samplerStateDirty || textureDirty; }
bool isModifiedAfter(const std::chrono::duration<double>& t) const { return t < lastModified; }

void create() noexcept override;

Expand Down Expand Up @@ -112,6 +113,7 @@ class Texture2D : public gfx::Texture2D {
std::shared_ptr<PremultipliedImage> imageData{nullptr};
bool textureDirty{true};
bool samplerStateDirty{true};
std::chrono::duration<double> lastModified{0};

SharedImageAllocation imageAllocation;
vk::ImageLayout imageLayout{vk::ImageLayout::eUndefined};
Expand Down
14 changes: 11 additions & 3 deletions src/mbgl/vulkan/descriptor_set.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
#include <mbgl/vulkan/texture2d.hpp>
#include <mbgl/util/logging.hpp>
#include <mbgl/util/instrumentation.hpp>
#include <mbgl/util/monotonic_timer.hpp>

#include <cassert>
#include <math.h>
Expand Down Expand Up @@ -120,8 +121,8 @@ void DescriptorSet::allocate() {
dirty = std::vector(descriptorSets.size(), true);
}

void DescriptorSet::markDirty(bool value) {
std::fill(dirty.begin(), dirty.end(), value);
void DescriptorSet::markDirty() {
std::fill(dirty.begin(), dirty.end(), true);
}

void DescriptorSet::bind(CommandEncoder& encoder) {
Expand Down Expand Up @@ -186,7 +187,14 @@ void UniformDescriptorSet::update(const gfx::UniformBufferArray& uniforms,
}

ImageDescriptorSet::ImageDescriptorSet(Context& context_)
: DescriptorSet(context_, DescriptorSetType::DrawableImage) {}
: DescriptorSet(context_, DescriptorSetType::DrawableImage),
lastModified(0.0) {}

void ImageDescriptorSet::markDirty() {
DescriptorSet::markDirty();

lastModified = util::MonotonicTimer::now();
}

void ImageDescriptorSet::update(const std::array<gfx::Texture2DPtr, shaders::maxTextureCountPerShader>& textures) {
MLN_TRACE_FUNC();
Expand Down
4 changes: 2 additions & 2 deletions src/mbgl/vulkan/drawable.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -436,8 +436,8 @@ bool Drawable::bindDescriptors(CommandEncoder& encoder) const noexcept {
for (const auto& texture : textures) {
if (!texture) continue;
const auto textureImpl = static_cast<const Texture2D*>(texture.get());
if (textureImpl->isDirty()) {
impl->imageDescriptorSet->markDirty(true);
if (textureImpl->isModifiedAfter(impl->imageDescriptorSet->getLastModified())) {
impl->imageDescriptorSet->markDirty();
break;
}
}
Expand Down
3 changes: 3 additions & 0 deletions src/mbgl/vulkan/texture2d.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -362,7 +362,9 @@ void Texture2D::createTexture() {
context.renderingStats().numCreatedTextures++;
context.renderingStats().numActiveTextures++;
context.renderingStats().memTextures += getDataSize();

textureDirty = false;
lastModified = util::MonotonicTimer::now();
}

void Texture2D::createSampler() {
Expand Down Expand Up @@ -392,6 +394,7 @@ void Texture2D::createSampler() {
sampler = context.getBackend().getDevice()->createSampler(samplerCreateInfo);

samplerStateDirty = false;
lastModified = util::MonotonicTimer::now();
}

void Texture2D::destroyTexture() {
Expand Down

0 comments on commit 4ae61fb

Please sign in to comment.