From d8fcbcd868edcf3b94d3292216799687aa3a8e99 Mon Sep 17 00:00:00 2001 From: Ryan Fleury Date: Thu, 25 Sep 2025 11:40:35 -0700 Subject: [PATCH] eliminate texture_cache; replace with trivial usage of artifact cache, in raddbg layer defining the bitmap visualizer, which was the only usage --- src/base/base_entry_point.c | 6 - src/raddbg/raddbg_main.c | 3 - src/raddbg/raddbg_views.c | 71 +++++++- src/texture_cache/texture_cache.c | 280 ------------------------------ src/texture_cache/texture_cache.h | 118 ------------- 5 files changed, 69 insertions(+), 409 deletions(-) delete mode 100644 src/texture_cache/texture_cache.c delete mode 100644 src/texture_cache/texture_cache.h diff --git a/src/base/base_entry_point.c b/src/base/base_entry_point.c index 851042bc..b8ba0f79 100644 --- a/src/base/base_entry_point.c +++ b/src/base/base_entry_point.c @@ -86,9 +86,6 @@ main_thread_base_entry_point(int arguments_count, char **arguments) #if defined(RENDER_CORE_H) && !defined(R_INIT_MANUAL) r_init(&cmdline); #endif -#if defined(TEXTURE_CACHE_H) && !defined(TEX_INIT_MANUAL) - tex_init(); -#endif #if defined(GEO_CACHE_H) && !defined(GEO_INIT_MANUAL) geo_init(); #endif @@ -211,9 +208,6 @@ async_thread_entry_point(void *params) #endif #if defined(FILE_STREAM_H) fs_async_tick(); -#endif -#if defined(TEXTURE_CACHE_H) - tex_async_tick(); #endif } diff --git a/src/raddbg/raddbg_main.c b/src/raddbg/raddbg_main.c index 33939e69..f4533078 100644 --- a/src/raddbg/raddbg_main.c +++ b/src/raddbg/raddbg_main.c @@ -259,7 +259,6 @@ #include "font_provider/font_provider_inc.h" #include "render/render_inc.h" #include "ptr_graph_cache/ptr_graph_cache.h" -#include "texture_cache/texture_cache.h" #include "geo_cache/geo_cache.h" #include "font_cache/font_cache.h" #include "draw/draw.h" @@ -309,7 +308,6 @@ #include "font_provider/font_provider_inc.c" #include "render/render_inc.c" #include "ptr_graph_cache/ptr_graph_cache.c" -#include "texture_cache/texture_cache.c" #include "geo_cache/geo_cache.c" #include "font_cache/font_cache.c" #include "draw/draw.c" @@ -499,7 +497,6 @@ entry_point(CmdLine *cmd_line) os_gfx_init(); fp_init(); r_init(cmd_line); - tex_init(); geo_init(); fnt_init(); d_init(); diff --git a/src/raddbg/raddbg_views.c b/src/raddbg/raddbg_views.c index 69267dec..92e61641 100644 --- a/src/raddbg/raddbg_views.c +++ b/src/raddbg/raddbg_views.c @@ -3724,6 +3724,13 @@ EV_EXPAND_RULE_INFO_FUNCTION_DEF(graph) //////////////////////////////// //~ rjf: bitmap @view_hook_impl +typedef struct RD_BitmapTopology RD_BitmapTopology; +struct RD_BitmapTopology +{ + Vec2S16 dim; + R_Tex2DFormat fmt; +}; + typedef struct RD_BitmapBoxDrawData RD_BitmapBoxDrawData; struct RD_BitmapBoxDrawData { @@ -3742,6 +3749,46 @@ struct RD_BitmapCanvasBoxDrawData F32 zoom; }; +internal AC_Artifact +rd_bitmap_artifact_create(String8 key, B32 *retry_out) +{ + Access *access = access_open(); + + //- rjf: unpack key + U128 hash = {0}; + RD_BitmapTopology top = {0}; + { + U64 key_read_off = 0; + key_read_off += str8_deserial_read_struct(key, key_read_off, &hash); + key_read_off += str8_deserial_read_struct(key, key_read_off, &top); + } + String8 data = c_data_from_hash(access, hash); + + //- rjf: create texture + R_Handle texture = {0}; + if(top.dim.x > 0 && top.dim.y > 0 && + data.size >= (U64)top.dim.x*(U64)top.dim.y*(U64)r_tex2d_format_bytes_per_pixel_table[top.fmt]) + { + texture = r_tex2d_alloc(R_ResourceKind_Static, v2s32(top.dim.x, top.dim.y), top.fmt, data.str); + } + + //- rjf: bundle as artifact + AC_Artifact artifact = {0}; + StaticAssert(sizeof(artifact) >= sizeof(texture), tex_artifact_size_check); + MemoryCopy(&artifact, &texture, Min(sizeof(texture), sizeof(artifact))); + + access_close(access); + return artifact; +} + +internal void +rd_bitmap_artifact_destroy(AC_Artifact artifact) +{ + R_Handle texture = {0}; + MemoryCopy(&texture, &artifact, Min(sizeof(texture), sizeof(artifact))); + r_tex2d_release(texture); +} + internal Vec2F32 rd_bitmap_screen_from_canvas_pos(Vec2F32 view_center_pos, F32 zoom, Rng2F32 rect, Vec2F32 cvs) { @@ -3868,9 +3915,29 @@ RD_VIEW_UI_FUNCTION_DEF(bitmap) //- rjf: map expression artifacts -> texture // C_Key texture_key = rd_key_from_eval_space_range(eval.space, offset_range, 0); - TEX_Topology topology = tex_topology_make(dim, fmt); + RD_BitmapTopology topology = {v2s16(dim.x, dim.y), fmt}; U128 data_hash = {0}; - R_Handle texture = tex_texture_from_key_topology(access, texture_key, topology, &data_hash); + R_Handle texture = {0}; + for EachIndex(rewind_idx, C_KEY_HASH_HISTORY_COUNT) + { + U128 hash = c_hash_from_key(texture_key, rewind_idx); + struct + { + U128 hash; + RD_BitmapTopology top; + } + key_data = {hash, topology}; + String8 key = str8_struct(&key_data); + AC_Artifact artifact = ac_artifact_from_key(access, key, rd_bitmap_artifact_create, rd_bitmap_artifact_destroy, 0); + R_Handle texture_candidate = {0}; + MemoryCopy(&texture_candidate, &artifact, Min(sizeof(texture_candidate), sizeof(artifact))); + if(!r_handle_match(texture_candidate, r_handle_zero())) + { + data_hash = hash; + texture = texture_candidate; + break; + } + } String8 data = c_data_from_hash(access, data_hash); ////////////////////////////// diff --git a/src/texture_cache/texture_cache.c b/src/texture_cache/texture_cache.c deleted file mode 100644 index ef9c6d22..00000000 --- a/src/texture_cache/texture_cache.c +++ /dev/null @@ -1,280 +0,0 @@ -// Copyright (c) Epic Games Tools -// Licensed under the MIT license (https://opensource.org/license/mit/) - -#undef LAYER_COLOR -#define LAYER_COLOR 0xe34cd4ff - -//////////////////////////////// -//~ rjf: Basic Helpers - -internal TEX_Topology -tex_topology_make(Vec2S32 dim, R_Tex2DFormat fmt) -{ - TEX_Topology top = {0}; - top.dim.x = (S16)Clamp(0, dim.x, max_S32); - top.dim.y = (S16)Clamp(0, dim.y, max_S32); - top.fmt = fmt; - return top; -} - -//////////////////////////////// -//~ rjf: Main Layer Initialization - -internal void -tex_init(void) -{ - Arena *arena = arena_alloc(); - tex_shared = push_array(arena, TEX_Shared, 1); - tex_shared->arena = arena; - tex_shared->slots_count = 1024; - tex_shared->stripes_count = Min(tex_shared->slots_count, os_get_system_info()->logical_processor_count); - tex_shared->slots = push_array(arena, TEX_Slot, tex_shared->slots_count); - tex_shared->stripes = push_array(arena, TEX_Stripe, tex_shared->stripes_count); - tex_shared->stripes_free_nodes = push_array(arena, TEX_Node *, tex_shared->stripes_count); - for(U64 idx = 0; idx < tex_shared->stripes_count; idx += 1) - { - tex_shared->stripes[idx].arena = arena_alloc(); - tex_shared->stripes[idx].rw_mutex = rw_mutex_alloc(); - tex_shared->stripes[idx].cv = cond_var_alloc(); - } - tex_shared->req_mutex = mutex_alloc(); - tex_shared->req_arena = arena_alloc(); -} - -//////////////////////////////// -//~ rjf: Cache Lookups - -internal R_Handle -tex_texture_from_hash_topology(Access *access, U128 hash, TEX_Topology topology) -{ - R_Handle handle = {0}; - { - //- rjf: unpack hash - 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]; - - //- rjf: get results, request if needed - for(B32 write_mode = 0; write_mode <= 1; write_mode += 1) - { - B32 got_node = 0; - RWMutexScope(stripe->rw_mutex, write_mode) - { - // rjf: get node - TEX_Node *node = 0; - for(TEX_Node *n = slot->first; n != 0; n = n->next) - { - if(u128_match(hash, n->hash) && MemoryMatchStruct(&topology, &n->topology)) - { - node = n; - got_node = 1; - break; - } - } - - // rjf: no node? -> create & request - if(write_mode && !node) - { - node = tex_shared->stripes_free_nodes[stripe_idx]; - if(node) - { - SLLStackPop(tex_shared->stripes_free_nodes[stripe_idx]); - } - else - { - node = push_array_no_zero(stripe->arena, TEX_Node, 1); - } - MemoryZeroStruct(node); - DLLPushBack(slot->first, slot->last, node); - node->hash = hash; - MemoryCopyStruct(&node->topology, &topology); - MutexScope(tex_shared->req_mutex) - { - TEX_RequestNode *n = push_array(tex_shared->req_arena, TEX_RequestNode, 1); - SLLQueuePush(tex_shared->first_req, tex_shared->last_req, n); - n->v.hash = hash; - n->v.top = topology; - tex_shared->req_count += 1; - } - cond_var_broadcast(async_tick_start_cond_var); - } - - // rjf: node? -> grab & access - if(!write_mode && node) - { - handle = node->texture; - access_touch(access, &node->access_pt, stripe->cv); - } - } - if(got_node) - { - break; - } - } - } - return handle; -} - -internal R_Handle -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(access, hash, topology); - if(!r_handle_match(handle, r_handle_zero())) - { - if(hash_out) - { - *hash_out = hash; - } - break; - } - } - return handle; -} - -//////////////////////////////// -//~ rjf: Asynchronous Tick - -internal void -tex_async_tick(void) -{ - if(ins_atomic_u64_eval(&tex_shared) == 0) { return; } - ProfBeginFunction(); - Temp scratch = scratch_begin(0, 0); - - //- rjf: do eviction pass - { - U64 check_time_us = os_now_microseconds(); - U64 check_time_user_clocks = update_tick_idx(); - U64 evict_threshold_us = 10*1000000; - U64 evict_threshold_user_clocks = 10; - Rng1U64 range = lane_range(tex_shared->slots_count); - for EachInRange(slot_idx, range) - { - 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]; - for(B32 write_mode = 0; write_mode <= 1; write_mode += 1) - { - B32 slot_has_work = 0; - RWMutexScope(stripe->rw_mutex, write_mode) - { - for(TEX_Node *n = slot->first; n != 0; n = n->next) - { - if(access_pt_is_expired(&n->access_pt) && - n->load_count != 0 && - n->working_count == 0) - { - slot_has_work = 1; - if(!write_mode) - { - break; - } - else - { - DLLRemove(slot->first, slot->last, n); - if(!r_handle_match(n->texture, r_handle_zero())) - { - r_tex2d_release(n->texture); - } - SLLStackPush(tex_shared->stripes_free_nodes[stripe_idx], n); - } - } - } - } - if(!slot_has_work) - { - break; - } - } - } - } - - //- rjf: gather all requests - local_persist TEX_Request *reqs = 0; - local_persist U64 reqs_count = 0; - if(lane_idx() == 0) MutexScope(tex_shared->req_mutex) - { - reqs_count = tex_shared->req_count; - reqs = push_array(scratch.arena, TEX_Request, reqs_count); - U64 idx = 0; - for EachNode(r, TEX_RequestNode, tex_shared->first_req) - { - MemoryCopyStruct(&reqs[idx], &r->v); - idx += 1; - } - arena_clear(tex_shared->req_arena); - tex_shared->first_req = tex_shared->last_req = 0; - tex_shared->req_count = 0; - tex_shared->lane_req_take_counter = 0; - } - lane_sync(); - - //- rjf: do requests - for(;;) - { - //- rjf: get next request - U64 req_num = ins_atomic_u64_inc_eval(&tex_shared->lane_req_take_counter); - if(req_num < 1 || reqs_count < req_num) - { - break; - } - U64 req_idx = req_num-1; - U128 hash = reqs[req_idx].hash; - TEX_Topology top = reqs[req_idx].top; - Access *access = access_open(); - - //- rjf: unpack request - 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]; - String8 data = c_data_from_hash(access, hash); - - //- rjf: create texture - R_Handle texture = {0}; - if(top.dim.x > 0 && top.dim.y > 0 && data.size >= (U64)top.dim.x*(U64)top.dim.y*(U64)r_tex2d_format_bytes_per_pixel_table[top.fmt]) - { - texture = r_tex2d_alloc(R_ResourceKind_Static, v2s32(top.dim.x, top.dim.y), top.fmt, data.str); - } - - //- rjf: commit results to cache - RWMutexScope(stripe->rw_mutex, 1) - { - for(TEX_Node *n = slot->first; n != 0; n = n->next) - { - if(u128_match(n->hash, hash) && MemoryMatchStruct(&top, &n->topology)) - { - n->texture = texture; - ins_atomic_u64_dec_eval(&n->working_count); - ins_atomic_u64_inc_eval(&n->load_count); - break; - } - } - } - - access_close(access); - } - - scratch_end(scratch); - ProfEnd(); -} - -//////////////////////////////// -//~ rjf: Artifact Cache Hooks / Lookups - -internal void * -tex_artifact_create(String8 key, B32 *retry_out) -{ - -} - -internal void -tex_artifact_destroy(void *ptr) -{ - -} diff --git a/src/texture_cache/texture_cache.h b/src/texture_cache/texture_cache.h deleted file mode 100644 index 4efeaee3..00000000 --- a/src/texture_cache/texture_cache.h +++ /dev/null @@ -1,118 +0,0 @@ -// Copyright (c) Epic Games Tools -// Licensed under the MIT license (https://opensource.org/license/mit/) - -#ifndef TEXTURE_CACHE_H -#define TEXTURE_CACHE_H - -//////////////////////////////// -//~ rjf: Texture Topology - -typedef struct TEX_Topology TEX_Topology; -struct TEX_Topology -{ - Vec2S16 dim; - R_Tex2DFormat fmt; -}; - -//////////////////////////////// -//~ rjf: Cache Types - -typedef struct TEX_Node TEX_Node; -struct TEX_Node -{ - TEX_Node *next; - TEX_Node *prev; - U128 hash; - TEX_Topology topology; - R_Handle texture; - AccessPt access_pt; - U64 working_count; - U64 load_count; -}; - -typedef struct TEX_Slot TEX_Slot; -struct TEX_Slot -{ - TEX_Node *first; - TEX_Node *last; -}; - -typedef struct TEX_Stripe TEX_Stripe; -struct TEX_Stripe -{ - Arena *arena; - RWMutex rw_mutex; - CondVar cv; -}; - -//////////////////////////////// -//~ rjf: Shared State - -typedef struct TEX_Request TEX_Request; -struct TEX_Request -{ - U128 hash; - TEX_Topology top; -}; - -typedef struct TEX_RequestNode TEX_RequestNode; -struct TEX_RequestNode -{ - TEX_RequestNode *next; - TEX_Request v; -}; - -typedef struct TEX_Shared TEX_Shared; -struct TEX_Shared -{ - Arena *arena; - - // rjf: cache - U64 slots_count; - U64 stripes_count; - TEX_Slot *slots; - TEX_Stripe *stripes; - TEX_Node **stripes_free_nodes; - - // rjf: requests - Mutex req_mutex; - Arena *req_arena; - TEX_RequestNode *first_req; - TEX_RequestNode *last_req; - U64 req_count; - U64 lane_req_take_counter; -}; - -//////////////////////////////// -//~ rjf: Globals - -global TEX_Shared *tex_shared = 0; - -//////////////////////////////// -//~ rjf: Basic Helpers - -internal TEX_Topology tex_topology_make(Vec2S32 dim, R_Tex2DFormat fmt); - -//////////////////////////////// -//~ rjf: Main Layer Initialization - -internal void tex_init(void); - -//////////////////////////////// -//~ rjf: Cache Lookups - -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: Asynchronous Tick - -internal void tex_async_tick(void); - -//////////////////////////////// -//~ rjf: Artifact Cache Hooks / Lookups - -internal void *tex_artifact_create(String8 key, B32 *retry_out); -internal void tex_artifact_destroy(void *ptr); - -#endif // TEXTURE_CACHE_H