roxlap_render/lib.rs
1//! roxlap-render — unified CPU/GPU renderer facade.
2//!
3//! One [`SceneRenderer`] hides the choice between the CPU opticast
4//! path (`roxlap-core` / `roxlap-scene`, presented via `softbuffer`)
5//! and the GPU compute-shader path (`roxlap-gpu`, presented via its
6//! own wgpu surface). Construction picks the GPU backend when asked
7//! and able, and **falls back to CPU automatically** when WGPU init
8//! fails — so a host never has to branch on GPU availability or carry
9//! the `Scene`→GPU upload/refresh/transform glue itself.
10//!
11//! Hosts stay thin: build a `Scene`, advance it from input, then call
12//! [`SceneRenderer::render`] each frame. The facade owns the window
13//! surface, the framebuffer/z-buffer (CPU) or the resident scene +
14//! dirty-chunk tracking (GPU), and presentation.
15//!
16//! The per-frame flow is `render` → *(optional overlays)* → finish.
17//! Between [`SceneRenderer::render`] and the finishing
18//! [`SceneRenderer::present`] / [`SceneRenderer::paint_egui`] call, a
19//! host may overlay depth-tested world-space lines with
20//! [`SceneRenderer::draw_lines`] (editor gizmos, debug geometry — see
21//! [`Line3`]); they land in the framebuffer, occluded by the rendered
22//! scene, with egui still painting panels on top.
23//!
24//! This is the RF.0 skeleton: backend selection + fallback + a
25//! clear-to-sky frame. RF.1/RF.2 fill in the real CPU/GPU scene
26//! render; RF.3 adds sprites; RF.4 adds framebuffer capture.
27
28#![forbid(unsafe_code)]
29
30mod cpu;
31/// WebGL2 framebuffer presenter for the CPU backend on wasm (the
32/// browser has no `softbuffer`).
33#[cfg(target_arch = "wasm32")]
34mod cpu_blit;
35#[cfg(feature = "hud")]
36mod cpu_egui;
37mod gpu;
38
39#[cfg(not(target_arch = "wasm32"))]
40use std::sync::Arc;
41
42use roxlap_core::opticast::OpticastSettings;
43use roxlap_core::sky::Sky;
44use roxlap_core::sprite::SpriteLighting;
45use roxlap_core::Camera;
46use roxlap_scene::Scene;
47
48pub use roxlap_formats::kfa::KfaSprite;
49pub use roxlap_formats::sprite::Sprite;
50pub use roxlap_gpu::{GpuInitError, GpuRendererSettings, PowerPreference};
51// Re-exported so hosts can name the [`SceneRenderer::new`] bounds
52// without adding a direct `raw-window-handle` dependency of their own.
53pub use raw_window_handle::{HasDisplayHandle, HasWindowHandle};
54// Re-exported so hosts feed [`SceneRenderer::paint_egui`] from the exact
55// egui version the renderer was built against (`hud` feature).
56#[cfg(feature = "hud")]
57pub use egui;
58
59use crate::cpu::CpuBackend;
60use crate::gpu::GpuBackend;
61
62/// Type-erased display handle stored by the CPU backend's softbuffer
63/// surface. `raw-window-handle` implements `HasDisplayHandle` for
64/// `Arc<H>` (`H: ?Sized`), and the bare trait object implements its
65/// own object-safe trait — so `Arc<W>` coerces to `Arc<DynDisplay>`
66/// for any provider `W`.
67#[cfg(not(target_arch = "wasm32"))]
68pub(crate) type DynDisplay = dyn HasDisplayHandle + Send + Sync + 'static;
69/// Type-erased window handle counterpart to [`DynDisplay`].
70#[cfg(not(target_arch = "wasm32"))]
71pub(crate) type DynWindow = dyn HasWindowHandle + Send + Sync + 'static;
72
73/// One placed sprite instance: which [`SpriteSet::models`] entry and
74/// where in the world.
75pub struct SpriteInstanceDesc {
76 pub model: usize,
77 pub pos: [f32; 3],
78}
79
80/// Backend-agnostic sprite description. The facade builds the CPU
81/// per-instance draw list and the GPU instanced registry from the
82/// same data, so both backends show identical sprites. The host owns
83/// content (which models, where, recolouring) — building a recoloured
84/// variant is just a second [`Sprite`] model with edited `kv6.voxels`.
85pub struct SpriteSet {
86 /// Distinct voxel models (KV6 + base orientation). Instances index
87 /// into this; their position overrides the model's.
88 pub models: Vec<Sprite>,
89 pub instances: Vec<SpriteInstanceDesc>,
90 /// Model the [`SceneRenderer::carve_active_sprite`] hotkey edits
91 /// (GPU only, mirroring the demo's `G`-carve). `None` disables it.
92 pub carve_model: Option<usize>,
93}
94
95/// Per-frame inputs both backends consume. The host builds the
96/// [`OpticastSettings`] (it owns scan distance etc.); the facade does
97/// everything else (pool config, sky fill, render, present).
98pub struct FrameParams<'a> {
99 /// CPU opticast settings (scan distance, mip ladder, framebuffer
100 /// geometry). Ignored by the GPU backend.
101 pub settings: &'a OpticastSettings,
102 /// Packed engine sky colour: the CPU sky-miss fill + skycast, and
103 /// the clear colour if no scene renders.
104 pub sky_color: u32,
105 /// Optional sky panorama for the CPU rasterizer's sky sampling.
106 pub sky: Option<&'a Sky>,
107 /// CPU fog: packed colour + max scan distance (voxels). `0` scan
108 /// distance disables CPU fog.
109 pub fog_color: u32,
110 pub fog_max_scan_dist: i32,
111 /// CPU: treat z=255 as air (avoids the S1.X bedrock path for
112 /// out-of-bounds cameras).
113 pub treat_z_max_as_air: bool,
114 /// GPU scene-grid LOD scan distance (world units); see GPU.11.1.
115 /// Ignored by the CPU backend.
116 pub gpu_mip_scan_dist: f32,
117 /// GPU outer-DDA step budget (chunks). Ignored by the CPU backend.
118 pub gpu_max_outer_steps: u32,
119 /// GPU vertical field of view (radians). Ignored by the CPU
120 /// backend (it derives projection from [`OpticastSettings`]).
121 pub gpu_fov_y_rad: f32,
122 /// CPU sprite shading (built by the host from its engine). Required
123 /// for the CPU backend to draw sprites; ignored by the GPU backend
124 /// (its sprite pass shades from the uploaded model colours). `None`
125 /// skips CPU sprite drawing.
126 pub sprite_lighting: Option<&'a SpriteLighting<'a>>,
127 /// Per-face directional shading for the voxel grids — voxlap's
128 /// `setsideshades(top, bot, left, right, up, down)`, the grid-scan
129 /// analogue of [`sprite_lighting`](Self::sprite_lighting). Each
130 /// entry darkens the faces pointing that way; the host typically
131 /// passes its engine's `side_shades()`. The default `[0; 6]` keeps
132 /// `sideshademode` off (no per-side shading), so existing hosts and
133 /// the oracle goldens are unaffected. Applied each frame by **both**
134 /// backends: the CPU rasteriser via `gcsub`, and the GPU scene-DDA
135 /// pass by darkening a hit voxel's brightness by the hit face's
136 /// shade (the face taken from the DDA's last-stepped axis).
137 pub side_shades: [i8; 6],
138}
139
140/// Result of [`SceneRenderer::pick`] — a resolved screen→world voxel
141/// hit. `world` is the surface point (`cam.pos + t · normalize(ray)`);
142/// `grid` + `voxel` are the owning grid and its **grid-local** voxel
143/// (transform-correct for rotated / translated grids).
144#[derive(Clone, Copy, PartialEq, Debug)]
145pub struct PickHit {
146 pub world: [f32; 3],
147 pub grid: roxlap_scene::GridId,
148 pub voxel: glam::IVec3,
149}
150
151/// A world-space view ray: the canonical unproject output of
152/// [`SceneRenderer::view_ray`]. `dir` is unit-length. Feed it straight
153/// to [`roxlap_scene::Scene::raycast`] for depth-free, backend-agnostic
154/// voxel picking (`scene.raycast(ray.origin, ray.dir, max_dist)`), or
155/// intersect it with a plane for tile selection.
156#[derive(Clone, Copy, PartialEq, Debug)]
157pub struct Ray {
158 pub origin: glam::DVec3,
159 pub dir: glam::DVec3,
160}
161
162/// A world-space line segment to draw over a rendered frame via
163/// [`SceneRenderer::draw_lines`] — editor gizmos (bounding boxes, floor
164/// grids, axes, hover wireframes), debug paths, etc.
165#[derive(Clone, Copy, PartialEq, Debug)]
166pub struct Line3 {
167 /// World-space endpoints (voxel units), in the same frame the
168 /// rendered scene + `camera` use.
169 pub a: [f64; 3],
170 pub b: [f64; 3],
171 /// `0xAARRGGBB` — the high byte is an alpha blend factor (`0xFF`
172 /// opaque, `0x00` invisible), the low 24 bits the RGB colour.
173 pub color: u32,
174 /// Screen-space thickness in pixels (`<= 1.0` draws a 1px line).
175 pub width_px: f32,
176 /// `true`: the segment is occluded by nearer rendered geometry
177 /// (depth-tested against the frame's z-buffer). `false`: always on
178 /// top (e.g. a hover highlight that should show through the model).
179 pub depth_test: bool,
180}
181
182/// Which renderer a [`SceneRenderer`] resolved to at construction.
183#[derive(Clone, Copy, PartialEq, Eq, Debug)]
184pub enum Backend {
185 /// `roxlap-core` opticast, presented via `softbuffer`.
186 Cpu,
187 /// `roxlap-gpu` compute marcher, presented via wgpu.
188 Gpu,
189}
190
191/// Construction-time options for [`SceneRenderer::new`].
192pub struct RenderOptions {
193 /// Try the GPU backend first. When `false`, or when GPU init
194 /// fails, the renderer uses the CPU backend.
195 pub want_gpu: bool,
196 /// Settings forwarded to [`roxlap_gpu::GpuRenderer`] when the GPU
197 /// backend is selected.
198 pub gpu: GpuRendererSettings,
199 /// Packed `0x00RRGGBB` (alpha ignored) the empty/clear frame fills
200 /// with until a scene render lands. Also the CPU sky-miss colour
201 /// default if a frame supplies none.
202 pub clear_sky: u32,
203 /// CPU [`ScratchPool`](roxlap_core::rasterizer::ScratchPool) `lastx`
204 /// sizing — the largest combined grid `vsid` the CPU rasterizer
205 /// will see. Pre-sizing keeps later frames allocation-free.
206 pub cpu_max_grid_vsid: u32,
207 /// CPU strip-parallel render thread count (capped to the rayon
208 /// pool). One [`ScratchPool`](roxlap_core::rasterizer::ScratchPool)
209 /// slot per thread.
210 pub cpu_render_threads: usize,
211}
212
213impl Default for RenderOptions {
214 fn default() -> Self {
215 Self {
216 want_gpu: false,
217 gpu: GpuRendererSettings::default(),
218 clear_sky: 0x0099_b3d9,
219 // 32 chunks × CHUNK_SIZE_XY — the scene-demo's widest
220 // combined ground grid.
221 cpu_max_grid_vsid: 32 * roxlap_scene::CHUNK_SIZE_XY,
222 cpu_render_threads: 4,
223 }
224 }
225}
226
227/// Renderer-internal backend; never exposes wgpu or softbuffer types.
228/// The GPU variant owns the whole wgpu device/queue/pipelines, so
229/// it's boxed to keep the enum small.
230enum BackendImpl {
231 // Both variants boxed so the enum stays small regardless of which
232 // backend's state is larger (clippy::large_enum_variant).
233 Cpu(Box<CpuBackend>),
234 Gpu(Box<GpuBackend>),
235}
236
237/// Unified renderer over the CPU and GPU paths. See the crate docs.
238pub struct SceneRenderer {
239 inner: BackendImpl,
240}
241
242impl SceneRenderer {
243 /// Build a renderer for `window` — any [`raw-window-handle`]
244 /// provider (winit, SDL, GLFW, …) in an `Arc`. `size` is the
245 /// window's initial physical framebuffer size in pixels; thereafter
246 /// the host reports changes via [`Self::resize`]. Passing the size
247 /// explicitly keeps the facade decoupled from any one windowing
248 /// library's size API.
249 ///
250 /// Selects the GPU backend when `opts.want_gpu` and WGPU
251 /// initialises; otherwise the CPU backend. **Never fails** — a
252 /// missing/incompatible GPU silently yields the CPU path (the
253 /// message is logged to stderr).
254 ///
255 /// [`raw-window-handle`]: raw_window_handle
256 #[cfg(not(target_arch = "wasm32"))]
257 #[must_use]
258 pub fn new<W>(window: Arc<W>, size: (u32, u32), opts: &RenderOptions) -> Self
259 where
260 W: HasWindowHandle + HasDisplayHandle + Send + Sync + 'static,
261 {
262 if opts.want_gpu {
263 match GpuBackend::new(window.clone(), size, opts) {
264 Ok(g) => {
265 return Self {
266 inner: BackendImpl::Gpu(Box::new(g)),
267 };
268 }
269 Err(e) => {
270 eprintln!(
271 "roxlap-render: GPU init failed ({e}); falling back to the CPU renderer",
272 );
273 }
274 }
275 }
276 Self {
277 inner: BackendImpl::Cpu(Box::new(CpuBackend::new(window, size, opts))),
278 }
279 }
280
281 /// wasm/WebGPU build-time entry: build a renderer over an HTML
282 /// `canvas`. `size` is the canvas's initial framebuffer size in
283 /// pixels; the host reports later changes via [`Self::resize`].
284 ///
285 /// Async because the browser drives wgpu's adapter/device requests
286 /// through its event loop — `await` it inside a
287 /// `wasm_bindgen_futures::spawn_local` task. Selects the GPU
288 /// (WebGPU) backend when `opts.want_gpu` and WebGPU is available;
289 /// otherwise (no WebGPU, or init failed) it falls back to the CPU
290 /// opticast path presented through a WebGL2 blit on the same canvas.
291 /// **Never fails** — the message is logged to the browser console.
292 #[cfg(target_arch = "wasm32")]
293 pub async fn new_from_canvas_async(
294 canvas: web_sys::HtmlCanvasElement,
295 size: (u32, u32),
296 opts: &RenderOptions,
297 ) -> Self {
298 if opts.want_gpu {
299 // `SurfaceTarget::Canvas` moves the canvas into wgpu, so the
300 // GPU attempt gets a clone — the CPU fallback keeps the
301 // original if WebGPU init fails.
302 match GpuBackend::new_async(canvas.clone(), size, opts).await {
303 Ok(g) => {
304 return Self {
305 inner: BackendImpl::Gpu(Box::new(g)),
306 };
307 }
308 Err(e) => {
309 web_sys::console::warn_1(
310 &format!("roxlap-render: WebGPU init failed ({e}); using the CPU renderer")
311 .into(),
312 );
313 }
314 }
315 }
316 Self {
317 inner: BackendImpl::Cpu(Box::new(CpuBackend::new_from_canvas(canvas, size, opts))),
318 }
319 }
320
321 /// Which backend was selected.
322 #[must_use]
323 pub fn backend(&self) -> Backend {
324 match self.inner {
325 BackendImpl::Cpu(_) => Backend::Cpu,
326 BackendImpl::Gpu(_) => Backend::Gpu,
327 }
328 }
329
330 /// The GPU adapter description when on the GPU backend, else
331 /// `None`.
332 #[must_use]
333 pub fn adapter_info(&self) -> Option<&str> {
334 match &self.inner {
335 BackendImpl::Gpu(g) => Some(g.adapter_info()),
336 BackendImpl::Cpu(_) => None,
337 }
338 }
339
340 /// Upload an equirectangular sky panorama (RGBA8, `w×h`) for the
341 /// GPU marcher's sky sampling. No-op on the CPU backend, which
342 /// samples the [`Sky`] passed in each [`FrameParams`] instead.
343 pub fn set_sky_panorama(&mut self, rgba: &[u8], w: u32, h: u32) {
344 if let BackendImpl::Gpu(g) = &mut self.inner {
345 g.set_sky_panorama(rgba, w, h);
346 }
347 }
348
349 /// Follow a window resize. CPU resizes its framebuffer lazily, so
350 /// this only matters to the GPU swapchain — but it's safe to call
351 /// for both.
352 pub fn resize(&mut self, width: u32, height: u32) {
353 match &mut self.inner {
354 BackendImpl::Cpu(c) => c.resize(width, height),
355 BackendImpl::Gpu(g) => g.resize(width, height),
356 }
357 }
358
359 /// Composite `scene` from `camera` with `frame` params into the
360 /// backend's frame buffer — **without presenting**. The CPU backend
361 /// fills sky + runs the opticast compositor into an owned buffer;
362 /// the GPU backend uploads/refreshes the scene, runs the compute
363 /// marcher + sprite pass, and acquires (but does not present) the
364 /// swapchain frame.
365 ///
366 /// Finish the frame with exactly one of [`present`](Self::present)
367 /// (no overlay) or [`paint_egui`](Self::paint_egui) (UI overlay).
368 /// Calling `render` again without finishing drops the pending frame.
369 pub fn render(&mut self, scene: &mut Scene, camera: &Camera, frame: &FrameParams) {
370 match &mut self.inner {
371 BackendImpl::Cpu(c) => c.render(scene, camera, frame),
372 BackendImpl::Gpu(g) => g.render(scene, camera, frame),
373 }
374 }
375
376 /// Draw world-space [`Line3`] segments over the frame
377 /// [`render`](Self::render) composited, using that frame's camera +
378 /// projection + depth buffer. Call **after** [`render`](Self::render)
379 /// and **before** [`present`](Self::present) /
380 /// [`paint_egui`](Self::paint_egui) — the lines land in the
381 /// framebuffer, so a subsequent `paint_egui` still draws its panels
382 /// on top.
383 ///
384 /// `camera` must be the one the last frame rendered with (the
385 /// projection is taken from that frame). Depth-tested segments
386 /// (`Line3::depth_test`) are occluded by nearer rendered geometry;
387 /// always-on-top segments ignore depth. See [`Line3`] for colour /
388 /// width / blend semantics.
389 pub fn draw_lines(&mut self, camera: &Camera, lines: &[Line3]) {
390 match &mut self.inner {
391 BackendImpl::Cpu(c) => c.draw_lines(camera, lines),
392 BackendImpl::Gpu(g) => g.draw_lines(camera, lines),
393 }
394 }
395
396 /// Present the frame [`render`](Self::render) composited, with no UI
397 /// overlay. Pairs with `render`; use [`paint_egui`](Self::paint_egui)
398 /// instead to overlay an egui UI before presenting.
399 pub fn present(&mut self) {
400 match &mut self.inner {
401 BackendImpl::Cpu(c) => c.present(),
402 BackendImpl::Gpu(g) => g.present(),
403 }
404 }
405
406 /// Overlay an egui UI on the frame [`render`](Self::render)
407 /// composited, then present it (`hud` feature). The host runs egui
408 /// itself (e.g. `egui` + `egui-winit`) and passes the tessellated
409 /// `jobs` ([`egui::Context::tessellate`]) and the per-frame
410 /// `textures` delta from [`egui::FullOutput`]; `pixels_per_point` is
411 /// the UI scale (`ctx.pixels_per_point()`).
412 ///
413 /// The GPU backend paints via `egui-wgpu`; the CPU backend
414 /// software-rasterises the tessellation into its framebuffer. Use
415 /// this **instead of** [`present`](Self::present) — both finish the
416 /// frame.
417 #[cfg(feature = "hud")]
418 pub fn paint_egui(
419 &mut self,
420 jobs: &[egui::ClippedPrimitive],
421 textures: &egui::TexturesDelta,
422 pixels_per_point: f32,
423 ) {
424 match &mut self.inner {
425 BackendImpl::Cpu(c) => c.paint_egui(jobs, textures, pixels_per_point),
426 BackendImpl::Gpu(g) => g.paint_egui(jobs, textures, pixels_per_point),
427 }
428 }
429
430 /// Register sprite models + instances. The CPU backend builds a
431 /// per-instance draw list; the GPU backend builds an instanced
432 /// model registry. Call once at setup (or again to replace).
433 pub fn set_sprites(&mut self, set: &SpriteSet) {
434 match &mut self.inner {
435 BackendImpl::Cpu(c) => c.set_sprites(set),
436 BackendImpl::Gpu(g) => g.set_sprites(set),
437 }
438 }
439
440 /// Register animated KFA sprites (one or more bone hierarchies).
441 /// The GPU backend uploads each limb's kv6 as an instanced model
442 /// **once** (appended to the sprite registry) and seeds the limb
443 /// instances at their current pose; the CPU backend caches the
444 /// posed limbs for drawing. Call once at setup, after
445 /// [`set_sprites`](Self::set_sprites), then drive motion per frame
446 /// with [`update_kfa_poses`](Self::update_kfa_poses).
447 ///
448 /// Limbs are posed from the sprites' current
449 /// [`kfaval`](roxlap_formats::kfa::KfaSprite::kfaval) (advance
450 /// [`animsprite`](roxlap_formats::kfa::KfaSprite::animsprite) first
451 /// if using a baked curve), so `kfas` is taken `&mut`.
452 pub fn set_kfa_sprites(&mut self, kfas: &mut [KfaSprite]) {
453 match &mut self.inner {
454 BackendImpl::Cpu(c) => c.set_kfa_sprites(kfas),
455 BackendImpl::Gpu(g) => g.set_kfa_sprites(kfas),
456 }
457 }
458
459 /// Re-pose the registered KFA sprites from their current
460 /// `kfaval[]`. Call each frame after advancing the animation
461 /// (`kfa.animsprite(dt_ms)` or poking `kfaval[]`). The GPU backend
462 /// takes the cheap transform-only update (no model-volume
463 /// re-upload); the CPU backend re-solves limb transforms for the
464 /// next [`render`](Self::render). Must follow a
465 /// [`set_kfa_sprites`](Self::set_kfa_sprites) with the same sprites.
466 pub fn update_kfa_poses(&mut self, kfas: &mut [KfaSprite]) {
467 match &mut self.inner {
468 BackendImpl::Cpu(c) => c.update_kfa_poses(kfas),
469 BackendImpl::Gpu(g) => g.update_kfa_poses(kfas),
470 }
471 }
472
473 /// Carve the next z-layer off the [`SpriteSet::carve_model`] and
474 /// re-upload (the demo's `G` hotkey + GPU.12 copy-on-modify). GPU
475 /// only; a no-op on the CPU backend. Returns the voxels removed.
476 pub fn carve_active_sprite(&mut self) -> u32 {
477 match &mut self.inner {
478 BackendImpl::Cpu(_) => 0,
479 BackendImpl::Gpu(g) => g.carve_active_sprite(),
480 }
481 }
482
483 /// Request that the next [`render`](Self::render) capture its
484 /// framebuffer for [`take_capture`](Self::take_capture). CPU only
485 /// (the GPU swapchain isn't read back) — a no-op on GPU.
486 pub fn request_capture(&mut self) {
487 if let BackendImpl::Cpu(c) = &mut self.inner {
488 c.request_capture();
489 }
490 }
491
492 /// Take the most recently captured frame as packed `0x00RRGGBB`
493 /// pixels + dimensions, or `None` if no capture is ready / GPU.
494 pub fn take_capture(&mut self) -> Option<(Vec<u32>, u32, u32)> {
495 match &mut self.inner {
496 BackendImpl::Cpu(c) => c.take_capture(),
497 BackendImpl::Gpu(_) => None,
498 }
499 }
500
501 /// Screen→world picking input: the world-space hit distance `t` at
502 /// window pixel `(x, y)` from the **last rendered frame**, or `None`
503 /// for out-of-bounds pixels and sky / no-hit. The host reconstructs
504 /// the world hit point as `cam.pos + t * normalize(ray_dir)`, where
505 /// `ray_dir` is the same per-pixel ray the frame was rendered with
506 /// (see the backend's projection).
507 ///
508 /// `t` is the distance to the nearest **scene-grid** surface
509 /// (terrain + grids); sprites do not occlude it (the sprite pass
510 /// reads depth read-only), so a cursor sprite under the pointer is
511 /// transparent to the pick.
512 ///
513 /// Cost: the CPU backend reads its in-memory z-buffer (free); the
514 /// GPU backend stages the depth buffer and blocks on a device poll
515 /// (cheap at click time — do not call every frame). The GPU path
516 /// only has depth when the last frame drew sprites (`write_depth`).
517 #[must_use]
518 pub fn pick_depth(&self, x: u32, y: u32) -> Option<f32> {
519 match &self.inner {
520 BackendImpl::Cpu(c) => c.pick_depth(x, y),
521 BackendImpl::Gpu(g) => g.pick_depth(x, y),
522 }
523 }
524
525 /// World-space view-ray direction (un-normalised) for window pixel
526 /// `(x, y)`, under the projection the **last frame** rendered with.
527 /// The backends differ (CPU `setcamera` vs GPU vertical-FOV
528 /// pinhole), so this hides which one is active. `None` before the
529 /// first frame. Intersect it with a plane for tile picking, or feed
530 /// it to [`Self::pick`] for a voxel.
531 #[must_use]
532 pub fn pixel_ray(&self, camera: &Camera, x: f64, y: f64) -> Option<[f64; 3]> {
533 match &self.inner {
534 BackendImpl::Cpu(c) => c.pixel_ray(camera, x, y),
535 BackendImpl::Gpu(g) => g.pixel_ray(camera, x, y),
536 }
537 }
538
539 /// Canonical screen→world unproject: the full view [`Ray`]
540 /// (`camera.pos` origin + unit direction) for window pixel
541 /// `(x, y)`, under whichever projection the last frame used. The
542 /// one entry point both backends honour — hosts never reconstruct
543 /// the projection. `None` before the first frame or for a
544 /// degenerate ray.
545 ///
546 /// Compose with [`roxlap_scene::Scene::raycast`] for depth-free
547 /// picking that's identical on CPU and GPU:
548 /// `renderer.view_ray(cam, x, y).and_then(|r| scene.raycast(r.origin, r.dir, max))`.
549 #[must_use]
550 pub fn view_ray(&self, camera: &Camera, x: f64, y: f64) -> Option<Ray> {
551 let d = self.pixel_ray(camera, x, y)?;
552 let len = (d[0] * d[0] + d[1] * d[1] + d[2] * d[2]).sqrt();
553 if len < 1e-12 {
554 return None;
555 }
556 Some(Ray {
557 origin: glam::DVec3::from_array([camera.pos[0], camera.pos[1], camera.pos[2]]),
558 dir: glam::DVec3::new(d[0] / len, d[1] / len, d[2] / len),
559 })
560 }
561
562 /// One-call screen→world voxel pick: unproject pixel `(x, y)` with
563 /// the active backend's projection, read the last frame's depth
564 /// there, reconstruct the world hit, and resolve it to the owning
565 /// grid + grid-local voxel via [`Scene::resolve_voxel`]. `None` on
566 /// sky / no-hit, or when no grid claims the surface.
567 ///
568 /// `scene` and `camera` must be the ones the last frame rendered;
569 /// the projection (size + FOV / `hx,hy,hz`) is taken from that
570 /// frame. Cheap on CPU (in-memory z-buffer); on GPU it stages the
571 /// depth buffer (a click-time device poll — not per frame).
572 #[must_use]
573 pub fn pick(&self, scene: &Scene, camera: &Camera, x: u32, y: u32) -> Option<PickHit> {
574 let dir = self.pixel_ray(camera, f64::from(x), f64::from(y))?;
575 let t = f64::from(self.pick_depth(x, y)?);
576 let len = (dir[0] * dir[0] + dir[1] * dir[1] + dir[2] * dir[2]).sqrt();
577 if len < 1e-9 {
578 return None;
579 }
580 let s = t / len; // world = cam.pos + t · (dir / |dir|)
581 let world = glam::DVec3::new(
582 camera.pos[0] + dir[0] * s,
583 camera.pos[1] + dir[1] * s,
584 camera.pos[2] + dir[2] * s,
585 );
586 let (grid, voxel) = scene.resolve_voxel(world, glam::DVec3::from_array(dir))?;
587 #[allow(clippy::cast_possible_truncation)]
588 let world_f32 = [world.x as f32, world.y as f32, world.z as f32];
589 Some(PickHit {
590 world: world_f32,
591 grid,
592 voxel,
593 })
594 }
595}
596
597#[cfg(test)]
598mod tests {
599 use super::*;
600
601 #[test]
602 fn options_default_is_cpu_intent() {
603 let o = RenderOptions::default();
604 assert!(!o.want_gpu);
605 assert_eq!(o.clear_sky & 0xFF00_0000, 0, "clear_sky is 0x00RRGGBB");
606 }
607}