87de34f53dae041f4a9be1dd465c656c9ef3f222
4 #include "vg_platform.h"
6 #define VG_PROFILE_SAMPLE_COUNT 128
8 static int vg_profiler
= 0;
14 float samples
[ VG_PROFILE_SAMPLE_COUNT
];
15 u32 buffer_count
, buffer_current
;
24 struct timespec start
;
27 VG_STATIC
void vg_profile_begin( struct vg_profile
*profile
)
29 clock_gettime( CLOCK_REALTIME
, &profile
->start
);
32 VG_STATIC
void vg_profile_increment( struct vg_profile
*profile
)
34 profile
->buffer_current
++;
36 if( profile
->buffer_count
< VG_PROFILE_SAMPLE_COUNT
)
37 profile
->buffer_count
++;
39 if( profile
->buffer_current
>= VG_PROFILE_SAMPLE_COUNT
)
40 profile
->buffer_current
= 0;
42 profile
->samples
[ profile
->buffer_current
] = 0.0f
;
45 VG_STATIC
void vg_profile_end( struct vg_profile
*profile
)
47 struct timespec time_end
;
49 clock_gettime( CLOCK_REALTIME
, &time_end
);
50 float delta
= vg_time_diff( 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 for( int i
=0; i
<VG_PROFILE_SAMPLE_COUNT
-1; i
++ )
98 for( int j
=0; j
<count
; j
++ )
103 ptrs
[j
] = VG_PROFILE_SAMPLE_COUNT
-1;
105 float sample
= profiles
[j
]->samples
[ptrs
[j
]],
106 px
= (total
/ (budget
)) * sw
,
107 wx
= (sample
/ (budget
)) * sw
;
109 ui_rect block
= { panel
[0] + px
, panel
[1] + (float)i
*sh
,
112 u32 colour
= colours
[ (j
+colour_offset
) % vg_list_size(colours
) ];
113 ui_fill_rect( block
, colour
);
122 for( int i
=0; i
<count
; i
++ )
124 snprintf( infbuf
, 64, "%.4fms %s",
125 avgs
[i
] * (1.0f
/(VG_PROFILE_SAMPLE_COUNT
-1)),
128 ui_text( (ui_rect
){ panel
[0] + panel
[2] + 4,
129 panel
[1] + i
* 14, 0, 0 },
136 VG_STATIC
void vg_profiler_init(void)
138 vg_convar_push( (struct vg_convar
){
139 .name
= "vg_profiler",
140 .data
= &vg_profiler
,
141 .data_type
= k_convar_dtype_i32
,
142 .opt_i32
= { .min
=0, .max
=1, .clamp
=1 },
147 #endif /* VG_PROFILER_H */