-
Notifications
You must be signed in to change notification settings - Fork 12
/
Copy pathfilter.rs
384 lines (361 loc) · 12.4 KB
/
filter.rs
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
use crate::{buffer::Buffer, device::Device, sys::*, Error, Quality};
use std::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,
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,
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().and_then(|buf| {
if buf.size == albedo.len() {
Some(buf)
} else {
None
}
}) {
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().and_then(|buf| {
if buf.size == normal.len() {
Some(buf)
} else {
None
}
}) {
None => {
self.albedo = 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().and_then(|buf| {
if buf.size == albedo.len() {
Some(buf)
} else {
None
}
}) {
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: Buffer,
normal: Buffer,
) -> Option<&mut RayTracing<'a>> {
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: Buffer) -> Option<&mut RayTracing<'a>> {
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
}
/// 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;
match &self.albedo {
None => {}
Some(buffer) => {
if buffer.size != buffer_dims {
self.albedo = None;
}
}
}
match &self.normal {
None => {}
Some(buffer) => {
if buffer.size != buffer_dims {
self.normal = None;
}
}
}
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: &mut Buffer) -> Result<(), Error> {
self.execute_filter_buffer(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: &mut Buffer) -> Result<(), Error> {
self.execute_filter_buffer(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 mut out = self
.device
.create_buffer(output)
.ok_or(Error::OutOfMemory)?;
self.execute_filter_buffer(color.as_ref(), &mut 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: &mut 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 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);
oidnSetFilterInt(
self.handle,
b"quality\0" as *const _ as _,
self.filter_quality as i32,
);
oidnCommitFilter(self.handle);
oidnExecuteFilter(self.handle);
}
Ok(())
}
}
impl Drop for RayTracing<'_> {
fn drop(&mut self) {
unsafe {
oidnReleaseFilter(self.handle);
oidnReleaseDevice(self.device.0);
}
}
}
unsafe impl Send for RayTracing<'_> {}