nvidia-dlss 0.1.0

Vulkan bindings for NVIDIA DLSS Ray Reconstruction through NGX
Documentation
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
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
#[cfg(feature = "vk-graph")]
use vk_graph::driver::{device::Device, instance::Instance};
use {
    crate::{
        DLSS_RUNTIME_FILE, DlssRrConfig, DlssRrFrameConstants, DlssRrQuality, DlssRrResources,
        IdentityKind, InitInfo, NativeDlssRrResources, VulkanInitInfo,
    },
    anyhow::Context as _,
    ash::vk::{self, Handle as _},
    std::{
        ffi::{CStr, CString, c_char, c_void},
        os::unix::ffi::OsStrExt as _,
        path::Path,
        ptr::NonNull,
        sync::{
            Arc, Mutex,
            atomic::{AtomicPtr, Ordering},
        },
    },
};

const MAX_EXTENSIONS: usize = 32;
const MAX_EXTENSION_NAME_SIZE: usize = 256;

static ACTIVE_CONTEXT: AtomicPtr<NativeContext> = AtomicPtr::new(std::ptr::null_mut());
static API_LOCK: Mutex<()> = Mutex::new(());

#[derive(Clone)]
pub struct Evaluator {
    inner: Arc<EvaluatorInner>,
}

impl Evaluator {
    #[must_use]
    pub fn is_active(&self) -> bool {
        !self.inner.context.load(Ordering::SeqCst).is_null()
    }

    /// # Errors
    /// Returns an error for zero dimensions, a shut-down evaluator, or an NGX query failure.
    pub fn optimal_render_extent(
        &self,
        output_extent: [u32; 2],
        quality: DlssRrQuality,
    ) -> anyhow::Result<[u32; 2]> {
        anyhow::ensure!(
            output_extent[0] > 0 && output_extent[1] > 0,
            "zero output extent"
        );

        let _guard = API_LOCK
            .lock()
            .unwrap_or_else(std::sync::PoisonError::into_inner);
        let context = self.inner.context.load(Ordering::SeqCst);

        anyhow::ensure!(!context.is_null(), "ngx evaluator has been shut down");

        let mut output = NativeExtent {
            width: 0,
            height: 0,
        };

        let result = unsafe {
            nvidia_dlss_ngx_optimal_settings(
                context,
                output_extent[0],
                output_extent[1],
                quality as u32,
                &raw mut output,
            )
        };

        check_result(result).context("querying ngx optimal render extent")?;

        anyhow::ensure!(output.width > 0 && output.height > 0, "zero render extent");

        Ok([output.width, output.height])
    }

    /// # Safety
    ///
    /// This evaluator and its owning NGX context must remain valid until execution completes. The
    /// command buffer must be recording on the context's device; every image and view must belong
    /// to that device, and each declared layout must match its actual state. Image ranges,
    /// formats, and extents must describe their views accurately. Before evaluation,
    /// transition all inputs (including depth and the reflection guide) to
    /// `SHADER_READ_ONLY_OPTIMAL` and the output to `GENERAL`. Evaluation leaves these
    /// layouts unchanged; this wrapper does not record layout transitions. All handles must
    /// remain valid until GPU execution completes. If dimensions or quality change, prior
    /// evaluations must have completed first.
    ///
    /// # Errors
    /// Returns an error for a null command buffer, invalid resource declarations,
    /// a shut-down evaluator, or an NGX creation/evaluation failure.
    pub unsafe fn evaluate(
        &self,
        command_buffer: vk::CommandBuffer,
        frame: &DlssRrFrameConstants,
        resources: &DlssRrResources,
        quality: DlssRrQuality,
    ) -> anyhow::Result<()> {
        anyhow::ensure!(
            command_buffer != vk::CommandBuffer::null(),
            "null vulkan command buffer"
        );

        let resources = resources.native(self.inner.config)?;

        anyhow::ensure!(
            quality != DlssRrQuality::Dlaa
                || resources.input_color.extent() == resources.output_color.extent(),
            "dlaa requires equal render and output dimensions"
        );

        let _guard = API_LOCK
            .lock()
            .unwrap_or_else(std::sync::PoisonError::into_inner);
        let context = self.inner.context.load(Ordering::SeqCst);

        anyhow::ensure!(!context.is_null(), "ngx evaluator has been shut down");

        let result = unsafe {
            nvidia_dlss_ngx_evaluate(
                context,
                usize::try_from(command_buffer.as_raw())? as *mut c_void,
                quality as u32,
                frame,
                &raw const resources,
            )
        };

        check_result(result).context("evaluating ngx dlss-rr")
    }

    /// # Safety
    ///
    /// All NGX GPU work must have completed before shutdown.
    ///
    /// # Errors
    /// Returns an error if NGX resource release or shutdown fails.
    pub unsafe fn shutdown(&self) -> anyhow::Result<()> {
        self.inner.shutdown()
    }
}

struct EvaluatorInner {
    config: DlssRrConfig,
    context: AtomicPtr<NativeContext>,
    #[cfg(feature = "vk-graph")]
    _device: Option<Device>,
}

impl EvaluatorInner {
    fn shutdown(&self) -> anyhow::Result<()> {
        let _guard = API_LOCK
            .lock()
            .unwrap_or_else(std::sync::PoisonError::into_inner);
        let context = self.context.swap(std::ptr::null_mut(), Ordering::SeqCst);

        if context.is_null() {
            return Ok(());
        }

        let _ = ACTIVE_CONTEXT.compare_exchange(
            context,
            std::ptr::null_mut(),
            Ordering::SeqCst,
            Ordering::SeqCst,
        );

        let result = unsafe { nvidia_dlss_ngx_shutdown(context) };

        check_result(result).context("shutting down ngx")
    }
}

#[repr(C)]
struct NativeContext {
    _private: [u8; 0],
}

#[repr(C)]
struct NativeExtensions {
    count: u32,
    names: [[c_char; MAX_EXTENSION_NAME_SIZE]; MAX_EXTENSIONS],
}

impl NativeExtensions {
    fn extension_names(&self) -> anyhow::Result<Vec<CString>> {
        let count = usize::try_from(self.count).context("converting ngx extension count")?;

        anyhow::ensure!(
            count <= MAX_EXTENSIONS,
            "invalid ngx extension count {count}"
        );

        self.names[..count]
            .iter()
            .map(|name| {
                let name = unsafe { CStr::from_ptr(name.as_ptr()) };

                CString::new(name.to_bytes()).context("copying ngx extension name")
            })
            .collect()
    }
}

impl Default for NativeExtensions {
    fn default() -> Self {
        Self {
            count: 0,
            names: [[0; MAX_EXTENSION_NAME_SIZE]; MAX_EXTENSIONS],
        }
    }
}

#[derive(Clone, Copy)]
#[repr(C)]
struct NativeExtent {
    width: u32,
    height: u32,
}

#[derive(Clone, Copy)]
#[repr(C)]
struct NativeIdentity {
    application_id: u64,
    project_id: *const c_char,
    engine_version: *const c_char,
}

unsafe extern "C" {
    fn nvidia_dlss_ngx_instance_extensions(
        identity: *const NativeIdentity,
        application_data_path: *const c_char,
        runtime_path: *const c_char,
        output: *mut NativeExtensions,
    ) -> i32;
    fn nvidia_dlss_ngx_device_extensions(
        identity: *const NativeIdentity,
        application_data_path: *const c_char,
        runtime_path: *const c_char,
        instance: *mut c_void,
        physical_device: *mut c_void,
        output: *mut NativeExtensions,
    ) -> i32;
    fn nvidia_dlss_ngx_init(
        identity: *const NativeIdentity,
        application_data_path: *const c_char,
        runtime_path: *const c_char,
        instance: *mut c_void,
        physical_device: *mut c_void,
        device: *mut c_void,
        get_instance_proc_addr: vk::PFN_vkGetInstanceProcAddr,
        get_device_proc_addr: vk::PFN_vkGetDeviceProcAddr,
        config: *const DlssRrConfig,
        output: *mut *mut NativeContext,
    ) -> i32;
    fn nvidia_dlss_ngx_optimal_settings(
        context: *mut NativeContext,
        output_width: u32,
        output_height: u32,
        quality: u32,
        output: *mut NativeExtent,
    ) -> i32;
    fn nvidia_dlss_ngx_evaluate(
        context: *mut NativeContext,
        command_buffer: *mut c_void,
        quality: u32,
        frame: *const DlssRrFrameConstants,
        resources: *const NativeDlssRrResources,
    ) -> i32;
    fn nvidia_dlss_ngx_shutdown(context: *mut NativeContext) -> i32;
    fn nvidia_dlss_ngx_result_name(result: i32) -> *const c_char;
}

pub struct Ngx {
    info: PreparedInfo,
    instance_extensions: Box<[CString]>,
    evaluator: Option<Evaluator>,
}

impl Ngx {
    /// Returns whether this build includes the native NGX backend.
    #[must_use]
    pub const fn native_backend_available() -> bool {
        true
    }

    /// Returns the build-staged runtime directory, or `None` without the native backend.
    #[must_use]
    pub fn staged_runtime_dir() -> Option<&'static Path> {
        Some(Path::new(env!("NVIDIA_DLSS_RUNTIME_DIR")))
    }

    /// # Errors
    /// Returns an error for invalid paths, missing runtime files, filesystem errors,
    /// or NGX extension discovery failure.
    pub fn new(info: InitInfo) -> anyhow::Result<Self> {
        let info = PreparedInfo::new(info)?;
        let identity = info.identity();
        let mut output = NativeExtensions::default();
        let _guard = API_LOCK
            .lock()
            .unwrap_or_else(std::sync::PoisonError::into_inner);

        let result = unsafe {
            nvidia_dlss_ngx_instance_extensions(
                &raw const identity,
                info.application_data_path.as_ptr(),
                info.runtime_path.as_ptr(),
                &raw mut output,
            )
        };

        check_result(result).context("querying ngx instance extensions")?;

        Ok(Self {
            info,
            instance_extensions: output.extension_names()?.into_boxed_slice(),
            evaluator: None,
        })
    }

    #[must_use]
    pub fn instance_extensions(&self) -> &[CString] {
        &self.instance_extensions
    }

    /// Queries NGX device extension requirements.
    ///
    /// # Safety
    ///
    /// `physical_device` must be a valid physical device enumerated from `instance`.
    /// The instance, physical device, and Vulkan loader that created them must remain
    /// valid throughout the call and must not be destroyed concurrently.
    ///
    /// # Errors
    /// Returns an error if NGX rejects the device or extension discovery fails.
    #[cfg(feature = "vk-graph")]
    pub unsafe fn device_extensions(
        &self,
        instance: &Instance,
        physical_device: vk::PhysicalDevice,
    ) -> anyhow::Result<Vec<CString>> {
        unsafe { self.device_extensions_raw(instance.handle(), physical_device) }
    }

    /// # Safety
    ///
    /// `instance` must be a valid Vulkan instance, and `physical_device` must be a valid
    /// physical device enumerated from it. The instance, physical device, and Vulkan loader
    /// that created them must remain valid throughout the call and must not be destroyed
    /// concurrently.
    ///
    /// # Errors
    /// Returns an error if NGX rejects the handles/device or extension discovery fails.
    pub unsafe fn device_extensions_raw(
        &self,
        instance: vk::Instance,
        physical_device: vk::PhysicalDevice,
    ) -> anyhow::Result<Vec<CString>> {
        let identity = self.info.identity();
        let mut output = NativeExtensions::default();
        let _guard = API_LOCK
            .lock()
            .unwrap_or_else(std::sync::PoisonError::into_inner);

        let result = unsafe {
            nvidia_dlss_ngx_device_extensions(
                &raw const identity,
                self.info.application_data_path.as_ptr(),
                self.info.runtime_path.as_ptr(),
                usize::try_from(instance.as_raw())? as *mut c_void,
                usize::try_from(physical_device.as_raw())? as *mut c_void,
                &raw mut output,
            )
        };

        check_result(result).context("querying ngx device extensions")?;

        output.extension_names()
    }

    /// Initializes NGX while retaining ownership of the graph device.
    ///
    /// # Safety
    ///
    /// The instance and device must enable the extensions returned by NGX. Complete all
    /// NGX GPU work before explicit shutdown through `Ngx` or any evaluator. Dropping
    /// these wrappers does not shut down NGX.
    ///
    /// # Errors
    /// Returns an error if a context is already active or NGX initialization fails.
    #[cfg(feature = "vk-graph")]
    pub unsafe fn initialize(
        &mut self,
        device: &Device,
        config: DlssRrConfig,
    ) -> anyhow::Result<Evaluator> {
        let instance = &device.physical.instance;
        let info = VulkanInitInfo {
            instance: instance.handle(),
            physical_device: device.physical.handle,
            device: device.handle(),
            get_instance_proc_addr: Instance::entry(instance).static_fn().get_instance_proc_addr,
            get_device_proc_addr: instance.fp_v1_0().get_device_proc_addr,
        };

        unsafe { self.initialize_vulkan(&info, config, Some(device.clone())) }
    }

    /// Initializes NGX without retaining ownership of the Vulkan device.
    ///
    /// # Safety
    ///
    /// Handles must be valid and belong to the same Vulkan instance and physical device.
    /// The instance and device must enable the extensions returned by NGX. The entrypoints
    /// must belong to the loader that created these handles. Keep the loader, instance, and device alive
    /// until explicit shutdown through `Ngx` or any evaluator completes, and complete all
    /// NGX GPU work before shutdown. Dropping these wrappers does not shut down NGX.
    ///
    /// # Errors
    /// Returns an error if a context is already active or NGX initialization fails.
    pub unsafe fn initialize_raw(
        &mut self,
        info: VulkanInitInfo,
        config: DlssRrConfig,
    ) -> anyhow::Result<Evaluator> {
        unsafe {
            self.initialize_vulkan(
                &info,
                config,
                #[cfg(feature = "vk-graph")]
                None,
            )
        }
    }

    unsafe fn initialize_vulkan(
        &mut self,
        info: &VulkanInitInfo,
        config: DlssRrConfig,
        #[cfg(feature = "vk-graph")] device: Option<Device>,
    ) -> anyhow::Result<Evaluator> {
        anyhow::ensure!(
            !self.evaluator.as_ref().is_some_and(Evaluator::is_active),
            "ngx is already initialized"
        );

        self.evaluator = None;

        let _guard = API_LOCK
            .lock()
            .unwrap_or_else(std::sync::PoisonError::into_inner);

        anyhow::ensure!(
            ACTIVE_CONTEXT.load(Ordering::SeqCst).is_null(),
            "an ngx context is already active"
        );

        let identity = self.info.identity();
        let mut context = std::ptr::null_mut();

        let result = unsafe {
            nvidia_dlss_ngx_init(
                &raw const identity,
                self.info.application_data_path.as_ptr(),
                self.info.runtime_path.as_ptr(),
                usize::try_from(info.instance.as_raw())? as *mut c_void,
                usize::try_from(info.physical_device.as_raw())? as *mut c_void,
                usize::try_from(info.device.as_raw())? as *mut c_void,
                info.get_instance_proc_addr,
                info.get_device_proc_addr,
                &raw const config,
                &raw mut context,
            )
        };

        check_result(result).context("initializing ngx")?;
        let context = NonNull::new(context).context("ngx returned a null context")?;
        let context = context.as_ptr();
        ACTIVE_CONTEXT.store(context, Ordering::SeqCst);

        let evaluator = Evaluator {
            inner: Arc::new(EvaluatorInner {
                config,
                context: AtomicPtr::new(context),
                #[cfg(feature = "vk-graph")]
                _device: device,
            }),
        };
        self.evaluator = Some(evaluator.clone());
        Ok(evaluator)
    }

    #[must_use]
    pub fn evaluator(&self) -> Option<Evaluator> {
        self.evaluator
            .as_ref()
            .filter(|evaluator| evaluator.is_active())
            .cloned()
    }

    /// # Safety
    ///
    /// All NGX GPU work must have completed before shutdown.
    ///
    /// # Errors
    /// Returns an error if NGX resource release or shutdown fails.
    pub unsafe fn shutdown(&mut self) -> anyhow::Result<()> {
        match self.evaluator.take() {
            Some(evaluator) => unsafe { evaluator.shutdown() },
            None => Ok(()),
        }
    }
}

struct PreparedInfo {
    application_data_path: CString,
    runtime_path: CString,
    application_id: u64,
    project_id: CString,
    engine_version: CString,
}

impl PreparedInfo {
    fn new(info: InitInfo) -> anyhow::Result<Self> {
        Self::path_context(&info.application_data_path, "ngx application data path")?;
        Self::path_context(&info.runtime_path, "ngx runtime path")?;

        std::fs::create_dir_all(&info.application_data_path).with_context(|| {
            format!(
                "creating ngx application data directory {}",
                info.application_data_path.display()
            )
        })?;

        anyhow::ensure!(
            info.runtime_path.join(DLSS_RUNTIME_FILE).is_file(),
            "dlssd runtime is missing {}",
            info.runtime_path.join(DLSS_RUNTIME_FILE).display()
        );

        let (application_id, project_id, engine_version) = match info.identity.kind {
            IdentityKind::ApplicationId(application_id) => (
                application_id,
                CString::new("").expect("empty strings contain no null bytes"),
                CString::new("").expect("empty strings contain no null bytes"),
            ),
            IdentityKind::Project {
                project_id,
                engine_version,
            } => (
                0,
                CString::new(project_id).context("converting nvidia project id")?,
                CString::new(engine_version).context("converting nvidia engine version")?,
            ),
        };

        Ok(Self {
            application_data_path: Self::path_cstring(&info.application_data_path)?,
            runtime_path: Self::path_cstring(&info.runtime_path)?,
            application_id,
            project_id,
            engine_version,
        })
    }

    fn identity(&self) -> NativeIdentity {
        NativeIdentity {
            application_id: self.application_id,
            project_id: self.project_id.as_ptr(),
            engine_version: self.engine_version.as_ptr(),
        }
    }

    fn path_context(path: &Path, purpose: &str) -> anyhow::Result<()> {
        anyhow::ensure!(path.is_absolute(), "{purpose} must be an absolute path");

        path.to_str()
            .with_context(|| format!("{purpose} is not valid utf-8: {}", path.display()))?;

        Ok(())
    }

    fn path_cstring(path: &Path) -> anyhow::Result<CString> {
        CString::new(path.as_os_str().as_bytes())
            .with_context(|| format!("path contains a null byte: {}", path.display()))
    }
}

fn check_result(result: i32) -> anyhow::Result<()> {
    anyhow::ensure!(result == 0, "{}", result_name(result));

    Ok(())
}

fn result_name(result: i32) -> String {
    let name = unsafe { nvidia_dlss_ngx_result_name(result) };

    if name.is_null() {
        return format!("ngx result {result:#x}");
    }

    format!(
        "{} ({:#x})",
        unsafe { CStr::from_ptr(name) }.to_string_lossy(),
        result.cast_unsigned()
    )
}

#[cfg(test)]
mod tests {
    use {super::*, std::mem::size_of};

    #[test]
    fn native_extension_output_matches_cpp() {
        assert_eq!(size_of::<NativeExtensions>(), 4 + 32 * 256);
    }
}