veloren_voxygen/render/pipelines/
figure.rs

1use super::{
2    super::{AaMode, Bound, Consts, GlobalsLayouts, Mesh, Model},
3    AtlasData,
4    terrain::Vertex,
5};
6use crate::mesh::greedy::GreedyMesh;
7use bytemuck::{Pod, Zeroable};
8use vek::*;
9
10#[repr(C)]
11#[derive(Copy, Clone, Debug, Zeroable, Pod)]
12pub struct Locals {
13    model_mat: [[f32; 4]; 4],
14    highlight_col: [f32; 4],
15    model_light: [f32; 4],
16    model_glow: [f32; 4],
17    atlas_offs: [i32; 4],
18    model_pos: [f32; 3],
19    flags: u32,
20}
21
22#[repr(C)]
23#[derive(Copy, Clone, Debug, Zeroable, Pod)]
24pub struct BoneData {
25    bone_mat: [[f32; 4]; 4],
26    normals_mat: [[f32; 4]; 4],
27}
28
29pub type BoundLocals = Bound<(Consts<Locals>, Consts<BoneData>)>;
30
31impl Locals {
32    pub fn new(
33        model_mat: anim::vek::Mat4<f32>,
34        col: Rgb<f32>,
35        pos: anim::vek::Vec3<f32>,
36        atlas_offs: Vec2<i32>,
37        is_player: bool,
38        light: f32,
39        glow: (Vec3<f32>, f32),
40    ) -> Self {
41        let mut flags = 0;
42        flags |= is_player as u32;
43
44        Self {
45            model_mat: model_mat.into_col_arrays(),
46            highlight_col: [col.r, col.g, col.b, 1.0],
47            model_pos: pos.into_array(),
48            atlas_offs: Vec4::from(atlas_offs).into_array(),
49            model_light: [light, 1.0, 1.0, 1.0],
50            model_glow: [glow.0.x, glow.0.y, glow.0.z, glow.1],
51            flags,
52        }
53    }
54}
55
56impl Default for Locals {
57    fn default() -> Self {
58        Self::new(
59            anim::vek::Mat4::identity(),
60            Rgb::broadcast(1.0),
61            anim::vek::Vec3::default(),
62            Vec2::default(),
63            false,
64            1.0,
65            (Vec3::zero(), 0.0),
66        )
67    }
68}
69
70impl BoneData {
71    pub fn new(bone_mat: anim::vek::Mat4<f32>, normals_mat: anim::vek::Mat4<f32>) -> Self {
72        Self {
73            bone_mat: bone_mat.into_col_arrays(),
74            normals_mat: normals_mat.into_col_arrays(),
75        }
76    }
77}
78
79impl Default for BoneData {
80    fn default() -> Self { Self::new(anim::vek::Mat4::identity(), anim::vek::Mat4::identity()) }
81}
82
83pub struct FigureModel {
84    pub opaque: Option<Model<Vertex>>,
85    /* TODO: Consider using mipmaps instead of storing multiple texture atlases for different
86     * LOD levels. */
87}
88
89impl FigureModel {
90    /// Start a greedy mesh designed for figure bones.
91    pub fn make_greedy<'a>() -> GreedyMesh<'a, FigureSpriteAtlasData> {
92        // NOTE: Required because we steal two bits from the normal in the shadow uint
93        // in order to store the bone index.  The two bits are instead taken out
94        // of the atlas coordinates, which is why we "only" allow 1 << 15 per
95        // coordinate instead of 1 << 16.
96        let max_size = Vec2::new((1 << 15) - 1, (1 << 15) - 1);
97        GreedyMesh::new(max_size, crate::mesh::greedy::general_config())
98    }
99}
100
101pub type BoneMeshes = (Mesh<Vertex>, anim::vek::Aabb<f32>);
102
103pub struct FigureLayout {
104    pub locals: wgpu::BindGroupLayout,
105}
106
107impl FigureLayout {
108    pub fn new(device: &wgpu::Device) -> Self {
109        Self {
110            locals: device.create_bind_group_layout(&wgpu::BindGroupLayoutDescriptor {
111                label: None,
112                entries: &[
113                    // locals
114                    wgpu::BindGroupLayoutEntry {
115                        binding: 0,
116                        visibility: wgpu::ShaderStages::VERTEX_FRAGMENT,
117                        ty: wgpu::BindingType::Buffer {
118                            ty: wgpu::BufferBindingType::Uniform,
119                            has_dynamic_offset: false,
120                            min_binding_size: None,
121                        },
122                        count: None,
123                    },
124                    // bone data
125                    wgpu::BindGroupLayoutEntry {
126                        binding: 1,
127                        visibility: wgpu::ShaderStages::VERTEX_FRAGMENT,
128                        ty: wgpu::BindingType::Buffer {
129                            ty: wgpu::BufferBindingType::Uniform,
130                            has_dynamic_offset: false,
131                            min_binding_size: None,
132                        },
133                        count: None,
134                    },
135                ],
136            }),
137        }
138    }
139
140    pub fn bind_locals(
141        &self,
142        device: &wgpu::Device,
143        locals: Consts<Locals>,
144        bone_data: Consts<BoneData>,
145    ) -> BoundLocals {
146        let bind_group = device.create_bind_group(&wgpu::BindGroupDescriptor {
147            label: None,
148            layout: &self.locals,
149            entries: &[
150                wgpu::BindGroupEntry {
151                    binding: 0,
152                    resource: locals.buf().as_entire_binding(),
153                },
154                wgpu::BindGroupEntry {
155                    binding: 1,
156                    resource: bone_data.buf().as_entire_binding(),
157                },
158            ],
159        });
160
161        BoundLocals {
162            bind_group,
163            with: (locals, bone_data),
164        }
165    }
166}
167
168pub struct FigurePipeline {
169    pub pipeline: wgpu::RenderPipeline,
170}
171
172impl FigurePipeline {
173    pub fn new(
174        device: &wgpu::Device,
175        vs_module: &wgpu::ShaderModule,
176        fs_module: &wgpu::ShaderModule,
177        global_layout: &GlobalsLayouts,
178        layout: &FigureLayout,
179        aa_mode: AaMode,
180        format: wgpu::TextureFormat,
181    ) -> Self {
182        common_base::span!(_guard, "FigurePipeline::new");
183        let render_pipeline_layout =
184            device.create_pipeline_layout(&wgpu::PipelineLayoutDescriptor {
185                label: Some("Figure pipeline layout"),
186                push_constant_ranges: &[],
187                bind_group_layouts: &[
188                    &global_layout.globals,
189                    &global_layout.shadow_textures,
190                    global_layout.figure_sprite_atlas_layout.layout(),
191                    &layout.locals,
192                ],
193            });
194
195        let samples = aa_mode.samples();
196
197        let render_pipeline = device.create_render_pipeline(&wgpu::RenderPipelineDescriptor {
198            label: Some("Figure pipeline"),
199            layout: Some(&render_pipeline_layout),
200            vertex: wgpu::VertexState {
201                module: vs_module,
202                entry_point: Some("main"),
203                buffers: &[Vertex::desc()],
204                compilation_options: Default::default(),
205            },
206            primitive: wgpu::PrimitiveState {
207                topology: wgpu::PrimitiveTopology::TriangleList,
208                strip_index_format: None,
209                front_face: wgpu::FrontFace::Ccw,
210                cull_mode: Some(wgpu::Face::Back),
211                unclipped_depth: false,
212                polygon_mode: wgpu::PolygonMode::Fill,
213                conservative: false,
214            },
215            depth_stencil: Some(wgpu::DepthStencilState {
216                format: wgpu::TextureFormat::Depth32Float,
217                depth_write_enabled: true,
218                depth_compare: wgpu::CompareFunction::GreaterEqual,
219                stencil: wgpu::StencilState {
220                    front: wgpu::StencilFaceState::IGNORE,
221                    back: wgpu::StencilFaceState::IGNORE,
222                    read_mask: !0,
223                    write_mask: 0,
224                },
225                bias: wgpu::DepthBiasState {
226                    constant: 0,
227                    slope_scale: 0.0,
228                    clamp: 0.0,
229                },
230            }),
231            multisample: wgpu::MultisampleState {
232                count: samples,
233                mask: !0,
234                alpha_to_coverage_enabled: false,
235            },
236            fragment: Some(wgpu::FragmentState {
237                module: fs_module,
238                entry_point: Some("main"),
239                targets: &[
240                    Some(wgpu::ColorTargetState {
241                        format,
242                        blend: None,
243                        write_mask: wgpu::ColorWrites::ALL,
244                    }),
245                    Some(wgpu::ColorTargetState {
246                        format: wgpu::TextureFormat::Rgba8Uint,
247                        blend: None,
248                        write_mask: wgpu::ColorWrites::ALL,
249                    }),
250                ],
251                compilation_options: Default::default(),
252            }),
253            multiview: None,
254            cache: None,
255        });
256
257        Self {
258            pipeline: render_pipeline,
259        }
260    }
261}
262
263/// Represents texture that can be converted into texture atlases for figures
264/// and sprites.
265pub struct FigureSpriteAtlasData {
266    pub col_lights: Vec<[u8; 4]>,
267}
268
269impl AtlasData for FigureSpriteAtlasData {
270    type SliceMut<'a> = std::slice::IterMut<'a, [u8; 4]>;
271
272    const TEXTURES: usize = 1;
273
274    fn blank_with_size(sz: Vec2<u16>) -> Self {
275        let col_lights =
276            vec![Vertex::make_col_light(254, 0, Rgb::broadcast(254), true); sz.as_().product()];
277        Self { col_lights }
278    }
279
280    fn as_texture_data(&self) -> [(wgpu::TextureFormat, &[u8]); Self::TEXTURES] {
281        [(
282            wgpu::TextureFormat::Rgba8Unorm,
283            bytemuck::cast_slice(self.col_lights.as_slice()),
284        )]
285    }
286
287    fn layout() -> Vec<wgpu::BindGroupLayoutEntry> {
288        vec![
289            // col lights
290            wgpu::BindGroupLayoutEntry {
291                binding: 0,
292                visibility: wgpu::ShaderStages::VERTEX_FRAGMENT,
293                ty: wgpu::BindingType::Texture {
294                    sample_type: wgpu::TextureSampleType::Float { filterable: true },
295                    view_dimension: wgpu::TextureViewDimension::D2,
296                    multisampled: false,
297                },
298                count: None,
299            },
300            wgpu::BindGroupLayoutEntry {
301                binding: 1,
302                visibility: wgpu::ShaderStages::VERTEX_FRAGMENT,
303                ty: wgpu::BindingType::Sampler(wgpu::SamplerBindingType::Filtering),
304                count: None,
305            },
306        ]
307    }
308
309    fn slice_mut(&mut self, range: std::ops::Range<usize>) -> Self::SliceMut<'_> {
310        self.col_lights[range].iter_mut()
311    }
312}