use crate::{
cli::{ComputeMode, RenderArgs},
png_out, render_core, validate,
};
use indicatrix::renderer::gpu_backend::GpuBackend;
use indicatrix_net::SceneState;
pub const MAX_CLI_SAMPLES: u32 = 1_000_000;
pub fn validate_render_params(width: u32, height: u32, samples: u32) -> Result<(), String> {
if width == 0 || height == 0 {
return Err("--width and --height must both be positive".to_string());
}
let pixels = u64::from(width) * u64::from(height);
if pixels > u64::from(validate::MAX_PIXELS) {
return Err(format!(
"--width x --height ({width}x{height} = {pixels} px) exceeds the maximum of {} px",
validate::MAX_PIXELS
));
}
if samples == 0 {
return Err("--samples must be positive".to_string());
}
if samples > MAX_CLI_SAMPLES {
return Err(format!(
"--samples must be <= {MAX_CLI_SAMPLES} (got {samples})"
));
}
Ok(())
}
pub fn run(args: &RenderArgs) -> Result<(), String> {
validate_render_params(args.width, args.height, args.samples)?;
let scene_text = std::fs::read_to_string(&args.scene)
.map_err(|e| format!("could not read scene file {}: {e}", args.scene.display()))?;
let mut scene: SceneState = serde_json::from_str(&scene_text).map_err(|e| {
format!(
"could not parse scene file {} as JSON: {e}",
args.scene.display()
)
})?;
scene.width = args.width;
scene.height = args.height;
validate::validate_scene(&scene)?;
let gpu = match args.compute_mode {
ComputeMode::OnlyCpu => GpuBackend::disabled(),
ComputeMode::Hybrid | ComputeMode::OnlyGpu => GpuBackend::acquire(),
};
let threads = render_core::effective_thread_count(args.threads);
let backend_desc = gpu.adapter_label().map_or_else(
|| format!("CPU across {threads} threads"),
|label| format!("GPU: {label} (CPU fallback across {threads} threads if needed)"),
);
tracing::info!(
"indicatrix-worker render: {}x{} @ {} spp -> {} ({backend_desc})",
args.width,
args.height,
args.samples,
args.out.display()
);
let sums = render_core::trace_samples_with_gpu(&gpu, &scene, 0, args.samples, args.threads);
let rgba = png_out::tonemap_to_rgba(args.width, args.height, args.samples, &sums);
png_out::write_png(args.width, args.height, rgba, &args.out)?;
tracing::info!("wrote {}", args.out.display());
Ok(())
}
#[cfg(test)]
mod tests {
use super::*;
use indicatrix::{
geometry::cuts::StandardGemCuts,
optics::{materials::GemMaterial, raytracer::LightingPreset},
};
use std::path::PathBuf;
#[test]
fn validate_render_params_rejects_zero_and_negative() {
assert!(validate_render_params(0, 1080, 64).is_err());
assert!(validate_render_params(1920, 0, 64).is_err());
assert!(validate_render_params(1920, 1080, 0).is_err());
}
#[test]
fn validate_render_params_rejects_absurd_dimensions() {
let err = validate_render_params(100_000, 100_000, 64).unwrap_err();
assert!(err.contains("exceeds the maximum"), "{err}");
}
#[test]
fn validate_render_params_rejects_absurd_sample_counts() {
let err = validate_render_params(1920, 1080, 50_000_000).unwrap_err();
assert!(err.contains("--samples"), "{err}");
}
#[test]
fn validate_render_params_accepts_sensible_values() {
assert!(validate_render_params(3840, 2160, 4096).is_ok());
assert!(validate_render_params(8, 8, 4).is_ok());
}
fn write_tiny_scene_json(dir: &std::path::Path) -> PathBuf {
let scene = SceneState {
width: 999, height: 999,
yaw: 0.4,
pitch: 0.3,
distance: 3.0,
light_yaw: 0.85,
light_pitch: 0.95,
exposure: 1.0,
max_bounces: 4,
lighting_preset: LightingPreset::Daylight,
material: GemMaterial::diamond(),
planes: StandardGemCuts::standard_round_brilliant(),
girdle_frosted: false,
backdrop: 0.0,
};
let path = dir.join("scene.json");
std::fs::write(&path, serde_json::to_string(&scene).unwrap()).unwrap();
path
}
#[test]
fn run_produces_a_png_of_the_requested_dimensions() {
let dir = std::env::temp_dir().join(format!(
"indicatrix-worker-render-test-{}",
std::process::id()
));
std::fs::create_dir_all(&dir).unwrap();
let scene_path = write_tiny_scene_json(&dir);
let out_path = dir.join("render.png");
let args = RenderArgs {
scene: scene_path,
out: out_path.clone(),
width: 8,
height: 8,
samples: 4,
threads: 2,
compute_mode: ComputeMode::OnlyCpu,
};
run(&args).unwrap();
let img = image::open(&out_path).expect("render must produce a valid, readable PNG");
assert_eq!(img.width(), 8);
assert_eq!(img.height(), 8);
let _ = std::fs::remove_dir_all(&dir);
}
#[test]
fn run_rejects_a_missing_scene_file() {
let args = RenderArgs {
scene: PathBuf::from("this/path/does/not/exist.json"),
out: std::env::temp_dir().join("never.png"),
width: 8,
height: 8,
samples: 4,
threads: 1,
compute_mode: ComputeMode::OnlyCpu,
};
assert!(run(&args).is_err());
}
#[test]
fn run_rejects_a_scene_with_a_degenerate_normal() {
let dir = std::env::temp_dir().join(format!(
"indicatrix-worker-render-badscene-{}",
std::process::id()
));
std::fs::create_dir_all(&dir).unwrap();
let mut scene = SceneState {
width: 8,
height: 8,
yaw: 0.0,
pitch: 0.0,
distance: 3.0,
light_yaw: 0.0,
light_pitch: 0.0,
exposure: 1.0,
max_bounces: 4,
lighting_preset: LightingPreset::Daylight,
material: GemMaterial::diamond(),
planes: StandardGemCuts::standard_round_brilliant(),
girdle_frosted: false,
backdrop: 0.0,
};
scene.planes[0].normal = [0.0, 0.0, 0.0];
let scene_path = dir.join("scene.json");
std::fs::write(&scene_path, serde_json::to_string(&scene).unwrap()).unwrap();
let args = RenderArgs {
scene: scene_path,
out: dir.join("never.png"),
width: 8,
height: 8,
samples: 4,
threads: 1,
compute_mode: ComputeMode::OnlyCpu,
};
let err = run(&args).unwrap_err();
assert!(err.contains("degenerate"), "{err}");
let _ = std::fs::remove_dir_all(&dir);
}
#[cfg(feature = "gpu")]
#[test]
fn run_produces_a_png_of_the_requested_dimensions_via_gpu() {
let dir = std::env::temp_dir().join(format!(
"indicatrix-worker-render-gpu-test-{}",
std::process::id()
));
std::fs::create_dir_all(&dir).unwrap();
let scene_path = write_tiny_scene_json(&dir);
let out_path = dir.join("render.png");
let args = RenderArgs {
scene: scene_path,
out: out_path.clone(),
width: 8,
height: 8,
samples: 4,
threads: 2,
compute_mode: ComputeMode::OnlyGpu,
};
run(&args).unwrap();
let img = image::open(&out_path).expect("render must produce a valid, readable PNG");
assert_eq!(img.width(), 8);
assert_eq!(img.height(), 8);
let _ = std::fs::remove_dir_all(&dir);
}
}