veloren_voxygen/render/pipelines/
skybox.rs1use 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
30pub 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: "main",
60 buffers: &[Vertex::desc()],
61 },
62 primitive: wgpu::PrimitiveState {
63 topology: wgpu::PrimitiveTopology::TriangleList,
64 strip_index_format: None,
65 front_face: wgpu::FrontFace::Ccw,
66 cull_mode: Some(wgpu::Face::Back),
67 unclipped_depth: false,
68 polygon_mode: wgpu::PolygonMode::Fill,
69 conservative: false,
70 },
71 depth_stencil: Some(wgpu::DepthStencilState {
72 format: wgpu::TextureFormat::Depth32Float,
73 depth_write_enabled: true,
74 depth_compare: wgpu::CompareFunction::GreaterEqual,
75 stencil: wgpu::StencilState {
76 front: wgpu::StencilFaceState::IGNORE,
77 back: wgpu::StencilFaceState::IGNORE,
78 read_mask: !0,
79 write_mask: 0,
80 },
81 bias: wgpu::DepthBiasState {
82 constant: 0,
83 slope_scale: 0.0,
84 clamp: 0.0,
85 },
86 }),
87 multisample: wgpu::MultisampleState {
88 count: samples,
89 mask: !0,
90 alpha_to_coverage_enabled: false,
91 },
92 fragment: Some(wgpu::FragmentState {
93 module: fs_module,
94 entry_point: "main",
95 targets: &[
96 Some(wgpu::ColorTargetState {
97 format,
98 blend: Some(wgpu::BlendState {
99 color: wgpu::BlendComponent::REPLACE,
100 alpha: wgpu::BlendComponent {
101 src_factor: wgpu::BlendFactor::Zero,
102 dst_factor: wgpu::BlendFactor::One,
103 operation: wgpu::BlendOperation::Add,
104 },
105 }),
106 write_mask: wgpu::ColorWrites::ALL,
107 }),
108 Some(wgpu::ColorTargetState {
109 format: wgpu::TextureFormat::Rgba8Uint,
110 blend: None,
111 write_mask: wgpu::ColorWrites::ALL,
112 }),
113 ],
114 }),
115 multiview: None,
116 });
117
118 Self {
119 pipeline: render_pipeline,
120 }
121 }
122}
123
124#[rustfmt::skip]
125pub fn create_mesh() -> Mesh<Vertex> {
126 let mut mesh = Mesh::new();
127
128 mesh.push_quad(Quad::new(
130 Vertex { pos: [-1.0, -1.0, -1.0] },
131 Vertex { pos: [-1.0, 1.0, -1.0] },
132 Vertex { pos: [-1.0, 1.0, 1.0] },
133 Vertex { pos: [-1.0, -1.0, 1.0] },
134 ));
135 mesh.push_quad(Quad::new(
137 Vertex { pos: [ 1.0, -1.0, 1.0] },
138 Vertex { pos: [ 1.0, 1.0, 1.0] },
139 Vertex { pos: [ 1.0, 1.0, -1.0] },
140 Vertex { pos: [ 1.0, -1.0, -1.0] },
141 ));
142 mesh.push_quad(Quad::new(
144 Vertex { pos: [ 1.0, -1.0, -1.0] },
145 Vertex { pos: [-1.0, -1.0, -1.0] },
146 Vertex { pos: [-1.0, -1.0, 1.0] },
147 Vertex { pos: [ 1.0, -1.0, 1.0] },
148 ));
149 mesh.push_quad(Quad::new(
151 Vertex { pos: [ 1.0, 1.0, 1.0] },
152 Vertex { pos: [-1.0, 1.0, 1.0] },
153 Vertex { pos: [-1.0, 1.0, -1.0] },
154 Vertex { pos: [ 1.0, 1.0, -1.0] },
155 ));
156 mesh.push_quad(Quad::new(
158 Vertex { pos: [-1.0, -1.0, -1.0] },
159 Vertex { pos: [ 1.0, -1.0, -1.0] },
160 Vertex { pos: [ 1.0, 1.0, -1.0] },
161 Vertex { pos: [-1.0, 1.0, -1.0] },
162 ));
163 mesh.push_quad(Quad::new(
165 Vertex { pos: [-1.0, 1.0, 1.0] },
166 Vertex { pos: [ 1.0, 1.0, 1.0] },
167 Vertex { pos: [ 1.0, -1.0, 1.0] },
168 Vertex { pos: [-1.0, -1.0, 1.0] },
169 ));
170
171 mesh
172}