Skip to content

Commit 2bd9388

Browse files
committed
Add ProfilerCounter
1 parent 01aadcc commit 2bd9388

File tree

1 file changed

+43
-1
lines changed

1 file changed

+43
-1
lines changed

unity-native-plugin/src/profiler.rs

Lines changed: 43 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
use std::marker::PhantomData;
2+
use std::ptr::null_mut;
13
use crate::define_unity_interface;
24
use crate::interface::UnityInterface;
35
use crate::bitflag;
@@ -377,6 +379,24 @@ define_unity_interface!(
377379

378380
pub type ProfilerCounterStatePtrCallback = UnityProfilerCounterStatePtrCallback;
379381

382+
pub struct ProfilerCounter<T> {
383+
pub(crate) counter: *mut ::std::os::raw::c_void,
384+
}
385+
386+
impl<T> ProfilerCounter<T> {
387+
pub fn value(&self) -> &T {
388+
unsafe {
389+
self.counter as &T
390+
}
391+
}
392+
393+
pub fn value_mut(&mut self) -> &mut T {
394+
unsafe {
395+
self.counter as &mut T
396+
}
397+
}
398+
}
399+
380400
macro_rules! impl_profiler_v2 {
381401
() => {
382402
impl_profiler!();
@@ -404,12 +424,34 @@ macro_rules! impl_profiler_v2 {
404424
activate_func: ProfilerCounterStatePtrCallback,
405425
deactivate_func: ProfilerCounterStatePtrCallback,
406426
user_data: *mut ::std::os::raw::c_void) -> *mut ::std::os::raw::c_void {
407-
self.interface().CreateCounterValue.expect("CreateCounterValue")(category, name.as_ptr(), flags.into(), value_type.into(), value_unit.into(), value_size, counter_flags.into(), activate_func, deactivate_func, user_data)
427+
self.interface().CreateCounterValue.expect("CreateCounterValue")(category, name.as_ptr(), flags.into(), value_type as u8, value_unit as u8, value_size, counter_flags.into(), activate_func, deactivate_func, user_data)
408428
}
409429

410430
pub unsafe fn flush_counter_value(&self, counter: *mut ::std::os::raw::c_void) {
411431
self.interface().FlushCounterValue.expect("FlushCounterValue")(counter)
412432
}
433+
434+
pub unsafe fn create_counter<T>(&self,
435+
category: ProfilerCategoryId,
436+
name: &std::ffi::CStr,
437+
flags: ProfilerMarkerFlags,
438+
value_type: ProfilerMarkerDataType,
439+
value_unit: ProfilerMarkerDataUnit,
440+
counter_flags: ProfilerCounterFlags,
441+
activate_func: ProfilerCounterStatePtrCallback,
442+
deactivate_func: ProfilerCounterStatePtrCallback,
443+
user_data: *mut ::std::os::raw::c_void) -> Option<ProfilerCounter<T>> {
444+
let r = self.create_counter_value(category, name, flags, value_type, value_unit, std::mem::size_of::<T>(), counter_flags.into(), activate_func, deactivate_func, user_data);
445+
if r != null_mut() {
446+
Some(ProfilerCounter::<T> { counter: r })
447+
} else {
448+
None
449+
}
450+
}
451+
452+
pub unsafe fn flush_counter<T>(&self, counter: &mut ProfilerCounter<T>) {
453+
self.flush_counter_value(counter.counter);
454+
}
413455
}
414456
}
415457

0 commit comments

Comments
 (0)