6c9530c07dc36cc7ce298aa9313136a4598d9275
5 #include "vg_platform.h"
7 #define VG_PROFILE_SAMPLE_COUNT 128
9 static int vg_profiler
= 0;
15 float samples
[ VG_PROFILE_SAMPLE_COUNT
];
16 u32 buffer_count
, buffer_current
;
25 struct timespec start
;
28 static void vg_profile_begin( struct vg_profile
*profile
)
30 clock_gettime( CLOCK_REALTIME
, &profile
->start
);
33 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 static void vg_profile_end( struct vg_profile
*profile
)
48 struct timespec time_end
;
50 clock_gettime( CLOCK_REALTIME
, &time_end
);
51 float delta
= vg_time_diff( profile
->start
, time_end
);
54 if( profile
->mode
== k_profile_mode_frame
)
56 profile
->samples
[ profile
->buffer_current
] = delta
;
57 vg_profile_increment( profile
);
61 profile
->samples
[ profile
->buffer_current
] += delta
;
65 static void vg_profile_drawn( struct vg_profile
**profiles
, u32 count
,
66 float budget
, ui_rect panel
, u32 colour_offset
)
75 panel
[3] = VG_PROFILE_SAMPLE_COUNT
* 2;
77 float sh
= panel
[3] / VG_PROFILE_SAMPLE_COUNT
,
80 float *avgs
= alloca( count
* sizeof(float) );
81 int *ptrs
= alloca( count
* sizeof(int) );
83 for( int i
=0; i
<count
; i
++ )
85 ptrs
[i
] = profiles
[i
]->buffer_current
;
89 u32 colours
[] = { 0xff0000ff, 0xff00ff00, 0xff00ffff, 0xffff0000,
90 0xffff00ff, 0xffffff00 };
92 for( int i
=0; i
<VG_PROFILE_SAMPLE_COUNT
-1; i
++ )
96 for( int j
=0; j
<count
; j
++ )
101 ptrs
[j
] = VG_PROFILE_SAMPLE_COUNT
-1;
103 float sample
= profiles
[j
]->samples
[ptrs
[j
]],
104 px
= (total
/ (budget
*0.25f
)) * sw
,
105 wx
= (sample
/ (budget
*0.25f
)) * sw
;
107 ui_rect block
= { panel
[0] + px
, panel
[1] + (float)i
*sh
,
110 u32 colour
= colours
[ (j
+colour_offset
) % vg_list_size(colours
) ];
111 ui_fill_rect( &ui_global_ctx
, block
, colour
);
120 for( int i
=0; i
<count
; i
++ )
122 snprintf( infbuf
, 64, "%.1fms %s",
123 avgs
[i
] * (1.0f
/(VG_PROFILE_SAMPLE_COUNT
-1)),
126 ui_text( &ui_global_ctx
, (ui_rect
){ panel
[0] + panel
[2] + 4,
127 panel
[1] + i
* 14, 0, 0 },
134 static void vg_profiler_init(void)
136 vg_convar_push( (struct vg_convar
){
137 .name
= "vg_profiler",
138 .data
= &vg_profiler
,
139 .data_type
= k_convar_dtype_i32
,
140 .opt_i32
= { .min
=0, .max
=1, .clamp
=1 },
145 #endif /* VG_PROFILER_H */