Skip to content

Commit

Permalink
Set some descriptors
Browse files Browse the repository at this point in the history
  • Loading branch information
RobDangerous committed Sep 9, 2024
1 parent d698c5b commit df288d8
Show file tree
Hide file tree
Showing 8 changed files with 56 additions and 8 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ extern "C" {

typedef struct kope_d3d12_buffer {
struct ID3D12Resource *resource;
size_t size;
} kope_d3d12_buffer;

#ifdef __cplusplus
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -87,3 +87,22 @@ void kope_d3d12_command_list_draw_indexed(kope_g5_command_list *list, uint32_t i
list->d3d12.list->IASetPrimitiveTopology(D3D_PRIMITIVE_TOPOLOGY_TRIANGLELIST);
list->d3d12.list->DrawIndexedInstanced(index_count, instance_count, first_index, base_vertex, first_instance);
}

void kope_d3d12_command_list_set_descriptor_table(kope_g5_command_list *list, uint32_t table_index, kope_d3d12_descriptor_set *set) {
uint32_t execution_index = list->d3d12.device->execution_context_index;

if (!set->copied_to_execution_context[execution_index]) {
D3D12_CPU_DESCRIPTOR_HANDLE src_descriptor = list->d3d12.device->staging_descriptor_heap->GetCPUDescriptorHandleForHeapStart();
src_descriptor.ptr += set->allocation.offset * list->d3d12.device->cbv_srv_uav_increment;

D3D12_CPU_DESCRIPTOR_HANDLE dst_descriptor =
list->d3d12.device->execution_contexts[execution_index].descriptor_heap->GetCPUDescriptorHandleForHeapStart();
dst_descriptor.ptr += set->allocation.offset * list->d3d12.device->cbv_srv_uav_increment;

list->d3d12.device->device->CopyDescriptorsSimple((UINT)set->descriptor_count, dst_descriptor, src_descriptor, D3D12_DESCRIPTOR_HEAP_TYPE_CBV_SRV_UAV);
}

D3D12_GPU_DESCRIPTOR_HANDLE gpu_descriptor = list->d3d12.device->execution_contexts[execution_index].descriptor_heap->GetGPUDescriptorHandleForHeapStart();
gpu_descriptor.ptr += set->allocation.offset * list->d3d12.device->cbv_srv_uav_increment;
list->d3d12.list->SetGraphicsRootDescriptorTable(table_index, gpu_descriptor);
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@

#include <kope/graphics5/commandlist.h>

#include "descriptorset_structs.h"
#include "pipeline_structs.h"

#ifdef __cplusplus
Expand All @@ -26,6 +27,8 @@ void kope_d3d12_command_list_set_pipeline(kope_g5_command_list *list, kope_d3d12
void kope_d3d12_command_list_draw_indexed(kope_g5_command_list *list, uint32_t index_count, uint32_t instance_count, uint32_t first_index, int32_t base_vertex,
uint32_t first_instance);

void kope_d3d12_command_list_set_descriptor_table(kope_g5_command_list *list, uint32_t table_index, kope_d3d12_descriptor_set *set);

#ifdef __cplusplus
}
#endif
Expand Down
Original file line number Diff line number Diff line change
@@ -1,2 +1,12 @@
#include "descriptorset_functions.h"
#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;
desc.BufferLocation = buffer->d3d12.resource->GetGPUVirtualAddress();
desc.SizeInBytes = (UINT)buffer->d3d12.size;

D3D12_CPU_DESCRIPTOR_HANDLE descriptor_handle = device->d3d12.staging_descriptor_heap->GetCPUDescriptorHandleForHeapStart();
descriptor_handle.ptr += (set->allocation.offset + index) * device->d3d12.cbv_srv_uav_increment;
device->d3d12.device->CreateConstantBufferView(&desc, descriptor_handle);
}
Original file line number Diff line number Diff line change
@@ -1,10 +1,16 @@
#ifndef KOPE_D3D12_DESCRIPTORSET_FUNCTIONS_HEADER
#define KOPE_D3D12_DESCRIPTORSET_FUNCTIONS_HEADER

#include "buffer_structs.h"
#include "descriptorset_structs.h"
#include "device_structs.h"

#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);

#ifdef __cplusplus
}
#endif
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,16 @@

#include "d3d12mini.h"

#include "device_structs.h"

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

#ifdef __cplusplus
extern "C" {
#endif

typedef struct kope_d3d12_descriptor_set {
bool copied_to_execution_context[KOPE_D3D12_NUM_EXECUTION_CONTEXTS];
oa_allocation_t allocation;
size_t descriptor_count;
} kope_d3d12_descriptor_set;
Expand Down
15 changes: 10 additions & 5 deletions Backends/Graphics5/Direct3D12/Sources/kope/direct3d12/device.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,9 @@ void kope_d3d12_device_create(kope_g5_device *device, const kope_g5_device_wishl
kinc_microsoft_affirm(dxgi_factory->CreateSwapChain((IUnknown *)device->d3d12.queue, &desc, &device->d3d12.swap_chain));
}

device->d3d12.cbv_srv_uav_increment = device->d3d12.device->GetDescriptorHandleIncrementSize(D3D12_DESCRIPTOR_HEAP_TYPE_CBV_SRV_UAV);
device->d3d12.sampler_increment = device->d3d12.device->GetDescriptorHandleIncrementSize(D3D12_DESCRIPTOR_HEAP_TYPE_SAMPLER);

{
D3D12_DESCRIPTOR_HEAP_DESC desc = {};
desc.NumDescriptors = KOPE_INDEX_ALLOCATOR_SIZE;
Expand Down Expand Up @@ -124,7 +127,7 @@ void kope_d3d12_device_create(kope_g5_device *device, const kope_g5_device_wishl
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)));
kinc_microsoft_affirm(device->d3d12.device->CreateDescriptorHeap(&desc, IID_GRAPHICS_PPV_ARGS(&device->d3d12.staging_descriptor_heap)));

oa_create(&device->d3d12.descriptor_sets_allocator, 1024 * 10, 4096);
}
Expand Down Expand Up @@ -175,7 +178,7 @@ void kope_d3d12_device_create_buffer(kope_g5_device *device, const kope_g5_buffe
D3D12_RESOURCE_DESC resourceDesc;
resourceDesc.Dimension = D3D12_RESOURCE_DIMENSION_BUFFER;
resourceDesc.Alignment = 0;
resourceDesc.Width = parameters->size;
resourceDesc.Width = 256; // parameters->size;
resourceDesc.Height = 1;
resourceDesc.DepthOrArraySize = 1;
resourceDesc.MipLevels = 1;
Expand All @@ -185,6 +188,8 @@ void kope_d3d12_device_create_buffer(kope_g5_device *device, const kope_g5_buffe
resourceDesc.Layout = D3D12_TEXTURE_LAYOUT_ROW_MAJOR;
resourceDesc.Flags = D3D12_RESOURCE_FLAG_NONE;

buffer->d3d12.size = 256; // parameters->size;

kinc_microsoft_affirm(device->d3d12.device->CreateCommittedResource(&heapProperties, D3D12_HEAP_FLAG_NONE, &resourceDesc, D3D12_RESOURCE_STATE_GENERIC_READ,
NULL, IID_GRAPHICS_PPV_ARGS(&buffer->d3d12.resource)));
}
Expand Down Expand Up @@ -468,9 +473,6 @@ void kope_d3d12_device_execute_command_list(kope_g5_device *device, kope_g5_comm
if (execution_context->blocking_frame_index <= completed_frame) {
device->d3d12.execution_context_index = execution_index;

execution_context->descriptor_heap_offset = 0;
execution_context->sampler_heap_offset = 0;

ID3D12DescriptorHeap *heaps[] = {execution_context->descriptor_heap, execution_context->sampler_heap};
list->d3d12.list->SetDescriptorHeaps(2, heaps);

Expand Down Expand Up @@ -512,6 +514,9 @@ 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) {
for (uint32_t index = 0; index < KOPE_D3D12_NUM_EXECUTION_CONTEXTS; ++index) {
set->copied_to_execution_context[index] = false;
}
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 @@ -18,9 +18,7 @@ struct ID3D12DescriptorHeap;
typedef struct kope_d3d12_execution_context {
uint64_t blocking_frame_index;
struct ID3D12DescriptorHeap *descriptor_heap;
size_t descriptor_heap_offset;
struct ID3D12DescriptorHeap *sampler_heap;
size_t sampler_heap_offset;
} kope_d3d12_execution_context;

#define KOPE_D3D12_NUM_EXECUTION_CONTEXTS 2
Expand All @@ -38,6 +36,9 @@ typedef struct kope_d3d12_device {
kope_index_allocator dsv_index_allocator;
uint32_t dsv_increment;

uint32_t cbv_srv_uav_increment;
uint32_t sampler_increment;

kope_d3d12_execution_context execution_contexts[KOPE_D3D12_NUM_EXECUTION_CONTEXTS];
uint32_t execution_context_index;

Expand All @@ -48,7 +49,7 @@ typedef struct kope_d3d12_device {
HANDLE frame_event;
uint64_t current_frame_index;

struct ID3D12DescriptorHeap *descriptor_sets_heap;
struct ID3D12DescriptorHeap *staging_descriptor_heap;
oa_allocator_t descriptor_sets_allocator;

kope_g5_command_list management_list;
Expand Down

0 comments on commit df288d8

Please sign in to comment.