From d45931b42340a68b26a0400a697e3d4a47be7a37 Mon Sep 17 00:00:00 2001 From: Stephan Unverwerth Date: Sat, 8 Jan 2022 03:37:32 +0100 Subject: [PATCH] LibSoftGPU: Take sample from pixel center This adds a half pixel offset to the edge value calculation in order to sample the triangle at pixel centers. This is in line with actual OpenGL rasterization rules and generates correctly interpolated vertex attributes including texture coordinates. --- Userland/Libraries/LibSoftGPU/Device.cpp | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/Userland/Libraries/LibSoftGPU/Device.cpp b/Userland/Libraries/LibSoftGPU/Device.cpp index d00ba76052..92cf4b80f1 100644 --- a/Userland/Libraries/LibSoftGPU/Device.cpp +++ b/Userland/Libraries/LibSoftGPU/Device.cpp @@ -259,6 +259,11 @@ void Device::rasterize_triangle(const Triangle& triangle) int const render_bounds_top = render_bounds.y(); int const render_bounds_bottom = render_bounds.y() + render_bounds.height(); + auto const half_pixel_offset = Vector2 { + expand4(subpixel_factor / 2), + expand4(subpixel_factor / 2), + }; + // Iterate over all blocks within the bounds of the triangle for (int by = by0; by < by1; by += 2) { for (int bx = bx0; bx < bx1; bx += 2) { @@ -270,7 +275,7 @@ void Device::rasterize_triangle(const Triangle& triangle) i32x4 { by, by, by + 1, by + 1 }, }; - auto edge_values = calculate_edge_values4(quad.screen_coordinates * subpixel_factor); + auto edge_values = calculate_edge_values4(quad.screen_coordinates * subpixel_factor + half_pixel_offset); // Generate triangle coverage mask quad.mask = test_point4(edge_values);