@swf2es/render 0.1.45 → 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 +1 -0
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +1 -0
- 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/textures.d.ts +9 -0
- package/dist/textures.d.ts.map +1 -1
- package/dist/textures.js +13 -0
- 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 +20 -3
- package/dist/webgl/backend.js.map +1 -1
- package/dist/webgl/gpu-textures.d.ts +17 -2
- package/dist/webgl/gpu-textures.d.ts.map +1 -1
- package/dist/webgl/gpu-textures.js +86 -2
- package/dist/webgl/gpu-textures.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 +10 -0
- package/src/ramps.ts +188 -0
- package/src/textures.ts +17 -0
- package/src/webgl/backend.ts +21 -3
- package/src/webgl/gpu-textures.ts +97 -4
- package/src/webgl/ramps.ts +338 -0
|
@@ -1,9 +1,22 @@
|
|
|
1
1
|
import type { GlState } from "@swf2es/context3d/device";
|
|
2
|
+
import { RAMP_BYTES, rampOf } from "../ramps.js";
|
|
2
3
|
import { ROW_FLOATS, ROWS_A_LINE, type Rows } from "../rows.js";
|
|
3
4
|
import { MIRROR, REPEAT, SMOOTH } from "../scene.js";
|
|
4
5
|
import { LINE_TEXELS, type Textures } from "../textures.js";
|
|
5
6
|
import { OUTLINE_ROW, OUTLINE_UNIT, ROW_TEXELS, TEXTURE_UNIT } from "./programs.js";
|
|
6
7
|
|
|
8
|
+
/**
|
|
9
|
+
* Fewer ramps than this set in a frame (Textures.addRamp) are built on the
|
|
10
|
+
* CPU (rampOf) into the lines' bytes, uploaded with the other lines, so
|
|
11
|
+
* that a frame making a ramp or two, as most do, draws nothing more for
|
|
12
|
+
* them; as many or more, as instances under changing tints make, are
|
|
13
|
+
* built by the backend's pass on the GPU. Both make the same bytes.
|
|
14
|
+
*/
|
|
15
|
+
const RAMPS_ON_GPU = 16;
|
|
16
|
+
|
|
17
|
+
/** Whether the machine keeps a word's low byte first, as a texel's red is in rampOf's swapped words. */
|
|
18
|
+
const LITTLE_ENDIAN = new Uint8Array(Uint32Array.of(1).buffer)[0] === 1;
|
|
19
|
+
|
|
7
20
|
/**
|
|
8
21
|
* The textures the programs read: the registry's, uploaded as they are
|
|
9
22
|
* added or change, and the samplers they are read through; the rows, a
|
|
@@ -29,9 +42,20 @@ export class GpuTextures {
|
|
|
29
42
|
* names in `textures`, the bytes of its lines as uploaded, and how many
|
|
30
43
|
* lines it has room for.
|
|
31
44
|
*/
|
|
32
|
-
|
|
45
|
+
lines: WebGLTexture | null = null;
|
|
33
46
|
private lineBytes = new Uint8Array(0);
|
|
34
|
-
|
|
47
|
+
/** lineBytes as words, which a ramp spanned on the CPU is written into (rampOf). */
|
|
48
|
+
private lineWords = new Uint32Array(0);
|
|
49
|
+
lineRoom = 0;
|
|
50
|
+
/**
|
|
51
|
+
* The records of the lines built from them (Textures.addRamp), a line's at
|
|
52
|
+
* its line's place; which lines are built so; and those to build in the
|
|
53
|
+
* backend's next pass (WebGLBackend's ramps), each once.
|
|
54
|
+
*/
|
|
55
|
+
rampRecords = new Uint8Array(0);
|
|
56
|
+
private built = new Uint8Array(0);
|
|
57
|
+
readonly builds: number[] = [];
|
|
58
|
+
private building = new Uint8Array(0);
|
|
35
59
|
|
|
36
60
|
constructor(gl: WebGL2RenderingContext, state: GlState) {
|
|
37
61
|
this.gl = gl;
|
|
@@ -47,7 +71,12 @@ export class GpuTextures {
|
|
|
47
71
|
this.outlineRows = 0;
|
|
48
72
|
this.lines = null;
|
|
49
73
|
this.lineBytes = new Uint8Array(0);
|
|
74
|
+
this.lineWords = new Uint32Array(0);
|
|
50
75
|
this.lineRoom = 0;
|
|
76
|
+
this.rampRecords = new Uint8Array(0);
|
|
77
|
+
this.built = new Uint8Array(0);
|
|
78
|
+
this.builds.length = 0;
|
|
79
|
+
this.building = new Uint8Array(0);
|
|
51
80
|
this.textures = [];
|
|
52
81
|
this.samplers = [];
|
|
53
82
|
this.rowsTexture = gl.createTexture() as WebGLTexture;
|
|
@@ -144,15 +173,43 @@ export class GpuTextures {
|
|
|
144
173
|
this.growLines(textures.lines);
|
|
145
174
|
}
|
|
146
175
|
|
|
147
|
-
|
|
176
|
+
const pending = textures.pending;
|
|
177
|
+
let ramps = 0;
|
|
178
|
+
for (let k = 0; k < pending.length; k++) {
|
|
179
|
+
ramps += textures.get(pending[k])?.ramp ? 1 : 0;
|
|
180
|
+
}
|
|
181
|
+
|
|
182
|
+
const onGpu = ramps >= RAMPS_ON_GPU || !LITTLE_ENDIAN;
|
|
183
|
+
for (let k = 0; k < pending.length; k++) {
|
|
184
|
+
const id = pending[k];
|
|
148
185
|
const data = textures.get(id);
|
|
149
186
|
const fill = data?.pixels ? null : (data?.fill ?? null);
|
|
150
187
|
if (!data || (!data.pixels && !data.source && fill === null)) {
|
|
151
188
|
continue;
|
|
152
189
|
}
|
|
153
190
|
|
|
191
|
+
if (data.line !== undefined && data.pixels instanceof Uint8Array && data.ramp) {
|
|
192
|
+
const line = data.line;
|
|
193
|
+
const record =
|
|
194
|
+
data.pixels.length === RAMP_BYTES ? data.pixels : data.pixels.subarray(0, RAMP_BYTES);
|
|
195
|
+
this.rampRecords.set(record, line * RAMP_BYTES);
|
|
196
|
+
this.built[line] = onGpu ? 1 : 0;
|
|
197
|
+
this.textures[id] = this.lines;
|
|
198
|
+
data.pixels = null;
|
|
199
|
+
if (onGpu) {
|
|
200
|
+
this.build(line);
|
|
201
|
+
continue;
|
|
202
|
+
}
|
|
203
|
+
|
|
204
|
+
rampOf(record, this.lineWords, true, line * LINE_TEXELS);
|
|
205
|
+
linesFrom = Math.min(linesFrom, line);
|
|
206
|
+
linesTo = Math.max(linesTo, line + 1);
|
|
207
|
+
continue;
|
|
208
|
+
}
|
|
209
|
+
|
|
154
210
|
if (data.line !== undefined && data.pixels instanceof Uint8Array) {
|
|
155
211
|
this.lineBytes.set(data.pixels.subarray(0, LINE_TEXELS * 4), data.line * LINE_TEXELS * 4);
|
|
212
|
+
this.built[data.line] = 0;
|
|
156
213
|
this.textures[id] = this.lines;
|
|
157
214
|
linesFrom = Math.min(linesFrom, data.line);
|
|
158
215
|
linesTo = Math.max(linesTo, data.line + 1);
|
|
@@ -290,8 +347,34 @@ export class GpuTextures {
|
|
|
290
347
|
textures.pending.length = 0;
|
|
291
348
|
|
|
292
349
|
if (grown || linesTo > 0) {
|
|
293
|
-
|
|
350
|
+
const from = grown ? 0 : linesFrom;
|
|
351
|
+
const to = grown ? this.lineRoom : linesTo;
|
|
352
|
+
this.uploadLines(from, to);
|
|
353
|
+
// Lines built from records in between had their bytes overwritten with none: built again.
|
|
354
|
+
for (let line = from; line < to; line++) {
|
|
355
|
+
if (this.built[line]) {
|
|
356
|
+
this.build(line);
|
|
357
|
+
}
|
|
358
|
+
}
|
|
359
|
+
}
|
|
360
|
+
}
|
|
361
|
+
|
|
362
|
+
/** Line `line` built from its record in the backend's next pass, once however often asked. */
|
|
363
|
+
private build(line: number): void {
|
|
364
|
+
if (!this.building[line]) {
|
|
365
|
+
this.building[line] = 1;
|
|
366
|
+
this.builds.push(line);
|
|
367
|
+
}
|
|
368
|
+
}
|
|
369
|
+
|
|
370
|
+
/** The lines built (builds), done with. */
|
|
371
|
+
builtAll(): void {
|
|
372
|
+
const builds = this.builds;
|
|
373
|
+
for (let i = 0; i < builds.length; i++) {
|
|
374
|
+
this.building[builds[i]] = 0;
|
|
294
375
|
}
|
|
376
|
+
|
|
377
|
+
this.builds.length = 0;
|
|
295
378
|
}
|
|
296
379
|
|
|
297
380
|
/**
|
|
@@ -303,6 +386,16 @@ export class GpuTextures {
|
|
|
303
386
|
const bytes = new Uint8Array(room * LINE_TEXELS * 4);
|
|
304
387
|
bytes.set(this.lineBytes);
|
|
305
388
|
this.lineBytes = bytes;
|
|
389
|
+
this.lineWords = new Uint32Array(bytes.buffer);
|
|
390
|
+
const records = new Uint8Array(room * RAMP_BYTES);
|
|
391
|
+
records.set(this.rampRecords);
|
|
392
|
+
this.rampRecords = records;
|
|
393
|
+
const built = new Uint8Array(room);
|
|
394
|
+
built.set(this.built);
|
|
395
|
+
this.built = built;
|
|
396
|
+
const building = new Uint8Array(room);
|
|
397
|
+
building.set(this.building);
|
|
398
|
+
this.building = building;
|
|
306
399
|
this.lineRoom = room;
|
|
307
400
|
this.lines ??= this.gl.createTexture() as WebGLTexture;
|
|
308
401
|
}
|
|
@@ -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
|
+
}
|