15# define NVSDK_NGX_HEADER_ONLY
16# include <nvsdk_ngx.h>
17# include <nvsdk_ngx_defs.h>
18# include <nvsdk_ngx_defs_dlssd.h>
23static const int NGX_APPLICATION_ID = 100334311;
30void DLSSDenoiser::CUDATexture::init(
Device *device,
int width,
int height,
int num_components)
32 CUDA_ARRAY_DESCRIPTOR desc = {};
35 desc.Format = CU_AD_FORMAT_FLOAT;
36 desc.NumChannels = num_components;
38 cuda_device_assert(device, cuArrayCreate((CUarray *)&
array, &desc));
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;
46 CUDA_RESOURCE_DESC res_desc = {};
47 res_desc.resType = CU_RESOURCE_TYPE_ARRAY;
48 res_desc.res.array.hArray = (CUarray)
array;
51 device, cuTexObjectCreate((
CUtexObject *)&texture_handle, &res_desc, &tex_desc,
nullptr));
52 cuda_device_assert(device, cuSurfObjectCreate((CUsurfObject *)&surface_handle, &res_desc));
54void DLSSDenoiser::CUDATexture::destroy()
56 cuSurfObjectDestroy((CUsurfObject)surface_handle);
61 cuArrayDestroy((CUarray)
array);
68 CUDADevice *
const cuda_device =
static_cast<CUDADevice *
>(denoiser_device_);
69 const CUDAContextScope scope(cuda_device);
76 const wchar_t *
const app_paths[] = {app_path.c_str()};
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:
88 case NVSDK_NGX_LOGGING_LEVEL_ON:
91 case NVSDK_NGX_LOGGING_LEVEL_VERBOSE:
96 feature_info.LoggingInfo.MinimumLoggingLevel = NVSDK_NGX_LOGGING_LEVEL_ON;
100 ngx_device_ =
new NVSDK_NGX_CUDADevice{
101 cuda_device->cuContext,
static_cast<CUDADeviceQueue *
>(denoiser_queue_.get())->stream()};
103 const NVSDK_NGX_Result
result = NVSDK_NGX_CUDA_Init1(
104 NGX_APPLICATION_ID, app_data_path.c_str(), ngx_device_, &feature_info);
106 if (result == NVSDK_NGX_Result_FAIL_FeatureNotSupported) {
108 ngx_device_ =
nullptr;
109 set_error(
"Failed to load NGX driver");
111 else if (NVSDK_NGX_FAILED(result)) {
112 set_error(
"Failed to initialize NGX driver");
116DLSSDenoiser::~DLSSDenoiser()
118 CUDADevice *
const cuda_device =
static_cast<CUDADevice *
>(denoiser_device_);
119 const CUDAContextScope scope(cuda_device);
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();
130 if (ngx_device_ ==
nullptr) {
134 if (handle_ !=
nullptr) {
135 NVSDK_NGX_CUDA_ReleaseFeature(handle_);
138 const NVSDK_NGX_Result
result = NVSDK_NGX_CUDA_Shutdown1(ngx_device_);
140 if (NVSDK_NGX_FAILED(result)) {
141 set_error(
"Failed to shutdown NGX driver");
147bool DLSSDenoiser::is_device_supported(
const DeviceInfo &device)
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;
163 CUdevice cuDevice = 0;
164 cuDeviceGet(&cuDevice, device.
num);
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();
174 NVSDK_NGX_FeatureRequirement requirement = {NVSDK_NGX_FeatureSupportResult_Supported};
176 const NVSDK_NGX_Result
result = NVSDK_NGX_CUDA_GetFeatureRequirements(
177 cuDevice, &discovery_info, &requirement);
179 if (NVSDK_NGX_SUCCEED(result)) {
180 supported_cache.emplace(device.
num, requirement.FeatureSupported);
181 return requirement.FeatureSupported == NVSDK_NGX_FeatureSupportResult_Supported;
188bool DLSSDenoiser::denoise_create_if_needed(DenoiseContext &context)
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) {
197 CUDADevice *
const cuda_device =
static_cast<CUDADevice *
>(denoiser_device_);
198 const CUDAContextScope scope(cuda_device);
200 if (handle_ !=
nullptr) {
201 denoiser_queue_->synchronize();
203 NVSDK_NGX_CUDA_ReleaseFeature(handle_);
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();
218 if (
context.denoised_buffer_params.width < 32 ||
context.denoised_buffer_params.height < 32) {
224 NVSDK_NGX_Parameter *params =
nullptr;
225 if (NVSDK_NGX_FAILED(NVSDK_NGX_CUDA_AllocateParameters(¶ms))) {
230 params->Set(NVSDK_NGX_Parameter_Width,
context.buffer_params.width);
231 params->Set(NVSDK_NGX_Parameter_Height,
context.buffer_params.height);
233 params->Set(NVSDK_NGX_Parameter_OutWidth,
context.denoised_buffer_params.width);
234 params->Set(NVSDK_NGX_Parameter_OutHeight,
context.denoised_buffer_params.height);
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) {
245 perf_quality_value = NVSDK_NGX_PerfQuality_Value_MaxQuality;
248 perf_quality_value = NVSDK_NGX_PerfQuality_Value_Balanced;
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;
258 params->Set(NVSDK_NGX_Parameter_PerfQualityValue, perf_quality_value);
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);
267 params->Set(NVSDK_NGX_Parameter_DLSS_Roughness_Mode, NVSDK_NGX_DLSS_Roughness_Mode_Packed);
271 params->Set(NVSDK_NGX_Parameter_FreeMemOnReleaseFeature, 1);
273 const NVSDK_NGX_Result
result = NVSDK_NGX_CUDA_CreateFeature1(
274 ngx_device_, NVSDK_NGX_Feature_RayReconstruction, params, &handle_);
276 NVSDK_NGX_CUDA_DestroyParameters(params);
278 if (NVSDK_NGX_FAILED(result)) {
279 set_error(
"Failed to create DLSS instance");
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);
300 cuda_device,
context.denoised_buffer_params.width,
context.denoised_buffer_params.height, 4);
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;
306 return !cuda_device->have_error();
309bool DLSSDenoiser::denoise_configure_if_needed(DenoiseContext & )
314bool DLSSDenoiser::denoise_filter_color_preprocess(
const DenoiseContext &context,
315 const DenoisePass &pass)
317 if (pass.type != get_actual_display_pass_type(
context.buffer_params)) {
327 &
context.render_buffers->buffer.device_pointer,
330 &buffer_params.
width,
335 &pass.denoised_offset,
336 &pass.num_components);
338 return denoiser_queue_->enqueue(
341bool DLSSDenoiser::denoise_filter_color_postprocess(
const DenoiseContext &context,
342 const DenoisePass &pass)
344 if (pass.type != get_actual_display_pass_type(
context.buffer_params)) {
354 &
context.render_buffers->buffer.device_pointer,
357 &buffer_params.
width,
364 &pass.denoised_offset,
366 &pass.num_components,
367 ¶ms_.upscale_factor);
369 return denoiser_queue_->enqueue(
373bool DLSSDenoiser::denoise_filter_guiding_preprocess(DenoiseContext &context)
380 const int pass_specular_albedo =
context.buffer_params.get_pass_offset(
383 const int pass_backward_motion =
context.buffer_params.get_pass_offset(
385 const int pass_specular_motion =
context.buffer_params.get_pass_offset(
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,
400 &
context.pass_denoising_albedo,
401 &pass_specular_albedo,
402 &
context.pass_denoising_normal,
404 &pass_backward_motion,
405 &pass_specular_motion,
408 &buffer_params.
width,
412 return denoiser_queue_->enqueue(
416bool DLSSDenoiser::denoise_run(
const DenoiseContext &context,
const DenoisePass &pass)
418 if (pass.type != get_actual_display_pass_type(
context.buffer_params)) {
422 NVSDK_NGX_Parameter *params =
nullptr;
423 if (NVSDK_NGX_FAILED(NVSDK_NGX_CUDA_AllocateParameters(¶ms))) {
427 CUDADevice *
const cuda_device =
static_cast<CUDADevice *
>(denoiser_device_);
428 const CUDAContextScope scope(cuda_device);
430 params->Set(NVSDK_NGX_Parameter_Reset, 0);
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);
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);
445 params->Set(NVSDK_NGX_Parameter_DLSS_Render_Subrect_Dimensions_Width,
447 params->Set(NVSDK_NGX_Parameter_DLSS_Render_Subrect_Dimensions_Height,
450 params->Set(NVSDK_NGX_Parameter_DLSS_Indicator_Invert_Y_Axis, 1);
452 const NVSDK_NGX_Result
result = NVSDK_NGX_CUDA_EvaluateFeature(handle_, params,
nullptr);
454 NVSDK_NGX_CUDA_DestroyParameters(params);
456 return NVSDK_NGX_SUCCEED(result);
const BufferPass * get_actual_display_pass(PassType type, PassMode mode=PassMode::NOISY) const
NODE_DECLARE PassType type
virtual bool denoise_filter_color_postprocess(const DenoiseContext &context, const DenoisePass &pass)
virtual bool denoise_filter_color_preprocess(const DenoiseContext &context, const DenoisePass &pass)
@ DENOISER_QUALITY_BALANCED
unsigned long long CUtexObject
#define CCL_NAMESPACE_END
#define assert(assertion)
ccl_gpu_kernel_postfix const ccl_global int ccl_global float const int work_size
@ PASS_DENOISING_BACKWARD_MOTION
@ PASS_DENOISING_SPECULAR_MOTION
@ PASS_DENOISING_ROUGHNESS
@ PASS_DENOISING_SPECULAR_ALBEDO
@ DEVICE_KERNEL_FILTER_COLOR_POSTPROCESS_FROM_SURFACE
@ DEVICE_KERNEL_FILTER_COLOR_PREPROCESS_TO_SURFACE
@ DEVICE_KERNEL_FILTER_GUIDING_PREPROCESS_TO_SURFACE
int context(const bContext *C, const char *member, bContextDataResult *result)
string path_cache_get(const string &sub)
string path_get(const string &sub)
wstring string_to_wstring(const string &str)