diff --git a/Backends/Graphics5/Direct3D12/Sources/kope/direct3d12/d3d12unit.cpp b/Backends/Graphics5/Direct3D12/Sources/kope/direct3d12/d3d12unit.cpp index 42633b9e7..39d62796e 100644 --- a/Backends/Graphics5/Direct3D12/Sources/kope/direct3d12/d3d12unit.cpp +++ b/Backends/Graphics5/Direct3D12/Sources/kope/direct3d12/d3d12unit.cpp @@ -2,6 +2,7 @@ #include "buffer.cpp" #include "commandlist.cpp" +#include "descriptorset.cpp" #include "device.cpp" #include "pipeline.cpp" #include "texture.cpp" diff --git a/Backends/Graphics5/Direct3D12/Sources/kope/direct3d12/descriptorset.cpp b/Backends/Graphics5/Direct3D12/Sources/kope/direct3d12/descriptorset.cpp new file mode 100644 index 000000000..a7610cb5c --- /dev/null +++ b/Backends/Graphics5/Direct3D12/Sources/kope/direct3d12/descriptorset.cpp @@ -0,0 +1,2 @@ +#include "descriptorset_functions.h" +#include "descriptorset_structs.h" diff --git a/Backends/Graphics5/Direct3D12/Sources/kope/direct3d12/descriptorset_functions.h b/Backends/Graphics5/Direct3D12/Sources/kope/direct3d12/descriptorset_functions.h new file mode 100644 index 000000000..250d44bf0 --- /dev/null +++ b/Backends/Graphics5/Direct3D12/Sources/kope/direct3d12/descriptorset_functions.h @@ -0,0 +1,12 @@ +#ifndef KOPE_D3D12_DESCRIPTORSET_FUNCTIONS_HEADER +#define KOPE_D3D12_DESCRIPTORSET_FUNCTIONS_HEADER + +#ifdef __cplusplus +extern "C" { +#endif + +#ifdef __cplusplus +} +#endif + +#endif diff --git a/Backends/Graphics5/Direct3D12/Sources/kope/direct3d12/descriptorset_structs.h b/Backends/Graphics5/Direct3D12/Sources/kope/direct3d12/descriptorset_structs.h new file mode 100644 index 000000000..937273fa4 --- /dev/null +++ b/Backends/Graphics5/Direct3D12/Sources/kope/direct3d12/descriptorset_structs.h @@ -0,0 +1,21 @@ +#ifndef KOPE_D3D12_DESCRIPTORSET_STRUCTS_HEADER +#define KOPE_D3D12_DESCRIPTORSET_STRUCTS_HEADER + +#include "d3d12mini.h" + +#include + +#ifdef __cplusplus +extern "C" { +#endif + +typedef struct kope_d3d12_descriptor_set { + oa_allocation_t allocation; + size_t descriptor_count; +} kope_d3d12_descriptor_set; + +#ifdef __cplusplus +} +#endif + +#endif diff --git a/Backends/Graphics5/Direct3D12/Sources/kope/direct3d12/device.cpp b/Backends/Graphics5/Direct3D12/Sources/kope/direct3d12/device.cpp index 3c24f4c19..8b1ea9ecd 100644 --- a/Backends/Graphics5/Direct3D12/Sources/kope/direct3d12/device.cpp +++ b/Backends/Graphics5/Direct3D12/Sources/kope/direct3d12/device.cpp @@ -119,6 +119,16 @@ void kope_d3d12_device_create(kope_g5_device *device, const kope_g5_device_wishl device->d3d12.execution_context_index = 0; device->d3d12.execution_contexts[device->d3d12.execution_context_index].blocking_frame_index = 1; + { + D3D12_DESCRIPTOR_HEAP_DESC desc = {}; + desc.NumDescriptors = 1024 * 10; + desc.Type = D3D12_DESCRIPTOR_HEAP_TYPE_CBV_SRV_UAV; + desc.Flags = D3D12_DESCRIPTOR_HEAP_FLAG_NONE; + kinc_microsoft_affirm(device->d3d12.device->CreateDescriptorHeap(&desc, IID_GRAPHICS_PPV_ARGS(&device->d3d12.descriptor_sets_heap))); + + oa_create(&device->d3d12.descriptor_sets_allocator, 1024 * 10, 4096); + } + { kope_d3d12_device_create_command_list(device, &device->d3d12.management_list); @@ -500,3 +510,8 @@ void kope_d3d12_device_execute_command_list(kope_g5_device *device, kope_g5_comm list->d3d12.presenting = false; } } + +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_sets_allocator, descriptor_count, &set->allocation); + set->descriptor_count = descriptor_count; +} diff --git a/Backends/Graphics5/Direct3D12/Sources/kope/direct3d12/device_functions.h b/Backends/Graphics5/Direct3D12/Sources/kope/direct3d12/device_functions.h index be663cf68..e4150ab25 100644 --- a/Backends/Graphics5/Direct3D12/Sources/kope/direct3d12/device_functions.h +++ b/Backends/Graphics5/Direct3D12/Sources/kope/direct3d12/device_functions.h @@ -3,6 +3,8 @@ #include +#include "descriptorset_structs.h" + #ifdef __cplusplus extern "C" { #endif @@ -19,6 +21,8 @@ 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); + kope_g5_texture *kope_d3d12_device_get_framebuffer(kope_g5_device *device); void kope_d3d12_device_execute_command_list(kope_g5_device *device, kope_g5_command_list *list); diff --git a/Backends/Graphics5/Direct3D12/Sources/kope/direct3d12/device_structs.h b/Backends/Graphics5/Direct3D12/Sources/kope/direct3d12/device_structs.h index 156ce5a38..8f480328d 100644 --- a/Backends/Graphics5/Direct3D12/Sources/kope/direct3d12/device_structs.h +++ b/Backends/Graphics5/Direct3D12/Sources/kope/direct3d12/device_structs.h @@ -4,6 +4,7 @@ #include "d3d12mini.h" #include +#include #ifdef __cplusplus extern "C" { @@ -47,6 +48,9 @@ typedef struct kope_d3d12_device { HANDLE frame_event; uint64_t current_frame_index; + struct ID3D12DescriptorHeap *descriptor_sets_heap; + oa_allocator_t descriptor_sets_allocator; + kope_g5_command_list management_list; } kope_d3d12_device; diff --git a/Sources/kope/util/offalloc/offalloc.h b/Sources/kope/util/offalloc/offalloc.h index b3cc5417b..1c2e354b5 100644 --- a/Sources/kope/util/offalloc/offalloc.h +++ b/Sources/kope/util/offalloc/offalloc.h @@ -1,75 +1,83 @@ -// (C) Sebastian Aaltonen 2023 -// MIT License (see file: LICENSE) -// C99 conversion by Aarni Gratseff -// https://github.com/aarni57/offalloc - -#ifndef OFFALLOC_H -#define OFFALLOC_H - -#include "stdint.h" - -#define OA_NUM_TOP_BINS 32 -#define OA_BINS_PER_LEAF 8 -#define OA_NUM_LEAF_BINS OA_NUM_TOP_BINS * OA_BINS_PER_LEAF - -#define OA_NO_SPACE 0xffffffff - -typedef uint16_t oa_index_t; -#define OA_INVALID_INDEX (oa_index_t)0xffff -#define OA_UNUSED (oa_index_t)0xffff - -typedef struct oa_allocation_t { - uint32_t offset; - oa_index_t index; // internal: node index -} oa_allocation_t; - -typedef struct ao_storage_report_t { - uint32_t total_free_space; - uint32_t largest_free_region; -} oa_storage_report_t; - -typedef struct ao_region_t { - uint32_t size; - uint32_t count; -} oa_region_t; - -typedef struct ao_storage_report_full_t { - oa_region_t free_regions[OA_NUM_LEAF_BINS]; -} oa_storage_report_full_t; - -typedef struct ao_node_t { - uint32_t data_offset; - uint32_t data_size; // 'used' flag stored as high bit - oa_index_t bin_list_prev; - oa_index_t bin_list_next; - oa_index_t neighbor_prev; - oa_index_t neighbor_next; -} oa_node_t; - -typedef struct ao_allocator_t { - uint32_t size; - uint32_t max_allocs; - uint32_t free_storage; - - uint32_t used_bins_top; - uint8_t used_bins[OA_NUM_TOP_BINS]; - oa_index_t bin_indices[OA_NUM_LEAF_BINS]; - - oa_node_t *nodes; - oa_index_t *free_nodes; - uint32_t free_offset; -} oa_allocator_t; - -// - -int oa_create(oa_allocator_t *self, uint32_t size, uint32_t max_allocs); -void oa_destroy(oa_allocator_t *self); - -int oa_allocate(oa_allocator_t *self, uint32_t size, oa_allocation_t *allocation); -void oa_free(oa_allocator_t *self, oa_allocation_t *allocation); - -uint32_t oa_allocation_size(oa_allocator_t *self, const oa_allocation_t *allocation); -void oa_storage_report(const oa_allocator_t *self, oa_storage_report_t *report); -void oa_storage_report_full(const oa_allocator_t *self, oa_storage_report_full_t *report); - -#endif +// (C) Sebastian Aaltonen 2023 +// MIT License (see file: LICENSE) +// C99 conversion by Aarni Gratseff +// https://github.com/aarni57/offalloc + +#ifndef OFFALLOC_H +#define OFFALLOC_H + +#include "stdint.h" + +#ifdef __cplusplus +extern "C" { +#endif + +#define OA_NUM_TOP_BINS 32 +#define OA_BINS_PER_LEAF 8 +#define OA_NUM_LEAF_BINS OA_NUM_TOP_BINS *OA_BINS_PER_LEAF + +#define OA_NO_SPACE 0xffffffff + +typedef uint16_t oa_index_t; +#define OA_INVALID_INDEX (oa_index_t)0xffff +#define OA_UNUSED (oa_index_t)0xffff + +typedef struct oa_allocation_t { + uint32_t offset; + oa_index_t index; // internal: node index +} oa_allocation_t; + +typedef struct ao_storage_report_t { + uint32_t total_free_space; + uint32_t largest_free_region; +} oa_storage_report_t; + +typedef struct ao_region_t { + uint32_t size; + uint32_t count; +} oa_region_t; + +typedef struct ao_storage_report_full_t { + oa_region_t free_regions[OA_NUM_LEAF_BINS]; +} oa_storage_report_full_t; + +typedef struct ao_node_t { + uint32_t data_offset; + uint32_t data_size; // 'used' flag stored as high bit + oa_index_t bin_list_prev; + oa_index_t bin_list_next; + oa_index_t neighbor_prev; + oa_index_t neighbor_next; +} oa_node_t; + +typedef struct ao_allocator_t { + uint32_t size; + uint32_t max_allocs; + uint32_t free_storage; + + uint32_t used_bins_top; + uint8_t used_bins[OA_NUM_TOP_BINS]; + oa_index_t bin_indices[OA_NUM_LEAF_BINS]; + + oa_node_t *nodes; + oa_index_t *free_nodes; + uint32_t free_offset; +} oa_allocator_t; + +// + +int oa_create(oa_allocator_t *self, uint32_t size, uint32_t max_allocs); +void oa_destroy(oa_allocator_t *self); + +int oa_allocate(oa_allocator_t *self, uint32_t size, oa_allocation_t *allocation); +void oa_free(oa_allocator_t *self, oa_allocation_t *allocation); + +uint32_t oa_allocation_size(oa_allocator_t *self, const oa_allocation_t *allocation); +void oa_storage_report(const oa_allocator_t *self, oa_storage_report_t *report); +void oa_storage_report_full(const oa_allocator_t *self, oa_storage_report_full_t *report); + +#ifdef __cplusplus +} +#endif + +#endif diff --git a/Sources/kope/util/utilunit.c b/Sources/kope/util/utilunit.c index 25f1b1055..70f36fc4d 100644 --- a/Sources/kope/util/utilunit.c +++ b/Sources/kope/util/utilunit.c @@ -1 +1,2 @@ -#include "indexallocator.c" +#include "indexallocator.c" +#include "offalloc/offalloc.c"