summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorMitya Selivanov <automainint@guattari.tech>2025-04-07 08:02:24 +0200
committerMitya Selivanov <automainint@guattari.tech>2025-04-07 08:02:24 +0200
commitaf8c9212a029ddaf8e7eef66cecaa762263ca0c5 (patch)
tree3453d3e0eeacd6c3e496aba1e66e11ae3504a590
parent3a9715a7a5cc03fef0f99064601112c093ddaae1 (diff)
downloadreduced_system_layer-af8c9212a029ddaf8e7eef66cecaa762263ca0c5.zip
Add graphics profiling
-rwxr-xr-xgraphics.c38
-rwxr-xr-xreduced_system_layer.c72
2 files changed, 84 insertions, 26 deletions
diff --git a/graphics.c b/graphics.c
index e46dca5..79e3a6c 100755
--- a/graphics.c
+++ b/graphics.c
@@ -333,6 +333,8 @@ void draw_pixels_to_buffer(Pixel_Buffer dst, Box area, Pixel_Buffer src) {
if (area.width < EPSILON || area.height < EPSILON)
return;
+ PROFILER_begin(PROFILE_DRAW_PIXELS);
+
i64 i0 = (i64) floor(area.x);
i64 i1 = (i64) floor(area.x + area.width);
i64 j0 = (i64) floor(area.y);
@@ -356,9 +358,13 @@ void draw_pixels_to_buffer(Pixel_Buffer dst, Box area, Pixel_Buffer src) {
for (; d < d_end; ++d, ii += di)
put_pixel_(d, s[(i64) ii]);
}
+
+ PROFILER_end(PROFILE_DRAW_PIXELS);
}
void fill_rectangle_to_buffer(Pixel_Buffer dst, vec4_f32 color, Box area) {
+ PROFILER_begin(PROFILE_FILL_RECTANGLE);
+
i64 i0 = (i64) floor(area.x);
i64 i1 = (i64) floor(area.x + area.width);
i64 j0 = (i64) floor(area.y);
@@ -375,9 +381,13 @@ void fill_rectangle_to_buffer(Pixel_Buffer dst, vec4_f32 color, Box area) {
for (; p < p_end; ++p)
put_pixel_(p, color);
}
+
+ PROFILER_end(PROFILE_FILL_RECTANGLE);
}
void fill_triangle_to_buffer(Pixel_Buffer dst, vec4_f32 color, vec2 vertices[3]) {
+ PROFILER_begin(PROFILE_FILL_TRIANGLE);
+
f64 x0 = vertices[0].x;
f64 y0 = vertices[0].y;
f64 x1 = vertices[1].x;
@@ -418,6 +428,8 @@ void fill_triangle_to_buffer(Pixel_Buffer dst, vec4_f32 color, vec2 vertices[3])
for (; p < p_end; ++p)
put_pixel_(p, color);
}
+
+ PROFILER_end(PROFILE_FILL_TRIANGLE);
}
static void triangles_area_(f64 *x0, f64 *y0, f64 *x1, f64 *y1, vec2 p, vec2 s, i64 num, vec2 *v) {
@@ -438,6 +450,8 @@ static void triangles_area_(f64 *x0, f64 *y0, f64 *x1, f64 *y1, vec2 p, vec2 s,
}
void fill_triangles_to_buffer(Pixel_Buffer dst, vec4_f32 color, vec2 position, vec2 scale, i64 num_triangles, vec2 *vertices) {
+ PROFILER_begin(PROFILE_FILL_TRIANGLES);
+
f64 x0, y0, x1, y1;
triangles_area_(&x0, &y0, &x1, &y1, position, scale, num_triangles, vertices);
@@ -455,9 +469,13 @@ void fill_triangles_to_buffer(Pixel_Buffer dst, vec4_f32 color, vec2 position, v
put_pixel_(dst.pixels + j * dst.stride + i, color);
break;
}
+
+ PROFILER_end(PROFILE_FILL_TRIANGLES);
}
void fill_quad_to_buffer(Pixel_Buffer dst, vec4_f32 color, vec2 vertices[4]) {
+ PROFILER_begin(PROFILE_FILL_QUAD);
+
f64 x0 = vertices[0].x;
f64 y0 = vertices[0].y;
f64 x1 = vertices[1].x;
@@ -500,9 +518,13 @@ void fill_quad_to_buffer(Pixel_Buffer dst, vec4_f32 color, vec2 vertices[4]) {
for (; p < p_end; ++p)
put_pixel_(p, color);
}
+
+ PROFILER_end(PROFILE_FILL_QUAD);
}
void fill_ellipse_to_buffer(Pixel_Buffer dst, vec4_f32 color, Box area) {
+ PROFILER_begin(PROFILE_FILL_ELLIPSE);
+
f64 y1 = area.y + area.height;
i64 j0 = (i64) floor(area.y);
@@ -515,7 +537,7 @@ void fill_ellipse_to_buffer(Pixel_Buffer dst, vec4_f32 color, Box area) {
f64 half_h = area.height / 2.0;
if (half_h < EPSILON)
- return;
+ goto _finish;
f64 inv_half_h = 1.0 / half_h;
f64 cx = area.x + half_w;
@@ -539,6 +561,9 @@ void fill_ellipse_to_buffer(Pixel_Buffer dst, vec4_f32 color, Box area) {
for (; p < p_end; ++p)
put_pixel_(p, color);
}
+
+_finish:
+ PROFILER_end(PROFILE_FILL_ELLIPSE);
}
static b8 quad_from_line_(vec2 *dst, vec2 vertices[2], f64 width) {
@@ -572,11 +597,14 @@ static b8 quad_from_line_(vec2 *dst, vec2 vertices[2], f64 width) {
void fill_line_to_buffer(Pixel_Buffer dst, vec4_f32 color, vec2 vertices[2], f64 width) {
if (width < EPSILON) return;
+ PROFILER_begin(PROFILE_FILL_LINE);
+
vec2 quad[4];
if (!quad_from_line_(quad, vertices, width)) return;
fill_quad_to_buffer(dst, color, quad);
// TODO: Also render line ends as circles.
+ PROFILER_end(PROFILE_FILL_LINE);
}
static i64 char_column_offset_(c32 c, i64 column_index) {
@@ -816,6 +844,8 @@ void draw_text_area_to_buffer(Pixel_Buffer dst, i32 font, vec4_f32 color, Box ar
if (text == NULL || num_chars <= 0 || max_size.x < EPSILON || max_size.y < EPSILON)
return;
+ PROFILER_begin(PROFILE_DRAW_TEXT_AREA);
+
i64 num_columns = enum_text_columns_(num_chars, text);
i64 num_rows = enum_text_rows_(num_chars, text);
@@ -831,12 +861,16 @@ void draw_text_area_to_buffer(Pixel_Buffer dst, i32 font, vec4_f32 color, Box ar
ky = k * max_size.y;
draw_text_(dst, color, area.x, area.y, kx, ky, num_chars, text);
+
+ PROFILER_end(PROFILE_DRAW_TEXT_AREA);
}
void draw_text_cursor_to_buffer(Pixel_Buffer dst, i32 font, vec4_f32 color, Box area, vec2 max_size, i64 num_chars, c32 *text, i64 cursor, i64 selection) {
if (max_size.x < EPSILON || max_size.y < EPSILON)
return;
+ PROFILER_begin(PROFILE_DRAW_TEXT_CURSOR);
+
i64 num_columns = enum_text_columns_(num_chars, text);
i64 num_rows = enum_text_rows_(num_chars, text);
i64 cursor_x = text_cursor_(cursor, text);
@@ -921,6 +955,8 @@ void draw_text_cursor_to_buffer(Pixel_Buffer dst, i32 font, vec4_f32 color, Box
.height = ky * (CHAR_NUM_BITS_Y_ - 1),
}
);
+
+ PROFILER_end(PROFILE_DRAW_TEXT_CURSOR);
}
// ================================================================
diff --git a/reduced_system_layer.c b/reduced_system_layer.c
index 7e0dcf7..131ee5c 100755
--- a/reduced_system_layer.c
+++ b/reduced_system_layer.c
@@ -268,7 +268,7 @@ i32 main(i32 argc, c8 **argv);
#endif
#ifndef STATIC_MEMORY_BUFFER_SIZE
-#define STATIC_MEMORY_BUFFER_SIZE (200 * 1024 * 1024)
+#define STATIC_MEMORY_BUFFER_SIZE (400 * 1024 * 1024)
#endif
#ifndef MEMORY_CHUNK_SIZE
@@ -756,10 +756,20 @@ extern Platform g_platform;
Platform g_platform = {0};
enum {
+ PROFILE_MEMORY,
PROFILE_DOWNSCALE,
PROFILE_RESIZE,
PROFILE_WAITING,
PROFILE_FRAME,
+ PROFILE_DRAW_PIXELS,
+ PROFILE_FILL_RECTANGLE,
+ PROFILE_FILL_TRIANGLE,
+ PROFILE_FILL_TRIANGLES,
+ PROFILE_FILL_QUAD,
+ PROFILE_FILL_ELLIPSE,
+ PROFILE_FILL_LINE,
+ PROFILE_DRAW_TEXT_AREA,
+ PROFILE_DRAW_TEXT_CURSOR,
};
// ================================================================
@@ -1285,6 +1295,10 @@ static void *memory_buffer_allocate_from_(i64 memory_buffer_size, u8 *memory_buf
return NULL;
}
+ u8 *dst = NULL;
+
+ PROFILER_begin(PROFILE_MEMORY);
+
i64 occupied_len = memory_buffer_occupied_len_(memory_buffer_size);
i64 occupied_len_bytes = occupied_len * 8;
i64 occupied_len_bits = occupied_len * 64;
@@ -1293,27 +1307,27 @@ static void *memory_buffer_allocate_from_(i64 memory_buffer_size, u8 *memory_buf
if (memory_buffer_size <= occupied_len_bytes) {
LOG_ERROR("Memory buffer too small.");
- return NULL;
+ goto _finish;
}
if (alignment <= 0) {
LOG_ERROR("Invalid alignment: %lld", alignment);
- return NULL;
+ goto _finish;
}
if (size < 0) {
LOG_ERROR("Invalid size: %lld", size);
- return NULL;
+ goto _finish;
}
if (previous_size < 0) {
LOG_ERROR("Invalid previous size: %lld", previous_size);
- return NULL;
+ goto _finish;
}
if (previous_size > 0 && previous_data == NULL) {
LOG_ERROR("Invalid previous data.");
- return NULL;
+ goto _finish;
}
u8 *data = memory_buffer + occupied_len_bytes;
@@ -1329,10 +1343,10 @@ static void *memory_buffer_allocate_from_(i64 memory_buffer_size, u8 *memory_buf
i64 num_chunks = (align_(size, alignment) + MEMORY_CHUNK_SIZE - 1) / MEMORY_CHUNK_SIZE;
i64 chunk = (memory_buffer_size + MEMORY_CHUNK_SIZE - 1) / MEMORY_CHUNK_SIZE;
- if (num_chunks == prev_num_chunks)
- return previous_data;
-
- u8 *dst = NULL;
+ if (num_chunks == prev_num_chunks) {
+ dst = (u8 *) previous_data;
+ goto _finish;
+ }
if (num_chunks < prev_num_chunks) {
// Reuse previously allocated space.
@@ -1399,8 +1413,10 @@ static void *memory_buffer_allocate_from_(i64 memory_buffer_size, u8 *memory_buf
}
if (num_chunks == 0)
- return NULL;
+ dst = NULL;
+_finish:
+ PROFILER_end(PROFILE_MEMORY);
return dst;
}
@@ -1493,10 +1509,8 @@ void PROFILER_init(u32 slot, c8 *name) {
}
void PROFILER_begin(u32 slot) {
- if ((i64) (u64) slot >= _profiler_num_slots) {
- LOG_ERROR("Invalid slot: %d", (i32) slot);
+ if ((i64) (u64) slot >= _profiler_num_slots)
return;
- }
if (_profiler_slots[slot].count < 0) {
LOG_ERROR("Sanity");
@@ -1533,10 +1547,8 @@ static void normalize_time_(i64 *sec, i64 *nsec) {
}
void PROFILER_end(u32 slot) {
- if ((i64) (u64) slot >= _profiler_num_slots) {
- LOG_ERROR("Invalid slot: %d", (i32) slot);
+ if ((i64) (u64) slot >= _profiler_num_slots)
return;
- }
if (_profiler_slots[slot].count <= 0) {
LOG_ERROR("Sanity");
@@ -3125,10 +3137,20 @@ void put_image_to_main_window_(void) {
}
void init_main_window(void) {
- PROFILER_init(PROFILE_DOWNSCALE, "Downscale anti-aliasing");
- PROFILER_init(PROFILE_RESIZE, "Resize in-place");
- PROFILER_init(PROFILE_WAITING, "Waiting for events");
- PROFILER_init(PROFILE_FRAME, "Update frame");
+ PROFILER_init(PROFILE_MEMORY, "Memory allocator");
+ PROFILER_init(PROFILE_DOWNSCALE, "Downscale anti-aliasing");
+ PROFILER_init(PROFILE_RESIZE, "Resize in-place");
+ PROFILER_init(PROFILE_WAITING, "Waiting for events");
+ PROFILER_init(PROFILE_FRAME, "Update frame");
+ PROFILER_init(PROFILE_DRAW_PIXELS, "Draw pixels");
+ PROFILER_init(PROFILE_FILL_RECTANGLE, "Fill rectangle");
+ PROFILER_init(PROFILE_FILL_TRIANGLE, "Fill triangle");
+ PROFILER_init(PROFILE_FILL_TRIANGLES, "Fill triangles");
+ PROFILER_init(PROFILE_FILL_QUAD, "Fill quad");
+ PROFILER_init(PROFILE_FILL_ELLIPSE, "Fill ellipse");
+ PROFILER_init(PROFILE_FILL_LINE, "Fill line");
+ PROFILER_init(PROFILE_DRAW_TEXT_AREA, "Draw text area");
+ PROFILER_init(PROFILE_DRAW_TEXT_CURSOR, "Draw text cursor");
for (i64 i = 0; i < NUM_FRAMES_AVERAGED; ++i)
_frame_duration[i] = (MIN_FRAME_DURATION + MAX_FRAME_DURATION) / 2;
@@ -3358,11 +3380,11 @@ void shutdown_all_systems(void) {
if (_profiler_num_slots > 0) {
printf("\nPROFILER REPORT\n\n");
for (i64 i = 0; i < _profiler_num_slots; ++i) {
- if (_profiler_slots[i].name == NULL)
- continue;
- f64 k = 1. / _profiler_slots[i].amount;
+ if (_profiler_slots[i].name == NULL || _profiler_slots[i].amount == 0)
+ continue;
+ f64 k = _profiler_slots[i].amount == 0 ? 0. : 1. / _profiler_slots[i].amount;
f64 f = ((f64) _profiler_slots[i].time_sec) * k + ((f64 )_profiler_slots[i].time_nsec * .000000001) * k;
- printf("%-31s %3lld.%09lld / %4lld = %g\n", _profiler_slots[i].name, _profiler_slots[i].time_sec, _profiler_slots[i].time_nsec, _profiler_slots[i].amount, f);
+ printf("%-31s %3lld.%09lld / %4lld = %.6f\n", _profiler_slots[i].name, _profiler_slots[i].time_sec, _profiler_slots[i].time_nsec, _profiler_slots[i].amount, f);
}
printf("\n");
}