From e8e15219722021648d00b1901765307d51996a41 Mon Sep 17 00:00:00 2001 From: Swung0x48 Date: Sun, 12 Jul 2026 18:40:48 -0400 Subject: [PATCH] [Perf] (MG_Backend/DirectVulkan): skip cross-draw re-sync of unchanged textures via a content-version early-out; SyncTextureAndGetDescriptor 8.9%->2.4% --- .../DirectVulkan/Renderer/VkTextureManager.cpp | 14 ++++++++++++++ .../DirectVulkan/Renderer/VkTextureManager.h | 5 +++++ .../GLState/TextureState/TextureObject.cpp | 7 +++++++ .../MG_State/GLState/TextureState/TextureObject.h | 8 ++++++++ .../GLState/TextureState/TextureObject2DCube.cpp | 3 +++ 5 files changed, 37 insertions(+) diff --git a/MobileGL/MG_Backend/DirectVulkan/Renderer/VkTextureManager.cpp b/MobileGL/MG_Backend/DirectVulkan/Renderer/VkTextureManager.cpp index 5ce20443..974f94ad 100644 --- a/MobileGL/MG_Backend/DirectVulkan/Renderer/VkTextureManager.cpp +++ b/MobileGL/MG_Backend/DirectVulkan/Renderer/VkTextureManager.cpp @@ -1065,6 +1065,18 @@ namespace MobileGL::MG_Backend::DirectVulkan { Bool VkTextureManager::SyncTexture(MG_State::GLState::ITextureObject &texture, TextureResource &outResource) { + // Cross-draw fast path: if the resource is already built and neither the texture's + // pixel content (bumped in MarkStorageDirty) nor its params changed since the last + // sync, there is nothing to re-check or re-upload - skip CheckMipmapCompleteness, + // SyncTextureResource, SyncTextureViews and the per-level dirty scan. Layout is + // maintained separately by the transition path, so the resource still reflects truth. + const Uint64 syncingContentVersion = texture.GetContentVersion(); + if (outResource.image != VK_NULL_HANDLE && + outResource.syncedContentVersion == syncingContentVersion && + outResource.syncedTextureParamsVersion == texture.GetTextureParamsVersion()) { + return true; + } + TextureUploadTarget uploadTarget = TextureUploadTarget::Unknown; IntVec3 texelSize{0, 0, 0}; SizeT byteSize = 0; @@ -1111,6 +1123,7 @@ namespace MobileGL::MG_Backend::DirectVulkan { DumpTextureSyncStats(texture.GetExternalIndex(), texture.GetFormat(), uploadTarget, mipLevelCount, texelSize, byteSize, hasDirtyMipLevel); if (!hasDirtyMipLevel) { + outResource.syncedContentVersion = syncingContentVersion; return true; } @@ -1118,6 +1131,7 @@ namespace MobileGL::MG_Backend::DirectVulkan { MGLOG_D("%s: UploadDirtyMipLevels failed", __func__); return false; } + outResource.syncedContentVersion = syncingContentVersion; return true; } diff --git a/MobileGL/MG_Backend/DirectVulkan/Renderer/VkTextureManager.h b/MobileGL/MG_Backend/DirectVulkan/Renderer/VkTextureManager.h index a972dc12..77ff064d 100644 --- a/MobileGL/MG_Backend/DirectVulkan/Renderer/VkTextureManager.h +++ b/MobileGL/MG_Backend/DirectVulkan/Renderer/VkTextureManager.h @@ -97,6 +97,9 @@ public: VkImageViewType viewType = VK_IMAGE_VIEW_TYPE_2D; VkSampleCountFlagBits sampleCount = VK_SAMPLE_COUNT_1_BIT; Uint16 syncedTextureParamsVersion = 0; + // Snapshot of ITextureObject::GetContentVersion() at the last successful sync; + // lets SyncTexture skip the whole re-check/re-upload when content is unchanged. + Uint64 syncedContentVersion = 0; TextureResource() = default; TextureResource(const TextureResource&) = delete; @@ -120,6 +123,7 @@ public: std::swap(this->viewType, that.viewType); std::swap(this->sampleCount, that.sampleCount); std::swap(this->syncedTextureParamsVersion, that.syncedTextureParamsVersion); + std::swap(this->syncedContentVersion, that.syncedContentVersion); } void Reset() { @@ -166,6 +170,7 @@ public: viewType = VK_IMAGE_VIEW_TYPE_2D; sampleCount = VK_SAMPLE_COUNT_1_BIT; syncedTextureParamsVersion = 0; + syncedContentVersion = 0; } ~TextureResource() { diff --git a/MobileGL/MG_State/GLState/TextureState/TextureObject.cpp b/MobileGL/MG_State/GLState/TextureState/TextureObject.cpp index 4aae56a0..3b744a16 100644 --- a/MobileGL/MG_State/GLState/TextureState/TextureObject.cpp +++ b/MobileGL/MG_State/GLState/TextureState/TextureObject.cpp @@ -207,6 +207,10 @@ namespace MobileGL { return m_textureParamsVersion; } + Uint64 TextureObjectBase::GetContentVersion() const { + return m_contentVersion; + } + Int TextureObjectBase::GetSamples() const { return m_samples; } @@ -257,6 +261,9 @@ namespace MobileGL { void TextureObjectWithOneMipmap::MarkStorageDirty(TextureUploadTarget uploadTarget, Uint mipmapLevel, Bool dirty) { + if (dirty) { + ++m_contentVersion; + } m_textureStorage.MarkDirty(GetIndexOfTextureUploadTarget(uploadTarget), mipmapLevel, dirty); } diff --git a/MobileGL/MG_State/GLState/TextureState/TextureObject.h b/MobileGL/MG_State/GLState/TextureState/TextureObject.h index 4b753cd9..7294ecf9 100644 --- a/MobileGL/MG_State/GLState/TextureState/TextureObject.h +++ b/MobileGL/MG_State/GLState/TextureState/TextureObject.h @@ -47,6 +47,10 @@ namespace MobileGL::MG_State::GLState { virtual Uint GetImmutableLevels() const = 0; virtual void SetImmutableLevels(Uint levels) = 0; virtual Uint16 GetTextureParamsVersion() const = 0; + // Monotonic counter bumped on every CPU-side pixel mutation (see MarkStorageDirty). + // Backends compare it against a per-resource snapshot to skip re-syncing unchanged + // textures across draws (e.g. the block atlas bound across a whole terrain batch). + virtual Uint64 GetContentVersion() const = 0; virtual Int GetSamples() const = 0; virtual void SetSamples(Int samples) = 0; virtual Bool HasFixedSampleLocations() const = 0; @@ -86,6 +90,7 @@ namespace MobileGL::MG_State::GLState { Uint GetImmutableLevels() const override; void SetImmutableLevels(Uint levels) override; Uint16 GetTextureParamsVersion() const override; + Uint64 GetContentVersion() const override; Int GetSamples() const override; void SetSamples(Int samples) override; Bool HasFixedSampleLocations() const override; @@ -108,6 +113,9 @@ namespace MobileGL::MG_State::GLState { UintVec2 m_levelRange = {0, 1000}; Uint m_immutableLevels = 0; Uint16 m_textureParamsVersion = 0; + // Starts at 1 so a freshly-created backend resource (snapshot 0) never spuriously + // matches before its first sync. Bumped only on dirty=true in MarkStorageDirty. + Uint64 m_contentVersion = 1; Int m_samples = 0; Bool m_fixedSampleLocations = true; }; diff --git a/MobileGL/MG_State/GLState/TextureState/TextureObject2DCube.cpp b/MobileGL/MG_State/GLState/TextureState/TextureObject2DCube.cpp index 84385810..cf0b77f2 100644 --- a/MobileGL/MG_State/GLState/TextureState/TextureObject2DCube.cpp +++ b/MobileGL/MG_State/GLState/TextureState/TextureObject2DCube.cpp @@ -41,6 +41,9 @@ namespace MobileGL { } void TextureObject2DCube::MarkStorageDirty(TextureUploadTarget uploadTarget, Uint mipmapLevel, bool dirty) { + if (dirty) { + ++m_contentVersion; + } m_textureStorage.MarkDirty(GetIndexOfTextureUploadTarget(uploadTarget), mipmapLevel, dirty); }