nightshade 0.14.1

A cross-platform data-oriented game engine.
Documentation
//! wgpu rendering backend.
//!
//! The [`WgpuRenderer`] supports DirectX 12, Metal, Vulkan, and WebGPU backends.
//!
//! Key types:
//!
//! - [`WgpuRenderer`]: Main renderer managing the GPU device, surface, and render graph
//! - [`CameraViewport`]: Off-screen render target for camera output
//!
//! Submodules:
//!
//! - [`rendergraph`]: Declarative pass-based render graph with automatic resource management
//! - [`passes`]: Built-in geometry, shadow, and post-processing passes
//! - [`texture_cache`]: GPU texture caching and lifecycle management
//! - [`glyph_atlas`]: dynamic glyph atlas backed by cosmic-text + swash

pub mod brdf_lut;
mod camera_viewport;
#[cfg(feature = "assets")]
pub mod envmap_filter;
mod execution;
mod frame;
pub mod glyph_atlas;
#[cfg(feature = "assets")]
pub mod hdr;
mod initialization;
pub mod material_texture_arrays;
pub mod mip_generator;
pub mod passes;
pub mod rendergraph;
pub mod shader_compose;
pub mod text_mesh;
pub mod texture_cache;
pub mod ui_texture_array;

#[derive(Default, Clone)]
pub struct WindowRenderState {
    /// Every distinct buffer size this window has dispatched at this
    /// frame (or recently). The cache lets multi-camera-per-window
    /// dispatches early-return when the shared graph is already at the
    /// requested size for *any* of this window's cameras, instead of
    /// always falling through to the per-pass texture pool.
    pub recent_buffer_sizes: Vec<(u32, u32)>,
    pub last_settings_version: Option<u64>,
}

const RECENT_BUFFER_SIZE_CAPACITY: usize = 8;

pub struct CameraViewport {
    pub texture: wgpu::Texture,
    pub view: wgpu::TextureView,
    pub size: (u32, u32),
    pub has_rendered_at_least_once: bool,
    pub last_active_view: Option<crate::ecs::camera::components::EffectiveShading>,
    pub last_settings_version: u64,
    pub last_render_frame: u64,
    pub last_camera_world_transform: Option<nalgebra_glm::Mat4>,
}

const DEPTH_PICK_SAMPLE_SIZE: u32 = 5;

pub struct WgpuRenderer {
    surface: wgpu::Surface<'static>,
    device: wgpu::Device,
    queue: wgpu::Queue,
    surface_config: wgpu::SurfaceConfiguration,
    surface_format: wgpu::TextureFormat,
    graph: rendergraph::RenderGraph<crate::ecs::world::World>,
    depth_id: rendergraph::ResourceId,
    scene_color_id: rendergraph::ResourceId,
    compute_output_id: rendergraph::ResourceId,
    fxaa_output_id: rendergraph::ResourceId,
    swapchain_id: rendergraph::ResourceId,
    viewport_resource_id: rendergraph::ResourceId,
    ui_depth_id: rendergraph::ResourceId,
    entity_id_id: rendergraph::ResourceId,
    view_normals_id: rendergraph::ResourceId,
    ssao_raw_id: rendergraph::ResourceId,
    ssao_id: rendergraph::ResourceId,
    ssgi_raw_id: rendergraph::ResourceId,
    ssgi_id: rendergraph::ResourceId,
    ssr_raw_id: rendergraph::ResourceId,
    ssr_id: rendergraph::ResourceId,
    ui_image_pass: Option<Box<passes::UiImagePass>>,
    pub(super) glyph_atlas: glyph_atlas::GlyphAtlas,
    glyph_atlas_initialized: bool,
    text_mesh_signatures: std::collections::HashMap<freecs::Entity, u64>,
    /// Per-camera viewport texture pool.
    camera_viewports: std::collections::HashMap<freecs::Entity, CameraViewport>,
    _brdf_lut_texture: wgpu::Texture,
    brdf_lut_view: wgpu::TextureView,
    material_texture_arrays: material_texture_arrays::MaterialTextureArrays,
    pub(super) ui_texture_array: ui_texture_array::UiTextureArray,
    mip_generator: mip_generator::MipGenerator,
    depth_pick_compute_pipeline: wgpu::ComputePipeline,
    depth_pick_bind_group_layout: wgpu::BindGroupLayout,
    depth_pick_storage_buffer: wgpu::Buffer,
    depth_pick_uniform_buffer: wgpu::Buffer,
    depth_pick_staging_buffer: wgpu::Buffer,
    depth_pick_bind_group: Option<wgpu::BindGroup>,
    depth_pick_pending: bool,
    depth_pick_map_complete: std::sync::Arc<std::sync::atomic::AtomicBool>,
    depth_pick_center: (u32, u32),
    depth_pick_texture_size: (u32, u32),
    depth_pick_camera: Option<freecs::Entity>,
    #[cfg(not(target_arch = "wasm32"))]
    screenshot_staging_buffer: wgpu::Buffer,
    #[cfg(not(target_arch = "wasm32"))]
    screenshot_pending: bool,
    #[cfg(not(target_arch = "wasm32"))]
    screenshot_map_complete: std::sync::Arc<std::sync::atomic::AtomicBool>,
    #[cfg(not(target_arch = "wasm32"))]
    screenshot_path: Option<std::path::PathBuf>,
    #[cfg(not(target_arch = "wasm32"))]
    screenshot_width: u32,
    #[cfg(not(target_arch = "wasm32"))]
    screenshot_height: u32,
    #[cfg(not(target_arch = "wasm32"))]
    screenshot_max_dimension: Option<u32>,
    render_buffer_size: (u32, u32),
    /// Render-graph state. Tracks the last buffer size used by the
    /// renderer's dispatch so consecutive dispatches don't
    /// pessimistically resize when the size is already current.
    window_render_state: WindowRenderState,
    last_settings_signature: Option<u64>,
    frame_index: u64,
    text_meshes_prepared_for_frame: u64,
    extract_dirty_done_for_frame: u64,
}

pub async fn create_wgpu_renderer<W>(
    window_handle: W,
    initial_width: u32,
    initial_height: u32,
) -> Result<WgpuRenderer, Box<dyn std::error::Error>>
where
    W: Into<wgpu::SurfaceTarget<'static>>,
{
    WgpuRenderer::new_async(window_handle, initial_width, initial_height).await
}