2024-01-21 20:38:02 -08:00
|
|
|
package sectr
|
|
|
|
|
2024-02-08 07:50:36 -08:00
|
|
|
import "base:runtime"
|
|
|
|
import "core:fmt"
|
|
|
|
import "core:mem"
|
2024-01-29 23:27:45 -08:00
|
|
|
import "core:mem/virtual"
|
2024-02-08 07:50:36 -08:00
|
|
|
import "core:os"
|
|
|
|
|
|
|
|
import rl "vendor:raylib"
|
2024-01-29 23:27:45 -08:00
|
|
|
|
2024-05-06 20:29:47 -07:00
|
|
|
Str_App_State := "App State"
|
|
|
|
|
2024-05-12 22:52:55 -07:00
|
|
|
#region("Memory")
|
|
|
|
|
2024-02-27 04:50:57 -08:00
|
|
|
Memory_App : Memory
|
2024-01-29 23:27:45 -08:00
|
|
|
|
2024-03-07 12:57:05 -08:00
|
|
|
Memory_Base_Address_Persistent :: Terabyte * 1
|
|
|
|
Memory_Base_Address_Frame :: Memory_Base_Address_Persistent + Memory_Reserve_Persistent * 2
|
|
|
|
Memory_Base_Address_Transient :: Memory_Base_Address_Frame + Memory_Reserve_Frame * 2
|
|
|
|
Memory_Base_Address_Files_Buffer :: Memory_Base_Address_Transient + Memory_Reserve_Transient * 2
|
|
|
|
|
|
|
|
// This reserve goes beyond the typical amount of ram the user has,
|
|
|
|
// TODO(Ed): Setup warnings when the amount is heading toward half the ram size
|
|
|
|
Memory_Reserve_Persistent :: 32 * Gigabyte
|
|
|
|
Memory_Reserve_Frame :: 16 * Gigabyte
|
|
|
|
Memory_Reserve_Transient :: 16 * Gigabyte
|
|
|
|
Memory_Reserve_FilesBuffer :: 64 * Gigabyte
|
2024-03-05 07:15:44 -08:00
|
|
|
|
2024-03-07 12:57:05 -08:00
|
|
|
Memory_Commit_Initial_Persistent :: 4 * Kilobyte
|
|
|
|
Memory_Commit_Initial_Frame :: 4 * Kilobyte
|
|
|
|
Memory_Commit_Initial_Transient :: 4 * Kilobyte
|
|
|
|
Memory_Commit_Initial_Filebuffer :: 4 * Kilobyte
|
2024-03-05 07:15:44 -08:00
|
|
|
|
2024-03-07 12:57:05 -08:00
|
|
|
MemorySnapshot :: struct {
|
|
|
|
persistent : []u8,
|
|
|
|
frame : []u8,
|
|
|
|
transient : []u8,
|
|
|
|
// files_buffer cannot be restored from snapshot
|
|
|
|
}
|
2024-02-08 13:05:15 -08:00
|
|
|
|
2024-03-07 12:57:05 -08:00
|
|
|
Memory :: struct {
|
|
|
|
persistent : ^VArena,
|
|
|
|
frame : ^VArena,
|
|
|
|
transient : ^VArena,
|
|
|
|
files_buffer : ^VArena,
|
2024-02-13 14:16:39 -08:00
|
|
|
|
2024-05-06 20:29:47 -07:00
|
|
|
state : ^State,
|
|
|
|
|
2024-03-07 12:57:05 -08:00
|
|
|
// Should only be used for small memory allocation iterations
|
|
|
|
// Not for large memory env states
|
|
|
|
snapshot : MemorySnapshot,
|
2024-02-13 14:16:39 -08:00
|
|
|
|
2024-03-10 23:05:18 -07:00
|
|
|
replay : ReplayState,
|
|
|
|
logger : Logger,
|
|
|
|
profiler : ^SpallProfiler
|
2024-02-13 14:16:39 -08:00
|
|
|
}
|
|
|
|
|
2024-03-07 12:57:05 -08:00
|
|
|
persistent_allocator :: proc() -> Allocator {
|
2024-03-20 19:34:18 -07:00
|
|
|
result := varena_allocator( Memory_App.persistent )
|
|
|
|
return result
|
2024-02-13 14:16:39 -08:00
|
|
|
}
|
|
|
|
|
2024-03-07 12:57:05 -08:00
|
|
|
frame_allocator :: proc() -> Allocator {
|
2024-03-20 19:34:18 -07:00
|
|
|
result := varena_allocator( Memory_App.frame )
|
|
|
|
return result
|
2024-02-13 14:16:39 -08:00
|
|
|
}
|
|
|
|
|
2024-02-23 06:36:23 -08:00
|
|
|
transient_allocator :: proc() -> Allocator {
|
2024-03-20 19:34:18 -07:00
|
|
|
result := varena_allocator( Memory_App.transient )
|
|
|
|
return result
|
2024-02-13 14:16:39 -08:00
|
|
|
}
|
|
|
|
|
2024-03-07 12:57:05 -08:00
|
|
|
files_buffer_allocator :: proc() -> Allocator {
|
2024-03-20 19:34:18 -07:00
|
|
|
result := varena_allocator( Memory_App.files_buffer )
|
|
|
|
return result
|
2024-01-29 23:27:45 -08:00
|
|
|
}
|
|
|
|
|
2024-03-10 07:31:21 -07:00
|
|
|
persistent_slab_allocator :: proc() -> Allocator {
|
2024-03-20 19:34:18 -07:00
|
|
|
state := get_state()
|
|
|
|
result := slab_allocator( state.persistent_slab )
|
|
|
|
return result
|
2024-03-10 07:31:21 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
frame_slab_allocator :: proc() -> Allocator {
|
2024-03-20 19:34:18 -07:00
|
|
|
result := slab_allocator( get_state().frame_slab )
|
|
|
|
return result
|
2024-03-10 07:31:21 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
transient_slab_allocator :: proc() -> Allocator {
|
2024-03-20 19:34:18 -07:00
|
|
|
result := slab_allocator( get_state().transient_slab )
|
|
|
|
return result
|
2024-02-09 10:09:58 -08:00
|
|
|
}
|
|
|
|
|
2024-03-07 12:57:05 -08:00
|
|
|
// TODO(Ed) : Implment host memory mapping api
|
|
|
|
save_snapshot :: proc( snapshot : ^MemorySnapshot )
|
|
|
|
{
|
|
|
|
// Make sure the snapshot size is able to hold the current size of the arenas
|
|
|
|
// Grow the files & mapping otherwise
|
|
|
|
{
|
|
|
|
// TODO(Ed) : Implement eventually
|
|
|
|
}
|
|
|
|
|
|
|
|
persistent := Memory_App.persistent
|
|
|
|
mem.copy_non_overlapping( & snapshot.persistent[0], persistent.reserve_start, int(persistent.commit_used) )
|
|
|
|
|
|
|
|
frame := Memory_App.frame
|
|
|
|
mem.copy_non_overlapping( & snapshot.frame[0], frame.reserve_start, int(frame.commit_used) )
|
|
|
|
|
|
|
|
transient := Memory_App.transient
|
|
|
|
mem.copy_non_overlapping( & snapshot.transient[0], transient.reserve_start, int(transient.commit_used) )
|
2024-02-09 10:09:58 -08:00
|
|
|
}
|
|
|
|
|
2024-03-07 12:57:05 -08:00
|
|
|
// TODO(Ed) : Implment host memory mapping api
|
|
|
|
load_snapshot :: proc( snapshot : ^MemorySnapshot ) {
|
|
|
|
persistent := Memory_App.persistent
|
|
|
|
mem.copy_non_overlapping( persistent.reserve_start, & snapshot.persistent[0], int(persistent.commit_used) )
|
|
|
|
|
|
|
|
frame := Memory_App.frame
|
|
|
|
mem.copy_non_overlapping( frame.reserve_start, & snapshot.frame[0], int(frame.commit_used) )
|
|
|
|
|
|
|
|
transient := Memory_App.transient
|
|
|
|
mem.copy_non_overlapping( transient.reserve_start, & snapshot.transient[0], int(transient.commit_used) )
|
|
|
|
}
|
|
|
|
|
|
|
|
// TODO(Ed) : Implement usage of this
|
2024-03-05 07:15:44 -08:00
|
|
|
MemoryConfig :: struct {
|
|
|
|
reserve_persistent : uint,
|
|
|
|
reserve_frame : uint,
|
|
|
|
reserve_transient : uint,
|
|
|
|
reserve_filebuffer : uint,
|
|
|
|
|
|
|
|
commit_initial_persistent : uint,
|
|
|
|
commit_initial_frame : uint,
|
|
|
|
commit_initial_transient : uint,
|
|
|
|
commit_initial_filebuffer : uint,
|
|
|
|
}
|
|
|
|
|
2024-05-12 22:52:55 -07:00
|
|
|
#endregion("Memory")
|
|
|
|
|
|
|
|
#region("State")
|
|
|
|
|
2024-05-11 19:38:05 -07:00
|
|
|
// ALl nobs available for this application
|
2024-02-11 20:00:06 -08:00
|
|
|
AppConfig :: struct {
|
2024-03-05 07:15:44 -08:00
|
|
|
using memory : MemoryConfig,
|
|
|
|
|
2024-02-11 20:00:06 -08:00
|
|
|
resolution_width : uint,
|
|
|
|
resolution_height : uint,
|
2024-02-13 14:16:39 -08:00
|
|
|
refresh_rate : uint,
|
2024-03-08 00:34:21 -08:00
|
|
|
|
|
|
|
cam_min_zoom : f32,
|
|
|
|
cam_max_zoom : f32,
|
|
|
|
cam_zoom_mode : CameraZoomMode,
|
|
|
|
cam_zoom_smooth_snappiness : f32,
|
|
|
|
cam_zoom_sensitivity_smooth : f32,
|
|
|
|
cam_zoom_sensitivity_digital : f32,
|
|
|
|
|
2024-03-08 15:45:08 -08:00
|
|
|
engine_refresh_hz : uint,
|
|
|
|
|
2024-03-14 21:02:28 -07:00
|
|
|
timing_fps_moving_avg_alpha : f32,
|
|
|
|
|
2024-03-09 11:24:02 -08:00
|
|
|
ui_resize_border_width : f32,
|
2024-02-11 20:00:06 -08:00
|
|
|
}
|
|
|
|
|
2024-05-11 19:38:05 -07:00
|
|
|
AppWindow :: struct {
|
|
|
|
extent : Extents2, // Window half-size
|
|
|
|
dpi_scale : f32, // Dots per inch scale (provided by raylib via glfw)
|
|
|
|
ppcm : f32, // Dots per centimetre
|
|
|
|
}
|
|
|
|
|
|
|
|
FontData :: struct {
|
|
|
|
provider : FontProviderData,
|
|
|
|
|
|
|
|
// TODO(Ed): We can have font constants here I guess but eventually
|
|
|
|
// I rather have fonts configurable for a 'theme' combo
|
|
|
|
// So that way which IDs are picked depends on runtime
|
|
|
|
firacode : FontID,
|
|
|
|
squidgy_slimes : FontID,
|
|
|
|
rec_mono_semicasual_reg : FontID,
|
|
|
|
|
|
|
|
default_font : FontID,
|
|
|
|
}
|
|
|
|
|
|
|
|
FrameTime :: struct {
|
|
|
|
sleep_is_granular : b32,
|
|
|
|
|
|
|
|
delta_seconds : f64,
|
|
|
|
delta_ms : f64,
|
|
|
|
delta_ns : Duration,
|
|
|
|
target_ms : f64,
|
|
|
|
elapsed_ms : f64,
|
|
|
|
avg_ms : f64,
|
|
|
|
fps_avg : f64,
|
|
|
|
}
|
|
|
|
|
|
|
|
// Global Singleton stored in the persistent virtual arena, the first allocated data.
|
|
|
|
// Use get_state() to conviently retrieve at any point for the program's lifetime
|
2024-01-21 20:38:02 -08:00
|
|
|
State :: struct {
|
2024-03-10 17:09:04 -07:00
|
|
|
default_slab_policy : SlabPolicy,
|
|
|
|
persistent_slab : Slab,
|
|
|
|
frame_slab : Slab,
|
|
|
|
transient_slab : Slab, // TODO(Ed): This needs to be recreated per transient wipe
|
|
|
|
transinet_clear_lock : b32, // Pravents auto-free of transient at designated intervals
|
|
|
|
transient_clear_time : f32, // Time in seconds for the usual period to clear transient
|
|
|
|
transient_clear_elapsed : f32, // Time since last clear
|
2024-03-10 07:31:21 -07:00
|
|
|
|
2024-03-08 20:20:49 -08:00
|
|
|
string_cache : StringCache,
|
2024-03-07 12:57:05 -08:00
|
|
|
|
2024-03-06 19:41:17 -08:00
|
|
|
input_data : [2]InputState,
|
|
|
|
input_prev : ^InputState,
|
|
|
|
input : ^InputState,
|
2024-02-08 07:50:36 -08:00
|
|
|
|
|
|
|
debug : DebugData,
|
|
|
|
|
2024-01-21 20:38:02 -08:00
|
|
|
project : Project,
|
|
|
|
|
2024-02-11 20:00:06 -08:00
|
|
|
config : AppConfig,
|
|
|
|
app_window : AppWindow,
|
2024-05-10 16:50:37 -07:00
|
|
|
screen_ui : UI_ScreenState,
|
2024-01-21 20:38:02 -08:00
|
|
|
|
|
|
|
monitor_id : i32,
|
|
|
|
monitor_refresh_hz : i32,
|
|
|
|
|
2024-05-11 19:38:05 -07:00
|
|
|
// using frametime : FrameTime,
|
2024-03-08 15:45:08 -08:00
|
|
|
sleep_is_granular : b32,
|
|
|
|
|
2024-03-14 21:02:28 -07:00
|
|
|
frametime_delta_seconds : f64,
|
|
|
|
frametime_delta_ms : f64,
|
|
|
|
frametime_delta_ns : Duration,
|
|
|
|
frametime_target_ms : f64,
|
|
|
|
frametime_elapsed_ms : f64,
|
|
|
|
frametime_avg_ms : f64,
|
|
|
|
fps_avg : f64,
|
2024-03-02 07:24:09 -08:00
|
|
|
|
2024-05-11 19:38:05 -07:00
|
|
|
// fonts : FontData,
|
|
|
|
font_provider_data : FontProviderData,
|
|
|
|
|
2024-02-13 14:16:39 -08:00
|
|
|
font_firacode : FontID,
|
|
|
|
font_squidgy_slimes : FontID,
|
|
|
|
font_rec_mono_semicasual_reg : FontID,
|
|
|
|
default_font : FontID,
|
2024-02-22 20:15:29 -08:00
|
|
|
|
|
|
|
// There are two potential UI contextes for this prototype so far,
|
|
|
|
// the screen-space UI and the current workspace UI.
|
|
|
|
// This is used so that the ui api doesn't need to have the user pass the context every single time.
|
2024-03-10 17:09:04 -07:00
|
|
|
ui_context : ^UI_State,
|
2024-05-09 01:02:33 -07:00
|
|
|
|
|
|
|
// The camera is considered the "context" for coodrinate space operations in rendering
|
|
|
|
cam_context : Camera,
|
2024-01-21 20:38:02 -08:00
|
|
|
}
|
|
|
|
|
2024-05-11 19:38:05 -07:00
|
|
|
get_state :: #force_inline proc "contextless" () -> ^ State {
|
2024-05-07 23:26:39 -07:00
|
|
|
return cast( ^ State ) Memory_App.persistent.reserve_start
|
2024-02-09 10:09:58 -08:00
|
|
|
}
|
|
|
|
|
2024-05-11 19:38:05 -07:00
|
|
|
// get_frametime :: #force_inline proc "contextless" () -> FrameTime {
|
|
|
|
// return get_state().frametime
|
|
|
|
// }
|
2024-05-12 22:52:55 -07:00
|
|
|
|
|
|
|
#endregion("State")
|