From 3874b547a9f509a3004ee2236e7185384c551b3c Mon Sep 17 00:00:00 2001 From: Swung0x48 Date: Wed, 23 Sep 2026 10:53:36 -0400 Subject: [PATCH] [MG_Remote, MG_Test] (P7): band ReadPixels larger than one reply slot --- MobileGL/MG_Remote/Client/ClientSession.h | 11 +- MobileGL/MG_Remote/Client/EmitTables.cpp | 256 ++++++++++++++------- MobileGL/MG_Remote/Client/EmitTables.h | 58 +++++ MobileGL/MG_Remote/Transport/ReplySlot.h | 22 +- MobileGL/MG_Test/Wire/RemoteClientTest.cpp | 167 ++++++++++++++ 5 files changed, 417 insertions(+), 97 deletions(-) diff --git a/MobileGL/MG_Remote/Client/ClientSession.h b/MobileGL/MG_Remote/Client/ClientSession.h index af201bc5..2d007134 100644 --- a/MobileGL/MG_Remote/Client/ClientSession.h +++ b/MobileGL/MG_Remote/Client/ClientSession.h @@ -366,8 +366,10 @@ namespace MobileGL::MG_Remote::Client { // DECLINED IS A REAL ANSWER - MapPersistent's nullptr and the four Bool acceptances. Bool ReadReply(Uint64 seq, void* outBytes, Uint64 outCapacity, Int32* outStatus, Uint64* outSize); - // What one answer may carry. A ReadPixels bigger than this is Fatal rather than - // chunked, so the client checks BEFORE it emits. + // What one answer may carry. No single answer is ever bigger than this: a ReadPixels + // whose whole read is bigger is split by the emitter into row bands (or, for a row + // wider than a reply, single-row pieces) that each fit (g5-readback, EmitTables.h + // PlanReadbackBands), and the client still checks every band BEFORE it emits. Uint32 MaxReplyBytes() const; // ID-47, the fifth primitive: true exactly when an answer of `bytes` can be posted. Bool ReplyCanHold(Uint64 bytes) const; @@ -375,8 +377,9 @@ namespace MobileGL::MG_Remote::Client { // Returns when the answer fits; otherwise // Fatal{ReplyTooLarge, "ReadPixels x > "} // and abort - AT THE CLIENT, BEFORE EMISSION. Package c1's OnReadPixels emitter calls - // this once, immediately before EmitAndWait(MGPWireOp::ReadPixels, ...), with the - // record's box, its Format/Type enums and the DstSize it computed; the server's Post + // this immediately before each EmitAndWait(MGPWireOp::ReadPixels, ...), with that + // band's box, its Format/Type enums and the DstSize it computed - and, when not even one + // pixel fits a reply, once for that one pixel instead of emitting; the server's Post // keeps its own refusal as the last line of defence, but that one fires on the apply // thread with the record already on the wire, where all the client sees is a hang. void RequireReadPixelsReplyFits(Uint32 width, Uint32 height, Uint32 format, Uint32 type, diff --git a/MobileGL/MG_Remote/Client/EmitTables.cpp b/MobileGL/MG_Remote/Client/EmitTables.cpp index 961d09ac..82a27900 100644 --- a/MobileGL/MG_Remote/Client/EmitTables.cpp +++ b/MobileGL/MG_Remote/Client/EmitTables.cpp @@ -197,6 +197,16 @@ namespace MobileGL::MG_Remote::Client { return (rowBytes % alignment) == 0; } + // A band's box origin (g5-readback): the read's origin plus the band's offset, summed + // wide. It can only leave Int32 for a read whose far edge was already past INT32_MAX, + // and every pixel out there is outside any framebuffer - GL leaves their values + // undefined - so it saturates instead of wrapping onto real pixels. + Int32 ReadbackBandOrigin(GLint origin, Uint64 offset) { + const Int64 at = static_cast(origin) + static_cast(offset); + constexpr Int64 kMax = std::numeric_limits::max(); + return static_cast(at > kMax ? kMax : at); + } + // ---- the session, demanded rather than assumed -------------------------------- // // Every class-B slot needs one. A null session here is NOT the monolith answer - the @@ -920,38 +930,6 @@ namespace MobileGL::MG_Remote::Client { if (width <= 0 || height <= 0) return; const Uint64 bytesPerPixel = ReadbackBytesPerPixel(format, type); - // ONE tight-size function for production AND the control (M3 / codex 10a). The first - // cut computed this inline here while the test drove TightReadbackByteCount, so a - // `+16` on the production line stayed green - the test observed a different number. - // Now the number the server allocates and the number the test asserts come from the - // same body. - const Uint64 tight = TightReadbackBytes(width, height, format, type); - - MG_Pipe::MGPReadbackInfo info{}; - info.Res = MG_Pipe::kMGPipeNullHandle; // "the bound read surface answers" - info.Box = MG_Pipe::MGPBox{x, y, 0, static_cast(width), - static_cast(height), 1}; - info.Format = static_cast(format); - info.Type = static_cast(type); - info.Target = 0; - info.Level = 0; - info.DstOffset = 0; - info.DstSize = tight; - - // CHECKED BEFORE THE EMISSION, not after the answer (ID-47), and through S1's - // HELPER rather than a copy of it here. A reply bigger than a slot is Fatal on the - // SERVER too, and s1 keeps that as the last line of defence - but it fires on the - // apply thread with the record already on the wire, where all the client sees is a - // hang. This one names the read, at the call site that knows what the read was. - // - // THE NUMBER IS ID-49's TIGHT EXTENT and not the packed one: the pack state never - // crosses, so the answer that has to fit a slot is w*h*bytesPerPixel. The cap is - // the pool's own, read live, so s1's growth of SEG_REPLY to 16 MiB / eight 2 MiB - // slots needed no edit in this file - only the merge. - session.RequireReadPixelsReplyFits(static_cast(width), - static_cast(height), - static_cast(format), - static_cast(type), tight); PixelStoreParameters pack{}; if (MG_State::pGLContext != nullptr) { @@ -965,50 +943,115 @@ namespace MobileGL::MG_Remote::Client { return; } - Int32 status = 0; - Uint64 replySize = 0; - if (!pbo && ReadbackPackStateIsTight(width, bytesPerPixel, pack)) { - // THE COMMON CASE, AND IT KEEPS THE ZERO-COPY. A neutral pack state means the - // destination layout IS the tight layout, so the reply lands straight in the - // application's buffer and there is no bounce at all. It is a fast path for the - // SAME bytes, not a second rule: ScatterTightReadback below is a memcpy of the - // whole run in exactly this case, and the control drives that function. - session.EmitAndWait(MG_Pipe::MGPWireOp::ReadPixels, &info, sizeof(info), nullptr, 0, - pixels, tight, &status, &replySize); - // M2 / codex 11: an OK reply that arrived short, or a DECLINE/ERROR, must not be - // handed back as pixels. The Fatal aborts before the application reads the buffer, - // so the bytes EmitAndWait already copied into `pixels` are never observed. - RequireReadbackReplyComplete(status, replySize, tight); - ApplyReadbackByteSwap(pixels, tight, type, pack); + // P7 GATE 5 (g5-readback): A READ LARGER THAN ONE REPLY SLOT IS BANDED, NOT REFUSED. + // KHR-GL46.direct_state_access.renderbuffers_storage reads 256x512 RGBA/FLOAT = + // 2,097,152 bytes, sixteen over MaxReplyBytes, and that used to be + // Fatal{ReplyTooLarge} on every split arm while the monolith passed. Each band below + // is an ordinary read_pixels record - its own box, its own TIGHT DstSize - so every + // answer still fits one slot (ID-47) and the server's PH-3 bound (`tight == DstSize + // <= LinkTerms.maxReplyBytes`, PipeApplier.cpp OnReadPixels) holds unchanged: no + // reply is chunked, the READ is. The cap is the link's own, read live, exactly as + // before, so a different SEG_REPLY geometry or a stream link needs no edit here. + ReadbackBandPlan plan; + if (!PlanReadbackBands(static_cast(width), static_cast(height), + bytesPerPixel, session.MaxReplyBytes(), plan)) { + // NOT EVEN ONE PIXEL FITS (or there is no reply pool at all). That is the only + // read no banding can answer, and ID-47's named refusal is kept for it - for the + // one-pixel piece that cannot be sent, so the message names what was impossible. + session.RequireReadPixelsReplyFits(1, 1, static_cast(format), + static_cast(type), bytesPerPixel); return; } - // The bounce is the price of the application having asked for a layout. It is the - // tight size and never more, and it is freed before this returns - R-11's rule one - // level out: nothing here outlives the call. - Vector bounce(static_cast(tight)); - session.EmitAndWait(MG_Pipe::MGPWireOp::ReadPixels, &info, sizeof(info), nullptr, 0, - bounce.data(), tight, &status, &replySize); - // BEFORE THE SCATTER, so a short or non-OK reply never reaches the application's - // pointer at all (the bounce is the only thing that held the partial bytes). - RequireReadbackReplyComplete(status, replySize, tight); - ApplyReadbackByteSwap(bounce.data(), tight, type, pack); - if (pbo) { - // pixels is an offset, never a host pointer. Upload only the - // requested rows so PACK padding and untouched bytes survive. - pbo->SyncGpuWrites(); - const Uint64 alignment = std::max(pack.Alignment, 1); - const Uint64 rowPixels = pack.RowLength > 0 ? pack.RowLength : width; - const Uint64 stride = (rowPixels * bytesPerPixel + alignment - 1) / alignment * alignment; - const Uint64 start = reinterpret_cast(pixels) + pack.SkipRows * stride + - pack.SkipPixels * bytesPerPixel; - const SizeT rowBytes = static_cast(width) * bytesPerPixel; - for (Int y = 0; y < height; ++y) - pbo->UploadSubData({bounce.data() + y * rowBytes, rowBytes}, start + y * stride); - return; - } - ScatterTightReadbackIntoPackState(bounce.data(), pixels, width, height, bytesPerPixel, - pack); + // THE COMMON CASE KEEPS THE ZERO-COPY. A neutral pack state means the destination + // layout IS the tight layout, so each band's reply lands straight in the + // application's buffer at the band's tight offset (whole-width bands and single-row + // pieces are both contiguous there). It is a fast path for the SAME bytes, not a + // second rule: ScatterReadbackBandIntoPackState is a memcpy of the band in exactly + // this case, and the control drives that function. + const Bool direct = !pbo && ReadbackPackStateIsTight(width, bytesPerPixel, pack); + // The bounce is the price of the application having asked for a layout. It is ONE + // band - at most a reply slot - rather than the whole read, and it is freed before + // this returns: R-11's rule one level out, nothing here outlives the call. + Vector bounce; + Bool pboSynced = false; + ForEachReadbackBand(static_cast(width), static_cast(height), plan, + [&](const ReadbackBand& band) { + // ONE tight-size function for production AND the control (M3 / codex 10a): the + // number the server allocates and the number the test asserts come from the + // same body, per band now. + const Uint64 bandBytes = TightReadbackBytes(static_cast(band.Columns), + static_cast(band.Rows), + format, type); + MG_Pipe::MGPReadbackInfo info{}; + info.Res = MG_Pipe::kMGPipeNullHandle; // "the bound read surface answers" + info.Box = MG_Pipe::MGPBox{ReadbackBandOrigin(x, band.FirstColumn), + ReadbackBandOrigin(y, band.FirstRow), 0, + static_cast(band.Columns), + static_cast(band.Rows), 1}; + info.Format = static_cast(format); + info.Type = static_cast(type); + info.Target = 0; + info.Level = 0; + info.DstOffset = 0; + info.DstSize = bandBytes; + + // CHECKED BEFORE THE EMISSION, not after the answer (ID-47), through S1's + // helper, for every record. The plan makes it true by construction; it stays + // because it is the one check that names the read at the call site if the plan + // and the pool ever disagree, where the server's PH-3 refusal would only answer + // ERROR on the apply thread. + session.RequireReadPixelsReplyFits(static_cast(band.Columns), + static_cast(band.Rows), + static_cast(format), + static_cast(type), bandBytes); + + Int32 status = 0; + Uint64 replySize = 0; + if (direct) { + Uint8* at = pixels == nullptr + ? nullptr + : static_cast(pixels) + + (band.FirstRow * static_cast(width) + band.FirstColumn) * + bytesPerPixel; + session.EmitAndWait(MG_Pipe::MGPWireOp::ReadPixels, &info, sizeof(info), + nullptr, 0, at, bandBytes, &status, &replySize); + // M2 / codex 11: an OK reply that arrived short, or a DECLINE/ERROR, must not + // be handed back as pixels. The Fatal aborts before the application reads the + // buffer, so the bytes EmitAndWait already copied are never observed. + RequireReadbackReplyComplete(status, replySize, bandBytes); + ApplyReadbackByteSwap(at, bandBytes, type, pack); + return; + } + + if (bounce.size() < bandBytes) bounce.resize(static_cast(bandBytes)); + session.EmitAndWait(MG_Pipe::MGPWireOp::ReadPixels, &info, sizeof(info), nullptr, + 0, bounce.data(), bandBytes, &status, &replySize); + // BEFORE THE SCATTER, so a short or non-OK reply never reaches the application's + // pointer at all (the bounce is the only thing that held the partial bytes). + RequireReadbackReplyComplete(status, replySize, bandBytes); + ApplyReadbackByteSwap(bounce.data(), bandBytes, type, pack); + if (pbo) { + // pixels is an offset, never a host pointer. Upload only the requested + // rows so PACK padding and untouched bytes survive; the offsets are the + // overflow-checked layout's, the same numbers the range check accepted. + if (!pboSynced) { + pbo->SyncGpuWrites(); + pboSynced = true; + } + const Uint64 bandRowBytes = band.Columns * bytesPerPixel; + for (Uint64 row = 0; row < band.Rows; ++row) { + pbo->UploadSubData({bounce.data() + row * bandRowBytes, + static_cast(bandRowBytes)}, + pboOffset + layout.Start + + (band.FirstRow + row) * layout.RowStride + + band.FirstColumn * bytesPerPixel); + } + return; + } + ScatterReadbackBandIntoPackState(bounce.data(), pixels, width, band, bytesPerPixel, + pack); + }); } void EmitPresent() { @@ -1853,12 +1896,41 @@ namespace MobileGL::MG_Remote::Client { return status == Wire::ReplySink::kStatusOk && replySize == expected; } - // ID-49's scatter. Exported for the same reason as the refusal above: the control drives - // THIS, which is what the emitter calls, rather than a second copy of 8.4.4's arithmetic. - void ScatterTightReadbackIntoPackState(const void* tight, void* destination, GLsizei width, - GLsizei height, Uint64 bytesPerPixel, - const PixelStoreParameters& pack) { - if (tight == nullptr || destination == nullptr || width <= 0 || height <= 0) return; + // g5-readback's plan (EmitTables.h, ReadbackBand). Pixels-per-reply first and the row test + // against it, so no product here can overflow whatever the inputs: a unit control drives + // this with Uint64 extremes, and the emitter with a GLsizei width. + Bool PlanReadbackBands(Uint64 width, Uint64 height, Uint64 bytesPerPixel, Uint64 maxReplyBytes, + ReadbackBandPlan& plan) { + if (width == 0 || height == 0 || bytesPerPixel == 0 || maxReplyBytes < bytesPerPixel) { + return false; + } + const Uint64 pixelsPerReply = maxReplyBytes / bytesPerPixel; // >= 1 + if (width <= pixelsPerReply) { + // floor(floor(cap / bpp) / width) == floor(cap / (bpp * width)): the most whole rows + // one reply holds, and at least one because a row fits. + plan.RowsPerBand = pixelsPerReply / width; + plan.ColumnsPerBand = width; + } else { + // A SINGLE ROW IS LARGER THAN A REPLY (a >131071-pixel RGBA/FLOAT row at the 2 MiB + // slot - wider than any attachment, but a legal glReadPixels whose in-bounds pixels + // GL still defines). Each row is cut into pieces; one row per band keeps every piece + // contiguous in the tight layout. + plan.RowsPerBand = 1; + plan.ColumnsPerBand = pixelsPerReply; + } + return true; + } + + // ID-49's scatter, for one band of a banded read (g5-readback). The whole-read scatter below + // is this function over the single band {0, height, 0, width}, so there is one copy of + // 8.4.4's arithmetic and the control drives it. + void ScatterReadbackBandIntoPackState(const void* band, void* destination, GLsizei width, + const ReadbackBand& where, Uint64 bytesPerPixel, + const PixelStoreParameters& pack) { + if (band == nullptr || destination == nullptr || width <= 0 || where.Rows == 0 || + where.Columns == 0) { + return; + } const Uint64 rowPixels = pack.RowLength > 0 ? static_cast(pack.RowLength) : static_cast(width); const Uint64 alignment = pack.Alignment > 0 ? static_cast(pack.Alignment) : 1ull; @@ -1871,23 +1943,37 @@ namespace MobileGL::MG_Remote::Client { // would do with the same state rather than clamping, so the two arms stay byte-identical. // The fast path (ReadbackPackStateIsTight) already rejects any ROW_LENGTH != width, so // this only runs on the scatter path the application asked for. - const Uint64 writtenPerRow = static_cast(width) * bytesPerPixel; + const Uint64 writtenPerRow = where.Columns * bytesPerPixel; // SKIP_IMAGES and IMAGE_HEIGHT ARE IGNORED (codex 6). glReadPixels is a 2-D read; GL // does not apply the image-level pack parameters to it, and the monolith conversion path // says so explicitly with honorPackImageParams=false (DirectGLES.cpp:10905). Applying // SKIP_IMAGES here shifted a read with SKIP_IMAGES=1 by a whole image and overran an // application buffer sized for exactly `height` rows. Only SKIP_ROWS and SKIP_PIXELS - - // the 2-D skips - offset the first written byte. + // the 2-D skips - offset the first written byte; the band's own row and column then + // offset it within the read, on the SAME stride, so a banded read writes exactly the + // bytes the whole-read scatter would have, in the same row order. auto* out = static_cast(destination) + - static_cast(pack.SkipRows) * strideBytes + - static_cast(pack.SkipPixels) * bytesPerPixel; - const auto* in = static_cast(tight); - for (Uint64 row = 0; row < static_cast(height); ++row) { + (static_cast(pack.SkipRows) + where.FirstRow) * strideBytes + + (static_cast(pack.SkipPixels) + where.FirstColumn) * bytesPerPixel; + const auto* in = static_cast(band); + for (Uint64 row = 0; row < where.Rows; ++row) { std::memcpy(out + row * strideBytes, in + row * writtenPerRow, static_cast(writtenPerRow)); } } + // ID-49's scatter. Exported for the same reason as the refusal above: the control drives + // THIS, which is what the emitter calls, rather than a second copy of 8.4.4's arithmetic. + void ScatterTightReadbackIntoPackState(const void* tight, void* destination, GLsizei width, + GLsizei height, Uint64 bytesPerPixel, + const PixelStoreParameters& pack) { + if (tight == nullptr || destination == nullptr || width <= 0 || height <= 0) return; + ScatterReadbackBandIntoPackState( + tight, destination, width, + ReadbackBand{0, static_cast(height), 0, static_cast(width)}, + bytesPerPixel, pack); + } + // ============================================================================= // P5b d1 - the draw family's record plan, at namespace scope so the unit cases drive // exactly what the emitters above call (R-16). diff --git a/MobileGL/MG_Remote/Client/EmitTables.h b/MobileGL/MG_Remote/Client/EmitTables.h index 45707409..950d894a 100644 --- a/MobileGL/MG_Remote/Client/EmitTables.h +++ b/MobileGL/MG_Remote/Client/EmitTables.h @@ -146,6 +146,64 @@ namespace MobileGL::MG_Remote::Client { GLsizei height, Uint64 bytesPerPixel, const PixelStoreParameters& pack); + // P7 gate 5 (g5-readback). A glReadPixels whose tight answer is larger than one reply slot + // is NOT one record: the client splits the w*h rectangle into BANDS, emits each band as an + // ordinary read_pixels record with its own box and its own tight DstSize (so every answer + // still fits one slot, ID-47, and the server's PH-3 bound `tight == DstSize <= maxReplyBytes` + // holds unchanged), and scatters each band into the application's destination under its + // own GL_PACK_* state. No reply is chunked; the READ is. + // + // THE SHAPE. Whole-width row bands of floor(maxReplyBytes / rowBytes) rows when a row fits + // a reply; otherwise every row is cut into column pieces of floor(maxReplyBytes / bpp) + // pixels. Both shapes are contiguous in the tight layout, which is what lets the neutral- + // pack fast path keep reading straight into the application's pointer. Bands are in + // row-major order and cover the rectangle exactly once. + // + // NO PLAN exactly when not even ONE pixel fits a reply (maxReplyBytes < bytesPerPixel, + // including a link with no reply pool at all); the emitter then refuses BY NAME through + // ID-47's unchanged helper rather than emitting anything. That is the only ReadPixels shape + // left that ends in Fatal{ReplyTooLarge}: CONTRACT-P5 row 23 makes an answer that cannot fit + // a slot a named Fatal, not a GL error, and no pixel can be returned in pieces smaller than + // itself. + // + // THE PLAN IS TWO NUMBERS AND THE BANDS ARE WALKED, NOT LISTED: a band list would be one + // allocation per read that grows with the read, and the walk below is the only place the + // order and the coverage are decided - the emitter and the unit control both call it. + struct ReadbackBand { + Uint64 FirstRow = 0; + Uint64 Rows = 0; + Uint64 FirstColumn = 0; + Uint64 Columns = 0; + }; + struct ReadbackBandPlan { + Uint64 RowsPerBand = 0; // 1 when a single row does not fit a reply + Uint64 ColumnsPerBand = 0; // the whole width when it does + }; + // False (and `plan` untouched) exactly when not even one pixel fits a reply. + Bool PlanReadbackBands(Uint64 width, Uint64 height, Uint64 bytesPerPixel, Uint64 maxReplyBytes, + ReadbackBandPlan& plan); + template + void ForEachReadbackBand(Uint64 width, Uint64 height, const ReadbackBandPlan& plan, + Visit&& visit) { + if (plan.RowsPerBand == 0 || plan.ColumnsPerBand == 0) return; + for (Uint64 row = 0; row < height; row += plan.RowsPerBand) { + const Uint64 rows = plan.RowsPerBand < height - row ? plan.RowsPerBand : height - row; + for (Uint64 column = 0; column < width; column += plan.ColumnsPerBand) { + const Uint64 columns = + plan.ColumnsPerBand < width - column ? plan.ColumnsPerBand : width - column; + visit(ReadbackBand{row, rows, column, columns}); + } + } + } + + // The scatter for ONE band: `band` holds band.Rows x band.Columns tight pixels, and they land + // where ScatterTightReadbackIntoPackState would have put those pixels of the whole + // `width`-wide read. ScatterTightReadbackIntoPackState is this function over the single band + // {0, height, 0, width}, so the whole-read arithmetic and the banded one cannot drift. + void ScatterReadbackBandIntoPackState(const void* band, void* destination, GLsizei width, + const ReadbackBand& where, Uint64 bytesPerPixel, + const PixelStoreParameters& pack); + // ID-49. True when the destination layout IS the tight layout, which is the only condition // under which EmitReadPixels may read the reply straight into the application pointer and // skip the bounce. Exported because the FAST PATH and the SCATTER have to agree, and the diff --git a/MobileGL/MG_Remote/Transport/ReplySlot.h b/MobileGL/MG_Remote/Transport/ReplySlot.h index 658d974d..5f207e25 100644 --- a/MobileGL/MG_Remote/Transport/ReplySlot.h +++ b/MobileGL/MG_Remote/Transport/ReplySlot.h @@ -30,11 +30,15 @@ // // A REPLY LARGER THAN ONE SLOT IS FATAL, NOT CHUNKED, AND THE CLIENT SAYS SO // FIRST (ID-47). P5's only large answer is ReadPixels, and the client knows its -// size before it emits the record, so it refuses an oversize read BY NAME before -// emission (RequireReadPixelsFits: Fatal{ReplyTooLarge, "ReadPixels x -// > "}); Post's own refusal is the server's last line of -// defence, and reaching it means the two sides disagree about the frame rather -// than that the pool is too small. Chunking is a P6 debt; the pool has no knob. +// size before it emits the record, so it refuses an oversize ANSWER BY NAME +// before emission (RequireReadPixelsFits: Fatal{ReplyTooLarge, "ReadPixels +// x > "}); Post's own refusal is the server's last +// line of defence, and reaching it means the two sides disagree about the frame +// rather than that the pool is too small. The pool still has no knob and a reply +// is still never chunked: since P7 gate 5 (g5-readback) the client splits a READ +// larger than one slot into bands, each an ordinary read_pixels record whose +// answer fits (EmitTables.h PlanReadbackBands), so the refusal is left for the +// one read no banding can answer - a single pixel larger than a slot. // // ORDERING. The client only looks at a slot after it has seen // RingControl::appliedSeq >= its own seq with an ACQUIRE load, and the server @@ -102,6 +106,8 @@ namespace MobileGL::MG_Remote::Transport { // 640x480). A 2400x1080 RGBA8 device surface is ~10.4 MB and is a P6 debt // (chunked readback or a dedicated readback carrier), recorded in the // ROADMAP by the integrator - the geometry here is not the place it is paid. + // It was paid at P7 gate 5 by client-side banding (g5-readback), with this + // geometry unchanged. inline constexpr std::uint32_t kDefaultReplySlotCount = 8; // Slot 0 exists and is used: seq is 1-based, so seq % slotCount hits slot 0 @@ -196,9 +202,9 @@ namespace MobileGL::MG_Remote::Transport { WireLogFatal("MGPipe: Fatal{ReplyTooLarge, \"ReadPixels %ux%u 0x%04X/0x%04X %llu > %u\"} - " "the answer does not fit one SEG_REPLY slot (%u slots of %u bytes, payload " "cap %u; a cap of 0 means no reply pool is configured). Refused at the " - "client before emission (ID-47): P5 neither truncates nor chunks a reply, " - "and a read larger than the cap is a P6 debt (chunked readback), not a " - "bigger pool", + "client before emission (ID-47): a reply is neither truncated nor " + "chunked, and the client bands any read whose single pixel fits a slot, " + "so this answer is one that no banding could have sent", static_cast(width), static_cast(height), static_cast(format), static_cast(type), static_cast(bytes), diff --git a/MobileGL/MG_Test/Wire/RemoteClientTest.cpp b/MobileGL/MG_Test/Wire/RemoteClientTest.cpp index 1ba65d72..c81e4451 100644 --- a/MobileGL/MG_Test/Wire/RemoteClientTest.cpp +++ b/MobileGL/MG_Test/Wire/RemoteClientTest.cpp @@ -1225,6 +1225,173 @@ TEST(RemoteReadback, TheFastPathIsTakenExactlyWhenTheScatterWouldChangeNothing) << "every layout took the fast path, so the equality above is vacuous"; } +// ===================================================================================== +// P7 gate 5 (g5-readback): a read larger than one reply slot is BANDED, not refused +// ===================================================================================== + +namespace { + // The bands the production walk visits, in order - the emitter iterates the same walk. + std::vector CollectReadbackBands(Uint64 width, Uint64 height, Uint64 bpp, Uint64 cap, + Bool* planned = nullptr) { + ReadbackBandPlan plan; + const Bool ok = PlanReadbackBands(width, height, bpp, cap, plan); + if (planned != nullptr) *planned = ok; + std::vector bands; + if (ok) ForEachReadbackBand(width, height, plan, [&](const ReadbackBand& band) { bands.push_back(band); }); + return bands; + } +} // namespace + +TEST(RemoteReadback, TheCtsReadOfExactlyTwoMiBIsTwoBandsThatEachFitASlot) { + // KHR-GL46.direct_state_access.renderbuffers_storage's own read: 256x512 RGBA/FLOAT is + // 2,097,152 bytes against ID-47's 2,097,136. It used to be Fatal{ReplyTooLarge}; it is now two + // records - 511 rows (2,093,056 bytes) and the single top row - and BOTH are answers one slot + // holds, so the server's PH-3 bound (tight == DstSize <= maxReplyBytes) is met per record. + constexpr Uint64 kCap = 2u * 1024u * 1024u - 16u; + const Uint64 bpp = 16; + ASSERT_EQ(TightReadbackByteCount(256, 512, 0x1908 /*RGBA*/, 0x1406 /*FLOAT*/), 2097152u); + const auto bands = CollectReadbackBands(256, 512, bpp, kCap); + ASSERT_EQ(bands.size(), 2u); + EXPECT_EQ(bands[0].FirstRow, 0u); + EXPECT_EQ(bands[0].Rows, 511u); + EXPECT_EQ(bands[1].FirstRow, 511u); + EXPECT_EQ(bands[1].Rows, 1u); + for (const ReadbackBand& band : bands) { + EXPECT_EQ(band.FirstColumn, 0u); + EXPECT_EQ(band.Columns, 256u); + EXPECT_LE(TightReadbackByteCount(static_cast(band.Columns), static_cast(band.Rows), + 0x1908, 0x1406), + kCap); + } + // A read that already fits is ONE record, unchanged from before: exactly the cap, and E2's + // 640x480 RGBA8 snapshot. + EXPECT_EQ(CollectReadbackBands(131071, 1, 16, kCap).size(), 1u) << "exactly the cap is one band"; + EXPECT_EQ(CollectReadbackBands(640, 480, 4, kCap).size(), 1u); + // More than the whole 16 MiB SEG_REPLY: 2048x2100 RGBA8 is 255-row bands, nine of them. + EXPECT_EQ(CollectReadbackBands(2048, 2100, 4, kCap).size(), 9u); +} + +TEST(RemoteReadback, EveryPlanCoversTheReadOnceInRowMajorOrderWithBandsThatFitAndStayContiguous) { + // The plan's invariants over a sweep that includes both shapes - whole-width row bands and, + // for a row wider than a reply, single-row column pieces - at small caps so every boundary is + // hit: the cap exactly one row, one byte short of a row, one pixel, one pixel short of two. + struct Shape { Uint64 w, h, bpp, cap; }; + const Shape shapes[] = { + {7, 5, 4, 28}, {7, 5, 4, 27}, {7, 5, 4, 4}, {7, 5, 4, 7}, {7, 5, 16, 1000}, + {20, 3, 16, 100}, {1, 9, 3, 3}, {9, 1, 3, 26}, {256, 512, 16, 2097136}, {5, 5, 8, 8}, + {33, 17, 4, 131}, {1024, 513, 4, 2097136}, + }; + int rowShapes = 0, pieceShapes = 0; + for (const Shape& s : shapes) { + SCOPED_TRACE(std::to_string(s.w) + "x" + std::to_string(s.h) + " bpp " + std::to_string(s.bpp) + + " cap " + std::to_string(s.cap)); + Bool planned = false; + const auto bands = CollectReadbackBands(s.w, s.h, s.bpp, s.cap, &planned); + ASSERT_TRUE(planned); + std::vector covered(static_cast(s.w * s.h), 0); + Uint64 previousTightEnd = 0; + Bool pieces = false; + for (const ReadbackBand& band : bands) { + ASSERT_GT(band.Rows, 0u); + ASSERT_GT(band.Columns, 0u); + EXPECT_LE(band.Rows * band.Columns * s.bpp, s.cap) << "a band's answer does not fit a reply"; + // CONTIGUOUS IN THE TIGHT LAYOUT (whole-width rows, or a piece of one row), and in + // order: the direct path reads each answer straight into the application's buffer at + // exactly this offset, so a gap or an overlap here is a torn picture there. + EXPECT_TRUE(band.Columns == s.w || band.Rows == 1); + if (band.Columns != s.w) pieces = true; + const Uint64 tightStart = band.FirstRow * s.w + band.FirstColumn; + EXPECT_EQ(tightStart, previousTightEnd) << "bands are not consecutive in row-major order"; + previousTightEnd = tightStart + (band.Rows - 1) * s.w + band.Columns; + for (Uint64 r = 0; r < band.Rows; ++r) + for (Uint64 c = 0; c < band.Columns; ++c) + ++covered[static_cast((band.FirstRow + r) * s.w + band.FirstColumn + c)]; + } + EXPECT_EQ(previousTightEnd, s.w * s.h); + for (size_t i = 0; i < covered.size(); ++i) ASSERT_EQ(covered[i], 1) << "pixel " << i; + // AS FEW RECORDS AS THE SHAPE ALLOWS: a whole-width band takes every row a reply holds. + if (!pieces) { + const Uint64 rowsPerReply = s.cap / (s.w * s.bpp); + EXPECT_EQ(bands.size(), (s.h + rowsPerReply - 1) / rowsPerReply); + ++rowShapes; + } else { + EXPECT_GT(s.w * s.bpp, s.cap) << "a row that fits a reply was cut into pieces"; + ++pieceShapes; + } + } + EXPECT_GT(rowShapes, 0); + EXPECT_GT(pieceShapes, 0) << "the sweep never reached the column-piece arm"; +} + +TEST(RemoteReadback, NotEvenOnePixelFittingIsTheOnlyReadWithNoPlan) { + // The one read banding cannot answer, left to ID-47's named Fatal at the emitter: a single + // pixel larger than a reply (a server that declared a cap below 16 bytes), or no reply pool. + Bool planned = true; + EXPECT_TRUE(CollectReadbackBands(4, 4, 16, 15, &planned).empty()); + EXPECT_FALSE(planned); + EXPECT_TRUE(CollectReadbackBands(4, 4, 4, 0, &planned).empty()); + EXPECT_FALSE(planned); + // And one byte more is a plan, of single-pixel pieces. + const auto bands = CollectReadbackBands(4, 4, 16, 16, &planned); + EXPECT_TRUE(planned); + EXPECT_EQ(bands.size(), 16u); + // Degenerate reads plan nothing (the emitter returns before planning them). + EXPECT_TRUE(CollectReadbackBands(0, 4, 4, 64, &planned).empty()); + EXPECT_FALSE(planned); +} + +TEST(RemoteReadback, ABandedScatterWritesExactlyTheBytesTheWholeScatterWrites) { + // The bounce path's composition: every band scattered on its own must leave the destination + // byte-identical to one whole-read scatter, gaps included, for pack states that pad, skip, + // and - the ill-formed 0 < ROW_LENGTH < width case m6 keeps verbatim - overlap. Both band + // shapes are driven: row bands at a cap of two rows and a bit, pieces at a cap under a row. + constexpr GLsizei kW = 11; + constexpr GLsizei kH = 7; + constexpr Uint8 kSentinel = 0xEE; + for (const Uint64 bpp : {Uint64{3}, Uint64{4}, Uint64{16}}) { + std::vector tight(static_cast(kW) * kH * bpp); + for (size_t i = 0; i < tight.size(); ++i) tight[i] = static_cast(i * 13 + 5); + std::vector layouts(6); + layouts[1].Alignment = 8; + layouts[2].RowLength = kW + 3; + layouts[2].Alignment = 8; + layouts[3].SkipRows = 2; + layouts[3].SkipPixels = 3; + layouts[4].RowLength = kW + 1; + layouts[4].SkipRows = 1; + layouts[4].SkipPixels = 1; + layouts[4].Alignment = 2; + layouts[5].RowLength = 4; // < width: consecutive rows overlap, so ORDER decides the bytes + for (const Uint64 cap : {kW * bpp * 2 + 5, kW * bpp - 1, bpp * 3}) { + for (size_t l = 0; l < layouts.size(); ++l) { + SCOPED_TRACE("bpp " + std::to_string(bpp) + " cap " + std::to_string(cap) + " layout " + + std::to_string(l)); + const PixelStoreParameters& pack = layouts[l]; + std::vector whole(4096, kSentinel); + ScatterTightReadbackIntoPackState(tight.data(), whole.data(), kW, kH, bpp, pack); + + ReadbackBandPlan plan; + ASSERT_TRUE(PlanReadbackBands(kW, kH, bpp, cap, plan)); + std::vector banded(4096, kSentinel); + int bandCount = 0; + ForEachReadbackBand(kW, kH, plan, [&](const ReadbackBand& band) { + // What the server would answer for this band: its own tight rectangle. + std::vector answer(static_cast(band.Rows * band.Columns * bpp)); + for (Uint64 r = 0; r < band.Rows; ++r) { + std::memcpy(answer.data() + r * band.Columns * bpp, + tight.data() + ((band.FirstRow + r) * kW + band.FirstColumn) * bpp, + static_cast(band.Columns * bpp)); + } + ScatterReadbackBandIntoPackState(answer.data(), banded.data(), kW, band, bpp, pack); + ++bandCount; + }); + EXPECT_GT(bandCount, 1) << "the cap did not band this read, so the comparison is vacuous"; + EXPECT_EQ(banded, whole); + } + } + } +} + // ===================================================================================== // R-17: the routing, its reply mailbox, and the arm that is actually installed // =====================================================================================