1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
use crate::{Error, Quality, buffer::Buffer, device::Device, sys::*};
use std::{cell::Cell, mem};
/// A generic ray tracing denoising filter for denoising
/// images produces with Monte Carlo ray tracing methods
/// such as path tracing.
pub struct RayTracing<'a> {
handle: OIDNFilter,
device: &'a Device,
albedo: Option<Buffer>,
normal: Option<Buffer>,
hdr: bool,
input_scale: f32,
srgb: bool,
clean_aux: bool,
weights: Option<Vec<u8>>,
// Tracks whether OIDN currently holds a shared-data pointer into `weights`.
weights_set: Cell<bool>,
img_dims: (usize, usize, usize),
filter_quality: OIDNQuality,
}
impl<'a> RayTracing<'a> {
pub fn new(device: &'a Device) -> RayTracing<'a> {
unsafe {
oidnRetainDevice(device.0);
}
let filter = unsafe { oidnNewFilter(device.0, b"RT\0" as *const _ as _) };
RayTracing {
handle: filter,
device,
albedo: None,
normal: None,
hdr: false,
input_scale: f32::NAN,
srgb: false,
clean_aux: false,
weights: None,
weights_set: Cell::new(false),
img_dims: (0, 0, 0),
filter_quality: 0,
}
}
/// Sets the quality of the output, the default is high.
///
/// Balanced lowers the precision, if possible, however
/// some devices will not support this and so
/// the result (and performance) will stay the same as high.
/// Balanced is recommended for realtime usages.
pub fn filter_quality(&mut self, quality: Quality) -> &mut RayTracing<'a> {
self.filter_quality = quality.as_raw_oidn_quality();
self
}
/// Set input auxiliary images containing the albedo and normals.
///
/// Albedo must have three channels per pixel with values in [0, 1].
/// Normal must contain the shading normal as three channels per pixel
/// *world-space* or *view-space* vectors with arbitrary length, values
/// in `[-1, 1]`.
///
/// # Panics
/// - if resource creation fails
pub fn albedo_normal(&mut self, albedo: &[f32], normal: &[f32]) -> &mut RayTracing<'a> {
match self.albedo.as_mut().filter(|buf| buf.size == albedo.len()) {
None => {
self.albedo = Some(self.device.create_buffer(albedo).unwrap());
}
Some(buf) => {
buf.write(albedo)
.expect("we check if the size is the same already");
}
}
match self.normal.as_mut().filter(|buf| buf.size == normal.len()) {
None => {
self.normal = Some(self.device.create_buffer(normal).unwrap());
}
Some(buf) => {
buf.write(normal)
.expect("we check if the size is the same already");
}
}
self
}
/// Set an input auxiliary image containing the albedo per pixel (three
/// channels, values in `[0, 1]`).
///
/// # Panics
/// - if resource creation fails
pub fn albedo(&mut self, albedo: &[f32]) -> &mut RayTracing<'a> {
match self.albedo.as_mut().filter(|buf| buf.size == albedo.len()) {
None => {
self.albedo = Some(self.device.create_buffer(albedo).unwrap());
}
Some(buf) => {
buf.write(albedo)
.expect("we check if the size is the same already");
}
}
self
}
/// Set input auxiliary buffer containing the albedo and normals.
///
/// Albedo buffer must have three channels per pixel with values in [0, 1].
/// Normal must contain the shading normal as three channels per pixel
/// *world-space* or *view-space* vectors with arbitrary length, values
/// in `[-1, 1]`.
///
/// This function is the same as [RayTracing::albedo_normal] but takes
/// buffers instead
///
/// Returns [None] if either buffer was not created by this device
pub fn albedo_normal_buffer(
&mut self,
albedo: impl Into<Buffer>,
normal: impl Into<Buffer>,
) -> Option<&mut RayTracing<'a>> {
let albedo = albedo.into();
let normal = normal.into();
if !self.device.same_device_as_buf(&albedo) || !self.device.same_device_as_buf(&normal) {
return None;
}
self.albedo = Some(albedo);
self.normal = Some(normal);
Some(self)
}
/// Set an input auxiliary buffer containing the albedo per pixel (three
/// channels, values in `[0, 1]`).
///
/// This function is the same as [RayTracing::albedo] but takes buffers
/// instead
///
/// Returns [None] if albedo buffer was not created by this device
pub fn albedo_buffer(&mut self, albedo: impl Into<Buffer>) -> Option<&mut RayTracing<'a>> {
let albedo = albedo.into();
if !self.device.same_device_as_buf(&albedo) {
return None;
}
self.albedo = Some(albedo);
Some(self)
}
/// Set whether the color is HDR.
pub fn hdr(&mut self, hdr: bool) -> &mut RayTracing<'a> {
self.hdr = hdr;
self
}
#[deprecated(since = "1.3.1", note = "Please use RayTracing::input_scale instead")]
pub fn hdr_scale(&mut self, hdr_scale: f32) -> &mut RayTracing<'a> {
self.input_scale = hdr_scale;
self
}
/// Sets a scale to apply to input values before filtering, without scaling
/// the output too.
///
/// This can be used to map color or auxiliary feature values to the
/// expected range. E.g. for mapping HDR values to physical units (which
/// affects the quality of the output but not the range of the output
/// values). If not set, the scale is computed implicitly for HDR images
/// or set to 1 otherwise
pub fn input_scale(&mut self, input_scale: f32) -> &mut RayTracing<'a> {
self.input_scale = input_scale;
self
}
/// Set whether the color is encoded with the sRGB (or 2.2 gamma) curve (LDR
/// only) or is linear.
///
/// The output will be encoded with the same curve.
pub fn srgb(&mut self, srgb: bool) -> &mut RayTracing<'a> {
self.srgb = srgb;
self
}
/// Set whether the auxiliary feature (albedo, normal) images are
/// noise-free.
///
/// Recommended for highest quality but should not be enabled for noisy
/// auxiliary images to avoid residual noise.
pub fn clean_aux(&mut self, clean_aux: bool) -> &mut RayTracing<'a> {
self.clean_aux = clean_aux;
self
}
/// Set custom trained model weights for the RT filter.
///
/// The bytes are copied and kept alive by the filter until new weights are
/// supplied or [RayTracing::clear_weights] is called.
pub fn weights(&mut self, weights: &[u8]) -> &mut RayTracing<'a> {
self.weights = Some(weights.to_vec());
if self.weights_set.get() {
let weights = self.weights.as_ref().expect("weights were just set");
unsafe {
oidnSetSharedFilterData(
self.handle,
b"weights\0" as *const _ as _,
weights.as_ptr() as *mut _,
weights.len(),
);
}
}
self
}
/// Clear any previously supplied custom model weights.
pub fn clear_weights(&mut self) -> &mut RayTracing<'a> {
if self.weights_set.get() {
unsafe {
oidnUnsetFilterData(self.handle, b"weights\0" as *const _ as _);
}
self.weights_set.set(false);
}
self.weights = None;
self
}
fn buffer_size_mismatch(buffer: &Option<Buffer>, expected: usize) -> bool {
buffer.as_ref().is_some_and(|b| b.size != expected)
}
/// sets the dimensions of the denoising image, if new width * new height
/// does not equal old width * old height
pub fn image_dimensions(&mut self, width: usize, height: usize) -> &mut RayTracing<'a> {
let buffer_dims = 3 * width * height;
if Self::buffer_size_mismatch(&self.albedo, buffer_dims) {
self.albedo = None;
unsafe {
oidnUnsetFilterImage(self.handle, b"albedo\0" as *const _ as _);
}
}
if Self::buffer_size_mismatch(&self.normal, buffer_dims) {
self.normal = None;
unsafe {
oidnUnsetFilterImage(self.handle, b"normal\0" as *const _ as _);
}
}
self.img_dims = (width, height, buffer_dims);
self
}
pub fn filter(&self, color: &[f32], output: &mut [f32]) -> Result<(), Error> {
self.execute_filter(Some(color), output)
}
pub fn filter_buffer(&self, color: &Buffer, output: &Buffer) -> Result<(), Error> {
self.execute_filter_buffer(Some(color), output)
}
/// Starts filtering buffer-backed images asynchronously.
///
/// The returned guard keeps the filter and buffers borrowed until the
/// device has been synchronized. Dropping the guard synchronizes the
/// device as a convenience, but leaking it prevents synchronization.
///
/// # Safety
///
/// The returned guard must not be leaked with mechanisms such as
/// [`std::mem::forget`] or [`std::mem::ManuallyDrop`]. It must be waited
/// or dropped before the filter or buffers are accessed, mutated, or
/// released.
pub unsafe fn filter_buffer_async<'filter>(
&'filter mut self,
color: &'filter Buffer,
output: &'filter mut Buffer,
) -> Result<PendingFilter<'filter, 'a>, Error> {
unsafe { self.execute_filter_buffer_async(Some(color), output) }
}
pub fn filter_in_place(&self, color: &mut [f32]) -> Result<(), Error> {
self.execute_filter(None, color)
}
pub fn filter_in_place_buffer(&self, color: &Buffer) -> Result<(), Error> {
self.execute_filter_buffer(None, color)
}
/// Starts in-place filtering on a buffer asynchronously.
///
/// The returned guard keeps the filter and buffer borrowed until the device
/// has been synchronized. Dropping the guard synchronizes the device as a
/// convenience, but leaking it prevents synchronization.
///
/// # Safety
///
/// The returned guard must not be leaked with mechanisms such as
/// [`std::mem::forget`] or [`std::mem::ManuallyDrop`]. It must be waited
/// or dropped before the filter or buffer are accessed, mutated, or
/// released.
pub unsafe fn filter_in_place_buffer_async<'filter>(
&'filter mut self,
color: &'filter mut Buffer,
) -> Result<PendingFilter<'filter, 'a>, Error> {
unsafe { self.execute_filter_buffer_async(None, color) }
}
fn execute_filter(&self, color: Option<&[f32]>, output: &mut [f32]) -> Result<(), Error> {
let color = match color {
None => None,
Some(color) => Some(self.device.create_buffer(color).ok_or(Error::OutOfMemory)?),
};
let out = self
.device
.create_buffer(output)
.ok_or(Error::OutOfMemory)?;
self.execute_filter_buffer(color.as_ref(), &out)?;
unsafe {
oidnReadBuffer(
out.buf,
0,
out.size * mem::size_of::<f32>(),
output.as_mut_ptr() as *mut _,
)
};
Ok(())
}
fn execute_filter_buffer(&self, color: Option<&Buffer>, output: &Buffer) -> Result<(), Error> {
self.configure_filter_buffer(color, output)?;
unsafe {
oidnExecuteFilter(self.handle);
}
Ok(())
}
unsafe fn execute_filter_buffer_async<'filter>(
&'filter mut self,
color: Option<&'filter Buffer>,
output: &'filter mut Buffer,
) -> Result<PendingFilter<'filter, 'a>, Error> {
self.configure_filter_buffer(color, output)?;
unsafe {
oidnExecuteFilterAsync(self.handle);
}
Ok(PendingFilter {
filter: self,
_color: color,
_output: output,
complete: false,
})
}
fn configure_filter_buffer(
&self,
color: Option<&Buffer>,
output: &Buffer,
) -> Result<(), Error> {
if let Some(alb) = &self.albedo {
if alb.size != self.img_dims.2 {
return Err(Error::InvalidImageDimensions);
}
unsafe {
oidnSetFilterImage(
self.handle,
b"albedo\0" as *const _ as _,
alb.buf,
OIDNFormat_OIDN_FORMAT_FLOAT3,
self.img_dims.0 as _,
self.img_dims.1 as _,
0,
0,
0,
);
}
// No use supplying normal if albedo was
// not also given.
if let Some(norm) = &self.normal {
if norm.size != self.img_dims.2 {
return Err(Error::InvalidImageDimensions);
}
unsafe {
oidnSetFilterImage(
self.handle,
b"normal\0" as *const _ as _,
norm.buf,
OIDNFormat_OIDN_FORMAT_FLOAT3,
self.img_dims.0 as _,
self.img_dims.1 as _,
0,
0,
0,
);
}
}
}
let color_buffer = match color {
Some(color) => {
if !self.device.same_device_as_buf(color) {
return Err(Error::InvalidArgument);
}
if color.size != self.img_dims.2 {
return Err(Error::InvalidImageDimensions);
}
color
}
None => {
if !self.device.same_device_as_buf(output) {
return Err(Error::InvalidArgument);
}
if output.size != self.img_dims.2 {
return Err(Error::InvalidImageDimensions);
}
// actually this is a needed borrow, the compiler complains otherwise
#[allow(clippy::needless_borrow)]
&output
}
};
unsafe {
oidnSetFilterImage(
self.handle,
b"color\0" as *const _ as _,
color_buffer.buf,
OIDNFormat_OIDN_FORMAT_FLOAT3,
self.img_dims.0 as _,
self.img_dims.1 as _,
0,
0,
0,
);
}
if !self.device.same_device_as_buf(output) {
return Err(Error::InvalidArgument);
}
if output.size != self.img_dims.2 {
return Err(Error::InvalidImageDimensions);
}
unsafe {
oidnSetFilterImage(
self.handle,
b"output\0" as *const _ as _,
output.buf,
OIDNFormat_OIDN_FORMAT_FLOAT3,
self.img_dims.0 as _,
self.img_dims.1 as _,
0,
0,
0,
);
oidnSetFilterBool(self.handle, b"hdr\0" as *const _ as _, self.hdr);
oidnSetFilterFloat(
self.handle,
b"inputScale\0" as *const _ as _,
self.input_scale,
);
oidnSetFilterBool(self.handle, b"srgb\0" as *const _ as _, self.srgb);
oidnSetFilterBool(self.handle, b"clean_aux\0" as *const _ as _, self.clean_aux);
if let Some(weights) = &self.weights {
oidnSetSharedFilterData(
self.handle,
b"weights\0" as *const _ as _,
weights.as_ptr() as *mut _,
weights.len(),
);
self.weights_set.set(true);
}
oidnSetFilterInt(
self.handle,
b"quality\0" as *const _ as _,
self.filter_quality,
);
oidnCommitFilter(self.handle);
}
Ok(())
}
}
impl Drop for RayTracing<'_> {
fn drop(&mut self) {
unsafe {
oidnReleaseFilter(self.handle);
oidnReleaseDevice(self.device.0);
}
}
}
unsafe impl Send for RayTracing<'_> {}
/// Completion guard for an asynchronous OIDN filter execution.
///
/// Calling [`PendingFilter::wait`] or dropping this guard blocks until
/// [`Device::sync`] completes.
#[must_use = "dropping the guard blocks to synchronize; call wait() explicitly when possible"]
pub struct PendingFilter<'filter, 'device> {
filter: &'filter mut RayTracing<'device>,
_color: Option<&'filter Buffer>,
_output: &'filter mut Buffer,
complete: bool,
}
impl PendingFilter<'_, '_> {
/// Blocks until the asynchronous filter execution has completed.
pub fn wait(mut self) {
self.finish();
}
fn finish(&mut self) {
if !self.complete {
self.filter.device.sync();
self.complete = true;
}
}
}
impl Drop for PendingFilter<'_, '_> {
fn drop(&mut self) {
self.finish();
}
}