Skip to content

Commit

Permalink
good!
Browse files Browse the repository at this point in the history
  • Loading branch information
shiinamiyuki committed May 12, 2023
1 parent a4e2b81 commit 48d98a0
Show file tree
Hide file tree
Showing 9 changed files with 234 additions and 101 deletions.
13 changes: 12 additions & 1 deletion build.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,20 @@
use std::env;

fn main() {
println!("cargo:rerun-if-changed=build.rs");
let out = cmake::Config::new("ext/assimp")
.no_build_target(true)
.build();
dbg!(out.display());
println!("cargo:rustc-link-search=native={}/build/bin", out.display());
println!("cargo:rustc-link-lib=dylib=assimpd");
match env::var("PROFILE") {
Ok(x) => {
if x == "release" {
println!("cargo:rustc-link-lib=dylib=assimp");
} else if x == "debug" {
println!("cargo:rustc-link-lib=dylib=assimpd");
}
}
_ => unreachable!(),
}
}
22 changes: 22 additions & 0 deletions scenes/cbox/test.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
[
{
"method":{
"type":"pt",
"use_nee":false
},
"out":"output/vanilla.exr"
},
{
"method":{
"type":"pt",
"use_nee":true
},
"out":"output/nee.exr"
},
{
"method":{
"type":"mcmc"
},
"out":"output/mcmc.exr"
}
]
76 changes: 17 additions & 59 deletions src/bin/akari_cli.rs
Original file line number Diff line number Diff line change
@@ -1,88 +1,46 @@
use akari_render::{
film::{Film, FilmColorRepr},
integrator::{
mcmc::{self, MCMC},
mcmc::{self, Mcmc},
normal::NormalVis,
pt::PathTracer,
Integrator,
render, Integrator, RenderTask,
},
};
use clap::{arg, Arg, Command};
use luisa_compute as luisa;
use std::{env::current_exe, process::exit};
use std::{env::current_exe, fs::File, process::exit};
fn main() {
luisa::init_logger();
let mut cmd = Command::new("akari_cli")
.allow_missing_positional(false)
.arg(Arg::new("scene").help("Scene file to render"))
.arg(arg!(-s --scene <SCENE> "Scene file to render"))
.arg(arg!(-m --method <METHOD> "Render method config file"))
.arg(
arg!(-d --device <DEVICE> "Compute device. One of: cpu, cuda, dx, metal. Default: cpu"),
)
.arg(arg!(-o --output <FILE> "Output file name. Default: out.png"))
.arg(arg!(--spp <SPP> "Set samples per pixel, override existing value"));
);
let help = cmd.render_help();
let matches = cmd.get_matches();
let scene = matches.get_one::<String>("scene");
let output = matches
.get_one::<String>("output")
.cloned()
.unwrap_or("out.png".to_string());
let spp = matches
.get_one::<String>("spp")
.map(|s| s.parse::<u32>().unwrap());
let method = matches.get_one::<String>("method");
if scene.is_none() {
println!("{}", help);
exit(1);
}
if method.is_none() {
println!("{}", help);
exit(1);
}
let device = matches
.get_one::<String>("device")
.cloned()
.unwrap_or("cpu".to_string());
let ctx = luisa::Context::new(current_exe().unwrap());
let device = ctx.create_device(&device);
let method = method.unwrap();
let task: RenderTask = {
let file = File::open(method).unwrap();
serde_json::from_reader(file).unwrap()
};
let scene = akari_render::scene::Scene::load(device.clone(), &scene.unwrap());
let mut film = Film::new(
device.clone(),
scene.camera.resolution(),
FilmColorRepr::SRgb,
);
let output_image: luisa::Tex2d<luisa::Float4> = device.create_tex2d(
luisa::PixelStorage::Float4,
scene.camera.resolution().x,
scene.camera.resolution().y,
1,
);
// {
// let normal_vis = NormalVis::new(device.clone(), 1);
// normal_vis.render(&scene, &mut film).unwrap_or_else(|e| {
// println!("Render failed: {:?}", e);
// exit(1);
// });
// }
let tic = std::time::Instant::now();
// {
// let pt = PathTracer::new(device.clone(), spp.unwrap_or(1), 64, 5, true);
// pt.render(&scene, &mut film);
// }
{
let mcmc = MCMC::new(
device.clone(),
spp.unwrap_or(1),
1,
5,
true,
mcmc::Method::Kelemen {
small_sigma: 0.01,
large_step_prob: 0.1,
},
100,
100000,
);
mcmc.render(&scene, &mut film);
}
film.copy_to_rgba_image(&output_image);
let toc = std::time::Instant::now();
log::info!("Rendered in {:.1}ms", (toc - tic).as_secs_f64() * 1e3);

akari_render::util::write_image(&output_image, &output);
render(device, &scene, &task);
}
6 changes: 6 additions & 0 deletions src/color.rs
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,12 @@ impl ColorVar {
}
}
impl Color {
pub fn max(&self) -> Expr<f32> {
match self {
Color::Rgb(v) => v.reduce_max(),
Color::Spectral(_) => todo!(),
}
}
pub fn flatten(&self) -> Expr<FlatColor> {
match self {
Color::Rgb(rgb) => FlatColorExpr::new(make_float4(rgb.x(), rgb.y(), rgb.z(), 0.0)),
Expand Down
Empty file added src/integrator/gpt.rs
Empty file.
95 changes: 66 additions & 29 deletions src/integrator/mcmc.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,4 @@
use rand::{thread_rng, Rng};

use super::pt::PathTracer;
use super::pt::{self, PathTracer};
use super::Integrator;
use crate::{
color::*,
Expand All @@ -16,8 +14,10 @@ use crate::{
util::alias_table::AliasTable,
*,
};
use rand::{thread_rng, Rng};
use serde::{Deserialize, Serialize};

#[derive(Clone, Copy, Debug)]
#[derive(Clone, Copy, Debug, Serialize, Deserialize)]
pub enum Method {
Kelemen {
small_sigma: f32,
Expand All @@ -26,12 +26,49 @@ pub enum Method {
LangevinOnline,
LangevinHybrid,
}
pub struct MCMC {
impl Default for Method {
fn default() -> Self {
Method::Kelemen {
small_sigma: 0.01,
large_step_prob: 0.1,
}
}
}
pub struct Mcmc {
pub device: Device,
pub pt: PathTracer,
pub method: Method,
pub n_chains: usize,
pub n_bootstrap: usize,
config: Config,
}

#[derive(Clone, Serialize, Deserialize, Debug)]
#[serde(default)]
pub struct Config {
pub spp: u32,
pub max_depth: u32,
pub rr_depth: u32,
pub spp_per_pass: u32,
pub use_nee: bool,
pub method: Method,
pub n_chains: usize,
pub n_bootstrap: usize,
}
impl Default for Config {
fn default() -> Self {
let default_pt = pt::Config::default();
Self {
spp: default_pt.spp,
spp_per_pass: default_pt.spp_per_pass,
use_nee: default_pt.use_nee,
max_depth: default_pt.max_depth,
rr_depth: default_pt.rr_depth,
method: Method::default(),
n_chains: 512,
n_bootstrap: 100000,
}
}
}
#[derive(Clone, Copy, Debug, Value)]
#[repr(C)]
Expand All @@ -52,33 +89,29 @@ struct RenderState {
b_init: f32,
b_init_cnt: u32,
}
impl MCMC {
impl Mcmc {
fn sample_dimension(&self) -> usize {
4 + self.pt.max_depth as usize * 4
4 + self.pt.max_depth as usize * 5
}
pub fn new(
device: Device,
spp: u32,
spp_per_pass: u32,
max_depth: u32,
use_nee: bool,
method: Method,
n_chains: usize,
n_bootstrap: usize,
) -> Self {
pub fn new(device: Device, config: Config) -> Self {
let pt_config = pt::Config {
spp: config.spp,
max_depth: config.max_depth,
spp_per_pass: config.spp_per_pass,
use_nee: config.use_nee,
rr_depth: config.rr_depth,
};
Self {
device: device.clone(),
pt: PathTracer::new(device.clone(), spp, spp_per_pass, max_depth, use_nee),
method,
n_chains,
n_bootstrap,
pt: PathTracer::new(device.clone(), pt_config),
method: config.method,
n_chains: config.n_chains,
n_bootstrap: config.n_bootstrap,
config,
}
}
fn scalar_contribution(&self, color: &Color) -> Expr<f32> {
match color {
Color::Rgb(v) => v.reduce_max(),
Color::Spectral(_) => todo!(),
}
color.max()
}
fn evaluate<S: IndependentSampler + Clone>(
&self,
Expand Down Expand Up @@ -352,16 +385,15 @@ impl MCMC {
film.set_splat_scale(b as f32 / self.pt.spp as f32);
}
}
impl Integrator for MCMC {
impl Integrator for Mcmc {
fn render(&self, scene: &Scene, film: &mut Film) {
let resolution = scene.camera.resolution();
log::info!(
"Resolution {}x{}, spp: {}",
"Resolution {}x{}\nconfig: {:#?}",
resolution.x,
resolution.y,
self.pt.spp
&self.config
);

assert_eq!(resolution.x, film.resolution().x);
assert_eq!(resolution.y, film.resolution().y);
film.clear();
Expand All @@ -370,3 +402,8 @@ impl Integrator for MCMC {
self.render_loop(scene, &color_repr, &render_state, film);
}
}

pub fn render(device: Device, scene: &Scene, film: &mut Film, config: &Config) {
let mcmc = Mcmc::new(device.clone(), config.clone());
mcmc.render(scene, film);
}
67 changes: 64 additions & 3 deletions src/integrator/mod.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,70 @@
use crate::{*, camera::*, film::*, scene::*};
use serde::{Deserialize, Serialize};

use crate::{film::*, scene::*, *};

pub trait Integrator {
fn render(&self, scene: &Scene, film: &mut Film);
}

pub mod pt;
pub mod gpt;
pub mod mcmc;
pub mod normal;
pub mod mcmc;
pub mod pt;

#[derive(Clone, Serialize, Deserialize)]
#[serde(tag = "type")]
pub enum Method {
#[serde(rename = "pt")]
PathTracer(pt::Config),
#[serde(rename = "mcmc")]
Mcmc(mcmc::Config),
}
#[derive(Clone, Serialize, Deserialize)]
pub struct RenderConfig {
pub method: Method,
pub out: String,
}
#[derive(Clone, Serialize, Deserialize)]
#[serde(untagged)]
pub enum RenderTask {
Single(RenderConfig),
Multi(Vec<RenderConfig>),
}

pub fn render(device: Device, scene: &Scene, task: &RenderTask) {
fn render_single(device: Device, scene: &Scene, config: &RenderConfig) {
let mut film = Film::new(
device.clone(),
scene.camera.resolution(),
FilmColorRepr::SRgb,
);
let output_image: luisa::Tex2d<luisa::Float4> = device.create_tex2d(
luisa::PixelStorage::Float4,
scene.camera.resolution().x,
scene.camera.resolution().y,
1,
);
let tic = std::time::Instant::now();
match &config.method {
Method::PathTracer(config) => pt::render(device.clone(), scene, &mut film, &config),
Method::Mcmc(config) => mcmc::render(device.clone(), scene, &mut film, &config),
}
let toc = std::time::Instant::now();
log::info!("Rendered in {:.1}ms", (toc - tic).as_secs_f64() * 1e3);
film.copy_to_rgba_image(&output_image);
util::write_image(&output_image, &config.out);
}
match task {
RenderTask::Single(config) => render_single(device, scene, config),
RenderTask::Multi(configs) => {
let tic = std::time::Instant::now();
log::info!("Rendering with {} methods", configs.len());
for (i, config) in configs.iter().enumerate() {
log::info!("Rendering {}/{}", i + 1, configs.len());
render_single(device.clone(), scene, config)
}
let toc = std::time::Instant::now();
log::info!("Rendered in {:.3}s", (toc - tic).as_secs_f64());
}
}
}
1 change: 1 addition & 0 deletions src/integrator/normal.rs
Original file line number Diff line number Diff line change
Expand Up @@ -64,3 +64,4 @@ impl Integrator for NormalVis {
});
}
}

Loading

0 comments on commit 48d98a0

Please sign in to comment.