facett-core 0.1.7

facett — visual kernel: render a node/edge Scene into egui (wgpu fast path to come)
Documentation
//! **The L0 GPU lane** (feature `wgpu`) — the extracted shared scaffold the three
//! skin kernels (facett-map `GpuMapRenderer`, facett-map3d `Map3dRenderer`,
//! facett-graph3d `LogoRenderer`) will be re-pointed at in Phase C.
//!
//! All three share the identical `egui_wgpu::CallbackTrait` plumbing: a `*Renderer`
//! stored in `callback_resources`, **installed once** via an `install_renderer`,
//! driven by a paint callback. Phase A extracts:
//!
//! - [`types`] — the bytemuck `Vertex`/`Uniform`/`DrawIndirectArgs` structs +
//!   `color32_to_f32` (byte-identical to the skins' copies).
//! - [`buffer`] — the upload-once + **chunking** algorithm (fix #12), verbatim.
//! - [`install_renderer`] — the install-once lifecycle each kernel hand-rolls.
//! - [`CULL_WGSL`] / [`DRAW_WGSL`] — the cull + draw shaders, **byte-identical** to
//!   `facett-map/src/gpu/*.wgsl` (do not change their semantics).
//!
//! Nothing in the skins consumes this yet — it is the scaffold for Phase B/C.

pub mod buffer;
pub mod offscreen;
pub mod sdf_pipeline;
pub mod types;

pub use buffer::{ChunkSpan, chunk_budget, partition_chunks, rebase_chunk_meta};
pub use offscreen::{
    gpu_scissor_px, scissor_intersection, OffscreenColorDepth, ScissorPx, BLIT_WGSL, DEPTH_FORMAT,
    OFFSCREEN_FORMAT,
};
pub use sdf_pipeline::{
    GpuSdfHeadless, GpuSdfRenderer, SdfUniforms, offscreen_render, LINE_WGSL, SDF_WGSL,
};
pub use types::{
    DrawIndirectArgs, DrawUniforms, GpuWayMeta, Vertex, ViewportCull, color32_to_f32,
};

/// The GPU frustum-cull + LOD + stream-compaction compute shader, **byte-identical**
/// to `facett-map/src/gpu/cull.wgsl` (the country-scale chunked pipeline). The
/// chunking partition in [`buffer`] keeps this per-way shader unchanged.
pub const CULL_WGSL: &str = include_str!("cull.wgsl");

/// The line-draw vertex/fragment shader, **byte-identical** to
/// `facett-map/src/gpu/draw.wgsl`.
pub const DRAW_WGSL: &str = include_str!("draw.wgsl");

/// **Install-once** lifecycle, generic over the renderer type — the pattern all
/// three skin kernels hand-roll (`install_renderer` in facett-map / facett-map3d /
/// facett-graph3d). If a renderer of type `R` is already in the egui-wgpu
/// `RenderState`'s `callback_resources`, this is a no-op; otherwise `make` builds
/// one (from the device + target format) and inserts it. Hosts call this once at
/// startup so the paint callback can `resources.get::<R>()`.
///
/// Returns `true` if a renderer was newly installed, `false` if one already existed.
pub fn install_renderer<R, F>(render_state: &egui_wgpu::RenderState, make: F) -> bool
where
    R: 'static + Send + Sync,
    F: FnOnce(&wgpu::Device, wgpu::TextureFormat) -> R,
{
    if render_state.renderer.read().callback_resources.get::<R>().is_some() {
        return false; // already installed
    }
    let renderer = make(&render_state.device, render_state.target_format);
    render_state.renderer.write().callback_resources.insert(renderer);
    true
}

#[cfg(test)]
mod tests {
    use super::*;

    /// INJECT-ASSERT: the extracted shaders are byte-identical to facett-map's (the
    /// chunking move must not change shader semantics — fix #12 is load-bearing).
    #[test]
    fn shaders_are_byte_identical_to_facett_map() {
        let map_cull = include_str!("../../../../facett-map/src/gpu/cull.wgsl");
        let map_draw = include_str!("../../../../facett-map/src/gpu/draw.wgsl");
        assert_eq!(CULL_WGSL, map_cull, "cull.wgsl byte-identical to facett-map");
        assert_eq!(DRAW_WGSL, map_draw, "draw.wgsl byte-identical to facett-map");
        // The cull shader's per-way contract (6 u32 words/vertex) is unchanged.
        assert!(CULL_WGSL.contains("const VPV: u32 = 6u;"));
        assert!(CULL_WGSL.contains("@workgroup_size(64)"));
    }
}