├── .gitignore ├── Cargo.toml ├── optix-derive ├── Cargo.toml └── src │ └── lib.rs ├── optix-sys ├── Cargo.toml ├── build.rs ├── cuda_wrapper.rs ├── examples │ └── s01_hello │ │ └── main.rs ├── optix_stubs-capi │ ├── CMakeLists.txt │ └── optix_stubs.c ├── optix_wrapper.rs └── src │ ├── cuda_sys.rs │ ├── cuda_wrapper.h │ ├── lib.rs │ └── optix_wrapper.h ├── optix ├── Cargo.toml ├── examples │ ├── 02_pipeline │ │ ├── LaunchParams.h │ │ ├── devicePrograms.cu │ │ ├── main.rs │ │ └── sample_renderer.rs │ ├── 03_window │ │ ├── devicePrograms.cu │ │ ├── gl_util.rs │ │ ├── main.rs │ │ └── sample_renderer.rs │ ├── 04_mesh │ │ ├── devicePrograms.cu │ │ ├── gl_util.rs │ │ ├── main.rs │ │ └── sample_renderer.rs │ ├── 05_sbtdata │ │ ├── devicePrograms.cu │ │ ├── gl_util.rs │ │ ├── main.rs │ │ └── sample_renderer.rs │ ├── 06_multimesh │ │ ├── devicePrograms.cu │ │ ├── gl_util.rs │ │ ├── main.rs │ │ └── sample_renderer.rs │ ├── 07_obj │ │ ├── devicePrograms.cu │ │ ├── gl_util.rs │ │ ├── main.rs │ │ └── sample_renderer.rs │ ├── 08_texture │ │ ├── devicePrograms.cu │ │ ├── gl_util.rs │ │ ├── main.rs │ │ └── sample_renderer.rs │ ├── 09_shadow │ │ ├── LaunchParams.h │ │ ├── devicePrograms.cu │ │ ├── gl_util.rs │ │ ├── main.rs │ │ └── sample_renderer.rs │ ├── 10_softshadow │ │ ├── devicePrograms.cu │ │ ├── gl_util.rs │ │ ├── main.rs │ │ └── sample_renderer.rs │ ├── common │ │ ├── lcg.h │ │ ├── types.h │ │ └── vec.h │ └── data │ │ ├── sponza.mtl │ │ ├── sponza.obj │ │ └── sponzaMaps │ │ ├── 00_skap.JPG │ │ ├── 01_STUB-bump.jpg │ │ ├── 01_STUB.JPG │ │ ├── 01_S_ba.JPG │ │ ├── 01_S_kap-bump.jpg │ │ ├── 01_S_kap.JPG │ │ ├── 01_St_kp-bump.jpg │ │ ├── 01_St_kp.JPG │ │ ├── KAMEN-bump.jpg │ │ ├── KAMEN-stup.JPG │ │ ├── KAMEN.JPG │ │ ├── prozor1.JPG │ │ ├── reljef-bump.jpg │ │ ├── reljef.JPG │ │ ├── sp_luk-bump.JPG │ │ ├── sp_luk.JPG │ │ ├── vrata_ko.JPG │ │ ├── vrata_kr.JPG │ │ ├── x01_st-bump.jpg │ │ └── x01_st.JPG └── src │ ├── acceleration.rs │ ├── buffer.rs │ ├── cuda │ ├── allocator.rs │ ├── array.rs │ ├── buffer.rs │ ├── context.rs │ ├── error.rs │ ├── mod.rs │ ├── nvrtc.rs │ ├── stream.rs │ └── texture_object.rs │ ├── device_context.rs │ ├── error.rs │ ├── instance.rs │ ├── lib.rs │ ├── math.rs │ ├── module.rs │ ├── pipeline.rs │ ├── program_group.rs │ ├── shader_binding_table.rs │ └── texture.rs └── rustfmt.toml /.gitignore: -------------------------------------------------------------------------------- 1 | /target 2 | **/*.rs.bk 3 | Cargo.lock 4 | *.png 5 | build-settings.toml 6 | ptx_path.toml 7 | .ccls-cache 8 | .vscode 9 | 10 | 11 | #Added by cargo 12 | # 13 | #already existing elements are commented out 14 | 15 | #/target 16 | #**/*.rs.bk 17 | 18 | 19 | #Added by cargo 20 | # 21 | #already existing elements are commented out 22 | 23 | #/target 24 | #**/*.rs.bk 25 | #Cargo.lock 26 | -------------------------------------------------------------------------------- /Cargo.toml: -------------------------------------------------------------------------------- 1 | [workspace] 2 | 3 | members = [ 4 | "optix-sys", 5 | "optix-derive", 6 | "optix" 7 | ] -------------------------------------------------------------------------------- /optix-derive/Cargo.toml: -------------------------------------------------------------------------------- 1 | [package] 2 | name = "optix-derive" 3 | version = "0.1.0" 4 | authors = ["Anders Langlands "] 5 | edition = "2018" 6 | 7 | # See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html 8 | [lib] 9 | proc-macro = true 10 | 11 | [dependencies] 12 | syn = "1.0.5" 13 | quote = "1.0.2" 14 | proc-macro2 = "1.0.1" 15 | optix-sys = {path="../optix-sys"} 16 | -------------------------------------------------------------------------------- /optix-derive/src/lib.rs: -------------------------------------------------------------------------------- 1 | #![feature(proc_macro_diagnostic)] 2 | 3 | extern crate proc_macro; 4 | use proc_macro::TokenStream; 5 | use quote::quote; 6 | use syn::{ 7 | punctuated::Punctuated, token::Comma, Data, DataEnum, DataStruct, 8 | DeriveInput, Field, Fields, Variant, 9 | }; 10 | 11 | #[proc_macro_attribute] 12 | pub fn device_shared(_attr: TokenStream, item: TokenStream) -> TokenStream { 13 | let input = syn::parse_macro_input!(item as DeriveInput); 14 | 15 | let result = match &input.data { 16 | Data::Struct(DataStruct { 17 | fields: Fields::Named(fields), 18 | .. 19 | }) => do_struct(&input, &fields.named), 20 | Data::Enum(DataEnum { variants, .. }) => do_enum(&input, &variants), 21 | _ => unimplemented!(), 22 | }; 23 | 24 | // panic!("{}", result); 25 | 26 | result.into() 27 | } 28 | 29 | fn do_enum( 30 | input: &DeriveInput, 31 | variants: &Punctuated, 32 | ) -> proc_macro2::TokenStream { 33 | let name = &input.ident; 34 | 35 | let variant_idents: Vec<_> = variants 36 | .iter() 37 | .map(|variant| format!("{}", &variant.ident)) 38 | .collect(); 39 | 40 | let s_name = format!("{}", name); 41 | 42 | let result = quote! { 43 | #[repr(u32)] 44 | #[allow(dead_code)] 45 | #[derive(Copy, Clone, PartialEq, PartialOrd, Debug)] 46 | #input 47 | 48 | impl DeviceShareable for #name { 49 | type Target = u32; 50 | 51 | fn to_device(&self) -> u32 { 52 | *self as u32 53 | } 54 | 55 | fn cuda_type() -> String { 56 | #s_name.into() 57 | } 58 | 59 | fn cuda_decl() -> String { 60 | let mut s = format!("enum class {}: u32 {{\n", #s_name); 61 | 62 | #( 63 | s = format!("{} {},\n", s, #variant_idents); 64 | )* 65 | 66 | s = format!("{} }};", s); 67 | 68 | s 69 | } 70 | 71 | fn zero() -> Self::Target { 72 | 0 73 | } 74 | } 75 | }; 76 | 77 | result 78 | } 79 | 80 | fn do_struct( 81 | input: &DeriveInput, 82 | fields: &Punctuated, 83 | ) -> proc_macro2::TokenStream { 84 | let name = &input.ident; 85 | let d_name = proc_macro2::Ident::new( 86 | &format!("{}D", input.ident), 87 | proc_macro2::Span::call_site(), 88 | ); 89 | let generics = &input.generics; 90 | let where_clause = &input.generics.where_clause; 91 | 92 | let field_vis = fields.iter().map(|field| &field.vis); 93 | let field_name = fields.iter().map(|field| &field.ident); 94 | let field_type = fields.iter().map(|field| &field.ty); 95 | 96 | let result = quote! { 97 | // output original struct first 98 | #input 99 | 100 | // now device-compatible struct 101 | #[repr(C)] 102 | #[derive(Copy, Clone)] 103 | pub struct #d_name#generics #where_clause { 104 | #( 105 | #field_vis #field_name: <#field_type as DeviceShareable>::Target, 106 | )* 107 | } 108 | }; 109 | 110 | let s_field_name = fields 111 | .iter() 112 | .map(|field| { 113 | format!( 114 | "{}", 115 | match &field.ident { 116 | Some(s) => s, 117 | None => panic!("no ident"), 118 | } 119 | ) 120 | }) 121 | .collect::>(); 122 | 123 | let s_name = format!("{}", name); 124 | let field_type = fields.iter().map(|field| &field.ty); 125 | 126 | let cuda_decl = quote! { 127 | fn cuda_type() -> String { 128 | #s_name.into() 129 | } 130 | fn cuda_decl() -> String { 131 | let mut s = format!("struct {} {{", #s_name); 132 | #( 133 | s = format!("{} {} {};", s, <#field_type as DeviceShareable>::cuda_type(), #s_field_name); 134 | )* 135 | s = format!("{} }};", s); 136 | s 137 | } 138 | }; 139 | 140 | let field_name = fields.iter().map(|field| &field.ident); 141 | let field_type = fields.iter().map(|field| &field.ty); 142 | let zero = quote! { 143 | fn zero() -> Self::Target { 144 | #d_name { 145 | #( 146 | #field_name: <#field_type as DeviceShareable>::zero(), 147 | )* 148 | } 149 | } 150 | }; 151 | 152 | let field_name = fields.iter().map(|field| &field.ident); 153 | 154 | let result = quote! { 155 | #result 156 | 157 | // now impl DeviceShareable for the original struct 158 | impl#generics DeviceShareable for #name#generics #where_clause { 159 | type Target = #d_name#generics; 160 | fn to_device(&self) -> Self::Target { 161 | #d_name { 162 | #( 163 | #field_name: self.#field_name.to_device(), 164 | )* 165 | } 166 | } 167 | #cuda_decl 168 | #zero 169 | } 170 | }; 171 | 172 | // panic!("{}", result); 173 | 174 | result 175 | } 176 | 177 | #[cfg(test)] 178 | mod tests { 179 | #[test] 180 | fn it_works() { 181 | assert_eq!(2 + 2, 4); 182 | } 183 | } 184 | -------------------------------------------------------------------------------- /optix-sys/Cargo.toml: -------------------------------------------------------------------------------- 1 | [package] 2 | name = "optix-sys" 3 | version = "0.1.0" 4 | authors = ["Anders Langlands "] 5 | edition = "2018" 6 | 7 | # See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html 8 | 9 | [dev-dependencies] 10 | nalgebra-glm = "0.4.0" 11 | 12 | [build-dependencies] 13 | bindgen = "0.51.0" 14 | cmake = "0.1.41" 15 | 16 | [dependencies] 17 | thiserror = "1.0" 18 | -------------------------------------------------------------------------------- /optix-sys/build.rs: -------------------------------------------------------------------------------- 1 | use std::path::{Path, PathBuf}; 2 | 3 | fn main() { 4 | let optix_root = std::env::var("OPTIX_ROOT") 5 | .expect("Could not get OPTIX_ROOT from environment"); 6 | let cuda_root = std::env::var("CUDA_ROOT") 7 | .expect("Could not get CUDA_ROOT from environment"); 8 | 9 | bindgen_cuda(&cuda_root); 10 | bindgen_optix(&optix_root, &cuda_root); 11 | } 12 | 13 | fn get_modified_time(path: &Path) -> std::time::SystemTime { 14 | std::fs::metadata(path).unwrap().modified().unwrap() 15 | } 16 | 17 | fn bindgen_optix(optix_root: &str, cuda_root: &str) { 18 | let out_path = PathBuf::from(std::env::var("OUT_DIR").unwrap()) 19 | .join("optix_wrapper.rs"); 20 | 21 | let header_path = 22 | PathBuf::from(std::env::var("CARGO_MANIFEST_DIR").unwrap()) 23 | .join("src") 24 | .join("optix_wrapper.h"); 25 | 26 | if !out_path.is_file() 27 | || get_modified_time(&out_path) < get_modified_time(&header_path) 28 | { 29 | let bindings = bindgen::Builder::default() 30 | .header("src/optix_wrapper.h") 31 | .clang_arg(format!("-I{}/include", optix_root)) 32 | .clang_arg(format!("-I{}/include", cuda_root)) 33 | .whitelist_recursively(false) 34 | .whitelist_type("Optix.*") 35 | .whitelist_type("RaygenRecord") 36 | .whitelist_type("MissRecord") 37 | .whitelist_type("HitgroupRecord") 38 | .blacklist_type("OptixBuildInput") 39 | .whitelist_function("optix.*") 40 | .whitelist_var("OptixSbtRecordHeaderSize") 41 | .whitelist_var("OptixSbtRecordAlignment") 42 | .whitelist_var("OptixAccelBufferByteAlignment") 43 | .whitelist_var("OptixInstanceByteAlignment") 44 | .whitelist_var("OptixAabbBufferByteAlignment") 45 | .whitelist_var("OptixGeometryTransformByteAlignment") 46 | .whitelist_var("OptixTransformByteAlignment") 47 | .layout_tests(false) 48 | .generate_comments(false) 49 | .rustified_enum("OptixResult") 50 | .constified_enum_module("OptixCompileOptimizationLevel") 51 | .constified_enum_module("OptixCompileDebugLevel") 52 | .constified_enum_module("OptixTraversableGraphFlags") 53 | .constified_enum_module("OptixExceptionFlags") 54 | .constified_enum_module("OptixProgramGroupKind") 55 | .rustified_enum("GeometryFlags") 56 | .rustified_enum("OptixGeometryFlags") 57 | .rustified_enum("OptixVertexFormat") 58 | .rustified_enum("OptixIndicesFormat") 59 | .rust_target(bindgen::RustTarget::Nightly) 60 | .rustfmt_bindings(true) 61 | .generate() 62 | .expect("Unable to generate optix bindings"); 63 | 64 | let dbg_path = std::path::PathBuf::from( 65 | std::env::var("CARGO_MANIFEST_DIR").unwrap(), 66 | ); 67 | bindings 68 | .write_to_file(dbg_path.join("optix_wrapper.rs")) 69 | .expect("Couldn't write bindings!"); 70 | 71 | bindings 72 | .write_to_file(out_path) 73 | .expect("Couldn't write bindings!"); 74 | } 75 | 76 | let dst_capi = cmake::Config::new("optix_stubs-capi") 77 | .define("INC_OPTIX", &format!("{}/include", optix_root)) 78 | .define("INC_CUDA", &format!("{}/include", cuda_root)) 79 | .always_configure(false) 80 | .build(); 81 | 82 | println!("cargo:rustc-link-search=native={}", dst_capi.display()); 83 | println!("cargo:rustc-link-lib=static=optix_stubs-capi"); 84 | } 85 | 86 | fn bindgen_cuda(cuda_root: &str) { 87 | let out_path = std::path::PathBuf::from(std::env::var("OUT_DIR").unwrap()) 88 | .join("cuda_wrapper.rs"); 89 | 90 | let header_path = 91 | std::path::PathBuf::from(std::env::var("CARGO_MANIFEST_DIR").unwrap()) 92 | .join("src") 93 | .join("cuda_wrapper.h"); 94 | 95 | if !out_path.is_file() 96 | || get_modified_time(&out_path) < get_modified_time(&header_path) 97 | { 98 | let bindings = bindgen::Builder::default() 99 | .header("src/cuda_wrapper.h") 100 | .clang_arg(format!("-I{}/include", cuda_root)) 101 | .whitelist_recursively(false) 102 | .whitelist_type("CU.*") 103 | .whitelist_type("Nvrtc.*") 104 | .whitelist_type("_nvrtc.*") 105 | .whitelist_type("nvrtc.*") 106 | .whitelist_type("cuda.*") 107 | .whitelist_type("cuuint.*") 108 | .whitelist_type("textureReference") 109 | .whitelist_type("surfaceReference") 110 | .whitelist_type("dim3") 111 | .whitelist_function("cuda.*") 112 | .whitelist_function("cu.*") 113 | .whitelist_function("nvrtc.*") 114 | .blacklist_type("cudaResourceDesc") 115 | .layout_tests(false) 116 | .constified_enum_module("CUresult") 117 | .constified_enum_module("nvrtcResult") 118 | .constified_enum_module("cudaMemcpyKind") 119 | .constified_enum_module("cudaError_enum") 120 | .constified_enum_module("cudaError") 121 | .generate() 122 | .expect("Unable to generate cuda bindings"); 123 | 124 | let dbg_path = std::path::PathBuf::from( 125 | std::env::var("CARGO_MANIFEST_DIR").unwrap(), 126 | ); 127 | bindings 128 | .write_to_file(dbg_path.join("cuda_wrapper.rs")) 129 | .expect("Couldn't write bindings!"); 130 | 131 | bindings 132 | .write_to_file(out_path) 133 | .expect("Couldn't write bindings!"); 134 | } 135 | 136 | println!( 137 | "cargo:rustc-link-search=native={}", 138 | format!("{}/lib64", cuda_root) 139 | ); 140 | println!("cargo:rustc-link-lib=dylib=cudart"); 141 | println!("cargo:rustc-link-lib=dylib=cuda"); 142 | println!("cargo:rustc-link-lib=dylib=nvrtc"); 143 | } 144 | -------------------------------------------------------------------------------- /optix-sys/examples/s01_hello/main.rs: -------------------------------------------------------------------------------- 1 | use optix_sys::cuda_sys::{cudaFree, cudaGetDeviceCount}; 2 | use optix_sys::{optixInit, OptixResult}; 3 | 4 | fn main() { 5 | unsafe { 6 | cudaFree(std::ptr::null_mut()); 7 | let mut num_devices = 0i32; 8 | cudaGetDeviceCount(&mut num_devices as *mut i32); 9 | if num_devices == 0 { 10 | panic!("No CUDA devices found"); 11 | } 12 | println!("Found {} CUDA devices", num_devices); 13 | 14 | let result = optixInit(); 15 | if result != OptixResult::OPTIX_SUCCESS { 16 | panic!("OptiX init failed!"); 17 | } 18 | 19 | println!("OptiX initialized successfully! Yay!"); 20 | } 21 | } 22 | -------------------------------------------------------------------------------- /optix-sys/optix_stubs-capi/CMakeLists.txt: -------------------------------------------------------------------------------- 1 | cmake_minimum_required(VERSION 3.5) 2 | project(optix_stubs-capi) 3 | 4 | set(CMAKE_EXPORT_COMPILE_COMMANDS ON) 5 | 6 | add_library(optix_stubs-capi STATIC 7 | optix_stubs.c 8 | ) 9 | 10 | target_include_directories(optix_stubs-capi PUBLIC ${INC_OPTIX} ${INC_CUDA}) 11 | # target_link_libraries(optix_stubs-capi PUBLIC ${LIB_CUDA}/lib/libosdCPU.so ${LIB_OPTIX}/lib/libosdGPU.so) 12 | install(TARGETS optix_stubs-capi DESTINATION ${CMAKE_INSTALL_PREFIX}) 13 | -------------------------------------------------------------------------------- /optix-sys/src/cuda_wrapper.h: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include -------------------------------------------------------------------------------- /optix-sys/src/optix_wrapper.h: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | static const size_t OptixSbtRecordHeaderSize = OPTIX_SBT_RECORD_HEADER_SIZE; 4 | static const size_t OptixSbtRecordAlignment = OPTIX_SBT_RECORD_ALIGNMENT; 5 | static const size_t OptixAccelBufferByteAlignment = 6 | OPTIX_ACCEL_BUFFER_BYTE_ALIGNMENT; 7 | static const size_t OptixInstanceByteAlignment = OPTIX_INSTANCE_BYTE_ALIGNMENT; 8 | static const size_t OptixAabbBufferByteAlignment = 9 | OPTIX_AABB_BUFFER_BYTE_ALIGNMENT; 10 | static const size_t OptixGeometryTransformByteAlignment = 11 | OPTIX_GEOMETRY_TRANSFORM_BYTE_ALIGNMENT; 12 | static const size_t OptixTransformByteAlignment = 13 | OPTIX_TRANSFORM_BYTE_ALIGNMENT; 14 | 15 | /** 16 | *
17 | */ 18 | enum GeometryFlags { 19 | None = OPTIX_GEOMETRY_FLAG_NONE, 20 | DisableAnyHit = OPTIX_GEOMETRY_FLAG_DISABLE_ANYHIT, 21 | RequireSingleAnyHitCall = OPTIX_GEOMETRY_FLAG_REQUIRE_SINGLE_ANYHIT_CALL 22 | }; -------------------------------------------------------------------------------- /optix/Cargo.toml: -------------------------------------------------------------------------------- 1 | [package] 2 | name = "optix" 3 | version = "0.1.0" 4 | authors = ["Anders Langlands "] 5 | edition = "2018" 6 | 7 | # See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html 8 | 9 | [dependencies] 10 | optix-sys = {path="../optix-sys"} 11 | bitflags = "1.1.0" 12 | gl = "0.13.0" 13 | imath = {path="../../imath-rs", optional=true} 14 | cfg-if = "0.1.10" 15 | nalgebra-glm = {version = "0.5.0", optional=true} 16 | nalgebra = {version = "0.19.0", optional=true} 17 | log = "0.4.8" 18 | thiserror = "1.0" 19 | bitfield = "0.13.2" 20 | ustr = "0.2" 21 | 22 | [dev-dependencies] 23 | optix-derive = {path="../optix-derive"} 24 | glfw = "0.32.0" 25 | tobj = "0.1.10" 26 | image = "0.22.2" 27 | enum_primitive = "0.1.1" 28 | num = "0.2.0" 29 | 30 | [features] 31 | default=["math-nalgebra"] 32 | math-imath = ["imath"] 33 | math-nalgebra = ["nalgebra-glm", "nalgebra"] 34 | 35 | -------------------------------------------------------------------------------- /optix/examples/02_pipeline/LaunchParams.h: -------------------------------------------------------------------------------- 1 | // ======================================================================== // 2 | // Copyright 2018-2019 Ingo Wald // 3 | // // 4 | // Licensed under the Apache License, Version 2.0 (the "License"); // 5 | // you may not use this file except in compliance with the License. // 6 | // You may obtain a copy of the License at // 7 | // // 8 | // http://www.apache.org/licenses/LICENSE-2.0 // 9 | // // 10 | // Unless required by applicable law or agreed to in writing, software // 11 | // distributed under the License is distributed on an "AS IS" BASIS, // 12 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. // 13 | // See the License for the specific language governing permissions and // 14 | // limitations under the License. // 15 | // ======================================================================== // 16 | 17 | #pragma once 18 | 19 | #include "vec.h" 20 | 21 | namespace osc { 22 | 23 | typedef unsigned int uint32_t; 24 | struct LaunchParams { 25 | int frameID{0}; 26 | uint32_t* colorBuffer; 27 | V2i32 fbSize; 28 | }; 29 | 30 | } // namespace osc 31 | -------------------------------------------------------------------------------- /optix/examples/02_pipeline/devicePrograms.cu: -------------------------------------------------------------------------------- 1 | // ======================================================================== // 2 | // Copyright 2018-2019 Ingo Wald // 3 | // // 4 | // Licensed under the Apache License, Version 2.0 (the "License"); // 5 | // you may not use this file except in compliance with the License. // 6 | // You may obtain a copy of the License at // 7 | // // 8 | // http://www.apache.org/licenses/LICENSE-2.0 // 9 | // // 10 | // Unless required by applicable law or agreed to in writing, software // 11 | // distributed under the License is distributed on an "AS IS" BASIS, // 12 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. // 13 | // See the License for the specific language governing permissions and // 14 | // limitations under the License. // 15 | // ======================================================================== // 16 | 17 | #include 18 | 19 | #include "LaunchParams.h" 20 | 21 | using namespace osc; 22 | 23 | namespace osc { 24 | 25 | /*! launch parameters in constant memory, filled in by optix upon 26 | optixLaunch (this gets filled in from the buffer we pass to 27 | optixLaunch) */ 28 | extern "C" __constant__ LaunchParams optixLaunchParams; 29 | 30 | //------------------------------------------------------------------------------ 31 | // closest hit and anyhit programs for radiance-type rays. 32 | // 33 | // Note eventually we will have to create one pair of those for each 34 | // ray type and each geometry type we want to render; but this 35 | // simple example doesn't use any actual geometries yet, so we only 36 | // create a single, dummy, set of them (we do have to have at least 37 | // one group of them to set up the SBT) 38 | //------------------------------------------------------------------------------ 39 | 40 | extern "C" __global__ void 41 | __closesthit__radiance() { /*! for this simple example, this will remain empty 42 | */ 43 | } 44 | 45 | extern "C" __global__ void 46 | __anyhit__radiance() { /*! for this simple example, this will remain empty */ 47 | } 48 | 49 | //------------------------------------------------------------------------------ 50 | // miss program that gets called for any ray that did not have a 51 | // valid intersection 52 | // 53 | // as with the anyhit/closest hit programs, in this example we only 54 | // need to have _some_ dummy function to set up a valid SBT 55 | // ------------------------------------------------------------------------------ 56 | 57 | extern "C" __global__ void 58 | __miss__radiance() { /*! for this simple example, this will remain empty */ 59 | } 60 | 61 | //------------------------------------------------------------------------------ 62 | // ray gen program - the actual rendering happens in here 63 | //------------------------------------------------------------------------------ 64 | extern "C" __global__ void __raygen__renderFrame() { 65 | if (optixLaunchParams.frameID == 0 && optixGetLaunchIndex().x == 0 && 66 | optixGetLaunchIndex().y == 0) { 67 | // we could of course also have used optixGetLaunchDims to query 68 | // the launch size, but accessing the optixLaunchParams here 69 | // makes sure they're not getting optimized away (because 70 | // otherwise they'd not get used) 71 | printf("############################################\n"); 72 | printf("Hello world from OptiX 7 raygen program!\n(within a " 73 | "%ix%i-sized launch)\n", 74 | optixLaunchParams.fbSize.x, optixLaunchParams.fbSize.y); 75 | printf("############################################\n"); 76 | } 77 | 78 | // ------------------------------------------------------------------ 79 | // for this example, produce a simple test pattern: 80 | // ------------------------------------------------------------------ 81 | 82 | // compute a test pattern based on pixel ID 83 | const int ix = optixGetLaunchIndex().x; 84 | const int iy = optixGetLaunchIndex().y; 85 | 86 | const int r = (ix % 256); 87 | const int g = (iy % 256); 88 | const int b = ((ix + iy) % 256); 89 | 90 | // convert to 32-bit rgba value (we explicitly set alpha to 0xff 91 | // to make stb_image_write happy ... 92 | const uint32_t rgba = 0xff000000 | (r << 0) | (g << 8) | (b << 16); 93 | 94 | // and write to frame buffer ... 95 | const uint32_t fbIndex = ix + iy * optixLaunchParams.fbSize.x; 96 | optixLaunchParams.colorBuffer[fbIndex] = rgba; 97 | } 98 | 99 | } // namespace osc 100 | -------------------------------------------------------------------------------- /optix/examples/02_pipeline/main.rs: -------------------------------------------------------------------------------- 1 | #[macro_use] 2 | extern crate enum_primitive; 3 | 4 | use num::FromPrimitive; 5 | 6 | mod sample_renderer; 7 | use optix::cuda::{TaggedAllocator, TaggedMallocator}; 8 | use sample_renderer::{MemTags, SampleRenderer}; 9 | 10 | use optix::math::*; 11 | 12 | fn main() { 13 | let alloc = TaggedMallocator::new(); 14 | let mut sample = SampleRenderer::new(v2i32(1200, 1024), &alloc).unwrap(); 15 | sample.render(); 16 | 17 | println!("Total allocated: {}", alloc.total_allocated()); 18 | let tags = alloc.tag_allocations(); 19 | for (tag, size) in tags.iter() { 20 | println!("{:?}: {}", MemTags::from_u64(*tag).unwrap(), size); 21 | } 22 | } 23 | -------------------------------------------------------------------------------- /optix/examples/03_window/devicePrograms.cu: -------------------------------------------------------------------------------- 1 | // ======================================================================== // 2 | // Copyright 2018-2019 Ingo Wald // 3 | // // 4 | // Licensed under the Apache License, Version 2.0 (the "License"); // 5 | // you may not use this file except in compliance with the License. // 6 | // You may obtain a copy of the License at // 7 | // // 8 | // http://www.apache.org/licenses/LICENSE-2.0 // 9 | // // 10 | // Unless required by applicable law or agreed to in writing, software // 11 | // distributed under the License is distributed on an "AS IS" BASIS, // 12 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. // 13 | // See the License for the specific language governing permissions and // 14 | // limitations under the License. // 15 | // ======================================================================== // 16 | 17 | #include "vec.h" 18 | #include 19 | 20 | namespace osc { 21 | #include "launch_params.h" 22 | } 23 | 24 | using namespace osc; 25 | 26 | namespace osc { 27 | 28 | /*! launch parameters in constant memory, filled in by optix upon 29 | optixLaunch (this gets filled in from the buffer we pass to 30 | optixLaunch) */ 31 | extern "C" __constant__ LaunchParams optixLaunchParams; 32 | 33 | //------------------------------------------------------------------------------ 34 | // closest hit and anyhit programs for radiance-type rays. 35 | // 36 | // Note eventually we will have to create one pair of those for each 37 | // ray type and each geometry type we want to render; but this 38 | // simple example doesn't use any actual geometries yet, so we only 39 | // create a single, dummy, set of them (we do have to have at least 40 | // one group of them to set up the SBT) 41 | //------------------------------------------------------------------------------ 42 | 43 | extern "C" __global__ void 44 | __closesthit__radiance() { /*! for this simple example, this will remain empty 45 | */ 46 | } 47 | 48 | extern "C" __global__ void 49 | __anyhit__radiance() { /*! for this simple example, this will remain empty */ 50 | } 51 | 52 | //------------------------------------------------------------------------------ 53 | // miss program that gets called for any ray that did not have a 54 | // valid intersection 55 | // 56 | // as with the anyhit/closest hit programs, in this example we only 57 | // need to have _some_ dummy function to set up a valid SBT 58 | // ------------------------------------------------------------------------------ 59 | 60 | extern "C" __global__ void 61 | __miss__radiance() { /*! for this simple example, this will remain empty */ 62 | } 63 | 64 | //------------------------------------------------------------------------------ 65 | // ray gen program - the actual rendering happens in here 66 | //------------------------------------------------------------------------------ 67 | extern "C" __global__ void __raygen__renderFrame() { 68 | const int frameID = optixLaunchParams.frame_id; 69 | if (frameID == 0 && optixGetLaunchIndex().x == 0 && 70 | optixGetLaunchIndex().y == 0) { 71 | // we could of course also have used optixGetLaunchDims to query 72 | // the launch size, but accessing the optixLaunchParams here 73 | // makes sure they're not getting optimized away (because 74 | // otherwise they'd not get used) 75 | printf("############################################\n"); 76 | printf("Hello world from OptiX 7 raygen program!\n(within a " 77 | "%ix%i-sized launch)\n", 78 | optixLaunchParams.fb_size.x, optixLaunchParams.fb_size.y); 79 | printf("############################################\n"); 80 | } 81 | 82 | // ------------------------------------------------------------------ 83 | // for this example, produce a simple test pattern: 84 | // ------------------------------------------------------------------ 85 | 86 | // compute a test pattern based on pixel ID 87 | const int ix = optixGetLaunchIndex().x; 88 | const int iy = optixGetLaunchIndex().y; 89 | 90 | const float r = float((ix + frameID) % 256) / 255.0f; 91 | const float g = float((iy + frameID) % 256) / 255.0f; 92 | const float b = float((ix + iy + frameID) % 256) / 255.0f; 93 | 94 | // and write to frame buffer ... 95 | const unsigned fbIndex = ix + iy * optixLaunchParams.fb_size.x; 96 | optixLaunchParams.color_buffer[fbIndex] = make_float4(r, g, b, 1.0f); 97 | } 98 | 99 | } // namespace osc 100 | -------------------------------------------------------------------------------- /optix/examples/03_window/main.rs: -------------------------------------------------------------------------------- 1 | #[macro_use] 2 | extern crate enum_primitive; 3 | 4 | use num::FromPrimitive; 5 | mod sample_renderer; 6 | use optix::cuda::{TaggedAllocator, TaggedMallocator}; 7 | use sample_renderer::*; 8 | 9 | use glfw::{Action, Context, Key}; 10 | pub mod gl_util; 11 | use crate::gl_util::*; 12 | 13 | use optix::math::*; 14 | 15 | fn main() { 16 | let mut glfw = glfw::init(glfw::FAIL_ON_ERRORS).unwrap(); 17 | glfw.window_hint(glfw::WindowHint::ContextVersion(4, 1)); 18 | glfw.window_hint(glfw::WindowHint::OpenGlForwardCompat(true)); 19 | glfw.window_hint(glfw::WindowHint::OpenGlProfile( 20 | glfw::OpenGlProfileHint::Core, 21 | )); 22 | 23 | let mut width = 960u32; 24 | let mut height = 540u32; 25 | 26 | let alloc = TaggedMallocator::new(); 27 | let mut sample = 28 | SampleRenderer::new(v2i32(width as i32, height as i32), &alloc) 29 | .unwrap(); 30 | 31 | let (mut window, events) = glfw 32 | .create_window( 33 | width, 34 | height, 35 | "Example 03: in window", 36 | glfw::WindowMode::Windowed, 37 | ) 38 | .expect("failed to create glfw window"); 39 | 40 | window.set_key_polling(true); 41 | window.make_current(); 42 | 43 | // retina displays will return a higher res for the framebuffer 44 | // which we need to use for the viewport 45 | let (fb_width, fb_height) = window.get_framebuffer_size(); 46 | 47 | gl::load_with(|s| { 48 | glfw.get_proc_address_raw(s) as *const std::os::raw::c_void 49 | }); 50 | 51 | let mut fsq = FullscreenQuad::new(width, height).unwrap(); 52 | 53 | let mut image_data = 54 | vec![v4f32(0.0, 0.0, 0.0, 0.0); (width * height) as usize]; 55 | 56 | unsafe { 57 | gl::Viewport(0, 0, fb_width, fb_height); 58 | }; 59 | 60 | while !window.should_close() { 61 | glfw.poll_events(); 62 | for (_, event) in glfw::flush_messages(&events) { 63 | handle_window_event(&mut window, event); 64 | } 65 | 66 | let (w, h) = window.get_framebuffer_size(); 67 | let w = w as u32; 68 | let h = h as u32; 69 | if (w != width || h != height) { 70 | fsq.resize(w, h); 71 | sample.resize(v2i32(w as i32, h as i32)); 72 | width = w; 73 | height = h; 74 | image_data 75 | .resize((width * height) as usize, v4f32(0.0, 0.0, 0.0, 0.0)); 76 | } 77 | 78 | sample.render(); 79 | sample.download_pixels(&mut image_data).unwrap(); 80 | fsq.update_texture(&image_data); 81 | fsq.set_progression(1); 82 | 83 | // draw the quad 84 | fsq.draw(); 85 | 86 | window.swap_buffers(); 87 | } 88 | 89 | println!("Total allocated: {}", alloc.total_allocated()); 90 | let tags = alloc.tag_allocations(); 91 | for (tag, size) in tags.iter() { 92 | println!("{:?}: {}", MemTags::from_u64(*tag).unwrap(), size); 93 | } 94 | } 95 | 96 | fn handle_window_event(window: &mut glfw::Window, event: glfw::WindowEvent) { 97 | match event { 98 | glfw::WindowEvent::Key(Key::Escape, _, Action::Press, _) => { 99 | window.set_should_close(true) 100 | } 101 | _ => {} 102 | } 103 | } 104 | -------------------------------------------------------------------------------- /optix/examples/04_mesh/devicePrograms.cu: -------------------------------------------------------------------------------- 1 | // ======================================================================== // 2 | // Copyright 2018-2019 Ingo Wald // 3 | // // 4 | // Licensed under the Apache License, Version 2.0 (the "License"); // 5 | // you may not use this file except in compliance with the License. // 6 | // You may obtain a copy of the License at // 7 | // // 8 | // http://www.apache.org/licenses/LICENSE-2.0 // 9 | // // 10 | // Unless required by applicable law or agreed to in writing, software // 11 | // distributed under the License is distributed on an "AS IS" BASIS, // 12 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. // 13 | // See the License for the specific language governing permissions and // 14 | // limitations under the License. // 15 | // ======================================================================== // 16 | 17 | #include 18 | #include 19 | 20 | namespace osc { 21 | #include "launch_params.h" 22 | } 23 | 24 | using namespace osc; 25 | 26 | namespace osc { 27 | 28 | /*! launch parameters in constant memory, filled in by optix upon 29 | optixLaunch (this gets filled in from the buffer we pass to 30 | optixLaunch) */ 31 | extern "C" __constant__ LaunchParams optixLaunchParams; 32 | 33 | // for this simple example, we have a single ray type 34 | enum { SURFACE_RAY_TYPE = 0, RAY_TYPE_COUNT }; 35 | 36 | static __forceinline__ __device__ void* unpackPointer(u32 i0, u32 i1) { 37 | const u64 uptr = static_cast(i0) << 32 | i1; 38 | void* ptr = reinterpret_cast(uptr); 39 | return ptr; 40 | } 41 | 42 | static __forceinline__ __device__ void packPointer(void* ptr, u32& i0, 43 | u32& i1) { 44 | const u64 uptr = reinterpret_cast(ptr); 45 | i0 = uptr >> 32; 46 | i1 = uptr & 0x00000000ffffffff; 47 | } 48 | 49 | template static __forceinline__ __device__ T* getPRD() { 50 | const u32 u0 = optixGetPayload_0(); 51 | const u32 u1 = optixGetPayload_1(); 52 | return reinterpret_cast(unpackPointer(u0, u1)); 53 | } 54 | 55 | /*! helper function that creates a semi-random color from an ID */ 56 | inline __device__ f32x3 randomColor(int i) { 57 | int r = unsigned(i) * 13 * 17 + 0x234235; 58 | int g = unsigned(i) * 7 * 3 * 5 + 0x773477; 59 | int b = unsigned(i) * 11 * 19 + 0x223766; 60 | return make_f32x3((r & 255) / 255.f, (g & 255) / 255.f, (b & 255) / 255.f); 61 | } 62 | 63 | //------------------------------------------------------------------------------ 64 | // closest hit and anyhit programs for radiance-type rays. 65 | // 66 | // Note eventually we will have to create one pair of those for each 67 | // ray type and each geometry type we want to render; but this 68 | // simple example doesn't use any actual geometries yet, so we only 69 | // create a single, dummy, set of them (we do have to have at least 70 | // one group of them to set up the SBT) 71 | //------------------------------------------------------------------------------ 72 | 73 | extern "C" __global__ void __closesthit__radiance() { 74 | const int primID = optixGetPrimitiveIndex(); 75 | f32x3& prd = *(f32x3*)getPRD(); 76 | prd = randomColor(primID); 77 | } 78 | 79 | extern "C" __global__ void 80 | __anyhit__radiance() { /*! for this simple example, this will remain empty */ 81 | } 82 | 83 | //------------------------------------------------------------------------------ 84 | // miss program that gets called for any ray that did not have a 85 | // valid intersection 86 | // 87 | // as with the anyhit/closest hit programs, in this example we only 88 | // need to have _some_ dummy function to set up a valid SBT 89 | // ------------------------------------------------------------------------------ 90 | 91 | extern "C" __global__ void __miss__radiance() { 92 | f32x3& prd = *(f32x3*)getPRD(); 93 | // set to constant white as background color 94 | prd = make_f32x3(1.f, 1.0f, 1.0f); 95 | } 96 | 97 | //------------------------------------------------------------------------------ 98 | // ray gen program - the actual rendering happens in here 99 | //------------------------------------------------------------------------------ 100 | extern "C" __global__ void __raygen__renderFrame() { 101 | // compute a test pattern based on pixel ID 102 | const int ix = optixGetLaunchIndex().x; 103 | const int iy = optixGetLaunchIndex().y; 104 | 105 | const auto& camera = optixLaunchParams.camera; 106 | 107 | // our per-ray data for this example. what we initialize it to 108 | // won't matter, since this value will be overwritten by either 109 | // the miss or hit program, anyway 110 | f32x3 pixelColorPRD = make_f32x3(0.f, 0.0f, 0.0f); 111 | 112 | // the values we store the PRD pointer in: 113 | u32 u0, u1; 114 | packPointer(&pixelColorPRD, u0, u1); 115 | 116 | // normalized screen plane position, in [0,1]^2 117 | const f32x2 screen = make_f32x2(f32(ix) + .5f, f32(iy) + .5f) / 118 | make_f32x2(optixLaunchParams.frame.size.x, 119 | optixLaunchParams.frame.size.y); 120 | 121 | // generate ray direction 122 | f32x3 rayDir = 123 | normalize(camera.direction + (screen.x - 0.5f) * camera.horizontal + 124 | (screen.y - 0.5f) * camera.vertical); 125 | 126 | /* 127 | if (ix == 960 / 2 && iy == 540 / 2) { 128 | f32x3 p = camera.position; 129 | printf("position: %f %f %f\n", p.x, p.y, p.z); 130 | f32x3 d = camera.direction; 131 | printf("direction: %f %f %f\n", d.x, d.y, d.z); 132 | f32x3 h = camera.horizontal; 133 | printf("horizontal: %f %f %f\n", h.x, h.y, h.z); 134 | f32x3 v = camera.vertical; 135 | printf("vertical: %f %f %f\n", v.x, v.y, v.z); 136 | printf("raydir: %f %f %f\n", rayDir.x, rayDir.y, rayDir.z); 137 | } 138 | */ 139 | 140 | optixTrace(optixLaunchParams.traversable, (float3)camera.position, 141 | (float3)rayDir, 142 | 0.f, // tmin 143 | 1e20f, // tmax 144 | 0.0f, // rayTime 145 | OptixVisibilityMask(255), 146 | OPTIX_RAY_FLAG_DISABLE_ANYHIT, // OPTIX_RAY_FLAG_NONE, 147 | SURFACE_RAY_TYPE, // SBT offset 148 | RAY_TYPE_COUNT, // SBT stride 149 | SURFACE_RAY_TYPE, // missSBTIndex 150 | u0, u1); 151 | 152 | // and write to frame buffer ... 153 | const u32 fbIndex = ix + iy * optixLaunchParams.frame.size.x; 154 | optixLaunchParams.frame.color_buffer[fbIndex] = 155 | make_f32x4(pixelColorPRD.x, pixelColorPRD.y, pixelColorPRD.z, 1.0f); 156 | } 157 | 158 | } // namespace osc 159 | -------------------------------------------------------------------------------- /optix/examples/04_mesh/main.rs: -------------------------------------------------------------------------------- 1 | #[macro_use] 2 | extern crate enum_primitive; 3 | use num::FromPrimitive; 4 | 5 | mod sample_renderer; 6 | use optix::cuda::TaggedMallocator; 7 | use sample_renderer::*; 8 | 9 | use glfw::{Action, Context, Key}; 10 | pub mod gl_util; 11 | use crate::gl_util::*; 12 | 13 | use optix::math::*; 14 | 15 | fn main() { 16 | let mut glfw = glfw::init(glfw::FAIL_ON_ERRORS).unwrap(); 17 | glfw.window_hint(glfw::WindowHint::ContextVersion(4, 1)); 18 | glfw.window_hint(glfw::WindowHint::OpenGlForwardCompat(true)); 19 | glfw.window_hint(glfw::WindowHint::OpenGlProfile( 20 | glfw::OpenGlProfileHint::Core, 21 | )); 22 | 23 | let mut width = 960u32; 24 | let mut height = 540u32; 25 | 26 | let mut mesh = TriangleMesh::new(); 27 | mesh.add_cube(v3f32(0.0, -1.5, 0.0), v3f32(10.0, 1.0, 10.0)); 28 | mesh.add_cube(v3f32(0.0, 0.0, 0.0), v3f32(2.0, 2.0, 2.0)); 29 | 30 | let camera = Camera { 31 | from: v3f32(-10.0, 2.0, -12.0), 32 | at: v3f32(0.0, 0.0, 0.0), 33 | up: v3f32(0.0, 1.0, 0.0), 34 | }; 35 | 36 | let alloc = TaggedMallocator::new(); 37 | let mut sample = SampleRenderer::new( 38 | v2i32(width as i32, height as i32), 39 | camera, 40 | mesh, 41 | &alloc, 42 | ) 43 | .unwrap(); 44 | 45 | let (mut window, events) = glfw 46 | .create_window( 47 | width, 48 | height, 49 | "Example 04: first mesh", 50 | glfw::WindowMode::Windowed, 51 | ) 52 | .expect("failed to create glfw window"); 53 | 54 | window.set_key_polling(true); 55 | window.make_current(); 56 | 57 | // retina displays will return a higher res for the framebuffer 58 | // which we need to use for the viewport 59 | let (fb_width, fb_height) = window.get_framebuffer_size(); 60 | 61 | gl::load_with(|s| { 62 | glfw.get_proc_address_raw(s) as *const std::os::raw::c_void 63 | }); 64 | 65 | let mut fsq = FullscreenQuad::new(width, height).unwrap(); 66 | 67 | let mut image_data = 68 | vec![v4f32(0.0, 0.0, 0.0, 0.0); (width * height) as usize]; 69 | 70 | unsafe { 71 | gl::Viewport(0, 0, fb_width, fb_height); 72 | }; 73 | 74 | while !window.should_close() { 75 | glfw.poll_events(); 76 | for (_, event) in glfw::flush_messages(&events) { 77 | handle_window_event(&mut window, event); 78 | } 79 | 80 | let (w, h) = window.get_framebuffer_size(); 81 | let w = w as u32; 82 | let h = h as u32; 83 | if w != width || h != height { 84 | fsq.resize(w, h); 85 | sample.resize(v2i32(w as i32, h as i32)); 86 | width = w; 87 | height = h; 88 | image_data 89 | .resize((width * height) as usize, v4f32(0.0, 0.0, 0.0, 0.0)); 90 | } 91 | 92 | sample.render(); 93 | sample.download_pixels(&mut image_data).unwrap(); 94 | fsq.update_texture(&image_data); 95 | fsq.set_progression(1); 96 | 97 | // draw the quad 98 | fsq.draw(); 99 | 100 | window.swap_buffers(); 101 | } 102 | 103 | println!("Total allocated: {}", alloc.total_allocated()); 104 | let tags = alloc.tag_allocations(); 105 | for (tag, size) in tags.iter() { 106 | println!("{:?}: {}", MemTags::from_u64(*tag).unwrap(), size); 107 | } 108 | } 109 | 110 | fn handle_window_event(window: &mut glfw::Window, event: glfw::WindowEvent) { 111 | match event { 112 | glfw::WindowEvent::Key(Key::Escape, _, Action::Press, _) => { 113 | window.set_should_close(true) 114 | } 115 | _ => {} 116 | } 117 | } 118 | -------------------------------------------------------------------------------- /optix/examples/05_sbtdata/devicePrograms.cu: -------------------------------------------------------------------------------- 1 | // ======================================================================== // 2 | // Copyright 2018-2019 Ingo Wald // 3 | // // 4 | // Licensed under the Apache License, Version 2.0 (the "License"); // 5 | // you may not use this file except in compliance with the License. // 6 | // You may obtain a copy of the License at // 7 | // // 8 | // http://www.apache.org/licenses/LICENSE-2.0 // 9 | // // 10 | // Unless required by applicable law or agreed to in writing, software // 11 | // distributed under the License is distributed on an "AS IS" BASIS, // 12 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. // 13 | // See the License for the specific language governing permissions and // 14 | // limitations under the License. // 15 | // ======================================================================== // 16 | 17 | #include 18 | #include 19 | 20 | namespace osc { 21 | #include "launch_params.h" 22 | } 23 | 24 | using namespace osc; 25 | 26 | namespace osc { 27 | 28 | /*! launch parameters in constant memory, filled in by optix upon 29 | optixLaunch (this gets filled in from the buffer we pass to 30 | optixLaunch) */ 31 | extern "C" __constant__ LaunchParams optixLaunchParams; 32 | 33 | // for this simple example, we have a single ray type 34 | enum { SURFACE_RAY_TYPE = 0, RAY_TYPE_COUNT }; 35 | 36 | static __forceinline__ __device__ void* unpackPointer(u32 i0, u32 i1) { 37 | const u64 uptr = static_cast(i0) << 32 | i1; 38 | void* ptr = reinterpret_cast(uptr); 39 | return ptr; 40 | } 41 | 42 | static __forceinline__ __device__ void packPointer(void* ptr, u32& i0, 43 | u32& i1) { 44 | const u64 uptr = reinterpret_cast(ptr); 45 | i0 = uptr >> 32; 46 | i1 = uptr & 0x00000000ffffffff; 47 | } 48 | 49 | template static __forceinline__ __device__ T* getPRD() { 50 | const u32 u0 = optixGetPayload_0(); 51 | const u32 u1 = optixGetPayload_1(); 52 | return reinterpret_cast(unpackPointer(u0, u1)); 53 | } 54 | 55 | /*! helper function that creates a semi-random color from an ID */ 56 | inline __device__ f32x3 randomColor(int i) { 57 | int r = unsigned(i) * 13 * 17 + 0x234235; 58 | int g = unsigned(i) * 7 * 3 * 5 + 0x773477; 59 | int b = unsigned(i) * 11 * 19 + 0x223766; 60 | return make_f32x3((r & 255) / 255.f, (g & 255) / 255.f, (b & 255) / 255.f); 61 | } 62 | 63 | //------------------------------------------------------------------------------ 64 | // closest hit and anyhit programs for radiance-type rays. 65 | // 66 | // Note eventually we will have to create one pair of those for each 67 | // ray type and each geometry type we want to render; but this 68 | // simple example doesn't use any actual geometries yet, so we only 69 | // create a single, dummy, set of them (we do have to have at least 70 | // one group of them to set up the SBT) 71 | //------------------------------------------------------------------------------ 72 | 73 | extern "C" __global__ void __closesthit__radiance() { 74 | const TriangleMeshSBTData& sbtData = 75 | *(const TriangleMeshSBTData*)optixGetSbtDataPointer(); 76 | 77 | // compute normal: 78 | const int primID = optixGetPrimitiveIndex(); 79 | const i32x3 index = sbtData.index[primID]; 80 | const f32x3& A = sbtData.vertex[index.x]; 81 | const f32x3& B = sbtData.vertex[index.y]; 82 | const f32x3& C = sbtData.vertex[index.z]; 83 | const f32x3 Ng = normalize(cross(B - A, C - A)); 84 | 85 | const f32x3 rayDir = optixGetWorldRayDirection(); 86 | const float cosDN = 0.2f + .8f * fabsf(dot(rayDir, Ng)); 87 | f32x3& prd = *(f32x3*)getPRD(); 88 | prd = cosDN * sbtData.color; 89 | } 90 | 91 | extern "C" __global__ void 92 | __anyhit__radiance() { /*! for this simple example, this will remain empty */ 93 | } 94 | 95 | //------------------------------------------------------------------------------ 96 | // miss program that gets called for any ray that did not have a 97 | // valid intersection 98 | // 99 | // as with the anyhit/closest hit programs, in this example we only 100 | // need to have _some_ dummy function to set up a valid SBT 101 | // ------------------------------------------------------------------------------ 102 | 103 | extern "C" __global__ void __miss__radiance() { 104 | f32x3& prd = *(f32x3*)getPRD(); 105 | // set to constant white as background color 106 | prd = make_f32x3(1.f, 1.0f, 1.0f); 107 | } 108 | 109 | //------------------------------------------------------------------------------ 110 | // ray gen program - the actual rendering happens in here 111 | //------------------------------------------------------------------------------ 112 | extern "C" __global__ void __raygen__renderFrame() { 113 | // compute a test pattern based on pixel ID 114 | const int ix = optixGetLaunchIndex().x; 115 | const int iy = optixGetLaunchIndex().y; 116 | 117 | const auto& camera = optixLaunchParams.camera; 118 | 119 | // our per-ray data for this example. what we initialize it to 120 | // won't matter, since this value will be overwritten by either 121 | // the miss or hit program, anyway 122 | f32x3 pixelColorPRD = make_f32x3(0.f, 0.0f, 0.0f); 123 | 124 | // the values we store the PRD pointer in: 125 | u32 u0, u1; 126 | packPointer(&pixelColorPRD, u0, u1); 127 | 128 | // normalized screen plane position, in [0,1]^2 129 | const f32x2 screen = make_f32x2(f32(ix) + .5f, f32(iy) + .5f) / 130 | make_f32x2(optixLaunchParams.frame.size.x, 131 | optixLaunchParams.frame.size.y); 132 | 133 | // generate ray direction 134 | f32x3 rayDir = 135 | normalize(camera.direction + (screen.x - 0.5f) * camera.horizontal + 136 | (screen.y - 0.5f) * camera.vertical); 137 | optixTrace(optixLaunchParams.traversable, (float3)camera.position, 138 | (float3)rayDir, 139 | 0.f, // tmin 140 | 1e20f, // tmax 141 | 0.0f, // rayTime 142 | OptixVisibilityMask(255), 143 | OPTIX_RAY_FLAG_DISABLE_ANYHIT, // OPTIX_RAY_FLAG_NONE, 144 | SURFACE_RAY_TYPE, // SBT offset 145 | RAY_TYPE_COUNT, // SBT stride 146 | SURFACE_RAY_TYPE, // missSBTIndex 147 | u0, u1); 148 | 149 | // and write to frame buffer ... 150 | const u32 fbIndex = ix + iy * optixLaunchParams.frame.size.x; 151 | optixLaunchParams.frame.color_buffer[fbIndex] = 152 | make_float4(pixelColorPRD.x, pixelColorPRD.y, pixelColorPRD.z, 1.0f); 153 | } 154 | 155 | } // namespace osc 156 | -------------------------------------------------------------------------------- /optix/examples/05_sbtdata/main.rs: -------------------------------------------------------------------------------- 1 | #[macro_use] 2 | extern crate enum_primitive; 3 | use num::FromPrimitive; 4 | 5 | mod sample_renderer; 6 | use sample_renderer::*; 7 | 8 | use glfw::{Action, Context, Key}; 9 | pub mod gl_util; 10 | use crate::gl_util::*; 11 | 12 | use optix::cuda::TaggedMallocator; 13 | use optix::math::*; 14 | 15 | fn main() { 16 | let mut glfw = glfw::init(glfw::FAIL_ON_ERRORS).unwrap(); 17 | glfw.window_hint(glfw::WindowHint::ContextVersion(4, 1)); 18 | glfw.window_hint(glfw::WindowHint::OpenGlForwardCompat(true)); 19 | glfw.window_hint(glfw::WindowHint::OpenGlProfile( 20 | glfw::OpenGlProfileHint::Core, 21 | )); 22 | 23 | let mut width = 960u32; 24 | let mut height = 540u32; 25 | 26 | let mut mesh = TriangleMesh::new(); 27 | mesh.add_cube(v3f32(0.0, -1.5, 0.0), v3f32(10.0, 1.0, 10.0)); 28 | mesh.add_cube(v3f32(0.0, 0.0, 0.0), v3f32(2.0, 2.0, 2.0)); 29 | 30 | let camera = Camera { 31 | from: v3f32(-10.0, 2.0, -12.0), 32 | at: v3f32(0.0, 0.0, 0.0), 33 | up: v3f32(0.0, 1.0, 0.0), 34 | }; 35 | 36 | let alloc = TaggedMallocator::new(); 37 | let mut sample = SampleRenderer::new( 38 | v2i32(width as i32, height as i32), 39 | camera, 40 | mesh, 41 | &alloc, 42 | ) 43 | .unwrap(); 44 | 45 | let (mut window, events) = glfw 46 | .create_window( 47 | width, 48 | height, 49 | "Example 04: first mesh", 50 | glfw::WindowMode::Windowed, 51 | ) 52 | .expect("failed to create glfw window"); 53 | 54 | window.set_key_polling(true); 55 | window.make_current(); 56 | 57 | // retina displays will return a higher res for the framebuffer 58 | // which we need to use for the viewport 59 | let (fb_width, fb_height) = window.get_framebuffer_size(); 60 | 61 | gl::load_with(|s| { 62 | glfw.get_proc_address_raw(s) as *const std::os::raw::c_void 63 | }); 64 | 65 | let mut fsq = FullscreenQuad::new(width, height).unwrap(); 66 | 67 | let mut image_data = 68 | vec![v4f32(0.0, 0.0, 0.0, 0.0); (width * height) as usize]; 69 | 70 | unsafe { 71 | gl::Viewport(0, 0, fb_width, fb_height); 72 | }; 73 | 74 | while !window.should_close() { 75 | glfw.poll_events(); 76 | for (_, event) in glfw::flush_messages(&events) { 77 | handle_window_event(&mut window, event); 78 | } 79 | 80 | let (w, h) = window.get_framebuffer_size(); 81 | let w = w as u32; 82 | let h = h as u32; 83 | if w != width || h != height { 84 | fsq.resize(w, h); 85 | sample.resize(v2i32(w as i32, h as i32)); 86 | width = w; 87 | height = h; 88 | image_data 89 | .resize((width * height) as usize, v4f32(0.0, 0.0, 0.0, 0.0)); 90 | } 91 | 92 | sample.render(); 93 | sample.download_pixels(&mut image_data).unwrap(); 94 | fsq.update_texture(&image_data); 95 | fsq.set_progression(1); 96 | 97 | // draw the quad 98 | fsq.draw(); 99 | 100 | window.swap_buffers(); 101 | } 102 | } 103 | 104 | fn handle_window_event(window: &mut glfw::Window, event: glfw::WindowEvent) { 105 | match event { 106 | glfw::WindowEvent::Key(Key::Escape, _, Action::Press, _) => { 107 | window.set_should_close(true) 108 | } 109 | _ => {} 110 | } 111 | } 112 | -------------------------------------------------------------------------------- /optix/examples/06_multimesh/devicePrograms.cu: -------------------------------------------------------------------------------- 1 | // ======================================================================== // 2 | // Copyright 2018-2019 Ingo Wald // 3 | // // 4 | // Licensed under the Apache License, Version 2.0 (the "License"); // 5 | // you may not use this file except in compliance with the License. // 6 | // You may obtain a copy of the License at // 7 | // // 8 | // http://www.apache.org/licenses/LICENSE-2.0 // 9 | // // 10 | // Unless required by applicable law or agreed to in writing, software // 11 | // distributed under the License is distributed on an "AS IS" BASIS, // 12 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. // 13 | // See the License for the specific language governing permissions and // 14 | // limitations under the License. // 15 | // ======================================================================== // 16 | 17 | #include 18 | #include 19 | 20 | namespace osc { 21 | #include "launch_params.h" 22 | } 23 | 24 | using namespace osc; 25 | 26 | namespace osc { 27 | 28 | /*! launch parameters in constant memory, filled in by optix upon 29 | optixLaunch (this gets filled in from the buffer we pass to 30 | optixLaunch) */ 31 | extern "C" __constant__ LaunchParams optixLaunchParams; 32 | 33 | // for this simple example, we have a single ray type 34 | enum { SURFACE_RAY_TYPE = 0, RAY_TYPE_COUNT }; 35 | 36 | static __forceinline__ __device__ void* unpackPointer(u32 i0, u32 i1) { 37 | const u64 uptr = static_cast(i0) << 32 | i1; 38 | void* ptr = reinterpret_cast(uptr); 39 | return ptr; 40 | } 41 | 42 | static __forceinline__ __device__ void packPointer(void* ptr, u32& i0, 43 | u32& i1) { 44 | const u64 uptr = reinterpret_cast(ptr); 45 | i0 = uptr >> 32; 46 | i1 = uptr & 0x00000000ffffffff; 47 | } 48 | 49 | template static __forceinline__ __device__ T* getPRD() { 50 | const u32 u0 = optixGetPayload_0(); 51 | const u32 u1 = optixGetPayload_1(); 52 | return reinterpret_cast(unpackPointer(u0, u1)); 53 | } 54 | 55 | /*! helper function that creates a semi-random color from an ID */ 56 | inline __device__ f32x3 randomColor(int i) { 57 | int r = unsigned(i) * 13 * 17 + 0x234235; 58 | int g = unsigned(i) * 7 * 3 * 5 + 0x773477; 59 | int b = unsigned(i) * 11 * 19 + 0x223766; 60 | return make_f32x3((r & 255) / 255.f, (g & 255) / 255.f, (b & 255) / 255.f); 61 | } 62 | 63 | //------------------------------------------------------------------------------ 64 | // closest hit and anyhit programs for radiance-type rays. 65 | // 66 | // Note eventually we will have to create one pair of those for each 67 | // ray type and each geometry type we want to render; but this 68 | // simple example doesn't use any actual geometries yet, so we only 69 | // create a single, dummy, set of them (we do have to have at least 70 | // one group of them to set up the SBT) 71 | //------------------------------------------------------------------------------ 72 | 73 | extern "C" __global__ void __closesthit__radiance() { 74 | const TriangleMeshSBTData& sbtData = 75 | *(const TriangleMeshSBTData*)optixGetSbtDataPointer(); 76 | 77 | // compute normal: 78 | const int primID = optixGetPrimitiveIndex(); 79 | const i32x3 index = sbtData.index[primID]; 80 | const f32x3& A = sbtData.vertex[index.x]; 81 | const f32x3& B = sbtData.vertex[index.y]; 82 | const f32x3& C = sbtData.vertex[index.z]; 83 | const f32x3 Ng = normalize(cross(B - A, C - A)); 84 | 85 | const f32x3 rayDir = optixGetWorldRayDirection(); 86 | const float cosDN = 0.2f + .8f * fabsf(dot(rayDir, Ng)); 87 | f32x3& prd = *(f32x3*)getPRD(); 88 | prd = cosDN * sbtData.color; 89 | } 90 | 91 | extern "C" __global__ void 92 | __anyhit__radiance() { /*! for this simple example, this will remain empty */ 93 | } 94 | 95 | //------------------------------------------------------------------------------ 96 | // miss program that gets called for any ray that did not have a 97 | // valid intersection 98 | // 99 | // as with the anyhit/closest hit programs, in this example we only 100 | // need to have _some_ dummy function to set up a valid SBT 101 | // ------------------------------------------------------------------------------ 102 | 103 | extern "C" __global__ void __miss__radiance() { 104 | f32x3& prd = *(f32x3*)getPRD(); 105 | // set to constant white as background color 106 | prd = make_f32x3(1.f, 1.0f, 1.0f); 107 | } 108 | 109 | //------------------------------------------------------------------------------ 110 | // ray gen program - the actual rendering happens in here 111 | //------------------------------------------------------------------------------ 112 | extern "C" __global__ void __raygen__renderFrame() { 113 | // compute a test pattern based on pixel ID 114 | const int ix = optixGetLaunchIndex().x; 115 | const int iy = optixGetLaunchIndex().y; 116 | 117 | const auto& camera = optixLaunchParams.camera; 118 | 119 | // our per-ray data for this example. what we initialize it to 120 | // won't matter, since this value will be overwritten by either 121 | // the miss or hit program, anyway 122 | f32x3 pixelColorPRD = make_f32x3(0.f, 0.0f, 0.0f); 123 | 124 | // the values we store the PRD pointer in: 125 | u32 u0, u1; 126 | packPointer(&pixelColorPRD, u0, u1); 127 | 128 | // normalized screen plane position, in [0,1]^2 129 | const f32x2 screen = make_f32x2(f32(ix) + .5f, f32(iy) + .5f) / 130 | make_f32x2(optixLaunchParams.frame.size.x, 131 | optixLaunchParams.frame.size.y); 132 | 133 | // generate ray direction 134 | f32x3 rayDir = 135 | normalize(camera.direction + (screen.x - 0.5f) * camera.horizontal + 136 | (screen.y - 0.5f) * camera.vertical); 137 | 138 | optixTrace(optixLaunchParams.traversable, (float3)camera.position, 139 | (float3)rayDir, 140 | 0.f, // tmin 141 | 1e20f, // tmax 142 | 0.0f, // rayTime 143 | OptixVisibilityMask(255), 144 | OPTIX_RAY_FLAG_DISABLE_ANYHIT, // OPTIX_RAY_FLAG_NONE, 145 | SURFACE_RAY_TYPE, // SBT offset 146 | RAY_TYPE_COUNT, // SBT stride 147 | SURFACE_RAY_TYPE, // missSBTIndex 148 | u0, u1); 149 | 150 | // and write to frame buffer ... 151 | const u32 fbIndex = ix + iy * optixLaunchParams.frame.size.x; 152 | optixLaunchParams.frame.color_buffer[fbIndex] = 153 | make_float4(pixelColorPRD.x, pixelColorPRD.y, pixelColorPRD.z, 1.0f); 154 | } 155 | 156 | } // namespace osc 157 | -------------------------------------------------------------------------------- /optix/examples/06_multimesh/main.rs: -------------------------------------------------------------------------------- 1 | #[macro_use] 2 | extern crate enum_primitive; 3 | use num::FromPrimitive; 4 | 5 | mod sample_renderer; 6 | use sample_renderer::*; 7 | 8 | use glfw::{Action, Context, Key}; 9 | pub mod gl_util; 10 | use crate::gl_util::*; 11 | 12 | use optix::cuda::TaggedMallocator; 13 | use optix::math::*; 14 | 15 | fn main() { 16 | let mut glfw = glfw::init(glfw::FAIL_ON_ERRORS).unwrap(); 17 | glfw.window_hint(glfw::WindowHint::ContextVersion(4, 1)); 18 | glfw.window_hint(glfw::WindowHint::OpenGlForwardCompat(true)); 19 | glfw.window_hint(glfw::WindowHint::OpenGlProfile( 20 | glfw::OpenGlProfileHint::Core, 21 | )); 22 | 23 | let mut width = 960u32; 24 | let mut height = 540u32; 25 | 26 | let mut meshes = Vec::new(); 27 | let mut mesh = TriangleMesh::new(v3f32(0.8, 0.2, 0.1)); 28 | mesh.add_cube(v3f32(0.0, -1.5, 0.0), v3f32(10.0, 1.0, 10.0)); 29 | meshes.push(mesh); 30 | let mut mesh = TriangleMesh::new(v3f32(0.2, 0.8, 0.1)); 31 | mesh.add_cube(v3f32(0.0, 0.0, 0.0), v3f32(2.0, 2.0, 2.0)); 32 | meshes.push(mesh); 33 | 34 | let camera = Camera { 35 | from: v3f32(-10.0, 2.0, -12.0), 36 | at: v3f32(0.0, 0.0, 0.0), 37 | up: v3f32(0.0, 1.0, 0.0), 38 | }; 39 | 40 | let alloc = TaggedMallocator::new(); 41 | let mut sample = SampleRenderer::new( 42 | v2i32(width as i32, height as i32), 43 | camera, 44 | meshes, 45 | &alloc, 46 | ) 47 | .unwrap(); 48 | 49 | let (mut window, events) = glfw 50 | .create_window( 51 | width, 52 | height, 53 | "Example 06: multiple meshes", 54 | glfw::WindowMode::Windowed, 55 | ) 56 | .expect("failed to create glfw window"); 57 | 58 | window.set_key_polling(true); 59 | window.make_current(); 60 | 61 | // retina displays will return a higher res for the framebuffer 62 | // which we need to use for the viewport 63 | let (fb_width, fb_height) = window.get_framebuffer_size(); 64 | 65 | gl::load_with(|s| { 66 | glfw.get_proc_address_raw(s) as *const std::os::raw::c_void 67 | }); 68 | 69 | let mut fsq = FullscreenQuad::new(width, height).unwrap(); 70 | 71 | let mut image_data = 72 | vec![v4f32(0.0, 0.0, 0.0, 0.0); (width * height) as usize]; 73 | 74 | unsafe { 75 | gl::Viewport(0, 0, fb_width, fb_height); 76 | }; 77 | 78 | while !window.should_close() { 79 | glfw.poll_events(); 80 | for (_, event) in glfw::flush_messages(&events) { 81 | handle_window_event(&mut window, event); 82 | } 83 | 84 | let (w, h) = window.get_framebuffer_size(); 85 | let w = w as u32; 86 | let h = h as u32; 87 | if w != width || h != height { 88 | fsq.resize(w, h); 89 | sample.resize(v2i32(w as i32, h as i32)); 90 | width = w; 91 | height = h; 92 | image_data 93 | .resize((width * height) as usize, v4f32(0.0, 0.0, 0.0, 0.0)); 94 | } 95 | 96 | sample.render(); 97 | sample.download_pixels(&mut image_data).unwrap(); 98 | fsq.update_texture(&image_data); 99 | fsq.set_progression(1); 100 | 101 | // draw the quad 102 | fsq.draw(); 103 | 104 | window.swap_buffers(); 105 | } 106 | } 107 | 108 | fn handle_window_event(window: &mut glfw::Window, event: glfw::WindowEvent) { 109 | match event { 110 | glfw::WindowEvent::Key(Key::Escape, _, Action::Press, _) => { 111 | window.set_should_close(true) 112 | } 113 | _ => {} 114 | } 115 | } 116 | -------------------------------------------------------------------------------- /optix/examples/07_obj/devicePrograms.cu: -------------------------------------------------------------------------------- 1 | // ======================================================================== // 2 | // Copyright 2018-2019 Ingo Wald // 3 | // // 4 | // Licensed under the Apache License, Version 2.0 (the "License"); // 5 | // you may not use this file except in compliance with the License. // 6 | // You may obtain a copy of the License at // 7 | // // 8 | // http://www.apache.org/licenses/LICENSE-2.0 // 9 | // // 10 | // Unless required by applicable law or agreed to in writing, software // 11 | // distributed under the License is distributed on an "AS IS" BASIS, // 12 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. // 13 | // See the License for the specific language governing permissions and // 14 | // limitations under the License. // 15 | // ======================================================================== // 16 | 17 | #include 18 | #include 19 | 20 | namespace osc { 21 | #include "launch_params.h" 22 | } 23 | 24 | using namespace osc; 25 | 26 | namespace osc { 27 | 28 | /*! launch parameters in constant memory, filled in by optix upon 29 | optixLaunch (this gets filled in from the buffer we pass to 30 | optixLaunch) */ 31 | extern "C" __constant__ LaunchParams optixLaunchParams; 32 | 33 | // for this simple example, we have a single ray type 34 | enum { SURFACE_RAY_TYPE = 0, RAY_TYPE_COUNT }; 35 | 36 | static __forceinline__ __device__ void* unpackPointer(u32 i0, u32 i1) { 37 | const u64 uptr = static_cast(i0) << 32 | i1; 38 | void* ptr = reinterpret_cast(uptr); 39 | return ptr; 40 | } 41 | 42 | static __forceinline__ __device__ void packPointer(void* ptr, u32& i0, 43 | u32& i1) { 44 | const u64 uptr = reinterpret_cast(ptr); 45 | i0 = uptr >> 32; 46 | i1 = uptr & 0x00000000ffffffff; 47 | } 48 | 49 | template static __forceinline__ __device__ T* getPRD() { 50 | const u32 u0 = optixGetPayload_0(); 51 | const u32 u1 = optixGetPayload_1(); 52 | return reinterpret_cast(unpackPointer(u0, u1)); 53 | } 54 | 55 | /*! helper function that creates a semi-random color from an ID */ 56 | inline __device__ f32x3 randomColor(int i) { 57 | int r = unsigned(i) * 13 * 17 + 0x234235; 58 | int g = unsigned(i) * 7 * 3 * 5 + 0x773477; 59 | int b = unsigned(i) * 11 * 19 + 0x223766; 60 | return make_f32x3((r & 255) / 255.f, (g & 255) / 255.f, (b & 255) / 255.f); 61 | } 62 | 63 | //------------------------------------------------------------------------------ 64 | // closest hit and anyhit programs for radiance-type rays. 65 | // 66 | // Note eventually we will have to create one pair of those for each 67 | // ray type and each geometry type we want to render; but this 68 | // simple example doesn't use any actual geometries yet, so we only 69 | // create a single, dummy, set of them (we do have to have at least 70 | // one group of them to set up the SBT) 71 | //------------------------------------------------------------------------------ 72 | 73 | extern "C" __global__ void __closesthit__radiance() { 74 | const TriangleMeshSBTData& sbtData = 75 | *(const TriangleMeshSBTData*)optixGetSbtDataPointer(); 76 | 77 | // compute normal: 78 | const int primID = optixGetPrimitiveIndex(); 79 | const i32x3 index = sbtData.index[primID]; 80 | const f32x3& A = sbtData.vertex[index.x]; 81 | const f32x3& B = sbtData.vertex[index.y]; 82 | const f32x3& C = sbtData.vertex[index.z]; 83 | const f32x3 Ng = normalize(cross(B - A, C - A)); 84 | 85 | const f32x3 rayDir = optixGetWorldRayDirection(); 86 | const float cosDN = 0.2f + .8f * fabsf(dot(rayDir, Ng)); 87 | f32x3& prd = *(f32x3*)getPRD(); 88 | prd = cosDN * sbtData.color; 89 | } 90 | 91 | extern "C" __global__ void 92 | __anyhit__radiance() { /*! for this simple example, this will remain empty */ 93 | } 94 | 95 | //------------------------------------------------------------------------------ 96 | // miss program that gets called for any ray that did not have a 97 | // valid intersection 98 | // 99 | // as with the anyhit/closest hit programs, in this example we only 100 | // need to have _some_ dummy function to set up a valid SBT 101 | // ------------------------------------------------------------------------------ 102 | 103 | extern "C" __global__ void __miss__radiance() { 104 | f32x3& prd = *(f32x3*)getPRD(); 105 | // set to constant white as background color 106 | prd = make_f32x3(1.f, 1.0f, 1.0f); 107 | } 108 | 109 | //------------------------------------------------------------------------------ 110 | // ray gen program - the actual rendering happens in here 111 | //------------------------------------------------------------------------------ 112 | extern "C" __global__ void __raygen__renderFrame() { 113 | // compute a test pattern based on pixel ID 114 | const int ix = optixGetLaunchIndex().x; 115 | const int iy = optixGetLaunchIndex().y; 116 | 117 | const auto& camera = optixLaunchParams.camera; 118 | 119 | // our per-ray data for this example. what we initialize it to 120 | // won't matter, since this value will be overwritten by either 121 | // the miss or hit program, anyway 122 | f32x3 pixelColorPRD = make_f32x3(0.f, 0.0f, 0.0f); 123 | 124 | // the values we store the PRD pointer in: 125 | u32 u0, u1; 126 | packPointer(&pixelColorPRD, u0, u1); 127 | 128 | // normalized screen plane position, in [0,1]^2 129 | const f32x2 screen = make_f32x2(f32(ix) + .5f, f32(iy) + .5f) / 130 | make_f32x2(optixLaunchParams.frame.size.x, 131 | optixLaunchParams.frame.size.y); 132 | 133 | // generate ray direction 134 | f32x3 rayDir = 135 | normalize(camera.direction + (screen.x - 0.5f) * camera.horizontal + 136 | (screen.y - 0.5f) * camera.vertical); 137 | 138 | optixTrace(optixLaunchParams.traversable, (float3)camera.position, 139 | (float3)rayDir, 140 | 0.f, // tmin 141 | 1e20f, // tmax 142 | 0.0f, // rayTime 143 | OptixVisibilityMask(255), 144 | OPTIX_RAY_FLAG_DISABLE_ANYHIT, // OPTIX_RAY_FLAG_NONE, 145 | SURFACE_RAY_TYPE, // SBT offset 146 | RAY_TYPE_COUNT, // SBT stride 147 | SURFACE_RAY_TYPE, // missSBTIndex 148 | u0, u1); 149 | 150 | // and write to frame buffer ... 151 | const u32 fbIndex = ix + iy * optixLaunchParams.frame.size.x; 152 | optixLaunchParams.frame.color_buffer[fbIndex] = 153 | make_float4(pixelColorPRD.x, pixelColorPRD.y, pixelColorPRD.z, 1.0f); 154 | } 155 | 156 | } // namespace osc 157 | -------------------------------------------------------------------------------- /optix/examples/07_obj/main.rs: -------------------------------------------------------------------------------- 1 | #[macro_use] 2 | extern crate enum_primitive; 3 | use num::FromPrimitive; 4 | 5 | mod sample_renderer; 6 | use sample_renderer::*; 7 | 8 | use glfw::{Action, Context, Key}; 9 | pub mod gl_util; 10 | use crate::gl_util::*; 11 | 12 | use optix::cuda::TaggedMallocator; 13 | use optix::math::*; 14 | 15 | fn load_model(path: &std::path::Path) -> Model { 16 | let (models, materials) = tobj::load_obj(path).unwrap(); 17 | 18 | let mut bounds = Box3f32::make_empty(); 19 | let meshes = models 20 | .into_iter() 21 | .map(|model| { 22 | let diffuse = if let Some(material_id) = model.mesh.material_id { 23 | materials[material_id].diffuse.into() 24 | } else { 25 | v3f32(0.8, 0.8, 0.8) 26 | }; 27 | 28 | let vertex: Vec = model 29 | .mesh 30 | .positions 31 | .chunks(3) 32 | .map(|c| { 33 | let p = v3f32(c[0], c[1], c[2]); 34 | bounds.extend_by_pnt(p); 35 | p 36 | }) 37 | .collect(); 38 | 39 | let normal: Vec = model 40 | .mesh 41 | .normals 42 | .chunks(3) 43 | .map(|c| v3f32(c[0], c[1], c[2])) 44 | .collect(); 45 | 46 | let texcoord: Vec = model 47 | .mesh 48 | .texcoords 49 | .chunks(2) 50 | .map(|c| v2f32(c[0], c[1])) 51 | .collect(); 52 | 53 | let index: Vec = model 54 | .mesh 55 | .indices 56 | .chunks(3) 57 | .map(|c| v3i32(c[0] as i32, c[1] as i32, c[2] as i32)) 58 | .collect(); 59 | 60 | Mesh { 61 | vertex, 62 | normal, 63 | texcoord, 64 | index, 65 | diffuse, 66 | } 67 | }) 68 | .collect::>(); 69 | 70 | Model { meshes, bounds } 71 | } 72 | 73 | fn main() { 74 | let mut glfw = glfw::init(glfw::FAIL_ON_ERRORS).unwrap(); 75 | glfw.window_hint(glfw::WindowHint::ContextVersion(4, 1)); 76 | glfw.window_hint(glfw::WindowHint::OpenGlForwardCompat(true)); 77 | glfw.window_hint(glfw::WindowHint::OpenGlProfile( 78 | glfw::OpenGlProfileHint::Core, 79 | )); 80 | 81 | let mut width = 960u32; 82 | let mut height = 540u32; 83 | 84 | let model = load_model(&std::path::Path::new(&format!( 85 | "{}/examples/data/sponza.obj", 86 | std::env::var("CARGO_MANIFEST_DIR").unwrap() 87 | ))); 88 | 89 | let camera = Camera { 90 | from: v3f32(-1293.07, 154.681, -0.7304), 91 | at: model.bounds.center() - v3f32(0.0, 400.0, 0.0), 92 | up: v3f32(0.0, 1.0, 0.0), 93 | }; 94 | 95 | let alloc = TaggedMallocator::new(); 96 | let mut sample = SampleRenderer::new( 97 | v2i32(width as i32, height as i32), 98 | camera, 99 | model, 100 | &alloc, 101 | ) 102 | .unwrap(); 103 | 104 | let (mut window, events) = glfw 105 | .create_window( 106 | width, 107 | height, 108 | "Example 07: First Real Model", 109 | glfw::WindowMode::Windowed, 110 | ) 111 | .expect("failed to create glfw window"); 112 | 113 | window.set_key_polling(true); 114 | window.make_current(); 115 | 116 | // retina displays will return a higher res for the framebuffer 117 | // which we need to use for the viewport 118 | let (fb_width, fb_height) = window.get_framebuffer_size(); 119 | 120 | gl::load_with(|s| { 121 | glfw.get_proc_address_raw(s) as *const std::os::raw::c_void 122 | }); 123 | 124 | let mut fsq = FullscreenQuad::new(width, height).unwrap(); 125 | 126 | let mut image_data = 127 | vec![v4f32(0.0, 0.0, 0.0, 0.0); (width * height) as usize]; 128 | 129 | unsafe { 130 | gl::Viewport(0, 0, fb_width, fb_height); 131 | }; 132 | 133 | while !window.should_close() { 134 | glfw.poll_events(); 135 | for (_, event) in glfw::flush_messages(&events) { 136 | handle_window_event(&mut window, event); 137 | } 138 | 139 | let (w, h) = window.get_framebuffer_size(); 140 | let w = w as u32; 141 | let h = h as u32; 142 | if w != width || h != height { 143 | fsq.resize(w, h); 144 | sample.resize(v2i32(w as i32, h as i32)); 145 | width = w; 146 | height = h; 147 | image_data 148 | .resize((width * height) as usize, v4f32(0.0, 0.0, 0.0, 0.0)); 149 | } 150 | 151 | sample.render(); 152 | sample.download_pixels(&mut image_data).unwrap(); 153 | fsq.update_texture(&image_data); 154 | fsq.set_progression(1); 155 | 156 | // draw the quad 157 | fsq.draw(); 158 | 159 | window.swap_buffers(); 160 | } 161 | } 162 | 163 | fn handle_window_event(window: &mut glfw::Window, event: glfw::WindowEvent) { 164 | match event { 165 | glfw::WindowEvent::Key(Key::Escape, _, Action::Press, _) => { 166 | window.set_should_close(true) 167 | } 168 | _ => {} 169 | } 170 | } 171 | -------------------------------------------------------------------------------- /optix/examples/08_texture/devicePrograms.cu: -------------------------------------------------------------------------------- 1 | // ======================================================================== // 2 | // Copyright 2018-2019 Ingo Wald // 3 | // // 4 | // Licensed under the Apache License, Version 2.0 (the "License"); // 5 | // you may not use this file except in compliance with the License. // 6 | // You may obtain a copy of the License at // 7 | // // 8 | // http://www.apache.org/licenses/LICENSE-2.0 // 9 | // // 10 | // Unless required by applicable law or agreed to in writing, software // 11 | // distributed under the License is distributed on an "AS IS" BASIS, // 12 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. // 13 | // See the License for the specific language governing permissions and // 14 | // limitations under the License. // 15 | // ======================================================================== // 16 | 17 | #include 18 | #include 19 | 20 | namespace osc { 21 | #include "launch_params.h" 22 | } 23 | 24 | using namespace osc; 25 | 26 | namespace osc { 27 | 28 | /*! launch parameters in constant memory, filled in by optix upon 29 | optixLaunch (this gets filled in from the buffer we pass to 30 | optixLaunch) */ 31 | extern "C" __constant__ LaunchParams optixLaunchParams; 32 | 33 | // for this simple example, we have a single ray type 34 | enum { SURFACE_RAY_TYPE = 0, RAY_TYPE_COUNT }; 35 | 36 | static __forceinline__ __device__ void* unpackPointer(u32 i0, u32 i1) { 37 | const u64 uptr = static_cast(i0) << 32 | i1; 38 | void* ptr = reinterpret_cast(uptr); 39 | return ptr; 40 | } 41 | 42 | static __forceinline__ __device__ void packPointer(void* ptr, u32& i0, 43 | u32& i1) { 44 | const u64 uptr = reinterpret_cast(ptr); 45 | i0 = uptr >> 32; 46 | i1 = uptr & 0x00000000ffffffff; 47 | } 48 | 49 | template static __forceinline__ __device__ T* getPRD() { 50 | const u32 u0 = optixGetPayload_0(); 51 | const u32 u1 = optixGetPayload_1(); 52 | return reinterpret_cast(unpackPointer(u0, u1)); 53 | } 54 | 55 | /*! helper function that creates a semi-random color from an ID */ 56 | inline __device__ f32x3 randomColor(int i) { 57 | int r = unsigned(i) * 13 * 17 + 0x234235; 58 | int g = unsigned(i) * 7 * 3 * 5 + 0x773477; 59 | int b = unsigned(i) * 11 * 19 + 0x223766; 60 | return make_f32x3((r & 255) / 255.f, (g & 255) / 255.f, (b & 255) / 255.f); 61 | } 62 | 63 | //------------------------------------------------------------------------------ 64 | // closest hit and anyhit programs for radiance-type rays. 65 | // 66 | // Note eventually we will have to create one pair of those for each 67 | // ray type and each geometry type we want to render; but this 68 | // simple example doesn't use any actual geometries yet, so we only 69 | // create a single, dummy, set of them (we do have to have at least 70 | // one group of them to set up the SBT) 71 | //------------------------------------------------------------------------------ 72 | extern "C" __global__ void __closesthit__radiance() { 73 | const TriangleMeshSBTData& sbtData = 74 | *(const TriangleMeshSBTData*)optixGetSbtDataPointer(); 75 | 76 | // ------------------------------------------------------------------ 77 | // gather some basic hit information 78 | // ------------------------------------------------------------------ 79 | const int primID = optixGetPrimitiveIndex(); 80 | const i32x3 index = sbtData.index[primID]; 81 | const float u = optixGetTriangleBarycentrics().x; 82 | const float v = optixGetTriangleBarycentrics().y; 83 | 84 | // ------------------------------------------------------------------ 85 | // compute normal, using either shading normal (if avail), or 86 | // geometry normal (fallback) 87 | // ------------------------------------------------------------------ 88 | auto N = make_f32x3(0.0f, 0.0f, 0.0f); 89 | if (!sbtData.normal.is_null()) { 90 | N = (1.f - u - v) * sbtData.normal[index.x] + 91 | u * sbtData.normal[index.y] + v * sbtData.normal[index.z]; 92 | } else { 93 | const f32x3& A = sbtData.vertex[index.x]; 94 | const f32x3& B = sbtData.vertex[index.y]; 95 | const f32x3& C = sbtData.vertex[index.z]; 96 | N = normalize(cross(B - A, C - A)); 97 | } 98 | N = normalize(N); 99 | 100 | // ------------------------------------------------------------------ 101 | // compute diffuse material color, including diffuse texture, if 102 | // available 103 | // ------------------------------------------------------------------ 104 | f32x3 diffuseColor = sbtData.color; 105 | if (sbtData.has_texture && !sbtData.texcoord.is_null()) { 106 | const f32x2 tc = (1.f - u - v) * sbtData.texcoord[index.x] + 107 | u * sbtData.texcoord[index.y] + 108 | v * sbtData.texcoord[index.z]; 109 | 110 | f32x4 fromTexture = tex2D(sbtData.texture, tc.x, 1.0f - tc.y); 111 | diffuseColor = diffuseColor * make_f32x3(fromTexture); 112 | } 113 | 114 | // ------------------------------------------------------------------ 115 | // perform some simple "NdotD" shading 116 | // ------------------------------------------------------------------ 117 | const f32x3 rayDir = optixGetWorldRayDirection(); 118 | const float cosDN = 0.2f + .8f * fabsf(dot(rayDir, N)); 119 | 120 | f32x3& prd = *(f32x3*)getPRD(); 121 | prd = cosDN * diffuseColor; 122 | } 123 | 124 | extern "C" __global__ void 125 | __anyhit__radiance() { /*! for this simple example, this will remain empty */ 126 | } 127 | 128 | //------------------------------------------------------------------------------ 129 | // miss program that gets called for any ray that did not have a 130 | // valid intersection 131 | // 132 | // as with the anyhit/closest hit programs, in this example we only 133 | // need to have _some_ dummy function to set up a valid SBT 134 | // ------------------------------------------------------------------------------ 135 | 136 | extern "C" __global__ void __miss__radiance() { 137 | f32x3& prd = *(f32x3*)getPRD(); 138 | // set to constant white as background color 139 | prd = make_f32x3(1.f, 1.0f, 1.0f); 140 | } 141 | 142 | //------------------------------------------------------------------------------ 143 | // ray gen program - the actual rendering happens in here 144 | //------------------------------------------------------------------------------ 145 | extern "C" __global__ void __raygen__renderFrame() { 146 | // compute a test pattern based on pixel ID 147 | const int ix = optixGetLaunchIndex().x; 148 | const int iy = optixGetLaunchIndex().y; 149 | 150 | const auto& camera = optixLaunchParams.camera; 151 | 152 | // our per-ray data for this example. what we initialize it to 153 | // won't matter, since this value will be overwritten by either 154 | // the miss or hit program, anyway 155 | f32x3 pixelColorPRD = make_f32x3(0.f, 0.0f, 0.0f); 156 | 157 | // the values we store the PRD pointer in: 158 | u32 u0, u1; 159 | packPointer(&pixelColorPRD, u0, u1); 160 | 161 | // normalized screen plane position, in [0,1]^2 162 | const f32x2 screen = make_f32x2(f32(ix) + .5f, f32(iy) + .5f) / 163 | make_f32x2(optixLaunchParams.frame.size.x, 164 | optixLaunchParams.frame.size.y); 165 | 166 | // generate ray direction 167 | f32x3 rayDir = 168 | normalize(camera.direction + (screen.x - 0.5f) * camera.horizontal + 169 | (screen.y - 0.5f) * camera.vertical); 170 | 171 | optixTrace(optixLaunchParams.traversable, (float3)camera.position, 172 | (float3)rayDir, 173 | 0.f, // tmin 174 | 1e20f, // tmax 175 | 0.0f, // rayTime 176 | OptixVisibilityMask(255), 177 | OPTIX_RAY_FLAG_DISABLE_ANYHIT, // OPTIX_RAY_FLAG_NONE, 178 | SURFACE_RAY_TYPE, // SBT offset 179 | RAY_TYPE_COUNT, // SBT stride 180 | SURFACE_RAY_TYPE, // missSBTIndex 181 | u0, u1); 182 | 183 | // and write to frame buffer ... 184 | const u32 fbIndex = ix + iy * optixLaunchParams.frame.size.x; 185 | optixLaunchParams.frame.color_buffer[fbIndex] = 186 | make_float4(pixelColorPRD.x, pixelColorPRD.y, pixelColorPRD.z, 1.0f); 187 | } 188 | 189 | } // namespace osc 190 | -------------------------------------------------------------------------------- /optix/examples/08_texture/main.rs: -------------------------------------------------------------------------------- 1 | #[macro_use] 2 | extern crate enum_primitive; 3 | use num::FromPrimitive; 4 | 5 | mod sample_renderer; 6 | use sample_renderer::*; 7 | 8 | use glfw::{Action, Context, Key}; 9 | pub mod gl_util; 10 | use crate::gl_util::*; 11 | 12 | use optix::cuda::TaggedMallocator; 13 | use optix::math::*; 14 | 15 | use std::rc::Rc; 16 | 17 | fn main() { 18 | let mut glfw = glfw::init(glfw::FAIL_ON_ERRORS).unwrap(); 19 | glfw.window_hint(glfw::WindowHint::ContextVersion(4, 1)); 20 | glfw.window_hint(glfw::WindowHint::OpenGlForwardCompat(true)); 21 | glfw.window_hint(glfw::WindowHint::OpenGlProfile( 22 | glfw::OpenGlProfileHint::Core, 23 | )); 24 | 25 | let mut width = 960u32; 26 | let mut height = 540u32; 27 | 28 | let model = load_model(&std::path::Path::new(&format!( 29 | "{}/examples/data/sponza.obj", 30 | std::env::var("CARGO_MANIFEST_DIR").unwrap() 31 | ))); 32 | 33 | let camera = Camera { 34 | from: v3f32(-1293.07, 154.681, -0.7304), 35 | at: model.bounds.center() - v3f32(0.0, 400.0, 0.0), 36 | up: v3f32(0.0, 1.0, 0.0), 37 | }; 38 | 39 | let alloc = TaggedMallocator::new(); 40 | let mut sample = SampleRenderer::new( 41 | v2i32(width as i32, height as i32), 42 | camera, 43 | model, 44 | &alloc, 45 | ) 46 | .unwrap(); 47 | 48 | let (mut window, events) = glfw 49 | .create_window( 50 | width, 51 | height, 52 | "Example 08: Adding Texture", 53 | glfw::WindowMode::Windowed, 54 | ) 55 | .expect("failed to create glfw window"); 56 | 57 | window.set_key_polling(true); 58 | window.make_current(); 59 | 60 | // retina displays will return a higher res for the framebuffer 61 | // which we need to use for the viewport 62 | let (fb_width, fb_height) = window.get_framebuffer_size(); 63 | 64 | gl::load_with(|s| { 65 | glfw.get_proc_address_raw(s) as *const std::os::raw::c_void 66 | }); 67 | 68 | let mut fsq = FullscreenQuad::new(width, height).unwrap(); 69 | 70 | let mut image_data = 71 | vec![v4f32(0.0, 0.0, 0.0, 0.0); (width * height) as usize]; 72 | 73 | unsafe { 74 | gl::Viewport(0, 0, fb_width, fb_height); 75 | }; 76 | 77 | while !window.should_close() { 78 | glfw.poll_events(); 79 | for (_, event) in glfw::flush_messages(&events) { 80 | handle_window_event(&mut window, event); 81 | } 82 | 83 | let (w, h) = window.get_framebuffer_size(); 84 | let w = w as u32; 85 | let h = h as u32; 86 | if w != width || h != height { 87 | fsq.resize(w, h); 88 | sample.resize(v2i32(w as i32, h as i32)); 89 | width = w; 90 | height = h; 91 | image_data 92 | .resize((width * height) as usize, v4f32(0.0, 0.0, 0.0, 0.0)); 93 | } 94 | 95 | sample.render(); 96 | sample.download_pixels(&mut image_data).unwrap(); 97 | fsq.update_texture(&image_data); 98 | fsq.set_progression(1); 99 | 100 | // draw the quad 101 | fsq.draw(); 102 | 103 | window.swap_buffers(); 104 | } 105 | } 106 | 107 | fn handle_window_event(window: &mut glfw::Window, event: glfw::WindowEvent) { 108 | match event { 109 | glfw::WindowEvent::Key(Key::Escape, _, Action::Press, _) => { 110 | window.set_should_close(true) 111 | } 112 | _ => {} 113 | } 114 | } 115 | 116 | fn load_texture(path: &std::path::Path) -> Option> { 117 | let im = match image::open(path) { 118 | Ok(im) => im.to_rgba(), 119 | Err(e) => { 120 | println!("{}", e); 121 | return None; 122 | } 123 | }; 124 | 125 | let dim = im.dimensions(); 126 | 127 | Some(Rc::new(Texture { 128 | resolution: v2i32(dim.0 as i32, dim.1 as i32), 129 | pixels: im.into_raw(), 130 | })) 131 | } 132 | 133 | fn load_model(path: &std::path::Path) -> Model { 134 | let (models, materials) = tobj::load_obj(path).unwrap(); 135 | 136 | let mut bounds = Box3f32::make_empty(); 137 | let mut loaded_texture_ids = std::collections::HashMap::new(); 138 | let mut textures = Vec::new(); 139 | let meshes = models 140 | .into_iter() 141 | .map(|model| { 142 | let (diffuse, diffuse_texture_id) = if let Some(material_id) = 143 | model.mesh.material_id 144 | { 145 | let diffuse = materials[material_id].diffuse.into(); 146 | 147 | // load the diffuse texture if there is one 148 | let diffuse_texture = &materials[material_id].diffuse_texture; 149 | let diffuse_texture_id = if diffuse_texture != "" { 150 | // if our texture cache has an entry for this texture 151 | // just return the (maybe) texture_id 152 | if loaded_texture_ids.contains_key(&diffuse_texture) { 153 | loaded_texture_ids[&diffuse_texture] 154 | } else { 155 | // If we don't have an entry, try and load the texture 156 | let full_texture_path = std::path::PathBuf::from( 157 | std::env::var("CARGO_MANIFEST_DIR").unwrap(), 158 | ) 159 | .join("examples/data") 160 | .join(diffuse_texture.replace("\\", "/")); 161 | 162 | println!( 163 | "Loading texture {}", 164 | full_texture_path.display() 165 | ); 166 | 167 | match load_texture(&full_texture_path) { 168 | Some(texture) => { 169 | textures.push(texture); 170 | let texture_id = Some(textures.len() - 1); 171 | loaded_texture_ids 172 | .insert(diffuse_texture, texture_id); 173 | texture_id 174 | } 175 | None => { 176 | loaded_texture_ids 177 | .insert(diffuse_texture, None); 178 | None 179 | } 180 | } 181 | } 182 | } else { 183 | None 184 | }; 185 | 186 | (diffuse, diffuse_texture_id) 187 | } else { 188 | (v3f32(0.8, 0.8, 0.8), None) 189 | }; 190 | 191 | let vertex: Vec = model 192 | .mesh 193 | .positions 194 | .chunks(3) 195 | .map(|c| { 196 | let p = v3f32(c[0], c[1], c[2]); 197 | bounds.extend_by_pnt(p); 198 | p 199 | }) 200 | .collect(); 201 | 202 | let normal: Vec = model 203 | .mesh 204 | .normals 205 | .chunks(3) 206 | .map(|c| v3f32(c[0], c[1], c[2])) 207 | .collect(); 208 | 209 | let texcoord: Vec = model 210 | .mesh 211 | .texcoords 212 | .chunks(2) 213 | .map(|c| v2f32(c[0], c[1])) 214 | .collect(); 215 | 216 | let index: Vec = model 217 | .mesh 218 | .indices 219 | .chunks(3) 220 | .map(|c| v3i32(c[0] as i32, c[1] as i32, c[2] as i32)) 221 | .collect(); 222 | 223 | Mesh { 224 | vertex, 225 | normal, 226 | texcoord, 227 | index, 228 | diffuse, 229 | diffuse_texture_id, 230 | } 231 | }) 232 | .collect::>(); 233 | 234 | Model { 235 | meshes, 236 | textures, 237 | bounds, 238 | } 239 | } 240 | -------------------------------------------------------------------------------- /optix/examples/09_shadow/LaunchParams.h: -------------------------------------------------------------------------------- 1 | // ======================================================================== // 2 | // Copyright 2018-2019 Ingo Wald // 3 | // // 4 | // Licensed under the Apache License, Version 2.0 (the "License"); // 5 | // you may not use this file except in compliance with the License. // 6 | // You may obtain a copy of the License at // 7 | // // 8 | // http://www.apache.org/licenses/LICENSE-2.0 // 9 | // // 10 | // Unless required by applicable law or agreed to in writing, software // 11 | // distributed under the License is distributed on an "AS IS" BASIS, // 12 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. // 13 | // See the License for the specific language governing permissions and // 14 | // limitations under the License. // 15 | // ======================================================================== // 16 | 17 | #pragma once 18 | 19 | #include 20 | #include 21 | 22 | namespace osc { 23 | 24 | enum { RADIANCE_RAY_TYPE = 0, SHADOW_RAY_TYPE, RAY_TYPE_COUNT }; 25 | 26 | struct TriangleMeshSBTData { 27 | V3f32 color; 28 | V3f32* vertex; 29 | V3f32* normal; 30 | V2f32* texcoord; 31 | V3i32* index; 32 | bool hasTexture; 33 | cudaTextureObject_t texture; 34 | }; 35 | 36 | struct LaunchParams { 37 | struct { 38 | float4* colorBuffer; 39 | V2i32 size; 40 | } frame; 41 | 42 | struct { 43 | V3f32 position; 44 | V3f32 direction; 45 | V3f32 horizontal; 46 | V3f32 vertical; 47 | } camera; 48 | 49 | OptixTraversableHandle traversable; 50 | }; 51 | 52 | } // namespace osc 53 | -------------------------------------------------------------------------------- /optix/examples/09_shadow/devicePrograms.cu: -------------------------------------------------------------------------------- 1 | // ======================================================================== // 2 | // Copyright 2018-2019 Ingo Wald // 3 | // // 4 | // Licensed under the Apache License, Version 2.0 (the "License"); // 5 | // you may not use this file except in compliance with the License. // 6 | // You may obtain a copy of the License at // 7 | // // 8 | // http://www.apache.org/licenses/LICENSE-2.0 // 9 | // // 10 | // Unless required by applicable law or agreed to in writing, software // 11 | // distributed under the License is distributed on an "AS IS" BASIS, // 12 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. // 13 | // See the License for the specific language governing permissions and // 14 | // limitations under the License. // 15 | // ======================================================================== // 16 | 17 | #include 18 | #include 19 | 20 | enum { RADIANCE_RAY_TYPE = 0, SHADOW_RAY_TYPE, RAY_TYPE_COUNT }; 21 | 22 | namespace osc { 23 | #include "launch_params.h" 24 | } 25 | 26 | using namespace osc; 27 | 28 | namespace osc { 29 | 30 | /*! launch parameters in constant memory, filled in by optix upon 31 | optixLaunch (this gets filled in from the buffer we pass to 32 | optixLaunch) */ 33 | extern "C" __constant__ LaunchParams optixLaunchParams; 34 | 35 | static __forceinline__ __device__ void* unpackPointer(u32 i0, u32 i1) { 36 | const u64 uptr = static_cast(i0) << 32 | i1; 37 | void* ptr = reinterpret_cast(uptr); 38 | return ptr; 39 | } 40 | 41 | static __forceinline__ __device__ void packPointer(void* ptr, u32& i0, 42 | u32& i1) { 43 | const u64 uptr = reinterpret_cast(ptr); 44 | i0 = uptr >> 32; 45 | i1 = uptr & 0x00000000ffffffff; 46 | } 47 | 48 | template static __forceinline__ __device__ T* getPRD() { 49 | const u32 u0 = optixGetPayload_0(); 50 | const u32 u1 = optixGetPayload_1(); 51 | return reinterpret_cast(unpackPointer(u0, u1)); 52 | } 53 | 54 | //------------------------------------------------------------------------------ 55 | // closest hit and anyhit programs for radiance-type rays. 56 | // 57 | // Note eventually we will have to create one pair of those for each 58 | // ray type and each geometry type we want to render; but this 59 | // simple example doesn't use any actual geometries yet, so we only 60 | // create a single, dummy, set of them (we do have to have at least 61 | // one group of them to set up the SBT) 62 | //------------------------------------------------------------------------------ 63 | extern "C" __global__ void __closesthit__shadow() { 64 | /* not going to be used ... */ 65 | } 66 | 67 | extern "C" __global__ void __closesthit__radiance() { 68 | const TriangleMeshSBTData& sbtData = 69 | *(const TriangleMeshSBTData*)optixGetSbtDataPointer(); 70 | 71 | // ------------------------------------------------------------------ 72 | // gather some basic hit information 73 | // ------------------------------------------------------------------ 74 | const int primID = optixGetPrimitiveIndex(); 75 | const V3i32 index = sbtData.index[primID]; 76 | const f32 u = optixGetTriangleBarycentrics().x; 77 | const f32 v = optixGetTriangleBarycentrics().y; 78 | 79 | // ------------------------------------------------------------------ 80 | // compute normal, using either shading normal (if avail), or 81 | // geometry normal (fallback) 82 | // ------------------------------------------------------------------ 83 | const V3f32& A = sbtData.vertex[index.x]; 84 | const V3f32& B = sbtData.vertex[index.y]; 85 | const V3f32& C = sbtData.vertex[index.z]; 86 | V3f32 Ng = cross(B - A, C - A); 87 | V3f32 Ns = (!sbtData.normal.is_null()) 88 | ? ((1.f - u - v) * sbtData.normal[index.x] + 89 | u * sbtData.normal[index.y] + v * sbtData.normal[index.z]) 90 | : Ng; 91 | 92 | // ------------------------------------------------------------------ 93 | // face-forward and normalize normals 94 | // ------------------------------------------------------------------ 95 | const V3f32 rayDir = optixGetWorldRayDirection(); 96 | 97 | if (dot(rayDir, Ng) > 0.f) 98 | Ng = -Ng; 99 | Ng = normalize(Ng); 100 | 101 | if (dot(Ng, Ns) < 0.f) 102 | Ns = Ns - 2.f * dot(Ng, Ns) * Ng; 103 | Ns = normalize(Ns); 104 | 105 | // ------------------------------------------------------------------ 106 | // compute diffuse material color, including diffuse texture, if 107 | // available 108 | // ------------------------------------------------------------------ 109 | V3f32 diffuseColor = sbtData.color; 110 | if (sbtData.has_texture && !sbtData.texcoord.is_null()) { 111 | const V2f32 tc = (1.f - u - v) * sbtData.texcoord[index.x] + 112 | u * sbtData.texcoord[index.y] + 113 | v * sbtData.texcoord[index.z]; 114 | 115 | V4f32 fromTexture = tex2D(sbtData.texture, tc.x, tc.y); 116 | diffuseColor = diffuseColor * fromTexture.xyz(); 117 | } 118 | 119 | // ------------------------------------------------------------------ 120 | // compute shadow 121 | // ------------------------------------------------------------------ 122 | const V3f32 surfPos = (1.f - u - v) * sbtData.vertex[index.x] + 123 | u * sbtData.vertex[index.y] + 124 | v * sbtData.vertex[index.z]; 125 | const V3f32 lightPos(-907.108f, 2205.875f, -400.0267f); 126 | const V3f32 lightDir = lightPos - surfPos; 127 | 128 | // trace shadow ray: 129 | V3f32 lightVisibility(1.f); 130 | // the values we store the PRD pointer in: 131 | u32 u0, u1; 132 | packPointer(&lightVisibility, u0, u1); 133 | optixTrace(optixLaunchParams.traversable, surfPos + 1e-3f * Ng, lightDir, 134 | 1e-3f, // tmin 135 | 1.f - 1e-3f, // tmax 136 | 0.0f, // rayTime 137 | OptixVisibilityMask(255), 138 | // anyhit ON for shadow rays: 139 | OPTIX_RAY_FLAG_NONE, 140 | SHADOW_RAY_TYPE, // SBT offset 141 | RAY_TYPE_COUNT, // SBT stride 142 | SHADOW_RAY_TYPE, // missSBTIndex 143 | u0, u1); 144 | 145 | // ------------------------------------------------------------------ 146 | // final shading: a bit of ambient, a bit of directional ambient, 147 | // and directional component based on shadowing 148 | // ------------------------------------------------------------------ 149 | const float cosDN = 0.1f + .8f * fabsf(dot(rayDir, Ns)); 150 | 151 | V3f32& prd = *(V3f32*)getPRD(); 152 | prd = (.1f + (.2f + .8f * lightVisibility) * cosDN) * diffuseColor; 153 | } 154 | 155 | extern "C" __global__ void 156 | __anyhit__radiance() { /*! for this simple example, this will remain empty */ 157 | } 158 | 159 | extern "C" __global__ void __anyhit__shadow() { 160 | // in this simple example, we terminate on ANY hit 161 | V3f32& prd = *(V3f32*)getPRD(); 162 | prd = V3f32(0.f); 163 | optixTerminateRay(); 164 | } 165 | 166 | //------------------------------------------------------------------------------ 167 | // miss program that gets called for any ray that did not have a 168 | // valid intersection 169 | // 170 | // as with the anyhit/closest hit programs, in this example we only 171 | // need to have _some_ dummy function to set up a valid SBT 172 | // ------------------------------------------------------------------------------ 173 | 174 | extern "C" __global__ void __miss__radiance() { 175 | V3f32& prd = *(V3f32*)getPRD(); 176 | // set to constant white as background color 177 | prd = V3f32(1.f); 178 | } 179 | 180 | extern "C" __global__ void __miss__shadow() { 181 | // misses shouldn't mess with shadow opacity - do nothing 182 | } 183 | 184 | //------------------------------------------------------------------------------ 185 | // ray gen program - the actual rendering happens in here 186 | //------------------------------------------------------------------------------ 187 | extern "C" __global__ void __raygen__renderFrame() { 188 | // compute a test pattern based on pixel ID 189 | const int ix = optixGetLaunchIndex().x; 190 | const int iy = optixGetLaunchIndex().y; 191 | 192 | const auto& camera = optixLaunchParams.camera; 193 | 194 | // our per-ray data for this example. what we initialize it to 195 | // won't matter, since this value will be overwritten by either 196 | // the miss or hit program, anyway 197 | V3f32 pixelColorPRD = V3f32(0.f, 0.0f, 0.0f); 198 | 199 | // the values we store the PRD pointer in: 200 | u32 u0, u1; 201 | packPointer(&pixelColorPRD, u0, u1); 202 | 203 | // normalized screen plane position, in [0,1]^2 204 | const V2f32 screen = 205 | V2f32(f32(ix) + .5f, f32(iy) + .5f) / 206 | V2f32(optixLaunchParams.frame.size.x, optixLaunchParams.frame.size.y); 207 | 208 | // generate ray direction 209 | V3f32 rayDir = 210 | normalize(camera.direction + (screen.x - 0.5f) * camera.horizontal + 211 | (screen.y - 0.5f) * camera.vertical); 212 | 213 | optixTrace(optixLaunchParams.traversable, (float3)camera.position, 214 | (float3)rayDir, 215 | 0.f, // tmin 216 | 1e20f, // tmax 217 | 0.0f, // rayTime 218 | OptixVisibilityMask(255), 219 | OPTIX_RAY_FLAG_DISABLE_ANYHIT, // OPTIX_RAY_FLAG_NONE, 220 | RADIANCE_RAY_TYPE, // SBT offset 221 | RAY_TYPE_COUNT, // SBT stride 222 | RADIANCE_RAY_TYPE, // missSBTIndex 223 | u0, u1); 224 | 225 | // and write to frame buffer ... 226 | const u32 fbIndex = ix + iy * optixLaunchParams.frame.size.x; 227 | optixLaunchParams.frame.color_buffer[fbIndex] = 228 | make_float4(pixelColorPRD.x, pixelColorPRD.y, pixelColorPRD.z, 1.0f); 229 | } 230 | 231 | } // namespace osc 232 | -------------------------------------------------------------------------------- /optix/examples/09_shadow/main.rs: -------------------------------------------------------------------------------- 1 | #[macro_use] 2 | extern crate enum_primitive; 3 | use num::FromPrimitive; 4 | 5 | mod sample_renderer; 6 | use sample_renderer::*; 7 | 8 | use glfw::{Action, Context, Key}; 9 | pub mod gl_util; 10 | use crate::gl_util::*; 11 | 12 | use optix::cuda::TaggedMallocator; 13 | use optix::math::*; 14 | 15 | use std::rc::Rc; 16 | 17 | fn main() { 18 | let mut glfw = glfw::init(glfw::FAIL_ON_ERRORS).unwrap(); 19 | glfw.window_hint(glfw::WindowHint::ContextVersion(4, 1)); 20 | glfw.window_hint(glfw::WindowHint::OpenGlForwardCompat(true)); 21 | glfw.window_hint(glfw::WindowHint::OpenGlProfile( 22 | glfw::OpenGlProfileHint::Core, 23 | )); 24 | 25 | let mut width = 960u32; 26 | let mut height = 540u32; 27 | 28 | let model = load_model(&std::path::Path::new(&format!( 29 | "{}/examples/data/sponza.obj", 30 | std::env::var("CARGO_MANIFEST_DIR").unwrap() 31 | ))); 32 | 33 | let camera = Camera { 34 | from: v3f32(-1293.07, 154.681, -0.7304), 35 | at: model.bounds.center() - v3f32(0.0, 400.0, 0.0), 36 | up: v3f32(0.0, 1.0, 0.0), 37 | }; 38 | 39 | let mut sample = 40 | SampleRenderer::new(v2i32(width as i32, height as i32), camera, model) 41 | .unwrap(); 42 | 43 | let (mut window, events) = glfw 44 | .create_window( 45 | width, 46 | height, 47 | "Example 09: shadow", 48 | glfw::WindowMode::Windowed, 49 | ) 50 | .expect("failed to create glfw window"); 51 | 52 | window.set_key_polling(true); 53 | window.make_current(); 54 | 55 | // retina displays will return a higher res for the framebuffer 56 | // which we need to use for the viewport 57 | let (fb_width, fb_height) = window.get_framebuffer_size(); 58 | 59 | gl::load_with(|s| { 60 | glfw.get_proc_address_raw(s) as *const std::os::raw::c_void 61 | }); 62 | 63 | let mut fsq = FullscreenQuad::new(width, height).unwrap(); 64 | 65 | let mut image_data = 66 | vec![v4f32(0.0, 0.0, 0.0, 0.0); (width * height) as usize]; 67 | 68 | unsafe { 69 | gl::Viewport(0, 0, fb_width, fb_height); 70 | }; 71 | 72 | while !window.should_close() { 73 | glfw.poll_events(); 74 | for (_, event) in glfw::flush_messages(&events) { 75 | handle_window_event(&mut window, event); 76 | } 77 | 78 | let (w, h) = window.get_framebuffer_size(); 79 | let w = w as u32; 80 | let h = h as u32; 81 | if w != width || h != height { 82 | fsq.resize(w, h); 83 | sample.resize(v2i32(w as i32, h as i32)); 84 | width = w; 85 | height = h; 86 | image_data 87 | .resize((width * height) as usize, v4f32(0.0, 0.0, 0.0, 0.0)); 88 | } 89 | 90 | sample.render(); 91 | sample.download_pixels(&mut image_data).unwrap(); 92 | fsq.update_texture(&image_data); 93 | fsq.set_progression(1); 94 | 95 | // draw the quad 96 | fsq.draw(); 97 | 98 | window.swap_buffers(); 99 | } 100 | } 101 | 102 | fn handle_window_event(window: &mut glfw::Window, event: glfw::WindowEvent) { 103 | match event { 104 | glfw::WindowEvent::Key(Key::Escape, _, Action::Press, _) => { 105 | window.set_should_close(true) 106 | } 107 | _ => {} 108 | } 109 | } 110 | 111 | fn load_texture(path: &std::path::Path) -> Option> { 112 | let im = match image::open(path) { 113 | Ok(im) => im.to_rgba(), 114 | Err(e) => { 115 | println!("{}", e); 116 | return None; 117 | } 118 | }; 119 | 120 | let dim = im.dimensions(); 121 | 122 | Some(Rc::new(Texture { 123 | resolution: v2i32(dim.0 as i32, dim.1 as i32), 124 | pixels: im.into_raw(), 125 | })) 126 | } 127 | 128 | fn load_model(path: &std::path::Path) -> Model { 129 | let (models, materials) = tobj::load_obj(path).unwrap(); 130 | 131 | let mut bounds = Box3f32::make_empty(); 132 | let mut loaded_texture_ids = std::collections::HashMap::new(); 133 | let mut textures = Vec::new(); 134 | let meshes = models 135 | .into_iter() 136 | .map(|model| { 137 | let (diffuse, diffuse_texture_id) = if let Some(material_id) = 138 | model.mesh.material_id 139 | { 140 | let diffuse = materials[material_id].diffuse.into(); 141 | 142 | // load the diffuse texture if there is one 143 | let diffuse_texture = &materials[material_id].diffuse_texture; 144 | let diffuse_texture_id = if diffuse_texture != "" { 145 | // if our texture cache has an entry for this texture 146 | // just return the (maybe) texture_id 147 | if loaded_texture_ids.contains_key(&diffuse_texture) { 148 | loaded_texture_ids[&diffuse_texture] 149 | } else { 150 | // If we don't have an entry, try and load the texture 151 | let full_texture_path = std::path::PathBuf::from( 152 | std::env::var("CARGO_MANIFEST_DIR").unwrap(), 153 | ) 154 | .join("examples/data") 155 | .join(diffuse_texture.replace("\\", "/")); 156 | 157 | match load_texture(&full_texture_path) { 158 | Some(texture) => { 159 | textures.push(texture); 160 | let texture_id = Some(textures.len() - 1); 161 | loaded_texture_ids 162 | .insert(diffuse_texture, texture_id); 163 | texture_id 164 | } 165 | None => { 166 | loaded_texture_ids 167 | .insert(diffuse_texture, None); 168 | None 169 | } 170 | } 171 | } 172 | } else { 173 | None 174 | }; 175 | 176 | (diffuse, diffuse_texture_id) 177 | } else { 178 | (v3f32(0.8, 0.8, 0.8), None) 179 | }; 180 | 181 | let vertex: Vec = model 182 | .mesh 183 | .positions 184 | .chunks(3) 185 | .map(|c| { 186 | let p = v3f32(c[0], c[1], c[2]); 187 | bounds.extend_by_pnt(p); 188 | p 189 | }) 190 | .collect(); 191 | // println!("Mesh has {} vertices", vertex.len()); 192 | 193 | let normal: Vec = model 194 | .mesh 195 | .normals 196 | .chunks(3) 197 | .map(|c| v3f32(c[0], c[1], c[2])) 198 | .collect(); 199 | 200 | let texcoord: Vec = model 201 | .mesh 202 | .texcoords 203 | .chunks(2) 204 | .map(|c| v2f32(c[0], c[1])) 205 | .collect(); 206 | 207 | let index: Vec = model 208 | .mesh 209 | .indices 210 | .chunks(3) 211 | .map(|c| v3i32(c[0] as i32, c[1] as i32, c[2] as i32)) 212 | .collect(); 213 | 214 | Mesh { 215 | vertex, 216 | normal, 217 | texcoord, 218 | index, 219 | diffuse, 220 | diffuse_texture_id, 221 | } 222 | }) 223 | .collect::>(); 224 | 225 | Model { 226 | meshes, 227 | textures, 228 | bounds, 229 | } 230 | } 231 | -------------------------------------------------------------------------------- /optix/examples/10_softshadow/main.rs: -------------------------------------------------------------------------------- 1 | mod sample_renderer; 2 | use sample_renderer::*; 3 | 4 | use glfw::{Action, Context, Key}; 5 | pub mod gl_util; 6 | use crate::gl_util::*; 7 | 8 | use optix::math::*; 9 | 10 | use std::rc::Rc; 11 | 12 | fn main() { 13 | let mut glfw = glfw::init(glfw::FAIL_ON_ERRORS).unwrap(); 14 | glfw.window_hint(glfw::WindowHint::ContextVersion(4, 1)); 15 | glfw.window_hint(glfw::WindowHint::OpenGlForwardCompat(true)); 16 | glfw.window_hint(glfw::WindowHint::OpenGlProfile( 17 | glfw::OpenGlProfileHint::Core, 18 | )); 19 | 20 | let mut width = 960u32; 21 | let mut height = 540u32; 22 | 23 | let model = load_model(&std::path::Path::new(&format!( 24 | "{}/examples/data/sponza.obj", 25 | std::env::var("CARGO_MANIFEST_DIR").unwrap() 26 | ))); 27 | 28 | let camera = Camera { 29 | from: v3f32(-1293.07, 154.681, -0.7304), 30 | at: model.bounds.center() - v3f32(0.0, 400.0, 0.0), 31 | up: v3f32(0.0, 1.0, 0.0), 32 | }; 33 | 34 | let light_size = 200.0; 35 | let light = QuadLight { 36 | origin: v3f32(-1000.0 - light_size, 800.0, -light_size), 37 | du: v3f32(2.0 * light_size, 0.0, 0.0), 38 | dv: v3f32(0.0, 0.0, 2.0 * light_size), 39 | power: v3f32(3000000.0, 3000000.0, 3000000.0), 40 | }; 41 | 42 | let mut sample = SampleRenderer::new( 43 | v2i32(width as i32, height as i32), 44 | camera, 45 | model, 46 | light, 47 | ) 48 | .unwrap(); 49 | 50 | let (mut window, events) = glfw 51 | .create_window( 52 | width, 53 | height, 54 | "Example 10: soft shadow", 55 | glfw::WindowMode::Windowed, 56 | ) 57 | .expect("failed to create glfw window"); 58 | 59 | window.set_key_polling(true); 60 | window.make_current(); 61 | 62 | // retina displays will return a higher res for the framebuffer 63 | // which we need to use for the viewport 64 | let (fb_width, fb_height) = window.get_framebuffer_size(); 65 | 66 | gl::load_with(|s| { 67 | glfw.get_proc_address_raw(s) as *const std::os::raw::c_void 68 | }); 69 | 70 | let mut fsq = FullscreenQuad::new(width, height).unwrap(); 71 | 72 | let mut image_data: Vec = 73 | vec![v4f32(0.0, 0.0, 0.0, 0.0).into(); (width * height) as usize]; 74 | 75 | unsafe { 76 | gl::Viewport(0, 0, fb_width, fb_height); 77 | }; 78 | 79 | while !window.should_close() { 80 | glfw.poll_events(); 81 | for (_, event) in glfw::flush_messages(&events) { 82 | handle_window_event(&mut window, event); 83 | } 84 | 85 | let (w, h) = window.get_framebuffer_size(); 86 | let w = w as u32; 87 | let h = h as u32; 88 | if w != width || h != height { 89 | fsq.resize(w, h); 90 | sample.resize(v2i32(w as i32, h as i32)); 91 | width = w; 92 | height = h; 93 | image_data.resize( 94 | (width * height) as usize, 95 | v4f32(0.0, 0.0, 0.0, 0.0).into(), 96 | ); 97 | } 98 | 99 | sample.render(); 100 | sample.download_pixels(image_data.as_mut_slice()).unwrap(); 101 | fsq.update_texture(&image_data); 102 | fsq.set_progression(1); 103 | 104 | // draw the quad 105 | fsq.draw(); 106 | 107 | window.swap_buffers(); 108 | } 109 | } 110 | 111 | fn handle_window_event(window: &mut glfw::Window, event: glfw::WindowEvent) { 112 | match event { 113 | glfw::WindowEvent::Key(Key::Escape, _, Action::Press, _) => { 114 | window.set_should_close(true) 115 | } 116 | _ => {} 117 | } 118 | } 119 | 120 | fn load_texture(path: &std::path::Path) -> Option> { 121 | let im = match image::open(path) { 122 | Ok(im) => im.to_rgba(), 123 | Err(e) => { 124 | println!("{}", e); 125 | return None; 126 | } 127 | }; 128 | 129 | let dim = im.dimensions(); 130 | 131 | Some(Rc::new(Texture { 132 | resolution: v2i32(dim.0 as i32, dim.1 as i32), 133 | pixels: im.into_raw(), 134 | })) 135 | } 136 | 137 | fn load_model(path: &std::path::Path) -> Model { 138 | let (models, materials) = tobj::load_obj(path).unwrap(); 139 | 140 | let mut bounds = Box3f32::make_empty(); 141 | let mut loaded_texture_ids = std::collections::HashMap::new(); 142 | let mut textures = Vec::new(); 143 | let meshes = models 144 | .into_iter() 145 | .map(|model| { 146 | let (diffuse, diffuse_texture_id) = if let Some(material_id) = 147 | model.mesh.material_id 148 | { 149 | let diffuse = materials[material_id].diffuse.into(); 150 | 151 | // load the diffuse texture if there is one 152 | let diffuse_texture = &materials[material_id].diffuse_texture; 153 | let diffuse_texture_id = if diffuse_texture != "" { 154 | // if our texture cache has an entry for this texture 155 | // just return the (maybe) texture_id 156 | if loaded_texture_ids.contains_key(&diffuse_texture) { 157 | loaded_texture_ids[&diffuse_texture] 158 | } else { 159 | // If we don't have an entry, try and load the texture 160 | let full_texture_path = std::path::PathBuf::from( 161 | std::env::var("CARGO_MANIFEST_DIR").unwrap(), 162 | ) 163 | .join("examples/data") 164 | .join(diffuse_texture.replace("\\", "/")); 165 | 166 | match load_texture(&full_texture_path) { 167 | Some(texture) => { 168 | textures.push(texture); 169 | let texture_id = Some(textures.len() - 1); 170 | loaded_texture_ids 171 | .insert(diffuse_texture, texture_id); 172 | texture_id 173 | } 174 | None => { 175 | loaded_texture_ids 176 | .insert(diffuse_texture, None); 177 | None 178 | } 179 | } 180 | } 181 | } else { 182 | None 183 | }; 184 | 185 | (diffuse, diffuse_texture_id) 186 | } else { 187 | (v3f32(0.8, 0.8, 0.8), None) 188 | }; 189 | 190 | let vertex: Vec = model 191 | .mesh 192 | .positions 193 | .chunks(3) 194 | .map(|c| { 195 | let p = v3f32(c[0], c[1], c[2]); 196 | bounds.extend_by_pnt(p); 197 | p.into() 198 | }) 199 | .collect(); 200 | // println!("Mesh has {} vertices", vertex.len()); 201 | 202 | let normal: Vec = model 203 | .mesh 204 | .normals 205 | .chunks(3) 206 | .map(|c| v3f32(c[0], c[1], c[2]).into()) 207 | .collect(); 208 | 209 | let texcoord: Vec = model 210 | .mesh 211 | .texcoords 212 | .chunks(2) 213 | .map(|c| v2f32(c[0], c[1]).into()) 214 | .collect(); 215 | 216 | let index: Vec = model 217 | .mesh 218 | .indices 219 | .chunks(3) 220 | .map(|c| v3i32(c[0] as i32, c[1] as i32, c[2] as i32).into()) 221 | .collect(); 222 | 223 | Mesh { 224 | vertex, 225 | normal, 226 | texcoord, 227 | index, 228 | diffuse, 229 | diffuse_texture_id, 230 | } 231 | }) 232 | .collect::>(); 233 | 234 | Model { 235 | meshes, 236 | textures, 237 | bounds, 238 | } 239 | } 240 | -------------------------------------------------------------------------------- /optix/examples/common/lcg.h: -------------------------------------------------------------------------------- 1 | #include "types.h" 2 | 3 | namespace osc { 4 | /*! simple 24-bit linear congruence generator */ 5 | template struct LCG { 6 | 7 | inline DEVICE 8 | LCG() { /* intentionally empty so we can use it in device vars that 9 | don't allow dynamic initialization (ie, PRD) */ 10 | } 11 | inline DEVICE LCG(u32 val0, u32 val1) { init(val0, val1); } 12 | 13 | inline DEVICE void init(u32 val0, u32 val1) { 14 | u32 v0 = val0; 15 | u32 v1 = val1; 16 | u32 s0 = 0; 17 | 18 | for (u32 n = 0; n < N; n++) { 19 | s0 += 0x9e3779b9; 20 | v0 += 21 | ((v1 << 4) + 0xa341316c) ^ (v1 + s0) ^ ((v1 >> 5) + 0xc8013ea4); 22 | v1 += 23 | ((v0 << 4) + 0xad90777d) ^ (v0 + s0) ^ ((v0 >> 5) + 0x7e95761e); 24 | } 25 | state = v0; 26 | } 27 | 28 | // Generate random u32 in [0, 2^24) 29 | inline DEVICE float operator()() { 30 | const u32 LCG_A = 1664525u; 31 | const u32 LCG_C = 1013904223u; 32 | state = (LCG_A * state + LCG_C); 33 | return (state & 0x00FFFFFF) / (float)0x01000000; 34 | } 35 | 36 | u32 state; 37 | }; 38 | 39 | } // namespace osc 40 | -------------------------------------------------------------------------------- /optix/examples/common/types.h: -------------------------------------------------------------------------------- 1 | #pragma once 2 | 3 | #if __CUDACC__ 4 | #define DEVICE __device__ 5 | #define FORCEINLINE __forceinline__ 6 | #else 7 | #define DEVICE 8 | #define FORCEINLINE 9 | #endif 10 | 11 | namespace osc { 12 | using f32 = float; 13 | using f64 = double; 14 | 15 | using i8 = char; 16 | using i16 = short; 17 | using i32 = int; 18 | using i64 = long long; 19 | 20 | using u8 = unsigned char; 21 | using u16 = unsigned short; 22 | using u32 = unsigned int; 23 | using u64 = unsigned long long; 24 | } // namespace osc -------------------------------------------------------------------------------- /optix/examples/data/sponza.mtl: -------------------------------------------------------------------------------- 1 | # 3ds Max Wavefront OBJ Exporter v0.94b - (c)2007 guruware 2 | # File Created: 09.08.2011 00:15:24 3 | 4 | newmtl sp_00_luk_mali 5 | Ns 50.0000 6 | Ni 1.5000 7 | d 1.0000 8 | Tr 1.0000 9 | Tf 1.0000 1.0000 1.0000 10 | illum 2 11 | Ka 0.0000 0.0000 0.0000 12 | Kd 0.7451 0.7098 0.6745 13 | Ks 0.0000 0.0000 0.0000 14 | Ke 0.0000 0.0000 0.0000 15 | map_Ka sponzaMaps\sp_luk.JPG 16 | map_Kd sponzaMaps\sp_luk.JPG 17 | map_bump sponzaMaps\sp_luk.JPG 18 | bump sponzaMaps\sp_luk.JPG 19 | 20 | newmtl sp_svod_kapitel 21 | Ns 50.0000 22 | Ni 1.5000 23 | d 1.0000 24 | Tr 1.0000 25 | Tf 1.0000 1.0000 1.0000 26 | illum 2 27 | Ka 0.0000 0.0000 0.0000 28 | Kd 0.7137 0.7059 0.6588 29 | Ks 0.0000 0.0000 0.0000 30 | Ke 0.0000 0.0000 0.0000 31 | map_Ka sponzaMaps\00_skap.JPG 32 | map_Kd sponzaMaps\00_skap.JPG 33 | map_bump sponzaMaps\00_skap.JPG 34 | bump sponzaMaps\00_skap.JPG 35 | 36 | newmtl sp_01_stub_baza_ 37 | Ns 20.0000 38 | Ni 1.5000 39 | d 1.0000 40 | Tr 1.0000 41 | Tf 1.0000 1.0000 1.0000 42 | illum 2 43 | Ka 0.0000 0.0000 0.0000 44 | Kd 0.7843 0.7843 0.7843 45 | Ks 0.0000 0.0000 0.0000 46 | Ke 0.0000 0.0000 0.0000 47 | 48 | newmtl sp_01_stub_kut 49 | Ns 50.0000 50 | Ni 1.5000 51 | d 1.0000 52 | Tr 1.0000 53 | Tf 1.0000 1.0000 1.0000 54 | illum 2 55 | Ka 0.0000 0.0000 0.0000 56 | Kd 0.7373 0.7098 0.6706 57 | Ks 0.0000 0.0000 0.0000 58 | Ke 0.0000 0.0000 0.0000 59 | map_Ka sponzaMaps\01_STUB.JPG 60 | map_Kd sponzaMaps\01_STUB.JPG 61 | map_bump sponzaMaps\01_STUB.JPG 62 | bump sponzaMaps\01_STUB.JPG 63 | 64 | newmtl sp_00_stup 65 | Ns 50.0000 66 | Ni 1.5000 67 | d 1.0000 68 | Tr 1.0000 69 | Tf 1.0000 1.0000 1.0000 70 | illum 2 71 | Ka 0.0000 0.0000 0.0000 72 | Kd 0.7373 0.7098 0.6706 73 | Ks 0.0000 0.0000 0.0000 74 | Ke 0.0000 0.0000 0.0000 75 | map_Ka sponzaMaps\01_STUB.JPG 76 | map_Kd sponzaMaps\01_STUB.JPG 77 | map_bump sponzaMaps\01_STUB.JPG 78 | bump sponzaMaps\01_STUB.JPG 79 | 80 | newmtl sp_01_stub_baza 81 | Ns 50.0000 82 | Ni 1.5000 83 | d 1.0000 84 | Tr 1.0000 85 | Tf 1.0000 1.0000 1.0000 86 | illum 2 87 | Ka 0.0000 0.0000 0.0000 88 | Kd 0.8000 0.7843 0.7490 89 | Ks 0.0000 0.0000 0.0000 90 | Ke 0.0000 0.0000 0.0000 91 | map_Ka sponzaMaps\01_S_ba.JPG 92 | map_Kd sponzaMaps\01_S_ba.JPG 93 | map_bump sponzaMaps\01_S_ba.JPG 94 | bump sponzaMaps\01_S_ba.JPG 95 | 96 | newmtl sp_01_stub 97 | Ns 50.0000 98 | Ni 1.5000 99 | d 1.0000 100 | Tr 1.0000 101 | Tf 1.0000 1.0000 1.0000 102 | illum 2 103 | Ka 0.0000 0.0000 0.0000 104 | Kd 0.7373 0.7098 0.6706 105 | Ks 0.0000 0.0000 0.0000 106 | Ke 0.0000 0.0000 0.0000 107 | map_Ka sponzaMaps\01_STUB.JPG 108 | map_Kd sponzaMaps\01_STUB.JPG 109 | map_bump sponzaMaps\01_STUB.JPG 110 | bump sponzaMaps\01_STUB.JPG 111 | 112 | newmtl sp_01_stup 113 | Ns 50.0000 114 | Ni 1.5000 115 | d 1.0000 116 | Tr 1.0000 117 | Tf 1.0000 1.0000 1.0000 118 | illum 2 119 | Ka 0.0000 0.0000 0.0000 120 | Kd 0.8275 0.8000 0.7686 121 | Ks 0.0000 0.0000 0.0000 122 | Ke 0.0000 0.0000 0.0000 123 | map_Ka sponzaMaps\x01_st.JPG 124 | map_Kd sponzaMaps\x01_st.JPG 125 | 126 | newmtl sp_vijenac 127 | Ns 50.0000 128 | Ni 1.5000 129 | d 1.0000 130 | Tr 1.0000 131 | Tf 1.0000 1.0000 1.0000 132 | illum 2 133 | Ka 0.0000 0.0000 0.0000 134 | Kd 0.7137 0.7059 0.6588 135 | Ks 0.0000 0.0000 0.0000 136 | Ke 0.0000 0.0000 0.0000 137 | map_Ka sponzaMaps\00_skap.JPG 138 | map_Kd sponzaMaps\00_skap.JPG 139 | map_bump sponzaMaps\00_skap.JPG 140 | bump sponzaMaps\00_skap.JPG 141 | 142 | newmtl sp_00_pod 143 | Ns 50.0000 144 | Ni 1.5000 145 | d 1.0000 146 | Tr 1.0000 147 | Tf 1.0000 1.0000 1.0000 148 | illum 2 149 | Ka 0.0000 0.0000 0.0000 150 | Kd 0.6275 0.5725 0.5608 151 | Ks 0.0000 0.0000 0.0000 152 | Ke 0.0000 0.0000 0.0000 153 | map_Ka sponzaMaps\KAMEN.JPG 154 | map_Kd sponzaMaps\KAMEN.JPG 155 | map_bump sponzaMaps\KAMEN.JPG 156 | bump sponzaMaps\KAMEN.JPG 157 | 158 | newmtl sp_00_svod 159 | Ns 0.0000 160 | Ni 1.5000 161 | d 1.0000 162 | Tr 1.0000 163 | Tf 1.0000 1.0000 1.0000 164 | illum 2 165 | Ka 0.0000 0.0000 0.0000 166 | Kd 0.9412 0.8667 0.7373 167 | Ks 0.0340 0.0323 0.0293 168 | Ke 0.0000 0.0000 0.0000 169 | 170 | newmtl sp_02_reljef 171 | Ns 50.0000 172 | Ni 1.5000 173 | d 1.0000 174 | Tr 1.0000 175 | Tf 1.0000 1.0000 1.0000 176 | illum 2 177 | Ka 0.0000 0.0000 0.0000 178 | Kd 0.5294 0.4980 0.4902 179 | Ks 0.0000 0.0000 0.0000 180 | Ke 0.0000 0.0000 0.0000 181 | map_Ka sponzaMaps\reljef.JPG 182 | map_Kd sponzaMaps\reljef.JPG 183 | map_bump sponzaMaps\reljef.JPG 184 | bump sponzaMaps\reljef.JPG 185 | 186 | newmtl sp_01_luk_a 187 | Ns 50.0000 188 | Ni 1.5000 189 | d 1.0000 190 | Tr 1.0000 191 | Tf 1.0000 1.0000 1.0000 192 | illum 2 193 | Ka 0.0000 0.0000 0.0000 194 | Kd 0.7451 0.7098 0.6745 195 | Ks 0.0000 0.0000 0.0000 196 | Ke 0.0000 0.0000 0.0000 197 | map_Ka sponzaMaps\sp_luk.JPG 198 | map_Kd sponzaMaps\sp_luk.JPG 199 | map_bump sponzaMaps\sp_luk.JPG 200 | bump sponzaMaps\sp_luk.JPG 201 | 202 | newmtl sp_zid_vani 203 | Ns 50.0000 204 | Ni 1.5000 205 | d 1.0000 206 | Tr 1.0000 207 | Tf 1.0000 1.0000 1.0000 208 | illum 2 209 | Ka 0.0000 0.0000 0.0000 210 | Kd 0.6275 0.5725 0.5608 211 | Ks 0.0000 0.0000 0.0000 212 | Ke 0.0000 0.0000 0.0000 213 | map_Ka sponzaMaps\KAMEN.JPG 214 | map_Kd sponzaMaps\KAMEN.JPG 215 | map_bump sponzaMaps\KAMEN.JPG 216 | bump sponzaMaps\KAMEN.JPG 217 | 218 | newmtl sp_01_stup_baza 219 | Ns 50.0000 220 | Ni 1.5000 221 | d 1.0000 222 | Tr 1.0000 223 | Tf 1.0000 1.0000 1.0000 224 | illum 2 225 | Ka 0.0000 0.0000 0.0000 226 | Kd 0.8000 0.7843 0.7490 227 | Ks 0.0000 0.0000 0.0000 228 | Ke 0.0000 0.0000 0.0000 229 | map_Ka sponzaMaps\01_S_ba.JPG 230 | map_Kd sponzaMaps\01_S_ba.JPG 231 | map_bump sponzaMaps\01_S_ba.JPG 232 | bump sponzaMaps\01_S_ba.JPG 233 | 234 | newmtl sp_00_zid 235 | Ns 50.0000 236 | Ni 1.5000 237 | d 1.0000 238 | Tr 1.0000 239 | Tf 1.0000 1.0000 1.0000 240 | illum 2 241 | Ka 0.0000 0.0000 0.0000 242 | Kd 0.6275 0.5725 0.5608 243 | Ks 0.0000 0.0000 0.0000 244 | Ke 0.0000 0.0000 0.0000 245 | map_Ka sponzaMaps\KAMEN.JPG 246 | map_Kd sponzaMaps\KAMEN.JPG 247 | map_bump sponzaMaps\KAMEN.JPG 248 | bump sponzaMaps\KAMEN.JPG 249 | 250 | newmtl sp_00_prozor 251 | Ns 50.0000 252 | Ni 1.5000 253 | d 1.0000 254 | Tr 1.0000 255 | Tf 1.0000 1.0000 1.0000 256 | illum 2 257 | Ka 0.0000 0.0000 0.0000 258 | Kd 1.0000 1.0000 1.0000 259 | Ks 0.0000 0.0000 0.0000 260 | Ke 0.0000 0.0000 0.0000 261 | map_Ka sponzaMaps\prozor1.JPG 262 | map_Kd sponzaMaps\prozor1.JPG 263 | map_bump sponzaMaps\prozor1.JPG 264 | bump sponzaMaps\prozor1.JPG 265 | 266 | newmtl sp_00_vrata_krug 267 | Ns 20.0000 268 | Ni 1.5000 269 | d 1.0000 270 | Tr 1.0000 271 | Tf 1.0000 1.0000 1.0000 272 | illum 2 273 | Ka 0.0000 0.0000 0.0000 274 | Kd 0.7843 0.7843 0.7843 275 | Ks 0.0000 0.0000 0.0000 276 | Ke 0.0000 0.0000 0.0000 277 | map_Ka sponzaMaps\vrata_kr.JPG 278 | map_Kd sponzaMaps\vrata_kr.JPG 279 | map_bump sponzaMaps\vrata_kr.JPG 280 | bump sponzaMaps\vrata_kr.JPG 281 | 282 | newmtl sp_00_vrata_kock 283 | Ns 20.0000 284 | Ni 1.5000 285 | d 1.0000 286 | Tr 1.0000 287 | Tf 1.0000 1.0000 1.0000 288 | illum 2 289 | Ka 0.0000 0.0000 0.0000 290 | Kd 0.7843 0.7843 0.7843 291 | Ks 0.0000 0.0000 0.0000 292 | Ke 0.0000 0.0000 0.0000 293 | map_Ka sponzaMaps\vrata_ko.JPG 294 | map_Kd sponzaMaps\vrata_ko.JPG 295 | map_bump sponzaMaps\vrata_ko.JPG 296 | bump sponzaMaps\vrata_ko.JPG 297 | -------------------------------------------------------------------------------- /optix/examples/data/sponzaMaps/00_skap.JPG: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/anderslanglands/optix-rs/78542bb137932008e22c2f2f7facc1a7833451c8/optix/examples/data/sponzaMaps/00_skap.JPG -------------------------------------------------------------------------------- /optix/examples/data/sponzaMaps/01_STUB-bump.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/anderslanglands/optix-rs/78542bb137932008e22c2f2f7facc1a7833451c8/optix/examples/data/sponzaMaps/01_STUB-bump.jpg -------------------------------------------------------------------------------- /optix/examples/data/sponzaMaps/01_STUB.JPG: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/anderslanglands/optix-rs/78542bb137932008e22c2f2f7facc1a7833451c8/optix/examples/data/sponzaMaps/01_STUB.JPG -------------------------------------------------------------------------------- /optix/examples/data/sponzaMaps/01_S_ba.JPG: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/anderslanglands/optix-rs/78542bb137932008e22c2f2f7facc1a7833451c8/optix/examples/data/sponzaMaps/01_S_ba.JPG -------------------------------------------------------------------------------- /optix/examples/data/sponzaMaps/01_S_kap-bump.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/anderslanglands/optix-rs/78542bb137932008e22c2f2f7facc1a7833451c8/optix/examples/data/sponzaMaps/01_S_kap-bump.jpg -------------------------------------------------------------------------------- /optix/examples/data/sponzaMaps/01_S_kap.JPG: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/anderslanglands/optix-rs/78542bb137932008e22c2f2f7facc1a7833451c8/optix/examples/data/sponzaMaps/01_S_kap.JPG -------------------------------------------------------------------------------- /optix/examples/data/sponzaMaps/01_St_kp-bump.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/anderslanglands/optix-rs/78542bb137932008e22c2f2f7facc1a7833451c8/optix/examples/data/sponzaMaps/01_St_kp-bump.jpg -------------------------------------------------------------------------------- /optix/examples/data/sponzaMaps/01_St_kp.JPG: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/anderslanglands/optix-rs/78542bb137932008e22c2f2f7facc1a7833451c8/optix/examples/data/sponzaMaps/01_St_kp.JPG -------------------------------------------------------------------------------- /optix/examples/data/sponzaMaps/KAMEN-bump.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/anderslanglands/optix-rs/78542bb137932008e22c2f2f7facc1a7833451c8/optix/examples/data/sponzaMaps/KAMEN-bump.jpg -------------------------------------------------------------------------------- /optix/examples/data/sponzaMaps/KAMEN-stup.JPG: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/anderslanglands/optix-rs/78542bb137932008e22c2f2f7facc1a7833451c8/optix/examples/data/sponzaMaps/KAMEN-stup.JPG -------------------------------------------------------------------------------- /optix/examples/data/sponzaMaps/KAMEN.JPG: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/anderslanglands/optix-rs/78542bb137932008e22c2f2f7facc1a7833451c8/optix/examples/data/sponzaMaps/KAMEN.JPG -------------------------------------------------------------------------------- /optix/examples/data/sponzaMaps/prozor1.JPG: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/anderslanglands/optix-rs/78542bb137932008e22c2f2f7facc1a7833451c8/optix/examples/data/sponzaMaps/prozor1.JPG -------------------------------------------------------------------------------- /optix/examples/data/sponzaMaps/reljef-bump.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/anderslanglands/optix-rs/78542bb137932008e22c2f2f7facc1a7833451c8/optix/examples/data/sponzaMaps/reljef-bump.jpg -------------------------------------------------------------------------------- /optix/examples/data/sponzaMaps/reljef.JPG: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/anderslanglands/optix-rs/78542bb137932008e22c2f2f7facc1a7833451c8/optix/examples/data/sponzaMaps/reljef.JPG -------------------------------------------------------------------------------- /optix/examples/data/sponzaMaps/sp_luk-bump.JPG: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/anderslanglands/optix-rs/78542bb137932008e22c2f2f7facc1a7833451c8/optix/examples/data/sponzaMaps/sp_luk-bump.JPG -------------------------------------------------------------------------------- /optix/examples/data/sponzaMaps/sp_luk.JPG: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/anderslanglands/optix-rs/78542bb137932008e22c2f2f7facc1a7833451c8/optix/examples/data/sponzaMaps/sp_luk.JPG -------------------------------------------------------------------------------- /optix/examples/data/sponzaMaps/vrata_ko.JPG: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/anderslanglands/optix-rs/78542bb137932008e22c2f2f7facc1a7833451c8/optix/examples/data/sponzaMaps/vrata_ko.JPG -------------------------------------------------------------------------------- /optix/examples/data/sponzaMaps/vrata_kr.JPG: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/anderslanglands/optix-rs/78542bb137932008e22c2f2f7facc1a7833451c8/optix/examples/data/sponzaMaps/vrata_kr.JPG -------------------------------------------------------------------------------- /optix/examples/data/sponzaMaps/x01_st-bump.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/anderslanglands/optix-rs/78542bb137932008e22c2f2f7facc1a7833451c8/optix/examples/data/sponzaMaps/x01_st-bump.jpg -------------------------------------------------------------------------------- /optix/examples/data/sponzaMaps/x01_st.JPG: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/anderslanglands/optix-rs/78542bb137932008e22c2f2f7facc1a7833451c8/optix/examples/data/sponzaMaps/x01_st.JPG -------------------------------------------------------------------------------- /optix/src/cuda/allocator.rs: -------------------------------------------------------------------------------- 1 | use super::{CUdeviceptr, Error}; 2 | use bitfield::*; 3 | use optix_sys::cuda_sys as sys; 4 | use std::cell::{Cell, Ref, RefCell}; 5 | use std::collections::HashMap; 6 | 7 | type Result = std::result::Result; 8 | 9 | pub trait Allocator { 10 | unsafe fn alloc( 11 | &self, 12 | size: usize, 13 | alignment: usize, 14 | tag: u64, 15 | ) -> Result; 16 | unsafe fn dealloc(&self, allocation: Allocation) -> Result<()>; 17 | } 18 | 19 | pub trait TaggedAllocator: Allocator { 20 | fn visit(&self, _: F) 21 | where 22 | F: FnMut(&HashMap); 23 | 24 | fn total_allocated(&self) -> usize; 25 | fn clone_map(&self) -> HashMap; 26 | } 27 | 28 | pub struct Mallocator {} 29 | 30 | impl Mallocator { 31 | pub fn new() -> Mallocator { 32 | Mallocator {} 33 | } 34 | } 35 | 36 | pub struct TaggedMallocator { 37 | total_allocated: Cell, 38 | allocs_by_tag: RefCell>, 39 | } 40 | 41 | impl TaggedMallocator { 42 | pub fn new() -> TaggedMallocator { 43 | TaggedMallocator { 44 | total_allocated: Cell::new(0), 45 | allocs_by_tag: RefCell::new(HashMap::new()), 46 | } 47 | } 48 | 49 | pub fn tag_allocations(&self) -> Ref> { 50 | self.allocs_by_tag.borrow() 51 | } 52 | 53 | pub fn total_allocated(&self) -> usize { 54 | self.total_allocated.get() 55 | } 56 | } 57 | 58 | pub struct Allocation { 59 | ptr: CUdeviceptr, 60 | tagged_size: TaggedSize, 61 | } 62 | 63 | impl Allocation { 64 | pub fn new(ptr: CUdeviceptr, size: usize, tag: u64) -> Allocation { 65 | let mut tagged_size = TaggedSize(0); 66 | tagged_size.set_size(size as u64); 67 | tagged_size.set_tag(tag); 68 | Allocation { ptr, tagged_size } 69 | } 70 | 71 | pub fn size(&self) -> usize { 72 | self.tagged_size.get_size() as usize 73 | } 74 | 75 | pub fn tag(&self) -> u64 { 76 | self.tagged_size.get_tag() 77 | } 78 | 79 | pub fn ptr(&self) -> CUdeviceptr { 80 | self.ptr 81 | } 82 | } 83 | 84 | bitfield! { 85 | pub struct TaggedSize(u64); 86 | get_size, set_size: 39, 0; 87 | get_tag, set_tag: 63, 40; 88 | } 89 | 90 | impl Allocator for Mallocator { 91 | unsafe fn alloc( 92 | &self, 93 | size: usize, 94 | alignment: usize, 95 | tag: u64, 96 | ) -> Result { 97 | // cuda mallocs are always 256 or 512-byte aligned on recent GPUs so we 98 | // can satisfy anything up to that 99 | // FIXME: check this don't assume it's 512 100 | if alignment > 512 || !alignment.is_power_of_two() { 101 | return Err(Error::AllocationAlignment { 102 | size, 103 | align: alignment, 104 | }); 105 | } 106 | 107 | let mut ptr = std::ptr::null_mut(); 108 | let res = sys::cudaMalloc(&mut ptr, size); 109 | if res != sys::cudaError::cudaSuccess || ptr.is_null() { 110 | Err(Error::AllocationFailed { 111 | source: res.into(), 112 | size: size, 113 | }) 114 | } else { 115 | Ok(Allocation::new(ptr as CUdeviceptr, size, tag)) 116 | } 117 | } 118 | 119 | unsafe fn dealloc(&self, allocation: Allocation) -> Result<()> { 120 | sys::cudaFree(allocation.ptr as *mut std::os::raw::c_void); 121 | Ok(()) 122 | } 123 | } 124 | 125 | impl Allocator for TaggedMallocator { 126 | unsafe fn alloc( 127 | &self, 128 | size: usize, 129 | alignment: usize, 130 | tag: u64, 131 | ) -> Result { 132 | // cuda mallocs are always 512-byte aligned on recent GPUs so we can 133 | // satisfy anything up to that 134 | // FIXME: check this don't assume it's 512 135 | if alignment > 512 || !alignment.is_power_of_two() { 136 | return Err(Error::AllocationAlignment { 137 | size, 138 | align: alignment, 139 | }); 140 | } 141 | 142 | let mut ptr = std::ptr::null_mut(); 143 | let res = sys::cudaMalloc(&mut ptr, size); 144 | if res != sys::cudaError::cudaSuccess || ptr.is_null() { 145 | Err(Error::AllocationFailed { 146 | source: res.into(), 147 | size: size, 148 | }) 149 | } else { 150 | let aligned_size = size.max(512); 151 | self.total_allocated 152 | .set(self.total_allocated.get() + aligned_size); 153 | *self.allocs_by_tag.borrow_mut().entry(tag).or_insert(0) += 154 | aligned_size; 155 | Ok(Allocation::new(ptr as CUdeviceptr, size, tag)) 156 | } 157 | } 158 | 159 | unsafe fn dealloc(&self, allocation: Allocation) -> Result<()> { 160 | sys::cudaFree(allocation.ptr as *mut std::os::raw::c_void); 161 | self.total_allocated 162 | .set(self.total_allocated.get() - allocation.size()); 163 | // *self 164 | // .allocs_by_tag 165 | // .borrow_mut() 166 | // .get_mut(&allocation.tag()) 167 | // .unwrap() -= allocation.size(); 168 | match self.allocs_by_tag.borrow_mut().get_mut(&allocation.tag()) { 169 | Some(total) => *total -= allocation.size(), 170 | None => (), 171 | } 172 | Ok(()) 173 | } 174 | } 175 | 176 | impl TaggedAllocator for TaggedMallocator { 177 | fn total_allocated(&self) -> usize { 178 | self.total_allocated.get() 179 | } 180 | 181 | fn visit(&self, mut closure: F) 182 | where 183 | F: FnMut(&HashMap), 184 | { 185 | let rg = self.allocs_by_tag.borrow(); 186 | closure(&*rg); 187 | } 188 | 189 | fn clone_map(&self) -> HashMap { 190 | self.allocs_by_tag.borrow().clone() 191 | } 192 | } 193 | 194 | #[cfg(test)] 195 | mod test { 196 | #[test] 197 | fn test_bitfield() { 198 | use super::TaggedSize; 199 | let mut sz = TaggedSize(0); 200 | sz.set_size(578); 201 | sz.set_tag(1017); 202 | 203 | assert_eq!(sz.get_size(), 578); 204 | assert_eq!(sz.get_tag(), 1017); 205 | } 206 | } 207 | -------------------------------------------------------------------------------- /optix/src/cuda/array.rs: -------------------------------------------------------------------------------- 1 | use optix_sys::cuda_sys as sys; 2 | 3 | use super::error::Error; 4 | type Result = std::result::Result; 5 | 6 | #[derive(Debug)] 7 | pub struct Array { 8 | ptr: sys::cudaArray_t, 9 | } 10 | 11 | impl Array { 12 | pub fn new( 13 | data: &[T], 14 | desc: ChannelFormatDesc, 15 | width: usize, 16 | height: usize, 17 | num_components: usize, 18 | flags: ArrayFlags, 19 | ) -> Result { 20 | let mut ptr = std::ptr::null_mut(); 21 | unsafe { 22 | let res = sys::cudaMallocArray( 23 | &mut ptr, 24 | &desc as *const ChannelFormatDesc 25 | as *const sys::cudaChannelFormatDesc, 26 | width, 27 | height, 28 | flags.bits(), 29 | ); 30 | if res != sys::cudaError::cudaSuccess { 31 | return Err(Error::ArrayAllocationFailed { 32 | source: res.into(), 33 | desc, 34 | width, 35 | height, 36 | num_components, 37 | flags, 38 | }); 39 | } 40 | 41 | let pitch = width * num_components * std::mem::size_of::(); 42 | let res = sys::cudaMemcpy2DToArray( 43 | ptr, 44 | 0, 45 | 0, 46 | data.as_ptr() as *const std::os::raw::c_void, 47 | pitch, 48 | pitch, 49 | height, 50 | super::MemcpyKind::HostToDevice as u32, 51 | ); 52 | if res != sys::cudaError::cudaSuccess { 53 | return Err(Error::ArrayMemcpy2DFailed { source: res.into() }); 54 | } 55 | 56 | Ok(Array { ptr }) 57 | } 58 | } 59 | 60 | pub fn as_device_ptr(&self) -> sys::cudaArray_t { 61 | self.ptr 62 | } 63 | } 64 | 65 | impl Drop for Array { 66 | fn drop(&mut self) { 67 | unsafe { 68 | let res = sys::cudaFreeArray(self.ptr); 69 | if res != sys::cudaError::cudaSuccess { 70 | panic!("cudaFreeArray failed: {:?}", res); 71 | } 72 | } 73 | } 74 | } 75 | 76 | bitflags::bitflags! { 77 | pub struct ArrayFlags: u32 { 78 | const DEFAULT = 0x00; 79 | const LAYERED = 0x01; 80 | const SURFACE_LOAD_STORE = 0x02; 81 | const CUBEMAP = 0x04; 82 | const TEXTURE_GATHER = 0x08; 83 | const COLOR_ATTACHMENT = 0x20; 84 | } 85 | } 86 | 87 | #[repr(C)] 88 | #[derive(Debug)] 89 | pub struct ChannelFormatDesc { 90 | pub x: i32, 91 | pub y: i32, 92 | pub z: i32, 93 | pub w: i32, 94 | pub f: ChannelFormatKind, 95 | } 96 | 97 | #[repr(u32)] 98 | #[derive(Copy, Clone, Debug, PartialEq)] 99 | pub enum ChannelFormatKind { 100 | Signed = sys::cudaChannelFormatKind_cudaChannelFormatKindSigned, 101 | Unsigned = sys::cudaChannelFormatKind_cudaChannelFormatKindUnsigned, 102 | Float = sys::cudaChannelFormatKind_cudaChannelFormatKindFloat, 103 | None = sys::cudaChannelFormatKind_cudaChannelFormatKindNone, 104 | } 105 | -------------------------------------------------------------------------------- /optix/src/cuda/buffer.rs: -------------------------------------------------------------------------------- 1 | use super::allocator::{Allocation, Allocator, Mallocator}; 2 | use optix_sys::cuda_sys::{cudaError, cudaMemcpy, cudaMemcpyKind, CUdeviceptr}; 3 | 4 | use std::os::raw::c_void; 5 | 6 | use super::error::Error; 7 | type Result = std::result::Result; 8 | 9 | #[repr(u32)] 10 | #[derive(Debug, Copy, Clone, thiserror::Error)] 11 | pub enum MemcpyKind { 12 | #[error("Host to Host")] 13 | HostToHost = cudaMemcpyKind::cudaMemcpyHostToHost, 14 | #[error("Host to Device")] 15 | HostToDevice = cudaMemcpyKind::cudaMemcpyHostToDevice, 16 | #[error("Device to Host")] 17 | DeviceToHost = cudaMemcpyKind::cudaMemcpyDeviceToHost, 18 | #[error("Device to Device")] 19 | DeviceToDevice = cudaMemcpyKind::cudaMemcpyDeviceToDevice, 20 | #[error("Default")] 21 | Default = cudaMemcpyKind::cudaMemcpyDefault, 22 | } 23 | 24 | impl From for MemcpyKind { 25 | fn from(k: cudaMemcpyKind::Type) -> MemcpyKind { 26 | match k { 27 | cudaMemcpyKind::cudaMemcpyHostToHost => MemcpyKind::HostToHost, 28 | cudaMemcpyKind::cudaMemcpyHostToDevice => MemcpyKind::HostToDevice, 29 | cudaMemcpyKind::cudaMemcpyDeviceToHost => MemcpyKind::DeviceToHost, 30 | cudaMemcpyKind::cudaMemcpyDeviceToDevice => { 31 | MemcpyKind::DeviceToDevice 32 | } 33 | cudaMemcpyKind::cudaMemcpyDefault => MemcpyKind::Default, 34 | _ => unreachable!(), 35 | } 36 | } 37 | } 38 | 39 | impl From for cudaMemcpyKind::Type { 40 | fn from(k: MemcpyKind) -> cudaMemcpyKind::Type { 41 | match k { 42 | MemcpyKind::HostToHost => cudaMemcpyKind::cudaMemcpyHostToHost, 43 | MemcpyKind::HostToDevice => cudaMemcpyKind::cudaMemcpyHostToDevice, 44 | MemcpyKind::DeviceToHost => cudaMemcpyKind::cudaMemcpyDeviceToHost, 45 | MemcpyKind::DeviceToDevice => { 46 | cudaMemcpyKind::cudaMemcpyDeviceToDevice 47 | } 48 | MemcpyKind::Default => cudaMemcpyKind::cudaMemcpyDefault, 49 | } 50 | } 51 | } 52 | 53 | pub struct Buffer<'a, AllocT = Mallocator> 54 | where 55 | AllocT: Allocator, 56 | { 57 | allocation: Allocation, 58 | _alloc: &'a AllocT, 59 | } 60 | 61 | impl<'a, AllocT> Buffer<'a, AllocT> 62 | where 63 | AllocT: Allocator, 64 | { 65 | pub fn new( 66 | size_in_bytes: usize, 67 | alignment: usize, 68 | tag: u64, 69 | allocator: &'a AllocT, 70 | ) -> Result> { 71 | let allocation = 72 | unsafe { allocator.alloc(size_in_bytes, alignment, tag)? }; 73 | Ok(Buffer { 74 | allocation, 75 | _alloc: allocator, 76 | }) 77 | } 78 | 79 | pub fn with_data( 80 | data: &[T], 81 | alignment: usize, 82 | tag: u64, 83 | allocator: &'a AllocT, 84 | ) -> Result> 85 | where 86 | T: Sized, 87 | { 88 | let size_in_bytes = std::mem::size_of::() * data.len(); 89 | 90 | if size_in_bytes != 0 { 91 | let allocation = 92 | unsafe { allocator.alloc(size_in_bytes, alignment, tag)? }; 93 | 94 | let res = unsafe { 95 | cudaMemcpy( 96 | allocation.ptr() as *mut c_void, 97 | data.as_ptr() as *const c_void, 98 | size_in_bytes, 99 | cudaMemcpyKind::cudaMemcpyHostToDevice, 100 | ) 101 | }; 102 | if res != cudaError::cudaSuccess { 103 | return Err(Error::BufferUploadFailed { source: res.into() }); 104 | } 105 | Ok(Buffer { 106 | allocation, 107 | _alloc: allocator, 108 | }) 109 | } else { 110 | // TODO: we rely on being able to create zero-sized allocations in 111 | // rama, to simplify passing optional stuff to cuda that we can 112 | // represent as a null ptr. Is this the right wat to do this? Or 113 | // should we ban zero-length allocations and have rama use Option 114 | // etc to represent those situations at the expense of complexity? 115 | // Err(Error::ZeroAllocation) 116 | Ok(Buffer { 117 | allocation: Allocation::new(0, 0, tag), 118 | _alloc: allocator, 119 | }) 120 | } 121 | } 122 | 123 | pub fn upload(&mut self, data: &[T]) -> Result<()> { 124 | let sz = data.len() * std::mem::size_of::(); 125 | if sz != self.allocation.size() { 126 | return Err(Error::BufferUploadWrongSize { 127 | upload_size: sz, 128 | buffer_size: self.allocation.size(), 129 | }); 130 | } 131 | unsafe { 132 | let res = cudaMemcpy( 133 | self.allocation.ptr() as *mut c_void, 134 | data.as_ptr() as *const c_void, 135 | self.allocation.size(), 136 | cudaMemcpyKind::cudaMemcpyHostToDevice, 137 | ); 138 | if res != cudaError::cudaSuccess { 139 | return Err(Error::BufferUploadFailed { source: res.into() }); 140 | } 141 | } 142 | 143 | Ok(()) 144 | } 145 | 146 | pub unsafe fn upload_ptr( 147 | &mut self, 148 | data: *const c_void, 149 | size: usize, 150 | ) -> Result<()> { 151 | let res = cudaMemcpy( 152 | self.allocation.ptr() as *mut c_void, 153 | data as *const c_void, 154 | size, 155 | cudaMemcpyKind::cudaMemcpyHostToDevice, 156 | ); 157 | if res != cudaError::cudaSuccess { 158 | return Err(Error::BufferUploadFailed { source: res.into() }); 159 | } 160 | 161 | Ok(()) 162 | } 163 | 164 | pub fn download(&self, data: &mut [T]) -> Result<()> { 165 | let sz = data.len() * std::mem::size_of::(); 166 | if sz != self.allocation.size() { 167 | return Err(Error::BufferDownloadWrongSize { 168 | download_size: sz, 169 | buffer_size: self.allocation.size(), 170 | }); 171 | } 172 | unsafe { 173 | let res = cudaMemcpy( 174 | data.as_mut_ptr() as *mut c_void, 175 | self.allocation.ptr() as *mut c_void, 176 | self.allocation.size(), 177 | cudaMemcpyKind::cudaMemcpyDeviceToHost, 178 | ); 179 | if res != cudaError::cudaSuccess { 180 | return Err(Error::BufferDownloadFailed { source: res.into() }); 181 | } 182 | } 183 | 184 | Ok(()) 185 | } 186 | 187 | pub fn download_primitive(&self) -> Result 188 | where 189 | T: Default, 190 | { 191 | let sz = std::mem::size_of::(); 192 | if sz != self.allocation.size() { 193 | return Err(Error::BufferDownloadWrongSize { 194 | download_size: sz, 195 | buffer_size: self.allocation.size(), 196 | }); 197 | } 198 | 199 | let mut data = T::default(); 200 | unsafe { 201 | let res = cudaMemcpy( 202 | &mut data as *mut T as *mut c_void, 203 | self.allocation.ptr() as *mut c_void, 204 | self.allocation.size(), 205 | cudaMemcpyKind::cudaMemcpyDeviceToHost, 206 | ); 207 | if res != cudaError::cudaSuccess { 208 | return Err(Error::BufferDownloadFailed { source: res.into() }); 209 | } 210 | } 211 | 212 | Ok(data) 213 | } 214 | 215 | pub fn as_ptr(&self) -> *const c_void { 216 | self.allocation.ptr() as *const c_void 217 | } 218 | 219 | pub fn as_device_ptr(&self) -> CUdeviceptr { 220 | self.allocation.ptr() as CUdeviceptr 221 | } 222 | 223 | pub fn as_mut_ptr(&mut self) -> *mut c_void { 224 | self.allocation.ptr() as *mut c_void 225 | } 226 | 227 | pub fn byte_size(&self) -> usize { 228 | self.allocation.size() 229 | } 230 | } 231 | 232 | impl<'a, AllocT> Drop for Buffer<'a, AllocT> 233 | where 234 | AllocT: Allocator, 235 | { 236 | fn drop(&mut self) { 237 | unsafe { 238 | let mut a = Allocation::new(0, 0, 0); 239 | std::mem::swap(&mut self.allocation, &mut a); 240 | self._alloc.dealloc(a).expect("dealloc failed") 241 | } 242 | } 243 | } 244 | -------------------------------------------------------------------------------- /optix/src/cuda/context.rs: -------------------------------------------------------------------------------- 1 | use optix_sys::cuda_sys as sys; 2 | 3 | use super::error::Error; 4 | type Result = std::result::Result; 5 | 6 | #[derive(Copy, Clone)] 7 | pub struct ContextRef { 8 | ctx: sys::CUcontext, 9 | } 10 | 11 | impl ContextRef { 12 | pub fn ctx(&self) -> sys::CUcontext { 13 | self.ctx 14 | } 15 | } 16 | 17 | impl std::ops::Deref for ContextRef { 18 | type Target = sys::CUcontext; 19 | fn deref(&self) -> &sys::CUcontext { 20 | &self.ctx 21 | } 22 | } 23 | 24 | pub struct Context {} 25 | 26 | impl Context { 27 | pub fn get_current() -> Result { 28 | unsafe { 29 | let mut ctx = std::ptr::null_mut(); 30 | let res = sys::cuCtxGetCurrent(&mut ctx); 31 | if res != sys::cudaError::cudaSuccess { 32 | return Err(Error::CouldNotGetCurrentContext { 33 | source: res.into(), 34 | }); 35 | } 36 | Ok(ContextRef { ctx }) 37 | } 38 | } 39 | } 40 | -------------------------------------------------------------------------------- /optix/src/cuda/error.rs: -------------------------------------------------------------------------------- 1 | use optix_sys::cuda_sys::Error as CudaError; 2 | 3 | #[derive(Debug, thiserror::Error)] 4 | pub enum Error { 5 | #[error("allocation of size {size:} bytes failed.")] 6 | AllocationFailed { source: CudaError, size: usize }, 7 | #[error("allocation of size {size:} bytes failed as could not satisfy alignment of {align:} bytes.")] 8 | AllocationAlignment { size: usize, align: usize }, 9 | #[error("Tried to allocate zero bytes")] 10 | ZeroAllocation, 11 | #[error("Buffer allocation of size {size:} bytes failed.")] 12 | BufferAllocationFailed { source: CudaError, size: usize }, 13 | #[error( 14 | "Buffer allocation of {desc:?}, {width:}x{height:} flags: {flags:?}" 15 | )] 16 | ArrayAllocationFailed { 17 | source: CudaError, 18 | desc: super::ChannelFormatDesc, 19 | width: usize, 20 | height: usize, 21 | num_components: usize, 22 | flags: super::ArrayFlags, 23 | }, 24 | #[error("Array memcpy 2d failed.")] 25 | ArrayMemcpy2DFailed { source: CudaError }, 26 | #[error("Buffer upload failed.")] 27 | BufferUploadFailed { source: CudaError }, 28 | #[error("Buffer download failed.")] 29 | BufferDownloadFailed { source: CudaError }, 30 | #[error( 31 | "Tried to upload {upload_size:} bytes of data to a buffer of {buffer_size:} bytes" 32 | )] 33 | BufferUploadWrongSize { 34 | upload_size: usize, 35 | buffer_size: usize, 36 | }, 37 | #[error( 38 | "Tried to download {download_size:} bytes of data from a buffer of {buffer_size:} bytes" 39 | )] 40 | BufferDownloadWrongSize { 41 | download_size: usize, 42 | buffer_size: usize, 43 | }, 44 | #[error("Could not set device {device:}")] 45 | CouldNotSetDevice { source: CudaError, device: i32 }, 46 | #[error("Failed to create stream")] 47 | StreamCreationFailed { source: CudaError }, 48 | #[error("Could not get device {device:} properties")] 49 | CouldNotGetDeviceProperties { source: CudaError, device: i32 }, 50 | #[error("Could not get current context")] 51 | CouldNotGetCurrentContext { source: CudaError }, 52 | #[error("Device sync failed")] 53 | DeviceSyncFailed { source: CudaError }, 54 | #[error("Texture object creation failed")] 55 | TextureObjectCreationFailed { source: CudaError }, 56 | #[error("Could not get mem info")] 57 | GetMemInfoFailed { source: CudaError }, 58 | } 59 | -------------------------------------------------------------------------------- /optix/src/cuda/mod.rs: -------------------------------------------------------------------------------- 1 | pub mod context; 2 | pub use context::{Context, ContextRef}; 3 | pub mod buffer; 4 | pub use buffer::{Buffer, MemcpyKind}; 5 | pub mod error; 6 | pub mod nvrtc; 7 | pub mod stream; 8 | pub use stream::Stream; 9 | pub mod texture_object; 10 | pub use texture_object::{ 11 | ResourceDesc, TextureAddressMode, TextureDesc, TextureDescBuilder, 12 | TextureFilterMode, TextureObject, TextureReadMode, 13 | }; 14 | pub mod array; 15 | pub use array::{Array, ArrayFlags, ChannelFormatDesc, ChannelFormatKind}; 16 | pub mod allocator; 17 | pub use allocator::{Allocator, Mallocator, TaggedAllocator, TaggedMallocator}; 18 | 19 | pub use error::Error; 20 | type Result = std::result::Result; 21 | 22 | use optix_sys::cuda_sys as sys; 23 | 24 | use std::ffi::CStr; 25 | 26 | pub use sys::{cudaTextureObject_t, CUdeviceptr}; 27 | 28 | pub fn init() { 29 | unsafe { 30 | sys::cudaFree(std::ptr::null_mut()); 31 | } 32 | } 33 | 34 | pub fn get_device_count() -> i32 { 35 | let mut count = 0i32; 36 | unsafe { 37 | sys::cudaGetDeviceCount(&mut count as *mut i32); 38 | } 39 | 40 | count 41 | } 42 | 43 | pub fn set_device(device: i32) -> Result<()> { 44 | unsafe { 45 | let res = sys::cudaSetDevice(device); 46 | if res != sys::cudaError_enum::CUDA_SUCCESS as u32 { 47 | return Err(Error::CouldNotSetDevice { 48 | source: res.into(), 49 | device, 50 | }); 51 | } 52 | } 53 | 54 | Ok(()) 55 | } 56 | 57 | pub fn get_device_properties(device: i32) -> Result { 58 | unsafe { 59 | let mut prop = std::mem::MaybeUninit::::uninit(); 60 | let res = sys::cudaGetDeviceProperties(prop.as_mut_ptr(), device); 61 | if res != sys::cudaError_enum::CUDA_SUCCESS { 62 | return Err(Error::CouldNotGetDeviceProperties { 63 | source: res.into(), 64 | device, 65 | }); 66 | } 67 | let prop = prop.assume_init(); 68 | Ok(DeviceProp { prop }) 69 | } 70 | } 71 | 72 | pub fn device_synchronize() -> Result<()> { 73 | let res = unsafe { 74 | sys::cudaDeviceSynchronize(); 75 | sys::cudaGetLastError() 76 | }; 77 | if res != sys::cudaError_enum::CUDA_SUCCESS { 78 | Err(Error::DeviceSyncFailed { source: res.into() }) 79 | } else { 80 | Ok(()) 81 | } 82 | } 83 | 84 | pub fn mem_get_info() -> Result<(usize, usize)> { 85 | let (res, free, total) = unsafe { 86 | let mut free = 0usize; 87 | let mut total = 0usize; 88 | ( 89 | sys::cudaMemGetInfo( 90 | &mut free as *mut usize, 91 | &mut total as *mut usize, 92 | ), 93 | free, 94 | total, 95 | ) 96 | }; 97 | if res != sys::cudaError_enum::CUDA_SUCCESS { 98 | Err(Error::GetMemInfoFailed { source: res.into() }) 99 | } else { 100 | Ok((free, total)) 101 | } 102 | } 103 | 104 | pub struct DeviceProp { 105 | prop: sys::cudaDeviceProp, 106 | } 107 | 108 | impl DeviceProp { 109 | pub fn name(&self) -> String { 110 | unsafe { 111 | CStr::from_ptr(self.prop.name.as_ptr() as *const i8) 112 | .to_string_lossy() 113 | .into_owned() 114 | } 115 | } 116 | 117 | pub fn total_global_mem(&self) -> usize { 118 | self.prop.totalGlobalMem 119 | } 120 | 121 | pub fn shared_mem_per_block(&self) -> usize { 122 | self.prop.sharedMemPerBlock 123 | } 124 | 125 | pub fn registers_per_block(&self) -> i32 { 126 | self.prop.regsPerBlock 127 | } 128 | 129 | pub fn warp_size(&self) -> i32 { 130 | self.prop.warpSize 131 | } 132 | 133 | pub fn mem_pitch(&self) -> usize { 134 | self.prop.memPitch 135 | } 136 | 137 | pub fn max_threads_per_block(&self) -> i32 { 138 | self.prop.maxThreadsPerBlock 139 | } 140 | 141 | pub fn max_threads_dim(&self) -> &[i32; 3] { 142 | &self.prop.maxThreadsDim 143 | } 144 | 145 | pub fn max_grid_size(&self) -> &[i32; 3] { 146 | &self.prop.maxGridSize 147 | } 148 | 149 | pub fn clock_rate(&self) -> i32 { 150 | self.prop.clockRate 151 | } 152 | 153 | pub fn total_const_mem(&self) -> usize { 154 | self.prop.totalConstMem 155 | } 156 | 157 | pub fn major(&self) -> i32 { 158 | self.prop.major 159 | } 160 | 161 | pub fn minor(&self) -> i32 { 162 | self.prop.minor 163 | } 164 | 165 | pub fn texture_alignment(&self) -> usize { 166 | self.prop.textureAlignment 167 | } 168 | 169 | pub fn texture_pitch_alignment(&self) -> usize { 170 | self.prop.texturePitchAlignment 171 | } 172 | 173 | pub fn multi_processor_count(&self) -> i32 { 174 | self.prop.multiProcessorCount 175 | } 176 | 177 | pub fn kernel_exec_timeout_enabled(&self) -> bool { 178 | self.prop.kernelExecTimeoutEnabled != 0 179 | } 180 | 181 | pub fn integrated(&self) -> bool { 182 | self.prop.integrated != 0 183 | } 184 | 185 | pub fn can_map_host_memory(&self) -> bool { 186 | return self.prop.canMapHostMemory != 0; 187 | } 188 | 189 | pub fn compute_mode(&self) -> ComputeMode { 190 | match self.prop.computeMode as u32 { 191 | sys::cudaComputeMode_cudaComputeModeDefault => ComputeMode::Default, 192 | sys::cudaComputeMode_cudaComputeModeExclusive => { 193 | ComputeMode::Exclusive 194 | } 195 | sys::cudaComputeMode_cudaComputeModeProhibited => { 196 | ComputeMode::Prohibited 197 | } 198 | sys::cudaComputeMode_cudaComputeModeExclusiveProcess => { 199 | ComputeMode::ExclusiveProcess 200 | } 201 | _ => unreachable!(), 202 | } 203 | } 204 | 205 | pub fn max_texture_1d(&self) -> i32 { 206 | self.prop.maxTexture1D 207 | } 208 | 209 | pub fn max_texture_1d_mipmap(&self) -> i32 { 210 | self.prop.maxTexture1DMipmap 211 | } 212 | 213 | pub fn max_texture_1d_linear(&self) -> i32 { 214 | self.prop.maxTexture1DLinear 215 | } 216 | 217 | pub fn max_texture_2d(&self) -> &[i32; 2] { 218 | &self.prop.maxTexture2D 219 | } 220 | 221 | pub fn max_texture_2d_mipmap(&self) -> &[i32; 2] { 222 | &self.prop.maxTexture2DMipmap 223 | } 224 | 225 | pub fn max_texture_2d_linear(&self) -> &[i32; 3] { 226 | &self.prop.maxTexture2DLinear 227 | } 228 | 229 | pub fn max_texture_2d_gather(&self) -> &[i32; 2] { 230 | &self.prop.maxTexture2DGather 231 | } 232 | 233 | pub fn max_texture_3d(&self) -> &[i32; 3] { 234 | &self.prop.maxTexture3D 235 | } 236 | 237 | pub fn max_texture_3d_alt(&self) -> &[i32; 3] { 238 | &self.prop.maxTexture3DAlt 239 | } 240 | 241 | pub fn max_texture_cubemap(&self) -> i32 { 242 | self.prop.maxTextureCubemap 243 | } 244 | 245 | pub fn max_texture_1d_layered(&self) -> &[i32; 2] { 246 | &self.prop.maxTexture1DLayered 247 | } 248 | 249 | pub fn max_texture_2d_layered(&self) -> &[i32; 3] { 250 | &self.prop.maxTexture2DLayered 251 | } 252 | 253 | pub fn max_texture_cubemap_layered(&self) -> &[i32; 2] { 254 | &self.prop.maxTextureCubemapLayered 255 | } 256 | 257 | pub fn max_surface_1d(&self) -> i32 { 258 | self.prop.maxSurface1D 259 | } 260 | 261 | pub fn max_surface_2d(&self) -> &[i32; 2] { 262 | &self.prop.maxSurface2D 263 | } 264 | 265 | pub fn max_surface_3d(&self) -> &[i32; 3] { 266 | &self.prop.maxSurface3D 267 | } 268 | 269 | pub fn max_surface_1d_layered(&self) -> &[i32; 2] { 270 | &self.prop.maxSurface1DLayered 271 | } 272 | 273 | pub fn max_surface_2d_layered(&self) -> &[i32; 3] { 274 | &self.prop.maxSurface2DLayered 275 | } 276 | 277 | pub fn max_surface_cubemap(&self) -> i32 { 278 | self.prop.maxSurfaceCubemap 279 | } 280 | 281 | pub fn max_surface_cubemap_layered(&self) -> &[i32; 2] { 282 | &self.prop.maxSurfaceCubemapLayered 283 | } 284 | 285 | pub fn surface_alignment(&self) -> usize { 286 | self.prop.surfaceAlignment 287 | } 288 | } 289 | 290 | #[derive(Copy, Clone, Debug, thiserror::Error)] 291 | pub enum ComputeMode { 292 | #[error("< Default compute mode (Multiple threads can use cuda::set_device() with this device)")] 293 | Default, 294 | #[error("< Compute-exclusive-thread mode (Only one thread in one process will be able to use cuda::set_device() with this device)")] 295 | Exclusive, 296 | #[error("< Compute-prohibited mode (No threads can use cuda::set_device() with this device)")] 297 | Prohibited, 298 | #[error("< Compute-exclusive-process mode (Many threads in one process will be able to use cuda::set_device() with this device)")] 299 | ExclusiveProcess, 300 | } 301 | 302 | #[cfg(test)] 303 | mod tests { 304 | #[test] 305 | fn it_works() { 306 | assert_eq!(2 + 2, 4); 307 | } 308 | } 309 | -------------------------------------------------------------------------------- /optix/src/cuda/nvrtc.rs: -------------------------------------------------------------------------------- 1 | use optix_sys::cuda_sys::{ 2 | nvrtcCompileProgram, nvrtcCreateProgram, nvrtcDestroyProgram, 3 | nvrtcGetErrorString, nvrtcGetPTX, nvrtcGetPTXSize, nvrtcGetProgramLog, 4 | nvrtcGetProgramLogSize, nvrtcProgram, nvrtcResult, 5 | }; 6 | use std::ffi::CString; 7 | use std::os::raw::c_char; 8 | 9 | use std::fmt; 10 | 11 | #[derive(Debug)] 12 | pub struct Error { 13 | error_string: String, 14 | } 15 | 16 | impl fmt::Display for Error { 17 | fn fmt(&self, output: &mut fmt::Formatter) -> fmt::Result { 18 | write!(output, "nvrtc compilation error: {}", self.error_string) 19 | } 20 | } 21 | 22 | impl std::error::Error for Error { 23 | fn source(&self) -> Option<&(dyn std::error::Error + 'static)> { 24 | None 25 | } 26 | } 27 | 28 | pub fn get_error_string(result: nvrtcResult::Type) -> Error { 29 | unsafe { 30 | Error { 31 | error_string: std::ffi::CStr::from_ptr(nvrtcGetErrorString(result)) 32 | .to_string_lossy() 33 | .into_owned(), 34 | } 35 | } 36 | } 37 | 38 | /// A CUDA program object that can be compiled to generate PTX 39 | pub struct Program { 40 | pub prog: nvrtcProgram, 41 | } 42 | 43 | /// Represents a header file that can be included by a program. The `name` is 44 | /// the name by which the header will be referenced in the CUDA source. The 45 | /// `contents` is just the CUDA contents of the header. 46 | #[derive(Clone)] 47 | pub struct Header { 48 | pub name: String, 49 | pub contents: String, 50 | } 51 | 52 | impl fmt::Debug for Header { 53 | fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { 54 | write!(f, "// HEADER: {}", self.name)?; 55 | write!(f, "{}", self.contents) 56 | } 57 | } 58 | 59 | pub type Result = std::result::Result; 60 | 61 | impl Program { 62 | /// Create a new `Program` with the given `src`, using the entry point 63 | /// `name` and with a list of `headers` to include. If there are no headers 64 | /// to include, just pass an empty `Vec`. 65 | pub fn new(src: &str, name: &str, headers: &[Header]) -> Result { 66 | let src = CString::new(src).unwrap(); 67 | let name = CString::new(name).unwrap(); 68 | let mut header_names = Vec::new(); 69 | let mut header_contents = Vec::new(); 70 | for h in headers { 71 | header_names.push(CString::new(h.name.clone()).unwrap()); 72 | header_contents.push(CString::new(h.contents.clone()).unwrap()); 73 | } 74 | let mut header_names_arr = Vec::new(); 75 | let mut header_contents_arr = Vec::new(); 76 | for i in 0..header_names.len() { 77 | header_names_arr.push(header_names[i].as_ptr() as *const c_char); 78 | header_contents_arr 79 | .push(header_contents[i].as_ptr() as *const c_char); 80 | } 81 | 82 | let (prog, result) = unsafe { 83 | let mut prog: nvrtcProgram = std::ptr::null_mut(); 84 | let result = nvrtcCreateProgram( 85 | &mut prog, 86 | src.as_ptr() as *const c_char, 87 | name.as_ptr() as *const c_char, 88 | header_names.len() as i32, 89 | header_contents_arr.as_ptr() as *const *const c_char, 90 | header_names_arr.as_ptr() as *const *const c_char, 91 | ); 92 | (prog, result) 93 | }; 94 | 95 | if result != nvrtcResult::NVRTC_SUCCESS { 96 | Err(get_error_string(result)) 97 | } else { 98 | Ok(Program { prog }) 99 | } 100 | } 101 | 102 | /// Compile this program with the given `options` 103 | pub fn compile_program(&mut self, options: &Vec) -> Result<()> { 104 | let mut coptions = Vec::new(); 105 | for o in options { 106 | let c = CString::new(o.clone()).unwrap(); 107 | coptions.push(c); 108 | } 109 | 110 | let mut options_arr = Vec::new(); 111 | for o in &coptions { 112 | options_arr.push(o.as_ptr() as *const c_char); 113 | } 114 | 115 | let result = unsafe { 116 | nvrtcCompileProgram( 117 | self.prog, 118 | options_arr.len() as i32, 119 | options_arr.as_ptr() as *const *const c_char, 120 | ) 121 | }; 122 | if result != nvrtcResult::NVRTC_SUCCESS { 123 | Err(Error { 124 | error_string: format!( 125 | "{}\n{}", 126 | get_error_string(result).error_string, 127 | self.get_program_log().unwrap() 128 | ), 129 | }) 130 | } else { 131 | Ok(()) 132 | } 133 | } 134 | 135 | /// Get the program compilation log 136 | pub fn get_program_log(&self) -> Result { 137 | let (log_size, result) = unsafe { 138 | let mut log_size: usize = 0; 139 | let result = nvrtcGetProgramLogSize(self.prog, &mut log_size); 140 | (log_size, result) 141 | }; 142 | 143 | if result != nvrtcResult::NVRTC_SUCCESS { 144 | return Err(get_error_string(result)); 145 | } 146 | 147 | let buffer = create_whitespace_cstring(log_size); 148 | 149 | let result = unsafe { 150 | nvrtcGetProgramLog(self.prog, buffer.as_ptr() as *mut c_char) 151 | }; 152 | 153 | if result != nvrtcResult::NVRTC_SUCCESS { 154 | Err(get_error_string(result)) 155 | } else { 156 | Ok(buffer.to_string_lossy().into_owned()) 157 | } 158 | } 159 | 160 | /// Assuming a successful compilation, get the generated PTX as a `String` 161 | pub fn get_ptx(&self) -> Result { 162 | let (ptx_size, result) = unsafe { 163 | let mut ptx_size: usize = 0; 164 | let result = nvrtcGetPTXSize(self.prog, &mut ptx_size); 165 | (ptx_size, result) 166 | }; 167 | 168 | if result != nvrtcResult::NVRTC_SUCCESS { 169 | return Err(get_error_string(result)); 170 | } 171 | 172 | let buffer = create_whitespace_cstring(ptx_size); 173 | 174 | let result = 175 | unsafe { nvrtcGetPTX(self.prog, buffer.as_ptr() as *mut c_char) }; 176 | 177 | if result != nvrtcResult::NVRTC_SUCCESS { 178 | Err(get_error_string(result)) 179 | } else { 180 | Ok(buffer.to_string_lossy().into_owned()) 181 | } 182 | } 183 | } 184 | 185 | impl Drop for Program { 186 | fn drop(&mut self) { 187 | unsafe { 188 | nvrtcDestroyProgram(&mut self.prog); 189 | } 190 | } 191 | } 192 | 193 | fn create_whitespace_cstring(len: usize) -> CString { 194 | let mut buffer: Vec = Vec::with_capacity(len as usize); 195 | buffer.extend([b' '].iter().cycle().take(len as usize - 1)); 196 | unsafe { CString::from_vec_unchecked(buffer) } 197 | } 198 | 199 | #[test] 200 | fn test_compile() { 201 | let optix_root = std::env::var("OPTIX_ROOT") 202 | .expect("Could not get OPTIX_ROOT from environment"); 203 | let cuda_root = std::env::var("CUDA_ROOT") 204 | .expect("Could not get CUDA_ROOT from environment"); 205 | 206 | // Create a vector of options to pass to the compiler 207 | let optix_inc = format!("-I{}/include", optix_root); 208 | let cuda_inc = format!("-I{}/include", cuda_root); 209 | 210 | let options = vec![ 211 | optix_inc, 212 | cuda_inc, 213 | "-arch=compute_30".to_owned(), 214 | "-rdc=true".to_owned(), 215 | "-std=c++14".to_owned(), 216 | "-D__x86_64".to_owned(), 217 | "--device-as-default-execution-space".to_owned(), 218 | ]; 219 | 220 | // The program object allows us to compile the cuda source and get ptx from 221 | // it if successful. 222 | let mut prg = Program::new( 223 | "#include \n__device__ float myfun() { return 1.0f; }", 224 | "myfun", 225 | &Vec::new(), 226 | ) 227 | .unwrap(); 228 | 229 | match prg.compile_program(&options) { 230 | Err(code) => { 231 | panic!("{}: {}", code, prg.get_program_log().unwrap()); 232 | } 233 | Ok(_) => { 234 | println!("Compilation successful"); 235 | println!("{}", prg.get_ptx().unwrap()); 236 | } 237 | } 238 | } 239 | -------------------------------------------------------------------------------- /optix/src/cuda/stream.rs: -------------------------------------------------------------------------------- 1 | use optix_sys::cuda_sys as sys; 2 | 3 | use super::error::Error; 4 | type Result = std::result::Result; 5 | 6 | pub struct Stream { 7 | s: sys::CUstream, 8 | } 9 | 10 | impl Stream { 11 | pub fn new() -> Result { 12 | let mut s: sys::CUstream = std::ptr::null_mut(); 13 | let res = unsafe { sys::cudaStreamCreate(&mut s) }; 14 | 15 | if res != sys::cudaError::cudaSuccess { 16 | return Err(Error::StreamCreationFailed { source: res.into() }); 17 | } 18 | 19 | Ok(Stream { s }) 20 | } 21 | 22 | pub fn as_sys_ptr(&self) -> sys::CUstream { 23 | self.s 24 | } 25 | } 26 | 27 | impl Default for Stream { 28 | fn default() -> Stream { 29 | Stream { 30 | s: std::ptr::null_mut(), 31 | } 32 | } 33 | } 34 | 35 | impl Drop for Stream { 36 | fn drop(&mut self) { 37 | unsafe { 38 | if !self.s.is_null() { 39 | sys::cudaStreamDestroy(self.s); 40 | } 41 | } 42 | } 43 | } 44 | -------------------------------------------------------------------------------- /optix/src/cuda/texture_object.rs: -------------------------------------------------------------------------------- 1 | use optix_sys::cuda_sys as sys; 2 | 3 | use super::error::Error; 4 | type Result = std::result::Result; 5 | 6 | #[repr(C)] 7 | pub struct TextureDesc { 8 | /// Texture address mode for up to 3 dimensions 9 | pub address_mode: [TextureAddressMode; 3], 10 | /// Texture filter mode 11 | pub filter_mode: TextureFilterMode, 12 | /// Texture read mode 13 | pub read_mode: TextureReadMode, 14 | /// Perform sRGB->Linear conversion during texture read 15 | pub srgb: i32, 16 | /// Texture border color 17 | pub border_color: [f32; 4], 18 | /// Indicates whether texture reads are normalized or not 19 | pub normalized_coords: i32, 20 | /// Limit to the anisotropy ratio 21 | pub max_anisotropy: u32, 22 | /// Mipmap filter mode 23 | pub mipmap_filter_mode: TextureFilterMode, 24 | /// Offset applied to the supplied mipmap level 25 | pub mipmap_level_bias: f32, 26 | /// Lower end of the mipmap level range to clamp access to 27 | pub min_mipmap_level_clamp: f32, 28 | /// Upper end of the mipmap level range to clamp access to 29 | pub max_mipmap_level_clamp: f32, 30 | } 31 | 32 | impl TextureDesc { 33 | pub fn new() -> TextureDescBuilder { 34 | TextureDescBuilder { 35 | desc: TextureDesc::default(), 36 | } 37 | } 38 | } 39 | 40 | impl Default for TextureDesc { 41 | fn default() -> TextureDesc { 42 | TextureDesc { 43 | address_mode: [TextureAddressMode::Clamp; 3], 44 | filter_mode: TextureFilterMode::Point, 45 | read_mode: TextureReadMode::ElementType, 46 | srgb: 0, 47 | border_color: [0.0f32; 4], 48 | normalized_coords: 0, 49 | max_anisotropy: 1, 50 | mipmap_filter_mode: TextureFilterMode::Point, 51 | mipmap_level_bias: 0.0, 52 | min_mipmap_level_clamp: 0.0, 53 | max_mipmap_level_clamp: 99.0, 54 | } 55 | } 56 | } 57 | 58 | pub struct TextureDescBuilder { 59 | desc: TextureDesc, 60 | } 61 | 62 | impl TextureDescBuilder { 63 | pub fn address_mode(mut self, mode: [TextureAddressMode; 3]) -> Self { 64 | self.desc.address_mode = mode; 65 | self 66 | } 67 | 68 | pub fn filter_mode(mut self, filter_mode: TextureFilterMode) -> Self { 69 | self.desc.filter_mode = filter_mode; 70 | self 71 | } 72 | 73 | pub fn read_mode(mut self, read_mode: TextureReadMode) -> Self { 74 | self.desc.read_mode = read_mode; 75 | self 76 | } 77 | 78 | pub fn srgb(mut self, srgb: bool) -> Self { 79 | self.desc.srgb = if srgb { 1 } else { 0 }; 80 | self 81 | } 82 | 83 | pub fn border_color(mut self, border_color: [f32; 4]) -> Self { 84 | self.desc.border_color = border_color; 85 | self 86 | } 87 | 88 | pub fn normalized_coords(mut self, normalized_coords: bool) -> Self { 89 | self.desc.normalized_coords = if normalized_coords { 1 } else { 0 }; 90 | self 91 | } 92 | 93 | pub fn max_anisotropy(mut self, max_anisotropy: u32) -> Self { 94 | self.desc.max_anisotropy = max_anisotropy; 95 | self 96 | } 97 | 98 | pub fn mipmap_filter_mode( 99 | mut self, 100 | mipmap_filter_mode: TextureFilterMode, 101 | ) -> Self { 102 | self.desc.mipmap_filter_mode = mipmap_filter_mode; 103 | self 104 | } 105 | 106 | pub fn mipmap_level_bias(mut self, mipmap_level_bias: f32) -> Self { 107 | self.desc.mipmap_level_bias = mipmap_level_bias; 108 | self 109 | } 110 | 111 | pub fn min_mipmap_level_clamp( 112 | mut self, 113 | min_mipmap_level_clamp: f32, 114 | ) -> Self { 115 | self.desc.min_mipmap_level_clamp = min_mipmap_level_clamp; 116 | self 117 | } 118 | 119 | pub fn max_mipmap_level_clamp( 120 | mut self, 121 | max_mipmap_level_clamp: f32, 122 | ) -> Self { 123 | self.desc.max_mipmap_level_clamp = max_mipmap_level_clamp; 124 | self 125 | } 126 | 127 | pub fn build(self) -> TextureDesc { 128 | self.desc 129 | } 130 | } 131 | 132 | #[repr(u32)] 133 | #[derive(Copy, Clone, Debug)] 134 | pub enum TextureAddressMode { 135 | Wrap = sys::cudaTextureAddressMode_cudaAddressModeWrap, 136 | Clamp = sys::cudaTextureAddressMode_cudaAddressModeClamp, 137 | Mirror = sys::cudaTextureAddressMode_cudaAddressModeMirror, 138 | Border = sys::cudaTextureAddressMode_cudaAddressModeBorder, 139 | } 140 | 141 | #[repr(u32)] 142 | #[derive(Copy, Clone, Debug)] 143 | pub enum TextureFilterMode { 144 | Point = sys::cudaTextureFilterMode_cudaFilterModePoint, 145 | Linear = sys::cudaTextureFilterMode_cudaFilterModeLinear, 146 | } 147 | 148 | #[repr(u32)] 149 | #[derive(Copy, Clone, Debug)] 150 | pub enum TextureReadMode { 151 | ElementType = sys::cudaTextureReadMode_cudaReadModeElementType, 152 | NormalizedFloat = sys::cudaTextureReadMode_cudaReadModeNormalizedFloat, 153 | } 154 | 155 | #[derive(Debug)] 156 | pub enum ResourceDesc { 157 | Array(super::array::Array), 158 | } 159 | 160 | #[derive(Debug)] 161 | pub struct TextureObject { 162 | ptr: sys::cudaTextureObject_t, 163 | _res_desc: ResourceDesc, 164 | } 165 | 166 | impl TextureObject { 167 | pub fn new( 168 | res_desc: ResourceDesc, 169 | tex_desc: &TextureDesc, 170 | ) -> Result { 171 | unsafe { 172 | let d_res_desc = match &res_desc { 173 | ResourceDesc::Array(array) => sys::cudaResourceDesc { 174 | resType: sys::cudaResourceType_cudaResourceTypeArray, 175 | res: sys::cudaResourceDescUnion { 176 | array: sys::cudaResourceDescUnionArray { 177 | array: array.as_device_ptr(), 178 | }, 179 | }, 180 | }, 181 | }; 182 | let mut ptr = 0; 183 | let res = sys::cudaCreateTextureObject( 184 | &mut ptr, 185 | &d_res_desc, 186 | tex_desc as *const TextureDesc as *const sys::cudaTextureDesc, 187 | std::ptr::null(), 188 | ); 189 | if res != sys::cudaError::cudaSuccess { 190 | return Err(Error::TextureObjectCreationFailed { 191 | source: res.into(), 192 | }); 193 | } 194 | 195 | Ok(TextureObject { 196 | ptr, 197 | _res_desc: res_desc, 198 | }) 199 | } 200 | } 201 | 202 | pub fn as_device_ptr(&self) -> sys::cudaTextureObject_t { 203 | self.ptr 204 | } 205 | } 206 | 207 | impl Drop for TextureObject { 208 | fn drop(&mut self) { 209 | unsafe { 210 | let res = sys::cudaDestroyTextureObject(self.ptr); 211 | if res != sys::cudaError::cudaSuccess { 212 | panic!( 213 | "cudaDestroyTextureObject {:p} failed: {:?}", 214 | self.ptr as *const u8, res 215 | ); 216 | } 217 | } 218 | } 219 | } 220 | -------------------------------------------------------------------------------- /optix/src/error.rs: -------------------------------------------------------------------------------- 1 | use super::cuda; 2 | use optix_sys as sys; 3 | 4 | use super::BufferFormat; 5 | 6 | #[derive(Debug, thiserror::Error)] 7 | pub enum Error { 8 | #[error("OptiX initialization failed")] 9 | InitializationFailed { source: sys::Error }, 10 | #[error("Failed to create OptiX device context")] 11 | DeviceContextCreateFailed { source: sys::Error }, 12 | #[error("A device context method failed")] 13 | DeviceContextMethodFailed { source: sys::Error }, 14 | #[error("Failed to set disk cache path '{}'", "path.display()")] 15 | SetCacheLocationFailed { 16 | source: sys::Error, 17 | path: std::path::PathBuf, 18 | }, 19 | #[error("Module creation failed:\n{log:}")] 20 | ModuleCreationFailed { source: sys::Error, log: String }, 21 | #[error("ProgramGroup creation failed:\n{log:}")] 22 | ProgramGroupCreationFailed { source: sys::Error, log: String }, 23 | #[error("Pipeline creation failed:\n{log:}")] 24 | PipelineCreationFailed { source: sys::Error, log: String }, 25 | #[error("OptiX launch failed")] 26 | LaunchFailed { source: sys::Error }, 27 | #[error("CUDA error")] 28 | CudaError { 29 | #[from] 30 | source: cuda::Error, 31 | }, 32 | #[error("Incorrect vertex buffer format: {format:?}")] 33 | IncorrectVertexBufferFormat { format: super::BufferFormat }, 34 | #[error("Incorrect index buffer format: {format:?}")] 35 | IncorrectIndexBufferFormat { format: super::BufferFormat }, 36 | #[error("Failed to compute accel memory usage")] 37 | AccelComputeMemoryUsageFailed { source: sys::Error }, 38 | #[error("Failed to build accel")] 39 | AccelBuildFailed { source: sys::Error }, 40 | #[error("Failed to compact accel")] 41 | AccelCompactFailed { source: sys::Error }, 42 | #[error("Buffer shape mismatch. Expected {e_format:?}x{e_count:}")] 43 | BufferShapeMismatch { 44 | e_format: BufferFormat, 45 | e_count: usize, 46 | }, 47 | } 48 | -------------------------------------------------------------------------------- /optix/src/instance.rs: -------------------------------------------------------------------------------- 1 | use super::acceleration::TraversableHandle; 2 | use super::cuda::Allocator; 3 | use super::math::M4f32; 4 | use bitflags::bitflags; 5 | use optix_sys as sys; 6 | use std::mem::MaybeUninit; 7 | 8 | pub use sys::OptixInstance as Instance; 9 | 10 | bitflags! { 11 | pub struct InstanceFlags: u32 { 12 | const NONE = sys::OptixInstanceFlags_OPTIX_INSTANCE_FLAG_NONE; 13 | const DISABLE_TRIANGLE_FACE_CULLING = sys::OptixInstanceFlags_OPTIX_INSTANCE_FLAG_DISABLE_TRIANGLE_FACE_CULLING; 14 | const FLIP_TRIANGLE_FACING = sys::OptixInstanceFlags_OPTIX_INSTANCE_FLAG_FLIP_TRIANGLE_FACING; 15 | const DISABLE_ANYHIT = sys::OptixInstanceFlags_OPTIX_INSTANCE_FLAG_DISABLE_ANYHIT; 16 | const ENFORCE_ANYHIT = sys::OptixInstanceFlags_OPTIX_INSTANCE_FLAG_ENFORCE_ANYHIT; 17 | const DISABLE_TRANSFORM = sys::OptixInstanceFlags_OPTIX_INSTANCE_FLAG_DISABLE_TRANSFORM; 18 | } 19 | } 20 | 21 | pub fn make_instance<'a, AllocT>( 22 | transform: &M4f32, 23 | instance_id: u32, 24 | sbt_offset: u32, 25 | visibility_mask: u32, 26 | flags: InstanceFlags, 27 | traversable: &TraversableHandle<'a, AllocT>, 28 | ) -> Instance 29 | where 30 | AllocT: Allocator, 31 | { 32 | let mut inst = MaybeUninit::::uninit(); 33 | let mut inst = unsafe { 34 | std::ptr::write_bytes(inst.as_mut_ptr(), 0, 1); 35 | inst.assume_init() 36 | }; 37 | 38 | unsafe { 39 | std::ptr::copy_nonoverlapping( 40 | transform.as_ptr(), 41 | inst.transform.as_mut_ptr(), 42 | 12, 43 | ); 44 | } 45 | 46 | inst.instanceId = instance_id; 47 | inst.sbtOffset = sbt_offset; 48 | inst.visibilityMask = visibility_mask; 49 | inst.flags = flags.bits(); 50 | inst.traversableHandle = traversable.hnd; 51 | 52 | inst 53 | } 54 | -------------------------------------------------------------------------------- /optix/src/math.rs: -------------------------------------------------------------------------------- 1 | use super::{ 2 | buffer::{BufferElement, BufferFormat}, 3 | math_type, DeviceShareable, 4 | }; 5 | 6 | #[cfg(feature = "math-imath")] 7 | pub use imath::*; 8 | 9 | cfg_if::cfg_if! { 10 | if #[cfg(feature="math-nalgebra")] { 11 | 12 | pub use nalgebra_glm::U8Vec2 as V2u8; 13 | pub use nalgebra_glm::U8Vec3 as V3u8; 14 | pub use nalgebra_glm::U8Vec4 as V4u8; 15 | 16 | pub use nalgebra_glm::I16Vec2 as V2u16; 17 | pub use nalgebra_glm::I16Vec3 as V3u16; 18 | pub use nalgebra_glm::I16Vec4 as V4u16; 19 | 20 | pub use nalgebra_glm::IVec2 as V2i32; 21 | pub use nalgebra_glm::IVec3 as V3i32; 22 | pub use nalgebra_glm::IVec4 as V4i32; 23 | 24 | pub use nalgebra_glm::Vec2 as V2f32; 25 | pub use nalgebra_glm::Vec3 as V3f32; 26 | pub use nalgebra_glm::Vec4 as V4f32; 27 | 28 | pub use nalgebra_glm::DVec2 as V2f64; 29 | pub use nalgebra_glm::DVec3 as V3f64; 30 | pub use nalgebra_glm::DVec4 as V4f64; 31 | 32 | pub use nalgebra_glm::Mat4x4 as M4f32; 33 | pub use nalgebra_glm::DMat4x4 as M4f64; 34 | 35 | #[inline(always)] 36 | pub fn v2f32(x: f32, y: f32) -> V2f32 { 37 | V2f32::new(x, y) 38 | } 39 | 40 | #[inline(always)] 41 | pub fn v3f32(x: f32, y: f32, z: f32) -> V3f32 { 42 | V3f32::new(x, y, z) 43 | } 44 | 45 | #[inline(always)] 46 | pub fn v4f32(x: f32, y: f32, z: f32, w: f32) -> V4f32 { 47 | V4f32::new(x, y, z, w) 48 | } 49 | 50 | #[inline(always)] 51 | pub fn v2f64(x: f64, y: f64) -> V2f64 { 52 | V2f64::new(x, y) 53 | } 54 | 55 | #[inline(always)] 56 | pub fn v3f64(x: f64, y: f64, z: f64) -> V3f64 { 57 | V3f64::new(x, y, z) 58 | } 59 | 60 | #[inline(always)] 61 | pub fn v4f64(x: f64, y: f64, z: f64, w: f64) -> V4f64 { 62 | V4f64::new(x, y, z, w) 63 | } 64 | 65 | #[inline(always)] 66 | pub fn v2i32(x: i32, y: i32) -> V2i32 { 67 | V2i32::new(x, y) 68 | } 69 | 70 | #[inline(always)] 71 | pub fn v3i32(x: i32, y: i32, z: i32) -> V3i32 { 72 | V3i32::new(x, y, z) 73 | } 74 | 75 | #[inline(always)] 76 | pub fn v4i32(x: i32, y: i32, z: i32, w: i32) -> V4i32 { 77 | V4i32::new(x, y, z, w) 78 | } 79 | 80 | #[inline(always)] 81 | pub fn v2u8(x: u8, y: u8) -> V2u8 { 82 | V2u8::new(x, y) 83 | } 84 | 85 | #[inline(always)] 86 | pub fn v3u8(x: u8, y: u8, z: u8) -> V3u8 { 87 | V3u8::new(x, y, z) 88 | } 89 | 90 | #[inline(always)] 91 | pub fn v4u8(x: u8, y: u8, z: u8, w: u8) -> V4u8 { 92 | V4u8::new(x, y, z, w) 93 | } 94 | 95 | pub use nalgebra_glm::{ 96 | normalize, 97 | cross, 98 | translate, 99 | translation, 100 | scale, 101 | scaling, 102 | rotate, 103 | rotation, 104 | ortho, 105 | perspective_fov_rh, 106 | perspective_fov_rh_zo, 107 | perspective_fov_lh, 108 | inverse, 109 | inverse_transpose, 110 | affine_inverse, 111 | transpose, 112 | length, 113 | determinant, 114 | zero, 115 | }; 116 | 117 | pub use nalgebra_glm::{Dimension, Scalar, Number, RealField}; 118 | 119 | pub fn cast_slice_v4u8(s: &[u8]) -> &[V4u8] { 120 | if s.len() % 4 != 0 { 121 | panic!("Tried to cast slice of length {} to V4u8", s.len()); 122 | } 123 | 124 | unsafe { 125 | std::slice::from_raw_parts(s.as_ptr() as *const V4u8, s.len() / 4) 126 | } 127 | } 128 | 129 | pub fn cast_slice_v3i32(s: &[i32]) -> &[V3i32] { 130 | if s.len() % 3 != 0 { 131 | panic!("Tried to cast slice of length {} to V3i32", s.len()); 132 | } 133 | 134 | unsafe { 135 | std::slice::from_raw_parts(s.as_ptr() as *const V3i32, s.len() / 3) 136 | } 137 | } 138 | 139 | pub fn cast_slice_v3f32(s: &[f32]) -> &[V3f32] { 140 | if s.len() % 3 != 0 { 141 | panic!("Tried to cast slice of length {} to V3f32", s.len()); 142 | } 143 | 144 | unsafe { 145 | std::slice::from_raw_parts(s.as_ptr() as *const V3f32, s.len() / 3) 146 | } 147 | } 148 | 149 | pub fn cast_slice_v4f32(s: &[f32]) -> &[V4f32] { 150 | if s.len() % 4 != 0 { 151 | panic!("Tried to cast slice of length {} to V4f32", s.len()); 152 | } 153 | 154 | unsafe { 155 | std::slice::from_raw_parts(s.as_ptr() as *const V4f32, s.len() / 4) 156 | } 157 | } 158 | 159 | pub fn cast_slice_v2f32(s: &[f32]) -> &[V2f32] { 160 | if s.len() % 2 != 0 { 161 | panic!("Tried to cast slice of length {} to V2f32", s.len()); 162 | } 163 | 164 | unsafe { 165 | std::slice::from_raw_parts(s.as_ptr() as *const V2f32, s.len() / 2) 166 | } 167 | } 168 | 169 | pub fn cast_slice_m4f32(s: &[f32]) -> &[M4f32] { 170 | if s.len() % 16 != 0 { 171 | panic!("Tried to cast slice of length {} to M4f32", s.len()); 172 | } 173 | 174 | unsafe { 175 | std::slice::from_raw_parts(s.as_ptr() as *const M4f32, s.len() / 16) 176 | } 177 | } 178 | 179 | pub fn cast_slice_m4f64(s: &[f64]) -> &[M4f64] { 180 | if s.len() % 16 != 0 { 181 | panic!("Tried to cast slice of length {} to M4f64", s.len()); 182 | } 183 | 184 | unsafe { 185 | std::slice::from_raw_parts(s.as_ptr() as *const M4f64, s.len() / 16) 186 | } 187 | } 188 | 189 | use nalgebra_glm::{TVec3, vec3, min2, max2}; 190 | 191 | pub struct Box3 where T: RealField { 192 | pub min: TVec3, 193 | pub max: TVec3, 194 | } 195 | 196 | impl Box3 where T: RealField { 197 | pub fn new(min: TVec3, max: TVec3) -> Box3 { 198 | Box3{min, max} 199 | } 200 | 201 | pub fn make_empty() -> Box3 { 202 | let max = T::min_value(); 203 | let min = T::max_value(); 204 | Box3 { 205 | min: vec3(min, min, min), 206 | max: vec3(max, max, max), 207 | } 208 | } 209 | 210 | pub fn center(&self) -> TVec3 { 211 | let d = self.max - self.min; 212 | let half = T::from_f32(0.5).unwrap(); 213 | self.min + d.component_mul(&vec3(half, half, half)) 214 | } 215 | 216 | pub fn extend_by_pnt(&mut self, pnt: TVec3) { 217 | self.min = min2(&self.min, &pnt); 218 | self.max = max2(&self.max, &pnt); 219 | } 220 | } 221 | 222 | pub type Box3f32 = Box3; 223 | pub type Box3f64 = Box3; 224 | 225 | #[inline(always)] 226 | pub fn hmax(v: V3f32) -> f32 { 227 | nalgebra_glm::comp_max(&v) 228 | } 229 | 230 | #[inline(always)] 231 | pub fn hmin(v: V3f32) -> f32 { 232 | nalgebra_glm::comp_min(&v) 233 | } 234 | 235 | pub fn m4f64_to_m4f32(m: &M4f64) -> M4f32 { 236 | M4f32::new( 237 | m[(0, 0)] as f32, 238 | m[(0, 1)] as f32, 239 | m[(0, 2)] as f32, 240 | m[(0, 3)] as f32, 241 | m[(1, 0)] as f32, 242 | m[(1, 1)] as f32, 243 | m[(1, 2)] as f32, 244 | m[(1, 3)] as f32, 245 | m[(2, 0)] as f32, 246 | m[(2, 1)] as f32, 247 | m[(2, 2)] as f32, 248 | m[(2, 3)] as f32, 249 | m[(3, 0)] as f32, 250 | m[(3, 1)] as f32, 251 | m[(3, 2)] as f32, 252 | m[(3, 3)] as f32, 253 | ) 254 | } 255 | 256 | } 257 | } 258 | 259 | math_type!(V2u8, uchar2, BufferFormat::U8x2, 2, u8, 2); 260 | math_type!(V3u8, uchar3, BufferFormat::U8x3, 3, u8, 1); 261 | math_type!(V4u8, uchar4, BufferFormat::U8x4, 4, u8, 4); 262 | 263 | math_type!(V2u16, ushort2, BufferFormat::U16x2, 2, u16, 4); 264 | math_type!(V3u16, ushort3, BufferFormat::U16x3, 3, u16, 2); 265 | math_type!(V4u16, ushort4, BufferFormat::U16x4, 4, u16, 8); 266 | 267 | math_type!(V2i32, i32x2, BufferFormat::I32x2, 2, i32, 8); 268 | math_type!(V3i32, i32x3, BufferFormat::I32x3, 3, i32, 4); 269 | math_type!(V4i32, i32x4, BufferFormat::I32x4, 4, i32, 16); 270 | 271 | math_type!(V2f32, f32x2, BufferFormat::F32x2, 2, f32, 8); 272 | math_type!(V3f32, f32x3, BufferFormat::F32x3, 3, f32, 4); 273 | math_type!(V4f32, f32x4, BufferFormat::F32x4, 4, f32, 16); 274 | 275 | impl DeviceShareable for M4f32 { 276 | type Target = M4f32; 277 | 278 | fn to_device(&self) -> M4f32 { 279 | *self 280 | } 281 | 282 | fn cuda_type() -> String { 283 | "M4f32".into() 284 | } 285 | 286 | fn zero() -> M4f32 { 287 | zero::() 288 | } 289 | } 290 | -------------------------------------------------------------------------------- /optix/src/module.rs: -------------------------------------------------------------------------------- 1 | use optix_sys as sys; 2 | 3 | use super::error::Error; 4 | type Result = std::result::Result; 5 | 6 | pub use super::device_context::DeviceContext; 7 | 8 | use std::ffi::{CStr, CString}; 9 | use ustr::Ustr; 10 | 11 | #[repr(u32)] 12 | #[derive(Debug, Hash, PartialEq, Copy, Clone)] 13 | pub enum CompileOptimizationLevel { 14 | Level0 = 15 | sys::OptixCompileOptimizationLevel::OPTIX_COMPILE_OPTIMIZATION_LEVEL_0, 16 | Level1 = 17 | sys::OptixCompileOptimizationLevel::OPTIX_COMPILE_OPTIMIZATION_LEVEL_1, 18 | Level2 = 19 | sys::OptixCompileOptimizationLevel::OPTIX_COMPILE_OPTIMIZATION_LEVEL_2, 20 | Level3 = 21 | sys::OptixCompileOptimizationLevel::OPTIX_COMPILE_OPTIMIZATION_LEVEL_3, 22 | } 23 | 24 | #[repr(u32)] 25 | #[derive(Debug, Hash, PartialEq, Copy, Clone)] 26 | pub enum CompileDebugLevel { 27 | None = sys::OptixCompileDebugLevel::OPTIX_COMPILE_DEBUG_LEVEL_NONE, 28 | LineInfo = sys::OptixCompileDebugLevel::OPTIX_COMPILE_DEBUG_LEVEL_LINEINFO, 29 | FULL = sys::OptixCompileDebugLevel::OPTIX_COMPILE_DEBUG_LEVEL_FULL, 30 | } 31 | 32 | #[derive(Debug, Hash, PartialEq, Copy, Clone)] 33 | pub struct ModuleCompileOptions { 34 | pub max_register_count: i32, 35 | pub opt_level: CompileOptimizationLevel, 36 | pub debug_level: CompileDebugLevel, 37 | } 38 | 39 | impl From for sys::OptixModuleCompileOptions { 40 | fn from(o: ModuleCompileOptions) -> sys::OptixModuleCompileOptions { 41 | sys::OptixModuleCompileOptions { 42 | maxRegisterCount: o.max_register_count, 43 | optLevel: o.opt_level as u32, 44 | debugLevel: o.debug_level as u32, 45 | } 46 | } 47 | } 48 | 49 | bitflags! { 50 | pub struct TraversableGraphFlags: u32 { 51 | const ALLOW_ANY = sys::OptixTraversableGraphFlags::OPTIX_TRAVERSABLE_GRAPH_FLAG_ALLOW_ANY; 52 | const ALLOW_SINGLE_GAS = sys::OptixTraversableGraphFlags::OPTIX_TRAVERSABLE_GRAPH_FLAG_ALLOW_SINGLE_GAS; 53 | const ALLOW_SINGLE_LEVEL_INSTANCING = sys::OptixTraversableGraphFlags::OPTIX_TRAVERSABLE_GRAPH_FLAG_ALLOW_SINGLE_LEVEL_INSTANCING; 54 | } 55 | } 56 | 57 | bitflags! { 58 | pub struct ExceptionFlags: u32 { 59 | const NONE = sys::OptixExceptionFlags::OPTIX_EXCEPTION_FLAG_NONE; 60 | const STACK_OVERFLOW = sys::OptixExceptionFlags::OPTIX_EXCEPTION_FLAG_STACK_OVERFLOW; 61 | const TRACE_DEPTH = sys::OptixExceptionFlags::OPTIX_EXCEPTION_FLAG_TRACE_DEPTH; 62 | const USER = sys::OptixExceptionFlags::OPTIX_EXCEPTION_FLAG_USER; 63 | const DEBUG = sys::OptixExceptionFlags::OPTIX_EXCEPTION_FLAG_DEBUG; 64 | } 65 | } 66 | 67 | #[derive(Debug, Hash, PartialEq, Clone)] 68 | pub struct PipelineCompileOptions { 69 | pub uses_motion_blur: bool, 70 | pub traversable_graph_flags: TraversableGraphFlags, 71 | pub num_payload_values: i32, 72 | pub num_attribute_values: i32, 73 | pub exception_flags: ExceptionFlags, 74 | pub pipeline_launch_params_variable_name: Ustr, 75 | } 76 | 77 | pub struct Module { 78 | pub(crate) module: sys::OptixModule, 79 | } 80 | 81 | pub type ModuleRef = super::Ref; 82 | 83 | impl DeviceContext { 84 | pub fn module_create_from_ptx( 85 | &mut self, 86 | module_compile_options: ModuleCompileOptions, 87 | pipeline_compile_options: &PipelineCompileOptions, 88 | ptx: &str, 89 | ) -> Result<(ModuleRef, String)> { 90 | let cptx = CString::new(ptx).unwrap(); 91 | let mut log = [0u8; 4096]; 92 | let mut log_len = log.len(); 93 | 94 | let launch_param = CString::new( 95 | pipeline_compile_options 96 | .pipeline_launch_params_variable_name 97 | .as_str(), 98 | ) 99 | .unwrap(); 100 | 101 | let popt = sys::OptixPipelineCompileOptions { 102 | usesMotionBlur: if pipeline_compile_options.uses_motion_blur { 103 | 1 104 | } else { 105 | 0 106 | }, 107 | traversableGraphFlags: pipeline_compile_options 108 | .traversable_graph_flags 109 | .bits(), 110 | numPayloadValues: pipeline_compile_options.num_payload_values, 111 | numAttributeValues: pipeline_compile_options.num_attribute_values, 112 | exceptionFlags: pipeline_compile_options.exception_flags.bits(), 113 | pipelineLaunchParamsVariableName: launch_param.as_ptr(), 114 | }; 115 | 116 | let mopt = module_compile_options.into(); 117 | let mut module = std::ptr::null_mut(); 118 | let res = unsafe { 119 | sys::optixModuleCreateFromPTX( 120 | self.ctx, 121 | &mopt, 122 | &popt, 123 | cptx.as_ptr(), 124 | cptx.as_bytes().len(), 125 | log.as_mut_ptr() as *mut i8, 126 | &mut log_len, 127 | &mut module, 128 | ) 129 | }; 130 | 131 | let log = CStr::from_bytes_with_nul(&log[0..log_len]) 132 | .unwrap() 133 | .to_string_lossy() 134 | .into_owned(); 135 | 136 | if res != sys::OptixResult::OPTIX_SUCCESS { 137 | return Err(Error::ModuleCreationFailed { 138 | source: res.into(), 139 | log, 140 | }); 141 | } 142 | 143 | let module = super::Ref::new(Module { module }); 144 | // self.modules.push(super::Ref::clone(&module)); 145 | Ok((module, log)) 146 | } 147 | } 148 | -------------------------------------------------------------------------------- /optix/src/pipeline.rs: -------------------------------------------------------------------------------- 1 | use optix_sys as sys; 2 | 3 | use super::error::Error; 4 | type Result = std::result::Result; 5 | 6 | use super::device_context::DeviceContext; 7 | use super::module::{CompileDebugLevel, PipelineCompileOptions}; 8 | use super::program_group::ProgramGroupRef; 9 | 10 | use std::ffi::CStr; 11 | 12 | #[derive(Debug, Hash, PartialEq, Copy, Clone)] 13 | pub struct PipelineLinkOptions { 14 | pub max_trace_depth: u32, 15 | pub debug_level: CompileDebugLevel, 16 | pub override_uses_motion_blur: bool, 17 | } 18 | 19 | impl From for sys::OptixPipelineLinkOptions { 20 | fn from(o: PipelineLinkOptions) -> sys::OptixPipelineLinkOptions { 21 | sys::OptixPipelineLinkOptions { 22 | maxTraceDepth: o.max_trace_depth, 23 | debugLevel: o.debug_level as u32, 24 | overrideUsesMotionBlur: o.override_uses_motion_blur as i32, 25 | } 26 | } 27 | } 28 | 29 | pub struct Pipeline { 30 | pub(crate) pipeline: sys::OptixPipeline, 31 | } 32 | 33 | impl Drop for Pipeline { 34 | fn drop(&mut self) { 35 | unsafe { 36 | sys::optixPipelineDestroy(self.pipeline); 37 | } 38 | } 39 | } 40 | 41 | pub type PipelineRef = super::Ref; 42 | 43 | impl DeviceContext { 44 | pub fn pipeline_create( 45 | &mut self, 46 | pipeline_compile_options: &PipelineCompileOptions, 47 | link_options: PipelineLinkOptions, 48 | program_groups: &[ProgramGroupRef], 49 | ) -> Result<(PipelineRef, String)> { 50 | let popt = sys::OptixPipelineCompileOptions { 51 | usesMotionBlur: if pipeline_compile_options.uses_motion_blur { 52 | 1 53 | } else { 54 | 0 55 | }, 56 | traversableGraphFlags: pipeline_compile_options 57 | .traversable_graph_flags 58 | .bits(), 59 | numPayloadValues: pipeline_compile_options.num_payload_values, 60 | numAttributeValues: pipeline_compile_options.num_attribute_values, 61 | exceptionFlags: pipeline_compile_options.exception_flags.bits(), 62 | pipelineLaunchParamsVariableName: unsafe { 63 | pipeline_compile_options 64 | .pipeline_launch_params_variable_name 65 | .as_char_ptr() 66 | }, 67 | }; 68 | 69 | let link_options: sys::OptixPipelineLinkOptions = link_options.into(); 70 | 71 | let pgs: Vec = 72 | program_groups.iter().map(|pg| pg.pg).collect(); 73 | 74 | let mut log = [0u8; 4096]; 75 | let mut log_len = log.len(); 76 | 77 | let mut pipeline: sys::OptixPipeline = std::ptr::null_mut(); 78 | 79 | let res = unsafe { 80 | sys::optixPipelineCreate( 81 | self.ctx, 82 | &popt, 83 | &link_options, 84 | pgs.as_ptr(), 85 | pgs.len() as u32, 86 | log.as_mut_ptr() as *mut i8, 87 | &mut log_len, 88 | &mut pipeline, 89 | ) 90 | }; 91 | 92 | let log = CStr::from_bytes_with_nul(&log[0..log_len]) 93 | .unwrap() 94 | .to_string_lossy() 95 | .into_owned(); 96 | 97 | if res != sys::OptixResult::OPTIX_SUCCESS { 98 | return Err(Error::PipelineCreationFailed { 99 | source: res.into(), 100 | log, 101 | }); 102 | } 103 | let pipeline = super::Ref::new(Pipeline { pipeline }); 104 | self.pipelines.push(super::Ref::clone(&pipeline)); 105 | Ok((pipeline, log)) 106 | } 107 | 108 | /// Sets the stack sizes for a pipeline. 109 | /// 110 | /// Users are encouraged to see the programming guide and the 111 | /// implementations of the helper functions to understand how to 112 | /// construct the stack sizes based on their particular needs. 113 | /// If this method is not used, an internal default implementation is used. 114 | /// The default implementation is correct (but not necessarily optimal) as 115 | /// long as the maximum depth of call trees of CC and DC programs is at most 116 | /// 2 and no motion transforms are used. 117 | /// The maxTraversableGraphDepth responds to the maximal number of 118 | /// traversables visited when calling trace. Every acceleration structure 119 | /// and motion transform count as one level of traversal. E.g., for a simple 120 | /// IAS (instance acceleration structure) -> GAS (geometry acceleration 121 | /// structure) traversal graph, the maxTraversableGraphDepth is two. For 122 | /// IAS -> MT (motion transform) -> GAS, the maxTraversableGraphDepth is 123 | /// three. Note that it does not matter whether a IAS or GAS has motion 124 | /// or not, it always counts as one. Launching optix with exceptions 125 | /// turned on (see OPTIX_EXCEPTION_FLAG_TRACE_DEPTH) will throw an 126 | /// exception if the specified maxTraversableGraphDepth is too small. 127 | /// 128 | /// #Arguments 129 | /// * `direct_callable_stack_size_from_traversable` - The direct stack size 130 | /// requirement for direct callables invoked from IS or AH 131 | /// * `direct_callable_stack_size_from_state` - The direct stack size 132 | /// requirement for direct callables invoked from RG, MS, or CH. 133 | /// * `continuation_stack_size` - The continuation stack requirement. 134 | /// * `max_traversable_graph_depth` - The maximum depth of a traversable 135 | /// graph 136 | /// passed to trace 137 | /// 138 | /// # Panics 139 | /// If the FFI call to optixPipelineSetStackSize returns an error 140 | pub fn pipeline_set_stack_size( 141 | &self, 142 | pipeline: &mut PipelineRef, 143 | direct_callable_stack_size_from_traversable: u32, 144 | direct_callable_stack_size_from_state: u32, 145 | continuation_stack_size: u32, 146 | max_traversable_graph_depth: u32, 147 | ) { 148 | let res = unsafe { 149 | sys::optixPipelineSetStackSize( 150 | pipeline.pipeline, 151 | direct_callable_stack_size_from_traversable, 152 | direct_callable_stack_size_from_state, 153 | continuation_stack_size, 154 | max_traversable_graph_depth, 155 | ) 156 | }; 157 | if res != sys::OptixResult::OPTIX_SUCCESS { 158 | panic!("optixPipelineSetStackSize failed"); 159 | } 160 | } 161 | } 162 | -------------------------------------------------------------------------------- /optix/src/shader_binding_table.rs: -------------------------------------------------------------------------------- 1 | use super::cuda::{self, Allocator}; 2 | use optix_sys as sys; 3 | 4 | use super::{DeviceShareable, ProgramGroupRef}; 5 | 6 | pub trait SbtData {} 7 | 8 | impl SbtData for SbtRecord where T: DeviceShareable {} 9 | 10 | impl SbtData for T where T: DeviceShareable {} 11 | 12 | #[allow(dead_code)] 13 | pub struct ShaderBindingTable<'a, 't, AllocT> 14 | where 15 | AllocT: Allocator, 16 | { 17 | pub(crate) sbt: sys::OptixShaderBindingTable, 18 | rg: cuda::Buffer<'a, AllocT>, 19 | rec_rg: Box, 20 | ex: Option>, 21 | rec_ex: Option>, 22 | ms: Option>, 23 | rec_ms: Vec>, 24 | hg: Option>, 25 | rec_hg: Vec>, 26 | cl: Option>, 27 | rec_cl: Vec>, 28 | } 29 | 30 | pub struct ShaderBindingTableBuilder<'a, 't, AllocT> 31 | where 32 | AllocT: Allocator, 33 | { 34 | rg: cuda::Buffer<'a, AllocT>, 35 | rec_rg: Box, 36 | ex: Option>, 37 | rec_ex: Option>, 38 | ms: Option>, 39 | ms_stride: u32, 40 | ms_count: u32, 41 | rec_ms: Vec>, 42 | hg: Option>, 43 | hg_stride: u32, 44 | hg_count: u32, 45 | rec_hg: Vec>, 46 | cl: Option>, 47 | cl_stride: u32, 48 | cl_count: u32, 49 | rec_cl: Vec>, 50 | } 51 | 52 | impl<'a, 't, AllocT> ShaderBindingTable<'a, 't, AllocT> 53 | where 54 | AllocT: Allocator, 55 | { 56 | pub fn new( 57 | rec_rg: SbtRecord, 58 | tag: u64, 59 | allocator: &'a AllocT, 60 | ) -> ShaderBindingTableBuilder<'a, 't, AllocT> 61 | where 62 | T: DeviceShareable + SbtData + 't, 63 | { 64 | ShaderBindingTableBuilder::new(rec_rg, tag, allocator) 65 | } 66 | } 67 | 68 | impl<'a, 't, AllocT> ShaderBindingTableBuilder<'a, 't, AllocT> 69 | where 70 | AllocT: Allocator, 71 | { 72 | pub fn new( 73 | rec_rg: SbtRecord, 74 | tag: u64, 75 | allocator: &'a AllocT, 76 | ) -> ShaderBindingTableBuilder<'a, 't, AllocT> 77 | where 78 | AllocT: Allocator, 79 | T: 't + DeviceShareable + SbtData, 80 | { 81 | let rec_rg_d = rec_rg.to_device_record(); 82 | ShaderBindingTableBuilder { 83 | rg: cuda::Buffer::with_data( 84 | std::slice::from_ref(&rec_rg_d), 85 | sys::OptixSbtRecordAlignment, 86 | tag, 87 | allocator, 88 | ) 89 | .unwrap(), 90 | rec_rg: Box::new(rec_rg), 91 | ex: None, 92 | rec_ex: None, 93 | ms: None, 94 | ms_stride: 0, 95 | ms_count: 0, 96 | rec_ms: Vec::new(), 97 | hg: None, 98 | hg_stride: 0, 99 | hg_count: 0, 100 | rec_hg: Vec::new(), 101 | cl: None, 102 | cl_stride: 0, 103 | cl_count: 0, 104 | rec_cl: Vec::new(), 105 | } 106 | } 107 | 108 | pub fn exception_record( 109 | mut self, 110 | rec_ex: SbtRecord, 111 | tag: u64, 112 | allocator: &'a AllocT, 113 | ) -> ShaderBindingTableBuilder<'a, 't, AllocT> 114 | where 115 | T: DeviceShareable + SbtData + 't, 116 | { 117 | let rec_ex_d = rec_ex.to_device_record(); 118 | self.ex = Some( 119 | cuda::Buffer::with_data( 120 | std::slice::from_ref(&rec_ex_d), 121 | sys::OptixSbtRecordAlignment, 122 | tag, 123 | allocator, 124 | ) 125 | .unwrap(), 126 | ); 127 | self.rec_ex = Some(Box::new(rec_ex)); 128 | 129 | self 130 | } 131 | 132 | pub fn miss_records( 133 | mut self, 134 | rec_miss: Vec>, 135 | tag: u64, 136 | allocator: &'a AllocT, 137 | ) -> ShaderBindingTableBuilder<'a, 't, AllocT> 138 | where 139 | T: DeviceShareable + SbtData + 't, 140 | { 141 | let rec_miss_d: Vec> = 142 | rec_miss.iter().map(|r| r.to_device_record()).collect(); 143 | self.ms = Some( 144 | cuda::Buffer::with_data( 145 | &rec_miss_d, 146 | sys::OptixSbtRecordAlignment, 147 | tag, 148 | allocator, 149 | ) 150 | .unwrap(), 151 | ); 152 | self.ms_stride = 153 | std::mem::size_of::>() as u32; 154 | self.ms_count = rec_miss.len() as u32; 155 | for r in rec_miss { 156 | self.rec_ms.push(Box::new(r)); 157 | } 158 | 159 | self 160 | } 161 | 162 | pub fn hitgroup_records( 163 | mut self, 164 | rec_hg: Vec>, 165 | tag: u64, 166 | allocator: &'a AllocT, 167 | ) -> ShaderBindingTableBuilder<'a, 't, AllocT> 168 | where 169 | T: DeviceShareable + SbtData + 't, 170 | { 171 | let rec_hg_d: Vec> = 172 | rec_hg.iter().map(|r| r.to_device_record()).collect(); 173 | self.hg = Some( 174 | cuda::Buffer::with_data( 175 | &rec_hg_d, 176 | sys::OptixSbtRecordAlignment, 177 | tag, 178 | allocator, 179 | ) 180 | .unwrap(), 181 | ); 182 | self.hg_stride = 183 | std::mem::size_of::>() as u32; 184 | self.hg_count = rec_hg.len() as u32; 185 | for r in rec_hg { 186 | self.rec_hg.push(Box::new(r)); 187 | } 188 | 189 | self 190 | } 191 | 192 | pub fn callables_records( 193 | mut self, 194 | rec_cl: Vec>, 195 | tag: u64, 196 | allocator: &'a AllocT, 197 | ) -> ShaderBindingTableBuilder<'a, 't, AllocT> 198 | where 199 | T: DeviceShareable + SbtData + 't, 200 | { 201 | let rec_cl_d: Vec> = 202 | rec_cl.iter().map(|r| r.to_device_record()).collect(); 203 | self.cl = Some( 204 | cuda::Buffer::with_data( 205 | &rec_cl_d, 206 | sys::OptixSbtRecordAlignment, 207 | tag, 208 | allocator, 209 | ) 210 | .unwrap(), 211 | ); 212 | self.cl_stride = 213 | std::mem::size_of::>() as u32; 214 | self.cl_count = rec_cl.len() as u32; 215 | for r in rec_cl { 216 | self.rec_cl.push(Box::new(r)); 217 | } 218 | 219 | self 220 | } 221 | 222 | pub fn build(self) -> ShaderBindingTable<'a, 't, AllocT> { 223 | ShaderBindingTable { 224 | sbt: sys::OptixShaderBindingTable { 225 | raygenRecord: self.rg.as_device_ptr(), 226 | exceptionRecord: if let Some(ex) = &self.ex { 227 | ex.as_device_ptr() 228 | } else { 229 | 0 230 | }, 231 | missRecordBase: if let Some(ms) = &self.ms { 232 | ms.as_device_ptr() 233 | } else { 234 | 0 235 | }, 236 | missRecordStrideInBytes: self.ms_stride, 237 | missRecordCount: self.ms_count, 238 | hitgroupRecordBase: if let Some(hg) = &self.hg { 239 | hg.as_device_ptr() 240 | } else { 241 | 0 242 | }, 243 | hitgroupRecordStrideInBytes: self.hg_stride, 244 | hitgroupRecordCount: self.hg_count, 245 | callablesRecordBase: if let Some(cl) = &self.cl { 246 | cl.as_device_ptr() 247 | } else { 248 | 0 249 | }, 250 | callablesRecordStrideInBytes: self.cl_stride, 251 | callablesRecordCount: self.cl_count, 252 | }, 253 | rg: self.rg, 254 | rec_rg: self.rec_rg, 255 | ex: self.ex, 256 | rec_ex: self.rec_ex, 257 | ms: self.ms, 258 | rec_ms: self.rec_ms, 259 | hg: self.hg, 260 | rec_hg: self.rec_hg, 261 | cl: self.cl, 262 | rec_cl: self.rec_cl, 263 | } 264 | } 265 | } 266 | 267 | pub struct SbtRecord 268 | where 269 | T: DeviceShareable, 270 | { 271 | program_group: ProgramGroupRef, 272 | pub data: T, 273 | } 274 | 275 | impl SbtRecord 276 | where 277 | T: DeviceShareable, 278 | { 279 | pub fn new(data: T, program_group: ProgramGroupRef) -> SbtRecord { 280 | SbtRecord { 281 | program_group, 282 | data, 283 | } 284 | } 285 | 286 | pub fn to_device_record(&self) -> SbtRecordDevice { 287 | let mut rec = SbtRecordDevice { 288 | header: [0u8; 32], 289 | data: self.data.to_device(), 290 | }; 291 | 292 | let res = unsafe { 293 | optix_sys::optixSbtRecordPackHeader( 294 | self.program_group.sys_ptr(), 295 | rec.header.as_mut_ptr() as *mut std::os::raw::c_void, 296 | ) 297 | }; 298 | if res != optix_sys::OptixResult::OPTIX_SUCCESS { 299 | panic!("optixSbtRecordPackHeader failed"); 300 | } 301 | 302 | rec 303 | } 304 | } 305 | 306 | #[repr(C)] 307 | #[repr(align(16))] 308 | pub struct SbtRecordDevice { 309 | header: [u8; 32], 310 | data: T, 311 | } 312 | -------------------------------------------------------------------------------- /optix/src/texture.rs: -------------------------------------------------------------------------------- 1 | use super::buffer::BufferElement; 2 | use super::cuda; 3 | use super::error::Error; 4 | use super::math::*; 5 | use super::DeviceShareable; 6 | type Result = std::result::Result; 7 | 8 | #[derive(Debug)] 9 | pub struct Texture { 10 | texture_object: cuda::TextureObject, 11 | } 12 | 13 | pub trait TextureElement: BufferElement { 14 | const CHANNEL_DESC: cuda::ChannelFormatDesc; 15 | const READ_MODE: cuda::TextureReadMode; 16 | const SRGB: bool; 17 | } 18 | 19 | #[repr(u32)] 20 | pub enum WrapMode { 21 | Clamp = cuda::TextureAddressMode::Clamp as u32, 22 | Wrap = cuda::TextureAddressMode::Wrap as u32, 23 | Border = cuda::TextureAddressMode::Border as u32, 24 | Mirror = cuda::TextureAddressMode::Mirror as u32, 25 | } 26 | 27 | impl From for cuda::TextureAddressMode { 28 | fn from(m: WrapMode) -> cuda::TextureAddressMode { 29 | match m { 30 | WrapMode::Clamp => cuda::TextureAddressMode::Clamp, 31 | WrapMode::Wrap => cuda::TextureAddressMode::Wrap, 32 | WrapMode::Border => cuda::TextureAddressMode::Border, 33 | WrapMode::Mirror => cuda::TextureAddressMode::Mirror, 34 | } 35 | } 36 | } 37 | 38 | impl Texture { 39 | pub fn new( 40 | pixels: &[T], 41 | width: usize, 42 | height: usize, 43 | wrap_mode: WrapMode, 44 | ) -> Result 45 | where 46 | T: TextureElement, 47 | { 48 | let pixels = unsafe { 49 | std::slice::from_raw_parts( 50 | pixels.as_ptr() as *const T::ComponentType, 51 | pixels.len() * T::COMPONENTS, 52 | ) 53 | }; 54 | let array = cuda::Array::new( 55 | pixels, 56 | T::CHANNEL_DESC, 57 | width, 58 | height, 59 | T::COMPONENTS, 60 | cuda::ArrayFlags::DEFAULT, 61 | ) 62 | .unwrap(); 63 | 64 | let tex_desc = cuda::TextureDesc::new() 65 | .address_mode([wrap_mode.into(); 3]) 66 | .filter_mode(cuda::TextureFilterMode::Linear) 67 | .read_mode(T::READ_MODE) 68 | .normalized_coords(true) 69 | .srgb(T::SRGB) 70 | .build(); 71 | 72 | let texture_object = cuda::TextureObject::new( 73 | cuda::ResourceDesc::Array(array), 74 | &tex_desc, 75 | ) 76 | .unwrap(); 77 | 78 | Ok(Texture { texture_object }) 79 | } 80 | } 81 | 82 | impl TextureElement for u8 { 83 | const CHANNEL_DESC: cuda::ChannelFormatDesc = cuda::ChannelFormatDesc { 84 | x: 8, 85 | y: 0, 86 | z: 0, 87 | w: 0, 88 | f: cuda::ChannelFormatKind::Unsigned, 89 | }; 90 | const READ_MODE: cuda::TextureReadMode = 91 | cuda::TextureReadMode::NormalizedFloat; 92 | const SRGB: bool = true; 93 | } 94 | 95 | impl TextureElement for f32 { 96 | const CHANNEL_DESC: cuda::ChannelFormatDesc = cuda::ChannelFormatDesc { 97 | x: 32, 98 | y: 0, 99 | z: 0, 100 | w: 0, 101 | f: cuda::ChannelFormatKind::Float, 102 | }; 103 | const READ_MODE: cuda::TextureReadMode = cuda::TextureReadMode::ElementType; 104 | const SRGB: bool = false; 105 | } 106 | 107 | impl TextureElement for V4u8 { 108 | const CHANNEL_DESC: cuda::ChannelFormatDesc = cuda::ChannelFormatDesc { 109 | x: 8, 110 | y: 8, 111 | z: 8, 112 | w: 8, 113 | f: cuda::ChannelFormatKind::Unsigned, 114 | }; 115 | const READ_MODE: cuda::TextureReadMode = 116 | cuda::TextureReadMode::NormalizedFloat; 117 | const SRGB: bool = true; 118 | } 119 | 120 | impl TextureElement for V4f32 { 121 | const CHANNEL_DESC: cuda::ChannelFormatDesc = cuda::ChannelFormatDesc { 122 | x: 32, 123 | y: 32, 124 | z: 32, 125 | w: 32, 126 | f: cuda::ChannelFormatKind::Float, 127 | }; 128 | const READ_MODE: cuda::TextureReadMode = cuda::TextureReadMode::ElementType; 129 | const SRGB: bool = false; 130 | } 131 | 132 | impl DeviceShareable for Texture { 133 | type Target = cuda::cudaTextureObject_t; 134 | fn to_device(&self) -> Self::Target { 135 | self.texture_object.as_device_ptr() 136 | } 137 | fn cuda_type() -> String { 138 | "cudaTextureObject_t".into() 139 | } 140 | fn zero() -> Self::Target { 141 | 0 142 | } 143 | } 144 | 145 | // impl DeviceShareable for Option { 146 | // type Target = cuda::cudaTextureObject_t; 147 | // fn to_device(&self) -> Self::Target { 148 | // match self { 149 | // Some(t) => t.texture_object.as_device_ptr(), 150 | // None => 0, 151 | // } 152 | // } 153 | // fn cuda_type() -> String { 154 | // "cudaTextureObject_t".into() 155 | // } 156 | // fn zero() -> Self::Target { 157 | // 0 158 | // } 159 | // } 160 | 161 | // impl DeviceShareable for Option> { 162 | // type Target = cuda::cudaTextureObject_t; 163 | // fn to_device(&self) -> Self::Target { 164 | // match self { 165 | // Some(t) => t.texture_object.as_device_ptr(), 166 | // None => 0, 167 | // } 168 | // } 169 | // fn cuda_type() -> String { 170 | // "cudaTextureObject_t".into() 171 | // } 172 | // fn zero() -> Self::Target { 173 | // 0 174 | // } 175 | // } 176 | -------------------------------------------------------------------------------- /rustfmt.toml: -------------------------------------------------------------------------------- 1 | edition = "2018" 2 | max_width = 80 3 | wrap_comments = true 4 | --------------------------------------------------------------------------------