From 4821bd1e85d456dd992b99dc73ad23c8740cf459 Mon Sep 17 00:00:00 2001 From: jacob Date: Mon, 28 Jul 2025 17:59:02 -0500 Subject: [PATCH] decouple gp from sprite layer --- src/base/base.h | 20 ++++++++++++++++++ src/base/base_core.h | 29 ------------------------- src/draw/draw_core.c | 2 -- src/draw/draw_core.h | 6 ++---- src/dxc/dxc_core_win32.cpp | 5 ----- src/gp/gp.c | 2 ++ src/gp/gp.h | 1 - src/gp/gp_core.h | 2 -- src/gp/gp_core_dx12.c | 43 +++++++------------------------------- src/user/user_core.c | 35 +++++++++++++++++-------------- 10 files changed, 51 insertions(+), 94 deletions(-) diff --git a/src/base/base.h b/src/base/base.h index f4679bda..a358108e 100644 --- a/src/base/base.h +++ b/src/base/base.h @@ -1,6 +1,26 @@ #ifndef BASE_H #define BASE_H +/* Intrinsic header info: + * max ? INLINE f32 clamp_f32(f32 v, f32 min, f32 max) { return v < min ? min : v > max ? max : v; } INLINE f64 clamp_f64(f64 v, f64 min, f64 max) { return v < min ? min : v > max ? max : v; } -/* ========================== * - * Configurable constants - * ========================== */ - -#include "../config.h" - #ifdef __cplusplus } #endif diff --git a/src/draw/draw_core.c b/src/draw/draw_core.c index 9cbc7bac..fa1d9b04 100644 --- a/src/draw/draw_core.c +++ b/src/draw/draw_core.c @@ -24,7 +24,6 @@ void draw_material(struct gp_render_sig *sig, struct draw_material_params params struct gp_render_cmd_desc cmd = ZI; cmd.kind = GP_RENDER_CMD_KIND_DRAW_MATERIAL; cmd.material.xf = params.xf; - cmd.material.sprite = params.sprite; cmd.material.texture = params.texture; cmd.material.clip = params.clip; cmd.material.tint = params.tint; @@ -284,7 +283,6 @@ void draw_ui_rect(struct gp_render_sig *sig, struct draw_ui_rect_params params) struct gp_render_cmd_desc cmd = ZI; cmd.kind = GP_RENDER_CMD_KIND_DRAW_UI_RECT; cmd.ui_rect.xf = params.xf; - cmd.ui_rect.sprite = params.sprite; cmd.ui_rect.texture = params.texture; cmd.ui_rect.clip = params.clip; cmd.ui_rect.tint = params.tint; diff --git a/src/draw/draw_core.h b/src/draw/draw_core.h index 00703d81..e97e3f88 100644 --- a/src/draw/draw_core.h +++ b/src/draw/draw_core.h @@ -16,8 +16,7 @@ struct draw_startup_receipt draw_startup(struct font_startup_receipt *font_sr); struct draw_material_params { struct xform xf; - struct gp_resource *texture; /* Overrides sprite if set */ - struct sprite_tag sprite; + struct gp_resource *texture; struct clip_rect clip; u32 tint; b32 is_light; @@ -78,8 +77,7 @@ void draw_grid(struct gp_render_sig *sig, struct xform xf, u32 bg0_color, u32 bg struct draw_ui_rect_params { struct xform xf; - struct gp_resource *texture; /* Overrides sprite if set */ - struct sprite_tag sprite; + struct gp_resource *texture; struct clip_rect clip; u32 tint; }; diff --git a/src/dxc/dxc_core_win32.cpp b/src/dxc/dxc_core_win32.cpp index 81c3d1de..6f3b8d22 100644 --- a/src/dxc/dxc_core_win32.cpp +++ b/src/dxc/dxc_core_win32.cpp @@ -1,10 +1,5 @@ #if !RESOURCE_RELOADING -extern "C" -{ -#include "dxc.h" -} - struct dxc_compile_result dxc_compile(struct arena *arena, struct string shader_source, i32 num_args, struct string *args) { (UNUSED)arena; diff --git a/src/gp/gp.c b/src/gp/gp.c index 97f6290e..3aed7318 100644 --- a/src/gp/gp.c +++ b/src/gp/gp.c @@ -1,5 +1,7 @@ #include "gp.h" +#include "../kernel/kernel.h" + #if PLATFORM_WINDOWS # include "gp_core_dx12.c" #else diff --git a/src/gp/gp.h b/src/gp/gp.h index a4964346..5efd4495 100644 --- a/src/gp/gp.h +++ b/src/gp/gp.h @@ -9,7 +9,6 @@ #include "../inc/inc.h" #include "../resource/resource.h" #include "../watch/watch.h" -#include "../kernel/kernel.h" #include "gp_core.h" diff --git a/src/gp/gp_core.h b/src/gp/gp_core.h index c6909b21..8841ef6d 100644 --- a/src/gp/gp_core.h +++ b/src/gp/gp_core.h @@ -67,7 +67,6 @@ struct gp_render_cmd_desc { union { struct { struct xform xf; - struct sprite_tag sprite; struct gp_resource *texture; struct clip_rect clip; u32 tint; @@ -77,7 +76,6 @@ struct gp_render_cmd_desc { } material; struct { struct xform xf; - struct sprite_tag sprite; struct gp_resource *texture; struct clip_rect clip; u32 tint; diff --git a/src/gp/gp_core_dx12.c b/src/gp/gp_core_dx12.c index 33382d43..1c2a5927 100644 --- a/src/gp/gp_core_dx12.c +++ b/src/gp/gp_core_dx12.c @@ -1,6 +1,3 @@ -/* TODO: Remove this */ -#include "../sprite/sprite.h" - #pragma warning(push, 0) # define UNICODE # define COBJMACROS @@ -2547,8 +2544,7 @@ struct render_sig { struct material_instance_desc { struct xform xf; - struct sprite_tag sprite; - struct dx12_resource *texture; + u32 texture_id; struct clip_rect clip; u32 tint; b32 is_light; @@ -2558,8 +2554,7 @@ struct material_instance_desc { struct ui_rect_instance_desc { struct xform xf; - struct sprite_tag sprite; - struct dx12_resource *texture; + u32 texture_id; struct clip_rect clip; u32 tint; }; @@ -2632,10 +2627,10 @@ u32 gp_push_render_cmd(struct gp_render_sig *render_sig, struct gp_render_cmd_de case GP_RENDER_CMD_KIND_DRAW_MATERIAL: { + struct dx12_resource *texture = (struct dx12_resource *)cmd_desc->material.texture; struct material_instance_desc *instance_desc = arena_push(sig->material_instance_descs_arena, struct material_instance_desc); instance_desc->xf = cmd_desc->material.xf; - instance_desc->sprite = cmd_desc->material.sprite; - instance_desc->texture = (struct dx12_resource *)cmd_desc->material.texture; + instance_desc->texture_id = texture ? texture->srv_descriptor->index : 0xFFFFFFFF; instance_desc->clip = cmd_desc->material.clip; instance_desc->tint = cmd_desc->material.tint; instance_desc->is_light = cmd_desc->material.is_light; @@ -2646,10 +2641,10 @@ u32 gp_push_render_cmd(struct gp_render_sig *render_sig, struct gp_render_cmd_de case GP_RENDER_CMD_KIND_DRAW_UI_RECT: { + struct dx12_resource *texture = (struct dx12_resource *)cmd_desc->ui_rect.texture; struct ui_rect_instance_desc *instance_desc = arena_push(sig->ui_rect_instance_descs_arena, struct ui_rect_instance_desc); instance_desc->xf = cmd_desc->ui_rect.xf; - instance_desc->sprite = cmd_desc->ui_rect.sprite; - instance_desc->texture = (struct dx12_resource *)cmd_desc->ui_rect.texture; + instance_desc->texture_id = texture ? texture->srv_descriptor->index : 0xFFFFFFFF; instance_desc->clip = cmd_desc->ui_rect.clip; instance_desc->tint = cmd_desc->ui_rect.tint; ret = ++sig->num_ui_rect_instance_descs; @@ -2739,7 +2734,6 @@ struct gp_resource *gp_run_render(struct gp_render_sig *gp_render_sig, struct gp rsig->ui_target = gbuff_alloc(DXGI_FORMAT_R8G8B8A8_UNORM, ui_size, D3D12_RESOURCE_STATE_RENDER_TARGET); } - struct sprite_scope *sprite_scope = sprite_scope_begin(); struct pipeline_scope *pipeline_scope = pipeline_scope_begin(); struct pipeline *material_pipeline = pipeline_from_name(pipeline_scope, LIT("kernel_material")); struct pipeline *flood_pipeline = pipeline_from_name(pipeline_scope, LIT("kernel_flood")); @@ -2782,17 +2776,7 @@ struct gp_resource *gp_run_render(struct gp_render_sig *gp_render_sig, struct gp for (u32 i = 0; i < rsig->num_material_instance_descs; ++i) { struct material_instance_desc *desc = &((struct material_instance_desc *)arena_base(rsig->material_instance_descs_arena))[i]; struct k_material_instance *instance = &material_instances[i]; - u32 texture_id = 0xFFFFFFFF; - if (desc->texture != 0) { - texture_id = desc->texture->srv_descriptor->index; - } else if (desc->sprite.hash != 0) { - struct sprite_texture *st = sprite_texture_from_tag_async(sprite_scope, desc->sprite); - struct dx12_resource *texture = (struct dx12_resource *)st->gp_texture; - if (texture) { - texture_id = texture->srv_descriptor->index; - } - } - instance->tex_nurid = k_uint_from_u32(texture_id); + instance->tex_nurid = k_uint_from_u32(desc->texture_id); instance->grid_id = k_uint_from_u32(desc->grid_id); instance->xf = k_float2x3_from_xform(desc->xf); instance->uv0 = k_float2_from_v2(desc->clip.p0); @@ -2809,17 +2793,7 @@ struct gp_resource *gp_run_render(struct gp_render_sig *gp_render_sig, struct gp for (u32 i = 0; i < rsig->num_ui_rect_instance_descs; ++i) { struct ui_rect_instance_desc *desc = &((struct ui_rect_instance_desc *)arena_base(rsig->ui_rect_instance_descs_arena))[i]; struct k_ui_instance *instance = &ui_rect_instances[i]; - u32 texture_id = 0xFFFFFFFF; - if (desc->texture != 0) { - texture_id = desc->texture->srv_descriptor->index; - } else if (desc->sprite.hash != 0) { - struct sprite_texture *st = sprite_texture_from_tag_async(sprite_scope, desc->sprite); - struct dx12_resource *texture = (struct dx12_resource *)st->gp_texture; - if (texture) { - texture_id = texture->srv_descriptor->index; - } - } - instance->tex_nurid = k_uint_from_u32(texture_id); + instance->tex_nurid = k_uint_from_u32(desc->texture_id); instance->xf = k_float2x3_from_xform(desc->xf); instance->uv0 = k_float2_from_v2(desc->clip.p0); instance->uv1 = k_float2_from_v2(desc->clip.p1); @@ -3166,7 +3140,6 @@ struct gp_resource *gp_run_render(struct gp_render_sig *gp_render_sig, struct gp } command_list_close(cl); pipeline_scope_end(pipeline_scope); - sprite_scope_end(sprite_scope); render_sig_reset(rsig); scratch_end(scratch); diff --git a/src/user/user_core.c b/src/user/user_core.c index ccdfad0d..0f17cf16 100644 --- a/src/user/user_core.c +++ b/src/user/user_core.c @@ -1224,15 +1224,15 @@ INTERNAL void user_update(struct sys_window *window) /* Draw sprite */ if (!sprite_tag_is_nil(sprite)) { struct sprite_sheet *sheet = sprite_sheet_from_tag_async(sprite_frame_scope, sprite); - sprite_texture_from_tag_prefetch(sprite_frame_scope, sprite); + struct sprite_texture *texture = sprite_texture_from_tag_async(sprite_frame_scope, sprite); /* TODO: Fade in placeholder if texture isn't loaded */ - if (sheet->loaded) { + if (sheet->loaded && texture->loaded) { b32 is_light = sim_ent_has_prop(ent, SEPROP_LIGHT_TEST); struct v3 emittance = ent->sprite_emittance; u32 tint = ent->sprite_tint; struct sprite_sheet_frame frame = sprite_sheet_get_frame(sheet, ent->animation_frame); - struct draw_material_params params = DRAW_MATERIAL_PARAMS(.xf = sprite_xform, .sprite = sprite, .tint = tint, .clip = frame.clip, .is_light = is_light, .light_emittance = emittance); + struct draw_material_params params = DRAW_MATERIAL_PARAMS(.xf = sprite_xform, .texture = texture->gp_texture, .tint = tint, .clip = frame.clip, .is_light = is_light, .light_emittance = emittance); draw_material(G.render_sig, params); } } @@ -1242,18 +1242,21 @@ INTERNAL void user_update(struct sys_window *window) if (sim_ent_has_prop(ent, SEPROP_TILE_CHUNK)) { struct v2i32 chunk_index = ent->tile_chunk_index; struct sprite_tag tile_sprite = sprite_tag_from_path(LIT("sprite/tile.ase")); - f32 tile_size = 1.f / SIM_TILES_PER_UNIT_SQRT; - for (i32 tile_y = 0; tile_y < SIM_TILES_PER_CHUNK_SQRT; ++tile_y) { - for (i32 tile_x = 0; tile_x < SIM_TILES_PER_CHUNK_SQRT; ++tile_x) { - struct v2i32 local_tile_index = V2I32(tile_x, tile_y); - enum sim_tile_kind tile = ent->tile_chunk_tiles[local_tile_index.x + (local_tile_index.y * SIM_TILES_PER_CHUNK_SQRT)]; - //if (tile > -1) { - if (tile == SIM_TILE_KIND_WALL) { - struct v2i32 world_tile_index = sim_world_tile_index_from_local_tile_index(chunk_index, local_tile_index); - struct v2 pos = sim_pos_from_world_tile_index(world_tile_index); - struct xform tile_xf = xform_from_rect(RECT_FROM_V2(pos, V2(tile_size, tile_size))); - struct draw_material_params params = DRAW_MATERIAL_PARAMS(.xf = tile_xf, .sprite = tile_sprite, .is_light = 1, .light_emittance = V3(0, 0, 0)); - draw_material(G.render_sig, params); + struct sprite_texture *tile_texture = sprite_texture_from_tag_async(sprite_frame_scope, tile_sprite); + if (tile_texture->loaded) { + f32 tile_size = 1.f / SIM_TILES_PER_UNIT_SQRT; + for (i32 tile_y = 0; tile_y < SIM_TILES_PER_CHUNK_SQRT; ++tile_y) { + for (i32 tile_x = 0; tile_x < SIM_TILES_PER_CHUNK_SQRT; ++tile_x) { + struct v2i32 local_tile_index = V2I32(tile_x, tile_y); + enum sim_tile_kind tile = ent->tile_chunk_tiles[local_tile_index.x + (local_tile_index.y * SIM_TILES_PER_CHUNK_SQRT)]; + //if (tile > -1) { + if (tile == SIM_TILE_KIND_WALL) { + struct v2i32 world_tile_index = sim_world_tile_index_from_local_tile_index(chunk_index, local_tile_index); + struct v2 pos = sim_pos_from_world_tile_index(world_tile_index); + struct xform tile_xf = xform_from_rect(RECT_FROM_V2(pos, V2(tile_size, tile_size))); + struct draw_material_params params = DRAW_MATERIAL_PARAMS(.xf = tile_xf, .texture = tile_texture->gp_texture, .is_light = 1, .light_emittance = V3(0, 0, 0)); + draw_material(G.render_sig, params); + } } } } @@ -1694,7 +1697,7 @@ INTERNAL void user_update(struct sys_window *window) struct sprite_texture *t = sprite_texture_from_tag_async(sprite_frame_scope, crosshair); struct v2 size = V2(t->width, t->height); struct xform xf = XFORM_TRS(.t = crosshair_pos, .s = size); - draw_ui_rect(G.render_sig, DRAW_UI_RECT_PARAMS(.xf = xf, .sprite = crosshair)); + draw_ui_rect(G.render_sig, DRAW_UI_RECT_PARAMS(.xf = xf, .texture = t->gp_texture)); } /* FIXME: Enable this */