Compare commits

...
Author SHA1 Message Date
swung0x48 335637af38 [Docs] (Anland): record the GLX, Chrome and server-config fixes 2026-10-02 03:09:18 -04:00
swung0x48 4236b42a57 [Fix] (Server+Transpiler): configure the server once per process, and enable blend_func_extended for index-1 outputs 2026-10-02 03:09:17 -04:00
swung0x48 0b6842ac8b [Fix] (EGL+GL): let ANGLE's GL backend run on MobileGL (GBM displays, foreign wl_display, precision query, ES-free GL_VERSION) 2026-10-02 02:58:55 -04:00
swung0x48 47db68d557 [Feature] (GLX): serve as a glvnd GLX vendor and present remote X windows 2026-10-02 02:11:36 -04:00
swung0x48 ce11c9accd [Docs] (Anland): record the Chrome/dma-buf feedback fix 2026-10-02 01:45:03 -04:00
swung0x48 7bb9d9829f [Docs] (Anland): record the Plasma desktop milestone and the remaining issues 2026-10-02 01:38:57 -04:00
swung0x48 ccf8eb6571 [Fix] (Client): serialize multi-threaded GL streams, follow Wayland window resizes, export the KHR image aliases 2026-10-02 01:38:21 -04:00
swung0x48 0b314ddae7 [Fix] (P14): serialize sessions on one backend turn and give each client surface its own native surface 2026-10-02 01:38:20 -04:00
swung0x48 d5acece005 [Docs] (Anland): record the Plasma bring-up state after the P14 server-state fixes 2026-10-02 00:04:01 -04:00
swung0x48 30fef23d52 [Fix] (P14): scope the server staged stores and the context-live flag to the calling session 2026-10-02 00:04:00 -04:00
swung0x48 cbb87b968a [Docs] (Anland): drop the stale-glslang note and record d554c87c and the landed tooling 2026-10-01 23:36:27 -04:00
swung0x48 d554c87c45 [Fix] (Loader+Texture): tolerate drivers without EXT swap-with-damage, and pair stencil-only storage correctly
- eglSwapBuffersWithDamageEXT is an extension entry point that Android's
  framework libEGL does not export through dlsym; requiring it was a
  bring-up FATAL on TB321FU. Resolve EXT, fall back to the KHR spelling
  (same function, interchangeable signature), then to plain
  eglSwapBuffers (a zero-rectangle damage call is defined to be exactly
  that). The GLES dlsym fallback gains a self-reference guard for
  MobileGL installed under a GLES soname, where dlsym would otherwise
  hand the backend the frontend's own entry points and recurse forever.
- NormalizePixelFormat maps GL_STENCIL_INDEX8/GL_STENCIL_INDEX to
  GL_STENCIL_INDEX/GL_UNSIGNED_BYTE instead of falling through to
  RGBA/UBYTE, which no driver can pair with stencil-only storage.
- Tests pin the EXT->KHR->none preference order and the stencil pair.
- The anland handoff is rewritten to the post-P14 landed state: device
  evidence (win-smoke green screen, Konsole on screen, eglinfo/glxinfo),
  the remote-only recovery procedure, and the known gaps (Plasma session
  bring-up, cursor scale, the no-CPU-blit requirement, resize/surface
  loss, AVC audit).
2026-10-01 23:16:33 -04:00
swung0x48 7a48521d37 [Fix] (DirectGLES): destroy stale window surfaces before creating a new one
A session that dies without its teardown (crash, SIGKILLed client) leaks
its native window surface; the server-owned display window is one
ANativeWindow for the process's lifetime, and Adreno refuses a second
eglCreateWindowSurface on it with EGL_BAD_NATIVE_WINDOW - on TB321FU this
made every session after the first die with 'MobileGL could not acquire
the app Surface 300b'. InitWindowSurface now finds and destroys any
surface still bound to the window it is about to use, and the session
registry records the window with each surface to make that possible.
2026-10-01 22:32:53 -04:00
swung0x48 6946ff3a5c [Docs] (Anland): refresh the handoff to the post-P14 landed state
The document still described the state before the state-ownership work
landed: it called the unified server design unfinished, listed the
KernelSU grant as unsolved, said the directory bind had not taken effect,
and presented MobileGLAppsService plus the per-app fork worker as pending
drafts. None of that is true now.

Rewritten against the current tree and the device:

- header: architecture landed per the user's correction, acceptance still
  open; the rejected fork-worker/AppsService path is removed and the
  unified server lives in the MobileGL library (design 11-state-ownership,
  commits d52c031c / 4ed1c9ce / 5c6399e2 / ff212061 / 5a0f13fd, plus the
  landed 6fb880cd / ebc552a0 that used to be listed as uncommitted)
- MobileGL: HEAD moved to 5a0f13fd; the state-ownership section now records
  what S1-S6 landed and the debts that remain (process-level unit shadows,
  g_backendContextGeneration, per-record ContextSlot, the glIsBuffer probe,
  the window half of pbuffer->window)
- anland: 2ffa1f5 and 33e496c pushed; the draft inventory is now a landed
  inventory plus the three uncommitted KWin-acceptance fixes
- device: KernelSU grant resolved (reinstall changed the uid to 10270), the
  clone restarted with the directory bind effective, the 5a0f13fd APK
  serving @anland-mobilegl with six completed client sessions, and the
  client library installed with a stale build stamp
- new: concurrency warning (parallel sessions have overwritten the dist and
  the source tar), the current desktop-session.service failure state, and a
  rewritten continuation order

Verified: doc citations resolve (scripts/check_doc_citations.py), device
facts checked against HA27Q3LQ at refresh time.
2026-10-01 20:41:54 -04:00
swung0x48 5a0f13fdfa [Fix] (DirectGLES): advertise the GLSL 1.00-era ARB strings KWin gates on
GL_ARB_shader_objects/vertex_shader/fragment_shader and
GL_ARB_texture_non_power_of_two are core since GL 2.0 and trivially true
for the 4.6 this backend claims, but KWin 6's EglContext::checkSupported()
requires exactly these strings before it brings up compositing. Found on
TB321FU: kwin_wayland --anland failed with 'Could not initialize rendering
context' after a successful context create + makeCurrent on the
server-owned window surface.
2026-10-01 20:21:35 -04:00
swung0x48 ff2120616d [Feature] (P14): key the backend twin tables by session and share group
The last piece of state ownership this phase owed: the eight DirectGLES twin
tables resolved a handle process-wide, so two contexts of one session that do
NOT share a share group - which have independent name spaces, so the same
{slot, gen} in both is legal - were handed the SAME twin, driver ids,
persistent map and pooled store included. Two contexts that DO share a group
were served by whichever of them twinned first. Design and per-slice shape:
docs/Disaggregated/design/11-state-ownership.md (S6).

Key: DirectGLES.h's TwinKey{SessionKey, ShareGroupKey} - the same two words
the applier's object records are filed under, with the group derived by
MGPipeApplierShareGroupKeyFor so a record and the twin describing it can never
land in different buckets. Session is in the key because a twin names a driver
id and no driver id crosses a connection.

Resolution: SessionRuntime.cpp's ResolveThreadTwinKey, installed by
InstallSessionRuntimeHooks beside the S2 PipeInputs probe, the S4 native
tuple key and the S5 applier key. It reads the ThreadSessionScope's session
and CurrentContextToken() - apply-thread state only, never MG_State, so the
frontend-keyed-lookup prohibition on the apply thread still holds. A thread
that belongs to no session answers {0, 0}, which is the process singleton this
replaces; the monolith and every unit case are unchanged by construction.

Shape: SlotTables.h's SlotTableRegistry holds one SharedPtr bucket per key,
with a thread-local last-key memo. It forwards every handle-keyed entry -
GetOrCreate, FindByHandle, LiveGenAt, HandleOf, StateForHandle,
NoteStateForHandle, ReleaseByHandle, ForEachLive, the counters - to the
CALLING THREAD's bucket, so the ~150 call sites in Managers.cpp and
DirectGLES.cpp are literally unchanged. The holder list stays process-wide: a
death notice is about an object, not a bucket, and each shared holder is one
bucket. ForEachLive now walks the calling thread's bucket (what a context can
see is its own group); the death and diagnostic walks take the new
ForEachBucket.

DropEveryTwinForEndedServerSession is session-scoped: it runs on the apply
thread of the session that is ending, while S2 lets neighbour sessions keep
rendering in the same process. No session on the thread keeps the old
process-wide drop verbatim. Managers.h's "a texture is owned by exactly one
context (share groups are not implemented)" is corrected: a texture belongs to
a share group and its sibling contexts legitimately mutate it.

Tests: MG_Test/Wire/TwinOwnershipTest.cpp, five cases driving the real twin
tables with no EGL and no wire - the same handle in two nonshared groups is
two twins and neither overwrites the other; two contexts of one group resolve
ONE twin; a per-session drop takes only that session's; a thread with no
session still gets the process-wide table; and the six-registry template
repeats the first three. Negative control run and reverted: making the
registry answer a single {0, 0} bucket turns four of the five red.

Full host ctest against a stashed baseline, compared by test name: 0 new
failures. The one name in the difference set
(LogForwardChannel.APeerThatStopsReadingCostsWarnLinesNotTheCallersTime) is a
pre-existing flake, failing on the baseline too with the identical assertion.
The 103-case acceptance set plus the five new cases is 108/108 green.

Remaining debts (documented): the unit shadows (g_activeTextureUnit,
g_boundTexturesCache, g_boundSamplersCache) are still process-global; the
driver-level glIsBuffer probe is still not usable on this host, so sharing is
proven at the twin layer (the twin IS the object holding the driver id); the
backend context/sync generations are still process-wide; the per-record
ContextSlot is still S1's edge declaration.
2026-10-01 20:11:53 -04:00
swung0x48 5c6399e240 [Docs] (Design): record S5 landed shape and remaining twin-key debt 2026-10-01 19:23:57 -04:00
swung0x48 4ed1c9cef2 [Feature] (P14): state ownership - multi-session, multi-context, share groups
Breaks the one process = one server = one session = one context equation
that the whole stack's correctness used to rest on. Design and per-slice
shape: docs/Disaggregated/design/11-state-ownership.md.

S1 wire: bind_context ring record (opcode 84) announces the current
context at the make-current edge; CreateContext/DestroyContext join the
SurfaceOp control frames; control revision 5 -> 6; the server keeps a
per-session token table and refuses unknown tokens by name.

S2 server: SessionRuntime per session with thread-scoped resolution
(singletons become fallbacks), per-thread gPipeInputs, per-session latch
domains, per-thread segment resolvers; the in-process unix supervisor
serves up to MOBILEGL_IPC_INPROC_MAX_SESSIONS (16) concurrent sessions;
surface mode is declared per session and the display lease refuses a
second on-screen session by name.

S3 frontend: ShareGroupState owns the shared object stores and name
generators (buffer/texture/renderbuffer/sampler/program), GLContext keeps
per-context bindings/errors/container objects; pGLContext becomes
thread-local with a process default context preserving the single-context
world verbatim; eglCreateContext builds a real GLContext and joins the
sharing context's group.

S4 backend: native EGL tuples keyed by (session, context token);
InitDisplayAndContext's unconditional teardown is gone - creating a
second context or surface no longer destroys the first; eglCreateContext
sharing is wired to the native driver; bind_context is the native switch
point.

S5 applier: MGPipeApplierState splits into MGPipeObjectRecords (per
share group) and MGPipeWorkingState (per context), resolved through a
{session, token} registry with a thread-local cached fast path.

Tests: MultiSessionTest (incl. two sessions x two contexts interleaved
with real clear/readback witnesses), ApplierOwnershipTest (with a
null-resolver negative control), ContextStateOwnershipTest,
ServerLoopEglTest native-context liveness; 99/99 of the acceptance set
green on the host build.

Known debts (documented in the design doc): twin registries still keyed
by handle alone, no per-record ContextSlot, process-global backend
generations, MakeCurrent dedup per current binding.
2026-10-01 19:16:41 -04:00
swung0x48 ebc552a048 [Fix] (WSI): report Wayland present failure and keep GLX drawables off XIDs
Wayland Present() failure now returns EGL_BAD_SURFACE instead of being
dropped. In spawn mode a GLX X drawable is backed by a remote pbuffer so
the XID is never mistaken for an Android ANativeWindow; X11 pixel present
and remote resize are still unimplemented.
2026-10-01 13:45:52 -04:00
swung0x48 6fb880cdf2 [Build] (Linux): alias libGL.so and libGL.so.1 to libMobileGL
GLX clients link against libGL; give the Linux build the same alias
treatment libEGL already had so glvnd-less clients can load MobileGL.
2026-10-01 13:45:51 -04:00
swung0x48 d52c031c50 [Docs] (Design): state ownership for multi-session/context/share-group (P14)
The whole stack assumes one process = one server = one session = one
context = one share group. The anland unified server needs that equation
broken in the library instead of fork-per-app workers. This document fixes
the four ownership layers (session, context, share group, thread current),
the wire changes (per-record context slot, CreateContext/DestroyContext
SurfaceOps, context-tagged reverse events), and the staged plan P14a-d
with acceptance tests.
2026-10-01 13:45:41 -04:00
swung0x48 4156ca6e36 [Docs] (Anland): hand off unified runtime work and record corrected state ownership requirements 2026-10-01 13:18:23 -04:00
swung0x48 2f64ede8c6 [Feature] (Remote): serve concurrent shared-memory clients in isolated workers 2026-10-01 12:53:27 -04:00
swung0x48 a5b463a5e8 [Feature] (Remote): expose a package-neutral embedded display server API 2026-10-01 12:31:57 -04:00
BZLZHH 32c79f4135 [Feature] (EGL): Wayland window surfaces, presented through wl_shm
A Wayland client's window is its own wl_surface, and what the compositor shows is the wl_buffer
attached to it - but the pixels of a MobileGL frame are drawn where the backend is, on a split
build in another process on another OS.  So far a wl_egl_window was taken for an X11 window and
its client-side address sent to the Android server (EGL_BAD_NATIVE_WINDOW at best), and under
glvnd a Wayland display never reached this vendor at all.

A window on a Wayland display - asked for as EGL_PLATFORM_WAYLAND, or a wl_display recognised by
its first word being wl_display_interface - is now a window surface in the EGL state at the
wl_egl_window's size with a pbuffer of that size behind it in the backend, whatever
MOBILEGL_IPC_SURFACE says.  At every swap the frame is read back out of the default framebuffer
through this library's own glReadPixels (the application's read framebuffer, pack buffer and pack
state put back as they were), its rows flipped and swizzled into a wl_shm buffer, and attached,
damaged and committed on the application's wl_surface.  Three buffers rotate on their release
events, dispatched on an event queue of the surface's own.  libwayland-client is loaded at run
time, so neither the build nor the library depends on it.  EGL_KHR_platform_wayland and
EGL_EXT_platform_wayland are announced on Linux.

Measured: glmark2-wayland runs its build scene at 13 FPS (400x300) and the window shows the frame
under kwin.  A window resize is not followed yet; the surface keeps its creation size.
2026-10-01 12:25:20 -04:00
gold f458924360 [Fix] (EGL): a second display is served, and glvnd gets its answers from one table
Two defects, both found by asking the client directly rather than by reading its log.

The backend refused to initialize any EGLDisplay but the first - "already bound to a different
EGLDisplay" - and the field it compared against has no reader anywhere else in the tree: the domain
rule was the whole of the rule.  What it cost was a compositor, which asks for the display it will
draw into AFTER probing another one.  The probe succeeded, the real display was refused, and the
caller reported the refusal one layer up as "no usable DRM render device" - a message that names
where it stopped, not why.

The second: glvnd asks a vendor for entry points from two places.  getProcAddress is how it builds
its own static EGL table, and getDispatchAddress is how it answers eglGetProcAddress for every name
it does not know itself - which is every extension entry point, the device queries among them.  This
side answered the second from a GL-only table, so an application asking for eglQueryDeviceStringEXT
got a null for a function the vendor implements.
2026-10-01 12:22:55 -04:00
gold 0dad3fcaa1 [Feature] (EGL): MobileGL serves EGL through libglvnd
On a system with libglvnd the libEGL.so.1 an application links is a dispatcher, not an
implementation, and the supported way to be the implementation it talks to is to be a vendor
library it loads and routes to.  On this machine it is also the ONLY way that reaches a caller which
does not link EGL directly: kwin goes through libepoxy, which dlopen()s libEGL.so.1 and dlsyms its
entry points at startup, so a library injected with LD_PRELOAD is invisible to it however loudly it
is named - measured, with the client in kwin's maps and not one call into it.

So this is the vendor half of that ABI: __egl_Main answers glvnd with the entry points by name, the
platforms this implementation serves, and the device it is associated with - the render node of the
machine, because that is what lets a compositor match its own node to the display it is about to
draw into, and because glvnd drops a vendor that does not answer at all.

Two details are the difference between a vendor that is loaded and a vendor that is used.  glvnd
requires setDispatchIndex to exist even when a vendor has nothing to record, which this one has -
its entry points are the implementation, they never re-enter glvnd.  And a vendor must hand over ITS
OWN entry points: with default visibility a reference inside this library resolves against the whole
process, where another libEGL that loaded first wins.  That is what -Bsymbolic-functions is for;
declaring the entry points protected says the same thing but does not compile against the platform
headers, which declare them default.
2026-10-01 12:22:55 -04:00
gold 75f83b20fc [Feature] (EGL): the platform and device extensions are one list, spelled once
A vendor string and an eglQueryString answer have to agree, and they are read by two different
parties: the system's libglvnd reads the first to decide which vendor a platform request should be
routed to, and an application reads the second before it decides to make that request at all.  Kept
apart they drift, and the drift has a signature - a library that is loaded, initialized, and never
called, while the caller reports that it found no provider for a function it never reached.

The list gains the device family for the same reason: a compositor asks which devices exist and
reads the DRM node off one before it asks for a display on it, so an implementation that answers
"no devices" is one it cannot use no matter how well the implementation draws.
2026-10-01 12:22:54 -04:00
gold 57781688ce [Chore] (glvnd): vendor the EGL vendor ABI headers
Two MIT headers from libglvnd, verbatim, because MobileGL is about to implement the vendor half of
glvnd's EGL ABI and the distribution ships the runtime without them.
2026-10-01 12:22:54 -04:00
swung0x48 7308668488 [Docs] (Anland): kgsl-as-server.md - read with the kernel as the server, routes A and B are about who owns the buffer (6.x + Mesa is A, 5.x is B), plus the split-level analogy to virtio-gpu native context vs virgl / venus; linked from the README 2026-09-30 13:00:11 -04:00
swung0x48 134791b4b9 [Docs] (Anland): compare today's container Mesa (freedreno / turnip over kgsl) with routes A and B - it is route A's no-change dma-buf tier with the renderer in the app process, Anland's dma-buf ingress was shaped around it, and 5.x's private protocol sat between A and B 2026-09-30 12:45:16 -04:00
swung0x48 c24926cafe [Docs] (Anland): add route B - Anland hands the window's Surface to the MobileGL server (the FCL shape: SC mode leaves the parent Surface's queue free, waylandbridge itself receives it over binder), with its MobileGL items B1-B4, the Anland extension, its limits, and the phasing reordered around it 2026-09-30 12:24:24 -04:00
swung0x48 c5fa722be7 [Docs] (Anland): correct the window model - a toplevel attaches on demand to at most one Android window and several can be attached at once (popups and subsurfaces composite into the parent); P12 does not fit because the Android window and its input belong to Anland, not because of the window count 2026-09-30 12:17:56 -04:00
swung0x48 b7aa8d2011 [Docs] (Anland): research note - what MobileGL and Anland would each need for MobileGL to render Anland's Linux apps (glibc split client in the container, bionic render server, native AHB ingress into waylandbridge), with the two source-audit reports, their briefs and the notes index entry 2026-09-30 11:57:57 -04:00
swung0x48 378020adf3 [Fix] (MG_Remote, P11 T0): the T0 POST also checks what the CPU writes through the held lock after the import and its first GPU use, in both directions - Mali-G1-Ultra passed the old single round (the import cleans the CPU cache) while its GPU saw 0/1024 post-import words, so T0 was granted and create-indirect-in-world-align1024 x Magma lost every Flywheel object on inproc/spawn (0.867/0.848); it now fails there and runs T2 (0.999984), and the Redmi passes both rounds and keeps T0 2026-09-30 10:21:46 -04:00
swung0x48 028cf4eb62 [Merge] (trace): take dev's create-indirect-in-world-align1024 fixture (a derived trace replacing the original create-indirect capture) and register its documentation-only derived_from key 2026-09-30 10:21:46 -04:00
swung0x48 08124c99f1 [Test] (trace): replace the create-indirect fixture with minecraft-1.21.1-neoforge-create-indirect-in-world-align1024, a derived trace and not the original capture - the desktop capture bound Flywheel's scatter lists at 16-byte SSBO offsets that Adreno 830 and Mali's Vulkan driver (64) reject, and fixture_tools/realign_buffer_ranges.cpp moves the 1,270 binds off a 1024-byte boundary onto 1024-aligned slots (3,812 calls inserted, target call 504631 -> 508443, host SSIM unchanged to 9 digits on both backends) 2026-09-30 10:00:26 -04:00
swung0x48 648800a2da [Docs] (P8): close the phase - ruling ID-P8-19 (the exit device check and the P12 cross-host rerun), the P8 README and PLAN, the roadmap row and header, MEASUREMENTS row 19, the status page and the last debt 2026-09-30 08:25:38 -04:00
swung0x48 b4bee9dd38 [Docs] (P8): CONTRACT-P8 (the rules P8 landed, each with its code anchor and its test), rulings ID-P8-14..18 (MD + MF and the create-indirect fixture's alignment, SE, SV, the dev merge, the wave 2 integration gate) and the debts wave 2 hands on 2026-09-30 07:55:50 -04:00
swung0x48 494305381e [Merge] (P8): integrate wave 2 package MF - the push flush ladder gets dev's Adreno 830 queued-ring-copy guard (FlushPendingRangesFrom, create-instancing x Espryt x monolith 0.870 -> 0.99998 on the Redmi), and both G5 scripts pin the two reviewed ladder bodies under ID-P8-14 2026-09-30 07:12:50 -04:00
swung0x48 fb106a682e [Docs] (P8-MF): the package note - the push-ladder mirror of dev f973008c, create-instancing Espryt monolith 0.870 -> 0.99998 on the Redmi with spawn unchanged, why spawn was unaffected (the same two partial maps behind queued ring copies, the copy already submitted by the server's intervening whole-buffer maps), the red-onces and the gate 2026-09-30 07:09:28 -04:00
swung0x48 c8c4397475 [Merge] (P8): integrate wave 2 package SV - Magma's 3D native mip blit uses one layer, the depth/stencil mip decline gets its tally row, 1D depth mips decline by name, and the wire arm issues glDispatchComputeIndirect natively
# Conflicts:
#	MobileGL/MG_IntegrationTest/CMakeLists.txt
2026-09-30 06:11:04 -04:00
swung0x48 9c24a14d2b [CI] (scripts, P8-MF): pin G5's tenth row as well and re-pin both flush ladders on the reviewed post-fix bodies (ID-P8-14) - dev f973008c moved FlushPendingRangesNow past every base ref CI passes (the Adreno 830 queued ring copy) and ac9ab078 mirrors it into FlushPendingRangesFrom; both scripts' self-tests now run the pin-precedence and one-token controls on both ladders 2026-09-30 05:45:10 -04:00
swung0x48 ac9ab078cc [Fix] (Espryt, P8-MF): the push arm's flush ladder keeps a partial range-invalidating map behind an upload-ring copy still queued on the store, as dev f973008c's pull arm does - FlushPendingRangesFrom's ring tier stamps ringCopyRetireSerial and InvalidateFlushAccessFor refuses tier 1 for a partial range while that copy is unretired (the whole-buffer orphan-map stays exempt); Adreno 830 loses the queued copy and create-instancing on the monolith arm drew terrain sections empty 2026-09-30 05:43:55 -04:00
swung0x48 377d90d9f1 [Merge] (P8): integrate wave 2 package SE - the spawn/tcp default framebuffer publishes its surface to the client, the CPU mip arm declines on a driver-written base, D's case 3 runs on the Espryt split arms, the unmigrated-emulation name list is linted against its call sites, and the P8 notes' citations point at their content again 2026-09-30 05:24:11 -04:00
swung0x48 4cfb29a553 [Docs] (P8-SV): the note's gate table (G1 0/0, wire-declines 61/66, split_coverage, every lane, retrace 230/236) and the dirty-surface scan finding 2026-09-30 05:02:37 -04:00
swung0x48 51e95c9260 [Docs] (P8-SE): the note's gate and retrace readings 2026-09-30 04:47:27 -04:00
swung0x48 982958ebb1 [Test] (P8-SV): rename WireIndirectDispatchScenario's dispatch store member m_parameters -> m_groupCounts - gen_pipe_dirty_surface.py reads writes to shutter member names across all of MobileGL, and the name made NEW_PATCH_STATE's shutter look written outside its roots (self-test negative control 16 stopped tripping) 2026-09-30 04:42:06 -04:00
swung0x48 3717424229 [Merge] (P8): merge dev f973008c - the Espryt partial-flush fix for a queued ring copy on Adreno 830 (wave 2 MD), and dev's empty-binding uniform block fix deduplicated against P11's 7784d6bb (one code path), UnboundUniformBlockScenario registered on the split arms 2026-09-30 04:34:15 -04:00
swung0x48 f973008c4d [Fix] (Espryt): a partial buffer flush no longer takes the range-invalidating map while an upload-ring copy into the same store may still be queued - Adreno 830 loses the queued copy, and create-instancing drew whole Embeddium terrain sections empty 2026-09-30 04:07:41 -04:00
swung0x48 edcb9328e2 [Docs] (P8-SE): cite the integration CMakeLists by path - the bare basename resolves to the top-level file 2026-09-30 03:55:20 -04:00
swung0x48 e61060fbdf [Docs] (P8-SE): the package note, and the P8 package notes' line citations re-anchored by content at HEAD (A 14, B 29, C 10, D 12, E 18 plus the copy-image-shadow-mirror line, F 4) 2026-09-30 03:55:01 -04:00
swung0x48 3ce8ce3cb1 [Test] (P8-SE): the ForcedDs knob lanes run the default-framebuffer depth/stencil case again on all three arms (it was subtracted while red on spawn / tcp) 2026-09-30 03:55:01 -04:00
swung0x48 394d255487 [Docs] (P8-SV): the package note - the four items with their before/after, the design, the gates, ten red-once rows, the monolith observation and what was found but not fixed 2026-09-30 03:45:40 -04:00
swung0x48 0222852b94 [Fix] (Espryt, P8-SE): a spawn / tcp server publishes the default framebuffer's depth/stencil format (it has no frontend placeholder; the case leaves pending-fix for the three arms); the server CPU mip arm declines by name (generate-mipmap-store-declined) instead of filtering a store the driver wrote over, and E's refused-readback knob reaches it (CpuMipRefused. entries); WireIndirectDrawScenario case 3 runs on the Espryt split arms; copy-image-shadow-mirror goes and unmigrated_emulation_sites.py holds PipeCatalogueTest's list to the call sites 2026-09-30 03:44:29 -04:00
swung0x48 2edb2aa82a [Test] (P8-SV): WireMipShapeScenario (3D chain on both backends x every arm, 1D depth decline on Magma split+spawn, the 3D chain under the validation layer inproc) and WireIndirectDispatchScenario (both backends x every arm, Magma gated .IndirectDispatch.), the MipmapDepthStencilAspect tally in F1's depth/stencil decline case, WireDeclinePeek, and the two parity rows 2026-09-30 03:44:03 -04:00
swung0x48 fc415aee76 [Fix] (Magma, P8-SV): the wire arm's 3D native mip blit uses one layer with the depth in its z offsets, the depth/stencil mip decline gets its WireDeclineTally row (MipmapDepthStencilAspect), the depth shader mip refuses 1D/1D-array/3D view types before the chain grows, and glDispatchComputeIndirect is issued as vkCmdDispatchIndirect from the wire store (P8-D's barrier rule, wdsp counter, two named declines) 2026-09-30 03:44:03 -04:00
swung0x48 307267c188 [Docs] (P8): the monolith defect count is 12 (13 reports, one duplicate between C and D), 9 of them left to P13, not 14 and 11 2026-09-30 03:24:47 -04:00
swung0x48 cbbaf77f6d [Docs] (P8): wave 1 integrated - rulings ID-P8-5..13 (the survey, packages A-F, the integration gate, the wave 2 scope and the monolith defects left to P13), the survey note, open questions 9/11/15 answered, the debts and the P8 contract amendments, the P8 README table, PLAN-P8 wave 2 and the status page 2026-09-30 03:09:09 -04:00
swung0x48 39cd8fb73b [Merge] (P8): integrate package E - a texture readback the Espryt driver refuses is answered from the split server's staged store, which now follows glCopyImageSubData (FollowCopy, client-box merge, sticky driver-written mark, named decline texture-readback-store-declined); CopyImageStoreReadbackScenario on both backends x every arm plus the extent-scoped StoreRead knob; the dead CopyImageSubData+RENDERBUFFER latch goes (census 79 -> 78) 2026-09-30 02:17:51 -04:00
swung0x48 92eab9f3b7 [Merge] (P8): integrate package D - the Magma wire arm issues glDraw*Indirect / glMultiDraw*Indirect[Count] natively from the wire store (vkCmdDraw*Indirect[Count]) instead of a CPU expansion behind a GPU-idle wait, with an INDIRECT_COMMAND_READ barrier once per shader write; windr[] PipeStats counters and WireIndirectDrawScenario on both backends x every arm 2026-09-30 01:28:20 -04:00
swung0x48 9441071567 [Merge] (P8): integrate package C - the Espryt split server refreshes its staged copy of a buffer a shader wrote before a CPU reader consumes it (storage/atomic/buffer-image marks, XFB captures into the copy; restart rewrite, multi-draw rebase, *IndirectCount, CPU-fed indirect fields, XFB scatter and fp64 narrowing read through SplitHostBytesForCpuRead); GpuWrittenDrawInputScenario (12 cases) on the Espryt split arms 2026-09-30 00:45:16 -04:00
swung0x48 2f530ad8a2 [Merge] (P8): integrate package B - Espryt glGenerateMipmap on the server: the record's generated window (BASE/MAX clip, B1) and RGB16F/RGB32F natively or on the server's CPU from the driver's level (B2), retiring both split generate-mipmap names; GenerateMipmapServerScenario on both backends x monolith/Split/Spawn/Tcp plus the forced-CPU arm 2026-09-30 00:01:29 -04:00
swung0x48 c00f5eabae [Docs] (P8-E): the note's gate and retrace readings and two residuals (the extent-scoped tcp knob, the ERROR line before a store answer) 2026-09-29 23:51:49 -04:00
swung0x48 24b8aef616 [Fix] (P8 A): exempt F's Magma-only F1WireScenario.GenerateMipmapWithoutAShaderArmShapeDeclinesAndKeepsTheSession on DirectGLES as single-backend - it landed with P8-F after A's exemption table was cut, skips on Espryt like its F1Wire decline siblings, and split_coverage.py named it missing on the merged head 2026-09-29 23:41:10 -04:00
swung0x48 8133cd52ae [Merge] (P8): integrate package A - register the monolith-only cases on split/spawn/tcp (P8Coverage.cmake, five server-knob tcp supervisors), gate them with split_coverage.py and its exemption table, pin the tcp supervisor's Vulkan env 2026-09-29 23:37:12 -04:00
swung0x48 06f28b0a1b [Docs] (P8-A): the note says what the census measured about the audit's 396 and DoublePrecision on tcp, and the knob-port comment names the split/verify one-port overlap 2026-09-29 23:18:08 -04:00
swung0x48 b9feb9d7be [Docs] (P8-B): the package note - the generated window and the server CPU arm, the coverage matrix, seven red-onces and monolith defects M1-M3 recorded for dev 2026-09-29 23:16:05 -04:00
swung0x48 2b4ae539da [Merge] (P8): integrate package F - reclassify what our client cannot send: the multi-draw latches and Magma client-vertex-array become ProtocolCorruption, Magma mip shader-shape becomes a pre-growth named decline, unpublished cap bits and the index-mirror sentinel marked reserved 2026-09-29 23:07:03 -04:00
swung0x48 e815ce8d33 [Docs] (P8-C): the package note - readers and writers of the server's staged copy, the twelve GPU-written draw-input cases, each red-once, the retrace subset and the five monolith defects recorded for dev 2026-09-29 22:53:18 -04:00
swung0x48 00d6c2480e [Docs] (P8-E): the package note - the split read never touched a shadow, the store route and FollowCopy, the red-once table, the monolith defect for dev 2026-09-29 22:43:02 -04:00
swung0x48 f823422bfa [Fix] (Espryt, P8-E): a texture readback the driver refuses is answered from the server's staged store, which now follows glCopyImageSubData (FollowCopy, client-box merge, sticky driver-written mark, named decline); CopyImageStoreReadbackScenario on both backends x four arms plus the extent-scoped store-read knob; the dead CopyImageSubData+RENDERBUFFER latch goes (census 79 -> 78) 2026-09-29 22:43:02 -04:00
swung0x48 e52a997f34 [Fix] (Espryt, P8-C): the split server refreshes its staged copy of a buffer a shader wrote before a CPU reader consumes it - storage/atomic/buffer-image writes mark the server twin, XFB captures land in the copy, the restart rewrite, multi-draw rebase, *IndirectCount, CPU-fed indirect fields, XFB scatter and fp64 narrowing read through SplitHostBytesForCpuRead; GpuWrittenDrawInputScenario (12 cases) on the split arms 2026-09-29 22:38:33 -04:00
swung0x48 c7a14ed790 [Test] (P8-F): give the mip shader-shape decline its own .ShaderMipDecline. tail - spawn_lane_parity.py holds each server-knob tail to exactly one case, and sharing .ShaderMip1. named two 2026-09-29 22:37:49 -04:00
swung0x48 d639e54616 [Perf] (P8-D, Magma): the wire arm issues glDraw*Indirect / glMultiDraw*Indirect[Count] as vkCmdDraw*Indirect[Count] from the wire store instead of expanding them on the CPU behind a GPU-idle wait (create-indirect split: 321 waits / ~8 s -> 0), with an INDIRECT_COMMAND_READ barrier once per shader write; windr[] PipeStats counters, WireIndirectDrawScenario on both backends x every arm, notes/p8/D.md 2026-09-29 22:32:49 -04:00
swung0x48 32f222dbc2 [Docs] (P8-A): the package note - the 304-case census per arm, the server-knob lanes and their supervisors, the exemption classes, the red-once record, lane wall times and the findings 2026-09-29 22:30:20 -04:00
swung0x48 a2c81a2f64 [Fix] (P8-A): the tcp lane's shared supervisor carries the Vulkan ICD and the three lavapipe iterationRP repairs, which Magma reads in the server - DirectVulkan.Tcp IterationRPProgram203 missed its golden output by one texel without them 2026-09-29 22:30:20 -04:00
swung0x48 8dac1fd8a4 [Fix] (Espryt, P8-B): glGenerateMipmap on the server - the record's plan is the generated window (B1) and RGB16F/RGB32F generate natively or on the server's CPU from its own level (B2), retiring both split generate-mipmap names; GenerateMipmapServerScenario on both backends x monolith/Split/Spawn/Tcp plus the forced-CPU arm 2026-09-29 22:22:55 -04:00
swung0x48 b71cb9cc2c [Docs] (P8-F): cite MG_Backend/Init.cpp by path - the bare basename is ambiguous 2026-09-29 22:15:45 -04:00
swung0x48 efd3533bd1 [Docs] (P8-F): the package note - the four items, their evidence, the red-once record and what was found but not fixed 2026-09-29 22:15:29 -04:00
swung0x48 67890f523a [Refactor] (P8-F): reclassify what our client cannot send - the multi-draw client-indices/instanced latches and Magma's client-vertex-array become ProtocolCorruption, Magma's mipmap-shader-format-or-shape becomes a named decline decided before the chain grows (with a knob-forced 3D case on split and spawn), and the three unpublished cap bits and the index-mirror sentinel are marked reserved 2026-09-29 22:15:29 -04:00
swung0x48 976fbe4cd4 [Test] (P8-A): register the monolith-only integration cases on the split, spawn and tcp arms (P8Coverage.cmake: 246 ambient cases, five server-knob lanes each on its own tcp supervisor), retire the ClientSideIndices and GuiBatch exclusions, and gate the rest with split_coverage.py and its exemption table 2026-09-29 22:14:31 -04:00
swung0x48 8d3e2317a6 [Docs] (P8): re-scope the phase - strike the original scope in the roadmap and the P8 README, add PLAN-P8 (six items closed, packages P8-0 and A-G, the rewritten gates) and rulings ID-P8-1..4, and mark P8 in progress on the status page 2026-09-29 21:31:15 -04:00
swung0x48 0b55b55d4d [Docs] (P11): the status page lists the decisions waiting on the user - the P8 start and the six debt rulings 2026-09-29 21:17:21 -04:00
swung0x48 642ac891af [Docs] (P11): ruling ID-P11-20 - the closing head's gate, lanes and retrace numbers 2026-09-29 21:13:12 -04:00
swung0x48 592558f5fc [Docs] (Debts): the debt review - drop the closed E1, DualBlock and GetCaps-blobref rows (open question 24 closed, the blobref rule now a CONTRACT-P5 row-18 invariant), move the unmeasured M2 note to MEASUREMENTS, rewrite HyperOS / P11 RSS / named refusals, split the P12 row, merge the two CI rows, add three B2 rows, and mark each destination as needing a ruling or ready 2026-09-29 21:12:28 -04:00
swung0x48 0a117243d5 [Docs] (P11): close the phase - rulings ID-P11-17..19 (B2 T0 zero-copy, the stale indexed binding and Magma empty-uniform fixes with the skip-hole, T0 on by default), B2-DEVICE, the P11 README, the roadmap row and header, the status page and the debts P11 hands on 2026-09-29 20:33:00 -04:00
swung0x48 344e2f8439 [Docs] (P11 B2): T0 is the default (the user, 2026-09-29) - CONTRACT-P11 B2's tier table, unset versus 0, the revision-5 fix-up; the knob rows in design/09 2026-09-29 20:15:29 -04:00
swung0x48 91900bdc5e [Test] (P11 B2): the unset knob on a stream and on a no-AHardwareBuffer shared-segment session - T2, no W/I fallback line, the fallback counted; the 0x80 ask accepted and 0x81 refused 2026-09-29 20:15:29 -04:00
swung0x48 b32140e614 [Feat] (MG_Remote, P11 B2): T0 is the default - an unset MOBILEGL_IPC_ADOPT_TIER asks T0 (ask 0x80) and falls back to T2 quietly (MGLOG_D, still counted: client T0Fallbacks, server T0Refusals), while an explicit 0 keeps every named line; revision 5's schema comment and pin fixed up 2026-09-29 20:15:29 -04:00
swung0x48 7030d307be [Test] (Magma): UnboundUniformBlockScenario - a dispatch and a draw whose program declares a uniform block at an empty binding point (never bound, or its buffer deleted while bound) run and produce their fallback, and a buffer bound there afterwards is read, on both backends 2026-09-29 19:58:41 -04:00
swung0x48 853c5f1264 [Fix] (Magma): a uniform block declared at an empty binding point reads a zeroed block instead of dereferencing a null buffer 2026-09-29 19:55:53 -04:00
swung0x48 e44d6ac7da [Docs] (P11 B2): CONTRACT-P11 - a buffer deleted while bound to an indexed point: the defect, the fix, Magma's behaviour before it, the tests with their red-once, and the G1 delta 2026-09-29 19:44:08 -04:00
swung0x48 8571e0c001 [Test] (P11 B2): DeletedBoundBufferScenario - a buffer deleted while bound to a storage, uniform, atomic-counter or transform-feedback point, its slot reused, then a dispatch and a draw, on monolith and Split/Spawn/Tcp for both backends; and the one-process arena pair as a regression 2026-09-29 19:32:19 -04:00
swung0x48 7784d6bbf7 [Fix] (Magma): a uniform block declared at an empty binding point reads a zeroed block on the monolith arm instead of dereferencing a null buffer, as the wire arm already does 2026-09-29 19:32:19 -04:00
swung0x48 4aec5621a8 [Fix] (MG_State, P11 B2): glDeleteBuffers moves the bind-point generation of every indexed target it unbinds, and a transform-feedback object swap moves it too - the server's window no longer names a deleted buffer's handle after its slot is reused 2026-09-29 19:32:19 -04:00
swung0x48 4c02279f22 [Docs] (P11 B2): CONTRACT-P11 B2 - the Espryt split/spawn itest entries now pin MOBILEGL_ITEST_REQUIRE_GPU themselves 2026-09-29 19:12:46 -04:00
swung0x48 e869d18ce8 [Test] (P11 B2): pin MOBILEGL_ITEST_REQUIRE_GPU=1 on every Espryt split/spawn itest environment, as the Magma and Espryt TCP lists do - a handshake-time Fatal fails there instead of skipping as a ctest pass 2026-09-29 18:57:04 -04:00
swung0x48 9d1e788597 [Docs] (P11 B2): CONTRACT-P11 B2 - M2 staying green on Espryt is consistent with the driver holding its own reference, not verified 2026-09-29 18:44:25 -04:00
swung0x48 ce5753c883 [Docs] (P11 B2): CONTRACT-P11 B2 - the tier table that replaces A1's shared-segment Fatal, revision 5, the Offer and its thread, POST, the staged-copy audit, lifetime, CPU reads, host and device gates with their red-once 2026-09-29 18:24:53 -04:00
swung0x48 5e65907d63 [Test] (P11 B2): RemoteClientAdoptT0.KnobTwoIsTodaysT2WithNoT0Line - the default asks 2 and neither side settles, imports or logs T0 even where the stand-in platform could run it 2026-09-29 18:20:25 -04:00
swung0x48 98bf1925aa [Test] (P11 B2): LargeArenaAdoptionScenario - a draw still queued when the arena is rewritten or respecified keeps the bytes it was issued against (no readback between the two draws, so an in-place write or an early free shows) 2026-09-29 18:06:48 -04:00
swung0x48 17f1c0b59b [Tool] (P11 B2): extmem probe --hold-ms - the T0S child keeps its AHardwareBuffer locked while the parent holds both imports, for the dmabuf memory calibration 2026-09-29 17:50:50 -04:00
swung0x48 2522f1803c [Test] (P11 B2): T0 protocol on the host - the Offer inbox (late, out of order, stale, missing) over both aux channels, the whole T0 path on a stand-in platform, A1's two handshake-Fatal cases rewritten as named fallbacks, revision 4 refused by name, AdoptTier0 lanes on the shared-segment arms and the allow switch through spawn 2026-09-29 17:50:50 -04:00
swung0x48 7cde55b1c1 [Feat] (Magma, P11 B2): import a T0 store through VK_ANDROID_external_memory_android_hardware_buffer as a wire buffer (external VkBufferObject), serial-gated release, done-no-bytes readback, and the sustained-lock POST on the renderer's device 2026-09-29 17:50:50 -04:00
swung0x48 2a69f61c88 [Feat] (Espryt, P11 B2): import a T0 store with glBufferStorageExternalEXT as the adopted twin - no staged copy, server writes queued GPU-ordered, CPU readers on the import's coherent map, fence-gated release, re-import after a context loss, and the sustained-lock POST in a throwaway context 2026-09-29 17:50:50 -04:00
swung0x48 590d74525d [Feat] (MG_Remote, P11 B2): T0 behind MOBILEGL_IPC_ADOPT_TIER - control revision 5 (LinkTerms.adoptTier ask), kCapAdoptT0 granted at the first native bind after the backend's POST, the AHardwareBuffer Offer on the aux socket matched to its map_persistent record by seq, named T2 fallbacks instead of A1's handshake Fatal, the server allow switch, done-no-bytes CPU reads 2026-09-29 17:40:56 -04:00
swung0x48 b57276b88a [Tool] (extmem_probe, P11 B2): route mode - the probe serves in the render server's place (-Pmobilegl.extmemProbeAsServer) and --route-client sends a held-lock AHB over B1's fd: pair, directly on aux and through a socketpair hop 2026-09-29 16:44:37 -04:00
swung0x48 ac25e3323a [Tool] (extmem_probe): build inside the API-26 trace APK - memfd_create through syscall() below API 30, the two Vulkan 1.1 calls through vkGetInstanceProcAddr below API 28 2026-09-29 16:31:52 -04:00
swung0x48 96881f639c [Build] (P11 A3): MOBILEGL_BUILD_EXTMEM_PROBE / -Pmobilegl.buildExtmemProbe packages tools/spikes/extmem_probe as libMobileGLExtmemProbe.so in the trace flavour 2026-09-29 16:31:52 -04:00
swung0x48 16ea23c927 [Tool] (extmem_probe): marker/args-file mode for the trace APK exec hook, --regions-at-end, and the T0S sustained-lock rows (Vulkan copy/fill + GL compute on a never-unlocked client AHB) 2026-09-29 16:31:52 -04:00
swung0x48 b9b14c4775 [Docs] (P11): status page - the two handed-on dev bugs are merged, the gate row points at the B1 and M2 rulings 2026-09-29 16:14:34 -04:00
swung0x48 37c7aede31 [Docs] (P11): ruling ID-P11-16 on B1 (fd: endpoint, token broker pairing at hand-off, app_process helper, per-peer apply affinity, the HyperOS battery requirement, the device pass), the B1 debts, the status page, the phase README and the roadmap row 2026-09-29 16:14:18 -04:00
swung0x48 64b5794bc9 [Docs] (P11 B1): CONTRACT-P11 B1 - under the EOF Hello wait a second client slower than 2 s sees a send failure rather than Refuse{Busy}, and the unix-reach argument is the device's 2026-09-29 16:10:45 -04:00
swung0x48 ce4dca2835 [Docs] (P11 B1): CONTRACT-P11 B1 - the rebased head's device pass: rd12 shm vs tcp on both routes, Magma openra re-measured clean, the 10 s slow start and the plugin flavour 2026-09-29 16:06:30 -04:00
swung0x48 df826f426f [Docs] (P11 B1): CONTRACT-P11 B1 - the broker pairs at hand-off, a paired unix session waits for its Hello until EOF (the decision and its file:line), the lone-prime guard, the supervisor's announced core as the one source, and the follow-up gates 2026-09-29 15:03:52 -04:00
swung0x48 7ccb920ad2 [Feat] (Plugin, P11 B1): the broker presents the PairBind pair at hand-off (PairBindFrames, the encoder's bytes) and answers v2 with that flag and the apply core its supervisor announced; the helper passes MOBILEGL_IPC_FD_PAIRED=1 on, and Java no longer re-derives the reserved core 2026-09-29 15:03:52 -04:00
swung0x48 582adb0dd6 [Test] (P11 B1): the broker's PairBind templates against the encoder, a pair presented at hand-off whose Hello comes 5 s later, a pair presented only at program start refused by name, and the reserved core on lone-prime, symmetric and unreadable topologies 2026-09-29 15:00:30 -04:00
swung0x48 578795c84d [Feat] (MG_Remote, P11 B1): pairing at hand-off - the fd: endpoint presents no PairBind when MOBILEGL_IPC_FD_PAIRED=1 says the broker did, names the server's refusal when its own PairBinds arrive too late, and a paired unix session waits for its Hello until EOF instead of 10 s 2026-09-29 15:00:30 -04:00
swung0x48 9af4c8aefd [Feat] (MG_Remote, P11 B1): the reserved apply core never takes a lone prime core - several prime cores give the lowest, one gives a big core beside it or none, and the log names which - and the supervisor announces the core it reserves on stdout at start-up, the one source the broker relays 2026-09-29 15:00:30 -04:00
swung0x48 5a97eb8d1b [Docs] (P11 B1): CONTRACT-P11 B1 - anchor the evidence paths to ~/w7/notes/p11/evidence/b1 and the report 2026-09-29 14:47:31 -04:00
swung0x48 fc427a14d6 [Docs] (P11 B1): CONTRACT-P11 - the external-client route (helper, token broker, fd: endpoint), the token rule and the named refusals, the apply-thread policy by peer and plane with its selection measurements, the keep-alive setting users must make, the ROM dependence and the gates 2026-09-29 14:47:31 -04:00
swung0x48 54e7c92cf4 [Feat] (Plugin, P11 B1): the broker hands the helper the core the server reserves for the session's apply thread and the helper execs the program through taskset on every other core (--keep-affinity opts out), so a client the scheduler would put there is not starved 2026-09-29 14:47:31 -04:00
swung0x48 1abddf0d00 [Test] (P11 B1): ApplyThreadPolicy - every row of the auto table by peer and plane, the Redmi topology, the shapes with no prime pair to split, explicit masks, spins and unrecognised text 2026-09-29 14:47:31 -04:00
swung0x48 0819f9d410 [Feat] (MG_Remote, P11 B1): the apply thread's auto affinity and spin chosen per session from the peer's dial mode and data plane - a dialled-in shared-segment client leaves the apply thread only the lowest prime core, stream, forked and in-process peers keep every big core 2026-09-29 14:47:31 -04:00
swung0x48 952a1ca4a7 [Tool] (P11 B1): trace flavour gate tooling - package the retrace CLI as libMobileGLRetrace.so (packageRetraceExe) and run an external client as a child of the app kept in front (mobilegl_exec_argv) 2026-09-29 14:47:31 -04:00
swung0x48 bd05e061fc [Feat] (Plugin, P11 B1): same-device external clients - the exported token-checked broker that connects twice to the server's own endpoint and answers every request, the app_process helper that execs the program on the pair, a private broker listener beside a TCP-only supervisor, and the copyable launch command on the server screen 2026-09-29 14:47:31 -04:00
swung0x48 23a9470a28 [Test] (P11 B1): the fd: endpoint end to end against a real server process after two bare connects, its PairBind pair read off the server side, and its named refusals that write nothing 2026-09-29 14:47:31 -04:00
swung0x48 53098bc57f [Feat] (MG_Remote, P11 B1): the fd:<control>,<aux> client endpoint - adopt two connected unix sockets a broker opened for this process, refuse by name anything that is not a distinct connected AF_UNIX stream pair, and present the client's own PairBind pair before the Hello, over shared segments 2026-09-29 14:47:31 -04:00
swung0x48 76f75f3a0c [Docs] (P11): ruling ID-P11-15 on the M2 dev merge - the scattered-upload fix completed on the split arms, the trim fence re-audited on the split server, the census and ratchet notes; DEBTS closes the two merged dev bugs and takes M2 leftovers 2026-09-29 13:46:37 -04:00
swung0x48 d587d394fe [Docs] (P11): zero-copy stays switchable (ruling ID-P11-14) - MOBILEGL_IPC_ADOPT_TIER 2 (default) keeps today shared-memory push, 0 takes the T0 import, and an unusable T0 falls back to T2 with a named line; the server gets its own allow switch 2026-09-29 13:45:57 -04:00
swung0x48 de996d539f [Test] (P11 M2): DescriptorPoolGrowthScenario waits for the inproc server to apply every record it emitted before each census read - under run-ahead a census right after the burst's swap read the pools before the burst, or a trim a frame late 2026-09-29 13:29:38 -04:00
swung0x48 b4b1825951 [Docs] (P11 M2): TextureEmit.h and the applier's accumulation name the one box-only record that is not a whole level - the oversize fallback's withheld cover, a partial box spanning gaps 2026-09-29 12:36:41 -04:00
swung0x48 ec5f6ead3f [Test] (P11 M2): TextureEmit.MoreThanKMaxDirtyRectsCollapsesToTheBoxWithRegionCountZero keeps its name and pins the one collapse left - a footprint larger than a record carries falls back to the storage's rect list 2026-09-29 12:26:10 -04:00
swung0x48 f636a4cdae [Test] (P11 M2): the link ratchet records TextureObjectWithOneMipmap::GetStorageDirtyFootprint - dev 9524c688's new virtual on the placeholder vtable UniformManager already reaches (171 -> 172) 2026-09-29 12:26:10 -04:00
swung0x48 bd2e43ede3 [Test] (P11 M2): dev's two Magma scenarios on the split arms - both on Espryt's three arms, OversizedRecordingSplitScenario on Magma's three (and pinned to a 64-draw budget on split and spawn, where the server reads it), DescriptorPoolGrowthScenario's census cases on Magma's split arm only; the split-recording arming reads both role logs and dev's pinned lane holds its log's RESOURCE_LOCK, and the census cases count the sets the wire arm holds between its descriptor rewinds 2026-09-29 12:20:15 -04:00
swung0x48 e2333d98a8 [Fix] (Magma, P11 M2): DirectVulkan::GetDescriptorPoolCensus reads the server renderer's pools on the apply thread under inproc (the test probe, between drain batches) instead of racing its growth, rewinds and trims, and answers unavailable on spawn and tcp 2026-09-29 12:20:15 -04:00
swung0x48 5c9ad5b499 [Test] (P11 M2): RemoteGuards pins GetStorageDirtyFootprint on the apply thread as Fatal{RoleViolation, "texture-legacy-arm"}, beside the rest of the dirty model's rows 2026-09-29 12:16:55 -04:00
swung0x48 9145433890 [Fix] (P11 M2, Espryt, Magma): the texture emitter carries a level's exact dirty footprint as its regions (dev 9524c688) - a single partial box as one region, only a whole-level write as none - so both servers upload exactly the texels written over GPU content; the applier keeps it exact up to 4096 regions, and Espryt's handle arm applies the storage's box-or-rects cut-offs itself while its texture holds no GPU writes 2026-09-29 12:16:55 -04:00
swung0x48 c371599811 [Merge] (P11): dev@c14283ee - scattered sub-image writes upload exactly their texels, Magma submits a frame's recording every 16384 draws with backpressure, and Magma grows descriptor pools geometrically and trims a quiet slot whole; the handle arm notes driver-side writes by handle and the wire arm splits recordings and keeps the trim's epoch peak across descriptor rewinds 2026-09-29 12:11:37 -04:00
swung0x48 b34eed580d [Docs] (P11): HSPIKE - an app_process helper with no Context gets connected sockets to the server app over Binder under the shell uid and another app uid, token-checked; ruling ID-P11-13 on the single broker mechanism and the ROM caveat 2026-09-29 11:42:59 -04:00
swung0x48 c14283ee2d [Docs] (Magma): the trim threshold's comment states the bound its argument proves - a growth adds at most twice what the slot held, so the slot stays over a third full 2026-09-29 10:58:33 -04:00
swung0x48 9dccabf03c [Test] (Magma): descriptor pool growth and trim - the pure sizing policy adds O(log N) pools for an N-set frame, and DescriptorPoolGrowthScenario's 4096-distinct-set frame lands whole on both backends, grows at most 7 pools, and is given back after quiet frames and re-grown while they are still in flight 2026-09-29 10:34:16 -04:00
swung0x48 53fa16c2c2 [Fix] (Magma): a frame slot whose descriptor pools stay under a quarter full for 120 frames is handed back whole - its set cache cleared, grown pools destroyed, base pool reset and every memo holding a set handle dropped - so rd12's loading burst no longer lives for the whole process (live sets 15,042 -> 39, pools 65 -> 6); MOBILEGL_MAGMA_DESCRIPTOR_TRIM_FRAMES (0 = never) and a test-visible pool census 2026-09-29 10:34:16 -04:00
swung0x48 c2de6576ad [Fix] (Magma): a frame slot's next descriptor pool doubles its LARGEST pool of the same update-after-bind flavour, capped at 4096 sets - doubling the first (oldest, smallest) one made every grown pool the same size, so rd12's 15,016-set loading frame grew one slot to 60 pools (1x128 + 59x256); now 9 (64 ... 4096, 4096, 4096) 2026-09-29 10:34:15 -04:00
swung0x48 35e1c861a3 [Docs] (P11): ruling ID-P11-12 on pairing by PairBind nonce and revision 3 -> 4; B0 F2 corrected - the product TCP path already paired by identity, the defect was unix endpoints and the one-shot TCP server; the TCP fixture comment no longer names AcceptPair 2026-09-29 10:20:53 -04:00
swung0x48 9ebcf5a73d [Docs] (P11 PAIR): CONTRACT-P11 - the PairBind wire change and revision 3 -> 4, the acceptor's rules, cross-revision behaviour, the gates and the red-once 2026-09-29 10:19:59 -04:00
swung0x48 05dc94fa38 [Test] (P11 PAIR): PairAcceptorTest - a stray before a client, four interleaved connections, an orphaned aux past the budget and a Hello-first client, each on unix and tcp - and the device's probe-then-client case end to end in ServerSpawnTest 2026-09-29 10:19:59 -04:00
swung0x48 abe29eae2d [Fix] (MG_Remote, P11 PAIR): a socket client's control and aux connections pair by the nonce each presents first, not by arrival order - a readiness probe or a second client can no longer take half a session, and a client that sends its Hello first is refused by name 2026-09-29 10:19:59 -04:00
swung0x48 0a6af63655 [Feat] (P11 PAIR): the PairBind control message - the client's nonce and which of its two connections this is, the first frame on each - with the control revision 3 -> 4 and its pin 2026-09-29 10:19:59 -04:00
swung0x48 32b81dae42 [Docs] (P11): the two dev bugs P11 handed on are fixed on dev (Espryt scattered uploads 9524c688; Magma rd12 command-buffer growth d20c2d01) and arrive with the next dev merge 2026-09-29 09:52:37 -04:00
swung0x48 9c32d9fa68 [Test] (Magma): OversizedRecordingSplitScenario carries state across the split - a depth occluder, a stencil mark, an additive blend destination, a 4x multisample target resolved after it, occlusion and primitives-generated queries and a transform-feedback capture left open across it, each checked on both backends and in the budget-64 lane 2026-09-29 09:44:58 -04:00
swung0x48 4d3d677a7f [Test] (Magma): OversizedRecordingSplitScenario - a 1024-draw frame, one pixel and one uniform value per draw, lands whole on both backends; the DirectVulkan.SplitRecording lane pins the draw budget to 64 and asserts the split fired 2026-09-29 09:44:58 -04:00
swung0x48 d20c2d01ae [Fix] (Magma): a frame's recording is submitted every 16384 draws/dispatches and continues on a fresh command buffer, with at most frames-in-flight + 1 submissions outstanding - rd12's 981k-draw loading frame no longer grows one command buffer until Adreno's 16 KiB driver mappings exhaust vm.max_map_count (MOBILEGL_MAGMA_MAX_DRAWS_PER_COMMAND_BUFFER, 0 = unbounded) 2026-09-29 09:44:58 -04:00
swung0x48 2fcde6eb9b [Docs] (P11): B0-CROSS-APP - SELinux refuses a foreign context the connect to the server app unix endpoint, past a brokered connect the shared segments work unchanged, default placement makes shm slower than tcp; ruling ID-P11-11 on what B1 must build, and the HyperOS uid freeze as a debt 2026-09-29 09:34:26 -04:00
swung0x48 21c7e8cd63 [Test] (MG_IntegrationTest, MG_Test): scattered writes over a GPU clear on both backends - two distant 2x2 boxes, a hundred boxes, boxes filling 3/4 of their union and three quadrants spanning the level, the 3/4 and level-spanning cases again at the image-bindable re-mint, and a MipmapStorage footprint fuzz against a reference bitmap 2026-09-29 09:06:07 -04:00
swung0x48 9524c688ca [Fix] (Espryt, Magma): scattered glTexSubImage writes upload exactly their texels - MipmapStorage keeps an exact dirty footprint beside its rect list (a write log once a merge or a fold would widen the list past the writes), Magma always uploads it, Espryt uploads it box by box (through the unpack ring too) once the driver texture may hold GPU writes and merges it at the image-bindable re-mint; the union box, the 3/4 cut-off, the 96-rect fold and a level-spanning union no longer put the stale shadow over GPU-written texels (two 2x2 writes over a GPU clear: 41 of 64 texels red -> 0) 2026-09-29 09:06:07 -04:00
swung0x48 6481691d1e [Docs] (P11): the status page, the roadmap row and the phase README for P11 in progress, and ruling ID-P11-10 on absorbing origin@86dc38a0 2026-09-29 08:06:38 -04:00
swung0x48 9176dcff3d [Merge] (P11): origin feat/disaggregated@86dc38a0 - its eglSwapInterval port is dev c60639db (already merged) plus ForgetRequestedSwapInterval, so an ended Magma server session no longer hands its interval to the next 2026-09-29 08:05:57 -04:00
swung0x48 26de2b60fc [Docs] (P11): rulings ID-P11-1..9 with the integration gate, A-DEVICE (T0 is GO from the app domain on both backends; adoption buys RSS not frame time on the Adreno; split T2 costs RSS and p99), the debts the dev merge closes and the ones P11 hands on, and W2-REMINT marking the monolith re-mint fixed 2026-09-29 08:03:09 -04:00
swung0x48 82467823ef [Docs] (P11 C): the contract's A1/A2 sections, Config.h's GPU-ordered landing, Init.cpp's DirectVulkan resource table, and the stream refusal CONTRACT-P6 / design 06 / 07 claimed now cites its code 2026-09-29 07:23:41 -04:00
swung0x48 2a902cbd79 [Test] (P11 A1/A2): LargeArenaAdoptionScenario asserts the tier its arena landed in and reads the tcp AdoptTier0 refusal; registered on Split/Spawn/Tcp for both backends (Espryt through the split-arms macro, Magma's Arena. block and Buffers lane), the monolith lanes declare adopted 2026-09-29 07:23:41 -04:00
swung0x48 3c72e5c637 [Fix] (P11 A1): settle MOBILEGL_IPC_ADOPT_TIER at the handshake from the data plane - a stream refuses T0/T1 by name and runs T2 on either side, a client over shared segments dies by name before any record, the at-use check trusts the settlement; unit gates on a real tcp session and a real inproc handshake 2026-09-29 07:23:41 -04:00
swung0x48 1797db0070 [Docs] (P11): B0 drives the cross-app client first as a retrace executable run from adb shell, then re-checks the connection and fd passing from a retrace client inside an app, the domain a Termux-style client really runs in 2026-09-29 07:23:41 -04:00
swung0x48 01a33dfc58 [Docs] (P11): B0 tests the cross-app client as a retrace client in one app against a render server in another - no Termux route; the fallback channel is a bound Service in the server app handing over a socketpair end 2026-09-29 07:23:41 -04:00
swung0x48 93a72678ca [Docs] (P11): B is scheduled - the consumer is a GL program started from another app on the same device (Termux and the like); B0 probes a cross-app fd channel, B1 moves such a client onto shared-memory segments, B2 adds the T0 import 2026-09-29 07:23:41 -04:00
swung0x48 3cb3e32b87 [Test] (P11 M): ImageBindableRemintScenario pins a barrier before its imageLoad copy - the kept-allocation case read the pre-clear upload once on push-monolith under load, the ID-P9-11 shape 2026-09-29 06:47:11 -04:00
swung0x48 89f1e6cfba [Test] (P11 M): P4aFinalFixScenario expects no re-mint count on the monolith arm either - dev's 568090f0 keeps an immutable, unwidened allocation there as the server already did 2026-09-29 06:47:10 -04:00
swung0x48 2c7d99d5e8 [Test] (P11 M): the server half of a split eglSwapInterval - Magma's backend object carries the request into its table's SetSwapInterval (null before dev@c60639db) 2026-09-29 06:33:36 -04:00
swung0x48 9cde08064c [Test] (P11 M): dev's ImageBindableRemint and PartialUploadOverGpuContent scenarios on the Split/Spawn/Tcp arms of both backends, three scattered-upload cases for the handle arm's carried layout, and TextureRemintPullScenario's monolith Espryt skip retired 2026-09-29 06:30:54 -04:00
swung0x48 48a306025d [Fix] (Espryt, P11 M): the push builds' monolith re-mint merges the applier's pending boxes over the GPU readback - the client clears its dirty flags at emission there, so a pending glTexSubImage was read back over and lost 2026-09-29 06:30:54 -04:00
swung0x48 e0102bad8c [Merge] (P11): dev@c60639db - the Espryt image-bindable re-mint reads GPU levels back on the monolith arm too, partial writes into converted or reshaped levels upload only their region on every arm, Magma orders an upload after the recording's writes and queued clears, and Magma's eglSwapInterval reaches the swapchain 2026-09-29 06:15:56 -04:00
swung0x48 ffeed0da3a [Docs] (P11): the re-scoped plan - each roadmap item against the tree at 74ae652e; A makes the adopt tier honest (client-side verdict, the Stream refusal, a tier assertion, three arms) and re-runs open question 3 and the baseline on the device, B (T0) is conditional, C closes by docs 2026-09-29 05:52:51 -04:00
swung0x48 86dc38a07e [Fix] (Magma): eglSwapInterval reaches the swapchain - 0 picks IMMEDIATE, else MAILBOX, else FIFO; >= 1 FIFO; < 0 FIFO_RELAXED, else FIFO; rebuilt after the next present when the mode changes, and an ended server session forgets its interval (MC 26.3 inproc vsync on: flat 120) 2026-09-29 05:52:22 -04:00
swung0x48 c60639db1d [Fix] (Magma): eglSwapInterval reaches the swapchain - 0 picks IMMEDIATE, else MAILBOX, else FIFO; >= 1 FIFO; < 0 FIFO_RELAXED, else FIFO; rebuilt after the next present when the mode changes (MC 26.3 vsync on: 190-290 fps -> 120) 2026-09-29 05:51:18 -04:00
swung0x48 74ae652ecd [Docs] (P10): close the phase - CONTRACT-P10 sections 2-3, rulings ID-P10-1..14 with the integration gate, C-MEASUREMENTS (device fence bench, loopback credit 1/2/3, the retrace round-trip census), the roadmap row, the debts P10 hands on, design/09, the phase README and the status page 2026-09-29 05:29:00 -04:00
swung0x48 414dd09bda [Test] (P10): the fence bench comment - the replay corpus does carry timed fence waits (improved-transparency-minecraft-26.3), the bench isolates the shape 2026-09-29 05:29:00 -04:00
swung0x48 47bbf5e6fd [Docs] (P10 A): CONTRACT-P10 section 1 - the fence-report protocol, the present-time report, the gates and red-once - with the re-scoped plan and A-FENCE-POLL, the paired host bench and the llvmpipe finding 2026-09-29 04:34:16 -04:00
swung0x48 73089cf2b3 [Test] (P10 B): the caps mirror's parallel-shader-compile entry is the client's switch in both directions - the extension-list read-back case pins compilation inline, and a new case holds the reconcile (red once with it removed) 2026-09-29 04:34:02 -04:00
swung0x48 80d205c401 [Refactor] (Remote, P10 B): the emit table's SetSwapInterval slot is the server forwarder - SetSwapInterval_Unmigrated and the BackendObject_Remote override deleted, class C empty (69 emitted / 0 unmigrated), and a unit control that the slot crosses as one control frame 2026-09-29 04:34:02 -04:00
swung0x48 4373964443 [Test] (P10 B): AsyncCompileScenario (AsyncOn/AsyncOff/OptimisticShaderStatus), XfbPrimitiveQueryScenario and PrimitivesGeneratedNoXfbScenario on the split arms of both backends - Espryt Split/Spawn/Tcp, Magma Split/Spawn/Tcp gated, PrimGenReroute split+spawn only 2026-09-29 04:34:02 -04:00
swung0x48 2271f6c1ec [Fix] (Remote, P10 B): GL_KHR_parallel_shader_compile follows the client's compile switch - the caps mirror re-derives it on adoption, so a tcp server started with another MOBILEGL_ASYNC_SHADER_COMPILE no longer decides the client's string 2026-09-29 04:34:02 -04:00
swung0x48 8d0a1b6ca5 [Test] (P10 A): PacedFrameFenceWaitsBench - the per-frame timed wait on an older frame's fence as an opt-in measurement (MGITEST_FENCE_PACE_BENCH, _LAG) on both backends 2026-09-29 04:29:49 -04:00
swung0x48 625f4db47d [Feat] (MG_Remote, P10 A): a present reports the fences finished so far before it returns the credit, so the client the credit releases finds the fence of a frame ago already known 2026-09-29 04:29:49 -04:00
swung0x48 b1d60523e4 [Fix] (Magma): an upload into an image the open recording already wrote, or that still has a queued clear, is submitted after that work - a glTexSubImage after a clear or a draw into the level no longer ends up under it 2026-09-29 04:28:55 -04:00
swung0x48 8966d5c294 [Fix] (Espryt, Magma): a partial write into a level whose upload is converted (widened RGB formats, Magma's RGB->RGBA expansion) or reshaped (Espryt 1D arrays) uploads only its dirty region - the rest of the stale shadow no longer lands over GPU-drawn texels 2026-09-29 04:28:22 -04:00
swung0x48 bcff9b00cc [Feat] (MG_Remote): fence polls are answered from the server's reports - the apply thread posts kEventFenceSignaled as the GPU finishes each client fence (and submits and re-checks while idle), glClientWaitSync(0) / glGetSynciv answer locally, and only every MOBILEGL_IPC_POLL_ESCALATE-th unanswered poll crosses 2026-09-29 03:59:44 -04:00
swung0x48 de33d40173 [Docs] (P9): close the phase - the contract's remint section and invariants, rulings ID-P9-11..12 with the integration gate, the roadmap row, the debts P9 hands on, the phase README and the status page 2026-09-29 03:36:45 -04:00
swung0x48 7aa00761f8 [Docs] (P9 W2): W2-REMINT - no pull protocol, the census, the coverage matrix and the red-once record; design/04 8.4, the P5c/P5e contract amendments, open question 2 and the P7 census attribution 2026-09-29 03:24:03 -04:00
swung0x48 aa0c242b51 [Test] (P9 W2): TextureRemintPullScenario - fourteen re-mint cases on both backends x monolith/Split/Spawn/Tcp, and the forced-readback-failure fallback entries 2026-09-29 03:23:54 -04:00
swung0x48 6c2f8a560c [Fix] (Espryt, P9 W2): a texture re-mint never needs the client - read back only levels the driver holds, replay the staged level when the readback is refused, count trp on the server path, and retire the texture-remint-pull markers and OnTexturePullRequest (callbacks 9 -> 8) 2026-09-29 03:23:54 -04:00
swung0x48 568090f09f [Fix] (Espryt): the image-bindable re-mint reads every level back from the driver texture before replaying it, so GPU-written levels survive the first glBindImageTexture - iris-derivative SSIM 0.996196552 -> 0.996376198 2026-09-29 03:06:45 -04:00
swung0x48 738b29f3e4 [Docs] (P9): the contract's first three sections (pack-buffer readback, graded log forwarding, the glGetError observation point), both fault injections, the W1 note with its red-once evidence, and rulings ID-P9-1..10 2026-09-29 03:03:03 -04:00
swung0x48 a7a82ffee5 [Test] (MG_Remote): P9 W3 - LogForwardTest leaves no role logs in /tmp on a green run 2026-09-29 02:42:20 -04:00
swung0x48 24560ffe81 [Fix] (MG_Remote): P9 W3 - a FATAL forward waits once per stall rather than once per line (MGLOG_F also marks survivable conditions), and F2 proves the flood got through on a live connection, inside TCP_USER_TIMEOUT 2026-09-29 02:42:20 -04:00
swung0x48 6bcfb1fba1 [Feat] (MG_Remote): P9 W3 - the server's log forward carries each line's level and never blocks a logging thread (<=WARN dropped and counted, >=ERROR lossless and paced, FATAL a bounded flush), fault injection F2, and the producerless OnLog callback deleted (9 -> 8) 2026-09-29 02:42:20 -04:00
swung0x48 362daafeed [Fix] (MG_Remote): the pack-buffer readbacks check their whole shape before the read and refuse it at one latch site per sink, each with a raw-peer control, and a verb counts as statically barriered only when every op that stamps it waits 2026-09-29 02:21:36 -04:00
swung0x48 6485ae1dc0 [Feat] (MG_Remote): a read into a pixel pack buffer crosses as read_pixels_to_buffer / get_texture_image_to_buffer and waits for nothing - the server lands the rows through the buffer-write gate on both backends, the client marks the buffer GPU-written, and a device lost with the write still owed ends in Fatal{ReadbackDeclined, "buffer-writeback"} 2026-09-29 02:02:29 -04:00
swung0x48 ad705a7392 [Docs] (P9): the implementation hand-off - the roadmap row checked against the tree (half of it is done, one design premise changed), five work packages and two fault injections 2026-09-29 01:23:58 -04:00
swung0x48 acfd367852 [Docs] (P12): the FCL fork's keep-running setting is committed and pushed 2026-09-29 01:10:19 -04:00
swung0x48 743f4e8e6e [Docs] (P12): close the phase - the contract, the disposition of every leftover item, a G1 line that only compares same-machine builds, and the doc citation the docs reshuffle broke 2026-09-29 01:05:45 -04:00
swung0x48 6e35ea2c02 [Docs] (P12): the portrait-window clipping was the 0x0 window request, not Magma - root cause, fix and evidence 2026-09-29 00:54:46 -04:00
swung0x48 c932af7f3c [Fix] (EGLImpl): a server-owned window surface with no EGL_WIDTH/HEIGHT asks for the native window's size, so a landscape game is no longer clipped in a portrait server window 2026-09-29 00:54:46 -04:00
swung0x48 8a71ea5954 [Docs] (P12): exit gate (a) passes - FCL's Minecraft renders into the render server window on both backends and killing the server latches device-lost in tens of milliseconds 2026-09-29 00:27:29 -04:00
swung0x48 984e2b69f4 [Feat] (tools): the FCL exit-gate driver - Minecraft renders into the render server window, then the server is killed 2026-09-29 00:27:28 -04:00
swung0x48 f2ad2fae9b [Fix] (MG_Remote): once the device-lost latch has spoken, declined verbs no longer log one ERROR line each - an FCL game wrote 113,080 of them in 48 s 2026-09-29 00:27:28 -04:00
swung0x48 27cbc53241 [Docs] (P12): exit gate (b) passes - a WSL client replays the in-world trace over Wi-Fi TCP at ssim 0.999883; the load frame is bandwidth-bound, the steady state is latency-bound 2026-09-28 23:22:06 -04:00
swung0x48 b60da6729d [Feat] (tools): the cross-host acceptance driver and its report reducer, and a note that netbench's nc sink understates the link 2026-09-28 23:22:05 -04:00
swung0x48 b4f0c53426 [Docs] (Disaggregated): the cross-host eglMakeCurrent hang was the host's xray TUN proxy stalling its relay, not MobileGL; direct Wi-Fi runs pass and are bandwidth-bound 2026-09-28 22:57:20 -04:00
swung0x48 429b4472bb [Fix] (tools): cross-host runs first prove the WSL -> phone TCP path is end to end, and netbench's pkill no longer kills its own shell 2026-09-28 22:57:20 -04:00
swung0x48 221a8144c2 [Docs] (Disaggregated): sync stage progress and roadmap with the five-arm perf comparison and the cross-host hang blocker 2026-09-28 22:33:53 -04:00
swung0x48 1987bdff5c [Feat] (Android): the render server screen and binary ship in the plugin APK too, not just trace 2026-09-28 22:10:56 -04:00
swung0x48 2fa58940e4 [Fix] (ci): the trace APK is a launchable app; assert exactly one launcher activity
The "Trace APK must not expose a launcher activity" check predates the render-server
screen: 1f72597dd gave the trace APK a launcher on purpose and 884f6aac9 moved the entry
icon back to PostActivity with a bottom navigation bar between the two screens, so the
apk.yml build job has been red ever since - taking the whole retrace matrix down with it
(skipped, not failed), which hid the distinction.

What is left of the old isolation contract is the count: exactly one
android.intent.action.MAIN in the merged manifest (a second one means a screen grew a
launcher entry by accident), and it belongs to PostActivity. The fclPlugin metadata ban is
unchanged.
2026-09-28 21:54:16 -04:00
swung0x48 35dddc0c89 [Fix] (MG_Remote): the socket-io stats are LinkMetrics free functions, not StreamLink members
6bccf98d7 put TakeSendStats/TakeReadStats on the concrete StreamLink class, so
ClientSession.cpp and ServerLoop.cpp had to include <MG_Remote/Transport/StreamLink.h> -
which the P6.5 seam gate (scripts/ci/link_seam_purity.py, "concrete link included above
Transport") rejects, and the MGPipe generators and hygiene gates job has been red since.

The counters stay in StreamLink.cpp beside the recv/sendmsg loops that feed them; only the
struct and the two Take functions move, to LinkMetrics.h as LinkIoStats +
LinkMetricsTakeReadStats/TakeSendStats. The readers drop the concrete include; the hot
loops are untouched.
2026-09-28 21:54:08 -04:00
swung0x48 3bdd565dc6 [Chore] (docs): Remove orphaned CS_Refactor notes accidentally committed in 4e48702f2 2026-09-28 21:16:35 -04:00
swung0x48 884f6aac96 [Feat] (Android): trace APK boots to POST; bottom nav bar switches to the render server
The trace flavor manifest removed PostActivity and made ServerControlActivity
the launcher, so the APK opened straight into the render server screen. Keep
both screens: PostActivity is the launcher again, and a bottom navigation bar
(NavBar, plugin flavor unaffected) switches between them via
FLAG_ACTIVITY_REORDER_TO_FRONT with transitions suppressed, so each screen
keeps its state. ServerControlActivity is singleTop now and handles extras in
onNewIntent so adb-driven starts still work. The POST screen also shows an
indeterminate progress bar while the self-test runs.
2026-09-28 21:06:44 -04:00
swung0x48 b9a89967b0 [Docs] (Disaggregated): five-arm transport perf comparison and bottleneck attribution
rd12/openra on-device matrix (monolith/inproc/spawn+shm/spawn+tcp),
tcp-localhost transport_wait decomposition (present credit=1 ~74% +
futex wakeup tax; SPIN_US=2000 alone 2.9x), cross-host control-plane
hang evidence chain blocking P12 acceptance B, Wi-Fi bandwidth ceiling,
and the prioritized knob/code optimization list.
2026-09-28 20:55:45 -04:00
swung0x48 141227e8ba [Feat] (tools): add the disaggregated-mode device perf matrix and profiling harness
run_matrix.sh drives interleaved monolith/inproc/spawn+shm/spawn+tcp
benchmark rounds on an adb device with per-run arm-marker verification;
profile_matrix.sh layers simpleperf, ftrace wakeup capture and a /proc
sampler on top; make_report.py aggregates runs into REPORT.md. The wifi
kit carries the cross-host control-plane hang repro and phone-side
strace/fd-reconcile helpers. .trace-work/ (run output) is now ignored.
2026-09-28 19:36:30 -04:00
swung0x48 930c29e9e0 [Feat] (Android): the render server screen takes intent extras for prefill and autostart
listen/token/env/onscreen prefill the form, --ez start true starts the
server, --ez stop true stops it - adb drives the screen without the IME,
which is how the on-screen unix path (@name + shm, ServerOwned window)
was verified end to end from the UI.
2026-09-27 08:36:57 -04:00
swung0x48 c16fb5a8ac [Feat] (MG_Remote): the in-process display server serves unix endpoints too
mobilegl_server_serve_inprocess refused anything but tcp:// because its
only supervisor was the TCP one's thread hand-off. A unix endpoint
(@abstract name or filesystem path) now runs a supervisor of the same
shape minus the TCP exposure machinery: AcceptPair pairs a client's
control and SCM_RIGHTS connections by arrival order, there is no
DataBind to route and no pre-auth gate, sessions run on threads one at
a time with Busy for a second client, the latch resets between sessions
and g_inProcessStop ends it. That gives the on-screen server the
on-device fast path: ServerOwned windows over a unix socket with shm
data, no TCP copies.

The control screen's tcp-only guard for the on-screen mode goes with
the refusal it mirrored.
2026-09-27 07:43:56 -04:00
swung0x48 e3e890ed5d [Feat] (Trace): the replay runner builds as an adb-shell executable for Android, and unix endpoints carry a headless window
MOBILEGL_BUILD_TRACE_REPLAY now also produces mobilegl_trace_replay on
ANDROID: the same CLI as desktop, linked against the APK runner's
glws/glproc glue. On the device it dials a render server with no
Activity at all - MOBILEGL_IPC_CONTROL=unix:@name with shm data to the
offscreen supervisor (a cross-uid abstract connect needs root or an
SELinux waiver; loopback TCP works from plain adb shell), or
tcp:// with MOBILEGL_IPC_SURFACE=server to the on-screen display server.

glws_android grows the desktop glws_egl's server-owned null-window
createWindowSurface, gated on MOBILEGL_TRACE_SURFACE=window so the APK's
pbuffer mode keeps its old answer, and the config carries EGL_WINDOW_BIT
for it. The control screen refuses unix endpoints in on-screen mode:
the in-process display server serves tcp:// only.

Verified on-device with an OpenRA retrace, ssim=1.0 both ways:
offscreen service on @mgl-cli with the CLI as root, and the display
Activity on tcp://127.0.0.1:40613 with the replay's frames on the
phone's screen.
2026-09-27 06:28:00 -04:00
swung0x48 bf6feebb22 [Perf] (MG_Remote): The producer's transport park is timed as its own per-frame number, and a reducer pairs the two role logs into a frame-time breakdown
transport_wait_ns measures wall time in Ring::Wait's flush/spin/sleep, so
the client's parked time is read directly instead of inferred by
subtracting CPU clocks that run concurrently with the server.
frame_stats.py matches client and server present serials into per-frame
avg/p50/p99 splits; the README documents the MOBILEGL_PIPE_STATS=1
workflow.
2026-09-27 04:45:07 -04:00
swung0x48 f10db410e8 [Feat] (Android): the render server screen also starts the on-screen display server
An On-screen window checkbox switches Start from the offscreen service
to MobileGLDisplayActivity (process :mglwin), which serves in its own
process and renders a client's window surface onto its SurfaceView.
Stop ends either shape: stopService for the supervisor, killProcess for
:mglwin, and the running check covers both. Verified end to end with an
OpenRA retrace: the client dialed tcp://127.0.0.1:40613 with
MOBILEGL_IPC_SURFACE=server from a freeform window while the display
Activity held the screen, frames landed on the SurfaceView and the run
passed with ssim=1.0.
2026-09-27 04:39:01 -04:00
swung0x48 aaa50c88e1 [Feat] (Android): the render server screen takes unix endpoints, and is titled for what it serves
The endpoint field accepts @abstract names and filesystem paths beside
tcp://, matching what mobilegl_server_main's Listen already binds - on
Android the abstract namespace is the on-device rendezvous that always
works, and a relative path lands in the app's files dir, the service's
working directory. Unix endpoints skip the token rule (same-device
reach by construction) and are probed with LocalSocket instead of a
TCP connect. The screen is now titled MobileGL Render Server.
2026-09-27 04:39:00 -04:00
swung0x48 1f72597dd8 [Feat] (Android): a launcher screen starts and stops the offscreen TCP server
ServerControlActivity (trace flavor) collects the listen endpoint, auth
token and extra env, then drives MobileGLServerService with the same
extras adb used. Non-loopback listens are held to the 16-byte token the
server enforces, and a connect probe confirms the supervisor bound its
socket. The screen is the APK's launcher icon, which the trace flavor
lacked since PostActivity is removed there.
2026-09-27 04:39:00 -04:00
392 changed files with 41079 additions and 2970 deletions
+27 -2
View File
@@ -61,15 +61,40 @@ if ! grep -Eq '^lib/[^/]+/libMobileGL\.so$' <<<"$plugin_contents"; then
exit 1
fi
# The render server is not trace-only: both flavors carry the control screen, the offscreen
# service, the on-screen display activity, and (split native builds) the supervisor executable.
# The plugin APK ships to users, so its build keeps the three components private (manifest
# placeholder serverComponentsExported=false); only the debuggable trace APK exports them for adb.
for component in ServerControlActivity MobileGLServerService MobileGLDisplayActivity; do
require "$component" "$plugin_manifest" "plugin render server component ($component)"
require "$component" "$trace_manifest" "trace render server component ($component)"
done
if [[ "${MOBILEGL_CI_DISAGGREGATED:-OFF}" == "ON" ]]; then
trace_contents=$(unzip -Z1 "$trace_apk")
for entry in "plugin:${plugin_contents}" "trace:${trace_contents}"; do
if ! grep -Eq '^lib/[^/]+/libMobileGLServer\.so$' <<<"${entry#*:}"; then
echo "::error::${entry%%:*} APK does not contain libMobileGLServer.so (both flavors ship the render server)" >&2
exit 1
fi
done
fi
require 'top.mobilegl.plugin.trace' "$trace_manifest" 'trace package name'
require 'top.mobilegl.plugin.TRACE_REPLAY' "$trace_manifest" 'trace replay action'
if grep -Fq 'fclPlugin' <<<"$trace_manifest"; then
echo '::error::Trace APK must not advertise renderer-plugin metadata' >&2
exit 1
fi
if grep -Fq 'android.intent.action.MAIN' <<<"$trace_manifest"; then
echo '::error::Trace APK must not expose a launcher activity' >&2
# The trace APK IS a launchable app now: PostActivity (the POST self-test screen) is its
# entry icon and the bottom navigation bar switches to the render server screen. What is
# left of the old "no launcher" contract is the count - exactly ONE MAIN activity, so a
# screen that accidentally grows a launcher entry (ServerControlActivity was one for a day)
# reds here instead of shipping two icons.
main_count=$(grep -Fc 'android.intent.action.MAIN' <<<"$trace_manifest")
if [[ "$main_count" -ne 1 ]]; then
echo "::error::Trace APK must expose exactly one launcher activity (PostActivity); found ${main_count} android.intent.action.MAIN" >&2
exit 1
fi
require 'PostActivity' "$trace_manifest" 'trace launcher activity (PostActivity)'
echo 'Validated unified MobileGL plugin APK and isolated trace APK.'
+29
View File
@@ -1461,6 +1461,22 @@ jobs:
- name: Spawn/split lane parity (9.2)
run: python3 scripts/ci/spawn_lane_parity.py build-split
# P8-A (ID-P8-2): every monolith case, tails normalised, is on a gated split arm or is named
# with a reason class in scripts/data/split_coverage_exemptions.txt. The same `ctest -N`
# cost as the parity step above, and the question it cannot answer: parity compares the
# split arms with EACH OTHER, so a case missing from all three passes it.
- name: Split coverage against monolith (P8-A)
run: |
python3 scripts/ci/split_coverage.py --self-test
python3 scripts/ci/split_coverage.py build-split
# P8-SE: PipeCatalogueTest's unmigrated-emulation list equals the MGPipeUnmigratedEmulation
# call sites in the tree, one site per name. A source lint: no build needed.
- name: Unmigrated-emulation list against its call sites (P8-SE)
run: |
python3 scripts/ci/unmigrated_emulation_sites.py --self-test
python3 scripts/ci/unmigrated_emulation_sites.py
- name: Unit tests on the split runtime
working-directory: build-split
env:
@@ -2042,6 +2058,13 @@ jobs:
"PersistentCoherentMapScenario.TwoWritesThroughTheCoherentPointerEachReachTheirOwnDraw",
"PersistentCoherentMapScenario.AWriteAfterAFrameBoundaryReachesTheNextFramesDraw",
"StorageBufferRegrowScenario.AGrownStoreIsVisibleThroughItsExistingIndexedBinding",
# P11 A2: the >= 16 MiB arena and its tier assertion (emulated).
"LargeArenaAdoptionScenario.SubDataAfterAnInFlightDrawReachesTheNextDraw",
"LargeArenaAdoptionScenario.RespecifiedVertexArenaKeepsVaoBindings",
"LargeArenaAdoptionScenario.RespecifiedIndexArenaKeepsVaoBinding",
"LargeArenaAdoptionScenario.ReadbackSeesTheLatestCpuWrite",
"LargeArenaAdoptionScenario.GpuWriteIntoTheArenaIsReadBack",
"LargeArenaAdoptionScenario.TheArenaLandsInTheTierItsLaneDeclares",
]
slp.require_green(json.load(open(sys.argv[2])), sys.argv[3],
["DirectVulkan.Split.Buffers." + name for name in cases])
@@ -2104,6 +2127,12 @@ jobs:
"PersistentCoherentMapScenario.TwoWritesThroughTheCoherentPointerEachReachTheirOwnDraw",
"PersistentCoherentMapScenario.AWriteAfterAFrameBoundaryReachesTheNextFramesDraw",
"StorageBufferRegrowScenario.AGrownStoreIsVisibleThroughItsExistingIndexedBinding",
"LargeArenaAdoptionScenario.SubDataAfterAnInFlightDrawReachesTheNextDraw",
"LargeArenaAdoptionScenario.RespecifiedVertexArenaKeepsVaoBindings",
"LargeArenaAdoptionScenario.RespecifiedIndexArenaKeepsVaoBinding",
"LargeArenaAdoptionScenario.ReadbackSeesTheLatestCpuWrite",
"LargeArenaAdoptionScenario.GpuWriteIntoTheArenaIsReadBack",
"LargeArenaAdoptionScenario.TheArenaLandsInTheTierItsLaneDeclares",
]
required = [sys.argv[4] + name for name in cases]
if sys.argv[5] == "tcp":
+1
View File
@@ -28,3 +28,4 @@ tools/trace_replay/work/
__pycache__/
*.py[cod]
/.gradle
/.trace-work/
+11
View File
@@ -0,0 +1,11 @@
# glvnd vendor ABI headers
'libeglabi.h' and 'GLdispatchABI.h' from libglvnd (MIT), copied verbatim with their own licence
headers intact.
They are here because MobileGL implements the *vendor* half of glvnd's EGL ABI - the side a library
exposes so the system's 'libEGL.so.1', which IS glvnd, can dispatch to it. The distribution ships
the runtime but not these headers in the base image, and a vendored copy of two MIT headers is a
smaller dependency than a build-time download of a source tree.
Upstream ABI version implemented: 'EGL_VENDOR_ABI_MAJOR_VERSION' 0 (the header pins it).
+141
View File
@@ -0,0 +1,141 @@
/*
* Copyright (c) 2013, NVIDIA CORPORATION.
*
* Permission is hereby granted, free of charge, to any person obtaining a
* copy of this software and/or associated documentation files (the
* "Materials"), to deal in the Materials without restriction, including
* without limitation the rights to use, copy, modify, merge, publish,
* distribute, sublicense, and/or sell copies of the Materials, and to
* permit persons to whom the Materials are furnished to do so, subject to
* the following conditions:
*
* The above copyright notice and this permission notice shall be included
* unaltered in all copies or substantial portions of the Materials.
* Any additions, deletions, or changes to the original source files
* must be clearly indicated in accompanying documentation.
*
* THE MATERIALS ARE PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
* MATERIALS OR THE USE OR OTHER DEALINGS IN THE MATERIALS.
*/
#include <GL/gl.h>
#if !defined(__GL_DISPATCH_ABI_H)
#define __GL_DISPATCH_ABI_H
#if defined(__cplusplus)
extern "C" {
#endif
/*!
* \defgroup gldispatchabi GL dispatching ABI
*
* This is not a complete ABI, but rather a fragment common to the libEGL and
* libGLX ABIs. Changes to this file should be accompanied by a version bump to
* these client ABIs.
*/
/*!
* Thread-local implementation used by libglvnd. This is passed into the patch
* function callback via the type parameter.
*
* For most architectures, the vendor library can ignore this parameter, since
* it will always be the same value. It's used for systems like ARM, where the
* stubs might be use the ARM or Thumb instruction sets.
*
* The stub type does not make any distinction between TLS and TSD stubs. The
* entire purpose of entrypoint rewriting is to skip the dispatch table in
* libGLdispatch.so, so it doesn't matter how that dispatch table is stored.
*/
enum {
/*!
* Indicates that the stubs aren't defined in assembly. For example, if the
* dispatch stubs are written in C. Vendor libraries generally won't see
* this value.
*/
__GLDISPATCH_STUB_UNKNOWN,
/*!
* Used for stubs on x86 systems.
*/
__GLDISPATCH_STUB_X86,
/*!
* Used for stubs on x86-64 systems.
*/
__GLDISPATCH_STUB_X86_64,
/*!
* Used for stubs on ARMv7, using the Thumb instruction set.
*/
__GLDISPATCH_STUB_ARMV7_THUMB,
/*!
* Used for stubs on ARMv7, using the normal ARM instruction set.
*/
__GLDISPATCH_STUB_ARMV7_ARM,
/*!
* Used for stubs on ARMv8/aarch64.
*/
__GLDISPATCH_STUB_AARCH64,
/*!
* Used for stubs on x32 builds (x86-64 with 32-bit pointers).
*/
__GLDISPATCH_STUB_X32,
/*!
* Used for stubs on PPC64 systems.
*/
__GLDISPATCH_STUB_PPC64,
/*!
* Used for stubs on PPC64LE systems. Same as PPC64, for compatibility.
*/
__GLDISPATCH_STUB_PPC64LE = __GLDISPATCH_STUB_PPC64,
/*!
* Used for stubs on LOONGARCH/loongarch64.
*/
__GLDISPATCH_STUB_LOONGARCH64,
};
/*!
* A callback function called by the vendor library to fetch the address of an
* entrypoint.
*
* The function returns two pointers, one writable and one executable. The two
* pointers may or may not be the same virtual address, but they will both be
* mappings of the same physical memory.
*
* The vendor library should write its entrypoint to the address returned by
* \p writePtr, but should use the address from \p execPtr for things like
* calculating PC-relative offsets.
*
* Note that if this function fails, then the vendor library can still try to
* patch other entrypoints.
*
* Note that on ARM, the low-order bit of both \c execPtr and \p writePtr will
* be zero, even if the stub uses the thumb instruction set. The vendor library
* should use the \c type parameter of \c initiatePatch to determine which
* instruction set to use.
*
* \param funcName The function name.
* \param[out] writePtr The pointer that the vendor library can write to.
* \param[out] execPtr The pointer to the executable code.
* \return GL_TRUE if the entrypoint exists, or GL_FALSE if it doesn't.
*/
typedef GLboolean (*DispatchPatchLookupStubOffset)(const char *funcName,
void **writePtr, const void **execPtr);
#if defined(__cplusplus)
}
#endif
#endif // __GL_DISPATCH_ABI_H
+464
View File
@@ -0,0 +1,464 @@
/*
* Copyright (c) 2013, NVIDIA CORPORATION.
*
* Permission is hereby granted, free of charge, to any person obtaining a
* copy of this software and/or associated documentation files (the
* "Materials"), to deal in the Materials without restriction, including
* without limitation the rights to use, copy, modify, merge, publish,
* distribute, sublicense, and/or sell copies of the Materials, and to
* permit persons to whom the Materials are furnished to do so, subject to
* the following conditions:
*
* The above copyright notice and this permission notice shall be included
* unaltered in all copies or substantial portions of the Materials.
* Any additions, deletions, or changes to the original source files
* must be clearly indicated in accompanying documentation.
*
* THE MATERIALS ARE PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
* MATERIALS OR THE USE OR OTHER DEALINGS IN THE MATERIALS.
*/
#if !defined(__LIB_EGL_ABI_H)
#define __LIB_EGL_ABI_H
#include <stdint.h>
#include <EGL/egl.h>
#include <EGL/eglext.h>
#include "glvnd/GLdispatchABI.h"
#if defined(__cplusplus)
extern "C" {
#endif
/*!
* \defgroup eglvendorabi EGL Vendor ABI
*
* Definition of ABI exported by libEGL.so to libEGL_VENDOR.so libraries.
*
* Each vendor is associated with three distinct dispatch table types:
*
* - static EGL dispatch table: this is the fixed list of EGL 1.5 entrypoints
* provided by the vendor at load time during the initial handshake.
* - dynamic EGL dispatch table: this is a structure allocated by the API
* library at runtime used to manage EGL extension functions which are not
* present in the static table.
* - core GL dispatch table: this is a structure maintained by the API library
* which contains both GL core (static) and GL extension (dynamic) functions.
*
* @{
*/
/*
* Rendering API handling:
*
* libEGL only supports OpenGL and OpenGL ES, not OpenVG. If OpenVG or any
* other API is added, then the major version number will be incremented.
*
* When the application calls eglBindAPI, libEGL will forward the call to every
* vendor library. In addition, a vendor library can query the current API from
* libEGL using the getCurrentApi callback.
*
* Vendor libraries are not required to support both GL and GLES, but they must
* be able to deal with either one as the current rendering API. If a vendor
* doesn't support the current API, then it should return an error from
* eglCreateContext.
*/
/*!
* Current version of the ABI.
*
* This version number contains a major number in the high-order 16 bits, and
* a minor version number in the low-order 16 bits.
*
* The major version number is incremented when an interface change will break
* backwards compatibility with existing vendor libraries. The minor version
* number is incremented when there's a change but existing vendor libraries
* will still work.
*/
#define EGL_VENDOR_ABI_MAJOR_VERSION ((uint32_t) 0)
#define EGL_VENDOR_ABI_MINOR_VERSION ((uint32_t) 2)
#define EGL_VENDOR_ABI_VERSION ((EGL_VENDOR_ABI_MAJOR_VERSION << 16) | EGL_VENDOR_ABI_MINOR_VERSION)
static inline uint32_t EGL_VENDOR_ABI_GET_MAJOR_VERSION(uint32_t version)
{
return version >> 16;
}
static inline uint32_t EGL_VENDOR_ABI_GET_MINOR_VERSION(uint32_t version)
{
return version & 0xFFFF;
}
/*!
* This opaque structure stores function pointers for EGL extension functions.
* It is allocated at runtime by the API library. Vendor-provided dispatch
* functions retrieve and operate on this structure using the API below.
*/
typedef struct __EGLvendorInfoRec __EGLvendorInfo;
/****************************************************************************
* API library exports *
****************************************************************************/
typedef struct __EGLapiExportsRec {
/************************************************************************
* The following routines are used by vendor-implemented EGL dispatch
* functions to lookup and call into the right vendor.
************************************************************************/
/*!
* This function must be called at the start of every EGL dispatch stub. It
* performs any necessary per-call bookkeeping.
*/
void (* threadInit) (void);
EGLenum (* getCurrentApi)(void);
/*!
* This function retrieves the appropriate current dynamic dispatch table,
* if a GL context is current. Otherwise, this returns NULL.
*/
__EGLvendorInfo *(*getCurrentVendor)(void);
/*!
* This retrieves the current context for this thread.
*/
EGLContext (*getCurrentContext)(void);
/*!
* Returns the current display for this thread.
*/
EGLDisplay (*getCurrentDisplay)(void);
/*!
* Returns the current drawable for this thread.
*/
EGLSurface (*getCurrentSurface)(EGLint readDraw);
/*!
* This function retrieves an entry point from the dynamic dispatch table
* given an index into the table.
*/
__eglMustCastToProperFunctionPointerType (*fetchDispatchEntry) (__EGLvendorInfo *dynDispatch, int index);
/*!
* Sets the last error for the current thread. The error will be returned
* the next time the app calls eglGetError().
*
* This function will override a call to \c setLastVendor, and vice-versa.
*/
void (* setEGLError) (EGLint errorCode);
/*!
* Notifies libEGL about the vendor library that an EGL function is
* dispatched to. This is used to look up the last EGL error code from a
* vendor.
*
* A vendor library may generate an EGL error asynchronously, if it
* offloads some EGL functions onto a worker thread. In that case, calling
* \c setEGLError to set the last error would not work.
*
* When an EGL dispatch stubs finds the vendor library, it must call this
* function before it dispatches the function call.
*
* This function will override a call to \c setEGLError, and vice-versa.
*
* \param vendor The vendor library of the current EGL function call.
* \return EGL_TRUE on success, or EGL_FALSE on failure.
*/
EGLBoolean (* setLastVendor) (__EGLvendorInfo *vendor);
/************************************************************************
* These routines are used by vendor dispatch functions to look up
* and add mappings between various objects and vendors.
************************************************************************/
// As far as I can tell, all EGL functions that take a context or drawable
// also take an EGLDisplay. So, we don't need any extra functions for
// mapping a context/surface/whatever to a EGLDisplay or vendor.
/*!
* Returns the EGL vendor for an EGLDisplay handle.
*/
__EGLvendorInfo *(*getVendorFromDisplay)(EGLDisplay dpy);
/*!
* Returns the EGL vendor for an EGLDeviceEXT handle.
*/
__EGLvendorInfo *(*getVendorFromDevice)(EGLDeviceEXT dev);
/**
* Sets the EGL vendor for an EGLDeviceEXT handle. The dispatch stub for
* any function that returns an EGLDeviceEXT handle should call this.
*
* Supported since ABI version 1.2.
*/
EGLBoolean (* setVendorForDevice)(EGLDeviceEXT dev, __EGLvendorInfo *vendor);
} __EGLapiExports;
/*****************************************************************************
* API library imports *
*****************************************************************************/
/*!
* The enum values accepted by \c __EGLapiImports::getVendorString.
*/
enum
{
/*!
* The set of platform extensions that the vendor supports.
*
* Platform extensions typically just add enums to the existing
* eglGetPlatformDisplay function, so we don't need any additional logic
* or special handling from libEGL itself.
*
* As a result, vendor libraries can add these extensions without having
* to modify libEGL to match.
*/
__EGL_VENDOR_STRING_PLATFORM_EXTENSIONS,
};
/*!
* This structure stores required and optional vendor library callbacks.
*/
typedef struct __EGLapiImportsRec {
/**
* Creates an EGLDisplay. This function is used to handle both
* \c eglGetDisplay and \c eglGetPlatformDisplay.
*
* For some platform types, libEGL may be able to identify a specific
* vendor to use.
*
* If libEGL can't figure out which vendor to use on its own, then it will
* go through the list of available vendor libraries and call this function
* until one succeeds.
*
* If the application calls eglGetPlatformDisplay, then the parameters are
* passed through from the application.
*
* If the application calls eglGetDisplay, and the native display handle is
* not \c EGL_DEFAULT_DISPLAY, then libEGL will try to guess a platform
* type, and will pass that platform to the vendor library.
*
* Lastly, if eglGetDisplay is called with \c EGL_DEFAULT_DISPLAY, then
* libEGL will call into each vendor library with \p platform set to
* \c EGL_NONE. The vendor library can then select a default display to
* return.
*
* In all cases, if the vendor library can't find a matching EGLDisplay,
* then it should return \c EGL_NO_DISPLAY. Any errors should be reported
* through the vendor's \c eglGetError function.
*/
EGLDisplay (* getPlatformDisplay) (EGLenum platform, void *nativeDisplay,
const EGLAttrib *attrib_list);
/*!
* Checks if the vendor library supports a given client API (that is, the
* API value passed to \c eglBindAPI).
*/
EGLBoolean (* getSupportsAPI) (EGLenum api);
/*!
* (OPTIONAL) Returns a string from the vendor library. This is used for
* anything that isn't available from eglQueryString.
*/
const char * (* getVendorString) (int name);
/*
* NOTE: libEGL will load all of the vendor libraries up front, so it
* handles extension functions slightly differently than EGL.
*/
/*!
* This retrieves the pointer to the real EGL or core GL function.
*
* \param procName The name of the function.
* \return A pointer to a function, or \c NULL if the vendor does not
* support the function.
*/
void * (* getProcAddress) (const char *procName);
/*!
* This retrieves vendor-neutral functions which use the
* __EGLdispatchTableDynamic API above to dispatch to the correct vendor.
*
* A vendor library must provide a dispatch function for all EGL display
* extension functions that it supports.
*
* Client extension functions cannot be dispatched based on an EGDisplay,
* so they must be handled in libEGL itself.
*
* \param procName The name of the function.
* \return A pointer to a function, or \c NULL if the vendor does not
* support the function or \p procName is not a EGL display function.
*/
void * (*getDispatchAddress) (const char *procName);
/*!
* This notifies the vendor library which dispatch table index is
* assigned to a particular EGL extension function.
*/
void (*setDispatchIndex) (const char *procName, int index);
/*
* The vendor library may use the isPatchSupported, initiatePatch,
* releasePatch, and patchThreadAttach callbacks to re-write libglvnd's
* entrypoints at make current time, provided no other contexts are current
* and the TLS model supports this functionality. This is a performance
* optimization that may not be available at runtime; the vendor library
* must not depend on this functionality for correctness.
*
* To use this optimization, the vendor library must provide at least the
* isPatchSupported and initiatePatch entrypoints.
*
* Note that these functions are identical to the ones used in GLX.
*/
/*!
* (OPTIONAL) Checks to see if the vendor library supports patching the
* given stub type and size.
*
* \param type The type of entrypoints. This will be a one of the
* __GLDISPATCH_STUB_* values.
* \param stubSize The maximum size of the stub that the vendor library can
* write, in bytes.
* \param lookupStubOffset A callback into libglvnd to look up the address
* of each entrypoint.
*/
GLboolean (* isPatchSupported)(int type, int stubSize);
/*!
* (OPTIONAL) Called by libglvnd to request that a vendor library patch its
* top-level entrypoints.
*
* The vendor library should use the \p lookupStubOffset callback to find
* the addresses of each entrypoint.
*
* This function may be called more than once to patch multiple sets of
* entrypoints. For example, depending on how they're built, libOpenGL.so
* or libGL.so may have their own entrypoints that are separate functions
* from the ones in libGLdispatch.
*
* Note that during this call is the only time that the entrypoints can be
* modified. After the call to \c initiatePatch returns, the vendor library
* should treat the entrypoints as read-only.
*
* \param type The type of entrypoints. This will be a one of the
* __GLDISPATCH_STUB_* values.
* \param stubSize The maximum size of the stub that the vendor library can
* write, in bytes.
* \param lookupStubOffset A callback into libglvnd to look up the address
* of each entrypoint.
*
* \return GL_TRUE if the vendor library supports patching with this type
* and size.
*/
GLboolean (*initiatePatch)(int type,
int stubSize,
DispatchPatchLookupStubOffset lookupStubOffset);
/*!
* (OPTIONAL) Called by libglvnd to notify the current vendor that it no
* longer owns the top-level entrypoints.
*
* Libglvnd will take care of the restoring the entrypoints back to their
* original state. The vendor library must not try to modify them.
*/
void (*releasePatch)(void);
/*!
* (OPTIONAL) Called at the start of window-system functions (GLX and EGL).
* This callback allows vendor libraries to perform any per-thread
* initialization.
*
* This is basically a workaround for broken applications. A lot of apps
* will make one or more invalid GLX/EGL calls on a thread (often including
* a MakeCurrent with invalid parameters), and then will try to call an
* OpenGL function.
*
* A non-libglvnd-based driver would be able to initialize any thread state
* even on a bogus GLX call, but with libglvnd, those calls wouldn't get
* past libGLX.
*
* This function is optional. If it's \c NULL, then libGLdispatch will
* simply ignore it.
*
* \note This function may be called concurrently from multiple threads.
*/
void (*patchThreadAttach)(void);
/*!
* (OPTIONAL) Tries to determine the platform type for a native display.
*
* If the vendor library provides this function, then libglvnd will call it
* to determine which platform to use for a native display handle in
* eglGetDisplay.
*
* If no vendor library identifies the platform, then libglvnd will fall
* back to its own platform detection logic.
*
* Libglvnd can call this function for any native display handle except
* \c EGL_DEFAULT_DISPLAY.
*
* No matter what the value of \p native_display, the vendor library must
* not crash, and must not return a false match. If the vendor library
* can't identify the display, then it must return \c EGL_NONE.
*
* In particular, that means that a vendor library must not return any sort
* of default or fallback platform.
*
* \param native_display The native display handle passed to eglGetDisplay.
* \return Either a platform type enum or EGL_NONE.
*/
EGLenum (* findNativeDisplayPlatform) (void *native_display);
} __EGLapiImports;
/*****************************************************************************/
#define __EGL_MAIN_PROTO_NAME "__egl_Main"
typedef EGLBoolean (* __PFNEGLMAINPROC) (uint32_t version, const __EGLapiExports *exports,
__EGLvendorInfo *vendor, __EGLapiImports *imports);
/*!
* Vendor libraries must export a function called __egl_Main() with the
* following prototype.
*
* This function also performs a handshake based on the ABI version number.
* Vendor libraries can optionally use the version number to support older
* versions of the ABI.
*
* \param[in] version The ABI version. The upper 16 bits contains the major version
* number, and the lower 16 bits contains the minor version number.
*
* \param[in] exports The table of functions provided by libEGL. This pointer will
* remain valid for as long as the vendor is loaded.
*
* \param[in] vendor The opaque pointer used to identify this vendor library. This
* may be used in future versions to provide additional per-vendor information.
*
* \param[out] imports The function table that the vendor library should fill
* in. The vendor library must assign every non-optional function in the
* struct.
*
* \return True on success. If the vendor library does not support the
* requested ABI version or if some other error occurs, then it should return
* False.
*/
EGLBoolean __egl_Main(uint32_t version, const __EGLapiExports *exports,
__EGLvendorInfo *vendor, __EGLapiImports *imports);
/*!
* @}
*/
#if defined(__cplusplus)
}
#endif
#endif /* __LIB_EGL_ABI_H */
+95 -2
View File
@@ -29,6 +29,8 @@ option(MOBILEGL_BUILD_DISAGGREGATED "Build the MG_Remote transport layer (two-pr
# only decides whether the shims are compiled in.
option(MOBILEGL_BUILD_DISAGGREGATED_INPROC "Compile the in-process (one-process, two-role) split shims; implies MOBILEGL_BUILD_DISAGGREGATED" OFF)
option(MOBILEGL_BUILD_SERVER_SPIKE "Build the P0 spike-A MobileGLServer delivery-chain executable (Android only)" OFF)
option(MOBILEGL_BUILD_EXTMEM_PROBE "Package tools/spikes/extmem_probe as libMobileGLExtmemProbe.so for the trace APK's exec hook (P11 A3, Android only)" OFF)
option(MOBILEGL_EXTMEM_PROBE_AS_SERVER "Spike only (P11 B2 step 1): also package the probe as libMobileGLServer.so, the real server as libMobileGLServerReal.so" OFF)
# The PipeInputs strangler (ARCHITECTURE.md 9.2). OFF is the pull build and must stay
# byte-identical to a tree without either option: MGB_CTX is the live GLContext, no
# MGPipe/PipeInputs source is compiled, every MGP_FILL is ((void)0).
@@ -397,11 +399,15 @@ set(SOURCE_FILES
MobileGL/MG_Util/Texture/TextureFormatProcessor.cpp
MobileGL/MG_Impl/GLXImpl/Exporting/Definitions.cpp
MobileGL/MG_Impl/GLXImpl/Exporting/GlvndVendor.cpp
MobileGL/MG_Impl/GLXImpl/GLXImpl.cpp
MobileGL/MG_Impl/GLXImpl/LookUp/LookUp.cpp
MobileGL/MG_Impl/EGLImpl/Exporting/Definitions.cpp
MobileGL/MG_Impl/EGLImpl/Exporting/GlvndVendor.cpp
MobileGL/MG_Impl/EGLImpl/EGLImpl.cpp
MobileGL/MG_Impl/EGLImpl/WaylandWindow.cpp
# @INSERTION_POINT:SOURCE_FILE_GLIMPL@ #
MobileGL/MG_Impl/GLImpl/Sampler/Validators.cpp
@@ -459,6 +465,7 @@ set(SOURCE_FILES
MobileGL/MG_Backend/DirectVulkan/Renderer/VkRenderPassManager.cpp
MobileGL/MG_State/GLState/Core.cpp
MobileGL/MG_State/GLState/ShareGroupState.cpp
MobileGL/MG_State/EGLState/Core.cpp
MobileGL/MG_State/GLState/ErrorState/Error.cpp
MobileGL/MG_State/GLState/BufferState/BufferState.cpp
@@ -607,9 +614,17 @@ if (MOBILEGL_BUILD_DISAGGREGATED)
MobileGL/MG_Remote/Transport/InProcessTransport.cpp
MobileGL/MG_Remote/Transport/SocketTransport.cpp
MobileGL/MG_Remote/Transport/ControlInbox.cpp
# P9 W3: the server's log forward - a queue and a sender thread, graded by severity
# (<= WARN lossy, >= ERROR lossless and paced), so no logging thread writes the socket.
MobileGL/MG_Remote/Transport/LogForward.cpp
# Keeps MG_Util/Debug/Log.h - and through it the GL frontend's
# umbrella header - out of the header-only wire code (WireLog.h).
MobileGL/MG_Remote/Transport/WireLog.cpp
# P11 A1: the adopt tier settled at the handshake from the knob and the data plane, by
# both roles (so SERVER-partition code in the link ratchet, not the client's tracker).
MobileGL/MG_Remote/Transport/AdoptTier.cpp
# P11 B2: T0 - the client's AHardwareBuffer store and its hop descriptor (both roles).
MobileGL/MG_Remote/Transport/AdoptT0.cpp
# ---- P5: the three new directories ------------------------------
# Wire/ the G3 codec: MGPWireRec_* in and out of SEG_CMD, blobs and
# var-tails in and out of SEG_STAGE. [w1]
@@ -649,7 +664,16 @@ if (MOBILEGL_BUILD_DISAGGREGATED)
MobileGL/MG_Remote/Server/ServerSession.cpp
MobileGL/MG_Remote/Server/PipeApplier.cpp
MobileGL/MG_Remote/Server/ServerLoop.cpp
# P14 S2: the per-session runtime and the thread routing that lets one process serve
# several sessions (the real session a singleton accessor answers with on that
# session's own threads).
MobileGL/MG_Remote/Server/SessionRuntime.cpp
MobileGL/MG_Remote/Server/ServerMain.cpp
# P11 PAIR: a socket client's control and aux connections paired by the nonce each
# presents first (PairBind), not by arrival order - both unix supervisors take pairs here.
MobileGL/MG_Remote/Server/PairAcceptor.cpp
# P11 B2: T0's Offers on the aux socket, matched to their map_persistent records by seq.
MobileGL/MG_Remote/Server/AdoptInbox.cpp
MobileGL/MG_Remote/Server/ServerSpawn.cpp
# P5f fc: the value frame that replaced the control mailbox's function-pointer payload,
# and its codec against protocol.fbs's SurfaceOp/SurfaceReply (the spawn arm's seam).
@@ -659,6 +683,8 @@ if (MOBILEGL_BUILD_DISAGGREGATED)
# (the display Activity's SurfaceView on Android; nobody installs one on the host, so a
# ServerOwned request is refused by name there).
MobileGL/MG_Remote/Server/ServerDisplay.cpp
# Package-neutral C ABI for an APK that owns the server and its ANativeWindow.
MobileGL/MG_Remote/Server/EmbeddedServer.cpp
# P7 gate 5 (g5-msprobe): the Magma wire arm's two multisample depth/stencil resolve
# arms and the probe that measures the first against the second, shared by the
# renderer's bring-up and the Vulkan POST. Wire-only - the monolith records neither arm -
@@ -682,7 +708,7 @@ if (ANDROID)
MobileGL/MG_Util/SelfTest/DriverBenchJni.cpp
)
if (MOBILEGL_BUILD_DISAGGREGATED)
# P12 (on-screen server window): the trace APK's MobileGLDisplayActivity - the in-process
# P12 (on-screen server window): the APK's MobileGLDisplayActivity - the in-process
# display server's thread, its SurfaceView's ANativeWindow in and out of ServerDisplay, and
# the geometry up-call. Split build only: nothing else has an in-process server to drive.
list(APPEND SOURCE_FILES
@@ -763,6 +789,9 @@ if (MOBILEGL_BUILD_DISAGGREGATED)
# target, and above all no flatc in the build graph. protocol_generated.h
# is committed and regenerated by scripts/gen_protocol.py.
list(APPEND MOBILEGL_INCLUDE_DIR ${CMAKE_SOURCE_DIR}/3rdparty/flatbuffers/include)
# The vendored glvnd vendor-ABI headers (3rdparty/glvnd): the entry point a vendor library
# exposes so the system libEGL - which IS glvnd - can dispatch to it.
list(APPEND MOBILEGL_INCLUDE_DIR ${CMAKE_SOURCE_DIR}/3rdparty/glvnd/include)
endif()
add_library(${CMAKE_PROJECT_NAME} SHARED
@@ -809,7 +838,7 @@ target_compile_definitions(${CMAKE_PROJECT_NAME}
if(UNIX AND NOT APPLE AND NOT ANDROID)
foreach(MOBILEGL_LOADER_ALIAS
libEGL.so libEGL.so.1)
libEGL.so libEGL.so.1 libGL.so libGL.so.1 libGLX_mobilegl.so.0)
add_custom_command(TARGET ${CMAKE_PROJECT_NAME} POST_BUILD
COMMAND ${CMAKE_COMMAND} -E create_symlink
"$<TARGET_FILE_NAME:${CMAKE_PROJECT_NAME}>"
@@ -873,6 +902,19 @@ if (TRACY_ENABLE)
target_compile_definitions(${CMAKE_PROJECT_NAME}_s PUBLIC -DTRACY_ENABLE)
endif ()
# GLVND, AND WHY THIS IS A LINKER FLAG RATHER THAN A VISIBILITY ONE.
#
# MobileGL can serve as a glvnd EGL vendor - the library the system's libEGL.so.1 dispatches to -
# and a vendor must hand glvnd ITS OWN entry points: glvnd calls whatever address it is given, so
# an address that resolves to another libEGL in the process is a call back into the dispatcher.
# With default visibility every reference inside this library resolves against the whole process,
# and a process that loaded the system libEGL first wins. Marking the entry points protected says
# the same thing in the source, except the platform's own GL/EGL headers declare them default and
# the two visibilities do not mix; this flag is that promise made by the linker instead.
if (UNIX AND NOT APPLE)
target_link_options(${CMAKE_PROJECT_NAME} PRIVATE "LINKER:-Bsymbolic-functions")
endif()
if (ANDROID)
target_link_libraries(${CMAKE_PROJECT_NAME} PUBLIC
android
@@ -982,6 +1024,12 @@ if (ANDROID AND MOBILEGL_BUILD_INTEGRATION_TEST)
add_subdirectory(MobileGL/MG_IntegrationTest)
endif()
# The replay runner also builds for Android: an adb-shell executable that can
# dial a render server on the device (unix or TCP) without any Activity.
if (ANDROID AND MOBILEGL_BUILD_TRACE_REPLAY)
add_subdirectory(tools/trace_replay)
endif()
# ---------------------------------------------------------------------------
# P0 spike A: the Android delivery chain for a second native executable.
#
@@ -1090,3 +1138,48 @@ if (ANDROID AND MOBILEGL_BUILD_SERVER_SPIKE)
RUNTIME_OUTPUT_DIRECTORY "${CMAKE_LIBRARY_OUTPUT_DIRECTORY}")
endif()
endif()
if (ANDROID AND MOBILEGL_BUILD_EXTMEM_PROBE)
# P11 A3 (open question 3): the spike-B external-memory probe, exec'd from the trace
# app's own process by the spike hook (`--es mobilegl_spike_spawn
# libMobileGLExtmemProbe.so`), so its rows answer for the untrusted_app domain. The
# same PIE-executable-named-like-a-library shape as MobileGLServerSpike above; it
# links nothing from MobileGL.
add_executable(MobileGLExtmemProbe
${CMAKE_CURRENT_SOURCE_DIR}/tools/spikes/extmem_probe/extmem_probe.cpp)
set_target_properties(MobileGLExtmemProbe PROPERTIES
PREFIX "lib"
SUFFIX ".so"
OUTPUT_NAME "MobileGLExtmemProbe"
POSITION_INDEPENDENT_CODE ON)
target_compile_options(MobileGLExtmemProbe PRIVATE -fPIE)
target_link_options(MobileGLExtmemProbe PRIVATE -pie)
target_link_libraries(MobileGLExtmemProbe PRIVATE vulkan EGL GLESv3 android log)
if (CMAKE_LIBRARY_OUTPUT_DIRECTORY)
set_target_properties(MobileGLExtmemProbe PROPERTIES
RUNTIME_OUTPUT_DIRECTORY "${CMAKE_LIBRARY_OUTPUT_DIRECTORY}")
endif()
if (MOBILEGL_EXTMEM_PROBE_AS_SERVER)
# P11 B2 step 1, spike APKs only: MobileGLServerService execs `libMobileGLServer.so
# <endpoint> --serve` and B1's broker connects to whatever that listens on, so the probe
# takes the server's file name and answers "does a client's AHardwareBuffer cross this
# route into the server app" on the real route (tools/spikes/extmem_probe, route mode).
if (TARGET MobileGLServer)
set_target_properties(MobileGLServer PROPERTIES OUTPUT_NAME "MobileGLServerReal")
endif()
add_executable(MobileGLExtmemProbeAsServer
${CMAKE_CURRENT_SOURCE_DIR}/tools/spikes/extmem_probe/extmem_probe.cpp)
set_target_properties(MobileGLExtmemProbeAsServer PROPERTIES
PREFIX "lib"
SUFFIX ".so"
OUTPUT_NAME "MobileGLServer"
POSITION_INDEPENDENT_CODE ON)
target_compile_options(MobileGLExtmemProbeAsServer PRIVATE -fPIE)
target_link_options(MobileGLExtmemProbeAsServer PRIVATE -pie)
target_link_libraries(MobileGLExtmemProbeAsServer PRIVATE vulkan EGL GLESv3 android log)
if (CMAKE_LIBRARY_OUTPUT_DIRECTORY)
set_target_properties(MobileGLExtmemProbeAsServer PROPERTIES
RUNTIME_OUTPUT_DIRECTORY "${CMAKE_LIBRARY_OUTPUT_DIRECTORY}")
endif()
endif()
endif()
+51 -10
View File
@@ -132,6 +132,18 @@ namespace MobileGL::MG_Config {
Bool MagmaR11G11B10FFallback = false;
// MOBILEGL_MAGMA_FRAMESINFLIGHT: requested Magma frames in flight, defaulting to 3.
Uint32 MagmaFramesInFlight = 3;
// MOBILEGL_MAGMA_MAX_DRAWS_PER_COMMAND_BUFFER: GL draws and dispatches Magma records into
// one command buffer before it submits it and continues on a fresh one. 0 = unbounded.
// Drivers back a command buffer with GPU memory that is only returned when the buffer is
// freed; Adreno maps it into the process in 16 KiB chunks, so one ~1M-draw loading frame
// exhausts vm.max_map_count. See VulkanRenderer::SplitOversizedRecording.
Uint32 MagmaMaxDrawsPerCommandBuffer = 16384;
// MOBILEGL_MAGMA_DESCRIPTOR_TRIM_FRAMES: how many consecutive frames (strictly, BeginFrame
// epochs) a frame slot's descriptor pools must stay under a quarter full before Magma
// hands the slot's whole set cache back and lets it re-grow. 0 = never trim. A loading
// frame otherwise keeps its burst of sets (rd12: ~15k, where steady frames use a few dozen)
// for the life of the process. See UniformManager::TrimFrameDescriptorPools.
Uint32 MagmaDescriptorTrimFrames = 120;
// MOBILEGL_ESPRYT_AVOID_SAMPLER_MIPMAP_MIN_FILTER: avoid mipmap min filters in samplers,
// resolves certain rendering bugs on ANGLE + llvmpipe.
Bool EsprytAvoidSamplerMipmapMinFilter = false;
@@ -202,9 +214,12 @@ namespace MobileGL::MG_Config {
// persistently+coherently mapped storage at definition time (negative control /
// escape hatch). Frontend-scoped: it engages only where the active backend
// provides AcquirePersistentMap. With adoption on, an app SubData into a busy
// 128MB arena is a plain memcpy into GPU-visible memory; every driver-mediated
// route for the same write stalls the thread or ghost-copies the whole arena on
// this class of Mali driver, and the arena stops costing its size again in RAM.
// 128MB arena lands GPU-ordered through the backend's resident-subdata op
// (BufferObject::LandBytesIntoResidentStore, since 0ee3384b: an in-place write
// tore the frames still reading the old bytes) - still no driver-mediated write
// into the busy mutable store, which on this class of Mali driver stalls the
// thread or ghost-copies the whole arena, and the arena stops costing its size
// again in RAM.
Bool DisableLargeBufferAdoption = false;
// MOBILEGL_ESPRYT_FORCE_DS_READBACK_EMULATION: make DirectGLES skip the native ES
// depth/stencil reads and always go through the shader-sampling emulation. Core GL
@@ -408,8 +423,9 @@ namespace MobileGL::MG_Config {
Uint32 PipeIndexMirrorMb = 64;
// MOBILEGL_PIPE_STATS_PERIOD: frames per boundary-counter summary line. 120 is the
// steady-state cadence; the device retrace harness never reaches the teardown dump
// and a trimmed fixture (create-indirect) is shorter than 120 frames, so a run that
// needs its numbers at all sets this low enough to land at least one window.
// and a trimmed fixture (minecraft-1.21.1-neoforge-create-indirect-in-world-align1024)
// is shorter than 120 frames, so a run that needs its numbers at all sets this low
// enough to land at least one window.
Uint32 PipeStatsPeriod = 120;
// MOBILEGL_PIPE_STATS_FILE: where the boundary counters' teardown JSON dump goes.
// Empty (the default) means no dump; the per-120-frame summary line still goes to
@@ -485,6 +501,9 @@ namespace MobileGL::MG_Config {
Offscreen = 0, // a window surface is the client's own window, as before (the default)
Server = 1, // a window surface is the SERVER's window: WindowKind::ServerOwned
};
// IpcTable::AdoptTier when MOBILEGL_IPC_ADOPT_TIER is unset (or unparseable): T0 by default,
// with QUIET fallbacks. Not a tier a user can type - the knob admits 0..2.
inline constexpr Uint32 kAdoptTierUnset = 3;
struct IpcTable {
// MOBILEGL_IPC_SERVER_PATH: where to find libMobileGLServer. P6 consumes it; P5
// lands the parse because t1's ctest ENVIRONMENT blocks and add_trace_replay_test's
@@ -595,11 +614,33 @@ namespace MobileGL::MG_Config {
// flag keyed on wantReply would disable the very mechanism it exists to enable
// (docs/Disaggregated/notes/p12/CREATE-WINDOW-MEASURED.md sections 7-11).
Uint32 CreateWindow = 10;
// MOBILEGL_IPC_ADOPT_TIER: 2 = emulate (client keeps the shadow and pushes), which
// is the only tier P5 implements and the reason persistent-map-push can be non-zero
// at all (R-6). 0 and 1 parse and are Fatal at use with "P11"; they exist now so the
// negative control has a spelling the day P11 writes it.
Uint32 AdoptTier = 2;
// MOBILEGL_IPC_PBO_READBACK_SYNC (P9 W1, MG_Remote/CONTRACT-P9.md §1): 1 = a glReadPixels
// or glGetTexImage into a bound GL_PIXEL_PACK_BUFFER takes the reply-slot form again -
// the client waits for the pixels and uploads them into the buffer itself, as before P9.
// 0 (default) sends read_pixels_to_buffer / get_texture_image_to_buffer, waits for
// nothing, and marks the buffer GPU-written. The 1 arm is the A/B control and the
// red-once of the wait-count gate (PackBufferReadbackScenario): it must show a reply
// wait per read.
Uint32 PboReadbackSync = 0;
// MOBILEGL_IPC_POLL_ESCALATE (P10, MG_Remote/CONTRACT-P10.md §1): a glClientWaitSync with a
// zero timeout or a glGetSynciv(GL_SYNC_STATUS) is answered by the client from what the
// server reported (kEventFenceSignaled) - "signaled" when it has been, "not yet" otherwise
// - and only the Nth "not yet" in a row on one fence crosses as a real round trip, so a
// poll loop cannot outlive a late report. 0 = every poll crosses, as before P10: the A/B
// arm and the red-once of the no-reply gate (FencePollScenario).
Uint32 PollEscalate = 64;
// MOBILEGL_IPC_ADOPT_TIER, THE ZERO-COPY SWITCH (ruling ID-P11-14, CONTRACT-P11 B2):
// UNSET (kAdoptTierUnset, the default since 2026-09-29, the user's decision) = T0 where the
// session can run it, T2 QUIETLY where it cannot (MGLOG_D only, still counted);
// 0 = T0 asked by name - a T0 this session cannot use (a stream plane, the server's allow
// switch, no AHardwareBuffer, a failed server POST) is T2 plus one named line;
// 1 = T1, closed - refused by name, then T2; 2 = emulate (T2: the client keeps the shadow
// and pushes). Never a Fatal (MG_Remote/Transport/AdoptTier.h).
Uint32 AdoptTier = kAdoptTierUnset;
// MOBILEGL_IPC_ALLOW_ADOPT_T0 (server): 1 (default) = grant a client's T0 ask when this
// platform and the backend's POST self-test allow it; 0 = never grant it (the session
// runs T2 and says so). Read by the server role only; kept through ServerSpawn.
Uint32 AllowAdoptT0 = 1;
// MOBILEGL_IPC_VERB_BARRIER: 1 = the client blocks at every verb boundary until
// appliedSeq reaches its emitSeq (R-1). 0 is the negative control: it is EXPECTED to
// be red, because 31 of the 63 PipeInputs fields are still pulled from a live
+18 -6
View File
@@ -230,6 +230,9 @@ namespace MobileGL::MG_ConfigLoader {
features.AdvertiseFp64 = QueryEnvFlag("MOBILEGL_ADVERTISE_FP64");
features.MagmaR11G11B10FFallback = QueryEnvFlag("MOBILEGL_MAGMA_R11G11B10F_FALLBACK");
features.MagmaFramesInFlight = QueryEnvUint32("MOBILEGL_MAGMA_FRAMESINFLIGHT", 3, 1, 64);
features.MagmaMaxDrawsPerCommandBuffer =
QueryEnvUint32("MOBILEGL_MAGMA_MAX_DRAWS_PER_COMMAND_BUFFER", 16384, 0, 1u << 24);
features.MagmaDescriptorTrimFrames = QueryEnvUint32("MOBILEGL_MAGMA_DESCRIPTOR_TRIM_FRAMES", 120, 0, 1u << 20);
features.EsprytAvoidSamplerMipmapMinFilter =
QueryEnvFlag("MOBILEGL_ESPRYT_AVOID_SAMPLER_MIPMAP_MIN_FILTER");
features.EsprytAvoidExplicitLodBias = QueryEnvFlag("MOBILEGL_ESPRYT_AVOID_EXPLICIT_LOD_BIAS");
@@ -421,14 +424,21 @@ namespace MobileGL::MG_ConfigLoader {
// cap of 4 (see WireTables.cpp's kCreateWindowMax). WireTables.cpp's static_assert and
// StreamLink's crossing log are the checks on this number.
ipc.CreateWindow = QueryEnvUint32("MOBILEGL_IPC_CREATE_WINDOW", 10, 1, 12);
// P9 W1: the pack-buffer readback's A/B and red-once control (Config.h says both).
ipc.PboReadbackSync = QueryEnvUint32("MOBILEGL_IPC_PBO_READBACK_SYNC", 0, 0, 1);
// P10: fence polls answered locally; 0 is the A/B arm (Config.h).
ipc.PollEscalate = QueryEnvUint32("MOBILEGL_IPC_POLL_ESCALATE", 64, 0, 1000000);
// The verify harness compares the pushed block against the applier per verb; a
// batched queue lets the comparer read a supplied field mid-apply, which is a
// torn read rather than a divergence. The batch is therefore off whenever the
// shadow comparer is armed.
if (MG_Config::Features.PipeVerify) ipc.BatchWaits = 0;
// 2 is the only tier P5 implements (R-6). 0 and 1 parse here and are refused at the
// point of use, which is where the "P11" in the message belongs.
ipc.AdoptTier = QueryEnvUint32("MOBILEGL_IPC_ADOPT_TIER", 2, 0, 2);
// UNSET is T0 with quiet fallbacks (the default since 2026-09-29); 0 asks for T0 by name, 1 is
// refused by name, 2 is T2 - settled at the handshake from the data plane (P11 A1/B2,
// MG_Remote/Transport/AdoptTier.h). An unparseable value is warned about and reads as unset.
ipc.AdoptTier = QueryEnvUint32("MOBILEGL_IPC_ADOPT_TIER", MG_Config::kAdoptTierUnset, 0, 2);
// P11 B2: the server's own T0 allow switch (Config.h).
ipc.AllowAdoptT0 = QueryEnvUint32("MOBILEGL_IPC_ALLOW_ADOPT_T0", 1, 0, 1);
ipc.VerbBarrier = QueryEnvUint32("MOBILEGL_IPC_VERB_BARRIER", 1, 0, 1);
// P5e (MG_Remote/CONTRACT-P5E.md §1). The wait rule's A/B, parsed here like every
// other IPC knob and armed only where the server publishes kCapRunAheadApply.
@@ -522,11 +532,13 @@ namespace MobileGL::MG_ConfigLoader {
// changes the wall clock by seconds (the P12 sweep: 28.3 s at 1 down to 10.8 s at 6) a
// log that does not name it makes every run ambiguous about its own configuration.
MGLOG_I("Config: IPC ring=%uMiB stage=%uMiB wire-deferred=%uMiB spin=%uus event-wait=%ums "
"persistent-block=%uKiB create-window=%u "
"adopt-tier=%u verb-barrier=%u run-ahead=%u present-credit=%u control-timeout=%ums "
"persistent-block=%uKiB create-window=%u pbo-readback-sync=%u poll-escalate=%u "
"adopt-tier=%s allow-t0=%u verb-barrier=%u run-ahead=%u present-credit=%u control-timeout=%ums "
"cold-start=%ums strict=%d audit=%d role-split-state=%d affinity='%s' surface=%s",
ipc.RingMb, ipc.StageMb, ipc.WireDeferredMb, ipc.SpinUs, ipc.EventWaitMs,
ipc.PersistentBlockKb, ipc.CreateWindow, ipc.AdoptTier,
ipc.PersistentBlockKb, ipc.CreateWindow, ipc.PboReadbackSync, ipc.PollEscalate,
ipc.AdoptTier == 0 ? "0" : ipc.AdoptTier == 1 ? "1" : ipc.AdoptTier == 2 ? "2" : "unset(T0)",
ipc.AllowAdoptT0,
ipc.VerbBarrier, ipc.RunAhead, ipc.PresentCredit, ipc.ControlTimeoutMs, ipc.ColdStartMs,
static_cast<int>(ipc.StrictErrors), static_cast<int>(ipc.Audit),
static_cast<int>(ipc.RoleSplitState), ipc.ServerAffinity.c_str(),
+5
View File
@@ -34,6 +34,11 @@
#ifdef _WIN32
#define MOBILEGL_EXPORT extern "C" __declspec(dllexport)
#else
// Default visibility, which is what the platform's own GL and EGL headers declare their entry
// points with. The one place where that is not enough - a vendor library handing glvnd its own
// entry points, where a reference resolving against the process instead of this library would be a
// call back into the dispatcher - is answered at link time with -Bsymbolic-functions rather than
// here, because a second visibility on the same declaration does not compile.
#define MOBILEGL_EXPORT extern "C" __attribute__((visibility("default")))
#endif
+45 -11
View File
@@ -172,11 +172,15 @@ namespace MobileGL::MG_Backend {
return false;
}
if (m_eglDisplayInitialized && m_eglDisplay != dpy) {
MGLOG_E("InitializeEGLDisplay failed: backend already bound to a different EGLDisplay");
return false;
}
// A DISPLAY IS A CONNECTION, NOT A RESOURCE THIS BACKEND OWNS, and refusing the second one
// refused a caller for a reason that is not there: the surface and context tables are keyed
// by the display each was created on, every operation carries its own display handle, and
// this field has no reader anywhere - it was the whole of the rule.
//
// What the rule cost was a compositor. One asks for the display it will draw into AFTER
// probing another, so the second eglInitialize is the one that matters, and a refusal there
// is reported one layer up as "no usable DRM render device" - the probe having succeeded
// and the real display having failed, with nothing in between that says which was refused.
m_eglDisplay = dpy;
m_eglDisplayInitialized = true;
if (major) {
@@ -196,12 +200,15 @@ namespace MobileGL::MG_Backend {
Bool BackendObject::ResizeEGLWindowSurface(EGLSurface surface, Uint32 width, Uint32 height) {
const std::lock_guard<std::recursive_mutex> lock(m_eglStateMutex);
auto surfaceIt = m_eglSurfaces.find(surface);
if (surfaceIt == m_eglSurfaces.end() || surfaceIt->second.Kind != SurfaceKind::Window) {
if (surfaceIt == m_eglSurfaces.end() || surfaceIt->second.Kind == SurfaceKind::None) {
MGLOG_E("ResizeEGLWindowSurface failed: no window surface is initialized");
return false;
}
// A pbuffer-backed window (a Wayland window presented through wl_shm) resizes too.
surfaceIt->second.Window.Width = width;
surfaceIt->second.Window.Height = height;
surfaceIt->second.Width = static_cast<EGLint>(std::max<Uint32>(width, 1));
surfaceIt->second.Height = static_cast<EGLint>(std::max<Uint32>(height, 1));
if (m_eglSurface == surface) {
m_windowHandle.Width = width;
m_windowHandle.Height = height;
@@ -279,6 +286,7 @@ namespace MobileGL::MG_Backend {
return true;
}
m_activatingSurface = surface;
if (surfaceState->Kind == SurfaceKind::Window) {
SetWindowHandle(surfaceState->Window);
if (!InitWindowSurface()) {
@@ -298,8 +306,14 @@ namespace MobileGL::MG_Backend {
m_eglSurface = surface;
m_eglSurfaceInitialized = true;
m_eglSurfaceKind = surfaceState->Kind;
m_eglCurrentThreads.clear();
m_backendCapabilitiesInitialized = false;
// NOT m_eglCurrentThreads.clear(): another thread's current context and surface are its
// own (a multi-window client renders each window on its own thread), and wiping them made
// that thread's next eglSwapBuffers fail with "no current context attached".
auto activatedIt = m_eglSurfaces.find(surface);
if (activatedIt != m_eglSurfaces.end() && !activatedIt->second.Activated) {
activatedIt->second.Activated = true;
m_backendCapabilitiesInitialized = false;
}
return true;
}
@@ -366,6 +380,14 @@ namespace MobileGL::MG_Backend {
m_eglCurrentThreads.clear();
}
void BackendObject::ForgetActiveEGLSurface() {
const std::lock_guard<std::recursive_mutex> lock(m_eglStateMutex);
m_eglSurfaceInitialized = false;
m_eglSurfaceKind = SurfaceKind::None;
m_eglSurface = EGL_NO_SURFACE;
m_windowHandle = {};
}
Bool BackendObject::SwapEGLBuffers(EGLDisplay dpy, EGLSurface draw) {
const std::lock_guard<std::recursive_mutex> lock(m_eglStateMutex);
if (!m_eglDisplayInitialized || m_eglDisplay != dpy) {
@@ -382,10 +404,16 @@ namespace MobileGL::MG_Backend {
MGLOG_E("SwapEGLBuffers failed: draw surface is not current on this thread");
return false;
}
if (!m_eglSurfaceInitialized || draw == EGL_NO_SURFACE || draw != m_eglSurface) {
if (draw == EGL_NO_SURFACE || !GetRegisteredEGLSurface(draw)) {
MGLOG_E("SwapEGLBuffers failed: invalid draw surface");
return false;
}
// The calling thread's surface, which another thread's make-current may have moved the
// backend off: bring it back before presenting it.
if ((!m_eglSurfaceInitialized || draw != m_eglSurface) && !ActivateEGLSurface(draw)) {
MGLOG_E("SwapEGLBuffers failed: the draw surface could not be re-activated");
return false;
}
const auto& backendFunctions = GetBackendFunctions();
if (!backendFunctions.Present) {
@@ -426,8 +454,9 @@ namespace MobileGL::MG_Backend {
m_eglSurfaces.erase(surfaceIt);
if (m_eglSurface == surface) {
OnEGLSurfaceReleased(surface);
ResetEGLRuntimeState();
ForgetActiveEGLSurface();
}
OnEGLSurfaceForgotten(surface);
}
void BackendObject::ReleaseEGLCurrentThread(const std::thread::id& threadKey) {
@@ -458,8 +487,9 @@ namespace MobileGL::MG_Backend {
m_eglSurfaces.erase(surfaceIt);
if (m_eglSurface == surface) {
OnEGLSurfaceReleased(surface);
ResetEGLRuntimeState();
ForgetActiveEGLSurface();
}
OnEGLSurfaceForgotten(surface);
}
void BackendObject::ReleaseEGLResources() {
@@ -492,4 +522,8 @@ namespace MobileGL::MG_Backend {
void BackendObject::OnEGLSurfaceReleased(EGLSurface surface) {
(void)surface;
}
void BackendObject::OnEGLSurfaceForgotten(EGLSurface surface) {
(void)surface;
}
} // namespace MobileGL::MG_Backend
+14
View File
@@ -629,18 +629,28 @@ namespace MobileGL {
struct EGLSurfaceState {
SurfaceKind Kind = SurfaceKind::None;
Bool DestroyPending = false;
// Activated at least once: a re-activation (a multi-window client switching back)
// is a re-bind and does not re-run the backend's capability probe.
Bool Activated = false;
WindowHandle Window;
EGLint Width = 1;
EGLint Height = 1;
};
void ResetEGLRuntimeState();
// ResetEGLRuntimeState for a released ACTIVE surface: the backend has no active surface
// any more, but every OTHER thread's current context and surface stay as they were (a
// multi-window client closing one popup must not unbind its other windows' threads).
void ForgetActiveEGLSurface();
Bool RegisterEGLWindowSurface(EGLSurface surface, const WindowHandle& handle);
Bool RegisterEGLPbufferSurface(EGLSurface surface, EGLint width, EGLint height);
const EGLSurfaceState* GetRegisteredEGLSurface(EGLSurface surface) const;
Bool ActivateEGLSurface(EGLSurface surface);
virtual Bool InitPbufferSurface(EGLint width, EGLint height);
virtual void OnEGLSurfaceReleased(EGLSurface surface);
// Every registered surface the base class drops, current or not, after it is gone from
// m_eglSurfaces. A backend that keeps a native surface per client surface frees it here.
virtual void OnEGLSurfaceForgotten(EGLSurface surface);
FormatCapabilityCache& MutableFormatCapabilities();
mutable std::recursive_mutex m_eglStateMutex;
@@ -648,6 +658,10 @@ namespace MobileGL {
WindowHandle m_windowHandle;
EGLDisplay m_eglDisplay = EGL_NO_DISPLAY;
EGLSurface m_eglSurface = EGL_NO_SURFACE;
// The client surface ActivateEGLSurface is bringing up while it calls InitWindowSurface /
// InitPbufferSurface, which take no surface argument: a backend that maps client surfaces
// to native ones reads it to re-bind an existing native surface instead of making another.
EGLSurface m_activatingSurface = EGL_NO_SURFACE;
Bool m_eglDisplayInitialized = false;
Bool m_eglSurfaceInitialized = false;
Bool m_backendCapabilitiesInitialized = false;
@@ -906,13 +906,45 @@ namespace MobileGL::MG_Backend::DirectGLES {
#endif
}
Bool BackendObject_DirectGLES::RebindNativeSurfaceFor(EGLSurface surface) {
const auto it = m_nativeSurfaces.find(surface);
if (it == m_nativeSurfaces.end()) return false;
if (DirectGLES::BindSessionSurface(it->second)) return true;
// The session no longer owns it (destroyed with a window loss): forget and recreate.
m_nativeSurfaces.erase(it);
return false;
}
void BackendObject_DirectGLES::RestoreSurfaceAfterCreation(EGLSurface previousNativeDraw) {
// The ACTIVE CONTEXT's own surface, exactly: the creation's bind attached the new surface to
// whichever context the session had bound (often another window's thread), and putting back
// "the surface activated last" instead re-attached THAT context to a third window's surface -
// a desktop drawing into and reading back from the panel's pbuffer.
if (previousNativeDraw == EGL_NO_SURFACE) return;
if (!DirectGLES::BindSessionSurface(previousNativeDraw)) return;
for (const auto& [client, native] : m_nativeSurfaces) {
if (native != previousNativeDraw) continue;
const auto state = m_eglSurfaces.find(client);
if (state == m_eglSurfaces.end()) break;
m_eglSurface = client;
m_eglSurfaceInitialized = true;
m_eglSurfaceKind = state->second.Kind;
if (state->second.Kind == SurfaceKind::Window) m_windowHandle = state->second.Window;
break;
}
}
Bool BackendObject_DirectGLES::InitWindowSurface() {
if (RebindNativeSurfaceFor(m_activatingSurface)) return true;
// Only use EGL for now
auto nativeWindow = reinterpret_cast<NativeWindowType>(m_windowHandle.Handle);
if (!DirectGLES::InitWindowSurface(nativeWindow)) {
MGLOG_E("Failed to initialize window surface for DirectGLES backend");
return false;
}
if (m_activatingSurface != EGL_NO_SURFACE) {
m_nativeSurfaces[m_activatingSurface] = DirectGLES::CurrentSessionDrawSurface();
}
return true;
}
@@ -976,8 +1008,19 @@ namespace MobileGL::MG_Backend::DirectGLES {
return true;
}
// ---- P14 S4: A SECOND SURFACE NO LONGER TEARS THE CONTEXT DOWN. ----
//
// This used to be `DestroyEGLContext(); ResetEGLRuntimeState();` whenever a surface was
// created while another was active, i.e. every pbuffer -> window -> pbuffer switch destroyed
// the native context - and with it every buffer, texture and program the application had
// built. The native context now belongs to the (session, context token) tuple the backend
// resolves, and creating a surface for it only adds a surface: the tuple keeps its context
// and its objects. ResetEGLRuntimeState still runs because the base class's per-surface
// bookkeeping (which surface is active, which threads made it current) does describe the
// surface that is being replaced.
const EGLSurface previous = m_eglSurfaceInitialized ? m_eglSurface : EGL_NO_SURFACE;
const EGLSurface previousNativeDraw = DirectGLES::CurrentContextDrawSurface();
if (m_eglSurfaceInitialized) {
DestroyEGLContext();
ResetEGLRuntimeState();
}
@@ -986,7 +1029,12 @@ namespace MobileGL::MG_Backend::DirectGLES {
// the first virtual tuple adopts that bind. On failure nothing is known to be bound. The
// pull arm below is the original statement, byte for byte (G1).
const Bool created = BackendObject::CreateEGLWindowSurface(surface, handle);
if (created) {
if (created && previous != EGL_NO_SURFACE && previous != surface) {
// Another surface was current: the creation must not leave the context that held it
// drawing to the new one (P14 multi-window clients).
RestoreSurfaceAfterCreation(previousNativeDraw);
NoteNativeContextGone();
} else if (created) {
NoteNativeContextFreshFromSurfaceCreation();
} else {
NoteNativeContextGone();
@@ -1008,8 +1056,11 @@ namespace MobileGL::MG_Backend::DirectGLES {
return true;
}
// P14 S4: as for the window surface above - the native context is the (session, context
// token) tuple's and survives a surface switch. This used to be DestroyEGLContext().
const EGLSurface previous = m_eglSurfaceInitialized ? m_eglSurface : EGL_NO_SURFACE;
const EGLSurface previousNativeDraw = DirectGLES::CurrentContextDrawSurface();
if (m_eglSurfaceInitialized) {
DestroyEGLContext();
ResetEGLRuntimeState();
}
@@ -1017,7 +1068,10 @@ namespace MobileGL::MG_Backend::DirectGLES {
// ID-54 / ID-67: as for the window surface - InitPbufferSurface bound natively. The pull
// arm below is the original statement, byte for byte (G1).
const Bool created = BackendObject::CreateEGLPbufferSurface(surface, width, height);
if (created) {
if (created && previous != EGL_NO_SURFACE && previous != surface) {
RestoreSurfaceAfterCreation(previousNativeDraw);
NoteNativeContextGone();
} else if (created) {
NoteNativeContextFreshFromSurfaceCreation();
} else {
NoteNativeContextGone();
@@ -1028,8 +1082,34 @@ namespace MobileGL::MG_Backend::DirectGLES {
#endif
}
Bool BackendObject_DirectGLES::ResizeEGLWindowSurface(EGLSurface surface, Uint32 width, Uint32 height) {
const std::lock_guard<std::recursive_mutex> lock(m_eglStateMutex);
const auto state = m_eglSurfaces.find(surface);
const auto native = m_nativeSurfaces.find(surface);
if (state == m_eglSurfaces.end() || state->second.Kind != SurfaceKind::Pbuffer || native == m_nativeSurfaces.end()) {
return BackendObject::ResizeEGLWindowSurface(surface, width, height);
}
// A pbuffer has no resize: the client surface gets a new native pbuffer at the new size,
// and every context that drew to the old one draws to the new one.
const EGLint w = static_cast<EGLint>(std::max<Uint32>(width, 1));
const EGLint h = static_cast<EGLint>(std::max<Uint32>(height, 1));
const EGLSurface replacement = DirectGLES::CreateSessionPbuffer(w, h);
if (replacement == EGL_NO_SURFACE) {
MGLOG_E("ResizeEGLWindowSurface: no %dx%d pbuffer could replace the old drawable", w, h);
return false;
}
DirectGLES::ReplaceSessionSurface(native->second, replacement);
native->second = replacement;
return BackendObject::ResizeEGLWindowSurface(surface, width, height);
}
Bool BackendObject_DirectGLES::InitPbufferSurface(EGLint width, EGLint height) {
return DirectGLES::InitPbufferSurface(width, height);
if (RebindNativeSurfaceFor(m_activatingSurface)) return true;
if (!DirectGLES::InitPbufferSurface(width, height)) return false;
if (m_activatingSurface != EGL_NO_SURFACE) {
m_nativeSurfaces[m_activatingSurface] = DirectGLES::CurrentSessionDrawSurface();
}
return true;
}
Bool BackendObject_DirectGLES::MakeEGLCurrent(EGLDisplay dpy, EGLSurface draw, EGLSurface read, EGLContext ctx) {
@@ -1089,7 +1169,16 @@ namespace MobileGL::MG_Backend::DirectGLES {
m_eglSurfaceInitialized && m_eglSurface == draw &&
DirectGLES::IsBackendContextCurrentOnThisThread() &&
NativeBindCanBeSkippedFor(dpy, draw, read, ctx);
if (!nativelyCurrentAlready && !DirectGLES::MakeCurrent()) {
// The draw surface's own native surface (P14 multi-window): a known one is re-bound, so the
// bind goes to it and not to whichever surface the session drew to last.
// The real bind still runs whenever the skip above says so (ID-67: a different tuple onto
// the same surface needs MakeCurrent's invalidations even though the driver triple is equal).
const auto nativeDraw = m_nativeSurfaces.find(draw);
if (!nativelyCurrentAlready && nativeDraw != m_nativeSurfaces.end()) {
(void)DirectGLES::SelectSessionSurface(nativeDraw->second);
}
const Bool bound = nativelyCurrentAlready || DirectGLES::MakeCurrent();
if (!bound) {
NoteNativeContextGone();
return false;
}
@@ -1129,13 +1218,24 @@ namespace MobileGL::MG_Backend::DirectGLES {
}
void BackendObject_DirectGLES::OnEGLSurfaceReleased(EGLSurface surface) {
// P14: releasing a surface releases THAT SURFACE (OnEGLSurfaceForgotten), never the
// contexts. This used to be DestroyEGLContext() - every native context of the session, with
// every object the client had built - which a multi-window client hit on its first closed
// popup.
(void)surface;
DestroyEGLContext();
#if MOBILEGL_BUILD_DISAGGREGATED
NoteNativeContextGone();
#endif
}
void BackendObject_DirectGLES::OnEGLSurfaceForgotten(EGLSurface surface) {
const auto it = m_nativeSurfaces.find(surface);
if (it == m_nativeSurfaces.end()) return;
const EGLSurface native = it->second;
m_nativeSurfaces.erase(it);
DirectGLES::DestroySessionSurface(native);
}
const RendererInfo& BackendObject_DirectGLES::GetRendererInfo() const {
return MutableRendererInfo();
}
@@ -1165,6 +1265,13 @@ namespace MobileGL::MG_Backend::DirectGLES {
V_OpenGL30, V_OpenGL31, V_OpenGL32, V_OpenGL33, V_OpenGL40, V_OpenGL41, V_OpenGL42, V_OpenGL43,
V_OpenGL44, V_OpenGL45, V_OpenGL46,
E_GL_ARB_draw_buffers_blend,
// The GLSL 1.00/1.10-era ARB trio plus NPOT textures: all core since GL 2.0 and
// trivially true for every context this backend claims (4.6). KWin 6's
// EglContext::checkSupported() gates desktop-GL compositing on exactly these four
// strings, so withholding them fails its renderer bring-up on an otherwise capable
// implementation.
E_GL_ARB_shader_objects, E_GL_ARB_vertex_shader, E_GL_ARB_fragment_shader,
E_GL_ARB_texture_non_power_of_two,
E_GL_ARB_compute_shader, E_GL_ARB_shader_storage_buffer_object, E_GL_ARB_shader_image_load_store,
E_GL_ARB_clear_buffer_object, E_GL_ARB_program_interface_query, E_GL_ARB_framebuffer_object, E_GL_EXT_framebuffer_object,
E_GL_ARB_depth_texture, E_GL_ARB_buffer_storage, E_GL_ARB_texture_storage,
@@ -38,6 +38,7 @@ namespace MobileGL::MG_Backend::DirectGLES {
Bool InitializeEGLDisplay(EGLDisplay dpy, EGLint* major, EGLint* minor) override;
Bool CreateEGLWindowSurface(EGLSurface surface, const WindowHandle& handle) override;
Bool CreateEGLPbufferSurface(EGLSurface surface, EGLint width, EGLint height) override;
Bool ResizeEGLWindowSurface(EGLSurface surface, Uint32 width, Uint32 height) override;
Bool MakeEGLCurrent(EGLDisplay dpy, EGLSurface draw, EGLSurface read, EGLContext ctx) override;
Bool SwapEGLBuffers(EGLDisplay dpy, EGLSurface draw) override;
void ReleaseEGLSurface(EGLSurface surface) override;
@@ -57,8 +58,19 @@ namespace MobileGL::MG_Backend::DirectGLES {
void UpdateDynamicBackendParameters();
Bool InitPbufferSurface(EGLint width, EGLint height) override;
void OnEGLSurfaceReleased(EGLSurface surface) override;
void OnEGLSurfaceForgotten(EGLSurface surface) override;
// An existing native surface for the client surface being activated, re-bound; false when
// there is none yet (the caller creates it).
Bool RebindNativeSurfaceFor(EGLSurface surface);
// EGL: creating a surface does not change what is current. The creation bound the new
// surface natively; put the previous one back for the context that held it.
void RestoreSurfaceAfterCreation(EGLSurface previousNativeDraw);
Bool m_initialized = false;
// P14: ONE NATIVE SURFACE PER CLIENT SURFACE, created on its first activation and re-bound
// on every later one. A multi-window client switches surfaces every frame; making a new
// native surface each time leaked them and lost what the window had drawn.
UnorderedMap<EGLSurface, EGLSurface> m_nativeSurfaces;
MG_External::EGLFunctionsTable m_EGLFunctions;
MG_External::GLESFunctionsTable m_GLESFunctions;
MG_External::GLESCapabilities m_GLESCapabilities;
File diff suppressed because it is too large Load Diff
+132 -5
View File
@@ -23,15 +23,21 @@ namespace MobileGL::MG_Backend::DirectGLES {
// Content uploads use scratch bindings, so draws and dispatches call this after
// texture synchronization.
void BindCurrentTextures();
#if MOBILEGL_BUILD_DISAGGREGATED
Bool ReadTextureImageWire(const MG_Pipe::MGPReadbackInfo& info, Vector<Uint8>& bytes);
// Native/server-only color level readback. Returns tightly packed, owned
// Driver-side color/depth level readback. Returns tightly packed, owned
// bytes in the requested pair; never consults a frontend texture or PACK/PBO.
// The caller resolves identity/extent from its resource record and supplies
// whether the SOURCE allocation already uses an image carrier.
// The caller supplies identity and extent, and whether the SOURCE allocation
// already uses an image carrier.
Bool ReadTextureLevelTight(GLuint texture, TextureTarget target, TextureUploadTarget uploadTarget,
TextureInternalFormat logicalFormat, GLint level, const IntVec3& logicalExtent,
Bool sourceUsesImageCarrier, GLenum format, GLenum type, Vector<Uint8>& bytes);
#if MOBILEGL_BUILD_DISAGGREGATED
Bool ReadTextureImageWire(const MG_Pipe::MGPReadbackInfo& info, Vector<Uint8>& bytes);
// Whether the NATIVE texture currently holds `level` of `uploadTarget` at `logicalExtent`
// (glGetTexLevelParameteriv). False for a level the driver never allocated - one defined after
// the texture's last sync - so a re-mint does not read what is not there. A query the driver
// refuses answers true, keeping the caller on its readback path.
Bool NativeTextureLevelHasExtent(GLuint texture, TextureTarget target, TextureUploadTarget uploadTarget,
GLint level, const IntVec3& logicalExtent);
#endif
void ClearBufferfi(GLenum buffer, GLint drawbuffer, GLfloat depth, GLint stencil);
void ClearBufferfv(GLenum buffer, GLint drawbuffer, const GLfloat* value);
@@ -105,6 +111,25 @@ namespace MobileGL::MG_Backend::DirectGLES {
void ShaderStorageBlockBinding(GLuint program, const GLchar* storageBlockName, GLuint storageBlockBinding);
Bool InitWindowSurface(NativeWindowType window);
Bool InitPbufferSurface(EGLint width, EGLint height);
// The calling thread's session's current native draw surface (the one the last Init*Surface
// created, or the last BindSessionSurface named).
EGLSurface CurrentSessionDrawSurface();
// The surface the calling thread's ACTIVE context last drew to (EGL_NO_SURFACE for none).
EGLSurface CurrentContextDrawSurface();
// Makes an EXISTING native surface of the calling thread's session the one it draws to, and
// binds it to the active context. False for a surface the session does not own.
Bool BindSessionSurface(EGLSurface surface);
// The same selection without the bind (the caller binds - MakeCurrent - unconditionally).
Bool SelectSessionSurface(EGLSurface surface);
// Destroys one native surface of the calling thread's session - and only it; the session's
// contexts and other surfaces are untouched.
void DestroySessionSurface(EGLSurface surface);
// A new pbuffer of the calling thread's session, created with the active context's config and
// bound to nothing (EGL: creating a surface changes no binding).
EGLSurface CreateSessionPbuffer(EGLint width, EGLint height);
// Every context of the session that drew to `previous` draws to `replacement` instead, and
// `previous` is destroyed (a pbuffer-backed window's resize).
void ReplaceSessionSurface(EGLSurface previous, EGLSurface replacement);
Bool MakeCurrent();
Bool ReleaseCurrent();
// True when the backend ES context is current on the calling thread, i.e.
@@ -198,6 +223,108 @@ namespace MobileGL::MG_Backend::DirectGLES {
void SetEGLFuncsTable(const MG_External::EGLFunctionsTable& eglFuncs);
void SetGLESFuncsTable(const MG_External::GLESFunctionsTable& glesFuncs);
void SetGLESCapabilities(const MG_External::GLESCapabilities& capabilities);
// ---------------------------------------------------------------------------------
// P14 S4 (docs/Disaggregated/design/11-state-ownership.md): THE NATIVE TUPLE IS PER CONTEXT
// ---------------------------------------------------------------------------------
//
// The display is process state and stays one object (eglInitialize once per process, terminated
// when the last native context leaves). The native EGLContext, the config it was created with
// and the native surfaces are keyed by (session, client context token) - the identity S1 put on
// the wire and S2 gave a per-thread owner. Creating a second context therefore no longer tears
// the first one's native context down, which is what lets two contexts (and two share groups)
// be live at once in one process.
// Answers, for the CALLING THREAD, which session it belongs to and which client context token
// that session has bound. Installed once by the server layer; with no resolver installed -
// monolith, a frontend-only process, a unit case - the answer is the single key {0, 0}, which is
// exactly the process singleton this replaces. `false` also means {0,0}.
using NativeContextKeyResolver = Bool (*)(Uint64* outSessionKey, Uint64* outContextToken);
void SetNativeContextKeyResolver(NativeContextKeyResolver resolver);
#if MOBILEGL_PIPE_PUSH
// ---------------------------------------------------------------------------------
// P14 S6 (docs/Disaggregated/design/11-state-ownership.md): THE TWIN TABLES' KEY, AND ITS
// SHAPE IS S4's, ONE LEVEL DOWN. A twin is a DRIVER object, so it lives in one native
// context's object namespace; the identity a twin table must be keyed on is therefore
// {session, share group} - the SAME two words the applier's OBJECT RECORDS are filed under
// (PipeApply.h's MGPipeApplierKey, derived by the same probe and the same `0 means its own
// group` rule, so the two agree by construction rather than by two conventions).
//
// Answers, for the CALLING THREAD, which session it belongs to and which share group that
// session's current context is in. Installed once by the server layer beside the native and
// applier probes; with no probe installed - or on a thread that is no session's own, which
// is the client process, the in-process client+server shape and every unit case - the
// answer is the single key {0, 0}, exactly the process-wide table this replaces.
struct TwinKey {
Uint64 SessionKey = 0;
Uint64 ShareGroupKey = 0;
Bool operator==(const TwinKey& other) const {
return SessionKey == other.SessionKey && ShareGroupKey == other.ShareGroupKey;
}
Bool operator!=(const TwinKey& other) const { return !(*this == other); }
};
struct TwinKeyHash {
SizeT operator()(const TwinKey& key) const {
// The 64-bit mix SlotAllocator and the applier's registry already use for their own
// pair keys, so every keyed table in this tree hashes a session the same way.
Uint64 mixed = key.SessionKey ^ (key.ShareGroupKey + 0x9E3779B97F4A7C15ull +
(key.SessionKey << 6) + (key.SessionKey >> 2));
mixed ^= mixed >> 33;
mixed *= 0xFF51AFD7ED558CCDull;
mixed ^= mixed >> 33;
return static_cast<SizeT>(mixed);
}
};
using TwinKeyResolver = Bool (*)(TwinKey* outKey);
void SetTwinKeyResolver(TwinKeyResolver resolver);
// The calling thread's key. Answers false for {0, 0} - the process-wide group - which is
// also the answer when no probe has ever been installed.
Bool CurrentTwinKey(TwinKey* outKey);
#endif
// Builds the native context for `contextToken` under the calling thread's session. A
// `shareGroupToken` whose group already has a live native context in this session hands that
// context to eglCreateContext as the share argument - which is what makes two contexts see one
// set of buffer/texture objects. Idempotent: a token that already has one answers true.
Bool CreateNativeContextFor(Uint64 contextToken, Uint64 shareGroupToken);
// Destroys ONE context's native tuple (the DestroyContext control frame). The session's other
// contexts are untouched; the display is terminated only when the last context in the process
// has gone.
void DestroyNativeContextFor(Uint64 contextToken);
// P14 S4. THE NATIVE HALF OF A CONTEXT SWITCH. `bind_context` moves which context the session's
// records belong to; this makes the calling thread's native tuple follow it, binding the
// session's current surface to the newly named context's native EGLContext. A no-op when the
// named context has no native context yet (the session never sent CreateContext) or when the
// native context already current on this thread IS that one - so the single-context world pays
// nothing for it. Called from the bind_context path on the apply thread.
Bool MakeNativeContextCurrentForBoundToken();
#if MOBILEGL_BUILD_DISAGGREGATED
// P14: the server's backend turn moved to the calling apply thread (ServerLoop.cpp's
// BackendTurnLock). Takes the backend's GL ownership for this thread and drops every
// binding shadow, which still describes the previous holder's native context.
void OnBackendTurnHandoff();
#endif
// How many native contexts this process holds, and how many of them belong to one session. The
// test observables for "a second context did not tear the first one down".
Uint64 NativeContextCount();
Uint64 NativeContextCountForSession(Uint64 sessionKey);
// The calling thread's session, as the resolver above reports it (0 with no resolver).
Uint64 CurrentNativeSessionKey();
// ---- Test observables (P14 S4). EGL exposes no "which EGLContext does this client context
// hold" query and no way to ask a context for its share group, so a case that must tell "the
// same native context still serves" from "a fresh one was built", or a sharing pair from two
// unrelated contexts, reads it here. These are pure reads plus one key override; neither is on
// a production path and neither changes behaviour.
EGLContext NativeContextHandleFor(Uint64 sessionKey, Uint64 contextToken);
// Overrides the key the calling thread resolves to, for the life of the call. The backend's
// ordinary answer comes from the server layer's probe; this is how a case drives exactly the
// tuple it means when no session runtime is installed.
void ForceNativeContextKeyForTesting(Uint64 sessionKey, Uint64 contextToken);
void DestroyEGLContext();
// Transform feedback capture spans, performed by the real ES driver. The
File diff suppressed because it is too large Load Diff
+205 -19
View File
@@ -332,6 +332,11 @@ namespace MobileGL::MG_Backend::DirectGLES {
using const_iterator = typename BackendMap::const_iterator;
#if MOBILEGL_PIPE_PUSH
using SlotTable = BackendSlotTable<StateObject, BackendObject, kKind>;
// P14 S6: the ONE bucket of this kind the calling thread is in. Every handle-keyed entry
// below resolves through it, so the six twin registries answer per {session, share
// group} without a single call site naming a bucket.
using SlotTables = SlotTableRegistry<StateObject, BackendObject, kKind>;
using TwinKey = DirectGLES::TwinKey;
#endif
BackendPtr& GetOrCreate(const StatePtr& stateObj) {
@@ -348,7 +353,7 @@ namespace MobileGL::MG_Backend::DirectGLES {
// insertion (D13; SlotTables.h) - so a table used outside a registry arms
// it too, which is right: it is the twin, not the registry, that owns the
// driver id a guarded destructor exists for.
return m_slotTable.GetOrCreate(stateObj);
return m_slotTables.ForCallingThread().GetOrCreate(stateObj);
}
#endif
// Twin creation is the moment a driver-owned id starts needing a guarded
@@ -407,7 +412,7 @@ namespace MobileGL::MG_Backend::DirectGLES {
#endif
#if MOBILEGL_PIPE_PUSH
if (EsprytSlotTablesEnabled()) {
return m_slotTable.Find(stateObj);
return m_slotTables.ForCallingThread().Find(stateObj);
}
#endif
const auto entryIt = m_entries.find(stateObj);
@@ -445,7 +450,7 @@ namespace MobileGL::MG_Backend::DirectGLES {
// backend memo stores instead of a raw pointer, a GL name or a bare lifetime id.
MG_Pipe::MGPipeHandle HandleOf(const StateObject* stateObj) const {
if (EsprytSlotTablesEnabled()) {
return m_slotTable.HandleOf(stateObj);
return m_slotTables.HandleOf(stateObj);
}
return MG_Pipe::kMGPipeNullHandle;
}
@@ -454,7 +459,7 @@ namespace MobileGL::MG_Backend::DirectGLES {
// the lookup a backend memo that already holds a handle wants: no lifetime-id probe.
BackendPtr* FindByHandle(MG_Pipe::MGPipeHandle handle) {
if (EsprytSlotTablesEnabled()) {
return m_slotTable.FindByHandle(handle);
return m_slotTables.FindByHandle(handle);
}
return nullptr;
}
@@ -490,7 +495,7 @@ namespace MobileGL::MG_Backend::DirectGLES {
return nullptr;
#endif
}
const Uint32 liveGen = m_slotTable.LiveGenAt(handle.Slot);
const Uint32 liveGen = m_slotTables.LiveGenAt(handle.Slot);
if (liveGen != 0 && liveGen > handle.Gen) {
#if MOBILEGL_BUILD_DISAGGREGATED
MG_Pipe::MGPipeSessionFail( // @Ph-declined (ID-P7-1): PH-2 stays Fatal, CONTRACT-P7 §12
@@ -502,7 +507,7 @@ namespace MobileGL::MG_Backend::DirectGLES {
return nullptr;
#endif
}
return &m_slotTable.GetOrCreate(handle);
return &m_slotTables.GetOrCreate(handle);
}
// The generation of the LIVE entry at this slot, or 0. It exists so a caller can
@@ -510,7 +515,7 @@ namespace MobileGL::MG_Backend::DirectGLES {
// performs silently.
Uint32 LiveGenAt(Uint32 slot) const {
if (!EsprytSlotTablesEnabled()) return 0;
return m_slotTable.LiveGenAt(slot);
return m_slotTables.LiveGenAt(slot);
}
// P5c (hd): the two halves of SlotTables.h's state note, forwarded. A caller holding
@@ -524,12 +529,12 @@ namespace MobileGL::MG_Backend::DirectGLES {
// which is the answer the legacy walk's own weak_ptr test already gives.
void NoteStateForHandle(MG_Pipe::MGPipeHandle handle, const StatePtr& stateObj) {
if (EsprytSlotTablesEnabled()) {
m_slotTable.NoteStateForHandle(handle, stateObj);
m_slotTables.NoteStateForHandle(handle, stateObj);
}
}
StatePtr StateForHandle(MG_Pipe::MGPipeHandle handle) const {
if (EsprytSlotTablesEnabled()) {
return m_slotTable.StateForHandle(handle);
return m_slotTables.StateForHandle(handle);
}
return nullptr;
}
@@ -591,9 +596,52 @@ namespace MobileGL::MG_Backend::DirectGLES {
template <typename Fn>
void ForEachLive(Fn&& fn) const {
if (EsprytSlotTablesEnabled()) {
m_slotTable.ForEachLive(fn);
m_slotTables.ForEachLive(fn);
}
}
// ---- P14 S6: the KEYED shape's own controls ----------------------------------------
//
// Everything above is the shape a CALL SITE sees, and it is deliberately the shape it
// saw before the rekey. These four are the shape only a caller that has to reason about
// buckets needs: the teardown of a session, a test asking whether two contexts landed in
// two namespaces, and a diagnostic.
//
// THE ONE BUCKET THIS THREAD RESOLVES TO. A by-value copy of it is exactly what a
// fixture's saved registry was, and it is what makes `= {}` and "put it back" both mean
// something under a key.
SlotTable& BucketForCallingThread() {
if (EsprytSlotTablesEnabled()) return m_slotTables.ForCallingThread();
// The legacy arm has no buckets; the empty table's Reset() is the only honest
// answer, and no shipping path asks (the arm is compiled out or the bit is clear).
return m_legacyEmptyTable;
}
Vector<SharedPtr<SlotTable>> BucketSnapshot() const {
if (EsprytSlotTablesEnabled()) return m_slotTables.Snapshot();
return {};
}
SizeT BucketCount() const {
return EsprytSlotTablesEnabled() ? m_slotTables.BucketCount() : 0;
}
// Drop one {session, share group}. Answers whether such a bucket existed.
Bool DropBucket(const TwinKey& key) {
return EsprytSlotTablesEnabled() && m_slotTables.Drop(key);
}
// Drop every bucket of one session - the difference between "this session's twins go"
// and "the process's do" - and answer how many groups that took with it.
Uint32 DropBucketsOfSession(Uint64 sessionKey) {
return EsprytSlotTablesEnabled() ? m_slotTables.DropSession(sessionKey) : 0;
}
// Empty every bucket. The teardown of a process-shaped world, where the key is {0, 0}.
void ResetBuckets() {
if (EsprytSlotTablesEnabled()) m_slotTables.Reset();
}
// Every bucket, walked with its key.
template <typename Fn>
void ForEachBucket(Fn&& fn) const {
if (EsprytSlotTablesEnabled()) m_slotTables.ForEachBucket(fn);
}
#endif
// The seven DirectGLES.cpp call sites drive the LEGACY arm and nothing else. On the
@@ -664,7 +712,14 @@ namespace MobileGL::MG_Backend::DirectGLES {
Uint32 m_creationTick = 0;
Bool m_isCollecting = false;
#if MOBILEGL_PIPE_PUSH
BackendSlotTable<StateObject, BackendObject, kKind> m_slotTable;
// P14 S6: the ONE bucket per {session, share group}, hidden behind the same member names
// the single table had, so every handle-keyed call site above and every caller below is
// unchanged. See SlotTables.h's SlotTableRegistry for the resolution chain.
SlotTables m_slotTables;
// The legacy arm's stand-in for BucketForCallingThread(). Never read after a Reset and
// never populated: the two entries that could write it are the handle-keyed ones, which
// return early on that arm.
SlotTable m_legacyEmptyTable;
#endif
};
@@ -1004,6 +1059,14 @@ namespace MobileGL::MG_Backend::DirectGLES {
// (the UBO binding walk) skip the probe while their pre-pass epoch read
// matches, exactly like the per-VAO memo stamps.
Uint64 drawCleanEpoch = 0;
// Frame serial whose completion retires the last glCopyBufferSubData the upload
// ring staged INTO this store (the flush ladder's ring tier: FlushPendingRangesNow
// on the pull arm, FlushPendingRangesFrom on the push arm); 0 = none. Until it
// retires, both keep every partial range off the range-invalidating map: on
// Adreno 830 that map, on a store with a queued copy, loses the copy (see
// FlushPendingRangesNow and InvalidateFlushAccessFor). Draw thread only, like the
// flush.
Uint64 ringCopyRetireSerial = 0;
// Zero-copy coherent persistent map (EXT_buffer_storage): the GL store is
// immutable, persistently+coherently mapped, and persistentPtr is what the app
// (and the frontend PipeResource) write into directly. While set, draw-time
@@ -1042,6 +1105,21 @@ namespace MobileGL::MG_Backend::DirectGLES {
// the legacy arm did; this member exists for the drains that have no object, which
// in P3a is the readback flush and the fp64 narrowing.
const Uint8* hostBytes = nullptr;
#endif
#if MOBILEGL_BUILD_DISAGGREGATED
// P11 B2 (T0): the client's AHardwareBuffer this store IS - imported with
// glBufferStorageExternalEXT, so `id` is immutable, persistentMapped and
// persistentPtr is this server's own coherent map of the client's pages. The
// reference is this twin's own, released only after a fence that follows the
// store's last use has signalled (RetireT0Import). Survives an ES context loss:
// the next ensure re-imports it. Null for every store T0 did not adopt.
void* externalAhb = nullptr;
// P8-C: A SHADER WROTE THIS STORE ON THIS SIDE since the R-11 staged copy (or, for
// T0, the coherent map) was last known to match it - set by the draw / dispatch that
// bound it writable (storage block, atomic counter, writable buffer image), cleared
// by SplitHostBytesForCpuRead's refresh and by a respecify. Only the server's own
// CPU readers consult it; the client keeps its own set (GpuWritePending.h).
Bool serverGpuWritten = false;
#endif
};
@@ -1058,8 +1136,14 @@ namespace MobileGL::MG_Backend::DirectGLES {
// HandleOf, no ForEachLive), and the handle overloads never look at it. Death is
// announced by the family's own ResourceDestroy call, not by the shared death notice
// (D-L), and the slot is freed by the CLIENT after that call returns.
//
// P14 S6: A BUCKET REGISTRY, like the six TwinRegistry tables. A buffer twin owns a
// driver buffer id, so the SAME {slot, gen} in two contexts that do not share a group
// must be two resources - and it was one process-wide table until this slice. The
// handle-keyed member-for-member forwarding in SlotTables.h is what leaves every
// `g_backendBufferResources.X(...)` call site below untouched.
using BackendBufferResourceTable =
BackendSlotTable<MG_State::GLState::BufferObject, GLESBufferResource, MG_Pipe::MGPipeKind::Buffer>;
SlotTableRegistry<MG_State::GLState::BufferObject, GLESBufferResource, MG_Pipe::MGPipeKind::Buffer>;
extern BackendBufferResourceTable g_backendBufferResources;
// Resolved once per process and latched, exactly like EsprytSlotTablesEnabled() and
@@ -1104,6 +1188,34 @@ namespace MobileGL::MG_Backend::DirectGLES {
void RequireStagedCoverage(GLESBufferResource& resource, const Uint8* hostBase, SizeT start,
SizeT end, const char* site);
// P11 B2: THE CPU-READABLE BYTES OF A SPLIT SERVER'S BUFFER TWIN, for the handle-arm
// readers that have no frontend object (indirect commands, primitive-restart
// substitution, the multi-draw index rebase): the R-11 staged copy, or - for a T0 store,
// which has none - this server's own coherent map of the client's AHardwareBuffer, read
// exactly as the monolith arm reads the adopted map through MappedData().
inline const Uint8* SplitHostBytes(const GLESBufferResource& resource) {
if (resource.hostBytes != nullptr) return resource.hostBytes;
return resource.externalAhb != nullptr ? static_cast<const Uint8*>(resource.persistentPtr) : nullptr;
}
// P8-C: THE SAME BYTES, MADE CURRENT FIRST - for a reader that CONSUMES them (the restart
// rewrite, the multi-draw rebase, the *IndirectCount count, an indirect command's
// CPU-fed fields, the XFB scatter's pre-capture bytes). SplitHostBytes above is only
// "what uploads staged": a store a shader wrote on this side (serverGpuWritten) is
// refreshed from this server's own GL buffer - queued uploads flushed into it first,
// then the whole store read back into the staged copy, which also gives an orphaned
// store its first copy - and a T0 store lands its queued resident writes and finishes
// before its coherent map is read. A clean staged store costs one flag test.
// `res` names the record whose descriptor width the copy takes; `site` names the
// reader in the one-time diagnostic when the refresh cannot run.
const Uint8* SplitHostBytesForCpuRead(GLESBufferResource& resource, MG_Pipe::MGPipeHandle res,
const char* site);
// P8-C: bytes the SERVER itself put into a store's GL buffer and already holds on the CPU
// (an XFB capture it mapped for the client's writeback, a scatter it composed) go into
// the staged copy as well, so no refresh is owed for them. A T0 store has no copy.
void StageServerWrittenRange(GLESBufferResource& resource, MG_Pipe::MGPipeHandle res, const void* bytes,
SizeT offset, SizeT size);
// M-3's WHOLE-STORE predicate, and the GUARD every caller of RequireStagedCoverage over
// a whole-store read has to carry: true when the application DECLARED the store's
// content (glBufferData(size, data)), so a coverage gap is a missing record; false when
@@ -1143,10 +1255,14 @@ namespace MobileGL::MG_Backend::DirectGLES {
// ("do not take tier 1"), which drops the range onto the staging ring and costs a copy
// rather than a corruption.
//
// `ringCopyInFlight`: the store still has an upload-ring glCopyBufferSubData queued that the
// GPU may not have run (GLESBufferResource::ringCopyRetireSerial not yet retired).
//
// Returns the glMapBufferRange access bits, or 0 when tier 1 must not be taken.
inline constexpr SizeT kEsprytInvalidateRangeMinBytes = 128u * 1024u;
constexpr GLbitfield InvalidateFlushAccessFor(SizeT queuedStart, SizeT queuedEnd, SizeT mapStart,
SizeT mapEnd, SizeT limit, SizeT storageSize) {
SizeT mapEnd, SizeT limit, SizeT storageSize,
Bool ringCopyInFlight) {
if (mapEnd <= mapStart) return 0u;
// THE WIDENING REFUSAL. Not >=, not "covers": exactly, in both directions. A map
// narrower than the queued range leaves bytes unwritten inside a range it has just
@@ -1158,6 +1274,13 @@ namespace MobileGL::MG_Backend::DirectGLES {
// page-substitution fast path needs a page-coverable range to engage, and below it
// the driver falls back to waiting out the WAR hazard on the CPU.
if (!wholeBuffer && size < kEsprytInvalidateRangeMinBytes) return 0u;
// A partial range-invalidating map must not overtake a ring copy into this store the GPU
// has not run yet. Adreno 830 answers such a map on a store with a copy still queued by
// losing the copy (dev f973008c, the pull arm's FlushPendingRangesNow: create-instancing
// drew whole Embeddium terrain sections empty, SSIM 0.870). The range goes to the ring
// instead, GPU-ordered behind the copies. The whole-buffer orphan-map is exempt: it
// rewrites every byte from the shadow the queued copies were taken from.
if (!wholeBuffer && ringCopyInFlight) return 0u;
return GL_MAP_WRITE_BIT |
(wholeBuffer ? GL_MAP_INVALIDATE_BUFFER_BIT : GL_MAP_INVALIDATE_RANGE_BIT);
}
@@ -1243,6 +1366,9 @@ namespace MobileGL::MG_Backend::DirectGLES {
void UnregisterBufferBackendOps();
// The ES context died: unregister ops, invalidate all outstanding GL ids
// (they belonged to the dead context) and drop deferred deletes.
// P14: a server session ended while another session's native context is still alive (one
// process-wide share group): drops only the ending session's own staged bytes.
void OnServerSessionEndedWithOthersLive();
void OnBackendContextDestroyed();
// Get-or-create the backend resource and bring its storage up to date
@@ -1267,6 +1393,10 @@ namespace MobileGL::MG_Backend::DirectGLES {
// Deletes GL buffers whose owning frontend objects died (possibly on a
// thread without a current ES context). Called from draw-time sync.
void ProcessDeferredBufferReleases();
#if MOBILEGL_BUILD_DISAGGREGATED
// P11 B2: the T0 half of that drain - run right after it (retired AHardwareBuffers).
void ProcessDeferredT0Retires();
#endif
// glBindBuffer with a redundant-bind cache for GL_ARRAY_BUFFER.
void BindBufferId(GLenum target, Uint id);
@@ -1963,7 +2093,10 @@ namespace MobileGL::MG_Backend::DirectGLES {
// allocation, or capture the old native contents, merge pending
// upload regions and re-arm the server's whole-level replay set.
// First allocations use staged bytes directly. No frontend dirty
// flag, allocator or pixel-store object participates.
// flag, allocator or pixel-store object participates. P9 W2: a level
// the driver does not hold (defined after the last sync) is never read
// back, and a readback the driver refuses falls back to the staged
// level when the store covers it (notes/p9/W2-REMINT.md).
void RequireImageBindableStorageByHandle(MG_Pipe::MGPipeHandle res,
const MG_Pipe::MGPipeResourceRecord& record);
#endif
@@ -1972,6 +2105,9 @@ namespace MobileGL::MG_Backend::DirectGLES {
// carrier the storage is an integer texture holding codes and glGetTexImage still owes
// the application floats.
Bool RequiresImageBindableStorage() const { return m_imageBindableStorageRequired; }
// See m_driverMayHoldGpuWrites. Callers holding the STATE texture use
// NoteDriverSideTextureWrite, which also reaches the storage behind a view.
void NoteDriverSideWrite() { m_driverMayHoldGpuWrites = true; }
void Bind(GLenum target, Uint unit = TempTextureUnit);
Uint GetBackendTextureId() const;
@@ -2088,6 +2224,15 @@ namespace MobileGL::MG_Backend::DirectGLES {
Bool m_isInitialized = false;
Bool m_imageBindableStorageRequired = false;
Bool m_backendStorageImmutable = false;
// Whether the driver texture may hold texels the shadow never saw. Set the first time
// anything other than a texel upload can write it - an application framebuffer
// attachment, a writable image binding, a copy or blit into it, a driver-side mipmap
// generation - and never cleared. Until then the driver holds, outside a level's
// pending writes, exactly the shadow bytes it was last sent, so those writes may go up
// as their union box (one unpack-ring job, which is what Mali needs); from then on
// only the texels the client wrote may go up, since the shadow is stale everywhere
// else. See SyncMipmapsToBackend.
Bool m_driverMayHoldGpuWrites = false;
// Latches the "this driver has no buffer textures" report to once per texture. The
// report is emitted from the respecify path, which bails before recording the state
// it was asked to apply - so without the latch the texture stays permanently dirty
@@ -2105,10 +2250,16 @@ namespace MobileGL::MG_Backend::DirectGLES {
// unconditionally bumps it), the content version on every CPU pixel mutation, and
// the params version covers SetSamples/SetFixedSampleLocations, which bump neither
// of the other two but feed the shape probe. The context id pins the generation to
// the context that produced it - generations restart at 0 with a new context, and a
// texture is owned by exactly one context (share groups are not implemented), so a
// mutation can never happen under a context this key does not name. 0 = never
// stamped (real context ids start at 1). Backend-side invalidation rides on
// the context that produced it - generations restart at 0 with a new context, so the
// id is part of the key. IT IS NOT AN OWNERSHIP CLAIM: a texture belongs to a SHARE
// GROUP (P14 S3/S6 - share groups ARE implemented, `ShareGroupState` holds the object
// tables and the twin tables are keyed by {session, share group}), so two contexts of
// one group both legitimately mutate this texture. What the id buys is only the
// cheaper half of the gate: a mutation made under a context sharing this texture
// stamps a different id and re-derives the shape, which is a spurious re-derivation
// (safe, and only on a shape mutation) rather than a missed one - the per-mutation
// versions below are what actually catch a change, and they are group-independent.
// 0 = never stamped (real context ids start at 1). Backend-side invalidation rides on
// m_isInitialized: RequireImageBindableStorage and RecreateBackendTexture clear it.
Uint64 m_syncedShapeContextId = 0;
Uint64 m_syncedShapeGeneration = 0;
@@ -2257,6 +2408,11 @@ namespace MobileGL::MG_Backend::DirectGLES {
SharedPtr<BackendTextureObject>& SyncTextureObjectToBackend(
const SharedPtr<MG_State::GLState::ITextureObject>& textureObject,
Bool imageBindableStorageRequired = false);
// Every path that lets the DRIVER write a texture's texels calls this (see
// BackendTextureObject::m_driverMayHoldGpuWrites): an application framebuffer attachment,
// a writable image binding, a copy, blit or mipmap generation into it. A view writes its
// storage texture's image, so the storage texture is the one marked.
void NoteDriverSideTextureWrite(const SharedPtr<MG_State::GLState::ITextureObject>& textureObject);
#if MOBILEGL_PIPE_PUSH
// ---- P5e SEAM (MG_Remote/CONTRACT-P5E.md §4.2, §5.2; declared by c0e, bodied by
// id/tx2) --------------------------------------------------------------------------
@@ -2273,6 +2429,29 @@ namespace MobileGL::MG_Backend::DirectGLES {
SharedPtr<BackendTextureObject>& SyncTextureToBackendByHandle(
MG_Pipe::MGPipeHandle texture, Bool imageBindableStorageRequired = false);
BackendTextureObject* ResolveTextureTwin(MG_Pipe::MGPipeHandle texture);
// NoteDriverSideTextureWrite's HANDLE ARM, for the same paths when the caller holds a
// Texture handle (a framebuffer surface's Res, an image unit's Res, VerbMipRes,
// VerbCopyTexDst, a copy-image endpoint): the twin the handle's record names is marked, or
// its storage owner's (Desc.ViewOf) for a view. Nothing on the apply thread reads a
// frontend object for it.
//
// P8-E: under a transport the same storage owner's staged store is marked driver-written
// too (StagedTextureStore::MarkDriverWritten), so a refused readback does not answer from
// bytes the driver has since overwritten - unless `storeFollowsTheWrite`, which only
// glCopyImageSubData passes: it tries FollowCopyImageInStagedStore first and marks the store
// itself when that fails.
void NoteDriverSideTextureWriteByHandle(MG_Pipe::MGPipeHandle texture, Bool storeFollowsTheWrite = false);
#endif
#if MOBILEGL_BUILD_DISAGGREGATED
// P8-E (notes/p8/E.md): make glCopyImageSubData's texel-block move in the server's staged
// store too (StagedTextureStore::FollowCopy), so the destination level's bytes stay its
// content. Box in each image's own texel grid. False when the store cannot follow: a cube
// map or 1D array endpoint (copy-image puts faces / layers on z, the store does not), a view,
// two different internal formats, a level whose bytes did not all cross, a source the
// driver has written, or a monolith arm.
Bool FollowCopyImageInStagedStore(MG_Pipe::MGPipeHandle source, Uint32 sourceLevel, const IntVec3& sourceOrigin,
MG_Pipe::MGPipeHandle destination, Uint32 destinationLevel,
const IntVec3& destinationOrigin, const IntVec3& size);
#endif
// Brings every texture the next draw reads - the touched units' bindings and the draw
// FBO's texture attachments - onto the backend, through the two borrowed-pair memos
@@ -3030,6 +3209,11 @@ namespace MobileGL::MG_Backend::DirectGLES {
// Same for gl_BaseVertex: only a program that reads it pays for the per-draw
// uniform write, and only such a program needs the reset after one.
Bool ReadsBaseVertex() const { return m_baseVertexUniformLocation >= 0; }
#if MOBILEGL_BUILD_DISAGGREGATED
// P8-C: same for the gl_BaseInstance uniform - which an indirect draw feeds from the
// command on the CPU only when the mg_IndirectParams view is absent.
Bool ReadsBaseInstance() const { return m_baseInstanceUniformLocation >= 0; }
#endif
// Which viewport indices the next draw's fragments may keep, one bit each. Written
// once per replay pass; see ForEachViewportRoutingPass.
void SetViewportPassMask(Uint32 indexMask) const;
@@ -3525,7 +3709,9 @@ namespace MobileGL::MG_Backend::DirectGLES {
// what makes HandleOf below resolve at all. Not one member that would dereference it is
// instantiated - no Find(StateObject*), no ForEachLive - and the handle overloads never
// look at it.
using BackendSamplerViewTable = BackendSlotTable<MG_State::GLState::ITextureObject,
// P14 S6: and a bucket registry for the same reason the buffer one above is - a sampler
// view's twin is a driver object of the texture's group.
using BackendSamplerViewTable = SlotTableRegistry<MG_State::GLState::ITextureObject,
BackendSamplerViewObject,
MG_Pipe::MGPipeKind::SamplerViewCso>;
extern BackendSamplerViewTable g_backendSamplerViews;
+7 -6
View File
@@ -191,15 +191,16 @@ namespace MobileGL::MG_Backend::DirectGLES::MultiDrawImpl {
if (question == IndexBufferQuestion::Presence) return view;
if (question == IndexBufferQuestion::HostBytes) {
// No Sync* pair here and none is missing: on this side the applier has
// already consumed the persistent-map blocks and the shader writebacks for
// this resource before the draw verb replayed, so the server's staged shadow
// IS the synced copy. That is also why this arm cannot be expressed as "find
// the object and run the monolith body".
// The applier has already consumed the persistent-map blocks for this
// resource before the draw verb replayed; the one Sync* this arm owes is the
// monolith's SyncGpuWrites, and (P8-C) SplitHostBytesForCpuRead is it: a store
// a shader wrote on this side is read back from this server's own GL buffer,
// because no writeback ever reaches the server's staged copy. That is also why
// this arm cannot be expressed as "find the object and run the monolith body".
auto* resource = BufferImpl::FindBufferResourceForHandle(res);
if (resource == nullptr) RefuseMissingIndexBufferRecord(entry, res, "backend resource");
view.Size = BufferImpl::ResourceWidthForHandle(res);
view.HostBytes = resource->hostBytes;
view.HostBytes = BufferImpl::SplitHostBytesForCpuRead(*resource, res, "multidraw_index_rebase");
if (view.HostBytes != nullptr && IndexBufferRecordHasDefinedContent(st, res)) {
BufferImpl::RequireStagedCoverage(*resource, view.HostBytes, 0, view.Size,
"multidraw_index_rebase");
+267
View File
@@ -11,6 +11,12 @@
#include <MG_Pipe/MGPipeHandles.h>
// P14 S6: the key this table is filed under includes the calling thread's {session, share
// group}, and the probe that answers it lives on the backend's public surface beside S4's
// native-tuple key (DirectGLES.h). Included here so this header is self-contained for the two
// translation units that include it without copying Managers.h's include order.
#include "DirectGLES.h"
#if MOBILEGL_PIPE_PUSH
#include <MG_Impl/Pipe/SlotAllocator.h>
#endif
@@ -155,6 +161,249 @@ namespace MobileGL::MG_Backend::DirectGLES {
return enabled;
}
// =====================================================================================
// P14 S6 (docs/Disaggregated/design/11-state-ownership.md): THE TWIN TABLE IS PER
// {session, share group}, NOT PER PROCESS.
// =====================================================================================
//
// WHAT WAS WRONG, in one sentence: a twin is a DRIVER object - a GL buffer id, a program, a
// texture, the storage behind them - so it belongs to one native context's object namespace,
// and a table keyed by {slot, gen} ALONE resolves the same handle value to the same twin
// whichever context asked. The frontend gives two contexts that do NOT share a group two
// independent name spaces, so the same {slot, gen} in both is legal and means two different
// objects; one process-wide table made the second context adopt the first's twin (including
// its driver ids, its persistent map and its pooled store). At the same time two contexts of
// ONE group - which must see one set of objects - were served by whichever of them twinned
// first, i.e. by luck rather than by the group the two agree on.
//
// THE KEY IS the same pair the applier files its OBJECT RECORDS under: {session, share
// group}. Same probe, same `0 means this context started its own group` rule (see
// DirectGLES.h's TwinKey and SessionRuntime.cpp's ResolveThreadTwinKey), so a record and the
// twin it describes are always in the same bucket. Session is in the key because a twin
// names a driver id and no driver id crosses a connection - two connections in one process
// are two clients, and nothing may be shared with either from the other.
//
// THE HOLDER LIST STAYS PROCESS-WIDE AND THAT IS NOT A COMPROMISE. A notice is about an
// OBJECT, not about a bucket: OnFrontendObjectDestroyed and ReleaseTwinByHandle must reach
// the by-value copies a fixture or a context reset is holding, whichever bucket they sit in,
// and "which bucket holds it" is exactly the question that produced the two-holder leak the
// list exists to fix. Each SHARED holder of the list is ONE bucket; the walk therefore covers
// every bucket of every table.
//
// THE SAME TABLE OBJECT IS REUSED, NOT REPLACED, and that is what keeps ~200 call sites
// alive: `g_backendTextureObjects.Find(...)` still answers from the bucket the calling thread
// is in, because the object the global names multiplexes on the key at each lookup. A caller
// that must address a bucket OTHER than the calling thread's - the death walks, the
// session-teardown drop - takes Shared() or calls the static entries, which walk buckets;
// and a caller that wants the whole process's view (the teardown, the tests) takes Snapshot().
// FORWARD-DECLARED, because the registry below is DEFINED before the table it holds; the
// members that name the table are defined out of line at the bottom of this header.
template <typename StateObject, typename BackendObject, MG_Pipe::MGPipeKind kKind>
class BackendSlotTable;
template <typename StateObject, typename BackendObject, MG_Pipe::MGPipeKind kKind>
class SlotTableRegistry {
public:
using Table = BackendSlotTable<StateObject, BackendObject, kKind>;
using Key = TwinKey;
using StatePtr = SharedPtr<StateObject>;
using BackendPtr = SharedPtr<BackendObject>;
// The bound the two tables that ARE a bucket registry have always spelled by name; the
// six registries reach it through their own `SlotTable` alias.
static constexpr Uint32 kMaxHandleSlot = 1u << 20;
// One bucket. SHARED, so a caller may keep it across other lookups: the map owns the
// pointee and never moves it.
SharedPtr<Table> Shared(const Key& key) const {
// The common case on the hot path: the same key as last time. One compare, no probe.
if (m_memoTable != nullptr && m_memoKey == key) return m_memoTable;
const auto found = m_buckets.find(key);
if (found != m_buckets.end()) {
m_memoKey = key;
m_memoTable = found->second;
return found->second;
}
SharedPtr<Table> fresh = MakeShared<Table>();
m_buckets.emplace(key, fresh);
m_memoKey = key;
m_memoTable = fresh;
return fresh;
}
// The calling thread's bucket - the ONE entry every in-place call site below resolves
// through, and the process-wide answer on a thread that belongs to no session.
//
// CONST, AND IT ANSWERS A MUTABLE BUCKET: the index is `mutable` because a LOOKUP may
// have to create the bucket it is about to miss in (a handle with no twin yet is the
// ordinary case), and the bucket's own entries are the table's business rather than the
// registry's. The alternative - a const overload returning "no table" - would make every
// read-only caller handle a case that cannot arise.
Table& ForCallingThread() const {
Key key;
CurrentTwinKey(&key);
return *Shared(key);
}
// Every bucket, in no particular order.
Vector<SharedPtr<Table>> Snapshot() const {
Vector<SharedPtr<Table>> all;
all.reserve(m_buckets.size());
for (const auto& bucket : m_buckets) all.push_back(bucket.second);
return all;
}
SizeT BucketCount() const { return m_buckets.size(); }
// THE PER-BUCKET ONE-SHOT, because a bucket held by SharedPtr is never dropped by an
// assignment: a caller that means "this context/group is gone" has to be able to say so,
// and the key is how it says which one. The bucket LEAVES the index before its twins are
// released, so a twin destructor that re-entered Shared() cannot find a half-erased one.
Bool Drop(const Key& key) {
const auto found = m_buckets.find(key);
if (found == m_buckets.end()) return false;
SharedPtr<Table> dropped = found->second;
m_buckets.erase(found);
if (m_memoTable == dropped) {
m_memoKey = Key{};
m_memoTable = nullptr;
}
dropped->Reset();
return true;
}
// EVERY bucket of one session, for the teardown of a session that owned several groups.
// The session is the key's high half, so this is one pass and no per-group bookkeeping -
// and it is the difference between "this session's twins go" and "the process's do".
Uint32 DropSession(Uint64 sessionKey) {
Uint32 dropped = 0;
for (auto bucket = m_buckets.begin(); bucket != m_buckets.end();) {
if (bucket->first.SessionKey != sessionKey) {
++bucket;
continue;
}
SharedPtr<Table> table = bucket->second;
if (m_memoTable == table) {
m_memoKey = Key{};
m_memoTable = nullptr;
}
bucket = m_buckets.erase(bucket);
table->Reset();
++dropped;
}
return dropped;
}
// Replace this registry with an empty one, releasing every bucket's twins. This is what
// `registry = {}` meant when the registry WAS one table; a caller that must keep the
// tables alive past the call takes a Snapshot() first.
void Reset() {
const Vector<SharedPtr<Table>> all = Snapshot();
m_buckets.clear();
m_memoKey = Key{};
m_memoTable = nullptr;
for (const SharedPtr<Table>& table : all) table->Reset();
}
// ---- the calling thread's bucket, member for member ---------------------------------
//
// THIS IS WHY THE SHAPE COST THE CALL SITES NOTHING. Every handle-keyed entry the table
// has is repeated here and forwarded to the bucket the calling thread resolves to, so
// `g_backendTextureObjects.FindByHandle(h)` and `g_backendBufferResources.GetOrCreate(h)`
// - unchanged source - answer from the right context's object namespace. A caller that
// must name a bucket OTHER than its own (a death walk, a case) takes Shared()/Snapshot()
// and calls the table directly.
BackendPtr& GetOrCreate(MG_Pipe::MGPipeHandle handle) {
return ForCallingThread().GetOrCreate(handle);
}
BackendPtr* FindByHandle(MG_Pipe::MGPipeHandle handle) {
return ForCallingThread().FindByHandle(handle);
}
const BackendPtr* FindByHandle(MG_Pipe::MGPipeHandle handle) const {
return ForCallingThread().FindByHandle(handle);
}
// Resolve-or-create in a bucket the caller NAMES rather than the one it is in. The
// monolith-glue frontend-keyed entry (StateBackendObjectRegistry::GetOrCreate below)
// needs it: a state object handed over on the client side of a sync is described by a
// record in the CALLING thread's group, and a twin built for it must land in the same
// bucket as that record.
BackendPtr& GetOrCreateIn(const Key& key, MG_Pipe::MGPipeHandle handle) {
return Shared(key)->GetOrCreate(handle);
}
Uint32 LiveGenAt(Uint32 slot) const { return ForCallingThread().LiveGenAt(slot); }
MG_Pipe::MGPipeHandle HandleOf(const StateObject* stateObj) const {
return ForCallingThread().HandleOf(stateObj);
}
void NoteStateForHandle(MG_Pipe::MGPipeHandle handle, const StatePtr& stateObj) {
ForCallingThread().NoteStateForHandle(handle, stateObj);
}
StatePtr StateForHandle(MG_Pipe::MGPipeHandle handle) const {
return ForCallingThread().StateForHandle(handle);
}
template <typename Fn>
void ForEachLive(Fn&& fn) const {
// THE CALLING THREAD'S BUCKET, not every bucket, and that is the semantic answer
// rather than a shortcut: a caller walking twins is asking "which of the objects I
// can see need this", and what it can see is its own share group. Every bucket in
// one process would walk a neighbour session's objects.
ForCallingThread().ForEachLive(fn);
}
// Every bucket, in one pass. The caller receives the key with the handle so it can tell
// which context a twin belongs to - the only walks that need the whole process's view
// are a diagnostic and a teardown.
template <typename Fn>
void ForEachBucket(Fn&& fn) const {
for (const auto& bucket : m_buckets) {
bucket.second->ForEachLive(
[&](MG_Pipe::MGPipeHandle handle, const BackendPtr& twin) { fn(bucket.first, handle, twin); });
}
}
// The process-wide answer for the two statics: a notice is about an OBJECT, so it must
// reach the twin in EVERY bucket of EVERY holder. They forward to the table's own
// statics, which walk the holder list - each SHARED holder of it is one bucket.
static Bool ReleaseTwinByHandle(MG_Pipe::MGPipeHandle handle) {
return Table::ReleaseTwinByHandle(handle);
}
static Bool OnFrontendObjectDestroyed(Uint64 lifetimeId) {
return Table::OnFrontendObjectDestroyed(lifetimeId);
}
// The instance spelling, and it HANDS THE TWIN OUT rather than answering a Bool: the two
// tables that ARE a bucket registry (buffers, sampler views) have always been called as
// `g_backendBufferResources.ReleaseByHandle(res)` and their callers still have to decide
// what happens to the driver id the twin owns. The bucket is the CALLING THREAD'S, which
// is the bucket that twin was created in.
BackendPtr ReleaseByHandle(MG_Pipe::MGPipeHandle handle) {
return ForCallingThread().ReleaseByHandle(handle);
}
static Uint32 HolderCount() { return Table::HolderCount(); }
Uint32 LiveCount() const { return ForCallingThread().LiveCount(); }
Uint32 CompositeLiveCount() const { return ForCallingThread().CompositeLiveCount(); }
SizeT OrdinaryCapacityForTest() const { return ForCallingThread().OrdinaryCapacityForTest(); }
SizeT CompositeCapacityForTest() const { return ForCallingThread().CompositeCapacityForTest(); }
// ---- the bucket index's own controls, for the two tables that ARE a registry --------
//
// The six TwinRegistry registries carry the same three under the names in Managers.h
// (DropBucket / DropBucketsOfSession / ResetBuckets); they are repeated here because
// SlotTableRegistry IS the public type of the buffer and sampler-view tables, so a caller
// of those has no registry object in between.
Bool DropBucket(const Key& key) { return Drop(key); }
// Drop every bucket of one session; answers how many groups that took with it.
Uint32 DropBucketsOfSession(Uint64 sessionKey) { return DropSession(sessionKey); }
// Empty every bucket (the process-shaped world's teardown, key {0, 0}).
void ResetBuckets() { Reset(); }
private:
// ONE BUCKET PER LIVE {session, share group}, held by SHARED pointer so a caller may
// snapshot one across other lookups and so `Drop` can take a bucket's twins away
// explicitly - an embedded table could do neither. `std::unordered_map` rather than the
// tree's own flat Vector: this map is probed once per twin lookup on the draw path.
mutable std::unordered_map<Key, SharedPtr<Table>, TwinKeyHash> m_buckets;
// Hot-path memo. A registry is per KIND and each kind is touched several times per draw;
// a thread only ever moves between buckets when its session binds another context.
mutable SharedPtr<Table> m_memoTable;
mutable Key m_memoKey;
};
template <typename StateObject, typename BackendObject, MG_Pipe::MGPipeKind kKind>
class BackendSlotTable {
public:
@@ -616,6 +865,24 @@ namespace MobileGL::MG_Backend::DirectGLES {
}
}
// Empty this table, releasing every twin it holds. THE SPACES ARE MOVED OUT FIRST and
// that is the whole of the care taken here: a twin's destructor is a driver call that
// could re-enter GetOrCreate on this table (SlotTables.h's ReleaseTwinAt says why), and
// destroying the entries in place would leave that re-entry resizing the vector the
// destructors are being walked out of. Here the table is already empty when the entries
// die, so a re-entrant insert grows a fresh vector.
//
// This is what `table = {}` used to mean, spelled so a caller cannot miss the ordering.
void Reset() {
Vector<Entry> slots = std::move(m_slots);
Vector<Entry> band = std::move(m_band);
BackendPtr nullTwin = std::move(m_nullTwin);
ForgetHandle();
slots.clear();
band.clear();
nullTwin.reset();
}
Uint32 LiveCount() const {
Uint32 count = 0;
for (const Entry& entry : m_slots) {
@@ -1,6 +1,7 @@
// Included in DirectGLES.cpp after the native packed-word readback helper.
// The server needs owned tight rows for a storage promotion, not client PACK
// layout or a fallback into a frontend texture's potentially stale shadow.
// A storage promotion needs the driver's own texels as owned tight rows, not
// client PACK layout or a fallback into a frontend texture's potentially stale
// shadow.
class ScopedWireTextureReadLevel {
public:
ScopedWireTextureReadLevel(GLuint texture, GLenum target, GLint level)
@@ -280,6 +281,144 @@
return true;
}
#if MOBILEGL_BUILD_DISAGGREGATED
// P9 W2-a. The store's Defined-ness follows the client's respecifies; the driver's follows the
// twin's last sync. A re-mint asks this before reading a level back, because a level defined
// after that sync (null data, or null data plus a partial upload) is not on the driver yet and
// its FBO attachment is incomplete - which used to end the session at
// Fatal{ResourceUnavailable, "image-promotion-readback"} on legal GL.
Bool NativeTextureLevelHasExtent(GLuint texture, TextureTarget target, TextureUploadTarget uploadTarget,
GLint level, const IntVec3& logicalExtent) {
if (!texture || level < 0 || !g_GLESFuncs.glGetTexLevelParameteriv) return true;
const IntVec3 extent = TextureImpl::GetBackendUploadSize(target, logicalExtent);
const GLenum nativeTarget = TextureImpl::ConvertTextureTargetToBackendGLEnum(target);
GLenum binding = GL_NONE;
switch (nativeTarget) {
case GL_TEXTURE_2D: binding = GL_TEXTURE_BINDING_2D; break;
case GL_TEXTURE_3D: binding = GL_TEXTURE_BINDING_3D; break;
case GL_TEXTURE_2D_ARRAY: binding = GL_TEXTURE_BINDING_2D_ARRAY; break;
case GL_TEXTURE_CUBE_MAP: binding = GL_TEXTURE_BINDING_CUBE_MAP; break;
case GL_TEXTURE_CUBE_MAP_ARRAY: binding = GL_TEXTURE_BINDING_CUBE_MAP_ARRAY; break;
default: return true;
}
// A cube's levels are per face; every other target answers for the whole level.
const GLenum queryTarget = nativeTarget == GL_TEXTURE_CUBE_MAP
? TextureImpl::ConvertTextureUploadTargetToBackendGLEnum(uploadTarget) : nativeTarget;
DrainESErrors();
GLint previous = 0;
g_GLESFuncs.glGetIntegerv(binding, &previous);
g_GLESFuncs.glBindTexture(nativeTarget, texture);
GLint width = 0, height = 0, depth = 0;
g_GLESFuncs.glGetTexLevelParameteriv(queryTarget, level, GL_TEXTURE_WIDTH, &width);
g_GLESFuncs.glGetTexLevelParameteriv(queryTarget, level, GL_TEXTURE_HEIGHT, &height);
g_GLESFuncs.glGetTexLevelParameteriv(queryTarget, level, GL_TEXTURE_DEPTH, &depth);
const Bool answered = g_GLESFuncs.glGetError() == GL_NO_ERROR;
g_GLESFuncs.glBindTexture(nativeTarget, static_cast<GLuint>(previous));
if (!answered) return true;
const Bool layered = nativeTarget == GL_TEXTURE_3D || nativeTarget == GL_TEXTURE_2D_ARRAY ||
nativeTarget == GL_TEXTURE_CUBE_MAP_ARRAY;
return width == extent.x() && height == extent.y() && (!layered || depth == extent.z());
}
// P8-E (notes/p8/E.md). MGITEST_ESPRYT_REFUSE_TEXTURE_READBACK_EXTENT=<w>x<h>, READ BY THE SERVER:
// a colour level of exactly that extent is treated as one the driver refused to attach - what an
// Adreno does for RGB16 and the 16-bit SNORM formats, and what no host driver can be made to do.
// Scoped by the extent so the tcp lane's shared supervisor can carry it for the whole lane: only
// the scenario that builds that extent (CopyImageStoreReadbackScenario) ever meets it.
static Bool WireTextureReadbackRefusedForTesting(const IntVec3& extent) {
static const IntVec3 s_refused = [] {
const char* value = std::getenv("MGITEST_ESPRYT_REFUSE_TEXTURE_READBACK_EXTENT");
if (value == nullptr) return IntVec3{0, 0, 0};
char* end = nullptr;
const long width = std::strtol(value, &end, 10);
if (end == value || *end != 'x') return IntVec3{0, 0, 0};
const long height = std::strtol(end + 1, nullptr, 10);
if (width <= 0 || height <= 0) return IntVec3{0, 0, 0};
return IntVec3{static_cast<Int>(width), static_cast<Int>(height), 1};
}();
return s_refused.x() > 0 && extent == s_refused;
}
// P8-E: THE STORE ROUTE, for a level the driver refused to read back. The server's staged store
// is the only CPU copy of the level on this side, and it answers only while its bytes ARE the
// level: every byte crossed (IsCovered), at the extent asked for, and the texture was never
// written by the driver except by copies the store followed (!IsDriverWritten - see
// StagedTextureStore's P8-E block and FollowCopyImageInStagedStore). The conversion is
// GetTexImageViaShadowConversion's, into tight rows: a packed format read with its own type
// takes the stored words verbatim, everything else decodes to wide RGBA and converts row by row.
// Anything else DECLINES BY NAME: the reply is an error, and the client's
// Fatal{ReplyError, "ReadPixels"} ends the session instead of handing back bytes the driver has
// since overwritten.
static Bool ReadWireLevelFromStagedStore(MG_Pipe::MGPipeHandle res, TextureInternalFormat logicalFormat,
TextureUploadTarget uploadTarget, GLint level, const IntVec3& extent,
GLenum format, GLenum type, Vector<Uint8>& bytes) {
auto& store = MG_Remote::Server::ServerStagedTexture();
const Uint64 key = MG_Remote::Server::StagedTextureStore::KeyForHandle(res);
const auto target = static_cast<Uint16>(uploadTarget);
const auto mip = static_cast<Uint16>(level);
ReadbackChannelMapping mapping{};
const SizeT pixelBytes = GetReadbackChannelMapping(format, mapping) ? GetReadbackDstPixelSize(mapping, type) : 0;
const SizeT texels = static_cast<SizeT>(std::max(extent.x(), 0)) * static_cast<SizeT>(std::max(extent.y(), 0)) *
static_cast<SizeT>(std::max(extent.z(), 0));
const char* declined = nullptr;
if (!store.CopiesIntoServerStorage()) {
declined = "this arm keeps no staged store";
} else if (store.IsDriverWritten(key)) {
declined = "the driver has written the texture since its bytes crossed";
} else if (!store.IsCovered(key, target, mip)) {
declined = "not every byte of the level crossed";
} else if (store.LevelExtentOrUndefined(key, target, mip) != extent) {
declined = "the staged level is not the extent asked for";
} else if (pixelBytes == 0 || texels == 0) {
declined = "the format/type pair has no readback mapping";
}
Vector<Uint8> out;
if (declined == nullptr) {
const Uint8* staged = store.RequireLevelBytes(key, target, mip, "texture-readback-from-store");
if (MG_Util::PixelStoreProcessor::IsRawPackedPixelTransfer(
logicalFormat, MG_Util::ConvertGLEnumToTextureInputFormat(format),
MG_Util::ConvertGLEnumToTexturePixelDataType(type))) {
if (store.LevelByteSize(key, target, mip) != texels * pixelBytes) {
declined = "the staged words are not the client's word size";
} else {
out.assign(staged, staged + texels * pixelBytes);
}
} else {
Vector<Uint8> wide;
Bool isInteger = false;
Bool isSigned = false;
if (!MG_Util::PixelStoreProcessor::DecodeShadowDataToWideRGBA(logicalFormat, staged, texels, wide,
isInteger, isSigned)) {
declined = "the staged format has no wide decoding";
} else if (mapping.isInteger != isInteger) {
declined = "an integer read of a normalized level, or the reverse";
} else {
const GLenum wideType = isInteger ? (isSigned ? GL_INT : GL_UNSIGNED_INT) : GL_FLOAT;
const SizeT rowTexels = static_cast<SizeT>(extent.x());
const SizeT rows = texels / rowTexels;
const SizeT wideRowBytes = rowTexels * 4 * GetReadbackComponentSize(wideType);
out.resize(texels * pixelBytes);
for (SizeT row = 0; row < rows; ++row) {
ReadbackImpl::ConvertWideReadbackRow(wide.data() + row * wideRowBytes,
out.data() + row * rowTexels * pixelBytes, rowTexels,
wideType, mapping, type);
}
}
}
}
if (declined != nullptr) {
MGLOG_E_ONCE("MGPipe: texture {%u,%u} level %d: the driver refused the readback and the staged store "
"cannot answer it (%s) - DECLINED (texture-readback-store-declined)",
res.Slot, res.Gen, level, declined);
return false;
}
MGLOG_W_ONCE("MGPipe: texture {%u,%u} level %d: the driver refused the readback (internal format %u); "
"answered from the server's staged store (texture-readback-from-store)",
res.Slot, res.Gen, level, static_cast<unsigned>(logicalFormat));
bytes = Move(out);
return true;
}
Bool ReadTextureImageWire(const MG_Pipe::MGPReadbackInfo& info, Vector<Uint8>& bytes) {
const auto* record = PipeTextureRecordForHandle(info.Res);
if (!record) return false;
@@ -293,17 +432,32 @@
const Bool wholeCube = info.Target == GL_TEXTURE_CUBE_MAP;
const IntVec3 extent{static_cast<Int>(info.Box.W), static_cast<Int>(info.Box.H),
wholeCube ? 1 : static_cast<Int>(info.Box.D)};
const auto logicalFormat = static_cast<TextureInternalFormat>(record->Desc.InternalFormat);
// The R32UI word leg never goes through a framebuffer, so the knob leaves it alone (it is
// ReadTextureLevelTight's first choice for exactly these reads).
const Bool packedWordRead =
!twin->RequiresImageBindableStorage() &&
MG_Util::PixelStoreProcessor::IsRawPackedPixelTransfer(
logicalFormat, MG_Util::ConvertGLEnumToTextureInputFormat(info.Format),
MG_Util::ConvertGLEnumToTexturePixelDataType(info.Type)) &&
MG_Util::GetInputBytesPerPixel(MG_Util::ConvertGLEnumToTextureInputFormat(info.Format),
MG_Util::ConvertGLEnumToTexturePixelDataType(info.Type)) == sizeof(Uint32);
const Bool refusedForTesting = !packedWordRead && WireTextureReadbackRefusedForTesting(extent);
Vector<Uint8> result;
for (Int face = 0; face < (wholeCube ? 6 : 1); ++face) {
const auto uploadTarget = wholeCube
? static_cast<TextureUploadTarget>(static_cast<Int>(TextureUploadTarget::CubeMapPositiveX) + face)
: MG_Util::ConvertGLEnumToTextureUploadTarget(info.Target);
Vector<Uint8> slice;
if (!ReadTextureLevelTight(twin->GetBackendTextureId(), target, uploadTarget,
static_cast<TextureInternalFormat>(record->Desc.InternalFormat), info.Level, extent,
twin->RequiresImageBindableStorage(), info.Format, info.Type, slice)) return false;
if (refusedForTesting ||
!ReadTextureLevelTight(twin->GetBackendTextureId(), target, uploadTarget, logicalFormat, info.Level,
extent, twin->RequiresImageBindableStorage(), info.Format, info.Type, slice)) {
if (!ReadWireLevelFromStagedStore(info.Res, logicalFormat, uploadTarget, info.Level, extent,
info.Format, info.Type, slice)) return false;
}
result.insert(result.end(), slice.begin(), slice.end());
}
bytes = Move(result);
return true;
}
#endif // MOBILEGL_BUILD_DISAGGREGATED
@@ -345,6 +345,10 @@ namespace MobileGL::MG_Backend::DirectVulkan {
pVulkanRenderer.reset();
ClearProgramResourceCaches();
}
// And the swap interval it asked for (DirectVulkan.h).
if (MG_Config::Transport != MG_Config::TransportMode::Monolith) {
ForgetRequestedSwapInterval();
}
}
#else
BackendObject_DirectVulkan::~BackendObject_DirectVulkan() = default;
@@ -363,7 +367,9 @@ namespace MobileGL::MG_Backend::DirectVulkan {
// Any renderer instance this assignment replaces is destroyed here;
// fence/timer-query handles stamped with the old generation go stale.
BumpRendererGeneration();
pVulkanRenderer = MakeUnique<MG_Backend::DirectVulkan::VulkanRenderer>(nativeWindow);
VulkanRendererConfig config;
config.SwapInterval = GetRequestedSwapInterval();
pVulkanRenderer = MakeUnique<MG_Backend::DirectVulkan::VulkanRenderer>(nativeWindow, config);
MOBILEGL_ASSERT(pVulkanRenderer != nullptr, "InitWindowSurface: VulkanRenderer creation failed");
pVulkanRenderer->Initialize();
return true;
@@ -486,6 +492,12 @@ namespace MobileGL::MG_Backend::DirectVulkan {
return BackendObject::SwapEGLBuffers(dpy, draw);
}
void BackendObject_DirectVulkan::SetEGLSwapInterval(Int interval) {
// Same lock as SwapEGLBuffers: the renderer reads the request in Present.
const std::lock_guard<std::recursive_mutex> lock(m_eglStateMutex);
BackendObject::SetEGLSwapInterval(interval);
}
void BackendObject_DirectVulkan::ReleaseEGLSurface(EGLSurface surface) {
const std::lock_guard<std::recursive_mutex> lock(m_eglStateMutex);
BackendObject::ReleaseEGLSurface(surface);
@@ -727,6 +739,7 @@ namespace MobileGL::MG_Backend::DirectVulkan {
static Bool funcsTableInitialized = false;
if (!funcsTableInitialized) {
funcsTable.Present = Present;
funcsTable.SetSwapInterval = SetSwapInterval;
funcsTable.GL.DrawArrays = DrawArrays;
funcsTable.GL.DrawElements = DrawElements;
funcsTable.GL.DrawElementsBaseVertex = DrawElementsBaseVertex;
@@ -34,6 +34,7 @@ namespace MobileGL::MG_Backend::DirectVulkan {
Bool CreateEGLPbufferSurface(EGLSurface surface, EGLint width, EGLint height) override;
Bool MakeEGLCurrent(EGLDisplay dpy, EGLSurface draw, EGLSurface read, EGLContext ctx) override;
Bool SwapEGLBuffers(EGLDisplay dpy, EGLSurface draw) override;
void SetEGLSwapInterval(Int interval) override;
void ReleaseEGLSurface(EGLSurface surface) override;
void ReleaseEGLResources() override;
@@ -0,0 +1,29 @@
// MobileGL - MobileGL/MG_Backend/DirectVulkan/DescriptorPoolCensus.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>
namespace MobileGL::MG_Backend::DirectVulkan {
// A count of Magma's per-frame descriptor pools and the sets they hold (UniformManager), for
// tests. Dependency-free on purpose: the integration scenarios include it next to the system GL
// headers. They link the static library on desktop; the shipping shared library is built with
// hidden visibility, so on Android nothing outside the library can call GetDescriptorPoolCensus.
struct DescriptorPoolCensus {
bool available = false; // false when no Magma renderer is live (e.g. the DirectGLES backend)
uint32_t frameSlots = 0;
uint32_t pools = 0; // descriptor pools across every frame slot
uint32_t maxPoolsInOneSlot = 0;
uint64_t capacitySets = 0; // sum of maxSets over those pools
uint64_t cachedSets = 0; // sets allocated from them and held in the per-layout caches
uint64_t slotTrims = 0; // whole-slot trims since the renderer came up
};
DescriptorPoolCensus GetDescriptorPoolCensus();
} // namespace MobileGL::MG_Backend::DirectVulkan
@@ -370,16 +370,36 @@ namespace MobileGL::MG_Backend::DirectVulkan {
#endif // !MOBILEGL_BUILD_DISAGGREGATED
#if MOBILEGL_BUILD_DISAGGREGATED
// The verb's handles identify server stores. Readback orders GPU-produced
// command/count bytes before CPU tier expansion; never inspect a client binding.
// The verb's handles identify server stores; never inspect a client binding.
//
// P8-D: THE GPU READS THE WORDS (VulkanRenderer::DrawWireIndirectNative, WireDraw.inc), as
// on the monolith arm. The CPU expansion below is kept for the one shape the monolith arm
// also expands - a COUNT form the device cannot issue natively (no VK_KHR_draw_indirect_count,
// or maxdrawcount > 1 without multiDrawIndirect: VulkanRenderer.cpp's
// MultiDrawElementsIndirectCount) - where ReadWireBuffer orders GPU-produced command/count
// bytes before the expansion.
void DrawWireIndirect(GLenum mode, GLenum type, const void* indirect, GLsizei drawcount,
GLsizei stride, Bool indexed, Bool counted = false, GLintptr countOffset = 0) {
if (drawcount <= 0) return;
if (drawcount <= 0 || countOffset < 0) return;
if (pVulkanRenderer->DrawWireIndirectNative(mode, type, reinterpret_cast<Uint64>(indirect), drawcount,
stride, indexed, counted, static_cast<Uint64>(countOffset)))
return;
auto& buffers = pVulkanRenderer->GetWireBufferManager();
const auto& state = MG_Pipe::MGPipeApplier();
// P8-D: this call reads its words on the CPU; the waits those reads take are its own.
struct ExpansionCount {
VkBufferManager& buffers;
const Uint64 waitsBefore = buffers.GetWireHostWaitCount();
~ExpansionCount() {
if (!MG_Util::PipeStats::Enabled()) return;
MG_Util::PipeStats::AddCalls(MG_Util::PipeStats::CallClass::WireIndirectCpuExpansions, 1);
MG_Util::PipeStats::AddCalls(MG_Util::PipeStats::CallClass::WireHostWaitsIndirect,
buffers.GetWireHostWaitCount() - waitsBefore);
}
} expansionCount{buffers};
if (counted) {
Uint32 count = 0;
if (countOffset < 0 || !buffers.ReadWireBuffer(state.VerbIndirectParameterBuffer,
if (!buffers.ReadWireBuffer(state.VerbIndirectParameterBuffer,
static_cast<Uint64>(countOffset), sizeof(count), &count)) return;
drawcount = static_cast<GLsizei>(std::min<Uint32>(count, static_cast<Uint32>(drawcount)));
if (!drawcount) return;
@@ -917,12 +937,12 @@ namespace MobileGL::MG_Backend::DirectVulkan {
void DispatchComputeIndirect(GLintptr indirect) {
#if MOBILEGL_BUILD_DISAGGREGATED
// P8-SV: THE GPU READS THE GROUP COUNTS on the wire arm too - VulkanRenderer's
// DispatchComputeIndirect takes its transport branch into DispatchWireComputeIndirect
// (WireDraw.inc), which issues vkCmdDispatchIndirect from the verb's store. The CPU read
// that was here (ReadWireBuffer, a whole-GPU wait once a shader had written the store) is gone.
if (MG_Config::Transport != MG_Config::TransportMode::Monolith) {
Uint32 groups[3]{};
if (indirect >= 0 && pVulkanRenderer->GetWireBufferManager().ReadWireBuffer(
MG_Pipe::MGPipeApplier().VerbDispatchIndirectBuffer,
static_cast<Uint64>(indirect), sizeof(groups), groups))
pVulkanRenderer->DispatchCompute(groups[0], groups[1], groups[2]);
pVulkanRenderer->DispatchComputeIndirect(indirect);
return;
}
#endif
@@ -1741,4 +1761,52 @@ namespace MobileGL::MG_Backend::DirectVulkan {
MG_Util::PipeStats::OnPresent();
}
}
namespace {
Optional<Int> g_requestedSwapInterval;
} // namespace
void SetSwapInterval(Int interval) {
g_requestedSwapInterval = interval;
if (pVulkanRenderer) {
pVulkanRenderer->SetSwapInterval(interval);
}
}
Optional<Int> GetRequestedSwapInterval() {
return g_requestedSwapInterval;
}
#if MOBILEGL_BUILD_DISAGGREGATED
void ForgetRequestedSwapInterval() {
g_requestedSwapInterval.reset();
}
#endif
DescriptorPoolCensus GetDescriptorPoolCensus() {
#if MOBILEGL_BUILD_DISAGGREGATED
// P11 M2: UNDER A TRANSPORT THE POOLS ARE THE SERVER RENDERER'S, and its apply thread
// grows, rewinds and trims them while the test thread would be reading - a census taken
// after a swap the client ran ahead of, or mid-burst, walks a set cache BeginFrame may be
// clearing. Inproc, the server is a thread of this process: the read is posted to it as
// the test probe, which runs between drain batches, so it sees every record published
// before it and none half-applied - without waiting on the GPU, which is the point of the
// trim case (it gives a burst back with other slots' work in flight). Spawn and tcp keep
// their pools in another process; the client has no renderer and answers unavailable.
if (MG_Config::Transport != MG_Config::TransportMode::Monolith &&
!MG_Remote::Server::ServerLoop::OnApplyThread()) {
if (MG_Config::Transport != MG_Config::TransportMode::InProcess) return DescriptorPoolCensus{};
DescriptorPoolCensus census{};
const MobileGLResult result = MG_Remote::Server::ServerLoopInstance().RunProbeOnApplyThreadForTesting(
+[](void* user) -> MobileGLResult {
*static_cast<DescriptorPoolCensus*>(user) =
pVulkanRenderer ? pVulkanRenderer->GetDescriptorPoolCensus() : DescriptorPoolCensus{};
return MOBILEGL_OK;
},
&census);
return result == MOBILEGL_OK ? census : DescriptorPoolCensus{};
}
#endif
return pVulkanRenderer ? pVulkanRenderer->GetDescriptorPoolCensus() : DescriptorPoolCensus{};
}
} // namespace MobileGL::MG_Backend::DirectVulkan
@@ -147,4 +147,14 @@ namespace MobileGL::MG_Backend::DirectVulkan {
// only exist as vkCmdWriteTimestamp results); the frontend falls back.
Int64 GetGpuTimestampNs();
void Present();
// eglSwapInterval. Remembered across renderers: one created for a later window surface
// starts from the app's last request (GetRequestedSwapInterval) instead of the default.
void SetSwapInterval(Int interval);
Optional<Int> GetRequestedSwapInterval();
#if MOBILEGL_BUILD_DISAGGREGATED
// The swap interval an ended server session asked for is not the next one's, the same rule as
// DirectGLES::ForgetRequestedSwapInterval: under a transport ~BackendObject_DirectVulkan forgets
// it, so the next session starts at the default until it asks.
void ForgetRequestedSwapInterval();
#endif
} // namespace MobileGL::MG_Backend::DirectVulkan
@@ -8,6 +8,8 @@
#include "SwapchainObject.h"
#include <span>
#include "MG_Impl/GLImpl/Framebuffer/GL_Framebuffer.h"
#include "MG_State/GLState/TextureState/TextureObject2D.h"
@@ -140,12 +142,26 @@ namespace MobileGL::MG_Backend::DirectVulkan {
}
VkPresentModeKHR SwapchainObject::ChooseSwapchainPresentMode(
const Vector<VkPresentModeKHR>& availablePresentModes) {
for (auto desiredPresentMode : s_desiredPresentModes) {
for (const auto& presentMode : availablePresentModes) {
if (presentMode == desiredPresentMode) {
return presentMode;
}
const Vector<VkPresentModeKHR>& availablePresentModes, Optional<Int> swapInterval) {
static constexpr VkPresentModeKHR s_unthrottledPresentModes[]{
VK_PRESENT_MODE_IMMEDIATE_KHR, VK_PRESENT_MODE_MAILBOX_KHR, VK_PRESENT_MODE_FIFO_KHR};
static constexpr VkPresentModeKHR s_vsyncPresentModes[]{VK_PRESENT_MODE_FIFO_KHR};
static constexpr VkPresentModeKHR s_adaptiveVsyncPresentModes[]{VK_PRESENT_MODE_FIFO_RELAXED_KHR,
VK_PRESENT_MODE_FIFO_KHR};
std::span<const VkPresentModeKHR> ranked = s_desiredPresentModes;
if (swapInterval) {
if (*swapInterval == 0) {
ranked = s_unthrottledPresentModes;
} else if (*swapInterval > 0) {
ranked = s_vsyncPresentModes;
} else {
ranked = s_adaptiveVsyncPresentModes;
}
}
for (auto desiredPresentMode : ranked) {
if (std::find(availablePresentModes.begin(), availablePresentModes.end(), desiredPresentMode) !=
availablePresentModes.end()) {
return desiredPresentMode;
}
}
@@ -153,9 +169,13 @@ namespace MobileGL::MG_Backend::DirectVulkan {
return availablePresentModes[0];
}
const char* SwapchainObject::GetPresentModeName(VkPresentModeKHR presentMode) {
return string_VkPresentModeKHR(presentMode);
}
void SwapchainObject::Create(VkDevice device, VkPhysicalDevice physicalDevice, VkSurfaceKHR surface,
Uint32 graphicsQueueFamily, Uint32 presentQueueFamily, Uint32 minImageCountHint,
VkExtent2D desiredExtent) {
VkExtent2D desiredExtent, Optional<Int> swapInterval) {
const auto swapchainCapabilities = GetSwapchainCapabilities(physicalDevice, surface);
MOBILEGL_ASSERT(swapchainCapabilities.IsComplete(),
"SwapchainObject::Create failed: incomplete swapchain capabilities");
@@ -174,8 +194,10 @@ namespace MobileGL::MG_Backend::DirectVulkan {
MGLOG_D(" %s", string_VkPresentModeKHR(pm));
}
const auto presentMode = ChooseSwapchainPresentMode(swapchainCapabilities.presentModes);
const auto presentMode = ChooseSwapchainPresentMode(swapchainCapabilities.presentModes, swapInterval);
MGLOG_I("Picked present mode: %s", string_VkPresentModeKHR(presentMode));
m_presentMode = presentMode;
m_supportedPresentModes = swapchainCapabilities.presentModes;
const auto& swapchainCaps = swapchainCapabilities.capabilities;
Uint32 targetImageCount = std::max<Uint32>(minImageCountHint, swapchainCaps.minImageCount);
@@ -26,13 +26,23 @@ namespace MobileGL::MG_Backend::DirectVulkan {
static SwapchainCapabilities GetSwapchainCapabilities(VkPhysicalDevice physicalDevice, VkSurfaceKHR surface);
static VkSurfaceFormatKHR ChooseSwapchainSurfaceFormat(const Vector<VkSurfaceFormatKHR>& availableFormats);
static VkPresentModeKHR ChooseSwapchainPresentMode(const Vector<VkPresentModeKHR>& availablePresentModes);
// swapInterval is the app's eglSwapInterval, empty until it asks (s_desiredPresentModes
// ranks the modes then). 0 wants IMMEDIATE, else MAILBOX, else FIFO; >= 1 wants FIFO,
// which cannot skip vblanks, so 2 and up pace like 1; a negative interval (adaptive
// vsync) wants FIFO_RELAXED, else FIFO.
static VkPresentModeKHR ChooseSwapchainPresentMode(const Vector<VkPresentModeKHR>& availablePresentModes,
Optional<Int> swapInterval = Nullopt);
static const char* GetPresentModeName(VkPresentModeKHR presentMode);
void Create(VkDevice device, VkPhysicalDevice physicalDevice, VkSurfaceKHR surface, Uint32 graphicsQueueFamily,
Uint32 presentQueueFamily, Uint32 minImageCountHint, VkExtent2D desiredExtent);
Uint32 presentQueueFamily, Uint32 minImageCountHint, VkExtent2D desiredExtent,
Optional<Int> swapInterval);
void Shutdown(VkDevice device);
VkSwapchainKHR GetHandle() const { return m_swapchain; }
VkPresentModeKHR GetPresentMode() const { return m_presentMode; }
// The surface's present modes as queried when this swapchain was created.
const Vector<VkPresentModeKHR>& GetSupportedPresentModes() const { return m_supportedPresentModes; }
const VkSurfaceFormatKHR& GetSurfaceFormat() const { return m_surfaceFormat; }
VkExtent2D GetExtent() const { return m_extent; }
// Surface-space extent (before the pre-rotation quarter-turn swap) this swapchain was
@@ -79,6 +89,8 @@ namespace MobileGL::MG_Backend::DirectVulkan {
};
VkSwapchainKHR m_swapchain = VK_NULL_HANDLE;
VkPresentModeKHR m_presentMode = VK_PRESENT_MODE_FIFO_KHR;
Vector<VkPresentModeKHR> m_supportedPresentModes;
VkSurfaceFormatKHR m_surfaceFormat{};
VkExtent2D m_extent{};
VkExtent2D m_surfaceExtent{};
@@ -591,6 +591,11 @@ namespace MobileGL::MG_Backend::DirectVulkan {
m_samplerResolveMemoHighWater = 0;
m_setsPerFrame = setsPerFrame;
m_peakDescriptorSetsObserved = 0;
m_trimQuietEpochs = MG_Config::Features.MagmaDescriptorTrimFrames;
// The renderer records its first frame into slot 0 before any BeginFrame runs.
m_epochSlot = 0;
m_epochOpen = true;
m_descriptorSlotTrims = 0;
m_textureManager = textureManager;
m_samplerManager = samplerManager;
@@ -600,6 +605,8 @@ namespace MobileGL::MG_Backend::DirectVulkan {
frame.activeDescriptorPoolIndex = 0;
frame.allocatedSetsThisFrame = 0;
frame.peakAllocatedSetsThisFrame = 0;
frame.capacitySets = m_setsPerFrame;
frame.quietEpochs = 0;
frame.descriptorPools.clear();
VkDescriptorPool initialPool = VK_NULL_HANDLE;
@@ -654,6 +661,8 @@ namespace MobileGL::MG_Backend::DirectVulkan {
frame.activeDescriptorPoolIndex = 0;
frame.allocatedSetsThisFrame = 0;
frame.peakAllocatedSetsThisFrame = 0;
frame.capacitySets = 0;
frame.quietEpochs = 0;
}
m_frames.clear();
#if MOBILEGL_BUILD_DISAGGREGATED
@@ -662,6 +671,8 @@ namespace MobileGL::MG_Backend::DirectVulkan {
m_wirePrivateStoragePlaceholders = 0;
m_wireInvalidStorageImagesBindNull = false;
#endif
m_epochSlot = 0;
m_epochOpen = false;
m_bufferManager = nullptr;
m_programFactory = nullptr;
@@ -701,6 +712,20 @@ namespace MobileGL::MG_Backend::DirectVulkan {
"UniformDescriptorBinder: new descriptor set peak observed=%u (base setsPerFrame=%u, frame=%u, pools=%zu)",
m_peakDescriptorSetsObserved, m_setsPerFrame, frameIndex, frame.descriptorPools.size());
}
// The epoch that just ended is the one of the slot begun last - this slot again on the
// drain path, the previous frame's slot on the Present path. Its peak is still unreset.
if (m_epochOpen && m_epochSlot < m_frames.size()) {
NoteEndedEpochForTrim(m_frames[m_epochSlot].peakAllocatedSetsThisFrame);
}
m_epochSlot = frameIndex;
m_epochOpen = true;
// No pending command buffer references this slot's sets here (see TrimFrameDescriptorPools
// for why that holds at both of BeginFrame's call sites), so a quiet grown slot can be
// handed back whole.
if (m_trimQuietEpochs != 0 && frame.descriptorPools.size() > 1 &&
frame.capacitySets >= kDescriptorTrimMinCapacity && frame.quietEpochs >= m_trimQuietEpochs) {
TrimFrameDescriptorPools(frame, frameIndex);
}
frame.activeDescriptorPoolIndex = 0;
frame.allocatedSetsThisFrame = 0;
frame.peakAllocatedSetsThisFrame = 0;
@@ -709,11 +734,7 @@ namespace MobileGL::MG_Backend::DirectVulkan {
}
// The frame's descriptor sets are recycled above, so last frame's reuse targets
// are gone: start the per-draw descriptor-reuse cache fresh this frame.
for (auto& entry : m_descriptorReuseMemo) {
entry.valid = false;
}
m_fastRebindMemo.valid = false;
m_lastBindValid = false;
InvalidateDescriptorSetMemos();
// Re-fingerprint the bound sampler set fresh this frame so any GL object address
// reuse cannot outlive a single frame (see SamplerResolveMemo). Only the entries a
// resolve has actually written can be valid, so the high-water mark bounds the
@@ -747,10 +768,11 @@ namespace MobileGL::MG_Backend::DirectVulkan {
entry.second.cursor = 0;
}
frame.allocatedSetsThisFrame = 0;
frame.peakAllocatedSetsThisFrame = 0;
for (auto& entry : m_descriptorReuseMemo) entry.valid = false;
m_fastRebindMemo.valid = false;
m_lastBindValid = false;
// The PEAK is not reset: a rewind ends a window of the epoch, not the epoch. The trim scores
// an epoch by the most sets the slot held at once in it (NoteEndedEpochForTrim at the next
// BeginFrame), and on this arm that is the largest window - resetting here would score a
// burst frame by its last window alone and trim a slot that re-grows every frame.
InvalidateDescriptorSetMemos();
const Uint32 touchedBindings =
std::min<Uint32>(m_samplerResolveMemoHighWater, static_cast<Uint32>(m_samplerResolveMemo.size()));
for (Uint32 binding = 0; binding < touchedBindings; ++binding) {
@@ -761,6 +783,68 @@ namespace MobileGL::MG_Backend::DirectVulkan {
}
#endif
void UniformManager::InvalidateDescriptorSetMemos() {
// Every VkDescriptorSet handle held outside the per-layout caches - in this backend they all
// live in this class. A trim can hand a destroyed set's handle value to a fresh allocation,
// so the bind-dedup shadow must drop too, not only the two reuse memos.
for (auto& entry : m_descriptorReuseMemo) {
entry.valid = false;
}
m_fastRebindMemo.valid = false;
m_lastBindValid = false;
}
void UniformManager::NoteEndedEpochForTrim(Uint32 epochPeakSets) {
for (auto& slot : m_frames) {
if (static_cast<Uint64>(epochPeakSets) * kDescriptorTrimHeadroom > slot.capacitySets) {
slot.quietEpochs = 0;
} else if (slot.quietEpochs < std::numeric_limits<Uint32>::max()) {
++slot.quietEpochs;
}
}
}
void UniformManager::TrimFrameDescriptorPools(FrameResources& frame, Uint32 frameIndex) {
const SizeT poolsBefore = frame.descriptorPools.size();
const Uint64 capacityBefore = frame.capacitySets;
// Holders first: the per-layout cache and every memo that may name one of these sets.
frame.descriptorSetCacheByLayout.clear();
InvalidateDescriptorSetMemos();
for (SizeT i = 1; i < frame.descriptorPools.size(); ++i) {
if (frame.descriptorPools[i].handle != VK_NULL_HANDLE) {
vkDestroyDescriptorPool(m_device, frame.descriptorPools[i].handle, nullptr);
}
}
frame.descriptorPools.resize(1);
auto& base = frame.descriptorPools[0];
// Frees every set allocated from it; the spec defines no failure for vkResetDescriptorPool.
vkResetDescriptorPool(m_device, base.handle, 0);
base.allocatedSets = 0;
frame.activeDescriptorPoolIndex = 0;
frame.capacitySets = base.maxSets;
frame.quietEpochs = 0;
++m_descriptorSlotTrims;
MGLOG_D("UniformDescriptorBinder: frame %u trimmed its descriptor pools (%zu pools / %llu sets -> 1 / %u)",
frameIndex, poolsBefore, static_cast<unsigned long long>(capacityBefore), base.maxSets);
}
DescriptorPoolCensus UniformManager::GetDescriptorPoolCensus() const {
DescriptorPoolCensus census;
census.available = true;
census.frameSlots = static_cast<uint32_t>(m_frames.size());
for (const auto& frame : m_frames) {
const auto poolCount = static_cast<uint32_t>(frame.descriptorPools.size());
census.pools += poolCount;
census.maxPoolsInOneSlot = std::max(census.maxPoolsInOneSlot, poolCount);
census.capacitySets += frame.capacitySets;
for (const auto& cacheEntryPair : frame.descriptorSetCacheByLayout) {
census.cachedSets += cacheEntryPair.second.sets.size();
}
}
census.slotTrims = m_descriptorSlotTrims;
return census;
}
void UniformManager::OnDescriptorSetLayoutDestroyed(VkDescriptorSetLayout descriptorSetLayout) {
SizeT purgedSets = 0;
for (auto& frame : m_frames) {
@@ -2700,9 +2784,20 @@ namespace MobileGL::MG_Backend::DirectVulkan {
auto& bindingPoint = MGB_CTX->GetBufferBindingPoint(BufferTarget::Uniform, frontendBinding);
const auto& bufferObject = bindingPoint.GetBoundObject();
MOBILEGL_ASSERT(bufferObject != nullptr,
"ResolveUniformBufferPayload: no UBO bound at frontend binding %u for block '%s'",
frontendBinding, program.GetUniformBlockName(static_cast<Uint32>(blockIndex)).c_str());
static thread_local Vector<Uint8> paddedUbo;
if (bufferObject == nullptr) {
// A block the program declares at a point with nothing bound - never bound, or its
// buffer was deleted while bound, which unbinds it (UnboundUniformBlockScenario,
// DeletedBoundBufferScenario). GL leaves what the shader READS undefined and raises no
// error; this used to dereference the null object. Answer it with a zeroed block of the
// reflected size, as the wire arm does (ResolveWireUniformBufferPayload).
const VkDeviceSize blockSize =
static_cast<VkDeviceSize>(program.GetUBOSizeAt(static_cast<Uint32>(blockIndex)));
paddedUbo.assign(static_cast<SizeT>(blockSize > 0 ? blockSize : 16), 0);
out.payload = paddedUbo.data();
out.payloadSize = static_cast<VkDeviceSize>(paddedUbo.size());
return true;
}
bufferObject->SyncPersistentMappedRange();
MOBILEGL_ASSERT(bufferObject->MappedData() != nullptr && bufferObject->GetSize() != 0,
@@ -2735,7 +2830,6 @@ namespace MobileGL::MG_Backend::DirectVulkan {
outSize = blockSize;
outData = bufferObject->MappedData() + static_cast<SizeT>(rangeStart);
if (available < blockSize) {
static thread_local Vector<Uint8> paddedUbo;
paddedUbo.assign(static_cast<SizeT>(blockSize), 0);
Memcpy(paddedUbo.data(), outData, static_cast<SizeT>(available));
outData = paddedUbo.data();
@@ -2831,32 +2925,49 @@ namespace MobileGL::MG_Backend::DirectVulkan {
return true;
}
Uint32 UniformManager::NextDescriptorPoolMaxSets(const Vector<DescriptorPoolBucket>& pools, Bool updateAfterBind,
Uint32 initialMaxSets) {
const Uint32 minSets = std::max<Uint32>(1, initialMaxSets);
const Uint32 maxSets = std::max(minSets, kMaxDescriptorPoolSets);
Uint32 largest = 0;
for (const auto& pool : pools) {
if (pool.updateAfterBind == updateAfterBind) {
largest = std::max(largest, pool.maxSets);
}
}
if (largest == 0) {
return minSets;
}
const Uint64 doubled = static_cast<Uint64>(largest) * 2;
return static_cast<Uint32>(std::clamp<Uint64>(doubled, minSets, maxSets));
}
Bool UniformManager::GrowFrameDescriptorPool(FrameResources& frame, Uint32 frameIndex, Bool updateAfterBind) {
if (frame.descriptorPools.empty()) {
return false;
}
const auto matchingBucket = std::find_if(
frame.descriptorPools.begin(), frame.descriptorPools.end(),
[updateAfterBind](const DescriptorPoolBucket& candidate) { return candidate.updateAfterBind == updateAfterBind; });
const Uint32 currentMaxSets = matchingBucket != frame.descriptorPools.end()
? std::max<Uint32>(1, matchingBucket->maxSets)
: m_setsPerFrame;
const Uint32 grownMaxSets = currentMaxSets <= (std::numeric_limits<Uint32>::max() / 2) ? (currentMaxSets * 2)
: currentMaxSets;
Uint32 grownMaxSets = NextDescriptorPoolMaxSets(frame.descriptorPools, updateAfterBind, m_setsPerFrame);
VkDescriptorPool grownPool = VK_NULL_HANDLE;
if (!CreateDescriptorPool(grownMaxSets, updateAfterBind, grownPool)) {
MGLOG_E_ONCE("UniformDescriptorBinder::GrowFrameDescriptorPool failed: cannot create grown pool (%u -> %u sets)",
currentMaxSets, grownMaxSets);
return false;
// A large pool is a large driver allocation; if the device refuses one, settle for less
// rather than failing the draw - halving down to the base size, where the old policy lived.
while (!CreateDescriptorPool(grownMaxSets, updateAfterBind, grownPool)) {
if (grownMaxSets <= m_setsPerFrame) {
MGLOG_E_ONCE("UniformDescriptorBinder::GrowFrameDescriptorPool failed: cannot create grown pool (%u sets)",
grownMaxSets);
return false;
}
grownMaxSets = std::max(m_setsPerFrame, grownMaxSets / 2);
}
frame.descriptorPools.push_back({grownPool, grownMaxSets, 0, updateAfterBind});
frame.activeDescriptorPoolIndex = static_cast<Uint32>(frame.descriptorPools.size() - 1);
MGLOG_D(
"UniformDescriptorBinder: frame %u descriptor pool exhausted, grew pool (%u -> %u sets), poolCount=%zu",
frameIndex, currentMaxSets, grownMaxSets, frame.descriptorPools.size());
frame.capacitySets += grownMaxSets;
// Growing is the opposite of quiet: a trim must wait a full quiet run from here.
frame.quietEpochs = 0;
MGLOG_D("UniformDescriptorBinder: frame %u descriptor pool exhausted, grew a %u-set pool (updateAfterBind=%d), "
"poolCount=%zu",
frameIndex, grownMaxSets, updateAfterBind ? 1 : 0, frame.descriptorPools.size());
return true;
}
@@ -14,6 +14,7 @@
#include "VkSamplerManager.h"
#include "VkTextureManager.h"
#include "WirePlaceholderKey.h"
#include "../DescriptorPoolCensus.h"
#include "../VkIncludes.h"
#include <Includes.h>
@@ -84,9 +85,11 @@ namespace MobileGL::MG_Backend::DirectVulkan {
// FREE_DESCRIPTOR_SET_BIT) and the pool accounting is credited, so program
// churn recycles pool capacity instead of abandoning it. GPU-safe: the layout
// only dies after >1024 idle frame boundaries, so no in-flight command buffer
// references its sets. This is the only eviction path for the per-layout
// caches - a live layout's entry must never be purged (its sets would be
// unreachable pool slots), so there is deliberately no age-based sweep here.
// references its sets. This is the only PER-LAYOUT eviction path for the
// caches - a live layout's entry must never be purged on its own (its sets would be
// unreachable pool slots), so there is deliberately no age-based sweep here. The
// other path, TrimFrameDescriptorPools, drops a slot's whole cache together with
// the pools behind it.
void OnDescriptorSetLayoutDestroyed(VkDescriptorSetLayout descriptorSetLayout);
// One record per visited CombinedImageSampler DESCRIPTOR (post fallback substitution,
// in binding order, and within a binding in array-element order): the resolved texture
@@ -158,13 +161,46 @@ namespace MobileGL::MG_Backend::DirectVulkan {
static Bool ProgramSamplesOnlySingleLevelTextures(const MagmaProgramSource& program,
const ProgramFactory::VkProgramObject& programObj);
private:
// ---- per-frame descriptor pools ----------------------------------------------------
// Each frame slot owns a list of pools. The first is the base pool (non-update-after-bind,
// setsPerFrame sets) and is never destroyed while the manager lives; every later one was
// added by GrowFrameDescriptorPool when an allocation found no room. A pool serves one
// updateAfterBind flavour, because a set can only come from a pool created with the flag
// its layout was.
struct DescriptorPoolBucket {
VkDescriptorPool handle = VK_NULL_HANDLE;
Uint32 maxSets = 0;
Uint32 allocatedSets = 0;
Bool updateAfterBind = false;
};
// One pool's maxSets never exceeds this. It bounds one pool's driver memory: CreateDescriptorPool
// declares 8 descriptors of each of 6 types per set, so a 4096-set pool asks for 196,608
// descriptors - about 12 MiB at 64 bytes each on a driver that backs every declared one.
static constexpr Uint32 kMaxDescriptorPoolSets = 4096;
// The growth policy, pure so the host tests can drive it: the size of the next pool a slot
// adds for `updateAfterBind` layouts, given the slot's existing pools. Twice the LARGEST pool
// of that flavour, capped at kMaxDescriptorPoolSets; `initialMaxSets` when the slot has no
// pool of that flavour yet. So a frame that needs N sets adds O(log N) pools while N stays
// under the cap, and one more pool per kMaxDescriptorPoolSets sets beyond it. Taking the
// largest (not the first) pool is the point: the first same-flavour pool is the oldest,
// smallest one, and doubling IT made every grown pool the same size.
static Uint32 NextDescriptorPoolMaxSets(const Vector<DescriptorPoolBucket>& pools, Bool updateAfterBind,
Uint32 initialMaxSets);
// Trim policy (see TrimFrameDescriptorPools). A slot is "quiet" in an epoch whose peak set
// count times kDescriptorTrimHeadroom still fits its capacity; a slot that has been quiet for
// MOBILEGL_MAGMA_DESCRIPTOR_TRIM_FRAMES consecutive epochs, and holds at least
// kDescriptorTrimMinCapacity sets, is trimmed. The headroom is what keeps the trim from
// thrashing: geometric growth leaves a re-grown slot at least a third full (the pool a growth
// adds is at most twice the largest before it, so at most twice everything the slot held, and
// the epoch that grew it needed more than that), so the epoch that grew a slot is never quiet
// for it. The floor keeps a slot sized for ordinary
// frames from being trimmed at all - the trim exists to give back a burst.
static constexpr Uint32 kDescriptorTrimHeadroom = 4;
static constexpr Uint32 kDescriptorTrimMinCapacity = 1024;
// For tests (DescriptorPoolCensus.h): pools, capacity, cached sets and trims over every slot.
DescriptorPoolCensus GetDescriptorPoolCensus() const;
private:
// A cached descriptor set together with the pool it was allocated from, so a
// layout-destroyed purge can vkFreeDescriptorSets it back and credit the
@@ -239,6 +275,10 @@ namespace MobileGL::MG_Backend::DirectVulkan {
void DestroyWirePlaceholderImage(WirePlaceholderImage& image) const;
#endif
// descriptorSetCacheByLayout is the per-frame set cache: BeginFrame rewinds every cursor, so
// a slot re-writes the sets it allocated in earlier frames instead of allocating new ones, and
// the cache only ever grows - except through TrimFrameDescriptorPools, which gives a slot's
// whole burst back at once (and OnDescriptorSetLayoutDestroyed, one dead layout at a time).
struct FrameResources {
Vector<DescriptorPoolBucket> descriptorPools;
UnorderedMap<VkDescriptorSetLayout, DescriptorSetCacheEntry> descriptorSetCacheByLayout;
@@ -252,6 +292,10 @@ namespace MobileGL::MG_Backend::DirectVulkan {
Uint32 activeDescriptorPoolIndex = 0;
Uint32 allocatedSetsThisFrame = 0;
Uint32 peakAllocatedSetsThisFrame = 0;
// Sum of maxSets over descriptorPools, kept in step by Initialize, Grow and Trim.
Uint64 capacitySets = 0;
// Consecutive epochs (see NoteEndedEpochForTrim) that were quiet for THIS slot's capacity.
Uint32 quietEpochs = 0;
};
static Bool ResolveSamplerTexture(const MagmaProgramSource& program,
@@ -395,6 +439,28 @@ namespace MobileGL::MG_Backend::DirectVulkan {
const Vector<Uint32>& dynamicOffsets);
Bool CreateDescriptorPool(Uint32 maxSets, Bool updateAfterBind, VkDescriptorPool& outPool) const;
Bool GrowFrameDescriptorPool(FrameResources& frame, Uint32 frameIndex, Bool updateAfterBind);
// An epoch is the stretch between two BeginFrame calls: one frame, or the part of one between
// two drains (VulkanRenderer::TryDrainFrameTransients). Called by BeginFrame with the epoch
// that just ended, it updates every slot's quiet count - a burst in any slot counts against
// all of them, so bursts that land in the other slots still hold a slot's trim off.
void NoteEndedEpochForTrim(Uint32 epochPeakSets);
// Gives a slot's whole descriptor burst back: clears its per-layout cache, destroys every
// grown pool and resets the base pool, so the slot re-grows geometrically from setsPerFrame.
// Whole-slot rather than per-set: vkFreeDescriptorSets fragments a pool and can never empty
// a grown one. Called only from BeginFrame, which runs only when no pending command buffer
// references the slot's sets:
// - Present path (VulkanRenderer::Present, after WaitAndAcquireNextImage): the slot's
// imageInFlightFence has been waited. That fence rides the slot's LAST submission - the
// Present submit - and every mid-frame submission of the slot (FlushPendingCommands,
// SplitOversizedRecording, readbacks) went to the same queue earlier, so the fence's
// signal covers them (vkQueueSubmit's fence scope includes all earlier submissions).
// - Drain path (VulkanRenderer::TryDrainFrameTransients): every submission has been
// observed complete and no recording is open.
// Sets are per slot: a slot's sets are only ever bound into recordings made while it is the
// current slot, so trimming one slot never touches another slot's in-flight work.
void TrimFrameDescriptorPools(FrameResources& frame, Uint32 frameIndex);
// Drops every handle this class holds to a descriptor set outside the per-layout caches.
void InvalidateDescriptorSetMemos();
VkResult AllocateDescriptorSetsFromActivePool(
Uint32 frameIndex, const ProgramFactory::VkProgramObject& programObj, VkDescriptorSet& outDescriptorSet);
VkResult AcquireDescriptorSet(Uint32 frameIndex,
@@ -412,6 +478,13 @@ namespace MobileGL::MG_Backend::DirectVulkan {
Uint32 m_maxBindings = 0;
Uint32 m_setsPerFrame = 0;
Uint32 m_peakDescriptorSetsObserved = 0;
// MOBILEGL_MAGMA_DESCRIPTOR_TRIM_FRAMES, latched at Initialize (0 = never trim).
Uint32 m_trimQuietEpochs = 0;
// The slot whose epoch is open (the last one BeginFrame was called for); valid once
// m_epochOpen is set. Its peak is the ended epoch's peak at the next BeginFrame.
Uint32 m_epochSlot = 0;
Bool m_epochOpen = false;
Uint64 m_descriptorSlotTrims = 0;
VkTextureManager* m_textureManager = nullptr;
VkSamplerManager* m_samplerManager = nullptr;
mutable SharedPtr<MG_State::GLState::ITextureObject> m_fallbackTexture2D;
@@ -15,7 +15,14 @@
#include "MG_Pipe/MGPipeCallbacks.h"
#include "MG_Pipe/PipeApply.h"
#include "MG_Remote/Server/StagedShadow.h"
#if MOBILEGL_BUILD_DISAGGREGATED
#include "MG_Remote/Transport/AdoptT0.h"
#endif
#if MOBILEGL_BUILD_DISAGGREGATED && defined(__ANDROID__)
#include <android/hardware_buffer.h>
#endif
#include <Config.h>
#include <chrono>
#include <cstdlib>
#endif
@@ -206,6 +213,16 @@ namespace MobileGL::MG_Backend::DirectVulkan {
// modified ranges. A server pointer is never donated across the wire.
.MapPersistent = [](MG_Pipe::MGPipeHandle, Uint64, const void*) -> void* { return nullptr; },
.UnmapPersistent = [](MG_Pipe::MGPipeHandle) {},
#if defined(__ANDROID__)
// P11 B2 (T0): the one door a server pointer is still never donated through stays shut;
// what T0 adds is the other direction - the CLIENT's pages imported as the store.
.ImportExternal = [](MG_Pipe::MGPipeHandle res, void* ahb, Uint64 size) -> Bool {
return WireManager().ImportWireBuffer(res, ahb, size);
},
.SelfTestExternal = [](char* why, Uint64 whyBytes) -> Bool {
return VkBufferManager::SelfTestWireImport(why, whyBytes);
},
#endif
};
#endif
} // namespace
@@ -245,11 +262,17 @@ namespace MobileGL::MG_Backend::DirectVulkan {
// to wait for a submission (DeferredWireRelease), and three lines from now the record
// no longer carries it.
const Uint64 orphanedUseSerial = resource->lastUseSerial;
DeferWireRelease(std::move(resource->buffer), orphanedUseSerial);
// P11 B2: a T0 store is ALWAYS released through the serial-gated list (never "at once"),
// so the client's AHardwareBuffer outlives every submission that can still name it however
// the client's own release (at this respecify) raced the frames in flight.
DeferWireRelease(std::move(resource->buffer),
resource->imported ? T0ReleaseSerial(orphanedUseSerial) : orphanedUseSerial);
resource->imported = false;
resource->size = desc.Width;
resource->lastUseSerial = 0;
resource->lastUseSubmitIndex = 0; // M2 r2: the new store carries no submission yet (B3's stamp is per store)
resource->gpuWritesPending = false;
resource->indirectReadBarrierPending = false;
resource->stagedCoverage.clear();
++m_sliceEpochCounter;
if (resource->size == 0) return;
@@ -294,7 +317,19 @@ namespace MobileGL::MG_Backend::DirectVulkan {
barrier.dstAccessMask = VK_ACCESS_HOST_READ_BIT | VK_ACCESS_HOST_WRITE_BIT;
vkCmdPipelineBarrier(commands, VK_PIPELINE_STAGE_ALL_COMMANDS_BIT, VK_PIPELINE_STAGE_HOST_BIT,
0, 1, &barrier, 0, nullptr, 0, nullptr);
if (!pVulkanRenderer->WaitForSubmitIndex(pVulkanRenderer->GetSyncPointSubmitIndex(), UINT64_MAX, true)) {
// P8-D: counted here, past the idle early-out, so `whw` is waits TAKEN, not calls.
++m_wireHostWaits;
const Bool timed = MG_Util::PipeStats::Enabled();
const auto waitStart = timed ? std::chrono::steady_clock::now() : std::chrono::steady_clock::time_point{};
const Bool waited =
pVulkanRenderer->WaitForSubmitIndex(pVulkanRenderer->GetSyncPointSubmitIndex(), UINT64_MAX, true);
if (timed) {
MG_Util::PipeStats::AddCalls(MG_Util::PipeStats::CallClass::WireHostWaits, 1);
MG_Util::PipeStats::AddCalls(MG_Util::PipeStats::CallClass::WireHostWaitMicros,
static_cast<Uint64>(std::chrono::duration_cast<std::chrono::microseconds>(
std::chrono::steady_clock::now() - waitStart).count()));
}
if (!waited) {
return false;
}
resource.lastUseSerial = 0;
@@ -537,11 +572,23 @@ namespace MobileGL::MG_Backend::DirectVulkan {
}
void VkBufferManager::ReadbackWireBuffer(MG_Pipe::MGPipeHandle res, Uint64 offset, Uint64 size) {
Vector<Uint8> bytes(static_cast<SizeT>(size));
if (!ReadWireBuffer(res, offset, size, bytes.data())) {
// P11 B2: A T0 STORE'S READBACK IS "DONE, NO BYTES". Its pages are the client's
// AHardwareBuffer, so what the client needs before it reads them is this server's GPU
// having finished every write to them - the resident copies WriteWireBuffer recorded and
// any shader write - and nothing crossing SEG_EVENT. The wait is the store's own host-access
// wait (a barrier to host, then the sync point's submission); the reply then says "done".
// One death for both shapes: the store could not be made readable.
auto* resource = FindWireBuffer(res);
const Bool t0 = resource != nullptr && resource->imported;
Vector<Uint8> bytes(t0 ? 0 : static_cast<SizeT>(size));
if (t0 ? !WaitForWireBufferHostAccess(*resource) : !ReadWireBuffer(res, offset, size, bytes.data())) {
MGLOG_F("Magma: Fatal{ResourceUnavailable, \"buffer-readback\"} {slot=%u, gen=%u}", res.Slot, res.Gen);
std::abort();
}
if (t0) {
++m_sliceEpochCounter;
return;
}
if (MG_Pipe::gMGPipeCallbacks.OnBufferWriteback == nullptr) {
MGLOG_F("Magma: Fatal{ResourceUnavailable, \"buffer-writeback-callback\"}");
std::abort();
@@ -561,6 +608,7 @@ namespace MobileGL::MG_Backend::DirectVulkan {
resource->lastUseSerial = m_frameSerial;
resource->stagedCoverage.clear();
resource->gpuWritesPending = true;
resource->indirectReadBarrierPending = true;
++m_sliceEpochCounter;
// The existing client dirty-state protocol accepts one whole-store
// notification. The store remains on the GPU and exact later SubData
@@ -573,10 +621,363 @@ namespace MobileGL::MG_Backend::DirectVulkan {
MG_Pipe::gMGPipeCallbacks.OnGpuWritten(res, 1, &range);
}
Bool VkBufferManager::TakeWireIndirectReadBarrier(MG_Pipe::MGPipeHandle res) {
auto* resource = FindWireBuffer(res);
if (!resource || !resource->indirectReadBarrierPending) return false;
resource->indirectReadBarrierPending = false;
return true;
}
// ---- P11 B2: T0 - THE CLIENT'S AHardwareBuffer AS A WIRE STORE -----------------------------
// The serial a T0 store is released under: its last use, or - when no GPU command has named it
// since a host-access wait - the current frame's, so the release still waits for the sync
// point rather than taking DeferWireRelease's "never named, destroy now" arm. Conservative by
// at most one submission.
Uint64 VkBufferManager::T0ReleaseSerial(Uint64 lastUseSerial) const {
if (lastUseSerial != 0) return lastUseSerial;
return m_frameSerial != 0 ? m_frameSerial : 1;
}
#if defined(__ANDROID__)
namespace {
Int32 T0PickMemoryType(const VkPhysicalDeviceMemoryProperties& props, Uint32 bits,
VkMemoryPropertyFlags want) {
for (Uint32 i = 0; i < props.memoryTypeCount; ++i) {
if ((bits & (1u << i)) != 0 && (props.memoryTypes[i].propertyFlags & want) == want)
return static_cast<Int32>(i);
}
return -1;
}
struct T0ImportedBuffer {
VkBuffer buffer = VK_NULL_HANDLE;
VkDeviceMemory memory = VK_NULL_HANDLE;
void* mapped = nullptr;
Bool coherent = true;
};
void T0DestroyImported(VkDevice device, T0ImportedBuffer& imported) {
if (imported.mapped != nullptr) vkUnmapMemory(device, imported.memory);
if (imported.buffer != VK_NULL_HANDLE) vkDestroyBuffer(device, imported.buffer, nullptr);
if (imported.memory != VK_NULL_HANDLE) vkFreeMemory(device, imported.memory, nullptr);
imported = T0ImportedBuffer{};
}
// A VkBuffer on a dedicated allocation IMPORTED from `ahb`, in a host-visible memory type
// (coherent preferred), mapped whole. The server's own host map is what WriteWireBuffer's
// idle path and every CPU reader of a wire store use, so a type without HOST_VISIBLE is a
// refusal, not a degraded import. Nothing is left behind on failure.
Bool T0ImportAhb(const VulkanRenderer::WireAhbImport& ctx, void* ahb, Uint64 size, VkBufferUsageFlags usage,
T0ImportedBuffer& out, String& why) {
out = T0ImportedBuffer{};
const auto getProperties =
reinterpret_cast<PFN_vkGetAndroidHardwareBufferPropertiesANDROID>(ctx.getAhbProperties);
VkAndroidHardwareBufferPropertiesANDROID properties{
VK_STRUCTURE_TYPE_ANDROID_HARDWARE_BUFFER_PROPERTIES_ANDROID};
VkResult result = getProperties(ctx.device, static_cast<const AHardwareBuffer*>(ahb), &properties);
if (result != VK_SUCCESS) {
why = "vkGetAndroidHardwareBufferPropertiesANDROID -> " + std::to_string(static_cast<int>(result));
return false;
}
VkExternalMemoryBufferCreateInfo external{VK_STRUCTURE_TYPE_EXTERNAL_MEMORY_BUFFER_CREATE_INFO};
external.handleTypes = VK_EXTERNAL_MEMORY_HANDLE_TYPE_ANDROID_HARDWARE_BUFFER_BIT_ANDROID;
VkBufferCreateInfo bufferInfo{VK_STRUCTURE_TYPE_BUFFER_CREATE_INFO};
bufferInfo.pNext = &external;
bufferInfo.size = size;
bufferInfo.usage = usage;
bufferInfo.sharingMode = VK_SHARING_MODE_EXCLUSIVE;
result = vkCreateBuffer(ctx.device, &bufferInfo, nullptr, &out.buffer);
if (result != VK_SUCCESS) {
out.buffer = VK_NULL_HANDLE;
why = "vkCreateBuffer(external AHB) -> " + std::to_string(static_cast<int>(result));
return false;
}
Int32 type = T0PickMemoryType(ctx.memory, properties.memoryTypeBits,
VK_MEMORY_PROPERTY_HOST_VISIBLE_BIT | VK_MEMORY_PROPERTY_HOST_COHERENT_BIT);
out.coherent = type >= 0;
if (type < 0)
type = T0PickMemoryType(ctx.memory, properties.memoryTypeBits, VK_MEMORY_PROPERTY_HOST_VISIBLE_BIT);
if (type < 0) {
why = "no host-visible memory type can import it (memoryTypeBits 0x" +
std::to_string(properties.memoryTypeBits) + ")";
T0DestroyImported(ctx.device, out);
return false;
}
VkMemoryDedicatedAllocateInfo dedicated{VK_STRUCTURE_TYPE_MEMORY_DEDICATED_ALLOCATE_INFO};
dedicated.buffer = out.buffer;
VkImportAndroidHardwareBufferInfoANDROID import{VK_STRUCTURE_TYPE_IMPORT_ANDROID_HARDWARE_BUFFER_INFO_ANDROID};
import.buffer = static_cast<AHardwareBuffer*>(ahb);
import.pNext = &dedicated;
VkMemoryAllocateInfo allocateInfo{VK_STRUCTURE_TYPE_MEMORY_ALLOCATE_INFO};
allocateInfo.pNext = &import;
allocateInfo.allocationSize = properties.allocationSize;
allocateInfo.memoryTypeIndex = static_cast<Uint32>(type);
result = vkAllocateMemory(ctx.device, &allocateInfo, nullptr, &out.memory);
if (result != VK_SUCCESS) {
out.memory = VK_NULL_HANDLE;
why = "vkAllocateMemory(import AHB) -> " + std::to_string(static_cast<int>(result));
T0DestroyImported(ctx.device, out);
return false;
}
result = vkBindBufferMemory(ctx.device, out.buffer, out.memory, 0);
if (result == VK_SUCCESS) result = vkMapMemory(ctx.device, out.memory, 0, VK_WHOLE_SIZE, 0, &out.mapped);
if (result != VK_SUCCESS || out.mapped == nullptr) {
out.mapped = nullptr;
why = "vkBindBufferMemory/vkMapMemory -> " + std::to_string(static_cast<int>(result));
T0DestroyImported(ctx.device, out);
return false;
}
return true;
}
} // namespace
#endif
Bool VkBufferManager::ImportWireBuffer(MG_Pipe::MGPipeHandle res, void* ahb, Uint64 size) {
#if defined(__ANDROID__)
auto* resource = FindWireBuffer(res);
if (resource == nullptr || ahb == nullptr || size == 0 || size != resource->size) return false;
VulkanRenderer::WireAhbImport ctx;
if (pVulkanRenderer == nullptr || !pVulkanRenderer->GetWireAhbImport(ctx)) {
MGLOG_W_ONCE("Magma T0: this device did not take VK_ANDROID_external_memory_android_hardware_buffer");
return false;
}
VkBufferUsageFlags usage = kPersistentBackedUsage | VK_BUFFER_USAGE_TRANSFER_DST_BIT;
if (m_initInfo.transformFeedbackUsageEnabled) usage |= VK_BUFFER_USAGE_TRANSFORM_FEEDBACK_BUFFER_BIT_EXT;
T0ImportedBuffer imported;
String why;
if (!T0ImportAhb(ctx, ahb, size, usage, imported, why)) {
MGLOG_W("Magma T0: store {slot=%u, gen=%u} (%llu bytes) not imported - %s", res.Slot, res.Gen,
static_cast<unsigned long long>(size), why.c_str());
return false;
}
// The T2 store it replaces goes the way a respecify's does (and any draw still naming it
// keeps it until its submission completes).
DeferWireRelease(std::move(resource->buffer), resource->lastUseSerial);
MG_Remote::Transport::AdoptT0::AcquireImported(ahb);
resource->buffer.AdoptExternal(ctx.device, imported.buffer, imported.memory, size, imported.mapped,
imported.coherent, ahb);
resource->imported = true;
resource->lastUseSerial = 0;
resource->lastUseSubmitIndex = 0;
resource->gpuWritesPending = false;
// The whole store holds bytes the client supplied (its AHB, seeded from its shadow).
resource->stagedCoverage.clear();
MG_Remote::Server::StagedShadowStore::CoverageAdd(resource->stagedCoverage, 0, static_cast<SizeT>(size));
++m_sliceEpochCounter;
++m_wireStoreCount;
NoteWireStorePeaks();
PublishWireReclaimGauges();
MGLOG_D("Magma T0: imported {slot=%u, gen=%u} (%llu bytes, %s memory)", res.Slot, res.Gen,
static_cast<unsigned long long>(size), imported.coherent ? "coherent" : "non-coherent");
return true;
#else
(void)res;
(void)ahb;
(void)size;
return false;
#endif
}
// THE POST (B2; decided by probe, never by driver name). The AHB API promises CPU coherence only
// across an unlock/lock, and T0 holds the client's lock for the store's life, so this exercises
// that pattern on THIS device before kCapAdoptT0 may be published:
// hold - a 64 KiB BLOB allocated and locked by the test, never unlocked while it runs;
// read - this server's own host map of the import reads what the CPU wrote through the lock;
// gpu - one submission copies that pattern out of the import (the GPU read) and fills another
// region of it (the GPU write), then a transfer->host barrier and a fence wait;
// back - the fill is read through the HELD pointer, the copy from a host-visible staging buffer;
// steady - the same again with bytes the CPU wrote AFTER the import and its first GPU use (the
// block at the end says why the first round alone is not enough).
Bool VkBufferManager::SelfTestWireImport(char* whyOut, Uint64 whyBytes) {
const auto answer = [&](Bool ok, const String& text) {
std::snprintf(whyOut, static_cast<SizeT>(whyBytes), "%s", text.c_str());
return ok;
};
#if defined(__ANDROID__)
VulkanRenderer::WireAhbImport ctx;
if (pVulkanRenderer == nullptr || !pVulkanRenderer->GetWireAhbImport(ctx))
return answer(false, "the device did not take VK_ANDROID_external_memory_android_hardware_buffer");
constexpr Uint64 kBytes = 64 * 1024;
constexpr Uint32 kWords = 1024; // one 4 KiB region
constexpr Uint64 kFillAt = 4 * 4096; // region 4
constexpr Uint32 kFillWord = 0x6C0FFEE0u;
Vector<Uint32> pattern(kBytes / 4);
for (SizeT i = 0; i < pattern.size(); ++i) pattern[i] = 0x5A000000u ^ static_cast<Uint32>(i * 2654435761u);
MG_Remote::Transport::AdoptT0::HeldStore held;
String why;
if (!MG_Remote::Transport::AdoptT0::AllocateHeld(kBytes, pattern.data(), held, why))
return answer(false, "hold: " + why);
T0ImportedBuffer imported, staging;
VkCommandPool pool = VK_NULL_HANDLE;
VkFence fence = VK_NULL_HANDLE;
String verdict;
Bool ok = false;
do {
if (!T0ImportAhb(ctx, held.ahb, kBytes,
VK_BUFFER_USAGE_TRANSFER_SRC_BIT | VK_BUFFER_USAGE_TRANSFER_DST_BIT |
VK_BUFFER_USAGE_STORAGE_BUFFER_BIT,
imported, why)) {
verdict = "import: " + why;
break;
}
if (!imported.coherent) {
VkMappedMemoryRange range{VK_STRUCTURE_TYPE_MAPPED_MEMORY_RANGE};
range.memory = imported.memory;
range.size = VK_WHOLE_SIZE;
vkInvalidateMappedMemoryRanges(ctx.device, 1, &range);
}
if (std::memcmp(imported.mapped, pattern.data(), kWords * 4) != 0) {
verdict = "read: the server's host map of the import does not hold the CPU's pattern";
break;
}
// A host-visible staging buffer for the GPU read's destination.
VkBufferCreateInfo stagingInfo{VK_STRUCTURE_TYPE_BUFFER_CREATE_INFO};
stagingInfo.size = kWords * 4;
stagingInfo.usage = VK_BUFFER_USAGE_TRANSFER_DST_BIT;
stagingInfo.sharingMode = VK_SHARING_MODE_EXCLUSIVE;
if (vkCreateBuffer(ctx.device, &stagingInfo, nullptr, &staging.buffer) != VK_SUCCESS) {
staging.buffer = VK_NULL_HANDLE;
verdict = "staging buffer";
break;
}
VkMemoryRequirements requirements{};
vkGetBufferMemoryRequirements(ctx.device, staging.buffer, &requirements);
const Int32 stagingType = T0PickMemoryType(ctx.memory, requirements.memoryTypeBits,
VK_MEMORY_PROPERTY_HOST_VISIBLE_BIT | VK_MEMORY_PROPERTY_HOST_COHERENT_BIT);
VkMemoryAllocateInfo stagingAlloc{VK_STRUCTURE_TYPE_MEMORY_ALLOCATE_INFO};
stagingAlloc.allocationSize = requirements.size;
stagingAlloc.memoryTypeIndex = static_cast<Uint32>(stagingType < 0 ? 0 : stagingType);
if (stagingType < 0 || vkAllocateMemory(ctx.device, &stagingAlloc, nullptr, &staging.memory) != VK_SUCCESS ||
vkBindBufferMemory(ctx.device, staging.buffer, staging.memory, 0) != VK_SUCCESS ||
vkMapMemory(ctx.device, staging.memory, 0, VK_WHOLE_SIZE, 0, &staging.mapped) != VK_SUCCESS) {
verdict = "staging memory";
break;
}
std::memset(staging.mapped, 0, kWords * 4);
VkCommandPoolCreateInfo poolInfo{VK_STRUCTURE_TYPE_COMMAND_POOL_CREATE_INFO};
poolInfo.queueFamilyIndex = ctx.queueFamily;
poolInfo.flags = VK_COMMAND_POOL_CREATE_TRANSIENT_BIT;
VkCommandBuffer commands = VK_NULL_HANDLE;
VkCommandBufferAllocateInfo commandInfo{VK_STRUCTURE_TYPE_COMMAND_BUFFER_ALLOCATE_INFO};
commandInfo.level = VK_COMMAND_BUFFER_LEVEL_PRIMARY;
commandInfo.commandBufferCount = 1;
VkFenceCreateInfo fenceInfo{VK_STRUCTURE_TYPE_FENCE_CREATE_INFO};
if (vkCreateCommandPool(ctx.device, &poolInfo, nullptr, &pool) != VK_SUCCESS) {
pool = VK_NULL_HANDLE;
verdict = "command pool";
break;
}
commandInfo.commandPool = pool;
if (vkAllocateCommandBuffers(ctx.device, &commandInfo, &commands) != VK_SUCCESS ||
vkCreateFence(ctx.device, &fenceInfo, nullptr, &fence) != VK_SUCCESS) {
verdict = "command buffer / fence";
break;
}
VkCommandBufferBeginInfo begin{VK_STRUCTURE_TYPE_COMMAND_BUFFER_BEGIN_INFO};
begin.flags = VK_COMMAND_BUFFER_USAGE_ONE_TIME_SUBMIT_BIT;
vkBeginCommandBuffer(commands, &begin);
VkMemoryBarrier hostWrites{VK_STRUCTURE_TYPE_MEMORY_BARRIER};
hostWrites.srcAccessMask = VK_ACCESS_HOST_WRITE_BIT;
hostWrites.dstAccessMask = VK_ACCESS_TRANSFER_READ_BIT | VK_ACCESS_TRANSFER_WRITE_BIT;
vkCmdPipelineBarrier(commands, VK_PIPELINE_STAGE_HOST_BIT, VK_PIPELINE_STAGE_TRANSFER_BIT, 0, 1,
&hostWrites, 0, nullptr, 0, nullptr);
VkBufferCopy region{0, 0, kWords * 4};
vkCmdCopyBuffer(commands, imported.buffer, staging.buffer, 1, &region);
vkCmdFillBuffer(commands, imported.buffer, kFillAt, kWords * 4, kFillWord);
VkMemoryBarrier toHost{VK_STRUCTURE_TYPE_MEMORY_BARRIER};
toHost.srcAccessMask = VK_ACCESS_TRANSFER_WRITE_BIT;
toHost.dstAccessMask = VK_ACCESS_HOST_READ_BIT;
vkCmdPipelineBarrier(commands, VK_PIPELINE_STAGE_TRANSFER_BIT, VK_PIPELINE_STAGE_HOST_BIT, 0, 1, &toHost,
0, nullptr, 0, nullptr);
vkEndCommandBuffer(commands);
VkSubmitInfo submit{VK_STRUCTURE_TYPE_SUBMIT_INFO};
submit.commandBufferCount = 1;
submit.pCommandBuffers = &commands;
if (vkQueueSubmit(ctx.queue, 1, &submit, fence) != VK_SUCCESS ||
vkWaitForFences(ctx.device, 1, &fence, VK_TRUE, 2'000'000'000ull) != VK_SUCCESS) {
verdict = "gpu: the submission did not complete within 2 s";
break;
}
Uint32 gpuRead = 0, heldSaw = 0;
const auto* copied = static_cast<const Uint32*>(staging.mapped);
const auto* heldWords = static_cast<const Uint32*>(held.ptr);
for (Uint32 i = 0; i < kWords; ++i) {
if (copied[i] == pattern[i]) ++gpuRead;
if (heldWords[kFillAt / 4 + i] == kFillWord) ++heldSaw;
}
ok = gpuRead == kWords && heldSaw == kWords;
verdict = "64 KiB AHB, lock held throughout: host map read ok (" +
String(imported.coherent ? "coherent" : "non-coherent") + " memory), GPU read " +
std::to_string(gpuRead) + "/1024 words of the CPU's pattern, GPU write seen through the "
"held lock " + std::to_string(heldSaw) + "/1024 words";
if (!ok) break;
// STEADY STATE. The round above reads bytes the CPU wrote BEFORE the import, and an import
// can clean the CPU's caches on its way in, so it passes on a device whose GPU never sees a
// write made through the held lock afterwards - which is all of T0's real traffic (Mali-G1
// on a Dimensity: the first round passed, then Flywheel's staging scatter read stale bytes
// and the create-indirect trace lost every instanced object). So the CPU now writes a fresh
// pattern into region 1 and reads region 4 back into its cache, and one more submission
// copies region 1 out and overwrites region 4.
constexpr Uint64 kSteadyAt = 1 * 4096; // region 1
constexpr Uint32 kSteadyFillWord = 0x3D5EA11Eu;
auto* heldWrite = static_cast<volatile Uint32*>(held.ptr);
Uint32 cached = 0;
for (Uint32 i = 0; i < kWords; ++i) {
heldWrite[kSteadyAt / 4 + i] = 0xA5000000u ^ (i * 40503u);
cached ^= heldWrite[kFillAt / 4 + i];
}
(void)cached;
std::memset(staging.mapped, 0, kWords * 4);
if (vkResetFences(ctx.device, 1, &fence) != VK_SUCCESS ||
vkResetCommandPool(ctx.device, pool, 0) != VK_SUCCESS ||
vkBeginCommandBuffer(commands, &begin) != VK_SUCCESS) {
ok = false;
verdict = "steady: the second submission could not be recorded";
break;
}
vkCmdPipelineBarrier(commands, VK_PIPELINE_STAGE_HOST_BIT, VK_PIPELINE_STAGE_TRANSFER_BIT, 0, 1,
&hostWrites, 0, nullptr, 0, nullptr);
VkBufferCopy steadyRegion{kSteadyAt, 0, kWords * 4};
vkCmdCopyBuffer(commands, imported.buffer, staging.buffer, 1, &steadyRegion);
vkCmdFillBuffer(commands, imported.buffer, kFillAt, kWords * 4, kSteadyFillWord);
vkCmdPipelineBarrier(commands, VK_PIPELINE_STAGE_TRANSFER_BIT, VK_PIPELINE_STAGE_HOST_BIT, 0, 1, &toHost,
0, nullptr, 0, nullptr);
vkEndCommandBuffer(commands);
if (vkQueueSubmit(ctx.queue, 1, &submit, fence) != VK_SUCCESS ||
vkWaitForFences(ctx.device, 1, &fence, VK_TRUE, 2'000'000'000ull) != VK_SUCCESS) {
ok = false;
verdict = "steady: the second submission did not complete within 2 s";
break;
}
Uint32 steadyRead = 0, steadySaw = 0;
for (Uint32 i = 0; i < kWords; ++i) {
if (copied[i] == (0xA5000000u ^ (i * 40503u))) ++steadyRead;
if (heldWords[kFillAt / 4 + i] == kSteadyFillWord) ++steadySaw;
}
ok = steadyRead == kWords && steadySaw == kWords;
verdict += "; with the import in use, GPU read " + std::to_string(steadyRead) +
"/1024 words the CPU wrote after it, GPU write seen through the held lock " +
std::to_string(steadySaw) + "/1024 words";
} while (false);
if (fence != VK_NULL_HANDLE) vkDestroyFence(ctx.device, fence, nullptr);
if (pool != VK_NULL_HANDLE) vkDestroyCommandPool(ctx.device, pool, nullptr);
T0DestroyImported(ctx.device, staging);
T0DestroyImported(ctx.device, imported);
MG_Remote::Transport::AdoptT0::ReleaseHeld(held);
return answer(ok, verdict);
#else
return answer(false, "no AHardwareBuffer on this platform");
#endif
}
void VkBufferManager::DestroyWireBuffer(MG_Pipe::MGPipeHandle res) {
const auto found = m_wireBuffers.find(WireBufferKey(res));
if (found == m_wireBuffers.end()) return;
DeferWireRelease(std::move(found->second.buffer), found->second.lastUseSerial);
DeferWireRelease(std::move(found->second.buffer),
found->second.imported ? T0ReleaseSerial(found->second.lastUseSerial)
: found->second.lastUseSerial); // P11 B2, as Respecify
m_wireBuffers.erase(found);
++m_sliceEpochCounter;
PublishWireReclaimGauges();
@@ -125,6 +125,14 @@ namespace MobileGL::MG_Backend::DirectVulkan {
Bool CopyWireBufferSubWordRangeToSlice(MG_Pipe::MGPipeHandle res, Uint64 offset, Uint64 size,
Uint32 frameIndex, const BufferSlice& dst, Uint64 dstSkip);
void MarkWireBufferGpuWritten(MG_Pipe::MGPipeHandle res, Uint64 offset, Uint64 size);
// P8-D: whether a shader write was marked on this store since the last indirect draw
// read it natively, clearing the mark. The caller records the INDIRECT_COMMAND_READ
// barrier before the draw when this answers true. Transfer writes need no mark: their own
// after-barriers already make them visible to MEMORY_READ at ALL_COMMANDS.
Bool TakeWireIndirectReadBarrier(MG_Pipe::MGPipeHandle res);
// P8-D: every WaitForWireBufferHostAccess that actually waited, over this manager's life -
// the attribution base a caller reads before and after its own ReadWireBuffer.
Uint64 GetWireHostWaitCount() const { return m_wireHostWaits; }
// Resource-op entry points. All run on the server apply owner.
void CreateWireBuffer(MG_Pipe::MGPipeHandle res, const MG_Pipe::MGPResourceDesc& desc);
@@ -134,6 +142,15 @@ namespace MobileGL::MG_Backend::DirectVulkan {
void FlushWireBuffer(MG_Pipe::MGPipeHandle res, Uint64 offset, Uint64 size, const void* bytes);
void ReadbackWireBuffer(MG_Pipe::MGPipeHandle res, Uint64 offset, Uint64 size);
void DestroyWireBuffer(MG_Pipe::MGPipeHandle res);
// P11 B2 (T0): make the client's AHardwareBuffer `ahb` (a BLOB of `size` bytes) buffer
// `res`'s store - a VkBuffer on memory imported through
// VK_ANDROID_external_memory_android_hardware_buffer, host-visible and mapped like every
// wire store, so every wire path (draw binds, WriteWireBuffer's ordered copy, CPU readers)
// applies unchanged. The T2 store it replaces goes through DeferWireRelease. False = not
// imported (the T2 store is untouched and the client is DECLINED).
Bool ImportWireBuffer(MG_Pipe::MGPipeHandle res, void* ahb, Uint64 size);
// The POST self-test of the sustained-lock pattern on this device (see the definition).
static Bool SelfTestWireImport(char* why, Uint64 whyBytes);
#endif
// Recreate all per-frame transient arenas
@@ -245,12 +262,22 @@ namespace MobileGL::MG_Backend::DirectVulkan {
// Only ranges actually submitted by resource_subdata are covered. No
// shadow is retained: flush cannot replay stale bytes over GPU writes.
Vector<Range1D> stagedCoverage;
// P11 B2 (T0): `buffer` is the client's imported AHardwareBuffer. Its readback posts
// no bytes (the client reads its own pages once the wait below it is done).
Bool imported = false;
// P8-D: a shader write was marked (MarkWireBufferGpuWritten) and no indirect draw has
// recorded its INDIRECT_COMMAND_READ barrier since (TakeWireIndirectReadBarrier).
Bool indirectReadBarrierPending = false;
};
// P8-D: see GetWireHostWaitCount.
Uint64 m_wireHostWaits = 0;
static Uint64 WireBufferKey(MG_Pipe::MGPipeHandle res) {
return (static_cast<Uint64>(res.Gen) << 32) | res.Slot;
}
WireBufferResource* FindWireBuffer(MG_Pipe::MGPipeHandle res);
Bool WaitForWireBufferHostAccess(WireBufferResource& resource);
// P11 B2: the serial a T0 store's release waits behind (never "destroy at once").
Uint64 T0ReleaseSerial(Uint64 lastUseSerial) const;
// P7 wave 4 (M2), ID-P7-27: THE WIRE ARM'S ORPHANS NEED A RECLAIM THAT IS NOT A FRAME
// BOUNDARY.
@@ -8,13 +8,45 @@
#include "VkBufferObject.h"
#if MOBILEGL_BUILD_DISAGGREGATED
#include <MG_Remote/Transport/AdoptT0.h>
#endif
namespace MobileGL::MG_Backend::DirectVulkan {
#if MOBILEGL_BUILD_DISAGGREGATED
void VkBufferObject::MoveExternalFrom(VkBufferObject& other) {
m_device = other.m_device;
m_externalMemory = other.m_externalMemory;
m_externalAhb = other.m_externalAhb;
m_externalCoherent = other.m_externalCoherent;
other.m_device = VK_NULL_HANDLE;
other.m_externalMemory = VK_NULL_HANDLE;
other.m_externalAhb = nullptr;
other.m_externalCoherent = true;
}
void VkBufferObject::AdoptExternal(VkDevice device, VkBuffer buffer, VkDeviceMemory memory, VkDeviceSize size,
void* mapped, Bool coherent, void* ahb) {
Destroy();
m_device = device;
m_buffer = buffer;
m_externalMemory = memory;
m_mappedData = mapped;
m_size = size;
m_externalCoherent = coherent;
m_externalAhb = ahb;
}
#endif
VkBufferObject::VkBufferObject(VkBufferObject&& other) noexcept {
m_allocator = other.m_allocator;
m_buffer = other.m_buffer;
m_allocation = other.m_allocation;
m_mappedData = other.m_mappedData;
m_size = other.m_size;
#if MOBILEGL_BUILD_DISAGGREGATED
MoveExternalFrom(other);
#endif
other.m_allocator = nullptr;
other.m_buffer = VK_NULL_HANDLE;
@@ -35,6 +67,9 @@ namespace MobileGL::MG_Backend::DirectVulkan {
m_allocation = other.m_allocation;
m_mappedData = other.m_mappedData;
m_size = other.m_size;
#if MOBILEGL_BUILD_DISAGGREGATED
MoveExternalFrom(other);
#endif
other.m_allocator = nullptr;
other.m_buffer = VK_NULL_HANDLE;
@@ -89,6 +124,24 @@ namespace MobileGL::MG_Backend::DirectVulkan {
}
void VkBufferObject::Destroy() {
#if MOBILEGL_BUILD_DISAGGREGATED
if (m_externalMemory != VK_NULL_HANDLE) {
// P11 B2 (T0): an imported AHardwareBuffer store. Reached through the wire arm's
// deferred release, i.e. once no submission can still name it.
if (m_mappedData != nullptr) vkUnmapMemory(m_device, m_externalMemory);
if (m_buffer != VK_NULL_HANDLE) vkDestroyBuffer(m_device, m_buffer, nullptr);
vkFreeMemory(m_device, m_externalMemory, nullptr);
MG_Remote::Transport::AdoptT0::ReleaseImported(m_externalAhb);
m_mappedData = nullptr;
m_buffer = VK_NULL_HANDLE;
m_externalMemory = VK_NULL_HANDLE;
m_externalAhb = nullptr;
m_device = VK_NULL_HANDLE;
m_externalCoherent = true;
m_size = 0;
return;
}
#endif
Unmap();
if (m_allocator != nullptr && m_buffer != VK_NULL_HANDLE && m_allocation != nullptr) {
vmaDestroyBuffer(m_allocator, m_buffer, m_allocation);
@@ -117,6 +170,10 @@ namespace MobileGL::MG_Backend::DirectVulkan {
}
void VkBufferObject::Unmap() {
#if MOBILEGL_BUILD_DISAGGREGATED
// An imported store stays mapped for its life (Destroy unmaps it).
if (m_externalMemory != VK_NULL_HANDLE) return;
#endif
if (!IsValid() || m_mappedData == nullptr) {
m_mappedData = nullptr;
return;
@@ -143,6 +200,16 @@ namespace MobileGL::MG_Backend::DirectVulkan {
}
Memcpy(static_cast<Uint8*>(mapped) + offset, data, static_cast<SizeT>(size));
#if MOBILEGL_BUILD_DISAGGREGATED
if (m_externalMemory != VK_NULL_HANDLE) {
if (m_externalCoherent) return true;
VkMappedMemoryRange range{VK_STRUCTURE_TYPE_MAPPED_MEMORY_RANGE};
range.memory = m_externalMemory;
range.offset = 0;
range.size = VK_WHOLE_SIZE;
return vkFlushMappedMemoryRanges(m_device, 1, &range) == VK_SUCCESS;
}
#endif
const VkResult flushResult = vmaFlushAllocation(m_allocator, m_allocation, offset, size);
if (flushResult != VK_SUCCESS) {
MGLOG_E_ONCE("VkBufferObject::Upload failed: vmaFlushAllocation returned %d", flushResult);
@@ -167,6 +234,16 @@ namespace MobileGL::MG_Backend::DirectVulkan {
if (resolvedSize == 0) {
return true;
}
#if MOBILEGL_BUILD_DISAGGREGATED
if (m_externalMemory != VK_NULL_HANDLE) {
if (m_externalCoherent) return true;
VkMappedMemoryRange range{VK_STRUCTURE_TYPE_MAPPED_MEMORY_RANGE};
range.memory = m_externalMemory;
range.offset = 0;
range.size = VK_WHOLE_SIZE;
return vkInvalidateMappedMemoryRanges(m_device, 1, &range) == VK_SUCCESS;
}
#endif
const VkResult result = vmaInvalidateAllocation(m_allocator, m_allocation, offset, resolvedSize);
if (result != VK_SUCCESS) {
@@ -64,7 +64,25 @@ namespace MobileGL::MG_Backend::DirectVulkan {
}
void* GetMappedData() const { return m_mappedData; }
Bool IsMapped() const { return m_mappedData != nullptr; }
Bool IsValid() const { return m_allocator != nullptr && m_buffer != VK_NULL_HANDLE && m_allocation != nullptr; }
Bool IsValid() const {
#if MOBILEGL_BUILD_DISAGGREGATED
if (m_externalMemory != VK_NULL_HANDLE) return m_buffer != VK_NULL_HANDLE;
#endif
return m_allocator != nullptr && m_buffer != VK_NULL_HANDLE && m_allocation != nullptr;
}
#if MOBILEGL_BUILD_DISAGGREGATED
// P11 B2 (T0): a store whose memory is NOT this allocator's - a client's AHardwareBuffer
// imported with VK_ANDROID_external_memory_android_hardware_buffer into `memory` (a
// dedicated allocation bound to `buffer`, mapped persistently at `mapped`). It behaves
// like any mapped store (GetSlice, Upload, Invalidate), and Destroy frees the buffer and
// the memory and drops `ahb` - which is why the store goes through the wire arm's
// serial-gated deferred release like every other: the AHardwareBuffer is let go only once
// no submission can still name it. `coherent` = the memory type is HOST_COHERENT.
void AdoptExternal(VkDevice device, VkBuffer buffer, VkDeviceMemory memory, VkDeviceSize size,
void* mapped, Bool coherent, void* ahb);
Bool IsExternal() const { return m_externalMemory != VK_NULL_HANDLE; }
#endif
private:
VmaAllocator m_allocator = nullptr;
@@ -72,5 +90,12 @@ namespace MobileGL::MG_Backend::DirectVulkan {
VmaAllocation m_allocation = nullptr;
void* m_mappedData = nullptr;
VkDeviceSize m_size = 0;
#if MOBILEGL_BUILD_DISAGGREGATED
VkDevice m_device = VK_NULL_HANDLE;
VkDeviceMemory m_externalMemory = VK_NULL_HANDLE;
void* m_externalAhb = nullptr;
Bool m_externalCoherent = true;
void MoveExternalFrom(VkBufferObject& other);
#endif
};
} // namespace MobileGL::MG_Backend::DirectVulkan
@@ -1333,6 +1333,42 @@ namespace MobileGL::MG_Backend::DirectVulkan {
}
}
VkTextureManager::TextureResource* VkTextureManager::FindTextureResource(
MG_State::GLState::ITextureObject& textureOrView) {
auto it = m_textureResources.find(MakeTextureIdentity(&StorageTextureOf(textureOrView)));
return it != m_textureResources.end() ? &it->second : nullptr;
}
Bool VkTextureManager::HasLiveImage(MG_State::GLState::ITextureObject& textureOrView) const {
auto it = m_textureResources.find(MakeTextureIdentity(&StorageTextureOf(textureOrView)));
return it != m_textureResources.end() && it->second.image != VK_NULL_HANDLE;
}
Bool VkTextureManager::HasPendingTexelUpload(MG_State::GLState::ITextureObject& textureOrView) const {
auto& storage = StorageTextureOf(textureOrView);
auto it = m_textureResources.find(MakeTextureIdentity(&storage));
if (it == m_textureResources.end() || it->second.image == VK_NULL_HANDLE) {
return false;
}
return it->second.syncedShapeVersion == storage.GetShapeVersion() &&
it->second.syncedContentVersion != storage.GetContentVersion();
}
Bool VkTextureManager::WasTextureWrittenThisRecording(MG_State::GLState::ITextureObject& textureOrView) const {
auto it = m_textureResources.find(MakeTextureIdentity(&StorageTextureOf(textureOrView)));
return it != m_textureResources.end() && it->second.lastRecordingWriteGeneration == m_recordingGeneration;
}
void VkTextureManager::StampTextureRecordingWrite(MG_State::GLState::ITextureObject* texture) {
if (texture == nullptr) {
return;
}
auto it = m_textureResources.find(MakeTextureIdentity(&StorageTextureOf(*texture)));
if (it != m_textureResources.end()) {
StampResourceRecordingWrite(it->second);
}
}
void VkTextureManager::UpdateTrackedImageLayout(MG_State::GLState::ITextureObject* texture, VkImageLayout newLayout) {
MOBILEGL_ASSERT(texture != nullptr, "UpdateTrackedImageLayout: texture is null");
auto it = m_textureResources.find(MakeTextureIdentity(texture));
@@ -1361,7 +1397,7 @@ namespace MobileGL::MG_Backend::DirectVulkan {
"UpdateTrackedImageLayoutAfterAttachmentWrite: textureId=%d mipLevel=%u out of range %u",
texture->GetExternalIndex(), writtenMipLevel, resource.mipLevels);
// Pre-pass stream bookkeeping: the render pass that just ended wrote this image.
StampResourceRecordingUse(resource);
StampResourceRecordingWrite(resource);
if (resource.layout != newLayout && resource.mipLevels > 1) {
VkPipelineStageFlags srcStageMask = VK_PIPELINE_STAGE_TOP_OF_PIPE_BIT;
@@ -1475,8 +1511,9 @@ namespace MobileGL::MG_Backend::DirectVulkan {
resource->aspect, 0, resource->mipLevels);
MOBILEGL_ASSERT(ok, "TransitionTextureForStorageImage: transition failed for textureId=%d",
texture.GetExternalIndex());
// Pre-pass stream bookkeeping: a command referencing the image was recorded.
StampResourceRecordingUse(*resource);
// Pre-pass stream bookkeeping: a command referencing the image was recorded, and an
// image unit may store to it.
StampResourceRecordingWrite(*resource);
return ok;
}
@@ -4077,18 +4114,17 @@ namespace MobileGL::MG_Backend::DirectVulkan {
VkDeviceSize offset = 0;
// Sub-region upload (a small sprite in a big atlas): only the dirty box
// is staged and copied. texelSize keeps the LEVEL extent - the staging
// row copy needs it for the shadow's stride. Plain color formats only;
// the RGB-expand and depth(+stencil) conversion passes rewrite whole
// levels and stay full-size.
// row copy needs it for the stride. Color formats only (an RGB-expanded
// level is cut from its expanded bytes); the depth(+stencil) conversion
// passes rewrite whole levels and stay full-size.
Bool subRegion = false;
IntVec3 regionLo = {0, 0, 0};
IntVec3 regionSize = {0, 0, 0};
SizeT texelBytes = 0;
// Scatter refinement of the single dirty box: when the storage's rect
// list reports the writes' true footprint (~100 sprites whose union box
// spans the whole atlas), each rect is staged tightly and copied with
// its own VkBufferImageCopy in ONE vkCmdCopyBufferToImage. Empty means
// "stage the one box above". Only set while subRegion.
// The writes' exact footprint when it is more than one box (~100 sprites
// whose union box spans the whole atlas): each rect is staged tightly and
// copied with its own VkBufferImageCopy in ONE vkCmdCopyBufferToImage.
// Empty means "stage the one box above". Only set while subRegion.
Vector<MG_State::GLState::MipmapDirtyRegion> rects;
};
@@ -4100,6 +4136,7 @@ namespace MobileGL::MG_Backend::DirectVulkan {
targets.push_back(uploadTarget);
}
const TextureFormatInfo formatInfo = ResolveTextureFormatInfo(mipmapTexture.GetFormat());
Vector<MG_State::GLState::MipmapDirtyRegion> dirtyFootprint;
VkDeviceSize stagingSize = 0;
for (const TextureUploadTarget target : targets) {
@@ -4142,34 +4179,48 @@ namespace MobileGL::MG_Backend::DirectVulkan {
uploadItem.source = source;
uploadItem.offset = stagingSize;
uploadItem.uploadByteSize = byteSize;
if (!formatInfo.expandRgbToRgba &&
GetAspectMaskForFormat(outResource.format) == VK_IMAGE_ASPECT_COLOR_BIT) {
// The RGB -> RGBA expansion rewrites the level texel for texel into a tight buffer
// of its own, so it runs first and the dirty region below is taken from whichever
// bytes are staged. Uploading an expanded level WHOLE put the shadow's stale texels
// over whatever the GPU had drawn into the rest of it.
if (formatInfo.expandRgbToRgba) {
const Bool expanded = ExpandRgbSourceToRgba(source, byteSize, texelSize, formatInfo,
uploadItem.expandedData);
MOBILEGL_ASSERT(expanded,
"UploadDirtyMipLevels: failed to expand RGB textureId=%d target=%s level=%u to RGBA staging data",
mipmapTexture.GetExternalIndex(),
MG_Util::ConvertTextureUploadTargetToString(target).c_str(), level);
uploadItem.uploadByteSize = uploadItem.expandedData.size();
}
const SizeT levelBytes = uploadItem.uploadByteSize;
if (GetAspectMaskForFormat(outResource.format) == VK_IMAGE_ASPECT_COLOR_BIT) {
const auto region = mipmapTexture.GetStorageDirtyRegion(target, level);
const SizeT texelCount = static_cast<SizeT>(texelSize.x()) *
static_cast<SizeT>(texelSize.y()) *
static_cast<SizeT>(std::max(texelSize.z(), 1));
if (!region.Empty() && !region.CoversWholeLevel(texelSize) && texelCount > 0 &&
byteSize % texelCount == 0) {
// Exactly the texels the client wrote, never their union box or the storage's
// rect list (which merges touching writes into their bounding box, folds when
// full and is withheld past 3/4 of the box): the image may hold what the GPU
// drew or cleared anywhere else, and the shadow is stale there. Each box is one
// more VkBufferImageCopy in the same vkCmdCopyBufferToImage, which costs next
// to nothing, so there is no cheaper shape worth being wrong for. A union box
// that spans the level with gaps in it is not a whole-level write either.
mipmapTexture.GetStorageDirtyFootprint(target, level, dirtyFootprint);
if (!region.Empty() && (dirtyFootprint.size() >= 2 || !region.CoversWholeLevel(texelSize)) &&
texelCount > 0 && levelBytes % texelCount == 0) {
uploadItem.subRegion = true;
uploadItem.regionLo = region.lo;
uploadItem.regionSize = {region.hi.x() - region.lo.x(), region.hi.y() - region.lo.y(),
region.hi.z() - region.lo.z()};
uploadItem.texelBytes = byteSize / texelCount;
uploadItem.texelBytes = levelBytes / texelCount;
uploadItem.uploadByteSize = static_cast<SizeT>(uploadItem.regionSize.x()) *
static_cast<SizeT>(uploadItem.regionSize.y()) *
static_cast<SizeT>(uploadItem.regionSize.z()) *
uploadItem.texelBytes;
// Scatter refinement: the storage only hands out its rect list
// when the rects' summed area is materially smaller than the
// union box (0 otherwise), so taking it always stages fewer
// bytes than the box - the very amplification this path exists
// to avoid paying twice.
MG_State::GLState::MipmapDirtyRegion
dirtyRects[MG_State::GLState::MipmapStorage::kMaxDirtyRects];
const SizeT dirtyRectCount = mipmapTexture.GetStorageDirtyRects(
target, level, dirtyRects, MG_State::GLState::MipmapStorage::kMaxDirtyRects);
if (dirtyRectCount >= 2) {
uploadItem.rects.assign(dirtyRects, dirtyRects + dirtyRectCount);
// Several boxes: each is staged tightly and copied on its own. One box is
// the union box above.
if (dirtyFootprint.size() >= 2) {
uploadItem.rects.assign(dirtyFootprint.begin(), dirtyFootprint.end());
SizeT rectTexels = 0;
for (const auto& rect : uploadItem.rects) {
rectTexels += rect.TexelCount();
@@ -4195,15 +4246,6 @@ namespace MobileGL::MG_Backend::DirectVulkan {
}
}
}
if (formatInfo.expandRgbToRgba) {
const Bool expanded = ExpandRgbSourceToRgba(source, byteSize, texelSize, formatInfo,
uploadItem.expandedData);
MOBILEGL_ASSERT(expanded,
"UploadDirtyMipLevels: failed to expand RGB textureId=%d target=%s level=%u to RGBA staging data",
mipmapTexture.GetExternalIndex(),
MG_Util::ConvertTextureUploadTargetToString(target).c_str(), level);
uploadItem.uploadByteSize = uploadItem.expandedData.size();
}
uploadItems.push_back(Move(uploadItem));
if (!uploadItems.back().expandedData.empty()) {
uploadItems.back().source = uploadItems.back().expandedData.data();
@@ -4438,6 +4480,7 @@ namespace MobileGL::MG_Backend::DirectVulkan {
const Bool depthSelectsArrayLayer = outResource.viewType == VK_IMAGE_VIEW_TYPE_1D_ARRAY ||
outResource.viewType == VK_IMAGE_VIEW_TYPE_2D_ARRAY ||
outResource.viewType == VK_IMAGE_VIEW_TYPE_CUBE_ARRAY;
Vector<VkBufferImageCopy> rectCopies;
for (const auto& item : uploadItems) {
if (!item.rects.empty()) {
// Multi-rect item: one VkBufferImageCopy per rect, all submitted in a
@@ -4445,7 +4488,7 @@ namespace MobileGL::MG_Backend::DirectVulkan {
// construction, so no two copies write the same texels. Multi-rect
// implies subRegion, which implies a plain color aspect - the combined
// depth-stencil split below can never see one of these.
VkBufferImageCopy rectCopies[MG_State::GLState::MipmapStorage::kMaxDirtyRects];
rectCopies.resize(item.rects.size());
Uint32 rectCopyCount = 0;
VkDeviceSize runningOffset = item.offset;
for (const auto& rect : item.rects) {
@@ -4476,7 +4519,7 @@ namespace MobileGL::MG_Backend::DirectVulkan {
runningOffset += static_cast<VkDeviceSize>(rect.TexelCount() * item.texelBytes);
}
vkCmdCopyBufferToImage(commandBuffer, stagingBuffer, outResource.image,
VK_IMAGE_LAYOUT_TRANSFER_DST_OPTIMAL, rectCopyCount, rectCopies);
VK_IMAGE_LAYOUT_TRANSFER_DST_OPTIMAL, rectCopyCount, rectCopies.data());
continue;
}
const Uint32 depthOrLayers = item.texelSize.z() > 0 ? static_cast<Uint32>(item.texelSize.z()) : 1u;
@@ -332,6 +332,11 @@ public:
// into the frame's PRE command buffer - which executes strictly before the
// frame's commands - instead of splitting the active render pass.
Uint64 lastRecordingGeneration = 0;
// The same, for the last recorded command that WRITES the image (a render pass it is an
// attachment of, a clear, a blit/copy into it, a mip generation, a storage-image binding).
// An upload into an image written this recording has to be ordered after that write
// (VulkanRenderer::OrderPendingUploadAfterRecording).
Uint64 lastRecordingWriteGeneration = 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;
@@ -381,6 +386,7 @@ public:
std::swap(this->storageUsageResolved, that.storageUsageResolved);
std::swap(this->syncedTextureParamsVersion, that.syncedTextureParamsVersion);
std::swap(this->lastRecordingGeneration, that.lastRecordingGeneration);
std::swap(this->lastRecordingWriteGeneration, that.lastRecordingWriteGeneration);
std::swap(this->syncedContentVersion, that.syncedContentVersion);
std::swap(this->syncedMipLevelCount, that.syncedMipLevelCount);
std::swap(this->syncedShapeVersion, that.syncedShapeVersion);
@@ -623,6 +629,26 @@ public:
Bool WasTouchedThisRecording(const TextureResource& resource) const {
return resource.lastRecordingGeneration == m_recordingGeneration;
}
// A write is a use too. See TextureResource::lastRecordingWriteGeneration.
void StampResourceRecordingWrite(TextureResource& resource) const {
resource.lastRecordingGeneration = m_recordingGeneration;
resource.lastRecordingWriteGeneration = m_recordingGeneration;
}
void StampTextureRecordingWrite(MG_State::GLState::ITextureObject* texture);
// Upload-ordering queries (VulkanRenderer::OrderPendingUploadAfterRecording). None of them
// syncs, so nothing pending is uploaded by asking. All answer for the STORAGE texture.
//
// Uploads are recorded into a batch submitted AHEAD of the frame's recording
// (FlushPendingUploads), so an upload into an image the open recording already WROTE lands
// before that write, which then overwrites the newer texels. (An image the recording only
// read keeps the old trade: its earlier reads see the newer texels.)
Bool HasLiveImage(MG_State::GLState::ITextureObject& textureOrView) const;
// The next sync would record client texels into the image that is live now: same shape (a
// shape change mints a new image instead) and content moved since the last sync.
Bool HasPendingTexelUpload(MG_State::GLState::ITextureObject& textureOrView) const;
Bool WasTextureWrittenThisRecording(MG_State::GLState::ITextureObject& textureOrView) const;
// The live resource, or null.
TextureResource* FindTextureResource(MG_State::GLState::ITextureObject& textureOrView);
// Records that this texture is bound to a GL image unit, so its image must carry
// VK_IMAGE_USAGE_STORAGE_BIT. Must be called before NeedsStorageImagePreparation, and
// therefore before the render pass is committed: an image that has to be upgraded is
@@ -2003,6 +2003,9 @@ void main() {
outBinding.label, texture->GetExternalIndex());
return false;
}
if (!isReadFramebuffer) {
textureManager.StampResourceRecordingWrite(*resource);
}
outBinding.image = resource->image;
outBinding.trackedLayout = &resource->layout;
@@ -2119,6 +2122,9 @@ void main() {
outBinding.label, texture->GetExternalIndex(), static_cast<Uint32>(requiredAspectMask));
return false;
}
if (!isReadFramebuffer) {
textureManager.StampResourceRecordingWrite(*resource);
}
outBinding.image = resource->image;
outBinding.trackedLayout = &resource->layout;
@@ -2159,6 +2165,7 @@ void main() {
texture.GetExternalIndex(), mipLevel, resource->mipLevels);
return false;
}
textureManager.StampResourceRecordingWrite(*resource);
outBinding.image = resource->image;
outBinding.trackedLayout = &resource->layout;
@@ -3175,7 +3182,10 @@ void main() {
}
VulkanRenderer::VulkanRenderer(NativeWindowType window, const VulkanRendererConfig& cfg)
: m_window(window), m_config(cfg) {
: m_window(window), m_config(cfg), m_presentsToAppWindow(window != NativeWindowType{}) {
if (!m_presentsToAppWindow) {
m_config.SwapInterval.reset();
}
// Initialize();
}
@@ -3572,6 +3582,9 @@ void main() {
m_device = VK_NULL_HANDLE;
}
s_vkCmdDrawIndexedIndirectCount = nullptr;
#if MOBILEGL_BUILD_DISAGGREGATED
s_vkCmdWireDrawIndirectCount = nullptr;
#endif
s_vkCmdDrawMultiEXT = nullptr;
s_vkCmdDrawMultiIndexedEXT = nullptr;
@@ -7073,6 +7086,10 @@ void main() {
Bool VulkanRenderer::SetupDraw(FrameContext::FrameData& frame, GLenum mode, Flags<DrawSetupAspect> aspects,
const DrawCmdParam& drawParams,
const IndexBufferView* pIndexBufferView) {
// First, before anything of this draw is recorded or resolved - on the wire arm too: a
// split server records every draw its client sends into the same frame command buffer, so
// the same loading frame grows it the same way there.
SplitOversizedRecording();
#if MOBILEGL_BUILD_DISAGGREGATED
if (MG_Config::Transport != MG_Config::TransportMode::Monolith) {
RewindWireDescriptorSetsIfDue();
@@ -7381,6 +7398,15 @@ void main() {
VkRenderPassManager::EndRenderPass(frame.commandBuffer);
activeRenderPass = nullptr;
}
// The syncs below upload each sampled texture's pending texels into a batch that runs
// ahead of this recording; one whose image the recording already cleared or drew into
// would be overwritten by that earlier work. Submit it first, here, before anything below
// records into the frame or decides from the active pass.
for (auto* sampledTexture : sampledTextures) {
if (sampledTexture != nullptr && OrderPendingUploadAfterRecording(frame, *sampledTexture)) {
activeRenderPass = nullptr;
}
}
Bool needSampledTextureTransitions = false;
auto& sampledResources = m_sampledResourcesScratch;
sampledResources.assign(sampledTextures.size(), nullptr);
@@ -7699,6 +7725,8 @@ void main() {
}
void VulkanRenderer::DispatchCompute(GLuint numGroupsX, GLuint numGroupsY, GLuint numGroupsZ) {
// Before the wire branch, for SetupDraw's reason.
SplitOversizedRecording();
#if MOBILEGL_BUILD_DISAGGREGATED
if (MG_Config::Transport != MG_Config::TransportMode::Monolith) {
RewindWireDescriptorSetsIfDue();
@@ -7760,6 +7788,16 @@ void main() {
}
void VulkanRenderer::DispatchComputeIndirect(GLintptr indirect) {
SplitOversizedRecording();
#if MOBILEGL_BUILD_DISAGGREGATED
// P8-SV: the wire arm's own indirect dispatch, DispatchCompute's shape one call over.
if (MG_Config::Transport != MG_Config::TransportMode::Monolith) {
RewindWireDescriptorSetsIfDue();
m_textureManager->CollectGarbage();
DispatchWireComputeIndirect(indirect);
return;
}
#endif
m_textureManager->CollectGarbage();
auto& frame = m_frameContext.GetCurrent();
// See DispatchCompute: the dispatch accessor, not the draw one.
@@ -8203,6 +8241,7 @@ void main() {
m_renderPassManager->QueueRenderbufferClear(
{.mask = GL_COLOR_BUFFER_BIT, .color = payload.color}, attachment);
} else if (attachment.IsTexture()) {
SettleTexelsBeforeQueuedClear(*fbo, attachment);
m_clearManager->QueueClear({.mask = GL_COLOR_BUFFER_BIT, .color = payload.color},
attachment);
}
@@ -8214,6 +8253,19 @@ void main() {
return;
}
if ((deferredMask & GL_COLOR_BUFFER_BIT) != 0) {
for (const auto drawBuffer : fbo->GetDrawBuffers()) {
if (drawBuffer != FramebufferAttachmentType::None) {
SettleTexelsBeforeQueuedClear(*fbo, fbo->GetAttachment(drawBuffer));
}
}
}
if ((deferredMask & GL_DEPTH_BUFFER_BIT) != 0) {
SettleTexelsBeforeQueuedClear(*fbo, fbo->GetAttachment(FramebufferAttachmentType::Depth));
}
if ((deferredMask & GL_STENCIL_BUFFER_BIT) != 0) {
SettleTexelsBeforeQueuedClear(*fbo, fbo->GetAttachment(FramebufferAttachmentType::Stencil));
}
m_clearManager->QueueClear(deferredMask, payload, *fbo);
m_renderPassManager->QueueRenderbufferClear(deferredMask, payload, *fbo);
}
@@ -8291,6 +8343,7 @@ void main() {
if (!attachment.IsTexture()) {
return;
}
SettleTexelsBeforeQueuedClear(framebuffer, attachment);
m_clearManager->QueueClear(payload, attachment);
};
@@ -8657,7 +8710,8 @@ void main() {
}
Bool VulkanRenderer::MaterializePendingClearForTexture(VkCommandBuffer commandBuffer,
MG_State::GLState::ITextureObject& texture) {
MG_State::GLState::ITextureObject& texture,
Bool uploadPendingTexels) {
Vector<PendingClearEntry> pendingClears;
if (!m_clearManager->GetPendingClears(&texture, pendingClears)) {
return true;
@@ -8669,8 +8723,9 @@ void main() {
commandBuffer != m_frameContext.GetCurrent().commandBuffer,
"MaterializePendingClearForTexture requires no active render pass on the target buffer");
auto* resource = m_textureManager->SyncTextureAndGetDescriptor(texture);
if (resource == nullptr) {
auto* resource = uploadPendingTexels ? m_textureManager->SyncTextureAndGetDescriptor(texture)
: m_textureManager->FindTextureResource(texture);
if (resource == nullptr || resource->image == VK_NULL_HANDLE) {
// Declined sync (an incomplete texture, say). Nothing to clear into, and every line
// below dereferences this - the assert that used to stand here is compiled out of
// every build past DEBUG.
@@ -8822,6 +8877,9 @@ void main() {
"MaterializePendingClearForTexture: failed to transition textureId=%d to sampled layout",
texture.GetExternalIndex());
resource->layout = sampledLayout;
// The clear is now a write in this recording, which is what orders a later upload into
// the image after it (OrderPendingUploadAfterRecording).
m_textureManager->StampResourceRecordingWrite(*resource);
m_clearManager->PopPendingClear(&texture);
MGLOG_D("MaterializePendingClearForTexture: textureId=%d pending clear materialized",
@@ -8829,6 +8887,62 @@ void main() {
return true;
}
Bool VulkanRenderer::OrderPendingUploadAfterRecording(FrameContext::FrameData& frame,
MG_State::GLState::ITextureObject& texture) {
if (!m_textureManager->HasPendingTexelUpload(texture)) {
return false;
}
// An open pass drawing into the image stamps that write only when it ends.
if (const auto* activeRenderPass = VkRenderPassManager::GetActiveRenderPass();
activeRenderPass != nullptr && ActiveRenderPassUsesTexture(*activeRenderPass, texture)) {
VkRenderPassManager::EndRenderPass(frame.commandBuffer);
}
const Bool clearFirst = m_clearManager->HasPendingClear(&texture);
if (!clearFirst && !m_textureManager->WasTextureWrittenThisRecording(texture)) {
// Nothing recorded writes the image: the upload may run ahead of the recording.
return false;
}
if (!frame.isCommandRecording) {
m_frameContext.BeginCommandRecording();
}
if (VkRenderPassManager::GetActiveRenderPass() != nullptr) {
VkRenderPassManager::EndRenderPass(frame.commandBuffer);
}
if (clearFirst && !MaterializePendingClearForTexture(frame.commandBuffer, texture,
/*uploadPendingTexels=*/false)) {
MGLOG_E_ONCE("OrderPendingUploadAfterRecording: textureId=%d kept its queued clear; it may land over "
"texels written after it",
texture.GetExternalIndex());
}
if (!FlushPendingCommands()) {
MGLOG_E_ONCE("OrderPendingUploadAfterRecording: submit failed; textureId=%d's upload may run ahead of "
"earlier work on it",
texture.GetExternalIndex());
return false;
}
m_frameContext.BeginCommandRecording();
// New command buffer: the same reset SetupDraw applies when it begins one.
m_lastSampledSetValid = false;
return true;
}
void VulkanRenderer::SettleTexelsBeforeQueuedClear(
const MG_State::GLState::FramebufferObject& framebuffer,
const MG_State::GLState::FramebufferAttachmentObject& attachment) {
// The default framebuffer's attachments are placeholders: its clears land on the
// swapchain image (MaterializePendingClearForDefaultFramebuffer) and it has no texels.
if (framebuffer.IsDefaultFramebuffer() || !attachment.IsTexture() || attachment.IsRenderbuffer()) {
return;
}
const auto texture = attachment.GetTexture();
if (!texture || (m_textureManager->HasLiveImage(*texture) &&
!m_textureManager->HasPendingTexelUpload(*texture))) {
return;
}
OrderPendingUploadAfterRecording(m_frameContext.GetCurrent(), *texture);
m_textureManager->SyncTextureAndGetDescriptor(*texture);
}
Bool VulkanRenderer::MaterializeMultisamplePendingClear(VkCommandBuffer commandBuffer,
MG_State::GLState::ITextureObject& texture,
VkTextureManager::TextureResource& resource,
@@ -10782,6 +10896,7 @@ void main() {
const Bool dstClearReady = materializeClear(dstEndpoint);
MOBILEGL_ASSERT(dstClearReady, "%s: failed to materialize pending clear for destination objectId=%u",
__func__, CopyImageEndpointName(dstEndpoint));
m_textureManager->StampTextureRecordingWrite(dstEndpoint.Texture.get());
const VkImageLayout srcOriginalLayout = *srcImage.trackedLayout;
const VkImageLayout dstOriginalLayout = *dstImage.trackedLayout;
@@ -11066,6 +11181,13 @@ void main() {
}
auto& frame = m_frameContext.GetCurrent();
if (!readFbo->IsDefaultFramebuffer()) {
// The read syncs the source texture, whose pending texels must land after work already
// recorded against it (see OrderPendingUploadAfterRecording).
if (const auto readTexture = readFbo->GetAttachment(readFbo->GetReadBuffer()).GetTexture()) {
OrderPendingUploadAfterRecording(frame, *readTexture);
}
}
if (!frame.isCommandRecording) {
m_frameContext.BeginCommandRecording();
}
@@ -11791,6 +11913,9 @@ void main() {
return;
}
// The sync below uploads pending texels, which must land after work already recorded
// against this image (see OrderPendingUploadAfterRecording).
OrderPendingUploadAfterRecording(m_frameContext.GetCurrent(), *textureObject);
auto* resource = m_textureManager->SyncTextureAndGetDescriptor(*textureObject);
// Two shapes end up in the same place, and for the same reason: the GL level being read has
// no GPU storage, so UploadDirtyMipLevels never wrote it and the CPU shadow is the ONLY copy
@@ -12136,6 +12261,7 @@ void main() {
resource = m_textureManager->SyncTextureAndGetDescriptor(*texture);
MOBILEGL_ASSERT(resource != nullptr && resource->image != VK_NULL_HANDLE,
"GenerateMipmap failed to resync the backend texture after allocating mip storage.");
m_textureManager->StampResourceRecordingWrite(*resource);
if (resource->layout == VK_IMAGE_LAYOUT_UNDEFINED) {
const VkImageLayout finalLayout = ResolveGenerateMipmapFinalLayout(resource->aspect);
Bool transitioned = VkTextureManager::TransitionImageLayout(
@@ -14091,6 +14217,54 @@ void main() {
return FlushPendingCommands();
}
void VulkanRenderer::SplitOversizedRecording() {
const Uint32 maxDraws = MG_Config::Features.MagmaMaxDrawsPerCommandBuffer;
if (maxDraws == 0) {
return;
}
// Counted before the check, so a buffer carries at most maxDraws calls before the one
// that splits it. OnFrameCommandRecordingBegan resets the count on every new recording.
if (++m_drawsInRecording <= maxDraws || !HasPendingRecordedWork()) {
return;
}
// Nothing of the current call is recorded yet, so the split falls between two GL calls,
// exactly where a glFlush would put it: the render pass ends with STORE, the next draw
// re-begins it with LOAD, and queries and transform feedback are bracketed per draw.
if (!FlushPendingCommands()) {
return;
}
// Fresh command buffer: the same reset every other mid-frame flush site applies.
m_lastSampledSetValid = false;
++m_oversizedRecordingSplits;
MGLOG_I_ONCE("DirectVulkan: a frame recorded more than %u draws into one command buffer; submitting it and "
"continuing on a fresh one (MOBILEGL_MAGMA_MAX_DRAWS_PER_COMMAND_BUFFER, 0 = unbounded)",
maxDraws);
MGLOG_D("DirectVulkan: split an oversized recording (%llu so far, submit index %llu)",
static_cast<unsigned long long>(m_oversizedRecordingSplits),
static_cast<unsigned long long>(m_submitCounter));
// Splitting only bounds the driver's command memory if the submitted buffers are freed
// as fast as they are produced. A GPU that falls behind would park them all on the
// retired list, which is the same growth spread over more buffers - so hold the queue
// to frames-in-flight + 1 outstanding submissions, the steady state of a pipelined
// frame plus this split. Wait on the fence directly rather than through
// WaitForSubmitIndex: its opportunistic TryDrainFrameTransients may rewind the transient
// arena, and this runs at the start of a draw whose caller may already hold slices of it.
const SizeT maxOutstanding = static_cast<SizeT>(m_frameContext.GetFrameCount()) + 1;
while (m_inFlightSubmits.size() > maxOutstanding) {
const SubmitRecord oldest = m_inFlightSubmits.front();
if (oldest.fence == VK_NULL_HANDLE) {
break;
}
const VkResult waitResult = vkWaitForFences(m_device, 1, &oldest.fence, VK_TRUE, UINT64_MAX);
if (waitResult != VK_SUCCESS) {
MGLOG_E_ONCE("SplitOversizedRecording: vkWaitForFences returned %d", waitResult);
break;
}
OnSubmitsCompletedUpTo(oldest.submitIndex);
}
}
Bool VulkanRenderer::WaitForSubmitIndex(Uint64 submitIndex, Uint64 timeoutNs, Bool flushIfPending) {
if (IsSubmitIndexComplete(submitIndex)) {
return true;
@@ -14136,6 +14310,7 @@ void main() {
}
void VulkanRenderer::OnFrameCommandRecordingBegan(VkCommandBuffer commandBuffer) {
m_drawsInRecording = 0;
// Dynamic state does not survive a command-buffer boundary.
ResetDynamicStateShadow();
InvalidateSetupDrawSnapshots();
@@ -14406,8 +14581,22 @@ void main() {
if (!m_swapchainResizeRequested && SwapchainIsOutOfDate()) {
m_swapchainResizeRequested = true;
}
// A new eglSwapInterval lands here too, between frames: this one is on screen and the
// next has recorded nothing. Only a different present mode is worth the rebuild (it
// drops every pipeline); otherwise the live swapchain already paces as asked.
if (m_config.SwapInterval != m_swapchainSwapInterval) {
if (SwapchainObject::ChooseSwapchainPresentMode(m_swapchainObject.GetSupportedPresentModes(),
m_config.SwapInterval) !=
m_swapchainObject.GetPresentMode()) {
m_swapchainResizeRequested = true;
} else {
MGLOG_D("DirectVulkan: swap interval %d -> %s", *m_config.SwapInterval,
SwapchainObject::GetPresentModeName(m_swapchainObject.GetPresentMode()));
m_swapchainSwapInterval = m_config.SwapInterval;
}
}
if (m_swapchainResizeRequested) {
MGLOG_D("Present, processing requested swapchain resize");
MGLOG_D("Present, processing requested swapchain rebuild (resize or swap interval)");
if (!RecreateSwapchain()) {
m_presentSuspended = true;
m_swapchainResizeRequested = false;
@@ -15156,6 +15345,30 @@ void main() {
// P7 gate 5 (g5-msprobe): which multisample depth/stencil resolve arm goes first is measured
// once the device exists (ArmWireDepthResolveOrder, below), not keyed on the vendor.
m_wirePreferShaderDepthResolve = false;
// P11 B2 (T0): a client's AHardwareBuffer imported as a wire buffer store. Wire arms, on
// Android, whenever the device advertises the extension - not per session: the device is
// created once per server and a later session asking T0 must find it able. Enabling it
// changes no T2 behaviour; only an import uses it. The 1.0-era dependencies are core at the
// 1.1 this device is created at, and are enabled too when advertised (the spike's set).
m_wireAhbImport = false;
m_wireGetAhbProperties = nullptr;
#if defined(__ANDROID__)
if (MG_Config::Transport != MG_Config::TransportMode::Monolith &&
m_physicalDevice.properties.apiVersion >= VK_API_VERSION_1_1 &&
IsExtensionSupported(availableExtensions, VK_ANDROID_EXTERNAL_MEMORY_ANDROID_HARDWARE_BUFFER_EXTENSION_NAME) &&
IsExtensionSupported(availableExtensions, VK_EXT_QUEUE_FAMILY_FOREIGN_EXTENSION_NAME)) {
m_wireAhbImport = true;
for (const char* name : {VK_ANDROID_EXTERNAL_MEMORY_ANDROID_HARDWARE_BUFFER_EXTENSION_NAME,
VK_EXT_QUEUE_FAMILY_FOREIGN_EXTENSION_NAME, VK_KHR_EXTERNAL_MEMORY_EXTENSION_NAME,
VK_KHR_SAMPLER_YCBCR_CONVERSION_EXTENSION_NAME,
VK_KHR_DEDICATED_ALLOCATION_EXTENSION_NAME,
VK_KHR_GET_MEMORY_REQUIREMENTS_2_EXTENSION_NAME,
VK_KHR_BIND_MEMORY_2_EXTENSION_NAME, VK_KHR_MAINTENANCE_1_EXTENSION_NAME}) {
if (IsExtensionSupported(availableExtensions, name))
EnableOptionalDeviceExtension(availableExtensions, enabledDeviceExtensions, name);
}
}
#endif
#endif
if ((descriptorIndexingCore || descriptorIndexingExtension) && getPhysicalDeviceFeatures2 != nullptr &&
getPhysicalDeviceProperties2 != nullptr) {
@@ -15715,6 +15928,11 @@ void main() {
m_wireCreateRenderPass2 = reinterpret_cast<PFN_vkCreateRenderPass2>(
vkGetDeviceProcAddr(m_device, wireDepthResolveCore ? "vkCreateRenderPass2" : "vkCreateRenderPass2KHR"));
}
if (m_wireAhbImport) {
m_wireGetAhbProperties =
reinterpret_cast<void*>(vkGetDeviceProcAddr(m_device, "vkGetAndroidHardwareBufferPropertiesANDROID"));
if (m_wireGetAhbProperties == nullptr) m_wireAhbImport = false;
}
#endif
s_vkCmdDrawIndexedIndirectCount = reinterpret_cast<PFNDrawIndexedIndirectCountFunc>(
@@ -15727,6 +15945,16 @@ void main() {
MGLOG_W("VK_KHR_draw_indirect_count enabled but vkCmdDrawIndexedIndirectCount entry point is missing, will continue as if VK_KHR_draw_indirect_count is not supported!");
m_drawIndirectCountExtensionEnabled = false;
}
#if MOBILEGL_BUILD_DISAGGREGATED
// P8-D: the wire arm's glMultiDrawArraysIndirectCount. Null = that verb reads its count on
// the CPU (WireDraw.inc's DrawWireIndirectNative answers false for it).
s_vkCmdWireDrawIndirectCount = reinterpret_cast<PFNDrawIndexedIndirectCountFunc>(
vkGetDeviceProcAddr(m_device, "vkCmdDrawIndirectCountKHR"));
if (s_vkCmdWireDrawIndirectCount == nullptr) {
s_vkCmdWireDrawIndirectCount = reinterpret_cast<PFNDrawIndexedIndirectCountFunc>(
vkGetDeviceProcAddr(m_device, "vkCmdDrawIndirectCount"));
}
#endif
s_vkCmdDrawMultiEXT = nullptr;
s_vkCmdDrawMultiIndexedEXT = nullptr;
@@ -16085,7 +16313,12 @@ void main() {
m_swapchainObject.Create(m_device, m_physicalDevice.handle, m_surface,
static_cast<Uint32>(m_physicalDevice.queueFamilies.graphicsFamily),
static_cast<Uint32>(m_physicalDevice.queueFamilies.presentFamily),
m_config.MaxFramesInFlight, desiredExtent);
m_config.MaxFramesInFlight, desiredExtent, m_config.SwapInterval);
if (m_config.SwapInterval && m_config.SwapInterval != m_swapchainSwapInterval) {
MGLOG_D("DirectVulkan: swap interval %d -> %s", *m_config.SwapInterval,
SwapchainObject::GetPresentModeName(m_swapchainObject.GetPresentMode()));
}
m_swapchainSwapInterval = m_config.SwapInterval;
// The FragCoordYFlip variants bake this height in; it is the only input to a shader
// module that lives outside the GL program, so the factory has to learn it here (and on
// every recreation, which is the only way it can change).
@@ -16541,10 +16774,29 @@ void main() {
m_swapchainResizeRequested = true;
}
void VulkanRenderer::SetSwapInterval(Int interval) {
if (m_presentsToAppWindow) {
m_config.SwapInterval = interval;
}
}
VkInstance VulkanRenderer::GetInstance() const {
return m_instance;
}
#if MOBILEGL_BUILD_DISAGGREGATED
Bool VulkanRenderer::GetWireAhbImport(WireAhbImport& out) const {
out = WireAhbImport{};
if (!m_wireAhbImport || m_device == VK_NULL_HANDLE || m_wireGetAhbProperties == nullptr) return false;
out.device = m_device;
out.queue = m_graphicsQueue;
out.queueFamily = static_cast<Uint32>(m_physicalDevice.queueFamilies.graphicsFamily);
vkGetPhysicalDeviceMemoryProperties(m_physicalDevice.handle, &out.memory);
out.getAhbProperties = m_wireGetAhbProperties;
return true;
}
#endif
Bool VulkanRenderer::IsDrawIndirectCountExtensionEnabled() const {
return m_drawIndirectCountExtensionEnabled;
}
@@ -160,6 +160,25 @@ namespace MobileGL::MG_Backend::DirectVulkan {
// GetSyncPointSubmitIndex(), which answers m_submitCounter when nothing is recorded -
// true for a fence taken at that instant, wrong for work about to be recorded.
Uint64 GetWireNextSubmitIndex() const { return m_submitCounter + 1; }
// P8-D: the wire arm's indirect draw families, issued as vkCmdDraw[Indexed]Indirect[Count]
// from the wire stores MGPipeApplier().VerbIndirectBuffer / VerbIndirectParameterBuffer
// name. `offset` / `countOffset` are byte offsets into those stores; `stride` 0 means
// tightly packed. FALSE = this device cannot issue the COUNT form natively (the monolith
// arm's own condition, see the definition in WireDraw.inc) and the caller must read the
// words on the CPU; TRUE = issued, or declined by name.
Bool DrawWireIndirectNative(GLenum mode, GLenum type, Uint64 offset, GLsizei drawcount, GLsizei stride,
Bool indexed, Bool counted, Uint64 countOffset);
// P11 B2 (T0): what importing a client's AHardwareBuffer as a wire buffer store needs from
// this device. False when the device did not take VK_ANDROID_external_memory_android_
// hardware_buffer at creation (not Android, not advertised, or a monolith device).
struct WireAhbImport {
VkDevice device = VK_NULL_HANDLE;
VkQueue queue = VK_NULL_HANDLE;
Uint32 queueFamily = 0;
VkPhysicalDeviceMemoryProperties memory{};
void* getAhbProperties = nullptr; // PFN_vkGetAndroidHardwareBufferPropertiesANDROID
};
Bool GetWireAhbImport(WireAhbImport& out) const;
#endif
// FrameContext::IRecordingObserver: prepares the frame's timer-query
@@ -327,6 +346,10 @@ namespace MobileGL::MG_Backend::DirectVulkan {
// signal as soon as the GPU actually finishes, which MC 1.21.5's
// fence-paced ring buffers rely on to recycle their space.
Uint64 GetSyncPointSubmitIndex() const;
// For tests: the per-frame descriptor pools (see DescriptorPoolCensus.h).
DescriptorPoolCensus GetDescriptorPoolCensus() const {
return m_uniformManager ? m_uniformManager->GetDescriptorPoolCensus() : DescriptorPoolCensus{};
}
// Non-blocking: polls outstanding submission fences and reports
// whether every submission up to `submitIndex` has completed.
Bool IsSubmitIndexComplete(Uint64 submitIndex);
@@ -390,6 +413,12 @@ namespace MobileGL::MG_Backend::DirectVulkan {
Bool ResolveOcclusionQueryResult(const Vector<Uint32>& slots, Uint64& outSamples);
void RequestSwapchainResize(Uint32 width, Uint32 height);
// eglSwapInterval. Records the request; Present rebuilds the swapchain for it once the
// current frame is on screen, and only when it maps to a different present mode. A
// renderer created without an app window ignores it: its surface (headless, or an
// AImageReader nobody consumes) is never displayed, and FIFO there would block once
// the reader's queue fills.
void SetSwapInterval(Int interval);
// Re-query the surface and report whether the live swapchain no longer matches it
// (size or orientation). This - not a VK_SUBOPTIMAL_KHR result - is what decides a
// rebuild, so a surface the driver merely considers suboptimal cannot thrash.
@@ -564,6 +593,10 @@ namespace MobileGL::MG_Backend::DirectVulkan {
// target unwritten while the shader control resolved - the shader arm then resolves
// first and the render-pass arm is the fallback (P7 gate 5, g5-msprobe).
Bool m_wirePreferShaderDepthResolve = false;
// P11 B2 (T0): the device took the AHardwareBuffer import extensions at creation, and the
// one entry point the import needs (PFN_vkGetAndroidHardwareBufferPropertiesANDROID).
Bool m_wireAhbImport = false;
void* m_wireGetAhbProperties = nullptr;
// Runs the probe (memoized per device identity) and sets the member above. Called at the end of
// device creation, after ArmPrimGenReroute: it records on m_graphicsQueue.
void ArmWireDepthResolveOrder();
@@ -655,7 +688,14 @@ namespace MobileGL::MG_Backend::DirectVulkan {
void CollectWireObjects(Uint64 completedSubmit, Bool all = false);
Vector<WireRetiredObjects> m_wireRetiredObjects;
Uint32 m_wirePreparationDepth = 0;
// P8-D: set only while DrawWireIndirectNative is inside SetupDraw, so SetupWireDraw can
// tell the wire arm's own indirect draw from a monolith indirect entry point reached under
// a transport (still the `buffer-legacy-arm` role violation).
Bool m_wireNativeIndirectDraw = false;
void DispatchWireCompute(GLuint x, GLuint y, GLuint z);
// P8-SV: glDispatchComputeIndirect on the wire arm - vkCmdDispatchIndirect from the
// dispatch-indirect store, as the monolith arm issues it (WireDraw.inc).
void DispatchWireComputeIndirect(GLintptr offset);
void RewindWireDescriptorSetsIfDue();
UniquePtr<RenderPassEntry> m_wireDrawPass;
Vector<VkImageView> m_wireDrawViews;
@@ -730,6 +770,15 @@ namespace MobileGL::MG_Backend::DirectVulkan {
// only), so the presenting path's frames-in-flight pipelining is
// untouched. Returns true when the drain ran.
Bool TryDrainFrameTransients();
// Bounded recording: called at the top of every GL draw and dispatch, before anything
// of that call is recorded. Once the open frame command buffer holds
// MagmaMaxDrawsPerCommandBuffer draws/dispatches, submits it (FlushPendingCommands) so
// the call continues on a fresh one, then waits until at most frames-in-flight + 1
// submissions are outstanding. A driver backs a command buffer with GPU memory it only
// returns when the buffer is freed (Adreno: one 16 KiB mapping per chunk), so a frame
// that is never split grows it without bound - rd12's loading frame records ~1M draws
// into one buffer and runs the process out of vm.max_map_count.
void SplitOversizedRecording();
Vector<SubmitRecord> m_inFlightSubmits;
Vector<VkFence> m_freeSubmitFences;
@@ -741,6 +790,10 @@ namespace MobileGL::MG_Backend::DirectVulkan {
// aging clocks, while present-less loops still cross a boundary every few
// iterations. Reset in Present.
Uint32 m_drainsSinceLastPresent = 0;
// GL draws/dispatches recorded into the open frame command buffer; reset whenever a
// recording begins (OnFrameCommandRecordingBegan). See SplitOversizedRecording.
Uint32 m_drawsInRecording = 0;
Uint64 m_oversizedRecordingSplits = 0;
NativeWindowType m_window = 0;
void* m_platformDisplay = nullptr;
@@ -760,6 +813,11 @@ namespace MobileGL::MG_Backend::DirectVulkan {
// Shutdown() deletes it.
void* m_fallbackImageReader = nullptr;
VulkanRendererConfig m_config;
// Constructed for the app's window rather than a pbuffer (m_window is later filled in
// for the pbuffer fallbacks too, so it cannot tell them apart).
Bool m_presentsToAppWindow = false;
// The m_config.SwapInterval the live swapchain was built for; Present compares the two.
Optional<Int> m_swapchainSwapInterval;
Bool m_swapchainResizeRequested = false;
// Presentation is suspended while the window is zero-area (minimized): the
// swapchain is unusable/out of date, so Present drops frames instead of
@@ -878,6 +936,12 @@ namespace MobileGL::MG_Backend::DirectVulkan {
VkDeviceSize countBufferOffset, Uint32 maxDrawCount,
Uint32 stride);
static inline PFNDrawIndexedIndirectCountFunc s_vkCmdDrawIndexedIndirectCount = nullptr;
#if MOBILEGL_BUILD_DISAGGREGATED
// P8-D: vkCmdDrawIndirectCount (same signature, same extension), for the wire arm's
// glMultiDrawArraysIndirectCount. The monolith arm never loads it: it reads that count on
// the CPU (DirectVulkan.cpp's MultiDrawArraysIndirectCount).
static inline PFNDrawIndexedIndirectCountFunc s_vkCmdWireDrawIndirectCount = nullptr;
#endif
// VK_EXT_multi_draw entry points, loaded at device creation when the extension
// (and its multiDraw feature) is enabled; null otherwise.
static inline PFN_vkCmdDrawMultiEXT s_vkCmdDrawMultiEXT = nullptr;
@@ -1861,8 +1925,28 @@ namespace MobileGL::MG_Backend::DirectVulkan {
MG_State::GLState::ITextureObject& texture, Uint32 mipLevel,
Uint32 depthSlice, const VkClearValue& clearValue,
VkImageLayout finalLayout = VK_IMAGE_LAYOUT_TRANSFER_DST_OPTIMAL);
// Records the texture's queued clears. By default the texture is synced first, which
// uploads any pending texels BEFORE the clear - right only for texels written before it.
// `uploadPendingTexels = false` leaves them pending (OrderPendingUploadAfterRecording).
Bool MaterializePendingClearForTexture(VkCommandBuffer commandBuffer,
MG_State::GLState::ITextureObject& texture);
MG_State::GLState::ITextureObject& texture,
Bool uploadPendingTexels = true);
// Texture uploads run in a batch submitted ahead of the frame's recording, so the upload
// the next sync of `texture` records would land BEFORE a write already recorded into the
// same image (a render, clear, blit or copy), and before a clear still queued for it (such
// a clear predates the texels: SettleTexelsBeforeQueuedClear). When either holds, this
// records the queued clear without the texels and submits the recording, so the upload
// follows both. An image the recording only READ keeps the old trade: no submit, and its
// earlier reads see the newer texels. It may end the render pass and replace
// frame.commandBuffer (recording again on return), so call it only where the caller holds
// no render-pass or command-buffer state across it. Returns whether it submitted.
Bool OrderPendingUploadAfterRecording(FrameContext::FrameData& frame,
MG_State::GLState::ITextureObject& texture);
// Called for each texture a glClear/glClearBuffer* is about to queue a clear on: uploads
// whatever texels are still pending (and mints the image if there is none), so that any
// texel pending when the clear is materialized was written after it.
void SettleTexelsBeforeQueuedClear(const MG_State::GLState::FramebufferObject& framebuffer,
const MG_State::GLState::FramebufferAttachmentObject& attachment);
// The multisample arm of the above. Split out rather than branched inline because it
// shares none of the transfer path: a multisample image carries no TRANSFER_DST usage, so
// neither the TRANSFER_DST transition nor vkCmdClearColorImage is legal on one.
@@ -58,7 +58,7 @@ MGL_WIRE_DECLINE(CurrentAttributePayload)
MGL_WIRE_DECLINE(CurrentAttributeUpload)
MGL_WIRE_DECLINE(BeginRenderPass)
// ---- DispatchWireCompute ----------------------------------------------------------------
// ---- DispatchWireCompute (and DispatchWireComputeIndirect, P8-SV) -------------------------
MGL_WIRE_DECLINE(ComputeProgramLinkOrSpirv)
MGL_WIRE_DECLINE(ComputeTextureResourcesUnprepared)
MGL_WIRE_DECLINE(ComputePipelineUnavailable)
@@ -104,3 +104,25 @@ MGL_WIRE_DECLINE(ShapeCreateImageFailed)
// Appended by the B3 review round: the preserve-copy arm of a reallocation was the one exit in
// SyncWireTextureShape still leaving no trace at all.
MGL_WIRE_DECLINE(ShapePreserveCopyFailed)
// ---- VulkanRenderer::GenerateWireMipmap (P8-F) -----------------------------------------
// `mipmap-shader-format-or-shape` was a Fatal on this arm and a decline on the monolith arm; it
// is the monolith's decline now. Counted once per refused glGenerateMipmap, whose chain is left
// as it was.
MGL_WIRE_DECLINE(MipmapShaderFormatOrShape)
// ---- VulkanRenderer::DrawWireIndirectNative (P8-D), in statement order -----------------------
MGL_WIRE_DECLINE(IndirectBufferUnbound)
MGL_WIRE_DECLINE(IndirectCommandRange)
MGL_WIRE_DECLINE(IndirectCountRange)
MGL_WIRE_DECLINE(IndirectIndexStore)
// ---- P8-SV -------------------------------------------------------------------------------
// VulkanRenderer::GenerateWireMipmap: a combined depth/stencil (or stencil-only) chain, which no
// arm generates (CONTRACT-P7 §3.2). It logged but was never counted, so MGWIRE-DECLINES could
// not see it. Counted once per refused glGenerateMipmap, whose chain is left as it was.
MGL_WIRE_DECLINE(MipmapDepthStencilAspect)
// VulkanRenderer::DispatchWireComputeIndirect, in statement order: the dispatch-indirect store
// is missing, or the three group-count words lie outside it or are misaligned.
MGL_WIRE_DECLINE(DispatchIndirectBufferUnbound)
MGL_WIRE_DECLINE(DispatchIndirectRange)
@@ -64,6 +64,14 @@
if (source.samples != VK_SAMPLE_COUNT_1_BIT || destination.samples != VK_SAMPLE_COUNT_1_BIT)
return false;
if (source.is3D || destination.is3D) return false;
// P8-SV: THE PASS MAKES 2D VIEWS (makeView below), which a 1D, 1D-array or 3D image does
// not admit. A caller that fills viewType from the image gets a refusal here, and the
// wire arm's GenerateWireMipmap asks the same question before it grows the chain.
for (const VkImageViewType viewType : {source.viewType, destination.viewType}) {
if (viewType == VK_IMAGE_VIEW_TYPE_1D || viewType == VK_IMAGE_VIEW_TYPE_1D_ARRAY ||
viewType == VK_IMAGE_VIEW_TYPE_3D)
return false;
}
if (source.format != destination.format) return false;
// DEPTH ONLY. A combined depth/stencil image can be sampled for its depth and
// rendered as a depth attachment, but this pass writes gl_FragDepth and NOTHING
@@ -207,7 +207,10 @@
MGL_WIRE_DECLINE_AT(ProgramLinkOrSpirv, "program is not linked or carries no SPIR-V");
return false;
}
if (aspects & DrawSetupAspect::IndirectDrawBuffer)
// P8-D: the wire arm's own indirect draw (DrawWireIndirectNative) is the one caller that
// may pass this aspect; a monolith indirect entry point reached here is still the role
// violation.
if ((aspects & DrawSetupAspect::IndirectDrawBuffer) && !m_wireNativeIndirectDraw)
WireBufferLegacyFatal();
MakeXfbWritesVisible();
const auto* fbo = state.DrawFramebuffer();
@@ -294,7 +297,14 @@
const Uint32 location = input.bindingAttributeLocations[binding];
const auto& attr = vao.Attributes[location];
const auto& resource = state.VertexBuffers[location];
if (MG_Pipe::MGPipeHandleIsNull(resource.Res)) MagmaWireFatal("client-vertex-array@P8");
// P8-F: A RECORD INVARIANT, NOT AN UNMIGRATED VERB (it carried a P8 phase tag).
// The client turns every enabled client-memory array into an owned buffer before the
// draw record is built, or refuses the draw itself (MG_Impl/Pipe/OwnedDrawInputs.h),
// so an attribute the program reads with no buffer handle is a foreign peer's record.
if (MG_Pipe::MGPipeHandleIsNull(resource.Res))
MG_Pipe::MGPipeSessionFail(MG_Pipe::MGPipeFatalFamily::ProtocolCorruption,
"MGPipe: Fatal{ProtocolCorruption, \"Magma:client-vertex-array\"} location=%u: an "
"attribute the program reads carries no buffer handle", location);
BufferSlice slice{};
if (!m_bufferManager.AcquireWireSlice(BufferKind::Vertex, resource.Res, slice))
MagmaWireFatal("vertex-buffer-resource");
@@ -310,8 +320,10 @@
const SizeT stride = static_cast<SizeT>(attr.Stride);
SizeT count = stride ? 1 + (static_cast<SizeT>(slice.size - offset) - elementSize) / stride : 1;
// Direct non-indexed draws bound the conversion; indexed/multi-draw may
// name arbitrary vertices, so preserve the entire valid source stream.
if (stride && !(aspects & DrawSetupAspect::IndexBuffer) && !resource.Divisor)
// name arbitrary vertices, so preserve the entire valid source stream. So may an
// indirect draw (P8-D): its vertex range is in GPU words nobody read.
if (stride && !(aspects & DrawSetupAspect::IndexBuffer) &&
!(aspects & DrawSetupAspect::IndirectDrawBuffer) && !resource.Divisor)
count = std::min<SizeT>(count, static_cast<SizeT>(drawParams.firstVertex) + drawParams.vertexCount);
if (!count) {
MGL_WIRE_DECLINE_AT(VertexStreamCountZero, "the converted vertex stream would hold no elements");
@@ -601,6 +613,136 @@
return true;
}
// ---- P8-D: THE WIRE ARM'S INDIRECT DRAWS READ THEIR WORDS ON THE GPU ----------------------
//
// The shape the monolith arm records (VulkanRenderer.cpp's MultiDraw{Elements,Arrays}Indirect
// and MultiDrawElementsIndirectCount), with the wire store's own VkBuffer in place of the
// frontend buffer's resident slice. Before this, every wire indirect draw read its commands
// through ReadWireBuffer and expanded them on the CPU, and a store a shader had written made
// that read a whole-GPU wait (WaitForWireBufferHostAccess) per draw.
//
// THE ONE FALSE: a COUNT form this device cannot issue - the monolith arm's own condition for
// vkCmdDrawIndexedIndirectCount (MultiDrawElementsIndirectCount), extended to the non-indexed
// twin the monolith never loads. Everything else is issued here or declined by name.
Bool VulkanRenderer::DrawWireIndirectNative(GLenum mode, GLenum type, Uint64 offset, GLsizei drawcount,
GLsizei stride, Bool indexed, Bool counted, Uint64 countOffset) {
if (drawcount <= 0) return true;
if (counted) {
const auto countEntry = indexed ? s_vkCmdDrawIndexedIndirectCount : s_vkCmdWireDrawIndirectCount;
if (!m_drawIndirectCountExtensionEnabled || countEntry == nullptr ||
(!m_multiDrawIndirectFeatureEnabled && drawcount != 1))
return false;
}
const auto& state = MG_Pipe::MGPipeApplier();
// GL's command sizes (GL 4.6 10.3.10), which are also Vulkan's.
const Uint64 commandSize = indexed ? 5 * sizeof(Uint32) : 4 * sizeof(Uint32);
if (stride == 0) stride = static_cast<GLsizei>(commandSize);
BufferSlice commands{};
if (MG_Pipe::MGPipeHandleIsNull(state.VerbIndirectBuffer) ||
!m_bufferManager.AcquireWireSlice(BufferKind::Indirect, state.VerbIndirectBuffer, commands)) {
MGL_WIRE_DECLINE_AT(IndirectBufferUnbound, "an indirect draw names no live indirect buffer store");
return true;
}
const Uint64 span = static_cast<Uint64>(stride) * static_cast<Uint64>(drawcount - 1) + commandSize;
// GL refuses a stride that is not a multiple of four (INVALID_VALUE), and Vulkan requires
// it for drawCount > 1; a peer's record that breaks either is declined, not issued.
if (stride < static_cast<GLsizei>(commandSize) || (stride & 3) != 0 || (offset & 3u) != 0 ||
offset > commands.size || span > commands.size - offset) {
MGL_WIRE_DECLINE_AT(IndirectCommandRange, "the indirect commands lie outside their store or are misaligned");
return true;
}
BufferSlice parameters{};
if (counted) {
if (MG_Pipe::MGPipeHandleIsNull(state.VerbIndirectParameterBuffer) ||
!m_bufferManager.AcquireWireSlice(BufferKind::Indirect, state.VerbIndirectParameterBuffer, parameters) ||
(countOffset & 3u) != 0 || countOffset > parameters.size || parameters.size - countOffset < sizeof(Uint32)) {
MGL_WIRE_DECLINE_AT(IndirectCountRange, "the indirect draw count lies outside its parameter store");
return true;
}
}
// The index view is the WHOLE bound element store past its binding offset, as the monolith
// arm binds it: the commands name their own firstIndex, which only the GPU reads.
IndexBufferView indices{};
DrawCmdParam vertexRange{};
vertexRange.instanceCount = 1;
Flags<DrawSetupAspect> aspects = DrawSetupAspect::IndirectDrawBuffer;
if (indexed) {
const SizeT width = MG_Util::GetGLTypeSize(type);
BufferSlice elements{};
if (width == 0 || MG_Pipe::MGPipeHandleIsNull(state.IndexBuffer.Res) ||
!m_bufferManager.AcquireWireSlice(BufferKind::Index, state.IndexBuffer.Res, elements) ||
state.IndexBuffer.Offset > elements.size) {
MGL_WIRE_DECLINE_AT(IndirectIndexStore, "an indexed indirect draw names no usable element store");
return true;
}
indices.indexType = type;
indices.indexByteOffset = 0;
indices.indexByteSize = static_cast<SizeT>(elements.size - state.IndexBuffer.Offset);
vertexRange.vertexCount = static_cast<Uint32>(indices.indexByteSize / width);
aspects |= DrawSetupAspect::IndexBuffer;
}
// A shader write the store was marked with (MarkWireBufferGpuWritten) is made visible to
// the indirect read here, once per write, so a program that omits GL_COMMAND_BARRIER_BIT
// is not left racing a read the old CPU path used to take after a whole-GPU wait. The
// application's own glMemoryBarrier is BuildMemoryBarrierForGlBarriers, as on the monolith
// arm; transfer writes carry their own MEMORY_READ after-barrier (VkBufferManager.cpp).
const Bool commandsWritten = m_bufferManager.TakeWireIndirectReadBarrier(state.VerbIndirectBuffer);
const Bool countWritten = counted && m_bufferManager.TakeWireIndirectReadBarrier(state.VerbIndirectParameterBuffer);
if (commandsWritten || countWritten) {
RetireWireDrawPass();
auto& barrierFrame = m_frameContext.GetCurrent();
if (!barrierFrame.isCommandRecording) m_frameContext.BeginCommandRecording();
if (VkRenderPassManager::GetActiveRenderPass() != nullptr)
VkRenderPassManager::EndRenderPass(barrierFrame.commandBuffer);
VkMemoryBarrier barrier{VK_STRUCTURE_TYPE_MEMORY_BARRIER};
barrier.srcAccessMask = VK_ACCESS_MEMORY_WRITE_BIT;
barrier.dstAccessMask = VK_ACCESS_INDIRECT_COMMAND_READ_BIT;
vkCmdPipelineBarrier(barrierFrame.commandBuffer, VK_PIPELINE_STAGE_ALL_COMMANDS_BIT,
VK_PIPELINE_STAGE_DRAW_INDIRECT_BIT, 0, 1, &barrier, 0, nullptr, 0, nullptr);
if (MG_Util::PipeStats::Enabled())
MG_Util::PipeStats::AddCalls(MG_Util::PipeStats::CallClass::WireIndirectBarriers, 1);
}
auto& frame = m_frameContext.GetCurrent();
m_wireNativeIndirectDraw = true;
const Bool ready = SetupDraw(frame, mode, aspects, vertexRange, indexed ? &indices : nullptr);
m_wireNativeIndirectDraw = false;
if (!ready) return true;
MOBILEGL_ASSERT(frame.isCommandRecording, "%s: frame recording was not started", __func__);
const VkCommandBuffer commandBuffer = frame.commandBuffer;
const VkDeviceSize first = commands.offset + offset;
const Uint32 count = static_cast<Uint32>(drawcount);
const Uint32 byteStride = static_cast<Uint32>(stride);
if (counted) {
const auto countEntry = indexed ? s_vkCmdDrawIndexedIndirectCount : s_vkCmdWireDrawIndirectCount;
countEntry(commandBuffer, commands.buffer, first, parameters.buffer, parameters.offset + countOffset,
count, byteStride);
} else if (count == 1 || (!m_multiDrawForceUnrollIndirect && m_multiDrawIndirectFeatureEnabled)) {
// maxDrawIndirectCount bounds one call; GL's drawcount does not.
const Uint32 limit = std::max<Uint32>(1u, m_physicalDevice.properties.limits.maxDrawIndirectCount);
for (Uint32 base = 0; base < count; base += limit) {
const Uint32 chunk = std::min(count - base, limit);
const VkDeviceSize at = first + static_cast<VkDeviceSize>(base) * byteStride;
if (indexed) vkCmdDrawIndexedIndirect(commandBuffer, commands.buffer, at, chunk, byteStride);
else vkCmdDrawIndirect(commandBuffer, commands.buffer, at, chunk, byteStride);
}
} else {
// No multiDrawIndirect: one indirect draw per command, still read by the GPU
// (the monolith arm's own fallback).
for (Uint32 i = 0; i < count; ++i) {
const VkDeviceSize at = first + static_cast<VkDeviceSize>(i) * byteStride;
if (indexed) vkCmdDrawIndexedIndirect(commandBuffer, commands.buffer, at, 1, 0);
else vkCmdDrawIndirect(commandBuffer, commands.buffer, at, 1, 0);
}
}
if (MG_Util::PipeStats::Enabled())
MG_Util::PipeStats::AddCalls(MG_Util::PipeStats::CallClass::WireIndirectNativeDraws, 1);
return true;
}
void VulkanRenderer::DispatchWireCompute(GLuint x, GLuint y, GLuint z) {
RetireWireDrawPass();
auto& frame = m_frameContext.GetCurrent();
@@ -628,4 +770,80 @@
vkCmdBindPipeline(frame.commandBuffer, VK_PIPELINE_BIND_POINT_COMPUTE, pipeline);
vkCmdDispatch(frame.commandBuffer, x, y, z);
}
// ---- P8-SV: THE WIRE ARM'S INDIRECT DISPATCH READS ITS GROUP COUNTS ON THE GPU ----------
//
// The monolith arm's shape (VulkanRenderer.cpp's DispatchComputeIndirect) with the wire
// store's own VkBuffer in place of the frontend buffer's resident slice. Before this the wire
// arm read the three words through ReadWireBuffer and dispatched them from the CPU, and a store
// a shader had written made that read a whole-GPU wait (WaitForWireBufferHostAccess) per call.
// There is no shape the device cannot issue, so there is no CPU path left: a record whose
// store or words are unusable is declined by name.
//
// The barrier is P8-D's draw rule (DrawWireIndirectNative): a store marked shader-written gets
// one MEMORY_WRITE -> INDIRECT_COMMAND_READ barrier before its next indirect read, whether or
// not the application issued GL_COMMAND_BARRIER_BIT; DRAW_INDIRECT is the stage that consumes
// VkDispatchIndirectCommand too. It is TAKEN before the bindings below, because binding this
// dispatch's own storage buffers marks them written, and a store that is both this dispatch's
// parameters and one of its outputs must keep that mark for the next reader.
void VulkanRenderer::DispatchWireComputeIndirect(GLintptr offset) {
RetireWireDrawPass();
const auto& state = MG_Pipe::MGPipeApplier();
constexpr Uint64 kCommandSize = 3 * sizeof(Uint32); // GL 4.6 10.3.10 / VkDispatchIndirectCommand
BufferSlice parameters{};
if (MG_Pipe::MGPipeHandleIsNull(state.VerbDispatchIndirectBuffer) ||
!m_bufferManager.AcquireWireSlice(BufferKind::Indirect, state.VerbDispatchIndirectBuffer, parameters)) {
MGL_WIRE_DECLINE_AT(DispatchIndirectBufferUnbound, "an indirect dispatch names no live dispatch-indirect store");
return;
}
// GL refuses an offset that is negative or not a multiple of four (INVALID_VALUE) and a
// command past the end of the buffer (INVALID_OPERATION); a peer's record that breaks
// either is declined, not issued.
if (offset < 0 || (static_cast<Uint64>(offset) & 3u) != 0 || static_cast<Uint64>(offset) > parameters.size ||
parameters.size - static_cast<Uint64>(offset) < kCommandSize) {
MGL_WIRE_DECLINE_AT(DispatchIndirectRange, "the dispatch group counts lie outside their store or are misaligned");
return;
}
auto& frame = m_frameContext.GetCurrent();
if (m_bufferManager.TakeWireIndirectReadBarrier(state.VerbDispatchIndirectBuffer)) {
if (!frame.isCommandRecording) m_frameContext.BeginCommandRecording();
if (VkRenderPassManager::GetActiveRenderPass() != nullptr)
VkRenderPassManager::EndRenderPass(frame.commandBuffer);
VkMemoryBarrier barrier{VK_STRUCTURE_TYPE_MEMORY_BARRIER};
barrier.srcAccessMask = VK_ACCESS_MEMORY_WRITE_BIT;
barrier.dstAccessMask = VK_ACCESS_INDIRECT_COMMAND_READ_BIT;
vkCmdPipelineBarrier(frame.commandBuffer, VK_PIPELINE_STAGE_ALL_COMMANDS_BIT,
VK_PIPELINE_STAGE_DRAW_INDIRECT_BIT, 0, 1, &barrier, 0, nullptr, 0, nullptr);
if (MG_Util::PipeStats::Enabled())
MG_Util::PipeStats::AddCalls(MG_Util::PipeStats::CallClass::WireIndirectBarriers, 1);
}
const auto program = WireProgramSource(state.DispatchProgram);
if (!program.GetLinkStatus() || !program.GetSpirvStatus()) {
MGL_WIRE_DECLINE_AT(ComputeProgramLinkOrSpirv, "the compute program is not linked or carries no SPIR-V");
return;
}
const auto& programObj = m_programFactory->GetOrCreateProgram(program, {});
if (!m_uniformManager->PrepareWireTextureResources(program, programObj)) {
MGL_WIRE_DECLINE_AT(ComputeTextureResourcesUnprepared, "a sampled/storage image could not be made ready for the dispatch");
return;
}
const auto pipeline = GetOrCreateComputePipeline(programObj);
if (!pipeline) {
MGL_WIRE_DECLINE_AT(ComputePipelineUnavailable, "no compute pipeline could be created for this dispatch");
return;
}
// PrepareWireTextureResources may have flushed and begun a new recording.
auto& dispatchFrame = m_frameContext.GetCurrent();
if (!dispatchFrame.isCommandRecording) m_frameContext.BeginCommandRecording();
if (!m_uniformManager->BindProgramUniformBuffers(dispatchFrame.commandBuffer, program, programObj,
m_frameContext.GetCurrentFrameIndex(), VK_PIPELINE_BIND_POINT_COMPUTE)) {
MGL_WIRE_DECLINE_AT(ComputeUniformBufferBinding, "the dispatch's uniform/storage bindings could not be bound");
return;
}
vkCmdBindPipeline(dispatchFrame.commandBuffer, VK_PIPELINE_BIND_POINT_COMPUTE, pipeline);
vkCmdDispatchIndirect(dispatchFrame.commandBuffer, parameters.buffer,
parameters.offset + static_cast<VkDeviceSize>(offset));
if (MG_Util::PipeStats::Enabled())
MG_Util::PipeStats::AddCalls(MG_Util::PipeStats::CallClass::WireIndirectNativeDispatches, 1);
}
#endif
@@ -20,6 +20,8 @@
// missing WHICH feature bit - was the one thing it did not. The census, the retrace
// refusal tally and every log grep key on the leading word, so it is unchanged and
// unprefixed; everything after the first space is for the reader of the next device run.
// P8-F: the refusal is a named decline now (WireDeclines.def MipmapShaderFormatOrShape);
// the line it logs keeps this word and this detail.
static String MagmaWireShapeDetail(const char* name, VkFormat format, VkFormat storageFormat,
const VkFormatProperties& props, VkImageAspectFlags aspect,
VkImageViewType viewType, VkImageUsageFlags usage,
@@ -1392,16 +1394,22 @@
// release build both arms ship as. Nothing in GL promises a stencil mip either - the
// pass writes gl_FragDepth and would leave the stencil aspect of the level it produced
// undefined, which is worse than leaving the whole level alone and saying so.
//
// P8-SV: a NAMED decline (WireDeclines.def MipmapDepthStencilAspect), so MGWIRE-DECLINES
// counts it like every other refusal on this arm; the line keeps its words.
if (resource->aspect != VK_IMAGE_ASPECT_COLOR_BIT && resource->aspect != VK_IMAGE_ASPECT_DEPTH_BIT) {
MGLOG_E_ONCE("DirectVulkan::GenerateWireMipmap: aspect 0x%x (a combined depth/stencil or "
"stencil-only texture, format %d) has no mip generation on either arm; "
"declining and leaving levels %u..%u as they are",
static_cast<Uint32>(resource->aspect), static_cast<Int>(resource->format),
base + 1, base + count - 1);
MGL_WIRE_DECLINE_AT(MipmapDepthStencilAspect,
"DirectVulkan::GenerateWireMipmap: aspect 0x%x (a combined depth/stencil or "
"stencil-only texture, format %d) has no mip generation on either arm; "
"declining and leaving levels %u..%u as they are",
static_cast<Uint32>(resource->aspect), static_cast<Int>(resource->format),
base + 1, base + count - 1);
return;
}
if (!m_textureManager->GrowWireTextureMipChain(handle,base+count,frame.commandBuffer))
MagmaWireFatal("mipmap-growth");
// P8-F: WHICH ARM TAKES THIS CHAIN IS DECIDED BEFORE THE CHAIN IS GROWN, for the
// depth-stencil decline's reason above. Nothing here depends on the growth: the format,
// its features, the knob tier, the aspect, view type, usage and layer window are the
// same before and after it (GrowWireTextureMipChain carries every one of them over).
VkFormatProperties props{};
const VkFormat format = viewFormat == VK_FORMAT_UNDEFINED ? resource->format : viewFormat;
vkGetPhysicalDeviceFormatProperties(m_physicalDevice.handle,format,&props);
@@ -1411,6 +1419,55 @@
(props.optimalTilingFeatures & required) == required;
const VkFilter filter = props.optimalTilingFeatures & VK_FORMAT_FEATURE_SAMPLED_IMAGE_FILTER_LINEAR_BIT ?
VK_FILTER_LINEAR : VK_FILTER_NEAREST;
const Bool layersInRange = baseLayer < resource->arrayLayers &&
layerCount <= resource->arrayLayers - baseLayer;
// Both shader arms render a 2D view of one layer of one level, so a 1D, 1D-array or 3D
// image is a shape neither takes.
const Bool planar = resource->viewType != VK_IMAGE_VIEW_TYPE_1D &&
resource->viewType != VK_IMAGE_VIEW_TYPE_1D_ARRAY &&
resource->viewType != VK_IMAGE_VIEW_TYPE_3D;
// The depth arm's shape: the layer window, and what WireDepthMipmap.inc's pass refuses
// for an image this function hands it (the view type is passed to it below) - a 1D,
// 1D-array or 3D view type, and a format that does not both sample and attach as depth.
// Its other refusals, multisample and a default image, are never built here. P8-SV: the
// view type half is new; before it a 1D depth chain reached the pass, which made a 2D
// view of the 1D image.
const auto depthPassFeatures =
VK_FORMAT_FEATURE_SAMPLED_IMAGE_BIT | VK_FORMAT_FEATURE_DEPTH_STENCIL_ATTACHMENT_BIT;
const Bool depthArmTakesShape = planar &&
layersInRange && (props.optimalTilingFeatures & depthPassFeatures) == depthPassFeatures;
// The colour arm's shape (the two shader shapes are explained at the colour arm below).
const Bool colour = resource->aspect == VK_IMAGE_ASPECT_COLOR_BIT;
// The shader arm samples through a sampler2D and writes a vec4, which is what
// BlitWireColorImage's own float check enforces; naming it here rather than letting the
// blit return false is what puts the reason in the decline line.
const Bool sampledFloat = vkuFormatIsSampledFloat(format);
const Bool renderableFormat =
(props.optimalTilingFeatures & VK_FORMAT_FEATURE_COLOR_ATTACHMENT_BIT) != 0;
const Bool attachable = forcedShaderTier < 2 &&
(resource->usageFlags & VK_IMAGE_USAGE_COLOR_ATTACHMENT_BIT) != 0;
const Bool viaScratch = !attachable && renderableFormat &&
(resource->usageFlags & VK_IMAGE_USAGE_TRANSFER_DST_BIT) != 0;
const Bool colourArmTakesShape =
colour && planar && layersInRange && sampledFloat && (attachable || viaScratch);
// P8-F: `mipmap-shader-format-or-shape` IS A NAMED DECLINE, NOT A FATAL (rule I (a)).
// The monolith arm has always answered this chain with a decline (VulkanRenderer.cpp's
// GenerateMipmap: a format without blit support is skipped and the chain left unwritten),
// and a shape no shader arm takes is a coverage gap in this backend, not a broken record.
// The leading word stays, so every device-log grep keyed on it still finds the line.
const Bool shaderArmTakesShape =
resource->aspect == VK_IMAGE_ASPECT_DEPTH_BIT ? depthArmTakesShape : colourArmTakesShape;
if (!nativeBlit && !shaderArmTakesShape) {
const String detail = MagmaWireShapeDetail("mipmap-shader-format-or-shape", format,
resource->format, props, resource->aspect, resource->viewType, resource->usageFlags,
resource->extent, resource->depth, resource->mipLevels, baseLayer, layerCount,
resource->arrayLayers);
MGL_WIRE_DECLINE_AT(MipmapShaderFormatOrShape, "%s; no mip arm takes this shape, levels %u..%u left as they are",
detail.c_str(), base + 1, base + count - 1);
return;
}
if (!m_textureManager->GrowWireTextureMipChain(handle,base+count,frame.commandBuffer))
MagmaWireFatal("mipmap-growth");
if (!nativeBlit && resource->aspect == VK_IMAGE_ASPECT_DEPTH_BIT) {
// P7 wave 2-B, CONTRACT-P7 §5.1 (A): THE DEPTH ARM, and it is first because a
// depth format without BLIT_DST is the ordinary case, not the exotic one -
@@ -1422,18 +1479,17 @@
// The per-level pass is WireDepthMipmap.inc's, which is the monolith's fragment
// shader with the monolith's arithmetic on a baked pipeline that owns no frontend
// object - §3.2's "observably the monolith's" without the GL program the monolith
// needs to get there.
if (baseLayer >= resource->arrayLayers || layerCount > resource->arrayLayers - baseLayer)
MagmaWireFatal(MagmaWireShapeDetail("mipmap-shader-format-or-shape", format,
resource->format, props, resource->aspect, resource->viewType,
resource->usageFlags, resource->extent, resource->depth, resource->mipLevels,
baseLayer, layerCount, resource->arrayLayers).c_str());
// needs to get there. Its layer window and format were checked before the growth.
for (Uint32 level = base + 1; level < base + count; ++level) {
for (Uint32 layer = baseLayer; layer < baseLayer + layerCount; ++layer) {
WireImage source{};
source.image = resource->image;
source.format = format;
source.aspect = VK_IMAGE_ASPECT_DEPTH_BIT;
// P8-SV: the image's own view type, so the pass can refuse a shape it cannot
// render (it makes 2D views) instead of receiving the struct's 2D default.
source.viewType = resource->viewType;
source.is3D = resource->viewType == VK_IMAGE_VIEW_TYPE_3D;
source.layout = resource->layout;
source.trackedLayout = &resource->layout;
source.levels = resource->mipLevels;
@@ -1445,15 +1501,19 @@
destination.level = level;
destination.extent = {std::max(resource->extent.width >> level, 1u),
std::max(resource->extent.height >> level, 1u)};
// The pass reports the SHAPE it cannot take (1D, 3D, multisample, a
// format without sampled or depth-attachment support), so the refusal
// keeps its name and gains the reason.
if (!GenerateWireDepthMipLevel(source, destination))
MagmaWireFatal(MagmaWireShapeDetail("mipmap-shader-format-or-shape", format,
// The pass reports the SHAPE it cannot take. depthArmTakesShape above asks
// its questions first, so this is reached only if the two lists drift apart
// - and then it is the same named decline (P8-F), not a session's end. It
// can only refuse the first level: the shape does not change down the chain.
if (!GenerateWireDepthMipLevel(source, destination)) {
const String detail = MagmaWireShapeDetail("mipmap-shader-format-or-shape", format,
resource->format, props, resource->aspect, resource->viewType,
resource->usageFlags, resource->extent, resource->depth,
resource->mipLevels, baseLayer, layerCount,
resource->arrayLayers).c_str());
resource->mipLevels, baseLayer, layerCount, resource->arrayLayers);
MGL_WIRE_DECLINE_AT(MipmapShaderFormatOrShape, "%s; the depth pass refused level %u",
detail.c_str(), level);
return;
}
}
}
} else if (!nativeBlit) {
@@ -1470,27 +1530,9 @@
// cannot, an owned scratch attachment of the destination level's size renders
// the mip and vkCmdCopyImage moves it in: a raw texel move, no conversion, and
// the texels still come from GPU contents rather than from a staged shadow.
const Bool colour = resource->aspect == VK_IMAGE_ASPECT_COLOR_BIT;
const Bool planar = resource->viewType != VK_IMAGE_VIEW_TYPE_1D &&
resource->viewType != VK_IMAGE_VIEW_TYPE_1D_ARRAY &&
resource->viewType != VK_IMAGE_VIEW_TYPE_3D;
const Bool layersInRange = baseLayer < resource->arrayLayers &&
layerCount <= resource->arrayLayers - baseLayer;
// The shader arm samples through a sampler2D and writes a vec4, which is what
// BlitWireColorImage's own float check enforces; naming it here rather than
// letting the blit return false is what puts the reason in the fatal line.
const Bool sampledFloat = vkuFormatIsSampledFloat(format);
const Bool renderableFormat =
(props.optimalTilingFeatures & VK_FORMAT_FEATURE_COLOR_ATTACHMENT_BIT) != 0;
const Bool attachable = forcedShaderTier < 2 &&
(resource->usageFlags & VK_IMAGE_USAGE_COLOR_ATTACHMENT_BIT) != 0;
const Bool viaScratch = !attachable && renderableFormat &&
(resource->usageFlags & VK_IMAGE_USAGE_TRANSFER_DST_BIT) != 0;
if (!colour || !planar || !layersInRange || !sampledFloat || (!attachable && !viaScratch))
MagmaWireFatal(MagmaWireShapeDetail("mipmap-shader-format-or-shape", format,
resource->format, props, resource->aspect, resource->viewType,
resource->usageFlags, resource->extent, resource->depth, resource->mipLevels,
baseLayer, layerCount, resource->arrayLayers).c_str());
//
// Which of the two (`attachable` or `viaScratch`), and whether either takes the
// shape at all, was decided before the growth (colourArmTakesShape, P8-F).
for (Uint32 level = base + 1; level < base + count; ++level) {
for (Uint32 layer = baseLayer; layer < baseLayer + layerCount; ++layer) {
WireImage source{};
@@ -1574,14 +1616,24 @@
if (!VkTextureManager::TransitionImageLayout(frame.commandBuffer,resource->image,resource->layout,
VK_IMAGE_LAYOUT_TRANSFER_DST_OPTIMAL,stage,VK_PIPELINE_STAGE_TRANSFER_BIT,access,
VK_ACCESS_TRANSFER_WRITE_BIT,resource->aspect,0,resource->mipLevels)) MagmaWireFatal("mipmap-transition");
// P8-SV: A 3D IMAGE HAS ONE ARRAY LAYER, AND ITS DEPTH IS THE Z EXTENT OF THE BLIT.
// ResolveWireTextureStorage answers `layerCount` in slices for Tex3D (Depth >> level,
// VkTextureManager.cpp) - the addressing attachments, descriptors and readback use - and
// passing that on as the blit's subresource layerCount names layers the image (created
// with arrayLayers = 1) does not have (VUID-vkCmdBlitImage-srcImage-00240,
// -srcSubresource-01707, -dstSubresource-01708). The monolith arm blits layer 0, count 1,
// with the z offsets below halving the depth level by level; so does this one now.
const Bool volume = resource->viewType == VK_IMAGE_VIEW_TYPE_3D;
const Uint32 blitBaseLayer = volume ? 0u : baseLayer;
const Uint32 blitLayerCount = volume ? 1u : layerCount;
for (Uint32 level=base+1;level<base+count;++level) {
VkImageLayout previous = VK_IMAGE_LAYOUT_TRANSFER_DST_OPTIMAL;
VkTextureManager::TransitionImageLayout(frame.commandBuffer,resource->image,previous,
VK_IMAGE_LAYOUT_TRANSFER_SRC_OPTIMAL,VK_PIPELINE_STAGE_TRANSFER_BIT,VK_PIPELINE_STAGE_TRANSFER_BIT,
VK_ACCESS_TRANSFER_WRITE_BIT,VK_ACCESS_TRANSFER_READ_BIT,resource->aspect,level-1,1);
VkImageBlit blit{};
blit.srcSubresource = {resource->aspect,level-1,baseLayer,layerCount};
blit.dstSubresource = {resource->aspect,level,baseLayer,layerCount};
blit.srcSubresource = {resource->aspect,level-1,blitBaseLayer,blitLayerCount};
blit.dstSubresource = {resource->aspect,level,blitBaseLayer,blitLayerCount};
blit.srcOffsets[1] = {static_cast<Int32>(std::max(resource->extent.width>>(level-1),1u)),
static_cast<Int32>(std::max(resource->extent.height>>(level-1),1u)),
static_cast<Int32>(std::max(resource->depth>>(level-1),1u))};
@@ -22,6 +22,9 @@ namespace MobileGL::MG_Backend::DirectVulkan {
Uint64 CacheVersion = MG_Config::CacheVersion;
Uint32 SurfaceWidth = 1;
Uint32 SurfaceHeight = 1;
// The app's eglSwapInterval; empty until it asks. Window renderers only - see
// VulkanRenderer::SetSwapInterval.
Optional<Int> SwapInterval;
Bool DisablePipelineCache = false;
#if MOBILEGL_LOG_ACTIVE_LEVEL <= MOBILEGL_LOG_LEVEL_DEBUG
Bool EnableValidationLayers = true;
+7 -6
View File
@@ -81,9 +81,10 @@ namespace MobileGL::MG_Backend {
// DirectGLES consumes all thirteen migrated families (P2's 0..6, P3a's 7..8, P4a's
// 9..12): it registers the resource op table in Initialize()
// (BackendObject_DirectGLES.cpp:849) and reads every other family out of gPipeInputs.
// DirectVulkan registers NO resource ops - MGPipeSetResourceOps has exactly one caller
// in the whole tree and it is Managers.cpp:2594 - so bit 7 is CLEAR for it, which is
// the same fact ObjectSubsystemControlScenario already pins from the client side.
// DirectVulkan registers its wire resource op table off monolith only
// (VkBufferManager::RegisterWireResourceOps, VkBufferManager.cpp): under a transport it
// owns buffer stores and claims bit 7 (the P4a mask, see ConsumedSubsystemsFor below);
// under monolith it registers none and its buffers keep the legacy BufferBackendOps path.
// P5e (MG_Remote/CONTRACT-P5E.md §6.2). THE ONE CONSTANT THE WHOLE PHASE HANGS ON.
//
// kCapRunAheadApply says "this server applies an unbarriered record without reading
@@ -185,9 +186,9 @@ namespace MobileGL::MG_Backend {
// ZERO IS THE EXPLICIT ANSWER FOR P5, not an omission (ServerSession.h's block):
// every optional capability bit belongs to the package that owns its question, and
// withholding one leaves the legacy path running, which is the safe direction.
// kCapNeedsHostIndexBytes and kCapNeedsHostUboBytes must be 0 for the whole of P5
// by ruling - they are the only two things that ask for an MGHostSpan, and 0 is
// what keeps every one of them out of the first IPC frame (contract table 0).
// kCapNeedsHostIndexBytes, kCapNeedsHostUboBytes and kCapViewportArray are RESERVED
// (P8-F) and never set here: the first two are the only things that ask for an
// MGHostSpan, and 0 keeps every one of them off the wire (contract table 0).
//
// P5b t2 (CONTRACT-P5B.md §6.5) PUBLISHES THE ONE BIT P5b ADDS, and this is the
// only place that can: the question kCapBackendOwnsXfbCapture answers is "does the
+6 -5
View File
@@ -339,6 +339,8 @@ namespace MobileGL::MG_Pipe {
inputs.m_live = live;
inputs.m_contextIdentity = identity;
}
static void SetServerContextLive(PipeInputs& inputs, Bool live) { inputs.m_serverContextLive = live; }
static Bool ServerContextLive(const PipeInputs& inputs) { return inputs.m_serverContextLive; }
};
// ---- P5f (f1): the dual-block rehearsal's selection functions (PipeInputs.h) -----------
@@ -397,14 +399,13 @@ namespace MobileGL::MG_Pipe {
MGPipeStampAccess::SetServerStamped(MGPipeClientInputs(), false);
}
namespace { Bool g_serverContextLive = false; }
// The calling thread's block, so the calling session's (PipeInputs.h's m_serverContextLive).
void MGPipeServerSetContextLive(Bool live) {
g_serverContextLive = live;
MGPipeStampAccess::SetServerContextLive(gPipeInputs, live);
MGPipeServerBlockNoteIdentity();
}
Bool MGPipeServerContextIsLive() { return g_serverContextLive; }
Bool MGPipeServerContextIsLive() { return MGPipeStampAccess::ServerContextLive(gPipeInputs); }
#if MOBILEGL_BUILD_DISAGGREGATED
namespace { const void* g_serverOwnedWindow = nullptr; }
@@ -433,7 +434,7 @@ namespace MobileGL::MG_Pipe {
// hands out a null slot - an unnamed crash where the rehearsal exists to produce a
// named one.
const Uint64 serial = MGPipeApplierContextSerial();
MGPipeStampAccess::SetIdentity(gPipeInputs, g_serverContextLive,
MGPipeStampAccess::SetIdentity(gPipeInputs, MGPipeServerContextIsLive(),
reinterpret_cast<const void*>(static_cast<std::uintptr_t>(
(serial << 1) | Uint64{1})));
}
+52 -1
View File
@@ -748,6 +748,12 @@ namespace MobileGL::MG_Pipe {
// ---- identity ----
const void* m_contextIdentity = nullptr;
Bool m_live = false;
// MGPipeServerSetContextLive's flag. In the block, not in a process global, because the
// in-process display server runs several sessions at once and each has its own served
// context: one session's teardown or eglMakeCurrent(NO_CONTEXT) must not mark a
// neighbour's context dead mid-frame (P14; the per-thread block resolution below is
// what makes this the calling session's).
Bool m_serverContextLive = false;
MGPipeVerb m_currentVerb = MGPipeVerb::kVerbCount;
#if MOBILEGL_PIPE_POISON
MGPipeFilledState m_filled{};
@@ -820,6 +826,10 @@ namespace MobileGL::MG_Pipe {
// The single global the backends read through MGB_CTX (ARCHITECTURE.md 9.2). An inline
// variable: no .cpp is needed for the definition.
//
// P14 S2: IT IS THE PROCESS-WIDE BLOCK, and `gPipeInputs` below is the name every reader
// spells - now one reference per THREAD, resolving to a served session's own block when the
// calling thread belongs to one. See the block after it.
//
// LEAK-AT-EXIT STORAGE, and it is the same rule Init.cpp and GlobalObjects.cpp state for
// pGLContext and pActiveBackendObject: "a process that exits without eglTerminate simply
// leaks the global singletons to the OS instead of running destructors during static
@@ -834,7 +844,48 @@ namespace MobileGL::MG_Pipe {
// about to be. So the reference is never dropped: nothing here can start such a chain.
// A live context releases these SharedPtrs the ordinary way, at the fill point.
// (P3a; the exit-time heap corruption this closes is p3a-results/exit-order-v1.md.)
inline PipeInputs& gPipeInputs = *new PipeInputs();
inline PipeInputs& gPipeInputsDefault = *new PipeInputs();
// ============================================================================
// P14 S2: WHOSE BLOCK IS `gPipeInputs`?
// ============================================================================
//
// ONE PROCESS, SEVERAL SERVED SESSIONS. The applier writes this block and the backend reads it
// through MGB_CTX, both on the SAME apply thread - the block IS "the state the record in hand
// carries". Two sessions applying concurrently on two apply threads through one block would
// read each other's render state, which is the cross-talk a multi-session server must not
// have. Nothing about the block's contents changed; what changed is that the name resolves to
// the SESSION'S block on a thread that belongs to one.
//
// WHY A THREAD-LOCAL REFERENCE, AND NOT SOMETHING CLEVERER. MGB_CTX is
// `(&::MobileGL::MG_Pipe::gPipeInputs)` (MG_Pipe/PipeInputsSwitch.h) and is spelled at ~380
// sites in MG_Backend/MG_Impl; a reference whose TARGET varies with the calling thread is the
// only shape that keeps all of them compiling and all of them correct. The cost is one TLS
// address computation per access site - not one per field, and with no guard: the reference is
// bound once per thread on its first use.
//
// THE RESOLVER IS A HOOK because this header may not know about sessions (MG_Remote/Server
// owns them and includes THIS header, not the other way round). It is installed once by the
// server's session registry and never replaced; null - the client process, the pull build, a
// unit case - makes every thread resolve to gPipeInputsDefault, which is the old behaviour
// byte for byte.
//
// BINDING IS PER THREAD AND HAPPENS AT FIRST USE, so a thread must be inside its session's
// scope before it touches the block. Both of a session's threads are: the control thread opens
// its scope before it reads a byte of the Hello, and the apply thread opens it as its first
// act (ServerLoop::ApplyThreadMain).
using PipeInputsThreadResolver = PipeInputs* (*)();
inline PipeInputsThreadResolver g_pipeInputsThreadResolver = nullptr;
inline PipeInputs& MGPipeServerInputsForCallingThread() {
const PipeInputsThreadResolver resolve = g_pipeInputsThreadResolver;
if (resolve != nullptr) {
if (PipeInputs* const block = resolve()) return *block;
}
return gPipeInputsDefault;
}
inline thread_local PipeInputs& gPipeInputs = MGPipeServerInputsForCallingThread();
#if MOBILEGL_BUILD_DISAGGREGATED
// ============================================================================
+321 -15
View File
@@ -7,6 +7,8 @@
// End of Source File Header
#include "EGLImpl.h"
#include "EGLPlatformExtensions.h"
#include "WaylandWindow.h"
#include "../GetProcAddress.h"
#include <Init.h>
#include <MG_Backend/BackendObjects.h>
@@ -15,6 +17,13 @@
// MG_Config::ServerOwnedWindowSurfaces (P12, MOBILEGL_IPC_SURFACE). Split-only: the pull build's
// translation unit is unchanged (G1).
#include <Config.h>
// P14 S1 (docs/Disaggregated/design/11-state-ownership.md). The EGL context lifecycle's
// client half: eglCreateContext/eglDestroyContext cross as two control frames, and a
// context switch emits the in-band bind_context record. Reached through MG_Remote::Client
// because that is the seam MG_Impl already uses for its other wire producers
// (PipeFill.cpp's emitters) and because the frame channel belongs to the server role - this
// file must not name ServerLoop's forwarders directly.
#include <MG_Remote/Client/WireTables.h>
#endif
#include <mutex>
#include <sstream>
@@ -50,11 +59,65 @@ namespace MobileGL::MG_Impl::EGLImpl {
return backendObject;
}
#if MOBILEGL_BUILD_DISAGGREGATED
// P14 S1 (docs/Disaggregated/design/11-state-ownership.md). THE CONTEXT BINDING, AND
// THE ONE PLACE IT IS EMITTED.
//
// `bind_context` is the ring's half of eglMakeCurrent: the MakeCurrent control frame
// tells the server which native tuple to hold, and this record tells it which CONTEXT
// every record after it belongs to - the attribution the session table was built for.
//
// IT IS EMITTED AFTER THE SWITCH HAS SUCCEEDED AND BEFORE eglMakeCurrent RETURNS, and
// that is the whole ordering argument: the app thread cannot publish a record of the
// new context until this call has returned, and the ring is FIFO, so the binding is
// behind no verb of the context it names and ahead of every one that follows. Waiting
// for the first verb instead would need the emitter to know which context the tracker
// is about to prime; this needs only the token EGLState already holds.
//
// An unchanged token emits NOTHING: an identical eglMakeCurrent is a legal no-op in
// EGL and the session is already bound to that context.
//
// THE COMPARISON IS AGAINST THE BINDING LAST SENT, NOT THIS THREAD'S PREVIOUS CONTEXT
// (GLStreamScope in EGLImpl.h): the session has one current context and another thread
// may have moved it since this one last bound. The caller holds EGLOperationMutex.
thread_local Uint64 t_threadContextToken = 0;
Uint64 g_boundContextToken = 0; // guarded by EGLOperationMutex
void EmitContextBinding(EGLStateContext* state, EGLContext switched, Uint64 previousToken) {
(void)previousToken;
if (MG_Config::Transport == MG_Config::TransportMode::Monolith) return;
// The apply thread IS the server in this process; a record emitted there would
// wait on the thread that has to apply it (EmitAndWait), which is a deadlock and
// not a slow path.
if (MG_Remote::Client::RunsAsTheServerRole()) return;
t_threadContextToken = switched == EGL_NO_CONTEXT ? 0 : state->GetContextClientToken(switched);
StreamBindCallingThreadLocked();
}
#endif
std::recursive_mutex& EGLOperationMutex() {
static std::recursive_mutex mutex;
return mutex;
}
#if MOBILEGL_BUILD_DISAGGREGATED
} // namespace
Bool StreamGateActive() {
return MG_Config::Transport != MG_Config::TransportMode::Monolith &&
!MG_Remote::Client::RunsAsTheServerRole();
}
std::recursive_mutex& StreamMutex() { return EGLOperationMutex(); }
void StreamBindCallingThreadLocked() {
const Uint64 token = t_threadContextToken;
if (token == g_boundContextToken) return;
if (MG_Remote::Client::EmitBindContextRecord(token)) g_boundContextToken = token;
}
namespace {
#endif
String CurrentThreadIdString() {
std::ostringstream stream;
stream << std::this_thread::get_id();
@@ -116,6 +179,105 @@ namespace MobileGL::MG_Impl::EGLImpl {
return reinterpret_cast<void*>(static_cast<SizeT>(nativeHandle));
}
}
#if MOBILEGL_WAYLAND_WINDOWS
// Every Wayland window surface's presentation, by surface. Guarded by EGLOperationMutex,
// which every entry point that reaches it holds.
UnorderedMap<EGLSurface, UniquePtr<Wayland::WindowSurface>>& WaylandSurfaces() {
static auto* surfaces = new UnorderedMap<EGLSurface, UniquePtr<Wayland::WindowSurface>>();
return *surfaces;
}
// A WINDOW ON A WAYLAND DISPLAY. Whatever MOBILEGL_IPC_SURFACE says, the window is this
// application's own wl_surface, so neither the server's window nor the display host's frame
// is what it shows: the backend draws into a pbuffer of the window's size, and each swap
// reads the frame back and attaches it to the wl_surface (WaylandWindow.h). Answers false
// when the display is not a Wayland one; otherwise *out is the surface, or EGL_NO_SURFACE
// with the error set.
// A gbm_surface is scanout memory of a device this library does not draw with; a window
// on a GBM display is refused rather than taken for some other kind of window.
template <typename State>
Bool RefuseGbmWindow(State* state, EGLDisplay dpy) {
constexpr EGLenum kPlatformGbm = 0x31D7; // EGL_PLATFORM_GBM_KHR == EGL_PLATFORM_GBM_MESA
Uint64 nativeDisplay = 0;
EGLenum platform = EGL_NONE;
if (!state->GetDisplayNative(dpy, &nativeDisplay, &platform) || platform != kPlatformGbm) return false;
MGLOG_W_ONCE("EGL: window surfaces on a GBM display are not supported (offscreen display only)");
state->SetError(EGL_BAD_NATIVE_WINDOW);
return true;
}
template <typename State>
Bool CreateWaylandWindowSurface(State* state, EGLDisplay dpy, EGLConfig config, void* window,
Bool platformWindow, EGLSurface* out) {
Uint64 nativeDisplay = 0;
EGLenum platform = EGL_NONE;
if (!state->GetDisplayNative(dpy, &nativeDisplay, &platform) ||
!Wayland::IsWaylandDisplay(nativeDisplay, platform)) {
return false;
}
*out = EGL_NO_SURFACE;
if (!state->IsDisplayInitialized(dpy)) {
state->SetError(EGL_NOT_INITIALIZED);
return true;
}
if (!state->ValidateConfigOnDisplay(dpy, config)) {
state->SetError(EGL_BAD_CONFIG);
return true;
}
EGLint width = 0;
EGLint height = 0;
if (!Wayland::WindowSize(window, &width, &height)) {
state->SetError(EGL_BAD_NATIVE_WINDOW);
return true;
}
EGLint alpha = 0;
(void)state->GetConfigAttrib(dpy, config, EGL_ALPHA_SIZE, &alpha);
auto presentation =
Wayland::WindowSurface::Create(reinterpret_cast<void*>(static_cast<uintptr_t>(nativeDisplay)), window,
width, height, alpha > 0);
if (!presentation) {
state->SetError(EGL_BAD_NATIVE_WINDOW);
return true;
}
const EGLSurface surface =
platformWindow ? state->CreatePlatformWindowSurface(dpy, config, window, nullptr)
: state->CreateWindowSurface(
dpy, config, (NativeWindowType)(reinterpret_cast<uintptr_t>(window)), nullptr);
if (surface == EGL_NO_SURFACE) return true;
(void)state->ResizeSurface(dpy, surface, width, height);
auto* backendObject = GetBackendObject(state);
if (!backendObject || !backendObject->CreateEGLPbufferSurface(surface, width, height)) {
MGLOG_E("eglCreateWindowSurface: the backend refused the %dx%d drawable behind a Wayland window",
width, height);
state->DestroySurface(dpy, surface);
state->SetError(EGL_BAD_ALLOC);
return true;
}
WaylandSurfaces()[surface] = std::move(presentation);
*out = surface;
return true;
}
// A WAYLAND WINDOW THE APPLICATION RESIZED (wl_egl_window_resize). As with a driver's own
// window surface, the new size takes effect at the next make-current or swap: the EGL
// surface reports it, the wl_shm presentation adopts it, and the drawable behind it is
// resized so the frame rendered at the new size is the frame read back. Qt resizes the
// window and then makes the context current, so the make-current arm is the one it hits.
void ApplyWaylandResize(EGLStateContext* state, EGLDisplay dpy, EGLSurface surface) {
const auto it = WaylandSurfaces().find(surface);
if (it == WaylandSurfaces().end()) return;
EGLint width = 0;
EGLint height = 0;
if (!it->second->TakeResize(&width, &height)) return;
(void)state->ResizeSurface(dpy, surface, width, height);
auto* backendObject = MG_Backend::pActiveBackendObject.get();
if (backendObject == nullptr ||
!backendObject->ResizeEGLWindowSurface(surface, static_cast<Uint32>(width), static_cast<Uint32>(height))) {
MGLOG_E("Wayland: the drawable behind a %dx%d window could not be resized", width, height);
}
}
#endif
} // namespace
EGLSurface CreateWindowSurface(EGLDisplay dpy, EGLConfig config, NativeWindowType window,
@@ -124,6 +286,17 @@ namespace MobileGL::MG_Impl::EGLImpl {
if (!state) {
return EGL_NO_SURFACE;
}
#if MOBILEGL_WAYLAND_WINDOWS
if (RefuseGbmWindow(state, dpy)) return EGL_NO_SURFACE;
{
const std::lock_guard<std::recursive_mutex> operationLock(EGLOperationMutex());
EGLSurface waylandSurface = EGL_NO_SURFACE;
if (CreateWaylandWindowSurface(state, dpy, config, ToVoidHandle(window), /*platformWindow=*/false,
&waylandSurface)) {
return waylandSurface;
}
}
#endif
if (!state->IsDisplayInitialized(dpy)) {
state->SetError(EGL_NOT_INITIALIZED);
return EGL_NO_SURFACE;
@@ -146,11 +319,34 @@ namespace MobileGL::MG_Impl::EGLImpl {
return EGL_NO_SURFACE;
}
Uint32 requestedWidth = static_cast<Uint32>(std::max<EGLint>(GetAttribValue(attrib_list, EGL_WIDTH, 0), 0));
Uint32 requestedHeight = static_cast<Uint32>(std::max<EGLint>(GetAttribValue(attrib_list, EGL_HEIGHT, 0), 0));
#if MOBILEGL_BUILD_DISAGGREGATED && defined(__ANDROID__)
// P12: A GAME'S OWN WINDOW SIZE IS THE SIZE OF THE FRAMES IT RENDERS, and a server-owned
// window has to take that size or clip. SDL (Minecraft's Android backend) creates the EGL
// window surface with no EGL_WIDTH / EGL_HEIGHT, so the request went out as 0x0 - "use the
// server window's own size" - and a landscape 2620x1280 game drawn into a portrait 1280x2620
// server window came out clipped to its bottom-left 1280x1280 corner (Espryt and Magma
// alike; the same game in a landscape window was right). The native window it handed us
// knows the size the game believes it has: ask for THAT, and the server fixes its window
// buffer to it and aspect-fits the view (MobileGLDisplayActivity), letterboxed.
if (serverOwnedWindow && requestedWidth == 0 && requestedHeight == 0 && !IsNullNativeHandle(window)) {
auto* native = reinterpret_cast<ANativeWindow*>(ToVoidHandle(window));
const int32_t nativeWidth = ANativeWindow_getWidth(native);
const int32_t nativeHeight = ANativeWindow_getHeight(native);
if (nativeWidth > 0 && nativeHeight > 0) {
requestedWidth = static_cast<Uint32>(nativeWidth);
requestedHeight = static_cast<Uint32>(nativeHeight);
MGLOG_I("eglCreateWindowSurface: no EGL_WIDTH/EGL_HEIGHT; the server-owned window is asked for the "
"native window's %dx%d", nativeWidth, nativeHeight);
}
}
#endif
const MG_Backend::WindowHandle windowHandle = {
.Backend = DetectWindowBackend(),
.Handle = ToVoidHandle(window),
.Width = static_cast<Uint32>(std::max<EGLint>(GetAttribValue(attrib_list, EGL_WIDTH, 0), 0)),
.Height = static_cast<Uint32>(std::max<EGLint>(GetAttribValue(attrib_list, EGL_HEIGHT, 0), 0)),
.Width = requestedWidth,
.Height = requestedHeight,
};
#if MOBILEGL_BUILD_DISAGGREGATED
@@ -183,6 +379,10 @@ namespace MobileGL::MG_Impl::EGLImpl {
EGLBoolean SwapBuffers(EGLDisplay dpy, EGLSurface draw) {
const std::lock_guard<std::recursive_mutex> operationLock(EGLOperationMutex());
#if MOBILEGL_BUILD_DISAGGREGATED
// The present is this thread's context's (GLStreamScope in EGLImpl.h).
if (StreamGateActive()) StreamBindCallingThreadLocked();
#endif
auto* state = GetState();
if (!state) {
return EGL_FALSE;
@@ -197,11 +397,23 @@ namespace MobileGL::MG_Impl::EGLImpl {
MGLOG_E_ONCE("activeBackendObject not initialized!");
return EGL_FALSE;
}
#if MOBILEGL_WAYLAND_WINDOWS
// A Wayland window shows what is attached to it: the frame is read back and attached
// BEFORE the swap, while it is still the drawable's content.
if (auto it = WaylandSurfaces().find(draw); it != WaylandSurfaces().end() && !it->second->Present()) {
state->SetError(EGL_BAD_SURFACE);
return EGL_FALSE;
}
#endif
if (!backendObject->SwapEGLBuffers(dpy, draw)) {
MGLOG_E_ONCE("eglSwapBuffers failed on thread=%s dpy=%p draw=%p", CurrentThreadIdString().c_str(), dpy, draw);
state->SetError(EGL_BAD_SURFACE);
return EGL_FALSE;
}
#if MOBILEGL_WAYLAND_WINDOWS
// A resize made between frames takes effect for the next one.
ApplyWaylandResize(state, dpy, draw);
#endif
return EGL_TRUE;
}
@@ -219,7 +431,35 @@ namespace MobileGL::MG_Impl::EGLImpl {
if (!state) {
return EGL_NO_CONTEXT;
}
return state->CreateContext(dpy, config, shareCtx, attrib_list);
const EGLContext context = state->CreateContext(dpy, config, shareCtx, attrib_list);
if (context == EGL_NO_CONTEXT) {
return EGL_NO_CONTEXT;
}
#if MOBILEGL_BUILD_DISAGGREGATED
// P14 S1 (docs/Disaggregated/design/11-state-ownership.md). THE TOKEN CROSSES BEFORE
// eglCreateContext RETURNS. EGLState minted it, together with the share group it was
// derived into (the sharing context's group, or a fresh one), and the server tables it
// here through the same blocking handshake the surface forwarders use - so the first
// bind_context after this call names a context the session already has, and the
// sink's "this session never created that token" refusal is unreachable on the
// well-behaved path.
//
// A REFUSAL DROPS THE CLIENT'S CONTEXT AGAIN. Keeping it would hand the application a
// handle whose first verb the server refuses - a late failure where an early one is
// available, and EGL_NO_CONTEXT with EGL_BAD_ACCESS is the early one. NoWire (the
// pre-Start bring-up, the server role) keeps it: there is no server to refuse.
if (MG_Config::Transport != MG_Config::TransportMode::Monolith &&
!MG_Remote::Client::RunsAsTheServerRole()) {
const auto emitted = MG_Remote::Client::SendCreateContextFrame(
state->GetContextClientToken(context), state->GetContextShareGroupToken(context), 0);
if (emitted == MG_Remote::Client::ContextFrameEmit::Refused) {
state->DestroyContext(dpy, context);
state->SetError(EGL_BAD_ACCESS);
return EGL_NO_CONTEXT;
}
}
#endif
return context;
}
EGLBoolean Initialize(EGLDisplay dpy, EGLint* major, EGLint* minor) {
@@ -270,11 +510,20 @@ namespace MobileGL::MG_Impl::EGLImpl {
const auto oldDraw = state->GetCurrentSurface(EGL_DRAW);
const auto oldRead = state->GetCurrentSurface(EGL_READ);
const auto oldContext = state->GetCurrentContext();
#if MOBILEGL_BUILD_DISAGGREGATED
// P14 S1. The context the calling thread had current BEFORE this call, as the wire
// knows it. Read here because `state->MakeCurrent` below has already committed the
// new one by the time the emission points are reached.
const Uint64 oldContextToken = state->GetContextClientToken(oldContext);
#endif
const String threadId = CurrentThreadIdString();
MGLOG_D("eglMakeCurrent begin thread=%s dpy=%p draw=%p read=%p ctx=%p oldDpy=%p oldDraw=%p oldRead=%p oldCtx=%p",
threadId.c_str(), dpy, draw, read, ctx, oldDisplay, oldDraw, oldRead, oldContext);
#if MOBILEGL_WAYLAND_WINDOWS
if (draw != EGL_NO_SURFACE) ApplyWaylandResize(state, dpy, draw);
#endif
if (!state->MakeCurrent(dpy, draw, read, ctx)) {
const EGLint error = state->ConsumeError();
MGLOG_D("eglMakeCurrent rejected by EGLState thread=%s error=0x%04x", threadId.c_str(), error);
@@ -294,6 +543,9 @@ namespace MobileGL::MG_Impl::EGLImpl {
}
}
MGLOG_D("eglMakeCurrent release succeeded thread=%s", threadId.c_str());
#if MOBILEGL_BUILD_DISAGGREGATED
EmitContextBinding(state, EGL_NO_CONTEXT, oldContextToken);
#endif
return EGL_TRUE;
}
@@ -312,6 +564,9 @@ namespace MobileGL::MG_Impl::EGLImpl {
}
MGLOG_D("eglMakeCurrent attach succeeded thread=%s dpy=%p draw=%p read=%p ctx=%p", threadId.c_str(), dpy, draw,
read, ctx);
#if MOBILEGL_BUILD_DISAGGREGATED
EmitContextBinding(state, ctx, oldContextToken);
#endif
return EGL_TRUE;
}
@@ -320,7 +575,34 @@ namespace MobileGL::MG_Impl::EGLImpl {
if (!state) {
return EGL_FALSE;
}
return state->DestroyContext(dpy, ctx) ? EGL_TRUE : EGL_FALSE;
#if MOBILEGL_BUILD_DISAGGREGATED
// P14 S1. Read BEFORE the client's own book drops the context: after DestroyContext
// the handle no longer resolves and the token is unrecoverable. The frame is sent
// AFTER the client's state machine has accepted the destroy, so one EGL refused (a
// context still current on some thread, EGL_BAD_ACCESS) never reaches the server - the
// two ends stay in step without the server having to guess why a token vanished.
const Uint64 contextToken = state->GetContextClientToken(ctx);
#endif
if (!state->DestroyContext(dpy, ctx)) {
return EGL_FALSE;
}
#if MOBILEGL_BUILD_DISAGGREGATED
if (contextToken != 0 && MG_Config::Transport != MG_Config::TransportMode::Monolith &&
!MG_Remote::Client::RunsAsTheServerRole()) {
const std::lock_guard<std::recursive_mutex> streamLock(EGLOperationMutex());
// The server drops its binding to 0 when the current context is destroyed
// (ServerSession::DestroyContext's compare-and-swap); the gate mirrors it so the next
// write re-binds.
if (g_boundContextToken == contextToken) g_boundContextToken = 0;
const auto emitted = MG_Remote::Client::SendDestroyContextFrame(contextToken);
if (emitted == MG_Remote::Client::ContextFrameEmit::Refused) {
MGLOG_E_ONCE("eglDestroyContext: the server holds no context token %llu; the "
"session's table and this client's EGL book have drifted apart",
static_cast<unsigned long long>(contextToken));
}
}
#endif
return EGL_TRUE;
}
EGLBoolean DestroySurface(EGLDisplay dpy, EGLSurface surface) {
@@ -332,6 +614,9 @@ namespace MobileGL::MG_Impl::EGLImpl {
if (!state->DestroySurface(dpy, surface)) {
return EGL_FALSE;
}
#if MOBILEGL_WAYLAND_WINDOWS
WaylandSurfaces().erase(surface);
#endif
if (auto* backendObject = MG_Backend::pActiveBackendObject.get()) {
backendObject->ReleaseEGLSurface(surface);
}
@@ -355,16 +640,20 @@ namespace MobileGL::MG_Impl::EGLImpl {
// re-initializes lazily via GetStateEnsureInitialized(); process exit
// then has nothing left to destroy.
if (!state->HasAnyInitializedDisplay() && !state->HasAnyCurrentContext()) {
// P14 S3: the process default context (and with it the GL objects the frontend
// built while no EGL context was current) goes before Destroy() does, so nothing
// it owns outlives glslang::FinalizeProcess() through a thread's fallback binding.
MG_State::ReleaseProcessDefaultGLContext();
MobileGL::Destroy();
}
return EGL_TRUE;
}
EGLBoolean ReleaseThread() {
// Always succeeds (EGL 1.4 §3.11): after the last eglTerminate tore MobileGL down there is
// simply nothing of this thread's left to release.
if (!MG_State::pEGLContext) return EGL_TRUE;
auto* state = GetState();
if (!state) {
return EGL_FALSE;
}
if (auto* backendObject = MG_Backend::pActiveBackendObject.get()) {
(void)backendObject->MakeEGLCurrent(EGL_NO_DISPLAY, EGL_NO_SURFACE, EGL_NO_SURFACE, EGL_NO_CONTEXT);
}
@@ -440,14 +729,10 @@ namespace MobileGL::MG_Impl::EGLImpl {
case EGL_CLIENT_APIS:
return "OpenGL OpenGL_ES";
case EGL_EXTENSIONS:
if (display == EGL_NO_DISPLAY) {
return "EGL_EXT_client_extensions "
"EGL_EXT_platform_base "
"EGL_KHR_platform_base "
"EGL_MESA_platform_surfaceless";
}
return "EGL_KHR_create_context "
"EGL_MESA_platform_surfaceless";
// One list, in EGLPlatformExtensions.h, because the vendor string glvnd reads and
// this query an application reads have to agree: an application that does not see the
// platform extension here never asks for a platform display at all.
return display == EGL_NO_DISPLAY ? kClientExtensionString : kDisplayExtensionString;
default:
state->SetError(EGL_BAD_PARAMETER);
return nullptr;
@@ -689,6 +974,16 @@ namespace MobileGL::MG_Impl::EGLImpl {
if (!state) {
return EGL_NO_DISPLAY;
}
#if MOBILEGL_WAYLAND_WINDOWS
// A "GBM" display whose native display is a wl_display is a Wayland one: Chromium's
// Wayland GL layer names the GBM platform for every display it asks for, including the
// one on its own Wayland connection whose windows are wl_egl_windows.
constexpr EGLenum kPlatformGbm = 0x31D7;
if (platform == kPlatformGbm && native_display != nullptr &&
Wayland::IsWaylandDisplay(reinterpret_cast<Uint64>(native_display), EGL_NONE)) {
platform = Wayland::kPlatformWayland;
}
#endif
return state->GetPlatformDisplay(platform, native_display);
}
@@ -698,6 +993,17 @@ namespace MobileGL::MG_Impl::EGLImpl {
if (!state) {
return EGL_NO_SURFACE;
}
#if MOBILEGL_WAYLAND_WINDOWS
if (RefuseGbmWindow(state, dpy)) return EGL_NO_SURFACE;
{
const std::lock_guard<std::recursive_mutex> operationLock(EGLOperationMutex());
EGLSurface waylandSurface = EGL_NO_SURFACE;
if (CreateWaylandWindowSurface(state, dpy, config, native_window, /*platformWindow=*/true,
&waylandSurface)) {
return waylandSurface;
}
}
#endif
#if MOBILEGL_BUILD_DISAGGREGATED
// P12 (on-screen server window), D1: as CreateWindowSurface - with MOBILEGL_IPC_SURFACE=server
// the window is the server's, and a headless client's NULL is accepted.
+60
View File
@@ -9,6 +9,8 @@
#pragma once
#include <Includes.h>
#include <mutex>
namespace MobileGL::MG_Impl::EGLImpl {
EGLSurface CreateWindowSurface(EGLDisplay dpy, EGLConfig config, NativeWindowType window,
const EGLint* attrib_list);
@@ -62,4 +64,62 @@ namespace MobileGL::MG_Impl::EGLImpl {
const EGLAttrib* attrib_list);
EGLBoolean WaitSync(EGLDisplay dpy, EGLSync sync, EGLint flags);
__eglMustCastToProperFunctionPointerType GetProcAddress(const char* name);
#if MOBILEGL_BUILD_DISAGGREGATED
// THE CLIENT'S STREAM GATE. Under a split transport every thread of the process writes into
// ONE ring, and the server attributes each record to the context the last applied
// `bind_context` named - one current context per SESSION. A context, though, is current per
// THREAD, and Qt renders each window on its own QSGRenderThread: two threads' records then
// interleave on the ring with no binding between them and land in the wrong context's
// applier (empty sampler windows at a Clear, another context's buffers under an upload).
//
// So every GL entry point and every EGL operation that writes to the stream runs under one
// process-wide lock (EGLOperationMutex, which the EGL side already held), and before it
// writes, the server is re-bound to the CALLING thread's context whenever the last binding
// actually sent names a different one. A no-op scope on a monolith transport and on the
// server's own apply thread.
Bool StreamGateActive();
std::recursive_mutex& StreamMutex();
void StreamBindCallingThreadLocked();
class GLStreamScope {
public:
GLStreamScope() {
if (!StreamGateActive()) return;
m_mutex = &StreamMutex();
m_mutex->lock();
StreamBindCallingThreadLocked();
}
~GLStreamScope() {
if (m_mutex != nullptr) m_mutex->unlock();
}
GLStreamScope(const GLStreamScope&) = delete;
GLStreamScope& operator=(const GLStreamScope&) = delete;
private:
std::recursive_mutex* m_mutex = nullptr;
};
// The lock alone, for every EGL entry point: an EGL call may publish into the ring too (a
// surface forwarder's catch-up wait, a present, a context frame), and a second thread's GL
// record written into the middle of it is a torn record the server latches the session on.
class StreamLockScope {
public:
StreamLockScope() {
if (!StreamGateActive()) return;
m_mutex = &StreamMutex();
m_mutex->lock();
}
~StreamLockScope() {
if (m_mutex != nullptr) m_mutex->unlock();
}
StreamLockScope(const StreamLockScope&) = delete;
StreamLockScope& operator=(const StreamLockScope&) = delete;
private:
std::recursive_mutex* m_mutex = nullptr;
};
#else
class GLStreamScope {};
class StreamLockScope {};
#endif
} // namespace MobileGL::MG_Impl::EGLImpl
@@ -0,0 +1,27 @@
// SPDX-License-Identifier: LGPL-3.0-only
#pragma once
namespace MobileGL::MG_Impl::EGLImpl {
// Keep libglvnd routing and direct queries consistent. Device/GBM extensions
// are absent until their entry points and image paths exist.
inline constexpr const char* kDeviceExtensionString = "";
#if defined(__linux__) && !defined(__ANDROID__)
// GBM is claimed as a display platform only: a display on a gbm_device is an offscreen display
// like a surfaceless one (the frames come from the backend, not from the device), and window
// surfaces on it are refused. Chromium's Wayland GPU process hands its GL implementation nothing
// but a GBM display, so without the claim it never reaches this library at all.
#define MOBILEGL_PLATFORM_WAYLAND_EXTENSIONS \
" EGL_KHR_platform_wayland EGL_EXT_platform_wayland EGL_KHR_platform_gbm EGL_MESA_platform_gbm"
#else
#define MOBILEGL_PLATFORM_WAYLAND_EXTENSIONS ""
#endif
inline constexpr const char* kPlatformExtensionString =
"EGL_EXT_client_extensions EGL_EXT_platform_base EGL_KHR_platform_base "
"EGL_MESA_platform_surfaceless" MOBILEGL_PLATFORM_WAYLAND_EXTENSIONS;
inline constexpr const char* kDisplayExtensionString =
"EGL_KHR_create_context EGL_EXT_platform_base EGL_KHR_platform_base "
"EGL_MESA_platform_surfaceless" MOBILEGL_PLATFORM_WAYLAND_EXTENSIONS;
inline constexpr const char* kClientExtensionString =
"EGL_EXT_client_extensions EGL_KHR_create_context EGL_EXT_platform_base "
"EGL_KHR_platform_base EGL_MESA_platform_surfaceless" MOBILEGL_PLATFORM_WAYLAND_EXTENSIONS;
}
@@ -9,14 +9,34 @@
#include <Includes.h>
#include "../EGLImpl.h"
#include <vector>
namespace {
// The KHR/EXT spellings take EGLint attribute lists where the core 1.5 entry points take
// EGLAttrib (pointer-sized). Reinterpreting one as the other reads two EGLints as one
// attribute on 64-bit, so the list is widened instead.
std::vector<EGLAttrib> WidenAttribList(const EGLint* attribs) {
std::vector<EGLAttrib> widened;
if (attribs == nullptr) return widened;
for (; *attribs != EGL_NONE; attribs += 2) {
widened.push_back(static_cast<EGLAttrib>(attribs[0]));
widened.push_back(static_cast<EGLAttrib>(attribs[1]));
}
widened.push_back(EGL_NONE);
return widened;
}
} // namespace
MOBILEGL_EGL_API EGLSurface eglCreateWindowSurface(EGLDisplay dpy, EGLConfig config, NativeWindowType window,
const EGLint* attrib_list) {
const MobileGL::MG_Impl::EGLImpl::StreamLockScope mglStreamLock;
MGLOG_D("eglCreateWindowSurface(dpy=%p, config=%p, window=%p, attrib_list=%p)", dpy, config, window, attrib_list);
return MobileGL::MG_Impl::EGLImpl::CreateWindowSurface(dpy, config, window, attrib_list);
}
MOBILEGL_EGL_API EGLBoolean eglChooseConfig(EGLDisplay dpy, const EGLint* attrib_list, EGLConfig* configs,
EGLint config_size, EGLint* num_config) {
const MobileGL::MG_Impl::EGLImpl::StreamLockScope mglStreamLock;
MGLOG_D("eglChooseConfig(dpy=%p, attrib_list=%p, configs=%p, config_size=%d, num_config=%p)", dpy, attrib_list,
configs, config_size, num_config);
return MobileGL::MG_Impl::EGLImpl::ChooseConfig(dpy, attrib_list, configs, config_size, num_config);
@@ -24,6 +44,7 @@ MOBILEGL_EGL_API EGLBoolean eglChooseConfig(EGLDisplay dpy, const EGLint* attrib
MOBILEGL_EGL_API EGLContext eglCreateContext(EGLDisplay dpy, EGLConfig config, EGLContext shareCtx,
const EGLint* attrib_list) {
const MobileGL::MG_Impl::EGLImpl::StreamLockScope mglStreamLock;
MGLOG_D("eglCreateContext(dpy=%p, config=%p, shareCtx=%p, attrib_list=%p)", dpy, config, shareCtx, attrib_list);
#ifdef TRACY_ENABLE
tracy::StartupProfiler();
@@ -32,26 +53,31 @@ MOBILEGL_EGL_API EGLContext eglCreateContext(EGLDisplay dpy, EGLConfig config, E
}
MOBILEGL_EGL_API EGLBoolean eglInitialize(EGLDisplay dpy, EGLint* major, EGLint* minor) {
const MobileGL::MG_Impl::EGLImpl::StreamLockScope mglStreamLock;
MGLOG_D("eglInitialize(dpy=%p, major=%p, minor=%p)", dpy, major, minor);
return MobileGL::MG_Impl::EGLImpl::Initialize(dpy, major, minor);
}
MOBILEGL_EGL_API EGLDisplay eglGetDisplay(NativeDisplayType display) {
const MobileGL::MG_Impl::EGLImpl::StreamLockScope mglStreamLock;
MGLOG_D("eglGetDisplay(display=%p)", display);
return MobileGL::MG_Impl::EGLImpl::GetDisplay(display);
}
MOBILEGL_EGL_API EGLint eglGetError() {
const MobileGL::MG_Impl::EGLImpl::StreamLockScope mglStreamLock;
MGLOG_D("eglGetError()");
return MobileGL::MG_Impl::EGLImpl::GetError();
}
MOBILEGL_EGL_API EGLBoolean eglMakeCurrent(EGLDisplay dpy, EGLSurface draw, EGLSurface read, EGLContext ctx) {
const MobileGL::MG_Impl::EGLImpl::StreamLockScope mglStreamLock;
MGLOG_D("eglMakeCurrent(dpy=%p, draw=%p, read=%p, ctx=%p)", dpy, draw, read, ctx);
return MobileGL::MG_Impl::EGLImpl::MakeCurrent(dpy, draw, read, ctx);
}
MOBILEGL_EGL_API EGLBoolean eglDestroyContext(EGLDisplay dpy, EGLContext ctx) {
const MobileGL::MG_Impl::EGLImpl::StreamLockScope mglStreamLock;
MGLOG_D("eglDestroyContext(dpy=%p, ctx=%p)", dpy, ctx);
#ifdef TRACY_ENABLE
tracy::ShutdownProfiler();
@@ -60,56 +86,67 @@ MOBILEGL_EGL_API EGLBoolean eglDestroyContext(EGLDisplay dpy, EGLContext ctx) {
}
MOBILEGL_EGL_API EGLBoolean eglDestroySurface(EGLDisplay dpy, EGLSurface surface) {
const MobileGL::MG_Impl::EGLImpl::StreamLockScope mglStreamLock;
MGLOG_D("eglDestroySurface(dpy=%p, surface=%p)", dpy, surface);
return MobileGL::MG_Impl::EGLImpl::DestroySurface(dpy, surface);
}
MOBILEGL_EGL_API EGLBoolean eglTerminate(EGLDisplay dpy) {
const MobileGL::MG_Impl::EGLImpl::StreamLockScope mglStreamLock;
MGLOG_D("eglTerminate(dpy=%p)", dpy);
return MobileGL::MG_Impl::EGLImpl::Terminate(dpy);
}
MOBILEGL_EGL_API EGLBoolean eglReleaseThread(void) {
const MobileGL::MG_Impl::EGLImpl::StreamLockScope mglStreamLock;
MGLOG_D("eglReleaseThread()");
return MobileGL::MG_Impl::EGLImpl::ReleaseThread();
}
MOBILEGL_EGL_API EGLContext eglGetCurrentContext(void) {
const MobileGL::MG_Impl::EGLImpl::StreamLockScope mglStreamLock;
MGLOG_D("eglGetCurrentContext()");
return MobileGL::MG_Impl::EGLImpl::GetCurrentContext();
}
MOBILEGL_EGL_API EGLBoolean eglGetConfigAttrib(EGLDisplay dpy, EGLConfig config, EGLint attribute, EGLint* value) {
const MobileGL::MG_Impl::EGLImpl::StreamLockScope mglStreamLock;
MGLOG_D("eglGetConfigAttrib(dpy=%p, config=%p, attribute=%d, value=%p)", dpy, config, attribute, value);
return MobileGL::MG_Impl::EGLImpl::GetConfigAttrib(dpy, config, attribute, value);
}
MOBILEGL_EGL_API EGLBoolean eglBindAPI(EGLenum api) {
const MobileGL::MG_Impl::EGLImpl::StreamLockScope mglStreamLock;
MGLOG_D("eglBindAPI(api=%u)", api);
return MobileGL::MG_Impl::EGLImpl::BindAPI(api);
}
MOBILEGL_EGL_API EGLSurface eglGetCurrentSurface(EGLint readdraw) {
const MobileGL::MG_Impl::EGLImpl::StreamLockScope mglStreamLock;
MGLOG_D("eglGetCurrentSurface(readdraw=%d)", readdraw);
return MobileGL::MG_Impl::EGLImpl::GetCurrentSurface(readdraw);
}
MOBILEGL_EGL_API EGLBoolean eglQuerySurface(EGLDisplay display, EGLSurface surface, EGLint attribute, EGLint* value) {
const MobileGL::MG_Impl::EGLImpl::StreamLockScope mglStreamLock;
MGLOG_D("eglQuerySurface(display=%p, surface=%p, attribute=%d, value=%p)", display, surface, attribute, value);
return MobileGL::MG_Impl::EGLImpl::QuerySurface(display, surface, attribute, value);
}
MOBILEGL_EGL_API char const* eglQueryString(EGLDisplay display, EGLint name) {
const MobileGL::MG_Impl::EGLImpl::StreamLockScope mglStreamLock;
MGLOG_D("eglQueryString(display=%p, name=%d)", display, name);
return MobileGL::MG_Impl::EGLImpl::QueryString(display, name);
}
MOBILEGL_EGL_API EGLBoolean eglSwapInterval(EGLDisplay dpy, EGLint interval) {
const MobileGL::MG_Impl::EGLImpl::StreamLockScope mglStreamLock;
MGLOG_D("eglSwapInterval(dpy=%p, interval=%d)", dpy, interval);
return MobileGL::MG_Impl::EGLImpl::SwapInterval(dpy, interval);
}
MOBILEGL_EGL_API EGLBoolean eglSwapBuffers(EGLDisplay dpy, EGLSurface draw) {
const MobileGL::MG_Impl::EGLImpl::StreamLockScope mglStreamLock;
MGLOG_D("eglSwapBuffers(dpy=%p, draw=%p)", dpy, draw);
#ifdef TRACY_ENABLE
FrameMark;
@@ -118,32 +155,38 @@ MOBILEGL_EGL_API EGLBoolean eglSwapBuffers(EGLDisplay dpy, EGLSurface draw) {
}
MOBILEGL_EGL_API EGLSurface eglCreatePbufferSurface(EGLDisplay dpy, EGLConfig config, const EGLint* attrib_list) {
const MobileGL::MG_Impl::EGLImpl::StreamLockScope mglStreamLock;
MGLOG_D("eglCreatePbufferSurface(dpy=%p, config=%p, attrib_list=%p)", dpy, config, attrib_list);
return MobileGL::MG_Impl::EGLImpl::CreatePbufferSurface(dpy, config, attrib_list);
}
MOBILEGL_EGL_API __eglMustCastToProperFunctionPointerType eglGetProcAddress(const char* name) {
const MobileGL::MG_Impl::EGLImpl::StreamLockScope mglStreamLock;
MGLOG_D("eglGetProcAddress(name=%s)", name ? name : "null");
return MobileGL::MG_Impl::EGLImpl::GetProcAddress(name);
}
MOBILEGL_EGL_API EGLBoolean eglBindTexImage(EGLDisplay dpy, EGLSurface surface, EGLint buffer) {
const MobileGL::MG_Impl::EGLImpl::StreamLockScope mglStreamLock;
MGLOG_D("eglBindTexImage(dpy=%p, surface=%p, buffer=%d)", dpy, surface, buffer);
return MobileGL::MG_Impl::EGLImpl::BindTexImage(dpy, surface, buffer);
}
MOBILEGL_EGL_API EGLBoolean eglReleaseTexImage(EGLDisplay dpy, EGLSurface surface, EGLint buffer) {
const MobileGL::MG_Impl::EGLImpl::StreamLockScope mglStreamLock;
MGLOG_D("eglReleaseTexImage(dpy=%p, surface=%p, buffer=%d)", dpy, surface, buffer);
return MobileGL::MG_Impl::EGLImpl::ReleaseTexImage(dpy, surface, buffer);
}
MOBILEGL_EGL_API EGLBoolean eglCopyBuffers(EGLDisplay dpy, EGLSurface surface, EGLNativePixmapType target) {
const MobileGL::MG_Impl::EGLImpl::StreamLockScope mglStreamLock;
MGLOG_D("eglCopyBuffers(dpy=%p, surface=%p, target=%p)", dpy, surface, target);
return MobileGL::MG_Impl::EGLImpl::CopyBuffers(dpy, surface, target);
}
MOBILEGL_EGL_API EGLSurface eglCreatePbufferFromClientBuffer(EGLDisplay dpy, EGLenum buftype, EGLClientBuffer buffer,
EGLConfig config, const EGLint* attrib_list) {
const MobileGL::MG_Impl::EGLImpl::StreamLockScope mglStreamLock;
MGLOG_D("eglCreatePbufferFromClientBuffer(dpy=%p, buftype=%u, buffer=%p, config=%p, attrib_list=%p)", dpy, buftype,
buffer, config, attrib_list);
return MobileGL::MG_Impl::EGLImpl::CreatePbufferFromClientBuffer(dpy, buftype, buffer, config, attrib_list);
@@ -151,85 +194,119 @@ MOBILEGL_EGL_API EGLSurface eglCreatePbufferFromClientBuffer(EGLDisplay dpy, EGL
MOBILEGL_EGL_API EGLSurface eglCreatePixmapSurface(EGLDisplay dpy, EGLConfig config, EGLNativePixmapType pixmap,
const EGLint* attrib_list) {
const MobileGL::MG_Impl::EGLImpl::StreamLockScope mglStreamLock;
MGLOG_D("eglCreatePixmapSurface(dpy=%p, config=%p, pixmap=%p, attrib_list=%p)", dpy, config, pixmap, attrib_list);
return MobileGL::MG_Impl::EGLImpl::CreatePixmapSurface(dpy, config, pixmap, attrib_list);
}
MOBILEGL_EGL_API EGLBoolean eglGetConfigs(EGLDisplay dpy, EGLConfig* configs, EGLint config_size, EGLint* num_config) {
const MobileGL::MG_Impl::EGLImpl::StreamLockScope mglStreamLock;
MGLOG_D("eglGetConfigs(dpy=%p, configs=%p, config_size=%d, num_config=%p)", dpy, configs, config_size, num_config);
return MobileGL::MG_Impl::EGLImpl::GetConfigs(dpy, configs, config_size, num_config);
}
MOBILEGL_EGL_API EGLDisplay eglGetCurrentDisplay(void) {
const MobileGL::MG_Impl::EGLImpl::StreamLockScope mglStreamLock;
MGLOG_D("eglGetCurrentDisplay()");
return MobileGL::MG_Impl::EGLImpl::GetCurrentDisplay();
}
MOBILEGL_EGL_API EGLenum eglQueryAPI(void) {
const MobileGL::MG_Impl::EGLImpl::StreamLockScope mglStreamLock;
MGLOG_D("eglQueryAPI()");
return MobileGL::MG_Impl::EGLImpl::QueryAPI();
}
MOBILEGL_EGL_API EGLBoolean eglQueryContext(EGLDisplay dpy, EGLContext ctx, EGLint attribute, EGLint* value) {
const MobileGL::MG_Impl::EGLImpl::StreamLockScope mglStreamLock;
MGLOG_D("eglQueryContext(dpy=%p, ctx=%p, attribute=%d, value=%p)", dpy, ctx, attribute, value);
return MobileGL::MG_Impl::EGLImpl::QueryContext(dpy, ctx, attribute, value);
}
MOBILEGL_EGL_API EGLBoolean eglSurfaceAttrib(EGLDisplay dpy, EGLSurface surface, EGLint attribute, EGLint value) {
const MobileGL::MG_Impl::EGLImpl::StreamLockScope mglStreamLock;
MGLOG_D("eglSurfaceAttrib(dpy=%p, surface=%p, attribute=%d, value=%d)", dpy, surface, attribute, value);
return MobileGL::MG_Impl::EGLImpl::SurfaceAttrib(dpy, surface, attribute, value);
}
MOBILEGL_EGL_API EGLBoolean eglWaitClient(void) {
const MobileGL::MG_Impl::EGLImpl::StreamLockScope mglStreamLock;
MGLOG_D("eglWaitClient()");
return MobileGL::MG_Impl::EGLImpl::WaitClient();
}
MOBILEGL_EGL_API EGLBoolean eglWaitGL(void) {
const MobileGL::MG_Impl::EGLImpl::StreamLockScope mglStreamLock;
MGLOG_D("eglWaitGL()");
return MobileGL::MG_Impl::EGLImpl::WaitGL();
}
MOBILEGL_EGL_API EGLBoolean eglWaitNative(EGLint engine) {
const MobileGL::MG_Impl::EGLImpl::StreamLockScope mglStreamLock;
MGLOG_D("eglWaitNative(engine=%d)", engine);
return MobileGL::MG_Impl::EGLImpl::WaitNative(engine);
}
MOBILEGL_EGL_API EGLSync eglCreateSync(EGLDisplay dpy, EGLenum type, const EGLAttrib* attrib_list) {
const MobileGL::MG_Impl::EGLImpl::StreamLockScope mglStreamLock;
MGLOG_D("eglCreateSync(dpy=%p, type=%u, attrib_list=%p)", dpy, type, attrib_list);
return MobileGL::MG_Impl::EGLImpl::CreateSync(dpy, type, attrib_list);
}
MOBILEGL_EGL_API EGLBoolean eglDestroySync(EGLDisplay dpy, EGLSync sync) {
const MobileGL::MG_Impl::EGLImpl::StreamLockScope mglStreamLock;
MGLOG_D("eglDestroySync(dpy=%p, sync=%p)", dpy, sync);
return MobileGL::MG_Impl::EGLImpl::DestroySync(dpy, sync);
}
MOBILEGL_EGL_API EGLint eglClientWaitSync(EGLDisplay dpy, EGLSync sync, EGLint flags, EGLTime timeout) {
const MobileGL::MG_Impl::EGLImpl::StreamLockScope mglStreamLock;
MGLOG_D("eglClientWaitSync(dpy=%p, sync=%p, flags=%d, timeout=%llu)", dpy, sync, flags,
static_cast<unsigned long long>(timeout));
return MobileGL::MG_Impl::EGLImpl::ClientWaitSync(dpy, sync, flags, timeout);
}
MOBILEGL_EGL_API EGLBoolean eglGetSyncAttrib(EGLDisplay dpy, EGLSync sync, EGLint attribute, EGLAttrib* value) {
const MobileGL::MG_Impl::EGLImpl::StreamLockScope mglStreamLock;
MGLOG_D("eglGetSyncAttrib(dpy=%p, sync=%p, attribute=%d, value=%p)", dpy, sync, attribute, value);
return MobileGL::MG_Impl::EGLImpl::GetSyncAttrib(dpy, sync, attribute, value);
}
MOBILEGL_EGL_API EGLImage eglCreateImage(EGLDisplay dpy, EGLContext ctx, EGLenum target, EGLClientBuffer buffer,
const EGLAttrib* attrib_list) {
const MobileGL::MG_Impl::EGLImpl::StreamLockScope mglStreamLock;
MGLOG_D("eglCreateImage(dpy=%p, ctx=%p, target=%u, buffer=%p, attrib_list=%p)", dpy, ctx, target, buffer,
attrib_list);
return MobileGL::MG_Impl::EGLImpl::CreateImage(dpy, ctx, target, buffer, attrib_list);
}
MOBILEGL_EGL_API EGLBoolean eglDestroyImage(EGLDisplay dpy, EGLImage image) {
const MobileGL::MG_Impl::EGLImpl::StreamLockScope mglStreamLock;
MGLOG_D("eglDestroyImage(dpy=%p, image=%p)", dpy, image);
return MobileGL::MG_Impl::EGLImpl::DestroyImage(dpy, image);
}
// EGL_KHR_image_base's spellings. KWin loads these two by name and calls eglDestroyImageKHR
// for every cached client buffer when it dies, wl_shm ones included; a null answer from
// eglGetProcAddress made that a jump to address 0.
MOBILEGL_EGL_API EGLImage eglCreateImageKHR(EGLDisplay dpy, EGLContext ctx, EGLenum target, EGLClientBuffer buffer,
const EGLint* attrib_list) {
const MobileGL::MG_Impl::EGLImpl::StreamLockScope mglStreamLock;
MGLOG_D("eglCreateImageKHR(dpy=%p, ctx=%p, target=%u, buffer=%p, attrib_list=%p)", dpy, ctx, target, buffer,
attrib_list);
const std::vector<EGLAttrib> attribs = WidenAttribList(attrib_list);
return MobileGL::MG_Impl::EGLImpl::CreateImage(dpy, ctx, target, buffer, attrib_list ? attribs.data() : nullptr);
}
MOBILEGL_EGL_API EGLBoolean eglDestroyImageKHR(EGLDisplay dpy, EGLImage image) {
const MobileGL::MG_Impl::EGLImpl::StreamLockScope mglStreamLock;
MGLOG_D("eglDestroyImageKHR(dpy=%p, image=%p)", dpy, image);
return MobileGL::MG_Impl::EGLImpl::DestroyImage(dpy, image);
}
MOBILEGL_EGL_API EGLDisplay eglGetPlatformDisplay(EGLenum platform, void* native_display,
const EGLAttrib* attrib_list) {
const MobileGL::MG_Impl::EGLImpl::StreamLockScope mglStreamLock;
MGLOG_D("eglGetPlatformDisplay(platform=%u, native_display=%p, attrib_list=%p)", platform, native_display,
attrib_list);
return MobileGL::MG_Impl::EGLImpl::GetPlatformDisplay(platform, native_display, attrib_list);
@@ -237,14 +314,17 @@ MOBILEGL_EGL_API EGLDisplay eglGetPlatformDisplay(EGLenum platform, void* native
MOBILEGL_EGL_API EGLDisplay eglGetPlatformDisplayEXT(EGLenum platform, void* native_display,
const EGLint* attrib_list) {
const MobileGL::MG_Impl::EGLImpl::StreamLockScope mglStreamLock;
MGLOG_D("eglGetPlatformDisplayEXT(platform=%u, native_display=%p, attrib_list=%p)", platform, native_display,
attrib_list);
return MobileGL::MG_Impl::EGLImpl::GetPlatformDisplay(
platform, native_display, reinterpret_cast<const EGLAttrib*>(attrib_list));
const std::vector<EGLAttrib> attribs = WidenAttribList(attrib_list);
return MobileGL::MG_Impl::EGLImpl::GetPlatformDisplay(platform, native_display,
attrib_list ? attribs.data() : nullptr);
}
MOBILEGL_EGL_API EGLSurface eglCreatePlatformWindowSurface(EGLDisplay dpy, EGLConfig config, void* native_window,
const EGLAttrib* attrib_list) {
const MobileGL::MG_Impl::EGLImpl::StreamLockScope mglStreamLock;
MGLOG_D("eglCreatePlatformWindowSurface(dpy=%p, config=%p, native_window=%p, attrib_list=%p)", dpy, config,
native_window, attrib_list);
return MobileGL::MG_Impl::EGLImpl::CreatePlatformWindowSurface(dpy, config, native_window, attrib_list);
@@ -252,12 +332,14 @@ MOBILEGL_EGL_API EGLSurface eglCreatePlatformWindowSurface(EGLDisplay dpy, EGLCo
MOBILEGL_EGL_API EGLSurface eglCreatePlatformPixmapSurface(EGLDisplay dpy, EGLConfig config, void* native_pixmap,
const EGLAttrib* attrib_list) {
const MobileGL::MG_Impl::EGLImpl::StreamLockScope mglStreamLock;
MGLOG_D("eglCreatePlatformPixmapSurface(dpy=%p, config=%p, native_pixmap=%p, attrib_list=%p)", dpy, config,
native_pixmap, attrib_list);
return MobileGL::MG_Impl::EGLImpl::CreatePlatformPixmapSurface(dpy, config, native_pixmap, attrib_list);
}
MOBILEGL_EGL_API EGLBoolean eglWaitSync(EGLDisplay dpy, EGLSync sync, EGLint flags) {
const MobileGL::MG_Impl::EGLImpl::StreamLockScope mglStreamLock;
MGLOG_D("eglWaitSync(dpy=%p, sync=%p, flags=%d)", dpy, sync, flags);
return MobileGL::MG_Impl::EGLImpl::WaitSync(dpy, sync, flags);
}
@@ -0,0 +1,322 @@
// MobileGL - MobileGL/MG_Impl/EGLImpl/Exporting/GlvndVendor.cpp
// 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
// MOBILEGL AS A GLVND EGL VENDOR.
//
// On a system with libglvnd, the libEGL.so.1 an application links is not an implementation at all:
// it is a dispatcher, and the implementations are vendor libraries it loads from
// /usr/share/glvnd/egl_vendor.d (or from whatever __EGL_VENDOR_LIBRARY_FILENAMES names) and routes
// every EGL call to. That is the supported way to be the implementation an application actually
// talks to, and on this machine it is the ONLY way that reaches a caller which does not link EGL
// directly: kwin goes through libepoxy, which dlopen()s libEGL.so.1 and dlsyms its entry points at
// startup, so a library injected with LD_PRELOAD is invisible to it however loudly it is named.
//
// What glvnd asks a vendor for is the other half of that ABI, and this file is it: glvnd calls
// __egl_Main once, the library answers with the functions it wants dispatched to it, and - the part
// that decides whether a compositor can use it at all - with the string naming the PLATFORMS it
// serves. A vendor that does not claim EGL_PLATFORM_GBM_KHR is never asked for a GBM display, and
// the caller that asked for one is told there is no usable device: the client sitting in its maps,
// loaded and never called, is exactly what that looks like from the outside.
//
// The entry points below are this library's own (MG_Impl::EGLImpl, the same functions
// Exporting/Definitions.cpp exports), so a vendor call and a direct call cannot drift apart.
#include <Includes.h>
#include <glvnd/libeglabi.h>
#include "../EGLImpl.h"
#include "../EGLPlatformExtensions.h"
// Defined in Definitions.cpp; eglext.h declares them only under EGL_EGLEXT_PROTOTYPES.
extern "C" EGLImage eglCreateImageKHR(EGLDisplay dpy, EGLContext ctx, EGLenum target, EGLClientBuffer buffer,
const EGLint* attrib_list);
extern "C" EGLBoolean eglDestroyImageKHR(EGLDisplay dpy, EGLImage image);
namespace {
using namespace MobileGL;
const __EGLapiExports* g_eglExports = nullptr;
// The platforms this vendor serves. GBM is the one that matters here: the compositor asks for
// a display on the GBM device it opened, and a vendor that does not name the platform is not
// asked at all. This side treats a GBM device as it treats every other native display - the
// frames come from the display host, not from the device - so claiming the platform is honest
// rather than optimistic.
constexpr const char* kPlatformExtensions = MG_Impl::EGLImpl::kPlatformExtensionString;
// glvnd asks for the client-only extension list separately from the platform one; the split is
// its own invention, and this side answers both from the same query.
const char* EGLAPIENTRY VendorQueryString(EGLDisplay dpy, EGLint name) {
return MG_Impl::EGLImpl::QueryString(dpy, name);
}
EGLDisplay EGLAPIENTRY VendorGetPlatformDisplay(EGLenum platform, void* native_display,
const EGLAttrib* attrib_list) {
// EGL_NONE means the application called eglGetDisplay(EGL_DEFAULT_DISPLAY): the vendor is
// being asked to name its own default display.
if (platform == EGL_NONE) {
return MG_Impl::EGLImpl::GetDisplay(EGL_DEFAULT_DISPLAY);
}
return MG_Impl::EGLImpl::GetPlatformDisplay(platform, native_display, attrib_list);
}
EGLBoolean EGLAPIENTRY VendorGetSupportsAPI(EGLenum api) {
// What this library answers for EGL_CLIENT_APIS, said as a question.
return (api == EGL_OPENGL_ES_API || api == EGL_OPENGL_API) ? EGL_TRUE : EGL_FALSE;
}
const char* EGLAPIENTRY VendorGetVendorString(int name) {
if (name == __EGL_VENDOR_STRING_PLATFORM_EXTENSIONS) {
return kPlatformExtensions;
}
return nullptr;
}
// GLVND ASKS FOR EVERY EGL ENTRY POINT BY NAME, AND DROPS THE VENDOR IF ONE IS MISSING: the
// first eight functions the table below names are looked up before this library is ever asked
// for a display, and a single null answer means glvnd quietly stops considering it - the
// library is loaded, __egl_Main has run, and nothing is ever dispatched to it.
struct EntryPoint {
const char* name;
void* address;
};
// ---- the device family -------------------------------------------------------------------
//
// A compositor does not ask for a display and hope. It asks which devices exist, reads the DRM
// node off the one it wants, and only then asks for a display ON that device - so a vendor that
// answers "no devices" is one it cannot use, however well the vendor draws. The message it
// gives is "no usable DRM render device", which names where it stopped rather than why.
//
// This implementation therefore names one device: the render node of the machine it runs on.
// That is an honest answer even though the frames do not come from that node - they come from
// the display host on the other end of the socket, which is why every surface here is a host
// frame - because the question is which device this display is associated with, and the answer
// is what lets the compositor match its own node to the display it is about to draw into.
const char* DrmRenderNode() {
// MOBILEGL_DEVICE_DRM_NODE names another node: a machine with more than one GPU, or a test
// that wants the mismatch refused rather than papered over.
const char* configured = std::getenv("MOBILEGL_DEVICE_DRM_NODE");
return (configured != nullptr && configured[0] != '\0') ? configured : "/dev/dri/renderD128";
}
// One device, one identity. A pointer to a private object rather than a number, so that a
// device from somewhere else cannot compare equal to this one by accident.
struct DeviceToken { int unused; };
DeviceToken g_deviceToken;
EGLBoolean EGLAPIENTRY VendorQueryDevicesEXT(EGLint max_devices, EGLDeviceEXT* devices, EGLint* num_devices) {
if (num_devices == nullptr) {
return EGL_FALSE;
}
*num_devices = 1;
if (devices != nullptr && max_devices >= 1) {
devices[0] = reinterpret_cast<EGLDeviceEXT>(&g_deviceToken);
}
return EGL_TRUE;
}
EGLBoolean EGLAPIENTRY VendorQueryDeviceAttribEXT(EGLDeviceEXT device, EGLint attribute, EGLAttrib* value) {
if (device != reinterpret_cast<EGLDeviceEXT>(&g_deviceToken) || value == nullptr) {
return EGL_FALSE;
}
switch (attribute) {
case EGL_DRM_DEVICE_FILE_EXT:
case EGL_DRM_RENDER_NODE_FILE_EXT:
*value = reinterpret_cast<EGLAttrib>(DrmRenderNode());
return EGL_TRUE;
default:
return EGL_FALSE;
}
}
const char* EGLAPIENTRY VendorQueryDeviceStringEXT(EGLDeviceEXT device, EGLint name) {
if (device != reinterpret_cast<EGLDeviceEXT>(&g_deviceToken)) {
return nullptr;
}
// EGL_EXT_device_drm asks for the node as a STRING, not as an attribute, and that query is
// the one a compositor actually makes before it opens anything - it is how the display it is
// about to create is matched to a node it already holds. Answering only EGL_EXTENSIONS here
// leaves that lookup empty, and the failure surfaces one layer down as the node "not being
// openable", which is the compositor's wording for having had nothing to open.
if (name == EGL_DRM_DEVICE_FILE_EXT || name == EGL_DRM_RENDER_NODE_FILE_EXT) {
return DrmRenderNode();
}
if (name == EGL_EXTENSIONS) {
return "EGL_EXT_device_drm EGL_EXT_device_drm_render_node";
}
return nullptr;
}
EGLBoolean EGLAPIENTRY VendorQueryDisplayAttribEXT(EGLDisplay dpy, EGLint attribute, EGLAttrib* value) {
(void)dpy;
if (value == nullptr) {
return EGL_FALSE;
}
if (attribute == EGL_DEVICE_EXT) {
*value = reinterpret_cast<EGLAttrib>(&g_deviceToken);
return EGL_TRUE;
}
return EGL_FALSE;
}
// Three of the names glvnd insists on are debug extensions this implementation does not
// provide, and glvnd insists on them all the same - a vendor that answers null for any of them
// is dropped. They are answered honestly rather than pretended: KHR_debug is not in the
// extension strings above, so a caller that reads them will not call these at all.
EGLint EGLAPIENTRY VendorDebugMessageControlKHR(EGLDEBUGPROCKHR callback, const EGLAttrib* attrib_list) {
(void)callback;
(void)attrib_list;
return EGL_FALSE;
}
EGLBoolean EGLAPIENTRY VendorQueryDebugKHR(EGLint attribute, EGLAttrib* value) {
(void)attribute;
(void)value;
return EGL_FALSE;
}
EGLint EGLAPIENTRY VendorLabelObjectKHR(EGLDisplay dpy, EGLenum objectType, EGLObjectKHR object, EGLLabelKHR label) {
(void)dpy;
(void)objectType;
(void)object;
(void)label;
return EGL_BAD_PARAMETER;
}
const EntryPoint kEglEntryPoints[] = {
// The core of EGL 1.5, every one of which glvnd looks up by name before it considers this
// library usable.
{"eglInitialize", reinterpret_cast<void*>(eglInitialize)},
{"eglChooseConfig", reinterpret_cast<void*>(eglChooseConfig)},
{"eglCopyBuffers", reinterpret_cast<void*>(eglCopyBuffers)},
{"eglCreateContext", reinterpret_cast<void*>(eglCreateContext)},
{"eglCreatePbufferSurface", reinterpret_cast<void*>(eglCreatePbufferSurface)},
{"eglCreatePixmapSurface", reinterpret_cast<void*>(eglCreatePixmapSurface)},
{"eglCreateWindowSurface", reinterpret_cast<void*>(eglCreateWindowSurface)},
{"eglDestroyContext", reinterpret_cast<void*>(eglDestroyContext)},
{"eglDestroySurface", reinterpret_cast<void*>(eglDestroySurface)},
{"eglGetConfigAttrib", reinterpret_cast<void*>(eglGetConfigAttrib)},
{"eglGetConfigs", reinterpret_cast<void*>(eglGetConfigs)},
{"eglMakeCurrent", reinterpret_cast<void*>(eglMakeCurrent)},
{"eglQueryContext", reinterpret_cast<void*>(eglQueryContext)},
{"eglQuerySurface", reinterpret_cast<void*>(eglQuerySurface)},
{"eglSwapBuffers", reinterpret_cast<void*>(eglSwapBuffers)},
{"eglTerminate", reinterpret_cast<void*>(eglTerminate)},
{"eglWaitGL", reinterpret_cast<void*>(eglWaitGL)},
{"eglWaitNative", reinterpret_cast<void*>(eglWaitNative)},
{"eglBindTexImage", reinterpret_cast<void*>(eglBindTexImage)},
{"eglReleaseTexImage", reinterpret_cast<void*>(eglReleaseTexImage)},
{"eglSurfaceAttrib", reinterpret_cast<void*>(eglSurfaceAttrib)},
{"eglSwapInterval", reinterpret_cast<void*>(eglSwapInterval)},
{"eglBindAPI", reinterpret_cast<void*>(eglBindAPI)},
{"eglCreatePbufferFromClientBuffer", reinterpret_cast<void*>(eglCreatePbufferFromClientBuffer)},
{"eglReleaseThread", reinterpret_cast<void*>(eglReleaseThread)},
{"eglWaitClient", reinterpret_cast<void*>(eglWaitClient)},
{"eglGetError", reinterpret_cast<void*>(eglGetError)},
{"eglCreateSync", reinterpret_cast<void*>(eglCreateSync)},
{"eglDestroySync", reinterpret_cast<void*>(eglDestroySync)},
{"eglClientWaitSync", reinterpret_cast<void*>(eglClientWaitSync)},
{"eglGetSyncAttrib", reinterpret_cast<void*>(eglGetSyncAttrib)},
{"eglWaitSync", reinterpret_cast<void*>(eglWaitSync)},
{"eglCreateImage", reinterpret_cast<void*>(eglCreateImage)},
{"eglDestroyImage", reinterpret_cast<void*>(eglDestroyImage)},
{"eglCreateImageKHR", reinterpret_cast<void*>(eglCreateImageKHR)},
{"eglDestroyImageKHR", reinterpret_cast<void*>(eglDestroyImageKHR)},
{"eglCreatePlatformWindowSurface", reinterpret_cast<void*>(eglCreatePlatformWindowSurface)},
{"eglCreatePlatformPixmapSurface", reinterpret_cast<void*>(eglCreatePlatformPixmapSurface)},
// The platform and current-state queries: not in glvnd's required list, but the names an
// application that talks to a vendor through a dispatcher will ask for by name.
{"eglGetDisplay", reinterpret_cast<void*>(eglGetDisplay)},
{"eglGetCurrentDisplay", reinterpret_cast<void*>(eglGetCurrentDisplay)},
{"eglGetCurrentSurface", reinterpret_cast<void*>(eglGetCurrentSurface)},
{"eglGetCurrentContext", reinterpret_cast<void*>(eglGetCurrentContext)},
{"eglQueryAPI", reinterpret_cast<void*>(eglQueryAPI)},
{"eglGetProcAddress", reinterpret_cast<void*>(eglGetProcAddress)},
// The two queries whose answer depends on which side of glvnd is asking.
{"eglQueryString", reinterpret_cast<void*>(VendorQueryString)},
{"eglGetPlatformDisplay", reinterpret_cast<void*>(VendorGetPlatformDisplay)},
{"eglGetPlatformDisplayEXT", reinterpret_cast<void*>(VendorGetPlatformDisplay)},
// What glvnd requires and this implementation does not otherwise have.
{"eglQueryDevicesEXT", reinterpret_cast<void*>(VendorQueryDevicesEXT)},
{"eglQueryDeviceAttribEXT", reinterpret_cast<void*>(VendorQueryDeviceAttribEXT)},
{"eglQueryDeviceStringEXT", reinterpret_cast<void*>(VendorQueryDeviceStringEXT)},
{"eglQueryDisplayAttribEXT", reinterpret_cast<void*>(VendorQueryDisplayAttribEXT)},
{"eglDebugMessageControlKHR", reinterpret_cast<void*>(VendorDebugMessageControlKHR)},
{"eglQueryDebugKHR", reinterpret_cast<void*>(VendorQueryDebugKHR)},
{"eglLabelObjectKHR", reinterpret_cast<void*>(VendorLabelObjectKHR)},
};
// THE SAME ANSWER TO BOTH OF GLVND'S QUESTIONS, and that is not a shortcut - glvnd asks them
// from two places and a library that answers one of them has a hole the size of the other:
//
// getProcAddress is asked for every entry point glvnd builds its own static EGL table
// from, so a null here drops the vendor outright.
// getDispatchAddress is what glvnd falls back to for eglGetProcAddress on any name it does
// not know itself - which is every extension entry point that is not in
// its list, the device queries among them. A vendor that answers these
// from a GL-only table hands the application a null for a function the
// vendor implements, and the caller reports the extension as missing.
void* LookupEntryPoint(const char* procName) {
if (procName == nullptr) {
return nullptr;
}
for (const EntryPoint& entry : kEglEntryPoints) {
if (std::strcmp(entry.name, procName) == 0) {
return entry.address;
}
}
// Anything that is not EGL is a GL or GLES entry point, and the tree's own resolver is the
// table for those.
return reinterpret_cast<void*>(MG_Impl::EGLImpl::GetProcAddress(procName));
}
void* EGLAPIENTRY VendorGetProcAddress(const char* procName) { return LookupEntryPoint(procName); }
// glvnd hands a vendor the index each EGL entry point occupies in its dispatch table, so that a
// vendor whose entry points are thin wrappers around glvnd can call back into the right slot.
// This library's entry points ARE the implementation - they never re-enter glvnd - so there is
// nothing to record; the callback exists because glvnd refuses a vendor that does not provide
// one, and refusing it means the library is loaded and never dispatched to.
void EGLAPIENTRY VendorSetDispatchIndex(const char* procName, int index) {
(void)procName;
(void)index;
}
void* EGLAPIENTRY VendorGetDispatchAddress(const char* procName) { return LookupEntryPoint(procName); }
} // namespace
extern "C" MOBILEGL_EGL_API EGLBoolean __egl_Main(uint32_t version, const __EGLapiExports* exports,
__EGLvendorInfo* vendor, __EGLapiImports* imports) {
(void)vendor;
if (EGL_VENDOR_ABI_GET_MAJOR_VERSION(version) != EGL_VENDOR_ABI_MAJOR_VERSION) {
// A vendor that does not know the ABI it is being handed must say so, not guess: the
// structures on either side are the ABI.
MGLOG_E("__egl_Main: unsupported glvnd vendor ABI version 0x%08x (this library implements %u.%u)", version,
static_cast<unsigned>(EGL_VENDOR_ABI_MAJOR_VERSION),
static_cast<unsigned>(EGL_VENDOR_ABI_MINOR_VERSION));
return EGL_FALSE;
}
if (exports == nullptr || imports == nullptr) {
MGLOG_E("__egl_Main: glvnd handed no exports/imports table");
return EGL_FALSE;
}
g_eglExports = exports;
imports->getPlatformDisplay = VendorGetPlatformDisplay;
imports->getSupportsAPI = VendorGetSupportsAPI;
imports->getVendorString = VendorGetVendorString;
imports->getProcAddress = VendorGetProcAddress;
imports->getDispatchAddress = VendorGetDispatchAddress;
imports->setDispatchIndex = VendorSetDispatchIndex;
MGLOG_I("glvnd vendor: MobileGL is serving EGL through libglvnd (ABI %u.%u, platforms: %s)",
static_cast<unsigned>(EGL_VENDOR_ABI_MAJOR_VERSION), static_cast<unsigned>(EGL_VENDOR_ABI_MINOR_VERSION),
kPlatformExtensions);
return EGL_TRUE;
}
+433
View File
@@ -0,0 +1,433 @@
// MobileGL - MobileGL/MG_Impl/EGLImpl/WaylandWindow.cpp
// 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
#include "WaylandWindow.h"
#if MOBILEGL_WAYLAND_WINDOWS
#include <MG_Impl/GLImpl/Buffer/GL_Buffer.h>
#include <MG_Impl/GLImpl/Framebuffer/GL_Framebuffer.h>
#include <MG_Impl/GLImpl/Getter/GL_Getter.h>
#include <MG_Impl/GLImpl/RenderState/GL_RenderState.h>
#include <MG_Util/Debug/Log.h>
#include <cerrno>
#include <cstdarg>
#include <cstdint>
#include <cstring>
#include <dlfcn.h>
#include <fcntl.h>
#include <mutex>
#include <sys/mman.h>
#include <sys/uio.h>
#include <unistd.h>
namespace MobileGL::MG_Impl::EGLImpl {
void ReadBackFrameBGRA(EGLint width, EGLint height, Vector<Uint8>& scratch, Uint8* dst, SizeT dstStride) {
namespace GL = MG_Impl::GLImpl;
GLint readFramebuffer = 0, packBuffer = 0, alignment = 4, rowLength = 0, skipRows = 0, skipPixels = 0;
GL::GetIntegerv(GL_READ_FRAMEBUFFER_BINDING, &readFramebuffer);
GL::GetIntegerv(GL_PIXEL_PACK_BUFFER_BINDING, &packBuffer);
GL::GetIntegerv(GL_PACK_ALIGNMENT, &alignment);
GL::GetIntegerv(GL_PACK_ROW_LENGTH, &rowLength);
GL::GetIntegerv(GL_PACK_SKIP_ROWS, &skipRows);
GL::GetIntegerv(GL_PACK_SKIP_PIXELS, &skipPixels);
if (readFramebuffer != 0) GL::BindFramebuffer(GL_READ_FRAMEBUFFER, 0);
if (packBuffer != 0) GL::BindBuffer(GL_PIXEL_PACK_BUFFER, 0);
GL::PixelStorei(GL_PACK_ALIGNMENT, 4);
GL::PixelStorei(GL_PACK_ROW_LENGTH, 0);
GL::PixelStorei(GL_PACK_SKIP_ROWS, 0);
GL::PixelStorei(GL_PACK_SKIP_PIXELS, 0);
const SizeT rowBytes = static_cast<SizeT>(width) * 4;
scratch.resize(rowBytes * static_cast<SizeT>(height));
GL::ReadPixels(0, 0, width, height, GL_RGBA, GL_UNSIGNED_BYTE, scratch.data());
GL::PixelStorei(GL_PACK_ALIGNMENT, alignment);
GL::PixelStorei(GL_PACK_ROW_LENGTH, rowLength);
GL::PixelStorei(GL_PACK_SKIP_ROWS, skipRows);
GL::PixelStorei(GL_PACK_SKIP_PIXELS, skipPixels);
if (packBuffer != 0) GL::BindBuffer(GL_PIXEL_PACK_BUFFER, static_cast<GLuint>(packBuffer));
if (readFramebuffer != 0) GL::BindFramebuffer(GL_READ_FRAMEBUFFER, static_cast<GLuint>(readFramebuffer));
for (EGLint y = 0; y < height; ++y) {
const Uint8* src = scratch.data() + static_cast<SizeT>(height - 1 - y) * rowBytes;
Uint8* row = dst + static_cast<SizeT>(y) * dstStride;
for (EGLint x = 0; x < width; ++x) {
row[x * 4 + 0] = src[x * 4 + 2];
row[x * 4 + 1] = src[x * 4 + 1];
row[x * 4 + 2] = src[x * 4 + 0];
row[x * 4 + 3] = src[x * 4 + 3];
}
}
}
} // namespace MobileGL::MG_Impl::EGLImpl
namespace MobileGL::MG_Impl::EGLImpl::Wayland {
namespace {
// libwayland-client's types are opaque here: everything goes through wl_proxy_* and the
// exported interface descriptions, and nothing reads a field of either.
struct wl_proxy;
struct wl_interface;
struct wl_event_queue;
// wayland-egl-backend.h, WL_EGL_WINDOW_VERSION 3: the struct libwayland-egl allocates for
// wl_egl_window_create and the driver reads. The ABI is versioned by its first word.
struct EglWindow {
const intptr_t version;
int width;
int height;
int dx;
int dy;
int attached_width;
int attached_height;
void* driver_private;
void (*resize_callback)(EglWindow*, void*);
void (*destroy_window_callback)(void*);
wl_proxy* surface;
};
constexpr intptr_t kEglWindowVersion = 3;
// Request opcodes (wayland.xml, the order the requests are declared in).
constexpr uint32_t kDisplayGetRegistry = 1;
constexpr uint32_t kRegistryBind = 0;
constexpr uint32_t kShmCreatePool = 0;
constexpr uint32_t kShmPoolCreateBuffer = 0;
constexpr uint32_t kShmPoolDestroy = 1;
constexpr uint32_t kBufferDestroy = 0;
constexpr uint32_t kSurfaceAttach = 1;
constexpr uint32_t kSurfaceDamage = 2;
constexpr uint32_t kSurfaceCommit = 6;
constexpr uint32_t kSurfaceDamageBuffer = 9; // since wl_surface version 4
constexpr uint32_t kMarshalFlagDestroy = 1u << 0;
// wl_shm formats. The two every compositor must support; byte order in memory is
// B, G, R, A for both (little-endian 0xAARRGGBB).
constexpr uint32_t kShmFormatArgb8888 = 0;
constexpr uint32_t kShmFormatXrgb8888 = 1;
struct Api {
Bool loaded = false;
const wl_interface* displayInterface = nullptr;
const wl_interface* registryInterface = nullptr;
const wl_interface* shmInterface = nullptr;
const wl_interface* shmPoolInterface = nullptr;
const wl_interface* bufferInterface = nullptr;
wl_proxy* (*marshalFlags)(wl_proxy*, uint32_t, const wl_interface*, uint32_t, uint32_t, ...) = nullptr;
int (*addListener)(wl_proxy*, void (**)(void), void*) = nullptr;
void (*destroy)(wl_proxy*) = nullptr;
uint32_t (*getVersion)(wl_proxy*) = nullptr;
void* (*createWrapper)(void*) = nullptr;
void (*wrapperDestroy)(void*) = nullptr;
void (*setQueue)(wl_proxy*, wl_event_queue*) = nullptr;
wl_event_queue* (*createQueue)(void*) = nullptr;
void (*queueDestroy)(wl_event_queue*) = nullptr;
int (*roundtripQueue)(void*, wl_event_queue*) = nullptr;
int (*dispatchQueue)(void*, wl_event_queue*) = nullptr;
int (*dispatchQueuePending)(void*, wl_event_queue*) = nullptr;
int (*flush)(void*) = nullptr;
};
// Loaded once. RTLD_NOLOAD first: a Wayland client has the library already, and the copy
// it is using is the one whose wl_display this must talk to.
const Api& WaylandApi() {
static Api api;
static std::once_flag once;
std::call_once(once, [] {
void* lib = dlopen("libwayland-client.so.0", RTLD_NOW | RTLD_NOLOAD);
if (lib == nullptr) lib = dlopen("libwayland-client.so.0", RTLD_NOW);
if (lib == nullptr) return;
const auto sym = [lib](const char* name) { return dlsym(lib, name); };
api.displayInterface = static_cast<const wl_interface*>(sym("wl_display_interface"));
api.registryInterface = static_cast<const wl_interface*>(sym("wl_registry_interface"));
api.shmInterface = static_cast<const wl_interface*>(sym("wl_shm_interface"));
api.shmPoolInterface = static_cast<const wl_interface*>(sym("wl_shm_pool_interface"));
api.bufferInterface = static_cast<const wl_interface*>(sym("wl_buffer_interface"));
api.marshalFlags = reinterpret_cast<decltype(api.marshalFlags)>(sym("wl_proxy_marshal_flags"));
api.addListener = reinterpret_cast<decltype(api.addListener)>(sym("wl_proxy_add_listener"));
api.destroy = reinterpret_cast<decltype(api.destroy)>(sym("wl_proxy_destroy"));
api.getVersion = reinterpret_cast<decltype(api.getVersion)>(sym("wl_proxy_get_version"));
api.createWrapper = reinterpret_cast<decltype(api.createWrapper)>(sym("wl_proxy_create_wrapper"));
api.wrapperDestroy = reinterpret_cast<decltype(api.wrapperDestroy)>(sym("wl_proxy_wrapper_destroy"));
api.setQueue = reinterpret_cast<decltype(api.setQueue)>(sym("wl_proxy_set_queue"));
api.createQueue = reinterpret_cast<decltype(api.createQueue)>(sym("wl_display_create_queue"));
api.queueDestroy = reinterpret_cast<decltype(api.queueDestroy)>(sym("wl_event_queue_destroy"));
api.roundtripQueue = reinterpret_cast<decltype(api.roundtripQueue)>(sym("wl_display_roundtrip_queue"));
api.dispatchQueue = reinterpret_cast<decltype(api.dispatchQueue)>(sym("wl_display_dispatch_queue"));
api.dispatchQueuePending =
reinterpret_cast<decltype(api.dispatchQueuePending)>(sym("wl_display_dispatch_queue_pending"));
api.flush = reinterpret_cast<decltype(api.flush)>(sym("wl_display_flush"));
api.loaded = api.displayInterface && api.registryInterface && api.shmInterface &&
api.shmPoolInterface && api.bufferInterface && api.marshalFlags && api.addListener && api.destroy &&
api.getVersion && api.createWrapper && api.wrapperDestroy && api.setQueue &&
api.createQueue && api.queueDestroy && api.roundtripQueue && api.dispatchQueue &&
api.dispatchQueuePending && api.flush;
if (!api.loaded) MGLOG_E("Wayland: libwayland-client.so.0 lacks an entry point this library uses");
});
return api;
}
struct ShmBuffer {
wl_proxy* buffer = nullptr;
void* map = nullptr;
SizeT size = 0;
EGLint width = 0;
EGLint height = 0;
EGLint stride = 0;
Bool busy = false;
};
// The compositor is done with a buffer: it may be drawn into again.
void OnBufferRelease(void* data, wl_proxy*) { static_cast<ShmBuffer*>(data)->busy = false; }
void (*const kBufferListener[])(void) = {reinterpret_cast<void (*)(void)>(&OnBufferRelease)};
} // namespace
struct WindowSurface::Impl {
void* display = nullptr;
EglWindow* window = nullptr;
wl_event_queue* queue = nullptr;
void* displayWrapper = nullptr;
wl_proxy* registry = nullptr;
wl_proxy* shm = nullptr;
uint32_t format = kShmFormatXrgb8888;
EGLint width = 0;
EGLint height = 0;
// Three: one on the glass, one the compositor may still be reading, one to draw into.
ShmBuffer buffers[3];
Vector<Uint8> scratch;
~Impl() {
const Api& api = WaylandApi();
for (auto& buffer : buffers) Release(buffer);
// Neither wl_shm (version 1) nor wl_registry has a destroy request: the proxies go
// on this side only.
if (shm) api.destroy(shm);
if (registry) api.destroy(registry);
if (displayWrapper) api.wrapperDestroy(displayWrapper);
if (queue) api.queueDestroy(queue);
if (window && window->driver_private == this) {
window->driver_private = nullptr;
window->destroy_window_callback = nullptr;
}
}
void Release(ShmBuffer& buffer) {
const Api& api = WaylandApi();
if (buffer.buffer) api.marshalFlags(buffer.buffer, kBufferDestroy, nullptr, api.getVersion(buffer.buffer),
kMarshalFlagDestroy);
if (buffer.map) munmap(buffer.map, buffer.size);
buffer = ShmBuffer{};
}
Bool Allocate(ShmBuffer& buffer) {
const Api& api = WaylandApi();
Release(buffer);
const EGLint stride = width * 4;
const SizeT size = static_cast<SizeT>(stride) * static_cast<SizeT>(height);
const int fd = memfd_create("mobilegl-wayland", MFD_CLOEXEC);
if (fd < 0) {
MGLOG_E("Wayland: memfd_create for a %dx%d buffer failed: %s", width, height, std::strerror(errno));
return false;
}
if (ftruncate(fd, static_cast<off_t>(size)) != 0) {
MGLOG_E("Wayland: sizing a %dx%d buffer failed: %s", width, height, std::strerror(errno));
close(fd);
return false;
}
void* map = mmap(nullptr, size, PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0);
if (map == MAP_FAILED) {
MGLOG_E("Wayland: mapping a %dx%d buffer failed: %s", width, height, std::strerror(errno));
close(fd);
return false;
}
// One pool per buffer, destroyed at once: the buffer keeps the memory alive.
wl_proxy* pool = api.marshalFlags(shm, kShmCreatePool, api.shmPoolInterface, api.getVersion(shm), 0, nullptr,
fd, static_cast<int32_t>(size));
close(fd);
if (pool == nullptr) {
munmap(map, size);
return false;
}
wl_proxy* wlBuffer = api.marshalFlags(pool, kShmPoolCreateBuffer, api.bufferInterface, api.getVersion(pool),
0, nullptr, 0, width, height, stride, format);
api.marshalFlags(pool, kShmPoolDestroy, nullptr, api.getVersion(pool), kMarshalFlagDestroy);
if (wlBuffer == nullptr) {
munmap(map, size);
return false;
}
buffer.buffer = wlBuffer;
buffer.map = map;
buffer.size = size;
buffer.width = width;
buffer.height = height;
buffer.stride = stride;
buffer.busy = false;
api.addListener(wlBuffer, const_cast<void (**)(void)>(kBufferListener), &buffer);
return true;
}
// A buffer the compositor is not reading: the events already received first, then (all
// three still held) a blocking wait for the next release.
ShmBuffer* NextFree() {
const Api& api = WaylandApi();
for (;;) {
if (api.dispatchQueuePending(display, queue) < 0) return nullptr;
for (auto& buffer : buffers) {
if (buffer.buffer == nullptr || buffer.busy) continue;
if (buffer.width != width || buffer.height != height) {
if (!Allocate(buffer)) return nullptr;
}
return &buffer;
}
for (auto& buffer : buffers) {
if (buffer.buffer == nullptr) return Allocate(buffer) ? &buffer : nullptr;
}
if (api.dispatchQueue(display, queue) < 0) return nullptr;
}
}
};
namespace {
void OnRegistryGlobal(void* data, wl_proxy* registry, uint32_t name, const char* interface, uint32_t) {
auto* impl = static_cast<WindowSurface::Impl*>(data);
if (impl->shm != nullptr || std::strcmp(interface, "wl_shm") != 0) return;
const Api& api = WaylandApi();
impl->shm = api.marshalFlags(registry, kRegistryBind, api.shmInterface, 1, 0, name, "wl_shm", 1u, nullptr);
}
void OnRegistryGlobalRemove(void*, wl_proxy*, uint32_t) {}
void (*const kRegistryListener[])(void) = {reinterpret_cast<void (*)(void)>(&OnRegistryGlobal),
reinterpret_cast<void (*)(void)>(&OnRegistryGlobalRemove)};
// The application destroyed its wl_egl_window first: nothing may be read from it after this.
void OnWindowDestroyed(void* data) { static_cast<WindowSurface::Impl*>(data)->window = nullptr; }
} // namespace
namespace {
// Copies `size` bytes at `address` of this process, or answers false where nothing is
// mapped - the kernel does the reading, so a wild pointer is an error code, not a fault.
Bool ReadOwnMemory(const void* address, void* out, SizeT size) {
iovec local{out, size};
iovec remote{const_cast<void*>(address), size};
return process_vm_readv(getpid(), &local, 1, &remote, 1, 0) == static_cast<ssize_t>(size);
}
} // namespace
Bool IsWaylandDisplay(Uint64 nativeDisplay, EGLenum platform) {
if (platform == kPlatformWayland) return true;
if (nativeDisplay == 0 || (platform != EGL_NONE && platform != 0)) return false;
const Api& api = WaylandApi();
if (!api.loaded) return false;
const void* interface = *reinterpret_cast<const void* const*>(static_cast<uintptr_t>(nativeDisplay));
if (interface == api.displayInterface) return true;
// AN APPLICATION WITH ITS OWN COPY OF LIBWAYLAND-CLIENT (Chromium links one in statically)
// has its own wl_display_interface, at its own address. The interface is still the
// protocol's: a wl_interface whose first member names it "wl_display". Whatever else the
// first word of a native display is (a gbm_device's is a function address), the probe
// only reads through the kernel.
const char* name = nullptr;
char text[sizeof("wl_display")] = {};
return interface != nullptr && ReadOwnMemory(interface, &name, sizeof(name)) && name != nullptr &&
ReadOwnMemory(name, text, sizeof(text)) && std::memcmp(text, "wl_display", sizeof(text)) == 0;
}
Bool IsWaylandWindow(const void* window) {
if (window == nullptr) return false;
const auto* eglWindow = static_cast<const EglWindow*>(window);
return eglWindow->version >= kEglWindowVersion && eglWindow->surface != nullptr;
}
Bool WindowSize(const void* window, EGLint* width, EGLint* height) {
if (!IsWaylandWindow(window)) return false;
const auto* eglWindow = static_cast<const EglWindow*>(window);
*width = eglWindow->width;
*height = eglWindow->height;
return eglWindow->width > 0 && eglWindow->height > 0;
}
WindowSurface::WindowSurface(UniquePtr<Impl> impl) : m_impl(std::move(impl)) {}
WindowSurface::~WindowSurface() = default;
UniquePtr<WindowSurface> WindowSurface::Create(void* wlDisplay, void* wlEglWindow, EGLint width, EGLint height,
Bool hasAlpha) {
const Api& api = WaylandApi();
if (!api.loaded) {
MGLOG_E("Wayland: a window surface needs libwayland-client.so.0, and it could not be used");
return nullptr;
}
if (wlDisplay == nullptr || !IsWaylandWindow(wlEglWindow) || width <= 0 || height <= 0) return nullptr;
auto impl = MakeUnique<Impl>();
impl->display = wlDisplay;
impl->window = static_cast<EglWindow*>(wlEglWindow);
impl->width = width;
impl->height = height;
impl->format = hasAlpha ? kShmFormatArgb8888 : kShmFormatXrgb8888;
// A queue of this surface's own, so the application's dispatching never sees these events
// and this never dispatches the application's.
impl->queue = api.createQueue(wlDisplay);
impl->displayWrapper = api.createWrapper(wlDisplay);
if (impl->queue == nullptr || impl->displayWrapper == nullptr) return nullptr;
api.setQueue(static_cast<wl_proxy*>(impl->displayWrapper), impl->queue);
auto* wrapper = static_cast<wl_proxy*>(impl->displayWrapper);
impl->registry = api.marshalFlags(wrapper, kDisplayGetRegistry, api.registryInterface, api.getVersion(wrapper),
0, nullptr);
if (impl->registry == nullptr) return nullptr;
api.addListener(impl->registry, const_cast<void (**)(void)>(kRegistryListener), impl.get());
if (api.roundtripQueue(wlDisplay, impl->queue) < 0 || impl->shm == nullptr) {
MGLOG_E("Wayland: the compositor offers no wl_shm; a window surface cannot be presented");
return nullptr;
}
impl->window->driver_private = impl.get();
impl->window->destroy_window_callback = &OnWindowDestroyed;
MGLOG_I("Wayland: window surface %dx%d presented through wl_shm (%s)", width, height,
hasAlpha ? "ARGB8888" : "XRGB8888");
return UniquePtr<WindowSurface>(new WindowSurface(std::move(impl)));
}
Bool WindowSurface::TakeResize(EGLint* width, EGLint* height) {
Impl& impl = *m_impl;
if (impl.window == nullptr || impl.window->width <= 0 || impl.window->height <= 0) return false;
if (impl.window->width == impl.width && impl.window->height == impl.height) return false;
impl.width = impl.window->width;
impl.height = impl.window->height;
*width = impl.width;
*height = impl.height;
return true;
}
Bool WindowSurface::Present() {
Impl& impl = *m_impl;
if (impl.window == nullptr) return false;
const Api& api = WaylandApi();
ShmBuffer* buffer = impl.NextFree();
if (buffer == nullptr) {
MGLOG_E_ONCE("Wayland: no wl_shm buffer could be taken for the frame; it is not shown");
return false;
}
// GL's rows run bottom-up and RGBA; a wl_shm buffer's run top-down and B, G, R, A.
ReadBackFrameBGRA(impl.width, impl.height, impl.scratch, static_cast<Uint8*>(buffer->map),
static_cast<SizeT>(buffer->stride));
wl_proxy* surface = impl.window->surface;
const uint32_t surfaceVersion = api.getVersion(surface);
api.marshalFlags(surface, kSurfaceAttach, nullptr, surfaceVersion, 0, buffer->buffer, impl.window->dx,
impl.window->dy);
if (surfaceVersion >= 4) {
api.marshalFlags(surface, kSurfaceDamageBuffer, nullptr, surfaceVersion, 0, 0, 0, impl.width,
impl.height);
} else {
api.marshalFlags(surface, kSurfaceDamage, nullptr, surfaceVersion, 0, 0, 0, impl.width, impl.height);
}
api.marshalFlags(surface, kSurfaceCommit, nullptr, surfaceVersion, 0);
buffer->busy = true;
impl.window->attached_width = impl.width;
impl.window->attached_height = impl.height;
impl.window->dx = 0;
impl.window->dy = 0;
api.flush(impl.display);
return true;
}
} // namespace MobileGL::MG_Impl::EGLImpl::Wayland
#endif
+87
View File
@@ -0,0 +1,87 @@
// MobileGL - MobileGL/MG_Impl/EGLImpl/WaylandWindow.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
// WAYLAND WINDOW SURFACES. A Wayland client hands eglCreateWindowSurface a wl_egl_window, and what
// the compositor shows is whatever wl_buffer the client attaches to that window's wl_surface. The
// pixels of a MobileGL frame are drawn where the backend is - on a split build, in another process
// on another OS - so a window surface here is a pbuffer the backend draws into, plus this: at every
// swap the frame is read back and attached to the wl_surface as a wl_shm buffer.
//
// libwayland-client is loaded at run time (it is already in every Wayland client's process), so the
// library neither links it nor needs its headers to build.
#include <Includes.h>
#if defined(__linux__) && !defined(__ANDROID__)
#define MOBILEGL_WAYLAND_WINDOWS 1
#else
#define MOBILEGL_WAYLAND_WINDOWS 0
#endif
#if MOBILEGL_WAYLAND_WINDOWS
#include <EGL/egl.h>
namespace MobileGL::MG_Impl::EGLImpl {
// The current context's default-framebuffer frame, `width` x `height`, written top-down in
// B, G, R, A byte order (a wl_shm ARGB8888 buffer and a 24/32-bit little-endian X image both
// hold that) to `dst`, one row every `dstStride` bytes. Through this library's own entry
// points: the application's read framebuffer, pack buffer and pack state are saved and put
// back, so the readback is invisible to it. `scratch` is the caller's, reused across frames.
void ReadBackFrameBGRA(EGLint width, EGLint height, Vector<Uint8>& scratch, Uint8* dst, SizeT dstStride);
} // namespace MobileGL::MG_Impl::EGLImpl
namespace MobileGL::MG_Impl::EGLImpl::Wayland {
// EGL_PLATFORM_WAYLAND_KHR and EGL_PLATFORM_WAYLAND_EXT are the same enum.
inline constexpr EGLenum kPlatformWayland = 0x31D8;
// Whether a display is a Wayland one: asked for as EGL_PLATFORM_WAYLAND, or (eglGetDisplay with
// no platform) a native display that is a wl_display - recognised the way Mesa does, by its
// first word being libwayland-client's wl_display_interface (or, for an application with its
// own copy of libwayland, an interface named "wl_display").
Bool IsWaylandDisplay(Uint64 nativeDisplay, EGLenum platform);
// Whether `window` looks like a wl_egl_window this library can present to.
Bool IsWaylandWindow(const void* window);
// The size the application last gave its wl_egl_window.
Bool WindowSize(const void* window, EGLint* width, EGLint* height);
// One window surface's presentation: the shm buffers, the event queue they are dispatched on,
// and the readback scratch. Created for a wl_egl_window on a wl_display; null with the reason
// logged when libwayland-client or the compositor's wl_shm is not there.
class WindowSurface {
public:
static UniquePtr<WindowSurface> Create(void* wlDisplay, void* wlEglWindow, EGLint width, EGLint height,
Bool hasAlpha);
~WindowSurface();
WindowSurface(const WindowSurface&) = delete;
WindowSurface& operator=(const WindowSurface&) = delete;
// Reads the frame back out of the current context's default framebuffer and puts it on
// the window: attach, damage, commit. The application's read-framebuffer and pack state
// are put back exactly as they were. False (logged) when the frame did not reach the
// window; the swap itself goes on regardless.
Bool Present();
// wl_egl_window_resize's effect, taken: true (with the new size) when the application gave
// its wl_egl_window a size this presentation is not at yet. The presentation adopts it -
// its next wl_shm buffer is that size - and the caller resizes the drawable behind it.
Bool TakeResize(EGLint* width, EGLint* height);
struct Impl;
private:
explicit WindowSurface(UniquePtr<Impl> impl);
UniquePtr<Impl> m_impl;
};
} // namespace MobileGL::MG_Impl::EGLImpl::Wayland
#endif
@@ -22,6 +22,7 @@
#include "../Sync/GL_Sync.h"
#include "../Debug/GL_Debug.h"
#include <MG_State/GLState/Core.h>
#include <MG_Impl/EGLImpl/EGLImpl.h>
#define DECLARE_GL_FUNCTION_STUB_HEAD(type, name, ...) MOBILEGL_GL_API type gl##name(__VA_ARGS__) {
@@ -40,22 +41,26 @@
#define DECLARE_GL_FUNCTION_END(type, name, ...) \
ZoneScopedC(TRACY_ZONECOLOR_ENTRY); \
MGLOG_D("Implementing function: %s(...)", __FUNCTION__); \
const MobileGL::MG_Impl::EGLImpl::GLStreamScope mglStreamScope; \
return MobileGL::MG_Impl::GLImpl::name(__VA_ARGS__); \
}
#define DECLARE_GL_FUNCTION_END_NO_RETURN(type, name, ...) \
ZoneScopedC(TRACY_ZONECOLOR_ENTRY); \
MGLOG_D("Implementing function: %s(...)", __FUNCTION__); \
const MobileGL::MG_Impl::EGLImpl::GLStreamScope mglStreamScope; \
MobileGL::MG_Impl::GLImpl::name(__VA_ARGS__); \
}
#else
#define DECLARE_GL_FUNCTION_END(type, name, ...) \
MGLOG_D("Implementing function: %s(...)", __FUNCTION__); \
const MobileGL::MG_Impl::EGLImpl::GLStreamScope mglStreamScope; \
return MobileGL::MG_Impl::GLImpl::name(__VA_ARGS__); \
}
#define DECLARE_GL_FUNCTION_END_NO_RETURN(type, name, ...) \
MGLOG_D("Implementing function: %s(...)", __FUNCTION__); \
const MobileGL::MG_Impl::EGLImpl::GLStreamScope mglStreamScope; \
MobileGL::MG_Impl::GLImpl::name(__VA_ARGS__); \
}
#endif
@@ -116,12 +121,14 @@ DECLARE_GL_FUNCTION_HEAD(void, EnableVertexAttribArray, GLuint index) DECLARE_GL
// call really did have nothing to do; MGPipeClientFinish answers false-cheap there too.
MOBILEGL_GL_API void glFinish() {
MGLOG_D("Implementing function: %s(...)", __FUNCTION__);
const MobileGL::MG_Impl::EGLImpl::GLStreamScope mglStreamScope;
#if MOBILEGL_BUILD_DISAGGREGATED
MobileGL::MG_Pipe::MGPipeClientFinish();
#endif
}
MOBILEGL_GL_API void glFlush() {
MGLOG_D("Implementing function: %s(...)", __FUNCTION__);
const MobileGL::MG_Impl::EGLImpl::GLStreamScope mglStreamScope;
#if MOBILEGL_BUILD_DISAGGREGATED
MobileGL::MG_Pipe::MGPipeClientFlush();
#endif
@@ -149,7 +156,7 @@ DECLARE_GL_FUNCTION_HEAD(void, GetProgramInfoLog, GLuint program, GLsizei bufSiz
DECLARE_GL_FUNCTION_HEAD(void, GetRenderbufferParameteriv, GLenum target, GLenum pname, GLint* params) DECLARE_GL_FUNCTION_END_NO_RETURN(void, GetRenderbufferParameteriv, target, pname, params)
DECLARE_GL_FUNCTION_HEAD(void, GetShaderiv, GLuint shader, GLenum pname, GLint* params) DECLARE_GL_FUNCTION_END_NO_RETURN(void, GetShaderiv, shader, pname, params)
DECLARE_GL_FUNCTION_HEAD(void, GetShaderInfoLog, GLuint shader, GLsizei bufSize, GLsizei* length, GLchar* infoLog) DECLARE_GL_FUNCTION_END_NO_RETURN(void, GetShaderInfoLog, shader, bufSize, length, infoLog)
DECLARE_GL_FUNCTION_STUB_HEAD(void, GetShaderPrecisionFormat, GLenum shadertype, GLenum precisiontype, GLint* range, GLint* precision) DECLARE_GL_FUNCTION_STUB_END_NO_RETURN(void, GetShaderPrecisionFormat, shadertype, precisiontype, range, precision)
DECLARE_GL_FUNCTION_HEAD(void, GetShaderPrecisionFormat, GLenum shadertype, GLenum precisiontype, GLint* range, GLint* precision) DECLARE_GL_FUNCTION_END_NO_RETURN(void, GetShaderPrecisionFormat, shadertype, precisiontype, range, precision)
DECLARE_GL_FUNCTION_HEAD(void, GetShaderSource, GLuint shader, GLsizei bufSize, GLsizei* length, GLchar* source) DECLARE_GL_FUNCTION_END_NO_RETURN(void, GetShaderSource, shader, bufSize, length, source)
DECLARE_GL_FUNCTION_HEAD(void, GetTexParameterfv, GLenum target, GLenum pname, GLfloat* params) DECLARE_GL_FUNCTION_END_NO_RETURN(void, GetTexParameterfv, target, pname, params)
DECLARE_GL_FUNCTION_HEAD(void, GetTexParameteriv, GLenum target, GLenum pname, GLint* params) DECLARE_GL_FUNCTION_END_NO_RETURN(void, GetTexParameteriv, target, pname, params)
+9 -1
View File
@@ -617,11 +617,19 @@ namespace MobileGL::MG_Impl::GLImpl {
MGLOG_D("vendorString: %s", vendorString.c_str());
return (const GLubyte*)vendorString.c_str();
case GL_VERSION: {
// "OpenGL ES" must not appear anywhere in a desktop GL_VERSION: some callers (ANGLE's GL
// backend among them) search the whole string for it rather than its prefix, and
// then take this for an ES driver, read the version from the wrong offsets and look
// for ES-named extensions. The backend is named "GLES" here for that reason only.
String backendName = rendererInfo.BackendName;
if (const auto at = backendName.find("OpenGL ES"); at != String::npos) {
backendName.replace(at, sizeof("OpenGL ES") - 1, "GLES");
}
versionStr =
std::format("{} {} {}, {} Backend, GIT@" GIT_COMMIT_HASH_SHORT,
rendererInfo.RendererGLInfo.TargetGLVersion.toString(), MG_Config::ProjectName,
MG_Config::CoreVersion.toFormattedString(MG_Config::DefaultVersionStringFormatAttrib),
rendererInfo.BackendName);
backendName);
MGLOG_D("versionStr: %s", versionStr.c_str());
return (const GLubyte*)versionStr.c_str();
}
@@ -2405,6 +2405,47 @@ namespace MobileGL::MG_Impl::GLImpl {
ShaderBinary_State(count, shaders, binaryformat, binary, length);
}
// GL 4.6 core 7.13 (ARB_ES2_compatibility). The shading language here is desktop GLSL, whose
// float and int are 32-bit IEEE-754 / two's complement at every precision qualifier, so every
// precision answers the same: log2 of the range and the mantissa bits. A silent stub answered
// zeros, which an ES-on-GL layer reads as "no highp" and refuses ES 2.0 outright.
void GetShaderPrecisionFormat(GLenum shadertype, GLenum precisiontype, GLint* range, GLint* precision) {
if (shadertype != GL_VERTEX_SHADER && shadertype != GL_FRAGMENT_SHADER) {
MG_State::pGLContext->RecordError(
ErrorCode::InvalidEnum,
MakeUnique<GenericErrorInfo>("MG_Impl/GLImpl", __func__,
"shadertype must be GL_VERTEX_SHADER or GL_FRAGMENT_SHADER."));
return;
}
GLint rangeLow = 0, rangeHigh = 0, bits = 0;
switch (precisiontype) {
case GL_LOW_FLOAT:
case GL_MEDIUM_FLOAT:
case GL_HIGH_FLOAT:
rangeLow = 127;
rangeHigh = 127;
bits = 23;
break;
case GL_LOW_INT:
case GL_MEDIUM_INT:
case GL_HIGH_INT:
rangeLow = 31;
rangeHigh = 30;
bits = 0;
break;
default:
MG_State::pGLContext->RecordError(
ErrorCode::InvalidEnum,
MakeUnique<GenericErrorInfo>("MG_Impl/GLImpl", __func__, "precisiontype is not a precision."));
return;
}
if (range) {
range[0] = rangeLow;
range[1] = rangeHigh;
}
if (precision) *precision = bits;
}
void SpecializeShader(GLuint shader, const GLchar* pEntryPoint, GLuint numSpecializationConstants,
const GLuint* pConstantIndex, const GLuint* pConstantValue) {
SpecializeShader_State(shader, pEntryPoint, numSpecializationConstants, pConstantIndex, pConstantValue);
@@ -41,6 +41,7 @@ namespace MobileGL::MG_Impl::GLImpl {
void GetShaderiv(GLuint shader, GLenum pname, GLint* params);
void GetShaderInfoLog(GLuint shader, GLsizei bufSize, GLsizei* length, GLchar* infoLog);
void GetShaderSource(GLuint shader, GLsizei bufSize, GLsizei* length, GLchar* source);
void GetShaderPrecisionFormat(GLenum shadertype, GLenum precisiontype, GLint* range, GLint* precision);
GLint GetUniformLocation(GLuint program, const GLchar* name);
void GetUniformfv(GLuint program, GLint location, GLfloat* params);
void GetUniformiv(GLuint program, GLint location, GLint* params);
@@ -41,6 +41,18 @@
// call sites below are declarations only, exactly as the frontend's mutators are.
#include <MG_Pipe/PipeMutation.h>
// P9 (W1, MG_Remote/CONTRACT-P9.md §1): AN UNPACK SOURCE IS READ FROM THE BUFFER'S CLIENT SHADOW,
// and under a transport a GPU write into that buffer - a read_pixels_to_buffer, which is how a
// glReadPixels into a pack buffer now crosses, or a shader store - reaches the shadow only through
// SyncGpuWrites, exactly as glMapBufferRange and glGetBufferSubData already do. Monolith keeps its
// read as it was: its ReadPixels maps the driver PBO back into the shadow inside the call, and the
// pull build compiles this to nothing (G1).
#if MOBILEGL_BUILD_DISAGGREGATED
#define MGL_SYNC_PIXEL_UNPACK_SOURCE(buffer) do { if (MG_Config::Transport != MG_Config::TransportMode::Monolith) (buffer)->SyncGpuWrites(); } while (0)
#else
#define MGL_SYNC_PIXEL_UNPACK_SOURCE(buffer) ((void)0)
#endif
namespace MobileGL::MG_Impl::GLImpl {
static SharedPtr<MG_State::GLState::ITextureObject> nullTextureObject;
static UnorderedMap<Uint, Bool> g_autoGenerateMipmapByTextureId;
@@ -1773,6 +1785,7 @@ namespace MobileGL::MG_Impl::GLImpl {
const auto& unpackBuffer =
MG_State::pGLContext->GetBufferBindingSlot(BufferTarget::PixelUnpack).GetBoundObject();
if (!unpackBuffer) return data;
MGL_SYNC_PIXEL_UNPACK_SOURCE(unpackBuffer);
return reinterpret_cast<const char*>(unpackBuffer->MappedData()) + reinterpret_cast<SizeT>(data);
}
@@ -1819,6 +1832,7 @@ namespace MobileGL::MG_Impl::GLImpl {
const auto& pixelUnpackBufferObject =
MG_State::pGLContext->GetBufferBindingSlot(BufferTarget::PixelUnpack).GetBoundObject();
if (pixelUnpackBufferObject) {
MGL_SYNC_PIXEL_UNPACK_SOURCE(pixelUnpackBufferObject);
originalPixels = reinterpret_cast<const char*>(pixelUnpackBufferObject->MappedData()) +
reinterpret_cast<SizeT>(pixels);
}
@@ -1941,6 +1955,7 @@ namespace MobileGL::MG_Impl::GLImpl {
if (pixelUnpackBufferObject) {
MGLOG_D("TexSubImage2D_State: Using Pixel Unpack Buffer Object ID: %u",
pixelUnpackBufferObject->GetExternalIndex());
MGL_SYNC_PIXEL_UNPACK_SOURCE(pixelUnpackBufferObject);
originalPixels = reinterpret_cast<const char*>(pixelUnpackBufferObject->MappedData()) +
reinterpret_cast<SizeT>(pixels);
}
@@ -2030,6 +2045,7 @@ namespace MobileGL::MG_Impl::GLImpl {
const auto& pixelUnpackBufferObject =
MG_State::pGLContext->GetBufferBindingSlot(BufferTarget::PixelUnpack).GetBoundObject();
if (pixelUnpackBufferObject) {
MGL_SYNC_PIXEL_UNPACK_SOURCE(pixelUnpackBufferObject);
originalPixels = reinterpret_cast<const char*>(pixelUnpackBufferObject->MappedData()) +
reinterpret_cast<SizeT>(pixels);
}
@@ -2535,6 +2551,7 @@ namespace MobileGL::MG_Impl::GLImpl {
if (pixelUnpackBufferObject) {
MGLOG_D("%s: Using Pixel Unpack Buffer Object ID: %u", __func__,
pixelUnpackBufferObject->GetExternalIndex());
MGL_SYNC_PIXEL_UNPACK_SOURCE(pixelUnpackBufferObject);
originalPixels = reinterpret_cast<const char*>(pixelUnpackBufferObject->MappedData()) +
reinterpret_cast<SizeT>(pixels);
}
@@ -2682,6 +2699,7 @@ namespace MobileGL::MG_Impl::GLImpl {
if (pixelUnpackBufferObject) {
MGLOG_D("%s: Using Pixel Unpack Buffer Object ID: %u", __func__,
pixelUnpackBufferObject->GetExternalIndex());
MGL_SYNC_PIXEL_UNPACK_SOURCE(pixelUnpackBufferObject);
originalPixels = reinterpret_cast<const char*>(pixelUnpackBufferObject->MappedData()) +
reinterpret_cast<SizeT>(pixels);
}
@@ -2800,6 +2818,7 @@ namespace MobileGL::MG_Impl::GLImpl {
const auto& pixelUnpackBufferObject =
MG_State::pGLContext->GetBufferBindingSlot(BufferTarget::PixelUnpack).GetBoundObject();
if (pixelUnpackBufferObject) {
MGL_SYNC_PIXEL_UNPACK_SOURCE(pixelUnpackBufferObject);
originalPixels = reinterpret_cast<const char*>(pixelUnpackBufferObject->MappedData()) +
reinterpret_cast<SizeT>(pixels);
}
@@ -6262,6 +6281,7 @@ namespace MobileGL::MG_Impl::GLImpl {
const auto& pixelUnpackBufferObject =
MG_State::pGLContext->GetBufferBindingSlot(BufferTarget::PixelUnpack).GetBoundObject();
if (pixelUnpackBufferObject) {
MGL_SYNC_PIXEL_UNPACK_SOURCE(pixelUnpackBufferObject);
originalPixels = reinterpret_cast<const char*>(pixelUnpackBufferObject->MappedData()) +
reinterpret_cast<SizeT>(pixels);
}
@@ -8,12 +8,20 @@
#include <Includes.h>
#include "../LookUp/LookUp.h"
#include "../../EGLImpl/EGLImpl.h"
// Every GLX entry point holds the client's stream lock, as every EGL and GL one does: they are EGL
// calls underneath (and a swap reads the frame back), so they cannot interleave with another
// thread's GL records.
using GLXStreamLock = MobileGL::MG_Impl::EGLImpl::StreamLockScope;
MOBILEGL_GLX_API void* glXGetProcAddress(const char* name) {
GLXStreamLock streamLock;
return MG_Impl::GLXImpl::GetProcAddress(name);
}
MOBILEGL_GLX_API void* glXGetProcAddressARB(const char* name) {
GLXStreamLock streamLock;
return MG_Impl::GLXImpl::GetProcAddressARB(name);
}
@@ -27,149 +35,184 @@ namespace GLXImpl = MobileGL::MG_Impl::GLXImpl;
// and XVisualInfo* crosses as void*.
MOBILEGL_GLX_API int glXQueryExtension(Display* dpy, int* errorBase, int* eventBase) {
GLXStreamLock streamLock;
return GLXImpl::QueryExtension(dpy, errorBase, eventBase);
}
MOBILEGL_GLX_API int glXQueryVersion(Display* dpy, int* major, int* minor) {
GLXStreamLock streamLock;
return GLXImpl::QueryVersion(dpy, major, minor);
}
MOBILEGL_GLX_API const char* glXQueryExtensionsString(Display* dpy, int screen) {
GLXStreamLock streamLock;
return GLXImpl::QueryExtensionsString(dpy, screen);
}
MOBILEGL_GLX_API const char* glXGetClientString(Display* dpy, int name) {
GLXStreamLock streamLock;
return GLXImpl::GetClientString(dpy, name);
}
MOBILEGL_GLX_API const char* glXQueryServerString(Display* dpy, int screen, int name) {
GLXStreamLock streamLock;
return GLXImpl::QueryServerString(dpy, screen, name);
}
MOBILEGL_GLX_API void** glXGetFBConfigs(Display* dpy, int screen, int* nelements) {
GLXStreamLock streamLock;
return GLXImpl::GetFBConfigs(dpy, screen, nelements);
}
MOBILEGL_GLX_API void** glXChooseFBConfig(Display* dpy, int screen, const int* attribList,
int* nelements) {
GLXStreamLock streamLock;
return GLXImpl::ChooseFBConfig(dpy, screen, attribList, nelements);
}
MOBILEGL_GLX_API int glXGetFBConfigAttrib(Display* dpy, void* config, int attribute, int* value) {
GLXStreamLock streamLock;
return GLXImpl::GetFBConfigAttrib(dpy, config, attribute, value);
}
MOBILEGL_GLX_API void* glXGetVisualFromFBConfig(Display* dpy, void* config) {
GLXStreamLock streamLock;
return GLXImpl::GetVisualFromFBConfig(dpy, config);
}
MOBILEGL_GLX_API void* glXChooseVisual(Display* dpy, int screen, int* attribList) {
GLXStreamLock streamLock;
return GLXImpl::ChooseVisual(dpy, screen, attribList);
}
MOBILEGL_GLX_API int glXGetConfig(Display* dpy, void* visualInfo, int attribute, int* value) {
GLXStreamLock streamLock;
return GLXImpl::GetConfig(dpy, visualInfo, attribute, value);
}
MOBILEGL_GLX_API void* glXCreateContext(Display* dpy, void* visualInfo, void* shareList, int direct) {
GLXStreamLock streamLock;
return GLXImpl::CreateContext(dpy, visualInfo, shareList, direct);
}
MOBILEGL_GLX_API void* glXCreateNewContext(Display* dpy, void* config, int renderType,
void* shareList, int direct) {
GLXStreamLock streamLock;
return GLXImpl::CreateNewContext(dpy, config, renderType, shareList, direct);
}
MOBILEGL_GLX_API void* glXCreateContextAttribsARB(Display* dpy, void* config, void* shareContext,
int direct, const int* attribList) {
GLXStreamLock streamLock;
return GLXImpl::CreateContextAttribsARB(dpy, config, shareContext, direct, attribList);
}
MOBILEGL_GLX_API void glXDestroyContext(Display* dpy, void* context) {
GLXStreamLock streamLock;
GLXImpl::DestroyContext(dpy, context);
}
MOBILEGL_GLX_API int glXMakeCurrent(Display* dpy, unsigned long drawable, void* context) {
GLXStreamLock streamLock;
return GLXImpl::MakeCurrent(dpy, drawable, context);
}
MOBILEGL_GLX_API int glXMakeContextCurrent(Display* dpy, unsigned long draw, unsigned long read,
void* context) {
GLXStreamLock streamLock;
return GLXImpl::MakeContextCurrent(dpy, draw, read, context);
}
MOBILEGL_GLX_API void glXSwapBuffers(Display* dpy, unsigned long drawable) {
GLXStreamLock streamLock;
GLXImpl::SwapBuffers(dpy, drawable);
}
MOBILEGL_GLX_API unsigned long glXCreateWindow(Display* dpy, void* config, unsigned long window,
const int* attribList) {
GLXStreamLock streamLock;
return GLXImpl::CreateWindow(dpy, config, window, attribList);
}
MOBILEGL_GLX_API void glXDestroyWindow(Display* dpy, unsigned long window) {
GLXStreamLock streamLock;
GLXImpl::DestroyWindow(dpy, window);
}
MOBILEGL_GLX_API void* glXGetCurrentContext() {
GLXStreamLock streamLock;
return GLXImpl::GetCurrentContext();
}
MOBILEGL_GLX_API unsigned long glXGetCurrentDrawable() {
GLXStreamLock streamLock;
return GLXImpl::GetCurrentDrawable();
}
MOBILEGL_GLX_API unsigned long glXGetCurrentReadDrawable() {
GLXStreamLock streamLock;
return GLXImpl::GetCurrentReadDrawable();
}
MOBILEGL_GLX_API Display* glXGetCurrentDisplay() {
GLXStreamLock streamLock;
return GLXImpl::GetCurrentDisplay();
}
MOBILEGL_GLX_API int glXIsDirect(Display* dpy, void* context) {
GLXStreamLock streamLock;
return GLXImpl::IsDirect(dpy, context);
}
MOBILEGL_GLX_API void glXWaitGL() {
GLXStreamLock streamLock;
GLXImpl::WaitGL();
}
MOBILEGL_GLX_API void glXWaitX() {
GLXStreamLock streamLock;
GLXImpl::WaitX();
}
MOBILEGL_GLX_API int glXQueryContext(Display* dpy, void* context, int attribute, int* value) {
GLXStreamLock streamLock;
return GLXImpl::QueryContext(dpy, context, attribute, value);
}
MOBILEGL_GLX_API void glXQueryDrawable(Display* dpy, unsigned long drawable, int attribute,
unsigned int* value) {
GLXStreamLock streamLock;
GLXImpl::QueryDrawable(dpy, drawable, attribute, value);
}
MOBILEGL_GLX_API void glXSwapIntervalEXT(Display* dpy, unsigned long drawable, int interval) {
GLXStreamLock streamLock;
GLXImpl::SwapIntervalEXT(dpy, drawable, interval);
}
MOBILEGL_GLX_API int glXSwapIntervalMESA(unsigned int interval) {
GLXStreamLock streamLock;
return GLXImpl::SwapIntervalMESA(interval);
}
MOBILEGL_GLX_API int glXGetSwapIntervalMESA() {
GLXStreamLock streamLock;
return GLXImpl::GetSwapIntervalMESA();
}
MOBILEGL_GLX_API int glXSwapIntervalSGI(int interval) {
GLXStreamLock streamLock;
return GLXImpl::SwapIntervalSGI(interval);
}
// Legacy entry points some loaders probe for; harmless no-op stubs.
MOBILEGL_GLX_API void glXCopyContext(Display*, void*, void*, unsigned long) {
GLXStreamLock streamLock;
MGLOG_W_ONCE("glx: glXCopyContext is not supported");
}
MOBILEGL_GLX_API unsigned long glXCreateGLXPixmap(Display*, void*, unsigned long) {
GLXStreamLock streamLock;
MGLOG_W_ONCE("glx: glXCreateGLXPixmap is not supported");
return 0;
}
@@ -177,6 +220,7 @@ MOBILEGL_GLX_API unsigned long glXCreateGLXPixmap(Display*, void*, unsigned long
MOBILEGL_GLX_API void glXDestroyGLXPixmap(Display*, unsigned long) {}
MOBILEGL_GLX_API unsigned long glXCreatePixmap(Display*, void*, unsigned long, const int*) {
GLXStreamLock streamLock;
MGLOG_W_ONCE("glx: glXCreatePixmap is not supported");
return 0;
}
@@ -184,6 +228,7 @@ MOBILEGL_GLX_API unsigned long glXCreatePixmap(Display*, void*, unsigned long, c
MOBILEGL_GLX_API void glXDestroyPixmap(Display*, unsigned long) {}
MOBILEGL_GLX_API unsigned long glXCreatePbuffer(Display*, void*, const int*) {
GLXStreamLock streamLock;
MGLOG_W_ONCE("glx: glXCreatePbuffer is not supported");
return 0;
}
@@ -191,12 +236,14 @@ MOBILEGL_GLX_API unsigned long glXCreatePbuffer(Display*, void*, const int*) {
MOBILEGL_GLX_API void glXDestroyPbuffer(Display*, unsigned long) {}
MOBILEGL_GLX_API void glXUseXFont(unsigned long, int, int, int) {
GLXStreamLock streamLock;
MGLOG_W_ONCE("glx: glXUseXFont is not supported");
}
MOBILEGL_GLX_API void glXSelectEvent(Display*, unsigned long, unsigned long) {}
MOBILEGL_GLX_API void glXGetSelectedEvent(Display*, unsigned long, unsigned long* eventMask) {
GLXStreamLock streamLock;
if (eventMask) {
*eventMask = 0;
}
@@ -0,0 +1,128 @@
// MobileGL - MobileGL/MG_Impl/GLXImpl/Exporting/GlvndVendor.cpp
// 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
// MOBILEGL AS A GLVND GLX VENDOR.
//
// The libGL.so.1 / libGLX.so.0 an X11 application links on a glvnd system is a dispatcher: it
// picks a vendor library per X screen - the one the X server names, or the one
// __GLX_VENDOR_LIBRARY_NAME names - loads it as libGLX_<name>.so.0 and routes every GLX call and,
// once a context is current, every GL call to it. Under a Wayland compositor the X server is
// Xwayland, which names the distribution's driver; that driver then needs a GPU device this
// machine's X clients do not have, and glxinfo ends in "failed to retrieve device information".
// Installed as libGLX_mobilegl.so.0 and named by __GLX_VENDOR_LIBRARY_NAME=mobilegl, this library
// is the one asked instead, through the same entry points it exports directly.
//
// The ABI (libglvnd's libglxabi.h, vendor ABI 1.0) is mirrored here rather than included: that
// header includes GL/glx.h and Xlib.h, and this library's GLX layer is built without X headers.
#include <Includes.h>
#if defined(__linux__) && !defined(__ANDROID__)
#include "../GLXImpl.h"
#include "../../GetProcAddress.h"
namespace {
using namespace MobileGL;
struct GLXVendorInfo;
using XBool = int;
// __GLXapiExportsRec, field for field.
struct GLXApiExports {
GLXVendorInfo* (*getDynDispatch)(Display* dpy, int screen);
GLXVendorInfo* (*getCurrentDynDispatch)();
void* (*fetchDispatchEntry)(GLXVendorInfo* dynDispatch, int index);
void* (*getCurrentContext)();
int (*addVendorContextMapping)(Display* dpy, void* context, GLXVendorInfo* vendor);
void (*removeVendorContextMapping)(Display* dpy, void* context);
GLXVendorInfo* (*vendorFromContext)(void* context);
int (*addVendorFBConfigMapping)(Display* dpy, void* config, GLXVendorInfo* vendor);
void (*removeVendorFBConfigMapping)(Display* dpy, void* config);
GLXVendorInfo* (*vendorFromFBConfig)(Display* dpy, void* config);
int (*addVendorDrawableMapping)(Display* dpy, XID drawable, GLXVendorInfo* vendor);
void (*removeVendorDrawableMapping)(Display* dpy, XID drawable);
GLXVendorInfo* (*vendorFromDrawable)(Display* dpy, XID drawable);
};
// __GLXapiImportsRec, field for field; the patch hooks are optional and left null.
struct GLXApiImports {
XBool (*isScreenSupported)(Display* dpy, int screen);
void* (*getProcAddress)(const unsigned char* procName);
void* (*getDispatchAddress)(const unsigned char* procName);
void (*setDispatchIndex)(const unsigned char* procName, int index);
XBool (*notifyError)(Display* dpy, unsigned char error, XID resid, unsigned char opcode, XBool coreX11error);
unsigned char (*isPatchSupported)(int type, int stubSize);
unsigned char (*initiatePatch)(int type, int stubSize, void* lookupStubOffset);
void (*releasePatch)();
void (*patchThreadAttach)();
};
constexpr Uint32 kGLXVendorABIMajor = 1;
const GLXApiExports* g_glxExports = nullptr;
GLXVendorInfo* g_glxVendor = nullptr;
XBool VendorIsScreenSupported(Display*, int) { return 1; }
// GLX names are the exported entry points (each holds the stream lock); GL names are the
// implementation's, as eglGetProcAddress hands them out.
void* VendorGetProcAddress(const unsigned char* procName) {
const char* name = reinterpret_cast<const char*>(procName);
if (std::strncmp(name, "glX", 3) == 0) return MG_Impl::GLXImpl::GetGLXEntryPoint(name);
return MG_Impl::GetProcAddress(name);
}
// A context made by an extension is one libGLX did not see created, so it is told whose it is
// - or glXMakeCurrent on it finds no vendor and fails.
void* VendorCreateContextAttribsARB(Display* dpy, void* config, void* share, int direct, const int* attribs) {
using Create = void* (*)(Display*, void*, void*, int, const int*);
auto* create = reinterpret_cast<Create>(MG_Impl::GLXImpl::GetGLXEntryPoint("glXCreateContextAttribsARB"));
void* context = create ? create(dpy, config, share, direct, attribs) : nullptr;
if (context && g_glxExports && g_glxExports->addVendorContextMapping(dpy, context, g_glxVendor) != 0) {
using Destroy = void (*)(Display*, void*);
if (auto* destroy = reinterpret_cast<Destroy>(MG_Impl::GLXImpl::GetGLXEntryPoint("glXDestroyContext"))) {
destroy(dpy, context);
}
return nullptr;
}
return context;
}
// libGLX asks for the GLX functions it has no dispatcher of its own for (the extensions). This
// is the only vendor in the process that answers for its screens, so the "dispatcher" is the
// implementation itself; only context creation has bookkeeping to do on the way.
void* VendorGetDispatchAddress(const unsigned char* procName) {
const char* name = reinterpret_cast<const char*>(procName);
if (std::strcmp(name, "glXCreateContextAttribsARB") == 0) {
return reinterpret_cast<void*>(&VendorCreateContextAttribsARB);
}
return MG_Impl::GLXImpl::GetGLXEntryPoint(name);
}
void VendorSetDispatchIndex(const unsigned char*, int) {}
} // namespace
extern "C" MOBILEGL_API int __glx_Main(Uint32 version, const void* exportsTable, void* vendor, void* importsTable) {
const auto* exports = static_cast<const GLXApiExports*>(exportsTable);
auto* imports = static_cast<GLXApiImports*>(importsTable);
if ((version >> 16) != kGLXVendorABIMajor || exports == nullptr || imports == nullptr) return 0;
g_glxExports = exports;
g_glxVendor = static_cast<GLXVendorInfo*>(vendor);
imports->isScreenSupported = &VendorIsScreenSupported;
imports->getProcAddress = &VendorGetProcAddress;
imports->getDispatchAddress = &VendorGetDispatchAddress;
imports->setDispatchIndex = &VendorSetDispatchIndex;
imports->notifyError = nullptr;
imports->isPatchSupported = nullptr;
imports->initiatePatch = nullptr;
imports->releasePatch = nullptr;
imports->patchThreadAttach = nullptr;
return 1;
}
#endif // __linux__ && !__ANDROID__
+131 -6
View File
@@ -10,8 +10,10 @@
#if defined(__linux__) && !defined(__ANDROID__)
#include "../EGLImpl/EGLImpl.h"
#include "../EGLImpl/WaylandWindow.h"
#include "../GetProcAddress.h"
#include <Init.h>
#include <Config.h>
namespace MobileGL::MG_Impl::GLXImpl {
namespace {
@@ -156,6 +158,53 @@ namespace MobileGL::MG_Impl::GLXImpl {
return *functions;
}
// ---- X11 presentation of a remote drawable (libxcb through libX11's own connection,
// dlopen'd like the rest; xcb's request is plain arguments, no struct layouts) ----
struct XcbVoidCookie {
unsigned int Sequence;
};
struct XcbFunctions {
void* (*GetXCBConnection)(Display*) = nullptr;
Uint32 (*GenerateId)(void*) = nullptr;
XcbVoidCookie (*CreateGC)(void*, Uint32, Uint32, Uint32, const void*) = nullptr;
XcbVoidCookie (*FreeGC)(void*, Uint32) = nullptr;
XcbVoidCookie (*PutImage)(void*, Uint8, Uint32, Uint32, Uint16, Uint16, Int16, Int16, Uint8, Uint8,
Uint32, const Uint8*) = nullptr;
Uint32 (*MaximumRequestLength)(void*) = nullptr;
int (*Flush)(void*) = nullptr;
Bool Valid() const {
return GetXCBConnection && GenerateId && CreateGC && FreeGC && PutImage && MaximumRequestLength &&
Flush;
}
};
const XcbFunctions& Xcb() {
static const XcbFunctions* functions = [] {
auto* fns = new XcbFunctions();
void* x11xcb = dlopen("libX11-xcb.so.1", RTLD_LOCAL | RTLD_NOW);
void* xcb = dlopen("libxcb.so.1", RTLD_LOCAL | RTLD_NOW);
if (x11xcb && xcb) {
fns->GetXCBConnection =
reinterpret_cast<decltype(fns->GetXCBConnection)>(dlsym(x11xcb, "XGetXCBConnection"));
fns->GenerateId = reinterpret_cast<decltype(fns->GenerateId)>(dlsym(xcb, "xcb_generate_id"));
fns->CreateGC = reinterpret_cast<decltype(fns->CreateGC)>(dlsym(xcb, "xcb_create_gc"));
fns->FreeGC = reinterpret_cast<decltype(fns->FreeGC)>(dlsym(xcb, "xcb_free_gc"));
fns->PutImage = reinterpret_cast<decltype(fns->PutImage)>(dlsym(xcb, "xcb_put_image"));
fns->MaximumRequestLength = reinterpret_cast<decltype(fns->MaximumRequestLength)>(
dlsym(xcb, "xcb_get_maximum_request_length"));
fns->Flush = reinterpret_cast<decltype(fns->Flush)>(dlsym(xcb, "xcb_flush"));
}
if (!fns->Valid()) {
MGLOG_E_ONCE("glx: libX11-xcb/libxcb entry points missing; remote windows are not shown");
}
return fns;
}();
return *functions;
}
constexpr Uint8 kXcbImageFormatZPixmap = 2;
// ---- FBConfigs: mirror the two EGLState configs, stencil-8 first so
// stencil-wanting choosers (GLFW's default hints) match config 1 ----
struct FBConfigInfo {
@@ -187,6 +236,14 @@ namespace MobileGL::MG_Impl::GLXImpl {
Uint32 Width = 0;
Uint32 Height = 0;
std::chrono::steady_clock::time_point LastSizePoll{};
Bool RemotePbuffer = false;
// A remote drawable's presentation: the window's depth, the GC its frames are put
// with (0 until the first one) and the readback buffers.
::Display* XDisplay = nullptr;
Uint32 Depth = 0;
Uint32 Gc = 0;
Vector<Uint8> Scratch;
Vector<Uint8> Image;
};
std::recursive_mutex& RegistryMutex() {
@@ -255,7 +312,8 @@ namespace MobileGL::MG_Impl::GLXImpl {
return it == contexts.end() ? nullptr : &it->second;
}
Bool QueryDrawableSize(Display* dpy, GLXDrawableHandle drawable, Uint32& width, Uint32& height) {
Bool QueryDrawableSize(Display* dpy, GLXDrawableHandle drawable, Uint32& width, Uint32& height,
Uint32* depthOut = nullptr) {
const auto& x11 = X11();
if (!x11.Valid() || !dpy || drawable == 0) {
return false;
@@ -272,9 +330,59 @@ namespace MobileGL::MG_Impl::GLXImpl {
}
width = std::max(w, 1u);
height = std::max(h, 1u);
if (depthOut) *depthOut = depth;
return true;
}
// A REMOTE DRAWABLE IS A PBUFFER, AND NOTHING OF IT REACHES THE X WINDOW BY ITSELF: the
// backend draws in another process (another OS), where an XID means nothing. So at every
// swap the frame is read back and put on the window with PutImage - the same contract as
// the Wayland windows' wl_shm presentation. Called with the drawable current.
void PresentRemoteDrawable(GLXDrawableHandle drawable, DrawableSurface& surface) {
const EGLImpl::GLStreamScope stream; // the readback is GL records of this thread's context
const auto& xcb = Xcb();
if (!xcb.Valid() || !surface.XDisplay) return;
if (surface.Depth != 24 && surface.Depth != 32) {
MGLOG_W_ONCE("glx: a depth-%u window cannot be shown (24/32-bit only)", surface.Depth);
return;
}
void* connection = xcb.GetXCBConnection(surface.XDisplay);
if (!connection) return;
if (surface.Gc == 0) {
surface.Gc = xcb.GenerateId(connection);
(void)xcb.CreateGC(connection, surface.Gc, static_cast<Uint32>(drawable), 0, nullptr);
}
const EGLint width = static_cast<EGLint>(surface.Width);
const EGLint height = static_cast<EGLint>(surface.Height);
const SizeT rowBytes = static_cast<SizeT>(width) * 4;
surface.Image.resize(rowBytes * static_cast<SizeT>(height));
EGLImpl::ReadBackFrameBGRA(width, height, surface.Scratch, surface.Image.data(), rowBytes);
// A request over the server's maximum length closes the connection, so the image goes
// in bands of whole rows that each fit (the request header is 24 bytes).
const SizeT maxBytes = static_cast<SizeT>(xcb.MaximumRequestLength(connection)) * 4;
const SizeT budget = maxBytes > 64 ? maxBytes - 64 : rowBytes;
const EGLint bandRows = std::max<EGLint>(1, static_cast<EGLint>(budget / rowBytes));
for (EGLint y = 0; y < height; y += bandRows) {
const EGLint rows = std::min(bandRows, height - y);
(void)xcb.PutImage(connection, kXcbImageFormatZPixmap, static_cast<Uint32>(drawable), surface.Gc,
static_cast<Uint16>(width), static_cast<Uint16>(rows), 0, static_cast<Int16>(y), 0,
static_cast<Uint8>(surface.Depth), static_cast<Uint32>(rowBytes * rows),
surface.Image.data() + rowBytes * static_cast<SizeT>(y));
}
(void)xcb.Flush(connection);
}
void ReleaseDrawableSurface(DrawableSurface& surface) {
if (surface.Gc != 0 && surface.XDisplay) {
const auto& xcb = Xcb();
if (void* connection = xcb.Valid() ? xcb.GetXCBConnection(surface.XDisplay) : nullptr) {
(void)xcb.FreeGC(connection, surface.Gc);
}
}
EGLImpl::DestroySurface(surface.Display, surface.Surface);
}
// The backends never query the X window size themselves; the GLX layer
// owns size discovery and pushes changes through the internal resize hook
// (same contract as the WGL and CGL layers). Polling XGetGeometry is a
@@ -294,6 +402,7 @@ namespace MobileGL::MG_Impl::GLXImpl {
if (width == surface.Width && height == surface.Height) {
return;
}
// A remote X drawable is a pbuffer; resizing it gives it a new one at the new size.
if (EGLImpl::ResizePlatformWindowSurface(surface.Display, surface.Surface,
static_cast<EGLint>(width),
static_cast<EGLint>(height))) {
@@ -313,7 +422,8 @@ namespace MobileGL::MG_Impl::GLXImpl {
Uint32 width = 0;
Uint32 height = 0;
if (!QueryDrawableSize(dpy, drawable, width, height)) {
Uint32 depth = 0;
if (!QueryDrawableSize(dpy, drawable, width, height, &depth)) {
MGLOG_E_ONCE("glx: XGetGeometry failed for drawable 0x%lx", drawable);
return nullptr;
}
@@ -323,8 +433,14 @@ namespace MobileGL::MG_Impl::GLXImpl {
EGL_HEIGHT, static_cast<EGLAttrib>(height),
EGL_NONE,
};
EGLSurface surface = EGLImpl::CreatePlatformWindowSurface(
context.Display, context.Config, reinterpret_cast<void*>(drawable), attribs);
const Bool remotePbuffer = MG_Config::Transport == MG_Config::TransportMode::Spawn ||
MG_Config::Transport == MG_Config::TransportMode::UnixSocket;
const EGLint pbufferAttribs[] = {EGL_WIDTH, static_cast<EGLint>(width),
EGL_HEIGHT, static_cast<EGLint>(height), EGL_NONE};
EGLSurface surface = remotePbuffer
? EGLImpl::CreatePbufferSurface(context.Display, context.Config, pbufferAttribs)
: EGLImpl::CreatePlatformWindowSurface(
context.Display, context.Config, reinterpret_cast<void*>(drawable), attribs);
if (surface == EGL_NO_SURFACE) {
MGLOG_E_ONCE("glx: failed to create window surface for drawable 0x%lx (%ux%u)", drawable,
width, height);
@@ -336,6 +452,9 @@ namespace MobileGL::MG_Impl::GLXImpl {
record.Surface = surface;
record.Width = width;
record.Height = height;
record.RemotePbuffer = remotePbuffer;
record.XDisplay = dpy;
record.Depth = depth;
record.LastSizePoll = std::chrono::steady_clock::now();
auto [inserted, _] = surfaces.emplace(drawable, record);
return &inserted->second;
@@ -961,8 +1080,14 @@ namespace MobileGL::MG_Impl::GLXImpl {
MGLOG_W_ONCE("glx: glXSwapBuffers with no surface for drawable 0x%lx", drawable);
return;
}
SyncSurfaceSize(dpy, drawable, it->second);
// What the window shows is the frame the swap finishes, read back while it is still the
// drawable's content (only from the thread it is current to: the readback is a GL call).
if (it->second.RemotePbuffer && t_current.Draw == drawable && t_current.Context) {
PresentRemoteDrawable(drawable, it->second);
}
EGLImpl::SwapBuffers(it->second.Display, it->second.Surface);
// A resize takes effect for the next frame.
SyncSurfaceSize(dpy, drawable, it->second);
}
GLXDrawableHandle CreateWindow(Display*, GLXFBConfigHandle config, GLXDrawableHandle window,
@@ -988,7 +1113,7 @@ namespace MobileGL::MG_Impl::GLXImpl {
if (it == surfaces.end()) {
return;
}
EGLImpl::DestroySurface(it->second.Display, it->second.Surface);
ReleaseDrawableSurface(it->second);
surfaces.erase(it);
}
+5
View File
@@ -15,6 +15,9 @@
#endif
extern "C" EGLDisplay eglGetPlatformDisplayEXT(EGLenum platform, void* native_display, const EGLint* attrib_list);
extern "C" EGLImage eglCreateImageKHR(EGLDisplay dpy, EGLContext ctx, EGLenum target, EGLClientBuffer buffer,
const EGLint* attrib_list);
extern "C" EGLBoolean eglDestroyImageKHR(EGLDisplay dpy, EGLImage image);
#define GETPROC(name, var) \
if (strcmp(#name, var) == 0) { \
@@ -64,6 +67,8 @@ namespace MobileGL::MG_Impl {
GETPROC(eglGetSyncAttrib, name);
GETPROC(eglCreateImage, name);
GETPROC(eglDestroyImage, name);
GETPROC(eglCreateImageKHR, name);
GETPROC(eglDestroyImageKHR, name);
GETPROC(eglGetPlatformDisplay, name);
GETPROC(eglGetPlatformDisplayEXT, name);
GETPROC(eglCreatePlatformWindowSurface, name);
+4
View File
@@ -48,6 +48,10 @@ namespace MobileGL::MG_Impl {
MakeUnique<GLImpl::FramebufferImpl::DefaultFramebufferInfo>(fbo0, colorTex, depthTex, stencilTex);
MG_State::pGLContext->GetFramebufferBindingSlot(FramebufferTarget::Draw).Bind(fbo0);
MG_State::pGLContext->GetFramebufferBindingSlot(FramebufferTarget::Read).Bind(fbo0);
// P14 S3: every GLContext a later eglCreateContext builds starts with this same default
// framebuffer, so a context the application creates after this one is not left with an
// unbound framebuffer 0.
MG_State::SetDefaultFramebuffer(fbo0);
#if defined(__APPLE__) && !defined(MOBILEGL_IOS)
NSOpenGLImpl::InstallHooks();
#endif
+4 -4
View File
@@ -247,10 +247,10 @@ namespace MobileGL::MG_Pipe {
for (Uint32 w = 0; w < kMGPipeShaderBufferWritableMaskWords; ++w) {
header.WritableMask[w] = writableMask[w];
}
// HostSpanCount IS 0 ALWAYS on Espryt, and that is a ruling rather than an omission:
// the second var-tail exists for kCapNeedsHostUboBytes (Magma's named-UBO ring) and
// that bit is 0 for the whole of P5 (CONTRACT-P5.md table 0). The codec's host-span
// honesty pass is the guard that says so out loud if a backend ever publishes it.
// HostSpanCount IS 0 ALWAYS, and that is a ruling rather than an omission: the
// second var-tail exists for kCapNeedsHostUboBytes (a Magma named-UBO ring that was
// never built) and that bit is RESERVED (P8-F): no server publishes it. The codec's
// host-span honesty pass is the guard that says so out loud if a peer sends one.
header.HostSpanCount = 0;
header.ContentHash = hash;
MGPipeRouteSetShaderBuffers(header, m_entries.data());
+51 -16
View File
@@ -233,9 +233,9 @@ namespace MobileGL::MG_Pipe {
desc.StorageKind = MGPipeTextureStorageKindForTarget(texture.GetTarget());
desc.BindMask = bindMask;
// STICKY AND FOREVER: everImageBound, the client-side answer MGPipeTypes.h asks for.
// It is the PREVENTION half of the texture-remint stall class - a texture the server
// knows may be image-bound is allocated image-bindable up front, so the re-mint that
// would have to pull its texels back never happens.
// It PREVENTS the texture re-mint - a texture the server knows may be image-bound is
// allocated image-bindable up front, so the server never has to read its levels back
// and re-upload them into a new carrier (P9 W2).
desc.ImageBindableHint = (bindMask & kMGPipeBindShaderImage) != 0 ? 1 : 0;
if (storageDefined) {
const MGPipeTextureExtent extent = MGPipeTextureExtentOf(texture);
@@ -702,8 +702,7 @@ namespace MobileGL::MG_Pipe {
// mask rides resource_create and every resource_respecify - and an IMMUTABLE texture has
// no further respecify, that being what immutable means - so for the canonical order
// `glTexStorage2D(...); glBindImageTexture(...)` the applier's record kept
// ImageBindableHint = 0 for ever and the PREVENTION half of the texture-remint stall
// class never fired. So a mask that actually MOVES re-emits the stored descriptor with
// ImageBindableHint = 0 for ever and the re-mint PREVENTION never fired. So a mask that actually MOVES re-emits the stored descriptor with
// the new mask: every storage-defining field is byte-identical to what the applier
// holds, which is exactly the shape wire applies as a METADATA UPDATE - the descriptor
// is replaced, no reallocation is acked, and NO pending upload is dropped, so a mask
@@ -1470,21 +1469,53 @@ namespace MobileGL::MG_Pipe {
const MGPBox unionBox = MGPipeBoxOfDirtyRegion(mipmap->GetStorageDirtyRegion(uploadTarget, level));
if (unionBox.W == 0 || unionBox.H == 0 || unionBox.D == 0) return false;
// THE REGION LIST BEHIND THE BOX. 0 is legal and means "the union box is the whole
// story" - it covers a single rect (identical to the box by construction), more
// rects than the cap, and a summed area so close to the box's that one big upload
// beats many small ones. The invariant that makes the server's choice safe is that
// the two describe the SAME texels: every rect lies inside the box, and their union
// is the box.
// THE REGION LIST BEHIND THE BOX IS THE LEVEL'S EXACT DIRTY FOOTPRINT (P11 M2, dev
// 9524c688): disjoint boxes holding every texel written since the level was last
// clean and no other. The storage's rect list this used to carry only COVERS the
// writes - it merges touching rects into their bounding box, folds when full and is
// withheld (0) past 3/4 of the union box - and once the far side's image may hold GPU
// writes, whatever it uploads beyond the writes goes up over them. Both servers read
// the list as the texels to upload: Magma always, Espryt once its driver texture may
// hold GPU writes (and otherwise it re-applies the storage's box-or-rects choice
// itself, so the Mali job count is the server's call as before).
//
// 0 IS NOW THE WHOLE-LEVEL SPELLING - a footprint that is the whole level - with one
// exception (below). Every other write carries its boxes, a single one included, so the
// applier's accumulation (boxes union, lists concatenate, "0 regions" makes the entry
// box only) never widens two exact contributions into their union box. THE EXCEPTION:
// a footprint larger than one record may carry (kMGPipeMaxPendingUploadRegions) falls
// back to the storage's rect list - a COVER, stated once in the log - and when that list
// is withheld (0: its rects fill 3/4 of the union box) this 0 is a PARTIAL, INEXACT
// box. A reader that takes "box only" as "the client owns every texel of the box"
// (AdoptDriverLevelIntoShadow's covering-box rule) is exact everywhere but here.
//
// READ AS BOXES, ONCE, because both shapes below want them in that form: the
// whole-level record converts each one straight back through MGPipeBuildSubRegion,
// and a piece clips each one to its own slab.
MG_State::GLState::MipmapDirtyRegion rects[MG_State::GLState::MipmapStorage::kMaxDirtyRects];
const SizeT rectCount = mipmap->GetStorageDirtyRects(
uploadTarget, level, rects, MG_State::GLState::MipmapStorage::kMaxDirtyRects);
MGPBox rectBoxes[MG_State::GLState::MipmapStorage::kMaxDirtyRects];
for (SizeT i = 0; i < rectCount; ++i) rectBoxes[i] = MGPipeBoxOfDirtyRegion(rects[i]);
mipmap->GetStorageDirtyFootprint(uploadTarget, level, m_footprint);
m_rectBoxes.clear();
const Bool wholeLevelWrite =
m_footprint.size() == 1 && m_footprint.front().lo.x() <= 0 && m_footprint.front().lo.y() <= 0 &&
m_footprint.front().lo.z() <= 0 && m_footprint.front().hi.x() >= levelSize.x() &&
m_footprint.front().hi.y() >= levelSize.y() &&
m_footprint.front().hi.z() >= std::max<Int>(levelSize.z(), 1);
if (wholeLevelWrite) {
// The box is the story.
} else if (m_footprint.size() <= static_cast<SizeT>(kMGPipeMaxPendingUploadRegions)) {
for (const auto& box : m_footprint) m_rectBoxes.push_back(MGPipeBoxOfDirtyRegion(box));
} else {
MGLOG_W_ONCE("MGPipe: texture {slot=%u, gen=%u} level %u has %zu disjoint dirty boxes, more than one "
"record carries (%u); it goes out as the storage's rect list, which covers the writes "
"but not exactly",
pending.Handle.Slot, pending.Handle.Gen, static_cast<Uint>(level), m_footprint.size(),
kMGPipeMaxPendingUploadRegions);
MG_State::GLState::MipmapDirtyRegion rects[MG_State::GLState::MipmapStorage::kMaxDirtyRects];
const SizeT coverCount = mipmap->GetStorageDirtyRects(
uploadTarget, level, rects, MG_State::GLState::MipmapStorage::kMaxDirtyRects);
for (SizeT i = 0; i < coverCount; ++i) m_rectBoxes.push_back(MGPipeBoxOfDirtyRegion(rects[i]));
}
const SizeT rectCount = m_rectBoxes.size();
const MGPBox* rectBoxes = m_rectBoxes.data();
Bool dispatched = false;
Bool accepted = true;
@@ -1624,6 +1655,10 @@ namespace MobileGL::MG_Pipe {
Vector<Entry> m_renderbuffers;
Vector<DrainEntry> m_drain;
Vector<MGPSubRegion> m_regions;
// EmitOneLevel's scratch, kept for its capacity: the level's exact footprint and the
// boxes the record's regions are built from.
Vector<MG_State::GLState::MipmapDirtyRegion> m_footprint;
Vector<MGPBox> m_rectBoxes;
Bool m_draining = false;
MGPResourceDesc m_lastDesc{};
+9 -1
View File
@@ -303,9 +303,15 @@ namespace MobileGL::MG_Pipe {
// A different context is a different server: nothing the tracker latched about
// the old one says anything about this one, and the first walk on a fresh
// context must publish a COMPLETE state rather than an increment.
if (m_context != &ctx) {
//
// KEYED ON THE CONTEXT'S NEVER-REUSED ID, NOT ITS ADDRESS: a context destroyed and
// remade at the same heap address (Qt does this) is a fresh server-side applier
// with empty windows, and an address compare would skip the re-prime that refills
// them.
if (m_context != &ctx || m_contextId != ctx.GetTextureContextId()) {
Reset();
m_context = &ctx;
m_contextId = ctx.GetTextureContextId();
}
const Bool wasPrimed = m_primed;
@@ -811,6 +817,7 @@ namespace MobileGL::MG_Pipe {
m_staged = RenderStateParameters{};
m_stagedAttribs = AttribDefaults{};
m_context = nullptr;
m_contextId = 0;
m_lastDirty = 0;
m_primed = false;
m_freshlyPrimed = false;
@@ -917,6 +924,7 @@ namespace MobileGL::MG_Pipe {
AttribDefaults m_stagedAttribs{};
const void* m_context = nullptr;
Uint64 m_contextId = 0;
Uint32 m_lastDirty = 0;
Bool m_primed = false;
Bool m_freshlyPrimed = false;
+2 -2
View File
@@ -263,8 +263,8 @@ namespace MobileGL::MG_Pipe {
if (vao) {
if (const auto& bound = vao->GetIndexBufferBindingSlot().GetBoundObject()) {
m_lastIndex.Res = MGPipeSlots().Acquire(MGPipeKind::Buffer, bound->GetLifetimeId());
// The ELEMENT_ARRAY bit, and it is the one the split path keys on
// (kCapNeedsHostIndexBytes -> restart rewriting, multi-draw flattening).
// The INDEX|ELEMENT_ARRAY bind bits. ELEMENT_ARRAY's D-B7 reader
// (kCapNeedsHostIndexBytes) is RESERVED (P8-F) and was never built.
// Noted at every draw for RefreshBindMask's reason: an EBO defined through
// DSA and unbound before its last respecify would otherwise never publish
// it, and getting that bit wrong is invisible in monolith.
File diff suppressed because it is too large Load Diff
@@ -29,9 +29,11 @@ namespace MGITest {
};
bool PeekPipeTextureResourceRecord(unsigned glTextureName, PipeTextureResourceRecordPeek* out);
// The process-wide texture-remint pull count (PipeStats "tex-remint-pulls", `trp=` on the
// The process-wide texture re-mint count (PipeStats "tex-remint-pulls", `trp=` on the
// summary line; ROADMAP open question 2). Arms the PipeStats counters for this process on
// the first call, which is what lets a case read the number without a stats-enabled lane.
// Process-wide means it sees the SERVER's count only where the server is this process:
// monolith and inproc, never spawn or tcp.
bool PeekPipeStatsTextureRemintPulls(unsigned long long* out);
// Espryt's count of texture uploads it actually issued (PipeStats "tex-upload-emissions"):
// what tells a CONSUMED pending upload apart from a DROPPED one, since the record's set is
@@ -25,6 +25,9 @@ foreach(listName IN ITEMS @MGL_SPLIT_ARM_TEST_LISTS@)
elseif(entry MATCHES "PersistentCoherentMapScenario.TheMapLandsInTheArmItsLaneDeclares$")
set_tests_properties("${entry}" PROPERTIES ENVIRONMENT
"MGITEST_PMAP_LANE=1;MGITEST_PERSISTENT_MAP_ARM=emulated;MOBILEGL_PIPE_STATS=1;MOBILEGL_PIPE_STATS_PERIOD=1")
elseif(entry MATCHES "LargeArenaAdoptionScenario.TheArenaLandsInTheTierItsLaneDeclares$")
# P11 A2: every split arm runs T2, so the arena is declared emulated.
set_tests_properties("${entry}" PROPERTIES ENVIRONMENT "MGITEST_PERSISTENT_MAP_ARM=emulated")
elseif(entry MATCHES "CtWireScenario\\.")
set_tests_properties("${entry}" PROPERTIES ENVIRONMENT "MOBILEGL_TEST_SERVER_COUNTERS=1")
endif()
@@ -20,6 +20,7 @@
#include <MG_Remote/Client/CapsMirror.h>
#include <MG_Remote/Client/ClientSession.h>
#include <MG_Remote/Client/EmitTables.h>
#include <MG_Remote/Client/GpuWritePending.h>
#include <MG_Remote/Server/ServerLoop.h>
#include <chrono>
#include <atomic>
@@ -164,6 +165,14 @@ namespace MGITest {
state.cmdWrapPads = encoder.CmdWrapPads();
state.cmdBytesWritten = encoder.CmdBytesWritten();
state.stageReclaimWaits = encoder.StageReclaimWaits();
state.replyPostings = session->ReplyPostings();
state.packBufferReadbackMarks = MobileGL::MG_Remote::Client::ProducerMarkCount(
MobileGL::MG_Remote::Client::GpuWriteProducer::ReadPixelsPackBuffer);
const auto fences = MobileGL::MG_Remote::Client::ReadFencePollCounters();
state.fenceLocalAnswers = fences.LocalAnswers;
state.fenceEscalations = fences.Escalations;
state.fenceRoundTrips = fences.RoundTrips;
state.fenceServerReports = fences.ServerReports;
}
#endif
return state;
@@ -119,6 +119,26 @@ namespace MGITest {
unsigned long long cmdWrapPads = 0;
unsigned long long cmdBytesWritten = 0;
unsigned long long stageReclaimWaits = 0;
// ---- P9 (W1, CONTRACT-P9.md §1): the pack-buffer readback's two proofs -------------
//
// replyPostings: records this client published on a row that carries a reply slot
// (ClientSession::ReplyPostings, always counted - it is the create window's clock, not a
// stats counter). A read into a pack buffer that moves it went through the reply form.
// packBufferReadbackMarks: GpuWritePending's row-4 producer count - the buffers a pack
// read marked GPU-written. The reply form marks nothing, so the pair tells the two forms
// apart from both sides.
unsigned long long replyPostings = 0;
unsigned long long packBufferReadbackMarks = 0;
// ---- P10 (CONTRACT-P10.md §1): what the fence polls cost ----------------------------
// EmitTables.h FencePollCounters, verbatim: polls answered with no record, zero-timeout
// polls escalated to a round trip, every fence answer that crossed, and the server's
// kEventFenceSignaled reports consumed.
unsigned long long fenceLocalAnswers = 0;
unsigned long long fenceEscalations = 0;
unsigned long long fenceRoundTrips = 0;
unsigned long long fenceServerReports = 0;
};
SplitRuntimeState PeekSplitRuntime();
@@ -0,0 +1,38 @@
// MobileGL - MobileGL/MG_IntegrationTest/Harness/WireDeclinePeek.cpp
// Copyright (c) 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
#include "WireDeclinePeek.h"
#if !defined(__ANDROID__)
#include <Config.h>
#if MOBILEGL_BUILD_DISAGGREGATED
#include <MG_Backend/DirectVulkan/Renderer/WireDeclineTally.h>
#include <cstring>
#define MGITEST_WIRE_DECLINE_PEEK_LIVE 1
#endif
#endif
namespace MGITest {
#if defined(MGITEST_WIRE_DECLINE_PEEK_LIVE)
bool PeekWireDeclineCount(const char* row, unsigned long long* out) {
if (row == nullptr || out == nullptr) return false;
namespace Magma = MobileGL::MG_Backend::DirectVulkan;
for (MobileGL::SizeT i = 0; i < Magma::WireDeclineTally::kSiteCount; ++i) {
const auto site = static_cast<Magma::WireDeclineSite>(i);
if (std::strcmp(Magma::WireDeclineSiteName(site), row) != 0) continue;
*out = Magma::WireDeclineTally::Get(site);
return true;
}
return false;
}
#else
bool PeekWireDeclineCount(const char*, unsigned long long*) { return false; }
#endif
} // namespace MGITest
@@ -0,0 +1,20 @@
// MobileGL - MobileGL/MG_IntegrationTest/Harness/WireDeclinePeek.h
// Copyright (c) 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
//
// P8-SV: one row of the Magma wire arm's decline tally (WireDeclines.def, the counts behind the
// server's MGWIRE-DECLINES lines), read as a run total out of THIS process - WireIndirectPeek.h's
// shape. Process-wide means the SERVER's counts only where the server is this process: the inproc
// arm, never spawn or tcp, and never the monolith arm, which takes no wire path. False where the
// reading cannot be taken: a pull build, Android, or a name that is not a row.
#pragma once
namespace MGITest {
bool PeekWireDeclineCount(const char* row, unsigned long long* out);
} // namespace MGITest
@@ -0,0 +1,38 @@
// MobileGL - MobileGL/MG_IntegrationTest/Harness/WireIndirectPeek.cpp
// Copyright (c) 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
#include "WireIndirectPeek.h"
#if !defined(__ANDROID__)
#include <MG_Pipe/MGPipe.h>
#if MOBILEGL_PIPE_PUSH
#include <MG_Util/Metrics/PipeStats.h>
#define MGITEST_WIRE_INDIRECT_PEEK_LIVE 1
#endif
#endif
namespace MGITest {
#if defined(MGITEST_WIRE_INDIRECT_PEEK_LIVE)
bool PeekWireIndirectCounters(WireIndirectCounters* out) {
if (out == nullptr) return false;
namespace Stats = MobileGL::MG_Util::PipeStats;
if (!Stats::Enabled()) Stats::SetEnabledForTesting(true);
out->nativeDraws = Stats::TotalCalls(Stats::CallClass::WireIndirectNativeDraws);
out->cpuExpansions = Stats::TotalCalls(Stats::CallClass::WireIndirectCpuExpansions);
out->barriers = Stats::TotalCalls(Stats::CallClass::WireIndirectBarriers);
out->hostWaits = Stats::TotalCalls(Stats::CallClass::WireHostWaits);
out->indirectWaits = Stats::TotalCalls(Stats::CallClass::WireHostWaitsIndirect);
out->nativeDispatches = Stats::TotalCalls(Stats::CallClass::WireIndirectNativeDispatches);
return true;
}
#else
bool PeekWireIndirectCounters(WireIndirectCounters*) { return false; }
#endif
} // namespace MGITest
@@ -0,0 +1,29 @@
// MobileGL - MobileGL/MG_IntegrationTest/Harness/WireIndirectPeek.h
// Copyright (c) 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
//
// P8-D: the Magma wire arm's indirect-draw counters (PipeStats `windr[...]`), read as run totals
// out of THIS process - P4aFinalFixPeek.cpp's shape. Process-wide means the SERVER's counts only
// where the server is this process: monolith and inproc, never spawn or tcp. The first call arms
// the counters (a case reads before and after its workload, so it must peek before it draws).
// False where the reading cannot be taken: a pull build, Android.
#pragma once
namespace MGITest {
struct WireIndirectCounters {
unsigned long long nativeDraws = 0; // wire-indirect-native-draws (`wind`)
unsigned long long cpuExpansions = 0; // wire-indirect-cpu-expansions (`wixp`)
unsigned long long barriers = 0; // wire-indirect-barriers (`wibar`)
unsigned long long hostWaits = 0; // wire-host-waits (`whw`)
unsigned long long indirectWaits = 0; // wire-host-waits-indirect (`whwi`)
unsigned long long nativeDispatches = 0; // P8-SV: wire-indirect-native-dispatches (`wdsp`)
};
bool PeekWireIndirectCounters(WireIndirectCounters* out);
} // namespace MGITest
@@ -0,0 +1,244 @@
# P8-A (ID-P8-2): the monolith cases' split, spawn and tcp registrations.
#
# Included ONCE from CMakeLists.txt, inside `if (MOBILEGL_BUILD_DISAGGREGATED)`, and the place is
# load-bearing twice over: it must come BEFORE the Magma tier-2 replay (every
# mgl_itest_register_split_arms call here is replayed onto DirectVulkan's informational
# integration-magma-all-{split,spawn,tcp} arms from MGL_SPLIT_ARM_CALLS) and BEFORE the
# SplitLogPaths configure_file at the end of that file (every entry here gets its private
# MOBILEGL_LOG_FILE_PATH from MGL_SPLIT_ARM_TEST_LISTS, or SplitLogPaths.PrivateAndDistinct reds).
#
# THE GATE IS scripts/ci/split_coverage.py: after the arm and tail segments come off, every
# monolith DirectGLES. / DirectVulkan. case is on a gated split arm or is named, with a reason
# class, in scripts/data/split_coverage_exemptions.txt. Deleting a line below reds it BY NAME.
# The per-case census this list was cut from (inproc / spawn / tcp, junit per arm) is
# docs/Disaggregated/notes/p8/A.md.
# ---- (a) THE AMBIENT CASES -----------------------------------------------------------------
#
# Every case here was registered on monolith only, and on all three arms under the armed split
# environment it either PASSED (records crossed: ScenarioFixture's EmitSeq assertion is armed on
# these arms) or SKIPPED for the same reason its monolith entry skips on this host (ClipDistance,
# DualSourceBlend, IterationRP*, SsboDeclarationForm's two known gaps, and the knob-pinned cases
# the (a') block below runs with their knob). Cases that belong to monolith by nature are in the
# exemption table instead, never here: no-records (limits / shader-binary queries answered from
# the caps mirror), single-backend (Magma-only lanes and declines), mechanism, pending-fix.
#
# ONE FILTER, not one call per scenario: each call is three discovery runs plus three more for
# the Magma replay. Explicit names where a scenario is split between registered and exempt
# cases; `Scenario.*` only where every case of the scenario is here.
set(MGL_P8A_AMBIENT_CASES
AdvertisedLimitsScenario.ImageUnitBindingsAreReportedFromTheFrontendState
AdvertisedLimitsScenario.IndexedBufferBindingsAreReportedVerbatimOnBothWidths
BufferTextureScenario.*
ClearTexImageUndefinedLevelZeroScenario.*
ClipDistanceScenario.*
CopyImageLevelRangeScenario.*
CopyImagePacked16Scenario.*
CrossFrameBufferScenario.*
DefaultFramebufferAcrossSwapScenario.*
DepthStencilReadbackAttachmentShapeScenario.DepthOfALayeredCubeAttachmentReadsBack
DepthStencilReadbackAttachmentShapeScenario.DepthOfAPlainTexture2DAttachmentReadsBack
DepthStencilReadbackAttachmentShapeScenario.DepthOfAnArrayLayerAttachmentReadsBack
DepthStencilReadbackAttachmentShapeScenario.PackedArrayLayerAttachmentReadsBackBothAspects
DepthStencilReadbackScenario.*
DoublePrecisionScenario.*
DualSourceBlendScenario.*
EmptyScissorScenario.*
F1WireScenario.ColorBlitToDefaultFromANonSampleableSourceDegrades
F1WireScenario.ColorBlitToDefaultHonorsScissorAndReversedRect
F1WireScenario.ColorBlitToDefaultIgnoresViewportAndPreservesOrientation
F1WireScenario.ComputeImageStoreFramebufferPixels
F1WireScenario.ComputeImageStoreThroughViewPreservesOtherRootLayer
F1WireScenario.ComputeWrittenVertexAndIndexBuffersDrawAndReadBack
F1WireScenario.CopyImageInPlaceAcrossLevelsAndLayers
F1WireScenario.EnabledVertexBufferDrawKeepsNamedP7Fatal
F1WireScenario.GenerateMipmapHonorsMutableBaseAndMaxWithoutChangingOtherLevels
F1WireScenario.GenerateMipmapThroughViewKeepsOwnerLevelsAndLayersOutsideItsWindow
F1WireScenario.MultisampleColorBlitScalesAndFlipsThroughTheResolveScratch
F1WireScenario.PartialTextureUploadPreservesGpuClearPixels
F1WireScenario.SrgbDrawTracksFramebufferConversion
F1WireScenario.StreamedBufferSubDataBeforeEachDrawIsOrdered
F1WireScenario.Texture1DFramebufferClearPixels
F1WireScenario.TextureReadbackLargerThanAReplySlotContainsGpuWrites
F1WireScenario.TextureViewClearTargetsItsRootMipAndLayer
F1WireScenario.UniformBufferRangeAndRebindPixels
F1WireScenario.VertexIdSamplerAndScalarUniformPixels
FormatlessImageBakeScenario.*
FragCoordOriginScenario.*
FragmentOutputArrayIndexScenario.*
GeometryDrawModeScenario.*
GuiBatchScenario.*
# dev f973008c merge: the empty-binding uniform block (853c5f12 + P11 7784d6bb)
UnboundUniformBlockScenario.*
ImageFormatQualifierScenario.*
ImageLoadStoreSsoScenario.PerElementImageUnitsReachAPipelineDraw
ImageLoadStoreSsoScenario.ReassigningAnImageUnitBetweenDrawsReachesTheNextDraw
IterationRPFirstReductionScenario.*
IterationRPProgram203Scenario.*
IterationRPScratchFixScenario.*
LayeredAttachmentBarrierScenario.*
LayeredAttachmentShapeScenario.*
NonCoreImageFormatScenario.*
OrientationScenario.*
P4aFinalFixScenario.*
P4aSeamAuditScenario.*
PipelineFailureScenario.*
PointSizeDemotionScenario.*
ProgramPipelineScenario.ASamplerUnitRewrittenBetweenDrawsKeepsPaintingTheRightTexture
ProgramPipelineScenario.AStageProgramsUniformBlockBindingReachesThePipelineDraw
ProgramPipelineScenario.ATwoStagePipelinePaintsWhatItsStagesDescribe
ProgramPipelineScenario.AUniformDeclaredInTwoStagesKeepsTheValueTheWrittenStageHolds
ProgramPipelineScenario.ComputeAndGraphicsStagesShareOnePipeline
ProgramPipelineScenario.RebindingAUniformBlockBetweenDrawsReachesTheNextDraw
ProgramPipelineScenario.SwitchingBetweenAPipelineAndAMonolithicProgramLeavesNoError
ProgramPipelineScenario.UniformsGoToTheActiveShaderProgram
RenderbufferBlendFormatScenario.*
ResidentIndexScenario.*
SampleMaskScopeScenario.*
SampleVariablesScenario.*
SnormAttachmentScenario.*
SpirvShaderBinaryScenario.SpecializedModulesLinkAndRenderWithTheirConstantsApplied
SsboArrayDynamicIndexScenario.*
SsboArrayLengthScenario.*
SsboDeclarationFormScenario.*
StorageBufferRegrowScenario.AGrownStoreIsVisibleThroughItsExistingIndexedBinding
StreamedArenaScenario.*
TessellationDrawModeScenario.*
ThreeChannelAttachmentScenario.*
UnboundCounterBlockScenario.*
UnlocatedIoBlockScenario.*
UnwrittenPositionOutputScenario.ARedeclaredButUnwrittenPositionStillDraws
UnwrittenPositionOutputScenario.AShaderWithNoPositionBlockStillDraws
UnwrittenPositionOutputScenario.AWrittenRedeclaredPositionStillDraws
UnwrittenPositionOutputScenario.CapturingAWrittenPositionStillDraws
VertexArrayEnableDisableScenario.*
VertexAttribBindingScenario.*
ViewportArrayScenario.*
)
list(JOIN MGL_P8A_AMBIENT_CASES ":" mglP8aAmbientFilter)
mgl_itest_register_split_arms("${mglP8aAmbientFilter}" "")
# ---- (a') THE SERVER-READ KNOB LANES, EACH WITH A TCP SUPERVISOR OF ITS OWN ------------------
#
# The monolith file pins five whole-process knobs in lanes of their own (PointSizeDemotion.,
# UnlocatedIoBlocks., NoViewportArrayEmulation., WidenedPacked16., ForcedDepthStencilEmulation.),
# because without them the cases are unfalsifiable on llvmpipe or skip. Every one of the five is
# read through ConfigLoader IN THE PROCESS THAT RUNS THE BACKEND - the server. Under inproc that
# is this process and under spawn the child inherits ::environ, so the entry's ENVIRONMENT
# reaches it; under tcp the server is a supervisor started by a fixture, and the lane-wide
# TcpServer.Start supervisor never sees a per-entry variable. MEASURED on this tree before this
# block existed: with the knob in the client's environment only, the Tcp copies of
# PointSizeDemotion's and UnlocatedIoBlocks' arming cases read no arming line and
# NoViewportArrayEmulation's negative control saw the emulation still on - red, where the Split
# and Spawn copies were green.
#
# THE KNOB CANNOT GO ON THE SHARED FIXTURE (the IC TcpServer.Start precedent, which works for
# MOBILEGL_ESPRYT_ENABLE_TEXTURE_VIEW only because every arm sets that one): it would retarget
# every other tcp case - viewport emulation off breaks ViewportArrayScenario's positive cases,
# the IO-block strip rewrites every interface block. So each lane gets a supervisor of its own,
# on its own port, started by its own fixture with the knob in the fixture's ENVIRONMENT.
#
# THE PORTS: the lane endpoint's port + MOBILEGL_ITEST_TCP_KNOB_PORT_OFFSET + the lane's index.
# The default offset (1000) keeps the five clear of every per-tree lane endpoint in use (40613
# CI, 407xx / 409xx split and verify builds, 484x0 package trees). One overlap remains: a tree
# whose split and verify builds sit one port apart (409x3 / 409x4) gets knob ports one apart as
# well, so the two builds must not run integration-tcp at the same moment - set the cache
# variable differently in one of them if they ever do.
#
# THE LOCK IS STILL mobilegl-tcp: spawn_lane_parity.py / junit_tally.py require it of every
# integration-tcp entry, and one tcp session at a time is what every other tcp entry assumes.
#
# NOT FOR DirectVulkan: all five knobs are Espryt's (Magma routes viewports natively, has no
# IO-block strip, no packed-16 widening, no DS readback emulation; point-size demotion's Magma
# lane is monolith-only and stays so here).
set(MOBILEGL_ITEST_TCP_KNOB_PORT_OFFSET 1000 CACHE STRING
"Port offset (from MOBILEGL_ITEST_TCP_ENDPOINT's) of the per-knob loopback supervisors")
if(NOT MOBILEGL_ITEST_TCP_ENDPOINT MATCHES "^(tcp://.+):([0-9]+)$")
message(FATAL_ERROR "P8-A: MOBILEGL_ITEST_TCP_ENDPOINT '${MOBILEGL_ITEST_TCP_ENDPOINT}' is not "
"tcp://<host>:<port>; the per-knob tcp supervisors derive their ports from it")
endif()
set(mglP8aTcpHost "${CMAKE_MATCH_1}")
set(mglP8aTcpPort "${CMAKE_MATCH_2}")
set(MGL_P8A_KNOB_LANES PointSizeDemotion UnlocatedIoBlocks NoViewportArrayEmulation WidenedPacked16
ForcedDs)
set(MGL_P8A_KNOB_FILTER_PointSizeDemotion "PointSizeDemotionScenario.*")
set(MGL_P8A_KNOB_ENV_PointSizeDemotion "MOBILEGL_POINT_SIZE_DEMOTION=1")
set(MGL_P8A_KNOB_FILTER_UnlocatedIoBlocks "UnlocatedIoBlockScenario.*")
set(MGL_P8A_KNOB_ENV_UnlocatedIoBlocks "MOBILEGL_ESPRYT_UNLOCATED_IO_BLOCKS=1")
# One case, as on monolith: the three positive cases describe behaviour the backend does not
# have with the emulation off.
set(MGL_P8A_KNOB_FILTER_NoViewportArrayEmulation
"ViewportArrayScenario.WithoutTheEmulationEveryIndexCollapsesOntoViewportZero")
set(MGL_P8A_KNOB_ENV_NoViewportArrayEmulation "MOBILEGL_ESPRYT_FORCE_VIEWPORT_ARRAY_EMULATION=0")
set(MGL_P8A_KNOB_FILTER_WidenedPacked16 "CopyImagePacked16Scenario.*")
set(MGL_P8A_KNOB_ENV_WidenedPacked16 "MOBILEGL_ESPRYT_WIDEN_PACKED16_STORAGE=1")
# ForcedDs keeps the spelling the existing Split/Spawn DepthStencilReadbackMatrix block uses. The
# default-framebuffer case was left out here while it was red on spawn and tcp; P8-SE fixed it
# (notes/p8/SE.md) and it runs under the knob on every arm like the rest.
set(MGL_P8A_KNOB_FILTER_ForcedDs
"DepthStencilReadbackScenario.*:DepthStencilReadbackAttachmentShapeScenario.*")
set(MGL_P8A_KNOB_ENV_ForcedDs "MOBILEGL_ESPRYT_FORCE_DS_READBACK_EMULATION=1")
# The Matrix scenario's Tcp copy moves HERE from the ForcedDs block (same names): that block
# put the knob in the client's environment only, so its Tcp arm ran the native readback path.
set(MGL_P8A_KNOB_TCP_FILTER_ForcedDs
"DepthStencilReadbackMatrixScenario.*:${MGL_P8A_KNOB_FILTER_ForcedDs}")
set(mglP8aKnobIndex 0)
foreach(mglP8aLane IN LISTS MGL_P8A_KNOB_LANES)
string(TOLOWER "${mglP8aLane}" mglP8aLaneLower)
math(EXPR mglP8aKnobPort "${mglP8aTcpPort} + ${MOBILEGL_ITEST_TCP_KNOB_PORT_OFFSET} + ${mglP8aKnobIndex}")
math(EXPR mglP8aKnobIndex "${mglP8aKnobIndex} + 1")
set(mglP8aKnobEndpoint "${mglP8aTcpHost}:${mglP8aKnobPort}")
set(mglP8aKnobFixture "mobilegl-tcp-p8a-${mglP8aLaneLower}")
set(mglP8aKnobState "${CMAKE_CURRENT_BINARY_DIR}/tcp-server-p8a-${mglP8aLaneLower}.json")
set(mglP8aKnob "${MGL_P8A_KNOB_ENV_${mglP8aLane}}")
add_test(NAME TcpServer.Start.P8a${mglP8aLane} COMMAND "${Python3_EXECUTABLE}" "${MGL_TCP_FIXTURE}"
start --server "${MGL_ITEST_SPLIT_SERVER_PATH}" --endpoint "${mglP8aKnobEndpoint}"
--state "${mglP8aKnobState}")
add_test(NAME TcpServer.Stop.P8a${mglP8aLane} COMMAND "${Python3_EXECUTABLE}" "${MGL_TCP_FIXTURE}"
stop --state "${mglP8aKnobState}")
# The texture-view opt-in as on the shared fixture, so this supervisor differs from it in
# the knob alone.
set_tests_properties(TcpServer.Start.P8a${mglP8aLane} PROPERTIES
FIXTURES_SETUP ${mglP8aKnobFixture} TIMEOUT 20
ENVIRONMENT "MOBILEGL_ESPRYT_ENABLE_TEXTURE_VIEW=1;${mglP8aKnob}")
set_tests_properties(TcpServer.Stop.P8a${mglP8aLane} PROPERTIES
FIXTURES_CLEANUP ${mglP8aKnobFixture} TIMEOUT 20)
foreach(mglP8aArm Split Spawn Tcp)
string(TOLOWER "${mglP8aArm}" mglP8aArmLower)
string(TOUPPER "${mglP8aArm}" mglP8aArmUpper)
set(mglP8aEnvironment "${MGL_ITEST_GLES_${mglP8aArmUpper}_ENVIRONMENT}")
set(mglP8aFilter "${MGL_P8A_KNOB_FILTER_${mglP8aLane}}")
set(mglP8aProperties)
if(mglP8aArm STREQUAL "Tcp")
string(REPLACE "MOBILEGL_IPC_CONTROL=${MOBILEGL_ITEST_TCP_ENDPOINT}"
"MOBILEGL_IPC_CONTROL=${mglP8aKnobEndpoint}"
mglP8aEnvironment "${mglP8aEnvironment}")
if(mglP8aEnvironment STREQUAL MGL_ITEST_GLES_TCP_ENVIRONMENT)
message(FATAL_ERROR "P8-A: the Tcp arm environment names no "
"MOBILEGL_IPC_CONTROL=${MOBILEGL_ITEST_TCP_ENDPOINT} to retarget")
endif()
set(mglP8aProperties FIXTURES_REQUIRED ${mglP8aKnobFixture} RESOURCE_LOCK mobilegl-tcp)
if(DEFINED MGL_P8A_KNOB_TCP_FILTER_${mglP8aLane})
set(mglP8aFilter "${MGL_P8A_KNOB_TCP_FILTER_${mglP8aLane}}")
endif()
endif()
gtest_discover_tests(MobileGLIntegrationTest
TEST_PREFIX "DirectGLES.${mglP8aArm}.${mglP8aLane}."
TEST_LIST "MGL_SPLIT_ARM_TESTS_${MGL_SPLIT_ARM_INDEX}"
TEST_FILTER "${mglP8aFilter}"
DISCOVERY_TIMEOUT 30
PROPERTIES
LABELS "integration-gpu\;integration-${mglP8aArmLower}"
TIMEOUT ${MGL_ITEST_TIMEOUT}
ENVIRONMENT "${mglP8aEnvironment}\;${mglP8aKnob}"
${mglP8aProperties}
)
list(APPEND MGL_SPLIT_ARM_TEST_LISTS "MGL_SPLIT_ARM_TESTS_${MGL_SPLIT_ARM_INDEX}")
math(EXPR MGL_SPLIT_ARM_INDEX "${MGL_SPLIT_ARM_INDEX} + 1")
endforeach()
endforeach()
message(STATUS "Integration tests: P8-A registers ${mglP8aKnobIndex} server-knob lane(s) on "
"split/spawn/tcp, each tcp arm on its own supervisor")
@@ -0,0 +1,373 @@
// MobileGL - MobileGL/MG_IntegrationTest/Scenarios/CopyImageStoreReadbackScenario.cpp
// Copyright (c) 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
//
// Scenario - glCopyImageSubData INTO A LEVEL THE DRIVER WILL NOT READ BACK (P8-E,
// docs/Disaggregated/notes/p8/E.md).
//
// The formats are the ones an ES driver keeps as texture-only storage: GL_RGB16 (EXT_texture_norm16
// makes the three-channel spelling sampleable, never colour-renderable) and the 16-bit SNORM family
// (renderable only with EXT_render_snorm, which Adreno does not have). Their glGetTexImage cannot go
// through a framebuffer on such a driver, so something other than the GPU image has to answer it:
// the CPU shadow on the monolith arm, the server's staged store on a split one. A copy writes the
// GPU image and nothing else, which is exactly the write such a byte store never sees.
//
// GL_RGB9_E5 is the positive control: a 32-bit packed format, read through the scratch GL_R32UI
// image whoever wrote the level, so it must stay green under every configuration below.
//
// ARMS. Both backends on the monolith arm and on Split / Spawn / Tcp, every case, with no knob:
// on llvmpipe every one of these formats is attachable, so these runs pin the GPU route and the
// copy itself. Magma is the control backend - it reads the VkImage back whatever the format, so it
// has no store route to exercise.
//
// THE KNOB. MGITEST_ESPRYT_REFUSE_TEXTURE_READBACK_EXTENT=<w>x<h> is read BY THE ESPRYT SERVER
// (WireTextureReadback.inc, ReadTextureImageWire): a colour level of exactly that extent is
// treated as a level the driver refused to attach, which is what an Adreno does for every one of
// these formats and what llvmpipe can be made to do no other way. The `DirectGLES.<arm>.StoreRead.`
// entries set it, and this scenario then builds its textures at THAT extent instead of the default
// one - so the knob is scoped by the extent, and the tcp lane's shared supervisor can carry it
// (the TcpServer.Start fixture's ENVIRONMENT) without touching any other entry, this scenario's
// own knob-free tcp entries included. Under the knob every read must still be right, and the
// server log must name the store route - otherwise the entry would be pinning the GPU route a
// second time under a name that claims otherwise.
#include <cstdint>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <string>
#include <vector>
#include "../Harness/HeadlessGL.h"
#include "../Harness/PipeStatsWindow.h"
#include "../Harness/ScenarioFixture.h"
#include "../Harness/SplitRuntimePeek.h"
#ifdef GLAPI
#undef GLAPI
#endif
#define GL_GLEXT_PROTOTYPES
#include <GL/gl.h>
#include <GL/glcorearb.h>
#undef GL_GLEXT_PROTOTYPES
namespace MGITest {
namespace {
constexpr const char* kRefuseExtentKnob = "MGITEST_ESPRYT_REFUSE_TEXTURE_READBACK_EXTENT";
// What the Espryt server logs (once per process) when it answers a read from its store.
constexpr const char* kStoreReadMarker = "texture-readback-from-store";
// The knob-free extent. Deliberately not the extent the knob entries use, so the tcp
// supervisor - which carries the knob for the whole lane - leaves these textures alone.
constexpr int kDefaultWidth = 12;
constexpr int kDefaultHeight = 6;
struct CopyFormat {
GLenum internalFormat;
GLenum format;
GLenum type;
int components; // 16-bit components per texel; 0 for the one 32-bit packed format
bool isSigned;
const char* name;
};
const CopyFormat kFormats[] = {
{GL_RGB16, GL_RGB, GL_UNSIGNED_SHORT, 3, false, "rgb16"},
{GL_RGBA16_SNORM, GL_RGBA, GL_SHORT, 4, true, "rgba16_snorm"},
{GL_RGB16_SNORM, GL_RGB, GL_SHORT, 3, true, "rgb16_snorm"},
{GL_RGBA16, GL_RGBA, GL_UNSIGNED_SHORT, 4, false, "rgba16"},
{GL_RGB9_E5, GL_RGB, GL_UNSIGNED_INT_5_9_9_9_REV, 0, false, "rgb9_e5"},
};
int TexelBytes(const CopyFormat& format) { return format.components == 0 ? 4 : format.components * 2; }
// Every word differs from its neighbours and from the other patterns, so a mis-addressed
// row, a swapped endpoint or a copy that never happened cannot cancel out. SNORM never
// gets -32768: it and -32767 are the same value (-1.0), and a read that goes through a
// normalized float is entitled to answer either.
std::vector<std::uint8_t> MakeTexels(const CopyFormat& format, int width, int height, std::uint32_t seed) {
const std::size_t texels = static_cast<std::size_t>(width) * static_cast<std::size_t>(height);
std::vector<std::uint8_t> bytes(texels * static_cast<std::size_t>(TexelBytes(format)));
if (format.components == 0) {
for (std::size_t i = 0; i < texels; ++i) {
std::uint32_t word = (static_cast<std::uint32_t>(i) + 1u) * 2654435761u ^ seed * 40503u;
word ^= word >> 13;
std::memcpy(bytes.data() + i * 4, &word, 4);
}
return bytes;
}
const std::size_t words = texels * static_cast<std::size_t>(format.components);
for (std::size_t i = 0; i < words; ++i) {
std::uint32_t mix = (static_cast<std::uint32_t>(i) + 1u) * 2654435761u ^ seed * 40503u;
mix ^= mix >> 15;
std::uint16_t value = static_cast<std::uint16_t>(mix >> 8);
if (format.isSigned && value == 0x8000u) value = 0x8001u;
std::memcpy(bytes.data() + i * 2, &value, 2);
}
return bytes;
}
// `image` with the w x h block at (dx, dy) replaced by the block of `source` at (sx, sy).
std::vector<std::uint8_t> WithBlock(std::vector<std::uint8_t> image, int width, const std::vector<std::uint8_t>& source,
int sourceWidth, int sx, int sy, int dx, int dy, int w, int h,
int texelBytes) {
for (int row = 0; row < h; ++row) {
std::memcpy(image.data() + (static_cast<std::size_t>(dy + row) * width + dx) * texelBytes,
source.data() + (static_cast<std::size_t>(sy + row) * sourceWidth + sx) * texelBytes,
static_cast<std::size_t>(w) * texelBytes);
}
return image;
}
class CopyImageStoreReadbackScenario : public ScenarioTest {
protected:
void SetUp() override {
ScenarioTest::SetUp();
if (!Ready()) return;
m_wire = SplitRuntimeSkipReason().empty();
m_espryt = Gl().BackendName() == "DirectGLES";
if (const char* knob = std::getenv(kRefuseExtentKnob)) {
int width = 0, height = 0;
ASSERT_EQ(std::sscanf(knob, "%dx%d", &width, &height), 2)
<< kRefuseExtentKnob << "=" << knob << " is not <w>x<h>";
ASSERT_GE(width, 12) << "the copy windows below need at least a 12x6 level";
ASSERT_GE(height, 6) << "the copy windows below need at least a 12x6 level";
m_width = width;
m_height = height;
m_forced = true;
}
// 16-bit RGB rows are 6 bytes a texel: the default 4-byte alignment would pad every
// row of an odd-width level and shear both the upload and the read.
glPixelStorei(GL_UNPACK_ALIGNMENT, 1);
glPixelStorei(GL_PACK_ALIGNMENT, 1);
ASSERT_EQ(FirstGLError(), 0u);
}
void TearDown() override {
if (!Ready()) return;
glPixelStorei(GL_UNPACK_ALIGNMENT, 4);
glPixelStorei(GL_PACK_ALIGNMENT, 4);
glBindTexture(GL_TEXTURE_2D, 0);
if (!m_textures.empty()) {
glDeleteTextures(static_cast<GLsizei>(m_textures.size()), m_textures.data());
}
if (m_renderbuffer != 0) glDeleteRenderbuffers(1, &m_renderbuffer);
if (m_fbo != 0) {
BindDefaultFramebuffer();
glDeleteFramebuffers(1, &m_fbo);
}
FirstGLError();
}
GLuint MakeTexture(const CopyFormat& format, const std::vector<std::uint8_t>& texels, bool immutable) {
GLuint texture = 0;
glGenTextures(1, &texture);
m_textures.push_back(texture);
glBindTexture(GL_TEXTURE_2D, texture);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
if (immutable) {
glTexStorage2D(GL_TEXTURE_2D, 1, format.internalFormat, m_width, m_height);
glTexSubImage2D(GL_TEXTURE_2D, 0, 0, 0, m_width, m_height, format.format, format.type,
texels.data());
} else {
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAX_LEVEL, 0);
glTexImage2D(GL_TEXTURE_2D, 0, static_cast<GLint>(format.internalFormat), m_width, m_height, 0,
format.format, format.type, texels.data());
}
glBindTexture(GL_TEXTURE_2D, 0);
return texture;
}
std::vector<std::uint8_t> ReadTexImage(GLuint texture, const CopyFormat& format) {
std::vector<std::uint8_t> bytes(
static_cast<std::size_t>(m_width) * m_height * static_cast<std::size_t>(TexelBytes(format)), 0xA5);
glBindTexture(GL_TEXTURE_2D, texture);
glGetTexImage(GL_TEXTURE_2D, 0, format.format, format.type, bytes.data());
glBindTexture(GL_TEXTURE_2D, 0);
return bytes;
}
// The first texel that differs names the defect (which half of the copy, which word);
// the rest would only bury it.
void ExpectImage(const std::vector<std::uint8_t>& got, const std::vector<std::uint8_t>& want,
const CopyFormat& format, const std::string& what) {
ASSERT_EQ(got.size(), want.size());
const int texelBytes = TexelBytes(format);
for (std::size_t at = 0; at < got.size(); ++at) {
if (got[at] == want[at]) continue;
const std::size_t texel = at / static_cast<std::size_t>(texelBytes);
const int x = static_cast<int>(texel % static_cast<std::size_t>(m_width));
const int y = static_cast<int>(texel / static_cast<std::size_t>(m_width));
std::string gotHex, wantHex;
char buffer[4];
for (int b = 0; b < texelBytes; ++b) {
std::snprintf(buffer, sizeof(buffer), "%02x", got[texel * texelBytes + b]);
gotHex += buffer;
std::snprintf(buffer, sizeof(buffer), "%02x", want[texel * texelBytes + b]);
wantHex += buffer;
}
ADD_FAILURE() << what << " (" << format.name << ", " << Gl().BackendName() << ", "
<< (m_wire ? "split" : "monolith") << (m_forced ? ", store read forced" : "")
<< "): texel (" << x << ", " << y << ") reads " << gotHex << ", want " << wantHex;
return;
}
}
// Under the knob the Espryt server must have answered from its store at least once;
// otherwise the entry proves nothing about that route.
void ExpectStoreReadNamedSince(const PipeStatsWindow::LogMark& mark) {
if (!m_forced || !m_espryt || !m_wire) return;
const std::string log = PipeStatsWindow::ReadServerLogSince(mark);
EXPECT_NE(log.find(kStoreReadMarker), std::string::npos)
<< kRefuseExtentKnob << " is set but the server log carries no '" << kStoreReadMarker
<< "' line: the knob did not reach the server, or no read took the store route";
}
// The copy window: 5x3 texels from (1, 1) of the source to (6, 2) of the destination,
// inside a 12x6 level and away from the partial upload's (0, 0) 3x2 corner.
static constexpr int kSrcX = 1, kSrcY = 1, kDstX = 6, kDstY = 2, kCopyW = 5, kCopyH = 3;
void CopyWindow(GLuint source, GLuint destination) {
glCopyImageSubData(source, GL_TEXTURE_2D, 0, kSrcX, kSrcY, 0, destination, GL_TEXTURE_2D, 0, kDstX,
kDstY, 0, kCopyW, kCopyH, 1);
}
bool m_wire = false;
bool m_espryt = false;
bool m_forced = false;
int m_width = kDefaultWidth;
int m_height = kDefaultHeight;
std::vector<GLuint> m_textures;
GLuint m_renderbuffer = 0;
GLuint m_fbo = 0;
};
// The control: an uploaded level reads back as uploaded.
TEST_F(CopyImageStoreReadbackScenario, AnUploadedLevelReadsBackExactly) {
if (!Ready()) return;
const auto mark = PipeStatsWindow::MarkLaneLog();
for (const CopyFormat& format : kFormats) {
const auto texels = MakeTexels(format, m_width, m_height, 11);
const GLuint texture = MakeTexture(format, texels, /*immutable=*/false);
ASSERT_EQ(FirstGLError(), 0u) << format.name << ": setup raised a GL error";
ExpectImage(ReadTexImage(texture, format), texels, format, "the uploaded level");
ASSERT_EQ(FirstGLError(), 0u) << format.name << ": glGetTexImage raised a GL error";
}
ExpectStoreReadNamedSince(mark);
}
// THE CASE THE PACKAGE IS ABOUT: the copy lands in the destination's GPU image only, and the
// read has to see it - whatever answers the read.
TEST_F(CopyImageStoreReadbackScenario, ACopiedWindowReadsBackOverTheUpload) {
if (!Ready()) return;
const auto mark = PipeStatsWindow::MarkLaneLog();
for (const CopyFormat& format : kFormats) {
const auto sourceTexels = MakeTexels(format, m_width, m_height, 23);
const auto destinationTexels = MakeTexels(format, m_width, m_height, 37);
const GLuint source = MakeTexture(format, sourceTexels, /*immutable=*/false);
const GLuint destination = MakeTexture(format, destinationTexels, /*immutable=*/false);
ASSERT_EQ(FirstGLError(), 0u) << format.name << ": setup raised a GL error";
CopyWindow(source, destination);
ASSERT_EQ(FirstGLError(), 0u) << format.name << ": glCopyImageSubData raised a GL error";
const auto want = WithBlock(destinationTexels, m_width, sourceTexels, m_width, kSrcX, kSrcY, kDstX,
kDstY, kCopyW, kCopyH, TexelBytes(format));
ExpectImage(ReadTexImage(destination, format), want, format, "the copy destination");
ExpectImage(ReadTexImage(source, format), sourceTexels, format, "the copy source");
ASSERT_EQ(FirstGLError(), 0u) << format.name << ": glGetTexImage raised a GL error";
}
ExpectStoreReadNamedSince(mark);
}
// The same into immutable storage (glTexStorage2D, then the whole level uploaded).
TEST_F(CopyImageStoreReadbackScenario, ACopiedWindowReadsBackFromAnImmutableLevel) {
if (!Ready()) return;
const auto mark = PipeStatsWindow::MarkLaneLog();
for (const CopyFormat& format : kFormats) {
const auto sourceTexels = MakeTexels(format, m_width, m_height, 41);
const auto destinationTexels = MakeTexels(format, m_width, m_height, 43);
const GLuint source = MakeTexture(format, sourceTexels, /*immutable=*/true);
const GLuint destination = MakeTexture(format, destinationTexels, /*immutable=*/true);
ASSERT_EQ(FirstGLError(), 0u) << format.name << ": setup raised a GL error";
CopyWindow(source, destination);
ASSERT_EQ(FirstGLError(), 0u) << format.name << ": glCopyImageSubData raised a GL error";
const auto want = WithBlock(destinationTexels, m_width, sourceTexels, m_width, kSrcX, kSrcY, kDstX,
kDstY, kCopyW, kCopyH, TexelBytes(format));
ExpectImage(ReadTexImage(destination, format), want, format, "the immutable copy destination");
ASSERT_EQ(FirstGLError(), 0u) << format.name << ": glGetTexImage raised a GL error";
}
ExpectStoreReadNamedSince(mark);
}
// A copy, then an ordinary partial upload into the same level elsewhere. The upload is the
// client's; the copied window is not, and a byte store that took the upload as "the level"
// would lose it again.
TEST_F(CopyImageStoreReadbackScenario, APartialUploadAfterTheCopyKeepsTheCopiedWindow) {
if (!Ready()) return;
const auto mark = PipeStatsWindow::MarkLaneLog();
for (const CopyFormat& format : kFormats) {
const auto sourceTexels = MakeTexels(format, m_width, m_height, 53);
const auto destinationTexels = MakeTexels(format, m_width, m_height, 59);
const auto patch = MakeTexels(format, 3, 2, 61);
const GLuint source = MakeTexture(format, sourceTexels, /*immutable=*/false);
const GLuint destination = MakeTexture(format, destinationTexels, /*immutable=*/false);
ASSERT_EQ(FirstGLError(), 0u) << format.name << ": setup raised a GL error";
CopyWindow(source, destination);
glBindTexture(GL_TEXTURE_2D, destination);
glTexSubImage2D(GL_TEXTURE_2D, 0, 0, 0, 3, 2, format.format, format.type, patch.data());
glBindTexture(GL_TEXTURE_2D, 0);
ASSERT_EQ(FirstGLError(), 0u) << format.name << ": the copy or the upload raised a GL error";
auto want = WithBlock(destinationTexels, m_width, sourceTexels, m_width, kSrcX, kSrcY, kDstX, kDstY,
kCopyW, kCopyH, TexelBytes(format));
want = WithBlock(want, m_width, patch, 3, 0, 0, 0, 0, 3, 2, TexelBytes(format));
ExpectImage(ReadTexImage(destination, format), want, format, "the copy destination after the upload");
ASSERT_EQ(FirstGLError(), 0u) << format.name << ": glGetTexImage raised a GL error";
}
ExpectStoreReadNamedSince(mark);
}
// A RENDERBUFFER endpoint. The split arm resolves it by handle (DirectGLES.cpp,
// MakeGLESCopyImageEndpoint), which is what retired the "CopyImageSubData+RENDERBUFFER"
// latch; this is the case that says so on every arm. It has no store to answer from - a
// renderbuffer's texels were never uploaded - so the knob entries do not run it.
TEST_F(CopyImageStoreReadbackScenario, ARenderbufferSourceLandsInTheTexture) {
if (!Ready()) return;
const CopyFormat format{GL_RGBA16, GL_RGBA, GL_UNSIGNED_SHORT, 4, false, "rgba16"};
const auto destinationTexels = MakeTexels(format, m_width, m_height, 71);
const GLuint destination = MakeTexture(format, destinationTexels, /*immutable=*/false);
glGenRenderbuffers(1, &m_renderbuffer);
glBindRenderbuffer(GL_RENDERBUFFER, m_renderbuffer);
glRenderbufferStorage(GL_RENDERBUFFER, GL_RGBA16, m_width, m_height);
glBindRenderbuffer(GL_RENDERBUFFER, 0);
glGenFramebuffers(1, &m_fbo);
glBindFramebuffer(GL_FRAMEBUFFER, m_fbo);
glFramebufferRenderbuffer(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_RENDERBUFFER, m_renderbuffer);
ASSERT_EQ(glCheckFramebufferStatus(GL_FRAMEBUFFER), static_cast<GLenum>(GL_FRAMEBUFFER_COMPLETE))
<< "the RGBA16 renderbuffer is not attachable";
// k / 65535 for four 16-bit words: exact through a float clear and a UNORM16 store.
const std::uint16_t words[4] = {0x1234, 0x5678, 0x9abc, 0xffff};
const GLfloat color[4] = {words[0] / 65535.0f, words[1] / 65535.0f, words[2] / 65535.0f,
words[3] / 65535.0f};
glDisable(GL_SCISSOR_TEST);
glClearBufferfv(GL_COLOR, 0, color);
BindDefaultFramebuffer();
ASSERT_EQ(FirstGLError(), 0u) << "setup raised a GL error";
glCopyImageSubData(m_renderbuffer, GL_RENDERBUFFER, 0, kSrcX, kSrcY, 0, destination, GL_TEXTURE_2D, 0,
kDstX, kDstY, 0, kCopyW, kCopyH, 1);
ASSERT_EQ(FirstGLError(), 0u) << "glCopyImageSubData from the renderbuffer raised a GL error";
std::vector<std::uint8_t> cleared(static_cast<std::size_t>(m_width) * m_height * 8);
for (std::size_t texel = 0; texel < cleared.size() / 8; ++texel) {
std::memcpy(cleared.data() + texel * 8, words, 8);
}
const auto want = WithBlock(destinationTexels, m_width, cleared, m_width, kSrcX, kSrcY, kDstX, kDstY,
kCopyW, kCopyH, 8);
ExpectImage(ReadTexImage(destination, format), want, format, "the texture a renderbuffer was copied into");
ASSERT_EQ(FirstGLError(), 0u) << "glGetTexImage raised a GL error";
}
} // namespace
} // namespace MGITest
@@ -257,7 +257,7 @@ namespace {
// RESIDENT, residency comes only from AdoptPersistentMap, and every persistent-map
// acquisition declines under split by R-6 (PipeApply.cpp's MGPipeApplyMapPersistent:
// "A SPLIT BUILD RUNS AT TIER T2 AND DECLINES EVERY ACQUISITION, ALWAYS"; T0/T1 are
// P11's and AdoptTierIsEmulate is a named Fatal for them). So the capability is wired
// unimplemented, refused by name at the handshake since P11 A1). So the capability is wired
// AHEAD of its consumer on purpose, and `rsd=` on the PipeStats line is the counter that
// will show the records the day P11 lands a tier that adopts. See notes/p7/magma-c.md.
TEST_F(CtWireScenario, TheServerPublishesTheResidentSubDataCapabilityFromItsOwnTable) {
@@ -0,0 +1,369 @@
// MobileGL - MobileGL/MG_IntegrationTest/Scenarios/DeletedBoundBufferScenario.cpp
// Copyright (c) 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
//
// Scenario - A BUFFER DELETED WHILE IT IS STILL BOUND TO AN INDEXED BINDING POINT.
//
// glDeleteBuffers unbinds the buffer from every binding point of the current context (GL 4.6
// core 5.1.2, ES 3.2 6.1.1), indexed ones included, and the frontend does (BufferState::
// MarkBufferObjectForDeletion). Under a transport that is not enough: the SERVER holds its own
// copy of each indexed window (set_shader_buffers / the uniform window), re-sent only when the
// target's bind-point GENERATION moves (Tracker.h's bits 15/16/17). A delete that cleared the
// points without moving the generation left the server naming the dead buffer's handle; once
// the client recycled that handle's slot at a newer generation, the next draw or dispatch that
// walked the window asked the backend for the old one:
// Espryt Fatal{ProtocolCorruption, "BackendSlotTable.Generation"} (the whole session dies);
// Magma the handle resolves to nothing and the draw / dispatch that declares the point is
// dropped.
// (P11 B2 addendum; first seen as LargeArenaAdoptionScenario.GpuWriteIntoTheArenaIsReadBack
// followed by TheArenaLandsInTheTierItsLaneDeclares in one process.)
//
// Each case, for one indexed target: bind a buffer at point 0 and use it (so the window naming it
// reaches the server), delete it WHILE BOUND, create buffers until its slot has been reused at a
// newer generation, then draw and dispatch with a program that DECLARES point 0 but never
// touches it (a uniform-guarded branch: reading an empty point would be undefined, declaring it
// is not). Both must run - no Fatal, a marker written, the frame drawn - and a fresh buffer bound
// at the same point afterwards must be read correctly.
#include <array>
#include <initializer_list>
#include <sstream>
#include <string>
#include <vector>
#include "../Harness/HeadlessGL.h"
#include "../Harness/ScenarioFixture.h"
#ifdef GLAPI
#undef GLAPI
#endif
#define GL_GLEXT_PROTOTYPES
#include <GL/gl.h>
#include <GL/glcorearb.h>
#undef GL_GLEXT_PROTOTYPES
namespace MGITest {
namespace {
// More than enough for any free-list order: the dead buffer's slot is reused well before.
constexpr int kRecycledBuffers = 16;
constexpr unsigned int kMarker = 0xC0FFEEu;
constexpr unsigned int kFreshWord = 0x1234u;
constexpr GLuint kResultBinding = 7;
constexpr const char* kQuadVertexSource = R"(#version 430 core
layout(location = 0) in vec2 a_pos;
void main() { gl_Position = vec4(a_pos, 0.0, 1.0); }
)";
constexpr const char* kQuadFragmentSource = R"(#version 430 core
out vec4 o_color;
void main() { o_color = vec4(0.0, 1.0, 0.0, 1.0); }
)";
// The target's declaration and the one read of it; the read is only ever taken with
// u_read = 1 (a fresh buffer bound). Result: the marker, unconditional, and what was read.
std::string ComputeSource(const char* declaration, const char* read) {
std::ostringstream os;
os << "#version 430 core\n"
"layout(local_size_x = 1) in;\n"
"layout(std430, binding = 7) buffer Result { uint marker; uint value; };\n"
"uniform uint u_read;\n"
<< declaration << "\n"
<< "void main() {\n"
" uint v = 0u;\n"
" if (u_read != 0u) { v = " << read << "; }\n"
" marker = 0xC0FFEEu;\n"
" value = v;\n"
"}\n";
return os.str();
}
constexpr const char* kCaptureVertexSource = R"(#version 430 core
layout(location = 0) in vec2 a_pos;
out float vs_value;
void main() {
vs_value = float(gl_VertexID) * 2.0 + 1.0;
gl_Position = vec4(a_pos, 0.0, 1.0);
gl_PointSize = 1.0;
}
)";
class DeletedBoundBufferScenario : public ScenarioTest {
protected:
void SetUp() override {
ScenarioTest::SetUp();
if (!Ready()) return;
m_quad = Link(kQuadVertexSource, kQuadFragmentSource);
ASSERT_NE(m_quad, 0u) << m_log;
const float quad[] = {-1.f, -1.f, 1.f, -1.f, 1.f, 1.f, -1.f, -1.f, 1.f, 1.f, -1.f, 1.f};
glGenVertexArrays(1, &m_vao);
glBindVertexArray(m_vao);
glGenBuffers(1, &m_vbo);
glBindBuffer(GL_ARRAY_BUFFER, m_vbo);
glBufferData(GL_ARRAY_BUFFER, sizeof(quad), quad, GL_STATIC_DRAW);
glVertexAttribPointer(0, 2, GL_FLOAT, GL_FALSE, 0, nullptr);
glEnableVertexAttribArray(0);
// The result block lives at its own point, bound ONCE here: nothing later rebinds
// a storage point, so the storage window the server holds is only ever re-sent
// by what the case itself does.
const unsigned int zero[2] = {0u, 0u};
glGenBuffers(1, &m_result);
glBindBuffer(GL_SHADER_STORAGE_BUFFER, m_result);
glBufferData(GL_SHADER_STORAGE_BUFFER, sizeof(zero), zero, GL_DYNAMIC_DRAW);
glBindBufferBase(GL_SHADER_STORAGE_BUFFER, kResultBinding, m_result);
}
void TearDown() override {
if (!Ready()) return;
glUseProgram(0);
glBindVertexArray(0);
if (!m_owned.empty()) glDeleteBuffers(static_cast<GLsizei>(m_owned.size()), m_owned.data());
if (m_result != 0) glDeleteBuffers(1, &m_result);
if (m_vbo != 0) glDeleteBuffers(1, &m_vbo);
if (m_vao != 0) glDeleteVertexArrays(1, &m_vao);
for (GLuint p : {m_quad, m_compute, m_capture}) {
if (p != 0) glDeleteProgram(p);
}
m_owned.clear();
m_result = m_vbo = m_vao = m_quad = m_compute = m_capture = 0;
}
GLuint Compile(GLenum stage, const char* source) {
const GLuint shader = glCreateShader(stage);
glShaderSource(shader, 1, &source, nullptr);
glCompileShader(shader);
GLint ok = 0;
glGetShaderiv(shader, GL_COMPILE_STATUS, &ok);
if (!ok) {
char log[2048] = {};
glGetShaderInfoLog(shader, sizeof(log) - 1, nullptr, log);
m_log = std::string("shader did not compile: ") + log;
glDeleteShader(shader);
return 0;
}
return shader;
}
GLuint LinkShaders(std::initializer_list<GLuint> shaders, const char* varying = nullptr) {
const GLuint program = glCreateProgram();
for (GLuint s : shaders) glAttachShader(program, s);
if (varying != nullptr) glTransformFeedbackVaryings(program, 1, &varying, GL_INTERLEAVED_ATTRIBS);
glLinkProgram(program);
for (GLuint s : shaders) glDeleteShader(s);
GLint ok = 0;
glGetProgramiv(program, GL_LINK_STATUS, &ok);
if (!ok) {
char log[2048] = {};
glGetProgramInfoLog(program, sizeof(log) - 1, nullptr, log);
m_log = std::string("program did not link: ") + log;
glDeleteProgram(program);
return 0;
}
return program;
}
GLuint Link(const char* vs, const char* fs, const char* varying = nullptr) {
const GLuint v = Compile(GL_VERTEX_SHADER, vs);
if (v == 0) return 0;
const GLuint f = Compile(GL_FRAGMENT_SHADER, fs);
if (f == 0) {
glDeleteShader(v);
return 0;
}
return LinkShaders({v, f}, varying);
}
GLuint LinkCompute(const std::string& source) {
const GLuint c = Compile(GL_COMPUTE_SHADER, source.c_str());
return c == 0 ? 0 : LinkShaders({c});
}
GLuint MakeBuffer(GLenum target, const void* data, GLsizeiptr size) {
GLuint buffer = 0;
glGenBuffers(1, &buffer);
glBindBuffer(target, buffer);
glBufferData(target, size, data, GL_DYNAMIC_DRAW);
glBindBuffer(target, 0);
return buffer;
}
// Delete the buffer still bound at `target` point 0, then take the dead buffer's slot
// back at a newer generation: every new buffer is defined with content, which is what
// makes the server twin it (and so move its live generation) at once.
void DeleteWhileBoundAndRecycle(GLuint& bound) {
glDeleteBuffers(1, &bound);
bound = 0;
std::vector<unsigned int> payload(64);
for (int i = 0; i < kRecycledBuffers; ++i) {
for (auto& word : payload) word = static_cast<unsigned int>(i);
m_owned.push_back(MakeBuffer(GL_ARRAY_BUFFER, payload.data(),
static_cast<GLsizeiptr>(payload.size() * sizeof(unsigned int))));
}
}
// The draw path first (Espryt walks the whole storage window at every draw), then the
// frame must show the quad.
void DrawTheQuadAndExpectIt(const char* when) {
glViewport(0, 0, Gl().Width(), Gl().Height());
glClearColor(0.f, 0.f, 0.f, 1.f);
glClear(GL_COLOR_BUFFER_BIT);
glUseProgram(m_quad);
glBindVertexArray(m_vao);
glDrawArrays(GL_TRIANGLES, 0, 6);
unsigned char px[4] = {0, 0, 0, 0};
glReadPixels(Gl().Width() / 2, Gl().Height() / 2, 1, 1, GL_RGBA, GL_UNSIGNED_BYTE, px);
EXPECT_GT(px[1], 200) << "the quad was not drawn " << when;
EXPECT_LT(px[0], 50) << when;
}
// Dispatch the target's program once; returns {marker, value}.
std::array<unsigned int, 2> Dispatch(unsigned int read) {
const unsigned int zero[2] = {0u, 0u};
glBindBuffer(GL_SHADER_STORAGE_BUFFER, m_result);
glBufferSubData(GL_SHADER_STORAGE_BUFFER, 0, sizeof(zero), zero);
glUseProgram(m_compute);
glUniform1ui(glGetUniformLocation(m_compute, "u_read"), read);
glDispatchCompute(1, 1, 1);
glMemoryBarrier(GL_SHADER_STORAGE_BARRIER_BIT | GL_BUFFER_UPDATE_BARRIER_BIT |
GL_ATOMIC_COUNTER_BARRIER_BIT);
std::array<unsigned int, 2> out = {0xDEADBEEFu, 0xDEADBEEFu};
glBindBuffer(GL_SHADER_STORAGE_BUFFER, m_result);
glGetBufferSubData(GL_SHADER_STORAGE_BUFFER, 0, sizeof(out), out.data());
glBindBuffer(GL_SHADER_STORAGE_BUFFER, 0);
return out;
}
// The three shader-buffer targets share one shape.
void RunShaderBufferTarget(GLenum target, const char* declaration, const char* read,
unsigned int freshSeed, unsigned int expectedRead) {
m_compute = LinkCompute(ComputeSource(declaration, read));
ASSERT_NE(m_compute, 0u) << m_log;
// 1. A buffer at point 0, used once so the window naming it crosses.
const unsigned int seed[4] = {freshSeed, 0u, 0u, 0u};
GLuint doomed = MakeBuffer(target, seed, sizeof(seed));
glBindBufferBase(target, 0, doomed);
auto first = Dispatch(0);
EXPECT_EQ(first[0], kMarker) << "the dispatch with the buffer bound did not run";
ASSERT_EQ(FirstGLError(), 0u);
// 2. Deleted while bound; its slot recycled at a newer generation.
DeleteWhileBoundAndRecycle(doomed);
GLint stillBound = -1;
glGetIntegeri_v(target == GL_SHADER_STORAGE_BUFFER ? GL_SHADER_STORAGE_BUFFER_BINDING
: target == GL_UNIFORM_BUFFER ? GL_UNIFORM_BUFFER_BINDING
: GL_ATOMIC_COUNTER_BUFFER_BINDING,
0, &stillBound);
EXPECT_EQ(stillBound, 0) << "glDeleteBuffers did not unbind indexed point 0";
// 3. The SAME program dispatched again - no glUseProgram of another program in
// between, because the uniform window's shutter mixes the program identity in and a
// program change would re-send the window anyway - then a draw. Both must run.
auto after = Dispatch(0);
EXPECT_EQ(after[0], kMarker)
<< "the dispatch that declares the deleted buffer's point did not run: its window still named "
"the dead handle (Magma drops the dispatch; Espryt would have died with "
"Fatal{ProtocolCorruption, \"BackendSlotTable.Generation\"} before this line)";
DrawTheQuadAndExpectIt("after the bound buffer was deleted and its slot reused");
EXPECT_EQ(FirstGLError(), 0u);
// 4. A fresh buffer at the same point reads correctly.
const unsigned int fresh[4] = {freshSeed, 0u, 0u, 0u};
const GLuint replacement = MakeBuffer(target, fresh, sizeof(fresh));
m_owned.push_back(replacement);
glBindBufferBase(target, 0, replacement);
auto read1 = Dispatch(1);
EXPECT_EQ(read1[0], kMarker);
EXPECT_EQ(read1[1], expectedRead) << "the buffer bound after the delete was not the one read";
EXPECT_EQ(FirstGLError(), 0u);
glBindBufferBase(target, 0, 0);
}
GLuint m_quad = 0;
GLuint m_compute = 0;
GLuint m_capture = 0;
GLuint m_vao = 0;
GLuint m_vbo = 0;
GLuint m_result = 0;
std::vector<GLuint> m_owned;
std::string m_log;
};
} // namespace
TEST_F(DeletedBoundBufferScenario, AStorageBufferDeletedWhileBoundLeavesNoStaleBinding) {
if (!Ready() || IsSkipped()) return;
RunShaderBufferTarget(GL_SHADER_STORAGE_BUFFER,
"layout(std430, binding = 0) buffer Target { uint word; };", "word",
kFreshWord, kFreshWord);
}
TEST_F(DeletedBoundBufferScenario, AUniformBufferDeletedWhileBoundLeavesNoStaleBinding) {
if (!Ready() || IsSkipped()) return;
RunShaderBufferTarget(GL_UNIFORM_BUFFER, "layout(std140, binding = 0) uniform Target { uint word; };",
"word", kFreshWord, kFreshWord);
}
// The counter's pre-increment value is what the shader reads, and the buffer then holds seed + 1.
TEST_F(DeletedBoundBufferScenario, AnAtomicCounterBufferDeletedWhileBoundLeavesNoStaleBinding) {
if (!Ready() || IsSkipped()) return;
GLint counters = 0;
glGetIntegerv(GL_MAX_COMPUTE_ATOMIC_COUNTERS, &counters);
if (counters < 1) GTEST_SKIP() << "GL_MAX_COMPUTE_ATOMIC_COUNTERS is " << counters;
RunShaderBufferTarget(GL_ATOMIC_COUNTER_BUFFER, "layout(binding = 0, offset = 0) uniform atomic_uint counter;",
"atomicCounterIncrement(counter)", 41u, 41u);
}
// Transform feedback: nothing re-sends a capture window at a draw today (bit 17 is computed and
// not emitted), so the stale-handle failure cannot reach the backend through this target; the
// case pins the behaviour anyway - the delete unbinds, a draw after the slot is reused runs,
// and a fresh capture buffer at the same point receives exactly the captured values.
TEST_F(DeletedBoundBufferScenario, ATransformFeedbackBufferDeletedWhileBoundLeavesNoStaleBinding) {
if (!Ready() || IsSkipped()) return;
m_capture = Link(kCaptureVertexSource, kQuadFragmentSource, "vs_value");
ASSERT_NE(m_capture, 0u) << m_log;
constexpr int kPoints = 4;
const float poison[kPoints + 2] = {-7.f, -7.f, -7.f, -7.f, -7.f, -7.f};
auto capture = [&](GLuint into) {
glBindBufferBase(GL_TRANSFORM_FEEDBACK_BUFFER, 0, into);
glUseProgram(m_capture);
glBindVertexArray(m_vao);
glEnable(GL_RASTERIZER_DISCARD);
glBeginTransformFeedback(GL_POINTS);
glDrawArrays(GL_POINTS, 0, kPoints);
glEndTransformFeedback();
glDisable(GL_RASTERIZER_DISCARD);
};
GLuint doomed = MakeBuffer(GL_TRANSFORM_FEEDBACK_BUFFER, poison, sizeof(poison));
capture(doomed);
ASSERT_EQ(FirstGLError(), 0u);
DeleteWhileBoundAndRecycle(doomed);
GLint stillBound = -1;
glGetIntegeri_v(GL_TRANSFORM_FEEDBACK_BUFFER_BINDING, 0, &stillBound);
EXPECT_EQ(stillBound, 0) << "glDeleteBuffers did not unbind transform feedback point 0";
DrawTheQuadAndExpectIt("after the bound capture buffer was deleted and its slot reused");
EXPECT_EQ(FirstGLError(), 0u);
const GLuint fresh = MakeBuffer(GL_TRANSFORM_FEEDBACK_BUFFER, poison, sizeof(poison));
m_owned.push_back(fresh);
capture(fresh);
glBindBufferBase(GL_TRANSFORM_FEEDBACK_BUFFER, 0, 0);
float captured[kPoints + 2] = {};
glBindBuffer(GL_TRANSFORM_FEEDBACK_BUFFER, fresh);
glGetBufferSubData(GL_TRANSFORM_FEEDBACK_BUFFER, 0, sizeof(captured), captured);
glBindBuffer(GL_TRANSFORM_FEEDBACK_BUFFER, 0);
EXPECT_EQ(FirstGLError(), 0u);
for (int i = 0; i < kPoints + 2; ++i) {
const float expected = i < kPoints ? static_cast<float>(i) * 2.f + 1.f : -7.f;
EXPECT_EQ(captured[i], expected) << "capture record " << i;
}
}
} // namespace MGITest
@@ -0,0 +1,378 @@
// MobileGL - MobileGL/MG_IntegrationTest/Scenarios/DescriptorPoolGrowthScenario.cpp
// 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
//
// Scenario - ONE FRAME THAT NEEDS THOUSANDS OF DISTINCT DESCRIPTOR SETS.
//
// DirectVulkan takes a draw's descriptor set from a per-frame-slot cache and adds a pool to the
// slot when the cache runs dry (UniformManager). Two things went wrong with a burst like
// Minecraft rd12's loading frame (15,016 sets in one slot):
// - Growth was linear. The next pool was sized at twice the slot's FIRST pool of the flavour,
// which is always its oldest and smallest, so every grown pool came out the same size: the
// loading slot ended with 59 pools of 256 sets.
// - The burst was kept forever. The cache only rewinds, so the slot held all 15k sets (and the
// pools behind them) for the life of the process, while steady frames used a few dozen.
// Now each pool doubles the slot's largest one (capped), and a slot whose pools stay under a
// quarter full for MOBILEGL_MAGMA_DESCRIPTOR_TRIM_FRAMES frames is handed back whole and re-grows.
//
// Each of kDraws draws paints its own pixel of a 64x64 target and samples one of kTextures
// textures, a different one from each of the previous draws' so neither the renderer's per-draw
// descriptor reuse memo (four entries) nor its dynamic-offset-only rebind can let two draws share
// a set: one frame needs kDraws sets. Every pixel must hold its own draw's texture colour, on both
// backends. On DirectVulkan the cases also read the pools through GetDescriptorPoolCensus - which
// only the desktop build can reach, since the integration harness links the static library there
// and the hidden-visibility shipping library on Android.
#include <algorithm>
#include <cstdint>
#include <cstdlib>
#include <sstream>
#include <string>
#include <vector>
#include "../Harness/HeadlessGL.h"
#include "../Harness/ScenarioFixture.h"
#include "../Harness/SplitRuntimePeek.h"
#if !defined(__ANDROID__)
#include <MG_Backend/DirectVulkan/DescriptorPoolCensus.h>
#endif
#ifdef GLAPI
#undef GLAPI
#endif
#define GL_GLEXT_PROTOTYPES
#include <GL/gl.h>
#include <GL/glcorearb.h>
#undef GL_GLEXT_PROTOTYPES
namespace MGITest {
namespace {
constexpr int kSide = 64;
constexpr int kDraws = kSide * kSide; // well under the 16384-draw command-buffer split budget
// More than the descriptor reuse memo's four entries, so the texture a draw samples is never
// one of the last four draws'.
constexpr int kTextures = 8;
// The base pool size (VulkanRenderer's kDescriptorSetsPerFrame) and the growth cap
// (UniformManager::kMaxDescriptorPoolSets).
constexpr std::uint64_t kBaseSets = 64;
constexpr std::uint64_t kMaxPoolSets = 4096;
constexpr const char* kVS = R"(#version 330 core
in vec2 aPos;
uniform int uIndex;
void main() {
vec2 cell = vec2(float(uIndex % 64), float(uIndex / 64));
gl_Position = vec4((cell + aPos) / 32.0 - 1.0, 0.0, 1.0);
}
)";
constexpr const char* kFS = R"(#version 330 core
uniform sampler2D uTex;
out vec4 o_color;
void main() { o_color = texture(uTex, vec2(0.5)); }
)";
// Texture t's single texel; every channel differs between any two textures.
Rgba8 TextureColor(int t) {
return Rgba8{static_cast<std::uint8_t>(16 + 32 * t), static_cast<std::uint8_t>(240 - 32 * t),
static_cast<std::uint8_t>(8 + 29 * t), 255};
}
std::string Describe(const Rgba8& c) {
std::ostringstream os;
os << "rgba(" << static_cast<int>(c.r) << ", " << static_cast<int>(c.g) << ", " << static_cast<int>(c.b)
<< ", " << static_cast<int>(c.a) << ")";
return os.str();
}
unsigned long EnvUnsigned(const char* name, unsigned long fallback) {
const char* value = std::getenv(name);
if (value == nullptr || *value == '\0') return fallback;
char* end = nullptr;
const unsigned long parsed = std::strtoul(value, &end, 10);
return (end != nullptr && *end == '\0') ? parsed : fallback;
}
// P11 M2: HOW MANY OF THE BURST'S SETS ONE FRAME SLOT HOLDS AT ONCE. All kDraws of them on
// the monolith arm. Under a split transport the server draws through the WIRE arm, which
// rewinds a slot's set cursors every UniformManager::kWireDescriptorSetBudget (2048) sets once
// it has proved the GPU done with them (VulkanRenderer::RewindWireDescriptorSetsIfDue), so its
// cache holds the burst 2048 sets at a time: the burst still needs a set per draw, grows the
// slot geometrically to that and is given back by the same trim, only the census counts the
// sets held rather than the draws. (The census itself is in-process - inproc it is read on the
// server's apply thread; spawn and tcp answer unavailable - see GetDescriptorPoolCensus.)
constexpr std::uint64_t kWireDescriptorSetBudget = 2048;
std::uint64_t BurstSetsHeldAtOnce() {
const std::uint64_t draws = static_cast<std::uint64_t>(kDraws);
return PeekSplitRuntime().transportResolved ? std::min(draws, kWireDescriptorSetBudget) : draws;
}
// Pools a geometric policy adds for n sets from an empty flavour: 64, 128, ... up to the cap,
// then one per cap. The old first-pool rule needed n/128 or n/256 of them.
std::uint64_t GeometricPoolBound(std::uint64_t n) {
std::uint64_t pools = 0, capacity = 0, size = kBaseSets;
while (capacity < n) {
capacity += size;
++pools;
size = std::min(size * 2, kMaxPoolSets);
}
return pools;
}
struct Census {
bool available = false;
std::uint64_t pools = 0;
std::uint64_t capacitySets = 0;
std::uint64_t cachedSets = 0;
std::uint64_t slotTrims = 0;
};
Census ReadCensus() {
Census out;
#if !defined(__ANDROID__)
// P11 M2: THE POOLS ARE THE SERVER RENDERER'S under a split transport, and under
// run-ahead a swap - or a draw - returns here before the server has applied it: a census
// taken right after the burst's swap read the pools as they were before the burst
// (cachedSets=0, measured under load). So first the harness's applied-seq fence (the
// TriangleScenario / MagmaWireReclaimScenario shape): every record this client emitted
// is applied. Inproc only - the census is in-process, spawn and tcp answer unavailable
// - and the read itself then runs on the apply thread (GetDescriptorPoolCensus).
// The fence's default 5 s is not enough here: the burst is 4096 draws with a queue-idle
// rewind in the middle, and on a loaded host lavapipe applies it in more than that
// (measured: 21 of 40 runs past 5 s beside the retrace lane).
if (const SplitRuntimeState runtime = PeekSplitRuntime();
runtime.transportResolved && runtime.transportName == "inproc") {
EXPECT_TRUE(WaitForSplitAppliedForTesting(runtime.emitSeq, 120000))
<< "the server never applied record " << runtime.emitSeq << " before the census";
}
const auto census = MobileGL::MG_Backend::DirectVulkan::GetDescriptorPoolCensus();
out.available = census.available;
out.pools = census.pools;
out.capacitySets = census.capacitySets;
out.cachedSets = census.cachedSets;
out.slotTrims = census.slotTrims;
#endif
return out;
}
std::string Describe(const Census& c) {
std::ostringstream os;
os << "pools=" << c.pools << " capacity=" << c.capacitySets << " cachedSets=" << c.cachedSets
<< " slotTrims=" << c.slotTrims;
return os.str();
}
::testing::AssertionResult EveryPixelHoldsItsDrawsTexture(const Image& image, const char* when) {
int wrong = 0;
int firstX = -1, firstY = -1;
Rgba8 firstSeen{};
for (int y = 0; y < kSide; ++y) {
for (int x = 0; x < kSide; ++x) {
const Rgba8 expected = TextureColor((y * kSide + x) % kTextures);
const Rgba8 seen = image.At(x, y);
if (seen != expected) {
if (wrong == 0) {
firstX = x;
firstY = y;
firstSeen = seen;
}
++wrong;
}
}
}
if (wrong == 0) return ::testing::AssertionSuccess();
const int draw = firstY * kSide + firstX;
return ::testing::AssertionFailure()
<< when << ": " << wrong << " of " << kDraws << " pixels do not hold their own draw's texture. "
<< "First: (" << firstX << ", " << firstY << ") = " << Describe(firstSeen) << ", drawn by draw "
<< draw << " with texture " << (draw % kTextures) << " = "
<< Describe(TextureColor(draw % kTextures)) << " (a pixel no draw reached holds 0,0,0,0)";
}
class DescriptorPoolGrowthScenario : public ScenarioTest {
protected:
void SetUp() override {
ScenarioTest::SetUp();
if (!Ready()) return;
static const float kVertices[] = {0.0f, 0.0f, 1.0f, 0.0f, 0.0f, 1.0f, 1.0f, 1.0f};
glGenVertexArrays(1, &m_vao);
glBindVertexArray(m_vao);
glGenBuffers(1, &m_vbo);
glBindBuffer(GL_ARRAY_BUFFER, m_vbo);
glBufferData(GL_ARRAY_BUFFER, sizeof(kVertices), kVertices, GL_STATIC_DRAW);
glEnableVertexAttribArray(0);
glVertexAttribPointer(0, 2, GL_FLOAT, GL_FALSE, 2 * sizeof(float), nullptr);
m_textures.resize(kTextures);
glGenTextures(kTextures, m_textures.data());
for (int t = 0; t < kTextures; ++t) {
const Rgba8 c = TextureColor(t);
const std::uint8_t texel[4] = {c.r, c.g, c.b, c.a};
glBindTexture(GL_TEXTURE_2D, m_textures[t]);
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA8, 1, 1, 0, GL_RGBA, GL_UNSIGNED_BYTE, texel);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
}
glBindTexture(GL_TEXTURE_2D, 0);
}
void TearDown() override {
if (Ready()) {
glUseProgram(0);
glBindVertexArray(0);
glBindTexture(GL_TEXTURE_2D, 0);
BindDefaultFramebuffer();
if (m_program != 0) glDeleteProgram(m_program);
if (!m_textures.empty()) glDeleteTextures(static_cast<GLsizei>(m_textures.size()), m_textures.data());
if (m_vbo != 0) glDeleteBuffers(1, &m_vbo);
if (m_vao != 0) glDeleteVertexArrays(1, &m_vao);
DestroyColorFbo(m_target);
}
ScenarioTest::TearDown();
}
::testing::AssertionResult Prepare() {
std::string error;
m_program = CompileProgram(kVS, kFS, &error);
if (m_program == 0) return ::testing::AssertionFailure() << error;
m_indexLocation = glGetUniformLocation(m_program, "uIndex");
m_target = MakeColorFbo(kSide, kSide);
if (m_target.fbo == 0) return ::testing::AssertionFailure() << "MakeColorFbo failed";
glUseProgram(m_program);
glUniform1i(glGetUniformLocation(m_program, "uTex"), 0);
glActiveTexture(GL_TEXTURE0);
glBindVertexArray(m_vao);
BindFbo(m_target);
glDisable(GL_SCISSOR_TEST);
glDisable(GL_DEPTH_TEST);
glDisable(GL_BLEND);
return ::testing::AssertionSuccess();
}
// Draw i paints pixel (i % 64, i / 64) with texture i % kTextures.
void Draw(int i) {
glBindTexture(GL_TEXTURE_2D, m_textures[i % kTextures]);
glUniform1i(m_indexLocation, i);
glDrawArrays(GL_TRIANGLE_STRIP, 0, 4);
}
// The burst: every pixel, one draw and one descriptor set each.
void DrawBurst() {
ClearTo(0.0f, 0.0f, 0.0f, 0.0f);
for (int i = 0; i < kDraws; ++i) Draw(i);
}
static bool IsVulkan() { return Gl().BackendName() == std::string("DirectVulkan"); }
GLuint m_vao = 0;
GLuint m_vbo = 0;
GLuint m_program = 0;
GLint m_indexLocation = -1;
std::vector<GLuint> m_textures;
ColorFbo m_target;
};
} // namespace
// One frame, kDraws distinct sets. Every pixel lands, and the frame slot grows O(log N) pools
// for it, not N / 128.
TEST_F(DescriptorPoolGrowthScenario, AFrameOfDistinctSetsLandsWholeAndGrowsItsPoolsGeometrically) {
if (!Ready()) return;
ASSERT_TRUE(Prepare());
const Census before = ReadCensus();
DrawBurst();
const Image image = ReadPixels(kSide, kSide);
const Census after = ReadCensus();
EXPECT_TRUE(EveryPixelHoldsItsDrawsTexture(image, "one frame of distinct descriptor sets"));
EXPECT_EQ(FirstGLError(), 0u);
RecordProperty("census_checked", (IsVulkan() && after.available) ? "yes" : "no");
if (!IsVulkan() || !after.available) return;
// Falsifiability first: had the renderer's memos collapsed the draws onto shared sets, the
// slot would never have needed to grow and the bound below would pass on any policy.
const std::uint64_t held = BurstSetsHeldAtOnce();
ASSERT_GE(after.cachedSets, before.cachedSets + held)
<< "the frame did not allocate one descriptor set per draw, so it does not exercise growth. Before: "
<< Describe(before) << "; after: " << Describe(after);
const std::uint64_t added = after.pools - before.pools;
RecordProperty("census_before", Describe(before));
RecordProperty("census_after", Describe(after));
EXPECT_LE(added, GeometricPoolBound(held + 1))
<< "a frame needing " << held << " descriptor sets at once added " << added << " pools; doubling the "
<< "slot's largest pool needs at most " << GeometricPoolBound(held + 1) << ". Before: " << Describe(before)
<< "; after: " << Describe(after);
}
// Burst, then quiet frames until the renderer hands the burst back, then the burst again while
// the quiet frames are still in flight. Nothing waits between frames except where noted, so the
// trim runs with other frame slots' work outstanding.
TEST_F(DescriptorPoolGrowthScenario, AGivenBackBurstLeavesNoStaleSetBehind) {
if (!Ready()) return;
ASSERT_TRUE(Prepare());
const unsigned long trimFrames = EnvUnsigned("MOBILEGL_MAGMA_DESCRIPTOR_TRIM_FRAMES", 120);
const unsigned long framesInFlight = EnvUnsigned("MOBILEGL_MAGMA_FRAMESINFLIGHT", 3);
// The trim fires once trimFrames quiet frames have followed the burst, at the burst slot's next
// turn; past this many frames it is not coming.
const unsigned long quietFrameLimit = trimFrames + 2 * framesInFlight + 16;
DrawBurst();
Gl().EndFrame();
const Census afterBurst = ReadCensus();
// Quiet frames: one draw each, the same pixel and texture the burst gave draw 0.
const bool observe = IsVulkan() && afterBurst.available && trimFrames != 0;
unsigned long quietFrames = 0;
Census afterQuiet = afterBurst;
for (; quietFrames < quietFrameLimit; ++quietFrames) {
Draw(0);
Gl().EndFrame();
if (observe) {
afterQuiet = ReadCensus();
if (afterQuiet.slotTrims > afterBurst.slotTrims) {
++quietFrames;
break;
}
}
}
// The burst again, straight away. The slot it lands in re-grows from its base pool.
DrawBurst();
const Image image = ReadPixels(kSide, kSide);
const Census afterSecondBurst = ReadCensus();
EXPECT_TRUE(EveryPixelHoldsItsDrawsTexture(image, "the burst drawn after the first one was given back"));
EXPECT_EQ(FirstGLError(), 0u);
RecordProperty("census_checked", observe ? "yes" : "no");
if (!observe) return;
RecordProperty("census_after_burst", Describe(afterBurst));
RecordProperty("quiet_frames", std::to_string(quietFrames));
RecordProperty("census_after_quiet", Describe(afterQuiet));
RecordProperty("census_after_second_burst", Describe(afterSecondBurst));
const std::uint64_t held = BurstSetsHeldAtOnce();
ASSERT_GE(afterBurst.cachedSets, held)
<< "the burst did not allocate one descriptor set per draw: " << Describe(afterBurst);
EXPECT_GT(afterQuiet.slotTrims, afterBurst.slotTrims)
<< quietFrames << " frames of one draw each followed a " << kDraws << "-set frame, but no frame slot was "
<< "trimmed (MOBILEGL_MAGMA_DESCRIPTOR_TRIM_FRAMES=" << trimFrames << "). After the burst: "
<< Describe(afterBurst) << "; after the quiet frames: " << Describe(afterQuiet);
EXPECT_LE(afterQuiet.cachedSets + held / 2, afterBurst.cachedSets)
<< "the trim did not give the burst's sets back. After the burst: " << Describe(afterBurst)
<< "; after the quiet frames: " << Describe(afterQuiet);
EXPECT_LT(afterQuiet.pools, afterBurst.pools)
<< "the trim did not give the burst's pools back. After the burst: " << Describe(afterBurst)
<< "; after the quiet frames: " << Describe(afterQuiet);
EXPECT_GE(afterSecondBurst.cachedSets, afterQuiet.cachedSets + held)
<< "the second burst was not served by freshly allocated sets: " << Describe(afterSecondBurst);
}
} // namespace MGITest
@@ -3,6 +3,7 @@
#include "../Harness/SplitRuntimePeek.h"
#include "../Harness/PipeStatsWindow.h"
#include "../Harness/SplitLane.h"
#include "../Harness/WireDeclinePeek.h"
#include <array>
#include <algorithm>
#include <cstring>
@@ -659,6 +660,12 @@ TEST_F(F1WireScenario, GenerateMipmapDepthStencilDeclinesAndKeepsTheSession) {
glDisable(GL_SCISSOR_TEST);
glClearDepth(0.5);
glClear(GL_DEPTH_BUFFER_BIT);
// P8-SV: the decline is a WireDeclines.def row now (MipmapDepthStencilAspect), so the server's
// MGWIRE-DECLINES dump sees it. Readable where the server is this process: the inproc arm.
// Red once (executed, reverted): the site back to a bare MGLOG_E_ONCE - the count stays 0.
unsigned long long declinesBefore = 0;
const bool counted = PeekSplitRuntime().transportName == "inproc" &&
PeekWireDeclineCount("MipmapDepthStencilAspect", &declinesBefore);
glGenerateMipmap(GL_TEXTURE_2D);
EXPECT_EQ(FirstGLError(), GLenum(GL_NO_ERROR)) << "F1.DepthStencilMip.error";
// The session is what the retired Fatal used to take with it. A readback is a round trip
@@ -669,12 +676,77 @@ TEST_F(F1WireScenario, GenerateMipmapDepthStencilDeclinesAndKeepsTheSession) {
glReadPixels(1, 1, 1, 1, GL_DEPTH_COMPONENT, GL_FLOAT, &level0);
EXPECT_EQ(FirstGLError(), GLenum(GL_NO_ERROR)) << "F1.DepthStencilMip.readback";
EXPECT_NEAR(level0, 0.5f, 0.002f) << "F1.DepthStencilMip.level0";
if (counted) {
unsigned long long declinesAfter = 0;
ASSERT_TRUE(PeekWireDeclineCount("MipmapDepthStencilAspect", &declinesAfter));
EXPECT_EQ(declinesAfter - declinesBefore, 1u) << "F1.DepthStencilMip.tally: one named decline per refused "
"glGenerateMipmap (WireDeclines.def MipmapDepthStencilAspect)";
}
glFramebufferTexture2D(GL_FRAMEBUFFER, GL_DEPTH_STENCIL_ATTACHMENT, GL_TEXTURE_2D, 0, 0);
glDeleteTextures(1, &depthStencil);
glBindTexture(GL_TEXTURE_2D, texture);
glClearDepth(1.0);
}
// P8-F: `Magma:mipmap-shader-format-or-shape` IS A NAMED DECLINE ON THE WIRE ARM NOW, which is
// the monolith arm's answer for a chain it cannot build (VulkanRenderer.cpp's GenerateMipmap
// declines a format without blit support and leaves the chain unwritten). It used to end the
// session. A 3D texture is a shape neither wire shader arm takes (the colour pass samples a
// sampler2D), so with the native blit forced off (MGITEST_MAGMA_FORCE_SHADER_MIPMAP) it is what
// reaches the refusal on this host - lavapipe blits a 3D RGBA8 chain natively otherwise.
//
// What this case pins: no GL error, the session still answering (two readback round trips), level
// 0 untouched, and level 1 still holding the application's own texels - the decline wrote nothing.
// Magma only, and knob-gated, like GenerateMipmapWithoutNativeBlitPixels above.
// Red once (executed, reverted): restore the MagmaWireFatal and this case dies with
// Fatal{UnmigratedVerb, "Magma:mipmap-shader-format-or-shape ..."}.
TEST_F(F1WireScenario, GenerateMipmapWithoutAShaderArmShapeDeclinesAndKeepsTheSession) {
if (!Ready()) return;
if (Gl().BackendName() != "DirectVulkan")
GTEST_SKIP() << "Magma wire-arm decline (P8-F, mipmap-shader-format-or-shape): the knob and "
"the shader mip arms it forces are Magma's; Espryt ("
<< Gl().BackendName() << ") generates this chain itself";
const std::string tier = SplitLane::MarkerValue("MGITEST_MAGMA_FORCE_SHADER_MIPMAP");
if (tier.empty())
GTEST_SKIP() << "MGITEST_MAGMA_FORCE_SHADER_MIPMAP is unset: this driver blits a 3D RGBA8 "
"chain natively, so the decline under test is not entered";
GLuint volume = 0;
glGenTextures(1, &volume);
glBindTexture(GL_TEXTURE_3D, volume);
glTexStorage3D(GL_TEXTURE_3D, 3, GL_RGBA8, 4, 4, 4);
const std::array<GLubyte, 4> base{200, 40, 80, 255};
const std::array<GLubyte, 4> sentinel{10, 220, 30, 255};
const auto fill = [](int side, const std::array<GLubyte, 4>& texel) {
std::vector<GLubyte> pixels(static_cast<size_t>(side * side * side * 4));
for (size_t at = 0; at < pixels.size(); at += 4) std::copy(texel.begin(), texel.end(), pixels.begin() + at);
return pixels;
};
const auto level0 = fill(4, base);
const auto level1 = fill(2, sentinel);
glTexSubImage3D(GL_TEXTURE_3D, 0, 0, 0, 0, 4, 4, 4, GL_RGBA, GL_UNSIGNED_BYTE, level0.data());
glTexSubImage3D(GL_TEXTURE_3D, 1, 0, 0, 0, 2, 2, 2, GL_RGBA, GL_UNSIGNED_BYTE, level1.data());
ASSERT_EQ(FirstGLError(), GLenum(GL_NO_ERROR)) << "F1.ShaderMipDecline.setup";
const auto before = PeekSplitRuntime().emitSeq;
glGenerateMipmap(GL_TEXTURE_3D);
ASSERT_GT(PeekSplitRuntime().emitSeq, before) << "F1.ShaderMipDecline.wire";
EXPECT_EQ(FirstGLError(), GLenum(GL_NO_ERROR)) << "F1.ShaderMipDecline.error";
for (const int level : {0, 1}) {
const int side = 4 >> level;
std::vector<GLubyte> pixels(static_cast<size_t>(side * side * side * 4), 0x5a);
glGetTexImage(GL_TEXTURE_3D, level, GL_RGBA, GL_UNSIGNED_BYTE, pixels.data());
ASSERT_EQ(FirstGLError(), GLenum(GL_NO_ERROR)) << "F1.ShaderMipDecline.readback level " << level;
const std::array<GLubyte, 4>& expected = level == 0 ? base : sentinel;
size_t wrong = 0;
for (size_t at = 0; at < pixels.size(); at += 4)
if (!std::equal(expected.begin(), expected.end(), pixels.begin() + at)) ++wrong;
EXPECT_EQ(wrong, 0u) << "F1.ShaderMipDecline.level" << level << " tier=" << tier
<< ": a declined mip must leave every level as the application wrote it";
}
glBindTexture(GL_TEXTURE_3D, 0);
glDeleteTextures(1, &volume);
glBindTexture(GL_TEXTURE_2D, texture);
}
TEST_F(F1WireScenario, GenerateMipmapHonorsMutableBaseAndMaxWithoutChangingOtherLevels) {
if (!Ready()) return;
const std::array<GLubyte, 4> colors[5] = {
@@ -0,0 +1,199 @@
// MobileGL - MobileGL/MG_IntegrationTest/Scenarios/FencePollScenario.cpp
// 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
//
// P10 (A, MG_Remote/CONTRACT-P10.md §1): FENCE POLLS ANSWERED FROM THE REVERSE CHANNEL.
//
// A glClientWaitSync / glGetSynciv(GL_SYNC_STATUS) used to be a kWaitReply record: its answer
// came back only after the server had applied every record before it, so a poll was a full
// pipeline drain and, over TCP, a round trip - once per frame in Minecraft's fence use. Now the
// server reports each fence the GPU finishes (kEventFenceSignaled) and the client answers polls
// from those reports; the Nth unanswered zero-timeout poll in a row (MOBILEGL_IPC_POLL_ESCALATE)
// still crosses, which is what bounds a loop whose report is late.
//
// Three cases. The first is ordinary GL - the idiom's two poll loops end - and its flush-bit arm
// runs on the monolith lanes too. The other two are about the wire and skip where there is none.
#include <chrono>
#include <cstdio>
#include <cstdlib>
#include <deque>
#include <string>
#include <thread>
#include "../Harness/ScenarioFixture.h"
#include "../Harness/SplitRuntimePeek.h"
#ifdef GLAPI
#undef GLAPI
#endif
#define GL_GLEXT_PROTOTYPES
#include <GL/gl.h>
#include <GL/glcorearb.h>
#undef GL_GLEXT_PROTOTYPES
namespace MGITest {
namespace {
using Clock = std::chrono::steady_clock;
constexpr auto kLoopBound = std::chrono::seconds(10);
class FencePollScenario : public ScenarioTest {
protected:
// Some GPU work for a fence to wait behind.
static void QueueWork() {
glClearColor(0.25f, 0.5f, 0.75f, 1.0f);
glClear(GL_COLOR_BUFFER_BIT);
}
};
// The GL idiom: `while (glClientWaitSync(s, flags, 0) == GL_TIMEOUT_EXPIRED) {}` and the
// glGetSynciv twin. With nothing else in the loop, no record would ever be published if
// the poll were not a doorbell point, and a local "not yet" would be repeated for ever if
// nothing made it true - so the loop ending, bounded, is the property.
//
// WITHOUT GL_SYNC_FLUSH_COMMANDS_BIT GL itself does not promise the loop ends (the fence
// may sit unflushed in the driver), so that arm is split-only: there the transport
// promises it, because the poll flushes the link and an idle server submits.
TEST_F(FencePollScenario, ZeroTimeoutPollLoopsEndInBoundedTime) {
if (!Ready()) return;
const bool split = SplitRuntimeSkipReason().empty();
for (const GLbitfield flags : {GLbitfield(GL_SYNC_FLUSH_COMMANDS_BIT), GLbitfield(0)}) {
if (flags == 0 && !split) continue;
QueueWork();
GLsync sync = glFenceSync(GL_SYNC_GPU_COMMANDS_COMPLETE, 0);
ASSERT_NE(sync, nullptr);
const auto start = Clock::now();
GLenum status = GL_TIMEOUT_EXPIRED;
unsigned long long polls = 0;
while ((status = glClientWaitSync(sync, flags, 0)) == GL_TIMEOUT_EXPIRED &&
Clock::now() - start < kLoopBound) {
++polls;
}
EXPECT_TRUE(status == GL_ALREADY_SIGNALED || status == GL_CONDITION_SATISFIED)
<< "glClientWaitSync(flags 0x" << std::hex << flags << std::dec << ", 0) still "
<< status << " after " << polls << " polls";
glDeleteSync(sync);
}
QueueWork();
GLsync sync = glFenceSync(GL_SYNC_GPU_COMMANDS_COMPLETE, 0);
ASSERT_NE(sync, nullptr);
const auto start = Clock::now();
GLint value = GL_UNSIGNALED;
while (value != GL_SIGNALED && Clock::now() - start < kLoopBound) {
glGetSynciv(sync, GL_SYNC_STATUS, 1, nullptr, &value);
}
EXPECT_EQ(value, GL_SIGNALED) << "glGetSynciv(GL_SYNC_STATUS) never read signaled";
glDeleteSync(sync);
EXPECT_EQ(FirstGLError(), static_cast<GLenum>(GL_NO_ERROR));
}
// THE WIRE HALF: once a fence is known signaled, polling it costs nothing - no reply-slot
// record, no round trip - however often it is asked. MOBILEGL_IPC_POLL_ESCALATE=0 puts the
// round trip back and must fail both counters (its red-once).
TEST_F(FencePollScenario, PollsOfASignaledFenceCrossNothing) {
if (!Ready()) return;
const std::string why = SplitRuntimeSkipReason();
if (!why.empty()) GTEST_SKIP() << why;
QueueWork();
GLsync sync = glFenceSync(GL_SYNC_GPU_COMMANDS_COMPLETE, 0);
ASSERT_NE(sync, nullptr);
const GLenum waited = glClientWaitSync(sync, GL_SYNC_FLUSH_COMMANDS_BIT, 5000000000ull);
ASSERT_TRUE(waited == GL_ALREADY_SIGNALED || waited == GL_CONDITION_SATISFIED) << waited;
constexpr unsigned kPolls = 100;
const SplitRuntimeState before = PeekSplitRuntime();
for (unsigned i = 0; i < kPolls; ++i) {
EXPECT_EQ(glClientWaitSync(sync, 0, 0), static_cast<GLenum>(GL_ALREADY_SIGNALED));
GLint value = GL_UNSIGNALED;
glGetSynciv(sync, GL_SYNC_STATUS, 1, nullptr, &value);
EXPECT_EQ(value, GL_SIGNALED);
}
const SplitRuntimeState after = PeekSplitRuntime();
glDeleteSync(sync);
EXPECT_EQ(after.replyPostings - before.replyPostings, 0u)
<< "polling a signaled fence published reply-slot records (MOBILEGL_IPC_POLL_ESCALATE=0 "
"does this on purpose)";
EXPECT_EQ(after.fenceRoundTrips - before.fenceRoundTrips, 0u);
EXPECT_EQ(after.fenceLocalAnswers - before.fenceLocalAnswers, 2ull * kPolls);
}
// THE SERVER'S HALF: a fence the GPU finishes while the client sends nothing - it only
// polls - is still reported. The apply thread is idle then; it must submit what the driver
// holds and keep re-checking rather than park for ever, or the only way out of the loop is
// the escalation. The polls are paced (10 ms apart) so the escalation cannot come first by
// rate: 64 of them is 640 ms, far more than a clear takes. Red-once: take the idle
// re-check out of ServerLoop and this case escalates.
TEST_F(FencePollScenario, AFenceThatFinishesWhileTheClientOnlyPollsIsReported) {
if (!Ready()) return;
const std::string why = SplitRuntimeSkipReason();
if (!why.empty()) GTEST_SKIP() << why;
QueueWork();
GLsync sync = glFenceSync(GL_SYNC_GPU_COMMANDS_COMPLETE, 0);
ASSERT_NE(sync, nullptr);
const SplitRuntimeState before = PeekSplitRuntime();
const auto start = Clock::now();
GLenum status = GL_TIMEOUT_EXPIRED;
while ((status = glClientWaitSync(sync, 0, 0)) == GL_TIMEOUT_EXPIRED &&
Clock::now() - start < kLoopBound) {
std::this_thread::sleep_for(std::chrono::milliseconds(10));
}
const SplitRuntimeState after = PeekSplitRuntime();
glDeleteSync(sync);
EXPECT_TRUE(status == GL_ALREADY_SIGNALED || status == GL_CONDITION_SATISFIED) << status;
EXPECT_EQ(after.fenceEscalations - before.fenceEscalations, 0u)
<< "the loop needed an escalated round trip: the server did not report the fence on its "
"own while the client only polled";
EXPECT_GE(after.fenceServerReports - before.fenceServerReports, 1u);
}
// P10 A's PAIRED RECORD - a measurement, not a gate: it skips unless
// MGITEST_FENCE_PACE_BENCH=1. Minecraft's shape, isolated (in the replay corpus only
// improved-transparency-minecraft-26.3 carries timed fence waits): every frame queues work
// and a fence, swaps, and waits - with a timeout - on the fence it made two frames earlier.
// The same entry run with MOBILEGL_IPC_POLL_ESCALATE=0 and with the default is the A/B; it
// prints frames, wall time, reply-slot records and the fence counters.
TEST_F(FencePollScenario, PacedFrameFenceWaitsBench) {
if (!Ready()) return;
const char* bench = std::getenv("MGITEST_FENCE_PACE_BENCH");
if (bench == nullptr || std::string(bench) != "1") {
GTEST_SKIP() << "a measurement: set MGITEST_FENCE_PACE_BENCH=1";
}
const std::string why = SplitRuntimeSkipReason();
if (!why.empty()) GTEST_SKIP() << why;
constexpr int kFrames = 600;
// MGITEST_FENCE_PACE_LAG: how many frames back the waited fence is (default 2).
const char* lagEnv = std::getenv("MGITEST_FENCE_PACE_LAG");
const size_t lag = lagEnv != nullptr && std::atoi(lagEnv) > 0 ? static_cast<size_t>(std::atoi(lagEnv)) : 2;
std::deque<GLsync> inFlight;
const SplitRuntimeState before = PeekSplitRuntime();
const auto start = Clock::now();
for (int frame = 0; frame < kFrames; ++frame) {
QueueWork();
inFlight.push_back(glFenceSync(GL_SYNC_GPU_COMMANDS_COMPLETE, 0));
Gl().EndFrame();
if (inFlight.size() > lag) {
GLsync old = inFlight.front();
inFlight.pop_front();
const GLenum waited = glClientWaitSync(old, GL_SYNC_FLUSH_COMMANDS_BIT, 1000000000ull);
EXPECT_TRUE(waited == GL_ALREADY_SIGNALED || waited == GL_CONDITION_SATISFIED) << waited;
glDeleteSync(old);
}
}
const double ms = std::chrono::duration<double, std::milli>(Clock::now() - start).count();
const SplitRuntimeState after = PeekSplitRuntime();
for (GLsync sync : inFlight) glDeleteSync(sync);
const char* escalate = std::getenv("MOBILEGL_IPC_POLL_ESCALATE");
std::printf("P10BENCH transport=%s poll-escalate=%s lag=%zu frames=%d ms=%.1f ms/frame=%.3f "
"reply-records=%llu fence-round-trips=%llu fence-local=%llu fence-reports=%llu\n",
after.transportName.c_str(), escalate != nullptr ? escalate : "(default)", lag, kFrames, ms,
ms / kFrames, after.replyPostings - before.replyPostings,
after.fenceRoundTrips - before.fenceRoundTrips,
after.fenceLocalAnswers - before.fenceLocalAnswers,
after.fenceServerReports - before.fenceServerReports);
}
} // namespace
} // namespace MGITest
@@ -0,0 +1,555 @@
// MobileGL - MobileGL/MG_IntegrationTest/Scenarios/GenerateMipmapServerScenario.cpp
// Copyright (c) 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
//
// Scenario - glGenerateMipmap ON THE FORMATS ESPRYT DOES NOT HAND STRAIGHT TO THE DRIVER
// (P8-B, docs/Disaggregated/notes/p8/B.md).
//
// Espryt generates R11F_G11F_B10F and depth chains with per-level blits, and RGB16F / RGB32F
// chains outside glGenerateMipmap because ES refuses a three-channel float format there. Until
// P8-B the split arms of both were named refusals:
// B1 Fatal{UnmigratedEmulation, "generate-mipmap-storage"} whenever the texture's descriptor
// held fewer levels than the full chain from level 0 - a MAX_LEVEL below the chain, or a
// glTexStorage2D with fewer levels - which is legal GL (the generated window is
// BASE_LEVEL+1 .. min(p, MAX_LEVEL), and immutable storage bounds it too);
// B2 Fatal{UnmigratedEmulation, "generate-mipmap-cpu-filter"} on every RGB16F / RGB32F
// generation, because the monolith filters the frontend's level shadows on the CPU.
//
// EVERY CASE CHECKS THE GL ANSWER BY SAMPLING. The base level is split into two halves (bottom
// rows A, top rows B) so a generation that loses the row order, or reads another level, cannot
// average to the right colour. Each generated level is read back through texelFetch into an RGBA8
// target, and the levels outside the window are checked unchanged (or still undefined).
//
// ARMS. Both backends, monolith and Split / Spawn / Tcp; Magma is the control. The forced-CPU arm
// (MGITEST_ESPRYT_FORCE_CPU_MIPMAP=1, read by the SERVER) runs the three-channel cases through
// Espryt's server-side CPU filter, which a host driver never needs: llvmpipe generates both
// formats natively. Under the knob the server log must name that arm, or the entry proved nothing.
//
// THE CPU ARM'S STORE ROUTE (P8-SE). When the driver cannot read the base level back, the server's
// CPU arm filters the staged store's copy of it - but only for a texture the driver never wrote
// (P8-E's sticky mark), because a render target's level 0 exists only on the driver and the store
// still holds what crossed before the draw. The `CpuMipRefused.` entries add
// MGITEST_ESPRYT_REFUSE_TEXTURE_READBACK_EXTENT=16x16 (P8-E's knob, also read by the SERVER) to the
// forced-CPU knob: a render-target base must decline by name (generate-mipmap-store-declined) and
// never filter the stale store, and an uploaded-only base must still be filtered from the store.
//
// MONOLITH DEFECTS, recorded for dev (ID-P8-3) and skipped by name on the monolith arm they live
// on unless MGITEST_RUN_MONOLITH_MIP_DEFECTS=1 is set (which is how each was shown red):
// M1 Espryt: the RGB16F / RGB32F CPU filter reads the frontend's level shadow, which a GPU
// write to level 0 leaves stale (DirectGLES.cpp GenerateThreeChannelFloatMipmapOnCpu);
// M2 Espryt: the R11F / depth blit chains start at level 1 and run to the end of the chain,
// ignoring BASE_LEVEL and MAX_LEVEL, and the storage grow before them defines levels past
// MAX_LEVEL (DirectGLES.cpp GenerateColorTexture2DMipmap / GenerateDepthTexture2DMipmap /
// EnsureGenerateMipmapStorageAllocated);
// M3 Magma: a colour format without BLIT_SRC|BLIT_DST (R11F_G11F_B10F on lavapipe) is skipped
// with a warning and its levels stay undefined texels - the monolith has no shader arm,
// which the wire arm has had since P7 (VulkanRenderer.cpp GenerateMipmap).
#include <algorithm>
#include <array>
#include <cmath>
#include <cstdlib>
#include <string>
#include <vector>
#include "../Harness/HeadlessGL.h"
#include "../Harness/PipeStatsWindow.h"
#include "../Harness/ScenarioFixture.h"
#include "../Harness/SplitRuntimePeek.h"
#ifdef GLAPI
#undef GLAPI
#endif
#define GL_GLEXT_PROTOTYPES
#include <GL/gl.h>
#include <GL/glcorearb.h>
#undef GL_GLEXT_PROTOTYPES
namespace MGITest {
namespace {
constexpr int kEdge = 16;
constexpr const char* kForceCpuMipmap = "MGITEST_ESPRYT_FORCE_CPU_MIPMAP";
constexpr const char* kCpuMipmapMarker = "generate-mipmap-server-cpu";
constexpr const char* kRunMonolithDefects = "MGITEST_RUN_MONOLITH_MIP_DEFECTS";
// P8-SE: E's server-read knob; the entries set it to kEdge x kEdge, the base level's extent.
constexpr const char* kRefuseReadback = "MGITEST_ESPRYT_REFUSE_TEXTURE_READBACK_EXTENT";
constexpr const char* kStoreDeclinedMarker = "generate-mipmap-store-declined";
constexpr const char* kStoreFilteredMarker = "filtering the staged store's copy";
constexpr const char* kVertexSource = R"(#version 430 core
void main() {
vec2 positions[3] = vec2[3](vec2(-1.0, -1.0), vec2(3.0, -1.0), vec2(-1.0, 3.0));
gl_Position = vec4(positions[gl_VertexID], 0.0, 1.0);
}
)";
constexpr const char* kSampleSource = R"(#version 430 core
uniform sampler2D u_texture;
uniform int u_level;
out vec4 o_color;
void main() { o_color = texelFetch(u_texture, ivec2(gl_FragCoord.xy), u_level); }
)";
constexpr const char* kPaintSource = R"(#version 430 core
uniform vec4 u_bottom;
uniform vec4 u_top;
uniform float u_split;
out vec4 o_color;
void main() { o_color = gl_FragCoord.y < u_split ? u_bottom : u_top; }
)";
using Rgb = std::array<float, 3>;
// Exact in R11F_G11F_B10F, RGB16F and RGB32F alike, and apart in every channel.
constexpr Rgb kBottom = {0.25f, 0.5f, 0.75f};
constexpr Rgb kTop = {0.875f, 0.375f, 0.125f};
constexpr Rgb kSentinel = {1.0f, 0.0f, 1.0f};
constexpr Rgb kLevelZero = {1.0f, 0.0f, 0.0f};
constexpr Rgb kLevelTwo = {0.0f, 0.0f, 1.0f};
constexpr Rgb kLevelThree = {1.0f, 1.0f, 0.0f};
constexpr float kDepthBottom = 0.25f;
constexpr float kDepthTop = 0.75f;
constexpr float kDepthLevelZero = 0.125f;
constexpr float kDepthLevelTwo = 0.5f;
constexpr float kDepthLevelThree = 1.0f;
struct Format {
GLenum internalFormat;
bool depth;
const char* name;
};
constexpr Format kPackedFloat = {GL_R11F_G11F_B10F, false, "R11F_G11F_B10F"};
constexpr Format kDepth32F = {GL_DEPTH_COMPONENT32F, true, "DEPTH_COMPONENT32F"};
constexpr Format kRgb16F = {GL_RGB16F, false, "RGB16F"};
constexpr Format kRgb32F = {GL_RGB32F, false, "RGB32F"};
int ToByte(float value) { return static_cast<int>(std::lround(value * 255.0f)); }
class GenerateMipmapServerScenario : public ScenarioTest {
protected:
void SetUp() override {
ScenarioTest::SetUp();
if (!Ready()) return;
m_wire = SplitRuntimeSkipReason().empty();
m_espryt = Gl().BackendName() == "DirectGLES";
m_forcedCpu = std::getenv(kForceCpuMipmap) != nullptr;
m_refusedReadback = std::getenv(kRefuseReadback) != nullptr;
std::string error;
m_sampleProgram = CompileProgram(kVertexSource, kSampleSource, &error);
ASSERT_NE(m_sampleProgram, 0u) << error;
m_paintProgram = CompileProgram(kVertexSource, kPaintSource, &error);
ASSERT_NE(m_paintProgram, 0u) << error;
m_target = MakeColorFbo(kEdge, kEdge);
ASSERT_NE(m_target.fbo, 0u) << "could not create the sampling target";
glGenVertexArrays(1, &m_vao);
glGenFramebuffers(1, &m_fbo);
glDisable(GL_SCISSOR_TEST);
glDisable(GL_DEPTH_TEST);
glDisable(GL_BLEND);
glColorMask(GL_TRUE, GL_TRUE, GL_TRUE, GL_TRUE);
ASSERT_EQ(FirstGLError(), 0u);
}
void TearDown() override {
if (!Ready()) return;
glUseProgram(0);
glBindVertexArray(0);
glBindTexture(GL_TEXTURE_2D, 0);
if (m_sampleProgram != 0) glDeleteProgram(m_sampleProgram);
if (m_paintProgram != 0) glDeleteProgram(m_paintProgram);
if (m_vao != 0) glDeleteVertexArrays(1, &m_vao);
if (m_fbo != 0) glDeleteFramebuffers(1, &m_fbo);
if (m_texture != 0) glDeleteTextures(1, &m_texture);
BindDefaultFramebuffer();
if (m_target.fbo != 0) DestroyColorFbo(m_target);
glViewport(0, 0, Gl().Width(), Gl().Height());
FirstGLError();
}
// Whether this case meets a recorded monolith defect on this arm (see the header). The
// knob runs it anyway, which is how each defect is shown red and how dev's fix is checked.
bool MonolithDefect(bool onEspryt, bool onMagma) const {
if (m_wire || std::getenv(kRunMonolithDefects) != nullptr) return false;
return m_espryt ? onEspryt : onMagma;
}
static constexpr const char* kM1 =
"monolith defect M1 (recorded for dev, ID-P8-3): Espryt's CPU filter reads the frontend's "
"level shadow, which the draw into level 0 left stale";
static constexpr const char* kM2 =
"monolith defect M2 (recorded for dev, ID-P8-3): Espryt's blit chain ignores BASE_LEVEL and "
"MAX_LEVEL and its storage grow defines levels past MAX_LEVEL";
static constexpr const char* kM3 =
"monolith defect M3 (recorded for dev, ID-P8-3): Magma skips a format without native blit "
"and leaves the generated levels empty";
GLuint NewTexture() {
glGenTextures(1, &m_texture);
glBindTexture(GL_TEXTURE_2D, m_texture);
return m_texture;
}
// A level `edge` x `edge` whose rows below the middle are `bottom` and above it `top`.
static std::vector<float> Halves(const Format& format, int edge, const Rgb& bottom, const Rgb& top,
float depthBottom, float depthTop) {
const int channels = format.depth ? 1 : 3;
std::vector<float> texels(static_cast<std::size_t>(edge * edge * channels));
for (int y = 0; y < edge; ++y) {
for (int x = 0; x < edge; ++x) {
float* texel = texels.data() + static_cast<std::size_t>((y * edge + x) * channels);
const bool low = y < edge / 2;
if (format.depth) {
texel[0] = low ? depthBottom : depthTop;
} else {
for (int c = 0; c < 3; ++c) texel[c] = low ? bottom[c] : top[c];
}
}
}
return texels;
}
static std::vector<float> Flat(const Format& format, int edge, const Rgb& colour, float depth) {
return Halves(format, edge, colour, colour, depth, depth);
}
static void DefineLevel(const Format& format, int level, int edge, const std::vector<float>& texels) {
glTexImage2D(GL_TEXTURE_2D, level, static_cast<GLint>(format.internalFormat), edge, edge, 0,
format.depth ? GL_DEPTH_COMPONENT : GL_RGB, GL_FLOAT, texels.data());
}
static void UploadLevel(const Format& format, int level, int edge, const std::vector<float>& texels) {
glTexSubImage2D(GL_TEXTURE_2D, level, 0, 0, edge, edge, format.depth ? GL_DEPTH_COMPONENT : GL_RGB,
GL_FLOAT, texels.data());
}
static int LevelWidth(int level) {
GLint width = 0;
glGetTexLevelParameteriv(GL_TEXTURE_2D, level, GL_TEXTURE_WIDTH, &width);
return width;
}
// texelFetch's level is relative to BASE_LEVEL and bounded by MAX_LEVEL, so the window is
// reopened over every defined level before any of them is read.
Image SampleLevel(int level, int maxLevel) {
const int edge = std::max(kEdge >> level, 1);
glActiveTexture(GL_TEXTURE0);
glBindTexture(GL_TEXTURE_2D, m_texture);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_BASE_LEVEL, 0);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAX_LEVEL, maxLevel);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST_MIPMAP_NEAREST);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
glUseProgram(m_sampleProgram);
glUniform1i(glGetUniformLocation(m_sampleProgram, "u_texture"), 0);
glUniform1i(glGetUniformLocation(m_sampleProgram, "u_level"), level);
BindFbo(m_target);
glViewport(0, 0, edge, edge);
glBindVertexArray(m_vao);
glDrawArrays(GL_TRIANGLES, 0, 3);
return ReadPixelsRect(0, 0, edge, edge);
}
// Every row of `image` against the half it belongs to. A depth level is read in its red
// channel only, and only in its bottom and top rows: the rows next to the boundary are
// where a shader-based depth mip is entitled to a half-texel offset.
static void ExpectHalves(const Image& image, const Format& format, const Rgb& bottom, const Rgb& top,
float depthBottom, float depthTop, const std::string& what) {
const int height = image.Height();
for (int y = 0; y < height; ++y) {
if (format.depth && y != 0 && y != height - 1) continue;
const bool low = y < height / 2;
for (int x = 0; x < image.Width(); ++x) {
const Rgba8 got = image.At(x, y);
if (format.depth) {
const int want = ToByte(low ? depthBottom : depthTop);
EXPECT_NEAR(got.r, want, 2) << what << " depth at (" << x << "," << y << ") got " << got;
if (std::abs(got.r - want) > 2) return;
continue;
}
const Rgb& colour = low ? bottom : top;
const int want[3] = {ToByte(colour[0]), ToByte(colour[1]), ToByte(colour[2])};
const int have[3] = {got.r, got.g, got.b};
for (int c = 0; c < 3; ++c) {
EXPECT_NEAR(have[c], want[c], 2) << what << " channel " << c << " at (" << x << ","
<< y << ") got " << got;
if (std::abs(have[c] - want[c]) > 2) return;
}
}
}
}
static void ExpectFlat(const Image& image, const Format& format, const Rgb& colour, float depth,
const std::string& what) {
ExpectHalves(image, format, colour, colour, depth, depth, what);
}
void Generate() {
m_mark = PipeStatsWindow::MarkLaneLog();
glBindTexture(GL_TEXTURE_2D, m_texture);
glGenerateMipmap(GL_TEXTURE_2D);
ASSERT_EQ(FirstGLError(), 0u) << "glGenerateMipmap";
}
// Under the knob the SERVER must say it filtered on the CPU; without that line the entry
// ran the native arm and its green says nothing about the arm it is named for.
void ExpectCpuArmNamed() {
if (!m_forcedCpu || !m_wire || !m_espryt) return;
const std::string log = PipeStatsWindow::ReadServerLogSince(m_mark);
EXPECT_NE(log.find(kCpuMipmapMarker), std::string::npos)
<< kForceCpuMipmap << " is set but the server log after glGenerateMipmap carries no '"
<< kCpuMipmapMarker << "' line: the knob did not reach the server, or the three-channel "
<< "generation never took the server's CPU arm";
}
// ---- B1: the generated window, on the two formats Espryt blits level by level ----------
// Levels 0..2 defined, MAX_LEVEL = 2: the descriptor holds three of the chain's five levels.
void MutableMaxLevelTwo(const Format& format) {
if (MonolithDefect(true, !format.depth)) GTEST_SKIP() << (m_espryt ? kM2 : kM3);
NewTexture();
DefineLevel(format, 0, kEdge, Halves(format, kEdge, kBottom, kTop, kDepthBottom, kDepthTop));
DefineLevel(format, 1, kEdge / 2, Flat(format, kEdge / 2, kSentinel, kDepthLevelZero));
DefineLevel(format, 2, kEdge / 4, Flat(format, kEdge / 4, kSentinel, kDepthLevelZero));
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAX_LEVEL, 2);
ASSERT_EQ(FirstGLError(), 0u) << "define levels 0..2";
Generate();
EXPECT_EQ(LevelWidth(3), 0) << format.name << ": level 3 is past MAX_LEVEL and must stay undefined";
for (int level = 1; level <= 2; ++level)
ExpectHalves(SampleLevel(level, 2), format, kBottom, kTop, kDepthBottom, kDepthTop,
std::string(format.name) + " MAX_LEVEL=2 level " + std::to_string(level));
ExpectHalves(SampleLevel(0, 2), format, kBottom, kTop, kDepthBottom, kDepthTop,
std::string(format.name) + " MAX_LEVEL=2 level 0 (the base, unchanged)");
EXPECT_EQ(FirstGLError(), 0u);
}
// glTexStorage2D with three of the chain's five levels.
void PartialStorage(const Format& format) {
if (MonolithDefect(false, !format.depth)) GTEST_SKIP() << kM3;
NewTexture();
glTexStorage2D(GL_TEXTURE_2D, 3, format.internalFormat, kEdge, kEdge);
UploadLevel(format, 0, kEdge, Halves(format, kEdge, kBottom, kTop, kDepthBottom, kDepthTop));
ASSERT_EQ(FirstGLError(), 0u) << "partial storage";
Generate();
for (int level = 1; level <= 2; ++level)
ExpectHalves(SampleLevel(level, 2), format, kBottom, kTop, kDepthBottom, kDepthTop,
std::string(format.name) + " three-level storage level " + std::to_string(level));
EXPECT_EQ(FirstGLError(), 0u);
}
// BASE_LEVEL = 1, MAX_LEVEL = 2 over four defined levels: only level 2 is generated, from
// level 1; levels 0 and 3 keep what they held and level 4 is never defined.
void MutableBaseLevelOne(const Format& format) {
if (MonolithDefect(true, !format.depth)) GTEST_SKIP() << (m_espryt ? kM2 : kM3);
NewTexture();
DefineLevel(format, 0, kEdge, Flat(format, kEdge, kLevelZero, kDepthLevelZero));
DefineLevel(format, 1, kEdge / 2,
Halves(format, kEdge / 2, kBottom, kTop, kDepthBottom, kDepthTop));
DefineLevel(format, 2, kEdge / 4, Flat(format, kEdge / 4, kLevelTwo, kDepthLevelTwo));
DefineLevel(format, 3, kEdge / 8, Flat(format, kEdge / 8, kLevelThree, kDepthLevelThree));
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_BASE_LEVEL, 1);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAX_LEVEL, 2);
ASSERT_EQ(FirstGLError(), 0u) << "define levels 0..3";
Generate();
EXPECT_EQ(LevelWidth(4), 0) << format.name << ": level 4 is past MAX_LEVEL and must stay undefined";
const std::string name(format.name);
ExpectFlat(SampleLevel(0, 3), format, kLevelZero, kDepthLevelZero, name + " level 0 (below BASE_LEVEL)");
ExpectHalves(SampleLevel(1, 3), format, kBottom, kTop, kDepthBottom, kDepthTop, name + " level 1 (base)");
ExpectHalves(SampleLevel(2, 3), format, kBottom, kTop, kDepthBottom, kDepthTop,
name + " level 2 (generated from level 1)");
ExpectFlat(SampleLevel(3, 3), format, kLevelThree, kDepthLevelThree,
name + " level 3 (past MAX_LEVEL)");
EXPECT_EQ(FirstGLError(), 0u);
}
// ---- B2: RGB16F / RGB32F ----------------------------------------------------------------
void UploadedBase(const Format& format) {
NewTexture();
DefineLevel(format, 0, kEdge, Halves(format, kEdge, kBottom, kTop, 0, 0));
ASSERT_EQ(FirstGLError(), 0u) << "define level 0";
Generate();
const int last = 4;
for (int level = 1; level < last; ++level)
ExpectHalves(SampleLevel(level, last), format, kBottom, kTop, 0, 0,
std::string(format.name) + " uploaded base, level " + std::to_string(level));
EXPECT_EQ(LevelWidth(last), 1) << format.name << ": the generated chain ends at 1x1";
ExpectCpuArmNamed();
EXPECT_EQ(FirstGLError(), 0u);
}
// Complementary's colortex2 shape: the base is a render target the GPU wrote after its one
// upload, so the chain must come from the GPU's level 0 and never from the uploaded texels.
void RenderTargetBase(const Format& format) {
if (MonolithDefect(true, false)) GTEST_SKIP() << kM1;
PaintSentinelBaseAsRenderTarget(format);
if (HasFatalFailure() || IsSkipped()) return;
Generate();
const int last = 4;
for (int level = 0; level < last; ++level)
ExpectHalves(SampleLevel(level, last), format, kBottom, kTop, 0, 0,
std::string(format.name) + " render-target base, level " + std::to_string(level));
ExpectCpuArmNamed();
EXPECT_EQ(FirstGLError(), 0u);
}
// ---- P8-SE: the CPU arm's store route under a refused readback ----------------------
// Only the `CpuMipRefused.` entries carry both server knobs; anywhere else the store route
// is unreachable on a host (its driver reads every level back) and the case says so.
void SkipUnlessTheStoreRouteIsArmed() {
if (m_wire && m_espryt && m_forcedCpu && m_refusedReadback) return;
GTEST_SKIP() << "the store route needs Espryt's split server with " << kForceCpuMipmap << " and "
<< kRefuseReadback << "=" << kEdge << "x" << kEdge << " (the CpuMipRefused. entries)";
}
// The render-target base again, with the driver refusing to read level 0 back: the store
// holds the uploaded sentinel, the driver the drawn halves, and P8-E's mark says so. The
// arm must decline by name - a stale chain of sentinel texels is the defect.
void RefusedReadbackOfADrawnBase(const Format& format) {
SkipUnlessTheStoreRouteIsArmed();
if (IsSkipped()) return;
PaintSentinelBaseAsRenderTarget(format);
if (HasFatalFailure() || IsSkipped()) return;
Generate();
const std::string log = PipeStatsWindow::ReadServerLogSince(m_mark);
EXPECT_NE(log.find(kStoreDeclinedMarker), std::string::npos)
<< "the base level was drawn and its readback refused, but the server log after glGenerateMipmap "
"has no '" << kStoreDeclinedMarker << "' line";
EXPECT_EQ(log.find(kStoreFilteredMarker), std::string::npos)
<< "the CPU arm filtered the staged store's copy of a level the driver has since drawn over";
const int last = 4;
ExpectHalves(SampleLevel(0, last), format, kBottom, kTop, 0, 0,
std::string(format.name) + " drawn base (untouched by the decline)");
for (int level = 1; level < last; ++level) {
const Image image = SampleLevel(level, last);
const Rgba8 got = image.At(0, 0);
EXPECT_FALSE(std::abs(got.r - ToByte(kSentinel[0])) <= 2 && std::abs(got.g - ToByte(kSentinel[1])) <= 2 &&
std::abs(got.b - ToByte(kSentinel[2])) <= 2)
<< format.name << " level " << level << " is the uploaded sentinel " << got
<< ": the chain was filtered from the stale staged store";
}
ExpectCpuArmNamed();
FirstGLError();
}
// The control: a base that only ever crossed as an upload IS what the store holds, so a
// refused readback is answered from the store and the chain is right. This is what tells
// the check above from "the store route always declines" - the generation's own
// driver-write note must not count against the level it reads.
void RefusedReadbackOfAnUploadedBase(const Format& format) {
SkipUnlessTheStoreRouteIsArmed();
if (IsSkipped()) return;
NewTexture();
DefineLevel(format, 0, kEdge, Halves(format, kEdge, kBottom, kTop, 0, 0));
ASSERT_EQ(FirstGLError(), 0u) << "define level 0";
Generate();
const std::string log = PipeStatsWindow::ReadServerLogSince(m_mark);
EXPECT_NE(log.find(kStoreFilteredMarker), std::string::npos)
<< "the uploaded base's readback was refused, but the server log has no '" << kStoreFilteredMarker
<< "' line: the store route did not answer";
EXPECT_EQ(log.find(kStoreDeclinedMarker), std::string::npos)
<< "the store route declined a level that only ever crossed as an upload";
const int last = 4;
for (int level = 1; level < last; ++level)
ExpectHalves(SampleLevel(level, last), format, kBottom, kTop, 0, 0,
std::string(format.name) + " uploaded base, store-filtered level " +
std::to_string(level));
EXPECT_EQ(FirstGLError(), 0u);
}
// Level 0 uploaded as the sentinel, then drawn over as a render target (bottom / top halves).
void PaintSentinelBaseAsRenderTarget(const Format& format) {
NewTexture();
DefineLevel(format, 0, kEdge, Flat(format, kEdge, kSentinel, 0));
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
glBindFramebuffer(GL_FRAMEBUFFER, m_fbo);
glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_2D, m_texture, 0);
const GLenum status = glCheckFramebufferStatus(GL_FRAMEBUFFER);
if (status != GL_FRAMEBUFFER_COMPLETE) {
glBindFramebuffer(GL_FRAMEBUFFER, 0);
FirstGLError();
GTEST_SKIP() << format.name << " is not colour-renderable on " << Gl().BackendName()
<< " (status 0x" << std::hex << status << ")";
}
glViewport(0, 0, kEdge, kEdge);
glUseProgram(m_paintProgram);
glUniform4f(glGetUniformLocation(m_paintProgram, "u_bottom"), kBottom[0], kBottom[1], kBottom[2], 1);
glUniform4f(glGetUniformLocation(m_paintProgram, "u_top"), kTop[0], kTop[1], kTop[2], 1);
glUniform1f(glGetUniformLocation(m_paintProgram, "u_split"), kEdge / 2.0f);
glBindVertexArray(m_vao);
glDrawArrays(GL_TRIANGLES, 0, 3);
glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_2D, 0, 0);
glBindFramebuffer(GL_FRAMEBUFFER, 0);
ASSERT_EQ(FirstGLError(), 0u) << "draw into level 0";
}
bool m_wire = false;
bool m_espryt = false;
bool m_forcedCpu = false;
bool m_refusedReadback = false;
GLuint m_sampleProgram = 0;
GLuint m_paintProgram = 0;
GLuint m_vao = 0;
GLuint m_fbo = 0;
GLuint m_texture = 0;
ColorFbo m_target{};
PipeStatsWindow::LogMark m_mark{};
};
} // namespace
// B1.
TEST_F(GenerateMipmapServerScenario, PackedFloatMutableMaxLevelBelowTheChain) {
if (!Ready()) return;
MutableMaxLevelTwo(kPackedFloat);
}
TEST_F(GenerateMipmapServerScenario, PackedFloatStorageWithFewerLevelsThanTheChain) {
if (!Ready()) return;
PartialStorage(kPackedFloat);
}
TEST_F(GenerateMipmapServerScenario, PackedFloatMutableBaseLevelOneKeepsTheLevelsOutsideItsWindow) {
if (!Ready()) return;
MutableBaseLevelOne(kPackedFloat);
}
TEST_F(GenerateMipmapServerScenario, DepthMutableMaxLevelBelowTheChain) {
if (!Ready()) return;
MutableMaxLevelTwo(kDepth32F);
}
TEST_F(GenerateMipmapServerScenario, DepthStorageWithFewerLevelsThanTheChain) {
if (!Ready()) return;
PartialStorage(kDepth32F);
}
TEST_F(GenerateMipmapServerScenario, DepthMutableBaseLevelOneKeepsTheLevelsOutsideItsWindow) {
if (!Ready()) return;
MutableBaseLevelOne(kDepth32F);
}
// B2.
TEST_F(GenerateMipmapServerScenario, Rgb16fUploadedBaseGeneratesItsChain) {
if (!Ready()) return;
UploadedBase(kRgb16F);
}
TEST_F(GenerateMipmapServerScenario, Rgb16fRenderTargetBaseGeneratesFromTheGpuWrittenLevel) {
if (!Ready()) return;
RenderTargetBase(kRgb16F);
}
TEST_F(GenerateMipmapServerScenario, Rgb32fUploadedBaseGeneratesItsChain) {
if (!Ready()) return;
UploadedBase(kRgb32F);
}
TEST_F(GenerateMipmapServerScenario, Rgb32fRenderTargetBaseGeneratesFromTheGpuWrittenLevel) {
if (!Ready()) return;
RenderTargetBase(kRgb32F);
}
// P8-SE: the CPU arm's store route (header; the CpuMipRefused. entries).
TEST_F(GenerateMipmapServerScenario, Rgb16fDrawnBaseWithARefusedReadbackDeclinesByName) {
if (!Ready()) return;
RefusedReadbackOfADrawnBase(kRgb16F);
}
TEST_F(GenerateMipmapServerScenario, Rgb16fUploadedBaseWithARefusedReadbackIsFilteredFromTheStore) {
if (!Ready()) return;
RefusedReadbackOfAnUploadedBase(kRgb16F);
}
} // namespace MGITest
@@ -0,0 +1,800 @@
// MobileGL - MobileGL/MG_IntegrationTest/Scenarios/GpuWrittenDrawInputScenario.cpp
// 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
//
// Scenario - DRAW INPUTS A SHADER WROTE, READ BY A BACKEND ON THE CPU (P8-C).
//
// Espryt emulates five desktop-GL draw features on the CPU, and each one reads a buffer's bytes
// on the side that applies the draw:
//
// * the primitive-restart rewrite (an application restart index that is not the all-ones value
// of the index type): the whole element array buffer, rewritten into a scratch copy;
// * the multi-draw rebase tier, which a batch with such a restart index is forced onto;
// * *IndirectCount: the count word of the parameter buffer (GLES has no counted indirect draw);
// * a native indirect draw's gl_BaseVertex, fed to the shader as a uniform from the command;
// * a GL_DOUBLE vertex array, narrowed to float from the source buffer's bytes.
//
// Under split those bytes are the server's R-11 staged copy, which only uploads fill. A buffer a
// shader WROTE on the server - storage block, atomic counter, buffer image, transform feedback -
// is therefore read stale unless the server refreshes its copy first. Every case below writes
// the buffer on the GPU only (the staged copy holds a poison the correct picture cannot come
// from), so a stale read is a wrong picture, never a lucky one.
//
// MONOLITH ARMS KNOWN BROKEN HERE SKIP BY NAME (SkipBrokenMonolith). Espryt reads three from the
// frontend shadow without SyncGpuWrites: *IndirectCount (OPEN-QUESTIONS 15), an indirect
// gl_BaseVertex (the comment beside it in DirectGLES.cpp says so) and a GL_DOUBLE vertex array
// (the M-3 deviation declared in Managers.cpp). Magma reads MultiDrawArraysIndirectCount's count
// the same way, and its restart rewrite and GL_DOUBLE conversion call SyncGpuWrites while the
// draw is being recorded, so the draw then records into an ended command buffer (SIGSEGV in
// lavapipe). Those arms are dev's to fix (ID-P8-3); the skip keeps the ambient monolith lane
// honest without hiding the split arms, which run the same cases under DirectGLES.Split/Spawn/Tcp,
// DirectVulkan.*.Full and the inproc A/B.
#include <cstdint>
#include <iterator>
#include <string>
#include <vector>
#include "../Harness/HeadlessGL.h"
#include "../Harness/ScenarioFixture.h"
#include "../Harness/SplitRuntimePeek.h"
#ifdef GLAPI
#undef GLAPI
#endif
#define GL_GLEXT_PROTOTYPES
#include <GL/gl.h>
#include <GL/glcorearb.h>
#undef GL_GLEXT_PROTOTYPES
namespace MGITest {
namespace {
constexpr int kSurface = 64;
constexpr const char* kFlatVertex = R"(#version 430 core
layout(location = 0) in vec2 aPos;
void main() { gl_Position = vec4(aPos, 0.0, 1.0); }
)";
constexpr const char* kFlatFragment = R"(#version 430 core
out vec4 oColor;
void main() { oColor = vec4(0.0, 1.0, 0.0, 1.0); }
)";
// Green when the shader saw gl_BaseVertex == 9 (the value the compute shader wrote),
// red for anything else - in particular 3, the value the staged copy still holds.
constexpr const char* kBaseVertexVertex = R"(#version 450 core
#extension GL_ARB_shader_draw_parameters : require
layout(location = 0) in vec2 aPos;
flat out vec4 vColor;
void main() {
vColor = gl_BaseVertexARB == 9 ? vec4(0.0, 1.0, 0.0, 1.0) : vec4(1.0, 0.0, 0.0, 1.0);
gl_Position = vec4(aPos, 0.0, 1.0);
}
)";
constexpr const char* kBaseVertexFragment = R"(#version 450 core
flat in vec4 vColor;
out vec4 oColor;
void main() { oColor = vColor; }
)";
// ---- the restart scene (PrimitiveRestartScenario's geometry) ----
//
// Two triangles with a gap between them, plus two spare vertices at the origin. Index 7 is
// the application's restart index and a legal vertex, so a dropped restart welds the strip
// across the gap (caught by the gap probe) instead of reading out of bounds.
constexpr GLfloat kRestartVertices[] = {
-0.9f, -0.9f, -0.1f, -0.9f, -0.9f, 0.9f, // 0-2: left triangle
0.1f, -0.9f, 0.9f, -0.9f, 0.9f, 0.9f, // 3-5: right triangle
0.0f, 0.0f, 0.0f, 0.0f, // 6, 7: spares
};
constexpr GLuint kRestartIndex = 7;
constexpr GLuint kRestartIndexCount = 7;
// What the compute shader writes: {0, 1, 2, 7, 3, 4, 5}.
constexpr const char* kWriteRestartIndices = R"(#version 430 core
layout(local_size_x = 1) in;
layout(std430, binding = 0) buffer Indices { uint value[]; };
void main() {
value[0] = 0u; value[1] = 1u; value[2] = 2u; value[3] = 7u;
value[4] = 3u; value[5] = 4u; value[6] = 5u;
}
)";
// The staged poison: every index names spare vertex 6, a strip of degenerate triangles
// that paints nothing.
const std::vector<GLuint> kRestartPoison(kRestartIndexCount, 6u);
// The same seven indices, produced by a TRANSFORM FEEDBACK capture: one point per index.
constexpr const char* kCaptureIndicesVertex = R"(#version 430 core
flat out uint idx;
const uint kTable[7] = uint[7](0u, 1u, 2u, 7u, 3u, 4u, 5u);
void main() {
idx = kTable[gl_VertexID];
gl_Position = vec4(0.0, 0.0, 0.0, 1.0);
gl_PointSize = 1.0;
}
)";
// ...and by a capture with a HOLE after every index (gl_SkipComponents1), which ES cannot
// express and Espryt therefore scatters itself. The captured words land at the even
// slots; the odd slots keep what the application staged there.
constexpr const char* kCaptureScatteredVertex = R"(#version 430 core
flat out uint idx;
const uint kTable[4] = uint[4](0u, 2u, 3u, 5u);
void main() {
idx = kTable[gl_VertexID];
gl_Position = vec4(0.0, 0.0, 0.0, 1.0);
gl_PointSize = 1.0;
}
)";
constexpr const char* kCaptureFragment = R"(#version 430 core
out vec4 oColor;
void main() { oColor = vec4(1.0); }
)";
// Even slots are the capture's (poison 6 until it lands), odd slots the holes: after the
// scatter the first seven words are {0, 1, 2, 7, 3, 4, 5}; before it, a strip of
// degenerate triangles.
constexpr GLuint kScatterStaged[8] = {6u, 1u, 6u, 7u, 6u, 4u, 6u, 6u};
// ...and by IMAGE STORES into a buffer texture over the element array buffer.
constexpr const char* kImageStoreRestartIndices = R"(#version 430 core
layout(local_size_x = 1) in;
layout(r32ui, binding = 0) uniform writeonly uimageBuffer indices;
const uint kTable[7] = uint[7](0u, 1u, 2u, 7u, 3u, 4u, 5u);
void main() {
for (int i = 0; i < 7; ++i) imageStore(indices, i, uvec4(kTable[i]));
}
)";
// NDC (-0.5, -0.5), (0.6, -0.5), (0.2, -0.5) on the 64x64 surface.
constexpr int kLeftX = 16, kRightX = 51, kGapX = 38, kProbeY = 16;
// ---- the indirect scene (DrawParametersScenario's geometry) ----
//
// Three pad vertices at the origin, then the left half of the surface as two triangles
// (vertices 3-8), then the right half (9-14). Element indices 0..5 address one half
// through the command's baseVertex.
constexpr int kLeftFirst = 3;
constexpr int kRightFirst = 9;
constexpr int kHalfCount = 6;
std::vector<GLfloat> HalvesVertices() {
std::vector<GLfloat> v(6, 0.0f);
const float bounds[2][2] = {{-1.0f, 0.0f}, {0.0f, 1.0f}};
for (const auto& half : bounds) {
const float x0 = half[0];
const float x1 = half[1];
const float quad[] = {x0, -1.0f, x1, -1.0f, x1, 1.0f, x0, -1.0f, x1, 1.0f, x0, 1.0f};
v.insert(v.end(), std::begin(quad), std::end(quad));
}
return v;
}
struct ElementsCommand {
std::uint32_t count, instanceCount, firstIndex;
std::int32_t baseVertex;
std::uint32_t baseInstance;
};
struct ArraysCommand {
std::uint32_t count, instanceCount, first, baseInstance;
};
bool IsGreen(const Rgba8& p) { return p.g > 128 && p.r < 128; }
class GpuWrittenDrawInputScenario : public ScenarioTest {
protected:
void SetUp() override {
ScenarioTest::SetUp();
if (!Ready()) return;
m_target = MakeColorFbo(kSurface, kSurface);
ASSERT_NE(m_target.fbo, 0u) << "the 64x64 colour target did not build";
glGenVertexArrays(1, &m_vao);
glBindVertexArray(m_vao);
glDisable(GL_DEPTH_TEST);
glDisable(GL_SCISSOR_TEST);
glDisable(GL_CULL_FACE);
DrainErrors();
}
void TearDown() override {
if (!Ready()) return;
glDisable(GL_PRIMITIVE_RESTART);
glPrimitiveRestartIndex(0);
glUseProgram(0);
glBindBufferBase(GL_SHADER_STORAGE_BUFFER, 0, 0);
glBindBufferBase(GL_SHADER_STORAGE_BUFFER, 1, 0);
glBindBuffer(GL_DRAW_INDIRECT_BUFFER, 0);
glBindBuffer(GL_PARAMETER_BUFFER, 0);
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, 0);
glBindBuffer(GL_ARRAY_BUFFER, 0);
glBindVertexArray(0);
glBindImageTexture(0, 0, 0, GL_FALSE, 0, GL_READ_ONLY, GL_R32UI);
if (m_texture != 0) glDeleteTextures(1, &m_texture);
m_texture = 0;
for (GLuint* buffer : {&m_vbo, &m_ebo, &m_commands, &m_parameters}) {
if (*buffer != 0) glDeleteBuffers(1, buffer);
*buffer = 0;
}
if (m_vao != 0) glDeleteVertexArrays(1, &m_vao);
for (GLuint program : m_programs) glDeleteProgram(program);
m_programs.clear();
BindDefaultFramebuffer();
DestroyColorFbo(m_target);
DrainErrors();
}
static void DrainErrors() {
for (int i = 0; i < 16 && glGetError() != GL_NO_ERROR; ++i) {
}
}
// THE ARMS THIS FILE SKIPS, BY NAME: the monolith arm of a backend whose reason is
// non-null. Every split arm, and the inproc A/B, runs the case.
bool SkipBrokenMonolith(const char* espryt, const char* magma) {
if (PeekSplitRuntime().transportName != "monolith") return false;
const std::string backend = Gl().BackendName();
const char* why = backend == "DirectGLES" ? espryt : backend == "DirectVulkan" ? magma : nullptr;
if (why == nullptr) return false;
m_skipReason = "monolith " + backend + ": " + why +
" (dev's fix, ID-P8-3) - this case gates the split arms";
return true;
}
static constexpr const char* kUnsyncedCount =
"the *IndirectCount count is read from the frontend shadow without SyncGpuWrites "
"(OPEN-QUESTIONS 15)";
static constexpr const char* kUnsyncedBaseVertex =
"an indirect command's gl_BaseVertex is read from the frontend shadow without "
"SyncGpuWrites";
static constexpr const char* kUnsyncedDoubles =
"a GL_DOUBLE vertex array is narrowed from the client shadow without SyncGpuWrites "
"(the M-3 deviation declared in Managers.cpp)";
static constexpr const char* kSyncMidRecording =
"SyncGpuWrites inside the draw (the restart rewrite, the GL_DOUBLE vertex conversion) "
"submits the frame mid-recording and the draw then records into the ended command "
"buffer (SIGSEGV)";
GLuint Program(const char* vertex, const char* fragment) {
std::string error;
const GLuint program = CompileProgram(vertex, fragment, &error);
EXPECT_NE(program, 0u) << error;
if (program != 0) m_programs.push_back(program);
return program;
}
GLuint ComputeProgram(const char* source) {
const GLuint shader = glCreateShader(GL_COMPUTE_SHADER);
glShaderSource(shader, 1, &source, nullptr);
glCompileShader(shader);
GLint compiled = GL_FALSE;
glGetShaderiv(shader, GL_COMPILE_STATUS, &compiled);
if (!compiled) {
char log[4096]{};
glGetShaderInfoLog(shader, sizeof(log), nullptr, log);
ADD_FAILURE() << log;
glDeleteShader(shader);
return 0;
}
const GLuint program = glCreateProgram();
glAttachShader(program, shader);
glLinkProgram(program);
glDeleteShader(shader);
GLint linked = GL_FALSE;
glGetProgramiv(program, GL_LINK_STATUS, &linked);
if (!linked) {
char log[4096]{};
glGetProgramInfoLog(program, sizeof(log), nullptr, log);
ADD_FAILURE() << log;
glDeleteProgram(program);
return 0;
}
m_programs.push_back(program);
return program;
}
// Buffer `name` at `target` with `bytes` bytes of `data` (null: an orphaned store).
static void Store(GLuint& name, GLenum target, GLsizeiptr bytes, const void* data) {
if (name == 0) glGenBuffers(1, &name);
glBindBuffer(target, name);
glBufferData(target, bytes, data, GL_DYNAMIC_COPY);
}
void BindVertices(const GLfloat* vertices, GLsizeiptr bytes) {
glBindVertexArray(m_vao);
Store(m_vbo, GL_ARRAY_BUFFER, bytes, vertices);
glEnableVertexAttribArray(0);
glVertexAttribPointer(0, 2, GL_FLOAT, GL_FALSE, 2 * sizeof(GLfloat), nullptr);
}
// One dispatch of `source` with `first` (and `second`, if non-zero) bound as storage
// buffers 0 and 1, then the barrier the draw that consumes them needs.
void WriteWithCompute(const char* source, GLuint first, GLuint second, GLbitfield barrier) {
const GLuint compute = ComputeProgram(source);
ASSERT_NE(compute, 0u);
glBindBufferBase(GL_SHADER_STORAGE_BUFFER, 0, first);
if (second != 0) glBindBufferBase(GL_SHADER_STORAGE_BUFFER, 1, second);
glUseProgram(compute);
glDispatchCompute(1, 1, 1);
glMemoryBarrier(barrier | GL_SHADER_STORAGE_BARRIER_BIT);
glBindBufferBase(GL_SHADER_STORAGE_BUFFER, 0, 0);
glBindBufferBase(GL_SHADER_STORAGE_BUFFER, 1, 0);
glUseProgram(0);
}
template <typename DrawFn>
Image Render(GLuint program, DrawFn&& draw) {
BindFbo(m_target);
ClearTo(0.0f, 0.0f, 0.0f, 1.0f);
glUseProgram(program);
glBindVertexArray(m_vao);
draw();
return ReadPixels(kSurface, kSurface);
}
// The element array buffer of the restart scene, written by a compute shader over the
// poison (or over an orphaned store when `orphaned`).
void BuildRestartScene(bool orphaned) {
BindVertices(kRestartVertices, sizeof(kRestartVertices));
Store(m_ebo, GL_ELEMENT_ARRAY_BUFFER, kRestartIndexCount * sizeof(GLuint),
orphaned ? nullptr : kRestartPoison.data());
WriteWithCompute(kWriteRestartIndices, m_ebo, 0, GL_ELEMENT_ARRAY_BARRIER_BIT);
ArmRestart();
}
// The element array buffer bound to the scene's VAO, restart index 7 on.
void ArmRestart() {
glBindVertexArray(m_vao);
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, m_ebo);
glEnable(GL_PRIMITIVE_RESTART);
glPrimitiveRestartIndex(kRestartIndex);
}
// A vertex + fragment program whose `varyings` are captured INTERLEAVED.
GLuint XfbProgram(const char* vertex, const char* fragment, const char* const* varyings,
GLsizei count) {
const GLuint vs = glCreateShader(GL_VERTEX_SHADER);
glShaderSource(vs, 1, &vertex, nullptr);
glCompileShader(vs);
const GLuint fs = glCreateShader(GL_FRAGMENT_SHADER);
glShaderSource(fs, 1, &fragment, nullptr);
glCompileShader(fs);
const GLuint program = glCreateProgram();
glAttachShader(program, vs);
glAttachShader(program, fs);
glTransformFeedbackVaryings(program, count, varyings, GL_INTERLEAVED_ATTRIBS);
glLinkProgram(program);
glDeleteShader(vs);
glDeleteShader(fs);
GLint linked = GL_FALSE;
glGetProgramiv(program, GL_LINK_STATUS, &linked);
if (!linked) {
char log[4096]{};
glGetProgramInfoLog(program, sizeof(log), nullptr, log);
ADD_FAILURE() << "the capture program did not link: " << log;
glDeleteProgram(program);
return 0;
}
m_programs.push_back(program);
return program;
}
// `vertices` points drawn with rasterization off, captured into m_ebo from byte 0.
void CaptureIntoElementBuffer(GLuint program, GLsizei vertices) {
glBindVertexArray(m_vao);
glUseProgram(program);
glEnable(GL_RASTERIZER_DISCARD);
glBindBufferBase(GL_TRANSFORM_FEEDBACK_BUFFER, 0, m_ebo);
glBeginTransformFeedback(GL_POINTS);
glDrawArrays(GL_POINTS, 0, vertices);
glEndTransformFeedback();
glBindBufferBase(GL_TRANSFORM_FEEDBACK_BUFFER, 0, 0);
glDisable(GL_RASTERIZER_DISCARD);
glUseProgram(0);
}
static void ExpectSplitStrip(const Image& image, const char* what) {
EXPECT_TRUE(IsGreen(image.At(kLeftX, kProbeY)))
<< what << ": the left triangle did not render (" << image.At(kLeftX, kProbeY)
<< ") - the backend drew the indices it had staged, not the ones the compute "
"shader wrote";
EXPECT_TRUE(IsGreen(image.At(kRightX, kProbeY)))
<< what << ": the right triangle did not render (" << image.At(kRightX, kProbeY) << ")";
EXPECT_FALSE(IsGreen(image.At(kGapX, kProbeY)))
<< what << ": the gap is covered, so the restart at index 7 was dropped";
}
// Left and right half of the halves scene, sampled at their centres.
static Rgba8 Half(const Image& image, int half) {
return image.At(kSurface * (1 + 2 * half) / 4, kSurface / 2);
}
ColorFbo m_target;
GLuint m_vao = 0;
GLuint m_vbo = 0;
GLuint m_ebo = 0;
GLuint m_commands = 0;
GLuint m_parameters = 0;
GLuint m_texture = 0;
std::vector<GLuint> m_programs;
std::string m_skipReason;
};
} // namespace
// ---- the primitive-restart rewrite (DirectGLES.cpp ScopedRestartIndexSubstitution) ----
TEST_F(GpuWrittenDrawInputScenario, RestartIndicesAComputeShaderWroteSplitTheStrip) {
if (!Ready()) return;
if (SkipBrokenMonolith(nullptr, kSyncMidRecording)) GTEST_SKIP() << m_skipReason;
const GLuint program = Program(kFlatVertex, kFlatFragment);
ASSERT_NE(program, 0u);
BuildRestartScene(/*orphaned=*/false);
const Image image = Render(program, [] {
glDrawElements(GL_TRIANGLE_STRIP, kRestartIndexCount, GL_UNSIGNED_INT, nullptr);
});
EXPECT_EQ(FirstGLError(), 0u);
ExpectSplitStrip(image, "glDrawElements over compute-written restart indices");
}
// The same indices in a store the application ORPHANED (glBufferData(NULL)): nothing was
// ever staged for it, so the only copy of its bytes is the one the GPU holds.
TEST_F(GpuWrittenDrawInputScenario, RestartIndicesAComputeShaderWroteIntoAnOrphanedStoreSplitTheStrip) {
if (!Ready()) return;
if (SkipBrokenMonolith(nullptr, kSyncMidRecording)) GTEST_SKIP() << m_skipReason;
const GLuint program = Program(kFlatVertex, kFlatFragment);
ASSERT_NE(program, 0u);
BuildRestartScene(/*orphaned=*/true);
const Image image = Render(program, [] {
glDrawElements(GL_TRIANGLE_STRIP, kRestartIndexCount, GL_UNSIGNED_INT, nullptr);
});
EXPECT_EQ(FirstGLError(), 0u);
ExpectSplitStrip(image, "glDrawElements over compute-written restart indices in an orphaned store");
}
// ---- the multi-draw rebase tier (MultiDraw.cpp RunRebasedDrawElements) ----
//
// An arbitrary restart index forces a multi-draw onto the one tier that rewrites the stream,
// and that tier reads the whole element array buffer on the CPU. Sub-draw 0 is {0,1,2} (the
// left triangle); sub-draw 1 is {7,3,4,5} (a restart, then the right triangle).
TEST_F(GpuWrittenDrawInputScenario, RestartIndicesAComputeShaderWroteSplitEveryMultiDrawStrip) {
if (!Ready()) return;
if (SkipBrokenMonolith(nullptr, kSyncMidRecording)) GTEST_SKIP() << m_skipReason;
const GLuint program = Program(kFlatVertex, kFlatFragment);
ASSERT_NE(program, 0u);
BuildRestartScene(/*orphaned=*/false);
const GLsizei counts[2] = {3, 4};
const void* offsets[2] = {nullptr, reinterpret_cast<const void*>(3 * sizeof(GLuint))};
const Image image = Render(program, [&] {
glMultiDrawElements(GL_TRIANGLE_STRIP, counts, GL_UNSIGNED_INT, offsets, 2);
});
EXPECT_EQ(FirstGLError(), 0u);
ExpectSplitStrip(image, "glMultiDrawElements over compute-written restart indices");
}
// ---- *IndirectCount (DirectGLES.cpp MultiDraw{Elements,Arrays}IndirectCount) ----
//
// The compute shader writes BOTH commands and the count; the staged copies hold zeroes, so a
// stale count draws nothing and a stale command (on a device without native indirect) draws
// nothing either.
TEST_F(GpuWrittenDrawInputScenario, ACountAComputeShaderWroteDrivesMultiDrawElementsIndirectCount) {
if (!Ready()) return;
if (SkipBrokenMonolith(kUnsyncedCount, nullptr)) GTEST_SKIP() << m_skipReason;
const GLuint program = Program(kFlatVertex, kFlatFragment);
ASSERT_NE(program, 0u);
const std::vector<GLfloat> vertices = HalvesVertices();
BindVertices(vertices.data(), static_cast<GLsizeiptr>(vertices.size() * sizeof(GLfloat)));
const GLuint indices[kHalfCount] = {0, 1, 2, 3, 4, 5};
Store(m_ebo, GL_ELEMENT_ARRAY_BUFFER, sizeof(indices), indices);
const ElementsCommand zeroCommands[2] = {};
Store(m_commands, GL_DRAW_INDIRECT_BUFFER, sizeof(zeroCommands), zeroCommands);
const GLuint zeroCount = 0;
Store(m_parameters, GL_PARAMETER_BUFFER, sizeof(zeroCount), &zeroCount);
WriteWithCompute(R"(#version 430 core
layout(local_size_x = 1) in;
layout(std430, binding = 0) buffer Commands { uint command[]; };
layout(std430, binding = 1) buffer Parameters { uint count; };
void main() {
command[0] = 6u; command[1] = 1u; command[2] = 0u; command[3] = 3u; command[4] = 0u;
command[5] = 6u; command[6] = 1u; command[7] = 0u; command[8] = 9u; command[9] = 0u;
count = 2u;
}
)",
m_commands, m_parameters, GL_COMMAND_BARRIER_BIT);
glBindVertexArray(m_vao);
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, m_ebo);
glBindBuffer(GL_DRAW_INDIRECT_BUFFER, m_commands);
glBindBuffer(GL_PARAMETER_BUFFER, m_parameters);
const Image image = Render(program, [] {
glMultiDrawElementsIndirectCount(GL_TRIANGLES, GL_UNSIGNED_INT, nullptr, 0, 2, 0);
});
EXPECT_EQ(FirstGLError(), 0u);
EXPECT_TRUE(IsGreen(Half(image, 0)))
<< "command 0 did not draw (" << Half(image, 0)
<< "): the count was read from a copy the compute shader never reached";
EXPECT_TRUE(IsGreen(Half(image, 1))) << "command 1 did not draw (" << Half(image, 1) << ")";
}
TEST_F(GpuWrittenDrawInputScenario, ACountAComputeShaderWroteDrivesMultiDrawArraysIndirectCount) {
if (!Ready()) return;
if (SkipBrokenMonolith(kUnsyncedCount, kUnsyncedCount)) GTEST_SKIP() << m_skipReason;
const GLuint program = Program(kFlatVertex, kFlatFragment);
ASSERT_NE(program, 0u);
const std::vector<GLfloat> vertices = HalvesVertices();
BindVertices(vertices.data(), static_cast<GLsizeiptr>(vertices.size() * sizeof(GLfloat)));
const ArraysCommand zeroCommands[2] = {};
Store(m_commands, GL_DRAW_INDIRECT_BUFFER, sizeof(zeroCommands), zeroCommands);
const GLuint zeroCount = 0;
Store(m_parameters, GL_PARAMETER_BUFFER, sizeof(zeroCount), &zeroCount);
WriteWithCompute(R"(#version 430 core
layout(local_size_x = 1) in;
layout(std430, binding = 0) buffer Commands { uint command[]; };
layout(std430, binding = 1) buffer Parameters { uint count; };
void main() {
command[0] = 6u; command[1] = 1u; command[2] = 3u; command[3] = 0u;
command[4] = 6u; command[5] = 1u; command[6] = 9u; command[7] = 0u;
count = 2u;
}
)",
m_commands, m_parameters, GL_COMMAND_BARRIER_BIT);
glBindVertexArray(m_vao);
glBindBuffer(GL_DRAW_INDIRECT_BUFFER, m_commands);
glBindBuffer(GL_PARAMETER_BUFFER, m_parameters);
const Image image = Render(program, [] {
glMultiDrawArraysIndirectCount(GL_TRIANGLES, nullptr, 0, 2, 0);
});
EXPECT_EQ(FirstGLError(), 0u);
EXPECT_TRUE(IsGreen(Half(image, 0)))
<< "command 0 did not draw (" << Half(image, 0)
<< "): the count was read from a copy the compute shader never reached";
EXPECT_TRUE(IsGreen(Half(image, 1))) << "command 1 did not draw (" << Half(image, 1) << ")";
}
// ---- a native indirect draw's gl_BaseVertex (DirectGLES.cpp ExecuteIndexedIndirectCommands) ----
//
// The GPU executes the command itself, so the geometry is right whatever the backend's copy
// says; gl_BaseVertex is the one field Espryt feeds from the CPU. The staged command names
// the left half (baseVertex 3), the compute shader moves it to the right half (9).
TEST_F(GpuWrittenDrawInputScenario, ABaseVertexAComputeShaderWroteReachesGlBaseVertex) {
if (!Ready()) return;
if (SkipBrokenMonolith(kUnsyncedBaseVertex, nullptr)) GTEST_SKIP() << m_skipReason;
const GLuint program = Program(kBaseVertexVertex, kBaseVertexFragment);
ASSERT_NE(program, 0u);
const std::vector<GLfloat> vertices = HalvesVertices();
BindVertices(vertices.data(), static_cast<GLsizeiptr>(vertices.size() * sizeof(GLfloat)));
const GLuint indices[kHalfCount] = {0, 1, 2, 3, 4, 5};
Store(m_ebo, GL_ELEMENT_ARRAY_BUFFER, sizeof(indices), indices);
const ElementsCommand staged{kHalfCount, 1, 0, kLeftFirst, 0};
Store(m_commands, GL_DRAW_INDIRECT_BUFFER, sizeof(staged), &staged);
WriteWithCompute(R"(#version 430 core
layout(local_size_x = 1) in;
layout(std430, binding = 0) buffer Commands { int command[]; };
void main() { command[3] = 9; }
)",
m_commands, 0, GL_COMMAND_BARRIER_BIT);
glBindVertexArray(m_vao);
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, m_ebo);
glBindBuffer(GL_DRAW_INDIRECT_BUFFER, m_commands);
const Image image = Render(program, [] {
glDrawElementsIndirect(GL_TRIANGLES, GL_UNSIGNED_INT, nullptr);
});
EXPECT_EQ(FirstGLError(), 0u);
EXPECT_TRUE(IsGreen(Half(image, 1)))
<< "the right half is " << Half(image, 1)
<< ": red means the draw ran with the compute-written baseVertex but gl_BaseVertex was "
"fed from the staged command; black means the draw did not run there at all";
EXPECT_FALSE(IsGreen(Half(image, 0))) << "the left half drew: the GPU saw the staged command";
}
// ---- indirect commands with no staged copy at all (DirectGLES.cpp ResolveIndirectCommandBytes) ----
//
// The command buffer is ORPHANED and only the compute shader ever writes it - the GPU-culling
// shape. Nothing was staged for it, so a backend that needs a CPU copy before it will issue
// the draw has to make one from its own GL buffer.
TEST_F(GpuWrittenDrawInputScenario, IndirectCommandsAComputeShaderWroteIntoAnOrphanedStoreDraw) {
if (!Ready()) return;
const GLuint program = Program(kFlatVertex, kFlatFragment);
ASSERT_NE(program, 0u);
const std::vector<GLfloat> vertices = HalvesVertices();
BindVertices(vertices.data(), static_cast<GLsizeiptr>(vertices.size() * sizeof(GLfloat)));
const GLuint indices[kHalfCount] = {0, 1, 2, 3, 4, 5};
Store(m_ebo, GL_ELEMENT_ARRAY_BUFFER, sizeof(indices), indices);
Store(m_commands, GL_DRAW_INDIRECT_BUFFER, sizeof(ElementsCommand), nullptr);
WriteWithCompute(R"(#version 430 core
layout(local_size_x = 1) in;
layout(std430, binding = 0) buffer Commands { uint command[]; };
void main() { command[0] = 6u; command[1] = 1u; command[2] = 0u; command[3] = 3u; command[4] = 0u; }
)",
m_commands, 0, GL_COMMAND_BARRIER_BIT);
glBindVertexArray(m_vao);
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, m_ebo);
glBindBuffer(GL_DRAW_INDIRECT_BUFFER, m_commands);
const Image image = Render(program, [] {
glDrawElementsIndirect(GL_TRIANGLES, GL_UNSIGNED_INT, nullptr);
});
EXPECT_EQ(FirstGLError(), 0u);
EXPECT_TRUE(IsGreen(Half(image, 0)))
<< "the compute-written command did not draw (" << Half(image, 0)
<< "): the backend refused a draw whose command buffer it had no CPU copy of";
EXPECT_FALSE(IsGreen(Half(image, 1))) << "the right half drew: the command was not the one written";
}
// ---- the other GPU writers: transform feedback, buffer images, atomic counters ----
//
// The same restart reader over indices a CAPTURE produced. Espryt maps the captured range
// back at glEndTransformFeedback for the client; the server's own copy has to take it too.
TEST_F(GpuWrittenDrawInputScenario, RestartIndicesATransformFeedbackCapturedSplitTheStrip) {
if (!Ready()) return;
if (SkipBrokenMonolith(nullptr, kSyncMidRecording)) GTEST_SKIP() << m_skipReason;
const GLuint program = Program(kFlatVertex, kFlatFragment);
ASSERT_NE(program, 0u);
const char* varyings[] = {"idx"};
const GLuint capture = XfbProgram(kCaptureIndicesVertex, kCaptureFragment, varyings, 1);
ASSERT_NE(capture, 0u);
BindVertices(kRestartVertices, sizeof(kRestartVertices));
Store(m_ebo, GL_ELEMENT_ARRAY_BUFFER, kRestartIndexCount * sizeof(GLuint), kRestartPoison.data());
CaptureIntoElementBuffer(capture, kRestartIndexCount);
ArmRestart();
const Image image = Render(program, [] {
glDrawElements(GL_TRIANGLE_STRIP, kRestartIndexCount, GL_UNSIGNED_INT, nullptr);
});
EXPECT_EQ(FirstGLError(), 0u);
ExpectSplitStrip(image, "glDrawElements over restart indices a transform feedback captured");
}
// ...through the SCATTER Espryt runs for a layout with holes: the server composes the range
// from its pre-capture bytes and the captured words, and its own copy has to keep the result.
TEST_F(GpuWrittenDrawInputScenario, RestartIndicesAScatteredTransformFeedbackCapturedSplitTheStrip) {
if (!Ready()) return;
if (SkipBrokenMonolith(nullptr, kSyncMidRecording)) GTEST_SKIP() << m_skipReason;
const GLuint program = Program(kFlatVertex, kFlatFragment);
ASSERT_NE(program, 0u);
const char* varyings[] = {"idx", "gl_SkipComponents1"};
const GLuint capture = XfbProgram(kCaptureScatteredVertex, kCaptureFragment, varyings, 2);
ASSERT_NE(capture, 0u);
BindVertices(kRestartVertices, sizeof(kRestartVertices));
Store(m_ebo, GL_ELEMENT_ARRAY_BUFFER, sizeof(kScatterStaged), kScatterStaged);
CaptureIntoElementBuffer(capture, 4);
ArmRestart();
const Image image = Render(program, [] {
glDrawElements(GL_TRIANGLE_STRIP, kRestartIndexCount, GL_UNSIGNED_INT, nullptr);
});
EXPECT_EQ(FirstGLError(), 0u);
ExpectSplitStrip(image, "glDrawElements over restart indices a scattered capture composed");
}
// ...and over indices IMAGE STORES wrote through a buffer texture on the element buffer.
TEST_F(GpuWrittenDrawInputScenario, RestartIndicesImageStoresWroteSplitTheStrip) {
if (!Ready()) return;
if (SkipBrokenMonolith(nullptr, kSyncMidRecording)) GTEST_SKIP() << m_skipReason;
const GLuint program = Program(kFlatVertex, kFlatFragment);
ASSERT_NE(program, 0u);
const GLuint compute = ComputeProgram(kImageStoreRestartIndices);
ASSERT_NE(compute, 0u);
BindVertices(kRestartVertices, sizeof(kRestartVertices));
Store(m_ebo, GL_ELEMENT_ARRAY_BUFFER, kRestartIndexCount * sizeof(GLuint), kRestartPoison.data());
glGenTextures(1, &m_texture);
glBindTexture(GL_TEXTURE_BUFFER, m_texture);
glTexBuffer(GL_TEXTURE_BUFFER, GL_R32UI, m_ebo);
glBindTexture(GL_TEXTURE_BUFFER, 0);
glBindImageTexture(0, m_texture, 0, GL_FALSE, 0, GL_WRITE_ONLY, GL_R32UI);
glUseProgram(compute);
glDispatchCompute(1, 1, 1);
glMemoryBarrier(GL_ELEMENT_ARRAY_BARRIER_BIT | GL_SHADER_IMAGE_ACCESS_BARRIER_BIT);
glUseProgram(0);
glBindImageTexture(0, 0, 0, GL_FALSE, 0, GL_READ_ONLY, GL_R32UI);
ArmRestart();
const Image image = Render(program, [] {
glDrawElements(GL_TRIANGLE_STRIP, kRestartIndexCount, GL_UNSIGNED_INT, nullptr);
});
EXPECT_EQ(FirstGLError(), 0u);
ExpectSplitStrip(image, "glDrawElements over restart indices image stores wrote");
}
// The GPU-culling shape at its most common: the draw count is an ATOMIC COUNTER the compute
// shader bumped once per surviving command, and that counter buffer is the parameter buffer.
TEST_F(GpuWrittenDrawInputScenario, ACountAnAtomicCounterProducedDrivesMultiDrawElementsIndirectCount) {
if (!Ready()) return;
if (SkipBrokenMonolith(kUnsyncedCount, nullptr)) GTEST_SKIP() << m_skipReason;
const GLuint program = Program(kFlatVertex, kFlatFragment);
ASSERT_NE(program, 0u);
const GLuint compute = ComputeProgram(R"(#version 430 core
layout(local_size_x = 1) in;
layout(binding = 0, offset = 0) uniform atomic_uint drawCount;
layout(std430, binding = 0) buffer Commands { uint command[]; };
void main() {
const uint baseVertex[2] = uint[2](3u, 9u);
for (int i = 0; i < 2; ++i) {
uint slot = atomicCounterIncrement(drawCount);
command[slot * 5u + 0u] = 6u;
command[slot * 5u + 1u] = 1u;
command[slot * 5u + 2u] = 0u;
command[slot * 5u + 3u] = baseVertex[i];
command[slot * 5u + 4u] = 0u;
}
}
)");
ASSERT_NE(compute, 0u);
const std::vector<GLfloat> vertices = HalvesVertices();
BindVertices(vertices.data(), static_cast<GLsizeiptr>(vertices.size() * sizeof(GLfloat)));
const GLuint indices[kHalfCount] = {0, 1, 2, 3, 4, 5};
Store(m_ebo, GL_ELEMENT_ARRAY_BUFFER, sizeof(indices), indices);
const ElementsCommand zeroCommands[2] = {};
Store(m_commands, GL_DRAW_INDIRECT_BUFFER, sizeof(zeroCommands), zeroCommands);
const GLuint zeroCount = 0;
Store(m_parameters, GL_ATOMIC_COUNTER_BUFFER, sizeof(zeroCount), &zeroCount);
glBindBufferBase(GL_ATOMIC_COUNTER_BUFFER, 0, m_parameters);
glBindBufferBase(GL_SHADER_STORAGE_BUFFER, 0, m_commands);
glUseProgram(compute);
glDispatchCompute(1, 1, 1);
glMemoryBarrier(GL_COMMAND_BARRIER_BIT | GL_ATOMIC_COUNTER_BARRIER_BIT | GL_SHADER_STORAGE_BARRIER_BIT);
glUseProgram(0);
glBindBufferBase(GL_ATOMIC_COUNTER_BUFFER, 0, 0);
glBindBufferBase(GL_SHADER_STORAGE_BUFFER, 0, 0);
glBindVertexArray(m_vao);
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, m_ebo);
glBindBuffer(GL_DRAW_INDIRECT_BUFFER, m_commands);
glBindBuffer(GL_PARAMETER_BUFFER, m_parameters);
const Image image = Render(program, [] {
glMultiDrawElementsIndirectCount(GL_TRIANGLES, GL_UNSIGNED_INT, nullptr, 0, 2, 0);
});
EXPECT_EQ(FirstGLError(), 0u);
EXPECT_TRUE(IsGreen(Half(image, 0)))
<< "command 0 did not draw (" << Half(image, 0)
<< "): the count was read from a copy the atomic counter never reached";
EXPECT_TRUE(IsGreen(Half(image, 1))) << "command 1 did not draw (" << Half(image, 1) << ")";
}
// ---- fp64 vertex narrowing (Managers.cpp SyncFloat64AttributeAsFloat32ByHandle) ----
//
// A GL_DOUBLE vertex array is narrowed to float on the CPU from the source buffer's bytes,
// and the narrowed copy is memoised on the source's serial - which a GPU write never moves.
// Two rounds: the first catches a stale read, the second a stale memo. The compute shader
// writes each double as its two IEEE-754 words (low word 0), so it needs no fp64 itself.
TEST_F(GpuWrittenDrawInputScenario, DoubleVerticesAComputeShaderWroteAreNarrowedFromTheWrittenBytes) {
if (!Ready()) return;
if (SkipBrokenMonolith(kUnsyncedDoubles, kSyncMidRecording)) GTEST_SKIP() << m_skipReason;
const GLuint program = Program(kFlatVertex, kFlatFragment);
ASSERT_NE(program, 0u);
// Four vertices, x then y, all 100.0: a strip wholly off the surface.
const double poison[8] = {100.0, 100.0, 100.0, 100.0, 100.0, 100.0, 100.0, 100.0};
glBindVertexArray(m_vao);
Store(m_vbo, GL_ARRAY_BUFFER, sizeof(poison), poison);
glEnableVertexAttribArray(0);
glVertexAttribPointer(0, 2, GL_DOUBLE, GL_FALSE, 2 * sizeof(double), nullptr);
// High words: -1.0 = 0xBFF00000, 1.0 = 0x3FF00000, 0.0 = 0.
const auto writeQuad = [&](const char* highWords) {
const std::string source = std::string(R"(#version 430 core
layout(local_size_x = 1) in;
layout(std430, binding = 0) buffer Positions { uint word[]; };
void main() {
const uint hi[8] = uint[8]()") + highWords + R"();
for (int i = 0; i < 8; ++i) { word[2 * i] = 0u; word[2 * i + 1] = hi[i]; }
}
)";
WriteWithCompute(source.c_str(), m_vbo, 0, GL_VERTEX_ATTRIB_ARRAY_BARRIER_BIT);
};
// Round 1: the whole surface, (-1,-1) (1,-1) (-1,1) (1,1).
writeQuad("0xBFF00000u, 0xBFF00000u, 0x3FF00000u, 0xBFF00000u, 0xBFF00000u, 0x3FF00000u, "
"0x3FF00000u, 0x3FF00000u");
Image image = Render(program, [] { glDrawArrays(GL_TRIANGLE_STRIP, 0, 4); });
EXPECT_EQ(FirstGLError(), 0u);
EXPECT_TRUE(IsGreen(Half(image, 0)) && IsGreen(Half(image, 1)))
<< "round 1: the quad is " << Half(image, 0) << " / " << Half(image, 1)
<< " - black means the doubles were narrowed from the staged poison, not the written bytes";
// Round 2: only the left half, (-1,-1) (0,-1) (-1,1) (0,1).
writeQuad("0xBFF00000u, 0xBFF00000u, 0u, 0xBFF00000u, 0xBFF00000u, 0x3FF00000u, 0u, 0x3FF00000u");
image = Render(program, [] { glDrawArrays(GL_TRIANGLE_STRIP, 0, 4); });
EXPECT_EQ(FirstGLError(), 0u);
EXPECT_TRUE(IsGreen(Half(image, 0))) << "round 2: the left half is " << Half(image, 0);
EXPECT_FALSE(IsGreen(Half(image, 1)))
<< "round 2: the right half still drew - the narrowed copy of round 1 was reused after the "
"second GPU write";
}
} // namespace MGITest
@@ -0,0 +1,591 @@
// MobileGL - MobileGL/MG_IntegrationTest/Scenarios/ImageBindableRemintScenario.cpp
// 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
//
// Scenario - THE FIRST glBindImageTexture OF A TEXTURE THE GPU HAS ALREADY WRITTEN.
//
// ES only allows immutable storage on an image unit, and a few formats need a wider carrier on
// top (TextureImpl::GetImageBindableStorageWidening), so Espryt re-mints a texture the first time
// it is image-bound after it already has driver storage (RequireImageBindableStorage). The new
// storage has to hold what the old one held. The re-mint used to fill it from the frontend's
// shadow - the CLIENT's copy of each level - and the shadow never sees a GPU write: a render or
// clear into the level, a glGenerateMipmap, a null-data definition only the GPU ever filled. Every
// such level came back as the bytes the application last uploaded, or zeroes. That is the
// ~26.9k-pixel iris-derivative difference: an RGBA16F render target, first image-bound after it
// was rendered into.
//
// Every case gives a texture its content by one means, lets the backend create its storage (a
// sampling draw, or the FBO attach of a GPU clear), writes it on the GPU where the case says so,
// and then binds it to an image unit for the first time: a compute imageLoad copies the level into
// a destination that was image-bound before it was ever synced (so the destination itself never
// re-mints), and the destination is read back through an FBO. The expectation is the GL answer.
//
// The controls are the other half: levels the GPU never wrote (including formats whose driver
// readback may be refused), levels the driver does not have yet because they were defined after
// the last sync, and a pending client write that has to land on top of GPU content. Magma never
// re-mints from a shadow (it copies image to image) and is the control backend.
#include <array>
#include <cstdint>
#include <cstdlib>
#include <sstream>
#include <string>
#include <vector>
#include "../Harness/HeadlessGL.h"
#include "../Harness/ScenarioFixture.h"
#ifdef GLAPI
#undef GLAPI
#endif
#define GL_GLEXT_PROTOTYPES
#include <GL/gl.h>
#include <GL/glcorearb.h>
#undef GL_GLEXT_PROTOTYPES
namespace MGITest {
namespace {
constexpr int kEdge = 8;
constexpr const char* kVS = R"(#version 430 core
void main() {
vec2 positions[3] = vec2[3](vec2(-1.0, -1.0), vec2(3.0, -1.0), vec2(-1.0, 3.0));
gl_Position = vec4(positions[gl_VertexID], 0.0, 1.0);
}
)";
constexpr const char* kSampleFS = R"(#version 430 core
uniform sampler2D u_tex;
out vec4 o_color;
void main() { o_color = texelFetch(u_tex, ivec2(gl_FragCoord.xy) % 4, 0); }
)";
// Copies an image into an RGBA8 image with imageLoad; a signed-normalized source is mapped
// to [0, 1] so it survives the unsigned destination.
std::string CopyCS(const char* srcFormat, bool snorm) {
std::string s = "#version 430 core\nlayout(local_size_x = 8, local_size_y = 8) in;\n";
s += std::string("layout(") + srcFormat + ", binding = 0) readonly uniform image2D u_src;\n";
s += "layout(rgba8, binding = 1) writeonly uniform image2D u_dst;\n";
s += "void main() { ivec2 p = ivec2(gl_GlobalInvocationID.xy); vec4 v = imageLoad(u_src, p);\n";
if (snorm) s += " v = v * 0.5 + 0.5;\n";
s += " imageStore(u_dst, p, v); }\n";
return s;
}
std::vector<GLubyte> Pattern(int edge, GLubyte magic) {
std::vector<GLubyte> t(static_cast<std::size_t>(edge * edge) * 4u);
for (int y = 0; y < edge; ++y)
for (int x = 0; x < edge; ++x) {
const std::size_t at = static_cast<std::size_t>((y * edge + x) * 4);
t[at + 0] = magic;
t[at + 1] = static_cast<GLubyte>(16 * (x + 1));
t[at + 2] = static_cast<GLubyte>(16 * (y + 1));
t[at + 3] = 0xFF;
}
return t;
}
std::vector<GLubyte> Solid(int edge, GLubyte r, GLubyte g, GLubyte b, GLubyte a) {
std::vector<GLubyte> t(static_cast<std::size_t>(edge * edge) * 4u);
for (std::size_t i = 0; i < t.size(); i += 4) {
t[i] = r;
t[i + 1] = g;
t[i + 2] = b;
t[i + 3] = a;
}
return t;
}
const GLfloat kGreen[4] = {0.0f, 1.0f, 0.0f, 1.0f};
class ImageBindableRemintScenario : public ScenarioTest {
protected:
void SetUp() override {
ScenarioTest::SetUp();
if (!Ready()) return;
std::string error;
m_sampleProgram = CompileProgram(kVS, kSampleFS, &error);
ASSERT_NE(m_sampleProgram, 0u) << error;
m_scratch = MakeColorFbo(4, 4);
glGenVertexArrays(1, &m_vao);
FirstGLError();
}
void TearDown() override {
if (!Ready()) return;
glUseProgram(0);
glBindImageTexture(0, 0, 0, GL_FALSE, 0, GL_READ_ONLY, GL_RGBA8);
glBindImageTexture(1, 0, 0, GL_FALSE, 0, GL_READ_ONLY, GL_RGBA8);
if (m_sampleProgram) glDeleteProgram(m_sampleProgram);
if (m_vao) glDeleteVertexArrays(1, &m_vao);
if (!m_textures.empty()) glDeleteTextures(static_cast<GLsizei>(m_textures.size()), m_textures.data());
if (m_fbo) glDeleteFramebuffers(1, &m_fbo);
BindDefaultFramebuffer();
DestroyColorFbo(m_scratch);
glViewport(0, 0, Gl().Width(), Gl().Height());
FirstGLError();
}
GLuint NewTexture() {
GLuint t = 0;
glGenTextures(1, &t);
m_textures.push_back(t);
return t;
}
static void Nearest(GLenum target, GLuint tex, int maxLevel) {
glBindTexture(target, tex);
glTexParameteri(target, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
glTexParameteri(target, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
glTexParameteri(target, GL_TEXTURE_MAX_LEVEL, maxLevel);
glBindTexture(target, 0);
}
// A draw that samples the texture: the backend now holds storage for it.
void SyncBySampling(GLuint tex) {
BindFbo(m_scratch);
glUseProgram(m_sampleProgram);
glActiveTexture(GL_TEXTURE0);
glBindTexture(GL_TEXTURE_2D, tex);
glUniform1i(glGetUniformLocation(m_sampleProgram, "u_tex"), 0);
glBindVertexArray(m_vao);
glDrawArrays(GL_TRIANGLES, 0, 3);
glBindVertexArray(0);
glBindTexture(GL_TEXTURE_2D, 0);
glUseProgram(0);
BindDefaultFramebuffer();
glFinish();
}
// A GPU write the frontend's shadow never sees. A layer >= 0 names one layer of an
// array texture.
void GpuClear(GLuint tex, int level, const GLfloat* color, int layer = -1) {
if (!m_fbo) glGenFramebuffers(1, &m_fbo);
glBindFramebuffer(GL_FRAMEBUFFER, m_fbo);
if (layer >= 0) {
glFramebufferTextureLayer(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, tex, level, layer);
} else {
glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_2D, tex, level);
}
EXPECT_EQ(glCheckFramebufferStatus(GL_FRAMEBUFFER), GLenum(GL_FRAMEBUFFER_COMPLETE))
<< "the clear target is incomplete";
glClearBufferfv(GL_COLOR, 0, color);
glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_2D, 0, 0);
BindDefaultFramebuffer();
}
// The first image binding of (src, level[, layer]): an imageLoad copy into a fresh RGBA8
// destination, read back through an FBO.
std::vector<GLubyte> ImageCopy(GLuint src, int level, GLenum unitFormat, const char* glslFormat,
bool snorm = false, int layer = 0) {
const GLuint dst = NewTexture();
glBindTexture(GL_TEXTURE_2D, dst);
glTexStorage2D(GL_TEXTURE_2D, 1, GL_RGBA8, kEdge, kEdge);
glBindTexture(GL_TEXTURE_2D, 0);
glBindImageTexture(1, dst, 0, GL_FALSE, 0, GL_WRITE_ONLY, GL_RGBA8);
glBindImageTexture(0, src, level, GL_FALSE, layer, GL_READ_ONLY, unitFormat);
EXPECT_EQ(FirstGLError(), 0u) << "glBindImageTexture";
const std::string cs = CopyCS(glslFormat, snorm);
const char* csp = cs.c_str();
const GLuint shader = glCreateShader(GL_COMPUTE_SHADER);
glShaderSource(shader, 1, &csp, nullptr);
glCompileShader(shader);
const GLuint program = glCreateProgram();
glAttachShader(program, shader);
glLinkProgram(program);
glDeleteShader(shader);
GLint linked = GL_FALSE;
glGetProgramiv(program, GL_LINK_STATUS, &linked);
EXPECT_EQ(linked, GL_TRUE) << "the copy program did not link";
glUseProgram(program);
// P11 M: A BARRIER THE SPEC DOES NOT ASK FOR, pinned for the reason
// TextureRemintPullScenario's twin gives (ID-P9-11, DEBTS "待归因"): on llvmpipe the
// kept-allocation case (GpuClearOverAnImmutableUploadSurvives - no re-mint to
// serialize anything) read the pre-clear upload once on the push-monolith arm under
// load, the same shape P9 measured 3/30 on the split arm. The cases ask what the
// storage HOLDS after the first image binding, not how a clear orders against an
// image load, so the ordering is pinned here.
glMemoryBarrier(GL_ALL_BARRIER_BITS);
glDispatchCompute(1, 1, 1);
glMemoryBarrier(GL_ALL_BARRIER_BITS);
glUseProgram(0);
glDeleteProgram(program);
glBindImageTexture(0, 0, 0, GL_FALSE, 0, GL_READ_ONLY, GL_RGBA8);
glBindImageTexture(1, 0, 0, GL_FALSE, 0, GL_READ_ONLY, GL_RGBA8);
if (!m_fbo) glGenFramebuffers(1, &m_fbo);
glBindFramebuffer(GL_FRAMEBUFFER, m_fbo);
glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_2D, dst, 0);
std::vector<GLubyte> out(static_cast<std::size_t>(kEdge * kEdge) * 4u, 0xA5);
glPixelStorei(GL_PACK_ALIGNMENT, 1);
glReadPixels(0, 0, kEdge, kEdge, GL_RGBA, GL_UNSIGNED_BYTE, out.data());
glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_2D, 0, 0);
BindDefaultFramebuffer();
EXPECT_EQ(FirstGLError(), 0u) << "copy/readback";
return out;
}
// The edge x edge corner of `got` against an RGBA8 expectation, the first `channels`
// channels only, within 2 codes (a normalized value's round trip).
static void ExpectImage(const std::vector<GLubyte>& got, int edge, const std::vector<GLubyte>& want,
int channels = 4) {
int bad = 0;
std::ostringstream first;
for (int y = 0; y < edge; ++y)
for (int x = 0; x < edge; ++x) {
const std::size_t g = static_cast<std::size_t>((y * kEdge + x) * 4);
const std::size_t w = static_cast<std::size_t>((y * edge + x) * 4);
bool texelBad = false;
for (int c = 0; c < channels; ++c)
texelBad |= std::abs(int(got[g + c]) - int(want[w + c])) > 2;
if (texelBad && bad++ == 0) {
first << "(" << x << "," << y << ") got " << int(got[g]) << "," << int(got[g + 1]) << ","
<< int(got[g + 2]) << "," << int(got[g + 3]) << " want " << int(want[w]) << ","
<< int(want[w + 1]) << "," << int(want[w + 2]) << "," << int(want[w + 3]);
}
}
EXPECT_EQ(bad, 0) << bad << " of " << edge * edge << " texels differ; first " << first.str();
}
// One glTexSubImage of opaque blue per {x, y, w, h} box, left pending.
static void WriteBlueBoxes(GLuint tex, const std::vector<std::array<int, 4>>& boxes) {
glBindTexture(GL_TEXTURE_2D, tex);
for (const auto& b : boxes) {
std::vector<GLubyte> blue(static_cast<std::size_t>(b[2] * b[3]) * 4u, 0);
for (std::size_t i = 0; i < blue.size(); i += 4) {
blue[i + 2] = 255;
blue[i + 3] = 255;
}
glTexSubImage2D(GL_TEXTURE_2D, 0, b[0], b[1], b[2], b[3], GL_RGBA, GL_UNSIGNED_BYTE, blue.data());
}
glBindTexture(GL_TEXTURE_2D, 0);
}
// kEdge x kEdge green, blue inside the boxes.
static std::vector<GLubyte> GreenWithBlueBoxes(const std::vector<std::array<int, 4>>& boxes) {
auto want = Solid(kEdge, 0, 255, 0, 255);
for (const auto& b : boxes)
for (int y = b[1]; y < b[1] + b[3]; ++y)
for (int x = b[0]; x < b[0] + b[2]; ++x) {
const std::size_t at = static_cast<std::size_t>((y * kEdge + x) * 4);
want[at] = 0;
want[at + 1] = 0;
want[at + 2] = 255;
want[at + 3] = 255;
}
return want;
}
GLuint m_sampleProgram = 0, m_vao = 0, m_fbo = 0;
ColorFbo m_scratch{};
std::vector<GLuint> m_textures;
};
// Control: the shadow and the driver agree.
TEST_F(ImageBindableRemintScenario, UploadedLevelKeepsItsUpload) {
if (!Ready()) return;
const GLuint t = NewTexture();
const auto p = Pattern(kEdge, 0x40);
glBindTexture(GL_TEXTURE_2D, t);
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA8, kEdge, kEdge, 0, GL_RGBA, GL_UNSIGNED_BYTE, p.data());
Nearest(GL_TEXTURE_2D, t, 0);
SyncBySampling(t);
ExpectImage(ImageCopy(t, 0, GL_RGBA8, "rgba8"), kEdge, p);
}
TEST_F(ImageBindableRemintScenario, GpuClearOverAnUploadSurvives) {
if (!Ready()) return;
const GLuint t = NewTexture();
const auto p = Pattern(kEdge, 0x40);
glBindTexture(GL_TEXTURE_2D, t);
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA8, kEdge, kEdge, 0, GL_RGBA, GL_UNSIGNED_BYTE, p.data());
Nearest(GL_TEXTURE_2D, t, 0);
SyncBySampling(t);
GpuClear(t, 0, kGreen);
SyncBySampling(t);
ExpectImage(ImageCopy(t, 0, GL_RGBA8, "rgba8"), kEdge, Solid(kEdge, 0, 255, 0, 255));
}
// Immutable in a core image format: the storage is already what an image unit needs.
TEST_F(ImageBindableRemintScenario, GpuClearOverAnImmutableUploadSurvives) {
if (!Ready()) return;
const GLuint t = NewTexture();
const auto p = Pattern(kEdge, 0x40);
glBindTexture(GL_TEXTURE_2D, t);
glTexStorage2D(GL_TEXTURE_2D, 1, GL_RGBA8, kEdge, kEdge);
glTexSubImage2D(GL_TEXTURE_2D, 0, 0, 0, kEdge, kEdge, GL_RGBA, GL_UNSIGNED_BYTE, p.data());
Nearest(GL_TEXTURE_2D, t, 0);
SyncBySampling(t);
GpuClear(t, 0, kGreen);
SyncBySampling(t);
ExpectImage(ImageCopy(t, 0, GL_RGBA8, "rgba8"), kEdge, Solid(kEdge, 0, 255, 0, 255));
}
// A null-data definition only the GPU ever filled: the shadow holds zeroes.
TEST_F(ImageBindableRemintScenario, GpuClearOfANullDefinitionSurvives) {
if (!Ready()) return;
const GLuint t = NewTexture();
glBindTexture(GL_TEXTURE_2D, t);
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA8, kEdge, kEdge, 0, GL_RGBA, GL_UNSIGNED_BYTE, nullptr);
Nearest(GL_TEXTURE_2D, t, 0);
SyncBySampling(t);
GpuClear(t, 0, kGreen);
SyncBySampling(t);
ExpectImage(ImageCopy(t, 0, GL_RGBA8, "rgba8"), kEdge, Solid(kEdge, 0, 255, 0, 255));
}
// A client write still pending when the bind comes lands on top of the GPU's content,
// and only inside its own box.
TEST_F(ImageBindableRemintScenario, PendingSubImageLandsOnTheGpuClear) {
if (!Ready()) return;
const GLuint t = NewTexture();
const auto p = Pattern(kEdge, 0x40);
glBindTexture(GL_TEXTURE_2D, t);
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA8, kEdge, kEdge, 0, GL_RGBA, GL_UNSIGNED_BYTE, p.data());
Nearest(GL_TEXTURE_2D, t, 0);
SyncBySampling(t);
GpuClear(t, 0, kGreen);
SyncBySampling(t);
const auto blue = Solid(2, 0, 0, 255, 255);
glBindTexture(GL_TEXTURE_2D, t);
glTexSubImage2D(GL_TEXTURE_2D, 0, 2, 2, 2, 2, GL_RGBA, GL_UNSIGNED_BYTE, blue.data());
glBindTexture(GL_TEXTURE_2D, 0);
auto want = Solid(kEdge, 0, 255, 0, 255);
for (int y = 2; y < 4; ++y)
for (int x = 2; x < 4; ++x) {
const std::size_t at = static_cast<std::size_t>((y * kEdge + x) * 4);
want[at] = 0;
want[at + 1] = 0;
want[at + 2] = 255;
want[at + 3] = 255;
}
ExpectImage(ImageCopy(t, 0, GL_RGBA8, "rgba8"), kEdge, want);
}
// Several pending writes: only their texels land on the GPU's clear, never the gaps between
// them. The re-mint merged the union box of the shadow over the readback whenever the rect
// list was withheld, and kept the shadow outright when that box spanned the level.
// Two 3x7 columns with a one-texel gap: 42 of their union's 49 texels, past the 3/4 at which
// the storage withholds its rect list.
TEST_F(ImageBindableRemintScenario, PendingSubImagesFillingThreeQuartersOfTheirUnionLandOnTheGpuClear) {
if (!Ready()) return;
const std::vector<std::array<int, 4>> boxes = {{0, 0, 3, 7}, {4, 0, 3, 7}};
const GLuint t = NewTexture();
const auto p = Pattern(kEdge, 0x40);
glBindTexture(GL_TEXTURE_2D, t);
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA8, kEdge, kEdge, 0, GL_RGBA, GL_UNSIGNED_BYTE, p.data());
Nearest(GL_TEXTURE_2D, t, 0);
SyncBySampling(t);
GpuClear(t, 0, kGreen);
SyncBySampling(t);
WriteBlueBoxes(t, boxes);
ExpectImage(ImageCopy(t, 0, GL_RGBA8, "rgba8"), kEdge, GreenWithBlueBoxes(boxes));
}
// Three quadrants: the union box is the whole level; the fourth quadrant is the GPU's.
TEST_F(ImageBindableRemintScenario, PendingSubImagesWhoseUnionSpansTheLevelLandOnTheGpuClear) {
if (!Ready()) return;
const std::vector<std::array<int, 4>> boxes = {{0, 0, 4, 4}, {4, 0, 4, 4}, {0, 4, 4, 4}};
const GLuint t = NewTexture();
const auto p = Pattern(kEdge, 0x40);
glBindTexture(GL_TEXTURE_2D, t);
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA8, kEdge, kEdge, 0, GL_RGBA, GL_UNSIGNED_BYTE, p.data());
Nearest(GL_TEXTURE_2D, t, 0);
SyncBySampling(t);
GpuClear(t, 0, kGreen);
SyncBySampling(t);
WriteBlueBoxes(t, boxes);
ExpectImage(ImageCopy(t, 0, GL_RGBA8, "rgba8"), kEdge, GreenWithBlueBoxes(boxes));
}
TEST_F(ImageBindableRemintScenario, GeneratedMipLevelSurvives) {
if (!Ready()) return;
const GLuint t = NewTexture();
const auto s = Solid(kEdge, 200, 100, 50, 255);
glBindTexture(GL_TEXTURE_2D, t);
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA8, kEdge, kEdge, 0, GL_RGBA, GL_UNSIGNED_BYTE, s.data());
glGenerateMipmap(GL_TEXTURE_2D);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST_MIPMAP_NEAREST);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
glBindTexture(GL_TEXTURE_2D, 0);
SyncBySampling(t);
ExpectImage(ImageCopy(t, 1, GL_RGBA8, "rgba8"), kEdge / 2, Solid(kEdge / 2, 200, 100, 50, 255));
}
TEST_F(ImageBindableRemintScenario, GpuClearOfAnUploadedMipLevelSurvives) {
if (!Ready()) return;
const GLuint t = NewTexture();
const auto s0 = Solid(kEdge, 200, 100, 50, 255);
const auto s1 = Solid(kEdge / 2, 10, 20, 30, 255);
glBindTexture(GL_TEXTURE_2D, t);
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA8, kEdge, kEdge, 0, GL_RGBA, GL_UNSIGNED_BYTE, s0.data());
glTexImage2D(GL_TEXTURE_2D, 1, GL_RGBA8, kEdge / 2, kEdge / 2, 0, GL_RGBA, GL_UNSIGNED_BYTE, s1.data());
Nearest(GL_TEXTURE_2D, t, 1);
SyncBySampling(t);
GpuClear(t, 1, kGreen);
SyncBySampling(t);
ExpectImage(ImageCopy(t, 1, GL_RGBA8, "rgba8"), kEdge / 2, Solid(kEdge / 2, 0, 255, 0, 255));
}
// Control: a level defined (null data) after the last sync is not on the driver yet, so
// there is nothing to read back for it; the bound level 0 must still be the upload.
TEST_F(ImageBindableRemintScenario, LevelDefinedAfterTheSyncDoesNotDisturbTheOthers) {
if (!Ready()) return;
const GLuint t = NewTexture();
const auto p = Pattern(kEdge, 0x40);
glBindTexture(GL_TEXTURE_2D, t);
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA8, kEdge, kEdge, 0, GL_RGBA, GL_UNSIGNED_BYTE, p.data());
Nearest(GL_TEXTURE_2D, t, 0);
SyncBySampling(t);
glBindTexture(GL_TEXTURE_2D, t);
glTexImage2D(GL_TEXTURE_2D, 1, GL_RGBA8, kEdge / 2, kEdge / 2, 0, GL_RGBA, GL_UNSIGNED_BYTE, nullptr);
glBindTexture(GL_TEXTURE_2D, 0);
ExpectImage(ImageCopy(t, 0, GL_RGBA8, "rgba8"), kEdge, p);
}
// Control: the same late level, partly uploaded - the shadow is the only copy of it.
TEST_F(ImageBindableRemintScenario, LevelDefinedAndUploadedAfterTheSyncKeepsTheUpload) {
if (!Ready()) return;
const GLuint t = NewTexture();
const auto p = Pattern(kEdge, 0x40);
glBindTexture(GL_TEXTURE_2D, t);
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA8, kEdge, kEdge, 0, GL_RGBA, GL_UNSIGNED_BYTE, p.data());
Nearest(GL_TEXTURE_2D, t, 0);
SyncBySampling(t);
glBindTexture(GL_TEXTURE_2D, t);
glTexImage2D(GL_TEXTURE_2D, 1, GL_RGBA8, kEdge / 2, kEdge / 2, 0, GL_RGBA, GL_UNSIGNED_BYTE, nullptr);
const auto blue = Solid(2, 0, 0, 255, 255);
glTexSubImage2D(GL_TEXTURE_2D, 1, 0, 0, 2, 2, GL_RGBA, GL_UNSIGNED_BYTE, blue.data());
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAX_LEVEL, 1);
glBindTexture(GL_TEXTURE_2D, 0);
ExpectImage(ImageCopy(t, 1, GL_RGBA8, "rgba8"), 2, blue);
}
// GL_RG8 is not an ESSL image format, so the re-mint also WIDENS the storage (to RGBA8)
// and cannot keep the old allocation even though it is immutable.
TEST_F(ImageBindableRemintScenario, GpuClearSurvivesAWideningRemint) {
if (!Ready()) return;
const GLuint t = NewTexture();
std::vector<GLubyte> rg(static_cast<std::size_t>(kEdge * kEdge) * 2u, 0x33);
glBindTexture(GL_TEXTURE_2D, t);
glTexStorage2D(GL_TEXTURE_2D, 1, GL_RG8, kEdge, kEdge);
glPixelStorei(GL_UNPACK_ALIGNMENT, 1);
glTexSubImage2D(GL_TEXTURE_2D, 0, 0, 0, kEdge, kEdge, GL_RG, GL_UNSIGNED_BYTE, rg.data());
glPixelStorei(GL_UNPACK_ALIGNMENT, 4);
Nearest(GL_TEXTURE_2D, t, 0);
SyncBySampling(t);
const GLfloat c[4] = {0.5f, 0.25f, 0.0f, 1.0f};
GpuClear(t, 0, c);
SyncBySampling(t);
ExpectImage(ImageCopy(t, 0, GL_RG8, "rg8"), kEdge, Solid(kEdge, 128, 64, 0, 255), 2);
}
// Control: widened, uploaded only.
TEST_F(ImageBindableRemintScenario, UploadSurvivesAWideningRemint) {
if (!Ready()) return;
const GLuint t = NewTexture();
std::vector<GLubyte> rg(static_cast<std::size_t>(kEdge * kEdge) * 2u);
for (std::size_t i = 0; i < rg.size(); i += 2) {
rg[i] = 0x33;
rg[i + 1] = 0x99;
}
glBindTexture(GL_TEXTURE_2D, t);
glTexStorage2D(GL_TEXTURE_2D, 1, GL_RG8, kEdge, kEdge);
glPixelStorei(GL_UNPACK_ALIGNMENT, 1);
glTexSubImage2D(GL_TEXTURE_2D, 0, 0, 0, kEdge, kEdge, GL_RG, GL_UNSIGNED_BYTE, rg.data());
glPixelStorei(GL_UNPACK_ALIGNMENT, 4);
Nearest(GL_TEXTURE_2D, t, 0);
SyncBySampling(t);
ExpectImage(ImageCopy(t, 0, GL_RG8, "rg8"), kEdge, Solid(kEdge, 0x33, 0x99, 0, 255), 2);
}
// Control: a signed-normalized level (not colour-renderable on every ES driver, so its
// readback may be refused and the shadow used instead), uploaded only.
TEST_F(ImageBindableRemintScenario, SnormUploadSurvives) {
if (!Ready()) return;
const GLuint t = NewTexture();
std::vector<GLbyte> s(static_cast<std::size_t>(kEdge * kEdge) * 4u);
for (std::size_t i = 0; i < s.size(); i += 4) {
s[i] = 127;
s[i + 1] = 0;
s[i + 2] = -127;
s[i + 3] = 127;
}
glBindTexture(GL_TEXTURE_2D, t);
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA8_SNORM, kEdge, kEdge, 0, GL_RGBA, GL_BYTE, s.data());
Nearest(GL_TEXTURE_2D, t, 0);
SyncBySampling(t);
ExpectImage(ImageCopy(t, 0, GL_RGBA8_SNORM, "rgba8_snorm", true), kEdge, Solid(kEdge, 255, 128, 0, 255),
3);
}
// Control: RGBA16_SNORM is desktop-only as an image format, so this re-mint widens too.
TEST_F(ImageBindableRemintScenario, Snorm16UploadSurvivesAWideningRemint) {
if (!Ready()) return;
const GLuint t = NewTexture();
std::vector<GLshort> s(static_cast<std::size_t>(kEdge * kEdge) * 4u);
for (std::size_t i = 0; i < s.size(); i += 4) {
s[i] = 32767;
s[i + 1] = 0;
s[i + 2] = -32767;
s[i + 3] = 32767;
}
glBindTexture(GL_TEXTURE_2D, t);
glTexStorage2D(GL_TEXTURE_2D, 1, GL_RGBA16_SNORM, kEdge, kEdge);
glTexSubImage2D(GL_TEXTURE_2D, 0, 0, 0, kEdge, kEdge, GL_RGBA, GL_SHORT, s.data());
Nearest(GL_TEXTURE_2D, t, 0);
SyncBySampling(t);
ExpectImage(ImageCopy(t, 0, GL_RGBA16_SNORM, "rgba16_snorm", true), kEdge,
Solid(kEdge, 255, 128, 0, 255), 3);
}
// An array texture is read back one layer at a time: the GPU-cleared layer and the
// uploaded one both have to come through. The clear's FBO attach is what gives the
// texture its driver storage here.
TEST_F(ImageBindableRemintScenario, GpuClearOfOneArrayLayerSurvivesBesideTheUploadedLayer) {
if (!Ready()) return;
const GLuint t = NewTexture();
const auto p = Pattern(kEdge, 0x40);
std::vector<GLubyte> layers(p);
layers.insert(layers.end(), p.begin(), p.end());
glBindTexture(GL_TEXTURE_2D_ARRAY, t);
glTexImage3D(GL_TEXTURE_2D_ARRAY, 0, GL_RGBA8, kEdge, kEdge, 2, 0, GL_RGBA, GL_UNSIGNED_BYTE,
layers.data());
glBindTexture(GL_TEXTURE_2D_ARRAY, 0);
Nearest(GL_TEXTURE_2D_ARRAY, t, 0);
GpuClear(t, 0, kGreen, /*layer=*/1);
glFinish();
ExpectImage(ImageCopy(t, 0, GL_RGBA8, "rgba8", false, /*layer=*/1), kEdge, Solid(kEdge, 0, 255, 0, 255));
ExpectImage(ImageCopy(t, 0, GL_RGBA8, "rgba8", false, /*layer=*/0), kEdge, p);
}
// A view owns no storage, so its first image binding is the VIEWED texture's transition,
// and the GPU's texels there have to come through it. Needs glTextureView, which Espryt
// only has where the ES driver does (not on llvmpipe).
TEST_F(ImageBindableRemintScenario, GpuClearSurvivesTheFirstImageBindingOfAView) {
if (!Ready()) return;
const GLuint owner = NewTexture();
const auto p = Pattern(kEdge, 0x40);
glBindTexture(GL_TEXTURE_2D, owner);
glTexStorage2D(GL_TEXTURE_2D, 1, GL_RGBA8, kEdge, kEdge);
glTexSubImage2D(GL_TEXTURE_2D, 0, 0, 0, kEdge, kEdge, GL_RGBA, GL_UNSIGNED_BYTE, p.data());
Nearest(GL_TEXTURE_2D, owner, 0);
SyncBySampling(owner);
GpuClear(owner, 0, kGreen);
SyncBySampling(owner);
const GLuint view = NewTexture();
FirstGLError();
glTextureView(view, GL_TEXTURE_2D, owner, GL_RGBA8, 0, 1, 0, 1);
if (const GLenum error = FirstGLError()) {
GTEST_SKIP() << "glTextureView is unavailable on backend " << Gl().BackendName() << " ("
<< GLErrorName(error) << ")";
}
Nearest(GL_TEXTURE_2D, view, 0);
SyncBySampling(view);
ExpectImage(ImageCopy(view, 0, GL_RGBA8, "rgba8"), kEdge, Solid(kEdge, 0, 255, 0, 255));
}
} // namespace
} // namespace MGITest
@@ -49,8 +49,10 @@
#include <vector>
#include "../Harness/HeadlessGL.h"
#include "../Harness/PersistentMapPeek.h"
#include "../Harness/PipeStatsWindow.h"
#include "../Harness/ScenarioFixture.h"
#include "../Harness/SplitLane.h"
#ifdef GLAPI
#undef GLAPI
@@ -94,6 +96,12 @@ void main() { word = 0xC0FFEEu; }
// Set by the MapPersistentRoundtrips. ctest entry and by nothing else; a harness marker,
// never read by the library.
constexpr const char* kLaneMarker = "MGITEST_MPR_LANE";
// P11 A1: set (to the tier the lane names, "T0") only by the `.AdoptTier0.` tcp entries,
// which also set MOBILEGL_IPC_ADOPT_TIER=0; a harness marker, never read by the library.
constexpr const char* kAdoptTierRefusalMarker = "MGITEST_ADOPT_TIER_REFUSAL";
// P11 B2: set by the shared-segment `.AdoptTier0.` / `.AdoptT0Disallowed.` entries to the reason word the
// server gives for not granting T0 ("no AHardwareBuffer" on a host, "disallowed" with its allow switch off).
constexpr const char* kAdoptT0FallbackMarker = "MGITEST_ADOPT_T0_FALLBACK";
// Draws issued against the arena inside the counted window. One definition, many draws:
// "one per definition" (1) and "one per draw" (kDrawsInTheWindow) have to be different
// numbers or the assertion cannot tell them apart.
@@ -260,6 +268,27 @@ void main() { word = 0xC0FFEEu; }
return px;
}
// P11 A2: the arena's tier as the frontend object records it, against the lane's
// declaration. Adopted = the resource owner minted a persistent coherent mapping and
// the CPU shadow was released; emulated = the owner declined (T2) and the shadow is
// the truth. Pixels cannot tell them apart - every case above is green on both.
void ExpectDeclaredArm(const std::string& declared, const char* when) {
bool adopted = false;
ASSERT_TRUE(PeekBufferIsAdoptedPersistentMap(m_arena, &adopted))
<< "no frontend BufferObject behind the arena " << when;
const std::string landed = adopted ? "adopted" : "emulated";
RecordProperty(std::string("arena_arm_") + (m_armPeeks++ == 0 ? "defined" : "used"),
landed.c_str());
EXPECT_EQ(declared, landed)
<< "the lane declared the " << declared << " arm and the " << (kArenaBytes >> 20)
<< " MiB arena is " << landed << " " << when
<< ". Monolith lanes adopt on both backends (Espryt's handle arm mints through "
"Ops_H_MapPersistentTracked, Magma's legacy AcquirePersistentMap); split lanes "
"run T2, where every acquisition declines.";
}
int m_armPeeks = 0;
unsigned int m_program = 0;
unsigned int m_compute = 0;
unsigned int m_vao = 0;
@@ -293,6 +322,44 @@ void main() { word = 0xC0FFEEu; }
EXPECT_LT(px[0], 50) << "the draw still shows the previous frame's bytes";
}
// P11 B2: THE SAME REWRITE WITH NO READBACK BETWEEN THE TWO DRAWS. The case above reads
// pixels after the first draw, which retires it before the SubData, so a store that took the
// write IN PLACE (straight into pages the GPU reads - a T0 import is the client's
// AHardwareBuffer, an adopted store is a coherent map) would still pass it. Here the first
// draw is only queued when the arena is rewritten: it must still fetch the bytes it was
// issued against (red, left half) and the second draw the new ones (green, right half).
TEST_F(LargeArenaAdoptionScenario, ADrawQueuedBeforeASubDataKeepsItsOwnBytes) {
if (!Ready() || IsSkipped()) return;
const GLint w = Gl().Width();
const GLint h = Gl().Height();
UploadQuad(1.f, 0.f, 0.f);
glViewport(0, 0, w, h);
glClearColor(0.f, 0.f, 0.f, 1.f);
glClear(GL_COLOR_BUFFER_BIT);
glUseProgram(m_program);
glBindVertexArray(m_vao);
glEnable(GL_SCISSOR_TEST);
glScissor(0, 0, w / 2, h);
glDrawArrays(GL_TRIANGLES, 0, 6);
UploadQuad(0.f, 1.f, 0.f);
glScissor(w / 2, 0, w - w / 2, h);
glDrawArrays(GL_TRIANGLES, 0, 6);
glDisable(GL_SCISSOR_TEST);
std::array<unsigned char, 4> left = {0, 0, 0, 0};
std::array<unsigned char, 4> right = {0, 0, 0, 0};
glReadPixels(w / 4, h / 2, 1, 1, GL_RGBA, GL_UNSIGNED_BYTE, left.data());
glReadPixels(w / 2 + w / 4, h / 2, 1, 1, GL_RGBA, GL_UNSIGNED_BYTE, right.data());
EXPECT_EQ(FirstGLError(), 0u);
EXPECT_GT(left[0], 200) << "the draw queued BEFORE the rewrite lost its bytes: the SubData reached "
"pages that draw had not read yet (a write that landed in place instead "
"of GPU-ordered)";
EXPECT_LT(left[1], 50) << "the draw queued before the rewrite fetched the rewritten bytes";
EXPECT_GT(right[1], 200) << "the draw after the rewrite did not see the new bytes";
EXPECT_LT(right[0], 50) << "the draw after the rewrite still fetched the old bytes";
}
// Respecifying a frontend buffer preserves its VAO attachments even when the
// backend replaces the adopted store's GL name. Keep every attribute binding
// unchanged so a stale backend VAO cannot be repaired by a frontend rebind.
@@ -341,6 +408,44 @@ void main() { word = 0xC0FFEEu; }
glDeleteVertexArrays(1, &otherVao);
}
// P11 B2: A RESPECIFY WHILE A DRAW OF THE OLD STORE IS STILL QUEUED. The case above reads pixels
// before every respecify, so the old store is retired by then and a server that freed it at
// once would still pass. Here the first draw is only queued when the arena is redefined: the
// old store (a T0 import: the client's AHardwareBuffer, which the client lets go of at this
// very respecify) must outlive it, and the draw must show the bytes it was issued against.
TEST_F(LargeArenaAdoptionScenario, ARespecifyWithADrawQueuedKeepsThatDrawsStore) {
if (!Ready() || IsSkipped()) return;
const GLint w = Gl().Width();
const GLint h = Gl().Height();
UploadQuad(1.f, 0.f, 0.f);
glViewport(0, 0, w, h);
glClearColor(0.f, 0.f, 0.f, 1.f);
glClear(GL_COLOR_BUFFER_BIT);
glUseProgram(m_program);
glBindVertexArray(m_vao);
glEnable(GL_SCISSOR_TEST);
glScissor(0, 0, w / 2, h);
glDrawArrays(GL_TRIANGLES, 0, 6);
glBindBuffer(GL_ARRAY_BUFFER, m_arena);
glBufferData(GL_ARRAY_BUFFER, kArenaBytes, nullptr, GL_DYNAMIC_DRAW);
UploadQuad(0.f, 1.f, 0.f);
glScissor(w / 2, 0, w - w / 2, h);
glDrawArrays(GL_TRIANGLES, 0, 6);
glDisable(GL_SCISSOR_TEST);
std::array<unsigned char, 4> left = {0, 0, 0, 0};
std::array<unsigned char, 4> right = {0, 0, 0, 0};
glReadPixels(w / 4, h / 2, 1, 1, GL_RGBA, GL_UNSIGNED_BYTE, left.data());
glReadPixels(w / 2 + w / 4, h / 2, 1, 1, GL_RGBA, GL_UNSIGNED_BYTE, right.data());
EXPECT_EQ(FirstGLError(), 0u);
EXPECT_GT(left[0], 200) << "the draw queued before the respecify lost the store it was issued "
"against (freed before that draw ran)";
EXPECT_LT(left[1], 50) << "the draw queued before the respecify fetched the new store's bytes";
EXPECT_GT(right[1], 200) << "the draw after the respecify did not fetch the new store";
EXPECT_LT(right[0], 50) << "the draw after the respecify still fetched the old store";
}
TEST_F(LargeArenaAdoptionScenario, RespecifiedIndexArenaKeepsVaoBinding) {
if (!Ready() || IsSkipped()) return;
@@ -505,4 +610,140 @@ void main() { word = 0xC0FFEEu; }
<< window.line;
}
// P11 A2 (MG_Remote/CONTRACT-P11.md §2): WHICH TIER THE ARENA REALLY LANDED IN. The cases above
// are green whether the arena adopted or stayed emulated, so the lane declares the arm
// (MGITEST_PERSISTENT_MAP_ARM) and this case asserts it, the way
// PersistentCoherentMapScenario.TheMapLandsInTheArmItsLaneDeclares does for a sub-16-MiB map.
// Asked twice: after the NULL-data definition (TryAdoptLargeStorage's door) and after a frame
// of use that ends in an SSBO binding and a dispatch (the storage-binding door,
// EnsureGpuResidentStorage, which Magma's server walk opens). Lanes that declare nothing - the
// ambient entries, which run under whatever transport the job exports - skip.
TEST_F(LargeArenaAdoptionScenario, TheArenaLandsInTheTierItsLaneDeclares) {
if (!Ready() || IsSkipped()) return;
const std::string declared = SplitLane::DeclaredPersistentMapArm();
if (declared.empty()) {
GTEST_SKIP() << "this lane declares no arm: MGITEST_PERSISTENT_MAP_ARM is set only by the "
"lanes that pin their transport (monolith: adopted; the split arms: "
"emulated)";
}
if (!PersistentMapPeekAvailable()) {
GTEST_SKIP() << "this lane declares the " << declared << " arm but Harness/PersistentMapPeek "
"cannot look in this build (on Android this module links the shipping "
"library, built -fvisibility=hidden). 'Could not look' is not 'it was "
<< declared << "'.";
}
ExpectDeclaredArm(declared, "right after its NULL-data definition");
if (HasFatalFailure()) return;
UploadQuad(1.f, 0.f, 0.f);
DrawQuad();
EXPECT_GT(CenterPixel()[0], 200) << "the draw from the arena never landed";
Gl().EndFrame();
GLint maxComputeStorageBlocks = 0;
glGetIntegerv(GL_MAX_COMPUTE_SHADER_STORAGE_BLOCKS, &maxComputeStorageBlocks);
if (maxComputeStorageBlocks >= 1) {
const GLuint compute = CompileStage(GL_COMPUTE_SHADER, kMarkerComputeSource);
ASSERT_NE(compute, 0u) << m_buildLog;
m_compute = glCreateProgram();
glAttachShader(m_compute, compute);
glLinkProgram(m_compute);
glDeleteShader(compute);
glBindBufferRange(GL_SHADER_STORAGE_BUFFER, 0, m_arena, 0, sizeof(unsigned int));
glUseProgram(m_compute);
glDispatchCompute(1, 1, 1);
glMemoryBarrier(GL_SHADER_STORAGE_BARRIER_BIT | GL_BUFFER_UPDATE_BARRIER_BIT);
unsigned int marker = 0;
glBindBuffer(GL_ARRAY_BUFFER, m_arena);
glGetBufferSubData(GL_ARRAY_BUFFER, 0, sizeof(marker), &marker);
EXPECT_EQ(marker, 0xC0FFEEu) << "the dispatch through the arena's SSBO binding never landed";
} else {
RecordProperty("arena_arm_used", "not asked: no compute shader storage blocks");
}
EXPECT_EQ(FirstGLError(), 0u);
ExpectDeclaredArm(declared, "after a frame of draws, an SSBO binding and a dispatch");
}
// P11 A1 (MG_Remote/CONTRACT-P11.md §1): THE TCP LANE WITH MOBILEGL_IPC_ADOPT_TIER=0. A stream
// data plane has no memory the two sides share, so the client refuses the knob by name at its
// handshake - exactly one `Refuse{AdoptTierOnStream, "T0"}` line in its log - and the session
// runs T2: the other cases in the same `.AdoptTier0.` lane are green and the arena is emulated.
// Before A1 this lane ran T2 silently. Everywhere else it skips.
TEST_F(LargeArenaAdoptionScenario, AStreamSessionRefusesTheAdoptTierOnceByName) {
if (!Ready() || IsSkipped()) return;
const char* tier = std::getenv(kAdoptTierRefusalMarker);
if (tier == nullptr || tier[0] == '\0') {
GTEST_SKIP() << "runs only in the `.AdoptTier0.` tcp entries, which set "
"MOBILEGL_IPC_ADOPT_TIER=0 and " << kAdoptTierRefusalMarker << "=T0";
}
if (PipeStatsWindow::LibraryLogPath().empty()) {
GTEST_SKIP() << "the lane configured no MOBILEGL_LOG_FILE_PATH, and the client's log is "
"where the refusal is written";
}
// A frame of the arena's work first: a refusal is only worth reading from a session that
// then ran - and an armed split lane requires the case to move the record ordinal anyway.
UploadQuad(0.f, 1.f, 0.f);
DrawQuad();
EXPECT_GT(CenterPixel()[1], 200) << "the session that refused the tier did not render";
EXPECT_EQ(FirstGLError(), 0u);
Gl().EndFrame();
const std::string log = PipeStatsWindow::ReadWholeFile(PipeStatsWindow::LibraryLogPath());
const std::string line = std::string("Refuse{AdoptTierOnStream, \"") + tier + "\"}";
std::size_t count = 0;
for (auto at = log.find(line); at != std::string::npos; at = log.find(line, at + 1)) ++count;
RecordProperty("adopt_tier_refusals", static_cast<int>(count));
EXPECT_EQ(count, 1u) << "the client names MOBILEGL_IPC_ADOPT_TIER on a stream exactly once, at "
"its handshake (" << line << "), in " << PipeStatsWindow::LibraryLogPath();
EXPECT_EQ(log.find("Fatal{UnimplementedAdoptTier"), std::string::npos) << log;
}
// P11 B2 (MG_Remote/CONTRACT-P11.md B2): A SHARED-SEGMENT SESSION THAT ASKS T0 AND CANNOT RUN IT.
// MOBILEGL_IPC_ADOPT_TIER=0 over shared segments asks T0 in the Hello (A1 made it die at the
// handshake; ruling ID-P11-14 made it this). The lane's server cannot grant it - a host has no
// AHardwareBuffer, or `.AdoptT0Disallowed.` turned its allow switch off - so it refuses by name
// at its first native bind, publishes no kCapAdoptT0, and the client falls back to T2 with ONE
// named line at its first map_persistent. Never a Fatal; the other cases in the lane are green
// and the arena is emulated. Everywhere else it skips.
TEST_F(LargeArenaAdoptionScenario, ASessionThatCannotRunT0FallsBackToT2OnceByName) {
if (!Ready() || IsSkipped()) return;
const char* why = std::getenv(kAdoptT0FallbackMarker);
if (why == nullptr || why[0] == '\0') {
GTEST_SKIP() << "runs only in the shared-segment `.AdoptTier0.` / `.AdoptT0Disallowed.` entries, "
"which set MOBILEGL_IPC_ADOPT_TIER=0 and " << kAdoptT0FallbackMarker;
}
if (PipeStatsWindow::LibraryLogPath().empty()) {
GTEST_SKIP() << "the lane configured no MOBILEGL_LOG_FILE_PATH, and the two logs are where the "
"fallback is written";
}
// A frame of the arena's work first: the fallback is only worth reading from a session that
// then ran on T2 - and the client decides at its first map_persistent, which SetUp's
// definition of the arena has already made.
UploadQuad(0.f, 1.f, 0.f);
DrawQuad();
EXPECT_GT(CenterPixel()[1], 200) << "the session that fell back to T2 did not render";
EXPECT_EQ(FirstGLError(), 0u);
Gl().EndFrame();
const auto countOf = [](const std::string& text, const std::string& needle) {
std::size_t n = 0;
for (auto at = text.find(needle); at != std::string::npos; at = text.find(needle, at + 1)) ++n;
return n;
};
const std::string client = PipeStatsWindow::ReadWholeFile(PipeStatsWindow::LibraryLogPath());
const std::string server = PipeStatsWindow::ReadServerLogSince(PipeStatsWindow::LogMark{});
const std::string clientLine = "Refuse{AdoptT0Unavailable, \"no kCapAdoptT0\"}";
const std::string serverLine = std::string("Refuse{AdoptT0Unavailable, \"") + why + "\"}";
RecordProperty("t0_client_fallbacks", static_cast<int>(countOf(client, clientLine)));
RecordProperty("t0_server_refusals", static_cast<int>(countOf(server, serverLine)));
EXPECT_EQ(countOf(client, clientLine), 1u)
<< "the client names a T0 it asked for and cannot run exactly once, at its first map_persistent ("
<< clientLine << "), in " << PipeStatsWindow::LibraryLogPath();
EXPECT_EQ(countOf(server, serverLine), 1u)
<< "the server names why it did not grant T0 exactly once, at its first native bind (" << serverLine
<< "), in " << PipeStatsWindow::ServerLibraryLogPath();
EXPECT_EQ(client.find("Fatal{"), std::string::npos) << client;
EXPECT_EQ(server.find("Fatal{"), std::string::npos) << server;
}
} // namespace MGITest
@@ -0,0 +1,765 @@
// MobileGL - MobileGL/MG_IntegrationTest/Scenarios/OversizedRecordingSplitScenario.cpp
// 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
//
// Scenario - A FRAME WITH MORE DRAWS THAN ONE COMMAND BUFFER MAY HOLD IS SPLIT, AND LOSES NOTHING.
//
// DirectVulkan records a frame into one command buffer, and used to submit it only at Present.
// A driver keeps a command buffer's GPU memory until the buffer is freed, and the Adreno 830
// driver maps that memory into the process in 16 KiB chunks. Minecraft rd12's loading frame
// records 981,654 draws (3.1M commands) into one buffer. It grew to ~40k such mappings and
// aborted inside the driver's own calloc once vm.max_map_count (65530) was spent. The renderer
// now submits the open buffer after MOBILEGL_MAGMA_MAX_DRAWS_PER_COMMAND_BUFFER draws and goes
// on recording into a fresh one (VulkanRenderer::SplitOversizedRecording).
//
// The first case writes each pixel of a 32x32 target with its own draw and its own uniform
// value, so it pins that no draw is lost and no per-draw state is left behind on either side of
// a split. The rest put GL state that one draw leaves for a later one ACROSS the split: a depth
// occluder, a stencil mark, a blend destination, the samples of a multisample target, an
// occlusion or primitives-generated query that is still open, and a transform-feedback capture
// that is still active. Each draws its "before" part, then kFillers draws, then its "after"
// part. kFillers is more than twice the SplitRecording lane's budget of 64, so at least one
// split lands between the two parts wherever the recording's count stood.
//
// The ambient registrations run the same draws unsplit, because the default budget is far above
// any of these frames; that is the control. The DirectVulkan.SplitRecording. entry pins the
// budget to 64, and there every case also asserts the renderer's latched MGLOG_I - the one
// assertion that is red on a renderer that never splits.
#include <algorithm>
#include <cstdint>
#include <cstdlib>
#include <filesystem>
#include <fstream>
#include <iterator>
#include <sstream>
#include <string>
#include <vector>
#include "../Harness/HeadlessGL.h"
#include "../Harness/PipeStatsWindow.h"
#include "../Harness/ScenarioFixture.h"
#ifdef GLAPI
#undef GLAPI
#endif
#define GL_GLEXT_PROTOTYPES
#include <GL/gl.h>
#include <GL/glcorearb.h>
#undef GL_GLEXT_PROTOTYPES
namespace MGITest {
namespace {
constexpr int kSide = 32;
constexpr int kDraws = kSide * kSide;
// More than twice the SplitRecording lane's budget (64): wherever the count stands when
// they start, at least one split lands among them.
constexpr int kFillers = 150;
// Draws inside the query and capture spans: also more than twice the budget.
constexpr int kSpanDraws = 200;
// aPos spans the unit square; the uniform moves it onto cell (i % 32, i / 32) of a
// 32x32 grid, which is exactly one pixel of a 32x32 target.
constexpr const char* kVS = R"(#version 330 core
in vec2 aPos;
uniform int uIndex;
void main() {
vec2 cell = vec2(float(uIndex % 32), float(uIndex / 32));
gl_Position = vec4((cell + aPos) / 16.0 - 1.0, 0.0, 1.0);
}
)";
// Multiples of 8/255 survive an RGBA8 round trip exactly, so the readback bytes are
// (8x, 8y, 255, 255) for the pixel at (x, y) and nothing else can produce them.
constexpr const char* kFS = R"(#version 330 core
uniform int uIndex;
out vec4 o_color;
void main() {
o_color = vec4(float(uIndex % 32) * 8.0 / 255.0, float(uIndex / 32) * 8.0 / 255.0, 1.0, 1.0);
}
)";
// The state cases: the unit-square vertices (or a triangle of them) stretched over an
// NDC rectangle at a given depth, in a uniform colour. A zero rectangle makes a
// zero-area primitive - a draw that reaches the renderer and rasterizes nothing.
constexpr const char* kRectVS = R"(#version 330 core
in vec2 aPos;
uniform vec4 uRect;
uniform float uDepth;
void main() { gl_Position = vec4(mix(uRect.xy, uRect.zw, aPos), uDepth, 1.0); }
)";
constexpr const char* kRectFS = R"(#version 330 core
uniform vec4 uColor;
out vec4 o_color;
void main() { o_color = uColor; }
)";
// The capture case: one point per draw, carrying 2 * uIndex + 1.
constexpr const char* kCaptureVS = R"(#version 330 core
uniform int uIndex;
out float vs_value;
void main() {
vs_value = float(uIndex) * 2.0 + 1.0;
gl_Position = vec4(0.0, 0.0, 0.0, 1.0);
}
)";
constexpr const char* kCaptureFS = R"(#version 330 core
out vec4 o_color;
void main() { o_color = vec4(1.0); }
)";
constexpr float kCapturePoison = -12345.0f;
// The budget the process was launched with, or 0 when it is unset or not a number.
unsigned long PinnedDrawBudget() {
const char* value = std::getenv("MOBILEGL_MAGMA_MAX_DRAWS_PER_COMMAND_BUFFER");
if (value == nullptr || *value == '\0') return 0;
char* end = nullptr;
const unsigned long parsed = std::strtoul(value, &end, 10);
return (end != nullptr && *end == '\0') ? parsed : 0;
}
// The library log, for the arming assertion. MOBILEGL_LOG_FILE_PATH is read at log-init,
// every process in the lane appends to it, and only bytes appended after the snapshot
// count. P11 M2: ON A SPLIT BUILD IT IS A BASE NAME - the library writes one file per role
// (PipeStatsWindow.h) - and the latched line comes from the RENDERER, which under a
// transport is the server role: the apply thread under inproc, the spawned server process
// under spawn (it inherits this entry's environment, budget and log base included). So the
// mark and the read cover both roles, as PrimitivesGeneratedNoXfbScenario's arming does; a
// read of the literal base found no file and reported an unsplit renderer. In the pull
// build the client role is the whole log.
std::string LibraryLogPath() { return PipeStatsWindow::LibraryLogPath(); }
PipeStatsWindow::LogMark LibraryLogSize() { return PipeStatsWindow::MarkLaneLog(); }
std::string LibraryLogSince(const PipeStatsWindow::LogMark& mark) {
return PipeStatsWindow::ReadLaneLogSince(mark);
}
std::string Describe(const Rgba8& c) {
std::ostringstream os;
os << "rgba(" << static_cast<int>(c.r) << ", " << static_cast<int>(c.g) << ", " << static_cast<int>(c.b)
<< ", " << static_cast<int>(c.a) << ")";
return os.str();
}
::testing::AssertionResult EveryPixelHoldsItsOwnDraw(const Image& image) {
int wrong = 0;
int firstX = -1, firstY = -1;
Rgba8 firstSeen{};
for (int y = 0; y < kSide; ++y) {
for (int x = 0; x < kSide; ++x) {
const Rgba8 expected{static_cast<std::uint8_t>(8 * x), static_cast<std::uint8_t>(8 * y), 255, 255};
const Rgba8 seen = image.At(x, y);
if (seen != expected) {
if (wrong == 0) {
firstX = x;
firstY = y;
firstSeen = seen;
}
++wrong;
}
}
}
if (wrong == 0) {
return ::testing::AssertionSuccess();
}
return ::testing::AssertionFailure()
<< wrong << " of " << kDraws << " pixels do not hold their own draw's colour. First: (" << firstX
<< ", " << firstY << ") = " << firstSeen << ", drawn by draw " << (firstY * kSide + firstX)
<< " (a pixel no draw reached still holds the clear colour 0,0,0,0)";
}
// Every pixel of columns [x0, x1] (all rows) is `expected`, each channel within `tolerance`.
::testing::AssertionResult ColumnsAre(const Image& image, int x0, int x1, const Rgba8& expected,
int tolerance, const char* what) {
int wrong = 0;
int firstX = -1, firstY = -1;
Rgba8 firstSeen{};
for (int y = 0; y < kSide; ++y) {
for (int x = x0; x <= x1; ++x) {
const Rgba8 seen = image.At(x, y);
const bool close = std::abs(seen.r - expected.r) <= tolerance &&
std::abs(seen.g - expected.g) <= tolerance &&
std::abs(seen.b - expected.b) <= tolerance &&
std::abs(seen.a - expected.a) <= tolerance;
if (!close) {
if (wrong == 0) {
firstX = x;
firstY = y;
firstSeen = seen;
}
++wrong;
}
}
}
if (wrong == 0) return ::testing::AssertionSuccess();
return ::testing::AssertionFailure()
<< what << ": " << wrong << " pixels of columns " << x0 << ".." << x1 << " are not "
<< Describe(expected) << "; first (" << firstX << ", " << firstY << ") = " << Describe(firstSeen);
}
class OversizedRecordingSplitScenario : public ScenarioTest {
protected:
void SetUp() override {
ScenarioTest::SetUp();
if (!Ready()) return;
// Taken before the case draws anything, so the line the arming check looks for
// can only have been written by this case. The renderer latches it at its first
// split, and ctest gives every case a process of its own.
m_logBefore = LibraryLogSize();
// 0..3: the unit square as a strip. 4..6: its lower-left triangle. 7..9: its
// upper-right triangle - the two share the diagonal, so together they cover every
// sample of the square exactly once.
static const float kVertices[] = {0.0f, 0.0f, 1.0f, 0.0f, 0.0f, 1.0f, 1.0f, 1.0f, 0.0f, 0.0f,
1.0f, 0.0f, 0.0f, 1.0f, 1.0f, 0.0f, 1.0f, 1.0f, 0.0f, 1.0f};
glGenVertexArrays(1, &m_vao);
glBindVertexArray(m_vao);
glGenBuffers(1, &m_vbo);
glBindBuffer(GL_ARRAY_BUFFER, m_vbo);
glBufferData(GL_ARRAY_BUFFER, sizeof(kVertices), kVertices, GL_STATIC_DRAW);
glEnableVertexAttribArray(0);
glVertexAttribPointer(0, 2, GL_FLOAT, GL_FALSE, 2 * sizeof(float), nullptr);
}
void TearDown() override {
if (Ready()) {
glDisable(GL_DEPTH_TEST);
glDisable(GL_STENCIL_TEST);
glDisable(GL_BLEND);
glDisable(GL_RASTERIZER_DISCARD);
glDepthMask(GL_TRUE);
glStencilMask(0xFF);
glColorMask(GL_TRUE, GL_TRUE, GL_TRUE, GL_TRUE);
glUseProgram(0);
glBindVertexArray(0);
BindDefaultFramebuffer();
if (m_rectProgram != 0) glDeleteProgram(m_rectProgram);
if (m_vbo != 0) glDeleteBuffers(1, &m_vbo);
if (m_vao != 0) glDeleteVertexArrays(1, &m_vao);
for (GLuint rbo : m_renderbuffers) glDeleteRenderbuffers(1, &rbo);
for (GLuint fbo : m_framebuffers) glDeleteFramebuffers(1, &fbo);
for (ColorFbo& target : m_colorTargets) DestroyColorFbo(target);
}
ScenarioTest::TearDown();
}
// In the SplitRecording lane (budget pinned below what every case draws, DirectVulkan,
// a log to read), a split must have happened during this case. Elsewhere this case is
// the unsplit control and there is nothing to arm.
void ExpectTheRecordingWasSplitWhereTheBudgetIsPinned() {
const unsigned long budget = PinnedDrawBudget();
const bool splitExpected = budget > 0 && budget * 2 < static_cast<unsigned long>(kFillers) &&
Gl().BackendName() == std::string("DirectVulkan") &&
!LibraryLogPath().empty();
RecordProperty("arming_checked", splitExpected ? "yes" : "no");
if (!splitExpected) return;
const std::string appended = LibraryLogSince(m_logBefore);
EXPECT_NE(appended.find("draws into one command buffer; submitting it"), std::string::npos)
<< "MOBILEGL_MAGMA_MAX_DRAWS_PER_COMMAND_BUFFER=" << budget << " and this case recorded more "
<< "than " << kFillers << " draws in one frame, but DirectVulkan never reported splitting the "
"recording. A renderer that does not split grows a single command buffer without bound "
"(rd12 on Adreno: ~40k driver mappings, then SIGABRT). See "
"VulkanRenderer::SplitOversizedRecording. Log appended by this case:\n"
<< appended;
}
bool BuildRectProgram(std::string* error) {
m_rectProgram = CompileProgram(kRectVS, kRectFS, error);
if (m_rectProgram == 0) return false;
m_rectLoc = glGetUniformLocation(m_rectProgram, "uRect");
m_depthLoc = glGetUniformLocation(m_rectProgram, "uDepth");
m_colorLoc = glGetUniformLocation(m_rectProgram, "uColor");
glUseProgram(m_rectProgram);
glBindVertexArray(m_vao);
return true;
}
// The rectangle [x0, x1] x [y0, y1] in NDC; `first`/`count` pick the strip or a triangle.
void DrawRect(float x0, float y0, float x1, float y1, float depth, float r, float g, float b, float a,
GLenum mode = GL_TRIANGLE_STRIP, GLint first = 0, GLsizei count = 4) {
glUniform4f(m_rectLoc, x0, y0, x1, y1);
glUniform1f(m_depthLoc, depth);
glUniform4f(m_colorLoc, r, g, b, a);
glDrawArrays(mode, first, count);
}
// One pixel of the 32x32 grid, by index.
void DrawPixel(int index, float r, float g, float b, float a) {
const float x = static_cast<float>(index % kSide);
const float y = static_cast<float>(index / kSide);
DrawRect(x / 16.0f - 1.0f, y / 16.0f - 1.0f, (x + 1.0f) / 16.0f - 1.0f, (y + 1.0f) / 16.0f - 1.0f, 0.0f,
r, g, b, a);
}
// Draws that reach the renderer and rasterize nothing: zero-area strips.
void DrawFillers(int count) {
for (int i = 0; i < count; ++i) {
DrawRect(0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f);
}
}
// A kSide x kSide RGBA8 target, optionally with a DEPTH24_STENCIL8 renderbuffer.
// Returns fbo 0 when the driver will not complete it.
ColorFbo MakeTarget(bool withDepthStencil) {
ColorFbo target = MakeColorFbo(kSide, kSide);
if (target.fbo == 0) return target;
m_colorTargets.push_back(target);
if (withDepthStencil) {
GLuint rbo = 0;
glGenRenderbuffers(1, &rbo);
m_renderbuffers.push_back(rbo);
glBindRenderbuffer(GL_RENDERBUFFER, rbo);
glRenderbufferStorage(GL_RENDERBUFFER, GL_DEPTH24_STENCIL8, kSide, kSide);
glBindFramebuffer(GL_FRAMEBUFFER, target.fbo);
glFramebufferRenderbuffer(GL_FRAMEBUFFER, GL_DEPTH_STENCIL_ATTACHMENT, GL_RENDERBUFFER, rbo);
if (glCheckFramebufferStatus(GL_FRAMEBUFFER) != GL_FRAMEBUFFER_COMPLETE) {
target.fbo = 0;
}
}
return target;
}
Image ReadTarget(const ColorFbo& target) {
glBindFramebuffer(GL_FRAMEBUFFER, target.fbo);
return ReadPixels(kSide, kSide);
}
PipeStatsWindow::LogMark m_logBefore{};
GLuint m_vao = 0;
GLuint m_vbo = 0;
GLuint m_rectProgram = 0;
GLint m_rectLoc = -1;
GLint m_depthLoc = -1;
GLint m_colorLoc = -1;
std::vector<ColorFbo> m_colorTargets;
std::vector<GLuint> m_renderbuffers;
std::vector<GLuint> m_framebuffers;
};
} // namespace
// kDraws single-pixel quads into a fresh target, one uniform value per draw, with no
// readback, flush or rebind in between. Every pixel must hold its own draw's colour.
TEST_F(OversizedRecordingSplitScenario, EveryDrawOfTheFrameLandsAcrossCommandBufferSplits) {
if (!Ready()) return;
std::string error;
const unsigned int program = CompileProgram(kVS, kFS, &error);
ASSERT_NE(program, 0u) << error;
ColorFbo target = MakeTarget(false);
ASSERT_NE(target.fbo, 0u) << "MakeColorFbo failed";
BindFbo(target);
glDisable(GL_SCISSOR_TEST);
glDisable(GL_DEPTH_TEST);
glDisable(GL_BLEND);
ClearTo(0.0f, 0.0f, 0.0f, 0.0f);
glUseProgram(program);
glBindVertexArray(m_vao);
const GLint indexLocation = glGetUniformLocation(program, "uIndex");
for (int i = 0; i < kDraws; ++i) {
glUniform1i(indexLocation, i);
glDrawArrays(GL_TRIANGLE_STRIP, 0, 4);
}
const Image image = ReadPixels(kSide, kSide);
glUseProgram(0);
glDeleteProgram(program);
EXPECT_TRUE(EveryPixelHoldsItsOwnDraw(image));
EXPECT_EQ(FirstGLError(), 0u);
ExpectTheRecordingWasSplitWhereTheBudgetIsPinned();
}
// (a) An occluder drawn before the split must still fail the depth test of a draw after it:
// the depth attachment's contents have to survive the render pass ending and re-beginning.
TEST_F(OversizedRecordingSplitScenario, ADepthOccluderDrawnBeforeTheSplitHidesADrawAfterIt) {
if (!Ready()) return;
std::string error;
ASSERT_TRUE(BuildRectProgram(&error)) << error;
ColorFbo target = MakeTarget(true);
if (target.fbo == 0) GTEST_SKIP() << "no complete RGBA8 + DEPTH24_STENCIL8 target on this driver";
BindFbo(target);
glClearDepth(1.0);
ClearTo(0.0f, 0.0f, 0.0f, 1.0f);
glEnable(GL_DEPTH_TEST);
glDepthFunc(GL_LESS);
glDepthMask(GL_TRUE);
DrawRect(-1.0f, -1.0f, 0.0f, 1.0f, -0.5f, 1.0f, 0.0f, 0.0f, 1.0f); // left half, near, red
DrawFillers(kFillers);
DrawRect(-1.0f, -1.0f, 1.0f, 1.0f, 0.5f, 0.0f, 1.0f, 0.0f, 1.0f); // everything, far, green
const Image image = ReadTarget(target);
EXPECT_TRUE(ColumnsAre(image, 0, kSide / 2 - 1, Rgba8{255, 0, 0, 255}, 0,
"the near occluder drawn before the split must hide the far draw after it"));
EXPECT_TRUE(ColumnsAre(image, kSide / 2, kSide - 1, Rgba8{0, 255, 0, 255}, 0,
"where nothing occludes, the far draw after the split must land"));
EXPECT_EQ(FirstGLError(), 0u);
ExpectTheRecordingWasSplitWhereTheBudgetIsPinned();
}
// (b) A stencil mark made before the split must gate a draw after it.
TEST_F(OversizedRecordingSplitScenario, AStencilMarkMadeBeforeTheSplitGatesADrawAfterIt) {
if (!Ready()) return;
std::string error;
ASSERT_TRUE(BuildRectProgram(&error)) << error;
ColorFbo target = MakeTarget(true);
if (target.fbo == 0) GTEST_SKIP() << "no complete RGBA8 + DEPTH24_STENCIL8 target on this driver";
BindFbo(target);
glDisable(GL_DEPTH_TEST);
glStencilMask(0xFF);
glClearStencil(0);
glClear(GL_STENCIL_BUFFER_BIT);
ClearTo(0.0f, 0.0f, 0.0f, 1.0f);
glEnable(GL_STENCIL_TEST);
glStencilFunc(GL_ALWAYS, 1, 0xFF);
glStencilOp(GL_KEEP, GL_KEEP, GL_REPLACE);
DrawRect(-1.0f, -1.0f, 0.0f, 1.0f, 0.0f, 0.0f, 0.0f, 1.0f, 1.0f); // left half: stencil 1, blue
glStencilFunc(GL_EQUAL, 1, 0xFF);
glStencilOp(GL_KEEP, GL_KEEP, GL_KEEP);
DrawFillers(kFillers);
DrawRect(-1.0f, -1.0f, 1.0f, 1.0f, 0.0f, 0.0f, 1.0f, 0.0f, 1.0f); // everything, green, where stencil == 1
const Image image = ReadTarget(target);
EXPECT_TRUE(ColumnsAre(image, 0, kSide / 2 - 1, Rgba8{0, 255, 0, 255}, 0,
"where the mark was made before the split, the draw after it must pass"));
EXPECT_TRUE(ColumnsAre(image, kSide / 2, kSide - 1, Rgba8{0, 0, 0, 255}, 0,
"where no mark was made, the draw after the split must be stencilled out"));
EXPECT_EQ(FirstGLError(), 0u);
ExpectTheRecordingWasSplitWhereTheBudgetIsPinned();
}
// (c) Additive blending onto pixels written before the split: every one of kSpanDraws
// full-target draws adds exactly 1/255 of red onto a target whose green was written first,
// so the result counts every draw on both sides of every split.
TEST_F(OversizedRecordingSplitScenario, BlendingAccumulatesOntoPixelsWrittenBeforeTheSplit) {
if (!Ready()) return;
std::string error;
ASSERT_TRUE(BuildRectProgram(&error)) << error;
ColorFbo target = MakeTarget(false);
ASSERT_NE(target.fbo, 0u) << "MakeColorFbo failed";
BindFbo(target);
glDisable(GL_DEPTH_TEST);
ClearTo(0.0f, 0.0f, 0.0f, 0.0f);
glEnable(GL_BLEND);
glBlendEquation(GL_FUNC_ADD);
glBlendFunc(GL_ONE, GL_ONE);
DrawRect(-1.0f, -1.0f, 1.0f, 1.0f, 0.0f, 0.0f, 55.0f / 255.0f, 0.0f, 0.0f);
for (int i = 0; i < kSpanDraws; ++i) {
DrawRect(-1.0f, -1.0f, 1.0f, 1.0f, 0.0f, 1.0f / 255.0f, 0.0f, 0.0f, 0.0f);
}
const Image image = ReadTarget(target);
EXPECT_TRUE(ColumnsAre(image, 0, kSide - 1, Rgba8{static_cast<std::uint8_t>(kSpanDraws), 55, 0, 0}, 1,
"every additive draw, and the green written before any split, must be in the sum"));
EXPECT_EQ(FirstGLError(), 0u);
ExpectTheRecordingWasSplitWhereTheBudgetIsPinned();
}
// (d) A multisample target drawn on both sides of the split, then resolved. Before the
// split a triangle covers the lower-left half of the target; after it, the complementary
// triangle covers the upper-right half. On the diagonal each pixel's samples are shared
// between the two, so only a target that kept every SAMPLE across the split resolves them to
// red + green = 255. One that was resolved or re-broadcast at the split gives about 190
// there, and one whose samples were dropped gives black or garbage.
TEST_F(OversizedRecordingSplitScenario, AMultisampleTargetKeepsItsSamplesAcrossTheSplit) {
if (!Ready()) return;
std::string error;
ASSERT_TRUE(BuildRectProgram(&error)) << error;
GLint maxSamples = 0;
glGetIntegerv(GL_MAX_SAMPLES, &maxSamples);
if (maxSamples < 2) {
GTEST_SKIP() << "GL_MAX_SAMPLES is " << maxSamples << "; this needs a multisample renderbuffer";
}
const GLsizei samples = std::min<GLint>(4, maxSamples);
GLuint msFbo = 0, msRbo = 0;
glGenFramebuffers(1, &msFbo);
m_framebuffers.push_back(msFbo);
glGenRenderbuffers(1, &msRbo);
m_renderbuffers.push_back(msRbo);
glBindRenderbuffer(GL_RENDERBUFFER, msRbo);
glRenderbufferStorageMultisample(GL_RENDERBUFFER, samples, GL_RGBA8, kSide, kSide);
glBindFramebuffer(GL_FRAMEBUFFER, msFbo);
glFramebufferRenderbuffer(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_RENDERBUFFER, msRbo);
if (glCheckFramebufferStatus(GL_FRAMEBUFFER) != GL_FRAMEBUFFER_COMPLETE) {
GTEST_SKIP() << "no complete " << samples << "x multisample RGBA8 renderbuffer on this driver";
}
ColorFbo resolved = MakeTarget(false);
ASSERT_NE(resolved.fbo, 0u) << "MakeColorFbo failed";
glBindFramebuffer(GL_FRAMEBUFFER, msFbo);
glViewport(0, 0, kSide, kSide);
glDisable(GL_DEPTH_TEST);
ClearTo(0.0f, 0.0f, 0.0f, 1.0f);
DrawRect(-1.0f, -1.0f, 1.0f, 1.0f, 0.0f, 1.0f, 0.0f, 0.0f, 1.0f, GL_TRIANGLES, 4, 3); // lower-left, red
DrawFillers(kFillers);
DrawRect(-1.0f, -1.0f, 1.0f, 1.0f, 0.0f, 0.0f, 1.0f, 0.0f, 1.0f, GL_TRIANGLES, 7, 3); // upper-right, green
glBindFramebuffer(GL_READ_FRAMEBUFFER, msFbo);
glBindFramebuffer(GL_DRAW_FRAMEBUFFER, resolved.fbo);
glBlitFramebuffer(0, 0, kSide, kSide, 0, 0, kSide, kSide, GL_COLOR_BUFFER_BIT, GL_NEAREST);
const Image image = ReadTarget(resolved);
int wrong = 0;
std::string first;
for (int y = 0; y < kSide && wrong < 1000; ++y) {
for (int x = 0; x < kSide; ++x) {
const Rgba8 seen = image.At(x, y);
bool ok = false;
// The diagonal x + y = kSide in pixel units passes through the pixels whose
// x + y == kSide - 1; every other pixel lies wholly on one side of it.
if (x + y < kSide - 1) {
ok = seen == Rgba8{255, 0, 0, 255};
} else if (x + y > kSide - 1) {
ok = seen == Rgba8{0, 255, 0, 255};
} else {
ok = seen.b == 0 && seen.a == 255 && seen.r > 0 && seen.g > 0 &&
std::abs(static_cast<int>(seen.r) + static_cast<int>(seen.g) - 255) <= 3;
}
if (!ok) {
if (wrong == 0) {
std::ostringstream os;
os << "(" << x << ", " << y << ") = " << Describe(seen);
first = os.str();
}
++wrong;
}
}
}
EXPECT_EQ(wrong, 0) << wrong << " resolved pixels are wrong; first " << first
<< ". Lower-left of the diagonal must be red, upper-right green, and a pixel on it "
"red + green = 255 with both present (its samples were split between a draw before "
"the command-buffer split and one after it)";
EXPECT_EQ(FirstGLError(), 0u);
ExpectTheRecordingWasSplitWhereTheBudgetIsPinned();
}
// (e) Occlusion queries open across the split. GL_SAMPLES_PASSED counts kSpanDraws
// single-pixel draws on both sides of it; GL_ANY_SAMPLES_PASSED is answered by the one draw
// made before the split, the fillers after it pass nothing - with a fillers-only control
// that must answer false.
TEST_F(OversizedRecordingSplitScenario, OcclusionQueriesOpenAcrossTheSplitCountBothSides) {
if (!Ready()) return;
std::string error;
ASSERT_TRUE(BuildRectProgram(&error)) << error;
ColorFbo target = MakeTarget(false);
ASSERT_NE(target.fbo, 0u) << "MakeColorFbo failed";
BindFbo(target);
glDisable(GL_DEPTH_TEST);
ClearTo(0.0f, 0.0f, 0.0f, 1.0f);
GLuint queries[3] = {};
glGenQueries(3, queries);
FirstGLError();
// Any samples: control first (fillers only), then one real pixel before the split.
glBeginQuery(GL_ANY_SAMPLES_PASSED, queries[0]);
const unsigned int anyBeginError = FirstGLError();
if (anyBeginError != 0u) {
glDeleteQueries(3, queries);
GTEST_SKIP() << Gl().BackendName() << " does not accept GL_ANY_SAMPLES_PASSED ("
<< GLErrorName(anyBeginError) << ")";
}
DrawFillers(kFillers);
glEndQuery(GL_ANY_SAMPLES_PASSED);
glBeginQuery(GL_ANY_SAMPLES_PASSED, queries[1]);
DrawPixel(0, 1.0f, 1.0f, 1.0f, 1.0f);
DrawFillers(kFillers);
glEndQuery(GL_ANY_SAMPLES_PASSED);
GLuint anyControl = 7, anyAcross = 7;
glGetQueryObjectuiv(queries[0], GL_QUERY_RESULT, &anyControl);
glGetQueryObjectuiv(queries[1], GL_QUERY_RESULT, &anyAcross);
EXPECT_EQ(anyControl, static_cast<GLuint>(GL_FALSE)) << "fillers alone rasterize nothing";
EXPECT_EQ(anyAcross, static_cast<GLuint>(GL_TRUE))
<< "the only passing draw was made before the split; the query that spans it lost that answer";
// Samples passed: kSpanDraws distinct single-sample pixels, spanning at least one split.
glBeginQuery(GL_SAMPLES_PASSED, queries[2]);
const unsigned int samplesBeginError = FirstGLError();
if (samplesBeginError == 0u) {
for (int i = 0; i < kSpanDraws; ++i) {
DrawPixel(i, 1.0f, 1.0f, 1.0f, 1.0f);
}
glEndQuery(GL_SAMPLES_PASSED);
GLuint samplesPassed = 0;
glGetQueryObjectuiv(queries[2], GL_QUERY_RESULT, &samplesPassed);
if (Gl().BackendName() == std::string("DirectGLES")) {
// OpenGL ES has no sample-counting occlusion query, only ANY_SAMPLES_PASSED*, so
// DirectGLES answers GL_SAMPLES_PASSED with a 0/1 approximation (DirectGLES.cpp,
// the occlusion-query result path). The exact count is not this backend's to give.
RecordProperty("samples_passed", "0/1 approximation on OpenGL ES; exact count not checked");
EXPECT_EQ(samplesPassed, 1u) << "samples passed, so the approximation must read 1";
} else {
EXPECT_EQ(samplesPassed, static_cast<GLuint>(kSpanDraws))
<< "each of the " << kSpanDraws << " draws covers exactly one single-sample pixel";
}
} else {
RecordProperty("samples_passed", std::string("not accepted: ") + GLErrorName(samplesBeginError));
}
glDeleteQueries(3, queries);
EXPECT_EQ(FirstGLError(), 0u);
ExpectTheRecordingWasSplitWhereTheBudgetIsPinned();
}
// (e) GL_PRIMITIVES_GENERATED open across the split: kSpanDraws strips of two triangles.
// A one-draw query first establishes that this stack counts at all.
TEST_F(OversizedRecordingSplitScenario, APrimitivesGeneratedQueryOpenAcrossTheSplitCountsBothSides) {
if (!Ready()) return;
std::string error;
ASSERT_TRUE(BuildRectProgram(&error)) << error;
ColorFbo target = MakeTarget(false);
ASSERT_NE(target.fbo, 0u) << "MakeColorFbo failed";
BindFbo(target);
glDisable(GL_DEPTH_TEST);
ClearTo(0.0f, 0.0f, 0.0f, 1.0f);
GLuint queries[2] = {};
glGenQueries(2, queries);
FirstGLError();
glBeginQuery(GL_PRIMITIVES_GENERATED, queries[0]);
const unsigned int beginError = FirstGLError();
if (beginError != 0u) {
glDeleteQueries(2, queries);
GTEST_SKIP() << Gl().BackendName() << " does not accept GL_PRIMITIVES_GENERATED ("
<< GLErrorName(beginError) << ")";
}
DrawPixel(0, 1.0f, 1.0f, 1.0f, 1.0f);
glEndQuery(GL_PRIMITIVES_GENERATED);
GLuint control = 0;
glGetQueryObjectuiv(queries[0], GL_QUERY_RESULT, &control);
if (control == 0u) {
glDeleteQueries(2, queries);
GTEST_SKIP() << "GL_PRIMITIVES_GENERATED reads 0 for a single unsplit strip on " << Gl().BackendName()
<< " (" << Gl().RendererString() << "): nothing this stack counts, so nothing to carry";
}
ASSERT_EQ(control, 2u) << "one strip of four vertices is two triangles";
glBeginQuery(GL_PRIMITIVES_GENERATED, queries[1]);
for (int i = 0; i < kSpanDraws; ++i) {
DrawPixel(i, 1.0f, 1.0f, 1.0f, 1.0f);
}
glEndQuery(GL_PRIMITIVES_GENERATED);
GLuint generated = 0;
glGetQueryObjectuiv(queries[1], GL_QUERY_RESULT, &generated);
EXPECT_EQ(generated, static_cast<GLuint>(2 * kSpanDraws))
<< kSpanDraws << " strips of two triangles each, drawn across at least one split";
glDeleteQueries(2, queries);
EXPECT_EQ(FirstGLError(), 0u);
ExpectTheRecordingWasSplitWhereTheBudgetIsPinned();
}
// (f) Transform feedback active across the split: kSpanDraws one-point draws, draw i
// capturing 2i + 1, must append in order into one buffer with nothing past the end, and
// GL_TRANSFORM_FEEDBACK_PRIMITIVES_WRITTEN must count every one.
TEST_F(OversizedRecordingSplitScenario, ATransformFeedbackCaptureActiveAcrossTheSplitAppendsEveryDraw) {
if (!Ready()) return;
auto compile = [](GLenum stage, const char* source, std::string* log) -> GLuint {
const GLuint shader = glCreateShader(stage);
glShaderSource(shader, 1, &source, nullptr);
glCompileShader(shader);
GLint ok = 0;
glGetShaderiv(shader, GL_COMPILE_STATUS, &ok);
if (!ok) {
char buffer[2048] = {};
glGetShaderInfoLog(shader, sizeof(buffer) - 1, nullptr, buffer);
*log = buffer;
glDeleteShader(shader);
return 0;
}
return shader;
};
std::string log;
const GLuint vs = compile(GL_VERTEX_SHADER, kCaptureVS, &log);
ASSERT_NE(vs, 0u) << log;
const GLuint fs = compile(GL_FRAGMENT_SHADER, kCaptureFS, &log);
ASSERT_NE(fs, 0u) << log;
const GLuint program = glCreateProgram();
glAttachShader(program, vs);
glAttachShader(program, fs);
const char* varyings[] = {"vs_value"};
glTransformFeedbackVaryings(program, 1, varyings, GL_INTERLEAVED_ATTRIBS);
glLinkProgram(program);
glDeleteShader(vs);
glDeleteShader(fs);
GLint linked = 0;
glGetProgramiv(program, GL_LINK_STATUS, &linked);
if (!linked) {
char buffer[2048] = {};
glGetProgramInfoLog(program, sizeof(buffer) - 1, nullptr, buffer);
glDeleteProgram(program);
FAIL() << "capture program failed to link: " << buffer;
}
constexpr int kSlack = 4;
std::vector<float> poison(kSpanDraws + kSlack, kCapturePoison);
GLuint buffer = 0;
glGenBuffers(1, &buffer);
glBindBuffer(GL_TRANSFORM_FEEDBACK_BUFFER, buffer);
glBufferData(GL_TRANSFORM_FEEDBACK_BUFFER, static_cast<GLsizeiptr>(poison.size() * sizeof(float)),
poison.data(), GL_STATIC_COPY);
glBindBufferBase(GL_TRANSFORM_FEEDBACK_BUFFER, 0, buffer);
GLuint written = 0;
glGenQueries(1, &written);
glUseProgram(program);
glBindVertexArray(m_vao);
const GLint indexLocation = glGetUniformLocation(program, "uIndex");
glEnable(GL_RASTERIZER_DISCARD);
glBeginQuery(GL_TRANSFORM_FEEDBACK_PRIMITIVES_WRITTEN, written);
glBeginTransformFeedback(GL_POINTS);
for (int i = 0; i < kSpanDraws; ++i) {
glUniform1i(indexLocation, i);
glDrawArrays(GL_POINTS, 0, 1);
}
glEndTransformFeedback();
glEndQuery(GL_TRANSFORM_FEEDBACK_PRIMITIVES_WRITTEN);
glDisable(GL_RASTERIZER_DISCARD);
glUseProgram(0);
GLuint primitivesWritten = 0;
glGetQueryObjectuiv(written, GL_QUERY_RESULT, &primitivesWritten);
std::vector<float> captured(poison.size(), 0.0f);
glBindBuffer(GL_TRANSFORM_FEEDBACK_BUFFER, buffer);
glGetBufferSubData(GL_TRANSFORM_FEEDBACK_BUFFER, 0, static_cast<GLsizeiptr>(captured.size() * sizeof(float)),
captured.data());
glBindBufferBase(GL_TRANSFORM_FEEDBACK_BUFFER, 0, 0);
glBindBuffer(GL_TRANSFORM_FEEDBACK_BUFFER, 0);
glDeleteQueries(1, &written);
glDeleteBuffers(1, &buffer);
glDeleteProgram(program);
EXPECT_EQ(primitivesWritten, static_cast<GLuint>(kSpanDraws));
int wrong = 0;
std::string first;
for (int i = 0; i < static_cast<int>(captured.size()); ++i) {
const float expected = i < kSpanDraws ? static_cast<float>(2 * i + 1) : kCapturePoison;
if (captured[i] != expected) {
if (wrong == 0) {
std::ostringstream os;
os << "record " << i << " = " << captured[i] << ", expected " << expected;
first = os.str();
}
++wrong;
}
}
EXPECT_EQ(wrong, 0) << wrong << " of " << captured.size() << " records are wrong; first " << first
<< " (records past " << kSpanDraws << " must keep the poison)";
EXPECT_EQ(FirstGLError(), 0u);
ExpectTheRecordingWasSplitWhereTheBudgetIsPinned();
}
} // namespace MGITest
@@ -33,7 +33,7 @@
// must see it too.
// M-A Nothing produced kMGPipeBindSampler / kMGPipeBindShaderImage, so ImageBindableHint was
// dead: the applier never saw a texture become image-bound, the metadata respecify
// (ID-18 M4) had no live trigger, and the remint pull the hint exists to prevent was
// (ID-18 M4) had no live trigger, and the re-mint the hint exists to prevent was
// neither prevented nor counted. The case here reads the applier's record around a
// glBindImageTexture: the hint arrives as a metadata update that keeps the pending upload
// standing beside it, and the picture after the transition is the texels that upload
@@ -537,9 +537,12 @@ void main() { oColor = texture(uTex, vUv); }
return;
}
// THE NUMBER ROADMAP OPEN QUESTION 2 ASKS FOR: a texture Espryt allocated BEFORE the
// hint reached it is re-minted image-bindable at the bind and its levels replayed
// from the client's shadow - one remint pull, counted. Arming the counter here is
// THE NUMBER ROADMAP OPEN QUESTION 2 ASKS FOR, on a texture Espryt allocated BEFORE the
// hint reached it. An immutable RGBA8 allocation is already what an image unit needs,
// so neither arm re-mints it and the counter does not move: the server has kept it
// since P9 W2, and the monolith re-mint has had the same keep path since dev's
// 568090f0 (merged in P11 M), where it used to re-mint from the shadow and count one.
// A counted re-mint is TextureRemintPullScenario's A and H. Arming the counter here is
// what makes it readable without a stats-enabled lane.
unsigned long long pullsBefore = 0;
const bool pullsReadable = PeekPipeStatsTextureRemintPulls(&pullsBefore);
@@ -580,11 +583,10 @@ void main() { oColor = texture(uTex, vUv); }
// THE TRANSITION. An immutable texture has no storage-defining respecify left, so the
// hint can only arrive as a metadata update (ID-18 M4). Espryt syncs the texture
// eagerly inside glBindImageTexture and the widening re-mints its storage, replaying
// every defined level from the shadow (the remint pull the counter below counts), so
// the standing upload is consumed by that regeneration here and the picture that
// follows is blue whatever the metadata respecify did to the record - the KEPT
// property is proved further down, on a texture no remint stands in front of.
// eagerly inside glBindImageTexture and KEEPS the allocation (above), so the standing
// upload is consumed by the kept storage's own sync and the picture that follows is
// blue - the KEPT property of the record is proved further down, on a texture that is
// image-bindable already.
(void)uploadsBeforeBind;
(void)uploadsReadable;
glBindImageTexture(0, texture, 0, GL_FALSE, 0, GL_READ_ONLY, GL_RGBA8);
@@ -608,14 +610,10 @@ void main() { oColor = texture(uTex, vUv); }
const auto runtime = PeekSplitRuntime();
if (runtime.transportResolved && runtime.transportName == "inproc") {
ASSERT_TRUE(runtime.sessionActive);
EXPECT_EQ(pullsAfter, pullsBefore)
<< "the server must preserve this already immutable RGBA8 allocation; "
"image binding needs no remint pull";
} else {
EXPECT_EQ(pullsAfter, pullsBefore + 1)
<< "the monolith re-mint of a texture allocated before its hint was not counted "
"as a remint pull (trp= on the stats line is ROADMAP open question 2's number)";
}
EXPECT_EQ(pullsAfter, pullsBefore)
<< "an already immutable RGBA8 allocation must be kept on every arm; image binding "
"needs no re-mint (trp= on the stats line is ROADMAP open question 2's number)";
}
// THE PREVENTION HALF, measured the other way round: a texture whose hint arrives at
@@ -0,0 +1,454 @@
// MobileGL - MobileGL/MG_IntegrationTest/Scenarios/PackBufferReadbackScenario.cpp
// 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
//
// P9 (W1, MG_Remote/CONTRACT-P9.md §1): READS INTO A PIXEL PACK BUFFER ARE ANSWERED BY NOTHING.
//
// Under a transport a glReadPixels / glGetTexImage whose destination is a bound
// GL_PIXEL_PACK_BUFFER no longer waits for its pixels: it crosses as read_pixels_to_buffer /
// get_texture_image_to_buffer, the server lands the rows in the buffer at the client's PACK
// layout, and the client marks the buffer GPU-written so that the next CPU read of it - a
// glGetBufferSubData, a glMapBufferRange, or a texture upload that uses it as the UNPACK source -
// fetches the bytes through resource_readback.
//
// THE CASES ARE ORDINARY GL AND RUN ON THE MONOLITH ARMS TOO. Each one compares the pack buffer
// against the same read into client memory under the same pack state, both destinations
// pre-filled with the same byte, so the padding the pack state skips has to survive as well: the
// monolith and every split arm must agree with themselves, and therefore with each other. The
// one exception is the last case, which is about the WIRE (the read posted no reply and marked
// the buffer) and skips where there is no wire to ask.
//
// Every image is POSITION-DEPENDENT, so a row landed at the wrong stride, a band at the wrong
// offset or a byte swapped the wrong way cannot look right.
#include <array>
#include <cstddef>
#include <cstdint>
#include <cstring>
#include <string>
#include <vector>
#include "../Harness/HeadlessGL.h"
#include "../Harness/ScenarioFixture.h"
#include "../Harness/SplitRuntimePeek.h"
#ifdef GLAPI
#undef GLAPI
#endif
#define GL_GLEXT_PROTOTYPES
#include <GL/gl.h>
#include <GL/glcorearb.h>
#undef GL_GLEXT_PROTOTYPES
namespace MGITest {
namespace {
constexpr GLubyte kFill = 0xAB;
// A texel that names its own position (and its layer), so nothing misplaced can match.
std::array<GLubyte, 4> PatternRgba8(int x, int y, int layer) {
return {static_cast<GLubyte>(x * 7 + layer * 31), static_cast<GLubyte>(y * 11 + layer),
static_cast<GLubyte>((x ^ y) + layer * 5), static_cast<GLubyte>(0x80 | (x + y))};
}
// Integers below 2^24 in every component, so float storage is exact and a byte swap of
// any of them is visibly different from the value.
std::array<GLfloat, 4> PatternRgba32f(int x, int y) {
return {static_cast<GLfloat>(x + 1), static_cast<GLfloat>(y + 3),
static_cast<GLfloat>(x * 64 + y), static_cast<GLfloat>(1000 + x * y)};
}
struct PackState {
const char* name;
GLint alignment;
GLint rowLength;
GLint skipPixels;
GLint skipRows;
GLboolean swapBytes;
};
// Neutral, then each PACK parameter that changes the layout, then all of them at once.
constexpr PackState kPackStates[] = {
{"neutral", 4, 0, 0, 0, GL_FALSE},
{"alignment 1", 1, 0, 0, 0, GL_FALSE},
{"alignment 8", 8, 0, 0, 0, GL_FALSE},
{"row length 53", 4, 53, 0, 0, GL_FALSE},
{"skip pixels 3, skip rows 2", 4, 0, 3, 2, GL_FALSE},
{"everything", 8, 47, 5, 3, GL_FALSE},
};
class PackBufferReadbackScenario : public ScenarioTest {
protected:
void SetUp() override {
ScenarioTest::SetUp();
if (!Ready()) return;
DrainErrors();
glGenFramebuffers(1, &m_fbo);
glBindFramebuffer(GL_FRAMEBUFFER, m_fbo);
glDisable(GL_SCISSOR_TEST);
ResetPack();
}
void TearDown() override {
if (!Ready()) return;
ResetPack();
glBindBuffer(GL_PIXEL_PACK_BUFFER, 0);
glBindBuffer(GL_PIXEL_UNPACK_BUFFER, 0);
for (GLuint buffer : m_buffers) glDeleteBuffers(1, &buffer);
glBindFramebuffer(GL_FRAMEBUFFER, 0);
if (m_fbo != 0) glDeleteFramebuffers(1, &m_fbo);
for (GLuint texture : m_textures) glDeleteTextures(1, &texture);
m_buffers.clear();
m_textures.clear();
m_fbo = 0;
DrainErrors();
ScenarioTest::TearDown();
}
static void ResetPack() {
glPixelStorei(GL_PACK_ALIGNMENT, 4);
glPixelStorei(GL_PACK_ROW_LENGTH, 0);
glPixelStorei(GL_PACK_SKIP_PIXELS, 0);
glPixelStorei(GL_PACK_SKIP_ROWS, 0);
glPixelStorei(GL_PACK_IMAGE_HEIGHT, 0);
glPixelStorei(GL_PACK_SKIP_IMAGES, 0);
glPixelStorei(GL_PACK_SWAP_BYTES, GL_FALSE);
glPixelStorei(GL_UNPACK_ALIGNMENT, 4);
glPixelStorei(GL_UNPACK_ROW_LENGTH, 0);
}
static void ApplyPack(const PackState& pack) {
glPixelStorei(GL_PACK_ALIGNMENT, pack.alignment);
glPixelStorei(GL_PACK_ROW_LENGTH, pack.rowLength);
glPixelStorei(GL_PACK_SKIP_PIXELS, pack.skipPixels);
glPixelStorei(GL_PACK_SKIP_ROWS, pack.skipRows);
glPixelStorei(GL_PACK_SWAP_BYTES, pack.swapBytes);
}
static void DrainErrors() {
for (int i = 0; i < 16 && glGetError() != GL_NO_ERROR; ++i) {
}
}
// The bytes a pack state makes a width x height read occupy, from the first byte the
// read may touch (GL 4.6 §8.4.4.1 / §18.2): the skips, then the padded rows, the last
// row tight.
static std::size_t PackedExtent(const PackState& pack, int width, int height, std::size_t bpp) {
const std::size_t rowPixels = pack.rowLength > 0 ? static_cast<std::size_t>(pack.rowLength)
: static_cast<std::size_t>(width);
const std::size_t align = static_cast<std::size_t>(pack.alignment);
const std::size_t stride = (rowPixels * bpp + align - 1) / align * align;
return static_cast<std::size_t>(pack.skipRows) * stride + static_cast<std::size_t>(pack.skipPixels) * bpp +
static_cast<std::size_t>(height - 1) * stride + static_cast<std::size_t>(width) * bpp;
}
GLuint NewTexture() {
GLuint texture = 0;
glGenTextures(1, &texture);
m_textures.push_back(texture);
return texture;
}
GLuint NewBuffer(GLenum target, std::size_t size, GLubyte fill) {
GLuint buffer = 0;
glGenBuffers(1, &buffer);
m_buffers.push_back(buffer);
glBindBuffer(target, buffer);
const std::vector<GLubyte> initial(size, fill);
glBufferData(target, static_cast<GLsizeiptr>(size), initial.data(), GL_DYNAMIC_READ);
return buffer;
}
// An RGBA8 texture holding the pattern, attached as COLOR_ATTACHMENT0 for reading.
GLuint AttachPatternRgba8(int width, int height) {
std::vector<GLubyte> texels(static_cast<std::size_t>(width) * height * 4);
for (int y = 0; y < height; ++y)
for (int x = 0; x < width; ++x) {
const auto t = PatternRgba8(x, y, 0);
std::memcpy(texels.data() + (static_cast<std::size_t>(y) * width + x) * 4, t.data(), 4);
}
const GLuint texture = NewTexture();
glBindTexture(GL_TEXTURE_2D, texture);
glTexStorage2D(GL_TEXTURE_2D, 1, GL_RGBA8, width, height);
glTexSubImage2D(GL_TEXTURE_2D, 0, 0, 0, width, height, GL_RGBA, GL_UNSIGNED_BYTE, texels.data());
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
glBindFramebuffer(GL_FRAMEBUFFER, m_fbo);
glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_2D, texture, 0);
glReadBuffer(GL_COLOR_ATTACHMENT0);
EXPECT_EQ(glCheckFramebufferStatus(GL_FRAMEBUFFER), static_cast<GLenum>(GL_FRAMEBUFFER_COMPLETE));
EXPECT_EQ(FirstGLError(), static_cast<GLenum>(GL_NO_ERROR)) << "pattern upload";
return texture;
}
// glReadPixels of (x, y, width, height) under `pack` into client memory and into a
// pack buffer at `offset`, both pre-filled with kFill; the buffer's bytes are then
// read back by `readBack` and the two must be identical, byte for byte, padding
// included - and the bytes in front of `offset` must still be kFill.
template <typename ReadBack>
void ExpectPackBufferMatchesClientMemory(const PackState& pack, int x, int y, int width, int height,
GLenum type, std::size_t bpp, ReadBack&& readBack) {
ApplyPack(pack);
const std::size_t extent = PackedExtent(pack, width, height, bpp);
std::vector<GLubyte> host(extent, kFill);
glReadPixels(x, y, width, height, GL_RGBA, type, host.data());
ASSERT_EQ(FirstGLError(), static_cast<GLenum>(GL_NO_ERROR)) << pack.name << ": client-memory read";
constexpr std::size_t kOffset = 64;
NewBuffer(GL_PIXEL_PACK_BUFFER, kOffset + extent, kFill);
glReadPixels(x, y, width, height, GL_RGBA, type, reinterpret_cast<void*>(kOffset));
ASSERT_EQ(FirstGLError(), static_cast<GLenum>(GL_NO_ERROR)) << pack.name << ": pack-buffer read";
std::vector<GLubyte> actual(kOffset + extent, 0);
readBack(actual);
ASSERT_EQ(FirstGLError(), static_cast<GLenum>(GL_NO_ERROR)) << pack.name << ": buffer read-back";
glBindBuffer(GL_PIXEL_PACK_BUFFER, 0);
ResetPack();
std::size_t lead = 0;
for (std::size_t i = 0; i < kOffset; ++i) lead += actual[i] != kFill ? 1 : 0;
EXPECT_EQ(lead, 0u) << pack.name << ": " << lead << " bytes in front of the read's offset moved";
std::size_t bad = 0, first = 0;
for (std::size_t i = 0; i < extent; ++i) {
if (actual[kOffset + i] != host[i] && bad++ == 0) first = i;
}
EXPECT_EQ(bad, 0u) << pack.name << ": " << bad << " of " << extent
<< " bytes differ from the client-memory read, first at byte " << first
<< " (buffer " << static_cast<int>(actual[kOffset + first]) << ", host "
<< static_cast<int>(host[first]) << ")";
}
GLuint m_fbo = 0;
std::vector<GLuint> m_buffers;
std::vector<GLuint> m_textures;
};
TEST_F(PackBufferReadbackScenario, ReadPixelsIntoAPackBufferMatchesClientMemoryUnderEveryPackState) {
if (!Ready()) return;
AttachPatternRgba8(37, 23);
if (HasFatalFailure()) return;
for (const PackState& pack : kPackStates) {
ExpectPackBufferMatchesClientMemory(pack, 2, 1, 31, 19, GL_UNSIGNED_BYTE, 4, [](std::vector<GLubyte>& out) {
glGetBufferSubData(GL_PIXEL_PACK_BUFFER, 0, static_cast<GLsizeiptr>(out.size()), out.data());
});
if (HasFatalFailure()) return;
}
}
// The map is the OTHER CPU reader, and the one an application that streams readbacks
// through a PBO actually uses: glMapBufferRange(GL_MAP_READ_BIT) must see the pixels the
// server landed, not the bytes the client shadow held before the read.
TEST_F(PackBufferReadbackScenario, MapBufferRangeAfterAPackBufferReadSeesThePixels) {
if (!Ready()) return;
AttachPatternRgba8(37, 23);
if (HasFatalFailure()) return;
for (const PackState& pack : {kPackStates[0], kPackStates[5]}) {
ExpectPackBufferMatchesClientMemory(pack, 0, 0, 37, 23, GL_UNSIGNED_BYTE, 4, [](std::vector<GLubyte>& out) {
void* mapped = glMapBufferRange(GL_PIXEL_PACK_BUFFER, 0, static_cast<GLsizeiptr>(out.size()),
GL_MAP_READ_BIT);
ASSERT_NE(mapped, nullptr);
std::memcpy(out.data(), mapped, out.size());
glUnmapBuffer(GL_PIXEL_PACK_BUFFER);
});
if (HasFatalFailure()) return;
}
}
// GL_PACK_SWAP_BYTES reverses every component's bytes; the client carries the group
// width and the server swaps, so a wrong width or a skipped swap is a different float.
TEST_F(PackBufferReadbackScenario, SwapBytesIntoAPackBufferMatchesClientMemory) {
if (!Ready()) return;
constexpr int kWidth = 13, kHeight = 9;
std::vector<GLfloat> texels(static_cast<std::size_t>(kWidth) * kHeight * 4);
for (int y = 0; y < kHeight; ++y)
for (int x = 0; x < kWidth; ++x) {
const auto t = PatternRgba32f(x, y);
std::memcpy(texels.data() + (static_cast<std::size_t>(y) * kWidth + x) * 4, t.data(), sizeof(t));
}
const GLuint texture = NewTexture();
glBindTexture(GL_TEXTURE_2D, texture);
glTexStorage2D(GL_TEXTURE_2D, 1, GL_RGBA32F, kWidth, kHeight);
glTexSubImage2D(GL_TEXTURE_2D, 0, 0, 0, kWidth, kHeight, GL_RGBA, GL_FLOAT, texels.data());
glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_2D, texture, 0);
glReadBuffer(GL_COLOR_ATTACHMENT0);
ASSERT_EQ(glCheckFramebufferStatus(GL_FRAMEBUFFER), static_cast<GLenum>(GL_FRAMEBUFFER_COMPLETE));
const PackState swapped{"swap bytes, row length 17", 4, 17, 1, 1, GL_TRUE};
ExpectPackBufferMatchesClientMemory(swapped, 0, 0, kWidth, kHeight, GL_FLOAT, 16, [](std::vector<GLubyte>& out) {
glGetBufferSubData(GL_PIXEL_PACK_BUFFER, 0, static_cast<GLsizeiptr>(out.size()), out.data());
});
}
// glGetTexImage into a pack buffer, 2D and layered: the layered case is the one whose
// layout has an image stride (GL_PACK_IMAGE_HEIGHT) and a skip in the third dimension.
TEST_F(PackBufferReadbackScenario, GetTexImageIntoAPackBufferMatchesClientMemory) {
if (!Ready()) return;
constexpr int kWidth = 11, kHeight = 7, kLayers = 3;
std::vector<GLubyte> texels(static_cast<std::size_t>(kWidth) * kHeight * kLayers * 4);
for (int layer = 0; layer < kLayers; ++layer)
for (int y = 0; y < kHeight; ++y)
for (int x = 0; x < kWidth; ++x) {
const auto t = PatternRgba8(x, y, layer);
std::memcpy(texels.data() + ((static_cast<std::size_t>(layer) * kHeight + y) * kWidth + x) * 4,
t.data(), 4);
}
const GLuint flat = NewTexture();
glBindTexture(GL_TEXTURE_2D, flat);
glTexStorage2D(GL_TEXTURE_2D, 1, GL_RGBA8, kWidth, kHeight);
glTexSubImage2D(GL_TEXTURE_2D, 0, 0, 0, kWidth, kHeight, GL_RGBA, GL_UNSIGNED_BYTE, texels.data());
const GLuint layered = NewTexture();
glBindTexture(GL_TEXTURE_2D_ARRAY, layered);
glTexStorage3D(GL_TEXTURE_2D_ARRAY, 1, GL_RGBA8, kWidth, kHeight, kLayers);
glTexSubImage3D(GL_TEXTURE_2D_ARRAY, 0, 0, 0, 0, kWidth, kHeight, kLayers, GL_RGBA, GL_UNSIGNED_BYTE,
texels.data());
ASSERT_EQ(FirstGLError(), static_cast<GLenum>(GL_NO_ERROR)) << "texture upload";
struct Shape {
const char* name;
GLenum target;
GLuint texture;
int depth;
GLint rowLength, alignment, imageHeight, skipImages;
};
const Shape shapes[] = {
{"2D, row length 13, alignment 8", GL_TEXTURE_2D, flat, 1, 13, 8, 0, 0},
{"2D array, image height 9, skip images 1", GL_TEXTURE_2D_ARRAY, layered, kLayers, 0, 4, 9, 1},
};
for (const Shape& shape : shapes) {
glBindTexture(shape.target, shape.texture);
glPixelStorei(GL_PACK_ROW_LENGTH, shape.rowLength);
glPixelStorei(GL_PACK_ALIGNMENT, shape.alignment);
glPixelStorei(GL_PACK_IMAGE_HEIGHT, shape.imageHeight);
glPixelStorei(GL_PACK_SKIP_IMAGES, shape.skipImages);
const std::size_t rowPixels = shape.rowLength > 0 ? static_cast<std::size_t>(shape.rowLength) : kWidth;
const std::size_t stride = (rowPixels * 4 + shape.alignment - 1) / shape.alignment * shape.alignment;
const std::size_t imageRows = shape.imageHeight > 0 ? static_cast<std::size_t>(shape.imageHeight) : kHeight;
const std::size_t imageStride = stride * imageRows;
const std::size_t extent = static_cast<std::size_t>(shape.skipImages) * imageStride +
static_cast<std::size_t>(shape.depth - 1) * imageStride +
static_cast<std::size_t>(kHeight - 1) * stride + kWidth * 4u;
std::vector<GLubyte> host(extent, kFill);
glGetTexImage(shape.target, 0, GL_RGBA, GL_UNSIGNED_BYTE, host.data());
ASSERT_EQ(FirstGLError(), static_cast<GLenum>(GL_NO_ERROR)) << shape.name << ": client-memory read";
constexpr std::size_t kOffset = 32;
NewBuffer(GL_PIXEL_PACK_BUFFER, kOffset + extent, kFill);
glGetTexImage(shape.target, 0, GL_RGBA, GL_UNSIGNED_BYTE, reinterpret_cast<void*>(kOffset));
ASSERT_EQ(FirstGLError(), static_cast<GLenum>(GL_NO_ERROR)) << shape.name << ": pack-buffer read";
std::vector<GLubyte> actual(kOffset + extent, 0);
glGetBufferSubData(GL_PIXEL_PACK_BUFFER, 0, static_cast<GLsizeiptr>(actual.size()), actual.data());
glBindBuffer(GL_PIXEL_PACK_BUFFER, 0);
ResetPack();
std::size_t bad = 0, first = 0;
for (std::size_t i = 0; i < kOffset; ++i) bad += actual[i] != kFill ? 1 : 0;
EXPECT_EQ(bad, 0u) << shape.name << ": bytes in front of the offset moved";
bad = 0;
for (std::size_t i = 0; i < extent; ++i)
if (actual[kOffset + i] != host[i] && bad++ == 0) first = i;
EXPECT_EQ(bad, 0u) << shape.name << ": " << bad << " of " << extent
<< " bytes differ from the client-memory read, first at byte " << first;
}
}
// THE UNPACK HALF. A read into a buffer followed by an upload FROM that buffer is a
// GPU-to-GPU copy an application expects to work; under a transport the upload reads the
// client's shadow of the buffer, which the pack read did not touch, so this is exactly
// the case that goes wrong if the upload path forgets that the buffer was GPU-written.
TEST_F(PackBufferReadbackScenario, APackBufferReadFeedsAnUploadFromTheSameBuffer) {
if (!Ready()) return;
constexpr int kWidth = 29, kHeight = 17;
AttachPatternRgba8(kWidth, kHeight);
if (HasFatalFailure()) return;
const GLuint buffer = NewBuffer(GL_PIXEL_PACK_BUFFER, static_cast<std::size_t>(kWidth) * kHeight * 4, kFill);
glReadPixels(0, 0, kWidth, kHeight, GL_RGBA, GL_UNSIGNED_BYTE, nullptr);
glBindBuffer(GL_PIXEL_PACK_BUFFER, 0);
const GLuint copy = NewTexture();
glBindTexture(GL_TEXTURE_2D, copy);
glTexStorage2D(GL_TEXTURE_2D, 1, GL_RGBA8, kWidth, kHeight);
glBindBuffer(GL_PIXEL_UNPACK_BUFFER, buffer);
glTexSubImage2D(GL_TEXTURE_2D, 0, 0, 0, kWidth, kHeight, GL_RGBA, GL_UNSIGNED_BYTE, nullptr);
glBindBuffer(GL_PIXEL_UNPACK_BUFFER, 0);
ASSERT_EQ(FirstGLError(), static_cast<GLenum>(GL_NO_ERROR)) << "read into the buffer, then upload from it";
glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_2D, copy, 0);
std::vector<GLubyte> actual(static_cast<std::size_t>(kWidth) * kHeight * 4, 0);
glReadPixels(0, 0, kWidth, kHeight, GL_RGBA, GL_UNSIGNED_BYTE, actual.data());
std::size_t bad = 0;
std::string first;
for (int y = 0; y < kHeight; ++y)
for (int x = 0; x < kWidth; ++x) {
const auto expected = PatternRgba8(x, y, 0);
if (std::memcmp(actual.data() + (static_cast<std::size_t>(y) * kWidth + x) * 4, expected.data(), 4) != 0 &&
bad++ == 0)
first = "(" + std::to_string(x) + "," + std::to_string(y) + ")";
}
EXPECT_EQ(bad, 0u) << bad << " texels of the uploaded copy differ from the pattern, first at " << first;
}
// ORDER AGAINST LATER WRITES. A glBufferSubData into part of the buffer AFTER the read
// must win over the read's bytes there and leave the rest of the read alone - on the
// server the pixels and the later write are two queued ranges of one store, drained in
// record order.
TEST_F(PackBufferReadbackScenario, ALaterBufferSubDataWinsOverThePackReadWhereTheyOverlap) {
if (!Ready()) return;
constexpr int kWidth = 16, kHeight = 8;
AttachPatternRgba8(kWidth, kHeight);
if (HasFatalFailure()) return;
const std::size_t size = static_cast<std::size_t>(kWidth) * kHeight * 4;
std::vector<GLubyte> host(size, kFill);
glReadPixels(0, 0, kWidth, kHeight, GL_RGBA, GL_UNSIGNED_BYTE, host.data());
NewBuffer(GL_PIXEL_PACK_BUFFER, size, kFill);
glReadPixels(0, 0, kWidth, kHeight, GL_RGBA, GL_UNSIGNED_BYTE, nullptr);
const std::vector<GLubyte> overwrite(40, 0x5A);
glBufferSubData(GL_PIXEL_PACK_BUFFER, 100, static_cast<GLsizeiptr>(overwrite.size()), overwrite.data());
std::vector<GLubyte> actual(size, 0);
glGetBufferSubData(GL_PIXEL_PACK_BUFFER, 0, static_cast<GLsizeiptr>(size), actual.data());
ASSERT_EQ(FirstGLError(), static_cast<GLenum>(GL_NO_ERROR));
std::memcpy(host.data() + 100, overwrite.data(), overwrite.size());
std::size_t bad = 0, first = 0;
for (std::size_t i = 0; i < size; ++i)
if (actual[i] != host[i] && bad++ == 0) first = i;
EXPECT_EQ(bad, 0u) << bad << " bytes differ, first at " << first;
}
// THE WIRE HALF, and the one case that is about the transport rather than the pixels:
// under a transport a pack-buffer read posts NO reply-slot record and marks the buffer
// GPU-written, once per read. MOBILEGL_IPC_PBO_READBACK_SYNC=1 puts the reply form back,
// and this case must then fail on both counters - that is its red-once, recorded in
// docs/Disaggregated/notes/p9/.
TEST_F(PackBufferReadbackScenario, APackBufferReadPostsNoReplyAndMarksTheBuffer) {
if (!Ready()) return;
const std::string split = SplitRuntimeSkipReason();
if (!split.empty()) GTEST_SKIP() << split;
constexpr int kWidth = 24, kHeight = 12;
AttachPatternRgba8(kWidth, kHeight);
if (HasFatalFailure()) return;
NewBuffer(GL_PIXEL_PACK_BUFFER, static_cast<std::size_t>(kWidth) * kHeight * 4, kFill);
// A warm-up read, so every state record the read's fill has to publish once is out
// of the way before the counted window opens.
glReadPixels(0, 0, kWidth, kHeight, GL_RGBA, GL_UNSIGNED_BYTE, nullptr);
ASSERT_EQ(FirstGLError(), static_cast<GLenum>(GL_NO_ERROR));
constexpr int kReads = 4;
const SplitRuntimeState before = PeekSplitRuntime();
for (int i = 0; i < kReads; ++i) glReadPixels(0, 0, kWidth, kHeight, GL_RGBA, GL_UNSIGNED_BYTE, nullptr);
glGetTexImage(GL_TEXTURE_2D, 0, GL_RGBA, GL_UNSIGNED_BYTE, nullptr);
const SplitRuntimeState after = PeekSplitRuntime();
ASSERT_EQ(FirstGLError(), static_cast<GLenum>(GL_NO_ERROR));
EXPECT_EQ(after.replyPostings - before.replyPostings, 0u)
<< "a read into a pack buffer published a reply-slot record: it took the reply form "
"(MOBILEGL_IPC_PBO_READBACK_SYNC=1 does this on purpose)";
EXPECT_EQ(after.packBufferReadbackMarks - before.packBufferReadbackMarks,
static_cast<unsigned long long>(kReads + 1))
<< "every pack-buffer read marks its buffer GPU-written exactly once";
// And the bytes are still right after all of that.
std::vector<GLubyte> actual(static_cast<std::size_t>(kWidth) * kHeight * 4, 0);
glGetBufferSubData(GL_PIXEL_PACK_BUFFER, 0, static_cast<GLsizeiptr>(actual.size()), actual.data());
const auto corner = PatternRgba8(kWidth - 1, kHeight - 1, 0);
EXPECT_EQ(std::memcmp(actual.data() + actual.size() - 4, corner.data(), 4), 0);
}
} // namespace
} // namespace MGITest
@@ -0,0 +1,600 @@
// MobileGL - MobileGL/MG_IntegrationTest/Scenarios/PartialUploadOverGpuContentScenario.cpp
// 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
//
// Scenario - A SMALL glTexSubImage INTO A LEVEL THE GPU HAS WRITTEN.
//
// Espryt keeps a CPU shadow of every level and syncs a pending client write by uploading the
// region it covers (SyncMipmapsToBackend's sub-rect path). The shadow never sees a GPU write, so
// only the region may go up: anything more puts the shadow's stale texels over what the GPU drew.
// The region upload used to be skipped whenever the shadow's bytes had to be CONVERTED on the way
// to the driver - a three-channel format the driver cannot render is stored four-channel and every
// upload is repacked - and whenever the ES upload is shaped differently from the shadow (a 1D
// array, stored as a 2D array one texel high): those levels went up WHOLE, and a 2x2 write erased
// the rest of the level.
//
// Every case uploads a level, lets the GPU clear it, writes one 2x2 box, and reads the level back:
// the clear outside the box, the write inside. The formats are picked by what they cost the ES
// upload, which is per driver - on Mesa llvmpipe GL_SRGB8, GL_RGB8_SNORM and GL_RGB8UI are stored
// four-channel (see ThreeChannelAttachmentScenario) while GL_RGB16F is native; ES has no renderable
// three-channel format at all, so on a phone all four are converted. GL_RGBA8 is the control that
// never converts. A format a backend cannot attach is skipped, not failed.
//
// The second half is about ORDER, with GL_RGBA8. Magma queues a clear until the texture's next
// use and records texel uploads into a batch submitted ahead of the frame's recorded commands, so
// a write issued after a clear of (or a draw into) the level reached the image FIRST and the
// earlier clear or draw landed over it: the box read back green. Each order is covered - the
// clear already recorded, still queued, synced by a draw or by the readback, the reverse order
// in which the clear must win, and a draw instead of a clear.
//
// The last part is about SCATTER: several boxes written between two syncs, with the GPU's clear in
// the gaps between them. Everything a backend has to go on is the union box and the storage's
// rect list, and both describe more than the writes - the box spans the gaps, the list merges
// touching rects into their bounding box, folds once it holds 96, and is withheld once its rects
// fill 3/4 of the box; a box spanning the level went up as the whole level. Espryt also uploaded
// the union box whenever its unpack ring was in use. Each case below reaches one of those: two
// distant boxes, a hundred of them, boxes filling more than 3/4 of their union, and boxes whose
// union spans the level.
#include <cmath>
#include <cstdint>
#include <cstring>
#include <sstream>
#include <string>
#include <vector>
#include "../Harness/HeadlessGL.h"
#include "../Harness/ScenarioFixture.h"
#ifdef GLAPI
#undef GLAPI
#endif
#define GL_GLEXT_PROTOTYPES
#include <GL/gl.h>
#include <GL/glcorearb.h>
#undef GL_GLEXT_PROTOTYPES
namespace MGITest {
namespace {
constexpr int kEdge = 8;
constexpr int kBoxAt = 2;
constexpr int kBoxEdge = 2;
constexpr const char* kVS = R"(#version 430 core
void main() {
vec2 positions[3] = vec2[3](vec2(-1.0, -1.0), vec2(3.0, -1.0), vec2(-1.0, 3.0));
gl_Position = vec4(positions[gl_VertexID], 0.0, 1.0);
}
)";
// How a value in [0, 1] is spelled in the client data, and how the level is cleared and
// read back. Integer levels spell 1 as kIntegerOne.
enum class Encoding { Unorm8, Snorm8, Float32, Uint8 };
constexpr unsigned kIntegerOne = 9;
struct Format {
GLenum internalFormat;
GLenum clientFormat;
GLenum clientType;
int components;
Encoding encoding;
};
std::vector<std::uint8_t> Fill(const Format& f, int texels, float r, float g, float b) {
const float rgba[4] = {r, g, b, 1.0f};
const std::size_t componentBytes = f.encoding == Encoding::Float32 ? 4u : 1u;
std::vector<std::uint8_t> out(static_cast<std::size_t>(texels) * f.components * componentBytes);
for (int t = 0; t < texels; ++t) {
for (int c = 0; c < f.components; ++c) {
std::uint8_t* dst = out.data() + (static_cast<std::size_t>(t) * f.components + c) * componentBytes;
const float v = rgba[c];
switch (f.encoding) {
case Encoding::Unorm8: *dst = static_cast<std::uint8_t>(std::lround(v * 255.0f)); break;
case Encoding::Snorm8:
*dst = static_cast<std::uint8_t>(static_cast<std::int8_t>(std::lround(v * 127.0f)));
break;
case Encoding::Float32: std::memcpy(dst, &v, sizeof(v)); break;
case Encoding::Uint8: *dst = static_cast<std::uint8_t>(std::lround(v * kIntegerOne)); break;
}
}
}
return out;
}
class PartialUploadOverGpuContentScenario : public ScenarioTest {
protected:
void SetUp() override {
ScenarioTest::SetUp();
if (!Ready()) return;
m_scratch = MakeColorFbo(4, 4);
glGenVertexArrays(1, &m_vao);
glGenFramebuffers(1, &m_fbo);
FirstGLError();
}
void TearDown() override {
if (!Ready()) return;
glUseProgram(0);
for (GLuint p : m_programs) glDeleteProgram(p);
if (!m_textures.empty()) glDeleteTextures(static_cast<GLsizei>(m_textures.size()), m_textures.data());
if (m_vao) glDeleteVertexArrays(1, &m_vao);
if (m_fbo) glDeleteFramebuffers(1, &m_fbo);
BindDefaultFramebuffer();
DestroyColorFbo(m_scratch);
glViewport(0, 0, Gl().Width(), Gl().Height());
FirstGLError();
}
GLuint NewTexture(GLenum target) {
GLuint t = 0;
glGenTextures(1, &t);
m_textures.push_back(t);
glBindTexture(target, t);
glTexParameteri(target, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
glTexParameteri(target, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
glTexParameteri(target, GL_TEXTURE_MAX_LEVEL, 0);
glBindTexture(target, 0);
return t;
}
// A draw that samples the texture, which is what syncs its pending writes to the
// driver (an FBO attach alone may find the framebuffer already synced).
void SyncBySampling(GLenum target, GLuint tex, const char* samplerType, const char* coord) {
std::string fs = std::string("#version 430 core\nuniform ") + samplerType +
" u_tex;\nout vec4 o_color;\nvoid main() { o_color = vec4(texelFetch(u_tex, " + coord +
", 0)); }\n";
std::string error;
const GLuint program = CompileProgram(kVS, fs.c_str(), &error);
ASSERT_NE(program, 0u) << error;
m_programs.push_back(program);
BindFbo(m_scratch);
glUseProgram(program);
glActiveTexture(GL_TEXTURE0);
glBindTexture(target, tex);
glUniform1i(glGetUniformLocation(program, "u_tex"), 0);
glBindVertexArray(m_vao);
glDrawArrays(GL_TRIANGLES, 0, 3);
glBindVertexArray(0);
glBindTexture(target, 0);
glUseProgram(0);
BindDefaultFramebuffer();
glFinish();
EXPECT_EQ(FirstGLError(), 0u) << "sampling sync";
}
// Attaches (tex, layer) to the scratch framebuffer; false when this backend cannot.
bool Attach(GLuint tex, int layer) {
glBindFramebuffer(GL_FRAMEBUFFER, m_fbo);
if (layer >= 0) {
glFramebufferTextureLayer(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, tex, 0, layer);
} else {
glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_2D, tex, 0);
}
return glCheckFramebufferStatus(GL_FRAMEBUFFER) == GL_FRAMEBUFFER_COMPLETE;
}
void Detach() {
glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_2D, 0, 0);
BindDefaultFramebuffer();
}
// The GPU write the shadow never sees: the whole level (or layer) to green.
void GpuClearGreen(const Format& f) {
if (f.encoding == Encoding::Uint8) {
const GLuint green[4] = {0u, kIntegerOne, 0u, 1u};
glClearBufferuiv(GL_COLOR, 0, green);
} else {
const GLfloat green[4] = {0.0f, 1.0f, 0.0f, 1.0f};
glClearBufferfv(GL_COLOR, 0, green);
}
}
// The attached image's rgb, in units of "one", width x height texels.
std::vector<float> ReadRgb(const Format& f, int width, int height) {
std::vector<float> rgb(static_cast<std::size_t>(width * height) * 3u);
glPixelStorei(GL_PACK_ALIGNMENT, 1);
if (f.encoding == Encoding::Uint8) {
std::vector<GLuint> texels(static_cast<std::size_t>(width * height) * 4u, 0xA5A5A5A5u);
glReadPixels(0, 0, width, height, GL_RGBA_INTEGER, GL_UNSIGNED_INT, texels.data());
for (int i = 0; i < width * height; ++i)
for (int c = 0; c < 3; ++c)
rgb[i * 3 + c] = static_cast<float>(texels[i * 4 + c]) / kIntegerOne;
} else {
std::vector<GLfloat> texels(static_cast<std::size_t>(width * height) * 4u, -7.0f);
glReadPixels(0, 0, width, height, GL_RGBA, GL_FLOAT, texels.data());
for (int i = 0; i < width * height; ++i)
for (int c = 0; c < 3; ++c) rgb[i * 3 + c] = texels[i * 4 + c];
}
EXPECT_EQ(FirstGLError(), 0u) << "readback";
return rgb;
}
// Green everywhere but blue inside [boxX, boxX + boxW) x [boxY, boxY + boxH).
static void ExpectClearAroundBox(const std::vector<float>& rgb, int width, int height, int boxX, int boxY,
int boxW, int boxH) {
int bad = 0;
std::ostringstream first;
for (int y = 0; y < height; ++y)
for (int x = 0; x < width; ++x) {
const bool inBox = x >= boxX && x < boxX + boxW && y >= boxY && y < boxY + boxH;
const float want[3] = {0.0f, inBox ? 0.0f : 1.0f, inBox ? 1.0f : 0.0f};
const float* got = &rgb[static_cast<std::size_t>(y * width + x) * 3u];
bool texelBad = false;
for (int c = 0; c < 3; ++c) texelBad |= std::fabs(got[c] - want[c]) > 0.02f;
if (texelBad && bad++ == 0) {
first << "(" << x << "," << y << ") got " << got[0] << "," << got[1] << "," << got[2]
<< " want " << want[0] << "," << want[1] << "," << want[2];
}
}
EXPECT_EQ(bad, 0) << bad << " of " << width * height << " texels wrong; first " << first.str()
<< " (red outside the box: the stale shadow went back up; green inside it: the "
"write landed before the clear or draw)";
}
// Upload red, sync, clear green on the GPU, sync, write a blue 2x2 box, sync, read.
void Run2D(const Format& f) {
const GLuint t = NewTexture(GL_TEXTURE_2D);
const bool integer = f.encoding == Encoding::Uint8;
const char* sampler = integer ? "usampler2D" : "sampler2D";
const auto red = Fill(f, kEdge * kEdge, 1.0f, 0.0f, 0.0f);
glBindTexture(GL_TEXTURE_2D, t);
glPixelStorei(GL_UNPACK_ALIGNMENT, 1);
glTexImage2D(GL_TEXTURE_2D, 0, static_cast<GLint>(f.internalFormat), kEdge, kEdge, 0, f.clientFormat,
f.clientType, red.data());
glBindTexture(GL_TEXTURE_2D, 0);
ASSERT_EQ(FirstGLError(), 0u) << "definition";
SyncBySampling(GL_TEXTURE_2D, t, sampler, "ivec2(0)");
if (!Attach(t, -1)) {
Detach();
GTEST_SKIP() << "the format does not attach on backend " << Gl().BackendName();
}
GpuClearGreen(f);
Detach();
SyncBySampling(GL_TEXTURE_2D, t, sampler, "ivec2(0)");
const auto blue = Fill(f, kBoxEdge * kBoxEdge, 0.0f, 0.0f, 1.0f);
glBindTexture(GL_TEXTURE_2D, t);
glTexSubImage2D(GL_TEXTURE_2D, 0, kBoxAt, kBoxAt, kBoxEdge, kBoxEdge, f.clientFormat, f.clientType,
blue.data());
glBindTexture(GL_TEXTURE_2D, 0);
glPixelStorei(GL_UNPACK_ALIGNMENT, 4);
SyncBySampling(GL_TEXTURE_2D, t, sampler, "ivec2(0)");
ASSERT_TRUE(Attach(t, -1));
const auto rgb = ReadRgb(f, kEdge, kEdge);
Detach();
ExpectClearAroundBox(rgb, kEdge, kEdge, kBoxAt, kBoxAt, kBoxEdge, kBoxEdge);
}
// P11 M: TWO SCATTERED WRITES INTO ONE LEVEL, so the level carries a rect list (their
// area is well under 3/4 of their union box) and not just a box. Under a transport
// (and on the push builds' monolith arm) the list crosses in the client's record,
// whose regions state their source pitch and offset in the CLIENT's layout; a level
// whose upload is converted is cut from the conversion's wider buffer, so those
// numbers must not address it (Managers.cpp, carriedLayoutApplies). The whole level
// is uploaded green first, so every texel the union box spans outside the two boxes
// is green in the shadow too: this case is about WHERE the boxes land, not about GPU
// content between them. Green everywhere but the two blue boxes.
void Run2DScatter(const Format& f) {
constexpr int kFirstAt = 1, kSecondAt = 5;
const GLuint t = NewTexture(GL_TEXTURE_2D);
const bool integer = f.encoding == Encoding::Uint8;
const char* sampler = integer ? "usampler2D" : "sampler2D";
const auto green = Fill(f, kEdge * kEdge, 0.0f, 1.0f, 0.0f);
glBindTexture(GL_TEXTURE_2D, t);
glPixelStorei(GL_UNPACK_ALIGNMENT, 1);
glTexImage2D(GL_TEXTURE_2D, 0, static_cast<GLint>(f.internalFormat), kEdge, kEdge, 0, f.clientFormat,
f.clientType, green.data());
glBindTexture(GL_TEXTURE_2D, 0);
ASSERT_EQ(FirstGLError(), 0u) << "definition";
SyncBySampling(GL_TEXTURE_2D, t, sampler, "ivec2(0)");
if (!Attach(t, -1)) {
Detach();
glPixelStorei(GL_UNPACK_ALIGNMENT, 4);
GTEST_SKIP() << "the format does not attach on backend " << Gl().BackendName();
}
Detach();
const auto blue = Fill(f, kBoxEdge * kBoxEdge, 0.0f, 0.0f, 1.0f);
glBindTexture(GL_TEXTURE_2D, t);
glTexSubImage2D(GL_TEXTURE_2D, 0, kFirstAt, kFirstAt, kBoxEdge, kBoxEdge, f.clientFormat, f.clientType,
blue.data());
glTexSubImage2D(GL_TEXTURE_2D, 0, kSecondAt, kSecondAt, kBoxEdge, kBoxEdge, f.clientFormat,
f.clientType, blue.data());
glBindTexture(GL_TEXTURE_2D, 0);
glPixelStorei(GL_UNPACK_ALIGNMENT, 4);
SyncBySampling(GL_TEXTURE_2D, t, sampler, "ivec2(0)");
ASSERT_TRUE(Attach(t, -1));
const auto rgb = ReadRgb(f, kEdge, kEdge);
Detach();
int bad = 0;
std::ostringstream first;
for (int y = 0; y < kEdge; ++y)
for (int x = 0; x < kEdge; ++x) {
const auto inBox = [&](int at) {
return x >= at && x < at + kBoxEdge && y >= at && y < at + kBoxEdge;
};
const bool blueHere = inBox(kFirstAt) || inBox(kSecondAt);
const float want[3] = {0.0f, blueHere ? 0.0f : 1.0f, blueHere ? 1.0f : 0.0f};
const float* got = &rgb[static_cast<std::size_t>(y * kEdge + x) * 3u];
bool texelBad = false;
for (int c = 0; c < 3; ++c) texelBad |= std::fabs(got[c] - want[c]) > 0.02f;
if (texelBad && bad++ == 0) {
first << "(" << x << "," << y << ") got " << got[0] << "," << got[1] << "," << got[2]
<< " want " << want[0] << "," << want[1] << "," << want[2];
}
}
EXPECT_EQ(bad, 0) << bad << " of " << kEdge * kEdge << " texels wrong; first " << first.str()
<< " (a box read at the wrong pitch or offset lands the wrong bytes)";
}
// Building blocks of the RGBA8 ordering cases: the same texels, issued in the orders
// a backend that defers work may get wrong.
static Format Rgba8() { return {GL_RGBA8, GL_RGBA, GL_UNSIGNED_BYTE, 4, Encoding::Unorm8}; }
GLuint NewRedRgba8() {
const GLuint t = NewTexture(GL_TEXTURE_2D);
const auto red = Fill(Rgba8(), kEdge * kEdge, 1.0f, 0.0f, 0.0f);
glBindTexture(GL_TEXTURE_2D, t);
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA8, kEdge, kEdge, 0, GL_RGBA, GL_UNSIGNED_BYTE, red.data());
glBindTexture(GL_TEXTURE_2D, 0);
EXPECT_EQ(FirstGLError(), 0u) << "definition";
return t;
}
void SampleRgba8(GLuint t) { SyncBySampling(GL_TEXTURE_2D, t, "sampler2D", "ivec2(0)"); }
void ClearRgba8Green(GLuint t) {
ASSERT_TRUE(Attach(t, -1));
GpuClearGreen(Rgba8());
Detach();
}
// A draw that writes green over the whole level.
void DrawRgba8Green(GLuint t) {
std::string error;
const GLuint program = CompileProgram(
kVS, "#version 430 core\nout vec4 o_color;\nvoid main() { o_color = vec4(0.0, 1.0, 0.0, 1.0); }\n",
&error);
ASSERT_NE(program, 0u) << error;
m_programs.push_back(program);
ASSERT_TRUE(Attach(t, -1));
glViewport(0, 0, kEdge, kEdge);
glUseProgram(program);
glBindVertexArray(m_vao);
glDrawArrays(GL_TRIANGLES, 0, 3);
glBindVertexArray(0);
glUseProgram(0);
Detach();
glViewport(0, 0, Gl().Width(), Gl().Height());
}
void WriteRgba8BlueBox(GLuint t) {
const auto blue = Fill(Rgba8(), kBoxEdge * kBoxEdge, 0.0f, 0.0f, 1.0f);
glBindTexture(GL_TEXTURE_2D, t);
glTexSubImage2D(GL_TEXTURE_2D, 0, kBoxAt, kBoxAt, kBoxEdge, kBoxEdge, GL_RGBA, GL_UNSIGNED_BYTE,
blue.data());
glBindTexture(GL_TEXTURE_2D, 0);
}
// Green everywhere, and blue in the box when `boxWins`.
void ExpectRgba8(GLuint t, bool boxWins) {
ASSERT_TRUE(Attach(t, -1));
const auto rgb = ReadRgb(Rgba8(), kEdge, kEdge);
Detach();
ExpectClearAroundBox(rgb, kEdge, kEdge, kBoxAt, kBoxAt, boxWins ? kBoxEdge : 0, boxWins ? kBoxEdge : 0);
}
struct Box {
int x, y, w, h;
};
// An edge x edge RGBA8 level uploaded red, cleared green on the GPU, then written blue in
// every box by one glTexSubImage each, with no sync in between; read back after a sync.
// Blue inside the boxes, green everywhere else.
void RunScatteredWrites(int edge, const std::vector<Box>& boxes) {
const GLuint t = NewTexture(GL_TEXTURE_2D);
const auto red = Fill(Rgba8(), edge * edge, 1.0f, 0.0f, 0.0f);
glBindTexture(GL_TEXTURE_2D, t);
glPixelStorei(GL_UNPACK_ALIGNMENT, 1);
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA8, edge, edge, 0, GL_RGBA, GL_UNSIGNED_BYTE, red.data());
glBindTexture(GL_TEXTURE_2D, 0);
ASSERT_EQ(FirstGLError(), 0u) << "definition";
SampleRgba8(t);
ClearRgba8Green(t);
SampleRgba8(t);
glBindTexture(GL_TEXTURE_2D, t);
for (const Box& box : boxes) {
const auto blue = Fill(Rgba8(), box.w * box.h, 0.0f, 0.0f, 1.0f);
glTexSubImage2D(GL_TEXTURE_2D, 0, box.x, box.y, box.w, box.h, GL_RGBA, GL_UNSIGNED_BYTE,
blue.data());
}
glBindTexture(GL_TEXTURE_2D, 0);
glPixelStorei(GL_UNPACK_ALIGNMENT, 4);
ASSERT_EQ(FirstGLError(), 0u) << "writes";
SampleRgba8(t);
ASSERT_TRUE(Attach(t, -1));
const auto rgb = ReadRgb(Rgba8(), edge, edge);
Detach();
int bad = 0, stale = 0;
std::ostringstream first;
for (int y = 0; y < edge; ++y)
for (int x = 0; x < edge; ++x) {
bool inBox = false;
for (const Box& box : boxes)
inBox |= x >= box.x && x < box.x + box.w && y >= box.y && y < box.y + box.h;
const float want[3] = {0.0f, inBox ? 0.0f : 1.0f, inBox ? 1.0f : 0.0f};
const float* got = &rgb[static_cast<std::size_t>(y * edge + x) * 3u];
bool texelBad = false;
for (int c = 0; c < 3; ++c) texelBad |= std::fabs(got[c] - want[c]) > 0.02f;
if (!texelBad) continue;
stale += got[0] > 0.5f;
if (bad++ == 0) {
first << "(" << x << "," << y << ") got " << got[0] << "," << got[1] << "," << got[2]
<< " want " << want[0] << "," << want[1] << "," << want[2];
}
}
EXPECT_EQ(bad, 0) << bad << " of " << edge * edge << " texels wrong, " << stale
<< " of them red - the stale shadow went up outside the writes; first "
<< first.str();
}
GLuint m_vao = 0, m_fbo = 0;
ColorFbo m_scratch{};
std::vector<GLuint> m_programs;
std::vector<GLuint> m_textures;
};
// Control: the shadow's bytes go to the driver as they are.
TEST_F(PartialUploadOverGpuContentScenario, Rgba8SubImageKeepsTheGpuClear) {
if (!Ready()) return;
Run2D({GL_RGBA8, GL_RGBA, GL_UNSIGNED_BYTE, 4, Encoding::Unorm8});
}
TEST_F(PartialUploadOverGpuContentScenario, Srgb8SubImageKeepsTheGpuClear) {
if (!Ready()) return;
Run2D({GL_SRGB8, GL_RGB, GL_UNSIGNED_BYTE, 3, Encoding::Unorm8});
}
TEST_F(PartialUploadOverGpuContentScenario, Rgb8SnormSubImageKeepsTheGpuClear) {
if (!Ready()) return;
Run2D({GL_RGB8_SNORM, GL_RGB, GL_BYTE, 3, Encoding::Snorm8});
}
TEST_F(PartialUploadOverGpuContentScenario, Rgb16fSubImageKeepsTheGpuClear) {
if (!Ready()) return;
Run2D({GL_RGB16F, GL_RGB, GL_FLOAT, 3, Encoding::Float32});
}
TEST_F(PartialUploadOverGpuContentScenario, Rgb8uiSubImageKeepsTheGpuClear) {
if (!Ready()) return;
Run2D({GL_RGB8UI, GL_RGB_INTEGER, GL_UNSIGNED_BYTE, 3, Encoding::Uint8});
}
// P11 M: the rect-list shape of the same upload (Run2DScatter). RGBA8 is the control whose
// record pitch does address the uploaded bytes; the other two are stored four-channel on
// llvmpipe and on every ES driver, so their boxes are cut from the conversion's buffer.
TEST_F(PartialUploadOverGpuContentScenario, Rgba8ScatteredSubImagesLandWhereWritten) {
if (!Ready()) return;
Run2DScatter({GL_RGBA8, GL_RGBA, GL_UNSIGNED_BYTE, 4, Encoding::Unorm8});
}
TEST_F(PartialUploadOverGpuContentScenario, Srgb8ScatteredSubImagesLandWhereWritten) {
if (!Ready()) return;
Run2DScatter({GL_SRGB8, GL_RGB, GL_UNSIGNED_BYTE, 3, Encoding::Unorm8});
}
TEST_F(PartialUploadOverGpuContentScenario, Rgb8uiScatteredSubImagesLandWhereWritten) {
if (!Ready()) return;
Run2DScatter({GL_RGB8UI, GL_RGB_INTEGER, GL_UNSIGNED_BYTE, 3, Encoding::Uint8});
}
// A 1D array's ES image is a 2D array one texel high with the layers in depth, so the
// upload is shaped differently from the shadow (layers as rows) even with no conversion.
TEST_F(PartialUploadOverGpuContentScenario, OneDimensionalArraySubImageKeepsTheGpuClearedLayer) {
if (!Ready()) return;
constexpr int kLayers = 2;
const Format f{GL_RGBA8, GL_RGBA, GL_UNSIGNED_BYTE, 4, Encoding::Unorm8};
const GLuint t = NewTexture(GL_TEXTURE_1D_ARRAY);
const auto red = Fill(f, kEdge * kLayers, 1.0f, 0.0f, 0.0f);
glBindTexture(GL_TEXTURE_1D_ARRAY, t);
glTexImage2D(GL_TEXTURE_1D_ARRAY, 0, GL_RGBA8, kEdge, kLayers, 0, GL_RGBA, GL_UNSIGNED_BYTE, red.data());
glBindTexture(GL_TEXTURE_1D_ARRAY, 0);
ASSERT_EQ(FirstGLError(), 0u) << "definition";
SyncBySampling(GL_TEXTURE_1D_ARRAY, t, "sampler1DArray", "ivec2(0)");
if (!Attach(t, 1)) {
Detach();
GTEST_SKIP() << "a 1D array layer does not attach on backend " << Gl().BackendName();
}
GpuClearGreen(f);
Detach();
SyncBySampling(GL_TEXTURE_1D_ARRAY, t, "sampler1DArray", "ivec2(0)");
const auto blue = Fill(f, kBoxEdge, 0.0f, 0.0f, 1.0f);
glBindTexture(GL_TEXTURE_1D_ARRAY, t);
glTexSubImage2D(GL_TEXTURE_1D_ARRAY, 0, kBoxAt, 1, kBoxEdge, 1, GL_RGBA, GL_UNSIGNED_BYTE, blue.data());
glBindTexture(GL_TEXTURE_1D_ARRAY, 0);
SyncBySampling(GL_TEXTURE_1D_ARRAY, t, "sampler1DArray", "ivec2(0)");
ASSERT_TRUE(Attach(t, 1));
const auto layer1 = ReadRgb(f, kEdge, 1);
Detach();
ExpectClearAroundBox(layer1, kEdge, 1, kBoxAt, 0, kBoxEdge, 1);
}
// The orders below matter to a backend that defers GPU work (Magma queues clears and runs
// texel uploads in a batch ahead of the frame's recorded commands); every one is plain GL.
// The clear is still queued when the write arrives: it has to reach the level first.
TEST_F(PartialUploadOverGpuContentScenario, SubImageAfterAQueuedClearLandsOnTheClear) {
if (!Ready()) return;
const GLuint t = NewRedRgba8();
SampleRgba8(t);
ClearRgba8Green(t);
WriteRgba8BlueBox(t);
SampleRgba8(t);
ExpectRgba8(t, true);
}
// Same, with the readback as the first thing to sync the write.
TEST_F(PartialUploadOverGpuContentScenario, SubImageAfterAQueuedClearLandsOnTheClearForAReadback) {
if (!Ready()) return;
const GLuint t = NewRedRgba8();
SampleRgba8(t);
ClearRgba8Green(t);
WriteRgba8BlueBox(t);
ExpectRgba8(t, true);
}
// The other order: the write is still pending when the clear arrives, and the clear wins.
TEST_F(PartialUploadOverGpuContentScenario, ClearAfterAPendingSubImageWins) {
if (!Ready()) return;
const GLuint t = NewRedRgba8();
SampleRgba8(t);
WriteRgba8BlueBox(t);
ClearRgba8Green(t);
SampleRgba8(t);
ExpectRgba8(t, false);
}
// A draw into the level instead of a clear.
TEST_F(PartialUploadOverGpuContentScenario, SubImageAfterADrawIntoTheLevelLandsOnTheDraw) {
if (!Ready()) return;
const GLuint t = NewRedRgba8();
SampleRgba8(t);
DrawRgba8Green(t);
WriteRgba8BlueBox(t);
SampleRgba8(t);
ExpectRgba8(t, true);
}
// Two boxes in opposite corners: their union box is 7x7, of which the writes are 8 texels.
TEST_F(PartialUploadOverGpuContentScenario, TwoDistantSubImagesKeepTheGpuClearBetweenThem) {
if (!Ready()) return;
RunScatteredWrites(kEdge, {{0, 0, 2, 2}, {5, 5, 2, 2}});
}
// A hundred 2x2 boxes two texels apart: more than the 96 rects the storage keeps, so its
// list folds neighbours together, gaps and all.
TEST_F(PartialUploadOverGpuContentScenario, AHundredSubImagesKeepTheGpuClearBetweenThem) {
if (!Ready()) return;
std::vector<Box> boxes;
for (int j = 0; j < 10; ++j)
for (int i = 0; i < 10; ++i) boxes.push_back({4 * i + 1, 4 * j + 1, 2, 2});
RunScatteredWrites(40, boxes);
}
// Four 4x4 boxes around a one-texel cross: 64 of their union's 81 texels, past the 3/4 at
// which the storage stops handing its rect list out.
TEST_F(PartialUploadOverGpuContentScenario, SubImagesFillingThreeQuartersOfTheirUnionKeepTheGpuClearBetweenThem) {
if (!Ready()) return;
RunScatteredWrites(16, {{2, 2, 4, 4}, {7, 2, 4, 4}, {2, 7, 4, 4}, {7, 7, 4, 4}});
}
// Three quadrants: the union box is the whole level, and the list merges the touching
// boxes into it; the fourth quadrant is still the GPU's.
TEST_F(PartialUploadOverGpuContentScenario, SubImagesWhoseUnionSpansTheLevelKeepTheGpuClearInTheGap) {
if (!Ready()) return;
RunScatteredWrites(kEdge, {{0, 0, 4, 4}, {4, 0, 4, 4}, {0, 4, 4, 4}});
}
} // namespace
} // namespace MGITest
@@ -30,7 +30,9 @@ TEST_F(SyncWireScenario, FenceWaitStatusAndDeletionCrossAndPreserveRenderedPixel
glWaitSync(sync, 0, GL_TIMEOUT_IGNORED);
glDeleteSync(sync);
EXPECT_FALSE(glIsSync(sync));
EXPECT_GE(PeekSplitRuntime().emitSeq, before + 5);
// Create, the server-side wait and the delete always cross; since P10 the two polls may be
// answered from the server's fence report instead (FencePollScenario), so they are not counted.
EXPECT_GE(PeekSplitRuntime().emitSeq, before + 3);
GLubyte pixel[4]{};
glReadPixels(1, 1, 1, 1, GL_RGBA, GL_UNSIGNED_BYTE, pixel);
const int expected[4] = {64, 128, 191, 255};
@@ -0,0 +1,610 @@
// MobileGL - MobileGL/MG_IntegrationTest/Scenarios/TextureRemintPullScenario.cpp
// Copyright (c) 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
//
// Scenario - A TEXTURE RE-MINT NEEDS NO BYTE FROM THE CLIENT (P9 W2, notes/p9/W2-REMINT.md).
//
// The name is the ROADMAP's: P4a named `texture-remint-pull` the one new stall class of the
// split - a texture first bound to an image unit AFTER the backend allocated it has to be
// re-minted in image-bindable (possibly channel-widened) storage, and design/04 §8.4 assumed the
// server held no texels to fill it with, so the levels would be PULLED back from the client. P9
// W2 measured the opposite on both backends: Espryt's server reads each level back from the old
// GPU texture and replays its own staged store for what is still pending
// (RequireImageBindableStorageByHandle), Magma copies the old image into the new one GPU to GPU
// (PreserveTextureContentsOnRecreate). No pull protocol exists, and this scenario is what says
// the re-mint is right without one.
//
// EVERY CASE HAS ONE SHAPE. Give a texture its content by one means, sync it to the backend with
// a sampling draw (so the backend really holds storage when the bind arrives - a texture whose
// first sync comes after the bind is allocated image-bindable up front and re-mints nothing),
// optionally write it on the GPU, then glBindImageTexture it and copy it with imageLoad into a
// destination that was image-bound BEFORE its own first sync (so the destination never re-mints),
// and read the destination back through a framebuffer. The expectation is the GL answer, never
// "what monolith does": the cases exist exactly where the arms can differ.
//
// A mutable, uploaded, synced the store and the GPU agree
// B mutable, uploaded, then cleared on the GPU the store is STALE, the GPU holds it
// C immutable RGBA8, uploaded, then cleared Espryt's split keeps the allocation
// D null-defined, only ever cleared on the GPU the store has NO bytes
// E cleared, then an unsynced partial upload GPU texels merged with the pending box
// F glGenerateMipmap's level 1 GPU-generated, no bytes ever crossed
// F2 an uploaded level 1, then cleared on the GPU as B, one level down
// G level 1 null-defined AFTER the last sync the driver does not hold the level
// G2 level 0 redefined larger, null, after the sync the driver holds the OLD extent
// G3 G, plus a partial upload into that level the store holds the whole level
// H widened rg8 immutable, uploaded, then cleared the carrier re-mint (Espryt promotes)
// H2 widened rg8, uploaded only
// I RGBA8_SNORM mutable, uploaded only
// J RGBA16_SNORM immutable (widened), uploaded only
//
// G and G3 were Fatal{ResourceUnavailable, "image-promotion-readback"} on Espryt's split arms
// until W2-a: the promotion read a level the driver never allocated.
//
// ARMS. Both backends on the monolith arm and on Split / Spawn / Tcp, every case. The monolith
// DirectGLES arm used to be skipped for B, C, D, E, F, F2 and H: its re-mint replayed the client's
// shadow over texels the GPU wrote (W2-REMINT.md §6, the half of ID-P9-8 left to dev). Dev's
// 568090f0 made it read the GPU level back first (AdoptDriverLevelIntoShadow), P11 M merged it,
// and the skip is retired; E is also what holds the push builds' monolith arm to merging the
// applier's pending boxes, which the frontend's dirty model no longer names there.
//
// W2-d's COUNTER. `trp=` (TextureRemintPulls) counts a re-mint of storage the backend already
// held on BOTH Espryt arms now. The peek is process-wide, so it sees the server's count only
// where the server is this process: monolith and inproc. A/H assert +1 there, C asserts +0 on
// inproc (the kept allocation is not a re-mint).
//
// W2-b's CONTROL. MGITEST_ESPRYT_FORCE_REMINT_READBACK_FAILURE=1 (read by the SERVER) makes every
// promotion readback fail, which a host driver cannot be made to do for real. The
// `DirectGLES.{Split,Spawn}.RemintFallback.` entries run the uploaded-only cases under it: the
// store covers those levels, so the pixels stay right and the server log names the fallback.
#include <algorithm>
#include <cstdint>
#include <cstdio>
#include <cstdlib>
#include <string>
#include <vector>
#include "../Harness/HeadlessGL.h"
#include "../Harness/P4aFinalFixPeek.h"
#include "../Harness/PipeStatsWindow.h"
#include "../Harness/ScenarioFixture.h"
#include "../Harness/SplitRuntimePeek.h"
#ifdef GLAPI
#undef GLAPI
#endif
#define GL_GLEXT_PROTOTYPES
#include <GL/gl.h>
#include <GL/glcorearb.h>
#undef GL_GLEXT_PROTOTYPES
namespace MGITest {
namespace {
constexpr int kEdge = 8;
constexpr const char* kForceReadbackFailure = "MGITEST_ESPRYT_FORCE_REMINT_READBACK_FAILURE";
constexpr const char* kFallbackMarker = "remint-readback-fallback";
constexpr const char* kVertexSource = R"(#version 430 core
void main() {
vec2 positions[3] = vec2[3](vec2(-1.0, -1.0), vec2(3.0, -1.0), vec2(-1.0, 3.0));
gl_Position = vec4(positions[gl_VertexID], 0.0, 1.0);
}
)";
// The sync: any draw that samples the texture makes the backend allocate and upload it.
constexpr const char* kSampleSource = R"(#version 430 core
uniform sampler2D u_texture;
out vec4 o_color;
void main() { o_color = texelFetch(u_texture, ivec2(gl_FragCoord.xy) % 4, 0); }
)";
std::string CopyComputeSource(const char* sourceFormat, bool signedNormalized) {
std::string source = "#version 430 core\nlayout(local_size_x = 8, local_size_y = 8) in;\n";
source += std::string("layout(") + sourceFormat + ", binding = 0) readonly uniform image2D u_source;\n";
source += "layout(rgba8, binding = 1) writeonly uniform image2D u_destination;\n";
source += "void main() {\n ivec2 p = ivec2(gl_GlobalInvocationID.xy);\n";
source += " vec4 v = imageLoad(u_source, p);\n";
if (signedNormalized) source += " v = v * 0.5 + 0.5;\n";
source += " imageStore(u_destination, p, v);\n}\n";
return source;
}
std::vector<GLubyte> Pattern(int edge, GLubyte magic) {
std::vector<GLubyte> texels(static_cast<std::size_t>(edge * edge) * 4u);
for (int y = 0; y < edge; ++y) {
for (int x = 0; x < edge; ++x) {
const std::size_t at = static_cast<std::size_t>((y * edge + x) * 4);
texels[at + 0] = magic;
texels[at + 1] = static_cast<GLubyte>(16 * (x + 1));
texels[at + 2] = static_cast<GLubyte>(16 * (y + 1));
texels[at + 3] = 0xFF;
}
}
return texels;
}
std::vector<GLubyte> Solid(int edge, GLubyte r, GLubyte g, GLubyte b, GLubyte a) {
std::vector<GLubyte> texels(static_cast<std::size_t>(edge * edge) * 4u);
for (std::size_t i = 0; i < texels.size(); i += 4) {
texels[i] = r;
texels[i + 1] = g;
texels[i + 2] = b;
texels[i + 3] = a;
}
return texels;
}
const GLfloat kGreen[4] = {0.0f, 1.0f, 0.0f, 1.0f};
class TextureRemintPullScenario : public ScenarioTest {
protected:
void SetUp() override {
ScenarioTest::SetUp();
if (!Ready()) return;
GLint computeImages = 0;
glGetIntegerv(GL_MAX_COMPUTE_IMAGE_UNIFORMS, &computeImages);
FirstGLError();
if (computeImages < 2) {
GTEST_SKIP() << "the compute stage has fewer than two image uniforms";
}
m_wire = SplitRuntimeSkipReason().empty();
m_inproc = m_wire && PeekSplitRuntime().transportName == "inproc";
m_espryt = Gl().BackendName() == "DirectGLES";
m_forced = std::getenv(kForceReadbackFailure) != nullptr;
std::string error;
m_sampleProgram = CompileProgram(kVertexSource, kSampleSource, &error);
ASSERT_NE(m_sampleProgram, 0u) << error;
m_scratch = MakeColorFbo(4, 4);
ASSERT_NE(m_scratch.fbo, 0u) << "could not create the sampling target";
glGenVertexArrays(1, &m_vao);
glGenFramebuffers(1, &m_fbo);
ASSERT_EQ(FirstGLError(), 0u);
}
void TearDown() override {
if (!Ready()) return;
glUseProgram(0);
glBindImageTexture(0, 0, 0, GL_FALSE, 0, GL_READ_ONLY, GL_RGBA8);
glBindImageTexture(1, 0, 0, GL_FALSE, 0, GL_READ_ONLY, GL_RGBA8);
glBindTexture(GL_TEXTURE_2D, 0);
if (m_sampleProgram != 0) glDeleteProgram(m_sampleProgram);
if (m_vao != 0) glDeleteVertexArrays(1, &m_vao);
if (m_fbo != 0) glDeleteFramebuffers(1, &m_fbo);
if (!m_textures.empty()) {
glDeleteTextures(static_cast<GLsizei>(m_textures.size()), m_textures.data());
}
BindDefaultFramebuffer();
if (m_scratch.fbo != 0) DestroyColorFbo(m_scratch);
glViewport(0, 0, Gl().Width(), Gl().Height());
FirstGLError();
}
GLuint NewTexture() {
GLuint texture = 0;
glGenTextures(1, &texture);
m_textures.push_back(texture);
return texture;
}
static void NearestWithMaxLevel(GLuint texture, int maxLevel) {
glBindTexture(GL_TEXTURE_2D, texture);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAX_LEVEL, maxLevel);
glBindTexture(GL_TEXTURE_2D, 0);
}
// A draw that samples the texture: from here on the backend holds storage for it.
void SyncBySampling(GLuint texture) {
BindFbo(m_scratch);
glUseProgram(m_sampleProgram);
glActiveTexture(GL_TEXTURE0);
glBindTexture(GL_TEXTURE_2D, texture);
glUniform1i(glGetUniformLocation(m_sampleProgram, "u_texture"), 0);
glBindVertexArray(m_vao);
glDrawArrays(GL_TRIANGLES, 0, 3);
glBindVertexArray(0);
glBindTexture(GL_TEXTURE_2D, 0);
glUseProgram(0);
BindDefaultFramebuffer();
glFinish();
}
// A GPU write the client's shadow never sees.
void ClearOnTheGpu(GLuint texture, int level, const GLfloat* color) {
glBindFramebuffer(GL_FRAMEBUFFER, m_fbo);
glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_2D, texture, level);
ASSERT_EQ(glCheckFramebufferStatus(GL_FRAMEBUFFER), GLenum(GL_FRAMEBUFFER_COMPLETE));
glClearBufferfv(GL_COLOR, 0, color);
glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_2D, 0, 0);
BindDefaultFramebuffer();
}
// THE TRANSITION AND THE READ. The destination is image-bound before its first sync,
// so only the source can re-mint; the copy goes through imageLoad, which reads the
// re-minted storage and nothing else.
std::vector<GLubyte> ImageCopy(GLuint source, int level, GLenum unitFormat, const char* glslFormat,
bool signedNormalized = false) {
const GLuint destination = NewTexture();
glBindTexture(GL_TEXTURE_2D, destination);
glTexStorage2D(GL_TEXTURE_2D, 1, GL_RGBA8, kEdge, kEdge);
glBindTexture(GL_TEXTURE_2D, 0);
glBindImageTexture(1, destination, 0, GL_FALSE, 0, GL_WRITE_ONLY, GL_RGBA8);
glBindImageTexture(0, source, level, GL_FALSE, 0, GL_READ_ONLY, unitFormat);
EXPECT_EQ(FirstGLError(), 0u) << "glBindImageTexture raised a GL error";
const std::string computeSource = CopyComputeSource(glslFormat, signedNormalized);
const char* text = computeSource.c_str();
const GLuint shader = glCreateShader(GL_COMPUTE_SHADER);
glShaderSource(shader, 1, &text, nullptr);
glCompileShader(shader);
const GLuint program = glCreateProgram();
glAttachShader(program, shader);
glLinkProgram(program);
glDeleteShader(shader);
GLint linked = GL_FALSE;
glGetProgramiv(program, GL_LINK_STATUS, &linked);
EXPECT_EQ(linked, GL_TRUE) << "the imageLoad copy did not link";
glUseProgram(program);
// A BARRIER THE SPEC DOES NOT ASK FOR, AND IT IS HERE ON PURPOSE. GL orders a
// framebuffer write (the GPU clears above) before a later shader image load without
// one, yet on the Espryt split arm the kept-allocation case (C, no re-mint to
// serialize anything) read the pre-clear texels in 3 of 30 runs without it and 0 of
// 30 with it, while every server-side decision was identical in passing and failing
// runs (W2-REMINT.md §7). That ordering gap is not the re-mint's question; this case
// asks what the re-minted storage HOLDS, so the ordering is pinned here.
glMemoryBarrier(GL_ALL_BARRIER_BITS);
glDispatchCompute(1, 1, 1);
glMemoryBarrier(GL_ALL_BARRIER_BITS);
glUseProgram(0);
glDeleteProgram(program);
glBindImageTexture(0, 0, 0, GL_FALSE, 0, GL_READ_ONLY, GL_RGBA8);
glBindImageTexture(1, 0, 0, GL_FALSE, 0, GL_READ_ONLY, GL_RGBA8);
glBindFramebuffer(GL_FRAMEBUFFER, m_fbo);
glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_2D, destination, 0);
std::vector<GLubyte> read(static_cast<std::size_t>(kEdge * kEdge) * 4u, 0xA5);
glPixelStorei(GL_PACK_ALIGNMENT, 1);
glReadPixels(0, 0, kEdge, kEdge, GL_RGBA, GL_UNSIGNED_BYTE, read.data());
glPixelStorei(GL_PACK_ALIGNMENT, 4);
glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_2D, 0, 0);
BindDefaultFramebuffer();
EXPECT_EQ(FirstGLError(), 0u) << "the copy or its readback raised a GL error";
return read;
}
// The `edge` x `edge` corner of the read against `want`, `channels` channels, within 2.
void ExpectTexels(const char* what, const std::vector<GLubyte>& read, int edge,
const std::vector<GLubyte>& want, int channels = 4) {
int wrong = 0;
int firstX = -1, firstY = -1;
for (int y = 0; y < edge; ++y) {
for (int x = 0; x < edge; ++x) {
for (int c = 0; c < channels; ++c) {
const int got = read[static_cast<std::size_t>((y * kEdge + x) * 4 + c)];
const int expected = want[static_cast<std::size_t>((y * edge + x) * 4 + c)];
if (std::abs(got - expected) > 2 && wrong++ == 0) {
firstX = x;
firstY = y;
}
}
}
}
const std::size_t readAt = static_cast<std::size_t>((std::max(firstY, 0) * kEdge + std::max(firstX, 0)) * 4);
const std::size_t wantAt = static_cast<std::size_t>((std::max(firstY, 0) * edge + std::max(firstX, 0)) * 4);
EXPECT_EQ(wrong, 0) << what << ": " << wrong << " channel(s) differ; first at (" << firstX << ", "
<< firstY << ") read " << int(read[readAt]) << "," << int(read[readAt + 1]) << ","
<< int(read[readAt + 2]) << "," << int(read[readAt + 3]) << " want "
<< int(want[wantAt]) << "," << int(want[wantAt + 1]) << ","
<< int(want[wantAt + 2]) << "," << int(want[wantAt + 3]) << " (backend "
<< Gl().BackendName() << ", " << (m_wire ? "split" : "monolith") << ")";
}
// W2-d: where the peek can see the server's count (monolith, inproc) - and only on a
// push build, which is where PipeStats exists; the pull build's peek answers false (P11
// M: the skip retired above now runs H there too, and A always did).
bool RemintCountVisible() const {
unsigned long long ignored = 0;
return m_espryt && (!m_wire || m_inproc) && PeekPipeStatsTextureRemintPulls(&ignored);
}
unsigned long long RemintCount() {
unsigned long long count = 0;
EXPECT_TRUE(PeekPipeStatsTextureRemintPulls(&count));
return count;
}
// W2-b: under the forced-failure knob the server must have named its fallback.
void ExpectFallbackNamedSince(const PipeStatsWindow::LogMark& mark) {
if (!m_forced) return;
const std::string log = PipeStatsWindow::ReadServerLogSince(mark);
EXPECT_NE(log.find(kFallbackMarker), std::string::npos)
<< kForceReadbackFailure << " is set but the server log after the image bind carries no '"
<< kFallbackMarker << "' line: either the knob did not reach the server (the entry "
<< "proves nothing) or the promotion took no readback at all";
}
bool m_wire = false;
bool m_inproc = false;
bool m_espryt = false;
bool m_forced = false;
GLuint m_sampleProgram = 0;
GLuint m_vao = 0;
GLuint m_fbo = 0;
ColorFbo m_scratch{};
std::vector<GLuint> m_textures;
};
TEST_F(TextureRemintPullScenario, AnUploadedMutableTextureKeepsItsTexelsAcrossTheImageBind) {
if (!Ready()) return;
const bool counted = RemintCountVisible() && !m_forced;
const unsigned long long before = counted ? RemintCount() : 0;
const GLuint texture = NewTexture();
const auto pattern = Pattern(kEdge, 0x40);
glBindTexture(GL_TEXTURE_2D, texture);
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA8, kEdge, kEdge, 0, GL_RGBA, GL_UNSIGNED_BYTE, pattern.data());
NearestWithMaxLevel(texture, 0);
SyncBySampling(texture);
const auto mark = PipeStatsWindow::MarkLaneLog();
ExpectTexels("an uploaded mutable texture", ImageCopy(texture, 0, GL_RGBA8, "rgba8"), kEdge, pattern);
ExpectFallbackNamedSince(mark);
if (counted) {
EXPECT_EQ(RemintCount(), before + 1)
<< "the re-mint of a mutable texture the backend already held was not counted (trp=)";
}
}
TEST_F(TextureRemintPullScenario, AGpuClearAfterTheUploadSurvivesTheImageBind) {
if (!Ready()) return;
const GLuint texture = NewTexture();
const auto pattern = Pattern(kEdge, 0x40);
glBindTexture(GL_TEXTURE_2D, texture);
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA8, kEdge, kEdge, 0, GL_RGBA, GL_UNSIGNED_BYTE, pattern.data());
NearestWithMaxLevel(texture, 0);
SyncBySampling(texture);
ClearOnTheGpu(texture, 0, kGreen);
SyncBySampling(texture);
ExpectTexels("a GPU clear after the upload", ImageCopy(texture, 0, GL_RGBA8, "rgba8"), kEdge,
Solid(kEdge, 0, 255, 0, 255));
}
TEST_F(TextureRemintPullScenario, AnImmutableTexturesGpuClearSurvivesTheImageBind) {
if (!Ready()) return;
const bool counted = RemintCountVisible() && m_inproc;
const unsigned long long before = counted ? RemintCount() : 0;
const GLuint texture = NewTexture();
const auto pattern = Pattern(kEdge, 0x40);
glBindTexture(GL_TEXTURE_2D, texture);
glTexStorage2D(GL_TEXTURE_2D, 1, GL_RGBA8, kEdge, kEdge);
glTexSubImage2D(GL_TEXTURE_2D, 0, 0, 0, kEdge, kEdge, GL_RGBA, GL_UNSIGNED_BYTE, pattern.data());
NearestWithMaxLevel(texture, 0);
SyncBySampling(texture);
ClearOnTheGpu(texture, 0, kGreen);
SyncBySampling(texture);
ExpectTexels("an immutable texture's GPU clear", ImageCopy(texture, 0, GL_RGBA8, "rgba8"), kEdge,
Solid(kEdge, 0, 255, 0, 255));
if (counted) {
EXPECT_EQ(RemintCount(), before)
<< "an immutable core-format allocation is KEPT by the server, not re-minted, and must "
"not count (trp=)";
}
}
TEST_F(TextureRemintPullScenario, ANullDefinedTextureWrittenOnlyByTheGpuSurvivesTheImageBind) {
if (!Ready()) return;
const GLuint texture = NewTexture();
glBindTexture(GL_TEXTURE_2D, texture);
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA8, kEdge, kEdge, 0, GL_RGBA, GL_UNSIGNED_BYTE, nullptr);
NearestWithMaxLevel(texture, 0);
SyncBySampling(texture);
ClearOnTheGpu(texture, 0, kGreen);
SyncBySampling(texture);
ExpectTexels("a null-defined texture only the GPU wrote", ImageCopy(texture, 0, GL_RGBA8, "rgba8"),
kEdge, Solid(kEdge, 0, 255, 0, 255));
}
TEST_F(TextureRemintPullScenario, APendingPartialUploadMergesOverGpuWrittenTexels) {
if (!Ready()) return;
const GLuint texture = NewTexture();
const auto pattern = Pattern(kEdge, 0x40);
glBindTexture(GL_TEXTURE_2D, texture);
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA8, kEdge, kEdge, 0, GL_RGBA, GL_UNSIGNED_BYTE, pattern.data());
NearestWithMaxLevel(texture, 0);
SyncBySampling(texture);
ClearOnTheGpu(texture, 0, kGreen);
SyncBySampling(texture);
const auto blue = Solid(2, 0, 0, 255, 255);
glBindTexture(GL_TEXTURE_2D, texture);
glTexSubImage2D(GL_TEXTURE_2D, 0, 2, 2, 2, 2, GL_RGBA, GL_UNSIGNED_BYTE, blue.data());
glBindTexture(GL_TEXTURE_2D, 0);
auto want = Solid(kEdge, 0, 255, 0, 255);
for (int y = 2; y < 4; ++y) {
for (int x = 2; x < 4; ++x) {
const std::size_t at = static_cast<std::size_t>((y * kEdge + x) * 4);
want[at] = 0;
want[at + 1] = 0;
want[at + 2] = 255;
}
}
ExpectTexels("a pending partial upload over GPU texels", ImageCopy(texture, 0, GL_RGBA8, "rgba8"),
kEdge, want);
}
TEST_F(TextureRemintPullScenario, AGeneratedMipLevelSurvivesTheImageBind) {
if (!Ready()) return;
const GLuint texture = NewTexture();
const auto solid = Solid(kEdge, 200, 100, 50, 255);
glBindTexture(GL_TEXTURE_2D, texture);
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA8, kEdge, kEdge, 0, GL_RGBA, GL_UNSIGNED_BYTE, solid.data());
glGenerateMipmap(GL_TEXTURE_2D);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST_MIPMAP_NEAREST);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
glBindTexture(GL_TEXTURE_2D, 0);
SyncBySampling(texture);
ExpectTexels("glGenerateMipmap's level 1", ImageCopy(texture, 1, GL_RGBA8, "rgba8"), kEdge / 2,
Solid(kEdge / 2, 200, 100, 50, 255));
}
TEST_F(TextureRemintPullScenario, AGpuClearedMipLevelSurvivesTheImageBind) {
if (!Ready()) return;
const GLuint texture = NewTexture();
const auto level0 = Solid(kEdge, 200, 100, 50, 255);
const auto level1 = Solid(kEdge / 2, 10, 20, 30, 255);
glBindTexture(GL_TEXTURE_2D, texture);
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA8, kEdge, kEdge, 0, GL_RGBA, GL_UNSIGNED_BYTE, level0.data());
glTexImage2D(GL_TEXTURE_2D, 1, GL_RGBA8, kEdge / 2, kEdge / 2, 0, GL_RGBA, GL_UNSIGNED_BYTE, level1.data());
NearestWithMaxLevel(texture, 1);
SyncBySampling(texture);
ClearOnTheGpu(texture, 1, kGreen);
SyncBySampling(texture);
ExpectTexels("a GPU-cleared level 1", ImageCopy(texture, 1, GL_RGBA8, "rgba8"), kEdge / 2,
Solid(kEdge / 2, 0, 255, 0, 255));
}
// W2-a. Level 1 exists in the store (a null-data respecify) and on no driver: the texture's
// last sync predates it. The promotion must not read it back.
TEST_F(TextureRemintPullScenario, ALevelDefinedWithoutDataAfterTheLastSyncDoesNotEndTheSession) {
if (!Ready()) return;
const GLuint texture = NewTexture();
const auto pattern = Pattern(kEdge, 0x40);
glBindTexture(GL_TEXTURE_2D, texture);
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA8, kEdge, kEdge, 0, GL_RGBA, GL_UNSIGNED_BYTE, pattern.data());
NearestWithMaxLevel(texture, 0);
SyncBySampling(texture);
glBindTexture(GL_TEXTURE_2D, texture);
glTexImage2D(GL_TEXTURE_2D, 1, GL_RGBA8, kEdge / 2, kEdge / 2, 0, GL_RGBA, GL_UNSIGNED_BYTE, nullptr);
glBindTexture(GL_TEXTURE_2D, 0);
ExpectTexels("level 0 beside a level defined after the last sync",
ImageCopy(texture, 0, GL_RGBA8, "rgba8"), kEdge, pattern);
}
// W2-a's extent half: the driver holds level 0 at its OLD extent. The new contents are
// undefined (GL 4.6 core 8.5), so only "no Fatal, no GL error" is asserted.
TEST_F(TextureRemintPullScenario, ALevelRedefinedLargerWithoutDataAfterTheLastSyncDoesNotEndTheSession) {
if (!Ready()) return;
const GLuint texture = NewTexture();
const auto pattern = Pattern(kEdge / 2, 0x40);
glBindTexture(GL_TEXTURE_2D, texture);
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA8, kEdge / 2, kEdge / 2, 0, GL_RGBA, GL_UNSIGNED_BYTE,
pattern.data());
NearestWithMaxLevel(texture, 0);
SyncBySampling(texture);
glBindTexture(GL_TEXTURE_2D, texture);
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA8, kEdge, kEdge, 0, GL_RGBA, GL_UNSIGNED_BYTE, nullptr);
glBindTexture(GL_TEXTURE_2D, 0);
(void)ImageCopy(texture, 0, GL_RGBA8, "rgba8");
EXPECT_EQ(FirstGLError(), 0u);
}
// W2-a's store half: the level is on no driver, but its whole staged run is in the store
// (the client's level shadow, the partial upload inside it), and that is what replays.
TEST_F(TextureRemintPullScenario, APartialUploadIntoALevelDefinedAfterTheLastSyncReachesTheImage) {
if (!Ready()) return;
const GLuint texture = NewTexture();
const auto pattern = Pattern(kEdge, 0x40);
glBindTexture(GL_TEXTURE_2D, texture);
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA8, kEdge, kEdge, 0, GL_RGBA, GL_UNSIGNED_BYTE, pattern.data());
NearestWithMaxLevel(texture, 0);
SyncBySampling(texture);
glBindTexture(GL_TEXTURE_2D, texture);
glTexImage2D(GL_TEXTURE_2D, 1, GL_RGBA8, kEdge / 2, kEdge / 2, 0, GL_RGBA, GL_UNSIGNED_BYTE, nullptr);
const auto blue = Solid(2, 0, 0, 255, 255);
glTexSubImage2D(GL_TEXTURE_2D, 1, 0, 0, 2, 2, GL_RGBA, GL_UNSIGNED_BYTE, blue.data());
// Level 1 inside [BASE_LEVEL, MAX_LEVEL], or the image access is invalid (8.26).
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAX_LEVEL, 1);
glBindTexture(GL_TEXTURE_2D, 0);
ExpectTexels("a partial upload into a level defined after the last sync",
ImageCopy(texture, 1, GL_RGBA8, "rgba8"), 2, blue);
}
TEST_F(TextureRemintPullScenario, AWidenedFormatsGpuClearSurvivesTheCarrierRemint) {
if (!Ready()) return;
const bool counted = RemintCountVisible();
const unsigned long long before = counted ? RemintCount() : 0;
const GLuint texture = NewTexture();
std::vector<GLubyte> rg(static_cast<std::size_t>(kEdge * kEdge) * 2u, 0x33);
glBindTexture(GL_TEXTURE_2D, texture);
glTexStorage2D(GL_TEXTURE_2D, 1, GL_RG8, kEdge, kEdge);
glPixelStorei(GL_UNPACK_ALIGNMENT, 1);
glTexSubImage2D(GL_TEXTURE_2D, 0, 0, 0, kEdge, kEdge, GL_RG, GL_UNSIGNED_BYTE, rg.data());
glPixelStorei(GL_UNPACK_ALIGNMENT, 4);
NearestWithMaxLevel(texture, 0);
SyncBySampling(texture);
const GLfloat halfQuarter[4] = {0.5f, 0.25f, 0.0f, 1.0f};
ClearOnTheGpu(texture, 0, halfQuarter);
SyncBySampling(texture);
ExpectTexels("a widened rg8 texture's GPU clear", ImageCopy(texture, 0, GL_RG8, "rg8"), kEdge,
Solid(kEdge, 128, 64, 0, 255), 2);
if (counted) {
EXPECT_EQ(RemintCount(), before + 1)
<< "the carrier re-mint of a widened texture the backend already held was not counted "
"(trp=)";
}
}
TEST_F(TextureRemintPullScenario, AWidenedFormatsUploadSurvivesTheCarrierRemint) {
if (!Ready()) return;
const GLuint texture = NewTexture();
std::vector<GLubyte> rg(static_cast<std::size_t>(kEdge * kEdge) * 2u);
for (std::size_t i = 0; i < rg.size(); i += 2) {
rg[i] = 0x33;
rg[i + 1] = 0x99;
}
glBindTexture(GL_TEXTURE_2D, texture);
glTexStorage2D(GL_TEXTURE_2D, 1, GL_RG8, kEdge, kEdge);
glPixelStorei(GL_UNPACK_ALIGNMENT, 1);
glTexSubImage2D(GL_TEXTURE_2D, 0, 0, 0, kEdge, kEdge, GL_RG, GL_UNSIGNED_BYTE, rg.data());
glPixelStorei(GL_UNPACK_ALIGNMENT, 4);
NearestWithMaxLevel(texture, 0);
SyncBySampling(texture);
const auto mark = PipeStatsWindow::MarkLaneLog();
ExpectTexels("a widened rg8 upload", ImageCopy(texture, 0, GL_RG8, "rg8"), kEdge,
Solid(kEdge, 0x33, 0x99, 0, 255), 2);
ExpectFallbackNamedSince(mark);
}
TEST_F(TextureRemintPullScenario, AnRgba8SnormUploadSurvivesTheImageBind) {
if (!Ready()) return;
const GLuint texture = NewTexture();
std::vector<GLbyte> texels(static_cast<std::size_t>(kEdge * kEdge) * 4u);
for (std::size_t i = 0; i < texels.size(); i += 4) {
texels[i] = 127;
texels[i + 1] = 0;
texels[i + 2] = -127;
texels[i + 3] = 127;
}
glBindTexture(GL_TEXTURE_2D, texture);
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA8_SNORM, kEdge, kEdge, 0, GL_RGBA, GL_BYTE, texels.data());
NearestWithMaxLevel(texture, 0);
SyncBySampling(texture);
const auto mark = PipeStatsWindow::MarkLaneLog();
ExpectTexels("an RGBA8_SNORM upload", ImageCopy(texture, 0, GL_RGBA8_SNORM, "rgba8_snorm", true), kEdge,
Solid(kEdge, 255, 128, 0, 255), 3);
ExpectFallbackNamedSince(mark);
}
TEST_F(TextureRemintPullScenario, AnRgba16SnormImmutableUploadSurvivesTheCarrierRemint) {
if (!Ready()) return;
const GLuint texture = NewTexture();
std::vector<GLshort> texels(static_cast<std::size_t>(kEdge * kEdge) * 4u);
for (std::size_t i = 0; i < texels.size(); i += 4) {
texels[i] = 32767;
texels[i + 1] = 0;
texels[i + 2] = -32767;
texels[i + 3] = 32767;
}
glBindTexture(GL_TEXTURE_2D, texture);
glTexStorage2D(GL_TEXTURE_2D, 1, GL_RGBA16_SNORM, kEdge, kEdge);
glTexSubImage2D(GL_TEXTURE_2D, 0, 0, 0, kEdge, kEdge, GL_RGBA, GL_SHORT, texels.data());
NearestWithMaxLevel(texture, 0);
SyncBySampling(texture);
const auto mark = PipeStatsWindow::MarkLaneLog();
ExpectTexels("an RGBA16_SNORM immutable upload",
ImageCopy(texture, 0, GL_RGBA16_SNORM, "rgba16_snorm", true), kEdge,
Solid(kEdge, 255, 128, 0, 255), 3);
ExpectFallbackNamedSince(mark);
}
} // namespace
} // namespace MGITest
@@ -0,0 +1,314 @@
// MobileGL - MobileGL/MG_IntegrationTest/Scenarios/UnboundUniformBlockScenario.cpp
// Copyright (c) 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
//
// Scenario - A PROGRAM DECLARES A UNIFORM BLOCK AND NOTHING IS BOUND AT ITS BINDING POINT.
//
// The uniform-block sibling of UnboundImageDescriptorScenario. A draw or dispatch whose program
// declares a uniform block at an indexed point with no buffer on it raises no GL error - whether
// the point was never bound or its buffer was deleted while bound (glDeleteBuffers unbinds it
// from every indexed point of the current context). Only what the shader READS from the block is
// undefined.
//
// DirectVulkan's ResolveUniformBufferPayload looked up the buffer at the block's point and
// dereferenced it with nothing there: a SIGSEGV inside the draw or the dispatch, taking the whole
// process with it. It now answers with a zeroed block of the reflected size.
//
// The block is STATICALLY USED in every case, because an unreferenced block is optimised out
// before it ever reaches a descriptor and would prove nothing. Its one read sits behind a
// uniform-controlled branch that is false whenever the point is empty, so no undefined value
// reaches an assertion. Each case also binds a buffer at the same point and reads it back through
// the same program - proof that the block is live and wired to that point - and then empties the
// point again, so the transition back to "nothing bound" is covered as well as the first use.
//
// DirectGLES forwards the empty point to the GLES driver, which does what GL says, so it is the
// control - every test here must stay green on both backends.
#include <array>
#include <string>
#include "../Harness/HeadlessGL.h"
#include "../Harness/ScenarioFixture.h"
#ifdef GLAPI
#undef GLAPI
#endif
#define GL_GLEXT_PROTOTYPES
#include <GL/gl.h>
#include <GL/glcorearb.h>
#undef GL_GLEXT_PROTOTYPES
namespace MGITest {
namespace {
constexpr int kFboSize = 32;
// The point under test. Not 0, so a stray glBindBufferBase(GL_UNIFORM_BUFFER, 0, ...) from
// an earlier scenario in this process is not what decides the case - and SetUp empties
// this one explicitly anyway.
constexpr GLuint kUniformPoint = 5;
constexpr GLuint kResultPoint = 0;
constexpr unsigned int kMarker = 0xC0FFEEu;
constexpr unsigned int kFallback = 7u;
constexpr unsigned int kBoundWord = 0x1234u;
// No vertex attributes: the quad's corners come from gl_VertexID, so nothing about the
// vertex fetch can be confused with the descriptor question under test.
constexpr const char* kQuadVertexSource = R"(#version 430 core
void main() {
vec2 corner = vec2((gl_VertexID & 1) == 0 ? -1.0 : 1.0,
(gl_VertexID & 2) == 0 ? -1.0 : 1.0);
gl_Position = vec4(corner, 0.0, 1.0);
}
)";
// Green unless u_read is set, in which case the block's tint.
constexpr const char* kFragmentSource = R"(#version 430 core
layout(std140, binding = 5) uniform Target { vec4 u_tint; };
uniform int u_read;
out vec4 o_color;
void main() {
vec4 color = vec4(0.0, 1.0, 0.0, 1.0);
if (u_read != 0) {
color = u_tint;
}
o_color = color;
}
)";
// The marker is unconditional, so a zero marker means the dispatch never ran; the value is
// the fallback unless u_read is set, in which case it is the block's word.
constexpr const char* kComputeSource = R"(#version 430 core
layout(local_size_x = 1) in;
layout(std430, binding = 0) buffer Result { uint g_marker; uint g_value; };
layout(std140, binding = 5) uniform Target { uint u_word; };
uniform int u_read;
void main() {
uint value = 7u;
if (u_read != 0) {
value = u_word;
}
g_marker = 0xC0FFEEu;
g_value = value;
}
)";
class UnboundUniformBlockScenario : public ScenarioTest {
protected:
void SetUp() override {
ScenarioTest::SetUp();
if (!Ready()) return;
m_target = MakeColorFbo(kFboSize, kFboSize);
ASSERT_NE(m_target.fbo, 0u) << "could not create the render target";
glGenVertexArrays(1, &m_vao);
const unsigned int zero[2] = {0u, 0u};
glGenBuffers(1, &m_result);
glBindBuffer(GL_SHADER_STORAGE_BUFFER, m_result);
glBufferData(GL_SHADER_STORAGE_BUFFER, sizeof(zero), zero, GL_DYNAMIC_COPY);
glBindBuffer(GL_SHADER_STORAGE_BUFFER, 0);
glBindBufferBase(GL_SHADER_STORAGE_BUFFER, kResultPoint, m_result);
// The harness shares one context across every scenario in the process, so an
// earlier one may have left a buffer at this point. The whole subject here is that
// nothing is bound, so say so rather than assume it.
glBindBufferBase(GL_UNIFORM_BUFFER, kUniformPoint, 0);
ASSERT_EQ(FirstGLError(), 0u) << "setting up the scenario raised a GL error";
}
void TearDown() override {
if (!Ready()) return;
glUseProgram(0);
glBindVertexArray(0);
glBindBufferBase(GL_UNIFORM_BUFFER, kUniformPoint, 0);
glBindBufferBase(GL_SHADER_STORAGE_BUFFER, kResultPoint, 0);
if (m_block != 0) glDeleteBuffers(1, &m_block);
if (m_result != 0) glDeleteBuffers(1, &m_result);
if (m_vao != 0) glDeleteVertexArrays(1, &m_vao);
if (m_draw != 0) glDeleteProgram(m_draw);
if (m_compute != 0) glDeleteProgram(m_compute);
BindDefaultFramebuffer();
DestroyColorFbo(m_target);
glViewport(0, 0, Gl().Width(), Gl().Height());
FirstGLError();
}
unsigned int MakeComputeProgram(const char* source) {
const GLuint shader = glCreateShader(GL_COMPUTE_SHADER);
glShaderSource(shader, 1, &source, nullptr);
glCompileShader(shader);
GLint compiled = GL_FALSE;
glGetShaderiv(shader, GL_COMPILE_STATUS, &compiled);
if (compiled == GL_FALSE) {
char log[4096] = {};
glGetShaderInfoLog(shader, sizeof(log) - 1, nullptr, log);
ADD_FAILURE() << "the compute shader did not compile: " << log;
glDeleteShader(shader);
return 0;
}
const GLuint program = glCreateProgram();
glAttachShader(program, shader);
glLinkProgram(program);
glDeleteShader(shader);
GLint linked = GL_FALSE;
glGetProgramiv(program, GL_LINK_STATUS, &linked);
if (linked == GL_FALSE) {
char log[4096] = {};
glGetProgramInfoLog(program, sizeof(log) - 1, nullptr, log);
ADD_FAILURE() << "the compute program did not link: " << log;
glDeleteProgram(program);
return 0;
}
return program;
}
// The block must be ACTIVE and at kUniformPoint, and the guard uniform must exist:
// if either were optimised away the program would never ask for the empty point and
// every case below would pass without testing anything.
static void ExpectTheBlockIsLiveAtThePoint(GLuint program) {
const GLuint block = glGetUniformBlockIndex(program, "Target");
ASSERT_NE(block, GL_INVALID_INDEX) << "the uniform block was optimised out - the case proves nothing";
GLint binding = -1;
glGetActiveUniformBlockiv(program, block, GL_UNIFORM_BLOCK_BINDING, &binding);
ASSERT_EQ(binding, static_cast<GLint>(kUniformPoint)) << "the block is not at the point under test";
ASSERT_NE(glGetUniformLocation(program, "u_read"), -1) << "the guard uniform was optimised out";
ASSERT_EQ(FirstGLError(), 0u) << "querying the block raised a GL error";
}
static void ExpectThePointIsEmpty(const char* when) {
GLint bound = -1;
glGetIntegeri_v(GL_UNIFORM_BUFFER_BINDING, kUniformPoint, &bound);
ASSERT_EQ(bound, 0) << "uniform point " << kUniformPoint << " is not empty " << when;
}
// A 16-byte std140 block at kUniformPoint holding `words`.
void BindBlock(const std::array<unsigned int, 4>& words) {
if (m_block == 0) glGenBuffers(1, &m_block);
glBindBuffer(GL_UNIFORM_BUFFER, m_block);
glBufferData(GL_UNIFORM_BUFFER, sizeof(words), words.data(), GL_DYNAMIC_DRAW);
glBindBuffer(GL_UNIFORM_BUFFER, 0);
glBindBufferBase(GL_UNIFORM_BUFFER, kUniformPoint, m_block);
}
// Runs the compute program once; returns {marker, value}.
std::array<unsigned int, 2> Dispatch(int read) {
const unsigned int zero[2] = {0u, 0u};
glBindBuffer(GL_SHADER_STORAGE_BUFFER, m_result);
glBufferSubData(GL_SHADER_STORAGE_BUFFER, 0, sizeof(zero), zero);
glBindBuffer(GL_SHADER_STORAGE_BUFFER, 0);
glUseProgram(m_compute);
glUniform1i(glGetUniformLocation(m_compute, "u_read"), read);
glDispatchCompute(1, 1, 1);
glMemoryBarrier(GL_BUFFER_UPDATE_BARRIER_BIT);
std::array<unsigned int, 2> out = {0xDEADBEEFu, 0xDEADBEEFu};
glBindBuffer(GL_SHADER_STORAGE_BUFFER, m_result);
glGetBufferSubData(GL_SHADER_STORAGE_BUFFER, 0, sizeof(out), out.data());
glBindBuffer(GL_SHADER_STORAGE_BUFFER, 0);
glUseProgram(0);
return out;
}
void ExpectTheDispatchRunsOnAnEmptyPoint(const char* when) {
ASSERT_NO_FATAL_FAILURE(ExpectThePointIsEmpty(when));
const auto result = Dispatch(0);
EXPECT_EQ(FirstGLError(), 0u) << "the dispatch raised a GL error " << when;
EXPECT_EQ(result[0], kMarker) << "the dispatch did not run " << when;
EXPECT_EQ(result[1], kFallback) << "the dispatch took the block's branch " << when;
}
// Draws the full-target quad and expects every pixel to be `color`.
void ExpectTheDraw(int read, const char* color, const std::string& when) {
BindFbo(m_target);
ClearTo(0.0f, 0.0f, 0.0f, 1.0f);
glBindVertexArray(m_vao);
glUseProgram(m_draw);
glUniform1i(glGetUniformLocation(m_draw, "u_read"), read);
glDrawArrays(GL_TRIANGLE_STRIP, 0, 4);
glBindVertexArray(0);
glUseProgram(0);
EXPECT_EQ(FirstGLError(), 0u) << "the draw raised a GL error " << when;
const Image image = ReadPixels(kFboSize, kFboSize);
ASSERT_FALSE(image.Empty()) << "the readback came back empty " << when;
EXPECT_TRUE(RegionIsMostly(image, 0, kFboSize - 1, 0, kFboSize - 1, color, 0.0, when))
<< "an all-black target means the draw was dropped";
}
void ExpectTheDrawRunsOnAnEmptyPoint(const std::string& when) {
ASSERT_NO_FATAL_FAILURE(ExpectThePointIsEmpty(when.c_str()));
ExpectTheDraw(0, "green", when);
}
ColorFbo m_target{};
GLuint m_vao = 0;
GLuint m_result = 0;
GLuint m_block = 0;
unsigned int m_draw = 0;
unsigned int m_compute = 0;
};
} // namespace
TEST_F(UnboundUniformBlockScenario, ADispatchDeclaringAnEmptyUniformPointRuns) {
if (!Ready() || IsSkipped()) return;
m_compute = MakeComputeProgram(kComputeSource);
ASSERT_NE(m_compute, 0u);
ASSERT_NO_FATAL_FAILURE(ExpectTheBlockIsLiveAtThePoint(m_compute));
ExpectTheDispatchRunsOnAnEmptyPoint("with nothing ever bound at the point");
BindBlock({kBoundWord, 0u, 0u, 0u});
const auto bound = Dispatch(1);
EXPECT_EQ(FirstGLError(), 0u);
EXPECT_EQ(bound[0], kMarker) << "the dispatch did not run with a buffer bound";
EXPECT_EQ(bound[1], kBoundWord) << "the program did not read the buffer bound at the block's point";
glBindBufferBase(GL_UNIFORM_BUFFER, kUniformPoint, 0);
ExpectTheDispatchRunsOnAnEmptyPoint("after the point was emptied again");
}
TEST_F(UnboundUniformBlockScenario, ADrawDeclaringAnEmptyUniformPointPaintsTheFrame) {
if (!Ready() || IsSkipped()) return;
std::string error;
m_draw = CompileProgram(kQuadVertexSource, kFragmentSource, &error);
ASSERT_NE(m_draw, 0u) << error;
ASSERT_NO_FATAL_FAILURE(ExpectTheBlockIsLiveAtThePoint(m_draw));
ExpectTheDrawRunsOnAnEmptyPoint("with nothing ever bound at the point");
// Opaque red: 1.0f and 0.0f as their IEEE-754 words, so the block is one vec4.
BindBlock({0x3F800000u, 0u, 0u, 0x3F800000u});
ExpectTheDraw(1, "red", "reading the buffer bound at the block's point");
glBindBufferBase(GL_UNIFORM_BUFFER, kUniformPoint, 0);
ExpectTheDrawRunsOnAnEmptyPoint("after the point was emptied again");
}
// The shape real applications reach it by: the buffer is bound and used, then deleted while
// still bound, which empties the point without the application ever unbinding it.
TEST_F(UnboundUniformBlockScenario, AUniformBufferDeletedWhileBoundLeavesAnEmptyPointBothPathsSurvive) {
if (!Ready() || IsSkipped()) return;
m_compute = MakeComputeProgram(kComputeSource);
ASSERT_NE(m_compute, 0u);
ASSERT_NO_FATAL_FAILURE(ExpectTheBlockIsLiveAtThePoint(m_compute));
std::string error;
m_draw = CompileProgram(kQuadVertexSource, kFragmentSource, &error);
ASSERT_NE(m_draw, 0u) << error;
ASSERT_NO_FATAL_FAILURE(ExpectTheBlockIsLiveAtThePoint(m_draw));
BindBlock({kBoundWord, 0u, 0u, 0u});
const auto bound = Dispatch(1);
EXPECT_EQ(bound[0], kMarker) << "the dispatch did not run with a buffer bound";
EXPECT_EQ(bound[1], kBoundWord) << "the program did not read the buffer bound at the block's point";
ASSERT_EQ(FirstGLError(), 0u);
glDeleteBuffers(1, &m_block);
m_block = 0;
ExpectTheDispatchRunsOnAnEmptyPoint("after the bound buffer was deleted");
ExpectTheDrawRunsOnAnEmptyPoint("after the bound buffer was deleted");
}
} // namespace MGITest
@@ -0,0 +1,292 @@
// MobileGL - MobileGL/MG_IntegrationTest/Scenarios/WireIndirectDispatchScenario.cpp
// Copyright (c) 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
//
// P8-SV: THE MAGMA WIRE ARM ISSUES glDispatchComputeIndirect FROM THE WIRE STORE, AS THE MONOLITH
// ARM DOES (docs/Disaggregated/notes/p8/SV.md).
//
// Before P8-SV the wire arm read the three group counts through ReadWireBuffer and dispatched them
// from the CPU, so a dispatch buffer a shader had written cost a whole-GPU wait per call - the
// dispatch twin of what P8-D removed for indirect draws (WireIndirectDrawScenario.cpp). The cases
// pin both halves:
// * THE GRID, on every arm and both backends - each dispatch marks exactly the work groups its
// counts name, and GPU-written counts are the ones the dispatch sees (the buffer starts with
// OTHER counts, so a stale read shows as the wrong grid);
// * THE SERVER'S OWN COUNTERS (Harness/WireIndirectPeek.h), where they can be read - Magma on the
// inproc arm: every call issued as vkCmdDispatchIndirect (`wdsp`), no host wait (`whw`), and the
// INDIRECT_COMMAND_READ barrier exactly once per shader write (`wibar`).
// The counters are read BEFORE the grids are read back: that readback is a host read of a store a
// shader wrote, so it takes a host wait of its own, which is the application's and not the
// dispatch's.
#include <array>
#include <cstdint>
#include <string>
#include <vector>
#include "../Harness/HeadlessGL.h"
#include "../Harness/ScenarioFixture.h"
#include "../Harness/SplitRuntimePeek.h"
#include "../Harness/WireIndirectPeek.h"
#ifdef GLAPI
#undef GLAPI
#endif
#define GL_GLEXT_PROTOTYPES
#include <GL/gl.h>
#include <GL/glext.h>
namespace MGITest {
namespace {
// Grids up to 4 x 4 x 2: one cell per work group.
constexpr std::uint32_t kGridX = 4, kGridY = 4, kGridZ = 2;
constexpr std::uint32_t kCells = kGridX * kGridY * kGridZ;
// One invocation per work group, and each group marks its own cell.
constexpr const char* kMarkSource = R"(#version 430 core
layout(local_size_x = 1) in;
layout(std430, binding = 2) buffer Marks { uint marks[]; };
void main() {
uvec3 g = gl_WorkGroupID;
atomicAdd(marks[(g.z * 4u + g.y) * 4u + g.x], 1u);
}
)";
// Copies `source` into `words`, one word per invocation: the compute write under test.
constexpr const char* kCopySource = R"(#version 430 core
layout(local_size_x = 1) in;
layout(std430, binding = 0) buffer Words { uint words[]; };
layout(std430, binding = 1) readonly buffer Source { uint source[]; };
void main() { words[gl_GlobalInvocationID.x] = source[gl_GlobalInvocationID.x]; }
)";
using Grid = std::array<std::uint32_t, 3>;
class WireIndirectDispatchScenario : public ScenarioTest {
protected:
void SetUp() override {
ScenarioTest::SetUp();
if (!Ready()) return;
m_magma = Gl().BackendName() == "DirectVulkan";
m_transport = PeekSplitRuntime().transportName;
m_mark = CompileCompute(kMarkSource);
ASSERT_NE(m_mark, 0u);
m_copy = CompileCompute(kCopySource);
ASSERT_NE(m_copy, 0u);
ASSERT_EQ(FirstGLError(), GLenum(GL_NO_ERROR)) << "scene setup left a GL error behind";
// The counters are armed by the first peek, so it has to come before any dispatch.
if (CountersReadable()) {
ASSERT_TRUE(PeekWireIndirectCounters(&m_before))
<< "Magma on the inproc arm must be able to read the wire arm's own counters";
}
}
void TearDown() override {
if (!Ready()) return;
glBindBuffer(GL_DISPATCH_INDIRECT_BUFFER, 0);
for (GLuint& buffer : m_marks) {
if (buffer != 0) glDeleteBuffers(1, &buffer);
}
for (GLuint* buffer : {&m_groupCounts, &m_source}) {
if (*buffer != 0) glDeleteBuffers(1, buffer);
*buffer = 0;
}
if (m_mark != 0) glDeleteProgram(m_mark);
if (m_copy != 0) glDeleteProgram(m_copy);
}
static GLuint CompileCompute(const char* source) {
const GLuint shader = glCreateShader(GL_COMPUTE_SHADER);
glShaderSource(shader, 1, &source, nullptr);
glCompileShader(shader);
GLint ok = GL_FALSE;
glGetShaderiv(shader, GL_COMPILE_STATUS, &ok);
if (ok != GL_TRUE) {
glDeleteShader(shader);
return 0;
}
const GLuint program = glCreateProgram();
glAttachShader(program, shader);
glLinkProgram(program);
glDeleteShader(shader);
glGetProgramiv(program, GL_LINK_STATUS, &ok);
if (ok != GL_TRUE) {
glDeleteProgram(program);
return 0;
}
return program;
}
static void Fill(GLuint& name, GLenum target, const std::vector<std::uint32_t>& words) {
if (name == 0) glGenBuffers(1, &name);
glBindBuffer(target, name);
glBufferData(target, static_cast<GLsizeiptr>(words.size() * sizeof(std::uint32_t)), words.data(),
GL_DYNAMIC_COPY);
}
// The dispatch-indirect store, holding CPU-written counts.
void Parameters(const std::vector<std::uint32_t>& words) {
Fill(m_groupCounts, GL_DISPATCH_INDIRECT_BUFFER, words);
}
// The compute write: the parameter store (already holding its poison) receives `words`.
void ComputeWriteParameters(const std::vector<std::uint32_t>& words) {
Fill(m_source, GL_SHADER_STORAGE_BUFFER, words);
glBindBufferBase(GL_SHADER_STORAGE_BUFFER, 0, m_groupCounts);
glBindBufferBase(GL_SHADER_STORAGE_BUFFER, 1, m_source);
glUseProgram(m_copy);
glDispatchCompute(static_cast<GLuint>(words.size()), 1, 1);
glBindBufferBase(GL_SHADER_STORAGE_BUFFER, 0, 0);
glBindBufferBase(GL_SHADER_STORAGE_BUFFER, 1, 0);
}
// One indirect dispatch of the mark program into a fresh, zeroed cell buffer. Returns
// the index of that buffer for ExpectGrid.
size_t DispatchIndirect(GLintptr offset) {
GLuint marks = 0;
Fill(marks, GL_SHADER_STORAGE_BUFFER, std::vector<std::uint32_t>(kCells, 0u));
m_marks.push_back(marks);
glBindBufferBase(GL_SHADER_STORAGE_BUFFER, 2, marks);
glUseProgram(m_mark);
glBindBuffer(GL_DISPATCH_INDIRECT_BUFFER, m_groupCounts);
glDispatchComputeIndirect(offset);
glBindBufferBase(GL_SHADER_STORAGE_BUFFER, 2, 0);
return m_marks.size() - 1;
}
// A round trip that reads no buffer store, so the server has applied every dispatch
// above before the counters are read - and no host wait of its own is added.
static void Settle() {
BindDefaultFramebuffer();
(void)ReadPixels(1, 1);
}
void ExpectGrid(size_t which, const Grid& grid, const std::string& what) {
glMemoryBarrier(GL_BUFFER_UPDATE_BARRIER_BIT);
std::vector<std::uint32_t> cells(kCells, 0xDEADu);
glBindBuffer(GL_SHADER_STORAGE_BUFFER, m_marks[which]);
glGetBufferSubData(GL_SHADER_STORAGE_BUFFER, 0, static_cast<GLsizeiptr>(kCells * sizeof(std::uint32_t)),
cells.data());
ASSERT_EQ(FirstGLError(), GLenum(GL_NO_ERROR)) << what << ": readback";
size_t wrong = 0;
std::string first;
for (std::uint32_t z = 0; z < kGridZ; ++z) {
for (std::uint32_t y = 0; y < kGridY; ++y) {
for (std::uint32_t x = 0; x < kGridX; ++x) {
const bool inside = x < grid[0] && y < grid[1] && z < grid[2];
const std::uint32_t got = cells[(z * kGridY + y) * kGridX + x];
if (got != (inside ? 1u : 0u) && wrong++ == 0) {
first = "group (" + std::to_string(x) + "," + std::to_string(y) + "," +
std::to_string(z) + ") = " + std::to_string(got);
}
}
}
}
EXPECT_EQ(wrong, 0u) << what << ": expected exactly the " << grid[0] << "x" << grid[1] << "x"
<< grid[2] << " grid marked once; first wrong " << first;
}
// Magma's server in THIS process: the inproc arm. Spawn and tcp servers are other
// processes, and the monolith arm never takes the wire path at all.
bool CountersReadable() const { return m_magma && m_transport == "inproc"; }
WireIndirectCounters Delta() const {
WireIndirectCounters now{};
EXPECT_TRUE(PeekWireIndirectCounters(&now));
WireIndirectCounters delta{};
delta.nativeDraws = now.nativeDraws - m_before.nativeDraws;
delta.cpuExpansions = now.cpuExpansions - m_before.cpuExpansions;
delta.barriers = now.barriers - m_before.barriers;
delta.hostWaits = now.hostWaits - m_before.hostWaits;
delta.indirectWaits = now.indirectWaits - m_before.indirectWaits;
delta.nativeDispatches = now.nativeDispatches - m_before.nativeDispatches;
return delta;
}
bool m_magma = false;
std::string m_transport;
WireIndirectCounters m_before{};
GLuint m_mark = 0, m_copy = 0, m_groupCounts = 0, m_source = 0;
std::vector<GLuint> m_marks;
};
// CPU-written counts at two non-zero offsets of one store. On Magma/inproc both calls must
// be issued natively, with no host wait and no barrier (no shader wrote the store).
TEST_F(WireIndirectDispatchScenario, CpuWrittenGroupCountsAreReadByTheGpuAtTheirOffsets) {
if (!Ready() || IsSkipped()) return;
Parameters({1, 1, 1, 3, 2, 1, 4, 1, 2});
const size_t first = DispatchIndirect(12);
const size_t second = DispatchIndirect(24);
ASSERT_EQ(FirstGLError(), GLenum(GL_NO_ERROR));
Settle();
if (CountersReadable()) {
const WireIndirectCounters delta = Delta();
EXPECT_EQ(delta.nativeDispatches, 2u) << "every indirect dispatch must be vkCmdDispatchIndirect";
EXPECT_EQ(delta.hostWaits, 0u) << "a host wait was taken with no GPU write to the counts";
EXPECT_EQ(delta.barriers, 0u) << "no shader wrote this store, so no barrier is due";
}
ExpectGrid(first, {3, 2, 1}, "glDispatchComputeIndirect at byte 12");
ExpectGrid(second, {4, 1, 2}, "glDispatchComputeIndirect at byte 24");
}
// Counts a compute shader wrote, behind the application's own GL_COMMAND_BARRIER_BIT. The
// store starts with OTHER counts, so a stale read dispatches the wrong grid. On
// Magma/inproc: issued natively and - the point of the change - NO host wait.
TEST_F(WireIndirectDispatchScenario, ComputeWrittenGroupCountsDispatchWithoutAHostWait) {
if (!Ready() || IsSkipped()) return;
Parameters({1, 1, 1});
ComputeWriteParameters({3, 2, 1});
glMemoryBarrier(GL_COMMAND_BARRIER_BIT);
const size_t first = DispatchIndirect(0);
Parameters({1, 1, 1, 1, 1, 1});
ComputeWriteParameters({1, 1, 1, 2, 2, 2});
glMemoryBarrier(GL_COMMAND_BARRIER_BIT);
const size_t second = DispatchIndirect(12);
ASSERT_EQ(FirstGLError(), GLenum(GL_NO_ERROR));
Settle();
if (CountersReadable()) {
const WireIndirectCounters delta = Delta();
EXPECT_EQ(delta.nativeDispatches, 2u) << "both dispatches must be issued natively";
EXPECT_EQ(delta.hostWaits, 0u)
<< "an indirect dispatch of shader-written counts waited for the GPU on the host";
EXPECT_EQ(delta.barriers, 2u) << "one INDIRECT_COMMAND_READ barrier per freshly written store";
}
ExpectGrid(first, {3, 2, 1}, "compute-written counts at byte 0");
ExpectGrid(second, {2, 2, 2}, "compute-written counts at byte 12");
}
// Flywheel's shape, one call over: the counts are written and dispatched with no
// glMemoryBarrier between (GL leaves it undefined). The wire arm orders it anyway, once per
// write, as P8-D does for draws. Magma's split arms only: the monolith arm records no
// dependency here (P8-D recorded that half for dev), and Espryt's is its driver's.
TEST_F(WireIndirectDispatchScenario, ShaderWrittenGroupCountsWithoutAnApplicationBarrierAreOrdered) {
if (!Ready() || IsSkipped()) return;
if (!m_magma || m_transport == "monolith") {
GTEST_SKIP() << "the implicit INDIRECT_COMMAND_READ barrier is the Magma wire arm's (P8-D, P8-SV)";
}
Parameters({1, 1, 1});
ComputeWriteParameters({3, 2, 1});
const size_t first = DispatchIndirect(0);
const size_t again = DispatchIndirect(0);
ComputeWriteParameters({1, 2, 2});
const size_t third = DispatchIndirect(0);
ASSERT_EQ(FirstGLError(), GLenum(GL_NO_ERROR));
Settle();
if (CountersReadable()) {
const WireIndirectCounters delta = Delta();
EXPECT_EQ(delta.nativeDispatches, 3u);
EXPECT_EQ(delta.hostWaits, 0u) << "a host wait was taken";
EXPECT_EQ(delta.barriers, 2u) << "exactly one barrier per write: the second dispatch reads no new write";
}
ExpectGrid(first, {3, 2, 1}, "the first dispatch after the write");
ExpectGrid(again, {3, 2, 1}, "a second dispatch, no write between");
ExpectGrid(third, {1, 2, 2}, "the dispatch after a second write");
}
} // namespace
} // namespace MGITest
@@ -0,0 +1,370 @@
// MobileGL - MobileGL/MG_IntegrationTest/Scenarios/WireIndirectDrawScenario.cpp
// Copyright (c) 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
//
// P8-D: THE MAGMA WIRE ARM ISSUES INDIRECT DRAWS FROM THE WIRE STORE, AS THE MONOLITH ARM DOES.
//
// Before P8-D every wire indirect draw read its commands (and its count word) through
// ReadWireBuffer and expanded them on the CPU, and a store a shader had written turned that read
// into a whole-GPU wait per draw (the create-indirect trace, now
// minecraft-1.21.1-neoforge-create-indirect-in-world-align1024: 321 expansions, 327 waits, ~4-8 s of
// waiting per replay on lavapipe). The cases pin both halves of the change:
// * PIXELS, on every arm and both backends where the backend's own semantics allow it - each
// indirect form draws exactly the strips its commands name, and GPU-written commands are the
// ones the draw sees (the poison the buffers start with names the OTHER strips);
// * THE SERVER'S OWN COUNTERS (Harness/WireIndirectPeek.h), where they can be read - Magma on
// the inproc arm, whose server is this process: every call issued natively, no CPU
// expansion, no host wait, and the INDIRECT_COMMAND_READ barrier exactly once per write.
//
// Four strips across the viewport, one per quarter; a command draws one strip. Green = drawn,
// red = the clear. The VBO opens with three padding vertices so a command that loses its `first`
// or `firstIndex` draws a degenerate triangle rather than the right strip.
#include <array>
#include <cstdint>
#include <string>
#include <vector>
#include "../Harness/HeadlessGL.h"
#include "../Harness/ScenarioFixture.h"
#include "../Harness/SplitRuntimePeek.h"
#include "../Harness/WireIndirectPeek.h"
#ifdef GLAPI
#undef GLAPI
#endif
#define GL_GLEXT_PROTOTYPES
#include <GL/gl.h>
#include <GL/glext.h>
namespace MGITest {
namespace {
constexpr int kStrips = 4;
constexpr std::uint32_t kPad = 3;
constexpr std::uint32_t kStripVertices = 6;
constexpr const char* kVertexSource = R"(#version 430 core
layout(location = 0) in vec2 aPos;
void main() { gl_Position = vec4(aPos, 0.0, 1.0); }
)";
constexpr const char* kFragmentSource = R"(#version 430 core
out vec4 oColor;
void main() { oColor = vec4(0.0, 1.0, 0.0, 1.0); }
)";
// Copies `source` into `words`, one word per invocation: the compute write under test.
constexpr const char* kCopySource = R"(#version 430 core
layout(local_size_x = 1) in;
layout(std430, binding = 0) buffer Words { uint words[]; };
layout(std430, binding = 1) readonly buffer Source { uint source[]; };
void main() { words[gl_GlobalInvocationID.x] = source[gl_GlobalInvocationID.x]; }
)";
struct ArraysCommand {
std::uint32_t count, instanceCount, first, baseInstance;
};
struct ElementsCommand {
std::uint32_t count, instanceCount, firstIndex;
std::int32_t baseVertex;
std::uint32_t baseInstance;
};
// A padded elements command, for the stride case: 32 bytes, the tail unread.
struct PaddedElementsCommand {
ElementsCommand command;
std::uint32_t padding[3];
};
ArraysCommand Arrays(int strip) { return {kStripVertices, 1, kPad + kStripVertices * strip, 0}; }
ElementsCommand Elements(int strip) { return {kStripVertices, 1, kStripVertices * strip, 0, 0}; }
using Strips = std::array<bool, kStrips>;
class WireIndirectDrawScenario : public ScenarioTest {
protected:
void SetUp() override {
ScenarioTest::SetUp();
if (!Ready()) return;
m_magma = Gl().BackendName() == "DirectVulkan";
m_transport = PeekSplitRuntime().transportName;
std::string error;
m_program = CompileProgram(kVertexSource, kFragmentSource, &error);
ASSERT_NE(m_program, 0u) << error;
m_copy = CompileCompute(kCopySource);
ASSERT_NE(m_copy, 0u);
std::vector<float> vertices(2 * kPad, 0.0f);
for (int strip = 0; strip < kStrips; ++strip) {
const float x0 = -1.0f + 0.5f * static_cast<float>(strip);
const float x1 = x0 + 0.5f;
const float quad[12] = {x0, -1.0f, x1, -1.0f, x1, 1.0f, x0, -1.0f, x1, 1.0f, x0, 1.0f};
vertices.insert(vertices.end(), quad, quad + 12);
}
std::vector<std::uint32_t> indices;
for (std::uint32_t i = 0; i < kStripVertices * kStrips; ++i) indices.push_back(kPad + i);
glGenVertexArrays(1, &m_vao);
glBindVertexArray(m_vao);
glGenBuffers(1, &m_vbo);
glBindBuffer(GL_ARRAY_BUFFER, m_vbo);
glBufferData(GL_ARRAY_BUFFER, static_cast<GLsizeiptr>(vertices.size() * sizeof(float)),
vertices.data(), GL_STATIC_DRAW);
glEnableVertexAttribArray(0);
glVertexAttribPointer(0, 2, GL_FLOAT, GL_FALSE, 2 * sizeof(float), nullptr);
glGenBuffers(1, &m_ebo);
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, m_ebo);
glBufferData(GL_ELEMENT_ARRAY_BUFFER, static_cast<GLsizeiptr>(indices.size() * sizeof(std::uint32_t)),
indices.data(), GL_STATIC_DRAW);
ASSERT_EQ(FirstGLError(), GLenum(GL_NO_ERROR)) << "scene setup left a GL error behind";
// The counters are armed by the first peek, so it has to come before any draw.
if (CountersReadable()) {
ASSERT_TRUE(PeekWireIndirectCounters(&m_before))
<< "Magma on the inproc arm must be able to read the wire arm's own counters";
}
}
void TearDown() override {
if (!Ready()) return;
glBindVertexArray(0);
for (GLuint* buffer : {&m_vbo, &m_ebo, &m_indirect, &m_parameter, &m_source}) {
if (*buffer != 0) glDeleteBuffers(1, buffer);
*buffer = 0;
}
if (m_vao != 0) glDeleteVertexArrays(1, &m_vao);
if (m_program != 0) glDeleteProgram(m_program);
if (m_copy != 0) glDeleteProgram(m_copy);
}
static GLuint CompileCompute(const char* source) {
const GLuint shader = glCreateShader(GL_COMPUTE_SHADER);
glShaderSource(shader, 1, &source, nullptr);
glCompileShader(shader);
GLint ok = GL_FALSE;
glGetShaderiv(shader, GL_COMPILE_STATUS, &ok);
if (ok != GL_TRUE) {
glDeleteShader(shader);
return 0;
}
const GLuint program = glCreateProgram();
glAttachShader(program, shader);
glLinkProgram(program);
glDeleteShader(shader);
glGetProgramiv(program, GL_LINK_STATUS, &ok);
if (ok != GL_TRUE) {
glDeleteProgram(program);
return 0;
}
return program;
}
template <typename T>
static void Fill(GLuint& name, GLenum target, const std::vector<T>& data) {
if (name == 0) glGenBuffers(1, &name);
glBindBuffer(target, name);
glBufferData(target, static_cast<GLsizeiptr>(data.size() * sizeof(T)), data.data(), GL_DYNAMIC_COPY);
}
// The compute write: `words` (already holding its poison) receives `data` from the GPU.
template <typename T>
void ComputeWrite(GLuint words, const std::vector<T>& data) {
Fill(m_source, GL_SHADER_STORAGE_BUFFER, data);
glBindBufferBase(GL_SHADER_STORAGE_BUFFER, 0, words);
glBindBufferBase(GL_SHADER_STORAGE_BUFFER, 1, m_source);
glUseProgram(m_copy);
glDispatchCompute(static_cast<GLuint>(data.size() * sizeof(T) / sizeof(std::uint32_t)), 1, 1);
glBindBufferBase(GL_SHADER_STORAGE_BUFFER, 0, 0);
glBindBufferBase(GL_SHADER_STORAGE_BUFFER, 1, 0);
}
template <typename DrawFn>
Image Render(DrawFn&& draw) {
BindDefaultFramebuffer();
glViewport(0, 0, Gl().Width(), Gl().Height());
glDisable(GL_DEPTH_TEST);
ClearTo(1.0f, 0.0f, 0.0f, 1.0f);
glUseProgram(m_program);
glBindVertexArray(m_vao);
glBindBuffer(GL_DRAW_INDIRECT_BUFFER, m_indirect);
draw();
return ReadPixels(Gl().Width(), Gl().Height());
}
static void ExpectStrips(const Image& image, const Strips& drawn, const std::string& what) {
for (int strip = 0; strip < kStrips; ++strip) {
const int x = image.Width() * (2 * strip + 1) / (2 * kStrips);
EXPECT_STREQ(image.ColorName(x, image.Height() / 2), drawn[strip] ? "green" : "red")
<< what << ": strip " << strip;
}
}
// Magma's server in THIS process: the inproc arm. Spawn and tcp servers are other
// processes, and the monolith arm never takes the wire path at all.
bool CountersReadable() const { return m_magma && m_transport == "inproc"; }
WireIndirectCounters Delta() const {
WireIndirectCounters now{};
EXPECT_TRUE(PeekWireIndirectCounters(&now));
return {now.nativeDraws - m_before.nativeDraws, now.cpuExpansions - m_before.cpuExpansions,
now.barriers - m_before.barriers, now.hostWaits - m_before.hostWaits,
now.indirectWaits - m_before.indirectWaits};
}
bool m_magma = false;
std::string m_transport;
WireIndirectCounters m_before{};
GLuint m_program = 0, m_copy = 0, m_vao = 0, m_vbo = 0, m_ebo = 0;
GLuint m_indirect = 0, m_parameter = 0, m_source = 0;
};
// Every indirect form from CPU-written commands. On Magma/inproc the server must have
// issued all six natively, expanded none, waited for nothing and recorded no barrier (no
// shader ever wrote these stores).
TEST_F(WireIndirectDrawScenario, EveryIndirectFormIsIssuedNativelyFromItsStore) {
if (!Ready() || IsSkipped()) return;
// Arrays commands at 0.., elements commands after them: the offsets are byte offsets
// into one store, as an application packs them.
const std::vector<ArraysCommand> arrays = {Arrays(0), Arrays(1), Arrays(3), Arrays(2)};
Fill(m_indirect, GL_DRAW_INDIRECT_BUFFER, arrays);
Image image = Render([&] { glDrawArraysIndirect(GL_TRIANGLES, reinterpret_cast<const void*>(16)); });
ExpectStrips(image, {false, true, false, false}, "glDrawArraysIndirect at byte 16");
image = Render([&] { glMultiDrawArraysIndirect(GL_TRIANGLES, nullptr, 3, 0); });
ExpectStrips(image, {true, true, false, true}, "glMultiDrawArraysIndirect, 3 commands");
const std::vector<PaddedElementsCommand> padded = {{Elements(3), {}}, {Elements(0), {}}};
Fill(m_indirect, GL_DRAW_INDIRECT_BUFFER, padded);
image = Render([&] {
glMultiDrawElementsIndirect(GL_TRIANGLES, GL_UNSIGNED_INT, nullptr, 2, sizeof(PaddedElementsCommand));
});
ExpectStrips(image, {true, false, false, true}, "glMultiDrawElementsIndirect, stride 32");
image = Render([&] {
glDrawElementsIndirect(GL_TRIANGLES, GL_UNSIGNED_INT,
reinterpret_cast<const void*>(sizeof(PaddedElementsCommand)));
});
ExpectStrips(image, {true, false, false, false}, "glDrawElementsIndirect at byte 32");
// The count forms: three commands each, a count word that allows fewer.
Fill(m_parameter, GL_PARAMETER_BUFFER, std::vector<std::uint32_t>{7, 2, 1});
Fill(m_indirect, GL_DRAW_INDIRECT_BUFFER, std::vector<ArraysCommand>{Arrays(0), Arrays(1), Arrays(2)});
image = Render([&] {
glMultiDrawArraysIndirectCount(GL_TRIANGLES, nullptr, 4, 3, 0);
});
ExpectStrips(image, {true, true, false, false}, "glMultiDrawArraysIndirectCount, count word 2 of 3");
Fill(m_indirect, GL_DRAW_INDIRECT_BUFFER, std::vector<ElementsCommand>{Elements(3), Elements(2), Elements(1)});
image = Render([&] {
glMultiDrawElementsIndirectCount(GL_TRIANGLES, GL_UNSIGNED_INT, nullptr, 8, 3, 0);
});
ExpectStrips(image, {false, false, false, true}, "glMultiDrawElementsIndirectCount, count word 1 of 3");
EXPECT_EQ(FirstGLError(), GLenum(GL_NO_ERROR));
if (!CountersReadable()) return;
const WireIndirectCounters delta = Delta();
EXPECT_EQ(delta.nativeDraws, 6u) << "every indirect call must be issued as vkCmdDraw*Indirect[Count]";
EXPECT_EQ(delta.cpuExpansions, 0u) << "no indirect call may read its words on the CPU";
EXPECT_EQ(delta.indirectWaits, 0u) << "an indirect call took a host wait";
EXPECT_EQ(delta.hostWaits, 0u) << "a host wait was taken with no GPU write anywhere";
EXPECT_EQ(delta.barriers, 0u) << "no shader wrote these stores, so no barrier is due";
}
// Commands a compute shader wrote, behind the application's own GL_COMMAND_BARRIER_BIT.
// The buffers start with commands for the OTHER strips, so a read of stale words shows.
// On Magma/inproc: issued natively, and - the point of the package - NO host wait.
TEST_F(WireIndirectDrawScenario, ComputeWrittenCommandsDrawWithoutAHostWait) {
if (!Ready() || IsSkipped()) return;
Fill(m_indirect, GL_DRAW_INDIRECT_BUFFER, std::vector<ArraysCommand>{Arrays(2), Arrays(3)});
ComputeWrite(m_indirect, std::vector<ArraysCommand>{Arrays(0), Arrays(1)});
glMemoryBarrier(GL_COMMAND_BARRIER_BIT);
Image image = Render([&] { glMultiDrawArraysIndirect(GL_TRIANGLES, nullptr, 2, 0); });
ExpectStrips(image, {true, true, false, false}, "glMultiDrawArraysIndirect of compute-written commands");
Fill(m_indirect, GL_DRAW_INDIRECT_BUFFER, std::vector<ElementsCommand>{Elements(0), Elements(1)});
ComputeWrite(m_indirect, std::vector<ElementsCommand>{Elements(3), Elements(2)});
glMemoryBarrier(GL_COMMAND_BARRIER_BIT);
image = Render([&] { glMultiDrawElementsIndirect(GL_TRIANGLES, GL_UNSIGNED_INT, nullptr, 2, 0); });
ExpectStrips(image, {false, false, true, true}, "glMultiDrawElementsIndirect of compute-written commands");
EXPECT_EQ(FirstGLError(), GLenum(GL_NO_ERROR));
if (!CountersReadable()) return;
const WireIndirectCounters delta = Delta();
EXPECT_EQ(delta.nativeDraws, 2u) << "both draws must be issued natively";
EXPECT_EQ(delta.cpuExpansions, 0u) << "no indirect call may read its words on the CPU";
EXPECT_EQ(delta.indirectWaits, 0u)
<< "an indirect draw of shader-written commands waited for the GPU on the host";
EXPECT_EQ(delta.hostWaits, 0u) << "a host wait was taken";
EXPECT_EQ(delta.barriers, 2u) << "one INDIRECT_COMMAND_READ barrier per freshly written store";
}
// The COUNT word a compute shader wrote. Espryt reads that word on the CPU from the
// server's staged copy of the parameter buffer; P8-C refreshes that copy after a shader
// write (SplitHostBytesForCpuRead), so its split arms answer here too (P8-SE). Espryt's
// MONOLITH arm still reads the frontend shadow without a sync (OPEN-QUESTIONS 15, C.md's
// monolith defects), left to P13 (ID-P8-13).
TEST_F(WireIndirectDrawScenario, ComputeWrittenCountWordIsTheOneTheGpuReads) {
if (!Ready() || IsSkipped()) return;
if (!m_magma && m_transport == "monolith") {
GTEST_SKIP() << "Espryt's monolith arm reads the count word from the frontend shadow without "
"a sync (OQ15; recorded for P13, ID-P8-13)";
}
Fill(m_indirect, GL_DRAW_INDIRECT_BUFFER, std::vector<ElementsCommand>{Elements(0), Elements(3), Elements(2)});
Fill(m_parameter, GL_PARAMETER_BUFFER, std::vector<std::uint32_t>{3});
ComputeWrite(m_parameter, std::vector<std::uint32_t>{1});
glMemoryBarrier(GL_COMMAND_BARRIER_BIT);
Image image = Render([&] {
glMultiDrawElementsIndirectCount(GL_TRIANGLES, GL_UNSIGNED_INT, nullptr, 0, 3, 0);
});
ExpectStrips(image, {true, false, false, false}, "glMultiDrawElementsIndirectCount, shader-written count 1");
if (m_transport == "monolith") {
// DirectVulkan.cpp's monolith MultiDrawArraysIndirectCount reads this word from the
// frontend shadow without SyncGpuWrites: recorded for dev (ID-P8-3), not this lane's.
return;
}
Fill(m_indirect, GL_DRAW_INDIRECT_BUFFER, std::vector<ArraysCommand>{Arrays(1), Arrays(2), Arrays(3)});
Fill(m_parameter, GL_PARAMETER_BUFFER, std::vector<std::uint32_t>{0});
ComputeWrite(m_parameter, std::vector<std::uint32_t>{2});
glMemoryBarrier(GL_COMMAND_BARRIER_BIT);
image = Render([&] { glMultiDrawArraysIndirectCount(GL_TRIANGLES, nullptr, 0, 3, 0); });
ExpectStrips(image, {false, true, true, false}, "glMultiDrawArraysIndirectCount, shader-written count 2");
EXPECT_EQ(FirstGLError(), GLenum(GL_NO_ERROR));
if (!CountersReadable()) return;
const WireIndirectCounters delta = Delta();
EXPECT_EQ(delta.nativeDraws, 2u);
EXPECT_EQ(delta.cpuExpansions, 0u) << "a count word was read on the CPU";
EXPECT_EQ(delta.hostWaits, 0u) << "a host wait was taken";
EXPECT_EQ(delta.barriers, 2u) << "one barrier per draw whose count store a shader wrote";
}
// Flywheel's shape (minecraft-1.21.1-neoforge-create-indirect-in-world-align1024: 322
// cull-then-draw rounds, no glMemoryBarrier between them). GL leaves it undefined; the
// wire arm orders it anyway, once per write,
// because the CPU path it replaced did (by waiting). Magma's split arms only: the monolith
// arm records no dependency here (recorded for dev), and Espryt's is its driver's.
TEST_F(WireIndirectDrawScenario, ShaderWrittenCommandsWithoutAnApplicationBarrierAreOrdered) {
if (!Ready() || IsSkipped()) return;
if (!m_magma || m_transport == "monolith") {
GTEST_SKIP() << "the implicit INDIRECT_COMMAND_READ barrier is the Magma wire arm's (P8-D)";
}
Fill(m_indirect, GL_DRAW_INDIRECT_BUFFER, std::vector<ArraysCommand>{Arrays(3), Arrays(2)});
ComputeWrite(m_indirect, std::vector<ArraysCommand>{Arrays(0), Arrays(1)});
Image image = Render([&] { glMultiDrawArraysIndirect(GL_TRIANGLES, nullptr, 2, 0); });
ExpectStrips(image, {true, true, false, false}, "the first draw after the write");
image = Render([&] { glDrawArraysIndirect(GL_TRIANGLES, reinterpret_cast<const void*>(16)); });
ExpectStrips(image, {false, true, false, false}, "a second draw, no write between");
ComputeWrite(m_indirect, std::vector<ArraysCommand>{Arrays(2), Arrays(3)});
image = Render([&] { glMultiDrawArraysIndirect(GL_TRIANGLES, nullptr, 2, 0); });
ExpectStrips(image, {false, false, true, true}, "the draw after a second write");
EXPECT_EQ(FirstGLError(), GLenum(GL_NO_ERROR));
if (!CountersReadable()) return;
const WireIndirectCounters delta = Delta();
EXPECT_EQ(delta.nativeDraws, 3u);
EXPECT_EQ(delta.cpuExpansions, 0u);
EXPECT_EQ(delta.hostWaits, 0u) << "a host wait was taken";
EXPECT_EQ(delta.barriers, 2u) << "exactly one barrier per write: the second draw reads no new write";
}
} // namespace
} // namespace MGITest
@@ -0,0 +1,220 @@
// MobileGL - MobileGL/MG_IntegrationTest/Scenarios/WireMipShapeScenario.cpp
// Copyright (c) 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
//
// P8-SV: TWO glGenerateMipmap SHAPES THE MAGMA WIRE ARM GOT WRONG (docs/Disaggregated/notes/p8/SV.md).
//
// * A 3D chain through the NATIVE blit. The wire arm passed the level's depth as the blit's
// subresource layerCount on an image with one array layer (VUID-vkCmdBlitImage-srcImage-00240,
// -srcSubresource-01707, -dstSubresource-01708). Lavapipe blits a 3D image by its z offsets and
// ignores layerCount, so the PIXELS below are right with or without the fix on this host; what
// goes red on the host is the validation layer, which the `.Mip3DValidation.` entry runs this
// case under (inproc, so the server's Vulkan instance is this process and its stdout is ours).
// The pixel half runs on both backends and every arm, and is what a driver that honours
// layerCount would fail.
// * A 1D DEPTH chain with no native blit. The depth shader arm renders 2D views, which a 1D image
// does not admit; the wire arm handed it the chain anyway. It is now the named decline
// MipmapShaderFormatOrShape, decided before the chain grows, and the session carries on.
// Magma only, and reachable on this host only with MGITEST_MAGMA_FORCE_SHADER_MIPMAP (lavapipe
// blits depth natively), which the SERVER reads: split and spawn entries only.
#include <array>
#include <cstdint>
#include <fstream>
#include <string>
#include <vector>
#include "../Harness/HeadlessGL.h"
#include "../Harness/ScenarioFixture.h"
#include "../Harness/SplitLane.h"
#include "../Harness/SplitRuntimePeek.h"
#include "../Harness/WireDeclinePeek.h"
#ifdef GLAPI
#undef GLAPI
#endif
#define GL_GLEXT_PROTOTYPES
#include <GL/gl.h>
#include <GL/glext.h>
namespace MGITest {
namespace {
using Texel = std::array<int, 4>;
// Is the Khronos validation layer loaded into THIS process? On the inproc arm the server's
// Vulkan instance lives here, so this is the proof that a run "under the layer" really was.
bool ValidationLayerMapped() {
#if defined(__linux__)
std::ifstream maps("/proc/self/maps");
std::string line;
while (std::getline(maps, line)) {
if (line.find("VkLayer_khronos_validation") != std::string::npos) return true;
}
#endif
return false;
}
class WireMipShapeScenario : public ScenarioTest {
protected:
void SetUp() override {
ScenarioTest::SetUp();
if (!Ready()) return;
m_magma = Gl().BackendName() == "DirectVulkan";
m_transport = PeekSplitRuntime().transportName;
}
// The Magma server's decline tally is readable where the server is this process.
bool DeclinesReadable() const { return m_magma && m_transport == "inproc"; }
bool m_magma = false;
std::string m_transport;
};
// A 3D RGBA8 chain, 8x8x8 with its full storage, whose level-0 slices come in PAIRS of one
// colour: level 1's slice k is then pair k's colour exactly, and every level below is the
// average of two slices of the level above. A blit that loses the z axis (one slice only,
// or slices read as array layers of a 2D image) cannot produce those numbers.
TEST_F(WireMipShapeScenario, VolumeChainIsGeneratedAlongItsDepth) {
if (!Ready() || IsSkipped()) return;
constexpr int kSide = 8, kLevels = 4;
const std::array<Texel, 4> pairs = {Texel{200, 40, 80, 255}, Texel{40, 200, 120, 255},
Texel{100, 100, 240, 255}, Texel{240, 160, 0, 255}};
GLuint volume = 0;
glGenTextures(1, &volume);
glBindTexture(GL_TEXTURE_3D, volume);
glTexStorage3D(GL_TEXTURE_3D, kLevels, GL_RGBA8, kSide, kSide, kSide);
std::vector<GLubyte> level0(static_cast<size_t>(kSide * kSide * kSide * 4));
for (int z = 0; z < kSide; ++z) {
const Texel& texel = pairs[static_cast<size_t>(z / 2)];
for (int at = 0; at < kSide * kSide; ++at) {
for (int c = 0; c < 4; ++c)
level0[static_cast<size_t>((z * kSide * kSide + at) * 4 + c)] = static_cast<GLubyte>(texel[c]);
}
}
glPixelStorei(GL_UNPACK_ALIGNMENT, 4);
glTexSubImage3D(GL_TEXTURE_3D, 0, 0, 0, 0, kSide, kSide, kSide, GL_RGBA, GL_UNSIGNED_BYTE, level0.data());
glTexParameteri(GL_TEXTURE_3D, GL_TEXTURE_MIN_FILTER, GL_LINEAR_MIPMAP_LINEAR);
ASSERT_EQ(FirstGLError(), GLenum(GL_NO_ERROR)) << "3D texture setup";
glGenerateMipmap(GL_TEXTURE_3D);
ASSERT_EQ(FirstGLError(), GLenum(GL_NO_ERROR)) << "glGenerateMipmap(GL_TEXTURE_3D)";
const auto average = [](const Texel& a, const Texel& b) {
return Texel{(a[0] + b[0]) / 2, (a[1] + b[1]) / 2, (a[2] + b[2]) / 2, (a[3] + b[3]) / 2};
};
// expected[level][slice]
std::vector<std::vector<Texel>> expected(kLevels);
expected[1].assign(pairs.begin(), pairs.end());
expected[2] = {average(pairs[0], pairs[1]), average(pairs[2], pairs[3])};
expected[3] = {average(expected[2][0], expected[2][1])};
glPixelStorei(GL_PACK_ALIGNMENT, 4);
for (int level = 1; level < kLevels; ++level) {
const int side = kSide >> level;
GLint width = 0, height = 0, depth = 0;
glGetTexLevelParameteriv(GL_TEXTURE_3D, level, GL_TEXTURE_WIDTH, &width);
glGetTexLevelParameteriv(GL_TEXTURE_3D, level, GL_TEXTURE_HEIGHT, &height);
glGetTexLevelParameteriv(GL_TEXTURE_3D, level, GL_TEXTURE_DEPTH, &depth);
EXPECT_EQ(width, side) << "level " << level;
EXPECT_EQ(height, side) << "level " << level;
EXPECT_EQ(depth, side) << "level " << level;
std::vector<GLubyte> pixels(static_cast<size_t>(side * side * side * 4), 0x5a);
glGetTexImage(GL_TEXTURE_3D, level, GL_RGBA, GL_UNSIGNED_BYTE, pixels.data());
ASSERT_EQ(FirstGLError(), GLenum(GL_NO_ERROR)) << "readback of level " << level;
for (int z = 0; z < side; ++z) {
const Texel& want = expected[static_cast<size_t>(level)][static_cast<size_t>(z)];
size_t wrong = 0;
Texel first{};
for (int at = 0; at < side * side; ++at) {
const size_t base = static_cast<size_t>((z * side * side + at) * 4);
bool ok = true;
for (int c = 0; c < 4; ++c) {
const int got = pixels[base + static_cast<size_t>(c)];
if (got < want[c] - 2 || got > want[c] + 2) ok = false;
}
if (!ok && wrong++ == 0) {
for (int c = 0; c < 4; ++c) first[c] = pixels[base + static_cast<size_t>(c)];
}
}
EXPECT_EQ(wrong, 0u) << "level " << level << " slice " << z << ": expected (" << want[0] << ","
<< want[1] << "," << want[2] << "," << want[3] << "), first wrong texel ("
<< first[0] << "," << first[1] << "," << first[2] << "," << first[3] << ")";
}
}
glBindTexture(GL_TEXTURE_3D, 0);
glDeleteTextures(1, &volume);
// The `.Mip3DValidation.` entry: the blit above ran under the Khronos validation layer,
// whose VUID lines the entry's FAIL_REGULAR_EXPRESSION reads. Without the layer actually
// loaded, a clean output would prove nothing, so the entry requires it.
if (SplitLane::MarkerIsOne("MGITEST_REQUIRE_VK_VALIDATION_LAYER")) {
ASSERT_TRUE(m_magma && m_transport == "inproc")
<< "the validation entry reads the server's layer output in-process: Magma, inproc";
EXPECT_TRUE(ValidationLayerMapped())
<< "VK_LAYER_KHRONOS_validation is not loaded in this process, so the absence of "
"VUID lines in this entry's output says nothing";
}
}
// A 1D DEPTH chain with the native blit forced off: no mip arm renders a 1D image, so the
// wire arm declines by name (MipmapShaderFormatOrShape) before growing the chain. Pinned:
// no GL error, the session answering (two readback round trips), level 0 as uploaded, level
// 1 still the application's own sentinel - and, where the server is this process, exactly
// one MipmapShaderFormatOrShape decline counted.
TEST_F(WireMipShapeScenario, Depth1DChainWithoutANativeBlitDeclinesByName) {
if (!Ready() || IsSkipped()) return;
if (!m_magma)
GTEST_SKIP() << "Magma wire-arm decline (P8-SV): the knob and the depth shader arm it forces are "
"Magma's; Espryt (" << Gl().BackendName() << ") generates this chain itself";
const std::string tier = SplitLane::MarkerValue("MGITEST_MAGMA_FORCE_SHADER_MIPMAP");
if (tier.empty())
GTEST_SKIP() << "MGITEST_MAGMA_FORCE_SHADER_MIPMAP is unset: this driver blits depth natively, "
"so the depth shader arm this case refuses is not entered";
constexpr int kWidth = 16;
GLuint line = 0;
glGenTextures(1, &line);
glBindTexture(GL_TEXTURE_1D, line);
glTexStorage1D(GL_TEXTURE_1D, 5, GL_DEPTH_COMPONENT32F, kWidth);
const std::vector<GLfloat> level0(kWidth, 0.25f), level1(kWidth / 2, 0.75f);
glPixelStorei(GL_UNPACK_ALIGNMENT, 4);
glTexSubImage1D(GL_TEXTURE_1D, 0, 0, kWidth, GL_DEPTH_COMPONENT, GL_FLOAT, level0.data());
glTexSubImage1D(GL_TEXTURE_1D, 1, 0, kWidth / 2, GL_DEPTH_COMPONENT, GL_FLOAT, level1.data());
ASSERT_EQ(FirstGLError(), GLenum(GL_NO_ERROR)) << "1D depth texture setup";
unsigned long long declinesBefore = 0;
const bool counted =
DeclinesReadable() && PeekWireDeclineCount("MipmapShaderFormatOrShape", &declinesBefore);
glGenerateMipmap(GL_TEXTURE_1D);
EXPECT_EQ(FirstGLError(), GLenum(GL_NO_ERROR)) << "glGenerateMipmap(GL_TEXTURE_1D) on a depth chain";
glPixelStorei(GL_PACK_ALIGNMENT, 4);
for (const int level : {0, 1}) {
const int width = kWidth >> level;
std::vector<GLfloat> depths(static_cast<size_t>(width), -1.0f);
glGetTexImage(GL_TEXTURE_1D, level, GL_DEPTH_COMPONENT, GL_FLOAT, depths.data());
ASSERT_EQ(FirstGLError(), GLenum(GL_NO_ERROR)) << "readback of level " << level;
const GLfloat want = level == 0 ? 0.25f : 0.75f;
size_t wrong = 0;
GLfloat first = 0.0f;
for (const GLfloat depth : depths) {
if (depth < want - 0.0001f || depth > want + 0.0001f) {
if (wrong++ == 0) first = depth;
}
}
EXPECT_EQ(wrong, 0u) << "level " << level << " tier=" << tier << ": a declined mip must leave "
<< "every level as the application wrote it (expected " << want
<< ", first wrong " << first << ")";
}
glBindTexture(GL_TEXTURE_1D, 0);
glDeleteTextures(1, &line);
if (counted) {
unsigned long long declinesAfter = 0;
ASSERT_TRUE(PeekWireDeclineCount("MipmapShaderFormatOrShape", &declinesAfter));
EXPECT_EQ(declinesAfter - declinesBefore, 1u)
<< "the 1D depth chain must be refused once, by name, before the chain grows";
}
}
} // namespace
} // namespace MGITest
+4 -1
View File
@@ -163,7 +163,10 @@
X(QueryDestroy, DeleteBackendQuery) \
X(QueryCounter, QueryCounterTimestamp) \
X(QueryTimestamp, GetGpuTimestampNs) \
X(DeleteStreamOutput, DeleteTransformFeedback)
X(DeleteStreamOutput, DeleteTransformFeedback) \
/* P9 (CONTRACT-P9.md §1): the pack-buffer halves stamp as the verb they reproduce. */ \
X(ReadPixelsToBuffer, ReadPixels) \
X(GetTextureImageToBuffer, GetTextureImage)
// X(Op, Why) - verb-shaped calls that are deliberately NOT stamp points.
//
+22 -14
View File
@@ -15,12 +15,20 @@
// The backend -> frontend reverse channel, named (plan B section 7.1).
//
// Today this traffic is 95 call sites across 17 methods poked directly into frontend
// objects. gallium has no vocabulary for shadow writeback, GPU-write notification, texture
// re-send requests or default-framebuffer geometry, because in Mesa the state tracker and
// the driver share an address space. Naming them as nine callbacks plus one forward
// terminator (MGPipeContext::ResourceSubDataComplete) is the deliberate deviation (D8).
// objects. gallium has no vocabulary for shadow writeback, GPU-write notification or
// default-framebuffer geometry, because in Mesa the state tracker and the driver share an
// address space. Naming them as callbacks is the deliberate deviation (D8).
//
// NINE, NOT THE TEN PLAN B WROTE. The tenth was OnXfbScatterReady, and it went with the
// SEVEN, NOT THE TEN PLAN B WROTE. Three went, each because nothing produces it.
//
// OnTexturePullRequest, the server asking the client to re-send a texture's levels after
// re-minting it (with op 50 ResourceSubDataComplete as the forward terminator). P9 W2 measured
// that no re-mint needs the client: Espryt's server reads the GPU level back and replays its own
// staged store, Magma copies GPU to GPU, on every arm (notes/p9/W2-REMINT.md). So the callback
// went; op 50 stays in the opcode table as a retired row (opcodes only ever grow) and its decoder
// still declines it.
//
// OnXfbScatterReady went with the
// design it belonged to: plan B put the XFB scatter on the CLIENT (the server would hand
// back the packed scratch and the client would run the patch loop over its own shadow), so
// there had to be a callback that told the client the layout. P5c/P5f went the other way -
@@ -31,6 +39,13 @@
// reader of this file has to rule out by hand, and the size assertion below made it look
// load-bearing.
//
// And OnLog, deleted in P9 W3 for the same reason: zero producers, zero consumers, no
// EventKind. Plan B pictured the server's log lines as reverse-channel records; they never
// were. They travel the CONTROL plane as LogLine frames (protocol.fbs), written by the server's
// logger through its log forward (MG_Remote/Transport/LogForward.h) - which is also where the
// severity policy this entry's comment promised ("<= WARN lossy, >= ERROR lossless and rate
// limited") now lives, on the level LogLine has always carried.
//
// Installed at context creation. In a monolith these are direct calls; under split they are
// records on the reverse channel, and their ORDER is a correctness requirement rather than
// an optimization (section 7.4).
@@ -43,11 +58,6 @@ namespace MobileGL::MG_Pipe {
void (*OnGpuWritten)(MGPipeHandle res, Uint rangeCount, const MGPRange* ranges);
void (*OnBufferWriteback)(MGPipeHandle res, Uint64 offset, MGPBlobRef bytes);
void (*OnTextureWriteback)(MGPipeHandle res, const MGPBox* box, MGPBlobRef bytes);
// The one new stall class in this design (D-B6): the server recast a texture and
// needs its texels back. The client answers with zero or more ResourceSubData
// records terminated by ResourceSubDataComplete carrying the same pullSerial.
void (*OnTexturePullRequest)(MGPipeHandle res, Uint16 target, Uint16 firstLevel, Uint16 levelCount,
Uint64 pullSerial);
// SHAPE ONLY, never bytes: the client owns the CPU shadow and allocates the levels
// itself.
void (*OnMipLevelsGenerated)(MGPipeHandle res, Uint16 base, Uint16 count);
@@ -55,13 +65,11 @@ namespace MobileGL::MG_Pipe {
// pDefaultFramebufferInfo.
void (*OnSurfaceChanged)(const MGPSurfaceInfo* info);
void (*OnCapsInvalidated)();
// <= WARN is lossy, >= ERROR is lossless and rate limited.
void (*OnLog)(Uint8 level, const char* text);
};
// Nine, and the count is asserted so a tenth cannot be added without touching the
// Seven, and the count is asserted so an eighth cannot be added without touching the
// transport's reverse-channel record table.
inline constexpr SizeT kMGPipeCallbackCount = 9;
inline constexpr SizeT kMGPipeCallbackCount = 7;
static_assert(sizeof(MGPipeCallbacks) == kMGPipeCallbackCount * sizeof(void (*)()),
"MGPipeCallbacks gained or lost a callback");
+7 -7
View File
@@ -13,16 +13,16 @@
//
// Monolith: Ptr addresses the frontend shadow or the application's own memory and the
// accessor is one predictable branch. Split: Ptr is null and the bytes live in a staging
// segment named by Seg/Offset, or - for the index bytes a server-side primitive-restart
// rewrite or multi-draw flattening consumes - in the server's own index host mirror, which
// costs no wire traffic at all (D-B7).
// segment named by Seg/Offset. D-B7's third shape - index bytes a server-side restart
// rewrite reads out of its own index host mirror, at no wire cost - was never built; its
// sentinel below is RESERVED (P8-F).
namespace MobileGL::MG_Pipe {
// Seg sentinels. Anything else is a real SEG_STAGE id assigned by the transport.
inline constexpr Uint32 kMGHostSpanSegNone = 0;
// "The bytes are already on your side": the server reads them out of the index host
// mirror it maintains for every resource created with the ELEMENT_ARRAY bind bit while
// kCapNeedsHostIndexBytes is set. When the mirror is over budget the tracker degrades
// to per-draw staging and counts the bytes in index-bytes-shipped.
// RESERVED (P8-F). It was to mean "the bytes are already on your side", read out of an
// index host mirror armed by kCapNeedsHostIndexBytes. Neither was built (client indices
// travel as owned element buffers; the cap is reserved), so no encoder writes this value
// and SegmentTable::Resolve answers it "unknown". Kept so the value is never reused.
inline constexpr Uint32 kMGHostSpanSegFromServerIndexMirror = 0xFFFFFFFFu;
struct MGHostSpan {

Some files were not shown because too many files have changed in this diff Show More