mirror of
https://github.com/MobileGL-Dev/MobileGL
synced 2026-11-16 22:43:24 +09:00
[Perf] (Espryt): drain small partial writes as a whole-store orphan map
This commit is contained in:
@@ -1511,6 +1511,35 @@ namespace MobileGL::MG_Backend::DirectGLES {
|
||||
// this one against a sha pinned at 3e298c9a. Neither ladder may drift, and neither
|
||||
// may drift AWAY FROM THE OTHER without the gate saying so.
|
||||
|
||||
// P15: the small-store write rename (WholeStoreRenameApplies, Managers.h), applied to the
|
||||
// queue BEFORE the drain takes it. The ladder above is pinned and is not touched: it is
|
||||
// handed one whole-store range, which its own tier 1 already answers with the
|
||||
// whole-buffer orphan-map. Draw path only - the readback drains keep their exact ranges.
|
||||
void RenameSmallStoreForDrain(GLESBufferResource& resource, const Uint8* hostBase, SizeT frontendSize) {
|
||||
const SizeT store = resource.storageSize;
|
||||
const Bool mapUsable = !MG_Config::Features.EsprytDisableInvalidateFlush &&
|
||||
g_GLESFuncs.glMapBufferRange && g_GLESFuncs.glUnmapBuffer;
|
||||
const Bool foreignBytes = resource.serverGpuWritten || resource.persistentMapped ||
|
||||
resource.immutableStorage || resource.externalAhb != nullptr;
|
||||
// The size gate first, with the whole store as the queue: it answers every refusal
|
||||
// that does not depend on the queue before any lock is taken.
|
||||
if (!WholeStoreRenameApplies(store, frontendSize, store, hostBase != nullptr, foreignBytes, mapUsable))
|
||||
return;
|
||||
// Under split the host bytes are the server's staged copy, which only holds what the
|
||||
// content records delivered; monolith's base is the frontend shadow, the whole store.
|
||||
if (ServerStaged().CopiesIntoServerStorage() && !ServerStaged().IsCovered(&resource, 0, store)) return;
|
||||
const std::lock_guard<std::mutex> lock(resource.pendingMutex);
|
||||
if (resource.pendingRanges.empty() || !resource.pendingResidentWrites.empty()) return;
|
||||
SizeT queuedBytes = 0;
|
||||
for (const auto& range : resource.pendingRanges) {
|
||||
const SizeT end = std::min(range.end, store);
|
||||
queuedBytes += end - std::min(range.start, end);
|
||||
}
|
||||
if (!WholeStoreRenameApplies(store, frontendSize, queuedBytes, true, false, true)) return;
|
||||
resource.pendingRanges.clear();
|
||||
resource.pendingRanges.Add({0, store});
|
||||
}
|
||||
|
||||
// Land the app bytes queued for an ADOPTED store on the GPU timeline: staged
|
||||
// into the upload ring and delivered by glCopyBufferSubData. The destination
|
||||
// is the IMMUTABLE persistent store, which the driver can neither rename nor
|
||||
@@ -3555,6 +3584,7 @@ namespace MobileGL::MG_Backend::DirectGLES {
|
||||
} else if (!resource->pendingRanges.empty()) {
|
||||
// Same rule as Ops_H_Readback's, at the draw-time drain: with no frontend object
|
||||
// `hostBase` is the server shadow and every queued range must be staged.
|
||||
RenameSmallStoreForDrain(*resource, hostBase, size);
|
||||
MGL_SERVER_STAGED_REQUIRE_PENDING(*resource, hostBase, size, "ensure_flush_pending");
|
||||
FlushPendingRangesFrom(*resource, hostBase, size);
|
||||
resource->syncedChangeSerial = serial;
|
||||
|
||||
@@ -984,6 +984,46 @@ namespace MobileGL::MG_Backend::DirectGLES {
|
||||
(wholeBuffer ? GL_MAP_INVALIDATE_BUFFER_BIT : GL_MAP_INVALIDATE_RANGE_BIT);
|
||||
}
|
||||
|
||||
// P15: WRITE RENAMING FOR A SMALL STORE. The draw-time drain sends a partial range below
|
||||
// the tier-1 threshold through the upload ring, and every such range is one
|
||||
// glCopyBufferSubData: a GPU copy job the driver builds on the render thread (FCL MC 1.21.5
|
||||
// monolith on Adreno 830: 0.08 of the drain's 0.105 ms/frame) and then runs on the GPU
|
||||
// timeline. Minecraft's immediate-mode batches rewrite a small per-format vertex buffer from
|
||||
// offset 0 before each draw, so these ranges are partial writes of tiny stores (the
|
||||
// 1.21.5 in-world trace: ~122 a frame, 81 of them into 288-byte stores). For such a store
|
||||
// the drain rewrites the WHOLE store from the host bytes instead, which is tier 1's
|
||||
// whole-buffer orphan-map: draws already recorded keep the old storage, draws after read the
|
||||
// new one, and no copy job is recorded - the rename Magma's RenameBusyWireStore makes under
|
||||
// the same 64 KiB bound.
|
||||
//
|
||||
// The bytes the rename moves that the queue did not ask for are bounded by
|
||||
// kEsprytWholeStoreRenameMaxExtraBytes: a 128-byte write into a 53 KiB index store stays a
|
||||
// ring copy rather than becoming a 53 KiB memcpy. On that trace the bound keeps 108 of the
|
||||
// 122 renames for 101 KiB of extra host copying a frame; renaming every store up to 64 KiB
|
||||
// would copy 811 KiB.
|
||||
//
|
||||
// Widening a map past the queued range is the edge that already drew blood (the note on
|
||||
// InvalidateFlushAccessFor): it is only exact when the host bytes ARE the store, byte for
|
||||
// byte, outside the queued ranges too. So every way the store can hold bytes the host
|
||||
// copy does not is a refusal, and the caller passes them in as one flag: a shader write
|
||||
// since the last whole readback (serverGpuWritten), queued resident writes, a coherent or
|
||||
// imported store, immutable storage. `hostBytesCoverStore` is the host copy's own extent:
|
||||
// the frontend shadow on monolith, the staged coverage under split. `mapUsable` is tier 1's
|
||||
// own gate - without the map the whole range would just be a bigger ring copy.
|
||||
//
|
||||
// `queuedBytes` is the sum of the queued ranges clamped to the store (VecRange1D keeps them
|
||||
// disjoint).
|
||||
inline constexpr SizeT kEsprytWholeStoreRenameMaxBytes = 64u * 1024u;
|
||||
inline constexpr SizeT kEsprytWholeStoreRenameMaxExtraBytes = 8u * 1024u;
|
||||
constexpr Bool WholeStoreRenameApplies(SizeT storageSize, SizeT frontendSize, SizeT queuedBytes,
|
||||
Bool hostBytesCoverStore, Bool storeHoldsBytesTheHostDoesNot,
|
||||
Bool mapUsable) {
|
||||
if (!mapUsable || !hostBytesCoverStore || storeHoldsBytesTheHostDoesNot) return false;
|
||||
if (storageSize == 0 || storageSize > kEsprytWholeStoreRenameMaxBytes) return false;
|
||||
if (frontendSize != storageSize || queuedBytes == 0 || queuedBytes > storageSize) return false;
|
||||
return storageSize - queuedBytes <= kEsprytWholeStoreRenameMaxExtraBytes;
|
||||
}
|
||||
|
||||
// The handle arms of the two draw-path entry points below. IsBufferDrawCleanByHandle
|
||||
// asks the applier the same five questions IsBufferDrawClean asks the frontend object,
|
||||
// with identical semantics (D-A4); EnsureBufferResourceForHandle is the ensure path
|
||||
|
||||
@@ -264,4 +264,38 @@ void main() {
|
||||
<< "range untouched";
|
||||
}
|
||||
|
||||
// The same partial SubData, but drained by the NEXT DISPATCH rather than by a readback: the
|
||||
// counter buffer is ensured as a binding, which is the draw-time drain. DirectGLES renames a
|
||||
// small store there - the whole store is rewritten from the host copy instead of the queued
|
||||
// range being copied - and a store a shader wrote is exactly the one whose host copy is stale
|
||||
// outside the range: offset 4 still holds the seed on the CPU side and the first dispatch's
|
||||
// increments on the GPU. Renaming it would put the seed back and the second dispatch would
|
||||
// count from there.
|
||||
TEST_F(AtomicCounterScenario, APartialSubDataBetweenDispatchesKeepsTheShaderWrittenNeighbour) {
|
||||
if (!Ready() || IsSkipped()) return;
|
||||
|
||||
const GLuint zero = MakeCounterBuffer(0, {0u, 0u});
|
||||
MakeCounterBuffer(1, {0u});
|
||||
ASSERT_EQ(FirstGLError(), 0u);
|
||||
|
||||
Dispatch();
|
||||
|
||||
const unsigned int reseed = 4242u;
|
||||
glBindBuffer(GL_ATOMIC_COUNTER_BUFFER, zero);
|
||||
glBufferSubData(GL_ATOMIC_COUNTER_BUFFER, 0, sizeof(reseed), &reseed);
|
||||
glBindBuffer(GL_ATOMIC_COUNTER_BUFFER, 0);
|
||||
ASSERT_EQ(FirstGLError(), 0u) << "re-seeding offset 0 raised a GL error";
|
||||
|
||||
Dispatch();
|
||||
|
||||
const std::vector<unsigned int> values = ReadCounters(zero, 2);
|
||||
EXPECT_EQ(FirstGLError(), 0u);
|
||||
EXPECT_EQ(values[0], reseed + kInvocations)
|
||||
<< "offset 0 read back " << values[0] << "; the SubData did not reach the second dispatch";
|
||||
EXPECT_EQ(values[1], 4 * kInvocations)
|
||||
<< "offset 4 read back " << values[1] << "; " << 2 * kInvocations
|
||||
<< " means the drain before the second dispatch rewrote the shader-written neighbour from the "
|
||||
<< "stale host copy";
|
||||
}
|
||||
|
||||
} // namespace MGITest
|
||||
|
||||
@@ -497,6 +497,63 @@ void main() {
|
||||
|
||||
// ---- a self-test of the assertions, not of MobileGL ------------------
|
||||
//
|
||||
// Inside ONE frame: draw, overwrite PART of the vertex buffer, draw again. This is the
|
||||
// immediate-mode streaming shape (Minecraft rewrites a small per-format vertex buffer from
|
||||
// offset 0 before every batch), and the first draw is still queued when the write lands -
|
||||
// it must keep the bytes it was issued with. DirectGLES drains this partial range of a
|
||||
// small store by renaming the whole store rather than copying the range, so this pins that
|
||||
// the rename is an orphan the first draw survives and that it carries the bytes OUTSIDE
|
||||
// the written range (the right quad's vertices) into the new storage.
|
||||
TEST_F(CrossFrameBufferScenario, APartialSubDataBetweenTwoDrawsOfOneFrameKeepsTheFirstDrawsBytes) {
|
||||
ASSERT_NO_FATAL_FAILURE(BuildScene(/*immutable=*/false));
|
||||
for (int frame = 0; frame < kWarmupFrames; ++frame) {
|
||||
BeginFrame();
|
||||
DrawScene();
|
||||
Gl().EndFrame();
|
||||
}
|
||||
|
||||
// The left quad's four vertices moved onto the right half and turned blue: 80 of the
|
||||
// buffer's 160 bytes.
|
||||
std::vector<Vertex> moved = SceneVertices(/*leftQuadIsGreen=*/false);
|
||||
for (int i = 0; i < kLeftQuadVertexCount; ++i) {
|
||||
moved[i].x += 1.0f;
|
||||
moved[i].r = 0.0f;
|
||||
moved[i].g = 0.0f;
|
||||
moved[i].b = 1.0f;
|
||||
}
|
||||
const GLsizeiptr leftQuadBytes = GLsizeiptr(kLeftQuadVertexCount * sizeof(Vertex));
|
||||
ASSERT_LT(leftQuadBytes, m_vertexBytes) << "the write must be a partial range of the store";
|
||||
|
||||
BeginFrame();
|
||||
DrawScene();
|
||||
glBindBuffer(GL_ARRAY_BUFFER, m_vbo);
|
||||
glBufferSubData(GL_ARRAY_BUFFER, 0, leftQuadBytes, moved.data());
|
||||
glBindBuffer(GL_ARRAY_BUFFER, 0);
|
||||
ASSERT_EQ(FirstGLError(), GLenum(GL_NO_ERROR)) << "the partial SubData raised a GL error";
|
||||
DrawScene();
|
||||
const Image frame = ReadFrame();
|
||||
Gl().EndFrame();
|
||||
|
||||
ExpectHalves(frame, "red", "blue",
|
||||
"a draw queued before a partial SubData must keep its bytes, and the draw after it "
|
||||
"must see the new ones");
|
||||
|
||||
// The next frame draws only from the rewritten store: the right quad's vertices
|
||||
// (outside the written range) must have come along with the rename, so the second
|
||||
// index set still reaches them.
|
||||
BeginFrame();
|
||||
glBindVertexArray(m_vao);
|
||||
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, m_ebo);
|
||||
glBufferSubData(GL_ELEMENT_ARRAY_BUFFER, 0, m_indexBytes, kIndicesRightQuad);
|
||||
glBindVertexArray(0);
|
||||
DrawScene();
|
||||
const Image next = ReadFrame();
|
||||
Gl().EndFrame();
|
||||
ExpectHalves(next, "black", "green",
|
||||
"the bytes outside the partial write were lost when the store was rewritten");
|
||||
EXPECT_EQ(FirstGLError(), GLenum(GL_NO_ERROR));
|
||||
}
|
||||
|
||||
// Every case above leans on ExpectHalves. ExpectHalves used to sample the
|
||||
// centre pixel of each half - two pixels for a 12288-pixel readback - and
|
||||
// that is measurably too weak to stand behind a claim about buffer
|
||||
|
||||
@@ -902,3 +902,51 @@ TEST(EsprytFlushLadder, APartialRangeDoesNotOvertakeAQueuedRingCopy) {
|
||||
static_cast<GLbitfield>(GL_MAP_WRITE_BIT | GL_MAP_INVALIDATE_BUFFER_BIT))
|
||||
<< "the whole-buffer orphan-map rewrites every byte from the shadow the copies were taken from";
|
||||
}
|
||||
|
||||
// P15: the small-store write rename. A partial range of a small store is drained as the whole
|
||||
// store - tier 1's orphan-map - instead of one upload-ring copy job per range, but only while the
|
||||
// host bytes are the store byte for byte, and only while the bytes nobody asked for stay small.
|
||||
namespace {
|
||||
using MobileGL::MG_Backend::DirectGLES::BufferImpl::kEsprytWholeStoreRenameMaxBytes;
|
||||
using MobileGL::MG_Backend::DirectGLES::BufferImpl::kEsprytWholeStoreRenameMaxExtraBytes;
|
||||
using MobileGL::MG_Backend::DirectGLES::BufferImpl::WholeStoreRenameApplies;
|
||||
} // namespace
|
||||
|
||||
TEST(EsprytFlushLadder, ASmallStoreWhoseHostBytesAreTheStoreIsRenamedWhole) {
|
||||
// Minecraft's immediate-mode shape: 96 of a 288-byte vertex store, from offset 0.
|
||||
EXPECT_TRUE(WholeStoreRenameApplies(288, 288, 96, true, false, true));
|
||||
EXPECT_TRUE(WholeStoreRenameApplies(kEsprytWholeStoreRenameMaxBytes, kEsprytWholeStoreRenameMaxBytes,
|
||||
kEsprytWholeStoreRenameMaxBytes - kEsprytWholeStoreRenameMaxExtraBytes, true,
|
||||
false, true));
|
||||
// And the renamed range is one tier 1 takes as the whole-buffer orphan-map, with or without a
|
||||
// ring copy still queued on the store.
|
||||
EXPECT_EQ(InvalidateFlushAccessFor(0, 288, 0, 288, 288, 288, true),
|
||||
static_cast<GLbitfield>(GL_MAP_WRITE_BIT | GL_MAP_INVALIDATE_BUFFER_BIT));
|
||||
}
|
||||
|
||||
TEST(EsprytFlushLadder, ARenameThatWouldMoveTooManyUnaskedBytesKeepsItsQueuedRanges) {
|
||||
EXPECT_FALSE(WholeStoreRenameApplies(kEsprytWholeStoreRenameMaxBytes + 1, kEsprytWholeStoreRenameMaxBytes + 1,
|
||||
kEsprytWholeStoreRenameMaxBytes + 1, true, false, true))
|
||||
<< "above the store bound the queue is drained as it stands";
|
||||
// The 1.21.5 trace's other shape: 128 bytes into a 53 KiB index store.
|
||||
EXPECT_FALSE(WholeStoreRenameApplies(53824, 53824, 128, true, false, true))
|
||||
<< "rewriting 53 KiB for a 128-byte write moves more bytes than the copy job it saves";
|
||||
EXPECT_FALSE(WholeStoreRenameApplies(16384, 16384, 16384 - kEsprytWholeStoreRenameMaxExtraBytes - 1, true,
|
||||
false, true));
|
||||
EXPECT_FALSE(WholeStoreRenameApplies(0, 0, 0, true, false, true));
|
||||
EXPECT_FALSE(WholeStoreRenameApplies(288, 288, 0, true, false, true)) << "nothing queued, nothing to rename";
|
||||
}
|
||||
|
||||
// The refusals are the widening rule: the rename rewrites bytes outside the queued ranges, which
|
||||
// is exact only when the host copy holds them.
|
||||
TEST(EsprytFlushLadder, AStoreHoldingBytesTheHostCopyDoesNotIsNeverRenamed) {
|
||||
EXPECT_FALSE(WholeStoreRenameApplies(288, 288, 96, true, true, true))
|
||||
<< "a shader-written (or coherent, imported, resident-written) store would get the stale "
|
||||
"host bytes back over what the GPU put there";
|
||||
EXPECT_FALSE(WholeStoreRenameApplies(288, 288, 96, false, false, true))
|
||||
<< "under split a staged copy that does not cover the store has no bytes for the rest of it";
|
||||
EXPECT_FALSE(WholeStoreRenameApplies(288, 576, 96, true, false, true))
|
||||
<< "a store the frontend has already resized is a respecify, not a rename";
|
||||
EXPECT_FALSE(WholeStoreRenameApplies(288, 288, 96, true, false, false))
|
||||
<< "without tier 1's map the whole store would only be a bigger ring copy";
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user