Skip to main content

ascending_graphics/ui/
pipeline.rs

1use crate::{
2    BufferLayout, GpuDevice, LayoutStorage, PipeLineLayout, RectVertex,
3    StaticVertexBuffer, SystemLayout, TextureLayout,
4};
5use bytemuck::{Pod, Zeroable};
6
7/// [`crate::Rect`] RenderPipeline Layout
8///
9#[repr(C)]
10#[derive(Clone, Copy, Hash, Pod, Zeroable)]
11pub struct RectRenderPipeline;
12
13impl PipeLineLayout for RectRenderPipeline {
14    fn create_layout(
15        &self,
16        gpu_device: &mut GpuDevice,
17        layouts: &mut LayoutStorage,
18        surface_format: wgpu::TextureFormat,
19    ) -> wgpu::RenderPipeline {
20        let shader = gpu_device.device().create_shader_module(
21            wgpu::ShaderModuleDescriptor {
22                label: Some("Shader"),
23                source: wgpu::ShaderSource::Wgsl(
24                    include_str!("../shaders/rectangle_shader.wgsl").into(),
25                ),
26            },
27        );
28
29        let system_layout = layouts.create_layout(gpu_device, SystemLayout);
30        let texture_layout = layouts.create_layout(gpu_device, TextureLayout);
31
32        // Create the render pipeline.
33        gpu_device.device().create_render_pipeline(
34            &wgpu::RenderPipelineDescriptor {
35                label: Some("rectangle_render_pipeline"),
36                layout: Some(&gpu_device.device().create_pipeline_layout(
37                    &wgpu::PipelineLayoutDescriptor {
38                        label: Some("rectangle_render_pipeline_layout"),
39                        bind_group_layouts: &[
40                            Some(&system_layout),
41                            Some(&texture_layout),
42                        ],
43                        immediate_size: 0,
44                    },
45                )),
46                vertex: wgpu::VertexState {
47                    module: &shader,
48                    entry_point: Some("vertex"),
49                    buffers: &[
50                        Some(wgpu::VertexBufferLayout {
51                            array_stride: StaticVertexBuffer::stride(),
52                            step_mode: wgpu::VertexStepMode::Vertex,
53                            attributes: &[
54                                StaticVertexBuffer::vertex_attribute(),
55                            ],
56                        }),
57                        Some(wgpu::VertexBufferLayout {
58                            array_stride: RectVertex::stride() as u64,
59                            step_mode: wgpu::VertexStepMode::Instance,
60                            attributes: &RectVertex::attributes(),
61                        }),
62                    ],
63                    compilation_options: Default::default(),
64                },
65                primitive: wgpu::PrimitiveState {
66                    topology: wgpu::PrimitiveTopology::TriangleList,
67                    strip_index_format: None,
68                    front_face: wgpu::FrontFace::Ccw,
69                    cull_mode: None,
70                    unclipped_depth: false,
71                    polygon_mode: wgpu::PolygonMode::Fill,
72                    conservative: false,
73                },
74                depth_stencil: Some(wgpu::DepthStencilState {
75                    format: wgpu::TextureFormat::Depth32Float,
76                    depth_write_enabled: Some(true),
77                    depth_compare: Some(wgpu::CompareFunction::LessEqual),
78                    stencil: wgpu::StencilState::default(),
79                    bias: wgpu::DepthBiasState::default(),
80                }),
81                multisample: wgpu::MultisampleState::default(),
82                fragment: Some(wgpu::FragmentState {
83                    module: &shader,
84                    entry_point: Some("fragment"),
85                    targets: &[Some(wgpu::ColorTargetState {
86                        format: surface_format,
87                        blend: Some(wgpu::BlendState::ALPHA_BLENDING),
88                        write_mask: wgpu::ColorWrites::ALL,
89                    })],
90                    compilation_options: Default::default(),
91                }),
92                multiview_mask: None,
93                cache: None,
94            },
95        )
96    }
97}