gpu layer refactor progress
This commit is contained in:
parent
0ba8ca3538
commit
19c0140868
@ -233,7 +233,7 @@ void P_AppStartup(String args_str)
|
||||
/* Global systems */
|
||||
RES_Startup();
|
||||
W_Startup();
|
||||
gp_startup();
|
||||
GPU_Startup();
|
||||
|
||||
/* Subsystems */
|
||||
AC_StartupReceipt asset_cache_sr = AC_Startup();
|
||||
|
||||
@ -9,7 +9,7 @@ D_StartupReceipt D_Startup(F_StartupReceipt *font_sr)
|
||||
D_SharedState *g = &D_shared_state;
|
||||
(UNUSED)font_sr;
|
||||
u32 pixel_white = 0xFFFFFFFF;
|
||||
g->solid_white_texture = gp_texture_alloc(GP_TEXTURE_FORMAT_R8G8B8A8_UNORM, 0, VEC2I32(1, 1), &pixel_white);
|
||||
g->solid_white_texture = GPU_AllocTexture(GP_TEXTURE_FORMAT_R8G8B8A8_UNORM, 0, VEC2I32(1, 1), &pixel_white);
|
||||
return (D_StartupReceipt) { 0 };
|
||||
}
|
||||
|
||||
@ -26,7 +26,7 @@ void D_DrawMaterial(GPU_RenderSig *sig, D_MaterialParams params)
|
||||
cmd.material.tint = params.tint;
|
||||
cmd.material.is_light = params.is_light;
|
||||
cmd.material.light_emittance = params.light_emittance;
|
||||
gp_push_render_cmd(sig, &cmd);
|
||||
GPU_PushRenderCmd(sig, &cmd);
|
||||
}
|
||||
|
||||
////////////////////////////////
|
||||
@ -39,7 +39,7 @@ void D_DrawPolyEx(GPU_RenderSig *sig, Vec2Array vertices, GPU_Indices indices, u
|
||||
cmd.ui_shape.vertices = vertices;
|
||||
cmd.ui_shape.indices = indices;
|
||||
cmd.ui_shape.color = color;
|
||||
gp_push_render_cmd(sig, &cmd);
|
||||
GPU_PushRenderCmd(sig, &cmd);
|
||||
}
|
||||
|
||||
/* Draws a filled polygon using triangles in a fan pattern */
|
||||
@ -269,7 +269,7 @@ void D_DrawGrid(GPU_RenderSig *sig, Xform xf, u32 bg0_color, u32 bg1_color, u32
|
||||
cmd.grid.line_thickness = thickness;
|
||||
cmd.grid.line_spacing = spacing;
|
||||
cmd.grid.offset = offset;
|
||||
grid_id = gp_push_render_cmd(sig, &cmd);
|
||||
grid_id = GPU_PushRenderCmd(sig, &cmd);
|
||||
}
|
||||
|
||||
GPU_RenderCmdDesc cmd = ZI;
|
||||
@ -277,7 +277,7 @@ void D_DrawGrid(GPU_RenderSig *sig, Xform xf, u32 bg0_color, u32 bg1_color, u32
|
||||
cmd.material.xf = xf;
|
||||
cmd.material.tint = ColorWhite;
|
||||
cmd.material.grid_cmd_id = grid_id;
|
||||
gp_push_render_cmd(sig, &cmd);
|
||||
GPU_PushRenderCmd(sig, &cmd);
|
||||
}
|
||||
|
||||
////////////////////////////////
|
||||
@ -291,7 +291,7 @@ void D_DrawUiRect(GPU_RenderSig *sig, D_UiRectParams params)
|
||||
cmd.ui_rect.texture = params.texture;
|
||||
cmd.ui_rect.clip = params.clip;
|
||||
cmd.ui_rect.tint = params.tint;
|
||||
gp_push_render_cmd(sig, &cmd);
|
||||
GPU_PushRenderCmd(sig, &cmd);
|
||||
}
|
||||
|
||||
////////////////////////////////
|
||||
|
||||
@ -93,7 +93,7 @@ P_JobDef(F_LoadAssetJob, job)
|
||||
RES_CloseResource(&res);
|
||||
|
||||
/* Send texture to GPU */
|
||||
GPU_Resource *texture = gp_texture_alloc(GP_TEXTURE_FORMAT_R8G8B8A8_UNORM, 0, VEC2I32(result.image_width, result.image_height), result.image_pixels);
|
||||
GPU_Resource *texture = GPU_AllocTexture(GP_TEXTURE_FORMAT_R8G8B8A8_UNORM, 0, VEC2I32(result.image_width, result.image_height), result.image_pixels);
|
||||
|
||||
/* Allocate store memory */
|
||||
F_Font *font = 0;
|
||||
|
||||
@ -3,7 +3,7 @@
|
||||
#include "../kernel/kernel.h"
|
||||
|
||||
#if PlatformIsWindows
|
||||
# include "gpu_core_dx12.c"
|
||||
# include "gpu_dx12.c"
|
||||
#else
|
||||
# error Gp core not implemented for this platform
|
||||
# error Gpu layer not implemented for this platform
|
||||
#endif
|
||||
|
||||
@ -12,4 +12,8 @@
|
||||
|
||||
#include "gpu_core.h"
|
||||
|
||||
#if PlatformIsWindows
|
||||
# include "gpu_dx12.h"
|
||||
#endif
|
||||
|
||||
#endif
|
||||
|
||||
@ -110,7 +110,7 @@ Struct(GPU_MemoryInfo)
|
||||
////////////////////////////////
|
||||
//~ Startup
|
||||
|
||||
void gp_startup(void);
|
||||
void GPU_Startup(void);
|
||||
|
||||
////////////////////////////////
|
||||
//~ Resource operations
|
||||
@ -120,45 +120,42 @@ void gp_startup(void);
|
||||
* the caller to make sure the released resources aren't then referenced in
|
||||
* any runs
|
||||
*/
|
||||
void gp_resource_release(GPU_Resource *resource);
|
||||
void GPU_ReleaseResource(GPU_Resource *resource);
|
||||
|
||||
////////////////////////////////
|
||||
//~ Texture operations
|
||||
|
||||
GPU_Resource *gp_texture_alloc(GPU_TextureFormat format, u32 flags, Vec2I32 size, void *initial_data);
|
||||
GPU_Resource *GPU_AllocTexture(GPU_TextureFormat format, u32 flags, Vec2I32 size, void *initial_data);
|
||||
|
||||
Vec2I32 gp_texture_get_size(GPU_Resource *texture);
|
||||
Vec2I32 GPU_GetTextureSize(GPU_Resource *texture);
|
||||
|
||||
////////////////////////////////
|
||||
//~ Render operations
|
||||
|
||||
GPU_RenderSig *gp_render_sig_alloc(void);
|
||||
GPU_RenderSig *GPU_AllocRenderSig(void);
|
||||
|
||||
/* Returns a cmd id internal to the sig */
|
||||
u32 gp_push_render_cmd(GPU_RenderSig *render_sig, GPU_RenderCmdDesc *desc);
|
||||
u32 GPU_PushRenderCmd(GPU_RenderSig *render_sig, GPU_RenderCmdDesc *desc);
|
||||
|
||||
GPU_Resource *gp_run_render(GPU_RenderSig *gp_render_sig, GPU_RenderParams render_params);
|
||||
GPU_Resource *GPU_RunRender(GPU_RenderSig *gp_render_sig, GPU_RenderParams render_params);
|
||||
|
||||
////////////////////////////////
|
||||
//~ Memory query
|
||||
|
||||
GPU_MemoryInfo gp_query_memory_info(void);
|
||||
GPU_MemoryInfo GPU_QueryMemoryInfo(void);
|
||||
|
||||
////////////////////////////////
|
||||
//~ Swapchain
|
||||
|
||||
GPU_Swapchain *gp_swapchain_alloc(P_Window *window, Vec2I32 resolution);
|
||||
GPU_Swapchain *GPU_AllocSwapchain(P_Window *window, Vec2I32 resolution);
|
||||
|
||||
void gp_swapchain_release(GPU_Swapchain *gp_swapchain);
|
||||
void GPU_ReleaseSwapchain(GPU_Swapchain *gp_swapchain);
|
||||
|
||||
/* Waits until a new backbuffer is ready to be written to.
|
||||
* This should be called before rendering for minimum latency. */
|
||||
void gp_swapchain_wait(GPU_Swapchain *gp_swapchain);
|
||||
|
||||
////////////////////////////////
|
||||
//~ Present
|
||||
void GPU_WaitOnSwapchain(GPU_Swapchain *gp_swapchain);
|
||||
|
||||
/* 1. Clears the backbuffer and ensures it's at size `backbuffer_resolution`
|
||||
* 2. Blits `texture` to the backbuffer using `texture_xf`
|
||||
* 3. Presents the backbuffer */
|
||||
void gp_present(GPU_Swapchain *gp_swapchain, Vec2I32 backbuffer_resolution, GPU_Resource *texture, Xform texture_xf, i32 vsync);
|
||||
void GPU_PresentSwapchain(GPU_Swapchain *gp_swapchain, Vec2I32 backbuffer_resolution, GPU_Resource *texture, Xform texture_xf, i32 vsync);
|
||||
|
||||
File diff suppressed because it is too large
Load Diff
692
src/gpu/gpu_dx12.h
Normal file
692
src/gpu/gpu_dx12.h
Normal file
@ -0,0 +1,692 @@
|
||||
////////////////////////////////
|
||||
//~ D3D12 headers
|
||||
|
||||
#pragma warning(push, 0)
|
||||
# define UNICODE
|
||||
# define COBJMACROS
|
||||
# include <Windows.h>
|
||||
# include <d3d12.h>
|
||||
# include <dxgidebug.h>
|
||||
# include <dxgi1_6.h>
|
||||
# include <combaseapi.h>
|
||||
# include <d3dcompiler.h>
|
||||
#pragma warning(pop)
|
||||
|
||||
////////////////////////////////
|
||||
//~ Dx12
|
||||
|
||||
#define DX12_ALLOW_TEARING 1
|
||||
#define DX12_WAIT_FRAME_LATENCY 1
|
||||
#define DX12_SWAPCHAIN_FLAGS (((DX12_ALLOW_TEARING != 0) * DXGI_SWAP_CHAIN_FLAG_ALLOW_TEARING) | ((DX12_WAIT_FRAME_LATENCY != 0) * DXGI_SWAP_CHAIN_FLAG_FRAME_LATENCY_WAITABLE_OBJECT))
|
||||
#define DX12_SWAPCHAIN_BUFFER_COUNT (4)
|
||||
|
||||
/* Arbitrary limits */
|
||||
#define DX12_NUM_CBV_SRV_UAV_DESCRIPTORS (1024 * 64)
|
||||
#define DX12_NUM_RTV_DESCRIPTORS (1024 * 1)
|
||||
#define DX12_COMMAND_BUFFER_MIN_SIZE (1024 * 64)
|
||||
|
||||
#define DX12_MULTI_QUEUE !ProfilingIsEnabled
|
||||
#if DX12_MULTI_QUEUE
|
||||
# define DX12_QUEUE_DIRECT 0
|
||||
# define DX12_QUEUE_COMPUTE 1
|
||||
# define DX12_QUEUE_COPY 2
|
||||
# define DX12_QUEUE_COPY_BACKGROUND 3
|
||||
# define DX12_NUM_QUEUES 4
|
||||
#else
|
||||
# define DX12_QUEUE_DIRECT 0
|
||||
# define DX12_QUEUE_COMPUTE 0
|
||||
# define DX12_QUEUE_COPY 0
|
||||
# define DX12_QUEUE_COPY_BACKGROUND 0
|
||||
# define DX12_NUM_QUEUES 1
|
||||
#endif
|
||||
|
||||
#if RtcIsEnabled
|
||||
//# define DX12_DEBUG 1
|
||||
# define DX12_DEBUG 0
|
||||
#else
|
||||
# define DX12_DEBUG 0
|
||||
#endif
|
||||
|
||||
/* ========================== *
|
||||
* structs
|
||||
* ========================== */
|
||||
|
||||
struct shader_desc
|
||||
{
|
||||
String file;
|
||||
String func;
|
||||
};
|
||||
|
||||
struct pipeline_rtv_desc
|
||||
{
|
||||
DXGI_FORMAT format;
|
||||
b32 blending;
|
||||
};
|
||||
|
||||
struct pipeline_desc
|
||||
{
|
||||
String name;
|
||||
|
||||
/* If a dxc string is set, then it will be used directly instead of looking up dxc from archive using pipeline name */
|
||||
String vs_dxc;
|
||||
String ps_dxc;
|
||||
String cs_dxc;
|
||||
|
||||
struct pipeline_rtv_desc rtvs[8];
|
||||
};
|
||||
|
||||
struct pipeline
|
||||
{
|
||||
String name;
|
||||
u64 hash;
|
||||
b32 success;
|
||||
b32 is_gfx;
|
||||
String error;
|
||||
i64 compilation_time_ns;
|
||||
|
||||
/* Lock global pipelines mutex when accessing */
|
||||
i64 refcount;
|
||||
|
||||
ID3D12PipelineState *pso;
|
||||
ID3D12RootSignature *rootsig;
|
||||
struct pipeline_desc desc;
|
||||
|
||||
struct pipeline *next;
|
||||
};
|
||||
|
||||
struct pipeline_error
|
||||
{
|
||||
String msg;
|
||||
struct pipeline_error *next;
|
||||
};
|
||||
|
||||
struct pipeline_include
|
||||
{
|
||||
String name;
|
||||
u64 name_hash;
|
||||
struct pipeline_include *next;
|
||||
};
|
||||
|
||||
struct pipeline_scope
|
||||
{
|
||||
Arena *arena;
|
||||
Dict *refs;
|
||||
struct pipeline_scope *next_free;
|
||||
};
|
||||
|
||||
struct command_queue_desc
|
||||
{
|
||||
enum D3D12_COMMAND_LIST_TYPE type;
|
||||
enum D3D12_COMMAND_QUEUE_PRIORITY priority;
|
||||
String dbg_name;
|
||||
};
|
||||
|
||||
struct command_queue
|
||||
{
|
||||
struct command_queue_desc desc;
|
||||
ID3D12CommandQueue *cq;
|
||||
Arena *arena;
|
||||
|
||||
P_Mutex submit_fence_mutex;
|
||||
u64 submit_fence_target;
|
||||
ID3D12Fence *submit_fence;
|
||||
|
||||
struct command_list_pool *cl_pool;
|
||||
|
||||
#if ProfilingGpu
|
||||
__prof_dx12_ctx(prof);
|
||||
#endif
|
||||
};
|
||||
|
||||
struct command_list_pool
|
||||
{
|
||||
struct command_queue *cq;
|
||||
Arena *arena;
|
||||
P_Mutex mutex;
|
||||
struct command_list *first_submitted_command_list;
|
||||
struct command_list *last_submitted_command_list;
|
||||
};
|
||||
|
||||
struct command_list
|
||||
{
|
||||
struct command_queue *cq;
|
||||
struct command_list_pool *pool;
|
||||
struct ID3D12CommandAllocator *ca;
|
||||
struct ID3D12GraphicsCommandList *cl;
|
||||
P_Lock global_record_lock;
|
||||
|
||||
struct pipeline *cur_pipeline;
|
||||
|
||||
struct command_descriptor_heap *first_command_descriptor_heap;
|
||||
struct command_buffer *first_command_buffer;
|
||||
|
||||
u64 submitted_fence_target;
|
||||
struct command_list *prev_submitted;
|
||||
struct command_list *next_submitted;
|
||||
};
|
||||
|
||||
struct command_descriptor_heap
|
||||
{
|
||||
D3D12_DESCRIPTOR_HEAP_TYPE type;
|
||||
ID3D12DescriptorHeap *heap;
|
||||
D3D12_CPU_DESCRIPTOR_HANDLE start_cpu_handle;
|
||||
D3D12_GPU_DESCRIPTOR_HANDLE start_gpu_handle;
|
||||
|
||||
struct command_descriptor_heap *next_in_command_list;
|
||||
|
||||
u64 submitted_fence_target;
|
||||
struct command_queue *submitted_cq;
|
||||
struct command_descriptor_heap *prev_submitted;
|
||||
struct command_descriptor_heap *next_submitted;
|
||||
};
|
||||
|
||||
struct command_buffer
|
||||
{
|
||||
struct command_buffer_group *group;
|
||||
|
||||
u64 size;
|
||||
struct dx12_resource *resource;
|
||||
D3D12_VERTEX_BUFFER_VIEW vbv;
|
||||
D3D12_INDEX_BUFFER_VIEW Ibv;
|
||||
|
||||
struct command_buffer *next_in_command_list;
|
||||
|
||||
u64 submitted_fence_target;
|
||||
struct command_queue *submitted_cq;
|
||||
struct command_buffer *prev_submitted;
|
||||
struct command_buffer *next_submitted;
|
||||
};
|
||||
|
||||
struct command_buffer_group
|
||||
{
|
||||
struct command_buffer *first_submitted;
|
||||
struct command_buffer *last_submitted;
|
||||
};
|
||||
|
||||
struct descriptor
|
||||
{
|
||||
struct cpu_descriptor_heap *heap;
|
||||
|
||||
u32 index;
|
||||
D3D12_CPU_DESCRIPTOR_HANDLE handle;
|
||||
|
||||
struct descriptor *next_free;
|
||||
};
|
||||
|
||||
struct dx12_resource
|
||||
{
|
||||
enum D3D12_RESOURCE_STATES state;
|
||||
ID3D12Resource *resource;
|
||||
struct descriptor *cbv_descriptor;
|
||||
struct descriptor *srv_descriptor;
|
||||
struct descriptor *uav_descriptor;
|
||||
struct descriptor *rtv_descriptor;
|
||||
|
||||
D3D12_GPU_VIRTUAL_ADDRESS gpu_address; /* NOTE: 0 for textures */
|
||||
|
||||
Vec2I32 texture_size;
|
||||
struct dx12_resource *next_free;
|
||||
};
|
||||
|
||||
struct swapchain_buffer
|
||||
{
|
||||
struct swapchain *swapchain;
|
||||
ID3D12Resource *resource;
|
||||
struct descriptor *rtv_descriptor;
|
||||
D3D12_RESOURCE_STATES state;
|
||||
};
|
||||
|
||||
struct swapchain
|
||||
{
|
||||
IDXGISwapChain3 *swapchain;
|
||||
HWND hwnd;
|
||||
HANDLE waitable;
|
||||
Vec2I32 resolution;
|
||||
struct swapchain_buffer buffers[DX12_SWAPCHAIN_BUFFER_COUNT];
|
||||
|
||||
struct swapchain *next_free;
|
||||
};
|
||||
|
||||
struct cpu_descriptor_heap
|
||||
{
|
||||
enum D3D12_DESCRIPTOR_HEAP_TYPE type;
|
||||
Arena *arena;
|
||||
P_Mutex mutex;
|
||||
|
||||
u32 descriptor_size;
|
||||
u32 num_descriptors_reserved;
|
||||
u32 num_descriptors_capacity;
|
||||
|
||||
struct descriptor *first_free_descriptor;
|
||||
|
||||
ID3D12DescriptorHeap *heap;
|
||||
struct D3D12_CPU_DESCRIPTOR_HANDLE handle;
|
||||
};
|
||||
|
||||
enum fenced_release_kind
|
||||
{
|
||||
FENCED_RELEASE_KIND_NONE,
|
||||
FENCED_RELEASE_KIND_RESOURCE,
|
||||
FENCED_RELEASE_KIND_PIPELINE
|
||||
};
|
||||
|
||||
struct fenced_release_data
|
||||
{
|
||||
enum fenced_release_kind kind;
|
||||
void *ptr;
|
||||
};
|
||||
|
||||
struct command_queue_alloc_job_sig { struct command_queue_desc *descs_in; struct command_queue **cqs_out; };
|
||||
|
||||
struct pipeline_alloc_job_sig { struct pipeline_desc *descs_in; struct pipeline **pipelines_out; };
|
||||
|
||||
struct dx12_upload_job_sig { struct dx12_resource *resource; void *data; };
|
||||
|
||||
struct shader_compile_desc
|
||||
{
|
||||
String src;
|
||||
String friendly_name;
|
||||
String entry;
|
||||
String target;
|
||||
};
|
||||
|
||||
struct shader_compile_result
|
||||
{
|
||||
i64 elapsed_ns;
|
||||
String dxc;
|
||||
String errors;
|
||||
b32 success;
|
||||
};
|
||||
|
||||
struct shader_compile_job_sig
|
||||
{
|
||||
Arena *arena;
|
||||
struct shader_compile_desc *descs;
|
||||
struct shader_compile_result *results;
|
||||
};
|
||||
|
||||
|
||||
struct render_sig
|
||||
{
|
||||
Arena *arena;
|
||||
RandState rand;
|
||||
u32 frame_index;
|
||||
|
||||
/* Material instances */
|
||||
u32 num_material_instance_descs;
|
||||
Arena *material_instance_descs_arena;
|
||||
|
||||
/* Ui instances */
|
||||
u32 num_ui_rect_instance_descs;
|
||||
Arena *ui_rect_instance_descs_arena;
|
||||
|
||||
/* UI shapes */
|
||||
Arena *ui_shape_verts_arena;
|
||||
Arena *ui_shape_indices_arena;
|
||||
|
||||
/* Grids */
|
||||
u32 num_material_grid_descs;
|
||||
Arena *material_grid_descs_arena;
|
||||
|
||||
/* Resources */
|
||||
struct dx12_resource *albedo;
|
||||
struct dx12_resource *emittance;
|
||||
struct dx12_resource *emittance_flood_read;
|
||||
struct dx12_resource *emittance_flood_target;
|
||||
struct dx12_resource *shade_read;
|
||||
struct dx12_resource *shade_target;
|
||||
struct dx12_resource *ui_target;
|
||||
};
|
||||
|
||||
struct material_instance_desc
|
||||
{
|
||||
Xform xf;
|
||||
u32 texture_id;
|
||||
ClipRect clip;
|
||||
u32 tint;
|
||||
b32 is_light;
|
||||
Vec3 light_emittance;
|
||||
u32 grid_id;
|
||||
};
|
||||
|
||||
struct ui_rect_instance_desc
|
||||
{
|
||||
Xform xf;
|
||||
u32 texture_id;
|
||||
ClipRect clip;
|
||||
u32 tint;
|
||||
};
|
||||
|
||||
struct material_grid_desc
|
||||
{
|
||||
f32 line_thickness;
|
||||
f32 line_spacing;
|
||||
Vec2 offset;
|
||||
u32 bg0_color;
|
||||
u32 bg1_color;
|
||||
u32 line_color;
|
||||
u32 x_color;
|
||||
u32 y_color;
|
||||
};
|
||||
|
||||
/* ========================== *
|
||||
* Global state
|
||||
* ========================== */
|
||||
|
||||
Struct(GPU_D12_SharedState)
|
||||
{
|
||||
Atomic32 initialized;
|
||||
|
||||
/* Descriptor heaps pool */
|
||||
P_Mutex command_descriptor_heaps_mutex;
|
||||
Arena *command_descriptor_heaps_arena;
|
||||
struct command_descriptor_heap *first_submitted_command_descriptor_heap;
|
||||
struct command_descriptor_heap *last_submitted_command_descriptor_heap;
|
||||
|
||||
/* Command buffers pool */
|
||||
P_Mutex command_buffers_mutex;
|
||||
Arena *command_buffers_arena;
|
||||
Dict *command_buffers_dict;
|
||||
|
||||
/* Resources pool */
|
||||
P_Mutex resources_mutex;
|
||||
Arena *resources_arena;
|
||||
struct dx12_resource *first_free_resource;
|
||||
|
||||
/* Swapchains pool */
|
||||
P_Mutex swapchains_mutex;
|
||||
Arena *swapchains_arena;
|
||||
struct swapchain *first_free_swapchain;
|
||||
|
||||
/* Shader bytecode archive */
|
||||
TAR_Archive dxc_archive;
|
||||
|
||||
/* Pipeline cache */
|
||||
P_Mutex pipelines_mutex;
|
||||
Arena *pipelines_arena;
|
||||
struct pipeline *first_free_pipeline;
|
||||
Dict *pipeline_descs;
|
||||
Dict *top_pipelines; /* Latest pipelines */
|
||||
Dict *top_successful_pipelines; /* Latest pipelines that successfully compiled */
|
||||
struct pipeline_scope *first_free_pipeline_scope;
|
||||
|
||||
/* Fenced release queue */
|
||||
P_Mutex fenced_releases_mutex;
|
||||
Arena *fenced_releases_arena;
|
||||
u64 fenced_release_targets[DX12_NUM_QUEUES];
|
||||
|
||||
/* Factory */
|
||||
IDXGIFactory6 *factory;
|
||||
|
||||
/* Adapter */
|
||||
IDXGIAdapter1 *adapter;
|
||||
|
||||
/* Device */
|
||||
ID3D12Device *device;
|
||||
|
||||
/* Descriptor sizes */
|
||||
u32 desc_sizes[D3D12_DESCRIPTOR_HEAP_TYPE_NUM_TYPES];
|
||||
u32 desc_counts[D3D12_DESCRIPTOR_HEAP_TYPE_NUM_TYPES];
|
||||
|
||||
/* Global descriptor heaps */
|
||||
struct cpu_descriptor_heap *cbv_srv_uav_heap;
|
||||
struct cpu_descriptor_heap *rtv_heap;
|
||||
|
||||
/* Command queues */
|
||||
P_Mutex global_command_list_record_mutex;
|
||||
P_Mutex global_submit_mutex;
|
||||
struct command_queue *command_queues[DX12_NUM_QUEUES];
|
||||
|
||||
/* Evictor job */
|
||||
P_Counter evictor_job_counter;
|
||||
P_Cv evictor_wake_cv;
|
||||
P_Mutex evictor_wake_mutex;
|
||||
i64 evictor_wake_gen;
|
||||
b32 evictor_shutdown;
|
||||
};
|
||||
|
||||
extern GPU_D12_SharedState GPU_D12_shared_state;
|
||||
|
||||
/* ========================== *
|
||||
* Startup
|
||||
* ========================== */
|
||||
|
||||
void GPU_Startup(void);
|
||||
|
||||
P_ExitFuncDef(gp_shutdown);
|
||||
|
||||
/* ========================== *
|
||||
* Dx12 device initialization
|
||||
* ========================== */
|
||||
|
||||
void dx12_init_error(String error);
|
||||
|
||||
void dx12_init_device(void);
|
||||
|
||||
/* ========================== *
|
||||
* Dx12 object initialization
|
||||
* ========================== */
|
||||
|
||||
void dx12_init_objects(void);
|
||||
|
||||
/* ========================== *
|
||||
* Dx12 pipeline initialization
|
||||
* ========================== */
|
||||
|
||||
void dx12_init_pipelines(void);
|
||||
|
||||
/* ========================== *
|
||||
* Noise texture initialization
|
||||
* ========================== */
|
||||
|
||||
void dx12_init_noise(void);
|
||||
|
||||
/* ========================== *
|
||||
* Shader compilation
|
||||
* ========================== */
|
||||
|
||||
P_JobDef(shader_compile_job, job);
|
||||
|
||||
/* ========================== *
|
||||
* Pipeline
|
||||
* ========================== */
|
||||
|
||||
P_JobDef(pipeline_alloc_job, job);
|
||||
|
||||
void pipeline_release_now(struct pipeline *pipeline);
|
||||
|
||||
/* ========================== *
|
||||
* Pipeline cache
|
||||
* ========================== */
|
||||
|
||||
struct pipeline_scope *pipeline_scope_begin(void);
|
||||
|
||||
void pipeline_scope_end(struct pipeline_scope *scope);
|
||||
|
||||
extern Readonly struct pipeline g_nil_pipeline;
|
||||
struct pipeline *pipeline_from_name(struct pipeline_scope *scope, String name);
|
||||
|
||||
void pipeline_register(u64 num_pipelines, struct pipeline **pipelines);
|
||||
|
||||
W_CallbackFuncDef(pipeline_watch_callback, name);
|
||||
|
||||
/* ========================== *
|
||||
* Descriptor
|
||||
* ========================== */
|
||||
|
||||
struct descriptor *descriptor_alloc(struct cpu_descriptor_heap *dh);
|
||||
|
||||
void descriptor_release(struct descriptor *descriptor);
|
||||
|
||||
/* ========================== *
|
||||
* CPU descriptor heap
|
||||
* ========================== */
|
||||
|
||||
struct cpu_descriptor_heap *cpu_descriptor_heap_alloc(enum D3D12_DESCRIPTOR_HEAP_TYPE type);
|
||||
|
||||
/* ========================== *
|
||||
* Fenced release
|
||||
* ========================== */
|
||||
|
||||
void fenced_release(void *data, enum fenced_release_kind kind);
|
||||
|
||||
/* ========================== *
|
||||
* Resource
|
||||
* ========================== */
|
||||
|
||||
struct dx12_resource *dx12_resource_alloc(D3D12_HEAP_PROPERTIES heap_props, D3D12_HEAP_FLAGS heap_flags, D3D12_RESOURCE_DESC desc, D3D12_RESOURCE_STATES initial_state);
|
||||
|
||||
void dx12_resource_release_now(struct dx12_resource *t);
|
||||
|
||||
void GPU_ReleaseResource(GPU_Resource *resource);
|
||||
|
||||
/* ========================== *
|
||||
* Resource barrier
|
||||
* ========================== */
|
||||
|
||||
struct dx12_resource_barrier_desc
|
||||
{
|
||||
enum D3D12_RESOURCE_BARRIER_TYPE type;
|
||||
struct dx12_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
|
||||
* ========================== */
|
||||
|
||||
P_JobDef(command_queue_alloc_job, job);
|
||||
|
||||
void command_queue_release(struct command_queue *cq);
|
||||
|
||||
/* ========================== *
|
||||
* Command list
|
||||
* ========================== */
|
||||
|
||||
struct command_list_pool *command_list_pool_alloc(struct command_queue *cq);
|
||||
|
||||
struct command_list *command_list_open(struct command_list_pool *pool);
|
||||
|
||||
/* TODO: Allow multiple command list submissions */
|
||||
u64 command_list_close(struct command_list *cl);
|
||||
|
||||
/* ========================== *
|
||||
* Command descriptor heap (GPU / shader visible descriptor heap)
|
||||
* ========================== */
|
||||
|
||||
struct command_descriptor_heap *command_list_push_descriptor_heap(struct command_list *cl, struct cpu_descriptor_heap *dh_cpu);
|
||||
|
||||
/* ========================== *
|
||||
* Command buffer
|
||||
* ========================== */
|
||||
|
||||
u64 command_buffer_hash_from_size(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)
|
||||
struct command_buffer *_command_list_push_buffer(struct command_list *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);
|
||||
|
||||
/* ========================== *
|
||||
* Upload
|
||||
* ========================== */
|
||||
|
||||
P_JobDef(dx12_upload_job, job);
|
||||
|
||||
/* ========================== *
|
||||
* Run utils
|
||||
* ========================== */
|
||||
|
||||
void command_list_set_pipeline(struct command_list *cl, struct pipeline *pipeline);
|
||||
|
||||
|
||||
void command_list_set_sig(struct command_list *cl, void *src, u32 size);
|
||||
|
||||
struct D3D12_VIEWPORT viewport_from_rect(Rect r);
|
||||
|
||||
D3D12_RECT scissor_from_rect(Rect r);
|
||||
|
||||
D3D12_VERTEX_BUFFER_VIEW vbv_from_command_buffer(struct command_buffer *cb, u32 vertex_size);
|
||||
|
||||
D3D12_INDEX_BUFFER_VIEW ibv_from_command_buffer(struct command_buffer *cb, DXGI_FORMAT format);
|
||||
|
||||
struct dx12_resource *gbuff_alloc(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(struct descriptor *descriptor, struct command_descriptor_heap *cdh);
|
||||
|
||||
/* ========================== *
|
||||
* Render sig
|
||||
* ========================== */
|
||||
|
||||
struct render_sig *render_sig_alloc(void);
|
||||
|
||||
void render_sig_reset(struct render_sig *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(struct 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);
|
||||
|
||||
struct swapchain_buffer *update_swapchain(struct swapchain *swapchain, Vec2I32 resolution);
|
||||
|
||||
/* ========================== *
|
||||
* Present
|
||||
* ========================== */
|
||||
|
||||
void present_blit(struct swapchain_buffer *dst, struct dx12_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, _);
|
||||
@ -206,7 +206,7 @@ S_StartupReceipt sprite_startup(void)
|
||||
u32 width = 64;
|
||||
u32 height = 64;
|
||||
u32 *pixels = generate_purple_black_image(scratch.arena, width, height);
|
||||
G.nil_texture->gp_texture = gp_texture_alloc(GP_TEXTURE_FORMAT_R8G8B8A8_UNORM, 0, VEC2I32(width, height), pixels);
|
||||
G.nil_texture->gp_texture = GPU_AllocTexture(GP_TEXTURE_FORMAT_R8G8B8A8_UNORM, 0, VEC2I32(width, height), pixels);
|
||||
EndScratch(scratch);
|
||||
}
|
||||
|
||||
@ -355,7 +355,7 @@ internal void cache_entry_load_texture(struct cache_ref ref, S_Tag tag)
|
||||
e->texture->height = decoded.height;
|
||||
e->texture->valid = 1;
|
||||
e->texture->loaded = 1;
|
||||
e->texture->gp_texture = gp_texture_alloc(GP_TEXTURE_FORMAT_R8G8B8A8_UNORM_SRGB, 0, VEC2I32(decoded.width, decoded.height), decoded.pixels);
|
||||
e->texture->gp_texture = GPU_AllocTexture(GP_TEXTURE_FORMAT_R8G8B8A8_UNORM_SRGB, 0, VEC2I32(decoded.width, decoded.height), decoded.pixels);
|
||||
/* TODO: Query gpu for more accurate texture size in VRAM */
|
||||
memory_size += (decoded.width * decoded.height) * sizeof(*decoded.pixels);
|
||||
success = 1;
|
||||
@ -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) {
|
||||
gp_resource_release(n->texture->gp_texture);
|
||||
GPU_ReleaseResource(n->texture->gp_texture);
|
||||
}
|
||||
ReleaseArena(n->arena);
|
||||
}
|
||||
|
||||
@ -220,14 +220,14 @@ struct user_startup_receipt user_startup(F_StartupReceipt *font_sr,
|
||||
/* GPU handles */
|
||||
G.world_to_ui_xf = XformIdentity;
|
||||
G.world_to_render_xf = XformIdentity;
|
||||
G.render_sig = gp_render_sig_alloc();
|
||||
G.render_sig = GPU_AllocRenderSig();
|
||||
|
||||
G.console_logs_arena = AllocArena(Gibi(64));
|
||||
//P_RegisterLogCallback(debug_console_log_callback, P_LogLevel_Success);
|
||||
P_RegisterLogCallback(debug_console_log_callback, P_LogLevel_Debug);
|
||||
|
||||
G.window = P_AllocWindow();
|
||||
G.swapchain = gp_swapchain_alloc(G.window, VEC2I32(100, 100));
|
||||
G.swapchain = GPU_AllocSwapchain(G.window, VEC2I32(100, 100));
|
||||
P_ShowWindow(G.window);
|
||||
|
||||
/* Start jobs */
|
||||
@ -1917,7 +1917,7 @@ internal void user_update(P_Window *window)
|
||||
* Query vram
|
||||
* ========================== */
|
||||
|
||||
GPU_MemoryInfo vram = gp_query_memory_info();
|
||||
GPU_MemoryInfo vram = GPU_QueryMemoryInfo();
|
||||
|
||||
/* ========================== *
|
||||
* Draw global debug info
|
||||
@ -2063,11 +2063,11 @@ internal void user_update(P_Window *window)
|
||||
params.world_to_render_xf = G.world_to_render_xf;
|
||||
params.render_to_ui_xf = G.render_to_ui_xf;
|
||||
params.effects_disabled = effects_disabled;
|
||||
render_texture = gp_run_render(G.render_sig, params);
|
||||
render_texture = GPU_RunRender(G.render_sig, params);
|
||||
}
|
||||
|
||||
/* Present */
|
||||
gp_present(G.swapchain, backbuffer_resolution, render_texture, G.ui_to_screen_xf, VSYNC);
|
||||
GPU_PresentSwapchain(G.swapchain, backbuffer_resolution, render_texture, G.ui_to_screen_xf, VSYNC);
|
||||
}
|
||||
|
||||
/* ========================== *
|
||||
@ -2088,7 +2088,7 @@ internal P_JobDef(user_update_job, _)
|
||||
__profn("User sleep");
|
||||
{
|
||||
__profn("Swapchain wait");
|
||||
gp_swapchain_wait(G.swapchain);
|
||||
GPU_WaitOnSwapchain(G.swapchain);
|
||||
}
|
||||
{
|
||||
__profn("Frame limiter wait");
|
||||
|
||||
Loading…
Reference in New Issue
Block a user