Skip to main content

gizmo_renderer/
gpu_types.rs

1use bytemuck::{Pod, Zeroable};
2
3#[repr(C)]
4#[derive(Copy, Clone, Debug, Pod, Zeroable)]
5pub struct Vertex {
6    pub position: [f32; 3],
7    pub color: [f32; 3],
8    pub normal: [f32; 3],
9    pub tex_coords: [f32; 2],
10    pub joint_indices: [u32; 4],
11    pub joint_weights: [f32; 4],
12    pub tangent: [f32; 4],
13}
14
15impl Default for Vertex {
16    fn default() -> Self {
17        Self {
18            position: [0.0; 3],
19            color: [1.0; 3],
20            normal: [0.0, 1.0, 0.0],
21            tex_coords: [0.0; 2],
22            joint_indices: [0; 4],
23            joint_weights: [0.0; 4],
24            tangent: [1.0, 0.0, 0.0, 1.0],
25        }
26    }
27}
28
29impl Vertex {
30    pub fn desc<'a>() -> wgpu::VertexBufferLayout<'a> {
31        wgpu::VertexBufferLayout {
32            array_stride: std::mem::size_of::<Vertex>() as wgpu::BufferAddress,
33            step_mode: wgpu::VertexStepMode::Vertex,
34            attributes: &[
35                wgpu::VertexAttribute {
36                    offset: 0,
37                    shader_location: 0,
38                    format: wgpu::VertexFormat::Float32x3,
39                },
40                wgpu::VertexAttribute {
41                    offset: std::mem::size_of::<[f32; 3]>() as wgpu::BufferAddress,
42                    shader_location: 1,
43                    format: wgpu::VertexFormat::Float32x3,
44                },
45                wgpu::VertexAttribute {
46                    offset: std::mem::size_of::<[f32; 6]>() as wgpu::BufferAddress,
47                    shader_location: 2,
48                    format: wgpu::VertexFormat::Float32x3,
49                },
50                wgpu::VertexAttribute {
51                    offset: std::mem::size_of::<[f32; 9]>() as wgpu::BufferAddress,
52                    shader_location: 3,
53                    format: wgpu::VertexFormat::Float32x2,
54                },
55                wgpu::VertexAttribute {
56                    offset: std::mem::size_of::<[f32; 11]>() as wgpu::BufferAddress,
57                    shader_location: 4,
58                    format: wgpu::VertexFormat::Uint32x4,
59                },
60                wgpu::VertexAttribute {
61                    offset: (std::mem::size_of::<[f32; 11]>() + std::mem::size_of::<[u32; 4]>())
62                        as wgpu::BufferAddress,
63                    shader_location: 5,
64                    format: wgpu::VertexFormat::Float32x4,
65                },
66                wgpu::VertexAttribute {
67                    offset: (std::mem::size_of::<[f32; 11]>() + std::mem::size_of::<[u32; 4]>() + std::mem::size_of::<[f32; 4]>())
68                        as wgpu::BufferAddress,
69                    shader_location: 6,
70                    format: wgpu::VertexFormat::Float32x4,
71                },
72            ],
73        }
74    }
75}
76
77#[repr(C)]
78#[derive(Copy, Clone, Debug, Pod, Zeroable)]
79pub struct LightData {
80    pub position: [f32; 4],  // xyz=pos, w=intensity
81    pub color: [f32; 4],     // rgb=color, a=radius
82    pub direction: [f32; 4], // xyz=direction (spot), w=inner_cutoff_cos
83    pub params: [f32; 4], // x=outer_cutoff_cos, y=light_type (0=point,1=spot,2=directional), zw=unused
84}
85
86#[repr(C)]
87#[derive(Copy, Clone, Debug, Pod, Zeroable)]
88pub struct PostProcessUniforms {
89    pub bloom_intensity: f32,
90    pub bloom_threshold: f32,
91    pub exposure: f32,
92    pub chromatic_aberration: f32,
93    pub vignette_intensity: f32,
94    pub film_grain_intensity: f32,
95    pub dof_focus_dist: f32,
96    pub dof_focus_range: f32,
97    pub dof_blur_size: f32,
98    pub _padding: [f32; 3],
99}
100
101/// Uniform block for the shadow pass vertex shader only (one cascade matrix per draw).
102#[repr(C)]
103#[derive(Copy, Clone, Debug, Pod, Zeroable)]
104pub struct ShadowVsUniform {
105    pub light_view_proj: [[f32; 4]; 4],
106}
107
108#[repr(C)]
109#[derive(Copy, Clone, Debug, Pod, Zeroable)]
110pub struct SceneUniforms {
111    pub view_proj: [[f32; 4]; 4],
112    pub camera_pos: [f32; 4],
113    pub sun_direction: [f32; 4],
114    pub sun_color: [f32; 4],
115    pub lights: [LightData; 10],
116    /// Directional CSM: world → light clip space per cascade (same order as shadow array layers).
117    pub light_view_proj: [[[f32; 4]; 4]; 4],
118    /// Far distance (along `camera_forward`) for cascades 0..3; `w` is always camera far plane.
119    pub cascade_splits: [f32; 4],
120    /// xyz = normalized camera forward in world space (for view-depth cascade selection).
121    pub camera_forward: [f32; 4],
122    /// x = camera z_near, y = 1 / shadow map resolution (PCF texel size), zw unused.
123    pub cascade_params: [f32; 4],
124    pub num_lights: u32,
125    pub exposure: f32,
126    pub _pre_align_pad: [u32; 2], // offset 1064-1071
127    pub _align_pad: [u32; 3],     // offset 1072-1083
128    pub environment_blend_t: f32, // offset 1084-1087
129    pub environment_preset: u32,  // offset 1088-1091
130    pub point_shadows_enabled: u32, // offset 1092-1095
131    pub environment_preset_2: u32, // offset 1096-1099
132    pub shading_mode: u32,        // offset 1100-1103
133                           // Total: 1104 bytes
134}
135
136#[repr(C)]
137#[derive(Copy, Clone, Debug, Pod, Zeroable)]
138pub struct InstanceRaw {
139    pub model: [[f32; 4]; 4],
140    pub albedo_color: [f32; 4],
141    pub roughness: f32,
142    pub metallic: f32,
143    pub unlit: f32,
144    pub _padding: f32,
145}