From b89755675011ef06123602f55513b80e5c9a67a8 Mon Sep 17 00:00:00 2001 From: Eurya0x13 Date: Thu, 26 Nov 2026 14:13:37 +0800 Subject: [PATCH] [Perf] (MG_Backend/DirectGLES, MG_Impl/Pipe, MG_State/GLState): reuse draw state - Reuse the resolved shader record during draw preparation and copies. - Read the tracked draw framebuffer when preparing a clear. - Keep one memo slot hint per VAO and validate it by identity and epoch. --- .../MG_Backend/DirectGLES/DirectGLESBind.cpp | 29 +++++++------------ .../MG_Backend/DirectGLES/DirectGLESCopy.cpp | 14 ++------- .../MG_Backend/DirectGLES/DirectGLESDraw.cpp | 6 ++-- .../DirectGLES/DirectGLESInternal.h | 14 ++++----- .../MG_Backend/DirectGLES/DirectGLESState.cpp | 8 +++-- MobileGL/MG_Impl/Pipe/VertexInputEmit.h | 14 ++++----- .../VertexArrayState/VertexArrayObject.h | 3 ++ 7 files changed, 39 insertions(+), 49 deletions(-) diff --git a/MobileGL/MG_Backend/DirectGLES/DirectGLESBind.cpp b/MobileGL/MG_Backend/DirectGLES/DirectGLESBind.cpp index c741f7b3..320511c2 100644 --- a/MobileGL/MG_Backend/DirectGLES/DirectGLESBind.cpp +++ b/MobileGL/MG_Backend/DirectGLES/DirectGLESBind.cpp @@ -911,7 +911,8 @@ namespace MobileGL::MG_Backend::DirectGLES { BufferImpl::SyncNeccessaryBuffers(currentVAO, vaoTwin, vaoConfigVersion, syncBit & DrawSyncBit::IndexBuffer, syncBit & DrawSyncBit::IndirectBuffer); // The draw's integer arrays and the program's narrow-integer seeds (NarrowIntegerSeeds.h). - IntegerAttributePlan integerPlan = ResolveIntegerAttributePlanForDraw(MG_Pipe::MGPipeApplier().DrawProgram); + const MG_Pipe::MGPipeHandle programCso = MG_Pipe::MGPipeApplier().DrawProgram; + IntegerAttributePlan integerPlan = ResolveIntegerAttributePlanForDraw(programCso); VertexArrayImpl::SyncCurrentVAOFromRecords(vaoTwin, integerPlan); TextureImpl::SyncNeccessaryTextures(textureKeys); // A draw reads and writes through its image units too, so the unit bindings have to be @@ -929,8 +930,8 @@ namespace MobileGL::MG_Backend::DirectGLES { // at all; the remaining uses of it below are the two callees' own, and both have a // handle arm of their own. P5e (pa) finished it: the pull itself is gone on that arm and // `currentProgram` is a null SharedPtr there, which this branch never reaches for. - PrgramImpl::SyncCurrentProgramByHandle(MG_Pipe::MGPipeApplier().DrawProgram, true, &integerPlan); - if (DropRefusedNarrowIntegerSeeds(integerPlan, MG_Pipe::MGPipeApplier().DrawProgram)) + const auto* programRecord = PrgramImpl::SyncCurrentProgramByHandle(programCso, true, &integerPlan); + if (DropRefusedNarrowIntegerSeeds(integerPlan, programCso)) VertexArrayImpl::SyncCurrentVAOFromRecords(vaoTwin, integerPlan); RenderStateImpl::SyncRenderState(); @@ -951,11 +952,11 @@ namespace MobileGL::MG_Backend::DirectGLES { // a conjunction over four callees; this one is served by the record whenever the PROGRAM // family is on the handle arm, which includes the mixed A/B where the texture bits are // off and `currentProgram` above is therefore still a real object. - VertexArrayImpl::SyncCurrentVertexAttributeValues(vaoTwin, MG_Pipe::MGPipeApplier().DrawProgram); + VertexArrayImpl::SyncCurrentVertexAttributeValues(vaoTwin, programCso); BindCurrentTextures(textureKeys, currentProgram); - BindCurrentProgramWithResources(currentProgram, textureKeys, MG_Pipe::MGPipeApplier().DrawProgram); - PushNarrowIntegerWidthsForDraw(integerPlan, MG_Pipe::MGPipeApplier().DrawProgram); + BindCurrentProgramWithResources(currentProgram, textureKeys, programCso, programRecord); + PushNarrowIntegerWidthsForDraw(integerPlan, programCso); // Last: opening the capture span needs the program current and the capture // buffers bound, and ES rejects most binding changes once it is open. @@ -1368,19 +1369,9 @@ namespace MobileGL::MG_Backend::DirectGLES { // (its note says so). The parameter stays for the monolith arm, which is ruling ID-81: that // build keeps this function's frontend text token for token. void BindCurrentProgramWithResources(const SharedPtr& currentProgram, - const TextureImpl::DrawTextureSyncKeys& keys, - MG_Pipe::MGPipeHandle programCso) { - // P5e (pg): THE HANDLE ARM'S GATE, from the record rather than from the object. Hoisted - // out of the `if` below because the whole body's entry condition is "the program linked - // and has SPIR-V", and on this arm both answers are fields of the descriptor - // (Desc.LinkStatus is ID-88's, Desc.SpirvStatus P4a's). - const MG_Pipe::MGPipeShaderCsoRecord* programRecord = nullptr; - programRecord = PrgramImpl::FindShaderCsoRecord(programCso); - if (programRecord != nullptr && (programRecord->Desc.LinkStatus == 0 || programRecord->Desc.SpirvStatus == 0)) { - programRecord = nullptr; - } - const Bool programUsable = programRecord != nullptr; - if (programUsable) { + const TextureImpl::DrawTextureSyncKeys& keys, MG_Pipe::MGPipeHandle programCso, + const MG_Pipe::MGPipeShaderCsoRecord* programRecord) { + if (programRecord != nullptr) { #ifdef TRACY_ENABLE ZoneScopedNC("BindCurrentProgram", TRACY_ZONECOLOR_BACKEND); #endif diff --git a/MobileGL/MG_Backend/DirectGLES/DirectGLESCopy.cpp b/MobileGL/MG_Backend/DirectGLES/DirectGLESCopy.cpp index 35f367eb..99d567c2 100644 --- a/MobileGL/MG_Backend/DirectGLES/DirectGLESCopy.cpp +++ b/MobileGL/MG_Backend/DirectGLES/DirectGLESCopy.cpp @@ -107,16 +107,8 @@ namespace MobileGL::MG_Backend::DirectGLES { // MGPipeApplier().DispatchProgram, which set_dispatch_program carried. A compute-only // pipeline has no draw program at all, which is why the two applier fields are separate. const MG_Pipe::MGPipeHandle dispatchCso = MG_Pipe::MGPipeApplier().DispatchProgram; - PrgramImpl::SyncCurrentProgramByHandle(dispatchCso, false); - - // The same gate as the frontend one below, from the record: a dispatch of a program - // the frontend reports unlinked, or one whose SPIR-V never arrived, binds nothing. - const auto* const record = PrgramImpl::FindShaderCsoRecord(dispatchCso); - if (record == nullptr || record->Desc.LinkStatus == 0 || record->Desc.SpirvStatus == 0) { - g_GLESFuncs.glUseProgram(0); - PrgramImpl::g_lastUsedBackendProgramId = 0; - return; - } + const auto* programRecord = PrgramImpl::SyncCurrentProgramByHandle(dispatchCso, false); + if (programRecord == nullptr) return; // Compute shaders sample textures through the same unit bindings as draws // (e.g. Flywheel's depth-pyramid downsample reads the depth attachment on @@ -125,7 +117,7 @@ namespace MobileGL::MG_Backend::DirectGLES { // Compute programs need the same per-program resource sync as draws: // uniform-block bindings and sampler units only exist through the API // because layout(binding) is stripped from the transpiled ESSL. - BindCurrentProgramWithResources(currentProgram, textureKeys, dispatchCso); + BindCurrentProgramWithResources(currentProgram, textureKeys, dispatchCso, programRecord); } Bool UpdateTextureBindingAtTarget(GLenum target) { diff --git a/MobileGL/MG_Backend/DirectGLES/DirectGLESDraw.cpp b/MobileGL/MG_Backend/DirectGLES/DirectGLESDraw.cpp index cf078b79..4cca0bc3 100644 --- a/MobileGL/MG_Backend/DirectGLES/DirectGLESDraw.cpp +++ b/MobileGL/MG_Backend/DirectGLES/DirectGLESDraw.cpp @@ -582,8 +582,10 @@ namespace MobileGL::MG_Backend::DirectGLES { const Bool widenedColorClear = widenedDrawBuffers != 0 && FramebufferImpl::g_integerColorDrawBufferMask == 0; GLint clearDrawFbo = 0; - g_GLESFuncs.glGetIntegerv(GL_DRAW_FRAMEBUFFER_BINDING, &clearDrawFbo); - if ((outOfRange || widenedColorClear) && clearDrawFbo != 0) { + if (outOfRange || widenedColorClear) { + g_GLESFuncs.glGetIntegerv(GL_DRAW_FRAMEBUFFER_BINDING, &clearDrawFbo); + } + if (clearDrawFbo != 0) { GLint maxDrawBuffers = 0; GLint clearedCount = 0; GLint firstDb = -1; diff --git a/MobileGL/MG_Backend/DirectGLES/DirectGLESInternal.h b/MobileGL/MG_Backend/DirectGLES/DirectGLESInternal.h index 6bb55873..22a9fe60 100644 --- a/MobileGL/MG_Backend/DirectGLES/DirectGLESInternal.h +++ b/MobileGL/MG_Backend/DirectGLES/DirectGLESInternal.h @@ -387,9 +387,9 @@ namespace MobileGL::MG_Backend::DirectGLES { const MG_Pipe::MGPipeShaderCsoRecord* ResolveGlobalConstantsRecordForHandle(MG_Pipe::MGPipeHandle handle, Uint uboSize); - // `plan` (a draw's) arms the program's narrow-integer seeds when it needs them. - void SyncCurrentProgramByHandle(MG_Pipe::MGPipeHandle cso, Bool forDraw, - const IntegerAttributePlan* plan = nullptr); + // Returns the validated record for resource binding in the same draw or dispatch. + const MG_Pipe::MGPipeShaderCsoRecord* SyncCurrentProgramByHandle(MG_Pipe::MGPipeHandle cso, Bool forDraw, + const IntegerAttributePlan* plan = nullptr); } // namespace PrgramImpl // Defined with the surface registry, below. @@ -398,8 +398,8 @@ namespace MobileGL::MG_Backend::DirectGLES { void BindCurrentFBO(FramebufferTarget target); void BindCurrentProgramWithResources(const SharedPtr& currentProgram, - const TextureImpl::DrawTextureSyncKeys& keys, - MG_Pipe::MGPipeHandle programCso); + const TextureImpl::DrawTextureSyncKeys& keys, MG_Pipe::MGPipeHandle programCso, + const MG_Pipe::MGPipeShaderCsoRecord* programRecord); void BindCurrentTextures(const TextureImpl::DrawTextureSyncKeys& keys, const SharedPtr& currentProgram); @@ -504,8 +504,8 @@ namespace MobileGL::MG_Backend::DirectGLES { void BindCurrentTextures(); void BindCurrentProgramWithResources(const SharedPtr& currentProgram, - const TextureImpl::DrawTextureSyncKeys& keys, - MG_Pipe::MGPipeHandle programCso); + const TextureImpl::DrawTextureSyncKeys& keys, MG_Pipe::MGPipeHandle programCso, + const MG_Pipe::MGPipeShaderCsoRecord* programRecord); PrgramImpl::BackendProgramObjectImpl* GetCurrentBackendProgram(); diff --git a/MobileGL/MG_Backend/DirectGLES/DirectGLESState.cpp b/MobileGL/MG_Backend/DirectGLES/DirectGLESState.cpp index bd5272d8..cf416f4a 100644 --- a/MobileGL/MG_Backend/DirectGLES/DirectGLESState.cpp +++ b/MobileGL/MG_Backend/DirectGLES/DirectGLESState.cpp @@ -1434,7 +1434,8 @@ namespace MobileGL::MG_Backend::DirectGLES { // and the clamp masks, the broadcast count, ImageUnitFormatsStillMatch and the three // patch clauses are unchanged - they are backend globals, fb's row and VALUE rows of the // residual block, none of which this family owns. - void SyncCurrentProgramByHandle(MG_Pipe::MGPipeHandle cso, Bool forDraw, const IntegerAttributePlan* plan) { + const MG_Pipe::MGPipeShaderCsoRecord* SyncCurrentProgramByHandle(MG_Pipe::MGPipeHandle cso, Bool forDraw, + const IntegerAttributePlan* plan) { #ifdef TRACY_ENABLE ZoneScopedC(TRACY_ZONECOLOR_BACKEND); #endif @@ -1453,7 +1454,7 @@ namespace MobileGL::MG_Backend::DirectGLES { if (record == nullptr || record->Desc.LinkStatus == 0 || record->Desc.SpirvStatus == 0) { g_GLESFuncs.glUseProgram(0); g_lastUsedBackendProgramId = 0; - return; + return nullptr; } // REFUSED at a draw, record-only at a dispatch: see the policy's own note. ResolveFragColorBroadcastCount(forDraw ? BroadcastCountPolicy::RefuseFallback @@ -1466,7 +1467,7 @@ namespace MobileGL::MG_Backend::DirectGLES { // than a draw with somebody else's shader. g_GLESFuncs.glUseProgram(0); g_lastUsedBackendProgramId = 0; - return; + return record; } // Sticky for the twin's lifetime: a draw that needs the seeds rebuilds the program once. if (plan != nullptr && plan->NeedsSeeds()) twin->NarrowIntegerSeeds().wanted = true; @@ -1488,6 +1489,7 @@ namespace MobileGL::MG_Backend::DirectGLES { } g_currentDrawProgramHandle = cso; g_currentDrawBackendProgram = twin; + return record; } } // namespace PrgramImpl } // namespace MobileGL::MG_Backend::DirectGLES diff --git a/MobileGL/MG_Impl/Pipe/VertexInputEmit.h b/MobileGL/MG_Impl/Pipe/VertexInputEmit.h index c63ada98..388178f5 100644 --- a/MobileGL/MG_Impl/Pipe/VertexInputEmit.h +++ b/MobileGL/MG_Impl/Pipe/VertexInputEmit.h @@ -487,18 +487,19 @@ namespace MobileGL::MG_Pipe { VaoMemo& MemoFor(const VertexArrayObject& vao) { const Uint64 lifetimeId = vao.GetLifetimeId(); const Uint64 epoch = MGPipeSlots().ResetEpoch(); - // The three emitters of one validate ask for the same array: the last answer's index, - // believed only while that entry still holds this id (a grow or an erase moves entries). - if (m_lastMemoIndex < m_vaoMemo.size() && m_vaoMemo[m_lastMemoIndex].LifetimeId == lifetimeId && - m_vaoMemo[m_lastMemoIndex].SlotEpoch == epoch) - return m_vaoMemo[m_lastMemoIndex]; + // A per-array hint survives switching arrays. Growth and erasure can move entries, + // so the slot is only a candidate until both identity and the allocator epoch match. + SizeT& hint = vao.PipeVertexInputMemoHint(); + if (hint < m_vaoMemo.size() && m_vaoMemo[hint].LifetimeId == lifetimeId && + m_vaoMemo[hint].SlotEpoch == epoch) + return m_vaoMemo[hint]; if (m_vaoMemo.empty() || (m_vaoMemoUsed + 1) * 2 > m_vaoMemo.size()) GrowVaoMemo(); const SizeT mask = m_vaoMemo.size() - 1; SizeT i = VaoMemoHome(lifetimeId, mask); while (m_vaoMemo[i].LifetimeId != 0 && m_vaoMemo[i].LifetimeId != lifetimeId) i = (i + 1) & mask; VaoMemo& memo = m_vaoMemo[i]; - m_lastMemoIndex = i; + hint = i; if (memo.LifetimeId == 0) ++m_vaoMemoUsed; if (memo.LifetimeId != lifetimeId || memo.SlotEpoch != epoch) { memo = VaoMemo{}; @@ -527,7 +528,6 @@ namespace MobileGL::MG_Pipe { } Vector m_vaoMemo; SizeT m_vaoMemoUsed = 0; - SizeT m_lastMemoIndex = ~SizeT{0}; public: // The VAO died (MGPipeEmitVertexElementsDestroyAndFree): its entry goes, by backward-shift diff --git a/MobileGL/MG_State/GLState/VertexArrayState/VertexArrayObject.h b/MobileGL/MG_State/GLState/VertexArrayState/VertexArrayObject.h index 97b176cb..a6143268 100644 --- a/MobileGL/MG_State/GLState/VertexArrayState/VertexArrayObject.h +++ b/MobileGL/MG_State/GLState/VertexArrayState/VertexArrayObject.h @@ -69,6 +69,8 @@ namespace MobileGL { // DirectVulkan's VulkanRenderer). Uint64 GetLifetimeId() const { return m_lifetimeId; } + SizeT& PipeVertexInputMemoHint() const { return m_pipeVertexInputMemoHint; } + void SetAttributeDivisor(Uint index, Uint divisor); Uint GetAttributeDivisor(Uint index) const; @@ -134,6 +136,7 @@ namespace MobileGL { const Uint m_externalIndex = 0; const Uint64 m_lifetimeId = AllocateLifetimeId(); + mutable SizeT m_pipeVertexInputMemoHint = ~SizeT{0}; Array m_attributes; Array m_attributeVersions; BindingSlot m_indexBufferBindingSlot;