Skip to content

perf(shader): evaluate only the selected cosine polynomial - #16350

Merged
oneandonlydean merged 1 commit into
boykopovar:mainfrom
Filip722:perf/conditional-cosine-20261106
Nov 7, 2026
Merged

oneandonlydean merged 1 commit into
boykopovar:mainfrom
Filip722:perf/conditional-cosine-20261106

Conversation

@Filip722

@Filip722 Filip722 commented Nov 6, 2026

Copy link
Copy Markdown
Contributor

What

The cosine accuracy fallback evaluates both its sine and cosine polynomial alternatives before selecting the required range. This keeps two arithmetic chains live even though only one result is selected.

  • Emit structured selection before evaluating the middle-range sine polynomial or the outer-range cosine polynomial.
  • Preserve the existing ordered comparisons, selected arguments, coefficients, sign handling, native cosine, accuracy-distance selection and near-zero repair.

The original choice p3 ? -cosine : p1 ? sine : cosine equals choosing sine when p1 && !p3 and choosing the original signed cosine otherwise, including when NaN makes both ordered comparisons false. Selected arithmetic, rounding/denormal modes, signed zero and upstream NaN repairs remain unchanged. This follows the existing conditional sine structure and retains the shared cosine chain.

A pinned Kyty source comparison motivated inspecting the larger accuracy sequence. Kyty's native-only trig behavior is not substituted for AnyPS5's fallback. No cross-engine speed ratio is claimed.

Tested

Same five focused reduction, near-zero, GCN, unary and denormal-flush fixtures pass on pinned main and the final clean candidate with required Vulkan, disabled shader cache and actual Radeon/Khronos instance/device loading. Neither arm reports VUIDs, missing layers or skips. A private extension of the existing synthetic fixture also produced 39,424 bit-identical output words across supported float/fused modes and retained the same two existing unsupported denormal-mode refusals. All 16 synthetic modules validate.

The executing game's primary fragment module retains 27 sine and nine cosine operations and gains nine conditional cosine regions. Static FMA count is unchanged and SPIR-V grows; no native instruction-count saving is claimed.

Sons of Sparta, Radeon 760M, same owned saved camp, system RADV Mesa 25.0.7-2+deb13u1, journal enabled, immediate presentation, exact arithmetic and optimized GPL/LTO. Requested/render extent and presentation 1920x1080, windowed client 1152x648; physical VkImage extent was not separately measured. Main was pinned at 0fbe14c15361 at the start of this batch; candidate is 492014640f0a.

Primary: run-level GPU timestamped command-span ms per delivered present, weighted over whole 1s summaries wholly inside first-present+50..110s. The screen and confirmation are separate prospective cohorts:

Study Main ms/present Change ms/present Paired reduction, 95% t CI
Fixed six-pair screen 10.075929 10.022394 0.531%, [0.311%, 0.751%]
One fresh four-pair confirmation 10.069140 10.019362 0.494%, [0.180%, 0.809%]

All 20 timed runs are valid, with zero exclusions or replacements. Frames are not independent replicates. This is a small command-span efficiency result at the approximately 60 FPS cap; no energy, uncapped FPS or stutter improvement is established. Divergent/backend behavior can limit the benefit on other workloads.

Complete timing-only evidence, protocols, every full upstream digest and original-byte/metric verifier. Both arms use the same disclosed minimal readiness hook; diagnostic draw probes are removed. Game data, shaders and screenshots remain private.

The overlap tool could not complete four shared-file comparisons (#5021, #7410, #12324, #13486). Manual source review of their unchanged audited heads found no duplicate conditional-cosine implementation or prerequisite; #13486's adjacent GCN limits are retained. This is not a claim that the tool returned a clean result.

Required original detailed 12s trace summaries from the first predeclared screen pair follow. These captures finish before the separate primary window used above. All 20 full original timing traces and benches are attached in the evidence bundle.

Main:

{"commit":"0fbe14c15361","dirty":true,"env":{"APS5_NO_PAD_INPUT":"1","APS5_PAD_INPUT_FILE":"<path>","APS5_TRACE_BENCH":"1","APS5_TRACE_DIR":"<path>","APS5_TRACE_EVENTS":"524288","APS5_TRACE_SECONDS":"30:12","APS5_TRACE_SUMMARY_EVERY":"1"},"gpu":"AMD Radeon 760M (RADV PHOENIX)","driver":"radv Mesa 25.0.7-2+deb13u1","os":"Linux 7.2.12-x64v3-xanmod1","cpu":"AMD Ryzen 5 7640HS w/ Radeon 760M Graphics","cpu_threads":12,"memory_mib":23820,"title_id":"PPSA28997","title":"God of War Sons of Sparta","content_version":"01.008.001","resolution":"1920x1080","window":{"spec":"seconds 30:12","first_present":1435,"ms":12012.005},"presents":720,"frame_ms":{"p50":16.684,"p90":16.692,"p99":16.707,"max":17.008},"gpu_busy_ms_per_present":9.955,"worker_busy_pct":{"queue worker 0x0":66.292},"top_phases_ms_per_frame":{"VideoOut.Flip":16.655,"VideoOut.Flip.vblank_wait":14.898,"Driver.Packet":10.981,"Driver.Packet.draw":9.073,"Driver.Draw":9.042,"Driver.Draw.device_setup":3.378,"Graphics.Draw":3.366,"Graphics.Draw.draw_and_resource_release":2.832},"trace_dropped":false,"trace":"aps5-trace-0fbe14c15361-3402525.json.gz"}

Change:

{"commit":"492014640f0a","dirty":true,"env":{"APS5_NO_PAD_INPUT":"1","APS5_PAD_INPUT_FILE":"<path>","APS5_TRACE_BENCH":"1","APS5_TRACE_DIR":"<path>","APS5_TRACE_EVENTS":"524288","APS5_TRACE_SECONDS":"30:12","APS5_TRACE_SUMMARY_EVERY":"1"},"gpu":"AMD Radeon 760M (RADV PHOENIX)","driver":"radv Mesa 25.0.7-2+deb13u1","os":"Linux 7.2.12-x64v3-xanmod1","cpu":"AMD Ryzen 5 7640HS w/ Radeon 760M Graphics","cpu_threads":12,"memory_mib":23820,"title_id":"PPSA28997","title":"God of War Sons of Sparta","content_version":"01.008.001","resolution":"1920x1080","window":{"spec":"seconds 30:12","first_present":1461,"ms":12012.003},"presents":720,"frame_ms":{"p50":16.683,"p90":16.693,"p99":16.708,"max":16.735},"gpu_busy_ms_per_present":9.905,"worker_busy_pct":{"queue worker 0x0":66.536},"top_phases_ms_per_frame":{"VideoOut.Flip":16.657,"VideoOut.Flip.vblank_wait":14.894,"Driver.Packet":11.026,"Driver.Packet.draw":9.158,"Driver.Draw":9.125,"Driver.Draw.device_setup":3.383,"Graphics.Draw":3.316,"Driver.Draw.draw_enqueued":2.904},"trace_dropped":false,"trace":"aps5-trace-492014640f0a-3403744.json.gz"}

Checklist

  • Based on current main; no other open PR implements the same functions
  • One topic per PR; follow-ups go in a new PR
  • No comments in code except technical debt
  • Unimplemented paths throw (NotImplemented_nid_no_patch); silent stubs are listed under Silent stubs in their library's technical debt file
  • No notes, investigation .md files or images added to the repository (attach them to this PR)
  • New third-party code is a submodule built from source
  • Generic behaviour, not specific to one title
  • Depends on: none
  • Fixes: none
  • Unblocks: none
  • GPUs tested: AMD Radeon 760M, system RADV Mesa 25.0.7-2+deb13u1, Debian Linux 7.2.12-x64v3-xanmod1
  • AI-assisted: yes

@github-actions github-actions Bot added perf Performance change shader core/shader labels Nov 6, 2026
@JJenkx

JJenkx commented Nov 6, 2026

Copy link
Copy Markdown
Contributor

Windows check of head 492014640f, merged onto main 6185bdcfc4 (b7181b6615, clean merge).

Setup: Windows 11 Enterprise 22621, WinLibs GCC 15.2 UCRT, Release, Radeon RX 6700 XT, AMD driver 26.10.1. Run through the Windows test queue with ANYPS5_REQUIRE_VULKAN=1 and Vulkan validation layers 1.4.363. The hosted Windows check does not run these GPU tests on pull requests.

This PR adds no test, so I ran the existing GPU execution tests that go through the sine/cosine emitter it changes:

  • agc_driver_sin_cos_near_zero, agc_driver_sin_cos_reduction, agc_driver_sdwa_sin_cos_f32, agc_driver_sdwa_sin_cos_f32_wave64, agc_driver_gcn_sin_near_zero and agc_driver_f64_transcendental all pass in 3 of 3 runs, none skipped.
  • The Vulkan device was created in each run, the validation layer loaded, and there were no VUID- errors. So the new OpSelectionMerge/OpBranchConditional/OpPhi structure is accepted by the validator and gives the expected results on AMD.

This is correctness only; I did not measure the performance effect on this machine.

AI-assisted: yes.

@TechLevelZero

Copy link
Copy Markdown
Contributor

✅ NVIDIA GPU test: passed

PR head 4920146 merged onto main 5299939 · tested 2026-11-06 11:41 UTC · 20 min

GPU NVIDIA RTX 2000 Ada Generation, driver 595.99.02, Vulkan 1.4.329
Host Ubuntu 26.04.1 LTS, kernel 7.0.0-38-generic, container ubuntu:24.04
Logs build and test logs for this run
Build ✅ passed (Release, -DANYPS5_ENABLE_SPIRV_TOOLS=ON)
Tests ✅ 2,916 passed · 5 failed (0 new, 4 also fail on main, 1 unstable on main) · 52 skipped (ANYPS5_REQUIRE_VULKAN=1)
Test setup CPU-only tests without a GPU; GPU tests (and any CPU-only test that failed or skipped without one) once on the GPU with VK_LAYER_KHRONOS_validation and ANYPS5_NO_SHADER_CACHE=1; main ran the same way
Test selection 2,973 of 4,210 tests run: the ones this change can reach, chosen by tools/select_tests.py from main's build graph
Vulkan validation ✅ no new validation IDs vs main (979 tests run on the GPU)
Slow tests (>30 s, GPU tests timed with validation layers) ⚠️ agc_driver_pipeline_journal 120.1 s
Unstable on main (1, these also failed in recent main runs on this machine; not counted against this PR)
agc_driver_pipeline_journal: failed in 1 of the last 10 main runs
Failing on main too (4, not counted against this PR)
agc_driver_image_table
guest_mmap_executable
guest_socket_options_multicast
web_library_assets
Validation IDs already present on main (9 IDs in 587 tests)
VUID-VkDeviceCreateInfo-pNext-pNext (587 tests): agc_driver, agc_driver_address_draw_space_reuse, agc_driver_address_passes, agc_driver_address_passes_targets_split, agc_driver_alias_copy_label, agc_driver_alpha_to_coverage, agc_driver_array_layer_target, agc_driver_bc_block_copy, and 579 more
VUID-VkShaderModuleCreateInfo-pCode-08737 (14 tests): agc_driver_dpp16_operands, agc_driver_dpp8_pk_fmac, agc_driver_dx10_clamp, agc_driver_f16_fma_denormal_modes, agc_driver_f16_fma_invalid_nan, agc_driver_float16_fma_rounding, agc_driver_float16_modifiers, agc_driver_float16_native, and 6 more
VUID-VkShaderModuleCreateInfo-pCode-08739 (14 tests): agc_driver_dpp16_operands, agc_driver_dpp8_pk_fmac, agc_driver_dx10_clamp, agc_driver_f16_fma_denormal_modes, agc_driver_f16_fma_invalid_nan, agc_driver_float16_fma_rounding, agc_driver_float16_modifiers, agc_driver_float16_native, and 6 more
VUID-VkShaderModuleCreateInfo-pCode-08741 (14 tests): agc_driver_dpp16_operands, agc_driver_dpp8_pk_fmac, agc_driver_dx10_clamp, agc_driver_f16_fma_denormal_modes, agc_driver_f16_fma_invalid_nan, agc_driver_float16_fma_rounding, agc_driver_float16_modifiers, agc_driver_float16_native, and 6 more
VUID-vkCmdCopyImage-srcImage-01548 (7 tests): agc_driver_depth_clear_hle, agc_driver_depth_plane_buffer_memory, agc_driver_depth_plane_partial_store, agc_driver_depth_plane_sampling, agc_driver_depth_storage_dispatch, agc_driver_depth_surface_from_storage, agc_driver_dispatch_move_depth
VUID-vkCmdCopyImage-srcImage-01551 (7 tests): agc_driver_depth_clear_hle, agc_driver_depth_plane_buffer_memory, agc_driver_depth_plane_partial_store, agc_driver_depth_plane_sampling, agc_driver_depth_storage_dispatch, agc_driver_depth_surface_from_storage, agc_driver_dispatch_move_depth
VUID-VkPipelineColorBlendStateCreateInfo-pAttachments-00605 (1 test): agc_driver_prepared_exports
VUID-VkPipelineShaderStageCreateInfo-module-parameter (1 test): agc_driver_float16_unused_nan_payloads
VUID-VkPipelineShaderStageCreateInfo-pSpecializationInfo-06849 (1 test): agc_driver_float16_unused_nan_payloads
Skipped (52)
agc_driver_gcn_display_present: skipped, no display: Vulkan support is either not configured in SDL or not available in current SDL video driver (offscreen) or platform
agc_driver_sample_order_grid_reference: skipped, the device does not keep depth drawn at custom sample locations at those locations, even after a transition that names them: the composite of uniform renders holds the guest's depth in 0 of 3005 sloped samples, so no path can be compared with it bit for bit
agc_driver_scattered_runs: skipped, host imports are unavailable: /dev/udmabuf is not accessible
agc_driver_vertex_elected_atomic_max: skipped, the device runs vertex programs in subgroups, so they keep their cross-lane reductions
agc_host_import_sparse: skipped, host imports are unavailable: /dev/udmabuf is not accessible
guest_http_uri_locale: 
guest_net_ctl: skipped, the host has no IPv4 address
guest_net_ctl_disconnected: skipped, the host has no IPv4 address
guest_video_out_color_space: skipped, no display or Vulkan device: SDL_InitSubSystem(VIDEO | GAMECONTROLLER) failed: wayland,x11 not available
guest_video_out_device_capability: skipped, no display or Vulkan device: SDL_InitSubSystem(VIDEO | GAMECONTROLLER) failed: wayland,x11 not available
guest_video_out_hdr: skipped, no display or Vulkan device: SDL_InitSubSystem(VIDEO | GAMECONTROLLER) failed: wayland,x11 not available
guest_video_out_high_frame_rate: skipped, no display or Vulkan device: SDL_InitSubSystem(VIDEO | GAMECONTROLLER) failed: wayland,x11 not available
guest_video_out_host_resolution_disabled: skipped, no display or Vulkan device: SDL_InitSubSystem(VIDEO | GAMECONTROLLER) failed: wayland,x11 not available
guest_video_out_host_resolution_four_k: skipped, no display or Vulkan device: SDL_InitSubSystem(VIDEO | GAMECONTROLLER) failed: wayland,x11 not available
guest_video_out_host_resolution_height_only: skipped, no display or Vulkan device: SDL_InitSubSystem(VIDEO | GAMECONTROLLER) failed: wayland,x11 not available
guest_video_out_host_resolution_limit: skipped, no display or Vulkan device: SDL_InitSubSystem(VIDEO | GAMECONTROLLER) failed: wayland,x11 not available
guest_video_out_host_resolution_malformed: skipped, no display or Vulkan device: SDL_InitSubSystem(VIDEO | GAMECONTROLLER) failed: wayland,x11 not available
guest_video_out_host_resolution_width_only: skipped, no display or Vulkan device: SDL_InitSubSystem(VIDEO | GAMECONTROLLER) failed: wayland,x11 not available
guest_video_out_output_mode: skipped, no display or Vulkan device: SDL_InitSubSystem(VIDEO | GAMECONTROLLER) failed: wayland,x11 not available
guest_video_out_resolution_status: skipped, no display or Vulkan device: SDL_InitSubSystem(VIDEO | GAMECONTROLLER) failed: wayland,x11 not available
guest_video_out_wqhd_detection: skipped, no display or Vulkan device: SDL_InitSubSystem(VIDEO | GAMECONTROLLER) failed: wayland,x11 not available
video_out_flip_aftergpu: skipped, no display or Vulkan device: DisplayWindow: Vulkan support is either not configured in SDL or not available in current SDL video driver (offscreen) or platform
video_out_flip_backtoback: skipped, no display or Vulkan device: DisplayWindow: Vulkan support is either not configured in SDL or not available in current SDL video driver (offscreen) or platform
video_out_flip_controls: skipped, no display or Vulkan device: DisplayWindow: Vulkan support is either not configured in SDL or not available in current SDL video driver (offscreen) or platform
video_out_flip_cpuflip: skipped, no display or Vulkan device: DisplayWindow: Vulkan support is either not configured in SDL or not available in current SDL video driver (offscreen) or platform
video_out_flip_cpuflip_in_order: skipped, no display or Vulkan device: DisplayWindow: Vulkan support is either not configured in SDL or not available in current SDL video driver (offscreen) or platform
video_out_flip_dcc: skipped, no display or Vulkan device: DisplayWindow: Vulkan support is either not configured in SDL or not a
Reproduce
git fetch origin 492014640f0ae36e24c60fa4c6cea7fe6f5ade17 5299939ecfa9e186e1eb457c6f4bd133bdb087c7
git checkout 5299939ecfa9e186e1eb457c6f4bd133bdb087c7 && git merge --no-ff 492014640f0ae36e24c60fa4c6cea7fe6f5ade17
git submodule update --init --recursive
cmake -S . -B build -G Ninja -DCMAKE_BUILD_TYPE=Release -DBUILD_TESTING=ON -DANYPS5_ENABLE_SPIRV_TOOLS=ON
cmake --build build && cmake --build build --target libs
VK_DRIVER_FILES=/nonexistent/icd.json ctest --test-dir build -E '^(agc|shader|recompiler|videoout|video_out|guest_video_out|vulkan)|gpu' --timeout 120 --parallel 32 --output-on-failure
ANYPS5_REQUIRE_VULKAN=1 ANYPS5_NO_SHADER_CACHE=1 VK_INSTANCE_LAYERS=VK_LAYER_KHRONOS_validation \
  ctest --test-dir build -R '^(agc|shader|recompiler|videoout|video_out|guest_video_out|vulkan)|gpu' --timeout 120 --parallel 4 --output-on-failure
# failures are then re-run alone: --timeout 360 --parallel 2; new validation IDs: the test alone, twice, --parallel 1

Automated build and test on a contributor's NVIDIA machine; it does not replace upstream CI. A new comment is posted for each new push. Full logs.

@milescoviello

Copy link
Copy Markdown
Contributor

NVIDIA runs to go with JJenkx's Radeon check, ANYPS5_REQUIRE_VULKAN=1 ANYPS5_NO_SHADER_CACHE=1, the sine/cosine execution tests (agc_driver_sin_cos_near_zero, _sin_cos_reduction, _sdwa_sin_cos_f32 and its wave64 run, _gcn_sin_near_zero, _f64_transcendental):

Machine Tree Result
Linux, RTX 4070 Ti, NVIDIA 615.71.09, GCC 15.2, Khronos layer 1.4.341 main 9a4bcc3018 6 of 6 pass, 6 devices created, no VUIDs
merge a7b09a17ce 6 of 6 pass, 6 devices created, no VUIDs
Windows 11 VM, GT 1030, NVIDIA 582.78, WinLibs GCC 15.2 merge-base 0fbe14c153 6 of 6 pass in each of 3 runs
head 492014640f 6 of 6 pass in each of 3 runs

So the new OpSelectionMerge / OpPhi structure gives the same results on NVIDIA's compiler too, Pascal included. Correctness only; no timing from me.

AI-assisted: yes (Claude Code)

@github-actions

github-actions Bot commented Nov 7, 2026 •

Copy link
Copy Markdown

Some comparisons could not be completed because a pull request conflicts with the base branch. Review these shared files manually before concluding that there is no overlap.

Pull request Shared files not compared
PR 5021 core/shader/recompiler/SpirvBackend/src/SpirvAlu/SpirvAluEmitterMath.cpp
PR 7410 core/shader/recompiler/SpirvBackend/src/SpirvAlu/SpirvAluEmitterMath.cpp
PR 12324 core/shader/recompiler/SpirvBackend/src/SpirvAlu/SpirvAluEmitterMath.cpp
PR 15343 core/shader/recompiler/SpirvBackend/src/SpirvAlu/SpirvAluEmitterMath.cpp

@oneandonlydean oneandonlydean self-assigned this Nov 7, 2026
@oneandonlydean
oneandonlydean merged commit 5b229aa into boykopovar:main Nov 7, 2026
14 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

perf Performance change shader core/shader

Projects

None yet

Development

Successfully merging this pull request may close these issues.

5 participants