Repository navigation
borealis: clamp shader-written depth to the depth clamp range - #1356
Merged
RyanTheTide merged 1 commit intoNov 30, 2026
Merged
RyanTheTide merged 1 commit into
RyanTheTide merged 1 commit into
Conversation
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
force-pushed
the
j815/fragdepth-viewport-clamp
branch
from
November 30, 2026 00:35
82d9859 to
39eac16
Compare
m0d16l14n1
marked this pull request as ready for review
November 30, 2026 00:35
There was a problem hiding this comment.
Verdict: MERGE at 39eac16e
Blockers:
- None.
Checked:
src/asahi/vulkan/hk_cmd_buffer.h: appendsfloat fs_depth_clamp[2]aftermesh_wholeoutside thecs/drawunion, preservingsizeof(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 ofhk_depth_clamp_bounds()acrossMAX2(dyn->vp.viewport_count, 1)viewports ([0, 1]when depth clamp is disabled, user-defined range forVK_DEPTH_CLAMP_MODE_USER_DEFINED_RANGE_EXT, or[min(minDepth, maxDepth), max(minDepth, maxDepth)]). Updated inhk_flush_dynamic_state_slow()onVP_VIEWPORTS,VP_VIEWPORT_COUNT,RS_DEPTH_CLAMP_ENABLE, andVP_DEPTH_CLAMP_RANGEwithmemcmp/hk_root_mark_dirty(). Verifiedhk_cmd_buffer_dirty_all()initializes all dynamic dirty bits andhk_g17p_flush_dynamic_state_fast()falls back to the slow path whenever any dynamic bit beyondRS_CULL_MODE/RS_FRONT_FACEis dirty.src/asahi/vulkan/hk_shader.c:hk_nir_clamp_frag_depth()skips internal shaders and shaders withoutFRAG_RESULT_DEPTH, loadsfs_depth_clampfrom the root table, converts vianir_f2fN(), clamps withfmax/fminwhile preserving NaN (nir_feq(z, z)) for AGX2 depth-never emulation, and runs inhk_compile_nir_impl()beforeagx_nir_lower_discard_zs_emit()and root-footprint measurement.- Hardware & unit verification: exact rebased head
39eac16e18dvsaurora-wip59328582164on J815 (M5, G17G) PASS (dEQP gate11,291 = 11,291, depth clamp76 = 76,fragdepth27 = 27,0diffs,0GPU faults); identical patch pre-rebase fixesdEQP-VK.depth.fs_clampanddEQP-VK.depth.bias_fs_clamp,meson test --suite asahi223/223,vkdepthfragzand Godot TPS neutral.
Evidence
- Diff:
+88/-0across 3 files, 1 commit (39eac16e18d1), DCO present,MERGEABLEonaurora-wip. - Multi-viewport union limitation and separate
depth.ez_fs_clampbehavior are clearly documented in the PR description.
|
@DjDeveloperr, code-owner heads-up: App approval at |
|
Merge-train landing at
Revert: |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
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_sidefxadds to side-effecting shaders. Reuseshk_depth_clamp_boundsfrom #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,
fragdepth27 = 27, 0 diffs, 0 GPU faults. Before the rebase: J815 MacBook Air 15" M5 (T8142, G17G), Borealis, kernel7.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_clampanddepth.bias_fs_clampfixed).meson test --suite asahi223/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
fs_depth_clamp[2], appended aftermesh_whole. Set inhk_flush_dynamic_stateon 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 ofhk_compile_nir_impl()beforeagx_nir_lower_discard_zs_emit()turns the depth store intostore_zs_agx, and before the root footprint is measured, so the trimmed root covers the new field. It rewrites eachFRAG_RESULT_DEPTHstore toz == 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.gl_FragCoord.z). The discard, empty and colour shaders compile to the same code as before.Evidence
Rebased head (39eac16) vs aurora-wip 5932858, same J815, same CTS build:
Before the rebase (82d9859 on #1346 head 2fe6cb3):
Amber probes (
dEQP-VK.depth.*shaders, edited locally), base #1346 + zero_one: usergl_FragDepth = 1.7, clamp on, viewport [0.1, 0.9] -> stored 1.0 (want 0.9);gl_FragDepth = 0.9into 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):
TPS (Godot 4 TPS demo, 1080p, FSR2,
FAST=1: one pass per arm, view 1 twice + the play route):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).
fragzis the shape this change touches: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.