shader: cache the tessellation stride analysis per shader pair #1167

Closed
github-bot wants to merge 1 commit from gh-pr/1167/head into gh-pr/1167/base
Owner

@⁠romainhedouin on GitHub · 2026-10-07 22:37 UTC · original
romainhedouin:perf/tessellation-stride-cache → main · +226 −3 · 2 files · 1 commits · closed without merging

PrepareTessellationPrograms runs AnalyzeTessellationPrograms on every patch draw. That decodes the LS and HS code again and walks every instruction, only to get the same two strides back.

The strides depend only on the two code blobs and the two control point counts (nothing else is read in Tessellation.cpp). So cache them, keyed on the content hashes from ShaderMapUserData plus both counts, behind a mutex. The analysis exits on failure, so only successes are stored. This is the same assumption ProgramCache already makes about shader code being immutable per hash.

Measured on an M series Mac with the small shaders from the existing test (6, 52 and 10 words): PrepareTessellationPrograms goes from 2.39 us to 0.45 us per call. A real HS is larger and the old cost grows with it, so this is a lower bound. Only games that draw patches are affected.

What I tested:

  • New check in ShaderRecompilerComputeTests (--tessellation-only): PrepareTessellationPrograms equals the uncached AnalyzeTessellationPrograms on the first call, on repeated calls, with two shader pairs interleaved, and with 8 threads first using 96 different pairs at once. Passes on main and on this branch.
  • The check fails when I break the cache on purpose (colliding key, swapped values).
  • ThreadSanitizer: no reports with the mutex, 22 without it.
  • The emulator does not build on arm64 macOS, so I linked the existing objects by hand and stubbed the 9 symbols that come from the x86 only files. Not run on x86_64 or in a game.
  • The control point counts in the key are not covered by the test: for shaders the analyzer accepts, the strides do not depend on them.

The shader builder of CheckTessellationProgram moved into BuildTessellationCode so the new check can reuse it.

AI tools were used for code review and drafting the tests. I reviewed the diff and ran the tests above.

**@⁠romainhedouin** on GitHub · 2026-10-07 22:37 UTC · [original](https://github.com/KytyPS5/KytyPS5/pull/1167) `romainhedouin:perf/tessellation-stride-cache` → `main` · +226 −3 · 2 files · 1 commits · closed without merging PrepareTessellationPrograms runs AnalyzeTessellationPrograms on every patch draw. That decodes the LS and HS code again and walks every instruction, only to get the same two strides back. The strides depend only on the two code blobs and the two control point counts (nothing else is read in Tessellation.cpp). So cache them, keyed on the content hashes from ShaderMapUserData plus both counts, behind a mutex. The analysis exits on failure, so only successes are stored. This is the same assumption ProgramCache already makes about shader code being immutable per hash. Measured on an M series Mac with the small shaders from the existing test (6, 52 and 10 words): PrepareTessellationPrograms goes from 2.39 us to 0.45 us per call. A real HS is larger and the old cost grows with it, so this is a lower bound. Only games that draw patches are affected. What I tested: - New check in ShaderRecompilerComputeTests (`--tessellation-only`): PrepareTessellationPrograms equals the uncached AnalyzeTessellationPrograms on the first call, on repeated calls, with two shader pairs interleaved, and with 8 threads first using 96 different pairs at once. Passes on main and on this branch. - The check fails when I break the cache on purpose (colliding key, swapped values). - ThreadSanitizer: no reports with the mutex, 22 without it. - The emulator does not build on arm64 macOS, so I linked the existing objects by hand and stubbed the 9 symbols that come from the x86 only files. Not run on x86_64 or in a game. - The control point counts in the key are not covered by the test: for shaders the analyzer accepts, the strides do not depend on them. The shader builder of CheckTessellationProgram moved into BuildTessellationCode so the new check can reuse it. AI tools were used for code review and drafting the tests. I reviewed the diff and ran the tests above.
PrepareTessellationPrograms decoded the LS and HS code again on every patch draw. The strides only depend on the two shader hashes and the control point counts, so cache them.
github-bot locked as GitHub sync and limited conversation to collaborators 2026-11-26 09:46:06 +00:00
github-bot closed this pull request 2026-11-26 09:46:06 +00:00

Pull request closed

This discussion has been locked. Commenting is limited to contributors.
No description provided.