1 /*
2 * Copyright © 2013 Intel Corporation
3 *
4 * Permission is hereby granted, free of charge, to any person obtaining a
5 * copy of this software and associated documentation files (the "Software"),
6 * to deal in the Software without restriction, including without limitation
7 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
8 * and/or sell copies of the Software, and to permit persons to whom the
9 * Software is furnished to do so, subject to the following conditions:
10 *
11 * The above copyright notice and this permission notice (including the next
12 * paragraph) shall be included in all copies or substantial portions of the
13 * Software.
14 *
15 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
18 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
20 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
21 * DEALINGS IN THE SOFTWARE.
22 */
23
24 /**
25 * \file builtin_types.cpp
26 *
27 * This contains _mesa_glsl_initialize_types(), a function which populates
28 * a symbol table with the available built-in types for a particular language
29 * version and set of enabled extensions.
30 */
31
32 #include "compiler/glsl_types.h"
33 #include "glsl_parser_extras.h"
34 #include "util/macros.h"
35 #include "main/consts_exts.h"
36
37 static const struct glsl_struct_field gl_DepthRangeParameters_fields[] = {
38 glsl_struct_field(&glsl_type_builtin_float, GLSL_PRECISION_HIGH, "near"),
39 glsl_struct_field(&glsl_type_builtin_float, GLSL_PRECISION_HIGH, "far"),
40 glsl_struct_field(&glsl_type_builtin_float, GLSL_PRECISION_HIGH, "diff"),
41 };
42
43 static const struct glsl_struct_field gl_PointParameters_fields[] = {
44 glsl_struct_field(&glsl_type_builtin_float, "size"),
45 glsl_struct_field(&glsl_type_builtin_float, "sizeMin"),
46 glsl_struct_field(&glsl_type_builtin_float, "sizeMax"),
47 glsl_struct_field(&glsl_type_builtin_float, "fadeThresholdSize"),
48 glsl_struct_field(&glsl_type_builtin_float, "distanceConstantAttenuation"),
49 glsl_struct_field(&glsl_type_builtin_float, "distanceLinearAttenuation"),
50 glsl_struct_field(&glsl_type_builtin_float, "distanceQuadraticAttenuation"),
51 };
52
53 static const struct glsl_struct_field gl_MaterialParameters_fields[] = {
54 glsl_struct_field(&glsl_type_builtin_vec4, "emission"),
55 glsl_struct_field(&glsl_type_builtin_vec4, "ambient"),
56 glsl_struct_field(&glsl_type_builtin_vec4, "diffuse"),
57 glsl_struct_field(&glsl_type_builtin_vec4, "specular"),
58 glsl_struct_field(&glsl_type_builtin_float, "shininess"),
59 };
60
61 static const struct glsl_struct_field gl_LightSourceParameters_fields[] = {
62 glsl_struct_field(&glsl_type_builtin_vec4, "ambient"),
63 glsl_struct_field(&glsl_type_builtin_vec4, "diffuse"),
64 glsl_struct_field(&glsl_type_builtin_vec4, "specular"),
65 glsl_struct_field(&glsl_type_builtin_vec4, "position"),
66 glsl_struct_field(&glsl_type_builtin_vec4, "halfVector"),
67 glsl_struct_field(&glsl_type_builtin_vec3, "spotDirection"),
68 glsl_struct_field(&glsl_type_builtin_float, "spotCosCutoff"),
69 glsl_struct_field(&glsl_type_builtin_float, "constantAttenuation"),
70 glsl_struct_field(&glsl_type_builtin_float, "linearAttenuation"),
71 glsl_struct_field(&glsl_type_builtin_float, "quadraticAttenuation"),
72 glsl_struct_field(&glsl_type_builtin_float, "spotExponent"),
73 glsl_struct_field(&glsl_type_builtin_float, "spotCutoff"),
74 };
75
76 static const struct glsl_struct_field gl_LightModelParameters_fields[] = {
77 glsl_struct_field(&glsl_type_builtin_vec4, "ambient"),
78 };
79
80 static const struct glsl_struct_field gl_LightModelProducts_fields[] = {
81 glsl_struct_field(&glsl_type_builtin_vec4, "sceneColor"),
82 };
83
84 static const struct glsl_struct_field gl_LightProducts_fields[] = {
85 glsl_struct_field(&glsl_type_builtin_vec4, "ambient"),
86 glsl_struct_field(&glsl_type_builtin_vec4, "diffuse"),
87 glsl_struct_field(&glsl_type_builtin_vec4, "specular"),
88 };
89
90 static const struct glsl_struct_field gl_FogParameters_fields[] = {
91 glsl_struct_field(&glsl_type_builtin_vec4, "color"),
92 glsl_struct_field(&glsl_type_builtin_float, "density"),
93 glsl_struct_field(&glsl_type_builtin_float, "start"),
94 glsl_struct_field(&glsl_type_builtin_float, "end"),
95 glsl_struct_field(&glsl_type_builtin_float, "scale"),
96 };
97
98 /**
99 * Code to populate a symbol table with the built-in types available in a
100 * particular shading language version. The table below contains tags every
101 * type with the GLSL/GLSL ES versions where it was introduced.
102 *
103 * @{
104 */
105 #define T(TYPE, MIN_GL, MIN_ES) \
106 { &glsl_type_builtin_##TYPE, MIN_GL, MIN_ES },
107
108 static const struct builtin_type_versions {
109 const glsl_type *const type;
110 int min_gl;
111 int min_es;
112 } builtin_type_versions[] = {
113 T(void, 110, 100)
114 T(bool, 110, 100)
115 T(bvec2, 110, 100)
116 T(bvec3, 110, 100)
117 T(bvec4, 110, 100)
118 T(int, 110, 100)
119 T(ivec2, 110, 100)
120 T(ivec3, 110, 100)
121 T(ivec4, 110, 100)
122 T(uint, 130, 300)
123 T(uvec2, 130, 300)
124 T(uvec3, 130, 300)
125 T(uvec4, 130, 300)
126 T(float, 110, 100)
127 T(vec2, 110, 100)
128 T(vec3, 110, 100)
129 T(vec4, 110, 100)
130 T(mat2, 110, 100)
131 T(mat3, 110, 100)
132 T(mat4, 110, 100)
133 T(mat2x3, 120, 300)
134 T(mat2x4, 120, 300)
135 T(mat3x2, 120, 300)
136 T(mat3x4, 120, 300)
137 T(mat4x2, 120, 300)
138 T(mat4x3, 120, 300)
139
140 T(double, 400, 999)
141 T(dvec2, 400, 999)
142 T(dvec3, 400, 999)
143 T(dvec4, 400, 999)
144 T(dmat2, 400, 999)
145 T(dmat3, 400, 999)
146 T(dmat4, 400, 999)
147 T(dmat2x3, 400, 999)
148 T(dmat2x4, 400, 999)
149 T(dmat3x2, 400, 999)
150 T(dmat3x4, 400, 999)
151 T(dmat4x2, 400, 999)
152 T(dmat4x3, 400, 999)
153
154 T(sampler1D, 110, 999)
155 T(sampler2D, 110, 100)
156 T(sampler3D, 110, 300)
157 T(samplerCube, 110, 100)
158 T(sampler1DArray, 130, 999)
159 T(sampler2DArray, 130, 300)
160 T(samplerCubeArray, 400, 320)
161 T(sampler2DRect, 140, 999)
162 T(samplerBuffer, 140, 320)
163 T(sampler2DMS, 150, 310)
164 T(sampler2DMSArray, 150, 320)
165
166 T(isampler1D, 130, 999)
167 T(isampler2D, 130, 300)
168 T(isampler3D, 130, 300)
169 T(isamplerCube, 130, 300)
170 T(isampler1DArray, 130, 999)
171 T(isampler2DArray, 130, 300)
172 T(isamplerCubeArray, 400, 320)
173 T(isampler2DRect, 140, 999)
174 T(isamplerBuffer, 140, 320)
175 T(isampler2DMS, 150, 310)
176 T(isampler2DMSArray, 150, 320)
177
178 T(usampler1D, 130, 999)
179 T(usampler2D, 130, 300)
180 T(usampler3D, 130, 300)
181 T(usamplerCube, 130, 300)
182 T(usampler1DArray, 130, 999)
183 T(usampler2DArray, 130, 300)
184 T(usamplerCubeArray, 400, 320)
185 T(usampler2DRect, 140, 999)
186 T(usamplerBuffer, 140, 320)
187 T(usampler2DMS, 150, 310)
188 T(usampler2DMSArray, 150, 320)
189
190 T(sampler1DShadow, 110, 999)
191 T(sampler2DShadow, 110, 300)
192 T(samplerCubeShadow, 130, 300)
193 T(sampler1DArrayShadow, 130, 999)
194 T(sampler2DArrayShadow, 130, 300)
195 T(samplerCubeArrayShadow, 400, 320)
196 T(sampler2DRectShadow, 140, 999)
197
198 T(image1D, 420, 999)
199 T(image2D, 420, 310)
200 T(image3D, 420, 310)
201 T(image2DRect, 420, 999)
202 T(imageCube, 420, 310)
203 T(imageBuffer, 420, 320)
204 T(image1DArray, 420, 999)
205 T(image2DArray, 420, 310)
206 T(imageCubeArray, 420, 320)
207 T(image2DMS, 420, 999)
208 T(image2DMSArray, 420, 999)
209 T(iimage1D, 420, 999)
210 T(iimage2D, 420, 310)
211 T(iimage3D, 420, 310)
212 T(iimage2DRect, 420, 999)
213 T(iimageCube, 420, 310)
214 T(iimageBuffer, 420, 320)
215 T(iimage1DArray, 420, 999)
216 T(iimage2DArray, 420, 310)
217 T(iimageCubeArray, 420, 320)
218 T(iimage2DMS, 420, 999)
219 T(iimage2DMSArray, 420, 999)
220 T(uimage1D, 420, 999)
221 T(uimage2D, 420, 310)
222 T(uimage3D, 420, 310)
223 T(uimage2DRect, 420, 999)
224 T(uimageCube, 420, 310)
225 T(uimageBuffer, 420, 320)
226 T(uimage1DArray, 420, 999)
227 T(uimage2DArray, 420, 310)
228 T(uimageCubeArray, 420, 320)
229 T(uimage2DMS, 420, 999)
230 T(uimage2DMSArray, 420, 999)
231
232 T(atomic_uint, 420, 310)
233 };
234
235 #undef T
236
237 static inline void
add_type(glsl_symbol_table * symbols,const glsl_type * const type)238 add_type(glsl_symbol_table *symbols, const glsl_type *const type)
239 {
240 symbols->add_type(glsl_get_type_name(type), type);
241 }
242
243 /**
244 * Populate the symbol table with available built-in types.
245 */
246 void
_mesa_glsl_initialize_types(struct _mesa_glsl_parse_state * state)247 _mesa_glsl_initialize_types(struct _mesa_glsl_parse_state *state)
248 {
249 struct glsl_symbol_table *symbols = state->symbols;
250
251 for (unsigned i = 0; i < ARRAY_SIZE(builtin_type_versions); i++) {
252 const struct builtin_type_versions *const t = &builtin_type_versions[i];
253 if (state->is_version(t->min_gl, t->min_es)) {
254 add_type(symbols, t->type);
255 }
256 }
257
258 /* Note: use glsl_type::get_struct_instance() to get the properly cached
259 * copy of the struct types.
260 */
261 {
262 #define GET_STRUCT_TYPE(NAME) glsl_struct_type(NAME##_fields, ARRAY_SIZE(NAME##_fields), #NAME, false /* packed */)
263
264 if (state->is_version(110, 100)) {
265 add_type(symbols, GET_STRUCT_TYPE(gl_DepthRangeParameters));
266 }
267
268 /* Add deprecated structure types. While these were deprecated in 1.30,
269 * they're still present. We've removed them in 1.40+ (OpenGL 3.1+).
270 */
271 if (state->compat_shader || state->ARB_compatibility_enable) {
272 add_type(symbols, GET_STRUCT_TYPE(gl_PointParameters));
273 add_type(symbols, GET_STRUCT_TYPE(gl_MaterialParameters));
274 add_type(symbols, GET_STRUCT_TYPE(gl_LightSourceParameters));
275 add_type(symbols, GET_STRUCT_TYPE(gl_LightModelParameters));
276 add_type(symbols, GET_STRUCT_TYPE(gl_LightModelProducts));
277 add_type(symbols, GET_STRUCT_TYPE(gl_LightProducts));
278 add_type(symbols, GET_STRUCT_TYPE(gl_FogParameters));
279 };
280
281 #undef GET_STRUCT_TYPE
282 }
283
284 /* Add types for enabled extensions. They may have already been added
285 * by the version-based loop, but attempting to add them a second time
286 * is harmless.
287 */
288 if (state->ARB_texture_cube_map_array_enable ||
289 state->EXT_texture_cube_map_array_enable ||
290 state->OES_texture_cube_map_array_enable) {
291 add_type(symbols, &glsl_type_builtin_samplerCubeArray);
292 add_type(symbols, &glsl_type_builtin_samplerCubeArrayShadow);
293 add_type(symbols, &glsl_type_builtin_isamplerCubeArray);
294 add_type(symbols, &glsl_type_builtin_usamplerCubeArray);
295 }
296
297 if (state->ARB_texture_multisample_enable) {
298 add_type(symbols, &glsl_type_builtin_sampler2DMS);
299 add_type(symbols, &glsl_type_builtin_isampler2DMS);
300 add_type(symbols, &glsl_type_builtin_usampler2DMS);
301 }
302 if (state->ARB_texture_multisample_enable ||
303 state->OES_texture_storage_multisample_2d_array_enable) {
304 add_type(symbols, &glsl_type_builtin_sampler2DMSArray);
305 add_type(symbols, &glsl_type_builtin_isampler2DMSArray);
306 add_type(symbols, &glsl_type_builtin_usampler2DMSArray);
307 }
308
309 if (state->ARB_texture_rectangle_enable) {
310 add_type(symbols, &glsl_type_builtin_sampler2DRect);
311 add_type(symbols, &glsl_type_builtin_sampler2DRectShadow);
312 }
313
314 if (state->EXT_gpu_shader4_enable) {
315 add_type(symbols, &glsl_type_builtin_uint);
316 add_type(symbols, &glsl_type_builtin_uvec2);
317 add_type(symbols, &glsl_type_builtin_uvec3);
318 add_type(symbols, &glsl_type_builtin_uvec4);
319
320 add_type(symbols, &glsl_type_builtin_samplerCubeShadow);
321
322 if (state->exts->EXT_texture_array) {
323 add_type(symbols, &glsl_type_builtin_sampler1DArray);
324 add_type(symbols, &glsl_type_builtin_sampler2DArray);
325 add_type(symbols, &glsl_type_builtin_sampler1DArrayShadow);
326 add_type(symbols, &glsl_type_builtin_sampler2DArrayShadow);
327 }
328 if (state->exts->EXT_texture_buffer_object) {
329 add_type(symbols, &glsl_type_builtin_samplerBuffer);
330 }
331
332 if (state->exts->EXT_texture_integer) {
333 add_type(symbols, &glsl_type_builtin_isampler1D);
334 add_type(symbols, &glsl_type_builtin_isampler2D);
335 add_type(symbols, &glsl_type_builtin_isampler3D);
336 add_type(symbols, &glsl_type_builtin_isamplerCube);
337
338 add_type(symbols, &glsl_type_builtin_usampler1D);
339 add_type(symbols, &glsl_type_builtin_usampler2D);
340 add_type(symbols, &glsl_type_builtin_usampler3D);
341 add_type(symbols, &glsl_type_builtin_usamplerCube);
342
343 if (state->exts->NV_texture_rectangle) {
344 add_type(symbols, &glsl_type_builtin_isampler2DRect);
345 add_type(symbols, &glsl_type_builtin_usampler2DRect);
346 }
347 if (state->exts->EXT_texture_array) {
348 add_type(symbols, &glsl_type_builtin_isampler1DArray);
349 add_type(symbols, &glsl_type_builtin_isampler2DArray);
350 add_type(symbols, &glsl_type_builtin_usampler1DArray);
351 add_type(symbols, &glsl_type_builtin_usampler2DArray);
352 }
353 if (state->exts->EXT_texture_buffer_object) {
354 add_type(symbols, &glsl_type_builtin_isamplerBuffer);
355 add_type(symbols, &glsl_type_builtin_usamplerBuffer);
356 }
357 }
358 }
359
360 if (state->EXT_texture_array_enable) {
361 add_type(symbols, &glsl_type_builtin_sampler1DArray);
362 add_type(symbols, &glsl_type_builtin_sampler2DArray);
363 add_type(symbols, &glsl_type_builtin_sampler1DArrayShadow);
364 add_type(symbols, &glsl_type_builtin_sampler2DArrayShadow);
365 }
366
367 if (state->OES_EGL_image_external_enable ||
368 state->OES_EGL_image_external_essl3_enable) {
369 add_type(symbols, &glsl_type_builtin_samplerExternalOES);
370 }
371
372 if (state->OES_texture_3D_enable) {
373 add_type(symbols, &glsl_type_builtin_sampler3D);
374 }
375
376 if (state->ARB_shader_image_load_store_enable ||
377 state->EXT_texture_cube_map_array_enable ||
378 state->OES_texture_cube_map_array_enable) {
379 add_type(symbols, &glsl_type_builtin_imageCubeArray);
380 add_type(symbols, &glsl_type_builtin_iimageCubeArray);
381 add_type(symbols, &glsl_type_builtin_uimageCubeArray);
382 }
383
384 if (state->ARB_shader_image_load_store_enable) {
385 add_type(symbols, &glsl_type_builtin_image1D);
386 add_type(symbols, &glsl_type_builtin_image2D);
387 add_type(symbols, &glsl_type_builtin_image3D);
388 add_type(symbols, &glsl_type_builtin_image2DRect);
389 add_type(symbols, &glsl_type_builtin_imageCube);
390 add_type(symbols, &glsl_type_builtin_imageBuffer);
391 add_type(symbols, &glsl_type_builtin_image1DArray);
392 add_type(symbols, &glsl_type_builtin_image2DArray);
393 add_type(symbols, &glsl_type_builtin_image2DMS);
394 add_type(symbols, &glsl_type_builtin_image2DMSArray);
395 add_type(symbols, &glsl_type_builtin_iimage1D);
396 add_type(symbols, &glsl_type_builtin_iimage2D);
397 add_type(symbols, &glsl_type_builtin_iimage3D);
398 add_type(symbols, &glsl_type_builtin_iimage2DRect);
399 add_type(symbols, &glsl_type_builtin_iimageCube);
400 add_type(symbols, &glsl_type_builtin_iimageBuffer);
401 add_type(symbols, &glsl_type_builtin_iimage1DArray);
402 add_type(symbols, &glsl_type_builtin_iimage2DArray);
403 add_type(symbols, &glsl_type_builtin_iimage2DMS);
404 add_type(symbols, &glsl_type_builtin_iimage2DMSArray);
405 add_type(symbols, &glsl_type_builtin_uimage1D);
406 add_type(symbols, &glsl_type_builtin_uimage2D);
407 add_type(symbols, &glsl_type_builtin_uimage3D);
408 add_type(symbols, &glsl_type_builtin_uimage2DRect);
409 add_type(symbols, &glsl_type_builtin_uimageCube);
410 add_type(symbols, &glsl_type_builtin_uimageBuffer);
411 add_type(symbols, &glsl_type_builtin_uimage1DArray);
412 add_type(symbols, &glsl_type_builtin_uimage2DArray);
413 add_type(symbols, &glsl_type_builtin_uimage2DMS);
414 add_type(symbols, &glsl_type_builtin_uimage2DMSArray);
415 }
416
417 if (state->EXT_texture_buffer_enable || state->OES_texture_buffer_enable) {
418 add_type(symbols, &glsl_type_builtin_samplerBuffer);
419 add_type(symbols, &glsl_type_builtin_isamplerBuffer);
420 add_type(symbols, &glsl_type_builtin_usamplerBuffer);
421
422 add_type(symbols, &glsl_type_builtin_imageBuffer);
423 add_type(symbols, &glsl_type_builtin_iimageBuffer);
424 add_type(symbols, &glsl_type_builtin_uimageBuffer);
425 }
426
427 if (state->has_atomic_counters()) {
428 add_type(symbols, &glsl_type_builtin_atomic_uint);
429 }
430
431 if (state->ARB_gpu_shader_fp64_enable) {
432 add_type(symbols, &glsl_type_builtin_double);
433 add_type(symbols, &glsl_type_builtin_dvec2);
434 add_type(symbols, &glsl_type_builtin_dvec3);
435 add_type(symbols, &glsl_type_builtin_dvec4);
436 add_type(symbols, &glsl_type_builtin_dmat2);
437 add_type(symbols, &glsl_type_builtin_dmat3);
438 add_type(symbols, &glsl_type_builtin_dmat4);
439 add_type(symbols, &glsl_type_builtin_dmat2x3);
440 add_type(symbols, &glsl_type_builtin_dmat2x4);
441 add_type(symbols, &glsl_type_builtin_dmat3x2);
442 add_type(symbols, &glsl_type_builtin_dmat3x4);
443 add_type(symbols, &glsl_type_builtin_dmat4x2);
444 add_type(symbols, &glsl_type_builtin_dmat4x3);
445 }
446
447 if (state->ARB_gpu_shader_int64_enable ||
448 state->AMD_gpu_shader_int64_enable) {
449 add_type(symbols, &glsl_type_builtin_int64_t);
450 add_type(symbols, &glsl_type_builtin_i64vec2);
451 add_type(symbols, &glsl_type_builtin_i64vec3);
452 add_type(symbols, &glsl_type_builtin_i64vec4);
453
454 add_type(symbols, &glsl_type_builtin_uint64_t);
455 add_type(symbols, &glsl_type_builtin_u64vec2);
456 add_type(symbols, &glsl_type_builtin_u64vec3);
457 add_type(symbols, &glsl_type_builtin_u64vec4);
458 }
459 }
460 /** @} */
461