wgpu-renderer/src/core/state.rs

736 lines
28 KiB
Rust
Raw Normal View History

2022-10-02 18:59:20 +03:00
use cgmath::prelude::*;
2023-11-04 02:40:00 +02:00
use wgpu::{InstanceDescriptor, Backends, TextureView, TextureViewDescriptor, StoreOp};
2023-04-13 20:30:28 +03:00
use std::default::Default;
use std::mem;
2022-10-02 18:59:20 +03:00
use std::time::Duration;
2022-10-03 22:31:01 +03:00
use wgpu::util::DeviceExt;
2022-10-01 23:58:09 +03:00
use winit::{event::*, window::Window};
2022-10-02 18:59:20 +03:00
use super::camera::{Camera, CameraController, CameraUniform};
use super::instance::{Instance, InstanceRaw};
2022-10-03 22:31:01 +03:00
use super::light::{DrawLight, LightUniform};
2022-10-02 22:03:50 +03:00
use super::model::{DrawModel, Model, ModelVertex, Vertex};
2023-01-29 14:54:23 +02:00
use super::pass::RenderPass;
2022-10-02 22:03:50 +03:00
use super::resources;
2022-10-02 18:59:20 +03:00
use super::texture::Texture;
2022-10-02 22:03:50 +03:00
2023-11-05 15:51:40 +02:00
const SHADOW_MAP_SIZE: u32 = 2048;
2023-04-16 14:49:05 +03:00
const SHADOW_MAP_LAYERS: u32 = 6;
2023-11-05 15:38:26 +02:00
#[repr(C)]
#[derive(Debug, Default, Copy, Clone, bytemuck::Pod, bytemuck::Zeroable)]
pub struct GlobalUniforms {
pub light_matrix_index: u32,
// No DownlevelFlags::BUFFER_BINDINGS_NOT_16_BYTE_ALIGNED in WebGL
pub use_shadowmaps: u32,
_padding: [u32; 2],
}
2022-10-01 23:58:09 +03:00
pub struct State {
pub size: winit::dpi::PhysicalSize<u32>,
surface: wgpu::Surface,
device: wgpu::Device,
queue: wgpu::Queue,
config: wgpu::SurfaceConfiguration,
2023-01-29 14:54:23 +02:00
geometry_pass: RenderPass,
2023-11-08 13:47:45 +02:00
fog_pass: RenderPass,
2022-10-02 18:59:20 +03:00
camera: Camera,
camera_uniform: CameraUniform,
camera_buffer: wgpu::Buffer,
camera_bind_group: wgpu::BindGroup,
camera_controller: CameraController,
2023-11-08 13:47:45 +02:00
geom_instances: Vec<Instance>,
geom_instance_buffer: wgpu::Buffer,
fog_instances: Vec<Instance>,
fog_instance_buffer: wgpu::Buffer,
2022-10-02 22:03:50 +03:00
depth_texture: Texture,
2023-11-08 13:47:45 +02:00
geom_model: Model,
fog_model: Model,
light_model: Model,
2022-10-03 22:31:01 +03:00
light_uniform: LightUniform,
light_buffer: wgpu::Buffer,
2023-01-29 14:54:23 +02:00
light_debug_pass: RenderPass,
2022-10-03 22:31:01 +03:00
light_bind_group: wgpu::BindGroup,
2023-04-15 14:32:06 +03:00
light_depth_bind_group: wgpu::BindGroup,
2023-01-29 18:57:29 +02:00
light_depth_pass: RenderPass,
2023-04-16 14:49:05 +03:00
light_depth_texture_target_views: [TextureView; SHADOW_MAP_LAYERS as usize],
2023-11-05 02:27:38 +02:00
light_matrix_uniform: GlobalUniforms,
light_matrix_buffer: wgpu::Buffer,
2022-10-01 23:58:09 +03:00
}
impl State {
// Creating some of the wgpu types requires async code
pub async fn new(window: &Window) -> Self {
2023-11-05 00:16:34 +02:00
log::info!("Creating surface");
let mut size = window.inner_size();
size.width = size.width.max(1);
size.height = size.height.max(1);
2023-11-05 02:27:38 +02:00
let instance = wgpu::Instance::new(InstanceDescriptor { backends: Backends::PRIMARY | Backends::GL, ..Default::default() });
2023-11-04 02:40:00 +02:00
let surface = unsafe { instance.create_surface(window) }.unwrap();
2022-10-01 23:58:09 +03:00
let adapter = instance
.request_adapter(&wgpu::RequestAdapterOptions {
power_preference: wgpu::PowerPreference::HighPerformance,
compatible_surface: Some(&surface),
force_fallback_adapter: false,
})
.await
2023-11-04 02:40:00 +02:00
.expect("failed to get adapter");
2022-10-01 23:58:09 +03:00
let (device, queue) = adapter
.request_device(
&wgpu::DeviceDescriptor {
features: wgpu::Features::default(),
2023-01-28 12:23:43 +02:00
limits: if cfg!(target_arch = "wasm32") {
2023-11-05 02:27:38 +02:00
// TODO: remove once webgpu?
2023-01-28 12:23:43 +02:00
wgpu::Limits::downlevel_webgl2_defaults()
} else {
wgpu::Limits::default()
},
2022-10-01 23:58:09 +03:00
label: None,
},
2023-11-05 02:27:38 +02:00
None,
2022-10-01 23:58:09 +03:00
)
.await
2023-11-04 02:40:00 +02:00
.expect("failed to get device");
2022-10-01 23:58:09 +03:00
2023-04-13 20:30:28 +03:00
let caps = surface.get_capabilities(&adapter);
2022-10-01 23:58:09 +03:00
let config = wgpu::SurfaceConfiguration {
usage: wgpu::TextureUsages::RENDER_ATTACHMENT,
2023-04-13 20:30:28 +03:00
format: caps.formats[0],
2022-10-01 23:58:09 +03:00
width: size.width,
height: size.height,
present_mode: wgpu::PresentMode::Fifo,
2023-01-23 17:15:02 +02:00
alpha_mode: wgpu::CompositeAlphaMode::Opaque,
2023-04-13 20:30:28 +03:00
view_formats: vec![caps.formats[0]],
2022-10-01 23:58:09 +03:00
};
surface.configure(&device, &config);
2022-10-02 18:59:20 +03:00
// Camera
let camera = Camera::new(
2023-01-28 13:23:41 +02:00
(-500.0, 150.0, 0.0).into(),
2022-10-02 18:59:20 +03:00
0.0,
0.0,
2023-01-24 17:00:51 +02:00
55.0,
2022-10-02 18:59:20 +03:00
config.width as f32 / config.height as f32,
2022-10-01 23:58:09 +03:00
);
2022-10-02 18:59:20 +03:00
let mut camera_uniform = CameraUniform::new();
2022-10-03 22:31:01 +03:00
camera_uniform.update(&camera);
2022-10-02 18:59:20 +03:00
let camera_buffer = device.create_buffer_init(&wgpu::util::BufferInitDescriptor {
label: Some("Camera Buffer"),
contents: bytemuck::cast_slice(&[camera_uniform]),
usage: wgpu::BufferUsages::UNIFORM | wgpu::BufferUsages::COPY_DST,
});
2023-11-05 00:16:34 +02:00
let camera_uniform_size = mem::size_of::<CameraUniform>() as u64;
2022-10-02 18:59:20 +03:00
let camera_bind_group_layout =
device.create_bind_group_layout(&wgpu::BindGroupLayoutDescriptor {
entries: &[wgpu::BindGroupLayoutEntry {
binding: 0,
2022-10-03 22:31:01 +03:00
visibility: wgpu::ShaderStages::VERTEX | wgpu::ShaderStages::FRAGMENT,
2022-10-02 18:59:20 +03:00
ty: wgpu::BindingType::Buffer {
ty: wgpu::BufferBindingType::Uniform,
has_dynamic_offset: false,
2023-11-05 00:16:34 +02:00
min_binding_size: wgpu::BufferSize::new(camera_uniform_size),
2022-10-02 18:59:20 +03:00
},
count: None,
}],
label: Some("camera_bind_group_layout"),
});
let camera_bind_group = device.create_bind_group(&wgpu::BindGroupDescriptor {
layout: &camera_bind_group_layout,
entries: &[wgpu::BindGroupEntry {
binding: 0,
resource: camera_buffer.as_entire_binding(),
}],
label: Some("camera_bind_group"),
2022-10-01 23:58:09 +03:00
});
2022-10-02 18:59:20 +03:00
2023-01-28 13:23:41 +02:00
let camera_controller = CameraController::new(400.0, 2.0);
2022-10-02 18:59:20 +03:00
2023-01-29 18:57:29 +02:00
let depth_texture = Texture::create_depth_texture(
&device,
"depth_texture",
2023-04-15 23:20:26 +03:00
Some(wgpu::CompareFunction::Less),
2023-04-16 14:49:05 +03:00
config.width,
config.height,
2023-01-30 00:40:50 +02:00
1,
wgpu::TextureUsages::RENDER_ATTACHMENT,
2023-01-29 18:57:29 +02:00
);
let light_depth_texture = Texture::create_depth_texture(
&device,
"light_depth_texture",
2023-04-15 23:05:53 +03:00
Some(wgpu::CompareFunction::LessEqual),
2023-04-16 14:49:05 +03:00
SHADOW_MAP_SIZE,
SHADOW_MAP_SIZE,
SHADOW_MAP_LAYERS,
wgpu::TextureUsages::RENDER_ATTACHMENT | wgpu::TextureUsages::TEXTURE_BINDING,
);
2023-04-16 14:49:05 +03:00
let light_depth_texture_target_views = (0..SHADOW_MAP_LAYERS)
2023-01-30 00:40:50 +02:00
.map(|i| {
light_depth_texture.texture.create_view(&TextureViewDescriptor {
label: Some("light_depth_texture_view"),
format: None,
dimension: Some(wgpu::TextureViewDimension::D2),
aspect: wgpu::TextureAspect::All,
base_mip_level: 0,
mip_level_count: None,
2023-11-04 15:22:40 +02:00
base_array_layer: i,
2023-11-04 02:40:00 +02:00
array_layer_count: Some(1),
})
2023-01-30 00:40:50 +02:00
})
.collect::<Vec<_>>()
.try_into()
.expect("failed to create light depth texture views");
2023-11-04 21:20:18 +02:00
let light_uniform = LightUniform::new([0.0, 0.0, 0.0], [1.0, 1.0, 1.0, 250000.0]);
2022-10-03 22:31:01 +03:00
// We'll want to update our lights position, so we use COPY_DST
let light_buffer = device.create_buffer_init(&wgpu::util::BufferInitDescriptor {
label: Some("Light UB"),
2022-10-03 22:31:01 +03:00
contents: bytemuck::cast_slice(&[light_uniform]),
usage: wgpu::BufferUsages::UNIFORM | wgpu::BufferUsages::COPY_DST,
});
2023-11-05 02:27:38 +02:00
let light_matrix_uniform = GlobalUniforms::default();
let light_matrix_buffer = device.create_buffer_init(&wgpu::util::BufferInitDescriptor {
label: Some("Light Matrix UB"),
contents: bytemuck::cast_slice(&[light_matrix_uniform]),
usage: wgpu::BufferUsages::UNIFORM | wgpu::BufferUsages::COPY_DST,
});
let light_uniform_size = mem::size_of::<LightUniform>() as u64;
2023-11-05 02:27:38 +02:00
let light_matrix_uniform_size = mem::size_of::<GlobalUniforms>() as u64;
2022-10-03 22:31:01 +03:00
let light_bind_group_layout =
device.create_bind_group_layout(&wgpu::BindGroupLayoutDescriptor {
2023-01-29 18:57:29 +02:00
entries: &[
2023-01-30 00:40:50 +02:00
// LightUniform
2023-01-29 18:57:29 +02:00
wgpu::BindGroupLayoutEntry {
binding: 0,
visibility: wgpu::ShaderStages::VERTEX | wgpu::ShaderStages::FRAGMENT,
ty: wgpu::BindingType::Buffer {
ty: wgpu::BufferBindingType::Uniform,
has_dynamic_offset: false,
min_binding_size: wgpu::BufferSize::new(light_uniform_size),
2023-01-29 18:57:29 +02:00
},
count: None,
2022-10-03 22:31:01 +03:00
},
2023-11-05 02:27:38 +02:00
// matrix index, use shadowmaps
2023-01-29 18:57:29 +02:00
wgpu::BindGroupLayoutEntry {
binding: 1,
2023-11-05 02:27:38 +02:00
// TODO: remove fragment vis once no shadowmap uniform
visibility: wgpu::ShaderStages::VERTEX | wgpu::ShaderStages::FRAGMENT,
ty: wgpu::BindingType::Buffer {
ty: wgpu::BufferBindingType::Uniform,
has_dynamic_offset: false,
min_binding_size: wgpu::BufferSize::new(light_matrix_uniform_size),
},
count: None,
},
2023-04-15 14:32:06 +03:00
],
label: Some("Light Bind Group Layout"),
});
let light_bind_group = device.create_bind_group(&wgpu::BindGroupDescriptor {
layout: &light_bind_group_layout,
entries: &[
// light struct
wgpu::BindGroupEntry {
binding: 0,
resource: light_buffer.as_entire_binding(),
},
// matrix index
wgpu::BindGroupEntry {
binding: 1,
resource: light_matrix_buffer.as_entire_binding(),
},
],
label: Some("Light Bind Group"),
});
let light_depth_bind_group_layout =
device.create_bind_group_layout(&wgpu::BindGroupLayoutDescriptor {
entries: &[
// depth textures
wgpu::BindGroupLayoutEntry {
2023-04-15 14:32:06 +03:00
binding: 0,
2023-01-29 18:57:29 +02:00
visibility: wgpu::ShaderStages::FRAGMENT,
ty: wgpu::BindingType::Texture {
multisampled: false,
view_dimension: wgpu::TextureViewDimension::D2Array,
2023-01-29 18:57:29 +02:00
sample_type: wgpu::TextureSampleType::Depth,
},
count: None,
2023-01-29 18:57:29 +02:00
},
wgpu::BindGroupLayoutEntry {
2023-04-15 14:32:06 +03:00
binding: 1,
2023-01-29 18:57:29 +02:00
visibility: wgpu::ShaderStages::FRAGMENT,
ty: wgpu::BindingType::Sampler(wgpu::SamplerBindingType::Comparison),
count: None,
2023-01-29 18:57:29 +02:00
},
],
label: Some("Light Depth Bind Group Layout"),
2022-10-03 22:31:01 +03:00
});
2023-04-15 14:32:06 +03:00
let light_depth_bind_group = device.create_bind_group(&wgpu::BindGroupDescriptor {
layout: &light_depth_bind_group_layout,
2023-01-29 18:57:29 +02:00
entries: &[
// depth textures
2023-01-29 18:57:29 +02:00
wgpu::BindGroupEntry {
2023-04-15 14:32:06 +03:00
binding: 0,
resource: wgpu::BindingResource::TextureView(&light_depth_texture.view),
},
wgpu::BindGroupEntry {
2023-04-15 14:32:06 +03:00
binding: 1,
resource: wgpu::BindingResource::Sampler(&light_depth_texture.sampler),
2023-01-29 18:57:29 +02:00
},
],
2023-04-15 14:32:06 +03:00
label: Some("Light Depth Bind Group"),
2022-10-03 22:31:01 +03:00
});
2022-10-02 18:59:20 +03:00
surface.configure(&device, &config);
2022-10-01 23:58:09 +03:00
let texture_bind_group_layout =
device.create_bind_group_layout(&wgpu::BindGroupLayoutDescriptor {
entries: &[
2022-10-04 01:30:50 +03:00
// diffuse
2022-10-01 23:58:09 +03:00
wgpu::BindGroupLayoutEntry {
binding: 0,
visibility: wgpu::ShaderStages::FRAGMENT,
ty: wgpu::BindingType::Texture {
multisampled: false,
view_dimension: wgpu::TextureViewDimension::D2,
sample_type: wgpu::TextureSampleType::Float { filterable: true },
},
count: None,
},
wgpu::BindGroupLayoutEntry {
binding: 1,
visibility: wgpu::ShaderStages::FRAGMENT,
2022-10-04 01:30:50 +03:00
ty: wgpu::BindingType::Sampler(wgpu::SamplerBindingType::Filtering),
count: None,
},
// normal
wgpu::BindGroupLayoutEntry {
binding: 2,
visibility: wgpu::ShaderStages::FRAGMENT,
ty: wgpu::BindingType::Texture {
multisampled: false,
sample_type: wgpu::TextureSampleType::Float { filterable: true },
view_dimension: wgpu::TextureViewDimension::D2,
},
count: None,
},
wgpu::BindGroupLayoutEntry {
binding: 3,
visibility: wgpu::ShaderStages::FRAGMENT,
2022-10-01 23:58:09 +03:00
ty: wgpu::BindingType::Sampler(wgpu::SamplerBindingType::Filtering),
count: None,
},
2023-01-24 03:59:06 +02:00
// metallic + roughness
wgpu::BindGroupLayoutEntry {
binding: 4,
visibility: wgpu::ShaderStages::FRAGMENT,
ty: wgpu::BindingType::Texture {
multisampled: false,
sample_type: wgpu::TextureSampleType::Float { filterable: true },
view_dimension: wgpu::TextureViewDimension::D2,
},
count: None,
},
wgpu::BindGroupLayoutEntry {
binding: 5,
visibility: wgpu::ShaderStages::FRAGMENT,
ty: wgpu::BindingType::Sampler(wgpu::SamplerBindingType::Filtering),
count: None,
},
2022-10-01 23:58:09 +03:00
],
label: Some("texture_bind_group_layout"),
});
2023-11-08 13:47:45 +02:00
let geom_model = resources::load_model_gltf(
2023-01-27 21:45:08 +02:00
"models/Sponza.glb",
2023-01-23 17:15:02 +02:00
&device,
&queue,
&texture_bind_group_layout,
)
2023-11-04 02:40:00 +02:00
.await
.unwrap();
2022-10-02 22:03:50 +03:00
2023-11-08 13:47:45 +02:00
let fog_model = resources::load_model_gltf(
"models/Cube.glb",
&device,
&queue,
&texture_bind_group_layout,
)
.await
.unwrap();
let light_model = resources::load_model_gltf(
"models/Cube.glb",
&device,
&queue,
&texture_bind_group_layout,
)
2023-11-04 02:40:00 +02:00
.await
.unwrap();
2023-11-08 13:47:45 +02:00
let geom_instances = vec![Instance {
// this sponza model isn't quite centered
position: [60.0, 0.0, 35.0].into(),
2023-01-27 02:40:40 +02:00
rotation: cgmath::Quaternion::one(),
2023-11-08 13:47:45 +02:00
scale: [1.0, 1.0, 1.0].into(),
2023-01-27 02:40:40 +02:00
}];
2023-11-08 13:47:45 +02:00
let geom_instance_data = geom_instances.iter().map(Instance::to_raw).collect::<Vec<_>>();
let geom_instance_buffer = device.create_buffer_init(&wgpu::util::BufferInitDescriptor {
label: Some("Geometry Instance Buffer"),
contents: bytemuck::cast_slice(&geom_instance_data),
usage: wgpu::BufferUsages::VERTEX,
});
2022-10-02 22:03:50 +03:00
2023-11-08 13:47:45 +02:00
let fog_instances = vec![Instance {
position: [0.0, 20.0, 0.0].into(),
rotation: cgmath::Quaternion::one(),
scale: [1360.0, 20.0, 600.0].into(),
}];
let fog_instance_data = fog_instances.iter().map(Instance::to_raw).collect::<Vec<_>>();
let fog_instance_buffer = device.create_buffer_init(&wgpu::util::BufferInitDescriptor {
label: Some("Fog Instance Buffer"),
contents: bytemuck::cast_slice(&fog_instance_data),
2022-10-02 18:59:20 +03:00
usage: wgpu::BufferUsages::VERTEX,
});
2023-04-15 14:32:06 +03:00
let light_depth_pass = RenderPass::new(
&device,
&[
&camera_bind_group_layout,
&light_bind_group_layout,
],
&[],
"depth.wgsl",
None,
Some(Texture::DEPTH_FORMAT),
&[ModelVertex::desc(), InstanceRaw::desc()],
"light depth pass",
2023-04-15 23:20:26 +03:00
true,
2023-11-08 13:47:45 +02:00
false,
2023-04-15 14:32:06 +03:00
);
2023-01-29 14:54:23 +02:00
let geometry_pass = RenderPass::new(
&device,
&[
&camera_bind_group_layout,
&light_bind_group_layout,
2023-04-15 14:32:06 +03:00
&light_depth_bind_group_layout,
2023-01-29 18:57:29 +02:00
&texture_bind_group_layout,
2023-01-29 14:54:23 +02:00
],
&[],
"pbr.wgsl",
2023-01-29 18:57:29 +02:00
Some(config.format),
2023-01-29 14:54:23 +02:00
Some(Texture::DEPTH_FORMAT),
&[ModelVertex::desc(), InstanceRaw::desc()],
2023-01-29 18:57:29 +02:00
"geometry pass",
2023-04-15 23:20:26 +03:00
false,
2023-11-08 13:47:45 +02:00
false,
);
let fog_pass = RenderPass::new(
&device,
&[
&camera_bind_group_layout,
&light_bind_group_layout,
&light_depth_bind_group_layout,
&texture_bind_group_layout,
],
&[],
"fog.wgsl",
Some(config.format),
Some(Texture::DEPTH_FORMAT),
&[ModelVertex::desc(), InstanceRaw::desc()],
"fog pass",
false,
true,
2023-01-29 14:54:23 +02:00
);
2022-10-01 23:58:09 +03:00
2023-01-29 14:54:23 +02:00
let light_debug_pass = RenderPass::new(
&device,
&[&camera_bind_group_layout, &light_bind_group_layout],
&[],
"light.wgsl",
2023-01-29 18:57:29 +02:00
Some(config.format),
2023-01-29 14:54:23 +02:00
Some(Texture::DEPTH_FORMAT),
&[ModelVertex::desc()],
2023-01-29 18:57:29 +02:00
"light debug pass",
2023-04-15 23:20:26 +03:00
false,
2023-11-08 13:47:45 +02:00
false,
2023-01-29 18:57:29 +02:00
);
2023-01-28 14:11:54 +02:00
Self {
2022-10-01 23:58:09 +03:00
size,
surface,
device,
queue,
config,
2023-01-29 14:54:23 +02:00
geometry_pass,
2023-11-08 13:47:45 +02:00
fog_pass,
2022-10-02 18:59:20 +03:00
camera,
camera_uniform,
camera_buffer,
camera_bind_group,
camera_controller,
2023-11-08 13:47:45 +02:00
geom_instances,
geom_instance_buffer,
fog_instances,
fog_instance_buffer,
2022-10-02 22:03:50 +03:00
depth_texture,
2023-11-08 13:47:45 +02:00
geom_model,
fog_model,
light_model,
2022-10-03 22:31:01 +03:00
light_uniform,
light_buffer,
2023-01-29 14:54:23 +02:00
light_debug_pass,
2022-10-03 22:31:01 +03:00
light_bind_group,
2023-04-16 14:49:05 +03:00
light_depth_bind_group,
2023-01-29 18:57:29 +02:00
light_depth_pass,
light_depth_texture_target_views,
light_matrix_uniform,
light_matrix_buffer,
2023-01-28 14:11:54 +02:00
}
2022-10-01 23:58:09 +03:00
}
pub fn resize(&mut self, new_size: winit::dpi::PhysicalSize<u32>) {
if new_size.width > 0 && new_size.height > 0 {
self.size = new_size;
self.config.width = new_size.width;
self.config.height = new_size.height;
self.surface.configure(&self.device, &self.config);
2022-10-02 18:59:20 +03:00
self.camera
.projection
.resize(new_size.width, new_size.height);
2023-01-29 18:57:29 +02:00
self.depth_texture = Texture::create_depth_texture(
&self.device,
"depth_texture",
2023-04-15 23:20:26 +03:00
Some(wgpu::CompareFunction::Less),
2023-04-16 14:49:05 +03:00
self.config.width,
self.config.height,
2023-01-30 00:40:50 +02:00
1,
wgpu::TextureUsages::RENDER_ATTACHMENT,
2023-01-29 18:57:29 +02:00
);
2022-10-01 23:58:09 +03:00
}
}
2022-10-02 18:59:20 +03:00
pub fn input(
&mut self,
window_event: Option<&WindowEvent>,
device_event: Option<&DeviceEvent>,
) -> bool {
2023-01-29 14:54:23 +02:00
self.camera_controller
2023-01-28 14:11:54 +02:00
.process_events(window_event, device_event)
2022-10-01 23:58:09 +03:00
}
2023-11-04 21:20:18 +02:00
pub fn update(&mut self, dt: Duration, time: Duration) {
2022-10-03 22:31:01 +03:00
// Update camera
2022-10-02 18:59:20 +03:00
self.camera.update(dt, &self.camera_controller);
self.camera_controller.reset(false);
2022-10-03 22:31:01 +03:00
self.camera_uniform.update(&self.camera);
2022-10-02 18:59:20 +03:00
self.queue.write_buffer(
&self.camera_buffer,
0,
bytemuck::cast_slice(&[self.camera_uniform]),
);
2022-10-03 22:31:01 +03:00
// Update the light
2023-11-04 21:20:18 +02:00
self.light_uniform.position[0] = f32::sin(time.as_secs_f32() * 0.5) * 500.0;
self.light_uniform.position[1] = 300.0 + f32::sin(time.as_secs_f32() * 0.3) * 150.0;
self.light_uniform.position[2] = f32::sin(time.as_secs_f32() * 0.8) * 100.0;
self.light_uniform.update_matrices();
2023-11-04 21:20:18 +02:00
self.light_uniform.color[0] = f32::abs(f32::sin(time.as_secs_f32() * 1.0));
self.light_uniform.color[1] = f32::abs(f32::sin(time.as_secs_f32() * 0.6));
self.light_uniform.color[2] = f32::abs(f32::sin(time.as_secs_f32() * 0.4));
2022-10-03 22:31:01 +03:00
self.queue.write_buffer(
&self.light_buffer,
0,
bytemuck::cast_slice(&[self.light_uniform]),
);
2022-10-02 18:59:20 +03:00
}
2022-10-01 23:58:09 +03:00
pub fn render(&mut self) -> Result<(), wgpu::SurfaceError> {
// render light to depth textures
2023-04-16 14:49:05 +03:00
for i in 0..SHADOW_MAP_LAYERS as usize {
2023-11-05 02:27:38 +02:00
self.light_matrix_uniform.light_matrix_index = i as u32;
self.light_matrix_uniform.use_shadowmaps = if cfg!(target_arch = "wasm32") { 0u32 } else { 1u32 };
2023-01-30 00:40:50 +02:00
self.queue.write_buffer(
&self.light_matrix_buffer,
2023-01-30 00:40:50 +02:00
0,
bytemuck::cast_slice(&[self.light_matrix_uniform]),
2023-01-30 00:40:50 +02:00
);
let mut depth_encoder = self
.device
.create_command_encoder(&wgpu::CommandEncoderDescriptor {
label: Some("Depth Encoder"),
});
{
let mut light_depth_render_pass =
2023-11-04 02:40:00 +02:00
depth_encoder.begin_render_pass(&wgpu::RenderPassDescriptor {
label: Some("Light Depth Render Pass"),
color_attachments: &[],
depth_stencil_attachment: Some(wgpu::RenderPassDepthStencilAttachment {
view: &self.light_depth_texture_target_views[i],
depth_ops: Some(wgpu::Operations {
load: wgpu::LoadOp::Clear(1.0),
store: StoreOp::Store,
}),
stencil_ops: None,
2023-01-29 18:57:29 +02:00
}),
2023-11-04 02:40:00 +02:00
timestamp_writes: None,
occlusion_query_set: None,
});
2023-01-29 18:57:29 +02:00
2023-11-08 13:47:45 +02:00
light_depth_render_pass.set_vertex_buffer(1, self.geom_instance_buffer.slice(..));
light_depth_render_pass.set_pipeline(&self.light_depth_pass.pipeline);
light_depth_render_pass.draw_model_instanced(
2023-11-08 13:47:45 +02:00
&self.geom_model,
0..self.geom_instances.len() as u32,
2023-04-15 14:32:06 +03:00
[&self.camera_bind_group, &self.light_bind_group].into(),
);
}
self.queue.submit(std::iter::once(depth_encoder.finish()));
2023-01-29 18:57:29 +02:00
}
// render geometry
let surface_texture = self.surface.get_current_texture()?;
let surface_view = surface_texture
.texture
.create_view(&wgpu::TextureViewDescriptor::default());
let mut encoder = self
.device
.create_command_encoder(&wgpu::CommandEncoderDescriptor {
label: Some("Render Encoder"),
});
2023-01-29 18:57:29 +02:00
2023-04-13 18:33:59 +03:00
encoder.push_debug_group("geometry pass");
2022-10-01 23:58:09 +03:00
{
2023-01-29 15:40:37 +02:00
let mut geom_render_pass = encoder.begin_render_pass(&wgpu::RenderPassDescriptor {
label: Some("Geometry Render Pass"),
2022-10-03 22:31:01 +03:00
color_attachments: &[Some(wgpu::RenderPassColorAttachment {
2023-01-29 15:40:37 +02:00
view: &surface_view,
2022-10-03 22:31:01 +03:00
resolve_target: None,
ops: wgpu::Operations {
load: wgpu::LoadOp::Clear(wgpu::Color {
r: 0.0,
g: 0.0,
b: 0.0,
a: 1.0,
}),
2023-11-04 02:40:00 +02:00
store: StoreOp::Store,
2022-10-03 22:31:01 +03:00
},
})],
2022-10-02 22:03:50 +03:00
depth_stencil_attachment: Some(wgpu::RenderPassDepthStencilAttachment {
view: &self.depth_texture.view,
depth_ops: Some(wgpu::Operations {
load: wgpu::LoadOp::Clear(1.0),
2023-11-04 02:40:00 +02:00
store: StoreOp::Store,
2022-10-02 22:03:50 +03:00
}),
stencil_ops: None,
}),
2023-11-04 02:40:00 +02:00
timestamp_writes: None,
occlusion_query_set: None,
2022-10-01 23:58:09 +03:00
});
2023-11-08 13:47:45 +02:00
geom_render_pass.set_vertex_buffer(1, self.geom_instance_buffer.slice(..));
2023-01-29 15:40:37 +02:00
geom_render_pass.set_pipeline(&self.geometry_pass.pipeline);
geom_render_pass.draw_model_instanced(
2023-11-08 13:47:45 +02:00
&self.geom_model,
0..self.geom_instances.len() as u32,
[&self.camera_bind_group, &self.light_bind_group, &self.light_depth_bind_group].into(),
);
}
encoder.pop_debug_group();
encoder.push_debug_group("fog pass");
{
let mut fog_render_pass = encoder.begin_render_pass(&wgpu::RenderPassDescriptor {
label: Some("Fog Render Pass"),
color_attachments: &[Some(wgpu::RenderPassColorAttachment {
view: &surface_view,
resolve_target: None,
ops: wgpu::Operations {
load: wgpu::LoadOp::Load,
store: StoreOp::Store,
},
})],
depth_stencil_attachment: Some(wgpu::RenderPassDepthStencilAttachment {
view: &self.depth_texture.view,
depth_ops: Some(wgpu::Operations {
load: wgpu::LoadOp::Load,
store: StoreOp::Store,
}),
stencil_ops: None,
}),
timestamp_writes: None,
occlusion_query_set: None,
});
fog_render_pass.set_vertex_buffer(1, self.fog_instance_buffer.slice(..));
fog_render_pass.set_pipeline(&self.fog_pass.pipeline);
fog_render_pass.draw_model_instanced(
&self.fog_model,
0..self.fog_instances.len() as u32,
2023-04-15 14:32:06 +03:00
[&self.camera_bind_group, &self.light_bind_group, &self.light_depth_bind_group].into(),
2022-10-03 22:31:01 +03:00
);
2023-01-29 15:40:37 +02:00
}
2023-04-13 18:33:59 +03:00
encoder.pop_debug_group();
2022-10-03 22:31:01 +03:00
2023-04-13 18:33:59 +03:00
encoder.push_debug_group("debug light pass");
2023-01-29 15:40:37 +02:00
{
let mut light_debug_render_pass =
encoder.begin_render_pass(&wgpu::RenderPassDescriptor {
label: Some("Light Debug Render Pass"),
color_attachments: &[Some(wgpu::RenderPassColorAttachment {
view: &surface_view,
resolve_target: None,
ops: wgpu::Operations {
load: wgpu::LoadOp::Load,
2023-11-04 02:40:00 +02:00
store: StoreOp::Store,
2023-01-29 15:40:37 +02:00
},
})],
depth_stencil_attachment: Some(wgpu::RenderPassDepthStencilAttachment {
view: &self.depth_texture.view,
depth_ops: Some(wgpu::Operations {
load: wgpu::LoadOp::Load,
2023-11-04 02:40:00 +02:00
store: StoreOp::Store,
2023-01-29 15:40:37 +02:00
}),
stencil_ops: None,
}),
2023-11-04 02:40:00 +02:00
timestamp_writes: None,
occlusion_query_set: None,
2023-01-29 15:40:37 +02:00
});
light_debug_render_pass.set_pipeline(&self.light_debug_pass.pipeline);
light_debug_render_pass.draw_light_model(
&self.light_model,
2022-10-02 22:03:50 +03:00
&self.camera_bind_group,
2022-10-03 22:31:01 +03:00
&self.light_bind_group,
2022-10-02 22:03:50 +03:00
);
2022-10-01 23:58:09 +03:00
}
2023-04-13 18:33:59 +03:00
encoder.pop_debug_group();
2022-10-01 23:58:09 +03:00
self.queue.submit(std::iter::once(encoder.finish()));
2023-01-29 15:40:37 +02:00
surface_texture.present();
2022-10-01 23:58:09 +03:00
2023-01-28 14:11:54 +02:00
Ok(())
2022-10-01 23:58:09 +03:00
}
}