Ghastrod 1 rok pred
rodič
commit
1d87af1865
4 zmenil súbory, kde vykonal 89 pridanie a 3 odobranie
  1. 66 1
      src/lib.rs
  2. 0 1
      src/main.rs
  3. 19 1
      src/object.rs
  4. 4 0
      src/shader.wgsl

+ 66 - 1
src/lib.rs

@@ -1,5 +1,6 @@
 use std::iter;
 
+use wgpu::{util::DeviceExt, BlendState, ColorTargetState, PipelineLayoutDescriptor, RenderPipelineDescriptor};
 use winit::{
     event::*,
     event_loop::EventLoop,
@@ -16,6 +17,8 @@ struct State<'a> {
     queue: wgpu::Queue,
     config: wgpu::SurfaceConfiguration,
     size: winit::dpi::PhysicalSize<u32>,
+    render_pipeline : wgpu::RenderPipeline,
+    vertex_buffer: wgpu::Buffer,
     // The window must be declared after the surface so
     // it gets dropped after it as the surface contains
     // unsafe references to the window's resources.
@@ -87,8 +90,68 @@ impl<'a> State<'a> {
             view_formats: vec![],
         };
 
+        let shader = device.create_shader_module(wgpu::ShaderModuleDescriptor {
+            label: Some("Shader 1 from shader.wgsl"),
+            source: wgpu::ShaderSource::Wgsl(include_str!("shader.wgsl").into()),
+        });
+        
+
+        let pipeline_layout = device.create_pipeline_layout(&PipelineLayoutDescriptor{
+            label: Some("Pipeline Layout"),
+            bind_group_layouts: &[
+
+            ],
+            push_constant_ranges: &[]
+        });
+
+        let render_pipeline = device.create_render_pipeline(&RenderPipelineDescriptor{
+            label: Some("RenderPipeline"),
+            layout: Some(&pipeline_layout),
+            vertex: wgpu::VertexState {
+                module: &shader,
+                entry_point: "@vertex",
+                buffers: &[
+                    Vertex::desc(),
+                ],
+            },
+            primitive: wgpu::PrimitiveState{
+                topology: wgpu::PrimitiveTopology::TriangleList,
+                strip_index_format: None,
+                front_face: wgpu::FrontFace::Ccw,
+                cull_mode: Some(wgpu::Face::Back),
+                unclipped_depth: false,
+                polygon_mode: wgpu::PolygonMode::Line,
+                conservative: false
+            },
+            depth_stencil: None,
+            multisample: wgpu::MultisampleState{
+                count: 1,
+                mask: !0,
+                alpha_to_coverage_enabled: false,
+            },
+            fragment: Some(wgpu::FragmentState{
+                module: &shader,
+                entry_point: "@fragment",
+                targets: &[Some(ColorTargetState{
+                    format: config.format,
+                    blend: Some(BlendState::REPLACE),
+                    write_mask: wgpu::ColorWrites::ALL
+                })]
+            }),
+            multiview: None
+        });
+        let plan1 = generate_plane();
+        let vertex_buffer = device.create_buffer_init(&wgpu::util::BufferInitDescriptor{
+            label: Some("Vertex Buffer 1"),
+            contents: bytemuck::cast_slice(todo!()),
+            usage: wgpu::BufferUsages::VERTEX,
+        });
+
+
         Self {
             surface,
+            vertex_buffer,
+            render_pipeline,
             device,
             queue,
             config,
@@ -130,7 +193,7 @@ impl<'a> State<'a> {
             });
 
         {
-            let _render_pass = encoder.begin_render_pass(&wgpu::RenderPassDescriptor {
+            let mut render_pass = encoder.begin_render_pass(&wgpu::RenderPassDescriptor {
                 label: Some("Render Pass"),
                 color_attachments: &[Some(wgpu::RenderPassColorAttachment {
                     view: &view,
@@ -149,6 +212,8 @@ impl<'a> State<'a> {
                 occlusion_query_set: None,
                 timestamp_writes: None,
             });
+            render_pass.set_pipeline(&self.render_pipeline);
+            //render_pass.draws
         }
 
         self.queue.submit(iter::once(encoder.finish()));

+ 0 - 1
src/main.rs

@@ -6,6 +6,5 @@ mod window;
 mod object;
 fn main() {
     println!("Hello, world!");
-    println!("{:?}", generate_plane(2, 2, 10., 10.).len());
     pollster::block_on(run());
 }

+ 19 - 1
src/object.rs

@@ -1,11 +1,26 @@
+use wgpu::vertex_attr_array;
+
 #[repr(C)]
 #[derive(Copy, Clone, Debug, bytemuck::Pod, bytemuck::Zeroable)]
-struct Vertex {
+pub struct Vertex {
     position: [f32; 3],
     color: [f32; 3],
     uv: [f32; 2],
 }
 
+impl Vertex{
+    const ATTRIB : [wgpu::VertexAttribute; 3]= vertex_attr_array![0 => Float32x3, 1 => Float32x3, 2 => Float32x2];
+
+    pub fn desc() -> wgpu::VertexBufferLayout<'static>{
+        wgpu::VertexBufferLayout{
+            array_stride: std::mem::size_of::<Vertex>() as wgpu::BufferAddress,
+            step_mode: wgpu::VertexStepMode::Vertex,
+            attributes: &Self::ATTRIB
+        }
+    }
+}
+
+
 #[derive(Default, Debug)]
 pub struct Mesh {
     vertices: Vec<Vertex>,
@@ -15,6 +30,9 @@ impl Mesh{
     pub fn len(self) -> usize{
         return self.vertices.len()
     }
+    pub fn vertices(self)-> Vec<Vertex>{
+        self.vertices
+    }
 }
 
 pub fn generate_plane(num_segx: u32, num_segy: u32, width: f32, height: f32) -> Mesh {

+ 4 - 0
src/shader.wgsl

@@ -4,3 +4,7 @@
 fn fs_main(in: VertexOutput) -> @location(0) vec4<f32> {
     return vec4<f32>(0.3, 0.2, 0.1, 1.0);
 }
+@vertex
+fn vs_main(){
+    
+}