1 //
2 // Copyright (c) 2002-2014 The ANGLE Project Authors. All rights reserved.
3 // Use of this source code is governed by a BSD-style license that can be
4 // found in the LICENSE file.
5 //
6 
7 //
8 // Create symbols that declare built-in definitions, add built-ins that
9 // cannot be expressed in the files, and establish mappings between
10 // built-in functions and operators.
11 //
12 
13 #include "compiler/translator/Initialize.h"
14 #include "compiler/translator/Cache.h"
15 
16 #include "compiler/translator/IntermNode.h"
17 #include "angle_gl.h"
18 
19 namespace sh
20 {
21 
InsertBuiltInFunctions(sh::GLenum type,ShShaderSpec spec,const ShBuiltInResources & resources,TSymbolTable & symbolTable)22 void InsertBuiltInFunctions(sh::GLenum type,
23                             ShShaderSpec spec,
24                             const ShBuiltInResources &resources,
25                             TSymbolTable &symbolTable)
26 {
27     const TType *voidType = TCache::getType(EbtVoid);
28     const TType *float1   = TCache::getType(EbtFloat);
29     const TType *float2   = TCache::getType(EbtFloat, 2);
30     const TType *float3   = TCache::getType(EbtFloat, 3);
31     const TType *float4   = TCache::getType(EbtFloat, 4);
32     const TType *int1     = TCache::getType(EbtInt);
33     const TType *int2     = TCache::getType(EbtInt, 2);
34     const TType *int3     = TCache::getType(EbtInt, 3);
35     const TType *uint1    = TCache::getType(EbtUInt);
36     const TType *bool1    = TCache::getType(EbtBool);
37     const TType *genType  = TCache::getType(EbtGenType);
38     const TType *genIType = TCache::getType(EbtGenIType);
39     const TType *genUType = TCache::getType(EbtGenUType);
40     const TType *genBType = TCache::getType(EbtGenBType);
41 
42     //
43     // Angle and Trigonometric Functions.
44     //
45     symbolTable.insertBuiltInOp(COMMON_BUILTINS, EOpRadians, genType, genType);
46     symbolTable.insertBuiltInOp(COMMON_BUILTINS, EOpDegrees, genType, genType);
47     symbolTable.insertBuiltInOp(COMMON_BUILTINS, EOpSin, genType, genType);
48     symbolTable.insertBuiltInOp(COMMON_BUILTINS, EOpCos, genType, genType);
49     symbolTable.insertBuiltInOp(COMMON_BUILTINS, EOpTan, genType, genType);
50     symbolTable.insertBuiltInOp(COMMON_BUILTINS, EOpAsin, genType, genType);
51     symbolTable.insertBuiltInOp(COMMON_BUILTINS, EOpAcos, genType, genType);
52     symbolTable.insertBuiltInOp(COMMON_BUILTINS, EOpAtan, genType, genType, genType);
53     symbolTable.insertBuiltInOp(COMMON_BUILTINS, EOpAtan, genType, genType);
54     symbolTable.insertBuiltInOp(ESSL3_BUILTINS, EOpSinh, genType, genType);
55     symbolTable.insertBuiltInOp(ESSL3_BUILTINS, EOpCosh, genType, genType);
56     symbolTable.insertBuiltInOp(ESSL3_BUILTINS, EOpTanh, genType, genType);
57     symbolTable.insertBuiltInOp(ESSL3_BUILTINS, EOpAsinh, genType, genType);
58     symbolTable.insertBuiltInOp(ESSL3_BUILTINS, EOpAcosh, genType, genType);
59     symbolTable.insertBuiltInOp(ESSL3_BUILTINS, EOpAtanh, genType, genType);
60 
61     //
62     // Exponential Functions.
63     //
64     symbolTable.insertBuiltInOp(COMMON_BUILTINS, EOpPow, genType, genType, genType);
65     symbolTable.insertBuiltInOp(COMMON_BUILTINS, EOpExp, genType, genType);
66     symbolTable.insertBuiltInOp(COMMON_BUILTINS, EOpLog, genType, genType);
67     symbolTable.insertBuiltInOp(COMMON_BUILTINS, EOpExp2, genType, genType);
68     symbolTable.insertBuiltInOp(COMMON_BUILTINS, EOpLog2, genType, genType);
69     symbolTable.insertBuiltInOp(COMMON_BUILTINS, EOpSqrt, genType, genType);
70     symbolTable.insertBuiltInOp(COMMON_BUILTINS, EOpInverseSqrt, genType, genType);
71 
72     //
73     // Common Functions.
74     //
75     symbolTable.insertBuiltInOp(COMMON_BUILTINS, EOpAbs, genType, genType);
76     symbolTable.insertBuiltInOp(ESSL3_BUILTINS, EOpAbs, genIType, genIType);
77     symbolTable.insertBuiltInOp(COMMON_BUILTINS, EOpSign, genType, genType);
78     symbolTable.insertBuiltInOp(ESSL3_BUILTINS, EOpSign, genIType, genIType);
79     symbolTable.insertBuiltInOp(COMMON_BUILTINS, EOpFloor, genType, genType);
80     symbolTable.insertBuiltInOp(ESSL3_BUILTINS, EOpTrunc, genType, genType);
81     symbolTable.insertBuiltInOp(ESSL3_BUILTINS, EOpRound, genType, genType);
82     symbolTable.insertBuiltInOp(ESSL3_BUILTINS, EOpRoundEven, genType, genType);
83     symbolTable.insertBuiltInOp(COMMON_BUILTINS, EOpCeil, genType, genType);
84     symbolTable.insertBuiltInOp(COMMON_BUILTINS, EOpFract, genType, genType);
85     symbolTable.insertBuiltInOp(COMMON_BUILTINS, EOpMod, genType, genType, float1);
86     symbolTable.insertBuiltInOp(COMMON_BUILTINS, EOpMod, genType, genType, genType);
87     symbolTable.insertBuiltInOp(COMMON_BUILTINS, EOpMin, genType, genType, float1);
88     symbolTable.insertBuiltInOp(COMMON_BUILTINS, EOpMin, genType, genType, genType);
89     symbolTable.insertBuiltInOp(ESSL3_BUILTINS, EOpMin, genIType, genIType, genIType);
90     symbolTable.insertBuiltInOp(ESSL3_BUILTINS, EOpMin, genIType, genIType, int1);
91     symbolTable.insertBuiltInOp(ESSL3_BUILTINS, EOpMin, genUType, genUType, genUType);
92     symbolTable.insertBuiltInOp(ESSL3_BUILTINS, EOpMin, genUType, genUType, uint1);
93     symbolTable.insertBuiltInOp(COMMON_BUILTINS, EOpMax, genType, genType, float1);
94     symbolTable.insertBuiltInOp(COMMON_BUILTINS, EOpMax, genType, genType, genType);
95     symbolTable.insertBuiltInOp(ESSL3_BUILTINS, EOpMax, genIType, genIType, genIType);
96     symbolTable.insertBuiltInOp(ESSL3_BUILTINS, EOpMax, genIType, genIType, int1);
97     symbolTable.insertBuiltInOp(ESSL3_BUILTINS, EOpMax, genUType, genUType, genUType);
98     symbolTable.insertBuiltInOp(ESSL3_BUILTINS, EOpMax, genUType, genUType, uint1);
99     symbolTable.insertBuiltInOp(COMMON_BUILTINS, EOpClamp, genType, genType, float1, float1);
100     symbolTable.insertBuiltInOp(COMMON_BUILTINS, EOpClamp, genType, genType, genType, genType);
101     symbolTable.insertBuiltInOp(ESSL3_BUILTINS, EOpClamp, genIType, genIType, int1, int1);
102     symbolTable.insertBuiltInOp(ESSL3_BUILTINS, EOpClamp, genIType, genIType, genIType, genIType);
103     symbolTable.insertBuiltInOp(ESSL3_BUILTINS, EOpClamp, genUType, genUType, uint1, uint1);
104     symbolTable.insertBuiltInOp(ESSL3_BUILTINS, EOpClamp, genUType, genUType, genUType, genUType);
105     symbolTable.insertBuiltInOp(COMMON_BUILTINS, EOpMix, genType, genType, genType, float1);
106     symbolTable.insertBuiltInOp(COMMON_BUILTINS, EOpMix, genType, genType, genType, genType);
107     symbolTable.insertBuiltInOp(ESSL3_BUILTINS, EOpMix, genType, genType, genType, genBType);
108     symbolTable.insertBuiltInOp(COMMON_BUILTINS, EOpStep, genType, genType, genType);
109     symbolTable.insertBuiltInOp(COMMON_BUILTINS, EOpStep, genType, float1, genType);
110     symbolTable.insertBuiltInOp(COMMON_BUILTINS, EOpSmoothStep, genType, genType, genType, genType);
111     symbolTable.insertBuiltInOp(COMMON_BUILTINS, EOpSmoothStep, genType, float1, float1, genType);
112 
113     const TType *outGenType = TCache::getType(EbtGenType, EvqOut);
114     const TType *outGenIType = TCache::getType(EbtGenIType, EvqOut);
115 
116     symbolTable.insertBuiltInOp(ESSL3_BUILTINS, EOpModf, genType, genType, outGenType);
117 
118     symbolTable.insertBuiltInOp(ESSL3_BUILTINS, EOpIsNan, genBType, genType);
119     symbolTable.insertBuiltInOp(ESSL3_BUILTINS, EOpIsInf, genBType, genType);
120     symbolTable.insertBuiltInOp(ESSL3_BUILTINS, EOpFloatBitsToInt, genIType, genType);
121     symbolTable.insertBuiltInOp(ESSL3_BUILTINS, EOpFloatBitsToUint, genUType, genType);
122     symbolTable.insertBuiltInOp(ESSL3_BUILTINS, EOpIntBitsToFloat, genType, genIType);
123     symbolTable.insertBuiltInOp(ESSL3_BUILTINS, EOpUintBitsToFloat, genType, genUType);
124 
125     symbolTable.insertBuiltInOp(ESSL3_1_BUILTINS, EOpFrexp, genType, genType, outGenIType);
126     symbolTable.insertBuiltInOp(ESSL3_1_BUILTINS, EOpLdexp, genType, genType, genIType);
127 
128     symbolTable.insertBuiltInOp(ESSL3_BUILTINS, EOpPackSnorm2x16, uint1, float2);
129     symbolTable.insertBuiltInOp(ESSL3_BUILTINS, EOpPackUnorm2x16, uint1, float2);
130     symbolTable.insertBuiltInOp(ESSL3_BUILTINS, EOpPackHalf2x16, uint1, float2);
131     symbolTable.insertBuiltInOp(ESSL3_BUILTINS, EOpUnpackSnorm2x16, float2, uint1);
132     symbolTable.insertBuiltInOp(ESSL3_BUILTINS, EOpUnpackUnorm2x16, float2, uint1);
133     symbolTable.insertBuiltInOp(ESSL3_BUILTINS, EOpUnpackHalf2x16, float2, uint1);
134 
135     symbolTable.insertBuiltInOp(ESSL3_1_BUILTINS, EOpPackUnorm4x8, uint1, float4);
136     symbolTable.insertBuiltInOp(ESSL3_1_BUILTINS, EOpPackSnorm4x8, uint1, float4);
137     symbolTable.insertBuiltInOp(ESSL3_1_BUILTINS, EOpUnpackUnorm4x8, float4, uint1);
138     symbolTable.insertBuiltInOp(ESSL3_1_BUILTINS, EOpUnpackSnorm4x8, float4, uint1);
139 
140     //
141     // Geometric Functions.
142     //
143     symbolTable.insertBuiltInOp(COMMON_BUILTINS, EOpLength, float1, genType);
144     symbolTable.insertBuiltInOp(COMMON_BUILTINS, EOpDistance, float1, genType, genType);
145     symbolTable.insertBuiltInOp(COMMON_BUILTINS, EOpDot, float1, genType, genType);
146     symbolTable.insertBuiltInOp(COMMON_BUILTINS, EOpCross, float3, float3, float3);
147     symbolTable.insertBuiltInOp(COMMON_BUILTINS, EOpNormalize, genType, genType);
148     symbolTable.insertBuiltInOp(COMMON_BUILTINS, EOpFaceforward, genType, genType, genType,
149                                 genType);
150     symbolTable.insertBuiltInOp(COMMON_BUILTINS, EOpReflect, genType, genType, genType);
151     symbolTable.insertBuiltInOp(COMMON_BUILTINS, EOpRefract, genType, genType, genType, float1);
152 
153     const TType *mat2   = TCache::getType(EbtFloat, 2, 2);
154     const TType *mat3   = TCache::getType(EbtFloat, 3, 3);
155     const TType *mat4   = TCache::getType(EbtFloat, 4, 4);
156     const TType *mat2x3 = TCache::getType(EbtFloat, 2, 3);
157     const TType *mat3x2 = TCache::getType(EbtFloat, 3, 2);
158     const TType *mat2x4 = TCache::getType(EbtFloat, 2, 4);
159     const TType *mat4x2 = TCache::getType(EbtFloat, 4, 2);
160     const TType *mat3x4 = TCache::getType(EbtFloat, 3, 4);
161     const TType *mat4x3 = TCache::getType(EbtFloat, 4, 3);
162 
163     //
164     // Matrix Functions.
165     //
166     symbolTable.insertBuiltInOp(COMMON_BUILTINS, EOpMulMatrixComponentWise, mat2, mat2, mat2);
167     symbolTable.insertBuiltInOp(COMMON_BUILTINS, EOpMulMatrixComponentWise, mat3, mat3, mat3);
168     symbolTable.insertBuiltInOp(COMMON_BUILTINS, EOpMulMatrixComponentWise, mat4, mat4, mat4);
169     symbolTable.insertBuiltInOp(ESSL3_BUILTINS, EOpMulMatrixComponentWise, mat2x3, mat2x3, mat2x3);
170     symbolTable.insertBuiltInOp(ESSL3_BUILTINS, EOpMulMatrixComponentWise, mat3x2, mat3x2, mat3x2);
171     symbolTable.insertBuiltInOp(ESSL3_BUILTINS, EOpMulMatrixComponentWise, mat2x4, mat2x4, mat2x4);
172     symbolTable.insertBuiltInOp(ESSL3_BUILTINS, EOpMulMatrixComponentWise, mat4x2, mat4x2, mat4x2);
173     symbolTable.insertBuiltInOp(ESSL3_BUILTINS, EOpMulMatrixComponentWise, mat3x4, mat3x4, mat3x4);
174     symbolTable.insertBuiltInOp(ESSL3_BUILTINS, EOpMulMatrixComponentWise, mat4x3, mat4x3, mat4x3);
175 
176     symbolTable.insertBuiltInOp(ESSL3_BUILTINS, EOpOuterProduct, mat2, float2, float2);
177     symbolTable.insertBuiltInOp(ESSL3_BUILTINS, EOpOuterProduct, mat3, float3, float3);
178     symbolTable.insertBuiltInOp(ESSL3_BUILTINS, EOpOuterProduct, mat4, float4, float4);
179     symbolTable.insertBuiltInOp(ESSL3_BUILTINS, EOpOuterProduct, mat2x3, float3, float2);
180     symbolTable.insertBuiltInOp(ESSL3_BUILTINS, EOpOuterProduct, mat3x2, float2, float3);
181     symbolTable.insertBuiltInOp(ESSL3_BUILTINS, EOpOuterProduct, mat2x4, float4, float2);
182     symbolTable.insertBuiltInOp(ESSL3_BUILTINS, EOpOuterProduct, mat4x2, float2, float4);
183     symbolTable.insertBuiltInOp(ESSL3_BUILTINS, EOpOuterProduct, mat3x4, float4, float3);
184     symbolTable.insertBuiltInOp(ESSL3_BUILTINS, EOpOuterProduct, mat4x3, float3, float4);
185 
186     symbolTable.insertBuiltInOp(ESSL3_BUILTINS, EOpTranspose, mat2, mat2);
187     symbolTable.insertBuiltInOp(ESSL3_BUILTINS, EOpTranspose, mat3, mat3);
188     symbolTable.insertBuiltInOp(ESSL3_BUILTINS, EOpTranspose, mat4, mat4);
189     symbolTable.insertBuiltInOp(ESSL3_BUILTINS, EOpTranspose, mat2x3, mat3x2);
190     symbolTable.insertBuiltInOp(ESSL3_BUILTINS, EOpTranspose, mat3x2, mat2x3);
191     symbolTable.insertBuiltInOp(ESSL3_BUILTINS, EOpTranspose, mat2x4, mat4x2);
192     symbolTable.insertBuiltInOp(ESSL3_BUILTINS, EOpTranspose, mat4x2, mat2x4);
193     symbolTable.insertBuiltInOp(ESSL3_BUILTINS, EOpTranspose, mat3x4, mat4x3);
194     symbolTable.insertBuiltInOp(ESSL3_BUILTINS, EOpTranspose, mat4x3, mat3x4);
195 
196     symbolTable.insertBuiltInOp(ESSL3_BUILTINS, EOpDeterminant, float1, mat2);
197     symbolTable.insertBuiltInOp(ESSL3_BUILTINS, EOpDeterminant, float1, mat3);
198     symbolTable.insertBuiltInOp(ESSL3_BUILTINS, EOpDeterminant, float1, mat4);
199 
200     symbolTable.insertBuiltInOp(ESSL3_BUILTINS, EOpInverse, mat2, mat2);
201     symbolTable.insertBuiltInOp(ESSL3_BUILTINS, EOpInverse, mat3, mat3);
202     symbolTable.insertBuiltInOp(ESSL3_BUILTINS, EOpInverse, mat4, mat4);
203 
204     const TType *vec  = TCache::getType(EbtVec);
205     const TType *ivec = TCache::getType(EbtIVec);
206     const TType *uvec = TCache::getType(EbtUVec);
207     const TType *bvec = TCache::getType(EbtBVec);
208 
209     //
210     // Vector relational functions.
211     //
212     symbolTable.insertBuiltInOp(COMMON_BUILTINS, EOpLessThanComponentWise, bvec, vec, vec);
213     symbolTable.insertBuiltInOp(COMMON_BUILTINS, EOpLessThanComponentWise, bvec, ivec, ivec);
214     symbolTable.insertBuiltInOp(ESSL3_BUILTINS, EOpLessThanComponentWise, bvec, uvec, uvec);
215     symbolTable.insertBuiltInOp(COMMON_BUILTINS, EOpLessThanEqualComponentWise, bvec, vec, vec);
216     symbolTable.insertBuiltInOp(COMMON_BUILTINS, EOpLessThanEqualComponentWise, bvec, ivec, ivec);
217     symbolTable.insertBuiltInOp(ESSL3_BUILTINS, EOpLessThanEqualComponentWise, bvec, uvec, uvec);
218     symbolTable.insertBuiltInOp(COMMON_BUILTINS, EOpGreaterThanComponentWise, bvec, vec, vec);
219     symbolTable.insertBuiltInOp(COMMON_BUILTINS, EOpGreaterThanComponentWise, bvec, ivec, ivec);
220     symbolTable.insertBuiltInOp(ESSL3_BUILTINS, EOpGreaterThanComponentWise, bvec, uvec, uvec);
221     symbolTable.insertBuiltInOp(COMMON_BUILTINS, EOpGreaterThanEqualComponentWise, bvec, vec, vec);
222     symbolTable.insertBuiltInOp(COMMON_BUILTINS, EOpGreaterThanEqualComponentWise, bvec, ivec,
223                                 ivec);
224     symbolTable.insertBuiltInOp(ESSL3_BUILTINS, EOpGreaterThanEqualComponentWise, bvec, uvec, uvec);
225     symbolTable.insertBuiltInOp(COMMON_BUILTINS, EOpEqualComponentWise, bvec, vec, vec);
226     symbolTable.insertBuiltInOp(COMMON_BUILTINS, EOpEqualComponentWise, bvec, ivec, ivec);
227     symbolTable.insertBuiltInOp(ESSL3_BUILTINS, EOpEqualComponentWise, bvec, uvec, uvec);
228     symbolTable.insertBuiltInOp(COMMON_BUILTINS, EOpEqualComponentWise, bvec, bvec, bvec);
229     symbolTable.insertBuiltInOp(COMMON_BUILTINS, EOpNotEqualComponentWise, bvec, vec, vec);
230     symbolTable.insertBuiltInOp(COMMON_BUILTINS, EOpNotEqualComponentWise, bvec, ivec, ivec);
231     symbolTable.insertBuiltInOp(ESSL3_BUILTINS, EOpNotEqualComponentWise, bvec, uvec, uvec);
232     symbolTable.insertBuiltInOp(COMMON_BUILTINS, EOpNotEqualComponentWise, bvec, bvec, bvec);
233     symbolTable.insertBuiltInOp(COMMON_BUILTINS, EOpAny, bool1, bvec);
234     symbolTable.insertBuiltInOp(COMMON_BUILTINS, EOpAll, bool1, bvec);
235     symbolTable.insertBuiltInOp(COMMON_BUILTINS, EOpLogicalNotComponentWise, bvec, bvec);
236 
237     //
238     // Integer functions
239     //
240     const TType *outGenUType = TCache::getType(EbtGenUType, EvqOut);
241 
242     symbolTable.insertBuiltInOp(ESSL3_1_BUILTINS, EOpBitfieldExtract, genIType, genIType, int1,
243                                 int1);
244     symbolTable.insertBuiltInOp(ESSL3_1_BUILTINS, EOpBitfieldExtract, genUType, genUType, int1,
245                                 int1);
246     symbolTable.insertBuiltInOp(ESSL3_1_BUILTINS, EOpBitfieldInsert, genIType, genIType, genIType,
247                                 int1, int1);
248     symbolTable.insertBuiltInOp(ESSL3_1_BUILTINS, EOpBitfieldInsert, genUType, genUType, genUType,
249                                 int1, int1);
250     symbolTable.insertBuiltInOp(ESSL3_1_BUILTINS, EOpBitfieldReverse, genIType, genIType);
251     symbolTable.insertBuiltInOp(ESSL3_1_BUILTINS, EOpBitfieldReverse, genUType, genUType);
252     symbolTable.insertBuiltInOp(ESSL3_1_BUILTINS, EOpBitCount, genIType, genIType);
253     symbolTable.insertBuiltInOp(ESSL3_1_BUILTINS, EOpBitCount, genIType, genUType);
254     symbolTable.insertBuiltInOp(ESSL3_1_BUILTINS, EOpFindLSB, genIType, genIType);
255     symbolTable.insertBuiltInOp(ESSL3_1_BUILTINS, EOpFindLSB, genIType, genUType);
256     symbolTable.insertBuiltInOp(ESSL3_1_BUILTINS, EOpFindMSB, genIType, genIType);
257     symbolTable.insertBuiltInOp(ESSL3_1_BUILTINS, EOpFindMSB, genIType, genUType);
258     symbolTable.insertBuiltInOp(ESSL3_1_BUILTINS, EOpUaddCarry, genUType, genUType, genUType,
259                                 outGenUType);
260     symbolTable.insertBuiltInOp(ESSL3_1_BUILTINS, EOpUsubBorrow, genUType, genUType, genUType,
261                                 outGenUType);
262     symbolTable.insertBuiltInOp(ESSL3_1_BUILTINS, EOpUmulExtended, voidType, genUType, genUType,
263                                 outGenUType, outGenUType);
264     symbolTable.insertBuiltInOp(ESSL3_1_BUILTINS, EOpImulExtended, voidType, genIType, genIType,
265                                 outGenIType, outGenIType);
266 
267     const TType *sampler2D   = TCache::getType(EbtSampler2D);
268     const TType *samplerCube = TCache::getType(EbtSamplerCube);
269 
270     //
271     // Texture Functions for GLSL ES 1.0
272     //
273     symbolTable.insertBuiltIn(ESSL1_BUILTINS, float4, "texture2D", sampler2D, float2);
274     symbolTable.insertBuiltIn(ESSL1_BUILTINS, float4, "texture2DProj", sampler2D, float3);
275     symbolTable.insertBuiltIn(ESSL1_BUILTINS, float4, "texture2DProj", sampler2D, float4);
276     symbolTable.insertBuiltIn(ESSL1_BUILTINS, float4, "textureCube", samplerCube, float3);
277 
278     if (resources.OES_EGL_image_external || resources.NV_EGL_stream_consumer_external)
279     {
280         const TType *samplerExternalOES = TCache::getType(EbtSamplerExternalOES);
281 
282         symbolTable.insertBuiltIn(ESSL1_BUILTINS, float4, "texture2D", samplerExternalOES, float2);
283         symbolTable.insertBuiltIn(ESSL1_BUILTINS, float4, "texture2DProj", samplerExternalOES,
284                                   float3);
285         symbolTable.insertBuiltIn(ESSL1_BUILTINS, float4, "texture2DProj", samplerExternalOES,
286                                   float4);
287     }
288 
289     if (resources.ARB_texture_rectangle)
290     {
291         const TType *sampler2DRect = TCache::getType(EbtSampler2DRect);
292 
293         symbolTable.insertBuiltIn(ESSL1_BUILTINS, float4, "texture2DRect", sampler2DRect, float2);
294         symbolTable.insertBuiltIn(ESSL1_BUILTINS, float4, "texture2DRectProj", sampler2DRect,
295                                   float3);
296         symbolTable.insertBuiltIn(ESSL1_BUILTINS, float4, "texture2DRectProj", sampler2DRect,
297                                   float4);
298     }
299 
300     if (resources.EXT_shader_texture_lod)
301     {
302         /* The *Grad* variants are new to both vertex and fragment shaders; the fragment
303          * shader specific pieces are added separately below.
304          */
305         symbolTable.insertBuiltIn(ESSL1_BUILTINS, TExtension::EXT_shader_texture_lod, float4,
306                                   "texture2DGradEXT", sampler2D, float2, float2, float2);
307         symbolTable.insertBuiltIn(ESSL1_BUILTINS, TExtension::EXT_shader_texture_lod, float4,
308                                   "texture2DProjGradEXT", sampler2D, float3, float2, float2);
309         symbolTable.insertBuiltIn(ESSL1_BUILTINS, TExtension::EXT_shader_texture_lod, float4,
310                                   "texture2DProjGradEXT", sampler2D, float4, float2, float2);
311         symbolTable.insertBuiltIn(ESSL1_BUILTINS, TExtension::EXT_shader_texture_lod, float4,
312                                   "textureCubeGradEXT", samplerCube, float3, float3, float3);
313     }
314 
315     if (type == GL_FRAGMENT_SHADER)
316     {
317         symbolTable.insertBuiltIn(ESSL1_BUILTINS, float4, "texture2D", sampler2D, float2, float1);
318         symbolTable.insertBuiltIn(ESSL1_BUILTINS, float4, "texture2DProj", sampler2D, float3,
319                                   float1);
320         symbolTable.insertBuiltIn(ESSL1_BUILTINS, float4, "texture2DProj", sampler2D, float4,
321                                   float1);
322         symbolTable.insertBuiltIn(ESSL1_BUILTINS, float4, "textureCube", samplerCube, float3,
323                                   float1);
324 
325         if (resources.OES_standard_derivatives)
326         {
327             symbolTable.insertBuiltInOp(ESSL1_BUILTINS, EOpDFdx,
328                                         TExtension::OES_standard_derivatives, genType, genType);
329             symbolTable.insertBuiltInOp(ESSL1_BUILTINS, EOpDFdy,
330                                         TExtension::OES_standard_derivatives, genType, genType);
331             symbolTable.insertBuiltInOp(ESSL1_BUILTINS, EOpFwidth,
332                                         TExtension::OES_standard_derivatives, genType, genType);
333         }
334 
335         if (resources.EXT_shader_texture_lod)
336         {
337             symbolTable.insertBuiltIn(ESSL1_BUILTINS, TExtension::EXT_shader_texture_lod, float4,
338                                       "texture2DLodEXT", sampler2D, float2, float1);
339             symbolTable.insertBuiltIn(ESSL1_BUILTINS, TExtension::EXT_shader_texture_lod, float4,
340                                       "texture2DProjLodEXT", sampler2D, float3, float1);
341             symbolTable.insertBuiltIn(ESSL1_BUILTINS, TExtension::EXT_shader_texture_lod, float4,
342                                       "texture2DProjLodEXT", sampler2D, float4, float1);
343             symbolTable.insertBuiltIn(ESSL1_BUILTINS, TExtension::EXT_shader_texture_lod, float4,
344                                       "textureCubeLodEXT", samplerCube, float3, float1);
345         }
346     }
347 
348     if (type == GL_VERTEX_SHADER)
349     {
350         symbolTable.insertBuiltIn(ESSL1_BUILTINS, float4, "texture2DLod", sampler2D, float2,
351                                   float1);
352         symbolTable.insertBuiltIn(ESSL1_BUILTINS, float4, "texture2DProjLod", sampler2D, float3,
353                                   float1);
354         symbolTable.insertBuiltIn(ESSL1_BUILTINS, float4, "texture2DProjLod", sampler2D, float4,
355                                   float1);
356         symbolTable.insertBuiltIn(ESSL1_BUILTINS, float4, "textureCubeLod", samplerCube, float3,
357                                   float1);
358     }
359 
360     const TType *gvec4 = TCache::getType(EbtGVec4);
361 
362     const TType *gsampler2D      = TCache::getType(EbtGSampler2D);
363     const TType *gsamplerCube    = TCache::getType(EbtGSamplerCube);
364     const TType *gsampler3D      = TCache::getType(EbtGSampler3D);
365     const TType *gsampler2DArray = TCache::getType(EbtGSampler2DArray);
366     const TType *gsampler2DMS    = TCache::getType(EbtGSampler2DMS);
367 
368     //
369     // Texture Functions for GLSL ES 3.0
370     //
371     symbolTable.insertBuiltIn(ESSL3_BUILTINS, gvec4, "texture", gsampler2D, float2);
372     symbolTable.insertBuiltIn(ESSL3_BUILTINS, gvec4, "texture", gsampler3D, float3);
373     symbolTable.insertBuiltIn(ESSL3_BUILTINS, gvec4, "texture", gsamplerCube, float3);
374     symbolTable.insertBuiltIn(ESSL3_BUILTINS, gvec4, "texture", gsampler2DArray, float3);
375     symbolTable.insertBuiltIn(ESSL3_BUILTINS, gvec4, "textureProj", gsampler2D, float3);
376     symbolTable.insertBuiltIn(ESSL3_BUILTINS, gvec4, "textureProj", gsampler2D, float4);
377     symbolTable.insertBuiltIn(ESSL3_BUILTINS, gvec4, "textureProj", gsampler3D, float4);
378     symbolTable.insertBuiltIn(ESSL3_BUILTINS, gvec4, "textureLod", gsampler2D, float2, float1);
379     symbolTable.insertBuiltIn(ESSL3_BUILTINS, gvec4, "textureLod", gsampler3D, float3, float1);
380     symbolTable.insertBuiltIn(ESSL3_BUILTINS, gvec4, "textureLod", gsamplerCube, float3, float1);
381     symbolTable.insertBuiltIn(ESSL3_BUILTINS, gvec4, "textureLod", gsampler2DArray, float3, float1);
382 
383     if (resources.OES_EGL_image_external_essl3)
384     {
385         const TType *samplerExternalOES = TCache::getType(EbtSamplerExternalOES);
386 
387         symbolTable.insertBuiltIn(ESSL3_BUILTINS, float4, "texture", samplerExternalOES, float2);
388         symbolTable.insertBuiltIn(ESSL3_BUILTINS, float4, "textureProj", samplerExternalOES,
389                                   float3);
390         symbolTable.insertBuiltIn(ESSL3_BUILTINS, float4, "textureProj", samplerExternalOES,
391                                   float4);
392     }
393 
394     if (resources.EXT_YUV_target)
395     {
396         const TType *samplerExternal2DY2YEXT = TCache::getType(EbtSamplerExternal2DY2YEXT);
397 
398         symbolTable.insertBuiltIn(ESSL3_BUILTINS, TExtension::EXT_YUV_target, float4, "texture",
399                                   samplerExternal2DY2YEXT, float2);
400         symbolTable.insertBuiltIn(ESSL3_BUILTINS, TExtension::EXT_YUV_target, float4, "textureProj",
401                                   samplerExternal2DY2YEXT, float3);
402         symbolTable.insertBuiltIn(ESSL3_BUILTINS, TExtension::EXT_YUV_target, float4, "textureProj",
403                                   samplerExternal2DY2YEXT, float4);
404 
405         const TType *yuvCscStandardEXT = TCache::getType(EbtYuvCscStandardEXT);
406 
407         symbolTable.insertBuiltIn(ESSL3_BUILTINS, TExtension::EXT_YUV_target, float3, "rgb_2_yuv",
408                                   float3, yuvCscStandardEXT);
409         symbolTable.insertBuiltIn(ESSL3_BUILTINS, TExtension::EXT_YUV_target, float3, "yuv_2_rgb",
410                                   float3, yuvCscStandardEXT);
411     }
412 
413     if (type == GL_FRAGMENT_SHADER)
414     {
415         symbolTable.insertBuiltIn(ESSL3_BUILTINS, gvec4, "texture", gsampler2D, float2, float1);
416         symbolTable.insertBuiltIn(ESSL3_BUILTINS, gvec4, "texture", gsampler3D, float3, float1);
417         symbolTable.insertBuiltIn(ESSL3_BUILTINS, gvec4, "texture", gsamplerCube, float3, float1);
418         symbolTable.insertBuiltIn(ESSL3_BUILTINS, gvec4, "texture", gsampler2DArray, float3,
419                                   float1);
420         symbolTable.insertBuiltIn(ESSL3_BUILTINS, gvec4, "textureProj", gsampler2D, float3, float1);
421         symbolTable.insertBuiltIn(ESSL3_BUILTINS, gvec4, "textureProj", gsampler2D, float4, float1);
422         symbolTable.insertBuiltIn(ESSL3_BUILTINS, gvec4, "textureProj", gsampler3D, float4, float1);
423 
424         if (resources.OES_EGL_image_external_essl3)
425         {
426             const TType *samplerExternalOES = TCache::getType(EbtSamplerExternalOES);
427 
428             symbolTable.insertBuiltIn(ESSL3_BUILTINS, float4, "texture", samplerExternalOES, float2,
429                                       float1);
430             symbolTable.insertBuiltIn(ESSL3_BUILTINS, float4, "textureProj", samplerExternalOES,
431                                       float3, float1);
432             symbolTable.insertBuiltIn(ESSL3_BUILTINS, float4, "textureProj", samplerExternalOES,
433                                       float4, float1);
434         }
435 
436         if (resources.EXT_YUV_target)
437         {
438             const TType *samplerExternal2DY2YEXT = TCache::getType(EbtSamplerExternal2DY2YEXT);
439 
440             symbolTable.insertBuiltIn(ESSL3_BUILTINS, TExtension::EXT_YUV_target, float4, "texture",
441                                       samplerExternal2DY2YEXT, float2, float1);
442             symbolTable.insertBuiltIn(ESSL3_BUILTINS, TExtension::EXT_YUV_target, float4,
443                                       "textureProj", samplerExternal2DY2YEXT, float3, float1);
444             symbolTable.insertBuiltIn(ESSL3_BUILTINS, TExtension::EXT_YUV_target, float4,
445                                       "textureProj", samplerExternal2DY2YEXT, float4, float1);
446         }
447     }
448 
449     const TType *sampler2DShadow      = TCache::getType(EbtSampler2DShadow);
450     const TType *samplerCubeShadow    = TCache::getType(EbtSamplerCubeShadow);
451     const TType *sampler2DArrayShadow = TCache::getType(EbtSampler2DArrayShadow);
452 
453     symbolTable.insertBuiltIn(ESSL3_BUILTINS, float1, "texture", sampler2DShadow, float3);
454     symbolTable.insertBuiltIn(ESSL3_BUILTINS, float1, "texture", samplerCubeShadow, float4);
455     symbolTable.insertBuiltIn(ESSL3_BUILTINS, float1, "texture", sampler2DArrayShadow, float4);
456     symbolTable.insertBuiltIn(ESSL3_BUILTINS, float1, "textureProj", sampler2DShadow, float4);
457     symbolTable.insertBuiltIn(ESSL3_BUILTINS, float1, "textureLod", sampler2DShadow, float3,
458                               float1);
459 
460     if (type == GL_FRAGMENT_SHADER)
461     {
462         symbolTable.insertBuiltIn(ESSL3_BUILTINS, float1, "texture", sampler2DShadow, float3,
463                                   float1);
464         symbolTable.insertBuiltIn(ESSL3_BUILTINS, float1, "texture", samplerCubeShadow, float4,
465                                   float1);
466         symbolTable.insertBuiltIn(ESSL3_BUILTINS, float1, "textureProj", sampler2DShadow, float4,
467                                   float1);
468     }
469 
470     symbolTable.insertBuiltIn(ESSL3_BUILTINS, int2, "textureSize", gsampler2D, int1);
471     symbolTable.insertBuiltIn(ESSL3_BUILTINS, int3, "textureSize", gsampler3D, int1);
472     symbolTable.insertBuiltIn(ESSL3_BUILTINS, int2, "textureSize", gsamplerCube, int1);
473     symbolTable.insertBuiltIn(ESSL3_BUILTINS, int3, "textureSize", gsampler2DArray, int1);
474     symbolTable.insertBuiltIn(ESSL3_BUILTINS, int2, "textureSize", sampler2DShadow, int1);
475     symbolTable.insertBuiltIn(ESSL3_BUILTINS, int2, "textureSize", samplerCubeShadow, int1);
476     symbolTable.insertBuiltIn(ESSL3_BUILTINS, int3, "textureSize", sampler2DArrayShadow, int1);
477     symbolTable.insertBuiltIn(ESSL3_BUILTINS, int2, "textureSize", gsampler2DMS);
478 
479     if (resources.OES_EGL_image_external_essl3)
480     {
481         const TType *samplerExternalOES = TCache::getType(EbtSamplerExternalOES);
482 
483         symbolTable.insertBuiltIn(ESSL3_BUILTINS, int2, "textureSize", samplerExternalOES, int1);
484     }
485 
486     if (resources.EXT_YUV_target)
487     {
488         const TType *samplerExternal2DY2YEXT = TCache::getType(EbtSamplerExternal2DY2YEXT);
489 
490         symbolTable.insertBuiltIn(ESSL3_BUILTINS, TExtension::EXT_YUV_target, int2, "textureSize",
491                                   samplerExternal2DY2YEXT, int1);
492     }
493 
494     if (type == GL_FRAGMENT_SHADER)
495     {
496         symbolTable.insertBuiltInOp(ESSL3_BUILTINS, EOpDFdx, genType, genType);
497         symbolTable.insertBuiltInOp(ESSL3_BUILTINS, EOpDFdy, genType, genType);
498         symbolTable.insertBuiltInOp(ESSL3_BUILTINS, EOpFwidth, genType, genType);
499     }
500 
501     symbolTable.insertBuiltIn(ESSL3_BUILTINS, gvec4, "textureOffset", gsampler2D, float2, int2);
502     symbolTable.insertBuiltIn(ESSL3_BUILTINS, gvec4, "textureOffset", gsampler3D, float3, int3);
503     symbolTable.insertBuiltIn(ESSL3_BUILTINS, float1, "textureOffset", sampler2DShadow, float3,
504                               int2);
505     symbolTable.insertBuiltIn(ESSL3_BUILTINS, gvec4, "textureOffset", gsampler2DArray, float3,
506                               int2);
507 
508     if (type == GL_FRAGMENT_SHADER)
509     {
510         symbolTable.insertBuiltIn(ESSL3_BUILTINS, gvec4, "textureOffset", gsampler2D, float2, int2,
511                                   float1);
512         symbolTable.insertBuiltIn(ESSL3_BUILTINS, gvec4, "textureOffset", gsampler3D, float3, int3,
513                                   float1);
514         symbolTable.insertBuiltIn(ESSL3_BUILTINS, float1, "textureOffset", sampler2DShadow, float3,
515                                   int2, float1);
516         symbolTable.insertBuiltIn(ESSL3_BUILTINS, gvec4, "textureOffset", gsampler2DArray, float3,
517                                   int2, float1);
518     }
519 
520     symbolTable.insertBuiltIn(ESSL3_BUILTINS, gvec4, "textureProjOffset", gsampler2D, float3, int2);
521     symbolTable.insertBuiltIn(ESSL3_BUILTINS, gvec4, "textureProjOffset", gsampler2D, float4, int2);
522     symbolTable.insertBuiltIn(ESSL3_BUILTINS, gvec4, "textureProjOffset", gsampler3D, float4, int3);
523     symbolTable.insertBuiltIn(ESSL3_BUILTINS, float1, "textureProjOffset", sampler2DShadow, float4,
524                               int2);
525 
526     if (type == GL_FRAGMENT_SHADER)
527     {
528         symbolTable.insertBuiltIn(ESSL3_BUILTINS, gvec4, "textureProjOffset", gsampler2D, float3,
529                                   int2, float1);
530         symbolTable.insertBuiltIn(ESSL3_BUILTINS, gvec4, "textureProjOffset", gsampler2D, float4,
531                                   int2, float1);
532         symbolTable.insertBuiltIn(ESSL3_BUILTINS, gvec4, "textureProjOffset", gsampler3D, float4,
533                                   int3, float1);
534         symbolTable.insertBuiltIn(ESSL3_BUILTINS, float1, "textureProjOffset", sampler2DShadow,
535                                   float4, int2, float1);
536     }
537 
538     symbolTable.insertBuiltIn(ESSL3_BUILTINS, gvec4, "textureLodOffset", gsampler2D, float2, float1,
539                               int2);
540     symbolTable.insertBuiltIn(ESSL3_BUILTINS, gvec4, "textureLodOffset", gsampler3D, float3, float1,
541                               int3);
542     symbolTable.insertBuiltIn(ESSL3_BUILTINS, float1, "textureLodOffset", sampler2DShadow, float3,
543                               float1, int2);
544     symbolTable.insertBuiltIn(ESSL3_BUILTINS, gvec4, "textureLodOffset", gsampler2DArray, float3,
545                               float1, int2);
546 
547     symbolTable.insertBuiltIn(ESSL3_BUILTINS, gvec4, "textureProjLod", gsampler2D, float3, float1);
548     symbolTable.insertBuiltIn(ESSL3_BUILTINS, gvec4, "textureProjLod", gsampler2D, float4, float1);
549     symbolTable.insertBuiltIn(ESSL3_BUILTINS, gvec4, "textureProjLod", gsampler3D, float4, float1);
550     symbolTable.insertBuiltIn(ESSL3_BUILTINS, float1, "textureProjLod", sampler2DShadow, float4,
551                               float1);
552 
553     symbolTable.insertBuiltIn(ESSL3_BUILTINS, gvec4, "textureProjLodOffset", gsampler2D, float3,
554                               float1, int2);
555     symbolTable.insertBuiltIn(ESSL3_BUILTINS, gvec4, "textureProjLodOffset", gsampler2D, float4,
556                               float1, int2);
557     symbolTable.insertBuiltIn(ESSL3_BUILTINS, gvec4, "textureProjLodOffset", gsampler3D, float4,
558                               float1, int3);
559     symbolTable.insertBuiltIn(ESSL3_BUILTINS, float1, "textureProjLodOffset", sampler2DShadow,
560                               float4, float1, int2);
561 
562     symbolTable.insertBuiltIn(ESSL3_BUILTINS, gvec4, "texelFetch", gsampler2D, int2, int1);
563     symbolTable.insertBuiltIn(ESSL3_BUILTINS, gvec4, "texelFetch", gsampler3D, int3, int1);
564     symbolTable.insertBuiltIn(ESSL3_BUILTINS, gvec4, "texelFetch", gsampler2DArray, int3, int1);
565 
566     if (resources.OES_EGL_image_external_essl3)
567     {
568         const TType *samplerExternalOES = TCache::getType(EbtSamplerExternalOES);
569 
570         symbolTable.insertBuiltIn(ESSL3_BUILTINS, float4, "texelFetch", samplerExternalOES, int2,
571                                   int1);
572     }
573 
574     if (resources.EXT_YUV_target)
575     {
576         const TType *samplerExternal2DY2YEXT = TCache::getType(EbtSamplerExternal2DY2YEXT);
577 
578         symbolTable.insertBuiltIn(ESSL3_BUILTINS, TExtension::EXT_YUV_target, float4, "texelFetch",
579                                   samplerExternal2DY2YEXT, int2, int1);
580     }
581 
582     symbolTable.insertBuiltIn(ESSL3_BUILTINS, gvec4, "texelFetchOffset", gsampler2D, int2, int1,
583                               int2);
584     symbolTable.insertBuiltIn(ESSL3_BUILTINS, gvec4, "texelFetchOffset", gsampler3D, int3, int1,
585                               int3);
586     symbolTable.insertBuiltIn(ESSL3_BUILTINS, gvec4, "texelFetchOffset", gsampler2DArray, int3,
587                               int1, int2);
588 
589     symbolTable.insertBuiltIn(ESSL3_BUILTINS, gvec4, "textureGrad", gsampler2D, float2, float2,
590                               float2);
591     symbolTable.insertBuiltIn(ESSL3_BUILTINS, gvec4, "textureGrad", gsampler3D, float3, float3,
592                               float3);
593     symbolTable.insertBuiltIn(ESSL3_BUILTINS, gvec4, "textureGrad", gsamplerCube, float3, float3,
594                               float3);
595     symbolTable.insertBuiltIn(ESSL3_BUILTINS, float1, "textureGrad", sampler2DShadow, float3,
596                               float2, float2);
597     symbolTable.insertBuiltIn(ESSL3_BUILTINS, float1, "textureGrad", samplerCubeShadow, float4,
598                               float3, float3);
599     symbolTable.insertBuiltIn(ESSL3_BUILTINS, gvec4, "textureGrad", gsampler2DArray, float3, float2,
600                               float2);
601     symbolTable.insertBuiltIn(ESSL3_BUILTINS, float1, "textureGrad", sampler2DArrayShadow, float4,
602                               float2, float2);
603 
604     symbolTable.insertBuiltIn(ESSL3_BUILTINS, gvec4, "textureGradOffset", gsampler2D, float2,
605                               float2, float2, int2);
606     symbolTable.insertBuiltIn(ESSL3_BUILTINS, gvec4, "textureGradOffset", gsampler3D, float3,
607                               float3, float3, int3);
608     symbolTable.insertBuiltIn(ESSL3_BUILTINS, float1, "textureGradOffset", sampler2DShadow, float3,
609                               float2, float2, int2);
610     symbolTable.insertBuiltIn(ESSL3_BUILTINS, gvec4, "textureGradOffset", gsampler2DArray, float3,
611                               float2, float2, int2);
612     symbolTable.insertBuiltIn(ESSL3_BUILTINS, float1, "textureGradOffset", sampler2DArrayShadow,
613                               float4, float2, float2, int2);
614 
615     symbolTable.insertBuiltIn(ESSL3_BUILTINS, gvec4, "textureProjGrad", gsampler2D, float3, float2,
616                               float2);
617     symbolTable.insertBuiltIn(ESSL3_BUILTINS, gvec4, "textureProjGrad", gsampler2D, float4, float2,
618                               float2);
619     symbolTable.insertBuiltIn(ESSL3_BUILTINS, gvec4, "textureProjGrad", gsampler3D, float4, float3,
620                               float3);
621     symbolTable.insertBuiltIn(ESSL3_BUILTINS, float1, "textureProjGrad", sampler2DShadow, float4,
622                               float2, float2);
623 
624     symbolTable.insertBuiltIn(ESSL3_BUILTINS, gvec4, "textureProjGradOffset", gsampler2D, float3,
625                               float2, float2, int2);
626     symbolTable.insertBuiltIn(ESSL3_BUILTINS, gvec4, "textureProjGradOffset", gsampler2D, float4,
627                               float2, float2, int2);
628     symbolTable.insertBuiltIn(ESSL3_BUILTINS, gvec4, "textureProjGradOffset", gsampler3D, float4,
629                               float3, float3, int3);
630     symbolTable.insertBuiltIn(ESSL3_BUILTINS, float1, "textureProjGradOffset", sampler2DShadow,
631                               float4, float2, float2, int2);
632 
633     const TType *atomicCounter = TCache::getType(EbtAtomicCounter);
634     symbolTable.insertBuiltIn(ESSL3_1_BUILTINS, uint1, "atomicCounter", atomicCounter);
635     symbolTable.insertBuiltIn(ESSL3_1_BUILTINS, uint1, "atomicCounterIncrement", atomicCounter);
636     symbolTable.insertBuiltIn(ESSL3_1_BUILTINS, uint1, "atomicCounterDecrement", atomicCounter);
637 
638     // Insert all atomic memory functions
639     const TType *int1InOut  = TCache::getType(EbtInt, EvqInOut);
640     const TType *uint1InOut = TCache::getType(EbtUInt, EvqInOut);
641     symbolTable.insertBuiltIn(ESSL3_1_BUILTINS, uint1, "atomicAdd", uint1InOut, uint1);
642     symbolTable.insertBuiltIn(ESSL3_1_BUILTINS, int1, "atomicAdd", int1InOut, int1);
643     symbolTable.insertBuiltIn(ESSL3_1_BUILTINS, uint1, "atomicMin", uint1InOut, uint1);
644     symbolTable.insertBuiltIn(ESSL3_1_BUILTINS, int1, "atomicMin", int1InOut, int1);
645     symbolTable.insertBuiltIn(ESSL3_1_BUILTINS, uint1, "atomicMax", uint1InOut, uint1);
646     symbolTable.insertBuiltIn(ESSL3_1_BUILTINS, int1, "atomicMax", int1InOut, int1);
647     symbolTable.insertBuiltIn(ESSL3_1_BUILTINS, uint1, "atomicAnd", uint1InOut, uint1);
648     symbolTable.insertBuiltIn(ESSL3_1_BUILTINS, int1, "atomicAnd", int1InOut, int1);
649     symbolTable.insertBuiltIn(ESSL3_1_BUILTINS, uint1, "atomicOr", uint1InOut, uint1);
650     symbolTable.insertBuiltIn(ESSL3_1_BUILTINS, int1, "atomicOr", int1InOut, int1);
651     symbolTable.insertBuiltIn(ESSL3_1_BUILTINS, uint1, "atomicXor", uint1InOut, uint1);
652     symbolTable.insertBuiltIn(ESSL3_1_BUILTINS, int1, "atomicXor", int1InOut, int1);
653     symbolTable.insertBuiltIn(ESSL3_1_BUILTINS, uint1, "atomicExchange", uint1InOut, uint1);
654     symbolTable.insertBuiltIn(ESSL3_1_BUILTINS, int1, "atomicExchange", int1InOut, int1);
655     symbolTable.insertBuiltIn(ESSL3_1_BUILTINS, uint1, "atomicCompSwap", uint1InOut, uint1, uint1);
656     symbolTable.insertBuiltIn(ESSL3_1_BUILTINS, int1, "atomicCompSwap", int1InOut, int1, int1);
657 
658     const TType *gimage2D      = TCache::getType(EbtGImage2D);
659     const TType *gimage3D      = TCache::getType(EbtGImage3D);
660     const TType *gimage2DArray = TCache::getType(EbtGImage2DArray);
661     const TType *gimageCube    = TCache::getType(EbtGImageCube);
662 
663     symbolTable.insertBuiltIn(ESSL3_1_BUILTINS, voidType, "imageStore", gimage2D, int2, gvec4);
664     symbolTable.insertBuiltIn(ESSL3_1_BUILTINS, voidType, "imageStore", gimage3D, int3, gvec4);
665     symbolTable.insertBuiltIn(ESSL3_1_BUILTINS, voidType, "imageStore", gimage2DArray, int3, gvec4);
666     symbolTable.insertBuiltIn(ESSL3_1_BUILTINS, voidType, "imageStore", gimageCube, int3, gvec4);
667 
668     symbolTable.insertBuiltIn(ESSL3_1_BUILTINS, gvec4, "imageLoad", gimage2D, int2);
669     symbolTable.insertBuiltIn(ESSL3_1_BUILTINS, gvec4, "imageLoad", gimage3D, int3);
670     symbolTable.insertBuiltIn(ESSL3_1_BUILTINS, gvec4, "imageLoad", gimage2DArray, int3);
671     symbolTable.insertBuiltIn(ESSL3_1_BUILTINS, gvec4, "imageLoad", gimageCube, int3);
672 
673     symbolTable.insertBuiltIn(ESSL3_1_BUILTINS, int2, "imageSize", gimage2D);
674     symbolTable.insertBuiltIn(ESSL3_1_BUILTINS, int3, "imageSize", gimage3D);
675     symbolTable.insertBuiltIn(ESSL3_1_BUILTINS, int3, "imageSize", gimage2DArray);
676     symbolTable.insertBuiltIn(ESSL3_1_BUILTINS, int2, "imageSize", gimageCube);
677 
678     symbolTable.insertBuiltInFunctionNoParameters(ESSL3_1_BUILTINS, EOpMemoryBarrier, voidType,
679                                                   "memoryBarrier");
680     symbolTable.insertBuiltInFunctionNoParameters(ESSL3_1_BUILTINS, EOpMemoryBarrierAtomicCounter,
681                                                   voidType, "memoryBarrierAtomicCounter");
682     symbolTable.insertBuiltInFunctionNoParameters(ESSL3_1_BUILTINS, EOpMemoryBarrierBuffer,
683                                                   voidType, "memoryBarrierBuffer");
684     symbolTable.insertBuiltInFunctionNoParameters(ESSL3_1_BUILTINS, EOpMemoryBarrierImage, voidType,
685                                                   "memoryBarrierImage");
686 
687     symbolTable.insertBuiltIn(ESSL3_1_BUILTINS, gvec4, "texelFetch", gsampler2DMS, int2, int1);
688 
689     // Insert all variations of textureGather.
690     symbolTable.insertBuiltIn(ESSL3_1_BUILTINS, gvec4, "textureGather", gsampler2D, float2);
691     symbolTable.insertBuiltIn(ESSL3_1_BUILTINS, gvec4, "textureGather", gsampler2D, float2, int1);
692     symbolTable.insertBuiltIn(ESSL3_1_BUILTINS, gvec4, "textureGather", gsampler2DArray, float3);
693     symbolTable.insertBuiltIn(ESSL3_1_BUILTINS, gvec4, "textureGather", gsampler2DArray, float3,
694                               int1);
695     symbolTable.insertBuiltIn(ESSL3_1_BUILTINS, gvec4, "textureGather", gsamplerCube, float3);
696     symbolTable.insertBuiltIn(ESSL3_1_BUILTINS, gvec4, "textureGather", gsamplerCube, float3, int1);
697     symbolTable.insertBuiltIn(ESSL3_1_BUILTINS, float4, "textureGather", sampler2DShadow, float2);
698     symbolTable.insertBuiltIn(ESSL3_1_BUILTINS, float4, "textureGather", sampler2DShadow, float2,
699                               float1);
700     symbolTable.insertBuiltIn(ESSL3_1_BUILTINS, float4, "textureGather", sampler2DArrayShadow,
701                               float3);
702     symbolTable.insertBuiltIn(ESSL3_1_BUILTINS, float4, "textureGather", sampler2DArrayShadow,
703                               float3, float1);
704     symbolTable.insertBuiltIn(ESSL3_1_BUILTINS, float4, "textureGather", samplerCubeShadow, float3);
705     symbolTable.insertBuiltIn(ESSL3_1_BUILTINS, float4, "textureGather", samplerCubeShadow, float3,
706                               float1);
707 
708     // Insert all variations of textureGatherOffset.
709     symbolTable.insertBuiltIn(ESSL3_1_BUILTINS, gvec4, "textureGatherOffset", gsampler2D, float2,
710                               int2);
711     symbolTable.insertBuiltIn(ESSL3_1_BUILTINS, gvec4, "textureGatherOffset", gsampler2D, float2,
712                               int2, int1);
713     symbolTable.insertBuiltIn(ESSL3_1_BUILTINS, gvec4, "textureGatherOffset", gsampler2DArray,
714                               float3, int2);
715     symbolTable.insertBuiltIn(ESSL3_1_BUILTINS, gvec4, "textureGatherOffset", gsampler2DArray,
716                               float3, int2, int1);
717     symbolTable.insertBuiltIn(ESSL3_1_BUILTINS, float4, "textureGatherOffset", sampler2DShadow,
718                               float2, float1, int2);
719     symbolTable.insertBuiltIn(ESSL3_1_BUILTINS, float4, "textureGatherOffset", sampler2DArrayShadow,
720                               float3, float1, int2);
721 
722     if (type == GL_COMPUTE_SHADER)
723     {
724         symbolTable.insertBuiltInFunctionNoParameters(ESSL3_1_BUILTINS, EOpBarrier, voidType,
725                                                       "barrier");
726         symbolTable.insertBuiltInFunctionNoParameters(ESSL3_1_BUILTINS, EOpMemoryBarrierShared,
727                                                       voidType, "memoryBarrierShared");
728         symbolTable.insertBuiltInFunctionNoParameters(ESSL3_1_BUILTINS, EOpGroupMemoryBarrier,
729                                                       voidType, "groupMemoryBarrier");
730     }
731 
732     if (type == GL_GEOMETRY_SHADER_OES)
733     {
734         TExtension extension = TExtension::OES_geometry_shader;
735         symbolTable.insertBuiltInFunctionNoParametersExt(ESSL3_1_BUILTINS, extension, EOpEmitVertex,
736                                                          voidType, "EmitVertex");
737         symbolTable.insertBuiltInFunctionNoParametersExt(ESSL3_1_BUILTINS, extension,
738                                                          EOpEndPrimitive, voidType, "EndPrimitive");
739     }
740 
741     //
742     // Depth range in window coordinates
743     //
744     TFieldList *fields       = NewPoolTFieldList();
745     TSourceLoc zeroSourceLoc = {0, 0, 0, 0};
746     auto highpFloat1         = new TType(EbtFloat, EbpHigh, EvqGlobal, 1);
747     TField *near             = new TField(highpFloat1, NewPoolTString("near"), zeroSourceLoc);
748     TField *far              = new TField(highpFloat1, NewPoolTString("far"), zeroSourceLoc);
749     TField *diff             = new TField(highpFloat1, NewPoolTString("diff"), zeroSourceLoc);
750     fields->push_back(near);
751     fields->push_back(far);
752     fields->push_back(diff);
753     TStructure *depthRangeStruct =
754         new TStructure(&symbolTable, NewPoolTString("gl_DepthRangeParameters"), fields);
755     symbolTable.insertStructType(COMMON_BUILTINS, depthRangeStruct);
756     TType depthRangeType(depthRangeStruct);
757     depthRangeType.setQualifier(EvqUniform);
758     symbolTable.insertVariable(COMMON_BUILTINS, "gl_DepthRange", depthRangeType);
759 
760     //
761     // Implementation dependent built-in constants.
762     //
763     symbolTable.insertConstInt(COMMON_BUILTINS, "gl_MaxVertexAttribs", resources.MaxVertexAttribs,
764                                EbpMedium);
765     symbolTable.insertConstInt(COMMON_BUILTINS, "gl_MaxVertexUniformVectors",
766                                resources.MaxVertexUniformVectors, EbpMedium);
767     symbolTable.insertConstInt(COMMON_BUILTINS, "gl_MaxVertexTextureImageUnits",
768                                resources.MaxVertexTextureImageUnits, EbpMedium);
769     symbolTable.insertConstInt(COMMON_BUILTINS, "gl_MaxCombinedTextureImageUnits",
770                                resources.MaxCombinedTextureImageUnits, EbpMedium);
771     symbolTable.insertConstInt(COMMON_BUILTINS, "gl_MaxTextureImageUnits",
772                                resources.MaxTextureImageUnits, EbpMedium);
773     symbolTable.insertConstInt(COMMON_BUILTINS, "gl_MaxFragmentUniformVectors",
774                                resources.MaxFragmentUniformVectors, EbpMedium);
775 
776     symbolTable.insertConstInt(ESSL1_BUILTINS, "gl_MaxVaryingVectors", resources.MaxVaryingVectors,
777                                EbpMedium);
778 
779     symbolTable.insertConstInt(COMMON_BUILTINS, "gl_MaxDrawBuffers", resources.MaxDrawBuffers,
780                                EbpMedium);
781     if (resources.EXT_blend_func_extended)
782     {
783         symbolTable.insertConstIntExt(COMMON_BUILTINS, TExtension::EXT_blend_func_extended,
784                                       "gl_MaxDualSourceDrawBuffersEXT",
785                                       resources.MaxDualSourceDrawBuffers, EbpMedium);
786     }
787 
788     symbolTable.insertConstInt(ESSL3_BUILTINS, "gl_MaxVertexOutputVectors",
789                                resources.MaxVertexOutputVectors, EbpMedium);
790     symbolTable.insertConstInt(ESSL3_BUILTINS, "gl_MaxFragmentInputVectors",
791                                resources.MaxFragmentInputVectors, EbpMedium);
792     symbolTable.insertConstInt(ESSL3_BUILTINS, "gl_MinProgramTexelOffset",
793                                resources.MinProgramTexelOffset, EbpMedium);
794     symbolTable.insertConstInt(ESSL3_BUILTINS, "gl_MaxProgramTexelOffset",
795                                resources.MaxProgramTexelOffset, EbpMedium);
796 
797     symbolTable.insertConstInt(ESSL3_1_BUILTINS, "gl_MaxImageUnits", resources.MaxImageUnits,
798                                EbpMedium);
799     symbolTable.insertConstInt(ESSL3_1_BUILTINS, "gl_MaxVertexImageUniforms",
800                                resources.MaxVertexImageUniforms, EbpMedium);
801     symbolTable.insertConstInt(ESSL3_1_BUILTINS, "gl_MaxFragmentImageUniforms",
802                                resources.MaxFragmentImageUniforms, EbpMedium);
803     symbolTable.insertConstInt(ESSL3_1_BUILTINS, "gl_MaxComputeImageUniforms",
804                                resources.MaxComputeImageUniforms, EbpMedium);
805     symbolTable.insertConstInt(ESSL3_1_BUILTINS, "gl_MaxCombinedImageUniforms",
806                                resources.MaxCombinedImageUniforms, EbpMedium);
807 
808     symbolTable.insertConstInt(ESSL3_1_BUILTINS, "gl_MaxCombinedShaderOutputResources",
809                                resources.MaxCombinedShaderOutputResources, EbpMedium);
810 
811     symbolTable.insertConstIvec3(ESSL3_1_BUILTINS, "gl_MaxComputeWorkGroupCount",
812                                  resources.MaxComputeWorkGroupCount, EbpHigh);
813     symbolTable.insertConstIvec3(ESSL3_1_BUILTINS, "gl_MaxComputeWorkGroupSize",
814                                  resources.MaxComputeWorkGroupSize, EbpHigh);
815     symbolTable.insertConstInt(ESSL3_1_BUILTINS, "gl_MaxComputeUniformComponents",
816                                resources.MaxComputeUniformComponents, EbpMedium);
817     symbolTable.insertConstInt(ESSL3_1_BUILTINS, "gl_MaxComputeTextureImageUnits",
818                                resources.MaxComputeTextureImageUnits, EbpMedium);
819 
820     symbolTable.insertConstInt(ESSL3_1_BUILTINS, "gl_MaxComputeAtomicCounters",
821                                resources.MaxComputeAtomicCounters, EbpMedium);
822     symbolTable.insertConstInt(ESSL3_1_BUILTINS, "gl_MaxComputeAtomicCounterBuffers",
823                                resources.MaxComputeAtomicCounterBuffers, EbpMedium);
824 
825     symbolTable.insertConstInt(ESSL3_1_BUILTINS, "gl_MaxVertexAtomicCounters",
826                                resources.MaxVertexAtomicCounters, EbpMedium);
827     symbolTable.insertConstInt(ESSL3_1_BUILTINS, "gl_MaxFragmentAtomicCounters",
828                                resources.MaxFragmentAtomicCounters, EbpMedium);
829     symbolTable.insertConstInt(ESSL3_1_BUILTINS, "gl_MaxCombinedAtomicCounters",
830                                resources.MaxCombinedAtomicCounters, EbpMedium);
831     symbolTable.insertConstInt(ESSL3_1_BUILTINS, "gl_MaxAtomicCounterBindings",
832                                resources.MaxAtomicCounterBindings, EbpMedium);
833 
834     symbolTable.insertConstInt(ESSL3_1_BUILTINS, "gl_MaxVertexAtomicCounterBuffers",
835                                resources.MaxVertexAtomicCounterBuffers, EbpMedium);
836     symbolTable.insertConstInt(ESSL3_1_BUILTINS, "gl_MaxFragmentAtomicCounterBuffers",
837                                resources.MaxFragmentAtomicCounterBuffers, EbpMedium);
838     symbolTable.insertConstInt(ESSL3_1_BUILTINS, "gl_MaxCombinedAtomicCounterBuffers",
839                                resources.MaxCombinedAtomicCounterBuffers, EbpMedium);
840     symbolTable.insertConstInt(ESSL3_1_BUILTINS, "gl_MaxAtomicCounterBufferSize",
841                                resources.MaxAtomicCounterBufferSize, EbpMedium);
842 
843     if (resources.OES_geometry_shader)
844     {
845         TExtension ext = TExtension::OES_geometry_shader;
846         symbolTable.insertConstIntExt(ESSL3_1_BUILTINS, ext, "gl_MaxGeometryInputComponents",
847                                       resources.MaxGeometryInputComponents, EbpMedium);
848         symbolTable.insertConstIntExt(ESSL3_1_BUILTINS, ext, "gl_MaxGeometryOutputComponents",
849                                       resources.MaxGeometryOutputComponents, EbpMedium);
850         symbolTable.insertConstIntExt(ESSL3_1_BUILTINS, ext, "gl_MaxGeometryImageUniforms",
851                                       resources.MaxGeometryImageUniforms, EbpMedium);
852         symbolTable.insertConstIntExt(ESSL3_1_BUILTINS, ext, "gl_MaxGeometryTextureImageUnits",
853                                       resources.MaxGeometryTextureImageUnits, EbpMedium);
854         symbolTable.insertConstIntExt(ESSL3_1_BUILTINS, ext, "gl_MaxGeometryOutputVertices",
855                                       resources.MaxGeometryOutputVertices, EbpMedium);
856         symbolTable.insertConstIntExt(ESSL3_1_BUILTINS, ext, "gl_MaxGeometryTotalOutputComponents",
857                                       resources.MaxGeometryTotalOutputComponents, EbpMedium);
858         symbolTable.insertConstIntExt(ESSL3_1_BUILTINS, ext, "gl_MaxGeometryUniformComponents",
859                                       resources.MaxGeometryUniformComponents, EbpMedium);
860         symbolTable.insertConstIntExt(ESSL3_1_BUILTINS, ext, "gl_MaxGeometryAtomicCounters",
861                                       resources.MaxGeometryAtomicCounters, EbpMedium);
862         symbolTable.insertConstIntExt(ESSL3_1_BUILTINS, ext, "gl_MaxGeometryAtomicCounterBuffers",
863                                       resources.MaxGeometryAtomicCounterBuffers, EbpMedium);
864     }
865 }
866 
IdentifyBuiltIns(sh::GLenum type,ShShaderSpec spec,const ShBuiltInResources & resources,TSymbolTable & symbolTable)867 void IdentifyBuiltIns(sh::GLenum type,
868                       ShShaderSpec spec,
869                       const ShBuiltInResources &resources,
870                       TSymbolTable &symbolTable)
871 {
872     //
873     // Insert some special built-in variables that are not in
874     // the built-in header files.
875     //
876 
877     if (resources.OVR_multiview && type != GL_COMPUTE_SHADER)
878     {
879         symbolTable.insertVariableExt(ESSL3_BUILTINS, TExtension::OVR_multiview, "gl_ViewID_OVR",
880                                       TType(EbtUInt, EbpHigh, EvqViewIDOVR, 1));
881 
882         // ESSL 1.00 doesn't have unsigned integers, so gl_ViewID_OVR is a signed integer in ESSL
883         // 1.00. This is specified in the WEBGL_multiview spec.
884         symbolTable.insertVariableExt(ESSL1_BUILTINS, TExtension::OVR_multiview, "gl_ViewID_OVR",
885                                       TType(EbtInt, EbpHigh, EvqViewIDOVR, 1));
886     }
887 
888     switch (type)
889     {
890         case GL_FRAGMENT_SHADER:
891         {
892             symbolTable.insertVariable(COMMON_BUILTINS, "gl_FragCoord",
893                                        TType(EbtFloat, EbpMedium, EvqFragCoord, 4));
894             symbolTable.insertVariable(COMMON_BUILTINS, "gl_FrontFacing",
895                                        TType(EbtBool, EbpUndefined, EvqFrontFacing, 1));
896             symbolTable.insertVariable(COMMON_BUILTINS, "gl_PointCoord",
897                                        TType(EbtFloat, EbpMedium, EvqPointCoord, 2));
898 
899             symbolTable.insertVariable(ESSL1_BUILTINS, "gl_FragColor",
900                                        TType(EbtFloat, EbpMedium, EvqFragColor, 4));
901             TType fragData(EbtFloat, EbpMedium, EvqFragData, 4);
902             if (spec != SH_WEBGL2_SPEC && spec != SH_WEBGL3_SPEC)
903             {
904                 fragData.makeArray(resources.MaxDrawBuffers);
905             }
906             else
907             {
908                 fragData.makeArray(1u);
909             }
910             symbolTable.insertVariable(ESSL1_BUILTINS, "gl_FragData", fragData);
911 
912             if (resources.EXT_blend_func_extended)
913             {
914                 symbolTable.insertVariableExt(
915                     ESSL1_BUILTINS, TExtension::EXT_blend_func_extended, "gl_SecondaryFragColorEXT",
916                     TType(EbtFloat, EbpMedium, EvqSecondaryFragColorEXT, 4));
917                 TType secondaryFragData(EbtFloat, EbpMedium, EvqSecondaryFragDataEXT, 4, 1);
918                 secondaryFragData.makeArray(resources.MaxDualSourceDrawBuffers);
919                 symbolTable.insertVariableExt(ESSL1_BUILTINS, TExtension::EXT_blend_func_extended,
920                                               "gl_SecondaryFragDataEXT", secondaryFragData);
921             }
922 
923             if (resources.EXT_frag_depth)
924             {
925                 symbolTable.insertVariableExt(
926                     ESSL1_BUILTINS, TExtension::EXT_frag_depth, "gl_FragDepthEXT",
927                     TType(EbtFloat, resources.FragmentPrecisionHigh ? EbpHigh : EbpMedium,
928                           EvqFragDepthEXT, 1));
929             }
930 
931             symbolTable.insertVariable(ESSL3_BUILTINS, "gl_FragDepth",
932                                        TType(EbtFloat, EbpHigh, EvqFragDepth, 1));
933 
934             if (resources.EXT_shader_framebuffer_fetch || resources.NV_shader_framebuffer_fetch)
935             {
936                 TType lastFragData(EbtFloat, EbpMedium, EvqLastFragData, 4, 1);
937                 lastFragData.makeArray(resources.MaxDrawBuffers);
938 
939                 if (resources.EXT_shader_framebuffer_fetch)
940                 {
941                     symbolTable.insertVariableExt(ESSL1_BUILTINS,
942                                                   TExtension::EXT_shader_framebuffer_fetch,
943                                                   "gl_LastFragData", lastFragData);
944                 }
945                 else if (resources.NV_shader_framebuffer_fetch)
946                 {
947                     symbolTable.insertVariableExt(
948                         ESSL1_BUILTINS, TExtension::NV_shader_framebuffer_fetch, "gl_LastFragColor",
949                         TType(EbtFloat, EbpMedium, EvqLastFragColor, 4));
950                     symbolTable.insertVariableExt(ESSL1_BUILTINS,
951                                                   TExtension::NV_shader_framebuffer_fetch,
952                                                   "gl_LastFragData", lastFragData);
953                 }
954             }
955             else if (resources.ARM_shader_framebuffer_fetch)
956             {
957                 symbolTable.insertVariableExt(
958                     ESSL1_BUILTINS, TExtension::ARM_shader_framebuffer_fetch, "gl_LastFragColorARM",
959                     TType(EbtFloat, EbpMedium, EvqLastFragColor, 4));
960             }
961 
962             if (resources.OES_geometry_shader)
963             {
964                 TExtension extension = TExtension::OES_geometry_shader;
965                 symbolTable.insertVariableExt(ESSL3_1_BUILTINS, extension, "gl_PrimitiveID",
966                                               TType(EbtInt, EbpHigh, EvqPrimitiveID, 1));
967                 symbolTable.insertVariableExt(ESSL3_1_BUILTINS, extension, "gl_Layer",
968                                               TType(EbtInt, EbpHigh, EvqLayer, 1));
969             }
970 
971             break;
972         }
973         case GL_VERTEX_SHADER:
974         {
975             symbolTable.insertVariable(COMMON_BUILTINS, "gl_Position",
976                                        TType(EbtFloat, EbpHigh, EvqPosition, 4));
977             symbolTable.insertVariable(COMMON_BUILTINS, "gl_PointSize",
978                                        TType(EbtFloat, EbpMedium, EvqPointSize, 1));
979             symbolTable.insertVariable(ESSL3_BUILTINS, "gl_InstanceID",
980                                        TType(EbtInt, EbpHigh, EvqInstanceID, 1));
981             symbolTable.insertVariable(ESSL3_BUILTINS, "gl_VertexID",
982                                        TType(EbtInt, EbpHigh, EvqVertexID, 1));
983 
984             // For internal use by ANGLE - not exposed to the parser.
985             symbolTable.insertVariable(GLSL_BUILTINS, "gl_ViewportIndex",
986                                        TType(EbtInt, EbpHigh, EvqViewportIndex));
987             // gl_Layer exists in other shader stages in ESSL, but not in vertex shader so far.
988             symbolTable.insertVariable(GLSL_BUILTINS, "gl_Layer", TType(EbtInt, EbpHigh, EvqLayer));
989             break;
990         }
991         case GL_COMPUTE_SHADER:
992         {
993             symbolTable.insertVariable(ESSL3_1_BUILTINS, "gl_NumWorkGroups",
994                                        TType(EbtUInt, EbpUndefined, EvqNumWorkGroups, 3));
995             symbolTable.insertVariable(ESSL3_1_BUILTINS, "gl_WorkGroupSize",
996                                        TType(EbtUInt, EbpUndefined, EvqWorkGroupSize, 3));
997             symbolTable.insertVariable(ESSL3_1_BUILTINS, "gl_WorkGroupID",
998                                        TType(EbtUInt, EbpUndefined, EvqWorkGroupID, 3));
999             symbolTable.insertVariable(ESSL3_1_BUILTINS, "gl_LocalInvocationID",
1000                                        TType(EbtUInt, EbpUndefined, EvqLocalInvocationID, 3));
1001             symbolTable.insertVariable(ESSL3_1_BUILTINS, "gl_GlobalInvocationID",
1002                                        TType(EbtUInt, EbpUndefined, EvqGlobalInvocationID, 3));
1003             symbolTable.insertVariable(ESSL3_1_BUILTINS, "gl_LocalInvocationIndex",
1004                                        TType(EbtUInt, EbpUndefined, EvqLocalInvocationIndex, 1));
1005             break;
1006         }
1007 
1008         case GL_GEOMETRY_SHADER_OES:
1009         {
1010             TExtension extension = TExtension::OES_geometry_shader;
1011 
1012             // Add built-in interface block gl_PerVertex and the built-in array gl_in.
1013             // TODO(jiawei.shao@intel.com): implement GL_OES_geometry_point_size.
1014             const TString *glPerVertexString = NewPoolTString("gl_PerVertex");
1015             symbolTable.insertInterfaceBlockNameExt(ESSL3_1_BUILTINS, extension, glPerVertexString);
1016 
1017             TFieldList *fieldList    = NewPoolTFieldList();
1018             TSourceLoc zeroSourceLoc = {0, 0, 0, 0};
1019             TField *glPositionField  = new TField(new TType(EbtFloat, EbpHigh, EvqPosition, 4),
1020                                                  NewPoolTString("gl_Position"), zeroSourceLoc);
1021             fieldList->push_back(glPositionField);
1022 
1023             TInterfaceBlock *glInBlock = new TInterfaceBlock(
1024                 glPerVertexString, fieldList, NewPoolTString("gl_in"), TLayoutQualifier::Create());
1025 
1026             // The array size of gl_in is undefined until we get a valid input primitive
1027             // declaration.
1028             TType glInType(glInBlock, EvqPerVertexIn, TLayoutQualifier::Create());
1029             glInType.makeArray(0u);
1030             symbolTable.insertVariableExt(ESSL3_1_BUILTINS, extension, "gl_in", glInType);
1031 
1032             TType glPositionType(EbtFloat, EbpHigh, EvqPosition, 4);
1033             glPositionType.setInterfaceBlock(new TInterfaceBlock(
1034                 glPerVertexString, fieldList, nullptr, TLayoutQualifier::Create()));
1035             symbolTable.insertVariableExt(ESSL3_1_BUILTINS, extension, "gl_Position",
1036                                           glPositionType);
1037             symbolTable.insertVariableExt(ESSL3_1_BUILTINS, extension, "gl_PrimitiveIDIn",
1038                                           TType(EbtInt, EbpHigh, EvqPrimitiveIDIn, 1));
1039             symbolTable.insertVariableExt(ESSL3_1_BUILTINS, extension, "gl_InvocationID",
1040                                           TType(EbtInt, EbpHigh, EvqInvocationID, 1));
1041             symbolTable.insertVariableExt(ESSL3_1_BUILTINS, extension, "gl_PrimitiveID",
1042                                           TType(EbtInt, EbpHigh, EvqPrimitiveID, 1));
1043             symbolTable.insertVariableExt(ESSL3_1_BUILTINS, extension, "gl_Layer",
1044                                           TType(EbtInt, EbpHigh, EvqLayer, 1));
1045 
1046             break;
1047         }
1048         default:
1049             UNREACHABLE();
1050     }
1051 }
1052 
InitExtensionBehavior(const ShBuiltInResources & resources,TExtensionBehavior & extBehavior)1053 void InitExtensionBehavior(const ShBuiltInResources &resources, TExtensionBehavior &extBehavior)
1054 {
1055     if (resources.OES_standard_derivatives)
1056     {
1057         extBehavior[TExtension::OES_standard_derivatives] = EBhUndefined;
1058     }
1059     if (resources.OES_EGL_image_external)
1060     {
1061         extBehavior[TExtension::OES_EGL_image_external] = EBhUndefined;
1062     }
1063     if (resources.OES_EGL_image_external_essl3)
1064     {
1065         extBehavior[TExtension::OES_EGL_image_external_essl3] = EBhUndefined;
1066     }
1067     if (resources.NV_EGL_stream_consumer_external)
1068     {
1069         extBehavior[TExtension::NV_EGL_stream_consumer_external] = EBhUndefined;
1070     }
1071     if (resources.ARB_texture_rectangle)
1072     {
1073         // Special: ARB_texture_rectangle extension does not follow the standard for #extension
1074         // directives - it is enabled by default. An extension directive may still disable it.
1075         extBehavior[TExtension::ARB_texture_rectangle] = EBhEnable;
1076     }
1077     if (resources.EXT_blend_func_extended)
1078     {
1079         extBehavior[TExtension::EXT_blend_func_extended] = EBhUndefined;
1080     }
1081     if (resources.EXT_draw_buffers)
1082     {
1083         extBehavior[TExtension::EXT_draw_buffers] = EBhUndefined;
1084     }
1085     if (resources.EXT_frag_depth)
1086     {
1087         extBehavior[TExtension::EXT_frag_depth] = EBhUndefined;
1088     }
1089     if (resources.EXT_shader_texture_lod)
1090     {
1091         extBehavior[TExtension::EXT_shader_texture_lod] = EBhUndefined;
1092     }
1093     if (resources.EXT_shader_framebuffer_fetch)
1094     {
1095         extBehavior[TExtension::EXT_shader_framebuffer_fetch] = EBhUndefined;
1096     }
1097     if (resources.NV_shader_framebuffer_fetch)
1098     {
1099         extBehavior[TExtension::NV_shader_framebuffer_fetch] = EBhUndefined;
1100     }
1101     if (resources.ARM_shader_framebuffer_fetch)
1102     {
1103         extBehavior[TExtension::ARM_shader_framebuffer_fetch] = EBhUndefined;
1104     }
1105     if (resources.OVR_multiview)
1106     {
1107         extBehavior[TExtension::OVR_multiview] = EBhUndefined;
1108     }
1109     if (resources.EXT_YUV_target)
1110     {
1111         extBehavior[TExtension::EXT_YUV_target] = EBhUndefined;
1112     }
1113     if (resources.OES_geometry_shader)
1114     {
1115         extBehavior[TExtension::OES_geometry_shader] = EBhUndefined;
1116         extBehavior[TExtension::EXT_geometry_shader] = EBhUndefined;
1117     }
1118 }
1119 
ResetExtensionBehavior(TExtensionBehavior & extBehavior)1120 void ResetExtensionBehavior(TExtensionBehavior &extBehavior)
1121 {
1122     for (auto &ext : extBehavior)
1123     {
1124         if (ext.first == TExtension::ARB_texture_rectangle)
1125         {
1126             ext.second = EBhEnable;
1127         }
1128         else
1129         {
1130             ext.second = EBhUndefined;
1131         }
1132     }
1133 }
1134 
1135 }  // namespace sh
1136