euv_core/game/engine3d/raytrace/session/
impl.rs1use super::*;
2
3#[cfg(not(target_arch = "wasm32"))]
4unsafe impl Sync for RgbaRowMutView {}
6
7#[cfg(not(target_arch = "wasm32"))]
8impl RgbaRowMutView {
9 #[must_use]
15 pub(crate) fn ptr(&self) -> *mut u8 {
16 let Self { ptr } = *self;
17 ptr
18 }
19}
20
21#[cfg(not(target_arch = "wasm32"))]
22unsafe impl Sync for RtPassMutView {}
24
25#[cfg(not(target_arch = "wasm32"))]
26impl RtPassMutView {
27 #[must_use]
33 pub(crate) fn accum_ptrs(&self) -> (*mut Vec3, *mut f32, *mut f32, *mut u32) {
34 let Self {
35 radiance,
36 moment_1,
37 moment_2,
38 sample_counts,
39 ..
40 } = *self;
41 (radiance, moment_1, moment_2, sample_counts)
42 }
43
44 #[must_use]
50 pub(crate) fn gbuffer_ptrs(&self) -> (*mut Vec3, *mut Vec3, *mut f32, *mut u8) {
51 let Self {
52 albedo,
53 normal,
54 depth,
55 hit_mask,
56 ..
57 } = *self;
58 (albedo, normal, depth, hit_mask)
59 }
60}
61
62impl Default for RaytraceConfig {
64 fn default() -> Self {
70 Self::new(RT_DEFAULT_VIEWPORT_WIDTH, RT_DEFAULT_VIEWPORT_HEIGHT)
71 }
72}
73
74impl RaytraceConfig {
76 #[must_use]
87 pub fn new(width: u32, height: u32) -> Self {
88 Self {
89 width: width.max(1),
90 height: height.max(1),
91 integrator: RtIntegratorMode::PathTrace,
92 tonemap: RtTonemapMode::Agx,
93 exposure: RT_DEFAULT_EXPOSURE,
94 tile_size: RT_DEFAULT_TILE_SIZE,
95 enable_denoiser: true,
96 whitted: WhittedIntegrator::default(),
97 path_trace: PathTraceIntegrator::default(),
98 rtao: RtaoIntegrator::default(),
99 hybrid_preview: HybridPreviewIntegrator::default(),
100 svgf: SvgfConfig::default(),
101 seed: RT_DEFAULT_SEED,
102 }
103 }
104
105 #[must_use]
116 pub fn with_integrator(mut self, integrator: RtIntegratorMode) -> Self {
117 self.set_integrator(integrator);
118 self
119 }
120
121 #[must_use]
132 pub fn with_tonemap(mut self, tonemap: RtTonemapMode) -> Self {
133 self.set_tonemap(tonemap);
134 self
135 }
136
137 #[must_use]
148 pub fn with_exposure(mut self, exposure: f32) -> Self {
149 self.set_exposure(exposure.max(0.01));
150 self
151 }
152
153 #[must_use]
164 pub fn with_tile_size(mut self, tile_size: u32) -> Self {
165 self.set_tile_size(tile_size.max(1));
166 self
167 }
168
169 #[must_use]
180 pub fn with_denoiser(mut self, enabled: bool) -> Self {
181 self.set_enable_denoiser(enabled);
182 self
183 }
184
185 #[must_use]
196 pub fn with_svgf(mut self, svgf: SvgfConfig) -> Self {
197 self.set_svgf(svgf);
198 self
199 }
200
201 #[must_use]
212 pub fn with_seed(mut self, seed: u32) -> Self {
213 self.set_seed(seed);
214 self
215 }
216}
217
218impl RaytraceSession {
220 #[must_use]
230 pub fn new(config: RaytraceConfig) -> Self {
231 let width: u32 = config.get_width().max(1);
232 let height: u32 = config.get_height().max(1);
233 let total_pixels: usize = (width as usize).saturating_mul(height as usize);
234 let mut stats: RaytraceStats = RaytraceStats::default();
235 stats.set_total_pixels(total_pixels as u32);
236 Self {
237 config,
238 accumulator: TemporalAccumulator::new(width, height),
239 gbuffer: RtGBuffer::new(width, height),
240 denoiser: SvgfDenoiser::new(width, height, config.get_svgf()),
241 variance_scratch: vec![0.0; total_pixels],
242 denoised_hdr: vec![Vec3::zero(); total_pixels],
243 rgba8_output: vec![0; total_pixels.saturating_mul(4)],
244 stats,
245 tile_cursor: 0,
246 pass_index: 0,
247 }
248 }
249
250 pub fn resize(&mut self, width: u32, height: u32) {
258 let w: u32 = width.max(1);
259 let h: u32 = height.max(1);
260 let total_pixels: usize = (w as usize).saturating_mul(h as usize);
261 let Self {
262 config,
263 accumulator,
264 gbuffer,
265 denoiser,
266 variance_scratch,
267 denoised_hdr,
268 rgba8_output,
269 stats,
270 tile_cursor,
271 pass_index,
272 } = self;
273 config.set_width(w);
274 config.set_height(h);
275 accumulator.resize(w, h);
276 gbuffer.resize(w, h);
277 denoiser.resize(w, h);
278 variance_scratch.resize(total_pixels, 0.0);
279 denoised_hdr.resize(total_pixels, Vec3::zero());
280 rgba8_output.resize(total_pixels.saturating_mul(4), 0);
281 *stats = RaytraceStats::default();
282 stats.set_total_pixels(total_pixels as u32);
283 *tile_cursor = 0;
284 *pass_index = 0;
285 }
286
287 pub fn reset(&mut self) {
293 let total_pixels: u32 = self
294 .get_config()
295 .get_width()
296 .saturating_mul(self.get_config().get_height());
297 let Self {
298 accumulator,
299 gbuffer,
300 variance_scratch,
301 denoised_hdr,
302 rgba8_output,
303 stats,
304 tile_cursor,
305 pass_index,
306 ..
307 } = self;
308 accumulator.reset_accumulation();
309 gbuffer.clear();
310 variance_scratch.fill(0.0);
311 denoised_hdr.fill(Vec3::zero());
312 rgba8_output.fill(0);
313 *stats = RaytraceStats::default();
314 stats.set_total_pixels(total_pixels);
315 *tile_cursor = 0;
316 *pass_index = 0;
317 }
318
319 pub fn set_integrator(&mut self, mode: RtIntegratorMode) {
326 if self.get_config().get_integrator() != mode {
327 let mut cfg: RaytraceConfig = self.get_config();
328 cfg.set_integrator(mode);
329 self.set_config(cfg);
330 self.reset();
331 }
332 }
333
334 pub fn render_progressive_step(
346 &mut self,
347 scene: &RaytraceScene,
348 budget_rays: u64,
349 ) -> RaytraceStats {
350 let step_start: RtTimer = rt_now();
351 let cam_fp: u64 = scene.get_camera().motion_fingerprint();
352 let scene_rev: u64 = scene.get_revision();
353 let cfg: RaytraceConfig = self.get_config();
354 let width: u32 = cfg.get_width().max(1);
355 let height: u32 = cfg.get_height().max(1);
356 let total_pixels: u32 = width.saturating_mul(height);
357 let tile_size: u32 = cfg.get_tile_size().max(1);
358 let tiles_x: u32 = width.div_ceil(tile_size);
359 let tiles_y: u32 = height.div_ceil(tile_size);
360 let total_tiles: u32 = tiles_x.saturating_mul(tiles_y).max(1);
361 let Self {
362 accumulator,
363 gbuffer,
364 denoiser,
365 variance_scratch,
366 denoised_hdr,
367 rgba8_output,
368 stats,
369 tile_cursor,
370 pass_index,
371 ..
372 } = self;
373 if accumulator.sync_motion(cam_fp, scene_rev) {
374 gbuffer.clear();
375 variance_scratch.fill(0.0);
376 *tile_cursor = 0;
377 *pass_index = 0;
378 *stats = RaytraceStats::default();
379 stats.set_total_pixels(total_pixels);
380 }
381 denoiser.set_config(cfg.get_svgf());
382 let ray_budget: u64 = budget_rays.max(1);
383 let mut step_counter: RtRayCounter = RtRayCounter::default();
384 let mut pixels_this_step: u32 = 0;
385 let camera: RtCamera = scene.get_camera();
386 let base_seed: u32 = cfg.get_seed();
387 let resolution: [u32; 2] = [width, height];
388 if ray_budget == u64::MAX && *tile_cursor == 0 {
389 let current_pass: u32 = *pass_index;
390 let pass_counter: RtRayCounter =
391 trace_full_pass_parallel(&cfg, scene, current_pass, accumulator, gbuffer);
392 step_counter.merge(pass_counter);
393 pixels_this_step = total_pixels;
394 *pass_index = pass_index.saturating_add(1);
395 } else {
396 let mut tiles_processed: u32 = 0;
397 while step_counter.total_rays() < ray_budget && tiles_processed < total_tiles {
398 let tile_idx: u32 = *tile_cursor;
399 let tx: u32 = tile_idx % tiles_x;
400 let ty: u32 = tile_idx / tiles_x;
401 let x_start: u32 = tx.saturating_mul(tile_size);
402 let y_start: u32 = ty.saturating_mul(tile_size);
403 let x_end: u32 = x_start.saturating_add(tile_size).min(width);
404 let y_end: u32 = y_start.saturating_add(tile_size).min(height);
405 let current_pass: u32 = *pass_index;
406 let jitter: [f32; 2] = r2_subpixel_jitter(current_pass);
407 for py in y_start..y_end {
408 let row_offset: usize = (py as usize) * (width as usize);
409 for px in x_start..x_end {
410 let pixel_idx: usize = row_offset + (px as usize);
411 let mut rng: RtRng =
412 RtRng::from_pixel_sample(pixel_idx as u32, current_pass, base_seed);
413 let lens_xi: [f32; 2] = rng.next_f32_pair();
414 let ray: Ray3D = camera.generate_ray([px, py], resolution, jitter, lens_xi);
415 let sample_out: RtSampleOutput =
416 dispatch_integrator_ray(&cfg, scene, &ray, &mut rng, &mut step_counter);
417 accumulator.accumulate_pixel(pixel_idx, sample_out.get_radiance());
418 if current_pass == 0 || gbuffer.get_hit_mask()[pixel_idx] == 0 {
419 gbuffer.set_pixel(
420 pixel_idx,
421 sample_out.get_albedo(),
422 sample_out.get_normal(),
423 sample_out.get_depth(),
424 sample_out.get_hit(),
425 );
426 }
427 pixels_this_step = pixels_this_step.saturating_add(1);
428 }
429 }
430 tiles_processed += 1;
431 *tile_cursor += 1;
432 if *tile_cursor >= total_tiles {
433 *tile_cursor = 0;
434 *pass_index = pass_index.saturating_add(1);
435 break;
436 }
437 }
438 }
439 let denoise_start: RtTimer = rt_now();
440 if cfg.get_enable_denoiser() {
441 accumulator.write_variance_into(gbuffer, variance_scratch.as_mut_slice());
442 denoiser.denoise(
443 accumulator.get_radiance().as_slice(),
444 variance_scratch.as_slice(),
445 gbuffer,
446 denoised_hdr.as_mut_slice(),
447 );
448 } else {
449 denoised_hdr.copy_from_slice(accumulator.get_radiance().as_slice());
450 }
451 let denoise_ms: f32 = rt_elapsed_ms(denoise_start);
452 tonemap_hdr_to_rgba8(
453 denoised_hdr.as_slice(),
454 cfg.get_tonemap(),
455 cfg.get_exposure(),
456 rgba8_output.as_mut_slice(),
457 );
458 let step_ms: f32 = rt_elapsed_ms(step_start);
459 let step_rays: u64 = step_counter.total_rays();
460 let mrays_per_sec: f32 = if step_ms > 1e-4 {
461 ((step_rays as f32) / (step_ms * 1000.0)).max(0.0)
462 } else {
463 0.0
464 };
465 let next_primary: u64 = stats
466 .get_primary_rays()
467 .saturating_add(step_counter.get_primary_rays());
468 let next_shadow: u64 = stats
469 .get_shadow_rays()
470 .saturating_add(step_counter.get_shadow_rays());
471 let next_bounce: u64 = stats
472 .get_bounce_rays()
473 .saturating_add(step_counter.get_bounce_rays());
474 let next_total: u64 = next_primary
475 .saturating_add(next_shadow)
476 .saturating_add(next_bounce);
477 stats.set_samples_per_pixel(accumulator.average_samples_per_pixel());
478 stats.set_completed_passes(*pass_index);
479 stats.set_primary_rays(next_primary);
480 stats.set_shadow_rays(next_shadow);
481 stats.set_bounce_rays(next_bounce);
482 stats.set_total_rays(next_total);
483 stats.set_last_step_rays(step_rays);
484 stats.set_last_step_ms(step_ms);
485 stats.set_mrays_per_sec(mrays_per_sec);
486 stats.set_denoise_ms(denoise_ms);
487 stats.set_pixels_traced_in_pass(if *tile_cursor == 0 {
488 total_pixels
489 } else {
490 stats
491 .get_pixels_traced_in_pass()
492 .saturating_add(pixels_this_step)
493 .min(total_pixels)
494 });
495 stats.set_total_pixels(total_pixels);
496 *stats
497 }
498
499 pub fn render_full_pass(&mut self, scene: &RaytraceScene) -> RaytraceStats {
510 let start_pass: u32 = self.get_pass_index();
511 let mut latest: RaytraceStats = self.render_progressive_step(scene, u64::MAX);
512 if latest.get_completed_passes() == start_pass && self.get_tile_cursor() != 0 {
513 latest = self.render_progressive_step(scene, u64::MAX);
514 }
515 latest
516 }
517
518 pub fn render_samples(&mut self, scene: &RaytraceScene, samples: u32) -> RaytraceStats {
530 let count: u32 = samples.max(1);
531 if count == 1 || self.get_tile_cursor() != 0 {
532 let mut latest: RaytraceStats = self.get_stats();
533 for _ in 0..count {
534 latest = self.render_full_pass(scene);
535 }
536 return latest;
537 }
538 let step_start: RtTimer = rt_now();
539 let cam_fp: u64 = scene.get_camera().motion_fingerprint();
540 let scene_rev: u64 = scene.get_revision();
541 let cfg: RaytraceConfig = self.get_config();
542 let width: u32 = cfg.get_width().max(1);
543 let height: u32 = cfg.get_height().max(1);
544 let total_pixels: u32 = width.saturating_mul(height);
545 let Self {
546 accumulator,
547 gbuffer,
548 denoiser,
549 variance_scratch,
550 denoised_hdr,
551 rgba8_output,
552 stats,
553 tile_cursor,
554 pass_index,
555 ..
556 } = self;
557 if accumulator.sync_motion(cam_fp, scene_rev) {
558 gbuffer.clear();
559 variance_scratch.fill(0.0);
560 *tile_cursor = 0;
561 *pass_index = 0;
562 *stats = RaytraceStats::default();
563 stats.set_total_pixels(total_pixels);
564 }
565 denoiser.set_config(cfg.get_svgf());
566 let mut step_counter: RtRayCounter = RtRayCounter::default();
567 for _ in 0..count {
568 let current_pass: u32 = *pass_index;
569 let pass_counter: RtRayCounter =
570 trace_full_pass_parallel(&cfg, scene, current_pass, accumulator, gbuffer);
571 step_counter.merge(pass_counter);
572 *pass_index = pass_index.saturating_add(1);
573 }
574 let denoise_start: RtTimer = rt_now();
575 if cfg.get_enable_denoiser() {
576 accumulator.write_variance_into(gbuffer, variance_scratch.as_mut_slice());
577 denoiser.denoise(
578 accumulator.get_radiance().as_slice(),
579 variance_scratch.as_slice(),
580 gbuffer,
581 denoised_hdr.as_mut_slice(),
582 );
583 } else {
584 denoised_hdr.copy_from_slice(accumulator.get_radiance().as_slice());
585 }
586 let denoise_ms: f32 = rt_elapsed_ms(denoise_start);
587 tonemap_hdr_to_rgba8(
588 denoised_hdr.as_slice(),
589 cfg.get_tonemap(),
590 cfg.get_exposure(),
591 rgba8_output.as_mut_slice(),
592 );
593 let step_ms: f32 = rt_elapsed_ms(step_start);
594 let step_rays: u64 = step_counter.total_rays();
595 let mrays_per_sec: f32 = if step_ms > 1e-4 {
596 ((step_rays as f32) / (step_ms * 1000.0)).max(0.0)
597 } else {
598 0.0
599 };
600 let next_primary: u64 = stats
601 .get_primary_rays()
602 .saturating_add(step_counter.get_primary_rays());
603 let next_shadow: u64 = stats
604 .get_shadow_rays()
605 .saturating_add(step_counter.get_shadow_rays());
606 let next_bounce: u64 = stats
607 .get_bounce_rays()
608 .saturating_add(step_counter.get_bounce_rays());
609 let next_total: u64 = next_primary
610 .saturating_add(next_shadow)
611 .saturating_add(next_bounce);
612 stats.set_samples_per_pixel(accumulator.average_samples_per_pixel());
613 stats.set_completed_passes(*pass_index);
614 stats.set_primary_rays(next_primary);
615 stats.set_shadow_rays(next_shadow);
616 stats.set_bounce_rays(next_bounce);
617 stats.set_total_rays(next_total);
618 stats.set_last_step_rays(step_rays);
619 stats.set_last_step_ms(step_ms);
620 stats.set_mrays_per_sec(mrays_per_sec);
621 stats.set_denoise_ms(denoise_ms);
622 stats.set_pixels_traced_in_pass(total_pixels);
623 stats.set_total_pixels(total_pixels);
624 *stats
625 }
626
627 #[must_use]
637 pub fn rgba8_slice(&self) -> &[u8] {
638 self.get_rgba8_output().as_slice()
639 }
640
641 #[must_use]
651 pub fn hdr_slice(&self) -> &[Vec3] {
652 self.get_denoised_hdr().as_slice()
653 }
654}