mirror of
https://github.com/MobileGL-Dev/MobileGL
synced 2026-11-26 19:22:16 +09:00
[Fix] (MG_Backend/DirectGLES): avoid PBO updates to sampled offscreen textures
- Track sampled storage by native context and draw batch. - Use the existing client upload for in-place edits to sampled storage.
This commit is contained in:
1 parent
10deb9cea4
commit
639ae0f636
6 files changed
+124
-8
No files matched your search
@@ -639,6 +639,7 @@ namespace MobileGL::MG_Backend::DirectGLES {
|
||||
} else if (g_EGLFuncs.eglSwapBuffers != nullptr) {
|
||||
mglSwapped = g_EGLFuncs.eglSwapBuffers(g_Display, drawSurface);
|
||||
}
|
||||
if (mglSwapped == EGL_TRUE) TextureImpl::EndDrawTextureBatch();
|
||||
NoteDrawSurfaceMoved(); // a window surface takes a new size at its swap
|
||||
if (drawSurface != EGL_NO_SURFACE) {
|
||||
const std::lock_guard<std::mutex> lock(g_nativeRegistryMutex);
|
||||
|
||||
@@ -7,9 +7,50 @@
|
||||
// End of Source File Header
|
||||
|
||||
#include "DirectGLESInternal.h"
|
||||
#include "DrawTextureReads.h"
|
||||
|
||||
namespace MobileGL::MG_Backend::DirectGLES {
|
||||
namespace TextureImpl {
|
||||
namespace {
|
||||
PerNativeContext<DrawTextureReads> g_drawTextureReads;
|
||||
} // namespace
|
||||
|
||||
void NoteDrawTextureFramebuffer(Uint framebuffer) {
|
||||
if (CurrentNativeContextSerial() != 0) g_drawTextureReads.Current().BindFramebuffer(framebuffer);
|
||||
}
|
||||
|
||||
void DeleteDrawTextureFramebuffer(Uint framebuffer) {
|
||||
if (CurrentNativeContextSerial() != 0) g_drawTextureReads.Current().DeleteFramebuffer(framebuffer);
|
||||
}
|
||||
|
||||
void EndDrawTextureBatch() {
|
||||
if (CurrentNativeContextSerial() != 0) g_drawTextureReads.Current().EndBatch();
|
||||
}
|
||||
|
||||
Bool WasTextureReadInDrawBatch(MG_Pipe::MGPipeHandle storage) {
|
||||
return CurrentNativeContextSerial() != 0 && g_drawTextureReads.Current().WasRead(storage.Slot, storage.Gen);
|
||||
}
|
||||
|
||||
void NoteDrawTextureReads() {
|
||||
if (CurrentNativeContextSerial() == 0) return;
|
||||
auto& reads = g_drawTextureReads.Current();
|
||||
const auto& state = MG_Pipe::MGPipeApplier();
|
||||
if (!reads.NeedsSamplerWalk(state.ContextSerial, state.SamplerViewsSerial)) return;
|
||||
|
||||
const SizeT end = std::min<SizeT>(static_cast<SizeT>(state.SamplerViewStart) + state.SamplerViewCount,
|
||||
state.BoundSamplerViews.size());
|
||||
for (SizeT unit = state.SamplerViewStart; unit < end; ++unit) {
|
||||
const auto* texture = PipeTextureRecordForHandle(state.BoundSamplerViews[unit].Texture);
|
||||
if (!texture) continue;
|
||||
// A sampler view and an update to its parent address the same texture storage.
|
||||
if (const auto* storage = PipeTextureStorageRecordForRecord(*texture)) {
|
||||
const auto handle = storage->Desc.Resource;
|
||||
reads.NoteRead(handle.Slot, handle.Gen);
|
||||
}
|
||||
}
|
||||
reads.RecordSamplerWalk(state.ContextSerial, state.SamplerViewsSerial);
|
||||
}
|
||||
|
||||
// Identity snapshot of what one texture unit has bound: the object in every binding
|
||||
// slot plus the unit's sampler object. This - not the context's texture bind
|
||||
// generation - is what the per-draw texture memos key on: the generation also bumps
|
||||
|
||||
@@ -0,0 +1,66 @@
|
||||
// MobileGL - MobileGL/MG_Backend/DirectGLES/DrawTextureReads.h
|
||||
// Copyright (c) 2025-2026 MobileGL-Dev
|
||||
// Licensed under the GNU Lesser General Public License v3.0:
|
||||
// https://www.gnu.org/licenses/gpl-3.0.txt
|
||||
// https://www.gnu.org/licenses/lgpl-3.0.txt
|
||||
// SPDX-License-Identifier: LGPL-3.0-only
|
||||
// End of Source File Header
|
||||
|
||||
#pragma once
|
||||
|
||||
#include <cstdint>
|
||||
#include <vector>
|
||||
|
||||
namespace MobileGL::MG_Backend::DirectGLES::TextureImpl {
|
||||
// One instance per native context. Slot generations distinguish reused texture names,
|
||||
// and batch stamps let us discard a batch without walking its texture slots.
|
||||
class DrawTextureReads {
|
||||
public:
|
||||
void BindFramebuffer(std::uint32_t framebuffer) {
|
||||
if (m_drawFramebuffer == framebuffer) return;
|
||||
m_drawFramebuffer = framebuffer;
|
||||
EndBatch();
|
||||
}
|
||||
|
||||
void DeleteFramebuffer(std::uint32_t framebuffer) {
|
||||
if (framebuffer == m_drawFramebuffer) BindFramebuffer(0);
|
||||
}
|
||||
|
||||
void EndBatch() {
|
||||
m_recordedSamplers = false;
|
||||
if (++m_batch == 0) {
|
||||
m_reads.clear();
|
||||
m_batch = 1;
|
||||
}
|
||||
}
|
||||
|
||||
bool NeedsSamplerWalk(std::uint64_t context, std::uint64_t views) const {
|
||||
return m_drawFramebuffer != 0 && (!m_recordedSamplers || context != m_context || views != m_views);
|
||||
}
|
||||
|
||||
void RecordSamplerWalk(std::uint64_t context, std::uint64_t views) {
|
||||
m_recordedSamplers = true;
|
||||
m_context = context;
|
||||
m_views = views;
|
||||
}
|
||||
|
||||
void NoteRead(std::uint32_t slot, std::uint32_t generation) {
|
||||
if (slot >= m_reads.size()) m_reads.resize(static_cast<std::size_t>(slot) + 1);
|
||||
m_reads[slot] = Stamp(generation);
|
||||
}
|
||||
|
||||
bool WasRead(std::uint32_t slot, std::uint32_t generation) const {
|
||||
return m_drawFramebuffer != 0 && slot < m_reads.size() && m_reads[slot] == Stamp(generation);
|
||||
}
|
||||
|
||||
private:
|
||||
std::uint64_t Stamp(std::uint32_t generation) const { return (std::uint64_t{m_batch} << 32) | generation; }
|
||||
|
||||
std::uint32_t m_drawFramebuffer = 0;
|
||||
std::uint32_t m_batch = 1;
|
||||
bool m_recordedSamplers = false;
|
||||
std::uint64_t m_context = 0;
|
||||
std::uint64_t m_views = 0;
|
||||
std::vector<std::uint64_t> m_reads;
|
||||
};
|
||||
} // namespace MobileGL::MG_Backend::DirectGLES::TextureImpl
|
||||
@@ -272,11 +272,16 @@ namespace MobileGL::MG_Backend::DirectGLES {
|
||||
// re-pushes index 0's state. Takes the count so it can do nothing at all in the common case.
|
||||
void EndViewportRoutingPasses(Uint passCount);
|
||||
|
||||
namespace TextureImpl {
|
||||
void NoteDrawTextureReads();
|
||||
} // namespace TextureImpl
|
||||
|
||||
// Issue one draw, replayed once per viewport-routing pass. Every application-visible draw
|
||||
// entry point wraps its native glDraw* call in this; the internal blit and clear helpers
|
||||
// deliberately do not, because they bind their own programs, which never route.
|
||||
template <typename IssueDraw>
|
||||
inline void ForEachViewportRoutingPass(IssueDraw&& issue) {
|
||||
TextureImpl::NoteDrawTextureReads();
|
||||
const Uint passCount = BeginViewportRoutingPasses();
|
||||
for (Uint pass = 0; pass < passCount; ++pass) {
|
||||
if (passCount > 1) {
|
||||
@@ -2102,6 +2107,10 @@ namespace MobileGL::MG_Backend::DirectGLES {
|
||||
// issued since the last one. Sampled access from a draw does not take it: that is the
|
||||
// order every driver keeps on its own, and it is the per-draw hot path.
|
||||
void NoteInPlaceTextureUpload();
|
||||
void NoteDrawTextureFramebuffer(Uint framebuffer);
|
||||
void DeleteDrawTextureFramebuffer(Uint framebuffer);
|
||||
void EndDrawTextureBatch();
|
||||
Bool WasTextureReadInDrawBatch(MG_Pipe::MGPipeHandle storage);
|
||||
// The barrier bits owed for the uploads noted since the last call (0 when none), and
|
||||
// the note cleared: the caller issues them, folded into any barrier of its own.
|
||||
GLbitfield TakeInPlaceTextureUploadBarrierBits();
|
||||
|
||||
@@ -46,6 +46,7 @@ namespace MobileGL::MG_Backend::DirectGLES {
|
||||
NoteFramebufferIdDeleted(m_backendFBOId);
|
||||
if (m_contextGeneration == g_backendContextGeneration && g_GLESFuncs.glDeleteFramebuffers) {
|
||||
g_GLESFuncs.glDeleteFramebuffers(1, &m_backendFBOId);
|
||||
TextureImpl::DeleteDrawTextureFramebuffer(m_backendFBOId);
|
||||
}
|
||||
m_backendFBOId = 0;
|
||||
}
|
||||
@@ -83,6 +84,7 @@ namespace MobileGL::MG_Backend::DirectGLES {
|
||||
if (bindsDraw) {
|
||||
g_driverFBOBindings[drawIdx] = id;
|
||||
g_driverFBOBindingKnown[drawIdx] = true;
|
||||
if (!drawMatches) TextureImpl::NoteDrawTextureFramebuffer(id);
|
||||
}
|
||||
if (bindsRead) {
|
||||
g_driverFBOBindings[readIdx] = id;
|
||||
|
||||
@@ -1665,6 +1665,8 @@ namespace MobileGL::MG_Backend::DirectGLES {
|
||||
MGLOG_D("Packed texture upload rejected for texture %u", m_backendTextureId);
|
||||
}
|
||||
}
|
||||
// Update sampled storage directly to avoid stalling the current offscreen batch on a PBO.
|
||||
const Bool useClientUnpack = stableStorage && WasTextureReadInDrawBatch(pushedRes);
|
||||
// Per-level scratch of the exact-footprint path, hoisted to reuse capacity.
|
||||
Vector<MG_State::GLState::MipmapDirtyRegion> dirtyFootprint;
|
||||
Vector<UnpackStagingBlock> stagingBlockOverflow;
|
||||
@@ -1995,13 +1997,8 @@ namespace MobileGL::MG_Backend::DirectGLES {
|
||||
static_cast<Uint16>(uploadTarget), static_cast<Uint16>(level))) &&
|
||||
SplitMipUpload(*pendingUpload, texelSize, dirtyRects);
|
||||
if (splitBoxUpload) dirtyRectCount = 2;
|
||||
// Everything above - the union box, the ring's one-box rule, the rect
|
||||
// list with its merges, fold and 3/4 cut-off - may upload shadow texels
|
||||
// the client did not write, which is only harmless while the driver
|
||||
// still holds those texels as they were last uploaded. Once it may hold
|
||||
// GPU writes, the level goes up as exactly the texels written, one box
|
||||
// (one ring job) each, whatever that costs: more boxes, and a union box
|
||||
// that spans the whole level no longer means a whole-level upload.
|
||||
// A union or split can include untouched texels while the shadow is authoritative.
|
||||
// Once the GPU may have written the texture, upload only the exact client footprint.
|
||||
const MG_State::GLState::MipmapDirtyRegion* uploadRects = dirtyRects;
|
||||
SizeT uploadRectCount = dirtyRectCount;
|
||||
if (m_driverMayHoldGpuWrites && regionsAddressable) {
|
||||
@@ -2094,7 +2091,7 @@ namespace MobileGL::MG_Backend::DirectGLES {
|
||||
UnpackStagingBlock stagingBlockArray[MG_State::GLState::MipmapStorage::kMaxDirtyRects];
|
||||
UnpackStagingBlock* stagingBlocks = stagingBlockArray;
|
||||
SizeT stagingBlockCount = 0;
|
||||
if (!ringUsable) {
|
||||
if (!ringUsable || useClientUnpack) {
|
||||
// Nothing to plan: every branch below keeps the client
|
||||
// pointer and its ROW_LENGTH striding, unchanged.
|
||||
} else if (perRectUpload) {
|
||||
|
||||
Reference in new issue
Block a user