#if 0 /* #/ ================================================================ #/ #/ graphics.c #/ #/ ---------------------------------------------------------------- #/ #/ (C) 2025 Mitya Selivanov #/ #/ ================================================================ #/ #/ Self-testing shell script #/ #/ ================================================================ SRC=${0##*./} BIN=${SRC%.*} gcc \ -Wall -Wextra -Werror -pedantic \ -Wno-missing-braces \ -Wno-old-style-declaration \ -Wno-overlength-strings \ -O3 -D NDEBUG \ -D NO_WAYLAND=1 \ -D GRAPHICS_TEST_SUITE \ -lX11 -lm -lasound \ -o $BIN $SRC && \ ./$BIN $@ STATUS=$? rm -f $BIN exit $? # */ #endif // ================================================================ #ifndef GRAPHICS_HEADER_GUARD_ #define GRAPHICS_HEADER_GUARD_ #ifdef EVERY_TEST_SUITE #define GRAPHICS_TEST_SUITE #endif #ifdef GRAPHICS_HEADER #define REDUCED_SYSTEM_LAYER_HEADER #endif #include "reduced_system_layer.c" // ================================================================ // // Options // // ================================================================ #ifndef EPSILON #define EPSILON (1e-9) #endif #ifndef ANTIALIASING_SCALE #define ANTIALIASING_SCALE 3 #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); // ================================================================ typedef struct { // NOTE: Dimensions are in pixels. i64 width; i64 height; i64 stride; vec4_f32 *pixels; } Pixel_Buffer; 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 rectangle_contains(f64 x0, f64 y0, f64 width, f64 height, f64 px, f64 py); b8 triangle_contains (f64 x0, f64 y0, f64 x1, f64 y1, f64 x2, f64 y2, f64 px, f64 py); b8 quad_contains (f64 x0, f64 y0, f64 x1, f64 y1, f64 x2, f64 y2, f64 x3, f64 y3, f64 px, f64 py); b8 ellipse_contains (f64 x0, f64 y0, f64 width, f64 height, f64 px, f64 py); b8 line_contains (f64 x0, f64 y0, f64 x1, f64 y1, f64 width, f64 px, f64 py); // ================================================================ enum { OP_NONE = 0, OP_REQUESTS, OP_PIXELS, OP_RECTANGLE, OP_TRIANGLE, OP_QUAD, OP_ELLIPSE, OP_LINE, OP_TEXT_AREA, OP_TEXT_CURSOR, }; typedef struct { f64 x; f64 y; f64 width; f64 height; } Box; typedef struct gx_request { u16 op; union { struct { i64 num; struct gx_request *values; } requests; 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 vertices[4]; } quad; struct { vec4_f32 color; Box area; } ellipse; struct { vec4_f32 color; vec2 vertices[2]; f64 width; } line; struct { vec4_f32 color; Box area; vec2 max_size; i64 num_chars; c32 * text; } text_area; struct { vec4_f32 color; Box area; vec2 max_size; i64 cursor; i64 selection; i64 num_chars; c32 * text; } text_cursor; }; } Gx_Request; typedef struct { i64 x; i64 y; Pixel_Buffer buffer; } Gx_Baked; typedef struct { i64 sketch_size; vec4_f32 *sketch; vec2 scale; } Gx_Context; void gx_render(Gx_Context context, Pixel_Buffer dst, Gx_Request req); #endif // GRAPHICS_HEADER_GUARD_ // ================================================================ #ifndef GRAPHICS_HEADER #ifndef GRAPHICS_IMPL_GUARD_ #define GRAPHICS_IMPL_GUARD_ 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 f32 max2_f32_(f32 a, f32 b) { return a < b ? b : a; } static f64 min3_(f64 a, f64 b, f64 c) { if (a < b && a < c) return a; if (b < c) return b; return c; } static f64 max3_(f64 a, f64 b, f64 c) { if (a > b && a > c) return a; if (b > c) return b; return c; } static b8 same_sign_(f64 a, f64 b) { if (a >= EPSILON && b <= -EPSILON) return 0; if (a <= -EPSILON && b >= EPSILON) return 0; return 1; } static f64 gamma_(f64 x) { if (x >= 0.0031308) return 1.055 * pow(x, 1.0 / 2.4) - 0.055; return 12.92 * x; } static f64 gamma_re_(f64 x) { if (x >= 0.04045) return pow((x + 0.055) / 1.055, 2.4); return x / 12.92; } static void put_pixel_alpha_(Pixel_Buffer dst, vec4_f32 color, i64 x, i64 y) { i64 n = y * dst.stride + x; if (color.w == 1.f) dst.pixels[n] = color; else { vec4_f32 dst_color = dst.pixels[n]; f32 dst_amount = 1. - color.w; dst.pixels[n] = (vec4_f32) { .x = dst_color.x * dst_amount + color.x * color.w, .y = dst_color.y * dst_amount + color.y * color.w, .z = dst_color.z * dst_amount + color.z * color.w, .w = max2_f32_(dst_color.w, color.w), }; } } void draw_pixels_raw(Pixel_Buffer dst, f64 x0, f64 y0, f64 w, f64 h, Pixel_Buffer src) { f64 x1 = x0 + w; f64 y1 = y0 + h; if (w < EPSILON || h < EPSILON) return; i64 i0 = (i64) floor(x0); i64 i1 = (i64) floor(x1); i64 j0 = (i64) floor(y0); i64 j1 = (i64) floor(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; f64 w_inv = 1. / w; f64 h_inv = 1. / h; for (i64 j = j0; j <= j1; ++j) { i64 src_j0 = (i64) floor(((j - y0) * src.height) * h_inv + .5); i64 src_j1 = (i64) floor(((j + 1 - y0) * src.height) * h_inv + .5); if (src_j0 == src_j1) src_j1 = src_j0 + 1; if (src_j1 <= 0 || src_j0 >= src.height) continue; if (src_j0 < 0) src_j0 = 0; if (src_j1 > src.height) src_j1 = src.height; for (i64 i = i0; i <= i1; ++i) { i64 src_i0 = (i64) floor(((i - x0) * src.width) * w_inv + .5); i64 src_i1 = (i64) floor(((i + 1 - x0) * src.width) * w_inv + .5); if (src_i0 == src_i1) src_i1 = src_i0 + 1; if (src_i1 <= 0 || src_i0 >= src.width) continue; if (src_i0 < 0) src_i0 = 0; if (src_i1 > src.width) src_i1 = src.width; vec4_f32 color = {0}; i64 n = 0; for (i64 jj = src_j0; jj < src_j1; ++jj) for (i64 ii = src_i0; ii < src_i1; ++ii) { color.x += gamma_re_(src.pixels[jj * src.stride + ii].x); color.y += gamma_re_(src.pixels[jj * src.stride + ii].y); color.z += gamma_re_(src.pixels[jj * src.stride + ii].z); color.w += gamma_re_(src.pixels[jj * src.stride + ii].w); ++n; } if (n == 0) continue; f32 n_inv = 1.f / n; color.x = gamma_(color.x * n_inv); color.y = gamma_(color.y * n_inv); color.z = gamma_(color.z * n_inv); color.w = gamma_(color.w * n_inv); put_pixel_alpha_(dst, color, i, j); } } } void fill_rectangle_raw(Pixel_Buffer dst, vec4_f32 color, f64 x0, f64 y0, f64 w, f64 h) { f64 x1 = x0 + w; f64 y1 = y0 + h; i64 i0 = (i64) ceil (x0); i64 i1 = (i64) floor(x1); i64 j0 = (i64) ceil (y0); i64 j1 = (i64) floor(y1); i64 i00 = i0; i64 j00 = j0; i64 i10 = i1; i64 j10 = j1; if (i0 < 0) i0 = 0; if (i1 > dst.width) i1 = dst.width; if (j0 < 0) j0 = 0; if (j1 > dst.height) j1 = dst.height; f64 kx0 = i00 - x0; f64 kx1 = x1 - i10; f64 ky0 = j00 - y0; f64 ky1 = y1 - j10; for (i64 j = j0; j < j1; ++j) for (i64 i = i0; i < i1; ++i) put_pixel_alpha_(dst, color, i, j); if (i00 > i10) { kx0 *= kx1; kx1 = 0.; } if (j00 > j10) { ky0 *= ky1; ky1 = 0.; } f64 alpha = color.w; if (i00 - 1 >= 0 && i00 - 1 < dst.width) { color.w = gamma_(gamma_re_(alpha) * kx0); for (i64 j = j0; j < j1; ++j) put_pixel_alpha_(dst, color, i00 - 1, j); } if (i10 >= 0 && i10 < dst.width) { color.w = gamma_(gamma_re_(alpha) * kx1); for (i64 j = j0; j < j1; ++j) put_pixel_alpha_(dst, color, i10, j); } if (j00 - 1 >= 0 && j00 - 1 < dst.height) { color.w = gamma_(gamma_re_(alpha) * ky0); for (i64 i = i0; i < i1; ++i) put_pixel_alpha_(dst, color, i, j00 - 1); } if (j10 >= 0 && j10 < dst.height) { color.w = gamma_(gamma_re_(alpha) * ky1); for (i64 i = i0; i < i1; ++i) put_pixel_alpha_(dst, color, i, j10); } if ( i00 - 1 >= 0 && i00 - 1 < dst.width && j00 - 1 >= 0 && j00 - 1 < dst.height) { color.w = gamma_(gamma_re_(alpha) * kx0 * ky0); put_pixel_alpha_(dst, color, i00 - 1, j00 - 1); } if ( i10 >= 0 && i10 < dst.width && j00 - 1 >= 0 && j00 - 1 < dst.height) { color.w = gamma_(gamma_re_(alpha) * kx1 * ky0); put_pixel_alpha_(dst, color, i10, j00 - 1); } if ( i00 - 1 >= 0 && i00 - 1 < dst.width && j10 >= 0 && j10 < dst.height) { color.w = gamma_(gamma_re_(alpha) * kx0 * ky1); put_pixel_alpha_(dst, color, i00 - 1, j10); } if ( i10 >= 0 && i10 < dst.width && j10 >= 0 && j10 < dst.height) { color.w = gamma_(gamma_re_(alpha) * kx1 * ky1); put_pixel_alpha_(dst, color, i10, j10); } } void fill_triangle_raw(Pixel_Buffer dst, vec4_f32 color, f64 x0, f64 y0, f64 x1, f64 y1, f64 x2, f64 y2) { 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 (triangle_contains(x0, y0, x1, y1, x2, y2, (f64) i, (f64) j)) { left = i; break; } for (i64 i = i1; i >= i0; --i) if (triangle_contains(x0, y0, x1, y1, x2, y2, (f64) i, (f64) j)) { right = i; break; } for (i64 i = left; i <= right; ++i) put_pixel_alpha_(dst, color, i, j); } } void fill_quad_raw(Pixel_Buffer dst, vec4_f32 color, f64 x0, f64 y0, f64 x1, f64 y1, f64 x2, f64 y2, f64 x3, f64 y3) { 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 (quad_contains(x0, y0, x1, y1, x2, y2, x3, y3, (f64) i, (f64) j)) { left = i; break; } for (i64 i = i1; i >= i0; --i) if (quad_contains(x0, y0, x1, y1, x2, y2, x3, y3, (f64) i, (f64) j)) { right = i; break; } for (i64 i = left; i <= right; ++i) put_pixel_alpha_(dst, color, i, j); } } void fill_ellipse_raw(Pixel_Buffer dst, vec4_f32 color, f64 x0, f64 y0, f64 w, f64 h) { f64 y1 = y0 + h; i64 j0 = (i64) floor(y0); i64 j1 = (i64) ceil (y1); if (j0 < 0) j0 = 0; if (j1 >= dst.height) j1 = dst.height - 1; f64 half_w = w / 2.0; f64 half_h = h / 2.0; if (half_h < EPSILON) return; f64 inv_half_h = 1.0 / half_h; f64 cx = x0 + half_w; f64 cy = y0 + 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; for (i64 i = left; i <= right; ++i) put_pixel_alpha_(dst, color, i, j); } } void fill_line_raw(Pixel_Buffer dst, vec4_f32 color, f64 x0, f64 y0, f64 x1, f64 y1, f64 width) { if (width < EPSILON) return; 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; f64 k = .5 / tl; tx *= width * k; ty *= width * k; fill_quad_raw(dst, color, x0 - tx, y0 - ty, x0 + tx, y0 + ty, x1 + tx, y1 + ty, x1 - tx, y1 - ty); } 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) * num_cols) * w_inv + .5); i64 offset = char_column_offset_(text[n], column); for (i64 j = j0; j <= j1; ++j) { i64 row = (i64) floor(((j - y) * CHAR_NUM_BITS_Y_) * h_inv + .5); if (char_bit_(offset, row)) put_pixel_alpha_(dst, color, i, j); } } } x += kx * (num_cols + char_spacing_(num_chars, text, n)); } } void draw_text_area_raw(Pixel_Buffer dst, vec4_f32 color, f64 x, f64 y, f64 width, f64 height, f64 max_width, f64 max_height, i64 num_chars, c32 *text) { if (text == NULL || num_chars <= 0 || max_width < EPSILON || max_height < EPSILON) return; i64 num_columns = enum_text_columns_(num_chars, text); i64 num_rows = enum_text_rows_(num_chars, text); f64 scale_x = width / num_columns; f64 scale_y = height / num_rows; f64 kx = scale_x / max_width; f64 ky = scale_y / max_height; f64 k = kx < ky ? kx : ky; kx = k * max_width; ky = k * max_height; draw_text_(dst, color, x, y, kx, ky, num_chars, text); } void draw_text_cursor_raw(Pixel_Buffer dst, vec4_f32 color, f64 x, f64 y, f64 width, f64 height, f64 max_width, f64 max_height, i64 cursor, i64 selection, i64 num_chars, c32 *text) { if (max_width < EPSILON || max_height < EPSILON) return; 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 = width / num_columns; f64 scale_y = height / num_rows; f64 kx = scale_x / max_width; f64 ky = scale_y / max_height; f64 k = kx < ky ? kx : ky; kx = k * max_width; ky = k * max_height; 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_raw( dst, color, x + kx * cursor_x, y + ky * cursor_y - ky * (CHAR_NUM_BITS_Y_ + 1), kx * (selection_x - cursor_x), ky * (CHAR_NUM_BITS_Y_ + 1) ); else { fill_rectangle_raw( dst, color, x + kx * cursor_x, y + ky * cursor_y - ky * (CHAR_NUM_BITS_Y_ + 1), kx * (num_columns - cursor_x), 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_raw( dst, color, x, y + ky * j - ky * (CHAR_NUM_BITS_Y_ + 1), kx * num_columns, ky * (CHAR_NUM_BITS_Y_ + 1) ); fill_rectangle_raw( dst, color, x, y + ky * selection_y - ky * (CHAR_NUM_BITS_Y_ + 1), kx * selection_x, ky * (CHAR_NUM_BITS_Y_ + 1) ); } } else fill_rectangle_raw( dst, color, x + kx * cursor_x, y + ky * cursor_y - ky * CHAR_NUM_BITS_Y_, kx * .5, ky * (CHAR_NUM_BITS_Y_ - 1) ); } // ================================================================ 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 = (f32) gamma_(rgb.x), .y = (f32) gamma_(rgb.y), .z = (f32) gamma_(rgb.z), }; } vec3_f32 rgb_gamma_remove(vec3_f32 rgb) { return (vec3_f32) { .x = (f32) gamma_re_(rgb.x), .y = (f32) gamma_re_(rgb.y), .z = (f32) gamma_re_(rgb.z), }; } vec4_f32 rgba_gamma_add(vec4_f32 rgba) { return (vec4_f32) { .x = (f32) gamma_(rgba.x), .y = (f32) gamma_(rgba.y), .z = (f32) gamma_(rgba.z), .w = (f32) gamma_(rgba.w), }; } vec4_f32 rgba_gamma_remove(vec4_f32 rgba) { return (vec4_f32) { .x = (f32) gamma_re_(rgba.x), .y = (f32) gamma_re_(rgba.y), .z = (f32) gamma_re_(rgba.z), .w = (f32) 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 rectangle_contains(f64 x0, f64 y0, f64 width, f64 height, f64 px, f64 py) { return px >= x0 && px < x0 + width && py >= y0 && py < y0 + height; } b8 triangle_contains(f64 x0, f64 y0, f64 x1, f64 y1, f64 x2, f64 y2, f64 px, f64 py) { // Z-components of cross-products // f64 x10 = x1 - x0; f64 x21 = x2 - x1; f64 x02 = x0 - x2; f64 y10 = y1 - y0; f64 y21 = y2 - y1; f64 y02 = y0 - y2; 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 quad_contains(f64 x0, f64 y0, f64 x1, f64 y1, f64 x2, f64 y2, f64 x3, f64 y3, f64 px, f64 py) { return triangle_contains(x0, y0, x1, y1, x2, y2, px, py) || triangle_contains(x0, y0, x2, y2, x3, y3, px, py); } b8 ellipse_contains(f64 x0, f64 y0, f64 width, f64 height, f64 px, f64 py) { f64 dw = width / 2; f64 dh = height / 2; if (dw < EPSILON || dh < EPSILON) return 0; f64 cx = x0 + dw; f64 cy = y0 + dh; f64 kx = 1. / dw; f64 ky = 1. / dh; f64 dx = (px - cx) * kx; f64 dy = (py - cy) * ky; return dx * dx + dy * dy - 1.0 < EPSILON; } b8 line_contains(f64 x0, f64 y0, f64 x1, f64 y1, f64 width, f64 px, f64 py) { 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 quad_contains(x0 - tx, y0 - ty, x0 + tx, y0 + ty, x1 + tx, y1 + ty, x1 - tx, y1 - ty, px, py); } // ================================================================ void gx_render_raw_(Gx_Context context, Pixel_Buffer dst, Gx_Request req) { switch (req.op) { case OP_REQUESTS: for (i64 i = 0; i < req.requests.num; ++i) gx_render_raw_(context, dst, req.requests.values[i]); break; case OP_PIXELS: draw_pixels_raw( dst, req.pixels.area.x, req.pixels.area.y, req.pixels.area.width, req.pixels.area.height, req.pixels.src ); break; case OP_RECTANGLE: fill_rectangle_raw( dst, req.rectangle.color, req.rectangle.area.x, req.rectangle.area.y, req.rectangle.area.width, req.rectangle.area.height ); break; case OP_TRIANGLE: fill_triangle_raw( dst, req.triangle.color, req.triangle.vertices[0].x, req.triangle.vertices[0].y, req.triangle.vertices[1].x, req.triangle.vertices[1].y, req.triangle.vertices[2].x, req.triangle.vertices[2].y ); break; case OP_QUAD: fill_quad_raw( dst, req.quad.color, req.quad.vertices[0].x, req.quad.vertices[0].y, req.quad.vertices[1].x, req.quad.vertices[1].y, req.quad.vertices[2].x, req.quad.vertices[2].y, req.quad.vertices[3].x, req.quad.vertices[3].y ); break; case OP_ELLIPSE: fill_ellipse_raw( dst, req.ellipse.color, req.ellipse.area.x, req.ellipse.area.y, req.ellipse.area.width, req.ellipse.area.height ); break; case OP_LINE: fill_line_raw( dst, req.line.color, req.line.vertices[0].x, req.line.vertices[0].y, req.line.vertices[1].x, req.line.vertices[1].y, req.line.width ); break; case OP_TEXT_AREA: draw_text_area_raw( dst, req.text_area.color, req.text_area.area.x, req.text_area.area.y, req.text_area.area.width, req.text_area.area.height, req.text_area.max_size.x, req.text_area.max_size.y, req.text_area.num_chars, req.text_area.text ); break; case OP_TEXT_CURSOR: draw_text_cursor_raw( dst, req.text_cursor.color, req.text_cursor.area.x, req.text_cursor.area.y, req.text_cursor.area.width, req.text_cursor.area.height, req.text_cursor.max_size.x, req.text_cursor.max_size.y, req.text_cursor.cursor, req.text_cursor.selection, req.text_cursor.num_chars, req.text_cursor.text ); break; default:; } } Gx_Request gx_scaled_(Gx_Request req, vec2 scale) { Gx_Request scaled = req; switch (req.op) { case OP_REQUESTS: for (i64 i = 0; i < req.requests.num; ++i) scaled.requests.values[i] = gx_scaled_(req.requests.values[i], scale); break; case OP_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 OP_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 OP_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 OP_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 OP_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 OP_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 OP_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 OP_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; } void gx_render_(Gx_Context context, Pixel_Buffer dst, Gx_Request req) { gx_render_raw_(context, dst, gx_scaled_(req, context.scale)); } void gx_render(Gx_Context context, Pixel_Buffer dst, Gx_Request req) { if (context.sketch == NULL) { context.sketch_size = MAX_NUM_PIXELS; context.sketch = g_platform.sketch; } if (dst.pixels == NULL) { dst.width = g_platform.frame_width; dst.height = g_platform.frame_height; dst.stride = g_platform.frame_width; 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, }; } gx_render_(context, dst, req); } // ================================================================ // // Test suite // // ================================================================ #ifdef GRAPHICS_TEST_SUITE #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 + 3e-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 * 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 + 6e-8) * 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 + 2e-7) * 100, 100); REQUIRE_EQ((cyan.z + 2e-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 + 2e-7) * 100, 100); } Pixel_Buffer pixels = { .width = 1280, .height = 720, .stride = 1280, .pixels = g_platform.sketch, }; vec4_f32 color = { 1., 1., 1., 1., }; BENCHMARK("fill rectangle") { BENCHMARK_BEGIN; { fill_rectangle_raw(pixels, color, 100, 100, 300, 200); } BENCHMARK_END; } BENCHMARK("fill triangle") { BENCHMARK_BEGIN; { fill_triangle_raw(pixels, color, 100, 100, 300, 100, 200, 250); } BENCHMARK_END; } BENCHMARK("fill quad") { BENCHMARK_BEGIN; { fill_quad_raw(pixels, color, 100, 100, 300, 100, 300, 200, 100, 200); } BENCHMARK_END; } BENCHMARK("fill ellipse") { BENCHMARK_BEGIN; { fill_ellipse_raw(pixels, color, 80, 80, 340, 240); } BENCHMARK_END; } BENCHMARK("fill line") { BENCHMARK_BEGIN; { fill_line_raw(pixels, color, 100, 100, 300, 200, 40); } BENCHMARK_END; } BENCHMARK("draw text area") { BENCHMARK_BEGIN; { c32 text[] = { 'H', 'e', 'l', 'l', 'o', ',', ' ', 'S', 'a', 'i', 'l', 'o', 'r', '!', }; draw_text_area_raw(pixels, color, 100, 100, 300, 200, 100, 200, sizeof text / sizeof *text, text); } BENCHMARK_END; } #ifndef EVERY_TEST_SUITE void update_and_render_frame(void) {} i32 main(i32 argc, c8 **argv) { return run_tests_and_benchmarks(argc, argv); } #endif #undef TEST_FILE #endif // GRAPHICS_TEST_SUITE // ================================================================ #endif // GRAPHICS_IMPL_GUARD_ #endif // GRAPHICS_HEADER