limine icon indicating copy to clipboard operation
limine copied to clipboard

Stretched wallpapers lose horizontal detail due to fixed-point rounding

Open pro-utkarshM opened this issue 2 months ago • 1 comments

On trunk 6f618ed039a749538220ee0b3dc0ab98ea6be773, genloop() rounds the horizontal image/framebuffer ratio down to six fractional bits before stepping through a stretched wallpaper.

For a 16-pixel-wide image on a 1920-pixel-wide framebuffer, floor(16 * 64 / 1920) = 0. Every destination column samples source column zero. Less extreme non-integral ratios also accumulate visible horizontal distortion; the vertical coordinate uses full-precision division.

Reproduction

The script below extracts the current genloop() and any fixed-point helpers verbatim from the source tree, compiles them with minimal host structures and an identity blend, and compares every output pixel to the integer coordinate oracle floor(x * image_width / framebuffer_width). It checks both full draws and split rectangles with reversed endpoints.

Save as stretch-check.py and run from the source root:

python3 stretch-check.py .
ASAN_OPTIONS=detect_leaks=0 python3 stretch-check.py . --sanitize
Standalone regression and optional benchmark
#!/usr/bin/env python3
"""Compile the source tree's genloop verbatim against a host pixel oracle."""
from pathlib import Path
import os
import subprocess
import sys
import tempfile

root = Path(sys.argv[1]).resolve()
source = (root / "common/lib/gterm.c").read_text()
start = source.index("// Draw rect at coordinates")
end = source.index("static uint32_t blend_external", start)
helpers = ""
if "typedef size_t fixedp6;" in source:
    helpers = source[source.index("typedef size_t fixedp6;"):start]
prefix = r'''
#include <stdint.h>
#include <stddef.h>
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
enum { IMAGE_TILED, IMAGE_CENTERED, IMAGE_STRETCHED };
struct image {
    size_t img_width, img_height, pitch, bpp, x_size, y_size;
    int64_t x_displacement, y_displacement;
    uint32_t back_colour;
    int type;
    uint8_t *img;
};
struct fb_info { uint64_t framebuffer_width, framebuffer_height; };
static struct image *background;
static uint32_t *bg_canvas;
'''
suffix = r'''
static uint32_t identity(struct fb_info *fb, size_t x, size_t y, uint32_t pixel) {
    (void)fb; (void)x; (void)y; return pixel;
}
static size_t compare(size_t iw, size_t ih, size_t w, size_t h) {
    size_t errors = 0;
    for (size_t y = 0; y < h; y++) {
        for (size_t x = 0; x < w; x++) {
            uint32_t expected = (y * ih / h) * iw + x * iw / w + 1;
            errors += bg_canvas[y * w + x] != expected;
        }
    }
    return errors;
}
static int check(size_t iw, size_t ih, size_t w, size_t h, int bench) {
    uint32_t *pixels = malloc(iw * ih * sizeof(*pixels));
    bg_canvas = calloc(w * h, sizeof(*bg_canvas));
    if (!pixels || !bg_canvas) abort();
    for (size_t i = 0; i < iw * ih; i++) pixels[i] = i + 1;
    struct image img = {.img_width=iw, .img_height=ih, .pitch=iw*4,
        .bpp=32, .type=IMAGE_STRETCHED, .img=(uint8_t *)pixels};
    struct fb_info fb = {.framebuffer_width=w, .framebuffer_height=h};
    background = &img;
    genloop(&fb, 0, w, 0, h, identity);
    size_t full = compare(iw, ih, w, h);
    for (size_t i = 0; i < w * h; i++) bg_canvas[i] = 0;
    size_t xs[] = {0, w/3, w-1, w};
    size_t ys[] = {0, h/2, h};
    for (size_t yi = 0; yi < 2; yi++) {
        for (size_t xi = 0; xi < 3; xi++) {
            // Reversed endpoints exercise the function's normalisation too.
            genloop(&fb, xs[xi+1], xs[xi], ys[yi+1], ys[yi], identity);
        }
    }
    size_t split = compare(iw, ih, w, h);
    printf("%zux%zu -> %zux%zu: full=%zu split=%zu incorrect pixels\n",
           iw, ih, w, h, full, split);
    if (bench) {
        struct timespec begin, end;
        clock_gettime(CLOCK_MONOTONIC, &begin);
        for (int i = 0; i < 100; i++) {
            genloop(&fb, 0, w, 0, h, identity);
            __asm__ volatile ("" : : : "memory");
        }
        clock_gettime(CLOCK_MONOTONIC, &end);
        double elapsed = (end.tv_sec - begin.tv_sec) * 1e3
                       + (end.tv_nsec - begin.tv_nsec) / 1e6;
        printf("render: %.3f ms/frame (100 iterations)\n", elapsed / 100);
    }
    free(pixels);
    free(bg_canvas);
    return full != 0 || split != 0;
}
int main(int argc, char **argv) {
    (void)argv;
    int errors = 0;
    if (argc > 1) {
        errors += check(640, 480, 1920, 1080, 1);
        errors += check(1920, 1080, 1920, 1080, 1);
        errors += check(1920, 1080, 3840, 2160, 1);
    } else {
        errors += check(16, 2, 1920, 7, 0);
        errors += check(1001, 13, 1920, 17, 0);
        errors += check(1920, 17, 1366, 13, 0);
        errors += check(3840, 21, 1920, 13, 0);
        errors += check(37, 19, 37, 19, 0);
        errors += check(1, 1, 1920, 3, 0);
        errors += check(1919, 3, 1, 5, 0);
    }
    return errors != 0;
}
'''
with tempfile.TemporaryDirectory(prefix="limine-stretch-") as temp:
    cfile = Path(temp) / "check.c"
    exe = Path(temp) / "check"
    cfile.write_text(prefix + helpers + source[start:end] + suffix)
    command = [os.environ.get("CC", "cc"), "-std=gnu11", "-O2", "-Wall",
               "-Wextra", "-Werror", str(cfile), "-o", str(exe)]
    if "--sanitize" in sys.argv:
        command += ["-fsanitize=address,undefined", "-fno-omit-frame-pointer"]
    subprocess.run(command, check=True)
    args = [str(exe)] + (["bench"] if "--bench" in sys.argv else [])
    sys.exit(subprocess.run(args).returncode)

On the baseline, three of seven image/framebuffer dimension pairs fail in both full and split rendering:

  • 16x2 to 1920x7: 12600 incorrect pixels.
  • 1001x13 to 1920x17: 31110 incorrect pixels.
  • 1920x17 to 1366x13: 17316 incorrect pixels.

Integer downscaling, identity scaling, a single source pixel, and a one-column destination pass as controls. With quotient/remainder stepping, all seven pairs pass both full and split comparisons; ASan/UBSan also pass (LeakSanitizer disabled because the host tracing environment does not support it).

Fix and validation

Carry the horizontal remainder between pixels and initialise both quotient and remainder from the absolute rectangle start. This preserves exact sampling across the separate margin and centre rectangles without division inside the pixel loop.

The full ./bootstrap, ./configure --enable-werror --enable-all, and make all -j8 build passes for BIOS and all configured UEFI targets.

Performance tradeoff: a Clang -O2 host microbenchmark (CC=clang python3 stretch-check.py . --bench, 100 draws with an identity blend) measured roughly 1.58 to 3.02 ms/frame for 640x480 to 1920x1080, and 6.26 to 12.82 ms/frame for 1920x1080 to 3840x2160. These measure the raw pixel loop on this host, not firmware framebuffer rendering; no physical boot or display timing is claimed.

The prepared fix is pushed; I will open the corresponding PR immediately.

pro-utkarshM avatar Sep 22 '26 03:09 pro-utkarshM

Fixed by #670

Mintsuki avatar Sep 26 '26 21:09 Mintsuki