power_play/res/sh/sh_common.h

162 lines
4.1 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_32BIT(s, n) STATIC_ASSERT((sizeof(s) / 4) == n)
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_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_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_32BIT(s, n)
#endif
/* ========================== *
* Blit shader structures
* ========================== */
SH_STRUCT(sh_blit_constants {
SH_DECL(float4x4, projection);
SH_DECL(uint, tex_urid);
SH_DECL(float, gamma);
});
SH_ASSERT_32BIT(struct sh_blit_constants, 18); /* Expected 32bit root constant size in shader */
/* ========================== *
* Material shader structures
* ========================== */
SH_STRUCT(sh_material_constants {
SH_DECL(float4x4, projection);
});
SH_ASSERT_32BIT(struct sh_material_constants, 16); /* Expected 32bit root constant size in shader */
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, 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);
SH_DECL(uint, emittance_tex_urid);
SH_DECL(uint, read_flood_tex_urid);
SH_DECL(uint, write_flood_tex_urid);
SH_DECL(uint, tex_width);
SH_DECL(uint, tex_height);
});
SH_ASSERT_32BIT(struct sh_flood_constants, 6); /* Expected 32bit root constant size in shader */
/* ========================== *
* Shade shader structures
* ========================== */
SH_STRUCT(sh_shade_constants {
SH_DECL(float, time);
SH_DECL(uint, albedo_tex_urid);
SH_DECL(uint, emittance_tex_urid);
SH_DECL(uint, emittance_flood_tex_urid);
SH_DECL(uint, write_tex_urid);
SH_DECL(uint, tex_width);
SH_DECL(uint, tex_height);
});
SH_ASSERT_32BIT(struct sh_shade_constants, 7); /* Expected 32bit root constant size in shader */
/* ========================== *
* Shape shader structures
* ========================== */
SH_STRUCT(sh_shape_constants {
SH_DECL(float4x4, projection);
});
SH_ASSERT_32BIT(struct sh_shape_constants, 16); /* Expected 32bit root constant size in shader */
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);
});
SH_ASSERT_32BIT(struct sh_ui_constants, 16); /* Expected 32bit root constant size in shader */
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);
});