2 //Copyright (C) 2002-2005 3Dlabs Inc. Ltd.
5 //Redistribution and use in source and binary forms, with or without
6 //modification, are permitted provided that the following conditions
9 // Redistributions of source code must retain the above copyright
10 // notice, this list of conditions and the following disclaimer.
12 // Redistributions in binary form must reproduce the above
13 // copyright notice, this list of conditions and the following
14 // disclaimer in the documentation and/or other materials provided
15 // with the distribution.
17 // Neither the name of 3Dlabs Inc. Ltd. nor the names of its
18 // contributors may be used to endorse or promote products derived
19 // from this software without specific prior written permission.
21 //THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
22 //"AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
23 //LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
24 //FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
25 //COPYRIGHT HOLDERS OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
26 //INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
27 //BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
28 //LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
29 //CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
30 //LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
31 //ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
32 //POSSIBILITY OF SUCH DAMAGE.
36 // Create strings that declare built-in definitions, add built-ins that
37 // cannot be expressed in the files, and establish mappings between
38 // built-in functions and operators.
41 #include "../Include/intermediate.h"
42 #include "Initialize.h"
44 void TBuiltIns::initialize()
47 // Initialize all the built-in strings for parsing.
49 TString BuiltInFunctions
;
50 TString BuiltInFunctionsVertex
;
51 TString BuiltInFunctionsFragment
;
52 TString StandardVertexVaryings
;
53 TString StandardFragmentVaryings
;
54 TString StandardVertexAttributes
;
55 TString StandardUniforms
;
58 //============================================================================
60 // Prototypes for built-in functions seen by both vertex and fragment shaders.
62 //============================================================================
64 TString
& s
= BuiltInFunctions
;
67 // Angle and Trigonometric Functions.
69 s
.append(TString("float radians(float degrees);"));
70 s
.append(TString("vec2 radians(vec2 degrees);"));
71 s
.append(TString("vec3 radians(vec3 degrees);"));
72 s
.append(TString("vec4 radians(vec4 degrees);"));
74 s
.append(TString("float degrees(float radians);"));
75 s
.append(TString("vec2 degrees(vec2 radians);"));
76 s
.append(TString("vec3 degrees(vec3 radians);"));
77 s
.append(TString("vec4 degrees(vec4 radians);"));
79 s
.append(TString("float sin(float angle);"));
80 s
.append(TString("vec2 sin(vec2 angle);"));
81 s
.append(TString("vec3 sin(vec3 angle);"));
82 s
.append(TString("vec4 sin(vec4 angle);"));
84 s
.append(TString("float cos(float angle);"));
85 s
.append(TString("vec2 cos(vec2 angle);"));
86 s
.append(TString("vec3 cos(vec3 angle);"));
87 s
.append(TString("vec4 cos(vec4 angle);"));
89 s
.append(TString("float tan(float angle);"));
90 s
.append(TString("vec2 tan(vec2 angle);"));
91 s
.append(TString("vec3 tan(vec3 angle);"));
92 s
.append(TString("vec4 tan(vec4 angle);"));
94 s
.append(TString("float asin(float x);"));
95 s
.append(TString("vec2 asin(vec2 x);"));
96 s
.append(TString("vec3 asin(vec3 x);"));
97 s
.append(TString("vec4 asin(vec4 x);"));
99 s
.append(TString("float acos(float x);"));
100 s
.append(TString("vec2 acos(vec2 x);"));
101 s
.append(TString("vec3 acos(vec3 x);"));
102 s
.append(TString("vec4 acos(vec4 x);"));
104 s
.append(TString("float atan(float y, float x);"));
105 s
.append(TString("vec2 atan(vec2 y, vec2 x);"));
106 s
.append(TString("vec3 atan(vec3 y, vec3 x);"));
107 s
.append(TString("vec4 atan(vec4 y, vec4 x);"));
109 s
.append(TString("float atan(float y_over_x);"));
110 s
.append(TString("vec2 atan(vec2 y_over_x);"));
111 s
.append(TString("vec3 atan(vec3 y_over_x);"));
112 s
.append(TString("vec4 atan(vec4 y_over_x);"));
115 // Exponential Functions.
117 s
.append(TString("float pow(float x, float y);"));
118 s
.append(TString("vec2 pow(vec2 x, vec2 y);"));
119 s
.append(TString("vec3 pow(vec3 x, vec3 y);"));
120 s
.append(TString("vec4 pow(vec4 x, vec4 y);"));
122 s
.append(TString("float exp(float x);"));
123 s
.append(TString("vec2 exp(vec2 x);"));
124 s
.append(TString("vec3 exp(vec3 x);"));
125 s
.append(TString("vec4 exp(vec4 x);"));
127 s
.append(TString("float log(float x);"));
128 s
.append(TString("vec2 log(vec2 x);"));
129 s
.append(TString("vec3 log(vec3 x);"));
130 s
.append(TString("vec4 log(vec4 x);"));
132 s
.append(TString("float exp2(float x);"));
133 s
.append(TString("vec2 exp2(vec2 x);"));
134 s
.append(TString("vec3 exp2(vec3 x);"));
135 s
.append(TString("vec4 exp2(vec4 x);"));
137 s
.append(TString("float log2(float x);"));
138 s
.append(TString("vec2 log2(vec2 x);"));
139 s
.append(TString("vec3 log2(vec3 x);"));
140 s
.append(TString("vec4 log2(vec4 x);"));
142 s
.append(TString("float sqrt(float x);"));
143 s
.append(TString("vec2 sqrt(vec2 x);"));
144 s
.append(TString("vec3 sqrt(vec3 x);"));
145 s
.append(TString("vec4 sqrt(vec4 x);"));
147 s
.append(TString("float inversesqrt(float x);"));
148 s
.append(TString("vec2 inversesqrt(vec2 x);"));
149 s
.append(TString("vec3 inversesqrt(vec3 x);"));
150 s
.append(TString("vec4 inversesqrt(vec4 x);"));
155 s
.append(TString("float abs(float x);"));
156 s
.append(TString("vec2 abs(vec2 x);"));
157 s
.append(TString("vec3 abs(vec3 x);"));
158 s
.append(TString("vec4 abs(vec4 x);"));
160 s
.append(TString("float sign(float x);"));
161 s
.append(TString("vec2 sign(vec2 x);"));
162 s
.append(TString("vec3 sign(vec3 x);"));
163 s
.append(TString("vec4 sign(vec4 x);"));
165 s
.append(TString("float floor(float x);"));
166 s
.append(TString("vec2 floor(vec2 x);"));
167 s
.append(TString("vec3 floor(vec3 x);"));
168 s
.append(TString("vec4 floor(vec4 x);"));
170 s
.append(TString("float ceil(float x);"));
171 s
.append(TString("vec2 ceil(vec2 x);"));
172 s
.append(TString("vec3 ceil(vec3 x);"));
173 s
.append(TString("vec4 ceil(vec4 x);"));
175 s
.append(TString("float fract(float x);"));
176 s
.append(TString("vec2 fract(vec2 x);"));
177 s
.append(TString("vec3 fract(vec3 x);"));
178 s
.append(TString("vec4 fract(vec4 x);"));
180 s
.append(TString("float mod(float x, float y);"));
181 s
.append(TString("vec2 mod(vec2 x, float y);"));
182 s
.append(TString("vec3 mod(vec3 x, float y);"));
183 s
.append(TString("vec4 mod(vec4 x, float y);"));
184 s
.append(TString("vec2 mod(vec2 x, vec2 y);"));
185 s
.append(TString("vec3 mod(vec3 x, vec3 y);"));
186 s
.append(TString("vec4 mod(vec4 x, vec4 y);"));
188 s
.append(TString("float min(float x, float y);"));
189 s
.append(TString("vec2 min(vec2 x, float y);"));
190 s
.append(TString("vec3 min(vec3 x, float y);"));
191 s
.append(TString("vec4 min(vec4 x, float y);"));
192 s
.append(TString("vec2 min(vec2 x, vec2 y);"));
193 s
.append(TString("vec3 min(vec3 x, vec3 y);"));
194 s
.append(TString("vec4 min(vec4 x, vec4 y);"));
196 s
.append(TString("float max(float x, float y);"));
197 s
.append(TString("vec2 max(vec2 x, float y);"));
198 s
.append(TString("vec3 max(vec3 x, float y);"));
199 s
.append(TString("vec4 max(vec4 x, float y);"));
200 s
.append(TString("vec2 max(vec2 x, vec2 y);"));
201 s
.append(TString("vec3 max(vec3 x, vec3 y);"));
202 s
.append(TString("vec4 max(vec4 x, vec4 y);"));
204 s
.append(TString("float clamp(float x, float minVal, float maxVal);"));
205 s
.append(TString("vec2 clamp(vec2 x, float minVal, float maxVal);"));
206 s
.append(TString("vec3 clamp(vec3 x, float minVal, float maxVal);"));
207 s
.append(TString("vec4 clamp(vec4 x, float minVal, float maxVal);"));
208 s
.append(TString("vec2 clamp(vec2 x, vec2 minVal, vec2 maxVal);"));
209 s
.append(TString("vec3 clamp(vec3 x, vec3 minVal, vec3 maxVal);"));
210 s
.append(TString("vec4 clamp(vec4 x, vec4 minVal, vec4 maxVal);"));
212 s
.append(TString("float mix(float x, float y, float a);"));
213 s
.append(TString("vec2 mix(vec2 x, vec2 y, float a);"));
214 s
.append(TString("vec3 mix(vec3 x, vec3 y, float a);"));
215 s
.append(TString("vec4 mix(vec4 x, vec4 y, float a);"));
216 s
.append(TString("vec2 mix(vec2 x, vec2 y, vec2 a);"));
217 s
.append(TString("vec3 mix(vec3 x, vec3 y, vec3 a);"));
218 s
.append(TString("vec4 mix(vec4 x, vec4 y, vec4 a);"));
220 s
.append(TString("float step(float edge, float x);"));
221 s
.append(TString("vec2 step(vec2 edge, vec2 x);"));
222 s
.append(TString("vec3 step(vec3 edge, vec3 x);"));
223 s
.append(TString("vec4 step(vec4 edge, vec4 x);"));
224 s
.append(TString("vec2 step(float edge, vec2 x);"));
225 s
.append(TString("vec3 step(float edge, vec3 x);"));
226 s
.append(TString("vec4 step(float edge, vec4 x);"));
228 s
.append(TString("float smoothstep(float edge0, float edge1, float x);"));
229 s
.append(TString("vec2 smoothstep(vec2 edge0, vec2 edge1, vec2 x);"));
230 s
.append(TString("vec3 smoothstep(vec3 edge0, vec3 edge1, vec3 x);"));
231 s
.append(TString("vec4 smoothstep(vec4 edge0, vec4 edge1, vec4 x);"));
232 s
.append(TString("vec2 smoothstep(float edge0, float edge1, vec2 x);"));
233 s
.append(TString("vec3 smoothstep(float edge0, float edge1, vec3 x);"));
234 s
.append(TString("vec4 smoothstep(float edge0, float edge1, vec4 x);"));
237 // Geometric Functions.
239 s
.append(TString("float length(float x);"));
240 s
.append(TString("float length(vec2 x);"));
241 s
.append(TString("float length(vec3 x);"));
242 s
.append(TString("float length(vec4 x);"));
244 s
.append(TString("float distance(float p0, float p1);"));
245 s
.append(TString("float distance(vec2 p0, vec2 p1);"));
246 s
.append(TString("float distance(vec3 p0, vec3 p1);"));
247 s
.append(TString("float distance(vec4 p0, vec4 p1);"));
249 s
.append(TString("float dot(float x, float y);"));
250 s
.append(TString("float dot(vec2 x, vec2 y);"));
251 s
.append(TString("float dot(vec3 x, vec3 y);"));
252 s
.append(TString("float dot(vec4 x, vec4 y);"));
254 s
.append(TString("vec3 cross(vec3 x, vec3 y);"));
255 s
.append(TString("float normalize(float x);"));
256 s
.append(TString("vec2 normalize(vec2 x);"));
257 s
.append(TString("vec3 normalize(vec3 x);"));
258 s
.append(TString("vec4 normalize(vec4 x);"));
260 s
.append(TString("float faceforward(float N, float I, float Nref);"));
261 s
.append(TString("vec2 faceforward(vec2 N, vec2 I, vec2 Nref);"));
262 s
.append(TString("vec3 faceforward(vec3 N, vec3 I, vec3 Nref);"));
263 s
.append(TString("vec4 faceforward(vec4 N, vec4 I, vec4 Nref);"));
265 s
.append(TString("float reflect(float I, float N);"));
266 s
.append(TString("vec2 reflect(vec2 I, vec2 N);"));
267 s
.append(TString("vec3 reflect(vec3 I, vec3 N);"));
268 s
.append(TString("vec4 reflect(vec4 I, vec4 N);"));
270 s
.append(TString("float refract(float I, float N, float eta);"));
271 s
.append(TString("vec2 refract(vec2 I, vec2 N, float eta);"));
272 s
.append(TString("vec3 refract(vec3 I, vec3 N, float eta);"));
273 s
.append(TString("vec4 refract(vec4 I, vec4 N, float eta);"));
278 s
.append(TString("mat2 matrixCompMult(mat2 x, mat2 y);"));
279 s
.append(TString("mat3 matrixCompMult(mat3 x, mat3 y);"));
280 s
.append(TString("mat4 matrixCompMult(mat4 x, mat4 y);"));
283 // Vector relational functions.
285 s
.append(TString("bvec2 lessThan(vec2 x, vec2 y);"));
286 s
.append(TString("bvec3 lessThan(vec3 x, vec3 y);"));
287 s
.append(TString("bvec4 lessThan(vec4 x, vec4 y);"));
289 s
.append(TString("bvec2 lessThan(ivec2 x, ivec2 y);"));
290 s
.append(TString("bvec3 lessThan(ivec3 x, ivec3 y);"));
291 s
.append(TString("bvec4 lessThan(ivec4 x, ivec4 y);"));
293 s
.append(TString("bvec2 lessThanEqual(vec2 x, vec2 y);"));
294 s
.append(TString("bvec3 lessThanEqual(vec3 x, vec3 y);"));
295 s
.append(TString("bvec4 lessThanEqual(vec4 x, vec4 y);"));
297 s
.append(TString("bvec2 lessThanEqual(ivec2 x, ivec2 y);"));
298 s
.append(TString("bvec3 lessThanEqual(ivec3 x, ivec3 y);"));
299 s
.append(TString("bvec4 lessThanEqual(ivec4 x, ivec4 y);"));
301 s
.append(TString("bvec2 greaterThan(vec2 x, vec2 y);"));
302 s
.append(TString("bvec3 greaterThan(vec3 x, vec3 y);"));
303 s
.append(TString("bvec4 greaterThan(vec4 x, vec4 y);"));
305 s
.append(TString("bvec2 greaterThan(ivec2 x, ivec2 y);"));
306 s
.append(TString("bvec3 greaterThan(ivec3 x, ivec3 y);"));
307 s
.append(TString("bvec4 greaterThan(ivec4 x, ivec4 y);"));
309 s
.append(TString("bvec2 greaterThanEqual(vec2 x, vec2 y);"));
310 s
.append(TString("bvec3 greaterThanEqual(vec3 x, vec3 y);"));
311 s
.append(TString("bvec4 greaterThanEqual(vec4 x, vec4 y);"));
313 s
.append(TString("bvec2 greaterThanEqual(ivec2 x, ivec2 y);"));
314 s
.append(TString("bvec3 greaterThanEqual(ivec3 x, ivec3 y);"));
315 s
.append(TString("bvec4 greaterThanEqual(ivec4 x, ivec4 y);"));
317 s
.append(TString("bvec2 equal(vec2 x, vec2 y);"));
318 s
.append(TString("bvec3 equal(vec3 x, vec3 y);"));
319 s
.append(TString("bvec4 equal(vec4 x, vec4 y);"));
321 s
.append(TString("bvec2 equal(ivec2 x, ivec2 y);"));
322 s
.append(TString("bvec3 equal(ivec3 x, ivec3 y);"));
323 s
.append(TString("bvec4 equal(ivec4 x, ivec4 y);"));
325 s
.append(TString("bvec2 equal(bvec2 x, bvec2 y);"));
326 s
.append(TString("bvec3 equal(bvec3 x, bvec3 y);"));
327 s
.append(TString("bvec4 equal(bvec4 x, bvec4 y);"));
329 s
.append(TString("bvec2 notEqual(vec2 x, vec2 y);"));
330 s
.append(TString("bvec3 notEqual(vec3 x, vec3 y);"));
331 s
.append(TString("bvec4 notEqual(vec4 x, vec4 y);"));
333 s
.append(TString("bvec2 notEqual(ivec2 x, ivec2 y);"));
334 s
.append(TString("bvec3 notEqual(ivec3 x, ivec3 y);"));
335 s
.append(TString("bvec4 notEqual(ivec4 x, ivec4 y);"));
337 s
.append(TString("bvec2 notEqual(bvec2 x, bvec2 y);"));
338 s
.append(TString("bvec3 notEqual(bvec3 x, bvec3 y);"));
339 s
.append(TString("bvec4 notEqual(bvec4 x, bvec4 y);"));
341 s
.append(TString("bool any(bvec2 x);"));
342 s
.append(TString("bool any(bvec3 x);"));
343 s
.append(TString("bool any(bvec4 x);"));
345 s
.append(TString("bool all(bvec2 x);"));
346 s
.append(TString("bool all(bvec3 x);"));
347 s
.append(TString("bool all(bvec4 x);"));
349 s
.append(TString("bvec2 not(bvec2 x);"));
350 s
.append(TString("bvec3 not(bvec3 x);"));
351 s
.append(TString("bvec4 not(bvec4 x);"));
354 // Texture Functions.
356 s
.append(TString("vec4 texture1D(sampler1D sampler, float coord);"));
357 s
.append(TString("vec4 texture1DProj(sampler1D sampler, vec2 coord);"));
358 s
.append(TString("vec4 texture1DProj(sampler1D sampler, vec4 coord);"));
360 s
.append(TString("vec4 texture2D(sampler2D sampler, vec2 coord);"));
361 s
.append(TString("vec4 texture2DProj(sampler2D sampler, vec3 coord);"));
362 s
.append(TString("vec4 texture2DProj(sampler2D sampler, vec4 coord);"));
364 s
.append(TString("vec4 texture3D(sampler3D sampler, vec3 coord);"));
365 s
.append(TString("vec4 texture3DProj(sampler3D sampler, vec4 coord);"));
367 s
.append(TString("vec4 textureCube(samplerCube sampler, vec3 coord);"));
369 s
.append(TString("vec4 shadow1D(sampler1DShadow sampler, vec3 coord);"));
371 s
.append(TString("vec4 shadow2D(sampler2DShadow sampler, vec3 coord);"));
373 s
.append(TString("vec4 shadow1DProj(sampler1DShadow sampler, vec4 coord);"));
375 s
.append(TString("vec4 shadow2DProj(sampler2DShadow sampler, vec4 coord);"));
381 s
.append(TString("float noise1(float x);"));
382 s
.append(TString("float noise1(vec2 x);"));
383 s
.append(TString("float noise1(vec3 x);"));
384 s
.append(TString("float noise1(vec4 x);"));
386 s
.append(TString("vec2 noise2(float x);"));
387 s
.append(TString("vec2 noise2(vec2 x);"));
388 s
.append(TString("vec2 noise2(vec3 x);"));
389 s
.append(TString("vec2 noise2(vec4 x);"));
391 s
.append(TString("vec3 noise3(float x);"));
392 s
.append(TString("vec3 noise3(vec2 x);"));
393 s
.append(TString("vec3 noise3(vec3 x);"));
394 s
.append(TString("vec3 noise3(vec4 x);"));
396 s
.append(TString("vec4 noise4(float x);"));
397 s
.append(TString("vec4 noise4(vec2 x);"));
398 s
.append(TString("vec4 noise4(vec3 x);"));
399 s
.append(TString("vec4 noise4(vec4 x);"));
401 s
.append(TString("\n"));
404 //============================================================================
406 // Prototypes for built-in functions seen by vertex shaders only.
408 //============================================================================
410 TString
& s
= BuiltInFunctionsVertex
;
413 // Geometric Functions.
415 s
.append(TString("vec4 ftransform();"));
418 // Texture Functions.
420 s
.append(TString("vec4 texture1DLod(sampler1D sampler, float coord, float lod);"));
421 s
.append(TString("vec4 texture1DProjLod(sampler1D sampler, vec2 coord, float lod);"));
422 s
.append(TString("vec4 texture1DProjLod(sampler1D sampler, vec4 coord, float lod);"));
424 s
.append(TString("vec4 texture2DLod(sampler2D sampler, vec2 coord, float lod);"));
425 s
.append(TString("vec4 texture2DProjLod(sampler2D sampler, vec3 coord, float lod);"));
426 s
.append(TString("vec4 texture2DProjLod(sampler2D sampler, vec4 coord, float lod);"));
428 s
.append(TString("vec4 texture3DLod(sampler3D sampler, vec3 coord, float lod);"));
429 s
.append(TString("vec4 texture3DProjLod(sampler3D sampler, vec4 coord, float lod);"));
430 s
.append(TString("vec4 textureCubeLod(samplerCube sampler, vec3 coord, float lod);"));
432 s
.append(TString("vec4 shadow1DLod(sampler1DShadow sampler, vec3 coord, float lod);"));
433 s
.append(TString("vec4 shadow2DLod(sampler2DShadow sampler, vec3 coord, float lod);"));
434 s
.append(TString("vec4 shadow1DProjLod(sampler1DShadow sampler, vec4 coord, float lod);"));
435 s
.append(TString("vec4 shadow2DProjLod(sampler2DShadow sampler, vec4 coord, float lod);"));
437 s
.append(TString("\n"));
440 //============================================================================
442 // Prototypes for built-in functions seen by fragment shaders only.
444 //============================================================================
446 TString
& s
= BuiltInFunctionsFragment
;
449 // Texture Functions.
451 s
.append(TString("vec4 texture1D(sampler1D sampler, float coord, float bias);"));
452 s
.append(TString("vec4 texture1DProj(sampler1D sampler, vec2 coord, float bias);"));
453 s
.append(TString("vec4 texture1DProj(sampler1D sampler, vec4 coord, float bias);"));
455 s
.append(TString("vec4 texture2D(sampler2D sampler, vec2 coord, float bias);"));
456 s
.append(TString("vec4 texture2DProj(sampler2D sampler, vec3 coord, float bias);"));
457 s
.append(TString("vec4 texture2DProj(sampler2D sampler, vec4 coord, float bias);"));
459 s
.append(TString("vec4 texture3D(sampler3D sampler, vec3 coord, float bias);"));
460 s
.append(TString("vec4 texture3DProj(sampler3D sampler, vec4 coord, float bias);"));
461 s
.append(TString("vec4 textureCube(samplerCube sampler, vec3 coord, float bias);"));
463 s
.append(TString("vec4 shadow1D(sampler1DShadow sampler, vec3 coord, float bias);"));
464 s
.append(TString("vec4 shadow2D(sampler2DShadow sampler, vec3 coord, float bias);"));
465 s
.append(TString("vec4 shadow1DProj(sampler1DShadow sampler, vec4 coord, float bias);"));
466 s
.append(TString("vec4 shadow2DProj(sampler2DShadow sampler, vec4 coord, float bias);"));
468 s
.append(TString("float dFdx(float p);"));
469 s
.append(TString("vec2 dFdx(vec2 p);"));
470 s
.append(TString("vec3 dFdx(vec3 p);"));
471 s
.append(TString("vec4 dFdx(vec4 p);"));
473 s
.append(TString("float dFdy(float p);"));
474 s
.append(TString("vec2 dFdy(vec2 p);"));
475 s
.append(TString("vec3 dFdy(vec3 p);"));
476 s
.append(TString("vec4 dFdy(vec4 p);"));
478 s
.append(TString("float fwidth(float p);"));
479 s
.append(TString("vec2 fwidth(vec2 p);"));
480 s
.append(TString("vec3 fwidth(vec3 p);"));
481 s
.append(TString("vec4 fwidth(vec4 p);"));
483 s
.append(TString("\n"));
486 //============================================================================
490 //============================================================================
492 TString
& s
= StandardUniforms
;
496 // OpenGL'uniform' state. Page numbers are in reference to version
497 // 1.4 of the OpenGL specification.
501 // Matrix state. p. 31, 32, 37, 39, 40.
503 s
.append(TString("uniform mat4 gl_ModelViewMatrix;"));
504 s
.append(TString("uniform mat4 gl_ProjectionMatrix;"));
505 s
.append(TString("uniform mat4 gl_ModelViewProjectionMatrix;"));
508 // Derived matrix state that provides inverse and transposed versions
509 // of the matrices above.
511 s
.append(TString("uniform mat3 gl_NormalMatrix;"));
513 s
.append(TString("uniform mat4 gl_ModelViewMatrixInverse;"));
514 s
.append(TString("uniform mat4 gl_ProjectionMatrixInverse;"));
515 s
.append(TString("uniform mat4 gl_ModelViewProjectionMatrixInverse;"));
517 s
.append(TString("uniform mat4 gl_ModelViewMatrixTranspose;"));
518 s
.append(TString("uniform mat4 gl_ProjectionMatrixTranspose;"));
519 s
.append(TString("uniform mat4 gl_ModelViewProjectionMatrixTranspose;"));
521 s
.append(TString("uniform mat4 gl_ModelViewMatrixInverseTranspose;"));
522 s
.append(TString("uniform mat4 gl_ProjectionMatrixInverseTranspose;"));
523 s
.append(TString("uniform mat4 gl_ModelViewProjectionMatrixInverseTranspose;"));
526 // Normal scaling p. 39.
528 s
.append(TString("uniform float gl_NormalScale;"));
531 // Depth range in window coordinates, p. 33
533 s
.append(TString("struct gl_DepthRangeParameters {"));
534 s
.append(TString(" float near;")); // n
535 s
.append(TString(" float far;")); // f
536 s
.append(TString(" float diff;")); // f - n
537 s
.append(TString("};"));
538 s
.append(TString("uniform gl_DepthRangeParameters gl_DepthRange;"));
542 // Point Size, p. 66, 67.
544 s
.append(TString("struct gl_PointParameters {"));
545 s
.append(TString(" float size;"));
546 s
.append(TString(" float sizeMin;"));
547 s
.append(TString(" float sizeMax;"));
548 s
.append(TString(" float fadeThresholdSize;"));
549 s
.append(TString(" float distanceConstantAttenuation;"));
550 s
.append(TString(" float distanceLinearAttenuation;"));
551 s
.append(TString(" float distanceQuadraticAttenuation;"));
552 s
.append(TString("};"));
554 s
.append(TString("uniform gl_PointParameters gl_Point;"));
557 // Material State p. 50, 55.
559 s
.append(TString("struct gl_MaterialParameters {"));
560 s
.append(TString(" vec4 emission;")); // Ecm
561 s
.append(TString(" vec4 ambient;")); // Acm
562 s
.append(TString(" vec4 diffuse;")); // Dcm
563 s
.append(TString(" vec4 specular;")); // Scm
564 s
.append(TString(" float shininess;")); // Srm
565 s
.append(TString("};"));
566 s
.append(TString("uniform gl_MaterialParameters gl_FrontMaterial;"));
567 s
.append(TString("uniform gl_MaterialParameters gl_BackMaterial;"));
570 // Light State p 50, 53, 55.
573 s
.append(TString("struct gl_LightSourceParameters {"));
574 s
.append(TString(" vec4 ambient;")); // Acli
575 s
.append(TString(" vec4 diffuse;")); // Dcli
576 s
.append(TString(" vec4 specular;")); // Scli
577 s
.append(TString(" vec4 position;")); // Ppli
578 s
.append(TString(" vec4 halfVector;")); // Derived: Hi
579 s
.append(TString(" vec3 spotDirection;")); // Sdli
580 s
.append(TString(" float spotExponent;")); // Srli
581 s
.append(TString(" float spotCutoff;")); // Crli
582 // (range: [0.0,90.0], 180.0)
583 s
.append(TString(" float spotCosCutoff;")); // Derived: cos(Crli)
584 // (range: [1.0,0.0],-1.0)
585 s
.append(TString(" float constantAttenuation;")); // K0
586 s
.append(TString(" float linearAttenuation;")); // K1
587 s
.append(TString(" float quadraticAttenuation;"));// K2
588 s
.append(TString("};"));
591 s
.append(TString("struct gl_LightModelParameters {"));
592 s
.append(TString(" vec4 ambient;")); // Acs
593 s
.append(TString("};"));
595 s
.append(TString("uniform gl_LightModelParameters gl_LightModel;"));
598 // Derived state from products of light and material.
601 s
.append(TString("struct gl_LightModelProducts {"));
602 s
.append(TString(" vec4 sceneColor;")); // Derived. Ecm + Acm * Acs
603 s
.append(TString("};"));
605 s
.append(TString("uniform gl_LightModelProducts gl_FrontLightModelProduct;"));
606 s
.append(TString("uniform gl_LightModelProducts gl_BackLightModelProduct;"));
608 s
.append(TString("struct gl_LightProducts {"));
609 s
.append(TString(" vec4 ambient;")); // Acm * Acli
610 s
.append(TString(" vec4 diffuse;")); // Dcm * Dcli
611 s
.append(TString(" vec4 specular;")); // Scm * Scli
612 s
.append(TString("};"));
620 s
.append(TString("struct gl_FogParameters {"));
621 s
.append(TString(" vec4 color;"));
622 s
.append(TString(" float density;"));
623 s
.append(TString(" float start;"));
624 s
.append(TString(" float end;"));
625 s
.append(TString(" float scale;")); // 1 / (gl_FogEnd - gl_FogStart)
626 s
.append(TString("};"));
628 s
.append(TString("uniform gl_FogParameters gl_Fog;"));
630 s
.append(TString("\n"));
633 //============================================================================
635 // Vertex attributes, p. 19.
637 //============================================================================
639 TString
& s
= StandardVertexAttributes
;
641 s
.append(TString("attribute vec4 gl_Color;"));
642 s
.append(TString("attribute vec4 gl_SecondaryColor;"));
643 s
.append(TString("attribute vec3 gl_Normal;"));
644 s
.append(TString("attribute vec4 gl_Vertex;"));
645 s
.append(TString("attribute vec4 gl_MultiTexCoord0;"));
646 s
.append(TString("attribute vec4 gl_MultiTexCoord1;"));
647 s
.append(TString("attribute vec4 gl_MultiTexCoord2;"));
648 s
.append(TString("attribute vec4 gl_MultiTexCoord3;"));
649 s
.append(TString("attribute vec4 gl_MultiTexCoord4;"));
650 s
.append(TString("attribute vec4 gl_MultiTexCoord5;"));
651 s
.append(TString("attribute vec4 gl_MultiTexCoord6;"));
652 s
.append(TString("attribute vec4 gl_MultiTexCoord7;"));
653 s
.append(TString("attribute float gl_FogCoord;"));
655 s
.append(TString("\n"));
658 //============================================================================
660 // Define the output varying interface from the vertex shader.
662 //============================================================================
664 TString
& s
= StandardVertexVaryings
;
666 s
.append(TString("varying vec4 gl_FrontColor;"));
667 s
.append(TString("varying vec4 gl_BackColor;"));
668 s
.append(TString("varying vec4 gl_FrontSecondaryColor;"));
669 s
.append(TString("varying vec4 gl_BackSecondaryColor;"));
670 s
.append(TString("varying vec4 gl_TexCoord[];"));
671 s
.append(TString("varying float gl_FogFragCoord;"));
673 s
.append(TString("\n"));
676 //============================================================================
678 // Define the input varying interface to the fragment shader.
680 //============================================================================
682 TString
& s
= StandardFragmentVaryings
;
684 s
.append(TString("varying vec4 gl_Color;"));
685 s
.append(TString("varying vec4 gl_SecondaryColor;"));
686 s
.append(TString("varying vec4 gl_TexCoord[];"));
687 s
.append(TString("varying float gl_FogFragCoord;"));
689 s
.append(TString("\n"));
692 builtInStrings
[EShLangFragment
].push_back(BuiltInFunctions
.c_str());
693 builtInStrings
[EShLangFragment
].push_back(BuiltInFunctionsFragment
);
694 builtInStrings
[EShLangFragment
].push_back(StandardUniforms
);
695 builtInStrings
[EShLangFragment
].push_back(StandardFragmentVaryings
);
697 builtInStrings
[EShLangVertex
].push_back(BuiltInFunctions
);
698 builtInStrings
[EShLangVertex
].push_back(BuiltInFunctionsVertex
);
699 builtInStrings
[EShLangVertex
].push_back(StandardVertexVaryings
);
700 builtInStrings
[EShLangVertex
].push_back(StandardVertexAttributes
);
701 builtInStrings
[EShLangVertex
].push_back(StandardUniforms
);
705 void TBuiltIns::initialize(const TBuiltInResource
&resources
)
708 // Initialize all the built-in strings for parsing.
710 TString StandardUniforms
;
713 //============================================================================
717 //============================================================================
719 TString
& s
= StandardUniforms
;
722 // Implementation dependent constants. The example values below
723 // are the minimum values allowed for these maximums.
725 char builtInConstant
[80];
726 sprintf(builtInConstant
, "const int gl_MaxLights = %d;", resources
.maxLights
); // GL 1.0
727 s
.append(TString(builtInConstant
));
729 sprintf(builtInConstant
, "const int gl_MaxClipPlanes = %d;", resources
.maxClipPlanes
); // GL 1.0
730 s
.append(TString(builtInConstant
));
732 sprintf(builtInConstant
, "const int gl_MaxTextureUnits = %d;", resources
.maxTextureUnits
); // GL 1.2
733 s
.append(TString(builtInConstant
));
735 sprintf(builtInConstant
, "const int gl_MaxTextureCoords = %d;", resources
.maxTextureCoords
); // ARB_fragment_program
736 s
.append(TString(builtInConstant
));
738 sprintf(builtInConstant
, "const int gl_MaxVertexAttribs = %d;", resources
.maxVertexAttribs
); // ARB_vertex_shader
739 s
.append(TString(builtInConstant
));
741 sprintf(builtInConstant
, "const int gl_MaxVertexUniformComponents = %d;", resources
.maxVertexUniformComponents
); // ARB_vertex_shader
742 s
.append(TString(builtInConstant
));
744 sprintf(builtInConstant
, "const int gl_MaxVaryingFloats = %d;", resources
.maxVaryingFloats
); // ARB_vertex_shader
745 s
.append(TString(builtInConstant
));
747 sprintf(builtInConstant
, "const int gl_MaxVertexTextureImageUnits = %d;", resources
.maxVertexTextureImageUnits
); // ARB_vertex_shader
748 s
.append(TString(builtInConstant
));
750 sprintf(builtInConstant
, "const int gl_MaxCombinedTextureImageUnits = %d;", resources
.maxCombinedTextureImageUnits
); // ARB_vertex_shader
751 s
.append(TString(builtInConstant
));
753 sprintf(builtInConstant
, "const int gl_MaxTextureImageUnits = %d;", resources
.maxTextureImageUnits
); // ARB_fragment_shader
754 s
.append(TString(builtInConstant
));
756 sprintf(builtInConstant
, "const int gl_MaxFragmentUniformComponents = %d;", resources
.maxFragmentUniformComponents
); // ARB_fragment_shader
757 s
.append(TString(builtInConstant
));
759 sprintf(builtInConstant
, "const int gl_MaxDrawBuffers = %d;", resources
.maxDrawBuffers
); // proposed ARB_draw_buffers
760 s
.append(TString(builtInConstant
));
763 // OpenGL'uniform' state. Page numbers are in reference to version
764 // 1.4 of the OpenGL specification.
768 // Matrix state. p. 31, 32, 37, 39, 40.
770 s
.append(TString("uniform mat4 gl_TextureMatrix[gl_MaxTextureCoords];"));
773 // Derived matrix state that provides inverse and transposed versions
774 // of the matrices above.
776 s
.append(TString("uniform mat4 gl_TextureMatrixInverse[gl_MaxTextureCoords];"));
778 s
.append(TString("uniform mat4 gl_TextureMatrixTranspose[gl_MaxTextureCoords];"));
780 s
.append(TString("uniform mat4 gl_TextureMatrixInverseTranspose[gl_MaxTextureCoords];"));
783 // Clip planes p. 42.
785 s
.append(TString("uniform vec4 gl_ClipPlane[gl_MaxClipPlanes];"));
788 // Light State p 50, 53, 55.
790 s
.append(TString("uniform gl_LightSourceParameters gl_LightSource[gl_MaxLights];"));
793 // Derived state from products of light.
795 s
.append(TString("uniform gl_LightProducts gl_FrontLightProduct[gl_MaxLights];"));
796 s
.append(TString("uniform gl_LightProducts gl_BackLightProduct[gl_MaxLights];"));
799 // Textureg Environment and Generation, p. 152, p. 40-42.
801 s
.append(TString("uniform vec4 gl_TextureEnvColor[gl_MaxTextureImageUnits];"));
802 s
.append(TString("uniform vec4 gl_EyePlaneS[gl_MaxTextureCoords];"));
803 s
.append(TString("uniform vec4 gl_EyePlaneT[gl_MaxTextureCoords];"));
804 s
.append(TString("uniform vec4 gl_EyePlaneR[gl_MaxTextureCoords];"));
805 s
.append(TString("uniform vec4 gl_EyePlaneQ[gl_MaxTextureCoords];"));
806 s
.append(TString("uniform vec4 gl_ObjectPlaneS[gl_MaxTextureCoords];"));
807 s
.append(TString("uniform vec4 gl_ObjectPlaneT[gl_MaxTextureCoords];"));
808 s
.append(TString("uniform vec4 gl_ObjectPlaneR[gl_MaxTextureCoords];"));
809 s
.append(TString("uniform vec4 gl_ObjectPlaneQ[gl_MaxTextureCoords];"));
811 s
.append(TString("\n"));
814 builtInStrings
[EShLangFragment
].push_back(StandardUniforms
);
815 builtInStrings
[EShLangVertex
].push_back(StandardUniforms
);
818 void IdentifyBuiltIns(EShLanguage language
, TSymbolTable
& symbolTable
)
821 // First, insert some special built-in variables that are not in
822 // the built-in header files.
826 case EShLangFragment
: {
827 symbolTable
.insert(*new TVariable(NewPoolTString("gl_FrontFacing"), TType(EbtBool
, EvqFace
, 1)));
828 symbolTable
.insert(*new TVariable(NewPoolTString("gl_FragCoord"), TType(EbtFloat
, EvqFragCoord
, 4)));
829 symbolTable
.insert(*new TVariable(NewPoolTString("gl_FragColor"), TType(EbtFloat
, EvqFragColor
, 4)));
830 symbolTable
.insert(*new TVariable(NewPoolTString("gl_FragDepth"), TType(EbtFloat
, EvqFragDepth
, 1)));
836 symbolTable
.insert(*new TVariable(NewPoolTString("gl_Position"), TType(EbtFloat
, EvqPosition
, 4)));
837 symbolTable
.insert(*new TVariable(NewPoolTString("gl_PointSize"), TType(EbtFloat
, EvqPointSize
, 1)));
838 symbolTable
.insert(*new TVariable(NewPoolTString("gl_ClipVertex"), TType(EbtFloat
, EvqClipVertex
, 4)));
844 // Next, identify which built-ins from the already loaded headers have
845 // a mapping to an operator. Those that are not identified as such are
846 // expected to be resolved through a library of functions, versus as
849 symbolTable
.relateToOperator("not", EOpVectorLogicalNot
);
851 symbolTable
.relateToOperator("matrixCompMult", EOpMul
);
852 symbolTable
.relateToOperator("mod", EOpMod
);
854 symbolTable
.relateToOperator("equal", EOpVectorEqual
);
855 symbolTable
.relateToOperator("notEqual", EOpVectorNotEqual
);
856 symbolTable
.relateToOperator("lessThan", EOpLessThan
);
857 symbolTable
.relateToOperator("greaterThan", EOpGreaterThan
);
858 symbolTable
.relateToOperator("lessThanEqual", EOpLessThanEqual
);
859 symbolTable
.relateToOperator("greaterThanEqual", EOpGreaterThanEqual
);
861 symbolTable
.relateToOperator("radians", EOpRadians
);
862 symbolTable
.relateToOperator("degrees", EOpDegrees
);
863 symbolTable
.relateToOperator("sin", EOpSin
);
864 symbolTable
.relateToOperator("cos", EOpCos
);
865 symbolTable
.relateToOperator("tan", EOpTan
);
866 symbolTable
.relateToOperator("asin", EOpAsin
);
867 symbolTable
.relateToOperator("acos", EOpAcos
);
868 symbolTable
.relateToOperator("atan", EOpAtan
);
870 symbolTable
.relateToOperator("pow", EOpPow
);
871 symbolTable
.relateToOperator("exp2", EOpExp2
);
872 symbolTable
.relateToOperator("log", EOpLog
);
873 symbolTable
.relateToOperator("exp", EOpExp
);
874 symbolTable
.relateToOperator("log2", EOpLog2
);
875 symbolTable
.relateToOperator("sqrt", EOpSqrt
);
876 symbolTable
.relateToOperator("inversesqrt", EOpInverseSqrt
);
878 symbolTable
.relateToOperator("abs", EOpAbs
);
879 symbolTable
.relateToOperator("sign", EOpSign
);
880 symbolTable
.relateToOperator("floor", EOpFloor
);
881 symbolTable
.relateToOperator("ceil", EOpCeil
);
882 symbolTable
.relateToOperator("fract", EOpFract
);
883 symbolTable
.relateToOperator("min", EOpMin
);
884 symbolTable
.relateToOperator("max", EOpMax
);
885 symbolTable
.relateToOperator("clamp", EOpClamp
);
886 symbolTable
.relateToOperator("mix", EOpMix
);
887 symbolTable
.relateToOperator("step", EOpStep
);
888 symbolTable
.relateToOperator("smoothstep", EOpSmoothStep
);
890 symbolTable
.relateToOperator("length", EOpLength
);
891 symbolTable
.relateToOperator("distance", EOpDistance
);
892 symbolTable
.relateToOperator("dot", EOpDot
);
893 symbolTable
.relateToOperator("cross", EOpCross
);
894 symbolTable
.relateToOperator("normalize", EOpNormalize
);
895 symbolTable
.relateToOperator("forward", EOpFaceForward
);
896 symbolTable
.relateToOperator("reflect", EOpReflect
);
897 symbolTable
.relateToOperator("refract", EOpRefract
);
899 symbolTable
.relateToOperator("any", EOpAny
);
900 symbolTable
.relateToOperator("all", EOpAll
);
907 case EShLangFragment
:
908 symbolTable
.relateToOperator("dFdx", EOpDPdx
);
909 symbolTable
.relateToOperator("dFdy", EOpDPdy
);
910 symbolTable
.relateToOperator("fwidth", EOpFwidth
);
916 symbolTable
.relateToOperator("itof", EOpItof
);
917 symbolTable
.relateToOperator("ftoi", EOpFtoi
);
918 symbolTable
.relateToOperator("skipPixels", EOpSkipPixels
);
919 symbolTable
.relateToOperator("readInput", EOpReadInput
);
920 symbolTable
.relateToOperator("writePixel", EOpWritePixel
);
921 symbolTable
.relateToOperator("bitmapLSB", EOpBitmapLsb
);
922 symbolTable
.relateToOperator("bitmapMSB", EOpBitmapMsb
);
923 symbolTable
.relateToOperator("writeOutput", EOpWriteOutput
);
924 symbolTable
.relateToOperator("readPixel", EOpReadPixel
);
926 default: assert (false && "Language not supported");
930 void IdentifyBuiltIns(EShLanguage language
, TSymbolTable
& symbolTable
, const TBuiltInResource
&resources
)
933 // First, insert some special built-in variables that are not in
934 // the built-in header files.
938 case EShLangFragment
: {
939 // Set up gl_FragData. The array size.
940 TType
fragData(EbtFloat
, EvqFragColor
, 4, false, true);
941 fragData
.setArraySize(resources
.maxDrawBuffers
);
942 symbolTable
.insert(*new TVariable(NewPoolTString("gl_FragData"), fragData
));