power_play/src/sh/sh_common.h
2025-07-25 13:43:47 -05:00

232 lines
7.9 KiB
C

#if SH_CPU
#define SH_STRUCT(s) PACK(struct s)
#define SH_DECL(t, n) struct CAT(sh_, t) n
#define SH_DECLS(t, n) SH_DECL(t, n)
#define SH_ENTRY(rootsig) static
#define SH_ASSERT_ROOT_CONST(s, n) 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 */
struct sh_uint { u32 v; };
INLINE struct sh_uint sh_uint_from_u32(u32 v)
{
return (struct sh_uint) { .v = v };
}
struct sh_int { i32 v; };
INLINE struct sh_int sh_int_from_i32(i32 v)
{
return (struct sh_int) { .v = v };
}
struct sh_uint2 { u32 v[2]; };
INLINE struct sh_uint2 sh_uint2_from_u32(u32 x, u32 y)
{
return (struct sh_uint2) { .v[0] = x, .v[1] = y };
}
struct sh_uint3 { u32 v[3]; };
INLINE struct sh_uint3 sh_uint3_from_u32(u32 x, u32 y, u32 z)
{
return (struct sh_uint3) { .v[0] = x, .v[1] = y, .v[2] = z };
}
struct sh_uint4 { u32 v[4]; };
INLINE struct sh_uint4 sh_uint4_from_u32(u32 x, u32 y, u32 z, u32 w)
{
return (struct sh_uint4) { .v[0] = x, .v[1] = y, .v[2] = z, .v[3] = w };
}
struct sh_float { f32 v; };
INLINE struct sh_float sh_float_from_f32(f32 v)
{
return (struct sh_float) { .v = v };
}
struct sh_float2 { f32 v[2]; };
INLINE struct sh_float2 sh_float2_from_v2(struct v2 v)
{
return (struct sh_float2) { .v[0] = v.x, .v[1] = v.y };
}
struct sh_float3 { f32 v[3]; };
INLINE struct sh_float3 sh_float3_from_v3(struct v3 v)
{
return (struct sh_float3) { .v[0] = v.x, .v[1] = v.y, .v[2] = v.z };
}
struct sh_float4x4 { f32 v[4][4]; };
INLINE struct sh_float4x4 sh_float4x4_from_mat4x4(struct mat4x4 v)
{
struct sh_float4x4 res;
STATIC_ASSERT(sizeof(res) == sizeof(v));
MEMCPY(&res, v.e, sizeof(res));
return res;
}
struct sh_float2x3 { f32 v[2][3]; };
INLINE struct sh_float2x3 sh_float2x3_from_xform(struct xform v)
{
struct sh_float2x3 res;
STATIC_ASSERT(sizeof(res) == sizeof(v));
MEMCPY(&res, &v, sizeof(res));
return res;
}
#else
#define SH_STRUCT(s) struct s
#define SH_DECL(t, n) t n
#define SH_DECLS(t, n) t n : n
#define SH_ENTRY(rootsig) [RootSignature(rootsig)]
#define SH_ASSERT_ROOT_CONST(s, n)
#endif
/* ========================== *
* Global textures
* ========================== */
/* Blue noise */
#define SH_BLUE_NOISE_TEX_ID 0
#define SH_BLUE_NOISE_TEX_WIDTH 128
#define SH_BLUE_NOISE_TEX_HEIGHT 128
#define SH_BLUE_NOISE_TEX_DEPTH 64
/* ========================== *
* Material shader structures
* ========================== */
SH_STRUCT(sh_material_constants {
/* ---------------------------------------------------- */
SH_DECL(float4x4, projection); /* 16 consts */
/* ---------------------------------------------------- */
});
SH_ASSERT_ROOT_CONST(struct sh_material_constants, 16); /* Expected to match num32BitConstants in shader's root signature */
SH_STRUCT(sh_material_instance {
SH_DECL(int, tex_nurid);
SH_DECL(int, grid_id);
SH_DECL(float2x3, xf);
SH_DECL(float2, uv0);
SH_DECL(float2, uv1);
SH_DECL(uint, tint_srgb);
SH_DECL(uint, is_light);
SH_DECL(float3, light_emittance_srgb);
});
SH_STRUCT(sh_material_grid {
SH_DECL(float, line_thickness);
SH_DECL(float, line_spacing);
SH_DECL(float2, offset);
SH_DECL(uint, bg0_srgb);
SH_DECL(uint, bg1_srgb);
SH_DECL(uint, line_srgb);
SH_DECL(uint, x_srgb);
SH_DECL(uint, y_srgb);
});
/* ========================== *
* Flood shader structures
* ========================== */
SH_STRUCT(sh_flood_constants {
/* ---------------------------------------------------- */
SH_DECL(int, step_len); /* 01 consts */
SH_DECL(uint, emittance_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, tex_width); /* 01 consts */
SH_DECL(uint, tex_height); /* 01 consts */
SH_DECL(uint2, _pad0); /* 02 consts (padding) */
/* ---------------------------------------------------- */
});
SH_ASSERT_ROOT_CONST(struct sh_flood_constants, 8); /* Expected to match num32BitConstants in shader's root signature */
/* ========================== *
* Shade shader structures
* ========================== */
#define SH_SHADE_FLAG_NONE (0 << 0)
#define SH_SHADE_FLAG_DISABLE_EFFECTS (1 << 0)
SH_STRUCT(sh_shade_constants {
/* ---------------------------------------------------- */
SH_DECL(uint4, frame_seed); /* 04 consts */
/* ---------------------------------------------------- */
SH_DECL(uint, flags); /* 01 consts */
SH_DECL(uint, _pad0); /* 01 consts (padding) */
SH_DECL(uint, tex_width); /* 01 consts */
SH_DECL(uint, tex_height); /* 01 consts */
/* ---------------------------------------------------- */
SH_DECL(float2, camera_offset); /* 02 consts */
SH_DECL(uint, frame_index); /* 01 consts */
SH_DECL(uint, albedo_tex_urid); /* 01 consts */
/* ---------------------------------------------------- */
SH_DECL(uint, emittance_tex_urid); /* 01 consts */
SH_DECL(uint, emittance_flood_tex_urid); /* 01 consts */
SH_DECL(uint, read_tex_urid); /* 01 consts */
SH_DECL(uint, target_tex_urid); /* 01 consts */
/* ---------------------------------------------------- */
});
SH_ASSERT_ROOT_CONST(struct sh_shade_constants, 16); /* Expected to match num32BitConstants in shader's root signature */
/* ========================== *
* Shape shader structures
* ========================== */
SH_STRUCT(sh_shape_constants {
/* ---------------------------------------------------- */
SH_DECL(float4x4, projection); /* 16 consts */
/* ---------------------------------------------------- */
});
SH_ASSERT_ROOT_CONST(struct sh_shape_constants, 16); /* Expected to match num32BitConstants in shader's root signature */
SH_STRUCT(sh_shape_vert {
SH_DECLS(float2, pos);
SH_DECLS(uint, color_srgb);
});
/* ========================== *
* UI shader structures
* ========================== */
SH_STRUCT(sh_ui_constants {
/* ---------------------------------------------------- */
SH_DECL(float4x4, projection); /* 16 consts */
/* ---------------------------------------------------- */
});
SH_ASSERT_ROOT_CONST(struct sh_ui_constants, 16); /* Expected to match num32BitConstants in shader's root signature */
SH_STRUCT(sh_ui_instance {
SH_DECL(int, tex_nurid);
SH_DECL(int, grid_id);
SH_DECL(float2x3, xf);
SH_DECL(float2, uv0);
SH_DECL(float2, uv1);
SH_DECL(uint, tint_srgb);
});
/* ========================== *
* Blit shader structures
* ========================== */
#define SH_BLIT_FLAG_NONE (0 << 0)
#define SH_BLIT_FLAG_TONE_MAP (1 << 0)
#define SH_BLIT_FLAG_GAMMA_CORRECT (1 << 1)
SH_STRUCT(sh_blit_constants {
/* ---------------------------------------------------- */
SH_DECL(float4x4, projection); /* 16 consts */
/* ---------------------------------------------------- */
SH_DECL(uint, flags); /* 01 consts */
SH_DECL(uint, tex_urid); /* 01 consts */
SH_DECL(float, exposure); /* 01 consts */
SH_DECL(float, gamma); /* 01 consts */
/* ---------------------------------------------------- */
});
SH_ASSERT_ROOT_CONST(struct sh_blit_constants, 20); /* Expected to match num32BitConstants in shader's root signature */