rustial-renderer-wgpu 0.0.1

Pure WGPU renderer for the rustial 2.5D map engine
Documentation
//! Tile render pipeline.

use crate::gpu::depth::DEPTH_FORMAT;
use crate::gpu::vertex::TileVertex;
use crate::pipeline::uniforms::ViewProjUniform;

/// The tile rendering pipeline and associated bind group layout.
pub struct TilePipeline {
    /// Render pipeline configured for map tile quad drawing.
    pub pipeline: wgpu::RenderPipeline,
    /// Render pipeline for translucent tile draws.
    ///
    /// Uses the same shader and bindings as the opaque pipeline but keeps
    /// depth testing while disabling depth writes so same-plane cross-fades
    /// do not self-occlude.
    pub translucent_pipeline: wgpu::RenderPipeline,
    /// Bind group layout for frame-global view-projection uniforms.
    pub uniform_bind_group_layout: wgpu::BindGroupLayout,
    /// Bind group layout for per-tile texture + sampler.
    pub texture_bind_group_layout: wgpu::BindGroupLayout,
}

impl TilePipeline {
    /// Create the tile pipeline.
    pub fn new(device: &wgpu::Device, surface_format: wgpu::TextureFormat) -> Self {
        let shader = device.create_shader_module(wgpu::ShaderModuleDescriptor {
            label: Some("tile_shader"),
            source: wgpu::ShaderSource::Wgsl(include_str!("../shaders/tile.wgsl").into()),
        });

        // Bind group 0: view-projection uniform.
        let uniform_bind_group_layout =
            device.create_bind_group_layout(&wgpu::BindGroupLayoutDescriptor {
                label: Some("tile_uniform_bgl"),
                entries: &[wgpu::BindGroupLayoutEntry {
                    binding: 0,
                    visibility: wgpu::ShaderStages::VERTEX_FRAGMENT,
                    ty: wgpu::BindingType::Buffer {
                        ty: wgpu::BufferBindingType::Uniform,
                        has_dynamic_offset: false,
                        min_binding_size: wgpu::BufferSize::new(
                            std::mem::size_of::<ViewProjUniform>() as u64,
                        ),
                    },
                    count: None,
                }],
            });

        // Bind group 1: texture + sampler.
        let texture_bind_group_layout =
            device.create_bind_group_layout(&wgpu::BindGroupLayoutDescriptor {
                label: Some("tile_texture_bgl"),
                entries: &[
                    wgpu::BindGroupLayoutEntry {
                        binding: 0,
                        visibility: wgpu::ShaderStages::FRAGMENT,
                        ty: wgpu::BindingType::Texture {
                            multisampled: false,
                            view_dimension: wgpu::TextureViewDimension::D2,
                            sample_type: wgpu::TextureSampleType::Float { filterable: true },
                        },
                        count: None,
                    },
                    wgpu::BindGroupLayoutEntry {
                        binding: 1,
                        visibility: wgpu::ShaderStages::FRAGMENT,
                        ty: wgpu::BindingType::Sampler(wgpu::SamplerBindingType::Filtering),
                        count: None,
                    },
                ],
            });

        let pipeline_layout = device.create_pipeline_layout(&wgpu::PipelineLayoutDescriptor {
            label: Some("tile_pipeline_layout"),
            bind_group_layouts: &[&uniform_bind_group_layout, &texture_bind_group_layout],
            push_constant_ranges: &[],
        });

        let build_pipeline = |label: &'static str,
                              depth_write_enabled: bool,
                              depth_compare: wgpu::CompareFunction| {
            device.create_render_pipeline(&wgpu::RenderPipelineDescriptor {
                label: Some(label),
                layout: Some(&pipeline_layout),
                vertex: wgpu::VertexState {
                    module: &shader,
                    entry_point: Some("vs_main"),
                    buffers: &[TileVertex::layout()],
                    compilation_options: wgpu::PipelineCompilationOptions::default(),
                },
                fragment: Some(wgpu::FragmentState {
                    module: &shader,
                    entry_point: Some("fs_main"),
                    targets: &[Some(wgpu::ColorTargetState {
                        format: surface_format,
                        blend: Some(wgpu::BlendState::ALPHA_BLENDING),
                        write_mask: wgpu::ColorWrites::ALL,
                    })],
                    compilation_options: wgpu::PipelineCompilationOptions::default(),
                }),
                primitive: wgpu::PrimitiveState {
                    topology: wgpu::PrimitiveTopology::TriangleList,
                    front_face: wgpu::FrontFace::Ccw,
                    cull_mode: Some(wgpu::Face::Back),
                    ..Default::default()
                },
                depth_stencil: Some(wgpu::DepthStencilState {
                    format: DEPTH_FORMAT,
                    depth_write_enabled,
                    depth_compare,
                    stencil: wgpu::StencilState::default(),
                    bias: wgpu::DepthBiasState::default(),
                }),
                multisample: wgpu::MultisampleState::default(),
                multiview: None,
                cache: None,
            })
        };

        let pipeline = build_pipeline("tile_pipeline", true, wgpu::CompareFunction::Less);
        let translucent_pipeline = build_pipeline(
            "tile_pipeline_translucent",
            false,
            wgpu::CompareFunction::LessEqual,
        );

        Self {
            pipeline,
            translucent_pipeline,
            uniform_bind_group_layout,
            texture_bind_group_layout,
        }
    }
}