// ================================================================ // // graphics.c // // ---------------------------------------------------------------- // // (C) 2025 Mitya Selivanov // // Any use of this code is prohibited. // // ================================================================ #ifndef GRAPHICS_HEADER_GUARD_ #define GRAPHICS_HEADER_GUARD_ #ifdef GRAPHICS_HEADER #define RUNTIME_HEADER #endif #include "runtime.c" // ================================================================ // // Options // // ================================================================ #ifndef EPSILON #define EPSILON (1e-9) #endif #ifndef GRAPHICS_CACHE_DEPTH #define GRAPHICS_CACHE_DEPTH 4 #endif #ifndef GRAPHICS_CACHE_SIZE #define GRAPHICS_CACHE_SIZE (128 * GRAPHICS_CACHE_DEPTH) #endif // ================================================================ // // Vector math // // ================================================================ vec3_f32 vec3_from_vec4_f32(vec4_f32 v); vec4_f32 vec4_from_vec3_f32(vec3_f32 v, f32 w); vec3_f32 vec3_f32_lerp (vec3_f32 a, vec3_f32 b, f32 t); vec4_f32 vec4_f32_lerp (vec4_f32 a, vec4_f32 b, f32 t); // ================================================================ // NOTE: Dimensions are in pixels. typedef struct { f64 x; f64 y; f64 width; f64 height; } Box; // TODO: // typedef struct { // b8 enabled; // i64 x; // i64 y; // i64 width; // i64 height; // } Bounding_Box; typedef struct { i64 width; i64 height; i64 stride; // Bounding_Box bounds; u8 *pixels; } Stencil_Buffer; typedef struct { i64 width; i64 height; i64 stride; // Bounding_Box bounds; vec4_f32 *pixels; } Pixel_Buffer; enum { GRAPHICS_DRAW_PIXELS, GRAPHICS_FILL_RECTANGLE, GRAPHICS_FILL_TRIANGLE, GRAPHICS_FILL_TRIANGLES, GRAPHICS_FILL_QUAD, GRAPHICS_FILL_ELLIPSE, GRAPHICS_FILL_LINE, GRAPHICS_DRAW_TEXT_AREA, GRAPHICS_DRAW_TEXT_CURSOR, }; typedef struct graphics_request_ { u16 op; union { struct { Box area; Pixel_Buffer src; } pixels; struct { vec4_f32 color; Box area; } rectangle; struct { vec4_f32 color; vec2 vertices[3]; } triangle; struct { vec4_f32 color; vec2 position; vec2 scale; i64 num_triangles; vec2 * vertices; } triangles; struct { vec4_f32 color; vec2 vertices[4]; } quad; struct { vec4_f32 color; Box area; } ellipse; struct { vec4_f32 color; vec2 vertices[2]; f64 width; } line; struct { i32 font; vec4_f32 color; Box area; vec2 max_size; i64 num_chars; c32 * text; } text_area; struct { i32 font; vec4_f32 color; Box area; vec2 max_size; i64 num_chars; c32 * text; i64 cursor; i64 selection; } text_cursor; }; } Graphics_Request; typedef struct { b8 disable_cache; vec2 scale; Pixel_Buffer dst; } Graphics_Context; // ================================================================ // // User program interface // // ================================================================ Box font_text_area_dispatch(i32 font, vec2 scale, i64 num_chars, c32 *text); void font_render_to_stencil_dispatch(Stencil_Buffer dst, i32 font, vec2 position, vec2 scale, i64 num_chars, c32 *text); // ================================================================ vec3_f32 lab_from_rgb(vec3_f32 rgb); vec3_f32 rgb_from_lab(vec3_f32 lab); vec3_f32 lab_from_lch(vec3_f32 lch); vec3_f32 lch_from_lab(vec3_f32 lab); vec3_f32 rgb_from_lch(vec3_f32 lch); vec3_f32 lch_from_rgb(vec3_f32 rgb); // NOTE: General rule to work with gamma is removing it, // then doing computations like blending, interpolation, etc, // then adding it back. vec3_f32 rgb_gamma_add (vec3_f32 rgb); vec3_f32 rgb_gamma_remove (vec3_f32 rgb); vec4_f32 rgba_gamma_add (vec4_f32 rgb); vec4_f32 rgba_gamma_remove(vec4_f32 rgb); vec3_f32 rgb_gamma_lerp (vec3_f32 a, vec3_f32 b, f32 t); vec4_f32 rgba_gamma_lerp(vec4_f32 a, vec4_f32 b, f32 t); vec3_f32 rgb_mix (vec3_f32 a, vec3_f32 b, f32 t); vec4_f32 rgba_mix (vec4_f32 a, vec4_f32 b, f32 t); vec3_f32 lch_mix (vec3_f32 a, vec3_f32 b, f32 t); vec3_f32 lch_lerp (vec3_f32 a, vec3_f32 b, f32 t); u32 rgb_u32_from_f32(vec3_f32 color); vec3_f32 rgb_f32_from_u32(u32 color); u32 rgba_u32_from_f32(vec4_f32 color); vec4_f32 rgba_f32_from_u32(u32 color); b8 hit_rectangle(Box area, vec2 point); b8 hit_triangle (vec2 vertices[3], vec2 point); b8 hit_quad (vec2 vertices[4], vec2 point); b8 hit_ellipse (Box area, vec2 point); b8 hit_line (vec2 vertices[2], f64 width, vec2 point); void draw_pixels_to_buffer (Pixel_Buffer dst, Box area, Pixel_Buffer src); void fill_rectangle_to_buffer (Pixel_Buffer dst, vec4_f32 color, Box area); void fill_triangle_to_buffer (Pixel_Buffer dst, vec4_f32 color, vec2 vertices[3]); void fill_triangles_to_buffer (Pixel_Buffer dst, vec4_f32 color, vec2 position, vec2 scale, i64 num_triangles, vec2 *vertices); void fill_quad_to_buffer (Pixel_Buffer dst, vec4_f32 color, vec2 vertices[4]); void fill_ellipse_to_buffer (Pixel_Buffer dst, vec4_f32 color, Box area); void fill_line_to_buffer (Pixel_Buffer dst, vec4_f32 color, vec2 vertices[2], f64 width); void draw_text_area_to_buffer (Pixel_Buffer dst, i32 font, vec4_f32 color, Box area, vec2 max_size, i64 num_chars, c32 *text); 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); void draw_pixels_cached (Box area, Pixel_Buffer src); void fill_rectangle_cached (vec4_f32 color, Box area); void fill_triangle_cached (vec4_f32 color, vec2 vertices[3]); void fill_triangles_cached (vec4_f32 color, vec2 position, vec2 scale, i64 num_triangles, vec2 *vertices); void fill_quad_cached (vec4_f32 color, vec2 vertices[4]); void fill_ellipse_cached (vec4_f32 color, Box area); void fill_line_cached (vec4_f32 color, vec2 vertices[2], f64 width); void draw_text_area_cached (i32 font, vec4_f32 color, Box area, vec2 max_size, i64 num_chars, c32 *text); void draw_text_cursor_cached(i32 font, vec4_f32 color, Box area, vec2 max_size, i64 num_chars, c32 *text, i64 cursor, i64 selection); void draw_pixels (Box area, Pixel_Buffer src); void fill_rectangle (vec4_f32 color, Box area); void fill_triangle (vec4_f32 color, vec2 vertices[3]); void fill_triangles (vec4_f32 color, vec2 position, vec2 scale, i64 num_triangles, vec2 *vertices); void fill_quad (vec4_f32 color, vec2 vertices[4]); void fill_ellipse (vec4_f32 color, Box area); void fill_line (vec4_f32 color, vec2 vertices[2], f64 width); void draw_text_area (i32 font, vec4_f32 color, Box area, vec2 max_size, i64 num_chars, c32 *text); void draw_text_cursor(i32 font, vec4_f32 color, Box area, vec2 max_size, i64 num_chars, c32 *text, i64 cursor, i64 selection); void perform_graphics_request(Graphics_Context context, Graphics_Request req); void clean_graphics_requests_cache(i64 amount); #endif // GRAPHICS_HEADER_GUARD_ // ================================================================ #ifndef GRAPHICS_HEADER #ifndef GRAPHICS_IMPL_GUARD_ #define GRAPHICS_IMPL_GUARD_ // FIXME, PERF: Re-encode for faster rendering. static u64 _bitfont[] = { 0xbc0000000000, 0xc00300000, 0x5fd5040093f24fc9, 0xa00a2c2a1a280105, 0xc000415e6f, 0x400000020be0000, 0x1c38a8400000007d, 0x40002043e1020215, 0x408102000000010, 0x9800000000020002, 0xf913e00000033, 0x53200000207c8800, 0x3654880000099, 0x54b800000f840e00, 0xe953c000001a, 0x953e000000674080, 0x1e54b800000f, 0x490000000000240, 0x88a08000000, 0x20a220050a142850, 0x6520800000, 0x912f801eab260be, 0x800034952bf0001f, 0xc850bf0000921427, 0xf00010a54afc0003, 0xd29427800002142b, 0x840007e1023f0000, 0x7d09100000217e, 0x3f000188a08fc000, 0xc30c0cfc00000810, 0x27803f101013f00f, 0xc244bf0000f214, 0x4bf0002f21427800, 0xc254a480006c24, 0x407c00102fc08100, 0xf208080f0000fa0, 0x531007d81c607c0, 0xc208288c031141, 0x83fc00046954b10, 0x180e03000000, 0x41040000000ff04, 0x8102040810000404, 0x2a54600000000101, 0x309123e0000e, 0xc912180000a22447, 0x8000062a54700007, 0xe52a4300000029f0, 0xa0000602043e0001, 0x1d48000002074, 0x1f000003610f8000, 0x13e04f800000010, 0x470000780813e00f, 0x184893e0000e224, 0x23e0001f12243000, 0x82a54100000008, 0x40780000009f0200, 0xe208080e0001f20, 0xa22007981860780, 0x82082888022282, 0x16c200004ca95320, 0x7f000004, 0x408200000086d04, 0x8204, }; enum { CHAR_NUM_BITS_X_ = 6, CHAR_NUM_BITS_Y_ = 7, CHAR_NUM_BITS_ = CHAR_NUM_BITS_X_ * CHAR_NUM_BITS_Y_, }; static void put_pixel_(vec4_f32 *dst, vec4_f32 color) { if (color.w == 1.f) *dst = color; else { f32 dst_amount = 1. - color.w; *dst = (vec4_f32) { .x = dst->x * dst_amount + color.x * color.w, .y = dst->y * dst_amount + color.y * color.w, .z = dst->z * dst_amount + color.z * color.w, .w = max2_f32_(dst->w, color.w), }; } } 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 + 0.5); i64 i1 = (i64) floor(area.x + area.width + 0.5); i64 j0 = (i64) floor(area.y + 0.5); i64 j1 = (i64) floor(area.y + area.height + 0.5); if (i0 < 0) i0 = 0; if (i1 > dst.width) i1 = dst.width; if (j0 < 0) j0 = 0; if (j1 > dst.height) j1 = dst.height; // FIXME, PERF: Use integer arithmetic. f64 di = src.width / area.width; f64 dj = src.height / area.height; f64 jj = (j0 - area.y) * dj + dj * .5; for (i64 j = j0; j < j1; ++j, jj += dj) { if (jj < 0 || jj >= src.height) continue; vec4_f32 *d = dst.pixels + j * dst.stride + i0; vec4_f32 *d_end = d + i1 - i0; vec4_f32 *s = src.pixels + (i64) jj * src.stride; f64 ii = (i0 - area.x) * di + di * .5; if (ii < 0 || ii >= src.width) continue; 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 + 0.5); i64 i1 = (i64) floor(area.x + area.width + 0.5); i64 j0 = (i64) floor(area.y + 0.5); i64 j1 = (i64) floor(area.y + area.height + 0.5); if (i0 < 0) i0 = 0; if (i1 > dst.width) i1 = dst.width; if (j0 < 0) j0 = 0; if (j1 > dst.height) j1 = dst.height; for (i64 j = j0; j < j1; ++j) { vec4_f32 *p = dst.pixels + j * dst.stride + i0; vec4_f32 *p_end = dst.pixels + j * dst.stride + i1; 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; f64 y1 = vertices[1].y; f64 x2 = vertices[2].x; f64 y2 = vertices[2].y; i64 i0 = (i64) floor(min3_(x0, x1, x2)); i64 j0 = (i64) floor(min3_(y0, y1, y2)); i64 i1 = (i64) ceil (max3_(x0, x1, x2)); i64 j1 = (i64) ceil (max3_(y0, y1, y2)); if (i0 < 0) i0 = 0; if (i1 >= dst.width) i1 = dst.width - 1; if (j0 < 0) j0 = 0; if (j1 >= dst.height) j1 = dst.height - 1; for (i64 j = j0; j <= j1; ++j) { // FIXME, PERF: Calculate the exact intersection. i64 left = i1; i64 right = i0; for (i64 i = i0; i <= i1; ++i) if (hit_triangle(vertices, (vec2) { (f64) i, (f64) j, })) { left = i; break; } for (i64 i = i1; i >= i0; --i) if (hit_triangle(vertices, (vec2) { (f64) i, (f64) j, })) { right = i; break; } vec4_f32 *p = dst.pixels + j * dst.stride + left; vec4_f32 *p_end = dst.pixels + j * dst.stride + right + 1; 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) { if (num <= 0) return; *x0 = min3_(p.x + v[0].x * s.x, p.x + v[1].x * s.x, p.x + v[2].x * s.x); *y0 = min3_(p.y + v[0].y * s.y, p.y + v[1].y * s.y, p.y + v[2].y * s.y); *x1 = max3_(p.x + v[0].x * s.x, p.x + v[1].x * s.x, p.x + v[2].x * s.x); *y1 = max3_(p.y + v[0].y * s.y, p.y + v[1].y * s.y, p.y + v[2].y * s.y); for (i64 i = 1; i < num; ++i) { *x0 = min4_(*x0, p.x + v[i * 3].x * s.x, p.x + v[i * 3 + 1].x * s.x, p.x + v[i * 3 + 2].x * s.x); *y0 = min4_(*y0, p.y + v[i * 3].y * s.y, p.y + v[i * 3 + 1].y * s.y, p.y + v[i * 3 + 2].y * s.y); *x1 = max4_(*x1, p.x + v[i * 3].x * s.x, p.x + v[i * 3 + 1].x * s.x, p.x + v[i * 3 + 2].x * s.x); *y1 = max4_(*y1, p.y + v[i * 3].y * s.y, p.y + v[i * 3 + 1].y * s.y, p.y + v[i * 3 + 2].y * s.y); } } 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); i64 i0 = (i64) floor(x0); i64 j0 = (i64) floor(y0); i64 i1 = (i64) ceil (x1); i64 j1 = (i64) ceil (y1); if (i0 < 0) i0 = 0; if (i1 >= dst.width) i1 = dst.width - 1; if (j0 < 0) j0 = 0; if (j1 >= dst.height) j1 = dst.height - 1; // FIXME, PERF: Use stencil buffer. for (i64 j = j0; j <= j1; ++j) for (i64 i = i0; i <= i1; ++i) for (i64 k = 0; k < num_triangles; ++k) if (hit_triangle(vertices + 3 * k, (vec2) { (f64) i, (f64) j, })) { 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; f64 y1 = vertices[1].y; f64 x2 = vertices[2].x; f64 y2 = vertices[2].y; f64 x3 = vertices[3].x; f64 y3 = vertices[3].y; i64 i0 = (i64) floor(min4_(x0, x1, x2, x3)); i64 j0 = (i64) floor(min4_(y0, y1, y2, y3)); i64 i1 = (i64) ceil (max4_(x0, x1, x2, x3)); i64 j1 = (i64) ceil (max4_(y0, y1, y2, y3)); if (i0 < 0) i0 = 0; if (i1 >= dst.width) i1 = dst.width - 1; if (j0 < 0) j0 = 0; if (j1 >= dst.height) j1 = dst.height - 1; for (i64 j = j0; j <= j1; ++j) { // FIXME, PERF: Calculate the exact intersection. i64 left = i1; i64 right = i0; for (i64 i = i0; i <= i1; ++i) if (hit_quad(vertices, (vec2) { (f64) i, (f64) j, })) { left = i; break; } for (i64 i = i1; i >= i0; --i) if (hit_quad(vertices, (vec2) { (f64) i, (f64) j, })) { right = i; break; } vec4_f32 *p = dst.pixels + j * dst.stride + left; vec4_f32 *p_end = dst.pixels + j * dst.stride + right + 1; 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); i64 j1 = (i64) ceil (y1); if (j0 < 0) j0 = 0; if (j1 >= dst.height) j1 = dst.height - 1; f64 half_w = area.width / 2.0; f64 half_h = area.height / 2.0; if (half_h < EPSILON) goto _finish; f64 inv_half_h = 1.0 / half_h; f64 cx = area.x + half_w; f64 cy = area.y + half_h; for (i64 j = j0; j <= j1; ++j) { f64 ry = (j - cy) * inv_half_h; if (ry > 1.0 - EPSILON || ry < -1.0 + EPSILON) continue; f64 dx = half_w * sqrt(1.0 - ry * ry); i64 left = (i64) ceil (cx - dx - EPSILON); i64 right = (i64) floor(cx + dx + EPSILON); if (left < 0) left = 0; if (right >= dst.width) right = dst.width - 1; vec4_f32 *p = dst.pixels + j * dst.stride + left; vec4_f32 *p_end = dst.pixels + j * dst.stride + right + 1; 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) { f64 x0 = vertices[0].x; f64 y0 = vertices[0].y; f64 x1 = vertices[1].x; f64 y1 = vertices[1].y; f64 dx = x1 - x0; f64 dy = y1 - y0; // Tangent // f64 tx = -dy; f64 ty = dx; f64 tl = sqrt(tx * tx + ty * ty); if (tl < EPSILON) return 0; f64 k = .5 / tl; tx *= width * k; ty *= width * k; dst[0] = (vec2) { x0 - tx, y0 - ty }; dst[1] = (vec2) { x0 + tx, y0 + ty }; dst[2] = (vec2) { x1 + tx, y1 + ty }; dst[3] = (vec2) { x1 - tx, y1 - ty }; return 1; } 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, use stencil buffer. PROFILER_end(PROFILE_FILL_LINE); } static i64 char_column_offset_(c32 c, i64 column_index) { if (column_index < 0 || column_index >= CHAR_NUM_BITS_X_) return -1; return (c - 32) * CHAR_NUM_BITS_ + column_index * CHAR_NUM_BITS_Y_; } static b8 char_bit_(i64 column_offset, i64 row_index) { if (column_offset < 0 || row_index < 0 || row_index >= CHAR_NUM_BITS_Y_) return 0; i64 bit_index = column_offset + row_index; i64 qword_index = bit_index / 64; if (qword_index < 0 || qword_index >= (i64) (sizeof _bitfont / sizeof *_bitfont)) return 0; u64 mask = 1ull << (bit_index % 64); return !!(_bitfont[qword_index] & mask); } static u64 char_column_convolved_(c32 c, i64 column_index) { if (column_index < 0 || column_index >= CHAR_NUM_BITS_X_) return 0; u64 column = 0; i64 offset = char_column_offset_(c, column_index); for (i64 y = 0; y < CHAR_NUM_BITS_Y_; ++y) if (char_bit_(offset, y)) column |= 3ull << y; return column; } static b8 char_column_empty_(c32 c, i64 column_index) { if (column_index < 0 || column_index >= CHAR_NUM_BITS_X_) return 1; i64 offset = char_column_offset_(c, column_index); for (i64 y = 0; y < CHAR_NUM_BITS_Y_; ++y) if (char_bit_(offset, y)) return 0; return 1; } static i64 char_width_(c32 c) { if (c < 32) return 0; if (c == ' ' || c > 127) return 4; i64 width = 0; for (; width < CHAR_NUM_BITS_X_; ++width) if (char_column_empty_(c, width) && char_column_empty_(c, width + 1)) break; return width; } static i64 char_spacing_(i64 num_chars, c32 *text, i64 index) { if (text == NULL) return 0; if (index < 0 || index + 1 >= num_chars) return 0; u64 a = char_column_convolved_(text[index], char_width_(text[index]) - 1); u64 b = char_column_convolved_(text[index + 1], 0); if (!!(a & b)) return 1; return 0; } static i64 text_cursor_(i64 num_chars, c32 *text) { if (text == NULL) return 0; i64 cursor = 0; for (i64 i = 0; i < num_chars; ++i) { if (text[i] <= ' ') { if (text[i] == '\n') cursor = 0; else if (text[i] == '\b' && i > 0) cursor -= char_width_(text[i - 1]) + char_spacing_(num_chars, text, i - 1); else if (text[i] == '\r') cursor = 0; else cursor += char_width_(' ') + char_spacing_(num_chars, text, i); continue; } cursor += char_width_(text[i]) + char_spacing_(num_chars, text, i); } return cursor; } static i64 enum_text_columns_(i64 num_chars, c32 *text) { if (text == NULL) return 0; i64 cols = 0; i64 n = 0; for (i64 i = 0; i < num_chars; ++i) { if (text[i] <= ' ') { if (text[i] == '\n') { if (cols < n) cols = n; n = 0; } else if (text[i] == '\b' && i > 0) { if (cols < n) cols = n; n -= char_width_(text[i - 1]) + char_spacing_(num_chars, text, i - 1); } else if (text[i] == '\r') { if (cols < n) cols = n; n = 0; } else n += char_width_(' ') + char_spacing_(num_chars, text, i); continue; } n += char_width_(text[i]) + char_spacing_(num_chars, text, i); } if (cols < n) cols = n; return cols; } static i64 enum_text_rows_(i64 num_chars, c32 *text) { if (text == NULL) return 0; i64 rows = 0; for (i64 i = 0; i <= num_chars; ++i) if (i == num_chars || text[i] == '\n') { if (rows > 0) ++rows; rows += CHAR_NUM_BITS_Y_; } return rows; } static void draw_text_(Pixel_Buffer dst, vec4_f32 color, f64 x0, f64 y0, f64 scale_x, f64 scale_y, i64 num_chars, c32 *text) { f64 kx = scale_x; f64 h = scale_y * CHAR_NUM_BITS_Y_; if (h < EPSILON) return; f64 x = x0; f64 y = y0; for (i64 n = 0; n < num_chars; ++n) { if (text[n] <= ' ') { if (text[n] == '\n') { x = x0; y += scale_y * (CHAR_NUM_BITS_Y_ + 1); } else if (text[n] == '\b' && n > 0) x -= kx * (char_width_(text[n - 1]) + char_spacing_(num_chars, text, n - 1)); else if (text[n] == '\r') x = x0; else x += kx * (char_width_(' ') + char_spacing_(num_chars, text, n)); continue; } i64 num_cols = char_width_(text[n]); f64 w = num_cols * kx; if (w >= EPSILON) { i64 i0 = (i64) floor(x); i64 i1 = (i64) ceil (x + w); i64 j0 = (i64) floor(y); i64 j1 = (i64) ceil (y + h); if (i0 < 0) i0 = 0; if (i1 >= dst.width) i1 = dst.width - 1; if (j0 < 0) j0 = 0; if (j1 >= dst.height) j1 = dst.height - 1; f64 w_inv = 1. / w; f64 h_inv = 1. / h; for (i64 i = i0; i <= i1; ++i) { i64 column = (i64) floor(((i - x + .5) * num_cols) * w_inv); i64 offset = char_column_offset_(text[n], column); for (i64 j = j0; j <= j1; ++j) { i64 row = (i64) floor(((j - y + .5) * CHAR_NUM_BITS_Y_) * h_inv); if (char_bit_(offset, row)) put_pixel_(dst.pixels + j * dst.stride + i, color); } } } x += kx * (num_cols + char_spacing_(num_chars, text, n)); } } #if !defined(GRAPHICS_ENABLE_FONT_CUSTOM_DISPATCH) Box font_text_area_dispatch(i32 font, vec2 scale, i64 num_chars, c32 *text) { // TODO (void) font; (void) scale; (void) num_chars; (void) text; return (Box) {0}; } void font_render_to_stencil_dispatch(Stencil_Buffer dst, i32 font, vec2 position, vec2 scale, i64 num_chars, c32 *text) { // TODO (void) dst; (void) font; (void) position; (void) scale; (void) num_chars; (void) text; } #endif // !defined(GRAPHICS_ENABLE_FONT_CUSTOM_DISPATCH) void draw_text_area_to_buffer(Pixel_Buffer dst, i32 font, vec4_f32 color, Box area, vec2 max_size, i64 num_chars, c32 *text) { (void) font; 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); f64 scale_x = area.width / num_columns; f64 scale_y = area.height / num_rows; f64 kx = scale_x / max_size.x; f64 ky = scale_y / max_size.y; f64 k = kx < ky ? kx : ky; kx = k * max_size.x; 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) { (void) font; 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); i64 cursor_y = enum_text_rows_(cursor, text); f64 scale_x = area.width / num_columns; f64 scale_y = area.height / num_rows; f64 kx = scale_x / max_size.x; f64 ky = scale_y / max_size.y; f64 k = kx < ky ? kx : ky; kx = k * max_size.x; ky = k * max_size.y; if (selection != 0) { i64 selection_x, selection_y; if (selection > 0) { selection_x = text_cursor_(cursor + selection, text); selection_y = enum_text_rows_(cursor + selection, text); } else { selection_x = cursor_x; selection_y = cursor_y; cursor_x = text_cursor_(cursor + selection, text); cursor_y = enum_text_rows_(cursor + selection, text); } if (cursor_y == selection_y) fill_rectangle_to_buffer( dst, color, (Box) { .x = area.x + kx * cursor_x, .y = area.y + ky * cursor_y - ky * (CHAR_NUM_BITS_Y_ + 1), .width = kx * (selection_x - cursor_x), .height = ky * (CHAR_NUM_BITS_Y_ + 1), } ); else { fill_rectangle_to_buffer( dst, color, (Box) { .x = area.x + kx * cursor_x, .y = area.y + ky * cursor_y - ky * (CHAR_NUM_BITS_Y_ + 1), .width = kx * (num_columns - cursor_x), .height = ky * (CHAR_NUM_BITS_Y_ + 1), } ); for (i64 j = cursor_y + CHAR_NUM_BITS_Y_ + 1; j < selection_y; j += CHAR_NUM_BITS_Y_ + 1) fill_rectangle_to_buffer( dst, color, (Box) { .x = area.x, .y = area.y + ky * j - ky * (CHAR_NUM_BITS_Y_ + 1), .width = kx * num_columns, .height = ky * (CHAR_NUM_BITS_Y_ + 1), } ); fill_rectangle_to_buffer( dst, color, (Box) { .x = area.x, .y = area.y + ky * selection_y - ky * (CHAR_NUM_BITS_Y_ + 1), .width = kx * selection_x, .height = ky * (CHAR_NUM_BITS_Y_ + 1), } ); } } else fill_rectangle_to_buffer( dst, color, (Box) { .x = area.x + kx * cursor_x, .y = area.y + ky * cursor_y - ky * CHAR_NUM_BITS_Y_, .width = kx * .5, .height = ky * (CHAR_NUM_BITS_Y_ - 1), } ); PROFILER_end(PROFILE_DRAW_TEXT_CURSOR); } // ================================================================ vec3_f32 vec3_from_vec4_f32(vec4_f32 v) { return (vec3_f32) { .x = v.x, .y = v.y, .z = v.z, }; } vec4_f32 vec4_from_vec3_f32(vec3_f32 v, f32 w) { return (vec4_f32) { .x = v.x, .y = v.y, .z = v.z, .w = w, }; } vec3_f32 vec3_f32_lerp(vec3_f32 a, vec3_f32 b, f32 t) { return (vec3_f32) { .x = a.x + (b.x - a.x) * t, .y = a.y + (b.y - a.y) * t, .z = a.z + (b.z - a.z) * t, }; } vec4_f32 vec4_f32_lerp(vec4_f32 a, vec4_f32 b, f32 t) { return (vec4_f32) { .x = a.x + (b.x - a.x) * t, .y = a.y + (b.y - a.y) * t, .z = a.z + (b.z - a.z) * t, .w = a.w + (b.w - a.w) * t, }; } // ================================================================ vec3_f32 rgb_gamma_add(vec3_f32 rgb) { return (vec3_f32) { .x = gamma_(rgb.x), .y = gamma_(rgb.y), .z = gamma_(rgb.z), }; } vec3_f32 rgb_gamma_remove(vec3_f32 rgb) { return (vec3_f32) { .x = gamma_re_(rgb.x), .y = gamma_re_(rgb.y), .z = gamma_re_(rgb.z), }; } vec4_f32 rgba_gamma_add(vec4_f32 rgba) { return (vec4_f32) { .x = gamma_(rgba.x), .y = gamma_(rgba.y), .z = gamma_(rgba.z), .w = gamma_(rgba.w), }; } vec4_f32 rgba_gamma_remove(vec4_f32 rgba) { return (vec4_f32) { .x = gamma_re_(rgba.x), .y = gamma_re_(rgba.y), .z = gamma_re_(rgba.z), .w = gamma_re_(rgba.w), }; } vec3_f32 lab_from_rgb(vec3_f32 rgb) { f64 l = 0.4122214708 * rgb.x + 0.5363325363 * rgb.y + 0.0514459929 * rgb.z; f64 m = 0.2119034982 * rgb.x + 0.6806995451 * rgb.y + 0.1073969566 * rgb.z; f64 s = 0.0883024619 * rgb.x + 0.2817188376 * rgb.y + 0.6299787005 * rgb.z; f64 l_ = cbrt(l); f64 m_ = cbrt(m); f64 s_ = cbrt(s); return (vec3_f32) { .x = (f32) (0.2104542553 * l_ + 0.7936177850 * m_ - 0.0040720468 * s_), .y = (f32) (1.9779984951 * l_ - 2.4285922050 * m_ + 0.4505937099 * s_), .z = (f32) (0.0259040371 * l_ + 0.7827717662 * m_ - 0.8086757660 * s_), }; } vec3_f32 rgb_from_lab(vec3_f32 lab) { f64 l_ = lab.x + 0.3963377774 * lab.y + 0.2158037573 * lab.z; f64 m_ = lab.x - 0.1055613458 * lab.y - 0.0638541728 * lab.z; f64 s_ = lab.x - 0.0894841775 * lab.y - 1.2914855480 * lab.z; f64 l = l_ * l_ * l_; f64 m = m_ * m_ * m_; f64 s = s_ * s_ * s_; return (vec3_f32) { .x = (f32) (+4.0767416621 * l - 3.3077115913 * m + 0.2309699292 * s), .y = (f32) (-1.2684380046 * l + 2.6097574011 * m - 0.3413193965 * s), .z = (f32) (-0.0041960863 * l - 0.7034186147 * m + 1.7076147010 * s), }; } vec3_f32 lch_from_lab(vec3_f32 lab) { f64 a = lab.y; f64 b = lab.z; return (vec3_f32) { .x = lab.x, .y = (f32) sqrt (a * a + b * b), .z = (f32) atan2(b, a), }; } vec3_f32 lab_from_lch(vec3_f32 lch) { return (vec3_f32) { .x = lch.x, .y = (f32) (lch.y * cos(lch.z)), .z = (f32) (lch.y * sin(lch.z)), }; } vec3_f32 rgb_from_lch(vec3_f32 lch) { return rgb_from_lab(lab_from_lch(lch)); } vec3_f32 lch_from_rgb(vec3_f32 rgb) { return lch_from_lab(lab_from_rgb(rgb)); } vec3_f32 rgb_gamma_lerp (vec3_f32 a, vec3_f32 b, f32 t) { return rgb_gamma_add(vec3_f32_lerp( rgb_gamma_remove(a), rgb_gamma_remove(b), t )); } vec4_f32 rgba_gamma_lerp(vec4_f32 a, vec4_f32 b, f32 t) { return rgba_gamma_add(vec4_f32_lerp( rgba_gamma_remove(a), rgba_gamma_remove(b), t )); } vec3_f32 rgb_mix(vec3_f32 a, vec3_f32 b, f32 t) { vec3_f32 a_lch = lch_from_rgb(a); vec3_f32 b_lch = lch_from_rgb(b); return rgb_from_lch((vec3_f32) { a_lch.x + (b_lch.x - a_lch.x) * t, a_lch.y + (b_lch.y - a_lch.y) * t, lch_from_rgb(rgb_gamma_lerp(a, b, t)).z, }); } vec4_f32 rgba_mix(vec4_f32 a, vec4_f32 b, f32 t) { vec3_f32 a_lch = lch_from_rgb(vec3_from_vec4_f32(a)); vec3_f32 b_lch = lch_from_rgb(vec3_from_vec4_f32(b)); vec4_f32 ab = rgba_gamma_lerp(a, b, t); return vec4_from_vec3_f32(rgb_from_lch((vec3_f32) { a_lch.x + (b_lch.x - a_lch.x) * t, a_lch.y + (b_lch.y - a_lch.y) * t, lch_from_rgb(vec3_from_vec4_f32(ab)).z, }), ab.w); } vec3_f32 lch_mix(vec3_f32 a, vec3_f32 b, f32 t) { return (vec3_f32) { a.x + (b.x - a.x) * t, a.y + (b.y - a.y) * t, lch_from_rgb(rgb_gamma_lerp(rgb_from_lch(a), rgb_from_lch(b), t)).z, }; } vec3_f32 lch_lerp(vec3_f32 a, vec3_f32 b, f32 t) { f32 delta_hue = b.z - a.z; if (delta_hue > M_PI) delta_hue -= M_PI * 2; if (delta_hue < -M_PI) delta_hue += M_PI * 2; return (vec3_f32) { .x = a.x + (b.x - a.x) * t, .y = a.y + (b.y - a.y) * t, .z = a.z + delta_hue * t, }; } u32 rgb_u32_from_f32(vec3_f32 color) { i32 ir = (i32) floor(color.x * 255. + .5); i32 ig = (i32) floor(color.y * 255. + .5); i32 ib = (i32) floor(color.z * 255. + .5); u32 r = ir < 0 ? 0u : ir > 255 ? 255u : (u32) ir; u32 g = ig < 0 ? 0u : ig > 255 ? 255u : (u32) ig; u32 b = ib < 0 ? 0u : ib > 255 ? 255u : (u32) ib; return (r << 16) | (g << 8) | b; } vec3_f32 rgb_f32_from_u32(u32 color) { return (vec3_f32) { .x = ((color & 0xff0000) >> 16) / 255.f, .y = ((color & 0x00ff00) >> 8) / 255.f, .z = (color & 0x0000ff) / 255.f, }; } u32 rgba_u32_from_f32(vec4_f32 color) { i32 ir = (i32) floor(color.x * 255. + .5); i32 ig = (i32) floor(color.y * 255. + .5); i32 ib = (i32) floor(color.z * 255. + .5); i32 ia = (i32) floor(color.w * 255. + .5); u32 r = ir < 0 ? 0u : ir > 255 ? 255u : (u32) ir; u32 g = ig < 0 ? 0u : ig > 255 ? 255u : (u32) ig; u32 b = ib < 0 ? 0u : ib > 255 ? 255u : (u32) ib; u32 a = ia < 0 ? 0u : ia > 255 ? 255u : (u32) ia; return (a << 24) | (r << 16) | (g << 8) | b; } vec4_f32 rgba_f32_from_u32(u32 color) { return (vec4_f32) { .x = ((color & 0x00ff0000) >> 16) / 255.f, .y = ((color & 0x0000ff00) >> 8) / 255.f, .z = (color & 0x000000ff) / 255.f, .w = ((color & 0xff000000) >> 24) / 255.f, }; } b8 hit_rectangle(Box area, vec2 point) { return point.x >= area.x && point.x < area.x + area.width && point.y >= area.y && point.y < area.y + area.height; } static b8 same_sign_(f64 a, f64 b) { if (a >= EPSILON && b <= -EPSILON) return 0; if (a <= -EPSILON && b >= EPSILON) return 0; return 1; } b8 hit_triangle(vec2 vertices[3], vec2 point) { f64 x0 = vertices[0].x; f64 y0 = vertices[0].y; f64 x1 = vertices[1].x; f64 y1 = vertices[1].y; f64 x2 = vertices[2].x; f64 y2 = vertices[2].y; f64 px = point.x; f64 py = point.y; if ( (px < x0 && px < x1 && px < x2) || (px > x0 && px > x1 && px > x2) || (py < y0 && py < y1 && py < y2) || (py > y0 && py > y1 && py > y2) ) return 0; f64 x10 = x1 - x0; f64 x21 = x2 - x1; f64 x02 = x0 - x2; f64 y10 = y1 - y0; f64 y21 = y2 - y1; f64 y02 = y0 - y2; // Z-components of cross-products f64 z0 = x02 * y10 - x10 * y02; f64 z1 = x10 * y21 - x21 * y10; f64 z2 = x21 * y02 - x02 * y21; f64 pz0 = x02 * (py - y0) - (px - x0) * y02; f64 pz1 = x10 * (py - y1) - (px - x1) * y10; f64 pz2 = x21 * (py - y2) - (px - x2) * y21; return same_sign_(z0, pz0) && same_sign_(z1, pz1) && same_sign_(z2, pz2); } b8 hit_quad(vec2 vertices[4], vec2 point) { f64 x0 = vertices[0].x; f64 y0 = vertices[0].y; f64 x1 = vertices[1].x; f64 y1 = vertices[1].y; f64 x2 = vertices[2].x; f64 y2 = vertices[2].y; f64 x3 = vertices[3].x; f64 y3 = vertices[3].y; return hit_triangle((vec2[3]) { { x0, y0 }, { x1, y1 }, { x2, y2 }, }, point) || hit_triangle((vec2[3]) { { x0, y0 }, { x2, y2 }, { x3, y3 }, }, point); } b8 hit_ellipse(Box area, vec2 point) { f64 dw = area.width / 2; f64 dh = area.height / 2; if (dw < EPSILON || dh < EPSILON) return 0; f64 cx = area.x + dw; f64 cy = area.y + dh; f64 kx = 1. / dw; f64 ky = 1. / dh; f64 dx = (point.x - cx) * kx; f64 dy = (point.y - cy) * ky; return dx * dx + dy * dy - 1.0 < EPSILON; } b8 hit_line(vec2 vertices[2], f64 width, vec2 point) { f64 x0 = vertices[0].x; f64 y0 = vertices[0].y; f64 x1 = vertices[1].x; f64 y1 = vertices[1].y; f64 dx = x1 - x0; f64 dy = y1 - y0; // Tangent // f64 tx = -dy; f64 ty = dx; f64 tl = sqrt(tx * tx + ty * ty); if (tl >= EPSILON) { tx /= tl; ty /= tl; } tx *= width * .5; ty *= width * .5; return hit_quad((vec2[4]) { { x0 - tx, y0 - ty }, { x0 + tx, y0 + ty }, { x1 + tx, y1 + ty }, { x1 - tx, y1 - ty }, }, point); } void draw_pixels_cached(Box area, Pixel_Buffer src) { perform_graphics_request( (Graphics_Context) {0}, (Graphics_Request) { .op = GRAPHICS_DRAW_PIXELS, .pixels = { .area = area, .src = src, }, } ); } void fill_rectangle_cached(vec4_f32 color, Box area) { perform_graphics_request( (Graphics_Context) {0}, (Graphics_Request) { .op = GRAPHICS_FILL_RECTANGLE, .rectangle = { .color = color, .area = area, }, } ); } void fill_triangle_cached(vec4_f32 color, vec2 vertices[3]) { perform_graphics_request( (Graphics_Context) {0}, (Graphics_Request) { .op = GRAPHICS_FILL_TRIANGLE, .triangle = { .color = color, .vertices = { vertices[0], vertices[1], vertices[2], }, }, } ); } void fill_triangles_cached(vec4_f32 color, vec2 position, vec2 scale, i64 num_triangles, vec2 *vertices) { perform_graphics_request( (Graphics_Context) {0}, (Graphics_Request) { .op = GRAPHICS_FILL_TRIANGLES, .triangles = { .color = color, .position = position, .scale = scale, .num_triangles = num_triangles, .vertices = vertices, }, } ); } void fill_quad_cached(vec4_f32 color, vec2 vertices[4]) { perform_graphics_request( (Graphics_Context) {0}, (Graphics_Request) { .op = GRAPHICS_FILL_QUAD, .quad = { .color = color, .vertices = { vertices[0], vertices[1], vertices[2], vertices[3], }, }, } ); } void fill_ellipse_cached(vec4_f32 color, Box area) { perform_graphics_request( (Graphics_Context) {0}, (Graphics_Request) { .op = GRAPHICS_FILL_ELLIPSE, .ellipse = { .color = color, .area = area, }, } ); } void fill_line_cached(vec4_f32 color, vec2 vertices[2], f64 width) { perform_graphics_request( (Graphics_Context) {0}, (Graphics_Request) { .op = GRAPHICS_FILL_LINE, .line = { .color = color, .vertices = { vertices[0], vertices[1], }, .width = width, }, } ); } void draw_text_area_cached(i32 font, vec4_f32 color, Box area, vec2 max_size, i64 num_chars, c32 *text) { perform_graphics_request( (Graphics_Context) {0}, (Graphics_Request) { .op = GRAPHICS_DRAW_TEXT_AREA, .text_area = { .font = font, .color = color, .area = area, .max_size = max_size, .num_chars = num_chars, .text = text, }, } ); } void draw_text_cursor_cached(i32 font, vec4_f32 color, Box area, vec2 max_size, i64 num_chars, c32 *text, i64 cursor, i64 selection) { perform_graphics_request( (Graphics_Context) {0}, (Graphics_Request) { .op = GRAPHICS_DRAW_TEXT_CURSOR, .text_cursor = { .font = font, .color = color, .area = area, .max_size = max_size, .num_chars = num_chars, .text = text, .cursor = cursor, .selection = selection, }, } ); } void draw_pixels(Box area, Pixel_Buffer src) { perform_graphics_request( (Graphics_Context) { .disable_cache = 1, }, (Graphics_Request) { .op = GRAPHICS_DRAW_PIXELS, .pixels = { .area = area, .src = src, }, } ); } void fill_rectangle(vec4_f32 color, Box area) { perform_graphics_request( (Graphics_Context) { .disable_cache = 1, }, (Graphics_Request) { .op = GRAPHICS_FILL_RECTANGLE, .rectangle = { .color = color, .area = area, }, } ); } void fill_triangle(vec4_f32 color, vec2 vertices[3]) { perform_graphics_request( (Graphics_Context) { .disable_cache = 1, }, (Graphics_Request) { .op = GRAPHICS_FILL_TRIANGLE, .triangle = { .color = color, .vertices = { vertices[0], vertices[1], vertices[2], }, }, } ); } void fill_triangles(vec4_f32 color, vec2 position, vec2 scale, i64 num_triangles, vec2 *vertices) { perform_graphics_request( (Graphics_Context) { .disable_cache = 1, }, (Graphics_Request) { .op = GRAPHICS_FILL_TRIANGLES, .triangles = { .color = color, .position = position, .scale = scale, .num_triangles = num_triangles, .vertices = vertices, }, } ); } void fill_quad(vec4_f32 color, vec2 vertices[4]) { perform_graphics_request( (Graphics_Context) { .disable_cache = 1, }, (Graphics_Request) { .op = GRAPHICS_FILL_QUAD, .quad = { .color = color, .vertices = { vertices[0], vertices[1], vertices[2], vertices[3], }, }, } ); } void fill_ellipse(vec4_f32 color, Box area) { perform_graphics_request( (Graphics_Context) { .disable_cache = 1, }, (Graphics_Request) { .op = GRAPHICS_FILL_ELLIPSE, .ellipse = { .color = color, .area = area, }, } ); } void fill_line(vec4_f32 color, vec2 vertices[2], f64 width) { perform_graphics_request( (Graphics_Context) { .disable_cache = 1, }, (Graphics_Request) { .op = GRAPHICS_FILL_LINE, .line = { .color = color, .vertices = { vertices[0], vertices[1], }, .width = width, }, } ); } void draw_text_area(i32 font, vec4_f32 color, Box area, vec2 max_size, i64 num_chars, c32 *text) { perform_graphics_request( (Graphics_Context) { .disable_cache = 1, }, (Graphics_Request) { .op = GRAPHICS_DRAW_TEXT_AREA, .text_area = { .font = font, .color = color, .area = area, .max_size = max_size, .num_chars = num_chars, .text = text, }, } ); } void draw_text_cursor(i32 font, vec4_f32 color, Box area, vec2 max_size, i64 num_chars, c32 *text, i64 cursor, i64 selection) { perform_graphics_request( (Graphics_Context) { .disable_cache = 1, }, (Graphics_Request) { .op = GRAPHICS_DRAW_TEXT_CURSOR, .text_cursor = { .font = font, .color = color, .area = area, .max_size = max_size, .num_chars = num_chars, .text = text, .cursor = cursor, .selection = selection, }, } ); } // ================================================================ typedef struct { Graphics_Request req; i64 x; i64 y; i64 width; i64 height; } Graphics_Request_Norm_; typedef union { u8 hash[BLAKE2B_OUTBYTES]; u64 index; } Graphics_Request_Id_; typedef struct { b8 occupied; Graphics_Request_Id_ id; Graphics_Request req; i64 width; i64 height; i64 buffer_len; vec4_f32 * buffer; } Graphics_Cache_Slot_; static Graphics_Cache_Slot_ _graphics_cache[GRAPHICS_CACHE_SIZE] = {0}; static i64 _graphics_cache_gc_index = 0; static void graphics_request_hash_(Blake2b_State *S, Graphics_Request req) { switch (req.op) { case GRAPHICS_FILL_TRIANGLES: blake2b_update(S, (u8 *) &req.triangles.color, sizeof req.triangles.color); blake2b_update(S, (u8 *) &req.triangles.position, sizeof req.triangles.position); blake2b_update(S, (u8 *) &req.triangles.scale, sizeof req.triangles.scale); blake2b_update(S, (u8 *) &req.triangles.num_triangles, sizeof req.triangles.num_triangles); blake2b_update(S, (u8 *) req.triangles.vertices, req.triangles.num_triangles * 3 * sizeof *req.triangles.vertices); break; case GRAPHICS_DRAW_PIXELS: blake2b_update(S, (u8 *) &req.pixels.area, sizeof req.pixels.area); blake2b_update(S, (u8 *) &req.pixels.src.width, sizeof req.pixels.src.width); blake2b_update(S, (u8 *) &req.pixels.src.height, sizeof req.pixels.src.height); blake2b_update(S, (u8 *) &req.pixels.src.stride, sizeof req.pixels.src.stride); for (i64 j = 0; j < req.pixels.src.height; ++j) blake2b_update( S, (u8 *) (req.pixels.src.pixels + j * req.pixels.src.stride), req.pixels.src.width * sizeof *req.pixels.src.pixels ); break; case GRAPHICS_DRAW_TEXT_AREA: blake2b_update(S, (u8 *) &req.text_area.font, sizeof req.text_area.font); blake2b_update(S, (u8 *) &req.text_area.color, sizeof req.text_area.color); blake2b_update(S, (u8 *) &req.text_area.area, sizeof req.text_area.area); blake2b_update(S, (u8 *) &req.text_area.max_size, sizeof req.text_area.max_size); blake2b_update(S, (u8 *) &req.text_area.num_chars, sizeof req.text_area.num_chars); blake2b_update( S, (u8 *) req.text_area.text, req.text_area.num_chars * sizeof *req.text_area.text ); break; case GRAPHICS_DRAW_TEXT_CURSOR: blake2b_update(S, (u8 *) &req.text_cursor.font, sizeof req.text_cursor.font); blake2b_update(S, (u8 *) &req.text_cursor.color, sizeof req.text_cursor.color); blake2b_update(S, (u8 *) &req.text_cursor.area, sizeof req.text_cursor.area); blake2b_update(S, (u8 *) &req.text_cursor.max_size, sizeof req.text_cursor.max_size); blake2b_update(S, (u8 *) &req.text_cursor.num_chars, sizeof req.text_cursor.num_chars); blake2b_update(S, (u8 *) req.text_cursor.text, req.text_cursor.num_chars * sizeof *req.text_cursor.text); blake2b_update(S, (u8 *) &req.text_cursor.cursor, sizeof req.text_cursor.cursor); blake2b_update( S, (u8 *) &req.text_cursor.selection, sizeof req.text_cursor.selection ); break; default: blake2b_update(S, (u8 *) &req, sizeof req); } } static Graphics_Request_Id_ graphics_request_id_(Graphics_Request req) { Blake2b_State S[1]; blake2b_init(S, BLAKE2B_OUTBYTES); graphics_request_hash_(S, req); Graphics_Request_Id_ id = {0}; blake2b_final(S, id.hash, BLAKE2B_OUTBYTES); return id; } static Graphics_Request graphics_request_scaled_(Graphics_Request req, vec2 scale) { Graphics_Request scaled = req; switch (req.op) { case GRAPHICS_DRAW_PIXELS: scaled.pixels.area.x *= scale.x; scaled.pixels.area.y *= scale.y; scaled.pixels.area.width *= scale.x; scaled.pixels.area.height *= scale.y; break; case GRAPHICS_FILL_RECTANGLE: scaled.rectangle.area.x *= scale.x; scaled.rectangle.area.y *= scale.y; scaled.rectangle.area.width *= scale.x; scaled.rectangle.area.height *= scale.y; break; case GRAPHICS_FILL_TRIANGLE: scaled.triangle.vertices[0].x *= scale.x; scaled.triangle.vertices[0].y *= scale.y; scaled.triangle.vertices[1].x *= scale.x; scaled.triangle.vertices[1].y *= scale.y; scaled.triangle.vertices[2].x *= scale.x; scaled.triangle.vertices[2].y *= scale.y; break; case GRAPHICS_FILL_TRIANGLES: scaled.triangles.position.x *= scale.x; scaled.triangles.position.y *= scale.y; scaled.triangles.scale.x *= scale.x; scaled.triangles.scale.y *= scale.y; break; case GRAPHICS_FILL_QUAD: scaled.quad.vertices[0].x *= scale.x; scaled.quad.vertices[0].y *= scale.y; scaled.quad.vertices[1].x *= scale.x; scaled.quad.vertices[1].y *= scale.y; scaled.quad.vertices[2].x *= scale.x; scaled.quad.vertices[2].y *= scale.y; scaled.quad.vertices[3].x *= scale.x; scaled.quad.vertices[3].y *= scale.y; break; case GRAPHICS_FILL_ELLIPSE: scaled.ellipse.area.x *= scale.x; scaled.ellipse.area.y *= scale.y; scaled.ellipse.area.width *= scale.x; scaled.ellipse.area.height *= scale.y; break; case GRAPHICS_FILL_LINE: scaled.line.vertices[0].x *= scale.x; scaled.line.vertices[0].y *= scale.y; scaled.line.vertices[1].x *= scale.x; scaled.line.vertices[1].y *= scale.y; f64 dx = req.line.vertices[1].x - req.line.vertices[0].x; f64 dy = req.line.vertices[1].y - req.line.vertices[0].y; f64 l = sqrt(dx * dx + dy * dy); if (l > EPSILON) { dx /= l; dy /= l; f64 tx = dy * scale.x; f64 ty = -dx * scale.y; scaled.line.width *= sqrt(tx * tx + ty * ty); } else scaled.line.width *= (scale.x + scale.y) / 2.0; break; case GRAPHICS_DRAW_TEXT_AREA: scaled.text_area.area.x *= scale.x; scaled.text_area.area.y *= scale.y; scaled.text_area.area.width *= scale.x; scaled.text_area.area.height *= scale.y; scaled.text_area.max_size.x *= scale.x; scaled.text_area.max_size.y *= scale.y; break; case GRAPHICS_DRAW_TEXT_CURSOR: scaled.text_cursor.area.x *= scale.x; scaled.text_cursor.area.y *= scale.y; scaled.text_cursor.area.width *= scale.x; scaled.text_cursor.area.height *= scale.y; scaled.text_cursor.max_size.x *= scale.x; scaled.text_cursor.max_size.y *= scale.y; break; default:; } return scaled; } static Graphics_Request graphics_request_moved_(Graphics_Request req, vec2 offset) { Graphics_Request moved = req; switch (req.op) { case GRAPHICS_DRAW_PIXELS: moved.pixels.area.x += offset.x; moved.pixels.area.y += offset.y; break; case GRAPHICS_FILL_RECTANGLE: moved.rectangle.area.x += offset.x; moved.rectangle.area.y += offset.y; break; case GRAPHICS_FILL_TRIANGLE: moved.triangle.vertices[0].x += offset.x; moved.triangle.vertices[0].y += offset.y; moved.triangle.vertices[1].x += offset.x; moved.triangle.vertices[1].y += offset.y; moved.triangle.vertices[2].x += offset.x; moved.triangle.vertices[2].y += offset.y; break; case GRAPHICS_FILL_TRIANGLES: moved.triangles.position.x += offset.x; moved.triangles.position.y += offset.y; break; case GRAPHICS_FILL_QUAD: moved.quad.vertices[0].x += offset.x; moved.quad.vertices[0].y += offset.y; moved.quad.vertices[1].x += offset.x; moved.quad.vertices[1].y += offset.y; moved.quad.vertices[2].x += offset.x; moved.quad.vertices[2].y += offset.y; moved.quad.vertices[3].x += offset.x; moved.quad.vertices[3].y += offset.y; break; case GRAPHICS_FILL_ELLIPSE: moved.ellipse.area.x += offset.x; moved.ellipse.area.y += offset.y; break; case GRAPHICS_FILL_LINE: moved.line.vertices[0].x += offset.x; moved.line.vertices[0].y += offset.y; moved.line.vertices[1].x += offset.x; moved.line.vertices[1].y += offset.y; break; case GRAPHICS_DRAW_TEXT_AREA: moved.text_area.area.x += offset.x; moved.text_area.area.y += offset.y; break; case GRAPHICS_DRAW_TEXT_CURSOR: moved.text_cursor.area.x += offset.x; moved.text_cursor.area.y += offset.y; break; default:; } return moved; } static Box graphics_request_area_(Graphics_Request req) { Box area = {0}; switch (req.op) { case GRAPHICS_DRAW_PIXELS: area = req.pixels .area; break; case GRAPHICS_FILL_RECTANGLE: area = req.rectangle .area; break; case GRAPHICS_FILL_ELLIPSE: area = req.ellipse .area; break; case GRAPHICS_DRAW_TEXT_AREA: area = req.text_area .area; break; case GRAPHICS_DRAW_TEXT_CURSOR: area = req.text_cursor.area; break; case GRAPHICS_FILL_TRIANGLE: { vec2 *v = req.triangle.vertices; area.x = min3_(v[0].x, v[1].x, v[2].x); area.y = min3_(v[0].y, v[1].y, v[2].y); area.width = max3_(v[0].x, v[1].x, v[2].x) - area.x; area.height = max3_(v[0].y, v[1].y, v[2].y) - area.y; } break; case GRAPHICS_FILL_TRIANGLES: if (req.triangles.num_triangles > 0) { f64 x0, y0, x1, y1; triangles_area_(&x0, &y0, &x1, &y1, req.triangles.position, req.triangles.scale, req.triangles.num_triangles, req.triangles.vertices); area.x = x0; area.y = y0; area.width = x1 - x0; area.height = y1 - y0; } break; case GRAPHICS_FILL_QUAD: case GRAPHICS_FILL_LINE: { vec2 quad[4] = {0}; if (req.op == GRAPHICS_FILL_LINE) quad_from_line_(quad, req.line.vertices, req.line.width); else { quad[0] = req.quad.vertices[0]; quad[1] = req.quad.vertices[1]; quad[2] = req.quad.vertices[2]; quad[3] = req.quad.vertices[3]; } area.x = min4_(quad[0].x, quad[1].x, quad[2].x, quad[3].x); area.y = min4_(quad[0].y, quad[1].y, quad[2].y, quad[3].y); area.width = max4_(quad[0].x, quad[1].x, quad[2].x, quad[3].x) - area.x; area.height = max4_(quad[0].y, quad[1].y, quad[2].y, quad[3].y) - area.y; } break; default:; } return area; } static Graphics_Request_Norm_ normalize_graphics_request_(Graphics_Request req) { Box area = graphics_request_area_(req); i64 x = (i64) floor(area.x - .5); i64 y = (i64) floor(area.y - .5); i64 w = (i64) ceil (area.x + area.width + .5) - x; i64 h = (i64) ceil (area.y + area.height + .5) - y; return (Graphics_Request_Norm_) { .req = graphics_request_moved_(req, (vec2) { -x, -y }), .x = x, .y = y, .width = w, .height = h, }; } static void perform_graphics_request_to_buffer_(Pixel_Buffer dst, Graphics_Request req) { switch (req.op) { case GRAPHICS_DRAW_PIXELS: draw_pixels_to_buffer( dst, req.pixels.area, req.pixels.src ); break; case GRAPHICS_FILL_RECTANGLE: fill_rectangle_to_buffer( dst, req.rectangle.color, req.rectangle.area ); break; case GRAPHICS_FILL_TRIANGLE: fill_triangle_to_buffer( dst, req.triangle.color, req.triangle.vertices ); break; case GRAPHICS_FILL_TRIANGLES: fill_triangles_to_buffer( dst, req.triangles.color, req.triangles.position, req.triangles.scale, req.triangles.num_triangles, req.triangles.vertices ); break; case GRAPHICS_FILL_QUAD: fill_quad_to_buffer( dst, req.quad.color, req.quad.vertices ); break; case GRAPHICS_FILL_ELLIPSE: fill_ellipse_to_buffer( dst, req.ellipse.color, req.ellipse.area ); break; case GRAPHICS_FILL_LINE: fill_line_to_buffer( dst, req.line.color, req.line.vertices, req.line.width ); break; case GRAPHICS_DRAW_TEXT_AREA: draw_text_area_to_buffer( dst, req.text_area.font, req.text_area.color, req.text_area.area, req.text_area.max_size, req.text_area.num_chars, req.text_area.text ); break; case GRAPHICS_DRAW_TEXT_CURSOR: draw_text_cursor_to_buffer( dst, req.text_cursor.font, req.text_cursor.color, req.text_cursor.area, req.text_cursor.max_size, req.text_cursor.num_chars, req.text_cursor.text, req.text_cursor.cursor, req.text_cursor.selection ); break; default:; } } static b8 mem_eq_(i64 size, void *x, void *y) { for (i64 i = 0; i < size; ++i) if (((u8 *) x)[i] != ((u8 *) y)[i]) return 0; return 1; } static void copy_pixels_quick_(Pixel_Buffer dst, i64 x, i64 y, Pixel_Buffer src) { for (i64 j = 0; j < src.height; ++j) { vec4_f32 *s = src.pixels + j * src.stride; vec4_f32 *d = dst.pixels + (y + j) * dst.stride + x; vec4_f32 *d_end = d + src.width; for (; d < d_end; ++d, ++s) { d->x = d->x * (1.f - s->w) + s->x * s->w; d->y = d->y * (1.f - s->w) + s->y * s->w; d->z = d->z * (1.f - s->w) + s->z * s->w; } } } static void perform_graphics_request_cached_(Pixel_Buffer dst, Graphics_Request req) { Graphics_Request_Norm_ norm = normalize_graphics_request_(req); if (norm.width <= 0 || norm.height <= 0) return; Graphics_Request_Id_ id = graphics_request_id_(norm.req); b8 reset_slot = 1; i64 slot = -1; i64 n = (i64) (id.index % (GRAPHICS_CACHE_SIZE / GRAPHICS_CACHE_DEPTH)) * GRAPHICS_CACHE_DEPTH; for (i64 i = 0; i < GRAPHICS_CACHE_DEPTH; ++i) { if (!_graphics_cache[n + i].occupied) continue; if (!mem_eq_(BLAKE2B_OUTBYTES, _graphics_cache[n + i].id.hash, id.hash)) continue; if (!mem_eq_(sizeof req, &_graphics_cache[n + i].req, &norm.req)) continue; reset_slot = 0; slot = n + i; break; } if (slot == -1) for (i64 i = 0; i < GRAPHICS_CACHE_DEPTH; ++i) if (!_graphics_cache[n + i].occupied) { slot = n + i; break; } if (slot == -1) { // Cache overflow. slot = n; } Graphics_Cache_Slot_ *s = _graphics_cache + slot; i64 num_pixels = norm.width * norm.height; if (reset_slot) { s->occupied = 1; s->id = id; s->req = norm.req; s->width = norm.width; s->height = norm.height; if (num_pixels > s->buffer_len) resize_dynamic_array_exact(&s->buffer_len, (void **) &s->buffer, sizeof *s->buffer, num_pixels); if (s->buffer_len < num_pixels) // Out of memory s->occupied = 0; else { mem_set_(s->buffer, 0, s->buffer_len * sizeof *s->buffer); perform_graphics_request_to_buffer_((Pixel_Buffer) { .width = s->width, .height = s->height, .stride = s->width, .pixels = s->buffer, }, s->req); } } if (s->occupied) copy_pixels_quick_(dst, norm.x, norm.y, (Pixel_Buffer) { .width = s->width, .height = s->height, .stride = s->width, .pixels = s->buffer, }); else perform_graphics_request_to_buffer_(dst, req); } static void scale_and_perform_graphics_request_(Graphics_Context context, Graphics_Request req) { Graphics_Request scaled = graphics_request_scaled_(req, context.scale); if (context.disable_cache) perform_graphics_request_to_buffer_(context.dst, scaled); else perform_graphics_request_cached_(context.dst, scaled); } static Graphics_Context graphics_context_defaults_(Graphics_Context context) { if (context.dst.pixels == NULL) { i64 max_height = g_platform.num_pixels / g_platform.frame_width; context.dst.width = g_platform.frame_width; context.dst.height = min2_i64_(max_height, g_platform.frame_height); context.dst.stride = g_platform.frame_width; context.dst.pixels = g_platform.pixels; if (context.scale.x == 0. && context.scale.y == 0.) context.scale = (vec2) { .x = ((f64) g_platform.frame_width) / g_platform.real_width, .y = ((f64) g_platform.frame_height) / g_platform.real_height, }; } return context; } // ================================================================ void perform_graphics_request(Graphics_Context context, Graphics_Request req) { scale_and_perform_graphics_request_(graphics_context_defaults_(context), req); } void clean_graphics_requests_cache(i64 amount) { for (; amount > 0; --amount) { Graphics_Cache_Slot_ *s = _graphics_cache + _graphics_cache_gc_index; i64 required_len = !s->occupied ? 0 : s->width * s->height; if (s->buffer_len > required_len) resize_dynamic_array_exact(&s->buffer_len, (void **) &s->buffer, sizeof *s->buffer, required_len); _graphics_cache_gc_index = (_graphics_cache_gc_index + 1) % GRAPHICS_CACHE_SIZE; } } // ================================================================ // // Test suite // // ================================================================ #if ENABLE_TESTING #define TEST_FILE graphics #include "test.c" TEST("colors") { REQUIRE_EQ(rgb_u32_from_f32((vec3_f32) { 1.f, 0.f, 0.f, }), 0xff0000); REQUIRE_EQ(rgb_u32_from_f32((vec3_f32) { 0.f, 1.f, 0.f, }), 0x00ff00); REQUIRE_EQ(rgb_u32_from_f32((vec3_f32) { 0.f, 0.f, 1.f, }), 0x0000ff); REQUIRE_EQ(rgb_u32_from_f32((vec3_f32) { .5f, 0.f, 0.f, }), 0x800000); REQUIRE_EQ(rgb_u32_from_f32((vec3_f32) { 0.f, .5f, 0.f, }), 0x008000); REQUIRE_EQ(rgb_u32_from_f32((vec3_f32) { 0.f, 0.f, .5f, }), 0x000080); REQUIRE_EQ(rgba_u32_from_f32((vec4_f32) { 1.f, 0.f, 0.f, 0.f, }), 0x00ff0000); REQUIRE_EQ(rgba_u32_from_f32((vec4_f32) { 0.f, 1.f, 0.f, 0.f, }), 0x0000ff00); REQUIRE_EQ(rgba_u32_from_f32((vec4_f32) { 0.f, 0.f, 1.f, 0.f, }), 0x000000ff); REQUIRE_EQ(rgba_u32_from_f32((vec4_f32) { 0.f, 0.f, 0.f, 1.f, }), 0xff000000); REQUIRE_EQ(rgba_u32_from_f32((vec4_f32) { .5f, 0.f, 0.f, 0.f, }), 0x00800000); REQUIRE_EQ(rgba_u32_from_f32((vec4_f32) { 0.f, .5f, 0.f, 0.f, }), 0x00008000); REQUIRE_EQ(rgba_u32_from_f32((vec4_f32) { 0.f, 0.f, .5f, 0.f, }), 0x00000080); REQUIRE_EQ(rgba_u32_from_f32((vec4_f32) { 0.f, 0.f, 0.f, .5f, }), 0x80000000); REQUIRE_EQ(lch_from_rgb((vec3_f32) { 0.f, 0.f, 0.f, }).x * 100, 0); REQUIRE_EQ(lch_from_rgb((vec3_f32) { 1.f, 1.f, 1.f, }).x * 100, 100); REQUIRE_EQ(lch_from_rgb((vec3_f32) { 1.f, 0.f, 0.f, }).x * 100, 62); REQUIRE_EQ(lch_from_rgb((vec3_f32) { 0.f, 1.f, 0.f, }).x * 100, 86); REQUIRE_EQ(lch_from_rgb((vec3_f32) { 0.f, 0.f, 1.f, }).x * 100, 45); REQUIRE_EQ(lch_from_rgb((vec3_f32) { .5f, .5f, .5f, }).y * 100, 0); REQUIRE_EQ(lch_from_rgb((vec3_f32) { 1.f, 0.f, 0.f, }).y * 100, 25); REQUIRE_EQ(lch_from_rgb((vec3_f32) { 0.f, 1.f, 0.f, }).y * 100, 29); REQUIRE_EQ(lch_from_rgb((vec3_f32) { 0.f, 0.f, 1.f, }).y * 100, 31); REQUIRE_EQ(lch_from_rgb((vec3_f32) { 1.f, 0.f, 0.f, }).z * 100, 51); REQUIRE_EQ(lch_from_rgb((vec3_f32) { 0.f, 1.f, 0.f, }).z * 100, 248); REQUIRE_EQ(lch_from_rgb((vec3_f32) { 0.f, 0.f, 1.f, }).z * 100, -167); vec3_f32 white = rgb_from_lch(lch_from_rgb((vec3_f32) { 1.f, 1.f, 1.f, })); REQUIRE_EQ(white.x * 100, 100); REQUIRE_EQ(white.y * 100, 100); REQUIRE_EQ((white.z + 6e-7) * 100, 100); vec3_f32 black = rgb_from_lch(lch_from_rgb((vec3_f32) { 0.f, 0.f, 0.f, })); REQUIRE_EQ(black.x * 100, 0); REQUIRE_EQ(black.y * 100, 0); REQUIRE_EQ(black.z * 100, 0); vec3_f32 grey = rgb_from_lch(lch_from_rgb((vec3_f32) { .5f, .5f, .5f, })); REQUIRE_EQ(grey.x * 100, 50); REQUIRE_EQ((grey.y + 3e-8) * 100, 50); REQUIRE_EQ((grey.z + 3e-7) * 100, 50); vec3_f32 red = rgb_from_lch(lch_from_rgb((vec3_f32) { 1.f, 0.f, 0.f, })); REQUIRE_EQ(red.x * 100, 100); REQUIRE_EQ(red.y * 100, 0); REQUIRE_EQ(red.z * 100, 0); vec3_f32 green = rgb_from_lch(lch_from_rgb((vec3_f32) { 0.f, 1.f, 0.f, })); REQUIRE_EQ(green.x * 100, 0); REQUIRE_EQ((green.y + 3e-7) * 100, 100); REQUIRE_EQ(green.z * 100, 0); vec3_f32 blue = rgb_from_lch(lch_from_rgb((vec3_f32) { 0.f, 0.f, 1.f, })); REQUIRE_EQ(blue.x * 100, 0); REQUIRE_EQ(blue.y * 100, 0); REQUIRE_EQ((blue.z + 3e-7) * 100, 100); vec3_f32 yellow = rgb_from_lch(lch_from_rgb((vec3_f32) { 1.f, 1.f, 0.f, })); REQUIRE_EQ(yellow.x * 100, 100); REQUIRE_EQ((yellow.y + 6e-8) * 100, 100); REQUIRE_EQ(yellow.z * 100, 0); vec3_f32 cyan = rgb_from_lch(lch_from_rgb((vec3_f32) { 0.f, 1.f, 1.f, })); REQUIRE_EQ(cyan.x * 100, 0); REQUIRE_EQ((cyan.y + 3e-7) * 100, 100); REQUIRE_EQ((cyan.z + 3e-7) * 100, 100); vec3_f32 purple = rgb_from_lch(lch_from_rgb((vec3_f32) { 1.f, 0.f, 1.f, })); REQUIRE_EQ(purple.x * 100, 100); REQUIRE_EQ(purple.y * 100, 0); REQUIRE_EQ((purple.z + 3e-7) * 100, 100); } TEST("rectangle corners") { i64 w = 10; i64 h = 10; vec4_f32 pixels[100] = {0}; fill_rectangle_to_buffer( (Pixel_Buffer) { .width = w, .height = h, .stride = w, .pixels = pixels }, (vec4_f32) { 1.0f, 1.0f, 1.0f, 1.0f, }, (Box) { .x = -1, .y = -1, .width = 2, .height = 2, } ); fill_rectangle_to_buffer( (Pixel_Buffer) { .width = w, .height = h, .stride = w, .pixels = pixels }, (vec4_f32) { 1.0f, 1.0f, 1.0f, 1.0f, }, (Box) { .x = 9, .y = -1, .width = 2, .height = 2, } ); fill_rectangle_to_buffer( (Pixel_Buffer) { .width = w, .height = h, .stride = w, .pixels = pixels }, (vec4_f32) { 1.0f, 1.0f, 1.0f, 1.0f, }, (Box) { .x = -1, .y = 9, .width = 2, .height = 2, } ); fill_rectangle_to_buffer( (Pixel_Buffer) { .width = w, .height = h, .stride = w, .pixels = pixels }, (vec4_f32) { 1.0f, 1.0f, 1.0f, 1.0f, }, (Box) { .x = 9, .y = 9, .width = 2, .height = 2, } ); b8 expect_bits[100] = { 1, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 1, }; b8 ok = 1; for (i64 i = 0; i < w * h; ++i) { if (expect_bits[i] != (pixels[i].x > 0.5) || expect_bits[i] != (pixels[i].y > 0.5) || expect_bits[i] != (pixels[i].z > 0.5) || expect_bits[i] != (pixels[i].w > 0.5)) { ok = 0; break; } } REQUIRE(ok); } TEST("rectangle middle") { i64 w = 10; i64 h = 10; vec4_f32 pixels[100] = {0}; fill_rectangle_to_buffer( (Pixel_Buffer) { .width = w, .height = h, .stride = w, .pixels = pixels }, (vec4_f32) { 1.0f, 1.0f, 1.0f, 1.0f, }, (Box) { .x = 1, .y = 1, .width = 8, .height = 8, } ); b8 expect_bits[100] = { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, }; b8 ok = 1; for (i64 i = 0; i < w * h; ++i) { if (expect_bits[i] != (pixels[i].x > 0.5) || expect_bits[i] != (pixels[i].y > 0.5) || expect_bits[i] != (pixels[i].z > 0.5) || expect_bits[i] != (pixels[i].w > 0.5)) { ok = 0; break; } } REQUIRE(ok); } TEST("rectangle aliasing") { i64 w = 10; i64 h = 10; vec4_f32 pixels[100] = {0}; fill_rectangle_to_buffer( (Pixel_Buffer) { .width = w, .height = h, .stride = w, .pixels = pixels }, (vec4_f32) { 1.0f, 1.0f, 1.0f, 1.0f, }, (Box) { .x = 1.4, .y = 1.4, .width = 2.0, .height = 2.0, } ); fill_rectangle_to_buffer( (Pixel_Buffer) { .width = w, .height = h, .stride = w, .pixels = pixels }, (vec4_f32) { 1.0f, 1.0f, 1.0f, 1.0f, }, (Box) { .x = 4.0, .y = 1.0, .width = 2.4, .height = 2.4, } ); fill_rectangle_to_buffer( (Pixel_Buffer) { .width = w, .height = h, .stride = w, .pixels = pixels }, (vec4_f32) { 1.0f, 1.0f, 1.0f, 1.0f, }, (Box) { .x = 6.6, .y = 1.0, .width = 2.0, .height = 2.0, } ); fill_rectangle_to_buffer( (Pixel_Buffer) { .width = w, .height = h, .stride = w, .pixels = pixels }, (vec4_f32) { 1.0f, 1.0f, 1.0f, 1.0f, }, (Box) { .x = 1.0, .y = 3.6, .width = 2.0, .height = 2.0, } ); fill_rectangle_to_buffer( (Pixel_Buffer) { .width = w, .height = h, .stride = w, .pixels = pixels }, (vec4_f32) { 1.0f, 1.0f, 1.0f, 1.0f, }, (Box) { .x = 4.0, .y = 4.0, .width = 2.4, .height = 2.4, } ); fill_rectangle_to_buffer( (Pixel_Buffer) { .width = w, .height = h, .stride = w, .pixels = pixels }, (vec4_f32) { 1.0f, 1.0f, 1.0f, 1.0f, }, (Box) { .x = 7.4, .y = 4.4, .width = 1.2, .height = 1.2, } ); b8 expect_bits[100] = { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 0, 1, 1, 0, 1, 1, 0, 0, 1, 1, 0, 1, 1, 0, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 0, 1, 1, 0, 1, 1, 0, 0, 1, 1, 0, 1, 1, 0, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, }; b8 ok = 1; for (i64 i = 0; i < w * h; ++i) { if (expect_bits[i] != (pixels[i].x > 0.5) || expect_bits[i] != (pixels[i].y > 0.5) || expect_bits[i] != (pixels[i].z > 0.5) || expect_bits[i] != (pixels[i].w > 0.5)) { ok = 0; break; } } REQUIRE(ok); } TEST("text letter A") { i64 w = 10; i64 h = 10; vec4_f32 pixels[100] = {0}; draw_text_area_to_buffer( (Pixel_Buffer) { .width = w, .height = h, .stride = w, .pixels = pixels, }, 0, (vec4_f32) { 1.0f, 1.0f, 1.0f, 1.0f, }, (Box) { .x = 0, .y = 0, .width = 10, .height = 10, }, (vec2) { .x = 10.0, .y = 10.0, }, 1, (c32[1]) { 'A', } ); b8 expect_bits[100] = { 0, 1, 1, 1, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 1, 1, 0, 0, 0, 0, 1, 0, 0, 0, 1, 1, 0, 0, 0, 0, 1, 0, 0, 0, 1, 1, 0, 0, 0, 0, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 1, 0, 0, 0, 1, 1, 0, 0, 0, 0, 1, 0, 0, 0, 1, 1, 0, 0, 0, 0, 1, 0, 0, 0, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, }; b8 ok = 1; for (i64 i = 0; i < w * h; ++i) { if (expect_bits[i] != (pixels[i].x > 0.5) || expect_bits[i] != (pixels[i].y > 0.5) || expect_bits[i] != (pixels[i].z > 0.5) || expect_bits[i] != (pixels[i].w > 0.5)) { ok = 0; break; } } REQUIRE(ok); } TEST("text letter z") { i64 w = 10; i64 h = 10; vec4_f32 pixels[100] = {0}; draw_text_area_to_buffer( (Pixel_Buffer) { .width = w, .height = h, .stride = w, .pixels = pixels, }, 0, (vec4_f32) { 1.0f, 1.0f, 1.0f, 1.0f, }, (Box) { .x = 0, .y = 0, .width = 10, .height = 10, }, (vec2) { .x = 10.0, .y = 10.0, }, 1, (c32[1]) { 'z', } ); b8 expect_bits[100] = { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 0, 0, 0, 0, 0, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, }; b8 ok = 1; for (i64 i = 0; i < w * h; ++i) { if (expect_bits[i] != (pixels[i].x > 0.5) || expect_bits[i] != (pixels[i].y > 0.5) || expect_bits[i] != (pixels[i].z > 0.5) || expect_bits[i] != (pixels[i].w > 0.5)) { ok = 0; break; } } REQUIRE(ok); } TEST("text digits 42") { i64 w = 10; i64 h = 10; vec4_f32 pixels[100] = {0}; draw_text_area_to_buffer( (Pixel_Buffer) { .width = w, .height = h, .stride = w, .pixels = pixels, }, 0, (vec4_f32) { 1.0f, 1.0f, 1.0f, 1.0f, }, (Box) { .x = 0, .y = 0, .width = 10, .height = 10, }, (vec2) { .x = 10.0, .y = 10.0, }, 2, (c32[2]) { '4', '2', } ); b8 expect_bits[100] = { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 1, 0, 0, 1, 1, 1, 1, 1, 0, 0, 1, 0, 0, 1, 1, 1, 1, 1, 0, 0, 1, 0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 0, 0, 0, 1, 1, 0, 1, 1, 1, 1, 0, 0, 0, 1, 1, 0, 0, 0, 0, 1, 0, 0, 1, 0, 0, 0, 0, 0, 0, 1, 0, 0, 1, 1, 1, 1, 0, 0, 0, 1, 0, 0, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, }; b8 ok = 1; for (i64 i = 0; i < w * h; ++i) { if (expect_bits[i] != (pixels[i].x > 0.5) || expect_bits[i] != (pixels[i].y > 0.5) || expect_bits[i] != (pixels[i].z > 0.5) || expect_bits[i] != (pixels[i].w > 0.5)) { ok = 0; break; } } REQUIRE(ok); } #undef TEST_FILE #endif // ENABLE_TESTING // ================================================================ #endif // GRAPHICS_IMPL_GUARD_ #endif // GRAPHICS_HEADER