Repository navigation
Conversation
RenderGdprBatches zero-filled the per-object constant slots of an indirect draw only when a batch had no object data at all. A batch whose object data writes some stages but not others (for example vertex-shader data only, drawn with an effect that also reads pixel-shader per-object data) left the other slot holding whatever an earlier draw wrote there: indirect draw arguments are reused between draws without being cleared. On Metal, Tr2IndirectDrawBuffer::Submit then passed that stale value to ConstantBufferAllocator::GetPage as a page index and crashed. The indirect draw writer now records which per-object stages the current command wrote, and RenderGdprBatches zero-fills every stage the layout declares but the batch left unwritten, logging the effect as before. ConstantBufferAllocator::GetPage asserts and returns nil for an out-of-range page instead of reading past its page table.
|
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
On Metal, the GPU-driven batch path (
RenderGdprBatcheswithg_gdrEnabled) crashes when an effect reads pixel-shader per-object data that the object's C++ per-object data does not provide. The crash is inConstantBufferAllocator::GetPage, called fromTr2IndirectDrawBuffer::Submit, with a garbage page index.RenderGdprBatchesalready zero-fills the per-object slots when a batch has no object data at all. It does not do so when the object data writes some stages but not the one the effect reads. This PR closes that gap:Tr2IndirectDrawBufferWriterrecords which per-object stages the current command wrote.RenderGdprBatcheszero-fills every per-object stage the layout declares but the batch left unwritten, and logs the effect as before.ConstantBufferAllocator::GetPagenow asserts on an out-of-range page and returnsnil, instead of reading past its page table.Minimal repro (by construction)
Tr2Renderer::GetPerObjectPSStartRegister()).Tr2PerObjectData::ApplyConstantBufferswrites onlyVERTEX_SHADER.perObject[1]is never written for that command, andSubmitbinds whatever value was already there.Mechanism
All line numbers are at
bf116baa.Tr2IndirectDrawBufferLayoutsetsm_hasPerObjectData[1]from the pixel-shader program's constant-buffer mask (trinity/Tr2IndirectDrawBuffer.cpp:52).SetPerObjectData( stage, ... )writesm_buffer->perObject[stage](Tr2IndirectDrawBuffer.cpp:97,:112). SeveralTr2PerObjectData::ApplyConstantBuffersoverrides write only the vertex-shader slot, or vertex and geometry.RenderGdprBatches(trinity/Tr2RenderContext.cpp:693-709), the zero-fill branch is anelseofif( batch.m_objectData ). A batch that has object data, but not for the pixel-shader stage, skips it.DPstructs allocated withnew DP[m_pageSize], which default-initialises them (Tr2IndirectDrawBuffer.cpp:256).SetFrameNumbersreuses them from offset 0 every frame (:382-383). An unwritten slot therefore holds an earlier draw's value or indeterminate memory.layout.m_hasPerObjectData[1]is set,Submitunpacksdp.perObject[1]as(page << 32) | offset(Tr2IndirectDrawBuffer.cpp:458). It passespagetoConstantBufferAllocator::GetPage(trinityal/metal/MetalUtils.mm:424-427), which indexesm_pages[page]unchecked.EXC_BAD_ACCESSinGetPagewith page indices such as0x55e4b002, or in the ARC retain of the pointer read from past the table.On DX12 the same unwritten slot holds a stale root CBV address. The existing comment there says that hangs the GPU, so the fix is in the shared draw path rather than in the Metal backend.
Fix
Tr2IndirectDrawBuffer.h/.cpp:m_writtenPerObjectDatastage bitmask. BothSetPerObjectDataoverloads set it, andNext()clears it.IsPerObjectDataWritten( stage )method reads it.Tr2RenderContext.cpp: afterApplyConstantBuffers,RenderGdprBatcheschecksVERTEX_SHADERandPIXEL_SHADER. Any stage thatHasPerObjectDatabut was not written gets the existing 4 KB zero block.elsebranch. A batch with no object data takes the same path, with the same result as before.MetalUtils.mm:GetPagedoesCCP_ASSERT( page < MAX_PAGE_COUNT )and returnsnilwhen out of range. Metal accepts anilbuffer binding, andnil.gpuAddressis 0. Any future slot bug becomes a wrong draw instead of a crash.Cost: one 4 KB zero upload per offending batch and stage, per frame. That is what the existing no-object-data path already pays. Well-formed batches pay one bit-or per
SetPerObjectData.Why not clear the indirect args instead? Zeroing a slot means page 0, offset 0 on Metal (someone else's constants) and a null GPU address on DX12. Writing a real zero block gives defined data on both backends, and it reuses the existing zero-fill.
Linked issue (optional)
Testing
Run on an Apple Silicon Mac, Apple clang 21.0.0, macOS SDK 27.2.
trinity/translation units (Tr2IndirectDrawBuffer.cpp,Tr2RenderContext.cpp) need Python, Blue, granny and cmf headers, so they were not compiled at all. The writer tracking and the zero-fill loop are reviewed by reading only, and need a real build to confirm.trinityal/metal/MetalUtils.mm, with-fobjc-arc -std=c++17 -DTRINITY_PLATFORM=TRINITY_METAL, against carbon-core and imageio headers that may not match the versions vcpkg pins.GetPagered/green, out-of-repo harness: the realMetalUtils.mmis linked to a small driver on a realMTLDevice. The driver allocates once, checksGetPageof that page is non-nil, then callsGetPage( 0x55e4b002 ).nil, exit 0, with asserts disabled and enabled. With asserts enabled, the assert fires once.trinityal/testshas no Metal test target. Happy to add one if you want it.Questions for the Trinity team
ApplyConstantBuffersoverride write every stage the layout declares?Tr2IndirectDrawBufferLayout::m_hasMaterialData/m_hasPerObjectDataandDP::material/DP::perObjecthave 2 entries.ShaderTypehas 6.Tr2Material::ApplyConstantBuffersloops over all 6 stages and callsHasMaterialConstants( i ), and some per-object types callSetPerObjectData( GEOMETRY_SHADER, ... ). On Metal those index past the 2-entry arrays. Should the Metal side clamp to vertex and pixel? Not changed here.ConstantBufferAllocator::Allocate(MetalUtils.mm:390-422) moves to the next page without checkingMAX_PAGE_COUNT. Past 32 pages (64 MB before a reset) it writes throughm_pageContents[32], which aliasesm_pages[0], and corrupts thatMTLBufferobject. We did not hit it here (about 0.3 to 0.6 MB per frame), but an out-of-repo harness corrupts the heap at 96 MB. Is 64 MB per frame a deliberate cap? Would you like a separate PR that guards it?MetalWorkQueue::BlitToDrawableAndPresentwaits onm_frameSemaphore(count 2,MetalWorkQueue.mm:97/:557). It returnsfalseat:585(no drawable) and:617(pipeline creation failed) without signalling it. The only signal is in the completion handler at:666. If nothing else rebalances it, two failed presents would block the next present forever. Is that right?🤖 Generated with Claude Code