pixi-silk 1.0.2 → 1.2.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/CHANGELOG.md +18 -0
- package/README.md +3 -2
- package/dist/SilkGraphics.d.ts +2 -1
- package/dist/SilkGraphics.js +6 -1
- package/dist/app.d.ts +8 -1
- package/dist/app.js +5 -1
- package/dist/batch.d.ts +2 -1
- package/dist/batch.js +4 -3
- package/dist/glass/GlassFilter.js +2 -1
- package/dist/shader-slot.js +3 -1
- package/dist/shader.js +4 -2
- package/dist/text/SilkText.d.ts +143 -6
- package/dist/text/SilkText.js +264 -35
- package/dist/text/atlas.d.ts +52 -2
- package/dist/text/atlas.js +231 -27
- package/dist/text/bidi.d.ts +36 -0
- package/dist/text/bidi.js +632 -0
- package/dist/text/cluster.d.ts +37 -0
- package/dist/text/cluster.js +277 -0
- package/dist/text/label.d.ts +20 -0
- package/dist/text/label.js +27 -1
- package/dist/text/layout.d.ts +162 -11
- package/dist/text/layout.js +541 -58
- package/dist/text/rich.d.ts +84 -0
- package/dist/text/rich.js +218 -0
- package/dist/text/shader.js +35 -5
- package/dist/text.d.ts +7 -4
- package/dist/text.js +6 -3
- package/dist/ui/textedit.d.ts +17 -2
- package/dist/ui/textedit.js +55 -18
- package/dist/webgl1/glsl100.js +123 -0
- package/dist/webgl1/hooks.js +137 -0
- package/dist/webgl1/programs.js +244 -0
- package/dist/webgl1.d.ts +41 -0
- package/dist/webgl1.js +104 -0
- package/package.json +14 -12
- package/skills/pixi-silk/SKILL.md +1 -0
- package/skills/pixi-silk-text/SKILL.md +57 -2
package/dist/ui/textedit.js
CHANGED
|
@@ -3,9 +3,10 @@ import { Spring, damp } from "../motion.js";
|
|
|
3
3
|
import { signal } from "../signal.js";
|
|
4
4
|
import { History } from "../history.js";
|
|
5
5
|
import { Driver } from "./driver.js";
|
|
6
|
-
import { caretAtPoint, lineOfIndex } from "../text/layout.js";
|
|
6
|
+
import { caretAtPoint, caretVisual, inlineTokens, lineOfIndex } from "../text/layout.js";
|
|
7
7
|
import { SilkText } from "../text/SilkText.js";
|
|
8
|
-
import {
|
|
8
|
+
import { InlinePlacer } from "../text/rich.js";
|
|
9
|
+
import { graphemeStops, isMac, nextWordStop, paragraphAt, previousWordStop, wordAt, wordSegments } from "../text/segment.js";
|
|
9
10
|
import { Container, Graphics, Matrix, Rectangle } from "pixi.js";
|
|
10
11
|
//#region src/ui/textedit.ts
|
|
11
12
|
/** Default colours of a {@link TextEditor}: a blue caret and highlight. Override single values with the `theme` option. */
|
|
@@ -66,7 +67,9 @@ const scratch = new Matrix();
|
|
|
66
67
|
* Events: `focus`, `blur`, `input` (every edit), `change` (on commit, when the text changed), `submit`
|
|
67
68
|
* (Enter in a single-line editor) and `tab`. The same values are on {@link TextEditor.state}.
|
|
68
69
|
*
|
|
69
|
-
*
|
|
70
|
+
* Right-to-left and mixed-direction text edits in place: the arrow keys move the way they point on
|
|
71
|
+
* screen, and a selection across a change of direction shows as several boxes. `{name}` tokens show
|
|
72
|
+
* inline objects (see `inlines`). Mixed styles in one editor are not supported.
|
|
70
73
|
*
|
|
71
74
|
* Experimental: the API may change in a minor release.
|
|
72
75
|
*
|
|
@@ -131,6 +134,8 @@ var TextEditor = class TextEditor extends Container {
|
|
|
131
134
|
_anchor = 0;
|
|
132
135
|
_focus = 0;
|
|
133
136
|
_upstream = false;
|
|
137
|
+
/** The caret goes with the character after it, where one offset has two places on screen. */
|
|
138
|
+
_lead = false;
|
|
134
139
|
_prefX = null;
|
|
135
140
|
_stops = [0];
|
|
136
141
|
_stopSet = /* @__PURE__ */ new Set([0]);
|
|
@@ -164,9 +169,10 @@ var TextEditor = class TextEditor extends Container {
|
|
|
164
169
|
_keepFocus = false;
|
|
165
170
|
_focusedAt = -Infinity;
|
|
166
171
|
_mac = isMac();
|
|
172
|
+
_placer;
|
|
167
173
|
constructor(options = {}) {
|
|
168
174
|
super({ label: "TextEditor" });
|
|
169
|
-
const { text = "", multiline = true, width, placeholder = "", maxLength = Infinity, readOnly = false, selectable = true, enter, tab = "next", history, element, ariaLabel, handles = true, theme, caretWidth = 1.5, ticker, ...style } = options;
|
|
175
|
+
const { text = "", multiline = true, width, placeholder = "", maxLength = Infinity, readOnly = false, selectable = true, enter, tab = "next", history, element, ariaLabel, handles = true, theme, caretWidth = 1.5, ticker, inlines = {}, ...style } = options;
|
|
170
176
|
this._multiline = multiline;
|
|
171
177
|
this._width = width;
|
|
172
178
|
this._readOnly = readOnly;
|
|
@@ -198,6 +204,7 @@ var TextEditor = class TextEditor extends Container {
|
|
|
198
204
|
wordWrapWidth: width
|
|
199
205
|
});
|
|
200
206
|
this.placeholderObject.alpha = this.theme.placeholderAlpha;
|
|
207
|
+
this._placer = new InlinePlacer(this.textObject, inlines);
|
|
201
208
|
this.textObject.eventMode = "none";
|
|
202
209
|
this.placeholderObject.eventMode = "none";
|
|
203
210
|
this._knobs.eventMode = "none";
|
|
@@ -245,6 +252,16 @@ var TextEditor = class TextEditor extends Container {
|
|
|
245
252
|
set value(v) {
|
|
246
253
|
this._sig.text.value = this._clean(String(v));
|
|
247
254
|
}
|
|
255
|
+
/** Objects shown where `{name}` appears in the text. Set a new object to change them. */
|
|
256
|
+
get inlines() {
|
|
257
|
+
return this._placer.defs;
|
|
258
|
+
}
|
|
259
|
+
/** Sets {@link TextEditor.inlines}. */
|
|
260
|
+
set inlines(v) {
|
|
261
|
+
this._placer.defs = v;
|
|
262
|
+
this._stopsFor();
|
|
263
|
+
this.redraw();
|
|
264
|
+
}
|
|
248
265
|
/** True while the editor has keyboard focus. */
|
|
249
266
|
get focused() {
|
|
250
267
|
return this._sig.focused.value;
|
|
@@ -405,11 +422,25 @@ var TextEditor = class TextEditor extends Container {
|
|
|
405
422
|
switch (key) {
|
|
406
423
|
case "ArrowLeft":
|
|
407
424
|
case "ArrowRight": {
|
|
408
|
-
const
|
|
425
|
+
const layout = this.textObject.layout;
|
|
426
|
+
const rtl = layout.lines[lineOfIndex(layout, this._focus, this._upstream)]?.rtl;
|
|
427
|
+
const screen = key === "ArrowLeft" ? -1 : 1;
|
|
428
|
+
const dir = screen * (rtl ? -1 : 1);
|
|
409
429
|
if (mac && e.metaKey) this._lineMove(dir, extend);
|
|
410
|
-
else if (byWord)
|
|
411
|
-
|
|
412
|
-
|
|
430
|
+
else if (byWord) {
|
|
431
|
+
const li = lineOfIndex(layout, this._focus, this._upstream);
|
|
432
|
+
const edges = new Set(wordSegments(text).flatMap((w) => w.word ? [w.start, w.end] : []));
|
|
433
|
+
const seen = layout.lines[li].runs ? caretVisual(layout, this._focus, screen, this._upstream, (i) => edges.has(i), this._lead) : null;
|
|
434
|
+
if (seen) this._moveTo(seen.index, extend, false, false, seen.leading);
|
|
435
|
+
else this._moveTo(dir < 0 ? previousWordStop(text, this._focus) : nextWordStop(text, this._focus, mac), extend);
|
|
436
|
+
} else if (!extend && this._anchor !== this._focus) this._moveTo(dir < 0 ? this.selectionStart : this.selectionEnd, false);
|
|
437
|
+
else {
|
|
438
|
+
const li = lineOfIndex(layout, this._focus, this._upstream);
|
|
439
|
+
const seen = layout.lines[li].runs ? caretVisual(layout, this._focus, screen, this._upstream, (i) => this._stopSet.has(i), this._lead) : null;
|
|
440
|
+
const next = this._step1(this._focus, dir);
|
|
441
|
+
if (seen !== null) this._moveTo(seen.index, extend, false, false, seen.leading);
|
|
442
|
+
else if (!layout.lines[li].runs || lineOfIndex(layout, next) !== li) this._moveTo(next, extend);
|
|
443
|
+
}
|
|
413
444
|
return true;
|
|
414
445
|
}
|
|
415
446
|
case "ArrowUp":
|
|
@@ -482,10 +513,13 @@ var TextEditor = class TextEditor extends Container {
|
|
|
482
513
|
const box = this._width ?? 0;
|
|
483
514
|
this.placeholderObject.visible = empty;
|
|
484
515
|
if (this._width !== void 0 && this._multiline) {
|
|
485
|
-
const
|
|
516
|
+
const rtl = layout.lines[0]?.rtl ?? false;
|
|
517
|
+
const align = t.align === "start" || t.align === "justify" ? rtl ? "right" : "left" : t.align === "end" ? rtl ? "left" : "right" : t.align;
|
|
518
|
+
const shift = align === "center" ? (box - layout.width) / 2 : align === "right" ? box - layout.width : 0;
|
|
486
519
|
t.x = shift;
|
|
487
520
|
this.placeholderObject.x = shift;
|
|
488
521
|
}
|
|
522
|
+
this._placer.sync();
|
|
489
523
|
const k = this._screenScale();
|
|
490
524
|
const off = t.layoutOffset;
|
|
491
525
|
const w = Math.max(box, layout.width, empty ? this.placeholderObject.layout.width : 0, t.fontSize * .5);
|
|
@@ -507,6 +541,7 @@ var TextEditor = class TextEditor extends Container {
|
|
|
507
541
|
this._off.length = 0;
|
|
508
542
|
this._field?.remove();
|
|
509
543
|
this._field = null;
|
|
544
|
+
this._placer.destroy();
|
|
510
545
|
super.destroy(options);
|
|
511
546
|
}
|
|
512
547
|
/** Advances the caret blink and animations by `dt` seconds. Returns true while it still moves. */
|
|
@@ -518,7 +553,8 @@ var TextEditor = class TextEditor extends Container {
|
|
|
518
553
|
return this._multiline ? s : s.replace(/\n/g, " ");
|
|
519
554
|
}
|
|
520
555
|
_stopsFor() {
|
|
521
|
-
|
|
556
|
+
const tokens = inlineTokens(this._text, (n) => Object.hasOwn(this._placer.defs, n));
|
|
557
|
+
this._stops = graphemeStops(this._text).filter((o) => !tokens.some((t) => o > t.start && o < t.end));
|
|
522
558
|
this._stopSet = new Set(this._stops);
|
|
523
559
|
}
|
|
524
560
|
/** The nearest grapheme boundary at or before `i`. */
|
|
@@ -574,10 +610,11 @@ var TextEditor = class TextEditor extends Container {
|
|
|
574
610
|
}));
|
|
575
611
|
return true;
|
|
576
612
|
}
|
|
577
|
-
_setSelection(anchor, focus, upstream = false, keepX = false) {
|
|
613
|
+
_setSelection(anchor, focus, upstream = false, keepX = false, lead = false) {
|
|
578
614
|
this._anchor = anchor;
|
|
579
615
|
this._focus = focus;
|
|
580
616
|
this._upstream = upstream;
|
|
617
|
+
this._lead = lead;
|
|
581
618
|
if (!keepX) this._prefX = null;
|
|
582
619
|
this._idle = 0;
|
|
583
620
|
this._sig.selection.value = {
|
|
@@ -588,10 +625,10 @@ var TextEditor = class TextEditor extends Container {
|
|
|
588
625
|
this.redraw();
|
|
589
626
|
this._driver.wake();
|
|
590
627
|
}
|
|
591
|
-
_moveTo(i, extend, upstream = false, keepX = false) {
|
|
628
|
+
_moveTo(i, extend, upstream = false, keepX = false, lead = false) {
|
|
592
629
|
const f = this._snap(i);
|
|
593
630
|
this._session++;
|
|
594
|
-
this._setSelection(extend ? this._anchor : f, f, upstream, keepX);
|
|
631
|
+
this._setSelection(extend ? this._anchor : f, f, upstream, keepX, lead);
|
|
595
632
|
}
|
|
596
633
|
_lineMove(dir, extend) {
|
|
597
634
|
const layout = this.textObject.layout;
|
|
@@ -603,7 +640,7 @@ var TextEditor = class TextEditor extends Container {
|
|
|
603
640
|
_vertical(dir, extend) {
|
|
604
641
|
const t = this.textObject;
|
|
605
642
|
const layout = t.layout;
|
|
606
|
-
const r = t.caretRect(this._focus, this._upstream);
|
|
643
|
+
const r = t.caretRect(this._focus, this._upstream, this._lead);
|
|
607
644
|
const x = this._prefX ?? r.x;
|
|
608
645
|
const li = r.line + dir;
|
|
609
646
|
if (li < 0 || li >= layout.lines.length) {
|
|
@@ -818,7 +855,7 @@ var TextEditor = class TextEditor extends Container {
|
|
|
818
855
|
const f = this._field;
|
|
819
856
|
const el = this._element;
|
|
820
857
|
if (!f || !el?.getBoundingClientRect) return;
|
|
821
|
-
const r = this.textObject.caretRect(this._focus, this._upstream);
|
|
858
|
+
const r = this.textObject.caretRect(this._focus, this._upstream, this._lead);
|
|
822
859
|
const top = this.textObject.toGlobal({
|
|
823
860
|
x: r.x,
|
|
824
861
|
y: r.top
|
|
@@ -943,12 +980,12 @@ var TextEditor = class TextEditor extends Container {
|
|
|
943
980
|
p.origin = range;
|
|
944
981
|
}
|
|
945
982
|
this._session++;
|
|
946
|
-
if (unit === "char") this._setSelection(extend ? this._anchor : i, i, hit.upstream);
|
|
983
|
+
if (unit === "char") this._setSelection(extend ? this._anchor : i, i, hit.upstream, false, hit.leading);
|
|
947
984
|
else this._setSelection(this._snap(range.start), this._snap(range.end));
|
|
948
985
|
}
|
|
949
986
|
_extend(hit, unit, origin) {
|
|
950
987
|
if (unit === "char") {
|
|
951
|
-
this._setSelection(this._anchor, hit.index, hit.upstream);
|
|
988
|
+
this._setSelection(this._anchor, hit.index, hit.upstream, false, hit.leading);
|
|
952
989
|
return;
|
|
953
990
|
}
|
|
954
991
|
const r = unit === "word" ? wordAt(this._text, hit.before ? hit.index - 1 : hit.index) : paragraphAt(this._text, hit.index);
|
|
@@ -1034,7 +1071,7 @@ var TextEditor = class TextEditor extends Container {
|
|
|
1034
1071
|
const t = this.textObject;
|
|
1035
1072
|
const g = this._caret;
|
|
1036
1073
|
const show = this.focused && !this._readOnly && this._anchor === this._focus;
|
|
1037
|
-
const r = t.caretRect(this._focus, this._upstream);
|
|
1074
|
+
const r = t.caretRect(this._focus, this._upstream, this._lead);
|
|
1038
1075
|
if (moved) {
|
|
1039
1076
|
this._caretX.target = r.x;
|
|
1040
1077
|
this._caretY.target = r.top;
|
|
@@ -0,0 +1,123 @@
|
|
|
1
|
+
//#region src/webgl1/glsl100.ts
|
|
2
|
+
/**
|
|
3
|
+
* @internal Replaces every call `name(...)` with `fn(args)`. Arguments are split at the commas of
|
|
4
|
+
* the call itself, so nested calls and constructors stay whole.
|
|
5
|
+
*/
|
|
6
|
+
function rewriteCalls(src, name, fn) {
|
|
7
|
+
const re = new RegExp(`\\b${name}\\(`, "g");
|
|
8
|
+
let out = "";
|
|
9
|
+
let at = 0;
|
|
10
|
+
for (let m = re.exec(src); m !== null; m = re.exec(src)) {
|
|
11
|
+
const args = [];
|
|
12
|
+
let depth = 1;
|
|
13
|
+
let start = m.index + m[0].length;
|
|
14
|
+
let j = start;
|
|
15
|
+
for (; j < src.length && depth > 0; j++) {
|
|
16
|
+
const c = src[j];
|
|
17
|
+
if (c === "(") depth++;
|
|
18
|
+
else if (c === ")") {
|
|
19
|
+
depth--;
|
|
20
|
+
if (depth === 0) args.push(src.slice(start, j).trim());
|
|
21
|
+
} else if (c === "," && depth === 1) {
|
|
22
|
+
args.push(src.slice(start, j).trim());
|
|
23
|
+
start = j + 1;
|
|
24
|
+
}
|
|
25
|
+
}
|
|
26
|
+
if (depth !== 0) throw new Error(`pixi-silk/webgl1: unclosed call to ${name}`);
|
|
27
|
+
out += src.slice(at, m.index) + fn(args);
|
|
28
|
+
at = j;
|
|
29
|
+
re.lastIndex = j;
|
|
30
|
+
}
|
|
31
|
+
return out + src.slice(at);
|
|
32
|
+
}
|
|
33
|
+
/**
|
|
34
|
+
* @internal The build's shader minifier (`build/glsl.ts`): no comments, no indentation, one
|
|
35
|
+
* statement per line as written. The published sources come out of it, the sources in a test or a
|
|
36
|
+
* dev server do not, so the conversion runs both through it and patches never see the difference.
|
|
37
|
+
*/
|
|
38
|
+
function normalizeGlsl(source) {
|
|
39
|
+
return source.replace(/\/\*[\s\S]*?\*\//g, " ").replace(/\/\/[^\n]*/g, "").split("\n").map((line) => line.trim().replace(/\s+/g, " ")).filter(Boolean).join("\n");
|
|
40
|
+
}
|
|
41
|
+
function applyPatch(src, patch) {
|
|
42
|
+
const replace = patch.replace;
|
|
43
|
+
const find = typeof patch.find === "string" ? normalizeGlsl(patch.find) : patch.find;
|
|
44
|
+
if (!(typeof find === "string" ? src.includes(find) : src.search(find) >= 0)) throw new Error(`pixi-silk/webgl1: shader patch no longer matches: ${String(find).slice(0, 80)}`);
|
|
45
|
+
if (typeof find === "string") return src.split(find).join(replace);
|
|
46
|
+
if (find.global) find.lastIndex = 0;
|
|
47
|
+
return src.replace(find, replace);
|
|
48
|
+
}
|
|
49
|
+
const isConstant = (s) => /^-?\d+$/.test(s.trim());
|
|
50
|
+
/** @internal Rewrites a GLSL ES 3.00 stage as GLSL ES 1.00. Throws when something is left that 1.00 lacks. */
|
|
51
|
+
function toGlsl100(src, stage, options = {}) {
|
|
52
|
+
src = normalizeGlsl(src);
|
|
53
|
+
for (const patch of options.patches ?? []) src = applyPatch(src, patch);
|
|
54
|
+
src = src.replace(/^#version 300 es\n/, "");
|
|
55
|
+
src = src.replace(/\((\w+) >> (\d+)\) & (\d+)/g, (_, v, s, m) => `bitsAt(${v}, ${2 ** Number(s)}.0, ${Number(m) + 1}.0)`);
|
|
56
|
+
src = src.replace(/\((\w+) & (\d+)\) (!=|==) 0/g, (_, v, b, op) => `${op === "==" ? "!" : ""}bitSet(${v}, ${b}.0)`);
|
|
57
|
+
src = src.replace(/\(((?:[^()]|\([^()]*\))*)\) % (\w+)/g, (_, a, b) => `imod(${a}, ${b})`);
|
|
58
|
+
src = src.replace(/(\w+) % (\w+)/g, (_, a, b) => `imod(${a}, ${b})`);
|
|
59
|
+
const caps = options.caps ?? {};
|
|
60
|
+
const used = /* @__PURE__ */ new Set();
|
|
61
|
+
src = src.replace(/for \(int (\w+) = ([^;]+); \1 < ([^;]+); \1\+\+\)(\s*)\{/g, (all, v, from, to, ws) => {
|
|
62
|
+
if (isConstant(from) && isConstant(to)) return all;
|
|
63
|
+
const cap = caps[to.trim()];
|
|
64
|
+
if (cap === void 0) throw new Error(`pixi-silk/webgl1: loop bound '${to}' has no cap`);
|
|
65
|
+
used.add(to.trim());
|
|
66
|
+
const i = `${v}_gl1`;
|
|
67
|
+
const inner = ws;
|
|
68
|
+
return `for (int ${i} = 0; ${i} < ${cap}; ${i}++)${ws}{${inner}int ${v} = ${from} + ${i};${inner}if (${v} >= ${to}) break;`;
|
|
69
|
+
});
|
|
70
|
+
for (const key of Object.keys(caps)) if (!used.has(key)) throw new Error(`pixi-silk/webgl1: cap for loop bound '${key}' matches no loop`);
|
|
71
|
+
src = src.replace(/\bflat /g, "");
|
|
72
|
+
if (stage === "vertex") src = src.replace(/^in /gm, "attribute ").replace(/^out /gm, "varying ");
|
|
73
|
+
else {
|
|
74
|
+
src = src.replace(/^in /gm, "varying ").replace(/^out vec4 finalColor;\n/m, "");
|
|
75
|
+
src = src.replace(/\bfinalColor\b/g, "gl_FragColor");
|
|
76
|
+
}
|
|
77
|
+
const sized = /* @__PURE__ */ new Set();
|
|
78
|
+
src = rewriteCalls(src, "textureLod", ([s, uv, lod]) => {
|
|
79
|
+
if (lod !== "0.0") throw new Error("pixi-silk/webgl1: textureLod with a level other than 0");
|
|
80
|
+
return `texture2D(${s}, ${uv})`;
|
|
81
|
+
});
|
|
82
|
+
src = rewriteCalls(src, "texelFetch", ([s, c]) => {
|
|
83
|
+
sized.add(s);
|
|
84
|
+
return `texel_${s}(${c})`;
|
|
85
|
+
});
|
|
86
|
+
src = rewriteCalls(src, "textureSize", ([s]) => {
|
|
87
|
+
sized.add(s);
|
|
88
|
+
return `ivec2(${s}Size)`;
|
|
89
|
+
});
|
|
90
|
+
src = src.replace(/\btexture\(/g, "texture2D(");
|
|
91
|
+
const helpers = [];
|
|
92
|
+
for (const s of sized) {
|
|
93
|
+
helpers.push(`uniform vec2 ${s}Size;`);
|
|
94
|
+
if (src.includes(`texel_${s}(`)) helpers.push(`vec4 texel_${s}(ivec2 c) { return texture2D(${s}, (vec2(c) + 0.5) / ${s}Size); }`);
|
|
95
|
+
}
|
|
96
|
+
if (src.includes("bitSet(")) helpers.push("bool bitSet(int f, float b) { return mod(floor(float(f) / b), 2.0) >= 1.0; }");
|
|
97
|
+
if (src.includes("bitsAt(")) helpers.push("int bitsAt(int f, float s, float m) { return int(mod(floor(float(f) / s), m)); }");
|
|
98
|
+
if (src.includes("imin(")) helpers.push("int imin(int a, int b) { return a < b ? a : b; }");
|
|
99
|
+
if (src.includes("imax(")) helpers.push("int imax(int a, int b) { return a > b ? a : b; }");
|
|
100
|
+
if (src.includes("imod(")) helpers.push("int imod(int a, int b) { int q = int(floor((float(a) + 0.5) / float(b))); return a - q * b; }");
|
|
101
|
+
if (helpers.length > 0) {
|
|
102
|
+
const lines = src.split("\n");
|
|
103
|
+
let last = -1;
|
|
104
|
+
lines.forEach((line, i) => {
|
|
105
|
+
if (/^uniform /.test(line)) last = i;
|
|
106
|
+
});
|
|
107
|
+
if (last < 0) throw new Error("pixi-silk/webgl1: no uniform to place the helpers after");
|
|
108
|
+
lines.splice(last + 1, 0, "", ...helpers);
|
|
109
|
+
src = lines.join("\n");
|
|
110
|
+
}
|
|
111
|
+
if (stage === "fragment" && /\b(dFdx|dFdy|fwidth)\(/.test(src)) {
|
|
112
|
+
src = src.replace(/^precision highp float;\n/m, "");
|
|
113
|
+
src = `precision highp float;\n#extension GL_OES_standard_derivatives : enable\n${src}`;
|
|
114
|
+
}
|
|
115
|
+
const left = src.match(/#version|\btexelFetch\(|\btextureSize\(|\btextureLod\(|\bflat\b|\s%\s|>>|<<|(?<!&)&(?!&)|^(in|out) /m);
|
|
116
|
+
if (left) throw new Error(`pixi-silk/webgl1: '${left[0].trim()}' has no GLSL ES 1.00 rewrite`);
|
|
117
|
+
return {
|
|
118
|
+
source: src,
|
|
119
|
+
sized: [...sized]
|
|
120
|
+
};
|
|
121
|
+
}
|
|
122
|
+
//#endregion
|
|
123
|
+
export { normalizeGlsl, rewriteCalls, toGlsl100 };
|
|
@@ -0,0 +1,137 @@
|
|
|
1
|
+
import { INSTANCE_ATTRIBUTES, Prim } from "../constants.js";
|
|
2
|
+
import { silkGpu } from "../shader-slot.js";
|
|
3
|
+
import { GL1_MAX_EDGES, GL1_VERTEX_ATTRIBUTES, INSTANCE_TEXTURE_WIDTH, gl1Sources } from "./programs.js";
|
|
4
|
+
import { Buffer, BufferImageSource, BufferUsage, GlProgram, UniformGroup } from "pixi.js";
|
|
5
|
+
//#region src/webgl1/hooks.ts
|
|
6
|
+
const SHAPE0 = 4;
|
|
7
|
+
const META = 16;
|
|
8
|
+
function dataTexture(label) {
|
|
9
|
+
return new BufferImageSource({
|
|
10
|
+
resource: new Float32Array(INSTANCE_TEXTURE_WIDTH * 4),
|
|
11
|
+
width: INSTANCE_TEXTURE_WIDTH,
|
|
12
|
+
height: 1,
|
|
13
|
+
format: "rgba32float",
|
|
14
|
+
alphaMode: "premultiplied-alpha",
|
|
15
|
+
scaleMode: "nearest",
|
|
16
|
+
autoGenerateMipmaps: false,
|
|
17
|
+
label
|
|
18
|
+
});
|
|
19
|
+
}
|
|
20
|
+
function ascending(n) {
|
|
21
|
+
const a = new Float32Array(n);
|
|
22
|
+
for (let i = 0; i < n; i++) a[i] = i;
|
|
23
|
+
return a;
|
|
24
|
+
}
|
|
25
|
+
/** @internal Shapes past the WebGL1 loop caps, reported once per page. */
|
|
26
|
+
const warned = { caps: false };
|
|
27
|
+
/** @internal Warns once when an instance needs more loop iterations than the WebGL1 shader runs. */
|
|
28
|
+
function checkCaps(data, count) {
|
|
29
|
+
if (warned.caps) return;
|
|
30
|
+
for (let i = 0; i < count; i++) {
|
|
31
|
+
const o = i * 44;
|
|
32
|
+
const type = data[o + META];
|
|
33
|
+
const a = data[o + SHAPE0 + 1];
|
|
34
|
+
if ((type === Prim.Poly || type === Prim.Union) && a > 4096 || type === Prim.Path && a + data[o + SHAPE0 + 2] > 256) {
|
|
35
|
+
warned.caps = true;
|
|
36
|
+
console.warn(`pixi-silk: a path is too long for WebGL1 (over ${GL1_MAX_EDGES} edges or 256 stroke windows) and draws in part. Split it into several paths.`);
|
|
37
|
+
return;
|
|
38
|
+
}
|
|
39
|
+
}
|
|
40
|
+
}
|
|
41
|
+
/** @internal Builds the hooks `enableWebGL1()` installs in the slot. */
|
|
42
|
+
function createGl1Hooks() {
|
|
43
|
+
const programs = /* @__PURE__ */ new Map();
|
|
44
|
+
const hooks = {
|
|
45
|
+
program(name, vertex, fragment) {
|
|
46
|
+
let entry = programs.get(name);
|
|
47
|
+
if (!entry) {
|
|
48
|
+
const s = gl1Sources(name, vertex, fragment);
|
|
49
|
+
entry = {
|
|
50
|
+
program: GlProgram.from({
|
|
51
|
+
vertex: s.vertex.source,
|
|
52
|
+
fragment: s.fragment.source,
|
|
53
|
+
name: `${name}-gl1`,
|
|
54
|
+
preferredVertexPrecision: "highp",
|
|
55
|
+
preferredFragmentPrecision: "highp"
|
|
56
|
+
}),
|
|
57
|
+
sized: [.../* @__PURE__ */ new Set([...s.vertex.sized, ...s.fragment.sized])]
|
|
58
|
+
};
|
|
59
|
+
programs.set(name, entry);
|
|
60
|
+
}
|
|
61
|
+
return entry.program;
|
|
62
|
+
},
|
|
63
|
+
resources(name) {
|
|
64
|
+
const sized = programs.get(name)?.sized ?? [];
|
|
65
|
+
const uniforms = {};
|
|
66
|
+
for (const s of sized) uniforms[`${s}Size`] = {
|
|
67
|
+
value: new Float32Array([1, 1]),
|
|
68
|
+
type: "vec2<f32>"
|
|
69
|
+
};
|
|
70
|
+
const out = { gl1Sizes: new UniformGroup(uniforms) };
|
|
71
|
+
if (name === "silk-sdf") out.uInstances = dataTexture("silk-instances-gl1");
|
|
72
|
+
return out;
|
|
73
|
+
},
|
|
74
|
+
attach(geometry) {
|
|
75
|
+
if (silkGpu.program === null) {
|
|
76
|
+
for (const name of INSTANCE_ATTRIBUTES) if (!GL1_VERTEX_ATTRIBUTES.has(name)) delete geometry.attributes[name];
|
|
77
|
+
}
|
|
78
|
+
geometry.addAttribute("aIndex", {
|
|
79
|
+
buffer: new Buffer({
|
|
80
|
+
data: ascending(32),
|
|
81
|
+
usage: BufferUsage.VERTEX | BufferUsage.COPY_DST,
|
|
82
|
+
label: "silk-index-gl1"
|
|
83
|
+
}),
|
|
84
|
+
format: "float32",
|
|
85
|
+
stride: 4,
|
|
86
|
+
offset: 0,
|
|
87
|
+
instance: true
|
|
88
|
+
});
|
|
89
|
+
},
|
|
90
|
+
commit(geometry, shader, data, count) {
|
|
91
|
+
const index = geometry.attributes.aIndex?.buffer;
|
|
92
|
+
if (index && index.data.length < count) {
|
|
93
|
+
let n = index.data.length;
|
|
94
|
+
while (n < count) n *= 2;
|
|
95
|
+
index.data = ascending(n);
|
|
96
|
+
}
|
|
97
|
+
const src = shader.resources.uInstances;
|
|
98
|
+
const rows = Math.max(1, Math.ceil(count / 64));
|
|
99
|
+
const floats = rows * INSTANCE_TEXTURE_WIDTH * 4;
|
|
100
|
+
let mirror = src.resource;
|
|
101
|
+
if (mirror.buffer.byteLength < floats * 4) mirror = new Float32Array(floats);
|
|
102
|
+
else mirror = new Float32Array(mirror.buffer, 0, floats);
|
|
103
|
+
mirror.set(data.subarray(0, count * 44));
|
|
104
|
+
src.resource = mirror;
|
|
105
|
+
if (src.height !== rows) src.resize(INSTANCE_TEXTURE_WIDTH, rows);
|
|
106
|
+
src.update();
|
|
107
|
+
hooks.syncSizes(shader);
|
|
108
|
+
checkCaps(data, count);
|
|
109
|
+
},
|
|
110
|
+
syncSizes(shader) {
|
|
111
|
+
const ramps = shader.resources.uGradients;
|
|
112
|
+
if (hooks.nearestRamps && ramps && ramps.style.scaleMode !== "nearest") {
|
|
113
|
+
ramps.style.scaleMode = "nearest";
|
|
114
|
+
ramps.style.update();
|
|
115
|
+
}
|
|
116
|
+
const group = shader.resources.gl1Sizes;
|
|
117
|
+
if (!group) return;
|
|
118
|
+
let changed = false;
|
|
119
|
+
for (const key of Object.keys(group.uniforms)) {
|
|
120
|
+
const src = shader.resources[key.slice(0, -4)];
|
|
121
|
+
const v = group.uniforms[key];
|
|
122
|
+
if (!src || v[0] === src.pixelWidth && v[1] === src.pixelHeight) continue;
|
|
123
|
+
v[0] = src.pixelWidth;
|
|
124
|
+
v[1] = src.pixelHeight;
|
|
125
|
+
changed = true;
|
|
126
|
+
}
|
|
127
|
+
if (changed) group.update();
|
|
128
|
+
},
|
|
129
|
+
release(shader) {
|
|
130
|
+
shader.resources.uInstances?.destroy();
|
|
131
|
+
},
|
|
132
|
+
nearestRamps: false
|
|
133
|
+
};
|
|
134
|
+
return hooks;
|
|
135
|
+
}
|
|
136
|
+
//#endregion
|
|
137
|
+
export { checkCaps, createGl1Hooks };
|