Compare commits

...
21 Commits
Author SHA1 Message Date
swung0x48 114bd70122 [Tools] (P14): FCL restore deletes the two properties instead of setting them empty. 2026-10-07 13:21:25 -04:00
swung0x48 0158a115de [Perf] (Magma): a wire draw that provably continues the open pass skips the attachment resolve and pass-key walk (rd12 monolith 63.2 -> 65.5, inproc 89.8 -> 92.6 fps). 2026-10-07 13:21:24 -04:00
swung0x48 8883e496bd [Perf] (MG_Pipe): the residual fill keeps unchanged object pins and a memoised vertex-buffer set keeps its hash (rd12 Espryt monolith +1%). 2026-10-07 13:21:23 -04:00
swung0x48 0cc54748f0 [Tools] (P14): layers.py can list the entries into one layer. 2026-10-07 12:32:18 -04:00
swung0x48 80dc9d45d9 [Perf] (MG_Pipe): a per-VAO memo keeps the emitters' handles and buffer set across draws, and an unchanged context-value record skips its hash (rd12 Espryt monolith +1%). 2026-10-07 12:32:05 -04:00
swung0x48 d07ec0255b [Tools] (P14): benchmark runs default to no per-frame glFinish, plus FCL and layer-split helpers. 2026-10-07 12:15:12 -04:00
swung0x48 4f4297f347 [Tools] (P14): opt-in fps log at swap (debug.mobilegl.fps_log) and a debug.mobilegl.transport override for app-hosted libraries. 2026-10-07 12:15:11 -04:00
swung0x48 5788a6023e [Perf] (Magma): a wire draw whose image descriptors cannot have moved rebinds its descriptor set with fresh buffer offsets (rd12 monolith 60.5 -> 63.3, inproc 83.7 -> 89.7 fps). 2026-10-07 12:15:10 -04:00
swung0x48 afcf8d96f9 [Perf] (Espryt): a VAO twin adopts an unchanged buffer set and element buffer instead of re-emitting them (rd12 monolith 75.2 -> 80.5 fps). 2026-10-07 12:15:10 -04:00
swung0x48 e6281b26de [Perf] (Magma): wire texture and buffer lookups get direct-mapped fronts and a continued pass stops re-marking its attachments (rd12 monolith 59.6 -> 60.5 fps). 2026-10-07 11:25:02 -04:00
swung0x48 eafe1179b3 [Tools] (P14): the profiler records the whole run and the analysis cuts the steady tail. 2026-10-07 10:50:39 -04:00
swung0x48 4ef9705ee0 [Perf] (Magma): wire draws memoise each unit's sampler and skip rebinding an unchanged pipeline, vertex or index binding (rd12 monolith 57.9 -> 60.3 fps). 2026-10-07 10:50:38 -04:00
swung0x48 b556ec3134 [Perf] (Espryt): a clean framebuffer attachment skips its per-draw resync behind the unit list's gate (rd12 monolith 74.5 -> 77.0 fps). 2026-10-07 10:50:37 -04:00
swung0x48 0b0089e599 [Tools] (P14): the sampler and profiler find spawn and tcp server processes by their install path, plus a per-component profile split. 2026-10-07 10:27:01 -04:00
swung0x48 8346d80999 [Perf] (MG_Pipe): lifetime-id slot lookups get a direct-mapped front and buffer binding slots are indexed (rd12 monolith +1.5%). 2026-10-07 10:27:00 -04:00
swung0x48 549a34bb1a [Perf] (MG_Remote): the apply loop asks the GPU-fault verdict every 256 records and at each present, not per record (Espryt rd12 inproc 55.5 -> 60.2 fps). 2026-10-07 10:26:59 -04:00
swung0x48 2b0857b016 [Tools] (P14): sampler sees server processes and GPU busy every tick, arms can name a second build, and a simpleperf profile helper. 2026-10-07 10:07:44 -04:00
swung0x48 a9108e1a0b [Perf] (Espryt): a VAO twin whose buffer set alone moved re-points enabled arrays without the 32-slot enable walk (rd12 monolith 70.3 -> 73.5 fps). 2026-10-07 10:07:43 -04:00
swung0x48 267211e8b4 [Perf] (Magma): wire draws stop rehashing program SPIR-V and stop allocating per draw (rd12 monolith 45.7 -> 56.7 fps). 2026-10-07 10:07:42 -04:00
swung0x48 51c1411696 [Docs] (P14): code-based perf estimate for every arm, written before the benchmark. 2026-10-07 09:18:22 -04:00
swung0x48 d56179d876 [Tools] (P14): pinned-clock perf matrix with a per-thread CPU and GPU-busy sampler. 2026-10-07 09:18:21 -04:00
39 changed files with 2029 additions and 73 deletions
+9
View File
@@ -458,6 +458,15 @@ namespace MobileGL::MG_ConfigLoader {
inline void InitTransport() {
String value;
QueryEnvVariable("MOBILEGL_TRANSPORT", value, "monolith");
#if defined(__ANDROID__)
// As debug.mobilegl.backend above: a developer picks the transport of an app-hosted
// library (a launcher's game process) without rebuilding it or its launcher.
char property[PROP_VALUE_MAX] = {};
if (__system_property_get("debug.mobilegl.transport", property) > 0) {
MGLOG_I("Config: debug.mobilegl.transport=%s overrides MOBILEGL_TRANSPORT=%s", property, value.c_str());
value = property;
}
#endif
String lowered = value;
std::transform(lowered.begin(), lowered.end(), lowered.begin(),
[](unsigned char c) { return static_cast<char>(std::tolower(c)); });
@@ -2325,6 +2325,14 @@ namespace MobileGL::MG_Backend::DirectGLES {
Vector<FboAttachmentSyncEntry>& list, Bool listValid) {
if (listValid) {
for (const auto& entry : list) {
// P14: the unit list's aggregate clean gate first (SyncNeccessaryTextures): an
// attachment whose storage, parameters and sampler are already the driver's
// costs two record reads instead of three nested syncs and their SharedPtr
// traffic. Same borrow rule as there - the list's key proves the twin alive.
const auto* record = PipeTextureRecordForHandle(entry.Res);
if (record != nullptr && entry.backend != nullptr &&
entry.backend->IsDrawSyncCleanByRecord(entry.Res, *record))
continue;
TextureImpl::SyncTextureToBackendByHandle(entry.Res, /*imageBindableStorageRequired=*/false);
}
return;
@@ -4682,6 +4690,7 @@ namespace MobileGL::MG_Backend::DirectGLES {
VertexArrayImpl::BackendVertexArrayObject* vaoTwin = nullptr;
Uint32 vaoConfigVersion = 0;
vaoTwin = VertexArrayImpl::ResolveVaoTwin(MG_Pipe::MGPipeApplier().BoundVertexElements);
if (vaoTwin != nullptr) vaoTwin->AdoptUnchangedBufferSets(MG_Pipe::MGPipeApplier());
// One program resolve and one texture-key capture serve the whole draw, for
// the same reason the twin resolve does: only frontend GL entry points move
// either, and none can run inside this preparation. GetProgramForDraw is a
+73 -10
View File
@@ -4962,10 +4962,10 @@ namespace MobileGL::MG_Backend::DirectGLES {
// half (an applier reset is not a slot recycle, so a re-created CSO at the same
// {slot, gen} restarts its ContentSerial at 1). If that rule is ever reverted, this
// gate is unsafe on any application that changes contexts.
const Bool attributesDirty = bufferIdsRemitted || !m_hasSyncedElements ||
!(m_syncedElementsHandle == st.BoundVertexElements) ||
m_syncedElementsSerial != rec->ContentSerial ||
m_syncedVertexBuffersSerial != st.VertexBuffersSerial;
const Bool elementsDirty = bufferIdsRemitted || !m_hasSyncedElements ||
!(m_syncedElementsHandle == st.BoundVertexElements) ||
m_syncedElementsSerial != rec->ContentSerial;
const Bool attributesDirty = elementsDirty || m_syncedVertexBuffersSerial != st.VertexBuffersSerial;
const Bool indexBufferDirty = bufferIdsRemitted || m_syncedIndexSerial != st.IndexBufferSerial;
// Emulation is server-owned: the client sends the draw's RAW base instance and never
// learns the answer. Applied here as well as in the applier so the decision is the
@@ -4977,6 +4977,17 @@ namespace MobileGL::MG_Backend::DirectGLES {
if (!emitAttributes && !indexBufferDirty) {
return;
}
// P14: ONLY THE BUFFER SET MOVED (a Minecraft chunk draw: same layout, next chunk's
// buffer). The driver VAO's enable flags are a function of the configuration alone,
// and the last full walk left them exactly as this configuration wants them, so the
// walk below re-points just the enabled attributes and skips the 32 enable/disable
// calls. Only after a walk in which every enabled attribute took the plain pointer
// path: the fp64, zero-stride, BGRA and no-buffer branches toggle enables or bind
// through other state, and the client-memory upload rewrites the VAO behind this
// gate, so any of them sends the next sync through the whole walk again.
const Bool buffersOnly = emitAttributes && !elementsDirty && m_lastWalkWasPlain &&
!m_hasConvertedFloat64Attribute;
Bool walkWasPlain = true;
m_hasConvertedFloat64Attribute = false;
Bind();
@@ -4996,6 +5007,7 @@ namespace MobileGL::MG_Backend::DirectGLES {
for (Uint attribIndex = 0; attribIndex < MG_Pipe::kMGPipeMaxVertexAttribs && emitAttributes;
++attribIndex) {
const MGPVertexAttribWire& attrib = rec->Attributes[attribIndex];
if (buffersOnly && !attrib.Enabled) continue;
const MG_Pipe::MGPVertexBuffer* binding =
VertexBufferForAttributeIndex(st, attribIndex);
const Uint32 divisor = binding != nullptr ? binding->Divisor : 0u;
@@ -5003,11 +5015,13 @@ namespace MobileGL::MG_Backend::DirectGLES {
// The enable/disable block. On this arm there is no per-attribute version to
// compare: the applier's Attributes[] IS what was last pushed, so a moved
// ContentSerial means re-emit and an unchanged one means the early-out above
// already returned.
if (attrib.Enabled) {
g_GLESFuncs.glEnableVertexAttribArray(attribIndex);
} else {
g_GLESFuncs.glDisableVertexAttribArray(attribIndex);
// already returned. A buffers-only walk leaves the flags as they are (above).
if (!buffersOnly) {
if (attrib.Enabled) {
g_GLESFuncs.glEnableVertexAttribArray(attribIndex);
} else {
g_GLESFuncs.glDisableVertexAttribArray(attribIndex);
}
}
// The fp64 narrowing, verbatim in behaviour including the Adreno workaround:
@@ -5017,6 +5031,7 @@ namespace MobileGL::MG_Backend::DirectGLES {
// carried separately from Type == Float64 on the wire precisely so this test
// can still tell the two apart.
if (attrib.IsLong || attrib.Type == static_cast<Uint32>(DataType::Float64)) {
walkWasPlain = false;
if (attrib.Enabled && attrib.Type == static_cast<Uint32>(DataType::Float64) &&
binding != nullptr &&
SyncFloat64AttributeAsFloat32ByHandle(attribIndex, attrib, *binding, fetchBaseInstance)) {
@@ -5036,12 +5051,17 @@ namespace MobileGL::MG_Backend::DirectGLES {
continue;
}
if (!attrib.Enabled || binding == nullptr) continue;
if (!attrib.Enabled) continue;
if (binding == nullptr) {
walkWasPlain = false;
continue;
}
// A resolved stride of zero is the binding model's "never advance" and
// glVertexAttribPointer cannot say it - its zero means "tightly packed", i.e.
// the opposite. ES 3.1's binding-point API can.
if (attrib.Stride == 0 && HasVertexBindingApi()) {
walkWasPlain = false;
if (!SyncZeroStrideAttributeByHandle(attribIndex, attrib, *binding)) {
continue;
}
@@ -5051,6 +5071,7 @@ namespace MobileGL::MG_Backend::DirectGLES {
}
if (!BindAttributeBufferByHandle(binding->Res)) {
walkWasPlain = false;
continue;
}
@@ -5061,6 +5082,7 @@ namespace MobileGL::MG_Backend::DirectGLES {
// format: the per-draw sync must not grow a glGetError round trip for the
// formats real applications use.
const Bool formatMayBeRefused = attrib.IsBgra != 0;
if (formatMayBeRefused) walkWasPlain = false;
if (formatMayBeRefused) {
while (g_GLESFuncs.glGetError() != GL_NO_ERROR) {
} // start from a clean slate so the check below is about THIS call
@@ -5116,6 +5138,8 @@ namespace MobileGL::MG_Backend::DirectGLES {
indexBufferSynced = true;
}
if (indexBufferSynced) {
m_syncedIndexRes = st.IndexBuffer.Res;
m_syncedIndexValid = true;
// The two element-array restore scopes (the restart substitution's and
// MultiDrawImpl's) put the DRIVER id back without touching this serial,
// which is correct: the serial records what the APPLIER last said, and
@@ -5129,19 +5153,58 @@ namespace MobileGL::MG_Backend::DirectGLES {
m_syncedElementsSerial = rec->ContentSerial;
m_syncedVertexBuffersSerial = st.VertexBuffersSerial;
m_hasSyncedElements = true;
// The window this walk emitted, for AdoptUnchangedBufferSets.
m_syncedBufferStart = st.VertexBufferStart;
m_syncedBufferCount = std::min<Uint32>(st.VertexBufferCount,
MG_Pipe::kMGPipeMaxVertexAttribs - st.VertexBufferStart);
m_syncedBufferBaseInstance = st.VertexFetchBaseInstance;
for (Uint32 i = 0; i < m_syncedBufferCount; ++i)
m_syncedBuffers[i] = st.VertexBuffers[m_syncedBufferStart + i];
m_syncedBuffersValid = st.VertexBufferStart <= MG_Pipe::kMGPipeMaxVertexAttribs;
m_syncedContextSerial = st.ContextSerial;
}
if (emitAttributes) {
m_syncedFetchBaseInstance = fetchBaseInstance;
m_lastWalkWasPlain = walkWasPlain;
}
m_syncedBufferIdGeneration = currentBufferIdGeneration;
}
void BackendVertexArrayObject::AdoptUnchangedBufferSets(const MG_Pipe::MGPipeApplierState& st) {
if (!m_hasSyncedElements || m_syncedContextSerial != st.ContextSerial) return;
if (m_syncedBuffersValid && m_syncedVertexBuffersSerial != st.VertexBuffersSerial &&
st.VertexBufferStart == m_syncedBufferStart && st.VertexFetchBaseInstance == m_syncedBufferBaseInstance &&
std::min<Uint32>(st.VertexBufferCount, MG_Pipe::kMGPipeMaxVertexAttribs - st.VertexBufferStart) ==
m_syncedBufferCount) {
Bool same = true;
for (Uint32 i = 0; same && i < m_syncedBufferCount; ++i) {
const auto& a = m_syncedBuffers[i];
const auto& b = st.VertexBuffers[m_syncedBufferStart + i];
same = a.Res == b.Res && a.Offset == b.Offset && a.Stride == b.Stride &&
a.Divisor == b.Divisor && a.BindingIndex == b.BindingIndex;
}
if (same) {
if (m_resolvedDrawBuffers.valid && m_resolvedDrawBuffers.buffersSerial == m_syncedVertexBuffersSerial)
m_resolvedDrawBuffers.buffersSerial = st.VertexBuffersSerial;
m_syncedVertexBuffersSerial = st.VertexBuffersSerial;
}
}
if (m_syncedIndexValid && m_syncedIndexSerial != st.IndexBufferSerial && m_syncedIndexRes == st.IndexBuffer.Res) {
if (m_resolvedDrawBuffers.iboSerial == m_syncedIndexSerial) m_resolvedDrawBuffers.iboSerial = st.IndexBufferSerial;
m_syncedIndexSerial = st.IndexBufferSerial;
}
}
void BackendVertexArrayObject::SyncClientSideAttributesForDrawArrays(
const SharedPtr<MG_State::GLState::VertexArrayObject>& stateVAOObject, GLint first, GLsizei count,
Uint32 fetchBaseInstance) {
if (!stateVAOObject || count <= 0 || first < 0) {
return;
}
// This rewrites the driver VAO's arrays behind SyncToBackendFromApplier's gate, so
// the next sync must not take its buffers-only walk, nor adopt an equal buffer set.
m_lastWalkWasPlain = false;
m_syncedBuffersValid = false;
Bind();
+19
View File
@@ -1181,6 +1181,13 @@ namespace MobileGL::MG_Backend::DirectGLES {
// overload, not defaulted to null") is what this obeys. The body is unchanged and
// was already record-only - it is this family's existence proof (scout S1 §2).
void SyncToBackendFromApplier();
// P14: the applier's buffer-set serials move whenever ANY VAO's set is published, so
// switching between two VAOs moves them although neither twin's buffers changed. Called
// before the draw's syncs: when the window this twin last emitted is the applier's
// window value for value, take the new serials as already synced (and carry the
// resolved-buffers memo along) instead of re-emitting a driver VAO that already holds
// exactly that. Only within the applier context the twin last synced against.
void AdoptUnchangedBufferSets(const MG_Pipe::MGPipeApplierState& st);
void SyncClientSideAttributesForDrawArrays(
const SharedPtr<MG_State::GLState::VertexArrayObject>& stateVAOObject, GLint first, GLsizei count,
Uint32 fetchBaseInstance = 0);
@@ -1392,6 +1399,18 @@ namespace MobileGL::MG_Backend::DirectGLES {
// baked into the driver VAO's attribute/element bindings may be dead even
// though every frontend version matches, so the next sync re-emits them all.
Uint64 m_syncedBufferIdGeneration = 0;
// P14: the last attribute walk sent every enabled attribute down the plain pointer
// path, so the driver VAO's enable flags are exactly the configuration's and a walk
// for a moved buffer set alone may skip them (SyncToBackendFromApplier).
Bool m_lastWalkWasPlain = false;
// P14: the vertex-buffer window and element buffer as last emitted into the driver
// VAO, for AdoptUnchangedBufferSets. Handles are {slot, gen}, never reused.
Array<MG_Pipe::MGPVertexBuffer, MG_Pipe::kMGPipeMaxVertexAttribs> m_syncedBuffers{};
Uint32 m_syncedBufferStart = 0, m_syncedBufferCount = 0, m_syncedBufferBaseInstance = 0;
Bool m_syncedBuffersValid = false;
MG_Pipe::MGPipeHandle m_syncedIndexRes = MG_Pipe::kMGPipeNullHandle;
Bool m_syncedIndexValid = false;
Uint64 m_syncedContextSerial = 0;
};
extern TwinRegistry<MG_State::GLState::VertexArrayObject, BackendVertexArrayObject, MG_Pipe::MGPipeKind::VertexElementsCso>
@@ -47,15 +47,27 @@ namespace MobileGL::MG_Backend::DirectVulkan {
const auto& stages = m_record->Archive->LinkedStages;
return std::find(stages.begin(), stages.end(), static_cast<Uint32>(stage)) != stages.end();
}
// The view has no lifetime of its own, so the memo lives in the server record it views
// (MGPipeShaderCsoRecord::BackendHashMemo), never in the client's program object: an entry
// answers only while the record's Serial and BindingsSerial are the ones it was taken at.
Bool GetBackendHashMemo(Uint flags, Uint64& hash) const {
// A view has no lifetime in which a memo can safely persist. The server
// factory's content cache still owns compiled programs; hash from current
// archive + binding tails rather than borrowing the client's mutable memo.
(void)flags;
(void)hash;
for (const auto& entry : m_record->BackendHashMemo) {
if (entry.Valid && entry.Key == flags && entry.Serial == m_record->Serial &&
entry.BindingsSerial == m_record->BindingsSerial) {
hash = entry.Hash;
return true;
}
}
return false;
}
void SetBackendHashMemo(Uint flags, Uint64 hash) const {
auto& memo = m_record->BackendHashMemo;
auto& entry = memo[m_record->BackendHashMemoNext++ % memo.size()];
entry.Serial = m_record->Serial;
entry.BindingsSerial = m_record->BindingsSerial;
entry.Hash = hash;
entry.Key = flags;
entry.Valid = true;
}
Uint64 GetBackendStateVersion() const { return m_record->Serial; }
Uint64 GetBlockBindingVersion() const { return m_record->BindingsSerial; }
@@ -153,8 +153,9 @@ namespace MobileGL::MG_Backend::DirectVulkan {
Bool UniformManager::ResolveWireImageDescriptor(
VkCommandBuffer commandBuffer, const MagmaProgramSource& program,
const ProgramFactory::VkProgramObject& programObj, Uint32 binding, Uint32 element,
Bool storage, VkDescriptorImageInfo& out) const {
Bool storage, VkDescriptorImageInfo& out, VkTextureManager::TextureResource** outResource) const {
out = {};
if (outResource != nullptr) *outResource = nullptr;
auto& state = MG_Pipe::MGPipeApplier();
const Int baseLocation = programObj.samplerUniformLocationByBinding[binding];
const Int location = baseLocation + static_cast<Int>(element);
@@ -183,6 +184,7 @@ namespace MobileGL::MG_Backend::DirectVulkan {
auto* resource = m_textureManager->SyncTextureResourceByHandle(handle, false, storage);
if (!resource) WireDescriptorFatal("image-resource");
m_textureManager->FlushPendingUploads();
if (outResource != nullptr) *outResource = resource;
const auto target = static_cast<MG_Pipe::MGPipeResourceTarget>(record.Desc.Target);
const Bool layerable = WireTargetHasLayers(target);
@@ -312,10 +314,38 @@ namespace MobileGL::MG_Backend::DirectVulkan {
MGLOG_E_ONCE("Magma wire: sampler CSO {%u, %u} named by texture unit %u is missing or no longer "
"live; sampling with default parameters", samplerHandle.Slot, samplerHandle.Gen, unit);
}
out.sampler = m_samplerManager->GetOrCreateSamplerFromParameters(stale ? kDefaultSamplerParams : sampler->Params,
static_cast<TextureInternalFormat>(record.Desc.InternalFormat),
domain == SamplerNumericDomain::SignedInteger || domain == SamplerNumericDomain::UnsignedInteger,
levels);
const Bool forceNearest =
domain == SamplerNumericDomain::SignedInteger || domain == SamplerNumericDomain::UnsignedInteger;
static_assert(kWireSamplerMemoUnits == MG_Pipe::kMGPipeMaxTextureUnits,
"the wire sampler memo has one entry per texture unit");
auto& memo = m_wireSamplerMemo[static_cast<SizeT>(unit)];
const Bool memoHit = !stale && memo.valid && memo.slot == samplerHandle.Slot &&
memo.gen == samplerHandle.Gen && memo.serial == sampler->Serial &&
memo.contextSerial == state.ContextSerial &&
memo.format == record.Desc.InternalFormat && memo.viewLevelCount == levels &&
memo.forceNearest == forceNearest &&
memo.frameBoundary == m_samplerManager->FrameBoundaryCount() &&
memo.destroyEpoch == m_samplerManager->DestroyEpoch();
if (memoHit) {
out.sampler = memo.sampler;
} else {
out.sampler = m_samplerManager->GetOrCreateSamplerFromParameters(
stale ? kDefaultSamplerParams : sampler->Params,
static_cast<TextureInternalFormat>(record.Desc.InternalFormat), forceNearest, levels);
memo.valid = !stale && out.sampler != VK_NULL_HANDLE;
if (memo.valid) {
memo.slot = samplerHandle.Slot;
memo.gen = samplerHandle.Gen;
memo.serial = sampler->Serial;
memo.contextSerial = state.ContextSerial;
memo.format = record.Desc.InternalFormat;
memo.viewLevelCount = levels;
memo.forceNearest = forceNearest;
memo.frameBoundary = m_samplerManager->FrameBoundaryCount();
memo.destroyEpoch = m_samplerManager->DestroyEpoch();
memo.sampler = out.sampler;
}
}
if (!out.sampler) return false;
}
return true;
@@ -602,6 +632,7 @@ namespace MobileGL::MG_Backend::DirectVulkan {
for (auto& entry : m_descriptorReuseMemo) {
entry.valid = false;
}
m_wireDescriptorRebind.valid = false;
m_lastBindValid = false;
}
@@ -688,6 +719,7 @@ namespace MobileGL::MG_Backend::DirectVulkan {
for (auto& entry : m_descriptorReuseMemo) {
entry.valid = false;
}
m_wireDescriptorRebind.valid = false;
MGLOG_D("UniformDescriptorBinder: freed %zu descriptor sets for destroyed layout", purgedSets);
}
}
@@ -1522,6 +1554,11 @@ namespace MobileGL::MG_Backend::DirectVulkan {
if (frame.activeDescriptorPoolIndex >= frame.descriptorPools.size()) {
frame.activeDescriptorPoolIndex = 0;
}
if (TryRebindWireDescriptorSet(commandBuffer, program, programObj, frameIndex, bindPoint)) return true;
// What the full resolve below proves for the next draw's rebind (WireDescriptorRebind).
Bool rebindEligible = true;
auto& rebindResources = m_wireRebindResourcesScratch;
rebindResources.clear();
// The descriptor set is chosen AFTER the writes are built (below), so a draw
// whose resolved descriptor content matches the previous draw can reuse that
@@ -1581,6 +1618,10 @@ namespace MobileGL::MG_Backend::DirectVulkan {
break; // ascending, so nothing past the cap can follow
}
const auto kind = programObj.bindingKinds[binding];
if (kind != ProgramFactory::DescriptorBindingKind::UniformBufferDynamic &&
kind != ProgramFactory::DescriptorBindingKind::StorageBuffer &&
kind != ProgramFactory::DescriptorBindingKind::CombinedImageSampler)
rebindEligible = false;
VkWriteDescriptorSet write{};
write.sType = VK_STRUCTURE_TYPE_WRITE_DESCRIPTOR_SET;
@@ -1701,8 +1742,12 @@ namespace MobileGL::MG_Backend::DirectVulkan {
const SizeT firstImageInfoIndex = imageInfos.size();
for (Uint32 element = 0; element < descriptorCount; ++element) {
VkDescriptorImageInfo imageInfo{};
VkTextureManager::TextureResource* sampledResource = nullptr;
const Bool hasImage = ResolveWireImageDescriptor(commandBuffer, program, programObj, binding,
element, false, imageInfo);
element, false, imageInfo, &sampledResource);
// A placeholder (an empty unit) has no resource to re-check: full resolve.
if (sampledResource == nullptr) rebindEligible = false;
else rebindResources.push_back(sampledResource);
if (!hasImage) {
MGLOG_E_ONCE(
"UniformDescriptorBinder::BindProgramUniformBuffers failed: sampler binding %u element %u "
@@ -1800,6 +1845,101 @@ namespace MobileGL::MG_Backend::DirectVulkan {
BindDescriptorSetDeduped(commandBuffer, bindPoint, programObj.pipelineLayout, descriptorSet,
dynamicOffsets);
// Stamp what this full resolve depended on, for TryRebindWireDescriptorSet. Taken after
// the walk: the walk's own syncs and barriers are part of what it proved.
auto& rebind = m_wireDescriptorRebind;
rebind.valid = rebindEligible && descriptorSet != VK_NULL_HANDLE &&
WireImageWriteEpoch() == m_wireImageBarrierEpoch;
if (rebind.valid) {
const auto& st = MG_Pipe::MGPipeApplier();
rebind.frameIndex = frameIndex;
rebind.bindPoint = bindPoint;
rebind.programHash = programObj.hash;
rebind.layout = programObj.descriptorSetLayout;
rebind.pipelineLayout = programObj.pipelineLayout;
rebind.programHandle = program.Handle();
rebind.programSerial = program.GetBackendStateVersion();
rebind.programBindingsSerial = program.GetBlockBindingVersion();
rebind.contextSerial = st.ContextSerial;
rebind.samplerViewsSerial = st.SamplerViewsSerial;
rebind.samplerStatesSerial = st.SamplerStatesSerial;
rebind.shaderImagesSerial = st.ShaderImagesSerial;
rebind.shaderBuffersSerial = st.ShaderBuffersSerial;
rebind.textureShutterSerial = st.TextureShutterSerial;
rebind.storeDestroyEpoch = m_bufferManager->GetWireStoreDestroyEpoch();
rebind.textureImageEpoch = m_textureManager->GetTextureImageEpoch();
rebind.resourceEraseEpoch = m_textureManager->GetResourceEraseEpoch();
rebind.samplerDestroyEpoch = m_samplerManager->DestroyEpoch();
rebind.samplerFrameBoundary = m_samplerManager->FrameBoundaryCount();
rebind.set = descriptorSet;
rebind.sampledResources.assign(rebindResources.begin(), rebindResources.end());
rebind.bufferInfos.assign(bufferInfos.begin(), bufferInfos.end());
}
return true;
}
Bool UniformManager::TryRebindWireDescriptorSet(VkCommandBuffer commandBuffer, const MagmaProgramSource& program,
const ProgramFactory::VkProgramObject& programObj,
Uint32 frameIndex, VkPipelineBindPoint bindPoint) {
const auto& rebind = m_wireDescriptorRebind;
if (!rebind.valid || rebind.frameIndex != frameIndex || rebind.bindPoint != bindPoint ||
rebind.programHash != programObj.hash || rebind.layout != programObj.descriptorSetLayout ||
rebind.pipelineLayout != programObj.pipelineLayout || !(rebind.programHandle == program.Handle()) ||
rebind.programSerial != program.GetBackendStateVersion() ||
rebind.programBindingsSerial != program.GetBlockBindingVersion())
return false;
const auto& st = MG_Pipe::MGPipeApplier();
if (rebind.contextSerial != st.ContextSerial || rebind.samplerViewsSerial != st.SamplerViewsSerial ||
rebind.samplerStatesSerial != st.SamplerStatesSerial || rebind.shaderImagesSerial != st.ShaderImagesSerial ||
rebind.shaderBuffersSerial != st.ShaderBuffersSerial || rebind.textureShutterSerial != st.TextureShutterSerial)
return false;
if (rebind.storeDestroyEpoch != m_bufferManager->GetWireStoreDestroyEpoch() ||
rebind.textureImageEpoch != m_textureManager->GetTextureImageEpoch() ||
rebind.resourceEraseEpoch != m_textureManager->GetResourceEraseEpoch() ||
rebind.samplerDestroyEpoch != m_samplerManager->DestroyEpoch() ||
rebind.samplerFrameBoundary != m_samplerManager->FrameBoundaryCount() ||
WireImageWriteEpoch() != m_wireImageBarrierEpoch)
return false;
// The resource pointers are good while the erase epoch holds (checked above). A sampled
// image that left GENERAL (rendered to, copied into) needs the full resolve's transition.
for (const auto* resource : rebind.sampledResources)
if (resource->layout != VK_IMAGE_LAYOUT_GENERAL) return false;
// The buffer descriptors, in the walk's order, re-resolved: a UBO's dynamic offset is this
// draw's; its buffer and range, and every storage buffer's window, must be the set's.
auto& infos = m_wireRebindBufferScratch;
infos.clear();
auto& dynamicOffsets = m_dynamicOffsetsScratch;
dynamicOffsets.clear();
for (const Uint32 binding : programObj.activeBindings) {
if (binding >= m_maxBindings) break;
const auto kind = programObj.bindingKinds[binding];
const Uint32 count = BindingDescriptorCount(programObj, binding);
if (kind == ProgramFactory::DescriptorBindingKind::UniformBufferDynamic) {
for (Uint32 element = 0; element < count; ++element) {
VkDescriptorBufferInfo info{};
Uint32 offset = 0;
if (!ResolveDynamicUboDescriptor(program, programObj, binding, element, frameIndex, info.buffer,
info.range, offset))
return false;
infos.push_back(info);
dynamicOffsets.push_back(offset);
}
} else if (kind == ProgramFactory::DescriptorBindingKind::StorageBuffer) {
for (Uint32 element = 0; element < count; ++element) {
VkDescriptorBufferInfo info{};
if (!ResolveStorageBufferDescriptor(program, programObj, binding, element, info)) return false;
infos.push_back(info);
}
}
}
if (infos.size() != rebind.bufferInfos.size()) return false;
for (SizeT i = 0; i < infos.size(); ++i) {
const auto& a = infos[i];
const auto& b = rebind.bufferInfos[i];
if (a.buffer != b.buffer || a.offset != b.offset || a.range != b.range) return false;
}
BindDescriptorSetDeduped(commandBuffer, bindPoint, programObj.pipelineLayout, rebind.set, dynamicOffsets);
return true;
}
} // namespace MobileGL::MG_Backend::DirectVulkan
@@ -260,7 +260,41 @@ namespace MobileGL::MG_Backend::DirectVulkan {
Bool storage);
Bool ResolveWireImageDescriptor(VkCommandBuffer commandBuffer, const MagmaProgramSource& program,
const ProgramFactory::VkProgramObject& programObj, Uint32 binding,
Uint32 element, Bool storage, VkDescriptorImageInfo& out) const;
Uint32 element, Bool storage, VkDescriptorImageInfo& out,
VkTextureManager::TextureResource** outResource = nullptr) const;
// P14: THE WIRE DESCRIPTOR REBIND. A draw whose image descriptors cannot have changed since
// the last full resolve of the same program in the same frame - same applier context, same
// unit / sampler / image / buffer-binding serials, same texture shutter, no texture image
// re-created or erased, no sampler destroyed, every sampled image still in GENERAL and no
// image write awaiting its barrier - re-resolves only its buffer descriptors (the UBO
// dynamic offsets move every draw) and, when those name the same buffers and ranges,
// rebinds the set it already has. Anything else, or any program with a texel buffer, storage
// image or placeholder descriptor, takes the full resolve.
struct WireDescriptorRebind {
Bool valid = false;
Uint32 frameIndex = 0;
VkPipelineBindPoint bindPoint = VK_PIPELINE_BIND_POINT_GRAPHICS;
Uint64 programHash = 0;
VkDescriptorSetLayout layout = VK_NULL_HANDLE;
VkPipelineLayout pipelineLayout = VK_NULL_HANDLE;
MG_Pipe::MGPipeHandle programHandle = MG_Pipe::kMGPipeNullHandle;
Uint64 programSerial = 0, programBindingsSerial = 0;
Uint64 contextSerial = 0, samplerViewsSerial = 0, samplerStatesSerial = 0, shaderImagesSerial = 0;
Uint64 shaderBuffersSerial = 0, textureShutterSerial = 0;
Uint64 storeDestroyEpoch = 0, textureImageEpoch = 0, resourceEraseEpoch = 0;
Uint64 samplerDestroyEpoch = 0, samplerFrameBoundary = 0;
VkDescriptorSet set = VK_NULL_HANDLE;
Vector<VkTextureManager::TextureResource*> sampledResources;
// The buffer descriptors in walk order: dynamic UBOs with offset 0 (their offset is the
// dynamic one), storage buffers whole.
Vector<VkDescriptorBufferInfo> bufferInfos;
};
WireDescriptorRebind m_wireDescriptorRebind;
Vector<VkDescriptorBufferInfo> m_wireRebindBufferScratch;
Vector<VkTextureManager::TextureResource*> m_wireRebindResourcesScratch;
Bool TryRebindWireDescriptorSet(VkCommandBuffer commandBuffer, const MagmaProgramSource& program,
const ProgramFactory::VkProgramObject& programObj, Uint32 frameIndex,
VkPipelineBindPoint bindPoint);
Bool ResolveWireTexelBufferDescriptor(const MagmaProgramSource& program,
const ProgramFactory::VkProgramObject& programObj, Uint32 binding,
Uint32 frameIndex, Bool storage, VkBufferView& out);
@@ -500,5 +534,23 @@ namespace MobileGL::MG_Backend::DirectVulkan {
}
// WireImageWriteEpoch() when ResolveWireImageDescriptor last made image writes visible.
mutable Uint64 m_wireImageBarrierEpoch = 0;
// P14: the wire arm's sampler memo, one entry per texture unit. The sampler a unit resolves
// to is a function of the sampler CSO it names (immutable for a {slot, gen, Serial} within
// one applier - ContextSerial tells appliers apart), the texture's format, the numeric
// domain and the view's level count. A hit skips the parameter hash and the cache map; it
// is taken only while the sampler manager has destroyed nothing (DestroyEpoch) and only
// within the frame boundary of the last real lookup, so every used entry is still stamped
// once per frame and the idle sweep can never retire a sampler this memo hands out.
struct WireSamplerMemo {
Bool valid = false;
Uint32 slot = 0, gen = 0;
Uint64 serial = 0, contextSerial = 0;
Uint32 format = 0, viewLevelCount = 0;
Bool forceNearest = false;
Uint64 frameBoundary = 0, destroyEpoch = 0;
VkSampler sampler = VK_NULL_HANDLE;
};
static constexpr SizeT kWireSamplerMemoUnits = 192; // MG_Pipe::kMGPipeMaxTextureUnits
mutable Array<WireSamplerMemo, kWireSamplerMemoUnits> m_wireSamplerMemo{};
};
} // namespace MobileGL::MG_Backend::DirectVulkan
@@ -20,7 +20,8 @@
namespace MobileGL::MG_Backend::DirectVulkan {
Bool VertexInputStateFactory::BuildWireVertexInput(const MG_Pipe::MGPipeVertexElementsRecord& elements,
const MG_Pipe::MGPipeApplierState& state, Uint32 activeMask, BackendVertexInputState& out) const {
out = BackendVertexInputState{};
// The caller's state is a per-draw scratch (WireDraw.inc): reset it without freeing.
out.ResetKeepingCapacity();
for (Uint32 location = 0; location < elements.AttributeCount; ++location) {
const auto& attr = elements.Attributes[location];
if (!attr.Enabled || !(activeMask & (1u << location))) continue;
@@ -74,6 +74,34 @@ namespace MobileGL::MG_Backend::DirectVulkan {
VkPipelineVertexInputStateCreateInfo state{
VK_STRUCTURE_TYPE_PIPELINE_VERTEX_INPUT_STATE_CREATE_INFO
};
// Back to a default-constructed state while keeping the vectors' storage, so a
// per-draw scratch rebuilds without touching the heap. Every member is reset by the
// default assignment; only the vectors' buffers are carried across it.
void ResetKeepingCapacity() {
const auto keep = [](auto& vector) {
auto storage = Move(vector);
storage.clear();
return storage;
};
auto keptBindings = keep(bindings);
auto keptAttributes = keep(attributes);
auto keptBufferKeys = keep(bindingBufferKeys);
auto keptBaseOffsets = keep(bindingBaseOffsets);
auto keptLocations = keep(bindingAttributeLocations);
auto keptClientMemory = keep(bindingUsesClientMemory);
auto keptConversions = keep(bindingConversions);
auto keptDivisors = keep(bindingDivisors);
*this = BackendVertexInputState{};
bindings = Move(keptBindings);
attributes = Move(keptAttributes);
bindingBufferKeys = Move(keptBufferKeys);
bindingBaseOffsets = Move(keptBaseOffsets);
bindingAttributeLocations = Move(keptLocations);
bindingUsesClientMemory = Move(keptClientMemory);
bindingConversions = Move(keptConversions);
bindingDivisors = Move(keptDivisors);
}
};
VertexInputStateFactory(const VulkanRendererConfig& config, VkPhysicalDevice physicalDevice):
@@ -184,8 +184,14 @@ namespace MobileGL::MG_Backend::DirectVulkan {
} // namespace
VkBufferManager::WireBufferResource* VkBufferManager::FindWireBuffer(MG_Pipe::MGPipeHandle res) {
const auto found = m_wireBuffers.find(WireBufferKey(res));
return found == m_wireBuffers.end() ? nullptr : &found->second;
const Uint64 key = WireBufferKey(res);
auto& lookup = m_wireBufferLookup[key % kWireBufferLookupEntries];
if (lookup.resource != nullptr && lookup.key == key && lookup.eraseCount == m_wireBufferEraseCount)
return lookup.resource;
const auto found = m_wireBuffers.find(key);
if (found == m_wireBuffers.end()) return nullptr;
lookup = {key, m_wireBufferEraseCount, &found->second};
return &found->second;
}
void VkBufferManager::CreateWireBuffer(MG_Pipe::MGPipeHandle res, const MG_Pipe::MGPResourceDesc& desc) {
@@ -992,6 +998,7 @@ namespace MobileGL::MG_Backend::DirectVulkan {
DeferWireRelease(std::move(found->second.buffer),
found->second.imported ? T0ReleaseSerial(found->second.lastUseSerial)
: found->second.lastUseSerial); // P11 B2, as Respecify
++m_wireBufferEraseCount;
m_wireBuffers.erase(found);
PublishWireReclaimGauges();
}
@@ -1163,6 +1170,7 @@ namespace MobileGL::MG_Backend::DirectVulkan {
// DestroyAllDeferredReleases above emptied the parked list; this destroys the stores the
// records still hold, so nothing this arm minted outlives the count.
if (!m_wireBuffers.empty()) ++m_wireStoreDestroyEpoch;
++m_wireBufferEraseCount;
m_wireBuffers.clear();
m_wireStoreCount = 0;
m_wireStoreCountPeak = 0;
@@ -292,6 +292,18 @@ namespace MobileGL::MG_Backend::DirectVulkan {
IBufferCopyCommandProvider* m_copyProvider = nullptr;
Vector<Vector<VkBufferObject>> m_deferredBufferReleases;
std::unordered_map<Uint64, WireBufferResource> m_wireBuffers;
// P14: a direct-mapped front for m_wireBuffers (every draw resolves each vertex, index and
// uniform buffer). Keyed on WireBufferKey ({slot, gen}, never reused) and believed only at
// the erase count it was taken at: a node never moves until erased, and every erase moves
// the count.
struct WireBufferLookup {
Uint64 key = 0;
Uint64 eraseCount = 0;
WireBufferResource* resource = nullptr;
};
static constexpr SizeT kWireBufferLookupEntries = 256;
Array<WireBufferLookup, kWireBufferLookupEntries> m_wireBufferLookup{};
Uint64 m_wireBufferEraseCount = 0;
// See DeferredWireRelease. ONE FLAT LIST rather than the per-frame-slot buckets above:
// the whole point is that these entries do not wait for a frame slot to come round.
Vector<DeferredWireRelease> m_deferredWireReleases;
@@ -274,6 +274,7 @@ namespace MobileGL::MG_Backend::DirectVulkan {
}
void VkSamplerManager::Shutdown() {
++m_destroyEpoch;
for (auto& [_, sampler] : m_samplers) {
if (m_device != VK_NULL_HANDLE && sampler.handle != VK_NULL_HANDLE) {
vkDestroySampler(m_device, sampler.handle, nullptr);
@@ -309,6 +310,7 @@ namespace MobileGL::MG_Backend::DirectVulkan {
if (m_device != VK_NULL_HANDLE && entry.handle != VK_NULL_HANDLE) {
vkDestroySampler(m_device, entry.handle, nullptr);
}
++m_destroyEpoch;
if (entry.usesCustomBorderColor && m_customBorderColorSamplerCount > 0) {
--m_customBorderColorSamplerCount;
}
@@ -56,6 +56,12 @@ public:
// fence wait. Self-gated: one counter bump and compare except on sweep
// boundaries.
void OnFrameBoundary();
// For a caller that memoises a returned VkSampler (UniformManager's wire sampler memo): the
// frame-boundary count a GetOrCreate call stamps, and a counter that moves whenever any cached
// sampler is destroyed. A memo may reuse a handle only while the destroy epoch is unchanged,
// and must still call GetOrCreate once per frame boundary so the entry stays stamped as used.
Uint64 FrameBoundaryCount() const { return m_frameBoundaryCounter; }
Uint64 DestroyEpoch() const { return m_destroyEpoch; }
// What GL_TEXTURE_BORDER_COLOR resolves to for one (sampler, texture) pair. `color` is always a
// legal VkBorderColor; when `isCustom` it is one of the *_CUSTOM_EXT values and `customValue`
@@ -117,6 +123,8 @@ private:
UnorderedMap<Uint64, SamplerCacheEntry> m_samplers;
// Monotonic frame-boundary counter (bumped in OnFrameBoundary) for cache aging.
Uint64 m_frameBoundaryCounter = 0;
// Moves on every vkDestroySampler of a cached entry (sweep and Shutdown).
Uint64 m_destroyEpoch = 0;
static inline thread_local XXH64_state_t* m_hashState = XXH64_createState();
};
} // namespace MobileGL::MG_Backend::DirectVulkan
@@ -1874,9 +1874,13 @@ namespace MobileGL::MG_Backend::DirectVulkan {
return nullptr;
}
const Uint64 key = MG_Record::StagedTextureStore::KeyForHandle(handle);
auto [it, inserted] = resources.try_emplace(key);
(void)inserted;
TextureResource& resource = it->second;
auto& lookup = (renderbuffer ? m_wireRenderbufferLookup : m_wireTextureLookup)[key % kWireResourceLookupEntries];
if (lookup.resource == nullptr || lookup.key != key || lookup.eraseEpoch != m_resourceEraseEpoch) {
auto [it, inserted] = resources.try_emplace(key);
(void)inserted;
lookup = {key, m_resourceEraseEpoch, &it->second};
}
TextureResource& resource = *lookup.resource;
if (resource.image != VK_NULL_HANDLE && resource.syncedWireSerial == record.Serial &&
record.PendingUploads.empty() && (!requireStorage || (resource.usageFlags & VK_IMAGE_USAGE_STORAGE_BIT) != 0)) {
return &resource;
@@ -693,6 +693,17 @@ private:
// SyncWireTextureShape defining the RGBA8 storage a YUV shared image is converted into.
Bool m_syncingYuvTextureStorage = false;
Uint64 m_resourceEraseEpoch = 1;
// P14: a direct-mapped front for the two wire resource maps, keyed on the handle key
// ({slot, gen}, never reused) and believed only at the erase epoch it was taken at - an
// unordered_map node never moves until it is erased, and every erase moves the epoch.
struct WireResourceLookup {
Uint64 key = 0;
Uint64 eraseEpoch = 0;
TextureResource* resource = nullptr;
};
static constexpr SizeT kWireResourceLookupEntries = 256;
Array<WireResourceLookup, kWireResourceLookupEntries> m_wireTextureLookup{};
Array<WireResourceLookup, kWireResourceLookupEntries> m_wireRenderbufferLookup{};
// Formats whose mutable-image probe failed on this device; their images are created
// without MUTABLE_FORMAT_BIT so repeat syncs neither re-probe nor flag-mismatch.
std::unordered_set<VkFormat> m_mutableFormatUnsupported;
@@ -289,6 +289,10 @@ namespace MobileGL::MG_Backend::DirectVulkan {
VkDeviceSize indexOffset = 0;
VkIndexType indexType = VK_INDEX_TYPE_MAX_ENUM;
static constexpr Uint32 kMaxShadowedVertexBindings = 8;
// The wire buffer manager's store-destroy epoch the vertex/index halves were stamped at:
// a wire store can be destroyed mid-recording and its VkBuffer handle value re-minted, so
// an equal handle is only the same buffer while this epoch has not moved.
Uint64 bindStoreEpoch = 0;
Bool vertexBindValid = false;
Uint32 vertexBindingCount = 0;
VkBuffer vertexBuffers[kMaxShadowedVertexBindings] = {};
@@ -568,6 +568,8 @@ namespace MobileGL::MG_Backend::DirectVulkan {
Uint32 swapchainImageIndex = 0;
Bool is3D = false;
MG_Pipe::MGPipeHandle storage = MG_Pipe::kMGPipeNullHandle;
// The storage's resource, for a non-default surface (null for the default framebuffer).
VkTextureManager::TextureResource* resource = nullptr;
};
// `isWriteTarget` selects which side of the default framebuffer this resolves: a write
// re-points it at the image Present() acquired, a read keeps the current one (see
@@ -785,6 +787,16 @@ namespace MobileGL::MG_Backend::DirectVulkan {
Uint32 swapchainImageIndex = 0;
Bool isDefault = false, framebufferSrgb = false;
Bool operator==(const WireDrawPassKey&) const = default;
// Default values, vectors emptied but their storage kept (the per-draw scratch).
void ResetKeepingCapacity() {
auto keptAttachments = Move(attachments);
auto keptColorReferences = Move(colorReferences);
*this = WireDrawPassKey{};
keptAttachments.clear();
keptColorReferences.clear();
attachments = Move(keptAttachments);
colorReferences = Move(keptColorReferences);
}
};
struct WireDrawPassCacheEntry {
WireDrawPassKey key;
@@ -799,6 +811,57 @@ namespace MobileGL::MG_Backend::DirectVulkan {
Vector<Vector<WireDrawPassCacheEntry>> m_wireDrawPassCaches;
WireDrawPassKey m_wireDrawPassKey;
WireRenderPassCompatibilityTable m_wireRenderPassCompatibility;
// The interned compatibility id of m_wireDrawPass's attachments, or 0 when not known yet.
// A draw that continues the pass (its WireDrawPassKey compares equal, which covers every
// input of the compatibility key) reuses it instead of building and hashing the key again.
Uint64 m_wireDrawPassCompatibilityId = 0;
// P14: SetupWireDraw's per-draw containers, cleared and refilled every draw instead of
// allocated and freed (the heap was ~10% of a Minecraft frame on the wire arm). A nested
// SetupWireDraw - nothing does that today - would use a local set (WireDraw.inc).
struct WireDrawScratch {
VertexInputStateFactory::BackendVertexInputState input;
Vector<BufferSlice> vertexSlices;
Vector<VkAttachmentDescription> attachments;
Vector<VkImageViewCreateInfo> viewInfos;
Vector<VkAttachmentReference> colors;
Vector<WireImage> sameLayoutAttachments;
WireDrawPassKey passKey;
WireRenderPassCompatibilityKey compatibilityKey;
struct GpuWriteMark {
MG_Pipe::MGPipeHandle storage;
Uint32 level, layer, layers;
};
Vector<GpuWriteMark> gpuWriteMarks;
};
// The current wire pass's attachments were marked GPU-written at this StagedTextureStore
// clear generation (SetupWireDraw); false whenever the pass changes.
Bool m_wirePassMarksValid = false;
// P14: THE OPEN PASS'S ATTACHMENTS, as the last full attachment walk resolved them. A draw
// into the same framebuffer record (FramebufferSerial moves on every framebuffer write,
// ContextSerial on every applier reset) while the pass is still open on this command buffer,
// with no texture image re-created or erased, and every attachment still in GENERAL with no
// pending upload, would resolve exactly these again and continue the pass: it skips both
// resolves and the pass key. Never for the default framebuffer, whose image index moves
// with each acquire.
struct WirePassMemo {
Bool valid = false;
Uint64 framebufferSerial = 0, contextSerial = 0;
MG_Pipe::MGPipeHandle drawFramebuffer = MG_Pipe::kMGPipeNullHandle;
Uint64 textureImageEpoch = 0, resourceEraseEpoch = 0;
Bool framebufferSrgb = false;
Uint64 passHash = 0;
struct Attachment {
VkTextureManager::TextureResource* resource = nullptr;
MG_Pipe::MGPipeHandle storage = MG_Pipe::kMGPipeNullHandle;
Bool renderbuffer = false;
};
Vector<Attachment> attachments;
Vector<WireDrawScratch::GpuWriteMark> marks;
};
WirePassMemo m_wirePassMemo;
Bool WirePassMemoHolds(const MG_Pipe::MGPFramebufferState& fbo, const FrameContext::FrameData& frame) const;
Uint64 m_wirePassMarksClearGeneration = 0;
WireDrawScratch m_wireDrawScratch;
struct WireRetiredObjects {
Uint64 submitIndex = 0;
UniquePtr<RenderPassEntry> drawPass;
@@ -104,6 +104,9 @@
VkRenderPassManager::EndRenderPass(frame.commandBuffer);
}
m_wireDrawPass.reset();
m_wireDrawPassCompatibilityId = 0;
m_wirePassMarksValid = false;
m_wirePassMemo.valid = false;
for (const auto view : m_wireDrawViews) vkDestroyImageView(m_device, view, nullptr);
m_wireDrawViews.clear();
m_wireDrawPassKey = {};
@@ -117,6 +120,9 @@
}
void VulkanRenderer::RetireWireDrawPass() {
m_wireDrawPassCompatibilityId = 0;
m_wirePassMarksValid = false;
m_wirePassMemo.valid = false;
if (!m_wireDrawPass) return;
auto& frame = m_frameContext.GetCurrent();
if (frame.isCommandRecording && VkRenderPassManager::GetActiveRenderPass())
@@ -198,9 +204,45 @@
~WirePreparationScope() { --depth; }
};
Bool VulkanRenderer::WirePassMemoHolds(const MG_Pipe::MGPFramebufferState& fbo,
const FrameContext::FrameData& frame) const {
const auto& memo = m_wirePassMemo;
if (!memo.valid || fbo.IsDefault || !m_wireDrawPass || !frame.isCommandRecording) return false;
const auto* active = VkRenderPassManager::GetActiveRenderPass();
if (active == nullptr || active->commandBuffer != frame.commandBuffer || active->hash != m_wireDrawPass->hash ||
active->hash != memo.passHash)
return false;
const auto& st = MG_Pipe::MGPipeApplier();
if (memo.framebufferSerial != st.FramebufferSerial || memo.contextSerial != st.ContextSerial ||
!(memo.drawFramebuffer == st.BoundFramebuffer[SizeT(MG_Pipe::MGPipeFramebufferTarget::Draw)]) ||
memo.textureImageEpoch != m_textureManager->GetTextureImageEpoch() ||
memo.resourceEraseEpoch != m_textureManager->GetResourceEraseEpoch() ||
memo.framebufferSrgb != MG_Pipe::gPipeInputs.IsCapabilityEnabled(CapabilityInput::FramebufferSrgb))
return false;
// The resources are good while the erase epoch holds. Each must still be what the walk
// left: in GENERAL and synced to its record's serial with nothing pending.
for (const auto& attachment : memo.attachments) {
const auto* resource = attachment.resource;
if (resource == nullptr || resource->layout != VK_IMAGE_LAYOUT_GENERAL) return false;
const auto& records = attachment.renderbuffer ? st.RenderbufferResources : st.TextureResources;
const auto handle = attachment.storage;
if (handle.Slot >= records.size()) return false;
const auto& record = records[handle.Slot];
if (!record.Live || record.Gen != handle.Gen || record.Serial != resource->syncedWireSerial ||
!record.PendingUploads.empty())
return false;
}
return true;
}
Bool VulkanRenderer::SetupWireDraw(FrameContext::FrameData& frame, GLenum mode,
Flags<DrawSetupAspect> aspects, const DrawCmdParam& drawParams, const IndexBufferView* indices) {
auto& state = MG_Pipe::MGPipeApplier();
// The per-draw containers (VulkanRenderer.h's WireDrawScratch). Nothing re-enters this
// function from inside it today; if something ever does, it gets its own set rather
// than clearing the outer draw's.
WireDrawScratch nestedScratch;
WireDrawScratch& scratch = m_wirePreparationDepth == 0 ? m_wireDrawScratch : nestedScratch;
const auto program = WireProgramSource(state.BoundShaderCso);
if (!program.GetLinkStatus() || !program.GetSpirvStatus()) {
MGL_WIRE_DECLINE_AT(ProgramLinkOrSpirv, "program is not linked or carries no SPIR-V");
@@ -250,22 +292,26 @@
// (WireSharedImage.inc).
AcquireNotedSharedImages();
// Lazy image uploads/promotions can submit the old recording. Resolve
// ALL attachments before descriptor binding captures the current cmd.
for (Uint32 i = 0; i < MG_Pipe::kMGPipeMaxColorAttachments; ++i) {
const Int32 slot = fbo->DrawBuffers[i];
if (slot >= 0) (void)ResolveWireImage(*fbo, fbo->Color[slot], VK_IMAGE_ASPECT_COLOR_BIT);
// ALL attachments before descriptor binding captures the current cmd - unless the open
// pass's attachments are provably the ones this draw would resolve (WirePassMemo).
Bool passContinues = WirePassMemoHolds(*fbo, frame);
if (!passContinues) {
for (Uint32 i = 0; i < MG_Pipe::kMGPipeMaxColorAttachments; ++i) {
const Int32 slot = fbo->DrawBuffers[i];
if (slot >= 0) (void)ResolveWireImage(*fbo, fbo->Color[slot], VK_IMAGE_ASPECT_COLOR_BIT);
}
if (fbo->IsDefault || fbo->Depth.Kind != MG_Pipe::kMGPipeSurfaceKindNone)
(void)ResolveWireImage(*fbo, fbo->Depth, VK_IMAGE_ASPECT_DEPTH_BIT);
else if (fbo->Stencil.Kind != MG_Pipe::kMGPipeSurfaceKindNone)
(void)ResolveWireImage(*fbo, fbo->Stencil, VK_IMAGE_ASPECT_STENCIL_BIT);
}
if (fbo->IsDefault || fbo->Depth.Kind != MG_Pipe::kMGPipeSurfaceKindNone)
(void)ResolveWireImage(*fbo, fbo->Depth, VK_IMAGE_ASPECT_DEPTH_BIT);
else if (fbo->Stencil.Kind != MG_Pipe::kMGPipeSurfaceKindNone)
(void)ResolveWireImage(*fbo, fbo->Stencil, VK_IMAGE_ASPECT_STENCIL_BIT);
m_textureManager->FlushPendingUploads();
const auto vaoHandle = state.BoundVertexElements;
if (MG_Pipe::MGPipeHandleIsNull(vaoHandle)) MagmaWireFatal("vertex-elements-record");
if (vaoHandle.Slot >= state.VertexElementsCsos.size()) MagmaWireFatal("vertex-elements-record");
const auto& vao = state.VertexElementsCsos[vaoHandle.Slot];
if (!vao.Live || vao.Gen != vaoHandle.Gen) MagmaWireFatal("vertex-elements-generation");
VertexInputStateFactory::BackendVertexInputState input{};
auto& input = scratch.input;
// P7 wave 2 package C, CONTRACT-P7 §3.2: THE `vertex-layout` ROW IS RETIRED AND SPLIT.
//
// A false is now ONE reason and only one - `buffer-window`, an enabled attribute the
@@ -301,7 +347,8 @@
WireDeclineTally::Count(WireDeclineSite::VertexFormatUnsupported);
return false;
}
Vector<BufferSlice> vertexSlices(input.bindings.size());
auto& vertexSlices = scratch.vertexSlices;
vertexSlices.assign(input.bindings.size(), BufferSlice{});
for (SizeT binding = 0; binding < input.bindings.size(); ++binding) {
const Uint32 location = input.bindingAttributeLocations[binding];
const auto& attr = vao.Attributes[location];
@@ -473,16 +520,34 @@
MGL_WIRE_DECLINE_AT(UniformBufferBinding, "the program's uniform/storage bindings could not be bound");
return false;
}
Vector<VkAttachmentDescription> attachments;
Vector<VkImageViewCreateInfo> viewInfos;
Vector<VkAttachmentReference> colors;
// Asked again: the descriptor resolve may have closed the pass, re-created an image or
// moved a layout.
if (passContinues) passContinues = WirePassMemoHolds(*fbo, frame);
// A memo that holds is the open pass itself (WirePassMemoHolds compared the active pass).
Bool continuing = true;
if (passContinues) {
auto& store = MG_Record::ServerStagedTexture();
if (!m_wirePassMarksValid || m_wirePassMarksClearGeneration != store.GpuDirtyClearGeneration()) {
for (const auto& mark : m_wirePassMemo.marks)
m_textureManager->MarkWireTextureGpuWritten(mark.storage, mark.level, mark.layer, mark.layers);
m_wirePassMarksValid = true;
m_wirePassMarksClearGeneration = store.GpuDirtyClearGeneration();
}
} else {
m_wirePassMemo.valid = false;
auto& attachments = scratch.attachments;
auto& viewInfos = scratch.viewInfos;
auto& colors = scratch.colors;
attachments.clear();
viewInfos.clear();
colors.clear();
VkAttachmentReference depth{VK_ATTACHMENT_UNUSED, VK_IMAGE_LAYOUT_GENERAL};
VkExtent2D extent{~0u, ~0u};
Uint32 layers = ~0u;
VkSampleCountFlagBits samples = VK_SAMPLE_COUNT_1_BIT;
Uint64 passHash = 0;
WireDrawPassKey passKey{};
auto& passKey = scratch.passKey;
passKey.ResetKeepingCapacity();
passKey.isDefault = fbo->IsDefault;
passKey.framebufferSrgb = MG_Pipe::gPipeInputs.IsCapabilityEnabled(CapabilityInput::FramebufferSrgb);
if (fbo->IsDefault) {
@@ -492,11 +557,16 @@
// attachments below resolve the same index, so this key has to be taken after it.
passKey.swapchainImageIndex = DefaultFramebufferWriteIndex(true);
}
Vector<WireImage> sameLayoutAttachments;
auto& sameLayoutAttachments = scratch.sameLayoutAttachments;
sameLayoutAttachments.clear();
scratch.gpuWriteMarks.clear();
m_wirePassMemo.attachments.clear();
const auto add = [&](const MG_Pipe::MGPSurface& surface, VkImageAspectFlags aspect) -> Uint32 {
WireImage image = ResolveWireImage(*fbo, surface, aspect, /*isWriteTarget=*/true);
if (!image.image) return VK_ATTACHMENT_UNUSED;
m_textureManager->FlushPendingUploads();
m_wirePassMemo.attachments.push_back(
{image.resource, image.storage, surface.Kind == MG_Pipe::kMGPipeSurfaceKindRenderbuffer});
if (aspect == VK_IMAGE_ASPECT_COLOR_BIT)
image.format = ResolveSrgbAttachmentWriteFormat(image.format,
MG_Pipe::gPipeInputs.IsCapabilityEnabled(CapabilityInput::FramebufferSrgb));
@@ -530,8 +600,9 @@
layers = std::min(layers, image.layers);
samples = image.samples;
passHash = XXH64(&description, sizeof(description), passHash);
// Marked below, once the pass is known: a continued pass repeats the same marks.
if (!image.isDefault && surface.Kind == MG_Pipe::kMGPipeSurfaceKindTexture)
m_textureManager->MarkWireTextureGpuWritten(image.storage, image.level, image.layer, image.layers);
scratch.gpuWriteMarks.push_back({image.storage, image.level, image.layer, image.layers});
if (image.isDefault) {
if (aspect == VK_IMAGE_ASPECT_COLOR_BIT) m_swapchainObject.SetImageContentDefined(image.swapchainImageIndex, true);
else m_swapchainObject.SetDepthStencilContentDefined(image.swapchainImageIndex, true);
@@ -571,7 +642,7 @@
passInfo.pAttachments = attachments.data();
passInfo.subpassCount = 1;
passInfo.pSubpasses = &subpass;
const Bool continuing = m_wireDrawPass && m_wireDrawPassKey == passKey;
continuing = m_wireDrawPass && m_wireDrawPassKey == passKey;
{
const auto* active = VkRenderPassManager::GetActiveRenderPass();
const Bool stillOpen = continuing && active != nullptr && active->commandBuffer == frame.commandBuffer;
@@ -580,9 +651,13 @@
if (!stillOpen)
for (auto& image : sameLayoutAttachments) TransitionWireImage(image, VK_IMAGE_LAYOUT_GENERAL);
}
if (!continuing) RetireWireDrawPass();
if (!continuing) {
RetireWireDrawPass();
m_wireDrawPassCompatibilityId = 0;
}
if (!continuing && !AcquireCachedWireDrawPass(passKey)) {
m_wireDrawPassKey = Move(passKey);
// A copy, not a move: passKey is the scratch's, and a new pass is the rare case.
m_wireDrawPassKey = passKey;
Vector<VkImageView> views;
views.reserve(viewInfos.size());
for (const auto& info : viewInfos) {
@@ -608,22 +683,90 @@
created.layers = layers;
created.hash = created.compatibilityHash = XXH64(colors.data(), colors.size() * sizeof(colors[0]), passHash);
}
// The attachments' GPU-write marks (MarkWireTextureGpuWritten): idempotent while nothing
// clears a GPU-dirty mark, so a draw that continues a pass whose draws already marked the
// same attachments - the pass key compared equal - repeats them only after a clear.
{
auto& store = MG_Record::ServerStagedTexture();
if (!continuing || !m_wirePassMarksValid ||
m_wirePassMarksClearGeneration != store.GpuDirtyClearGeneration()) {
for (const auto& mark : scratch.gpuWriteMarks)
m_textureManager->MarkWireTextureGpuWritten(mark.storage, mark.level, mark.layer, mark.layers);
m_wirePassMarksValid = true;
m_wirePassMarksClearGeneration = store.GpuDirtyClearGeneration();
}
}
{
// What this walk proved, for WirePassMemoHolds on the next draw.
const auto& st = MG_Pipe::MGPipeApplier();
auto& memo = m_wirePassMemo;
memo.valid = !fbo->IsDefault;
memo.framebufferSerial = st.FramebufferSerial;
memo.contextSerial = st.ContextSerial;
memo.drawFramebuffer = st.BoundFramebuffer[SizeT(MG_Pipe::MGPipeFramebufferTarget::Draw)];
memo.textureImageEpoch = m_textureManager->GetTextureImageEpoch();
memo.resourceEraseEpoch = m_textureManager->GetResourceEraseEpoch();
memo.framebufferSrgb = MG_Pipe::gPipeInputs.IsCapabilityEnabled(CapabilityInput::FramebufferSrgb);
memo.passHash = m_wireDrawPass->hash;
memo.marks.assign(scratch.gpuWriteMarks.begin(), scratch.gpuWriteMarks.end());
}
// The compatibility key is built from this walk's attachments only when the pass is new.
if (m_wireDrawPassCompatibilityId == 0) {
auto& pipelinePassKey = scratch.compatibilityKey;
pipelinePassKey.attachmentFormatsAndSamples.clear();
pipelinePassKey.colorReferences.clear();
for (const auto& attachment : attachments)
pipelinePassKey.attachmentFormatsAndSamples.push_back(
(static_cast<Uint64>(attachment.format) << 32) | static_cast<Uint32>(attachment.samples));
for (const auto& color : colors) pipelinePassKey.colorReferences.push_back(color.attachment);
pipelinePassKey.depthReference = depth.attachment;
m_wireDrawPassCompatibilityId = m_wireRenderPassCompatibility.Intern(pipelinePassKey);
}
} // !passContinues
auto& pass = *m_wireDrawPass;
WireRenderPassCompatibilityKey pipelinePassKey{};
for (const auto& attachment : attachments)
pipelinePassKey.attachmentFormatsAndSamples.push_back(
(static_cast<Uint64>(attachment.format) << 32) | static_cast<Uint32>(attachment.samples));
for (const auto& color : colors) pipelinePassKey.colorReferences.push_back(color.attachment);
pipelinePassKey.depthReference = depth.attachment;
const Uint64 compatibilityId = m_wireRenderPassCompatibility.Intern(pipelinePassKey);
// Interned once per pass (above): a continued pass has the same compatibility.
const Uint64 compatibilityId = m_wireDrawPassCompatibilityId;
const auto pipeline = GetOrCreatePipelineWithInput(mode, program, programObj, flags, input, pass,
ResolvePrimitiveRestartEnable(aspects, indices), compatibilityId);
if (pipeline == VK_NULL_HANDLE) {
MGL_WIRE_DECLINE_AT(PipelineUnavailable, "no graphics pipeline could be created for this draw");
return false;
}
for (Uint32 i = 0; i < vertexSlices.size(); ++i)
vkCmdBindVertexBuffers(frame.commandBuffer, i, 1, &vertexSlices[i].buffer, &vertexSlices[i].offset);
// One call for the whole run of bindings 0..n-1 rather than one per binding, and none when
// the command buffer already holds exactly these (DynamicStateShadow: reset at every
// recording begin and by every auxiliary pipeline; the current-attribute binds below only
// ever touch bindings past n).
auto& bindShadow = *g_dynamicStateShadow;
const Uint64 storeEpoch = m_bufferManager.GetWireStoreDestroyEpoch();
if (bindShadow.bindStoreEpoch != storeEpoch) {
bindShadow.vertexBindValid = false;
bindShadow.indexBindValid = false;
bindShadow.bindStoreEpoch = storeEpoch;
}
if (!vertexSlices.empty()) {
VkBuffer buffers[kMaxVertexAttribs];
VkDeviceSize offsets[kMaxVertexAttribs];
const Uint32 count = static_cast<Uint32>(std::min<SizeT>(vertexSlices.size(), kMaxVertexAttribs));
for (Uint32 i = 0; i < count; ++i) {
buffers[i] = vertexSlices[i].buffer;
offsets[i] = vertexSlices[i].offset;
}
constexpr Uint32 kShadowed = DynamicStateShadow::kMaxShadowedVertexBindings;
Bool same = bindShadow.vertexBindValid && bindShadow.vertexBindingCount == count && count <= kShadowed;
for (Uint32 i = 0; same && i < count; ++i)
same = bindShadow.vertexBuffers[i] == buffers[i] && bindShadow.vertexOffsets[i] == offsets[i];
if (!same) {
vkCmdBindVertexBuffers(frame.commandBuffer, 0, count, buffers, offsets);
bindShadow.vertexBindValid = count <= kShadowed;
bindShadow.vertexBindingCount = count;
for (Uint32 i = 0; i < count && i < kShadowed; ++i) {
bindShadow.vertexBuffers[i] = buffers[i];
bindShadow.vertexOffsets[i] = offsets[i];
}
}
for (Uint32 i = count; i < vertexSlices.size(); ++i)
vkCmdBindVertexBuffers(frame.commandBuffer, i, 1, &vertexSlices[i].buffer, &vertexSlices[i].offset);
}
Uint32 binding = static_cast<Uint32>(vertexSlices.size());
for (Uint32 location = 0; location < kMaxVertexAttribs; ++location) {
if (!(programObj.activeVertexInputLocationMask & (1u << location)) ||
@@ -644,7 +787,15 @@
}
vkCmdBindVertexBuffers(frame.commandBuffer, binding++, 1, &slice.buffer, &slice.offset);
}
if (indexSlice.IsValid()) vkCmdBindIndexBuffer(frame.commandBuffer, indexSlice.buffer, indexSlice.offset, indexType);
if (indexSlice.IsValid() &&
!(bindShadow.indexBindValid && bindShadow.indexBuffer == indexSlice.buffer &&
bindShadow.indexOffset == indexSlice.offset && bindShadow.indexType == indexType)) {
vkCmdBindIndexBuffer(frame.commandBuffer, indexSlice.buffer, indexSlice.offset, indexType);
bindShadow.indexBindValid = true;
bindShadow.indexBuffer = indexSlice.buffer;
bindShadow.indexOffset = indexSlice.offset;
bindShadow.indexType = indexType;
}
// Still open from the previous draw (nothing closed it, same command buffer): continue.
const auto* active = VkRenderPassManager::GetActiveRenderPass();
const Bool open = continuing && active != nullptr && active->commandBuffer == frame.commandBuffer &&
@@ -654,7 +805,11 @@
MGL_WIRE_DECLINE_AT(BeginRenderPass, "the render pass for this draw could not be begun");
return false;
}
vkCmdBindPipeline(frame.commandBuffer, VK_PIPELINE_BIND_POINT_GRAPHICS, pipeline);
if (!bindShadow.graphicsPipelineValid || bindShadow.graphicsPipeline != pipeline) {
vkCmdBindPipeline(frame.commandBuffer, VK_PIPELINE_BIND_POINT_GRAPHICS, pipeline);
bindShadow.graphicsPipelineValid = true;
bindShadow.graphicsPipeline = pipeline;
}
ApplyDynamicDrawStateTail(frame, pass.extent, fbo->IsDefault,
ResolveDrawViewportCount(programObj.writesViewportIndexBuiltin));
// Storage images this draw writes are what the next sampled-image barrier must cover.
@@ -172,6 +172,7 @@
std::max(resource->extent.height >> level, 1u)};
out.layout = resource->layout;
out.trackedLayout = &resource->layout;
out.resource = resource;
out.samples = resource->sampleCount;
// P7 wave 2-B2: the shader multisample resolve SAMPLES this image, which is a question
// about the image and not about the format - VkTextureManager asks for SAMPLED on every
@@ -373,6 +373,7 @@ namespace MobileGL::MG_Record {
m_driverWritten[bucketed] = true;
}
shadow.GpuDirty = false;
m_gpuDirtyClears.fetch_add(1, std::memory_order_relaxed);
return CopyRunInto(shadow, key, uploadTarget, level, imageOffset, bytes, byteSize, frontendShadow);
}
@@ -390,6 +391,7 @@ namespace MobileGL::MG_Record {
LevelShadow& shadow = m_shadows[bucketed].Levels[PackLevel(uploadTarget, level)];
RequireDeclaredLevel(shadow, key, uploadTarget, level, extent);
shadow.GpuDirty = false;
m_gpuDirtyClears.fetch_add(1, std::memory_order_relaxed);
// P8-E: the caller states these bytes ARE the level, which a followed copy is part of.
shadow.HoldsFollowedCopy = false;
shadow.Bytes.clear();
@@ -454,6 +456,7 @@ namespace MobileGL::MG_Record {
shadow.Covered.clear();
shadow.GpuDirty = false;
shadow.HoldsFollowedCopy = false;
m_gpuDirtyClears.fetch_add(1, std::memory_order_relaxed);
}
shadow.Extent = extent;
shadow.InternalFormat = internalFormat;
@@ -471,6 +474,7 @@ namespace MobileGL::MG_Record {
const ShadowKey bucketed = Bucketed(key);
const std::lock_guard<std::mutex> lock(m_mutex);
m_shadows.erase(bucketed);
m_gpuDirtyClears.fetch_add(1, std::memory_order_relaxed);
}
// T5's dirty mark: a GPU-side generation made this level's texels newer than any
@@ -482,14 +486,22 @@ namespace MobileGL::MG_Record {
const ShadowKey bucketed = Bucketed(key);
const std::lock_guard<std::mutex> lock(m_mutex);
m_shadows[bucketed].Levels[PackLevel(uploadTarget, level)].GpuDirty = dirty;
if (!dirty) m_gpuDirtyClears.fetch_add(1, std::memory_order_relaxed);
m_any.store(true, std::memory_order_release);
}
// P14: moves whenever a level's GPU-dirty mark may have gone false - a CPU run or adoption
// over it, a redefinition, an explicit clear, or a level/shadow dropped. A caller that
// re-marks the same levels every draw (Magma's attachments) may skip the repeat while it
// has not moved: marking is idempotent until something clears it.
Uint64 GpuDirtyClearGeneration() const { return m_gpuDirtyClears.load(std::memory_order_relaxed); }
void Drop(Uint64 key) {
if (!m_any.load(std::memory_order_acquire)) return;
const ShadowKey bucketed = Bucketed(key);
const std::lock_guard<std::mutex> lock(m_mutex);
m_shadows.erase(bucketed);
m_gpuDirtyClears.fetch_add(1, std::memory_order_relaxed);
m_driverWritten.erase(bucketed);
}
@@ -503,10 +515,12 @@ namespace MobileGL::MG_Record {
const std::lock_guard<std::mutex> lock(m_mutex);
if (session == 0) {
m_shadows.clear();
m_gpuDirtyClears.fetch_add(1, std::memory_order_relaxed);
m_driverWritten.clear();
return;
}
EraseSession(m_shadows, session);
m_gpuDirtyClears.fetch_add(1, std::memory_order_relaxed);
EraseSession(m_driverWritten, session);
}
@@ -1024,6 +1038,7 @@ namespace MobileGL::MG_Record {
// Read on every IsLevelGpuDirty / LevelExtentOrUndefined, so the monolith cost is one
// acquire load of a never-written flag rather than a mutex and two hash lookups.
std::atomic<Bool> m_any{false};
std::atomic<Uint64> m_gpuDirtyClears{0};
};
// ONE PER PROCESS, and its copying arm is decided ONCE at first use - StagedShadow's
+53
View File
@@ -46,12 +46,64 @@
#include <dlfcn.h>
#endif
#include <algorithm>
#include <chrono>
#include <type_traits>
#include <unordered_map>
#include <unordered_set>
#if defined(__ANDROID__)
#include <sys/system_properties.h>
#endif
#if defined(__unix__) || defined(__linux__) || defined(__ANDROID__)
#include <time.h>
#endif
namespace MobileGL::MG_Impl::EGLImpl {
namespace {
// MOBILEGL_FPS_LOG=1 (on Android also the property debug.mobilegl.fps_log=1): one INFO line
// about every two seconds with the frames this process presented, their mean frame time and
// the presenting thread's CPU per frame. A frame rate for an app-hosted game that shows none
// of its own (a launcher's Minecraft with the debug overlay closed, which is itself a cost).
// Off, a swap pays one load and branch.
Bool FrameRateLogEnabled() {
static const Bool enabled = [] {
const char* value = std::getenv("MOBILEGL_FPS_LOG");
#if defined(__ANDROID__)
char property[PROP_VALUE_MAX] = {};
if (value == nullptr && __system_property_get("debug.mobilegl.fps_log", property) > 0) value = property;
#endif
return value != nullptr && value[0] == '1';
}();
return enabled;
}
void NoteSwapForFrameRateLog() {
if (!FrameRateLogEnabled()) return;
using Clock = std::chrono::steady_clock;
static Clock::time_point windowStart = Clock::now();
static Uint32 frames = 0;
static Double cpuStartMs = -1.0;
const auto threadCpuMs = []() -> Double {
#if defined(__unix__) || defined(__linux__) || defined(__ANDROID__)
struct timespec now {};
if (clock_gettime(CLOCK_THREAD_CPUTIME_ID, &now) == 0)
return static_cast<Double>(now.tv_sec) * 1000.0 + static_cast<Double>(now.tv_nsec) / 1e6;
#endif
return -1.0;
};
if (cpuStartMs < 0.0) cpuStartMs = threadCpuMs();
++frames;
const auto now = Clock::now();
const Double elapsedMs = std::chrono::duration<Double, std::milli>(now - windowStart).count();
if (elapsedMs < 2000.0) return;
const Double cpuNow = threadCpuMs();
MGLOG_I("MobileGL fps: %.1f over %u frames (%.2f ms/frame, presenting thread CPU %.2f ms/frame)",
1000.0 * frames / elapsedMs, frames, elapsedMs / frames,
cpuNow >= 0.0 && cpuStartMs >= 0.0 ? (cpuNow - cpuStartMs) / frames : -1.0);
windowStart = now;
frames = 0;
cpuStartMs = cpuNow;
}
// SetDispatcherCurrentApiHook.
std::atomic<EGLenum (*)()> g_dispatcherCurrentApi{nullptr};
using EGLStateContext = MG_State::EGLState::EGLContext;
@@ -766,6 +818,7 @@ namespace MobileGL::MG_Impl::EGLImpl {
// A resize made between frames takes effect for the next one.
ApplyWaylandResize(state, dpy, draw);
#endif
NoteSwapForFrameRateLog();
return EGL_TRUE;
}
+22 -6
View File
@@ -133,8 +133,11 @@ namespace MobileGL::MG_Pipe {
case F::GetBoundTransformFeedbackName:
dst.m_boundTransformFeedbackName = ctx.GetBoundTransformFeedbackName();
break;
// The four object pins: assigned only when the object moved, so the residual fill of
// every draw does not pay a reference-count increment and decrement for a pointer it
// already holds (P14: one of these moves per Minecraft draw, the other three rarely).
case F::GetBoundVertexArray:
dst.m_boundVertexArray = ctx.GetBoundVertexArray();
if (dst.m_boundVertexArray != ctx.GetBoundVertexArray()) dst.m_boundVertexArray = ctx.GetBoundVertexArray();
break;
case F::GetBufferBindingSlot:
// Every global target has a slot; Index stays null - GLContext resolves it
@@ -247,10 +250,11 @@ namespace MobileGL::MG_Pipe {
dst.m_primitiveRestartIndex = ctx.GetPrimitiveRestartIndex();
break;
case F::GetProgramForDispatch:
dst.m_programForDispatch = ctx.GetProgramForDispatch();
if (dst.m_programForDispatch != ctx.GetProgramForDispatch())
dst.m_programForDispatch = ctx.GetProgramForDispatch();
break;
case F::GetProgramForDraw:
dst.m_programForDraw = ctx.GetProgramForDraw();
if (dst.m_programForDraw != ctx.GetProgramForDraw()) dst.m_programForDraw = ctx.GetProgramForDraw();
break;
case F::GetProvokingVertexMode:
dst.m_provokingVertexMode = ctx.GetProvokingVertexMode();
@@ -291,7 +295,8 @@ namespace MobileGL::MG_Pipe {
dst.m_transformFeedbackPausedPrimitiveCounter = ctx.GetTransformFeedbackPausedPrimitiveCounter();
break;
case F::GetTransformFeedbackProgram:
dst.m_transformFeedbackProgram = ctx.GetTransformFeedbackProgram();
if (dst.m_transformFeedbackProgram != ctx.GetTransformFeedbackProgram())
dst.m_transformFeedbackProgram = ctx.GetTransformFeedbackProgram();
break;
case F::GetViewport:
dst.m_viewport = ctx.GetViewport();
@@ -3165,11 +3170,22 @@ namespace MobileGL::MG_Pipe {
values.TransformFeedbackGeneration = ctx.GetTransformFeedbackGeneration();
values.BoundTransformFeedbackLifetimeId = ctx.GetBoundTransformFeedbackLifetimeId();
values.TransformFeedbackCapturedVertices = ctx.GetTransformFeedbackCapturedVertices();
// P14: the record is rebuilt every draw and nearly always equal to the last one. When it
// is byte-equal to the record whose hash the suppressor still holds, the suppressor
// would drop it: skip the hash. (An InvalidateAll changes what the suppressor holds,
// so a reset still re-emits.)
static MGPContextValues s_lastValues{};
static Uint64 s_lastHash = 0;
auto& suppressor = MGPipeSetHashSuppressorInstance();
if (s_lastHash != 0 && std::memcmp(&values, &s_lastValues, sizeof(values)) == 0 &&
suppressor.LastEmitted(MGPipeSuppressorSlot::SetContextValues) == s_lastHash)
return 0;
// The whole record is the hash input, padding included - `values{}` zeroes it, so
// the pad bytes are defined and the hash is stable.
const Uint64 contentHash = XXH64(&values, sizeof(values), 0);
if (!MGPipeSetHashSuppressorInstance().ShouldEmit(MGPipeSuppressorSlot::SetContextValues,
contentHash)) {
s_lastValues = values;
s_lastHash = contentHash == 0 ? 1 : contentHash; // the suppressor's own latch value
if (!suppressor.ShouldEmit(MGPipeSuppressorSlot::SetContextValues, contentHash)) {
return 0;
}
MGPipeRouteSetContextValues(values);
+12
View File
@@ -206,10 +206,20 @@ namespace MobileGL::MG_Pipe {
#endif
if (lifetimeId == 0) return kMGPipeNullHandle;
const KindState& state = StateOf(kind);
// The direct-mapped front (SlotAllocator.h): a hit is only believed when the slot it
// names is live AND still owned by this very lifetime id.
auto& recent = state.Recent[lifetimeId % KindState::kRecentEntries];
if (recent.LifetimeId == lifetimeId) {
const SlotState* entry = EntryOf(state, kind, recent.Slot);
if (entry != nullptr && entry->Live && entry->LifetimeId == lifetimeId)
return MGPipeHandle{recent.Slot, entry->Gen};
}
const auto it = state.ByLifetimeId.find(lifetimeId);
if (it == state.ByLifetimeId.end()) return kMGPipeNullHandle;
const SlotState* entry = EntryOf(state, kind, it->second);
if (entry == nullptr || !entry->Live) return kMGPipeNullHandle;
recent.LifetimeId = lifetimeId;
recent.Slot = it->second;
return MGPipeHandle{it->second, entry->Gen};
}
@@ -298,12 +308,14 @@ namespace MobileGL::MG_Pipe {
}
void MGPipeSlotAllocator::Reset() {
++m_resetEpoch;
for (KindState& state : m_kinds) {
state.Slots.clear();
state.FreeList.clear();
state.BandSlots.clear();
state.BandFreeList.clear();
state.ByLifetimeId.clear();
state.Recent.fill({});
state.LiveCount = 0;
state.BandLiveCount = 0;
}
+15
View File
@@ -120,6 +120,9 @@ namespace MobileGL::MG_Pipe {
// Context teardown / server reset / a unit test's fixture.
void Reset();
// Moves at every Reset(): a memo of handles minted by this allocator is good only while it
// holds (a handle lives as long as its object, and only a reset forgets that).
Uint64 ResetEpoch() const { return m_resetEpoch; }
private:
struct SlotState {
@@ -144,12 +147,24 @@ namespace MobileGL::MG_Pipe {
Vector<SlotState> BandSlots;
Vector<Uint32> BandFreeList;
UnorderedMap<Uint64, Uint32> ByLifetimeId;
// P14: a direct-mapped front for ByLifetimeId. Every draw resolves its VAO, its
// vertex buffers and its textures by lifetime id, and the hash map was ~2-3% of a
// Minecraft frame. Keyed on the lifetime id (monotonic, never reused) and confirmed
// against the slot's own LifetimeId and Live bit on every hit, so a stale entry can
// only miss. Mutable: FindByLifetimeId is const and fills it.
struct RecentEntry {
Uint64 LifetimeId = 0;
Uint32 Slot = 0;
};
static constexpr SizeT kRecentEntries = 1024;
mutable Array<RecentEntry, kRecentEntries> Recent{};
Uint32 LiveCount = 0;
// The band's share of LiveCount above, so the two spaces can be reported apart
// without walking either table. Always 0 for every kind but ShaderCso.
Uint32 BandLiveCount = 0;
};
Uint64 m_resetEpoch = 0;
KindState& StateOf(MGPipeKind kind);
const KindState& StateOf(MGPipeKind kind) const;
// The SlotState a (kind, slot) names, in whichever of the two vectors holds it, or
+83 -4
View File
@@ -168,7 +168,12 @@ namespace MobileGL::MG_Pipe {
const Uint64 lifetimeId = vao->GetLifetimeId();
const Uint32 configVersion = vao->GetConfigVersion();
const MGPipeHandle handle = MGPipeSlots().Acquire(MGPipeKind::VertexElementsCso, lifetimeId);
// The VAO's handle is the allocator's for the object's whole life (only an allocator
// reset forgets it), so it is resolved once per VAO rather than per draw (VaoMemo).
VaoMemo& memo = MemoFor(*vao);
if (MGPipeHandleIsNull(memo.ElementsHandle))
memo.ElementsHandle = MGPipeSlots().Acquire(MGPipeKind::VertexElementsCso, lifetimeId);
const MGPipeHandle handle = memo.ElementsHandle;
const SizeT slot = handle.Slot;
if (slot >= m_latch.size()) m_latch.resize(slot + 1);
Latch& latch = m_latch[slot];
@@ -200,7 +205,22 @@ namespace MobileGL::MG_Pipe {
const Array<MGPipeHandle, kMGPipeMaxVertexAttribs>* ownedClientBuffers = nullptr) {
const auto& vao = ctx.GetBoundVertexArray();
Uint32 count = 0;
if (vao) {
// The resolved set is a function of the VAO's configuration alone (buffers, strides,
// divisors and enables all move its version; its buffers' handles live as long as the
// VAO holds the buffers), so it is walked once per configuration rather than per draw -
// a Minecraft frame binds hundreds of chunk VAOs. Not for client-memory arrays, whose
// entries the draw itself supplies.
VaoMemo* memo = vao && ownedClientBuffers == nullptr ? &MemoFor(*vao) : nullptr;
Bool memoHit = false;
if (memo != nullptr && memo->BuffersValid && memo->ConfigVersion == vao->GetConfigVersion()) {
memoHit = true;
count = memo->BufferCount;
for (SizeT i = 0; i < count; ++i) {
m_entries[i] = memo->Buffers[i];
MGPipeResourceTrackerInstance().NoteBoundAs(m_entries[i].Res, BufferTarget::Vertex);
}
} else if (vao) {
Bool memoizable = memo != nullptr;
for (SizeT i = 0; i < kAttribs; ++i) {
if (vao->GetAttribute(static_cast<Uint>(i)).Enabled) count = static_cast<Uint32>(i) + 1;
}
@@ -230,10 +250,30 @@ namespace MobileGL::MG_Pipe {
entry.Stride = static_cast<Uint32>(attrib.Stride);
entry.Divisor = static_cast<Uint32>(attrib.Divisor);
entry.BindingIndex = static_cast<Uint32>(i);
if (attrib.Enabled && !attrib.Buffer) memoizable = false;
}
if (memo != nullptr) {
memo->HashValid = false;
memo->BuffersValid = memoizable && count <= kVaoMemoBuffers;
memo->ConfigVersion = vao->GetConfigVersion();
memo->BufferCount = count;
for (SizeT i = 0; i < count && i < kVaoMemoBuffers; ++i) memo->Buffers[i] = m_entries[i];
}
}
const Uint64 hash = MGPipeVertexBufferSetContentHash(m_entries.data(), 0, count, baseInstance);
// The set's hash is a function of the entries and the base instance, so a memoised set
// keeps the one it was hashed with for as long as the base instance is the same.
Uint64 hash = 0;
if (memoHit && memo->HashValid && memo->HashBaseInstance == baseInstance) {
hash = memo->Hash;
} else {
hash = MGPipeVertexBufferSetContentHash(m_entries.data(), 0, count, baseInstance);
if (memo != nullptr && memo->BuffersValid) {
memo->Hash = hash;
memo->HashBaseInstance = baseInstance;
memo->HashValid = true;
}
}
if (!MGPipeSetHashSuppressorInstance().ShouldEmit(MGPipeSuppressorSlot::SetVertexBuffers, hash)) {
return 0;
}
@@ -261,7 +301,13 @@ namespace MobileGL::MG_Pipe {
m_lastIndex = MGPIndexBuffer{};
if (vao) {
if (const auto& bound = vao->GetIndexBufferBindingSlot().GetBoundObject()) {
m_lastIndex.Res = MGPipeSlots().Acquire(MGPipeKind::Buffer, bound->GetLifetimeId());
// Per VAO, keyed on the element buffer's own lifetime id (VaoMemo).
VaoMemo& memo = MemoFor(*vao);
if (memo.IndexLifetimeId != bound->GetLifetimeId() || MGPipeHandleIsNull(memo.IndexHandle)) {
memo.IndexLifetimeId = bound->GetLifetimeId();
memo.IndexHandle = MGPipeSlots().Acquire(MGPipeKind::Buffer, memo.IndexLifetimeId);
}
m_lastIndex.Res = memo.IndexHandle;
// The INDEX|ELEMENT_ARRAY bind bits. ELEMENT_ARRAY's D-B7 reader
// (kCapNeedsHostIndexBytes) is RESERVED (P8-F) and was never built.
// Noted at every draw for RefreshBindMask's reason: an EBO defined through
@@ -353,6 +399,39 @@ namespace MobileGL::MG_Pipe {
void ResetCounters() { m_creates = m_binds = m_bufferSets = m_indexSets = 0; }
private:
// P14: what EmitVertexElements / EmitVertexBuffers / EmitIndexBuffer resolve from a VAO,
// direct-mapped on the VAO's lifetime id (monotonic, never reused) and dropped whole at an
// allocator reset. A colliding VAO simply takes the entry over. It changes no record: the
// same handles and entries are emitted, only found without the per-draw walk and lookups.
static constexpr SizeT kVaoMemoEntries = 1024;
static constexpr SizeT kVaoMemoBuffers = 8;
struct VaoMemo {
Uint64 LifetimeId = 0;
Uint64 SlotEpoch = 0;
MGPipeHandle ElementsHandle = kMGPipeNullHandle;
Uint64 IndexLifetimeId = 0;
MGPipeHandle IndexHandle = kMGPipeNullHandle;
Bool BuffersValid = false;
Uint32 ConfigVersion = 0;
Uint32 BufferCount = 0;
Bool HashValid = false;
Uint32 HashBaseInstance = 0;
Uint64 Hash = 0;
Array<MGPVertexBuffer, kVaoMemoBuffers> Buffers{};
};
VaoMemo& MemoFor(const VertexArrayObject& vao) {
const Uint64 lifetimeId = vao.GetLifetimeId();
const Uint64 epoch = MGPipeSlots().ResetEpoch();
VaoMemo& memo = m_vaoMemo[lifetimeId % kVaoMemoEntries];
if (memo.LifetimeId != lifetimeId || memo.SlotEpoch != epoch) {
memo = VaoMemo{};
memo.LifetimeId = lifetimeId;
memo.SlotEpoch = epoch;
}
return memo;
}
Array<VaoMemo, kVaoMemoEntries> m_vaoMemo{};
struct Latch {
// The PER-CONTEXT half: "has this emitter told THIS server about this handle's
// configuration". Cleared by Reset() at every make-current.
+16
View File
@@ -475,6 +475,22 @@ namespace MobileGL::MG_Pipe {
// so it obeys VertexBuffersSerial's rule: ADVANCED, never returned to a value it has
// already handed out.
Uint64 BindingsSerial = 0;
// P14: the backend program factory's content hash of THIS record's program, one entry per
// compile variant (the factory's own memo key). The hash reads the archive's SPIR-V, the
// block-binding tail and the variant, so an entry is valid exactly while Serial (a relink)
// and BindingsSerial (set_program_bindings) still hold the values it was taken at. It
// lives in the record so it dies with it: a recycled slot starts from a fresh record and
// no other record can ever answer from it. Nothing in MG_Pipe reads it.
struct BackendHashMemoEntry {
Uint64 Serial = 0;
Uint64 BindingsSerial = 0;
Uint64 Hash = 0;
Uint32 Key = 0;
Bool Valid = false;
};
mutable Array<BackendHashMemoEntry, 4> BackendHashMemo{};
mutable Uint32 BackendHashMemoNext = 0;
};
// The vertex-elements CSO as the applier holds it: the unpacked blob, both views, plus
+17 -4
View File
@@ -1087,6 +1087,14 @@ namespace MobileGL::MG_Remote::Server {
static std::uint64_t sFrameIndex = 0;
Uint64 applied = 0;
const bool timed = ServerFrameStatsEnabled();
// P14: the GPU-fault verdict is asked once per kFaultCheckRecordStride records inside a
// drain, and on every Present. On Espryt it is glGetGraphicsResetStatus, a driver ioctl
// (~3 us): asked per record it was a quarter of the apply thread on Minecraft (~7,500
// records a frame). The apply loop still asks before every drain (above), so a fault that
// lands while the session is parked still stops its very next record; one that lands
// mid-drain is acted on within the stride - the frame was being applied either way.
constexpr Uint32 kFaultCheckRecordStride = 256;
Uint32 recordsSinceFaultCheck = 0;
for (;;) {
if (SessionLatched()) break;
if (m_windowRequests.load(std::memory_order_acquire) != 0) {
@@ -1095,10 +1103,15 @@ namespace MobileGL::MG_Remote::Server {
}
bool corrupt = false;
const bool popped = consumer.ApplyOne(
[this, &applier, timed](const Transport::RingRecordView& record) {
// Check after acquiring each record: a producer may publish more work
// while this drain is running, after the outer-loop reset check.
if (m_backend != nullptr && m_backend->LatchIfGpuFaulted()) return false;
[this, &applier, timed, &recordsSinceFaultCheck](const Transport::RingRecordView& record) {
// Checked inside the drain too: a producer may publish more work while this
// drain is running, after the outer-loop check (stride above).
if (m_backend != nullptr &&
(++recordsSinceFaultCheck >= kFaultCheckRecordStride ||
record.kind == static_cast<std::uint16_t>(MG_Pipe::MGPWireOp::Present))) {
recordsSinceFaultCheck = 0;
if (m_backend->LatchIfGpuFaulted()) return false;
}
if (SessionLatched()) return false;
// P65ServerFrame: timed around the ONE call that is the server's work, and
// summed across the drains a frame spans. See the block above DrainRing's loop.
@@ -10,8 +10,11 @@
namespace MobileGL::MG_State::GLState {
BufferState::BufferState() {
m_bindingSlotIndex.fill(-1);
for (SizeT i = 0; i < m_bindingSlots.size(); ++i) {
m_bindingSlots[i] = BindingSlot<BufferObject>(GlobalBufferTargets[i]);
const auto target = static_cast<SizeT>(GlobalBufferTargets[i]);
if (target < m_bindingSlotIndex.size()) m_bindingSlotIndex[target] = static_cast<Int16>(i);
}
for (SizeT i = 0; i < m_touchedBindPointCount.size(); ++i) {
m_touchedBindPointCount[i] = 0;
@@ -19,6 +22,10 @@ namespace MobileGL::MG_State::GLState {
}
BindingSlot<BufferObject>& BufferState::GetBindingSlot(BufferTarget target) {
// O(1) through the index the constructor built (every draw asks for several targets).
const auto index = static_cast<SizeT>(target);
if (index < m_bindingSlotIndex.size() && m_bindingSlotIndex[index] >= 0)
return m_bindingSlots[static_cast<SizeT>(m_bindingSlotIndex[index])];
for (auto& bindingSlot : m_bindingSlots) {
if (bindingSlot.GetTarget() == target) {
return bindingSlot;
@@ -106,6 +106,8 @@ namespace MobileGL::MG_State::GLState {
Uint64 m_anyBufferChangeGeneration = 0;
Array<Uint64, BufferBindPointTargets.size()> m_bindPointGeneration{};
Array<BindingSlot<BufferObject>, GlobalBufferTargets.size()> m_bindingSlots;
// BufferTarget -> index into m_bindingSlots, -1 for a target with no global slot.
Array<Int16, static_cast<SizeT>(BufferTarget::BufferTargetCount)> m_bindingSlotIndex{};
// TODO: query the count somewhere globally?
// For glBindBufferBase / glBindBufferRange
Array<Array<BindingSlotRange1D<BufferObject>, BufferBindingPointCount>, BufferBindPointTargets.size()>
@@ -0,0 +1,79 @@
# P14 perf pass: estimate from the code (written before the benchmark)
Date: 2026-10-07. Code: `feat/disaggregated` at `f8a9466b` ("feat") against `dev` at `08124c99` ("dev",
the pull monolith, the "before" baseline per DEBTS.md). Device for the measurement that follows:
Lenovo Y700 (TB321FU, Snapdragon 8 Gen 3, Adreno 750), rooted, clocks pinned.
**Disclosure.** Before writing this I ran one unpinned pipeline-validation smoke of the new harness
(Magma only, one rep, dev / feat monolith / feat inproc): openra 247 / 178 / 146 fps, rd12
127 / 46 / 33 fps. Those numbers are bigger than the logged 16 % (DEBTS ID-P13-2) and I had seen
them when I wrote the Magma rows below, so the Magma magnitudes are not blind. The mechanism
attribution (which costs, on which thread) is from the code and is what the profile will test;
the Espryt, spawn and tcp rows were written before any run of those arms.
## 1. What each variant does per call and per draw
| | dev pull monolith | feat monolith (record arm) | feat inproc | feat spawn (shm) | feat tcp (localhost) |
|---|---|---|---|---|---|
| GL entry | frontend state only | + `GLStreamScope` (one call + branch) | + process-wide recursive mutex per GL call | same as inproc | same as inproc |
| state calls | frontend state | + mutation notes (`MGP_NOTE_*`), dirty bits | same as monolith | | |
| per draw, client side | none | `MGPipeValidateForVerb`: tracker update (18 shutters), up to 14 emitters (CSO cache probe 64-entry, sampler views/states walks with hash-map slot lookups + XXH64), **`EmitContextValues` + XXH64 every draw**, residual fill of up to 63 fields incl. 3 SharedPtr copies (atomics); verb port: `ReadDrawBindings` hash lookups, persistent-map tracker, `MarkGpuWritesForDraw` | same, plus each record memcpy'd into the SEG_CMD ring and blobs (UBO images, sub-data) into SEG_STAGE | same as inproc | + `sendmsg` flush at every wait / present / fence poll |
| per draw, server side | backend reads `pGLContext` | applier (`MGPipeApply*`, same thread) + backend reads applier records; one extra copy of each changed default-uniform block into the applier | decode + bounds checks + apply + backend on the apply thread; staged bytes copied into server storage | same as inproc, in a second process | + io thread `recv` into mirrors |
| Magma draw setup | `TrySetupDrawFastPath` (4 snapshots keyed on program lifetime id; skips render-pass, descriptor, texture-sync and pipeline work when nothing moved) | `SetupWireDraw` every draw: `PrepareWireTextureResources` (texture sync per sampled binding), attachments resolved twice, `BuildWireVertexInput` rebuilt (Vector pushes + 4 XXH64), descriptor writes rebuilt + FNV over every info + 4-entry reuse memo, ~10 heap Vectors for the render-pass key, `UnorderedMap` intern of the pass-compatibility key, pipeline memo probe, unconditional `vkCmdBindPipeline` | same backend work on the apply thread | | |
| Espryt draw setup | memos keyed on frontend pointers / versions | the same memos re-keyed on applier serials (texture sync list, unit-bindings epoch, FBO lists, resolved-buffers memo, cached `glBindBufferBase/Range`); per-unit record lookups in `ResolveAndBindUnitTextures` | same on the apply thread | | |
| threads that burn CPU | 1 | 1 | 2 (client + apply), run-ahead armed | 2 processes | 2 processes + io threads |
| sync points | none | none | `kWaitReply` / `kWaitApplied` rows (fence waits, queries, readbacks, mipmaps, copy-tex, make-current), present credit 1 | same + socket doorbell when parked | same + credit-1 ping-pong over the socket (P10: 11.2 of 19.4 ms/frame parked) |
The trace APK's library is the split shape (`MOBILEGL_BUILD_DISAGGREGATED=ON`), which also turns on
`MOBILEGL_PIPE_POISON` (a stamp per filled field and a freshness check per backend accessor read).
FCL's library is the push shape without it. So the trace APK's monolith arm carries a little more
than the FCL library does.
## 2. Predictions
Workloads: openra (light: 128 frames, ~28 draws and ~199 records per frame) and rd12 (Minecraft
1.21.4 RD12: 251 frames, ~7,500 records per frame, several hundred draws per frame). Both are light
on the GPU, so every arm should be **CPU-bound**: GPU busy well under 60 %, with one thread at or
near 100 % for monolith and dev.
Per-draw costs I expect (Y700 big core at ~2 GHz):
- feat validate point + verb port over dev: **~2-4 us per draw** (an XXH64 of the context values, a
few hash-map lookups, up to 63 field copies, the persistent-map and GPU-write walks).
- Magma `SetupWireDraw` over dev's fast path: **~8-15 us per draw** (about 10 heap allocations, a
texture sync per sampled texture, two attachment resolves, the vertex-input rebuild and the
descriptor rebuild). This dwarfs everything else in this table.
| workload / backend | dev | feat monolith | feat inproc | feat shm | feat tcp |
|---|---|---|---|---|---|
| rd12 Espryt | baseline, 1 thread saturated | **10-25 % slower** (validate point + verb port on a ~7-9 ms frame) | **0-20 % faster than feat monolith**, roughly at dev: client and apply threads overlap, total CPU +20-40 % | about inproc (P5/P8 history: shm about inproc) | **3-6x slower than shm**: credit-1 ping-pong and wake-ups, not bandwidth |
| rd12 Magma | baseline; dev Magma at or ahead of dev Espryt (fast path + earlier walk-skip work) | **1.6-2.5x the dev frame time**: SetupWireDraw dominates | apply-thread bound (the same SetupWireDraw), so **at most 10-25 % better than feat monolith and still far behind dev** | about inproc | as Espryt tcp, worse in absolute terms |
| openra Espryt | baseline | within 5 % (few draws; the fixed per-frame cost dominates) | **5-15 % slower** than monolith (handshake, no work to overlap) | 10-15 % slower than monolith | 20-30 % slower than monolith |
| openra Magma | baseline | 15-35 % slower (per-draw gap times only ~28 draws, plus per-frame wire costs) | slower than monolith, as Espryt | | |
## 3. What this says about the three beliefs (to be checked)
1. **"inproc is faster than the pull monolith."** Partly. Only on draw-heavy frames, only where the
apply thread's backend is cheap enough for the client half to be the critical path (Espryt),
and the gain is overlap on a second core, not less work: total CPU goes up. Light loads show
no gain and pay the handshake. Magma inproc stays behind dev until its draw setup is fixed.
2. **"push and pull are on par."** Wrong on Magma (the lost fast path) and wrong by a smaller margin
on draw-heavy Espryt (the validate point and verb port are per-draw costs pull never paid).
Close on light loads.
3. **"the two backends are on par."** On dev, roughly yes (Magma ahead on Minecraft-like frames).
On feat, no: Magma pays its draw setup in full. Both backends are CPU-bound on these scenes,
so parity means cutting CPU work until both feed the GPU.
## 4. Fix plan the estimate implies, in order
1. A Magma wire draw fast path: snapshot the resolved draw (program lifetime id, applier serials
for bound views, samplers, FBO and vertex elements, render-pass key, pipeline, descriptor
signature) and skip the re-resolution while nothing moved. Keys must be monotonic lifetime ids
or applier generations, never handles or pointers. Also cheaper on a miss: no per-draw heap
Vectors, one attachment resolve, a cached compatibility id, `vkCmdBindPipeline` deduplicated.
Expected: most of the Magma gap on rd12.
2. W9: read `MOBILEGL_PIPE_STATS` on both workloads (CSO mints vs binds, pipeline memo misses,
descriptor reuse misses) and resize where a cache thrashes. Expected: small (<5 %) unless a
cache is thrashing.
3. Then whatever simpleperf shows on the client half (validate point, `EmitContextValues`,
residual fill) and on the split arms (ring encode, mutex, waits).
+107
View File
@@ -0,0 +1,107 @@
#!/usr/bin/env python3
"""Split one function's inclusive samples by its direct callees (and by malloc/atomic leaves).
python callee_split.py <perf.data> <binary_cache> <function substring> [--depth 1] [--thread NAME]
Uses the NDK's simpleperf_report_lib (set SIMPLEPERF_DIR to <ndk>/simpleperf). For every sample
whose call chain contains a frame matching <function substring>, attributes the sample to the
frame directly below it (its callee), or to "<self>" when it is the leaf. Prints the percentage
of all samples of the selected threads.
"""
import argparse
import os
import sys
from collections import Counter
sys.path.insert(0, os.environ.get("SIMPLEPERF_DIR", ""))
from simpleperf_report_lib import ReportLib # noqa: E402
def tail_start(data, symfs, tail_ms):
"""Timestamp (ns) where the last `tail_ms` of the recording begins, or 0 for everything."""
if not tail_ms:
return 0
lib = ReportLib()
lib.SetRecordFile(data)
lib.SetSymfs(symfs)
# The end is the replay's last retraced call, not the recording's last sample: the app
# tears its context down after the benchmark ends, and that must not land in the window.
last = 0
while True:
sample = lib.GetNextSample()
if sample is None:
break
chain = lib.GetCallChainOfCurrentSample()
if any("retrace::retraceCall" in chain.entries[i].symbol.symbol_name for i in range(chain.nr)):
last = max(last, sample.time)
return last - int(tail_ms * 1e6)
def short(name):
name = name.split("(")[0]
return name.replace("MobileGL::", "").replace("MG_Backend::DirectVulkan::", "DV::")[-110:]
def main():
ap = argparse.ArgumentParser()
ap.add_argument("data")
ap.add_argument("symfs")
ap.add_argument("function")
ap.add_argument("--depth", type=int, default=1)
ap.add_argument("--thread", default=None)
ap.add_argument("--top", type=int, default=40)
ap.add_argument("--tail-ms", type=float, default=0.0, help="only the last N ms of the recording")
ap.add_argument("--leaf", action="store_true",
help="match <function> against the LEAF only and attribute to the nearest caller "
"outside the allocator/libc/atomics (who called malloc?)")
args = ap.parse_args()
start = tail_start(args.data, args.symfs, args.tail_ms)
lib = ReportLib()
lib.SetRecordFile(args.data)
lib.SetSymfs(args.symfs)
total = 0
hit = 0
callees = Counter()
while True:
sample = lib.GetNextSample()
if sample is None:
break
if (args.thread and args.thread not in sample.thread_comm) or sample.time < start:
continue
total += 1
# frames leaf first: the sample's own symbol, then callchain entries (callers)
frames = [lib.GetSymbolOfCurrentSample().symbol_name]
chain = lib.GetCallChainOfCurrentSample()
for i in range(chain.nr):
frames.append(chain.entries[i].symbol.symbol_name)
if args.leaf:
if args.function not in frames[0]:
continue
hit += 1
noise = ("scudo", "malloc", "free", "operator new", "operator delete", "__aarch64_", "@plt",
"memmove", "memcpy", "memset", "__emutls", "pthread_", "shared_count", "__release_shared",
"!!!", "std::__ndk1::")
owner = next((f for f in frames[1:] if not any(n in f for n in noise)), "<none>")
callees[short(owner)] += 1
continue
# find the outermost (closest to root) matching frame so recursion counts once
idx = None
for i in range(len(frames) - 1, -1, -1):
if args.function in frames[i]:
idx = i
break
if idx is None:
continue
hit += 1
if idx == 0:
callees["<self>"] += 1
continue
key = " > ".join(short(frames[j]) for j in range(idx - 1, max(idx - 1 - args.depth, -1), -1))
callees[key] += 1
print(f"samples={total} in '{args.function}'={hit} ({100.0 * hit / max(1, total):.1f}%)")
for key, n in callees.most_common(args.top):
print(f"{100.0 * n / max(1, total):6.2f}% {key}")
if __name__ == "__main__":
main()
+89
View File
@@ -0,0 +1,89 @@
#!/usr/bin/env python3
"""Inclusive share of fixed components on one thread of a simpleperf profile, converted to
milliseconds per frame with the thread's measured CPU ms/frame.
python components.py <perf.data> <binary_cache> <thread comm> [cpu_ms_per_frame] [--tail-ms=N]
A sample counts toward a component when any frame of its call chain matches the component's
pattern (first match in COMPONENTS order wins for the 'exclusive' column, so the rows of that
column add up to 100%).
"""
import os
import sys
from collections import Counter
sys.path.insert(0, os.environ.get("SIMPLEPERF_DIR", ""))
from simpleperf_report_lib import ReportLib # noqa: E402
def tail_start(data, symfs, tail_ms):
"""Timestamp (ns) where the last `tail_ms` of the recording begins, or 0 for everything."""
if not tail_ms:
return 0
lib = ReportLib()
lib.SetRecordFile(data)
lib.SetSymfs(symfs)
# The end is the replay's last retraced call, not the recording's last sample: the app
# tears its context down after the benchmark ends, and that must not land in the window.
last = 0
while True:
sample = lib.GetNextSample()
if sample is None:
break
chain = lib.GetCallChainOfCurrentSample()
if any("retrace::retraceCall" in chain.entries[i].symbol.symbol_name for i in range(chain.nr)):
last = max(last, sample.time)
return last - int(tail_ms * 1e6)
COMPONENTS = [
("trace parse (harness)", ("trace::Parser::", "BrotliFile", "trace::Call::~Call", "trace::File::")),
("validate point", ("MGPipeValidateForVerb",)),
("backend draw setup", ("SetupWireDraw", "VulkanRenderer::SetupDraw", "DirectGLES::PrepareForDraw")),
("backend draw other", ("DirectVulkan::DrawElements", "DirectGLES::DrawElements", "VulkanRenderer::DrawElements")),
("verb port / record sink", ("EmitDrawRecord", "VerbChannel::Port", "RecordVerbSink::")),
("wire encode (client)", ("ClientSession::Emit", "PipeWireEncoder::")),
("apply decode", ("PipeWireDecoder::", "PipeApplier::ApplyOne")),
("apply loop idle/poll", ("ServerLoop::ApplyThreadMain", "ServerLoop::DrainRing")),
("other GL entry points", ("retrace::retraceCall",)),
]
def main():
args = [a for a in sys.argv[1:] if not a.startswith("--tail-ms=")]
tail = next((float(a.split("=", 1)[1]) for a in sys.argv[1:] if a.startswith("--tail-ms=")), 0.0)
data, symfs, comm = args[0], args[1], args[2]
cpu = float(args[3]) if len(args) > 3 else None
start = tail_start(data, symfs, tail)
lib = ReportLib()
lib.SetRecordFile(data)
lib.SetSymfs(symfs)
total = 0
inclusive = Counter()
exclusive = Counter()
while True:
sample = lib.GetNextSample()
if sample is None:
break
if comm not in sample.thread_comm or sample.time < start:
continue
total += 1
frames = [lib.GetSymbolOfCurrentSample().symbol_name]
chain = lib.GetCallChainOfCurrentSample()
frames += [chain.entries[i].symbol.symbol_name for i in range(chain.nr)]
first = None
for name, patterns in COMPONENTS:
if any(p in f for f in frames for p in patterns):
inclusive[name] += 1
if first is None:
first = name
exclusive[first or "rest"] += 1
print(f"{comm}: {total} samples" + (f", {cpu:.2f} ms/frame" if cpu else ""))
for name, _ in COMPONENTS + [("rest", ())]:
inc = 100.0 * inclusive[name] / max(1, total)
exc = 100.0 * exclusive[name] / max(1, total)
ms = f" {cpu * exc / 100.0:6.2f} ms" if cpu else ""
print(f" {name:28} incl {inc:5.1f}% excl {exc:5.1f}%{ms}")
if __name__ == "__main__":
main()
+194
View File
@@ -0,0 +1,194 @@
// Device-side per-thread CPU sampler for the perf matrix (P14).
//
// Usage (as root, pinned to a little core):
// taskset 01 cpusampler <package> <interval_ms> <watch_file> <out.csv> [max_s]
//
// Every interval it reads /proc/<pid>/task/<tid>/schedstat (on-CPU ns, not the 10 ms tick
// counters of stat) for every thread of every process whose cmdline starts with <package>
// (the replay process) and of every libMobileGLServer.so process (spawn/tcp), and keeps the last
// kRingSeconds of samples in memory. When <watch_file> (benchmark.json) appears it dumps the
// ring to <out.csv> and exits; the host cuts the benchmark's tail window out of it with the
// file's mtime as the window end (make_report.py / cpu_window.py). Timestamps are
// CLOCK_REALTIME so they compare with st_mtim.
#define _GNU_SOURCE
#include <dirent.h>
#include <fcntl.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/stat.h>
#include <time.h>
#include <unistd.h>
#define MAXP 16
#define MAXT 512
#define RING 4096
struct Thread {
int pid, tid;
char comm[24];
};
struct Sample {
long long t; // CLOCK_REALTIME ns
int index; // into threads[]
long long cpu; // schedstat on-CPU ns
};
static struct Thread threads[MAXT];
static int threadCount;
static struct Sample* ring;
static long long ringCap, ringHead, ringSize;
static long long NowNs(void) {
struct timespec ts;
clock_gettime(CLOCK_REALTIME, &ts);
return (long long)ts.tv_sec * 1000000000LL + ts.tv_nsec;
}
static int ReadSmall(const char* path, char* buf, int n) {
int fd = open(path, O_RDONLY);
if (fd < 0) return -1;
int r = (int)read(fd, buf, n - 1);
close(fd);
if (r < 0) return -1;
buf[r] = 0;
return r;
}
// Pseudo-threads: pid -1 carries the Adreno kgsl cumulative busy time and the GPU / CPU clocks,
// so the host can say whether a window was GPU-bound and that the pin held.
static int Pseudo(const char* name) {
for (int i = 0; i < threadCount; ++i)
if (threads[i].pid == -1 && strcmp(threads[i].comm, name) == 0) return i;
if (threadCount >= MAXT) return -1;
threads[threadCount].pid = -1;
threads[threadCount].tid = -1;
snprintf(threads[threadCount].comm, sizeof threads[threadCount].comm, "%s", name);
return threadCount++;
}
static int FindThread(int pid, int tid) {
for (int i = 0; i < threadCount; ++i)
if (threads[i].tid == tid && threads[i].pid == pid) return i;
if (threadCount >= MAXT) return -1;
char path[96], buf[64];
snprintf(path, sizeof path, "/proc/%d/task/%d/comm", pid, tid);
threads[threadCount].pid = pid;
threads[threadCount].tid = tid;
threads[threadCount].comm[0] = 0;
if (ReadSmall(path, buf, sizeof buf) > 0) {
buf[strcspn(buf, "\n,")] = 0;
snprintf(threads[threadCount].comm, sizeof threads[threadCount].comm, "%s", buf);
}
return threadCount++;
}
static int ScanPids(const char* pkg, int* pids) {
int n = 0;
DIR* d = opendir("/proc");
if (!d) return 0;
struct dirent* e;
size_t len = strlen(pkg);
char path[64], buf[256];
while ((e = readdir(d)) && n < MAXP) {
if (e->d_name[0] < '0' || e->d_name[0] > '9') continue;
snprintf(path, sizeof path, "/proc/%s/cmdline", e->d_name);
if (ReadSmall(path, buf, sizeof buf) <= 0) continue;
// The replay process (and its :services), plus the render-server processes the spawn and
// tcp arms exec (their cmdline is the server library's path in the package's install dir).
if ((strncmp(buf, pkg, len) == 0 && (buf[len] == 0 || buf[len] == ':')) ||
(strstr(buf, "libMobileGLServer.so") != NULL && strstr(buf, pkg) != NULL))
pids[n++] = atoi(e->d_name);
}
closedir(d);
return n;
}
static void Push(long long t, int index, long long cpu) {
struct Sample* s = &ring[(ringHead + ringSize) % ringCap];
if (ringSize == ringCap) ringHead = (ringHead + 1) % ringCap;
else ++ringSize;
s->t = t;
s->index = index;
s->cpu = cpu;
}
int main(int argc, char** argv) {
if (argc < 5) {
fprintf(stderr, "usage: cpusampler <package> <interval_ms> <watch_file> <out.csv> [max_s]\n");
return 2;
}
const char* pkg = argv[1];
const int intervalMs = atoi(argv[2]) > 0 ? atoi(argv[2]) : 10;
const char* watch = argv[3];
const char* out = argv[4];
const long long maxNs = (argc > 5 ? atoll(argv[5]) : 1800) * 1000000000LL;
const long long ringSeconds = 30;
ringCap = (ringSeconds * 1000 / intervalMs + 1) * 128;
ring = calloc((size_t)ringCap, sizeof *ring);
if (!ring) return 1;
const long long start = NowNs();
int pids[MAXP], pidCount = 0;
long long iteration = 0;
char path[96], buf[512];
struct stat st;
long long mtime = -1;
for (;;) {
const long long now = NowNs();
if (now - start > maxNs) break;
if (iteration % 25 == 0 || pidCount == 0) pidCount = ScanPids(pkg, pids);
for (int p = 0; p < pidCount; ++p) {
snprintf(path, sizeof path, "/proc/%d/task", pids[p]);
DIR* d = opendir(path);
if (!d) continue;
struct dirent* e;
while ((e = readdir(d))) {
if (e->d_name[0] == '.') continue;
const int tid = atoi(e->d_name);
snprintf(path, sizeof path, "/proc/%d/task/%d/schedstat", pids[p], tid);
if (ReadSmall(path, buf, sizeof buf) <= 0) continue;
const int index = FindThread(pids[p], tid);
if (index >= 0) Push(now, index, atoll(buf));
}
closedir(d);
}
// gpu_clock_stats: cumulative busy time per power level; its sum is busy-since-boot.
// (gpubusy is a sliding ~1 s window, not a counter.)
if (ReadSmall("/sys/class/kgsl/kgsl-3d0/gpu_clock_stats", buf, sizeof buf) > 0) {
long long sum = 0;
for (char* q = buf; *q;) {
char* end = q;
const long long v = strtoll(q, &end, 10);
if (end == q) break;
sum += v;
q = end;
}
Push(now, Pseudo("gpubusy_us"), sum);
}
if (iteration % 10 == 0) {
if (ReadSmall("/sys/class/kgsl/kgsl-3d0/devfreq/cur_freq", buf, sizeof buf) > 0)
Push(now, Pseudo("gpufreq"), atoll(buf));
if (ReadSmall("/sys/devices/system/cpu/cpufreq/policy7/scaling_cur_freq", buf, sizeof buf) > 0)
Push(now, Pseudo("cpu7freq"), atoll(buf));
if (ReadSmall("/sys/devices/system/cpu/cpufreq/policy2/scaling_cur_freq", buf, sizeof buf) > 0)
Push(now, Pseudo("cpu2freq"), atoll(buf));
}
++iteration;
if (stat(watch, &st) == 0 && (long long)st.st_mtim.tv_sec * 1000000000LL + st.st_mtim.tv_nsec > start) {
mtime = (long long)st.st_mtim.tv_sec * 1000000000LL + st.st_mtim.tv_nsec;
break;
}
usleep((useconds_t)intervalMs * 1000);
}
FILE* f = fopen(out, "w");
if (!f) return 1;
fprintf(f, "# watch_mtime_ns=%lld interval_ms=%d\n", mtime, intervalMs);
fprintf(f, "t_ns,pid,tid,comm,cpu_ns\n");
for (long long i = 0; i < ringSize; ++i) {
const struct Sample* s = &ring[(ringHead + i) % ringCap];
const struct Thread* th = &threads[s->index];
fprintf(f, "%lld,%d,%d,%s,%lld\n", s->t, th->pid, th->tid, th->comm, s->cpu);
}
fclose(f);
return mtime >= 0 ? 0 : 3;
}
+83
View File
@@ -0,0 +1,83 @@
#!/usr/bin/env bash
# FCL Minecraft in-world fps for P14: swaps FCL's built-in libMobileGL.so (rooted device), picks
# the backend through FCL's own renderer id and the transport through debug.mobilegl.transport,
# launches FCL (it auto-enters the last world), and reads MobileGL's opt-in fps log
# (debug.mobilegl.fps_log=1, one line about every 2 s) over a steady window.
#
# SER=HA27Q3LQ LIBS="dev=<path> feat=<path>" CONFIGS="dev:DirectGLES feat:DirectGLES:inproc ..." \
# REPS=2 WARMUP=45 WINDOW=30 bash fcl_p14.sh run|restore
#
# The original library is saved once to /data/local/tmp/p14-fcl-orig.so and put back by
# `restore` (md5-checked), along with config.json and the two properties. Clocks are the
# caller's (pin_clocks.sh). FCL can die by itself at start-up (DEBTS.md, SIGRTMIN+2): a run with
# no fps lines is retried once.
set -u
export MSYS_NO_PATHCONV=1 MSYS2_ARG_CONV_EXCL='*'
SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
REPO_ROOT="$(cd "$SCRIPT_DIR/../../.." && pwd)"
SER="${SER:-HA27Q3LQ}"
FCL="${FCL:-com.tungsten.fcl.mgdebug.debug}"
REPS="${REPS:-2}" WARMUP="${WARMUP:-45}" WINDOW="${WINDOW:-30}"
OUT="${PERF_WORK:-$REPO_ROOT/.trace-work/perf-p14}/${TAG:-fcl}"
ESPRYT_ID=5e273ee2-baca-4c81-8e48-b63feefb9ba8
MAGMA_ID=2be0dc10-1eef-4ce2-b512-b266dd33fd9e
mkdir -p "$OUT"
su_sh() { adb -s "$SER" shell "su -c '$1'"; }
libdir() { su_sh "dirname /data/app/*/$FCL-*/lib/arm64/libMobileGL.so" | tr -d '\r' | head -1; }
CFG=/data/data/$FCL/files/config.json
save_original() {
local dir; dir=$(libdir)
# The renderer switch, as a device-side script: sed's own quotes cannot survive two shells.
printf '%s\n' 'sed -i "s/\"renderer\": \"[^\"]*\"/\"renderer\": \"$1\"/" "$2"' >"$OUT/p14-fcl-set.sh"
adb -s "$SER" push "$(cygpath -w "$OUT/p14-fcl-set.sh")" /data/local/tmp/p14-fcl-set.sh >/dev/null
su_sh "test -f /data/local/tmp/p14-fcl-orig.so || cp $dir/libMobileGL.so /data/local/tmp/p14-fcl-orig.so; test -f /data/local/tmp/p14-fcl-config.json || cp $CFG /data/local/tmp/p14-fcl-config.json"
}
install_lib() { # $1 host path
local dir; dir=$(libdir)
adb -s "$SER" push "$(cygpath -w "$1")" /data/local/tmp/p14-fcl-lib.so >/dev/null
# cat into the existing file keeps its owner, mode and SELinux label.
su_sh "cat /data/local/tmp/p14-fcl-lib.so > $dir/libMobileGL.so; rm -f /data/local/tmp/p14-fcl-lib.so"
}
run_one() { # name backend transport rep
local name="$1" backend="$2" transport="$3" rep="$4" dir="$OUT/$1-$2-$3-r$4"
mkdir -p "$dir"
local id=$ESPRYT_ID; [ "$backend" = DirectVulkan ] && id=$MAGMA_ID
su_sh "am force-stop $FCL; sh /data/local/tmp/p14-fcl-set.sh $id $CFG; setprop debug.mobilegl.fps_log 1; setprop debug.mobilegl.transport $transport"
adb -s "$SER" logcat -c
local t0; t0=$(adb -s "$SER" shell dumpsys battery | tr -d '\r' | sed -n 's/.*temperature: \([0-9]*\).*/\1/p')
su_sh "input keyevent KEYCODE_WAKEUP; monkey -p $FCL -c android.intent.category.LAUNCHER 1" >/dev/null 2>&1
sleep "$WARMUP"
local pid; pid=$(adb -s "$SER" shell pidof $FCL | tr -d '\r' | awk '{print $NF}')
adb -s "$SER" shell "su -c 'for t in /proc/$pid/task/*; do echo \$(cat \$t/comm) \$(cut -d\" \" -f1 \$t/schedstat); done'" >"$dir/cpu0.txt" 2>/dev/null
sleep "$WINDOW"
adb -s "$SER" shell "su -c 'for t in /proc/$pid/task/*; do echo \$(cat \$t/comm) \$(cut -d\" \" -f1 \$t/schedstat); done'" >"$dir/cpu1.txt" 2>/dev/null
adb -s "$SER" logcat -d >"$dir/logcat.txt"
su_sh "am force-stop $FCL"
local t1; t1=$(adb -s "$SER" shell dumpsys battery | tr -d '\r' | sed -n 's/.*temperature: \([0-9]*\).*/\1/p')
local lines; lines=$(grep -a "MobileGL fps:" "$dir/logcat.txt" | wc -l)
echo "name=$name backend=$backend transport=$transport rep=$rep pid=$pid temp0=$t0 temp1=$t1 fpslines=$lines" >"$dir/meta.txt"
grep -aE "Active backend type|debug.mobilegl.transport|MOBILEGL_TRANSPORT=inproc|run-ahead ARMED" "$dir/logcat.txt" | head -5 >"$dir/markers.txt"
echo "$(cat "$dir/meta.txt") :: $(grep -a 'MobileGL fps:' "$dir/logcat.txt" | tail -3 | sed 's/.*MobileGL fps: //' | tr '\n' '|')"
[ "$lines" -gt 0 ]
}
case "${1:-run}" in
run)
save_original
for ((r = 1; r <= REPS; r++)); do
for cfg in $CONFIGS; do
IFS=: read -r name backend transport <<<"$cfg"
transport="${transport:-monolith}"
lib=$(echo " $LIBS " | sed -n "s/.* $name=\([^ ]*\) .*/\1/p")
install_lib "$lib"
run_one "$name" "$backend" "$transport" "$r" || { sleep 10; run_one "$name" "$backend" "$transport" "$r" || true; }
sleep "${COOL:-20}"
done
done ;;
restore)
dir=$(libdir)
su_sh "am force-stop $FCL; cat /data/local/tmp/p14-fcl-orig.so > $dir/libMobileGL.so && md5sum $dir/libMobileGL.so; cat /data/local/tmp/p14-fcl-config.json > $CFG; resetprop --delete debug.mobilegl.fps_log; resetprop --delete debug.mobilegl.transport" ;;
esac
+103
View File
@@ -0,0 +1,103 @@
#!/usr/bin/env python3
"""Split one thread of a simpleperf profile by LAYER, in ms per frame.
python layers.py <perf.data> <binary_cache> <thread comm> <cpu_ms_per_frame> [--tail-ms=N]
Each sample goes to the layer of the first frame, walking from the leaf toward the root, that
belongs to one: the GPU driver (vendor GLES/Vulkan libraries, kgsl), the backend (MG_Backend),
the applier (MGPipeApply*, the record sink, the wire decoder), the client half (the frontend,
the validate point and its emitters, the verb port, the wire encoder), or the replay harness
(apitrace parsing and dispatch). Library code with no layer of its own (malloc, memcpy, atomics)
lands in the layer that called it. Kernel frames count as the driver only below a driver frame.
"""
import os
import sys
from collections import Counter
sys.path.insert(0, os.environ.get("SIMPLEPERF_DIR", ""))
from simpleperf_report_lib import ReportLib # noqa: E402
LAYERS = ["driver", "backend", "applier", "client half", "harness", "other"]
def layer_of(symbol, dso):
if any(k in dso for k in ("adreno", "libGLESv2", "libEGL_", "vulkan.", "libgsl", "libllvm-glnext", "msm_kgsl")):
return "driver"
if "MG_Backend::" in symbol:
return "backend"
if ("MGPipeApply" in symbol or "RecordVerbSink" in symbol or "PipeWireDecoder" in symbol
or "PipeApplier::" in symbol or "ServerLoop::" in symbol):
return "applier"
if any(k in symbol for k in ("MG_Pipe::", "MG_Impl::", "MG_Record::", "MG_State::", "MG_Remote::Client",
"PipeWireEncoder", "MG_Util::")) or "libMobileGL" in dso and symbol.startswith("gl"):
return "client half"
if any(k in symbol for k in ("trace::", "retrace", "Brotli", "mobilegl_apitrace", "mobilegl_trace")):
return "harness"
return None
def tail_start(data, symfs, tail_ms):
if not tail_ms:
return 0
lib = ReportLib()
lib.SetRecordFile(data)
lib.SetSymfs(symfs)
last = 0
while True:
sample = lib.GetNextSample()
if sample is None:
break
chain = lib.GetCallChainOfCurrentSample()
if any("retrace::retraceCall" in chain.entries[i].symbol.symbol_name for i in range(chain.nr)):
last = max(last, sample.time)
return last - int(tail_ms * 1e6)
def main():
args = [a for a in sys.argv[1:] if not a.startswith("--")]
tail = next((float(a.split("=", 1)[1]) for a in sys.argv[1:] if a.startswith("--tail-ms=")), 0.0)
data, symfs, comm, cpu = args[0], args[1], args[2], float(args[3])
start = tail_start(data, symfs, tail)
lib = ReportLib()
lib.SetRecordFile(data)
lib.SetSymfs(symfs)
counts = Counter()
detail = Counter()
detail_layer = next((a.split("=", 1)[1] for a in sys.argv[1:] if a.startswith("--detail=")), None)
total = 0
while True:
sample = lib.GetNextSample()
if sample is None:
break
if comm not in sample.thread_comm or sample.time < start:
continue
total += 1
leaf = lib.GetSymbolOfCurrentSample()
frames = [(leaf.symbol_name, leaf.dso_name)]
chain = lib.GetCallChainOfCurrentSample()
frames += [(chain.entries[i].symbol.symbol_name, chain.entries[i].symbol.dso_name) for i in range(chain.nr)]
found = None
owner = None
for symbol, dso in frames:
if "kallsyms" in dso:
continue
found = layer_of(symbol, dso)
if found:
owner = symbol
break
counts[found or "other"] += 1
if detail_layer and found == detail_layer:
# the outermost frame of the same layer below the first non-layer caller: the entry
# into that layer, e.g. MGPipeValidateForVerb or EmitDrawRecord for the client half
names = [f for f, d in frames if layer_of(f, d) == found]
detail[owner.split("(")[0][-90:] + " <- " + names[-1].split("(")[0][-60:]] += 1
print(f"{comm}: {total} samples, {cpu:.2f} ms/frame")
for name in LAYERS:
share = counts[name] / max(1, total)
print(f" {name:12} {100 * share:5.1f}% {cpu * share:6.2f} ms/frame")
for key, n in detail.most_common(30):
print(f" {cpu * n / max(1, total):6.3f} ms {key}")
if __name__ == "__main__":
main()
+198
View File
@@ -0,0 +1,198 @@
#!/usr/bin/env bash
# P14 perf matrix: dev (pull monolith, baseline APK) vs feat (monolith / inproc / spawn+shm /
# spawn+tcp-localhost) x {Espryt, Magma} x {openra, rd12}, on a rooted device with pinned clocks.
#
# SER=HA27Q3LQ PKG_FEAT=top.mobilegl.plugin.p14.trace PKG_DEV=top.mobilegl.plugin.p14dev.trace \
# ARMS="dev monolith inproc shm tcp" BACKENDS="DirectGLES DirectVulkan" WLS="openra rd12" REPS=3 \
# TAG=base bash matrix_p14.sh prep|run|ssim
#
# Per run: force-stop, clear outputs, start cpusampler (per-thread schedstat + kgsl gpubusy, 10 ms)
# watching benchmark.json, launch the replay, wait, pull result/benchmark/logcat/sampler CSV, and
# verify the arm and backend markers. Rounds interleave arms (and backends) so drift spreads evenly.
# Results: $PERF_WORK/<TAG>/<wl>/<backend>/<arm>-r<n>/ ; reduce with p14_report.py.
set -u
export MSYS_NO_PATHCONV=1 MSYS2_ARG_CONV_EXCL='*'
SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
REPO_ROOT="$(cd "$SCRIPT_DIR/../../.." && pwd)"
SER="${SER:-HA27Q3LQ}"
PKG_FEAT="${PKG_FEAT:-top.mobilegl.plugin.p14.trace}"
PKG_DEV="${PKG_DEV:-top.mobilegl.plugin.p14dev.trace}"
ARMS=(${ARMS:-dev monolith inproc shm tcp})
BACKENDS=(${BACKENDS:-DirectGLES DirectVulkan})
WLS=(${WLS:-openra rd12})
REPS="${REPS:-3}"
TAG="${TAG:-base}"
FIXTURES="${FIXTURES:-$REPO_ROOT/tools/trace_replay/fixtures}"
ROOT="${PERF_WORK:-$REPO_ROOT/.trace-work/perf-p14}/$TAG"
TIMEOUT="${TIMEOUT:-900}"
EXTRA_ENV="${EXTRA_ENV:-}"
COOL_C="${COOL_C:-38}" # wait until battery temperature is below this before a run
LOG="$ROOT/matrix.log"
SAMPLER=/data/local/tmp/cpusampler
shq() { adb -s "$SER" shell "$1"; }
log() { local line; line="[$(date '+%F %T')] $*"; mkdir -p "$ROOT"; echo "$line" >>"$LOG"; echo "$line"; }
# An arm is <kind>[@<tag>]: the tag picks another installed build of the trace APK
# (top.mobilegl.plugin.<tag>.trace), so two builds can be interleaved in one matrix.
arm_kind() { echo "${1%@*}"; }
pkg_of() {
case "$1" in
*@*) echo "top.mobilegl.plugin.${1#*@}.trace" ;;
dev) echo "$PKG_DEV" ;;
*) echo "$PKG_FEAT" ;;
esac
}
app_of() { echo "/data/user/0/$1/files/trace-replay"; }
wl_args() { # trace golden w h target crop
case "$1" in
openra) echo "openra openra.0000031249.png 640 480 31249 1 1 638 478" ;;
rd12) echo "rd12 minecraft-1.21.4-rd12-odinlite-in-world.0004660351.png 854 480 4660351 0 0 0 0" ;;
esac
}
arm_env() {
case "$(arm_kind "$1")" in
dev|monolith) echo "" ;;
inproc) echo "MOBILEGL_TRANSPORT=inproc;MOBILEGL_IPC_RUN_AHEAD=1" ;;
shm) echo "MOBILEGL_TRANSPORT=spawn;MOBILEGL_IPC_CONTROL=fork;MOBILEGL_IPC_DATA=auto;MOBILEGL_IPC_RUN_AHEAD=1" ;;
tcp) echo "MOBILEGL_TRANSPORT=spawn;MOBILEGL_IPC_CONTROL=tcp://127.0.0.1:40613;MOBILEGL_IPC_DATA=stream;MOBILEGL_IPC_RUN_AHEAD=1" ;;
esac
}
temp_dc() { adb -s "$SER" shell dumpsys battery 2>/dev/null | tr -d '\r' | sed -n 's/.*temperature: \([0-9]*\).*/\1/p' | head -1; }
cool_down() {
local t; t=$(temp_dc); local waited=0
while [ -n "$t" ] && [ "$t" -ge $((COOL_C * 10)) ] && [ "$waited" -lt 900 ]; do
sleep 15; waited=$((waited + 15)); t=$(temp_dc)
done
[ "$waited" -gt 0 ] && log "cooled ${waited}s to ${t}"
}
prep_pkg() {
local pkg="$1" app; app=$(app_of "$pkg")
local tmp="$ROOT/fixtures"; mkdir -p "$tmp"
for wl in openra rd12; do
local tgz="$FIXTURES/$( [ $wl = openra ] && echo openra.tgz || echo minecraft-1.21.4-rd12-odinlite-in-world.tgz)"
set -- $(wl_args $wl)
local golden="$FIXTURES/$2"
if [ ! -f "$tmp/$wl.trace" ]; then
rm -rf "$tmp/x"; mkdir -p "$tmp/x"; tar xzf "$tgz" -C "$tmp/x" || exit 1
mv "$(find "$tmp/x" -name '*.trace' | head -1)" "$tmp/$wl.trace"; rm -rf "$tmp/x"
fi
adb -s "$SER" push "$(cygpath -w "$tmp/$wl.trace")" /data/local/tmp/p14-$wl.trace >/dev/null || exit 1
adb -s "$SER" push "$(cygpath -w "$golden")" /data/local/tmp/p14-$wl.png >/dev/null || exit 1
shq "am force-stop $pkg; run-as $pkg sh -c 'mkdir -p $app/input-$wl $app/output'"
shq "cat /data/local/tmp/p14-$wl.trace | run-as $pkg sh -c 'cat > $app/input-$wl/trace.trace'"
shq "cat /data/local/tmp/p14-$wl.png | run-as $pkg sh -c 'cat > $app/input-$wl/golden.png'"
local size host; size=$(shq "run-as $pkg sh -c 'wc -c < $app/input-$wl/trace.trace'" | tr -d '\r ')
host=$(wc -c <"$tmp/$wl.trace")
[ "$size" = "$host" ] || { log "FATAL: $pkg $wl size $size != $host"; exit 1; }
log "prep $pkg $wl ok ($size bytes)"
done
shq "rm -f /data/local/tmp/p14-*.trace /data/local/tmp/p14-*.png; chmod 755 $SAMPLER"
}
verify_run() {
local dir="$1" arm="$2" backend="$3" all="$1/markers.txt" v="$1/verify.txt"
grep -aE "Config: MOBILEGL_TRANSPORT|spawn ARMED|control=tcp data=stream|run-ahead ARMED|running lockstep|DISARMED|Active backend type|debug.mobilegl.backend|Fatal\{" \
"$dir/logcat.txt" "$dir"/mgl.*.log 2>/dev/null >"$all"
{
echo "arm=$arm backend=$backend"
grep -aq "Active backend type set to $backend" "$all" && echo "BACKEND=OK" || echo "BACKEND=MISSING"
case "$(arm_kind "$arm")" in
dev|monolith) grep -aqE "Config: MOBILEGL_TRANSPORT=(inproc|spawn)" "$all" && echo "ARM=UNEXPECTED_SPLIT" || echo "ARM=OK(monolith)" ;;
inproc) grep -aq "Config: MOBILEGL_TRANSPORT=inproc - the MGPipe record stream" "$all" && echo "ARM=OK(inproc)" || echo "ARM=MISSING" ;;
shm) grep -aq "spawn ARMED - the server role runs in pid" "$all" && echo "ARM=OK(spawn)" || echo "ARM=MISSING" ;;
tcp) grep -aq "control=tcp data=stream server=" "$all" && echo "ARM=OK(tcp)" || echo "ARM=MISSING" ;;
esac
case "$(arm_kind "$arm")" in inproc|shm|tcp) grep -aq "run-ahead ARMED" "$all" && echo "RUN_AHEAD=OK" || echo "RUN_AHEAD=MISSING" ;; esac
grep -aq "running lockstep" "$all" && echo "LOCKSTEP=PRESENT"
echo "FATAL_LINES=$(grep -ac 'Fatal{' "$all")"
} >"$v"
}
# run_one <wl> <backend> <arm> <rep> <mode:benchmark|ssim>
run_one() {
local wl="$1" backend="$2" arm="$3" rep="$4" mode="$5"
local pkg app dir; pkg=$(pkg_of "$arm"); app=$(app_of "$pkg")
dir="$ROOT/$wl/$backend/$arm-r$rep"; rm -rf "$dir"; mkdir -p "$dir"
set -- $(wl_args "$wl"); local w="$3" h="$4" target="$5" cx="$6" cy="$7" cw="$8" ch="$9"
cool_down
local t0 epoch0 status=""; t0=$(temp_dc); epoch0=$(date +%s)
log "BEGIN $TAG $wl/$backend/$arm r$rep $mode temp=$t0"
shq "am force-stop $PKG_FEAT; am force-stop $PKG_DEV; am force-stop $pkg" >/dev/null 2>&1
shq "run-as $pkg sh -c 'rm -f $app/output/*.json $app/output/*.log $app/output/*.png /data/user/0/$pkg/files/mgl.*.log'" >/dev/null 2>&1
shq "su -c 'pkill -f cpusampler; rm -f /data/local/tmp/p14cpu.csv'" >/dev/null 2>&1
adb -s "$SER" logcat -c >/dev/null 2>&1
if [ "$(arm_kind "$arm")" = tcp ]; then
python "$(cygpath -w "$REPO_ROOT/tools/trace_replay/tcp_device_server.py")" start --serial "$SER" --package "$pkg" \
--backend "$backend" --listen tcp://127.0.0.1:40613 ${EXTRA_SERVER_ENV:+--env "$EXTRA_SERVER_ENV"} >"$dir/server-start.log" 2>&1 || { echo server_start_failed >"$dir/STATUS"; log "FAIL tcp server"; return 1; }
local waited=0
until adb -s "$SER" logcat -d 2>/dev/null | grep -q "listening on tcp://127.0.0.1:40613"; do
sleep 1; waited=$((waited + 1)); [ $waited -ge 30 ] && { echo server_not_listening >"$dir/STATUS"; log "FAIL tcp listen"; return 1; }
done
fi
local passflag=""
[ "$mode" = benchmark ] && passflag="--ez benchmark true --ez benchmark_finish ${FINISH:-false} --es benchmark_result_path $app/output/benchmark.json"
local env; env="$(arm_env "$arm")"; [ -n "$EXTRA_ENV" ] && env="${env:+$env;}$EXTRA_ENV"
if [ "$mode" = benchmark ]; then
shq "su -c 'nohup taskset 01 $SAMPLER $pkg 10 $app/output/benchmark.json /data/local/tmp/p14cpu.csv $TIMEOUT >/dev/null 2>&1 &'" >/dev/null 2>&1
fi
local am_out
am_out=$(shq "am start -a top.mobilegl.plugin.TRACE_REPLAY -n $pkg/top.mobilegl.plugin.trace.TraceReplayActivity --es trace_path $app/input-$wl/trace.trace --es golden_path $app/input-$wl/golden.png --es output_dir $app/output --es backend $backend --el target_call $target --ei width $w --ei height $h --es ssim_threshold 0.99 --ez use_pbuffer true $passflag --ei crop_x $cx --ei crop_y $cy --ei crop_width $cw --ei crop_height $ch --es mobilegl_env '$env'" 2>&1)
printf '%s\n' "$am_out" >"$dir/am-start.txt"
local deadline=$((epoch0 + TIMEOUT)) got=0
while [ "$(date +%s)" -lt "$deadline" ]; do
if adb -s "$SER" exec-out "run-as $pkg cat $app/output/result.json" 2>/dev/null | head -c 400 | grep -q '"passed"'; then got=1; break; fi
if [ $(( $(date +%s) - epoch0 )) -gt 25 ] && [ -z "$(shq "pidof $pkg" 2>/dev/null | tr -d '\r ')" ]; then status=process_died; break; fi
sleep 2
done
[ $got -eq 1 ] && { status=finished; sleep 2; } || [ -n "$status" ] || status=timeout
local t1; t1=$(temp_dc)
adb -s "$SER" logcat -d >"$dir/logcat.txt" 2>/dev/null
for f in result.json benchmark.json; do
adb -s "$SER" exec-out "run-as $pkg cat $app/output/$f" >"$dir/$f" 2>/dev/null
{ [ -s "$dir/$f" ] && ! head -c 200 "$dir/$f" | grep -q "No such file"; } || rm -f "$dir/$f"
done
if [ "$(arm_kind "$arm")" = tcp ]; then
for f in mgl.client.log mgl.server.log; do adb -s "$SER" exec-out "run-as $pkg cat /data/user/0/$pkg/files/$f" >"$dir/$f" 2>/dev/null; done
python "$(cygpath -w "$REPO_ROOT/tools/trace_replay/tcp_device_server.py")" stop --serial "$SER" --package "$pkg" >/dev/null 2>&1
fi
if [ "$mode" = benchmark ]; then
sleep 1
adb -s "$SER" exec-out "su -c 'cat /data/local/tmp/p14cpu.csv'" >"$dir/cpu.csv" 2>/dev/null
shq "su -c 'pkill -f cpusampler'" >/dev/null 2>&1
fi
shq "am force-stop $pkg" >/dev/null 2>&1
local passed="none"; [ -f "$dir/result.json" ] && passed=$(sed -n 's/.*"passed": *\([a-z]*\).*/\1/p' "$dir/result.json" | head -1)
echo "$status" >"$dir/STATUS"
echo "tag=$TAG wl=$wl backend=$backend arm=$arm rep=$rep mode=$mode status=$status passed=$passed dur=$(( $(date +%s) - epoch0 )) temp0=$t0 temp1=$t1" >"$dir/meta.txt"
verify_run "$dir" "$arm" "$backend"
local fps="-"; [ -f "$dir/benchmark.json" ] && fps=$(sed -n 's/.*"fps": *\([0-9.]*\).*/\1/p' "$dir/benchmark.json" | head -1)
log "END $TAG $wl/$backend/$arm r$rep $mode status=$status passed=$passed fps=$fps temp=$t1 :: $(tr '\n' ' ' <"$dir/verify.txt")"
}
main() {
log "=== matrix_p14 $1 tag=$TAG arms=${ARMS[*]} backends=${BACKENDS[*]} wls=${WLS[*]} reps=$REPS ==="
shq "input keyevent KEYCODE_WAKEUP; svc power stayon usb" >/dev/null 2>&1
case "$1" in
prep) for p in ${PREP_PKGS:-$PKG_FEAT $PKG_DEV}; do prep_pkg "$p"; done ;;
run)
for wl in "${WLS[@]}"; do
for ((r = 1; r <= REPS; r++)); do
for backend in "${BACKENDS[@]}"; do
for arm in "${ARMS[@]}"; do run_one "$wl" "$backend" "$arm" "$r" benchmark || true; done
done
done
done ;;
ssim)
for wl in "${WLS[@]}"; do for backend in "${BACKENDS[@]}"; do for arm in "${ARMS[@]}"; do
run_one "$wl" "$backend" "$arm" ssim ssim || true
done; done; done ;;
esac
log "=== done ==="
}
main "${1:-run}"
+190
View File
@@ -0,0 +1,190 @@
#!/usr/bin/env python3
"""Reduce matrix_p14.sh results: steady-state fps, per-thread CPU ms/frame, GPU busy share.
python p14_report.py <results_dir> [--md out.md] [--json out.json]
Steady state drops the first SKIP[wl] frames (the load frames: openra's 270/736 ms set-up
frames, rd12's single multi-second upload frame plus three) and averages the rest. The CPU window is the steady frames'
wall span ending at benchmark.json's mtime (cpusampler.c records that mtime and per-thread
schedstat on-CPU ns every 10 ms); per-thread CPU is interpolated at the window edges and divided
by the steady frame count. GPU busy = delta(kgsl gpu_clock_stats sum) / window.
"""
import argparse
import bisect
import json
import re
import statistics
import sys
from collections import defaultdict
from pathlib import Path
SKIP = {"openra": 8, "rd12": 4}
def load_cpu(path):
meta = {}
series = defaultdict(list) # (pid, tid, comm) -> [(t, v)]
if not path.exists():
return meta, series
with path.open(encoding="utf-8", errors="replace") as f:
for line in f:
if line.startswith("#"):
for k, v in re.findall(r"(\w+)=(-?\d+)", line):
meta[k] = int(v)
continue
if line.startswith("t_ns"):
continue
parts = line.rstrip("\n").split(",")
if len(parts) != 5:
continue
t, pid, tid, comm, v = parts
series[(int(pid), int(tid), comm)].append((int(t), int(v)))
return meta, series
def at(points, t):
ts = [p[0] for p in points]
i = bisect.bisect_left(ts, t)
if i <= 0:
return points[0][1] if points and points[0][0] - t < 50_000_000 else None
if i >= len(points):
return points[-1][1] if t - points[-1][0] < 50_000_000 else None
(t0, v0), (t1, v1) = points[i - 1], points[i]
return v0 + (v1 - v0) * (t - t0) / max(1, t1 - t0)
def window_delta(points, start, end):
"""On-CPU ns a counter series gained inside [start, end]: the sum of its positive steps, each
weighted by how much of its interval lies in the window. Robust to a counter that restarts
(a thread id reused by a new thread under the same name reads as a drop, which is skipped)."""
if not points or points[0][0] > start + 250_000_000 or points[-1][0] < end - 250_000_000:
return None
total = 0.0
for (t0, v0), (t1, v1) in zip(points, points[1:]):
if t1 <= start or t0 >= end or v1 <= v0:
continue
overlap = min(t1, end) - max(t0, start)
total += (v1 - v0) * overlap / max(1, t1 - t0)
return total
def reduce_run(run_dir, wl):
bench_path = run_dir / "benchmark.json"
if not bench_path.exists():
return None
bench = json.loads(bench_path.read_text(encoding="utf-8", errors="replace"))
frames = bench.get("frameTimesMs") or []
skip = SKIP.get(wl, 0)
steady = frames[skip:]
if not steady:
return None
out = {
"frames": len(frames),
"steady_frames": len(steady),
"steady_mean_ms": statistics.mean(steady),
"steady_median_ms": statistics.median(steady),
"steady_p95_ms": sorted(steady)[max(0, int(len(steady) * 0.95) - 1)],
"harness_fps": bench.get("fps"),
}
out["steady_fps"] = 1000.0 / out["steady_mean_ms"]
meta, series = load_cpu(run_dir / "cpu.csv")
end = meta.get("watch_mtime_ns", -1)
if end > 0 and series:
span = sum(steady) * 1e6
start = end - span
threads = []
procs = defaultdict(float)
for (pid, tid, comm), pts in series.items():
if pid < 0:
continue
used = window_delta(pts, start, end)
if used is None:
continue
ms = used / 1e6 / len(steady)
if ms <= 0.005:
continue
threads.append({"pid": pid, "tid": tid, "comm": comm, "ms_per_frame": ms,
"util": used / span})
procs[pid] += ms
threads.sort(key=lambda r: -r["ms_per_frame"])
out["threads"] = threads[:8]
out["cpu_ms_per_frame_total"] = sum(t["ms_per_frame"] for t in threads)
out["process_ms_per_frame"] = dict(procs)
gpu = [v for k, v in series.items() if k[2] == "gpubusy_us"]
if gpu:
a, b = at(gpu[0], start), at(gpu[0], end)
if a is not None and b is not None:
out["gpu_busy"] = (b - a) * 1e3 / span
for name in ("gpufreq", "cpu7freq", "cpu2freq"):
pts = [v for k, v in series.items() if k[2] == name]
if pts:
vals = [v for t, v in pts[0] if start <= t <= end]
if vals:
out[name] = (min(vals), max(vals))
return out
def main():
ap = argparse.ArgumentParser()
ap.add_argument("root", type=Path)
ap.add_argument("--md", type=Path)
ap.add_argument("--json", type=Path)
ap.add_argument("--threads", action="store_true", help="print per-run top threads")
args = ap.parse_args()
runs = defaultdict(list)
for bench in sorted(args.root.glob("*/*/*-r*/benchmark.json")):
run_dir = bench.parent
wl, backend = run_dir.parent.parent.name, run_dir.parent.name
m = re.fullmatch(r"(.+)-r(\d+)", run_dir.name)
if not m:
continue
verify = (run_dir / "verify.txt").read_text() if (run_dir / "verify.txt").exists() else ""
if "MISSING" in verify or "UNEXPECTED" in verify:
print(f"skip {run_dir}: {verify.strip()}", file=sys.stderr)
continue
r = reduce_run(run_dir, wl)
if r:
r["rep"] = int(m.group(2))
r["dir"] = str(run_dir)
runs[(wl, backend, m.group(1))].append(r)
if args.threads:
tops = ", ".join(f"{t['comm']}({t['pid']})={t['ms_per_frame']:.2f}" for t in r.get("threads", [])[:5])
print(f"{wl:7} {backend:12} {m.group(1):9} r{r['rep']} fps={r['steady_fps']:.1f} "
f"cpu={r.get('cpu_ms_per_frame_total', float('nan')):.2f} gpu={r.get('gpu_busy', float('nan')):.2f} :: {tops}")
order = {"dev": 0, "monolith": 1, "inproc": 2, "shm": 3, "tcp": 4}
rows = []
for (wl, backend, arm), rs in sorted(runs.items(), key=lambda kv: (kv[0][0], kv[0][1], order.get(kv[0][2], 9), kv[0][2])):
fps = sorted(r["steady_fps"] for r in rs)
best = max(rs, key=lambda r: r["steady_fps"])
med = statistics.median(fps)
main_thread = best.get("threads", [{}])[0] if best.get("threads") else {}
rows.append({
"wl": wl, "backend": backend, "arm": arm, "n": len(rs),
"best_fps": fps[-1], "median_fps": med,
"spread_pct": 100.0 * (fps[-1] - fps[0]) / med if len(fps) > 1 else 0.0,
"best_mean_ms": best["steady_mean_ms"], "best_median_ms": best["steady_median_ms"],
"cpu_total": best.get("cpu_ms_per_frame_total"),
"top_thread": f"{main_thread.get('comm', '?')} {main_thread.get('ms_per_frame', float('nan')):.2f}" if main_thread else "-",
"top_util": main_thread.get("util"),
"second_thread": (f"{best['threads'][1]['comm']} {best['threads'][1]['ms_per_frame']:.2f}"
if len(best.get("threads", [])) > 1 else "-"),
"gpu_busy": best.get("gpu_busy"),
"procs": best.get("process_ms_per_frame"),
})
lines = ["| workload | backend | arm | n | best fps | median fps | spread | mean ms | CPU ms/frame (all threads) | top thread ms/frame (util) | 2nd thread | GPU busy |",
"|---|---|---|---|---|---|---|---|---|---|---|---|"]
fmt = lambda v, f="{:.1f}": "-" if v is None else f.format(v)
for r in rows:
lines.append(f"| {r['wl']} | {r['backend']} | {r['arm']} | {r['n']} | {r['best_fps']:.1f} | {r['median_fps']:.1f} | "
f"{r['spread_pct']:.1f}% | {r['best_mean_ms']:.2f} | {fmt(r['cpu_total'], '{:.2f}')} | {r['top_thread']} "
f"({fmt(r['top_util'] and 100 * r['top_util'], '{:.0f}')}%) | {r['second_thread']} | {fmt(r['gpu_busy'] and 100 * r['gpu_busy'], '{:.0f}')}% |")
text = "\n".join(lines)
print(text)
if args.md:
args.md.write_text(text + "\n", encoding="utf-8")
if args.json:
args.json.write_text(json.dumps({"rows": rows, "runs": {"/".join(k): v for k, v in runs.items()}}, indent=1), encoding="utf-8")
if __name__ == "__main__":
main()
+29
View File
@@ -0,0 +1,29 @@
#!/usr/bin/env bash
# Pin / restore CPU and GPU clocks on a rooted bench device (P14).
# SER=<serial> bash pin_clocks.sh pin|restore|show
# pin: every cpufreq policy gets scaling_min = scaling_max = its PIN_<policy> value (default:
# the highest available frequency <= PIN_CPU_KHZ, 2.0 GHz), the Adreno devfreq min = max =
# PIN_GPU_HZ. The pre-pin values are saved on the device (/data/local/tmp/p14-clocks.saved)
# and restore puts them back. Never pin a hot device: check the battery temperature first.
set -u
export MSYS_NO_PATHCONV=1
SER="${SER:-HA27Q3LQ}"
PIN_CPU_KHZ="${PIN_CPU_KHZ:-2035200}"
PIN_LITTLE_KHZ="${PIN_LITTLE_KHZ:-1574400}"
PIN_GPU_HZ="${PIN_GPU_HZ:-903000000}"
KGSL=/sys/class/kgsl/kgsl-3d0/devfreq
SAVED=/data/local/tmp/p14-clocks.saved
su_sh() { adb -s "$SER" shell "su -c '$1'"; }
case "${1:-show}" in
pin)
su_sh "test -f $SAVED || { for p in /sys/devices/system/cpu/cpufreq/policy*; do echo \$p \$(cat \$p/scaling_min_freq) \$(cat \$p/scaling_max_freq); done; echo $KGSL \$(cat $KGSL/min_freq) \$(cat $KGSL/max_freq); } > $SAVED"
su_sh "for p in /sys/devices/system/cpu/cpufreq/policy*; do want=$PIN_CPU_KHZ; [ \$p = /sys/devices/system/cpu/cpufreq/policy0 ] && want=$PIN_LITTLE_KHZ; f=0; for a in \$(cat \$p/scaling_available_frequencies); do [ \$a -le \$want ] && f=\$a; done; echo \$f > \$p/scaling_max_freq; echo \$f > \$p/scaling_min_freq; echo \$f > \$p/scaling_max_freq; done; echo $PIN_GPU_HZ > $KGSL/max_freq; echo $PIN_GPU_HZ > $KGSL/min_freq; echo $PIN_GPU_HZ > $KGSL/max_freq"
"$0" show ;;
restore)
su_sh "test -f $SAVED && while read p mn mx; do if [ \$p = $KGSL ]; then echo \$mx > \$p/max_freq; echo \$mn > \$p/min_freq; else echo \$mx > \$p/scaling_max_freq; echo \$mn > \$p/scaling_min_freq; fi; done < $SAVED; rm -f $SAVED"
"$0" show ;;
show)
su_sh "for p in /sys/devices/system/cpu/cpufreq/policy*; do echo \$p cur=\$(cat \$p/scaling_cur_freq) min=\$(cat \$p/scaling_min_freq) max=\$(cat \$p/scaling_max_freq); done; echo gpu cur=\$(cat $KGSL/cur_freq) min=\$(cat $KGSL/min_freq) max=\$(cat $KGSL/max_freq); dumpsys battery | grep temperature" ;;
esac
+55
View File
@@ -0,0 +1,55 @@
#!/usr/bin/env bash
# One simpleperf profile of a matrix_p14 arm.
# ARM=monolith BACKEND=DirectVulkan WL=rd12 TAG=prof SYMDIR=<unstripped libs> bash profile_p14.sh
# Records the whole run (every thread of the app's processes, the spawn/tcp render server
# included, via --app) with frame-pointer call graphs, stops when benchmark.json appears, and
# builds a binary cache against the unstripped libraries in SYMDIR. The steady window is cut out
# afterwards: components.py / callee_split.py take --tail-ms (the steady frames' wall span from
# benchmark.json), so no launch-to-steady delay has to be guessed. Clocks are expected to be
# pinned by the caller (pin_clocks.sh).
set -u
export MSYS_NO_PATHCONV=1 MSYS2_ARG_CONV_EXCL='*'
SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
REPO_ROOT="$(cd "$SCRIPT_DIR/../../.." && pwd)"
SER="${SER:-HA27Q3LQ}"
ARM="${ARM:-monolith}" BACKEND="${BACKEND:-DirectVulkan}" WL="${WL:-rd12}" TAG="${TAG:-prof}"
FREQ="${FREQ:-2000}"
PKG_FEAT="${PKG_FEAT:-top.mobilegl.plugin.p14.trace}" PKG_DEV="${PKG_DEV:-top.mobilegl.plugin.p14dev.trace}"
SYMDIR="${SYMDIR:?unstripped library directory}"
NDK="${NDK:-/c/Users/yello/AppData/Local/Android/Sdk/ndk/27.3.13750724}"
OUT="${PERF_WORK:-$REPO_ROOT/.trace-work/perf-p14}/$TAG/$WL-$BACKEND-$ARM"
mkdir -p "$OUT"
pkg=$([ "$ARM" = dev ] && echo "$PKG_DEV" || echo "$PKG_FEAT")
app=/data/user/0/$pkg/files/trace-replay
case "$ARM" in
dev|monolith) env="" ;;
inproc) env="MOBILEGL_TRANSPORT=inproc;MOBILEGL_IPC_RUN_AHEAD=1" ;;
shm) env="MOBILEGL_TRANSPORT=spawn;MOBILEGL_IPC_CONTROL=fork;MOBILEGL_IPC_DATA=auto;MOBILEGL_IPC_RUN_AHEAD=1" ;;
esac
[ -n "${EXTRA_ENV:-}" ] && env="${env:+$env;}$EXTRA_ENV"
case "$WL" in
openra) args="--el target_call 31249 --ei width 640 --ei height 480" ;;
rd12) args="--el target_call 4660351 --ei width 854 --ei height 480" ;;
esac
adb -s "$SER" shell "am force-stop $PKG_FEAT; am force-stop $PKG_DEV; run-as $pkg sh -c 'rm -f $app/output/*.json'" >/dev/null 2>&1
adb -s "$SER" shell "su -c 'pkill -INT simpleperf; rm -f /data/local/tmp/p14perf.data'" >/dev/null 2>&1
# --app follows the package's processes and their new threads from the moment they exist.
adb -s "$SER" shell "su -c 'simpleperf record --app $pkg -e cpu-clock --call-graph fp -f $FREQ -o /data/local/tmp/p14perf.data'" >"$OUT/record.log" 2>&1 &
sp_pid=$!
sleep 2
adb -s "$SER" shell "am start -a top.mobilegl.plugin.TRACE_REPLAY -n $pkg/top.mobilegl.plugin.trace.TraceReplayActivity --es trace_path $app/input-$WL/trace.trace --es golden_path $app/input-$WL/golden.png --es output_dir $app/output --es backend $BACKEND $args --ez use_pbuffer true --ez benchmark true --ez benchmark_finish ${FINISH:-false} --es benchmark_result_path $app/output/benchmark.json --es mobilegl_env '$env'" >/dev/null
waited=0
until adb -s "$SER" exec-out "run-as $pkg cat $app/output/benchmark.json" 2>/dev/null | grep -q '"fps"'; do
sleep 1; waited=$((waited + 1)); [ "$waited" -ge 600 ] && break
done
adb -s "$SER" shell "su -c 'pkill -INT simpleperf'" >/dev/null 2>&1
wait "$sp_pid"
adb -s "$SER" exec-out "run-as $pkg cat $app/output/benchmark.json" >"$OUT/benchmark.json" 2>/dev/null
adb -s "$SER" shell "am force-stop $pkg" >/dev/null 2>&1
adb -s "$SER" shell "su -c 'chmod 644 /data/local/tmp/p14perf.data'"
adb -s "$SER" pull /data/local/tmp/p14perf.data "$(cygpath -w "$OUT/perf.data")" >/dev/null
W_SYM=$(cygpath -w "$SYMDIR")
# binary_cache/ mirrors the device paths: our unstripped libraries by build id, the rest pulled.
(cd "$OUT" && ANDROID_SERIAL="$SER" python "$(cygpath -w "$NDK/simpleperf/binary_cache_builder.py")" -i perf.data -lib "$W_SYM" >binary_cache.log 2>&1)
tail -1 "$OUT/record.log"
echo "wrote $OUT (steady tail: $(python -c "import json,sys; f=json.load(open(sys.argv[1]))['frameTimesMs']; print(round(sum(f[4:]),1))" "$(cygpath -w "$OUT/benchmark.json")") ms)"