rustial-renderer-wgpu 0.0.1

Pure WGPU renderer for the rustial 2.5D map engine
Documentation
//! Heatmap render pipeline with Gaussian accumulation.
//!
//! The heatmap is rendered in two passes:
//!
//! 1. **Accumulation pass**: Each heatmap point is rendered as a quad
//!    into an off-screen R16Float texture.  The fragment shader outputs
//!    Gaussian-weighted values with additive blending.
//!
//! 2. **Colour-mapping pass**: A fullscreen triangle samples the
//!    accumulated weight texture and maps it through a colour ramp.
//!    This pass is handled by a separate pipeline (see
//!    `heatmap_colormap_pipeline`).
//!
//! This module implements the accumulation pass (Pass 1).

use crate::gpu::depth::DEPTH_FORMAT;
use crate::gpu::heatmap_vertex::HeatmapVertex;
use crate::pipeline::uniforms::ViewProjUniform;

/// The heatmap accumulation pipeline (Pass 1).
pub struct HeatmapPipeline {
    /// Render pipeline for Gaussian accumulation.
    pub pipeline: wgpu::RenderPipeline,
    /// Bind group layout for frame-global view-projection uniforms.
    pub uniform_bind_group_layout: wgpu::BindGroupLayout,
}

impl HeatmapPipeline {
    /// Create the heatmap accumulation pipeline.
    ///
    /// The target format is `R16Float` for the off-screen accumulation
    /// texture.  Blending is additive.
    pub fn new(device: &wgpu::Device) -> Self {
        let shader = device.create_shader_module(wgpu::ShaderModuleDescriptor {
            label: Some("heatmap_shader"),
            source: wgpu::ShaderSource::Wgsl(include_str!("../shaders/heatmap.wgsl").into()),
        });

        let uniform_bind_group_layout =
            device.create_bind_group_layout(&wgpu::BindGroupLayoutDescriptor {
                label: Some("heatmap_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,
                }],
            });

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

        // Additive blend for accumulation: src + dst.
        let additive_blend = wgpu::BlendState {
            color: wgpu::BlendComponent {
                src_factor: wgpu::BlendFactor::One,
                dst_factor: wgpu::BlendFactor::One,
                operation: wgpu::BlendOperation::Add,
            },
            alpha: wgpu::BlendComponent {
                src_factor: wgpu::BlendFactor::One,
                dst_factor: wgpu::BlendFactor::One,
                operation: wgpu::BlendOperation::Add,
            },
        };

        let pipeline = device.create_render_pipeline(&wgpu::RenderPipelineDescriptor {
            label: Some("heatmap_pipeline"),
            layout: Some(&pipeline_layout),
            vertex: wgpu::VertexState {
                module: &shader,
                entry_point: Some("vs_main"),
                buffers: &[HeatmapVertex::layout()],
                compilation_options: wgpu::PipelineCompilationOptions::default(),
            },
            fragment: Some(wgpu::FragmentState {
                module: &shader,
                entry_point: Some("fs_main"),
                targets: &[Some(wgpu::ColorTargetState {
                    format: wgpu::TextureFormat::R16Float,
                    blend: Some(additive_blend),
                    write_mask: wgpu::ColorWrites::RED,
                })],
                compilation_options: wgpu::PipelineCompilationOptions::default(),
            }),
            primitive: wgpu::PrimitiveState {
                topology: wgpu::PrimitiveTopology::TriangleList,
                front_face: wgpu::FrontFace::Ccw,
                cull_mode: None,
                ..Default::default()
            },
            depth_stencil: Some(wgpu::DepthStencilState {
                format: DEPTH_FORMAT,
                depth_write_enabled: false,
                depth_compare: wgpu::CompareFunction::Always,
                stencil: wgpu::StencilState::default(),
                bias: wgpu::DepthBiasState::default(),
            }),
            multisample: wgpu::MultisampleState::default(),
            multiview: None,
            cache: None,
        });

        Self {
            pipeline,
            uniform_bind_group_layout,
        }
    }
}