Skip to main content

dreamwell_gpu/
lib.rs

1// Dreamwell GPU v1.0.0 — shared GPU infrastructure for editor and runtime.
2//
3// Provides: handle-based resource stores, fixed render pass chain,
4// meshlet/cull/matter pipeline, particles, sprites, text, lighting,
5// procedural generation, and animation.
6//
7// CRITICAL: No per-frame buffer creation. All GPU buffers allocated at
8// init/scene load. Per-frame work is write_buffer() only.
9
10// ── Core types ──────────────────────────────────────────────────────────
11pub mod handles;
12pub mod prelude;
13
14// ── Resource stores ─────────────────────────────────────────────────────
15pub mod stores;
16
17// ── Scene & rendering ───────────────────────────────────────────────────
18pub mod camera;
19pub mod lights;
20pub mod passes;
21pub mod render_scene;
22pub mod render_target;
23
24// ── GPU infrastructure ──────────────────────────────────────────────────
25pub mod animation;
26pub mod bind_pool;
27pub mod compute;
28pub mod csm;
29pub mod dof;
30pub mod dream_animations;
31pub mod dreamlet_catalog;
32pub mod features;
33pub mod formats;
34pub mod frame_arena;
35pub mod gpu_driven;
36pub mod hiz;
37pub mod ibl;
38pub mod lod;
39pub mod micro_dreamlet;
40pub mod motion_vectors;
41pub mod pipeline;
42pub mod pipeline_cache;
43pub mod post;
44pub mod primitives;
45pub mod ring;
46pub mod shader;
47pub mod ssao;
48pub mod ssgi;
49pub mod ssr;
50pub mod taa;
51pub mod tangent;
52pub mod texture;
53pub mod texture_cache;
54pub mod upload;
55pub mod volumetric;
56pub mod vram;
57
58// ── Dreamwell-specific GPU systems ──────────────────────────────────────
59pub mod avatar;
60pub mod dreammatter;
61pub mod materialize;
62pub mod meshlet;
63pub mod meshlet_render;
64pub mod observer;
65pub mod quantum_bridge;
66pub mod quantum_cull;
67pub mod spatial_event;
68
69// ── Content import ──────────────────────────────────────────────────────
70pub mod gltf;
71pub mod p3g;
72
73// ── Presentation systems ────────────────────────────────────────────────
74pub mod dream_procedures;
75pub mod particle;
76pub mod physics;
77pub mod picking;
78pub mod procedural;
79pub mod scene;
80pub mod skybox;
81pub mod sprite;
82pub mod text;
83pub mod tween;
84
85// ── Advanced GPU (always compiled, runtime-checked for support) ─────────
86pub mod cooperative_matrix;
87pub mod debug_vis;
88pub mod mesh_shader;
89pub mod ray_tracing;
90pub mod rt_denoise;
91pub mod rt_gi;
92pub mod rt_shadows;
93
94// ── Dream Lighting (DL-01 through DL-16) ───────────────────────────────
95pub mod dream_surface;
96pub mod dream_tsr;
97pub mod sdf;
98pub mod surface_cache;
99pub mod translucency_volume;
100
101// ── Re-exports ──────────────────────────────────────────────────────────
102pub use handles::*;
103
104/// GPU runtime — shared device, queue, and capability contexts.
105/// Both editor and runtime create one of these at startup.
106pub struct GpuRuntime {
107    pub device: wgpu::Device,
108    pub queue: wgpu::Queue,
109    pub capabilities: features::GpuCapabilities,
110    pub ray_tracing: ray_tracing::RayTracingContext,
111    pub mesh_shader: mesh_shader::MeshShaderContext,
112    pub coop_matrix: cooperative_matrix::CooperativeMatrixContext,
113}
114
115impl GpuRuntime {
116    /// Create a GpuRuntime from an existing device, queue, and adapter.
117    pub fn new(device: wgpu::Device, queue: wgpu::Queue, adapter: &wgpu::Adapter) -> Self {
118        let capabilities = features::GpuCapabilities::from_adapter(adapter);
119        let device_features = device.features();
120        let device_limits = device.limits();
121
122        let rt_ctx = ray_tracing::RayTracingContext::init_if_supported(&device, device_features);
123        let mesh_ctx = mesh_shader::MeshShaderContext::init_if_supported(device_features, &device_limits);
124        let coop_ctx = cooperative_matrix::CooperativeMatrixContext::init_if_supported(adapter, device_features);
125
126        log::info!("GPU: {}", capabilities.summary());
127        if rt_ctx.enabled {
128            log::info!("  Ray tracing: enabled");
129        }
130        if mesh_ctx.enabled {
131            log::info!("  Mesh shaders: enabled (max layers={})", mesh_ctx.max_output_layers);
132        }
133        if coop_ctx.enabled {
134            log::info!("  {}", coop_ctx.summary());
135        }
136
137        // Set device error callback to route to structured logging instead of panic.
138        device.on_uncaptured_error(std::sync::Arc::new(|error| {
139            log::error!("gpu_device_error:{error}");
140        }));
141
142        Self {
143            device,
144            queue,
145            capabilities,
146            ray_tracing: rt_ctx,
147            mesh_shader: mesh_ctx,
148            coop_matrix: coop_ctx,
149        }
150    }
151
152    /// Query GPU capabilities.
153    pub fn capabilities(&self) -> &features::GpuCapabilities {
154        &self.capabilities
155    }
156
157    /// Surface texture format for the current adapter.
158    pub fn surface_format(&self) -> wgpu::TextureFormat {
159        formats::TEXTURE_FORMAT
160    }
161
162    /// Handle surface resize. Called by runtime/editor when window resizes.
163    /// Note: actual surface reconfiguration is handled by the caller (winit).
164    /// This method updates internal state that depends on viewport dimensions.
165    pub fn resize(&mut self, _width: u32, _height: u32) {
166        // Currently no internal state depends on viewport size.
167        // GpuRuntime manages device/queue, not surfaces.
168        // Surface reconfiguration is the caller's responsibility.
169    }
170}