Skip to content

Commit

Permalink
Track resource states per mip level
Browse files Browse the repository at this point in the history
  • Loading branch information
RobDangerous committed Sep 22, 2024
1 parent 8aa8058 commit a01860e
Show file tree
Hide file tree
Showing 5 changed files with 39 additions and 37 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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 = {};
Expand Down Expand Up @@ -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 = {};
Expand Down Expand Up @@ -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);
}
}

Expand All @@ -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;
Expand Down Expand Up @@ -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;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
}
14 changes: 8 additions & 6 deletions Backends/Graphics5/Direct3D12/Sources/kope/direct3d12/device.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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;
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit a01860e

Please sign in to comment.