@@ -5,7 +5,9 @@ use imgui::Ui;
55
66use std:: time:: Duration ;
77
8- use crate :: { common:: Camera , optix:: OptixRenderer } ;
8+ use crate :: common:: Camera ;
9+ #[ cfg( feature = "optix" ) ]
10+ use crate :: optix:: OptixRenderer ;
911use anyhow:: Result ;
1012use cust:: {
1113 error:: CudaResult ,
@@ -15,6 +17,7 @@ use cust::{
1517 prelude:: * ,
1618} ;
1719use glam:: { U8Vec3 , USizeVec2 } ;
20+ #[ cfg( feature = "optix" ) ]
1821use optix:: {
1922 context:: DeviceContext ,
2023 denoiser:: { Denoiser , DenoiserModelKind , Image , ImageFormat } ,
@@ -33,44 +36,55 @@ pub(crate) static PTX: &str = include_str!(concat!(env!("OUT_DIR"), "/kernels.pt
3336pub struct CudaRenderer {
3437 stream : Stream ,
3538 module : Module ,
39+ #[ cfg( feature = "optix" ) ]
3640 denoiser : Denoiser ,
41+ #[ cfg( feature = "optix" ) ]
3742 _optix_context : DeviceContext ,
3843 _context : Context ,
3944
4045 buffers : CudaRendererBuffers ,
4146 cpu_image : Vec < U8Vec3 > ,
47+ #[ cfg( feature = "optix" ) ]
4248 optix_renderer : OptixRenderer ,
4349}
4450
4551impl CudaRenderer {
4652 pub fn new ( dimensions : USizeVec2 , camera : & Camera , scene : & Scene ) -> Result < Self > {
4753 let context = cust:: quick_init ( ) ?;
54+ #[ cfg( feature = "optix" ) ]
4855 optix:: init ( ) . unwrap ( ) ;
4956
57+ #[ cfg( feature = "optix" ) ]
5058 let mut optix_context = DeviceContext :: new ( & context, false ) . unwrap ( ) ;
5159
5260 let module = Module :: from_ptx ( PTX , & [ ] ) . unwrap ( ) ;
5361 let stream = Stream :: new ( StreamFlags :: NON_BLOCKING , None ) ?;
62+
63+ #[ cfg( feature = "optix" ) ]
5464 let mut denoiser =
5565 Denoiser :: new ( & optix_context, DenoiserModelKind :: Ldr , Default :: default ( ) ) . unwrap ( ) ;
56-
66+ # [ cfg ( feature = "optix" ) ]
5767 denoiser
5868 . setup_state ( & stream, dimensions. x as u32 , dimensions. y as u32 , false )
5969 . unwrap ( ) ;
6070
6171 let buffers = CudaRendererBuffers :: new ( dimensions, camera, scene) ?;
6272 let cpu_image = vec ! [ U8Vec3 :: ZERO ; dimensions. element_product( ) ] ;
6373
74+ #[ cfg( feature = "optix" ) ]
6475 let optix_renderer = OptixRenderer :: new ( & mut optix_context, & stream, scene) ?;
6576
6677 Ok ( Self {
6778 _context : context,
79+ #[ cfg( feature = "optix" ) ]
6880 _optix_context : optix_context,
81+ #[ cfg( feature = "optix" ) ]
6982 denoiser,
7083 module,
7184 stream,
7285 buffers,
7386 cpu_image,
87+ #[ cfg( feature = "optix" ) ]
7488 optix_renderer,
7589 } )
7690 }
@@ -97,9 +111,11 @@ impl CudaRenderer {
97111 self . cpu_image
98112 . resize ( new_size. element_product ( ) , U8Vec3 :: ZERO ) ;
99113
114+ #[ cfg( feature = "optix" ) ]
100115 self . denoiser
101116 . setup_state ( & self . stream , new_size. x as u32 , new_size. y as u32 , false )
102117 . unwrap ( ) ;
118+
103119 Ok ( ( ) )
104120 }
105121
@@ -123,7 +139,9 @@ impl CudaRenderer {
123139 let stream = & self . stream ;
124140
125141 let ( blocks, threads) = self . launch_dimensions ( ) ;
142+ #[ cfg( feature = "optix" ) ]
126143 let width = self . buffers . viewport . bounds . x as u32 ;
144+ #[ cfg( feature = "optix" ) ]
127145 let height = self . buffers . viewport . bounds . y as u32 ;
128146
129147 let start = Event :: new ( EventFlags :: DEFAULT ) ?;
@@ -144,24 +162,30 @@ impl CudaRenderer {
144162 }
145163
146164 let input_buf = if denoise {
147- let input_image = Image :: new (
148- & self . buffers . scaled_buffer ,
149- ImageFormat :: Float3 ,
150- width,
151- height,
152- ) ;
153-
154- self . denoiser
155- . invoke (
156- stream,
157- Default :: default ( ) ,
158- input_image,
159- Default :: default ( ) ,
160- & mut self . buffers . denoised_buffer ,
161- )
162- . unwrap ( ) ;
165+ #[ cfg( not( feature = "optix" ) ) ]
166+ unreachable ! ( ) ;
167+
168+ #[ cfg( feature = "optix" ) ]
169+ {
170+ let input_image = Image :: new (
171+ & self . buffers . scaled_buffer ,
172+ ImageFormat :: Float3 ,
173+ width,
174+ height,
175+ ) ;
176+
177+ self . denoiser
178+ . invoke (
179+ stream,
180+ Default :: default ( ) ,
181+ input_image,
182+ Default :: default ( ) ,
183+ & mut self . buffers . denoised_buffer ,
184+ )
185+ . unwrap ( ) ;
163186
164- self . buffers . denoised_buffer . as_device_ptr ( )
187+ self . buffers . denoised_buffer . as_device_ptr ( )
188+ }
165189 } else {
166190 self . buffers . scaled_buffer . as_device_ptr ( )
167191 } ;
@@ -204,6 +228,10 @@ impl CudaRenderer {
204228 start. record ( stream) ?;
205229
206230 if use_optix {
231+ #[ cfg( not( feature = "optix" ) ) ]
232+ unreachable ! ( ) ;
233+
234+ #[ cfg( feature = "optix" ) ]
207235 self . optix_renderer . render ( stream, & mut self . buffers ) ?;
208236 } else {
209237 unsafe {
0 commit comments