wgpu_quick_start/
my_render_pipeline.rs

1use wgpu::VertexBufferLayout;
2
3/// Descriptor to configure creating a render pipeline
4pub struct MyRenderPipelineDescriptor {
5    /// Entry-point of the function in the shader wgsl file. Default is `vs_main`
6    vertex_entry_point: String,
7
8    /// Name of the function in the shader wgsl file. Default is `fs_main`
9    fragment_entry_point: String,
10
11    /// Label assigned to the render pipeline
12    label_render_pipeline: String,
13
14    /// Topology of the points/vertices. Default is `wgpu::PrimitiveTopology::TriangleList`
15    topology: wgpu::PrimitiveTopology,
16}
17
18impl Default for MyRenderPipelineDescriptor {
19    fn default() -> Self {
20        Self::new()
21    }
22}
23
24impl MyRenderPipelineDescriptor {
25    pub fn new() -> Self {
26        Self {
27            vertex_entry_point: "vs_main".to_owned(),
28            fragment_entry_point: "fs_main".to_owned(),
29            label_render_pipeline: "Render Pipeline".to_owned(),
30            topology: wgpu::PrimitiveTopology::TriangleList,
31        }
32    }
33
34    pub fn with_label_render_pipeline(mut self, label_render_pipeline: String) -> Self {
35        self.label_render_pipeline = label_render_pipeline;
36        self
37    }
38
39    pub fn with_vertex_entry_point(mut self, vertex_entry_point: String) -> Self {
40        self.vertex_entry_point = vertex_entry_point;
41        self
42    }
43
44    pub fn with_fragment_entry_point(mut self, fragment_entry_point: String) -> Self {
45        self.fragment_entry_point = fragment_entry_point;
46        self
47    }
48
49    pub fn with_topology(mut self, topology: wgpu::PrimitiveTopology) -> Self {
50        self.topology = topology;
51        self
52    }
53}
54
55/// Create a pipeline layout with default values
56pub fn create_default_pipeline_layout(
57    device: &wgpu::Device,
58    label_pipeline_layout: Option<&str>,
59) -> wgpu::PipelineLayout {
60    device.create_pipeline_layout(&wgpu::PipelineLayoutDescriptor {
61        label: label_pipeline_layout,
62        bind_group_layouts: &[],
63        immediate_size: 0,
64    })
65}
66
67/// Create a new render pipeline with default values.
68///
69///
70pub fn create_default_render_pipeline<'a>(
71    device: &wgpu::Device,
72    shader: &'a wgpu::ShaderModule,
73    format: wgpu::TextureFormat,
74    render_pipeline_layout: &wgpu::PipelineLayout,
75    buffers: &'a [VertexBufferLayout<'a>],
76    descriptor: &MyRenderPipelineDescriptor,
77) -> wgpu::RenderPipeline {
78    device.create_render_pipeline(&wgpu::RenderPipelineDescriptor {
79        label: Some(&descriptor.label_render_pipeline),
80        layout: Some(render_pipeline_layout),
81        vertex: wgpu::VertexState {
82            module: shader,
83            entry_point: Some(&descriptor.vertex_entry_point),
84            buffers,
85            compilation_options: wgpu::PipelineCompilationOptions::default(),
86        },
87        fragment: Some(wgpu::FragmentState {
88            module: shader,
89            entry_point: Some(&descriptor.fragment_entry_point),
90            targets: &[Some(wgpu::ColorTargetState {
91                format,
92                blend: Some(wgpu::BlendState::REPLACE),
93                write_mask: wgpu::ColorWrites::ALL,
94            })],
95            compilation_options: wgpu::PipelineCompilationOptions::default(),
96        }),
97        primitive: wgpu::PrimitiveState {
98            topology: descriptor.topology,
99            strip_index_format: None,
100            front_face: wgpu::FrontFace::Ccw,
101            cull_mode: Some(wgpu::Face::Back),
102            polygon_mode: wgpu::PolygonMode::Fill,
103            unclipped_depth: false,
104            conservative: false,
105        },
106        depth_stencil: None,
107        multisample: wgpu::MultisampleState {
108            count: 1,
109            mask: !0,
110            alpha_to_coverage_enabled: false,
111        },
112        multiview_mask: None,
113        cache: None,
114    })
115}