Skip to content

Instantly share code, notes, and snippets.

@yorung
yorung / flush_gpu.cpp
Created July 23, 2016 08:19
[DX12] Flush GPU commands
commandQueue->Signal(fence, fenceValue);
WaitFenceValue(fence, fenceValue++);
@yorung
yorung / frame_resources.h
Last active July 23, 2016 08:08
[DX12] Frame resources
class FrameResources {
public:
~FrameResources();
ComPtr<ID3D12Resource> renderTarget;
ComPtr<ID3D12CommandAllocator> commandAllocator;
ComPtr<ID3D12Resource> constantBuffer;
ComPtr<ID3D12DescriptorHeap> srvHeap;
struct { char buf[256]; } *mappedConstantBuffer = nullptr;
UINT64 fenceValueToGuard = 0;
} frameResources[2];
@yorung
yorung / wait_fence_value_if_needed.cpp
Last active July 19, 2016 15:41
[DX12] Wait until the fence value reaches a certain value if needed.
void WaitFenceValue(ComPtr<ID3D12Fence> fence, UINT64 value)
{
if (fence->GetCompletedValue() >= value) {
return;
}
HANDLE fenceEvent = CreateEvent(nullptr, FALSE, FALSE, nullptr);
fence->SetEventOnCompletion(value, fenceEvent);
WaitForSingleObject(fenceEvent, INFINITE);
CloseHandle(fenceEvent);
}
@yorung
yorung / command_queue.cpp
Created July 19, 2016 15:00
[DX12] ExecuteCommandLists and Signal
commandQueue->ExecuteCommandLists(1, &A);
commandQueue->Signal(fence, 1);
commandQueue->ExecuteCommandLists(1, &B);
commandQueue->Signal(fence, 2);
commandQueue->ExecuteCommandLists(1, &C);
commandQueue->Signal(fence, 3);
@yorung
yorung / wait_fence_value.cpp
Last active July 19, 2016 15:42
[DX12] Wait until the fence value reaches a certain value.
void WaitFenceValue(ComPtr<ID3D12Fence> fence, UINT64 value)
{
HANDLE fenceEvent = CreateEvent(nullptr, FALSE, FALSE, nullptr);
fence->SetEventOnCompletion(value, fenceEvent);
WaitForSingleObject(fenceEvent, INFINITE);
CloseHandle(fenceEvent);
}
@yorung
yorung / dynamic_gpu_memory_allocation.cpp
Created July 9, 2016 10:42
[DX12] Usage of dynamic GPU memory allocator
afSetPipeline(pipelineState, rootSignature);
Mat invVP = inv(matV * matP);
int descriptorHeapIndex = deviceMan.AssignDescriptorHeap(2);
deviceMan.AssignConstantBuffer(descriptorHeapIndex, &invVP, sizeof(invVP));
deviceMan.AssignSRV(descriptorHeapIndex + 1, texId);
deviceMan.SetAssignedDescriptorHeap(descriptorHeapIndex);
afDraw(PT_TRIANGLESTRIP, 4);
@yorung
yorung / assgin_constant_buffer.cpp
Created July 9, 2016 10:14
[DX12] Assign constant buffer and write its GPU address into descriptor heap
void DeviceManDX12::AssignConstantBuffer(int descriptorHeapIndex, const void* buf, int size)
{
int sizeAligned = (size + 0xff) & ~0xff;
int numRequired = sizeAligned / 0x100;
if (numAssignedConstantBufferBlocks + numRequired > maxConstantBufferBlocks) {
assert(0);
return;
}
int top = numAssignedConstantBufferBlocks;
@yorung
yorung / dx12_descriptor_heap_manager.cpp
Created July 9, 2016 09:56
[DX12] Allocate, assign SRV and bind descriptor heap
int DeviceManDX12::AssignDescriptorHeap(int numRequired)
{
if (numAssignedSrvs + numRequired > maxSrvs) {
assert(0);
return -1;
}
int head = numAssignedSrvs;
numAssignedSrvs = numAssignedSrvs + numRequired;
return head;
}
@yorung
yorung / create_huge_descriptor_heap.cpp
Created July 9, 2016 09:23
[DX12] Create a huge descriptor heap
static const UINT maxSrvs = 1024;
ComPtr<ID3D12DescriptorHeap> srvHeap;
const D3D12_DESCRIPTOR_HEAP_DESC srvHeapDesc = { D3D12_DESCRIPTOR_HEAP_TYPE_CBV_SRV_UAV, maxSrvs, D3D12_DESCRIPTOR_HEAP_FLAG_SHADER_VISIBLE };
device->CreateDescriptorHeap(&srvHeapDesc, IID_PPV_ARGS(&srvHeap));
@yorung
yorung / dx11_buffer_renaming.cpp
Created July 9, 2016 09:10
A DX11 pseudocode to involve buffer renaming
ConstantBuffer cb;
for (obj : objs) {
void *p;
cb.Map(&p);
memcpy(p, obj.data, sizeof());
cb.Unmap();
Draw()
}