#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_STATIC_ASSERT(c) STATIC_ASSERT(c) 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_STATIC_ASSERT(c) _Static_assert(c, "") #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 * ========================== */ /* 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 structs * ========================== */ SH_STRUCT(sh_material_sig { /* ----------------------------------------------------- */ SH_DECL(float4x4, projection); /* 16 consts */ /* ----------------------------------------------------- */ SH_DECL(uint, instances_urid); /* 01 consts */ SH_DECL(uint, grids_urid); /* 01 consts */ SH_DECL(uint, _pad0); /* 01 consts (padding) */ SH_DECL(uint, _pad1); /* 01 consts (padding) */ /* ----------------------------------------------------- */ }); SH_ASSERT_ROOT_CONST(struct sh_material_sig, 20); SH_STRUCT(sh_material_instance { SH_DECL(uint, tex_nurid); SH_DECL(uint, 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 structs * ========================== */ SH_STRUCT(sh_flood_sig { /* ----------------------------------------------------- */ 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(uint, _pad0); /* 01 consts (padding) */ SH_DECL(uint, _pad1); /* 01 consts (padding) */ /* ----------------------------------------------------- */ }); SH_ASSERT_ROOT_CONST(struct sh_flood_sig, 8); /* ========================== * * Shade shader structs * ========================== */ #define SH_SHADE_FLAG_NONE (0 << 0) #define SH_SHADE_FLAG_DISABLE_EFFECTS (1 << 0) SH_STRUCT(sh_shade_sig { /* ----------------------------------------------------- */ 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_sig, 16); /* ========================== * * Shape shader structs * ========================== */ SH_STRUCT(sh_shape_sig { /* ----------------------------------------------------- */ SH_DECL(float4x4, projection); /* 16 consts */ /* ----------------------------------------------------- */ SH_DECL(uint, verts_urid); /* 01 consts */ SH_DECL(uint, _pad0); /* 01 consts (padding) */ SH_DECL(uint, _pad1); /* 01 consts (padding) */ SH_DECL(uint, _pad2); /* 01 consts (padding) */ /* ----------------------------------------------------- */ }); SH_ASSERT_ROOT_CONST(struct sh_shape_sig, 20); SH_STRUCT(sh_shape_vert { SH_DECL(float2, pos); SH_DECL(uint, color_srgb); }); /* ========================== * * UI shader structs * ========================== */ SH_STRUCT(sh_ui_sig { /* ----------------------------------------------------- */ SH_DECL(float4x4, projection); /* 16 consts */ /* ----------------------------------------------------- */ SH_DECL(uint, instances_urid); /* 01 consts */ SH_DECL(uint, _pad0); /* 01 consts (padding) */ SH_DECL(uint, _pad1); /* 01 consts (padding) */ SH_DECL(uint, _pad2); /* 01 consts (padding) */ /* ----------------------------------------------------- */ }); SH_ASSERT_ROOT_CONST(struct sh_ui_sig, 20); SH_STRUCT(sh_ui_instance { SH_DECL(uint, tex_nurid); SH_DECL(uint, grid_id); SH_DECL(float2x3, xf); SH_DECL(float2, uv0); SH_DECL(float2, uv1); SH_DECL(uint, tint_srgb); }); /* ========================== * * Blit shader structs * ========================== */ #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_sig { /* ----------------------------------------------------- */ 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_sig, 20);