Skip to content

Instantly share code, notes, and snippets.

@yorung
Created July 9, 2016 10:14
Show Gist options
  • Select an option

  • Save yorung/e039764cf6021b0228a350e07f48025d to your computer and use it in GitHub Desktop.

Select an option

Save yorung/e039764cf6021b0228a350e07f48025d to your computer and use it in GitHub Desktop.
[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;
numAssignedConstantBufferBlocks += numRequired;
memcpy(mappedConstantBuffer + top, buf, size);
D3D12_CONSTANT_BUFFER_VIEW_DESC cbvDesc = {};
cbvDesc.BufferLocation = constantBuffer->GetGPUVirtualAddress() + top * 0x100;
cbvDesc.SizeInBytes = sizeAligned;
assert((cbvDesc.SizeInBytes & 0xff) == 0);
D3D12_CPU_DESCRIPTOR_HANDLE ptr = srvHeap->GetCPUDescriptorHandleForHeapStart();
ptr.ptr += deviceMan.GetDevice()->GetDescriptorHandleIncrementSize(D3D12_DESCRIPTOR_HEAP_TYPE_CBV_SRV_UAV) * descriptorHeapIndex;
deviceMan.GetDevice()->CreateConstantBufferView(&cbvDesc, ptr);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment