Last active
September 30, 2024 06:25
-
-
Save polymonster/28896d761c3592bab48a94924c0d7406 to your computer and use it in GitHub Desktop.
Calling WinPixEvenRuntime ABI from Rust and windows-rs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#[derive(Copy, Clone)] | |
struct WinPixEventRuntime { | |
begin_event: BeginEventOnCommandList, | |
end_event: EndEventOnCommandList, | |
set_marker: SetMarkerOnCommandList, | |
} | |
impl WinPixEventRuntime { | |
pub fn create() -> Option<WinPixEventRuntime> { | |
unsafe { | |
let module = LoadLibraryA("WinPixEventRuntime.dll\0"); | |
let p_begin_event = GetProcAddress(module, "PIXBeginEventOnCommandList\0"); | |
let p_end_event = GetProcAddress(module, "PIXEndEventOnCommandList\0"); | |
let p_set_marker = GetProcAddress(module, "PIXSetMarkerOnCommandList\0"); | |
if p_begin_event.is_some() && p_end_event.is_some() && p_set_marker.is_some() { | |
return Some(WinPixEventRuntime { | |
begin_event: std::mem::transmute::<*const usize, BeginEventOnCommandList>( | |
p_begin_event.unwrap() as *const usize), | |
end_event: std::mem::transmute::<*const usize, EndEventOnCommandList>( | |
p_end_event.unwrap() as *const usize), | |
set_marker: std::mem::transmute::<*const usize, SetMarkerOnCommandList>( | |
p_set_marker.unwrap() as *const usize), | |
}); | |
} | |
None | |
} | |
} | |
pub fn begin_event_on_command_list(&self, command_list: ID3D12GraphicsCommandList, color: u64, name: &str) { | |
unsafe { | |
let null_name = CString::new(name).unwrap(); | |
let fn_begin_event : BeginEventOnCommandList = self.begin_event; | |
let p_cmd_list = std::mem::transmute::<IUnknown, *const core::ffi::c_void>(command_list.0.clone()); | |
fn_begin_event(p_cmd_list, color, PSTR(null_name.as_ptr() as _)); | |
} | |
} | |
pub fn end_event_on_command_list(&self, command_list: ID3D12GraphicsCommandList) { | |
unsafe { | |
let fn_end_event : EndEventOnCommandList = self.end_event; | |
let p_cmd_list = std::mem::transmute::<IUnknown, *const core::ffi::c_void>(command_list.0.clone()); | |
fn_end_event(p_cmd_list); | |
} | |
} | |
pub fn set_marker_on_command_list(&self, command_list: ID3D12GraphicsCommandList, color: u64, name: &str) { | |
unsafe { | |
let null_name = CString::new(name).unwrap(); | |
let fn_set_marker : SetMarkerOnCommandList = self.set_marker; | |
let p_cmd_list = std::mem::transmute::<IUnknown, *const core::ffi::c_void>(command_list.0.clone()); | |
fn_set_marker(p_cmd_list, color, PSTR(null_name.as_ptr() as _)); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment