Skip to content

Commit

Permalink
Build raytracing volumes
Browse files Browse the repository at this point in the history
  • Loading branch information
RobDangerous committed Sep 15, 2024
1 parent 384f958 commit 36dcd53
Show file tree
Hide file tree
Showing 10 changed files with 102 additions and 29 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -171,3 +171,34 @@ void kope_d3d12_command_list_set_compute_pipeline(kope_g5_command_list *list, ko
void kope_d3d12_command_list_compute(kope_g5_command_list *list, uint32_t workgroup_count_x, uint32_t workgroup_count_y, uint32_t workgroup_count_z) {
list->d3d12.list->Dispatch(workgroup_count_x, workgroup_count_y, workgroup_count_z);
}

void kope_d3d12_command_list_prepare_raytracing_volume(kope_g5_command_list *list, kope_g5_raytracing_volume *volume) {
D3D12_RAYTRACING_GEOMETRY_DESC geometry_desc = {};

geometry_desc.Type = D3D12_RAYTRACING_GEOMETRY_TYPE_TRIANGLES;
geometry_desc.Flags = D3D12_RAYTRACING_GEOMETRY_FLAG_OPAQUE;

geometry_desc.Triangles.Transform3x4 = 0;

geometry_desc.Triangles.IndexFormat = volume->d3d12.index_buffer != nullptr ? DXGI_FORMAT_R16_UINT : DXGI_FORMAT_UNKNOWN;
geometry_desc.Triangles.VertexFormat = DXGI_FORMAT_R32G32B32_FLOAT;
geometry_desc.Triangles.IndexCount = volume->d3d12.index_count;
geometry_desc.Triangles.VertexCount = (UINT)volume->d3d12.vertex_count;
geometry_desc.Triangles.IndexBuffer = volume->d3d12.index_buffer != nullptr ? volume->d3d12.index_buffer->d3d12.resource->GetGPUVirtualAddress() : 0;
geometry_desc.Triangles.VertexBuffer.StartAddress = volume->d3d12.vertex_buffer->d3d12.resource->GetGPUVirtualAddress();
geometry_desc.Triangles.VertexBuffer.StrideInBytes = sizeof(float) * 3;

D3D12_BUILD_RAYTRACING_ACCELERATION_STRUCTURE_INPUTS inputs = {};
inputs.Type = D3D12_RAYTRACING_ACCELERATION_STRUCTURE_TYPE_BOTTOM_LEVEL;
inputs.Flags = D3D12_RAYTRACING_ACCELERATION_STRUCTURE_BUILD_FLAG_PREFER_FAST_TRACE;
inputs.NumDescs = 1;
inputs.DescsLayout = D3D12_ELEMENTS_LAYOUT_ARRAY;
inputs.pGeometryDescs = &geometry_desc;

D3D12_BUILD_RAYTRACING_ACCELERATION_STRUCTURE_DESC build_desc = {};
build_desc.DestAccelerationStructureData = volume->d3d12.acceleration_structure.d3d12.resource->GetGPUVirtualAddress();
build_desc.Inputs = inputs;
build_desc.ScratchAccelerationStructureData = volume->d3d12.scratch_buffer.d3d12.resource->GetGPUVirtualAddress();

list->d3d12.list->BuildRaytracingAccelerationStructure(&build_desc, 0, nullptr);
}
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,8 @@ void kope_d3d12_command_list_set_compute_pipeline(kope_g5_command_list *list, ko

void kope_d3d12_command_list_compute(kope_g5_command_list *list, uint32_t workgroup_count_x, uint32_t workgroup_count_y, uint32_t workgroup_count_z);

void kope_d3d12_command_list_prepare_raytracing_volume(kope_g5_command_list *list, kope_g5_raytracing_volume *volume);

#ifdef __cplusplus
}
#endif
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ typedef struct kope_d3d12_command_list {

struct ID3D12CommandAllocator *allocator[KOPE_D3D12_COMMAND_LIST_ALLOCATOR_COUNT];

struct ID3D12GraphicsCommandList *list;
struct ID3D12GraphicsCommandList4 *list;

// a bunch of variables used to figure out what allocators can be reused
uint64_t execution_index;
Expand Down
59 changes: 34 additions & 25 deletions Backends/Graphics5/Direct3D12/Sources/kope/direct3d12/device.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -190,10 +190,22 @@ void kope_d3d12_device_create_buffer(kope_g5_device *device, const kope_g5_buffe

buffer->d3d12.size = parameters->size;

buffer->d3d12.resource_state = D3D12_RESOURCE_STATE_GENERIC_READ;
if ((parameters->usage_flags & KOPE_G5_BUFFER_USAGE_READ_WRITE) != 0) {
buffer->d3d12.resource_state = D3D12_RESOURCE_STATE_COMMON;
resourceDesc.Flags |= D3D12_RESOURCE_FLAG_ALLOW_UNORDERED_ACCESS;
}
else {
buffer->d3d12.resource_state = D3D12_RESOURCE_STATE_GENERIC_READ;
}

if ((parameters->usage_flags & KOPE_G5_BUFFER_USAGE_RAYTRACING_VOLUME) != 0) {
buffer->d3d12.resource_state = D3D12_RESOURCE_STATE_RAYTRACING_ACCELERATION_STRUCTURE;
resourceDesc.Flags |= D3D12_RESOURCE_FLAG_RAYTRACING_ACCELERATION_STRUCTURE;
}

kinc_microsoft_affirm(device->d3d12.device->CreateCommittedResource(&heapProperties, D3D12_HEAP_FLAG_NONE, &resourceDesc, D3D12_RESOURCE_STATE_GENERIC_READ,
NULL, IID_GRAPHICS_PPV_ARGS(&buffer->d3d12.resource)));
kinc_microsoft_affirm(device->d3d12.device->CreateCommittedResource(&heapProperties, D3D12_HEAP_FLAG_NONE, &resourceDesc,
(D3D12_RESOURCE_STATES)buffer->d3d12.resource_state, NULL,
IID_GRAPHICS_PPV_ARGS(&buffer->d3d12.resource)));
}

static uint32_t current_command_list_allocator_index(kope_g5_command_list *list) {
Expand Down Expand Up @@ -556,48 +568,45 @@ void kope_d3d12_device_create_sampler(kope_g5_device *device, const kope_g5_samp
device->d3d12.device->CreateSampler(&desc, descriptor_handle);
}

void kope_d3d12_device_create_raytracing_instance(kope_g5_device *device, kope_g5_buffer *vertex_buffer, uint64_t vertex_count, kope_g5_buffer *index_buffer,
uint32_t index_count) {
D3D12_RAYTRACING_GEOMETRY_DESC geometry_desc;
void kope_d3d12_device_create_raytracing_volume(kope_g5_device *device, kope_g5_buffer *vertex_buffer, uint64_t vertex_count, kope_g5_buffer *index_buffer,
uint32_t index_count, kope_g5_raytracing_volume *volume) {
volume->d3d12.vertex_buffer = vertex_buffer;
volume->d3d12.vertex_count = vertex_count;
volume->d3d12.index_buffer = index_buffer;
volume->d3d12.index_count = index_count;

D3D12_RAYTRACING_GEOMETRY_DESC geometry_desc = {};

geometry_desc.Type = D3D12_RAYTRACING_GEOMETRY_TYPE_TRIANGLES;
geometry_desc.Flags = D3D12_RAYTRACING_GEOMETRY_FLAG_OPAQUE;

geometry_desc.Triangles.Transform3x4 = 0;

geometry_desc.Triangles.IndexFormat = index_buffer != nullptr ? DXGI_FORMAT_R16_UINT : DXGI_FORMAT_UNKNOWN;
geometry_desc.Triangles.IndexFormat = volume->d3d12.index_buffer != nullptr ? DXGI_FORMAT_R16_UINT : DXGI_FORMAT_UNKNOWN;
geometry_desc.Triangles.VertexFormat = DXGI_FORMAT_R32G32B32_FLOAT;
geometry_desc.Triangles.IndexCount = index_count;
geometry_desc.Triangles.VertexCount = (UINT)vertex_count;
geometry_desc.Triangles.IndexBuffer = index_buffer != nullptr ? index_buffer->d3d12.resource->GetGPUVirtualAddress() : 0;
geometry_desc.Triangles.VertexBuffer.StartAddress = vertex_buffer->d3d12.resource->GetGPUVirtualAddress();
geometry_desc.Triangles.IndexCount = volume->d3d12.index_count;
geometry_desc.Triangles.VertexCount = (UINT)volume->d3d12.vertex_count;
geometry_desc.Triangles.IndexBuffer = volume->d3d12.index_buffer != nullptr ? volume->d3d12.index_buffer->d3d12.resource->GetGPUVirtualAddress() : 0;
geometry_desc.Triangles.VertexBuffer.StartAddress = volume->d3d12.vertex_buffer->d3d12.resource->GetGPUVirtualAddress();
geometry_desc.Triangles.VertexBuffer.StrideInBytes = sizeof(float) * 3;

D3D12_BUILD_RAYTRACING_ACCELERATION_STRUCTURE_INPUTS inputs;
D3D12_BUILD_RAYTRACING_ACCELERATION_STRUCTURE_INPUTS inputs = {};
inputs.Type = D3D12_RAYTRACING_ACCELERATION_STRUCTURE_TYPE_BOTTOM_LEVEL;
inputs.Flags = D3D12_RAYTRACING_ACCELERATION_STRUCTURE_BUILD_FLAG_PREFER_FAST_TRACE;
inputs.NumDescs = 1;
inputs.DescsLayout = D3D12_ELEMENTS_LAYOUT_ARRAY;
inputs.pGeometryDescs = &geometry_desc;

D3D12_RAYTRACING_ACCELERATION_STRUCTURE_PREBUILD_INFO prebuild_info;
D3D12_RAYTRACING_ACCELERATION_STRUCTURE_PREBUILD_INFO prebuild_info = {};
device->d3d12.device->GetRaytracingAccelerationStructurePrebuildInfo(&inputs, &prebuild_info);

kope_g5_buffer scratch;
kope_g5_buffer_parameters scratch_params;
scratch_params.size = prebuild_info.ScratchDataSizeInBytes;
scratch_params.usage_flags = KOPE_G5_BUFFER_USAGE_READ_WRITE;
kope_g5_device_create_buffer(device, &scratch_params, &scratch);
kope_g5_device_create_buffer(device, &scratch_params, &volume->d3d12.scratch_buffer); // TODO: delete later

kope_g5_buffer acceleration_structure;
kope_g5_buffer_parameters as_params;
scratch_params.size = prebuild_info.ResultDataMaxSizeInBytes;
scratch_params.usage_flags = KOPE_G5_BUFFER_USAGE_READ_WRITE;
kope_g5_device_create_buffer(device, &as_params, &acceleration_structure);

D3D12_BUILD_RAYTRACING_ACCELERATION_STRUCTURE_DESC build_desc;

build_desc.DestAccelerationStructureData = acceleration_structure.d3d12.resource->GetGPUVirtualAddress();
build_desc.Inputs = inputs;
build_desc.ScratchAccelerationStructureData = scratch.d3d12.resource->GetGPUVirtualAddress();
as_params.size = prebuild_info.ResultDataMaxSizeInBytes;
as_params.usage_flags = KOPE_G5_BUFFER_USAGE_READ_WRITE | KOPE_G5_BUFFER_USAGE_RAYTRACING_VOLUME;
kope_g5_device_create_buffer(device, &as_params, &volume->d3d12.acceleration_structure);
}
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,8 @@ kope_g5_texture *kope_d3d12_device_get_framebuffer(kope_g5_device *device);

void kope_d3d12_device_execute_command_list(kope_g5_device *device, kope_g5_command_list *list);

void kope_d3d12_device_create_raytracing_instance(kope_g5_device *device, kope_g5_buffer *vertex_buffer, uint64_t vertex_count, kope_g5_buffer *index_buffer,
uint32_t index_count);
void kope_d3d12_device_create_raytracing_volume(kope_g5_device *device, kope_g5_buffer *vertex_buffer, uint64_t vertex_count, kope_g5_buffer *index_buffer,
uint32_t index_count, kope_g5_raytracing_volume *volume);

#ifdef __cplusplus
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,16 @@ typedef struct kope_d3d12_device {
kope_g5_command_list management_list;
} kope_d3d12_device;

typedef struct kope_d3d12_raytracing_volume {
kope_g5_buffer *vertex_buffer;
uint64_t vertex_count;
kope_g5_buffer *index_buffer;
uint32_t index_count;

kope_g5_buffer scratch_buffer;
kope_g5_buffer acceleration_structure;
} kope_d3d12_raytracing_volume;

#ifdef __cplusplus
}
#endif
Expand Down
4 changes: 4 additions & 0 deletions Sources/kope/graphics5/commandlist.c
Original file line number Diff line number Diff line change
Expand Up @@ -33,3 +33,7 @@ void kope_g5_command_list_draw_indexed(kope_g5_command_list *list, uint32_t inde
void kope_g5_command_list_compute(kope_g5_command_list *list, uint32_t workgroup_count_x, uint32_t workgroup_count_y, uint32_t workgroup_count_z) {
KOPE_G5_CALL4(command_list_compute, list, workgroup_count_x, workgroup_count_y, workgroup_count_z);
}

void kope_g5_command_list_prepare_raytracing_volume(kope_g5_command_list *list, kope_g5_raytracing_volume *volume) {
KOPE_G5_CALL2(command_list_prepare_raytracing_volume, list, volume);
}
4 changes: 4 additions & 0 deletions Sources/kope/graphics5/commandlist.h
Original file line number Diff line number Diff line change
Expand Up @@ -125,6 +125,10 @@ KOPE_FUNC void kope_g5_command_list_compute(kope_g5_command_list *list, uint32_t

KOPE_FUNC void kope_g5_command_list_compute_indirect(kope_g5_command_list *list, kope_g5_buffer *indirect_buffer, uint64_t indirect_offset);

struct kope_g5_raytracing_volume;

KOPE_FUNC void kope_g5_command_list_prepare_raytracing_volume(kope_g5_command_list *list, struct kope_g5_raytracing_volume *volume);

KOPE_FUNC void kope_g5_command_list_present(kope_g5_command_list *list);

#ifdef __cplusplus
Expand Down
5 changes: 5 additions & 0 deletions Sources/kope/graphics5/device.c
Original file line number Diff line number Diff line change
Expand Up @@ -158,3 +158,8 @@ bool kope_g5_texture_format_is_depth(kope_g5_texture_format format) {
void kope_g5_device_create_sampler(kope_g5_device *device, const kope_g5_sampler_parameters *parameters, kope_g5_sampler *sampler) {
KOPE_G5_CALL3(device_create_sampler, device, parameters, sampler);
}

void kope_g5_device_create_raytracing_volume(kope_g5_device *device, kope_g5_buffer *vertex_buffer, uint64_t vertex_count, kope_g5_buffer *index_buffer,
uint32_t index_count, kope_g5_raytracing_volume *volume) {
KOPE_G5_CALL6(device_create_raytracing_volume, device, vertex_buffer, vertex_count, index_buffer, index_count, volume);
}
10 changes: 9 additions & 1 deletion Sources/kope/graphics5/device.h
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,8 @@ typedef enum kope_g5_buffer_usage {
KOPE_G5_BUFFER_USAGE_INDEX = 0x0010,
KOPE_G5_BUFFER_USAGE_READ_WRITE = 0x0020,
KOPE_G5_BUFFER_USAGE_INDIRECT = 0x0040,
KOPE_G5_BUFFER_USAGE_QUERY_RESOLVE = 0x0080
KOPE_G5_BUFFER_USAGE_QUERY_RESOLVE = 0x0080,
KOPE_G5_BUFFER_USAGE_RAYTRACING_VOLUME = 0x0100
} kope_g5_buffer_usage;

typedef struct kope_g5_buffer_parameters {
Expand Down Expand Up @@ -168,6 +169,13 @@ KOPE_FUNC void kope_g5_device_create_query_set(void *descriptor);

KOPE_FUNC void kope_g5_device_execute_command_list(kope_g5_device *device, kope_g5_command_list *list);

typedef struct kope_g5_raytracing_volume {
KOPE_G5_IMPL(raytracing_volume);
} kope_g5_raytracing_volume;

KOPE_FUNC void kope_g5_device_create_raytracing_volume(kope_g5_device *device, kope_g5_buffer *vertex_buffer, uint64_t vertex_count,
kope_g5_buffer *index_buffer, uint32_t index_count, kope_g5_raytracing_volume *volume);

#ifdef __cplusplus
}
#endif
Expand Down

0 comments on commit 36dcd53

Please sign in to comment.