From 2fe6cb3338bee6bdceeee7b5a92bc63c331e552f Mon Sep 17 00:00:00 2001 From: Nick Date: Sun, 29 Nov 2026 08:48:35 +0300 Subject: [PATCH] borealis: implement VK_EXT_depth_clamp_control With depth clamp enabled, the hardware clamps depth to the min/max Z of each scissor record, which hk filled from the viewport depth range. Take them from the user range instead when the clamp mode is VK_DEPTH_CLAMP_MODE_USER_DEFINED_RANGE_EXT, in both the generic and the native bind-tail viewport paths (shared hk_depth_clamp_bounds()). The range is dynamic state (VP_DEPTH_CLAMP_RANGE): it re-emits the viewport records like the clamp enable does, and the bind-tail snapshot keeps the mode and range so a cached viewport payload is not reused across a range change. Without depthRangeUnrestricted the user range lies within [0, 1], so the clamp-disabled bounds are unchanged. Signed-off-by: Nick --- src/asahi/vulkan/hk_cmd_buffer.h | 2 + src/asahi/vulkan/hk_cmd_draw.c | 53 +++++++++++++++++++++------ src/asahi/vulkan/hk_physical_device.c | 4 ++ 3 files changed, 48 insertions(+), 11 deletions(-) diff --git a/src/asahi/vulkan/hk_cmd_buffer.h b/src/asahi/vulkan/hk_cmd_buffer.h index fc1d5b6ee201..dc1dc7e9b894 100644 --- a/src/asahi/vulkan/hk_cmd_buffer.h +++ b/src/asahi/vulkan/hk_cmd_buffer.h @@ -98,6 +98,8 @@ struct hk_g17p_bind_tail_dyn_state { uint32_t depth_bias_constant_raw; uint32_t depth_bias_clamp_raw; uint32_t depth_bias_slope_raw; + VkDepthClampRangeEXT depth_clamp_range; + VkDepthClampModeEXT depth_clamp_mode; uint8_t depth_bias_representation; uint8_t viewport_count; uint8_t scissor_count; diff --git a/src/asahi/vulkan/hk_cmd_draw.c b/src/asahi/vulkan/hk_cmd_draw.c index 07f7908470b9..4c6e9db474f2 100644 --- a/src/asahi/vulkan/hk_cmd_draw.c +++ b/src/asahi/vulkan/hk_cmd_draw.c @@ -10859,6 +10859,28 @@ hk_effective_viewport_count(struct hk_cmd_buffer *cmd) return MAX2(dyn->vp.viewport_count, 1); } +/* Hardware depth clamp bounds for one viewport. The PPP depth clamp flag is + * the inverse of depth clip (see hk_flush_ppp_state), so with API depth clamp + * off the bounds stay [0, 1]. VK_EXT_depth_clamp_control replaces the viewport + * range with a user range; without depthRangeUnrestricted it lies in [0, 1]. + */ +static inline void +hk_depth_clamp_bounds(const struct vk_dynamic_graphics_state *dyn, + const VkViewport *vp, float *min_z, float *max_z) +{ + if (!dyn->rs.depth_clamp_enable) { + *min_z = 0.0f; + *max_z = 1.0f; + } else if (dyn->vp.depth_clamp_mode == + VK_DEPTH_CLAMP_MODE_USER_DEFINED_RANGE_EXT) { + *min_z = dyn->vp.depth_clamp_range.minDepthClamp; + *max_z = dyn->vp.depth_clamp_range.maxDepthClamp; + } else { + *min_z = MIN2(vp->minDepth, vp->maxDepth); + *max_z = MAX2(vp->minDepth, vp->maxDepth); + } +} + static void hk_compute_vp_scissor_rects(const struct vk_dynamic_graphics_state *dyn, uint32_t fb_width, uint32_t fb_height, @@ -10957,12 +10979,8 @@ hk_flush_vp_state(struct hk_cmd_buffer *cmd, struct hk_cs *cs, uint8_t **out) * * TODO: Reverse-engineer the finer interactions here. */ - const float min_z = dyn->rs.depth_clamp_enable - ? MIN2(vp->minDepth, vp->maxDepth) - : 0.0f; - const float max_z = dyn->rs.depth_clamp_enable - ? MAX2(vp->minDepth, vp->maxDepth) - : 1.0f; + float min_z, max_z; + hk_depth_clamp_bounds(dyn, vp, &min_z, &max_z); uint8_t *record = scissors + i * AGX_SCISSOR_LENGTH; if (hal300) { @@ -11283,6 +11301,8 @@ hk_g17p_bind_tail_dyn_capture(const struct vk_dynamic_graphics_state *dyn, dyn->vp.depth_clip_negative_one_to_one; out->rasterizer_discard_enable = dyn->rs.rasterizer_discard_enable; out->depth_clamp_enable = dyn->rs.depth_clamp_enable; + out->depth_clamp_mode = dyn->vp.depth_clamp_mode; + out->depth_clamp_range = dyn->vp.depth_clamp_range; out->depth_bias_enable = dyn->rs.depth_bias.enable; out->depth_bias_exact = dyn->rs.depth_bias.exact; out->depth_test_enable = dyn->ds.depth.test_enable; @@ -11305,6 +11325,14 @@ hk_g17p_bind_tail_viewport_dyn_matches( snap->depth_bias_enable != dyn->rs.depth_bias.enable) return false; + if (dyn->rs.depth_clamp_enable && + (snap->depth_clamp_mode != dyn->vp.depth_clamp_mode || + (dyn->vp.depth_clamp_mode == + VK_DEPTH_CLAMP_MODE_USER_DEFINED_RANGE_EXT && + memcmp(&snap->depth_clamp_range, &dyn->vp.depth_clamp_range, + sizeof(snap->depth_clamp_range)) != 0))) + return false; + if (dyn->rs.depth_bias.enable && (snap->depth_bias_exact != dyn->rs.depth_bias.exact || snap->depth_bias_representation != dyn->rs.depth_bias.representation || @@ -11685,14 +11713,15 @@ hk_g17p_draw_state_viewports(struct hk_cmd_buffer *cmd, maxy = MIN2(maxy, s->offset.y + s->extent.height); } - const bool clamp = dyn->rs.depth_clamp_enable; + float min_z, max_z; + hk_depth_clamp_bounds(dyn, v, &min_z, &max_z); state->viewports[i] = (struct agx_hal200_viewport){ .min_x = minx, .min_y = miny, .max_x = maxx, .max_y = maxy, - .min_z_raw = fui(clamp ? MIN2(v->minDepth, v->maxDepth) : 0.0f), - .max_z_raw = fui(clamp ? MAX2(v->minDepth, v->maxDepth) : 1.0f), + .min_z_raw = fui(min_z), + .max_z_raw = fui(max_z), .transform = { .translate_x = fui(v->x + 0.5f * v->width), .scale_x = fui(v->width * 0.5f), @@ -15393,7 +15422,8 @@ hk_flush_dynamic_state_slow(struct hk_cmd_buffer *cmd, struct hk_cs *cs, IS_DIRTY(VP_SCISSORS) || IS_DIRTY(VP_SCISSOR_COUNT) || IS_DIRTY(VP_VIEWPORTS) || IS_DIRTY(VP_VIEWPORT_COUNT) || IS_DIRTY(VP_DEPTH_CLIP_NEGATIVE_ONE_TO_ONE) || - IS_DIRTY(RS_DEPTH_CLIP_ENABLE) || IS_DIRTY(RS_DEPTH_CLAMP_ENABLE)) { + IS_DIRTY(RS_DEPTH_CLIP_ENABLE) || IS_DIRTY(RS_DEPTH_CLAMP_ENABLE) || + IS_DIRTY(VP_DEPTH_CLAMP_RANGE)) { /* TODO: Do less often? */ hk_reserve_scratch(cmd, cs, hw_vs); @@ -15613,7 +15643,8 @@ hk_flush_dynamic_state_slow(struct hk_cmd_buffer *cmd, struct hk_cs *cs, IS_DIRTY(VP_SCISSOR_COUNT) || IS_DIRTY(VP_VIEWPORTS) || IS_DIRTY(VP_VIEWPORT_COUNT) || IS_DIRTY(VP_DEPTH_CLIP_NEGATIVE_ONE_TO_ONE) || - IS_DIRTY(RS_DEPTH_CLIP_ENABLE) || IS_DIRTY(RS_DEPTH_CLAMP_ENABLE))) { + IS_DIRTY(RS_DEPTH_CLIP_ENABLE) || IS_DIRTY(RS_DEPTH_CLAMP_ENABLE) || + IS_DIRTY(VP_DEPTH_CLAMP_RANGE))) { hk_flush_vp_state(cmd, cs, &out); if (vk_command_buffer_has_error(&cmd->vk)) { diff --git a/src/asahi/vulkan/hk_physical_device.c b/src/asahi/vulkan/hk_physical_device.c index bb369e533937..861f40acaf56 100644 --- a/src/asahi/vulkan/hk_physical_device.c +++ b/src/asahi/vulkan/hk_physical_device.c @@ -291,6 +291,7 @@ hk_get_device_extensions(const struct agx_device *dev, .EXT_custom_border_color = true, .EXT_debug_marker = true, .EXT_depth_bias_control = true, + .EXT_depth_clamp_control = true, /* Lowered in NIR (agx_nir_lower_clip_m1_1) via the shared root table on * AGX2, native AGX3, and AGX3C/HAL300 (M5: G17G/G17S/G17C); other HAL300 * encodings such as AGX3D (G18G) stay off until qualified. */ @@ -787,6 +788,9 @@ hk_get_device_features( .floatRepresentation = false, .depthBiasExact = true, + /* VK_EXT_depth_clamp_control */ + .depthClampControl = true, + /* VK_EXT_depth_clip_control */ .depthClipControl = supported_extensions->EXT_depth_clip_control,