Skip to content

Commit e818208

Browse files
committed
Re-enable path-tracer.
In Rust-GPU#313 I disabled all the code relying on OptiX. I subsequently learned that the path-tracer's use of OptiX is optional. This commit re-enables it, and makes the optionality (via features) more strict so it works on systems without OptiX. Note: If you were rewriting this from scratch you'd probably group together the OptiX-dependent code pieces more nicely, but I just scattered `#[cfg(feature = "optix")]` attributes around to keep the modifications as small as possible.
1 parent e4e5ed3 commit e818208

8 files changed

Lines changed: 2106 additions & 418 deletions

File tree

Cargo.lock

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

Cargo.toml

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,8 @@ members = [
1919
"crates/rustc_codegen_nvvm_macros",
2020

2121
# 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.
22+
# OptiX SDK is a pain to install. The `path-tracer` crates are an exception because their OptiX
23+
# use is behind a feature, which is off by default.
2324
# "crates/optix",
2425
# "crates/optix_device",
2526
# "crates/optix_device_macros",
@@ -30,8 +31,8 @@ members = [
3031
# "crates/optix/examples/ex03_window",
3132
# "crates/optix/examples/ex04_mesh",
3233
# "crates/optix/examples/ex04_mesh/kernels",
33-
# "crates/optix/examples/path_tracer",
34-
# "crates/optix/examples/path_tracer/kernels",
34+
"crates/optix/examples/path_tracer",
35+
"crates/optix/examples/path_tracer/kernels",
3536

3637
"examples/gemm",
3738
"examples/gemm/kernels",

crates/optix/examples/path_tracer/Cargo.toml

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ 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+
optix = { version = "0.1", path = "../../../optix", optional = true }
1414
glium = "0.32.0"
1515
glutin = "0.28.0"
1616
imgui = "0.9.0"
@@ -22,3 +22,6 @@ anyhow = "1.0.53"
2222

2323
[build-dependencies]
2424
cuda_builder = { workspace = true, default-features = false }
25+
26+
[features]
27+
optix = ["dep:optix", "path-tracer-kernels/optix"]

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+
// njn: temp
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 & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,11 +9,11 @@ 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+
optix_device = { path = "../../../../optix_device", optional = true }
1313
approx = { version = "0.5" }
1414

1515
[lib]
1616
crate-type = ["cdylib", "rlib"]
1717

1818
[features]
19-
optix = []
19+
optix = ["dep:optix_device"]

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

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ extern crate alloc;
55
pub mod hittable;
66
pub mod material;
77
pub mod math;
8+
#[cfg(feature = "optix")]
89
pub mod optix;
910
pub mod render;
1011
pub mod render_kernels;
@@ -51,6 +52,7 @@ impl Ray {
5152
self.origin + t * self.dir
5253
}
5354

55+
#[cfg(feature = "optix")]
5456
pub fn from_optix() -> Self {
5557
use optix_device::intersection;
5658

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

Lines changed: 47 additions & 19 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+
#[cfg(feature = "optix")]
10+
use crate::optix::OptixRenderer;
911
use anyhow::Result;
1012
use cust::{
1113
error::CudaResult,
@@ -15,6 +17,7 @@ use cust::{
1517
prelude::*,
1618
};
1719
use glam::{U8Vec3, USizeVec2};
20+
#[cfg(feature = "optix")]
1821
use optix::{
1922
context::DeviceContext,
2023
denoiser::{Denoiser, DenoiserModelKind, Image, ImageFormat},
@@ -33,44 +36,55 @@ pub(crate) static PTX: &str = include_str!(concat!(env!("OUT_DIR"), "/kernels.pt
3336
pub struct CudaRenderer {
3437
stream: Stream,
3538
module: Module,
39+
#[cfg(feature = "optix")]
3640
denoiser: Denoiser,
41+
#[cfg(feature = "optix")]
3742
_optix_context: DeviceContext,
3843
_context: Context,
3944

4045
buffers: CudaRendererBuffers,
4146
cpu_image: Vec<U8Vec3>,
47+
#[cfg(feature = "optix")]
4248
optix_renderer: OptixRenderer,
4349
}
4450

4551
impl CudaRenderer {
4652
pub fn new(dimensions: USizeVec2, camera: &Camera, scene: &Scene) -> Result<Self> {
4753
let context = cust::quick_init()?;
54+
#[cfg(feature = "optix")]
4855
optix::init().unwrap();
4956

57+
#[cfg(feature = "optix")]
5058
let mut optix_context = DeviceContext::new(&context, false).unwrap();
5159

5260
let module = Module::from_ptx(PTX, &[]).unwrap();
5361
let stream = Stream::new(StreamFlags::NON_BLOCKING, None)?;
62+
63+
#[cfg(feature = "optix")]
5464
let mut denoiser =
5565
Denoiser::new(&optix_context, DenoiserModelKind::Ldr, Default::default()).unwrap();
56-
66+
#[cfg(feature = "optix")]
5767
denoiser
5868
.setup_state(&stream, dimensions.x as u32, dimensions.y as u32, false)
5969
.unwrap();
6070

6171
let buffers = CudaRendererBuffers::new(dimensions, camera, scene)?;
6272
let cpu_image = vec![U8Vec3::ZERO; dimensions.element_product()];
6373

74+
#[cfg(feature = "optix")]
6475
let optix_renderer = OptixRenderer::new(&mut optix_context, &stream, scene)?;
6576

6677
Ok(Self {
6778
_context: context,
79+
#[cfg(feature = "optix")]
6880
_optix_context: optix_context,
81+
#[cfg(feature = "optix")]
6982
denoiser,
7083
module,
7184
stream,
7285
buffers,
7386
cpu_image,
87+
#[cfg(feature = "optix")]
7488
optix_renderer,
7589
})
7690
}
@@ -97,9 +111,11 @@ impl CudaRenderer {
97111
self.cpu_image
98112
.resize(new_size.element_product(), U8Vec3::ZERO);
99113

114+
#[cfg(feature = "optix")]
100115
self.denoiser
101116
.setup_state(&self.stream, new_size.x as u32, new_size.y as u32, false)
102117
.unwrap();
118+
103119
Ok(())
104120
}
105121

@@ -123,7 +139,9 @@ impl CudaRenderer {
123139
let stream = &self.stream;
124140

125141
let (blocks, threads) = self.launch_dimensions();
142+
#[cfg(feature = "optix")]
126143
let width = self.buffers.viewport.bounds.x as u32;
144+
#[cfg(feature = "optix")]
127145
let height = self.buffers.viewport.bounds.y as u32;
128146

129147
let start = Event::new(EventFlags::DEFAULT)?;
@@ -144,24 +162,30 @@ impl CudaRenderer {
144162
}
145163

146164
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();
165+
#[cfg(not(feature = "optix"))]
166+
unreachable!();
167+
168+
#[cfg(feature = "optix")]
169+
{
170+
let input_image = Image::new(
171+
&self.buffers.scaled_buffer,
172+
ImageFormat::Float3,
173+
width,
174+
height,
175+
);
176+
177+
self.denoiser
178+
.invoke(
179+
stream,
180+
Default::default(),
181+
input_image,
182+
Default::default(),
183+
&mut self.buffers.denoised_buffer,
184+
)
185+
.unwrap();
163186

164-
self.buffers.denoised_buffer.as_device_ptr()
187+
self.buffers.denoised_buffer.as_device_ptr()
188+
}
165189
} else {
166190
self.buffers.scaled_buffer.as_device_ptr()
167191
};
@@ -204,6 +228,10 @@ impl CudaRenderer {
204228
start.record(stream)?;
205229

206230
if use_optix {
231+
#[cfg(not(feature = "optix"))]
232+
unreachable!();
233+
234+
#[cfg(feature = "optix")]
207235
self.optix_renderer.render(stream, &mut self.buffers)?;
208236
} else {
209237
unsafe {

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
pub mod common;
22
pub mod cpu;
33
pub mod cuda;
4+
#[cfg(feature = "optix")]
45
pub mod optix;
56
pub mod renderer;
67
pub mod viewer;

0 commit comments

Comments
 (0)