Skip to content

Instantly share code, notes, and snippets.

@yorung
yorung / DefineABuffer.h
Created January 15, 2017 13:36
[DX12] Define a vertex buffer and its view.
ID3D12Resource* vertexBuffer;
D3D12_VERTEX_BUFFER_VIEW vertexBufferView;
@yorung
yorung / set_render_target_and_clear.cpp
Last active September 3, 2016 16:54
[DX12] Create RTV and DSV on stack and discard immediately. Unlike D3D12_DESCRIPTOR_HEAP_TYPE_CBV_SRV_UAV, RTV and DSV can discard after API calls.
// DON'T DO THIS!!
// It seems working fine, however the DX12 debug layer treat this as following error.
//
// "An ID3D12DescriptorHeap object referenced in a command list ID3D12GraphicsCommandList Object "..." was
// deleted prior to executing the command list. This is invalid and can result in application instability.
// [ EXECUTION ERROR #921: OBJECT_DELETED_WHILE_STILL_IN_USE]"
//
ComPtr<ID3D12DescriptorHeap> rtvHeap, dsvHeap;
const D3D12_DESCRIPTOR_HEAP_DESC rtvHeapDesc = { D3D12_DESCRIPTOR_HEAP_TYPE_RTV, 1 };
device->CreateDescriptorHeap(&rtvHeapDesc, IID_PPV_ARGS(&rtvHeap));
@yorung
yorung / set_index_buffer.cpp
Created September 3, 2016 11:39
[DX12] Set index buffer, forget D3D12_INDEX_BUFFER_VIEW.
void SetIndexBuffer(ID3D12GraphicsCommandList* list, ID3D12Resource* indexBuffer)
{
D3D12_RESOURCE_DESC desc = indexBuffer->GetDesc();
D3D12_INDEX_BUFFER_VIEW indexBufferView = { indexBuffer->GetGPUVirtualAddress(), (UINT)desc.Width, AFIndexTypeToDevice };
list->IASetIndexBuffer(&indexBufferView);
}
@yorung
yorung / dx12_extract_root_signature_from_shader_binary.cpp
Last active March 14, 2020 20:15
[DX12] Extract root signature binary from a shader binary created with D3DCompileFromFile.
...
ComPtr<ID3DBlob> vertexShader = afCompileHLSL(shaderName, "VSMain", "vs_5_0");
ComPtr<ID3DBlob> pixelShader = afCompileHLSL(shaderName, "PSMain", "ps_5_0");
ComPtr<ID3DBlob> rootSignatureBlob;
ComPtr<ID3D12RootSignature> rootSignature;
if (S_OK == D3DGetBlobPart(vertexShader->GetBufferPointer(), vertexShader->GetBufferSize(), D3D_BLOB_ROOT_SIGNATURE, 0, &rootSignatureBlob))
{
device->CreateRootSignature(0, rootSignatureBlob->GetBufferPointer(), rootSignatureBlob->GetBufferSize(), IID_PPV_ARGS(&rootSignature));
}
...
@yorung
yorung / sky_cubemap.hlsl
Created August 27, 2016 04:06
[DX12] Define root signature in HLSL.
cbuffer perObject : register(b0)
{
row_major matrix invVP;
}
TextureCube texCube : register(t0);
SamplerState samplerState : register(s0);
struct VsToPs
{
@yorung
yorung / upload_texture.cpp
Created August 21, 2016 11:49
[DX12] Upload texture.
void afWriteTexture(ComPtr<ID3D12Resource> tex, const TexDesc& desc, int mipCount, const AFTexSubresourceData datas[])
{
const int maxSubresources = 100;
const UINT subResources = mipCount * desc.arraySize;
assert(subResources <= maxSubresources);
D3D12_PLACED_SUBRESOURCE_FOOTPRINT footprints[maxSubresources];
UINT64 rowSizeInBytes[maxSubresources], uploadSize;
UINT numRows[maxSubresources];
D3D12_RESOURCE_BARRIER transitions1[maxSubresources], transitions2[maxSubresources];
deviceMan.GetDevice()->GetCopyableFootprints(&tex->GetDesc(), 0, subResources, 0, footprints, numRows, rowSizeInBytes, &uploadSize);
@yorung
yorung / dx12_create_texture_from_dds.cpp
Last active August 21, 2016 12:31
[DX12] Create texture from dds files.
struct DDSHeader {
uint32_t h3[3];
int h, w;
uint32_t h2[2];
int mipCnt;
uint32_t h13[13];
uint32_t fourcc, bitsPerPixel, rMask, gMask, bMask, aMask, caps1, caps2;
bool IsCubeMap() const { return caps2 == 0xFE00; }
int GetArraySize() const { return IsCubeMap() ? 6 : 1; }
int GetMipCnt() const { return std::max(mipCnt, 1); }
@yorung
yorung / simple_texture_uploader.cpp
Last active July 27, 2016 16:28
[DX12] Simple texture uploader
void afWriteBuffer(const ComPtr<ID3D12Resource> res, const void* buf, int size)
{
void* p;
D3D12_RANGE readRange = {};
res->Map(0, &readRange, &p);
memcpy(p, buf, size);
D3D12_RANGE wroteRange = {0, (SIZE_T)size};
id->Unmap(0, &wroteRange);
}
@yorung
yorung / end_frame.cpp
Created July 23, 2016 08:50
[DX12] Record fenceValue to both command queue and frameResource.
FrameResources& res = frameResources[frameIndex];
commandQueue->Signal(fence.Get(), res.fenceValueToGuard = fenceValue++);
@yorung
yorung / begin_frame.cpp
Created July 23, 2016 08:35
[DX12] Wait GPU commands finish that committed 2 frames before.
frameIndex = swapChain->GetCurrentBackBufferIndex();
FrameResources& res = frameResources[frameIndex];
WaitFenceValue(fence, res.fenceValueToGuard);