From a01860e88bcedf9588e004cfcdb65110766892a0 Mon Sep 17 00:00:00 2001 From: Robert Konrad Date: Sun, 22 Sep 2024 03:12:40 +0200 Subject: [PATCH] Track resource states per mip level --- .../Sources/kope/direct3d12/commandlist.cpp | 46 +++++++++---------- .../kope/direct3d12/commandlist_functions.h | 2 +- .../Sources/kope/direct3d12/descriptorset.cpp | 12 ++--- .../Sources/kope/direct3d12/device.cpp | 14 +++--- .../Sources/kope/direct3d12/texture_structs.h | 2 +- 5 files changed, 39 insertions(+), 37 deletions(-) diff --git a/Backends/Graphics5/Direct3D12/Sources/kope/direct3d12/commandlist.cpp b/Backends/Graphics5/Direct3D12/Sources/kope/direct3d12/commandlist.cpp index 4770b59c8..a998292c4 100644 --- a/Backends/Graphics5/Direct3D12/Sources/kope/direct3d12/commandlist.cpp +++ b/Backends/Graphics5/Direct3D12/Sources/kope/direct3d12/commandlist.cpp @@ -21,18 +21,18 @@ void kope_d3d12_command_list_begin_render_pass(kope_g5_command_list *list, const list->d3d12.blocking_frame_index = render_target->d3d12.in_flight_frame_index; } - if (render_target->d3d12.resource_state != D3D12_RESOURCE_STATE_RENDER_TARGET) { + if (render_target->d3d12.resource_states[0] != D3D12_RESOURCE_STATE_RENDER_TARGET) { D3D12_RESOURCE_BARRIER barrier; barrier.Transition.pResource = render_target->d3d12.resource; barrier.Type = D3D12_RESOURCE_BARRIER_TYPE_TRANSITION; barrier.Flags = D3D12_RESOURCE_BARRIER_FLAG_NONE; - barrier.Transition.StateBefore = (D3D12_RESOURCE_STATES)render_target->d3d12.resource_state; + barrier.Transition.StateBefore = (D3D12_RESOURCE_STATES)render_target->d3d12.resource_states[0]; barrier.Transition.StateAfter = D3D12_RESOURCE_STATE_RENDER_TARGET; - barrier.Transition.Subresource = D3D12_RESOURCE_BARRIER_ALL_SUBRESOURCES; + barrier.Transition.Subresource = 0; list->d3d12.list->ResourceBarrier(1, &barrier); - render_target->d3d12.resource_state = D3D12_RESOURCE_STATE_RENDER_TARGET; + render_target->d3d12.resource_states[0] = D3D12_RESOURCE_STATE_RENDER_TARGET; } D3D12_RENDER_TARGET_VIEW_DESC desc = {}; @@ -61,18 +61,18 @@ void kope_d3d12_command_list_begin_render_pass(kope_g5_command_list *list, const list->d3d12.blocking_frame_index = render_target->d3d12.in_flight_frame_index; } - if (render_target->d3d12.resource_state != D3D12_RESOURCE_STATE_DEPTH_WRITE) { + if (render_target->d3d12.resource_states[0] != D3D12_RESOURCE_STATE_DEPTH_WRITE) { D3D12_RESOURCE_BARRIER barrier; barrier.Transition.pResource = render_target->d3d12.resource; barrier.Type = D3D12_RESOURCE_BARRIER_TYPE_TRANSITION; barrier.Flags = D3D12_RESOURCE_BARRIER_FLAG_NONE; - barrier.Transition.StateBefore = (D3D12_RESOURCE_STATES)render_target->d3d12.resource_state; + barrier.Transition.StateBefore = (D3D12_RESOURCE_STATES)render_target->d3d12.resource_states[0]; barrier.Transition.StateAfter = D3D12_RESOURCE_STATE_DEPTH_WRITE; - barrier.Transition.Subresource = D3D12_RESOURCE_BARRIER_ALL_SUBRESOURCES; + barrier.Transition.Subresource = 0; list->d3d12.list->ResourceBarrier(1, &barrier); - render_target->d3d12.resource_state = D3D12_RESOURCE_STATE_DEPTH_WRITE; + render_target->d3d12.resource_states[0] = D3D12_RESOURCE_STATE_DEPTH_WRITE; } D3D12_DEPTH_STENCIL_VIEW_DESC desc = {}; @@ -200,12 +200,12 @@ void kope_d3d12_command_list_set_descriptor_table(kope_g5_command_list *list, ui } } -void kope_d3d12_command_list_set_root_constants(kope_g5_command_list *list, const void *data, size_t data_size) { +void kope_d3d12_command_list_set_root_constants(kope_g5_command_list *list, uint32_t table_index, const void *data, size_t data_size) { if (list->d3d12.compute_pipeline_set) { - list->d3d12.list->SetComputeRoot32BitConstants(0, (UINT)(data_size / 4), data, 0); + list->d3d12.list->SetComputeRoot32BitConstants(table_index, (UINT)(data_size / 4), data, 0); } else { - list->d3d12.list->SetGraphicsRoot32BitConstants(0, (UINT)(data_size / 4), data, 0); + list->d3d12.list->SetGraphicsRoot32BitConstants(table_index, (UINT)(data_size / 4), data, 0); } } @@ -226,18 +226,18 @@ void kope_d3d12_command_list_copy_buffer_to_texture(kope_g5_command_list *list, source->buffer->d3d12.resource_state = D3D12_RESOURCE_STATE_COPY_SOURCE; } - if (destination->texture->d3d12.resource_state != D3D12_RESOURCE_STATE_COPY_DEST) { + if (destination->texture->d3d12.resource_states[0] != D3D12_RESOURCE_STATE_COPY_DEST) { D3D12_RESOURCE_BARRIER barrier; barrier.Transition.pResource = destination->texture->d3d12.resource; barrier.Type = D3D12_RESOURCE_BARRIER_TYPE_TRANSITION; barrier.Flags = D3D12_RESOURCE_BARRIER_FLAG_NONE; - barrier.Transition.StateBefore = (D3D12_RESOURCE_STATES)destination->texture->d3d12.resource_state; + barrier.Transition.StateBefore = (D3D12_RESOURCE_STATES)destination->texture->d3d12.resource_states[0]; barrier.Transition.StateAfter = D3D12_RESOURCE_STATE_COPY_DEST; - barrier.Transition.Subresource = D3D12_RESOURCE_BARRIER_ALL_SUBRESOURCES; + barrier.Transition.Subresource = 0; list->d3d12.list->ResourceBarrier(1, &barrier); - destination->texture->d3d12.resource_state = D3D12_RESOURCE_STATE_COPY_DEST; + destination->texture->d3d12.resource_states[0] = D3D12_RESOURCE_STATE_COPY_DEST; } D3D12_TEXTURE_COPY_LOCATION dst; @@ -269,32 +269,32 @@ void kope_d3d12_command_list_copy_buffer_to_texture(kope_g5_command_list *list, void kope_d3d12_command_list_copy_texture_to_texture(kope_g5_command_list *list, const kope_g5_image_copy_texture *source, const kope_g5_image_copy_texture *destination, uint32_t width, uint32_t height, uint32_t depth_or_array_layers) { - if (source->texture->d3d12.resource_state != D3D12_RESOURCE_STATE_COPY_SOURCE) { + if (source->texture->d3d12.resource_states[0] != D3D12_RESOURCE_STATE_COPY_SOURCE) { D3D12_RESOURCE_BARRIER barrier; barrier.Transition.pResource = source->texture->d3d12.resource; barrier.Type = D3D12_RESOURCE_BARRIER_TYPE_TRANSITION; barrier.Flags = D3D12_RESOURCE_BARRIER_FLAG_NONE; - barrier.Transition.StateBefore = (D3D12_RESOURCE_STATES)source->texture->d3d12.resource_state; + barrier.Transition.StateBefore = (D3D12_RESOURCE_STATES)source->texture->d3d12.resource_states[0]; barrier.Transition.StateAfter = D3D12_RESOURCE_STATE_COPY_SOURCE; - barrier.Transition.Subresource = D3D12_RESOURCE_BARRIER_ALL_SUBRESOURCES; + barrier.Transition.Subresource = 0; list->d3d12.list->ResourceBarrier(1, &barrier); - source->texture->d3d12.resource_state = D3D12_RESOURCE_STATE_COPY_SOURCE; + source->texture->d3d12.resource_states[0] = D3D12_RESOURCE_STATE_COPY_SOURCE; } - if (destination->texture->d3d12.resource_state != D3D12_RESOURCE_STATE_COPY_DEST) { + if (destination->texture->d3d12.resource_states[0] != D3D12_RESOURCE_STATE_COPY_DEST) { D3D12_RESOURCE_BARRIER barrier; barrier.Transition.pResource = destination->texture->d3d12.resource; barrier.Type = D3D12_RESOURCE_BARRIER_TYPE_TRANSITION; barrier.Flags = D3D12_RESOURCE_BARRIER_FLAG_NONE; - barrier.Transition.StateBefore = (D3D12_RESOURCE_STATES)destination->texture->d3d12.resource_state; + barrier.Transition.StateBefore = (D3D12_RESOURCE_STATES)destination->texture->d3d12.resource_states[0]; barrier.Transition.StateAfter = D3D12_RESOURCE_STATE_COPY_DEST; - barrier.Transition.Subresource = D3D12_RESOURCE_BARRIER_ALL_SUBRESOURCES; + barrier.Transition.Subresource = 0; list->d3d12.list->ResourceBarrier(1, &barrier); - destination->texture->d3d12.resource_state = D3D12_RESOURCE_STATE_COPY_DEST; + destination->texture->d3d12.resource_states[0] = D3D12_RESOURCE_STATE_COPY_DEST; } D3D12_TEXTURE_COPY_LOCATION dst; diff --git a/Backends/Graphics5/Direct3D12/Sources/kope/direct3d12/commandlist_functions.h b/Backends/Graphics5/Direct3D12/Sources/kope/direct3d12/commandlist_functions.h index 3b2d0444f..b07a903a7 100644 --- a/Backends/Graphics5/Direct3D12/Sources/kope/direct3d12/commandlist_functions.h +++ b/Backends/Graphics5/Direct3D12/Sources/kope/direct3d12/commandlist_functions.h @@ -37,7 +37,7 @@ void kope_d3d12_command_list_draw_indexed(kope_g5_command_list *list, uint32_t i void kope_d3d12_command_list_set_descriptor_table(kope_g5_command_list *list, uint32_t table_index, kope_d3d12_descriptor_set *set); -void kope_d3d12_command_list_set_root_constants(kope_g5_command_list *list, const void *data, size_t data_size); +void kope_d3d12_command_list_set_root_constants(kope_g5_command_list *list, uint32_t table_index, const void *data, size_t data_size); void kope_d3d12_command_list_set_compute_pipeline(kope_g5_command_list *list, kope_d3d12_compute_pipeline *pipeline); diff --git a/Backends/Graphics5/Direct3D12/Sources/kope/direct3d12/descriptorset.cpp b/Backends/Graphics5/Direct3D12/Sources/kope/direct3d12/descriptorset.cpp index 73c7f9c7c..e7cfeca9b 100644 --- a/Backends/Graphics5/Direct3D12/Sources/kope/direct3d12/descriptorset.cpp +++ b/Backends/Graphics5/Direct3D12/Sources/kope/direct3d12/descriptorset.cpp @@ -137,33 +137,33 @@ inline UINT D3D12CalcSubresource(UINT MipSlice, UINT ArraySlice, UINT PlaneSlice } void kope_d3d12_descriptor_set_prepare_srv_texture(kope_g5_command_list *list, kope_g5_texture *texture, uint32_t mip_level) { - if (texture->d3d12.resource_state != (D3D12_RESOURCE_STATE_PIXEL_SHADER_RESOURCE | D3D12_RESOURCE_STATE_NON_PIXEL_SHADER_RESOURCE)) { + if (texture->d3d12.resource_states[mip_level] != (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.StateBefore = (D3D12_RESOURCE_STATES)texture->d3d12.resource_states[mip_level]; barrier.Transition.StateAfter = D3D12_RESOURCE_STATE_PIXEL_SHADER_RESOURCE | D3D12_RESOURCE_STATE_NON_PIXEL_SHADER_RESOURCE; barrier.Transition.Subresource = D3D12CalcSubresource(mip_level, 0, 0, 0, 0); list->d3d12.list->ResourceBarrier(1, &barrier); - texture->d3d12.resource_state = D3D12_RESOURCE_STATE_PIXEL_SHADER_RESOURCE | D3D12_RESOURCE_STATE_NON_PIXEL_SHADER_RESOURCE; + texture->d3d12.resource_states[mip_level] = 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, uint32_t mip_level) { - if (texture->d3d12.resource_state != D3D12_RESOURCE_STATE_UNORDERED_ACCESS) { + if (texture->d3d12.resource_states[mip_level] != 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.StateBefore = (D3D12_RESOURCE_STATES)texture->d3d12.resource_states[mip_level]; barrier.Transition.StateAfter = D3D12_RESOURCE_STATE_UNORDERED_ACCESS; barrier.Transition.Subresource = D3D12CalcSubresource(mip_level, 0, 0, 0, 0); list->d3d12.list->ResourceBarrier(1, &barrier); - texture->d3d12.resource_state = D3D12_RESOURCE_STATE_UNORDERED_ACCESS; + texture->d3d12.resource_states[mip_level] = D3D12_RESOURCE_STATE_UNORDERED_ACCESS; } } diff --git a/Backends/Graphics5/Direct3D12/Sources/kope/direct3d12/device.cpp b/Backends/Graphics5/Direct3D12/Sources/kope/direct3d12/device.cpp index 2e503ae3e..92903074a 100644 --- a/Backends/Graphics5/Direct3D12/Sources/kope/direct3d12/device.cpp +++ b/Backends/Graphics5/Direct3D12/Sources/kope/direct3d12/device.cpp @@ -104,7 +104,7 @@ void kope_d3d12_device_create(kope_g5_device *device, const kope_g5_device_wishl parameters.dimension = KOPE_G5_TEXTURE_DIMENSION_2D; parameters.usage = KONG_G5_TEXTURE_USAGE_RENDER_ATTACHMENT; - device->d3d12.framebuffer_textures[i].d3d12.resource_state = D3D12_RESOURCE_STATE_PRESENT; + device->d3d12.framebuffer_textures[i].d3d12.resource_states[0] = D3D12_RESOURCE_STATE_PRESENT; } device->d3d12.framebuffer_index = 0; @@ -430,7 +430,9 @@ void kope_d3d12_device_create_texture(kope_g5_device *device, const kope_g5_text D3D12_RESOURCE_STATE_PIXEL_SHADER_RESOURCE, optimizedClearValuePointer, IID_GRAPHICS_PPV_ARGS(&texture->d3d12.resource))); - texture->d3d12.resource_state = D3D12_RESOURCE_STATE_PIXEL_SHADER_RESOURCE; + for (size_t mip_level = 0; mip_level < parameters->mip_level_count; ++mip_level) { + texture->d3d12.resource_states[mip_level] = D3D12_RESOURCE_STATE_PIXEL_SHADER_RESOURCE; + } texture->d3d12.format = format; texture->d3d12.width = parameters->width; @@ -461,18 +463,18 @@ static void wait_for_frame(kope_g5_device *device, uint64_t frame_index) { void kope_d3d12_device_execute_command_list(kope_g5_device *device, kope_g5_command_list *list) { if (list->d3d12.presenting) { kope_g5_texture *framebuffer = kope_d3d12_device_get_framebuffer(device); - if (framebuffer->d3d12.resource_state != D3D12_RESOURCE_STATE_PRESENT) { + if (framebuffer->d3d12.resource_states[0] != D3D12_RESOURCE_STATE_PRESENT) { D3D12_RESOURCE_BARRIER barrier; barrier.Transition.pResource = framebuffer->d3d12.resource; barrier.Type = D3D12_RESOURCE_BARRIER_TYPE_TRANSITION; barrier.Flags = D3D12_RESOURCE_BARRIER_FLAG_NONE; - barrier.Transition.StateBefore = (D3D12_RESOURCE_STATES)framebuffer->d3d12.resource_state; + barrier.Transition.StateBefore = (D3D12_RESOURCE_STATES)framebuffer->d3d12.resource_states[0]; barrier.Transition.StateAfter = D3D12_RESOURCE_STATE_PRESENT; - barrier.Transition.Subresource = D3D12_RESOURCE_BARRIER_ALL_SUBRESOURCES; + barrier.Transition.Subresource = 0; list->d3d12.list->ResourceBarrier(1, &barrier); - framebuffer->d3d12.resource_state = D3D12_RESOURCE_STATE_PRESENT; + framebuffer->d3d12.resource_states[0] = D3D12_RESOURCE_STATE_PRESENT; } } diff --git a/Backends/Graphics5/Direct3D12/Sources/kope/direct3d12/texture_structs.h b/Backends/Graphics5/Direct3D12/Sources/kope/direct3d12/texture_structs.h index a8aea2a71..a07956ad0 100644 --- a/Backends/Graphics5/Direct3D12/Sources/kope/direct3d12/texture_structs.h +++ b/Backends/Graphics5/Direct3D12/Sources/kope/direct3d12/texture_structs.h @@ -13,7 +13,7 @@ typedef struct kope_d3d12_texture { uint32_t width; uint32_t height; - uint32_t resource_state; + uint32_t resource_states[64]; uint32_t format; // used for framebuffer-synchronization