[MG_Backend] (Disaggregated): P5e pg - the program twin is keyed by the ShaderCso handle

- BackendProgramObjectImpl::SyncToBackendByHandle is real. It and the frontend overload
  feed ONE body (SyncToBackendFromSource) through ProgramArchiveSource, a view whose
  accessors are ProgramObject's names one for one - so the body is written once and the
  PULL build compiles the same text against a ProgramObject. In a pull build
  ProgramBuildSource IS ProgramObject, the function keeps its own name and its null
  guard, and G1's byte identity survives a rename and nothing else.
- The view answers the three POST-LINK MUTABLE reflection fields from the record's
  set_program_bindings tails and everything else from the record's archive. The tails are
  a whole-set replacement, so a location the sampler tail does not name has NO unit - not
  whatever the link-time snapshot said - and "has a bindings record been applied at all"
  is BindingsSerial != 0 rather than a non-empty tail.
- The nine-clause clean condition keeps its clause count and moves its inputs (D-H5):
  record.Serial vs the m_syncedShaderCsoSerial stamp P4a landed and nothing has read
  until now, BindingsSerial vs a new m_syncedBindingsSerial (a glUniform1i on an IMAGE
  uniform is baked into the ESSL, so the record that carries it must force a rebuild),
  record.Signature, Desc.LinkStatus and Desc.SpirvStatus.
- The signature has ONE authority per arm: the handle arm stamps and compares the
  CLIENT's number verbatim, so the two commutative hashes never have to stay
  bit-identical for a program with an override to stop rebuilding every draw.
- DirectGLES: SyncCurrentProgramByHandle, the stash re-keyed on the handle,
  PrepareForDraw/PrepareForCompute reading MGPipeApplier().DrawProgram/DispatchProgram,
  BindCurrentProgramWithResources and GetCurrentBackendProgram on the handle arm, the
  block binding and the sampler unit from the record's tails, and the sampler-pass memo
  on BindingsSerial instead of the frontend's backend-state version.
- MapUBO() has no run-ahead answer and now says so: every decline of
  ResolveGlobalConstantsRecord under a transport is Fatal{UnmigratedVerb,
  "set_global_constants"} rather than a torn read of the client's live globalUboScratch.
- The fragColor broadcast count takes a POLICY, because a dispatch is a third case: it
  has no draw framebuffer and no fragment stage, PrepareForCompute runs no SyncCurrentFBO,
  so it may neither fall back to a client row nor abort when the records are untrusted.
- Three of the family's five named frontend-keyed scope sites are now unreachable under a
  transport (ResolveGlobalConstantsRecord, SyncCurrentProgram,
  BindCurrentProgramWithResources). The other two are kept BY CONTRACT (§5.5):
  GetBackendProgramId is a GL-thread entry point and ShaderStorageBlockBinding is reached
  from the barriered set_storage_block_binding, which is pg's trailing item.
This commit is contained in:
2026-09-18 08:14:05 -04:00
parent 39d0677616
commit c5491ca675
3 changed files with 1127 additions and 240 deletions
+448 -121
View File
@@ -4096,6 +4096,20 @@ namespace MobileGL::MG_Backend::DirectGLES {
// never be consumed (the stale pair is overwritten before any consumer runs).
static const MG_State::GLState::ProgramObject* g_currentDrawFrontendProgram = nullptr;
static BackendProgramObjectImpl* g_currentDrawBackendProgram = nullptr;
#if MOBILEGL_PIPE_PUSH
// P5e (pg), CONTRACT-P5E.md §5.5: THE SAME STASH, KEYED ON THE HANDLE. The raw frontend
// pointer above is one of the five frontend-keyed reads this family retires: every
// consumer compared it against GetProgramForDraw().get(), which under run-ahead is a
// pointer to an object the client may already have relinked or freed. The handle arm
// compares against MGPipeApplier().DrawProgram instead - a {slot, gen} the record
// carried, which cannot be recycled behind the server's back because the gen moves with
// the slot.
//
// BOTH HALVES SURVIVE because the two arms do (ruling 1): the pointer is the monolith
// glue's key and the handle is the transport's, and SyncCurrentProgram* clears the one
// it does not use so a stale key can never be consumed.
static MG_Pipe::MGPipeHandle g_currentDrawProgramHandle = MG_Pipe::kMGPipeNullHandle;
#endif
// Memo of the per-draw enabled-draw-buffers walk feeding g_fragColorBroadcastCount:
// the answer is a pure function of WHICH FBO is bound and its draw-buffer edits, so
@@ -4227,19 +4241,13 @@ namespace MobileGL::MG_Backend::DirectGLES {
// the upload copies GetUBOSize() bytes and a short record would read past it.
//
// Anything else and the frontend's own MapUBO answers, exactly as it does today.
static const MG_Pipe::MGPipeShaderCsoRecord* ResolveGlobalConstantsRecord(
const MG_State::GLState::ProgramObject* program) {
#if MOBILEGL_BUILD_DISAGGREGATED
// P5c (G6, CONTRACT-P5C §5.4): the program's handle is resolved by frontend
// identity below - frontend-keyed twin resolution, named debt inside the scope -
// P3b/P4b rekeys the registry onto handles.
const MG_Pipe::MGPipeFrontendKeyedRegistryScope frontendKeyedRegistry;
#endif
// P4a decline-site P1: M - the mask says this family is not switched on, or there
// is no current program at all; it IS D's ProgramSubsystemEnabled() now. Silent,
// confirmed at the verification round.
if (!ProgramSubsystemEnabled() || program == nullptr) return nullptr;
const MG_Pipe::MGPipeHandle handle = g_backendProgramObjects.HandleOf(program);
// P5e (pg): `uboSize` is what the block image is checked against, and it is a PARAMETER
// now rather than a `program->GetUBOSize()` read inside the body - on the handle arm the
// size is the record's own Desc.GlobalUboSize and there is no ProgramObject to ask.
// `program` is null on that arm and is used for nothing but the monolith identity
// resolution below.
static const MG_Pipe::MGPipeShaderCsoRecord* ResolveGlobalConstantsRecordForHandle(
MG_Pipe::MGPipeHandle handle, Uint uboSize) {
const MG_Pipe::MGPipeShaderCsoRecord* const record = FindShaderCsoRecord(handle);
// P4a decline-site P4: S - FOLDED INTO P2/P3 AT THE VERIFICATION ROUND, which is
// what the review asked for and is why this one stays silent: both reasons
@@ -4277,7 +4285,7 @@ namespace MobileGL::MG_Backend::DirectGLES {
"of a program with a %u-byte default uniform block; running the "
"frontend's own uniform block.",
static_cast<unsigned>(handle.Slot), static_cast<unsigned>(handle.Gen),
static_cast<unsigned>(program->GetUBOSize()));
static_cast<unsigned>(uboSize));
return nullptr;
}
// P4a decline-site P7: S, and LOUD (ID-19) - this one is not a missing record,
@@ -4294,7 +4302,7 @@ namespace MobileGL::MG_Backend::DirectGLES {
// defect has to be caught - never reach MapUBO() at all, and a shipped build does
// not abort a running game over a client-side bug. If the integrator wants the
// stop on every arm, it is one #if away and this comment is where to say so.
if (record->GlobalConstants.size() < static_cast<SizeT>(program->GetUBOSize())) {
if (record->GlobalConstants.size() < static_cast<SizeT>(uboSize)) {
#if MOBILEGL_PIPE_POISON || MOBILEGL_PIPE_VERIFY
MGLOG_F("MGPipe: Fatal{ProtocolCorruption} "
"A program record does not describe the binding it names: the ShaderCso "
@@ -4302,7 +4310,7 @@ namespace MobileGL::MG_Backend::DirectGLES {
"%llu-byte default uniform block.",
static_cast<unsigned>(handle.Slot), static_cast<unsigned>(handle.Gen),
static_cast<unsigned long long>(record->GlobalConstants.size()),
static_cast<unsigned long long>(program->GetUBOSize()));
static_cast<unsigned long long>(uboSize));
std::abort();
#else
MGLOG_E_ONCE("A program record does not describe the binding it names: the ShaderCso "
@@ -4310,13 +4318,133 @@ namespace MobileGL::MG_Backend::DirectGLES {
"%llu-byte default uniform block; running the frontend's own uniform block.",
static_cast<unsigned>(handle.Slot), static_cast<unsigned>(handle.Gen),
static_cast<unsigned long long>(record->GlobalConstants.size()),
static_cast<unsigned long long>(program->GetUBOSize()));
static_cast<unsigned long long>(uboSize));
#endif
return nullptr;
}
return record;
}
// THE MONOLITH-GLUE HALF (ruling 1 / ID-81), unchanged in what it does: resolve the
// program's handle by frontend identity inside the named scope, then ask the body above.
// Reached only when `Transport == Monolith`, which is what keeps the push-monolith build
// token for token.
static const MG_Pipe::MGPipeShaderCsoRecord* ResolveGlobalConstantsRecord(
const MG_State::GLState::ProgramObject* program) {
#if MOBILEGL_BUILD_DISAGGREGATED
// P5c (G6, CONTRACT-P5C §5.4): frontend-keyed twin resolution, named debt inside the
// scope. P5e does not delete it - it stops REACHING it under a transport, which is
// the thing the allocator guard measures.
const MG_Pipe::MGPipeFrontendKeyedRegistryScope frontendKeyedRegistry;
#endif
// P4a decline-site P1: M - the mask says this family is not switched on, or there
// is no current program at all; it IS D's ProgramSubsystemEnabled() now. Silent,
// confirmed at the verification round.
if (!ProgramSubsystemEnabled() || program == nullptr) return nullptr;
return ResolveGlobalConstantsRecordForHandle(g_backendProgramObjects.HandleOf(program),
program->GetUBOSize());
}
#endif
// Read from the frontend rather than from the backend framebuffer sync, which
// only runs later in PrepareForDraw: a program compiled against a stale count
// would not be relinked until the draw after the one that needed it.
//
// P5e (pg): ITS OWN FUNCTION AND ITS OWN POLICY, because the three callers want three
// different answers when the record arm has nothing to say:
//
// FrontendFallback - the monolith arm, unchanged: read the draw-FBO binding slot.
// RefuseFallback - the handle arm AT A DRAW. With a live wire that binding slot is a
// BARRIER_PULLED row the client may already have moved, so a count
// read from it would compile the program against a framebuffer that
// is not the one being drawn to - silently, and only sometimes.
// The record is the answer or there is no answer (rule F).
// RecordOnly - the handle arm AT A DISPATCH. A compute program has no fragment
// stage and a dispatch has no draw framebuffer, so this count is not
// an input to what is being built; PrepareForCompute also does not
// run SyncCurrentFBO, so the framebuffer records are legitimately
// untrusted here and a refusal would abort a correct program. The
// previous resolution stands, which is what the monolith arm
// effectively does too when nothing rebound between the two calls.
enum class BroadcastCountPolicy { FrontendFallback, RefuseFallback, RecordOnly };
static void ResolveFragColorBroadcastCount(BroadcastCountPolicy policy) {
Bool broadcastCountResolved = false;
#if MOBILEGL_PIPE_PUSH
// The trust latch is fresh here and costs nothing: PrepareForDraw runs
// SyncCurrentFBO immediately before this, so the records have just been
// checked against the two bindings.
if (FramebufferSubsystemEnabled() && FramebufferImpl::g_fboRecordsTrusted) {
const MG_Pipe::MGPFramebufferState* const recordPtr =
BoundFramebufferRecord(FramebufferTarget::Draw);
// P4a decline-site F7: FLIPPED AT THE VERIFICATION ROUND from a silent
// decline to an assertion, because it is unreachable and a silent decline
// here is indistinguishable from a legitimate one. g_fboRecordsTrusted
// implies a record with a non-null handle on BOTH targets:
// SyncCurrentFBOByRecord sets the latch only after F2 has rejected a null
// pointer or handle on either, and it runs immediately before this in
// PrepareForDraw. Kept as a checked decline rather than deleted so a
// release build cannot dereference null if that ordering ever changes.
MOBILEGL_ASSERT(recordPtr != nullptr && !MG_Pipe::MGPipeHandleIsNull(recordPtr->Fbo));
if (recordPtr != nullptr && !MG_Pipe::MGPipeHandleIsNull(recordPtr->Fbo)) {
const MG_Pipe::MGPFramebufferState& record = *recordPtr;
if (!g_broadcastMemoHandleValid || g_broadcastMemoContentHash != record.ContentHash) {
Uint enabledDrawBuffers = 0;
for (Uint i = 0; i < MG_State::GLState::FramebufferObject::MAX_DRAW_BUFFERS; ++i) {
// -1 is FramebufferAttachmentType::None on the wire; every
// other value is an attachment index.
if (record.DrawBuffers[i] >= 0) {
enabledDrawBuffers = i + 1;
}
}
g_broadcastMemoContentHash = record.ContentHash;
g_broadcastMemoHandleCount = std::max<Uint>(enabledDrawBuffers, 1);
g_broadcastMemoHandleValid = true;
}
g_fragColorBroadcastCount = g_broadcastMemoHandleCount;
broadcastCountResolved = true;
}
}
#endif
if (policy == BroadcastCountPolicy::RecordOnly) return;
if (policy == BroadcastCountPolicy::RefuseFallback && !broadcastCountResolved) {
// The record arm declined under a transport. g_fboRecordsTrusted is set by
// SyncCurrentFBOByRecord, which PrepareForDraw runs immediately before this,
// so reaching here means the framebuffer family's own records are missing or
// untrusted - and the frontend fallback below is exactly the read this phase
// exists to retire.
MGLOG_F("MGPipe: Fatal{UnmigratedPipeInput, "
"\"GetFramebufferBindingSlot@SyncCurrentProgram\"} - the draw "
"framebuffer's record did not answer the fragColor broadcast count on "
"the handle arm, and the frontend binding slot is a row the client owns");
std::abort();
}
if (!broadcastCountResolved) {
const auto& drawSlot = GetFramebufferBindingSlotChecked(FramebufferTarget::Draw);
const auto& drawFBO = drawSlot.GetBoundObject();
const Uint16 slotVersion = drawSlot.GetVersion();
const Uint16 objectVersion = drawFBO ? drawFBO->GetObjectVersion() : 0;
if (!g_broadcastMemoValid || g_broadcastMemoFbo != drawFBO.get() ||
g_broadcastMemoSlotVersion != slotVersion ||
g_broadcastMemoObjectVersion != objectVersion) {
Uint enabledDrawBuffers = 0;
if (drawFBO) {
const auto& drawBuffers = drawFBO->GetDrawBuffers();
for (Uint i = 0; i < MG_State::GLState::FramebufferObject::MAX_DRAW_BUFFERS; ++i) {
if (drawBuffers[i] != FramebufferAttachmentType::None) {
enabledDrawBuffers = i + 1;
}
}
}
g_broadcastMemoFbo = drawFBO.get();
g_broadcastMemoSlotVersion = slotVersion;
g_broadcastMemoObjectVersion = objectVersion;
g_broadcastMemoCount = std::max<Uint>(enabledDrawBuffers, 1);
g_broadcastMemoValid = true;
}
g_fragColorBroadcastCount = g_broadcastMemoCount;
}
}
void SyncCurrentProgram(const SharedPtr<MG_State::GLState::ProgramObject>& currentProgram) {
#ifdef TRACY_ENABLE
@@ -4341,73 +4469,7 @@ namespace MobileGL::MG_Backend::DirectGLES {
g_lastUsedBackendProgramId = 0;
return;
}
// Read from the frontend rather than from the backend framebuffer sync, which
// only runs later in PrepareForDraw: a program compiled against a stale count
// would not be relinked until the draw after the one that needed it.
{
Bool broadcastCountResolved = false;
#if MOBILEGL_PIPE_PUSH
// The trust latch is fresh here and costs nothing: PrepareForDraw runs
// SyncCurrentFBO immediately before this, so the records have just been
// checked against the two bindings.
if (FramebufferSubsystemEnabled() && FramebufferImpl::g_fboRecordsTrusted) {
const MG_Pipe::MGPFramebufferState* const recordPtr =
BoundFramebufferRecord(FramebufferTarget::Draw);
// P4a decline-site F7: FLIPPED AT THE VERIFICATION ROUND from a silent
// decline to an assertion, because it is unreachable and a silent decline
// here is indistinguishable from a legitimate one. g_fboRecordsTrusted
// implies a record with a non-null handle on BOTH targets:
// SyncCurrentFBOByRecord sets the latch only after F2 has rejected a null
// pointer or handle on either, and it runs immediately before this in
// PrepareForDraw. Kept as a checked decline rather than deleted so a
// release build cannot dereference null if that ordering ever changes.
MOBILEGL_ASSERT(recordPtr != nullptr && !MG_Pipe::MGPipeHandleIsNull(recordPtr->Fbo));
if (recordPtr != nullptr && !MG_Pipe::MGPipeHandleIsNull(recordPtr->Fbo)) {
const MG_Pipe::MGPFramebufferState& record = *recordPtr;
if (!g_broadcastMemoHandleValid || g_broadcastMemoContentHash != record.ContentHash) {
Uint enabledDrawBuffers = 0;
for (Uint i = 0; i < MG_State::GLState::FramebufferObject::MAX_DRAW_BUFFERS; ++i) {
// -1 is FramebufferAttachmentType::None on the wire; every
// other value is an attachment index.
if (record.DrawBuffers[i] >= 0) {
enabledDrawBuffers = i + 1;
}
}
g_broadcastMemoContentHash = record.ContentHash;
g_broadcastMemoHandleCount = std::max<Uint>(enabledDrawBuffers, 1);
g_broadcastMemoHandleValid = true;
}
g_fragColorBroadcastCount = g_broadcastMemoHandleCount;
broadcastCountResolved = true;
}
}
#endif
if (!broadcastCountResolved) {
const auto& drawSlot = GetFramebufferBindingSlotChecked(FramebufferTarget::Draw);
const auto& drawFBO = drawSlot.GetBoundObject();
const Uint16 slotVersion = drawSlot.GetVersion();
const Uint16 objectVersion = drawFBO ? drawFBO->GetObjectVersion() : 0;
if (!g_broadcastMemoValid || g_broadcastMemoFbo != drawFBO.get() ||
g_broadcastMemoSlotVersion != slotVersion ||
g_broadcastMemoObjectVersion != objectVersion) {
Uint enabledDrawBuffers = 0;
if (drawFBO) {
const auto& drawBuffers = drawFBO->GetDrawBuffers();
for (Uint i = 0; i < MG_State::GLState::FramebufferObject::MAX_DRAW_BUFFERS; ++i) {
if (drawBuffers[i] != FramebufferAttachmentType::None) {
enabledDrawBuffers = i + 1;
}
}
}
g_broadcastMemoFbo = drawFBO.get();
g_broadcastMemoSlotVersion = slotVersion;
g_broadcastMemoObjectVersion = objectVersion;
g_broadcastMemoCount = std::max<Uint>(enabledDrawBuffers, 1);
g_broadcastMemoValid = true;
}
g_fragColorBroadcastCount = g_broadcastMemoCount;
}
}
ResolveFragColorBroadcastCount(BroadcastCountPolicy::FrontendFallback);
BackendProgramObjectImpl* twin = nullptr;
#if MOBILEGL_PIPE_PUSH
@@ -4461,7 +4523,7 @@ namespace MobileGL::MG_Backend::DirectGLES {
twin->GetUnormFallbackClampOutputMask() != g_unormFallbackClampOutputMask ||
twin->GetFragColorBroadcastCount() != g_fragColorBroadcastCount ||
twin->GetShaderStorageBlockBindingSignature() !=
ComputeShaderStorageBlockBindingSignature(*currentProgram) ||
ComputeShaderStorageBlockBindingSignatureOf(*currentProgram) ||
// A fourth of the same shape, and the reason glBindImageTexture itself does
// nothing: GLSL ES demands a format layout qualifier on an image where desktop
// GLSL lets a writeonly declaration omit one, so a format-less declaration is
@@ -4508,7 +4570,82 @@ namespace MobileGL::MG_Backend::DirectGLES {
}
g_currentDrawFrontendProgram = currentProgram.get();
g_currentDrawBackendProgram = twin;
#if MOBILEGL_PIPE_PUSH
g_currentDrawProgramHandle = MG_Pipe::kMGPipeNullHandle;
#endif
}
#if MOBILEGL_PIPE_PUSH
// P5e (pg), CONTRACT-P5E.md §5.5: THE SAME SYNC, FROM THE HANDLE THE RECORD CARRIED.
// Two overloads and not an `#if` inside one body (ruling 1 / ID-81): the frontend one
// above is visibly the monolith-glue half, this one names no frontend object at all.
//
// WHAT THE NINE-CLAUSE CONDITION BECOMES, and the clause COUNT does not shrink - its
// inputs move (D-H5, §5.5):
// GetLinkVersion() -> record.Serial vs GetSyncedShaderCsoSerial()
// GetImageUnitVersion() -> record.BindingsSerial vs GetSyncedBindingsSerial()
// ComputeShaderStorage...() -> record.Signature
// GetLinkStatus/SpirvStatus -> Desc.LinkStatus / Desc.SpirvStatus, above
// and the clamp masks, the broadcast count, ImageUnitFormatsStillMatch and the three
// patch clauses are unchanged - they are backend globals, fb's row and VALUE rows of the
// residual block, none of which this family owns.
void SyncCurrentProgramByHandle(MG_Pipe::MGPipeHandle cso, Bool forDraw) {
#ifdef TRACY_ENABLE
ZoneScopedC(TRACY_ZONECOLOR_BACKEND);
#endif
g_backendProgramObjects.CollectGarbageIfNeeded();
SamplerImpl::g_backendSamplerObjects.CollectGarbageIfNeeded();
g_currentDrawFrontendProgram = nullptr;
g_currentDrawBackendProgram = nullptr;
g_currentDrawProgramHandle = MG_Pipe::kMGPipeNullHandle;
const MG_Pipe::MGPipeShaderCsoRecord* const record = FindShaderCsoRecord(cso);
// THE SAME THREE-PART TEST THE FRONTEND ARM MAKES, from the descriptor. LinkStatus
// is a field rather than an implication (ID-88): create_shader_state is re-issued at
// every link that moves the link version and a FAILED relink of a bound program
// moves it too, so "a record exists" and "the program linked" are different
// statements. A program the frontend reports unlinked draws nothing, which is
// exactly what the monolith arm does with GetLinkStatus() == false.
if (record == nullptr || record->Desc.LinkStatus == 0 || record->Desc.SpirvStatus == 0) {
g_GLESFuncs.glUseProgram(0);
g_lastUsedBackendProgramId = 0;
return;
}
// REFUSED at a draw, record-only at a dispatch: see the policy's own note.
ResolveFragColorBroadcastCount(forDraw ? BroadcastCountPolicy::RefuseFallback
: BroadcastCountPolicy::RecordOnly);
BackendProgramObjectImpl* const twin = ResolveProgramTwin(cso);
if (twin == nullptr) {
// ResolveProgramTwin has already named the handle. A draw with no twin binds
// nothing, which is the visible no-op Use() makes of an unusable program rather
// than a draw with somebody else's shader.
g_GLESFuncs.glUseProgram(0);
g_lastUsedBackendProgramId = 0;
return;
}
if (!twin->GetBackendProgramId() ||
twin->GetSyncedShaderCsoSerial() != record->Serial ||
twin->GetSyncedBindingsSerial() != record->BindingsSerial ||
twin->GetSnormFallbackClampOutputMask() != g_snormFallbackClampOutputMask ||
twin->GetUnormFallbackClampOutputMask() != g_unormFallbackClampOutputMask ||
twin->GetFragColorBroadcastCount() != g_fragColorBroadcastCount ||
twin->GetShaderStorageBlockBindingSignature() != record->Signature ||
!twin->ImageUnitFormatsStillMatch() ||
(twin->GetPassthroughTessControlPatchVertices() >= 0 &&
(twin->GetPassthroughTessControlPatchVertices() !=
static_cast<Int>(MGB_CTX->GetPatchVertices()) ||
!BitwiseEqual(twin->GetPassthroughTessControlOuterLevel(),
MGB_CTX->GetPatchDefaultOuterLevel()) ||
!BitwiseEqual(twin->GetPassthroughTessControlInnerLevel(),
MGB_CTX->GetPatchDefaultInnerLevel())))) {
twin->SyncToBackendByHandle(cso);
}
g_currentDrawProgramHandle = cso;
g_currentDrawBackendProgram = twin;
}
#endif
} // namespace PrgramImpl
void BindCurrentFBO(FramebufferTarget target) {
@@ -4755,7 +4892,7 @@ namespace MobileGL::MG_Backend::DirectGLES {
static void BindCurrentProgramWithResources(
const SharedPtr<MG_State::GLState::ProgramObject>& currentProgram,
const TextureImpl::DrawTextureSyncKeys& keys);
const TextureImpl::DrawTextureSyncKeys& keys, MG_Pipe::MGPipeHandle programCso);
static void BindCurrentTextures(const TextureImpl::DrawTextureSyncKeys& keys,
const SharedPtr<MG_State::GLState::ProgramObject>& currentProgram);
@@ -4803,7 +4940,19 @@ namespace MobileGL::MG_Backend::DirectGLES {
// stores into a buffer texture from the FRAGMENT stage, not from a dispatch.
TextureImpl::MarkWritableImageBufferTexturesGpuWritten();
FramebufferImpl::SyncCurrentFBO();
PrgramImpl::SyncCurrentProgram(currentProgram);
#if MOBILEGL_PIPE_PUSH
// P5e (pg), kimi row 91 / S4 1.1: the SharedPtr pull above is the row this family
// retires. On the handle arm the program is MGPipeApplier().DrawProgram - a handle the
// set_draw_program record carried - and `currentProgram` is not consulted for the sync
// at all; the remaining uses of it below are the two callees' own, and both have a
// handle arm of their own.
if (ProgramHandleArm()) {
PrgramImpl::SyncCurrentProgramByHandle(MG_Pipe::MGPipeApplier().DrawProgram, true);
} else
#endif
{
PrgramImpl::SyncCurrentProgram(currentProgram);
}
RenderStateImpl::SyncRenderState();
BindCurrentFBO(FramebufferTarget::Draw);
@@ -4822,7 +4971,13 @@ namespace MobileGL::MG_Backend::DirectGLES {
VertexArrayImpl::SyncCurrentVertexAttributeValues(vaoTwin, currentProgram);
BindCurrentTextures(textureKeys, currentProgram);
BindCurrentProgramWithResources(currentProgram, textureKeys);
BindCurrentProgramWithResources(currentProgram, textureKeys,
#if MOBILEGL_PIPE_PUSH
MG_Pipe::MGPipeApplier().DrawProgram
#else
MG_Pipe::kMGPipeNullHandle
#endif
);
// Last: opening the capture span needs the program current and the capture
// buffers bound, and ES rejects most binding changes once it is open.
@@ -5222,6 +5377,15 @@ namespace MobileGL::MG_Backend::DirectGLES {
Uint32 programBackendStateVersion = 0;
Bool programLinked = false;
Uint contextGeneration = 0;
// P5e (pg) -> tx2, ruling 6 / ID-86: THE PROGRAM HALF OF THIS KEY HAS TO GO BY HANDLE,
// and this struct is tx2's line, so pg states the shape rather than writing it. The four
// program fields above are read off the frontend ProgramObject on EVERY DRAW, memo hit
// included (kimi row 100, the worst row of the program family). What replaces them on
// the handle arm is {DrawProgram.Slot, DrawProgram.Gen, ShaderCso record Serial,
// BindingsSerial}: Serial moves on every applied create_shader_state (how a relink
// travels) and BindingsSerial on every applied set_program_bindings (how a glUniform1i
// on a sampler travels). Both are needed - ruling 6's "both" - because a relink with
// UNCHANGED texture resolution still hands the twin new unit assignments.
decltype(TextureImpl::g_boundTexturesCache) boundTextures{};
};
// Small per-PROGRAM memo set, not one global: the program is part of the key
@@ -5316,27 +5480,63 @@ namespace MobileGL::MG_Backend::DirectGLES {
// _FlwFrameUniforms block and the _flw_depthPyramid sampler.
static void BindCurrentProgramWithResources(
const SharedPtr<MG_State::GLState::ProgramObject>& currentProgram,
const TextureImpl::DrawTextureSyncKeys& keys) {
if (currentProgram && currentProgram->GetLinkStatus() && currentProgram->GetSpirvStatus()) {
const TextureImpl::DrawTextureSyncKeys& keys, MG_Pipe::MGPipeHandle programCso) {
#if MOBILEGL_PIPE_PUSH
// P5e (pg): THE HANDLE ARM'S GATE, from the record rather than from the object. Hoisted
// out of the `if` below because the whole body's entry condition is "the program linked
// and has SPIR-V", and on this arm both answers are fields of the descriptor
// (Desc.LinkStatus is ID-88's, Desc.SpirvStatus P4a's).
const Bool handleArm = ProgramHandleArm();
const MG_Pipe::MGPipeShaderCsoRecord* programRecord = nullptr;
if (handleArm) {
programRecord = PrgramImpl::FindShaderCsoRecord(programCso);
if (programRecord != nullptr &&
(programRecord->Desc.LinkStatus == 0 || programRecord->Desc.SpirvStatus == 0)) {
programRecord = nullptr;
}
}
const Bool programUsable =
handleArm ? programRecord != nullptr
: (currentProgram && currentProgram->GetLinkStatus() && currentProgram->GetSpirvStatus());
#else
const Bool programUsable =
currentProgram && currentProgram->GetLinkStatus() && currentProgram->GetSpirvStatus();
#endif
if (programUsable) {
#ifdef TRACY_ENABLE
ZoneScopedNC("BindCurrentProgram", TRACY_ZONECOLOR_BACKEND);
#endif
#if MOBILEGL_BUILD_DISAGGREGATED
// P5c (G6, CONTRACT-P5C §5.4): the program/sampler twin resolutions below still key
// on frontend objects - named debt inside the scope - P3b/P4b rekeys the registry
// onto handles.
const MG_Pipe::MGPipeFrontendKeyedRegistryScope frontendKeyedRegistry;
PrgramImpl::BackendProgramObjectImpl* twin = nullptr;
#if MOBILEGL_PIPE_PUSH
if (handleArm) {
// THE STASH, KEYED ON THE HANDLE (CONTRACT-P5E §5.5). SyncCurrentProgramByHandle
// wrote it a few lines ago in the same Prepare; the fallback is the by-handle
// resolver, which is record-first and touches no frontend identity - so unlike
// the frontend fallback below it is not a registry probe at all.
twin = PrgramImpl::g_currentDrawProgramHandle == programCso
? PrgramImpl::g_currentDrawBackendProgram
: nullptr;
if (!twin) twin = PrgramImpl::ResolveProgramTwin(programCso);
} else
#endif
// The twin SyncCurrentProgram just resolved for this draw; the registry
// Find only runs if the stash somehow does not match (defensive fallback).
PrgramImpl::BackendProgramObjectImpl* twin =
PrgramImpl::g_currentDrawFrontendProgram == currentProgram.get()
? PrgramImpl::g_currentDrawBackendProgram
: nullptr;
if (!twin) {
auto* backendProgramSlot = PrgramImpl::g_backendProgramObjects.Find(currentProgram.get());
if (backendProgramSlot) {
twin = backendProgramSlot->get();
{
#if MOBILEGL_BUILD_DISAGGREGATED
// P5c (G6, CONTRACT-P5C §5.4): MONOLITH GLUE ONLY now. The frontend-keyed twin
// resolution below is the fourth of this family's five named scope sites, and
// P5e does not delete it - it stops reaching it under a transport, which is what
// the allocator guard measures.
const MG_Pipe::MGPipeFrontendKeyedRegistryScope frontendKeyedRegistry;
#endif
// The twin SyncCurrentProgram just resolved for this draw; the registry
// Find only runs if the stash somehow does not match (defensive fallback).
twin = PrgramImpl::g_currentDrawFrontendProgram == currentProgram.get()
? PrgramImpl::g_currentDrawBackendProgram
: nullptr;
if (!twin) {
auto* backendProgramSlot = PrgramImpl::g_backendProgramObjects.Find(currentProgram.get());
if (backendProgramSlot) {
twin = backendProgramSlot->get();
}
}
}
if (twin) {
@@ -5346,7 +5546,16 @@ namespace MobileGL::MG_Backend::DirectGLES {
// Global UBO: block index and binding-point assignment are cached at
// link time (CacheResourceLocations); re-upload only when the CPU shadow
// actually changed since the last upload for this program.
if (currentProgram->GetUBOSize() > 0 && backendProgram.HasGlobalUboBlock()) {
#if MOBILEGL_PIPE_PUSH
// P5e (pg): the block's SIZE is Desc.GlobalUboSize on the handle arm. It is the
// same number GetUBOSize() answers - the emitter takes it from there - but it is
// the record's copy, which is the point.
const Uint globalUboSize =
handleArm ? programRecord->Desc.GlobalUboSize : currentProgram->GetUBOSize();
#else
const Uint globalUboSize = currentProgram->GetUBOSize();
#endif
if (globalUboSize > 0 && backendProgram.HasGlobalUboBlock()) {
#ifdef TRACY_ENABLE
ZoneScopedNC("UpdateGlobalUBO", TRACY_ZONECOLOR_BACKEND);
#endif
@@ -5363,14 +5572,34 @@ namespace MobileGL::MG_Backend::DirectGLES {
// block right after this one and they are P4b's (dirty bits 15-17), which
// is why nothing here touches them.
const MG_Pipe::MGPipeShaderCsoRecord* const globalConstants =
PrgramImpl::ResolveGlobalConstantsRecord(currentProgram.get());
handleArm ? PrgramImpl::ResolveGlobalConstantsRecordForHandle(programCso,
globalUboSize)
: PrgramImpl::ResolveGlobalConstantsRecord(currentProgram.get());
// P5e (pg), gap G-C: `MapUBO()` HAS NO RUN-AHEAD ANSWER. It returns
// ProgramObject::globalUboScratch - the live CPU array every glUniform*
// writes into - so under a transport it is a torn read of client memory by
// construction, not a read that happens to race. Every reason
// ResolveGlobalConstantsRecord* can decline is a SEAM DEFECT here (bit 12
// off, the mis-keyed descriptor, the never-uploaded sentinel, a short block
// image), and each of them has already said so by name one level down; what
// this adds is that on the handle arm there is nothing legal to fall back
// to. CONTRACT-P5E §5.5: Fatal{UnmigratedVerb, "set_global_constants"}, not
// a torn upload.
if (handleArm && globalConstants == nullptr) {
MGLOG_F("MGPipe: Fatal{UnmigratedVerb, \"set_global_constants\"} - the "
"ShaderCso record {%u, %u} does not answer the default uniform "
"block for a draw of a program that declares %u bytes of it, and "
"MapUBO() is the client's live scratch array",
programCso.Slot, programCso.Gen, globalUboSize);
std::abort();
}
#endif
const Uint32 uboContentVersion =
#if MOBILEGL_PIPE_PUSH
globalConstants ? globalConstants->GlobalConstantsVersion :
#endif
currentProgram->GetUBOContentVersion();
const SizeT uboSize = static_cast<SizeT>(currentProgram->GetUBOSize());
const SizeT uboSize = static_cast<SizeT>(globalUboSize);
// Substituted at the two upload sites rather than hoisted into a local:
// MapUBO() is called there today and only there, and hoisting it would
// call it on paths that skip the upload entirely.
@@ -5422,12 +5651,12 @@ namespace MobileGL::MG_Backend::DirectGLES {
// failed): the original in-place upload.
if (backendProgram.GetLastUploadedGlobalUboVersion() != uboContentVersion) {
g_GLESFuncs.glBindBuffer(GL_UNIFORM_BUFFER, backendProgram.GetBackendGlobalUBOId());
g_GLESFuncs.glBufferSubData(GL_UNIFORM_BUFFER, 0, currentProgram->GetUBOSize(),
g_GLESFuncs.glBufferSubData(GL_UNIFORM_BUFFER, 0, globalUboSize,
MGB_UBO_BYTES);
if (MG_Util::PipeStats::Enabled()) {
MG_Util::PipeStats::AddBytes(
MG_Util::PipeStats::ByteClass::StageUboGlobal,
static_cast<Uint64>(currentProgram->GetUBOSize()));
static_cast<Uint64>(globalUboSize));
}
g_GLESFuncs.glBindBuffer(GL_UNIFORM_BUFFER, 0);
backendProgram.SetLastUploadedGlobalUboVersion(uboContentVersion);
@@ -5459,8 +5688,19 @@ namespace MobileGL::MG_Backend::DirectGLES {
continue;
}
// Connect buffer to backend binding point
auto binding = currentProgram->GetUniformBlockBinding(i);
// Connect buffer to backend binding point.
//
// P5e (pg): glUniformBlockBinding MOVES THIS AFTER THE LINK, so it is
// one of the three post-link mutable reflection fields the archive alone
// cannot answer - set_program_bindings' first tail carries it, dense in
// this very index space. The BINDING POINT it names is sb's row (the
// GetBufferBindingPoint below); this line is the boundary S4 R5 named.
auto binding =
#if MOBILEGL_PIPE_PUSH
handleArm ? PrgramImpl::ProgramBlockBindingFromRecord(*programRecord, i)
:
#endif
currentProgram->GetUniformBlockBinding(i);
auto& point = MGB_CTX->GetBufferBindingPoint(BufferTarget::Uniform, binding);
auto& bufferObj = point.GetBoundObject();
auto range = point.GetRange();
@@ -5519,7 +5759,18 @@ namespace MobileGL::MG_Backend::DirectGLES {
// and every previously touched unit's sampler-shadow row is exactly
// what this pass last left there, re-running it is a provable no-op.
auto& samplerPassMemo = backendProgram.GetSamplerPassMemo();
const Uint32 programBackendStateVersion = currentProgram->GetBackendStateVersion();
// P5e (pg), CONTRACT-P5E §5.5: THE SAMPLER PASS MEMO KEYS ON BindingsSerial
// on the handle arm. m_backendStateVersion is a frontend counter read on
// every draw (kimi row 105); what it is standing in for here is "has a
// sampler uniform's unit moved", and that is exactly what an applied
// set_program_bindings means. Truncated to the memo's Uint32 field, which is
// safe for a memo key - it is a change detector, not an ordering - and the
// record's serial would need four billion binding records to wrap.
const Uint32 programBackendStateVersion =
#if MOBILEGL_PIPE_PUSH
handleArm ? static_cast<Uint32>(programRecord->BindingsSerial) :
#endif
currentProgram->GetBackendStateVersion();
Bool samplerPassClean =
samplerPassMemo.valid && samplerPassMemo.contextId == keys.contextId &&
samplerPassMemo.unitBindingsEpoch == keys.unitBindingsEpoch &&
@@ -5542,8 +5793,18 @@ namespace MobileGL::MG_Backend::DirectGLES {
samplerPassMemo.count = 0;
Bool memoisable = true;
for (auto& samplerBinding : backendProgram.GetSamplerUniformBindings()) {
// P5e (pg): the unit glUniform1i put on this sampler - the second
// of the three post-link mutable fields, carried sparse and ascending
// by location in set_program_bindings' second tail.
const auto unit =
currentProgram->GetUniformSamplerOrImageUnitIndex(samplerBinding.frontendLocation);
#if MOBILEGL_PIPE_PUSH
handleArm
? PrgramImpl::ProgramSamplerUnitFromRecord(
*programRecord, samplerBinding.frontendLocation)
:
#endif
currentProgram->GetUniformSamplerOrImageUnitIndex(
samplerBinding.frontendLocation);
if (unit == -1) continue;
// Record the touched unit for the memo; a pass touching more
// units than the memo can carry (or an out-of-range unit)
@@ -5676,6 +5937,26 @@ namespace MobileGL::MG_Backend::DirectGLES {
// PrepareForCompute, where the current program (and therefore its registry twin)
// is pinned for the duration. Prefers the per-draw stash those preparations wrote.
static PrgramImpl::BackendProgramObjectImpl* GetCurrentBackendProgram() {
#if MOBILEGL_PIPE_PUSH
// P5e (pg), kimi row 107: THE PER-SUB-DRAW ROW. This is reached once per indirect
// sub-draw for the gl_DrawID / gl_BaseVertex / gl_BaseInstance uniforms, and before P5e
// every one of those pulled GetProgramForDraw() and, on a stash miss, probed the client
// allocator. On the handle arm the stash key is the applier's own DrawProgram and the
// miss path is the by-handle resolver, which is record-first and names no frontend
// identity - so the whole entry point leaves the frontend.
if (ProgramHandleArm()) {
const MG_Pipe::MGPipeHandle cso = MG_Pipe::MGPipeApplier().DrawProgram;
if (MG_Pipe::MGPipeHandleIsNull(cso)) return nullptr;
const auto* const record = PrgramImpl::FindShaderCsoRecord(cso);
if (record == nullptr || record->Desc.LinkStatus == 0 || record->Desc.SpirvStatus == 0) {
return nullptr;
}
if (PrgramImpl::g_currentDrawProgramHandle == cso) {
return PrgramImpl::g_currentDrawBackendProgram;
}
return PrgramImpl::ResolveProgramTwin(cso);
}
#endif
const auto& currentProgram = MGB_CTX->GetProgramForDraw();
if (!currentProgram || !currentProgram->GetLinkStatus() || !currentProgram->GetSpirvStatus()) {
return nullptr;
@@ -5684,8 +5965,8 @@ namespace MobileGL::MG_Backend::DirectGLES {
return PrgramImpl::g_currentDrawBackendProgram;
}
#if MOBILEGL_BUILD_DISAGGREGATED
// P5c (G6, CONTRACT-P5C §5.4): frontend-keyed twin resolution, named debt inside the
// scope - P3b/P4b rekeys the registry onto handles.
// P5c (G6, CONTRACT-P5C §5.4): MONOLITH GLUE ONLY now - the fifth of this family's five
// named scope sites, unreachable under a transport by the arm above.
const MG_Pipe::MGPipeFrontendKeyedRegistryScope frontendKeyedRegistry;
#endif
if (auto* backendProgramSlot = PrgramImpl::g_backendProgramObjects.Find(currentProgram.get())) {
@@ -5731,10 +6012,25 @@ namespace MobileGL::MG_Backend::DirectGLES {
// flattening a batch that turns out to need per-sub-draw values is unrecoverable -
// so an unanswerable program counts as needing them.
Bool CurrentProgramMayNeedPerSubDrawBuiltins(Bool batchCarriesBaseVertices) {
const auto& currentProgram = MGB_CTX->GetProgramForDraw();
const auto program = GetCurrentBackendProgram();
if (!currentProgram || program == nullptr ||
program->GetSyncedLinkVersion() != currentProgram->GetLinkVersion()) {
if (program == nullptr) return true;
#if MOBILEGL_PIPE_PUSH
// P5e (pg), kimi row 108: the same "is this twin current" question, from the record's
// serial instead of the frontend's link version. The ANSWER TO AN UNANSWERABLE PROGRAM
// IS STILL `true` - a flattened batch that turns out to need per-sub-draw values is
// unrecoverable, so "not known yet" has to count as "may need them", and a missing
// record is the most not-known-yet a program can be.
if (ProgramHandleArm()) {
const auto* const record =
PrgramImpl::FindShaderCsoRecord(MG_Pipe::MGPipeApplier().DrawProgram);
if (record == nullptr || program->GetSyncedShaderCsoSerial() != record->Serial) {
return true;
}
return program->ReadsDrawID() || (batchCarriesBaseVertices && program->ReadsBaseVertex());
}
#endif
const auto& currentProgram = MGB_CTX->GetProgramForDraw();
if (!currentProgram || program->GetSyncedLinkVersion() != currentProgram->GetLinkVersion()) {
return true;
}
return program->ReadsDrawID() || (batchCarriesBaseVertices && program->ReadsBaseVertex());
@@ -6130,8 +6426,33 @@ namespace MobileGL::MG_Backend::DirectGLES {
TextureImpl::SyncNeccessaryTextures(textureKeys);
TextureImpl::SyncImageTextureBindings();
TextureImpl::MarkWritableImageBufferTexturesGpuWritten();
PrgramImpl::SyncCurrentProgram(currentProgram);
#if MOBILEGL_PIPE_PUSH
// P5e (pg), kimi row 92: the DISPATCH half of the same retirement -
// MGPipeApplier().DispatchProgram, which set_dispatch_program carried. A compute-only
// pipeline has no draw program at all, which is why the two applier fields are separate.
const Bool programHandleArm = ProgramHandleArm();
const MG_Pipe::MGPipeHandle dispatchCso =
programHandleArm ? MG_Pipe::MGPipeApplier().DispatchProgram : MG_Pipe::kMGPipeNullHandle;
if (programHandleArm) {
PrgramImpl::SyncCurrentProgramByHandle(dispatchCso, false);
} else
#endif
{
PrgramImpl::SyncCurrentProgram(currentProgram);
}
#if MOBILEGL_PIPE_PUSH
if (programHandleArm) {
// The same gate as the frontend one below, from the record: a dispatch of a program
// the frontend reports unlinked, or one whose SPIR-V never arrived, binds nothing.
const auto* const record = PrgramImpl::FindShaderCsoRecord(dispatchCso);
if (record == nullptr || record->Desc.LinkStatus == 0 || record->Desc.SpirvStatus == 0) {
g_GLESFuncs.glUseProgram(0);
PrgramImpl::g_lastUsedBackendProgramId = 0;
return;
}
} else
#endif
if (!currentProgram || !currentProgram->GetLinkStatus() || !currentProgram->GetSpirvStatus()) {
g_GLESFuncs.glUseProgram(0);
PrgramImpl::g_lastUsedBackendProgramId = 0;
@@ -6145,7 +6466,13 @@ namespace MobileGL::MG_Backend::DirectGLES {
// Compute programs need the same per-program resource sync as draws:
// uniform-block bindings and sampler units only exist through the API
// because layout(binding) is stripped from the transpiled ESSL.
BindCurrentProgramWithResources(currentProgram, textureKeys);
BindCurrentProgramWithResources(currentProgram, textureKeys,
#if MOBILEGL_PIPE_PUSH
dispatchCso
#else
MG_Pipe::kMGPipeNullHandle
#endif
);
}
GLuint GetBackendProgramId(GLuint program) {
+450 -106
View File
@@ -15,6 +15,10 @@
#include <MG_Pipe/MGPipeCallbacks.h>
#include <MG_Pipe/MGPipeHostSpan.h>
#include <MG_Pipe/PipeApply.h>
// P5e (pg): the DEFINITION of the archive MGPipeShaderCsoRecord holds by SharedPtr. PipeApply.h
// needs only the name - the deleter is captured where the archive is constructed - but the
// program build reads through it, so the one translation unit that does needs the whole type.
#include <MG_State/GLState/ProgramState/ProgramArtifactsCodec.h>
#endif
#if MOBILEGL_BUILD_DISAGGREGATED
// R-11's server-owned staging copy. Header-only and package v1's; see its own header block for
@@ -742,9 +746,14 @@ namespace MobileGL::MG_Backend::DirectGLES {
// layout(binding) reflected at link time, or whatever glUniform1i stored afterwards.
// Rewrite every image uniform declaration to that unit so imageLoad/Store hits the
// unit the app bound with glBindImageTexture.
String RebindImageUniformsToFrontendUnits(
String source, const SharedPtr<MG_State::GLState::ProgramObject>& stateProgramObject) {
if (!stateProgramObject || source.find("image") == String::npos) {
String RebindImageUniformsToFrontendUnits(String source, const PrgramImpl::ProgramBuildSource& src) {
// P5e (pg): the SOURCE, not the frontend object. The two reads below - the uniform's
// location and the unit glUniform1i put there - are exactly the pair the record's
// set_program_bindings tail carries, so on the handle arm this bakes the unit the client
// observed rather than one it would have had to dereference a live ProgramObject for.
// The null test the SharedPtr form carried moved to the caller, which returned early on
// a null program long before it reached here.
if (source.find("image") == String::npos) {
return source;
}
static const std::regex imageDeclRegex(
@@ -762,12 +771,12 @@ namespace MobileGL::MG_Backend::DirectGLES {
std::smatch match;
if (std::regex_search(line, match, imageDeclRegex)) {
const String name = match[3].str();
Int location = stateProgramObject->GetUniformLocation(name);
Int location = src.GetUniformLocation(name);
if (location < 0) {
location = stateProgramObject->GetUniformLocation(name + "[0]");
location = src.GetUniformLocation(name + "[0]");
}
if (location >= 0) {
const Int unit = stateProgramObject->GetUniformSamplerOrImageUnitIndex(location);
const Int unit = src.GetUniformSamplerOrImageUnitIndex(location);
if (unit >= 0) {
const String bindingText = "binding = " + std::to_string(unit);
if (std::regex_search(line, bindingValueRegex)) {
@@ -10840,6 +10849,222 @@ namespace MobileGL::MG_Backend::DirectGLES {
TwinRegistry<MG_State::GLState::ProgramObject, BackendProgramObjectImpl, MG_Pipe::MGPipeKind::ShaderCso> g_backendProgramObjects;
#if MOBILEGL_PIPE_PUSH
// ---- P5e (pg): the source, both ways round ---------------------------------------
Int ProgramArchiveSource::GetUniformLocation(const String& name) const {
// ProgramObject::GetUniformLocation over the archive. Reproduced rather than called
// because the frontend's form is a non-static member over Artifacts() and this arm
// has no ProgramObject to call it on; the ARRAY RULES are the whole body and they
// are the part a paraphrase would get wrong, so they are transcribed exactly.
const auto it = Link->uniformLocations.find(name);
if (it != Link->uniformLocations.end()) return static_cast<Int>(it->second);
if (name.empty()) return -1;
// Reflection stores GL-style names: an array uniform is keyed "arr[0]" at its base
// location, so a bare "arr" resolves to that entry.
if (name.back() != ']') {
const auto suffixed = Link->uniformLocations.find(name + "[0]");
if (suffixed != Link->uniformLocations.end()) return static_cast<Int>(suffixed->second);
return -1;
}
if (name.length() < 4) return -1;
// An ARRAY OF ARRAYS is keyed by its full "[0]"-terminated spelling ("a[2][1][0]"),
// so a query that already ends in a subscript may still be the NAME of an array
// rather than an element of one. That reading is tried FIRST; only then is the
// trailing subscript read as an element index.
{
const auto arrayOfArrays = Link->uniformLocations.find(name + "[0]");
if (arrayOfArrays != Link->uniformLocations.end()) {
return static_cast<Int>(arrayOfArrays->second);
}
}
const SizeT bracket = name.rfind('[');
if (bracket == String::npos || bracket + 1 >= name.length() - 1) return -1;
Uint element = 0;
for (SizeT i = bracket + 1; i < name.length() - 1; ++i) {
if (name[i] < '0' || name[i] > '9') return -1;
element = element * 10 + static_cast<Uint>(name[i] - '0');
if (element > 0x0FFFFFFFu) return -1;
}
auto base = Link->uniformLocations.find(name.substr(0, bracket) + "[0]");
if (base == Link->uniformLocations.end()) {
base = Link->uniformLocations.find(name.substr(0, bracket));
if (base == Link->uniformLocations.end()) return -1;
}
const Int baseLocation = static_cast<Int>(base->second);
if (!IsValidUniformLocation(baseLocation)) return -1;
const Int index = Link->uniformIndexInTProgram[baseLocation];
if (!UniformAt(index).type.isArray) return -1;
if (static_cast<GLint>(element) >=
MG_State::GLState::ProgramObject::GetUniformArraySizeByTIndex(*Link, index)) {
return -1;
}
const Int location = baseLocation + static_cast<Int>(element);
if (!UniformLocationsAliasSameUniform(baseLocation, location)) return -1;
return location;
}
const String& ProgramArchiveSource::GetUniformBlockName(Uint index) const {
static const String kEmpty;
if (index >= Link->glBlockIndexToTProgram.size()) return kEmpty;
const Int tBlockIndex = Link->glBlockIndexToTProgram[index];
if (tBlockIndex < 0 || static_cast<SizeT>(tBlockIndex) >= Link->blockReflection.size()) {
return kEmpty;
}
return Link->blockReflection[tBlockIndex].name;
}
Uint ProgramArchiveSource::GetUniformBlockBinding(Uint index) const {
// THE OVERLAY WINS WHEN THERE IS ONE, and the archive's link-time value stands when
// there is not. glUniformBlockBinding moves this AFTER the link that produced the
// archive, so a twin answering from the archive alone would bind the program's
// blocks to the points it was LINKED with rather than the ones it is BOUND with.
if (OverlayGoverns && BlockBindingOverlay != nullptr) {
if (index < BlockBindingOverlay->size()) {
return static_cast<Uint>((*BlockBindingOverlay)[index]);
}
// A block past the tail the record declared. The tail is dense over
// GetActiveUniformBlocksCount(), so this is a record and an archive that
// disagree about how many blocks the program has - which can only happen if a
// create and a bindings record crossed. 0 is GL's own default and is the safe
// answer; the mismatch is the emitter's to fix, not this read's to hide.
return 0;
}
if (index >= Link->uniformBlockBinding.size()) return 0;
return static_cast<Uint>(Link->uniformBlockBinding[index]);
}
Int ProgramArchiveSource::GetUniformSamplerOrImageUnitIndex(Uint location) const {
if (OverlayGoverns) {
// THE TAIL IS THE WHOLE SET, so a location it does not name has no unit - NOT
// whatever the archive's link-time snapshot said. The client emits every
// assigned unit it can see, including the ones the link seeded from
// layout(binding=N), so "absent" really does mean -1.
//
// Sparse and ASCENDING BY LOCATION on the wire (CONTRACT-P5E §1), which is what
// lets this be a binary search: the build walks every location from 0 to
// maxUniformLocation, and a linear scan per location would be quadratic on a
// program with a thousand locations and a handful of samplers.
if (SamplerUnitOverlay == nullptr) return -1;
const auto it = std::lower_bound(
SamplerUnitOverlay->begin(), SamplerUnitOverlay->end(), location,
[](const MG_Pipe::MGPProgramSamplerUnit& entry, Uint value) {
return entry.Location < value;
});
if (it == SamplerUnitOverlay->end() || it->Location != location) return -1;
return static_cast<Int>(it->Unit);
}
if (location >= Link->uniformSamplerOrImageUnitIndex.size()) return -1;
return Link->uniformSamplerOrImageUnitIndex[location];
}
ProgramArchiveSource ProgramArchiveSource::FromFrontend(
const MG_State::GLState::ProgramObject& program) {
// THE MONOLITH-GLUE HALF (ruling 1 / ID-81). The "archive" is the frontend's own
// live tables, which is what makes this arm byte-for-byte what it was: there is no
// overlay because the three mutable fields ARE those tables' members, and the
// program object is pinned by the draw that is reading it.
ProgramArchiveSource source;
source.Link = &program.GetLinkReflection();
source.Spirv = &program.GetSpirvReflection();
source.Identity = program.GetExternalIndex();
source.IdentityIsHandleSlot = false;
source.Linked = program.GetLinkStatus();
source.SpirvUsable = program.GetSpirvStatus();
source.SpirvValidationEnabled = program.GetSpirvValidationEnabled();
source.PointSizeWasDemoted = program.PointSizeDemoted();
source.GlobalUboSize = program.GetUBOSize();
source.LinkedStages = program.GetLinkedShaderStages();
source.OverlayGoverns = false;
source.StorageOverrides = program.GetShaderStorageBlockBindingOverrides();
// The function this source's ComputeShaderStorageBlockBindingSignature overload
// replaces, so the monolith answer is unchanged and there is still exactly one
// place the number comes from on this arm.
source.StorageOverrideSignature = ComputeShaderStorageBlockBindingSignatureOf(program);
return source;
}
ProgramArchiveSource ProgramArchiveSource::FromRecord(
MG_Pipe::MGPipeHandle cso, const MG_Pipe::MGPipeShaderCsoRecord& record) {
// THE HANDLE ARM. Every field below is server-owned: the archive the create adopted,
// the descriptor the create stored, and the three tails set_program_bindings
// carried. Nothing here touches a ProgramObject, which is the property the strict
// lane measures.
ProgramArchiveSource source;
source.Link = &record.Archive->Link;
source.Spirv = &record.Archive->Spirv;
source.Identity = cso.Slot;
source.IdentityIsHandleSlot = true;
// LinkStatus IS A FIELD NOW, and that is the whole point of c0e adding it: before
// P5e "linked" was only implied by the create existing, which is a different
// statement - create_shader_state is re-issued at every link that moves the link
// version, and a FAILED relink of a bound program moves it too (ID-88).
source.Linked = record.Desc.LinkStatus != 0;
source.SpirvUsable = record.Desc.SpirvStatus != 0;
source.SpirvValidationEnabled = record.Desc.EnableSpirvValidation != 0;
source.PointSizeWasDemoted = record.Desc.PointSizeDemoted != 0;
source.GlobalUboSize = record.Desc.GlobalUboSize;
source.LinkedStages.reserve(record.Archive->LinkedStages.size());
for (const Uint32 stage : record.Archive->LinkedStages) {
source.LinkedStages.push_back(static_cast<ShaderStage>(stage));
}
// "HAS A BINDINGS RECORD EVER BEEN APPLIED TO THIS RECORD" - not "is the tail
// non-empty". A program with no uniform blocks, no sampler and no override emits a
// record with three empty tails, and that record is the statement that the archive's
// link-time values are no longer the answer.
source.OverlayGoverns = record.BindingsSerial != 0;
source.BlockBindingOverlay = &record.BlockBindings;
source.SamplerUnitOverlay = &record.SamplerUnits;
if (source.OverlayGoverns) {
source.StorageOverrides.reserve(record.StorageOverrides.size());
for (const auto& entry : record.StorageOverrides) {
source.StorageOverrides[entry.Name] = entry.Binding;
}
} else {
source.StorageOverrides = record.Archive->Link.shaderStorageBlockBinding;
}
// THE RECORD'S NUMBER EITHER WAY, and deliberately not a recomputation when the
// overlay is absent. This is the value the twin STAMPS and the draw path's clean
// clause COMPARES (CONTRACT-P5E §5.5 names record.Signature as the input), so the
// two have to be the same field or every draw of a program with an override
// rebuilds. A record with no bindings applied carries 0 while the archive may hold
// a link-time override map - the next set_program_bindings then produces a non-zero
// signature and forces exactly one rebuild, which is correct and, since the emitter
// sends the bindings in the same breath as the create, not a state a draw reaches.
source.StorageOverrideSignature = record.Signature;
return source;
}
Uint ProgramBlockBindingFromRecord(const MG_Pipe::MGPipeShaderCsoRecord& record, Int blockIndex) {
if (blockIndex < 0) return 0;
const SizeT index = static_cast<SizeT>(blockIndex);
if (record.BindingsSerial != 0) {
// The tail is the whole set; a block past it is a record and an archive that
// disagree about the block count, and GL's own default of 0 is the safe answer.
return index < record.BlockBindings.size()
? static_cast<Uint>(record.BlockBindings[index])
: 0;
}
if (!record.Archive || index >= record.Archive->Link.uniformBlockBinding.size()) return 0;
return static_cast<Uint>(record.Archive->Link.uniformBlockBinding[index]);
}
Int ProgramSamplerUnitFromRecord(const MG_Pipe::MGPipeShaderCsoRecord& record, Uint location) {
if (record.BindingsSerial != 0) {
const auto it = std::lower_bound(
record.SamplerUnits.begin(), record.SamplerUnits.end(), location,
[](const MG_Pipe::MGPProgramSamplerUnit& entry, Uint value) {
return entry.Location < value;
});
if (it == record.SamplerUnits.end() || it->Location != location) return -1;
return static_cast<Int>(it->Unit);
}
if (!record.Archive ||
location >= record.Archive->Link.uniformSamplerOrImageUnitIndex.size()) {
return -1;
}
return record.Archive->Link.uniformSamplerOrImageUnitIndex[location];
}
// P5e (id), CONTRACT-P5E §4.1. See Managers.h for the contract - in particular why the
// composite band had to land in the same package as this function.
BackendProgramObjectImpl* ResolveProgramTwin(MG_Pipe::MGPipeHandle cso) {
@@ -10911,8 +11136,8 @@ namespace MobileGL::MG_Backend::DirectGLES {
}
void ReseedShaderStorageBlockBindings(Uint backendProgramId,
const MG_State::GLState::ProgramObject& stateProgramObject) {
const auto& overrides = stateProgramObject.GetShaderStorageBlockBindingOverrides();
const ProgramBuildSource& src) {
const auto& overrides = src.GetShaderStorageBlockBindingOverrides();
if (overrides.empty()) return; // the overwhelming majority of programs
for (const auto& [blockName, binding] : overrides) {
if (binding < 0) continue;
@@ -10920,9 +11145,51 @@ namespace MobileGL::MG_Backend::DirectGLES {
}
}
Uint64 ComputeShaderStorageBlockBindingSignature(
const MG_State::GLState::ProgramObject& stateProgramObject) {
const auto& overrides = stateProgramObject.GetShaderStorageBlockBindingOverrides();
#if MOBILEGL_PIPE_PUSH
// P5e (pg). ON A PUSH BUILD THE SIGNATURE IS THE SOURCE'S, TAKEN VERBATIM, and it is not
// recomputed here for a reason worth stating: on the handle arm the number the DRAW PATH
// compares against is MGPipeShaderCsoRecord::Signature - the CLIENT's commutative hash,
// computed in MG_Impl/Pipe/ProgramEmit.h. If the server recomputed its own from the
// record's override tail, the two formulas would have to stay bit-identical for ever or
// every draw of every program with an override would rebuild. Taking the client's number
// verbatim removes that coupling: there is one authority, and the twin's stamp and the
// clean clause read the same field. The monolith arm's source carries the answer of the
// function this replaces, computed from the frontend map, so its behaviour is unchanged.
Uint64 ComputeShaderStorageBlockBindingSignature(const ProgramBuildSource& src) {
return src.StorageOverrideSignature;
}
// The COMPUTATION, still here and still the only one on this side: FromFrontend seeds a
// monolith source with it, and the monolith arm of SyncCurrentProgram's clean condition
// asks it per draw exactly as it always did. In a pull build this function does not
// exist and the overload above IS this body (see the #else), so the pull build gains no
// symbol - G1's rule for every new name in this phase.
Uint64 ComputeShaderStorageBlockBindingSignatureOf(
const MG_State::GLState::ProgramObject& program) {
const auto& overrides = program.GetShaderStorageBlockBindingOverrides();
if (overrides.empty()) return 0; // the overwhelming majority of programs
// Order-independent on purpose: the source is an UnorderedMap, so any signature that
// depended on iteration order would differ between two identical override sets and
// rebuild the program for nothing. Built from the VALUES, not from a change counter,
// so re-setting a block to the binding it already carries forces no rebuild - which
// is what the pipeline composite's per-draw uniform mirror depends on.
//
// MG_Impl/Pipe/ProgramEmit.h's MGPipeStorageOverrideSignatureEntry is this entry
// mix, verbatim, on the client side; the two are twins with a named pointer at each
// other because MG_Backend includes nothing from MG_Impl.
Uint64 signature = 0;
for (const auto& [blockName, binding] : overrides) {
if (binding < 0) continue; // never rebound; the declared qualifier still stands
Uint64 entry = std::hash<String>{}(blockName);
entry ^= (static_cast<Uint64>(static_cast<Uint32>(binding)) + 0x9e3779b97f4a7c15ull +
(entry << 6) + (entry >> 2));
signature += entry; // commutative combine
}
return signature;
}
#else
Uint64 ComputeShaderStorageBlockBindingSignature(const ProgramBuildSource& src) {
const auto& overrides = src.GetShaderStorageBlockBindingOverrides();
if (overrides.empty()) return 0; // the overwhelming majority of programs
// Order-independent on purpose: the source is an UnorderedMap, so any signature that
// depended on iteration order would differ between two identical override sets and
@@ -10944,6 +11211,7 @@ namespace MobileGL::MG_Backend::DirectGLES {
}
return signature;
}
#endif
namespace {
// The GL internal format bound to an image unit right now. GL_NONE for a unit
@@ -11120,8 +11388,7 @@ namespace MobileGL::MG_Backend::DirectGLES {
// reflection: which image uniforms declared NO format (the only ones a bake may touch -
// a declared format is authoritative and stays), what the units they address currently
// hold, and whether any format in play - declared or baked - is outside the ES core set.
ImageFormatBakeInputs CollectImageFormatBakeInputs(
const MG_State::GLState::ProgramObject& stateProgramObject) {
ImageFormatBakeInputs CollectImageFormatBakeInputs(const ProgramBuildSource& src) {
ImageFormatBakeInputs inputs;
// A format GLSL ES cannot spell on a driver with no GL_NV_image_formats to spell it
// with. There is no legal ESSL for such a shader at all, so the stage will not
@@ -11139,12 +11406,12 @@ namespace MobileGL::MG_Backend::DirectGLES {
++unspellableCount;
};
const Uint maxUniformLoc = stateProgramObject.GetMaxUniformLocation();
const Uint maxUniformLoc = src.GetMaxUniformLocation();
for (Uint loc = 0; loc <= maxUniformLoc; ++loc) {
const auto& name = stateProgramObject.GetUniformName(loc);
const auto& name = src.GetUniformName(loc);
if (name.empty()) continue;
if (!IsImageUniformType(stateProgramObject.GetUniformType(loc))) continue;
const auto& type = stateProgramObject.GetUniformTypeFacts(loc);
if (!IsImageUniformType(src.GetUniformType(loc))) continue;
const auto& type = src.GetUniformTypeFacts(loc);
if (type.hasFormat) {
// Declared, and therefore never overridden by the BAKE - but a non-core
// spelling still has to become legal ESSL somehow.
@@ -11170,7 +11437,7 @@ namespace MobileGL::MG_Backend::DirectGLES {
}
continue;
}
const Int unit = stateProgramObject.GetUniformSamplerOrImageUnitIndex(loc);
const Int unit = src.GetUniformSamplerOrImageUnitIndex(loc);
if (unit < 0 || unit >= MG_State::GLState::TextureState::MAX_TEXTURE_IMAGE_UNITS) continue;
const Uint boundFormat = BoundImageUnitFormat(unit);
@@ -11289,27 +11556,26 @@ namespace MobileGL::MG_Backend::DirectGLES {
// so there is nothing to fix at the API end and the emitted text has to carry it
// (RemapImageArrayElementUnits). Empty for every program that does not do this, which is
// very nearly all of them - one walk of the reflection and no allocation in that case.
Vector<ImageArrayUnitPlan> CollectNonConsecutiveImageArrayPlans(
const MG_State::GLState::ProgramObject& stateProgramObject) {
Vector<ImageArrayUnitPlan> CollectNonConsecutiveImageArrayPlans(const ProgramBuildSource& src) {
Vector<ImageArrayUnitPlan> plans;
const Uint maxUniformLoc = stateProgramObject.GetMaxUniformLocation();
const Uint maxUniformLoc = src.GetMaxUniformLocation();
for (Uint loc = 0; loc <= maxUniformLoc; ++loc) {
const auto& name = stateProgramObject.GetUniformName(loc);
const auto& name = src.GetUniformName(loc);
if (name.empty()) continue;
if (!IsImageUniformType(stateProgramObject.GetUniformType(loc))) continue;
if (!IsImageUniformType(src.GetUniformType(loc))) continue;
// Reflection repeats the array's "g_image[0]" spelling at EVERY location the array
// spans, so only the location that name resolves back to is the array itself.
if (stateProgramObject.GetUniformLocation(name) != static_cast<Int>(loc)) continue;
if (src.GetUniformLocation(name) != static_cast<Int>(loc)) continue;
const String baseName = ImageUniformBaseName(name);
if (baseName == name) continue; // a scalar image: one binding says it all
ImageArrayUnitPlan plan;
plan.name = baseName;
for (Uint element = loc; element <= maxUniformLoc &&
stateProgramObject.UniformLocationsAliasSameUniform(
src.UniformLocationsAliasSameUniform(
static_cast<Int>(loc), static_cast<Int>(element));
++element) {
plan.units.push_back(stateProgramObject.GetUniformSamplerOrImageUnitIndex(element));
plan.units.push_back(src.GetUniformSamplerOrImageUnitIndex(element));
}
if (plan.units.size() < 2) continue;
@@ -11905,7 +12171,7 @@ namespace MobileGL::MG_Backend::DirectGLES {
// the program exactly as dead as it already was - but with a driver log that says why,
// where today there is an empty one.
void BackendProgramObjectImpl::AttachPassthroughTessControlStage(
const MG_State::GLState::ProgramObject& stateProgramObject, const Int tessEvalShaderIndex,
const ProgramBuildSource& src, const Int tessEvalShaderIndex,
const Vector<Vector<unsigned int>>& shaderSpirvs, const String& vertexStageEssl,
const String& tessEvalStageEssl) {
// PATCH_VERTICES is dynamic state, and it decides the synthesized stage's output
@@ -11932,7 +12198,7 @@ namespace MobileGL::MG_Backend::DirectGLES {
MGLOG_E("Program %u has a tessellation evaluation stage with no control stage, but no "
"SPIR-V for it; the pass-through control stage GL describes cannot be checked, so "
"the program is left to fail its ES link.",
stateProgramObject.GetExternalIndex());
src.GetExternalIndex());
m_backendProgramUsable = false;
return;
}
@@ -11948,7 +12214,7 @@ namespace MobileGL::MG_Backend::DirectGLES {
MGLOG_E("Program %u has a tessellation evaluation stage with no control stage AND reads a "
"user-defined input through it; a synthesized pass-through control stage cannot "
"forward that, so the program is declined rather than fed an undefined varying.",
stateProgramObject.GetExternalIndex());
src.GetExternalIndex());
m_backendProgramUsable = false;
return;
}
@@ -11986,7 +12252,7 @@ namespace MobileGL::MG_Backend::DirectGLES {
if (backendShaderId == 0) {
MGLOG_E("Failed to create the synthesized pass-through tessellation control shader for "
"program %u.",
stateProgramObject.GetExternalIndex());
src.GetExternalIndex());
m_backendProgramUsable = false;
return;
}
@@ -11994,7 +12260,7 @@ namespace MobileGL::MG_Backend::DirectGLES {
const char* sourceCStr = source.c_str();
MGLOG_D("Synthesized pass-through tessellation control stage for program %u (patch vertices "
"%u):\n%s",
stateProgramObject.GetExternalIndex(), patchVertices, sourceCStr);
src.GetExternalIndex(), patchVertices, sourceCStr);
g_GLESFuncs.glShaderSource(backendShaderId, 1, &sourceCStr, nullptr);
g_GLESFuncs.glCompileShader(backendShaderId);
@@ -12011,7 +12277,7 @@ namespace MobileGL::MG_Backend::DirectGLES {
log.back() = '\0';
MGLOG_E("The synthesized pass-through tessellation control stage failed to compile for "
"program %u. Driver log: %s\nSource:\n%s",
stateProgramObject.GetExternalIndex(), log.data(), sourceCStr);
src.GetExternalIndex(), log.data(), sourceCStr);
m_backendProgramUsable = false;
g_GLESFuncs.glDeleteShader(backendShaderId);
return;
@@ -12023,6 +12289,25 @@ namespace MobileGL::MG_Backend::DirectGLES {
g_GLESFuncs.glDeleteShader(backendShaderId);
}
// ---- THE PROGRAM BUILD, AND THE ONE SOURCE IT READS (P5e pg) ----------------------
//
// ONE BODY, TWO HEADS, AND THE PULL BUILD'S HEAD IS THE ORIGINAL ONE. In the pull build
// `ProgramBuildSource` IS `MG_State::GLState::ProgramObject`, this function keeps its
// name, its signature and its null guard, and `src` is a reference to the object it was
// already dereferencing on every line - so the pull build compiles the same reads
// through the same accessors and G1's byte identity survives a rename and nothing else.
//
// In a PUSH build the body is a private worker that the two public overloads feed:
// SyncToBackend(program) - the monolith-glue half, ruling 1 - wraps the frontend's own
// archive, and SyncToBackendByHandle(cso) wraps the RECORD's. That is the whole of this
// family's rule F: after this, nothing in a program build dereferences a frontend
// object, and the forty accessor calls below read memory the server owns.
#if MOBILEGL_PIPE_PUSH
void BackendProgramObjectImpl::SyncToBackendFromSource(const ProgramBuildSource& src) {
#ifdef TRACY_ENABLE
ZoneScopedC(TRACY_ZONECOLOR_BACKEND);
#endif
#else
void BackendProgramObjectImpl::SyncToBackend(
const SharedPtr<MG_State::GLState::ProgramObject>& stateProgramObject) {
#ifdef TRACY_ENABLE
@@ -12032,24 +12317,26 @@ namespace MobileGL::MG_Backend::DirectGLES {
MGLOG_E_ONCE("State program object is null, skipping backend sync.");
return;
}
const ProgramBuildSource& src = *stateProgramObject;
#endif
// Recorded before either early return below, so Use() can always name the GL
// program a no-op draw belongs to - including the "linked but not drawable" exit.
m_frontendProgramId = stateProgramObject->GetExternalIndex();
m_frontendProgramId = src.GetExternalIndex();
// GetSpirvStatus() as well as GetLinkStatus(): a program whose phase-B job was
// cancelled (teardown) or whose optimizer run failed is fully linked and fully
// queryable, but has no SPIR-V to build a driver program out of. GL cannot retract
// a LINK_STATUS it already reported true, so "linked but not drawable" is the
// answer, and this is where the ES backend expresses it.
if (!stateProgramObject->GetLinkStatus() || !stateProgramObject->GetSpirvStatus()) {
if (!src.GetLinkStatus() || !src.GetSpirvStatus()) {
MGLOG_E_ONCE("Program object is not linked or has no generated SPIR-V, skipping backend sync. State "
"program ID: %u",
stateProgramObject->GetExternalIndex());
src.GetExternalIndex());
return;
}
MGLOG_D("Syncing program to backend. State program ID: %u, Backend ID: %u",
stateProgramObject->GetExternalIndex(), m_backendProgramId);
src.GetExternalIndex(), m_backendProgramId);
// Every link-derived cache below (incl. m_samplerUniformBindings and its
// lastAssignedUnit/lastAssignedLodBias program-state mirrors) is rebuilt;
// the sampler-pass memo keyed on them must not survive.
@@ -12061,8 +12348,8 @@ namespace MobileGL::MG_Backend::DirectGLES {
// The generated ESSL bakes these in (see the SetShaderStorageBlockBinding call in the
// transpile loop below), so the set they were generated against is part of what makes
// this build current - the draw path compares the signature and rebuilds on a change.
const auto& storageBlockBindingOverrides = stateProgramObject->GetShaderStorageBlockBindingOverrides();
m_shaderStorageBlockBindingSignature = ComputeShaderStorageBlockBindingSignature(*stateProgramObject);
const auto& storageBlockBindingOverrides = src.GetShaderStorageBlockBindingOverrides();
m_shaderStorageBlockBindingSignature = ComputeShaderStorageBlockBindingSignature(src);
// Rebuilt by the transpile loop below, one entry per atomic-counter block it finds.
// The top is snapshotted here so every stage of this program - and the draw path
// reading it afterwards - resolves the same slot for the same GL binding.
@@ -12079,19 +12366,19 @@ namespace MobileGL::MG_Backend::DirectGLES {
// compiles to depends on live glBindImageTexture state, so the pairs it was built
// against are recorded here and compared per draw (ImageUnitFormatsStillMatch).
// Taken BEFORE the transpile loop so both the bake and the key see one snapshot.
const ImageFormatBakeInputs imageFormatBake = CollectImageFormatBakeInputs(*stateProgramObject);
const ImageFormatBakeInputs imageFormatBake = CollectImageFormatBakeInputs(src);
m_formatlessImageUnits = imageFormatBake.units;
m_imageUnitFormatSignature = imageFormatBake.signature;
for (const auto& conflicted : imageFormatBake.conflictedNames) {
MGLOG_D("Image uniform '%s' of program %u declares no format and its elements address units with "
"different bound formats; left format-less.",
conflicted.c_str(), stateProgramObject->GetExternalIndex());
conflicted.c_str(), src.GetExternalIndex());
}
// ...and once more for image ARRAYS whose per-element units are not consecutive, which
// ESSL has no way to express in one declaration. Program-wide, like the bake, and read
// from the same snapshot of the reflection; the per-stage rewrite happens below.
const Vector<ImageArrayUnitPlan> nonConsecutiveImageArrays =
CollectNonConsecutiveImageArrayPlans(*stateProgramObject);
CollectNonConsecutiveImageArrayPlans(src);
// Detach all existing shaders
GLint attachedCount = 0;
@@ -12128,19 +12415,19 @@ namespace MobileGL::MG_Backend::DirectGLES {
// straight off the end of shaderSpirvs (a std::vector copy from garbage, which is
// how this crashed). GetLinkedShaderStages() is the list the modules were generated
// from, one entry per module, in module order.
const Vector<ShaderStage> linkedStages = stateProgramObject->GetLinkedShaderStages();
auto& shaderSpirvs = stateProgramObject->GetGeneratedSpirv();
const Vector<ShaderStage> linkedStages = src.GetLinkedShaderStages();
auto& shaderSpirvs = src.GetGeneratedSpirv();
// Both come from the same Link(), so they agree by construction. If they ever did
// not there would be no index this function could safely use for EITHER array, so
// this refuses the build instead of picking one and hoping.
if (linkedStages.size() != shaderSpirvs.size()) {
MGLOG_E_ONCE("Program %u: %zu linked stage(s) but %zu generated SPIR-V module(s); refusing to "
"build a backend program from mismatched link artifacts.",
stateProgramObject->GetExternalIndex(), linkedStages.size(), shaderSpirvs.size());
src.GetExternalIndex(), linkedStages.size(), shaderSpirvs.size());
m_backendProgramUsable = false;
return;
}
if (stateProgramObject->PointSizeDemoted()) {
if (src.PointSizeDemoted()) {
// THE ARMING SIGNAL, INFO on purpose and latched: the integration lane that
// pins MOBILEGL_POINT_SIZE_DEMOTION=1 asserts on exactly this line, because
// every rendering assertion stays green on a healthy driver whether the
@@ -12150,7 +12437,7 @@ namespace MobileGL::MG_Backend::DirectGLES {
"in those stages.");
}
MGLOG_D("Attaching %zu shaders to program %u", linkedStages.size(), m_backendProgramId);
for (const auto& ref : stateProgramObject->GetLinkedShaderSnapshot()) {
for (const auto& ref : src.GetLinkedShaderSnapshot()) {
if (!ref.shader) continue;
const auto& stage =
MG_Util::ConvertGLEnumToString(MG_Util::ConvertShaderStageToGLEnum(ref.shader->GetShaderStage()));
@@ -12159,7 +12446,7 @@ namespace MobileGL::MG_Backend::DirectGLES {
MGLOG_D("Original src @ %s: \n", stage.c_str());
MGLOG_D("%s:", src.empty() ? "" : src.c_str());
}
const Bool enableSpirvValidation = stateProgramObject->GetSpirvValidationEnabled();
const Bool enableSpirvValidation = src.GetSpirvValidationEnabled();
// Blocks a transform-feedback capture request names a member of ("StageData" of
// "StageData.attrib[0]"). The Adreno ES driver accepts such a request, links, and
@@ -12168,7 +12455,7 @@ namespace MobileGL::MG_Backend::DirectGLES {
// consumer keep matching. gl_PerVertex members ("gl_Position") carry no block
// prefix and so never enter this set.
std::set<String> xfbCaptureBlockNames;
for (const auto& xfbVarying : stateProgramObject->GetTransformFeedbackVaryings()) {
for (const auto& xfbVarying : src.GetTransformFeedbackVaryings()) {
const SizeT dot = xfbVarying.name.find('.');
if (dot != String::npos && dot > 0) {
xfbCaptureBlockNames.insert(xfbVarying.name.substr(0, dot));
@@ -12495,7 +12782,7 @@ namespace MobileGL::MG_Backend::DirectGLES {
// KHR-GL43.vertex_attrib_binding family behind "the draw captured zeros".
MGLOG_E("Shader transpilation to ESSL failed. State program ID: %u, stage: %s, "
"SPIRV-Cross error: %s",
stateProgramObject->GetExternalIndex(),
src.GetExternalIndex(),
MG_Util::ConvertGLEnumToString(glShaderType).c_str(),
transpileError.c_str());
m_backendProgramUsable = false;
@@ -12587,13 +12874,13 @@ namespace MobileGL::MG_Backend::DirectGLES {
"available on this device.",
tessellationStage ? "tessellation" : "geometry",
tessellationStage ? "tessellation" : "geometry",
stateProgramObject->GetExternalIndex());
src.GetExternalIndex());
}
source = RequestPointSizeExtension(std::move(source), pointSizeExtension);
}
}
source = RebindImageUniformsToFrontendUnits(std::move(source), stateProgramObject);
source = RebindImageUniformsToFrontendUnits(std::move(source), src);
// The completion half of the format bake, for the formats SPIRV-Cross throws on
// rather than prints (r8ui and the rest of its desktop-only set). Empty for every
// program whose format-less images bound a format the module could carry, which
@@ -12623,7 +12910,7 @@ namespace MobileGL::MG_Backend::DirectGLES {
MGLOG_E("Image array %s. Its elements address image units GLSL ES cannot be made to reach "
"from one declaration, so this stage will read and write the WRONG units. State "
"program ID: %u, stage: %s.",
declined.c_str(), stateProgramObject->GetExternalIndex(),
declined.c_str(), src.GetExternalIndex(),
MG_Util::ConvertGLEnumToString(glShaderType).c_str());
}
}
@@ -12668,7 +12955,7 @@ namespace MobileGL::MG_Backend::DirectGLES {
MGLOG_E("Program %u routes gl_ViewportIndex but its fragment stage has no "
"entry point to gate, so the routing cannot be emulated: every "
"index will rasterize against viewport 0. State program ID: %u.",
m_backendProgramId, stateProgramObject->GetExternalIndex());
m_backendProgramId, src.GetExternalIndex());
}
} else if (PromoteViewportIndexGlobalToVarying(source)) {
programRoutesViewportIndex = true;
@@ -12749,7 +13036,7 @@ namespace MobileGL::MG_Backend::DirectGLES {
}
MGLOG_E("Shader compilation failed. State program ID: %u, stage: %s, backend shader ID: "
"%u, driver log: %s\nSource:\n%s",
stateProgramObject->GetExternalIndex(),
src.GetExternalIndex(),
MG_Util::ConvertGLEnumToString(glShaderType).c_str(), backendShaderId,
log.data(), sourceForLog);
m_backendProgramUsable = false;
@@ -12790,7 +13077,7 @@ namespace MobileGL::MG_Backend::DirectGLES {
}
if (needsPassthroughTessControl) {
AttachPassthroughTessControlStage(*stateProgramObject, tessEvalShaderIndex, shaderSpirvs,
AttachPassthroughTessControlStage(src, tessEvalShaderIndex, shaderSpirvs,
vertexStageEssl, tessEvalStageEssl);
}
@@ -12810,9 +13097,9 @@ namespace MobileGL::MG_Backend::DirectGLES {
// the transpiled ESSL (`out vec4 result_0;` stays `result_0`), so the
// frontend's requested names carry over unchanged.
SizeT declaredXfbVaryingCount = 0;
if (stateProgramObject->GetTransformFeedbackVaryingCount() > 0 &&
if (src.GetTransformFeedbackVaryingCount() > 0 &&
g_GLESFuncs.glTransformFeedbackVaryings != nullptr) {
const auto& xfbVaryings = stateProgramObject->GetTransformFeedbackVaryings();
const auto& xfbVaryings = src.GetTransformFeedbackVaryings();
Vector<const GLchar*> xfbNames;
xfbNames.reserve(xfbVaryings.size());
// A block this build flattened no longer HAS the member the application asked
@@ -12827,7 +13114,7 @@ namespace MobileGL::MG_Backend::DirectGLES {
// capture stage is the vertex shader keeps the built-in and its spelling,
// whatever happened to a control stage behind it.
Bool captureStageDemoted = false;
if (stateProgramObject->PointSizeDemoted()) {
if (src.PointSizeDemoted()) {
for (const ShaderStage linkedStage : linkedStages) {
if (linkedStage == ShaderStage::TessEval || linkedStage == ShaderStage::Geometry) {
captureStageDemoted = true;
@@ -12860,7 +13147,7 @@ namespace MobileGL::MG_Backend::DirectGLES {
}
g_GLESFuncs.glTransformFeedbackVaryings(m_backendProgramId, static_cast<GLsizei>(xfbNames.size()),
xfbNames.data(),
stateProgramObject->GetTransformFeedbackBufferMode());
src.GetTransformFeedbackBufferMode());
// Unchecked before. A rejected capture set leaves the program linking happily
// with NO capture set at all, and then every draw of every span records
// nothing while the application reads its buffer's pre-draw bytes and
@@ -12875,8 +13162,8 @@ namespace MobileGL::MG_Backend::DirectGLES {
"%s (mode %s): [%s]. Every capture made with GL program %u will record nothing.",
m_backendProgramId, MG_Util::ConvertGLEnumToString(xfbError).c_str(),
MG_Util::ConvertGLEnumToString(
stateProgramObject->GetTransformFeedbackBufferMode()).c_str(),
declared.c_str(), stateProgramObject->GetExternalIndex());
src.GetTransformFeedbackBufferMode()).c_str(),
declared.c_str(), src.GetExternalIndex());
}
declaredXfbVaryingCount = xfbNames.size();
}
@@ -12899,7 +13186,7 @@ namespace MobileGL::MG_Backend::DirectGLES {
// links nothing no-ops every draw that uses it, and that has to be readable
// in an INFO-level artifact.
MGLOG_E("Program linking failed. State program ID: %u, backend program ID: %u, driver log: %s",
stateProgramObject->GetExternalIndex(), m_backendProgramId, log.data());
src.GetExternalIndex(), m_backendProgramId, log.data());
// The one link failure MobileGL can name a cause for that the driver's log never
// will: ESSL has no legal single declaration for a read+write image outside
// r32f/r32i/r32ui, so those are split into a coherent pair and the stage ends up
@@ -12939,13 +13226,13 @@ namespace MobileGL::MG_Backend::DirectGLES {
&linkedXfbBufferMode);
for (Int i = 0; i < kMaxDrainedProgramErrors && g_GLESFuncs.glGetError() != GL_NO_ERROR; ++i) {
}
const GLenum requestedMode = stateProgramObject->GetTransformFeedbackBufferMode();
const GLenum requestedMode = src.GetTransformFeedbackBufferMode();
if (static_cast<SizeT>(std::max(linkedXfbVaryings, 0)) != declaredXfbVaryingCount ||
static_cast<GLenum>(linkedXfbBufferMode) != requestedMode) {
MGLOG_E("Backend program %u (GL program %u) linked with a capture set the driver does not "
"agree with: asked for %zu varying(s) in mode %s, the driver reports %d varying(s) "
"in mode %s. Captures made with it will be empty or wrongly laid out.",
m_backendProgramId, stateProgramObject->GetExternalIndex(), declaredXfbVaryingCount,
m_backendProgramId, src.GetExternalIndex(), declaredXfbVaryingCount,
MG_Util::ConvertGLEnumToString(requestedMode).c_str(), linkedXfbVaryings,
MG_Util::ConvertGLEnumToString(
static_cast<GLenum>(linkedXfbBufferMode)).c_str());
@@ -13010,55 +13297,75 @@ namespace MobileGL::MG_Backend::DirectGLES {
}
// Create global UBO
if (stateProgramObject->GetUBOSize() > 0) {
if (src.GetUBOSize() > 0) {
g_GLESFuncs.glGenBuffers(1, &m_backendGlobalUBOId);
g_GLESFuncs.glBindBuffer(GL_UNIFORM_BUFFER, m_backendGlobalUBOId);
g_GLESFuncs.glBufferData(GL_UNIFORM_BUFFER, stateProgramObject->GetUBOSize(), nullptr, GL_STREAM_DRAW);
g_GLESFuncs.glBufferData(GL_UNIFORM_BUFFER, src.GetUBOSize(), nullptr, GL_STREAM_DRAW);
g_GLESFuncs.glBindBuffer(GL_UNIFORM_BUFFER, 0);
} else {
m_backendGlobalUBOId = 0;
}
CacheResourceLocations(stateProgramObject);
CacheResourceLocations(src);
// NOT the mechanism that makes a rebinding work - the transpiled qualifier above is.
// glShaderStorageBlockBinding is a GL 4.3 entry point that no real ES driver exposes,
// so this replay is a no-op almost everywhere; it stays because it is still correct
// (and cheaper than a rebuild) on a driver that does expose it, e.g. a desktop GL
// driver used as the ES backend. AFTER the link either way, because it needs the
// driver's linked interface.
ReseedShaderStorageBlockBindings(m_backendProgramId, *stateProgramObject);
ReseedShaderStorageBlockBindings(m_backendProgramId, src);
#if !MOBILEGL_PIPE_PUSH
m_syncedLinkVersion = src.GetLinkVersion();
m_syncedImageUnitVersion = src.GetImageUnitVersion();
#endif
m_isInitialized = true;
MGLOG_D("Program sync completed. backend ID %u", m_backendProgramId);
}
#if MOBILEGL_PIPE_PUSH
// ---- the two public heads (P5e pg) -----------------------------------------------
//
// THE CLEAN KEYS ARE STAMPED HERE AND NOT IN THE WORKER, because which key is
// authoritative is exactly what distinguishes the two arms - and a worker that stamped
// both would be claiming to know something it was not told. The monolith half keeps the
// frontend's two versions (and the record serial P4a added beside them); the handle half
// keeps the record's two serials and leaves the frontend versions at their
// never-matched sentinel, which is honest: on that arm nothing may ask a ProgramObject
// what its link version is.
void BackendProgramObjectImpl::SyncToBackend(
const SharedPtr<MG_State::GLState::ProgramObject>& stateProgramObject) {
#ifdef TRACY_ENABLE
ZoneScopedC(TRACY_ZONECOLOR_BACKEND);
#endif
if (!stateProgramObject) {
MGLOG_E_ONCE("State program object is null, skipping backend sync.");
return;
}
SyncToBackendFromSource(ProgramArchiveSource::FromFrontend(*stateProgramObject));
m_syncedLinkVersion = stateProgramObject->GetLinkVersion();
m_syncedImageUnitVersion = stateProgramObject->GetImageUnitVersion();
#if MOBILEGL_PIPE_PUSH
// P4a (D-B3): the ShaderCso record's Serial, stamped in the same breath as the two
// frontend versions it replaces. GetSyncedShaderCsoSerial() beside
// GetSyncedLinkVersion()/GetSyncedImageUnitVersion() is what the draw path's
// nine-clause rebuild condition reads on the handle arm; the clause COUNT does not
// shrink, its inputs move (D-H5).
//
// WHAT DOES *NOT* MOVE, and it is a design statement rather than an omission: the
// ARTEFACTS. D-H3 rules that in monolith all seven MGPBlobRefs are declared with
// Size 0 - "this record does not declare its blob" - and the LinkArtifacts /
// SpirvArtifacts ride beside the record through the entry point's companion
// pointers, so the applier stores the DESCRIPTOR and the identity and the server
// reads the frontend's own archive. That is what keeps the codec off the monolith
// hot path entirely, and it is why every artefact read above is still a read of
// stateProgramObject. The verify build is where the codec is exercised, by
// serialising, deserialising and field-comparing before storing.
if (ProgramSubsystemEnabled()) {
#if MOBILEGL_BUILD_DISAGGREGATED
// P5c (G6, CONTRACT-P5C §5.4): the program's ShaderCso handle is resolved by
// frontend identity below - frontend-keyed twin resolution, named debt inside
// the scope - P3b/P4b rekeys the registry onto handles.
// P5c (G6, CONTRACT-P5C §5.4): MONOLITH GLUE ONLY. This is the frontend-keyed
// lookup the phase exists to retire, and it survives here for the reason
// ruling 1 (ID-81) gives: under Transport=monolith the push build keeps its
// frontend arms token for token, and this arm is only ever reached from them.
// The handle half below names no frontend identity at all.
const MG_Pipe::MGPipeFrontendKeyedRegistryScope frontendKeyedRegistry;
#endif
// MONOLITH GLUE: the ShaderCso handle of a program this backend still arrives
// holding. The reader hides the composite band, so a program-pipeline composite
// - which the server must never learn is one - resolves through the same call.
// The reader hides the composite band, so a program-pipeline composite - which
// the server must never learn is one - resolves through the same call.
const MG_Pipe::MGPipeHandle cso = g_backendProgramObjects.HandleOf(stateProgramObject.get());
const auto* record = PipeShaderCsoRecordForHandle(cso);
if (record != nullptr) {
m_syncedShaderCsoSerial = record->Serial;
m_syncedBindingsSerial = record->BindingsSerial;
} else {
// NOT a fall-back and not a silent zero: a stamped 0 would make every later
// serial compare fire for ever, which is the safe direction but hides the
@@ -13068,12 +13375,50 @@ namespace MobileGL::MG_Backend::DirectGLES {
stateProgramObject->GetExternalIndex(), cso.Slot, cso.Gen);
}
}
#endif
m_isInitialized = true;
MGLOG_D("Program sync completed. backend ID %u", m_backendProgramId);
}
// P5e (pg), CONTRACT-P5E §5.5: THE BUILD THAT NAMES NO CLIENT MEMORY. Everything it
// reads is the record: the archive create_shader_state carried, the three binding tails
// set_program_bindings carried, and the descriptor. Nothing here resolves a frontend
// identity, probes the client allocator or holds a SharedPtr to a frontend object - the
// three absences rule F is made of.
void BackendProgramObjectImpl::SyncToBackendByHandle(MG_Pipe::MGPipeHandle cso) {
#ifdef TRACY_ENABLE
ZoneScopedC(TRACY_ZONECOLOR_BACKEND);
#endif
const auto* record = PipeShaderCsoRecordForHandle(cso);
if (record == nullptr) {
MGLOG_E_ONCE("MGPipe: shader CSO {%u, %u} has no applier record, so no driver "
"program can be built for it",
cso.Slot, cso.Gen);
return;
}
// THE ARCHIVE IS THE RECORD'S OR THE BUILD DOES NOT HAPPEN. A record with no archive
// is one the transport arm never filled - a monolith create that reached a handle
// arm - and building from an empty reflection would produce a program with no
// uniforms and no modules, i.e. a black screen with no marker anywhere. The
// create's own trip wire already refuses a record that declares no blobs AND
// carries no artefacts (PipeApply.cpp); this is the same statement one level later,
// where the twin can name the handle.
if (!record->Archive) {
MGLOG_E_ONCE("MGPipe: shader CSO {%u, %u} carries no server-owned archive; its "
"create_shader_state was emitted by the monolith arm and this is "
"the handle arm, so there is nothing to build from",
cso.Slot, cso.Gen);
m_backendProgramUsable = false;
return;
}
SyncToBackendFromSource(ProgramArchiveSource::FromRecord(cso, *record));
// The two server-owned keys the draw path's clean condition reads instead of
// GetLinkVersion()/GetImageUnitVersion(). BindingsSerial is the second because a
// glUniform1i on an IMAGE uniform is baked into the generated ESSL rather than
// re-issued per draw - the record that carries it must therefore force a rebuild,
// exactly as m_imageUnitVersion used to.
m_syncedShaderCsoSerial = record->Serial;
m_syncedBindingsSerial = record->BindingsSerial;
}
#endif
namespace {
// The GL name of the array element that lives at `location`, given the reflection
// name reported for it. Reflection reports one name per UNIFORM ("goku[0]") but
@@ -13081,7 +13426,7 @@ namespace MobileGL::MG_Backend::DirectGLES {
// repeatedly; this turns it back into "goku[k]". Anything that is not an array
// (or whose base location cannot be resolved) comes back unchanged, so the only
// behaviour that moves is the array case.
String SubscriptUniformNameForElement(const MG_State::GLState::ProgramObject& program, const String& name,
String SubscriptUniformNameForElement(const ProgramBuildSource& program, const String& name,
Uint location) {
if (name.size() < 3 || name.compare(name.size() - 3, 3, "[0]") != 0) return name;
const Int base = program.GetUniformLocation(name);
@@ -13096,13 +13441,12 @@ namespace MobileGL::MG_Backend::DirectGLES {
// (BindCurrentProgramWithResources) never issues glGetUniformBlockIndex /
// glGetUniformLocation string queries; block-to-binding-point assignments are
// program state and only need to be established here.
void BackendProgramObjectImpl::CacheResourceLocations(
const SharedPtr<MG_State::GLState::ProgramObject>& stateProgramObject) {
void BackendProgramObjectImpl::CacheResourceLocations(const ProgramBuildSource& src) {
m_globalUboBackendBlockIndex = -1;
m_globalUboBackendBlockSize = 0;
m_lastUploadedGlobalUboVersion = ~0u;
m_globalUboRingAllocation = {};
if (stateProgramObject->GetUBOSize() > 0) {
if (src.GetUBOSize() > 0) {
const Uint blockIndex =
g_GLESFuncs.glGetUniformBlockIndex(m_backendProgramId, MG_Util::ShaderTranspiler::GLOBAL_UBO_NAME);
if (blockIndex != GL_INVALID_INDEX) {
@@ -13119,16 +13463,16 @@ namespace MobileGL::MG_Backend::DirectGLES {
}
} else {
MGLOG_W_ONCE("Program %u has frontend global UBO storage, but backend has no %s block.",
stateProgramObject->GetExternalIndex(), MG_Util::ShaderTranspiler::GLOBAL_UBO_NAME);
src.GetExternalIndex(), MG_Util::ShaderTranspiler::GLOBAL_UBO_NAME);
}
}
const Int uboCount = stateProgramObject->GetActiveUniformBlocksCount();
const Int uboCount = src.GetActiveUniformBlocksCount();
m_uniformBlockBackendIndices.assign(static_cast<SizeT>(std::max(uboCount, 0)), -1);
Uint lastUBOBinding = 0; // binding 0 is reserved for the global UBO
for (Int i = 0; i < uboCount; ++i) {
++lastUBOBinding;
const auto& name = stateProgramObject->GetUniformBlockName(static_cast<Uint>(i));
const auto& name = src.GetUniformBlockName(static_cast<Uint>(i));
const GLuint backendBlkIdx = g_GLESFuncs.glGetUniformBlockIndex(m_backendProgramId, name.c_str());
if (backendBlkIdx == GL_INVALID_INDEX) {
// Either eliminated as unused, or an SSBO block (frontend reflection
@@ -13138,16 +13482,16 @@ namespace MobileGL::MG_Backend::DirectGLES {
m_uniformBlockBackendIndices[static_cast<SizeT>(i)] = static_cast<Int>(backendBlkIdx);
g_GLESFuncs.glUniformBlockBinding(m_backendProgramId, backendBlkIdx, lastUBOBinding);
MGLOG_D("CACHE prog=%u beProg=%u blk[%d]='%s' beIdx=%u -> bePoint=%u",
stateProgramObject->GetExternalIndex(), m_backendProgramId, i, name.c_str(), backendBlkIdx,
src.GetExternalIndex(), m_backendProgramId, i, name.c_str(), backendBlkIdx,
lastUBOBinding);
}
m_samplerUniformBindings.clear();
const Uint maxUniformLoc = stateProgramObject->GetMaxUniformLocation();
const Uint maxUniformLoc = src.GetMaxUniformLocation();
for (Uint loc = 0; loc <= maxUniformLoc; ++loc) {
const auto& name = stateProgramObject->GetUniformName(loc);
const auto& name = src.GetUniformName(loc);
if (name.empty()) continue;
const GLenum uniformType = stateProgramObject->GetUniformType(loc);
const GLenum uniformType = src.GetUniformType(loc);
if (IsImageUniformType(uniformType)) {
// ES image units come exclusively from the layout(binding=N) qualifier
// (preserved in the transpiled ESSL); glUniform1i on an image uniform
@@ -13163,7 +13507,7 @@ namespace MobileGL::MG_Backend::DirectGLES {
// Address each element by its own name instead; the frontend already
// reserves one location per element, so the element index is the distance
// from the array's base location.
const String elementName = SubscriptUniformNameForElement(*stateProgramObject, name, loc);
const String elementName = SubscriptUniformNameForElement(src, name, loc);
const Int backendLoc = g_GLESFuncs.glGetUniformLocation(m_backendProgramId, elementName.c_str());
if (backendLoc < 0) continue;
SamplerUniformBinding binding;
@@ -13858,11 +14202,11 @@ namespace MobileGL::MG_Backend::DirectGLES {
}
} // namespace FramebufferImpl
namespace PrgramImpl {
void BackendProgramObjectImpl::SyncToBackendByHandle(MG_Pipe::MGPipeHandle cso) {
MGPipeP5eSeamNotLanded("BackendProgramObjectImpl::SyncToBackendByHandle", "pg", cso);
}
} // namespace PrgramImpl
// PrgramImpl's seam is GONE, not stubbed: package pg wrote
// BackendProgramObjectImpl::SyncToBackendByHandle's real body beside the frontend overload
// it is the twin of (above, with the two ProgramArchiveSource heads). A second definition
// here would make the linker's answer depend on link order, which is the one failure this
// block's own comment was written to prevent.
namespace RenderbufferImpl {
void BackendRenderbufferObject::SyncToBackendByHandle(MG_Pipe::MGPipeHandle renderbuffer) {
+229 -13
View File
@@ -696,6 +696,26 @@ namespace MobileGL::MG_Backend::DirectGLES {
return enabled;
}
#if MOBILEGL_PIPE_PUSH
// P5e (pg), CONTRACT-P5E.md §5.8 / ruling 1 (ID-81): THE HANDLE ARM'S SELECTOR for the
// program family. Transport AND the family bit, in that order and both required:
//
// * `Transport != Monolith` because the push-MONOLITH build keeps its frontend arms token
// for token - the verify comparator needs them, and it is what makes
// MOBILEGL_IPC_RUN_AHEAD=0 a pure wait-rule A/B on identical server code rather than a
// comparison of two different backends;
// * the family bit because the A/B that switches this family off has to switch off the
// arm too, not only the emission - a server reading records nobody sends would draw
// with no program at all.
//
// NOT LATCHED, unlike the four above: Transport is configuration read once at bring-up and
// ProgramSubsystemEnabled() already latches, so this is one load and one test, and a latch
// here would only hide which half answered.
inline Bool ProgramHandleArm() {
return MG_Config::Transport != MG_Config::TransportMode::Monolith && ProgramSubsystemEnabled();
}
#endif
// ---- P4a: what the twins read INSTEAD of the frontend object ----
//
// One reader per record kind, all const, all null-on-miss, and all bounds-checked against
@@ -2342,6 +2362,173 @@ namespace MobileGL::MG_Backend::DirectGLES {
// Defined further down, next to CollectImageFormatBakeInputs; only referenced here.
struct ImageFormatBakeInputs;
#if MOBILEGL_PIPE_PUSH
// ---- P5e (pg), CONTRACT-P5E.md §5.5: THE ONE SOURCE A PROGRAM BUILD READS ----------
//
// WHY THIS TYPE EXISTS AT ALL. Building a driver program asks the frontend
// ProgramObject sixteen distinct questions and asks its reflection tables thousands of
// times, and EVERY ONE OF THEM used to be a read of client-owned memory on the apply
// thread. Under run-ahead that is not a race to be careful about, it is wrong by
// construction: ProgramObject::Link() REPLACES m_artifacts and m_spirv in place, and by
// the time the server builds, the client may be several links past the one the record
// describes. So the build reads THIS instead, and the two arms differ only in where it
// points.
//
// AND IT IS DELIBERATELY NOT A SECOND ProgramObject. Nothing here constructs a frontend
// object on the apply thread - ID-102's lesson, learned by id: a ProgramObject's own
// constructor mints a lifetime id, so an "object" built server-side would trip the very
// guards this phase installs and would make a red-once falsely green.
//
// THE OVERLAY IS THE OTHER HALF. Three reflection fields - the block-to-point map, the
// sampler unit per uniform LOCATION and the name-keyed storage-block override set - are
// members of LinkArtifacts that glUniformBlockBinding, glUniform1i and
// glShaderStorageBlockBinding move AFTER the link that produced the archive, without
// relinking. On the handle arm the record's set_program_bindings tails are the whole
// truth for all three (whole-set replacement, not a merge) and the archive's link-time
// values are shadowed; on the monolith arm there is no overlay because the "archive"
// IS the frontend's live table.
//
// THE ACCESSORS BELOW MIRROR ProgramObject's, one for one, and that duplication is
// deliberate rather than lazy: MG_Backend includes nothing from MG_State's program
// internals beyond the archive structs themselves, and ProgramObject's own forms are
// non-static members over Artifacts(). The three that ARE already static over
// LinkArtifacts (IsValidUniformLocation, UniformAtIn, GetUniformArraySizeByTIndex) are
// called rather than copied, which is where the boundary sits.
struct ProgramArchiveSource {
using LinkArtifacts = MG_State::GLState::LinkArtifacts;
using SpirvArtifacts = MG_State::GLState::SpirvArtifacts;
using UniformReflection = MG_State::GLState::ResourceReflection;
using TypeFacts = MG_State::GLState::TypeFacts;
using XfbVarying = MG_State::GLState::XfbVarying;
// Both are non-null for the whole lifetime of a source; a source is a local of the
// build that made it and never outlives the record or the object it points into.
const LinkArtifacts* Link = nullptr;
const SpirvArtifacts* Spirv = nullptr;
// WHAT A LOG LINE NAMES. The frontend's GL program name on the monolith arm; the
// ShaderCso slot on the handle arm, because the server does not know GL names and
// must not learn them (the handle is the identity the whole phase speaks in).
Uint Identity = 0;
// True when Identity is a handle slot rather than a GL name, so a message can say
// which it printed instead of leaving a reader to guess.
Bool IdentityIsHandleSlot = false;
Bool Linked = false;
Bool SpirvUsable = false;
Bool SpirvValidationEnabled = false;
Bool PointSizeWasDemoted = false;
Uint GlobalUboSize = 0;
// One entry per Spirv->generatedSpirv module, at the same index. Owned rather than
// referenced because the monolith arm builds it (GetLinkedShaderStages() returns by
// value) and the handle arm converts the frame's Uint32 words.
Vector<ShaderStage> LinkedStages;
// The post-link overlay. Governs iff OverlayGoverns; see the type comment.
Bool OverlayGoverns = false;
const Vector<Int32>* BlockBindingOverlay = nullptr;
const Vector<MG_Pipe::MGPProgramSamplerUnit>* SamplerUnitOverlay = nullptr;
// Built once per build from whichever side owns it, because TranspileSpirvToEssl
// takes the map by const reference and the record carries a vector.
UnorderedMap<String, Int> StorageOverrides;
// The client's commutative hash on the handle arm, ComputeShaderStorageBlockBinding-
// Signature's on the monolith one. Same function, computed on whichever side owns
// the map (MG_Impl/Pipe/ProgramEmit.h names its twin).
Uint64 StorageOverrideSignature = 0;
// ---- the archive's own answers ----
//
// EVERY NAME HERE IS ProgramObject's NAME, and that is load-bearing rather than
// tidy: the build body below is written once against `src`, whose TYPE is the
// build's (ProgramBuildSource), so the pull build compiles the very same text
// against a ProgramObject and G1's byte identity survives.
Uint GetExternalIndex() const { return Identity; }
Bool GetLinkStatus() const { return Linked; }
Bool GetSpirvStatus() const { return SpirvUsable; }
Bool GetSpirvValidationEnabled() const { return SpirvValidationEnabled; }
Bool PointSizeDemoted() const { return PointSizeWasDemoted; }
Uint GetUBOSize() const { return GlobalUboSize; }
const Vector<ShaderStage>& GetLinkedShaderStages() const { return LinkedStages; }
const UnorderedMap<String, Int>& GetShaderStorageBlockBindingOverrides() const {
return StorageOverrides;
}
// THE DEBUG LOG IS DELETED ON THIS ARM (CONTRACT-P5E §5.5). The snapshot is
// GL-thread-owned ShaderObject SharedPtrs - the exact shape rule C forbids an
// applier entry point to reach - and its one consumer in the build is an MGLOG_D
// dump of each stage's original GLSL, which the server does not have and does not
// need. An empty list makes that loop a no-op rather than a special case.
const Vector<MG_State::GLState::ProgramObject::LinkedShaderRef>& GetLinkedShaderSnapshot() const {
static const Vector<MG_State::GLState::ProgramObject::LinkedShaderRef> kNone;
return kNone;
}
Uint GetMaxUniformLocation() const { return Link->maxUniformLocation; }
Bool IsValidUniformLocation(Int location) const {
return MG_State::GLState::ProgramObject::IsValidUniformLocation(*Link, location);
}
const UniformReflection& UniformAt(Int tIndex) const {
return MG_State::GLState::ProgramObject::UniformAtIn(*Link, tIndex);
}
// Bounds-checked, exactly as ProgramObject::GetUniformName is not: the frontend's
// form indexes uniformIndexInTProgram raw because every caller there has already
// walked a legal location, and this one is reached from a handle arm where the
// location space comes off a record.
const String& GetUniformName(Uint location) const {
static const String kEmpty;
if (location >= Link->uniformIndexInTProgram.size()) return kEmpty;
return UniformAt(Link->uniformIndexInTProgram[location]).name;
}
GLenum GetUniformType(Uint location) const {
if (location >= Link->uniformIndexInTProgram.size()) return 0;
return UniformAt(Link->uniformIndexInTProgram[location]).glDefineType;
}
const TypeFacts& GetUniformTypeFacts(Uint location) const {
static const TypeFacts kEmpty{};
if (location >= Link->uniformIndexInTProgram.size()) return kEmpty;
return UniformAt(Link->uniformIndexInTProgram[location]).type;
}
Bool UniformLocationsAliasSameUniform(Int a, Int b) const {
if (!IsValidUniformLocation(a) || !IsValidUniformLocation(b)) return false;
return Link->uniformIndexInTProgram[a] == Link->uniformIndexInTProgram[b];
}
// ProgramObject::GetUniformLocation, reproduced over the archive. The array rules
// are the whole body: reflection keys an array under "arr[0]" at its base location,
// a bare "arr" resolves to that, an "arr[k]" resolves to base + k, and an array of
// arrays is keyed by its full "[0]"-terminated spelling - which is why the
// suffixed lookup is tried before the trailing subscript is read as an index.
Int GetUniformLocation(const String& name) const;
Int GetActiveUniformBlocksCount() const {
return static_cast<Int>(Link->glBlockIndexToTProgram.size());
}
const String& GetUniformBlockName(Uint index) const;
GLenum GetTransformFeedbackBufferMode() const { return Link->xfbBufferMode; }
SizeT GetTransformFeedbackVaryingCount() const { return Link->xfbVaryings.size(); }
const Vector<XfbVarying>& GetTransformFeedbackVaryings() const { return Link->xfbVaryings; }
const Vector<Vector<unsigned>>& GetGeneratedSpirv() const { return Spirv->generatedSpirv; }
// ---- the three the overlay governs ----
Uint GetUniformBlockBinding(Uint index) const;
Int GetUniformSamplerOrImageUnitIndex(Uint location) const;
// The monolith-glue constructor: the archive IS the frontend's live tables, so
// there is no overlay and the three mutable fields answer from them directly.
static ProgramArchiveSource FromFrontend(const MG_State::GLState::ProgramObject& program);
// The handle arm: the record's own archive, with its three tails overlaid.
static ProgramArchiveSource FromRecord(MG_Pipe::MGPipeHandle cso,
const MG_Pipe::MGPipeShaderCsoRecord& record);
};
// THE BUILD'S SOURCE TYPE, per build. This alias is what lets the program build have one
// body: in a push build it is the record-or-frontend view above, in a pull build it is
// the frontend object the body always read, spelled through the same name so the text
// does not move (G1).
using ProgramBuildSource = ProgramArchiveSource;
#else
using ProgramBuildSource = MG_State::GLState::ProgramObject;
#endif
class BackendProgramObjectImpl {
public:
// Per-link cache of a sampler-style uniform's backend location: built once in
@@ -2402,11 +2589,11 @@ namespace MobileGL::MG_Backend::DirectGLES {
~BackendProgramObjectImpl();
void SyncToBackend(const SharedPtr<MG_State::GLState::ProgramObject>& stateProgramObject);
#if MOBILEGL_PIPE_PUSH
// P5e SEAM (MG_Remote/CONTRACT-P5E.md §5.5; declared by c0e, bodied by pg): the same
// sync keyed on the ShaderCso HANDLE and answered from the record - the archive the
// create carries and the three binding tails set_program_bindings carries. It is an
// OVERLOAD beside the frontend one, which stays as the monolith-glue half, so the
// pull build's mangled names do not move.
// P5e (pg), CONTRACT-P5E.md §5.5: the same sync keyed on the ShaderCso HANDLE and
// answered from the record - the archive the create carries and the three binding
// tails set_program_bindings carries. It is an OVERLOAD beside the frontend one,
// which stays as the monolith-glue half, so the pull build's mangled names do not
// move (ruling 1 / ID-81: two overloads, not an #if inside one body).
void SyncToBackendByHandle(MG_Pipe::MGPipeHandle cso);
#endif
void Use();
@@ -2507,6 +2694,13 @@ namespace MobileGL::MG_Backend::DirectGLES {
// parameters - are all still specialised at the verb, from state this backend holds.
// 0 means "never stamped", which is a guaranteed miss (applier serials start at 1).
Uint64 GetSyncedShaderCsoSerial() const { return m_syncedShaderCsoSerial; }
// P5e (pg): the SECOND server-owned key, and it replaces GetImageUnitVersion() on
// the handle arm rather than duplicating the one above. A glUniform1i on an IMAGE
// uniform is BAKED into the generated ESSL (ES forbids the call outright), so the
// record that carries the new unit has to force a rebuild; BindingsSerial moves on
// every applied set_program_bindings, which is exactly when one can have changed.
// 0 means "never stamped", a guaranteed miss, because applier serials start at 1.
Uint64 GetSyncedBindingsSerial() const { return m_syncedBindingsSerial; }
#endif
// Whether the (unit, bound format) pairs this program's FORMAT-LESS image uniforms
// resolve to are still the ones its ESSL was generated against.
@@ -2532,7 +2726,12 @@ namespace MobileGL::MG_Backend::DirectGLES {
Uint64 ComputeImageUnitFormatSignature() const;
private:
void CacheResourceLocations(const SharedPtr<MG_State::GLState::ProgramObject>& stateProgramObject);
#if MOBILEGL_PIPE_PUSH
// The one body both public heads feed; see its definition for why it is a worker in
// a push build and IS SyncToBackend in a pull build.
void SyncToBackendFromSource(const ProgramBuildSource& src);
#endif
void CacheResourceLocations(const ProgramBuildSource& src);
// Builds, compiles and attaches the pass-through tessellation control stage GL 4.6
// core 11.2.2 describes, for a program that has an evaluation stage and none of its
@@ -2540,7 +2739,7 @@ namespace MobileGL::MG_Backend::DirectGLES {
// stage has been attached and before the link; see the definition for why it cannot
// regress a program that works today.
void AttachPassthroughTessControlStage(
const MG_State::GLState::ProgramObject& stateProgramObject, Int tessEvalShaderIndex,
const ProgramBuildSource& src, Int tessEvalShaderIndex,
const Vector<Vector<unsigned int>>& shaderSpirvs, const String& vertexStageEssl,
const String& tessEvalStageEssl);
@@ -2611,6 +2810,7 @@ namespace MobileGL::MG_Backend::DirectGLES {
// P4a's replacement for the two above on the handle arm; see GetSyncedShaderCsoSerial.
// Push-only, so the pull build's object is byte-for-byte the pre-P4a one (D-P).
Uint64 m_syncedShaderCsoSerial = 0;
Uint64 m_syncedBindingsSerial = 0;
#endif
// Image units addressed by the program's FORMAT-LESS image uniforms, and the digest
// of the (unit, format) pairs the generated ESSL baked. Empty/0 for every program
@@ -2669,15 +2869,20 @@ namespace MobileGL::MG_Backend::DirectGLES {
// program that was just built - best effort, on the same "only where the driver has
// the entry point" terms as ApplyShaderStorageBlockBinding above. Mirrors
// DirectVulkan's reseed-on-rebuild in BuildProgramResourceCache.
void ReseedShaderStorageBlockBindings(Uint backendProgramId,
const MG_State::GLState::ProgramObject& stateProgramObject);
void ReseedShaderStorageBlockBindings(Uint backendProgramId, const ProgramBuildSource& src);
// Order-independent digest of the program's glShaderStorageBlockBinding overrides.
// The generated ESSL carries them (ES has no way to move a storage block's binding
// after link), so a program built against a different set is stale and the draw path
// has to rebuild it. Computed from the values, so re-setting a block to the binding it
// already has costs nothing. 0 when nothing was ever rebound.
Uint64 ComputeShaderStorageBlockBindingSignature(
const MG_State::GLState::ProgramObject& stateProgramObject);
Uint64 ComputeShaderStorageBlockBindingSignature(const ProgramBuildSource& src);
#if MOBILEGL_PIPE_PUSH
// P5e (pg): the computation itself, for the monolith arm - the source constructor seeds
// itself with it and the monolith draw path asks it per draw. Push-only: in a pull build
// the overload above IS this body, so no name is added there.
Uint64 ComputeShaderStorageBlockBindingSignatureOf(
const MG_State::GLState::ProgramObject& program);
#endif
// Everything the image-format bake needs from one walk of a program's uniform
// reflection. GLSL ES requires a format layout qualifier on every image uniform;
@@ -2718,8 +2923,7 @@ namespace MobileGL::MG_Backend::DirectGLES {
// keeps the module it already had.
Bool declaresWidenableImageFormat = false;
};
ImageFormatBakeInputs CollectImageFormatBakeInputs(
const MG_State::GLState::ProgramObject& stateProgramObject);
ImageFormatBakeInputs CollectImageFormatBakeInputs(const ProgramBuildSource& src);
#if MOBILEGL_PIPE_PUSH
// ---- P5e SEAM (MG_Remote/CONTRACT-P5E.md §4.2, §5.5; declared by c0e, bodied by
@@ -2736,6 +2940,18 @@ namespace MobileGL::MG_Backend::DirectGLES {
// ResolveSamplerCsoTwin set - and null silently for the null handle, which is the legal
// "nothing bound".
BackendProgramObjectImpl* ResolveProgramTwin(MG_Pipe::MGPipeHandle cso);
// P5e (pg): the two PER-DRAW reads of set_program_bindings' tails, as free functions
// rather than through a ProgramArchiveSource - building a source per draw would copy the
// override map for a question that is one indexed read. Both answer exactly what their
// frontend twins do when the record carries no bindings yet: the archive's own link-time
// value, which for an untouched program IS the right answer.
//
// The block binding is dense in the BLOCK index space; the sampler unit is sparse and
// ascending by LOCATION, so it is a binary search (a program has thousands of locations
// and a handful of samplers, and a linear scan per sampler would be quadratic).
Uint ProgramBlockBindingFromRecord(const MG_Pipe::MGPipeShaderCsoRecord& record, Int blockIndex);
Int ProgramSamplerUnitFromRecord(const MG_Pipe::MGPipeShaderCsoRecord& record, Uint location);
#endif
} // namespace PrgramImpl