aienux
Aienux, the AienOS kernel: boot, scheduling, IPC and capability boundaries. Pre-alpha; host, emulator and physical evidence are labelled separately.
Brought by drakestapleton
aienux / native / kernel / ui / tests / sky_sharpness_test.c
| 1 | /* Production-path native-pixel regression. Include the renderer so the |
| 2 | * independent reference can sample the source map before any screen grid. |
| 3 | * The catalogue override isolates real catalogue stars, not a substitute |
| 4 | * point primitive. The normal frame uses the complete production catalogue. */ |
| 5 | #include <fcntl.h> |
| 6 | #include <stdio.h> |
| 7 | #include <stdlib.h> |
| 8 | #include <string.h> |
| 9 | #include "../sky_real.h" |
| 10 | static const ck_sky_star *catalogue; |
| 11 | static unsigned catalogue_count; |
| 12 | #define ck_sky_stars catalogue |
| 13 | #define ck_sky_nstars catalogue_count |
| 14 | #include "../sky_real.c" |
| 15 | #undef ck_sky_stars |
| 16 | #undef ck_sky_nstars |
| 17 | /* The pinned rejected oracle has the retired scale argument. There is no |
| 18 | * production compatibility API; only this historical comparison adapts it. */ |
| 19 | #ifdef CK_SKY_BASELINE |
| 20 | #define TEST_DRAW_STARS(cv, scale, ...) ck_sky_draw_stars(cv, scale, __VA_ARGS__) |
| 21 | #else |
| 22 | #define TEST_DRAW_STARS(cv, scale, ...) ck_sky_draw_stars(cv, __VA_ARGS__) |
| 23 | #endif |
| 24 | |
| 25 | #define W 3840 |
| 26 | #define H 2160 |
| 27 | static int failures; |
| 28 | static void check(int ok, const char *name, unsigned value) |
| 29 | { |
| 30 | printf("%s %s: %u\n", ok ? "PASS" : "FAIL", name, value); |
| 31 | failures += !ok; |
| 32 | } |
| 33 | static void crop(const char *dir, const char *name, const uint32_t *px, int x, int y) |
| 34 | { |
| 35 | if (!dir) return; |
| 36 | char path[1024]; |
| 37 | snprintf(path, sizeof path, "%s/%s.ppm", dir, name); |
| 38 | int f_fd = open(path, O_WRONLY | O_CREAT | O_TRUNC | O_CLOEXEC, 0644); |
| 39 | FILE *f = f_fd < 0 ? NULL : fdopen(f_fd, "wb"); |
| 40 | if (!f) exit(2); |
| 41 | fprintf(f, "P6\n65 65\n255\n"); |
| 42 | for (int j = y - 32; j <= y + 32; j++) |
| 43 | for (int i = x - 32; i <= x + 32; i++) { |
| 44 | uint32_t c = px[(size_t)j * W + i]; |
| 45 | unsigned char rgb[3] = {c >> 16, c >> 8, c}; |
| 46 | fwrite(rgb, 1, 3, f); |
| 47 | } |
| 48 | fclose(f); |
| 49 | } |
| 50 | static unsigned footprint(const uint32_t *px, unsigned *height, unsigned *pixels) |
| 51 | { |
| 52 | int x0 = W, x1 = -1, y0 = H, y1 = -1; |
| 53 | *pixels = 0; |
| 54 | for (int y = 0; y < H; y++) |
| 55 | for (int x = 0; x < W; x++) |
| 56 | if (px[(size_t)y * W + x] & 0xffffff) { |
| 57 | if (x < x0) x0 = x; |
| 58 | if (x > x1) x1 = x; |
| 59 | if (y < y0) y0 = y; |
| 60 | if (y > y1) y1 = y; |
| 61 | (*pixels)++; |
| 62 | } |
| 63 | *height = y1 < y0 ? 0 : (unsigned)(y1 - y0 + 1); |
| 64 | return x1 < x0 ? 0 : (unsigned)(x1 - x0 + 1); |
| 65 | } |
| 66 | /* Independent display encoding of one direct source-map sample. No |
| 67 | * production screen renderer, screen-grid cache or interpolator is used. */ |
| 68 | static uint32_t reference(const ck_sky_view *v, int x, int y) |
| 69 | { |
| 70 | static const unsigned char dither[16] = {0,8,2,10,12,4,14,6,3,11,1,9,15,7,13,5}; |
| 71 | int32_t rgb[3]; |
| 72 | milkyway_at(v, 2*x+1, 2*y+1, W, W/2, 864, rgb); |
| 73 | uint32_t c = 0; |
| 74 | for (int k = 0; k < 3; k++) { |
| 75 | unsigned l = (unsigned)rgb[k]; |
| 76 | unsigned v16 = ck_sky_srgb16[l >= 32767 ? 4095 : l >> 3]; |
| 77 | c = c << 8 | ((v16 + dither[(y & 3)*4+(x & 3)]) >> 4); |
| 78 | } |
| 79 | return c; |
| 80 | } |
| 81 | int main(int argc, char **argv) |
| 82 | { |
| 83 | const char *dir = argc > 1 ? argv[1] : NULL; |
| 84 | uint32_t *pixels = calloc((size_t)W*H, sizeof *pixels); |
| 85 | uint32_t *expected = calloc((size_t)W*H, sizeof *expected); |
| 86 | size_t bytes = ck_night_mem_bytes(W,H,512); |
| 87 | void *mem = malloc(bytes); |
| 88 | if (!pixels || !expected || !mem) return 2; |
| 89 | ck_canvas cv = {pixels,W,H}, ref = {expected,W,H}; |
| 90 | ck_rect all = {0,0,W,H}; |
| 91 | ck_night n; |
| 92 | catalogue = ck_sky_stars; |
| 93 | catalogue_count = ck_sky_nstars; |
| 94 | #ifdef CK_SKY_BASELINE |
| 95 | void *scratch = malloc((size_t)W*H*4); |
| 96 | if (!scratch || ck_night_init(&n,W,H,512,W/2,864,mem,bytes,scratch)) return 2; |
| 97 | #else |
| 98 | if (ck_night_init(&n,W,H,512,W/2,864,mem,bytes)) return 2; |
| 99 | #endif |
| 100 | /* Diffuse oracle. Every native pixel, including all off-quarter-grid |
| 101 | * positions, of a DIRECT ck_sky_draw_milkyway render must equal the |
| 102 | * independent per-pixel reference. This is the renderer contract and is |
| 103 | * separate from the UI policy below. */ |
| 104 | for (int y = 0; y < H; y++) |
| 105 | for (int x = 0; x < W; x++) expected[(size_t)y*W+x] = reference(&n.view,x,y); |
| 106 | ck_night_star tw[CK_NIGHT_TWINKLES]; |
| 107 | unsigned ntw; |
| 108 | unsigned mismatch = 0; |
| 109 | #ifdef CK_SKY_BASELINE |
| 110 | /* The pinned rejected source composed fog into n.sky and has no direct |
| 111 | * renderer entry point; keep its historical contract. */ |
| 112 | TEST_DRAW_STARS(&ref,512,W/2,864,&n.view,tw,CK_NIGHT_TWINKLES,&ntw); |
| 113 | for (size_t i = 0; i < (size_t)W*H; i++) mismatch += expected[i] != n.sky[i]; |
| 114 | check(!mismatch,"native frame versus direct source samples (mismatched pixels)",mismatch); |
| 115 | crop(dir,"native-map",n.sky,W/2,864); |
| 116 | #else |
| 117 | ck_sky_draw_milkyway(&cv,W/2,864,&n.view); |
| 118 | for (size_t i = 0; i < (size_t)W*H; i++) mismatch += expected[i] != pixels[i]; |
| 119 | check(!mismatch,"direct milkyway render versus direct source samples (mismatched pixels)",mismatch); |
| 120 | crop(dir,"native-map",pixels,W/2,864); |
| 121 | /* UI policy: the sky behind Allen is catalogue stars on black, with no |
| 122 | * diffuse fog, immediately after init and after a view change. */ |
| 123 | memset(expected,0,(size_t)W*H*4); |
| 124 | TEST_DRAW_STARS(&ref,512,W/2,864,&n.view,tw,CK_NIGHT_TWINKLES,&ntw); |
| 125 | mismatch = 0; |
| 126 | for (size_t i = 0; i < (size_t)W*H; i++) mismatch += expected[i] != n.sky[i]; |
| 127 | check(!mismatch,"clear sky after init is catalogue stars on black (mismatched pixels)",mismatch); |
| 128 | memcpy(pixels,n.sky,(size_t)W*H*4); |
| 129 | ck_sky_view original = n.view, other; |
| 130 | ck_sky_view_at(&other,200000000,30000000,80000,0); |
| 131 | ck_night_view(&n,&other); |
| 132 | memset(expected,0,(size_t)W*H*4); |
| 133 | TEST_DRAW_STARS(&ref,512,W/2,864,&n.view,tw,CK_NIGHT_TWINKLES,&ntw); |
| 134 | mismatch = 0; |
| 135 | for (size_t i = 0; i < (size_t)W*H; i++) mismatch += expected[i] != n.sky[i]; |
| 136 | check(!mismatch,"clear sky after view change is catalogue stars on black (mismatched pixels)",mismatch); |
| 137 | check(memcmp(&n.view,&other,sizeof other)==0 && memcmp(pixels,n.sky,(size_t)W*H*4)!=0,"view change applied and moved the stars",0); |
| 138 | ck_night_view(&n,&original); |
| 139 | memset(pixels,0,(size_t)W*H*4); |
| 140 | #endif |
| 141 | /* Pick the brightest real visible catalogue star, retaining its exact |
| 142 | * subpixel projection, magnitude, colour and twinkle schedule. */ |
| 143 | unsigned selected = ck_sky_nstars; |
| 144 | int32_t sx=0,sy=0; |
| 145 | for (unsigned i=0;i<ck_sky_nstars;i++) { |
| 146 | int16_t d[3] = {ck_sky_stars[i].x,ck_sky_stars[i].y,ck_sky_stars[i].z}; |
| 147 | int32_t x,y; |
| 148 | if (!ck_sky_project(&n.view,d,W,W/2,864,&x,&y) || x<1024 || y<1024 || x>(W-64)*16 || y>(H-64)*16) continue; |
| 149 | if (selected==ck_sky_nstars || ck_sky_stars[i].mag<ck_sky_stars[selected].mag) selected=i,sx=x,sy=y; |
| 150 | } |
| 151 | if (selected==ck_sky_nstars) return 2; |
| 152 | printf("catalogue star index=%u mag_code=%u x16=%d y16=%d\n",selected,ck_sky_stars[selected].mag,sx,sy); |
| 153 | catalogue = &ck_sky_stars[selected]; catalogue_count = 1; |
| 154 | static const unsigned scales[] = {128,256,512,1024}; |
| 155 | for (unsigned s=0;s<sizeof scales/sizeof scales[0];s++) { |
| 156 | memset(pixels,0,(size_t)W*H*4); |
| 157 | TEST_DRAW_STARS(&cv,scales[s],W/2,864,&n.view,tw,CK_NIGHT_TWINKLES,&ntw); |
| 158 | unsigned height,count,width=footprint(pixels,&height,&count); |
| 159 | printf("static k=%u width=%u height=%u lit_pixels=%u\n",scales[s],width,height,count); |
| 160 | check(width>=1 && width<=2 && height>=1 && height<=2,"static native footprint",width); |
| 161 | check(count>0,"visible static star",count); |
| 162 | if (scales[s]==512) crop(dir,"static-star",pixels,sx>>4,sy>>4); |
| 163 | /* Actual night overlay, full native frame, both voice settings and |
| 164 | * 16 phase samples. Galaxies disabled to isolate the catalogue |
| 165 | * point source, not to bypass its dynamic renderer. */ |
| 166 | ck_night lights = {0}; |
| 167 | lights.w=W; lights.h=H; lights.k=scales[s]; lights.ntw=ntw; |
| 168 | memcpy(lights.tw,tw,ntw*sizeof *tw); |
| 169 | unsigned maxw=0,maxh=0,mincount=~0u; |
| 170 | for (unsigned voice=0;voice<=256;voice+=256) |
| 171 | for (unsigned phase=0;phase<16;phase++) { |
| 172 | memset(pixels,0,(size_t)W*H*4); |
| 173 | TEST_DRAW_STARS(&cv,scales[s],W/2,864,&n.view,tw,CK_NIGHT_TWINKLES,&ntw); |
| 174 | lights.t=30000+phase*tw[0].period/16; |
| 175 | ck_night_lights(&lights,&cv,&all,voice,voice); |
| 176 | width=footprint(pixels,&height,&count); |
| 177 | if(width>maxw) maxw=width; |
| 178 | if(height>maxh) maxh=height; |
| 179 | if(count<mincount) mincount=count; |
| 180 | if(scales[s]==512 && voice==256 && phase==4) crop(dir,"twinkle-star",pixels,sx>>4,sy>>4); |
| 181 | } |
| 182 | printf("dynamic k=%u max_width=%u max_height=%u min_lit_pixels=%u phases=16 voice=0,256\n",scales[s],maxw,maxh,mincount); |
| 183 | check(maxw<=2 && maxh<=2 && mincount>0,"twinkle and voice native footprint",maxw); |
| 184 | } |
| 185 | free(mem); free(expected); free(pixels); |
| 186 | #ifdef CK_SKY_BASELINE |
| 187 | free(scratch); |
| 188 | #endif |
| 189 | printf("SKY_SHARPNESS: %s\n",failures ? "FAIL" : "PASS"); |
| 190 | return failures ? 1 : 0; |
| 191 | } |