Skip to content

Commit

Permalink
Copy buffers to textures and take care of buffer alignment
Browse files Browse the repository at this point in the history
  • Loading branch information
RobDangerous committed Sep 10, 2024
1 parent 9d89cbe commit 355bf92
Show file tree
Hide file tree
Showing 6 changed files with 47 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
#include "descriptorset_functions.h"
#include "descriptorset_structs.h"

#include <kope/util/align.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;
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;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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)));
Expand Down
6 changes: 6 additions & 0 deletions Sources/kope/util/align.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
#include "align.h"

int align_pow2(int value, int pow2_alignment) {
int mask = pow2_alignment - 1;
return value + (-value & mask);
}
16 changes: 16 additions & 0 deletions Sources/kope/util/align.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
#ifndef KOPE_ALIGN_HEADER
#define KOPE_ALIGN_HEADER

#include <stdint.h>

#ifdef __cplusplus
extern "C" {
#endif

int align_pow2(int value, int pow2_alignment);

#ifdef __cplusplus
}
#endif

#endif
1 change: 1 addition & 0 deletions Sources/kope/util/utilunit.c
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
#include "align.c"
#include "indexallocator.c"
#include "offalloc/offalloc.c"

0 comments on commit 355bf92

Please sign in to comment.