@margoengine/html 0.0.0-stage → 1.0.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/LICENSE +201 -0
- package/NOTICE +4 -0
- package/README.md +197 -2
- package/dist/blend.d.ts +33 -0
- package/dist/blend.js +185 -0
- package/dist/dom-CgZN0DP1.js +17 -0
- package/dist/dom.d.ts +2 -0
- package/dist/errors-fc4i1pSL.js +7 -0
- package/dist/gl-j8yV2Jj1.js +31 -0
- package/dist/gl.d.ts +4 -0
- package/dist/index.d.ts +88 -0
- package/dist/index.js +614 -0
- package/dist/inline.d.ts +13 -0
- package/dist/inline.js +153 -0
- package/dist/pass-BVRaGB09.js +16 -0
- package/dist/texture-gl.d.ts +17 -0
- package/dist/texture-gl.js +103 -0
- package/dist/texture-kX-VTlgW.js +313 -0
- package/dist/texture.d.ts +126 -0
- package/dist/texture.js +1 -0
- package/dist/view-rect-Be4T67Kk.js +16 -0
- package/package.json +64 -4
package/dist/index.js
ADDED
|
@@ -0,0 +1,614 @@
|
|
|
1
|
+
// @margo/core-built 1.0.0
|
|
2
|
+
import {coreRequirement as __margoCoreRequirement,assertCore as __margoAssertCore} from '@margoengine/core/extension';
|
|
3
|
+
const r=(engine,version="1.0.0",source=import.meta.url)=>__margoRuntime(engine,version,source);
|
|
4
|
+
import { t as e } from "./errors-fc4i1pSL.js";
|
|
5
|
+
import { n as t, t as n } from "./view-rect-Be4T67Kk.js";
|
|
6
|
+
import { engineRuntime as __margoRuntime } from "@margoengine/core/extension";
|
|
7
|
+
import { LabelRenderer as i } from "@margoengine/labels";
|
|
8
|
+
//#region ../shared/frame-resources.js
|
|
9
|
+
var a = (globalThis[Symbol.for("margoengine.frame-resources")] ??= { devices: /* @__PURE__ */ new WeakMap() }).devices;
|
|
10
|
+
function o(e) {
|
|
11
|
+
let t = a.get(e);
|
|
12
|
+
return t || (a.set(e, t = {
|
|
13
|
+
frame: 0,
|
|
14
|
+
open: !1,
|
|
15
|
+
lost: !1,
|
|
16
|
+
views: /* @__PURE__ */ new Set(),
|
|
17
|
+
drawing: 0,
|
|
18
|
+
retired: [],
|
|
19
|
+
written: /* @__PURE__ */ new WeakMap()
|
|
20
|
+
}), e.lost?.then?.(() => {
|
|
21
|
+
t.lost = !0;
|
|
22
|
+
let e = t.retired;
|
|
23
|
+
t.retired = [];
|
|
24
|
+
for (let t of e) t.destroy();
|
|
25
|
+
})), t;
|
|
26
|
+
}
|
|
27
|
+
function s(e, t) {
|
|
28
|
+
if (!t) return;
|
|
29
|
+
let n = o(e), r = [t, ...n.written.get(t)?.extras ?? []];
|
|
30
|
+
if (n.written.delete(t), !n.lost && n.open && (n.views.size > 1 || n.drawing > 0)) n.retired.push(...r);
|
|
31
|
+
else for (let e of r) e.destroy();
|
|
32
|
+
}
|
|
33
|
+
function c(e) {
|
|
34
|
+
return e?.adapterInfo?.vendor === "margo-webgl2" ? "webgl2" : "native";
|
|
35
|
+
}
|
|
36
|
+
function l(e) {
|
|
37
|
+
return c(e) === "native" && e?.features?.has("core-features-and-limits") === !1;
|
|
38
|
+
}
|
|
39
|
+
//#endregion
|
|
40
|
+
//#region ../shared/msaa.js
|
|
41
|
+
var u = "@vertex fn vertex(@builtin(vertex_index) i:u32)->@builtin(position) vec4f {return vec4f(array<vec2f,3>(vec2f(-1,-1),vec2f(3,-1),vec2f(-1,3))[i],0,1);}", d = (e, t, n) => e.createBindGroup({
|
|
42
|
+
layout: t.getBindGroupLayout(0),
|
|
43
|
+
entries: [{
|
|
44
|
+
binding: 0,
|
|
45
|
+
resource: n
|
|
46
|
+
}]
|
|
47
|
+
}), f = Symbol.for("margoengine.optional.msaa"), p = u + "\n@group(0) @binding(0) var source:texture_depth_multisampled_2d;\n@fragment fn fragment(@builtin(position) p:vec4f)->@builtin(frag_depth) f32 {\n let q=vec2i(p.xy);\n return max(max(textureLoad(source,q,0),textureLoad(source,q,1)),max(textureLoad(source,q,2),textureLoad(source,q,3)));\n}";
|
|
48
|
+
function m(e, t, n, r, i, a = !1) {
|
|
49
|
+
let o = (e) => p.replace(/return max[^;]+;/, `return textureLoad(source,q,${e});`), s = (n, r) => {
|
|
50
|
+
let a = e.createShaderModule({ code: n }), o = e.createRenderPipeline({
|
|
51
|
+
layout: "auto",
|
|
52
|
+
vertex: {
|
|
53
|
+
module: a,
|
|
54
|
+
entryPoint: "vertex"
|
|
55
|
+
},
|
|
56
|
+
fragment: {
|
|
57
|
+
module: a,
|
|
58
|
+
entryPoint: "fragment",
|
|
59
|
+
targets: []
|
|
60
|
+
},
|
|
61
|
+
depthStencil: {
|
|
62
|
+
format: i,
|
|
63
|
+
depthWriteEnabled: !0,
|
|
64
|
+
depthCompare: "always"
|
|
65
|
+
},
|
|
66
|
+
multisample: r
|
|
67
|
+
});
|
|
68
|
+
return {
|
|
69
|
+
pipeline: o,
|
|
70
|
+
group: d(e, o, t)
|
|
71
|
+
};
|
|
72
|
+
};
|
|
73
|
+
return {
|
|
74
|
+
draws: a ? [s(o(0).replace("_multisampled", ""), { count: r })] : r === 4 ? l(e) ? [
|
|
75
|
+
0,
|
|
76
|
+
1,
|
|
77
|
+
2,
|
|
78
|
+
3
|
|
79
|
+
].map((e) => s(o(e), {
|
|
80
|
+
count: r,
|
|
81
|
+
mask: 1 << e
|
|
82
|
+
})) : [s(o("i32(sample)").replace("p:vec4f)", "p:vec4f,@builtin(sample_index) sample:u32)"), { count: r })] : [s(p, { count: r })],
|
|
83
|
+
descriptor: {
|
|
84
|
+
colorAttachments: [],
|
|
85
|
+
depthStencilAttachment: {
|
|
86
|
+
view: n,
|
|
87
|
+
depthLoadOp: "clear",
|
|
88
|
+
depthClearValue: 0,
|
|
89
|
+
depthStoreOp: "store",
|
|
90
|
+
stencilReadOnly: !0
|
|
91
|
+
}
|
|
92
|
+
}
|
|
93
|
+
};
|
|
94
|
+
}
|
|
95
|
+
function h(e, t, r, i, a, o) {
|
|
96
|
+
let s = e.pipelines.get(i);
|
|
97
|
+
s || e.pipelines.set(i, s = /* @__PURE__ */ new Map());
|
|
98
|
+
let c = s.get(a);
|
|
99
|
+
if (!c) {
|
|
100
|
+
let t = e.samples === 1;
|
|
101
|
+
s.set(a, c = m(e.device, e.depthSource, r, i, a, t));
|
|
102
|
+
}
|
|
103
|
+
c.descriptor.depthStencilAttachment.view = r, c.descriptor.depthStencilAttachment.depthLoadOp = o?.view ? "load" : "clear";
|
|
104
|
+
let l = t.beginRenderPass(c.descriptor);
|
|
105
|
+
o && n(l, o);
|
|
106
|
+
let u = c.draws;
|
|
107
|
+
for (let e = 0; e < u.length; e++) l.setPipeline(u[e].pipeline), l.setBindGroup(0, u[e].group), l.draw(3);
|
|
108
|
+
l.end();
|
|
109
|
+
}
|
|
110
|
+
var g = class {
|
|
111
|
+
constructor(e, t, n) {
|
|
112
|
+
this.device = e, this.samples = n, this.pipelines = /* @__PURE__ */ new Map(), this.depthSource = t.createView({ aspect: "depth-only" });
|
|
113
|
+
}
|
|
114
|
+
copy(e, t, n, r) {
|
|
115
|
+
h(this, e.encoder, t, n, r, e);
|
|
116
|
+
}
|
|
117
|
+
}, _ = class {
|
|
118
|
+
constructor(e, t, n, r = 4) {
|
|
119
|
+
if (r !== 1 && r !== 4) throw RangeError("Scene samples must be 1 or 4");
|
|
120
|
+
this.samples = r, this.device = e, this.resolvedColor = t, this.resolvedDepth = n, this.textures = [], this.pipelines = /* @__PURE__ */ new Map();
|
|
121
|
+
try {
|
|
122
|
+
if (r === 1) {
|
|
123
|
+
this.color = t.createView(), this.depth = n.createView();
|
|
124
|
+
return;
|
|
125
|
+
}
|
|
126
|
+
for (let r of [t, n]) this.textures.push(e.createTexture({
|
|
127
|
+
size: [r.width, r.height],
|
|
128
|
+
format: r.format,
|
|
129
|
+
sampleCount: 4,
|
|
130
|
+
usage: GPUTextureUsage.RENDER_ATTACHMENT | GPUTextureUsage.TEXTURE_BINDING
|
|
131
|
+
}));
|
|
132
|
+
this.color = this.textures[0].createView(), this.depth = this.textures[1].createView(), this.resolution = { colorAttachments: [{
|
|
133
|
+
view: this.color,
|
|
134
|
+
resolveTarget: t.createView(),
|
|
135
|
+
loadOp: "load",
|
|
136
|
+
storeOp: "store"
|
|
137
|
+
}] }, this.finalResolution = {
|
|
138
|
+
colorAttachments: [{
|
|
139
|
+
...this.resolution.colorAttachments[0],
|
|
140
|
+
storeOp: "discard"
|
|
141
|
+
}],
|
|
142
|
+
depthStencilAttachment: {
|
|
143
|
+
view: this.depth,
|
|
144
|
+
depthLoadOp: "load",
|
|
145
|
+
depthStoreOp: "discard",
|
|
146
|
+
stencilReadOnly: !0
|
|
147
|
+
}
|
|
148
|
+
}, this.depthSource = this.textures[1].createView({ aspect: "depth-only" }), this.resolvedDepthView = n.createView();
|
|
149
|
+
} catch (e) {
|
|
150
|
+
throw this.dispose(), e;
|
|
151
|
+
}
|
|
152
|
+
}
|
|
153
|
+
begin(e) {
|
|
154
|
+
(this.viewContext ??= {}).view = e.view, Object.assign(this.original ??= {}, {
|
|
155
|
+
color: e.color,
|
|
156
|
+
depth: e.depth,
|
|
157
|
+
format: e.format,
|
|
158
|
+
descriptor: e.descriptor,
|
|
159
|
+
owner: v(e)
|
|
160
|
+
}), e.color = this.color, e.depth = this.depth, e.format = this.resolvedColor.format, this.baseDescriptor !== e.descriptor && (this.baseDescriptor = e.descriptor, this.descriptor = {
|
|
161
|
+
...e.descriptor,
|
|
162
|
+
depthStencil: {
|
|
163
|
+
...e.descriptor.depthStencil,
|
|
164
|
+
format: this.resolvedDepth.format
|
|
165
|
+
},
|
|
166
|
+
multisample: { count: this.samples }
|
|
167
|
+
}), e.descriptor = this.descriptor, e[f] = this, this.retired = !1, this.resized = e.width !== void 0 && (e.width !== this.resolvedDepth.width || e.height !== this.resolvedDepth.height);
|
|
168
|
+
}
|
|
169
|
+
copyDepth(e, t, n, r = this.resolvedDepth.format) {
|
|
170
|
+
this.depthSource ??= this.resolvedDepth.createView({ aspect: "depth-only" }), h(this, e, t, n, r, this.viewContext);
|
|
171
|
+
}
|
|
172
|
+
resolve(e, t = !1, n = !0) {
|
|
173
|
+
if (this.samples === 1) t && this.give(e);
|
|
174
|
+
else {
|
|
175
|
+
if (n && this.copyDepth(e, this.resolvedDepthView, 1), t) {
|
|
176
|
+
let t = this.viewContext?.view ? "store" : "discard";
|
|
177
|
+
this.finalResolution.colorAttachments[0].storeOp = t, this.finalResolution.depthStencilAttachment.depthStoreOp = t, this.give(e);
|
|
178
|
+
}
|
|
179
|
+
e.beginRenderPass(t ? this.finalResolution : this.resolution).end();
|
|
180
|
+
}
|
|
181
|
+
}
|
|
182
|
+
give(e) {
|
|
183
|
+
let t = this.original;
|
|
184
|
+
this.retired || !t || this.resized || (this.copyDepth(e, t.depth, t.descriptor.multisample?.count ?? 1, t.descriptor.depthStencil.format), this.retired = !0);
|
|
185
|
+
}
|
|
186
|
+
restore(e) {
|
|
187
|
+
let t = this.original;
|
|
188
|
+
this.give(e.encoder), Object.assign(e, {
|
|
189
|
+
color: t.color,
|
|
190
|
+
depth: t.depth,
|
|
191
|
+
format: t.format,
|
|
192
|
+
descriptor: t.descriptor
|
|
193
|
+
}), e[f] = t.owner;
|
|
194
|
+
}
|
|
195
|
+
present(e) {
|
|
196
|
+
this.resolve(e.encoder, !0), this.restore(e);
|
|
197
|
+
let r = e.descriptor.multisample?.count ?? 1;
|
|
198
|
+
if (!this.presentation || this.presentationFormat !== e.format || this.presentationSamples !== r) {
|
|
199
|
+
let t = this.device.createShaderModule({ code: u + "@group(0) @binding(0) var source:texture_2d<f32>;@fragment fn fragment(@builtin(position) p:vec4f)->@location(0) vec4f{return textureLoad(source,vec2i(p.xy),0);}" });
|
|
200
|
+
this.presentation = this.device.createRenderPipeline({
|
|
201
|
+
layout: "auto",
|
|
202
|
+
vertex: {
|
|
203
|
+
module: t,
|
|
204
|
+
entryPoint: "vertex"
|
|
205
|
+
},
|
|
206
|
+
fragment: {
|
|
207
|
+
module: t,
|
|
208
|
+
entryPoint: "fragment",
|
|
209
|
+
targets: [{ format: e.format }]
|
|
210
|
+
},
|
|
211
|
+
multisample: e.descriptor.multisample
|
|
212
|
+
}), this.presentationGroup = d(this.device, this.presentation, this.resolvedColor.createView()), this.presentationFormat = e.format, this.presentationSamples = r;
|
|
213
|
+
}
|
|
214
|
+
let i = this.presentationPass ??= { colorAttachments: [{
|
|
215
|
+
view: e.color,
|
|
216
|
+
loadOp: "clear",
|
|
217
|
+
storeOp: "store"
|
|
218
|
+
}] };
|
|
219
|
+
i.colorAttachments[0].view = e.color, i.colorAttachments[0].loadOp = t(e, "clear");
|
|
220
|
+
let a = e.encoder.beginRenderPass(i);
|
|
221
|
+
a.setPipeline(this.presentation), a.setBindGroup(0, this.presentationGroup), n(a, e), a.draw(3), a.end();
|
|
222
|
+
}
|
|
223
|
+
dispose() {
|
|
224
|
+
for (let e of this.textures) s(this.device, e);
|
|
225
|
+
this.textures = [], this.pipelines.clear();
|
|
226
|
+
}
|
|
227
|
+
};
|
|
228
|
+
function v(e) {
|
|
229
|
+
let t = e[f];
|
|
230
|
+
return t?.color === e.color && t.depth === e.depth ? t : void 0;
|
|
231
|
+
}
|
|
232
|
+
function y(e) {
|
|
233
|
+
v(e)?.resolve(e.encoder);
|
|
234
|
+
}
|
|
235
|
+
//#endregion
|
|
236
|
+
//#region ../shared/gpu.js
|
|
237
|
+
function b(e, t, n) {
|
|
238
|
+
return e.createBindGroup({
|
|
239
|
+
layout: t.getBindGroupLayout(0),
|
|
240
|
+
entries: n.map((e, t) => ({
|
|
241
|
+
binding: t,
|
|
242
|
+
resource: e
|
|
243
|
+
}))
|
|
244
|
+
});
|
|
245
|
+
}
|
|
246
|
+
function x(e, t, n = "main") {
|
|
247
|
+
return e.createComputePipeline({
|
|
248
|
+
layout: "auto",
|
|
249
|
+
compute: {
|
|
250
|
+
module: e.createShaderModule({ code: t }),
|
|
251
|
+
entryPoint: n
|
|
252
|
+
}
|
|
253
|
+
});
|
|
254
|
+
}
|
|
255
|
+
function S(e, t, n, r, i = 1) {
|
|
256
|
+
let a = e.encoder.beginComputePass();
|
|
257
|
+
a.setPipeline(t), a.setBindGroup(0, n), a.dispatchWorkgroups(r, i), a.end();
|
|
258
|
+
}
|
|
259
|
+
function C(t) {
|
|
260
|
+
if (!t.gpu) throw e("unsupported", "Scene depth requires WebGPU; see https://margoengine.com/errors#webgpu");
|
|
261
|
+
let n = t.plugins.get("optional:scene-depth");
|
|
262
|
+
n || (n = {coreRequirement:/*@__PURE__*/__margoCoreRequirement("1.0.0",import.meta.url),
|
|
263
|
+
name: "optional:scene-depth",
|
|
264
|
+
references: 0,
|
|
265
|
+
texture: void 0,
|
|
266
|
+
color: void 0,
|
|
267
|
+
target: void 0,
|
|
268
|
+
borrowed: !1,
|
|
269
|
+
setup() {},
|
|
270
|
+
registerPasses(e) {
|
|
271
|
+
e.add({
|
|
272
|
+
name: this.name + ":capture",
|
|
273
|
+
order: -15,
|
|
274
|
+
execute: (e) => {
|
|
275
|
+
if (e.backend !== "webgpu") return;
|
|
276
|
+
let t = v(e);
|
|
277
|
+
if (this.borrowed = !!t, t) {
|
|
278
|
+
this.borrowedDepth = t.resolvedDepth;
|
|
279
|
+
return;
|
|
280
|
+
}
|
|
281
|
+
let n = e.descriptor.multisample?.count ?? 4;
|
|
282
|
+
try {
|
|
283
|
+
if (n === 1) {
|
|
284
|
+
(!this.depthView || this.texture?.width !== e.width || this.texture?.height !== e.height || this.texture?.format !== e.descriptor.depthStencil.format) && (this.onDeviceRestored(), this.texture = e.device.createTexture({
|
|
285
|
+
size: [e.width, e.height],
|
|
286
|
+
format: e.descriptor.depthStencil.format,
|
|
287
|
+
usage: GPUTextureUsage.RENDER_ATTACHMENT | GPUTextureUsage.TEXTURE_BINDING
|
|
288
|
+
}), this.depthView = this.texture.createView()), this.outer = e.depth, e.depth = this.depthView;
|
|
289
|
+
return;
|
|
290
|
+
}
|
|
291
|
+
(this.target?.samples !== n || this.texture?.width !== e.width || this.texture?.height !== e.height || this.texture?.format !== e.descriptor.depthStencil.format || this.color?.format !== e.format) && (this.onDeviceRestored(), this.texture = e.device.createTexture({
|
|
292
|
+
size: [e.width, e.height],
|
|
293
|
+
format: e.descriptor.depthStencil.format,
|
|
294
|
+
usage: GPUTextureUsage.RENDER_ATTACHMENT | GPUTextureUsage.TEXTURE_BINDING
|
|
295
|
+
}), this.color = e.device.createTexture({
|
|
296
|
+
size: [e.width, e.height],
|
|
297
|
+
format: e.format,
|
|
298
|
+
usage: GPUTextureUsage.RENDER_ATTACHMENT | GPUTextureUsage.TEXTURE_BINDING
|
|
299
|
+
}), this.target = new _(e.device, this.color, this.texture, n)), this.target.begin(e);
|
|
300
|
+
} catch (e) {
|
|
301
|
+
throw this.onDeviceRestored(), e;
|
|
302
|
+
}
|
|
303
|
+
}
|
|
304
|
+
}), e.add({
|
|
305
|
+
name: this.name + ":restore",
|
|
306
|
+
order: 1e4,
|
|
307
|
+
execute: (e) => {
|
|
308
|
+
if (!(e.backend !== "webgpu" || this.borrowed)) {
|
|
309
|
+
if (e.descriptor.multisample?.count !== 1) return this.target.present(e);
|
|
310
|
+
this.outer &&= (this.copier ??= new g(e.device, this.texture, 1), this.copier.copy(e, this.outer, 1, e.descriptor.depthStencil.format), e.depth = this.outer, void 0);
|
|
311
|
+
}
|
|
312
|
+
}
|
|
313
|
+
});
|
|
314
|
+
},
|
|
315
|
+
onDeviceRestored() {
|
|
316
|
+
this.target?.dispose(), this.texture?.destroy(), this.color?.destroy(), this.target = this.texture = this.color = this.depthView = this.copier = this.outer = void 0;
|
|
317
|
+
},
|
|
318
|
+
dispose() {
|
|
319
|
+
this.onDeviceRestored();
|
|
320
|
+
}
|
|
321
|
+
}, t.use(n)), n.references++;
|
|
322
|
+
let r = !1;
|
|
323
|
+
return {
|
|
324
|
+
texture: () => n.borrowed ? n.borrowedDepth : n.texture,
|
|
325
|
+
release() {
|
|
326
|
+
r || (r = !0, --n.references === 0 && t.sceneOf(n)?.removePlugin(n.name));
|
|
327
|
+
}
|
|
328
|
+
};
|
|
329
|
+
}
|
|
330
|
+
//#endregion
|
|
331
|
+
//#region src/index.ts
|
|
332
|
+
var w = "\n@group(0) @binding(0) var depth:texture_depth_2d;\n@group(0) @binding(1) var<storage,read> points:array<vec4f>;\n@group(0) @binding(2) var<storage,read_write> output:array<u32>;\n@compute @workgroup_size(64) fn main(@builtin(global_invocation_id) id:vec3u){\n if(id.x>=arrayLength(&points)){return;}let point=points[id.x];let p=vec2i(point.xy);let size=vec2i(textureDimensions(depth));\n output[id.x]=0u;if(any(p<vec2i(0))||any(p>=size)){return;}\n let d=textureLoad(depth,p,0);output[id.x]=select(0u,1u,d>point.z+point.w);\n}", T = (e) => `
|
|
333
|
+
@group(0) @binding(0) var depth:texture_depth_2d;
|
|
334
|
+
@group(0) @binding(1) var<storage,read> points:array<vec4f>;
|
|
335
|
+
@group(0) @binding(2) var<storage,read_write> output:array<u32>;
|
|
336
|
+
@group(0) @binding(3) var<uniform> radius:vec4f;
|
|
337
|
+
@compute @workgroup_size(64) fn main(@builtin(global_invocation_id) id:vec3u){
|
|
338
|
+
if(id.x>=arrayLength(&points)){return;}let point=points[id.x];let size=vec2i(textureDimensions(depth));var covered=0u;
|
|
339
|
+
for(var j=0u;j<${e}u;j++){for(var i=0u;i<${e}u;i++){
|
|
340
|
+
let o=(vec2f(f32(i),f32(j))+.5)*(2./${e}.)-1.;let p=vec2i(floor(point.xy+o*radius.xy));
|
|
341
|
+
if(all(p>=vec2i(0))&&all(p<size)){if(textureLoad(depth,p,0)>point.z+point.w){covered++;}}
|
|
342
|
+
}}
|
|
343
|
+
output[id.x]=covered;
|
|
344
|
+
}`;
|
|
345
|
+
function E(e) {
|
|
346
|
+
if (e === void 0 || e === !1) return;
|
|
347
|
+
let t = e === !0 ? {} : e;
|
|
348
|
+
if (typeof t != "object" || !t) throw RangeError("Invalid HTML fade options; pass fade: true or an object of radius, samples and duration");
|
|
349
|
+
let n = t.radius ?? 8, r = t.samples ?? 4, i = t.duration ?? .15;
|
|
350
|
+
if (!(n >= 0 && n <= 256) || !Number.isInteger(r) || r < 2 || r > 8 || !(i >= 0 && i <= 10)) throw RangeError("Invalid HTML fade options; pass radius 0 to 256 CSS pixels, samples a whole number 2 to 8 and duration 0 to 10 seconds");
|
|
351
|
+
return {
|
|
352
|
+
radius: n,
|
|
353
|
+
samples: r,
|
|
354
|
+
duration: i
|
|
355
|
+
};
|
|
356
|
+
}
|
|
357
|
+
var D = "#version 300 es\nuniform mat4 camera;uniform vec3 world;uniform vec2 offset;uniform float bias;\nvoid main(){vec4 p=camera*vec4(world,1.0);p.xy+=offset*p.w;p.z-=2.0*bias*p.w;gl_Position=p;gl_PointSize=1.0;}", O = "#version 300 es\nprecision lowp float;out vec4 color;void main(){color=vec4(0.0);}";
|
|
358
|
+
function k(t) {
|
|
359
|
+
let n = t.createProgram(), r = t.createShader(t.VERTEX_SHADER), i = t.createShader(t.FRAGMENT_SHADER);
|
|
360
|
+
if (t.shaderSource(r, D), t.shaderSource(i, O), t.compileShader(r), t.compileShader(i), t.attachShader(n, r), t.attachShader(n, i), t.linkProgram(n), t.deleteShader(r), t.deleteShader(i), !t.getProgramParameter(n, t.LINK_STATUS) && !t.isContextLost()) throw e("failed", `HTML could not build its WebGL2 occlusion shaders (${t.getProgramInfoLog(n)}); pass depth: false to draw without occlusion`);
|
|
361
|
+
return {
|
|
362
|
+
gl: t,
|
|
363
|
+
program: n,
|
|
364
|
+
vao: t.createVertexArray(),
|
|
365
|
+
camera: t.getUniformLocation(n, "camera"),
|
|
366
|
+
world: t.getUniformLocation(n, "world"),
|
|
367
|
+
offset: t.getUniformLocation(n, "offset"),
|
|
368
|
+
bias: t.getUniformLocation(n, "bias"),
|
|
369
|
+
slots: []
|
|
370
|
+
};
|
|
371
|
+
}
|
|
372
|
+
var A = 1048576, j = class t extends i {coreRequirement=__margoCoreRequirement("1.0.0",import.meta.url);
|
|
373
|
+
constructor(e = {}) {
|
|
374
|
+
super({
|
|
375
|
+
...e,
|
|
376
|
+
name: e.name ?? "html",
|
|
377
|
+
order: 1e3
|
|
378
|
+
}), this.settings = e, this.records = /* @__PURE__ */ new Map(), this.slots = [], this.gpuSlots = 0, this.used = 0, this.free = [], this.pending = !1, this.generation = 0, this.disposed = !1, this.depthRequests = [], this.radiusValues = /* @__PURE__ */ new Float32Array(4), this.delta = 0;
|
|
379
|
+
let t = e.capacity ?? 1024, n = e.depthBias ?? 1e-4;
|
|
380
|
+
if (!Number.isInteger(t) || t < 1 || t > A) throw RangeError("Invalid HTML capacity; pass a whole number of anchors from 1 to 1,048,576");
|
|
381
|
+
if (!Number.isFinite(n) || n < 0 || n > .05 || e.depth !== void 0 && e.depth !== !1 && typeof e.depth != "function") throw RangeError("Invalid HTML depth options; pass depth: false or a function returning a depth texture, and a finite depthBias");
|
|
382
|
+
this.fade = E(e.fade), this.capacity = t, this.allocated = Math.min(t, 64), this.values = new Float32Array(this.allocated * 4);
|
|
383
|
+
for (let e = 0; e < this.values.length; e += 4) this.values[e] = this.values[e + 1] = -1;
|
|
384
|
+
}
|
|
385
|
+
static attach(e, n = {}) {__margoAssertCore(e,__margoCoreRequirement("1.0.0",import.meta.url));
|
|
386
|
+
let i = new t(n);
|
|
387
|
+
return r(e).use(i), i;
|
|
388
|
+
}
|
|
389
|
+
add(t, n = {}) {
|
|
390
|
+
if (this.disposed) throw e("disposed", "HTML is disposed; create a new HTML renderer");
|
|
391
|
+
let r = this.free.length ? this.free[this.free.length - 1] : this.used;
|
|
392
|
+
if (r >= this.capacity) throw RangeError("HTML anchor capacity exceeded; raise capacity or remove anchors");
|
|
393
|
+
let i = super.add(t, {
|
|
394
|
+
...n,
|
|
395
|
+
occlude: (e, t) => {
|
|
396
|
+
let i = this.records.get(e);
|
|
397
|
+
i.x = t.x, i.y = t.y, i.z = 1 - t.depth;
|
|
398
|
+
let a = this.runtime?.canvas;
|
|
399
|
+
if (a) {
|
|
400
|
+
let e = this.view, n = a.width / (a.clientWidth || a.width), o = a.height / (a.clientHeight || a.height), s = r * 4;
|
|
401
|
+
this.values[s] = t.x * n + (e ? e.viewport[0] * (e.pixels ? 1 : a.width) : 0), this.values[s + 1] = t.y * o + (e ? e.viewport[1] * (e.pixels ? 1 : a.height) : 0), this.values[s + 2] = i.z, this.values[s + 3] = this.settings.depthBias ?? 1e-4;
|
|
402
|
+
}
|
|
403
|
+
return !!(n.occlude ?? this.settings.occlude)?.(e, t) || i.hidden;
|
|
404
|
+
}
|
|
405
|
+
});
|
|
406
|
+
r === this.used ? this.used++ : this.free.pop(), r >= this.allocated && this.grow(r + 1);
|
|
407
|
+
let a = {
|
|
408
|
+
label: i,
|
|
409
|
+
index: r,
|
|
410
|
+
hidden: !1,
|
|
411
|
+
x: 0,
|
|
412
|
+
y: 0,
|
|
413
|
+
z: 0,
|
|
414
|
+
target: 1,
|
|
415
|
+
opacity: 1,
|
|
416
|
+
written: 1
|
|
417
|
+
};
|
|
418
|
+
return this.records.set(i, a), this.slots[r] = a, i;
|
|
419
|
+
}
|
|
420
|
+
grow(e) {
|
|
421
|
+
let t = this.allocated;
|
|
422
|
+
for (; t < e;) t *= 2;
|
|
423
|
+
t = Math.min(t, this.capacity);
|
|
424
|
+
let n = new Float32Array(t * 4);
|
|
425
|
+
n.set(this.values);
|
|
426
|
+
for (let e = this.allocated * 4; e < n.length; e += 4) n[e] = n[e + 1] = -1;
|
|
427
|
+
this.values = n, this.allocated = t;
|
|
428
|
+
}
|
|
429
|
+
remove(e) {
|
|
430
|
+
let t = this.records.get(e);
|
|
431
|
+
t && (this.free.push(t.index), this.free.sort((e, t) => t - e), this.slots[t.index] = void 0, this.records.delete(e), this.values.set([
|
|
432
|
+
-1,
|
|
433
|
+
-1,
|
|
434
|
+
0,
|
|
435
|
+
0
|
|
436
|
+
], t.index * 4)), super.remove(e);
|
|
437
|
+
}
|
|
438
|
+
setup(t) {
|
|
439
|
+
if (this.disposed) throw e("disposed", "HTML is disposed; create a new HTML renderer");
|
|
440
|
+
this.runtime = r(t), super.setup(t), this.setupGPU();
|
|
441
|
+
}
|
|
442
|
+
setupGPU() {
|
|
443
|
+
let e = this.runtime;
|
|
444
|
+
e.gpu && this.settings.depth !== !1 && (this.shared = this.settings.depth ? void 0 : this.shared ?? C(e), this.source = this.settings.depth || this.shared.texture, this.buffers(e.gpu), this.fade && (this.radius = e.gpu.createBuffer({
|
|
445
|
+
size: 16,
|
|
446
|
+
usage: GPUBufferUsage.UNIFORM | GPUBufferUsage.COPY_DST
|
|
447
|
+
})), this.pipeline = x(e.gpu, this.fade ? T(this.fade.samples) : w));
|
|
448
|
+
}
|
|
449
|
+
buffers(e) {
|
|
450
|
+
this.input?.destroy(), this.output?.destroy(), this.read?.destroy();
|
|
451
|
+
let t = this.gpuSlots = this.allocated;
|
|
452
|
+
this.input = e.createBuffer({
|
|
453
|
+
size: t * 16,
|
|
454
|
+
usage: GPUBufferUsage.STORAGE | GPUBufferUsage.COPY_DST
|
|
455
|
+
}), this.output = e.createBuffer({
|
|
456
|
+
size: t * 4,
|
|
457
|
+
usage: GPUBufferUsage.STORAGE | GPUBufferUsage.COPY_SRC
|
|
458
|
+
}), this.read = e.createBuffer({
|
|
459
|
+
size: t * 4,
|
|
460
|
+
usage: GPUBufferUsage.COPY_DST | GPUBufferUsage.MAP_READ
|
|
461
|
+
}), this.depthGroup = void 0;
|
|
462
|
+
}
|
|
463
|
+
onPreUpdate(e) {
|
|
464
|
+
this.delta += e;
|
|
465
|
+
}
|
|
466
|
+
update() {
|
|
467
|
+
let e = this.fade;
|
|
468
|
+
if (e) {
|
|
469
|
+
let t = e.duration > 0 ? this.delta / e.duration : Infinity;
|
|
470
|
+
this.delta = 0;
|
|
471
|
+
for (let e of this.records.values()) {
|
|
472
|
+
let n = e.target - e.opacity;
|
|
473
|
+
e.opacity = Math.abs(n) <= t ? e.target : e.opacity + Math.sign(n) * t, e.hidden = e.opacity <= 0;
|
|
474
|
+
}
|
|
475
|
+
}
|
|
476
|
+
if (super.update(), e) for (let e of this.records.values()) {
|
|
477
|
+
let t = Math.round(e.opacity * 1e3) / 1e3;
|
|
478
|
+
e.label.onScreen && t !== e.written && (e.written = t, e.label.element.style.opacity = t === 1 ? "" : String(t));
|
|
479
|
+
}
|
|
480
|
+
}
|
|
481
|
+
registerPasses(e) {
|
|
482
|
+
super.registerPasses(e), e.add({
|
|
483
|
+
name: this.name + ":depth",
|
|
484
|
+
order: 1001,
|
|
485
|
+
execute: (e) => {
|
|
486
|
+
e.backend === "webgpu" ? this.pipeline && !this.pending && this.sample(e) : this.settings.depth !== !1 && !e.gl.isContextLost() && this.query(e.gl, e.camera.matrix, e.view?.viewport);
|
|
487
|
+
}
|
|
488
|
+
});
|
|
489
|
+
}
|
|
490
|
+
sample(t) {
|
|
491
|
+
y(t);
|
|
492
|
+
let n = this.source();
|
|
493
|
+
if (!n || n.width !== t.width || n.height !== t.height || !n.format.startsWith("depth")) throw e("state", "HTML requires viewport-sized scene depth; pass a depth texture that matches the canvas, or depth: false");
|
|
494
|
+
if (!this.records.size) return;
|
|
495
|
+
this.gpuSlots !== this.allocated && this.buffers(t.device);
|
|
496
|
+
let r = this.used;
|
|
497
|
+
for (let e = 0; e < r; e++) if (!this.slots[e] || !this.slots[e].label.visible || !this.slots[e].label.screen.inFrustum) {
|
|
498
|
+
let t = e * 4;
|
|
499
|
+
this.values[t] = this.values[t + 1] = -1, this.values[t + 2] = this.values[t + 3] = 0;
|
|
500
|
+
}
|
|
501
|
+
if (t.device.queue.writeBuffer(this.input, 0, this.values, 0, r * 4), this.fade) {
|
|
502
|
+
let e = this.runtime.canvas, n = this.radiusValues, r = this.fade.radius * e.width / (e.clientWidth || e.width), i = this.fade.radius * e.height / (e.clientHeight || e.height);
|
|
503
|
+
(n[0] !== r || n[1] !== i || !this.depthGroup) && (n[0] = r, n[1] = i, t.device.queue.writeBuffer(this.radius, 0, n));
|
|
504
|
+
}
|
|
505
|
+
if (!this.depthGroup || n !== this.sampledDepth) {
|
|
506
|
+
let e = [
|
|
507
|
+
n.createView({ aspect: "depth-only" }),
|
|
508
|
+
{ buffer: this.input },
|
|
509
|
+
{ buffer: this.output }
|
|
510
|
+
];
|
|
511
|
+
this.radius && e.push({ buffer: this.radius }), this.depthGroup = b(t.device, this.pipeline, e), this.sampledDepth = n;
|
|
512
|
+
}
|
|
513
|
+
S(t, this.pipeline, this.depthGroup, Math.ceil(r / 64));
|
|
514
|
+
let i = this.read, a = this.generation, o = this.depthRequests;
|
|
515
|
+
for (let e = 0; e < r; e++) {
|
|
516
|
+
let t = this.slots[e];
|
|
517
|
+
if (!t) {
|
|
518
|
+
o[e] = void 0;
|
|
519
|
+
continue;
|
|
520
|
+
}
|
|
521
|
+
let n = o[e];
|
|
522
|
+
n || (o[e] = n = {
|
|
523
|
+
record: t,
|
|
524
|
+
x: 0,
|
|
525
|
+
y: 0,
|
|
526
|
+
z: 0
|
|
527
|
+
}), n.record = t, n.x = t.x, n.y = t.y, n.z = t.z;
|
|
528
|
+
}
|
|
529
|
+
o.length = r, t.encoder.copyBufferToBuffer(this.output, 0, i, 0, r * 4), this.pending = !0, queueMicrotask(async () => {
|
|
530
|
+
try {
|
|
531
|
+
if (await i.mapAsync(GPUMapMode.READ), this.disposed || a !== this.generation) return;
|
|
532
|
+
let e = new Uint32Array(i.getMappedRange(0, r * 4)), t = this.fade, n = t ? Math.max(4, t.radius) : 4, s = t ? t.samples * t.samples : 1;
|
|
533
|
+
for (let i = 0; i < r; i++) {
|
|
534
|
+
let r = o[i];
|
|
535
|
+
if (!r || this.slots[i] !== r.record) continue;
|
|
536
|
+
let a = r.record, c = Math.abs(r.x - a.x) + Math.abs(r.y - a.y) < n && Math.abs(r.z - a.z) < .001;
|
|
537
|
+
t ? a.target = c ? 1 - Math.min(e[i], s) / s : 1 : a.hidden = c && !!e[i];
|
|
538
|
+
}
|
|
539
|
+
} catch {
|
|
540
|
+
for (let e of this.records.values()) e.hidden = !1, e.target = 1;
|
|
541
|
+
} finally {
|
|
542
|
+
i.mapState === "mapped" && i.unmap(), a === this.generation && (this.pending = !1);
|
|
543
|
+
}
|
|
544
|
+
});
|
|
545
|
+
}
|
|
546
|
+
query(e, t, n) {
|
|
547
|
+
let r = this.glOcclusion;
|
|
548
|
+
r && r.gl !== e && this.releaseGL(), r = this.glOcclusion ??= k(e);
|
|
549
|
+
let i = this.fade, a = i ? i.samples : 1, o = a * a, s = i ? Math.max(4, i.radius) : 4, c = this.runtime?.canvas, l = c ? c.clientWidth || c.width : 1, u = c ? c.clientHeight || c.height : 1, [d, f, p, m] = n ?? [
|
|
550
|
+
0,
|
|
551
|
+
0,
|
|
552
|
+
e.drawingBufferWidth,
|
|
553
|
+
e.drawingBufferHeight
|
|
554
|
+
], h = p * l / e.drawingBufferWidth, g = m * u / e.drawingBufferHeight, _ = i?.radius ?? 0, v = this.settings.depthBias ?? 1e-4, y = !1;
|
|
555
|
+
for (let n = 0; n < this.used; n++) {
|
|
556
|
+
let c = this.slots[n], l = r.slots[n];
|
|
557
|
+
if (l?.pending) {
|
|
558
|
+
let t = !0;
|
|
559
|
+
for (let n = 0; n < o && t; n++) t = !l.drawn[n] || !!e.getQueryParameter(l.queries[n], e.QUERY_RESULT_AVAILABLE);
|
|
560
|
+
if (!t) continue;
|
|
561
|
+
if (l.pending = !1, c && l.record === c) {
|
|
562
|
+
let t = 0;
|
|
563
|
+
for (let n = 0; n < o; n++) l.drawn[n] && !e.getQueryParameter(l.queries[n], e.QUERY_RESULT) && t++;
|
|
564
|
+
let n = Math.abs(l.x - c.x) + Math.abs(l.y - c.y) < s && Math.abs(l.z - c.z) < .001;
|
|
565
|
+
i ? c.target = n ? 1 - t / o : 1 : c.hidden = n && t > 0;
|
|
566
|
+
}
|
|
567
|
+
}
|
|
568
|
+
if (!c || !c.label.visible || !c.label.screen.inFront || !c.label.screen.inFrustum) continue;
|
|
569
|
+
if (!l) {
|
|
570
|
+
l = r.slots[n] = {
|
|
571
|
+
queries: [],
|
|
572
|
+
drawn: [],
|
|
573
|
+
pending: !1,
|
|
574
|
+
x: 0,
|
|
575
|
+
y: 0,
|
|
576
|
+
z: 0
|
|
577
|
+
};
|
|
578
|
+
for (let t = 0; t < o; t++) l.queries.push(e.createQuery());
|
|
579
|
+
}
|
|
580
|
+
y || (y = !0, e.viewport(d, e.drawingBufferHeight - f - m, p, m), e.useProgram(r.program), e.bindVertexArray(r.vao), e.colorMask(!1, !1, !1, !1), e.depthMask(!1), e.enable(e.DEPTH_TEST), e.depthFunc(e.LESS), e.uniformMatrix4fv(r.camera, !1, t), e.uniform1f(r.bias, v));
|
|
581
|
+
let u = c.label.world;
|
|
582
|
+
e.uniform3f(r.world, u[0], u[1], u[2]);
|
|
583
|
+
for (let t = 0; t < o; t++) {
|
|
584
|
+
let n = a > 1 ? (t % a + .5) * 2 / a - 1 : 0, i = a > 1 ? (Math.floor(t / a) + .5) * 2 / a - 1 : 0, o = c.x + n * _, s = c.y + i * _;
|
|
585
|
+
l.drawn[t] = o >= 0 && o < h && s >= 0 && s < g, l.drawn[t] && (e.beginQuery(e.ANY_SAMPLES_PASSED, l.queries[t]), e.uniform2f(r.offset, n * _ / h * 2, -(i * _) / g * 2), e.drawArrays(e.POINTS, 0, 1), e.endQuery(e.ANY_SAMPLES_PASSED));
|
|
586
|
+
}
|
|
587
|
+
l.pending = !0, l.record = c, l.x = c.x, l.y = c.y, l.z = c.z;
|
|
588
|
+
}
|
|
589
|
+
y && (e.colorMask(!0, !0, !0, !0), e.depthMask(!0), e.bindVertexArray(null), e.useProgram(null));
|
|
590
|
+
}
|
|
591
|
+
releaseGL() {
|
|
592
|
+
let e = this.glOcclusion;
|
|
593
|
+
if (this.glOcclusion = void 0, e && !e.gl.isContextLost()) {
|
|
594
|
+
for (let t of e.slots) for (let n of t?.queries ?? []) e.gl.deleteQuery(n);
|
|
595
|
+
e.gl.deleteVertexArray(e.vao), e.gl.deleteProgram(e.program);
|
|
596
|
+
}
|
|
597
|
+
}
|
|
598
|
+
release(e = !0) {
|
|
599
|
+
this.releaseGL(), this.generation++, this.pending = !1, e && (this.shared?.release(), this.shared = void 0), this.input?.destroy(), this.output?.destroy(), this.read?.destroy(), this.radius?.destroy(), this.input = this.output = this.read = this.radius = void 0, this.gpuSlots = 0, this.pipeline = void 0, this.sampledDepth = this.depthGroup = void 0;
|
|
600
|
+
}
|
|
601
|
+
onDeviceRestored() {
|
|
602
|
+
this.release(!1);
|
|
603
|
+
for (let e of this.records.values()) e.hidden = !1, e.target = 1;
|
|
604
|
+
this.setupGPU();
|
|
605
|
+
}
|
|
606
|
+
dispose() {
|
|
607
|
+
this.disposed || (this.disposed = !0, this.release(), super.dispose(), this.records.clear(), this.slots.length = 0, this.runtime = void 0);
|
|
608
|
+
}
|
|
609
|
+
[Symbol.dispose ?? Symbol()]() {
|
|
610
|
+
this.dispose();
|
|
611
|
+
}
|
|
612
|
+
};
|
|
613
|
+
//#endregion
|
|
614
|
+
export { j as HTML };
|
package/dist/inline.d.ts
ADDED
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
import type { HTMLResourceInliner } from './texture.js';
|
|
2
|
+
/** Options for a self-contained foreignObject snapshot. No network operation starts until called. */
|
|
3
|
+
export interface InlineResourceOptions {
|
|
4
|
+
signal?: AbortSignal;
|
|
5
|
+
/** Whole snapshot's network/read deadline in milliseconds, default 15000. */
|
|
6
|
+
timeout?: number;
|
|
7
|
+
/** Explicit @font-face CSS instead of document-sheet discovery; URLs resolve against the element's baseURI. */
|
|
8
|
+
fontCSS?: string;
|
|
9
|
+
}
|
|
10
|
+
/** Fetches same-origin/CORS images and fonts into a styled detached copy; never mutates the live source. */
|
|
11
|
+
export declare function inlineResources(element: HTMLElement, options?: InlineResourceOptions): Promise<HTMLElement>;
|
|
12
|
+
/** Creates HTMLTexture's prepare hook, combining caller cancellation with surface removal. */
|
|
13
|
+
export declare function resourceInliner(options?: InlineResourceOptions): HTMLResourceInliner;
|