use std::iter;
use cgmath::Point3;
use crate::types::geometry::{Line, Mesh};
use wgpu::{util::DeviceExt, Buffer};
use winit::{
dpi::{PhysicalPosition, PhysicalSize},
event::*,
keyboard::PhysicalKey,
window::Window,
};
use crate::viewer::util::{lines_to_buffer, mesh_to_buffers};
use super::{
material::Material,
orbit_camera::{OrbitCamera, OrbitCameraController, OrbitCameraUniform},
texture::{self, Texture},
vertex::Vertex,
};
pub struct State<'a> {
surface: wgpu::Surface<'a>,
device: wgpu::Device,
queue: wgpu::Queue,
config: wgpu::SurfaceConfiguration,
size: winit::dpi::PhysicalSize<u32>,
render_pipeline: wgpu::RenderPipeline,
line_pipeline: wgpu::RenderPipeline,
vertex_buffers: Vec<Buffer>,
index_buffers: Vec<Buffer>,
num_indices: Vec<u32>,
line_vertex_buffers: Vec<Buffer>,
num_lines: Vec<u32>,
camera: OrbitCamera,
pub camera_controller: OrbitCameraController,
camera_uniform: OrbitCameraUniform,
camera_buffer: wgpu::Buffer,
camera_bind_group: wgpu::BindGroup,
depth_texture: Texture,
window: &'a Window,
pub mouse_pressed: bool,
pub last_mouse_pos: PhysicalPosition<f64>,
}
impl<'a> State<'a> {
pub async fn new(window: &'a Window, mesh: &Mesh<f32>, material: &Material) -> Self {
let size = window.inner_size();
let dim = mesh.bounds().dimensions();
let centroid = mesh.bounds().centroid();
let instance = wgpu::Instance::new(wgpu::InstanceDescriptor {
backends: wgpu::Backends::all(),
..Default::default()
});
let surface = instance.create_surface(window).unwrap();
let adapter = instance
.request_adapter(&wgpu::RequestAdapterOptions {
power_preference: wgpu::PowerPreference::default(),
compatible_surface: Some(&surface),
force_fallback_adapter: false,
})
.await
.unwrap();
let (device, queue) = adapter
.request_device(
&wgpu::DeviceDescriptor {
label: None,
required_features: wgpu::Features::empty(),
required_limits: if cfg!(target_arch = "wasm32") {
wgpu::Limits::downlevel_defaults()
} else {
wgpu::Limits::default()
},
memory_hints: wgpu::MemoryHints::Performance,
},
None, )
.await
.unwrap();
let surface_caps = surface.get_capabilities(&adapter);
let surface_format = surface_caps
.formats
.iter()
.copied()
.find(|f| f.is_srgb())
.unwrap_or(surface_caps.formats[0]);
let config = wgpu::SurfaceConfiguration {
usage: wgpu::TextureUsages::RENDER_ATTACHMENT,
format: surface_format,
width: size.width,
height: size.height,
present_mode: surface_caps.present_modes[0],
alpha_mode: surface_caps.alpha_modes[0],
view_formats: vec![],
desired_maximum_frame_latency: Default::default(),
};
surface.configure(&device, &config);
let default_position: Point3<f32> =
(centroid.x, centroid.z, centroid.y - 5. * dim.1).into();
let default_target: Point3<f32> = (centroid.x, centroid.z, centroid.y).into();
let camera = OrbitCamera {
eye: default_position,
target: default_target,
up: cgmath::Vector3::unit_y(),
aspect: config.width as f32 / config.height as f32,
fovy: 15.0,
znear: 0.1,
zfar: 1000.0,
};
let camera_controller = OrbitCameraController::new(default_position, default_target);
let mut camera_uniform = OrbitCameraUniform::new(default_position);
camera_uniform.update_view_proj(&camera);
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,
});
let camera_bind_group_layout =
device.create_bind_group_layout(&wgpu::BindGroupLayoutDescriptor {
entries: &[wgpu::BindGroupLayoutEntry {
binding: 0,
visibility: wgpu::ShaderStages::VERTEX,
ty: wgpu::BindingType::Buffer {
ty: wgpu::BufferBindingType::Uniform,
has_dynamic_offset: false,
min_binding_size: None,
},
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"),
});
let depth_texture =
texture::Texture::create_depth_texture(&device, &config, "depth_texture");
let shader = device.create_shader_module(wgpu::ShaderModuleDescriptor {
label: Some("Shader"),
source: wgpu::ShaderSource::Wgsl(material.load_shader_source().into()),
});
let line_material = Material::Line;
let line_shader = device.create_shader_module(wgpu::ShaderModuleDescriptor {
label: Some("Line Shader"),
source: wgpu::ShaderSource::Wgsl(line_material.load_shader_source().into()),
});
let render_pipeline_layout =
device.create_pipeline_layout(&wgpu::PipelineLayoutDescriptor {
label: Some("Render Pipeline Layout"),
bind_group_layouts: &[&camera_bind_group_layout],
push_constant_ranges: &[],
});
let render_pipeline = device.create_render_pipeline(&wgpu::RenderPipelineDescriptor {
label: Some("Render Pipeline"),
layout: Some(&render_pipeline_layout),
vertex: wgpu::VertexState {
module: &shader,
entry_point: "vs_main",
buffers: &[Vertex::desc()],
compilation_options: Default::default(),
},
fragment: Some(wgpu::FragmentState {
module: &shader,
entry_point: "fs_main",
targets: &[Some(wgpu::ColorTargetState {
format: config.format,
blend: Some(wgpu::BlendState {
color: wgpu::BlendComponent::REPLACE,
alpha: wgpu::BlendComponent::REPLACE,
}),
write_mask: wgpu::ColorWrites::ALL,
})],
compilation_options: Default::default(),
}),
primitive: wgpu::PrimitiveState {
topology: wgpu::PrimitiveTopology::TriangleList,
strip_index_format: None,
front_face: wgpu::FrontFace::Cw,
cull_mode: None,
polygon_mode: wgpu::PolygonMode::Fill,
unclipped_depth: false,
conservative: false,
},
depth_stencil: Some(wgpu::DepthStencilState {
format: texture::Texture::DEPTH_FORMAT,
depth_write_enabled: true,
depth_compare: wgpu::CompareFunction::LessEqual,
stencil: wgpu::StencilState::default(),
bias: wgpu::DepthBiasState {
constant: 1,
slope_scale: 1.0,
clamp: 0.0,
},
}),
multisample: wgpu::MultisampleState {
count: 1,
mask: !0,
alpha_to_coverage_enabled: false,
},
multiview: None,
cache: Default::default(),
});
let line_pipeline = device.create_render_pipeline(&wgpu::RenderPipelineDescriptor {
label: Some("Line Pipeline"),
layout: Some(&render_pipeline_layout),
vertex: wgpu::VertexState {
module: &line_shader,
entry_point: "vs_main",
buffers: &[Vertex::desc()],
compilation_options: Default::default(),
},
fragment: Some(wgpu::FragmentState {
module: &line_shader,
entry_point: "fs_main",
targets: &[Some(wgpu::ColorTargetState {
format: wgpu::TextureFormat::Bgra8UnormSrgb,
blend: Some(wgpu::BlendState::REPLACE),
write_mask: wgpu::ColorWrites::ALL,
})],
compilation_options: Default::default(),
}),
primitive: wgpu::PrimitiveState {
topology: wgpu::PrimitiveTopology::LineList,
strip_index_format: None,
front_face: wgpu::FrontFace::Cw,
cull_mode: None,
polygon_mode: wgpu::PolygonMode::Fill,
unclipped_depth: false,
conservative: false,
},
depth_stencil: Some(wgpu::DepthStencilState {
format: texture::Texture::DEPTH_FORMAT,
depth_write_enabled: true,
depth_compare: wgpu::CompareFunction::LessEqual,
stencil: wgpu::StencilState::default(),
bias: wgpu::DepthBiasState {
constant: -1,
slope_scale: 1.0,
clamp: 0.0,
},
}),
multisample: wgpu::MultisampleState::default(),
multiview: None,
cache: Default::default(),
});
Self {
surface,
device,
queue,
config,
size,
render_pipeline,
line_pipeline,
vertex_buffers: Vec::new(),
index_buffers: Vec::new(),
num_indices: Vec::new(),
line_vertex_buffers: Vec::new(),
num_lines: Vec::new(),
camera,
camera_controller,
camera_buffer,
camera_bind_group,
camera_uniform,
depth_texture,
window,
mouse_pressed: false,
last_mouse_pos: PhysicalPosition::new(0., 0.),
}
}
pub fn write_mesh_buffers(&mut self, meshes: &[&Mesh<f32>]) {
let buffers: Vec<(Buffer, Buffer, usize)> = meshes
.iter()
.map(|mesh| {
let (vertices, indices) = mesh_to_buffers(mesh);
let vertex_buffer =
self.device
.create_buffer_init(&wgpu::util::BufferInitDescriptor {
label: Some("Vertex Buffer"),
contents: bytemuck::cast_slice(&vertices),
usage: wgpu::BufferUsages::VERTEX,
});
let index_buffer =
self.device
.create_buffer_init(&wgpu::util::BufferInitDescriptor {
label: Some("Index Buffer"),
contents: bytemuck::cast_slice(&indices),
usage: wgpu::BufferUsages::INDEX,
});
(vertex_buffer, index_buffer, indices.len())
})
.collect();
for (vertex_buffer, index_buffer, n) in buffers {
self.vertex_buffers.push(vertex_buffer);
self.index_buffers.push(index_buffer);
self.num_indices.push(n as u32);
}
}
pub fn size(&self) -> PhysicalSize<u32> {
self.size
}
pub fn write_line_buffers(&mut self, lines: &[Line<f32>]) {
let line_buffers = lines_to_buffer(lines);
for line_buffer in line_buffers {
let vertex_buffer = self
.device
.create_buffer_init(&wgpu::util::BufferInitDescriptor {
label: Some("Line Vertex Buffer"),
contents: bytemuck::cast_slice(&line_buffer),
usage: wgpu::BufferUsages::VERTEX,
});
self.line_vertex_buffers.push(vertex_buffer);
self.num_lines.push(line_buffer.len() as u32);
}
}
pub fn window(&self) -> &Window {
&self.window
}
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);
self.depth_texture =
texture::Texture::create_depth_texture(&self.device, &self.config, "depth_texture");
self.camera.aspect = self.config.width as f32 / self.config.height as f32;
}
}
pub fn input(&mut self, event: &WindowEvent) -> bool {
match event {
WindowEvent::KeyboardInput {
event:
KeyEvent {
physical_key: PhysicalKey::Code(key),
state,
..
},
..
} => self.camera_controller.process_keyboard(*key, *state),
WindowEvent::MouseWheel { delta, .. } => {
self.camera_controller.process_scroll(delta);
true
}
WindowEvent::MouseInput {
button: MouseButton::Left,
state,
..
} => {
self.camera_controller.is_orbit = state.is_pressed();
self.mouse_pressed = state.is_pressed();
if !self.mouse_pressed {
self.camera_controller.orbit_horizontal = 0.0;
self.camera_controller.orbit_vertical = 0.0;
}
true
}
WindowEvent::CursorMoved { position, .. } => {
if self.mouse_pressed {
let delta_x = position.x - self.last_mouse_pos.x;
let delta_y = position.y - self.last_mouse_pos.y;
self.camera_controller.process_mouse(delta_x, delta_y, true);
}
self.last_mouse_pos = *position;
true
}
_ => false,
}
}
pub fn update(&mut self) {
self.camera_controller.update_camera(&mut self.camera);
self.camera_uniform.update_view_proj(&self.camera);
self.queue.write_buffer(
&self.camera_buffer,
0,
bytemuck::cast_slice(&[self.camera_uniform]),
);
}
pub fn render(&mut self) -> Result<(), wgpu::SurfaceError> {
let output = self.surface.get_current_texture()?;
let view = output
.texture
.create_view(&wgpu::TextureViewDescriptor::default());
let mut encoder = self
.device
.create_command_encoder(&wgpu::CommandEncoderDescriptor {
label: Some("Render Encoder"),
});
{
let mut render_pass = encoder.begin_render_pass(&wgpu::RenderPassDescriptor {
label: Some("Render Pass"),
color_attachments: &[Some(wgpu::RenderPassColorAttachment {
view: &view,
resolve_target: None,
ops: wgpu::Operations {
load: wgpu::LoadOp::Clear(wgpu::Color {
r: 5.0 / 255.0,
g: 6.0 / 255.0,
b: 7.0 / 255.0,
a: 1.0,
}),
store: wgpu::StoreOp::Store,
},
})],
depth_stencil_attachment: Some(wgpu::RenderPassDepthStencilAttachment {
view: &self.depth_texture.view,
depth_ops: Some(wgpu::Operations {
load: wgpu::LoadOp::Clear(1.0),
store: wgpu::StoreOp::Store,
}),
stencil_ops: None,
}),
occlusion_query_set: None,
timestamp_writes: None,
});
for (index, vertex_buffer) in self.vertex_buffers.iter().enumerate() {
let index_buffer = &self.index_buffers[index];
let num_indices = self.num_indices[index];
render_pass.set_pipeline(&self.render_pipeline);
render_pass.set_bind_group(0, &self.camera_bind_group, &[]);
render_pass.set_vertex_buffer(0, vertex_buffer.slice(..));
render_pass.set_index_buffer(index_buffer.slice(..), wgpu::IndexFormat::Uint32);
render_pass.draw_indexed(0..num_indices, 0, 0..1);
}
for (index, line_vertex_buffer) in self.line_vertex_buffers.iter().enumerate() {
let num_indices = self.num_lines[index];
render_pass.set_pipeline(&self.line_pipeline);
render_pass.set_bind_group(0, &self.camera_bind_group, &[]);
render_pass.set_vertex_buffer(0, line_vertex_buffer.slice(..));
render_pass.draw(0..num_indices, 0..1);
}
}
self.queue.submit(iter::once(encoder.finish()));
output.present();
Ok(())
}
}