5 #include "vg_platform.h"
7 #define VG_PROFILE_SAMPLE_COUNT 128
9 static int vg_profiler
= 0;
15 u32 samples
[ VG_PROFILE_SAMPLE_COUNT
];
16 u32 buffer_count
, buffer_current
;
28 VG_STATIC
void vg_profile_begin( struct vg_profile
*profile
)
30 profile
->start
= SDL_GetPerformanceCounter();
33 VG_STATIC
void vg_profile_increment( struct vg_profile
*profile
)
35 profile
->buffer_current
++;
37 if( profile
->buffer_count
< VG_PROFILE_SAMPLE_COUNT
)
38 profile
->buffer_count
++;
40 if( profile
->buffer_current
>= VG_PROFILE_SAMPLE_COUNT
)
41 profile
->buffer_current
= 0;
43 profile
->samples
[ profile
->buffer_current
] = 0.0f
;
46 VG_STATIC
void vg_profile_end( struct vg_profile
*profile
)
50 time_end
= SDL_GetPerformanceCounter();
51 u64 delta
= profile
->start
- time_end
;
53 if( profile
->mode
== k_profile_mode_frame
)
55 profile
->samples
[ profile
->buffer_current
] = delta
;
56 vg_profile_increment( profile
);
60 profile
->samples
[ profile
->buffer_current
] += delta
;
64 VG_STATIC
void vg_profile_drawn( struct vg_profile
**profiles
, u32 count
,
65 float budget
, ui_rect panel
, u32 colour_offset
)
74 panel
[3] = VG_PROFILE_SAMPLE_COUNT
* 2;
76 float sh
= panel
[3] / VG_PROFILE_SAMPLE_COUNT
,
79 ui_fill_rect( panel
, 0xa0000000 );
85 for( int i
=0; i
<count
; i
++ )
87 ptrs
[i
] = profiles
[i
]->buffer_current
;
91 u32 colours
[] = { 0xff0000ff, 0xff00ff00, 0xff00ffff, 0xffff0000,
92 0xffff00ff, 0xffffff00 };
94 float rate_mul
= 1.0f
/ (float)SDL_GetPerformanceFrequency();
96 for( int i
=0; i
<VG_PROFILE_SAMPLE_COUNT
-1; i
++ )
100 for( int j
=0; j
<count
; j
++ )
105 ptrs
[j
] = VG_PROFILE_SAMPLE_COUNT
-1;
107 float sample
= (float)profiles
[j
]->samples
[ptrs
[j
]] * rate_mul
,
108 px
= (total
/ (budget
)) * sw
,
109 wx
= (sample
/ (budget
)) * sw
;
111 ui_rect block
= { panel
[0] + px
, panel
[1] + (float)i
*sh
,
114 u32 colour
= colours
[ (j
+colour_offset
) % vg_list_size(colours
) ];
115 ui_fill_rect( block
, colour
);
124 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 },
138 VG_STATIC
void vg_profiler_init(void)
140 vg_convar_push( (struct vg_convar
){
141 .name
= "vg_profiler",
142 .data
= &vg_profiler
,
143 .data_type
= k_convar_dtype_i32
,
144 .opt_i32
= { .min
=0, .max
=1, .clamp
=1 },
149 #endif /* VG_PROFILER_H */