Interplane page as plain text, for reading tools and agents. English is the reference text: translate it with your own tool. Text in backticks and in ``` blocks (commands, fingerprints, file names) and link targets in <...> are exact: copy them, never translate them. Page: /drakestapleton/aienux/blob/main/native/kernel/ui/night.c # aienux Aienux, the AienOS kernel: boot, scheduling, IPC and capability boundaries. Pre-alpha; host, emulator and physical evidence are labelled separately. Brought by drakestapleton Sign in to star 0 stars 0 watching About Files History Releases Proposed changes Issues For developers branch `main` Branches Tags History `aienux / native / kernel / ui / night.c` 740 lines ยท 34685 bytes Raw Blame History 1 /* night.c -- the night sky and the merging black holes; see night.h. 2 * 3 * The sky is the real one (ui/sky_real.c, from ui/sky_data.c): Gaia DR3 4 * integrated Milky Way light inverse-projected at every native pixel, 5 * then the catalogue's sharp native-pixel stars on top. The finite 6 * celestial source map limits actual diffuse detail; there is no 7 * intermediate quarter-resolution screen grid or runtime blur. 8 * 9 * Bent light: a point mass moves the image of a star seen at angle theta 10 * from it to where the star really is, beta = theta - E^2 / theta (E the 11 * Einstein radius). Each pixel shows the sky at its own beta (with both 12 * holes' pulls added), so stars behind a hole stretch into arcs round it and 13 * a second, inner image appears on the far side. The pull is eased to zero 14 * between 2.2 E and 4.4 E so the bent region has no edge. It is worked out 15 * exactly near the holes and on an 8-pixel grid (and interpolated) further 16 * out, where it is smooth. The ripple adds a displacement along the radius 17 * that stretches one way and squeezes the other (cos 2 theta), the pattern 18 * a gravitational wave makes. */ 19 #include "night.h" 20 #include "sky_real.h" 21 22 #define S(v) ((int32_t)(((int64_t)(v) * (int64_t)n->k + 128) / 256)) 23 #define Q30 (1ull << 30) 24 25 /* ---- the galaxies: the real ones in the view (sky_real.c) ---- */ 26 27 static uint8_t band_t[256]; /* the sweeping band: ((1 + cos) / 2)^6, 0..255 */ 28 static uint32_t pow2_t[257]; /* 2^(k/256), Q16: for the dusk's distances without a square root per pixel */ 29 30 static uint64_t pow2_q16(uint32_t x) /* 2^(x / 65536), Q16; x below 40 << 16 */ 31 { 32 uint32_t n = x >> 16, f = x & 65535u, k = f >> 8, g = f & 255u; 33 uint64_t m = pow2_t[k] + ((uint64_t)(pow2_t[k + 1] - pow2_t[k]) * g >> 8); 34 return m << n; 35 } 36 37 size_t ck_night_mem_bytes(uint32_t w, uint32_t h, uint32_t k) 38 { 39 (void)k; 40 size_t n = (size_t)w * h * 4 + (size_t)(w / 4 + 1) * (h / 4 + 1) + 64; 41 for (unsigned i = 0; i < CK_NIGHT_GALAXIES; i++) { 42 ck_rect b = ck_sky_galaxy_bound(w, i); 43 n += (size_t)b.w * (size_t)b.h * 2; 44 } 45 return n; 46 } 47 48 /* exp(-(v / width)^2), Q16 */ 49 static uint32_t gauss(int64_t v, int64_t width) /* exp(-(v/width)^2), Q16 */ 50 { 51 int64_t q = v * 256 / (width ? width : 1); 52 if (q > 4095 || q < -4095) 53 return 0; 54 return ck_exp_neg((uint32_t)(q * q >> 8)); 55 } 56 57 58 /* ---- coming out of the dark ---- */ 59 60 static void appear_map(ck_night *n) 61 { 62 int32_t ah = n->h / 4 + 1; 63 int64_t far = 0; 64 int32_t cs[4][2] = {{0, 0}, {n->w, 0}, {0, n->h}, {n->w, n->h}}; 65 for (int i = 0; i < 4; i++) { 66 int64_t dx = cs[i][0] - n->cx, dy = cs[i][1] - n->cy, d = (int64_t)ck_isqrt((uint64_t)(dx * dx + dy * dy)); 67 far = d > far ? d : far; 68 } 69 for (int32_t y = 0; y < ah; y++) 70 for (int32_t x = 0; x < n->aw; x++) { 71 int64_t px = (int64_t)x * 4, py = (int64_t)y * 4, dx = px - n->cx, dy = py - n->cy; 72 uint32_t a = ck_fbm((int32_t)(px * 640 / n->h), (int32_t)(py * 640 / n->h), 909, 4) >> 8; 73 uint32_t r = (uint32_t)((int64_t)ck_isqrt((uint64_t)(dx * dx + dy * dy)) * 255 / (far ? far : 1)); 74 n->appear[(size_t)y * (size_t)n->aw + (size_t)x] = (uint8_t)((a * 6 + r * 4) / 10); 75 } 76 } 77 78 uint32_t ck_night_faded(const ck_night *n, int32_t x, int32_t y, uint32_t l) 79 { 80 if (n->t >= CK_NIGHT_FADE_END_MS) 81 return 256; 82 x = ck_clamp(x, 0, n->w - 1), y = ck_clamp(y, 0, n->h - 1); 83 int32_t from = 300 + n->appear[(size_t)(y >> 2) * (size_t)n->aw + (size_t)(x >> 2)] * 12 + (int32_t)(255 - l) * 8; 84 int32_t dt = (int32_t)n->t - from; 85 return dt <= 0 ? 0 : dt >= 1365 ? 256 : ck_smooth(dt * 48 >> 8); 86 } 87 88 static uint32_t fade_px(const ck_night *n, uint32_t c, int32_t x, int32_t y) 89 { 90 uint32_t r = c >> 16 & 255, g = c >> 8 & 255, b = c & 255, l = r > g ? r : g; 91 l = l > b ? l : b; 92 uint32_t f = ck_night_faded(n, x, y, l); 93 return f >= 256 ? c : ck_scale(c, f); 94 } 95 96 int ck_night_init(ck_night *n, uint32_t w, uint32_t h, uint32_t k, int32_t cx, int32_t cy, void *mem, size_t len) 97 { 98 if (!mem || w < 64 || h < 64 || w > CK_NIGHT_W_MAX || len < ck_night_mem_bytes(w, h, k)) 99 return -1; 100 (void)ck_sin(0); /* the tables, before anything else could ask for them */ 101 for (unsigned i = 0; i < 256; i++) { 102 uint64_t c = (uint64_t)(16384 + ck_cos(i * 256u)) >> 1; /* Q14 */ 103 uint64_t c2 = c * c >> 14, c6 = (c2 * c2 >> 14) * c2 >> 14; 104 band_t[i] = (uint8_t)(c6 * 255 >> 14); 105 } 106 uint64_t e2 = 1ull << 30; /* 2^(k/256): one factor of 2^(1/256) (Q30) per step */ 107 for (unsigned i = 0; i < 256; i++) { 108 pow2_t[i] = (uint32_t)((e2 + (1u << 13)) >> 14); 109 e2 = e2 * 1076652777ull >> 30; 110 } 111 pow2_t[256] = 131072; 112 *n = (ck_night){0}; 113 n->w = (int32_t)w, n->h = (int32_t)h, n->k = k, n->cx = cx, n->cy = cy; 114 uint8_t *p = mem; 115 n->sky = (uint32_t *)p; 116 p += (size_t)w * h * 4; 117 n->appear = p; 118 n->aw = (int32_t)(w / 4 + 1); 119 p += (size_t)(w / 4 + 1) * (h / 4 + 1); 120 n->view = ck_sky_the_view; /* default view: GW150914 region */ 121 ck_canvas cv = {n->sky, n->w, n->h}; 122 /* Point stars on black: no diffuse fog behind Allen. */ 123 for (size_t i = 0; i < (size_t)w * h; i++) 124 n->sky[i] = 0; 125 ck_sky_draw_stars(&cv, cx, cy, &n->view, n->tw, CK_NIGHT_TWINKLES, &n->ntw); 126 for (unsigned i = 0; i < CK_NIGHT_GALAXIES; i++) { 127 ck_night_galaxy *g = &n->gal[i]; 128 g->box = ck_sky_galaxy_box(w, h, cx, cy, &n->view, i); 129 g->light = p; 130 g->phase = p + (size_t)g->box.w * (size_t)g->box.h; 131 p += (size_t)g->box.w * (size_t)g->box.h * 2; 132 ck_sky_galaxy_maps(g, i, w, cx, cy, &n->view); 133 } 134 appear_map(n); 135 ck_night_at(n, 0); 136 return 0; 137 } 138 139 /* ---- the black holes and the ripple at a time ---- */ 140 141 static uint64_t root8(uint64_t l) /* l^(1/8), Q30 */ 142 { 143 for (int i = 0; i < 3; i++) 144 l = ck_isqrt(l << 30); 145 return l; 146 } 147 148 #define ORBIT_MS 5200u /* one turn of the wide orbit */ 149 150 static uint32_t orbit_angle(uint32_t t, int32_t *sep16, int32_t a0) 151 { 152 uint32_t spiral_from = (CK_NIGHT_SPIRAL_MS - CK_NIGHT_HOLES_MS) * 65536u / ORBIT_MS; 153 if (t < CK_NIGHT_SPIRAL_MS) { 154 *sep16 = a0; 155 return (t - CK_NIGHT_HOLES_MS) * 65536u / ORBIT_MS; 156 } 157 uint64_t left = ((uint64_t)(CK_NIGHT_MERGE_MS - t) << 30) / (CK_NIGHT_MERGE_MS - CK_NIGHT_SPIRAL_MS); 158 uint64_t r = root8(left), r2 = r * r >> 30, r5 = (r2 * r2 >> 30) * r >> 30; 159 *sep16 = (int32_t)((uint64_t)a0 * r2 >> 30); 160 /* the spiral's angle carries on at the wide orbit's speed: K (1 - L^(5/8)), K = 8/5 x its turns */ 161 uint64_t turns = (uint64_t)65536 * 8 * (CK_NIGHT_MERGE_MS - CK_NIGHT_SPIRAL_MS) / (5ull * ORBIT_MS); 162 return spiral_from + (uint32_t)(turns * (Q30 - r5) >> 30); 163 } 164 165 void ck_night_at(ck_night *n, uint32_t t) 166 { 167 n->t = t; 168 n->holes = 0; 169 n->falling = 0; 170 n->lens = (ck_rect){0, 0, 0, 0}; 171 n->ripple_w = 0; 172 int32_t e_full = S(60) * 16, rs_full = S(20) * 16, a0 = S(270) * 16; 173 int32_t cx = n->cx * 16 + 8, cy = n->cy * 16 + 8; 174 int32_t sep; 175 uint32_t last = orbit_angle(CK_NIGHT_MERGE_MS - 1, &sep, a0); 176 if (t >= CK_NIGHT_HOLES_MS && t < CK_NIGHT_MERGE_MS) { 177 uint32_t ang = orbit_angle(t, &sep, a0); 178 int32_t e = (int32_t)((int64_t)e_full * ck_smooth(ck_prog(t, CK_NIGHT_HOLES_MS, CK_NIGHT_HOLES_MS + 2600)) >> 8); 179 int32_t rs = (int32_t)((int64_t)rs_full * 180 ck_smooth(ck_prog(t, CK_NIGHT_HOLES_MS + 1400, CK_NIGHT_HOLES_MS + 3400)) >> 8); 181 int32_t hx = (int32_t)((int64_t)(sep / 2) * ck_cos(ang) >> 14); 182 int32_t hy = (int32_t)((int64_t)(sep / 2) * ck_sin(ang) >> 15); /* the orbit leans away: half as tall */ 183 n->hole[0] = (__typeof__(n->hole[0])){cx + hx, cy + hy, e, rs}; 184 n->hole[1] = (__typeof__(n->hole[1])){cx - hx, cy - hy, e, rs}; 185 n->holes = 2; 186 } else if (t >= CK_NIGHT_MERGE_MS && t < CK_NIGHT_GONE_MS) { 187 uint32_t tm = t - CK_NIGHT_MERGE_MS; 188 int32_t e = e_full * 362 / 256, rs0 = rs_full * 323 / 256, rs1 = rs_full * 2; 189 int32_t rs = rs0 + (int32_t)((int64_t)(rs1 - rs0) * ck_smooth(ck_prog(tm, 0, 180)) >> 8); 190 /* it rings: a quick wobble that dies away */ 191 int64_t wob = (int64_t)ck_sin(tm * 65536u / 95u) * ck_exp_neg(tm * 256u / 260u) >> 16; 192 rs += (int32_t)(rs * wob * 7 / 100 >> 14); 193 uint32_t keep = 256 - ck_smooth(ck_prog(t, CK_NIGHT_MERGE_MS + 900, CK_NIGHT_GONE_MS)); 194 n->hole[0] = (__typeof__(n->hole[0])){cx, cy, (int32_t)((int64_t)e * keep >> 8), 195 (int32_t)((int64_t)rs * (keep * keep >> 8) >> 8)}; 196 n->holes = 1; 197 } 198 for (unsigned i = 0; i < n->holes; i++) { 199 int32_t reach = (n->hole[i].e * 44 / 10 >> 4) + 2; 200 if (n->hole[i].e <= 0 && n->hole[i].rs <= 0) 201 continue; 202 reach = ck_max(reach, (n->hole[i].rs * 16 / 10 >> 4) + 2); 203 ck_rect b = {(n->hole[i].x >> 4) - reach, (n->hole[i].y >> 4) - reach, 2 * reach + 1, 2 * reach + 1}; 204 n->lens = ck_union(n->lens, b); 205 } 206 ck_rect all = {0, 0, n->w, n->h}; 207 if (n->lens.w && !ck_clip(&n->lens, &all)) 208 n->lens = (ck_rect){0, 0, 0, 0}; 209 if (t >= CK_NIGHT_MERGE_MS && t < CK_NIGHT_MERGE_MS + CK_NIGHT_RIPPLE_MS) { 210 uint32_t tr = t - CK_NIGHT_MERGE_MS; 211 int32_t w = ck_clamp(S(60), 4, 200), reach = w * 22 / 10; 212 int64_t far = 0; 213 int32_t cs[4][2] = {{0, 0}, {n->w, 0}, {0, n->h}, {n->w, n->h}}; 214 for (int i = 0; i < 4; i++) { 215 int64_t dx = cs[i][0] - n->cx, dy = cs[i][1] - n->cy, d = (int64_t)ck_isqrt((uint64_t)(dx * dx + dy * dy)); 216 far = d > far ? d : far; 217 } 218 far += reach; 219 n->ripple_r = (int32_t)(far * tr / CK_NIGHT_RIPPLE_MS); 220 n->ripple_w = w; 221 uint32_t env = ck_smooth(ck_prog(tr, 0, 220)) * 222 (256 - ck_smooth(ck_prog(tr, CK_NIGHT_RIPPLE_MS * 6 / 10, CK_NIGHT_RIPPLE_MS))) >> 8; 223 int64_t amp = (int64_t)S(6) * 16 * (256 - 150 * n->ripple_r / far) >> 8; 224 amp = amp * env >> 8; 225 int32_t lambda = ck_max(S(110), 8); 226 for (int32_t i = 0; i <= 2 * reach && i < 1024; i++) { 227 int32_t o = i - reach; 228 int64_t s = ck_sin((uint32_t)((int64_t)o * 65536 / lambda)); 229 n->ripple_disp[i] = (int32_t)((amp * s >> 14) * gauss(o, w) >> 16); 230 n->ripple_light[i] = (uint8_t)((uint64_t)gauss(o, w * 6 / 10) * env * 46 >> 24); 231 } 232 /* it stretches along the line the pair last lay on, and squeezes across it */ 233 n->ripple_c2 = ck_cos(2 * last); 234 n->ripple_s2 = ck_sin(2 * last); 235 } 236 } 237 238 /* ---- drawing the base ---- */ 239 240 static uint32_t lerp16(uint32_t a, uint32_t b, uint32_t f) /* f: 0..16 */ 241 { 242 return ck_mix(a, b, f * 16); 243 } 244 245 /* the sky at (sx, sy), 16ths of a pixel (pixel i's centre at 16 i + 8) */ 246 static uint32_t sample(const ck_night *n, int32_t sx, int32_t sy) 247 { 248 sx = ck_clamp(sx - 8, 0, (n->w - 1) * 16); 249 sy = ck_clamp(sy - 8, 0, (n->h - 1) * 16); 250 int32_t ix = sx >> 4, iy = sy >> 4, ix1 = ck_min(ix + 1, n->w - 1), iy1 = ck_min(iy + 1, n->h - 1); 251 const uint32_t *r0 = n->sky + (size_t)iy * (size_t)n->w, *r1 = n->sky + (size_t)iy1 * (size_t)n->w; 252 uint32_t fx = (uint32_t)sx & 15u, fy = (uint32_t)sy & 15u; 253 return lerp16(lerp16(r0[ix], r0[ix1], fx), lerp16(r1[ix], r1[ix1], fx), fy); 254 } 255 256 /* the holes' pull on the light at (X, Y) (16ths of a pixel): added to (sx, sy), where the sky is read from */ 257 static void lens_at(const ck_night *n, int64_t X, int64_t Y, int64_t *sx, int64_t *sy) 258 { 259 for (unsigned i = 0; i < n->holes; i++) { 260 int64_t e = n->hole[i].e, dx = X - n->hole[i].x, dy = Y - n->hole[i].y, d2 = dx * dx + dy * dy; 261 int64_t out = e * e * 1936 / 100, in = e * e * 484 / 100; /* (4.4 E)^2, (2.2 E)^2 */ 262 if (e <= 0 || d2 >= out) 263 continue; 264 if (d2 < 64) 265 d2 = 64; 266 int64_t f = e * e * 65536 / d2; 267 if (d2 > in) { 268 int64_t w = (out - d2) * 65536 / (out - in); 269 f = f * (w * w >> 16) >> 16; 270 } 271 *sx -= dx * f >> 16; 272 *sy -= dy * f >> 16; 273 } 274 } 275 276 /* where (X, Y) (16ths of a pixel) looks in the sky: offset (ax, ay), and the ripple's own faint light */ 277 static void warp(const ck_night *n, int64_t X, int64_t Y, int32_t *ax, int32_t *ay, uint32_t *light) 278 { 279 int64_t sx = 0, sy = 0; 280 *light = 0; 281 lens_at(n, X, Y, &sx, &sy); 282 if (n->ripple_w) { 283 int64_t dx = X - (n->cx * 16 + 8), dy = Y - (n->cy * 16 + 8), d2 = dx * dx + dy * dy; 284 int64_t r16 = (int64_t)ck_isqrt((uint64_t)d2), reach = n->ripple_w * 22 / 10; 285 int64_t i = (r16 >> 4) - n->ripple_r + reach; 286 if (r16 > 0 && i >= 0 && i <= 2 * reach && i < 1024) { 287 int64_t q = ((dx * dx - dy * dy) * n->ripple_c2 + 2 * dx * dy * n->ripple_s2) / d2; /* cos 2(theta - psi), Q14 */ 288 int64_t m = (int64_t)n->ripple_disp[i] * (5734 + (q * 10650 >> 14)) >> 14; 289 sx -= m * dx / r16; 290 sy -= m * dy / r16; 291 *light = n->ripple_light[i]; 292 } 293 } 294 *ax = (int32_t)sx; 295 *ay = (int32_t)sy; 296 } 297 298 static uint32_t finish(const ck_night *n, uint32_t c, int32_t x, int32_t y, uint32_t light) 299 { 300 if (n->t < CK_NIGHT_FADE_END_MS) 301 c = fade_px(n, c, x, y); 302 return light ? ck_add(c, CK_RGB(86, 108, 176), light) : c; 303 } 304 305 /* a pixel close to a hole: the pull worked out exactly, then the shadow and its thin ring of light */ 306 static uint32_t near_px(const ck_night *n, int32_t x, int32_t y) 307 { 308 int64_t X = (int64_t)x * 16 + 8, Y = (int64_t)y * 16 + 8; 309 int32_t ax, ay; 310 uint32_t light; 311 warp(n, X, Y, &ax, &ay, &light); 312 uint32_t c = finish(n, sample(n, (int32_t)X + ax, (int32_t)Y + ay), x, y, light); 313 for (unsigned i = 0; i < n->holes; i++) { 314 int64_t rs = n->hole[i].rs, dx = X - n->hole[i].x, dy = Y - n->hole[i].y, d2 = dx * dx + dy * dy; 315 if (rs <= 0 || d2 >= rs * rs * 9 / 4) 316 continue; 317 int32_t r = (int32_t)ck_isqrt((uint64_t)d2); 318 c = ck_mix(c, 0, ck_cover(r - (int32_t)rs)); 319 int32_t ring = ck_abs(r - (int32_t)(rs * 104 / 100)); 320 uint32_t a = ring < 22 ? (uint32_t)(22 - ring) * 6 : 0; 321 if (r > rs) 322 a += (uint32_t)((rs * 3 / 2 - r) * 46 / (rs / 2 + 1)) * (r < rs * 3 / 2); 323 c = ck_add(c, CK_RGB(255, 228, 196), a); 324 } 325 return c; 326 } 327 328 static int near_hole(const ck_night *n, int32_t x, int32_t y) 329 { 330 int64_t X = (int64_t)x * 16 + 8, Y = (int64_t)y * 16 + 8; 331 for (unsigned i = 0; i < n->holes; i++) { 332 int64_t reach = ck_max(n->hole[i].e, n->hole[i].rs * 3 / 2) + 8 * 16; 333 int64_t dx = X - n->hole[i].x, dy = Y - n->hole[i].y; 334 if (dx * dx + dy * dy < reach * reach) 335 return 1; 336 } 337 return 0; 338 } 339 340 /* does the 8 x 8 block at (bx, by) meet the band the ripple bends? */ 341 static int in_ripple(const ck_night *n, int32_t bx, int32_t by) 342 { 343 if (!n->ripple_w) 344 return 0; 345 int64_t reach = n->ripple_w * 22 / 10 + 1, lo = ck_max(0, n->ripple_r - (int32_t)reach), hi = n->ripple_r + reach; 346 int64_t nx = ck_clamp(n->cx, bx, bx + 8) - n->cx, ny = ck_clamp(n->cy, by, by + 8) - n->cy; 347 int64_t fx = ck_max(ck_abs(bx - n->cx), ck_abs(bx + 8 - n->cx)), fy = ck_max(ck_abs(by - n->cy), ck_abs(by + 8 - n->cy)); 348 return nx * nx + ny * ny <= hi * hi && fx * fx + fy * fy >= lo * lo; 349 } 350 351 void ck_night_base(const ck_night *n, ck_canvas *out, const ck_rect *r) 352 { 353 ck_rect d = *r, all = {0, 0, n->w, n->h}; 354 if (!ck_clip(&d, &all)) 355 return; 356 int fading = n->t < CK_NIGHT_FADE_END_MS; 357 for (int32_t y = d.y; y < d.y + d.h; y++) { 358 const uint32_t *src = n->sky + (size_t)y * (size_t)n->w; 359 uint32_t *dst = out->px + (size_t)y * (size_t)out->w; 360 if (fading) 361 for (int32_t x = d.x; x < d.x + d.w; x++) 362 dst[x] = fade_px(n, src[x], x, y); 363 else 364 for (int32_t x = d.x; x < d.x + d.w; x++) 365 dst[x] = src[x]; 366 } 367 if (!n->lens.w && !n->ripple_w) 368 return; 369 /* the bent parts, in 8 x 8 blocks on a grid fixed to the screen, so any split of r draws the same */ 370 for (int32_t by = d.y & ~7; by < d.y + d.h; by += 8) 371 for (int32_t bx = d.x & ~7; bx < d.x + d.w; bx += 8) { 372 ck_rect b = {bx, by, 8, 8}; 373 int lens = n->lens.w && ck_meets(b, &n->lens); 374 if (!lens && !in_ripple(n, bx, by)) 375 continue; 376 if (!ck_clip(&b, &d)) 377 continue; 378 if (lens && near_hole(n, bx + 4, by + 4)) { 379 for (int32_t y = b.y; y < b.y + b.h; y++) 380 for (int32_t x = b.x; x < b.x + b.w; x++) 381 out->px[(size_t)y * (size_t)out->w + (size_t)x] = near_px(n, x, y); 382 continue; 383 } 384 int32_t ax[4], ay[4]; 385 uint32_t li[4]; 386 for (int c = 0; c < 4; c++) 387 warp(n, ((int64_t)bx + (c & 1) * 8) * 16, ((int64_t)by + (c >> 1) * 8) * 16, &ax[c], &ay[c], &li[c]); 388 for (int32_t y = b.y; y < b.y + b.h; y++) { 389 int32_t wy = 2 * (y - by) + 1; 390 for (int32_t x = b.x; x < b.x + b.w; x++) { 391 int32_t wx = 2 * (x - bx) + 1; 392 int32_t w00 = (16 - wx) * (16 - wy), w10 = wx * (16 - wy), w01 = (16 - wx) * wy, w11 = wx * wy; 393 int32_t sx = (ax[0] * w00 + ax[1] * w10 + ax[2] * w01 + ax[3] * w11) >> 8; 394 int32_t sy = (ay[0] * w00 + ay[1] * w10 + ay[2] * w01 + ay[3] * w11) >> 8; 395 uint32_t l = (uint32_t)((int32_t)li[0] * w00 + (int32_t)li[1] * w10 + (int32_t)li[2] * w01 + 396 (int32_t)li[3] * w11) >> 8; 397 uint32_t c = sample(n, x * 16 + 8 + sx, y * 16 + 8 + sy); 398 out->px[(size_t)y * (size_t)out->w + (size_t)x] = finish(n, c, x, y, l); 399 } 400 } 401 } 402 } 403 404 /* ---- the dusk: everything on the screen falls into one black hole ---- */ 405 406 size_t ck_night_picture_bytes(uint32_t w, uint32_t h) 407 { 408 size_t n = 64 + (size_t)w * h * 4; 409 for (unsigned i = 1; i < CK_NIGHT_LEVELS; i++) 410 n += (size_t)(w >> i) * (size_t)(h >> i) * 4 * 2; 411 return n; 412 } 413 414 /* half the width and height of `from` (sw x sh) into to: the mean of each 2 x 2 */ 415 static void halve(const uint32_t *from, uint32_t sw, uint32_t sh, uint32_t *to) 416 { 417 uint32_t dw = sw / 2, dh = sh / 2; 418 for (uint32_t y = 0; y < dh; y++) { 419 const uint32_t *r0 = from + (size_t)(2 * y) * sw, *r1 = r0 + sw; 420 uint32_t *d = to + (size_t)y * dw; 421 for (uint32_t x = 0; x < dw; x++) { 422 uint32_t a = r0[2 * x], b = r0[2 * x + 1], c = r1[2 * x], e = r1[2 * x + 1]; 423 uint32_t color = 0; 424 for (int s = 0; s <= 16; s += 8) { 425 uint32_t light = ck_sky_lin[ck_ch(a, s)] + ck_sky_lin[ck_ch(b, s)] + 426 ck_sky_lin[ck_ch(c, s)] + ck_sky_lin[ck_ch(e, s)]; 427 color |= ck_light_encode(light >> 2) << s; 428 } 429 d[x] = color; 430 } 431 } 432 } 433 434 void ck_night_picture_take(const ck_night *n, const uint32_t *from, void *mem, ck_night_picture *kept) 435 { 436 uint32_t w = (uint32_t)n->w, h = (uint32_t)n->h, *p = mem; 437 size_t count = (size_t)w * h; 438 for (size_t i = 0; i < count; i++) 439 p[i] = from[i]; 440 kept->level[0] = p; 441 kept->sky[0] = n->sky; 442 p += count; 443 for (unsigned l = 1; l < CK_NIGHT_LEVELS; l++) { 444 uint32_t sw = w >> (l - 1), sh = h >> (l - 1); 445 halve(kept->level[l - 1], sw, sh, p); 446 kept->level[l] = p; 447 p += (size_t)(sw / 2) * (sh / 2); 448 halve(kept->sky[l - 1], sw, sh, p); 449 kept->sky[l] = p; 450 p += (size_t)(sw / 2) * (sh / 2); 451 } 452 } 453 454 void ck_night_fall(ck_night *n, const ck_night_dusk *d) 455 { 456 n->falling = 1; 457 n->dusk = *d; 458 int32_t cx = n->cx * 16 + 8, cy = n->cy * 16 + 8, rs = d->rs; 459 n->hole[0] = (__typeof__(n->hole[0])){cx, cy, d->e, rs}; 460 n->holes = 1; 461 n->ripple_w = 0; 462 n->lens = (ck_rect){0, 0, n->w, n->h}; 463 /* The disk and the ring, by distance from the hole, in 1024 steps out to 3.3 shadow radii. The disk is 464 * dark inside 1.15 radii, brightest at 1.5 and dies away beyond; its colour runs from warm white at the 465 * inner edge through gold to the blue of the sky's glow. The photon ring is a thin bright line at 1.06 466 * radii, a pixel and a quarter each way (wider on a big screen). */ 467 n->disk_far = rs * 33 / 10 + 16; 468 int32_t in = rs * 115 / 100, peak = rs * 150 / 100, ring = rs * 106 / 100, hw = 20 + rs / 64; 469 for (unsigned i = 0; i < 1024; i++) { 470 int32_t r = (int32_t)((int64_t)n->disk_far * (int64_t)i / 1024); 471 uint32_t a = 0, c = CK_RGB(255, 222, 180); 472 if (r >= in && peak > in) { 473 if (r < peak) { 474 uint32_t u = (uint32_t)((int64_t)(r - in) * 256 / (peak - in)); 475 a = ck_smooth((int32_t)u) * 220 >> 8; 476 c = ck_mix(CK_RGB(255, 222, 180), CK_RGB(255, 212, 150), u); 477 } else { 478 uint32_t v = (uint32_t)((int64_t)(r - peak) * 256 / (rs / 2 + 1)); /* half radii past the peak, 256 each */ 479 a = (ck_exp_neg(v) >> 8) * 220 >> 8; 480 c = ck_mix(CK_RGB(255, 212, 150), CK_RGB(150, 170, 255), ck_smooth((int32_t)(v * 256 / 360))); 481 } 482 } 483 int32_t off = ck_abs(r - ring); 484 if (off < hw) { 485 uint32_t ar = (uint32_t)(hw - off) * 255 / (uint32_t)hw; 486 ar = ar * ar >> 8; 487 c = a + ar ? ck_mix(c, CK_RGB(255, 244, 228), ar * 256 / (a + ar)) : c; 488 a += ar; 489 } 490 a = a * d->disk >> 8; 491 n->disk_a[i] = (uint8_t)(a > 255 ? 255 : a); 492 n->disk_c[i] = c; 493 } 494 } 495 496 /* a corner of the 8 x 8 grid during the dusk: where the picture is read from, and the distance to the hole */ 497 struct corner { 498 int32_t ax, ay; /* offset to the picture point shown here, 16ths of a pixel */ 499 int32_t sx, sy; /* that point itself, 16ths (for telling whether a block shows only the dark beyond the edge) */ 500 uint32_t crowd; /* how much brighter the crowded light is drawn, 256 = as it is */ 501 uint32_t h; /* log2 of the crowding of the area, Q16 */ 502 int32_t r; /* distance from the hole, 16ths of a pixel */ 503 int32_t inv; /* 2^28 / r */ 504 uint32_t vig; /* how much of the light the edges of the screen keep here, 0..256 */ 505 }; 506 507 static void corner_at(const ck_night *n, int32_t x, int32_t y, struct corner *c) 508 { 509 const ck_night_dusk *d = &n->dusk; 510 int64_t X = (int64_t)x * 16, Y = (int64_t)y * 16, dx = X - n->hole[0].x, dy = Y - n->hole[0].y; 511 int64_t d2 = dx * dx + dy * dy; 512 if (d2 < 256) 513 d2 = 256; 514 uint32_t l0 = ck_log2_q16((uint64_t)d2), l1 = ck_log2_q16((uint64_t)(d2 + d->fallen)), h = l1 - l0; 515 c->h = h; 516 uint64_t crowd = pow2_q16(h) >> 8; 517 c->crowd = crowd > d->crowd ? d->crowd : (uint32_t)crowd; 518 /* the picture point: the same direction turned by swirl per doubling, at the stretched distance */ 519 int64_t r = (int64_t)(pow2_q16(l0 >> 1) >> 16); /* the distance, 16ths, without a square root */ 520 uint32_t ang = (uint32_t)((uint64_t)d->swirl * (h >> 1) >> 16); 521 ang = (uint32_t)((int64_t)ang * d->calm / (d->calm + r)); 522 int64_t stretch = (int64_t)pow2_q16(h >> 1), cs = ck_cos(ang), sn = ck_sin(ang); 523 int64_t ux = (dx * cs - dy * sn) >> 14, uy = (dx * sn + dy * cs) >> 14; 524 int64_t sx = (ux * stretch >> 16) - dx, sy = (uy * stretch >> 16) - dy; 525 lens_at(n, X, Y, &sx, &sy); 526 /* far beyond the picture is still far beyond it: held where the block arithmetic stays in 32 bits */ 527 sx = sx > (1 << 22) ? (1 << 22) : sx < -(1 << 22) ? -(1 << 22) : sx; 528 sy = sy > (1 << 22) ? (1 << 22) : sy < -(1 << 22) ? -(1 << 22) : sy; 529 c->ax = (int32_t)sx; 530 c->ay = (int32_t)sy; 531 c->sx = (int32_t)X + c->ax; 532 c->sy = (int32_t)Y + c->ay; 533 c->r = (int32_t)r; 534 c->inv = (int32_t)((1 << 28) / (r > 0 ? r : 1)); 535 /* the edges go dark first: from `near` out to `far` the light falls off smoothly by up to `shade` */ 536 c->vig = 256 - (d->shade * ck_smooth(ck_prog((uint32_t)r, (uint32_t)d->near, (uint32_t)d->far)) >> 8); 537 } 538 539 /* the kept picture at (sx, sy) (16ths of a pixel of copy l, w x h): the picture itself inside its edges, the 540 * sky mirrored in them beyond (so the stars flow in from past the screen), black further out than that. */ 541 static uint32_t sample_kept(const ck_night_picture *kept, unsigned l, int32_t w, int32_t h, int32_t sx, int32_t sy) 542 { 543 const uint32_t *px = kept->level[l]; 544 int32_t W = (w - 1) * 16, H = (h - 1) * 16; 545 sx -= 8, sy -= 8; 546 if (sx < 0 || sy < 0 || sx > W || sy > H) { 547 sx = sx < 0 ? -sx : sx > W ? 2 * W - sx : sx; 548 sy = sy < 0 ? -sy : sy > H ? 2 * H - sy : sy; 549 if (sx < 0 || sy < 0 || sx > W || sy > H) 550 return 0; 551 px = kept->sky[l]; 552 } 553 int32_t ix = sx >> 4, iy = sy >> 4, ix1 = ck_min(ix + 1, w - 1), iy1 = ck_min(iy + 1, h - 1); 554 const uint32_t *r0 = px + (size_t)iy * (size_t)w, *r1 = px + (size_t)iy1 * (size_t)w; 555 uint32_t fx = (uint32_t)sx & 15u, fy = (uint32_t)sy & 15u; 556 return lerp16(lerp16(r0[ix], r0[ix1], fx), lerp16(r1[ix], r1[ix1], fx), fy); 557 } 558 559 /* c brightened g/256 times (g above 256), the excess warmer than the light itself */ 560 static uint32_t brighten(uint32_t c, uint32_t g) 561 { 562 if (g <= 256 || !c) 563 return c; 564 uint32_t x = g - 256; 565 uint32_t r = ck_light_encode(ck_sky_lin[ck_ch(c, 16)] * (256 + x) >> 8); 566 uint32_t gg = ck_light_encode(ck_sky_lin[ck_ch(c, 8)] * (256 + x * 7 / 8) >> 8); 567 uint32_t b = ck_light_encode(ck_sky_lin[ck_ch(c, 0)] * (256 + x * 5 / 8) >> 8); 568 return r << 16 | gg << 8 | b; 569 } 570 571 /* One 8 x 8 block of the dusk, clipped to d. Share the smooth warp's four 572 * corners while resolving the kept picture, shadow and light at native pixels. */ 573 static void fall_block(const ck_night *n, const ck_night_picture *kept, ck_canvas *out, const ck_rect *d, int32_t bx, 574 int32_t by, const struct corner *c00, const struct corner *c10, const struct corner *c01, 575 const struct corner *c11) 576 { 577 ck_rect b = {bx, by, 8, 8}; 578 if (!ck_clip(&b, d)) 579 return; 580 const ck_night_dusk *k = &n->dusk; 581 int32_t rs = k->rs, hx = n->hole[0].x, hy = n->hole[0].y; 582 int32_t rmin = ck_min(ck_min(c00->r, c10->r), ck_min(c01->r, c11->r)) - 182; /* the block's diagonal: 8 px * sqrt 2 */ 583 int32_t rmax = ck_max(ck_max(c00->r, c10->r), ck_max(c01->r, c11->r)); 584 int32_t W16 = n->w * 32 + 16, H16 = n->h * 32 + 16, X16 = -n->w * 16 - 16, Y16 = -n->h * 16 - 16; 585 /* all four corners read from the dark beyond the same edge of the mirrored sky: so does everything between */ 586 int beyond = (c00->sx < X16 && c10->sx < X16 && c01->sx < X16 && c11->sx < X16) || 587 (c00->sy < Y16 && c10->sy < Y16 && c01->sy < Y16 && c11->sy < Y16) || 588 (c00->sx > W16 && c10->sx > W16 && c01->sx > W16 && c11->sx > W16) || 589 (c00->sy > H16 && c10->sy > H16 && c01->sy > H16 && c11->sy > H16); 590 int disk = k->disk && rmin < n->disk_far, edge = rmin < rs + 16; 591 /* the light kept here: the closing of the horizon, and the edges of the screen going dark first */ 592 uint32_t keep = k->keep * ((c00->vig + c10->vig + c01->vig + c11->vig) >> 2) >> 8; 593 if ((!keep && !disk) || rmax < rs - 16 || (beyond && !disk)) { /* only the dark: inside the shadow, or past the edge */ 594 for (int32_t y = b.y; y < b.y + b.h; y++) 595 for (int32_t x = b.x; x < b.x + b.w; x++) 596 out->px[(size_t)y * (size_t)out->w + (size_t)x] = 0; 597 return; 598 } 599 /* Use smaller copies only when the warp actually minifies the source. 600 * Dimming is not minification: faint stars and text still need their detail. */ 601 uint32_t hm = (c00->h >> 2) + (c10->h >> 2) + (c01->h >> 2) + (c11->h >> 2), lvl = hm >> 17; 602 lvl = lvl >= CK_NIGHT_LEVELS ? CK_NIGHT_LEVELS - 1 : lvl; 603 int32_t lw = n->w >> lvl, lh = n->h >> lvl; 604 int crowded = c00->crowd > 256 || c10->crowd > 256 || c01->crowd > 256 || c11->crowd > 256; 605 int64_t cb = ck_cos(k->beam), sb = ck_sin(k->beam); 606 uint32_t inv_far = (uint32_t)((1u << 30) / (uint32_t)(n->disk_far > 0 ? n->disk_far : 1)); 607 /* Resolve every native destination pixel, even far from the hole. The 608 * shared corner grid accelerates the warp without upscaling 2 x 2 samples. */ 609 for (int32_t y = b.y; y < b.y + b.h; y++) { 610 int32_t wy = 2 * (y - by) + 1; 611 uint32_t *row = out->px + (size_t)y * (size_t)out->w; 612 for (int32_t x = b.x; x < b.x + b.w; x++) { 613 int32_t wx = 2 * (x - bx) + 1; 614 int32_t w00 = (16 - wx) * (16 - wy), w10 = wx * (16 - wy), w01 = (16 - wx) * wy, w11 = wx * wy; 615 uint32_t c = 0; 616 if (!beyond) { 617 int32_t ax = (c00->ax * w00 + c10->ax * w10 + c01->ax * w01 + c11->ax * w11) >> 8; 618 int32_t ay = (c00->ay * w00 + c10->ay * w10 + c01->ay * w01 + c11->ay * w11) >> 8; 619 c = sample_kept(kept, lvl, lw, lh, (x * 16 + 8 + ax) >> lvl, (y * 16 + 8 + ay) >> lvl); 620 if (c && crowded) { 621 uint32_t g = (uint32_t)((int32_t)c00->crowd * w00 + (int32_t)c10->crowd * w10 + 622 (int32_t)c01->crowd * w01 + (int32_t)c11->crowd * w11) >> 8; 623 c = brighten(c, g); 624 } 625 if (keep < 256) /* the picture goes before the disk does */ 626 c = ck_scale(c, keep); 627 } 628 if (disk || edge) { 629 int32_t r = (c00->r * w00 + c10->r * w10 + c01->r * w01 + c11->r * w11) >> 8; 630 if (disk && r < n->disk_far) { 631 uint32_t i = (uint32_t)r * inv_far >> 20, a = n->disk_a[i < 1024 ? i : 1023]; 632 if (a) { 633 /* brighter on the side turning toward the eye: by the cosine of the angle from the beam */ 634 int64_t inv = ((int64_t)c00->inv * w00 + (int64_t)c10->inv * w10 + (int64_t)c01->inv * w01 + 635 (int64_t)c11->inv * w11) >> 8; 636 int64_t dot = ((int64_t)(x * 16 + 8 - hx) * cb + (int64_t)(y * 16 + 8 - hy) * sb) >> 14; 637 int64_t cosb = dot * inv >> 14; /* Q14 */ 638 a = (uint32_t)((int64_t)a * (16384 + (cosb * 110 >> 8)) >> 14); 639 c = ck_add(c, n->disk_c[i < 1024 ? i : 1023], a > 255 ? 255 : a); 640 } 641 } 642 if (edge) 643 c = ck_mix(c, 0, ck_cover(r - rs)); 644 } 645 row[x] = c; 646 } 647 } 648 } 649 650 #define CHUNK 32 /* blocks of a row worked at once (their corners shared); 2.4 KiB of stack for the two rows */ 651 652 void ck_night_base_from(const ck_night *n, const ck_night_picture *kept, ck_canvas *out, const ck_rect *r) 653 { 654 ck_rect d = *r, all = {0, 0, n->w, n->h}; 655 if (!ck_clip(&d, &all)) 656 return; 657 if (!n->falling) { 658 ck_night_base(n, out, &d); 659 return; 660 } 661 struct corner rows[2][CHUNK + 1]; 662 int32_t bx0 = d.x & ~7, bx1 = d.x + d.w, by0 = d.y & ~7, by1 = d.y + d.h; 663 for (int32_t cx0 = bx0; cx0 < bx1; cx0 += CHUNK * 8) { 664 int32_t ncol = ck_min(CHUNK, (bx1 - cx0 + 7) / 8); 665 struct corner *top = rows[0], *bottom = rows[1]; 666 for (int32_t i = 0; i <= ncol; i++) 667 corner_at(n, cx0 + 8 * i, by0, &top[i]); 668 for (int32_t by = by0; by < by1; by += 8) { 669 for (int32_t i = 0; i <= ncol; i++) 670 corner_at(n, cx0 + 8 * i, by + 8, &bottom[i]); 671 for (int32_t i = 0; i < ncol; i++) 672 fall_block(n, kept, out, &d, cx0 + 8 * i, by, &top[i], &top[i + 1], &bottom[i], &bottom[i + 1]); 673 struct corner *t = top; 674 top = bottom, bottom = t; 675 } 676 } 677 } 678 679 /* ---- the lights on top ---- */ 680 681 ck_rect ck_night_star_box(const ck_night *n, unsigned i) 682 { 683 const ck_night_star *s = &n->tw[i]; 684 return (ck_rect){(s->x - 8) >> 4, (s->y - 8) >> 4, 2, 2}; 685 } 686 687 void ck_night_lights(const ck_night *n, ck_canvas *out, const ck_rect *r, uint32_t shimmer, uint32_t voice) 688 { 689 for (unsigned i = 0; i < n->ntw; i++) { 690 const ck_night_star *s = &n->tw[i]; 691 ck_rect b = ck_night_star_box(n, i); 692 if (!ck_meets(b, r) || (n->lens.w && ck_meets(b, &n->lens))) 693 continue; 694 uint32_t tw = (uint32_t)(16384 + ck_sin((n->t + s->phase) % s->period * 65536u / s->period)) >> 7; 695 uint32_t a = (tw * tw >> 8) * 120 >> 8; 696 a = a * ck_night_faded(n, s->x >> 4, s->y >> 4, 255) >> 8; 697 ck_sky_twinkle(out, r, s, a); 698 } 699 if (!shimmer && !voice) 700 return; 701 for (unsigned i = 0; i < CK_NIGHT_GALAXIES; i++) { 702 const ck_night_galaxy *g = &n->gal[i]; 703 ck_rect b = g->box; 704 if (!g->box.w || !ck_clip(&b, r)) 705 continue; 706 uint32_t sweep = n->t % g->period * 256u / g->period; 707 for (int32_t y = b.y; y < b.y + b.h; y++) { 708 const uint8_t *lp = g->light + (size_t)(y - g->box.y) * (size_t)g->box.w - (size_t)g->box.x; 709 const uint8_t *pp = g->phase + (size_t)(y - g->box.y) * (size_t)g->box.w - (size_t)g->box.x; 710 uint32_t *row = out->px + (size_t)y * (size_t)out->w; 711 for (int32_t x = b.x; x < b.x + b.w; x++) { 712 uint32_t l = lp[x]; 713 if (!l) 714 continue; 715 uint32_t a = (l * band_t[(pp[x] - sweep) & 255u] >> 8) * shimmer >> 8; 716 a += (l * l >> 8) * voice >> 9; 717 if (a) 718 row[x] = ck_add(row[x], g->tint, a); 719 } 720 } 721 } 722 } 723 /* ---- rebuilding the sky for a new view ---- */ 724 725 /* Rebuild point stars on black at native resolution, without diffuse fog. */ 726 void ck_night_view(ck_night *n, const ck_sky_view *v) 727 { 728 n->view = *v; 729 ck_canvas cv = {n->sky, n->w, n->h}; 730 for (size_t i = 0; i < (size_t)n->w * n->h; i++) 731 n->sky[i] = 0; 732 /* redraw stars over the new Milky Way */ 733 ck_sky_draw_stars(&cv, n->cx, n->cy, &n->view, n->tw, CK_NIGHT_TWINKLES, &n->ntw); 734 /* recompute galaxy boxes (some may enter or leave the view) */ 735 for (unsigned i = 0; i < CK_NIGHT_GALAXIES; i++) { 736 ck_night_galaxy *g = &n->gal[i]; 737 g->box = ck_sky_galaxy_box((uint32_t)n->w, (uint32_t)n->h, n->cx, n->cy, &n->view, i); 738 ck_sky_galaxy_maps(g, i, (uint32_t)n->w, n->cx, n->cy, &n->view); 739 } 740 }