1 #include "vg_platform.h"
2 #include "vg_profiler.h"
8 void vg_profile_begin( struct vg_profile
*profile
)
10 profile
->start
= SDL_GetPerformanceCounter();
13 void vg_profile_increment( struct vg_profile
*profile
)
15 profile
->buffer_current
++;
17 if( profile
->buffer_count
< VG_PROFILE_SAMPLE_COUNT
)
18 profile
->buffer_count
++;
20 if( profile
->buffer_current
>= VG_PROFILE_SAMPLE_COUNT
)
21 profile
->buffer_current
= 0;
23 profile
->samples
[ profile
->buffer_current
] = 0;
26 void vg_profile_end( struct vg_profile
*profile
)
28 u64 time_end
= SDL_GetPerformanceCounter(),
29 delta
= time_end
- profile
->start
;
31 if( profile
->mode
== k_profile_mode_frame
){
32 profile
->samples
[ profile
->buffer_current
] = delta
;
33 vg_profile_increment( profile
);
36 profile
->samples
[ profile
->buffer_current
] += delta
;
40 void vg_profile_drawn( struct vg_profile
**profiles
, u32 count
,
41 f64 budget
, ui_rect panel
,
42 int dir
, i32 normalize
)
48 panel
[3] = VG_PROFILE_SAMPLE_COUNT
* 2;
50 f64 sh
= (f32
)panel
[3^dir
] / (f32
)VG_PROFILE_SAMPLE_COUNT
,
51 sw
= (f32
)panel
[2^dir
];
53 ui_fill( panel
, 0xa0000000 );
55 if( count
> 8 ) vg_fatal_error( "Too many profiles\n" );
59 for( u32 i
=0; i
<count
; i
++ ){
61 colours
[i
] = ui_colour( k_ui_red
+ ((i
*3)&0xe) );
64 f64 rate_mul
= 1000.0 / (f64
)SDL_GetPerformanceFrequency();
66 for( i32 i
=0; i
<VG_PROFILE_SAMPLE_COUNT
; i
++ ){
71 for( u32 j
=0; j
<count
; j
++ )
72 budget
+= (f64
)profiles
[j
]->samples
[i
] * rate_mul
;
75 for( int j
=0; j
<count
; j
++ ){
76 f64 sample
= (f64
)profiles
[j
]->samples
[i
] * rate_mul
,
77 px
= (total
/ budget
) * sw
,
78 wx
= (sample
/ budget
) * sw
;
81 block
[0^dir
] = panel
[0^dir
] + px
;
82 block
[1^dir
] = panel
[1^dir
] + (f32
)i
*sh
;
83 block
[2^dir
] = VG_MAX( 1, wx
-1 );
84 block
[3^dir
] = ceilf(sh
)-1;
85 ui_fill( block
, colours
[j
] );
94 snprintf( infbuf
, 64, "accuracy: %.7fms", rate_mul
);
95 ui_text( (ui_rect
){ panel
[0] + 4,
96 panel
[1] + panel
[3] - 14, 500, 30 },
101 for( int i
=0; i
<count
; i
++ ){
102 snprintf( infbuf
, 64, "%.4fms %s",
103 avgs
[i
] * (1.0f
/(VG_PROFILE_SAMPLE_COUNT
-1)),
106 ui_text( (ui_rect
){ panel
[0] + 4,
107 panel
[1] + panel
[3] + 4 + i
*14,
109 infbuf
, 1, k_ui_align_left
, 0 );
113 void vg_profiler_init(void)
115 VG_VAR_I32( vg_profiler
, flags
=VG_VAR_PERSISTENT
);