@swf2es/render 0.1.45 → 0.1.47

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,284 @@
1
+ // Ramps built on the GPU (Textures.addRamp): the lines a frame set from
2
+ // records drawn into the lines' texture at once, each texel its entry as
3
+ // ramps.ts's rampOf finds it on the CPU, in the same integers, so that the
4
+ // bytes are the same. One instanced draw for all of them, each instance a
5
+ // run of lines one after another, each texel's record read from a texture of
6
+ // them by its row, uploaded from the first line set to the last; no uniform
7
+ // array, nothing indexed by gl_DrawID.
8
+ import { linkProgram } from "@swf2es/context3d/device";
9
+ import { RAMP_BYTES, RAMP_COLORS, RAMP_COUNT, RAMP_LINEAR, RAMP_RATIOS, RAMP_STOPS, TO_SRGB, } from "../ramps.js";
10
+ import { LINE_TEXELS } from "../textures.js";
11
+ import { BACK_UNIT, LAYER_UNIT, TEXTURE_UNIT } from "./programs.js";
12
+ /** Texels of a record, RGBA: the records' texture's width. */
13
+ const RECORD_TEXELS = RAMP_BYTES / 4;
14
+ /** The most lines built at once: the lines' texture's most, the runs' texture's width. */
15
+ const BUILDS_MOST = 2048;
16
+ /**
17
+ * Each instance a run of lines, from its first to past its last, two
18
+ * triangles across them in a viewport of the lines' texture. Drawn a quad
19
+ * each, 240 lines took SwiftShader 2.4 ms with a fragment shader that wrote
20
+ * a constant; one quad of as many texels took it 0.1 ms.
21
+ */
22
+ export const RAMP_VERTEX = `#version 300 es
23
+ precision highp float;
24
+ precision highp int;
25
+ uniform highp isampler2D uRuns;
26
+ uniform highp usampler2D uRecords;
27
+
28
+ void main(void) {
29
+ ivec2 run = texelFetch(uRuns, ivec2(gl_InstanceID, 0), 0).rg;
30
+ int c = gl_VertexID;
31
+ float x = c == 1 || c == 2 || c == 4 ? 1.0 : 0.0;
32
+ int row = c == 2 || c == 4 || c == 5 ? run.y : run.x;
33
+ // The records' texture has a row for each of the lines' texture's.
34
+ float room = float(textureSize(uRecords, 0).y);
35
+ gl_Position = vec4(x * 2.0 - 1.0, float(row) / room * 2.0 - 1.0, 0.0, 1.0);
36
+ }
37
+ `;
38
+ /**
39
+ * Each texel the entry of its line's ramp at its column, as rampOf finds it:
40
+ * the stop's colour, or the span's c0 + floor(d * j / w) in integers, the
41
+ * colour channels back through TO_SRGB in linear light, premultiplied. The
42
+ * ratios are read four a fetch, as the walk comes to them, and the quotient
43
+ * is found in floats and put right by its remainder: a fetch for each stop
44
+ * and four integer divisions an entry took SwiftShader a quarter more.
45
+ */
46
+ export const RAMP_FRAGMENT = `#version 300 es
47
+ precision highp float;
48
+ precision highp int;
49
+ uniform highp usampler2D uRecords;
50
+ uniform highp usampler2D uSrgb;
51
+ out vec4 finalColor;
52
+ int line;
53
+
54
+ ivec4 colorOf(int i) {
55
+ return ivec4(texelFetch(uRecords, ivec2(${RAMP_COLORS / 4} + i, line), 0));
56
+ }
57
+
58
+ int srgb(int l) {
59
+ return int(texelFetch(uSrgb, ivec2(l, 0), 0).r);
60
+ }
61
+
62
+ void main(void) {
63
+ line = int(gl_FragCoord.y);
64
+ int k = int(gl_FragCoord.x);
65
+ uvec4 head = texelFetch(uRecords, ivec2(0, line), 0);
66
+ int count = int(head[${RAMP_COUNT}]);
67
+ bool linear = head[${RAMP_LINEAR}] != 0u;
68
+ uvec4 ratios = texelFetch(uRecords, ivec2(${RAMP_RATIOS / 4}, line), 0);
69
+ int from = int(ratios.x);
70
+ ivec4 c;
71
+ if (k <= from) {
72
+ c = colorOf(0);
73
+ } else {
74
+ // The span k lies in, as rampOf walks them: each one entry wide at least.
75
+ int span = 0;
76
+ int w = 0;
77
+ int at = from + 1;
78
+ for (int i = 1; i < ${RAMP_STOPS}; i++) {
79
+ if (i >= count || at > 255) {
80
+ break;
81
+ }
82
+
83
+ if ((i & 3) == 0) {
84
+ ratios = texelFetch(uRecords, ivec2(${RAMP_RATIOS / 4} + (i >> 2), line), 0);
85
+ }
86
+
87
+ int to = int(ratios[i & 3]);
88
+ w = to - from;
89
+ int end = w <= 0 ? at : max(at, min(to, 255));
90
+ if (k <= end) {
91
+ span = i;
92
+ break;
93
+ }
94
+
95
+ at = end + 1;
96
+ from = to;
97
+ }
98
+
99
+ if (span == 0) {
100
+ c = colorOf(count - 1);
101
+ } else {
102
+ ivec4 x = colorOf(span - 1);
103
+ c = x;
104
+ if (w > 0) {
105
+ ivec4 d = colorOf(span) - x;
106
+ int j = k - from;
107
+ // floor(n / w) exactly: n is within 255 * 255 of 0, so (n + 0.5) / w is
108
+ // at least 0.5 / w from a whole number, which a division up to about 16 ULP
109
+ // out cannot cross; the remainder puts the quotient right on a GPU that
110
+ // divides worse.
111
+ ivec4 n = d * j;
112
+ ivec4 q = ivec4(floor((vec4(n) + 0.5) / float(w)));
113
+ ivec4 r = n - q * w;
114
+ q += ivec4(greaterThanEqual(r, ivec4(w))) - ivec4(lessThan(r, ivec4(0)));
115
+ c = (x + q) & 255;
116
+ }
117
+ }
118
+ }
119
+
120
+ ivec3 rgb = c.rgb;
121
+ if (linear) {
122
+ rgb = ivec3(srgb(c.r), srgb(c.g), srgb(c.b));
123
+ }
124
+
125
+ rgb = (rgb * (c.a + 1)) >> 8;
126
+ finalColor = vec4(vec3(rgb), float(c.a)) / 255.0;
127
+ }
128
+ `;
129
+ /**
130
+ * Builds the lines GpuTextures marked (builds) from their records: the
131
+ * records uploaded, then drawn into the lines' texture through a
132
+ * framebuffer of its own, blending off.
133
+ */
134
+ export class RampBuilder {
135
+ gl;
136
+ state;
137
+ program = null;
138
+ records = null;
139
+ recordRoom = 0;
140
+ srgb = null;
141
+ runList = null;
142
+ sorted = new Int32Array(BUILDS_MOST);
143
+ runs = new Int32Array(BUILDS_MOST * 2);
144
+ framebuffer = null;
145
+ target = null;
146
+ constructor(gl, state) {
147
+ this.gl = gl;
148
+ this.state = state;
149
+ }
150
+ /**
151
+ * As the context comes or comes back, and once what it made is deleted:
152
+ * nothing made yet, the program included, which link makes. Nothing here
153
+ * touches the context, so that destroy works on a lost one.
154
+ */
155
+ setUp() {
156
+ this.program = null;
157
+ this.records = null;
158
+ this.recordRoom = 0;
159
+ this.srgb = null;
160
+ this.runList = null;
161
+ this.framebuffer = null;
162
+ this.target = null;
163
+ }
164
+ /**
165
+ * The program linked, its samplers told their units: the last of the
166
+ * backend's setUp, so that a context lost meanwhile throws here with all
167
+ * else made, for destroy, and a frame or a BitmapData drawn while links
168
+ * would fail needs none.
169
+ */
170
+ link() {
171
+ const gl = this.gl;
172
+ const program = linkProgram(gl, RAMP_VERTEX, RAMP_FRAGMENT);
173
+ this.state.useProgram(program);
174
+ gl.uniform1i(gl.getUniformLocation(program, "uRecords"), TEXTURE_UNIT);
175
+ gl.uniform1i(gl.getUniformLocation(program, "uSrgb"), LAYER_UNIT);
176
+ gl.uniform1i(gl.getUniformLocation(program, "uRuns"), BACK_UNIT);
177
+ this.program = program;
178
+ }
179
+ /** What it made on the context, deleted. */
180
+ destroy() {
181
+ const gl = this.gl;
182
+ for (const texture of [this.records, this.srgb, this.runList]) {
183
+ if (texture) {
184
+ this.state.dropTexture(texture);
185
+ gl.deleteTexture(texture);
186
+ }
187
+ }
188
+ gl.deleteFramebuffer(this.framebuffer);
189
+ gl.deleteProgram(this.program);
190
+ this.setUp();
191
+ }
192
+ /** The lines `gpu` marked built into its lines' texture, and marked done. */
193
+ build(gpu, nearest) {
194
+ const gl = this.gl;
195
+ const state = this.state;
196
+ const program = this.program;
197
+ const lines = gpu.lines;
198
+ if (!program || !lines || gpu.builds.length === 0) {
199
+ gpu.builtAll();
200
+ return;
201
+ }
202
+ // The lines in order, into runs of lines one after another, each [first, past its last).
203
+ const count = Math.min(gpu.builds.length, BUILDS_MOST);
204
+ const sorted = this.sorted;
205
+ for (let i = 0; i < count; i++) {
206
+ sorted[i] = gpu.builds[i];
207
+ }
208
+ sorted.subarray(0, count).sort();
209
+ const runs = this.runs;
210
+ let made = 0;
211
+ for (let i = 0; i < count;) {
212
+ const first = sorted[i];
213
+ let past = first + 1;
214
+ i++;
215
+ while (i < count && sorted[i] === past) {
216
+ past++;
217
+ i++;
218
+ }
219
+ runs[made * 2] = first;
220
+ runs[made * 2 + 1] = past;
221
+ made++;
222
+ }
223
+ // The records from the first line built to the last, uploaded in one call.
224
+ this.upload(gpu, sorted[0], sorted[count - 1] + 1);
225
+ state.bindTexture(BACK_UNIT, this.runList);
226
+ state.activeTexture(BACK_UNIT);
227
+ gl.texSubImage2D(gl.TEXTURE_2D, 0, 0, 0, made, 1, gl.RG_INTEGER, gl.INT, runs);
228
+ if (this.target !== lines) {
229
+ if (this.framebuffer) {
230
+ gl.deleteFramebuffer(this.framebuffer);
231
+ }
232
+ this.framebuffer = gl.createFramebuffer();
233
+ state.bindFramebuffer(this.framebuffer);
234
+ gl.framebufferTexture2D(gl.FRAMEBUFFER, gl.COLOR_ATTACHMENT0, gl.TEXTURE_2D, lines, 0);
235
+ this.target = lines;
236
+ }
237
+ state.bindFramebuffer(this.framebuffer);
238
+ state.viewport(0, 0, LINE_TEXELS, gpu.lineRoom);
239
+ state.scissor(null);
240
+ state.colorMask(15);
241
+ state.stencil(null);
242
+ state.setBlend(null);
243
+ state.useProgram(program);
244
+ state.bindVertexArray(null);
245
+ state.bindTexture(TEXTURE_UNIT, this.records);
246
+ state.bindSampler(TEXTURE_UNIT, nearest);
247
+ state.bindTexture(LAYER_UNIT, this.srgb);
248
+ state.bindSampler(LAYER_UNIT, nearest);
249
+ state.bindSampler(BACK_UNIT, nearest);
250
+ gl.drawArraysInstanced(gl.TRIANGLES, 0, 6, made);
251
+ // Integer textures left on units a float sampler of the next program names, whether it reads
252
+ // them or not, fail its draws (INVALID_OPERATION).
253
+ state.bindTexture(TEXTURE_UNIT, null);
254
+ state.bindTexture(LAYER_UNIT, null);
255
+ state.bindTexture(BACK_UNIT, null);
256
+ gpu.builtAll();
257
+ }
258
+ /** Records `from` up to `to` uploaded; the textures made first where there are none or too few rows. */
259
+ upload(gpu, from, to) {
260
+ const gl = this.gl;
261
+ const state = this.state;
262
+ if (!this.srgb) {
263
+ this.srgb = gl.createTexture();
264
+ state.bindTexture(LAYER_UNIT, this.srgb);
265
+ state.activeTexture(LAYER_UNIT);
266
+ gl.texImage2D(gl.TEXTURE_2D, 0, gl.R8UI, 256, 1, 0, gl.RED_INTEGER, gl.UNSIGNED_BYTE, TO_SRGB);
267
+ this.runList = gl.createTexture();
268
+ state.bindTexture(BACK_UNIT, this.runList);
269
+ state.activeTexture(BACK_UNIT);
270
+ gl.texImage2D(gl.TEXTURE_2D, 0, gl.RG32I, BUILDS_MOST, 1, 0, gl.RG_INTEGER, gl.INT, null);
271
+ }
272
+ const made = !this.records || this.recordRoom !== gpu.lineRoom;
273
+ this.records ??= gl.createTexture();
274
+ state.bindTexture(TEXTURE_UNIT, this.records);
275
+ state.activeTexture(TEXTURE_UNIT);
276
+ if (made) {
277
+ this.recordRoom = gpu.lineRoom;
278
+ gl.texImage2D(gl.TEXTURE_2D, 0, gl.RGBA8UI, RECORD_TEXELS, this.recordRoom, 0, gl.RGBA_INTEGER, gl.UNSIGNED_BYTE, gpu.rampRecords);
279
+ return;
280
+ }
281
+ gl.texSubImage2D(gl.TEXTURE_2D, 0, 0, from, RECORD_TEXELS, to - from, gl.RGBA_INTEGER, gl.UNSIGNED_BYTE, gpu.rampRecords, from * RAMP_BYTES);
282
+ }
283
+ }
284
+ //# sourceMappingURL=ramps.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"ramps.js","sourceRoot":"","sources":["../../src/webgl/ramps.ts"],"names":[],"mappings":"AAAA,wEAAwE;AACxE,yEAAyE;AACzE,2EAA2E;AAC3E,0EAA0E;AAC1E,6EAA6E;AAC7E,4EAA4E;AAC5E,uCAAuC;AACvC,OAAO,EAAgB,WAAW,EAAE,MAAM,0BAA0B,CAAC;AACrE,OAAO,EACL,UAAU,EACV,WAAW,EACX,UAAU,EACV,WAAW,EACX,WAAW,EACX,UAAU,EACV,OAAO,GACR,MAAM,aAAa,CAAC;AACrB,OAAO,EAAE,WAAW,EAAE,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B,EAAE,CAAC,UAAU,CACX,EAAE,CAAC,UAAU,EACb,CAAC,EACD,EAAE,CAAC,OAAO,EACV,aAAa,EACb,IAAI,CAAC,UAAU,EACf,CAAC,EACD,EAAE,CAAC,YAAY,EACf,EAAE,CAAC,aAAa,EAChB,GAAG,CAAC,WAAW,CAChB,CAAC;YACF,OAAO;QACT,CAAC;QAED,EAAE,CAAC,aAAa,CACd,EAAE,CAAC,UAAU,EACb,CAAC,EACD,CAAC,EACD,IAAI,EACJ,aAAa,EACb,EAAE,GAAG,IAAI,EACT,EAAE,CAAC,YAAY,EACf,EAAE,CAAC,aAAa,EAChB,GAAG,CAAC,WAAW,EACf,IAAI,GAAG,UAAU,CAClB,CAAC;IACJ,CAAC;CACF"}
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@swf2es/render",
3
- "version": "0.1.45",
3
+ "version": "0.1.47",
4
4
  "description": "swf2es's own renderer: a scene list, a planner and a WebGL 2 backend (docs/renderer.md)",
5
5
  "keywords": [
6
6
  "swf",
@@ -8,7 +8,7 @@
8
8
  "renderer",
9
9
  "webgl2"
10
10
  ],
11
- "homepage": "https://swf2es.github.io/swf2es/",
11
+ "homepage": "https://swf2es.dev/",
12
12
  "bugs": "https://github.com/swf2es/swf2es/issues",
13
13
  "repository": {
14
14
  "type": "git",
@@ -22,6 +22,10 @@
22
22
  "types": "./dist/index.d.ts",
23
23
  "default": "./dist/index.js"
24
24
  },
25
+ "./ramps": {
26
+ "types": "./dist/ramps.d.ts",
27
+ "default": "./dist/ramps.js"
28
+ },
25
29
  "./webgl": {
26
30
  "types": "./dist/webgl/index.d.ts",
27
31
  "default": "./dist/webgl/index.js"
@@ -33,6 +37,6 @@
33
37
  "src"
34
38
  ],
35
39
  "dependencies": {
36
- "@swf2es/context3d": "0.1.45"
40
+ "@swf2es/context3d": "0.1.47"
37
41
  }
38
42
  }
package/src/index.ts CHANGED
@@ -53,6 +53,16 @@ export {
53
53
  wholeBitmap,
54
54
  } from "./measure.js";
55
55
  export { plan } from "./plan.js";
56
+ export {
57
+ RAMP_BYTES,
58
+ RAMP_COLORS,
59
+ RAMP_COUNT,
60
+ RAMP_LINEAR,
61
+ RAMP_RATIOS,
62
+ RAMP_STOPS,
63
+ rampOf,
64
+ TO_LINEAR,
65
+ } from "./ramps.js";
56
66
  export { type ColorRow, ROW_FLOATS, ROWS_A_LINE, Rows } from "./rows.js";
57
67
  export {
58
68
  ADD,
package/src/ramps.ts ADDED
@@ -0,0 +1,188 @@
1
+ // Gradient ramps built from their stops (Textures.addRamp): a record of a
2
+ // ramp's stops, from which the backend builds its 256 entries on the GPU,
3
+ // a line of the lines' texture, and rampOf, which builds the same entries
4
+ // on the CPU, entry for entry. The arithmetic is Flash's, as adl's ramps
5
+ // read back entry by entry show: each channel spanned between two stops in
6
+ // integers, c0 + floor((c1 - c0) * (k - r0) / (r1 - r0)); a span taking
7
+ // one entry at least, as Flash's loop does, so that a span of no width or
8
+ // going back takes its first stop's colour, and one ending behind where
9
+ // the ramp has got to is carried on past its end, wrapped into its byte;
10
+ // in linear RGB, the colour channels spanned in 8-bit linear light and
11
+ // taken back to sRGB by table. Each entry is then premultiplied as
12
+ // c * (a + 1) >> 8.
13
+
14
+ /** The most stops a record holds: as many as a gradient may have. */
15
+ export const RAMP_STOPS = 16;
16
+
17
+ /**
18
+ * Bytes of a record, a line of the stops' texture, RGBA: the count of its
19
+ * stops and whether they are spanned in linear light; their ratios, four a
20
+ * texel; then their colours, straight red, green, blue and alpha, a texel
21
+ * each, the colour channels in linear light already where they are spanned
22
+ * there (TO_LINEAR).
23
+ */
24
+ export const RAMP_BYTES = 128;
25
+ export const RAMP_COUNT = 0;
26
+ export const RAMP_LINEAR = 1;
27
+ export const RAMP_RATIOS = 4;
28
+ export const RAMP_COLORS = 32;
29
+
30
+ function toLinear(c: number): number {
31
+ return c <= 0.04045 ? c / 12.92 : ((c + 0.055) / 1.055) ** 2.4;
32
+ }
33
+
34
+ function toSrgb(l: number): number {
35
+ return l <= 0.0031308 ? 12.92 * l : 1.055 * l ** (1 / 2.4) - 0.055;
36
+ }
37
+
38
+ /**
39
+ * Each sRGB byte in 8-bit linear light, and each such back in sRGB, both
40
+ * floored: Flash's, as every byte a ramp's ends come out as shows (255 to
41
+ * 254, 13 to 12).
42
+ */
43
+ export const TO_LINEAR = Uint8Array.from({ length: 256 }, (_, c) =>
44
+ Math.floor(255 * toLinear(c / 255)),
45
+ );
46
+ export const TO_SRGB = Uint8Array.from({ length: 256 }, (_, l) =>
47
+ Math.min(255, Math.floor(255 * toSrgb(l / 255))),
48
+ );
49
+
50
+ /** Each byte as it is: what an RGB ramp's colours come back as. */
51
+ const BYTES = Uint8Array.from({ length: 256 }, (_, c) => c);
52
+
53
+ /**
54
+ * The 256 entries of the ramp `record` holds, premultiplied ARGB, into
55
+ * `out`: the first stop's colour up to its ratio, a span from each stop to
56
+ * the next, the last stop's colour on to the end. With `swapped`, each
57
+ * entry's red and blue swapped, the word a little-endian machine keeps a
58
+ * texel's red, green, blue and alpha bytes in. From `offset` in `out`, so
59
+ * that a line of a larger texture's words needs no view of its own.
60
+ */
61
+ export function rampOf(
62
+ record: Uint8Array,
63
+ out: Uint32Array,
64
+ swapped = false,
65
+ offset = 0,
66
+ ): Uint32Array {
67
+ const count = record[RAMP_COUNT];
68
+ const back = record[RAMP_LINEAR] ? TO_SRGB : BYTES;
69
+ const red = swapped ? 0 : 16;
70
+ const blue = swapped ? 16 : 0;
71
+
72
+ let at = 0;
73
+ const firstRatio = record[RAMP_RATIOS];
74
+ const first = entryAt(record, 0, back, red, blue);
75
+ for (; at <= firstRatio; at++) {
76
+ out[offset + at] = first;
77
+ }
78
+
79
+ for (let i = 1; i < count && at < 256; i++) {
80
+ at = span(record, i - 1, at, back, out, offset, red, blue);
81
+ }
82
+
83
+ const last = entryAt(record, count - 1, back, red, blue);
84
+ for (; at < 256; at++) {
85
+ out[offset + at] = last;
86
+ }
87
+
88
+ return out;
89
+ }
90
+
91
+ /**
92
+ * Stop `from`'s span to the next, from entry `at`, into `out` from `offset`: each channel
93
+ * c0 + floor(d * j / w) for the entry j past the stop, stepped on by
94
+ * floor(d / w) and the remainder carried (STEPS), the same integers as
95
+ * dividing for each. The entry after the last written.
96
+ */
97
+ function span(
98
+ record: Uint8Array,
99
+ from: number,
100
+ at: number,
101
+ back: Uint8Array,
102
+ out: Uint32Array,
103
+ offset: number,
104
+ red: number,
105
+ blue: number,
106
+ ): number {
107
+ const r0 = record[RAMP_RATIOS + from];
108
+ const width = record[RAMP_RATIOS + from + 1] - r0;
109
+ if (width <= 0) {
110
+ out[offset + at] = entryAt(record, from, back, red, blue);
111
+ return at + 1;
112
+ }
113
+
114
+ const x = RAMP_COLORS + from * 4;
115
+ const end = Math.max(at, Math.min(r0 + width, 255));
116
+ const j = at - r0;
117
+ for (let c = 0; c < 4; c++) {
118
+ start(c * 4, record[x + c], record[x + 4 + c], j, width);
119
+ }
120
+
121
+ const s = STEPS;
122
+ for (; at <= end; at++) {
123
+ out[offset + at] = packed(
124
+ s[12] & 0xff,
125
+ back[s[0] & 0xff],
126
+ back[s[4] & 0xff],
127
+ back[s[8] & 0xff],
128
+ red,
129
+ blue,
130
+ );
131
+ for (let c = 0; c < 16; c += 4) {
132
+ s[c] += s[c + 2];
133
+ s[c + 1] += s[c + 3];
134
+ if (s[c + 1] >= width) {
135
+ s[c + 1] -= width;
136
+ s[c]++;
137
+ }
138
+ }
139
+ }
140
+
141
+ return at;
142
+ }
143
+
144
+ /**
145
+ * Each channel of a span, red, green, blue and alpha, four numbers each:
146
+ * its value, the remainder over the span's width, and the whole and the
147
+ * remainder a step adds.
148
+ */
149
+ const STEPS = new Int32Array(16);
150
+
151
+ /** Channel `c` of STEPS from c0 to c1 over `width` entries, at entry j: c0 + floor((c1 - c0) * j / width). */
152
+ function start(c: number, c0: number, c1: number, j: number, width: number): void {
153
+ const d = c1 - c0;
154
+ const n = d * j;
155
+ const q = Math.floor(n / width);
156
+ const whole = Math.floor(d / width);
157
+ STEPS[c] = c0 + q;
158
+ STEPS[c + 1] = n - q * width;
159
+ STEPS[c + 2] = whole;
160
+ STEPS[c + 3] = d - whole * width;
161
+ }
162
+
163
+ /** Stop `i`'s colour as an entry, its colour channels back from the span's bytes, premultiplied. */
164
+ function entryAt(
165
+ record: Uint8Array,
166
+ i: number,
167
+ back: Uint8Array,
168
+ red: number,
169
+ blue: number,
170
+ ): number {
171
+ const x = RAMP_COLORS + i * 4;
172
+ return packed(
173
+ record[x + 3],
174
+ back[record[x]],
175
+ back[record[x + 1]],
176
+ back[record[x + 2]],
177
+ red,
178
+ blue,
179
+ );
180
+ }
181
+
182
+ /** Straight channels premultiplied, each times alpha + 1 over 256, red and blue at their shifts. */
183
+ function packed(a: number, r: number, g: number, b: number, red: number, blue: number): number {
184
+ const k = a + 1;
185
+ return (
186
+ ((a << 24) | (((r * k) >> 8) << red) | (((g * k) >> 8) << 8) | (((b * k) >> 8) << blue)) >>> 0
187
+ );
188
+ }
package/src/textures.ts CHANGED
@@ -20,6 +20,8 @@ export interface TextureData {
20
20
  source?: object | null;
21
21
  /** Its line of the lines' texture where it is a line (addLine). */
22
22
  line?: number;
23
+ /** Whether its pixels are a ramp's record, which the backend builds the line from (addRamp). */
24
+ ramp?: boolean;
23
25
  }
24
26
 
25
27
  /**
@@ -132,6 +134,21 @@ export class Textures extends Registry {
132
134
  return handle;
133
135
  }
134
136
 
137
+ /**
138
+ * A line texture (addLine) whose texels the backend builds from `record`,
139
+ * a ramp's stops (ramps.ts), rather than uploads: RAMP_BYTES set in its
140
+ * place, and no span of a ramp in script. Set again, refilled, from
141
+ * another record. Null where there are as many lines as there may be.
142
+ */
143
+ addRamp(record: Uint8Array, idle = IDLE_FRAMES): TextureHandle | null {
144
+ const handle = this.addLine(record, idle);
145
+ if (handle) {
146
+ (this.data[handle.id] as TextureData).ramp = true;
147
+ }
148
+
149
+ return handle;
150
+ }
151
+
135
152
  /** `id`'s line of the lines' texture, or -1 for a texture of its own. */
136
153
  lineOf(id: number): number {
137
154
  return this.data[id]?.line ?? -1;
@@ -35,6 +35,7 @@ import { KernelPrograms } from "./kernels.js";
35
35
  import { LinkLosses } from "./losses.js";
36
36
  import { ProgramQueue } from "./precompile.js";
37
37
  import { LAYER_UNIT, TEXTURE_UNIT } from "./programs.js";
38
+ import { RampBuilder } from "./ramps.js";
38
39
  import { Stage } from "./stage.js";
39
40
  import { type Layer, type Open, objectOf, type Target, TargetPool } from "./targets.js";
40
41
 
@@ -96,6 +97,8 @@ export class WebGLBackend {
96
97
  private readonly pool: TargetPool;
97
98
  private readonly stage: Stage;
98
99
  private readonly gpu: GpuTextures;
100
+ /** Ramps built from their records into the lines' texture (Textures.addRamp). */
101
+ private readonly ramps: RampBuilder;
99
102
  private readonly atlas: GeometryAtlas;
100
103
  private readonly compositor: Compositor;
101
104
  private readonly draws: Draws;
@@ -156,6 +159,7 @@ export class WebGLBackend {
156
159
  this.pool = new TargetPool(gl, this.state);
157
160
  this.stage = new Stage(gl, this.state, this.pool);
158
161
  this.gpu = new GpuTextures(gl, this.state);
162
+ this.ramps = new RampBuilder(gl, this.state);
159
163
  this.atlas = new GeometryAtlas(gl, this.state);
160
164
  this.compositor = new Compositor(
161
165
  gl,
@@ -325,11 +329,13 @@ export class WebGLBackend {
325
329
  this.stage.setUp();
326
330
  this.atlas.setUp();
327
331
  this.gpu.setUp();
332
+ this.ramps.setUp();
328
333
  // The context's own, gone with it.
329
334
  this.textureFramebuffers = [];
330
335
  // Linked last: a context lost meanwhile throws here (gaveUp), the rest made, for destroy.
331
336
  this.draws.setUp(textureMost);
332
337
  this.compositor.setUp();
338
+ this.ramps.link();
333
339
  }
334
340
 
335
341
  /**
@@ -389,7 +395,7 @@ export class WebGLBackend {
389
395
  const gl = this.gl;
390
396
  this.frameFilters = filters;
391
397
  this.atlas.place(geometries);
392
- this.gpu.uploadTextures(textures, this.clearTexture);
398
+ this.uploadTextures(textures);
393
399
  for (const id of keeps.removed) {
394
400
  this.pool.release(id);
395
401
  }
@@ -842,7 +848,7 @@ export class WebGLBackend {
842
848
  return false;
843
849
  }
844
850
 
845
- this.gpu.uploadTextures(textures, this.clearTexture);
851
+ this.uploadTextures(textures);
846
852
  const framebuffer = this.textureFramebuffer(id);
847
853
  if (!framebuffer) {
848
854
  return false;
@@ -852,6 +858,17 @@ export class WebGLBackend {
852
858
  return true;
853
859
  }
854
860
 
861
+ /**
862
+ * The textures uploaded (GpuTextures' uploadTextures), then the lines set
863
+ * from ramps' records built from them (RampBuilder).
864
+ */
865
+ private uploadTextures(textures: Textures): void {
866
+ this.gpu.uploadTextures(textures, this.clearTexture);
867
+ if (this.gpu.builds.length > 0) {
868
+ this.ramps.build(this.gpu, this.gpu.sampler(0));
869
+ }
870
+ }
871
+
855
872
  /** A texture of one colour, `width` by `height`, made: cleared to it (GpuTextures' uploadTextures). */
856
873
  private readonly clearTexture = (id: number, width: number, height: number, pixel: number) => {
857
874
  const framebuffer = this.textureFramebuffer(id);
@@ -906,7 +923,7 @@ export class WebGLBackend {
906
923
  return false;
907
924
  }
908
925
 
909
- this.gpu.uploadTextures(textures, this.clearTexture);
926
+ this.uploadTextures(textures);
910
927
  const source = this.textureFramebuffer(from);
911
928
  const target = source ? this.textureFramebuffer(to) : null;
912
929
  if (!source || !target) {
@@ -1140,6 +1157,7 @@ export class WebGLBackend {
1140
1157
  this.overProgram = null;
1141
1158
  }
1142
1159
 
1160
+ this.ramps.destroy();
1143
1161
  this.gpu.destroy();
1144
1162
  this.state.forget();
1145
1163
  }