shader: implement S_MEMREALTIME (SMEM 0x25) #601
No reviewers
Labels
No labels
bug
documentation
duplicate
enhancement
GPU
GUI
help wanted
question
Shaders
Video
wontfix
No milestone
No assignees
1 participant
Notifications
Due date
No due date set.
Dependencies
No dependencies set
Reference
github/KytyPS5--KytyPS5!601
Loading…
Reference in a new issue
No description provided.
Delete branch "gh-pr/601/head"
Deleting a branch is permanent. Although the deleted branch may continue to exist for a short time before it actually gets removed, it CANNOT be undone in most cases. Continue?
@maximeallanic on GitHub · 2026-09-13 11:30 UTC · original
maximeallanic:smem-memrealtime→main· +157 −1 · 18 files · 1 commits · closed without mergingRatchet & Clank: Rift Apart (PPSA01474, v01.005.003) stops during shader recompilation on a missing SMEM opcode:
SMEM 0x25 is
S_MEMREALTIME. Per the AMD ISA it "reads a 64-bit real time-counter and returns the value into a pair of SGPRS: SDST and SDST+1. The time value is from a constant 25MHz clock".What this does
S_MEMREALTIMEin the SMEM table (2 dwords, no memory operands). The ISA only defines the result for an even SDST, so an odd SDST stays unsupported.GetRealTimeCounter(dword)reads the counter, and the side-effectingMarkRealTimeCounterReadrecords that an invocation has read it.RebindBufferssamplesKernelGetProcessTimeCounter— the clock the title already observes — rescaled to 25 MHz with integer arithmetic, for every draw and dispatch.EmitShaderDataDwordLoad, like user data.The part I would like your opinion on
The failing shader does not read the clock once. It reads the clock, reads a budget from its own buffer, and then loops re-reading the clock until the deadline passes:
A Vulkan shader cannot observe time passing while it runs. With a counter sampled once per dispatch that loop never exits: the GPU hangs and the driver resets (
vkQueueSubmit failed: ErrorDeviceLost,nvlddmkmevent 153 on Windows).So this models the counter inside an invocation: the first read returns the sampled time and every later read returns
UINT64_MAX, which retires a deadline loop after one pass instead of spinning forever. It invents no duration, but a title does get one pass of work per dispatch where hardware would get many.Other models exist (advance by an estimated per-iteration cost, or calibrate from the measured duration of the previous dispatch). I picked the one that cannot hang and invents no number, but this is a semantic decision and I would rather follow your preference than impose mine.
Testing
Windows 11, RTX 4070, local build of
d3d7bd3, PPSA01474:shader_cfg_testspasses, including a new case covering the decode, the IR lowering, the reserved shader-data dwords, the read marker and SPIR-V validation.The title still renders nothing. With this change it stops later, in resource tracking, with
GetBufferResource dword 2 is not a valid runtime value(hash=0xf8bcd41f5f36626d): that shader builds its whole buffer descriptor in VGPRs from aBUFFER_LOAD_DWORDand an emulated unsigned division, then makes it wave-uniform withV_READFIRSTLANE_B32. That looks like a second shape of #507, and I have not touched it here.AI disclosure
This change was implemented with AI assistance (Claude): reading the AMD ISA to identify the opcode, writing the patch, and analysing the logs and the failing shader. I reviewed the change, built it, and ran the shader tests myself on the machine described above, and I am responsible for what it does.
🤖 Generated with Claude Code
Summary by CodeRabbit
@coderabbitai[bot] on GitHub · 2026-09-13 11:30 UTC · original · edited 2026-09-13 11:36 UTC
No actionable comments were generated in the recent review. 🎉
ℹ️ Recent review info
⚙️ Run configuration
Configuration used: defaults
Review profile: CHILL
Plan: Advanced
Run ID:
3906db4f-4c04-48f6-88a2-ed044c7899ef📥 Commits
Reviewing files that changed from the base of the PR and between
d3d7bd33f8and07455df1d1.📒 Files selected for processing (18)
src/graphics/host_gpu/renderer/pipeline/descriptors.cppsrc/graphics/shader/recompiler/backend/spirv/SpirvEmitter.cppsrc/graphics/shader/recompiler/backend/spirv/spirvEmitterFlow.cppsrc/graphics/shader/recompiler/backend/spirv/spirvEmitterInstructions.hsrc/graphics/shader/recompiler/backend/spirv/spirvEmitterInternal.hsrc/graphics/shader/recompiler/backend/spirv/spirvEmitterModule.cppsrc/graphics/shader/recompiler/backend/spirv/spirvEmitterProgram.cppsrc/graphics/shader/recompiler/frontend/decode/MemoryOps.cppsrc/graphics/shader/recompiler/frontend/decode/ShaderDecoder.cppsrc/graphics/shader/recompiler/frontend/decode/ShaderDecoder.hsrc/graphics/shader/recompiler/frontend/translate/Memory.cppsrc/graphics/shader/recompiler/frontend/translate/Translator.hsrc/graphics/shader/recompiler/ir/ShaderIR.hsrc/graphics/shader/recompiler/ir/opcodes/ValueOpcodes.cppsrc/graphics/shader/recompiler/ir/opcodes/ValueOpcodes.incsrc/graphics/shader/recompiler/ir/passes/BindingLayout.cpptests/ShaderRecompilerComputeTests.cpptests/shaderCfgTests.cppIncluded review availability: Your plan provides up to 8 included reviews per hour; 7 remain after this review.
📝 Walkthrough
Walkthrough
Changes
Real-Time Counter Support
src/graphics/shader/recompiler/frontend/decode/...,src/graphics/shader/recompiler/frontend/translate/...,src/graphics/shader/recompiler/ir/opcodes/*S_MEMREALTIME. The translator emitsGetRealTimeCounterandMarkRealTimeCounterRead.src/graphics/shader/recompiler/ir/ShaderIR.h,src/graphics/shader/recompiler/ir/passes/BindingLayout.cpp,src/graphics/host_gpu/renderer/pipeline/descriptors.cppsrc/graphics/shader/recompiler/backend/spirv/*0xffffffff.tests/ShaderRecompilerComputeTests.cpp,tests/shaderCfgTests.cppPriority: ⬇️ Low
Estimated code review effort: 3 (Moderate) | ~25 minutes
Change: Bug fix
Suggested reviewers:
nmzikSequence Diagram(s)
Merge Risk: ⚪ Minimal · up to
07455The real-time counter support is wired through decoding, shader data, and SPIR-V emission without an identified current-head defect.
🚥 Pre-merge checks | ✅ 4 | ❌ 1
❌ Failed checks (1 warning)
✅ Passed checks (4 passed)
Full details: Docstring Coverage
Explanation
Docstring coverage is 17.86% which is insufficient. The required threshold is 80.00%. Docstring coverage is scoped to functions touched by this diff. Analyzed 28 functions across 15 files. (3 skipped: 1 unsupported, 2 too large.)
✨ Finishing Touches
🧪 Generate unit tests (beta)
Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.
❤️ Share
Comment
@coderabbitai helpto get the list of available commands.@copilot-pull-request-reviewer[bot] on GitHub · 2026-09-13 11:38 UTC · original · reviewed
🟡 Changes recommended
Address the critical timer-frequency failure and wave64 per-half realtime-read state issues.
Get a fresh assessment by requesting another Copilot review.
Pull request overview
Implements AMD
S_MEMREALTIMEsupport end to end, including decoding, IR lowering, shader-data bindings, SPIR-V emission, and host counter sampling.Changes:
0x25, rejecting odd destinations.File summaries
tests/ShaderRecompilerComputeTests.cpptests/shaderCfgTests.cppsrc/graphics/shader/recompiler/ir/ShaderIR.hsrc/graphics/shader/recompiler/ir/passes/BindingLayout.cppsrc/graphics/shader/recompiler/ir/opcodes/ValueOpcodes.incsrc/graphics/shader/recompiler/ir/opcodes/ValueOpcodes.cppsrc/graphics/shader/recompiler/frontend/translate/Translator.hsrc/graphics/shader/recompiler/frontend/translate/Memory.cppS_MEMREALTIME.src/graphics/shader/recompiler/frontend/decode/ShaderDecoder.hsrc/graphics/shader/recompiler/frontend/decode/ShaderDecoder.cppsrc/graphics/shader/recompiler/frontend/decode/MemoryOps.cpp0x25.src/graphics/shader/recompiler/backend/spirv/spirvEmitterProgram.cppsrc/graphics/shader/recompiler/backend/spirv/spirvEmitterModule.cppsrc/graphics/shader/recompiler/backend/spirv/spirvEmitterInternal.hsrc/graphics/shader/recompiler/backend/spirv/spirvEmitterInstructions.hsrc/graphics/shader/recompiler/backend/spirv/spirvEmitterFlow.cppsrc/graphics/shader/recompiler/backend/spirv/SpirvEmitter.cppsrc/graphics/host_gpu/renderer/pipeline/descriptors.cppReview details
Suppressed comments (3)
src/graphics/shader/recompiler/backend/spirv/spirvEmitterFlow.cpp:570
lane_count == 2(wave64 emulated with host subgroups of 32),EmitBlockemits each IR instruction for both half contexts sequentially in the same SPIR-V invocation. The first half'sMarkRealTimeCounterReadstore runs before the second half'sGetRealTimeCounter, so the high half's first read observes the flag as set and returnsUINT32_MAXinstead of the sampled counter; SGPR results then disagree between the two halves. Emit the marker/read as one logical operation across both halves (for example, delay the store until both half emissions) and add a wave64/subgroup32 regression test.src/graphics/shader/recompiler/backend/spirv/spirvEmitterInternal.h:112
lane_count == 2, the emitter visits every IR instruction for half 0 and half 1 sequentially, but both halves currently use this single function-local flag. The half-0MarkRealTimeCounterReadstore therefore sets the flag before half 1 performs its first read, making the high emulated wave half receiveUINT64_MAXon its firstS_MEMREALTIMEinstead of the sampled counter. Keep the read state per emulated half (and select it usingstate.lane_half, as scratch storage does) so each half has independent first-read state.tests/shaderCfgTests.cpp:4680
S_MEMREALTIMEand consumes only the low dword, so it never exercises thefirst == falsepath that returnsUINT32_MAXon later reads, and the high-dword load may be dead-code eliminated. SPIR-V validation can still pass if the deadline-loop retirement logic is broken; add a two-read case (ideally across a loop/control-flow shape) and assert the generated behavior for the second read.💡 Add a
code-reviewagent skill or configure MCP servers for context-aware, tailored reviews. Learn more in the docs.@ -776,0 +781,4 @@const auto ticks = Libs::LibKernel::KernelGetProcessTimeCounter();const auto frequency = Libs::LibKernel::KernelGetProcessTimeCounterFrequency();return ticks / frequency * realtime_frequency +ticks % frequency * realtime_frequency / frequency;@Copilot on GitHub · 2026-09-13 11:38 UTC · original
KernelGetProcessTimeCounterFrequency()can be zero on the non-x86 path because it directly returnsQueryPerformanceFrequency(), but this helper unconditionally uses it for both division and modulo. A failed host timer would therefore make any realtime shader hit undefined divide-by-zero duringRebindBuffers; handle the zero case before scaling, consistent withKernelGetProcessTime().@Nmzik on GitHub · 2026-09-24 16:57 UTC · original
Credited
Pull request closed