use glam::{EulerRot, Mat3, Mat4, Vec3, vec3}; use std::collections::{HashMap, HashSet}; use weareshared::{packets::Resource, tree::SceneTree}; use wgpu::{ BindGroup, BindGroupDescriptor, BindGroupLayoutDescriptor, BlendState, Color, ColorTargetState, ColorWrites, CommandEncoder, Device, FragmentState, FrontFace, IndexFormat, LoadOp, MultisampleState, Operations, PipelineCompilationOptions, PipelineLayoutDescriptor, PolygonMode, PrimitiveState, PrimitiveTopology, PushConstantRange, RenderPassColorAttachment, RenderPassDescriptor, RenderPipeline, RenderPipelineDescriptor, ShaderStages, StoreOp, TextureFormat, TextureView, VertexAttribute, VertexBufferLayout, VertexFormat, VertexState, VertexStepMode, include_wgsl, }; use crate::scene_prepare::RPrefab; pub struct ScenePipeline { pipeline: RenderPipeline, bind_group: BindGroup, } macro_rules! v_attr { ($($n:literal),*) => { [$(VertexBufferLayout { step_mode: VertexStepMode::Vertex, array_stride: 4, attributes: &[VertexAttribute { format: VertexFormat::Float32, offset: 0, shader_location: $n, }], }),*] }; } impl ScenePipeline { pub fn new(device: &Device, format: TextureFormat) -> Self { let module = device.create_shader_module(include_wgsl!("shader.wgsl")); let bind_group_layout = device.create_bind_group_layout(&BindGroupLayoutDescriptor { entries: &[], label: None, }); let bind_group = device.create_bind_group(&BindGroupDescriptor { label: None, layout: &bind_group_layout, entries: &[], }); let pipeline_layout = device.create_pipeline_layout(&PipelineLayoutDescriptor { label: None, bind_group_layouts: &[&bind_group_layout], push_constant_ranges: &[PushConstantRange { range: 0..(4 * 4 * size_of::() as u32), stages: ShaderStages::VERTEX, }], }); let pipeline = device.create_render_pipeline(&RenderPipelineDescriptor { label: None, layout: Some(&pipeline_layout), fragment: Some(FragmentState { module: &module, entry_point: Some("fs_main"), targets: &[Some(ColorTargetState { blend: Some(BlendState::PREMULTIPLIED_ALPHA_BLENDING), format, write_mask: ColorWrites::all(), })], compilation_options: PipelineCompilationOptions::default(), }), vertex: VertexState { module: &module, entry_point: Some("vs_main"), buffers: &v_attr!(0, 1, 2, 3, 4, 5, 6, 7), compilation_options: PipelineCompilationOptions::default(), }, primitive: PrimitiveState { topology: PrimitiveTopology::TriangleList, front_face: FrontFace::Ccw, cull_mode: None, //Some(Face::Back), polygon_mode: PolygonMode::Fill, ..Default::default() }, depth_stencil: Default::default(), multisample: MultisampleState::default(), multiview: None, cache: None, }); Self { bind_group, pipeline, } } pub fn draw( &mut self, commands: &mut CommandEncoder, target: &TextureView, scene: &SceneTree, prefabs: &HashMap, prefabs_needed: &mut HashSet, ) { let mut rpass = commands.begin_render_pass(&RenderPassDescriptor { label: None, color_attachments: &[Some(RenderPassColorAttachment { view: target, resolve_target: None, ops: Operations { store: StoreOp::Store, load: LoadOp::Clear(Color { r: 0.01, g: 0.01, b: 0.01, a: 1., }), }, })], ..Default::default() }); let camera = Mat4::perspective_infinite_reverse_rh(1., 1., 0.1) * Mat4::look_at_rh(vec3(-5., 0., 0.), vec3(0., 0., 0.), Vec3::Y); for ob in scene.objects.values() { let prefab_projection = camera * Mat4::from_mat3(Mat3::from_euler( EulerRot::YXZ, ob.rot.x, ob.rot.y, ob.rot.z, )) * Mat4::from_translation(ob.pos.into()); if let Some(prefab) = prefabs.get(&ob.res) { for (affine, part) in &prefab.0 { let part_projection = prefab_projection * Mat4::from_mat3a(affine.matrix3) * Mat4::from_translation(affine.translation.into()); let projection = part_projection.to_cols_array().map(|v| v.to_le_bytes()); rpass.set_bind_group(0, &self.bind_group, &[]); rpass.set_pipeline(&self.pipeline); rpass.set_push_constants(ShaderStages::VERTEX, 0, projection.as_flattened()); rpass.set_index_buffer(part.index.slice(..), IndexFormat::Uint16); rpass.set_vertex_buffer(0, part.position[0].slice(..)); rpass.set_vertex_buffer(1, part.position[1].slice(..)); rpass.set_vertex_buffer(2, part.position[2].slice(..)); rpass.set_vertex_buffer(3, part.normal[0].slice(..)); rpass.set_vertex_buffer(4, part.normal[1].slice(..)); rpass.set_vertex_buffer(5, part.normal[2].slice(..)); rpass.set_vertex_buffer(6, part.texcoord[0].slice(..)); rpass.set_vertex_buffer(7, part.texcoord[1].slice(..)); rpass.draw_indexed(0..part.index_count, 0, 0..1); } } else { prefabs_needed.insert(ob.res); } } } }