builtin_variables.cpp revision 7e102996
1/* 2 * Copyright © 2010 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/** 26 * Building this file with MinGW g++ 7.3 or 7.4 with: 27 * scons platform=windows toolchain=crossmingw machine=x86 build=profile 28 * triggers an internal compiler error. 29 * Overriding the optimization level to -O1 works around the issue. 30 * MinGW 5.3.1 does not seem to have the bug, neither does 8.3. So for now 31 * we're simply testing for version 7.x here. 32 */ 33#if defined(__MINGW32__) && __GNUC__ == 7 34#warning "disabling optimizations for this file to work around compiler bug in MinGW gcc 7.x" 35#pragma GCC optimize("O1") 36#endif 37 38 39#include "ir.h" 40#include "ir_builder.h" 41#include "linker.h" 42#include "glsl_parser_extras.h" 43#include "glsl_symbol_table.h" 44#include "main/mtypes.h" 45#include "main/uniforms.h" 46#include "program/prog_statevars.h" 47#include "program/prog_instruction.h" 48#include "builtin_functions.h" 49 50using namespace ir_builder; 51 52static const struct gl_builtin_uniform_element gl_NumSamples_elements[] = { 53 {NULL, {STATE_NUM_SAMPLES, 0, 0}, SWIZZLE_XXXX} 54}; 55 56static const struct gl_builtin_uniform_element gl_DepthRange_elements[] = { 57 {"near", {STATE_DEPTH_RANGE, 0, 0}, SWIZZLE_XXXX}, 58 {"far", {STATE_DEPTH_RANGE, 0, 0}, SWIZZLE_YYYY}, 59 {"diff", {STATE_DEPTH_RANGE, 0, 0}, SWIZZLE_ZZZZ}, 60}; 61 62static const struct gl_builtin_uniform_element gl_ClipPlane_elements[] = { 63 {NULL, {STATE_CLIPPLANE, 0, 0}, SWIZZLE_XYZW} 64}; 65 66static const struct gl_builtin_uniform_element gl_Point_elements[] = { 67 {"size", {STATE_POINT_SIZE}, SWIZZLE_XXXX}, 68 {"sizeMin", {STATE_POINT_SIZE}, SWIZZLE_YYYY}, 69 {"sizeMax", {STATE_POINT_SIZE}, SWIZZLE_ZZZZ}, 70 {"fadeThresholdSize", {STATE_POINT_SIZE}, SWIZZLE_WWWW}, 71 {"distanceConstantAttenuation", {STATE_POINT_ATTENUATION}, SWIZZLE_XXXX}, 72 {"distanceLinearAttenuation", {STATE_POINT_ATTENUATION}, SWIZZLE_YYYY}, 73 {"distanceQuadraticAttenuation", {STATE_POINT_ATTENUATION}, SWIZZLE_ZZZZ}, 74}; 75 76static const struct gl_builtin_uniform_element gl_FrontMaterial_elements[] = { 77 {"emission", {STATE_MATERIAL, 0, STATE_EMISSION}, SWIZZLE_XYZW}, 78 {"ambient", {STATE_MATERIAL, 0, STATE_AMBIENT}, SWIZZLE_XYZW}, 79 {"diffuse", {STATE_MATERIAL, 0, STATE_DIFFUSE}, SWIZZLE_XYZW}, 80 {"specular", {STATE_MATERIAL, 0, STATE_SPECULAR}, SWIZZLE_XYZW}, 81 {"shininess", {STATE_MATERIAL, 0, STATE_SHININESS}, SWIZZLE_XXXX}, 82}; 83 84static const struct gl_builtin_uniform_element gl_BackMaterial_elements[] = { 85 {"emission", {STATE_MATERIAL, 1, STATE_EMISSION}, SWIZZLE_XYZW}, 86 {"ambient", {STATE_MATERIAL, 1, STATE_AMBIENT}, SWIZZLE_XYZW}, 87 {"diffuse", {STATE_MATERIAL, 1, STATE_DIFFUSE}, SWIZZLE_XYZW}, 88 {"specular", {STATE_MATERIAL, 1, STATE_SPECULAR}, SWIZZLE_XYZW}, 89 {"shininess", {STATE_MATERIAL, 1, STATE_SHININESS}, SWIZZLE_XXXX}, 90}; 91 92static const struct gl_builtin_uniform_element gl_LightSource_elements[] = { 93 {"ambient", {STATE_LIGHT, 0, STATE_AMBIENT}, SWIZZLE_XYZW}, 94 {"diffuse", {STATE_LIGHT, 0, STATE_DIFFUSE}, SWIZZLE_XYZW}, 95 {"specular", {STATE_LIGHT, 0, STATE_SPECULAR}, SWIZZLE_XYZW}, 96 {"position", {STATE_LIGHT, 0, STATE_POSITION}, SWIZZLE_XYZW}, 97 {"halfVector", {STATE_LIGHT, 0, STATE_HALF_VECTOR}, SWIZZLE_XYZW}, 98 {"spotDirection", {STATE_LIGHT, 0, STATE_SPOT_DIRECTION}, 99 MAKE_SWIZZLE4(SWIZZLE_X, 100 SWIZZLE_Y, 101 SWIZZLE_Z, 102 SWIZZLE_Z)}, 103 {"spotExponent", {STATE_LIGHT, 0, STATE_ATTENUATION}, SWIZZLE_WWWW}, 104 {"spotCutoff", {STATE_LIGHT, 0, STATE_SPOT_CUTOFF}, SWIZZLE_XXXX}, 105 {"spotCosCutoff", {STATE_LIGHT, 0, STATE_SPOT_DIRECTION}, SWIZZLE_WWWW}, 106 {"constantAttenuation", {STATE_LIGHT, 0, STATE_ATTENUATION}, SWIZZLE_XXXX}, 107 {"linearAttenuation", {STATE_LIGHT, 0, STATE_ATTENUATION}, SWIZZLE_YYYY}, 108 {"quadraticAttenuation", {STATE_LIGHT, 0, STATE_ATTENUATION}, SWIZZLE_ZZZZ}, 109}; 110 111static const struct gl_builtin_uniform_element gl_LightModel_elements[] = { 112 {"ambient", {STATE_LIGHTMODEL_AMBIENT, 0}, SWIZZLE_XYZW}, 113}; 114 115static const struct gl_builtin_uniform_element gl_FrontLightModelProduct_elements[] = { 116 {"sceneColor", {STATE_LIGHTMODEL_SCENECOLOR, 0}, SWIZZLE_XYZW}, 117}; 118 119static const struct gl_builtin_uniform_element gl_BackLightModelProduct_elements[] = { 120 {"sceneColor", {STATE_LIGHTMODEL_SCENECOLOR, 1}, SWIZZLE_XYZW}, 121}; 122 123static const struct gl_builtin_uniform_element gl_FrontLightProduct_elements[] = { 124 {"ambient", {STATE_LIGHTPROD, 0, 0, STATE_AMBIENT}, SWIZZLE_XYZW}, 125 {"diffuse", {STATE_LIGHTPROD, 0, 0, STATE_DIFFUSE}, SWIZZLE_XYZW}, 126 {"specular", {STATE_LIGHTPROD, 0, 0, STATE_SPECULAR}, SWIZZLE_XYZW}, 127}; 128 129static const struct gl_builtin_uniform_element gl_BackLightProduct_elements[] = { 130 {"ambient", {STATE_LIGHTPROD, 0, 1, STATE_AMBIENT}, SWIZZLE_XYZW}, 131 {"diffuse", {STATE_LIGHTPROD, 0, 1, STATE_DIFFUSE}, SWIZZLE_XYZW}, 132 {"specular", {STATE_LIGHTPROD, 0, 1, STATE_SPECULAR}, SWIZZLE_XYZW}, 133}; 134 135static const struct gl_builtin_uniform_element gl_TextureEnvColor_elements[] = { 136 {NULL, {STATE_TEXENV_COLOR, 0}, SWIZZLE_XYZW}, 137}; 138 139static const struct gl_builtin_uniform_element gl_EyePlaneS_elements[] = { 140 {NULL, {STATE_TEXGEN, 0, STATE_TEXGEN_EYE_S}, SWIZZLE_XYZW}, 141}; 142 143static const struct gl_builtin_uniform_element gl_EyePlaneT_elements[] = { 144 {NULL, {STATE_TEXGEN, 0, STATE_TEXGEN_EYE_T}, SWIZZLE_XYZW}, 145}; 146 147static const struct gl_builtin_uniform_element gl_EyePlaneR_elements[] = { 148 {NULL, {STATE_TEXGEN, 0, STATE_TEXGEN_EYE_R}, SWIZZLE_XYZW}, 149}; 150 151static const struct gl_builtin_uniform_element gl_EyePlaneQ_elements[] = { 152 {NULL, {STATE_TEXGEN, 0, STATE_TEXGEN_EYE_Q}, SWIZZLE_XYZW}, 153}; 154 155static const struct gl_builtin_uniform_element gl_ObjectPlaneS_elements[] = { 156 {NULL, {STATE_TEXGEN, 0, STATE_TEXGEN_OBJECT_S}, SWIZZLE_XYZW}, 157}; 158 159static const struct gl_builtin_uniform_element gl_ObjectPlaneT_elements[] = { 160 {NULL, {STATE_TEXGEN, 0, STATE_TEXGEN_OBJECT_T}, SWIZZLE_XYZW}, 161}; 162 163static const struct gl_builtin_uniform_element gl_ObjectPlaneR_elements[] = { 164 {NULL, {STATE_TEXGEN, 0, STATE_TEXGEN_OBJECT_R}, SWIZZLE_XYZW}, 165}; 166 167static const struct gl_builtin_uniform_element gl_ObjectPlaneQ_elements[] = { 168 {NULL, {STATE_TEXGEN, 0, STATE_TEXGEN_OBJECT_Q}, SWIZZLE_XYZW}, 169}; 170 171static const struct gl_builtin_uniform_element gl_Fog_elements[] = { 172 {"color", {STATE_FOG_COLOR}, SWIZZLE_XYZW}, 173 {"density", {STATE_FOG_PARAMS}, SWIZZLE_XXXX}, 174 {"start", {STATE_FOG_PARAMS}, SWIZZLE_YYYY}, 175 {"end", {STATE_FOG_PARAMS}, SWIZZLE_ZZZZ}, 176 {"scale", {STATE_FOG_PARAMS}, SWIZZLE_WWWW}, 177}; 178 179static const struct gl_builtin_uniform_element gl_NormalScale_elements[] = { 180 {NULL, {STATE_NORMAL_SCALE}, SWIZZLE_XXXX}, 181}; 182 183static const struct gl_builtin_uniform_element gl_FogParamsOptimizedMESA_elements[] = { 184 {NULL, {STATE_INTERNAL, STATE_FOG_PARAMS_OPTIMIZED}, SWIZZLE_XYZW}, 185}; 186 187static const struct gl_builtin_uniform_element gl_CurrentAttribVertMESA_elements[] = { 188 {NULL, {STATE_INTERNAL, STATE_CURRENT_ATTRIB, 0}, SWIZZLE_XYZW}, 189}; 190 191static const struct gl_builtin_uniform_element gl_CurrentAttribFragMESA_elements[] = { 192 {NULL, {STATE_INTERNAL, STATE_CURRENT_ATTRIB_MAYBE_VP_CLAMPED, 0}, SWIZZLE_XYZW}, 193}; 194 195#define MATRIX(name, statevar, modifier) \ 196 static const struct gl_builtin_uniform_element name ## _elements[] = { \ 197 { NULL, { statevar, 0, 0, 0, modifier}, SWIZZLE_XYZW }, \ 198 { NULL, { statevar, 0, 1, 1, modifier}, SWIZZLE_XYZW }, \ 199 { NULL, { statevar, 0, 2, 2, modifier}, SWIZZLE_XYZW }, \ 200 { NULL, { statevar, 0, 3, 3, modifier}, SWIZZLE_XYZW }, \ 201 } 202 203MATRIX(gl_ModelViewMatrix, 204 STATE_MODELVIEW_MATRIX, STATE_MATRIX_TRANSPOSE); 205MATRIX(gl_ModelViewMatrixInverse, 206 STATE_MODELVIEW_MATRIX, STATE_MATRIX_INVTRANS); 207MATRIX(gl_ModelViewMatrixTranspose, 208 STATE_MODELVIEW_MATRIX, 0); 209MATRIX(gl_ModelViewMatrixInverseTranspose, 210 STATE_MODELVIEW_MATRIX, STATE_MATRIX_INVERSE); 211 212MATRIX(gl_ProjectionMatrix, 213 STATE_PROJECTION_MATRIX, STATE_MATRIX_TRANSPOSE); 214MATRIX(gl_ProjectionMatrixInverse, 215 STATE_PROJECTION_MATRIX, STATE_MATRIX_INVTRANS); 216MATRIX(gl_ProjectionMatrixTranspose, 217 STATE_PROJECTION_MATRIX, 0); 218MATRIX(gl_ProjectionMatrixInverseTranspose, 219 STATE_PROJECTION_MATRIX, STATE_MATRIX_INVERSE); 220 221MATRIX(gl_ModelViewProjectionMatrix, 222 STATE_MVP_MATRIX, STATE_MATRIX_TRANSPOSE); 223MATRIX(gl_ModelViewProjectionMatrixInverse, 224 STATE_MVP_MATRIX, STATE_MATRIX_INVTRANS); 225MATRIX(gl_ModelViewProjectionMatrixTranspose, 226 STATE_MVP_MATRIX, 0); 227MATRIX(gl_ModelViewProjectionMatrixInverseTranspose, 228 STATE_MVP_MATRIX, STATE_MATRIX_INVERSE); 229 230MATRIX(gl_TextureMatrix, 231 STATE_TEXTURE_MATRIX, STATE_MATRIX_TRANSPOSE); 232MATRIX(gl_TextureMatrixInverse, 233 STATE_TEXTURE_MATRIX, STATE_MATRIX_INVTRANS); 234MATRIX(gl_TextureMatrixTranspose, 235 STATE_TEXTURE_MATRIX, 0); 236MATRIX(gl_TextureMatrixInverseTranspose, 237 STATE_TEXTURE_MATRIX, STATE_MATRIX_INVERSE); 238 239static const struct gl_builtin_uniform_element gl_NormalMatrix_elements[] = { 240 { NULL, { STATE_MODELVIEW_MATRIX, 0, 0, 0, STATE_MATRIX_INVERSE}, 241 MAKE_SWIZZLE4(SWIZZLE_X, SWIZZLE_Y, SWIZZLE_Z, SWIZZLE_Z) }, 242 { NULL, { STATE_MODELVIEW_MATRIX, 0, 1, 1, STATE_MATRIX_INVERSE}, 243 MAKE_SWIZZLE4(SWIZZLE_X, SWIZZLE_Y, SWIZZLE_Z, SWIZZLE_Z) }, 244 { NULL, { STATE_MODELVIEW_MATRIX, 0, 2, 2, STATE_MATRIX_INVERSE}, 245 MAKE_SWIZZLE4(SWIZZLE_X, SWIZZLE_Y, SWIZZLE_Z, SWIZZLE_Z) }, 246}; 247 248#undef MATRIX 249 250#define STATEVAR(name) {#name, name ## _elements, ARRAY_SIZE(name ## _elements)} 251 252static const struct gl_builtin_uniform_desc _mesa_builtin_uniform_desc[] = { 253 STATEVAR(gl_NumSamples), 254 STATEVAR(gl_DepthRange), 255 STATEVAR(gl_ClipPlane), 256 STATEVAR(gl_Point), 257 STATEVAR(gl_FrontMaterial), 258 STATEVAR(gl_BackMaterial), 259 STATEVAR(gl_LightSource), 260 STATEVAR(gl_LightModel), 261 STATEVAR(gl_FrontLightModelProduct), 262 STATEVAR(gl_BackLightModelProduct), 263 STATEVAR(gl_FrontLightProduct), 264 STATEVAR(gl_BackLightProduct), 265 STATEVAR(gl_TextureEnvColor), 266 STATEVAR(gl_EyePlaneS), 267 STATEVAR(gl_EyePlaneT), 268 STATEVAR(gl_EyePlaneR), 269 STATEVAR(gl_EyePlaneQ), 270 STATEVAR(gl_ObjectPlaneS), 271 STATEVAR(gl_ObjectPlaneT), 272 STATEVAR(gl_ObjectPlaneR), 273 STATEVAR(gl_ObjectPlaneQ), 274 STATEVAR(gl_Fog), 275 276 STATEVAR(gl_ModelViewMatrix), 277 STATEVAR(gl_ModelViewMatrixInverse), 278 STATEVAR(gl_ModelViewMatrixTranspose), 279 STATEVAR(gl_ModelViewMatrixInverseTranspose), 280 281 STATEVAR(gl_ProjectionMatrix), 282 STATEVAR(gl_ProjectionMatrixInverse), 283 STATEVAR(gl_ProjectionMatrixTranspose), 284 STATEVAR(gl_ProjectionMatrixInverseTranspose), 285 286 STATEVAR(gl_ModelViewProjectionMatrix), 287 STATEVAR(gl_ModelViewProjectionMatrixInverse), 288 STATEVAR(gl_ModelViewProjectionMatrixTranspose), 289 STATEVAR(gl_ModelViewProjectionMatrixInverseTranspose), 290 291 STATEVAR(gl_TextureMatrix), 292 STATEVAR(gl_TextureMatrixInverse), 293 STATEVAR(gl_TextureMatrixTranspose), 294 STATEVAR(gl_TextureMatrixInverseTranspose), 295 296 STATEVAR(gl_NormalMatrix), 297 STATEVAR(gl_NormalScale), 298 299 STATEVAR(gl_FogParamsOptimizedMESA), 300 STATEVAR(gl_CurrentAttribVertMESA), 301 STATEVAR(gl_CurrentAttribFragMESA), 302 303 {NULL, NULL, 0} 304}; 305 306 307namespace { 308 309/** 310 * Data structure that accumulates fields for the gl_PerVertex interface 311 * block. 312 */ 313class per_vertex_accumulator 314{ 315public: 316 per_vertex_accumulator(); 317 void add_field(int slot, const glsl_type *type, const char *name); 318 const glsl_type *construct_interface_instance() const; 319 320private: 321 glsl_struct_field fields[11]; 322 unsigned num_fields; 323}; 324 325 326per_vertex_accumulator::per_vertex_accumulator() 327 : fields(), 328 num_fields(0) 329{ 330} 331 332 333void 334per_vertex_accumulator::add_field(int slot, const glsl_type *type, 335 const char *name) 336{ 337 assert(this->num_fields < ARRAY_SIZE(this->fields)); 338 this->fields[this->num_fields].type = type; 339 this->fields[this->num_fields].name = name; 340 this->fields[this->num_fields].matrix_layout = GLSL_MATRIX_LAYOUT_INHERITED; 341 this->fields[this->num_fields].location = slot; 342 this->fields[this->num_fields].offset = -1; 343 this->fields[this->num_fields].interpolation = INTERP_MODE_NONE; 344 this->fields[this->num_fields].centroid = 0; 345 this->fields[this->num_fields].sample = 0; 346 this->fields[this->num_fields].patch = 0; 347 this->fields[this->num_fields].precision = GLSL_PRECISION_NONE; 348 this->fields[this->num_fields].memory_read_only = 0; 349 this->fields[this->num_fields].memory_write_only = 0; 350 this->fields[this->num_fields].memory_coherent = 0; 351 this->fields[this->num_fields].memory_volatile = 0; 352 this->fields[this->num_fields].memory_restrict = 0; 353 this->fields[this->num_fields].image_format = 0; 354 this->fields[this->num_fields].explicit_xfb_buffer = 0; 355 this->fields[this->num_fields].xfb_buffer = -1; 356 this->fields[this->num_fields].xfb_stride = -1; 357 this->num_fields++; 358} 359 360 361const glsl_type * 362per_vertex_accumulator::construct_interface_instance() const 363{ 364 return glsl_type::get_interface_instance(this->fields, this->num_fields, 365 GLSL_INTERFACE_PACKING_STD140, 366 false, 367 "gl_PerVertex"); 368} 369 370 371class builtin_variable_generator 372{ 373public: 374 builtin_variable_generator(exec_list *instructions, 375 struct _mesa_glsl_parse_state *state); 376 void generate_constants(); 377 void generate_uniforms(); 378 void generate_special_vars(); 379 void generate_vs_special_vars(); 380 void generate_tcs_special_vars(); 381 void generate_tes_special_vars(); 382 void generate_gs_special_vars(); 383 void generate_fs_special_vars(); 384 void generate_cs_special_vars(); 385 void generate_varyings(); 386 387private: 388 const glsl_type *array(const glsl_type *base, unsigned elements) 389 { 390 return glsl_type::get_array_instance(base, elements); 391 } 392 393 const glsl_type *type(const char *name) 394 { 395 return symtab->get_type(name); 396 } 397 398 ir_variable *add_input(int slot, const glsl_type *type, const char *name) 399 { 400 return add_variable(name, type, ir_var_shader_in, slot); 401 } 402 403 ir_variable *add_output(int slot, const glsl_type *type, const char *name) 404 { 405 return add_variable(name, type, ir_var_shader_out, slot); 406 } 407 408 ir_variable *add_index_output(int slot, int index, const glsl_type *type, const char *name) 409 { 410 return add_index_variable(name, type, ir_var_shader_out, slot, index); 411 } 412 413 ir_variable *add_system_value(int slot, const glsl_type *type, 414 const char *name) 415 { 416 return add_variable(name, type, ir_var_system_value, slot); 417 } 418 419 ir_variable *add_variable(const char *name, const glsl_type *type, 420 enum ir_variable_mode mode, int slot); 421 ir_variable *add_index_variable(const char *name, const glsl_type *type, 422 enum ir_variable_mode mode, int slot, int index); 423 ir_variable *add_uniform(const glsl_type *type, const char *name); 424 ir_variable *add_const(const char *name, int value); 425 ir_variable *add_const_ivec3(const char *name, int x, int y, int z); 426 void add_varying(int slot, const glsl_type *type, const char *name); 427 428 exec_list * const instructions; 429 struct _mesa_glsl_parse_state * const state; 430 glsl_symbol_table * const symtab; 431 432 /** 433 * True if compatibility-profile-only variables should be included. (In 434 * desktop GL, these are always included when the GLSL version is 1.30 and 435 * or below). 436 */ 437 const bool compatibility; 438 439 const glsl_type * const bool_t; 440 const glsl_type * const int_t; 441 const glsl_type * const uint_t; 442 const glsl_type * const uint64_t; 443 const glsl_type * const float_t; 444 const glsl_type * const vec2_t; 445 const glsl_type * const vec3_t; 446 const glsl_type * const vec4_t; 447 const glsl_type * const uvec3_t; 448 const glsl_type * const mat3_t; 449 const glsl_type * const mat4_t; 450 451 per_vertex_accumulator per_vertex_in; 452 per_vertex_accumulator per_vertex_out; 453}; 454 455 456builtin_variable_generator::builtin_variable_generator( 457 exec_list *instructions, struct _mesa_glsl_parse_state *state) 458 : instructions(instructions), state(state), symtab(state->symbols), 459 compatibility(state->compat_shader || state->ARB_compatibility_enable), 460 bool_t(glsl_type::bool_type), int_t(glsl_type::int_type), 461 uint_t(glsl_type::uint_type), 462 uint64_t(glsl_type::uint64_t_type), 463 float_t(glsl_type::float_type), vec2_t(glsl_type::vec2_type), 464 vec3_t(glsl_type::vec3_type), vec4_t(glsl_type::vec4_type), 465 uvec3_t(glsl_type::uvec3_type), 466 mat3_t(glsl_type::mat3_type), mat4_t(glsl_type::mat4_type) 467{ 468} 469 470ir_variable * 471builtin_variable_generator::add_index_variable(const char *name, 472 const glsl_type *type, 473 enum ir_variable_mode mode, int slot, int index) 474{ 475 ir_variable *var = new(symtab) ir_variable(type, name, mode); 476 var->data.how_declared = ir_var_declared_implicitly; 477 478 switch (var->data.mode) { 479 case ir_var_auto: 480 case ir_var_shader_in: 481 case ir_var_uniform: 482 case ir_var_system_value: 483 var->data.read_only = true; 484 break; 485 case ir_var_shader_out: 486 case ir_var_shader_storage: 487 break; 488 default: 489 /* The only variables that are added using this function should be 490 * uniforms, shader storage, shader inputs, and shader outputs, constants 491 * (which use ir_var_auto), and system values. 492 */ 493 assert(0); 494 break; 495 } 496 497 var->data.location = slot; 498 var->data.explicit_location = (slot >= 0); 499 var->data.explicit_index = 1; 500 var->data.index = index; 501 502 /* Once the variable is created an initialized, add it to the symbol table 503 * and add the declaration to the IR stream. 504 */ 505 instructions->push_tail(var); 506 507 symtab->add_variable(var); 508 return var; 509} 510 511ir_variable * 512builtin_variable_generator::add_variable(const char *name, 513 const glsl_type *type, 514 enum ir_variable_mode mode, int slot) 515{ 516 ir_variable *var = new(symtab) ir_variable(type, name, mode); 517 var->data.how_declared = ir_var_declared_implicitly; 518 519 switch (var->data.mode) { 520 case ir_var_auto: 521 case ir_var_shader_in: 522 case ir_var_uniform: 523 case ir_var_system_value: 524 var->data.read_only = true; 525 break; 526 case ir_var_shader_out: 527 case ir_var_shader_storage: 528 break; 529 default: 530 /* The only variables that are added using this function should be 531 * uniforms, shader storage, shader inputs, and shader outputs, constants 532 * (which use ir_var_auto), and system values. 533 */ 534 assert(0); 535 break; 536 } 537 538 var->data.location = slot; 539 var->data.explicit_location = (slot >= 0); 540 var->data.explicit_index = 0; 541 542 /* Once the variable is created an initialized, add it to the symbol table 543 * and add the declaration to the IR stream. 544 */ 545 instructions->push_tail(var); 546 547 symtab->add_variable(var); 548 return var; 549} 550 551extern "C" const struct gl_builtin_uniform_desc * 552_mesa_glsl_get_builtin_uniform_desc(const char *name) 553{ 554 for (unsigned i = 0; _mesa_builtin_uniform_desc[i].name != NULL; i++) { 555 if (strcmp(_mesa_builtin_uniform_desc[i].name, name) == 0) { 556 return &_mesa_builtin_uniform_desc[i]; 557 } 558 } 559 return NULL; 560} 561 562ir_variable * 563builtin_variable_generator::add_uniform(const glsl_type *type, 564 const char *name) 565{ 566 ir_variable *const uni = add_variable(name, type, ir_var_uniform, -1); 567 568 const struct gl_builtin_uniform_desc* const statevar = 569 _mesa_glsl_get_builtin_uniform_desc(name); 570 assert(statevar != NULL); 571 572 const unsigned array_count = type->is_array() ? type->length : 1; 573 574 ir_state_slot *slots = 575 uni->allocate_state_slots(array_count * statevar->num_elements); 576 577 for (unsigned a = 0; a < array_count; a++) { 578 for (unsigned j = 0; j < statevar->num_elements; j++) { 579 const struct gl_builtin_uniform_element *element = 580 &statevar->elements[j]; 581 582 memcpy(slots->tokens, element->tokens, sizeof(element->tokens)); 583 if (type->is_array()) { 584 if (strcmp(name, "gl_CurrentAttribVertMESA") == 0 || 585 strcmp(name, "gl_CurrentAttribFragMESA") == 0) { 586 slots->tokens[2] = a; 587 } else { 588 slots->tokens[1] = a; 589 } 590 } 591 592 slots->swizzle = element->swizzle; 593 slots++; 594 } 595 } 596 597 return uni; 598} 599 600 601ir_variable * 602builtin_variable_generator::add_const(const char *name, int value) 603{ 604 ir_variable *const var = add_variable(name, glsl_type::int_type, 605 ir_var_auto, -1); 606 var->constant_value = new(var) ir_constant(value); 607 var->constant_initializer = new(var) ir_constant(value); 608 var->data.has_initializer = true; 609 return var; 610} 611 612 613ir_variable * 614builtin_variable_generator::add_const_ivec3(const char *name, int x, int y, 615 int z) 616{ 617 ir_variable *const var = add_variable(name, glsl_type::ivec3_type, 618 ir_var_auto, -1); 619 ir_constant_data data; 620 memset(&data, 0, sizeof(data)); 621 data.i[0] = x; 622 data.i[1] = y; 623 data.i[2] = z; 624 var->constant_value = new(var) ir_constant(glsl_type::ivec3_type, &data); 625 var->constant_initializer = 626 new(var) ir_constant(glsl_type::ivec3_type, &data); 627 var->data.has_initializer = true; 628 return var; 629} 630 631 632void 633builtin_variable_generator::generate_constants() 634{ 635 add_const("gl_MaxVertexAttribs", state->Const.MaxVertexAttribs); 636 add_const("gl_MaxVertexTextureImageUnits", 637 state->Const.MaxVertexTextureImageUnits); 638 add_const("gl_MaxCombinedTextureImageUnits", 639 state->Const.MaxCombinedTextureImageUnits); 640 add_const("gl_MaxTextureImageUnits", state->Const.MaxTextureImageUnits); 641 add_const("gl_MaxDrawBuffers", state->Const.MaxDrawBuffers); 642 643 /* Max uniforms/varyings: GLSL ES counts these in units of vectors; desktop 644 * GL counts them in units of "components" or "floats" and also in units 645 * of vectors since GL 4.1 646 */ 647 if (!state->es_shader) { 648 add_const("gl_MaxFragmentUniformComponents", 649 state->Const.MaxFragmentUniformComponents); 650 add_const("gl_MaxVertexUniformComponents", 651 state->Const.MaxVertexUniformComponents); 652 } 653 654 if (state->is_version(410, 100)) { 655 add_const("gl_MaxVertexUniformVectors", 656 state->Const.MaxVertexUniformComponents / 4); 657 add_const("gl_MaxFragmentUniformVectors", 658 state->Const.MaxFragmentUniformComponents / 4); 659 660 /* In GLSL ES 3.00, gl_MaxVaryingVectors was split out to separate 661 * vertex and fragment shader constants. 662 */ 663 if (state->is_version(0, 300)) { 664 add_const("gl_MaxVertexOutputVectors", 665 state->ctx->Const.Program[MESA_SHADER_VERTEX].MaxOutputComponents / 4); 666 add_const("gl_MaxFragmentInputVectors", 667 state->ctx->Const.Program[MESA_SHADER_FRAGMENT].MaxInputComponents / 4); 668 } else { 669 add_const("gl_MaxVaryingVectors", 670 state->ctx->Const.MaxVarying); 671 } 672 673 /* EXT_blend_func_extended brings a built in constant 674 * for determining number of dual source draw buffers 675 */ 676 if (state->EXT_blend_func_extended_enable) { 677 add_const("gl_MaxDualSourceDrawBuffersEXT", 678 state->Const.MaxDualSourceDrawBuffers); 679 } 680 } else { 681 /* Note: gl_MaxVaryingFloats was deprecated in GLSL 1.30+, but not 682 * removed 683 */ 684 add_const("gl_MaxVaryingFloats", state->ctx->Const.MaxVarying * 4); 685 } 686 687 /* Texel offsets were introduced in ARB_shading_language_420pack (which 688 * requires desktop GLSL version 130), and adopted into desktop GLSL 689 * version 4.20 and GLSL ES version 3.00. 690 */ 691 if ((state->is_version(130, 0) && 692 state->ARB_shading_language_420pack_enable) || 693 state->is_version(420, 300)) { 694 add_const("gl_MinProgramTexelOffset", 695 state->Const.MinProgramTexelOffset); 696 add_const("gl_MaxProgramTexelOffset", 697 state->Const.MaxProgramTexelOffset); 698 } 699 700 if (state->has_clip_distance()) { 701 add_const("gl_MaxClipDistances", state->Const.MaxClipPlanes); 702 } 703 if (state->is_version(130, 0)) { 704 add_const("gl_MaxVaryingComponents", state->ctx->Const.MaxVarying * 4); 705 } 706 if (state->has_cull_distance()) { 707 add_const("gl_MaxCullDistances", state->Const.MaxClipPlanes); 708 add_const("gl_MaxCombinedClipAndCullDistances", 709 state->Const.MaxClipPlanes); 710 } 711 712 if (state->has_geometry_shader()) { 713 add_const("gl_MaxVertexOutputComponents", 714 state->Const.MaxVertexOutputComponents); 715 add_const("gl_MaxGeometryInputComponents", 716 state->Const.MaxGeometryInputComponents); 717 add_const("gl_MaxGeometryOutputComponents", 718 state->Const.MaxGeometryOutputComponents); 719 add_const("gl_MaxFragmentInputComponents", 720 state->Const.MaxFragmentInputComponents); 721 add_const("gl_MaxGeometryTextureImageUnits", 722 state->Const.MaxGeometryTextureImageUnits); 723 add_const("gl_MaxGeometryOutputVertices", 724 state->Const.MaxGeometryOutputVertices); 725 add_const("gl_MaxGeometryTotalOutputComponents", 726 state->Const.MaxGeometryTotalOutputComponents); 727 add_const("gl_MaxGeometryUniformComponents", 728 state->Const.MaxGeometryUniformComponents); 729 730 /* Note: the GLSL 1.50-4.40 specs require 731 * gl_MaxGeometryVaryingComponents to be present, and to be at least 64. 732 * But they do not define what it means (and there does not appear to be 733 * any corresponding constant in the GL specs). However, 734 * ARB_geometry_shader4 defines MAX_GEOMETRY_VARYING_COMPONENTS_ARB to 735 * be the maximum number of components available for use as geometry 736 * outputs. So we assume this is a synonym for 737 * gl_MaxGeometryOutputComponents. 738 */ 739 add_const("gl_MaxGeometryVaryingComponents", 740 state->Const.MaxGeometryOutputComponents); 741 } 742 743 if (compatibility) { 744 /* Note: gl_MaxLights stopped being listed as an explicit constant in 745 * GLSL 1.30, however it continues to be referred to (as a minimum size 746 * for compatibility-mode uniforms) all the way up through GLSL 4.30, so 747 * this seems like it was probably an oversight. 748 */ 749 add_const("gl_MaxLights", state->Const.MaxLights); 750 751 add_const("gl_MaxClipPlanes", state->Const.MaxClipPlanes); 752 753 /* Note: gl_MaxTextureUnits wasn't made compatibility-only until GLSL 754 * 1.50, however this seems like it was probably an oversight. 755 */ 756 add_const("gl_MaxTextureUnits", state->Const.MaxTextureUnits); 757 758 /* Note: gl_MaxTextureCoords was left out of GLSL 1.40, but it was 759 * re-introduced in GLSL 1.50, so this seems like it was probably an 760 * oversight. 761 */ 762 add_const("gl_MaxTextureCoords", state->Const.MaxTextureCoords); 763 } 764 765 if (state->has_atomic_counters()) { 766 add_const("gl_MaxVertexAtomicCounters", 767 state->Const.MaxVertexAtomicCounters); 768 add_const("gl_MaxFragmentAtomicCounters", 769 state->Const.MaxFragmentAtomicCounters); 770 add_const("gl_MaxCombinedAtomicCounters", 771 state->Const.MaxCombinedAtomicCounters); 772 add_const("gl_MaxAtomicCounterBindings", 773 state->Const.MaxAtomicBufferBindings); 774 775 if (state->has_geometry_shader()) { 776 add_const("gl_MaxGeometryAtomicCounters", 777 state->Const.MaxGeometryAtomicCounters); 778 } 779 if (state->is_version(110, 320)) { 780 add_const("gl_MaxTessControlAtomicCounters", 781 state->Const.MaxTessControlAtomicCounters); 782 add_const("gl_MaxTessEvaluationAtomicCounters", 783 state->Const.MaxTessEvaluationAtomicCounters); 784 } 785 } 786 787 if (state->is_version(420, 310)) { 788 add_const("gl_MaxVertexAtomicCounterBuffers", 789 state->Const.MaxVertexAtomicCounterBuffers); 790 add_const("gl_MaxFragmentAtomicCounterBuffers", 791 state->Const.MaxFragmentAtomicCounterBuffers); 792 add_const("gl_MaxCombinedAtomicCounterBuffers", 793 state->Const.MaxCombinedAtomicCounterBuffers); 794 add_const("gl_MaxAtomicCounterBufferSize", 795 state->Const.MaxAtomicCounterBufferSize); 796 797 if (state->has_geometry_shader()) { 798 add_const("gl_MaxGeometryAtomicCounterBuffers", 799 state->Const.MaxGeometryAtomicCounterBuffers); 800 } 801 if (state->is_version(110, 320)) { 802 add_const("gl_MaxTessControlAtomicCounterBuffers", 803 state->Const.MaxTessControlAtomicCounterBuffers); 804 add_const("gl_MaxTessEvaluationAtomicCounterBuffers", 805 state->Const.MaxTessEvaluationAtomicCounterBuffers); 806 } 807 } 808 809 if (state->is_version(430, 310) || state->ARB_compute_shader_enable) { 810 add_const("gl_MaxComputeAtomicCounterBuffers", 811 state->Const.MaxComputeAtomicCounterBuffers); 812 add_const("gl_MaxComputeAtomicCounters", 813 state->Const.MaxComputeAtomicCounters); 814 add_const("gl_MaxComputeImageUniforms", 815 state->Const.MaxComputeImageUniforms); 816 add_const("gl_MaxComputeTextureImageUnits", 817 state->Const.MaxComputeTextureImageUnits); 818 add_const("gl_MaxComputeUniformComponents", 819 state->Const.MaxComputeUniformComponents); 820 821 add_const_ivec3("gl_MaxComputeWorkGroupCount", 822 state->Const.MaxComputeWorkGroupCount[0], 823 state->Const.MaxComputeWorkGroupCount[1], 824 state->Const.MaxComputeWorkGroupCount[2]); 825 add_const_ivec3("gl_MaxComputeWorkGroupSize", 826 state->Const.MaxComputeWorkGroupSize[0], 827 state->Const.MaxComputeWorkGroupSize[1], 828 state->Const.MaxComputeWorkGroupSize[2]); 829 830 /* From the GLSL 4.40 spec, section 7.1 (Built-In Language Variables): 831 * 832 * The built-in constant gl_WorkGroupSize is a compute-shader 833 * constant containing the local work-group size of the shader. The 834 * size of the work group in the X, Y, and Z dimensions is stored in 835 * the x, y, and z components. The constants values in 836 * gl_WorkGroupSize will match those specified in the required 837 * local_size_x, local_size_y, and local_size_z layout qualifiers 838 * for the current shader. This is a constant so that it can be 839 * used to size arrays of memory that can be shared within the local 840 * work group. It is a compile-time error to use gl_WorkGroupSize 841 * in a shader that does not declare a fixed local group size, or 842 * before that shader has declared a fixed local group size, using 843 * local_size_x, local_size_y, and local_size_z. 844 * 845 * To prevent the shader from trying to refer to gl_WorkGroupSize before 846 * the layout declaration, we don't define it here. Intead we define it 847 * in ast_cs_input_layout::hir(). 848 */ 849 } 850 851 if (state->has_enhanced_layouts()) { 852 add_const("gl_MaxTransformFeedbackBuffers", 853 state->Const.MaxTransformFeedbackBuffers); 854 add_const("gl_MaxTransformFeedbackInterleavedComponents", 855 state->Const.MaxTransformFeedbackInterleavedComponents); 856 } 857 858 if (state->has_shader_image_load_store()) { 859 add_const("gl_MaxImageUnits", 860 state->Const.MaxImageUnits); 861 add_const("gl_MaxVertexImageUniforms", 862 state->Const.MaxVertexImageUniforms); 863 add_const("gl_MaxFragmentImageUniforms", 864 state->Const.MaxFragmentImageUniforms); 865 add_const("gl_MaxCombinedImageUniforms", 866 state->Const.MaxCombinedImageUniforms); 867 868 if (state->has_geometry_shader()) { 869 add_const("gl_MaxGeometryImageUniforms", 870 state->Const.MaxGeometryImageUniforms); 871 } 872 873 if (!state->es_shader) { 874 add_const("gl_MaxCombinedImageUnitsAndFragmentOutputs", 875 state->Const.MaxCombinedShaderOutputResources); 876 add_const("gl_MaxImageSamples", 877 state->Const.MaxImageSamples); 878 } 879 880 if (state->has_tessellation_shader()) { 881 add_const("gl_MaxTessControlImageUniforms", 882 state->Const.MaxTessControlImageUniforms); 883 add_const("gl_MaxTessEvaluationImageUniforms", 884 state->Const.MaxTessEvaluationImageUniforms); 885 } 886 } 887 888 if (state->is_version(440, 310) || 889 state->ARB_ES3_1_compatibility_enable) { 890 add_const("gl_MaxCombinedShaderOutputResources", 891 state->Const.MaxCombinedShaderOutputResources); 892 } 893 894 if (state->is_version(410, 0) || 895 state->ARB_viewport_array_enable || 896 state->OES_viewport_array_enable) 897 add_const("gl_MaxViewports", state->Const.MaxViewports); 898 899 if (state->has_tessellation_shader()) { 900 add_const("gl_MaxPatchVertices", state->Const.MaxPatchVertices); 901 add_const("gl_MaxTessGenLevel", state->Const.MaxTessGenLevel); 902 add_const("gl_MaxTessControlInputComponents", state->Const.MaxTessControlInputComponents); 903 add_const("gl_MaxTessControlOutputComponents", state->Const.MaxTessControlOutputComponents); 904 add_const("gl_MaxTessControlTextureImageUnits", state->Const.MaxTessControlTextureImageUnits); 905 add_const("gl_MaxTessEvaluationInputComponents", state->Const.MaxTessEvaluationInputComponents); 906 add_const("gl_MaxTessEvaluationOutputComponents", state->Const.MaxTessEvaluationOutputComponents); 907 add_const("gl_MaxTessEvaluationTextureImageUnits", state->Const.MaxTessEvaluationTextureImageUnits); 908 add_const("gl_MaxTessPatchComponents", state->Const.MaxTessPatchComponents); 909 add_const("gl_MaxTessControlTotalOutputComponents", state->Const.MaxTessControlTotalOutputComponents); 910 add_const("gl_MaxTessControlUniformComponents", state->Const.MaxTessControlUniformComponents); 911 add_const("gl_MaxTessEvaluationUniformComponents", state->Const.MaxTessEvaluationUniformComponents); 912 } 913 914 if (state->is_version(450, 320) || 915 state->OES_sample_variables_enable || 916 state->ARB_ES3_1_compatibility_enable) 917 add_const("gl_MaxSamples", state->Const.MaxSamples); 918} 919 920 921/** 922 * Generate uniform variables (which exist in all types of shaders). 923 */ 924void 925builtin_variable_generator::generate_uniforms() 926{ 927 if (state->is_version(400, 320) || 928 state->ARB_sample_shading_enable || 929 state->OES_sample_variables_enable) 930 add_uniform(int_t, "gl_NumSamples"); 931 add_uniform(type("gl_DepthRangeParameters"), "gl_DepthRange"); 932 add_uniform(array(vec4_t, VERT_ATTRIB_MAX), "gl_CurrentAttribVertMESA"); 933 add_uniform(array(vec4_t, VARYING_SLOT_MAX), "gl_CurrentAttribFragMESA"); 934 935 if (compatibility) { 936 add_uniform(mat4_t, "gl_ModelViewMatrix"); 937 add_uniform(mat4_t, "gl_ProjectionMatrix"); 938 add_uniform(mat4_t, "gl_ModelViewProjectionMatrix"); 939 add_uniform(mat3_t, "gl_NormalMatrix"); 940 add_uniform(mat4_t, "gl_ModelViewMatrixInverse"); 941 add_uniform(mat4_t, "gl_ProjectionMatrixInverse"); 942 add_uniform(mat4_t, "gl_ModelViewProjectionMatrixInverse"); 943 add_uniform(mat4_t, "gl_ModelViewMatrixTranspose"); 944 add_uniform(mat4_t, "gl_ProjectionMatrixTranspose"); 945 add_uniform(mat4_t, "gl_ModelViewProjectionMatrixTranspose"); 946 add_uniform(mat4_t, "gl_ModelViewMatrixInverseTranspose"); 947 add_uniform(mat4_t, "gl_ProjectionMatrixInverseTranspose"); 948 add_uniform(mat4_t, "gl_ModelViewProjectionMatrixInverseTranspose"); 949 add_uniform(float_t, "gl_NormalScale"); 950 add_uniform(type("gl_LightModelParameters"), "gl_LightModel"); 951 add_uniform(vec4_t, "gl_FogParamsOptimizedMESA"); 952 953 const glsl_type *const mat4_array_type = 954 array(mat4_t, state->Const.MaxTextureCoords); 955 add_uniform(mat4_array_type, "gl_TextureMatrix"); 956 add_uniform(mat4_array_type, "gl_TextureMatrixInverse"); 957 add_uniform(mat4_array_type, "gl_TextureMatrixTranspose"); 958 add_uniform(mat4_array_type, "gl_TextureMatrixInverseTranspose"); 959 960 add_uniform(array(vec4_t, state->Const.MaxClipPlanes), "gl_ClipPlane"); 961 add_uniform(type("gl_PointParameters"), "gl_Point"); 962 963 const glsl_type *const material_parameters_type = 964 type("gl_MaterialParameters"); 965 add_uniform(material_parameters_type, "gl_FrontMaterial"); 966 add_uniform(material_parameters_type, "gl_BackMaterial"); 967 968 add_uniform(array(type("gl_LightSourceParameters"), 969 state->Const.MaxLights), 970 "gl_LightSource"); 971 972 const glsl_type *const light_model_products_type = 973 type("gl_LightModelProducts"); 974 add_uniform(light_model_products_type, "gl_FrontLightModelProduct"); 975 add_uniform(light_model_products_type, "gl_BackLightModelProduct"); 976 977 const glsl_type *const light_products_type = 978 array(type("gl_LightProducts"), state->Const.MaxLights); 979 add_uniform(light_products_type, "gl_FrontLightProduct"); 980 add_uniform(light_products_type, "gl_BackLightProduct"); 981 982 add_uniform(array(vec4_t, state->Const.MaxTextureUnits), 983 "gl_TextureEnvColor"); 984 985 const glsl_type *const texcoords_vec4 = 986 array(vec4_t, state->Const.MaxTextureCoords); 987 add_uniform(texcoords_vec4, "gl_EyePlaneS"); 988 add_uniform(texcoords_vec4, "gl_EyePlaneT"); 989 add_uniform(texcoords_vec4, "gl_EyePlaneR"); 990 add_uniform(texcoords_vec4, "gl_EyePlaneQ"); 991 add_uniform(texcoords_vec4, "gl_ObjectPlaneS"); 992 add_uniform(texcoords_vec4, "gl_ObjectPlaneT"); 993 add_uniform(texcoords_vec4, "gl_ObjectPlaneR"); 994 add_uniform(texcoords_vec4, "gl_ObjectPlaneQ"); 995 996 add_uniform(type("gl_FogParameters"), "gl_Fog"); 997 } 998} 999 1000 1001/** 1002 * Generate special variables which exist in all shaders. 1003 */ 1004void 1005builtin_variable_generator::generate_special_vars() 1006{ 1007 if (state->ARB_shader_ballot_enable) { 1008 add_system_value(SYSTEM_VALUE_SUBGROUP_SIZE, uint_t, "gl_SubGroupSizeARB"); 1009 add_system_value(SYSTEM_VALUE_SUBGROUP_INVOCATION, uint_t, "gl_SubGroupInvocationARB"); 1010 add_system_value(SYSTEM_VALUE_SUBGROUP_EQ_MASK, uint64_t, "gl_SubGroupEqMaskARB"); 1011 add_system_value(SYSTEM_VALUE_SUBGROUP_GE_MASK, uint64_t, "gl_SubGroupGeMaskARB"); 1012 add_system_value(SYSTEM_VALUE_SUBGROUP_GT_MASK, uint64_t, "gl_SubGroupGtMaskARB"); 1013 add_system_value(SYSTEM_VALUE_SUBGROUP_LE_MASK, uint64_t, "gl_SubGroupLeMaskARB"); 1014 add_system_value(SYSTEM_VALUE_SUBGROUP_LT_MASK, uint64_t, "gl_SubGroupLtMaskARB"); 1015 } 1016} 1017 1018 1019/** 1020 * Generate variables which only exist in vertex shaders. 1021 */ 1022void 1023builtin_variable_generator::generate_vs_special_vars() 1024{ 1025 ir_variable *var; 1026 1027 if (state->is_version(130, 300) || state->EXT_gpu_shader4_enable) 1028 add_system_value(SYSTEM_VALUE_VERTEX_ID, int_t, "gl_VertexID"); 1029 if (state->is_version(460, 0)) { 1030 add_system_value(SYSTEM_VALUE_BASE_VERTEX, int_t, "gl_BaseVertex"); 1031 add_system_value(SYSTEM_VALUE_BASE_INSTANCE, int_t, "gl_BaseInstance"); 1032 add_system_value(SYSTEM_VALUE_DRAW_ID, int_t, "gl_DrawID"); 1033 } 1034 if (state->ARB_draw_instanced_enable) 1035 add_system_value(SYSTEM_VALUE_INSTANCE_ID, int_t, "gl_InstanceIDARB"); 1036 if (state->ARB_draw_instanced_enable || state->is_version(140, 300) || 1037 state->EXT_gpu_shader4_enable) 1038 add_system_value(SYSTEM_VALUE_INSTANCE_ID, int_t, "gl_InstanceID"); 1039 if (state->ARB_shader_draw_parameters_enable) { 1040 add_system_value(SYSTEM_VALUE_BASE_VERTEX, int_t, "gl_BaseVertexARB"); 1041 add_system_value(SYSTEM_VALUE_BASE_INSTANCE, int_t, "gl_BaseInstanceARB"); 1042 add_system_value(SYSTEM_VALUE_DRAW_ID, int_t, "gl_DrawIDARB"); 1043 } 1044 if (state->AMD_vertex_shader_layer_enable || 1045 state->ARB_shader_viewport_layer_array_enable) { 1046 var = add_output(VARYING_SLOT_LAYER, int_t, "gl_Layer"); 1047 var->data.interpolation = INTERP_MODE_FLAT; 1048 } 1049 if (state->AMD_vertex_shader_viewport_index_enable || 1050 state->ARB_shader_viewport_layer_array_enable) { 1051 var = add_output(VARYING_SLOT_VIEWPORT, int_t, "gl_ViewportIndex"); 1052 var->data.interpolation = INTERP_MODE_FLAT; 1053 } 1054 if (compatibility) { 1055 add_input(VERT_ATTRIB_POS, vec4_t, "gl_Vertex"); 1056 add_input(VERT_ATTRIB_NORMAL, vec3_t, "gl_Normal"); 1057 add_input(VERT_ATTRIB_COLOR0, vec4_t, "gl_Color"); 1058 add_input(VERT_ATTRIB_COLOR1, vec4_t, "gl_SecondaryColor"); 1059 add_input(VERT_ATTRIB_TEX0, vec4_t, "gl_MultiTexCoord0"); 1060 add_input(VERT_ATTRIB_TEX1, vec4_t, "gl_MultiTexCoord1"); 1061 add_input(VERT_ATTRIB_TEX2, vec4_t, "gl_MultiTexCoord2"); 1062 add_input(VERT_ATTRIB_TEX3, vec4_t, "gl_MultiTexCoord3"); 1063 add_input(VERT_ATTRIB_TEX4, vec4_t, "gl_MultiTexCoord4"); 1064 add_input(VERT_ATTRIB_TEX5, vec4_t, "gl_MultiTexCoord5"); 1065 add_input(VERT_ATTRIB_TEX6, vec4_t, "gl_MultiTexCoord6"); 1066 add_input(VERT_ATTRIB_TEX7, vec4_t, "gl_MultiTexCoord7"); 1067 add_input(VERT_ATTRIB_FOG, float_t, "gl_FogCoord"); 1068 } 1069} 1070 1071 1072/** 1073 * Generate variables which only exist in tessellation control shaders. 1074 */ 1075void 1076builtin_variable_generator::generate_tcs_special_vars() 1077{ 1078 add_system_value(SYSTEM_VALUE_PRIMITIVE_ID, int_t, "gl_PrimitiveID"); 1079 add_system_value(SYSTEM_VALUE_INVOCATION_ID, int_t, "gl_InvocationID"); 1080 add_system_value(SYSTEM_VALUE_VERTICES_IN, int_t, "gl_PatchVerticesIn"); 1081 1082 add_output(VARYING_SLOT_TESS_LEVEL_OUTER, array(float_t, 4), 1083 "gl_TessLevelOuter")->data.patch = 1; 1084 add_output(VARYING_SLOT_TESS_LEVEL_INNER, array(float_t, 2), 1085 "gl_TessLevelInner")->data.patch = 1; 1086 /* XXX What to do if multiple are flipped on? */ 1087 int bbox_slot = state->ctx->Const.NoPrimitiveBoundingBoxOutput ? -1 : 1088 VARYING_SLOT_BOUNDING_BOX0; 1089 if (state->EXT_primitive_bounding_box_enable) 1090 add_output(bbox_slot, array(vec4_t, 2), "gl_BoundingBoxEXT") 1091 ->data.patch = 1; 1092 if (state->OES_primitive_bounding_box_enable) 1093 add_output(bbox_slot, array(vec4_t, 2), "gl_BoundingBoxOES") 1094 ->data.patch = 1; 1095 if (state->is_version(0, 320) || state->ARB_ES3_2_compatibility_enable) 1096 add_output(bbox_slot, array(vec4_t, 2), "gl_BoundingBox") 1097 ->data.patch = 1; 1098} 1099 1100 1101/** 1102 * Generate variables which only exist in tessellation evaluation shaders. 1103 */ 1104void 1105builtin_variable_generator::generate_tes_special_vars() 1106{ 1107 ir_variable *var; 1108 1109 add_system_value(SYSTEM_VALUE_PRIMITIVE_ID, int_t, "gl_PrimitiveID"); 1110 add_system_value(SYSTEM_VALUE_VERTICES_IN, int_t, "gl_PatchVerticesIn"); 1111 add_system_value(SYSTEM_VALUE_TESS_COORD, vec3_t, "gl_TessCoord"); 1112 if (this->state->ctx->Const.GLSLTessLevelsAsInputs) { 1113 add_input(VARYING_SLOT_TESS_LEVEL_OUTER, array(float_t, 4), 1114 "gl_TessLevelOuter")->data.patch = 1; 1115 add_input(VARYING_SLOT_TESS_LEVEL_INNER, array(float_t, 2), 1116 "gl_TessLevelInner")->data.patch = 1; 1117 } else { 1118 add_system_value(SYSTEM_VALUE_TESS_LEVEL_OUTER, array(float_t, 4), 1119 "gl_TessLevelOuter"); 1120 add_system_value(SYSTEM_VALUE_TESS_LEVEL_INNER, array(float_t, 2), 1121 "gl_TessLevelInner"); 1122 } 1123 if (state->ARB_shader_viewport_layer_array_enable) { 1124 var = add_output(VARYING_SLOT_LAYER, int_t, "gl_Layer"); 1125 var->data.interpolation = INTERP_MODE_FLAT; 1126 var = add_output(VARYING_SLOT_VIEWPORT, int_t, "gl_ViewportIndex"); 1127 var->data.interpolation = INTERP_MODE_FLAT; 1128 } 1129} 1130 1131 1132/** 1133 * Generate variables which only exist in geometry shaders. 1134 */ 1135void 1136builtin_variable_generator::generate_gs_special_vars() 1137{ 1138 ir_variable *var; 1139 1140 var = add_output(VARYING_SLOT_LAYER, int_t, "gl_Layer"); 1141 var->data.interpolation = INTERP_MODE_FLAT; 1142 if (state->is_version(410, 0) || state->ARB_viewport_array_enable || 1143 state->OES_viewport_array_enable) { 1144 var = add_output(VARYING_SLOT_VIEWPORT, int_t, "gl_ViewportIndex"); 1145 var->data.interpolation = INTERP_MODE_FLAT; 1146 } 1147 if (state->is_version(400, 320) || state->ARB_gpu_shader5_enable || 1148 state->OES_geometry_shader_enable || state->EXT_geometry_shader_enable) { 1149 add_system_value(SYSTEM_VALUE_INVOCATION_ID, int_t, "gl_InvocationID"); 1150 } 1151 1152 /* Although gl_PrimitiveID appears in tessellation control and tessellation 1153 * evaluation shaders, it has a different function there than it has in 1154 * geometry shaders, so we treat it (and its counterpart gl_PrimitiveIDIn) 1155 * as special geometry shader variables. 1156 * 1157 * Note that although the general convention of suffixing geometry shader 1158 * input varyings with "In" was not adopted into GLSL 1.50, it is used in 1159 * the specific case of gl_PrimitiveIDIn. So we don't need to treat 1160 * gl_PrimitiveIDIn as an {ARB,EXT}_geometry_shader4-only variable. 1161 */ 1162 var = add_input(VARYING_SLOT_PRIMITIVE_ID, int_t, "gl_PrimitiveIDIn"); 1163 var->data.interpolation = INTERP_MODE_FLAT; 1164 var = add_output(VARYING_SLOT_PRIMITIVE_ID, int_t, "gl_PrimitiveID"); 1165 var->data.interpolation = INTERP_MODE_FLAT; 1166} 1167 1168 1169/** 1170 * Generate variables which only exist in fragment shaders. 1171 */ 1172void 1173builtin_variable_generator::generate_fs_special_vars() 1174{ 1175 ir_variable *var; 1176 1177 if (this->state->ctx->Const.GLSLFragCoordIsSysVal) 1178 add_system_value(SYSTEM_VALUE_FRAG_COORD, vec4_t, "gl_FragCoord"); 1179 else 1180 add_input(VARYING_SLOT_POS, vec4_t, "gl_FragCoord"); 1181 1182 if (this->state->ctx->Const.GLSLFrontFacingIsSysVal) 1183 add_system_value(SYSTEM_VALUE_FRONT_FACE, bool_t, "gl_FrontFacing"); 1184 else 1185 add_input(VARYING_SLOT_FACE, bool_t, "gl_FrontFacing"); 1186 1187 if (state->is_version(120, 100)) 1188 add_input(VARYING_SLOT_PNTC, vec2_t, "gl_PointCoord"); 1189 1190 if (state->has_geometry_shader() || state->EXT_gpu_shader4_enable) { 1191 var = add_input(VARYING_SLOT_PRIMITIVE_ID, int_t, "gl_PrimitiveID"); 1192 var->data.interpolation = INTERP_MODE_FLAT; 1193 } 1194 1195 /* gl_FragColor and gl_FragData were deprecated starting in desktop GLSL 1196 * 1.30, and were relegated to the compatibility profile in GLSL 4.20. 1197 * They were removed from GLSL ES 3.00. 1198 */ 1199 if (compatibility || !state->is_version(420, 300)) { 1200 add_output(FRAG_RESULT_COLOR, vec4_t, "gl_FragColor"); 1201 add_output(FRAG_RESULT_DATA0, 1202 array(vec4_t, state->Const.MaxDrawBuffers), "gl_FragData"); 1203 } 1204 1205 if (state->has_framebuffer_fetch() && !state->is_version(130, 300)) { 1206 ir_variable *const var = 1207 add_output(FRAG_RESULT_DATA0, 1208 array(vec4_t, state->Const.MaxDrawBuffers), 1209 "gl_LastFragData"); 1210 var->data.precision = GLSL_PRECISION_MEDIUM; 1211 var->data.read_only = 1; 1212 var->data.fb_fetch_output = 1; 1213 var->data.memory_coherent = 1; 1214 } 1215 1216 if (state->es_shader && state->language_version == 100 && state->EXT_blend_func_extended_enable) { 1217 add_index_output(FRAG_RESULT_COLOR, 1, vec4_t, 1218 "gl_SecondaryFragColorEXT"); 1219 add_index_output(FRAG_RESULT_DATA0, 1, 1220 array(vec4_t, state->Const.MaxDualSourceDrawBuffers), 1221 "gl_SecondaryFragDataEXT"); 1222 } 1223 1224 /* gl_FragDepth has always been in desktop GLSL, but did not appear in GLSL 1225 * ES 1.00. 1226 */ 1227 if (state->is_version(110, 300)) 1228 add_output(FRAG_RESULT_DEPTH, float_t, "gl_FragDepth"); 1229 1230 if (state->EXT_frag_depth_enable) 1231 add_output(FRAG_RESULT_DEPTH, float_t, "gl_FragDepthEXT"); 1232 1233 if (state->ARB_shader_stencil_export_enable) { 1234 ir_variable *const var = 1235 add_output(FRAG_RESULT_STENCIL, int_t, "gl_FragStencilRefARB"); 1236 if (state->ARB_shader_stencil_export_warn) 1237 var->enable_extension_warning("GL_ARB_shader_stencil_export"); 1238 } 1239 1240 if (state->AMD_shader_stencil_export_enable) { 1241 ir_variable *const var = 1242 add_output(FRAG_RESULT_STENCIL, int_t, "gl_FragStencilRefAMD"); 1243 if (state->AMD_shader_stencil_export_warn) 1244 var->enable_extension_warning("GL_AMD_shader_stencil_export"); 1245 } 1246 1247 if (state->is_version(400, 320) || 1248 state->ARB_sample_shading_enable || 1249 state->OES_sample_variables_enable) { 1250 add_system_value(SYSTEM_VALUE_SAMPLE_ID, int_t, "gl_SampleID"); 1251 add_system_value(SYSTEM_VALUE_SAMPLE_POS, vec2_t, "gl_SamplePosition"); 1252 /* From the ARB_sample_shading specification: 1253 * "The number of elements in the array is ceil(<s>/32), where 1254 * <s> is the maximum number of color samples supported by the 1255 * implementation." 1256 * Since no drivers expose more than 32x MSAA, we can simply set 1257 * the array size to 1 rather than computing it. 1258 */ 1259 add_output(FRAG_RESULT_SAMPLE_MASK, array(int_t, 1), "gl_SampleMask"); 1260 } 1261 1262 if (state->is_version(400, 320) || 1263 state->ARB_gpu_shader5_enable || 1264 state->OES_sample_variables_enable) { 1265 add_system_value(SYSTEM_VALUE_SAMPLE_MASK_IN, array(int_t, 1), "gl_SampleMaskIn"); 1266 } 1267 1268 if (state->is_version(430, 320) || 1269 state->ARB_fragment_layer_viewport_enable || 1270 state->OES_geometry_shader_enable || 1271 state->EXT_geometry_shader_enable) { 1272 var = add_input(VARYING_SLOT_LAYER, int_t, "gl_Layer"); 1273 var->data.interpolation = INTERP_MODE_FLAT; 1274 } 1275 1276 if (state->is_version(430, 0) || 1277 state->ARB_fragment_layer_viewport_enable || 1278 state->OES_viewport_array_enable) { 1279 var = add_input(VARYING_SLOT_VIEWPORT, int_t, "gl_ViewportIndex"); 1280 var->data.interpolation = INTERP_MODE_FLAT; 1281 } 1282 1283 if (state->is_version(450, 310) || state->ARB_ES3_1_compatibility_enable) 1284 add_system_value(SYSTEM_VALUE_HELPER_INVOCATION, bool_t, "gl_HelperInvocation"); 1285} 1286 1287 1288/** 1289 * Generate variables which only exist in compute shaders. 1290 */ 1291void 1292builtin_variable_generator::generate_cs_special_vars() 1293{ 1294 add_system_value(SYSTEM_VALUE_LOCAL_INVOCATION_ID, uvec3_t, 1295 "gl_LocalInvocationID"); 1296 add_system_value(SYSTEM_VALUE_WORK_GROUP_ID, uvec3_t, "gl_WorkGroupID"); 1297 add_system_value(SYSTEM_VALUE_NUM_WORK_GROUPS, uvec3_t, "gl_NumWorkGroups"); 1298 1299 if (state->ARB_compute_variable_group_size_enable) { 1300 add_system_value(SYSTEM_VALUE_LOCAL_GROUP_SIZE, 1301 uvec3_t, "gl_LocalGroupSizeARB"); 1302 } 1303 1304 add_system_value(SYSTEM_VALUE_GLOBAL_INVOCATION_ID, 1305 uvec3_t, "gl_GlobalInvocationID"); 1306 add_system_value(SYSTEM_VALUE_LOCAL_INVOCATION_INDEX, 1307 uint_t, "gl_LocalInvocationIndex"); 1308} 1309 1310 1311/** 1312 * Add a single "varying" variable. The variable's type and direction (input 1313 * or output) are adjusted as appropriate for the type of shader being 1314 * compiled. 1315 */ 1316void 1317builtin_variable_generator::add_varying(int slot, const glsl_type *type, 1318 const char *name) 1319{ 1320 switch (state->stage) { 1321 case MESA_SHADER_TESS_CTRL: 1322 case MESA_SHADER_TESS_EVAL: 1323 case MESA_SHADER_GEOMETRY: 1324 this->per_vertex_in.add_field(slot, type, name); 1325 /* FALLTHROUGH */ 1326 case MESA_SHADER_VERTEX: 1327 this->per_vertex_out.add_field(slot, type, name); 1328 break; 1329 case MESA_SHADER_FRAGMENT: 1330 add_input(slot, type, name); 1331 break; 1332 case MESA_SHADER_COMPUTE: 1333 /* Compute shaders don't have varyings. */ 1334 break; 1335 default: 1336 break; 1337 } 1338} 1339 1340 1341/** 1342 * Generate variables that are used to communicate data from one shader stage 1343 * to the next ("varyings"). 1344 */ 1345void 1346builtin_variable_generator::generate_varyings() 1347{ 1348 /* gl_Position and gl_PointSize are not visible from fragment shaders. */ 1349 if (state->stage != MESA_SHADER_FRAGMENT) { 1350 add_varying(VARYING_SLOT_POS, vec4_t, "gl_Position"); 1351 if (!state->es_shader || 1352 state->stage == MESA_SHADER_VERTEX || 1353 (state->stage == MESA_SHADER_GEOMETRY && 1354 (state->OES_geometry_point_size_enable || 1355 state->EXT_geometry_point_size_enable)) || 1356 ((state->stage == MESA_SHADER_TESS_CTRL || 1357 state->stage == MESA_SHADER_TESS_EVAL) && 1358 (state->OES_tessellation_point_size_enable || 1359 state->EXT_tessellation_point_size_enable))) { 1360 add_varying(VARYING_SLOT_PSIZ, float_t, "gl_PointSize"); 1361 } 1362 } 1363 1364 if (state->has_clip_distance()) { 1365 add_varying(VARYING_SLOT_CLIP_DIST0, array(float_t, 0), 1366 "gl_ClipDistance"); 1367 } 1368 if (state->has_cull_distance()) { 1369 add_varying(VARYING_SLOT_CULL_DIST0, array(float_t, 0), 1370 "gl_CullDistance"); 1371 } 1372 1373 if (compatibility) { 1374 add_varying(VARYING_SLOT_TEX0, array(vec4_t, 0), "gl_TexCoord"); 1375 add_varying(VARYING_SLOT_FOGC, float_t, "gl_FogFragCoord"); 1376 if (state->stage == MESA_SHADER_FRAGMENT) { 1377 add_varying(VARYING_SLOT_COL0, vec4_t, "gl_Color"); 1378 add_varying(VARYING_SLOT_COL1, vec4_t, "gl_SecondaryColor"); 1379 } else { 1380 add_varying(VARYING_SLOT_CLIP_VERTEX, vec4_t, "gl_ClipVertex"); 1381 add_varying(VARYING_SLOT_COL0, vec4_t, "gl_FrontColor"); 1382 add_varying(VARYING_SLOT_BFC0, vec4_t, "gl_BackColor"); 1383 add_varying(VARYING_SLOT_COL1, vec4_t, "gl_FrontSecondaryColor"); 1384 add_varying(VARYING_SLOT_BFC1, vec4_t, "gl_BackSecondaryColor"); 1385 } 1386 } 1387 1388 /* Section 7.1 (Built-In Language Variables) of the GLSL 4.00 spec 1389 * says: 1390 * 1391 * "In the tessellation control language, built-in variables are 1392 * intrinsically declared as: 1393 * 1394 * in gl_PerVertex { 1395 * vec4 gl_Position; 1396 * float gl_PointSize; 1397 * float gl_ClipDistance[]; 1398 * } gl_in[gl_MaxPatchVertices];" 1399 */ 1400 if (state->stage == MESA_SHADER_TESS_CTRL || 1401 state->stage == MESA_SHADER_TESS_EVAL) { 1402 const glsl_type *per_vertex_in_type = 1403 this->per_vertex_in.construct_interface_instance(); 1404 add_variable("gl_in", array(per_vertex_in_type, state->Const.MaxPatchVertices), 1405 ir_var_shader_in, -1); 1406 } 1407 if (state->stage == MESA_SHADER_GEOMETRY) { 1408 const glsl_type *per_vertex_in_type = 1409 this->per_vertex_in.construct_interface_instance(); 1410 add_variable("gl_in", array(per_vertex_in_type, 0), 1411 ir_var_shader_in, -1); 1412 } 1413 if (state->stage == MESA_SHADER_TESS_CTRL) { 1414 const glsl_type *per_vertex_out_type = 1415 this->per_vertex_out.construct_interface_instance(); 1416 add_variable("gl_out", array(per_vertex_out_type, 0), 1417 ir_var_shader_out, -1); 1418 } 1419 if (state->stage == MESA_SHADER_VERTEX || 1420 state->stage == MESA_SHADER_TESS_EVAL || 1421 state->stage == MESA_SHADER_GEOMETRY) { 1422 const glsl_type *per_vertex_out_type = 1423 this->per_vertex_out.construct_interface_instance(); 1424 const glsl_struct_field *fields = per_vertex_out_type->fields.structure; 1425 for (unsigned i = 0; i < per_vertex_out_type->length; i++) { 1426 ir_variable *var = 1427 add_variable(fields[i].name, fields[i].type, ir_var_shader_out, 1428 fields[i].location); 1429 var->data.interpolation = fields[i].interpolation; 1430 var->data.centroid = fields[i].centroid; 1431 var->data.sample = fields[i].sample; 1432 var->data.patch = fields[i].patch; 1433 var->data.precision = fields[i].precision; 1434 var->init_interface_type(per_vertex_out_type); 1435 } 1436 } 1437} 1438 1439 1440}; /* Anonymous namespace */ 1441 1442 1443void 1444_mesa_glsl_initialize_variables(exec_list *instructions, 1445 struct _mesa_glsl_parse_state *state) 1446{ 1447 builtin_variable_generator gen(instructions, state); 1448 1449 gen.generate_constants(); 1450 gen.generate_uniforms(); 1451 gen.generate_special_vars(); 1452 1453 gen.generate_varyings(); 1454 1455 switch (state->stage) { 1456 case MESA_SHADER_VERTEX: 1457 gen.generate_vs_special_vars(); 1458 break; 1459 case MESA_SHADER_TESS_CTRL: 1460 gen.generate_tcs_special_vars(); 1461 break; 1462 case MESA_SHADER_TESS_EVAL: 1463 gen.generate_tes_special_vars(); 1464 break; 1465 case MESA_SHADER_GEOMETRY: 1466 gen.generate_gs_special_vars(); 1467 break; 1468 case MESA_SHADER_FRAGMENT: 1469 gen.generate_fs_special_vars(); 1470 break; 1471 case MESA_SHADER_COMPUTE: 1472 gen.generate_cs_special_vars(); 1473 break; 1474 default: 1475 break; 1476 } 1477} 1478