From bcb31dd4eed167f7fce7a79ff9c0243af14d594a Mon Sep 17 00:00:00 2001 From: Robert Konrad Date: Tue, 10 Sep 2024 10:22:45 +0200 Subject: [PATCH] Support more descriptor types --- .../Sources/kope/direct3d12/commandlist.cpp | 15 ++++++-- .../Sources/kope/direct3d12/d3d12unit.cpp | 1 + .../Sources/kope/direct3d12/descriptorset.cpp | 35 +++++++++++++++++-- .../kope/direct3d12/descriptorset_functions.h | 4 +++ .../kope/direct3d12/descriptorset_structs.h | 5 ++- .../Sources/kope/direct3d12/device.cpp | 11 ++++-- .../kope/direct3d12/device_functions.h | 2 +- .../Sources/kope/direct3d12/sampler.cpp | 5 +++ .../kope/direct3d12/sampler_functions.h | 14 ++++++++ .../Sources/kope/direct3d12/sampler_structs.h | 18 ++++++++++ Sources/kope/graphics5/g5unit.c | 1 + Sources/kope/graphics5/sampler.c | 9 +++++ Sources/kope/graphics5/sampler.h | 28 +++++++++++++++ 13 files changed, 139 insertions(+), 9 deletions(-) create mode 100644 Backends/Graphics5/Direct3D12/Sources/kope/direct3d12/sampler.cpp create mode 100644 Backends/Graphics5/Direct3D12/Sources/kope/direct3d12/sampler_functions.h create mode 100644 Backends/Graphics5/Direct3D12/Sources/kope/direct3d12/sampler_structs.h create mode 100644 Sources/kope/graphics5/sampler.c create mode 100644 Sources/kope/graphics5/sampler.h diff --git a/Backends/Graphics5/Direct3D12/Sources/kope/direct3d12/commandlist.cpp b/Backends/Graphics5/Direct3D12/Sources/kope/direct3d12/commandlist.cpp index 67bfb42c7..994ce1526 100644 --- a/Backends/Graphics5/Direct3D12/Sources/kope/direct3d12/commandlist.cpp +++ b/Backends/Graphics5/Direct3D12/Sources/kope/direct3d12/commandlist.cpp @@ -89,7 +89,16 @@ 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) { - D3D12_GPU_DESCRIPTOR_HANDLE gpu_descriptor = list->d3d12.device->descriptor_heap->GetGPUDescriptorHandleForHeapStart(); - gpu_descriptor.ptr += set->allocation.offset * list->d3d12.device->cbv_srv_uav_increment; - list->d3d12.list->SetGraphicsRootDescriptorTable(table_index, gpu_descriptor); + if (set->descriptor_count > 0) { + D3D12_GPU_DESCRIPTOR_HANDLE gpu_descriptor = list->d3d12.device->descriptor_heap->GetGPUDescriptorHandleForHeapStart(); + gpu_descriptor.ptr += set->descriptor_allocation.offset * list->d3d12.device->cbv_srv_uav_increment; + list->d3d12.list->SetGraphicsRootDescriptorTable(table_index, gpu_descriptor); + table_index += 1; + } + + if (set->sampler_count > 0) { + D3D12_GPU_DESCRIPTOR_HANDLE gpu_descriptor = list->d3d12.device->sampler_heap->GetGPUDescriptorHandleForHeapStart(); + gpu_descriptor.ptr += set->sampler_allocation.offset * list->d3d12.device->sampler_increment; + list->d3d12.list->SetGraphicsRootDescriptorTable(table_index, gpu_descriptor); + } } diff --git a/Backends/Graphics5/Direct3D12/Sources/kope/direct3d12/d3d12unit.cpp b/Backends/Graphics5/Direct3D12/Sources/kope/direct3d12/d3d12unit.cpp index 39d62796e..375a4f2b7 100644 --- a/Backends/Graphics5/Direct3D12/Sources/kope/direct3d12/d3d12unit.cpp +++ b/Backends/Graphics5/Direct3D12/Sources/kope/direct3d12/d3d12unit.cpp @@ -5,4 +5,5 @@ #include "descriptorset.cpp" #include "device.cpp" #include "pipeline.cpp" +#include "sampler.cpp" #include "texture.cpp" diff --git a/Backends/Graphics5/Direct3D12/Sources/kope/direct3d12/descriptorset.cpp b/Backends/Graphics5/Direct3D12/Sources/kope/direct3d12/descriptorset.cpp index 6df7d1e46..5085538ff 100644 --- a/Backends/Graphics5/Direct3D12/Sources/kope/direct3d12/descriptorset.cpp +++ b/Backends/Graphics5/Direct3D12/Sources/kope/direct3d12/descriptorset.cpp @@ -2,11 +2,42 @@ #include "descriptorset_structs.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) { - D3D12_CONSTANT_BUFFER_VIEW_DESC desc; + D3D12_CONSTANT_BUFFER_VIEW_DESC desc = {}; desc.BufferLocation = buffer->d3d12.resource->GetGPUVirtualAddress(); desc.SizeInBytes = (UINT)buffer->d3d12.size; D3D12_CPU_DESCRIPTOR_HANDLE descriptor_handle = device->d3d12.descriptor_heap->GetCPUDescriptorHandleForHeapStart(); - descriptor_handle.ptr += (set->allocation.offset + index) * device->d3d12.cbv_srv_uav_increment; + descriptor_handle.ptr += (set->descriptor_allocation.offset + index) * device->d3d12.cbv_srv_uav_increment; device->d3d12.device->CreateConstantBufferView(&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 index) { + D3D12_SHADER_RESOURCE_VIEW_DESC desc = {}; + desc.ViewDimension = D3D12_SRV_DIMENSION_TEXTURE2D; + desc.Shader4ComponentMapping = D3D12_DEFAULT_SHADER_4_COMPONENT_MAPPING; + desc.Format = DXGI_FORMAT_R8G8B8A8_UNORM; + desc.Texture2D.MipLevels = 1; + desc.Texture2D.MostDetailedMip = 0; + 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); +} + +void kope_d3d12_descriptor_set_set_sampler(kope_g5_device *device, kope_d3d12_descriptor_set *set, kope_g5_sampler *sampler, uint32_t index) { + D3D12_SAMPLER_DESC desc = {}; + desc.Filter = D3D12_FILTER_MIN_MAG_MIP_POINT; + desc.AddressU = D3D12_TEXTURE_ADDRESS_MODE_WRAP; + desc.AddressV = D3D12_TEXTURE_ADDRESS_MODE_WRAP; + desc.AddressW = D3D12_TEXTURE_ADDRESS_MODE_WRAP; + desc.MinLOD = 0.0f; + desc.MaxLOD = 1.0f; + desc.MipLODBias = 0.0f; + desc.MaxAnisotropy = 1; + desc.ComparisonFunc = D3D12_COMPARISON_FUNC_LESS_EQUAL; + + D3D12_CPU_DESCRIPTOR_HANDLE descriptor_handle = device->d3d12.sampler_heap->GetCPUDescriptorHandleForHeapStart(); + descriptor_handle.ptr += (set->sampler_allocation.offset + index) * device->d3d12.sampler_increment; + device->d3d12.device->CreateSampler(&desc, descriptor_handle); +} diff --git a/Backends/Graphics5/Direct3D12/Sources/kope/direct3d12/descriptorset_functions.h b/Backends/Graphics5/Direct3D12/Sources/kope/direct3d12/descriptorset_functions.h index afc8528ae..5f023ecf4 100644 --- a/Backends/Graphics5/Direct3D12/Sources/kope/direct3d12/descriptorset_functions.h +++ b/Backends/Graphics5/Direct3D12/Sources/kope/direct3d12/descriptorset_functions.h @@ -5,11 +5,15 @@ #include "descriptorset_structs.h" #include "device_structs.h" +#include + #ifdef __cplusplus extern "C" { #endif 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_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); #ifdef __cplusplus } diff --git a/Backends/Graphics5/Direct3D12/Sources/kope/direct3d12/descriptorset_structs.h b/Backends/Graphics5/Direct3D12/Sources/kope/direct3d12/descriptorset_structs.h index f8fd5a579..6df0a08a1 100644 --- a/Backends/Graphics5/Direct3D12/Sources/kope/direct3d12/descriptorset_structs.h +++ b/Backends/Graphics5/Direct3D12/Sources/kope/direct3d12/descriptorset_structs.h @@ -12,8 +12,11 @@ extern "C" { #endif typedef struct kope_d3d12_descriptor_set { - oa_allocation_t allocation; + oa_allocation_t descriptor_allocation; size_t descriptor_count; + + oa_allocation_t sampler_allocation; + size_t sampler_count; } kope_d3d12_descriptor_set; #ifdef __cplusplus diff --git a/Backends/Graphics5/Direct3D12/Sources/kope/direct3d12/device.cpp b/Backends/Graphics5/Direct3D12/Sources/kope/direct3d12/device.cpp index 163bcc75d..9c49a6f4e 100644 --- a/Backends/Graphics5/Direct3D12/Sources/kope/direct3d12/device.cpp +++ b/Backends/Graphics5/Direct3D12/Sources/kope/direct3d12/device.cpp @@ -491,7 +491,14 @@ void kope_d3d12_device_execute_command_list(kope_g5_device *device, kope_g5_comm } } -void kope_d3d12_device_create_descriptor_set(kope_g5_device *device, uint32_t descriptor_count, kope_d3d12_descriptor_set *set) { - oa_allocate(&device->d3d12.descriptor_heap_allocator, descriptor_count, &set->allocation); +void kope_d3d12_device_create_descriptor_set(kope_g5_device *device, uint32_t descriptor_count, uint32_t sampler_count, kope_d3d12_descriptor_set *set) { + if (descriptor_count > 0) { + oa_allocate(&device->d3d12.descriptor_heap_allocator, descriptor_count, &set->descriptor_allocation); + } set->descriptor_count = descriptor_count; + + if (sampler_count > 0) { + oa_allocate(&device->d3d12.sampler_heap_allocator, sampler_count, &set->sampler_allocation); + } + set->sampler_count = sampler_count; } diff --git a/Backends/Graphics5/Direct3D12/Sources/kope/direct3d12/device_functions.h b/Backends/Graphics5/Direct3D12/Sources/kope/direct3d12/device_functions.h index e4150ab25..e3c0e5836 100644 --- a/Backends/Graphics5/Direct3D12/Sources/kope/direct3d12/device_functions.h +++ b/Backends/Graphics5/Direct3D12/Sources/kope/direct3d12/device_functions.h @@ -21,7 +21,7 @@ void kope_d3d12_device_create_command_list(kope_g5_device *device, kope_g5_comma void kope_d3d12_device_create_texture(kope_g5_device *device, const kope_g5_texture_parameters *parameters, kope_g5_texture *texture); -void kope_d3d12_device_create_descriptor_set(kope_g5_device *device, uint32_t descriptor_count, kope_d3d12_descriptor_set *set); +void kope_d3d12_device_create_descriptor_set(kope_g5_device *device, uint32_t descriptor_count, uint32_t sampler_count, kope_d3d12_descriptor_set *set); kope_g5_texture *kope_d3d12_device_get_framebuffer(kope_g5_device *device); diff --git a/Backends/Graphics5/Direct3D12/Sources/kope/direct3d12/sampler.cpp b/Backends/Graphics5/Direct3D12/Sources/kope/direct3d12/sampler.cpp new file mode 100644 index 000000000..56bba09cd --- /dev/null +++ b/Backends/Graphics5/Direct3D12/Sources/kope/direct3d12/sampler.cpp @@ -0,0 +1,5 @@ +#include "sampler_functions.h" + +#include "d3d12unit.h" + +#include diff --git a/Backends/Graphics5/Direct3D12/Sources/kope/direct3d12/sampler_functions.h b/Backends/Graphics5/Direct3D12/Sources/kope/direct3d12/sampler_functions.h new file mode 100644 index 000000000..73e694cda --- /dev/null +++ b/Backends/Graphics5/Direct3D12/Sources/kope/direct3d12/sampler_functions.h @@ -0,0 +1,14 @@ +#ifndef KOPE_D3D12_SAMPLER_FUNCTIONS_HEADER +#define KOPE_D3D12_SAMPLER_FUNCTIONS_HEADER + +#include + +#ifdef __cplusplus +extern "C" { +#endif + +#ifdef __cplusplus +} +#endif + +#endif diff --git a/Backends/Graphics5/Direct3D12/Sources/kope/direct3d12/sampler_structs.h b/Backends/Graphics5/Direct3D12/Sources/kope/direct3d12/sampler_structs.h new file mode 100644 index 000000000..ac4f6a5b9 --- /dev/null +++ b/Backends/Graphics5/Direct3D12/Sources/kope/direct3d12/sampler_structs.h @@ -0,0 +1,18 @@ +#ifndef KOPE_D3D12_SAMPLER_STRUCTS_HEADER +#define KOPE_D3D12_SAMPLER_STRUCTS_HEADER + +#include "d3d12mini.h" + +#ifdef __cplusplus +extern "C" { +#endif + +typedef struct kope_d3d12_sampler { + int nothing; +} kope_d3d12_sampler; + +#ifdef __cplusplus +} +#endif + +#endif diff --git a/Sources/kope/graphics5/g5unit.c b/Sources/kope/graphics5/g5unit.c index 497b80a37..742eacb83 100644 --- a/Sources/kope/graphics5/g5unit.c +++ b/Sources/kope/graphics5/g5unit.c @@ -4,4 +4,5 @@ #include "commandlist.c" #include "device.c" #include "graphics.c" +#include "sampler.c" #include "texture.c" diff --git a/Sources/kope/graphics5/sampler.c b/Sources/kope/graphics5/sampler.c new file mode 100644 index 000000000..3c472b616 --- /dev/null +++ b/Sources/kope/graphics5/sampler.c @@ -0,0 +1,9 @@ +#include "sampler.h" + +#ifdef KOPE_DIRECT3D12 +#include +#endif + +#ifdef KOPE_VULKAN +#include +#endif diff --git a/Sources/kope/graphics5/sampler.h b/Sources/kope/graphics5/sampler.h new file mode 100644 index 000000000..8343d2dd3 --- /dev/null +++ b/Sources/kope/graphics5/sampler.h @@ -0,0 +1,28 @@ +#ifndef KOPE_G5_SAMPLER_HEADER +#define KOPE_G5_SAMPLER_HEADER + +#include + +#include "api.h" + +#ifdef KOPE_DIRECT3D12 +#include +#endif + +#ifdef KOPE_VULKAN +#include +#endif + +#ifdef __cplusplus +extern "C" { +#endif + +typedef struct kope_g5_sampler { + KOPE_G5_IMPL(texture); +} kope_g5_sampler; + +#ifdef __cplusplus +} +#endif + +#endif