// Dreamwell GPU v1.0.0 — shared GPU infrastructure for editor and runtime.
//
// Provides: handle-based resource stores, fixed render pass chain,
// meshlet/cull/matter pipeline, particles, sprites, text, lighting,
// procedural generation, and animation.
//
// CRITICAL: No per-frame buffer creation. All GPU buffers allocated at
// init/scene load. Per-frame work is write_buffer() only.

// ── Core types ──────────────────────────────────────────────────────────
pub mod handles;
pub mod prelude;

// ── Resource stores ─────────────────────────────────────────────────────
pub mod stores;

// ── Scene & rendering ───────────────────────────────────────────────────
pub mod camera;
pub mod lights;
pub mod passes;
pub mod render_scene;
pub mod render_target;

// ── GPU infrastructure ──────────────────────────────────────────────────
pub mod animation;
pub mod bind_pool;
pub mod compute;
pub mod csm;
pub mod dof;
pub mod dream_animations;
pub mod dreamlet_catalog;
pub mod features;
pub mod formats;
pub mod frame_arena;
pub mod gpu_driven;
pub mod hiz;
pub mod ibl;
pub mod lod;
pub mod micro_dreamlet;
pub mod motion_vectors;
pub mod pipeline;
pub mod pipeline_cache;
pub mod post;
pub mod primitives;
pub mod ring;
pub mod shader;
pub mod ssao;
pub mod ssgi;
pub mod ssr;
pub mod taa;
pub mod tangent;
pub mod texture;
pub mod texture_cache;
pub mod upload;
pub mod volumetric;
pub mod vram;

// ── Dreamwell-specific GPU systems ──────────────────────────────────────
pub mod avatar;
pub mod dreammatter;
pub mod materialize;
pub mod meshlet;
pub mod meshlet_render;
pub mod observer;
pub mod quantum_bridge;
pub mod quantum_cull;
pub mod spatial_event;

// ── Content import ──────────────────────────────────────────────────────
pub mod gltf;
pub mod p3g;

// ── Presentation systems ────────────────────────────────────────────────
pub mod dream_procedures;
pub mod particle;
pub mod physics;
pub mod picking;
pub mod procedural;
pub mod scene;
pub mod skybox;
pub mod sprite;
pub mod text;
pub mod tween;

// ── Advanced GPU (always compiled, runtime-checked for support) ─────────
pub mod cooperative_matrix;
pub mod debug_vis;
pub mod mesh_shader;
pub mod ray_tracing;
pub mod rt_denoise;
pub mod rt_gi;
pub mod rt_shadows;

// ── Dream Lighting (DL-01 through DL-16) ───────────────────────────────
pub mod dream_surface;
pub mod dream_tsr;
pub mod sdf;
pub mod surface_cache;
pub mod translucency_volume;

// ── Re-exports ──────────────────────────────────────────────────────────
pub use handles::*;

/// GPU runtime — shared device, queue, and capability contexts.
/// Both editor and runtime create one of these at startup.
pub struct GpuRuntime {
    pub device: wgpu::Device,
    pub queue: wgpu::Queue,
    pub capabilities: features::GpuCapabilities,
    pub ray_tracing: ray_tracing::RayTracingContext,
    pub mesh_shader: mesh_shader::MeshShaderContext,
    pub coop_matrix: cooperative_matrix::CooperativeMatrixContext,
}

impl GpuRuntime {
    /// Create a GpuRuntime from an existing device, queue, and adapter.
    pub fn new(device: wgpu::Device, queue: wgpu::Queue, adapter: &wgpu::Adapter) -> Self {
        let capabilities = features::GpuCapabilities::from_adapter(adapter);
        let device_features = device.features();
        let device_limits = device.limits();

        let rt_ctx = ray_tracing::RayTracingContext::init_if_supported(&device, device_features);
        let mesh_ctx = mesh_shader::MeshShaderContext::init_if_supported(device_features, &device_limits);
        let coop_ctx = cooperative_matrix::CooperativeMatrixContext::init_if_supported(adapter, device_features);

        log::info!("GPU: {}", capabilities.summary());
        if rt_ctx.enabled {
            log::info!("  Ray tracing: enabled");
        }
        if mesh_ctx.enabled {
            log::info!("  Mesh shaders: enabled (max layers={})", mesh_ctx.max_output_layers);
        }
        if coop_ctx.enabled {
            log::info!("  {}", coop_ctx.summary());
        }

        // Set device error callback to route to structured logging instead of panic.
        device.on_uncaptured_error(std::sync::Arc::new(|error| {
            log::error!("gpu_device_error:{error}");
        }));

        Self {
            device,
            queue,
            capabilities,
            ray_tracing: rt_ctx,
            mesh_shader: mesh_ctx,
            coop_matrix: coop_ctx,
        }
    }

    /// Query GPU capabilities.
    pub fn capabilities(&self) -> &features::GpuCapabilities {
        &self.capabilities
    }

    /// Surface texture format for the current adapter.
    pub fn surface_format(&self) -> wgpu::TextureFormat {
        formats::TEXTURE_FORMAT
    }

    /// Handle surface resize. Called by runtime/editor when window resizes.
    /// Note: actual surface reconfiguration is handled by the caller (winit).
    /// This method updates internal state that depends on viewport dimensions.
    pub fn resize(&mut self, _width: u32, _height: u32) {
        // Currently no internal state depends on viewport size.
        // GpuRuntime manages device/queue, not surfaces.
        // Surface reconfiguration is the caller's responsibility.
    }
}
