From 4ae61fb9dc306a01f2f75a9696b3c9dc7ae05e68 Mon Sep 17 00:00:00 2001 From: Adrian Cojocaru Date: Tue, 21 Jan 2025 21:26:07 +0200 Subject: [PATCH] Use timestamps for image descriptor updates (#3152) --- include/mbgl/vulkan/descriptor_set.hpp | 8 +++++++- include/mbgl/vulkan/texture2d.hpp | 2 ++ src/mbgl/vulkan/descriptor_set.cpp | 14 +++++++++++--- src/mbgl/vulkan/drawable.cpp | 4 ++-- src/mbgl/vulkan/texture2d.cpp | 3 +++ 5 files changed, 25 insertions(+), 6 deletions(-) diff --git a/include/mbgl/vulkan/descriptor_set.hpp b/include/mbgl/vulkan/descriptor_set.hpp index b1c4619c5..1e3eb3f94 100644 --- a/include/mbgl/vulkan/descriptor_set.hpp +++ b/include/mbgl/vulkan/descriptor_set.hpp @@ -60,7 +60,7 @@ public: void allocate(); - void markDirty(bool value = true); + virtual void markDirty(); void bind(CommandEncoder& encoder); protected: @@ -91,7 +91,13 @@ public: ImageDescriptorSet(Context& context_); virtual ~ImageDescriptorSet() = default; + void markDirty() override; + const std::chrono::duration& getLastModified() const { return lastModified; } + void update(const std::array& textures); + +protected: + std::chrono::duration lastModified; }; } // namespace vulkan diff --git a/include/mbgl/vulkan/texture2d.hpp b/include/mbgl/vulkan/texture2d.hpp index b0aaef237..a67b40112 100644 --- a/include/mbgl/vulkan/texture2d.hpp +++ b/include/mbgl/vulkan/texture2d.hpp @@ -60,6 +60,7 @@ public: size_t numChannels() const noexcept override; bool isDirty() const { return samplerStateDirty || textureDirty; } + bool isModifiedAfter(const std::chrono::duration& t) const { return t < lastModified; } void create() noexcept override; @@ -112,6 +113,7 @@ private: std::shared_ptr imageData{nullptr}; bool textureDirty{true}; bool samplerStateDirty{true}; + std::chrono::duration lastModified{0}; SharedImageAllocation imageAllocation; vk::ImageLayout imageLayout{vk::ImageLayout::eUndefined}; diff --git a/src/mbgl/vulkan/descriptor_set.cpp b/src/mbgl/vulkan/descriptor_set.cpp index 488e94072..ae30ba05e 100644 --- a/src/mbgl/vulkan/descriptor_set.cpp +++ b/src/mbgl/vulkan/descriptor_set.cpp @@ -5,6 +5,7 @@ #include #include #include +#include #include #include @@ -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) { @@ -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& textures) { MLN_TRACE_FUNC(); diff --git a/src/mbgl/vulkan/drawable.cpp b/src/mbgl/vulkan/drawable.cpp index e3b0cf45f..06f52581e 100644 --- a/src/mbgl/vulkan/drawable.cpp +++ b/src/mbgl/vulkan/drawable.cpp @@ -436,8 +436,8 @@ bool Drawable::bindDescriptors(CommandEncoder& encoder) const noexcept { for (const auto& texture : textures) { if (!texture) continue; const auto textureImpl = static_cast(texture.get()); - if (textureImpl->isDirty()) { - impl->imageDescriptorSet->markDirty(true); + if (textureImpl->isModifiedAfter(impl->imageDescriptorSet->getLastModified())) { + impl->imageDescriptorSet->markDirty(); break; } } diff --git a/src/mbgl/vulkan/texture2d.cpp b/src/mbgl/vulkan/texture2d.cpp index 7c31f5330..8f2da1495 100644 --- a/src/mbgl/vulkan/texture2d.cpp +++ b/src/mbgl/vulkan/texture2d.cpp @@ -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() { @@ -392,6 +394,7 @@ void Texture2D::createSampler() { sampler = context.getBackend().getDevice()->createSampler(samplerCreateInfo); samplerStateDirty = false; + lastModified = util::MonotonicTimer::now(); } void Texture2D::destroyTexture() {