Skip to content

Instantly share code, notes, and snippets.

@yorung
yorung / create_constant_buffer.cpp
Created July 9, 2016 09:06
[DX12] Create a huge constant buffer and Map it
static const UINT maxConstantBufferBlocks = 1000;
ComPtr<ID3D12Resource> constantBuffer;
struct { char buf[0x100]; } *mappedConstantBuffer = nullptr;
int size = maxConstantBufferBlocks * 0x100;
D3D12_HEAP_PROPERTIES prop = { D3D12_HEAP_TYPE_UPLOAD, D3D12_CPU_PAGE_PROPERTY_UNKNOWN, D3D12_MEMORY_POOL_UNKNOWN, 1, 1 };
D3D12_RESOURCE_DESC desc = { D3D12_RESOURCE_DIMENSION_BUFFER, 0, (UINT64)size, 1, 1, 1, DXGI_FORMAT_UNKNOWN, { 1, 0 }, D3D12_TEXTURE_LAYOUT_ROW_MAJOR, D3D12_RESOURCE_FLAG_NONE };
device->CreateCommittedResource(&prop, D3D12_HEAP_FLAG_NONE, &desc, D3D12_RESOURCE_STATE_GENERIC_READ, nullptr, IID_PPV_ARGS(&constantBuffer));
@yorung
yorung / heap_binder.cpp
Created May 8, 2016 06:40
Bind a descriptor heap.
void afSetDescriptorHeap(ComPtr<ID3D12DescriptorHeap> heap)
{
ID3D12GraphicsCommandList* list = deviceMan.GetCommandList();
ID3D12DescriptorHeap* ppHeaps[] = { heap.Get() };
list->SetDescriptorHeaps(_countof(ppHeaps), ppHeaps);
list->SetGraphicsRootDescriptorTable(0, heap->GetGPUDescriptorHandleForHeapStart());
}
@yorung
yorung / descriptor_heap_helper.cpp
Created May 8, 2016 01:53
A helper function to create descriptor heap from array of ID3D12Resource.
ComPtr<ID3D12DescriptorHeap> afCreateDescriptorHeap(int numSrvs, ComPtr<ID3D12Resource> srvs[])
{
ComPtr<ID3D12DescriptorHeap> heap;
D3D12_DESCRIPTOR_HEAP_DESC srvHeapDesc = {};
srvHeapDesc.NumDescriptors = numSrvs;
srvHeapDesc.Type = D3D12_DESCRIPTOR_HEAP_TYPE_CBV_SRV_UAV;
srvHeapDesc.Flags = D3D12_DESCRIPTOR_HEAP_FLAG_SHADER_VISIBLE;
HRESULT hr = deviceMan.GetDevice()->CreateDescriptorHeap(&srvHeapDesc, IID_PPV_ARGS(&heap));
assert(hr == S_OK);
for (int i = 0; i < numSrvs; i++) {
@yorung
yorung / root_signature_helper.cpp
Created May 8, 2016 01:25
A wrapper for root signature creation.
ComPtr<ID3D12RootSignature> afCreateRootSignature(int numDescriptors, D3D12_DESCRIPTOR_RANGE descriptors[], int numSamplers, D3D12_STATIC_SAMPLER_DESC samplers[])
{
ComPtr<ID3D12RootSignature> rs;
ComPtr<ID3DBlob> signature;
ComPtr<ID3DBlob> error;
D3D12_ROOT_PARAMETER rootParameter = {};
rootParameter.ParameterType = D3D12_ROOT_PARAMETER_TYPE_DESCRIPTOR_TABLE;
rootParameter.ShaderVisibility = D3D12_SHADER_VISIBILITY_ALL;
rootParameter.DescriptorTable.NumDescriptorRanges = numDescriptors;
@yorung
yorung / wrapper_classes.h
Last active May 8, 2016 00:51
DirectX 12 wrapper classes to simplify.
class CSampler : public D3D12_STATIC_SAMPLER_DESC
{
public:
CSampler(int shaderRegister, D3D12_FILTER samplerFilter, D3D12_TEXTURE_ADDRESS_MODE wrap)
{
Filter = samplerFilter;
AddressU = wrap;
AddressV = wrap;
AddressW = D3D12_TEXTURE_ADDRESS_MODE_CLAMP;
MipLODBias = 0;
@yorung
yorung / draw.cpp
Last active May 8, 2016 06:27
DX12 pseudo code of drawing
ID3D12GraphicsCommandList* list = deviceMan.GetCommandList();
list->SetPipelineState(pipelineState.Get());
list->SetGraphicsRootSignature(rootSignature.Get());
afSetDescriptorHeap(heap);
afWriteBuffer(uboId, &invVP, sizeof(invVP));
list->IASetPrimitiveTopology(D3D_PRIMITIVE_TOPOLOGY_TRIANGLESTRIP);
@yorung
yorung / create_resources.cpp
Last active April 27, 2016 13:44
DX12 pseudo code of resource generation.
ComPtr<ID3D12Resource> texId = afLoadTexture(texFileName, texDesc);
ComPtr<ID3D12Resource> uboId = afCreateUBO(sizeof(Mat));
ComPtr<ID3D12Resource> srvs[] = {
uboId,
texId,
};
ComPtr<ID3D12DescriptorHeap> heap = afCreateDescriptorHeap(dimof(srvs), srvs);
@yorung
yorung / create_pso.cpp
Last active May 8, 2016 00:52
DX12 pseudo code of creating A PSO.
D3D12_DESCRIPTOR_RANGE descriptors[] = { // The descriptor table
CDescriptorCBV(0), // constant buffer view bound to register b0
CDescriptorSRV(0), // shader resource view bound to register t0
};
D3D12_STATIC_SAMPLER_DESC samplers[] = {
CSampler(0, D3D12_FILTER_MIN_MAG_LINEAR_MIP_POINT, D3D12_TEXTURE_ADDRESS_MODE_WRAP), // sampler bound to register s0
};
ComPtr<ID3D12RootSignature> rootSignature = afCreateRootSignature(dimof(descriptors), descriptors, dimof(samplers), samplers);
ComPtr<ID3D12PipelineState> pipelineState = afCreatePSO(shader, nullptr, 0, BM_NONE, DSM_DEPTH_CLOSEREQUAL_READONLY, CM_DISABLE, rootSignature);
@yorung
yorung / sample_vertex_shader.vert.glsl
Created March 28, 2016 16:38
A simple GLSL vertex shader
#version 310 es
precision highp float;
in vec3 POSITION;
in vec2 TEXCOORD;
in vec4 COLOR;
out vec2 texcoord;
out vec4 color;
layout (binding = 0) uniform matUbo {
layout (row_major) mat4 matProj;
@yorung
yorung / vao_maker.cpp
Last active March 28, 2016 16:47
A OpenGL sample to create vertex array object(VAO)
void afSetVertexAttributes(const InputElement elements[], int numElements, int numBuffers, GLuint const vertexBufferIds[], const int strides[])
{
for (int i = 0; i < numElements; i++) {
const InputElement& d = elements[i];
glBindBuffer(GL_ARRAY_BUFFER, vertexBufferIds[d.inputSlot]);
GLenum r = glGetError();
if (r != GL_NO_ERROR) {
aflog("glBindBuffer error! i=%d inputSlot=%d vbo=%d\n", i, d.inputSlot, vertexBufferIds[d.inputSlot].x);
}
afHandleGLError(glEnableVertexAttribArray(i));