gpu layer refactor progress

This commit is contained in:
jacob 2025-11-19 16:32:42 -06:00
parent b10866bed7
commit b4a51ff5af
14 changed files with 193 additions and 334 deletions

View File

@ -13,15 +13,13 @@ Enum(GPU_QueueKind)
{ {
#if GPU_MultiQueueEnabled #if GPU_MultiQueueEnabled
GPU_QueueKind_Direct = 0, GPU_QueueKind_Direct = 0,
GPU_QueueKind_Compute = 1, GPU_QueueKind_AsyncCompute = 1,
GPU_QueueKind_Copy = 2, GPU_QueueKind_AsyncCopy = 2,
GPU_QueueKind_BackgroundCopy = 3, GPU_NumQueues = 3
GPU_NumQueues = 4
#else #else
GPU_QueueKind_Direct = 0, GPU_QueueKind_Direct = 0,
GPU_QueueKind_Compute = 0, GPU_QueueKind_AsyncCompute = 0,
GPU_QueueKind_Copy = 0, GPU_QueueKind_AsyncCopy = 0,
GPU_QueueKind_BackgroundCopy = 0,
GPU_NumQueues = 1 GPU_NumQueues = 1
#endif #endif
}; };

View File

@ -1,132 +1,21 @@
GPU_D12_SharedState GPU_D12_shared_state = ZI; GPU_D12_SharedState GPU_D12_shared_state = ZI;
////////////////////////////////////////////////////////////
//~ Helpers
GPU_D12_FiberState *GPU_D12_FiberStateFromId(i16 fiber_id)
{
GPU_D12_SharedState *g = &GPU_D12_shared_state;
GPU_D12_FiberState **f = &g->fiber_states[fiber_id];
if (!*f)
{
Arena *perm = PermArena();
*f = PushStruct(perm, GPU_D12_FiberState);
}
return *f;
}
DXGI_FORMAT GPU_D12_DxgiFormatFromGpuFormat(GPU_Format format)
{
return (DXGI_FORMAT)format;
}
GPU_D12_Command *GPU_D12_PushCmd(GPU_D12_CommandList *cl)
{
GPU_D12_FiberState *f = GPU_D12_FiberStateFromId(FiberId());
Arena *perm = PermArena();
GPU_D12_Command *cmd = f->first_free_command;
if (cmd)
{
f->first_free_command = cmd->next;
}
else
{
cmd = PushStructNoZero(perm, GPU_D12_Command);
}
ZeroStruct(cmd);
SllQueuePush(cl->first, cl->last, cmd);
++cl->count;
return cmd;
}
u64 GPU_D12_ReuseHashFromResourceDesc(GPU_ResourceDesc desc, u64 buffer_size)
{
u64 result = RandU64FromSeeds(desc.kind, desc.flags);
switch(desc.kind)
{
default: break;
case GPU_ResourceKind_Texture1D:
case GPU_ResourceKind_Texture2D:
case GPU_ResourceKind_Texture3D:
{
result = RandU64FromSeeds(result, desc.texture.format);
result = RandU64FromSeeds(result, desc.texture.mip_levels);
result = RandU64FromSeeds(result, desc.clear_color.x);
result = RandU64FromSeeds(result, desc.clear_color.y);
result = RandU64FromSeeds(result, desc.clear_color.z);
result = RandU64FromSeeds(result, desc.clear_color.w);
result = RandU64FromSeeds(result, desc.texture.size.x);
result = RandU64FromSeeds(result, desc.texture.size.y);
result = RandU64FromSeeds(result, desc.texture.size.z);
} break;
case GPU_ResourceKind_Buffer:
{
result = RandU64FromSeeds(result, desc.buffer.heap_kind);
result = RandU64FromSeeds(result, buffer_size);
} break;
}
return result;
}
//////////////////////////////////////////////////////////// ////////////////////////////////////////////////////////////
//~ Startup //~ Startup
void GPU_D12_Startup(void) void GPU_D12_Startup(void)
{
GPU_D12_SharedState *g = &GPU_D12_shared_state;
/* Init device */
GPU_D12_InitDevice();
/* Init queues */
{
GPU_D12_QueueDesc descs[] = {
{.kind = GPU_QueueKind_Direct, .d3d_type = D3D12_COMMAND_LIST_TYPE_DIRECT, .d3d_priority = D3D12_COMMAND_QUEUE_PRIORITY_NORMAL, .dbg_name = Lit("Direct queue") },
{.kind = GPU_QueueKind_Compute, .d3d_type = D3D12_COMMAND_LIST_TYPE_COMPUTE, .d3d_priority = D3D12_COMMAND_QUEUE_PRIORITY_NORMAL, .dbg_name = Lit("Compute queue") },
{.kind = GPU_QueueKind_Copy, .d3d_type = D3D12_COMMAND_LIST_TYPE_COPY, .d3d_priority = D3D12_COMMAND_QUEUE_PRIORITY_HIGH, .dbg_name = Lit("Copy queue") },
{.kind = GPU_QueueKind_BackgroundCopy, .d3d_type = D3D12_COMMAND_LIST_TYPE_COPY, .d3d_priority = D3D12_COMMAND_QUEUE_PRIORITY_NORMAL, .dbg_name = Lit("Background copy queue") }
};
u32 job_count = 0; Fence job_fence = ZI;
job_count += RunJob(GPU_D12_InitQueue, .count = GPU_NumQueues, .sig.descs = descs, .fence = &job_fence);
YieldOnFence(&job_fence, job_count);
}
/* Init descriptor heaps */
g->cbv_srv_uav_heap = GPU_D12_InitDescriptorHeap(D3D12_DESCRIPTOR_HEAP_TYPE_CBV_SRV_UAV,
D3D12_DESCRIPTOR_HEAP_FLAG_SHADER_VISIBLE,
GPU_D12_MaxCbvSrvUavDescriptors,
ID3D12Device_GetDescriptorHandleIncrementSize(g->device, D3D12_DESCRIPTOR_HEAP_TYPE_CBV_SRV_UAV));
g->sampler_heap = GPU_D12_InitDescriptorHeap(D3D12_DESCRIPTOR_HEAP_TYPE_SAMPLER,
D3D12_DESCRIPTOR_HEAP_FLAG_SHADER_VISIBLE,
GPU_D12_MaxSamplerDescriptors,
ID3D12Device_GetDescriptorHandleIncrementSize(g->device, D3D12_DESCRIPTOR_HEAP_TYPE_SAMPLER));
g->rtv_heap = GPU_D12_InitDescriptorHeap(D3D12_DESCRIPTOR_HEAP_TYPE_RTV,
D3D12_DESCRIPTOR_HEAP_FLAG_NONE,
GPU_D12_MaxRtvDescriptors,
ID3D12Device_GetDescriptorHandleIncrementSize(g->device, D3D12_DESCRIPTOR_HEAP_TYPE_RTV));
/* Init rootsig */
GPU_D12_InitRootsig();
/* Start queue sync job */
JobPoolId sync_pool = InitJobPool(1, Lit("Dx12 queue sync"), JobPoolPriority_Critical);
RunJob(GPU_D12_StartQueueSync, .pool = sync_pool);
}
////////////////////////////////////////////////////////////
//~ Initialization
//- Device initialization
void GPU_D12_InitDevice(void)
{ {
GPU_D12_SharedState *g = &GPU_D12_shared_state; GPU_D12_SharedState *g = &GPU_D12_shared_state;
TempArena scratch = BeginScratchNoConflict(); TempArena scratch = BeginScratchNoConflict();
Arena *perm = PermArena();
//////////////////////////////
//- Initialize device
{
HRESULT hr = 0; HRESULT hr = 0;
/* Enable debug layer */ //- Enable debug layer
u32 dxgi_factory_flags = 0; u32 dxgi_factory_flags = 0;
#if GPU_DEBUG #if GPU_DEBUG
{ {
@ -157,12 +46,9 @@ void GPU_D12_InitDevice(void)
ID3D12Debug_Release(debug_controller0); ID3D12Debug_Release(debug_controller0);
dxgi_factory_flags |= DXGI_CREATE_FACTORY_DEBUG; dxgi_factory_flags |= DXGI_CREATE_FACTORY_DEBUG;
} }
#endif
#if GPU_DEBUG == 0 && GPU_DEBUG_VALIDATION != 0
# error Gpu validation is enabled but gpu debugging is not
#endif #endif
/* Create factory */ //- Create factory
{ {
__profn("Create factory"); __profn("Create factory");
hr = CreateDXGIFactory2(dxgi_factory_flags, &IID_IDXGIFactory6, (void **)&g->factory); hr = CreateDXGIFactory2(dxgi_factory_flags, &IID_IDXGIFactory6, (void **)&g->factory);
@ -172,7 +58,7 @@ void GPU_D12_InitDevice(void)
} }
} }
/* Create device */ //- Create device
{ {
__profn("Create device"); __profn("Create device");
IDXGIAdapter3 *adapter = 0; IDXGIAdapter3 *adapter = 0;
@ -180,7 +66,7 @@ void GPU_D12_InitDevice(void)
String error = Lit("Could not initialize GPU device."); String error = Lit("Could not initialize GPU device.");
String first_gpu_name = ZI; String first_gpu_name = ZI;
u32 adapter_index = 0; u32 adapter_index = 0;
b32 skip = 0; /* For debugging iGPU */ b32 skip = 0; /* For iGPU testing */
for (;;) for (;;)
{ {
{ {
@ -232,6 +118,8 @@ void GPU_D12_InitDevice(void)
g->device = device; g->device = device;
} }
//- Enable debug layer breaks
{
#if GPU_DEBUG #if GPU_DEBUG
/* Enable D3D12 Debug break */ /* Enable D3D12 Debug break */
{ {
@ -246,7 +134,6 @@ void GPU_D12_InitDevice(void)
ID3D12InfoQueue_SetBreakOnSeverity(info, D3D12_MESSAGE_SEVERITY_ERROR, 1); ID3D12InfoQueue_SetBreakOnSeverity(info, D3D12_MESSAGE_SEVERITY_ERROR, 1);
ID3D12InfoQueue_Release(info); ID3D12InfoQueue_Release(info);
} }
/* Enable DXGI Debug break */ /* Enable DXGI Debug break */
{ {
__profn("Enable dxgi debug break"); __profn("Enable dxgi debug break");
@ -261,44 +148,83 @@ void GPU_D12_InitDevice(void)
IDXGIInfoQueue_Release(dxgi_info); IDXGIInfoQueue_Release(dxgi_info);
} }
#endif #endif
}
}
//////////////////////////////
//- Initialize queues
{
GPU_D12_Queue *direct = PushStruct(perm, GPU_D12_Queue);
GPU_D12_Queue *async_compute = PushStruct(perm, GPU_D12_Queue);
GPU_D12_Queue *async_copy = PushStruct(perm, GPU_D12_Queue);
g->queues[GPU_QueueKind_Direct] = direct;
g->queues[GPU_QueueKind_AsyncCompute] = async_compute;
g->queues[GPU_QueueKind_AsyncCopy] = async_copy;
b32 ok = 1;
{
if (ok)
{
D3D12_COMMAND_QUEUE_DESC desc = { .Type = D3D12_COMMAND_LIST_TYPE_DIRECT, .Priority = D3D12_COMMAND_QUEUE_PRIORITY_HIGH };
ok = SUCCEEDED(ID3D12Device_CreateCommandQueue(g->device, &desc, &IID_ID3D12CommandQueue, (void **)&direct->d3d_queue));
if (ok)
{
ok = SUCCEEDED(ID3D12Device_CreateFence(g->device, 0, 0, &IID_ID3D12Fence, (void **)&direct->submit_fence));
}
}
if (ok)
{
D3D12_COMMAND_QUEUE_DESC desc = { .Type = D3D12_COMMAND_LIST_TYPE_COMPUTE, .Priority = D3D12_COMMAND_QUEUE_PRIORITY_NORMAL };
ok = SUCCEEDED(ID3D12Device_CreateCommandQueue(g->device, &desc, &IID_ID3D12CommandQueue, (void **)&async_compute->d3d_queue));
if (ok)
{
ok = SUCCEEDED(ID3D12Device_CreateFence(g->device, 0, 0, &IID_ID3D12Fence, (void **)&async_compute->submit_fence));
}
}
if (ok)
{
D3D12_COMMAND_QUEUE_DESC desc = { .Type = D3D12_COMMAND_LIST_TYPE_COPY, .Priority = D3D12_COMMAND_QUEUE_PRIORITY_NORMAL };
ok = SUCCEEDED(ID3D12Device_CreateCommandQueue(g->device, &desc, &IID_ID3D12CommandQueue, (void **)&async_copy->d3d_queue));
if (ok)
{
ok = SUCCEEDED(ID3D12Device_CreateFence(g->device, 0, 0, &IID_ID3D12Fence, (void **)&async_copy->submit_fence));
}
}
}
if (!ok)
{
Panic(Lit("Failed to create GPU Command Queues"));
}
}
/* Init descriptor heaps */
g->cbv_srv_uav_heap = GPU_D12_InitDescriptorHeap(D3D12_DESCRIPTOR_HEAP_TYPE_CBV_SRV_UAV,
D3D12_DESCRIPTOR_HEAP_FLAG_SHADER_VISIBLE,
GPU_D12_MaxCbvSrvUavDescriptors,
ID3D12Device_GetDescriptorHandleIncrementSize(g->device, D3D12_DESCRIPTOR_HEAP_TYPE_CBV_SRV_UAV));
g->sampler_heap = GPU_D12_InitDescriptorHeap(D3D12_DESCRIPTOR_HEAP_TYPE_SAMPLER,
D3D12_DESCRIPTOR_HEAP_FLAG_SHADER_VISIBLE,
GPU_D12_MaxSamplerDescriptors,
ID3D12Device_GetDescriptorHandleIncrementSize(g->device, D3D12_DESCRIPTOR_HEAP_TYPE_SAMPLER));
g->rtv_heap = GPU_D12_InitDescriptorHeap(D3D12_DESCRIPTOR_HEAP_TYPE_RTV,
D3D12_DESCRIPTOR_HEAP_FLAG_NONE,
GPU_D12_MaxRtvDescriptors,
ID3D12Device_GetDescriptorHandleIncrementSize(g->device, D3D12_DESCRIPTOR_HEAP_TYPE_RTV));
/* Init rootsig */
GPU_D12_InitRootsig();
/* Start queue sync job */
JobPoolId sync_pool = InitJobPool(1, Lit("Dx12 queue sync"), JobPoolPriority_Critical);
RunJob(GPU_D12_StartQueueSync, .pool = sync_pool);
EndScratch(scratch); EndScratch(scratch);
} }
//- Queue initialization ////////////////////////////////////////////////////////////
//~ Initialization
JobDef(GPU_D12_InitQueue, sig, id)
{
GPU_D12_SharedState *g = &GPU_D12_shared_state;
GPU_D12_QueueDesc desc = sig->descs[id];
Arena *perm = PermArena();
HRESULT hr = 0;
GPU_D12_Queue *queue = 0;
{
PushAlign(perm, CachelineSize);
queue = PushStruct(perm, GPU_D12_Queue);
PushAlign(perm, CachelineSize);
}
queue->desc = desc;
D3D12_COMMAND_QUEUE_DESC d3d_desc = ZI;
d3d_desc.Type = desc.d3d_type;
d3d_desc.Priority = desc.d3d_priority;
hr = ID3D12Device_CreateCommandQueue(g->device, &d3d_desc, &IID_ID3D12CommandQueue, (void **)&queue->d3d_queue);
if (FAILED(hr))
{
Panic(Lit("Failed to create command queue"));
}
hr = ID3D12Device_CreateFence(g->device, 0, 0, &IID_ID3D12Fence, (void **)&queue->submit_fence);
if (FAILED(hr))
{
Panic(Lit("Failed to create command queue fence"));
}
g->queues[desc.kind] = queue;
}
//- Heap initialization //- Heap initialization
@ -772,31 +698,6 @@ void GPU_QueueWait(GPU_QueueKind a, GPU_QueueKind b, i64 b_target_fence_value)
ID3D12CommandQueue_Wait(queue_a->d3d_queue, b_fence, b_target_fence_value); ID3D12CommandQueue_Wait(queue_a->d3d_queue, b_fence, b_target_fence_value);
} }
////////////////////////////////////////////////////////////
//~ @hookdef Rasterizer helper hooks
GPU_Viewport GPU_ViewportFromRect(Rng2 rect)
{
GPU_Viewport viewport = ZI;
viewport.top_left_x = rect.p0.x;
viewport.top_left_y = rect.p0.y;
viewport.width = rect.p1.x - rect.p0.x;
viewport.height = rect.p1.y - rect.p0.y;
viewport.min_depth = 0.0f;
viewport.max_depth = 1.0f;
return viewport;
}
GPU_Scissor GPU_ScissorFromRect(Rng2 rect)
{
GPU_Scissor scissor = ZI;
scissor.left = rect.p0.x;
scissor.top = rect.p0.y;
scissor.right = rect.p1.x;
scissor.bottom = rect.p1.y;
return scissor;
}
//////////////////////////////////////////////////////////// ////////////////////////////////////////////////////////////
//~ @hookdef Resource hooks //~ @hookdef Resource hooks

View File

@ -130,14 +130,6 @@ Struct(GPU_D12_ResourceReuseListBin)
//////////////////////////////////////////////////////////// ////////////////////////////////////////////////////////////
//~ Queue types //~ Queue types
Struct(GPU_D12_QueueDesc)
{
GPU_QueueKind kind;
D3D12_COMMAND_LIST_TYPE d3d_type;
D3D12_COMMAND_QUEUE_PRIORITY d3d_priority;
String dbg_name;
};
Struct(GPU_D12_Queue) Struct(GPU_D12_Queue)
{ {
GPU_D12_QueueDesc desc; GPU_D12_QueueDesc desc;
@ -287,8 +279,6 @@ Struct(GPU_D12_FiberState)
Struct(GPU_D12_SharedState) Struct(GPU_D12_SharedState)
{ {
GPU_D12_FiberState *fiber_states[MaxFibers];
Atomic64Padded resource_barrier_gen; Atomic64Padded resource_barrier_gen;
/* Stats */ /* Stats */

BIN
src/pp/pp_res/sound/test.mp3 (Stored with Git LFS)

Binary file not shown.

Binary file not shown.

Before

Width:  |  Height:  |  Size: 4.2 KiB

BIN
src/proto/pp_vis/pp_vis_res/sound/test.mp3 (Stored with Git LFS)

Binary file not shown.

Binary file not shown.

BIN
src/proto/pp_vis/pp_vis_res/sprite/box.ase (Stored with Git LFS)

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

BIN
src/proto/pp_vis/pp_vis_res/sprite/gun.ase (Stored with Git LFS)

Binary file not shown.

Binary file not shown.

BIN
src/proto/pp_vis/pp_vis_res/sprite/tim.ase (Stored with Git LFS)

Binary file not shown.