| 1 | //! 3D WGSL shader source and GPU vertex/uniform buffer layouts. |
| 2 | |
| 3 | use bytemuck::{Pod, Zeroable}; |
| 4 | |
| 5 | use super::fx::SurfaceFx; |
| 6 | |
| 7 | /// Point and spot lights each get this many shader slots. |
| 8 | pub(super) const MAX_LIGHTS: usize = 8; |
| 9 | /// Directional lights get this many shader slots. |
| 10 | pub(super) const MAX_DIR_LIGHTS: usize = 4; |
| 11 | /// Point lights that can cast shadows in one render. |
| 12 | pub(super) const MAX_SHADOW_LIGHTS: usize = 4; |
| 13 | /// Edge length in texels of each cube-map face. |
| 14 | pub(super) const SHADOW_MAP_SIZE: u32 = 1024; |
| 15 | |
| 16 | #[repr(C)] |
| 17 | #[derive(Clone, Copy, Debug, Pod, Zeroable)] |
| 18 | pub struct Vertex3D { |
| 19 | pub pos: [f32; 3], |
| 20 | pub normal: [f32; 3], |
| 21 | pub uv: [f32; 2], |
| 22 | } |
| 23 | |
| 24 | #[repr(C)] |
| 25 | #[derive(Clone, Copy, Pod, Zeroable)] |
| 26 | pub(super) struct MeshUniform { |
| 27 | pub(super) mvp: [f32; 16], |
| 28 | pub(super) model: [f32; 16], |
| 29 | /// Camera view matrix (used by `MtlChanBool` Eye-space position/normal operands). |
| 30 | pub(super) view: [f32; 16], |
| 31 | /// DoD `[origin_x, origin_y, canvas_w, canvas_h]` (zeros if standard texture) |
| 32 | pub(super) dod: [f32; 4], |
| 33 | /// `[use_diffuse_tex, lighting_enabled, specular_intensity, flip_normals]` |
| 34 | pub(super) flags: [f32; 4], |
| 35 | /// Diffuse color multiplier `(r, g, b, a)` |
| 36 | pub(super) diffuse_col: [f32; 4], |
| 37 | /// Specular color `(r, g, b, shininess / roughness / ward_spread_u)` |
| 38 | pub(super) specular_col: [f32; 4], |
| 39 | /// Ambient light `(r, g, b, 0)` |
| 40 | pub(super) ambient: [f32; 4], |
| 41 | /// Directional light directions TO light `(dx, dy, dz, 0)` |
| 42 | pub(super) dir_to_light: [[f32; 4]; MAX_DIR_LIGHTS], |
| 43 | /// Directional light colors * intensity `(r, g, b, 0)` |
| 44 | pub(super) dir_color: [[f32; 4]; MAX_DIR_LIGHTS], |
| 45 | /// Camera world position `(cx, cy, cz, 0)` |
| 46 | pub(super) cam_pos: [f32; 4], |
| 47 | /// `[num_point_lights, num_spot_lights, num_dir_lights, receives_shadows]` |
| 48 | pub(super) light_counts: [f32; 4], |
| 49 | /// UV affine matrix, column-major `[m00, m10, m01, m11]` |
| 50 | pub(super) uv_xform: [f32; 4], |
| 51 | /// UV translation, wrap mode and unlit light-pass count `[tx, ty, wrap, passes]` |
| 52 | pub(super) uv_off_wrap: [f32; 4], |
| 53 | /// `[bump_scale, bump_mode, bump_only, spec_model: 0=Blinn 1=Phong 2=CookTorrance 3=Pbr 4=Ward]` |
| 54 | pub(super) mat_fx: [f32; 4], |
| 55 | /// Bump UV affine matrix, column-major `[m00, m10, m01, m11]` |
| 56 | pub(super) bump_uv_xform: [f32; 4], |
| 57 | /// Bump UV translation, wrap mode and presence `[tx, ty, wrap, has_bump_tex]` |
| 58 | pub(super) bump_off_wrap: [f32; 4], |
| 59 | /// Bump DoD `[origin_x, origin_y, canvas_w, canvas_h]` (zeros if standard texture) |
| 60 | pub(super) bump_dod: [f32; 4], |
| 61 | /// `[bump_clamp_z, bump_channel, bump_filter_size, 0]` |
| 62 | pub(super) bump_params: [f32; 4], |
| 63 | /// `[diffuse_color_alpha, refractive_index, do_fresnel, ward_spread_v]` |
| 64 | pub(super) mtl_params: [f32; 4], |
| 65 | /// Environment inverse rotation col 0 + `env_mode` (`0=none, 1=SphereMap, 2=MtlReflect`) |
| 66 | pub(super) env_rot_0: [f32; 4], |
| 67 | /// Environment inverse rotation col 1 + `env_map_mode` (see `sample_sphere_map`) |
| 68 | pub(super) env_rot_1: [f32; 4], |
| 69 | /// Environment inverse rotation col 2 + `env_tex_slot` (`0=solid, 1=mat_tex, 2=bump_tex`) |
| 70 | pub(super) env_rot_2: [f32; 4], |
| 71 | /// `[strength_variability, constant_strength, 0, 0]` |
| 72 | pub(super) refl_strength: [f32; 4], |
| 73 | /// `[glancing_strength, face_on_strength, falloff, refract_ior]` |
| 74 | pub(super) refl_params: [f32; 4], |
| 75 | /// `[refl_tint_r, refl_tint_g, refl_tint_b, has_bg_material]` |
| 76 | pub(super) refl_tint: [f32; 4], |
| 77 | /// `[refract_tint_r, refract_tint_g, refract_tint_b, 0]` |
| 78 | pub(super) refract_tint: [f32; 4], |
| 79 | /// `MtlChanBool` operand A indices `[r, g, b, a]` |
| 80 | pub(super) cb_ops_a: [f32; 4], |
| 81 | /// `MtlChanBool` operation indices `[r, g, b, a]` |
| 82 | pub(super) cb_ops: [f32; 4], |
| 83 | /// `MtlChanBool` operand B indices `[r, g, b, a]` |
| 84 | pub(super) cb_ops_b: [f32; 4], |
| 85 | /// `[cb_enabled, cb_coord_space (0=Model,1=World,2=Eye), 0, 0]` |
| 86 | pub(super) cb_flags: [f32; 4], |
| 87 | /// Point light positions `(x, y, z, decay_type: 0=none, 1=linear, 2=quad)` |
| 88 | pub(super) point_pos: [[f32; 4]; MAX_LIGHTS], |
| 89 | /// Point light colors * intensity `(r, g, b, 0)` |
| 90 | pub(super) point_col: [[f32; 4]; MAX_LIGHTS], |
| 91 | /// Spot light positions `(x, y, z, cos_inner)` |
| 92 | pub(super) spot_pos: [[f32; 4]; MAX_LIGHTS], |
| 93 | /// Spot light forward directions `(dx, dy, dz, cos_outer)` |
| 94 | pub(super) spot_dir: [[f32; 4]; MAX_LIGHTS], |
| 95 | /// Spot light colors * intensity `(r, g, b, decay_type)` |
| 96 | pub(super) spot_col: [[f32; 4]; MAX_LIGHTS], |
| 97 | pub(super) sfx: SurfaceFx, |
| 98 | } |
| 99 | |
| 100 | #[repr(C)] |
| 101 | #[derive(Clone, Copy, Pod, Zeroable)] |
| 102 | pub(super) struct ShadowUniform { |
| 103 | /// Cube-face view-projections, six per shadow slot (`+X -X +Y -Y +Z -Z`). |
| 104 | pub(super) face_vp: [[f32; 16]; 6 * MAX_SHADOW_LIGHTS], |
| 105 | /// Per point light `(shadow slot or -1, density, 0, 0)`. |
| 106 | pub(super) point: [[f32; 4]; MAX_LIGHTS], |
| 107 | } |
| 108 | |
| 109 | #[repr(C)] |
| 110 | #[derive(Clone, Copy, Pod, Zeroable)] |
| 111 | pub(super) struct DepthToEyeZUniform { |
| 112 | /// `[P[2][2], P[2][3], P[3][2], P[3][3]]` from the camera projection matrix. |
| 113 | pub(super) proj_zw: [f32; 4], |
| 114 | /// `[EMPTY_Z_SENTINEL, 0.0, 0.0, 0.0]`. |
| 115 | pub(super) params: [f32; 4], |
| 116 | } |
| 117 | |
| 118 | /// Compute shader converting `Depth32Float` NDC depth (`[0, 1]`) into exact eye-space `Z` |
| 119 | /// (`R32Float`, negative in front of camera, `EMPTY_Z_SENTINEL` on empty sky pixels). |
| 120 | pub(super) const DEPTH_TO_EYE_Z_SHADER: &str = r#" |
| 121 | struct DepthToEyeZParams { |
| 122 | proj_zw: vec4<f32>, |
| 123 | params: vec4<f32>, |
| 124 | }; |
| 125 | |
| 126 | @group(0) @binding(0) var depth_tex: texture_depth_2d; |
| 127 | @group(0) @binding(1) var out_z: texture_storage_2d<r32float, write>; |
| 128 | @group(0) @binding(2) var<uniform> u: DepthToEyeZParams; |
| 129 | |
| 130 | @compute @workgroup_size(8, 8, 1) |
| 131 | fn depth_to_eye_z(@builtin(global_invocation_id) gid: vec3<u32>) { |
| 132 | let dims = textureDimensions(out_z); |
| 133 | if (gid.x >= dims.x || gid.y >= dims.y) { |
| 134 | return; |
| 135 | } |
| 136 | let d = textureLoad(depth_tex, vec2<i32>(gid.xy), 0); |
| 137 | var z_eye = u.params.x; |
| 138 | if (d < 1.0 - 1e-6) { |
| 139 | let p22 = u.proj_zw.x; |
| 140 | let p23 = u.proj_zw.y; |
| 141 | let p32 = u.proj_zw.z; |
| 142 | let p33 = u.proj_zw.w; |
| 143 | let denom = p22 - d * p23; |
| 144 | if (abs(denom) > 1e-12) { |
| 145 | z_eye = (d * p33 - p32) / denom; |
| 146 | } |
| 147 | } |
| 148 | textureStore(out_z, vec2<i32>(gid.xy), vec4<f32>(z_eye, 0.0, 0.0, 0.0)); |
| 149 | } |
| 150 | "#; |
| 151 | |
| 152 | /// UV affine plus wrap (`off_wrap.z`: 0 clamp, 1 wrap, 2 mirror), shared by both pipelines. |
| 153 | pub(super) const UV_XFORM_WGSL: &str = r#" |
| 154 | fn wrap_uv_only(uv_in: vec2<f32>, wrap_f: f32) -> vec2<f32> { |
| 155 | let wrap = i32(wrap_f + 0.5); |
| 156 | if (wrap == 1) { |
| 157 | return fract(uv_in); |
| 158 | } else if (wrap == 2) { |
| 159 | return vec2<f32>(1.0) - abs(fract(uv_in * 0.5) * 2.0 - vec2<f32>(1.0)); |
| 160 | } |
| 161 | return clamp(uv_in, vec2<f32>(0.0), vec2<f32>(1.0)); |
| 162 | } |
| 163 | |
| 164 | fn apply_uv_xform(raw_uv: vec2<f32>, m: vec4<f32>, off_wrap: vec4<f32>) -> vec2<f32> { |
| 165 | let uv = vec2<f32>(m.x * raw_uv.x + m.z * raw_uv.y, m.y * raw_uv.x + m.w * raw_uv.y) + off_wrap.xy; |
| 166 | let wrap = i32(off_wrap.z + 0.5); |
| 167 | if (wrap == 1 || wrap == 2) { |
| 168 | return wrap_uv_only(uv, off_wrap.z); |
| 169 | } |
| 170 | return uv; |
| 171 | } |
| 172 | "#; |
| 173 | |
| 174 | /// Per-caster `model` plus per-face view-projection and light position; writes the distance to |
| 175 | /// the light so the main pass can compare it against a fragment's own distance. Fusion's |
| 176 | /// `RendererOpenGL` shadow pass renders geometry depth regardless of texture or material alpha. |
| 177 | pub(super) const SHADOW_SHADER: &str = r#" |
| 178 | struct Caster { model: mat4x4<f32> }; |
| 179 | struct Face { vp: mat4x4<f32>, light_pos: vec4<f32> }; |
| 180 | |
| 181 | @group(0) @binding(0) var<uniform> caster: Caster; |
| 182 | @group(1) @binding(0) var<uniform> face: Face; |
| 183 | |
| 184 | struct VsOut { |
| 185 | @builtin(position) clip: vec4<f32>, |
| 186 | @location(0) world_pos: vec3<f32>, |
| 187 | }; |
| 188 | |
| 189 | @vertex |
| 190 | fn vs_shadow(@location(0) pos: vec3<f32>) -> VsOut { |
| 191 | let wp = caster.model * vec4<f32>(pos, 1.0); |
| 192 | return VsOut(face.vp * wp, wp.xyz); |
| 193 | } |
| 194 | |
| 195 | @fragment |
| 196 | fn fs_shadow(in: VsOut) -> @location(0) f32 { |
| 197 | return distance(in.world_pos, face.light_pos.xyz); |
| 198 | } |
| 199 | "#; |
| 200 | |
| 201 | /// WGSL source; `MAX_LIGHTS`, `MAX_DIR_LIGHTS`, `MAX_SHADOW_LIGHTS` and `SHADOW_MAP_SIZE` are |
| 202 | /// substituted with the Rust constants at pipeline creation. |
| 203 | pub(super) const SHADER_3D: &str = include_str!("mesh.wgsl"); |