Skip to content

Commit 277c713

Browse files
author
Felix Igelbrink
committed
denoiser wrapper working, not all features have been tested though
1 parent cea992a commit 277c713

4 files changed

Lines changed: 428 additions & 45 deletions

File tree

examples/denolser.py

Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
import optix as ox
2+
import cupy as cp
3+
import argparse
4+
import logging
5+
import sys
6+
import imageio
7+
import matplotlib.pyplot as plt
8+
9+
logging.basicConfig(stream=sys.stdout, level=logging.INFO)
10+
log = logging.getLogger()
11+
12+
if __name__ == "__main__":
13+
parser = argparse.ArgumentParser("Denoise images using the builtin Optix denoiser")
14+
15+
parser.add_argument('color_file', help="The input color image to denoise (exr format)")
16+
parser.add_argument('-n', '--normal', type=str, required=False,
17+
help="Additional normals image (exr format)")
18+
parser.add_argument('-a', '--albedo', type=str, required=False,
19+
help="Additional albedo image (exr format)")
20+
parser.add_argument('-f', '--flow', type=str, required=False,
21+
help=f"Additional flow image for temporal denoising")
22+
parser.add_argument('-o', '--out', type=str, default="denoised.exr", required=False,
23+
help="Output file. Defaults to \"denoised.exr\"")
24+
parser.add_argument('-F', '--Frames', type=int, nargs=2, required=False,
25+
help="First-Last frame number of a sequence")
26+
parser.add_argument('-e', '--exposure', type=float, required=False,
27+
help="apply exposure on output images")
28+
parser.add_argument('-t', '--tilesize', type=int, nargs=2, required=False,
29+
help="Use tiling to save GPU memory")
30+
parser.add_argument('-z', action='store_true', required=False,
31+
help="Apply flow to input images (no denoising) and write output")
32+
parser.add_argument('-k', action='store_true', required=False,
33+
help="Use kernel prediction model even if there are no AOVs")
34+
35+
args = parser.parse_args()
36+
37+
38+
# a color image is always required
39+
color_image = imageio.read(args.color_file).get_data(0)
40+
plt.imshow(color_image / 10000)
41+
plt.show()
42+
normal_image = None
43+
albedo_image = None
44+
flow_image = None
45+
if args.normal is not None:
46+
normal_image = imageio.read(args.normal).get_data(0)
47+
48+
if args.albedo is not None:
49+
albedo_image = imageio.read(args.albedo).get_data(0)
50+
51+
# setup optix context and denoiser
52+
53+
logger = ox.Logger(log)
54+
ctx = ox.DeviceContext(validation_mode=True, log_callback_function=logger, log_callback_level=3)
55+
56+
model_kind = ox.DenoiserModelKind.HDR
57+
58+
denoiser = ox.Denoiser(ctx, model_kind=model_kind, guide_albedo=args.albedo is not None, guide_normals=args.normal is not None, kp_mode=args.k)
59+
60+
ret = denoiser.invoke(color_image, albedo=albedo_image if args.albedo else None, normals=normal_image if args.normal else None)
61+
ret = cp.asnumpy(ret)
62+
63+
ret /= 10000
64+
plt.imshow(ret)
65+
plt.show()

optix/__init__.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
from .shader_binding_table import ShaderBindingTable
77
from .pipeline import CompileDebugLevel, ExceptionFlags, TraversableGraphFlags, \
88
PrimitiveTypeFlags, PipelineCompileOptions, PipelineLinkOptions, Pipeline
9+
from .denoiser import *
910
from .logging_utility import Logger
1011
from ._version import __version__
1112

optix/denoiser.pxd

Lines changed: 67 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -3,14 +3,22 @@ from .context cimport OptixDeviceContext, OptixContextObject
33
from libcpp.vector cimport vector
44
from .base cimport OptixObject
55
from libc.stdint cimport uintptr_t
6+
from libcpp cimport bool
67

78
cdef extern from "optix_includes.h" nogil:
8-
cdef enum OptixDenoiserModelKind:
9-
OPTIX_DENOISER_MODEL_KIND_LDR
10-
OPTIX_DENOISER_MODEL_KIND_HDR
11-
OPTIX_DENOISER_MODEL_KIND_AOV
12-
OPTIX_DENOISER_MODEL_KIND_TEMPORAL
13-
OPTIX_DENOISER_MODEL_KIND_TEMPORAL_AOV
9+
IF _OPTIX_VERSION > 70300:
10+
cdef enum OptixDenoiserModelKind:
11+
OPTIX_DENOISER_MODEL_KIND_LDR
12+
OPTIX_DENOISER_MODEL_KIND_HDR
13+
OPTIX_DENOISER_MODEL_KIND_AOV
14+
OPTIX_DENOISER_MODEL_KIND_TEMPORAL
15+
OPTIX_DENOISER_MODEL_KIND_TEMPORAL_AOV
16+
ELSE:
17+
cdef enum OptixDenoiserModelKind:
18+
OPTIX_DENOISER_MODEL_KIND_LDR
19+
OPTIX_DENOISER_MODEL_KIND_HDR
20+
OPTIX_DENOISER_MODEL_KIND_AOV
21+
OPTIX_DENOISER_MODEL_KIND_TEMPORAL
1422

1523

1624
cdef struct OptixDenoiserOptions:
@@ -57,7 +65,8 @@ cdef extern from "optix_includes.h" nogil:
5765
OptixImage2D normal
5866
OptixImage2D flow
5967

60-
ctypedef uintptr_t OptixDenoiser
68+
ctypedef struct OptixDenoiser:
69+
pass
6170

6271
OptixResult optixDenoiserCreate(OptixDeviceContext context,
6372
OptixDenoiserModelKind modelKind,
@@ -118,18 +127,66 @@ cdef extern from "optix_includes.h" nogil:
118127
CUdeviceptr scratch,
119128
size_t scratchSizeInBytes)
120129

130+
cdef extern from "optix_denoiser_tiling.h" nogil:
131+
OptixResult optixUtilDenoiserInvokeTiled(OptixDenoiser denoiser,
132+
CUstream stream,
133+
const OptixDenoiserParams *params,
134+
CUdeviceptr denoiserState,
135+
size_t denoiserStateSizeInBytes,
136+
const OptixDenoiserGuideLayer *guideLayer,
137+
const OptixDenoiserLayer *layers,
138+
unsigned int numLayers,
139+
CUdeviceptr scratch,
140+
size_t scratchSizeInBytes,
141+
unsigned int overlapWindowSizeInPixels,
142+
unsigned int tileWidth,
143+
unsigned int tileHeight)
144+
145+
cdef struct OptixUtilDenoiserImageTile:
146+
OptixImage2D input
147+
OptixImage2D output
148+
unsigned int inputOffsetX
149+
unsigned int inputOffsetY
150+
151+
OptixResult optixUtilDenoiserSplitImage(const OptixImage2D & input,
152+
const OptixImage2D & output,
153+
unsigned int overlapWindowSizeInPixels, unsigned int tileWidth,
154+
unsigned int tileHeight,
155+
vector[OptixUtilDenoiserImageTile]& tiles)
156+
157+
unsigned int optixUtilGetPixelStride(const OptixImage2D & image)
121158

122-
class Image2D(OptixObject):
159+
160+
161+
cdef class Image2D(OptixObject):
123162
cdef OptixImage2D image
124163
cdef object _d_data
125164

126-
class DenoiserLayer(OptixObject):
165+
cdef class DenoiserLayer(OptixObject):
127166
cdef OptixDenoiserLayer layer
128167
cdef Image2D input
129168
cdef Image2D previous_output
130169
cdef Image2D output
131170

132-
class Denoiser(OptixContextObject):
171+
cdef class DenoiserGuideLayer(OptixObject):
172+
cdef OptixDenoiserGuideLayer layer
173+
cdef Image2D albedo
174+
cdef Image2D normal
175+
cdef Image2D flow
176+
177+
cdef class Denoiser(OptixContextObject):
133178
cdef OptixDenoiser denoiser
179+
cdef object model_kind
134180
cdef bool guide_albedo
135181
cdef bool guide_normals
182+
cdef bool kp_mode
183+
cdef tuple tile_size
184+
cdef object _d_state
185+
cdef size_t _state_size
186+
cdef object _d_scratch
187+
cdef size_t _scratch_size
188+
cdef object _d_window
189+
cdef size_t _window_size
190+
cdef object _d_intensity
191+
cdef object _d_avg_color
192+
cdef unsigned int _overlap

0 commit comments

Comments
 (0)