From 355bf92bc0eeeb22a5e48eee235000d2c26e029e Mon Sep 17 00:00:00 2001 From: Robert Konrad Date: Wed, 11 Sep 2024 00:28:39 +0200 Subject: [PATCH] Copy buffers to textures and take care of buffer alignment --- .../Sources/kope/direct3d12/commandlist.cpp | 19 +++++++++++++++++++ .../Sources/kope/direct3d12/descriptorset.cpp | 4 +++- .../Sources/kope/direct3d12/device.cpp | 4 ++-- Sources/kope/util/align.c | 6 ++++++ Sources/kope/util/align.h | 16 ++++++++++++++++ Sources/kope/util/utilunit.c | 1 + 6 files changed, 47 insertions(+), 3 deletions(-) create mode 100644 Sources/kope/util/align.c create mode 100644 Sources/kope/util/align.h diff --git a/Backends/Graphics5/Direct3D12/Sources/kope/direct3d12/commandlist.cpp b/Backends/Graphics5/Direct3D12/Sources/kope/direct3d12/commandlist.cpp index 994ce1526..afc977d96 100644 --- a/Backends/Graphics5/Direct3D12/Sources/kope/direct3d12/commandlist.cpp +++ b/Backends/Graphics5/Direct3D12/Sources/kope/direct3d12/commandlist.cpp @@ -102,3 +102,22 @@ void kope_d3d12_command_list_set_descriptor_table(kope_g5_command_list *list, ui list->d3d12.list->SetGraphicsRootDescriptorTable(table_index, gpu_descriptor); } } + +void kope_g5_command_list_copy_buffer_to_texture(kope_g5_command_list *list, kope_g5_buffer *source, kope_g5_texture *destination, kope_uint3 size) { + D3D12_TEXTURE_COPY_LOCATION dst; + dst.pResource = destination->d3d12.resource; + dst.Type = D3D12_TEXTURE_COPY_TYPE_SUBRESOURCE_INDEX; + dst.SubresourceIndex = 0; + + D3D12_TEXTURE_COPY_LOCATION src; + src.pResource = source->d3d12.resource; + src.Type = D3D12_TEXTURE_COPY_TYPE_PLACED_FOOTPRINT; + src.PlacedFootprint.Offset = 0; + src.PlacedFootprint.Footprint.Depth = 1; + src.PlacedFootprint.Footprint.Format = DXGI_FORMAT_R8G8B8A8_UNORM; + src.PlacedFootprint.Footprint.Height = 512; + src.PlacedFootprint.Footprint.RowPitch = 512 * 4; + src.PlacedFootprint.Footprint.Width = 512; + + list->d3d12.list->CopyTextureRegion(&dst, 0, 0, 0, &src, NULL); +} diff --git a/Backends/Graphics5/Direct3D12/Sources/kope/direct3d12/descriptorset.cpp b/Backends/Graphics5/Direct3D12/Sources/kope/direct3d12/descriptorset.cpp index 4c5f388b7..869c9d1e9 100644 --- a/Backends/Graphics5/Direct3D12/Sources/kope/direct3d12/descriptorset.cpp +++ b/Backends/Graphics5/Direct3D12/Sources/kope/direct3d12/descriptorset.cpp @@ -1,10 +1,12 @@ #include "descriptorset_functions.h" #include "descriptorset_structs.h" +#include + 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; + desc.SizeInBytes = align_pow2((int)buffer->d3d12.size, 256); 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; diff --git a/Backends/Graphics5/Direct3D12/Sources/kope/direct3d12/device.cpp b/Backends/Graphics5/Direct3D12/Sources/kope/direct3d12/device.cpp index 16bef75e1..83f16b48e 100644 --- a/Backends/Graphics5/Direct3D12/Sources/kope/direct3d12/device.cpp +++ b/Backends/Graphics5/Direct3D12/Sources/kope/direct3d12/device.cpp @@ -178,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 = 256; // parameters->size; + resourceDesc.Width = align_pow2((int)parameters->size, 256); // 256 required for CBVs resourceDesc.Height = 1; resourceDesc.DepthOrArraySize = 1; resourceDesc.MipLevels = 1; @@ -188,7 +188,7 @@ 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; + buffer->d3d12.size = 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))); diff --git a/Sources/kope/util/align.c b/Sources/kope/util/align.c new file mode 100644 index 000000000..711af0960 --- /dev/null +++ b/Sources/kope/util/align.c @@ -0,0 +1,6 @@ +#include "align.h" + +int align_pow2(int value, int pow2_alignment) { + int mask = pow2_alignment - 1; + return value + (-value & mask); +} diff --git a/Sources/kope/util/align.h b/Sources/kope/util/align.h new file mode 100644 index 000000000..c387773f4 --- /dev/null +++ b/Sources/kope/util/align.h @@ -0,0 +1,16 @@ +#ifndef KOPE_ALIGN_HEADER +#define KOPE_ALIGN_HEADER + +#include + +#ifdef __cplusplus +extern "C" { +#endif + +int align_pow2(int value, int pow2_alignment); + +#ifdef __cplusplus +} +#endif + +#endif diff --git a/Sources/kope/util/utilunit.c b/Sources/kope/util/utilunit.c index 70f36fc4d..348edccc1 100644 --- a/Sources/kope/util/utilunit.c +++ b/Sources/kope/util/utilunit.c @@ -1,2 +1,3 @@ +#include "align.c" #include "indexallocator.c" #include "offalloc/offalloc.c"