@aardworx/wombat.rendering 0.8.0 → 0.9.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/core/command.d.ts +7 -9
- package/dist/core/command.d.ts.map +1 -1
- package/dist/core/renderTask.d.ts +64 -1
- package/dist/core/renderTask.d.ts.map +1 -1
- package/dist/core/renderTask.js +3 -1
- package/dist/core/renderTask.js.map +1 -1
- package/dist/resources/preparedRenderObject.js +3 -3
- package/dist/resources/preparedRenderObject.js.map +1 -1
- package/dist/resources/uniformBuffer.d.ts.map +1 -1
- package/dist/resources/uniformBuffer.js +17 -0
- package/dist/resources/uniformBuffer.js.map +1 -1
- package/dist/runtime/derivedUniforms/dispatch.d.ts +105 -0
- package/dist/runtime/derivedUniforms/dispatch.d.ts.map +1 -0
- package/dist/runtime/derivedUniforms/dispatch.js +355 -0
- package/dist/runtime/derivedUniforms/dispatch.js.map +1 -0
- package/dist/runtime/derivedUniforms/index.d.ts +6 -0
- package/dist/runtime/derivedUniforms/index.d.ts.map +1 -0
- package/dist/runtime/derivedUniforms/index.js +26 -0
- package/dist/runtime/derivedUniforms/index.js.map +1 -0
- package/dist/runtime/derivedUniforms/recipes.d.ts +42 -0
- package/dist/runtime/derivedUniforms/recipes.d.ts.map +1 -0
- package/dist/runtime/derivedUniforms/recipes.js +134 -0
- package/dist/runtime/derivedUniforms/recipes.js.map +1 -0
- package/dist/runtime/derivedUniforms/sceneIntegration.d.ts +56 -0
- package/dist/runtime/derivedUniforms/sceneIntegration.d.ts.map +1 -0
- package/dist/runtime/derivedUniforms/sceneIntegration.js +171 -0
- package/dist/runtime/derivedUniforms/sceneIntegration.js.map +1 -0
- package/dist/runtime/derivedUniforms/slotId.d.ts +15 -0
- package/dist/runtime/derivedUniforms/slotId.d.ts.map +1 -0
- package/dist/runtime/derivedUniforms/slotId.js +30 -0
- package/dist/runtime/derivedUniforms/slotId.js.map +1 -0
- package/dist/runtime/derivedUniforms/slots.d.ts +49 -0
- package/dist/runtime/derivedUniforms/slots.d.ts.map +1 -0
- package/dist/runtime/derivedUniforms/slots.js +123 -0
- package/dist/runtime/derivedUniforms/slots.js.map +1 -0
- package/dist/runtime/derivedUniforms/uberKernel.wgsl.d.ts +13 -0
- package/dist/runtime/derivedUniforms/uberKernel.wgsl.d.ts.map +1 -0
- package/dist/runtime/derivedUniforms/uberKernel.wgsl.js +218 -0
- package/dist/runtime/derivedUniforms/uberKernel.wgsl.js.map +1 -0
- package/dist/runtime/flattenTree.d.ts +5 -0
- package/dist/runtime/flattenTree.d.ts.map +1 -0
- package/dist/runtime/flattenTree.js +45 -0
- package/dist/runtime/flattenTree.js.map +1 -0
- package/dist/runtime/heapAdapter.d.ts +16 -0
- package/dist/runtime/heapAdapter.d.ts.map +1 -0
- package/dist/runtime/heapAdapter.js +263 -0
- package/dist/runtime/heapAdapter.js.map +1 -0
- package/dist/runtime/heapEffect.d.ts +235 -0
- package/dist/runtime/heapEffect.d.ts.map +1 -0
- package/dist/runtime/heapEffect.js +518 -0
- package/dist/runtime/heapEffect.js.map +1 -0
- package/dist/runtime/heapEffectIR.d.ts +34 -0
- package/dist/runtime/heapEffectIR.d.ts.map +1 -0
- package/dist/runtime/heapEffectIR.js +1161 -0
- package/dist/runtime/heapEffectIR.js.map +1 -0
- package/dist/runtime/heapEligibility.d.ts +9 -0
- package/dist/runtime/heapEligibility.d.ts.map +1 -0
- package/dist/runtime/heapEligibility.js +195 -0
- package/dist/runtime/heapEligibility.js.map +1 -0
- package/dist/runtime/heapScene.d.ts +240 -31
- package/dist/runtime/heapScene.d.ts.map +1 -1
- package/dist/runtime/heapScene.js +3334 -265
- package/dist/runtime/heapScene.js.map +1 -1
- package/dist/runtime/heapShaderFamily.d.ts +132 -0
- package/dist/runtime/heapShaderFamily.d.ts.map +1 -0
- package/dist/runtime/heapShaderFamily.js +866 -0
- package/dist/runtime/heapShaderFamily.js.map +1 -0
- package/dist/runtime/hybridScene.d.ts +133 -0
- package/dist/runtime/hybridScene.d.ts.map +1 -0
- package/dist/runtime/hybridScene.js +159 -0
- package/dist/runtime/hybridScene.js.map +1 -0
- package/dist/runtime/index.d.ts +8 -0
- package/dist/runtime/index.d.ts.map +1 -1
- package/dist/runtime/index.js +9 -0
- package/dist/runtime/index.js.map +1 -1
- package/dist/runtime/renderTask.d.ts +21 -4
- package/dist/runtime/renderTask.d.ts.map +1 -1
- package/dist/runtime/renderTask.js +127 -32
- package/dist/runtime/renderTask.js.map +1 -1
- package/dist/runtime/runtime.d.ts +33 -3
- package/dist/runtime/runtime.d.ts.map +1 -1
- package/dist/runtime/runtime.js +12 -3
- package/dist/runtime/runtime.js.map +1 -1
- package/dist/runtime/scenePass.d.ts +28 -3
- package/dist/runtime/scenePass.d.ts.map +1 -1
- package/dist/runtime/scenePass.js +37 -4
- package/dist/runtime/scenePass.js.map +1 -1
- package/dist/runtime/textureAtlas/atlasMipGutterKernel.d.ts +35 -0
- package/dist/runtime/textureAtlas/atlasMipGutterKernel.d.ts.map +1 -0
- package/dist/runtime/textureAtlas/atlasMipGutterKernel.js +247 -0
- package/dist/runtime/textureAtlas/atlasMipGutterKernel.js.map +1 -0
- package/dist/runtime/textureAtlas/atlasPool.d.ts +240 -0
- package/dist/runtime/textureAtlas/atlasPool.d.ts.map +1 -0
- package/dist/runtime/textureAtlas/atlasPool.js +798 -0
- package/dist/runtime/textureAtlas/atlasPool.js.map +1 -0
- package/dist/runtime/textureAtlas/bvhTree2d.d.ts +33 -0
- package/dist/runtime/textureAtlas/bvhTree2d.d.ts.map +1 -0
- package/dist/runtime/textureAtlas/bvhTree2d.js +501 -0
- package/dist/runtime/textureAtlas/bvhTree2d.js.map +1 -0
- package/dist/runtime/textureAtlas/packer.d.ts +50 -0
- package/dist/runtime/textureAtlas/packer.d.ts.map +1 -0
- package/dist/runtime/textureAtlas/packer.js +390 -0
- package/dist/runtime/textureAtlas/packer.js.map +1 -0
- package/dist/window/loop.d.ts +14 -0
- package/dist/window/loop.d.ts.map +1 -1
- package/dist/window/loop.js +18 -5
- package/dist/window/loop.js.map +1 -1
- package/package.json +5 -4
- package/src/core/command.ts +9 -9
- package/src/core/renderTask.ts +55 -2
- package/src/resources/preparedRenderObject.ts +3 -3
- package/src/resources/uniformBuffer.ts +15 -0
- package/src/runtime/derivedUniforms/dispatch.ts +407 -0
- package/src/runtime/derivedUniforms/index.ts +52 -0
- package/src/runtime/derivedUniforms/recipes.ts +159 -0
- package/src/runtime/derivedUniforms/sceneIntegration.ts +239 -0
- package/src/runtime/derivedUniforms/slotId.ts +35 -0
- package/src/runtime/derivedUniforms/slots.ts +155 -0
- package/src/runtime/derivedUniforms/uberKernel.wgsl.ts +220 -0
- package/src/runtime/flattenTree.ts +46 -0
- package/src/runtime/heapAdapter.ts +314 -0
- package/src/runtime/heapEffect.ts +719 -0
- package/src/runtime/heapEffectIR.ts +1211 -0
- package/src/runtime/heapEligibility.ts +189 -0
- package/src/runtime/heapScene.ts +3981 -349
- package/src/runtime/heapShaderFamily.ts +1102 -0
- package/src/runtime/hybridScene.ts +310 -0
- package/src/runtime/index.ts +46 -0
- package/src/runtime/renderTask.ts +167 -32
- package/src/runtime/runtime.ts +37 -4
- package/src/runtime/scenePass.ts +39 -4
- package/src/runtime/textureAtlas/atlasMipGutterKernel.ts +291 -0
- package/src/runtime/textureAtlas/atlasPool.ts +991 -0
- package/src/runtime/textureAtlas/bvhTree2d.ts +530 -0
- package/src/runtime/textureAtlas/packer.ts +429 -0
- package/src/window/loop.ts +32 -6
|
@@ -0,0 +1,220 @@
|
|
|
1
|
+
// §7 derived-uniforms uber kernel — single flat dispatch over leaves.
|
|
2
|
+
//
|
|
3
|
+
// Records are static-ish (only edited on RO add/remove). Each thread
|
|
4
|
+
// processes one record:
|
|
5
|
+
// 1. Load record (recipe_id, in0, in1, in2, out_byte).
|
|
6
|
+
// 2. Switch on recipe_id, compute inline (df_mul + collapse), write
|
|
7
|
+
// f32 mat4/mat3 to MainHeap at out_byte.
|
|
8
|
+
//
|
|
9
|
+
// No dirty test on the kernel: when constituents don't change, the
|
|
10
|
+
// kernel rewrites identical bits. Saves a CPU writeBuffer + a binding
|
|
11
|
+
// per dispatch. GPU does ~64 FLOPs per record × N records — trivial at
|
|
12
|
+
// the scales this runs at.
|
|
13
|
+
//
|
|
14
|
+
// df32 primitives mirror examples/df32-precision/. Two opacity hooks:
|
|
15
|
+
// - bitcast<f32>(bits & MASK) for Veltkamp split (integer ops, no
|
|
16
|
+
// algebraic identity).
|
|
17
|
+
// - fma(1.0, x, -y) for compensated subtractions.
|
|
18
|
+
|
|
19
|
+
export const DERIVED_UBER_KERNEL_WGSL = /* wgsl */ `
|
|
20
|
+
|
|
21
|
+
// ─── Bindings ─────────────────────────────────────────────────────
|
|
22
|
+
@group(0) @binding(0) var<storage, read> Constituents: array<vec2<f32>>;
|
|
23
|
+
@group(0) @binding(1) var<storage, read_write> MainHeap: array<f32>;
|
|
24
|
+
|
|
25
|
+
struct Record {
|
|
26
|
+
recipe_id: u32,
|
|
27
|
+
in0: u32, // constituent slot index
|
|
28
|
+
in1: u32, // constituent slot index, 0 if unused
|
|
29
|
+
in2: u32, // constituent slot index, 0 if unused
|
|
30
|
+
out_byte: u32, // byte offset in MainHeap
|
|
31
|
+
_pad: u32, // bumps stride to 24 (defensively 16-aligned)
|
|
32
|
+
}
|
|
33
|
+
@group(0) @binding(2) var<storage, read> Records: array<Record>;
|
|
34
|
+
|
|
35
|
+
// Live record count. arrayLength(Records) reports the GPU buffer's
|
|
36
|
+
// capacity (rounded up by allocator growth), not the live count —
|
|
37
|
+
// trailing slots hold stale data from swap-removed records. Threads
|
|
38
|
+
// past Count.count would read stale records and trample random
|
|
39
|
+
// MainHeap bytes.
|
|
40
|
+
struct CountUniform { count: u32 }
|
|
41
|
+
@group(0) @binding(3) var<uniform> Count: CountUniform;
|
|
42
|
+
|
|
43
|
+
// ─── df32 primitives ──────────────────────────────────────────────
|
|
44
|
+
|
|
45
|
+
fn split12(a: f32) -> vec2<f32> {
|
|
46
|
+
let hi = bitcast<f32>(bitcast<u32>(a) & 0xFFFFE000u);
|
|
47
|
+
let lo = a - hi;
|
|
48
|
+
return vec2<f32>(hi, lo);
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
fn two_sum(a: f32, b: f32) -> vec2<f32> {
|
|
52
|
+
let s = a + b;
|
|
53
|
+
let bb = fma(1.0, s, -a);
|
|
54
|
+
let t1 = fma(1.0, s, -bb);
|
|
55
|
+
let t2 = fma(1.0, a, -t1);
|
|
56
|
+
let t3 = fma(1.0, b, -bb);
|
|
57
|
+
return vec2<f32>(s, t2 + t3);
|
|
58
|
+
}
|
|
59
|
+
|
|
60
|
+
fn quick_two_sum(a: f32, b: f32) -> vec2<f32> {
|
|
61
|
+
let s = a + b;
|
|
62
|
+
let t = fma(1.0, s, -a);
|
|
63
|
+
return vec2<f32>(s, fma(1.0, b, -t));
|
|
64
|
+
}
|
|
65
|
+
|
|
66
|
+
fn two_prod(a: f32, b: f32) -> vec2<f32> {
|
|
67
|
+
let p = a * b;
|
|
68
|
+
let A = split12(a);
|
|
69
|
+
let B = split12(b);
|
|
70
|
+
let err = ((A.x * B.x - p) + A.x * B.y + A.y * B.x) + A.y * B.y;
|
|
71
|
+
return vec2<f32>(p, err);
|
|
72
|
+
}
|
|
73
|
+
|
|
74
|
+
fn df_add(a: vec2<f32>, b: vec2<f32>) -> vec2<f32> {
|
|
75
|
+
let s = two_sum(a.x, b.x);
|
|
76
|
+
let t = two_sum(a.y, b.y);
|
|
77
|
+
let s3 = quick_two_sum(s.x, s.y + t.x);
|
|
78
|
+
return quick_two_sum(s3.x, s3.y + t.y);
|
|
79
|
+
}
|
|
80
|
+
|
|
81
|
+
fn df_mul(a: vec2<f32>, b: vec2<f32>) -> vec2<f32> {
|
|
82
|
+
let p = two_prod(a.x, b.x);
|
|
83
|
+
let cross1 = fma(a.x, b.y, p.y);
|
|
84
|
+
let cross = fma(a.y, b.x, cross1);
|
|
85
|
+
return quick_two_sum(p.x, cross);
|
|
86
|
+
}
|
|
87
|
+
|
|
88
|
+
// ─── Constituents access ──────────────────────────────────────────
|
|
89
|
+
//
|
|
90
|
+
// One slot = 16 vec2<f32> = one df32 mat4 in row-major order
|
|
91
|
+
// (matching Aardvark M44d convention). Element (row, col) of slot k
|
|
92
|
+
// lives at vec2 index k*16 + row*4 + col.
|
|
93
|
+
|
|
94
|
+
fn read_entry(slot: u32, row: u32, col: u32) -> vec2<f32> {
|
|
95
|
+
return Constituents[slot * 16u + row * 4u + col];
|
|
96
|
+
}
|
|
97
|
+
|
|
98
|
+
// (A · B) entry (r, c) in df32, both A and B in constituents.
|
|
99
|
+
fn df_mul_entry(a_slot: u32, b_slot: u32, r: u32, c: u32) -> vec2<f32> {
|
|
100
|
+
var acc = vec2<f32>(0.0, 0.0);
|
|
101
|
+
for (var k: u32 = 0u; k < 4u; k = k + 1u) {
|
|
102
|
+
acc = df_add(acc, df_mul(read_entry(a_slot, r, k), read_entry(b_slot, k, c)));
|
|
103
|
+
}
|
|
104
|
+
return acc;
|
|
105
|
+
}
|
|
106
|
+
|
|
107
|
+
// Compute one full row of (A · B), returns 4 df32 entries.
|
|
108
|
+
fn df_mul_row(a_slot: u32, b_slot: u32, r: u32) -> array<vec2<f32>, 4> {
|
|
109
|
+
var out: array<vec2<f32>, 4>;
|
|
110
|
+
for (var c: u32 = 0u; c < 4u; c = c + 1u) {
|
|
111
|
+
out[c] = df_mul_entry(a_slot, b_slot, r, c);
|
|
112
|
+
}
|
|
113
|
+
return out;
|
|
114
|
+
}
|
|
115
|
+
|
|
116
|
+
// Triple product (A · B · C) entry (r, c). Uses 4 df_muls per entry
|
|
117
|
+
// — recomputes the inner row each call. Acceptable: at most one
|
|
118
|
+
// such recipe per RO and only on dirty frames.
|
|
119
|
+
fn df_mul3_entry(a_slot: u32, b_slot: u32, c_slot: u32, r: u32, c: u32) -> vec2<f32> {
|
|
120
|
+
// (A·B·C)[r,c] = sum_k (A·B)[r,k] · C[k,c]
|
|
121
|
+
var acc = vec2<f32>(0.0, 0.0);
|
|
122
|
+
for (var k: u32 = 0u; k < 4u; k = k + 1u) {
|
|
123
|
+
let ab_rk = df_mul_entry(a_slot, b_slot, r, k);
|
|
124
|
+
acc = df_add(acc, df_mul(ab_rk, read_entry(c_slot, k, c)));
|
|
125
|
+
}
|
|
126
|
+
return acc;
|
|
127
|
+
}
|
|
128
|
+
|
|
129
|
+
// ─── Writers ──────────────────────────────────────────────────────
|
|
130
|
+
//
|
|
131
|
+
// Mat4: 16 contiguous f32s, row-major.
|
|
132
|
+
// Mat3: 3 rows × 4-stride (std140 mat3<f32>), 12 f32s; last column
|
|
133
|
+
// per row left untouched.
|
|
134
|
+
|
|
135
|
+
fn write_mat4_entry(out_byte: u32, row: u32, col: u32, val: f32) {
|
|
136
|
+
MainHeap[(out_byte >> 2u) + row * 4u + col] = val;
|
|
137
|
+
}
|
|
138
|
+
|
|
139
|
+
fn write_mat3_entry(out_byte: u32, row: u32, col: u32, val: f32) {
|
|
140
|
+
MainHeap[(out_byte >> 2u) + row * 4u + col] = val;
|
|
141
|
+
}
|
|
142
|
+
|
|
143
|
+
// ─── Recipe arms ──────────────────────────────────────────────────
|
|
144
|
+
|
|
145
|
+
fn run_collapse_mat4(in0: u32, out_byte: u32) {
|
|
146
|
+
for (var r: u32 = 0u; r < 4u; r = r + 1u) {
|
|
147
|
+
for (var c: u32 = 0u; c < 4u; c = c + 1u) {
|
|
148
|
+
let e = read_entry(in0, r, c);
|
|
149
|
+
write_mat4_entry(out_byte, r, c, e.x + e.y);
|
|
150
|
+
}
|
|
151
|
+
}
|
|
152
|
+
}
|
|
153
|
+
|
|
154
|
+
// NormalMatrix: (M⁻¹)ᵀ upper-3×3.
|
|
155
|
+
// NM[i, j] = MInv[j, i]
|
|
156
|
+
fn run_collapse_mat3_normal(in0: u32, out_byte: u32) {
|
|
157
|
+
for (var i: u32 = 0u; i < 3u; i = i + 1u) {
|
|
158
|
+
for (var j: u32 = 0u; j < 3u; j = j + 1u) {
|
|
159
|
+
let e = read_entry(in0, j, i);
|
|
160
|
+
write_mat3_entry(out_byte, i, j, e.x + e.y);
|
|
161
|
+
}
|
|
162
|
+
}
|
|
163
|
+
}
|
|
164
|
+
|
|
165
|
+
fn run_dfmul2_collapse(in0: u32, in1: u32, out_byte: u32) {
|
|
166
|
+
for (var r: u32 = 0u; r < 4u; r = r + 1u) {
|
|
167
|
+
for (var c: u32 = 0u; c < 4u; c = c + 1u) {
|
|
168
|
+
let e = df_mul_entry(in0, in1, r, c);
|
|
169
|
+
write_mat4_entry(out_byte, r, c, e.x + e.y);
|
|
170
|
+
}
|
|
171
|
+
}
|
|
172
|
+
}
|
|
173
|
+
|
|
174
|
+
fn run_dfmul3_collapse(in0: u32, in1: u32, in2: u32, out_byte: u32) {
|
|
175
|
+
for (var r: u32 = 0u; r < 4u; r = r + 1u) {
|
|
176
|
+
for (var c: u32 = 0u; c < 4u; c = c + 1u) {
|
|
177
|
+
let e = df_mul3_entry(in0, in1, in2, r, c);
|
|
178
|
+
write_mat4_entry(out_byte, r, c, e.x + e.y);
|
|
179
|
+
}
|
|
180
|
+
}
|
|
181
|
+
}
|
|
182
|
+
|
|
183
|
+
// ─── Entry ────────────────────────────────────────────────────────
|
|
184
|
+
|
|
185
|
+
@compute @workgroup_size(64)
|
|
186
|
+
fn main(@builtin(global_invocation_id) gid: vec3<u32>) {
|
|
187
|
+
if (gid.x >= Count.count) { return; }
|
|
188
|
+
let r = Records[gid.x];
|
|
189
|
+
switch r.recipe_id {
|
|
190
|
+
case 0u: { run_collapse_mat4(r.in0, r.out_byte); } // ModelTrafo
|
|
191
|
+
case 1u: { run_collapse_mat4(r.in0, r.out_byte); } // ModelTrafoInv
|
|
192
|
+
case 2u: { run_collapse_mat3_normal(r.in0, r.out_byte); } // NormalMatrix
|
|
193
|
+
case 3u: { run_dfmul2_collapse(r.in0, r.in1, r.out_byte); } // ModelViewTrafo
|
|
194
|
+
case 4u: { run_dfmul2_collapse(r.in0, r.in1, r.out_byte); } // ModelViewTrafoInv
|
|
195
|
+
case 5u: { run_dfmul3_collapse(r.in0, r.in1, r.in2, r.out_byte); } // ModelViewProjTrafo
|
|
196
|
+
case 6u: { run_dfmul3_collapse(r.in0, r.in1, r.in2, r.out_byte); } // ModelViewProjTrafoInv
|
|
197
|
+
case 7u: { run_collapse_mat4(r.in0, r.out_byte); } // ViewTrafo
|
|
198
|
+
case 8u: { run_collapse_mat4(r.in0, r.out_byte); } // ViewTrafoInv
|
|
199
|
+
case 9u: { run_dfmul2_collapse(r.in0, r.in1, r.out_byte); } // ViewProjTrafo
|
|
200
|
+
case 10u:{ run_dfmul2_collapse(r.in0, r.in1, r.out_byte); } // ViewProjTrafoInv
|
|
201
|
+
case 11u:{ run_collapse_mat4(r.in0, r.out_byte); } // ProjTrafo
|
|
202
|
+
case 12u:{ run_collapse_mat4(r.in0, r.out_byte); } // ProjTrafoInv
|
|
203
|
+
default: { return; }
|
|
204
|
+
}
|
|
205
|
+
}
|
|
206
|
+
|
|
207
|
+
`;
|
|
208
|
+
|
|
209
|
+
/** Records are 5× u32 = 20 bytes. WGSL storage struct alignment of
|
|
210
|
+
* 4 means the array stride is 20 bytes. */
|
|
211
|
+
export const RECORD_STRIDE_BYTES = 24;
|
|
212
|
+
export const RECORD_STRIDE_U32 = 6;
|
|
213
|
+
|
|
214
|
+
export const RECORD_FIELD = {
|
|
215
|
+
recipe_id: 0,
|
|
216
|
+
in0: 1,
|
|
217
|
+
in1: 2,
|
|
218
|
+
in2: 3,
|
|
219
|
+
out_byte: 4,
|
|
220
|
+
} as const;
|
|
@@ -0,0 +1,46 @@
|
|
|
1
|
+
// flattenTree — reactive `RenderTree → aset<RenderObject>`.
|
|
2
|
+
//
|
|
3
|
+
// Inside a single Render command, draw order is not load-bearing
|
|
4
|
+
// (formal contract: an `aset` of objects, not a sequence). The two
|
|
5
|
+
// node-kinds `Ordered` and `Unordered` collapse to the same flat
|
|
6
|
+
// set — anything ordering-sensitive belongs at the Command level
|
|
7
|
+
// (separate Render passes, in the Command list's declared order).
|
|
8
|
+
//
|
|
9
|
+
// Reactive variants (`Adaptive`, `OrderedFromList`, `UnorderedFromSet`)
|
|
10
|
+
// stay reactive: marks propagate through `bind` / `collect`, and the
|
|
11
|
+
// resulting aset reflects current tree state without a manual rebuild.
|
|
12
|
+
//
|
|
13
|
+
// `OrderedFromList`'s `alist<RenderTree>` is converted via
|
|
14
|
+
// `AListBridges.toASet` — this drops index/order info, which we
|
|
15
|
+
// wouldn't honor anyway.
|
|
16
|
+
|
|
17
|
+
import { ASet, type aset } from "@aardworx/wombat.adaptive";
|
|
18
|
+
import { AListBridges } from "@aardworx/wombat.adaptive";
|
|
19
|
+
import type { RenderObject } from "../core/renderObject.js";
|
|
20
|
+
import type { RenderTree } from "../core/renderTree.js";
|
|
21
|
+
|
|
22
|
+
export function flattenRenderTree(tree: RenderTree): aset<RenderObject> {
|
|
23
|
+
switch (tree.kind) {
|
|
24
|
+
case "Empty":
|
|
25
|
+
return ASet.empty<RenderObject>();
|
|
26
|
+
case "Leaf":
|
|
27
|
+
return ASet.single(tree.object);
|
|
28
|
+
case "Ordered":
|
|
29
|
+
case "Unordered": {
|
|
30
|
+
// N-ary union via `unionMany(aset<aset<T>>)`. The naïve
|
|
31
|
+
// `children.reduce(ASet.union)` builds a 2N-deep nested-union
|
|
32
|
+
// tree; the reader's traversal recurses through it and blows
|
|
33
|
+
// the stack at ~2K leaves. `unionMany` is O(1)-deep regardless.
|
|
34
|
+
const children = tree.children.map(flattenRenderTree);
|
|
35
|
+
if (children.length === 0) return ASet.empty<RenderObject>();
|
|
36
|
+
if (children.length === 1) return children[0]!;
|
|
37
|
+
return ASet.unionMany(ASet.ofArray(children));
|
|
38
|
+
}
|
|
39
|
+
case "Adaptive":
|
|
40
|
+
return ASet.bind(flattenRenderTree, tree.tree);
|
|
41
|
+
case "OrderedFromList":
|
|
42
|
+
return ASet.collect(flattenRenderTree, AListBridges.toASet(tree.children));
|
|
43
|
+
case "UnorderedFromSet":
|
|
44
|
+
return ASet.collect(flattenRenderTree, tree.children);
|
|
45
|
+
}
|
|
46
|
+
}
|
|
@@ -0,0 +1,314 @@
|
|
|
1
|
+
// heapAdapter — `RenderObject → HeapDrawSpec`.
|
|
2
|
+
//
|
|
3
|
+
// Reactive: returns the same `HeapDrawSpec` shape the heap path
|
|
4
|
+
// already accepts. Per-input wiring:
|
|
5
|
+
//
|
|
6
|
+
// - vertexAttributes → spec.inputs[name] = BufferView (heap path
|
|
7
|
+
// handles BufferView ingest natively, pivots
|
|
8
|
+
// the pool aval-key onto the underlying
|
|
9
|
+
// `aval<IBuffer>` for share-by-identity).
|
|
10
|
+
// - uniforms → spec.inputs[name] = aval<unknown>.
|
|
11
|
+
// - textures/samplers → folded into a HeapTextureSet (single pair
|
|
12
|
+
// v1; the eligibility classifier already
|
|
13
|
+
// rejects multi-binding ROs).
|
|
14
|
+
// - indices → BufferView's `aval<IBuffer>` mapped to
|
|
15
|
+
// `aval<Uint32Array>` for the heap path's
|
|
16
|
+
// IndexPool.
|
|
17
|
+
// - effect → passed through.
|
|
18
|
+
// - pipelineState → passed through (heap path bucket-keys it).
|
|
19
|
+
//
|
|
20
|
+
// Drawcall wedges (instanceCount > 1, baseVertex/firstIndex/
|
|
21
|
+
// firstInstance != 0, non-indexed) are screened out by
|
|
22
|
+
// `isHeapEligible`; this adapter is the partner that converts the
|
|
23
|
+
// heap-eligible subset. If we see something we can't handle, throw —
|
|
24
|
+
// that's a classifier/adapter mismatch, a bug.
|
|
25
|
+
//
|
|
26
|
+
// No multi-binding texture API yet: `ro.textures.count <= 1` and
|
|
27
|
+
// `ro.samplers.count <= 1` are classifier invariants.
|
|
28
|
+
|
|
29
|
+
import { type aval, type AdaptiveToken } from "@aardworx/wombat.adaptive";
|
|
30
|
+
import type { IBuffer, HostBufferSource } from "../core/buffer.js";
|
|
31
|
+
import type { BufferView } from "../core/bufferView.js";
|
|
32
|
+
import { ITexture, type HostTextureSource } from "../core/texture.js";
|
|
33
|
+
import { ISampler } from "../core/sampler.js";
|
|
34
|
+
import type { RenderObject } from "../core/renderObject.js";
|
|
35
|
+
import type { HeapDrawSpec, HeapTextureSet } from "./heapScene.js";
|
|
36
|
+
import { AtlasPool } from "./textureAtlas/atlasPool.js";
|
|
37
|
+
|
|
38
|
+
/**
|
|
39
|
+
* View `HostBufferSource` as `Uint32Array`. Index buffers are u32 in
|
|
40
|
+
* the heap path; non-u32 indices would have to be transcoded (out
|
|
41
|
+
* of scope — eligibility doesn't currently inspect index format).
|
|
42
|
+
*/
|
|
43
|
+
function asUint32(data: HostBufferSource): Uint32Array {
|
|
44
|
+
if (data instanceof Uint32Array) return data;
|
|
45
|
+
if (ArrayBuffer.isView(data)) {
|
|
46
|
+
return new Uint32Array(data.buffer, data.byteOffset, data.byteLength / 4);
|
|
47
|
+
}
|
|
48
|
+
return new Uint32Array(data);
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
// IndexPool keys on aval identity → ROs sharing the same
|
|
52
|
+
// `ro.indices.buffer` must produce the SAME downstream
|
|
53
|
+
// `aval<Uint32Array>`. A fresh `.map(…)` per RO would defeat the
|
|
54
|
+
// pool's identity-based sharing and balloon GPU memory (e.g. 11K
|
|
55
|
+
// spheres each allocating their own copy of the 6144-index buffer
|
|
56
|
+
// → 270 MB).
|
|
57
|
+
const indicesAvalCache = new WeakMap<aval<IBuffer>, aval<Uint32Array>>();
|
|
58
|
+
|
|
59
|
+
// Per-aval shared "current pool ref" cell. ROs that share an
|
|
60
|
+
// `aval<ITexture>` (and therefore one AtlasPool entry) all read
|
|
61
|
+
// through the same cell; `repack` updates `cell.ref` so every
|
|
62
|
+
// `release` closure frees the latest sub-rect after a swap.
|
|
63
|
+
const currentRefCellCache = new WeakMap<aval<ITexture>, { ref: number }>();
|
|
64
|
+
function currentRefCellFor(av: aval<ITexture>, initial: number): { ref: number } {
|
|
65
|
+
let c = currentRefCellCache.get(av);
|
|
66
|
+
if (c === undefined) {
|
|
67
|
+
c = { ref: initial };
|
|
68
|
+
currentRefCellCache.set(av, c);
|
|
69
|
+
}
|
|
70
|
+
return c;
|
|
71
|
+
}
|
|
72
|
+
function indicesAvalFor(ibAval: aval<IBuffer>): aval<Uint32Array> {
|
|
73
|
+
let av = indicesAvalCache.get(ibAval);
|
|
74
|
+
if (av === undefined) {
|
|
75
|
+
av = ibAval.map((ib: IBuffer) => {
|
|
76
|
+
if (ib.kind !== "host") {
|
|
77
|
+
throw new Error("heapAdapter: index buffer flipped to native GPUBuffer; classifier should have caught this");
|
|
78
|
+
}
|
|
79
|
+
return asUint32(ib.data);
|
|
80
|
+
});
|
|
81
|
+
indicesAvalCache.set(ibAval, av);
|
|
82
|
+
}
|
|
83
|
+
return av;
|
|
84
|
+
}
|
|
85
|
+
|
|
86
|
+
/**
|
|
87
|
+
* Pull `(format, width, height, mipLevelCount)` out of an ITexture so
|
|
88
|
+
* we can run Tier-S eligibility against it. Both variants expose this:
|
|
89
|
+
*
|
|
90
|
+
* - `kind: "host"` with a `raw` source carries the fields directly
|
|
91
|
+
* (`width`, `height`, `format`, optional `mipLevelCount`).
|
|
92
|
+
* - `kind: "host"` with an `external` source (`ImageBitmap`,
|
|
93
|
+
* `HTMLCanvasElement`, `ImageData`, …) reads `.width`/`.height`
|
|
94
|
+
* off the image and uses the source's `format` override (default
|
|
95
|
+
* `rgba8unorm`); `generateMips` triggers a >1 mip count.
|
|
96
|
+
* - `kind: "gpu"` reads `.format`/`.width`/`.height`/`.mipLevelCount`
|
|
97
|
+
* off the resolved `GPUTexture`. Width/height come from the WebGPU
|
|
98
|
+
* texture surface; no host source is exposed (the pool can't
|
|
99
|
+
* CPU-downscale, so mip generation defers to the existing GPU
|
|
100
|
+
* mip chain when wantsMips is true).
|
|
101
|
+
*
|
|
102
|
+
* Returns `null` for sources we can't measure (e.g. an `external`
|
|
103
|
+
* source without dimensions — `HTMLVideoElement` whose readyState
|
|
104
|
+
* hasn't given us a video frame yet).
|
|
105
|
+
*/
|
|
106
|
+
interface TextureDescriptor {
|
|
107
|
+
format: GPUTextureFormat;
|
|
108
|
+
width: number;
|
|
109
|
+
height: number;
|
|
110
|
+
mipLevelCount: number;
|
|
111
|
+
host?: HostTextureSource;
|
|
112
|
+
}
|
|
113
|
+
function describeTexture(t: ITexture): TextureDescriptor | null {
|
|
114
|
+
if (t.kind === "gpu") {
|
|
115
|
+
const tex = t.texture;
|
|
116
|
+
return {
|
|
117
|
+
format: tex.format,
|
|
118
|
+
width: tex.width,
|
|
119
|
+
height: tex.height,
|
|
120
|
+
mipLevelCount: tex.mipLevelCount,
|
|
121
|
+
};
|
|
122
|
+
}
|
|
123
|
+
const src = t.source;
|
|
124
|
+
if (src.kind === "raw") {
|
|
125
|
+
return {
|
|
126
|
+
format: src.format,
|
|
127
|
+
width: src.width,
|
|
128
|
+
height: src.height,
|
|
129
|
+
mipLevelCount: src.mipLevelCount ?? 1,
|
|
130
|
+
host: src,
|
|
131
|
+
};
|
|
132
|
+
}
|
|
133
|
+
// external — read width/height off the source. Most types have it,
|
|
134
|
+
// HTMLVideoElement uses `videoWidth/videoHeight`.
|
|
135
|
+
const ext = src.source as unknown;
|
|
136
|
+
let w = 0, h = 0;
|
|
137
|
+
if (typeof HTMLVideoElement !== "undefined" && ext instanceof HTMLVideoElement) {
|
|
138
|
+
w = ext.videoWidth; h = ext.videoHeight;
|
|
139
|
+
} else if (
|
|
140
|
+
typeof ImageData !== "undefined" && ext instanceof ImageData
|
|
141
|
+
) {
|
|
142
|
+
w = ext.width; h = ext.height;
|
|
143
|
+
} else {
|
|
144
|
+
const any = ext as { width?: number; height?: number };
|
|
145
|
+
w = any.width ?? 0;
|
|
146
|
+
h = any.height ?? 0;
|
|
147
|
+
}
|
|
148
|
+
if (w <= 0 || h <= 0) return null;
|
|
149
|
+
return {
|
|
150
|
+
format: src.format ?? "rgba8unorm",
|
|
151
|
+
width: w,
|
|
152
|
+
height: h,
|
|
153
|
+
mipLevelCount: src.generateMips ? Math.floor(Math.log2(Math.max(w, h))) + 1 : 1,
|
|
154
|
+
host: src,
|
|
155
|
+
};
|
|
156
|
+
}
|
|
157
|
+
|
|
158
|
+
/**
|
|
159
|
+
* Convert a heap-eligible `RenderObject` into a `HeapDrawSpec`. The
|
|
160
|
+
* caller (hybrid render task) is responsible for ensuring eligibility
|
|
161
|
+
* before calling — this adapter throws on any disagreement.
|
|
162
|
+
*
|
|
163
|
+
* The `token` is used once to read `drawCall` and the (single) texture
|
|
164
|
+
* + sampler avals at addDraw time. Per-frame data updates flow
|
|
165
|
+
* through the heap path's pool/repack machinery via the returned
|
|
166
|
+
* spec's avals — this function does not subscribe to anything.
|
|
167
|
+
*/
|
|
168
|
+
export function renderObjectToHeapSpec(
|
|
169
|
+
ro: RenderObject,
|
|
170
|
+
token: AdaptiveToken,
|
|
171
|
+
pool?: AtlasPool,
|
|
172
|
+
): HeapDrawSpec {
|
|
173
|
+
// 1. Inputs map: vertex attributes (BufferView) + uniforms.
|
|
174
|
+
const inputs: { [name: string]: aval<unknown> | unknown } = {};
|
|
175
|
+
ro.vertexAttributes.iter((name, bv: BufferView) => { inputs[name] = bv; });
|
|
176
|
+
ro.uniforms.iter((name, av) => { inputs[name] = av; });
|
|
177
|
+
|
|
178
|
+
// 1b. Instance attributes — same shape, threaded via the heap path's
|
|
179
|
+
// per-RO instancing fast path (one record / one drawIndirect for
|
|
180
|
+
// `instanceCount > 1`, with per-instance attribute reads indexed
|
|
181
|
+
// by the in-RO instance idx).
|
|
182
|
+
let instanceAttributes: { [name: string]: aval<unknown> | unknown } | undefined;
|
|
183
|
+
if (ro.instanceAttributes !== undefined && ro.instanceAttributes.count > 0) {
|
|
184
|
+
instanceAttributes = {};
|
|
185
|
+
ro.instanceAttributes.iter((name, bv: BufferView) => {
|
|
186
|
+
instanceAttributes![name] = bv;
|
|
187
|
+
});
|
|
188
|
+
}
|
|
189
|
+
|
|
190
|
+
// 2. Indices: BufferView → aval<Uint32Array>. Map the underlying
|
|
191
|
+
// IBuffer aval; the heap path's IndexPool will key on this aval.
|
|
192
|
+
if (ro.indices === undefined) {
|
|
193
|
+
throw new Error("heapAdapter: RenderObject without indices not supported (heap is indexed-only)");
|
|
194
|
+
}
|
|
195
|
+
const indices: aval<Uint32Array> = indicesAvalFor(ro.indices.buffer);
|
|
196
|
+
|
|
197
|
+
// 3. Texture/sampler: single-pair v1. Classifier already capped
|
|
198
|
+
// counts at 1 each.
|
|
199
|
+
let textures: HeapTextureSet | undefined;
|
|
200
|
+
if (ro.textures.count === 1 && ro.samplers.count === 1) {
|
|
201
|
+
let texAval: aval<unknown> | undefined;
|
|
202
|
+
let texVal: ITexture | undefined;
|
|
203
|
+
let samplerAval: aval<unknown> | undefined;
|
|
204
|
+
let samplerVal: ISampler | undefined;
|
|
205
|
+
ro.textures.iter((_n, av) => {
|
|
206
|
+
texAval = av;
|
|
207
|
+
texVal = av.getValue(token) as ITexture;
|
|
208
|
+
});
|
|
209
|
+
ro.samplers.iter((_n, av) => {
|
|
210
|
+
samplerAval = av;
|
|
211
|
+
samplerVal = av.getValue(token) as ISampler;
|
|
212
|
+
});
|
|
213
|
+
void samplerAval;
|
|
214
|
+
if (texVal === undefined || samplerVal === undefined) {
|
|
215
|
+
throw new Error("heapAdapter: missing texture/sampler value");
|
|
216
|
+
}
|
|
217
|
+
|
|
218
|
+
// Inspect format/dimensions/mipLevels from ITexture. Both
|
|
219
|
+
// variants expose enough to classify:
|
|
220
|
+
// - kind: "host" → carries width/height/format on the source
|
|
221
|
+
// descriptor (raw) or implicit (external; default rgba8unorm).
|
|
222
|
+
// - kind: "gpu" → resolved GPUTexture exposes .format/.width/
|
|
223
|
+
// .height/.mipLevelCount.
|
|
224
|
+
const dims = describeTexture(texVal);
|
|
225
|
+
|
|
226
|
+
// Tier-S routing: pool present, format eligible, dims ≤ cap. Mips
|
|
227
|
+
// are honoured if the source advertises them.
|
|
228
|
+
const atlasFormat = dims === null ? null : AtlasPool.eligibleFormat(dims.format);
|
|
229
|
+
if (
|
|
230
|
+
pool !== undefined &&
|
|
231
|
+
dims !== null &&
|
|
232
|
+
atlasFormat !== null &&
|
|
233
|
+
AtlasPool.eligibleSize(dims.width, dims.height)
|
|
234
|
+
) {
|
|
235
|
+
const wantsMips = dims.mipLevelCount > 1;
|
|
236
|
+
const acq = pool.acquire(
|
|
237
|
+
atlasFormat,
|
|
238
|
+
texAval as aval<ITexture>,
|
|
239
|
+
dims.width,
|
|
240
|
+
dims.height,
|
|
241
|
+
{
|
|
242
|
+
wantsMips,
|
|
243
|
+
...(dims.host !== undefined ? { source: { width: dims.width, height: dims.height, host: dims.host } } : {}),
|
|
244
|
+
},
|
|
245
|
+
);
|
|
246
|
+
const texAvalTyped = texAval as aval<ITexture>;
|
|
247
|
+
// Per-aval shared current-ref cell so all ROs sharing a single
|
|
248
|
+
// `aval<ITexture>` see the same "live" pool ref. After a repack
|
|
249
|
+
// every release closure reads the cell and frees the LATEST
|
|
250
|
+
// sub-rect — no leaks even when multiple ROs are bucket-shared.
|
|
251
|
+
const cell = currentRefCellFor(texAvalTyped, acq.ref);
|
|
252
|
+
textures = {
|
|
253
|
+
kind: "atlas",
|
|
254
|
+
format: atlasFormat,
|
|
255
|
+
pageId: acq.pageId,
|
|
256
|
+
origin: acq.origin,
|
|
257
|
+
size: acq.size,
|
|
258
|
+
numMips: acq.numMips,
|
|
259
|
+
sampler: samplerVal,
|
|
260
|
+
page: acq.page,
|
|
261
|
+
poolRef: acq.ref,
|
|
262
|
+
release: () => pool.release(cell.ref),
|
|
263
|
+
// Reactivity hooks: heapScene subscribes to `sourceAval`; on
|
|
264
|
+
// mark, drains via `repack(newTex)` — the pool frees the old
|
|
265
|
+
// sub-rect, acquires a new one, and the heap path rewrites
|
|
266
|
+
// the drawHeader fields. Mirrors UniformPool.repack(av, val).
|
|
267
|
+
sourceAval: texAvalTyped,
|
|
268
|
+
repack: (newTex: ITexture) => {
|
|
269
|
+
const next = pool.repack(texAvalTyped, newTex, {
|
|
270
|
+
wantsMips: dims.mipLevelCount > 1,
|
|
271
|
+
});
|
|
272
|
+
cell.ref = next.ref;
|
|
273
|
+
return next;
|
|
274
|
+
},
|
|
275
|
+
};
|
|
276
|
+
} else {
|
|
277
|
+
// Tier-L fallback: keep the existing standalone path, which
|
|
278
|
+
// requires a resolved GPUTexture/GPUSampler.
|
|
279
|
+
if (texVal.kind !== "gpu") {
|
|
280
|
+
throw new Error("heapAdapter: standalone path requires ITexture.kind === 'gpu'");
|
|
281
|
+
}
|
|
282
|
+
if (samplerVal.kind !== "gpu") {
|
|
283
|
+
throw new Error("heapAdapter: standalone path requires ISampler.kind === 'gpu'");
|
|
284
|
+
}
|
|
285
|
+
textures = {
|
|
286
|
+
kind: "standalone",
|
|
287
|
+
texture: ITexture.fromGPU(texVal.texture),
|
|
288
|
+
sampler: ISampler.fromGPU(samplerVal.sampler),
|
|
289
|
+
};
|
|
290
|
+
}
|
|
291
|
+
} else if (ro.textures.count > 0 || ro.samplers.count > 0) {
|
|
292
|
+
throw new Error(
|
|
293
|
+
`heapAdapter: RO has ${ro.textures.count} texture(s) and ${ro.samplers.count} sampler(s); ` +
|
|
294
|
+
`single-pair only in v1 (classifier should have caught this)`,
|
|
295
|
+
);
|
|
296
|
+
}
|
|
297
|
+
|
|
298
|
+
// 4. DrawCall: read once. Classifier validates fields are heap-
|
|
299
|
+
// compatible (indexed, instanceCount=1, zero offsets).
|
|
300
|
+
const dc = ro.drawCall.getValue(token);
|
|
301
|
+
if (dc.kind !== "indexed") {
|
|
302
|
+
throw new Error("heapAdapter: non-indexed drawCall; classifier should have caught this");
|
|
303
|
+
}
|
|
304
|
+
|
|
305
|
+
return {
|
|
306
|
+
effect: ro.effect,
|
|
307
|
+
pipelineState: ro.pipelineState,
|
|
308
|
+
inputs,
|
|
309
|
+
...(instanceAttributes !== undefined ? { instanceAttributes } : {}),
|
|
310
|
+
...(dc.instanceCount > 1 ? { instanceCount: dc.instanceCount } : {}),
|
|
311
|
+
indices,
|
|
312
|
+
...(textures !== undefined ? { textures } : {}),
|
|
313
|
+
};
|
|
314
|
+
}
|