Skip to content

Commit

Permalink
Do some D3D12 descriptor set setup work
Browse files Browse the repository at this point in the history
  • Loading branch information
RobDangerous committed Sep 4, 2024
1 parent 28838ae commit d698c5b
Show file tree
Hide file tree
Showing 9 changed files with 144 additions and 76 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

#include "buffer.cpp"
#include "commandlist.cpp"
#include "descriptorset.cpp"
#include "device.cpp"
#include "pipeline.cpp"
#include "texture.cpp"
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
#include "descriptorset_functions.h"
#include "descriptorset_structs.h"
Original file line number Diff line number Diff line change
@@ -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
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
#ifndef KOPE_D3D12_DESCRIPTORSET_STRUCTS_HEADER
#define KOPE_D3D12_DESCRIPTORSET_STRUCTS_HEADER

#include "d3d12mini.h"

#include <kope/util/offalloc/offalloc.h>

#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
15 changes: 15 additions & 0 deletions Backends/Graphics5/Direct3D12/Sources/kope/direct3d12/device.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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);

Expand Down Expand Up @@ -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;
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@

#include <kope/graphics5/device.h>

#include "descriptorset_structs.h"

#ifdef __cplusplus
extern "C" {
#endif
Expand All @@ -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);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
#include "d3d12mini.h"

#include <kope/util/indexallocator.h>
#include <kope/util/offalloc/offalloc.h>

#ifdef __cplusplus
extern "C" {
Expand Down Expand Up @@ -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;

Expand Down
158 changes: 83 additions & 75 deletions Sources/kope/util/offalloc/offalloc.h
Original file line number Diff line number Diff line change
@@ -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
3 changes: 2 additions & 1 deletion Sources/kope/util/utilunit.c
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
#include "indexallocator.c"
#include "indexallocator.c"
#include "offalloc/offalloc.c"

0 comments on commit d698c5b

Please sign in to comment.