Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions src/asahi/vulkan/hk_cmd_buffer.h
Original file line number Diff line number Diff line change
Expand Up @@ -357,6 +357,13 @@ struct hk_root_descriptor_table {
* and fits one launch as it is (hk_mesh_launch_whole()), so the mesh
* workgroup IDs are the API's without reading the grid. */
uint32_t mesh_whole;

/* [min, max] for a depth value the fragment shader writes
* (hk_nir_clamp_frag_depth). The viewport z bounds clamp only the
* interpolated depth, so with depth clamp on this is the API clamp range
* (hk_depth_clamp_bounds, the union over all viewports when there are
* several), and [0, 1] with it off. */
float fs_depth_clamp[2];
};

/* The graphics arm is the larger of the two, so everything after the union --
Expand Down
30 changes: 30 additions & 0 deletions src/asahi/vulkan/hk_cmd_draw.c
Original file line number Diff line number Diff line change
Expand Up @@ -10881,6 +10881,26 @@ hk_depth_clamp_bounds(const struct vk_dynamic_graphics_state *dyn,
}
}

/* Range for a depth value the fragment shader writes (root fs_depth_clamp).
* The fragment shader does not know which viewport its primitive used, so with
* several viewports this is the union of their clamp ranges: exact for one
* viewport, for a user-defined clamp range, and for viewports sharing a depth
* range. */
static void
hk_fs_depth_clamp_range(const struct vk_dynamic_graphics_state *dyn,
float range[2])
{
const unsigned count = MAX2(dyn->vp.viewport_count, 1);

hk_depth_clamp_bounds(dyn, &dyn->vp.viewports[0], &range[0], &range[1]);
for (unsigned i = 1; i < count; ++i) {
float min_z, max_z;
hk_depth_clamp_bounds(dyn, &dyn->vp.viewports[i], &min_z, &max_z);
range[0] = MIN2(range[0], min_z);
range[1] = MAX2(range[1], max_z);
}
}

static void
hk_compute_vp_scissor_rects(const struct vk_dynamic_graphics_state *dyn,
uint32_t fb_width, uint32_t fb_height,
Expand Down Expand Up @@ -15084,6 +15104,16 @@ hk_flush_dynamic_state_slow(struct hk_cmd_buffer *cmd, struct hk_cs *cs,
}
}

if (IS_DIRTY(VP_VIEWPORTS) || IS_DIRTY(VP_VIEWPORT_COUNT) ||
IS_DIRTY(RS_DEPTH_CLAMP_ENABLE) || IS_DIRTY(VP_DEPTH_CLAMP_RANGE)) {
float clamp[2];
hk_fs_depth_clamp_range(dyn, clamp);
if (memcmp(desc->root.fs_depth_clamp, clamp, sizeof(clamp))) {
memcpy(desc->root.fs_depth_clamp, clamp, sizeof(clamp));
hk_root_mark_dirty(desc);
}
}

if (IS_DIRTY(VP_DEPTH_CLIP_NEGATIVE_ONE_TO_ONE)) {
const float clip_z_coeff =
dyn->vp.depth_clip_negative_one_to_one ? 0.5f : 0.0f;
Expand Down
51 changes: 51 additions & 0 deletions src/asahi/vulkan/hk_shader.c
Original file line number Diff line number Diff line change
Expand Up @@ -347,6 +347,54 @@ hk_fs_force_shader_depth_write(nir_shader *nir)
nir->info.outputs_written |= BITFIELD64_BIT(FRAG_RESULT_DEPTH);
}

static bool
hk_clamp_frag_depth_store(nir_builder *b, nir_intrinsic_instr *intr,
void *data)
{
if (intr->intrinsic != nir_intrinsic_store_output ||
nir_intrinsic_io_semantics(intr).location != FRAG_RESULT_DEPTH)
return false;

b->cursor = nir_before_instr(&intr->instr);
nir_def *value = intr->src[0].ssa;
nir_def *z = nir_channel(b, value, 0);
nir_def *range = nir_load_global_constant(
b, 2, 32,
nir_iadd_imm(b, nir_load_root_agx(b),
hk_root_descriptor_offset(fs_depth_clamp)),
.align_mul = 4, .access = ACCESS_CAN_SPECULATE);
range = nir_f2fN(b, range, z->bit_size);

/* NaN passes through: the AGX2 depth-never emulation relies on it. */
nir_def *clamped = nir_fmin(b, nir_fmax(b, z, nir_channel(b, range, 0)),
nir_channel(b, range, 1));
clamped = nir_bcsel(b, nir_feq(b, z, z), clamped, z);

nir_src_rewrite(&intr->src[0], nir_vector_insert_imm(b, value, clamped, 0));
return true;
}

/*
* Vulkan clamps the fragment depth to [min(n, f), max(n, f)] of the viewport
* (or the VK_EXT_depth_clamp_control range) when depth clamp is enabled. The
* viewport z bounds in the scissor records clamp only the interpolated depth:
* on G17G a fragment shader writing 1.7 with depth clamp on and the viewport
* depth range [0.1, 0.9] stored 1.0, and one writing 0.9 into [0.1, 0.5]
* stored 0.9. Clamp the written value in the shader to the draw's range
* (root fs_depth_clamp). This also covers the pass-through FragDepth
* agx_nir_lower_frag_sidefx adds for side-effecting shaders.
*/
static bool
hk_nir_clamp_frag_depth(nir_shader *nir)
{
if (nir->info.internal ||
!(nir->info.outputs_written & BITFIELD64_BIT(FRAG_RESULT_DEPTH)))
return false;

return nir_shader_intrinsics_pass(nir, hk_clamp_frag_depth_store,
nir_metadata_control_flow, NULL);
}

static bool
hk_cf_list_has_ray_query(struct exec_list *cf_list)
{
Expand Down Expand Up @@ -6628,6 +6676,9 @@ hk_compile_nir_impl(struct hk_device *dev, const VkAllocationCallbacks *pAllocat
shader->info.fs.interp.flat =
~(nir->info.linear_varyings | nir->info.perspective_varyings);

/* Before the depth store becomes store_zs_agx, and before the root
* footprint is measured below. */
NIR_PASS(_, nir, hk_nir_clamp_frag_depth);
/* Discards must be lowering before lowering MSAA to handle discards */
NIR_PASS(_, nir, agx_nir_lower_discard_zs_emit);
/* A shader that writes gl_SampleMask owns its coverage update. Apply
Expand Down