1#![warn(missing_docs)]
55#![warn(missing_doc_code_examples)]
56
57mod optick_api;
58
59#[macro_export]
72#[cfg(all(feature = "enable"))]
73macro_rules! event {
74 () => {
75 static mut _OPTICK_EVENT_DESCRIPTION : u64 = 0;
76 let mut _optick_counter = $crate::OptickCounter { event_data: 0 };
77 unsafe {
78 if _OPTICK_EVENT_DESCRIPTION == 0 {
79 _OPTICK_EVENT_DESCRIPTION = $crate::create_description($crate::function!(), file!(), line!());
80 }
81 _optick_counter.event_data = $crate::push_event(_OPTICK_EVENT_DESCRIPTION);
82 }
83 };
84 ($name:expr) => {
85 static mut _OPTICK_EVENT_DESCRIPTION : u64 = 0;
86 let mut _optick_counter = $crate::OptickCounter { event_data: 0 };
87 unsafe {
88 if _OPTICK_EVENT_DESCRIPTION == 0 {
89 _OPTICK_EVENT_DESCRIPTION = $crate::create_description($name, file!(), line!());
90 }
91 _optick_counter.event_data = $crate::push_event(_OPTICK_EVENT_DESCRIPTION);
92 }
93 };
94}
95
96
97#[macro_export]
112#[cfg(all(feature = "enable"))]
113macro_rules! tag {
114 ($name:expr, $value:expr) => {{
115 use $crate::OptickTag;
116 static mut _OPTICK_EVENT_DESCRIPTION : u64 = 0;
117 let mut _optick_counter = $crate::OptickCounter { event_data: 0 };
118 unsafe {
119 if _OPTICK_EVENT_DESCRIPTION == 0 {
120 _OPTICK_EVENT_DESCRIPTION = $crate::create_description($name, file!(), line!());
121 }
122 $value.attach(_OPTICK_EVENT_DESCRIPTION);
123 }}
124 };
125}
126
127#[macro_export]
129#[cfg(all(feature = "enable"))]
130macro_rules! function {
131 () => {{
132 fn f() {}
133 fn type_name_of<T>(_: T) -> &'static str {
134 std::any::type_name::<T>()
135 }
136 let name = type_name_of(f);
137 &name[..name.len() - 3]
138 }}
139}
140
141#[cfg(all(feature = "enable"))]
143pub struct OptickCounter {
144 pub event_data : u64,
146}
147
148#[cfg(all(feature = "enable"))]
150impl Drop for OptickCounter {
151 #[inline(always)]
152 fn drop(&mut self) {
153 if self.event_data != 0 {
154 pop_event(self.event_data);
155 }
156 }
157}
158
159pub const fn enabled() -> bool {
161 cfg!(all(feature = "enable"))
162}
163
164pub fn create_description(name: &str, file: &str, line: u32) -> u64 {
166 #[cfg(all(feature = "enable"))]
167 unsafe {
168 optick_api::OptickAPI_CreateEventDescription(
169 name.as_ptr() as *const i8,
170 name.len() as u16,
171 file.as_ptr() as *const i8,
172 file.len() as u16,
173 line
174 )
175 }
176}
177
178pub fn push_event(_description: u64) -> u64 {
180 #[cfg(all(feature = "enable"))]
181 unsafe {
182 optick_api::OptickAPI_PushEvent(_description)
183 }
184}
185
186pub fn pop_event(_event_data: u64) {
188 #[cfg(all(feature = "enable"))]
189 unsafe {
190 optick_api::OptickAPI_PopEvent(_event_data);
191 }
192}
193
194pub fn next_frame() {
205 #[cfg(all(feature = "enable"))]
206 unsafe {
207 static mut _OPTICK_INIT_ONCE : bool = false;
208 if _OPTICK_INIT_ONCE == false {
209 register_thread("MainThread");
210 _OPTICK_INIT_ONCE = true;
211 }
212 optick_api::OptickAPI_NextFrame();
213 }
214}
215
216pub fn register_thread(thread: &str) {
224 #[cfg(all(feature = "enable"))]
225 unsafe {
226 optick_api::OptickAPI_RegisterThread(
227 thread.as_ptr() as *const i8,
228 thread.len() as u16,
229 )
230 }
231}
232
233pub fn start_capture() {
235 #[cfg(all(feature = "enable"))]
236 {
237 register_thread("MainThread");
238 unsafe {
239 optick_api::OptickAPI_StartCapture();
240 }
241 }
242}
243
244pub fn stop_capture(path: &str) {
256 #[cfg(all(feature = "enable"))]
257 unsafe {
258 optick_api::OptickAPI_StopCapture(path.as_ptr() as *const i8, path.len() as u16);
259 }
260}
261
262pub trait OptickTag {
264 fn attach(&self, _description: u64);
266}
267
268impl OptickTag for &str {
270 fn attach(&self, description: u64) {
271 unsafe {
272 optick_api::OptickAPI_AttachTag_String(description, (*self).as_ptr() as *const i8, (*self).len() as u16);
273 }
274 }
275}
276
277impl OptickTag for String {
279 fn attach(&self, description: u64) {
280 unsafe {
281 optick_api::OptickAPI_AttachTag_String(description, (*self).as_ptr() as *const i8, (*self).len() as u16);
282 }
283 }
284}
285
286impl OptickTag for i32 {
288 fn attach(&self, description: u64) {
289 unsafe {
290 optick_api::OptickAPI_AttachTag_Int32(description, *self);
291 }
292 }
293}
294
295impl OptickTag for u32 {
297 fn attach(&self, description: u64) {
298 unsafe {
299 optick_api::OptickAPI_AttachTag_UInt32(description, *self);
300 }
301 }
302}
303
304impl OptickTag for u64 {
306 fn attach(&self, description: u64) {
307 unsafe {
308 optick_api::OptickAPI_AttachTag_UInt64(description, *self);
309 }
310 }
311}
312
313impl OptickTag for f32 {
315 fn attach(&self, description: u64) {
316 unsafe {
317 optick_api::OptickAPI_AttachTag_Float(description, *self);
318 }
319 }
320}
321
322impl OptickTag for (f32, f32, f32) {
324 fn attach(&self, description: u64) {
325 unsafe {
326 optick_api::OptickAPI_AttachTag_Point(description, (*self).0, (*self).1, (*self).2);
327 }
328 }
329}
330
331