Kumo
A hand-rolled game engine.
git clone https://git.sentomk.me/Kumo.git
commit a239e22718794310e68a70b4a42a2ff4c649956a parent bbf8c6157afb5014247228b8d70e4cdc0d2a32c1 Author: sentomk <sentomk@proton.me> Date: Tue, 13 Oct 2026 20:12:42 +0800 test: cover the drawing primitives of the rasterizer Check clear, fill_rect and draw_line, including clipping, both end points and the pixel picked when two are equally close to the ideal line, which is pinned with small pictures so that it is the same on every platform. For fill_triangle: two triangles sharing an edge draw each pixel exactly once in either winding, on a buffer that is not square, so that a mix-up of width and height shows. The result does not depend on the order of the corners, degenerate and clipped triangles behave, and 300 random triangles agree with an independent floating point version everywhere except exactly on an edge, which the top-left rule decides. Diffstat:
| A | tests/unit/raster_test.cpp | | | 292 | +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ |
1 file changed, 292 insertions(+), 0 deletions(-)
diff --git a/tests/unit/raster_test.cpp b/tests/unit/raster_test.cpp @@ -0,0 +1,292 @@ +#include "render/raster.h" + +#include <algorithm> +#include <array> +#include <cstddef> +#include <cstdint> + +#include "check.h" +#include "render/frame_buffer.h" + +using kumo::Color; +using kumo::FrameBuffer; +using kumo::Point; + +namespace { + +constexpr Color kOn = 0xFFFFFFFF; + +// The number of pixels that are not zero. +int count_set(const FrameBuffer& frame) { + return static_cast<int>(std::count_if(frame.pixels.begin(), + frame.pixels.end(), + [](Color c) { return c != 0; })); +} + +// A small deterministic random number generator (an LCG). The standard +// distributions are not used, because their results differ between standard +// libraries and these tests must behave the same everywhere. +struct Random { + uint64_t state; + uint32_t next() { + state = state * 6364136223846793005ull + 1442695040888963407ull; + return static_cast<uint32_t>(state >> 33); + } + // A value in [low, high]. + int between(int low, int high) { + return low + + static_cast<int>(next() % static_cast<uint32_t>(high - low + 1)); + } +}; + +// Splits the rectangle with corners (x0, y0) and (x1, y1) into two triangles, +// the second one given in the opposite winding, and checks that every pixel of +// the rectangle is drawn exactly once and nothing else is drawn. The diagonal +// goes through pixel centers whenever the rectangle is square, which is the +// case the top-left rule exists for. The buffer is not square, so that a mix-up +// of width and height shows. +void check_split_rect(int x0, int y0, int x1, int y1) { + FrameBuffer first(64, 48); + FrameBuffer second(64, 48); + const Point tl = kumo::pixel_point(x0, y0); + const Point tr = kumo::pixel_point(x1, y0); + const Point br = kumo::pixel_point(x1, y1); + const Point bl = kumo::pixel_point(x0, y1); + kumo::fill_triangle(first, tl, tr, br, kOn); + kumo::fill_triangle(second, tl, bl, br, kOn); + + int wrong = 0; + for (int y = 0; y < 48; ++y) { + for (int x = 0; x < 64; ++x) { + const int drawn = (first.at(x, y) != 0) + (second.at(x, y) != 0); + const bool inside = x >= x0 && x < x1 && y >= y0 && y < y1; + if (drawn != (inside ? 1 : 0)) ++wrong; + } + } + CHECK_EQ(wrong, 0); +} + +// Draws a line into an 8 by 5 buffer and compares it with a picture: '#' for a +// pixel that must be set and '.' for one that must not. +void expect_line(int x0, int y0, int x1, int y1, + const std::array<const char*, 5>& picture) { + FrameBuffer frame(8, 5); + kumo::draw_line(frame, x0, y0, x1, y1, kOn); + int wrong = 0; + for (int y = 0; y < 5; ++y) { + for (int x = 0; x < 8; ++x) { + if ((frame.at(x, y) != 0) != (picture[y][x] == '#')) ++wrong; + } + } + CHECK_EQ(wrong, 0); +} + +// The sign of a value: -1, 0 or 1. +int sign(double value) { return (value > 0) - (value < 0); } + +// The edge function again, written independently and in floating point. All +// the numbers involved are multiples of 1/512, so the arithmetic is exact. +double cross(double ax, double ay, double bx, double by, double px, double py) { + return (bx - ax) * (py - ay) - (by - ay) * (px - ax); +} + +} // namespace + +TEST(make_color_packs_alpha_red_green_blue) { + CHECK_EQ(kumo::make_color(1, 2, 3, 4), 0x04010203u); + CHECK_EQ(kumo::make_color(255, 0, 0), 0xFFFF0000u); +} + +TEST(clear_sets_every_pixel) { + FrameBuffer frame(7, 5); + kumo::clear(frame, 0x12345678); + CHECK_EQ(std::count(frame.pixels.begin(), frame.pixels.end(), 0x12345678u), + static_cast<std::ptrdiff_t>(35)); +} + +TEST(fill_rect_covers_exactly_the_rectangle) { + FrameBuffer frame(20, 10); + kumo::fill_rect(frame, 3, 2, 5, 4, kOn); + CHECK_EQ(count_set(frame), 20); + CHECK(frame.at(3, 2) != 0); + CHECK(frame.at(7, 5) != 0); + CHECK(frame.at(8, 5) == 0); + CHECK(frame.at(7, 6) == 0); + CHECK(frame.at(2, 2) == 0); +} + +TEST(fill_rect_is_clipped_to_the_buffer) { + FrameBuffer frame(20, 10); + kumo::fill_rect(frame, -10, -10, 25, 25, kOn); + CHECK_EQ(count_set(frame), 15 * 10); // x in [0, 15), y in [0, 10) + kumo::fill_rect(frame, 100, 100, 5, 5, 0); // far outside: nothing happens + CHECK_EQ(count_set(frame), 15 * 10); +} + +TEST(draw_line_includes_both_end_points) { + FrameBuffer frame(16, 16); + kumo::draw_line(frame, 2, 3, 2, 3, kOn); // a single point + CHECK_EQ(count_set(frame), 1); + CHECK(frame.at(2, 3) != 0); + + FrameBuffer horizontal(16, 16); + kumo::draw_line(horizontal, 1, 1, 8, 1, kOn); + CHECK_EQ(count_set(horizontal), 8); + + FrameBuffer vertical(16, 16); + kumo::draw_line(vertical, 5, 9, 5, 2, kOn); // upwards + CHECK_EQ(count_set(vertical), 8); +} + +TEST(draw_line_diagonal_hits_every_corner_pixel) { + FrameBuffer frame(16, 16); + kumo::draw_line(frame, 0, 0, 9, 9, kOn); + CHECK_EQ(count_set(frame), 10); + for (int i = 0; i <= 9; ++i) CHECK(frame.at(i, i) != 0); +} + +TEST(draw_line_draws_one_pixel_per_step_of_the_longer_side) { + FrameBuffer steep(16, 16); + kumo::draw_line(steep, 0, 0, 3, 9, kOn); + CHECK_EQ(count_set(steep), 10); + CHECK(steep.at(0, 0) != 0); + CHECK(steep.at(3, 9) != 0); + + FrameBuffer shallow(16, 16); + kumo::draw_line(shallow, 15, 2, 0, 5, kOn); // right to left + CHECK_EQ(count_set(shallow), 16); + CHECK(shallow.at(15, 2) != 0); + CHECK(shallow.at(0, 5) != 0); +} + +// When two pixels are equally close to the ideal line, which one a step picks +// is arbitrary, but it must be the same on every platform, so it is pinned +// here. Note that the same line drawn from the other end can differ. +TEST(draw_line_picks_the_same_pixel_when_two_are_equally_good) { + expect_line(0, 0, 7, 3, + {"##......", "..##....", "....##..", "......##", "........"}); + expect_line(0, 0, 4, 2, + {"#.......", ".##.....", "...##...", "........", "........"}); + expect_line(4, 2, 0, 0, + {"##......", "..##....", "....#...", "........", "........"}); + expect_line(0, 0, 2, 4, + {"#.......", ".#......", ".#......", "..#.....", "..#....."}); +} + +TEST(draw_line_is_clipped_to_the_buffer) { + FrameBuffer frame(64, 48); + kumo::draw_line(frame, -500, -500, 500, 500, kOn); + CHECK_EQ(count_set(frame), 48); // the diagonal, up to the shorter side + for (int i = 0; i < 48; ++i) CHECK(frame.at(i, i) != 0); +} + +TEST(two_triangles_sharing_an_edge_draw_each_pixel_once) { + check_split_rect(8, 8, 48, 40); + check_split_rect(5, 11, 42, 34); // not square: the diagonal misses centers + check_split_rect(0, 0, 48, 48); // square, touches the buffer's top left + check_split_rect(0, 0, 64, 48); // the whole buffer + check_split_rect(10, 10, 11, 11); // a single pixel +} + +TEST(triangle_does_not_depend_on_the_order_of_its_corners) { + const std::array<Point, 3> corners = {Point{52, 40}, Point{812, 113}, + Point{760, 895}}; // 1/16 pixel units + FrameBuffer expected(64, 64); + kumo::fill_triangle(expected, corners[0], corners[1], corners[2], kOn); + CHECK(count_set(expected) > 100); + + std::array<int, 3> order = {0, 1, 2}; + int permutations = 0; + do { + FrameBuffer frame(64, 64); + kumo::fill_triangle(frame, corners[order[0]], corners[order[1]], + corners[order[2]], kOn); + CHECK(frame.pixels == expected.pixels); + ++permutations; + } while (std::next_permutation(order.begin(), order.end())); + CHECK_EQ(permutations, 6); +} + +TEST(degenerate_triangles_draw_nothing) { + FrameBuffer frame(32, 32); + kumo::fill_triangle(frame, kumo::pixel_point(2, 2), kumo::pixel_point(10, 10), + kumo::pixel_point(20, 20), + kOn); // three points on a line + kumo::fill_triangle(frame, kumo::pixel_point(5, 5), kumo::pixel_point(5, 5), + kumo::pixel_point(9, 12), kOn); // two equal corners + CHECK_EQ(count_set(frame), 0); +} + +TEST(triangle_is_clipped_to_the_buffer) { + FrameBuffer outside(100, 100); + kumo::fill_triangle(outside, kumo::pixel_point(200, 200), + kumo::pixel_point(300, 200), kumo::pixel_point(250, 300), + kOn); + CHECK_EQ(count_set(outside), 0); + + // A triangle whose hypotenuse is x + y = 10 (in pixels). The pixels inside + // the buffer are those with x + y + 1 < 10; the ones with x + y + 1 == 10 + // are on the hypotenuse, which is neither a top nor a left edge. + FrameBuffer corner(100, 100); + kumo::fill_triangle(corner, kumo::pixel_point(-50, -50), + kumo::pixel_point(60, -50), kumo::pixel_point(-50, 60), + kOn); + CHECK_EQ(count_set(corner), 45); + + FrameBuffer huge(100, 100); + kumo::fill_triangle(huge, kumo::pixel_point(-30000, -30000), + kumo::pixel_point(30000, -30000), + kumo::pixel_point(0, 30000), kOn); + CHECK_EQ(count_set(huge), 100 * 100); +} + +// Compares fill_triangle with a floating point version of the same question +// on random triangles with corners in sixteenths of a pixel: is the center of +// the pixel inside? Centers that lie exactly on an edge are skipped, because +// the answer there is a matter of the top-left rule, which the tests above +// cover. Everywhere else the two must agree. +TEST(triangle_agrees_with_an_independent_floating_point_version) { + Random random = {12345}; + int compared = 0; + int disagreements = 0; + for (int round = 0; round < 300; ++round) { + Point p[3]; + for (Point& corner : p) { + corner.x = random.between(-20 * 16, 84 * 16); + corner.y = random.between(-20 * 16, 68 * 16); + } + FrameBuffer frame(64, 48); + kumo::fill_triangle(frame, p[0], p[1], p[2], kOn); + + double x[3], y[3]; + for (int i = 0; i < 3; ++i) { + x[i] = p[i].x / 16.0; + y[i] = p[i].y / 16.0; + } + if (cross(x[0], y[0], x[1], y[1], x[2], y[2]) == 0) continue; // degenerate + + for (int py = 0; py < 48; ++py) { + for (int px = 0; px < 64; ++px) { + const double cx = px + 0.5; + const double cy = py + 0.5; + const int s0 = sign(cross(x[0], y[0], x[1], y[1], cx, cy)); + const int s1 = sign(cross(x[1], y[1], x[2], y[2], cx, cy)); + const int s2 = sign(cross(x[2], y[2], x[0], y[0], cx, cy)); + const bool has_positive = s0 > 0 || s1 > 0 || s2 > 0; + const bool has_negative = s0 < 0 || s1 < 0 || s2 < 0; + bool inside; + if (has_positive && has_negative) { + inside = false; // clearly outside + } else if (s0 == 0 || s1 == 0 || s2 == 0) { + continue; // on an edge: decided by the top-left rule + } else { + inside = true; // all three on the same side + } + ++compared; + if ((frame.at(px, py) != 0) != inside) ++disagreements; + } + } + } + CHECK(compared > 100000); + CHECK_EQ(disagreements, 0); +}
