Skip to content

Commit

Permalink
Make texture arrays work
Browse files Browse the repository at this point in the history
  • Loading branch information
RobDangerous committed Sep 22, 2024
1 parent ca2333e commit d98750c
Show file tree
Hide file tree
Showing 5 changed files with 66 additions and 24 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -233,37 +233,40 @@ void kope_d3d12_command_list_copy_buffer_to_texture(kope_g5_command_list *list,
barrier.Flags = D3D12_RESOURCE_BARRIER_FLAG_NONE;
barrier.Transition.StateBefore = (D3D12_RESOURCE_STATES)destination->texture->d3d12.resource_states[destination->mip_level];
barrier.Transition.StateAfter = D3D12_RESOURCE_STATE_COPY_DEST;
barrier.Transition.Subresource = D3D12CalcSubresource(destination->mip_level, 0, 0, 0, 0);
barrier.Transition.Subresource = D3D12CalcSubresource(destination->mip_level, destination->origin_z, 0, destination->texture->d3d12.mip_level_count,
destination->texture->d3d12.depth_or_array_layers);

list->d3d12.list->ResourceBarrier(1, &barrier);

destination->texture->d3d12.resource_states[destination->mip_level] = D3D12_RESOURCE_STATE_COPY_DEST;
}

D3D12_TEXTURE_COPY_LOCATION dst;
dst.pResource = destination->texture->d3d12.resource;
dst.Type = D3D12_TEXTURE_COPY_TYPE_SUBRESOURCE_INDEX;
dst.SubresourceIndex = D3D12CalcSubresource(destination->mip_level, 0, 0, 0, 0);

D3D12_TEXTURE_COPY_LOCATION src;
src.pResource = source->buffer->d3d12.resource;
src.Type = D3D12_TEXTURE_COPY_TYPE_PLACED_FOOTPRINT;
src.PlacedFootprint.Offset = source->offset;
src.PlacedFootprint.Footprint.Depth = 1;
src.PlacedFootprint.Footprint.Format = DXGI_FORMAT_R8G8B8A8_UNORM;
src.PlacedFootprint.Footprint.Height = width;
src.PlacedFootprint.Footprint.Width = height;
src.PlacedFootprint.Footprint.RowPitch = source->bytes_per_row;
src.PlacedFootprint.Footprint.Width = height;
src.PlacedFootprint.Footprint.Height = width;
src.PlacedFootprint.Footprint.Depth = depth_or_array_layers;

D3D12_BOX source_box = {0};
source_box.left = 0;
source_box.right = source_box.left + width;
source_box.top = 0;
source_box.bottom = source_box.top + height;
source_box.front = 0;
source_box.back = 1;
source_box.back = source_box.front + depth_or_array_layers;

list->d3d12.list->CopyTextureRegion(&dst, destination->origin_x, destination->origin_y, destination->origin_z, &src, &source_box);
D3D12_TEXTURE_COPY_LOCATION dst;
dst.pResource = destination->texture->d3d12.resource;
dst.Type = D3D12_TEXTURE_COPY_TYPE_SUBRESOURCE_INDEX;
dst.SubresourceIndex = D3D12CalcSubresource(destination->mip_level, destination->origin_z, 0, destination->texture->d3d12.mip_level_count,
destination->texture->d3d12.depth_or_array_layers);

list->d3d12.list->CopyTextureRegion(&dst, destination->origin_x, destination->origin_y, 0, &src,
&source_box); // Set DstZ to zero because it has already been selected via dst.SubresourceIndex
}

void kope_d3d12_command_list_copy_texture_to_texture(kope_g5_command_list *list, const kope_g5_image_copy_texture *source,
Expand All @@ -276,7 +279,8 @@ void kope_d3d12_command_list_copy_texture_to_texture(kope_g5_command_list *list,
barrier.Flags = D3D12_RESOURCE_BARRIER_FLAG_NONE;
barrier.Transition.StateBefore = (D3D12_RESOURCE_STATES)source->texture->d3d12.resource_states[source->mip_level];
barrier.Transition.StateAfter = D3D12_RESOURCE_STATE_COPY_SOURCE;
barrier.Transition.Subresource = D3D12CalcSubresource(source->mip_level, 0, 0, 0, 0);
barrier.Transition.Subresource =
D3D12CalcSubresource(source->mip_level, source->origin_z, 0, source->texture->d3d12.mip_level_count, source->texture->d3d12.depth_or_array_layers);

list->d3d12.list->ResourceBarrier(1, &barrier);

Expand All @@ -290,22 +294,19 @@ void kope_d3d12_command_list_copy_texture_to_texture(kope_g5_command_list *list,
barrier.Flags = D3D12_RESOURCE_BARRIER_FLAG_NONE;
barrier.Transition.StateBefore = (D3D12_RESOURCE_STATES)destination->texture->d3d12.resource_states[destination->mip_level];
barrier.Transition.StateAfter = D3D12_RESOURCE_STATE_COPY_DEST;
barrier.Transition.Subresource = D3D12CalcSubresource(destination->mip_level, 0, 0, 0, 0);
barrier.Transition.Subresource = D3D12CalcSubresource(destination->mip_level, destination->origin_z, 0, destination->texture->d3d12.mip_level_count,
destination->texture->d3d12.depth_or_array_layers);

list->d3d12.list->ResourceBarrier(1, &barrier);

destination->texture->d3d12.resource_states[destination->mip_level] = D3D12_RESOURCE_STATE_COPY_DEST;
}

D3D12_TEXTURE_COPY_LOCATION dst;
dst.pResource = destination->texture->d3d12.resource;
dst.Type = D3D12_TEXTURE_COPY_TYPE_SUBRESOURCE_INDEX;
dst.SubresourceIndex = D3D12CalcSubresource(destination->mip_level, 0, 0, 0, 0);

D3D12_TEXTURE_COPY_LOCATION src;
src.pResource = source->texture->d3d12.resource;
src.Type = D3D12_TEXTURE_COPY_TYPE_SUBRESOURCE_INDEX;
src.SubresourceIndex = D3D12CalcSubresource(source->mip_level, 0, 0, 0, 0);
src.SubresourceIndex =
D3D12CalcSubresource(source->mip_level, source->origin_z, 0, source->texture->d3d12.mip_level_count, source->texture->d3d12.depth_or_array_layers);

D3D12_BOX source_box = {0};
source_box.left = source->origin_x;
Expand All @@ -315,6 +316,12 @@ void kope_d3d12_command_list_copy_texture_to_texture(kope_g5_command_list *list,
source_box.front = 0;
source_box.back = 1;

D3D12_TEXTURE_COPY_LOCATION dst;
dst.pResource = destination->texture->d3d12.resource;
dst.Type = D3D12_TEXTURE_COPY_TYPE_SUBRESOURCE_INDEX;
dst.SubresourceIndex = D3D12CalcSubresource(destination->mip_level, destination->origin_z, 0, destination->texture->d3d12.mip_level_count,
destination->texture->d3d12.depth_or_array_layers);

list->d3d12.list->CopyTextureRegion(&dst, destination->origin_x, destination->origin_y, destination->origin_z, &src, &source_box);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,32 @@ 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_array_view_srv(kope_g5_device *device, kope_d3d12_descriptor_set *set, kope_g5_texture *texture, uint32_t index) {
D3D12_SHADER_RESOURCE_VIEW_DESC desc = {};
desc.ViewDimension = D3D12_SRV_DIMENSION_TEXTURE2DARRAY;
desc.Shader4ComponentMapping = D3D12_DEFAULT_SHADER_4_COMPONENT_MAPPING;

DXGI_FORMAT format = (DXGI_FORMAT)texture->d3d12.format;
switch (format) {
case DXGI_FORMAT_D16_UNORM:
desc.Format = DXGI_FORMAT_R16_UNORM;
break;
default:
desc.Format = format;
break;
}

desc.Texture2DArray.MipLevels = 1;
desc.Texture2DArray.MostDetailedMip = 0;
desc.Texture2DArray.ResourceMinLODClamp = 0.0f;
desc.Texture2DArray.FirstArraySlice = 0;
desc.Texture2DArray.ArraySize = 2;

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->CreateShaderResourceView(texture->d3d12.resource, &desc, descriptor_handle);
}

void kope_d3d12_descriptor_set_set_texture_cube_view_srv(kope_g5_device *device, kope_d3d12_descriptor_set *set, kope_g5_texture *texture, uint32_t index) {
D3D12_SHADER_RESOURCE_VIEW_DESC desc = {};
desc.ViewDimension = D3D12_SRV_DIMENSION_TEXTURECUBE;
Expand All @@ -83,9 +109,9 @@ void kope_d3d12_descriptor_set_set_texture_cube_view_srv(kope_g5_device *device,
break;
}

desc.Texture2D.MipLevels = 1;
desc.Texture2D.MostDetailedMip = 0;
desc.Texture2D.ResourceMinLODClamp = 0.0f;
desc.TextureCube.MipLevels = 1;
desc.TextureCube.MostDetailedMip = 0;
desc.TextureCube.ResourceMinLODClamp = 0.0f;

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;
Expand Down Expand Up @@ -140,7 +166,7 @@ void kope_d3d12_descriptor_set_prepare_srv_texture(kope_g5_command_list *list, k
barrier.Flags = D3D12_RESOURCE_BARRIER_FLAG_NONE;
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);
barrier.Transition.Subresource = D3D12CalcSubresource(mip_level, 0, 0, texture->d3d12.mip_level_count, texture->d3d12.depth_or_array_layers);

list->d3d12.list->ResourceBarrier(1, &barrier);

Expand All @@ -157,7 +183,7 @@ void kope_d3d12_descriptor_set_prepare_uav_texture(kope_g5_command_list *list, k
barrier.Flags = D3D12_RESOURCE_BARRIER_FLAG_NONE;
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);
barrier.Transition.Subresource = D3D12CalcSubresource(mip_level, 0, 0, texture->d3d12.mip_level_count, texture->d3d12.depth_or_array_layers);

list->d3d12.list->ResourceBarrier(1, &barrier);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ void kope_d3d12_descriptor_set_set_buffer_view_srv(kope_g5_device *device, kope_
void kope_d3d12_descriptor_set_set_bvh_view_srv(kope_g5_device *device, kope_d3d12_descriptor_set *set, kope_g5_raytracing_hierarchy *bvh, 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 highest_mip_level, uint32_t mip_count, uint32_t index);
void kope_d3d12_descriptor_set_set_texture_array_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_cube_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 mip_level,
uint32_t index);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,10 @@ void kope_d3d12_device_create(kope_g5_device *device, const kope_g5_device_wishl

device->d3d12.framebuffer_textures[i].d3d12.width = kinc_window_width(0);
device->d3d12.framebuffer_textures[i].d3d12.height = kinc_window_height(0);
device->d3d12.framebuffer_textures[i].d3d12.depth_or_array_layers = 1;
device->d3d12.framebuffer_textures[i].d3d12.mip_level_count = 1;

device->d3d12.framebuffer_textures[i].d3d12.in_flight_frame_index = 0;

#ifdef KOPE_G5_VALIDATION
device->d3d12.framebuffer_textures[i].validation_format = KOPE_G5_TEXTURE_FORMAT_RGBA8_UNORM;
Expand Down Expand Up @@ -437,6 +441,8 @@ void kope_d3d12_device_create_texture(kope_g5_device *device, const kope_g5_text

texture->d3d12.width = parameters->width;
texture->d3d12.height = parameters->height;
texture->d3d12.depth_or_array_layers = parameters->depth_or_array_layers;
texture->d3d12.mip_level_count = parameters->mip_level_count;

texture->d3d12.in_flight_frame_index = 0;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@ typedef struct kope_d3d12_texture {

uint32_t width;
uint32_t height;
uint32_t depth_or_array_layers;
uint32_t mip_level_count;

uint32_t resource_states[64];
uint32_t format;
Expand Down

0 comments on commit d98750c

Please sign in to comment.