concinnity-device 0.18.65

GPU backends (Metal, Vulkan, DirectX) behind a device facade for Concinnity
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
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
// src/vulkan/post/upscale/xess.rs
//
// Intel XeSS temporal upscaling for the Vulkan backend. One of the three
// `VkUpscaleBackend` implementations; runs cross-vendor (Arc XMX + the DP4a
// fallback on other GPUs). The runtime DLL is `libxess.dll` (it carries the
// `xessVK*` entry points alongside the D3D12 ones), loaded on demand via
// `libloading` (cross-platform, mirroring the FSR module's choice over the DX
// path's `LoadLibraryA`). Failure to load logs a warning and `try_new` returns
// `None`; `build_upscaler` then falls through.
//
// Unlike FSR, XeSS needs Vulkan instance + device extensions (and a device
// feature chain) enabled at instance / device creation. Those are queried up
// front through `XessExtQuery` (held by `UpscaleSdk`, see `mod.rs`); this module
// only creates the upscale context after the device exists.
//
// The FFI bindings are inline (small, concentrated API), validated against XeSS
// SDK 3.0.1 (`inc/xess/{xess.h,xess_vk.h}`) by the size/offset asserts in the
// tests. `XESS_PACK_B()` is `pack(8)`, a no-op on x86_64 where every field is
// already <= 8-aligned, so `#[repr(C)]` matches byte-for-byte.
#![expect(
    non_camel_case_types,
    reason = "inline XeSS bindings keep the SDK's own C type names"
)]

use std::cell::Cell;
use std::ffi::{CString, c_char, c_void};
use std::ptr;

use ash::vk;

use crate::vulkan::owned::VkDevice;

use super::{UpscaleCamera, UpscaleImage, UpscaleInputs, VkUpscaleBackend, copy_ext_names};
use crate::vulkan::context::HDR_FORMAT;
use crate::vulkan::texture::GpuImage;

// xess_result_t: 0 == success, negative == error, positive == warning.
const XESS_RESULT_SUCCESS: i32 = 0;

// xess_quality_settings_t (a C enum, ABI int).
const XESS_QUALITY_SETTING_ULTRA_PERFORMANCE: i32 = 100;
const XESS_QUALITY_SETTING_PERFORMANCE: i32 = 101;
const XESS_QUALITY_SETTING_BALANCED: i32 = 102;
const XESS_QUALITY_SETTING_QUALITY: i32 = 103;
const XESS_QUALITY_SETTING_AA: i32 = 106;

// xess_init_flags_t (bitmask). The engine feeds HDR linear colour, low-res
// (render-resolution) UV motion vectors scaled to pixels via SetVelocityScale,
// and depth in [0,1] with 0 = near (NOT inverted). So the only flag set is
// auto-exposure (the scene is un-exposed pre-upscale, matching the FSR path).
const XESS_INIT_FLAG_ENABLE_AUTOEXPOSURE: u32 = 1 << 8;

type xess_context_handle_t = *mut c_void;

// xess_coord_t is a typedef of xess_2d_t (xess.h:87): { uint32_t x, y }.
#[repr(C)]
#[derive(Clone, Copy)]
struct xess_2d_t {
    x: u32,
    y: u32,
}

// xess_vk_image_view_info (xess_vk.h). VkImageView / VkImage are
// non-dispatchable 64-bit handles; VkImageSubresourceRange is 5 u32s (20 B);
// VkFormat is an ABI int. 48 bytes under pack(8).
#[repr(C)]
#[derive(Clone, Copy)]
struct xess_vk_image_view_info {
    image_view: vk::ImageView,
    image: vk::Image,
    subresource_range: vk::ImageSubresourceRange,
    format: vk::Format,
    width: u32,
    height: u32,
}

impl xess_vk_image_view_info {
    // An absent optional input (exposure scale / responsive mask): null
    // handles, no flags set so XeSS ignores it.
    fn empty() -> Self {
        Self {
            image_view: vk::ImageView::null(),
            image: vk::Image::null(),
            subresource_range: vk::ImageSubresourceRange {
                aspect_mask: vk::ImageAspectFlags::empty(),
                base_mip_level: 0,
                level_count: 0,
                base_array_layer: 0,
                layer_count: 0,
            },
            format: vk::Format::UNDEFINED,
            width: 0,
            height: 0,
        }
    }

    fn from_input(img: &UpscaleImage) -> Self {
        Self {
            image_view: img.view,
            image: img.image,
            subresource_range: vk::ImageSubresourceRange {
                aspect_mask: img.aspect,
                base_mip_level: 0,
                level_count: 1,
                base_array_layer: 0,
                layer_count: 1,
            },
            format: img.format,
            width: img.width,
            height: img.height,
        }
    }
}

#[repr(C)]
struct xess_vk_init_params_t {
    output_resolution: xess_2d_t,
    quality_setting: i32,
    init_flags: u32,
    creation_node_mask: u32,
    visible_node_mask: u32,
    temp_buffer_heap: vk::DeviceMemory,
    buffer_heap_offset: u64,
    temp_texture_heap: vk::DeviceMemory,
    texture_heap_offset: u64,
    pipeline_cache: vk::PipelineCache,
}

#[repr(C)]
struct xess_vk_execute_params_t {
    color_texture: xess_vk_image_view_info,
    velocity_texture: xess_vk_image_view_info,
    depth_texture: xess_vk_image_view_info,
    exposure_scale_texture: xess_vk_image_view_info,
    responsive_pixel_mask_texture: xess_vk_image_view_info,
    output_texture: xess_vk_image_view_info,
    jitter_offset_x: f32,
    jitter_offset_y: f32,
    exposure_scale: f32,
    reset_history: u32,
    input_width: u32,
    input_height: u32,
    input_color_base: xess_2d_t,
    input_motion_vector_base: xess_2d_t,
    input_depth_base: xess_2d_t,
    input_responsive_mask_base: xess_2d_t,
    reserved0: xess_2d_t,
    output_color_base: xess_2d_t,
}

// Extension / context entry points. XESS_API is a bare dllimport (no explicit
// __cdecl/__stdcall), so on x86_64 Windows `extern "C"` is the only convention.
type PfnXessVKGetRequiredInstanceExtensions =
    unsafe extern "C" fn(*mut u32, *mut *const *const c_char, *mut u32) -> i32;
type PfnXessVKGetRequiredDeviceExtensions = unsafe extern "C" fn(
    vk::Instance,
    vk::PhysicalDevice,
    *mut u32,
    *mut *const *const c_char,
) -> i32;
type PfnXessVKGetRequiredDeviceFeatures =
    unsafe extern "C" fn(vk::Instance, vk::PhysicalDevice, *mut *mut c_void) -> i32;
type PfnXessVKCreateContext = unsafe extern "C" fn(
    vk::Instance,
    vk::PhysicalDevice,
    vk::Device,
    *mut xess_context_handle_t,
) -> i32;
type PfnXessVKBuildPipelines =
    unsafe extern "C" fn(xess_context_handle_t, vk::PipelineCache, bool, u32) -> i32;
type PfnXessVKInit =
    unsafe extern "C" fn(xess_context_handle_t, *const xess_vk_init_params_t) -> i32;
type PfnXessVKExecute = unsafe extern "C" fn(
    xess_context_handle_t,
    vk::CommandBuffer,
    *const xess_vk_execute_params_t,
) -> i32;
type PfnXessDestroyContext = unsafe extern "C" fn(xess_context_handle_t) -> i32;
type PfnXessSetVelocityScale = unsafe extern "C" fn(xess_context_handle_t, f32, f32) -> i32;

fn lib_name() -> &'static str {
    if cfg!(windows) {
        "libxess.dll"
    } else {
        "libxess.so"
    }
}

// Pre-device extension / feature queries (XeSS-specific). Held by `UpscaleSdk`
// across instance + device creation so the SDK-owned device-feature chain
// (`device_features`) stays mapped through `vkCreateDevice`.
pub(super) struct XessExtQuery {
    _lib: libloading::Library,
    get_instance_exts: PfnXessVKGetRequiredInstanceExtensions,
    get_device_exts: PfnXessVKGetRequiredDeviceExtensions,
    get_device_features: PfnXessVKGetRequiredDeviceFeatures,
}

impl XessExtQuery {
    pub(super) fn load() -> Option<Self> {
        // SAFETY: loading a system library + reading well-known C symbols whose
        // prototypes match the XeSS SDK 3.0.1 headers; misses surface as `None`.
        unsafe {
            let lib = libloading::Library::new(lib_name()).ok()?;
            let get_instance_exts = *lib
                .get::<PfnXessVKGetRequiredInstanceExtensions>(
                    b"xessVKGetRequiredInstanceExtensions\0",
                )
                .ok()?;
            let get_device_exts = *lib
                .get::<PfnXessVKGetRequiredDeviceExtensions>(b"xessVKGetRequiredDeviceExtensions\0")
                .ok()?;
            let get_device_features = *lib
                .get::<PfnXessVKGetRequiredDeviceFeatures>(b"xessVKGetRequiredDeviceFeatures\0")
                .ok()?;
            Some(XessExtQuery {
                _lib: lib,
                get_instance_exts,
                get_device_exts,
                get_device_features,
            })
        }
    }

    // Returns XeSS's required instance extensions and the minimum Vulkan API
    // version it needs (XeSS 3.x shaders use SPV_KHR_integer_dot_product, which
    // requires a Vulkan 1.3 environment). The caller raises the instance
    // `apiVersion` to at least this (clamped to loader support).
    pub(super) fn instance_extensions(&self) -> (Vec<CString>, u32) {
        let mut count: u32 = 0;
        let mut exts: *const *const c_char = ptr::null();
        let mut min_api: u32 = 0;
        // SAFETY: the entry point was resolved from the loaded XeSS library at init and matches the
        // SDK's declared signature; the context and every parameter / out-param it is handed are
        // live for the call.
        let rc = unsafe { (self.get_instance_exts)(&mut count, &mut exts, &mut min_api) };
        if rc != XESS_RESULT_SUCCESS {
            tracing::warn!("XeSS: xessVKGetRequiredInstanceExtensions returned {rc}");
            return (Vec::new(), 0);
        }
        // SAFETY: `count`/`exts` are the pair the SDK just wrote on the success path above, and the
        // library owns that array for as long as it stays loaded.
        (unsafe { copy_ext_names(count, exts) }, min_api)
    }

    pub(super) fn device_extensions(
        &self,
        instance: &ash::Instance,
        physical_device: vk::PhysicalDevice,
    ) -> Vec<CString> {
        let mut count: u32 = 0;
        let mut exts: *const *const c_char = ptr::null();
        // SAFETY: the entry point was resolved from the loaded XeSS library at init and matches the
        // SDK's declared signature; the context and every parameter / out-param it is handed are
        // live for the call.
        let rc = unsafe {
            (self.get_device_exts)(instance.handle(), physical_device, &mut count, &mut exts)
        };
        if rc != XESS_RESULT_SUCCESS {
            tracing::warn!("XeSS: xessVKGetRequiredDeviceExtensions returned {rc}");
            return Vec::new();
        }
        // SAFETY: as in `instance_extensions` -- `count`/`exts` are the pair the SDK just wrote.
        unsafe { copy_ext_names(count, exts) }
    }

    // Patch the device-feature `pNext` chain with XeSS's required features and
    // return the (possibly new) chain head, to be set as `VkDeviceCreateInfo.pNext`.
    // `head` is the caller's existing chain; the returned memory the SDK adds is
    // owned by libxess.dll and valid while `self` lives. On failure the caller's
    // `head` is returned unchanged.
    pub(super) fn device_features(
        &self,
        instance: &ash::Instance,
        physical_device: vk::PhysicalDevice,
        head: *mut c_void,
    ) -> *mut c_void {
        let mut chain = head;
        let rc =
            // SAFETY: the entry point was resolved from the loaded XeSS library at init and matches
            // the SDK's declared signature; the context and every parameter / out-param it is
            // handed are live for the call.
            unsafe { (self.get_device_features)(instance.handle(), physical_device, &mut chain) };
        if rc != XESS_RESULT_SUCCESS {
            tracing::warn!(
                "XeSS: xessVKGetRequiredDeviceFeatures returned {rc}; using base features"
            );
            return head;
        }
        chain
    }
}

// The full XeSS context API, loaded once at context creation.
struct XessApi {
    _lib: libloading::Library,
    create_context: PfnXessVKCreateContext,
    build_pipelines: PfnXessVKBuildPipelines,
    init: PfnXessVKInit,
    execute: PfnXessVKExecute,
    destroy_context: PfnXessDestroyContext,
    set_velocity_scale: PfnXessSetVelocityScale,
}

impl XessApi {
    fn load() -> Option<Self> {
        // SAFETY: see `XessExtQuery::load`.
        unsafe {
            let lib = libloading::Library::new(lib_name()).ok()?;
            let create_context = *lib
                .get::<PfnXessVKCreateContext>(b"xessVKCreateContext\0")
                .ok()?;
            let build_pipelines = *lib
                .get::<PfnXessVKBuildPipelines>(b"xessVKBuildPipelines\0")
                .ok()?;
            let init = *lib.get::<PfnXessVKInit>(b"xessVKInit\0").ok()?;
            let execute = *lib.get::<PfnXessVKExecute>(b"xessVKExecute\0").ok()?;
            let destroy_context = *lib
                .get::<PfnXessDestroyContext>(b"xessDestroyContext\0")
                .ok()?;
            let set_velocity_scale = *lib
                .get::<PfnXessSetVelocityScale>(b"xessSetVelocityScale\0")
                .ok()?;
            Some(XessApi {
                _lib: lib,
                create_context,
                build_pipelines,
                init,
                execute,
                destroy_context,
                set_velocity_scale,
            })
        }
    }
}

// Map the engine's per-axis render-to-output ratio to the nearest XeSS quality
// preset. The preset is a hint for XeSS's internal model selection; the actual
// render dims are `output * scale` (passed via `inputWidth/Height`). Pure; unit
// tested. Mirrors `directx::post::upscale::xess::quality_from_scale`.
fn quality_from_scale(scale: f32) -> i32 {
    if scale >= 0.99 {
        XESS_QUALITY_SETTING_AA
    } else if scale >= 0.62 {
        XESS_QUALITY_SETTING_QUALITY
    } else if scale >= 0.55 {
        XESS_QUALITY_SETTING_BALANCED
    } else if scale >= 0.42 {
        XESS_QUALITY_SETTING_PERFORMANCE
    } else {
        XESS_QUALITY_SETTING_ULTRA_PERFORMANCE
    }
}

// Owns the XeSS context, the output texture the bloom + composite stack samples
// (at output resolution), and the loaded function table.
pub(in crate::vulkan) struct XessUpscaler {
    xess: XessApi,
    ctx: xess_context_handle_t,

    output: GpuImage,
    output_layout: Cell<vk::ImageLayout>,

    render_width: u32,
    render_height: u32,
    output_width: u32,
    output_height: u32,
    upscale_scale: f32,

    jitter: Cell<[f32; 2]>,
    reset_pending: Cell<bool>,
}

// SAFETY: The XeSS context handle + loaded function pointers are raw C pointers used
// only on the render thread; the trait's `Send` bound is satisfied unsafely,
// same as the rest of `VkContext`.
unsafe impl Send for XessUpscaler {}

impl XessUpscaler {
    // Try to construct an XeSS upscaler. Returns `Ok(None)` when XeSS is
    // unavailable (DLL miss / context init failure); `build_upscaler` falls
    // through. Assumes the XeSS instance / device extensions + features were
    // enabled at creation time (via `UpscaleSdk`); if they were not, the
    // context create / init below fails and we fall back.
    pub(super) fn try_new(
        gpu: super::UpscalerGpu<'_>,
        output_width: u32,
        output_height: u32,
        upscale_scale: f32,
    ) -> Result<Option<Self>, String> {
        let super::UpscalerGpu {
            alloc,
            instance,
            device,
            physical_device,
            command_pool,
            queue,
        } = gpu;
        let xess = match XessApi::load() {
            Some(api) => api,
            None => {
                tracing::warn!(
                    "XeSS (Vulkan): libxess.dll not found (build.rs did not bundle it; set \
                     XESS_SDK_ROOT or put the DLL on PATH). Trying the next backend."
                );
                return Ok(None);
            }
        };

        let (render_width, render_height, scale) =
            super::resolve_render_dims(output_width, output_height, upscale_scale);

        let mut ctx: xess_context_handle_t = ptr::null_mut();
        // SAFETY: the entry point was resolved from the loaded XeSS library at init and matches the
        // SDK's declared signature; the context and every parameter / out-param it is handed are
        // live for the call.
        let rc = unsafe {
            (xess.create_context)(
                instance.handle(),
                physical_device,
                device.handle(),
                &mut ctx,
            )
        };
        if rc != XESS_RESULT_SUCCESS || ctx.is_null() {
            tracing::warn!(
                "XeSS (Vulkan): xessVKCreateContext returned {rc}; trying the next backend"
            );
            return Ok(None);
        }

        let init_flags = XESS_INIT_FLAG_ENABLE_AUTOEXPOSURE;
        // SAFETY: the entry point was resolved from the loaded XeSS library at init and matches the
        // SDK's declared signature; the context and every parameter / out-param it is handed are
        // live for the call.
        let rc = unsafe {
            (xess.build_pipelines)(
                ctx,
                crate::vulkan::pipeline_cache::handle(),
                true,
                init_flags,
            )
        };
        if rc != XESS_RESULT_SUCCESS {
            tracing::warn!(
                "XeSS (Vulkan): xessVKBuildPipelines returned {rc}; trying the next backend"
            );
            // SAFETY: the entry point was resolved from the loaded XeSS library at init and matches
            // the SDK's declared signature; the context and every parameter / out-param it is
            // handed are live for the call.
            unsafe { (xess.destroy_context)(ctx) };
            return Ok(None);
        }

        let init_params = xess_vk_init_params_t {
            output_resolution: xess_2d_t {
                x: output_width,
                y: output_height,
            },
            quality_setting: quality_from_scale(scale),
            init_flags,
            creation_node_mask: 0,
            visible_node_mask: 0,
            temp_buffer_heap: vk::DeviceMemory::null(),
            buffer_heap_offset: 0,
            temp_texture_heap: vk::DeviceMemory::null(),
            texture_heap_offset: 0,
            pipeline_cache: crate::vulkan::pipeline_cache::handle(),
        };
        // SAFETY: the entry point was resolved from the loaded XeSS library at init and matches the
        // SDK's declared signature; the context and every parameter / out-param it is handed are
        // live for the call.
        let rc = unsafe { (xess.init)(ctx, &init_params) };
        if rc != XESS_RESULT_SUCCESS {
            tracing::warn!("XeSS (Vulkan): xessVKInit returned {rc}; trying the next backend");
            // SAFETY: the entry point was resolved from the loaded XeSS library at init and matches
            // the SDK's declared signature; the context and every parameter / out-param it is
            // handed are live for the call.
            unsafe { (xess.destroy_context)(ctx) };
            return Ok(None);
        }

        // Motion vectors are RG16F `prev_uv - cur_uv` in UV space; XeSS expects
        // pixel-space velocity (default low-res), so scale by the render extent.
        let rc =
            // SAFETY: the entry point was resolved from the loaded XeSS library at init and matches
            // the SDK's declared signature; the context and every parameter / out-param it is
            // handed are live for the call.
            unsafe { (xess.set_velocity_scale)(ctx, render_width as f32, render_height as f32) };
        if rc != XESS_RESULT_SUCCESS {
            tracing::warn!("XeSS (Vulkan): xessSetVelocityScale returned {rc} (non-fatal)");
        }

        let output = match super::create_output_image(
            alloc,
            device,
            command_pool,
            queue,
            output_width,
            output_height,
        ) {
            Ok(img) => img,
            Err(e) => {
                // SAFETY: the entry point was resolved from the loaded XeSS library at init and
                // matches the SDK's declared signature; the context and every parameter / out-param
                // it is handed are live for the call.
                unsafe { (xess.destroy_context)(ctx) };
                return Err(e);
            }
        };

        tracing::info!(
            "XeSS (Vulkan): context created: render {render_width}x{render_height} -> upscale \
             {output_width}x{output_height} (scale {scale:.3})"
        );

        Ok(Some(XessUpscaler {
            xess,
            ctx,
            output,
            output_layout: Cell::new(vk::ImageLayout::GENERAL),
            render_width,
            render_height,
            output_width,
            output_height,
            upscale_scale: scale,
            jitter: Cell::new([0.0, 0.0]),
            reset_pending: Cell::new(true),
        }))
    }
}

impl VkUpscaleBackend for XessUpscaler {
    fn render_dims(&self) -> (u32, u32) {
        (self.render_width, self.render_height)
    }
    fn output_dims(&self) -> (u32, u32) {
        (self.output_width, self.output_height)
    }
    fn scale(&self) -> f32 {
        self.upscale_scale
    }
    fn output_image(&self) -> &GpuImage {
        &self.output
    }
    fn output_layout(&self) -> vk::ImageLayout {
        self.output_layout.get()
    }
    fn set_output_layout(&self, layout: vk::ImageLayout) {
        self.output_layout.set(layout);
    }
    fn set_jitter(&self, offset: [f32; 2]) {
        self.jitter.set(offset);
    }
    fn jitter(&self) -> [f32; 2] {
        self.jitter.get()
    }

    // XeSS prescribes no jitter sequence; the engine's Halton-2/3 (shared with
    // the camera projection) drives both. XeSS wants the offset in [-0.5, 0.5].
    fn jitter_offset(&self, frame_index: u32) -> [f32; 2] {
        super::halton_jitter_offset(frame_index)
    }

    fn dispatch(
        &self,
        cmd: vk::CommandBuffer,
        inputs: UpscaleInputs<'_>,
        camera: UpscaleCamera,
    ) -> Result<(), String> {
        let UpscaleInputs {
            color,
            depth,
            motion,
        } = inputs;
        // XeSS ignores the camera's temporal / projection fields (auto-exposure
        // + its own frame heuristics); only the shared jitter feeds the dispatch.
        let jitter_offset = camera.jitter_offset;
        let reset = self.reset_pending.replace(false);
        let zero = xess_2d_t { x: 0, y: 0 };
        let output_view = xess_vk_image_view_info {
            image_view: self.output.view,
            image: self.output.image,
            subresource_range: vk::ImageSubresourceRange {
                aspect_mask: vk::ImageAspectFlags::COLOR,
                base_mip_level: 0,
                level_count: 1,
                base_array_layer: 0,
                layer_count: 1,
            },
            format: HDR_FORMAT,
            width: self.output_width,
            height: self.output_height,
        };
        let params = xess_vk_execute_params_t {
            color_texture: xess_vk_image_view_info::from_input(color),
            velocity_texture: xess_vk_image_view_info::from_input(motion),
            depth_texture: xess_vk_image_view_info::from_input(depth),
            exposure_scale_texture: xess_vk_image_view_info::empty(),
            responsive_pixel_mask_texture: xess_vk_image_view_info::empty(),
            output_texture: output_view,
            jitter_offset_x: jitter_offset[0],
            jitter_offset_y: jitter_offset[1],
            exposure_scale: 1.0,
            reset_history: if reset { 1 } else { 0 },
            input_width: self.render_width,
            input_height: self.render_height,
            input_color_base: zero,
            input_motion_vector_base: zero,
            input_depth_base: zero,
            input_responsive_mask_base: zero,
            reserved0: zero,
            output_color_base: zero,
        };
        // SAFETY: the entry point was resolved from the loaded XeSS library at init and matches the
        // SDK's declared signature; the context and every parameter / out-param it is handed are
        // live for the call.
        let rc = unsafe { (self.xess.execute)(self.ctx, cmd, &params) };
        if rc != XESS_RESULT_SUCCESS {
            return Err(format!("xessVKExecute returned {rc}"));
        }
        Ok(())
    }

    fn destroy(&mut self, _device: &VkDevice) {
        if !self.ctx.is_null() {
            // SAFETY: the entry point was resolved from the loaded XeSS library at init and matches
            // the SDK's declared signature; the context and every parameter / out-param it is
            // handed are live for the call.
            unsafe {
                let _ = (self.xess.destroy_context)(self.ctx);
            }
            self.ctx = ptr::null_mut();
        }
        self.output = GpuImage::null();
    }
}

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

    // Pin the XeSS VK struct layouts against the SDK 3.0.1 headers. These differ
    // from the D3D12 structs (image-view-info vs raw resource pointers), so they
    // are the most likely silent ABI mismatch.
    #[test]
    fn xess_vk_struct_sizes_match_sdk_v301() {
        // VkImageView(8) + VkImage(8) + VkImageSubresourceRange(20) +
        // VkFormat(4) + width(4) + height(4) = 48.
        assert_eq!(size_of::<xess_vk_image_view_info>(), 48);
        assert_eq!(offset_of!(xess_vk_image_view_info, image_view), 0);
        assert_eq!(offset_of!(xess_vk_image_view_info, image), 8);
        assert_eq!(offset_of!(xess_vk_image_view_info, subresource_range), 16);
        assert_eq!(offset_of!(xess_vk_image_view_info, format), 36);
        assert_eq!(offset_of!(xess_vk_image_view_info, width), 40);
        assert_eq!(offset_of!(xess_vk_image_view_info, height), 44);

        // xess_2d_t = 2 u32.
        assert_eq!(size_of::<xess_2d_t>(), 8);

        // init params: outputResolution(8) + quality(4) + flags(4) + 2 masks(8)
        // + 3 handles(24) + 2 offsets(16) = 64.
        assert_eq!(size_of::<xess_vk_init_params_t>(), 64);
        assert_eq!(offset_of!(xess_vk_init_params_t, quality_setting), 8);
        assert_eq!(offset_of!(xess_vk_init_params_t, init_flags), 12);
        assert_eq!(offset_of!(xess_vk_init_params_t, temp_buffer_heap), 24);
        assert_eq!(offset_of!(xess_vk_init_params_t, pipeline_cache), 56);

        // execute params: 6 image-view-infos (288) + 3 floats + reset + 2 dims
        // (24) + 6 coords (48) = 360.
        assert_eq!(size_of::<xess_vk_execute_params_t>(), 360);
        assert_eq!(offset_of!(xess_vk_execute_params_t, output_texture), 240);
        assert_eq!(offset_of!(xess_vk_execute_params_t, jitter_offset_x), 288);
        assert_eq!(offset_of!(xess_vk_execute_params_t, reset_history), 300);
        assert_eq!(offset_of!(xess_vk_execute_params_t, input_width), 304);
        assert_eq!(offset_of!(xess_vk_execute_params_t, input_color_base), 312);
        assert_eq!(offset_of!(xess_vk_execute_params_t, output_color_base), 352);
    }

    #[test]
    fn xess_quality_mapping_is_monotonic_by_scale() {
        assert_eq!(quality_from_scale(1.0), XESS_QUALITY_SETTING_AA);
        assert_eq!(quality_from_scale(2.0 / 3.0), XESS_QUALITY_SETTING_QUALITY);
        assert_eq!(quality_from_scale(0.587), XESS_QUALITY_SETTING_BALANCED);
        assert_eq!(quality_from_scale(0.5), XESS_QUALITY_SETTING_PERFORMANCE);
        assert_eq!(
            quality_from_scale(1.0 / 3.0),
            XESS_QUALITY_SETTING_ULTRA_PERFORMANCE
        );
    }
}