@swf2es/render 0.1.44 → 0.1.46
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/README.md +4 -2
- package/dist/index.d.ts +2 -1
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +2 -1
- package/dist/index.js.map +1 -1
- package/dist/ramps.d.ts +31 -0
- package/dist/ramps.d.ts.map +1 -0
- package/dist/ramps.js +130 -0
- package/dist/ramps.js.map +1 -0
- package/dist/rows.d.ts +28 -1
- package/dist/rows.d.ts.map +1 -1
- package/dist/rows.js +32 -1
- package/dist/rows.js.map +1 -1
- package/dist/scene.d.ts +6 -0
- package/dist/scene.d.ts.map +1 -1
- package/dist/scene.js +6 -0
- package/dist/scene.js.map +1 -1
- package/dist/spans.js +2 -2
- package/dist/spans.js.map +1 -1
- package/dist/textures.d.ts +42 -0
- package/dist/textures.d.ts.map +1 -1
- package/dist/textures.js +80 -14
- package/dist/textures.js.map +1 -1
- package/dist/webgl/backend.d.ts +7 -0
- package/dist/webgl/backend.d.ts.map +1 -1
- package/dist/webgl/backend.js +21 -5
- package/dist/webgl/backend.js.map +1 -1
- package/dist/webgl/draws.d.ts +50 -29
- package/dist/webgl/draws.d.ts.map +1 -1
- package/dist/webgl/draws.js +185 -85
- package/dist/webgl/draws.js.map +1 -1
- package/dist/webgl/gpu-textures.d.ts +31 -1
- package/dist/webgl/gpu-textures.d.ts.map +1 -1
- package/dist/webgl/gpu-textures.js +146 -4
- package/dist/webgl/gpu-textures.js.map +1 -1
- package/dist/webgl/programs.d.ts +40 -9
- package/dist/webgl/programs.d.ts.map +1 -1
- package/dist/webgl/programs.js +111 -18
- package/dist/webgl/programs.js.map +1 -1
- package/dist/webgl/ramps.d.ts +54 -0
- package/dist/webgl/ramps.d.ts.map +1 -0
- package/dist/webgl/ramps.js +277 -0
- package/dist/webgl/ramps.js.map +1 -0
- package/package.json +6 -2
- package/src/index.ts +11 -0
- package/src/ramps.ts +188 -0
- package/src/rows.ts +34 -1
- package/src/scene.ts +6 -0
- package/src/spans.ts +2 -2
- package/src/textures.ts +102 -14
- package/src/webgl/backend.ts +22 -5
- package/src/webgl/draws.ts +252 -91
- package/src/webgl/gpu-textures.ts +185 -5
- package/src/webgl/programs.ts +124 -18
- package/src/webgl/ramps.ts +338 -0
|
@@ -0,0 +1,338 @@
|
|
|
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 { type GlState, linkProgram } from "@swf2es/context3d/device";
|
|
9
|
+
import {
|
|
10
|
+
RAMP_BYTES,
|
|
11
|
+
RAMP_COLORS,
|
|
12
|
+
RAMP_COUNT,
|
|
13
|
+
RAMP_LINEAR,
|
|
14
|
+
RAMP_RATIOS,
|
|
15
|
+
RAMP_STOPS,
|
|
16
|
+
TO_SRGB,
|
|
17
|
+
} from "../ramps.js";
|
|
18
|
+
import { LINE_TEXELS } from "../textures.js";
|
|
19
|
+
import type { GpuTextures } from "./gpu-textures.js";
|
|
20
|
+
import { BACK_UNIT, LAYER_UNIT, TEXTURE_UNIT } from "./programs.js";
|
|
21
|
+
|
|
22
|
+
/** Texels of a record, RGBA: the records' texture's width. */
|
|
23
|
+
const RECORD_TEXELS = RAMP_BYTES / 4;
|
|
24
|
+
|
|
25
|
+
/** The most lines built at once: the lines' texture's most, the runs' texture's width. */
|
|
26
|
+
const BUILDS_MOST = 2048;
|
|
27
|
+
|
|
28
|
+
/**
|
|
29
|
+
* Each instance a run of lines, from its first to past its last, two
|
|
30
|
+
* triangles across them in a viewport of the lines' texture. Drawn a quad
|
|
31
|
+
* each, 240 lines took SwiftShader 2.4 ms with a fragment shader that wrote
|
|
32
|
+
* a constant; one quad of as many texels took it 0.1 ms.
|
|
33
|
+
*/
|
|
34
|
+
export const RAMP_VERTEX = `#version 300 es
|
|
35
|
+
precision highp float;
|
|
36
|
+
precision highp int;
|
|
37
|
+
uniform highp isampler2D uRuns;
|
|
38
|
+
uniform highp usampler2D uRecords;
|
|
39
|
+
|
|
40
|
+
void main(void) {
|
|
41
|
+
ivec2 run = texelFetch(uRuns, ivec2(gl_InstanceID, 0), 0).rg;
|
|
42
|
+
int c = gl_VertexID;
|
|
43
|
+
float x = c == 1 || c == 2 || c == 4 ? 1.0 : 0.0;
|
|
44
|
+
int row = c == 2 || c == 4 || c == 5 ? run.y : run.x;
|
|
45
|
+
// The records' texture has a row for each of the lines' texture's.
|
|
46
|
+
float room = float(textureSize(uRecords, 0).y);
|
|
47
|
+
gl_Position = vec4(x * 2.0 - 1.0, float(row) / room * 2.0 - 1.0, 0.0, 1.0);
|
|
48
|
+
}
|
|
49
|
+
`;
|
|
50
|
+
|
|
51
|
+
/**
|
|
52
|
+
* Each texel the entry of its line's ramp at its column, as rampOf finds it:
|
|
53
|
+
* the stop's colour, or the span's c0 + floor(d * j / w) in integers, the
|
|
54
|
+
* colour channels back through TO_SRGB in linear light, premultiplied.
|
|
55
|
+
*/
|
|
56
|
+
export const RAMP_FRAGMENT = `#version 300 es
|
|
57
|
+
precision highp float;
|
|
58
|
+
precision highp int;
|
|
59
|
+
uniform highp usampler2D uRecords;
|
|
60
|
+
uniform highp usampler2D uSrgb;
|
|
61
|
+
out vec4 finalColor;
|
|
62
|
+
int line;
|
|
63
|
+
|
|
64
|
+
int ratioOf(int i) {
|
|
65
|
+
uvec4 t = texelFetch(uRecords, ivec2(${RAMP_RATIOS / 4} + (i >> 2), line), 0);
|
|
66
|
+
return int(t[i & 3]);
|
|
67
|
+
}
|
|
68
|
+
|
|
69
|
+
ivec4 colorOf(int i) {
|
|
70
|
+
return ivec4(texelFetch(uRecords, ivec2(${RAMP_COLORS / 4} + i, line), 0));
|
|
71
|
+
}
|
|
72
|
+
|
|
73
|
+
int floorDiv(int n, int w) {
|
|
74
|
+
return n >= 0 ? n / w : -((w - 1 - n) / w);
|
|
75
|
+
}
|
|
76
|
+
|
|
77
|
+
int srgb(int l) {
|
|
78
|
+
return int(texelFetch(uSrgb, ivec2(l, 0), 0).r);
|
|
79
|
+
}
|
|
80
|
+
|
|
81
|
+
void main(void) {
|
|
82
|
+
line = int(gl_FragCoord.y);
|
|
83
|
+
int k = int(gl_FragCoord.x);
|
|
84
|
+
uvec4 head = texelFetch(uRecords, ivec2(0, line), 0);
|
|
85
|
+
int count = int(head[${RAMP_COUNT}]);
|
|
86
|
+
bool linear = head[${RAMP_LINEAR}] != 0u;
|
|
87
|
+
int from = ratioOf(0);
|
|
88
|
+
ivec4 c;
|
|
89
|
+
if (k <= from) {
|
|
90
|
+
c = colorOf(0);
|
|
91
|
+
} else {
|
|
92
|
+
// The span k lies in, as rampOf walks them: each one entry wide at least.
|
|
93
|
+
int span = 0;
|
|
94
|
+
int w = 0;
|
|
95
|
+
int at = from + 1;
|
|
96
|
+
for (int i = 1; i < ${RAMP_STOPS}; i++) {
|
|
97
|
+
if (i >= count || at > 255) {
|
|
98
|
+
break;
|
|
99
|
+
}
|
|
100
|
+
|
|
101
|
+
int to = ratioOf(i);
|
|
102
|
+
w = to - from;
|
|
103
|
+
int end = w <= 0 ? at : max(at, min(to, 255));
|
|
104
|
+
if (k <= end) {
|
|
105
|
+
span = i;
|
|
106
|
+
break;
|
|
107
|
+
}
|
|
108
|
+
|
|
109
|
+
at = end + 1;
|
|
110
|
+
from = to;
|
|
111
|
+
}
|
|
112
|
+
|
|
113
|
+
if (span == 0) {
|
|
114
|
+
c = colorOf(count - 1);
|
|
115
|
+
} else {
|
|
116
|
+
ivec4 x = colorOf(span - 1);
|
|
117
|
+
c = x;
|
|
118
|
+
if (w > 0) {
|
|
119
|
+
ivec4 d = colorOf(span) - x;
|
|
120
|
+
int j = k - from;
|
|
121
|
+
c = ivec4(
|
|
122
|
+
x.r + floorDiv(d.r * j, w),
|
|
123
|
+
x.g + floorDiv(d.g * j, w),
|
|
124
|
+
x.b + floorDiv(d.b * j, w),
|
|
125
|
+
x.a + floorDiv(d.a * j, w)) & 255;
|
|
126
|
+
}
|
|
127
|
+
}
|
|
128
|
+
}
|
|
129
|
+
|
|
130
|
+
ivec3 rgb = linear ? ivec3(srgb(c.r), srgb(c.g), srgb(c.b)) : c.rgb;
|
|
131
|
+
rgb = (rgb * (c.a + 1)) >> 8;
|
|
132
|
+
finalColor = vec4(vec3(rgb), float(c.a)) / 255.0;
|
|
133
|
+
}
|
|
134
|
+
`;
|
|
135
|
+
|
|
136
|
+
/**
|
|
137
|
+
* Builds the lines GpuTextures marked (builds) from their records: the
|
|
138
|
+
* records uploaded, then drawn into the lines' texture through a
|
|
139
|
+
* framebuffer of its own, blending off.
|
|
140
|
+
*/
|
|
141
|
+
export class RampBuilder {
|
|
142
|
+
private readonly gl: WebGL2RenderingContext;
|
|
143
|
+
private readonly state: GlState;
|
|
144
|
+
private program: WebGLProgram | null = null;
|
|
145
|
+
private records: WebGLTexture | null = null;
|
|
146
|
+
private recordRoom = 0;
|
|
147
|
+
private srgb: WebGLTexture | null = null;
|
|
148
|
+
private runList: WebGLTexture | null = null;
|
|
149
|
+
private readonly sorted = new Int32Array(BUILDS_MOST);
|
|
150
|
+
private readonly runs = new Int32Array(BUILDS_MOST * 2);
|
|
151
|
+
private framebuffer: WebGLFramebuffer | null = null;
|
|
152
|
+
private target: WebGLTexture | null = null;
|
|
153
|
+
|
|
154
|
+
constructor(gl: WebGL2RenderingContext, state: GlState) {
|
|
155
|
+
this.gl = gl;
|
|
156
|
+
this.state = state;
|
|
157
|
+
}
|
|
158
|
+
|
|
159
|
+
/**
|
|
160
|
+
* As the context comes or comes back, and once what it made is deleted:
|
|
161
|
+
* nothing made yet, the program included, which link makes. Nothing here
|
|
162
|
+
* touches the context, so that destroy works on a lost one.
|
|
163
|
+
*/
|
|
164
|
+
setUp(): void {
|
|
165
|
+
this.program = null;
|
|
166
|
+
this.records = null;
|
|
167
|
+
this.recordRoom = 0;
|
|
168
|
+
this.srgb = null;
|
|
169
|
+
this.runList = null;
|
|
170
|
+
this.framebuffer = null;
|
|
171
|
+
this.target = null;
|
|
172
|
+
}
|
|
173
|
+
|
|
174
|
+
/**
|
|
175
|
+
* The program linked, its samplers told their units: the last of the
|
|
176
|
+
* backend's setUp, so that a context lost meanwhile throws here with all
|
|
177
|
+
* else made, for destroy, and a frame or a BitmapData drawn while links
|
|
178
|
+
* would fail needs none.
|
|
179
|
+
*/
|
|
180
|
+
link(): void {
|
|
181
|
+
const gl = this.gl;
|
|
182
|
+
const program = linkProgram(gl, RAMP_VERTEX, RAMP_FRAGMENT);
|
|
183
|
+
this.state.useProgram(program);
|
|
184
|
+
gl.uniform1i(gl.getUniformLocation(program, "uRecords"), TEXTURE_UNIT);
|
|
185
|
+
gl.uniform1i(gl.getUniformLocation(program, "uSrgb"), LAYER_UNIT);
|
|
186
|
+
gl.uniform1i(gl.getUniformLocation(program, "uRuns"), BACK_UNIT);
|
|
187
|
+
this.program = program;
|
|
188
|
+
}
|
|
189
|
+
|
|
190
|
+
/** What it made on the context, deleted. */
|
|
191
|
+
destroy(): void {
|
|
192
|
+
const gl = this.gl;
|
|
193
|
+
for (const texture of [this.records, this.srgb, this.runList]) {
|
|
194
|
+
if (texture) {
|
|
195
|
+
this.state.dropTexture(texture);
|
|
196
|
+
gl.deleteTexture(texture);
|
|
197
|
+
}
|
|
198
|
+
}
|
|
199
|
+
|
|
200
|
+
gl.deleteFramebuffer(this.framebuffer);
|
|
201
|
+
gl.deleteProgram(this.program);
|
|
202
|
+
this.setUp();
|
|
203
|
+
}
|
|
204
|
+
|
|
205
|
+
/** The lines `gpu` marked built into its lines' texture, and marked done. */
|
|
206
|
+
build(gpu: GpuTextures, nearest: WebGLSampler): void {
|
|
207
|
+
const gl = this.gl;
|
|
208
|
+
const state = this.state;
|
|
209
|
+
const program = this.program;
|
|
210
|
+
const lines = gpu.lines;
|
|
211
|
+
if (!program || !lines || gpu.builds.length === 0) {
|
|
212
|
+
gpu.builtAll();
|
|
213
|
+
return;
|
|
214
|
+
}
|
|
215
|
+
|
|
216
|
+
// The lines in order, into runs of lines one after another, each [first, past its last).
|
|
217
|
+
const count = Math.min(gpu.builds.length, BUILDS_MOST);
|
|
218
|
+
const sorted = this.sorted;
|
|
219
|
+
for (let i = 0; i < count; i++) {
|
|
220
|
+
sorted[i] = gpu.builds[i];
|
|
221
|
+
}
|
|
222
|
+
|
|
223
|
+
sorted.subarray(0, count).sort();
|
|
224
|
+
const runs = this.runs;
|
|
225
|
+
let made = 0;
|
|
226
|
+
for (let i = 0; i < count; ) {
|
|
227
|
+
const first = sorted[i];
|
|
228
|
+
let past = first + 1;
|
|
229
|
+
i++;
|
|
230
|
+
while (i < count && sorted[i] === past) {
|
|
231
|
+
past++;
|
|
232
|
+
i++;
|
|
233
|
+
}
|
|
234
|
+
|
|
235
|
+
runs[made * 2] = first;
|
|
236
|
+
runs[made * 2 + 1] = past;
|
|
237
|
+
made++;
|
|
238
|
+
}
|
|
239
|
+
|
|
240
|
+
// The records from the first line built to the last, uploaded in one call.
|
|
241
|
+
this.upload(gpu, sorted[0], sorted[count - 1] + 1);
|
|
242
|
+
|
|
243
|
+
state.bindTexture(BACK_UNIT, this.runList);
|
|
244
|
+
state.activeTexture(BACK_UNIT);
|
|
245
|
+
gl.texSubImage2D(gl.TEXTURE_2D, 0, 0, 0, made, 1, gl.RG_INTEGER, gl.INT, runs);
|
|
246
|
+
|
|
247
|
+
if (this.target !== lines) {
|
|
248
|
+
if (this.framebuffer) {
|
|
249
|
+
gl.deleteFramebuffer(this.framebuffer);
|
|
250
|
+
}
|
|
251
|
+
|
|
252
|
+
this.framebuffer = gl.createFramebuffer() as WebGLFramebuffer;
|
|
253
|
+
state.bindFramebuffer(this.framebuffer);
|
|
254
|
+
gl.framebufferTexture2D(gl.FRAMEBUFFER, gl.COLOR_ATTACHMENT0, gl.TEXTURE_2D, lines, 0);
|
|
255
|
+
this.target = lines;
|
|
256
|
+
}
|
|
257
|
+
|
|
258
|
+
state.bindFramebuffer(this.framebuffer);
|
|
259
|
+
state.viewport(0, 0, LINE_TEXELS, gpu.lineRoom);
|
|
260
|
+
state.scissor(null);
|
|
261
|
+
state.colorMask(15);
|
|
262
|
+
state.stencil(null);
|
|
263
|
+
state.setBlend(null);
|
|
264
|
+
state.useProgram(program);
|
|
265
|
+
state.bindVertexArray(null);
|
|
266
|
+
state.bindTexture(TEXTURE_UNIT, this.records);
|
|
267
|
+
state.bindSampler(TEXTURE_UNIT, nearest);
|
|
268
|
+
state.bindTexture(LAYER_UNIT, this.srgb);
|
|
269
|
+
state.bindSampler(LAYER_UNIT, nearest);
|
|
270
|
+
state.bindSampler(BACK_UNIT, nearest);
|
|
271
|
+
gl.drawArraysInstanced(gl.TRIANGLES, 0, 6, made);
|
|
272
|
+
// Integer textures left on units a float sampler of the next program names, whether it reads
|
|
273
|
+
// them or not, fail its draws (INVALID_OPERATION).
|
|
274
|
+
state.bindTexture(TEXTURE_UNIT, null);
|
|
275
|
+
state.bindTexture(LAYER_UNIT, null);
|
|
276
|
+
state.bindTexture(BACK_UNIT, null);
|
|
277
|
+
gpu.builtAll();
|
|
278
|
+
}
|
|
279
|
+
|
|
280
|
+
/** Records `from` up to `to` uploaded; the textures made first where there are none or too few rows. */
|
|
281
|
+
private upload(gpu: GpuTextures, from: number, to: number): void {
|
|
282
|
+
const gl = this.gl;
|
|
283
|
+
const state = this.state;
|
|
284
|
+
if (!this.srgb) {
|
|
285
|
+
this.srgb = gl.createTexture() as WebGLTexture;
|
|
286
|
+
state.bindTexture(LAYER_UNIT, this.srgb);
|
|
287
|
+
state.activeTexture(LAYER_UNIT);
|
|
288
|
+
gl.texImage2D(
|
|
289
|
+
gl.TEXTURE_2D,
|
|
290
|
+
0,
|
|
291
|
+
gl.R8UI,
|
|
292
|
+
256,
|
|
293
|
+
1,
|
|
294
|
+
0,
|
|
295
|
+
gl.RED_INTEGER,
|
|
296
|
+
gl.UNSIGNED_BYTE,
|
|
297
|
+
TO_SRGB,
|
|
298
|
+
);
|
|
299
|
+
this.runList = gl.createTexture() as WebGLTexture;
|
|
300
|
+
state.bindTexture(BACK_UNIT, this.runList);
|
|
301
|
+
state.activeTexture(BACK_UNIT);
|
|
302
|
+
gl.texImage2D(gl.TEXTURE_2D, 0, gl.RG32I, BUILDS_MOST, 1, 0, gl.RG_INTEGER, gl.INT, null);
|
|
303
|
+
}
|
|
304
|
+
|
|
305
|
+
const made = !this.records || this.recordRoom !== gpu.lineRoom;
|
|
306
|
+
this.records ??= gl.createTexture() as WebGLTexture;
|
|
307
|
+
state.bindTexture(TEXTURE_UNIT, this.records);
|
|
308
|
+
state.activeTexture(TEXTURE_UNIT);
|
|
309
|
+
if (made) {
|
|
310
|
+
this.recordRoom = gpu.lineRoom;
|
|
311
|
+
gl.texImage2D(
|
|
312
|
+
gl.TEXTURE_2D,
|
|
313
|
+
0,
|
|
314
|
+
gl.RGBA8UI,
|
|
315
|
+
RECORD_TEXELS,
|
|
316
|
+
this.recordRoom,
|
|
317
|
+
0,
|
|
318
|
+
gl.RGBA_INTEGER,
|
|
319
|
+
gl.UNSIGNED_BYTE,
|
|
320
|
+
gpu.rampRecords,
|
|
321
|
+
);
|
|
322
|
+
return;
|
|
323
|
+
}
|
|
324
|
+
|
|
325
|
+
gl.texSubImage2D(
|
|
326
|
+
gl.TEXTURE_2D,
|
|
327
|
+
0,
|
|
328
|
+
0,
|
|
329
|
+
from,
|
|
330
|
+
RECORD_TEXELS,
|
|
331
|
+
to - from,
|
|
332
|
+
gl.RGBA_INTEGER,
|
|
333
|
+
gl.UNSIGNED_BYTE,
|
|
334
|
+
gpu.rampRecords,
|
|
335
|
+
from * RAMP_BYTES,
|
|
336
|
+
);
|
|
337
|
+
}
|
|
338
|
+
}
|