gpu run state

This commit is contained in:
jacob 2025-06-02 16:26:54 -05:00
parent 88a1ad1450
commit ec77a62fda
4 changed files with 105 additions and 41 deletions

View File

@ -684,7 +684,7 @@ enum __prof_plot_type {
__prof_plot_type_percentage = TracyPlotFormatPercentage, __prof_plot_type_percentage = TracyPlotFormatPercentage,
__prof_plot_type_watt = TracyPlotFormatWatt __prof_plot_type_watt = TracyPlotFormatWatt
}; };
#define __prof_plot_init(name, type, step, fill, color) TracyCPlotConfig(name, type, step, fill, color) #define __prof_plot_init(name, type, step, fill, color) TracyCPlotConfig(name, type, step, fill, BGR(color))
#define __prof_plot(name, val) TracyCPlot(name, val) #define __prof_plot(name, val) TracyCPlot(name, val)
#define __prof_plot_i(name, val) TracyCPlotI(name, val) #define __prof_plot_i(name, val) TracyCPlotI(name, val)

View File

@ -115,6 +115,18 @@ void gpu_push_cmd(struct gpu_cmd_store gpu_cmd_store, struct gpu_cmd_params para
void gpu_submit_cmds(struct gpu_cmd_store gpu_cmd_store); void gpu_submit_cmds(struct gpu_cmd_store gpu_cmd_store);
void gpu_run_cmds(struct gpu_cmd_store gpu_cmd_store, struct gpu_texture target, struct xform view, struct rect viewport); /* ========================== *
* Run
* ========================== */
struct gpu_run_state {
u64 handle;
};
struct gpu_run_state gpu_run_state_alloc(void);
void gpu_run_state_release(struct gpu_run_state gpu_run_state);
void gpu_run_cmds(struct gpu_run_state gpu_run_state, struct gpu_cmd_store gpu_cmd_store, struct gpu_texture target, struct xform view, struct rect viewport);
#endif #endif

View File

@ -60,6 +60,11 @@ struct dx11_shader {
ID3D11PixelShader *ps; ID3D11PixelShader *ps;
}; };
struct dx11_run_state {
i32 _;
struct dx11_run_state *next_free;
};
struct dx11_buffer { struct dx11_buffer {
D3D11_BUFFER_DESC desc; D3D11_BUFFER_DESC desc;
@ -120,10 +125,12 @@ struct dx11_cmd {
}; };
struct dx11_cmd_store { struct dx11_cmd_store {
/* Commands w/ data still in cpu memory */
struct arena cpu_cmds_arena; struct arena cpu_cmds_arena;
struct dx11_cmd *cpu_first_cmd; struct dx11_cmd *cpu_first_cmd;
struct dx11_cmd *cpu_last_cmd; struct dx11_cmd *cpu_last_cmd;
/* Commands w/ buffer data submitted to video memory */
struct arena gpu_cmds_arena; struct arena gpu_cmds_arena;
struct dx11_cmd *gpu_first_cmd; struct dx11_cmd *gpu_first_cmd;
struct dx11_cmd *gpu_last_cmd; struct dx11_cmd *gpu_last_cmd;
@ -165,21 +172,6 @@ struct dx11_texture {
* Global state * Global state
* ========================== */ * ========================== */
struct handle_slot {
u64 idx;
u64 gen;
void *data;
struct handle_slot *next_free;
};
struct handle_store {
struct sys_mutex mutex;
struct arena arena;
struct handle_slot *head_free;
struct handle_slot *array;
u64 count;
};
struct dx11_shader_desc { struct dx11_shader_desc {
enum dx11_shader_kind kind; enum dx11_shader_kind kind;
char *name_cstr; char *name_cstr;
@ -222,6 +214,11 @@ GLOBAL struct {
struct arena cmd_stores_arena; struct arena cmd_stores_arena;
struct dx11_cmd_store *first_free_cmd_store; struct dx11_cmd_store *first_free_cmd_store;
/* Run state pool */
struct sys_mutex run_states_mutex;
struct arena run_states_arena;
struct dx11_run_state *first_free_run_state;
/* Texture pool */ /* Texture pool */
struct sys_mutex textures_mutex; struct sys_mutex textures_mutex;
struct arena textures_arena; struct arena textures_arena;
@ -275,6 +272,10 @@ struct gpu_startup_receipt gpu_startup(struct sys_window *window)
G.cmd_stores_mutex = sys_mutex_alloc(); G.cmd_stores_mutex = sys_mutex_alloc();
G.cmd_stores_arena = arena_alloc(GIGABYTE(64)); G.cmd_stores_arena = arena_alloc(GIGABYTE(64));
/* Initialize run state pool */
G.run_states_mutex = sys_mutex_alloc();
G.run_states_arena = arena_alloc(GIGABYTE(64));
/* Initialize texture pool */ /* Initialize texture pool */
G.textures_mutex = sys_mutex_alloc(); G.textures_mutex = sys_mutex_alloc();
G.textures_arena = arena_alloc(GIGABYTE(64)); G.textures_arena = arena_alloc(GIGABYTE(64));
@ -982,6 +983,7 @@ INTERNAL struct dx11_buffer *dx11_buffer_alloc(struct D3D11_BUFFER_DESC desc, D3
INTERNAL void dx11_buffer_release(struct dx11_buffer *buffer) INTERNAL void dx11_buffer_release(struct dx11_buffer *buffer)
{ {
(UNUSED)buffer; (UNUSED)buffer;
ASSERT(false);
} }
#endif #endif
@ -1081,7 +1083,6 @@ struct gpu_cmd_store gpu_cmd_store_alloc(void)
arena_reset(&store->gpu_cmds_arena); arena_reset(&store->gpu_cmds_arena);
} }
/* Desc templates */ /* Desc templates */
const D3D11_BUFFER_DESC constant_buffer_desc = { const D3D11_BUFFER_DESC constant_buffer_desc = {
.Usage = D3D11_USAGE_DYNAMIC, .Usage = D3D11_USAGE_DYNAMIC,
@ -1095,7 +1096,6 @@ struct gpu_cmd_store gpu_cmd_store_alloc(void)
.MiscFlags = D3D11_RESOURCE_MISC_BUFFER_STRUCTURED .MiscFlags = D3D11_RESOURCE_MISC_BUFFER_STRUCTURED
}; };
/* Allocate buffers */ /* Allocate buffers */
{ {
/* Constant buffer */ /* Constant buffer */
@ -1151,6 +1151,7 @@ void gpu_cmd_store_release(struct gpu_cmd_store gpu_cmd_store)
{ {
__prof; __prof;
/* TODO */ /* TODO */
ASSERT(false);
(UNUSED)gpu_cmd_store; (UNUSED)gpu_cmd_store;
} }
@ -1332,20 +1333,56 @@ void gpu_submit_cmds(struct gpu_cmd_store gpu_cmd_store)
dx11_buffer_submit(store->buffers.test.instance_buffer); dx11_buffer_submit(store->buffers.test.instance_buffer);
} }
/* ========================== *
* Run state
* ========================== */
struct gpu_run_state gpu_run_state_alloc(void)
{
__prof;
struct dx11_run_state *state = NULL;
{
{
struct sys_lock lock = sys_mutex_lock_e(&G.run_states_mutex);
if (G.first_free_run_state) {
state = G.first_free_run_state;
G.first_free_run_state = state->next_free;
} else {
state = arena_push_no_zero(&G.run_states_arena, struct dx11_run_state);
}
sys_mutex_unlock(&lock);
}
MEMZERO_STRUCT(state);
}
struct gpu_run_state res = ZI;
res.handle = (u64)state;
return res;
}
void gpu_run_state_release(struct gpu_run_state gpu_run_state)
{
/* TODO */
__prof;
ASSERT(false);
(UNUSED)gpu_run_state;
}
/* ========================== * /* ========================== *
* Run * Run
* ========================== */ * ========================== */
enum dx11_unbind_flags { enum dx11_unbind_flags {
DX11_UNBIND_NONE = 0, DX11_UNBIND_NONE = 0,
DX11_UNBIND_VS = (1 << 0), DX11_UNBIND_VS = (1 << 0),
DX11_UNBIND_PS = (1 << 1), DX11_UNBIND_PS = (1 << 1),
DX11_UNBIND_IA = (1 << 2), DX11_UNBIND_IA = (1 << 2),
DX11_UNBIND_CBUFF = (1 << 3), DX11_UNBIND_CBUFF = (1 << 3),
DX11_UNBIND_VBUFF = (1 << 4), DX11_UNBIND_VBUFF = (1 << 4),
DX11_UNBIND_IBUFF = (1 << 5), DX11_UNBIND_IBUFF = (1 << 5),
DX11_UNBIND_SRV = (1 << 6), DX11_UNBIND_SRV = (1 << 6),
DX11_UNBIND_RTV = (1 << 7) DX11_UNBIND_RTV = (1 << 7)
}; };
INTERNAL void dx11_unbind(u32 flags) INTERNAL void dx11_unbind(u32 flags)
@ -1367,7 +1404,7 @@ INTERNAL void dx11_unbind(u32 flags)
if (flags & DX11_UNBIND_VS) { if (flags & DX11_UNBIND_VS) {
ID3D11DeviceContext_VSSetShaderResources(G.devcon, 0, 8, null_srvs); ID3D11DeviceContext_VSSetShaderResources(G.devcon, 0, 8, null_srvs);
} }
if (flags & DX11_UNBIND_VS) { if (flags & DX11_UNBIND_PS) {
ID3D11DeviceContext_PSSetShaderResources(G.devcon, 0, 8, null_srvs); ID3D11DeviceContext_PSSetShaderResources(G.devcon, 0, 8, null_srvs);
} }
} }
@ -1403,11 +1440,16 @@ INTERNAL void dx11_unbind(u32 flags)
} }
/* TODO: Lock resources during run */ /* TODO: Lock resources during run */
void gpu_run_cmds(struct gpu_cmd_store gpu_cmd_store, struct gpu_texture target, struct xform view, struct rect viewport) void gpu_run_cmds(struct gpu_run_state gpu_run_state, struct gpu_cmd_store gpu_cmd_store, struct gpu_texture target, struct xform view, struct rect viewport)
{ {
__prof; __prof;
__profscope_dx11(G.profiling_ctx, Run, RGB_F(0.5, 0.2, 0.2)); __profscope_dx11(G.profiling_ctx, Run, RGB_F(0.5, 0.2, 0.2));
struct sprite_scope *sprite_scope = sprite_scope_begin(); struct sprite_scope *sprite_scope = sprite_scope_begin();
struct dx11_run_state *run_state = (struct dx11_run_state *)gpu_run_state.handle;
(UNUSED)run_state;
struct dx11_cmd_store *store = (struct dx11_cmd_store *)gpu_cmd_store.handle; struct dx11_cmd_store *store = (struct dx11_cmd_store *)gpu_cmd_store.handle;
struct dx11_texture *target_texture = (struct dx11_texture *)target.handle; struct dx11_texture *target_texture = (struct dx11_texture *)target.handle;
@ -1856,6 +1898,7 @@ void gpu_swap_backbuffer(i32 vsync)
{ {
__prof; __prof;
#if RESOURCE_RELOADING #if RESOURCE_RELOADING
/* Reload dirty shaders */
for (u64 i = DX11_SHADER_KIND_NONE + 1; i < NUM_DX11_SHADER_KINDS; ++i) { for (u64 i = DX11_SHADER_KIND_NONE + 1; i < NUM_DX11_SHADER_KINDS; ++i) {
struct dx11_shader_desc *desc = &G.shader_info[i]; struct dx11_shader_desc *desc = &G.shader_info[i];
if (shader_unset_dirty(desc)) { if (shader_unset_dirty(desc)) {
@ -1865,26 +1908,29 @@ void gpu_swap_backbuffer(i32 vsync)
#endif #endif
#if GSTAT_ENABLED || PROFILING #if GSTAT_ENABLED || PROFILING
/* Check vram usage */
{ {
struct DXGI_QUERY_VIDEO_MEMORY_INFO info = get_memory_info(); struct DXGI_QUERY_VIDEO_MEMORY_INFO info = get_memory_info();
u64 vram = info.CurrentUsage; u64 vram = info.CurrentUsage;
u64 budget = info.Budget;
(UNUSED)vram;
(UNUSED)budget;
# if GSTAT_ENABLED # if GSTAT_ENABLED
{ {
u64 budget = info.Budget;
gstat_set(GSTAT_VRAM_USAGE, vram); gstat_set(GSTAT_VRAM_USAGE, vram);
gstat_set(GSTAT_VRAM_BUDGET, budget); gstat_set(GSTAT_VRAM_BUDGET, budget);
} }
# endif # endif
# if PROFILING # if PROFILING
{ {
LOCAL_PERSIST char *plot_name = NULL; LOCAL_PERSIST char *vram_plot_name = NULL;
LOCAL_PERSIST u64 prev_vram = 0; LOCAL_PERSIST u64 prev_vram = 0;
if (!plot_name) { if (!vram_plot_name) {
plot_name = "Video memory usage"; vram_plot_name = "Video memory usage";
__prof_plot_init(plot_name, __prof_plot_type_memory, 1, 1, 0); __prof_plot_init(vram_plot_name, __prof_plot_type_memory, 1, 1, 0);
} }
if (vram != prev_vram) { if (vram != prev_vram) {
__prof_plot_i(plot_name, vram); __prof_plot_i(vram_plot_name, vram);
} }
prev_vram = vram; prev_vram = vram;
} }

View File

@ -70,7 +70,7 @@ GLOBAL struct {
struct second_stat net_bytes_read; struct second_stat net_bytes_read;
struct second_stat net_bytes_sent; struct second_stat net_bytes_sent;
/* Render targets */ /* Gpu handles */
struct gpu_texture user_texture; struct gpu_texture user_texture;
struct gpu_texture backbuffer_texture; struct gpu_texture backbuffer_texture;
@ -78,6 +78,9 @@ GLOBAL struct {
struct gpu_cmd_store user_gpu_cmd_store; struct gpu_cmd_store user_gpu_cmd_store;
struct gpu_cmd_store backbuffer_gpu_cmd_store; struct gpu_cmd_store backbuffer_gpu_cmd_store;
struct gpu_run_state user_gpu_run_state;
struct gpu_run_state backbuffer_gpu_run_state;
struct xform world_to_user_xf; struct xform world_to_user_xf;
struct bind_state bind_states[USER_BIND_KIND_COUNT]; struct bind_state bind_states[USER_BIND_KIND_COUNT];
@ -252,10 +255,13 @@ struct user_startup_receipt user_startup(struct work_startup_receipt *work_sr,
/* User sim control */ /* User sim control */
G.user_sim_cmd_mutex = sys_mutex_alloc(); G.user_sim_cmd_mutex = sys_mutex_alloc();
/* GPU handles */
G.world_to_user_xf = XFORM_IDENT; G.world_to_user_xf = XFORM_IDENT;
G.world_gpu_cmd_store = gpu_cmd_store_alloc(); G.world_gpu_cmd_store = gpu_cmd_store_alloc();
G.user_gpu_cmd_store = gpu_cmd_store_alloc(); G.user_gpu_cmd_store = gpu_cmd_store_alloc();
G.backbuffer_gpu_cmd_store = gpu_cmd_store_alloc(); G.backbuffer_gpu_cmd_store = gpu_cmd_store_alloc();
G.user_gpu_run_state = gpu_run_state_alloc();
G.backbuffer_gpu_run_state = gpu_run_state_alloc();
//log_register_callback(debug_console_log_callback, LOG_LEVEL_SUCCESS); //log_register_callback(debug_console_log_callback, LOG_LEVEL_SUCCESS);
log_register_callback(debug_console_log_callback, LOG_LEVEL_DEBUG); log_register_callback(debug_console_log_callback, LOG_LEVEL_DEBUG);
@ -2091,14 +2097,14 @@ INTERNAL void user_update(void)
gpu_texture_clear(G.user_texture, 0); gpu_texture_clear(G.user_texture, 0);
gpu_texture_clear(G.backbuffer_texture, RGBA_F(0, 0, 0, 1)); gpu_texture_clear(G.backbuffer_texture, RGBA_F(0, 0, 0, 1));
/* Execute cmds */ /* Run cmds */
{ {
/* Render to user texture */ /* Render to user texture */
gpu_run_cmds(G.world_gpu_cmd_store, G.user_texture, G.world_to_user_xf, user_viewport); gpu_run_cmds(G.user_gpu_run_state, G.world_gpu_cmd_store, G.user_texture, G.world_to_user_xf, user_viewport);
gpu_run_cmds(G.user_gpu_cmd_store, G.user_texture, XFORM_IDENT, user_viewport); gpu_run_cmds(G.user_gpu_run_state, G.user_gpu_cmd_store, G.user_texture, XFORM_IDENT, user_viewport);
/* Render to backbuffer */ /* Render to backbuffer */
gpu_run_cmds(G.backbuffer_gpu_cmd_store, G.backbuffer_texture, XFORM_IDENT, backbuffer_viewport); gpu_run_cmds(G.backbuffer_gpu_run_state, G.backbuffer_gpu_cmd_store, G.backbuffer_texture, XFORM_IDENT, backbuffer_viewport);
} }
/* Swap backbuffer */ /* Swap backbuffer */