Sfoglia il codice sorgente

Separation of Wireframe Pipeline

Ghastrod 1 anno fa
parent
commit
3c45a02fc9
5 ha cambiato i file con 108 aggiunte e 4 eliminazioni
  1. 17 4
      src/lib.rs
  2. 1 0
      src/main.rs
  3. 35 0
      src/shaders/plane_shader.wgsl
  4. 1 0
      src/wireframe/mod.rs
  5. 54 0
      src/wireframe/wireframe_pipeline.rs

+ 17 - 4
src/lib.rs

@@ -1,7 +1,7 @@
 use std::{iter, time::{Duration, Instant}};
 
 use camera::{camera_buffer, camera_controller, camera_struct::Camera, camera_uniform};
-use wgpu::{util::DeviceExt, BlendState, ColorTargetState, PipelineLayoutDescriptor, RenderPipelineDescriptor};
+use wgpu::{util::DeviceExt, BlendState, ColorTargetState, PipelineLayoutDescriptor, RenderPipeline, RenderPipelineDescriptor};
 use winit::{
     event::*,
     event_loop::EventLoop,
@@ -11,11 +11,14 @@ use winit::{
 mod camera;
 mod object;
 mod sky;
+mod wireframe;
 
 #[cfg(target_arch = "wasm32")]
 use wasm_bindgen::prelude::*;
 
 struct State<'a> {
+    wireframe: bool,
+    wireframe_pipeline: RenderPipeline,
     surface: wgpu::Surface<'a>,
     device: wgpu::Device,
     queue: wgpu::Queue,
@@ -111,7 +114,7 @@ impl<'a> State<'a> {
 
         let shader = device.create_shader_module(wgpu::ShaderModuleDescriptor {
             label: Some("Shader 1 from shader.wgsl"),
-            source: wgpu::ShaderSource::Wgsl(include_str!("./shaders/shader.wgsl").into()),
+            source: wgpu::ShaderSource::Wgsl(include_str!("./shaders/plane_shader.wgsl").into()),
         });
         
 
@@ -153,7 +156,7 @@ impl<'a> State<'a> {
                 front_face: wgpu::FrontFace::Ccw,
                 cull_mode: None,
                 unclipped_depth: false,
-                polygon_mode: wgpu::PolygonMode::Line,
+                polygon_mode: wgpu::PolygonMode::Fill, //Or wgpu::PolygonMode::Fill
                 conservative: false
             },
             depth_stencil: None,
@@ -182,10 +185,15 @@ impl<'a> State<'a> {
         let old_frame_times : Vec<f64> = vec![];
         let max_history: usize = 60;
         let last_fps = Instant::now();
+
+        let wireframe_pipeline = wireframe::wireframe_pipeline::new(&device, &config, &camera_bind_group_layout);
+        let wireframe = true;
         Self {
+            wireframe,
             surface,
             vertex_buffer,
             render_pipeline,
+            wireframe_pipeline,
             device,
             queue,
             config,
@@ -280,7 +288,12 @@ impl<'a> State<'a> {
             });
             //render_pass.set_pipeline(&self.sky_pipeline);
 
-            render_pass.set_pipeline(&self.render_pipeline);
+            if self.wireframe {
+                render_pass.set_pipeline(&self.wireframe_pipeline);
+            }else{
+                render_pass.set_pipeline(&self.render_pipeline);
+            }
+
             render_pass.set_vertex_buffer(0, self.vertex_buffer.slice(..));
             render_pass.set_bind_group(0, &self.camera_bind_group, &[]);
             render_pass.draw(0..self.num_vertices, 0..1);

+ 1 - 0
src/main.rs

@@ -5,6 +5,7 @@ use crate::object::generate_plane;
 mod camera;
 mod compute;
 mod sky;
+mod wireframe;
 mod object;
 fn main() {
     println!("wgpu5 launched");

+ 35 - 0
src/shaders/plane_shader.wgsl

@@ -0,0 +1,35 @@
+// Vertex shader
+
+struct CameraUniform {
+    view_proj: mat4x4<f32>
+}
+
+@group(0) @binding(0)
+var<uniform> camera: CameraUniform;
+
+struct VertexInput {
+    @location(0) position: vec3<f32>,
+    @location(1) color: vec3<f32>,
+};
+
+struct VertexOutput {
+    @builtin(position) clip_position: vec4<f32>,
+    @location(0) color: vec3<f32>,
+};
+
+@vertex
+fn vs_main(
+    model: VertexInput,
+) -> VertexOutput {
+    var out: VertexOutput;
+    out.color = model.color;
+    out.clip_position = camera.view_proj * vec4<f32>(model.position, 1.0);
+    return out;
+}
+
+// Fragment shader
+
+@fragment
+fn fs_main(in: VertexOutput) -> @location(0) vec4<f32> {
+    return vec4<f32>(1.0,0.0,0.0, 1.0);
+}

+ 1 - 0
src/wireframe/mod.rs

@@ -0,0 +1 @@
+pub mod wireframe_pipeline;

+ 54 - 0
src/wireframe/wireframe_pipeline.rs

@@ -0,0 +1,54 @@
+use wgpu::{include_wgsl, BlendState, ColorTargetState, RenderPipeline, RenderPipelineDescriptor, SurfaceConfiguration};
+
+use crate::object;
+
+pub fn new(device: &wgpu::Device, config: &SurfaceConfiguration,camera_bind_group_layout: &wgpu::BindGroupLayout)-> RenderPipeline{
+
+    let wireframe_layout = device.create_pipeline_layout(&wgpu::PipelineLayoutDescriptor{
+        label: Some("Wireframe Pipeline Layout"),
+        bind_group_layouts: &[
+            &camera_bind_group_layout
+        ],
+        push_constant_ranges: &[]
+    });
+
+    let shader_module = device.create_shader_module(include_wgsl!("../shaders/plane_shader.wgsl"));
+
+    let wireframe_pipeline = device.create_render_pipeline(&RenderPipelineDescriptor{
+        label: Some("Wireframe Pipeline"),
+        layout: Some(&wireframe_layout),
+        vertex: wgpu::VertexState {
+            module: &shader_module,
+            entry_point: "vs_main".into(),
+            buffers: &[
+                object::Vertex::desc(),
+            ],
+        },
+        primitive: wgpu::PrimitiveState{
+            topology: wgpu::PrimitiveTopology::TriangleList,
+            strip_index_format: None,
+            front_face: wgpu::FrontFace::Ccw,
+            cull_mode: None,
+            unclipped_depth: false,
+            polygon_mode: wgpu::PolygonMode::Line, //Or wgpu::PolygonMode::Fill
+            conservative: false
+        },
+        depth_stencil: None,
+        multisample: wgpu::MultisampleState{
+            count: 1,
+            mask: !0,
+            alpha_to_coverage_enabled: false,
+        },
+        fragment: Some(wgpu::FragmentState{
+            module: &shader_module,
+            entry_point: "fs_main",
+            targets: &[Some(ColorTargetState{
+                format: config.format,
+                blend: Some(BlendState::REPLACE),
+                write_mask: wgpu::ColorWrites::ALL
+            })]
+        }),
+        multiview: None
+    });
+    return  wireframe_pipeline
+}