gpu layer refactor progress

This commit is contained in:
jacob 2025-07-31 22:37:39 -05:00
parent bdac093378
commit 8553fa624f
6 changed files with 296 additions and 332 deletions

View File

@ -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 //~ Logn
@ -1418,3 +1438,10 @@ Mat4x4 MulMat4x4(Mat4x4 m1, Mat4x4 m2)
return result; 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);
}

View File

@ -218,6 +218,7 @@ i64 SignF64(f64 f);
//~ Exponential ops //~ Exponential ops
u64 PowU64(u64 base, u8 exp); u64 PowU64(u64 base, u8 exp);
u64 AlignU64Pow2(u64 x);
f32 LnF32(f32 x); f32 LnF32(f32 x);
f32 ExpF32(f32 x); f32 ExpF32(f32 x);
f32 PowF32(f32 a, f32 b); f32 PowF32(f32 a, f32 b);
@ -409,3 +410,4 @@ SoftSpring MakeSpring(f32 hertz, f32 damping_ratio, f32 dt);
Mat4x4 Mat4x4FromXform(Xform xf); Mat4x4 Mat4x4FromXform(Xform xf);
Mat4x4 Mat4x4FromOrtho(f32 left, f32 right, f32 bottom, f32 top, f32 near_z, f32 far_z); Mat4x4 Mat4x4FromOrtho(f32 left, f32 right, f32 bottom, f32 top, f32 near_z, f32 far_z);
Mat4x4 MulMat4x4(Mat4x4 m1, Mat4x4 m2); Mat4x4 MulMat4x4(Mat4x4 m1, Mat4x4 m2);
Mat4x4 ProjectMat4x4View(Xform view, f32 viewport_width, f32 viewport_height);

View File

@ -120,7 +120,7 @@ void GPU_Startup(void);
* the caller to make sure the released resources aren't then referenced in * the caller to make sure the released resources aren't then referenced in
* any runs * any runs
*/ */
void GPU_ReleaseResource(GPU_Resource *resource); void GPU_ReleaseResourceFenced(GPU_Resource *resource);
//////////////////////////////// ////////////////////////////////
//~ Texture operations //~ Texture operations

File diff suppressed because it is too large Load Diff

View File

@ -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_UploadJobSig) { GPU_D12_Resource *resource; void *data; };
Struct(GPU_D12_WaitOnFenceJobSig) { ID3D12Fence *fence; u64 target; };
Struct(GPU_D12_ShaderDesc) Struct(GPU_D12_ShaderDesc)
{ {
String src; String src;
@ -350,6 +352,13 @@ Struct(GPU_D12_MaterialGridDesc)
u32 y_color; 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 * Global state
* ========================== */ * ========================== */
@ -432,242 +441,193 @@ extern GPU_D12_SharedState GPU_D12_shared_state;
* Startup * Startup
* ========================== */ * ========================== */
void GPU_Startup(void); P_ExitFuncDef(GPU_D12_Shutdown);
P_ExitFuncDef(gp_shutdown);
/* ========================== * /* ========================== *
* Dx12 device initialization * 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 * Dx12 object initialization
* ========================== */ * ========================== */
void dx12_init_objects(void); void GPU_D12_InitObjects(void);
/* ========================== * /* ========================== *
* Dx12 pipeline initialization * Dx12 pipeline initialization
* ========================== */ * ========================== */
void dx12_init_pipelines(void); void GPU_D12_Pipelines(void);
/* ========================== * /* ========================== *
* Noise texture initialization * Noise texture initialization
* ========================== */ * ========================== */
void dx12_init_noise(void); void GPU_D12_InitNoise(void);
/* ========================== * /* ========================== *
* Shader compilation * Shader compilation
* ========================== */ * ========================== */
P_JobDef(shader_compile_job, job); P_JobDef(GPU_D12_CompileShaderJob, job);
/* ========================== * /* ========================== *
* Pipeline * 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 * 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; extern Readonly GPU_D12_Pipeline GPU_D12_nil_pipeline;
GPU_D12_Pipeline *pipeline_from_name(GPU_D12_PipelineScope *scope, String name); 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 * 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 * 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 * Fenced release
* ========================== */ * ========================== */
void fenced_release(void *data, GPU_D12_FencedReleaseKind kind); void GPU_D12_ReleaseDataFenced(void *data, GPU_D12_FencedReleaseKind kind);
/* ========================== * /* ========================== *
* Resource * 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 * Resource barrier
* ========================== */ * ========================== */
struct dx12_resource_barrier_desc void GPU_D12_InsertBarrier(ID3D12GraphicsCommandList *cl, i32 num_descs, GPU_D12_ResourceBarrierDesc *descs);
{
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);
/* ========================== * /* ========================== *
* Command queue * 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 * 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 */ /* 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) * 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 * Command buffer
* ========================== */ * ========================== */
u64 command_buffer_hash_from_size(u64 size); u64 GPU_D12_CommandBufferHashFromSize(u64 size);
u64 align_up_pow2(u64 v); #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);
#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);
/* ========================== * /* ========================== *
* Wait job * Wait job
* ========================== */ * ========================== */
struct dx12_wait_fence_job_sig P_JobDef(GPU_D12_WaitOnFenceJob, job);
{
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);
/* ========================== * /* ========================== *
* Upload * Upload
* ========================== */ * ========================== */
P_JobDef(dx12_upload_job, job); P_JobDef(GPU_D12_UploadJob, job);
/* ========================== * /* ========================== *
* Run utils * 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 */ D3D12_GPU_DESCRIPTOR_HANDLE GPU_D12_GpuHandleFromDescriptor(GPU_D12_Descriptor *descriptor, GPU_D12_CommandDescriptorHeap *cdh);
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);
/* ========================== * /* ========================== *
* Render sig * 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); 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 * 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); GPU_D12_SwapchainBuffer *GPU_D12_UpdateSwapchain(GPU_D12_Swapchain *swapchain, 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);
/* ========================== * /* ========================== *
* Present * 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 * Evictor job
* ========================== */ * ========================== */
P_JobDef(dx12_evictor_job, _); P_JobDef(GPU_D12_EvictorJob, _);

View File

@ -1323,7 +1323,7 @@ internal P_JobDef(sprite_evictor_job, _)
for (struct evict_node *en = first_evicted; en; en = en->next_evicted) { for (struct evict_node *en = first_evicted; en; en = en->next_evicted) {
struct cache_entry *n = en->cache_entry; struct cache_entry *n = en->cache_entry;
if (n->kind == CACHE_ENTRY_KIND_TEXTURE && n->texture->valid) { 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); ReleaseArena(n->arena);
} }