2022-01-27 08:52:14 +01:00
|
|
|
#include "ray_pass.h"
|
2023-01-18 20:06:35 +01:00
|
|
|
#include "shaders/ray_interop.h" // for SPEC_SBT_RECORD_SIZE_INDEX
|
2022-02-01 05:50:39 +01:00
|
|
|
#include "ray_resources.h"
|
2022-01-31 07:24:55 +01:00
|
|
|
#include "vk_pipeline.h"
|
2022-01-27 08:52:14 +01:00
|
|
|
#include "vk_descriptor.h"
|
|
|
|
|
2022-02-21 08:30:23 +01:00
|
|
|
// FIXME this is only needed for MAX_CONCURRENT_FRAMES
|
2023-01-16 21:41:37 +01:00
|
|
|
// TODO specify it externally as ctor arg
|
2022-02-21 08:30:23 +01:00
|
|
|
#include "vk_framectl.h"
|
|
|
|
|
2022-01-27 08:52:14 +01:00
|
|
|
#define MAX_STAGES 16
|
|
|
|
#define MAX_MISS_GROUPS 8
|
|
|
|
#define MAX_HIT_GROUPS 8
|
|
|
|
|
2022-01-31 07:24:55 +01:00
|
|
|
typedef enum {
|
|
|
|
RayPassType_Compute,
|
|
|
|
RayPassType_Tracing,
|
|
|
|
} ray_pass_type_t;
|
|
|
|
|
2022-01-27 08:52:14 +01:00
|
|
|
typedef struct ray_pass_s {
|
2023-01-17 07:57:48 +01:00
|
|
|
ray_pass_type_t type; // TODO remove this in favor of VkPipelineStageFlagBits
|
|
|
|
VkPipelineStageFlagBits pipeline_type;
|
2022-01-31 07:24:55 +01:00
|
|
|
char debug_name[32];
|
2022-01-27 08:52:14 +01:00
|
|
|
|
|
|
|
struct {
|
2023-01-17 07:57:48 +01:00
|
|
|
int write_from;
|
2022-01-27 08:52:14 +01:00
|
|
|
vk_descriptors_t riptors;
|
2022-02-21 08:30:23 +01:00
|
|
|
VkDescriptorSet sets[MAX_CONCURRENT_FRAMES];
|
2022-01-27 08:52:14 +01:00
|
|
|
} desc;
|
2022-01-31 07:24:55 +01:00
|
|
|
} ray_pass_t;
|
2022-01-27 08:52:14 +01:00
|
|
|
|
2022-01-31 07:24:55 +01:00
|
|
|
typedef struct {
|
|
|
|
ray_pass_t header;
|
|
|
|
vk_pipeline_ray_t pipeline;
|
|
|
|
} ray_pass_tracing_impl_t;
|
|
|
|
|
|
|
|
typedef struct {
|
|
|
|
ray_pass_t header;
|
|
|
|
VkPipeline pipeline;
|
|
|
|
} ray_pass_compute_impl_t;
|
|
|
|
|
|
|
|
static void initPassDescriptors( ray_pass_t *header, const ray_pass_layout_t *layout ) {
|
|
|
|
header->desc.riptors = (vk_descriptors_t) {
|
|
|
|
.bindings = layout->bindings,
|
|
|
|
.num_bindings = layout->bindings_count,
|
|
|
|
.num_sets = COUNTOF(header->desc.sets),
|
|
|
|
.desc_sets = header->desc.sets,
|
|
|
|
.push_constants = layout->push_constants,
|
2022-01-27 08:52:14 +01:00
|
|
|
};
|
|
|
|
|
2022-01-31 07:24:55 +01:00
|
|
|
VK_DescriptorsCreate(&header->desc.riptors);
|
2023-01-17 07:57:48 +01:00
|
|
|
|
|
|
|
header->desc.write_from = layout->write_from;
|
2022-01-31 07:24:55 +01:00
|
|
|
}
|
2022-01-27 08:52:14 +01:00
|
|
|
|
2022-01-31 07:24:55 +01:00
|
|
|
static void finalizePassDescriptors( ray_pass_t *header, const ray_pass_layout_t *layout ) {
|
2022-10-30 00:34:37 +02:00
|
|
|
const size_t bindings_size = sizeof(layout->bindings[0]) * layout->bindings_count;
|
|
|
|
VkDescriptorSetLayoutBinding *bindings = Mem_Malloc(vk_core.pool, bindings_size);
|
|
|
|
memcpy(bindings, layout->bindings, bindings_size);
|
|
|
|
header->desc.riptors.bindings = bindings;
|
|
|
|
|
2022-01-31 07:24:55 +01:00
|
|
|
header->desc.riptors.values = Mem_Malloc(vk_core.pool, sizeof(header->desc.riptors.values[0]) * layout->bindings_count);
|
2022-01-27 08:52:14 +01:00
|
|
|
}
|
|
|
|
|
2022-01-31 07:24:55 +01:00
|
|
|
struct ray_pass_s *RayPassCreateTracing( const ray_pass_create_tracing_t *create ) {
|
|
|
|
ray_pass_tracing_impl_t *const pass = Mem_Malloc(vk_core.pool, sizeof(*pass));
|
|
|
|
ray_pass_t *const header = &pass->header;
|
2022-01-27 08:52:14 +01:00
|
|
|
|
2022-10-15 21:44:54 +02:00
|
|
|
// TODO support external specialization
|
|
|
|
ASSERT(!create->specialization);
|
|
|
|
|
|
|
|
const struct SpecializationData {
|
|
|
|
uint32_t sbt_record_size;
|
|
|
|
} spec_data = {
|
|
|
|
.sbt_record_size = vk_core.physical_device.sbt_record_size,
|
|
|
|
};
|
|
|
|
const VkSpecializationMapEntry spec_map[] = {
|
|
|
|
{.constantID = SPEC_SBT_RECORD_SIZE_INDEX, .offset = offsetof(struct SpecializationData, sbt_record_size), .size = sizeof(uint32_t) },
|
|
|
|
};
|
|
|
|
const VkSpecializationInfo spec = {
|
|
|
|
.mapEntryCount = COUNTOF(spec_map),
|
|
|
|
.pMapEntries = spec_map,
|
|
|
|
.dataSize = sizeof(spec_data),
|
|
|
|
.pData = &spec_data,
|
|
|
|
};
|
|
|
|
|
2022-01-31 07:24:55 +01:00
|
|
|
initPassDescriptors(header, &create->layout);
|
2022-01-27 08:52:14 +01:00
|
|
|
|
|
|
|
{
|
|
|
|
int stage_index = 0;
|
|
|
|
vk_shader_stage_t stages[MAX_STAGES];
|
|
|
|
int miss_index = 0;
|
|
|
|
int misses[MAX_MISS_GROUPS];
|
|
|
|
int hit_index = 0;
|
|
|
|
vk_pipeline_ray_hit_group_t hits[MAX_HIT_GROUPS];
|
|
|
|
|
|
|
|
vk_pipeline_ray_create_info_t prci = {
|
|
|
|
.debug_name = create->debug_name,
|
2022-01-31 07:24:55 +01:00
|
|
|
.layout = header->desc.riptors.pipeline_layout,
|
2022-01-27 08:52:14 +01:00
|
|
|
.stages = stages,
|
|
|
|
.groups = {
|
|
|
|
.hit = hits,
|
|
|
|
.miss = misses,
|
|
|
|
},
|
|
|
|
};
|
|
|
|
|
|
|
|
stages[stage_index++] = (vk_shader_stage_t) {
|
2022-10-22 22:04:00 +02:00
|
|
|
.module = create->raygen_module,
|
2022-10-23 00:07:09 +02:00
|
|
|
.filename = NULL,
|
2022-01-27 08:52:14 +01:00
|
|
|
.stage = VK_SHADER_STAGE_RAYGEN_BIT_KHR,
|
2022-10-15 21:44:54 +02:00
|
|
|
.specialization_info = &spec,
|
2022-01-27 08:52:14 +01:00
|
|
|
};
|
|
|
|
|
2022-01-31 07:24:55 +01:00
|
|
|
for (int i = 0; i < create->miss_count; ++i) {
|
2022-10-22 22:04:00 +02:00
|
|
|
const VkShaderModule shader_module = create->miss_module ? create->miss_module[i] : VK_NULL_HANDLE;
|
2022-01-27 08:52:14 +01:00
|
|
|
|
|
|
|
ASSERT(stage_index < MAX_STAGES);
|
|
|
|
ASSERT(miss_index < MAX_MISS_GROUPS);
|
|
|
|
|
|
|
|
// TODO handle duplicate filenames
|
2022-01-28 02:47:00 +01:00
|
|
|
// TODO really, there should be a global table of shader modules as some of them are used across several passes (e.g. any hit alpha test)
|
2022-01-27 08:52:14 +01:00
|
|
|
misses[miss_index++] = stage_index;
|
|
|
|
stages[stage_index++] = (vk_shader_stage_t) {
|
2022-10-22 22:04:00 +02:00
|
|
|
.module = shader_module,
|
2022-10-23 00:07:09 +02:00
|
|
|
.filename = NULL,
|
2022-01-27 08:52:14 +01:00
|
|
|
.stage = VK_SHADER_STAGE_MISS_BIT_KHR,
|
2022-10-15 21:44:54 +02:00
|
|
|
.specialization_info = &spec,
|
2022-01-27 08:52:14 +01:00
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2022-01-31 07:24:55 +01:00
|
|
|
for (int i = 0; i < create->hit_count; ++i) {
|
|
|
|
const ray_pass_hit_group_t *const group = create->hit + i;
|
2022-01-27 08:52:14 +01:00
|
|
|
|
|
|
|
ASSERT(hit_index < MAX_HIT_GROUPS);
|
|
|
|
|
|
|
|
// TODO handle duplicate filenames
|
2022-10-22 22:04:00 +02:00
|
|
|
if (group->any_module) {
|
|
|
|
ASSERT(stage_index < MAX_STAGES);
|
|
|
|
hits[hit_index].any = stage_index;
|
|
|
|
stages[stage_index++] = (vk_shader_stage_t) {
|
|
|
|
.module = group->any_module,
|
|
|
|
.filename = NULL,
|
|
|
|
.stage = VK_SHADER_STAGE_ANY_HIT_BIT_KHR,
|
|
|
|
.specialization_info = &spec,
|
|
|
|
};
|
2022-01-27 08:52:14 +01:00
|
|
|
} else {
|
|
|
|
hits[hit_index].any = -1;
|
|
|
|
}
|
|
|
|
|
2022-10-22 22:04:00 +02:00
|
|
|
if (group->closest_module) {
|
|
|
|
ASSERT(stage_index < MAX_STAGES);
|
|
|
|
hits[hit_index].closest = stage_index;
|
|
|
|
stages[stage_index++] = (vk_shader_stage_t) {
|
|
|
|
.module = group->closest_module,
|
|
|
|
.filename = NULL,
|
|
|
|
.stage = VK_SHADER_STAGE_CLOSEST_HIT_BIT_KHR,
|
|
|
|
.specialization_info = &spec,
|
|
|
|
};
|
2022-01-27 08:52:14 +01:00
|
|
|
} else {
|
|
|
|
hits[hit_index].closest = -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
++hit_index;
|
|
|
|
}
|
|
|
|
|
|
|
|
prci.groups.hit_count = hit_index;
|
|
|
|
prci.groups.miss_count = miss_index;
|
|
|
|
prci.stages_count = stage_index;
|
|
|
|
|
2022-01-31 07:24:55 +01:00
|
|
|
pass->pipeline = VK_PipelineRayTracingCreate(&prci);
|
2022-01-27 08:52:14 +01:00
|
|
|
}
|
|
|
|
|
2022-01-31 07:24:55 +01:00
|
|
|
if (pass->pipeline.pipeline == VK_NULL_HANDLE) {
|
|
|
|
VK_DescriptorsDestroy(&header->desc.riptors);
|
2022-01-27 08:52:14 +01:00
|
|
|
Mem_Free(pass);
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2022-01-31 07:24:55 +01:00
|
|
|
finalizePassDescriptors(header, &create->layout);
|
2022-01-31 05:49:37 +01:00
|
|
|
|
2022-01-31 07:24:55 +01:00
|
|
|
Q_strncpy(header->debug_name, create->debug_name, sizeof(header->debug_name));
|
|
|
|
header->type = RayPassType_Tracing;
|
2023-01-17 07:57:48 +01:00
|
|
|
header->pipeline_type = VK_PIPELINE_STAGE_RAY_TRACING_SHADER_BIT_KHR;
|
2022-01-27 08:52:14 +01:00
|
|
|
|
2022-01-31 07:24:55 +01:00
|
|
|
return header;
|
2022-01-27 08:52:14 +01:00
|
|
|
}
|
|
|
|
|
2022-01-31 07:24:55 +01:00
|
|
|
struct ray_pass_s *RayPassCreateCompute( const ray_pass_create_compute_t *create ) {
|
|
|
|
ray_pass_compute_impl_t *const pass = Mem_Malloc(vk_core.pool, sizeof(*pass));
|
|
|
|
ray_pass_t *const header = &pass->header;
|
|
|
|
|
|
|
|
initPassDescriptors(header, &create->layout);
|
|
|
|
|
|
|
|
const vk_pipeline_compute_create_info_t pcci = {
|
|
|
|
.layout = header->desc.riptors.pipeline_layout,
|
2022-10-22 22:04:00 +02:00
|
|
|
.shader_module = create->shader_module,
|
2022-01-31 07:24:55 +01:00
|
|
|
.specialization_info = create->specialization,
|
|
|
|
};
|
|
|
|
|
|
|
|
pass->pipeline = VK_PipelineComputeCreate( &pcci );
|
|
|
|
if (pass->pipeline == VK_NULL_HANDLE) {
|
|
|
|
VK_DescriptorsDestroy(&header->desc.riptors);
|
|
|
|
Mem_Free(pass);
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
finalizePassDescriptors(header, &create->layout);
|
|
|
|
|
|
|
|
Q_strncpy(header->debug_name, create->debug_name, sizeof(header->debug_name));
|
|
|
|
header->type = RayPassType_Compute;
|
2023-01-17 07:57:48 +01:00
|
|
|
header->pipeline_type = VK_PIPELINE_STAGE_COMPUTE_SHADER_BIT;
|
2022-01-31 07:24:55 +01:00
|
|
|
|
|
|
|
return header;
|
2022-01-27 08:52:14 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
void RayPassDestroy( struct ray_pass_s *pass ) {
|
2022-01-31 07:24:55 +01:00
|
|
|
switch (pass->type) {
|
|
|
|
case RayPassType_Tracing:
|
|
|
|
{
|
|
|
|
ray_pass_tracing_impl_t *tracing = (ray_pass_tracing_impl_t*)pass;
|
|
|
|
VK_PipelineRayTracingDestroy(&tracing->pipeline);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
case RayPassType_Compute:
|
|
|
|
{
|
|
|
|
ray_pass_compute_impl_t *compute = (ray_pass_compute_impl_t*)pass;
|
|
|
|
vkDestroyPipeline(vk_core.device, compute->pipeline, NULL);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-01-27 08:52:14 +01:00
|
|
|
VK_DescriptorsDestroy(&pass->desc.riptors);
|
|
|
|
Mem_Free(pass->desc.riptors.values);
|
2022-10-30 00:34:37 +02:00
|
|
|
Mem_Free((void*)pass->desc.riptors.bindings);
|
2022-01-27 08:52:14 +01:00
|
|
|
Mem_Free(pass);
|
|
|
|
}
|
|
|
|
|
2023-01-17 07:57:48 +01:00
|
|
|
static void performTracing( VkCommandBuffer cmdbuf, int set_slot, const ray_pass_tracing_impl_t *tracing, int width, int height ) {
|
2022-01-31 07:24:55 +01:00
|
|
|
vkCmdBindPipeline(cmdbuf, VK_PIPELINE_BIND_POINT_RAY_TRACING_KHR, tracing->pipeline.pipeline);
|
2022-02-21 08:30:23 +01:00
|
|
|
vkCmdBindDescriptorSets(cmdbuf, VK_PIPELINE_BIND_POINT_RAY_TRACING_KHR, tracing->header.desc.riptors.pipeline_layout, 0, 1, tracing->header.desc.riptors.desc_sets + set_slot, 0, NULL);
|
2023-01-17 07:57:48 +01:00
|
|
|
VK_PipelineRayTracingTrace(cmdbuf, &tracing->pipeline, width, height);
|
2022-01-31 07:24:55 +01:00
|
|
|
}
|
|
|
|
|
2023-01-17 07:57:48 +01:00
|
|
|
static void performCompute( VkCommandBuffer cmdbuf, int set_slot, const ray_pass_compute_impl_t *compute, int width, int height) {
|
2022-01-31 07:24:55 +01:00
|
|
|
const uint32_t WG_W = 8;
|
|
|
|
const uint32_t WG_H = 8;
|
|
|
|
|
|
|
|
vkCmdBindPipeline(cmdbuf, VK_PIPELINE_BIND_POINT_COMPUTE, compute->pipeline);
|
2022-02-21 08:30:23 +01:00
|
|
|
vkCmdBindDescriptorSets(cmdbuf, VK_PIPELINE_BIND_POINT_COMPUTE, compute->header.desc.riptors.pipeline_layout, 0, 1, compute->header.desc.riptors.desc_sets + set_slot, 0, NULL);
|
2023-01-17 07:57:48 +01:00
|
|
|
vkCmdDispatch(cmdbuf, (width + WG_W - 1) / WG_W, (height + WG_H - 1) / WG_H, 1);
|
2022-01-31 07:24:55 +01:00
|
|
|
}
|
2022-01-27 08:52:14 +01:00
|
|
|
|
2023-01-17 07:57:48 +01:00
|
|
|
void RayPassPerform(struct ray_pass_s *pass, VkCommandBuffer cmdbuf, ray_pass_perform_args_t args ) {
|
2023-01-21 22:03:57 +01:00
|
|
|
R_VkResourcesPrepareDescriptorsValues(cmdbuf,
|
|
|
|
(vk_resources_write_descriptors_args_t){
|
|
|
|
.pipeline = pass->pipeline_type,
|
|
|
|
.resources = args.resources,
|
|
|
|
.resources_map = args.resources_map,
|
|
|
|
.values = pass->desc.riptors.values,
|
|
|
|
.count = pass->desc.riptors.num_bindings,
|
|
|
|
.write_begin = pass->desc.write_from,
|
|
|
|
}
|
|
|
|
);
|
2022-01-27 08:52:14 +01:00
|
|
|
|
2023-01-17 07:57:48 +01:00
|
|
|
VK_DescriptorsWrite(&pass->desc.riptors, args.frame_set_slot);
|
|
|
|
|
2022-01-31 07:24:55 +01:00
|
|
|
DEBUG_BEGIN(cmdbuf, pass->debug_name);
|
|
|
|
|
|
|
|
switch (pass->type) {
|
|
|
|
case RayPassType_Tracing:
|
|
|
|
{
|
|
|
|
ray_pass_tracing_impl_t *tracing = (ray_pass_tracing_impl_t*)pass;
|
2023-01-17 07:57:48 +01:00
|
|
|
performTracing(cmdbuf, args.frame_set_slot, tracing, args.width, args.height);
|
2022-01-31 07:24:55 +01:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
case RayPassType_Compute:
|
|
|
|
{
|
|
|
|
ray_pass_compute_impl_t *compute = (ray_pass_compute_impl_t*)pass;
|
2023-01-17 07:57:48 +01:00
|
|
|
performCompute(cmdbuf, args.frame_set_slot, compute, args.width, args.height);
|
2022-01-31 07:24:55 +01:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
DEBUG_END(cmdbuf);
|
2022-01-27 08:52:14 +01:00
|
|
|
}
|