M3: STL/OBJ import with mesh validation, mass/COM/inertia, material database, RON scene with content-addressed blobs and v0->v1 migration. M4: deterministic heightmap terrain, trimesh colliders, wgpu renderer with instancing and depth, winit app with 1 kHz physics, fly camera, on-screen metrics overlay and per-frame CSV/dev logs. Docs: UI, logging, network and headless CLI plans.
100 lines
2.8 KiB
Rust
100 lines
2.8 KiB
Rust
//! Headless GPU device creation.
|
|
//!
|
|
//! The window path lives in the application: it owns the surface and passes the
|
|
//! device and queue to [`crate::Renderer`].
|
|
|
|
use std::future::Future;
|
|
use std::sync::Arc;
|
|
use std::task::{Context, Poll, Wake, Waker};
|
|
use std::thread::{self, Thread};
|
|
|
|
use crate::RenderError;
|
|
|
|
/// A logical device and its queue, created without a window surface.
|
|
#[derive(Debug, Clone)]
|
|
pub struct Gpu {
|
|
device: wgpu::Device,
|
|
queue: wgpu::Queue,
|
|
}
|
|
|
|
impl Gpu {
|
|
/// Requests a high-performance adapter, retrying with a fallback adapter.
|
|
pub fn headless() -> Result<Self, RenderError> {
|
|
let instance = wgpu::Instance::new(wgpu::InstanceDescriptor::new_without_display_handle());
|
|
let adapter = request_adapter(&instance)?;
|
|
let (device, queue) = block_on(adapter.request_device(&device_descriptor()))
|
|
.map_err(|error| RenderError::Device(error.to_string()))?;
|
|
Ok(Self { device, queue })
|
|
}
|
|
|
|
/// The device that owns renderer resources.
|
|
#[must_use]
|
|
pub fn device(&self) -> &wgpu::Device {
|
|
&self.device
|
|
}
|
|
|
|
/// The queue used to upload data and submit commands.
|
|
#[must_use]
|
|
pub fn queue(&self) -> &wgpu::Queue {
|
|
&self.queue
|
|
}
|
|
}
|
|
|
|
/// Device limits and features used by both the app and headless tests.
|
|
#[must_use]
|
|
pub fn device_descriptor() -> wgpu::DeviceDescriptor<'static> {
|
|
wgpu::DeviceDescriptor {
|
|
label: Some("fabrilis device"),
|
|
required_features: wgpu::Features::empty(),
|
|
required_limits: wgpu::Limits::default(),
|
|
experimental_features: wgpu::ExperimentalFeatures::default(),
|
|
memory_hints: wgpu::MemoryHints::default(),
|
|
trace: wgpu::Trace::Off,
|
|
}
|
|
}
|
|
|
|
fn request_adapter(instance: &wgpu::Instance) -> Result<wgpu::Adapter, RenderError> {
|
|
let options = wgpu::RequestAdapterOptions {
|
|
power_preference: wgpu::PowerPreference::HighPerformance,
|
|
compatible_surface: None,
|
|
force_fallback_adapter: false,
|
|
..Default::default()
|
|
};
|
|
match block_on(instance.request_adapter(&options)) {
|
|
Ok(adapter) => Ok(adapter),
|
|
Err(primary) => {
|
|
let fallback = wgpu::RequestAdapterOptions {
|
|
force_fallback_adapter: true,
|
|
..options
|
|
};
|
|
block_on(instance.request_adapter(&fallback)).map_err(|secondary| {
|
|
RenderError::Adapter(format!("{primary}; fallback adapter: {secondary}"))
|
|
})
|
|
}
|
|
}
|
|
}
|
|
|
|
struct ThreadWake(Thread);
|
|
|
|
impl Wake for ThreadWake {
|
|
fn wake(self: Arc<Self>) {
|
|
self.0.unpark();
|
|
}
|
|
|
|
fn wake_by_ref(self: &Arc<Self>) {
|
|
self.0.unpark();
|
|
}
|
|
}
|
|
|
|
/// Blocks on a future without pulling in an async runtime.
|
|
fn block_on<F: Future>(future: F) -> F::Output {
|
|
let waker = Waker::from(Arc::new(ThreadWake(thread::current())));
|
|
let mut context = Context::from_waker(&waker);
|
|
let mut future = std::pin::pin!(future);
|
|
loop {
|
|
match future.as_mut().poll(&mut context) {
|
|
Poll::Ready(value) => return value,
|
|
Poll::Pending => thread::park(),
|
|
}
|
|
}
|
|
}
|