hello triangle!
This commit is contained in:
parent
84f6eb4583
commit
b9d9e60e9f
@ -109,6 +109,20 @@ union U128
|
|||||||
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////
|
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////
|
||||||
//~
|
//~
|
||||||
|
|
||||||
|
global U64 U64_MAX = 0xffffffffffffffffull;
|
||||||
|
global U32 U32_MAX = 0xffffffff;
|
||||||
|
global U16 U16_MAX = 0xffff;
|
||||||
|
global U8 U8_MAX = 0xff;
|
||||||
|
global S64 S64_MAX = (S64)0x7fffffffffffffffll;
|
||||||
|
global S32 S32_MAX = (S32)0x7fffffff;
|
||||||
|
global S16 S16_MAX = (S16)0x7fff;
|
||||||
|
global S8 S8_MAX = (S8)0x7f;
|
||||||
|
|
||||||
|
global S64 S64_MIN = (S64)0x8000000000000000ll;
|
||||||
|
global S32 S32_MIN = (S32)0x80000000;
|
||||||
|
global S16 S16_MIN = (S16)0x8000;
|
||||||
|
global S8 S8_MIN = (S8)0x80;
|
||||||
|
|
||||||
global const U32 bitmask1 = 0x00000001;
|
global const U32 bitmask1 = 0x00000001;
|
||||||
global const U32 bitmask2 = 0x00000003;
|
global const U32 bitmask2 = 0x00000003;
|
||||||
global const U32 bitmask3 = 0x00000007;
|
global const U32 bitmask3 = 0x00000007;
|
||||||
|
|||||||
@ -33,6 +33,7 @@
|
|||||||
global R_D3D12_State *r_d3d12_state = 0;
|
global R_D3D12_State *r_d3d12_state = 0;
|
||||||
global R_D3D12_Command *r_d3d12_cmd = 0;
|
global R_D3D12_Command *r_d3d12_cmd = 0;
|
||||||
global R_D3D12_RenderTarget *r_d3d12_rt = 0;
|
global R_D3D12_RenderTarget *r_d3d12_rt = 0;
|
||||||
|
global R_D3D12_Pipeline *r_d3d12_pipeline = 0;
|
||||||
|
|
||||||
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////
|
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////
|
||||||
//~
|
//~
|
||||||
@ -95,6 +96,20 @@ internal void r_init()
|
|||||||
r_d3d12_state->arena = arena;
|
r_d3d12_state->arena = arena;
|
||||||
r_d3d12_state->window_handle = g_win32_window_handle;
|
r_d3d12_state->window_handle = g_win32_window_handle;
|
||||||
|
|
||||||
|
r_d3d12_state->viewport = push_array(arena, D3D12_VIEWPORT, 1);
|
||||||
|
r_d3d12_state->viewport->TopLeftX = 0.0f;
|
||||||
|
r_d3d12_state->viewport->TopLeftY = 0.0f;
|
||||||
|
r_d3d12_state->viewport->Width = (F32)WINDOW_WIDTH_PX;
|
||||||
|
r_d3d12_state->viewport->Height = (F32)WINDOW_HEIGHT_PX;
|
||||||
|
r_d3d12_state->viewport->MinDepth = 0.0f;
|
||||||
|
r_d3d12_state->viewport->MaxDepth = 1.0f;
|
||||||
|
|
||||||
|
r_d3d12_state->scissor_rect = push_array(arena, D3D12_RECT, 1);
|
||||||
|
r_d3d12_state->scissor_rect->left = 0;
|
||||||
|
r_d3d12_state->scissor_rect->top = 0;
|
||||||
|
r_d3d12_state->scissor_rect->right = WINDOW_WIDTH_PX;
|
||||||
|
r_d3d12_state->scissor_rect->bottom = WINDOW_HEIGHT_PX;
|
||||||
|
|
||||||
r_d3d12_cmd = push_array(arena,
|
r_d3d12_cmd = push_array(arena,
|
||||||
R_D3D12_Command,
|
R_D3D12_Command,
|
||||||
1);
|
1);
|
||||||
@ -105,6 +120,10 @@ internal void r_init()
|
|||||||
R_D3D12_RenderTarget, 1);
|
R_D3D12_RenderTarget, 1);
|
||||||
r_d3d12_rt->arena = arena;
|
r_d3d12_rt->arena = arena;
|
||||||
|
|
||||||
|
r_d3d12_pipeline = push_array(arena,
|
||||||
|
R_D3D12_Pipeline, 1);
|
||||||
|
r_d3d12_pipeline->arena = arena;
|
||||||
|
|
||||||
HRESULT hr;
|
HRESULT hr;
|
||||||
hr = D3D12GetDebugInterface(&IID_ID3D12Debug, (void **)&r_d3d12_state->debug);
|
hr = D3D12GetDebugInterface(&IID_ID3D12Debug, (void **)&r_d3d12_state->debug);
|
||||||
if(SUCCEEDED(hr) && r_d3d12_state->debug)
|
if(SUCCEEDED(hr) && r_d3d12_state->debug)
|
||||||
@ -363,6 +382,80 @@ internal void r_init()
|
|||||||
&err_blob);
|
&err_blob);
|
||||||
D3D12_SHADER_CHECK(hr, err_blob);
|
D3D12_SHADER_CHECK(hr, err_blob);
|
||||||
|
|
||||||
|
/// Vertex input layout
|
||||||
|
D3D12_INPUT_ELEMENT_DESC input_element_desc[2];
|
||||||
|
input_element_desc[0].SemanticName = "POSITION";
|
||||||
|
input_element_desc[0].SemanticIndex = 0;
|
||||||
|
input_element_desc[0].Format = DXGI_FORMAT_R32G32B32_FLOAT;
|
||||||
|
input_element_desc[0].InputSlot = 0;
|
||||||
|
input_element_desc[0].AlignedByteOffset = 0;
|
||||||
|
input_element_desc[0].InputSlotClass = D3D12_INPUT_CLASSIFICATION_PER_VERTEX_DATA;
|
||||||
|
input_element_desc[0].InstanceDataStepRate = 0;
|
||||||
|
|
||||||
|
input_element_desc[1].SemanticName = "COLOR";
|
||||||
|
input_element_desc[1].SemanticIndex = 0;
|
||||||
|
input_element_desc[1].Format = DXGI_FORMAT_R32G32B32A32_FLOAT;
|
||||||
|
input_element_desc[1].InputSlot = 0;
|
||||||
|
input_element_desc[1].AlignedByteOffset = 12;
|
||||||
|
input_element_desc[1].InputSlotClass = D3D12_INPUT_CLASSIFICATION_PER_VERTEX_DATA;
|
||||||
|
input_element_desc[1].InstanceDataStepRate = 0;
|
||||||
|
|
||||||
|
|
||||||
|
// Pipeline State Object PSO
|
||||||
|
D3D12_GRAPHICS_PIPELINE_STATE_DESC pso_desc = {0};
|
||||||
|
{
|
||||||
|
pso_desc.InputLayout.pInputElementDescs = input_element_desc;
|
||||||
|
pso_desc.InputLayout.NumElements = ArrayCount(input_element_desc);
|
||||||
|
|
||||||
|
pso_desc.pRootSignature = r_d3d12_state->root_signature;
|
||||||
|
|
||||||
|
pso_desc.VS = r_d3d12_get_shader_bytecode(vtx_shader);
|
||||||
|
pso_desc.PS = r_d3d12_get_shader_bytecode(pxl_shader);
|
||||||
|
|
||||||
|
D3D12_RASTERIZER_DESC raster_desc = { .FillMode = D3D12_FILL_MODE_SOLID,
|
||||||
|
.CullMode = D3D12_CULL_MODE_BACK,
|
||||||
|
.FrontCounterClockwise = FALSE,
|
||||||
|
.DepthBias = D3D12_DEFAULT_DEPTH_BIAS,
|
||||||
|
.DepthBiasClamp = D3D12_DEFAULT_DEPTH_BIAS_CLAMP,
|
||||||
|
.SlopeScaledDepthBias = D3D12_DEFAULT_SLOPE_SCALED_DEPTH_BIAS,
|
||||||
|
.DepthClipEnable = TRUE,
|
||||||
|
.MultisampleEnable = FALSE,
|
||||||
|
.AntialiasedLineEnable = FALSE,
|
||||||
|
.ForcedSampleCount = 0,
|
||||||
|
.ConservativeRaster = D3D12_CONSERVATIVE_RASTERIZATION_MODE_OFF};
|
||||||
|
pso_desc.RasterizerState = raster_desc;
|
||||||
|
|
||||||
|
D3D12_BLEND_DESC blend_desc = {0};
|
||||||
|
blend_desc.RenderTarget[0].SrcBlend = D3D12_BLEND_ONE;
|
||||||
|
blend_desc.RenderTarget[0].DestBlend = D3D12_BLEND_ZERO;
|
||||||
|
blend_desc.RenderTarget[0].BlendOp = D3D12_BLEND_OP_ADD;
|
||||||
|
blend_desc.RenderTarget[0].SrcBlendAlpha = D3D12_BLEND_ONE;
|
||||||
|
blend_desc.RenderTarget[0].DestBlendAlpha = D3D12_BLEND_ZERO;
|
||||||
|
blend_desc.RenderTarget[0].BlendOpAlpha = D3D12_BLEND_OP_ADD;
|
||||||
|
blend_desc.RenderTarget[0].LogicOp = D3D12_LOGIC_OP_NOOP;
|
||||||
|
blend_desc.RenderTarget[0].RenderTargetWriteMask = D3D12_COLOR_WRITE_ENABLE_ALL;
|
||||||
|
|
||||||
|
pso_desc.BlendState = blend_desc;
|
||||||
|
|
||||||
|
pso_desc.DepthStencilState.DepthEnable = FALSE;
|
||||||
|
pso_desc.DepthStencilState.StencilEnable = FALSE;
|
||||||
|
pso_desc.SampleMask = U32_MAX;
|
||||||
|
pso_desc.PrimitiveTopologyType = D3D12_PRIMITIVE_TOPOLOGY_TYPE_TRIANGLE;
|
||||||
|
pso_desc.NumRenderTargets = 1;
|
||||||
|
pso_desc.RTVFormats[0] = DXGI_FORMAT_R8G8B8A8_UNORM;
|
||||||
|
pso_desc.SampleDesc.Count = 1;
|
||||||
|
|
||||||
|
hr = ID3D12Device_CreateGraphicsPipelineState(r_d3d12_state->device,
|
||||||
|
&pso_desc,
|
||||||
|
&IID_ID3D12PipelineState,
|
||||||
|
(void **)&r_d3d12_pipeline->pso);
|
||||||
|
D3D12_CHECK(hr, "Failed to create pipeline state \n");
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
@ -382,8 +475,6 @@ internal void r_init()
|
|||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
// ---------------------------------------------------------------------------
|
// ---------------------------------------------------------------------------
|
||||||
// Fence
|
// Fence
|
||||||
hr = ID3D12Device_CreateFence(r_d3d12_state->device,
|
hr = ID3D12Device_CreateFence(r_d3d12_state->device,
|
||||||
@ -424,11 +515,18 @@ internal void r_render()
|
|||||||
|
|
||||||
HRESULT hr = ID3D12CommandAllocator_Reset(r_d3d12_cmd->allocator);
|
HRESULT hr = ID3D12CommandAllocator_Reset(r_d3d12_cmd->allocator);
|
||||||
D3D12_CHECK(hr, "Failed to reset command allocator\n");
|
D3D12_CHECK(hr, "Failed to reset command allocator\n");
|
||||||
hr = ID3D12GraphicsCommandList_Reset(r_d3d12_cmd->list1, r_d3d12_cmd->allocator, 0);
|
hr = ID3D12GraphicsCommandList_Reset(r_d3d12_cmd->list1, r_d3d12_cmd->allocator, r_d3d12_pipeline->pso);
|
||||||
D3D12_CHECK(hr, "Failed to reset command list\n");
|
D3D12_CHECK(hr, "Failed to reset command list\n");
|
||||||
|
|
||||||
U32 frame_index = IDXGISwapChain3_GetCurrentBackBufferIndex(r_d3d12_state->swapchain);
|
U32 frame_index = IDXGISwapChain3_GetCurrentBackBufferIndex(r_d3d12_state->swapchain);
|
||||||
|
|
||||||
|
ID3D12GraphicsCommandList_SetGraphicsRootSignature(r_d3d12_cmd->list1,
|
||||||
|
r_d3d12_state->root_signature);
|
||||||
|
ID3D12GraphicsCommandList_RSSetViewports(r_d3d12_cmd->list1,
|
||||||
|
1, r_d3d12_state->viewport);
|
||||||
|
ID3D12GraphicsCommandList_RSSetScissorRects(r_d3d12_cmd->list1,
|
||||||
|
1, r_d3d12_state->scissor_rect);
|
||||||
|
|
||||||
D3D12_RESOURCE_BARRIER barrier =
|
D3D12_RESOURCE_BARRIER barrier =
|
||||||
{
|
{
|
||||||
.Type = D3D12_RESOURCE_BARRIER_TYPE_TRANSITION,
|
.Type = D3D12_RESOURCE_BARRIER_TYPE_TRANSITION,
|
||||||
@ -448,6 +546,9 @@ internal void r_render()
|
|||||||
ID3D12DescriptorHeap_GetCPUDescriptorHandleForHeapStart(r_d3d12_rt->heap, &rtv_handle);
|
ID3D12DescriptorHeap_GetCPUDescriptorHandleForHeapStart(r_d3d12_rt->heap, &rtv_handle);
|
||||||
rtv_handle.ptr += frame_index * r_d3d12_rt->descriptor_size;
|
rtv_handle.ptr += frame_index * r_d3d12_rt->descriptor_size;
|
||||||
|
|
||||||
|
ID3D12GraphicsCommandList_OMSetRenderTargets(r_d3d12_cmd->list1,
|
||||||
|
1, &rtv_handle, FALSE, 0);
|
||||||
|
|
||||||
// Clear render target
|
// Clear render target
|
||||||
F32 clearColor[4] = {0.1f, 0.2f, 0.3f, 1.0f};
|
F32 clearColor[4] = {0.1f, 0.2f, 0.3f, 1.0f};
|
||||||
ID3D12GraphicsCommandList_ClearRenderTargetView(r_d3d12_cmd->list1,
|
ID3D12GraphicsCommandList_ClearRenderTargetView(r_d3d12_cmd->list1,
|
||||||
@ -456,6 +557,13 @@ internal void r_render()
|
|||||||
0,
|
0,
|
||||||
0);
|
0);
|
||||||
|
|
||||||
|
ID3D12GraphicsCommandList_IASetPrimitiveTopology(r_d3d12_cmd->list1,
|
||||||
|
D3D_PRIMITIVE_TOPOLOGY_TRIANGLELIST);
|
||||||
|
ID3D12GraphicsCommandList_IASetVertexBuffers(r_d3d12_cmd->list1,
|
||||||
|
0, 1, &r_d3d12_state->vtx_buf_view);
|
||||||
|
ID3D12GraphicsCommandList_DrawInstanced(r_d3d12_cmd->list1,
|
||||||
|
3, 1, 0, 0);
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
// Transition render target back to present state
|
// Transition render target back to present state
|
||||||
@ -508,6 +616,10 @@ internal void r_cleanup()
|
|||||||
D3D12_RELEASE(r_d3d12_state->swapchain);
|
D3D12_RELEASE(r_d3d12_state->swapchain);
|
||||||
D3D12_RELEASE(r_d3d12_state->fence);
|
D3D12_RELEASE(r_d3d12_state->fence);
|
||||||
D3D12_RELEASE(r_d3d12_state->vertex_buffer);
|
D3D12_RELEASE(r_d3d12_state->vertex_buffer);
|
||||||
|
D3D12_RELEASE(r_d3d12_state->root_signature);
|
||||||
|
|
||||||
|
// PSOs
|
||||||
|
D3D12_RELEASE(r_d3d12_pipeline->pso);
|
||||||
|
|
||||||
// RT
|
// RT
|
||||||
D3D12_RELEASE(r_d3d12_rt->heap);
|
D3D12_RELEASE(r_d3d12_rt->heap);
|
||||||
|
|||||||
@ -43,6 +43,13 @@ struct R_D3D12_RenderTarget
|
|||||||
ID3D12Resource *targets[R_NUM_FRAMES_IN_FLIGHT];
|
ID3D12Resource *targets[R_NUM_FRAMES_IN_FLIGHT];
|
||||||
};
|
};
|
||||||
|
|
||||||
|
typedef struct R_D3D12_Pipeline R_D3D12_Pipeline;
|
||||||
|
struct R_D3D12_Pipeline
|
||||||
|
{
|
||||||
|
Arena *arena;
|
||||||
|
ID3D12PipelineState *pso;
|
||||||
|
};
|
||||||
|
|
||||||
typedef struct R_D3D12_State R_D3D12_State;
|
typedef struct R_D3D12_State R_D3D12_State;
|
||||||
struct R_D3D12_State
|
struct R_D3D12_State
|
||||||
{
|
{
|
||||||
@ -59,6 +66,9 @@ struct R_D3D12_State
|
|||||||
D3D12_VERTEX_BUFFER_VIEW vtx_buf_view;
|
D3D12_VERTEX_BUFFER_VIEW vtx_buf_view;
|
||||||
ID3D12PipelineState *pipeline;
|
ID3D12PipelineState *pipeline;
|
||||||
ID3D12RootSignature *root_signature;
|
ID3D12RootSignature *root_signature;
|
||||||
|
D3D12_VIEWPORT *viewport;
|
||||||
|
D3D12_RECT *scissor_rect;
|
||||||
|
|
||||||
};
|
};
|
||||||
|
|
||||||
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////
|
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user