diff --git a/docs/flesh-map.md b/docs/flesh-map.md index 1320afe..bc8589d 100644 --- a/docs/flesh-map.md +++ b/docs/flesh-map.md @@ -99,16 +99,35 @@ The body-backed skin does not sieve: it holds its place within 0.1 mm and stretc bone surface between skin particles, which is where the drawn skin is; none enters the bone. The corner-pinned tarp's sieve is a limit of loose cloth, not of BDS skin. +P7 Solid grid sized by its members (pass ymir-dormant-map; `probes/flesh/grid-mask.probe.patch` on top of the P2 probe, +`grid-mask.fella.patch` adds `MASKSIZED` and an FNV hash of the pos_x, pos_y and vel_z lanes; runs `grid-mask-h.out`, +`grid-mask-k.out`). grid_sort's solid pass uses a mask from the member count (16 or more buckets, at least twice the count, +capped at the table) and walk_half hashes with the same mask. The hash is equal with and without it on a live Fella plus +self-colliding gut (961 live) and on 4 skip-flag Fellas, because walk_half reads a cell's particles in lane order whatever +the bucket count. The fluid grid is left full size: fluid_density sums in bucket order. Paired interleaved medians, 5 runs of +600 ticks: + +| Scene | Full table | Sized | Particle-free (bones only) | +| --- | --- | --- | --- | +| 4 Fellas pinned by skip flags | 0.988 | 0.486 | 0.096 | +| 1 live Fella out of contact + gut, 3 Fellas' bones (K) | 3.975 | 3.464 | | + +Phase split of the sized 4-Fella run, ms per tick: grid 0.056, particle contacts 0.087, attachments 0.021, distances 0.123, +bends 0.028, bodies 0.014. Those are walks over dormant lanes that active lists remove. + ## Cut order 1. cut-ymir-skin-sieve-test (Ymir, test and doc). Beside it: 2. cut-ymir-particle-limb-contact (Ymir: held list derived at - command time, LIMB attachment flag skips the attached body's limb tree). -3. ymir-particle-pinning, revised: dormant particles out of every loop through command-time active lists; pinned particles - bordering live ones are kinematic per substep; one-way attachment flag; wake writes position and velocity from the target. -4. ymir-attach-batch (spawn) and, after question ymir-flesh-particle-contact, ymir-flesh-no-particle-contact. -5. lodur-tissue-stack (plan data and the deterministic sheet builder; can start any time). -6. lodur-body-sheets-graph (binding: build dormant, wake per part, re-pin, wounds and severs as commands), after lodur-physiology. -7. bds-flesh-render-embedding; then lodur-organ-release-rope and lodur-exposed-tube-chains. + command time, LIMB attachment flag skips the attached body's limb tree) and 3. cut-ymir-flesh-no-particle-contact + (NO_PARTICLE_CONTACT phase bit, solid grid sized by its members; no ABI number). +4. cut-ymir-attach-batch (ABI 17): attach takes n descs and ends in one derivation; the ONE_WAY attachment flag. After + ymir-break-and-remove (ABI 16) and limb-contact. +5. cut-ymir-particle-pinning-dormant (ABI 18): set_pinned; dormant particles out of every substep walk through command-time + active lists and a derived solve inverse mass; pinned particles bordering live ones are kinematic per substep; wake writes + position and velocity from the anchor. After 2, 3 and 4. +6. lodur-tissue-stack (plan data and the deterministic sheet builder; can start any time). +7. lodur-body-sheets-graph (binding: build dormant, wake per part, re-pin, wounds and severs as commands), after lodur-physiology. +8. bds-flesh-render-embedding; then lodur-organ-release-rope and lodur-exposed-tube-chains. Budget reading: at rest flesh must cost nothing per tick (P2 B, H); one hurt part costs about 0.2 ms; a whole live Fella about 1 ms alone and 2.3 ms in a 4-Fella world; an eviscerated gut adds 1.3 to 2 ms. The rope scene already takes about 2 ms, so a diff --git a/docs/probes/flesh/grid-mask-h.out b/docs/probes/flesh/grid-mask-h.out new file mode 100644 index 0000000..cbc196b --- /dev/null +++ b/docs/probes/flesh/grid-mask-h.out @@ -0,0 +1,15 @@ +1 full median 0.980 HASH ce534bd202fabaca +1 sized median 0.476 HASH ce534bd202fabaca +1 empty median 0.094 HASH 14650fb0739d0383 +2 full median 0.991 HASH ce534bd202fabaca +2 sized median 0.456 HASH ce534bd202fabaca +2 empty median 0.095 HASH 14650fb0739d0383 +3 full median 0.987 HASH ce534bd202fabaca +3 sized median 0.496 HASH ce534bd202fabaca +3 empty median 0.097 HASH 14650fb0739d0383 +4 full median 1.011 HASH ce534bd202fabaca +4 sized median 0.488 HASH ce534bd202fabaca +4 empty median 0.097 HASH 14650fb0739d0383 +5 full median 0.988 HASH ce534bd202fabaca +5 sized median 0.486 HASH ce534bd202fabaca +5 empty median 0.096 HASH 14650fb0739d0383 diff --git a/docs/probes/flesh/grid-mask-k.out b/docs/probes/flesh/grid-mask-k.out new file mode 100644 index 0000000..04bd318 --- /dev/null +++ b/docs/probes/flesh/grid-mask-k.out @@ -0,0 +1,20 @@ +1 H X median 0.096 HASH 14650fb0739d0383 +1 H MASKSIZED median 0.095 HASH 14650fb0739d0383 +1 K X median 3.980 HASH 7c43ed61a4d7e339 +1 K MASKSIZED median 3.464 HASH 7c43ed61a4d7e339 +2 H X median 0.094 HASH 14650fb0739d0383 +2 H MASKSIZED median 0.094 HASH 14650fb0739d0383 +2 K X median 4.025 HASH 7c43ed61a4d7e339 +2 K MASKSIZED median 3.519 HASH 7c43ed61a4d7e339 +3 H X median 0.093 HASH 14650fb0739d0383 +3 H MASKSIZED median 0.094 HASH 14650fb0739d0383 +3 K X median 3.883 HASH 7c43ed61a4d7e339 +3 K MASKSIZED median 3.424 HASH 7c43ed61a4d7e339 +4 H X median 0.096 HASH 14650fb0739d0383 +4 H MASKSIZED median 0.096 HASH 14650fb0739d0383 +4 K X median 3.975 HASH 7c43ed61a4d7e339 +4 K MASKSIZED median 3.378 HASH 7c43ed61a4d7e339 +5 H X median 0.096 HASH 14650fb0739d0383 +5 H MASKSIZED median 0.096 HASH 14650fb0739d0383 +5 K X median 3.946 HASH 7c43ed61a4d7e339 +5 K MASKSIZED median 3.565 HASH 7c43ed61a4d7e339 diff --git a/docs/probes/flesh/grid-mask.fella.patch b/docs/probes/flesh/grid-mask.fella.patch new file mode 100644 index 0000000..1230e15 --- /dev/null +++ b/docs/probes/flesh/grid-mask.fella.patch @@ -0,0 +1,27 @@ +--- fella.c ++++ fella.c +@@ -15,7 +15,7 @@ + extern uint8_t* probe_pinned; + extern int32_t* probe_body_group; + extern double probe_t[8]; +-extern int probe_nocontact; ++extern int probe_nocontact; extern int probe_mask_sized; + + typedef struct { float x, y, z; } v3; + typedef struct { float x, y, z, w; } q4; +@@ -410,6 +410,7 @@ + } + if (pinmode) probe_pinned = pinned; + if (getenv("NOCONTACT")) probe_nocontact = 1; ++ if (getenv("MASKSIZED")) probe_mask_sized = 1; + if (filter) probe_body_group = bgroup; + if (filter && ngut) bgroup[0] = 0; // the gut (group 99) collides with every bone; nothing to do + // per-Fella counts +@@ -496,6 +497,7 @@ + printf("FELLA F=%d skin=%.3f muscle=%.3f live=%d pinmode=%d filter=%d gut=%d | per Fella: skin %d muscle %d particles, %d distances (%d stitches), bends total %d, attachments total %d | live particles %d | ms/tick median %.3f mean %.3f p95 %.3f max %.3f | max live deviation %.3f m", + F, s, sm, live, pinmode, filter, gut, per_layer[0], per_layer[1], fd, fs, nbend, natt, livec, tk[nt / 2], mean, tk[(int)(nt * 0.95)], tk[nt - 1], maxdev); + printf(" | phases ms/tick grid %.3f pcontact %.3f att %.3f dist %.3f bend %.3f tether %.3f body %.3f", probe_t[0]/(warm+ticks), probe_t[1]/(warm+ticks), probe_t[2]/(warm+ticks), probe_t[3]/(warm+ticks), probe_t[4]/(warm+ticks), probe_t[5]/(warm+ticks), probe_t[6]/(warm+ticks)); ++ { uint64_t hh = 1469598103934665603ull; for (int i = 0; i < W->particle_count; i++) { const unsigned char* b; b=(const unsigned char*)&W->pos_x[i]; for(int c=0;c<4;c++){hh^=b[c];hh*=1099511628211ull;} b=(const unsigned char*)&W->pos_y[i]; for(int c=0;c<4;c++){hh^=b[c];hh*=1099511628211ull;} b=(const unsigned char*)&W->vel_z[i]; for(int c=0;c<4;c++){hh^=b[c];hh*=1099511628211ull;} } printf(" | HASH %016llx", (unsigned long long)hh); } + if (cut) printf(" | cut %.3f ms sever %.3f ms wake %.4f ms", cut_ms, sever_ms, wake_ms); + printf("\n"); + printf("BUILD %.1f ms;", build_ms); return 0; diff --git a/docs/probes/flesh/grid-mask.probe.patch b/docs/probes/flesh/grid-mask.probe.patch new file mode 100644 index 0000000..6ea3310 --- /dev/null +++ b/docs/probes/flesh/grid-mask.probe.patch @@ -0,0 +1,135 @@ +--- ymir_particles.c ++++ ymir_particles.c +@@ -5,6 +5,16 @@ + // every sum in a fixed order. See ymir_particles.h. + + #include "ymir_particles.h" ++#include ++uint8_t* probe_pinned = 0; int32_t* probe_body_group = 0; ++#include ++double probe_t[8]; ++static double pnow(void){LARGE_INTEGER c,f;QueryPerformanceCounter(&c);QueryPerformanceFrequency(&f);return 1000.0*(double)c.QuadPart/(double)f.QuadPart;} ++#define T(k,x) do{double _t=pnow(); x; probe_t[k]+=pnow()-_t;}while(0) ++int probe_nocontact = 0; ++#define PIN(i) ((probe_pinned && probe_pinned[i]) || (probe_nocontact && !(p->phase[i] & (YMIR_PHASE_SELF_COLLIDE|YMIR_PHASE_FLUID)) && grid_ctx)) ++static int grid_ctx = 0; ++ + + #include + #include +@@ -1358,20 +1368,24 @@ + + // Counting sort into a hash grid of cell 1 / inv_cell: the live particles' predicted positions, fluid lanes left out when + // `solids` is set. Within a bucket particles keep index order, so every neighbour walk is the same on every run. ++int probe_mask_sized = 0; static uint32_t g_mask; + static void grid_sort(ymir_particles* p, float inv_cell, int solids) + { +- int32_t buckets = (int32_t)p->table_mask + 1; ++ uint32_t mask = p->table_mask; ++ if (solids && probe_mask_sized) { int32_t n = 0; for (int32_t i = 0; i < p->particle_count; ++i) n += p->particle_live[i] && !PIN(i) && !(p->phase[i] & YMIR_PHASE_FLUID); uint32_t t = 16; while (t < 2u * (uint32_t)n) t <<= 1; if (t - 1 < mask) mask = t - 1; } ++ g_mask = mask; ++ int32_t buckets = (int32_t)mask + 1; + memset(p->cell_start, 0, sizeof(int32_t) * (size_t)(buckets + 1)); + for (int32_t i = 0; i < p->particle_count; ++i) + { +- if (!p->particle_live[i] || (solids && (p->phase[i] & YMIR_PHASE_FLUID))) ++ if (!p->particle_live[i] || PIN(i) || (solids && (p->phase[i] & YMIR_PHASE_FLUID))) + { + continue; + } + p->cell_x[i] = cell_of(p->pp_x[i], inv_cell); + p->cell_y[i] = cell_of(p->pp_y[i], inv_cell); + p->cell_z[i] = cell_of(p->pp_z[i], inv_cell); +- p->cell_start[cell_hash(p->cell_x[i], p->cell_y[i], p->cell_z[i], p->table_mask) + 1]++; ++ p->cell_start[cell_hash(p->cell_x[i], p->cell_y[i], p->cell_z[i], mask) + 1]++; + } + for (int32_t c = 0; c < buckets; ++c) + { +@@ -1380,9 +1394,9 @@ + } + for (int32_t i = 0; i < p->particle_count; ++i) + { +- if (p->particle_live[i] && !(solids && (p->phase[i] & YMIR_PHASE_FLUID))) ++ if (p->particle_live[i] && !PIN(i) && !(solids && (p->phase[i] & YMIR_PHASE_FLUID))) + { +- uint32_t c = cell_hash(p->cell_x[i], p->cell_y[i], p->cell_z[i], p->table_mask); ++ uint32_t c = cell_hash(p->cell_x[i], p->cell_y[i], p->cell_z[i], mask); + p->sorted[p->cell_start[c] + p->cell_fill[c]++] = i; + } + } +@@ -1461,7 +1475,7 @@ + { + for (int32_t i = 0; i < p->particle_count; ++i) + { +- if (!p->particle_live[i] || (p->phase[i] & YMIR_PHASE_FLUID)) // the grid holds no fluid lane ++ if (!p->particle_live[i] || PIN(i) || (p->phase[i] & YMIR_PHASE_FLUID)) // the grid holds no fluid lane + { + continue; + } +@@ -1469,7 +1483,7 @@ + { + int32_t o = cell == 0 ? 13 : 13 + cell, da = o / 9 - 1, db = o / 3 % 3 - 1, dc = o % 3 - 1; + int32_t ca = p->cell_x[i] + da, cb = p->cell_y[i] + db, cc = p->cell_z[i] + dc; +- uint32_t bucket = cell_hash(ca, cb, cc, p->table_mask); ++ uint32_t bucket = cell_hash(ca, cb, cc, g_mask); + for (int32_t q = p->cell_start[bucket]; q < p->cell_start[bucket + 1]; ++q) + { + int32_t j = p->sorted[q]; +@@ -1979,7 +1993,7 @@ + { + continue; + } +- if (held_by(p, i, k)) ++ if (probe_body_group ? probe_body_group[k] == (int32_t)(p->phase[i] & YMIR_PHASE_GROUP_MASK) : held_by(p, i, k)) + { + continue; + } +@@ -2056,6 +2070,7 @@ + continue; + } + int32_t i = p->att_particle[a]; ++ if (PIN(i)) continue; + v3 at = end_point(p, a); + v3 d = vsub(P(p, p->pp_x, p->pp_y, p->pp_z, i), at); + float len = vlen(d); +@@ -2088,6 +2103,7 @@ + continue; + } + int32_t i = p->dist_a[e], j = p->dist_b[e]; ++ if (PIN(i) && PIN(j)) continue; + float wi = p->inv_mass[i], wj = p->inv_mass[j], alpha = p->dist_compliance[e] / (f->h * f->h); + v3 d = vsub(P(p, p->pp_x, p->pp_y, p->pp_z, j), P(p, p->pp_x, p->pp_y, p->pp_z, i)); + float len = vlen(d); +@@ -2117,6 +2133,7 @@ + continue; + } + int32_t a = p->bend_a[k], b = p->bend_b[k], c = p->bend_c[k]; ++ if (PIN(a) && PIN(b) && PIN(c)) continue; + float wa = p->inv_mass[a], wb = p->inv_mass[b], wc = p->inv_mass[c]; + v3 ja = {0.0f, 0.0f, 0.0f}, jc = {0.0f, 0.0f, 0.0f}; + for (int pass = 0; pass < 2; ++pass) +@@ -2320,17 +2337,17 @@ + } + put(p->pp_x, p->pp_y, p->pp_z, i, x); + } +- build_grid(p); +- particle_contacts(p); ++ grid_ctx = 1; T(0, build_grid(p);); ++ T(1, particle_contacts(p);); grid_ctx = 0; + if (p->live_fluid > 0) + { + fluid_density(p); + } +- solve_attachments(&f); +- solve_distances(&f); +- solve_bends(&f); +- solve_tethers(&f); +- body_contacts(&f); ++ T(2, solve_attachments(&f);); ++ T(3, solve_distances(&f);); ++ T(4, solve_bends(&f);); ++ T(5, solve_tethers(&f);); ++ T(6, body_contacts(&f);); + for (int32_t i = 0; i < p->particle_count; ++i) + { + if (!p->particle_live[i])