diff --git a/Backends/Graphics5/Direct3D12/Sources/kope/direct3d12/descriptorset.cpp b/Backends/Graphics5/Direct3D12/Sources/kope/direct3d12/descriptorset.cpp index e284a6e90..c688aad7c 100644 --- a/Backends/Graphics5/Direct3D12/Sources/kope/direct3d12/descriptorset.cpp +++ b/Backends/Graphics5/Direct3D12/Sources/kope/direct3d12/descriptorset.cpp @@ -27,6 +27,18 @@ void kope_d3d12_descriptor_set_set_texture_view_srv(kope_g5_device *device, kope device->d3d12.device->CreateShaderResourceView(texture->d3d12.resource, &desc, descriptor_handle); } +void kope_d3d12_descriptor_set_set_texture_view_uav(kope_g5_device *device, kope_d3d12_descriptor_set *set, kope_g5_texture *texture, uint32_t index) { + D3D12_UNORDERED_ACCESS_VIEW_DESC desc = {}; + desc.ViewDimension = D3D12_UAV_DIMENSION_TEXTURE2D; + desc.Format = DXGI_FORMAT_R8G8B8A8_UNORM; + desc.Texture2D.MipSlice = 0; + desc.Texture2D.PlaneSlice = 0; + + D3D12_CPU_DESCRIPTOR_HANDLE descriptor_handle = device->d3d12.descriptor_heap->GetCPUDescriptorHandleForHeapStart(); + descriptor_handle.ptr += (set->descriptor_allocation.offset + index) * device->d3d12.cbv_srv_uav_increment; + device->d3d12.device->CreateUnorderedAccessView(texture->d3d12.resource, NULL, &desc, descriptor_handle); +} + void kope_d3d12_descriptor_set_set_sampler(kope_g5_device *device, kope_d3d12_descriptor_set *set, kope_g5_sampler *sampler, uint32_t index) { D3D12_CPU_DESCRIPTOR_HANDLE src_handle = device->d3d12.all_samplers->GetCPUDescriptorHandleForHeapStart(); src_handle.ptr += sampler->d3d12.sampler_index * device->d3d12.sampler_increment; @@ -68,3 +80,19 @@ void kope_d3d12_descriptor_set_prepare_srv_texture(kope_g5_command_list *list, k texture->d3d12.resource_state = D3D12_RESOURCE_STATE_PIXEL_SHADER_RESOURCE | D3D12_RESOURCE_STATE_NON_PIXEL_SHADER_RESOURCE; } } + +void kope_d3d12_descriptor_set_prepare_uav_texture(kope_g5_command_list *list, kope_g5_texture *texture) { + if (texture->d3d12.resource_state != D3D12_RESOURCE_STATE_UNORDERED_ACCESS) { + D3D12_RESOURCE_BARRIER barrier; + barrier.Transition.pResource = texture->d3d12.resource; + barrier.Type = D3D12_RESOURCE_BARRIER_TYPE_TRANSITION; + barrier.Flags = D3D12_RESOURCE_BARRIER_FLAG_NONE; + barrier.Transition.StateBefore = (D3D12_RESOURCE_STATES)texture->d3d12.resource_state; + barrier.Transition.StateAfter = D3D12_RESOURCE_STATE_UNORDERED_ACCESS; + barrier.Transition.Subresource = D3D12_RESOURCE_BARRIER_ALL_SUBRESOURCES; + + list->d3d12.list->ResourceBarrier(1, &barrier); + + texture->d3d12.resource_state = D3D12_RESOURCE_STATE_UNORDERED_ACCESS; + } +} diff --git a/Backends/Graphics5/Direct3D12/Sources/kope/direct3d12/descriptorset_functions.h b/Backends/Graphics5/Direct3D12/Sources/kope/direct3d12/descriptorset_functions.h index d6c60795f..68a2d1db0 100644 --- a/Backends/Graphics5/Direct3D12/Sources/kope/direct3d12/descriptorset_functions.h +++ b/Backends/Graphics5/Direct3D12/Sources/kope/direct3d12/descriptorset_functions.h @@ -13,10 +13,12 @@ extern "C" { void kope_d3d12_descriptor_set_set_buffer_view_cbv(kope_g5_device *device, kope_d3d12_descriptor_set *set, kope_g5_buffer *buffer, uint32_t index); void kope_d3d12_descriptor_set_set_texture_view_srv(kope_g5_device *device, kope_d3d12_descriptor_set *set, kope_g5_texture *texture, uint32_t index); +void kope_d3d12_descriptor_set_set_texture_view_uav(kope_g5_device *device, kope_d3d12_descriptor_set *set, kope_g5_texture *texture, uint32_t index); void kope_d3d12_descriptor_set_set_sampler(kope_g5_device *device, kope_d3d12_descriptor_set *set, kope_g5_sampler *sampler, uint32_t index); void kope_d3d12_descriptor_set_prepare_cbv_buffer(kope_g5_command_list *list, kope_g5_buffer *buffer); void kope_d3d12_descriptor_set_prepare_srv_texture(kope_g5_command_list *list, kope_g5_texture *texture); +void kope_d3d12_descriptor_set_prepare_uav_texture(kope_g5_command_list *list, kope_g5_texture *texture); #ifdef __cplusplus } diff --git a/Backends/Graphics5/Direct3D12/Sources/kope/direct3d12/pipeline.cpp b/Backends/Graphics5/Direct3D12/Sources/kope/direct3d12/pipeline.cpp index 8f1797a87..03d0134b9 100644 --- a/Backends/Graphics5/Direct3D12/Sources/kope/direct3d12/pipeline.cpp +++ b/Backends/Graphics5/Direct3D12/Sources/kope/direct3d12/pipeline.cpp @@ -124,7 +124,7 @@ static void set_blend_state(D3D12_BLEND_DESC *desc, const kope_d3d12_color_targe desc->RenderTarget[target].RenderTargetWriteMask = (UINT8)target_state->write_mask; } -void kope_d3d12_pipeline_init(kope_d3d12_device *device, kope_d3d12_pipeline *pipe, const kope_d3d12_pipeline_parameters *parameters) { +void kope_d3d12_render_pipeline_init(kope_d3d12_device *device, kope_d3d12_pipeline *pipe, const kope_d3d12_render_pipeline_parameters *parameters) { D3D12_GRAPHICS_PIPELINE_STATE_DESC desc = {0}; desc.VS.BytecodeLength = parameters->vertex.shader.size; @@ -333,7 +333,27 @@ void kope_d3d12_pipeline_init(kope_d3d12_device *device, kope_d3d12_pipeline *pi device->device->CreateRootSignature(0, desc.VS.pShaderBytecode, desc.VS.BytecodeLength, IID_GRAPHICS_PPV_ARGS(&pipe->root_signature))); } -void kope_d3d12_pipeline_destroy(kope_d3d12_pipeline *pipe) { +void kope_d3d12_render_pipeline_destroy(kope_d3d12_pipeline *pipe) { + if (pipe->pipe != NULL) { + pipe->pipe->Release(); + pipe->pipe = NULL; + } +} + +void kope_d3d12_compute_pipeline_init(kope_d3d12_device *device, kope_d3d12_pipeline *pipe, const kope_d3d12_compute_pipeline_parameters *parameters) { + D3D12_COMPUTE_PIPELINE_STATE_DESC desc = {0}; + + desc.CS.pShaderBytecode = parameters->shader.data; + desc.CS.BytecodeLength = parameters->shader.size; + desc.pRootSignature = NULL; + + kinc_microsoft_affirm(device->device->CreateComputePipelineState(&desc, IID_GRAPHICS_PPV_ARGS(&pipe->pipe))); + + kinc_microsoft_affirm( + device->device->CreateRootSignature(0, desc.CS.pShaderBytecode, desc.CS.BytecodeLength, IID_GRAPHICS_PPV_ARGS(&pipe->root_signature))); +} + +void kope_d3d12_compute_pipeline_destroy(kope_d3d12_pipeline *pipe) { if (pipe->pipe != NULL) { pipe->pipe->Release(); pipe->pipe = NULL; diff --git a/Backends/Graphics5/Direct3D12/Sources/kope/direct3d12/pipeline_functions.h b/Backends/Graphics5/Direct3D12/Sources/kope/direct3d12/pipeline_functions.h index 1bdeaefed..9ed693c32 100644 --- a/Backends/Graphics5/Direct3D12/Sources/kope/direct3d12/pipeline_functions.h +++ b/Backends/Graphics5/Direct3D12/Sources/kope/direct3d12/pipeline_functions.h @@ -8,9 +8,13 @@ extern "C" { #endif -void kope_d3d12_pipeline_init(kope_d3d12_device *device, kope_d3d12_pipeline *pipe, const kope_d3d12_pipeline_parameters *parameters); +void kope_d3d12_render_pipeline_init(kope_d3d12_device *device, kope_d3d12_pipeline *pipe, const kope_d3d12_render_pipeline_parameters *parameters); -void kope_d3d12_pipeline_destroy(kope_d3d12_pipeline *pipe); +void kope_d3d12_render_pipeline_destroy(kope_d3d12_pipeline *pipe); + +void kope_d3d12_compute_pipeline_init(kope_d3d12_device *device, kope_d3d12_pipeline *pipe, const kope_d3d12_compute_pipeline_parameters *parameters); + +void kope_d3d12_compute_pipeline_destroy(kope_d3d12_pipeline *pipe); #ifdef __cplusplus } diff --git a/Backends/Graphics5/Direct3D12/Sources/kope/direct3d12/pipeline_structs.h b/Backends/Graphics5/Direct3D12/Sources/kope/direct3d12/pipeline_structs.h index 6639eb9b3..5afc1c0d7 100644 --- a/Backends/Graphics5/Direct3D12/Sources/kope/direct3d12/pipeline_structs.h +++ b/Backends/Graphics5/Direct3D12/Sources/kope/direct3d12/pipeline_structs.h @@ -188,13 +188,17 @@ typedef struct kope_d3d12_fragment_state { size_t targets_count; } kope_d3d12_fragment_state; -typedef struct kope_d3d12_pipeline_parameters { +typedef struct kope_d3d12_render_pipeline_parameters { kope_d3d12_vertex_state vertex; kope_d3d12_primitive_state primitive; kope_d3d12_depth_stencil_state depth_stencil; kope_d3d12_multisample_state multisample; kope_d3d12_fragment_state fragment; -} kope_d3d12_pipeline_parameters; +} kope_d3d12_render_pipeline_parameters; + +typedef struct kope_d3d12_compute_pipeline_parameters { + kope_d3d12_shader shader; +} kope_d3d12_compute_pipeline_parameters; struct ID3D12PipelineState; struct ID3D12RootSignature; diff --git a/Sources/kope/graphics5/commandlist.h b/Sources/kope/graphics5/commandlist.h index 590c97a88..ec8cd08da 100644 --- a/Sources/kope/graphics5/commandlist.h +++ b/Sources/kope/graphics5/commandlist.h @@ -89,8 +89,8 @@ KOPE_FUNC void kope_g5_command_list_copy_texture_to_texture(kope_g5_command_list KOPE_FUNC void kope_g5_command_list_clear_buffer(kope_g5_command_list *list, kope_g5_buffer *buffer, size_t offset, uint64_t size); -// KOPE_FUNC void kope_g5_command_list_resolve_query_set(kope_g5_command_list *list, GPUQuerySet querySet, uint32_t firstQuery, uint32_t queryCount, -// kope_g5_buffer *destination, uint64_t destinationOffset); +// KOPE_FUNC void kope_g5_command_list_resolve_query_set(kope_g5_command_list *list, GPUQuerySet query_set, uint32_t first_query, uint32_t query_count, +// kope_g5_buffer *destination, uint64_t destination_offset); typedef enum kope_g5_index_format { KOPE_G5_INDEX_FORMAT_UINT16, KOPE_G5_INDEX_FORMAT_UINT32 } kope_g5_index_format; @@ -117,7 +117,7 @@ KOPE_FUNC void kope_g5_command_list_set_stencil_reference(kope_g5_command_list * KOPE_FUNC void kope_g5_command_list_begin_occlusion_query(kope_g5_command_list *list, uint32_t query_index); -KOPE_FUNC void kope_g5_command_list_endOcclusionQuery(kope_g5_command_list *list); +KOPE_FUNC void kope_g5_command_list_end_occlusion_query(kope_g5_command_list *list); KOPE_FUNC void kope_g5_command_list_end_render_pass(kope_g5_command_list *list);