Blender V5.3
denoiser_dlss.cpp
Go to the documentation of this file.
1/* SPDX-FileCopyrightText: 2025 NVIDIA Corporation
2 * SPDX-FileCopyrightText: 2011-2022 Blender Foundation
3 *
4 * SPDX-License-Identifier: Apache-2.0 */
5
6#ifdef WITH_DLSS
7
10
12
13# include "util/path.h"
14
15# define NVSDK_NGX_HEADER_ONLY
16# include <nvsdk_ngx.h>
17# include <nvsdk_ngx_defs.h>
18# include <nvsdk_ngx_defs_dlssd.h>
19
21
22/* Application ID for Blender from NVIDIA. */
23static const int NGX_APPLICATION_ID = 100334311;
24
25static PassType get_actual_display_pass_type(const BufferParams &buffer_params)
26{
28}
29
30void DLSSDenoiser::CUDATexture::init(Device *device, int width, int height, int num_components)
31{
32 CUDA_ARRAY_DESCRIPTOR desc = {};
33 desc.Width = width;
34 desc.Height = height;
35 desc.Format = CU_AD_FORMAT_FLOAT;
36 desc.NumChannels = num_components;
37
38 cuda_device_assert(device, cuArrayCreate((CUarray *)&array, &desc));
39
40 CUDA_TEXTURE_DESC tex_desc = {};
41 tex_desc.addressMode[0] = CU_TR_ADDRESS_MODE_CLAMP;
42 tex_desc.addressMode[1] = CU_TR_ADDRESS_MODE_CLAMP;
43 tex_desc.addressMode[2] = CU_TR_ADDRESS_MODE_CLAMP;
44 tex_desc.flags = CU_TRSF_NORMALIZED_COORDINATES;
45
46 CUDA_RESOURCE_DESC res_desc = {};
47 res_desc.resType = CU_RESOURCE_TYPE_ARRAY;
48 res_desc.res.array.hArray = (CUarray)array;
49
50 cuda_device_assert(
51 device, cuTexObjectCreate((CUtexObject *)&texture_handle, &res_desc, &tex_desc, nullptr));
52 cuda_device_assert(device, cuSurfObjectCreate((CUsurfObject *)&surface_handle, &res_desc));
53}
54void DLSSDenoiser::CUDATexture::destroy()
55{
56 cuSurfObjectDestroy((CUsurfObject)surface_handle);
57 surface_handle = 0;
58 cuTexObjectDestroy((CUtexObject)texture_handle);
59 texture_handle = 0;
60
61 cuArrayDestroy((CUarray)array);
62 array = 0;
63}
64
65DLSSDenoiser::DLSSDenoiser(Device *denoiser_device, const DenoiseParams &params)
66 : DenoiserGPU(denoiser_device, params)
67{
68 CUDADevice *const cuda_device = static_cast<CUDADevice *>(denoiser_device_);
69 const CUDAContextScope scope(cuda_device);
70
71 /* Path to optionally search for features, in addition to the executable directory and driver. */
72 const wstring app_path = string_to_wstring(path_get("ngx"));
73 /* Path to write NGX logs to. */
74 const wstring app_data_path = string_to_wstring(path_cache_get());
75
76 const wchar_t *const app_paths[] = {app_path.c_str()};
77
78 NVSDK_NGX_FeatureCommonInfo feature_info = {};
79 feature_info.PathListInfo.Path = app_paths;
80 feature_info.PathListInfo.Length = 1;
81 feature_info.LoggingInfo.LoggingCallback =
82 [](const char *message, NVSDK_NGX_Logging_Level loggingLevel, NVSDK_NGX_Feature) {
83 switch (loggingLevel) {
84 case NVSDK_NGX_LOGGING_LEVEL_OFF:
85 case NVSDK_NGX_LOGGING_LEVEL_NUM:
86 assert(false);
87 break;
88 case NVSDK_NGX_LOGGING_LEVEL_ON:
89 LOG_INFO << message;
90 break;
91 case NVSDK_NGX_LOGGING_LEVEL_VERBOSE:
92 LOG_INFO << message;
93 break;
94 }
95 };
96 feature_info.LoggingInfo.MinimumLoggingLevel = NVSDK_NGX_LOGGING_LEVEL_ON;
97
98 /* Create a fixed handle for this particular CUDA context and queue combination, since NGX uses
99 * it for lookup internally. */
100 ngx_device_ = new NVSDK_NGX_CUDADevice{
101 cuda_device->cuContext, static_cast<CUDADeviceQueue *>(denoiser_queue_.get())->stream()};
102
103 const NVSDK_NGX_Result result = NVSDK_NGX_CUDA_Init1(
104 NGX_APPLICATION_ID, app_data_path.c_str(), ngx_device_, &feature_info);
105
106 if (result == NVSDK_NGX_Result_FAIL_FeatureNotSupported) {
107 delete ngx_device_;
108 ngx_device_ = nullptr;
109 set_error("Failed to load NGX driver");
110 }
111 else if (NVSDK_NGX_FAILED(result)) {
112 set_error("Failed to initialize NGX driver");
113 }
114}
115
116DLSSDenoiser::~DLSSDenoiser()
117{
118 CUDADevice *const cuda_device = static_cast<CUDADevice *>(denoiser_device_);
119 const CUDAContextScope scope(cuda_device);
120
121 tex_color_.destroy();
122 tex_depth_.destroy();
123 tex_diffuse_albedo_.destroy();
124 tex_specular_albedo_.destroy();
125 tex_normal_roughness_.destroy();
126 tex_motion_.destroy();
127 tex_specular_motion_.destroy();
128 tex_output_.destroy();
129
130 if (ngx_device_ == nullptr) {
131 return;
132 }
133
134 if (handle_ != nullptr) {
135 NVSDK_NGX_CUDA_ReleaseFeature(handle_);
136 }
137
138 const NVSDK_NGX_Result result = NVSDK_NGX_CUDA_Shutdown1(ngx_device_);
139
140 if (NVSDK_NGX_FAILED(result)) {
141 set_error("Failed to shutdown NGX driver");
142 }
143
144 delete ngx_device_;
145}
146
147bool DLSSDenoiser::is_device_supported(const DeviceInfo &device)
148{
149 if (device.type != DEVICE_CUDA && device.type != DEVICE_OPTIX) {
150 return false;
151 }
152
153 /* 'NVSDK_NGX_CUDA_GetFeatureRequirements' is an expensive call, so cache the result (since
154 * 'is_device_supported' is called a lot). */
155 static ccl::map<int, NVSDK_NGX_Feature_Support_Result> supported_cache;
156 if (const auto it = supported_cache.find(device.num); it != supported_cache.end()) {
157 return it->second == NVSDK_NGX_FeatureSupportResult_Supported;
158 }
159
160 /* Path to write NGX logs to. */
161 const wstring app_data_path = string_to_wstring(path_cache_get());
162
163 CUdevice cuDevice = 0;
164 cuDeviceGet(&cuDevice, device.num);
165
166 /* Query driver whether the DLSS-RR feature is supported on this particular device. */
167 NVSDK_NGX_FeatureDiscoveryInfo discovery_info = {};
168 discovery_info.SDKVersion = NVSDK_NGX_Version_API;
169 discovery_info.FeatureID = NVSDK_NGX_Feature_RayReconstruction;
170 discovery_info.Identifier.IdentifierType = NVSDK_NGX_Application_Identifier_Type_Application_Id;
171 discovery_info.Identifier.v.ApplicationId = NGX_APPLICATION_ID;
172 discovery_info.ApplicationDataPath = app_data_path.c_str();
173
174 NVSDK_NGX_FeatureRequirement requirement = {NVSDK_NGX_FeatureSupportResult_Supported};
175
176 const NVSDK_NGX_Result result = NVSDK_NGX_CUDA_GetFeatureRequirements(
177 cuDevice, &discovery_info, &requirement);
178
179 if (NVSDK_NGX_SUCCEED(result)) {
180 supported_cache.emplace(device.num, requirement.FeatureSupported);
181 return requirement.FeatureSupported == NVSDK_NGX_FeatureSupportResult_Supported;
182 }
183 else {
184 return false;
185 }
186}
187
188bool DLSSDenoiser::denoise_create_if_needed(DenoiseContext &context)
189{
190 const bool recreate_denoiser = last_width_ != context.denoised_buffer_params.width ||
191 last_height_ != context.denoised_buffer_params.height ||
192 last_upscale_factor_ != context.denoise_params.upscale_factor;
193 if (handle_ != nullptr && !recreate_denoiser) {
194 return true;
195 }
196
197 CUDADevice *const cuda_device = static_cast<CUDADevice *>(denoiser_device_);
198 const CUDAContextScope scope(cuda_device);
199
200 if (handle_ != nullptr) {
201 denoiser_queue_->synchronize();
202
203 NVSDK_NGX_CUDA_ReleaseFeature(handle_);
204 handle_ = nullptr;
205 }
206
207 tex_color_.destroy();
208 tex_depth_.destroy();
209 tex_diffuse_albedo_.destroy();
210 tex_specular_albedo_.destroy();
211 tex_normal_roughness_.destroy();
212 tex_motion_.destroy();
213 tex_specular_motion_.destroy();
214 tex_output_.destroy();
215
216 /* Feature creation fails below these dimensions.
217 * Avoid hard error that stops rendering and only disable denoising for very small viewports. */
218 if (context.denoised_buffer_params.width < 32 || context.denoised_buffer_params.height < 32) {
219 last_width_ = 0;
220 last_height_ = 0;
221 return false;
222 }
223
224 NVSDK_NGX_Parameter *params = nullptr;
225 if (NVSDK_NGX_FAILED(NVSDK_NGX_CUDA_AllocateParameters(&params))) {
226 return false;
227 }
228
229 /* Input params (with resolution divider applied). */
230 params->Set(NVSDK_NGX_Parameter_Width, context.buffer_params.width);
231 params->Set(NVSDK_NGX_Parameter_Height, context.buffer_params.height);
232 /* Output params. */
233 params->Set(NVSDK_NGX_Parameter_OutWidth, context.denoised_buffer_params.width);
234 params->Set(NVSDK_NGX_Parameter_OutHeight, context.denoised_buffer_params.height);
235
236 /* Usually the DLSS quality mode is set first and then DLSS should be queried for the optimal
237 * upscale factor to go along with it. In Cycles the upscale factor is already determined before
238 * the denoiser is initialized though, so we do it backwards and instead try to find a
239 * reasonable quality mode to match the denoiser settings here. */
240 NVSDK_NGX_PerfQuality_Value perf_quality_value = NVSDK_NGX_PerfQuality_Value_DLAA;
241 if (context.denoise_params.upscale_factor > 1) {
242 switch (context.denoise_params.quality) {
243 default:
245 perf_quality_value = NVSDK_NGX_PerfQuality_Value_MaxQuality;
246 break;
248 perf_quality_value = NVSDK_NGX_PerfQuality_Value_Balanced;
249 break;
251 perf_quality_value = NVSDK_NGX_PerfQuality_Value_MaxPerf;
252 if (context.denoise_params.upscale_factor >= 3) {
253 perf_quality_value = NVSDK_NGX_PerfQuality_Value_UltraPerformance;
254 }
255 break;
256 }
257 }
258 params->Set(NVSDK_NGX_Parameter_PerfQualityValue, perf_quality_value);
259
260 params->Set(NVSDK_NGX_Parameter_DLSS_Denoise_Mode, NVSDK_NGX_DLSS_Denoise_Mode_DLUnified);
261 params->Set(NVSDK_NGX_Parameter_DLSS_Feature_Create_Flags,
262 NVSDK_NGX_DLSS_Feature_Flags_IsHDR | NVSDK_NGX_DLSS_Feature_Flags_MVLowRes |
263 NVSDK_NGX_DLSS_Feature_Flags_AlphaUpscaling);
264 params->Set(NVSDK_NGX_Parameter_DLSS_Enable_Output_Subrects, 0);
265 params->Set(NVSDK_NGX_Parameter_Use_HW_Depth, NVSDK_NGX_DLSS_Depth_Type_Linear);
266 /* Normals and roughness are packed into one texture in 'denoise_filter_guiding_preprocess'. */
267 params->Set(NVSDK_NGX_Parameter_DLSS_Roughness_Mode, NVSDK_NGX_DLSS_Roughness_Mode_Packed);
268
269 /* Free internal resources immediately on release instead of doing delayed destruction. This is
270 * safe to do since the denoiser queue is synchronized before release above. */
271 params->Set(NVSDK_NGX_Parameter_FreeMemOnReleaseFeature, 1);
272
273 const NVSDK_NGX_Result result = NVSDK_NGX_CUDA_CreateFeature1(
274 ngx_device_, NVSDK_NGX_Feature_RayReconstruction, params, &handle_);
275
276 NVSDK_NGX_CUDA_DestroyParameters(params);
277
278 if (NVSDK_NGX_FAILED(result)) {
279 set_error("Failed to create DLSS instance");
280 return false;
281 }
282
283 /* DLSS requires inputs and outputs in separate CUDA textures/surfaces, while Cycles stores them
284 * in an interleaved buffer, so need to create these temporary textures and convert between the
285 * two storage variants (in 'denoise_filter_color_preprocess',
286 * 'denoise_filter_guiding_preprocess' and 'denoise_filter_color_postprocess'). */
287 tex_color_.init(cuda_device, context.buffer_params.width, context.buffer_params.height, 4);
288 tex_depth_.init(cuda_device, context.buffer_params.width, context.buffer_params.height, 1);
289 tex_diffuse_albedo_.init(
290 cuda_device, context.buffer_params.width, context.buffer_params.height, 4);
291 tex_specular_albedo_.init(
292 cuda_device, context.buffer_params.width, context.buffer_params.height, 4);
293 tex_normal_roughness_.init(
294 cuda_device, context.buffer_params.width, context.buffer_params.height, 4);
295 tex_motion_.init(cuda_device, context.buffer_params.width, context.buffer_params.height, 2);
296 tex_specular_motion_.init(
297 cuda_device, context.buffer_params.width, context.buffer_params.height, 2);
298
299 tex_output_.init(
300 cuda_device, context.denoised_buffer_params.width, context.denoised_buffer_params.height, 4);
301
302 last_width_ = context.denoised_buffer_params.width;
303 last_height_ = context.denoised_buffer_params.height;
304 last_upscale_factor_ = context.denoise_params.upscale_factor;
305
306 return !cuda_device->have_error();
307}
308
309bool DLSSDenoiser::denoise_configure_if_needed(DenoiseContext & /*context*/)
310{
311 return true;
312}
313
314bool DLSSDenoiser::denoise_filter_color_preprocess(const DenoiseContext &context,
315 const DenoisePass &pass)
316{
317 if (pass.type != get_actual_display_pass_type(context.buffer_params)) {
319 }
320
321 /* Input params (with resolution divider applied). */
322 const BufferParams &buffer_params = context.buffer_params;
323
324 const int work_size = buffer_params.width * buffer_params.height;
325
326 const DeviceKernelArguments args(&tex_color_.surface_handle,
327 &context.render_buffers->buffer.device_pointer,
328 &buffer_params.full_x,
329 &buffer_params.full_y,
330 &buffer_params.width,
331 &buffer_params.height,
332 &buffer_params.offset,
333 &buffer_params.stride,
334 &buffer_params.pass_stride,
335 &pass.denoised_offset,
336 &pass.num_components);
337
338 return denoiser_queue_->enqueue(
340}
341bool DLSSDenoiser::denoise_filter_color_postprocess(const DenoiseContext &context,
342 const DenoisePass &pass)
343{
344 if (pass.type != get_actual_display_pass_type(context.buffer_params)) {
346 }
347
348 /* Output params. */
349 const BufferParams &buffer_params = context.denoised_buffer_params;
350
351 const int work_size = buffer_params.width * buffer_params.height;
352
353 const DeviceKernelArguments args(&tex_output_.surface_handle,
354 &context.render_buffers->buffer.device_pointer,
355 &buffer_params.full_x,
356 &buffer_params.full_y,
357 &buffer_params.width,
358 &buffer_params.height,
359 &buffer_params.offset,
360 &buffer_params.stride,
361 &buffer_params.pass_stride,
362 &context.num_samples,
363 &pass.noisy_offset,
364 &pass.denoised_offset,
365 &context.pass_sample_count,
366 &pass.num_components,
367 &params_.upscale_factor);
368
369 return denoiser_queue_->enqueue(
371}
372
373bool DLSSDenoiser::denoise_filter_guiding_preprocess(DenoiseContext &context)
374{
375 const BufferParams &buffer_params = context.buffer_params;
376
377 const int work_size = buffer_params.width * buffer_params.height;
378
379 const int pass_depth = context.buffer_params.get_pass_offset(PASS_DENOISING_DEPTH);
380 const int pass_specular_albedo = context.buffer_params.get_pass_offset(
382 const int pass_roughness = context.buffer_params.get_pass_offset(PASS_DENOISING_ROUGHNESS);
383 const int pass_backward_motion = context.buffer_params.get_pass_offset(
385 const int pass_specular_motion = context.buffer_params.get_pass_offset(
387
388 const DeviceKernelArguments args(&tex_depth_.surface_handle,
389 &tex_diffuse_albedo_.surface_handle,
390 &tex_specular_albedo_.surface_handle,
391 &tex_normal_roughness_.surface_handle,
392 &tex_motion_.surface_handle,
393 &tex_specular_motion_.surface_handle,
394 &context.render_buffers->buffer.device_pointer,
395 &buffer_params.offset,
396 &buffer_params.stride,
397 &buffer_params.pass_stride,
398 &context.pass_sample_count,
399 &pass_depth,
400 &context.pass_denoising_albedo,
401 &pass_specular_albedo,
402 &context.pass_denoising_normal,
403 &pass_roughness,
404 &pass_backward_motion,
405 &pass_specular_motion,
406 &buffer_params.full_x,
407 &buffer_params.full_y,
408 &buffer_params.width,
409 &buffer_params.height,
410 &context.num_samples);
411
412 return denoiser_queue_->enqueue(
414}
415
416bool DLSSDenoiser::denoise_run(const DenoiseContext &context, const DenoisePass &pass)
417{
418 if (pass.type != get_actual_display_pass_type(context.buffer_params)) {
419 return true;
420 }
421
422 NVSDK_NGX_Parameter *params = nullptr;
423 if (NVSDK_NGX_FAILED(NVSDK_NGX_CUDA_AllocateParameters(&params))) {
424 return false;
425 }
426
427 CUDADevice *const cuda_device = static_cast<CUDADevice *>(denoiser_device_);
428 const CUDAContextScope scope(cuda_device);
429
430 params->Set(NVSDK_NGX_Parameter_Reset, 0);
431
432 params->Set(NVSDK_NGX_Parameter_Jitter_Offset_X, context.pixel_jitter.x);
433 params->Set(NVSDK_NGX_Parameter_Jitter_Offset_Y, context.pixel_jitter.y);
434
435 params->Set(NVSDK_NGX_Parameter_Color, &tex_color_.texture_handle);
436 params->Set(NVSDK_NGX_Parameter_Depth, &tex_depth_.texture_handle);
437 params->Set(NVSDK_NGX_Parameter_DiffuseAlbedo, &tex_diffuse_albedo_.texture_handle);
438 params->Set(NVSDK_NGX_Parameter_SpecularAlbedo, &tex_specular_albedo_.texture_handle);
439 params->Set(NVSDK_NGX_Parameter_GBuffer_Normals, &tex_normal_roughness_.texture_handle);
440 params->Set(NVSDK_NGX_Parameter_GBuffer_Roughness, &tex_normal_roughness_.texture_handle);
441 params->Set(NVSDK_NGX_Parameter_MotionVectors, &tex_motion_.texture_handle);
442 params->Set(NVSDK_NGX_Parameter_GBuffer_SpecularMvec, &tex_specular_motion_.texture_handle);
443 params->Set(NVSDK_NGX_Parameter_Output, &tex_output_.surface_handle);
444
445 params->Set(NVSDK_NGX_Parameter_DLSS_Render_Subrect_Dimensions_Width,
446 context.buffer_params.width);
447 params->Set(NVSDK_NGX_Parameter_DLSS_Render_Subrect_Dimensions_Height,
448 context.buffer_params.height);
449
450 params->Set(NVSDK_NGX_Parameter_DLSS_Indicator_Invert_Y_Axis, 1);
451
452 const NVSDK_NGX_Result result = NVSDK_NGX_CUDA_EvaluateFeature(handle_, params, nullptr);
453
454 NVSDK_NGX_CUDA_DestroyParameters(params);
455
456 return NVSDK_NGX_SUCCEED(result);
457}
458
460
461#endif
int pass_stride
Definition buffers.h:92
NODE_DECLARE int width
Definition buffers.h:70
const BufferPass * get_actual_display_pass(PassType type, PassMode mode=PassMode::NOISY) const
Definition buffers.cpp:196
NODE_DECLARE PassType type
Definition buffers.h:28
virtual bool denoise_filter_color_postprocess(const DenoiseContext &context, const DenoisePass &pass)
virtual bool denoise_filter_color_preprocess(const DenoiseContext &context, const DenoisePass &pass)
DeviceType type
@ DENOISER_QUALITY_FAST
Definition denoise.h:58
@ DENOISER_QUALITY_BALANCED
Definition denoise.h:57
@ DENOISER_QUALITY_HIGH
Definition denoise.h:56
unsigned long long CUtexObject
#define CCL_NAMESPACE_END
@ DEVICE_CUDA
@ DEVICE_OPTIX
#define assert(assertion)
ccl_gpu_kernel_postfix const ccl_global int ccl_global float const int work_size
PassType
@ PASS_COMBINED
@ PASS_DENOISING_BACKWARD_MOTION
@ PASS_DENOISING_SPECULAR_MOTION
@ PASS_DENOISING_ROUGHNESS
@ PASS_DENOISING_SPECULAR_ALBEDO
@ PASS_DENOISING_DEPTH
@ DEVICE_KERNEL_FILTER_COLOR_POSTPROCESS_FROM_SURFACE
@ DEVICE_KERNEL_FILTER_COLOR_PREPROCESS_TO_SURFACE
@ DEVICE_KERNEL_FILTER_GUIDING_PREPROCESS_TO_SURFACE
#define LOG_INFO
Definition log.h:106
int context(const bContext *C, const char *member, bContextDataResult *result)
@ DENOISED
Definition pass.h:22
string path_cache_get(const string &sub)
Definition path.cpp:383
string path_get(const string &sub)
Definition path.cpp:368
wstring string_to_wstring(const string &str)
Definition string.cpp:261