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
493 changes: 266 additions & 227 deletions src/asahi/vulkan/bvh/agx3_m5_legacy_leaf.comp

Large diffs are not rendered by default.

14 changes: 14 additions & 0 deletions src/asahi/vulkan/bvh/agx3_m5_legacy_plan.comp
Original file line number Diff line number Diff line change
Expand Up @@ -341,6 +341,18 @@ emit(uint id)
}
}

/* The node encoding dispatch (VkDispatchIndirectCommand): whole workgroups
* of HK_AGX3_NODE_JOBS_PER_GROUP jobs. */
void
write_encode_dispatch(uint jobs)
{
uint64_t dispatch = uint64_t(args.state) + uint64_t(HK_AGX3_PLAN_ENCODE_DISPATCH);
DEREF(REF(uint32_t)(dispatch)) =
(jobs + HK_AGX3_NODE_JOBS_PER_GROUP - 1u) / HK_AGX3_NODE_JOBS_PER_GROUP;
DEREF(REF(uint32_t)(dispatch + 4ul)) = 1u;
DEREF(REF(uint32_t)(dispatch + 8ul)) = 1u;
}

void
finish(uint id)
{
Expand All @@ -366,6 +378,7 @@ finish(uint id)
DEREF(args.envelope) = job;
if (DEREF(args.state).error != 0)
DEREF(args.state).job_count = 0;
write_encode_dispatch(DEREF(args.state).job_count);
}

/* A single active common leaf collapses to the same one-job native cluster
Expand Down Expand Up @@ -426,6 +439,7 @@ plan_single_leaf()
envelope.leaf_bound = 1;
DEREF(args.envelope) = envelope;
DEREF(args.state) = state;
write_encode_dispatch(state.job_count);
}

void
Expand Down
3 changes: 2 additions & 1 deletion src/asahi/vulkan/bvh/agx3_m5_legacy_refit.comp
Original file line number Diff line number Diff line change
Expand Up @@ -146,7 +146,8 @@ void prefix()
sum += DEREF(REF(hk_agx3_plan_state)(task.plan_state)).job_count;
}
DEREF(INDEX(uint32_t, batch.bases, batch.count)) = sum;
DEREF(INDEX(uint32_t, batch.dispatch, 0)) = sum;
DEREF(INDEX(uint32_t, batch.dispatch, 0)) =
(sum + HK_AGX3_NODE_JOBS_PER_GROUP - 1u) / HK_AGX3_NODE_JOBS_PER_GROUP;
DEREF(INDEX(uint32_t, batch.dispatch, 1)) = 1;
DEREF(INDEX(uint32_t, batch.dispatch, 2)) = 1;
}
Expand Down
4 changes: 3 additions & 1 deletion src/asahi/vulkan/bvh/hk_build_interface.h
Original file line number Diff line number Diff line change
Expand Up @@ -341,7 +341,9 @@ struct hk_m5_refit_batch_args {

/* One node encoding dispatch for the updates of a build call
* (agx3_m5_legacy_leaf.comp built with HK_M5_LEAF_BATCHED): workgroup w
* encodes job w - bases[u] of update u, bases[u] <= w < bases[u + 1]. */
* encodes the HK_AGX3_NODE_JOBS_PER_GROUP jobs from j = w *
* HK_AGX3_NODE_JOBS_PER_GROUP, each job j - bases[u] of the update u with
* bases[u] <= j < bases[u + 1]. */
struct hk_m5_leaf_task {
VOID_REF jobs;
REF(uint32_t) status;
Expand Down
9 changes: 4 additions & 5 deletions src/asahi/vulkan/hk_agx3_as_format.h
Original file line number Diff line number Diff line change
Expand Up @@ -39,11 +39,10 @@ hk_agx3_as_format_for_chip(enum agx_chip chip)
#define HK_AGX3_GEN1_IMAGE_ALIGN 16384u
#define HK_AGX3_M5_IMAGE_ALIGN 1024u

/* Encoding scratch after hk_agx3_plan_state's offset: GEN1 keeps the node
* encoding dispatch arguments (HK_AGX3_PLAN_ENCODE_DISPATCH) there, M5 only
* the plan state. */
/* Encoding scratch after hk_agx3_plan_state's offset: the plan state, then
* the node encoding dispatch arguments (HK_AGX3_PLAN_ENCODE_DISPATCH). */
#define HK_AGX3_GEN1_PLAN_TAIL_B 48u
#define HK_AGX3_M5_PLAN_TAIL_B 32u
#define HK_AGX3_M5_PLAN_TAIL_B 48u

/* Bounding-box leaf record size. The M5 record size,
* HK_AGX3_M5_AABB_RECORD_B, is defined with its layout in
Expand All @@ -52,7 +51,7 @@ hk_agx3_as_format_for_chip(enum agx_chip chip)

/* Lanes of one node-encoding workgroup. */
#define HK_AGX3_GEN1_NODE_GROUP_LANES 32u
#define HK_AGX3_M5_NODE_GROUP_LANES 8u
#define HK_AGX3_M5_NODE_GROUP_LANES 32u

static inline unsigned
hk_agx3_as_image_align(enum hk_agx3_as_format format)
Expand Down
25 changes: 16 additions & 9 deletions src/asahi/vulkan/hk_m5_acceleration_structure.c
Original file line number Diff line number Diff line change
Expand Up @@ -348,19 +348,23 @@ static_assert(sizeof(struct hk_bvh_instance_node) == 120, "node format");
static_assert(sizeof(struct hk_bvh_box_node) == 56, "node format");
static_assert(sizeof(struct hk_agx3_leaf_job) == 64, "native node job ABI");
/* hk_build_interface.h is shared with the GEN1 encoders, whose leaf and plan
* arguments end in fields (counts, leaf_bound; pairs) that M5 leaves zero and
* its encoders never read. */
* arguments end in fields (counts, leaf_bound; pairs). M5 node encoding reads
* counts only, to bound its last workgroup; it leaves the others zero. */
static_assert(sizeof(struct hk_agx3_leaf_args) == 40, "native node push ABI");
static_assert(sizeof(struct hk_agx3_envelope_job) == 48, "native envelope job ABI");
static_assert(sizeof(struct hk_agx3_envelope_args) == 64, "native envelope push ABI");
static_assert(sizeof(struct hk_agx3_plan_node) == 16, "native planner node ABI");
static_assert(sizeof(struct hk_agx3_plan_state) == 32, "native planner state ABI");
static_assert(sizeof(struct hk_agx3_plan_args) == 120, "native planner push ABI");
/* The M5 layout: the plan state alone after its scratch offset, 48-byte
* bounding-box records within the 64-byte child slots, 1 KiB images, and one
* node per 8-lane encoding workgroup (bvh/agx3_leaf.comp). */
static_assert(HK_AGX3_M5_PLAN_TAIL_B == sizeof(struct hk_agx3_plan_state),
"M5 scratch: no node-encoding dispatch arguments");
/* The M5 layout: the plan state, then the node encoding dispatch arguments
* the plan writes for the indirect encode (bvh/agx3_m5_legacy_plan.comp),
* 48-byte bounding-box records within the 64-byte child slots, 1 KiB images,
* and four 8-lane node jobs per encoding workgroup
* (bvh/agx3_m5_legacy_leaf.comp). */
static_assert(HK_AGX3_PLAN_ENCODE_DISPATCH == sizeof(struct hk_agx3_plan_state) &&
HK_AGX3_M5_PLAN_TAIL_B >= HK_AGX3_PLAN_ENCODE_DISPATCH +
sizeof(VkDispatchIndirectCommand),
"M5 scratch: plan state, then the encode dispatch arguments");
struct hk_agx3_m5_aabb_record {
uint32_t marker; /* leaf type 24 */
float min[3];
Expand All @@ -380,7 +384,8 @@ static_assert(sizeof(struct hk_agx3_m5_aabb_record) == HK_AGX3_M5_AABB_RECORD_B
HK_AGX3_M5_AABB_RECORD_B < HK_AGX3_AS_SLOT_B,
"M5 bounding-box records pack below the slot size");
static_assert(HK_AGX3_M5_IMAGE_ALIGN == 1024, "M5 image alignment");
static_assert(HK_AGX3_M5_NODE_GROUP_LANES == 8, "M5 encoder workgroup");
static_assert(HK_AGX3_M5_NODE_GROUP_LANES == 8 * HK_AGX3_NODE_JOBS_PER_GROUP,
"M5 encoder workgroup");
static_assert(sizeof(struct hk_bvh_box_node) == HK_BVH_NODE_PREFETCH_SIZE,
"the traversal prefetches a whole box node before it knows the "
"node type, so no node may be larger than one");
Expand Down Expand Up @@ -492,6 +497,7 @@ hk_cmd_build_agx3_from_ir_mode(VkCommandBuffer command_buffer,

const struct hk_agx3_leaf_args encode = {
.jobs = args->jobs, .status = node_status, .job_count = args->node_bound,
.counts = args->state,
};
const struct hk_agx3_envelope_args finish = {
.jobs = args->envelope, .status = envelope_status, .job_count = 1,
Expand All @@ -509,7 +515,8 @@ hk_cmd_build_agx3_from_ir_mode(VkCommandBuffer command_buffer,
if (envelope)
hk_cmd_dispatch_unaligned(command_buffer, 1, 1, 1);
else
hk_cmd_dispatch_indirect_addr(command_buffer, args->state);
hk_cmd_dispatch_indirect_addr(
command_buffer, args->state + HK_AGX3_PLAN_ENCODE_DISPATCH);
if (step == 0)
vk_common_CmdPipelineBarrier(command_buffer,
VK_PIPELINE_STAGE_COMPUTE_SHADER_BIT,
Expand Down
12 changes: 5 additions & 7 deletions src/asahi/vulkan/tests/test-hk-agx3-as-format.c
Original file line number Diff line number Diff line change
Expand Up @@ -40,9 +40,8 @@ check_layout(enum hk_agx3_as_format format, uint32_t leaves,
const struct hk_agx3_as_layout *l)
{
const uint32_t align = format == HK_AGX3_AS_FORMAT_M5 ? 1024 : 16384;
const uint32_t tail = format == HK_AGX3_AS_FORMAT_M5 ? 32 : 48;
assert(hk_agx3_as_image_align(format) == align);
assert(hk_agx3_as_plan_tail(format) == tail);
assert(hk_agx3_as_plan_tail(format) == 48);
if (!leaves) {
assert(l->slot_bound == 3 && l->encode_bytes == 0);
assert(l->as_bytes == 192 + (align == 16384 ? 0 : align - 1));
Expand All @@ -53,8 +52,8 @@ check_layout(enum hk_agx3_as_format format, uint32_t leaves,
assert(l->node_bound == nodes && l->slot_bound == slots);
assert(l->jobs == nodes * 16 && l->envelope == l->jobs + nodes * 64);
assert(l->state == l->envelope + 48);
/* GEN1's plan writes the indirect encode dispatch after its state. */
assert(l->node_status == l->state + tail);
/* The plan writes the indirect encode dispatch after its state. */
assert(l->node_status == l->state + 48);
assert(l->envelope_status == l->node_status + nodes * 4);
const uint64_t refit_levels = (l->envelope_status + 4 + 7) & ~7u;
assert(l->refit_levels == refit_levels);
Expand Down Expand Up @@ -87,8 +86,7 @@ main(void)
check_layout(format, leaves[i], &l);
}

/* The node encoder: one node per 8 lanes on M5, four 8-lane jobs per
* workgroup on GEN1. */
/* The node encoder: four 8-lane jobs per workgroup. */
size_t size_B;
const uint32_t *encoder = hk_agx3_node_encoder_spirv(chip, &size_B);
assert(encoder && spirv_local_size_x(encoder, size_B) ==
Expand Down Expand Up @@ -190,7 +188,7 @@ main(void)
/* The layouts differ exactly where the formats do. */
struct hk_agx3_as_layout g, m;
assert(hk_gen1_as_layout_init(64, &g) && hk_agx3_as_layout_init(64, &m));
assert(g.state == m.state && g.node_status == m.node_status + 16);
assert(g.state == m.state && g.node_status == m.node_status);
assert(m.as_bytes == g.as_bytes + 1023);

printf("hk_agx3_as_format: %u M5 and %u GEN1 chips, each with its own "
Expand Down