diff --git a/Backends/Graphics5/Direct3D12/Sources/kope/direct3d12/buffer_structs.h b/Backends/Graphics5/Direct3D12/Sources/kope/direct3d12/buffer_structs.h index fa94bdd6d..a39c59469 100644 --- a/Backends/Graphics5/Direct3D12/Sources/kope/direct3d12/buffer_structs.h +++ b/Backends/Graphics5/Direct3D12/Sources/kope/direct3d12/buffer_structs.h @@ -3,12 +3,15 @@ #include "d3d12mini.h" +#include + #ifdef __cplusplus extern "C" { #endif typedef struct kope_d3d12_buffer { struct ID3D12Resource *resource; + uint32_t resource_state; size_t size; } kope_d3d12_buffer; diff --git a/Backends/Graphics5/Direct3D12/Sources/kope/direct3d12/commandlist.cpp b/Backends/Graphics5/Direct3D12/Sources/kope/direct3d12/commandlist.cpp index afc977d96..52aba5762 100644 --- a/Backends/Graphics5/Direct3D12/Sources/kope/direct3d12/commandlist.cpp +++ b/Backends/Graphics5/Direct3D12/Sources/kope/direct3d12/commandlist.cpp @@ -104,6 +104,34 @@ void kope_d3d12_command_list_set_descriptor_table(kope_g5_command_list *list, ui } void kope_g5_command_list_copy_buffer_to_texture(kope_g5_command_list *list, kope_g5_buffer *source, kope_g5_texture *destination, kope_uint3 size) { + if (source->d3d12.resource_state != D3D12_RESOURCE_STATE_COPY_SOURCE) { + D3D12_RESOURCE_BARRIER barrier; + barrier.Transition.pResource = source->d3d12.resource; + barrier.Type = D3D12_RESOURCE_BARRIER_TYPE_TRANSITION; + barrier.Flags = D3D12_RESOURCE_BARRIER_FLAG_NONE; + barrier.Transition.StateBefore = (D3D12_RESOURCE_STATES)source->d3d12.resource_state; + barrier.Transition.StateAfter = D3D12_RESOURCE_STATE_COPY_SOURCE; + barrier.Transition.Subresource = D3D12_RESOURCE_BARRIER_ALL_SUBRESOURCES; + + list->d3d12.list->ResourceBarrier(1, &barrier); + + source->d3d12.resource_state = D3D12_RESOURCE_STATE_COPY_SOURCE; + } + + if (destination->d3d12.resource_state != D3D12_RESOURCE_STATE_COPY_DEST) { + D3D12_RESOURCE_BARRIER barrier; + barrier.Transition.pResource = destination->d3d12.resource; + barrier.Type = D3D12_RESOURCE_BARRIER_TYPE_TRANSITION; + barrier.Flags = D3D12_RESOURCE_BARRIER_FLAG_NONE; + barrier.Transition.StateBefore = (D3D12_RESOURCE_STATES)destination->d3d12.resource_state; + barrier.Transition.StateAfter = D3D12_RESOURCE_STATE_COPY_DEST; + barrier.Transition.Subresource = D3D12_RESOURCE_BARRIER_ALL_SUBRESOURCES; + + list->d3d12.list->ResourceBarrier(1, &barrier); + + destination->d3d12.resource_state = D3D12_RESOURCE_STATE_COPY_DEST; + } + D3D12_TEXTURE_COPY_LOCATION dst; dst.pResource = destination->d3d12.resource; dst.Type = D3D12_TEXTURE_COPY_TYPE_SUBRESOURCE_INDEX; diff --git a/Backends/Graphics5/Direct3D12/Sources/kope/direct3d12/descriptorset.cpp b/Backends/Graphics5/Direct3D12/Sources/kope/direct3d12/descriptorset.cpp index 869c9d1e9..e284a6e90 100644 --- a/Backends/Graphics5/Direct3D12/Sources/kope/direct3d12/descriptorset.cpp +++ b/Backends/Graphics5/Direct3D12/Sources/kope/direct3d12/descriptorset.cpp @@ -36,3 +36,35 @@ void kope_d3d12_descriptor_set_set_sampler(kope_g5_device *device, kope_d3d12_de device->d3d12.device->CopyDescriptorsSimple(1, dst_handle, src_handle, D3D12_DESCRIPTOR_HEAP_TYPE_SAMPLER); } + +void kope_d3d12_descriptor_set_prepare_cbv_buffer(kope_g5_command_list *list, kope_g5_buffer *buffer) { + if (buffer->d3d12.resource_state != D3D12_RESOURCE_STATE_VERTEX_AND_CONSTANT_BUFFER) { + D3D12_RESOURCE_BARRIER barrier; + barrier.Transition.pResource = buffer->d3d12.resource; + barrier.Type = D3D12_RESOURCE_BARRIER_TYPE_TRANSITION; + barrier.Flags = D3D12_RESOURCE_BARRIER_FLAG_NONE; + barrier.Transition.StateBefore = (D3D12_RESOURCE_STATES)buffer->d3d12.resource_state; + barrier.Transition.StateAfter = D3D12_RESOURCE_STATE_VERTEX_AND_CONSTANT_BUFFER; + barrier.Transition.Subresource = D3D12_RESOURCE_BARRIER_ALL_SUBRESOURCES; + + list->d3d12.list->ResourceBarrier(1, &barrier); + + buffer->d3d12.resource_state = D3D12_RESOURCE_STATE_VERTEX_AND_CONSTANT_BUFFER; + } +} + +void kope_d3d12_descriptor_set_prepare_srv_texture(kope_g5_command_list *list, kope_g5_texture *texture) { + if (texture->d3d12.resource_state != (D3D12_RESOURCE_STATE_PIXEL_SHADER_RESOURCE | D3D12_RESOURCE_STATE_NON_PIXEL_SHADER_RESOURCE)) { + 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_PIXEL_SHADER_RESOURCE | D3D12_RESOURCE_STATE_NON_PIXEL_SHADER_RESOURCE; + barrier.Transition.Subresource = D3D12_RESOURCE_BARRIER_ALL_SUBRESOURCES; + + list->d3d12.list->ResourceBarrier(1, &barrier); + + texture->d3d12.resource_state = D3D12_RESOURCE_STATE_PIXEL_SHADER_RESOURCE | D3D12_RESOURCE_STATE_NON_PIXEL_SHADER_RESOURCE; + } +} diff --git a/Backends/Graphics5/Direct3D12/Sources/kope/direct3d12/descriptorset_functions.h b/Backends/Graphics5/Direct3D12/Sources/kope/direct3d12/descriptorset_functions.h index 5f023ecf4..d6c60795f 100644 --- a/Backends/Graphics5/Direct3D12/Sources/kope/direct3d12/descriptorset_functions.h +++ b/Backends/Graphics5/Direct3D12/Sources/kope/direct3d12/descriptorset_functions.h @@ -15,6 +15,9 @@ void kope_d3d12_descriptor_set_set_buffer_view_cbv(kope_g5_device *device, kope_ 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_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); + #ifdef __cplusplus } #endif diff --git a/Backends/Graphics5/Direct3D12/Sources/kope/direct3d12/device.cpp b/Backends/Graphics5/Direct3D12/Sources/kope/direct3d12/device.cpp index 83f16b48e..e77783bf1 100644 --- a/Backends/Graphics5/Direct3D12/Sources/kope/direct3d12/device.cpp +++ b/Backends/Graphics5/Direct3D12/Sources/kope/direct3d12/device.cpp @@ -190,6 +190,8 @@ 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; + 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))); }