Skip to content

borealis: clamp shader-written depth to the depth clamp range - #1356

Merged
RyanTheTide merged 1 commit into
aurora-silicon:aurora-wipfrom
m0d16l14n1:j815/fragdepth-viewport-clamp
Nov 30, 2026
Merged

RyanTheTide merged 1 commit into
aurora-silicon:aurora-wipfrom
m0d16l14n1:j815/fragdepth-viewport-clamp

Conversation

@m0d16l14n1

@m0d16l14n1 m0d16l14n1 commented Nov 29, 2026 •

Copy link
Copy Markdown

What: With depth clamp on, clamp the depth a fragment shader writes to the depth clamp range: the viewport's [min(n, f), max(n, f)], or the VK_EXT_depth_clamp_control range. The viewport z bounds clamp only the interpolated depth, so a written depth was clamped only by the attachment format. Writing 1.7 with depth clamp on and a [0.1, 0.9] viewport stored 1.0, not 0.9. This also covers the pass-through FragDepth that agx_nir_lower_frag_sidefx adds to side-effecting shaders. Reuses hk_depth_clamp_bounds from #1346, which is now merged; rebased onto aurora-wip 5932858 (patch unchanged).
Tested: At the rebased head 39eac16 vs aurora-wip 5932858 on J815: dEQP gate 11,291 = 11,291 Pass, depth clamp lists 76 = 76, fragdepth 27 = 27, 0 diffs, 0 GPU faults. Before the rebase: J815 MacBook Air 15" M5 (T8142, G17G), Borealis, kernel 7.3.0-aurora-j815-dev, at 82d9859 vs base 2fe6cb3 (#1346 head). VK-GL-CTS 1.4.7.1: dEQP gate (15,958) 11,230 -> 11,230 Pass, 0 diffs; depth clamp lists (draw.*.depth_clamp, clipping.clip_volume.depth_clamp, 128) 76 = 76 Pass; glsl.builtin_var.fragdepth (45) 27 = 27 Pass. With VK_KHR_depth_clamp_zero_one exposed on top (local), dEQP-VK.depth.* + EDS depth clamp (63): 3 -> 1 Fail (depth.fs_clamp and depth.bias_fs_clamp fixed). meson test --suite asahi 223/223. vkdepth gl_FragDepth case neutral (fill b2f 2.292 -> 2.290 ms, -0.07%; tri128 +0.22%); Godot TPS demo 1080p (FAST=1) neutral (view 1 50.2/50.1 -> 49.7/49.3 fps, route 57.6 -> 57.6 fps; GPU p50 19.86 -> 19.93 ms, within run-to-run noise). 0 GPU faults. Regression line: 0 SELinux denials (N/A on this image), 0 failed units, 0 new kernel errors.
Risk: two-way door · all Borealis Macs (fragment shaders that write depth get one 8-byte root load and fmax/fmin; other shaders are unchanged).
Not yet: G13-G16 and M5 Pro/Max hardware untested. With several viewports with different depth ranges, the clamp uses the union of their ranges (the fragment shader does not know the viewport index). depth.ez_fs_clamp (early_fragment_tests + side effects) still fails: there the shader-triggered early test uses interpolated z without the viewport clamp, and no depth store is involved; that is a separate change.

Details
  • Root table: fs_depth_clamp[2], appended after mesh_whole. Set in hk_flush_dynamic_state on VP_VIEWPORTS, VP_VIEWPORT_COUNT, RS_DEPTH_CLAMP_ENABLE and VP_DEPTH_CLAMP_RANGE: hk_depth_clamp_bounds() of every viewport, merged into one range; [0, 1] with depth clamp off (as the viewport z bounds are).
  • hk_nir_clamp_frag_depth() runs in the fragment branch of hk_compile_nir_impl() before agx_nir_lower_discard_zs_emit() turns the depth store into store_zs_agx, and before the root footprint is measured, so the trimmed root covers the new field. It rewrites each FRAG_RESULT_DEPTH store to z == z ? clamp(z, min, max) : z: NaN passes through, since the AGX2 depth-never emulation relies on it. Internal (meta) shaders are skipped. The local-read variant already rejects depth-writing shaders.
  • On M5 the gl_FragDepth shader goes from 2 to 4 instructions (fmax_agx + fmin_agx; the NaN select folds away for gl_FragCoord.z). The discard, empty and colour shaders compile to the same code as before.
  • Spec: "Depth Clamping and Range Adjustment" in the fragment operations chapter: with depthClampEnable, z_f is clamped to [z_min, z_max] of the viewport used by the fragment.
Evidence

Rebased head (39eac16) vs aurora-wip 5932858, same J815, same CTS build:

caselist deqp-lists/dclamp-20261129.txt (128 cases)
awip (59328582164) 0 s: {'Pass': 76, 'NotSupported': 52} new GPU fault lines: 0
fdc2 (39eac16e18d) 0 s: {'Pass': 76, 'NotSupported': 52} new GPU fault lines: 0
fdc2 vs awip: 0 diffs, 0 regressions, 0 fixes
caselist deqp-lists/fragdepth-20261129.txt (45 cases)
awip (59328582164) 0 s: {'Pass': 27, 'NotSupported': 18} new GPU fault lines: 0
fdc2 (39eac16e18d) 0 s: {'Pass': 27, 'NotSupported': 18} new GPU fault lines: 0
fdc2 vs awip: 0 diffs, 0 regressions, 0 fixes
caselist deqp-lists/gate-20261128.txt (15958 cases)
awip (59328582164) 142 s: {'Pass': 11291, 'NotSupported': 4667} new GPU fault lines: 0
fdc2 (39eac16e18d) 126 s: {'Pass': 11291, 'NotSupported': 4667} new GPU fault lines: 0
fdc2 vs awip: 0 diffs, 0 regressions, 0 fixes

Before the rebase (82d9859 on #1346 head 2fe6cb3):

Amber probes (dEQP-VK.depth.* shaders, edited locally), base #1346 + zero_one: user gl_FragDepth = 1.7, clamp on, viewport [0.1, 0.9] -> stored 1.0 (want 0.9); gl_FragDepth = 0.9 into a [0.1, 0.5] viewport -> 0.9 (want 0.5); no shader depth write -> correct. With this change: 0.9 and 0.5.

Commands (gpu-perf tools; base and proposed installed as separate ICDs from the same build tree):

CASE_S=60 python3 tools/deqp-ab.py fdc1-gate deqp-lists/gate-20261128.txt base=<2fe6cb3338b> fdc1=<82d9859e1d4>
CASE_S=60 python3 tools/deqp-ab.py fdc1-dclamp deqp-lists/dclamp-20261129.txt base=<2fe6cb3338b> fdc1=<82d9859e1d4>
CASE_S=60 python3 tools/deqp-ab.py fdc1-fragdepth deqp-lists/fragdepth-20261129.txt base=<2fe6cb3338b> fdc1=<82d9859e1d4>
CASE_S=60 python3 tools/deqp-ab.py fdcz1-dcz deqp-lists/dcz-20261129.txt dcz1=<2fe6cb3338b + zero_one> fdcz1=<82d9859e1d4 + zero_one>
FAST=1 tools/mesa-ab.sh fdc1-tps a=<2fe6cb3338b> f=<82d9859e1d4>
for i in 1..6: VK_DRIVER_FILES=<base|this PR> vkdepth --reps 9      # alternating order, 3 runs base-first, 3 runs PR-first
caselist gate-20261128.txt (15958 cases)
base (2fe6cb3338b) 131 s: {'Pass': 11230, 'NotSupported': 4728} new GPU fault lines: 0
fdc1 (82d9859e1d4) 125 s: {'Pass': 11230, 'NotSupported': 4728} new GPU fault lines: 0
fdc1 vs base: 0 diffs, 0 regressions, 0 fixes

caselist dclamp-20261129.txt (128 cases)
base {'Pass': 76, 'NotSupported': 52}   fdc1 {'Pass': 76, 'NotSupported': 52}   0 diffs
caselist fragdepth-20261129.txt (45 cases)
base {'Pass': 27, 'NotSupported': 18}   fdc1 {'Pass': 27, 'NotSupported': 18}   0 diffs

caselist dcz-20261129.txt (63 cases)
dcz1  {'Fail': 3, 'Pass': 42, 'NotSupported': 18}
fdcz1 {'Fail': 1, 'Pass': 44, 'NotSupported': 18}
   dEQP-VK.depth.bias_fs_clamp: Fail -> Pass
   dEQP-VK.depth.fs_clamp: Fail -> Pass
   (dEQP-VK.depth.ez_fs_clamp: Fail in both)

TPS (Godot 4 TPS demo, 1080p, FSR2, FAST=1: one pass per arm, view 1 twice + the play route):

base BENCH view=1    frames=753  fps=50.2 ms_p50=19.93 gpu_p50=19.86
base BENCH view=1    frames=101  fps=50.1 ms_p50=20.09 gpu_p50=20.00
base BENCH view=play frames=1385 fps=57.6 ms_p50=17.26 gpu_p50=17.08
fdc1 BENCH view=1    frames=746  fps=49.7 ms_p50=20.07 gpu_p50=19.93
fdc1 BENCH view=1    frames=99   fps=49.3 ms_p50=20.04 gpu_p50=20.06
fdc1 BENCH view=play frames=1384 fps=57.6 ms_p50=17.22 gpu_p50=17.09

The TPS frame has no depth-writing fragment shader, so it only checks that nothing else moved (GPU p50 +0.35%, inside the spread between runs of one build).

vkdepth (depth-only, D32, 1920x1080, GPU timestamps around one render pass, median of 9 timed reps after 2 warm-ups per run; table = median over 6 runs per build). fragz is the shape this change touches:

#version 450
void main() { gl_FragDepth = gl_FragCoord.z; }
test case base ms this PR ms change runs
fill-b2f nofs 0.129 0.129 -0.39% 6 + 6
fill-b2f empty 0.129 0.129 +0.00% 6 + 6
fill-b2f discard 1.232 1.230 -0.16% 6 + 6
fill-b2f fragz 2.292 2.290 -0.07% 6 + 6
fill-b2f color 0.129 0.128 -0.39% 6 + 6
fill-f2b nofs 0.130 0.129 -0.77% 6 + 6
fill-f2b empty 0.129 0.129 +0.00% 6 + 6
fill-f2b discard 0.367 0.371 +1.23% 6 + 6
fill-f2b fragz 2.290 2.292 +0.04% 6 + 6
tri128 nofs 0.907 0.908 +0.11% 6 + 6
tri128 empty 0.823 0.829 +0.67% 6 + 6
tri128 discard 1.139 1.141 +0.18% 6 + 6
tri128 fragz 1.381 1.384 +0.22% 6 + 6
tri256 nofs 1.529 1.526 -0.23% 6 + 6
tri256 empty 1.304 1.302 -0.19% 6 + 6
tri256 discard 1.440 1.439 -0.07% 6 + 6
tri256 fragz 1.647 1.649 +0.15% 6 + 6
tri512 nofs 2.804 2.806 +0.09% 6 + 6
tri512 empty 2.346 2.349 +0.15% 6 + 6
tri512 discard 2.602 2.604 +0.06% 6 + 6
tri512 fragz 3.005 3.003 -0.05% 6 + 6

fill-f2b discard (+1.2%) compiles to the same fragment shader in both builds; a further single run read 0.364 ms with this PR against 0.365 ms base.

With depth clamp enabled, Vulkan clamps the fragment depth to
[min(n, f), max(n, f)] of the viewport, or to the
VK_EXT_depth_clamp_control range. The viewport z bounds in the scissor
records clamp only the interpolated depth. A depth value the fragment
shader writes was clamped only by the attachment format: writing 1.7
with depth clamp on stored 1.0 instead of the viewport's 0.9, and
writing 0.9 into a [0.1, 0.5] viewport stored 0.9.

Pass the draw's range in the root table (fs_depth_clamp: the clamp
range with depth clamp on, the union over the viewports when there are
several; [0, 1] with it off) and clamp every FRAG_RESULT_DEPTH store
to it before it becomes the ZS store. NaN passes through unchanged,
since the AGX2 depth-never emulation relies on it. Shaders that do not
write depth are unchanged.

Signed-off-by: Nick <nekreshen@yandex.ru>
@m0d16l14n1
m0d16l14n1 force-pushed the j815/fragdepth-viewport-clamp branch from 82d9859 to 39eac16 Compare November 30, 2026 00:35
@m0d16l14n1
m0d16l14n1 marked this pull request as ready for review November 30, 2026 00:35
@m0d16l14n1
m0d16l14n1 requested review from a team and DjDeveloperr as code owners November 30, 2026 00:35

@aurora-silicon-review aurora-silicon-review Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Verdict: MERGE at 39eac16e

Blockers:

  • None.

Checked:

  • src/asahi/vulkan/hk_cmd_buffer.h: appends float fs_depth_clamp[2] after mesh_whole outside the cs/draw union, preserving sizeof(cs) <= sizeof(draw) and 4-byte alignment matching .align_mul = 4.
  • src/asahi/vulkan/hk_cmd_draw.c: hk_fs_depth_clamp_range() computes the union of hk_depth_clamp_bounds() across MAX2(dyn->vp.viewport_count, 1) viewports ([0, 1] when depth clamp is disabled, user-defined range for VK_DEPTH_CLAMP_MODE_USER_DEFINED_RANGE_EXT, or [min(minDepth, maxDepth), max(minDepth, maxDepth)]). Updated in hk_flush_dynamic_state_slow() on VP_VIEWPORTS, VP_VIEWPORT_COUNT, RS_DEPTH_CLAMP_ENABLE, and VP_DEPTH_CLAMP_RANGE with memcmp/hk_root_mark_dirty(). Verified hk_cmd_buffer_dirty_all() initializes all dynamic dirty bits and hk_g17p_flush_dynamic_state_fast() falls back to the slow path whenever any dynamic bit beyond RS_CULL_MODE/RS_FRONT_FACE is dirty.
  • src/asahi/vulkan/hk_shader.c: hk_nir_clamp_frag_depth() skips internal shaders and shaders without FRAG_RESULT_DEPTH, loads fs_depth_clamp from the root table, converts via nir_f2fN(), clamps with fmax/fmin while preserving NaN (nir_feq(z, z)) for AGX2 depth-never emulation, and runs in hk_compile_nir_impl() before agx_nir_lower_discard_zs_emit() and root-footprint measurement.
  • Hardware & unit verification: exact rebased head 39eac16e18d vs aurora-wip 59328582164 on J815 (M5, G17G) PASS (dEQP gate 11,291 = 11,291, depth clamp 76 = 76, fragdepth 27 = 27, 0 diffs, 0 GPU faults); identical patch pre-rebase fixes dEQP-VK.depth.fs_clamp and dEQP-VK.depth.bias_fs_clamp, meson test --suite asahi 223/223, vkdepth fragz and Godot TPS neutral.
Evidence
  • Diff: +88/-0 across 3 files, 1 commit (39eac16e18d1), DCO present, MERGEABLE on aurora-wip.
  • Multi-viewport union limitation and separate depth.ez_fs_clamp behavior are clearly documented in the PR description.

@RyanTheTide

Copy link
Copy Markdown

@DjDeveloperr, code-owner heads-up: App approval at 39eac16e18d1 from a different lane. This PR goes into the merge train's next combined A/B (aurora-wip 5e89c89c + #1356 #1363) on M1 Pro G13S + M4 G16G. If that row is clean, I plan to land it via --admin under the 6-point rule no earlier than one hour from now, unless you post a hold.

@RyanTheTide

Copy link
Copy Markdown

Merge-train landing at 39eac16e18d1.

  • Review: aurora-silicon-review App APPROVE at 39eac16 (non-author).
  • Feature row: the author's exact-head hardware row on this PR.
  • Regression: combined A/B overlay of aurora-wip plus this PR and the other queued PRs, user space, default kernel command line. comb16 90c0e25c (aurora-wip 5e89c89c + borealis: clamp shader-written depth to the depth clamp range #1356 docs/asahi: mark G15G Vulkan colour compression merged #1363) vs 5e89c89c. M1 Pro (G13S, aurora-edge): 0 status changes. M4 (G16G, fleet85): 0 status changes, and the fleet85 DeviceLost baseline is identical on both arms. 0 GPU firmware crashes. Host 331/357 (base Gallium run-shim set). Each Mac ran 26,595 dEQP-VK + 8,153 dEQP-GLES cases, with 0 GPU firmware crashes, Enforcing, 0 failed units. Evidence traces/gpu/20261128-mergetrain-mesa.
  • --admin under the 6-point rule: the App approval does not count toward the ruleset. Dj was pinged here more than 1 h ago, and no blocking review is open.

Revert: git revert -m 1 <merge commit> on aurora-wip. A regression gets reverted first and fixed after.

@RyanTheTide
RyanTheTide merged commit b6781fc into aurora-silicon:aurora-wip Nov 30, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants