@aardworx/wombat.rendering 0.7.2 → 0.8.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/runtime/heapScene.d.ts +61 -0
- package/dist/runtime/heapScene.d.ts.map +1 -0
- package/dist/runtime/heapScene.js +354 -0
- package/dist/runtime/heapScene.js.map +1 -0
- package/dist/runtime/index.d.ts +1 -0
- package/dist/runtime/index.d.ts.map +1 -1
- package/dist/runtime/index.js +1 -0
- package/dist/runtime/index.js.map +1 -1
- package/package.json +1 -1
- package/src/runtime/heapScene.ts +510 -0
- package/src/runtime/index.ts +10 -0
|
@@ -0,0 +1,61 @@
|
|
|
1
|
+
import { Trafo3d, V3d, V4f } from "@aardworx/wombat.base";
|
|
2
|
+
import type { aval } from "@aardworx/wombat.adaptive";
|
|
3
|
+
import type { CanvasAttachment } from "../window/index.js";
|
|
4
|
+
/**
|
|
5
|
+
* Geometry triple. Tightly-packed Float32 positions / normals (3
|
|
6
|
+
* floats per vertex) plus Uint32 indices. The heap-scene packer
|
|
7
|
+
* concatenates all draws' geometry into per-group slabs.
|
|
8
|
+
*/
|
|
9
|
+
export interface HeapGeometry {
|
|
10
|
+
readonly positions: Float32Array;
|
|
11
|
+
readonly normals: Float32Array;
|
|
12
|
+
readonly indices: Uint32Array;
|
|
13
|
+
}
|
|
14
|
+
export interface HeapDrawSpec {
|
|
15
|
+
readonly geo: HeapGeometry;
|
|
16
|
+
readonly modelTrafo: aval<Trafo3d> | Trafo3d;
|
|
17
|
+
readonly color: aval<V4f> | V4f;
|
|
18
|
+
/** Group key — selects which pipeline-state group this draw joins. */
|
|
19
|
+
readonly groupKey: string;
|
|
20
|
+
}
|
|
21
|
+
export interface HeapSceneStats {
|
|
22
|
+
readonly groups: number;
|
|
23
|
+
readonly totalDraws: number;
|
|
24
|
+
readonly globalsBytes: number;
|
|
25
|
+
drawBytes: number;
|
|
26
|
+
readonly geometryBytes: number;
|
|
27
|
+
}
|
|
28
|
+
export interface HeapScene {
|
|
29
|
+
/** Render one frame against the given globals. */
|
|
30
|
+
frame(viewProj: Trafo3d, lightLocation: V3d): void;
|
|
31
|
+
readonly stats: HeapSceneStats;
|
|
32
|
+
dispose(): void;
|
|
33
|
+
}
|
|
34
|
+
/**
|
|
35
|
+
* Per-group texture set. Bindless isn't standard WebGPU, so a group
|
|
36
|
+
* with sampled textures gets a distinct bind-group layout and is
|
|
37
|
+
* keyed separately from groups with no textures or different sets.
|
|
38
|
+
*/
|
|
39
|
+
export interface HeapTextureSet {
|
|
40
|
+
readonly texture: GPUTexture;
|
|
41
|
+
readonly textureView?: GPUTextureView;
|
|
42
|
+
readonly sampler: GPUSampler;
|
|
43
|
+
}
|
|
44
|
+
export interface BuildHeapSceneOptions {
|
|
45
|
+
/** WGSL fragment-stage source per group key — must declare `fs(in: VsOut) -> @location(0) vec4<f32>`. */
|
|
46
|
+
readonly fragmentShaders: ReadonlyMap<string, string>;
|
|
47
|
+
/**
|
|
48
|
+
* Per-group texture set. When present, the group's bind-group
|
|
49
|
+
* layout adds a `texture_2d<f32>` at binding 4 and a sampler at
|
|
50
|
+
* binding 5. The user's FS WGSL must declare them.
|
|
51
|
+
*/
|
|
52
|
+
readonly texturesByGroupKey?: ReadonlyMap<string, HeapTextureSet>;
|
|
53
|
+
}
|
|
54
|
+
/**
|
|
55
|
+
* Build a multi-group heap-backed scene renderer. Buckets `draws` by
|
|
56
|
+
* `groupKey`, builds one pipeline + bind group + heap per bucket,
|
|
57
|
+
* subscribes per-RO avals so dirty marks propagate to per-slot
|
|
58
|
+
* `writeBuffer`s on the next `frame()`.
|
|
59
|
+
*/
|
|
60
|
+
export declare function buildHeapScene(device: GPUDevice, attach: CanvasAttachment, draws: readonly HeapDrawSpec[], opts: BuildHeapSceneOptions): HeapScene;
|
|
61
|
+
//# sourceMappingURL=heapScene.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"heapScene.d.ts","sourceRoot":"","s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,CAAC,EAAE,cAAc,CAAC;IACtC,QAAQ,CAAC,OAAO,EAAE,UAAU,CAAC;CAC9B;AAED,MAAM,WAAW,qBAAqB;IACpC,yGAAyG;IACzG,QAAQ,CAAC,eAAe,EAAE,WAAW,CAAC,MAAM,EAAE,MAAM,CAAC,CAAC;IACtD;;;;OAIG;IACH,QAAQ,CAAC,kBAAkB,CAAC,EAAE,WAAW,CAAC,MAAM,EAAE,cAAc,CAAC,CAAC;CACnE;AAED;;;;;GAKG;AACH,wBAAgB,cAAc,CAC5B,MAAM,EAAE,SAAS,EACjB,MAAM,EAAE,gBAAgB,EACxB,KAAK,EAAE,SAAS,YAAY,EAAE,EAC9B,IAAI,EAAE,qBAAqB,GAC1B,SAAS,CA6GX"}
|
|
@@ -0,0 +1,354 @@
|
|
|
1
|
+
// heapScene — multi-group heap-everything render path.
|
|
2
|
+
//
|
|
3
|
+
// Public-API extraction of the experimental heap-demo (commits in
|
|
4
|
+
// the `experimental/heap-arch` branch document the design). One
|
|
5
|
+
// shared GPURenderPipeline + GPUBindGroup per pipeline-state group,
|
|
6
|
+
// per-draw uniforms in a storage buffer, vertex pulling from packed
|
|
7
|
+
// position / normal slabs, indexed draws via a shared index buffer
|
|
8
|
+
// and `firstInstance` routing into the heap.
|
|
9
|
+
//
|
|
10
|
+
// Per-frame uploads:
|
|
11
|
+
// - Globals (one UBO per group): viewProj + lightLocation. ~80 B.
|
|
12
|
+
// Independent of draw count.
|
|
13
|
+
// - DrawHeader (one storage buffer per group): only slots whose
|
|
14
|
+
// `aval<Trafo3d>` or `aval<V4f>` were marked since last frame
|
|
15
|
+
// are re-uploaded — 160 B per dirty slot.
|
|
16
|
+
//
|
|
17
|
+
// Geometry is uploaded ONCE at construction (positions + normals +
|
|
18
|
+
// indices, packed across all draws in a group).
|
|
19
|
+
//
|
|
20
|
+
// The user supplies a fragment-shader body per group key. The vertex
|
|
21
|
+
// stage is provided by this module and reads from a fixed layout:
|
|
22
|
+
//
|
|
23
|
+
// @group(0) @binding(0) var<uniform> globals: Globals;
|
|
24
|
+
// @group(0) @binding(1) var<storage, read> draws: array<DrawHeader>;
|
|
25
|
+
// @group(0) @binding(2) var<storage, read> positions: array<f32>;
|
|
26
|
+
// @group(0) @binding(3) var<storage, read> normals: array<f32>;
|
|
27
|
+
//
|
|
28
|
+
// struct VsOut {
|
|
29
|
+
// @builtin(position) clipPos: vec4<f32>,
|
|
30
|
+
// @location(0) worldPos: vec3<f32>,
|
|
31
|
+
// @location(1) normal: vec3<f32>,
|
|
32
|
+
// @location(2) color: vec4<f32>,
|
|
33
|
+
// @location(3) lightLoc: vec3<f32>,
|
|
34
|
+
// };
|
|
35
|
+
//
|
|
36
|
+
// User WGSL must declare `@fragment fn fs(in: VsOut) -> @location(0) vec4<f32>`.
|
|
37
|
+
//
|
|
38
|
+
// Per-group texture sets. WebGPU 1.0 has no bindless, so texture-set
|
|
39
|
+
// is a wedge in the group key: groups with different texture sets
|
|
40
|
+
// can't share a bind group. Pass a `textures: { texture, sampler }`
|
|
41
|
+
// per groupKey via `BuildHeapSceneOptions.texturesByGroupKey`. The
|
|
42
|
+
// package adds the texture (binding 4) + sampler (binding 5) to that
|
|
43
|
+
// group's bind-group layout; the user's FS WGSL declares them.
|
|
44
|
+
import { Trafo3d, V3d, V4f } from "@aardworx/wombat.base";
|
|
45
|
+
import { AVal, addMarkingCallback } from "@aardworx/wombat.adaptive";
|
|
46
|
+
// ---------------------------------------------------------------------------
|
|
47
|
+
// Layouts
|
|
48
|
+
// ---------------------------------------------------------------------------
|
|
49
|
+
const GLOBALS_BYTES = 64 + 16; // mat4 + vec4
|
|
50
|
+
const GLOBALS_FLOATS = GLOBALS_BYTES / 4;
|
|
51
|
+
const DRAW_HEADER_BYTES = 64 * 2 + 16 + 16; // 2 mat4 + vec4 + (u32 + 3*pad)
|
|
52
|
+
const DRAW_HEADER_FLOATS = DRAW_HEADER_BYTES / 4;
|
|
53
|
+
function packMat44(m, dst, off) {
|
|
54
|
+
// M44d._data is the row-major Float64Array; not part of the public
|
|
55
|
+
// type surface. toArray() round-trips through a fresh number[] which
|
|
56
|
+
// we copy into the f32 staging buffer below.
|
|
57
|
+
const r = m.toArray();
|
|
58
|
+
for (let i = 0; i < 16; i++)
|
|
59
|
+
dst[off + i] = r[i];
|
|
60
|
+
}
|
|
61
|
+
function packGlobals(viewProj, lightLocation, dst) {
|
|
62
|
+
packMat44(viewProj.forward, dst, 0);
|
|
63
|
+
dst[16] = lightLocation.x;
|
|
64
|
+
dst[17] = lightLocation.y;
|
|
65
|
+
dst[18] = lightLocation.z;
|
|
66
|
+
dst[19] = 0;
|
|
67
|
+
}
|
|
68
|
+
function packDrawHeader(modelTrafo, color, vertexBase, dst, drawIndex) {
|
|
69
|
+
const off = drawIndex * DRAW_HEADER_FLOATS;
|
|
70
|
+
packMat44(modelTrafo.forward, dst, off + 0);
|
|
71
|
+
packMat44(modelTrafo.backward, dst, off + 16);
|
|
72
|
+
dst[off + 32] = color.x;
|
|
73
|
+
dst[off + 33] = color.y;
|
|
74
|
+
dst[off + 34] = color.z;
|
|
75
|
+
dst[off + 35] = color.w;
|
|
76
|
+
new Uint32Array(dst.buffer, dst.byteOffset, dst.length)[off + 36] = vertexBase;
|
|
77
|
+
}
|
|
78
|
+
// ---------------------------------------------------------------------------
|
|
79
|
+
// Shared WGSL prelude (struct + bindings + VS)
|
|
80
|
+
// ---------------------------------------------------------------------------
|
|
81
|
+
const SHADER_PRELUDE = /* wgsl */ `
|
|
82
|
+
struct Globals {
|
|
83
|
+
viewProj: mat4x4<f32>,
|
|
84
|
+
lightLocation: vec4<f32>,
|
|
85
|
+
};
|
|
86
|
+
struct DrawHeader {
|
|
87
|
+
modelTrafo: mat4x4<f32>,
|
|
88
|
+
modelTrafoInv: mat4x4<f32>,
|
|
89
|
+
color: vec4<f32>,
|
|
90
|
+
vertexBase: u32,
|
|
91
|
+
_pad0: u32, _pad1: u32, _pad2: u32,
|
|
92
|
+
};
|
|
93
|
+
@group(0) @binding(0) var<uniform> globals: Globals;
|
|
94
|
+
@group(0) @binding(1) var<storage, read> draws: array<DrawHeader>;
|
|
95
|
+
@group(0) @binding(2) var<storage, read> positions: array<f32>;
|
|
96
|
+
@group(0) @binding(3) var<storage, read> normals: array<f32>;
|
|
97
|
+
|
|
98
|
+
struct VsOut {
|
|
99
|
+
@builtin(position) clipPos: vec4<f32>,
|
|
100
|
+
@location(0) worldPos: vec3<f32>,
|
|
101
|
+
@location(1) normal: vec3<f32>,
|
|
102
|
+
@location(2) color: vec4<f32>,
|
|
103
|
+
@location(3) lightLoc: vec3<f32>,
|
|
104
|
+
};
|
|
105
|
+
|
|
106
|
+
fn fetchVec3(buf: ptr<storage, array<f32>, read>, base: u32) -> vec3<f32> {
|
|
107
|
+
return vec3<f32>((*buf)[base + 0u], (*buf)[base + 1u], (*buf)[base + 2u]);
|
|
108
|
+
}
|
|
109
|
+
|
|
110
|
+
@vertex
|
|
111
|
+
fn vs(@builtin(vertex_index) vid: u32, @builtin(instance_index) drawIdx: u32) -> VsOut {
|
|
112
|
+
let d = draws[drawIdx];
|
|
113
|
+
let base = (d.vertexBase + vid) * 3u;
|
|
114
|
+
let pos = fetchVec3(&positions, base);
|
|
115
|
+
let nor = fetchVec3(&normals, base);
|
|
116
|
+
// wombat.shader convention: matrices uploaded row-major; WGSL reads
|
|
117
|
+
// them as col-major = transposed. v * M is the row-vec dual of
|
|
118
|
+
// M.mul(v) (= M*v math).
|
|
119
|
+
let wp = vec4<f32>(pos, 1.0) * d.modelTrafo;
|
|
120
|
+
let n = (vec4<f32>(nor, 0.0) * d.modelTrafo).xyz;
|
|
121
|
+
var out: VsOut;
|
|
122
|
+
out.clipPos = wp * globals.viewProj;
|
|
123
|
+
out.worldPos = wp.xyz;
|
|
124
|
+
out.normal = n;
|
|
125
|
+
out.color = d.color;
|
|
126
|
+
out.lightLoc = globals.lightLocation.xyz;
|
|
127
|
+
return out;
|
|
128
|
+
}
|
|
129
|
+
`;
|
|
130
|
+
function packGeometry(device, geos, label) {
|
|
131
|
+
let totalVerts = 0, totalIndices = 0;
|
|
132
|
+
for (const g of geos) {
|
|
133
|
+
totalVerts += g.positions.length / 3;
|
|
134
|
+
totalIndices += g.indices.length;
|
|
135
|
+
}
|
|
136
|
+
const positions = new Float32Array(totalVerts * 3);
|
|
137
|
+
const normals = new Float32Array(totalVerts * 3);
|
|
138
|
+
const indices = new Uint32Array(totalIndices);
|
|
139
|
+
const entries = [];
|
|
140
|
+
let vOff = 0, iOff = 0;
|
|
141
|
+
for (const g of geos) {
|
|
142
|
+
const vCount = g.positions.length / 3;
|
|
143
|
+
positions.set(g.positions, vOff * 3);
|
|
144
|
+
normals.set(g.normals, vOff * 3);
|
|
145
|
+
indices.set(g.indices, iOff);
|
|
146
|
+
entries.push({ vertexBase: vOff, indexCount: g.indices.length, firstIndex: iOff });
|
|
147
|
+
vOff += vCount;
|
|
148
|
+
iOff += g.indices.length;
|
|
149
|
+
}
|
|
150
|
+
const mk = (data, usage, lbl) => {
|
|
151
|
+
const buf = device.createBuffer({
|
|
152
|
+
size: alignUp(data.byteLength, 4),
|
|
153
|
+
usage: usage | GPUBufferUsage.COPY_DST,
|
|
154
|
+
label: lbl,
|
|
155
|
+
});
|
|
156
|
+
device.queue.writeBuffer(buf, 0, data.buffer, data.byteOffset, data.byteLength);
|
|
157
|
+
return buf;
|
|
158
|
+
};
|
|
159
|
+
return {
|
|
160
|
+
positionsBuf: mk(positions, GPUBufferUsage.STORAGE, `${label}/pos`),
|
|
161
|
+
normalsBuf: mk(normals, GPUBufferUsage.STORAGE, `${label}/nor`),
|
|
162
|
+
indexBuf: mk(indices, GPUBufferUsage.INDEX, `${label}/idx`),
|
|
163
|
+
entries,
|
|
164
|
+
bytes: positions.byteLength + normals.byteLength + indices.byteLength,
|
|
165
|
+
};
|
|
166
|
+
}
|
|
167
|
+
function alignUp(n, a) { return (n + a - 1) & ~(a - 1); }
|
|
168
|
+
function asAval(v) {
|
|
169
|
+
return (typeof v === "object" && v !== null && typeof v.getValue === "function")
|
|
170
|
+
? v
|
|
171
|
+
: AVal.constant(v);
|
|
172
|
+
}
|
|
173
|
+
function buildGroup(device, attach, key, fragmentWgsl, textureSet, draws, modelTrafos, colors) {
|
|
174
|
+
const sig = attach.signature;
|
|
175
|
+
const colorAttachmentName = sig.colorNames[0];
|
|
176
|
+
if (colorAttachmentName === undefined) {
|
|
177
|
+
throw new Error("buildHeapScene: framebuffer signature has no color attachment");
|
|
178
|
+
}
|
|
179
|
+
const colorFormat = sig.colors.tryFind(colorAttachmentName);
|
|
180
|
+
const depthFormat = sig.depthStencil?.format;
|
|
181
|
+
const memberIndices = draws.map(d => d.sourceIndex);
|
|
182
|
+
const myTrafos = memberIndices.map(i => modelTrafos[i]);
|
|
183
|
+
const myColors = memberIndices.map(i => colors[i]);
|
|
184
|
+
const packed = packGeometry(device, draws.map(d => d.spec.geo), `heapScene/${key}`);
|
|
185
|
+
const globals = device.createBuffer({
|
|
186
|
+
size: alignUp(GLOBALS_BYTES, 16),
|
|
187
|
+
usage: GPUBufferUsage.UNIFORM | GPUBufferUsage.COPY_DST,
|
|
188
|
+
label: `heapScene/${key}/globals`,
|
|
189
|
+
});
|
|
190
|
+
const globalsStaging = new Float32Array(GLOBALS_FLOATS);
|
|
191
|
+
const heapBytes = DRAW_HEADER_BYTES * draws.length;
|
|
192
|
+
const drawHeap = device.createBuffer({
|
|
193
|
+
size: alignUp(heapBytes, 16),
|
|
194
|
+
usage: GPUBufferUsage.STORAGE | GPUBufferUsage.COPY_DST,
|
|
195
|
+
label: `heapScene/${key}/draws`,
|
|
196
|
+
});
|
|
197
|
+
const drawStaging = new Float32Array(DRAW_HEADER_FLOATS * draws.length);
|
|
198
|
+
for (let i = 0; i < draws.length; i++) {
|
|
199
|
+
packDrawHeader(myTrafos[i].force( /* allow-force */), myColors[i].force( /* allow-force */), packed.entries[i].vertexBase, drawStaging, i);
|
|
200
|
+
}
|
|
201
|
+
device.queue.writeBuffer(drawHeap, 0, drawStaging.buffer, drawStaging.byteOffset, heapBytes);
|
|
202
|
+
const dirty = new Set();
|
|
203
|
+
const subs = [];
|
|
204
|
+
for (let i = 0; i < draws.length; i++) {
|
|
205
|
+
subs.push(addMarkingCallback(myTrafos[i], () => dirty.add(i)));
|
|
206
|
+
subs.push(addMarkingCallback(myColors[i], () => dirty.add(i)));
|
|
207
|
+
}
|
|
208
|
+
const module = device.createShaderModule({
|
|
209
|
+
code: SHADER_PRELUDE + fragmentWgsl,
|
|
210
|
+
label: `heapScene/${key}/shader`,
|
|
211
|
+
});
|
|
212
|
+
const bindLayoutEntries = [
|
|
213
|
+
{ binding: 0, visibility: GPUShaderStage.VERTEX, buffer: { type: "uniform" } },
|
|
214
|
+
{ binding: 1, visibility: GPUShaderStage.VERTEX, buffer: { type: "read-only-storage" } },
|
|
215
|
+
{ binding: 2, visibility: GPUShaderStage.VERTEX, buffer: { type: "read-only-storage" } },
|
|
216
|
+
{ binding: 3, visibility: GPUShaderStage.VERTEX, buffer: { type: "read-only-storage" } },
|
|
217
|
+
];
|
|
218
|
+
if (textureSet !== undefined) {
|
|
219
|
+
bindLayoutEntries.push({ binding: 4, visibility: GPUShaderStage.FRAGMENT, texture: { sampleType: "float" } }, { binding: 5, visibility: GPUShaderStage.FRAGMENT, sampler: { type: "filtering" } });
|
|
220
|
+
}
|
|
221
|
+
const bindGroupLayout = device.createBindGroupLayout({
|
|
222
|
+
label: `heapScene/${key}/bgl`,
|
|
223
|
+
entries: bindLayoutEntries,
|
|
224
|
+
});
|
|
225
|
+
const pipelineLayout = device.createPipelineLayout({ bindGroupLayouts: [bindGroupLayout] });
|
|
226
|
+
const pipeline = device.createRenderPipeline({
|
|
227
|
+
label: `heapScene/${key}/pipeline`,
|
|
228
|
+
layout: pipelineLayout,
|
|
229
|
+
vertex: { module, entryPoint: "vs", buffers: [] },
|
|
230
|
+
fragment: { module, entryPoint: "fs", targets: [{ format: colorFormat }] },
|
|
231
|
+
primitive: { topology: "triangle-list", cullMode: "back", frontFace: "ccw" },
|
|
232
|
+
...(depthFormat !== undefined
|
|
233
|
+
? { depthStencil: { format: depthFormat, depthWriteEnabled: true, depthCompare: "less" } }
|
|
234
|
+
: {}),
|
|
235
|
+
});
|
|
236
|
+
const bindEntries = [
|
|
237
|
+
{ binding: 0, resource: { buffer: globals } },
|
|
238
|
+
{ binding: 1, resource: { buffer: drawHeap } },
|
|
239
|
+
{ binding: 2, resource: { buffer: packed.positionsBuf } },
|
|
240
|
+
{ binding: 3, resource: { buffer: packed.normalsBuf } },
|
|
241
|
+
];
|
|
242
|
+
if (textureSet !== undefined) {
|
|
243
|
+
bindEntries.push({ binding: 4, resource: textureSet.textureView ?? textureSet.texture.createView() }, { binding: 5, resource: textureSet.sampler });
|
|
244
|
+
}
|
|
245
|
+
const bindGroup = device.createBindGroup({
|
|
246
|
+
label: `heapScene/${key}/bg`,
|
|
247
|
+
layout: bindGroupLayout,
|
|
248
|
+
entries: bindEntries,
|
|
249
|
+
});
|
|
250
|
+
return {
|
|
251
|
+
key, pipeline, bindGroup, globals, globalsStaging, drawHeap, drawStaging,
|
|
252
|
+
packed, trafos: myTrafos, colors: myColors, dirty, subs,
|
|
253
|
+
};
|
|
254
|
+
}
|
|
255
|
+
/**
|
|
256
|
+
* Build a multi-group heap-backed scene renderer. Buckets `draws` by
|
|
257
|
+
* `groupKey`, builds one pipeline + bind group + heap per bucket,
|
|
258
|
+
* subscribes per-RO avals so dirty marks propagate to per-slot
|
|
259
|
+
* `writeBuffer`s on the next `frame()`.
|
|
260
|
+
*/
|
|
261
|
+
export function buildHeapScene(device, attach, draws, opts) {
|
|
262
|
+
const modelTrafos = draws.map(d => asAval(d.modelTrafo));
|
|
263
|
+
const colors = draws.map(d => asAval(d.color));
|
|
264
|
+
// Bucket by group key.
|
|
265
|
+
const buckets = new Map();
|
|
266
|
+
for (let i = 0; i < draws.length; i++) {
|
|
267
|
+
const d = draws[i];
|
|
268
|
+
let bucket = buckets.get(d.groupKey);
|
|
269
|
+
if (bucket === undefined) {
|
|
270
|
+
bucket = [];
|
|
271
|
+
buckets.set(d.groupKey, bucket);
|
|
272
|
+
}
|
|
273
|
+
bucket.push({ spec: d, sourceIndex: i });
|
|
274
|
+
}
|
|
275
|
+
const groups = [];
|
|
276
|
+
for (const [key, members] of buckets) {
|
|
277
|
+
const fs = opts.fragmentShaders.get(key);
|
|
278
|
+
if (fs === undefined) {
|
|
279
|
+
throw new Error(`buildHeapScene: no fragment shader supplied for groupKey '${key}'`);
|
|
280
|
+
}
|
|
281
|
+
const textureSet = opts.texturesByGroupKey?.get(key);
|
|
282
|
+
groups.push(buildGroup(device, attach, key, fs, textureSet, members, modelTrafos, colors));
|
|
283
|
+
}
|
|
284
|
+
const stats = {
|
|
285
|
+
groups: groups.length,
|
|
286
|
+
totalDraws: draws.length,
|
|
287
|
+
globalsBytes: GLOBALS_BYTES * groups.length,
|
|
288
|
+
drawBytes: 0,
|
|
289
|
+
geometryBytes: groups.reduce((acc, g) => acc + g.packed.bytes, 0),
|
|
290
|
+
};
|
|
291
|
+
function frame(viewProj, lightLocation) {
|
|
292
|
+
const fb = attach.framebuffer.force( /* allow-force */);
|
|
293
|
+
const colorAttachmentName = attach.signature.colorNames[0];
|
|
294
|
+
const colorView = fb.colors.tryFind(colorAttachmentName);
|
|
295
|
+
const depthFormat = attach.signature.depthStencil?.format;
|
|
296
|
+
const depthView = fb.depthStencil;
|
|
297
|
+
let totalDirtyBytes = 0;
|
|
298
|
+
const enc = device.createCommandEncoder({ label: "heapScene: encoder" });
|
|
299
|
+
let firstPass = true;
|
|
300
|
+
for (const g of groups) {
|
|
301
|
+
packGlobals(viewProj, lightLocation, g.globalsStaging);
|
|
302
|
+
device.queue.writeBuffer(g.globals, 0, g.globalsStaging.buffer, g.globalsStaging.byteOffset, GLOBALS_BYTES);
|
|
303
|
+
if (g.dirty.size > 0) {
|
|
304
|
+
for (const i of g.dirty) {
|
|
305
|
+
packDrawHeader(g.trafos[i].force( /* allow-force */), g.colors[i].force( /* allow-force */), g.packed.entries[i].vertexBase, g.drawStaging, i);
|
|
306
|
+
const byteOff = i * DRAW_HEADER_BYTES;
|
|
307
|
+
device.queue.writeBuffer(g.drawHeap, byteOff, g.drawStaging.buffer, g.drawStaging.byteOffset + byteOff, DRAW_HEADER_BYTES);
|
|
308
|
+
totalDirtyBytes += DRAW_HEADER_BYTES;
|
|
309
|
+
}
|
|
310
|
+
g.dirty.clear();
|
|
311
|
+
}
|
|
312
|
+
const pass = enc.beginRenderPass({
|
|
313
|
+
colorAttachments: [{
|
|
314
|
+
view: colorView,
|
|
315
|
+
clearValue: { r: 0.07, g: 0.07, b: 0.08, a: 1.0 },
|
|
316
|
+
loadOp: firstPass ? "clear" : "load",
|
|
317
|
+
storeOp: "store",
|
|
318
|
+
}],
|
|
319
|
+
...(depthView !== undefined && depthFormat !== undefined ? {
|
|
320
|
+
depthStencilAttachment: {
|
|
321
|
+
view: depthView,
|
|
322
|
+
depthClearValue: 1.0,
|
|
323
|
+
depthLoadOp: firstPass ? "clear" : "load",
|
|
324
|
+
depthStoreOp: "store",
|
|
325
|
+
},
|
|
326
|
+
} : {}),
|
|
327
|
+
});
|
|
328
|
+
pass.setPipeline(g.pipeline);
|
|
329
|
+
pass.setBindGroup(0, g.bindGroup);
|
|
330
|
+
pass.setIndexBuffer(g.packed.indexBuf, "uint32");
|
|
331
|
+
for (let i = 0; i < g.packed.entries.length; i++) {
|
|
332
|
+
const e = g.packed.entries[i];
|
|
333
|
+
pass.drawIndexed(e.indexCount, 1, e.firstIndex, 0, i);
|
|
334
|
+
}
|
|
335
|
+
pass.end();
|
|
336
|
+
firstPass = false;
|
|
337
|
+
}
|
|
338
|
+
stats.drawBytes = totalDirtyBytes;
|
|
339
|
+
device.queue.submit([enc.finish()]);
|
|
340
|
+
}
|
|
341
|
+
function dispose() {
|
|
342
|
+
for (const g of groups) {
|
|
343
|
+
for (const s of g.subs)
|
|
344
|
+
s.dispose();
|
|
345
|
+
g.globals.destroy();
|
|
346
|
+
g.drawHeap.destroy();
|
|
347
|
+
g.packed.positionsBuf.destroy();
|
|
348
|
+
g.packed.normalsBuf.destroy();
|
|
349
|
+
g.packed.indexBuf.destroy();
|
|
350
|
+
}
|
|
351
|
+
}
|
|
352
|
+
return { frame, stats, dispose };
|
|
353
|
+
}
|
|
354
|
+
//# sourceMappingURL=heapScene.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"heapScene.js","sourceRoot":"","sources":["../../src/runtime/heapScene.ts"],"names":[],"mappings":"AAAA,uDAAuD;AACvD,EAAE;AACF,kEAAkE;AAClE,gEAAgE;AAChE,oEAAoE;AACpE,oEAAoE;AACpE,mEAAmE;AACnE,6CAA6C;AAC7C,EAAE;AACF,qBAAqB;AACrB,oEAAoE;AACpE,iCAAiC;AACjC,kEAAkE;AAClE,kEAAkE;AAClE,8CAA8C;AAC9C,EAAE;AACF,mEAAmE;AACnE,gDAAgD;AAChD,EAAE;AACF,qEAAqE;AACrE,kEAAkE;AAClE,EAAE;AACF,uEAAuE;AACvE,iFAAiF;AACjF,0EAA0E;AAC1E,0EAA0E;AAC1E,EAAE;AACF,mBAAmB;AACnB,8CAA8C;AAC9C,8CAA8C;AAC9C,8CAA8C;AAC9C,8CAA8C;AAC9C,8CAA8C;AAC9C,OAAO;AACP,EAAE;AACF,iFAAiF;AACjF,EAAE;AACF,qEAAqE;AACrE,kEAAkE;AAClE,oEAAoE;AACpE,mEAAmE;AACnE,qEAAqE;AACrE,+DAA+D;AAE/D,OAAO,EAAE,OAAO,EAAE,GAAG,EAAE,GAAG,EAAa,MAAM,uBAAuB,CAAC;AACrE,OAAO,EAAE,IAAI,EAAE,kBAAkB,EAAE,MAAM,2BAA2B,CAAC;AAIrE,8EAA8E;AAC9E,UAAU;AACV,8EAA8E;AAE9E,MAAM,aAAa,GAAG,EAAE,GAAG,EAAE,CAAC,CAAoC,cAAc;AAChF,MAAM,cAAc,GAAG,aAAa,GAAG,CAAC,CAAC;AACzC,MAAM,iBAAiB,G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ACrB,CAAC,CAAC,MAAM,CAAC,YAAY,CAAC,OAAO,EAAE,CAAC;YAChC,CAAC,CAAC,MAAM,CAAC,UAAU,CAAC,OAAO,EAAE,CAAC;YAC9B,CAAC,CAAC,MAAM,CAAC,QAAQ,CAAC,OAAO,EAAE,CAAC;QAC9B,CAAC;IACH,CAAC;IAED,OAAO,EAAE,KAAK,EAAE,KAAK,EAAE,OAAO,EAAE,CAAC;AACnC,CAAC"}
|
package/dist/runtime/index.d.ts
CHANGED
|
@@ -3,4 +3,5 @@ export { compileRenderTask, type RuntimeContext, } from "./renderTask.js";
|
|
|
3
3
|
export { copy } from "./copy.js";
|
|
4
4
|
export { renderTo, type RenderToOptions, type RenderToResult, } from "./renderTo.js";
|
|
5
5
|
export { ScenePass, type WalkerStats, } from "./scenePass.js";
|
|
6
|
+
export { buildHeapScene, type HeapScene, type HeapSceneStats, type HeapDrawSpec, type HeapGeometry, type HeapTextureSet, type BuildHeapSceneOptions, } from "./heapScene.js";
|
|
6
7
|
//# sourceMappingURL=index.d.ts.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../src/runtime/index.ts"],"names":[],"mappings":"AAEA,OAAO,EACL,OAAO,EACP,KAAK,cAAc,GACpB,MAAM,cAAc,CAAC;AAEtB,OAAO,EACL,iBAAiB,EACjB,KAAK,cAAc,GACpB,MAAM,iBAAiB,CAAC;AAEzB,OAAO,EAAE,IAAI,EAAE,MAAM,WAAW,CAAC;AAEjC,OAAO,EACL,QAAQ,EACR,KAAK,eAAe,EACpB,KAAK,cAAc,GACpB,MAAM,eAAe,CAAC;AAEvB,OAAO,EACL,SAAS,EACT,KAAK,WAAW,GACjB,MAAM,gBAAgB,CAAC"}
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../src/runtime/index.ts"],"names":[],"mappings":"AAEA,OAAO,EACL,OAAO,EACP,KAAK,cAAc,GACpB,MAAM,cAAc,CAAC;AAEtB,OAAO,EACL,iBAAiB,EACjB,KAAK,cAAc,GACpB,MAAM,iBAAiB,CAAC;AAEzB,OAAO,EAAE,IAAI,EAAE,MAAM,WAAW,CAAC;AAEjC,OAAO,EACL,QAAQ,EACR,KAAK,eAAe,EACpB,KAAK,cAAc,GACpB,MAAM,eAAe,CAAC;AAEvB,OAAO,EACL,SAAS,EACT,KAAK,WAAW,GACjB,MAAM,gBAAgB,CAAC;AAExB,OAAO,EACL,cAAc,EACd,KAAK,SAAS,EACd,KAAK,cAAc,EACnB,KAAK,YAAY,EACjB,KAAK,YAAY,EACjB,KAAK,cAAc,EACnB,KAAK,qBAAqB,GAC3B,MAAM,gBAAgB,CAAC"}
|
package/dist/runtime/index.js
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.js","sourceRoot":"","sources":["../../src/runtime/index.ts"],"names":[],"mappings":"AAAA,oDAAoD;AAEpD,OAAO,EACL,OAAO,GAER,MAAM,cAAc,CAAC;AAEtB,OAAO,EACL,iBAAiB,GAElB,MAAM,iBAAiB,CAAC;AAEzB,OAAO,EAAE,IAAI,EAAE,MAAM,WAAW,CAAC;AAEjC,OAAO,EACL,QAAQ,GAGT,MAAM,eAAe,CAAC;AAEvB,OAAO,EACL,SAAS,GAEV,MAAM,gBAAgB,CAAC"}
|
|
1
|
+
{"version":3,"file":"index.js","sourceRoot":"","sources":["../../src/runtime/index.ts"],"names":[],"mappings":"AAAA,oDAAoD;AAEpD,OAAO,EACL,OAAO,GAER,MAAM,cAAc,CAAC;AAEtB,OAAO,EACL,iBAAiB,GAElB,MAAM,iBAAiB,CAAC;AAEzB,OAAO,EAAE,IAAI,EAAE,MAAM,WAAW,CAAC;AAEjC,OAAO,EACL,QAAQ,GAGT,MAAM,eAAe,CAAC;AAEvB,OAAO,EACL,SAAS,GAEV,MAAM,gBAAgB,CAAC;AAExB,OAAO,EACL,cAAc,GAOf,MAAM,gBAAgB,CAAC"}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@aardworx/wombat.rendering",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.8.0",
|
|
4
4
|
"description": "WebGPU rendering layer for the Wombat TypeScript stack \u2014 RenderObject/RenderTask/runtime + window glue, port of Aardvark.Rendering's lower layers on top of WebGPU and wombat.shader.",
|
|
5
5
|
"license": "MIT",
|
|
6
6
|
"author": "krauthaufen",
|
|
@@ -0,0 +1,510 @@
|
|
|
1
|
+
// heapScene — multi-group heap-everything render path.
|
|
2
|
+
//
|
|
3
|
+
// Public-API extraction of the experimental heap-demo (commits in
|
|
4
|
+
// the `experimental/heap-arch` branch document the design). One
|
|
5
|
+
// shared GPURenderPipeline + GPUBindGroup per pipeline-state group,
|
|
6
|
+
// per-draw uniforms in a storage buffer, vertex pulling from packed
|
|
7
|
+
// position / normal slabs, indexed draws via a shared index buffer
|
|
8
|
+
// and `firstInstance` routing into the heap.
|
|
9
|
+
//
|
|
10
|
+
// Per-frame uploads:
|
|
11
|
+
// - Globals (one UBO per group): viewProj + lightLocation. ~80 B.
|
|
12
|
+
// Independent of draw count.
|
|
13
|
+
// - DrawHeader (one storage buffer per group): only slots whose
|
|
14
|
+
// `aval<Trafo3d>` or `aval<V4f>` were marked since last frame
|
|
15
|
+
// are re-uploaded — 160 B per dirty slot.
|
|
16
|
+
//
|
|
17
|
+
// Geometry is uploaded ONCE at construction (positions + normals +
|
|
18
|
+
// indices, packed across all draws in a group).
|
|
19
|
+
//
|
|
20
|
+
// The user supplies a fragment-shader body per group key. The vertex
|
|
21
|
+
// stage is provided by this module and reads from a fixed layout:
|
|
22
|
+
//
|
|
23
|
+
// @group(0) @binding(0) var<uniform> globals: Globals;
|
|
24
|
+
// @group(0) @binding(1) var<storage, read> draws: array<DrawHeader>;
|
|
25
|
+
// @group(0) @binding(2) var<storage, read> positions: array<f32>;
|
|
26
|
+
// @group(0) @binding(3) var<storage, read> normals: array<f32>;
|
|
27
|
+
//
|
|
28
|
+
// struct VsOut {
|
|
29
|
+
// @builtin(position) clipPos: vec4<f32>,
|
|
30
|
+
// @location(0) worldPos: vec3<f32>,
|
|
31
|
+
// @location(1) normal: vec3<f32>,
|
|
32
|
+
// @location(2) color: vec4<f32>,
|
|
33
|
+
// @location(3) lightLoc: vec3<f32>,
|
|
34
|
+
// };
|
|
35
|
+
//
|
|
36
|
+
// User WGSL must declare `@fragment fn fs(in: VsOut) -> @location(0) vec4<f32>`.
|
|
37
|
+
//
|
|
38
|
+
// Per-group texture sets. WebGPU 1.0 has no bindless, so texture-set
|
|
39
|
+
// is a wedge in the group key: groups with different texture sets
|
|
40
|
+
// can't share a bind group. Pass a `textures: { texture, sampler }`
|
|
41
|
+
// per groupKey via `BuildHeapSceneOptions.texturesByGroupKey`. The
|
|
42
|
+
// package adds the texture (binding 4) + sampler (binding 5) to that
|
|
43
|
+
// group's bind-group layout; the user's FS WGSL declares them.
|
|
44
|
+
|
|
45
|
+
import { Trafo3d, V3d, V4f, type M44d } from "@aardworx/wombat.base";
|
|
46
|
+
import { AVal, addMarkingCallback } from "@aardworx/wombat.adaptive";
|
|
47
|
+
import type { aval, IDisposable } from "@aardworx/wombat.adaptive";
|
|
48
|
+
import type { CanvasAttachment } from "../window/index.js";
|
|
49
|
+
|
|
50
|
+
// ---------------------------------------------------------------------------
|
|
51
|
+
// Layouts
|
|
52
|
+
// ---------------------------------------------------------------------------
|
|
53
|
+
|
|
54
|
+
const GLOBALS_BYTES = 64 + 16; // mat4 + vec4
|
|
55
|
+
const GLOBALS_FLOATS = GLOBALS_BYTES / 4;
|
|
56
|
+
const DRAW_HEADER_BYTES = 64 * 2 + 16 + 16; // 2 mat4 + vec4 + (u32 + 3*pad)
|
|
57
|
+
const DRAW_HEADER_FLOATS = DRAW_HEADER_BYTES / 4;
|
|
58
|
+
|
|
59
|
+
function packMat44(m: M44d, dst: Float32Array, off: number): void {
|
|
60
|
+
// M44d._data is the row-major Float64Array; not part of the public
|
|
61
|
+
// type surface. toArray() round-trips through a fresh number[] which
|
|
62
|
+
// we copy into the f32 staging buffer below.
|
|
63
|
+
const r = m.toArray();
|
|
64
|
+
for (let i = 0; i < 16; i++) dst[off + i] = r[i]!;
|
|
65
|
+
}
|
|
66
|
+
|
|
67
|
+
function packGlobals(viewProj: Trafo3d, lightLocation: V3d, dst: Float32Array): void {
|
|
68
|
+
packMat44(viewProj.forward, dst, 0);
|
|
69
|
+
dst[16] = lightLocation.x as number;
|
|
70
|
+
dst[17] = lightLocation.y as number;
|
|
71
|
+
dst[18] = lightLocation.z as number;
|
|
72
|
+
dst[19] = 0;
|
|
73
|
+
}
|
|
74
|
+
|
|
75
|
+
function packDrawHeader(
|
|
76
|
+
modelTrafo: Trafo3d,
|
|
77
|
+
color: V4f,
|
|
78
|
+
vertexBase: number,
|
|
79
|
+
dst: Float32Array,
|
|
80
|
+
drawIndex: number,
|
|
81
|
+
): void {
|
|
82
|
+
const off = drawIndex * DRAW_HEADER_FLOATS;
|
|
83
|
+
packMat44(modelTrafo.forward, dst, off + 0);
|
|
84
|
+
packMat44(modelTrafo.backward, dst, off + 16);
|
|
85
|
+
dst[off + 32] = color.x; dst[off + 33] = color.y;
|
|
86
|
+
dst[off + 34] = color.z; dst[off + 35] = color.w;
|
|
87
|
+
new Uint32Array(dst.buffer, dst.byteOffset, dst.length)[off + 36] = vertexBase;
|
|
88
|
+
}
|
|
89
|
+
|
|
90
|
+
// ---------------------------------------------------------------------------
|
|
91
|
+
// Shared WGSL prelude (struct + bindings + VS)
|
|
92
|
+
// ---------------------------------------------------------------------------
|
|
93
|
+
|
|
94
|
+
const SHADER_PRELUDE = /* wgsl */`
|
|
95
|
+
struct Globals {
|
|
96
|
+
viewProj: mat4x4<f32>,
|
|
97
|
+
lightLocation: vec4<f32>,
|
|
98
|
+
};
|
|
99
|
+
struct DrawHeader {
|
|
100
|
+
modelTrafo: mat4x4<f32>,
|
|
101
|
+
modelTrafoInv: mat4x4<f32>,
|
|
102
|
+
color: vec4<f32>,
|
|
103
|
+
vertexBase: u32,
|
|
104
|
+
_pad0: u32, _pad1: u32, _pad2: u32,
|
|
105
|
+
};
|
|
106
|
+
@group(0) @binding(0) var<uniform> globals: Globals;
|
|
107
|
+
@group(0) @binding(1) var<storage, read> draws: array<DrawHeader>;
|
|
108
|
+
@group(0) @binding(2) var<storage, read> positions: array<f32>;
|
|
109
|
+
@group(0) @binding(3) var<storage, read> normals: array<f32>;
|
|
110
|
+
|
|
111
|
+
struct VsOut {
|
|
112
|
+
@builtin(position) clipPos: vec4<f32>,
|
|
113
|
+
@location(0) worldPos: vec3<f32>,
|
|
114
|
+
@location(1) normal: vec3<f32>,
|
|
115
|
+
@location(2) color: vec4<f32>,
|
|
116
|
+
@location(3) lightLoc: vec3<f32>,
|
|
117
|
+
};
|
|
118
|
+
|
|
119
|
+
fn fetchVec3(buf: ptr<storage, array<f32>, read>, base: u32) -> vec3<f32> {
|
|
120
|
+
return vec3<f32>((*buf)[base + 0u], (*buf)[base + 1u], (*buf)[base + 2u]);
|
|
121
|
+
}
|
|
122
|
+
|
|
123
|
+
@vertex
|
|
124
|
+
fn vs(@builtin(vertex_index) vid: u32, @builtin(instance_index) drawIdx: u32) -> VsOut {
|
|
125
|
+
let d = draws[drawIdx];
|
|
126
|
+
let base = (d.vertexBase + vid) * 3u;
|
|
127
|
+
let pos = fetchVec3(&positions, base);
|
|
128
|
+
let nor = fetchVec3(&normals, base);
|
|
129
|
+
// wombat.shader convention: matrices uploaded row-major; WGSL reads
|
|
130
|
+
// them as col-major = transposed. v * M is the row-vec dual of
|
|
131
|
+
// M.mul(v) (= M*v math).
|
|
132
|
+
let wp = vec4<f32>(pos, 1.0) * d.modelTrafo;
|
|
133
|
+
let n = (vec4<f32>(nor, 0.0) * d.modelTrafo).xyz;
|
|
134
|
+
var out: VsOut;
|
|
135
|
+
out.clipPos = wp * globals.viewProj;
|
|
136
|
+
out.worldPos = wp.xyz;
|
|
137
|
+
out.normal = n;
|
|
138
|
+
out.color = d.color;
|
|
139
|
+
out.lightLoc = globals.lightLocation.xyz;
|
|
140
|
+
return out;
|
|
141
|
+
}
|
|
142
|
+
`;
|
|
143
|
+
|
|
144
|
+
// ---------------------------------------------------------------------------
|
|
145
|
+
// Geometry packing
|
|
146
|
+
// ---------------------------------------------------------------------------
|
|
147
|
+
|
|
148
|
+
interface PackedGeometry {
|
|
149
|
+
readonly positionsBuf: GPUBuffer;
|
|
150
|
+
readonly normalsBuf: GPUBuffer;
|
|
151
|
+
readonly indexBuf: GPUBuffer;
|
|
152
|
+
readonly entries: ReadonlyArray<{ vertexBase: number; indexCount: number; firstIndex: number }>;
|
|
153
|
+
readonly bytes: number;
|
|
154
|
+
}
|
|
155
|
+
|
|
156
|
+
/**
|
|
157
|
+
* Geometry triple. Tightly-packed Float32 positions / normals (3
|
|
158
|
+
* floats per vertex) plus Uint32 indices. The heap-scene packer
|
|
159
|
+
* concatenates all draws' geometry into per-group slabs.
|
|
160
|
+
*/
|
|
161
|
+
export interface HeapGeometry {
|
|
162
|
+
readonly positions: Float32Array;
|
|
163
|
+
readonly normals: Float32Array;
|
|
164
|
+
readonly indices: Uint32Array;
|
|
165
|
+
}
|
|
166
|
+
|
|
167
|
+
function packGeometry(device: GPUDevice, geos: readonly HeapGeometry[], label: string): PackedGeometry {
|
|
168
|
+
let totalVerts = 0, totalIndices = 0;
|
|
169
|
+
for (const g of geos) {
|
|
170
|
+
totalVerts += g.positions.length / 3;
|
|
171
|
+
totalIndices += g.indices.length;
|
|
172
|
+
}
|
|
173
|
+
const positions = new Float32Array(totalVerts * 3);
|
|
174
|
+
const normals = new Float32Array(totalVerts * 3);
|
|
175
|
+
const indices = new Uint32Array(totalIndices);
|
|
176
|
+
const entries: { vertexBase: number; indexCount: number; firstIndex: number }[] = [];
|
|
177
|
+
let vOff = 0, iOff = 0;
|
|
178
|
+
for (const g of geos) {
|
|
179
|
+
const vCount = g.positions.length / 3;
|
|
180
|
+
positions.set(g.positions, vOff * 3);
|
|
181
|
+
normals.set(g.normals, vOff * 3);
|
|
182
|
+
indices.set(g.indices, iOff);
|
|
183
|
+
entries.push({ vertexBase: vOff, indexCount: g.indices.length, firstIndex: iOff });
|
|
184
|
+
vOff += vCount;
|
|
185
|
+
iOff += g.indices.length;
|
|
186
|
+
}
|
|
187
|
+
const mk = (data: ArrayBufferView, usage: GPUBufferUsageFlags, lbl: string): GPUBuffer => {
|
|
188
|
+
const buf = device.createBuffer({
|
|
189
|
+
size: alignUp(data.byteLength, 4),
|
|
190
|
+
usage: usage | GPUBufferUsage.COPY_DST,
|
|
191
|
+
label: lbl,
|
|
192
|
+
});
|
|
193
|
+
device.queue.writeBuffer(buf, 0, data.buffer, data.byteOffset, data.byteLength);
|
|
194
|
+
return buf;
|
|
195
|
+
};
|
|
196
|
+
return {
|
|
197
|
+
positionsBuf: mk(positions, GPUBufferUsage.STORAGE, `${label}/pos`),
|
|
198
|
+
normalsBuf: mk(normals, GPUBufferUsage.STORAGE, `${label}/nor`),
|
|
199
|
+
indexBuf: mk(indices, GPUBufferUsage.INDEX, `${label}/idx`),
|
|
200
|
+
entries,
|
|
201
|
+
bytes: positions.byteLength + normals.byteLength + indices.byteLength,
|
|
202
|
+
};
|
|
203
|
+
}
|
|
204
|
+
|
|
205
|
+
function alignUp(n: number, a: number): number { return (n + a - 1) & ~(a - 1); }
|
|
206
|
+
|
|
207
|
+
function asAval<T>(v: aval<T> | T): aval<T> {
|
|
208
|
+
return (typeof v === "object" && v !== null && typeof (v as { getValue?: unknown }).getValue === "function")
|
|
209
|
+
? (v as aval<T>)
|
|
210
|
+
: AVal.constant(v as T);
|
|
211
|
+
}
|
|
212
|
+
|
|
213
|
+
// ---------------------------------------------------------------------------
|
|
214
|
+
// Per-group internal state
|
|
215
|
+
// ---------------------------------------------------------------------------
|
|
216
|
+
|
|
217
|
+
interface Group {
|
|
218
|
+
readonly key: string;
|
|
219
|
+
readonly pipeline: GPURenderPipeline;
|
|
220
|
+
readonly bindGroup: GPUBindGroup;
|
|
221
|
+
readonly globals: GPUBuffer;
|
|
222
|
+
readonly globalsStaging: Float32Array;
|
|
223
|
+
readonly drawHeap: GPUBuffer;
|
|
224
|
+
readonly drawStaging: Float32Array;
|
|
225
|
+
readonly packed: PackedGeometry;
|
|
226
|
+
readonly trafos: aval<Trafo3d>[];
|
|
227
|
+
readonly colors: aval<V4f>[];
|
|
228
|
+
readonly dirty: Set<number>;
|
|
229
|
+
readonly subs: IDisposable[];
|
|
230
|
+
}
|
|
231
|
+
|
|
232
|
+
function buildGroup(
|
|
233
|
+
device: GPUDevice,
|
|
234
|
+
attach: CanvasAttachment,
|
|
235
|
+
key: string,
|
|
236
|
+
fragmentWgsl: string,
|
|
237
|
+
textureSet: HeapTextureSet | undefined,
|
|
238
|
+
draws: readonly { spec: HeapDrawSpec; sourceIndex: number }[],
|
|
239
|
+
modelTrafos: aval<Trafo3d>[],
|
|
240
|
+
colors: aval<V4f>[],
|
|
241
|
+
): Group {
|
|
242
|
+
const sig = attach.signature;
|
|
243
|
+
const colorAttachmentName = sig.colorNames[0];
|
|
244
|
+
if (colorAttachmentName === undefined) {
|
|
245
|
+
throw new Error("buildHeapScene: framebuffer signature has no color attachment");
|
|
246
|
+
}
|
|
247
|
+
const colorFormat = sig.colors.tryFind(colorAttachmentName)!;
|
|
248
|
+
const depthFormat = sig.depthStencil?.format;
|
|
249
|
+
|
|
250
|
+
const memberIndices = draws.map(d => d.sourceIndex);
|
|
251
|
+
const myTrafos = memberIndices.map(i => modelTrafos[i]!);
|
|
252
|
+
const myColors = memberIndices.map(i => colors[i]!);
|
|
253
|
+
|
|
254
|
+
const packed = packGeometry(device, draws.map(d => d.spec.geo), `heapScene/${key}`);
|
|
255
|
+
|
|
256
|
+
const globals = device.createBuffer({
|
|
257
|
+
size: alignUp(GLOBALS_BYTES, 16),
|
|
258
|
+
usage: GPUBufferUsage.UNIFORM | GPUBufferUsage.COPY_DST,
|
|
259
|
+
label: `heapScene/${key}/globals`,
|
|
260
|
+
});
|
|
261
|
+
const globalsStaging = new Float32Array(GLOBALS_FLOATS);
|
|
262
|
+
|
|
263
|
+
const heapBytes = DRAW_HEADER_BYTES * draws.length;
|
|
264
|
+
const drawHeap = device.createBuffer({
|
|
265
|
+
size: alignUp(heapBytes, 16),
|
|
266
|
+
usage: GPUBufferUsage.STORAGE | GPUBufferUsage.COPY_DST,
|
|
267
|
+
label: `heapScene/${key}/draws`,
|
|
268
|
+
});
|
|
269
|
+
const drawStaging = new Float32Array(DRAW_HEADER_FLOATS * draws.length);
|
|
270
|
+
for (let i = 0; i < draws.length; i++) {
|
|
271
|
+
packDrawHeader(
|
|
272
|
+
myTrafos[i]!.force(/* allow-force */),
|
|
273
|
+
myColors[i]!.force(/* allow-force */),
|
|
274
|
+
packed.entries[i]!.vertexBase, drawStaging, i,
|
|
275
|
+
);
|
|
276
|
+
}
|
|
277
|
+
device.queue.writeBuffer(drawHeap, 0, drawStaging.buffer, drawStaging.byteOffset, heapBytes);
|
|
278
|
+
|
|
279
|
+
const dirty = new Set<number>();
|
|
280
|
+
const subs: IDisposable[] = [];
|
|
281
|
+
for (let i = 0; i < draws.length; i++) {
|
|
282
|
+
subs.push(addMarkingCallback(myTrafos[i] as never, () => dirty.add(i)));
|
|
283
|
+
subs.push(addMarkingCallback(myColors[i] as never, () => dirty.add(i)));
|
|
284
|
+
}
|
|
285
|
+
|
|
286
|
+
const module = device.createShaderModule({
|
|
287
|
+
code: SHADER_PRELUDE + fragmentWgsl,
|
|
288
|
+
label: `heapScene/${key}/shader`,
|
|
289
|
+
});
|
|
290
|
+
const bindLayoutEntries: GPUBindGroupLayoutEntry[] = [
|
|
291
|
+
{ binding: 0, visibility: GPUShaderStage.VERTEX, buffer: { type: "uniform" } },
|
|
292
|
+
{ binding: 1, visibility: GPUShaderStage.VERTEX, buffer: { type: "read-only-storage" } },
|
|
293
|
+
{ binding: 2, visibility: GPUShaderStage.VERTEX, buffer: { type: "read-only-storage" } },
|
|
294
|
+
{ binding: 3, visibility: GPUShaderStage.VERTEX, buffer: { type: "read-only-storage" } },
|
|
295
|
+
];
|
|
296
|
+
if (textureSet !== undefined) {
|
|
297
|
+
bindLayoutEntries.push(
|
|
298
|
+
{ binding: 4, visibility: GPUShaderStage.FRAGMENT, texture: { sampleType: "float" } },
|
|
299
|
+
{ binding: 5, visibility: GPUShaderStage.FRAGMENT, sampler: { type: "filtering" } },
|
|
300
|
+
);
|
|
301
|
+
}
|
|
302
|
+
const bindGroupLayout = device.createBindGroupLayout({
|
|
303
|
+
label: `heapScene/${key}/bgl`,
|
|
304
|
+
entries: bindLayoutEntries,
|
|
305
|
+
});
|
|
306
|
+
const pipelineLayout = device.createPipelineLayout({ bindGroupLayouts: [bindGroupLayout] });
|
|
307
|
+
const pipeline = device.createRenderPipeline({
|
|
308
|
+
label: `heapScene/${key}/pipeline`,
|
|
309
|
+
layout: pipelineLayout,
|
|
310
|
+
vertex: { module, entryPoint: "vs", buffers: [] },
|
|
311
|
+
fragment: { module, entryPoint: "fs", targets: [{ format: colorFormat }] },
|
|
312
|
+
primitive: { topology: "triangle-list", cullMode: "back", frontFace: "ccw" },
|
|
313
|
+
...(depthFormat !== undefined
|
|
314
|
+
? { depthStencil: { format: depthFormat, depthWriteEnabled: true, depthCompare: "less" as GPUCompareFunction } }
|
|
315
|
+
: {}),
|
|
316
|
+
});
|
|
317
|
+
const bindEntries: GPUBindGroupEntry[] = [
|
|
318
|
+
{ binding: 0, resource: { buffer: globals } },
|
|
319
|
+
{ binding: 1, resource: { buffer: drawHeap } },
|
|
320
|
+
{ binding: 2, resource: { buffer: packed.positionsBuf } },
|
|
321
|
+
{ binding: 3, resource: { buffer: packed.normalsBuf } },
|
|
322
|
+
];
|
|
323
|
+
if (textureSet !== undefined) {
|
|
324
|
+
bindEntries.push(
|
|
325
|
+
{ binding: 4, resource: textureSet.textureView ?? textureSet.texture.createView() },
|
|
326
|
+
{ binding: 5, resource: textureSet.sampler },
|
|
327
|
+
);
|
|
328
|
+
}
|
|
329
|
+
const bindGroup = device.createBindGroup({
|
|
330
|
+
label: `heapScene/${key}/bg`,
|
|
331
|
+
layout: bindGroupLayout,
|
|
332
|
+
entries: bindEntries,
|
|
333
|
+
});
|
|
334
|
+
|
|
335
|
+
return {
|
|
336
|
+
key, pipeline, bindGroup, globals, globalsStaging, drawHeap, drawStaging,
|
|
337
|
+
packed, trafos: myTrafos, colors: myColors, dirty, subs,
|
|
338
|
+
};
|
|
339
|
+
}
|
|
340
|
+
|
|
341
|
+
// ---------------------------------------------------------------------------
|
|
342
|
+
// Public API
|
|
343
|
+
// ---------------------------------------------------------------------------
|
|
344
|
+
|
|
345
|
+
export interface HeapDrawSpec {
|
|
346
|
+
readonly geo: HeapGeometry;
|
|
347
|
+
readonly modelTrafo: aval<Trafo3d> | Trafo3d;
|
|
348
|
+
readonly color: aval<V4f> | V4f;
|
|
349
|
+
/** Group key — selects which pipeline-state group this draw joins. */
|
|
350
|
+
readonly groupKey: string;
|
|
351
|
+
}
|
|
352
|
+
|
|
353
|
+
export interface HeapSceneStats {
|
|
354
|
+
readonly groups: number;
|
|
355
|
+
readonly totalDraws: number;
|
|
356
|
+
readonly globalsBytes: number;
|
|
357
|
+
drawBytes: number;
|
|
358
|
+
readonly geometryBytes: number;
|
|
359
|
+
}
|
|
360
|
+
|
|
361
|
+
export interface HeapScene {
|
|
362
|
+
/** Render one frame against the given globals. */
|
|
363
|
+
frame(viewProj: Trafo3d, lightLocation: V3d): void;
|
|
364
|
+
readonly stats: HeapSceneStats;
|
|
365
|
+
dispose(): void;
|
|
366
|
+
}
|
|
367
|
+
|
|
368
|
+
/**
|
|
369
|
+
* Per-group texture set. Bindless isn't standard WebGPU, so a group
|
|
370
|
+
* with sampled textures gets a distinct bind-group layout and is
|
|
371
|
+
* keyed separately from groups with no textures or different sets.
|
|
372
|
+
*/
|
|
373
|
+
export interface HeapTextureSet {
|
|
374
|
+
readonly texture: GPUTexture;
|
|
375
|
+
readonly textureView?: GPUTextureView;
|
|
376
|
+
readonly sampler: GPUSampler;
|
|
377
|
+
}
|
|
378
|
+
|
|
379
|
+
export interface BuildHeapSceneOptions {
|
|
380
|
+
/** WGSL fragment-stage source per group key — must declare `fs(in: VsOut) -> @location(0) vec4<f32>`. */
|
|
381
|
+
readonly fragmentShaders: ReadonlyMap<string, string>;
|
|
382
|
+
/**
|
|
383
|
+
* Per-group texture set. When present, the group's bind-group
|
|
384
|
+
* layout adds a `texture_2d<f32>` at binding 4 and a sampler at
|
|
385
|
+
* binding 5. The user's FS WGSL must declare them.
|
|
386
|
+
*/
|
|
387
|
+
readonly texturesByGroupKey?: ReadonlyMap<string, HeapTextureSet>;
|
|
388
|
+
}
|
|
389
|
+
|
|
390
|
+
/**
|
|
391
|
+
* Build a multi-group heap-backed scene renderer. Buckets `draws` by
|
|
392
|
+
* `groupKey`, builds one pipeline + bind group + heap per bucket,
|
|
393
|
+
* subscribes per-RO avals so dirty marks propagate to per-slot
|
|
394
|
+
* `writeBuffer`s on the next `frame()`.
|
|
395
|
+
*/
|
|
396
|
+
export function buildHeapScene(
|
|
397
|
+
device: GPUDevice,
|
|
398
|
+
attach: CanvasAttachment,
|
|
399
|
+
draws: readonly HeapDrawSpec[],
|
|
400
|
+
opts: BuildHeapSceneOptions,
|
|
401
|
+
): HeapScene {
|
|
402
|
+
const modelTrafos: aval<Trafo3d>[] = draws.map(d => asAval(d.modelTrafo));
|
|
403
|
+
const colors: aval<V4f>[] = draws.map(d => asAval(d.color));
|
|
404
|
+
|
|
405
|
+
// Bucket by group key.
|
|
406
|
+
const buckets = new Map<string, { spec: HeapDrawSpec; sourceIndex: number }[]>();
|
|
407
|
+
for (let i = 0; i < draws.length; i++) {
|
|
408
|
+
const d = draws[i]!;
|
|
409
|
+
let bucket = buckets.get(d.groupKey);
|
|
410
|
+
if (bucket === undefined) {
|
|
411
|
+
bucket = [];
|
|
412
|
+
buckets.set(d.groupKey, bucket);
|
|
413
|
+
}
|
|
414
|
+
bucket.push({ spec: d, sourceIndex: i });
|
|
415
|
+
}
|
|
416
|
+
const groups: Group[] = [];
|
|
417
|
+
for (const [key, members] of buckets) {
|
|
418
|
+
const fs = opts.fragmentShaders.get(key);
|
|
419
|
+
if (fs === undefined) {
|
|
420
|
+
throw new Error(`buildHeapScene: no fragment shader supplied for groupKey '${key}'`);
|
|
421
|
+
}
|
|
422
|
+
const textureSet = opts.texturesByGroupKey?.get(key);
|
|
423
|
+
groups.push(buildGroup(device, attach, key, fs, textureSet, members, modelTrafos, colors));
|
|
424
|
+
}
|
|
425
|
+
|
|
426
|
+
const stats: HeapSceneStats = {
|
|
427
|
+
groups: groups.length,
|
|
428
|
+
totalDraws: draws.length,
|
|
429
|
+
globalsBytes: GLOBALS_BYTES * groups.length,
|
|
430
|
+
drawBytes: 0,
|
|
431
|
+
geometryBytes: groups.reduce((acc, g) => acc + g.packed.bytes, 0),
|
|
432
|
+
};
|
|
433
|
+
|
|
434
|
+
function frame(viewProj: Trafo3d, lightLocation: V3d): void {
|
|
435
|
+
const fb = (attach.framebuffer as aval<import("../core/index.js").IFramebuffer>).force(/* allow-force */);
|
|
436
|
+
const colorAttachmentName = attach.signature.colorNames[0]!;
|
|
437
|
+
const colorView = fb.colors.tryFind(colorAttachmentName)!;
|
|
438
|
+
const depthFormat = attach.signature.depthStencil?.format;
|
|
439
|
+
const depthView = fb.depthStencil;
|
|
440
|
+
|
|
441
|
+
let totalDirtyBytes = 0;
|
|
442
|
+
|
|
443
|
+
const enc = device.createCommandEncoder({ label: "heapScene: encoder" });
|
|
444
|
+
let firstPass = true;
|
|
445
|
+
for (const g of groups) {
|
|
446
|
+
packGlobals(viewProj, lightLocation, g.globalsStaging);
|
|
447
|
+
device.queue.writeBuffer(g.globals, 0, g.globalsStaging.buffer, g.globalsStaging.byteOffset, GLOBALS_BYTES);
|
|
448
|
+
|
|
449
|
+
if (g.dirty.size > 0) {
|
|
450
|
+
for (const i of g.dirty) {
|
|
451
|
+
packDrawHeader(
|
|
452
|
+
g.trafos[i]!.force(/* allow-force */),
|
|
453
|
+
g.colors[i]!.force(/* allow-force */),
|
|
454
|
+
g.packed.entries[i]!.vertexBase, g.drawStaging, i,
|
|
455
|
+
);
|
|
456
|
+
const byteOff = i * DRAW_HEADER_BYTES;
|
|
457
|
+
device.queue.writeBuffer(
|
|
458
|
+
g.drawHeap, byteOff,
|
|
459
|
+
g.drawStaging.buffer, g.drawStaging.byteOffset + byteOff,
|
|
460
|
+
DRAW_HEADER_BYTES,
|
|
461
|
+
);
|
|
462
|
+
totalDirtyBytes += DRAW_HEADER_BYTES;
|
|
463
|
+
}
|
|
464
|
+
g.dirty.clear();
|
|
465
|
+
}
|
|
466
|
+
|
|
467
|
+
const pass = enc.beginRenderPass({
|
|
468
|
+
colorAttachments: [{
|
|
469
|
+
view: colorView,
|
|
470
|
+
clearValue: { r: 0.07, g: 0.07, b: 0.08, a: 1.0 },
|
|
471
|
+
loadOp: firstPass ? "clear" : "load",
|
|
472
|
+
storeOp: "store",
|
|
473
|
+
}],
|
|
474
|
+
...(depthView !== undefined && depthFormat !== undefined ? {
|
|
475
|
+
depthStencilAttachment: {
|
|
476
|
+
view: depthView,
|
|
477
|
+
depthClearValue: 1.0,
|
|
478
|
+
depthLoadOp: firstPass ? "clear" : "load",
|
|
479
|
+
depthStoreOp: "store",
|
|
480
|
+
} satisfies GPURenderPassDepthStencilAttachment,
|
|
481
|
+
} : {}),
|
|
482
|
+
});
|
|
483
|
+
pass.setPipeline(g.pipeline);
|
|
484
|
+
pass.setBindGroup(0, g.bindGroup);
|
|
485
|
+
pass.setIndexBuffer(g.packed.indexBuf, "uint32");
|
|
486
|
+
for (let i = 0; i < g.packed.entries.length; i++) {
|
|
487
|
+
const e = g.packed.entries[i]!;
|
|
488
|
+
pass.drawIndexed(e.indexCount, 1, e.firstIndex, 0, i);
|
|
489
|
+
}
|
|
490
|
+
pass.end();
|
|
491
|
+
firstPass = false;
|
|
492
|
+
}
|
|
493
|
+
|
|
494
|
+
stats.drawBytes = totalDirtyBytes;
|
|
495
|
+
device.queue.submit([enc.finish()]);
|
|
496
|
+
}
|
|
497
|
+
|
|
498
|
+
function dispose(): void {
|
|
499
|
+
for (const g of groups) {
|
|
500
|
+
for (const s of g.subs) s.dispose();
|
|
501
|
+
g.globals.destroy();
|
|
502
|
+
g.drawHeap.destroy();
|
|
503
|
+
g.packed.positionsBuf.destroy();
|
|
504
|
+
g.packed.normalsBuf.destroy();
|
|
505
|
+
g.packed.indexBuf.destroy();
|
|
506
|
+
}
|
|
507
|
+
}
|
|
508
|
+
|
|
509
|
+
return { frame, stats, dispose };
|
|
510
|
+
}
|
package/src/runtime/index.ts
CHANGED
|
@@ -22,3 +22,13 @@ export {
|
|
|
22
22
|
ScenePass,
|
|
23
23
|
type WalkerStats,
|
|
24
24
|
} from "./scenePass.js";
|
|
25
|
+
|
|
26
|
+
export {
|
|
27
|
+
buildHeapScene,
|
|
28
|
+
type HeapScene,
|
|
29
|
+
type HeapSceneStats,
|
|
30
|
+
type HeapDrawSpec,
|
|
31
|
+
type HeapGeometry,
|
|
32
|
+
type HeapTextureSet,
|
|
33
|
+
type BuildHeapSceneOptions,
|
|
34
|
+
} from "./heapScene.js";
|