UNPKG

6 kBJavaScriptView Raw
1'use strict';
2
3Object.defineProperty(exports, '__esModule', { value: true });
4
5var uniformParsers = require('./uniformParsers.js');
6
7const GLSL_TO_SINGLE_SETTERS_CACHED = {
8 float: `
9 if (cv !== v)
10 {
11 cu.value = v;
12 gl.uniform1f(location, v);
13 }`,
14 vec2: `
15 if (cv[0] !== v[0] || cv[1] !== v[1])
16 {
17 cv[0] = v[0];
18 cv[1] = v[1];
19
20 gl.uniform2f(location, v[0], v[1])
21 }`,
22 vec3: `
23 if (cv[0] !== v[0] || cv[1] !== v[1] || cv[2] !== v[2])
24 {
25 cv[0] = v[0];
26 cv[1] = v[1];
27 cv[2] = v[2];
28
29 gl.uniform3f(location, v[0], v[1], v[2])
30 }`,
31 vec4: `
32 if (cv[0] !== v[0] || cv[1] !== v[1] || cv[2] !== v[2] || cv[3] !== v[3])
33 {
34 cv[0] = v[0];
35 cv[1] = v[1];
36 cv[2] = v[2];
37 cv[3] = v[3];
38
39 gl.uniform4f(location, v[0], v[1], v[2], v[3]);
40 }`,
41 int: `
42 if (cv !== v)
43 {
44 cu.value = v;
45
46 gl.uniform1i(location, v);
47 }`,
48 ivec2: `
49 if (cv[0] !== v[0] || cv[1] !== v[1])
50 {
51 cv[0] = v[0];
52 cv[1] = v[1];
53
54 gl.uniform2i(location, v[0], v[1]);
55 }`,
56 ivec3: `
57 if (cv[0] !== v[0] || cv[1] !== v[1] || cv[2] !== v[2])
58 {
59 cv[0] = v[0];
60 cv[1] = v[1];
61 cv[2] = v[2];
62
63 gl.uniform3i(location, v[0], v[1], v[2]);
64 }`,
65 ivec4: `
66 if (cv[0] !== v[0] || cv[1] !== v[1] || cv[2] !== v[2] || cv[3] !== v[3])
67 {
68 cv[0] = v[0];
69 cv[1] = v[1];
70 cv[2] = v[2];
71 cv[3] = v[3];
72
73 gl.uniform4i(location, v[0], v[1], v[2], v[3]);
74 }`,
75 uint: `
76 if (cv !== v)
77 {
78 cu.value = v;
79
80 gl.uniform1ui(location, v);
81 }`,
82 uvec2: `
83 if (cv[0] !== v[0] || cv[1] !== v[1])
84 {
85 cv[0] = v[0];
86 cv[1] = v[1];
87
88 gl.uniform2ui(location, v[0], v[1]);
89 }`,
90 uvec3: `
91 if (cv[0] !== v[0] || cv[1] !== v[1] || cv[2] !== v[2])
92 {
93 cv[0] = v[0];
94 cv[1] = v[1];
95 cv[2] = v[2];
96
97 gl.uniform3ui(location, v[0], v[1], v[2]);
98 }`,
99 uvec4: `
100 if (cv[0] !== v[0] || cv[1] !== v[1] || cv[2] !== v[2] || cv[3] !== v[3])
101 {
102 cv[0] = v[0];
103 cv[1] = v[1];
104 cv[2] = v[2];
105 cv[3] = v[3];
106
107 gl.uniform4ui(location, v[0], v[1], v[2], v[3]);
108 }`,
109 bool: `
110 if (cv !== v)
111 {
112 cu.value = v;
113 gl.uniform1i(location, v);
114 }`,
115 bvec2: `
116 if (cv[0] != v[0] || cv[1] != v[1])
117 {
118 cv[0] = v[0];
119 cv[1] = v[1];
120
121 gl.uniform2i(location, v[0], v[1]);
122 }`,
123 bvec3: `
124 if (cv[0] !== v[0] || cv[1] !== v[1] || cv[2] !== v[2])
125 {
126 cv[0] = v[0];
127 cv[1] = v[1];
128 cv[2] = v[2];
129
130 gl.uniform3i(location, v[0], v[1], v[2]);
131 }`,
132 bvec4: `
133 if (cv[0] !== v[0] || cv[1] !== v[1] || cv[2] !== v[2] || cv[3] !== v[3])
134 {
135 cv[0] = v[0];
136 cv[1] = v[1];
137 cv[2] = v[2];
138 cv[3] = v[3];
139
140 gl.uniform4i(location, v[0], v[1], v[2], v[3]);
141 }`,
142 mat2: "gl.uniformMatrix2fv(location, false, v)",
143 mat3: "gl.uniformMatrix3fv(location, false, v)",
144 mat4: "gl.uniformMatrix4fv(location, false, v)",
145 sampler2D: `
146 if (cv !== v)
147 {
148 cu.value = v;
149
150 gl.uniform1i(location, v);
151 }`,
152 samplerCube: `
153 if (cv !== v)
154 {
155 cu.value = v;
156
157 gl.uniform1i(location, v);
158 }`,
159 sampler2DArray: `
160 if (cv !== v)
161 {
162 cu.value = v;
163
164 gl.uniform1i(location, v);
165 }`
166};
167const GLSL_TO_ARRAY_SETTERS = {
168 float: `gl.uniform1fv(location, v)`,
169 vec2: `gl.uniform2fv(location, v)`,
170 vec3: `gl.uniform3fv(location, v)`,
171 vec4: "gl.uniform4fv(location, v)",
172 mat4: "gl.uniformMatrix4fv(location, false, v)",
173 mat3: "gl.uniformMatrix3fv(location, false, v)",
174 mat2: "gl.uniformMatrix2fv(location, false, v)",
175 int: "gl.uniform1iv(location, v)",
176 ivec2: "gl.uniform2iv(location, v)",
177 ivec3: "gl.uniform3iv(location, v)",
178 ivec4: "gl.uniform4iv(location, v)",
179 uint: "gl.uniform1uiv(location, v)",
180 uvec2: "gl.uniform2uiv(location, v)",
181 uvec3: "gl.uniform3uiv(location, v)",
182 uvec4: "gl.uniform4uiv(location, v)",
183 bool: "gl.uniform1iv(location, v)",
184 bvec2: "gl.uniform2iv(location, v)",
185 bvec3: "gl.uniform3iv(location, v)",
186 bvec4: "gl.uniform4iv(location, v)",
187 sampler2D: "gl.uniform1iv(location, v)",
188 samplerCube: "gl.uniform1iv(location, v)",
189 sampler2DArray: "gl.uniform1iv(location, v)"
190};
191function generateUniformsSync(group, uniformData) {
192 const funcFragments = [`
193 var v = null;
194 var cv = null;
195 var cu = null;
196 var t = 0;
197 var gl = renderer.gl;
198 `];
199 for (const i in group.uniforms) {
200 const data = uniformData[i];
201 if (!data) {
202 if (group.uniforms[i]?.group) {
203 if (group.uniforms[i].ubo) {
204 funcFragments.push(`
205 renderer.shader.syncUniformBufferGroup(uv.${i}, '${i}');
206 `);
207 } else {
208 funcFragments.push(`
209 renderer.shader.syncUniformGroup(uv.${i}, syncData);
210 `);
211 }
212 }
213 continue;
214 }
215 const uniform = group.uniforms[i];
216 let parsed = false;
217 for (let j = 0; j < uniformParsers.uniformParsers.length; j++) {
218 if (uniformParsers.uniformParsers[j].test(data, uniform)) {
219 funcFragments.push(uniformParsers.uniformParsers[j].code(i, uniform));
220 parsed = true;
221 break;
222 }
223 }
224 if (!parsed) {
225 const templateType = data.size === 1 && !data.isArray ? GLSL_TO_SINGLE_SETTERS_CACHED : GLSL_TO_ARRAY_SETTERS;
226 const template = templateType[data.type].replace("location", `ud["${i}"].location`);
227 funcFragments.push(`
228 cu = ud["${i}"];
229 cv = cu.value;
230 v = uv["${i}"];
231 ${template};`);
232 }
233 }
234 return new Function("ud", "uv", "renderer", "syncData", funcFragments.join("\n"));
235}
236
237exports.generateUniformsSync = generateUniformsSync;
238//# sourceMappingURL=generateUniformsSync.js.map