UNPKG

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