@margoengine/transitions 0.0.0-stage → 0.1.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.
@@ -0,0 +1,276 @@
1
+ //#region ../shared/view-rect.js
2
+ function e(e) {
3
+ return e?.view?.viewport;
4
+ }
5
+ function t(t, n) {
6
+ let r = e(n);
7
+ if (!r) return;
8
+ t.setViewport(r[0], r[1], r[2], r[3], 0, 1);
9
+ let i = n.view.scissor ?? r;
10
+ t.setScissorRect(i[0], i[1], i[2], i[3]);
11
+ }
12
+ function n(e, t = "clear") {
13
+ return e?.view?.index ? "load" : t;
14
+ }
15
+ //#endregion
16
+ //#region ../shared/frame-resources.js
17
+ var r = (globalThis[Symbol.for("margoengine.frame-resources")] ??= { devices: /* @__PURE__ */ new WeakMap() }).devices;
18
+ function i(e) {
19
+ let t = r.get(e);
20
+ return t || (r.set(e, t = {
21
+ frame: 0,
22
+ open: !1,
23
+ lost: !1,
24
+ views: /* @__PURE__ */ new Set(),
25
+ drawing: 0,
26
+ retired: [],
27
+ written: /* @__PURE__ */ new WeakMap()
28
+ }), e.lost?.then?.(() => {
29
+ t.lost = !0;
30
+ let e = t.retired;
31
+ t.retired = [];
32
+ for (let t of e) t.destroy();
33
+ })), t;
34
+ }
35
+ function a(e, t) {
36
+ if (!t) return;
37
+ let n = i(e), r = [t, ...n.written.get(t)?.extras ?? []];
38
+ if (n.written.delete(t), !n.lost && n.open && (n.views.size > 1 || n.drawing > 0)) n.retired.push(...r);
39
+ else for (let e of r) e.destroy();
40
+ }
41
+ function o(e) {
42
+ return e?.adapterInfo?.vendor === "margo-webgl2" ? "webgl2" : "native";
43
+ }
44
+ function s(e) {
45
+ return o(e) === "native" && e?.features?.has("core-features-and-limits") === !1;
46
+ }
47
+ //#endregion
48
+ //#region ../shared/msaa.js
49
+ var c = "@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);}", l = (e, t, n) => e.createBindGroup({
50
+ layout: t.getBindGroupLayout(0),
51
+ entries: [{
52
+ binding: 0,
53
+ resource: n
54
+ }]
55
+ }), u = Symbol.for("margoengine.optional.msaa"), d = c + "\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}";
56
+ function f(e, t, n, r, i, a = !1) {
57
+ let o = (e) => d.replace(/return max[^;]+;/, `return textureLoad(source,q,${e});`), c = (n, r) => {
58
+ let a = e.createShaderModule({ code: n }), o = e.createRenderPipeline({
59
+ layout: "auto",
60
+ vertex: {
61
+ module: a,
62
+ entryPoint: "vertex"
63
+ },
64
+ fragment: {
65
+ module: a,
66
+ entryPoint: "fragment",
67
+ targets: []
68
+ },
69
+ depthStencil: {
70
+ format: i,
71
+ depthWriteEnabled: !0,
72
+ depthCompare: "always"
73
+ },
74
+ multisample: r
75
+ });
76
+ return {
77
+ pipeline: o,
78
+ group: l(e, o, t)
79
+ };
80
+ };
81
+ return {
82
+ draws: a ? [c(o(0).replace("_multisampled", ""), { count: r })] : r === 4 ? s(e) ? [
83
+ 0,
84
+ 1,
85
+ 2,
86
+ 3
87
+ ].map((e) => c(o(e), {
88
+ count: r,
89
+ mask: 1 << e
90
+ })) : [c(o("i32(sample)").replace("p:vec4f)", "p:vec4f,@builtin(sample_index) sample:u32)"), { count: r })] : [c(d, { count: r })],
91
+ descriptor: {
92
+ colorAttachments: [],
93
+ depthStencilAttachment: {
94
+ view: n,
95
+ depthLoadOp: "clear",
96
+ depthClearValue: 0,
97
+ depthStoreOp: "store",
98
+ stencilReadOnly: !0
99
+ }
100
+ }
101
+ };
102
+ }
103
+ var p = class {
104
+ constructor(e, t, n, r = 4) {
105
+ if (r !== 1 && r !== 4) throw RangeError("Scene samples must be 1 or 4");
106
+ this.samples = r, this.device = e, this.resolvedColor = t, this.resolvedDepth = n, this.textures = [], this.pipelines = /* @__PURE__ */ new Map();
107
+ try {
108
+ if (r === 1) {
109
+ this.color = t.createView(), this.depth = n.createView();
110
+ return;
111
+ }
112
+ for (let r of [t, n]) this.textures.push(e.createTexture({
113
+ size: [r.width, r.height],
114
+ format: r.format,
115
+ sampleCount: 4,
116
+ usage: GPUTextureUsage.RENDER_ATTACHMENT | GPUTextureUsage.TEXTURE_BINDING
117
+ }));
118
+ this.color = this.textures[0].createView(), this.depth = this.textures[1].createView(), this.resolution = { colorAttachments: [{
119
+ view: this.color,
120
+ resolveTarget: t.createView(),
121
+ loadOp: "load",
122
+ storeOp: "store"
123
+ }] }, this.finalResolution = {
124
+ colorAttachments: [{
125
+ ...this.resolution.colorAttachments[0],
126
+ storeOp: "discard"
127
+ }],
128
+ depthStencilAttachment: {
129
+ view: this.depth,
130
+ depthLoadOp: "load",
131
+ depthStoreOp: "discard",
132
+ stencilReadOnly: !0
133
+ }
134
+ }, this.depthSource = this.textures[1].createView({ aspect: "depth-only" }), this.resolvedDepthView = n.createView();
135
+ } catch (e) {
136
+ throw this.dispose(), e;
137
+ }
138
+ }
139
+ begin(e) {
140
+ (this.viewContext ??= {}).view = e.view, Object.assign(this.original ??= {}, {
141
+ color: e.color,
142
+ depth: e.depth,
143
+ format: e.format,
144
+ descriptor: e.descriptor,
145
+ owner: m(e)
146
+ }), e.color = this.color, e.depth = this.depth, e.format = this.resolvedColor.format, this.baseDescriptor !== e.descriptor && (this.baseDescriptor = e.descriptor, this.descriptor = {
147
+ ...e.descriptor,
148
+ depthStencil: {
149
+ ...e.descriptor.depthStencil,
150
+ format: this.resolvedDepth.format
151
+ },
152
+ multisample: { count: this.samples }
153
+ }), e.descriptor = this.descriptor, e[u] = this, this.retired = !1;
154
+ }
155
+ copyDepth(e, n, r, i = this.resolvedDepth.format) {
156
+ let a = this.pipelines.get(r);
157
+ a || this.pipelines.set(r, a = /* @__PURE__ */ new Map());
158
+ let o = a.get(i);
159
+ o || (this.depthSource ??= this.resolvedDepth.createView({ aspect: "depth-only" }), a.set(i, o = f(this.device, this.depthSource, n, r, i, this.samples === 1))), o.descriptor.depthStencilAttachment.view = n, o.descriptor.depthStencilAttachment.depthLoadOp = this.viewContext?.view ? "load" : "clear";
160
+ let s = e.beginRenderPass(o.descriptor);
161
+ this.viewContext && t(s, this.viewContext);
162
+ let c = o.draws;
163
+ for (let e = 0; e < c.length; e++) s.setPipeline(c[e].pipeline), s.setBindGroup(0, c[e].group), s.draw(3);
164
+ s.end();
165
+ }
166
+ resolve(e, t = !1, n = !0) {
167
+ if (this.samples !== 1) {
168
+ if (n && this.copyDepth(e, this.resolvedDepthView, 1), t) {
169
+ let t = this.viewContext?.view ? "store" : "discard";
170
+ this.finalResolution.colorAttachments[0].storeOp = t, this.finalResolution.depthStencilAttachment.depthStoreOp = t, this.original && this.copyDepth(e, this.original.depth, this.original.descriptor.multisample?.count ?? 1, this.original.descriptor.depthStencil.format), this.retired = !0;
171
+ }
172
+ e.beginRenderPass(t ? this.finalResolution : this.resolution).end();
173
+ }
174
+ }
175
+ restore(e) {
176
+ let t = this.original;
177
+ this.samples === 1 ? (Object.assign(e, {
178
+ color: t.color,
179
+ depth: t.depth,
180
+ format: t.format,
181
+ descriptor: t.descriptor
182
+ }), e[u] = t.owner) : (this.retired || this.copyDepth(e.encoder, t.depth, t.descriptor.multisample?.count ?? 1, t.descriptor.depthStencil.format), Object.assign(e, {
183
+ color: t.color,
184
+ depth: t.depth,
185
+ format: t.format,
186
+ descriptor: t.descriptor
187
+ }), e[u] = t.owner);
188
+ }
189
+ present(e) {
190
+ this.resolve(e.encoder, !0), this.restore(e);
191
+ let r = e.descriptor.multisample?.count ?? 1;
192
+ if (!this.presentation || this.presentationFormat !== e.format || this.presentationSamples !== r) {
193
+ let t = this.device.createShaderModule({ code: c + "@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);}" });
194
+ this.presentation = this.device.createRenderPipeline({
195
+ layout: "auto",
196
+ vertex: {
197
+ module: t,
198
+ entryPoint: "vertex"
199
+ },
200
+ fragment: {
201
+ module: t,
202
+ entryPoint: "fragment",
203
+ targets: [{ format: e.format }]
204
+ },
205
+ multisample: e.descriptor.multisample
206
+ }), this.presentationGroup = l(this.device, this.presentation, this.resolvedColor.createView()), this.presentationFormat = e.format, this.presentationSamples = r;
207
+ }
208
+ let i = this.presentationPass ??= { colorAttachments: [{
209
+ view: e.color,
210
+ loadOp: "clear",
211
+ storeOp: "store"
212
+ }] };
213
+ i.colorAttachments[0].view = e.color, i.colorAttachments[0].loadOp = n(e, "clear");
214
+ let a = e.encoder.beginRenderPass(i);
215
+ a.setPipeline(this.presentation), a.setBindGroup(0, this.presentationGroup), t(a, e), a.draw(3), a.end();
216
+ }
217
+ dispose() {
218
+ for (let e of this.textures) a(this.device, e);
219
+ this.textures = [], this.pipelines.clear();
220
+ }
221
+ };
222
+ function m(e) {
223
+ let t = e[u];
224
+ return t?.color === e.color && t.depth === e.depth ? t : void 0;
225
+ }
226
+ //#endregion
227
+ //#region ../shared/gpu.js
228
+ var h = "\nstruct Vertex { @builtin(position) position:vec4f,@location(0) uv:vec2f }\n@vertex fn vertex(@builtin(vertex_index) index:u32)->Vertex {\n let p=array<vec2f,3>(vec2f(-1,-1),vec2f(3,-1),vec2f(-1,3))[index];\n return Vertex(vec4f(p,0,1),vec2f(p.x*0.5+0.5,0.5-p.y*0.5));\n}";
229
+ function g(e, t, n) {
230
+ return e.createBindGroup({
231
+ layout: t.getBindGroupLayout(0),
232
+ entries: n.map((e, t) => ({
233
+ binding: t,
234
+ resource: e
235
+ }))
236
+ });
237
+ }
238
+ function _(e, t, n, r = {}) {
239
+ let i = e.createShaderModule({ code: t });
240
+ return e.createRenderPipeline({
241
+ layout: "auto",
242
+ vertex: {
243
+ module: i,
244
+ entryPoint: "vertex"
245
+ },
246
+ fragment: {
247
+ module: i,
248
+ entryPoint: "fragment",
249
+ targets: [{ format: n }]
250
+ },
251
+ ...r
252
+ });
253
+ }
254
+ function v(e, r, i, a = "load") {
255
+ let o = e.encoder.beginRenderPass({ colorAttachments: [{
256
+ view: e.color,
257
+ loadOp: n(e, a),
258
+ storeOp: "store"
259
+ }] });
260
+ o.setPipeline(r), o.setBindGroup(0, i), t(o, e), o.draw(3), o.end();
261
+ }
262
+ //#endregion
263
+ //#region src/builtin.ts
264
+ var y = [
265
+ "wipe",
266
+ "dissolve",
267
+ "radial",
268
+ "chromatic"
269
+ ], b = h + "\n@group(0) @binding(0) var<uniform> settings:vec4f;\n@group(0) @binding(1) var before:texture_2d<f32>;\n@group(0) @binding(2) var after:texture_2d<f32>;\nfn sample(t:texture_2d<f32>,uv:vec2f)->vec4f {let size=vec2i(textureDimensions(t));return textureLoad(t,clamp(vec2i(uv*vec2f(size)),vec2i(0),size-1),0);}\n\n\nfn dissolvePcg(v:u32)->u32 {let s=v*747796405u+2891336453u;let w=((s>>((s>>28u)+4u))^s)*277803737u;return (w>>22u)^w;}\nfn dissolveThreshold(pixel:vec2u)->f32 {return f32(dissolvePcg(pixel.x+dissolvePcg(pixel.y))>>8u)*5.9604645e-8;}\n@fragment fn fragment(i:Vertex)->@location(0) vec4f {\n let uv=i.position.xy/vec2f(textureDimensions(after));\n let p=settings.x;if(p<=0.0){return sample(before,uv);}if(p>=1.0){return sample(after,uv);}\n if(settings.y==4.0){return mix(sample(before,uv),sample(after,uv),p);}\n var edge=uv.x;\n if(settings.y==1.0){edge=dissolveThreshold(vec2u(i.position.xy));}\n if(settings.y==2.0){edge=fract(atan2(uv.x-0.5,0.5-uv.y)/6.2831853+1.0);}\n if(settings.y==3.0){let shift=vec2f(sin(p*3.14159265)*settings.z,0);let a=sample(after,uv+shift);let b=sample(after,uv-shift);return mix(sample(before,uv),vec4f(a.r,sample(after,uv).g,b.b,1),p);}\n return mix(sample(before,uv),sample(after,uv),1.0-smoothstep(p-0.01,p+0.01,edge));\n}";
270
+ //#endregion
271
+ //#region src/formats.ts
272
+ function x(e, t) {
273
+ return /^(?:(?:r|rg|rgba)(?:8|16|32)(?:unorm|snorm|float)(?:-srgb)?|bgra8unorm(?:-srgb)?|rgb10a2unorm|rg11b10ufloat)$/.test(e) && !(t > 1 && /32float$/.test(e));
274
+ }
275
+ //#endregion
276
+ export { h as a, p as c, v as i, y as n, g as o, b as r, _ as s, x as t };
@@ -0,0 +1 @@
1
+ export declare function floatOutput(format: GPUTextureFormat, samples: number): boolean;
package/dist/glsl.d.ts ADDED
@@ -0,0 +1,23 @@
1
+ import type { EngineKernel, Scene } from '@margoengine/core';
2
+ import { type CustomTransitionOptions } from './custom.js';
3
+ import { LiveTransitions, type LiveTransitionOptions } from './live.js';
4
+ import type { Transitions } from './index.js';
5
+ /** A scalar or flattened vector, column-major matrix or fixed array uniform value. */
6
+ export type GLSLUniformValue = number | boolean | readonly (number | boolean)[];
7
+ /** Values and coordinate convention for importing a GLSL transition. */
8
+ export interface GLSLTransitionOptions {
9
+ /** Additional uniform values, snapshotted into WGSL constants. Default values come from // = JSON annotations. */
10
+ uniforms?: Readonly<Record<string, GLSLUniformValue>>;
11
+ /** GLSL UVs use bottom-left origin by default; false preserves the custom entry's top-left origin. Default true. */
12
+ flipY?: boolean;
13
+ }
14
+ /**
15
+ * Translates trusted GLSL ES 1.00 or 3.00 (`#version 300 es`) containing vec4 transition(vec2 uv) to the custom WGSL function contract.
16
+ * Exposes progress, ratio, getFromColor/getToColor, and from/to sampler2D aliases. No external shader assets are bundled.
17
+ * Syntax/type diagnostics carry code failed; asynchronous factory compilation validates the emitted WGSL before attachment.
18
+ */
19
+ export declare function translateGLSLTransition(source: string, options?: GLSLTransitionOptions): string;
20
+ /** Compiles and attaches a frozen GLSL transition. Call start('wipe',seconds); supplied GLSL controls the treatment. */
21
+ export declare function createGLSLTransition(engine: EngineKernel, source: string, options?: GLSLTransitionOptions & CustomTransitionOptions): Promise<Transitions>;
22
+ /** Compiles and attaches a GLSL transition between borrowed live scenes and cameras. */
23
+ export declare function createLiveGLSLTransition(engine: EngineKernel, before: Scene, after: Scene, source: string, options?: GLSLTransitionOptions & Pick<LiveTransitionOptions, 'beforeCamera' | 'afterCamera'> & CustomTransitionOptions): Promise<LiveTransitions>;