| 1 |
//! RenderDoc integration - <https://renderdoc.org/> |
| 2 |
#![cfg_attr(not(any(feature = "gles", feature = "vulkan")), allow(dead_code))] |
| 3 |
|
| 4 |
use alloc::format; |
| 5 |
use alloc::string::String; |
| 6 |
use core::{ffi, ptr}; |
| 7 |
|
| 8 |
/// The dynamically loaded RenderDoc API function table |
| 9 |
#[repr(C)] |
| 10 |
#[derive(Debug)] |
| 11 |
pub struct RenderDocApi { |
| 12 |
api: renderdoc_sys::RENDERDOC_API_1_4_1, |
| 13 |
lib: libloading::Library, |
| 14 |
} |
| 15 |
|
| 16 |
unsafe impl Send for RenderDocApi {} |
| 17 |
unsafe impl Sync for RenderDocApi {} |
| 18 |
|
| 19 |
/// RenderDoc API type |
| 20 |
#[derive(Debug)] |
| 21 |
pub enum RenderDoc { |
| 22 |
/// RenderDoc functionality is available |
| 23 |
Available { |
| 24 |
/// RenderDoc API with function pointers |
| 25 |
api: RenderDocApi, |
| 26 |
}, |
| 27 |
/// RenderDoc functionality is _not_ available |
| 28 |
NotAvailable { |
| 29 |
/// A description why renderdoc functionality is not available |
| 30 |
reason: String, |
| 31 |
}, |
| 32 |
} |
| 33 |
|
| 34 |
// TODO: replace with libloading API once supported |
| 35 |
#[cfg(unix)] |
| 36 |
const RTLD_NOLOAD: i32 = 0x4; |
| 37 |
|
| 38 |
impl RenderDoc { |
| 39 |
pub unsafe fn new() -> Self { |
| 40 |
type GetApiFn = unsafe extern "C" fn(version: u32, out: *mut *mut ffi::c_void) -> i32; |
| 41 |
|
| 42 |
#[cfg(windows)] |
| 43 |
let renderdoc_filename = "renderdoc.dll"; |
| 44 |
#[cfg(all(unix, not(target_os = "android")))] |
| 45 |
let renderdoc_filename = "librenderdoc.so"; |
| 46 |
#[cfg(target_os = "android")] |
| 47 |
let renderdoc_filename = "libVkLayer_GLES_RenderDoc.so"; |
| 48 |
|
| 49 |
#[cfg(unix)] |
| 50 |
let renderdoc_result: Result<libloading::Library, libloading::Error> = unsafe { |
| 51 |
libloading::os::unix::Library::open( |
| 52 |
Some(renderdoc_filename), |
| 53 |
libloading::os::unix::RTLD_NOW | RTLD_NOLOAD, |
| 54 |
) |
| 55 |
} |
| 56 |
.map(|lib| lib.into()); |
| 57 |
|
| 58 |
#[cfg(windows)] |
| 59 |
let renderdoc_result: Result<libloading::Library, libloading::Error> = |
| 60 |
libloading::os::windows::Library::open_already_loaded(renderdoc_filename) |
| 61 |
.map(|lib| lib.into()); |
| 62 |
|
| 63 |
let renderdoc_lib = match renderdoc_result { |
| 64 |
Ok(lib) => lib, |
| 65 |
Err(e) => { |
| 66 |
return RenderDoc::NotAvailable { |
| 67 |
reason: format!( |
| 68 |
"Unable to load renderdoc library '{renderdoc_filename}': {e:?}" |
| 69 |
), |
| 70 |
} |
| 71 |
} |
| 72 |
}; |
| 73 |
|
| 74 |
let get_api: libloading::Symbol<GetApiFn> = |
| 75 |
match unsafe { renderdoc_lib.get(c"RENDERDOC_GetAPI".to_bytes()) } { |
| 76 |
Ok(api) => api, |
| 77 |
Err(e) => { |
| 78 |
return RenderDoc::NotAvailable { |
| 79 |
reason: format!( |
| 80 |
"Unable to get RENDERDOC_GetAPI from renderdoc library '{}': {e:?}", |
| 81 |
renderdoc_filename |
| 82 |
), |
| 83 |
} |
| 84 |
} |
| 85 |
}; |
| 86 |
let mut obj = ptr::null_mut(); |
| 87 |
match unsafe { get_api(10401, &mut obj) } { |
| 88 |
1 => RenderDoc::Available { |
| 89 |
api: RenderDocApi { |
| 90 |
api: unsafe { *obj.cast::<renderdoc_sys::RENDERDOC_API_1_4_1>() }, |
| 91 |
lib: renderdoc_lib, |
| 92 |
}, |
| 93 |
}, |
| 94 |
return_value => RenderDoc::NotAvailable { |
| 95 |
reason: format!( |
| 96 |
"Unable to get API from renderdoc library '{}': {}", |
| 97 |
renderdoc_filename, return_value |
| 98 |
), |
| 99 |
}, |
| 100 |
} |
| 101 |
} |
| 102 |
} |
| 103 |
|
| 104 |
impl Default for RenderDoc { |
| 105 |
fn default() -> Self { |
| 106 |
if !cfg!(debug_assertions) { |
| 107 |
return RenderDoc::NotAvailable { |
| 108 |
reason: "RenderDoc support is only enabled with 'debug_assertions'".into(), |
| 109 |
}; |
| 110 |
} |
| 111 |
unsafe { Self::new() } |
| 112 |
} |
| 113 |
} |
| 114 |
/// An implementation specific handle |
| 115 |
pub type Handle = *mut ffi::c_void; |
| 116 |
|
| 117 |
impl RenderDoc { |
| 118 |
/// Start a RenderDoc frame capture |
| 119 |
pub unsafe fn start_frame_capture(&self, device_handle: Handle, window_handle: Handle) -> bool { |
| 120 |
match *self { |
| 121 |
Self::Available { api: ref entry } => { |
| 122 |
unsafe { entry.api.StartFrameCapture.unwrap()(device_handle, window_handle) }; |
| 123 |
true |
| 124 |
} |
| 125 |
Self::NotAvailable { ref reason } => { |
| 126 |
log::warn!("Could not start RenderDoc frame capture: {}", reason); |
| 127 |
false |
| 128 |
} |
| 129 |
} |
| 130 |
} |
| 131 |
|
| 132 |
/// End a RenderDoc frame capture |
| 133 |
pub unsafe fn end_frame_capture(&self, device_handle: Handle, window_handle: Handle) { |
| 134 |
match *self { |
| 135 |
Self::Available { api: ref entry } => { |
| 136 |
unsafe { entry.api.EndFrameCapture.unwrap()(device_handle, window_handle) }; |
| 137 |
} |
| 138 |
Self::NotAvailable { ref reason } => { |
| 139 |
log::warn!("Could not end RenderDoc frame capture: {}", reason) |
| 140 |
} |
| 141 |
}; |
| 142 |
} |
| 143 |
} |