6 #include "vg_platform.h"
8 #define VG_PROFILE_SAMPLE_COUNT 128
10 static int vg_profiler
= 0;
16 u64 samples
[ VG_PROFILE_SAMPLE_COUNT
];
17 u32 buffer_count
, buffer_current
;
29 static void vg_profile_begin( struct vg_profile
*profile
)
31 profile
->start
= SDL_GetPerformanceCounter();
34 static void vg_profile_increment( struct vg_profile
*profile
)
36 profile
->buffer_current
++;
38 if( profile
->buffer_count
< VG_PROFILE_SAMPLE_COUNT
)
39 profile
->buffer_count
++;
41 if( profile
->buffer_current
>= VG_PROFILE_SAMPLE_COUNT
)
42 profile
->buffer_current
= 0;
44 profile
->samples
[ profile
->buffer_current
] = 0;
47 static void vg_profile_end( struct vg_profile
*profile
)
49 u64 time_end
= SDL_GetPerformanceCounter(),
50 delta
= time_end
- profile
->start
;
52 if( profile
->mode
== k_profile_mode_frame
){
53 profile
->samples
[ profile
->buffer_current
] = delta
;
54 vg_profile_increment( profile
);
57 profile
->samples
[ profile
->buffer_current
] += delta
;
61 static void vg_profile_drawn( struct vg_profile
**profiles
, u32 count
,
62 float budget
, ui_rect panel
, u32 colour_offset
,
69 panel
[3] = VG_PROFILE_SAMPLE_COUNT
* 2;
71 f32 sh
= (f32
)panel
[3^dir
] / (f32
)VG_PROFILE_SAMPLE_COUNT
,
72 sw
= (f32
)panel
[2^dir
];
74 ui_fill( panel
, 0xa0000000 );
80 for( int i
=0; i
<count
; i
++ ){
82 ptrs
[i
] = profiles
[i
]->buffer_current
;
89 u32 colours
[] = { 0xff0000ff, 0xff00ff00, 0xff00ffff, 0xffff0000,
90 0xffff00ff, 0xffffff00 };
92 f64 rate_mul
= 1000.0 / (f64
)SDL_GetPerformanceFrequency();
94 for( int i
=0; i
<VG_PROFILE_SAMPLE_COUNT
-1; i
++ ){
97 for( int j
=0; j
<count
; j
++ ){
102 ptrs
[j
] = VG_PROFILE_SAMPLE_COUNT
-1;
107 f64 sample
= (f64
)profiles
[j
]->samples
[ptrs
[j
]] * rate_mul
,
108 px
= (total
/ (budget
)) * sw
,
109 wx
= (sample
/ (budget
)) * sw
;
112 block
[0^dir
] = panel
[0^dir
] + px
;
113 block
[1^dir
] = panel
[1^dir
] + (f32
)i
*sh
;
115 block
[3^dir
] = ceilf(sh
)-1;
117 u32 colour
= colours
[ (j
+colour_offset
) % vg_list_size(colours
) ];
118 ui_fill( block
, colour
);
127 snprintf( infbuf
, 64, "accuracy: %.7fms", rate_mul
);
128 ui_text( (ui_rect
){ panel
[0] + 4,
129 panel
[1] + panel
[3] - 14, 500, 30 },
132 k_ui_align_left
, 0 );
134 for( int i
=0; i
<count
; i
++ ){
135 snprintf( infbuf
, 64, "%.4fms %s",
136 avgs
[i
] * (1.0f
/(VG_PROFILE_SAMPLE_COUNT
-1)),
139 ui_text( (ui_rect
){ panel
[0] + panel
[2] + 4,
140 panel
[1] + i
* 14, 0, 0 },
143 k_ui_align_left
, 0 );
147 static void vg_profiler_init(void)
149 VG_VAR_I32( vg_profiler
, flags
=VG_VAR_PERSISTENT
);
152 #endif /* VG_PROFILER_H */