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 / flow.c
| 1 | /* flow.c -- Allen's ink; see flow.h. */ |
| 2 | #include "flow.h" |
| 3 | |
| 4 | #define W ((int32_t)CK_FLOW_W) |
| 5 | #define H ((int32_t)CK_FLOW_H) |
| 6 | #define IX(i, j) ((i) + (W + 2) * (j)) |
| 7 | #define Q 65536 /* one, 16.16 */ |
| 8 | #define HALF 32768 |
| 9 | |
| 10 | /* How the water behaves (16.16 unless said): a little thick, so a push |
| 11 | * becomes a slow curl rather than a splash; ink spreads a little more than |
| 12 | * the water does; both fade on their own so the tank always comes to rest. */ |
| 13 | #define VISC_PER_MS 205 /* diffusion of velocity per ms (0.05 per 16 ms step) */ |
| 14 | #define INK_PER_MS 328 /* diffusion of ink per ms (0.08 per 16 ms step) */ |
| 15 | #define INK_FADE_PER_MS 33 /* 0.5 of the ink per second */ |
| 16 | #define VEL_FADE_PER_MS 59 /* 0.9 of the velocity per second */ |
| 17 | #define BLEND_MS 700u /* a mode's pull eases in or out over this long */ |
| 18 | #define SOLVE_STEPS 10 /* Gauss-Seidel sweeps for diffusion and pressure */ |
| 19 | |
| 20 | static int32_t mul(int32_t a, int32_t b) { return (int32_t)(((int64_t)a * b) >> 16); } |
| 21 | |
| 22 | static uint32_t rnd(ck_flow *f) |
| 23 | { |
| 24 | f->rng ^= f->rng << 13; |
| 25 | f->rng ^= f->rng >> 17; |
| 26 | f->rng ^= f->rng << 5; |
| 27 | return f->rng; |
| 28 | } |
| 29 | |
| 30 | /* ---- the grid and the screen ---- */ |
| 31 | |
| 32 | /* A screen position to a grid position (16.16 cells, the border cell being 0). */ |
| 33 | static int32_t cell_x(const ck_flow *f, int32_t px) { return (int32_t)((((int64_t)px << 24) + f->cw / 2) / f->cw) + Q; } |
| 34 | static int32_t cell_y(const ck_flow *f, int32_t py) { return (int32_t)((((int64_t)py << 24) + f->ch / 2) / f->ch) + Q; } |
| 35 | /* px per second to cells per second */ |
| 36 | static int32_t cells_x(const ck_flow *f, int32_t vx) { return (int32_t)(((int64_t)vx << 24) / f->cw); } |
| 37 | static int32_t cells_y(const ck_flow *f, int32_t vy) { return (int32_t)(((int64_t)vy << 24) / f->ch); } |
| 38 | |
| 39 | /* The field sampled between cells (values sit at cell centres). */ |
| 40 | static int32_t sample(const int32_t *x, int32_t gx, int32_t gy) |
| 41 | { |
| 42 | gx = ck_clamp(gx - HALF, 0, (W + 1) * Q - 1); |
| 43 | gy = ck_clamp(gy - HALF, 0, (H + 1) * Q - 1); |
| 44 | int32_t i = gx >> 16, j = gy >> 16, s1 = gx & 0xffff, t1 = gy & 0xffff, s0 = Q - s1, t0 = Q - t1; |
| 45 | int64_t a = (int64_t)x[IX(i, j)] * t0 + (int64_t)x[IX(i, j + 1)] * t1; |
| 46 | int64_t b = (int64_t)x[IX(i + 1, j)] * t0 + (int64_t)x[IX(i + 1, j + 1)] * t1; |
| 47 | return (int32_t)((a * s0 + b * s1) >> 32); |
| 48 | } |
| 49 | |
| 50 | /* ---- Stam's steps ---- */ |
| 51 | |
| 52 | /* The border: walls for the velocity (the part across the wall mirrors), continuation for a scalar. */ |
| 53 | static void set_bnd(int32_t *x, int which) |
| 54 | { |
| 55 | for (int32_t i = 1; i <= W; i++) { |
| 56 | x[IX(i, 0)] = which == 2 ? -x[IX(i, 1)] : x[IX(i, 1)]; |
| 57 | x[IX(i, H + 1)] = which == 2 ? -x[IX(i, H)] : x[IX(i, H)]; |
| 58 | } |
| 59 | for (int32_t j = 1; j <= H; j++) { |
| 60 | x[IX(0, j)] = which == 1 ? -x[IX(1, j)] : x[IX(1, j)]; |
| 61 | x[IX(W + 1, j)] = which == 1 ? -x[IX(W, j)] : x[IX(W, j)]; |
| 62 | } |
| 63 | x[IX(0, 0)] = (x[IX(1, 0)] + x[IX(0, 1)]) / 2; |
| 64 | x[IX(0, H + 1)] = (x[IX(1, H + 1)] + x[IX(0, H)]) / 2; |
| 65 | x[IX(W + 1, 0)] = (x[IX(W, 0)] + x[IX(W + 1, 1)]) / 2; |
| 66 | x[IX(W + 1, H + 1)] = (x[IX(W, H + 1)] + x[IX(W + 1, H)]) / 2; |
| 67 | } |
| 68 | |
| 69 | /* x = (x0 + a * (the four neighbours)) / c, swept SOLVE_STEPS times. */ |
| 70 | static void lin_solve(int32_t *x, const int32_t *x0, int32_t a, int32_t c, int which) |
| 71 | { |
| 72 | int64_t inv = ((int64_t)1 << 46) / c; /* one division per solve instead of one per cell */ |
| 73 | for (int k = 0; k < SOLVE_STEPS; k++) { |
| 74 | for (int32_t j = 1; j <= H; j++) |
| 75 | for (int32_t i = 1; i <= W; i++) { |
| 76 | int64_t sum = (int64_t)x[IX(i - 1, j)] + x[IX(i + 1, j)] + x[IX(i, j - 1)] + x[IX(i, j + 1)]; |
| 77 | int64_t num = (((int64_t)x0[IX(i, j)] << 16) + a * sum) >> 14; |
| 78 | x[IX(i, j)] = (int32_t)((num * inv) >> 32); |
| 79 | } |
| 80 | set_bnd(x, which); |
| 81 | } |
| 82 | } |
| 83 | |
| 84 | static void diffuse(int32_t *x, const int32_t *x0, int32_t a, int which) { lin_solve(x, x0, a, Q + 4 * a, which); } |
| 85 | |
| 86 | /* Each cell takes the value found where its water came from, dt seconds ago. */ |
| 87 | static void advect(int32_t *d, const int32_t *d0, const int32_t *u, const int32_t *v, int32_t dt, int which) |
| 88 | { |
| 89 | for (int32_t j = 1; j <= H; j++) |
| 90 | for (int32_t i = 1; i <= W; i++) { |
| 91 | int32_t x = (i << 16) - mul(dt, u[IX(i, j)]), y = (j << 16) - mul(dt, v[IX(i, j)]); |
| 92 | x = ck_clamp(x, HALF, W * Q + HALF); |
| 93 | y = ck_clamp(y, HALF, H * Q + HALF); |
| 94 | int32_t i0 = x >> 16, j0 = y >> 16, s1 = x & 0xffff, t1 = y & 0xffff, s0 = Q - s1, t0 = Q - t1; |
| 95 | int64_t a = (int64_t)d0[IX(i0, j0)] * t0 + (int64_t)d0[IX(i0, j0 + 1)] * t1; |
| 96 | int64_t b = (int64_t)d0[IX(i0 + 1, j0)] * t0 + (int64_t)d0[IX(i0 + 1, j0 + 1)] * t1; |
| 97 | d[IX(i, j)] = (int32_t)((a * s0 + b * s1) >> 32); |
| 98 | } |
| 99 | set_bnd(d, which); |
| 100 | } |
| 101 | |
| 102 | /* Takes the divergence out of the velocity, so the water neither piles up nor |
| 103 | * vanishes anywhere: p and div are scratch. */ |
| 104 | static void project(int32_t *u, int32_t *v, int32_t *p, int32_t *div) |
| 105 | { |
| 106 | for (int32_t j = 1; j <= H; j++) |
| 107 | for (int32_t i = 1; i <= W; i++) { |
| 108 | div[IX(i, j)] = -(u[IX(i + 1, j)] - u[IX(i - 1, j)] + v[IX(i, j + 1)] - v[IX(i, j - 1)]) / 2; |
| 109 | p[IX(i, j)] = 0; |
| 110 | } |
| 111 | set_bnd(div, 0); |
| 112 | set_bnd(p, 0); |
| 113 | lin_solve(p, div, Q, 4 * Q, 0); |
| 114 | for (int32_t j = 1; j <= H; j++) |
| 115 | for (int32_t i = 1; i <= W; i++) { |
| 116 | u[IX(i, j)] -= (p[IX(i + 1, j)] - p[IX(i - 1, j)]) / 2; |
| 117 | v[IX(i, j)] -= (p[IX(i, j + 1)] - p[IX(i, j - 1)]) / 2; |
| 118 | } |
| 119 | set_bnd(u, 1); |
| 120 | set_bnd(v, 2); |
| 121 | } |
| 122 | |
| 123 | static void copy_field(int32_t *d, const int32_t *s) |
| 124 | { |
| 125 | for (size_t i = 0; i < CK_FLOW_CELLS; i++) |
| 126 | d[i] = s[i]; |
| 127 | } |
| 128 | |
| 129 | /* ---- what Allen does to the water ---- */ |
| 130 | |
| 131 | /* A push over a soft disc: velocity (vx, vy) and ink, both in grid units, strongest at the centre. */ |
| 132 | static void push(ck_flow *f, int32_t gx, int32_t gy, int32_t vx, int32_t vy, int32_t ink, int32_t r) |
| 133 | { |
| 134 | int32_t i0 = ck_max(1, (gx - r) >> 16), i1 = ck_min(W, (gx + r) >> 16); |
| 135 | int32_t j0 = ck_max(1, (gy - r) >> 16), j1 = ck_min(H, (gy + r) >> 16); |
| 136 | for (int32_t j = j0; j <= j1; j++) |
| 137 | for (int32_t i = i0; i <= i1; i++) { |
| 138 | int64_t dx = (i << 16) + HALF - gx, dy = (j << 16) + HALF - gy; |
| 139 | int64_t d2 = (dx * dx + dy * dy) >> 16, r2 = ((int64_t)r * r) >> 16; |
| 140 | if (d2 >= r2) |
| 141 | continue; |
| 142 | uint32_t w = ck_exp_neg((uint32_t)(d2 * 3 * 256 / r2)); /* exp(-3 d^2 / r^2), 16.16 */ |
| 143 | f->u[IX(i, j)] += (int32_t)(((int64_t)vx * w) >> 16); |
| 144 | f->v[IX(i, j)] += (int32_t)(((int64_t)vy * w) >> 16); |
| 145 | f->d[IX(i, j)] = ck_min(Q, f->d[IX(i, j)] + (int32_t)(((int64_t)ink * w) >> 16)); |
| 146 | } |
| 147 | } |
| 148 | |
| 149 | /* The pull of each mode, each weighted by how far it has eased in (0..256), |
| 150 | * all added: a change of mode is a change of weights, never a jump. */ |
| 151 | static void forces(ck_flow *f, uint32_t dt_ms) |
| 152 | { |
| 153 | int32_t cx = cell_x(f, f->cx), cy = cell_y(f, f->cy); |
| 154 | int32_t R = ck_max(Q / 2, (int32_t)(((int64_t)f->R << 24) / f->cw)); /* Allen's radius in cells */ |
| 155 | int32_t bx = cell_x(f, f->bx), by = cell_y(f, f->by); |
| 156 | uint32_t wi = f->weight[CK_FLOW_IDLE], wl = f->weight[CK_FLOW_LISTEN], wt = f->weight[CK_FLOW_THINK]; |
| 157 | uint32_t ws = f->weight[CK_FLOW_SPEAK]; |
| 158 | /* thinking deepens over the first four seconds */ |
| 159 | uint32_t deep = f->think_t ? 256 + ck_smooth(ck_prog(f->t, f->think_t, f->think_t + 4000)) * 3 / 4 : 256; |
| 160 | int32_t breath = ck_sin(f->t * 65536 / 7000); /* a slow swell, +-16384 */ |
| 161 | for (int32_t j = 1; j <= H; j++) |
| 162 | for (int32_t i = 1; i <= W; i++) { |
| 163 | int64_t dx = (i << 16) + HALF - cx, dy = (j << 16) + HALF - cy; |
| 164 | int64_t r = ck_isqrt((uint64_t)((dx * dx + dy * dy) >> 16)) << 8; /* 16.16 cells */ |
| 165 | if (r < Q / 8) |
| 166 | r = Q / 8; |
| 167 | int32_t tx = (int32_t)(-dy * Q / r), ty = (int32_t)(dx * Q / r); /* tangent, unit */ |
| 168 | int32_t rx = (int32_t)(dx * Q / r), ry = (int32_t)(dy * Q / r); /* outward, unit */ |
| 169 | /* a bell over the ring R..3.5R where the water turns */ |
| 170 | int64_t ring = r - 3 * R / 2; |
| 171 | uint32_t bell = r < 7 * R / 2 ? ck_exp_neg((uint32_t)(ring * ring * 512 / ((int64_t)R * R))) : 0; |
| 172 | int64_t ax = 0, ay = 0; /* cells per second per second, 16.16 */ |
| 173 | if (wi && bell) { /* idle: a slow turn with the breath */ |
| 174 | int64_t k = (int64_t)bell * wi >> 8; |
| 175 | int64_t turn = Q / 5 + (breath * (Q / 8) >> 14); |
| 176 | ax += (tx * turn >> 16) * k >> 16; |
| 177 | ay += (ty * turn >> 16) * k >> 16; |
| 178 | ax += (rx * (int64_t)(breath * (Q / 10) >> 14) >> 16) * k >> 16; |
| 179 | ay += (ry * (int64_t)(breath * (Q / 10) >> 14) >> 16) * k >> 16; |
| 180 | } |
| 181 | if (wt && bell) { /* thinking: the vortex, deepening, drawn a little inward */ |
| 182 | int64_t k = (int64_t)bell * wt * deep >> 16; |
| 183 | int64_t turn = 3 * Q / 2; |
| 184 | ax += (tx * turn >> 16) * k >> 16; |
| 185 | ay += (ty * turn >> 16) * k >> 16; |
| 186 | ax -= (rx * (int64_t)(Q / 4) >> 16) * k >> 16; |
| 187 | ay -= (ry * (int64_t)(Q / 4) >> 16) * k >> 16; |
| 188 | } |
| 189 | if (wl) { /* listening: a soft current from the bar up to Allen */ |
| 190 | int64_t lx = cx - bx, ly = cy - by, len = ck_isqrt((uint64_t)((lx * lx + ly * ly) >> 16)) << 8; |
| 191 | if (len > Q) { |
| 192 | int64_t px = (i << 16) + HALF - bx, py = (j << 16) + HALF - by; |
| 193 | int64_t along = (px * lx + py * ly) / len, across = (px * ly - py * lx) / len; |
| 194 | if (along > 0 && along < len) { |
| 195 | uint32_t near = ck_exp_neg((uint32_t)(across * across * 256 / ((int64_t)R * R))); |
| 196 | int64_t k = (int64_t)near * wl >> 8; |
| 197 | ax += (lx * Q / len * (Q / 2) >> 16) * k >> 16; |
| 198 | ay += (ly * Q / len * (Q / 2) >> 16) * k >> 16; |
| 199 | } |
| 200 | } |
| 201 | } |
| 202 | (void)ws; /* speaking pushes in rings as the words come (ck_flow_word) */ |
| 203 | f->u[IX(i, j)] += (int32_t)(ax * (int64_t)dt_ms / 1000); |
| 204 | f->v[IX(i, j)] += (int32_t)(ay * (int64_t)dt_ms / 1000); |
| 205 | } |
| 206 | /* ink from the heart: a trickle when idle, a stream when speaking */ |
| 207 | int32_t ink = (int32_t)(((int64_t)(wi * 6 + wl * 10 + ws * 60) * dt_ms) >> 8); |
| 208 | if (ink) |
| 209 | push(f, cx, cy, 0, 0, ink, R); |
| 210 | /* thinking: the ink wells up round him, where the vortex carries it, not under his own light */ |
| 211 | int32_t think_ink = (int32_t)(((int64_t)wt * 120 * deep / 256 * dt_ms) >> 8); |
| 212 | if (think_ink) |
| 213 | for (int k = 0; k < 8; k++) { |
| 214 | uint32_t a = (uint32_t)(f->t * 65536 / 9000) + (uint32_t)k * 8192; |
| 215 | push(f, cx + (int32_t)((int64_t)ck_cos(a) * 3 * R / 2 >> 14), cy + (int32_t)((int64_t)ck_sin(a) * 3 * R / 2 >> 14), 0, 0, |
| 216 | think_ink, R / 2); |
| 217 | } |
| 218 | /* a few motes of ink drift in from nowhere while he is awake, so the water is never empty */ |
| 219 | if ((wi || wl) && (rnd(f) & 15) == 0) |
| 220 | push(f, Q + (int32_t)(rnd(f) % (uint32_t)(W * Q)), Q + (int32_t)(rnd(f) % (uint32_t)(H * Q)), 0, 0, |
| 221 | Q / 40, R / 2); |
| 222 | } |
| 223 | |
| 224 | static void fade(int32_t *x, int32_t per_ms, uint32_t dt_ms) |
| 225 | { |
| 226 | int32_t k = (int32_t)ck_min(Q, (int32_t)(per_ms * dt_ms)); |
| 227 | for (size_t i = 0; i < CK_FLOW_CELLS; i++) |
| 228 | x[i] -= mul(x[i], k); |
| 229 | } |
| 230 | |
| 231 | static void step(ck_flow *f, uint32_t dt_ms) |
| 232 | { |
| 233 | int32_t dt = (int32_t)((dt_ms << 16) / 1000); |
| 234 | for (int m = 0; m < 5; m++) { /* the modes' weights ease toward where they should be */ |
| 235 | uint32_t want = m == f->mode ? 256 : 0, cur = f->weight[m], stepw = 256 * dt_ms / BLEND_MS; |
| 236 | stepw = stepw ? stepw : 1; |
| 237 | f->weight[m] = cur < want ? ck_min((int32_t)want, (int32_t)(cur + stepw)) |
| 238 | : ck_max((int32_t)want, (int32_t)cur - (int32_t)stepw); |
| 239 | } |
| 240 | forces(f, dt_ms); |
| 241 | /* velocity */ |
| 242 | copy_field(f->u0, f->u); |
| 243 | copy_field(f->v0, f->v); |
| 244 | diffuse(f->u, f->u0, (int32_t)(VISC_PER_MS * dt_ms), 1); |
| 245 | diffuse(f->v, f->v0, (int32_t)(VISC_PER_MS * dt_ms), 2); |
| 246 | project(f->u, f->v, f->u0, f->v0); |
| 247 | copy_field(f->u0, f->u); |
| 248 | copy_field(f->v0, f->v); |
| 249 | advect(f->u, f->u0, f->u0, f->v0, dt, 1); |
| 250 | advect(f->v, f->v0, f->u0, f->v0, dt, 2); |
| 251 | project(f->u, f->v, f->u0, f->v0); |
| 252 | /* ink */ |
| 253 | copy_field(f->d0, f->d); |
| 254 | diffuse(f->d, f->d0, (int32_t)(INK_PER_MS * dt_ms), 0); |
| 255 | copy_field(f->d0, f->d); |
| 256 | advect(f->d, f->d0, f->u, f->v, dt, 0); |
| 257 | uint32_t still = f->weight[CK_FLOW_SLEEP]; /* asleep the ink goes faster */ |
| 258 | fade(f->d, INK_FADE_PER_MS + (int32_t)(INK_FADE_PER_MS * 3 * still >> 8), dt_ms); |
| 259 | fade(f->u, VEL_FADE_PER_MS, dt_ms); |
| 260 | fade(f->v, VEL_FADE_PER_MS, dt_ms); |
| 261 | /* how fast each cell moves, for the colour: 2 cells per second and up is fully warm */ |
| 262 | for (size_t i = 0; i < CK_FLOW_CELLS; i++) { |
| 263 | int64_t s2 = ((int64_t)f->u[i] * f->u[i] + (int64_t)f->v[i] * f->v[i]) >> 16; |
| 264 | uint32_t s = (uint32_t)ck_isqrt((uint64_t)s2) << 8; /* 16.16 cells per second */ |
| 265 | uint32_t warm = s >= 2 * Q ? 256 : s * 256 / (2 * Q); |
| 266 | f->warm[i] = (uint16_t)(f->warm[i] + ((int32_t)warm - (int32_t)f->warm[i]) * (int32_t)dt_ms / 250); |
| 267 | } |
| 268 | f->steps++; |
| 269 | } |
| 270 | |
| 271 | /* ---- the interface ---- */ |
| 272 | |
| 273 | void ck_flow_init(ck_flow *f, int32_t w, int32_t h, int32_t cx, int32_t cy, int32_t R, int32_t bx, int32_t by, |
| 274 | uint32_t t) |
| 275 | { |
| 276 | *f = (ck_flow){0}; |
| 277 | f->w = w, f->h = h; |
| 278 | f->cw = ck_max(256, w * 256 / W); |
| 279 | f->ch = ck_max(256, h * 256 / H); |
| 280 | f->cx = cx, f->cy = cy, f->R = R, f->bx = bx, f->by = by; |
| 281 | f->t = f->stepped = t; |
| 282 | f->mode = CK_FLOW_SLEEP; /* still water until he is awake */ |
| 283 | f->weight[CK_FLOW_SLEEP] = 256; |
| 284 | f->rng = 0x9e3779b9u ^ (uint32_t)t; |
| 285 | } |
| 286 | |
| 287 | void ck_flow_mode(ck_flow *f, int mode, uint32_t t) |
| 288 | { |
| 289 | if (mode < 0 || mode > CK_FLOW_SLEEP || mode == f->mode) |
| 290 | return; |
| 291 | f->mode = mode; |
| 292 | f->think_t = mode == CK_FLOW_THINK ? (t ? t : 1) : 0; |
| 293 | } |
| 294 | |
| 295 | void ck_flow_key(ck_flow *f, uint32_t t) |
| 296 | { |
| 297 | f->key_t = t; |
| 298 | int32_t gx = cell_x(f, f->bx), gy = cell_y(f, f->by); |
| 299 | int32_t lx = cell_x(f, f->cx) - gx, ly = cell_y(f, f->cy) - gy; |
| 300 | int64_t len = ck_isqrt((uint64_t)(((int64_t)lx * lx + (int64_t)ly * ly) >> 16)) << 8; |
| 301 | if (len < Q) |
| 302 | len = Q; |
| 303 | int32_t speed = 5 * Q / 2; /* cells per second, toward Allen */ |
| 304 | push(f, gx, gy, (int32_t)((int64_t)lx * speed / len), (int32_t)((int64_t)ly * speed / len), Q / 2, Q); |
| 305 | } |
| 306 | |
| 307 | void ck_flow_word(ck_flow *f, uint32_t t) |
| 308 | { |
| 309 | f->word_t = t; |
| 310 | int32_t cx = cell_x(f, f->cx), cy = cell_y(f, f->cy); |
| 311 | int32_t R = ck_max(Q / 2, (int32_t)(((int64_t)f->R << 24) / f->cw)); |
| 312 | for (unsigned k = 0; k < 12; k++) { /* a ring of twelve pushes, outward */ |
| 313 | uint32_t a = k * 65536 / 12 + (rnd(f) & 2047); |
| 314 | int32_t ux = ck_cos(a) << 2, uy = ck_sin(a) << 2; /* unit, 16.16 */ |
| 315 | push(f, cx + (int32_t)((int64_t)ux * R / 2 >> 16), cy + (int32_t)((int64_t)uy * R / 2 >> 16), |
| 316 | (int32_t)((int64_t)ux * (3 * Q / 2) >> 16), (int32_t)((int64_t)uy * (3 * Q / 2) >> 16), Q / 6, R / 2); |
| 317 | } |
| 318 | } |
| 319 | |
| 320 | void ck_flow_touch(ck_flow *f, int32_t px, int32_t py, int32_t vx, int32_t vy, int32_t ink, int32_t r) |
| 321 | { |
| 322 | push(f, cell_x(f, px), cell_y(f, py), cells_x(f, vx), cells_y(f, vy), ink, |
| 323 | ck_max(Q / 4, (int32_t)(((int64_t)r << 24) / f->cw))); |
| 324 | } |
| 325 | |
| 326 | void ck_flow_at(ck_flow *f, uint32_t t) |
| 327 | { |
| 328 | f->t = t; |
| 329 | /* Catch up in fixed steps; after a long gap (the frame core was resting) start from now. */ |
| 330 | if (t - f->stepped > 8 * CK_FLOW_STEP_MS) |
| 331 | f->stepped = t - 8 * CK_FLOW_STEP_MS; |
| 332 | while (t - f->stepped >= CK_FLOW_STEP_MS) { |
| 333 | f->stepped += CK_FLOW_STEP_MS; |
| 334 | step(f, CK_FLOW_STEP_MS); |
| 335 | } |
| 336 | } |
| 337 | |
| 338 | void ck_flow_velocity(const ck_flow *f, int32_t px, int32_t py, int32_t *vx, int32_t *vy) |
| 339 | { |
| 340 | int32_t gx = cell_x(f, px), gy = cell_y(f, py); |
| 341 | *vx = (int32_t)(((int64_t)sample(f->u, gx, gy) * f->cw) >> 24); |
| 342 | *vy = (int32_t)(((int64_t)sample(f->v, gx, gy) * f->ch) >> 24); |
| 343 | } |
| 344 | |
| 345 | void ck_flow_mark(ck_flow *f, void (*mark)(ck_rect)) |
| 346 | { |
| 347 | for (int32_t j = 0; j <= H + 1; j++) |
| 348 | for (int32_t i = 0; i <= W + 1; i++) { |
| 349 | int32_t now = f->d[IX(i, j)], was = f->shown[IX(i, j)]; |
| 350 | int32_t warm = f->warm[IX(i, j)], warm_was = f->shown_warm[IX(i, j)]; |
| 351 | if (ck_abs(now - was) < Q / 400 && (now < Q / 200 || ck_abs(warm - warm_was) < 24)) |
| 352 | continue; |
| 353 | f->shown[IX(i, j)] = now; |
| 354 | f->shown_warm[IX(i, j)] = (uint16_t)warm; |
| 355 | /* the cell and one cell round it (the interpolation reaches that far) */ |
| 356 | ck_rect r = {(int32_t)(((int64_t)(i - 2) * f->cw) >> 8), (int32_t)(((int64_t)(j - 2) * f->ch) >> 8), |
| 357 | (int32_t)((3 * (int64_t)f->cw >> 8) + 2), (int32_t)((3 * (int64_t)f->ch >> 8) + 2)}; |
| 358 | mark(r); |
| 359 | } |
| 360 | } |
| 361 | |
| 362 | void ck_flow_draw(const ck_flow *f, ck_canvas *out, const ck_rect *r, uint32_t gain) |
| 363 | { |
| 364 | ck_rect c = *r; |
| 365 | ck_rect all = {0, 0, out->w, out->h}; |
| 366 | if (!ck_clip(&c, &all)) |
| 367 | return; |
| 368 | const uint32_t cool = CK_RGB(64, 86, 196), warm = CK_RGB(255, 228, 196); |
| 369 | /* Render the snapshot published by ck_flow_mark, not sub-threshold |
| 370 | * solver changes that did not invalidate their tiles. A full refresh |
| 371 | * must see exactly the same ink as an incremental redraw. */ |
| 372 | int64_t dgx = ((int64_t)1 << 24) / f->cw, rgx = ((int64_t)1 << 24) % f->cw; |
| 373 | int64_t numerator = ((int64_t)c.x << 24) + f->cw / 2; |
| 374 | int64_t gx0 = numerator / f->cw + Q, rx0 = numerator % f->cw; |
| 375 | for (int32_t y = c.y; y < c.y + c.h; y++) { |
| 376 | int32_t gy = cell_y(f, y); |
| 377 | uint32_t *px = out->px + (size_t)y * (size_t)out->w; |
| 378 | int64_t gx = gx0, rx = rx0; |
| 379 | for (int32_t x = c.x; x < c.x + c.w; x++, gx += dgx, rx += rgx) { |
| 380 | /* Carry the division remainder: sampling is independent of |
| 381 | * where a clipped strip starts, without per-pixel division. */ |
| 382 | if (rx >= f->cw) |
| 383 | rx -= f->cw, gx++; |
| 384 | int32_t ink = sample(f->shown, (int32_t)gx, gy); |
| 385 | if (ink < Q / 512) |
| 386 | continue; |
| 387 | uint32_t a = (uint32_t)(((int64_t)ink * gain) >> 16); |
| 388 | a = a > 230 ? 230 : a; |
| 389 | /* the colour: still water deep blue, moving water lit from within */ |
| 390 | int32_t gxx = ck_clamp((int32_t)gx - HALF, 0, (W + 1) * Q - 1), gyy = ck_clamp(gy - HALF, 0, (H + 1) * Q - 1); |
| 391 | uint32_t w = f->shown_warm[IX(gxx >> 16, gyy >> 16)]; |
| 392 | uint32_t col = ck_mix(cool, warm, w); |
| 393 | px[x] = ck_add(px[x], col, a); |
| 394 | } |
| 395 | } |
| 396 | } |
| 397 | |
| 398 | int64_t ck_flow_ink(const ck_flow *f) |
| 399 | { |
| 400 | int64_t s = 0; |
| 401 | for (size_t i = 0; i < CK_FLOW_CELLS; i++) |
| 402 | s += f->d[i]; |
| 403 | return s; |
| 404 | } |