diff --git a/src/common.h b/src/common.h index 682130e5..73f89c6a 100644 --- a/src/common.h +++ b/src/common.h @@ -684,7 +684,7 @@ enum __prof_plot_type { __prof_plot_type_percentage = TracyPlotFormatPercentage, __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_i(name, val) TracyCPlotI(name, val) diff --git a/src/gpu.h b/src/gpu.h index 4c9d08c7..fd30b29b 100644 --- a/src/gpu.h +++ b/src/gpu.h @@ -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_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 diff --git a/src/gpu_dx11.c b/src/gpu_dx11.c index 1cf86161..9d22c3d7 100644 --- a/src/gpu_dx11.c +++ b/src/gpu_dx11.c @@ -60,6 +60,11 @@ struct dx11_shader { ID3D11PixelShader *ps; }; +struct dx11_run_state { + i32 _; + struct dx11_run_state *next_free; +}; + struct dx11_buffer { D3D11_BUFFER_DESC desc; @@ -120,10 +125,12 @@ struct dx11_cmd { }; struct dx11_cmd_store { + /* Commands w/ data still in cpu memory */ struct arena cpu_cmds_arena; struct dx11_cmd *cpu_first_cmd; struct dx11_cmd *cpu_last_cmd; + /* Commands w/ buffer data submitted to video memory */ struct arena gpu_cmds_arena; struct dx11_cmd *gpu_first_cmd; struct dx11_cmd *gpu_last_cmd; @@ -165,21 +172,6 @@ struct dx11_texture { * 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 { enum dx11_shader_kind kind; char *name_cstr; @@ -222,6 +214,11 @@ GLOBAL struct { struct arena cmd_stores_arena; 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 */ struct sys_mutex textures_mutex; 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_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 */ G.textures_mutex = sys_mutex_alloc(); 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) { (UNUSED)buffer; + ASSERT(false); } #endif @@ -1081,7 +1083,6 @@ struct gpu_cmd_store gpu_cmd_store_alloc(void) arena_reset(&store->gpu_cmds_arena); } - /* Desc templates */ const D3D11_BUFFER_DESC constant_buffer_desc = { .Usage = D3D11_USAGE_DYNAMIC, @@ -1095,7 +1096,6 @@ struct gpu_cmd_store gpu_cmd_store_alloc(void) .MiscFlags = D3D11_RESOURCE_MISC_BUFFER_STRUCTURED }; - /* Allocate buffers */ { /* Constant buffer */ @@ -1151,6 +1151,7 @@ void gpu_cmd_store_release(struct gpu_cmd_store gpu_cmd_store) { __prof; /* TODO */ + ASSERT(false); (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); } +/* ========================== * + * 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 * ========================== */ enum dx11_unbind_flags { - DX11_UNBIND_NONE = 0, - DX11_UNBIND_VS = (1 << 0), - DX11_UNBIND_PS = (1 << 1), - DX11_UNBIND_IA = (1 << 2), - DX11_UNBIND_CBUFF = (1 << 3), - DX11_UNBIND_VBUFF = (1 << 4), - DX11_UNBIND_IBUFF = (1 << 5), - DX11_UNBIND_SRV = (1 << 6), - DX11_UNBIND_RTV = (1 << 7) + DX11_UNBIND_NONE = 0, + DX11_UNBIND_VS = (1 << 0), + DX11_UNBIND_PS = (1 << 1), + DX11_UNBIND_IA = (1 << 2), + DX11_UNBIND_CBUFF = (1 << 3), + DX11_UNBIND_VBUFF = (1 << 4), + DX11_UNBIND_IBUFF = (1 << 5), + DX11_UNBIND_SRV = (1 << 6), + DX11_UNBIND_RTV = (1 << 7) }; INTERNAL void dx11_unbind(u32 flags) @@ -1367,7 +1404,7 @@ INTERNAL void dx11_unbind(u32 flags) if (flags & DX11_UNBIND_VS) { 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); } } @@ -1403,11 +1440,16 @@ INTERNAL void dx11_unbind(u32 flags) } /* 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; __profscope_dx11(G.profiling_ctx, Run, RGB_F(0.5, 0.2, 0.2)); 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_texture *target_texture = (struct dx11_texture *)target.handle; @@ -1856,6 +1898,7 @@ void gpu_swap_backbuffer(i32 vsync) { __prof; #if RESOURCE_RELOADING + /* Reload dirty shaders */ for (u64 i = DX11_SHADER_KIND_NONE + 1; i < NUM_DX11_SHADER_KINDS; ++i) { struct dx11_shader_desc *desc = &G.shader_info[i]; if (shader_unset_dirty(desc)) { @@ -1865,26 +1908,29 @@ void gpu_swap_backbuffer(i32 vsync) #endif #if GSTAT_ENABLED || PROFILING + /* Check vram usage */ { struct DXGI_QUERY_VIDEO_MEMORY_INFO info = get_memory_info(); u64 vram = info.CurrentUsage; + u64 budget = info.Budget; + (UNUSED)vram; + (UNUSED)budget; # if GSTAT_ENABLED { - u64 budget = info.Budget; gstat_set(GSTAT_VRAM_USAGE, vram); gstat_set(GSTAT_VRAM_BUDGET, budget); } # endif # if PROFILING { - LOCAL_PERSIST char *plot_name = NULL; + LOCAL_PERSIST char *vram_plot_name = NULL; LOCAL_PERSIST u64 prev_vram = 0; - if (!plot_name) { - plot_name = "Video memory usage"; - __prof_plot_init(plot_name, __prof_plot_type_memory, 1, 1, 0); + if (!vram_plot_name) { + vram_plot_name = "Video memory usage"; + __prof_plot_init(vram_plot_name, __prof_plot_type_memory, 1, 1, 0); } if (vram != prev_vram) { - __prof_plot_i(plot_name, vram); + __prof_plot_i(vram_plot_name, vram); } prev_vram = vram; } diff --git a/src/user.c b/src/user.c index bb688512..a07a6221 100644 --- a/src/user.c +++ b/src/user.c @@ -70,7 +70,7 @@ GLOBAL struct { struct second_stat net_bytes_read; struct second_stat net_bytes_sent; - /* Render targets */ + /* Gpu handles */ struct gpu_texture user_texture; struct gpu_texture backbuffer_texture; @@ -78,6 +78,9 @@ GLOBAL struct { struct gpu_cmd_store user_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 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 */ G.user_sim_cmd_mutex = sys_mutex_alloc(); + /* GPU handles */ G.world_to_user_xf = XFORM_IDENT; G.world_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.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_DEBUG); @@ -2091,14 +2097,14 @@ INTERNAL void user_update(void) gpu_texture_clear(G.user_texture, 0); gpu_texture_clear(G.backbuffer_texture, RGBA_F(0, 0, 0, 1)); - /* Execute cmds */ + /* Run cmds */ { /* 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_cmd_store, G.user_texture, XFORM_IDENT, 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_run_state, G.user_gpu_cmd_store, G.user_texture, XFORM_IDENT, user_viewport); /* 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 */