forked from Rezmason/matrix
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.js
More file actions
172 lines (150 loc) · 5.72 KB
/
Copy pathmain.js
File metadata and controls
172 lines (150 loc) · 5.72 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
import { makeFullScreenQuad, makePipeline } from "./utils.js";
import makeRain from "./rainPass.js";
import makeBloomPass from "./bloomPass.js";
import makePalettePass from "./palettePass.js";
import makeStripePass from "./stripePass.js";
import makeImagePass from "./imagePass.js";
import makeQuiltPass from "./quiltPass.js";
import makeMirrorPass from "./mirrorPass.js";
import { setupCamera, cameraCanvas, cameraAspectRatio } from "../camera.js";
import getLKG from "./lkgHelper.js";
import { setupFullscreenToggle } from "../fullscreen.js";
import { createEffectsMapping, getEffectPass } from "../effects.js";
const dimensions = { width: 1, height: 1 };
/**
* Surface the first shader compile / program link failure (otherwise the browser only shows
* INVALID_OPERATION: useProgram: program not valid and then suppresses further errors).
*/
function installWebGLShaderDebugHooks() {
if (typeof WebGLRenderingContext === "undefined") {
return;
}
const proto = WebGLRenderingContext.prototype;
if (proto.__matrixShaderDebugHooked) {
return;
}
proto.__matrixShaderDebugHooked = true;
const compileShader = proto.compileShader;
proto.compileShader = function (shader) {
compileShader.call(this, shader);
if (!this.getShaderParameter(shader, this.COMPILE_STATUS)) {
console.error("[Matrix][WebGL] shader compile failed:\n", this.getShaderInfoLog(shader));
}
};
const linkProgram = proto.linkProgram;
proto.linkProgram = function (program) {
linkProgram.call(this, program);
if (!this.getProgramParameter(program, this.LINK_STATUS)) {
console.error("[Matrix][WebGL] program link failed:\n", this.getProgramInfoLog(program));
}
};
}
const loadJS = (src) =>
new Promise((resolve, reject) => {
const tag = document.createElement("script");
tag.onload = resolve;
tag.onerror = reject;
tag.src = src;
document.body.appendChild(tag);
});
export default async (canvas, config) => {
await Promise.all([loadJS("lib/regl.min.js"), loadJS("lib/gl-matrix.js")]);
installWebGLShaderDebugHooks();
const resize = () => {
const devicePixelRatio = window.devicePixelRatio ?? 1;
canvas.width = Math.ceil(canvas.clientWidth * devicePixelRatio * config.resolution);
canvas.height = Math.ceil(canvas.clientHeight * devicePixelRatio * config.resolution);
};
window.onresize = resize;
// Setup fullscreen toggle with proper cleanup
const cleanupFullscreen = setupFullscreenToggle(canvas);
// Recalculate canvas size when entering/exiting fullscreen.
// Chrome does not always fire window resize with updated clientWidth/clientHeight
// when entering/exiting fullscreen, so the render canvas keeps its pre-fullscreen
// bitmap size and looks badly scaled in fullscreen only.
const recalcOnFullscreenChange = () => {
resize();
};
document.addEventListener("fullscreenchange", recalcOnFullscreenChange);
document.addEventListener("webkitfullscreenchange", recalcOnFullscreenChange);
document.addEventListener("mozfullscreenchange", recalcOnFullscreenChange);
document.addEventListener("MSFullscreenChange", recalcOnFullscreenChange);
resize();
if (config.useCamera) {
await setupCamera();
}
const extensions = [
"OES_texture_half_float",
"OES_texture_half_float_linear",
"OES_standard_derivatives",
// Required to render into half-float FBOs (compute passes + rain); without it, framebuffer attachments can be incomplete.
"EXT_color_buffer_half_float",
];
// rainPass.frag.glsl uses fwidth() for MSDF anti-aliasing (OES_standard_derivatives).
// Some older stacks only expose float renderbuffers under a different name — regl may still enable it.
const optionalExtensions = ["WEBGL_color_buffer_float"];
switch (config.testFix) {
case "fwidth_10_1_2022_A":
extensions.push("OES_standard_derivatives");
break;
case "fwidth_10_1_2022_B":
optionalExtensions.forEach((ext) => extensions.push(ext));
extensions.length = 0;
break;
}
const regl = createREGL({
canvas,
pixelRatio: 1,
extensions,
optionalExtensions,
});
const cameraTex = regl.texture(cameraCanvas);
const lkg = await getLKG(config.useHoloplay, true);
// Create dynamic effects mapping
const passModules = {
makePalettePass,
makeStripePass,
makeImagePass,
makeMirrorPass,
};
const effects = createEffectsMapping("webgl", passModules);
const effectPass = getEffectPass(config.effect, effects, "palette");
const context = { regl, canvas, config, lkg, cameraTex, cameraAspectRatio };
const pipeline = makePipeline(context, [makeRain, makeBloomPass, effectPass, makeQuiltPass]);
const screenUniforms = { tex: pipeline[pipeline.length - 1].outputs.primary };
// Blit the final texture to the canvas. A nested `regl({ uniforms })` inside another draw can
// inherit the last pass's framebuffer binding, so the screen never gets the composed image.
const blitToCanvas = makeFullScreenQuad(regl, screenUniforms);
await Promise.all(pipeline.map((step) => step.ready));
const targetFrameTimeMilliseconds = 1000 / config.fps;
let last = NaN;
const tick = regl.frame(({ viewportWidth, viewportHeight }) => {
if (config.once) {
tick.cancel();
}
const now = regl.now() * 1000;
if (isNaN(last)) {
last = now;
}
const shouldRender = config.fps >= 60 || now - last >= targetFrameTimeMilliseconds || config.once == true;
if (shouldRender) {
while (now - targetFrameTimeMilliseconds > last) {
last += targetFrameTimeMilliseconds;
}
}
if (config.useCamera) {
cameraTex(cameraCanvas);
}
if (dimensions.width !== viewportWidth || dimensions.height !== viewportHeight) {
dimensions.width = viewportWidth;
dimensions.height = viewportHeight;
for (const step of pipeline) {
step.setSize(viewportWidth, viewportHeight);
}
}
for (const step of pipeline) {
step.execute(shouldRender);
}
blitToCanvas();
});
};