699e2a72eadfdacac40abac419c53737554c34dd
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 f64 budget
, ui_rect panel
,
63 int dir
, i32 normalize
)
69 panel
[3] = VG_PROFILE_SAMPLE_COUNT
* 2;
71 f64 sh
= (f32
)panel
[3^dir
] / (f32
)VG_PROFILE_SAMPLE_COUNT
,
72 sw
= (f32
)panel
[2^dir
];
74 ui_fill( panel
, 0xa0000000 );
79 for( u32 i
=0; i
<count
; i
++ ){
81 colours
[i
] = ui_colour( k_ui_red
+ ((i
*3)&0xe) );
84 f64 rate_mul
= 1000.0 / (f64
)SDL_GetPerformanceFrequency();
86 for( i32 i
=0; i
<VG_PROFILE_SAMPLE_COUNT
; i
++ ){
91 for( u32 j
=0; j
<count
; j
++ )
92 budget
+= (f64
)profiles
[j
]->samples
[i
] * rate_mul
;
95 for( int j
=0; j
<count
; j
++ ){
96 f64 sample
= (f64
)profiles
[j
]->samples
[i
] * rate_mul
,
97 px
= (total
/ budget
) * sw
,
98 wx
= (sample
/ budget
) * sw
;
101 block
[0^dir
] = panel
[0^dir
] + px
;
102 block
[1^dir
] = panel
[1^dir
] + (f32
)i
*sh
;
103 block
[2^dir
] = VG_MAX( 1, wx
-1 );
104 block
[3^dir
] = ceilf(sh
)-1;
105 ui_fill( block
, colours
[j
] );
114 snprintf( infbuf
, 64, "accuracy: %.7fms", rate_mul
);
115 ui_text( (ui_rect
){ panel
[0] + 4,
116 panel
[1] + panel
[3] - 14, 500, 30 },
119 k_ui_align_left
, 0 );
121 for( int i
=0; i
<count
; i
++ ){
122 snprintf( infbuf
, 64, "%.4fms %s",
123 avgs
[i
] * (1.0f
/(VG_PROFILE_SAMPLE_COUNT
-1)),
126 ui_text( (ui_rect
){ panel
[0] + 4,
127 panel
[1] + panel
[3] + 4 + i
*14,
129 infbuf
, 1, k_ui_align_left
, 0 );
133 static void vg_profiler_init(void)
135 VG_VAR_I32( vg_profiler
, flags
=VG_VAR_PERSISTENT
);
138 #endif /* VG_PROFILER_H */