feat: replace mesh queries with typed predicates
Amp-Thread-ID: https://ampcode.com/threads/T-019f9d91-77c1-7206-a60f-ed6554ce92ab Co-authored-by: Heaust Azure <heaust.azure@gmail.com>
This commit is contained in:
+17
-17
@@ -5,37 +5,37 @@ import { DerivedBvh } from "../static/bvh-core.js";
|
||||
import { RendererClient } from "../static/renderer-client.js";
|
||||
|
||||
const align16 = value => (value + 15) & ~15;
|
||||
const componentCounts = [1, 1, 1, 3, 3, 1, 1, 1, 1, 1, 16, 3, 3, 1];
|
||||
const scalarTypes = [1, 1, 1, 2, 2, 1, 1, 1, 1, 1, 2, 2, 2, 1];
|
||||
const componentCounts = [1, 1, 3, 3, 1, 1, 1, 1, 16, 3, 3, 16];
|
||||
const scalarTypes = [1, 1, 2, 2, 1, 1, 1, 1, 2, 2, 2, 1];
|
||||
|
||||
function snapshotFixture({ instances = 1 } = {}) {
|
||||
const memory = new WebAssembly.Memory({ initial: 4, maximum: 8, shared: true });
|
||||
const words = new Uint32Array(memory.buffer);
|
||||
const control = new Int32Array(memory.buffer, 0, 64);
|
||||
const ptr = 256;
|
||||
const counts = [1, 1, 1, 1, 1, ...Array(9).fill(instances)];
|
||||
const counts = [1, 1, 1, 1, ...Array(8).fill(instances)];
|
||||
const offsets = [];
|
||||
let cursor = 512;
|
||||
for (let i = 0; i < 14; i++) {
|
||||
let cursor = 448;
|
||||
for (let i = 0; i < 12; i++) {
|
||||
offsets.push(cursor);
|
||||
cursor = align16(cursor + counts[i] * componentCounts[i] * 4);
|
||||
}
|
||||
control.set([0x504e5359, 1, 256, 3, 64, 1, 1, 2, 1, 0, 7, 0, 4, 1, 0, 0]);
|
||||
control.set([2, 1, 1, ptr, cursor, 7, 0, 1, instances, 1, 64, 0, 0, 0, 0, 0], 16);
|
||||
control.set([0x504e5359, 1, 256, 3, 64, 2, 1, 2, 1, 0, 7, 0, 4, 1, 0, 0]);
|
||||
control.set([2, 1, 1, ptr, cursor, 7, 0, 1, instances, 2, 64, 0, 0, 0, 0, 0], 16);
|
||||
const blob = new Uint32Array(memory.buffer, ptr, cursor / 4);
|
||||
blob.set([0x31534452, 1, 64, cursor, 1, 7, 0, 14, 64, 32, 1, instances, 0x01020304, 3, 0, 0]);
|
||||
for (let i = 0; i < 14; i++) {
|
||||
blob.set([0x32534452, 2, 64, cursor, 1, 7, 0, 12, 64, 32, 1, instances, 0x01020304, 3, 0, 0]);
|
||||
for (let i = 0; i < 12; i++) {
|
||||
blob.set([i + 1, scalarTypes[i], offsets[i], counts[i], componentCounts[i], componentCounts[i] * 4, 4, 0], 16 + i * 8);
|
||||
}
|
||||
const stream = i => scalarTypes[i] === 2
|
||||
? new Float32Array(memory.buffer, ptr + offsets[i], counts[i] * componentCounts[i])
|
||||
: new Uint32Array(memory.buffer, ptr + offsets[i], counts[i] * componentCounts[i]);
|
||||
stream(0)[0] = 4; stream(1)[0] = 2; stream(2)[0] = 1;
|
||||
stream(3).set([-1, -1, -1]); stream(4).set([1, 1, 1]);
|
||||
stream(0)[0] = 4; stream(1)[0] = 2;
|
||||
stream(2).set([-1, -1, -1]); stream(3).set([1, 1, 1]);
|
||||
for (let i = 0; i < instances; i++) {
|
||||
stream(5)[i] = 10 + i; stream(6)[i] = 3; stream(7)[i] = 4; stream(8)[i] = 2;
|
||||
stream(9)[i] = 1; stream(13)[i] = 1;
|
||||
stream(11).set([i * 4, -1, -1], i * 3); stream(12).set([i * 4 + 2, 1, 1], i * 3);
|
||||
stream(4)[i] = 10 + i; stream(5)[i] = 3; stream(6)[i] = 4; stream(7)[i] = 2;
|
||||
stream(11)[i * 16] = 1;
|
||||
stream(9).set([i * 4, -1, -1], i * 3); stream(10).set([i * 4 + 2, 1, 1], i * 3);
|
||||
}
|
||||
return { memory, control, ptr, cursor };
|
||||
}
|
||||
@@ -84,7 +84,7 @@ function bvhSnapshot({ pickable = [1, 1], shifted = false } = {}) {
|
||||
return { instanceCount: count, streams: {
|
||||
instanceSlot: Uint32Array.from([5, 6]), instanceGeneration: Uint32Array.from([1, 1]),
|
||||
instanceMeshSlot: Uint32Array.from([2, 2]), instanceMeshGeneration: Uint32Array.from([4, 4]),
|
||||
instancePickable: Uint32Array.from(pickable),
|
||||
instanceType: Uint32Array.from(pickable.flatMap(x => [x, ...Array(15).fill(0)])),
|
||||
instanceWorldMin: Float32Array.from(shifted ? [10, -1, -1, 4, -1, -1] : [2, -1, -1, 4, -1, -1]),
|
||||
instanceWorldMax: Float32Array.from(shifted ? [12, 1, 1, 6, 1, 1] : [3, 1, 1, 6, 1, 1]),
|
||||
}};
|
||||
@@ -112,11 +112,11 @@ test("renderer pick returns gated instances and exact epoch", async () => {
|
||||
const scene = snapshotFixture();
|
||||
const ring = 8192;
|
||||
const ringHeader = new Int32Array(scene.memory.buffer, ring, 16);
|
||||
ringHeader.set([0x4e574159, 1, 1024, 24]);
|
||||
ringHeader.set([0x4e574159, 2, 1024, 40]);
|
||||
const rendererWorker = new WorkerMock(), bvhWorker = new WorkerMock();
|
||||
const bridge = { memory: scene.memory, ringPtr: ring, worker: rendererWorker, workerFactory: () => bvhWorker, free() {} };
|
||||
const client = new RendererClient(bridge);
|
||||
rendererWorker.reply({ type: "snapshot-init", controlPtr: 0, controlVersion: 1, schemaVersion: 1 });
|
||||
rendererWorker.reply({ type: "snapshot-init", controlPtr: 0, controlVersion: 1, schemaVersion: 2 });
|
||||
rendererWorker.reply({ type: "snapshot-published", epoch: 1 });
|
||||
const picking = client.pickRay([0, 0, 0], [1, 0, 0]);
|
||||
const request = bvhWorker.messages.find(message => message.type === "pick");
|
||||
|
||||
Reference in New Issue
Block a user