Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2,397 changes: 1,988 additions & 409 deletions Cargo.lock

Large diffs are not rendered by default.

18 changes: 14 additions & 4 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,18 @@ members = [
"crates/rustc_codegen_nvvm",
"crates/rustc_codegen_nvvm_macros",

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

"examples/gemm",
"examples/gemm/kernels",
Expand Down
3 changes: 2 additions & 1 deletion crates/optix/examples/path_tracer/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,8 @@ cust = { version = "0.3", path = "../../../cust", features = ["impl_glam"] }
image = "0.25.5"
path-tracer-kernels = { path = "kernels" }
gpu_rand = { version = "0.1", path = "../../../gpu_rand" }
optix = { version = "0.1", path = "../../../optix" }
# See the OPTIX_DISABLED comment.
# optix = { version = "0.1", path = "../../../optix" }
glium = "0.32.0"
glutin = "0.28.0"
imgui = "0.9.0"
Expand Down
13 changes: 8 additions & 5 deletions crates/optix/examples/path_tracer/build.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,12 @@ fn main() {
.copy_to(out_path.join("kernels.ptx"))
.build()
.unwrap();
CudaBuilder::new(manifest_dir.join("kernels"))
.copy_to(out_path.join("kernels_optix.ptx"))
.build_args(&["--features", "optix"])
.build()
.unwrap();

// See the OPTIX_DISABLED comment.
// #[cfg(feature = "optix")]
// CudaBuilder::new(manifest_dir.join("kernels"))
// .copy_to(out_path.join("kernels_optix.ptx"))
// .build_args(&["--features", "optix"])
// .build()
// .unwrap();
}
3 changes: 2 additions & 1 deletion crates/optix/examples/path_tracer/kernels/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,8 @@ glam = { version = "0.30", default-features = false, features = ["libm", "cuda"]
enum_dispatch = "0.3.13"
gpu_rand = { version = "0.1", path = "../../../../gpu_rand" }
cust_core = { path = "../../../../cust_core", features = ["glam"] }
optix_device = { path = "../../../../optix_device" }
# See the OPTIX_DISABLED comment.
# optix_device = { path = "../../../../optix_device", optional = true }
approx = { version = "0.5" }

[lib]
Expand Down
20 changes: 11 additions & 9 deletions crates/optix/examples/path_tracer/kernels/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,8 @@ extern crate alloc;
pub mod hittable;
pub mod material;
pub mod math;
pub mod optix;
// See the OPTIX_DISABLED comment.
// pub mod optix;
pub mod render;
pub mod render_kernels;
pub mod scene;
Expand Down Expand Up @@ -51,12 +52,13 @@ impl Ray {
self.origin + t * self.dir
}

pub fn from_optix() -> Self {
use optix_device::intersection;

Self {
dir: Vec3::from(intersection::ray_world_direction().to_array()),
origin: Vec3::from(intersection::ray_world_origin().to_array()),
}
}
// See the OPTIX_DISABLED comment.
// pub fn from_optix() -> Self {
// use optix_device::intersection;
//
// Self {
// dir: Vec3::from(intersection::ray_world_direction().to_array()),
// origin: Vec3::from(intersection::ray_world_origin().to_array()),
// }
// }
}
2 changes: 1 addition & 1 deletion crates/optix/examples/path_tracer/src/common.rs
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ impl CameraController {

self.yaw += delta.x * self.sensitivity;
self.pitch += delta.y * self.sensitivity;
self.pitch = self.pitch.min(89.0).max(-89.0);
self.pitch = self.pitch.clamp(-89.0, 89.0);

let yaw_rad = self.yaw.to_radians();
let pitch_rad = self.pitch.to_radians();
Expand Down
100 changes: 57 additions & 43 deletions crates/optix/examples/path_tracer/src/cuda/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,9 @@ use imgui::Ui;

use std::time::Duration;

use crate::{common::Camera, optix::OptixRenderer};
use crate::common::Camera;
// See the OPTIX_DISABLED comment.
// use crate::optix::OptixRenderer;
use anyhow::Result;
use cust::{
error::CudaResult,
Expand All @@ -15,10 +17,11 @@ use cust::{
prelude::*,
};
use glam::{U8Vec3, USizeVec2};
use optix::{
context::DeviceContext,
denoiser::{Denoiser, DenoiserModelKind, Image, ImageFormat},
};
// See the OPTIX_DISABLED comment.
// use optix::{
// context::DeviceContext,
// denoiser::{Denoiser, DenoiserModelKind, Image, ImageFormat},
// };
use path_tracer_kernels::scene::Scene;
/// Seed for the random states
pub const SEED: u64 = 932174513921034;
Expand All @@ -33,45 +36,51 @@ pub(crate) static PTX: &str = include_str!(concat!(env!("OUT_DIR"), "/kernels.pt
pub struct CudaRenderer {
stream: Stream,
module: Module,
denoiser: Denoiser,
_optix_context: DeviceContext,
// See the OPTIX_DISABLED comment.
// denoiser: Denoiser,
// _optix_context: DeviceContext,
_context: Context,

buffers: CudaRendererBuffers,
cpu_image: Vec<U8Vec3>,
optix_renderer: OptixRenderer,
// See the OPTIX_DISABLED comment.
// optix_renderer: OptixRenderer,
}

impl CudaRenderer {
pub fn new(dimensions: USizeVec2, camera: &Camera, scene: &Scene) -> Result<Self> {
let context = cust::quick_init()?;
optix::init().unwrap();

let mut optix_context = DeviceContext::new(&context, false).unwrap();
// See the OPTIX_DISABLED comment.
// optix::init().unwrap();
// let mut optix_context = DeviceContext::new(&context, false).unwrap();

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

denoiser
.setup_state(&stream, dimensions.x as u32, dimensions.y as u32, false)
.unwrap();
// See the OPTIX_DISABLED comment.
// let mut denoiser =
// Denoiser::new(&optix_context, DenoiserModelKind::Ldr, Default::default()).unwrap();
// denoiser
// .setup_state(&stream, dimensions.x as u32, dimensions.y as u32, false)
// .unwrap();

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

let optix_renderer = OptixRenderer::new(&mut optix_context, &stream, scene)?;
// See the OPTIX_DISABLED comment.
// let optix_renderer = OptixRenderer::new(&mut optix_context, &stream, scene)?;

Ok(Self {
_context: context,
_optix_context: optix_context,
denoiser,
// See the OPTIX_DISABLED comment.
// _optix_context: optix_context,
// denoiser,
module,
stream,
buffers,
cpu_image,
optix_renderer,
// See the OPTIX_DISABLED comment.
// optix_renderer,
})
}

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

self.denoiser
.setup_state(&self.stream, new_size.x as u32, new_size.y as u32, false)
.unwrap();
// See the OPTIX_DISABLED comment.
// self.denoiser
// .setup_state(&self.stream, new_size.x as u32, new_size.y as u32, false)
// .unwrap();

Ok(())
}

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

let (blocks, threads) = self.launch_dimensions();
let width = self.buffers.viewport.bounds.x as u32;
let height = self.buffers.viewport.bounds.y as u32;
// See the OPTIX_DISABLED comment.
// let width = self.buffers.viewport.bounds.x as u32;
// let height = self.buffers.viewport.bounds.y as u32;

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

let input_buf = if denoise {
let input_image = Image::new(
&self.buffers.scaled_buffer,
ImageFormat::Float3,
width,
height,
);

self.denoiser
.invoke(
stream,
Default::default(),
input_image,
Default::default(),
&mut self.buffers.denoised_buffer,
)
.unwrap();

// See the OPTIX_DISABLED comment.
// let input_image = Image::new(
// &self.buffers.scaled_buffer,
// ImageFormat::Float3,
// width,
// height,
// );
//
// self.denoiser
// .invoke(
// stream,
// Default::default(),
// input_image,
// Default::default(),
// &mut self.buffers.denoised_buffer,
// )
// .unwrap();
//
self.buffers.denoised_buffer.as_device_ptr()
} else {
self.buffers.scaled_buffer.as_device_ptr()
Expand Down Expand Up @@ -204,7 +217,8 @@ impl CudaRenderer {
start.record(stream)?;

if use_optix {
self.optix_renderer.render(stream, &mut self.buffers)?;
// See the OPTIX_DISABLED comment.
// self.optix_renderer.render(stream, &mut self.buffers)?;
} else {
unsafe {
let scene = DeviceBox::new_async(
Expand Down
3 changes: 2 additions & 1 deletion crates/optix/examples/path_tracer/src/main.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
pub mod common;
pub mod cpu;
pub mod cuda;
pub mod optix;
// See the OPTIX_DISABLED comment.
// pub mod optix;
pub mod renderer;
pub mod viewer;

Expand Down
6 changes: 3 additions & 3 deletions crates/optix/examples/path_tracer/src/viewer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -191,7 +191,7 @@ impl ViewerRenderer {
let out = ui
.window("crab")
.size([300.0, 300.0], Condition::FirstUseEver)
.build(|| renderer.render(&ui))
.build(|| renderer.render(ui))
.unwrap();

let raw =
Expand All @@ -217,15 +217,15 @@ impl ViewerRenderer {
target
.draw(
&*vertex_buffer,
&NoIndices(PrimitiveType::TrianglesList),
NoIndices(PrimitiveType::TrianglesList),
image_program,
&uniforms,
&Default::default(),
)
.unwrap();

let gl_window = display.gl_window();
platform.prepare_render(&ui, gl_window.window());
platform.prepare_render(ui, gl_window.window());

let draw_data = self.imgui_ctx.render();
imgui_renderer.render(&mut target, draw_data).unwrap();
Expand Down
2 changes: 0 additions & 2 deletions examples/i128_demo/kernels/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
#![no_std]

use cuda_std::prelude::*;

#[kernel]
Expand Down
Loading