Skip to content

Commit 82fc4b9

Browse files
nnethercoteLegNeato
authored andcommitted
Re-enable path-tracer.
In #313 I disabled all the code relying on OptiX. I subsequently learned that `path-tracer`'s use of OptiX is optional. `path-tracer` is a big and good example, so this commit re-enables it, but with the OptiX-specific bits commented out. (Commenting out code is crude, but I had trouble with features, as detailed within the commit. This is better than nothing.)
1 parent 65f66ec commit 82fc4b9

File tree

8 files changed

+2084
-473
lines changed

8 files changed

+2084
-473
lines changed

Cargo.lock

Lines changed: 1988 additions & 409 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Cargo.toml

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -18,8 +18,18 @@ members = [
1818
"crates/rustc_codegen_nvvm",
1919
"crates/rustc_codegen_nvvm_macros",
2020

21-
# These crates that rely on OptiX are disabled because OptiX is less important than CUDA and the
22-
# OptiX SDK is a pain to install.
21+
# OPTIX_DISABLED: These crates that rely on OptiX are disabled because OptiX is less important
22+
# than CUDA and the OptiX SDK is a pain to install.
23+
#
24+
# The `path-tracer` crates are an exception because the OptiX use is currently commented out. I
25+
# originally tried putting the OptiX use within these crates behind a feature, but `crates/optix`
26+
# would be built by `cargo build` even when the feature was disabled, even though it's not listed
27+
# in `members`. This would break the build if you don't have OptiX installed. (There were also
28+
# problems with `cargo doc --all-features`.) Commenting out the OptiX-specific code is crude, but
29+
# at least that way the non-OptiX-specific code (which is a majority of it) can be built and
30+
# tested, and the OptiX-specific code is still available to be used if necessary with only
31+
# moderate effort required to uncomment it. (Search for comments mentioning `OPTIX_DISABLED`.)
32+
#
2333
# "crates/optix",
2434
# "crates/optix_device",
2535
# "crates/optix_device_macros",
@@ -30,8 +40,8 @@ members = [
3040
# "crates/optix/examples/ex03_window",
3141
# "crates/optix/examples/ex04_mesh",
3242
# "crates/optix/examples/ex04_mesh/kernels",
33-
# "crates/optix/examples/path_tracer",
34-
# "crates/optix/examples/path_tracer/kernels",
43+
"crates/optix/examples/path_tracer",
44+
"crates/optix/examples/path_tracer/kernels",
3545

3646
"examples/gemm",
3747
"examples/gemm/kernels",

crates/optix/examples/path_tracer/Cargo.toml

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,8 @@ cust = { version = "0.3", path = "../../../cust", features = ["impl_glam"] }
1010
image = "0.25.5"
1111
path-tracer-kernels = { path = "kernels" }
1212
gpu_rand = { version = "0.1", path = "../../../gpu_rand" }
13-
optix = { version = "0.1", path = "../../../optix" }
13+
# See the OPTIX_DISABLED comment.
14+
# optix = { version = "0.1", path = "../../../optix" }
1415
glium = "0.32.0"
1516
glutin = "0.28.0"
1617
imgui = "0.9.0"

crates/optix/examples/path_tracer/build.rs

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -14,9 +14,12 @@ fn main() {
1414
.copy_to(out_path.join("kernels.ptx"))
1515
.build()
1616
.unwrap();
17-
CudaBuilder::new(manifest_dir.join("kernels"))
18-
.copy_to(out_path.join("kernels_optix.ptx"))
19-
.build_args(&["--features", "optix"])
20-
.build()
21-
.unwrap();
17+
18+
// See the OPTIX_DISABLED comment.
19+
// #[cfg(feature = "optix")]
20+
// CudaBuilder::new(manifest_dir.join("kernels"))
21+
// .copy_to(out_path.join("kernels_optix.ptx"))
22+
// .build_args(&["--features", "optix"])
23+
// .build()
24+
// .unwrap();
2225
}

crates/optix/examples/path_tracer/kernels/Cargo.toml

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,8 @@ glam = { version = "0.30", default-features = false, features = ["libm", "cuda"]
99
enum_dispatch = "0.3.13"
1010
gpu_rand = { version = "0.1", path = "../../../../gpu_rand" }
1111
cust_core = { path = "../../../../cust_core", features = ["glam"] }
12-
optix_device = { path = "../../../../optix_device" }
12+
# See the OPTIX_DISABLED comment.
13+
# optix_device = { path = "../../../../optix_device", optional = true }
1314
approx = { version = "0.5" }
1415

1516
[lib]

crates/optix/examples/path_tracer/kernels/src/lib.rs

Lines changed: 11 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,8 @@ extern crate alloc;
55
pub mod hittable;
66
pub mod material;
77
pub mod math;
8-
pub mod optix;
8+
// See the OPTIX_DISABLED comment.
9+
// pub mod optix;
910
pub mod render;
1011
pub mod render_kernels;
1112
pub mod scene;
@@ -51,12 +52,13 @@ impl Ray {
5152
self.origin + t * self.dir
5253
}
5354

54-
pub fn from_optix() -> Self {
55-
use optix_device::intersection;
56-
57-
Self {
58-
dir: Vec3::from(intersection::ray_world_direction().to_array()),
59-
origin: Vec3::from(intersection::ray_world_origin().to_array()),
60-
}
61-
}
55+
// See the OPTIX_DISABLED comment.
56+
// pub fn from_optix() -> Self {
57+
// use optix_device::intersection;
58+
//
59+
// Self {
60+
// dir: Vec3::from(intersection::ray_world_direction().to_array()),
61+
// origin: Vec3::from(intersection::ray_world_origin().to_array()),
62+
// }
63+
// }
6264
}

crates/optix/examples/path_tracer/src/cuda/mod.rs

Lines changed: 57 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,9 @@ use imgui::Ui;
55

66
use std::time::Duration;
77

8-
use crate::{common::Camera, optix::OptixRenderer};
8+
use crate::common::Camera;
9+
// See the OPTIX_DISABLED comment.
10+
// use crate::optix::OptixRenderer;
911
use anyhow::Result;
1012
use cust::{
1113
error::CudaResult,
@@ -15,10 +17,11 @@ use cust::{
1517
prelude::*,
1618
};
1719
use glam::{U8Vec3, USizeVec2};
18-
use optix::{
19-
context::DeviceContext,
20-
denoiser::{Denoiser, DenoiserModelKind, Image, ImageFormat},
21-
};
20+
// See the OPTIX_DISABLED comment.
21+
// use optix::{
22+
// context::DeviceContext,
23+
// denoiser::{Denoiser, DenoiserModelKind, Image, ImageFormat},
24+
// };
2225
use path_tracer_kernels::scene::Scene;
2326
/// Seed for the random states
2427
pub const SEED: u64 = 932174513921034;
@@ -33,45 +36,51 @@ pub(crate) static PTX: &str = include_str!(concat!(env!("OUT_DIR"), "/kernels.pt
3336
pub struct CudaRenderer {
3437
stream: Stream,
3538
module: Module,
36-
denoiser: Denoiser,
37-
_optix_context: DeviceContext,
39+
// See the OPTIX_DISABLED comment.
40+
// denoiser: Denoiser,
41+
// _optix_context: DeviceContext,
3842
_context: Context,
3943

4044
buffers: CudaRendererBuffers,
4145
cpu_image: Vec<U8Vec3>,
42-
optix_renderer: OptixRenderer,
46+
// See the OPTIX_DISABLED comment.
47+
// optix_renderer: OptixRenderer,
4348
}
4449

4550
impl CudaRenderer {
4651
pub fn new(dimensions: USizeVec2, camera: &Camera, scene: &Scene) -> Result<Self> {
4752
let context = cust::quick_init()?;
48-
optix::init().unwrap();
49-
50-
let mut optix_context = DeviceContext::new(&context, false).unwrap();
53+
// See the OPTIX_DISABLED comment.
54+
// optix::init().unwrap();
55+
// let mut optix_context = DeviceContext::new(&context, false).unwrap();
5156

5257
let module = Module::from_ptx(PTX, &[]).unwrap();
5358
let stream = Stream::new(StreamFlags::NON_BLOCKING, None)?;
54-
let mut denoiser =
55-
Denoiser::new(&optix_context, DenoiserModelKind::Ldr, Default::default()).unwrap();
5659

57-
denoiser
58-
.setup_state(&stream, dimensions.x as u32, dimensions.y as u32, false)
59-
.unwrap();
60+
// See the OPTIX_DISABLED comment.
61+
// let mut denoiser =
62+
// Denoiser::new(&optix_context, DenoiserModelKind::Ldr, Default::default()).unwrap();
63+
// denoiser
64+
// .setup_state(&stream, dimensions.x as u32, dimensions.y as u32, false)
65+
// .unwrap();
6066

6167
let buffers = CudaRendererBuffers::new(dimensions, camera, scene)?;
6268
let cpu_image = vec![U8Vec3::ZERO; dimensions.element_product()];
6369

64-
let optix_renderer = OptixRenderer::new(&mut optix_context, &stream, scene)?;
70+
// See the OPTIX_DISABLED comment.
71+
// let optix_renderer = OptixRenderer::new(&mut optix_context, &stream, scene)?;
6572

6673
Ok(Self {
6774
_context: context,
68-
_optix_context: optix_context,
69-
denoiser,
75+
// See the OPTIX_DISABLED comment.
76+
// _optix_context: optix_context,
77+
// denoiser,
7078
module,
7179
stream,
7280
buffers,
7381
cpu_image,
74-
optix_renderer,
82+
// See the OPTIX_DISABLED comment.
83+
// optix_renderer,
7584
})
7685
}
7786

@@ -97,9 +106,11 @@ impl CudaRenderer {
97106
self.cpu_image
98107
.resize(new_size.element_product(), U8Vec3::ZERO);
99108

100-
self.denoiser
101-
.setup_state(&self.stream, new_size.x as u32, new_size.y as u32, false)
102-
.unwrap();
109+
// See the OPTIX_DISABLED comment.
110+
// self.denoiser
111+
// .setup_state(&self.stream, new_size.x as u32, new_size.y as u32, false)
112+
// .unwrap();
113+
103114
Ok(())
104115
}
105116

@@ -123,8 +134,9 @@ impl CudaRenderer {
123134
let stream = &self.stream;
124135

125136
let (blocks, threads) = self.launch_dimensions();
126-
let width = self.buffers.viewport.bounds.x as u32;
127-
let height = self.buffers.viewport.bounds.y as u32;
137+
// See the OPTIX_DISABLED comment.
138+
// let width = self.buffers.viewport.bounds.x as u32;
139+
// let height = self.buffers.viewport.bounds.y as u32;
128140

129141
let start = Event::new(EventFlags::DEFAULT)?;
130142
let denoising_stop = Event::new(EventFlags::DEFAULT)?;
@@ -144,23 +156,24 @@ impl CudaRenderer {
144156
}
145157

146158
let input_buf = if denoise {
147-
let input_image = Image::new(
148-
&self.buffers.scaled_buffer,
149-
ImageFormat::Float3,
150-
width,
151-
height,
152-
);
153-
154-
self.denoiser
155-
.invoke(
156-
stream,
157-
Default::default(),
158-
input_image,
159-
Default::default(),
160-
&mut self.buffers.denoised_buffer,
161-
)
162-
.unwrap();
163-
159+
// See the OPTIX_DISABLED comment.
160+
// let input_image = Image::new(
161+
// &self.buffers.scaled_buffer,
162+
// ImageFormat::Float3,
163+
// width,
164+
// height,
165+
// );
166+
//
167+
// self.denoiser
168+
// .invoke(
169+
// stream,
170+
// Default::default(),
171+
// input_image,
172+
// Default::default(),
173+
// &mut self.buffers.denoised_buffer,
174+
// )
175+
// .unwrap();
176+
//
164177
self.buffers.denoised_buffer.as_device_ptr()
165178
} else {
166179
self.buffers.scaled_buffer.as_device_ptr()
@@ -204,7 +217,8 @@ impl CudaRenderer {
204217
start.record(stream)?;
205218

206219
if use_optix {
207-
self.optix_renderer.render(stream, &mut self.buffers)?;
220+
// See the OPTIX_DISABLED comment.
221+
// self.optix_renderer.render(stream, &mut self.buffers)?;
208222
} else {
209223
unsafe {
210224
let scene = DeviceBox::new_async(

crates/optix/examples/path_tracer/src/main.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
11
pub mod common;
22
pub mod cpu;
33
pub mod cuda;
4-
pub mod optix;
4+
// See the OPTIX_DISABLED comment.
5+
// pub mod optix;
56
pub mod renderer;
67
pub mod viewer;
78

0 commit comments

Comments
 (0)