veloren_voxygen/render/pipelines/
skybox.rs

1use super::super::{AaMode, GlobalsLayouts, Mesh, Quad, Vertex as VertexTrait};
2use bytemuck::{Pod, Zeroable};
3use std::mem;
4
5#[repr(C)]
6#[derive(Copy, Clone, Debug, Zeroable, Pod)]
7pub struct Vertex {
8    pub pos: [f32; 3],
9}
10
11impl Vertex {
12    fn desc<'a>() -> wgpu::VertexBufferLayout<'a> {
13        wgpu::VertexBufferLayout {
14            array_stride: Self::STRIDE,
15            step_mode: wgpu::VertexStepMode::Vertex,
16            attributes: &[wgpu::VertexAttribute {
17                offset: 0,
18                shader_location: 0,
19                format: wgpu::VertexFormat::Float32x3,
20            }],
21        }
22    }
23}
24
25impl VertexTrait for Vertex {
26    const QUADS_INDEX: Option<wgpu::IndexFormat> = None;
27    const STRIDE: wgpu::BufferAddress = mem::size_of::<Self>() as wgpu::BufferAddress;
28}
29
30// TODO: does skybox still do anything with new cloud shaders?
31pub struct SkyboxPipeline {
32    pub pipeline: wgpu::RenderPipeline,
33}
34
35impl SkyboxPipeline {
36    pub fn new(
37        device: &wgpu::Device,
38        vs_module: &wgpu::ShaderModule,
39        fs_module: &wgpu::ShaderModule,
40        layouts: &GlobalsLayouts,
41        aa_mode: AaMode,
42        format: wgpu::TextureFormat,
43    ) -> Self {
44        common_base::span!(_guard, "SkyboxPipeline::new");
45        let render_pipeline_layout =
46            device.create_pipeline_layout(&wgpu::PipelineLayoutDescriptor {
47                label: Some("Skybox pipeline layout"),
48                push_constant_ranges: &[],
49                bind_group_layouts: &[&layouts.globals, &layouts.shadow_textures],
50            });
51
52        let samples = aa_mode.samples();
53
54        let render_pipeline = device.create_render_pipeline(&wgpu::RenderPipelineDescriptor {
55            label: Some("Skybox pipeline"),
56            layout: Some(&render_pipeline_layout),
57            vertex: wgpu::VertexState {
58                module: vs_module,
59                entry_point: Some("main"),
60                buffers: &[Vertex::desc()],
61                compilation_options: Default::default(),
62            },
63            primitive: wgpu::PrimitiveState {
64                topology: wgpu::PrimitiveTopology::TriangleList,
65                strip_index_format: None,
66                front_face: wgpu::FrontFace::Ccw,
67                cull_mode: Some(wgpu::Face::Back),
68                unclipped_depth: false,
69                polygon_mode: wgpu::PolygonMode::Fill,
70                conservative: false,
71            },
72            depth_stencil: Some(wgpu::DepthStencilState {
73                format: wgpu::TextureFormat::Depth32Float,
74                depth_write_enabled: true,
75                depth_compare: wgpu::CompareFunction::GreaterEqual,
76                stencil: wgpu::StencilState {
77                    front: wgpu::StencilFaceState::IGNORE,
78                    back: wgpu::StencilFaceState::IGNORE,
79                    read_mask: !0,
80                    write_mask: 0,
81                },
82                bias: wgpu::DepthBiasState {
83                    constant: 0,
84                    slope_scale: 0.0,
85                    clamp: 0.0,
86                },
87            }),
88            multisample: wgpu::MultisampleState {
89                count: samples,
90                mask: !0,
91                alpha_to_coverage_enabled: false,
92            },
93            fragment: Some(wgpu::FragmentState {
94                module: fs_module,
95                entry_point: Some("main"),
96                targets: &[
97                    Some(wgpu::ColorTargetState {
98                        format,
99                        blend: Some(wgpu::BlendState {
100                            color: wgpu::BlendComponent::REPLACE,
101                            alpha: wgpu::BlendComponent {
102                                src_factor: wgpu::BlendFactor::Zero,
103                                dst_factor: wgpu::BlendFactor::One,
104                                operation: wgpu::BlendOperation::Add,
105                            },
106                        }),
107                        write_mask: wgpu::ColorWrites::ALL,
108                    }),
109                    Some(wgpu::ColorTargetState {
110                        format: wgpu::TextureFormat::Rgba8Uint,
111                        blend: None,
112                        write_mask: wgpu::ColorWrites::ALL,
113                    }),
114                ],
115                compilation_options: Default::default(),
116            }),
117            multiview: None,
118            cache: None,
119        });
120
121        Self {
122            pipeline: render_pipeline,
123        }
124    }
125}
126
127#[rustfmt::skip]
128pub fn create_mesh() -> Mesh<Vertex> {
129    let mut mesh = Mesh::new();
130
131    // -x
132    mesh.push_quad(Quad::new(
133        Vertex { pos: [-1.0, -1.0, -1.0] },
134        Vertex { pos: [-1.0,  1.0, -1.0] },
135        Vertex { pos: [-1.0,  1.0,  1.0] },
136        Vertex { pos: [-1.0, -1.0,  1.0] },
137    ));
138    // +x
139    mesh.push_quad(Quad::new(
140        Vertex { pos: [ 1.0, -1.0,  1.0] },
141        Vertex { pos: [ 1.0,  1.0,  1.0] },
142        Vertex { pos: [ 1.0,  1.0, -1.0] },
143        Vertex { pos: [ 1.0, -1.0, -1.0] },
144    ));
145    // -y
146    mesh.push_quad(Quad::new(
147        Vertex { pos: [ 1.0, -1.0, -1.0] },
148        Vertex { pos: [-1.0, -1.0, -1.0] },
149        Vertex { pos: [-1.0, -1.0,  1.0] },
150        Vertex { pos: [ 1.0, -1.0,  1.0] },
151    ));
152    // +y
153    mesh.push_quad(Quad::new(
154        Vertex { pos: [ 1.0,  1.0,  1.0] },
155        Vertex { pos: [-1.0,  1.0,  1.0] },
156        Vertex { pos: [-1.0,  1.0, -1.0] },
157        Vertex { pos: [ 1.0,  1.0, -1.0] },
158    ));
159    // -z
160    mesh.push_quad(Quad::new(
161        Vertex { pos: [-1.0, -1.0, -1.0] },
162        Vertex { pos: [ 1.0, -1.0, -1.0] },
163        Vertex { pos: [ 1.0,  1.0, -1.0] },
164        Vertex { pos: [-1.0,  1.0, -1.0] },
165    ));
166    // +z
167    mesh.push_quad(Quad::new(
168        Vertex { pos: [-1.0,  1.0,  1.0] },
169        Vertex { pos: [ 1.0,  1.0,  1.0] },
170        Vertex { pos: [ 1.0, -1.0,  1.0] },
171        Vertex { pos: [-1.0, -1.0,  1.0] },
172    ));
173
174    mesh
175}