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
6 changes: 4 additions & 2 deletions src/asahi/libagx/draws.cl
Original file line number Diff line number Diff line change
Expand Up @@ -632,7 +632,8 @@ libagx_trace_rays_select_setup(constant uint64_t *record,
libagx_trace_rays_group_count(width, height, local_size_x_log2);

constant uint32_t *raygen_handle = (constant uint32_t *)(uintptr_t)record[0];
const uint32_t selected = *raygen_handle;
const uint32_t selected =
(groups_xy.x && groups_xy.y && depth && record[0]) ? *raygen_handle : 0u;

for (uint32_t k = 0; k < kernel_count; ++k) {
const bool run = handles[k] == selected;
Expand Down Expand Up @@ -685,7 +686,8 @@ libagx_trace_rays_select_launch(constant uint64_t *record,
const uint32_t groups_y = groups_xy.y;

constant uint32_t *raygen_handle = (constant uint32_t *)(uintptr_t)record[0];
const uint32_t selected = *raygen_handle;
const uint32_t selected =
(groups_xy.x && groups_xy.y && depth && record[0]) ? *raygen_handle : 0u;

uint32_t chosen = kernel_count;
for (uint32_t k = 0; k < kernel_count; ++k) {
Expand Down
4 changes: 2 additions & 2 deletions src/asahi/vulkan/hk_cmd_trace_rays.c
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
* indirect forms let one GPU invocation fill in the launch size, because that
* is the only part that is not known until the command executes.
*
* Elsewhere a pipeline is one megakernel; on M5 it has one kernel per
* Elsewhere a pipeline is one megakernel; on G17P and M5 it has one kernel per
* ray-generation group. Which group a trace runs
* is named by the raygen shader binding table record, and that record is
* application memory whose contents are only required to be valid when the
Expand Down Expand Up @@ -84,7 +84,7 @@ hk_bound_rt_pipeline(struct hk_cmd_buffer *cmd)
}

/* The one kernel a trace dispatches without a GPU-side selection: the
* megakernel, or the only per-raygen kernel of an M5 pipeline. */
* megakernel, or the only per-raygen kernel of a G17P/M5 pipeline. */
static struct hk_api_shader *
hk_rt_single_kernel(const struct hk_ray_tracing_pipeline *pipeline)
{
Expand Down
126 changes: 75 additions & 51 deletions src/asahi/vulkan/hk_ray_tracing_pipeline.c
Original file line number Diff line number Diff line change
Expand Up @@ -15,13 +15,13 @@
*
* Two departures from lavapipe's single megakernel, both for throughput:
*
* - On M5, one kernel is compiled PER RAY-GENERATION GROUP rather than one
* for the whole pipeline. A trace runs exactly one raygen, so inlining
* every raygen into one kernel only makes each dispatch pay for the union:
* its code size, its register count (the occupancy of every trace was that
* of the worst raygen) and its scratch (the stack was sized for the largest
* raygen frame). hk_cmd_trace_rays.c selects the kernel on the GPU from
* the SBT.
* - On G17P and M5, one kernel is compiled PER RAY-GENERATION GROUP rather
* than one for the whole pipeline. A trace runs exactly one raygen, so
* inlining every raygen into one kernel only makes each dispatch pay for the
* union: its code size, its register count (the occupancy of every trace was
* that of the worst raygen) and its scratch (the stack was sized for the
* largest raygen frame). hk_cmd_trace_rays.c selects the kernel on the GPU
* from the SBT.
*
* - On all chips, a stage that several groups name (a hit group with and
* without an any-hit shader commonly share one closest-hit) is inlined once
Expand Down Expand Up @@ -2394,11 +2394,19 @@ hk_rt_cont_objects_local(nir_shader *part, unsigned pc, struct hash_table *seen)
* then keeps the inlined megakernel.
*/
static bool
hk_rt_cont_split(struct hk_rt_compiler *compiler, void *mem_ctx)
hk_rt_cont_split(struct hk_rt_compiler *compiler, void *mem_ctx,
const struct hk_ray_tracing_group *raygen_group)
{
struct hk_ray_tracing_pipeline *pipeline = compiler->pipeline;
const nir_shader *only_raygen =
raygen_group ? pipeline->stages[raygen_group->recursive_index]->nir
: NULL;

for (unsigned i = 0; i < pipeline->stage_count; ++i) {
if (only_raygen &&
pipeline->stages[i]->nir->info.stage == MESA_SHADER_RAYGEN &&
pipeline->stages[i]->nir != only_raygen)
continue;
if (hk_rt_cont_has_intrinsic(pipeline->stages[i]->nir,
nir_intrinsic_hit_object_execute_shader))
return false;
Expand Down Expand Up @@ -2431,7 +2439,9 @@ hk_rt_cont_split(struct hk_rt_compiler *compiler, void *mem_ctx)
} else if (stage->info.stage == MESA_SHADER_ANY_HIT) {
frames.any_hit = MAX2(frames.any_hit, own);
continue;
} else if (!hk_rt_cont_splits(stage) ||
} else if ((only_raygen && stage->info.stage == MESA_SHADER_RAYGEN &&
stage != only_raygen) ||
!hk_rt_cont_splits(stage) ||
_mesa_hash_table_search(ctx->entries, stage)) {
continue;
}
Expand Down Expand Up @@ -3225,10 +3235,10 @@ hk_ray_tracing_pipeline_get_shader(struct vk_pipeline *vk_pipeline,
return NULL;
}

/* All RT stages are inlined into a single compute megakernel, except on M5,
* which compiles one kernel per ray-generation group. Report each executable
* with the API stages it implements rather than COMPUTE. A library only
* retains stage NIR and has no executable to enumerate.
/* All RT stages are inlined into a single compute megakernel, except on G17P
* and M5, which compile one kernel per ray-generation group. Report each
* executable with the API stages it implements rather than COMPUTE. A library
* only retains stage NIR and has no executable to enumerate.
*/
static VkResult
hk_rt_pipeline_get_executable_properties(
Expand Down Expand Up @@ -3865,7 +3875,7 @@ hk_compile_rt_kernel(
/* Deeper pipelines run as continuations unless a ray query or hit object
* lives across a call; those keep the inlined megakernel. */
if (compiler.max_depth > HK_RT_INLINE_MAX_RAY_RECURSION_DEPTH &&
hk_rt_cont_split(&compiler, b->shader)) {
hk_rt_cont_split(&compiler, b->shader, raygen_group)) {
if (compiler.cont_stack_size > HK_RT_CONT_MAX_STACK_SIZE) {
ralloc_free(b->shader);
return vk_errorf(dev, VK_ERROR_OUT_OF_DEVICE_MEMORY,
Expand Down Expand Up @@ -4082,19 +4092,27 @@ hk_compile_rt_kernel(
}

/*
* On M5, one kernel per ray-generation group. A pipeline whose groups name no
* raygen (only a library can be created that way) has nothing to compile;
* hk_cmd_trace_rays.c refuses to trace with it.
* On G17P and M5, one kernel per ray-generation group; elsewhere the pipeline
* is compiled as a single megakernel.
*/
static bool
hk_rt_per_raygen_kernels(const struct hk_device *dev)
{
return agx_chip_g17p_or_m5(dev->dev.chip);
}

/*
* On G17P and M5, one kernel per ray-generation group. A pipeline whose groups
* name no raygen has nothing to compile; hk_cmd_trace_rays.c refuses to trace
* with it.
*/
static VkResult
hk_compile_ray_tracing_pipeline(
struct hk_device *dev, struct hk_ray_tracing_pipeline *pipeline,
const VkRayTracingPipelineCreateInfoKHR *create_info,
const VkAllocationCallbacks *pAllocator)
{
/* Only M5 compiles one kernel per ray-generation group; elsewhere the
* pipeline is one megakernel. */
if (!agx_chip_g17_m5(dev->dev.chip)) {
if (!hk_rt_per_raygen_kernels(dev)) {
struct hk_rt_kernel kernel = {0};
VkResult result = hk_compile_rt_kernel(dev, pipeline, create_info,
pAllocator, NULL, &kernel);
Expand Down Expand Up @@ -4141,17 +4159,17 @@ hk_compile_ray_tracing_pipeline(
return VK_SUCCESS;
}

/* The common binary entrypoints own vk_rt_pipeline objects. Honeykrisp's
* megakernel (or, on M5, per-raygen kernels) and retained library NIR have a
* different layout and lifetime. */
/* The common binary entrypoints own vk_rt_pipeline objects. Borealis's
* megakernel (or, on G17P and M5, per-raygen kernels) and retained library NIR
* have a different layout and lifetime. */
#define HK_RT_BINARY_MAGIC 0x42524b48u
#define HK_RT_BINARY_VERSION 1u

/* What an RT binary carries after the stage NIR. */
enum hk_rt_binary_executable {
HK_RT_BINARY_LIBRARY = 0,
HK_RT_BINARY_MEGAKERNEL = 1,
HK_RT_BINARY_RAYGEN_KERNELS = 2, /* M5 */
HK_RT_BINARY_RAYGEN_KERNELS = 2, /* G17P and M5 */
};

static bool
Expand Down Expand Up @@ -4239,9 +4257,12 @@ hk_rt_binary_serialize(struct hk_device *dev,
blob_write_uint32(blob, pipeline->stage_count);
blob_write_uint32(blob, pipeline->group_count);
blob_write_uint32(blob, pipeline->base.stages);
blob_write_uint32(blob, pipeline->kernel_count ? HK_RT_BINARY_RAYGEN_KERNELS
: pipeline->shader ? HK_RT_BINARY_MEGAKERNEL
: HK_RT_BINARY_LIBRARY);
const bool is_library =
!!(pipeline->base.flags & VK_PIPELINE_CREATE_2_LIBRARY_BIT_KHR);
const uint32_t kind = is_library ? HK_RT_BINARY_LIBRARY
: pipeline->shader ? HK_RT_BINARY_MEGAKERNEL
: HK_RT_BINARY_RAYGEN_KERNELS;
blob_write_uint32(blob, kind);
blob_write_bytes(blob, &pipeline->stack_sizes,
sizeof(pipeline->stack_sizes));
blob_write_uint32(blob, pipeline->stack_size);
Expand All @@ -4258,10 +4279,11 @@ hk_rt_binary_serialize(struct hk_device *dev,
if (!ok)
return false;
}
if (pipeline->shader && !pipeline->shader->vk.ops->serialize(
&dev->vk, &pipeline->shader->vk, blob))
if (kind == HK_RT_BINARY_MEGAKERNEL &&
!pipeline->shader->vk.ops->serialize(&dev->vk, &pipeline->shader->vk,
blob))
return false;
if (pipeline->kernel_count) {
if (kind == HK_RT_BINARY_RAYGEN_KERNELS) {
blob_write_uint32(blob, pipeline->kernel_count);
for (uint32_t i = 0; i < pipeline->kernel_count; ++i) {
const struct vk_shader *shader = &pipeline->kernels[i].shader->vk;
Expand Down Expand Up @@ -4484,7 +4506,7 @@ hk_rt_binary_load(struct hk_device *dev, struct hk_ray_tracing_pipeline *pipelin
uint32_t groups = blob_read_uint32(&blob);
pipeline->base.stages = blob_read_uint32(&blob);
const uint32_t executable = blob_read_uint32(&blob);
const uint32_t kernels_kind = agx_chip_g17_m5(dev->dev.chip)
const uint32_t kernels_kind = hk_rt_per_raygen_kernels(dev)
? HK_RT_BINARY_RAYGEN_KERNELS
: HK_RT_BINARY_MEGAKERNEL;
if (blob.overrun ||
Expand Down Expand Up @@ -4540,29 +4562,31 @@ hk_rt_binary_load(struct hk_device *dev, struct hk_ray_tracing_pipeline *pipelin
pipeline->shader = container_of(shader, struct hk_api_shader, vk);
} else if (executable == HK_RT_BINARY_RAYGEN_KERNELS) {
const uint32_t kernel_count = blob_read_uint32(&blob);
if (blob.overrun || kernel_count == 0 || kernel_count > groups)
if (blob.overrun || kernel_count > groups)
goto done;
pipeline->kernels = calloc(kernel_count, sizeof(*pipeline->kernels));
if (!pipeline->kernels) {
result = VK_ERROR_OUT_OF_HOST_MEMORY;
goto done;
}
for (uint32_t i = 0; i < kernel_count; ++i) {
const uint32_t handle = blob_read_uint32(&blob);
struct vk_shader *shader = NULL;
if (blob.overrun) {
result = VK_ERROR_UNKNOWN;
if (kernel_count) {
pipeline->kernels = calloc(kernel_count, sizeof(*pipeline->kernels));
if (!pipeline->kernels) {
result = VK_ERROR_OUT_OF_HOST_MEMORY;
goto done;
}
result = dev->vk.shader_ops->deserialize(
&dev->vk, &blob, dev->vk.physical->properties.shaderBinaryVersion,
alloc, &shader);
if (result != VK_SUCCESS)
goto done;
pipeline->kernels[i].shader =
container_of(shader, struct hk_api_shader, vk);
pipeline->kernels[i].raygen_handle_index = handle;
pipeline->kernel_count++;
for (uint32_t i = 0; i < kernel_count; ++i) {
const uint32_t handle = blob_read_uint32(&blob);
struct vk_shader *shader = NULL;
if (blob.overrun) {
result = VK_ERROR_UNKNOWN;
goto done;
}
result = dev->vk.shader_ops->deserialize(
&dev->vk, &blob, dev->vk.physical->properties.shaderBinaryVersion,
alloc, &shader);
if (result != VK_SUCCESS)
goto done;
pipeline->kernels[i].shader =
container_of(shader, struct hk_api_shader, vk);
pipeline->kernels[i].raygen_handle_index = handle;
pipeline->kernel_count++;
}
}
}
result =
Expand Down
17 changes: 9 additions & 8 deletions src/asahi/vulkan/hk_ray_tracing_pipeline.h
Original file line number Diff line number Diff line change
Expand Up @@ -53,17 +53,18 @@ struct hk_ray_tracing_pipeline {
struct hk_rt_stage_nir **stages;
struct hk_ray_tracing_group *groups;

/* The compiled megakernel. NULL on M5, which compiles kernels instead,
* and for a pipeline library, which is never executable. */
/* The compiled megakernel. NULL on G17P and M5, which compile per-raygen
* kernels instead, and for a pipeline library, which is never executable. */
struct hk_api_shader *shader;

/*
* On M5, one compute kernel per ray-generation group. Each kernel inlines only
* its own raygen plus every hit/miss/intersection/callable stage reachable
* through the shader binding table; vkCmdTraceRays selects the kernel on the
* GPU from the raygen record's group handle (hk_cmd_trace_rays.c), because
* the record's contents are only required to be valid at execution time.
* Empty elsewhere and for a pipeline library, which is never executable.
* On G17P and M5, one compute kernel per ray-generation group. Each kernel
* inlines only its own raygen plus every hit/miss/intersection/callable
* stage reachable through the shader binding table; vkCmdTraceRays selects
* the kernel on the GPU from the raygen record's group handle
* (hk_cmd_trace_rays.c), because the record's contents are only required to
* be valid at execution time. Empty elsewhere and for a pipeline library,
* which is never executable.
*/
uint32_t kernel_count;
struct hk_rt_kernel {
Expand Down
Loading