shader static assertion

This commit is contained in:
jacob 2025-07-25 21:42:43 -05:00
parent 26a64ce1eb
commit efbef9cd61
6 changed files with 84 additions and 72 deletions

View File

@ -76,7 +76,7 @@ struct gp_render_cmd_desc {
u32 tint; u32 tint;
b32 is_light; b32 is_light;
struct v3 light_emittance; struct v3 light_emittance;
i32 grid_cmd_id; u32 grid_cmd_id;
} material; } material;
struct { struct {
struct xform xf; struct xform xf;
@ -114,7 +114,7 @@ struct gp_render_params {
struct gp_render_sig *gp_render_sig_alloc(void); struct gp_render_sig *gp_render_sig_alloc(void);
/* Returns a cmd id internal to the sig */ /* Returns a cmd id internal to the sig */
i32 gp_push_render_cmd(struct gp_render_sig *render_sig, struct gp_render_cmd_desc *desc); u32 gp_push_render_cmd(struct gp_render_sig *render_sig, struct gp_render_cmd_desc *desc);
struct gp_resource *gp_run_render(struct gp_render_sig *gp_render_sig, struct gp_render_params render_params); struct gp_resource *gp_run_render(struct gp_render_sig *gp_render_sig, struct gp_render_params render_params);

View File

@ -2580,7 +2580,7 @@ struct material_instance_desc {
u32 tint; u32 tint;
b32 is_light; b32 is_light;
struct v3 light_emittance; struct v3 light_emittance;
i32 grid_id; u32 grid_id;
}; };
struct ui_rect_instance_desc { struct ui_rect_instance_desc {
@ -2649,9 +2649,9 @@ struct gp_render_sig *gp_render_sig_alloc(void)
return (struct gp_render_sig *)sig; return (struct gp_render_sig *)sig;
} }
i32 gp_push_render_cmd(struct gp_render_sig *render_sig, struct gp_render_cmd_desc *cmd_desc) u32 gp_push_render_cmd(struct gp_render_sig *render_sig, struct gp_render_cmd_desc *cmd_desc)
{ {
i32 ret = 0; u32 ret = 0;
struct render_sig *sig = (struct render_sig *)render_sig; struct render_sig *sig = (struct render_sig *)render_sig;
if (sig) { if (sig) {
switch (cmd_desc->kind) { switch (cmd_desc->kind) {
@ -2809,7 +2809,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) { 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 material_instance_desc *desc = &((struct material_instance_desc *)arena_base(rsig->material_instance_descs_arena))[i];
struct sh_material_instance *instance = &material_instances[i]; struct sh_material_instance *instance = &material_instances[i];
i32 texture_id = -1; u32 texture_id = 0xFFFFFFFF;
if (desc->texture != 0) { if (desc->texture != 0) {
texture_id = desc->texture->srv_descriptor->index; texture_id = desc->texture->srv_descriptor->index;
} else if (desc->sprite.hash != 0) { } else if (desc->sprite.hash != 0) {
@ -2819,8 +2819,8 @@ struct gp_resource *gp_run_render(struct gp_render_sig *gp_render_sig, struct gp
texture_id = texture->srv_descriptor->index; texture_id = texture->srv_descriptor->index;
} }
} }
instance->tex_nurid = sh_int_from_i32(texture_id); instance->tex_nurid = sh_uint_from_u32(texture_id);
instance->grid_id = sh_int_from_i32(desc->grid_id); instance->grid_id = sh_uint_from_u32(desc->grid_id);
instance->xf = sh_float2x3_from_xform(desc->xf); instance->xf = sh_float2x3_from_xform(desc->xf);
instance->uv0 = sh_float2_from_v2(desc->clip.p0); instance->uv0 = sh_float2_from_v2(desc->clip.p0);
instance->uv1 = sh_float2_from_v2(desc->clip.p1); instance->uv1 = sh_float2_from_v2(desc->clip.p1);
@ -2836,7 +2836,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) { 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 ui_rect_instance_desc *desc = &((struct ui_rect_instance_desc *)arena_base(rsig->ui_rect_instance_descs_arena))[i];
struct sh_ui_instance *instance = &ui_rect_instances[i]; struct sh_ui_instance *instance = &ui_rect_instances[i];
i32 texture_id = -1; u32 texture_id = 0xFFFFFFFF;
if (desc->texture != 0) { if (desc->texture != 0) {
texture_id = desc->texture->srv_descriptor->index; texture_id = desc->texture->srv_descriptor->index;
} else if (desc->sprite.hash != 0) { } else if (desc->sprite.hash != 0) {
@ -2846,7 +2846,7 @@ struct gp_resource *gp_run_render(struct gp_render_sig *gp_render_sig, struct gp
texture_id = texture->srv_descriptor->index; texture_id = texture->srv_descriptor->index;
} }
} }
instance->tex_nurid = sh_int_from_i32(texture_id); instance->tex_nurid = sh_uint_from_u32(texture_id);
instance->xf = sh_float2x3_from_xform(desc->xf); instance->xf = sh_float2x3_from_xform(desc->xf);
instance->uv0 = sh_float2_from_v2(desc->clip.p0); instance->uv0 = sh_float2_from_v2(desc->clip.p0);
instance->uv1 = sh_float2_from_v2(desc->clip.p1); instance->uv1 = sh_float2_from_v2(desc->clip.p1);

View File

@ -6,7 +6,7 @@
#define DECLS(t, n) t n : n #define DECLS(t, n) t n : n
#define resource_from_urid(urid) ResourceDescriptorHeap[(urid)] #define resource_from_urid(urid) ResourceDescriptorHeap[urid]
#define resource_from_nurid(nurid) ResourceDescriptorHeap[NonUniformResourceIndex(nurid)] #define resource_from_nurid(nurid) ResourceDescriptorHeap[NonUniformResourceIndex(nurid)]
#if !SH_CPU #if !SH_CPU

View File

@ -12,8 +12,8 @@ struct vs_input {
}; };
struct vs_output { struct vs_output {
nointerpolation DECLS(int, tex_nurid); nointerpolation DECLS(uint, tex_nurid);
nointerpolation DECLS(int, grid_id); nointerpolation DECLS(uint, grid_id);
DECLS(float2, uv); DECLS(float2, uv);
DECLS(float4, tint_lin); DECLS(float4, tint_lin);
DECLS(float4, emittance_lin); DECLS(float4, emittance_lin);
@ -61,13 +61,13 @@ SH_ENTRY struct ps_output ps(struct ps_input input)
float4 albedo = input.vs.tint_lin; float4 albedo = input.vs.tint_lin;
/* Texture */ /* Texture */
if (input.vs.tex_nurid >= 0) { if (input.vs.tex_nurid < 0xFFFFFFFF) {
Texture2D<float4> tex = resource_from_nurid(input.vs.tex_nurid); Texture2D<float4> tex = resource_from_nurid(input.vs.tex_nurid);
albedo *= tex.Sample(s_point_clamp, input.vs.uv); albedo *= tex.Sample(s_point_clamp, input.vs.uv);
} }
/* Grid */ /* Grid */
if (input.vs.grid_id >= 0) { if (input.vs.grid_id < 0xFFFFFFFF) {
StructuredBuffer<struct sh_material_grid> grids = resource_from_urid(sig.grids_urid); StructuredBuffer<struct sh_material_grid> grids = resource_from_urid(sig.grids_urid);
struct sh_material_grid grid = grids[input.vs.grid_id]; struct sh_material_grid grid = grids[input.vs.grid_id];
float2 grid_pos = input.vs.SV_Position.xy + grid.offset; float2 grid_pos = input.vs.SV_Position.xy + grid.offset;

View File

@ -3,6 +3,7 @@
#define SH_STRUCT(s) PACK(struct s) #define SH_STRUCT(s) PACK(struct s)
#define SH_DECL(t, n) struct CAT(sh_, t) n #define SH_DECL(t, n) struct CAT(sh_, t) n
#define SH_DECLS(t, n) SH_DECL(t, n) #define SH_DECLS(t, n) SH_DECL(t, n)
#define SH_STATIC_ASSERT(c) STATIC_ASSERT(c)
struct sh_uint { u32 v; }; struct sh_uint { u32 v; };
INLINE struct sh_uint sh_uint_from_u32(u32 v) INLINE struct sh_uint sh_uint_from_u32(u32 v)
@ -75,9 +76,14 @@ INLINE struct sh_float2x3 sh_float2x3_from_xform(struct xform v)
#define SH_STRUCT(s) struct s #define SH_STRUCT(s) struct s
#define SH_DECL(t, n) t n #define SH_DECL(t, n) t n
#define SH_DECLS(t, n) t n : n #define SH_DECLS(t, n) t n : n
#define SH_STATIC_ASSERT(c) _Static_assert(c, "")
#endif #endif
#define SH_ASSERT_ROOT_CONST(s, n) SH_STATIC_ASSERT((sizeof(s) % 16 == 0) && /* Root constant struct should pad to 16 byte alignment */ \
((sizeof(s) / 4) == n) && /* Root constant struct size should match the specified 32-bit-constant count */ \
(sizeof(s) <= 256)) /* Root constant struct can only fit 64 DWORDS */
/* ========================== * /* ========================== *
* Global textures * Global textures
* ========================== */ * ========================== */
@ -93,18 +99,19 @@ INLINE struct sh_float2x3 sh_float2x3_from_xform(struct xform v)
* ========================== */ * ========================== */
SH_STRUCT(sh_material_sig { SH_STRUCT(sh_material_sig {
/* ---------------------------------------------------- */ /* ----------------------------------------------------- */
SH_DECL(float4x4, projection); /* 16 consts */ SH_DECL(float4x4, projection); /* 16 consts */
/* ---------------------------------------------------- */ /* ----------------------------------------------------- */
SH_DECL(uint, instances_urid); /* 01 consts */ SH_DECL(uint, instances_urid); /* 01 consts */
SH_DECL(uint, grids_urid); /* 01 consts */ SH_DECL(uint, grids_urid); /* 01 consts */
SH_DECL(uint2, _pad0); /* 02 consts (padding) */ SH_DECL(uint2, _pad0); /* 02 consts (padding) */
/* ---------------------------------------------------- */ /* ----------------------------------------------------- */
}); });
SH_ASSERT_ROOT_CONST(struct sh_material_sig, 20);
SH_STRUCT(sh_material_instance { SH_STRUCT(sh_material_instance {
SH_DECL(int, tex_nurid); SH_DECL(uint, tex_nurid);
SH_DECL(int, grid_id); SH_DECL(uint, grid_id);
SH_DECL(float2x3, xf); SH_DECL(float2x3, xf);
SH_DECL(float2, uv0); SH_DECL(float2, uv0);
SH_DECL(float2, uv1); SH_DECL(float2, uv1);
@ -129,17 +136,18 @@ SH_STRUCT(sh_material_grid {
* ========================== */ * ========================== */
SH_STRUCT(sh_flood_sig { SH_STRUCT(sh_flood_sig {
/* ---------------------------------------------------- */ /* ----------------------------------------------------- */
SH_DECL(int, step_len); /* 01 consts */ SH_DECL(int, step_len); /* 01 consts */
SH_DECL(uint, emittance_tex_urid); /* 01 consts */ SH_DECL(uint, emittance_tex_urid); /* 01 consts */
SH_DECL(uint, read_flood_tex_urid); /* 01 consts */ SH_DECL(uint, read_flood_tex_urid); /* 01 consts */
SH_DECL(uint, target_flood_tex_urid); /* 01 consts */ SH_DECL(uint, target_flood_tex_urid); /* 01 consts */
/* ---------------------------------------------------- */ /* ----------------------------------------------------- */
SH_DECL(uint, tex_width); /* 01 consts */ SH_DECL(uint, tex_width); /* 01 consts */
SH_DECL(uint, tex_height); /* 01 consts */ SH_DECL(uint, tex_height); /* 01 consts */
SH_DECL(uint2, _pad0); /* 02 consts (padding) */ SH_DECL(uint2, _pad0); /* 02 consts (padding) */
/* ---------------------------------------------------- */ /* ----------------------------------------------------- */
}); });
SH_ASSERT_ROOT_CONST(struct sh_flood_sig, 8);
/* ========================== * /* ========================== *
* Shade shader structs * Shade shader structs
@ -149,34 +157,36 @@ SH_STRUCT(sh_flood_sig {
#define SH_SHADE_FLAG_DISABLE_EFFECTS (1 << 0) #define SH_SHADE_FLAG_DISABLE_EFFECTS (1 << 0)
SH_STRUCT(sh_shade_sig { SH_STRUCT(sh_shade_sig {
/* ---------------------------------------------------- */ /* ----------------------------------------------------- */
SH_DECL(uint4, frame_seed); /* 04 consts */ SH_DECL(uint4, frame_seed); /* 04 consts */
/* ---------------------------------------------------- */ /* ----------------------------------------------------- */
SH_DECL(uint, flags); /* 01 consts */ SH_DECL(uint, flags); /* 01 consts */
SH_DECL(uint, _pad0); /* 01 consts (padding) */ SH_DECL(uint, _pad0); /* 01 consts (padding) */
SH_DECL(uint, tex_width); /* 01 consts */ SH_DECL(uint, tex_width); /* 01 consts */
SH_DECL(uint, tex_height); /* 01 consts */ SH_DECL(uint, tex_height); /* 01 consts */
/* ---------------------------------------------------- */ /* ----------------------------------------------------- */
SH_DECL(float2, camera_offset); /* 02 consts */ SH_DECL(float2, camera_offset); /* 02 consts */
SH_DECL(uint, frame_index); /* 01 consts */ SH_DECL(uint, frame_index); /* 01 consts */
SH_DECL(uint, albedo_tex_urid); /* 01 consts */ SH_DECL(uint, albedo_tex_urid); /* 01 consts */
/* ---------------------------------------------------- */ /* ----------------------------------------------------- */
SH_DECL(uint, emittance_tex_urid); /* 01 consts */ SH_DECL(uint, emittance_tex_urid); /* 01 consts */
SH_DECL(uint, emittance_flood_tex_urid); /* 01 consts */ SH_DECL(uint, emittance_flood_tex_urid); /* 01 consts */
SH_DECL(uint, read_tex_urid); /* 01 consts */ SH_DECL(uint, read_tex_urid); /* 01 consts */
SH_DECL(uint, target_tex_urid); /* 01 consts */ SH_DECL(uint, target_tex_urid); /* 01 consts */
/* ---------------------------------------------------- */ /* ----------------------------------------------------- */
}); });
SH_ASSERT_ROOT_CONST(struct sh_shade_sig, 16);
/* ========================== * /* ========================== *
* Shape shader structs * Shape shader structs
* ========================== */ * ========================== */
SH_STRUCT(sh_shape_sig { SH_STRUCT(sh_shape_sig {
/* ---------------------------------------------------- */ /* ----------------------------------------------------- */
SH_DECL(float4x4, projection); /* 16 consts */ SH_DECL(float4x4, projection); /* 16 consts */
/* ---------------------------------------------------- */ /* ----------------------------------------------------- */
}); });
SH_ASSERT_ROOT_CONST(struct sh_shape_sig, 16);
SH_STRUCT(sh_shape_vert { SH_STRUCT(sh_shape_vert {
SH_DECLS(float2, pos); SH_DECLS(float2, pos);
@ -188,17 +198,18 @@ SH_STRUCT(sh_shape_vert {
* ========================== */ * ========================== */
SH_STRUCT(sh_ui_sig { SH_STRUCT(sh_ui_sig {
/* ---------------------------------------------------- */ /* ----------------------------------------------------- */
SH_DECL(float4x4, projection); /* 16 consts */ SH_DECL(float4x4, projection); /* 16 consts */
/* ---------------------------------------------------- */ /* ----------------------------------------------------- */
SH_DECL(uint, instances_urid); /* 01 consts */ SH_DECL(uint, instances_urid); /* 01 consts */
SH_DECL(uint3, _pad0); /* 03 consts (padding) */ SH_DECL(uint3, _pad0); /* 03 consts (padding) */
/* ---------------------------------------------------- */ /* ----------------------------------------------------- */
}); });
SH_ASSERT_ROOT_CONST(struct sh_ui_sig, 20);
SH_STRUCT(sh_ui_instance { SH_STRUCT(sh_ui_instance {
SH_DECL(int, tex_nurid); SH_DECL(uint, tex_nurid);
SH_DECL(int, grid_id); SH_DECL(uint, grid_id);
SH_DECL(float2x3, xf); SH_DECL(float2x3, xf);
SH_DECL(float2, uv0); SH_DECL(float2, uv0);
SH_DECL(float2, uv1); SH_DECL(float2, uv1);
@ -214,12 +225,13 @@ SH_STRUCT(sh_ui_instance {
#define SH_BLIT_FLAG_GAMMA_CORRECT (1 << 1) #define SH_BLIT_FLAG_GAMMA_CORRECT (1 << 1)
SH_STRUCT(sh_blit_sig { SH_STRUCT(sh_blit_sig {
/* ---------------------------------------------------- */ /* ----------------------------------------------------- */
SH_DECL(float4x4, projection); /* 16 consts */ SH_DECL(float4x4, projection); /* 16 consts */
/* ---------------------------------------------------- */ /* ----------------------------------------------------- */
SH_DECL(uint, flags); /* 01 consts */ SH_DECL(uint, flags); /* 01 consts */
SH_DECL(uint, tex_urid); /* 01 consts */ SH_DECL(uint, tex_urid); /* 01 consts */
SH_DECL(float, exposure); /* 01 consts */ SH_DECL(float, exposure); /* 01 consts */
SH_DECL(float, gamma); /* 01 consts */ SH_DECL(float, gamma); /* 01 consts */
/* ---------------------------------------------------- */ /* ----------------------------------------------------- */
}); });
SH_ASSERT_ROOT_CONST(struct sh_blit_sig, 20);

View File

@ -12,7 +12,7 @@ struct vs_input {
}; };
struct vs_output { struct vs_output {
nointerpolation DECLS(int, tex_nurid); nointerpolation DECLS(uint, tex_nurid);
DECLS(float2, uv); DECLS(float2, uv);
DECLS(float4, tint_srgb); DECLS(float4, tint_srgb);
DECLS(float4, SV_Position); DECLS(float4, SV_Position);
@ -58,7 +58,7 @@ SH_ENTRY struct ps_output ps(struct ps_input input)
float4 color = input.vs.tint_srgb; float4 color = input.vs.tint_srgb;
/* Texture */ /* Texture */
if (input.vs.tex_nurid >= 0) { if (input.vs.tex_nurid < 0xFFFFFFFF) {
Texture2D<float4> tex = resource_from_nurid(input.vs.tex_nurid); Texture2D<float4> tex = resource_from_nurid(input.vs.tex_nurid);
color *= tex.Sample(s_point_clamp, input.vs.uv); color *= tex.Sample(s_point_clamp, input.vs.uv);
} }