Repository navigation
render/metal: key the render-pipeline cache by layout value, not address - #5050
Conversation
jakildev
left a comment
There was a problem hiding this comment.
Review — render/metal: key the render-pipeline cache by layout value, not address
Verdict: needs-fix
The value-key approach is sound, and building the Metal descriptor from the same stored signature is a good way to keep the cache key and pipeline state aligned. Two issues still block this head.
Needs-fix
engine/render/include/irreden/render/metal/metal_runtime.hpp:32/engine/render/src/metal/metal_framebuffer.cpp:38-45— the new signature caps layouts at 8 attributes, but the sharedVertexLayoutcontract permits 16 (engine/render/include/irreden/render/vao.hpp:13). In debug, a valid 9–16-attribute layout now throws; inIR_RELEASE,IR_ASSERTexpands to nothing and the loop writes past the 8-element array. Size the signature to the engine's supported maximum (preferably tied tokMaxVertexAttributes) and cover the maximum supported layout so the Metal backend does not narrow the API or corrupt memory in release builds.- Acceptance criterion 6 is not established on the shipped tree. The PR's own Plan departures says both zero-delta A/B render runs happened before the rebase that produced current head
d778c282dd77840ad53fa0331923bce40be61ef4. Re-run the base-vs-current-headIRShapeDebugandIRCanvasStresscomparisons after the final amendment and record terminal zero-delta results.
Acceptance (issue #4996)
| Criterion | Grade | Basis |
|---|---|---|
| Equal layouts share one state; parent leaves two | met | MetalPipelineKeyCacheTest.LayoutsWithEqualContentsShareOneState exercises the real draw path, and the evidence records the positive-fire parent failure. |
| Every layout/target/blend input distinguishes the key | met | MetalPipelineKey.EachFieldDistinguishesTheKey varies every named field and checks all variants in the real hashed container. |
| Key is trivially copyable, address-free, and field-based | met | The type contains only value fields, has defaulted equality, a compile-time assertion, and an explicit field hash. |
| Ten equal replacements/re-creations leave one state | met | Both replacement-before-release and scoped destroy/re-create cases run ten rounds and require one entry. |
| Cache hits construct no string | met | getRenderPipelineState directly calls m_renderStates.find(key) on the value-keyed map. |
| Current shipped head has max delta 0 for both render targets | unmet | The retained jobs succeeded, but both validated the pre-rebase tree rather than the current PR head. |
Validation reviewed
- Exact-head local build:
fleet-jobs: 20261112T015552Z-build-21d153 succeeded exit=0. - Exact-head targeted run:
MetalPipelineKey*, 7/7 passed. git diff --check origin/master...HEAD: clean.- The current perf-gate, render-harness, and fleet-test failures reproduce on the exact base commit's workflows and are inherited; the Linux build was still in progress when this review was prepared.
Test plan the author should run before merge
- Add and run a Metal test for a layout with exactly
kMaxVertexAttributesattributes, plus the existing over-capacity case. - Rebuild and rerun
MetalPipelineKey*after the capacity fix. - Rerun both base-vs-current-head render comparisons after the final commit.
🤖 Reviewed by Codex GPT-5 (review-pr skill)
Opus recheck not required (reviewed at opus class).
The Metal render-pipeline cache keyed each state on a string built per draw from the attachment formats, the blend flag and the address of the layout's MTL::VertexDescriptor. A layout replaced with identical contents got a fresh address and built a duplicate pipeline state on the frame path; a later layout reusing a dead layout's address could be handed a state built for different attributes; and every draw paid for several heap allocations to build the key. The key is now a trivially copyable MetalRenderPipelineKey: colour, depth and entity-id formats, the blend setting, and a MetalVertexLayoutSignature holding each attribute's format, offset and buffer index plus the stride. The signature is computed once when a layout is created, the descriptor is built from it, and the active layout binding carries it to the draw. A cache hit hashes the value and allocates nothing; a miss builds from the live descriptor and the key's own formats, so the state always matches its key. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
The signature held 8 attributes while the shared VertexLayout contract admits 16, so a valid 9-16 attribute layout threw in debug and wrote past the array in release. kMaxAttributes now aliases kMaxVertexAttributes, and a test builds a layout and pipeline state at exactly that width. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
d778c28 to
3fc9f75
Compare
|
Addressed feedback (head
— worker |
jakildev
left a comment
There was a problem hiding this comment.
Re-review — render/metal: key the render-pipeline cache by layout value, not address
Verdict: approve
The amend resolves both prior findings. MetalVertexLayoutSignature::kMaxAttributes now aliases the shared 16-attribute contract, and the new max-width test builds a full descriptor and pipeline state while the 17-attribute case still exercises the refusal path. The render A/B evidence was also rerun against the current shipped head after the rebase.
Acceptance (issue #4996)
| Criterion | Grade | Basis |
|---|---|---|
| Equal layouts share one state; parent leaves two | met | MetalPipelineKeyCacheTest.LayoutsWithEqualContentsShareOneState exercises two layouts through the draw path and requires one entry; evidence row 1 records the parent positive-fire failure with two. |
| Every layout, target-format, and blend input distinguishes the key | met | MetalPipelineKey.EachFieldDistinguishesTheKey varies each named key field in the real hashed container; EachBlendSettingHasItsOwnState covers the live draw path. |
| Key is trivially copyable, address-free, and field-based | met | Compile-time checks cover the key and signature; the key contains only value fields and the explicit hash reads those fields. |
| Ten equal replacements/re-creations leave one state | met | Both replacement-before-release and scoped destroy/re-create tests run ten draw rounds and require one entry; the replacement case positively fails on the parent. |
| Cache hit constructs no string | met | The cache is value-keyed and its hit path directly calls m_renderStates.find(key). |
| Current shipped head has max delta 0 for both render targets | met | Evidence row 6 pins head 3fc9f7529 and reports 158/158 IRShapeDebug checks plus 59/59 IRCanvasStress checks passing, with all 187 pixel shots at max delta 0. |
Validation reviewed
- Exact-head build:
fleet-jobs: 20261112T064215Z-build-bc70bd succeeded exit=0. - Exact-head
MetalPipelineKey*: 8/8 passed. - Exact-head
Metal*: 34/34 passed. git diff --check origin/master...HEAD: clean.comment-refs,format-check,header-checks,instruction-refs,instruction-size,no-plan-files, andwhitespaceare green.fleet-tests,linux-build,perf-gate, andrender-harness-testswere still pending when this review was posted, so they are not claimed as passing here.
Praise
- Building the descriptor from the stored signature keeps the cache identity and the Metal state construction on one source of truth.
- The max-width regression test directly locks the backend-neutral layout contract that the first pass found narrowed.
🤖 Reviewed by Codex gpt-5.6-sol at opus class (review-pr skill)
Opus recheck not required (reviewed at opus class).
Summary
MetalRenderPipelineKey(metal_runtime.hpp): colour, depth and entity-id pixel formats, the blend setting, and aMetalVertexLayoutSignature(per attribute: format, offset, buffer index; plus attribute count and stride). Equality is defaulted andMetalRenderPipelineKeyHashreads only those fields. The cache isstd::unordered_map<MetalRenderPipelineKey, MTL::RenderPipelineState *, MetalRenderPipelineKeyHash>. The per-drawstd::stringkey and the descriptor-address input are gone.MetalVertexLayoutImplcomputes the signature once at construction and builds itsMTL::VertexDescriptorfrom the signature, so the key and the descriptor cannot disagree.bind()passes the signature tosetActiveMetalVertexLayout, andMetalVertexLayoutBindingcarries it.currentMetalRenderPipelineKey(blendingEnabled)builds the key from the bound target (including the entity-id attachment format) and the active layout. Each of the five draw functions changes one argument:getRenderPipelineState(currentMetalRenderPipelineKey(m_blendingEnabled), layout.vertexDescriptor_).key.entityIdPixelFormat_, no longer a re-read of global state) and the live descriptor. The cache stores no descriptor pointer. The failure branch is untouched, for render/metal: report allocation, pipeline and command-buffer failures in every build type #4995.kMaxAttributes=kMaxVertexAttributes(16,vao.hpp): every layout the sharedVertexLayoutcontract admits, the same cap the OpenGL backend asserts. A layout over that cap is refused at creation with anIR_ASSERT. The engine's layouts use at most 2.MetalPipelineStateProvider::renderPipelineStateCount()is the narrowest accessor the cache tests need.Test plan
fleet-build --target IrredenEngineTest: clean on head3fc9f7529(rebased onto master369cb6b06)fleet-run IrredenEngineTest --gtest_filter='MetalPipelineKey*': 8/8 pass;--gtest_filter='Metal*': 34/34 pass (includesMetalStickyUntrack*andMetalGpuComputeDispatchTest*)MetalPipelineKeyCacheTest.HoldsALayoutWithTheMostAttributesVertexLayoutAdmitsbuilds a 16-attribute layout, checks every slot's format and offset plus the stride, and builds a pipeline state from it. Mutation with the cap set back to 8 (the test'sstatic_assertdisabled): FAILS (Engine assertion failedthrown). Restored: passes. The over-capacity case (RefusesALayoutTheSignatureCannotHold, now 17 attributes) passes.renderPipelineStateCount()accessor added to the unchanged string-keyed cache), same cache tests:LayoutsWithEqualContentsShareOneStateFAILS (cachedStateCount()is 2, expected 1).ReplacingALayoutWithEqualContentsBuildsNoNewStateFAILS (2, expected 1). The other two pass on the parent.cmake -DPROJECT_ROOT=$PWD -P cmake/run_header_checks_standalone.cmake: exit 0python3 scripts/lint_comment_refs.py: exit 0369cb6b06(the fork point) and captured its output as temporary references (render-verify --update-references --force), then checked out head3fc9f7529, rebuilt (the log showsmetal_runtime.cpp,metal_render_impl.cpp,metal_pipeline.cppandmetal_framebuffer.cpprecompiled), and ranrender-verifyagainst them. Both read max delta 0 (details in the evidence table). Committed references were restored afterwards; none are in this diff.Acceptance evidence
fleet-run IrredenEngineTest --gtest_filter='MetalPipelineKey*'on branch and on parent + count shim[ OK ] MetalPipelineKeyCacheTest.LayoutsWithEqualContentsShareOneState; parent:[ FAILED ] ...LayoutsWithEqualContentsShareOneState:cachedStateCount()Which is: 2vs1MetalPipelineKey.EachFieldDistinguishesTheKey(pure key test; also buffer index and attribute count; all 10 variants are distinct in anunordered_setwith the real hash) andMetalPipelineKeyCacheTest.EachBlendSettingHasItsOwnState(draws with blending off/on/off → 2 states)[ OK ] MetalPipelineKey.EachFieldDistinguishesTheKey,[ OK ] MetalPipelineKeyCacheTest.EachBlendSettingHasItsOwnStatestatic_assert(std::is_trivially_copyable_v<MetalRenderPipelineKey>)inmetal_runtime.hpp, repeated for the key and the signature in the test) + source reviewMTL::PixelFormat, abool, and a signature ofMTL::VertexFormat/uint32_tfields, with no pointer members.operator==is defaulted;MetalRenderPipelineKeyHashcombines each field explicitly and never reads raw bytes or paddingReplacingALayoutWithEqualContentsBuildsNoNewState(move-assigns a fresh layout 10×, the shapedepth_tested_primitives.hpp's growth branch uses: replacement built before the old is released) andDestroyingAndRecreatingAnEqualLayoutBuildsNoNewState(scoped destroy-then-create 10×)[ OK ]on branch; the replace variant fails on the parent (2 entries). The scoped variant passes on the parent too, because the allocator hands the freed descriptor's address straight back (the stale-address hazard in this issue's Scope)static_assert) + source reviewgetRenderPipelineStatecallsm_renderStates.find(key)on theconst MetalRenderPipelineKey &it is passed.currentMetalRenderPipelineKeyreturns the key by value from runtime fields. Nothing on the hit path builds astd::string, callsstd::to_string, or allocates. The map's key type is the value structrender-verifymax delta 0 forIRShapeDebugandIRCanvasStresspython3 scripts/render-verify.py --target IRShapeDebugand--target IRCanvasStresson head3fc9f7529, against references captured from the fork-point build (master369cb6b06)all 158 checks PASS, all 151 pixel shots100.0 0 inf(match %, max delta, PSNR), plus 7 structural checks PASS. IRCanvasStress:all 59 checks PASS, all 36 pixel shots100.0 0 inf, plus 23 structural checks PASSPlan departures
render-verifyagainst the committed references. On this host, master itself fails 19 of 148 IRShapeDebug checks against them, all yaw-180/270 shots, max delta 109–120 (the identical set and deltas appear on an unrelated branch's head run on this host). So the committed references cannot show a delta of 0 for anyone. Criterion 6 is graded on a base-vs-branch A/B instead: the fork-point build's captures are the references. The A/B ran after the final rebase, on the pushed head.MetalRenderPipelineKey/MetalVertexLayoutSignature/MetalVertexAttributeSignature.Notes for reviewer
engine/render/visual glob, but the change has no visual effect (A/B max delta 0 on all 187 pixel shots). Perengine/render/CLAUDE.md, changes without a visual effect need no captures.fleet-pr-overlap --base master:VERDICT: overlap, every row trial-merges clean: render: configure LOD zoom thresholds and hysteresis per camera #4921test/CMakeLists.txt, render: skip redundant cardinal surface shadow queries (#4963) #5016engine/render/src/metal/metal_pipeline.cpp, profile: print the per-frame decomposition in the profile report #5045engine/render/src/metal/metal_render_impl.cpp, engine/voxel: run voxel-set position updates once per rendered frame #5046test/CMakeLists.txt.Closes #4996
🤖 Generated with Claude Code