builtin_variables.cpp revision 7ec681f3
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, MAT_ATTRIB_FRONT_EMISSION}, SWIZZLE_XYZW}, 78 {"ambient", {STATE_MATERIAL, MAT_ATTRIB_FRONT_AMBIENT}, SWIZZLE_XYZW}, 79 {"diffuse", {STATE_MATERIAL, MAT_ATTRIB_FRONT_DIFFUSE}, SWIZZLE_XYZW}, 80 {"specular", {STATE_MATERIAL, MAT_ATTRIB_FRONT_SPECULAR}, SWIZZLE_XYZW}, 81 {"shininess", {STATE_MATERIAL, MAT_ATTRIB_FRONT_SHININESS}, SWIZZLE_XXXX}, 82}; 83 84static const struct gl_builtin_uniform_element gl_BackMaterial_elements[] = { 85 {"emission", {STATE_MATERIAL, MAT_ATTRIB_BACK_EMISSION}, SWIZZLE_XYZW}, 86 {"ambient", {STATE_MATERIAL, MAT_ATTRIB_BACK_AMBIENT}, SWIZZLE_XYZW}, 87 {"diffuse", {STATE_MATERIAL, MAT_ATTRIB_BACK_DIFFUSE}, SWIZZLE_XYZW}, 88 {"specular", {STATE_MATERIAL, MAT_ATTRIB_BACK_SPECULAR}, SWIZZLE_XYZW}, 89 {"shininess", {STATE_MATERIAL, MAT_ATTRIB_BACK_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 {"spotCosCutoff", {STATE_LIGHT, 0, STATE_SPOT_DIRECTION}, SWIZZLE_WWWW}, 104 {"constantAttenuation", {STATE_LIGHT, 0, STATE_ATTENUATION}, SWIZZLE_XXXX}, 105 {"linearAttenuation", {STATE_LIGHT, 0, STATE_ATTENUATION}, SWIZZLE_YYYY}, 106 {"quadraticAttenuation", {STATE_LIGHT, 0, STATE_ATTENUATION}, SWIZZLE_ZZZZ}, 107 {"spotExponent", {STATE_LIGHT, 0, STATE_ATTENUATION}, SWIZZLE_WWWW}, 108 {"spotCutoff", {STATE_LIGHT, 0, STATE_SPOT_CUTOFF}, SWIZZLE_XXXX}, 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, MAT_ATTRIB_FRONT_AMBIENT}, SWIZZLE_XYZW}, 125 {"diffuse", {STATE_LIGHTPROD, 0, MAT_ATTRIB_FRONT_DIFFUSE}, SWIZZLE_XYZW}, 126 {"specular", {STATE_LIGHTPROD, 0, MAT_ATTRIB_FRONT_SPECULAR}, SWIZZLE_XYZW}, 127}; 128 129static const struct gl_builtin_uniform_element gl_BackLightProduct_elements[] = { 130 {"ambient", {STATE_LIGHTPROD, 0, MAT_ATTRIB_BACK_AMBIENT}, SWIZZLE_XYZW}, 131 {"diffuse", {STATE_LIGHTPROD, 0, MAT_ATTRIB_BACK_DIFFUSE}, SWIZZLE_XYZW}, 132 {"specular", {STATE_LIGHTPROD, 0, MAT_ATTRIB_BACK_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_EYESPACE}, SWIZZLE_XXXX}, 181}; 182 183static const struct gl_builtin_uniform_element gl_FogParamsOptimizedMESA_elements[] = { 184 {NULL, {STATE_FOG_PARAMS_OPTIMIZED}, SWIZZLE_XYZW}, 185}; 186 187#define ATTRIB(i) \ 188 static const struct gl_builtin_uniform_element gl_CurrentAttribFrag##i##MESA_elements[] = { \ 189 {NULL, {STATE_CURRENT_ATTRIB_MAYBE_VP_CLAMPED, i}, SWIZZLE_XYZW}, \ 190 }; 191 192ATTRIB(0) 193ATTRIB(1) 194ATTRIB(2) 195ATTRIB(3) 196ATTRIB(4) 197ATTRIB(5) 198ATTRIB(6) 199ATTRIB(7) 200ATTRIB(8) 201ATTRIB(9) 202ATTRIB(10) 203ATTRIB(11) 204ATTRIB(12) 205ATTRIB(13) 206ATTRIB(14) 207ATTRIB(15) 208ATTRIB(16) 209ATTRIB(17) 210ATTRIB(18) 211ATTRIB(19) 212ATTRIB(20) 213ATTRIB(21) 214ATTRIB(22) 215ATTRIB(23) 216ATTRIB(24) 217ATTRIB(25) 218ATTRIB(26) 219ATTRIB(27) 220ATTRIB(28) 221ATTRIB(29) 222ATTRIB(30) 223ATTRIB(31) 224 225#define MATRIX(name, statevar) \ 226 static const struct gl_builtin_uniform_element name ## _elements[] = { \ 227 { NULL, { statevar, 0, 0, 0}, SWIZZLE_XYZW }, \ 228 { NULL, { statevar, 0, 1, 1}, SWIZZLE_XYZW }, \ 229 { NULL, { statevar, 0, 2, 2}, SWIZZLE_XYZW }, \ 230 { NULL, { statevar, 0, 3, 3}, SWIZZLE_XYZW }, \ 231 } 232 233MATRIX(gl_ModelViewMatrix, STATE_MODELVIEW_MATRIX_TRANSPOSE); 234MATRIX(gl_ModelViewMatrixInverse, STATE_MODELVIEW_MATRIX_INVTRANS); 235MATRIX(gl_ModelViewMatrixTranspose, STATE_MODELVIEW_MATRIX); 236MATRIX(gl_ModelViewMatrixInverseTranspose, STATE_MODELVIEW_MATRIX_INVERSE); 237 238MATRIX(gl_ProjectionMatrix, STATE_PROJECTION_MATRIX_TRANSPOSE); 239MATRIX(gl_ProjectionMatrixInverse, STATE_PROJECTION_MATRIX_INVTRANS); 240MATRIX(gl_ProjectionMatrixTranspose, STATE_PROJECTION_MATRIX); 241MATRIX(gl_ProjectionMatrixInverseTranspose, STATE_PROJECTION_MATRIX_INVERSE); 242 243MATRIX(gl_ModelViewProjectionMatrix, STATE_MVP_MATRIX_TRANSPOSE); 244MATRIX(gl_ModelViewProjectionMatrixInverse, STATE_MVP_MATRIX_INVTRANS); 245MATRIX(gl_ModelViewProjectionMatrixTranspose, STATE_MVP_MATRIX); 246MATRIX(gl_ModelViewProjectionMatrixInverseTranspose, STATE_MVP_MATRIX_INVERSE); 247 248MATRIX(gl_TextureMatrix, STATE_TEXTURE_MATRIX_TRANSPOSE); 249MATRIX(gl_TextureMatrixInverse, STATE_TEXTURE_MATRIX_INVTRANS); 250MATRIX(gl_TextureMatrixTranspose, STATE_TEXTURE_MATRIX); 251MATRIX(gl_TextureMatrixInverseTranspose, STATE_TEXTURE_MATRIX_INVERSE); 252 253static const struct gl_builtin_uniform_element gl_NormalMatrix_elements[] = { 254 { NULL, { STATE_MODELVIEW_MATRIX_INVERSE, 0, 0, 0}, 255 MAKE_SWIZZLE4(SWIZZLE_X, SWIZZLE_Y, SWIZZLE_Z, SWIZZLE_Z) }, 256 { NULL, { STATE_MODELVIEW_MATRIX_INVERSE, 0, 1, 1}, 257 MAKE_SWIZZLE4(SWIZZLE_X, SWIZZLE_Y, SWIZZLE_Z, SWIZZLE_Z) }, 258 { NULL, { STATE_MODELVIEW_MATRIX_INVERSE, 0, 2, 2}, 259 MAKE_SWIZZLE4(SWIZZLE_X, SWIZZLE_Y, SWIZZLE_Z, SWIZZLE_Z) }, 260}; 261 262#undef MATRIX 263 264#define STATEVAR(name) {#name, name ## _elements, ARRAY_SIZE(name ## _elements)} 265 266static const struct gl_builtin_uniform_desc _mesa_builtin_uniform_desc[] = { 267 STATEVAR(gl_NumSamples), 268 STATEVAR(gl_DepthRange), 269 STATEVAR(gl_ClipPlane), 270 STATEVAR(gl_Point), 271 STATEVAR(gl_FrontMaterial), 272 STATEVAR(gl_BackMaterial), 273 STATEVAR(gl_LightSource), 274 STATEVAR(gl_LightModel), 275 STATEVAR(gl_FrontLightModelProduct), 276 STATEVAR(gl_BackLightModelProduct), 277 STATEVAR(gl_FrontLightProduct), 278 STATEVAR(gl_BackLightProduct), 279 STATEVAR(gl_TextureEnvColor), 280 STATEVAR(gl_EyePlaneS), 281 STATEVAR(gl_EyePlaneT), 282 STATEVAR(gl_EyePlaneR), 283 STATEVAR(gl_EyePlaneQ), 284 STATEVAR(gl_ObjectPlaneS), 285 STATEVAR(gl_ObjectPlaneT), 286 STATEVAR(gl_ObjectPlaneR), 287 STATEVAR(gl_ObjectPlaneQ), 288 STATEVAR(gl_Fog), 289 290 STATEVAR(gl_ModelViewMatrix), 291 STATEVAR(gl_ModelViewMatrixInverse), 292 STATEVAR(gl_ModelViewMatrixTranspose), 293 STATEVAR(gl_ModelViewMatrixInverseTranspose), 294 295 STATEVAR(gl_ProjectionMatrix), 296 STATEVAR(gl_ProjectionMatrixInverse), 297 STATEVAR(gl_ProjectionMatrixTranspose), 298 STATEVAR(gl_ProjectionMatrixInverseTranspose), 299 300 STATEVAR(gl_ModelViewProjectionMatrix), 301 STATEVAR(gl_ModelViewProjectionMatrixInverse), 302 STATEVAR(gl_ModelViewProjectionMatrixTranspose), 303 STATEVAR(gl_ModelViewProjectionMatrixInverseTranspose), 304 305 STATEVAR(gl_TextureMatrix), 306 STATEVAR(gl_TextureMatrixInverse), 307 STATEVAR(gl_TextureMatrixTranspose), 308 STATEVAR(gl_TextureMatrixInverseTranspose), 309 310 STATEVAR(gl_NormalMatrix), 311 STATEVAR(gl_NormalScale), 312 313 STATEVAR(gl_FogParamsOptimizedMESA), 314 315 STATEVAR(gl_CurrentAttribFrag0MESA), 316 STATEVAR(gl_CurrentAttribFrag1MESA), 317 STATEVAR(gl_CurrentAttribFrag2MESA), 318 STATEVAR(gl_CurrentAttribFrag3MESA), 319 STATEVAR(gl_CurrentAttribFrag4MESA), 320 STATEVAR(gl_CurrentAttribFrag5MESA), 321 STATEVAR(gl_CurrentAttribFrag6MESA), 322 STATEVAR(gl_CurrentAttribFrag7MESA), 323 STATEVAR(gl_CurrentAttribFrag8MESA), 324 STATEVAR(gl_CurrentAttribFrag9MESA), 325 STATEVAR(gl_CurrentAttribFrag10MESA), 326 STATEVAR(gl_CurrentAttribFrag11MESA), 327 STATEVAR(gl_CurrentAttribFrag12MESA), 328 STATEVAR(gl_CurrentAttribFrag13MESA), 329 STATEVAR(gl_CurrentAttribFrag14MESA), 330 STATEVAR(gl_CurrentAttribFrag15MESA), 331 STATEVAR(gl_CurrentAttribFrag16MESA), 332 STATEVAR(gl_CurrentAttribFrag17MESA), 333 STATEVAR(gl_CurrentAttribFrag18MESA), 334 STATEVAR(gl_CurrentAttribFrag19MESA), 335 STATEVAR(gl_CurrentAttribFrag20MESA), 336 STATEVAR(gl_CurrentAttribFrag21MESA), 337 STATEVAR(gl_CurrentAttribFrag22MESA), 338 STATEVAR(gl_CurrentAttribFrag23MESA), 339 STATEVAR(gl_CurrentAttribFrag24MESA), 340 STATEVAR(gl_CurrentAttribFrag25MESA), 341 STATEVAR(gl_CurrentAttribFrag26MESA), 342 STATEVAR(gl_CurrentAttribFrag27MESA), 343 STATEVAR(gl_CurrentAttribFrag28MESA), 344 STATEVAR(gl_CurrentAttribFrag29MESA), 345 STATEVAR(gl_CurrentAttribFrag30MESA), 346 STATEVAR(gl_CurrentAttribFrag31MESA), 347 348 {NULL, NULL, 0} 349}; 350 351 352namespace { 353 354/** 355 * Data structure that accumulates fields for the gl_PerVertex interface 356 * block. 357 */ 358class per_vertex_accumulator 359{ 360public: 361 per_vertex_accumulator(); 362 void add_field(int slot, const glsl_type *type, int precision, 363 const char *name, enum glsl_interp_mode interp); 364 const glsl_type *construct_interface_instance() const; 365 366private: 367 glsl_struct_field fields[14]; 368 unsigned num_fields; 369}; 370 371 372per_vertex_accumulator::per_vertex_accumulator() 373 : fields(), 374 num_fields(0) 375{ 376} 377 378 379void 380per_vertex_accumulator::add_field(int slot, const glsl_type *type, 381 int precision, const char *name, 382 enum glsl_interp_mode interp) 383{ 384 assert(this->num_fields < ARRAY_SIZE(this->fields)); 385 this->fields[this->num_fields].type = type; 386 this->fields[this->num_fields].name = name; 387 this->fields[this->num_fields].matrix_layout = GLSL_MATRIX_LAYOUT_INHERITED; 388 this->fields[this->num_fields].location = slot; 389 this->fields[this->num_fields].offset = -1; 390 this->fields[this->num_fields].interpolation = interp; 391 this->fields[this->num_fields].centroid = 0; 392 this->fields[this->num_fields].sample = 0; 393 this->fields[this->num_fields].patch = 0; 394 this->fields[this->num_fields].precision = precision; 395 this->fields[this->num_fields].memory_read_only = 0; 396 this->fields[this->num_fields].memory_write_only = 0; 397 this->fields[this->num_fields].memory_coherent = 0; 398 this->fields[this->num_fields].memory_volatile = 0; 399 this->fields[this->num_fields].memory_restrict = 0; 400 this->fields[this->num_fields].image_format = PIPE_FORMAT_NONE; 401 this->fields[this->num_fields].explicit_xfb_buffer = 0; 402 this->fields[this->num_fields].xfb_buffer = -1; 403 this->fields[this->num_fields].xfb_stride = -1; 404 this->num_fields++; 405} 406 407 408const glsl_type * 409per_vertex_accumulator::construct_interface_instance() const 410{ 411 return glsl_type::get_interface_instance(this->fields, this->num_fields, 412 GLSL_INTERFACE_PACKING_STD140, 413 false, 414 "gl_PerVertex"); 415} 416 417 418class builtin_variable_generator 419{ 420public: 421 builtin_variable_generator(exec_list *instructions, 422 struct _mesa_glsl_parse_state *state); 423 void generate_constants(); 424 void generate_uniforms(); 425 void generate_special_vars(); 426 void generate_vs_special_vars(); 427 void generate_tcs_special_vars(); 428 void generate_tes_special_vars(); 429 void generate_gs_special_vars(); 430 void generate_fs_special_vars(); 431 void generate_cs_special_vars(); 432 void generate_varyings(); 433 434private: 435 const glsl_type *array(const glsl_type *base, unsigned elements) 436 { 437 return glsl_type::get_array_instance(base, elements); 438 } 439 440 const glsl_type *type(const char *name) 441 { 442 return symtab->get_type(name); 443 } 444 445 ir_variable *add_input(int slot, const glsl_type *type, int precision, 446 const char *name, 447 enum glsl_interp_mode interp = INTERP_MODE_NONE) 448 { 449 return add_variable(name, type, precision, ir_var_shader_in, slot, interp); 450 } 451 452 ir_variable *add_input(int slot, const glsl_type *type, const char *name, 453 enum glsl_interp_mode interp = INTERP_MODE_NONE) 454 { 455 return add_input(slot, type, GLSL_PRECISION_NONE, name, interp); 456 } 457 458 ir_variable *add_output(int slot, const glsl_type *type, int precision, 459 const char *name) 460 { 461 return add_variable(name, type, precision, ir_var_shader_out, slot); 462 } 463 464 ir_variable *add_output(int slot, const glsl_type *type, const char *name) 465 { 466 return add_output(slot, type, GLSL_PRECISION_NONE, name); 467 } 468 469 ir_variable *add_index_output(int slot, int index, const glsl_type *type, 470 int precision, const char *name) 471 { 472 return add_index_variable(name, type, precision, ir_var_shader_out, slot, 473 index); 474 } 475 476 ir_variable *add_system_value(int slot, const glsl_type *type, int precision, 477 const char *name) 478 { 479 return add_variable(name, type, precision, ir_var_system_value, slot); 480 } 481 ir_variable *add_system_value(int slot, const glsl_type *type, 482 const char *name) 483 { 484 return add_system_value(slot, type, GLSL_PRECISION_NONE, name); 485 } 486 487 ir_variable *add_variable(const char *name, const glsl_type *type, 488 int precision, enum ir_variable_mode mode, 489 int slot, enum glsl_interp_mode interp = INTERP_MODE_NONE); 490 ir_variable *add_index_variable(const char *name, const glsl_type *type, 491 int precision, enum ir_variable_mode mode, 492 int slot, int index); 493 ir_variable *add_uniform(const glsl_type *type, int precision, 494 const char *name); 495 ir_variable *add_uniform(const glsl_type *type, const char *name) 496 { 497 return add_uniform(type, GLSL_PRECISION_NONE, name); 498 } 499 ir_variable *add_const(const char *name, int precision, int value); 500 ir_variable *add_const(const char *name, int value) 501 { 502 return add_const(name, GLSL_PRECISION_MEDIUM, value); 503 } 504 ir_variable *add_const_ivec3(const char *name, int x, int y, int z); 505 void add_varying(int slot, const glsl_type *type, int precision, 506 const char *name, 507 enum glsl_interp_mode interp = INTERP_MODE_NONE); 508 void add_varying(int slot, const glsl_type *type, const char *name, 509 enum glsl_interp_mode interp = INTERP_MODE_NONE) 510 { 511 add_varying(slot, type, GLSL_PRECISION_NONE, name, interp); 512 } 513 514 exec_list * const instructions; 515 struct _mesa_glsl_parse_state * const state; 516 glsl_symbol_table * const symtab; 517 518 /** 519 * True if compatibility-profile-only variables should be included. (In 520 * desktop GL, these are always included when the GLSL version is 1.30 and 521 * or below). 522 */ 523 const bool compatibility; 524 525 const glsl_type * const bool_t; 526 const glsl_type * const int_t; 527 const glsl_type * const uint_t; 528 const glsl_type * const uint64_t; 529 const glsl_type * const float_t; 530 const glsl_type * const vec2_t; 531 const glsl_type * const vec3_t; 532 const glsl_type * const vec4_t; 533 const glsl_type * const uvec3_t; 534 const glsl_type * const mat3_t; 535 const glsl_type * const mat4_t; 536 537 per_vertex_accumulator per_vertex_in; 538 per_vertex_accumulator per_vertex_out; 539}; 540 541 542builtin_variable_generator::builtin_variable_generator( 543 exec_list *instructions, struct _mesa_glsl_parse_state *state) 544 : instructions(instructions), state(state), symtab(state->symbols), 545 compatibility(state->compat_shader || state->ARB_compatibility_enable), 546 bool_t(glsl_type::bool_type), int_t(glsl_type::int_type), 547 uint_t(glsl_type::uint_type), 548 uint64_t(glsl_type::uint64_t_type), 549 float_t(glsl_type::float_type), vec2_t(glsl_type::vec2_type), 550 vec3_t(glsl_type::vec3_type), vec4_t(glsl_type::vec4_type), 551 uvec3_t(glsl_type::uvec3_type), 552 mat3_t(glsl_type::mat3_type), mat4_t(glsl_type::mat4_type) 553{ 554} 555 556ir_variable * 557builtin_variable_generator::add_index_variable(const char *name, 558 const glsl_type *type, 559 int precision, 560 enum ir_variable_mode mode, 561 int slot, int index) 562{ 563 ir_variable *var = new(symtab) ir_variable(type, name, mode); 564 var->data.how_declared = ir_var_declared_implicitly; 565 566 switch (var->data.mode) { 567 case ir_var_auto: 568 case ir_var_shader_in: 569 case ir_var_uniform: 570 case ir_var_system_value: 571 var->data.read_only = true; 572 break; 573 case ir_var_shader_out: 574 case ir_var_shader_storage: 575 break; 576 default: 577 /* The only variables that are added using this function should be 578 * uniforms, shader storage, shader inputs, and shader outputs, constants 579 * (which use ir_var_auto), and system values. 580 */ 581 assert(0); 582 break; 583 } 584 585 var->data.location = slot; 586 var->data.explicit_location = (slot >= 0); 587 var->data.explicit_index = 1; 588 var->data.index = index; 589 590 if (state->es_shader) 591 var->data.precision = precision; 592 593 /* Once the variable is created an initialized, add it to the symbol table 594 * and add the declaration to the IR stream. 595 */ 596 instructions->push_tail(var); 597 598 symtab->add_variable(var); 599 return var; 600} 601 602ir_variable * 603builtin_variable_generator::add_variable(const char *name, 604 const glsl_type *type, 605 int precision, 606 enum ir_variable_mode mode, int slot, 607 enum glsl_interp_mode interp) 608{ 609 ir_variable *var = new(symtab) ir_variable(type, name, mode); 610 var->data.how_declared = ir_var_declared_implicitly; 611 612 switch (var->data.mode) { 613 case ir_var_auto: 614 case ir_var_shader_in: 615 case ir_var_uniform: 616 case ir_var_system_value: 617 var->data.read_only = true; 618 break; 619 case ir_var_shader_out: 620 case ir_var_shader_storage: 621 break; 622 default: 623 /* The only variables that are added using this function should be 624 * uniforms, shader storage, shader inputs, and shader outputs, constants 625 * (which use ir_var_auto), and system values. 626 */ 627 assert(0); 628 break; 629 } 630 631 var->data.location = slot; 632 var->data.explicit_location = (slot >= 0); 633 var->data.explicit_index = 0; 634 var->data.interpolation = interp; 635 636 if (state->es_shader) 637 var->data.precision = precision; 638 639 /* Once the variable is created an initialized, add it to the symbol table 640 * and add the declaration to the IR stream. 641 */ 642 instructions->push_tail(var); 643 644 symtab->add_variable(var); 645 return var; 646} 647 648extern "C" const struct gl_builtin_uniform_desc * 649_mesa_glsl_get_builtin_uniform_desc(const char *name) 650{ 651 for (unsigned i = 0; _mesa_builtin_uniform_desc[i].name != NULL; i++) { 652 if (strcmp(_mesa_builtin_uniform_desc[i].name, name) == 0) { 653 return &_mesa_builtin_uniform_desc[i]; 654 } 655 } 656 return NULL; 657} 658 659ir_variable * 660builtin_variable_generator::add_uniform(const glsl_type *type, 661 int precision, 662 const char *name) 663{ 664 ir_variable *const uni = 665 add_variable(name, type, precision, ir_var_uniform, -1); 666 667 const struct gl_builtin_uniform_desc* const statevar = 668 _mesa_glsl_get_builtin_uniform_desc(name); 669 assert(statevar != NULL); 670 671 const unsigned array_count = type->is_array() ? type->length : 1; 672 673 ir_state_slot *slots = 674 uni->allocate_state_slots(array_count * statevar->num_elements); 675 676 for (unsigned a = 0; a < array_count; a++) { 677 for (unsigned j = 0; j < statevar->num_elements; j++) { 678 const struct gl_builtin_uniform_element *element = 679 &statevar->elements[j]; 680 681 memcpy(slots->tokens, element->tokens, sizeof(element->tokens)); 682 if (type->is_array()) 683 slots->tokens[1] = a; 684 685 slots->swizzle = element->swizzle; 686 slots++; 687 } 688 } 689 690 return uni; 691} 692 693 694ir_variable * 695builtin_variable_generator::add_const(const char *name, int precision, 696 int value) 697{ 698 ir_variable *const var = add_variable(name, glsl_type::int_type, 699 precision, ir_var_auto, -1); 700 var->constant_value = new(var) ir_constant(value); 701 var->constant_initializer = new(var) ir_constant(value); 702 var->data.has_initializer = true; 703 return var; 704} 705 706 707ir_variable * 708builtin_variable_generator::add_const_ivec3(const char *name, int x, int y, 709 int z) 710{ 711 ir_variable *const var = add_variable(name, glsl_type::ivec3_type, 712 GLSL_PRECISION_HIGH, 713 ir_var_auto, -1); 714 ir_constant_data data; 715 memset(&data, 0, sizeof(data)); 716 data.i[0] = x; 717 data.i[1] = y; 718 data.i[2] = z; 719 var->constant_value = new(var) ir_constant(glsl_type::ivec3_type, &data); 720 var->constant_initializer = 721 new(var) ir_constant(glsl_type::ivec3_type, &data); 722 var->data.has_initializer = true; 723 return var; 724} 725 726 727void 728builtin_variable_generator::generate_constants() 729{ 730 add_const("gl_MaxVertexAttribs", state->Const.MaxVertexAttribs); 731 add_const("gl_MaxVertexTextureImageUnits", 732 state->Const.MaxVertexTextureImageUnits); 733 add_const("gl_MaxCombinedTextureImageUnits", 734 state->Const.MaxCombinedTextureImageUnits); 735 add_const("gl_MaxTextureImageUnits", state->Const.MaxTextureImageUnits); 736 add_const("gl_MaxDrawBuffers", state->Const.MaxDrawBuffers); 737 738 /* Max uniforms/varyings: GLSL ES counts these in units of vectors; desktop 739 * GL counts them in units of "components" or "floats" and also in units 740 * of vectors since GL 4.1 741 */ 742 if (!state->es_shader) { 743 add_const("gl_MaxFragmentUniformComponents", 744 state->Const.MaxFragmentUniformComponents); 745 add_const("gl_MaxVertexUniformComponents", 746 state->Const.MaxVertexUniformComponents); 747 } 748 749 if (state->is_version(410, 100)) { 750 add_const("gl_MaxVertexUniformVectors", 751 state->Const.MaxVertexUniformComponents / 4); 752 add_const("gl_MaxFragmentUniformVectors", 753 state->Const.MaxFragmentUniformComponents / 4); 754 755 /* In GLSL ES 3.00, gl_MaxVaryingVectors was split out to separate 756 * vertex and fragment shader constants. 757 */ 758 if (state->is_version(0, 300)) { 759 add_const("gl_MaxVertexOutputVectors", 760 state->ctx->Const.Program[MESA_SHADER_VERTEX].MaxOutputComponents / 4); 761 add_const("gl_MaxFragmentInputVectors", 762 state->ctx->Const.Program[MESA_SHADER_FRAGMENT].MaxInputComponents / 4); 763 } else { 764 add_const("gl_MaxVaryingVectors", 765 state->ctx->Const.MaxVarying); 766 } 767 768 /* EXT_blend_func_extended brings a built in constant 769 * for determining number of dual source draw buffers 770 */ 771 if (state->EXT_blend_func_extended_enable) { 772 add_const("gl_MaxDualSourceDrawBuffersEXT", 773 state->Const.MaxDualSourceDrawBuffers); 774 } 775 } 776 777 /* gl_MaxVaryingFloats was deprecated in GLSL 1.30+, and was moved to 778 * compat profile in GLSL 4.20. GLSL ES never supported this constant. 779 */ 780 if (compatibility || !state->is_version(420, 100)) { 781 add_const("gl_MaxVaryingFloats", state->ctx->Const.MaxVarying * 4); 782 } 783 784 /* Texel offsets were introduced in ARB_shading_language_420pack (which 785 * requires desktop GLSL version 130), and adopted into desktop GLSL 786 * version 4.20 and GLSL ES version 3.00. 787 */ 788 if ((state->is_version(130, 0) && 789 state->ARB_shading_language_420pack_enable) || 790 state->is_version(420, 300)) { 791 add_const("gl_MinProgramTexelOffset", 792 state->Const.MinProgramTexelOffset); 793 add_const("gl_MaxProgramTexelOffset", 794 state->Const.MaxProgramTexelOffset); 795 } 796 797 if (state->has_clip_distance()) { 798 add_const("gl_MaxClipDistances", state->Const.MaxClipPlanes); 799 } 800 if (state->is_version(130, 0)) { 801 add_const("gl_MaxVaryingComponents", state->ctx->Const.MaxVarying * 4); 802 } 803 if (state->has_cull_distance()) { 804 add_const("gl_MaxCullDistances", state->Const.MaxClipPlanes); 805 add_const("gl_MaxCombinedClipAndCullDistances", 806 state->Const.MaxClipPlanes); 807 } 808 809 if (state->has_geometry_shader()) { 810 add_const("gl_MaxVertexOutputComponents", 811 state->Const.MaxVertexOutputComponents); 812 add_const("gl_MaxGeometryInputComponents", 813 state->Const.MaxGeometryInputComponents); 814 add_const("gl_MaxGeometryOutputComponents", 815 state->Const.MaxGeometryOutputComponents); 816 add_const("gl_MaxFragmentInputComponents", 817 state->Const.MaxFragmentInputComponents); 818 add_const("gl_MaxGeometryTextureImageUnits", 819 state->Const.MaxGeometryTextureImageUnits); 820 add_const("gl_MaxGeometryOutputVertices", 821 state->Const.MaxGeometryOutputVertices); 822 add_const("gl_MaxGeometryTotalOutputComponents", 823 state->Const.MaxGeometryTotalOutputComponents); 824 add_const("gl_MaxGeometryUniformComponents", 825 state->Const.MaxGeometryUniformComponents); 826 827 /* Note: the GLSL 1.50-4.40 specs require 828 * gl_MaxGeometryVaryingComponents to be present, and to be at least 64. 829 * But they do not define what it means (and there does not appear to be 830 * any corresponding constant in the GL specs). However, 831 * ARB_geometry_shader4 defines MAX_GEOMETRY_VARYING_COMPONENTS_ARB to 832 * be the maximum number of components available for use as geometry 833 * outputs. So we assume this is a synonym for 834 * gl_MaxGeometryOutputComponents. 835 */ 836 add_const("gl_MaxGeometryVaryingComponents", 837 state->Const.MaxGeometryOutputComponents); 838 } 839 840 if (compatibility) { 841 /* Note: gl_MaxLights stopped being listed as an explicit constant in 842 * GLSL 1.30, however it continues to be referred to (as a minimum size 843 * for compatibility-mode uniforms) all the way up through GLSL 4.30, so 844 * this seems like it was probably an oversight. 845 */ 846 add_const("gl_MaxLights", state->Const.MaxLights); 847 848 add_const("gl_MaxClipPlanes", state->Const.MaxClipPlanes); 849 850 /* Note: gl_MaxTextureUnits wasn't made compatibility-only until GLSL 851 * 1.50, however this seems like it was probably an oversight. 852 */ 853 add_const("gl_MaxTextureUnits", state->Const.MaxTextureUnits); 854 855 /* Note: gl_MaxTextureCoords was left out of GLSL 1.40, but it was 856 * re-introduced in GLSL 1.50, so this seems like it was probably an 857 * oversight. 858 */ 859 add_const("gl_MaxTextureCoords", state->Const.MaxTextureCoords); 860 } 861 862 if (state->has_atomic_counters()) { 863 add_const("gl_MaxVertexAtomicCounters", 864 state->Const.MaxVertexAtomicCounters); 865 add_const("gl_MaxFragmentAtomicCounters", 866 state->Const.MaxFragmentAtomicCounters); 867 add_const("gl_MaxCombinedAtomicCounters", 868 state->Const.MaxCombinedAtomicCounters); 869 add_const("gl_MaxAtomicCounterBindings", 870 state->Const.MaxAtomicBufferBindings); 871 872 if (state->has_geometry_shader()) { 873 add_const("gl_MaxGeometryAtomicCounters", 874 state->Const.MaxGeometryAtomicCounters); 875 } 876 if (state->is_version(110, 320)) { 877 add_const("gl_MaxTessControlAtomicCounters", 878 state->Const.MaxTessControlAtomicCounters); 879 add_const("gl_MaxTessEvaluationAtomicCounters", 880 state->Const.MaxTessEvaluationAtomicCounters); 881 } 882 } 883 884 if (state->is_version(420, 310)) { 885 add_const("gl_MaxVertexAtomicCounterBuffers", 886 state->Const.MaxVertexAtomicCounterBuffers); 887 add_const("gl_MaxFragmentAtomicCounterBuffers", 888 state->Const.MaxFragmentAtomicCounterBuffers); 889 add_const("gl_MaxCombinedAtomicCounterBuffers", 890 state->Const.MaxCombinedAtomicCounterBuffers); 891 add_const("gl_MaxAtomicCounterBufferSize", 892 state->Const.MaxAtomicCounterBufferSize); 893 894 if (state->has_geometry_shader()) { 895 add_const("gl_MaxGeometryAtomicCounterBuffers", 896 state->Const.MaxGeometryAtomicCounterBuffers); 897 } 898 if (state->is_version(110, 320)) { 899 add_const("gl_MaxTessControlAtomicCounterBuffers", 900 state->Const.MaxTessControlAtomicCounterBuffers); 901 add_const("gl_MaxTessEvaluationAtomicCounterBuffers", 902 state->Const.MaxTessEvaluationAtomicCounterBuffers); 903 } 904 } 905 906 if (state->is_version(430, 310) || state->ARB_compute_shader_enable) { 907 add_const("gl_MaxComputeAtomicCounterBuffers", 908 state->Const.MaxComputeAtomicCounterBuffers); 909 add_const("gl_MaxComputeAtomicCounters", 910 state->Const.MaxComputeAtomicCounters); 911 add_const("gl_MaxComputeImageUniforms", 912 state->Const.MaxComputeImageUniforms); 913 add_const("gl_MaxComputeTextureImageUnits", 914 state->Const.MaxComputeTextureImageUnits); 915 add_const("gl_MaxComputeUniformComponents", 916 state->Const.MaxComputeUniformComponents); 917 918 add_const_ivec3("gl_MaxComputeWorkGroupCount", 919 state->Const.MaxComputeWorkGroupCount[0], 920 state->Const.MaxComputeWorkGroupCount[1], 921 state->Const.MaxComputeWorkGroupCount[2]); 922 add_const_ivec3("gl_MaxComputeWorkGroupSize", 923 state->Const.MaxComputeWorkGroupSize[0], 924 state->Const.MaxComputeWorkGroupSize[1], 925 state->Const.MaxComputeWorkGroupSize[2]); 926 927 /* From the GLSL 4.40 spec, section 7.1 (Built-In Language Variables): 928 * 929 * The built-in constant gl_WorkGroupSize is a compute-shader 930 * constant containing the local work-group size of the shader. The 931 * size of the work group in the X, Y, and Z dimensions is stored in 932 * the x, y, and z components. The constants values in 933 * gl_WorkGroupSize will match those specified in the required 934 * local_size_x, local_size_y, and local_size_z layout qualifiers 935 * for the current shader. This is a constant so that it can be 936 * used to size arrays of memory that can be shared within the local 937 * work group. It is a compile-time error to use gl_WorkGroupSize 938 * in a shader that does not declare a fixed local group size, or 939 * before that shader has declared a fixed local group size, using 940 * local_size_x, local_size_y, and local_size_z. 941 * 942 * To prevent the shader from trying to refer to gl_WorkGroupSize before 943 * the layout declaration, we don't define it here. Intead we define it 944 * in ast_cs_input_layout::hir(). 945 */ 946 } 947 948 if (state->has_enhanced_layouts()) { 949 add_const("gl_MaxTransformFeedbackBuffers", 950 state->Const.MaxTransformFeedbackBuffers); 951 add_const("gl_MaxTransformFeedbackInterleavedComponents", 952 state->Const.MaxTransformFeedbackInterleavedComponents); 953 } 954 955 if (state->has_shader_image_load_store()) { 956 add_const("gl_MaxImageUnits", 957 state->Const.MaxImageUnits); 958 add_const("gl_MaxVertexImageUniforms", 959 state->Const.MaxVertexImageUniforms); 960 add_const("gl_MaxFragmentImageUniforms", 961 state->Const.MaxFragmentImageUniforms); 962 add_const("gl_MaxCombinedImageUniforms", 963 state->Const.MaxCombinedImageUniforms); 964 965 if (state->has_geometry_shader()) { 966 add_const("gl_MaxGeometryImageUniforms", 967 state->Const.MaxGeometryImageUniforms); 968 } 969 970 if (!state->es_shader) { 971 add_const("gl_MaxCombinedImageUnitsAndFragmentOutputs", 972 state->Const.MaxCombinedShaderOutputResources); 973 add_const("gl_MaxImageSamples", 974 state->Const.MaxImageSamples); 975 } 976 977 if (state->has_tessellation_shader()) { 978 add_const("gl_MaxTessControlImageUniforms", 979 state->Const.MaxTessControlImageUniforms); 980 add_const("gl_MaxTessEvaluationImageUniforms", 981 state->Const.MaxTessEvaluationImageUniforms); 982 } 983 } 984 985 if (state->is_version(440, 310) || 986 state->ARB_ES3_1_compatibility_enable) { 987 add_const("gl_MaxCombinedShaderOutputResources", 988 state->Const.MaxCombinedShaderOutputResources); 989 } 990 991 if (state->is_version(410, 0) || 992 state->ARB_viewport_array_enable || 993 state->OES_viewport_array_enable) { 994 add_const("gl_MaxViewports", GLSL_PRECISION_HIGH, 995 state->Const.MaxViewports); 996 } 997 998 if (state->has_tessellation_shader()) { 999 add_const("gl_MaxPatchVertices", state->Const.MaxPatchVertices); 1000 add_const("gl_MaxTessGenLevel", state->Const.MaxTessGenLevel); 1001 add_const("gl_MaxTessControlInputComponents", state->Const.MaxTessControlInputComponents); 1002 add_const("gl_MaxTessControlOutputComponents", state->Const.MaxTessControlOutputComponents); 1003 add_const("gl_MaxTessControlTextureImageUnits", state->Const.MaxTessControlTextureImageUnits); 1004 add_const("gl_MaxTessEvaluationInputComponents", state->Const.MaxTessEvaluationInputComponents); 1005 add_const("gl_MaxTessEvaluationOutputComponents", state->Const.MaxTessEvaluationOutputComponents); 1006 add_const("gl_MaxTessEvaluationTextureImageUnits", state->Const.MaxTessEvaluationTextureImageUnits); 1007 add_const("gl_MaxTessPatchComponents", state->Const.MaxTessPatchComponents); 1008 add_const("gl_MaxTessControlTotalOutputComponents", state->Const.MaxTessControlTotalOutputComponents); 1009 add_const("gl_MaxTessControlUniformComponents", state->Const.MaxTessControlUniformComponents); 1010 add_const("gl_MaxTessEvaluationUniformComponents", state->Const.MaxTessEvaluationUniformComponents); 1011 } 1012 1013 if (state->is_version(450, 320) || 1014 state->OES_sample_variables_enable || 1015 state->ARB_ES3_1_compatibility_enable) 1016 add_const("gl_MaxSamples", state->Const.MaxSamples); 1017} 1018 1019 1020/** 1021 * Generate uniform variables (which exist in all types of shaders). 1022 */ 1023void 1024builtin_variable_generator::generate_uniforms() 1025{ 1026 if (state->is_version(400, 320) || 1027 state->ARB_sample_shading_enable || 1028 state->OES_sample_variables_enable) 1029 add_uniform(int_t, GLSL_PRECISION_LOW, "gl_NumSamples"); 1030 add_uniform(type("gl_DepthRangeParameters"), "gl_DepthRange"); 1031 1032 for (unsigned i = 0; i < VARYING_SLOT_VAR0; i++) { 1033 char name[128]; 1034 1035 snprintf(name, sizeof(name), "gl_CurrentAttribFrag%uMESA", i); 1036 add_uniform(vec4_t, name); 1037 } 1038 1039 if (compatibility) { 1040 add_uniform(mat4_t, "gl_ModelViewMatrix"); 1041 add_uniform(mat4_t, "gl_ProjectionMatrix"); 1042 add_uniform(mat4_t, "gl_ModelViewProjectionMatrix"); 1043 add_uniform(mat3_t, "gl_NormalMatrix"); 1044 add_uniform(mat4_t, "gl_ModelViewMatrixInverse"); 1045 add_uniform(mat4_t, "gl_ProjectionMatrixInverse"); 1046 add_uniform(mat4_t, "gl_ModelViewProjectionMatrixInverse"); 1047 add_uniform(mat4_t, "gl_ModelViewMatrixTranspose"); 1048 add_uniform(mat4_t, "gl_ProjectionMatrixTranspose"); 1049 add_uniform(mat4_t, "gl_ModelViewProjectionMatrixTranspose"); 1050 add_uniform(mat4_t, "gl_ModelViewMatrixInverseTranspose"); 1051 add_uniform(mat4_t, "gl_ProjectionMatrixInverseTranspose"); 1052 add_uniform(mat4_t, "gl_ModelViewProjectionMatrixInverseTranspose"); 1053 add_uniform(float_t, "gl_NormalScale"); 1054 add_uniform(type("gl_LightModelParameters"), "gl_LightModel"); 1055 add_uniform(vec4_t, "gl_FogParamsOptimizedMESA"); 1056 1057 const glsl_type *const mat4_array_type = 1058 array(mat4_t, state->Const.MaxTextureCoords); 1059 add_uniform(mat4_array_type, "gl_TextureMatrix"); 1060 add_uniform(mat4_array_type, "gl_TextureMatrixInverse"); 1061 add_uniform(mat4_array_type, "gl_TextureMatrixTranspose"); 1062 add_uniform(mat4_array_type, "gl_TextureMatrixInverseTranspose"); 1063 1064 add_uniform(array(vec4_t, state->Const.MaxClipPlanes), "gl_ClipPlane"); 1065 add_uniform(type("gl_PointParameters"), "gl_Point"); 1066 1067 const glsl_type *const material_parameters_type = 1068 type("gl_MaterialParameters"); 1069 add_uniform(material_parameters_type, "gl_FrontMaterial"); 1070 add_uniform(material_parameters_type, "gl_BackMaterial"); 1071 1072 add_uniform(array(type("gl_LightSourceParameters"), 1073 state->Const.MaxLights), 1074 "gl_LightSource"); 1075 1076 const glsl_type *const light_model_products_type = 1077 type("gl_LightModelProducts"); 1078 add_uniform(light_model_products_type, "gl_FrontLightModelProduct"); 1079 add_uniform(light_model_products_type, "gl_BackLightModelProduct"); 1080 1081 const glsl_type *const light_products_type = 1082 array(type("gl_LightProducts"), state->Const.MaxLights); 1083 add_uniform(light_products_type, "gl_FrontLightProduct"); 1084 add_uniform(light_products_type, "gl_BackLightProduct"); 1085 1086 add_uniform(array(vec4_t, state->Const.MaxTextureUnits), 1087 "gl_TextureEnvColor"); 1088 1089 const glsl_type *const texcoords_vec4 = 1090 array(vec4_t, state->Const.MaxTextureCoords); 1091 add_uniform(texcoords_vec4, "gl_EyePlaneS"); 1092 add_uniform(texcoords_vec4, "gl_EyePlaneT"); 1093 add_uniform(texcoords_vec4, "gl_EyePlaneR"); 1094 add_uniform(texcoords_vec4, "gl_EyePlaneQ"); 1095 add_uniform(texcoords_vec4, "gl_ObjectPlaneS"); 1096 add_uniform(texcoords_vec4, "gl_ObjectPlaneT"); 1097 add_uniform(texcoords_vec4, "gl_ObjectPlaneR"); 1098 add_uniform(texcoords_vec4, "gl_ObjectPlaneQ"); 1099 1100 add_uniform(type("gl_FogParameters"), "gl_Fog"); 1101 } 1102} 1103 1104 1105/** 1106 * Generate special variables which exist in all shaders. 1107 */ 1108void 1109builtin_variable_generator::generate_special_vars() 1110{ 1111 if (state->ARB_shader_ballot_enable) { 1112 add_system_value(SYSTEM_VALUE_SUBGROUP_SIZE, uint_t, "gl_SubGroupSizeARB"); 1113 add_system_value(SYSTEM_VALUE_SUBGROUP_INVOCATION, uint_t, "gl_SubGroupInvocationARB"); 1114 add_system_value(SYSTEM_VALUE_SUBGROUP_EQ_MASK, uint64_t, "gl_SubGroupEqMaskARB"); 1115 add_system_value(SYSTEM_VALUE_SUBGROUP_GE_MASK, uint64_t, "gl_SubGroupGeMaskARB"); 1116 add_system_value(SYSTEM_VALUE_SUBGROUP_GT_MASK, uint64_t, "gl_SubGroupGtMaskARB"); 1117 add_system_value(SYSTEM_VALUE_SUBGROUP_LE_MASK, uint64_t, "gl_SubGroupLeMaskARB"); 1118 add_system_value(SYSTEM_VALUE_SUBGROUP_LT_MASK, uint64_t, "gl_SubGroupLtMaskARB"); 1119 } 1120} 1121 1122 1123/** 1124 * Generate variables which only exist in vertex shaders. 1125 */ 1126void 1127builtin_variable_generator::generate_vs_special_vars() 1128{ 1129 if (state->is_version(130, 300) || state->EXT_gpu_shader4_enable) { 1130 add_system_value(SYSTEM_VALUE_VERTEX_ID, int_t, GLSL_PRECISION_HIGH, 1131 "gl_VertexID"); 1132 } 1133 if (state->is_version(460, 0)) { 1134 add_system_value(SYSTEM_VALUE_BASE_VERTEX, int_t, "gl_BaseVertex"); 1135 add_system_value(SYSTEM_VALUE_BASE_INSTANCE, int_t, "gl_BaseInstance"); 1136 add_system_value(SYSTEM_VALUE_DRAW_ID, int_t, "gl_DrawID"); 1137 } 1138 if (state->EXT_draw_instanced_enable && state->is_version(0, 100)) 1139 add_system_value(SYSTEM_VALUE_INSTANCE_ID, int_t, GLSL_PRECISION_HIGH, 1140 "gl_InstanceIDEXT"); 1141 1142 if (state->ARB_draw_instanced_enable) 1143 add_system_value(SYSTEM_VALUE_INSTANCE_ID, int_t, "gl_InstanceIDARB"); 1144 1145 if (state->ARB_draw_instanced_enable || state->is_version(140, 300) || 1146 state->EXT_gpu_shader4_enable) { 1147 add_system_value(SYSTEM_VALUE_INSTANCE_ID, int_t, GLSL_PRECISION_HIGH, 1148 "gl_InstanceID"); 1149 } 1150 if (state->ARB_shader_draw_parameters_enable) { 1151 add_system_value(SYSTEM_VALUE_BASE_VERTEX, int_t, "gl_BaseVertexARB"); 1152 add_system_value(SYSTEM_VALUE_BASE_INSTANCE, int_t, "gl_BaseInstanceARB"); 1153 add_system_value(SYSTEM_VALUE_DRAW_ID, int_t, "gl_DrawIDARB"); 1154 } 1155 if (compatibility) { 1156 add_input(VERT_ATTRIB_POS, vec4_t, "gl_Vertex"); 1157 add_input(VERT_ATTRIB_NORMAL, vec3_t, "gl_Normal"); 1158 add_input(VERT_ATTRIB_COLOR0, vec4_t, "gl_Color"); 1159 add_input(VERT_ATTRIB_COLOR1, vec4_t, "gl_SecondaryColor"); 1160 add_input(VERT_ATTRIB_TEX0, vec4_t, "gl_MultiTexCoord0"); 1161 add_input(VERT_ATTRIB_TEX1, vec4_t, "gl_MultiTexCoord1"); 1162 add_input(VERT_ATTRIB_TEX2, vec4_t, "gl_MultiTexCoord2"); 1163 add_input(VERT_ATTRIB_TEX3, vec4_t, "gl_MultiTexCoord3"); 1164 add_input(VERT_ATTRIB_TEX4, vec4_t, "gl_MultiTexCoord4"); 1165 add_input(VERT_ATTRIB_TEX5, vec4_t, "gl_MultiTexCoord5"); 1166 add_input(VERT_ATTRIB_TEX6, vec4_t, "gl_MultiTexCoord6"); 1167 add_input(VERT_ATTRIB_TEX7, vec4_t, "gl_MultiTexCoord7"); 1168 add_input(VERT_ATTRIB_FOG, float_t, "gl_FogCoord"); 1169 } 1170} 1171 1172 1173/** 1174 * Generate variables which only exist in tessellation control shaders. 1175 */ 1176void 1177builtin_variable_generator::generate_tcs_special_vars() 1178{ 1179 add_system_value(SYSTEM_VALUE_PRIMITIVE_ID, int_t, GLSL_PRECISION_HIGH, 1180 "gl_PrimitiveID"); 1181 add_system_value(SYSTEM_VALUE_INVOCATION_ID, int_t, GLSL_PRECISION_HIGH, 1182 "gl_InvocationID"); 1183 add_system_value(SYSTEM_VALUE_VERTICES_IN, int_t, GLSL_PRECISION_HIGH, 1184 "gl_PatchVerticesIn"); 1185 1186 add_output(VARYING_SLOT_TESS_LEVEL_OUTER, array(float_t, 4), 1187 GLSL_PRECISION_HIGH, "gl_TessLevelOuter")->data.patch = 1; 1188 add_output(VARYING_SLOT_TESS_LEVEL_INNER, array(float_t, 2), 1189 GLSL_PRECISION_HIGH, "gl_TessLevelInner")->data.patch = 1; 1190 /* XXX What to do if multiple are flipped on? */ 1191 int bbox_slot = state->ctx->Const.NoPrimitiveBoundingBoxOutput ? -1 : 1192 VARYING_SLOT_BOUNDING_BOX0; 1193 if (state->EXT_primitive_bounding_box_enable) 1194 add_output(bbox_slot, array(vec4_t, 2), "gl_BoundingBoxEXT") 1195 ->data.patch = 1; 1196 if (state->OES_primitive_bounding_box_enable) { 1197 add_output(bbox_slot, array(vec4_t, 2), GLSL_PRECISION_HIGH, 1198 "gl_BoundingBoxOES")->data.patch = 1; 1199 } 1200 if (state->is_version(0, 320) || state->ARB_ES3_2_compatibility_enable) { 1201 add_output(bbox_slot, array(vec4_t, 2), GLSL_PRECISION_HIGH, 1202 "gl_BoundingBox")->data.patch = 1; 1203 } 1204 1205 /* NOTE: These are completely pointless. Writing these will never go 1206 * anywhere. But the specs demands it. So we add them with a slot of -1, 1207 * which makes the data go nowhere. 1208 */ 1209 if (state->NV_viewport_array2_enable) { 1210 add_output(-1, int_t, "gl_Layer"); 1211 add_output(-1, int_t, "gl_ViewportIndex"); 1212 add_output(-1, array(int_t, 1), "gl_ViewportMask"); 1213 } 1214 1215} 1216 1217 1218/** 1219 * Generate variables which only exist in tessellation evaluation shaders. 1220 */ 1221void 1222builtin_variable_generator::generate_tes_special_vars() 1223{ 1224 ir_variable *var; 1225 1226 add_system_value(SYSTEM_VALUE_PRIMITIVE_ID, int_t, GLSL_PRECISION_HIGH, 1227 "gl_PrimitiveID"); 1228 add_system_value(SYSTEM_VALUE_VERTICES_IN, int_t, GLSL_PRECISION_HIGH, 1229 "gl_PatchVerticesIn"); 1230 add_system_value(SYSTEM_VALUE_TESS_COORD, vec3_t, GLSL_PRECISION_HIGH, 1231 "gl_TessCoord"); 1232 if (this->state->ctx->Const.GLSLTessLevelsAsInputs) { 1233 add_input(VARYING_SLOT_TESS_LEVEL_OUTER, array(float_t, 4), 1234 GLSL_PRECISION_HIGH, "gl_TessLevelOuter")->data.patch = 1; 1235 add_input(VARYING_SLOT_TESS_LEVEL_INNER, array(float_t, 2), 1236 GLSL_PRECISION_HIGH, "gl_TessLevelInner")->data.patch = 1; 1237 } else { 1238 add_system_value(SYSTEM_VALUE_TESS_LEVEL_OUTER, array(float_t, 4), 1239 GLSL_PRECISION_HIGH, "gl_TessLevelOuter"); 1240 add_system_value(SYSTEM_VALUE_TESS_LEVEL_INNER, array(float_t, 2), 1241 GLSL_PRECISION_HIGH, "gl_TessLevelInner"); 1242 } 1243 if (state->ARB_shader_viewport_layer_array_enable || 1244 state->NV_viewport_array2_enable) { 1245 var = add_output(VARYING_SLOT_LAYER, int_t, "gl_Layer"); 1246 var->data.interpolation = INTERP_MODE_FLAT; 1247 var = add_output(VARYING_SLOT_VIEWPORT, int_t, "gl_ViewportIndex"); 1248 var->data.interpolation = INTERP_MODE_FLAT; 1249 } 1250 if (state->NV_viewport_array2_enable) { 1251 var = add_output(VARYING_SLOT_VIEWPORT_MASK, array(int_t, 1), 1252 "gl_ViewportMask"); 1253 var->data.interpolation = INTERP_MODE_FLAT; 1254 } 1255} 1256 1257 1258/** 1259 * Generate variables which only exist in geometry shaders. 1260 */ 1261void 1262builtin_variable_generator::generate_gs_special_vars() 1263{ 1264 ir_variable *var; 1265 1266 var = add_output(VARYING_SLOT_LAYER, int_t, GLSL_PRECISION_HIGH, "gl_Layer"); 1267 var->data.interpolation = INTERP_MODE_FLAT; 1268 if (state->is_version(410, 0) || state->ARB_viewport_array_enable || 1269 state->OES_viewport_array_enable) { 1270 var = add_output(VARYING_SLOT_VIEWPORT, int_t, GLSL_PRECISION_HIGH, 1271 "gl_ViewportIndex"); 1272 var->data.interpolation = INTERP_MODE_FLAT; 1273 } 1274 if (state->NV_viewport_array2_enable) { 1275 var = add_output(VARYING_SLOT_VIEWPORT_MASK, array(int_t, 1), 1276 "gl_ViewportMask"); 1277 var->data.interpolation = INTERP_MODE_FLAT; 1278 } 1279 if (state->is_version(400, 320) || state->ARB_gpu_shader5_enable || 1280 state->OES_geometry_shader_enable || state->EXT_geometry_shader_enable) { 1281 add_system_value(SYSTEM_VALUE_INVOCATION_ID, int_t, GLSL_PRECISION_HIGH, 1282 "gl_InvocationID"); 1283 } 1284 1285 /* Although gl_PrimitiveID appears in tessellation control and tessellation 1286 * evaluation shaders, it has a different function there than it has in 1287 * geometry shaders, so we treat it (and its counterpart gl_PrimitiveIDIn) 1288 * as special geometry shader variables. 1289 * 1290 * Note that although the general convention of suffixing geometry shader 1291 * input varyings with "In" was not adopted into GLSL 1.50, it is used in 1292 * the specific case of gl_PrimitiveIDIn. So we don't need to treat 1293 * gl_PrimitiveIDIn as an {ARB,EXT}_geometry_shader4-only variable. 1294 */ 1295 var = add_input(VARYING_SLOT_PRIMITIVE_ID, int_t, GLSL_PRECISION_HIGH, 1296 "gl_PrimitiveIDIn"); 1297 var->data.interpolation = INTERP_MODE_FLAT; 1298 var = add_output(VARYING_SLOT_PRIMITIVE_ID, int_t, GLSL_PRECISION_HIGH, 1299 "gl_PrimitiveID"); 1300 var->data.interpolation = INTERP_MODE_FLAT; 1301} 1302 1303 1304/** 1305 * Generate variables which only exist in fragment shaders. 1306 */ 1307void 1308builtin_variable_generator::generate_fs_special_vars() 1309{ 1310 ir_variable *var; 1311 1312 int frag_coord_precision = (state->is_version(0, 300) ? 1313 GLSL_PRECISION_HIGH : 1314 GLSL_PRECISION_MEDIUM); 1315 1316 if (this->state->ctx->Const.GLSLFragCoordIsSysVal) { 1317 add_system_value(SYSTEM_VALUE_FRAG_COORD, vec4_t, frag_coord_precision, 1318 "gl_FragCoord"); 1319 } else { 1320 add_input(VARYING_SLOT_POS, vec4_t, frag_coord_precision, "gl_FragCoord"); 1321 } 1322 1323 if (this->state->ctx->Const.GLSLFrontFacingIsSysVal) { 1324 var = add_system_value(SYSTEM_VALUE_FRONT_FACE, bool_t, "gl_FrontFacing"); 1325 var->data.interpolation = INTERP_MODE_FLAT; 1326 } else { 1327 var = add_input(VARYING_SLOT_FACE, bool_t, "gl_FrontFacing"); 1328 var->data.interpolation = INTERP_MODE_FLAT; 1329 } 1330 1331 if (state->is_version(120, 100)) { 1332 if (this->state->ctx->Const.GLSLPointCoordIsSysVal) 1333 add_system_value(SYSTEM_VALUE_POINT_COORD, vec2_t, 1334 GLSL_PRECISION_MEDIUM, "gl_PointCoord"); 1335 else 1336 add_input(VARYING_SLOT_PNTC, vec2_t, GLSL_PRECISION_MEDIUM, 1337 "gl_PointCoord"); 1338 } 1339 1340 if (state->has_geometry_shader() || state->EXT_gpu_shader4_enable) { 1341 var = add_input(VARYING_SLOT_PRIMITIVE_ID, int_t, GLSL_PRECISION_HIGH, 1342 "gl_PrimitiveID"); 1343 var->data.interpolation = INTERP_MODE_FLAT; 1344 } 1345 1346 /* gl_FragColor and gl_FragData were deprecated starting in desktop GLSL 1347 * 1.30, and were relegated to the compatibility profile in GLSL 4.20. 1348 * They were removed from GLSL ES 3.00. 1349 */ 1350 if (compatibility || !state->is_version(420, 300)) { 1351 add_output(FRAG_RESULT_COLOR, vec4_t, GLSL_PRECISION_MEDIUM, 1352 "gl_FragColor"); 1353 add_output(FRAG_RESULT_DATA0, 1354 array(vec4_t, state->Const.MaxDrawBuffers), 1355 GLSL_PRECISION_MEDIUM, 1356 "gl_FragData"); 1357 } 1358 1359 if (state->has_framebuffer_fetch() && !state->is_version(130, 300)) { 1360 ir_variable *const var = 1361 add_output(FRAG_RESULT_DATA0, 1362 array(vec4_t, state->Const.MaxDrawBuffers), 1363 "gl_LastFragData"); 1364 var->data.precision = GLSL_PRECISION_MEDIUM; 1365 var->data.read_only = 1; 1366 var->data.fb_fetch_output = 1; 1367 var->data.memory_coherent = 1; 1368 } 1369 1370 if (state->es_shader && state->language_version == 100 && state->EXT_blend_func_extended_enable) { 1371 add_index_output(FRAG_RESULT_COLOR, 1, vec4_t, 1372 GLSL_PRECISION_MEDIUM, "gl_SecondaryFragColorEXT"); 1373 add_index_output(FRAG_RESULT_DATA0, 1, 1374 array(vec4_t, state->Const.MaxDualSourceDrawBuffers), 1375 GLSL_PRECISION_MEDIUM, "gl_SecondaryFragDataEXT"); 1376 } 1377 1378 /* gl_FragDepth has always been in desktop GLSL, but did not appear in GLSL 1379 * ES 1.00. 1380 */ 1381 if (state->is_version(110, 300)) { 1382 add_output(FRAG_RESULT_DEPTH, float_t, GLSL_PRECISION_HIGH, 1383 "gl_FragDepth"); 1384 } 1385 1386 if (state->EXT_frag_depth_enable) 1387 add_output(FRAG_RESULT_DEPTH, float_t, "gl_FragDepthEXT"); 1388 1389 if (state->ARB_shader_stencil_export_enable) { 1390 ir_variable *const var = 1391 add_output(FRAG_RESULT_STENCIL, int_t, "gl_FragStencilRefARB"); 1392 if (state->ARB_shader_stencil_export_warn) 1393 var->enable_extension_warning("GL_ARB_shader_stencil_export"); 1394 } 1395 1396 if (state->AMD_shader_stencil_export_enable) { 1397 ir_variable *const var = 1398 add_output(FRAG_RESULT_STENCIL, int_t, "gl_FragStencilRefAMD"); 1399 if (state->AMD_shader_stencil_export_warn) 1400 var->enable_extension_warning("GL_AMD_shader_stencil_export"); 1401 } 1402 1403 if (state->is_version(400, 320) || 1404 state->ARB_sample_shading_enable || 1405 state->OES_sample_variables_enable) { 1406 add_system_value(SYSTEM_VALUE_SAMPLE_ID, int_t, GLSL_PRECISION_LOW, 1407 "gl_SampleID"); 1408 add_system_value(SYSTEM_VALUE_SAMPLE_POS, vec2_t, GLSL_PRECISION_MEDIUM, 1409 "gl_SamplePosition"); 1410 /* From the ARB_sample_shading specification: 1411 * "The number of elements in the array is ceil(<s>/32), where 1412 * <s> is the maximum number of color samples supported by the 1413 * implementation." 1414 * Since no drivers expose more than 32x MSAA, we can simply set 1415 * the array size to 1 rather than computing it. 1416 */ 1417 add_output(FRAG_RESULT_SAMPLE_MASK, array(int_t, 1), 1418 GLSL_PRECISION_HIGH, "gl_SampleMask"); 1419 } 1420 1421 if (state->is_version(400, 320) || 1422 state->ARB_gpu_shader5_enable || 1423 state->OES_sample_variables_enable) { 1424 add_system_value(SYSTEM_VALUE_SAMPLE_MASK_IN, array(int_t, 1), 1425 GLSL_PRECISION_HIGH, "gl_SampleMaskIn"); 1426 } 1427 1428 if (state->is_version(430, 320) || 1429 state->ARB_fragment_layer_viewport_enable || 1430 state->OES_geometry_shader_enable || 1431 state->EXT_geometry_shader_enable) { 1432 add_varying(VARYING_SLOT_LAYER, int_t, GLSL_PRECISION_HIGH, 1433 "gl_Layer", INTERP_MODE_FLAT); 1434 } 1435 1436 if (state->is_version(430, 0) || 1437 state->ARB_fragment_layer_viewport_enable || 1438 state->OES_viewport_array_enable) { 1439 add_varying(VARYING_SLOT_VIEWPORT, int_t, "gl_ViewportIndex", INTERP_MODE_FLAT); 1440 } 1441 1442 if (state->is_version(450, 310) || state->ARB_ES3_1_compatibility_enable) 1443 add_system_value(SYSTEM_VALUE_HELPER_INVOCATION, bool_t, "gl_HelperInvocation"); 1444} 1445 1446 1447/** 1448 * Generate variables which only exist in compute shaders. 1449 */ 1450void 1451builtin_variable_generator::generate_cs_special_vars() 1452{ 1453 add_system_value(SYSTEM_VALUE_LOCAL_INVOCATION_ID, uvec3_t, 1454 "gl_LocalInvocationID"); 1455 add_system_value(SYSTEM_VALUE_WORKGROUP_ID, uvec3_t, "gl_WorkGroupID"); 1456 add_system_value(SYSTEM_VALUE_NUM_WORKGROUPS, uvec3_t, "gl_NumWorkGroups"); 1457 1458 if (state->ARB_compute_variable_group_size_enable) { 1459 add_system_value(SYSTEM_VALUE_WORKGROUP_SIZE, 1460 uvec3_t, "gl_LocalGroupSizeARB"); 1461 } 1462 1463 add_system_value(SYSTEM_VALUE_GLOBAL_INVOCATION_ID, 1464 uvec3_t, "gl_GlobalInvocationID"); 1465 add_system_value(SYSTEM_VALUE_LOCAL_INVOCATION_INDEX, 1466 uint_t, "gl_LocalInvocationIndex"); 1467} 1468 1469 1470/** 1471 * Add a single "varying" variable. The variable's type and direction (input 1472 * or output) are adjusted as appropriate for the type of shader being 1473 * compiled. 1474 */ 1475void 1476builtin_variable_generator::add_varying(int slot, const glsl_type *type, 1477 int precision, const char *name, 1478 enum glsl_interp_mode interp) 1479{ 1480 switch (state->stage) { 1481 case MESA_SHADER_TESS_CTRL: 1482 case MESA_SHADER_TESS_EVAL: 1483 case MESA_SHADER_GEOMETRY: 1484 this->per_vertex_in.add_field(slot, type, precision, name, interp); 1485 FALLTHROUGH; 1486 case MESA_SHADER_VERTEX: 1487 this->per_vertex_out.add_field(slot, type, precision, name, interp); 1488 break; 1489 case MESA_SHADER_FRAGMENT: 1490 add_input(slot, type, precision, name, interp); 1491 break; 1492 case MESA_SHADER_COMPUTE: 1493 /* Compute shaders don't have varyings. */ 1494 break; 1495 default: 1496 break; 1497 } 1498} 1499 1500 1501/** 1502 * Generate variables that are used to communicate data from one shader stage 1503 * to the next ("varyings"). 1504 */ 1505void 1506builtin_variable_generator::generate_varyings() 1507{ 1508 struct gl_shader_compiler_options *options = 1509 &state->ctx->Const.ShaderCompilerOptions[state->stage]; 1510 1511 /* gl_Position and gl_PointSize are not visible from fragment shaders. */ 1512 if (state->stage != MESA_SHADER_FRAGMENT) { 1513 add_varying(VARYING_SLOT_POS, vec4_t, GLSL_PRECISION_HIGH, "gl_Position"); 1514 if (!state->es_shader || 1515 state->stage == MESA_SHADER_VERTEX || 1516 (state->stage == MESA_SHADER_GEOMETRY && 1517 (state->OES_geometry_point_size_enable || 1518 state->EXT_geometry_point_size_enable)) || 1519 ((state->stage == MESA_SHADER_TESS_CTRL || 1520 state->stage == MESA_SHADER_TESS_EVAL) && 1521 (state->OES_tessellation_point_size_enable || 1522 state->EXT_tessellation_point_size_enable))) { 1523 add_varying(VARYING_SLOT_PSIZ, 1524 float_t, 1525 state->is_version(0, 300) ? 1526 GLSL_PRECISION_HIGH : 1527 GLSL_PRECISION_MEDIUM, 1528 "gl_PointSize"); 1529 } 1530 if (state->stage == MESA_SHADER_VERTEX) { 1531 if (state->AMD_vertex_shader_viewport_index_enable || 1532 state->ARB_shader_viewport_layer_array_enable || 1533 state->NV_viewport_array2_enable) { 1534 add_varying(VARYING_SLOT_VIEWPORT, int_t, "gl_ViewportIndex", INTERP_MODE_FLAT); 1535 } 1536 1537 if (state->AMD_vertex_shader_layer_enable || 1538 state->ARB_shader_viewport_layer_array_enable || 1539 state->NV_viewport_array2_enable) { 1540 add_varying(VARYING_SLOT_LAYER, int_t, GLSL_PRECISION_HIGH, 1541 "gl_Layer", INTERP_MODE_FLAT); 1542 } 1543 1544 /* From the NV_viewport_array2 specification: 1545 * 1546 * "The variable gl_ViewportMask[] is available as an output variable 1547 * in the VTG languages. The array has ceil(v/32) elements where v is 1548 * the maximum number of viewports supported by the implementation." 1549 * 1550 * Since no drivers expose more than 16 viewports, we can simply set the 1551 * array size to 1 rather than computing it and dealing with varying 1552 * slot complication. 1553 */ 1554 if (state->NV_viewport_array2_enable) 1555 add_varying(VARYING_SLOT_VIEWPORT_MASK, array(int_t, 1), 1556 "gl_ViewportMask", INTERP_MODE_FLAT); 1557 } 1558 } 1559 1560 if (state->has_clip_distance()) { 1561 add_varying(VARYING_SLOT_CLIP_DIST0, array(float_t, 0), 1562 GLSL_PRECISION_HIGH, "gl_ClipDistance"); 1563 } 1564 if (state->has_cull_distance()) { 1565 add_varying(VARYING_SLOT_CULL_DIST0, array(float_t, 0), 1566 GLSL_PRECISION_HIGH, "gl_CullDistance"); 1567 } 1568 1569 if (compatibility) { 1570 add_varying(VARYING_SLOT_TEX0, array(vec4_t, 0), "gl_TexCoord"); 1571 add_varying(VARYING_SLOT_FOGC, float_t, "gl_FogFragCoord"); 1572 if (state->stage == MESA_SHADER_FRAGMENT) { 1573 add_varying(VARYING_SLOT_COL0, vec4_t, "gl_Color"); 1574 add_varying(VARYING_SLOT_COL1, vec4_t, "gl_SecondaryColor"); 1575 } else { 1576 add_varying(VARYING_SLOT_CLIP_VERTEX, vec4_t, "gl_ClipVertex"); 1577 add_varying(VARYING_SLOT_COL0, vec4_t, "gl_FrontColor"); 1578 add_varying(VARYING_SLOT_BFC0, vec4_t, "gl_BackColor"); 1579 add_varying(VARYING_SLOT_COL1, vec4_t, "gl_FrontSecondaryColor"); 1580 add_varying(VARYING_SLOT_BFC1, vec4_t, "gl_BackSecondaryColor"); 1581 } 1582 } 1583 1584 /* Section 7.1 (Built-In Language Variables) of the GLSL 4.00 spec 1585 * says: 1586 * 1587 * "In the tessellation control language, built-in variables are 1588 * intrinsically declared as: 1589 * 1590 * in gl_PerVertex { 1591 * vec4 gl_Position; 1592 * float gl_PointSize; 1593 * float gl_ClipDistance[]; 1594 * } gl_in[gl_MaxPatchVertices];" 1595 */ 1596 if (state->stage == MESA_SHADER_TESS_CTRL || 1597 state->stage == MESA_SHADER_TESS_EVAL) { 1598 const glsl_type *per_vertex_in_type = 1599 this->per_vertex_in.construct_interface_instance(); 1600 add_variable("gl_in", array(per_vertex_in_type, state->Const.MaxPatchVertices), 1601 GLSL_PRECISION_NONE, ir_var_shader_in, -1); 1602 } 1603 if (state->stage == MESA_SHADER_GEOMETRY) { 1604 const glsl_type *per_vertex_in_type = 1605 this->per_vertex_in.construct_interface_instance(); 1606 add_variable("gl_in", array(per_vertex_in_type, 0), 1607 GLSL_PRECISION_NONE, ir_var_shader_in, -1); 1608 } 1609 if (state->stage == MESA_SHADER_TESS_CTRL) { 1610 const glsl_type *per_vertex_out_type = 1611 this->per_vertex_out.construct_interface_instance(); 1612 add_variable("gl_out", array(per_vertex_out_type, 0), 1613 GLSL_PRECISION_NONE, ir_var_shader_out, -1); 1614 } 1615 if (state->stage == MESA_SHADER_VERTEX || 1616 state->stage == MESA_SHADER_TESS_EVAL || 1617 state->stage == MESA_SHADER_GEOMETRY) { 1618 const glsl_type *per_vertex_out_type = 1619 this->per_vertex_out.construct_interface_instance(); 1620 const glsl_struct_field *fields = per_vertex_out_type->fields.structure; 1621 for (unsigned i = 0; i < per_vertex_out_type->length; i++) { 1622 ir_variable *var = 1623 add_variable(fields[i].name, fields[i].type, fields[i].precision, 1624 ir_var_shader_out, fields[i].location); 1625 var->data.interpolation = fields[i].interpolation; 1626 var->data.centroid = fields[i].centroid; 1627 var->data.sample = fields[i].sample; 1628 var->data.patch = fields[i].patch; 1629 var->init_interface_type(per_vertex_out_type); 1630 1631 var->data.invariant = fields[i].location == VARYING_SLOT_POS && 1632 options->PositionAlwaysInvariant; 1633 1634 var->data.precise = fields[i].location == VARYING_SLOT_POS && 1635 options->PositionAlwaysPrecise; 1636 } 1637 } 1638} 1639 1640 1641}; /* Anonymous namespace */ 1642 1643 1644void 1645_mesa_glsl_initialize_variables(exec_list *instructions, 1646 struct _mesa_glsl_parse_state *state) 1647{ 1648 builtin_variable_generator gen(instructions, state); 1649 1650 gen.generate_constants(); 1651 gen.generate_uniforms(); 1652 gen.generate_special_vars(); 1653 1654 gen.generate_varyings(); 1655 1656 switch (state->stage) { 1657 case MESA_SHADER_VERTEX: 1658 gen.generate_vs_special_vars(); 1659 break; 1660 case MESA_SHADER_TESS_CTRL: 1661 gen.generate_tcs_special_vars(); 1662 break; 1663 case MESA_SHADER_TESS_EVAL: 1664 gen.generate_tes_special_vars(); 1665 break; 1666 case MESA_SHADER_GEOMETRY: 1667 gen.generate_gs_special_vars(); 1668 break; 1669 case MESA_SHADER_FRAGMENT: 1670 gen.generate_fs_special_vars(); 1671 break; 1672 case MESA_SHADER_COMPUTE: 1673 gen.generate_cs_special_vars(); 1674 break; 1675 default: 1676 break; 1677 } 1678} 1679