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 VG_STATIC
void vg_profile_begin( struct vg_profile
*profile
)
31 profile
->start
= SDL_GetPerformanceCounter();
34 VG_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 VG_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
)
54 profile
->samples
[ profile
->buffer_current
] = delta
;
55 vg_profile_increment( profile
);
59 profile
->samples
[ profile
->buffer_current
] += delta
;
63 VG_STATIC
void vg_profile_drawn( struct vg_profile
**profiles
, u32 count
,
64 float budget
, ui_rect panel
, u32 colour_offset
)
73 panel
[3] = VG_PROFILE_SAMPLE_COUNT
* 2;
75 float sh
= panel
[3] / VG_PROFILE_SAMPLE_COUNT
,
78 ui_fill_rect( panel
, 0xa0000000 );
84 for( int i
=0; i
<count
; i
++ )
86 ptrs
[i
] = profiles
[i
]->buffer_current
;
90 u32 colours
[] = { 0xff0000ff, 0xff00ff00, 0xff00ffff, 0xffff0000,
91 0xffff00ff, 0xffffff00 };
93 double rate_mul
= 1000.0 / (double)SDL_GetPerformanceFrequency();
95 for( int i
=0; i
<VG_PROFILE_SAMPLE_COUNT
-1; i
++ )
99 for( int j
=0; j
<count
; j
++ )
104 ptrs
[j
] = VG_PROFILE_SAMPLE_COUNT
-1;
106 float sample
= (double)profiles
[j
]->samples
[ptrs
[j
]] * rate_mul
,
107 px
= (total
/ (budget
)) * sw
,
108 wx
= (sample
/ (budget
)) * sw
;
110 ui_rect block
= { panel
[0] + px
, panel
[1] + (float)i
*sh
,
113 u32 colour
= colours
[ (j
+colour_offset
) % vg_list_size(colours
) ];
114 ui_fill_rect( block
, colour
);
123 for( int i
=0; i
<count
; i
++ )
125 snprintf( infbuf
, 64, "%.4fms %s",
126 avgs
[i
] * (1.0f
/(VG_PROFILE_SAMPLE_COUNT
-1)),
129 ui_text( (ui_rect
){ panel
[0] + panel
[2] + 4,
130 panel
[1] + i
* 14, 0, 0 },
137 VG_STATIC
void vg_profiler_init(void)
139 vg_var_push( (struct vg_var
){
140 .name
= "vg_profiler",
141 .data
= &vg_profiler
,
142 .data_type
= k_var_dtype_i32
,
143 .opt_i32
= { .min
=0, .max
=1, .clamp
=1 },
148 #endif /* VG_PROFILER_H */