From 600df042f89ab4d3ce72b84c83ca4134d69f67bf Mon Sep 17 00:00:00 2001 From: Mitya Selivanov Date: Thu, 23 Jan 2025 13:23:23 +0100 Subject: Cleanup --- graphics.c | 91 ++++++++++++++++++++++++++++---------------------------------- 1 file changed, 41 insertions(+), 50 deletions(-) (limited to 'graphics.c') diff --git a/graphics.c b/graphics.c index ec1e243..49a7e04 100755 --- a/graphics.c +++ b/graphics.c @@ -41,6 +41,7 @@ exit $? # */ #define REDUCED_SYSTEM_LAYER_HEADER #endif +#define NO_WAYLAND 1 #include "reduced_system_layer.c" // ================================================================ @@ -217,6 +218,10 @@ enum { 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; @@ -233,26 +238,6 @@ static f64 max3_(f64 a, f64 b, f64 c) { return c; } -static f64 min4_(f64 a, f64 b, f64 c, f64 d) { - if (a < b && a < c && a < d) - return a; - if (b < c && b < d) - return b; - if (c < d) - return c; - return d; -} - -static f64 max4_(f64 a, f64 b, f64 c, f64 d) { - if (a > b && a > c && a > d) - return a; - if (b > c && b > d) - return b; - if (c > d) - return c; - return d; -} - static b8 same_sign_(f64 a, f64 b) { if (a >= EPSILON && b <= -EPSILON) return 0; if (a <= -EPSILON && b >= EPSILON) return 0; @@ -274,15 +259,19 @@ static f64 gamma_re_(f64 x) { static void put_pixel_alpha_(Pixel_Buffer dst, vec4_f32 color, i64 x, i64 y) { i64 n = y * dst.stride + x; - vec4_f32 dst_color = dst.pixels[n]; - f64 a = color.w; - - dst.pixels[n] = (vec4_f32) { - .x = (f32) (dst_color.x * (1. - a) + color.x * a), - .y = (f32) (dst_color.y * (1. - a) + color.y * a), - .z = (f32) (dst_color.z * (1. - a) + color.z * a), - .w = 1., - }; + 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) { @@ -442,6 +431,8 @@ void fill_triangle_raw(Pixel_Buffer dst, vec4_f32 color, f64 x0, f64 y0, f64 x1, 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; @@ -474,6 +465,8 @@ void fill_quad_raw(Pixel_Buffer dst, vec4_f32 color, f64 x0, f64 y0, f64 x1, f64 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; @@ -495,33 +488,33 @@ void fill_quad_raw(Pixel_Buffer dst, vec4_f32 color, f64 x0, f64 y0, f64 x1, f64 } void fill_ellipse_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) floor(x0); - i64 i1 = (i64) ceil (x1); 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) { - i64 left = i1; - i64 right = i0; + f64 ry = (j - cy) * inv_half_h; + if (ry > 1.0 - EPSILON || ry < -1.0 + EPSILON) + continue; - for (i64 i = i0; i <= i1; ++i) - if (ellipse_contains(x0, y0, x1 - x0, y1 - y0, (f64) i, (f64) j)) { - left = i; - right = (i64) ceil(x0 + x1 - left); // symmetry - break; - } + f64 dx = half_w * sqrt(1.0 - ry * ry); - for (i64 i = right; i >= i0; --i) - if (ellipse_contains(x0, y0, x1 - x0, y1 - y0, (f64) i, (f64) j)) { - right = i; - break; - } + 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; @@ -1164,9 +1157,7 @@ b8 line_contains(f64 x0, f64 y0, f64 x1, f64 y1, f64 width, f64 px, f64 py) { tx *= width * .5; ty *= width * .5; - return - triangle_contains(x0 - tx, y0 - ty, x0 + tx, y0 + ty, x1 + tx, y1 + ty, px, py) - || triangle_contains(x0 - tx, y0 - ty, x1 + tx, y1 + ty, x1 - tx, y1 - ty, px, py); + return quad_contains(x0 - tx, y0 - ty, x0 + tx, y0 + ty, x1 + tx, y1 + ty, x1 - tx, y1 - ty, px, py); } // ================================================================ @@ -1326,8 +1317,8 @@ Gx_Request gx_scaled_(Gx_Request req, vec2 scale) { scaled.quad.vertices[1].y *= scale.y; scaled.quad.vertices[2].x *= scale.x; scaled.quad.vertices[2].y *= scale.y; - scaled.quad.vertices[4].x *= scale.x; - scaled.quad.vertices[4].y *= scale.y; + scaled.quad.vertices[3].x *= scale.x; + scaled.quad.vertices[3].y *= scale.y; break; case OP_ELLIPSE: -- cgit v1.2.3