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
)
71 panel
[3] = VG_PROFILE_SAMPLE_COUNT
* 2;
73 float sh
= panel
[3] / VG_PROFILE_SAMPLE_COUNT
,
76 ui_fill( panel
, 0xa0000000 );
82 for( int i
=0; i
<count
; i
++ ){
83 ptrs
[i
] = profiles
[i
]->buffer_current
;
87 u32 colours
[] = { 0xff0000ff, 0xff00ff00, 0xff00ffff, 0xffff0000,
88 0xffff00ff, 0xffffff00 };
90 double rate_mul
= 1000.0 / (double)SDL_GetPerformanceFrequency();
92 for( int i
=0; i
<VG_PROFILE_SAMPLE_COUNT
-1; i
++ ){
95 for( int j
=0; j
<count
; j
++ ){
99 ptrs
[j
] = VG_PROFILE_SAMPLE_COUNT
-1;
101 double sample
= (double)profiles
[j
]->samples
[ptrs
[j
]] * rate_mul
,
102 px
= (total
/ (budget
)) * sw
,
103 wx
= (sample
/ (budget
)) * sw
;
105 ui_rect block
= { panel
[0] + px
, panel
[1] + (float)i
*sh
,
108 u32 colour
= colours
[ (j
+colour_offset
) % vg_list_size(colours
) ];
109 ui_fill( block
, colour
);
118 snprintf( infbuf
, 64, "accuracy: %.7fms", rate_mul
);
119 ui_text( (ui_rect
){ panel
[0] + 4,
120 panel
[1] + 0 * 14, 500, 30 },
123 k_ui_align_left
, 0 );
125 for( int i
=0; i
<count
; i
++ ){
126 snprintf( infbuf
, 64, "%.4fms %s",
127 avgs
[i
] * (1.0f
/(VG_PROFILE_SAMPLE_COUNT
-1)),
130 ui_text( (ui_rect
){ panel
[0] + panel
[2] + 4,
131 panel
[1] + i
* 14, 0, 0 },
134 k_ui_align_left
, 0 );
138 static void vg_profiler_init(void)
140 VG_VAR_I32( vg_profiler
, flags
=VG_VAR_PERSISTENT
);
143 #endif /* VG_PROFILER_H */