anvilkit_render/renderer/
state.rs1use bevy_ecs::prelude::*;
7
8#[repr(C)]
17#[derive(Copy, Clone, Debug, bytemuck::Pod, bytemuck::Zeroable)]
18pub struct GpuLight {
19 pub position_type: [f32; 4],
20 pub direction_range: [f32; 4],
21 pub color_intensity: [f32; 4],
22 pub params: [f32; 4],
23}
24
25impl Default for GpuLight {
26 fn default() -> Self {
27 Self {
28 position_type: [0.0; 4],
29 direction_range: [0.0, -1.0, 0.0, 0.0],
30 color_intensity: [0.0; 4],
31 params: [0.0; 4],
32 }
33 }
34}
35
36pub const MAX_LIGHTS: usize = 8;
38
39#[repr(C)]
44#[derive(Copy, Clone, bytemuck::Pod, bytemuck::Zeroable)]
45pub struct PbrSceneUniform {
46 pub model: [[f32; 4]; 4], pub view_proj: [[f32; 4]; 4], pub normal_matrix: [[f32; 4]; 4], pub camera_pos: [f32; 4], pub light_dir: [f32; 4], pub light_color: [f32; 4], pub material_params: [f32; 4], pub lights: [GpuLight; MAX_LIGHTS], pub shadow_view_proj: [[f32; 4]; 4], pub emissive_factor: [f32; 4], }
60
61impl Default for PbrSceneUniform {
62 fn default() -> Self {
63 Self {
64 model: glam::Mat4::IDENTITY.to_cols_array_2d(),
65 view_proj: glam::Mat4::IDENTITY.to_cols_array_2d(),
66 normal_matrix: glam::Mat4::IDENTITY.to_cols_array_2d(),
67 camera_pos: [0.0; 4],
68 light_dir: [0.0, -1.0, 0.0, 0.0],
69 light_color: [1.0, 1.0, 1.0, 3.0],
70 material_params: [0.0, 0.5, 1.0, 0.0],
71 lights: [GpuLight::default(); MAX_LIGHTS],
72 shadow_view_proj: glam::Mat4::IDENTITY.to_cols_array_2d(),
73 emissive_factor: [0.0; 4],
74 }
75 }
76}
77
78#[derive(Resource)]
83pub struct RenderState {
84 pub surface_format: wgpu::TextureFormat,
85 pub surface_size: (u32, u32),
86 pub scene_uniform_buffer: wgpu::Buffer,
87 pub scene_bind_group: wgpu::BindGroup,
88 pub scene_bind_group_layout: wgpu::BindGroupLayout,
89 pub depth_texture_view: wgpu::TextureView,
90 pub hdr_texture_view: wgpu::TextureView,
92 pub tonemap_pipeline: wgpu::RenderPipeline,
93 pub tonemap_bind_group: wgpu::BindGroup,
94 pub tonemap_bind_group_layout: wgpu::BindGroupLayout,
95 pub ibl_shadow_bind_group: wgpu::BindGroup,
97 pub ibl_shadow_bind_group_layout: wgpu::BindGroupLayout,
98 pub shadow_pipeline: wgpu::RenderPipeline,
100 pub shadow_map_view: wgpu::TextureView,
101 pub hdr_msaa_texture_view: wgpu::TextureView,
103}
104
105#[cfg(test)]
106mod tests {
107 use super::*;
108
109 #[test]
110 fn test_pbr_scene_uniform_size() {
111 assert_eq!(std::mem::size_of::<PbrSceneUniform>(), 848);
112 }
113
114 #[test]
115 fn test_gpu_light_size() {
116 assert_eq!(std::mem::size_of::<GpuLight>(), 64);
117 }
118
119 #[test]
120 fn test_pbr_scene_uniform_default() {
121 let u = PbrSceneUniform::default();
122 assert_eq!(u.model[0][0], 1.0);
124 assert_eq!(u.model[1][1], 1.0);
125 assert_eq!(u.light_dir[1], -1.0);
127 assert_eq!(u.material_params[1], 0.5);
129 assert_eq!(u.material_params[2], 1.0);
131 }
132}