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
#![warn(
    trivial_casts,
    trivial_numeric_casts,
    unused_extern_crates,
    unused_import_braces,
    unused_qualifications
)]

//! RenderDoc integration - https://renderdoc.org/

/// The dynamically loaded RenderDoc API function table
#[repr(C)]
#[derive(Debug)]
pub struct RenderDocApi {
    api: renderdoc_sys::RENDERDOC_API_1_4_1,
    lib: libloading::Library,
}

unsafe impl Send for RenderDocApi {}

unsafe impl Sync for RenderDocApi {}

/// RenderDoc API type
#[derive(Debug)]
pub enum RenderDoc {
    /// RenderDoc functionality is available
    Available {
        /// RenderDoc API with function pointers
        api: RenderDocApi,
    },
    /// RenderDoc functionality is _not_ available
    NotAvailable {
        /// A description why renderdoc functionality is not available
        reason: String,
    },
}

impl RenderDoc {
    pub unsafe fn new() -> Self {
        type GetApiFn = unsafe extern "C" fn(version: u32, out: *mut *mut std::ffi::c_void) -> i32;

        #[cfg(windows)]
        let renderdoc_filename = "renderdoc.dll";
        #[cfg(all(unix, not(target_os = "android")))]
        let renderdoc_filename = "librenderdoc.so";
        #[cfg(target_os = "android")]
        let renderdoc_filename = "libVkLayer_GLES_RenderDoc.so";

        let renderdoc_lib = match libloading::Library::new(renderdoc_filename) {
            Ok(lib) => lib,
            Err(e) => {
                return RenderDoc::NotAvailable {
                    reason: format!(
                        "Unable to load renderdoc library '{}': {:?}",
                        renderdoc_filename, e
                    ),
                }
            }
        };

        let get_api: libloading::Symbol<GetApiFn> = match renderdoc_lib.get(b"RENDERDOC_GetAPI\0") {
            Ok(api) => api,
            Err(e) => {
                return RenderDoc::NotAvailable {
                    reason: format!(
                        "Unable to get RENDERDOC_GetAPI from renderdoc library '{}': {:?}",
                        renderdoc_filename, e
                    ),
                }
            }
        };
        let mut obj = std::ptr::null_mut();
        match get_api(10401, &mut obj) {
            1 => RenderDoc::Available {
                api: RenderDocApi {
                    api: *(obj as *mut renderdoc_sys::RENDERDOC_API_1_4_1),
                    lib: renderdoc_lib,
                },
            },
            return_value => RenderDoc::NotAvailable {
                reason: format!(
                    "Unable to get API from renderdoc library '{}': {}",
                    renderdoc_filename, return_value
                ),
            },
        }
    }
}

impl Default for RenderDoc {
    fn default() -> Self {
        if !cfg!(debug_assertions) {
            return RenderDoc::NotAvailable {
                reason: "RenderDoc support is only enabled with 'debug_assertions'".into(),
            };
        }
        unsafe { Self::new() }
    }
}
/// A implementation specific handle
pub type Handle = *mut ::std::os::raw::c_void;

impl RenderDoc {
    /// Start a RenderDoc frame capture
    pub unsafe fn start_frame_capture(&self, device_handle: Handle, window_handle: Handle) {
        match self {
            Self::Available { api: ref entry } => {
                entry.api.StartFrameCapture.unwrap()(device_handle, window_handle);
            }
            Self::NotAvailable { ref reason } => {
                log::warn!("Could not start RenderDoc frame capture: {}", reason)
            }
        };
    }

    /// End a RenderDoc frame capture
    pub unsafe fn end_frame_capture(&self, device_handle: Handle, window_handle: Handle) {
        match self {
            Self::Available { api: ref entry } => {
                entry.api.EndFrameCapture.unwrap()(device_handle, window_handle);
            }
            Self::NotAvailable { ref reason } => {
                log::warn!("Could not end RenderDoc frame capture: {}", reason)
            }
        };
    }
}