1 | const fragTemplate = [
|
2 | "precision mediump float;",
|
3 | "void main(void){",
|
4 | "float test = 0.1;",
|
5 | "%forloop%",
|
6 | "gl_FragColor = vec4(0.0);",
|
7 | "}"
|
8 | ].join("\n");
|
9 | function generateIfTestSrc(maxIfs) {
|
10 | let src = "";
|
11 | for (let i = 0; i < maxIfs; ++i) {
|
12 | if (i > 0) {
|
13 | src += "\nelse ";
|
14 | }
|
15 | if (i < maxIfs - 1) {
|
16 | src += `if(test == ${i}.0){}`;
|
17 | }
|
18 | }
|
19 | return src;
|
20 | }
|
21 | function checkMaxIfStatementsInShader(maxIfs, gl) {
|
22 | if (maxIfs === 0) {
|
23 | throw new Error("Invalid value of `0` passed to `checkMaxIfStatementsInShader`");
|
24 | }
|
25 | const shader = gl.createShader(gl.FRAGMENT_SHADER);
|
26 | while (true) {
|
27 | const fragmentSrc = fragTemplate.replace(/%forloop%/gi, generateIfTestSrc(maxIfs));
|
28 | gl.shaderSource(shader, fragmentSrc);
|
29 | gl.compileShader(shader);
|
30 | if (!gl.getShaderParameter(shader, gl.COMPILE_STATUS)) {
|
31 | maxIfs = maxIfs / 2 | 0;
|
32 | } else {
|
33 | break;
|
34 | }
|
35 | }
|
36 | return maxIfs;
|
37 | }
|
38 |
|
39 | export { checkMaxIfStatementsInShader };
|
40 |
|