Skip to content
Merged
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
56 changes: 28 additions & 28 deletions src/asahi/vulkan/hk_shader.c
Original file line number Diff line number Diff line change
Expand Up @@ -195,7 +195,7 @@ static_assert(sizeof(struct hk_vs_key) == 260, "packed");

struct hk_mesh_key {
/** An M5 mesh shader compiled without a fragment shader: write a zero
* gl_PrimitiveLayerEXT for every primitive (hk_m5_mesh_default_layer()).
* layer for every vertex (hk_m5_mesh_default_layer()).
*/
bool default_layer;
};
Expand Down Expand Up @@ -5012,26 +5012,28 @@ hk_store_writes_slot(const nir_intrinsic_instr *intr, gl_varying_slot slot)
* gl_PrimitiveLayerEXT nor gl_ViewportIndex has no such word. A vertex shader
* compiled without its fragment shader gets a zero layer output for that
* reason (agx_nir_lower_default_layer()). Give an M5 mesh shader compiled
* without its fragment shader the same: store gl_PrimitiveLayerEXT = 0 for
* every primitive the workgroup may output, as an application would, so that
* agx_m5_mesh_gather_varyings() and agx_nir_lower_m5_mesh() treat it like an
* application-written per-primitive layer. The stores come first in main,
* where every invocation of the workgroup executes them. A written viewport
* already exports a zero layer word.
* without its fragment shader the same: a zero layer for every vertex the
* workgroup may output. The layer is the same for the whole workgroup, so it
* takes the form hk_lower_multiview() gives the view index (vertex_layer in
* agx_nir_lower_m5_mesh()): a vertex word, which keeps the shader's shared
* vertices. A per-primitive layer would give every primitive private
* vertices. The stores come first in main, where every invocation of the
* workgroup executes them. A written viewport already exports a zero layer
* word.
*
* Runs on lowered I/O, after nir_lower_io_to_scalar(), and returns whether
* it added the stores.
*/
static bool
hk_m5_mesh_default_layer(nir_shader *nir)
{
const unsigned prims = nir->info.mesh.max_primitives_out;
const unsigned verts = nir->info.mesh.max_vertices_out;
const unsigned threads = nir->info.workgroup_size[0] *
nir->info.workgroup_size[1] *
nir->info.workgroup_size[2];
if (nir->info.stage != MESA_SHADER_MESH ||
(nir->info.outputs_written & (VARYING_BIT_LAYER | VARYING_BIT_VIEWPORT)) ||
nir->info.workgroup_size_variable || prims == 0 || threads == 0)
nir->info.workgroup_size_variable || verts == 0 || threads == 0)
return false;

nir_function_impl *impl = nir_shader_get_entrypoint(nir);
Expand All @@ -5046,29 +5048,27 @@ hk_m5_mesh_default_layer(nir_shader *nir)
}
}

/* Invocation i stores primitives i, i + threads, ... below prims. */
/* Invocation i stores vertices i, i + threads, ... below verts. */
nir_builder b = nir_builder_at(nir_before_impl(impl));
nir_def *lane = nir_load_local_invocation_index(&b);
for (unsigned first = 0; first < prims; first += threads) {
nir_def *prim = nir_iadd_imm(&b, lane, first);
for (unsigned first = 0; first < verts; first += threads) {
nir_def *vertex = nir_iadd_imm(&b, lane, first);
nir_if *partial = NULL;
if (first + threads > prims)
partial = nir_push_if(&b, nir_ult_imm(&b, prim, prims));
/* nir_lower_io's form of a gl_PrimitiveLayerEXT store. */
nir_store_per_primitive_output(&b, nir_imm_int(&b, 0), prim,
nir_imm_int(&b, 0), .base = 0,
.range = 1, .write_mask = 1,
.component = 0,
.src_type = nir_type_int32,
.io_semantics = {
.location = VARYING_SLOT_LAYER,
.num_slots = 1});
if (first + threads > verts)
partial = nir_push_if(&b, nir_ult_imm(&b, vertex, verts));
/* nir_lower_io's form of a per-vertex gl_Layer store. */
nir_store_per_vertex_output(&b, nir_imm_int(&b, 0), vertex,
nir_imm_int(&b, 0), .base = 0, .range = 1,
.write_mask = 1, .component = 0,
.src_type = nir_type_uint32,
.io_semantics = {
.location = VARYING_SLOT_LAYER,
.num_slots = 1});
if (partial)
nir_pop_if(&b, partial);
}

nir->info.outputs_written |= VARYING_BIT_LAYER;
nir->info.per_primitive_outputs |= VARYING_BIT_LAYER;
return nir_progress(true, impl, nir_metadata_none);
}

Expand Down Expand Up @@ -10133,9 +10133,9 @@ hk_compile_shader_impl(struct hk_device *dev, struct vk_shader_compile_info *inf
NIR_PASS(_, nir, agx_nir_lower_cull_distance_vs);
}
const bool m5_mesh = agx_chip_g17_m5(dev->dev.chip);
/* The layer word gives every primitive private vertices, which must
* fit the native output limits (agx_nir_lower_m5_mesh()). Keep the
* shader as it was to compile instead where they do not. */
/* The layer word must fit the native output limits
* (agx_nir_lower_m5_mesh()). Keep the shader as it was to compile
* instead where it does not. */
nir_shader *m5_without_layer = NULL;
if (m5_mesh && key_tmp.mesh.default_layer) {
m5_without_layer = nir_shader_clone(NULL, nir);
Expand Down Expand Up @@ -10275,7 +10275,7 @@ hk_compile_shader_impl(struct hk_device *dev, struct vk_shader_compile_info *inf
const bool m5_default_layer = m5_without_layer != NULL;
ralloc_free(m5_without_layer);
if (m5_default_layer)
shader->info.mesh.primitive_outputs |= VARYING_BIT_LAYER;
shader->info.mesh.vertex_outputs |= VARYING_BIT_LAYER;
/* A twin compiles with what this shader compiled with. */
if (obj->border_source)
obj->border_source->key.mesh.default_layer = m5_default_layer;
Expand Down