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 / mind.c
| 1 | /* mind.c -- Allen's presence; see mind.h. */ |
| 2 | #include "mind.h" |
| 3 | |
| 4 | #define S(v) ((int32_t)(((int64_t)(v) * (int64_t)m->k + 128) / 256)) |
| 5 | #define INNER 24u /* the last INNER points sit inside the sphere */ |
| 6 | #define TILT 3641u /* the upright axis leans toward the viewer by 20 degrees, in 65536ths */ |
| 7 | |
| 8 | static uint32_t rnd(ck_mind *m) |
| 9 | { |
| 10 | m->rng ^= m->rng << 13; |
| 11 | m->rng ^= m->rng >> 17; |
| 12 | m->rng ^= m->rng << 5; |
| 13 | return m->rng; |
| 14 | } |
| 15 | |
| 16 | static int64_t dist2(const int16_t *a, const int16_t *b) |
| 17 | { |
| 18 | int64_t dx = a[0] - b[0], dy = a[1] - b[1], dz = a[2] - b[2]; |
| 19 | return dx * dx + dy * dy + dz * dz; |
| 20 | } |
| 21 | |
| 22 | static int has_edge(const ck_mind *m, unsigned a, unsigned b) |
| 23 | { |
| 24 | for (unsigned i = 0; i < m->ne; i++) |
| 25 | if ((m->ea[i] == a && m->eb[i] == b) || (m->ea[i] == b && m->eb[i] == a)) |
| 26 | return 1; |
| 27 | return 0; |
| 28 | } |
| 29 | |
| 30 | void ck_mind_init(ck_mind *m, int32_t cx, int32_t cy, uint32_t k, uint32_t born) |
| 31 | { |
| 32 | *m = (ck_mind){0}; |
| 33 | m->cx = cx, m->cy = cy, m->k = k, m->born = born; |
| 34 | m->R = S(118); |
| 35 | m->rng = 0x9e3779b9u; |
| 36 | m->spin = 349; /* a turn in 48 s */ |
| 37 | unsigned outer = CK_MIND_NODES - INNER; |
| 38 | for (unsigned i = 0; i < CK_MIND_NODES; i++) { |
| 39 | int64_t y, rr, x, z, len; |
| 40 | if (i < outer) { /* evenly spread over the sphere: a Fibonacci lattice */ |
| 41 | y = 16384 - (int64_t)(2 * i + 1) * 16384 / outer; |
| 42 | rr = (int64_t)ck_isqrt((uint64_t)(16384 * 16384 - y * y)); |
| 43 | uint32_t th = (uint32_t)(i * 25033u); /* the golden angle */ |
| 44 | x = rr * ck_cos(th) >> 14, z = rr * ck_sin(th) >> 14; |
| 45 | len = 16384 - 2950 + (int64_t)(ck_hash((int32_t)i, 1, 31) % 5900u); /* +-18%: a cloud, not a ball */ |
| 46 | } else { |
| 47 | uint32_t h1 = ck_hash((int32_t)i, 2, 31), h2 = ck_hash((int32_t)i, 3, 31); |
| 48 | y = 16384 - (int64_t)(h1 % 32768u); |
| 49 | rr = (int64_t)ck_isqrt((uint64_t)(16384 * 16384 - y * y)); |
| 50 | x = rr * ck_cos(h2) >> 14, z = rr * ck_sin(h2) >> 14; |
| 51 | len = 4915 + (int64_t)(ck_hash((int32_t)i, 4, 31) % 5734u); /* 0.30 .. 0.65 */ |
| 52 | } |
| 53 | m->home[i][0] = (int16_t)(x * len >> 14); |
| 54 | m->home[i][1] = (int16_t)(y * len >> 14); |
| 55 | m->home[i][2] = (int16_t)(z * len >> 14); |
| 56 | } |
| 57 | /* each point joined to its nearest two (the inner ones to three) */ |
| 58 | for (unsigned i = 0; i < CK_MIND_NODES; i++) { |
| 59 | unsigned want = i < outer ? 2 : 3; |
| 60 | for (unsigned w = 0; w < want; w++) { |
| 61 | int best = -1; |
| 62 | int64_t bd = 0; |
| 63 | for (unsigned j = 0; j < CK_MIND_NODES; j++) { |
| 64 | if (j == i || has_edge(m, i, j)) |
| 65 | continue; |
| 66 | int64_t d = dist2(m->home[i], m->home[j]); |
| 67 | if (best < 0 || d < bd) |
| 68 | best = (int)j, bd = d; |
| 69 | } |
| 70 | if (best >= 0 && m->ne < CK_MIND_EDGES) |
| 71 | m->ea[m->ne] = (uint8_t)i, m->eb[m->ne] = (uint8_t)best, m->ne++; |
| 72 | } |
| 73 | } |
| 74 | } |
| 75 | |
| 76 | void ck_mind_mode(ck_mind *m, int mode, uint32_t t) |
| 77 | { |
| 78 | (void)t; |
| 79 | m->mode = mode; |
| 80 | } |
| 81 | |
| 82 | static void push(uint32_t *ring, uint32_t t) |
| 83 | { |
| 84 | for (unsigned i = 3; i > 0; i--) |
| 85 | ring[i] = ring[i - 1]; |
| 86 | ring[0] = t ? t : 1; |
| 87 | } |
| 88 | |
| 89 | void ck_mind_word(ck_mind *m, uint32_t t) { push(m->words, t); } |
| 90 | void ck_mind_key(ck_mind *m, uint32_t t) { push(m->keys, t); } |
| 91 | void ck_mind_voice(ck_mind *m, uint32_t level) { m->voice = level > 256 ? 256 : level; } |
| 92 | |
| 93 | static void spark(ck_mind *m, unsigned from) |
| 94 | { |
| 95 | if (m->nspark >= CK_MIND_SPARKS || !m->ne) |
| 96 | return; |
| 97 | /* a thread leaving `from`, or any thread when from is out of range */ |
| 98 | unsigned e = rnd(m) % m->ne, tries = 0; |
| 99 | if (from < CK_MIND_NODES) |
| 100 | while (m->ea[e] != from && m->eb[e] != from && tries++ < m->ne) |
| 101 | e = (e + 1) % m->ne; |
| 102 | unsigned s = m->nspark++; |
| 103 | m->spark[s].e = (uint16_t)e; |
| 104 | m->spark[s].back = (uint8_t)(from < CK_MIND_NODES ? m->eb[e] == from : rnd(m) & 1); |
| 105 | m->spark[s].t0 = m->t; |
| 106 | m->spark[s].dur = (uint16_t)(m->mode == CK_MIND_THINK ? 240 + rnd(m) % 260 : 420 + rnd(m) % 480); |
| 107 | } |
| 108 | |
| 109 | static uint32_t toward(uint32_t cur, uint32_t want, uint32_t dt, uint32_t ms) |
| 110 | { |
| 111 | int64_t d = (int64_t)want - cur, step = d * (int64_t)(dt < ms ? dt : ms) / (int64_t)ms; |
| 112 | if (step == 0 && d && dt) |
| 113 | step = d > 0 ? 1 : -1; |
| 114 | return (uint32_t)((int64_t)cur + step); |
| 115 | } |
| 116 | |
| 117 | static uint32_t pulse(int32_t off, int32_t width) /* (1 - (off/width)^2)^2, 0..256 */ |
| 118 | { |
| 119 | if (off <= -width || off >= width) |
| 120 | return 0; |
| 121 | int64_t q = 256 - (int64_t)off * off * 256 / ((int64_t)width * width); |
| 122 | return (uint32_t)(q * q >> 8); |
| 123 | } |
| 124 | |
| 125 | void ck_mind_at(ck_mind *m, uint32_t t) |
| 126 | { |
| 127 | uint32_t dt = t > m->t ? t - m->t : 0; |
| 128 | if (dt > 250) |
| 129 | dt = 250; /* a long pause is not a jump */ |
| 130 | m->t = t; |
| 131 | m->was = m->box; |
| 132 | if (t < m->born) { |
| 133 | m->alpha = 0; |
| 134 | m->box = (ck_rect){0, 0, 0, 0}; |
| 135 | return; |
| 136 | } |
| 137 | static const uint32_t spins[4] = {349, 520, 1400, 600}, squeezes[4] = {0, 4, 16, 0}, hearts[4] = {50, 70, 120, 80}, |
| 138 | rates[4] = {2, 6, 25, 12}; |
| 139 | int md = m->mode & 3; |
| 140 | m->spin = toward(m->spin, spins[md], dt, 700); |
| 141 | m->squeeze = toward(m->squeeze, squeezes[md], dt, 500); |
| 142 | uint32_t want_heart = hearts[md] + (md == CK_MIND_SPEAK ? m->voice * 120 / 256 : 0); |
| 143 | if (md == CK_MIND_THINK) |
| 144 | want_heart += (uint32_t)(16384 + ck_sin(t * 65536u / 900u)) * 30 >> 15; |
| 145 | m->heart = toward(m->heart, want_heart, dt, md == CK_MIND_SPEAK ? 80 : 400); |
| 146 | m->angle += m->spin * dt; |
| 147 | |
| 148 | /* sparks: born at the mode's rate; each that arrives lights its point, and while thinking may go on */ |
| 149 | uint32_t form = (uint32_t)ck_prog(t, m->born, m->born + CK_MIND_FORM_MS); |
| 150 | if (form >= 192) { |
| 151 | m->spawn += rates[md] * dt; |
| 152 | while (m->spawn >= 1000) { |
| 153 | m->spawn -= 1000; |
| 154 | spark(m, CK_MIND_NODES); |
| 155 | } |
| 156 | } |
| 157 | for (unsigned i = 0; i < m->nspark;) { |
| 158 | uint32_t age = t - m->spark[i].t0; |
| 159 | if (age < m->spark[i].dur) { |
| 160 | i++; |
| 161 | continue; |
| 162 | } |
| 163 | unsigned e = m->spark[i].e, to = m->spark[i].back ? m->ea[e] : m->eb[e]; |
| 164 | m->act[to] = 256; |
| 165 | m->spark[i] = m->spark[--m->nspark]; |
| 166 | if (md == CK_MIND_THINK ? rnd(m) % 100 < 70 : rnd(m) % 100 < 18) |
| 167 | spark(m, to); |
| 168 | } |
| 169 | for (unsigned i = 0; i < CK_MIND_NODES; i++) |
| 170 | m->act[i] = (uint16_t)(m->act[i] * (uint32_t)ck_exp_neg(dt * 256 / 260) >> 16); |
| 171 | |
| 172 | /* the shape: breathing, drawn in while thinking, and while being born gathered in from far out */ |
| 173 | int64_t scale = 65536 + ((int64_t)ck_sin(t * 65536u / 5500u) * 1966 >> 14) - (int64_t)m->squeeze * 65536 / 256; |
| 174 | uint32_t gather = ck_ease_out((int32_t)form); |
| 175 | scale = scale * (256 + (int64_t)(256 - gather) * 560 / 256) >> 8; /* 3.2 times out at the start */ |
| 176 | uint32_t swirl = (256 - gather) * (256 - gather) >> 8; /* an extra spiral turn that unwinds as it gathers */ |
| 177 | m->alpha = ck_smooth((int32_t)form * 3); |
| 178 | uint32_t a = (m->angle >> 8) + swirl * 256; |
| 179 | int64_t ca = ck_cos(a), sa = ck_sin(a), ct = ck_cos(TILT), st = ck_sin(TILT); |
| 180 | int64_t R16 = (int64_t)m->R * 16; |
| 181 | int32_t x0 = m->cx * 16, x1 = x0, y0 = m->cy * 16, y1 = y0; |
| 182 | for (unsigned i = 0; i < CK_MIND_NODES; i++) { |
| 183 | int64_t x = m->home[i][0], y = m->home[i][1], z = m->home[i][2]; |
| 184 | int64_t rx = (x * ca - z * sa) >> 14, rz = (x * sa + z * ca) >> 14; /* turn about the upright axis */ |
| 185 | int64_t ry = (y * ct - rz * st) >> 14, fz = (y * st + rz * ct) >> 14; /* lean toward the viewer */ |
| 186 | int64_t persp = 16384 + fz * 3 / 16; /* nearer is a little larger */ |
| 187 | m->px[i] = (int32_t)(m->cx * 16 + 8 + (rx * persp >> 14) * R16 / 16384 * scale / 65536); |
| 188 | m->py[i] = (int32_t)(m->cy * 16 + 8 + (ry * persp >> 14) * R16 / 16384 * scale / 65536); |
| 189 | uint32_t nr = (uint32_t)((fz + 16384) * 256 / 32768); |
| 190 | m->near[i] = (uint16_t)(nr > 256 ? 256 : nr); |
| 191 | /* light from the pulses: words outward from the heart, keys upward from below */ |
| 192 | int64_t rad = (int64_t)ck_isqrt((uint64_t)(x * x + y * y + z * z)) * 256 / 16384; |
| 193 | uint32_t extra = 0; |
| 194 | for (unsigned w = 0; w < 4; w++) { |
| 195 | if (m->words[w] && t - m->words[w] < 900) { |
| 196 | uint32_t age = t - m->words[w]; |
| 197 | extra += pulse((int32_t)rad - (int32_t)(age * 300 / 600), 70) * (900 - age) / 900 * 150 >> 8; |
| 198 | } |
| 199 | if (m->keys[w] && t - m->keys[w] < 600) { |
| 200 | uint32_t age = t - m->keys[w]; |
| 201 | int32_t ky = 300 - (int32_t)(age * 600 / 500), ny = (int32_t)(y * 256 / 16384); |
| 202 | extra += pulse(ny - ky, 60) * (600 - age) / 600 * 90 >> 8; |
| 203 | } |
| 204 | } |
| 205 | uint32_t l = 34 + m->near[i] * m->near[i] / 256 * 180 / 256 + extra + m->act[i] * 120 / 256; |
| 206 | m->lum[i] = (uint16_t)(l > 256 ? 256 : l); |
| 207 | x0 = ck_min(x0, m->px[i]), x1 = ck_max(x1, m->px[i]); |
| 208 | y0 = ck_min(y0, m->py[i]), y1 = ck_max(y1, m->py[i]); |
| 209 | } |
| 210 | m->hx = m->cx * 16 + 8, m->hy = m->cy * 16 + 8; |
| 211 | /* Native-pixel heart: scale changes the network, not its point lights. */ |
| 212 | m->hr = 24; |
| 213 | int32_t pad = 48; /* native points and their coverage edge */ |
| 214 | x0 = ck_min(x0 - pad, m->hx - m->hr - 16), x1 = ck_max(x1 + pad, m->hx + m->hr + 16); |
| 215 | y0 = ck_min(y0 - pad, m->hy - m->hr - 16), y1 = ck_max(y1 + pad, m->hy + m->hr + 16); |
| 216 | m->box = (ck_rect){(x0 >> 4) - 1, (y0 >> 4) - 1, ((x1 - x0) >> 4) + 3, ((y1 - y0) >> 4) + 3}; |
| 217 | } |
| 218 | |
| 219 | ck_rect ck_mind_dirty(const ck_mind *m) { return ck_union(m->was, m->box); } |
| 220 | |
| 221 | /* A filled geometric point, constant light inside and one-pixel coverage |
| 222 | * at its boundary. No radial falloff, bloom, or extra light outside the edge. */ |
| 223 | static void point(ck_canvas *out, const ck_rect *clip, int32_t cx, int32_t cy, |
| 224 | int32_t radius, uint32_t color, uint32_t alpha) |
| 225 | { |
| 226 | if (radius <= 0 || !alpha) |
| 227 | return; |
| 228 | ck_rect box = {(cx - radius - 8) >> 4, (cy - radius - 8) >> 4, 0, 0}; |
| 229 | box.w = ((cx + radius + 8) >> 4) - box.x + 1; |
| 230 | box.h = ((cy + radius + 8) >> 4) - box.y + 1; |
| 231 | ck_rect canvas = {0, 0, out->w, out->h}; |
| 232 | if (!ck_clip(&box, clip) || !ck_clip(&box, &canvas)) |
| 233 | return; |
| 234 | int64_t inner = ck_max(radius - 8, 0), outer = (int64_t)radius + 8; |
| 235 | int64_t inner2 = inner * inner, outer2 = outer * outer; |
| 236 | for (int32_t y = box.y; y < box.y + box.h; y++) { |
| 237 | int64_t dy = (int64_t)y * 16 + 8 - cy, dy2 = dy * dy; |
| 238 | uint32_t *row = out->px + (size_t)y * (size_t)out->w; |
| 239 | for (int32_t x = box.x; x < box.x + box.w; x++) { |
| 240 | int64_t dx = (int64_t)x * 16 + 8 - cx; |
| 241 | int64_t d2 = dx * dx + dy2; |
| 242 | if (d2 >= outer2) |
| 243 | continue; |
| 244 | uint32_t coverage = radius >= 8 && d2 <= inner2 ? 256u : |
| 245 | ck_cover((int32_t)ck_isqrt((uint64_t)d2) - radius); |
| 246 | if (coverage) |
| 247 | row[x] = ck_mix(row[x], color, coverage * alpha >> 8); |
| 248 | } |
| 249 | } |
| 250 | } |
| 251 | |
| 252 | /* Same native thread projection as ck_thread, but coverage mixing prevents |
| 253 | * crossings and background stars accumulating into interior white lights. |
| 254 | * Local to the mind: sky light remains additive. */ |
| 255 | static void thread(ck_canvas *cv, const ck_rect *clip, int32_t x0, int32_t y0, |
| 256 | int32_t x1, int32_t y1, int32_t width, uint32_t a0, uint32_t a1) |
| 257 | { |
| 258 | if (width <= 0 || (!a0 && !a1)) |
| 259 | return; |
| 260 | int steep = ck_abs(y1 - y0) > ck_abs(x1 - x0); |
| 261 | if (steep) { |
| 262 | int32_t t = x0; x0 = y0; y0 = t; |
| 263 | t = x1; x1 = y1; y1 = t; |
| 264 | } |
| 265 | if (x0 > x1) { |
| 266 | int32_t t = x0; x0 = x1; x1 = t; |
| 267 | t = y0; y0 = y1; y1 = t; |
| 268 | uint32_t u = a0; a0 = a1; a1 = u; |
| 269 | } |
| 270 | int32_t len = ck_max(x1 - x0, 1); |
| 271 | int64_t grad = (int64_t)(y1 - y0) * 65536 / len; |
| 272 | uint32_t slant = (uint32_t)(ck_isqrt((uint64_t)(65536ll * 65536ll + grad * grad)) >> 8); |
| 273 | int32_t radius = (int32_t)((int64_t)width * slant >> 9); |
| 274 | int32_t first = ck_max((x0 + 7) >> 4, steep ? clip->y : clip->x); |
| 275 | int32_t last = ck_min((x1 - 8) >> 4, (steep ? clip->y + clip->h : clip->x + clip->w) - 1); |
| 276 | for (int32_t X = first; X <= last; X++) { |
| 277 | int32_t along = X * 16 + 8 - x0; |
| 278 | int32_t yc = y0 + (int32_t)(along * grad >> 16); |
| 279 | uint32_t a = (uint32_t)((int32_t)a0 + ((int32_t)a1 - (int32_t)a0) * along / len); |
| 280 | for (int32_t Y = (yc - radius - 8) >> 4; Y <= (yc + radius + 8) >> 4; Y++) { |
| 281 | int32_t x = steep ? Y : X, y = steep ? X : Y; |
| 282 | if (x < clip->x || y < clip->y || x >= clip->x + clip->w || y >= clip->y + clip->h || |
| 283 | x < 0 || y < 0 || x >= cv->w || y >= cv->h) |
| 284 | continue; |
| 285 | uint32_t coverage = ck_cover(ck_abs(Y * 16 + 8 - yc) - radius); |
| 286 | size_t p = (size_t)y * (size_t)cv->w + (size_t)x; |
| 287 | cv->px[p] = ck_mix(cv->px[p], CK_RGB(120, 140, 255), a * coverage >> 8); |
| 288 | } |
| 289 | } |
| 290 | } |
| 291 | |
| 292 | void ck_mind_draw(const ck_mind *m, ck_canvas *out, const ck_rect *r) |
| 293 | { |
| 294 | if (!m->alpha || !ck_meets(m->box, r)) |
| 295 | return; |
| 296 | uint32_t A = m->alpha, form = (uint32_t)ck_prog(m->t, m->born, m->born + CK_MIND_FORM_MS); |
| 297 | /* the threads, fading in once the points have mostly gathered */ |
| 298 | uint32_t ta = ck_smooth(((int32_t)form - 128) * 2) * A >> 8; |
| 299 | if (ta) |
| 300 | for (unsigned i = 0; i < m->ne; i++) { |
| 301 | unsigned a = m->ea[i], b = m->eb[i]; |
| 302 | uint32_t a0 = (m->lum[a] * (16u + m->near[a] * 60u / 256u) >> 8) * ta >> 8, |
| 303 | a1 = (m->lum[b] * (16u + m->near[b] * 60u / 256u) >> 8) * ta >> 8; |
| 304 | thread(out, r, m->px[a], m->py[a], m->px[b], m->py[b], (int32_t)m->k / 16, a0, a1); |
| 305 | } |
| 306 | /* Cool native-pixel points, including activation; never warm/white discs. */ |
| 307 | for (unsigned i = 0; i < CK_MIND_NODES; i++) { |
| 308 | int32_t rad = 16; |
| 309 | uint32_t c = ck_mix(CK_RGB(32, 72, 144), CK_RGB(48, 112, 192), m->act[i]); |
| 310 | point(out, r, m->px[i], m->py[i], rad, c, m->lum[i] * A >> 8); |
| 311 | } |
| 312 | /* the sparks, each with a short fading tail */ |
| 313 | for (unsigned i = 0; i < m->nspark; i++) { |
| 314 | unsigned e = m->spark[i].e, a = m->ea[e], b = m->eb[e]; |
| 315 | if (m->spark[i].back) |
| 316 | a = m->eb[e], b = m->ea[e]; |
| 317 | int32_t f = (int32_t)((m->t - m->spark[i].t0) * 256u / m->spark[i].dur); |
| 318 | for (int k = 0; k < 3; k++) { |
| 319 | int32_t g = ck_clamp(f - k * 22, 0, 256); |
| 320 | int32_t x = m->px[a] + (int32_t)((int64_t)(m->px[b] - m->px[a]) * g >> 8); |
| 321 | int32_t y = m->py[a] + (int32_t)((int64_t)(m->py[b] - m->py[a]) * g >> 8); |
| 322 | point(out, r, x, y, 16 - k * 4, CK_RGB(48, 112, 192), (200 >> k) * A >> 8); |
| 323 | } |
| 324 | } |
| 325 | /* Voice and cadence modulate a cool point, not a bright central lamp. */ |
| 326 | point(out, r, m->hx, m->hy, m->hr, |
| 327 | ck_mix(CK_RGB(32, 72, 144), CK_RGB(48, 112, 192), m->heart), |
| 328 | ck_min(90 + (int32_t)m->heart, 256) * A >> 8); |
| 329 | } |