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
27 changes: 27 additions & 0 deletions src/asahi/vulkan/bvh/agx3_m5_legacy_leaf.comp
Original file line number Diff line number Diff line change
Expand Up @@ -23,10 +23,19 @@
#include "hk_build_interface.h"

layout(local_size_x = 8) in;
#ifdef HK_M5_LEAF_BATCHED
/* The restored jobs of every update of a build call (hk_m5_leaf_batch_args). */
layout(push_constant) uniform CONSTS
{
hk_m5_leaf_batch_args batch;
};
hk_agx3_leaf_args args;
#else
layout(push_constant) uniform CONSTS
{
hk_agx3_leaf_args args;
};
#endif

shared vec3 vertices[24];
shared vec3 lower[8], upper[8];
Expand Down Expand Up @@ -209,7 +218,25 @@ encode_instance(hk_agx3_leaf_job job, uint lane)
void
main()
{
#ifdef HK_M5_LEAF_BATCHED
/* The last update whose first job is at or before this workgroup. */
uint w = gl_WorkGroupID.x, lo = 0, hi = batch.count;
while (hi - lo > 1) {
uint mid = (lo + hi) / 2;
if (DEREF(INDEX(uint32_t, batch.bases, mid)) <= w)
lo = mid;
else
hi = mid;
}
uint first = DEREF(INDEX(uint32_t, batch.bases, lo));
hk_m5_leaf_task task = DEREF(INDEX(hk_m5_leaf_task, batch.tasks, lo));
args.jobs = REF(hk_agx3_leaf_job)(task.jobs);
args.status = task.status;
args.job_count = DEREF(INDEX(uint32_t, batch.bases, lo + 1)) - first;
uint id = w - first, lane = gl_LocalInvocationID.x;
#else
uint id = gl_WorkGroupID.x, lane = gl_LocalInvocationID.x;
#endif
if (id >= args.job_count)
return;
hk_agx3_leaf_job job = DEREF(INDEX(hk_agx3_leaf_job, args.jobs, id));
Expand Down
135 changes: 112 additions & 23 deletions src/asahi/vulkan/bvh/agx3_m5_legacy_refit.comp
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
/* Copyright 2026 Aurora Silicon
* SPDX-License-Identifier: MIT
* Refit a retained binary topology without sorting or repartitioning.
* Refit a retained binary topology without sorting or repartitioning, and
* keep the build's plan so the refit re-encodes the same native nodes
* (see hk_m5_refit_args for the phases).
*/
#version 460
#extension GL_GOOGLE_include_directive : require
Expand All @@ -16,30 +18,38 @@
#include "hk_build_interface.h"

layout(local_size_x = 64) in;
#ifdef HK_M5_REFIT_BATCHED
layout(push_constant) uniform CONSTS { hk_m5_refit_batch_args batch; };
hk_m5_refit_args args;
#else
layout(push_constant) uniform CONSTS { hk_m5_refit_args args; };
#endif
struct refit_node { uint parent; uint arrivals; uint reserved[2]; };
TYPE(refit_node, 4);
uint source_index(uint child)

REF(uint32_t) parent_of(uint64_t plan, uint id)
{
uint offset = ir_id_to_offset(child);
return ir_id_to_type(child) == vk_ir_node_internal
? args.leaf_bound + (offset - args.root_offset) / SIZEOF(vk_ir_box_node)
: offset / (args.root_offset / args.leaf_bound);
return INDEX(uint32_t, plan + uint64_t(HK_M5_PLAN_PARENTS), id);
}
void main()

REF(hk_agx3_leaf_job) retained_job(uint64_t plan, uint id)
{
return INDEX(hk_agx3_leaf_job, plan + uint64_t(HK_M5_PLAN_JOBS(args.node_bound)), id);
}

void refit(uint id)
{
uint id = gl_GlobalInvocationID.x;
uint header_words = SIZEOF(vk_ir_header) / 4;
uint internal_bound = args.node_bound - args.leaf_bound;
if (args.phase <= 1) {
if (args.phase == HK_M5_REFIT_SAVE_IR || args.phase == HK_M5_REFIT_PREPARE) {
uint64_t retained_base = args.retained;
/* The M5 retained copy covers the whole internal node capacity. */
uint words = header_words + internal_bound * (SIZEOF(vk_ir_box_node) / 4);
if (id < words) {
uint64_t addr = id < header_words ? uint64_t(args.header) + uint64_t(id)*4ul
: args.intermediate_bvh + uint64_t(args.root_offset) + uint64_t(id-header_words)*4ul;
REF(uint32_t) retained = REF(uint32_t)(retained_base + uint64_t(id)*4ul);
if (args.phase == 0) DEREF(retained) = DEREF(REF(uint32_t)(addr));
if (args.phase == HK_M5_REFIT_SAVE_IR) DEREF(retained) = DEREF(REF(uint32_t)(addr));
else {
uint value = DEREF(retained);
if (id < 3) value = 0x7fffffffu;
Expand All @@ -48,25 +58,60 @@ void main()
DEREF(REF(uint32_t)(addr)) = value;
}
}
if (args.phase == 1 && id < args.node_bound) {
DEREF(INDEX(refit_node, args.parents, id)).parent = 0xffffffffu;
if (args.phase == HK_M5_REFIT_PREPARE && id < args.node_bound)
DEREF(INDEX(refit_node, args.parents, id)).arrivals = 0;
}
return;
}
if (args.phase == 2) {
if (id >= DEREF(args.header).ir_internal_node_count) return;
REF(vk_ir_box_node) node = REF(vk_ir_box_node)(args.intermediate_bvh +
uint64_t(args.root_offset) + uint64_t(id)*uint64_t(SIZEOF(vk_ir_box_node)));
for (uint c = 0; c < 2; ++c) {
uint child = DEREF(node).children[c];
if (child != VK_BVH_INVALID_NODE)
DEREF(INDEX(refit_node, args.parents, source_index(child))).parent = args.leaf_bound + id;

if (args.phase == HK_M5_REFIT_SAVE_PLAN) {
/* Planning set every node's parent; inactive leaves and the root keep
* the ~0 it started from. */
if (id < args.node_bound)
DEREF(parent_of(args.plan, id)) =
DEREF(INDEX(hk_agx3_plan_node, args.plan_nodes, id)).parent;
hk_agx3_plan_state state = DEREF(REF(hk_agx3_plan_state)(args.plan_state));
if (id < state.job_count)
DEREF(retained_job(args.plan, id)) =
DEREF(INDEX(hk_agx3_leaf_job, args.plan_jobs, id));
if (id == 0) {
DEREF(REF(hk_agx3_plan_state)(args.plan + uint64_t(HK_M5_PLAN_STATE))) = state;
hk_agx3_envelope_job envelope =
DEREF(REF(hk_agx3_envelope_job)(args.plan_envelope));
envelope.bounds = REF(vk_ir_node)(uint64_t(envelope.bounds) - args.intermediate_bvh);
DEREF(REF(hk_agx3_envelope_job)(args.plan + uint64_t(HK_M5_PLAN_ENVELOPE))) = envelope;
}
return;
}

if (args.phase == HK_M5_REFIT_COPY_PLAN) {
if (id < HK_M5_PLAN_BYTES(args.node_bound) / 4u)
DEREF(INDEX(uint32_t, args.plan_copy, id)) = DEREF(INDEX(uint32_t, args.plan, id));
return;
}

/* REFIT: restore the plan with this update's addresses. */
hk_agx3_plan_state state =
DEREF(REF(hk_agx3_plan_state)(args.plan + uint64_t(HK_M5_PLAN_STATE)));
if (id < state.job_count) {
hk_agx3_leaf_job job = DEREF(retained_job(args.plan, id));
job.intermediate_bvh = args.intermediate_bvh;
job.output_bvh = args.output_bvh;
DEREF(INDEX(hk_agx3_leaf_job, args.plan_jobs, id)) = job;
}
if (id == 0) {
DEREF(REF(hk_agx3_plan_state)(args.plan_state)) = state;
hk_agx3_envelope_job envelope = DEREF(
REF(hk_agx3_envelope_job)(args.plan + uint64_t(HK_M5_PLAN_ENVELOPE)));
envelope.bounds = REF(vk_ir_node)(args.intermediate_bvh + uint64_t(envelope.bounds));
envelope.output_bvh = args.output_bvh;
envelope.resource_id = args.resource_id;
DEREF(REF(hk_agx3_envelope_job)(args.plan_envelope)) = envelope;
}

/* Then each leaf refits its ancestors; the last child to arrive at a
* node computes its bounds. */
if (id >= args.leaf_bound) return;
uint parent = DEREF(INDEX(refit_node, args.parents, id)).parent;
uint parent = DEREF(parent_of(args.plan, id));
for (uint steps = 0; parent != 0xffffffffu && steps < args.node_bound; ++steps) {
REF(vk_ir_box_node) node = REF(vk_ir_box_node)(args.intermediate_bvh +
uint64_t(args.root_offset) + uint64_t(parent-args.leaf_bound)*uint64_t(SIZEOF(vk_ir_box_node)));
Expand All @@ -85,6 +130,50 @@ void main()
bounds.max = max(bounds.max, DEREF(src).aabb.max);
}
DEREF(node).base.aabb = bounds;
parent = DEREF(INDEX(refit_node, args.parents, parent)).parent;
parent = DEREF(parent_of(args.plan, parent));
}
}

#ifdef HK_M5_REFIT_BATCHED
/* The node encoding of all updates: each update's restored jobs follow the
* jobs of the updates before it. */
void prefix()
{
uint sum = 0;
for (uint u = 0; u < batch.count; ++u) {
DEREF(INDEX(uint32_t, batch.bases, u)) = sum;
hk_m5_refit_args task = DEREF(INDEX(hk_m5_refit_args, batch.tasks, u));
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, 1)) = 1;
DEREF(INDEX(uint32_t, batch.dispatch, 2)) = 1;
}
#endif

void main()
{
#ifdef HK_M5_REFIT_BATCHED
if (batch.phase == HK_M5_REFIT_PREFIX) {
if (gl_GlobalInvocationID.x == 0)
prefix();
return;
}
/* The last update whose first workgroup is at or before this one. */
uint g = gl_WorkGroupID.x, lo = 0, hi = batch.count;
while (hi - lo > 1) {
uint mid = (lo + hi) / 2;
if (DEREF(INDEX(uint32_t, batch.groups, mid)) <= g)
lo = mid;
else
hi = mid;
}
args = DEREF(INDEX(hk_m5_refit_args, batch.tasks, lo));
args.phase = batch.phase;
refit((g - DEREF(INDEX(uint32_t, batch.groups, lo))) * 64u +
gl_LocalInvocationID.x);
#else
refit(gl_GlobalInvocationID.x);
#endif
}
94 changes: 93 additions & 1 deletion src/asahi/vulkan/bvh/hk_build_interface.h
Original file line number Diff line number Diff line change
Expand Up @@ -250,20 +250,112 @@ struct hk_agx3_refit_args {
};

/* The separate M5 backend keeps its qualified IR-topology refit protocol.
* GEN1 uses hk_agx3_refit_args and the current retained-job protocol. */
* GEN1 uses hk_agx3_refit_args and the current retained-job protocol.
*
* An updatable M5 structure keeps two things from its build, after its
* image: the IR header and internal nodes (the topology), and then the
* build's plan: the plan state, the envelope job, the IR parent of every IR
* node and the node jobs. An update refits the IR from the topology and
* re-encodes the same jobs into the same slots, so it never plans.
*
* Phases (agx3_m5_legacy_refit.comp):
* - SAVE_IR, at a build: copy the IR header and internal nodes into the
* structure.
* - PREPARE, at an update before its leaves: copy them back into scratch,
* reset the bounds and the active leaf count, and clear the arrival
* counters.
* - SAVE_PLAN, at a build after planning: copy the plan into the
* structure (the parents from the plan nodes).
* - REFIT, at an update after its leaves: recompute the bounds of the IR
* internal nodes bottom up, and restore the plan into encode scratch
* with this update's IR and image addresses.
* - COPY_PLAN, at an update into another structure: copy the plan.
*/
#define HK_M5_REFIT_SAVE_IR 0u
#define HK_M5_REFIT_PREPARE 1u
#define HK_M5_REFIT_SAVE_PLAN 2u
#define HK_M5_REFIT_REFIT 3u
#define HK_M5_REFIT_COPY_PLAN 4u

/* The retained plan: the hk_agx3_plan_state, the hk_agx3_envelope_job with
* its bounds as a byte offset into the IR, a uint32_t IR parent per IR
* node (~0 for the root and for leaves in no node), then node_bound
* hk_agx3_leaf_jobs (their IR and image addresses are stale). */
#define HK_M5_PLAN_STATE 0u
#define HK_M5_PLAN_ENVELOPE 32u
#define HK_M5_PLAN_PARENTS 80u
#define HK_M5_PLAN_JOBS(node_bound) \
((HK_M5_PLAN_PARENTS + 4u * (node_bound) + 7u) & ~7u)
#define HK_M5_PLAN_BYTES(node_bound) \
(HK_M5_PLAN_JOBS(node_bound) + 64u * (node_bound))

struct hk_m5_refit_args {
/* SAVE_IR and PREPARE: the retained IR header and internal nodes. */
VOID_REF retained;
VOID_REF metadata;
VOID_REF intermediate_bvh;
REF(vk_ir_header) header;
/* PREPARE and REFIT: an arrival counter per IR node, 16 bytes apart. */
VOID_REF parents;
uint32_t root_offset;
uint32_t leaf_bound;
uint32_t node_bound;
uint32_t phase;
/* SAVE_PLAN: the plan nodes. */
VOID_REF plan_nodes;
/* SAVE_PLAN: the plan to save; REFIT: where to restore it. */
VOID_REF plan_jobs;
VOID_REF plan_state;
VOID_REF plan_envelope;
/* The retained plan: SAVE_PLAN's destination, REFIT's and COPY_PLAN's
* source. */
VOID_REF plan;
/* COPY_PLAN: the destination structure's retained plan. */
VOID_REF plan_copy;
/* REFIT: the native image and its structure's ID. */
VOID_REF output_bvh;
uint32_t resource_id;
uint32_t padding;
};

#ifdef VULKAN
TYPE(hk_m5_refit_args, 8);
#endif

/* The updates of one build call run each refit phase in one dispatch
* (agx3_m5_legacy_refit.comp built with HK_M5_REFIT_BATCHED): workgroup g
* of the dispatch belongs to update u, groups[u] <= g < groups[u + 1], and
* runs that update's phase as workgroup g - groups[u] of its own dispatch.
* PREFIX, after REFIT, sums the restored jobs of all updates into bases[]
* (count + 1 entries) and the node encoding dispatch. */
#define HK_M5_REFIT_PREFIX 5u

struct hk_m5_refit_batch_args {
REF(hk_m5_refit_args) tasks;
REF(uint32_t) groups;
REF(uint32_t) bases;
REF(uint32_t) dispatch;
uint32_t count;
uint32_t phase;
};

/* 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]. */
struct hk_m5_leaf_task {
VOID_REF jobs;
REF(uint32_t) status;
};

#ifdef VULKAN
TYPE(hk_m5_leaf_task, 8);
#endif

struct hk_m5_leaf_batch_args {
REF(hk_m5_leaf_task) tasks;
REF(uint32_t) bases;
uint32_t count;
uint32_t padding;
};

/* Push constants of refit encoding. */
Expand Down
2 changes: 2 additions & 0 deletions src/asahi/vulkan/bvh/meson.build
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,8 @@ foreach s : [['encode.comp', 'hk_encode'], ['write.comp', 'hk_bvh_write'],
['agx3_m5_legacy_envelope.comp', 'hk_agx3_m5_legacy_envelope'],
['agx3_m5_legacy_plan.comp', 'hk_agx3_m5_legacy_plan'],
['agx3_m5_legacy_refit.comp', 'hk_agx3_m5_legacy_refit'],
['agx3_m5_legacy_refit.comp', 'hk_agx3_m5_legacy_refit_batched', '-DHK_M5_REFIT_BATCHED=1'],
['agx3_m5_legacy_leaf.comp', 'hk_agx3_m5_legacy_leaf_batched', '-DHK_M5_LEAF_BATCHED=1'],
['agx3_leaf.comp', 'hk_agx3_gen1_leaf'],
['agx3_leaf.comp', 'hk_agx3_gen1_leaf_pairs', '-DHK_AGX3_LEAF_PAIRS=1'],
['agx3_leaf.comp', 'hk_agx3_gen1_leaf_instances', '-DHK_AGX3_LEAF_ONLY=2'],
Expand Down
Loading