From 4e99312b75854e72be6463b6f99d104d5a4517b1 Mon Sep 17 00:00:00 2001 From: Ryan Fleury Date: Thu, 18 Sep 2025 15:53:00 -0700 Subject: [PATCH] eliminate tex_scope; fold into access --- src/raddbg/raddbg_views.c | 4 +- src/texture_cache/texture_cache.c | 92 ++----------------------------- src/texture_cache/texture_cache.h | 46 +--------------- 3 files changed, 9 insertions(+), 133 deletions(-) diff --git a/src/raddbg/raddbg_views.c b/src/raddbg/raddbg_views.c index 0120789f..7e9efbcf 100644 --- a/src/raddbg/raddbg_views.c +++ b/src/raddbg/raddbg_views.c @@ -3821,7 +3821,6 @@ RD_VIEW_UI_FUNCTION_DEF(bitmap) { Temp scratch = scratch_begin(0, 0); Access *access = access_open(); - TEX_Scope *tex_scope = tex_scope_open(); ////////////////////////////// //- rjf: evaluate expression @@ -3871,7 +3870,7 @@ RD_VIEW_UI_FUNCTION_DEF(bitmap) C_Key texture_key = rd_key_from_eval_space_range(eval.space, offset_range, 0); TEX_Topology topology = tex_topology_make(dim, fmt); U128 data_hash = {0}; - R_Handle texture = tex_texture_from_key_topology(tex_scope, texture_key, topology, &data_hash); + R_Handle texture = tex_texture_from_key_topology(access, texture_key, topology, &data_hash); String8 data = c_data_from_hash(access, data_hash); ////////////////////////////// @@ -4025,7 +4024,6 @@ RD_VIEW_UI_FUNCTION_DEF(bitmap) rd_store_view_param_f32(str8_lit("x"), view_center_pos.x); rd_store_view_param_f32(str8_lit("y"), view_center_pos.y); - tex_scope_close(tex_scope); access_close(access); scratch_end(scratch); } diff --git a/src/texture_cache/texture_cache.c b/src/texture_cache/texture_cache.c index 54d3e55c..d5e63181 100644 --- a/src/texture_cache/texture_cache.c +++ b/src/texture_cache/texture_cache.c @@ -44,93 +44,11 @@ tex_init(void) tex_shared->evictor_thread = thread_launch(tex_evictor_thread__entry_point, 0); } -//////////////////////////////// -//~ rjf: Thread Context Initialization - -internal void -tex_tctx_ensure_inited(void) -{ - if(tex_tctx == 0) - { - Arena *arena = arena_alloc(); - tex_tctx = push_array(arena, TEX_TCTX, 1); - tex_tctx->arena = arena; - } -} - -//////////////////////////////// -//~ rjf: Scoped Access - -internal TEX_Scope * -tex_scope_open(void) -{ - tex_tctx_ensure_inited(); - TEX_Scope *scope = tex_tctx->free_scope; - if(scope) - { - SLLStackPop(tex_tctx->free_scope); - } - else - { - scope = push_array_no_zero(tex_tctx->arena, TEX_Scope, 1); - } - MemoryZeroStruct(scope); - return scope; -} - -internal void -tex_scope_close(TEX_Scope *scope) -{ - for(TEX_Touch *touch = scope->top_touch, *next = 0; touch != 0; touch = next) - { - U128 hash = touch->hash; - next = touch->next; - U64 slot_idx = hash.u64[1]%tex_shared->slots_count; - U64 stripe_idx = slot_idx%tex_shared->stripes_count; - TEX_Slot *slot = &tex_shared->slots[slot_idx]; - TEX_Stripe *stripe = &tex_shared->stripes[stripe_idx]; - MutexScopeR(stripe->rw_mutex) - { - for(TEX_Node *n = slot->first; n != 0; n = n->next) - { - if(u128_match(hash, n->hash) && MemoryMatchStruct(&touch->topology, &n->topology)) - { - ins_atomic_u64_dec_eval(&n->scope_ref_count); - break; - } - } - } - SLLStackPush(tex_tctx->free_touch, touch); - } - SLLStackPush(tex_tctx->free_scope, scope); -} - -internal void -tex_scope_touch_node__stripe_r_guarded(TEX_Scope *scope, TEX_Node *node) -{ - TEX_Touch *touch = tex_tctx->free_touch; - ins_atomic_u64_inc_eval(&node->scope_ref_count); - ins_atomic_u64_eval_assign(&node->last_time_touched_us, os_now_microseconds()); - ins_atomic_u64_eval_assign(&node->last_user_clock_idx_touched, update_tick_idx()); - if(touch != 0) - { - SLLStackPop(tex_tctx->free_touch); - } - else - { - touch = push_array_no_zero(tex_tctx->arena, TEX_Touch, 1); - } - MemoryZeroStruct(touch); - touch->hash = node->hash; - touch->topology = node->topology; - SLLStackPush(scope->top_touch, touch); -} - //////////////////////////////// //~ rjf: Cache Lookups internal R_Handle -tex_texture_from_hash_topology(TEX_Scope *scope, U128 hash, TEX_Topology topology) +tex_texture_from_hash_topology(Access *access, U128 hash, TEX_Topology topology) { R_Handle handle = {0}; { @@ -148,7 +66,9 @@ tex_texture_from_hash_topology(TEX_Scope *scope, U128 hash, TEX_Topology topolog { handle = n->texture; found = !r_handle_match(r_handle_zero(), handle); - tex_scope_touch_node__stripe_r_guarded(scope, n); + ins_atomic_u64_eval_assign(&n->last_time_touched_us, os_now_microseconds()); + ins_atomic_u64_eval_assign(&n->last_user_clock_idx_touched, update_tick_idx()); + access_touch(access, &n->scope_ref_count, stripe->cv); break; } } @@ -196,13 +116,13 @@ tex_texture_from_hash_topology(TEX_Scope *scope, U128 hash, TEX_Topology topolog } internal R_Handle -tex_texture_from_key_topology(TEX_Scope *scope, C_Key key, TEX_Topology topology, U128 *hash_out) +tex_texture_from_key_topology(Access *access, C_Key key, TEX_Topology topology, U128 *hash_out) { R_Handle handle = {0}; for(U64 rewind_idx = 0; rewind_idx < C_KEY_HASH_HISTORY_COUNT; rewind_idx += 1) { U128 hash = c_hash_from_key(key, rewind_idx); - handle = tex_texture_from_hash_topology(scope, hash, topology); + handle = tex_texture_from_hash_topology(access, hash, topology); if(!r_handle_match(handle, r_handle_zero())) { if(hash_out) diff --git a/src/texture_cache/texture_cache.h b/src/texture_cache/texture_cache.h index b91007b3..a9140e91 100644 --- a/src/texture_cache/texture_cache.h +++ b/src/texture_cache/texture_cache.h @@ -47,35 +47,6 @@ struct TEX_Stripe CondVar cv; }; -//////////////////////////////// -//~ rjf: Scoped Access - -typedef struct TEX_Touch TEX_Touch; -struct TEX_Touch -{ - TEX_Touch *next; - U128 hash; - TEX_Topology topology; -}; - -typedef struct TEX_Scope TEX_Scope; -struct TEX_Scope -{ - TEX_Scope *next; - TEX_Touch *top_touch; -}; - -//////////////////////////////// -//~ rjf: Thread Context - -typedef struct TEX_TCTX TEX_TCTX; -struct TEX_TCTX -{ - Arena *arena; - TEX_Scope *free_scope; - TEX_Touch *free_touch; -}; - //////////////////////////////// //~ rjf: Shared State @@ -106,7 +77,6 @@ struct TEX_Shared //////////////////////////////// //~ rjf: Globals -thread_static TEX_TCTX *tex_tctx = 0; global TEX_Shared *tex_shared = 0; //////////////////////////////// @@ -119,23 +89,11 @@ internal TEX_Topology tex_topology_make(Vec2S32 dim, R_Tex2DFormat fmt); internal void tex_init(void); -//////////////////////////////// -//~ rjf: Thread Context Initialization - -internal void tex_tctx_ensure_inited(void); - -//////////////////////////////// -//~ rjf: Scoped Access - -internal TEX_Scope *tex_scope_open(void); -internal void tex_scope_close(TEX_Scope *scope); -internal void tex_scope_touch_node__stripe_r_guarded(TEX_Scope *scope, TEX_Node *node); - //////////////////////////////// //~ rjf: Cache Lookups -internal R_Handle tex_texture_from_hash_topology(TEX_Scope *scope, U128 hash, TEX_Topology topology); -internal R_Handle tex_texture_from_key_topology(TEX_Scope *scope, C_Key key, TEX_Topology topology, U128 *hash_out); +internal R_Handle tex_texture_from_hash_topology(Access *access, U128 hash, TEX_Topology topology); +internal R_Handle tex_texture_from_key_topology(Access *access, C_Key key, TEX_Topology topology, U128 *hash_out); //////////////////////////////// //~ rjf: Transfer Threads