resource layer refactor

This commit is contained in:
jacob 2025-07-30 20:51:57 -05:00
parent 6b4aec63f3
commit 061e88a75f
7 changed files with 91 additions and 81 deletions

View File

@ -233,7 +233,7 @@ void P_AppStartup(String args_str)
#endif
/* Global systems */
resource_startup();
R_Startup();
watch_startup();
gp_startup();

View File

@ -81,16 +81,16 @@ P_JobDef(F_LoadAssetJob, job)
Assert(countof(font_codes) < F_LookupTableSize);
/* Decode */
R_Resource res = resource_open(path);
if (!resource_exists(&res))
R_Resource res = R_OpenResource(path);
if (!R_ResourceExists(&res))
{
/* FIME: Load baked font instead of panicking */
P_Panic(StringFormat(scratch.arena,
Lit("Font \"%F\" not found"),
FmtString(path)));
}
TTF_Result result = ttf_decode(scratch.arena, resource_get_data(&res), point_size, font_codes, countof(font_codes));
resource_close(&res);
TTF_Result result = ttf_decode(scratch.arena, R_GetResourceData(&res), point_size, font_codes, countof(font_codes));
R_CloseResource(&res);
/* Send texture to GPU */
G_Resource *texture = gp_texture_alloc(GP_TEXTURE_FORMAT_R8G8B8A8_UNORM, 0, VEC2I32(result.image_width, result.image_height), result.image_pixels);

View File

@ -792,12 +792,12 @@ internal void dx12_init_noise(void)
{
String noise_res_name = Lit("noise_128x128x64_16.dat");
R_Resource noise_res = resource_open(noise_res_name);
R_Resource noise_res = R_OpenResource(noise_res_name);
DXGI_FORMAT format = DXGI_FORMAT_R16_UINT;
//u32 expected_size = K_BLUE_NOISE_TEX_WIDTH * K_BLUE_NOISE_TEX_HEIGHT * K_BLUE_NOISE_TEX_DEPTH * 2;
u32 expected_size = K_BLUE_NOISE_TEX_WIDTH * K_BLUE_NOISE_TEX_HEIGHT * K_BLUE_NOISE_TEX_DEPTH * 2;
if (resource_exists(&noise_res)) {
String data = resource_get_data(&noise_res);
if (R_ResourceExists(&noise_res)) {
String data = R_GetResourceData(&noise_res);
if (data.len != expected_size) {
P_Panic(StringFormat(scratch.arena,
Lit("Noise texture has unexpected size for a %Fx%Fx%F texture (expected %F, got %F)"),
@ -841,7 +841,7 @@ internal void dx12_init_noise(void)
} else {
P_Panic(StringFormat(scratch.arena, Lit("Noise resource \"%F\" not found"), FmtString(noise_res_name)));
}
resource_close(&noise_res);
R_CloseResource(&noise_res);
}
EndScratch(scratch);

View File

@ -1,35 +1,25 @@
/* ========================== *
* Global data
* ========================== */
R_SharedState R_shared_state = ZI;
/* Add resource data to binary */
////////////////////////////////
//~ Startup
Global struct {
Arena *arena;
#if RESOURCES_EMBEDDED
struct tar_archive archive;
#endif
} G = ZI, DebugAlias(G, G_resource);
/* ========================== *
* Startup
* ========================== */
R_StartupReceipt resource_startup(void)
R_StartupReceipt R_Startup(void)
{
__prof;
G.arena = AllocArena(Gibi(64));
R_SharedState *g = &R_shared_state;
g->arena = AllocArena(Gibi(64));
#if RESOURCES_EMBEDDED
String embedded_data = INC_GetResTar();
if (embedded_data.len <= 0) {
if (embedded_data.len <= 0)
{
P_Panic(Lit("No embedded resources found"));
}
G.archive = tar_parse(G.arena, embedded_data, Lit(""));
g->archive = tar_parse(g->arena, embedded_data, Lit(""));
#else
/* Ensure we have the right working directory */
if (!P_IsDir(Lit("res"))) {
if (!P_IsDir(Lit("res")))
{
P_Panic(Lit("Resource directory \"res\" not found. Make sure the executable is being launched from the correct working directory."));
}
#endif
@ -37,24 +27,25 @@ R_StartupReceipt resource_startup(void)
return (R_StartupReceipt) { 0 };
}
/* ========================== *
* Open / close
* ========================== */
////////////////////////////////
//~ Open / close
R_Resource resource_open(String name)
R_Resource R_OpenResource(String name)
{
__prof;
#if RESOURCES_EMBEDDED
R_SharedState *g = &R_shared_state;
R_Resource result = ZI;
struct tar_entry *entry = tar_get(&G.archive, name);
struct tar_entry *entry = tar_get(&g->archive, name);
result._data = entry->data;
result._name = entry->file_name;
result._exists = entry->valid;
return result;
#else
R_Resource result = ZI;
if (name.len < countof(result._name_text)) {
u8 path_text[RESOURCE_NAME_LEN_MAX + (sizeof("result/") - 1)];
if (name.len < countof(result._name_text))
{
u8 path_text[R_ResourceNameLenMax + (sizeof("result/") - 1)];
String path = ZI;
{
path_text[0] = 'r';
@ -70,14 +61,20 @@ R_Resource resource_open(String name)
P_File file = P_OpenFileReadWait(path);
P_FileMap file_map = ZI;
String data = ZI;
if (file.valid) {
if (file.valid)
{
file_map = P_OpenFileMap(file);
if (file_map.valid) {
if (file_map.valid)
{
data = P_GetFileMapData(file_map);
} else {
}
else
{
P_CloseFileMap(file_map);
}
} else {
}
else
{
P_CloseFIle(file);
}
@ -87,7 +84,9 @@ R_Resource resource_open(String name)
result._file_map = file_map;
result._name_len = name.len;
CopyBytes(result._name_text, name.text, name.len);
} else {
}
else
{
Assert(0);
}
return result;
@ -95,7 +94,7 @@ R_Resource resource_open(String name)
}
#if !RESOURCES_EMBEDDED
void resource_close(R_Resource *res_ptr)
void R_CloseResource(R_Resource *res_ptr)
{
P_CloseFileMap(res_ptr->_file_map);
P_CloseFIle(res_ptr->_file);

View File

@ -1,9 +1,10 @@
#define RESOURCE_NAME_LEN_MAX 256
////////////////////////////////
//~ Resource types
/* A resource contains data that can be retrieved globally by name.
* If enabled during compilation, resource data is embedded in the
* executable. Otherwise each resource represents a file on disk. */
Struct(R_Resource) {
#define R_ResourceNameLenMax 256
Struct(R_Resource)
{
String _data;
b32 _exists;
#if RESOURCES_EMBEDDED
@ -11,40 +12,50 @@ Struct(R_Resource) {
#else
P_File _file;
P_FileMap _file_map;
u8 _name_text[RESOURCE_NAME_LEN_MAX];
u8 _name_text[R_ResourceNameLenMax];
u8 _name_len;
#endif
};
/* ========================== *
* Startup
* ========================== */
////////////////////////////////
//~ Shared state
Struct(R_SharedState)
{
Arena *arena;
#if RESOURCES_EMBEDDED
struct tar_archive archive;
#endif
};
extern R_SharedState R_shared_state;
////////////////////////////////
//~ Startup
Struct(R_StartupReceipt) { i32 _; };
R_StartupReceipt resource_startup(void);
R_StartupReceipt R_Startup(void);
/* ========================== *
* Open / close
* ========================== */
////////////////////////////////
//~ Open / close
R_Resource resource_open(String name);
R_Resource R_OpenResource(String name);
#if RESOURCES_EMBEDDED
#define resource_close(res_ptr) (UNUSED)res_ptr
# define R_CloseResource(res_ptr) (UNUSED)res_ptr
#else
void resource_close(R_Resource *res_ptr);
void R_CloseResource(R_Resource *res_ptr);
#endif
/* ========================== *
* Data
* ========================== */
////////////////////////////////
//~ Resource data operations
#define resource_get_data(res_ptr) (res_ptr)->_data
#define R_GetResourceData(res_ptr) (res_ptr)->_data
#define resource_exists(res_ptr) (res_ptr)->_exists
#define R_ResourceExists(res_ptr) (res_ptr)->_exists
#if RESOURCES_EMBEDDED
#define resource_get_name(res_ptr) (res_ptr)->_name
# define R_GetResourceName(res_ptr) (res_ptr)->_name
#else
#define resource_get_name(res_ptr) STRING((res_ptr)->_name_len, (res_ptr)->_name_text)
# define R_GetResourceName(res_ptr) STRING((res_ptr)->_name_len, (res_ptr)->_name_text)
#endif

View File

@ -86,20 +86,20 @@ internal P_JobDef(sound_load_asset_job, job)
/* Decode */
MP3_Result decoded = ZI;
{
R_Resource sound_rs = resource_open(path);
if (resource_exists(&sound_rs)) {
R_Resource sound_rs = R_OpenResource(path);
if (R_ResourceExists(&sound_rs)) {
u64 decode_flags = 0;
if (flags & SOUND_FLAG_STEREO) {
decode_flags |= MP3_DecodeFlag_Stereo;
}
decoded = MP3_Decode(scratch.arena, resource_get_data(&sound_rs), SOUND_SAMPLE_RATE, decode_flags);
decoded = MP3_Decode(scratch.arena, R_GetResourceData(&sound_rs), SOUND_SAMPLE_RATE, decode_flags);
if (!decoded.success) {
error_msg = Lit("Failed to decode sound file");
}
} else {
error_msg = Lit("Resource not found");
}
resource_close(&sound_rs);
R_CloseResource(&sound_rs);
}
if (decoded.success) {

View File

@ -337,13 +337,13 @@ internal void cache_entry_load_texture(struct cache_ref ref, S_Tag tag)
/* Decode */
Ase_DecodedImage decoded = ZI;
{
R_Resource texture_rs = resource_open(path);
if (resource_exists(&texture_rs)) {
decoded = Ase_DecodeImage(scratch.arena, resource_get_data(&texture_rs));
R_Resource texture_rs = R_OpenResource(path);
if (R_ResourceExists(&texture_rs)) {
decoded = Ase_DecodeImage(scratch.arena, R_GetResourceData(&texture_rs));
} else {
P_LogErrorF("Sprite texture for \"%F\" not found", FmtString(path));
}
resource_close(&texture_rs);
R_CloseResource(&texture_rs);
}
if (decoded.success) {
@ -657,19 +657,19 @@ internal void cache_entry_load_sheet(struct cache_ref ref, S_Tag tag)
/* Decode */
Ase_DecodedSheet decoded = ZI;
{
R_Resource sheet_rs = resource_open(path);
if (resource_exists(&sheet_rs)) {
decoded = Ase_DecodeSheet(scratch.arena, resource_get_data(&sheet_rs));
R_Resource sheet_rs = R_OpenResource(path);
if (R_ResourceExists(&sheet_rs)) {
decoded = Ase_DecodeSheet(scratch.arena, R_GetResourceData(&sheet_rs));
} else {
P_LogErrorF("Sprite sheet for \"%F\" not found", FmtString(path));
}
resource_close(&sheet_rs);
R_CloseResource(&sheet_rs);
}
if (decoded.success) {
R_Resource sheet_rs = resource_open(path);
decoded = Ase_DecodeSheet(scratch.arena, resource_get_data(&sheet_rs));
resource_close(&sheet_rs);
R_Resource sheet_rs = R_OpenResource(path);
decoded = Ase_DecodeSheet(scratch.arena, R_GetResourceData(&sheet_rs));
R_CloseResource(&sheet_rs);
/* Initialize */
e->sheet = PushStructNoZero(e->arena, S_Sheet);