phobos 0.10.0

Fast, powerful Vulkan abstraction library
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
//! Exposes wrappers around the FSR2 library for supersampling, integrated with the phobos library.

use std::ffi::c_void;
use std::fmt::{Display, Formatter};
use std::mem::MaybeUninit;
use std::ops::{Deref, DerefMut};
use std::time::Duration;

use anyhow::Result;
use ash::vk;
use ash::vk::Handle;
use fsr2_sys::{
    FfxErrorCode, FfxFsr2Context, ffxFsr2ContextCreate, FfxFsr2ContextDescription,
    ffxFsr2ContextDestroy, ffxFsr2ContextDispatch, FfxFsr2DispatchDescription,
    ffxFsr2GetInterfaceVK, ffxFsr2GetJitterOffset, ffxFsr2GetJitterPhaseCount, ffxFsr2GetRenderResolutionFromQualityMode,
    ffxFsr2GetScratchMemorySizeVK, FfxFsr2InstanceFunctionPointerTableVk, FfxFsr2Interface, FfxFsr2MsgType,
    ffxGetCommandListVK, ffxGetDeviceVK, ffxGetTextureResourceVK, FfxResource,
    FfxResourceState, VkDevice, VkPhysicalDevice,
};
pub use fsr2_sys::{
    FfxDimensions2D, FfxFloatCoords2D, FfxFsr2InitializationFlagBits, FfxFsr2QualityMode,
};
use thiserror::Error;
use widestring::{WideChar as wchar_t, WideCStr};

use crate::{Allocator, ComputeSupport, DeletionQueue, ImageView, IncompleteCommandBuffer};
use crate::domain::ExecutionDomain;
use crate::pool::{Pool, Poolable, Pooled};

/// FSR2 API error, stores an error code that describes the cause of the error
#[derive(Debug, Error)]
pub struct Fsr2Error {
    /// Error code with the cause of the FSR2 error.
    pub code: FfxErrorCode,
}

/// Check the FSR2 error code for an Ok status, and return Ok(()) in that case,
/// or an error result instead.
fn check_fsr2_error(code: FfxErrorCode) -> Result<()> {
    if code == FfxErrorCode::Ok {
        Ok(())
    } else if code == FfxErrorCode::Eof {
        Ok(())
    } else {
        Err(Fsr2Error {
            code,
        }
        .into())
    }
}

impl Display for Fsr2Error {
    fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
        match self.code {
            FfxErrorCode::Ok => {
                write!(f, "Ok")
            }
            FfxErrorCode::InvalidPointer => {
                write!(f, "Invalid pointer")
            }
            FfxErrorCode::InvalidAlignment => {
                write!(f, "Invalid alignment")
            }
            FfxErrorCode::InvalidSize => {
                write!(f, "Invalid size")
            }
            FfxErrorCode::Eof => {
                write!(f, "EOF")
            }
            FfxErrorCode::InvalidPath => {
                write!(f, "Invalid path")
            }
            FfxErrorCode::ErrorEof => {
                write!(f, "EOF Error")
            }
            FfxErrorCode::MalformedData => {
                write!(f, "Malformed data")
            }
            FfxErrorCode::OutOfMemory => {
                write!(f, "Out of Memory")
            }
            FfxErrorCode::IncompleteInterface => {
                write!(f, "Incomplete interface")
            }
            FfxErrorCode::InvalidEnum => {
                write!(f, "Invalid enum")
            }
            FfxErrorCode::InvalidArgument => {
                write!(f, "Invalid argument")
            }
            FfxErrorCode::OutOfRange => {
                write!(f, "Out of range")
            }
            FfxErrorCode::NullDevice => {
                write!(f, "Null device")
            }
            FfxErrorCode::BackendApiError => {
                write!(f, "Backend API error")
            }
            FfxErrorCode::InsufficientMemory => {
                write!(f, "Insufficient memory")
            }
        }
    }
}

struct BackendData(pub Box<[u8]>);

impl Deref for BackendData {
    type Target = Box<[u8]>;

    fn deref(&self) -> &Self::Target {
        &self.0
    }
}

impl DerefMut for BackendData {
    fn deref_mut(&mut self) -> &mut Self::Target {
        &mut self.0
    }
}

impl Poolable for BackendData {
    type Key = usize;

    fn on_release(&mut self) {}
}

/// Represents the initialized FSR2 context with its backend data.
#[derive(Derivative)]
#[derivative(Debug)]
pub struct Fsr2Context {
    context: Box<FfxFsr2Context>,
    backend: FfxFsr2Interface,
    #[derivative(Debug = "ignore")]
    backend_scratch_data: Pooled<BackendData>,
    current_frame: usize,
    #[derivative(Debug = "ignore")]
    fp_table: FfxFsr2InstanceFunctionPointerTableVk,
    #[derivative(Debug = "ignore")]
    scratch_data_pool: Pool<BackendData>,
    create_flags: FfxFsr2InitializationFlagBits,
    display_size: FfxDimensions2D,
    max_render_size: FfxDimensions2D,
    #[derivative(Debug = "ignore")]
    device: VkDevice,
    #[derivative(Debug = "ignore")]
    phys_device: VkPhysicalDevice,
    #[derivative(Debug = "ignore")]
    deferred_backend_delete: DeletionQueue<ReleasedFsr2Context>,
}

/// Experimental reactive mask generation parameters
#[derive(Debug, Clone)]
pub struct Fsr2AutoReactiveDescription {
    /// Opaque only color buffer for the current frame, at render resolution
    pub color_opaque_only: Option<ImageView>,
    /// Cutoff value for TC
    pub auto_tc_threshold: f32,
    /// Value to scale the transparency and composition mask
    pub auto_tc_scale: f32,
    /// Value to scale the reactive mask
    pub auto_reactive_scale: f32,
    /// Value to clamp the reactive mask
    pub auto_reactive_max: f32,
}

/// Stores resources needed to execute an FSR2 dispatch.
#[derive(Debug, Clone)]
pub struct Fsr2DispatchResources {
    /// Color buffer for the current frame, at render resolution.
    pub color: ImageView,
    /// Depth buffer for the current frame, at render resolution
    pub depth: ImageView,
    /// Motion vectors for the current frame, at render resolution
    pub motion_vectors: ImageView,
    /// Optional 1x1 texture with the exposure value
    pub exposure: Option<ImageView>,
    /// Optional resource with the alpha value of reactive objects in the scene
    pub reactive: Option<ImageView>,
    /// Optional resource with the alpha value of special objects in the scene
    pub transparency_and_composition: Option<ImageView>,
    /// Output color buffer for the current frame at presentation resolution
    pub output: ImageView,
}

/// Holds all settings for an FSR2 dispatch.
#[derive(Debug, Clone)]
pub struct Fsr2DispatchDescription {
    /// Subpixel jitter offset applied to the camera
    pub jitter_offset: FfxFloatCoords2D,
    /// Scale factor to apply to motion vectors
    pub motion_vector_scale: FfxFloatCoords2D,
    /// Enable additional sharpening
    pub enable_sharpening: bool,
    /// 0..1 value for sharpening, where 0 is no additional sharpness
    pub sharpness: f32,
    /// Delta time between this frame and the previous frame
    pub frametime_delta: Duration,
    /// Pre exposure value, must be > 0.0
    pub pre_exposure: f32,
    /// Indicates the camera has moved discontinuously
    pub reset: bool,
    /// Distance to the near plane of the camera
    pub camera_near: f32,
    /// Distance to the far plane of the camera
    pub camera_far: f32,
    /// Camera angle FOV in the vertical direction, in radians
    pub camera_fov_vertical: f32,
    /// The scale factor to convert view space units to meters
    pub viewspace_to_meters_factor: f32,
    /// Experimental reactive mask generation parameters
    pub auto_reactive: Option<Fsr2AutoReactiveDescription>,
}

extern "system" fn fsr2_message_callback(ty: FfxFsr2MsgType, message: *const wchar_t) {
    let str = unsafe { WideCStr::from_ptr_str(message) };
    match ty {
        FfxFsr2MsgType::Error => {
            error!("FSR2 Error: {}", str.display())
        }
        FfxFsr2MsgType::Warning => {
            warn!("FSR2 Warning: {}", str.display())
        }
    }
}

pub(crate) struct Fsr2ContextCreateInfo<'a> {
    pub instance: &'a ash::Instance,
    pub physical_device: vk::PhysicalDevice,
    pub device: vk::Device,
    pub flags: FfxFsr2InitializationFlagBits,
    pub max_render_size: FfxDimensions2D,
    pub display_size: FfxDimensions2D,
}

#[allow(dead_code)]
struct ReleasedFsr2Context {
    pub context: Box<FfxFsr2Context>,
    pub backend: FfxFsr2Interface,
    pub backend_data: Pooled<BackendData>,
}

impl Fsr2Context {
    /// Creates the FSR2 context.
    unsafe fn create_context(
        fp_table: FfxFsr2InstanceFunctionPointerTableVk,
        mem_pool: &Pool<BackendData>,
        device: VkDevice,
        phys_device: VkPhysicalDevice,
        flags: FfxFsr2InitializationFlagBits,
        display_size: FfxDimensions2D,
        max_render_size: FfxDimensions2D,
    ) -> Result<(Box<FfxFsr2Context>, FfxFsr2Interface, Pooled<BackendData>)> {
        // First allocate a scratch buffer for backend instance data.
        // SAFETY: We assume a valid VkPhysicalDevice was passed in.
        let scratch_size = ffxFsr2GetScratchMemorySizeVK(phys_device, &fp_table);
        let mut scratch_data = BackendData::new_in_pool(mem_pool, &scratch_size)?;
        let scratch_pointer = scratch_data.as_mut_ptr();

        // Create the backend interface. We create an uninitialized interface struct first and let the API function
        // fill it in.
        let mut interface = MaybeUninit::<FfxFsr2Interface>::uninit();
        let err = ffxFsr2GetInterfaceVK(
            interface.as_mut_ptr(),
            scratch_pointer as *mut c_void,
            scratch_size,
            phys_device,
            &fp_table,
        );
        check_fsr2_error(err)?;

        // SAFETY: We just initialized the interface using the FSR2 API call above.
        let interface = interface.assume_init();

        // Now that we have the backend interface we can create the FSR2 context. We use the same strategy to
        // defer initialization to the API as above
        let mut context = Box::<FfxFsr2Context>::new_uninit();

        // Obtain FSR2 device
        let device = ffxGetDeviceVK(device);

        let info = FfxFsr2ContextDescription {
            flags,
            max_render_size,
            display_size,
            callbacks: interface,
            device,
            fp_message: fsr2_message_callback,
        };

        let err = ffxFsr2ContextCreate(context.as_mut_ptr(), &info);
        check_fsr2_error(err)?;

        let context = context.assume_init();

        Ok((context, interface, scratch_data))
    }

    pub(crate) fn new(info: Fsr2ContextCreateInfo) -> Result<Self> {
        unsafe {
            // Build a function pointer table with vulkan functions to pass to FSR2
            let functions_1_0 = info.instance.fp_v1_0();
            let functions_1_1 = info.instance.fp_v1_1();
            let fp_table = FfxFsr2InstanceFunctionPointerTableVk {
                // SAFETY: These are the same functions, but their types are from different crates.
                fp_enumerate_device_extension_properties: std::mem::transmute::<_, _>(
                    functions_1_0.enumerate_device_extension_properties,
                ),
                fp_get_device_proc_addr: std::mem::transmute::<_, _>(
                    functions_1_0.get_device_proc_addr,
                ),
                fp_get_physical_device_memory_properties: std::mem::transmute::<_, _>(
                    functions_1_0.get_physical_device_memory_properties,
                ),
                fp_get_physical_device_properties: std::mem::transmute::<_, _>(
                    functions_1_0.get_physical_device_properties,
                ),
                fp_get_physical_device_properties2: std::mem::transmute::<_, _>(
                    functions_1_1.get_physical_device_properties2,
                ),
                fp_get_physical_device_features2: std::mem::transmute::<_, _>(
                    functions_1_1.get_physical_device_features2,
                ),
            };

            let phys_device = VkPhysicalDevice::from_raw(info.physical_device.as_raw());
            let device = VkDevice::from_raw(info.device.as_raw());

            let data_pool =
                Pool::new(|size| Ok(BackendData(Box::new_zeroed_slice(*size).assume_init())))?;

            let (context, backend, scratch) = Self::create_context(
                fp_table,
                &data_pool,
                device,
                phys_device,
                info.flags,
                info.display_size,
                info.max_render_size,
            )?;

            info!(
                "Initialized FSR2 context. FSR2 version: {}.{}.{}",
                fsr2_sys::FFX_FSR2_VERSION_MAJOR,
                fsr2_sys::FFX_FSR2_VERSION_MINOR,
                fsr2_sys::FFX_FSR2_VERSION_PATCH
            );

            Ok(Self {
                context,
                backend,
                backend_scratch_data: scratch,
                current_frame: 0,
                fp_table,
                scratch_data_pool: data_pool,
                create_flags: info.flags,
                display_size: info.display_size,
                max_render_size: info.max_render_size,
                device,
                phys_device,
                deferred_backend_delete: DeletionQueue::new(4),
            })
        }
    }

    fn get_image_resource(&mut self, image: &ImageView, state: FfxResourceState) -> FfxResource {
        unsafe {
            let image_raw = fsr2_sys::VkImage::from_raw(image.image().as_raw());
            let view_raw = fsr2_sys::VkImageView::from_raw(image.handle().as_raw());
            ffxGetTextureResourceVK(
                &mut *self.context,
                image_raw,
                view_raw,
                image.width(),
                image.height(),
                image.format().as_raw(),
                std::ptr::null(),
                state,
            )
        }
    }

    fn get_optional_image_resource(
        &mut self,
        image: &Option<ImageView>,
        state: FfxResourceState,
    ) -> FfxResource {
        image
            .as_ref()
            .map(|image| self.get_image_resource(image, state))
            .unwrap_or_else(|| FfxResource::NULL)
    }

    /// Dispatch FSR2 commands, with no additional synchronization on resources used
    pub(crate) fn dispatch<D: ExecutionDomain + ComputeSupport, A: Allocator>(
        &mut self,
        descr: &Fsr2DispatchDescription,
        resources: &Fsr2DispatchResources,
        cmd: &IncompleteCommandBuffer<D, A>,
    ) -> Result<()> {
        // Clean up old fsr2 contexts after resizes
        self.deferred_backend_delete.next_frame();
        let cmd_raw = unsafe { fsr2_sys::VkCommandBuffer::from_raw(cmd.handle().as_raw()) };
        let cmd_list = unsafe { ffxGetCommandListVK(cmd_raw) };
        if descr.auto_reactive.is_some() {
            warn!("Auto-reactive is currently not supported. Please open an issue if you would like this added.");
        }
        let description = FfxFsr2DispatchDescription {
            command_list: cmd_list,
            color: self.get_image_resource(&resources.color, FfxResourceState::COMPUTE_READ),
            depth: self.get_image_resource(&resources.depth, FfxResourceState::COMPUTE_READ),
            motion_vectors: self
                .get_image_resource(&resources.motion_vectors, FfxResourceState::COMPUTE_READ),
            exposure: self
                .get_optional_image_resource(&resources.exposure, FfxResourceState::COMPUTE_READ),
            reactive: self
                .get_optional_image_resource(&resources.reactive, FfxResourceState::COMPUTE_READ),
            transparency_and_composition: self.get_optional_image_resource(
                &resources.transparency_and_composition,
                FfxResourceState::COMPUTE_READ,
            ),
            output: self.get_image_resource(&resources.output, FfxResourceState::UNORDERED_ACCESS),
            jitter_offset: descr.jitter_offset,
            motion_vector_scale: descr.motion_vector_scale,
            // Infer render size from color resource size
            render_size: FfxDimensions2D {
                width: resources.color.width(),
                height: resources.color.height(),
            },
            enable_sharpening: descr.enable_sharpening,
            sharpness: descr.sharpness,
            frametime_delta: descr.frametime_delta.as_secs_f32() * 1000.0,
            pre_exposure: descr.pre_exposure,
            reset: descr.reset,
            camera_near: descr.camera_near,
            camera_far: descr.camera_far,
            camera_vertical_fov: descr.camera_fov_vertical,
            viewspace_to_meters_factor: descr.viewspace_to_meters_factor,
            enable_auto_reactive: false,
            color_opaque_only: FfxResource::NULL,
            auto_tc_threshold: 0.0,
            auto_tc_scale: 0.0,
            auto_reactive_scale: 0.0,
            auto_reactive_max: 0.0,
        };

        let err = unsafe { ffxFsr2ContextDispatch(&mut *self.context, &description) };
        check_fsr2_error(err)?;

        self.current_frame += 1;

        Ok(())
    }

    /// Get the number of jitter phases. Must be supplied with the current render width.
    pub fn jitter_phase_count(&mut self, render_width: u32) -> i32 {
        unsafe { ffxFsr2GetJitterPhaseCount(render_width, self.display_size.width) }
    }

    /// Get the jitter offset values for this frame, given the current render width.
    pub fn jitter_offset(&mut self, render_width: u32) -> Result<(f32, f32)> {
        let phase_count = self.jitter_phase_count(render_width);
        let index = self.current_frame % phase_count as usize;
        let mut jitter_x = 0.0;
        let mut jitter_y = 0.0;
        let error = unsafe {
            ffxFsr2GetJitterOffset(&mut jitter_x, &mut jitter_y, index as i32, phase_count as u32)
        };
        check_fsr2_error(error)?;
        Ok((jitter_x, jitter_y))
    }

    /// Get the recommended render resolution based on a quality setting and the current display resolution.
    pub fn get_render_resolution(
        &mut self,
        quality_mode: FfxFsr2QualityMode,
    ) -> Result<FfxDimensions2D> {
        let mut render_width = 0;
        let mut render_height = 0;
        let err = unsafe {
            ffxFsr2GetRenderResolutionFromQualityMode(
                &mut render_width,
                &mut render_height,
                self.display_size.width,
                self.display_size.height,
                quality_mode,
            )
        };
        check_fsr2_error(err)?;
        Ok(FfxDimensions2D {
            width: render_width,
            height: render_height,
        })
    }

    /// Set the display resolution to a new resolution. After doing this, you should also recreate the relevant
    /// attachments with a new size based on the display size. This new size may not be greater than `max_render_size`.
    /// If `max_render_size` is set to `None`, it is assumed to be equal to `display_size`.
    /// Prefer setting `max_render_size` as low as possible to save memory.
    /// If the new display size and max render size are the same as the old values, this function is a no-op.
    pub fn set_display_resolution(
        &mut self,
        display_size: FfxDimensions2D,
        max_render_size: Option<FfxDimensions2D>,
    ) -> Result<()> {
        // Create new context if something changed
        let max_render_size = max_render_size.unwrap_or(display_size);
        if display_size.width == self.display_size.width
            && display_size.height == self.display_size.height
            && self.max_render_size.width == max_render_size.width
            && self.max_render_size.height == max_render_size.height
        {
            // nothing to do
            return Ok(());
        }
        let (context, backend, scratch) = unsafe {
            Self::create_context(
                self.fp_table,
                &self.scratch_data_pool,
                self.device,
                self.phys_device,
                self.create_flags,
                display_size,
                max_render_size,
            )?
        };
        // Swap out data
        let old_scratch = std::mem::replace(&mut self.backend_scratch_data, scratch);
        let old_context = std::mem::replace(&mut self.context, context);
        let old_backend = std::mem::replace(&mut self.backend, backend);
        // Defer deletion of old context
        self.deferred_backend_delete.push(ReleasedFsr2Context {
            context: old_context,
            backend: old_backend,
            backend_data: old_scratch,
        });
        // Reset context info
        self.display_size = display_size;
        self.max_render_size = max_render_size;
        self.current_frame = 0;
        Ok(())
    }
}

unsafe impl Send for Fsr2Context {}

unsafe impl Sync for Fsr2Context {}

impl Drop for Fsr2Context {
    fn drop(&mut self) {
        unsafe {
            ffxFsr2ContextDestroy(&mut *self.context);
        }
    }
}

impl Drop for ReleasedFsr2Context {
    fn drop(&mut self) {
        unsafe {
            ffxFsr2ContextDestroy(&mut *self.context);
        }
    }
}