Blender V5.3
denoiser_optix.cpp
Go to the documentation of this file.
1/* SPDX-FileCopyrightText: 2011-2022 Blender Foundation
2 *
3 * SPDX-License-Identifier: Apache-2.0 */
4
5#ifdef WITH_OPTIX
6
9
11# include "device/optix/queue.h"
12
13# include <optix_denoiser_tiling.h>
14
16
17OptiXDenoiser::OptiXDenoiser(Device *denoiser_device, const DenoiseParams &params)
18 : DenoiserGPU(denoiser_device, params), state_(denoiser_device, "__denoiser_state", true)
19{
20}
21
22OptiXDenoiser::~OptiXDenoiser()
23{
24 /* It is important that the OptixDenoiser handle is destroyed before the OptixDeviceContext
25 * handle, which is guaranteed since the local denoising device owning the OptiX device context
26 * is deleted as part of the Denoiser class destructor call after this. */
27 if (optix_denoiser_ != nullptr) {
28 optixDenoiserDestroy(optix_denoiser_);
29 }
30}
31
32bool OptiXDenoiser::is_device_supported(const DeviceInfo &device)
33{
34 if (device.type == DEVICE_OPTIX) {
35 return device.denoisers & DENOISER_OPTIX;
36 }
37 return false;
38}
39
40bool OptiXDenoiser::denoise_buffer(const BufferParams &buffer_params,
41 const BufferParams &denoised_buffer_params,
42 RenderBuffers *render_buffers,
43 const int num_samples,
44 const bool allow_inplace_modification,
45 const float2 pixel_jitter)
46{
47 OptiXDevice *const optix_device = static_cast<OptiXDevice *>(denoiser_device_);
48 const CUDAContextScope scope(optix_device);
49
50 return DenoiserGPU::denoise_buffer(buffer_params,
51 denoised_buffer_params,
52 render_buffers,
53 num_samples,
54 allow_inplace_modification,
55 pixel_jitter);
56}
57
58bool OptiXDenoiser::denoise_create_if_needed(DenoiseContext &context)
59{
60 const bool use_pass_albedo = (context.denoise_params.passes & DENOISER_PASS_ALBEDO) != 0;
61 const bool use_pass_normal = (context.denoise_params.passes & DENOISER_PASS_NORMAL) != 0;
62 const bool use_pass_motion = context.denoise_params.temporally_stable &&
63 context.guiding_params.pass_flow != PASS_UNUSED;
64 const bool use_upscale_model = context.denoise_params.upscale_factor == 2.0f;
65
66 const bool recreate_denoiser = (optix_denoiser_ == nullptr) ||
67 (use_pass_albedo_ != use_pass_albedo) ||
68 (use_pass_normal_ != use_pass_normal) ||
69 (use_pass_motion_ != use_pass_motion) ||
70 (use_upscale_model_ != use_upscale_model);
71 if (!recreate_denoiser) {
72 return true;
73 }
74
75 /* Destroy existing handle before creating new one. */
76 if (optix_denoiser_) {
77 optixDenoiserDestroy(optix_denoiser_);
78 }
79
80 /* Create OptiX denoiser handle on demand when it is first used. */
81 OptixDenoiserModelKind model;
82 if (use_pass_motion) {
83 if (use_upscale_model) {
84 model = OPTIX_DENOISER_MODEL_KIND_TEMPORAL_UPSCALE2X;
85 }
86 else {
87 model = OPTIX_DENOISER_MODEL_KIND_TEMPORAL;
88 }
89 }
90 else {
91 if (use_upscale_model) {
92 model = OPTIX_DENOISER_MODEL_KIND_UPSCALE2X;
93 }
94 else {
95 model = OPTIX_DENOISER_MODEL_KIND_AOV;
96 }
97 }
98
99 OptixDenoiserOptions denoiser_options = {};
100 denoiser_options.guideAlbedo = use_pass_albedo;
101 denoiser_options.guideNormal = use_pass_normal;
102 denoiser_options.denoiseAlpha = OPTIX_DENOISER_ALPHA_MODE_COPY;
103
104 const OptixResult result = optixDenoiserCreate(
105 static_cast<OptiXDevice *>(denoiser_device_)->context,
106 model,
107 &denoiser_options,
108 &optix_denoiser_);
109
110 if (result != OPTIX_SUCCESS) {
111 set_error("Failed to create OptiX denoiser");
112 return false;
113 }
114
115 /* OptiX denoiser handle was created with the requested number of input passes. */
116 use_pass_albedo_ = use_pass_albedo;
117 use_pass_normal_ = use_pass_normal;
118 use_pass_motion_ = use_pass_motion;
119 use_upscale_model_ = use_upscale_model;
120
121 /* OptiX denoiser has been created, but it needs configuration. */
122 is_configured_ = false;
123
124 return true;
125}
126
127bool OptiXDenoiser::denoise_configure_if_needed(DenoiseContext &context)
128{
129 /* Limit maximum tile size denoiser can be invoked with. */
130 const int2 tile_size = make_int2(min(context.buffer_params.width, 4096),
131 min(context.buffer_params.height, 4096));
132
133 if (is_configured_ && (configured_size_.x == tile_size.x && configured_size_.y == tile_size.y)) {
134 return true;
135 }
136
137 optix_device_assert(
138 denoiser_device_,
139 optixDenoiserComputeMemoryResources(optix_denoiser_, tile_size.x, tile_size.y, &sizes_));
140
141 const bool tiled = tile_size.x < context.buffer_params.width ||
142 tile_size.y < context.buffer_params.height;
143
144 /* Allocate denoiser state if tile size has changed since last setup. */
145 state_.device = denoiser_device_;
146 state_.alloc_to_device(sizes_.stateSizeInBytes + sizes_.withOverlapScratchSizeInBytes);
147
148 /* Initialize denoiser state for the current tile size. */
149 const OptixResult result = optixDenoiserSetup(
150 optix_denoiser_,
151 0, /* Work around bug in r495 drivers that causes artifacts when denoiser setup is called
152 * on a stream that is not the default stream. */
153 tile_size.x + (tiled ? sizes_.overlapWindowSizeInPixels * 2 : 0),
154 tile_size.y + (tiled ? sizes_.overlapWindowSizeInPixels * 2 : 0),
155 state_.device_pointer,
156 sizes_.stateSizeInBytes,
157 state_.device_pointer + sizes_.stateSizeInBytes,
158 sizes_.withOverlapScratchSizeInBytes);
159 if (result != OPTIX_SUCCESS) {
160 set_error("Failed to set up OptiX denoiser");
161 return false;
162 }
163
164 cuda_device_assert(denoiser_device_, cuCtxSynchronize());
165
166 is_configured_ = true;
167 configured_size_ = tile_size;
168
169 return true;
170}
171
172bool OptiXDenoiser::denoise_run(const DenoiseContext &context, const DenoisePass &pass)
173{
174 /* Set up input and output layer information. */
175 OptixImage2D color_layer = {0};
176 OptixImage2D albedo_layer = {0};
177 OptixImage2D normal_layer = {0};
178 OptixImage2D flow_layer = {0};
179
180 OptixImage2D output_layer = {0};
181 OptixImage2D prev_output_layer = {0};
182
183 /* Color pass. */
184 {
185 const int pass_denoised = pass.denoised_offset;
186 const int64_t pass_stride_in_bytes = context.buffer_params.pass_stride * sizeof(float);
187
188 color_layer.data = context.render_buffers->buffer.device_pointer +
189 pass_denoised * sizeof(float);
190 color_layer.width = context.buffer_params.width;
191 color_layer.height = context.buffer_params.height;
192 color_layer.rowStrideInBytes = pass_stride_in_bytes * context.buffer_params.stride;
193 color_layer.pixelStrideInBytes = pass_stride_in_bytes;
194 color_layer.format = pass.num_components > 3 && use_upscale_model_ ?
195 OPTIX_PIXEL_FORMAT_FLOAT4 :
196 OPTIX_PIXEL_FORMAT_FLOAT3;
197 }
198
199 /* Previous output. */
200 if (use_pass_motion_ && context.prev_output.offset != PASS_UNUSED) {
201 const int64_t pass_stride_in_bytes = context.prev_output.pass_stride * sizeof(float);
202
203 prev_output_layer.data = context.prev_output.device_pointer +
204 context.prev_output.offset * sizeof(float);
205 prev_output_layer.width = context.denoised_buffer_params.width;
206 prev_output_layer.height = context.denoised_buffer_params.height;
207 prev_output_layer.rowStrideInBytes = pass_stride_in_bytes * context.prev_output.stride;
208 prev_output_layer.pixelStrideInBytes = pass_stride_in_bytes;
209 prev_output_layer.format = OPTIX_PIXEL_FORMAT_FLOAT3;
210 }
211
212 /* Optional albedo and color passes. */
213 const device_ptr d_guiding_buffer = context.guiding_params.device_pointer;
214 const int64_t pixel_stride_in_bytes = context.guiding_params.pass_stride * sizeof(float);
215 const int64_t row_stride_in_bytes = context.guiding_params.stride * pixel_stride_in_bytes;
216
217 if (use_pass_albedo_) {
218 albedo_layer.data = d_guiding_buffer + context.guiding_params.pass_albedo * sizeof(float);
219 albedo_layer.width = context.buffer_params.width;
220 albedo_layer.height = context.buffer_params.height;
221 albedo_layer.rowStrideInBytes = row_stride_in_bytes;
222 albedo_layer.pixelStrideInBytes = pixel_stride_in_bytes;
223 albedo_layer.format = OPTIX_PIXEL_FORMAT_FLOAT3;
224 }
225
226 if (use_pass_normal_) {
227 normal_layer.data = d_guiding_buffer + context.guiding_params.pass_normal * sizeof(float);
228 normal_layer.width = context.buffer_params.width;
229 normal_layer.height = context.buffer_params.height;
230 normal_layer.rowStrideInBytes = row_stride_in_bytes;
231 normal_layer.pixelStrideInBytes = pixel_stride_in_bytes;
232 normal_layer.format = OPTIX_PIXEL_FORMAT_FLOAT3;
233 }
234
235 if (use_pass_motion_) {
236 flow_layer.data = d_guiding_buffer + context.guiding_params.pass_flow * sizeof(float);
237 flow_layer.width = context.buffer_params.width;
238 flow_layer.height = context.buffer_params.height;
239 flow_layer.rowStrideInBytes = row_stride_in_bytes;
240 flow_layer.pixelStrideInBytes = pixel_stride_in_bytes;
241 flow_layer.format = OPTIX_PIXEL_FORMAT_FLOAT2;
242 }
243
244 /* Denoise in-place of the noisy input in the render buffers.
245 * TODO: This is not valid for alpha with OPTIX_DENOISER_MODEL_KIND_UPSCALE2X +
246 * OPTIX_DENOISER_ALPHA_MODE_COPY, since concurrent alpha reads/writes overlap. */
247 {
248 output_layer = color_layer;
249 output_layer.width = context.denoised_buffer_params.width;
250 output_layer.height = context.denoised_buffer_params.height;
251 output_layer.rowStrideInBytes = output_layer.pixelStrideInBytes *
252 context.denoised_buffer_params.stride;
253 }
254
255 OptixDenoiserGuideLayer guide_layers = {};
256 guide_layers.albedo = albedo_layer;
257 guide_layers.normal = normal_layer;
258 guide_layers.flow = flow_layer;
259
260 OptixDenoiserLayer image_layers = {};
261 image_layers.input = color_layer;
262 image_layers.previousOutput = prev_output_layer;
263 image_layers.output = output_layer;
264
265 /* Finally run denoising. */
266 OptixDenoiserParams params = {}; /* All parameters are disabled/zero. */
267
268 optix_device_assert(denoiser_device_,
269 optixUtilDenoiserInvokeTiled(
270 optix_denoiser_,
271 static_cast<OptiXDeviceQueue *>(denoiser_queue_.get())->stream(),
272 &params,
273 state_.device_pointer,
274 sizes_.stateSizeInBytes,
275 &guide_layers,
276 &image_layers,
277 1,
278 state_.device_pointer + sizes_.stateSizeInBytes,
279 sizes_.withOverlapScratchSizeInBytes,
280 sizes_.overlapWindowSizeInPixels,
281 configured_size_.x,
282 configured_size_.y));
283
284 return true;
285}
286
288
289#endif
long long int int64_t
bool denoise_buffer(const BufferParams &buffer_params, const BufferParams &denoised_buffer_params, RenderBuffers *render_buffers, int num_samples, bool allow_inplace_modification, float2 pixel_jitter) override
DenoiserTypeMask denoisers
DeviceType type
@ DENOISER_PASS_NORMAL
Definition denoise.h:30
@ DENOISER_PASS_ALBEDO
Definition denoise.h:28
@ DENOISER_OPTIX
Definition denoise.h:12
#define PASS_UNUSED
#define CCL_NAMESPACE_END
@ DEVICE_OPTIX
ccl_device_forceinline int2 make_int2(const int x, const int y)
int context(const bContext *C, const char *member, bContextDataResult *result)
KERNEL_FEATURE_LIGHT_PASSES KERNEL_FEATURE_PATH_GUIDING float
#define min(a, b)
Definition sort.cc:38
uint64_t device_ptr
Definition types_base.h:44