Skip to content

Commit

Permalink
Introduce texture views
Browse files Browse the repository at this point in the history
  • Loading branch information
RobDangerous committed Sep 27, 2024
1 parent 3b58a72 commit 7a6d3f1
Show file tree
Hide file tree
Showing 8 changed files with 124 additions and 70 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@
#include <kope/graphics5/commandlist.h>
#include <kope/graphics5/device.h>

#include <kope/direct3d12/texture_functions.h>

#include "pipeline_structs.h"

#include <assert.h>
Expand Down Expand Up @@ -226,19 +228,25 @@ 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_states[destination->mip_level] != 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_states[destination->mip_level];
barrier.Transition.StateAfter = D3D12_RESOURCE_STATE_COPY_DEST;
barrier.Transition.Subresource = D3D12CalcSubresource(destination->mip_level, destination->origin_z, 0, destination->texture->d3d12.mip_level_count,
destination->texture->d3d12.depth_or_array_layers);
for (uint32_t array_layer = destination->origin_z; array_layer < destination->origin_z + depth_or_array_layers; ++array_layer) {
if (destination->texture->d3d12.resource_states[kope_d3d12_texture_resource_state_index(destination->texture, destination->mip_level, array_layer)] !=
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_states[kope_d3d12_texture_resource_state_index(destination->texture, destination->mip_level, array_layer)];
barrier.Transition.StateAfter = D3D12_RESOURCE_STATE_COPY_DEST;
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);
list->d3d12.list->ResourceBarrier(1, &barrier);

destination->texture->d3d12.resource_states[destination->mip_level] = D3D12_RESOURCE_STATE_COPY_DEST;
destination->texture->d3d12.resource_states[kope_d3d12_texture_resource_state_index(destination->texture, destination->mip_level, array_layer)] =
D3D12_RESOURCE_STATE_COPY_DEST;
}
}

D3D12_TEXTURE_COPY_LOCATION src;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
#include "descriptorset_functions.h"
#include "descriptorset_structs.h"

#include <kope/direct3d12/texture_functions.h>

#include <kope/util/align.h>

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) {
Expand Down Expand Up @@ -40,13 +42,13 @@ void kope_d3d12_descriptor_set_set_bvh_view_srv(kope_g5_device *device, kope_d3d
device->d3d12.device->CreateShaderResourceView(nullptr, &desc, descriptor_handle);
}

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_view_srv(kope_g5_device *device, kope_d3d12_descriptor_set *set, const kope_g5_texture_view *texture_view,
uint32_t index) {
D3D12_SHADER_RESOURCE_VIEW_DESC desc = {};
desc.ViewDimension = D3D12_SRV_DIMENSION_TEXTURE2D;
desc.Shader4ComponentMapping = D3D12_DEFAULT_SHADER_4_COMPONENT_MAPPING;

DXGI_FORMAT format = (DXGI_FORMAT)texture->d3d12.format;
DXGI_FORMAT format = (DXGI_FORMAT)texture_view->texture->d3d12.format;
switch (format) {
case DXGI_FORMAT_D16_UNORM:
desc.Format = DXGI_FORMAT_R16_UNORM;
Expand All @@ -59,21 +61,22 @@ void kope_d3d12_descriptor_set_set_texture_view_srv(kope_g5_device *device, kope
break;
}

desc.Texture2D.MipLevels = mip_count;
desc.Texture2D.MostDetailedMip = highest_mip_level;
desc.Texture2D.MipLevels = texture_view->mip_level_count;
desc.Texture2D.MostDetailedMip = texture_view->base_mip_level;
desc.Texture2D.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;
device->d3d12.device->CreateShaderResourceView(texture->d3d12.resource, &desc, descriptor_handle);
device->d3d12.device->CreateShaderResourceView(texture_view->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) {
void kope_d3d12_descriptor_set_set_texture_array_view_srv(kope_g5_device *device, kope_d3d12_descriptor_set *set, const kope_g5_texture_view *texture_view,
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;
DXGI_FORMAT format = (DXGI_FORMAT)texture_view->texture->d3d12.format;
switch (format) {
case DXGI_FORMAT_D16_UNORM:
desc.Format = DXGI_FORMAT_R16_UNORM;
Expand All @@ -83,23 +86,24 @@ void kope_d3d12_descriptor_set_set_texture_array_view_srv(kope_g5_device *device
break;
}

desc.Texture2DArray.MipLevels = 1;
desc.Texture2DArray.MostDetailedMip = 0;
desc.Texture2DArray.MipLevels = texture_view->mip_level_count;
desc.Texture2DArray.MostDetailedMip = texture_view->base_mip_level;
desc.Texture2DArray.ResourceMinLODClamp = 0.0f;
desc.Texture2DArray.FirstArraySlice = 0;
desc.Texture2DArray.ArraySize = 2;
desc.Texture2DArray.FirstArraySlice = texture_view->base_array_layer;
desc.Texture2DArray.ArraySize = texture_view->array_layer_count;

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);
device->d3d12.device->CreateShaderResourceView(texture_view->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) {
void kope_d3d12_descriptor_set_set_texture_cube_view_srv(kope_g5_device *device, kope_d3d12_descriptor_set *set, const kope_g5_texture_view *texture_view,
uint32_t index) {
D3D12_SHADER_RESOURCE_VIEW_DESC desc = {};
desc.ViewDimension = D3D12_SRV_DIMENSION_TEXTURECUBE;
desc.Shader4ComponentMapping = D3D12_DEFAULT_SHADER_4_COMPONENT_MAPPING;

DXGI_FORMAT format = (DXGI_FORMAT)texture->d3d12.format;
DXGI_FORMAT format = (DXGI_FORMAT)texture_view->texture->d3d12.format;
switch (format) {
case DXGI_FORMAT_D16_UNORM:
desc.Format = DXGI_FORMAT_R16_UNORM;
Expand All @@ -109,26 +113,26 @@ void kope_d3d12_descriptor_set_set_texture_cube_view_srv(kope_g5_device *device,
break;
}

desc.TextureCube.MipLevels = 1;
desc.TextureCube.MostDetailedMip = 0;
desc.TextureCube.MipLevels = texture_view->mip_level_count;
desc.TextureCube.MostDetailedMip = texture_view->base_mip_level;
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;
device->d3d12.device->CreateShaderResourceView(texture->d3d12.resource, &desc, descriptor_handle);
device->d3d12.device->CreateShaderResourceView(texture_view->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 mip_level,
void kope_d3d12_descriptor_set_set_texture_view_uav(kope_g5_device *device, kope_d3d12_descriptor_set *set, const kope_g5_texture_view *texture_view,
uint32_t index) {
D3D12_UNORDERED_ACCESS_VIEW_DESC desc = {};
desc.ViewDimension = D3D12_UAV_DIMENSION_TEXTURE2D;
desc.Format = (DXGI_FORMAT)texture->d3d12.format;
desc.Texture2D.MipSlice = mip_level;
desc.Format = (DXGI_FORMAT)texture_view->texture->d3d12.format;
desc.Texture2D.MipSlice = texture_view->base_mip_level;
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);
device->d3d12.device->CreateUnorderedAccessView(texture_view->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) {
Expand Down Expand Up @@ -157,36 +161,46 @@ void kope_d3d12_descriptor_set_prepare_cbv_buffer(kope_g5_command_list *list, ko
}
}

void kope_d3d12_descriptor_set_prepare_srv_texture(kope_g5_command_list *list, kope_g5_texture *texture, uint32_t highest_mip_level, uint32_t mip_count) {
for (uint32_t mip_level = highest_mip_level; mip_level < highest_mip_level + mip_count; ++mip_level) {
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_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, texture->d3d12.mip_level_count, texture->d3d12.depth_or_array_layers);

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

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_srv_texture(kope_g5_command_list *list, const kope_g5_texture_view *texture_view) {
for (uint32_t array_layer = texture_view->base_array_layer; array_layer < texture_view->base_array_layer + texture_view->array_layer_count; ++array_layer) {
for (uint32_t mip_level = texture_view->base_mip_level; mip_level < texture_view->base_mip_level + texture_view->mip_level_count; ++mip_level) {
if (texture_view->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_view->texture->d3d12.resource;
barrier.Type = D3D12_RESOURCE_BARRIER_TYPE_TRANSITION;
barrier.Flags = D3D12_RESOURCE_BARRIER_FLAG_NONE;
barrier.Transition.StateBefore =
(D3D12_RESOURCE_STATES)
texture_view->texture->d3d12.resource_states[kope_d3d12_texture_resource_state_index(texture_view->texture, mip_level, array_layer)];
barrier.Transition.StateAfter = D3D12_RESOURCE_STATE_PIXEL_SHADER_RESOURCE | D3D12_RESOURCE_STATE_NON_PIXEL_SHADER_RESOURCE;
barrier.Transition.Subresource =
D3D12CalcSubresource(mip_level, 0, 0, texture_view->texture->d3d12.mip_level_count, texture_view->texture->d3d12.depth_or_array_layers);

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

texture_view->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_states[mip_level] != D3D12_RESOURCE_STATE_UNORDERED_ACCESS) {
void kope_d3d12_descriptor_set_prepare_uav_texture(kope_g5_command_list *list, const kope_g5_texture_view *texture_view) {
if (texture_view->texture->d3d12.resource_states[texture_view->base_mip_level] != D3D12_RESOURCE_STATE_UNORDERED_ACCESS) {
D3D12_RESOURCE_BARRIER barrier;
barrier.Transition.pResource = texture->d3d12.resource;
barrier.Transition.pResource = texture_view->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_states[mip_level];
barrier.Transition.StateBefore =
(D3D12_RESOURCE_STATES)
texture_view->texture->d3d12.resource_states[kope_d3d12_texture_resource_state_index(texture_view->texture, texture_view->base_mip_level, 0)];
barrier.Transition.StateAfter = D3D12_RESOURCE_STATE_UNORDERED_ACCESS;
barrier.Transition.Subresource = D3D12CalcSubresource(mip_level, 0, 0, texture->d3d12.mip_level_count, texture->d3d12.depth_or_array_layers);
barrier.Transition.Subresource = D3D12CalcSubresource(texture_view->base_mip_level, 0, 0, texture_view->texture->d3d12.mip_level_count,
texture_view->texture->d3d12.depth_or_array_layers);

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

texture->d3d12.resource_states[mip_level] = D3D12_RESOURCE_STATE_UNORDERED_ACCESS;
texture_view->texture->d3d12.resource_states[texture_view->base_mip_level] = D3D12_RESOURCE_STATE_UNORDERED_ACCESS;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -14,17 +14,19 @@ 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_buffer_view_srv(kope_g5_device *device, kope_d3d12_descriptor_set *set, kope_g5_buffer *buffer, uint32_t index);
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,
void kope_d3d12_descriptor_set_set_texture_view_srv(kope_g5_device *device, kope_d3d12_descriptor_set *set, const kope_g5_texture_view *texture_view,
uint32_t index);
void kope_d3d12_descriptor_set_set_texture_array_view_srv(kope_g5_device *device, kope_d3d12_descriptor_set *set, const kope_g5_texture_view *texture_view,
uint32_t index);
void kope_d3d12_descriptor_set_set_texture_cube_view_srv(kope_g5_device *device, kope_d3d12_descriptor_set *set, const kope_g5_texture_view *texture_view,
uint32_t index);
void kope_d3d12_descriptor_set_set_texture_view_uav(kope_g5_device *device, kope_d3d12_descriptor_set *set, const kope_g5_texture_view *texture_view,
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, uint32_t highest_mip_level, uint32_t mip_count);
void kope_d3d12_descriptor_set_prepare_uav_texture(kope_g5_command_list *list, kope_g5_texture *texture, uint32_t mip_level);
void kope_d3d12_descriptor_set_prepare_srv_texture(kope_g5_command_list *list, const kope_g5_texture_view *texture_view);
void kope_d3d12_descriptor_set_prepare_uav_texture(kope_g5_command_list *list, const kope_g5_texture_view *texture_view);

#ifdef __cplusplus
}
Expand Down
10 changes: 7 additions & 3 deletions Backends/Graphics5/Direct3D12/Sources/kope/direct3d12/device.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -480,9 +480,6 @@ 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)));

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 All @@ -491,6 +488,13 @@ void kope_d3d12_device_create_texture(kope_g5_device *device, const kope_g5_text
texture->d3d12.mip_level_count = parameters->mip_level_count;

texture->d3d12.in_flight_frame_index = 0;

for (uint32_t array_layer = 0; array_layer < parameters->depth_or_array_layers; ++array_layer) {
for (uint32_t mip_level = 0; mip_level < parameters->mip_level_count; ++mip_level) {
texture->d3d12.resource_states[kope_d3d12_texture_resource_state_index(texture, mip_level, array_layer)] =
D3D12_RESOURCE_STATE_PIXEL_SHADER_RESOURCE;
}
}
}

kope_g5_texture *kope_d3d12_device_get_framebuffer(kope_g5_device *device) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,3 +3,7 @@
#include "d3d12unit.h"

#include <kope/graphics5/texture.h>

uint32_t kope_d3d12_texture_resource_state_index(kope_g5_texture *texture, uint32_t mip_level, uint32_t array_layer) {
return mip_level + (array_layer * texture->d3d12.mip_level_count);
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@
extern "C" {
#endif

uint32_t kope_d3d12_texture_resource_state_index(kope_g5_texture *texture, uint32_t mip_level, uint32_t array_layer);

#ifdef __cplusplus
}
#endif
Expand Down
8 changes: 1 addition & 7 deletions Sources/kope/graphics5/commandlist.h
Original file line number Diff line number Diff line change
Expand Up @@ -81,14 +81,8 @@ typedef struct kope_g5_render_pass_parameters {

KOPE_FUNC void kope_g5_command_list_begin_render_pass(kope_g5_command_list *list, const kope_g5_render_pass_parameters *parameters);

typedef enum kope_g5_image_copy_aspect {
KOPE_G5_IMAGE_COPY_ASPECT_ALL,
KOPE_G5_IMAGE_COPY_ASPECT_DEPTH_ONLY,
KOPE_G5_IMAGE_COPY_ASPECT_STENCIL_ONLY
} kope_g5_image_copy_aspect;

typedef struct kope_g5_image_copy_texture {
kope_g5_image_copy_aspect aspect;
kope_g5_texture_aspect aspect;
uint32_t mip_level;
uint32_t origin_x;
uint32_t origin_y;
Expand Down
Loading

0 comments on commit 7a6d3f1

Please sign in to comment.