builtin_variables.cpp 39 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075
  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. #include "ir.h"
  24. #include "glsl_parser_extras.h"
  25. #include "glsl_symbol_table.h"
  26. #include "main/core.h"
  27. #include "program/prog_parameter.h"
  28. #include "program/prog_statevars.h"
  29. #include "program/prog_instruction.h"
  30. struct gl_builtin_uniform_element {
  31. const char *field;
  32. int tokens[STATE_LENGTH];
  33. int swizzle;
  34. };
  35. struct gl_builtin_uniform_desc {
  36. const char *name;
  37. struct gl_builtin_uniform_element *elements;
  38. unsigned int num_elements;
  39. };
  40. static void generate_ARB_draw_buffers_variables(exec_list *,
  41. struct _mesa_glsl_parse_state *,
  42. bool, _mesa_glsl_parser_targets);
  43. static void
  44. generate_ARB_draw_instanced_variables(exec_list *,
  45. struct _mesa_glsl_parse_state *,
  46. bool, _mesa_glsl_parser_targets);
  47. struct builtin_variable {
  48. enum ir_variable_mode mode;
  49. int slot;
  50. const char *type;
  51. const char *name;
  52. glsl_precision prec;
  53. };
  54. static const builtin_variable builtin_core_vs_variables[] = {
  55. { ir_var_out, VERT_RESULT_HPOS, "vec4", "gl_Position", glsl_precision_high },
  56. { ir_var_out, VERT_RESULT_PSIZ, "float", "gl_PointSize", glsl_precision_medium },
  57. };
  58. static const builtin_variable builtin_core_fs_variables[] = {
  59. { ir_var_in, FRAG_ATTRIB_WPOS, "vec4", "gl_FragCoord", glsl_precision_medium },
  60. { ir_var_in, FRAG_ATTRIB_FACE, "bool", "gl_FrontFacing", glsl_precision_low },
  61. { ir_var_out, FRAG_RESULT_COLOR, "vec4", "gl_FragColor", glsl_precision_medium },
  62. };
  63. static const builtin_variable builtin_100ES_fs_variables[] = {
  64. { ir_var_in, FRAG_ATTRIB_PNTC, "vec2", "gl_PointCoord", glsl_precision_medium },
  65. };
  66. static const builtin_variable builtin_110_fs_variables[] = {
  67. { ir_var_out, FRAG_RESULT_DEPTH, "float", "gl_FragDepth", glsl_precision_medium },
  68. };
  69. static const builtin_variable builtin_110_deprecated_fs_variables[] = {
  70. { ir_var_in, FRAG_ATTRIB_COL0, "vec4", "gl_Color", glsl_precision_medium },
  71. { ir_var_in, FRAG_ATTRIB_COL1, "vec4", "gl_SecondaryColor", glsl_precision_medium },
  72. { ir_var_in, FRAG_ATTRIB_FOGC, "float", "gl_FogFragCoord", glsl_precision_medium },
  73. };
  74. static const builtin_variable builtin_110_deprecated_vs_variables[] = {
  75. { ir_var_in, VERT_ATTRIB_POS, "vec4", "gl_Vertex", glsl_precision_high },
  76. { ir_var_in, VERT_ATTRIB_NORMAL, "vec3", "gl_Normal", glsl_precision_medium },
  77. { ir_var_in, VERT_ATTRIB_COLOR0, "vec4", "gl_Color", glsl_precision_medium },
  78. { ir_var_in, VERT_ATTRIB_COLOR1, "vec4", "gl_SecondaryColor", glsl_precision_medium },
  79. { ir_var_in, VERT_ATTRIB_TEX0, "vec4", "gl_MultiTexCoord0", glsl_precision_high },
  80. { ir_var_in, VERT_ATTRIB_TEX1, "vec4", "gl_MultiTexCoord1", glsl_precision_high },
  81. { ir_var_in, VERT_ATTRIB_TEX2, "vec4", "gl_MultiTexCoord2", glsl_precision_high },
  82. { ir_var_in, VERT_ATTRIB_TEX3, "vec4", "gl_MultiTexCoord3", glsl_precision_high },
  83. { ir_var_in, VERT_ATTRIB_TEX4, "vec4", "gl_MultiTexCoord4", glsl_precision_high },
  84. { ir_var_in, VERT_ATTRIB_TEX5, "vec4", "gl_MultiTexCoord5", glsl_precision_high },
  85. { ir_var_in, VERT_ATTRIB_TEX6, "vec4", "gl_MultiTexCoord6", glsl_precision_high },
  86. { ir_var_in, VERT_ATTRIB_TEX7, "vec4", "gl_MultiTexCoord7", glsl_precision_high },
  87. { ir_var_in, VERT_ATTRIB_FOG, "float", "gl_FogCoord", glsl_precision_high },
  88. { ir_var_out, VERT_RESULT_CLIP_VERTEX, "vec4", "gl_ClipVertex", glsl_precision_high },
  89. { ir_var_out, VERT_RESULT_COL0, "vec4", "gl_FrontColor", glsl_precision_medium },
  90. { ir_var_out, VERT_RESULT_BFC0, "vec4", "gl_BackColor", glsl_precision_medium },
  91. { ir_var_out, VERT_RESULT_COL1, "vec4", "gl_FrontSecondaryColor", glsl_precision_medium },
  92. { ir_var_out, VERT_RESULT_BFC1, "vec4", "gl_BackSecondaryColor", glsl_precision_medium },
  93. { ir_var_out, VERT_RESULT_FOGC, "float", "gl_FogFragCoord", glsl_precision_medium },
  94. };
  95. static const builtin_variable builtin_120_fs_variables[] = {
  96. { ir_var_in, FRAG_ATTRIB_PNTC, "vec2", "gl_PointCoord", glsl_precision_medium },
  97. };
  98. static const builtin_variable builtin_130_vs_variables[] = {
  99. { ir_var_system_value, SYSTEM_VALUE_VERTEX_ID, "int", "gl_VertexID", glsl_precision_high },
  100. };
  101. static const builtin_variable builtin_110_deprecated_uniforms[] = {
  102. { ir_var_uniform, -1, "mat4", "gl_ModelViewMatrix", glsl_precision_undefined },
  103. { ir_var_uniform, -1, "mat4", "gl_ProjectionMatrix", glsl_precision_undefined },
  104. { ir_var_uniform, -1, "mat4", "gl_ModelViewProjectionMatrix", glsl_precision_undefined },
  105. { ir_var_uniform, -1, "mat3", "gl_NormalMatrix", glsl_precision_undefined },
  106. { ir_var_uniform, -1, "mat4", "gl_ModelViewMatrixInverse", glsl_precision_undefined },
  107. { ir_var_uniform, -1, "mat4", "gl_ProjectionMatrixInverse", glsl_precision_undefined },
  108. { ir_var_uniform, -1, "mat4", "gl_ModelViewProjectionMatrixInverse", glsl_precision_undefined },
  109. { ir_var_uniform, -1, "mat4", "gl_ModelViewMatrixTranspose", glsl_precision_undefined },
  110. { ir_var_uniform, -1, "mat4", "gl_ProjectionMatrixTranspose", glsl_precision_undefined },
  111. { ir_var_uniform, -1, "mat4", "gl_ModelViewProjectionMatrixTranspose", glsl_precision_undefined },
  112. { ir_var_uniform, -1, "mat4", "gl_ModelViewMatrixInverseTranspose", glsl_precision_undefined },
  113. { ir_var_uniform, -1, "mat4", "gl_ProjectionMatrixInverseTranspose", glsl_precision_undefined },
  114. { ir_var_uniform, -1, "mat4", "gl_ModelViewProjectionMatrixInverseTranspose", glsl_precision_undefined },
  115. { ir_var_uniform, -1, "float", "gl_NormalScale", glsl_precision_undefined },
  116. { ir_var_uniform, -1, "gl_LightModelParameters", "gl_LightModel", glsl_precision_undefined},
  117. /* Mesa-internal ATI_envmap_bumpmap state. */
  118. { ir_var_uniform, -1, "vec2", "gl_BumpRotMatrix0MESA", glsl_precision_undefined},
  119. { ir_var_uniform, -1, "vec2", "gl_BumpRotMatrix1MESA", glsl_precision_undefined},
  120. { ir_var_uniform, -1, "vec4", "gl_FogParamsOptimizedMESA", glsl_precision_undefined},
  121. };
  122. static struct gl_builtin_uniform_element gl_DepthRange_elements[] = {
  123. {"near", {STATE_DEPTH_RANGE, 0, 0}, SWIZZLE_XXXX},
  124. {"far", {STATE_DEPTH_RANGE, 0, 0}, SWIZZLE_YYYY},
  125. {"diff", {STATE_DEPTH_RANGE, 0, 0}, SWIZZLE_ZZZZ},
  126. };
  127. static struct gl_builtin_uniform_element gl_ClipPlane_elements[] = {
  128. {NULL, {STATE_CLIPPLANE, 0, 0}, SWIZZLE_XYZW}
  129. };
  130. static struct gl_builtin_uniform_element gl_Point_elements[] = {
  131. {"size", {STATE_POINT_SIZE}, SWIZZLE_XXXX},
  132. {"sizeMin", {STATE_POINT_SIZE}, SWIZZLE_YYYY},
  133. {"sizeMax", {STATE_POINT_SIZE}, SWIZZLE_ZZZZ},
  134. {"fadeThresholdSize", {STATE_POINT_SIZE}, SWIZZLE_WWWW},
  135. {"distanceConstantAttenuation", {STATE_POINT_ATTENUATION}, SWIZZLE_XXXX},
  136. {"distanceLinearAttenuation", {STATE_POINT_ATTENUATION}, SWIZZLE_YYYY},
  137. {"distanceQuadraticAttenuation", {STATE_POINT_ATTENUATION}, SWIZZLE_ZZZZ},
  138. };
  139. static struct gl_builtin_uniform_element gl_FrontMaterial_elements[] = {
  140. {"emission", {STATE_MATERIAL, 0, STATE_EMISSION}, SWIZZLE_XYZW},
  141. {"ambient", {STATE_MATERIAL, 0, STATE_AMBIENT}, SWIZZLE_XYZW},
  142. {"diffuse", {STATE_MATERIAL, 0, STATE_DIFFUSE}, SWIZZLE_XYZW},
  143. {"specular", {STATE_MATERIAL, 0, STATE_SPECULAR}, SWIZZLE_XYZW},
  144. {"shininess", {STATE_MATERIAL, 0, STATE_SHININESS}, SWIZZLE_XXXX},
  145. };
  146. static struct gl_builtin_uniform_element gl_BackMaterial_elements[] = {
  147. {"emission", {STATE_MATERIAL, 1, STATE_EMISSION}, SWIZZLE_XYZW},
  148. {"ambient", {STATE_MATERIAL, 1, STATE_AMBIENT}, SWIZZLE_XYZW},
  149. {"diffuse", {STATE_MATERIAL, 1, STATE_DIFFUSE}, SWIZZLE_XYZW},
  150. {"specular", {STATE_MATERIAL, 1, STATE_SPECULAR}, SWIZZLE_XYZW},
  151. {"shininess", {STATE_MATERIAL, 1, STATE_SHININESS}, SWIZZLE_XXXX},
  152. };
  153. static struct gl_builtin_uniform_element gl_LightSource_elements[] = {
  154. {"ambient", {STATE_LIGHT, 0, STATE_AMBIENT}, SWIZZLE_XYZW},
  155. {"diffuse", {STATE_LIGHT, 0, STATE_DIFFUSE}, SWIZZLE_XYZW},
  156. {"specular", {STATE_LIGHT, 0, STATE_SPECULAR}, SWIZZLE_XYZW},
  157. {"position", {STATE_LIGHT, 0, STATE_POSITION}, SWIZZLE_XYZW},
  158. {"halfVector", {STATE_LIGHT, 0, STATE_HALF_VECTOR}, SWIZZLE_XYZW},
  159. {"spotDirection", {STATE_LIGHT, 0, STATE_SPOT_DIRECTION},
  160. MAKE_SWIZZLE4(SWIZZLE_X,
  161. SWIZZLE_Y,
  162. SWIZZLE_Z,
  163. SWIZZLE_Z)},
  164. {"spotCosCutoff", {STATE_LIGHT, 0, STATE_SPOT_DIRECTION}, SWIZZLE_WWWW},
  165. {"spotCutoff", {STATE_LIGHT, 0, STATE_SPOT_CUTOFF}, SWIZZLE_XXXX},
  166. {"spotExponent", {STATE_LIGHT, 0, STATE_ATTENUATION}, SWIZZLE_WWWW},
  167. {"constantAttenuation", {STATE_LIGHT, 0, STATE_ATTENUATION}, SWIZZLE_XXXX},
  168. {"linearAttenuation", {STATE_LIGHT, 0, STATE_ATTENUATION}, SWIZZLE_YYYY},
  169. {"quadraticAttenuation", {STATE_LIGHT, 0, STATE_ATTENUATION}, SWIZZLE_ZZZZ},
  170. };
  171. static struct gl_builtin_uniform_element gl_LightModel_elements[] = {
  172. {"ambient", {STATE_LIGHTMODEL_AMBIENT, 0}, SWIZZLE_XYZW},
  173. };
  174. static struct gl_builtin_uniform_element gl_FrontLightModelProduct_elements[] = {
  175. {"sceneColor", {STATE_LIGHTMODEL_SCENECOLOR, 0}, SWIZZLE_XYZW},
  176. };
  177. static struct gl_builtin_uniform_element gl_BackLightModelProduct_elements[] = {
  178. {"sceneColor", {STATE_LIGHTMODEL_SCENECOLOR, 1}, SWIZZLE_XYZW},
  179. };
  180. static struct gl_builtin_uniform_element gl_FrontLightProduct_elements[] = {
  181. {"ambient", {STATE_LIGHTPROD, 0, 0, STATE_AMBIENT}, SWIZZLE_XYZW},
  182. {"diffuse", {STATE_LIGHTPROD, 0, 0, STATE_DIFFUSE}, SWIZZLE_XYZW},
  183. {"specular", {STATE_LIGHTPROD, 0, 0, STATE_SPECULAR}, SWIZZLE_XYZW},
  184. };
  185. static struct gl_builtin_uniform_element gl_BackLightProduct_elements[] = {
  186. {"ambient", {STATE_LIGHTPROD, 0, 1, STATE_AMBIENT}, SWIZZLE_XYZW},
  187. {"diffuse", {STATE_LIGHTPROD, 0, 1, STATE_DIFFUSE}, SWIZZLE_XYZW},
  188. {"specular", {STATE_LIGHTPROD, 0, 1, STATE_SPECULAR}, SWIZZLE_XYZW},
  189. };
  190. static struct gl_builtin_uniform_element gl_TextureEnvColor_elements[] = {
  191. {NULL, {STATE_TEXENV_COLOR, 0}, SWIZZLE_XYZW},
  192. };
  193. static struct gl_builtin_uniform_element gl_EyePlaneS_elements[] = {
  194. {NULL, {STATE_TEXGEN, 0, STATE_TEXGEN_EYE_S}, SWIZZLE_XYZW},
  195. };
  196. static struct gl_builtin_uniform_element gl_EyePlaneT_elements[] = {
  197. {NULL, {STATE_TEXGEN, 0, STATE_TEXGEN_EYE_T}, SWIZZLE_XYZW},
  198. };
  199. static struct gl_builtin_uniform_element gl_EyePlaneR_elements[] = {
  200. {NULL, {STATE_TEXGEN, 0, STATE_TEXGEN_EYE_R}, SWIZZLE_XYZW},
  201. };
  202. static struct gl_builtin_uniform_element gl_EyePlaneQ_elements[] = {
  203. {NULL, {STATE_TEXGEN, 0, STATE_TEXGEN_EYE_Q}, SWIZZLE_XYZW},
  204. };
  205. static struct gl_builtin_uniform_element gl_ObjectPlaneS_elements[] = {
  206. {NULL, {STATE_TEXGEN, 0, STATE_TEXGEN_OBJECT_S}, SWIZZLE_XYZW},
  207. };
  208. static struct gl_builtin_uniform_element gl_ObjectPlaneT_elements[] = {
  209. {NULL, {STATE_TEXGEN, 0, STATE_TEXGEN_OBJECT_T}, SWIZZLE_XYZW},
  210. };
  211. static struct gl_builtin_uniform_element gl_ObjectPlaneR_elements[] = {
  212. {NULL, {STATE_TEXGEN, 0, STATE_TEXGEN_OBJECT_R}, SWIZZLE_XYZW},
  213. };
  214. static struct gl_builtin_uniform_element gl_ObjectPlaneQ_elements[] = {
  215. {NULL, {STATE_TEXGEN, 0, STATE_TEXGEN_OBJECT_Q}, SWIZZLE_XYZW},
  216. };
  217. static struct gl_builtin_uniform_element gl_Fog_elements[] = {
  218. {"color", {STATE_FOG_COLOR}, SWIZZLE_XYZW},
  219. {"density", {STATE_FOG_PARAMS}, SWIZZLE_XXXX},
  220. {"start", {STATE_FOG_PARAMS}, SWIZZLE_YYYY},
  221. {"end", {STATE_FOG_PARAMS}, SWIZZLE_ZZZZ},
  222. {"scale", {STATE_FOG_PARAMS}, SWIZZLE_WWWW},
  223. };
  224. static struct gl_builtin_uniform_element gl_NormalScale_elements[] = {
  225. {NULL, {STATE_NORMAL_SCALE}, SWIZZLE_XXXX},
  226. };
  227. static struct gl_builtin_uniform_element gl_BumpRotMatrix0MESA_elements[] = {
  228. {NULL, {STATE_INTERNAL, STATE_ROT_MATRIX_0}, SWIZZLE_XYZW},
  229. };
  230. static struct gl_builtin_uniform_element gl_BumpRotMatrix1MESA_elements[] = {
  231. {NULL, {STATE_INTERNAL, STATE_ROT_MATRIX_1}, SWIZZLE_XYZW},
  232. };
  233. static struct gl_builtin_uniform_element gl_FogParamsOptimizedMESA_elements[] = {
  234. {NULL, {STATE_INTERNAL, STATE_FOG_PARAMS_OPTIMIZED}, SWIZZLE_XYZW},
  235. };
  236. static struct gl_builtin_uniform_element gl_CurrentAttribVertMESA_elements[] = {
  237. {NULL, {STATE_INTERNAL, STATE_CURRENT_ATTRIB, 0}, SWIZZLE_XYZW},
  238. };
  239. static struct gl_builtin_uniform_element gl_CurrentAttribFragMESA_elements[] = {
  240. {NULL, {STATE_INTERNAL, STATE_CURRENT_ATTRIB_MAYBE_VP_CLAMPED, 0}, SWIZZLE_XYZW},
  241. };
  242. #define MATRIX(name, statevar, modifier) \
  243. static struct gl_builtin_uniform_element name ## _elements[] = { \
  244. { NULL, { statevar, 0, 0, 0, modifier}, SWIZZLE_XYZW }, \
  245. { NULL, { statevar, 0, 1, 1, modifier}, SWIZZLE_XYZW }, \
  246. { NULL, { statevar, 0, 2, 2, modifier}, SWIZZLE_XYZW }, \
  247. { NULL, { statevar, 0, 3, 3, modifier}, SWIZZLE_XYZW }, \
  248. }
  249. MATRIX(gl_ModelViewMatrix,
  250. STATE_MODELVIEW_MATRIX, STATE_MATRIX_TRANSPOSE);
  251. MATRIX(gl_ModelViewMatrixInverse,
  252. STATE_MODELVIEW_MATRIX, STATE_MATRIX_INVTRANS);
  253. MATRIX(gl_ModelViewMatrixTranspose,
  254. STATE_MODELVIEW_MATRIX, 0);
  255. MATRIX(gl_ModelViewMatrixInverseTranspose,
  256. STATE_MODELVIEW_MATRIX, STATE_MATRIX_INVERSE);
  257. MATRIX(gl_ProjectionMatrix,
  258. STATE_PROJECTION_MATRIX, STATE_MATRIX_TRANSPOSE);
  259. MATRIX(gl_ProjectionMatrixInverse,
  260. STATE_PROJECTION_MATRIX, STATE_MATRIX_INVTRANS);
  261. MATRIX(gl_ProjectionMatrixTranspose,
  262. STATE_PROJECTION_MATRIX, 0);
  263. MATRIX(gl_ProjectionMatrixInverseTranspose,
  264. STATE_PROJECTION_MATRIX, STATE_MATRIX_INVERSE);
  265. MATRIX(gl_ModelViewProjectionMatrix,
  266. STATE_MVP_MATRIX, STATE_MATRIX_TRANSPOSE);
  267. MATRIX(gl_ModelViewProjectionMatrixInverse,
  268. STATE_MVP_MATRIX, STATE_MATRIX_INVTRANS);
  269. MATRIX(gl_ModelViewProjectionMatrixTranspose,
  270. STATE_MVP_MATRIX, 0);
  271. MATRIX(gl_ModelViewProjectionMatrixInverseTranspose,
  272. STATE_MVP_MATRIX, STATE_MATRIX_INVERSE);
  273. MATRIX(gl_TextureMatrix,
  274. STATE_TEXTURE_MATRIX, STATE_MATRIX_TRANSPOSE);
  275. MATRIX(gl_TextureMatrixInverse,
  276. STATE_TEXTURE_MATRIX, STATE_MATRIX_INVTRANS);
  277. MATRIX(gl_TextureMatrixTranspose,
  278. STATE_TEXTURE_MATRIX, 0);
  279. MATRIX(gl_TextureMatrixInverseTranspose,
  280. STATE_TEXTURE_MATRIX, STATE_MATRIX_INVERSE);
  281. static struct gl_builtin_uniform_element gl_NormalMatrix_elements[] = {
  282. { NULL, { STATE_MODELVIEW_MATRIX, 0, 0, 0, STATE_MATRIX_INVERSE},
  283. MAKE_SWIZZLE4(SWIZZLE_X, SWIZZLE_Y, SWIZZLE_Z, SWIZZLE_Z) },
  284. { NULL, { STATE_MODELVIEW_MATRIX, 0, 1, 1, STATE_MATRIX_INVERSE},
  285. MAKE_SWIZZLE4(SWIZZLE_X, SWIZZLE_Y, SWIZZLE_Z, SWIZZLE_Z) },
  286. { NULL, { STATE_MODELVIEW_MATRIX, 0, 2, 2, STATE_MATRIX_INVERSE},
  287. MAKE_SWIZZLE4(SWIZZLE_X, SWIZZLE_Y, SWIZZLE_Z, SWIZZLE_Z) },
  288. };
  289. #undef MATRIX
  290. #define STATEVAR(name) {#name, name ## _elements, Elements(name ## _elements)}
  291. static const struct gl_builtin_uniform_desc _mesa_builtin_uniform_desc[] = {
  292. STATEVAR(gl_DepthRange),
  293. STATEVAR(gl_ClipPlane),
  294. STATEVAR(gl_Point),
  295. STATEVAR(gl_FrontMaterial),
  296. STATEVAR(gl_BackMaterial),
  297. STATEVAR(gl_LightSource),
  298. STATEVAR(gl_LightModel),
  299. STATEVAR(gl_FrontLightModelProduct),
  300. STATEVAR(gl_BackLightModelProduct),
  301. STATEVAR(gl_FrontLightProduct),
  302. STATEVAR(gl_BackLightProduct),
  303. STATEVAR(gl_TextureEnvColor),
  304. STATEVAR(gl_EyePlaneS),
  305. STATEVAR(gl_EyePlaneT),
  306. STATEVAR(gl_EyePlaneR),
  307. STATEVAR(gl_EyePlaneQ),
  308. STATEVAR(gl_ObjectPlaneS),
  309. STATEVAR(gl_ObjectPlaneT),
  310. STATEVAR(gl_ObjectPlaneR),
  311. STATEVAR(gl_ObjectPlaneQ),
  312. STATEVAR(gl_Fog),
  313. STATEVAR(gl_ModelViewMatrix),
  314. STATEVAR(gl_ModelViewMatrixInverse),
  315. STATEVAR(gl_ModelViewMatrixTranspose),
  316. STATEVAR(gl_ModelViewMatrixInverseTranspose),
  317. STATEVAR(gl_ProjectionMatrix),
  318. STATEVAR(gl_ProjectionMatrixInverse),
  319. STATEVAR(gl_ProjectionMatrixTranspose),
  320. STATEVAR(gl_ProjectionMatrixInverseTranspose),
  321. STATEVAR(gl_ModelViewProjectionMatrix),
  322. STATEVAR(gl_ModelViewProjectionMatrixInverse),
  323. STATEVAR(gl_ModelViewProjectionMatrixTranspose),
  324. STATEVAR(gl_ModelViewProjectionMatrixInverseTranspose),
  325. STATEVAR(gl_TextureMatrix),
  326. STATEVAR(gl_TextureMatrixInverse),
  327. STATEVAR(gl_TextureMatrixTranspose),
  328. STATEVAR(gl_TextureMatrixInverseTranspose),
  329. STATEVAR(gl_NormalMatrix),
  330. STATEVAR(gl_NormalScale),
  331. STATEVAR(gl_BumpRotMatrix0MESA),
  332. STATEVAR(gl_BumpRotMatrix1MESA),
  333. STATEVAR(gl_FogParamsOptimizedMESA),
  334. STATEVAR(gl_CurrentAttribVertMESA),
  335. STATEVAR(gl_CurrentAttribFragMESA),
  336. {NULL, NULL, 0}
  337. };
  338. static ir_variable *
  339. add_variable(exec_list *instructions, glsl_symbol_table *symtab,
  340. const char *name, const glsl_type *type,
  341. enum ir_variable_mode mode, int slot, glsl_precision prec = glsl_precision_undefined)
  342. {
  343. ir_variable *var = new(symtab) ir_variable(type, name, mode, prec);
  344. switch (var->mode) {
  345. case ir_var_auto:
  346. case ir_var_in:
  347. case ir_var_const_in:
  348. case ir_var_uniform:
  349. case ir_var_system_value:
  350. var->read_only = true;
  351. break;
  352. case ir_var_inout:
  353. case ir_var_out:
  354. break;
  355. default:
  356. assert(0);
  357. break;
  358. }
  359. var->location = slot;
  360. var->explicit_location = (slot >= 0);
  361. var->explicit_index = 0;
  362. /* Once the variable is created an initialized, add it to the symbol table
  363. * and add the declaration to the IR stream.
  364. */
  365. instructions->push_tail(var);
  366. symtab->add_variable(var);
  367. return var;
  368. }
  369. static ir_variable *
  370. add_uniform(exec_list *instructions, glsl_symbol_table *symtab,
  371. const char *name, const glsl_type *type, glsl_precision prec = glsl_precision_undefined)
  372. {
  373. ir_variable *const uni =
  374. add_variable(instructions, symtab, name, type, ir_var_uniform, -1, prec);
  375. unsigned i;
  376. for (i = 0; _mesa_builtin_uniform_desc[i].name != NULL; i++) {
  377. if (strcmp(_mesa_builtin_uniform_desc[i].name, name) == 0) {
  378. break;
  379. }
  380. }
  381. assert(_mesa_builtin_uniform_desc[i].name != NULL);
  382. const struct gl_builtin_uniform_desc* const statevar =
  383. &_mesa_builtin_uniform_desc[i];
  384. const unsigned array_count = type->is_array() ? type->length : 1;
  385. uni->num_state_slots = array_count * statevar->num_elements;
  386. ir_state_slot *slots =
  387. ralloc_array(uni, ir_state_slot, uni->num_state_slots);
  388. uni->state_slots = slots;
  389. for (unsigned a = 0; a < array_count; a++) {
  390. for (unsigned j = 0; j < statevar->num_elements; j++) {
  391. struct gl_builtin_uniform_element *element = &statevar->elements[j];
  392. memcpy(slots->tokens, element->tokens, sizeof(element->tokens));
  393. if (type->is_array()) {
  394. if (strcmp(name, "gl_CurrentAttribVertMESA") == 0 ||
  395. strcmp(name, "gl_CurrentAttribFragMESA") == 0) {
  396. slots->tokens[2] = a;
  397. } else {
  398. slots->tokens[1] = a;
  399. }
  400. }
  401. slots->swizzle = element->swizzle;
  402. slots++;
  403. }
  404. }
  405. return uni;
  406. }
  407. static void
  408. add_builtin_variable(exec_list *instructions, glsl_symbol_table *symtab,
  409. const builtin_variable *proto, bool use_precision)
  410. {
  411. /* Create a new variable declaration from the description supplied by
  412. * the caller.
  413. */
  414. const glsl_type *const type = symtab->get_type(proto->type);
  415. assert(type != NULL);
  416. if (proto->mode == ir_var_uniform) {
  417. add_uniform(instructions, symtab, proto->name, type, use_precision ? proto->prec : glsl_precision_undefined);
  418. } else {
  419. add_variable(instructions, symtab, proto->name, type, proto->mode,
  420. proto->slot, use_precision ? proto->prec : glsl_precision_undefined);
  421. }
  422. }
  423. static ir_variable *
  424. add_builtin_constant(exec_list *instructions, glsl_symbol_table *symtab,
  425. const char *name, int value)
  426. {
  427. ir_variable *const var = add_variable(instructions, symtab,
  428. name, glsl_type::int_type,
  429. ir_var_auto, -1, glsl_precision_undefined);
  430. var->constant_value = new(var) ir_constant(value);
  431. var->constant_initializer = new(var) ir_constant(value);
  432. var->has_initializer = true;
  433. return var;
  434. }
  435. /* Several constants in GLSL ES have different names than normal desktop GLSL.
  436. * Therefore, this function should only be called on the ES path.
  437. */
  438. static void
  439. generate_100ES_uniforms(exec_list *instructions,
  440. struct _mesa_glsl_parse_state *state)
  441. {
  442. glsl_symbol_table *const symtab = state->symbols;
  443. add_builtin_constant(instructions, symtab, "gl_MaxVertexAttribs",
  444. state->Const.MaxVertexAttribs);
  445. add_builtin_constant(instructions, symtab, "gl_MaxVertexUniformVectors",
  446. state->Const.MaxVertexUniformComponents);
  447. add_builtin_constant(instructions, symtab, "gl_MaxVaryingVectors",
  448. state->Const.MaxVaryingFloats / 4);
  449. add_builtin_constant(instructions, symtab, "gl_MaxVertexTextureImageUnits",
  450. state->Const.MaxVertexTextureImageUnits);
  451. add_builtin_constant(instructions, symtab, "gl_MaxCombinedTextureImageUnits",
  452. state->Const.MaxCombinedTextureImageUnits);
  453. add_builtin_constant(instructions, symtab, "gl_MaxTextureImageUnits",
  454. state->Const.MaxTextureImageUnits);
  455. add_builtin_constant(instructions, symtab, "gl_MaxFragmentUniformVectors",
  456. state->Const.MaxFragmentUniformComponents);
  457. add_uniform(instructions, symtab, "gl_DepthRange",
  458. state->symbols->get_type("gl_DepthRangeParameters"), glsl_precision_undefined);
  459. }
  460. static void
  461. generate_110_uniforms(exec_list *instructions,
  462. struct _mesa_glsl_parse_state *state,
  463. bool add_deprecated)
  464. {
  465. glsl_symbol_table *const symtab = state->symbols;
  466. if (add_deprecated) {
  467. for (unsigned i = 0
  468. ; i < Elements(builtin_110_deprecated_uniforms)
  469. ; i++) {
  470. add_builtin_variable(instructions, symtab,
  471. & builtin_110_deprecated_uniforms[i], state->es_shader);
  472. }
  473. }
  474. if (add_deprecated) {
  475. add_builtin_constant(instructions, symtab, "gl_MaxLights",
  476. state->Const.MaxLights);
  477. add_builtin_constant(instructions, symtab, "gl_MaxClipPlanes",
  478. state->Const.MaxClipPlanes);
  479. add_builtin_constant(instructions, symtab, "gl_MaxTextureUnits",
  480. state->Const.MaxTextureUnits);
  481. add_builtin_constant(instructions, symtab, "gl_MaxTextureCoords",
  482. state->Const.MaxTextureCoords);
  483. }
  484. add_builtin_constant(instructions, symtab, "gl_MaxVertexAttribs",
  485. state->Const.MaxVertexAttribs);
  486. add_builtin_constant(instructions, symtab, "gl_MaxVertexUniformComponents",
  487. state->Const.MaxVertexUniformComponents);
  488. add_builtin_constant(instructions, symtab, "gl_MaxVaryingFloats",
  489. state->Const.MaxVaryingFloats);
  490. add_builtin_constant(instructions, symtab, "gl_MaxVertexTextureImageUnits",
  491. state->Const.MaxVertexTextureImageUnits);
  492. add_builtin_constant(instructions, symtab, "gl_MaxCombinedTextureImageUnits",
  493. state->Const.MaxCombinedTextureImageUnits);
  494. add_builtin_constant(instructions, symtab, "gl_MaxTextureImageUnits",
  495. state->Const.MaxTextureImageUnits);
  496. add_builtin_constant(instructions, symtab, "gl_MaxFragmentUniformComponents",
  497. state->Const.MaxFragmentUniformComponents);
  498. if (add_deprecated) {
  499. const glsl_type *const mat4_array_type =
  500. glsl_type::get_array_instance(glsl_type::mat4_type,
  501. state->Const.MaxTextureCoords);
  502. add_uniform(instructions, symtab, "gl_TextureMatrix", mat4_array_type);
  503. add_uniform(instructions, symtab, "gl_TextureMatrixInverse", mat4_array_type);
  504. add_uniform(instructions, symtab, "gl_TextureMatrixTranspose", mat4_array_type);
  505. add_uniform(instructions, symtab, "gl_TextureMatrixInverseTranspose", mat4_array_type);
  506. }
  507. add_uniform(instructions, symtab, "gl_DepthRange",
  508. symtab->get_type("gl_DepthRangeParameters"));
  509. if (add_deprecated) {
  510. add_uniform(instructions, symtab, "gl_ClipPlane",
  511. glsl_type::get_array_instance(glsl_type::vec4_type,
  512. state->Const.MaxClipPlanes));
  513. add_uniform(instructions, symtab, "gl_Point",
  514. symtab->get_type("gl_PointParameters"));
  515. const glsl_type *const material_parameters_type =
  516. symtab->get_type("gl_MaterialParameters");
  517. add_uniform(instructions, symtab, "gl_FrontMaterial", material_parameters_type);
  518. add_uniform(instructions, symtab, "gl_BackMaterial", material_parameters_type);
  519. const glsl_type *const light_source_array_type =
  520. glsl_type::get_array_instance(symtab->get_type("gl_LightSourceParameters"), state->Const.MaxLights);
  521. add_uniform(instructions, symtab, "gl_LightSource", light_source_array_type);
  522. const glsl_type *const light_model_products_type =
  523. symtab->get_type("gl_LightModelProducts");
  524. add_uniform(instructions, symtab, "gl_FrontLightModelProduct",
  525. light_model_products_type);
  526. add_uniform(instructions, symtab, "gl_BackLightModelProduct",
  527. light_model_products_type);
  528. const glsl_type *const light_products_type =
  529. glsl_type::get_array_instance(symtab->get_type("gl_LightProducts"),
  530. state->Const.MaxLights);
  531. add_uniform(instructions, symtab, "gl_FrontLightProduct", light_products_type);
  532. add_uniform(instructions, symtab, "gl_BackLightProduct", light_products_type);
  533. add_uniform(instructions, symtab, "gl_TextureEnvColor",
  534. glsl_type::get_array_instance(glsl_type::vec4_type,
  535. state->Const.MaxTextureUnits));
  536. const glsl_type *const texcoords_vec4 =
  537. glsl_type::get_array_instance(glsl_type::vec4_type,
  538. state->Const.MaxTextureCoords);
  539. add_uniform(instructions, symtab, "gl_EyePlaneS", texcoords_vec4);
  540. add_uniform(instructions, symtab, "gl_EyePlaneT", texcoords_vec4);
  541. add_uniform(instructions, symtab, "gl_EyePlaneR", texcoords_vec4);
  542. add_uniform(instructions, symtab, "gl_EyePlaneQ", texcoords_vec4);
  543. add_uniform(instructions, symtab, "gl_ObjectPlaneS", texcoords_vec4);
  544. add_uniform(instructions, symtab, "gl_ObjectPlaneT", texcoords_vec4);
  545. add_uniform(instructions, symtab, "gl_ObjectPlaneR", texcoords_vec4);
  546. add_uniform(instructions, symtab, "gl_ObjectPlaneQ", texcoords_vec4);
  547. add_uniform(instructions, symtab, "gl_Fog",
  548. symtab->get_type("gl_FogParameters"));
  549. }
  550. /* Mesa-internal current attrib state */
  551. const glsl_type *const vert_attribs =
  552. glsl_type::get_array_instance(glsl_type::vec4_type, VERT_ATTRIB_MAX);
  553. add_uniform(instructions, symtab, "gl_CurrentAttribVertMESA", vert_attribs);
  554. const glsl_type *const frag_attribs =
  555. glsl_type::get_array_instance(glsl_type::vec4_type, FRAG_ATTRIB_MAX);
  556. add_uniform(instructions, symtab, "gl_CurrentAttribFragMESA", frag_attribs);
  557. }
  558. /* This function should only be called for ES, not desktop GL. */
  559. static void
  560. generate_100ES_vs_variables(exec_list *instructions,
  561. struct _mesa_glsl_parse_state *state)
  562. {
  563. for (unsigned i = 0; i < Elements(builtin_core_vs_variables); i++) {
  564. add_builtin_variable(instructions, state->symbols,
  565. & builtin_core_vs_variables[i], state->es_shader);
  566. }
  567. generate_100ES_uniforms(instructions, state);
  568. generate_ARB_draw_buffers_variables(instructions, state, false,
  569. vertex_shader);
  570. }
  571. static void
  572. generate_110_vs_variables(exec_list *instructions,
  573. struct _mesa_glsl_parse_state *state,
  574. bool add_deprecated)
  575. {
  576. for (unsigned i = 0; i < Elements(builtin_core_vs_variables); i++) {
  577. add_builtin_variable(instructions, state->symbols,
  578. & builtin_core_vs_variables[i], state->es_shader);
  579. }
  580. if (add_deprecated) {
  581. for (unsigned i = 0
  582. ; i < Elements(builtin_110_deprecated_vs_variables)
  583. ; i++) {
  584. add_builtin_variable(instructions, state->symbols,
  585. & builtin_110_deprecated_vs_variables[i], state->es_shader);
  586. }
  587. }
  588. generate_110_uniforms(instructions, state, add_deprecated);
  589. /* From page 54 (page 60 of the PDF) of the GLSL 1.20 spec:
  590. *
  591. * "As with all arrays, indices used to subscript gl_TexCoord must
  592. * either be an integral constant expressions, or this array must be
  593. * re-declared by the shader with a size. The size can be at most
  594. * gl_MaxTextureCoords. Using indexes close to 0 may aid the
  595. * implementation in preserving varying resources."
  596. */
  597. const glsl_type *const vec4_array_type =
  598. glsl_type::get_array_instance(glsl_type::vec4_type, 0);
  599. add_variable(instructions, state->symbols,
  600. "gl_TexCoord", vec4_array_type, ir_var_out, VERT_RESULT_TEX0);
  601. generate_ARB_draw_buffers_variables(instructions, state, false,
  602. vertex_shader);
  603. }
  604. static void
  605. generate_120_vs_variables(exec_list *instructions,
  606. struct _mesa_glsl_parse_state *state,
  607. bool add_deprecated)
  608. {
  609. /* GLSL version 1.20 did not add any built-in variables in the vertex
  610. * shader.
  611. */
  612. generate_110_vs_variables(instructions, state, add_deprecated);
  613. }
  614. static void
  615. generate_130_uniforms(exec_list *instructions,
  616. struct _mesa_glsl_parse_state *state)
  617. {
  618. glsl_symbol_table *const symtab = state->symbols;
  619. add_builtin_constant(instructions, symtab, "gl_MaxClipDistances",
  620. state->Const.MaxClipPlanes);
  621. add_builtin_constant(instructions, symtab, "gl_MaxVaryingComponents",
  622. state->Const.MaxVaryingFloats);
  623. }
  624. static void
  625. generate_130_vs_variables(exec_list *instructions,
  626. struct _mesa_glsl_parse_state *state,
  627. bool add_deprecated)
  628. {
  629. generate_120_vs_variables(instructions, state, add_deprecated);
  630. for (unsigned i = 0; i < Elements(builtin_130_vs_variables); i++) {
  631. add_builtin_variable(instructions, state->symbols,
  632. & builtin_130_vs_variables[i], state->es_shader);
  633. }
  634. generate_130_uniforms(instructions, state);
  635. /* From the GLSL 1.30 spec, section 7.1 (Vertex Shader Special
  636. * Variables):
  637. *
  638. * The gl_ClipDistance array is predeclared as unsized and must
  639. * be sized by the shader either redeclaring it with a size or
  640. * indexing it only with integral constant expressions.
  641. *
  642. * We represent this in Mesa by initially declaring the array as
  643. * size 0.
  644. */
  645. const glsl_type *const clip_distance_array_type =
  646. glsl_type::get_array_instance(glsl_type::float_type, 0);
  647. add_variable(instructions, state->symbols,
  648. "gl_ClipDistance", clip_distance_array_type, ir_var_out,
  649. VERT_RESULT_CLIP_DIST0);
  650. }
  651. static void
  652. initialize_vs_variables(exec_list *instructions,
  653. struct _mesa_glsl_parse_state *state)
  654. {
  655. switch (state->language_version) {
  656. case 100:
  657. generate_100ES_vs_variables(instructions, state);
  658. break;
  659. case 110:
  660. generate_110_vs_variables(instructions, state, true);
  661. break;
  662. case 120:
  663. generate_120_vs_variables(instructions, state, true);
  664. break;
  665. case 130:
  666. generate_130_vs_variables(instructions, state, true);
  667. break;
  668. case 140:
  669. generate_130_vs_variables(instructions, state, false);
  670. break;
  671. }
  672. generate_ARB_draw_instanced_variables(instructions, state, false,
  673. vertex_shader);
  674. }
  675. /* This function should only be called for ES, not desktop GL. */
  676. static void
  677. generate_100ES_fs_variables(exec_list *instructions,
  678. struct _mesa_glsl_parse_state *state)
  679. {
  680. for (unsigned i = 0; i < Elements(builtin_core_fs_variables); i++) {
  681. add_builtin_variable(instructions, state->symbols,
  682. & builtin_core_fs_variables[i], state->es_shader);
  683. }
  684. for (unsigned i = 0; i < Elements(builtin_100ES_fs_variables); i++) {
  685. add_builtin_variable(instructions, state->symbols,
  686. & builtin_100ES_fs_variables[i], state->es_shader);
  687. }
  688. generate_100ES_uniforms(instructions, state);
  689. generate_ARB_draw_buffers_variables(instructions, state, false,
  690. fragment_shader);
  691. }
  692. static void
  693. generate_110_fs_variables(exec_list *instructions,
  694. struct _mesa_glsl_parse_state *state,
  695. bool add_deprecated)
  696. {
  697. for (unsigned i = 0; i < Elements(builtin_core_fs_variables); i++) {
  698. add_builtin_variable(instructions, state->symbols,
  699. & builtin_core_fs_variables[i], state->es_shader);
  700. }
  701. for (unsigned i = 0; i < Elements(builtin_110_fs_variables); i++) {
  702. add_builtin_variable(instructions, state->symbols,
  703. & builtin_110_fs_variables[i], state->es_shader);
  704. }
  705. if (add_deprecated) {
  706. for (unsigned i = 0
  707. ; i < Elements(builtin_110_deprecated_fs_variables)
  708. ; i++) {
  709. add_builtin_variable(instructions, state->symbols,
  710. & builtin_110_deprecated_fs_variables[i], state->es_shader);
  711. }
  712. }
  713. generate_110_uniforms(instructions, state, add_deprecated);
  714. /* From page 54 (page 60 of the PDF) of the GLSL 1.20 spec:
  715. *
  716. * "As with all arrays, indices used to subscript gl_TexCoord must
  717. * either be an integral constant expressions, or this array must be
  718. * re-declared by the shader with a size. The size can be at most
  719. * gl_MaxTextureCoords. Using indexes close to 0 may aid the
  720. * implementation in preserving varying resources."
  721. */
  722. const glsl_type *const vec4_array_type =
  723. glsl_type::get_array_instance(glsl_type::vec4_type, 0);
  724. add_variable(instructions, state->symbols,
  725. "gl_TexCoord", vec4_array_type, ir_var_in, FRAG_ATTRIB_TEX0);
  726. generate_ARB_draw_buffers_variables(instructions, state, false,
  727. fragment_shader);
  728. }
  729. static void
  730. generate_ARB_draw_buffers_variables(exec_list *instructions,
  731. struct _mesa_glsl_parse_state *state,
  732. bool warn, _mesa_glsl_parser_targets target)
  733. {
  734. /* gl_MaxDrawBuffers is available in all shader stages.
  735. */
  736. ir_variable *const mdb =
  737. add_builtin_constant(instructions, state->symbols, "gl_MaxDrawBuffers",
  738. state->Const.MaxDrawBuffers);
  739. if (warn)
  740. mdb->warn_extension = "GL_ARB_draw_buffers";
  741. /* gl_FragData is only available in the fragment shader.
  742. */
  743. if (target == fragment_shader) {
  744. const glsl_type *const vec4_array_type =
  745. glsl_type::get_array_instance(glsl_type::vec4_type,
  746. state->Const.MaxDrawBuffers);
  747. ir_variable *const fd =
  748. add_variable(instructions, state->symbols,
  749. "gl_FragData", vec4_array_type,
  750. ir_var_out, FRAG_RESULT_DATA0);
  751. if (warn)
  752. fd->warn_extension = "GL_ARB_draw_buffers";
  753. }
  754. }
  755. static void
  756. generate_ARB_draw_instanced_variables(exec_list *instructions,
  757. struct _mesa_glsl_parse_state *state,
  758. bool warn,
  759. _mesa_glsl_parser_targets target)
  760. {
  761. /* gl_InstanceIDARB is only available in the vertex shader.
  762. */
  763. if (target != vertex_shader)
  764. return;
  765. if (state->ARB_draw_instanced_enable) {
  766. ir_variable *inst =
  767. add_variable(instructions, state->symbols,
  768. "gl_InstanceIDARB", glsl_type::int_type,
  769. ir_var_system_value, SYSTEM_VALUE_INSTANCE_ID);
  770. if (warn)
  771. inst->warn_extension = "GL_ARB_draw_instanced";
  772. }
  773. if (state->ARB_draw_instanced_enable || state->language_version >= 140) {
  774. /* Originally ARB_draw_instanced only specified that ARB decorated name.
  775. * Since no vendor actually implemented that behavior and some apps use
  776. * the undecorated name, the extension now specifies that both names are
  777. * available.
  778. */
  779. ir_variable *inst =
  780. add_variable(instructions, state->symbols,
  781. "gl_InstanceID", glsl_type::int_type,
  782. ir_var_system_value, SYSTEM_VALUE_INSTANCE_ID);
  783. if (state->language_version < 140 && warn)
  784. inst->warn_extension = "GL_ARB_draw_instanced";
  785. }
  786. }
  787. static void
  788. generate_ARB_shader_stencil_export_variables(exec_list *instructions,
  789. struct _mesa_glsl_parse_state *state,
  790. bool warn)
  791. {
  792. /* gl_FragStencilRefARB is only available in the fragment shader.
  793. */
  794. ir_variable *const fd =
  795. add_variable(instructions, state->symbols,
  796. "gl_FragStencilRefARB", glsl_type::int_type,
  797. ir_var_out, FRAG_RESULT_STENCIL);
  798. if (warn)
  799. fd->warn_extension = "GL_ARB_shader_stencil_export";
  800. }
  801. static void
  802. generate_AMD_shader_stencil_export_variables(exec_list *instructions,
  803. struct _mesa_glsl_parse_state *state,
  804. bool warn)
  805. {
  806. /* gl_FragStencilRefAMD is only available in the fragment shader.
  807. */
  808. ir_variable *const fd =
  809. add_variable(instructions, state->symbols,
  810. "gl_FragStencilRefAMD", glsl_type::int_type,
  811. ir_var_out, FRAG_RESULT_STENCIL);
  812. if (warn)
  813. fd->warn_extension = "GL_AMD_shader_stencil_export";
  814. }
  815. static void
  816. generate_120_fs_variables(exec_list *instructions,
  817. struct _mesa_glsl_parse_state *state,
  818. bool add_deprecated)
  819. {
  820. generate_110_fs_variables(instructions, state, add_deprecated);
  821. for (unsigned i = 0
  822. ; i < Elements(builtin_120_fs_variables)
  823. ; i++) {
  824. add_builtin_variable(instructions, state->symbols,
  825. & builtin_120_fs_variables[i], state->es_shader);
  826. }
  827. }
  828. static void
  829. generate_fs_clipdistance(exec_list *instructions,
  830. struct _mesa_glsl_parse_state *state)
  831. {
  832. /* From the GLSL 1.30 spec, section 7.2 (Fragment Shader Special
  833. * Variables):
  834. *
  835. * The built-in input variable gl_ClipDistance array contains linearly
  836. * interpolated values for the vertex values written by the vertex shader
  837. * to the gl_ClipDistance vertex output variable. This array must be
  838. * sized in the fragment shader either implicitly or explicitly to be the
  839. * same size as it was sized in the vertex shader.
  840. *
  841. * In other words, the array must be pre-declared as implicitly sized. We
  842. * represent this in Mesa by initially declaring the array as size 0.
  843. */
  844. const glsl_type *const clip_distance_array_type =
  845. glsl_type::get_array_instance(glsl_type::float_type, 0);
  846. add_variable(instructions, state->symbols,
  847. "gl_ClipDistance", clip_distance_array_type, ir_var_in,
  848. FRAG_ATTRIB_CLIP_DIST0);
  849. }
  850. static void
  851. generate_130_fs_variables(exec_list *instructions,
  852. struct _mesa_glsl_parse_state *state)
  853. {
  854. generate_120_fs_variables(instructions, state, true);
  855. generate_130_uniforms(instructions, state);
  856. generate_fs_clipdistance(instructions, state);
  857. }
  858. static void
  859. generate_140_fs_variables(exec_list *instructions,
  860. struct _mesa_glsl_parse_state *state)
  861. {
  862. generate_120_fs_variables(instructions, state, false);
  863. generate_130_uniforms(instructions, state);
  864. generate_fs_clipdistance(instructions, state);
  865. }
  866. static void
  867. initialize_fs_variables(exec_list *instructions,
  868. struct _mesa_glsl_parse_state *state)
  869. {
  870. switch (state->language_version) {
  871. case 100:
  872. generate_100ES_fs_variables(instructions, state);
  873. break;
  874. case 110:
  875. generate_110_fs_variables(instructions, state, true);
  876. break;
  877. case 120:
  878. generate_120_fs_variables(instructions, state, true);
  879. break;
  880. case 130:
  881. generate_130_fs_variables(instructions, state);
  882. break;
  883. case 140:
  884. generate_140_fs_variables(instructions, state);
  885. break;
  886. }
  887. if (state->ARB_shader_stencil_export_enable)
  888. generate_ARB_shader_stencil_export_variables(instructions, state,
  889. state->ARB_shader_stencil_export_warn);
  890. if (state->AMD_shader_stencil_export_enable)
  891. generate_AMD_shader_stencil_export_variables(instructions, state,
  892. state->AMD_shader_stencil_export_warn);
  893. }
  894. void
  895. _mesa_glsl_initialize_variables(exec_list *instructions,
  896. struct _mesa_glsl_parse_state *state)
  897. {
  898. switch (state->target) {
  899. case vertex_shader:
  900. initialize_vs_variables(instructions, state);
  901. break;
  902. case geometry_shader:
  903. break;
  904. case fragment_shader:
  905. initialize_fs_variables(instructions, state);
  906. break;
  907. }
  908. }