diff --git a/src/base/base_math.c b/src/base/base_math.c index b506513f..cb05a245 100644 --- a/src/base/base_math.c +++ b/src/base/base_math.c @@ -253,6 +253,26 @@ u64 PowU64(u64 base, u8 exp) } } +//////////////////////////////// +//~ Align up + +u64 AlignU64Pow2(u64 x) +{ + u64 result = 0; + if (x > 0) + { + result = x - 1; + result |= result >> 1; + result |= result >> 2; + result |= result >> 4; + result |= result >> 8; + result |= result >> 16; + result |= result >> 32; + ++result; + } + return result; +} + //////////////////////////////// //~ Logn @@ -1418,3 +1438,10 @@ Mat4x4 MulMat4x4(Mat4x4 m1, Mat4x4 m2) return result; } + +Mat4x4 ProjectMat4x4View(Xform view, f32 viewport_width, f32 viewport_height) +{ + Mat4x4 projection = Mat4x4FromOrtho(0.0, viewport_width, viewport_height, 0.0, -1.0, 1.0); + Mat4x4 view4x4 = Mat4x4FromXform(view); + return MulMat4x4(projection, view4x4); +} diff --git a/src/base/base_math.h b/src/base/base_math.h index b2fcca7f..cca831f1 100644 --- a/src/base/base_math.h +++ b/src/base/base_math.h @@ -218,6 +218,7 @@ i64 SignF64(f64 f); //~ Exponential ops u64 PowU64(u64 base, u8 exp); +u64 AlignU64Pow2(u64 x); f32 LnF32(f32 x); f32 ExpF32(f32 x); f32 PowF32(f32 a, f32 b); @@ -409,3 +410,4 @@ SoftSpring MakeSpring(f32 hertz, f32 damping_ratio, f32 dt); Mat4x4 Mat4x4FromXform(Xform xf); Mat4x4 Mat4x4FromOrtho(f32 left, f32 right, f32 bottom, f32 top, f32 near_z, f32 far_z); Mat4x4 MulMat4x4(Mat4x4 m1, Mat4x4 m2); +Mat4x4 ProjectMat4x4View(Xform view, f32 viewport_width, f32 viewport_height); diff --git a/src/gpu/gpu_core.h b/src/gpu/gpu_core.h index 24da4936..b42bef61 100644 --- a/src/gpu/gpu_core.h +++ b/src/gpu/gpu_core.h @@ -120,7 +120,7 @@ void GPU_Startup(void); * the caller to make sure the released resources aren't then referenced in * any runs */ -void GPU_ReleaseResource(GPU_Resource *resource); +void GPU_ReleaseResourceFenced(GPU_Resource *resource); //////////////////////////////// //~ Texture operations diff --git a/src/gpu/gpu_dx12.c b/src/gpu/gpu_dx12.c index 0904156e..7ee79559 100644 --- a/src/gpu/gpu_dx12.c +++ b/src/gpu/gpu_dx12.c @@ -59,22 +59,22 @@ void GPU_Startup(void) /* Initialize dx12 */ /* TODO: Parallelize phases */ - dx12_init_device(); - dx12_init_objects(); - dx12_init_pipelines(); - dx12_init_noise(); + GPU_D12_InitDevice(); + GPU_D12_InitObjects(); + GPU_D12_Pipelines(); + GPU_D12_InitNoise(); /* Register callbacks */ #if RESOURCE_RELOADING - W_RegisterCallback(pipeline_watch_callback); + W_RegisterCallback(GPU_D12_WatchPipelineCallback); #endif - P_OnExit(gp_shutdown); + P_OnExit(GPU_D12_Shutdown); /* Start evictor job */ - P_Run(1, dx12_evictor_job, 0, P_Pool_Background, P_Priority_Low, &g->evictor_job_counter); + P_Run(1, GPU_D12_EvictorJob, 0, P_Pool_Background, P_Priority_Low, &g->evictor_job_counter); } -P_ExitFuncDef(gp_shutdown) +P_ExitFuncDef(GPU_D12_Shutdown) { __prof; GPU_D12_SharedState *g = &GPU_D12_shared_state; @@ -88,7 +88,7 @@ P_ExitFuncDef(gp_shutdown) } ID3D12Device_Release(g->device); #else - (UNUSED)command_queue_release; + (UNUSED)GPU_D12_ReleaseCommandQueue; #endif { @@ -104,7 +104,7 @@ P_ExitFuncDef(gp_shutdown) * Dx12 device initialization * ========================== */ -void dx12_init_error(String error) +void GPU_D12_PushInitError(String error) { TempArena scratch = BeginScratchNoConflict(); String msg = StringFormat(scratch.arena, Lit("Failed to initialize DirectX 12.\n\n%F"), FmtString(error)); @@ -112,7 +112,7 @@ void dx12_init_error(String error) EndScratch(scratch); } -void dx12_init_device(void) +void GPU_D12_InitDevice(void) { __prof; GPU_D12_SharedState *g = &GPU_D12_shared_state; @@ -128,14 +128,14 @@ void dx12_init_device(void) hr = D3D12GetDebugInterface(&IID_ID3D12Debug, (void **)&debug_controller0); if (FAILED(hr)) { - dx12_init_error(Lit("Failed to create ID3D12Debug0")); + GPU_D12_PushInitError(Lit("Failed to create ID3D12Debug0")); } ID3D12Debug1 *debug_controller1 = 0; hr = ID3D12Debug_QueryInterface(debug_controller0, &IID_ID3D12Debug1, (void **)&debug_controller1); if (FAILED(hr)) { - dx12_init_error(Lit("Failed to create ID3D12Debug1")); + GPU_D12_PushInitError(Lit("Failed to create ID3D12Debug1")); } ID3D12Debug_EnableDebugLayer(debug_controller0); @@ -155,7 +155,7 @@ void dx12_init_device(void) hr = CreateDXGIFactory2(dxgi_factory_flags, &IID_IDXGIFactory6, (void **)&g->factory); if (FAILED(hr)) { - dx12_init_error(Lit("Failed to initialize DXGI factory")); + GPU_D12_PushInitError(Lit("Failed to initialize DXGI factory")); } } @@ -207,7 +207,7 @@ void dx12_init_device(void) String fmt = Lit("Could not initialize device '%F' with D3D_FEATURE_LEVEL_12_0. Ensure that the device is capable and drivers are up to date."); error = StringFormat(scratch.arena, fmt, FmtString(first_gpu_name)); } - dx12_init_error(error); + GPU_D12_PushInitError(error); } g->adapter = adapter; g->device = device; @@ -221,7 +221,7 @@ void dx12_init_device(void) hr = ID3D12Device_QueryInterface(g->device, &IID_ID3D12InfoQueue, (void **)&info); if (FAILED(hr)) { - dx12_init_error(Lit("Failed to query ID3D12Device interface")); + GPU_D12_PushInitError(Lit("Failed to query ID3D12Device interface")); } ID3D12InfoQueue_SetBreakOnSeverity(info, D3D12_MESSAGE_SEVERITY_CORRUPTION, 1); ID3D12InfoQueue_SetBreakOnSeverity(info, D3D12_MESSAGE_SEVERITY_ERROR, 1); @@ -235,7 +235,7 @@ void dx12_init_device(void) hr = DXGIGetDebugInterface1(0, &IID_IDXGIInfoQueue, (void **)&dxgi_info); if (FAILED(hr)) { - dx12_init_error(Lit("Failed to get DXGI debug interface")); + GPU_D12_PushInitError(Lit("Failed to get DXGI debug interface")); } IDXGIInfoQueue_SetBreakOnSeverity(dxgi_info, DXGI_DEBUG_ALL, DXGI_INFO_QUEUE_MESSAGE_SEVERITY_CORRUPTION, 1); IDXGIInfoQueue_SetBreakOnSeverity(dxgi_info, DXGI_DEBUG_ALL, DXGI_INFO_QUEUE_MESSAGE_SEVERITY_ERROR, 1); @@ -294,7 +294,7 @@ void dx12_init_device(void) * Dx12 object initialization * ========================== */ -void dx12_init_objects(void) +void GPU_D12_InitObjects(void) { __prof; GPU_D12_SharedState *g = &GPU_D12_shared_state; @@ -310,8 +310,8 @@ void dx12_init_objects(void) g->desc_counts[D3D12_DESCRIPTOR_HEAP_TYPE_RTV] = DX12_NUM_RTV_DESCRIPTORS; /* Create global descriptor heaps */ - g->cbv_srv_uav_heap = cpu_descriptor_heap_alloc(D3D12_DESCRIPTOR_HEAP_TYPE_CBV_SRV_UAV); - g->rtv_heap = cpu_descriptor_heap_alloc(D3D12_DESCRIPTOR_HEAP_TYPE_RTV); + g->cbv_srv_uav_heap = GPU_D12_AllocCpuDescriptorHeap(D3D12_DESCRIPTOR_HEAP_TYPE_CBV_SRV_UAV); + g->rtv_heap = GPU_D12_AllocCpuDescriptorHeap(D3D12_DESCRIPTOR_HEAP_TYPE_RTV); /* Create command queues */ { @@ -327,7 +327,7 @@ void dx12_init_objects(void) sig.cqs_out = g->command_queues; { P_Counter counter = ZI; - P_Run(DX12_NUM_QUEUES, command_queue_alloc_job, &sig, P_Pool_Inherit, P_Priority_Inherit, &counter); + P_Run(DX12_NUM_QUEUES, GPU_D12_AllocCommandQueueJob, &sig, P_Pool_Inherit, P_Priority_Inherit, &counter); P_WaitOnCounter(&counter); } #if ProfilingIsEnabled @@ -350,9 +350,9 @@ void dx12_init_objects(void) * Dx12 pipeline initialization * ========================== */ -void pipeline_register(u64 num_pipelines, GPU_D12_Pipeline **pipelines); +void GPU_D12_RegisterPipeline(u64 num_pipelines, GPU_D12_Pipeline **pipelines); -void dx12_init_pipelines(void) +void GPU_D12_Pipelines(void) { __prof; GPU_D12_SharedState *g = &GPU_D12_shared_state; @@ -424,7 +424,7 @@ void dx12_init_pipelines(void) sig.descs_in = descs; sig.pipelines_out = pipelines; P_Counter counter = ZI; - P_Run(num_pipelines, pipeline_alloc_job, &sig, P_Pool_Inherit, P_Priority_Inherit, &counter); + P_Run(num_pipelines, GPU_D12_AllocPipelineJob, &sig, P_Pool_Inherit, P_Priority_Inherit, &counter); P_WaitOnCounter(&counter); } for (u32 i = 0; i < num_pipelines; ++i) @@ -447,7 +447,7 @@ void dx12_init_pipelines(void) P_MessageBox(P_MessageBoxKind_Warning, msg); } } - pipeline_register(num_pipelines, pipelines); + GPU_D12_RegisterPipeline(num_pipelines, pipelines); EndScratch(scratch); } @@ -456,7 +456,7 @@ void dx12_init_pipelines(void) * Noise texture initialization * ========================== */ -void dx12_init_noise(void) +void GPU_D12_InitNoise(void) { GPU_D12_SharedState *g = &GPU_D12_shared_state; TempArena scratch = BeginScratchNoConflict(); @@ -497,8 +497,8 @@ void dx12_init_noise(void) desc.SampleDesc.Count = 1; desc.SampleDesc.Quality = 0; - GPU_D12_Resource *r = dx12_resource_alloc(heap_props, heap_flags, desc, D3D12_RESOURCE_STATE_COPY_DEST); - r->srv_descriptor = descriptor_alloc(g->cbv_srv_uav_heap); + GPU_D12_Resource *r = GPU_D12_AllocResource(heap_props, heap_flags, desc, D3D12_RESOURCE_STATE_COPY_DEST); + r->srv_descriptor = GPU_D12_AllocDescriptor(g->cbv_srv_uav_heap); ID3D12Device_CreateShaderResourceView(g->device, r->resource, 0, r->srv_descriptor->handle); /* Upload texture */ @@ -507,7 +507,7 @@ void dx12_init_noise(void) GPU_D12_UploadJobSig sig = ZI; sig.resource = r; sig.data = data.text; - P_Run(1, dx12_upload_job, &sig, P_Pool_Inherit, P_Priority_Inherit, &counter); + P_Run(1, GPU_D12_UploadJob, &sig, P_Pool_Inherit, P_Priority_Inherit, &counter); P_WaitOnCounter(&counter); } } @@ -528,7 +528,7 @@ void dx12_init_noise(void) #if RESOURCE_RELOADING -P_JobDef(shader_compile_job, job) +P_JobDef(GPU_D12_CompileShaderJob, job) { __prof; GPU_D12_CompileShaderJobSig *sig = job.sig; @@ -579,7 +579,7 @@ P_JobDef(shader_compile_job, job) * Pipeline * ========================== */ -P_JobDef(pipeline_alloc_job, job) +P_JobDef(GPU_D12_AllocPipelineJob, job) { __prof; GPU_D12_SharedState *g = &GPU_D12_shared_state; @@ -907,7 +907,7 @@ P_JobDef(pipeline_alloc_job, job) EndScratch(scratch); } -void pipeline_release_now(GPU_D12_Pipeline *pipeline) +void GPU_D12_ReleasePipelineNow(GPU_D12_Pipeline *pipeline) { __prof; GPU_D12_SharedState *g = &GPU_D12_shared_state; @@ -927,7 +927,7 @@ void pipeline_release_now(GPU_D12_Pipeline *pipeline) * Pipeline cache * ========================== */ -GPU_D12_PipelineScope *pipeline_scope_begin(void) +GPU_D12_PipelineScope *GPU_D12_BeginPipelineScope(void) { __prof; GPU_D12_SharedState *g = &GPU_D12_shared_state; @@ -957,7 +957,7 @@ GPU_D12_PipelineScope *pipeline_scope_begin(void) return scope; } -void pipeline_scope_end(GPU_D12_PipelineScope *scope) +void GPU_D12_EndPipelineScope(GPU_D12_PipelineScope *scope) { __prof; GPU_D12_SharedState *g = &GPU_D12_shared_state; @@ -968,7 +968,7 @@ void pipeline_scope_end(GPU_D12_PipelineScope *scope) GPU_D12_Pipeline *pipeline = (GPU_D12_Pipeline *)entry->value; if (--pipeline->refcount <= 0) { - fenced_release(pipeline, GPU_D12_FencedReleaseKind_Pipeline); + GPU_D12_ReleaseDataFenced(pipeline, GPU_D12_FencedReleaseKind_Pipeline); } } scope->next_free = g->first_free_pipeline_scope; @@ -977,12 +977,12 @@ void pipeline_scope_end(GPU_D12_PipelineScope *scope) P_Unlock(&lock); } -Readonly GPU_D12_Pipeline g_nil_pipeline = ZI; -GPU_D12_Pipeline *pipeline_from_name(GPU_D12_PipelineScope *scope, String name) +Readonly GPU_D12_Pipeline GPU_D12_nil_pipeline = ZI; +GPU_D12_Pipeline *GPU_D12_PipelineFromName(GPU_D12_PipelineScope *scope, String name) { __prof; GPU_D12_SharedState *g = &GPU_D12_shared_state; - GPU_D12_Pipeline *result = &g_nil_pipeline; + GPU_D12_Pipeline *result = &GPU_D12_nil_pipeline; u64 hash = HashFnv64(Fnv64Basis, name); GPU_D12_Pipeline *tmp = (GPU_D12_Pipeline *)DictValueFromHash(scope->refs, hash); @@ -1011,7 +1011,7 @@ GPU_D12_Pipeline *pipeline_from_name(GPU_D12_PipelineScope *scope, String name) return result; } -void pipeline_register(u64 num_pipelines, GPU_D12_Pipeline **pipelines) +void GPU_D12_RegisterPipeline(u64 num_pipelines, GPU_D12_Pipeline **pipelines) { __prof; GPU_D12_SharedState *g = &GPU_D12_shared_state; @@ -1026,7 +1026,7 @@ void pipeline_register(u64 num_pipelines, GPU_D12_Pipeline **pipelines) GPU_D12_Pipeline *old_pipeline = (GPU_D12_Pipeline *)DictValueFromHash(g->top_pipelines, hash); if (old_pipeline && --old_pipeline->refcount <= 0) { - fenced_release(old_pipeline, GPU_D12_FencedReleaseKind_Pipeline); + GPU_D12_ReleaseDataFenced(old_pipeline, GPU_D12_FencedReleaseKind_Pipeline); } SetDictValue(g->pipelines_arena, g->top_pipelines, hash, (u64)pipeline); ++pipeline->refcount; @@ -1037,7 +1037,7 @@ void pipeline_register(u64 num_pipelines, GPU_D12_Pipeline **pipelines) GPU_D12_Pipeline *old_pipeline = (GPU_D12_Pipeline *)DictValueFromHash(g->top_successful_pipelines, hash); if (old_pipeline && --old_pipeline->refcount <= 0) { - fenced_release(old_pipeline, GPU_D12_FencedReleaseKind_Pipeline); + GPU_D12_ReleaseDataFenced(old_pipeline, GPU_D12_FencedReleaseKind_Pipeline); } SetDictValue(g->pipelines_arena, g->top_successful_pipelines, hash, (u64)pipeline); ++pipeline->refcount; @@ -1048,7 +1048,7 @@ void pipeline_register(u64 num_pipelines, GPU_D12_Pipeline **pipelines) } #if RESOURCE_RELOADING -W_CallbackFuncDef(pipeline_watch_callback, name) +W_CallbackFuncDef(GPU_D12_WatchPipelineCallback, name) { __prof; GPU_D12_SharedState *g = &GPU_D12_shared_state; @@ -1119,7 +1119,7 @@ W_CallbackFuncDef(pipeline_watch_callback, name) } { P_Counter counter = ZI; - P_Run(num_shaders, shader_compile_job, &sig, P_Pool_Inherit, P_Priority_Inherit, &counter); + P_Run(num_shaders, GPU_D12_CompileShaderJob, &sig, P_Pool_Inherit, P_Priority_Inherit, &counter); P_WaitOnCounter(&counter); } } @@ -1183,7 +1183,7 @@ W_CallbackFuncDef(pipeline_watch_callback, name) sig.descs_in = pipeline_descs; sig.pipelines_out = pipelines; P_Counter counter = ZI; - P_Run(num_pipelines, pipeline_alloc_job, &sig, P_Pool_Inherit, P_Priority_Inherit, &counter); + P_Run(num_pipelines, GPU_D12_AllocPipelineJob, &sig, P_Pool_Inherit, P_Priority_Inherit, &counter); P_WaitOnCounter(&counter); } { @@ -1220,7 +1220,7 @@ W_CallbackFuncDef(pipeline_watch_callback, name) } P_Unlock(&lock); } - pipeline_register(num_pipelines, pipelines); + GPU_D12_RegisterPipeline(num_pipelines, pipelines); } } @@ -1232,7 +1232,7 @@ W_CallbackFuncDef(pipeline_watch_callback, name) * Descriptor * ========================== */ -GPU_D12_Descriptor *descriptor_alloc(GPU_D12_CpuDescriptorHeap *dh) +GPU_D12_Descriptor *GPU_D12_AllocDescriptor(GPU_D12_CpuDescriptorHeap *dh) { __prof; GPU_D12_Descriptor *d = 0; @@ -1266,7 +1266,7 @@ GPU_D12_Descriptor *descriptor_alloc(GPU_D12_CpuDescriptorHeap *dh) return d; } -void descriptor_release(GPU_D12_Descriptor *descriptor) +void GPU_D12_ReleaseDescriptor(GPU_D12_Descriptor *descriptor) { GPU_D12_CpuDescriptorHeap *dh = descriptor->heap; P_Lock lock = P_LockE(&dh->mutex); @@ -1281,7 +1281,7 @@ void descriptor_release(GPU_D12_Descriptor *descriptor) * CPU descriptor heap * ========================== */ -GPU_D12_CpuDescriptorHeap *cpu_descriptor_heap_alloc(enum D3D12_DESCRIPTOR_HEAP_TYPE type) +GPU_D12_CpuDescriptorHeap *GPU_D12_AllocCpuDescriptorHeap(enum D3D12_DESCRIPTOR_HEAP_TYPE type) { __prof; GPU_D12_SharedState *g = &GPU_D12_shared_state; @@ -1331,7 +1331,7 @@ void cpu_descriptor_heap_release(GPU_D12_CpuDescriptorHeap *dh) * Fenced release * ========================== */ -void fenced_release(void *data, GPU_D12_FencedReleaseKind kind) +void GPU_D12_ReleaseDataFenced(void *data, GPU_D12_FencedReleaseKind kind) { GPU_D12_SharedState *g = &GPU_D12_shared_state; GPU_D12_FencedReleaseData fr = ZI; @@ -1376,7 +1376,7 @@ void fenced_release(void *data, GPU_D12_FencedReleaseKind kind) * Resource * ========================== */ -GPU_D12_Resource *dx12_resource_alloc(D3D12_HEAP_PROPERTIES heap_props, D3D12_HEAP_FLAGS heap_flags, D3D12_RESOURCE_DESC desc, D3D12_RESOURCE_STATES initial_state) +GPU_D12_Resource *GPU_D12_AllocResource(D3D12_HEAP_PROPERTIES heap_props, D3D12_HEAP_FLAGS heap_flags, D3D12_RESOURCE_DESC desc, D3D12_RESOURCE_STATES initial_state) { __prof; GPU_D12_SharedState *g = &GPU_D12_shared_state; @@ -1415,7 +1415,7 @@ GPU_D12_Resource *dx12_resource_alloc(D3D12_HEAP_PROPERTIES heap_props, D3D12_HE return r; } -void dx12_resource_release_now(GPU_D12_Resource *t) +void GPU_D12_ReleaseResourceNow(GPU_D12_Resource *t) { __prof; GPU_D12_SharedState *g = &GPU_D12_shared_state; @@ -1424,19 +1424,19 @@ void dx12_resource_release_now(GPU_D12_Resource *t) /* TODO: Batch lock heaps */ if (t->cbv_descriptor) { - descriptor_release(t->cbv_descriptor); + GPU_D12_ReleaseDescriptor(t->cbv_descriptor); } if (t->srv_descriptor) { - descriptor_release(t->srv_descriptor); + GPU_D12_ReleaseDescriptor(t->srv_descriptor); } if (t->uav_descriptor) { - descriptor_release(t->uav_descriptor); + GPU_D12_ReleaseDescriptor(t->uav_descriptor); } if (t->rtv_descriptor) { - descriptor_release(t->rtv_descriptor); + GPU_D12_ReleaseDescriptor(t->rtv_descriptor); } /* Release resource */ @@ -1449,17 +1449,17 @@ void dx12_resource_release_now(GPU_D12_Resource *t) P_Unlock(&lock); } -void GPU_ReleaseResource(GPU_Resource *resource) +void GPU_ReleaseResourceFenced(GPU_Resource *resource) { GPU_D12_Resource *r = (GPU_D12_Resource *)resource; - fenced_release(r, GPU_D12_FencedReleaseKind_Resource); + GPU_D12_ReleaseDataFenced(r, GPU_D12_FencedReleaseKind_Resource); } /* ========================== * * Resource barrier * ========================== */ -void dx12_resource_barriers(ID3D12GraphicsCommandList *cl, i32 num_descs, struct dx12_resource_barrier_desc *descs) +void GPU_D12_InsertBarrier(ID3D12GraphicsCommandList *cl, i32 num_descs, GPU_D12_ResourceBarrierDesc *descs) { __prof; TempArena scratch = BeginScratchNoConflict(); @@ -1468,7 +1468,7 @@ void dx12_resource_barriers(ID3D12GraphicsCommandList *cl, i32 num_descs, struct struct D3D12_RESOURCE_BARRIER *rbs = PushStructsNoZero(scratch.arena, struct D3D12_RESOURCE_BARRIER, num_descs); for (i32 i = 0; i < num_descs; ++i) { - struct dx12_resource_barrier_desc *desc = &descs[i]; + GPU_D12_ResourceBarrierDesc *desc = &descs[i]; GPU_D12_Resource *resource = desc->resource; enum D3D12_RESOURCE_BARRIER_TYPE type = desc->type; if (type == D3D12_RESOURCE_BARRIER_TYPE_TRANSITION) @@ -1515,9 +1515,9 @@ void dx12_resource_barriers(ID3D12GraphicsCommandList *cl, i32 num_descs, struct * Command queue * ========================== */ -GPU_D12_CommandListPool *command_list_pool_alloc(GPU_D12_CommandQueue *cq); +GPU_D12_CommandListPool *GPU_D12_AllocCommandListPool(GPU_D12_CommandQueue *cq); -P_JobDef(command_queue_alloc_job, job) +P_JobDef(GPU_D12_AllocCommandQueueJob, job) { __prof; GPU_D12_SharedState *g = &GPU_D12_shared_state; @@ -1548,13 +1548,13 @@ P_JobDef(command_queue_alloc_job, job) P_Panic(Lit("Failed to create command queue fence")); } - cq->cl_pool = command_list_pool_alloc(cq); + cq->cl_pool = GPU_D12_AllocCommandListPool(cq); sig->cqs_out[job.id] = cq; } } -void command_queue_release(GPU_D12_CommandQueue *cq) +void GPU_D12_ReleaseCommandQueue(GPU_D12_CommandQueue *cq) { __prof; /* TODO */ @@ -1566,7 +1566,7 @@ void command_queue_release(GPU_D12_CommandQueue *cq) * Command list * ========================== */ -GPU_D12_CommandListPool *command_list_pool_alloc(GPU_D12_CommandQueue *cq) +GPU_D12_CommandListPool *GPU_D12_AllocCommandListPool(GPU_D12_CommandQueue *cq) { GPU_D12_CommandListPool *pool = 0; { @@ -1578,7 +1578,7 @@ GPU_D12_CommandListPool *command_list_pool_alloc(GPU_D12_CommandQueue *cq) return pool; } -GPU_D12_CommandList *command_list_open(GPU_D12_CommandListPool *pool) +GPU_D12_CommandList *GPU_D12_BeginCommandList(GPU_D12_CommandListPool *pool) { __prof; GPU_D12_SharedState *g = &GPU_D12_shared_state; @@ -1678,7 +1678,7 @@ GPU_D12_CommandList *command_list_open(GPU_D12_CommandListPool *pool) } /* TODO: Allow multiple command list submissions */ -u64 command_list_close(GPU_D12_CommandList *cl) +u64 GPU_D12_EndCommandList(GPU_D12_CommandList *cl) { __prof; GPU_D12_SharedState *g = &GPU_D12_shared_state; @@ -1776,7 +1776,7 @@ u64 command_list_close(GPU_D12_CommandList *cl) * Command descriptor heap (GPU / shader visible descriptor heap) * ========================== */ -GPU_D12_CommandDescriptorHeap *command_list_push_descriptor_heap(GPU_D12_CommandList *cl, GPU_D12_CpuDescriptorHeap *dh_cpu) +GPU_D12_CommandDescriptorHeap *GPU_D12_PushDescriptorHeap(GPU_D12_CommandList *cl, GPU_D12_CpuDescriptorHeap *dh_cpu) { __prof; GPU_D12_SharedState *g = &GPU_D12_shared_state; @@ -1873,31 +1873,14 @@ GPU_D12_CommandDescriptorHeap *command_list_push_descriptor_heap(GPU_D12_Command * Command buffer * ========================== */ -u64 command_buffer_hash_from_size(u64 size) +u64 GPU_D12_CommandBufferHashFromSize(u64 size) { u64 hash = RandU64FromSeed(size); return hash; } -u64 align_up_pow2(u64 v) -{ - u64 result = 0; - if (v > 0) - { - result = v - 1; - result |= result >> 1; - result |= result >> 2; - result |= result >> 4; - result |= result >> 8; - result |= result >> 16; - result |= result >> 32; - ++result; - } - return result; -} - -#define command_list_push_buffer(cl, count, elems) _command_list_push_buffer((cl), count * ((elems) ? sizeof(*(elems)) : 0), (elems), (elems) ? sizeof(*(elems)) : 1) -GPU_D12_CommandBuffer *_command_list_push_buffer(GPU_D12_CommandList *cl, u64 data_len, void *data, u64 data_stride) +#define GPU_D12_PushCommandBuffer(cl, count, elems) GPU_D12__PushCommandBuffer((cl), count * ((elems) ? sizeof(*(elems)) : 0), (elems), (elems) ? sizeof(*(elems)) : 1) +GPU_D12_CommandBuffer *GPU_D12__PushCommandBuffer(GPU_D12_CommandList *cl, u64 data_len, void *data, u64 data_stride) { __prof; GPU_D12_SharedState *g = &GPU_D12_shared_state; @@ -1906,7 +1889,7 @@ GPU_D12_CommandBuffer *_command_list_push_buffer(GPU_D12_CommandList *cl, u64 da Assert(data_len % data_stride == 0); /* Determine size */ - u64 size = MaxU64(DX12_COMMAND_BUFFER_MIN_SIZE, align_up_pow2(data_len)); + u64 size = MaxU64(DX12_COMMAND_BUFFER_MIN_SIZE, AlignU64Pow2(data_len)); /* Allocate buffer */ GPU_D12_CommandBufferGroup *cb_group = 0; @@ -1916,7 +1899,7 @@ GPU_D12_CommandBuffer *_command_list_push_buffer(GPU_D12_CommandList *cl, u64 da P_Lock lock = P_LockE(&g->command_buffers_mutex); { - u64 group_hash = command_buffer_hash_from_size(size); + u64 group_hash = GPU_D12_CommandBufferHashFromSize(size); DictEntry *cb_group_entry = EnsureDictEntry(g->command_buffers_arena, g->command_buffers_dict, group_hash); cb_group = (GPU_D12_CommandBufferGroup *)cb_group_entry->value; if (!cb_group) @@ -1994,8 +1977,8 @@ GPU_D12_CommandBuffer *_command_list_push_buffer(GPU_D12_CommandList *cl, u64 da desc.SampleDesc.Quality = 0; D3D12_RESOURCE_STATES initial_state = D3D12_RESOURCE_STATE_GENERIC_READ; - r = dx12_resource_alloc(heap_props, heap_flags, desc, initial_state); - r->srv_descriptor = descriptor_alloc(g->cbv_srv_uav_heap); + r = GPU_D12_AllocResource(heap_props, heap_flags, desc, initial_state); + r->srv_descriptor = GPU_D12_AllocDescriptor(g->cbv_srv_uav_heap); } cb->resource = r; @@ -2036,10 +2019,10 @@ GPU_D12_CommandBuffer *_command_list_push_buffer(GPU_D12_CommandList *cl, u64 da * Wait job * ========================== */ -P_JobDef(dx12_wait_fence_job, job) +P_JobDef(GPU_D12_WaitOnFenceJob, job) { __prof; - struct dx12_wait_fence_job_sig *sig = job.sig; + GPU_D12_WaitOnFenceJobSig *sig = job.sig; ID3D12Fence *fence = sig->fence; u64 target = sig->target; if (ID3D12Fence_GetCompletedValue(fence) < target) @@ -2101,15 +2084,15 @@ GPU_Resource *GPU_AllocTexture(GPU_TextureFormat format, u32 flags, Vec2I32 size D3D12_RESOURCE_STATES initial_state = D3D12_RESOURCE_STATE_COPY_DEST; - GPU_D12_Resource *r = dx12_resource_alloc(heap_props, heap_flags, desc, initial_state); + GPU_D12_Resource *r = GPU_D12_AllocResource(heap_props, heap_flags, desc, initial_state); r->texture_size = size; - r->srv_descriptor = descriptor_alloc(g->cbv_srv_uav_heap); + r->srv_descriptor = GPU_D12_AllocDescriptor(g->cbv_srv_uav_heap); ID3D12Device_CreateShaderResourceView(g->device, r->resource, 0, r->srv_descriptor->handle); if (flags & GP_TEXTURE_FLAG_TARGETABLE) { desc.Flags |= D3D12_RESOURCE_FLAG_ALLOW_RENDER_TARGET | D3D12_RESOURCE_FLAG_ALLOW_UNORDERED_ACCESS; - r->uav_descriptor = descriptor_alloc(g->cbv_srv_uav_heap); - r->rtv_descriptor = descriptor_alloc(g->rtv_heap); + r->uav_descriptor = GPU_D12_AllocDescriptor(g->cbv_srv_uav_heap); + r->rtv_descriptor = GPU_D12_AllocDescriptor(g->rtv_heap); ID3D12Device_CreateUnorderedAccessView(g->device, r->resource, 0, 0, r->uav_descriptor->handle); ID3D12Device_CreateRenderTargetView(g->device, r->resource, 0, r->rtv_descriptor->handle); } @@ -2122,7 +2105,7 @@ GPU_Resource *GPU_AllocTexture(GPU_TextureFormat format, u32 flags, Vec2I32 size GPU_D12_UploadJobSig sig = ZI; sig.resource = r; sig.data = initial_data; - P_Run(1, dx12_upload_job, &sig, P_Pool_Inherit, P_Priority_Inherit, &counter); + P_Run(1, GPU_D12_UploadJob, &sig, P_Pool_Inherit, P_Priority_Inherit, &counter); P_WaitOnCounter(&counter); } @@ -2139,7 +2122,7 @@ Vec2I32 GPU_GetTextureSize(GPU_Resource *resource) * Upload * ========================== */ -P_JobDef(dx12_upload_job, job) +P_JobDef(GPU_D12_UploadJob, job) { GPU_D12_SharedState *g = &GPU_D12_shared_state; GPU_D12_UploadJobSig *sig = job.sig; @@ -2181,11 +2164,11 @@ P_JobDef(dx12_upload_job, job) upload_desc.SampleDesc.Quality = 0; D3D12_RESOURCE_STATES upload_initial_state = D3D12_RESOURCE_STATE_GENERIC_READ; - upload = dx12_resource_alloc(upload_heap_props, upload_heap_flags, upload_desc, upload_initial_state); + upload = GPU_D12_AllocResource(upload_heap_props, upload_heap_flags, upload_desc, upload_initial_state); } GPU_D12_CommandQueue *cq = g->command_queues[DX12_QUEUE_COPY_BACKGROUND]; - GPU_D12_CommandList *cl = command_list_open(cq->cl_pool); + GPU_D12_CommandList *cl = GPU_D12_BeginCommandList(cq->cl_pool); { /* Copyto upload heap */ { @@ -2230,21 +2213,21 @@ P_JobDef(dx12_upload_job, job) ID3D12GraphicsCommandList_CopyTextureRegion(cl->cl, &dst_loc, 0, 0, 0, &src_loc, 0); } - } u64 fence_target = command_list_close(cl); + } u64 fence_target = GPU_D12_EndCommandList(cl); /* Wait on fence so we know it's safe to release upload heap */ if (ID3D12Fence_GetCompletedValue(cq->submit_fence) < fence_target) { - struct dx12_wait_fence_job_sig wait_sig = ZI; + GPU_D12_WaitOnFenceJobSig wait_sig = ZI; wait_sig.fence = cq->submit_fence; wait_sig.target = fence_target; P_Counter counter = ZI; - P_Run(1, dx12_wait_fence_job, &wait_sig, P_Pool_Floating, P_Priority_Low, &counter); + P_Run(1, GPU_D12_WaitOnFenceJob, &wait_sig, P_Pool_Floating, P_Priority_Low, &counter); P_WaitOnCounter(&counter); } /* Release upload heap now */ - dx12_resource_release_now(upload); + GPU_D12_ReleaseResourceNow(upload); } } @@ -2252,7 +2235,7 @@ P_JobDef(dx12_upload_job, job) * Run utils * ========================== */ -void command_list_set_pipeline(GPU_D12_CommandList *cl, GPU_D12_Pipeline *pipeline) +void GPU_D12_SetPipeline(GPU_D12_CommandList *cl, GPU_D12_Pipeline *pipeline) { ID3D12GraphicsCommandList_SetPipelineState(cl->cl, pipeline->pso); if (pipeline->is_gfx) @@ -2266,7 +2249,7 @@ void command_list_set_pipeline(GPU_D12_CommandList *cl, GPU_D12_Pipeline *pipeli cl->cur_pipeline = pipeline; } -void command_list_set_sig(GPU_D12_CommandList *cl, void *src, u32 size) +void GPU_D12_SetSig(GPU_D12_CommandList *cl, void *src, u32 size) { __prof; Assert(size % 16 == 0); /* Root constant structs must pad to 16 bytes */ @@ -2288,7 +2271,7 @@ void command_list_set_sig(GPU_D12_CommandList *cl, void *src, u32 size) } } -struct D3D12_VIEWPORT viewport_from_rect(Rect r) +struct D3D12_VIEWPORT GPU_D12_ViewportFromRect(Rect r) { struct D3D12_VIEWPORT viewport = ZI; viewport.TopLeftX = r.x; @@ -2300,7 +2283,7 @@ struct D3D12_VIEWPORT viewport_from_rect(Rect r) return viewport; } -D3D12_RECT scissor_from_rect(Rect r) +D3D12_RECT GPU_D12_ScissorRectFromRect(Rect r) { D3D12_RECT scissor = ZI; scissor.left = r.x; @@ -2310,7 +2293,7 @@ D3D12_RECT scissor_from_rect(Rect r) return scissor; } -D3D12_VERTEX_BUFFER_VIEW vbv_from_command_buffer(GPU_D12_CommandBuffer *cb, u32 vertex_size) +D3D12_VERTEX_BUFFER_VIEW GPU_D12_VbvFromCommandBuffer(GPU_D12_CommandBuffer *cb, u32 vertex_size) { D3D12_VERTEX_BUFFER_VIEW vbv = ZI; vbv.BufferLocation = cb->resource->gpu_address; @@ -2319,7 +2302,7 @@ D3D12_VERTEX_BUFFER_VIEW vbv_from_command_buffer(GPU_D12_CommandBuffer *cb, u32 return vbv; } -D3D12_INDEX_BUFFER_VIEW ibv_from_command_buffer(GPU_D12_CommandBuffer *cb, DXGI_FORMAT format) +D3D12_INDEX_BUFFER_VIEW GPU_D12_IbvFromCommandBuffer(GPU_D12_CommandBuffer *cb, DXGI_FORMAT format) { D3D12_INDEX_BUFFER_VIEW ibv = ZI; ibv.BufferLocation = cb->resource->gpu_address; @@ -2328,7 +2311,7 @@ D3D12_INDEX_BUFFER_VIEW ibv_from_command_buffer(GPU_D12_CommandBuffer *cb, DXGI_ return ibv; } -GPU_D12_Resource *gbuff_alloc(DXGI_FORMAT format, Vec2I32 size, D3D12_RESOURCE_STATES initial_state) +GPU_D12_Resource *GPU_D12_AllocGbuff(DXGI_FORMAT format, Vec2I32 size, D3D12_RESOURCE_STATES initial_state) { __prof; GPU_D12_SharedState *g = &GPU_D12_shared_state; @@ -2351,10 +2334,10 @@ GPU_D12_Resource *gbuff_alloc(DXGI_FORMAT format, Vec2I32 size, D3D12_RESOURCE_S desc.SampleDesc.Quality = 0; desc.Flags = D3D12_RESOURCE_FLAG_ALLOW_RENDER_TARGET | D3D12_RESOURCE_FLAG_ALLOW_UNORDERED_ACCESS; - GPU_D12_Resource *r = dx12_resource_alloc(heap_props, heap_flags, desc, initial_state); - r->srv_descriptor = descriptor_alloc(g->cbv_srv_uav_heap); - r->uav_descriptor = descriptor_alloc(g->cbv_srv_uav_heap); - r->rtv_descriptor = descriptor_alloc(g->rtv_heap); + GPU_D12_Resource *r = GPU_D12_AllocResource(heap_props, heap_flags, desc, initial_state); + r->srv_descriptor = GPU_D12_AllocDescriptor(g->cbv_srv_uav_heap); + r->uav_descriptor = GPU_D12_AllocDescriptor(g->cbv_srv_uav_heap); + r->rtv_descriptor = GPU_D12_AllocDescriptor(g->rtv_heap); ID3D12Device_CreateShaderResourceView(g->device, r->resource, 0, r->srv_descriptor->handle); ID3D12Device_CreateUnorderedAccessView(g->device, r->resource, 0, 0, r->uav_descriptor->handle); ID3D12Device_CreateRenderTargetView(g->device, r->resource, 0, r->rtv_descriptor->handle); @@ -2363,15 +2346,7 @@ GPU_D12_Resource *gbuff_alloc(DXGI_FORMAT format, Vec2I32 size, D3D12_RESOURCE_S return r; } -/* Calculate the view projection matrix */ -Inline Mat4x4 calculate_vp(Xform view, f32 viewport_width, f32 viewport_height) -{ - Mat4x4 projection = Mat4x4FromOrtho(0.0, viewport_width, viewport_height, 0.0, -1.0, 1.0); - Mat4x4 view4x4 = Mat4x4FromXform(view); - return MulMat4x4(projection, view4x4); -} - -D3D12_GPU_DESCRIPTOR_HANDLE gpu_handle_from_descriptor(GPU_D12_Descriptor *descriptor, GPU_D12_CommandDescriptorHeap *cdh) +D3D12_GPU_DESCRIPTOR_HANDLE GPU_D12_GpuHandleFromDescriptor(GPU_D12_Descriptor *descriptor, GPU_D12_CommandDescriptorHeap *cdh) { GPU_D12_SharedState *g = &GPU_D12_shared_state; struct D3D12_GPU_DESCRIPTOR_HANDLE result = ZI; @@ -2383,7 +2358,7 @@ D3D12_GPU_DESCRIPTOR_HANDLE gpu_handle_from_descriptor(GPU_D12_Descriptor *descr * Render sig * ========================== */ -GPU_D12_RenderSig *render_sig_alloc(void) +GPU_D12_RenderSig *GPU_D12_AllocRenderSig(void) { __prof; GPU_D12_RenderSig *sig = 0; @@ -2402,7 +2377,7 @@ GPU_D12_RenderSig *render_sig_alloc(void) return sig; } -void render_sig_reset(GPU_D12_RenderSig *sig) +void GPU_D12_ResetRenderSig(GPU_D12_RenderSig *sig) { __prof; @@ -2426,7 +2401,7 @@ void render_sig_reset(GPU_D12_RenderSig *sig) GPU_RenderSig *GPU_AllocRenderSig(void) { __prof; - GPU_D12_RenderSig *sig = render_sig_alloc(); + GPU_D12_RenderSig *sig = GPU_D12_AllocRenderSig(); return (GPU_RenderSig *)sig; } @@ -2526,64 +2501,64 @@ GPU_Resource *GPU_RunRender(GPU_RenderSig *gp_render_sig, GPU_RenderParams param if (rsig->shade_target && !EqVec2I32(render_size, rsig->shade_target->texture_size)) { __profn("Release sig resources"); - fenced_release(rsig->albedo, GPU_D12_FencedReleaseKind_Resource); - fenced_release(rsig->emittance, GPU_D12_FencedReleaseKind_Resource); - fenced_release(rsig->emittance_flood_read, GPU_D12_FencedReleaseKind_Resource); - fenced_release(rsig->emittance_flood_target, GPU_D12_FencedReleaseKind_Resource); - fenced_release(rsig->shade_read, GPU_D12_FencedReleaseKind_Resource); - fenced_release(rsig->shade_target, GPU_D12_FencedReleaseKind_Resource); + GPU_D12_ReleaseDataFenced(rsig->albedo, GPU_D12_FencedReleaseKind_Resource); + GPU_D12_ReleaseDataFenced(rsig->emittance, GPU_D12_FencedReleaseKind_Resource); + GPU_D12_ReleaseDataFenced(rsig->emittance_flood_read, GPU_D12_FencedReleaseKind_Resource); + GPU_D12_ReleaseDataFenced(rsig->emittance_flood_target, GPU_D12_FencedReleaseKind_Resource); + GPU_D12_ReleaseDataFenced(rsig->shade_read, GPU_D12_FencedReleaseKind_Resource); + GPU_D12_ReleaseDataFenced(rsig->shade_target, GPU_D12_FencedReleaseKind_Resource); rsig->shade_target = 0; } if (!rsig->shade_target) { __profn("Allocate sig resources"); - rsig->albedo = gbuff_alloc(DXGI_FORMAT_R8G8B8A8_UNORM, render_size, D3D12_RESOURCE_STATE_RENDER_TARGET); - rsig->emittance = gbuff_alloc(DXGI_FORMAT_R16G16B16A16_FLOAT, render_size, D3D12_RESOURCE_STATE_RENDER_TARGET); - rsig->emittance_flood_read = gbuff_alloc(DXGI_FORMAT_R16G16_UINT, render_size, D3D12_RESOURCE_STATE_UNORDERED_ACCESS); - rsig->emittance_flood_target = gbuff_alloc(DXGI_FORMAT_R16G16_UINT, render_size, D3D12_RESOURCE_STATE_UNORDERED_ACCESS); - rsig->shade_read = gbuff_alloc(DXGI_FORMAT_R16G16B16A16_FLOAT, render_size, D3D12_RESOURCE_STATE_UNORDERED_ACCESS); - rsig->shade_target = gbuff_alloc(DXGI_FORMAT_R16G16B16A16_FLOAT, render_size, D3D12_RESOURCE_STATE_UNORDERED_ACCESS); + rsig->albedo = GPU_D12_AllocGbuff(DXGI_FORMAT_R8G8B8A8_UNORM, render_size, D3D12_RESOURCE_STATE_RENDER_TARGET); + rsig->emittance = GPU_D12_AllocGbuff(DXGI_FORMAT_R16G16B16A16_FLOAT, render_size, D3D12_RESOURCE_STATE_RENDER_TARGET); + rsig->emittance_flood_read = GPU_D12_AllocGbuff(DXGI_FORMAT_R16G16_UINT, render_size, D3D12_RESOURCE_STATE_UNORDERED_ACCESS); + rsig->emittance_flood_target = GPU_D12_AllocGbuff(DXGI_FORMAT_R16G16_UINT, render_size, D3D12_RESOURCE_STATE_UNORDERED_ACCESS); + rsig->shade_read = GPU_D12_AllocGbuff(DXGI_FORMAT_R16G16B16A16_FLOAT, render_size, D3D12_RESOURCE_STATE_UNORDERED_ACCESS); + rsig->shade_target = GPU_D12_AllocGbuff(DXGI_FORMAT_R16G16B16A16_FLOAT, render_size, D3D12_RESOURCE_STATE_UNORDERED_ACCESS); } /* Allocate ui buffers */ if (rsig->ui_target && !EqVec2I32(ui_size, rsig->ui_target->texture_size)) { - fenced_release(rsig->ui_target, GPU_D12_FencedReleaseKind_Resource); + GPU_D12_ReleaseDataFenced(rsig->ui_target, GPU_D12_FencedReleaseKind_Resource); rsig->ui_target = 0; } if (!rsig->ui_target) { - rsig->ui_target = gbuff_alloc(DXGI_FORMAT_R8G8B8A8_UNORM, ui_size, D3D12_RESOURCE_STATE_RENDER_TARGET); + rsig->ui_target = GPU_D12_AllocGbuff(DXGI_FORMAT_R8G8B8A8_UNORM, ui_size, D3D12_RESOURCE_STATE_RENDER_TARGET); } - GPU_D12_PipelineScope *pipeline_scope = pipeline_scope_begin(); - GPU_D12_Pipeline *material_pipeline = pipeline_from_name(pipeline_scope, Lit("kernel_material")); - GPU_D12_Pipeline *flood_pipeline = pipeline_from_name(pipeline_scope, Lit("kernel_flood")); - GPU_D12_Pipeline *shade_pipeline = pipeline_from_name(pipeline_scope, Lit("kernel_shade")); - GPU_D12_Pipeline *blit_pipeline = pipeline_from_name(pipeline_scope, Lit("kernel_blit")); - GPU_D12_Pipeline *ui_pipeline = pipeline_from_name(pipeline_scope, Lit("kernel_ui")); - GPU_D12_Pipeline *shape_pipeline = pipeline_from_name(pipeline_scope, Lit("kernel_shape")); + GPU_D12_PipelineScope *pipeline_scope = GPU_D12_BeginPipelineScope(); + GPU_D12_Pipeline *material_pipeline = GPU_D12_PipelineFromName(pipeline_scope, Lit("kernel_material")); + GPU_D12_Pipeline *flood_pipeline = GPU_D12_PipelineFromName(pipeline_scope, Lit("kernel_flood")); + GPU_D12_Pipeline *shade_pipeline = GPU_D12_PipelineFromName(pipeline_scope, Lit("kernel_shade")); + GPU_D12_Pipeline *blit_pipeline = GPU_D12_PipelineFromName(pipeline_scope, Lit("kernel_blit")); + GPU_D12_Pipeline *ui_pipeline = GPU_D12_PipelineFromName(pipeline_scope, Lit("kernel_ui")); + GPU_D12_Pipeline *shape_pipeline = GPU_D12_PipelineFromName(pipeline_scope, Lit("kernel_shape")); GPU_D12_CommandQueue *cq = g->command_queues[DX12_QUEUE_DIRECT]; - GPU_D12_CommandList *cl = command_list_open(cq->cl_pool); + GPU_D12_CommandList *cl = GPU_D12_BeginCommandList(cq->cl_pool); { __profn("Run render"); __profnc_dx12(cl->cq->prof, cl->cl, "Run render", Rgb32F(0.5, 0.2, 0.2)); - Mat4x4 world_to_render_vp_matrix = calculate_vp(world_to_render_xf, render_viewport.width, render_viewport.height); - Mat4x4 ui_vp_matrix = calculate_vp(XformIdentity, ui_viewport.width, ui_viewport.height); + Mat4x4 world_to_render_vp_matrix = ProjectMat4x4View(world_to_render_xf, render_viewport.width, render_viewport.height); + Mat4x4 ui_vp_matrix = ProjectMat4x4View(XformIdentity, ui_viewport.width, ui_viewport.height); Mat4x4 blit_vp_matrix = ZI; { Xform xf = render_to_ui_xf; xf = ScaleXform(xf, VEC2(render_size.x, render_size.y)); xf = TranslateXform(xf, VEC2(0.5, 0.5)); - blit_vp_matrix = calculate_vp(xf, ui_viewport.width, ui_viewport.height); + blit_vp_matrix = ProjectMat4x4View(xf, ui_viewport.width, ui_viewport.height); } /* Upload dummmy vert & index buffer */ /* TODO: Make these static */ /* Dummy vertex buffer */ LocalPersist u16 quad_indices[6] = { 0, 1, 2, 0, 2, 3 }; - GPU_D12_CommandBuffer *dummy_vertex_buffer = command_list_push_buffer(cl, 0, (u8 *)0); - GPU_D12_CommandBuffer *quad_index_buffer = command_list_push_buffer(cl, countof(quad_indices), quad_indices); + GPU_D12_CommandBuffer *dummy_vertex_buffer = GPU_D12_PushCommandBuffer(cl, 0, (u8 *)0); + GPU_D12_CommandBuffer *quad_index_buffer = GPU_D12_PushCommandBuffer(cl, countof(quad_indices), quad_indices); /* Process sig data into uploadable data */ K_MaterialInstance *material_instances = PushStructsNoZero(scratch.arena, K_MaterialInstance, rsig->num_material_instance_descs); @@ -2647,14 +2622,14 @@ GPU_Resource *GPU_RunRender(GPU_RenderSig *gp_render_sig, GPU_RenderParams param /* Upload buffers */ u64 num_ui_shape_verts = rsig->ui_shape_verts_arena->pos / sizeof(K_ShapeVert); u64 num_ui_shape_indices = rsig->ui_shape_indices_arena->pos / sizeof(u32); - GPU_D12_CommandBuffer *material_instance_buffer = command_list_push_buffer(cl, rsig->num_material_instance_descs, material_instances); - GPU_D12_CommandBuffer *ui_rect_instance_buffer = command_list_push_buffer(cl, rsig->num_ui_rect_instance_descs, ui_rect_instances); - GPU_D12_CommandBuffer *ui_shape_verts_buffer = command_list_push_buffer(cl, num_ui_shape_verts, (K_ShapeVert *)ArenaBase(rsig->ui_shape_verts_arena)); - GPU_D12_CommandBuffer *ui_shape_indices_buffer = command_list_push_buffer(cl, num_ui_shape_indices, (u32 *)ArenaBase(rsig->ui_shape_indices_arena)); - GPU_D12_CommandBuffer *grid_buffer = command_list_push_buffer(cl, rsig->num_material_grid_descs, grids); + GPU_D12_CommandBuffer *material_instance_buffer = GPU_D12_PushCommandBuffer(cl, rsig->num_material_instance_descs, material_instances); + GPU_D12_CommandBuffer *ui_rect_instance_buffer = GPU_D12_PushCommandBuffer(cl, rsig->num_ui_rect_instance_descs, ui_rect_instances); + GPU_D12_CommandBuffer *ui_shape_verts_buffer = GPU_D12_PushCommandBuffer(cl, num_ui_shape_verts, (K_ShapeVert *)ArenaBase(rsig->ui_shape_verts_arena)); + GPU_D12_CommandBuffer *ui_shape_indices_buffer = GPU_D12_PushCommandBuffer(cl, num_ui_shape_indices, (u32 *)ArenaBase(rsig->ui_shape_indices_arena)); + GPU_D12_CommandBuffer *grid_buffer = GPU_D12_PushCommandBuffer(cl, rsig->num_material_grid_descs, grids); /* Upload descriptor heap */ - GPU_D12_CommandDescriptorHeap *descriptor_heap = command_list_push_descriptor_heap(cl, g->cbv_srv_uav_heap); + GPU_D12_CommandDescriptorHeap *descriptor_heap = GPU_D12_PushDescriptorHeap(cl, g->cbv_srv_uav_heap); ID3D12DescriptorHeap *heaps[] = { descriptor_heap->heap }; ID3D12GraphicsCommandList_SetDescriptorHeaps(cl->cl, countof(heaps), heaps); @@ -2662,7 +2637,7 @@ GPU_Resource *GPU_RunRender(GPU_RenderSig *gp_render_sig, GPU_RenderParams param { /* Barrier */ { - struct dx12_resource_barrier_desc barriers[] = { + GPU_D12_ResourceBarrierDesc barriers[] = { { D3D12_RESOURCE_BARRIER_TYPE_TRANSITION, rsig->albedo, D3D12_RESOURCE_STATE_RENDER_TARGET }, { D3D12_RESOURCE_BARRIER_TYPE_TRANSITION, rsig->emittance, D3D12_RESOURCE_STATE_RENDER_TARGET } }; @@ -2670,7 +2645,7 @@ GPU_Resource *GPU_RunRender(GPU_RenderSig *gp_render_sig, GPU_RenderParams param rsig->albedo->rtv_descriptor->handle, rsig->emittance->rtv_descriptor->handle, }; - dx12_resource_barriers(cl->cl, countof(barriers), barriers); + GPU_D12_InsertBarrier(cl->cl, countof(barriers), barriers); ID3D12GraphicsCommandList_OMSetRenderTargets(cl->cl, countof(rtvs), rtvs, 0, 0); } /* Clear */ @@ -2690,11 +2665,11 @@ GPU_Resource *GPU_RunRender(GPU_RenderSig *gp_render_sig, GPU_RenderParams param __profnc_dx12(cl->cq->prof, cl->cl, "Material pass", Rgb32F(0.5, 0.2, 0.2)); /* Bind pipeline */ - command_list_set_pipeline(cl, material_pipeline); + GPU_D12_SetPipeline(cl, material_pipeline); /* Set Rasterizer State */ - D3D12_VIEWPORT viewport = viewport_from_rect(render_viewport); - D3D12_RECT scissor = scissor_from_rect(render_viewport); + D3D12_VIEWPORT viewport = GPU_D12_ViewportFromRect(render_viewport); + D3D12_RECT scissor = GPU_D12_ScissorRectFromRect(render_viewport); ID3D12GraphicsCommandList_RSSetViewports(cl->cl, 1, &viewport); ID3D12GraphicsCommandList_RSSetScissorRects(cl->cl, 1, &scissor); @@ -2703,12 +2678,12 @@ GPU_Resource *GPU_RunRender(GPU_RenderSig *gp_render_sig, GPU_RenderParams param sig.projection = world_to_render_vp_matrix; sig.instances_urid = material_instance_buffer->resource->srv_descriptor->index; sig.grids_urid = grid_buffer->resource->srv_descriptor->index; - command_list_set_sig(cl, &sig, sizeof(sig)); + GPU_D12_SetSig(cl, &sig, sizeof(sig)); /* Draw */ u32 instance_count = material_instance_buffer->size / sizeof(K_MaterialInstance); - D3D12_VERTEX_BUFFER_VIEW vbv = vbv_from_command_buffer(dummy_vertex_buffer, 0); - D3D12_INDEX_BUFFER_VIEW ibv = ibv_from_command_buffer(quad_index_buffer, DXGI_FORMAT_R16_UINT); + D3D12_VERTEX_BUFFER_VIEW vbv = GPU_D12_VbvFromCommandBuffer(dummy_vertex_buffer, 0); + D3D12_INDEX_BUFFER_VIEW ibv = GPU_D12_IbvFromCommandBuffer(quad_index_buffer, DXGI_FORMAT_R16_UINT); ID3D12GraphicsCommandList_IASetPrimitiveTopology(cl->cl, D3D_PRIMITIVE_TOPOLOGY_TRIANGLELIST); ID3D12GraphicsCommandList_IASetVertexBuffers(cl->cl, 0, 1, &vbv); ID3D12GraphicsCommandList_IASetIndexBuffer(cl->cl, &ibv); @@ -2719,7 +2694,7 @@ GPU_Resource *GPU_RunRender(GPU_RenderSig *gp_render_sig, GPU_RenderParams param { /* Barrier */ { - struct dx12_resource_barrier_desc barriers[] = { + GPU_D12_ResourceBarrierDesc barriers[] = { { D3D12_RESOURCE_BARRIER_TYPE_TRANSITION, rsig->emittance, D3D12_RESOURCE_STATE_ALL_SHADER_RESOURCE }, { D3D12_RESOURCE_BARRIER_TYPE_TRANSITION, rsig->emittance_flood_read, D3D12_RESOURCE_STATE_UNORDERED_ACCESS }, @@ -2727,7 +2702,7 @@ GPU_Resource *GPU_RunRender(GPU_RenderSig *gp_render_sig, GPU_RenderParams param { D3D12_RESOURCE_BARRIER_TYPE_TRANSITION, rsig->emittance_flood_target, D3D12_RESOURCE_STATE_UNORDERED_ACCESS } }; - dx12_resource_barriers(cl->cl, countof(barriers), barriers); + GPU_D12_InsertBarrier(cl->cl, countof(barriers), barriers); } } @@ -2738,7 +2713,7 @@ GPU_Resource *GPU_RunRender(GPU_RenderSig *gp_render_sig, GPU_RenderParams param __profnc_dx12(cl->cq->prof, cl->cl, "Flood pass", Rgb32F(0.5, 0.2, 0.2)); /* Bind pipeline */ - command_list_set_pipeline(cl, flood_pipeline); + GPU_D12_SetPipeline(cl, flood_pipeline); i32 step_length = -1; @@ -2752,10 +2727,10 @@ GPU_Resource *GPU_RunRender(GPU_RenderSig *gp_render_sig, GPU_RenderParams param /* UAV barrier */ { - struct dx12_resource_barrier_desc barriers[] = { + GPU_D12_ResourceBarrierDesc barriers[] = { { D3D12_RESOURCE_BARRIER_TYPE_UAV, rsig->emittance_flood_read, 0 } }; - dx12_resource_barriers(cl->cl, countof(barriers), barriers); + GPU_D12_InsertBarrier(cl->cl, countof(barriers), barriers); } /* Set sig */ @@ -2766,7 +2741,7 @@ GPU_Resource *GPU_RunRender(GPU_RenderSig *gp_render_sig, GPU_RenderParams param sig.target_flood_tex_urid = rsig->emittance_flood_target->uav_descriptor->index; sig.tex_width = render_size.x; sig.tex_height = render_size.y; - command_list_set_sig(cl, &sig, sizeof(sig)); + GPU_D12_SetSig(cl, &sig, sizeof(sig)); /* Dispatch */ ID3D12GraphicsCommandList_Dispatch(cl->cl, (render_size.x + 7) / 8, (render_size.y + 7) / 8, 1); @@ -2793,7 +2768,7 @@ GPU_Resource *GPU_RunRender(GPU_RenderSig *gp_render_sig, GPU_RenderParams param { /* Barrier */ { - struct dx12_resource_barrier_desc barriers[] = { + GPU_D12_ResourceBarrierDesc barriers[] = { { D3D12_RESOURCE_BARRIER_TYPE_TRANSITION, rsig->albedo, D3D12_RESOURCE_STATE_ALL_SHADER_RESOURCE }, { D3D12_RESOURCE_BARRIER_TYPE_TRANSITION, rsig->emittance, D3D12_RESOURCE_STATE_ALL_SHADER_RESOURCE }, @@ -2805,14 +2780,14 @@ GPU_Resource *GPU_RunRender(GPU_RenderSig *gp_render_sig, GPU_RenderParams param { D3D12_RESOURCE_BARRIER_TYPE_TRANSITION, rsig->shade_target, D3D12_RESOURCE_STATE_UNORDERED_ACCESS } }; - dx12_resource_barriers(cl->cl, countof(barriers), barriers); + GPU_D12_InsertBarrier(cl->cl, countof(barriers), barriers); } /* Clear */ { __profn("Clear shade target"); __profnc_dx12(cl->cq->prof, cl->cl, "Clear shade target", Rgb32F(0.5, 0.2, 0.2)); f32 clear_color[] = { 0.0f, 0.0f, 0.0f, 0.0f }; - ID3D12GraphicsCommandList_ClearUnorderedAccessViewFloat(cl->cl, gpu_handle_from_descriptor(rsig->shade_target->uav_descriptor, descriptor_heap), rsig->shade_target->uav_descriptor->handle, rsig->shade_target->resource, clear_color, 0, 0); + ID3D12GraphicsCommandList_ClearUnorderedAccessViewFloat(cl->cl, GPU_D12_GpuHandleFromDescriptor(rsig->shade_target->uav_descriptor, descriptor_heap), rsig->shade_target->uav_descriptor->handle, rsig->shade_target->resource, clear_color, 0, 0); } } @@ -2823,7 +2798,7 @@ GPU_Resource *GPU_RunRender(GPU_RenderSig *gp_render_sig, GPU_RenderParams param __profnc_dx12(cl->cq->prof, cl->cl, "Shade pass", Rgb32F(0.5, 0.2, 0.2)); /* Bind pipeline */ - command_list_set_pipeline(cl, shade_pipeline); + GPU_D12_SetPipeline(cl, shade_pipeline); u32 shade_flags = K_SHADE_FLAG_NONE; if (params.effects_disabled) @@ -2847,7 +2822,7 @@ GPU_Resource *GPU_RunRender(GPU_RenderSig *gp_render_sig, GPU_RenderParams param sig.emittance_flood_tex_urid = rsig->emittance_flood_read->srv_descriptor->index; sig.read_tex_urid = rsig->shade_read->uav_descriptor->index; sig.target_tex_urid = rsig->shade_target->uav_descriptor->index; - command_list_set_sig(cl, &sig, sizeof(sig)); + GPU_D12_SetSig(cl, &sig, sizeof(sig)); /* Dispatch */ ID3D12GraphicsCommandList_Dispatch(cl->cl, (render_size.x + 7) / 8, (render_size.y + 7) / 8, 1); @@ -2862,12 +2837,12 @@ GPU_Resource *GPU_RunRender(GPU_RenderSig *gp_render_sig, GPU_RenderParams param { /* Barrier */ { - struct dx12_resource_barrier_desc barriers[] = { + GPU_D12_ResourceBarrierDesc barriers[] = { { D3D12_RESOURCE_BARRIER_TYPE_TRANSITION, rsig->shade_read, D3D12_RESOURCE_STATE_UNORDERED_ACCESS }, { D3D12_RESOURCE_BARRIER_TYPE_UAV, rsig->shade_read, 0 }, { D3D12_RESOURCE_BARRIER_TYPE_TRANSITION, rsig->ui_target, D3D12_RESOURCE_STATE_RENDER_TARGET } }; - dx12_resource_barriers(cl->cl, countof(barriers), barriers); + GPU_D12_InsertBarrier(cl->cl, countof(barriers), barriers); ID3D12GraphicsCommandList_OMSetRenderTargets(cl->cl, 1, &rsig->ui_target->rtv_descriptor->handle, 0, 0); } /* Clear */ @@ -2886,11 +2861,11 @@ GPU_Resource *GPU_RunRender(GPU_RenderSig *gp_render_sig, GPU_RenderParams param __profnc_dx12(cl->cq->prof, cl->cl, "UI blit pass", Rgb32F(0.5, 0.2, 0.2)); /* Bind pipeline */ - command_list_set_pipeline(cl, blit_pipeline); + GPU_D12_SetPipeline(cl, blit_pipeline); /* Set Rasterizer State */ - D3D12_VIEWPORT viewport = viewport_from_rect(ui_viewport); - D3D12_RECT scissor = scissor_from_rect(ui_viewport); + D3D12_VIEWPORT viewport = GPU_D12_ViewportFromRect(ui_viewport); + D3D12_RECT scissor = GPU_D12_ScissorRectFromRect(ui_viewport); ID3D12GraphicsCommandList_RSSetViewports(cl->cl, 1, &viewport); ID3D12GraphicsCommandList_RSSetScissorRects(cl->cl, 1, &scissor); @@ -2901,11 +2876,11 @@ GPU_Resource *GPU_RunRender(GPU_RenderSig *gp_render_sig, GPU_RenderParams param sig.exposure = 2.0; sig.gamma = (f32)2.2; sig.tex_urid = rsig->shade_read->uav_descriptor->index; - command_list_set_sig(cl, &sig, sizeof(sig)); + GPU_D12_SetSig(cl, &sig, sizeof(sig)); /* Draw */ - D3D12_VERTEX_BUFFER_VIEW vbv = vbv_from_command_buffer(dummy_vertex_buffer, 0); - D3D12_INDEX_BUFFER_VIEW ibv = ibv_from_command_buffer(quad_index_buffer, DXGI_FORMAT_R16_UINT); + D3D12_VERTEX_BUFFER_VIEW vbv = GPU_D12_VbvFromCommandBuffer(dummy_vertex_buffer, 0); + D3D12_INDEX_BUFFER_VIEW ibv = GPU_D12_IbvFromCommandBuffer(quad_index_buffer, DXGI_FORMAT_R16_UINT); ID3D12GraphicsCommandList_IASetPrimitiveTopology(cl->cl, D3D_PRIMITIVE_TOPOLOGY_TRIANGLELIST); ID3D12GraphicsCommandList_IASetVertexBuffers(cl->cl, 0, 1, &vbv); ID3D12GraphicsCommandList_IASetIndexBuffer(cl->cl, &ibv); @@ -2919,11 +2894,11 @@ GPU_Resource *GPU_RunRender(GPU_RenderSig *gp_render_sig, GPU_RenderParams param __profnc_dx12(cl->cq->prof, cl->cl, "UI rect pass", Rgb32F(0.5, 0.2, 0.2)); /* Bind pipeline */ - command_list_set_pipeline(cl, ui_pipeline); + GPU_D12_SetPipeline(cl, ui_pipeline); /* Set Rasterizer State */ - D3D12_VIEWPORT viewport = viewport_from_rect(ui_viewport); - D3D12_RECT scissor = scissor_from_rect(ui_viewport); + D3D12_VIEWPORT viewport = GPU_D12_ViewportFromRect(ui_viewport); + D3D12_RECT scissor = GPU_D12_ScissorRectFromRect(ui_viewport); ID3D12GraphicsCommandList_RSSetViewports(cl->cl, 1, &viewport); ID3D12GraphicsCommandList_RSSetScissorRects(cl->cl, 1, &scissor); @@ -2931,12 +2906,12 @@ GPU_Resource *GPU_RunRender(GPU_RenderSig *gp_render_sig, GPU_RenderParams param K_UiSig sig = ZI; sig.projection = ui_vp_matrix; sig.instances_urid = ui_rect_instance_buffer->resource->srv_descriptor->index; - command_list_set_sig(cl, &sig, sizeof(sig)); + GPU_D12_SetSig(cl, &sig, sizeof(sig)); /* Draw */ u32 instance_count = ui_rect_instance_buffer->size / sizeof(K_UiInstance); - D3D12_VERTEX_BUFFER_VIEW vbv = vbv_from_command_buffer(dummy_vertex_buffer, 0); - D3D12_INDEX_BUFFER_VIEW ibv = ibv_from_command_buffer(quad_index_buffer, DXGI_FORMAT_R16_UINT); + D3D12_VERTEX_BUFFER_VIEW vbv = GPU_D12_VbvFromCommandBuffer(dummy_vertex_buffer, 0); + D3D12_INDEX_BUFFER_VIEW ibv = GPU_D12_IbvFromCommandBuffer(quad_index_buffer, DXGI_FORMAT_R16_UINT); ID3D12GraphicsCommandList_IASetPrimitiveTopology(cl->cl, D3D_PRIMITIVE_TOPOLOGY_TRIANGLELIST); ID3D12GraphicsCommandList_IASetVertexBuffers(cl->cl, 0, 1, &vbv); ID3D12GraphicsCommandList_IASetIndexBuffer(cl->cl, &ibv); @@ -2950,11 +2925,11 @@ GPU_Resource *GPU_RunRender(GPU_RenderSig *gp_render_sig, GPU_RenderParams param __profnc_dx12(cl->cq->prof, cl->cl, "UI shape pass", Rgb32F(0.5, 0.2, 0.2)); /* Bind pipeline */ - command_list_set_pipeline(cl, shape_pipeline); + GPU_D12_SetPipeline(cl, shape_pipeline); /* Set Rasterizer State */ - D3D12_VIEWPORT viewport = viewport_from_rect(ui_viewport); - D3D12_RECT scissor = scissor_from_rect(ui_viewport); + D3D12_VIEWPORT viewport = GPU_D12_ViewportFromRect(ui_viewport); + D3D12_RECT scissor = GPU_D12_ScissorRectFromRect(ui_viewport); ID3D12GraphicsCommandList_RSSetViewports(cl->cl, 1, &viewport); ID3D12GraphicsCommandList_RSSetScissorRects(cl->cl, 1, &scissor); @@ -2962,22 +2937,22 @@ GPU_Resource *GPU_RunRender(GPU_RenderSig *gp_render_sig, GPU_RenderParams param K_ShapeSig sig = ZI; sig.projection = ui_vp_matrix; sig.verts_urid = ui_shape_verts_buffer->resource->srv_descriptor->index; - command_list_set_sig(cl, &sig, sizeof(sig)); + GPU_D12_SetSig(cl, &sig, sizeof(sig)); /* Draw */ u32 index_count = ui_shape_indices_buffer->size / sizeof(u32); - D3D12_VERTEX_BUFFER_VIEW vbv = vbv_from_command_buffer(dummy_vertex_buffer, 0); - D3D12_INDEX_BUFFER_VIEW ibv = ibv_from_command_buffer(ui_shape_indices_buffer, DXGI_FORMAT_R32_UINT); + D3D12_VERTEX_BUFFER_VIEW vbv = GPU_D12_VbvFromCommandBuffer(dummy_vertex_buffer, 0); + D3D12_INDEX_BUFFER_VIEW ibv = GPU_D12_IbvFromCommandBuffer(ui_shape_indices_buffer, DXGI_FORMAT_R32_UINT); ID3D12GraphicsCommandList_IASetPrimitiveTopology(cl->cl, D3D_PRIMITIVE_TOPOLOGY_TRIANGLELIST); ID3D12GraphicsCommandList_IASetVertexBuffers(cl->cl, 0, 1, &vbv); ID3D12GraphicsCommandList_IASetIndexBuffer(cl->cl, &ibv); ID3D12GraphicsCommandList_DrawIndexedInstanced(cl->cl, index_count, 1, 0, 0, 0); } } - command_list_close(cl); - pipeline_scope_end(pipeline_scope); + GPU_D12_EndCommandList(cl); + GPU_D12_EndPipelineScope(pipeline_scope); - render_sig_reset(rsig); + GPU_D12_ResetRenderSig(rsig); EndScratch(scratch); return (GPU_Resource *)rsig->ui_target; @@ -3023,7 +2998,7 @@ GPU_MemoryInfo GPU_QueryMemoryInfo(void) * Swapchain * ========================== */ -void swapchain_init_resources(GPU_D12_Swapchain *swapchain) +void GPU_D12_InitSwapchainResources(GPU_D12_Swapchain *swapchain) { GPU_D12_SharedState *g = &GPU_D12_shared_state; for (u32 i = 0; i < countof(swapchain->buffers); ++i) @@ -3039,7 +3014,7 @@ void swapchain_init_resources(GPU_D12_Swapchain *swapchain) ZeroStruct(sb); sb->swapchain = swapchain; sb->resource = resource; - sb->rtv_descriptor = descriptor_alloc(g->rtv_heap); + sb->rtv_descriptor = GPU_D12_AllocDescriptor(g->rtv_heap); sb->state = D3D12_RESOURCE_STATE_COMMON; ID3D12Device_CreateRenderTargetView(g->device, sb->resource, 0, sb->rtv_descriptor->handle); } @@ -3109,7 +3084,7 @@ GPU_Swapchain *GPU_AllocSwapchain(P_Window *window, Vec2I32 resolution) IDXGISwapChain1_Release(swapchain1); swapchain->hwnd = hwnd; - swapchain_init_resources(swapchain); + GPU_D12_InitSwapchainResources(swapchain); return (GPU_Swapchain *)swapchain; } @@ -3133,7 +3108,7 @@ void GPU_WaitOnSwapchain(GPU_Swapchain *gp_swapchain) #endif } -GPU_D12_SwapchainBuffer *update_swapchain(GPU_D12_Swapchain *swapchain, Vec2I32 resolution) +GPU_D12_SwapchainBuffer *GPU_D12_UpdateSwapchain(GPU_D12_Swapchain *swapchain, Vec2I32 resolution) { __prof; GPU_D12_SharedState *g = &GPU_D12_shared_state; @@ -3145,7 +3120,7 @@ GPU_D12_SwapchainBuffer *update_swapchain(GPU_D12_Swapchain *swapchain, Vec2I32 HRESULT hr = 0; GPU_D12_CommandQueue *cq = g->command_queues[DX12_QUEUE_DIRECT]; /* Lock direct queue submissions (in case any write to backbuffer) */ - /* TODO: Less overkill approach - Only flush present_blit since we know it's the only operation targeting backbuffer */ + /* TODO: Less overkill approach - Only flush GPU_D12_BlitToSwapchain since we know it's the only operation targeting backbuffer */ P_Lock lock = P_LockE(&cq->submit_fence_mutex); //DEBUGBREAKABLE; //P_Lock lock = P_LockE(&g->global_command_list_record_mutex); @@ -3163,7 +3138,7 @@ GPU_D12_SwapchainBuffer *update_swapchain(GPU_D12_Swapchain *swapchain, Vec2I32 for (u32 i = 0; i < countof(swapchain->buffers); ++i) { GPU_D12_SwapchainBuffer *sb = &swapchain->buffers[i]; - descriptor_release(sb->rtv_descriptor); + GPU_D12_ReleaseDescriptor(sb->rtv_descriptor); ID3D12Resource_Release(sb->resource); } @@ -3177,7 +3152,7 @@ GPU_D12_SwapchainBuffer *update_swapchain(GPU_D12_Swapchain *swapchain, Vec2I32 } P_Unlock(&lock); - swapchain_init_resources(swapchain); + GPU_D12_InitSwapchainResources(swapchain); swapchain->resolution = resolution; } @@ -3190,16 +3165,16 @@ GPU_D12_SwapchainBuffer *update_swapchain(GPU_D12_Swapchain *swapchain, Vec2I32 * Present * ========================== */ -void present_blit(GPU_D12_SwapchainBuffer *dst, GPU_D12_Resource *src, Xform src_xf) +void GPU_D12_BlitToSwapchain(GPU_D12_SwapchainBuffer *dst, GPU_D12_Resource *src, Xform src_xf) { __prof; GPU_D12_SharedState *g = &GPU_D12_shared_state; - GPU_D12_PipelineScope *pipeline_scope = pipeline_scope_begin(); - GPU_D12_Pipeline *blit_pipeline = pipeline_from_name(pipeline_scope, Lit("kernel_blit")); + GPU_D12_PipelineScope *pipeline_scope = GPU_D12_BeginPipelineScope(); + GPU_D12_Pipeline *blit_pipeline = GPU_D12_PipelineFromName(pipeline_scope, Lit("kernel_blit")); if (blit_pipeline->success) { GPU_D12_CommandQueue *cq = g->command_queues[DX12_QUEUE_DIRECT]; - GPU_D12_CommandList *cl = command_list_open(cq->cl_pool); + GPU_D12_CommandList *cl = GPU_D12_BeginCommandList(cq->cl_pool); { __profn("Present blit"); __profnc_dx12(cl->cq->prof, cl->cl, "Present blit", Rgb32F(0.5, 0.2, 0.2)); @@ -3209,24 +3184,24 @@ void present_blit(GPU_D12_SwapchainBuffer *dst, GPU_D12_Resource *src, Xform src /* TODO: Make these static */ /* Dummy vertex buffer */ LocalPersist u16 quad_indices[6] = { 0, 1, 2, 0, 2, 3 }; - GPU_D12_CommandBuffer *dummy_vertex_buffer = command_list_push_buffer(cl, 0, (u8 *)0); - GPU_D12_CommandBuffer *quad_index_buffer = command_list_push_buffer(cl, countof(quad_indices), quad_indices); + GPU_D12_CommandBuffer *dummy_vertex_buffer = GPU_D12_PushCommandBuffer(cl, 0, (u8 *)0); + GPU_D12_CommandBuffer *quad_index_buffer = GPU_D12_PushCommandBuffer(cl, countof(quad_indices), quad_indices); /* Upload descriptor heap */ - GPU_D12_CommandDescriptorHeap *descriptor_heap = command_list_push_descriptor_heap(cl, g->cbv_srv_uav_heap); + GPU_D12_CommandDescriptorHeap *descriptor_heap = GPU_D12_PushDescriptorHeap(cl, g->cbv_srv_uav_heap); ID3D12DescriptorHeap *heaps[] = { descriptor_heap->heap }; ID3D12GraphicsCommandList_SetDescriptorHeaps(cl->cl, countof(heaps), heaps); Rect viewport_rect = RectFromVec2(VEC2(0, 0), VEC2(swapchain->resolution.x, swapchain->resolution.y)); - D3D12_VIEWPORT viewport = viewport_from_rect(viewport_rect); - D3D12_RECT scissor = scissor_from_rect(viewport_rect); + D3D12_VIEWPORT viewport = GPU_D12_ViewportFromRect(viewport_rect); + D3D12_RECT scissor = GPU_D12_ScissorRectFromRect(viewport_rect); Mat4x4 vp_matrix = ZI; { Xform xf = src_xf; xf = ScaleXform(xf, VEC2(src->texture_size.x, src->texture_size.y)); xf = TranslateXform(xf, VEC2(0.5, 0.5)); - vp_matrix = calculate_vp(xf, viewport.Width, viewport.Height); + vp_matrix = ProjectMat4x4View(xf, viewport.Width, viewport.Height); } /* Transition dst to render target */ @@ -3250,7 +3225,7 @@ void present_blit(GPU_D12_SwapchainBuffer *dst, GPU_D12_Resource *src, Xform src ID3D12GraphicsCommandList_ClearRenderTargetView(cl->cl, dst->rtv_descriptor->handle, clear_color, 0, 0); /* Bind pipeline */ - command_list_set_pipeline(cl, blit_pipeline); + GPU_D12_SetPipeline(cl, blit_pipeline); /* Set Rasterizer State */ ID3D12GraphicsCommandList_RSSetViewports(cl->cl, 1, &viewport); @@ -3261,11 +3236,11 @@ void present_blit(GPU_D12_SwapchainBuffer *dst, GPU_D12_Resource *src, Xform src sig.projection = vp_matrix; sig.flags = K_BLIT_FLAG_NONE; sig.tex_urid = src->srv_descriptor->index; - command_list_set_sig(cl, &sig, sizeof(sig)); + GPU_D12_SetSig(cl, &sig, sizeof(sig)); /* Draw */ - D3D12_VERTEX_BUFFER_VIEW vbv = vbv_from_command_buffer(dummy_vertex_buffer, 0); - D3D12_INDEX_BUFFER_VIEW ibv = ibv_from_command_buffer(quad_index_buffer, DXGI_FORMAT_R16_UINT); + D3D12_VERTEX_BUFFER_VIEW vbv = GPU_D12_VbvFromCommandBuffer(dummy_vertex_buffer, 0); + D3D12_INDEX_BUFFER_VIEW ibv = GPU_D12_IbvFromCommandBuffer(quad_index_buffer, DXGI_FORMAT_R16_UINT); ID3D12GraphicsCommandList_IASetPrimitiveTopology(cl->cl, D3D_PRIMITIVE_TOPOLOGY_TRIANGLELIST); ID3D12GraphicsCommandList_IASetVertexBuffers(cl->cl, 0, 1, &vbv); ID3D12GraphicsCommandList_IASetIndexBuffer(cl->cl, &ibv); @@ -3286,20 +3261,20 @@ void present_blit(GPU_D12_SwapchainBuffer *dst, GPU_D12_Resource *src, Xform src dst->state = rtb.StateAfter; } } - command_list_close(cl); + GPU_D12_EndCommandList(cl); } - pipeline_scope_end(pipeline_scope); + GPU_D12_EndPipelineScope(pipeline_scope); } void GPU_PresentSwapchain(GPU_Swapchain *gp_swapchain, Vec2I32 backbuffer_resolution, GPU_Resource *texture, Xform texture_xf, i32 vsync) { __prof; GPU_D12_Swapchain *swapchain = (GPU_D12_Swapchain *)gp_swapchain; - GPU_D12_SwapchainBuffer *swapchain_buffer = update_swapchain(swapchain, backbuffer_resolution); + GPU_D12_SwapchainBuffer *swapchain_buffer = GPU_D12_UpdateSwapchain(swapchain, backbuffer_resolution); GPU_D12_Resource *texture_resource = (GPU_D12_Resource *)texture; /* Blit */ - present_blit(swapchain_buffer, texture_resource, texture_xf); + GPU_D12_BlitToSwapchain(swapchain_buffer, texture_resource, texture_xf); u32 present_flags = 0; if (vsync == 0) @@ -3348,7 +3323,7 @@ void GPU_PresentSwapchain(GPU_Swapchain *gp_swapchain, Vec2I32 backbuffer_resolu * Evictor job * ========================== */ -P_JobDef(dx12_evictor_job, _) +P_JobDef(GPU_D12_EvictorJob, _) { GPU_D12_SharedState *g = &GPU_D12_shared_state; u64 completed_targets[DX12_NUM_QUEUES] = ZI; @@ -3388,12 +3363,12 @@ P_JobDef(dx12_evictor_job, _) { __profn("Wait on fence"); { - struct dx12_wait_fence_job_sig sig = ZI; + GPU_D12_WaitOnFenceJobSig sig = ZI; sig.fence = cq->submit_fence; sig.target = targets[i]; { P_Counter counter = ZI; - P_Run(1, dx12_wait_fence_job, &sig, P_Pool_Floating, P_Priority_Low, &counter); + P_Run(1, GPU_D12_WaitOnFenceJob, &sig, P_Pool_Floating, P_Priority_Low, &counter); P_WaitOnCounter(&counter); } } @@ -3417,13 +3392,13 @@ P_JobDef(dx12_evictor_job, _) case GPU_D12_FencedReleaseKind_Resource: { GPU_D12_Resource *resource = (GPU_D12_Resource *)fr->ptr; - dx12_resource_release_now(resource); + GPU_D12_ReleaseResourceNow(resource); } break; case GPU_D12_FencedReleaseKind_Pipeline: { GPU_D12_Pipeline *pipeline = (GPU_D12_Pipeline *)fr->ptr; - pipeline_release_now(pipeline); + GPU_D12_ReleasePipelineNow(pipeline); } break; } } diff --git a/src/gpu/gpu_dx12.h b/src/gpu/gpu_dx12.h index 4f964c28..1f741ece 100644 --- a/src/gpu/gpu_dx12.h +++ b/src/gpu/gpu_dx12.h @@ -263,6 +263,8 @@ Struct(GPU_D12_AllocPipelineJobSig) { GPU_D12_PipelineDesc *descs_in; GPU_D12_Pi Struct(GPU_D12_UploadJobSig) { GPU_D12_Resource *resource; void *data; }; +Struct(GPU_D12_WaitOnFenceJobSig) { ID3D12Fence *fence; u64 target; }; + Struct(GPU_D12_ShaderDesc) { String src; @@ -350,6 +352,13 @@ Struct(GPU_D12_MaterialGridDesc) u32 y_color; }; +Struct(GPU_D12_ResourceBarrierDesc) +{ + enum D3D12_RESOURCE_BARRIER_TYPE type; + GPU_D12_Resource *resource; + enum D3D12_RESOURCE_STATES new_state; /* 0 if type != D3D12_RESOURCE_BARRIER_TYPE_TRANSITION */ +}; + /* ========================== * * Global state * ========================== */ @@ -432,242 +441,193 @@ extern GPU_D12_SharedState GPU_D12_shared_state; * Startup * ========================== */ -void GPU_Startup(void); - -P_ExitFuncDef(gp_shutdown); +P_ExitFuncDef(GPU_D12_Shutdown); /* ========================== * * Dx12 device initialization * ========================== */ -void dx12_init_error(String error); +void GPU_D12_PushInitError(String error); -void dx12_init_device(void); +void GPU_D12_InitDevice(void); /* ========================== * * Dx12 object initialization * ========================== */ -void dx12_init_objects(void); +void GPU_D12_InitObjects(void); /* ========================== * * Dx12 pipeline initialization * ========================== */ -void dx12_init_pipelines(void); +void GPU_D12_Pipelines(void); /* ========================== * * Noise texture initialization * ========================== */ -void dx12_init_noise(void); +void GPU_D12_InitNoise(void); /* ========================== * * Shader compilation * ========================== */ -P_JobDef(shader_compile_job, job); +P_JobDef(GPU_D12_CompileShaderJob, job); /* ========================== * * Pipeline * ========================== */ - P_JobDef(pipeline_alloc_job, job); +P_JobDef(GPU_D12_AllocPipelineJob, job); -void pipeline_release_now(GPU_D12_Pipeline *pipeline); +void GPU_D12_ReleasePipelineNow(GPU_D12_Pipeline *pipeline); /* ========================== * * Pipeline cache * ========================== */ -GPU_D12_PipelineScope *pipeline_scope_begin(void); +GPU_D12_PipelineScope *GPU_D12_BeginPipelineScope(void); -void pipeline_scope_end(GPU_D12_PipelineScope *scope); +void GPU_D12_EndPipelineScope(GPU_D12_PipelineScope *scope); -extern Readonly GPU_D12_Pipeline g_nil_pipeline; -GPU_D12_Pipeline *pipeline_from_name(GPU_D12_PipelineScope *scope, String name); +extern Readonly GPU_D12_Pipeline GPU_D12_nil_pipeline; +GPU_D12_Pipeline *GPU_D12_PipelineFromName(GPU_D12_PipelineScope *scope, String name); -void pipeline_register(u64 num_pipelines, GPU_D12_Pipeline **pipelines); +void GPU_D12_RegisterPipeline(u64 num_pipelines, GPU_D12_Pipeline **pipelines); -W_CallbackFuncDef(pipeline_watch_callback, name); +W_CallbackFuncDef(GPU_D12_WatchPipelineCallback, name); /* ========================== * * Descriptor * ========================== */ -GPU_D12_Descriptor *descriptor_alloc(GPU_D12_CpuDescriptorHeap *dh); +GPU_D12_Descriptor *GPU_D12_AllocDescriptor(GPU_D12_CpuDescriptorHeap *dh); -void descriptor_release(GPU_D12_Descriptor *descriptor); +void GPU_D12_ReleaseDescriptor(GPU_D12_Descriptor *descriptor); /* ========================== * * CPU descriptor heap * ========================== */ -GPU_D12_CpuDescriptorHeap *cpu_descriptor_heap_alloc(enum D3D12_DESCRIPTOR_HEAP_TYPE type); +GPU_D12_CpuDescriptorHeap *GPU_D12_AllocCpuDescriptorHeap(enum D3D12_DESCRIPTOR_HEAP_TYPE type); /* ========================== * * Fenced release * ========================== */ -void fenced_release(void *data, GPU_D12_FencedReleaseKind kind); +void GPU_D12_ReleaseDataFenced(void *data, GPU_D12_FencedReleaseKind kind); /* ========================== * * Resource * ========================== */ -GPU_D12_Resource *dx12_resource_alloc(D3D12_HEAP_PROPERTIES heap_props, D3D12_HEAP_FLAGS heap_flags, D3D12_RESOURCE_DESC desc, D3D12_RESOURCE_STATES initial_state); +GPU_D12_Resource *GPU_D12_AllocResource(D3D12_HEAP_PROPERTIES heap_props, D3D12_HEAP_FLAGS heap_flags, D3D12_RESOURCE_DESC desc, D3D12_RESOURCE_STATES initial_state); -void dx12_resource_release_now(GPU_D12_Resource *t); +void GPU_D12_ReleaseResourceNow(GPU_D12_Resource *t); -void GPU_ReleaseResource(GPU_Resource *resource); +void GPU_ReleaseResourceFenced(GPU_Resource *resource); /* ========================== * * Resource barrier * ========================== */ -struct dx12_resource_barrier_desc -{ - enum D3D12_RESOURCE_BARRIER_TYPE type; - GPU_D12_Resource *resource; - enum D3D12_RESOURCE_STATES new_state; /* 0 if type != D3D12_RESOURCE_BARRIER_TYPE_TRANSITION */ -}; - -void dx12_resource_barriers(ID3D12GraphicsCommandList *cl, i32 num_descs, struct dx12_resource_barrier_desc *descs); +void GPU_D12_InsertBarrier(ID3D12GraphicsCommandList *cl, i32 num_descs, GPU_D12_ResourceBarrierDesc *descs); /* ========================== * * Command queue * ========================== */ -P_JobDef(command_queue_alloc_job, job); +P_JobDef(GPU_D12_AllocCommandQueueJob, job); -void command_queue_release(GPU_D12_CommandQueue *cq); +void GPU_D12_ReleaseCommandQueue(GPU_D12_CommandQueue *cq); /* ========================== * * Command list * ========================== */ -GPU_D12_CommandListPool *command_list_pool_alloc(GPU_D12_CommandQueue *cq); +GPU_D12_CommandListPool *GPU_D12_AllocCommandListPool(GPU_D12_CommandQueue *cq); -GPU_D12_CommandList *command_list_open(GPU_D12_CommandListPool *pool); +GPU_D12_CommandList *GPU_D12_BeginCommandList(GPU_D12_CommandListPool *pool); /* TODO: Allow multiple command list submissions */ -u64 command_list_close(GPU_D12_CommandList *cl); +u64 GPU_D12_EndCommandList(GPU_D12_CommandList *cl); /* ========================== * * Command descriptor heap (GPU / shader visible descriptor heap) * ========================== */ -GPU_D12_CommandDescriptorHeap *command_list_push_descriptor_heap(GPU_D12_CommandList *cl, GPU_D12_CpuDescriptorHeap *dh_cpu); +GPU_D12_CommandDescriptorHeap *GPU_D12_PushDescriptorHeap(GPU_D12_CommandList *cl, GPU_D12_CpuDescriptorHeap *dh_cpu); /* ========================== * * Command buffer * ========================== */ -u64 command_buffer_hash_from_size(u64 size); +u64 GPU_D12_CommandBufferHashFromSize(u64 size); -u64 align_up_pow2(u64 v); - -#define command_list_push_buffer(cl, count, elems) _command_list_push_buffer((cl), count * ((elems) ? sizeof(*(elems)) : 0), (elems), (elems) ? sizeof(*(elems)) : 1) -GPU_D12_CommandBuffer *_command_list_push_buffer(GPU_D12_CommandList *cl, u64 data_len, void *data, u64 data_stride); +#define GPU_D12_PushCommandBuffer(cl, count, elems) GPU_D12__PushCommandBuffer((cl), count * ((elems) ? sizeof(*(elems)) : 0), (elems), (elems) ? sizeof(*(elems)) : 1) +GPU_D12_CommandBuffer *GPU_D12__PushCommandBuffer(GPU_D12_CommandList *cl, u64 data_len, void *data, u64 data_stride); /* ========================== * * Wait job * ========================== */ -struct dx12_wait_fence_job_sig -{ - ID3D12Fence *fence; - u64 target; -}; - -P_JobDef(dx12_wait_fence_job, job); - -/* ========================== * - * Texture - * ========================== */ - -GPU_Resource *GPU_AllocTexture(GPU_TextureFormat format, u32 flags, Vec2I32 size, void *initial_data); - -Vec2I32 GPU_GetTextureSize(GPU_Resource *resource); +P_JobDef(GPU_D12_WaitOnFenceJob, job); /* ========================== * * Upload * ========================== */ -P_JobDef(dx12_upload_job, job); +P_JobDef(GPU_D12_UploadJob, job); /* ========================== * * Run utils * ========================== */ -void command_list_set_pipeline(GPU_D12_CommandList *cl, GPU_D12_Pipeline *pipeline); +void GPU_D12_SetPipeline(GPU_D12_CommandList *cl, GPU_D12_Pipeline *pipeline); -void command_list_set_sig(GPU_D12_CommandList *cl, void *src, u32 size); +void GPU_D12_SetSig(GPU_D12_CommandList *cl, void *src, u32 size); -struct D3D12_VIEWPORT viewport_from_rect(Rect r); +struct D3D12_VIEWPORT GPU_D12_ViewportFromRect(Rect r); -D3D12_RECT scissor_from_rect(Rect r); +D3D12_RECT GPU_D12_ScissorRectFromRect(Rect r); -D3D12_VERTEX_BUFFER_VIEW vbv_from_command_buffer(GPU_D12_CommandBuffer *cb, u32 vertex_size); +D3D12_VERTEX_BUFFER_VIEW GPU_D12_VbvFromCommandBuffer(GPU_D12_CommandBuffer *cb, u32 vertex_size); -D3D12_INDEX_BUFFER_VIEW ibv_from_command_buffer(GPU_D12_CommandBuffer *cb, DXGI_FORMAT format); +D3D12_INDEX_BUFFER_VIEW GPU_D12_IbvFromCommandBuffer(GPU_D12_CommandBuffer *cb, DXGI_FORMAT format); -GPU_D12_Resource *gbuff_alloc(DXGI_FORMAT format, Vec2I32 size, D3D12_RESOURCE_STATES initial_state); +GPU_D12_Resource *GPU_D12_AllocGbuff(DXGI_FORMAT format, Vec2I32 size, D3D12_RESOURCE_STATES initial_state); -/* Calculate the view projection matrix */ -Inline Mat4x4 calculate_vp(Xform view, f32 viewport_width, f32 viewport_height); - -D3D12_GPU_DESCRIPTOR_HANDLE gpu_handle_from_descriptor(GPU_D12_Descriptor *descriptor, GPU_D12_CommandDescriptorHeap *cdh); +D3D12_GPU_DESCRIPTOR_HANDLE GPU_D12_GpuHandleFromDescriptor(GPU_D12_Descriptor *descriptor, GPU_D12_CommandDescriptorHeap *cdh); /* ========================== * * Render sig * ========================== */ -GPU_D12_RenderSig *render_sig_alloc(void); +GPU_D12_RenderSig *GPU_D12_AllocRenderSig(void); -void render_sig_reset(GPU_D12_RenderSig *sig); +void GPU_D12_ResetRenderSig(GPU_D12_RenderSig *sig); GPU_RenderSig *GPU_AllocRenderSig(void); -u32 GPU_PushRenderCmd(GPU_RenderSig *render_sig, GPU_RenderCmdDesc *cmd_desc); - -/* ========================== * - * Render - * ========================== */ - -GPU_Resource *GPU_RunRender(GPU_RenderSig *gp_render_sig, GPU_RenderParams params); - -/* ========================== * - * Memory info - * ========================== */ - -GPU_MemoryInfo GPU_QueryMemoryInfo(void); - /* ========================== * * Swapchain * ========================== */ -void swapchain_init_resources(GPU_D12_Swapchain *swapchain); +void GPU_D12_InitSwapchainResources(GPU_D12_Swapchain *swapchain); -GPU_Swapchain *GPU_AllocSwapchain(P_Window *window, Vec2I32 resolution); - -void GPU_ReleaseSwapchain(GPU_Swapchain *gp_swapchain); - -void GPU_WaitOnSwapchain(GPU_Swapchain *gp_swapchain); - -GPU_D12_SwapchainBuffer *update_swapchain(GPU_D12_Swapchain *swapchain, Vec2I32 resolution); +GPU_D12_SwapchainBuffer *GPU_D12_UpdateSwapchain(GPU_D12_Swapchain *swapchain, Vec2I32 resolution); /* ========================== * * Present * ========================== */ -void present_blit(GPU_D12_SwapchainBuffer *dst, GPU_D12_Resource *src, Xform src_xf); +void GPU_D12_BlitToSwapchain(GPU_D12_SwapchainBuffer *dst, GPU_D12_Resource *src, Xform src_xf); -void GPU_PresentSwapchain(GPU_Swapchain *gp_swapchain, Vec2I32 backbuffer_resolution, GPU_Resource *texture, Xform texture_xf, i32 vsync); /* ========================== * * Evictor job * ========================== */ -P_JobDef(dx12_evictor_job, _); +P_JobDef(GPU_D12_EvictorJob, _); diff --git a/src/sprite/sprite_core.c b/src/sprite/sprite_core.c index de5cff60..d34dbe18 100644 --- a/src/sprite/sprite_core.c +++ b/src/sprite/sprite_core.c @@ -1323,7 +1323,7 @@ internal P_JobDef(sprite_evictor_job, _) for (struct evict_node *en = first_evicted; en; en = en->next_evicted) { struct cache_entry *n = en->cache_entry; if (n->kind == CACHE_ENTRY_KIND_TEXTURE && n->texture->valid) { - GPU_ReleaseResource(n->texture->gp_texture); + GPU_ReleaseResourceFenced(n->texture->gp_texture); } ReleaseArena(n->arena); }