Clone of mesa.
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

ir_variable.cpp 9.0KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303
  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 "glsl_parser_extras.h"
  24. #include "glsl_symbol_table.h"
  25. #include "ir.h"
  26. #include "builtin_variables.h"
  27. #ifndef Elements
  28. #define Elements(x) (sizeof(x)/sizeof(*(x)))
  29. #endif
  30. static ir_variable *
  31. add_variable(const char *name, enum ir_variable_mode mode,
  32. const glsl_type *type, exec_list *instructions,
  33. glsl_symbol_table *symtab)
  34. {
  35. ir_variable *var = new ir_variable(type, name);
  36. var->mode = mode;
  37. if (var->mode != ir_var_out)
  38. var->read_only = true;
  39. /* Once the variable is created an initialized, add it to the symbol table
  40. * and add the declaration to the IR stream.
  41. */
  42. instructions->push_tail(var);
  43. symtab->add_variable(var->name, var);
  44. return var;
  45. }
  46. static void
  47. add_builtin_variable(const builtin_variable *proto, exec_list *instructions,
  48. glsl_symbol_table *symtab)
  49. {
  50. /* Create a new variable declaration from the description supplied by
  51. * the caller.
  52. */
  53. const glsl_type *const type = symtab->get_type(proto->type);
  54. assert(type != NULL);
  55. add_variable(proto->name, proto->mode, type, instructions, symtab);
  56. }
  57. static void
  58. generate_110_uniforms(exec_list *instructions,
  59. glsl_symbol_table *symtab)
  60. {
  61. for (unsigned i = 0
  62. ; i < Elements(builtin_110_deprecated_uniforms)
  63. ; i++) {
  64. add_builtin_variable(& builtin_110_deprecated_uniforms[i],
  65. instructions, symtab);
  66. }
  67. /* FINISHME: Add support for gl_TextureMatrix[]. The size of this array is
  68. * FINISHME: implementation dependent based on the value of
  69. * FINISHME: GL_MAX_TEXTURE_COORDS.
  70. */
  71. /* FINISHME: Add support for gl_DepthRangeParameters */
  72. /* FINISHME: Add support for gl_ClipPlane[] */
  73. /* FINISHME: Add support for gl_PointParameters */
  74. /* FINISHME: Add support for gl_MaterialParameters
  75. * FINISHME: (glFrontMaterial, glBackMaterial)
  76. */
  77. /* FINISHME: Add support for gl_LightSource[] */
  78. /* FINISHME: Add support for gl_LightModel */
  79. /* FINISHME: Add support for gl_FrontLightProduct[], gl_BackLightProduct[] */
  80. /* FINISHME: Add support for gl_TextureEnvColor[] */
  81. /* FINISHME: Add support for gl_ObjectPlane*[], gl_EyePlane*[] */
  82. /* FINISHME: Add support for gl_Fog */
  83. }
  84. static void
  85. generate_110_vs_variables(exec_list *instructions,
  86. glsl_symbol_table *symtab)
  87. {
  88. for (unsigned i = 0; i < Elements(builtin_core_vs_variables); i++) {
  89. add_builtin_variable(& builtin_core_vs_variables[i],
  90. instructions, symtab);
  91. }
  92. for (unsigned i = 0
  93. ; i < Elements(builtin_110_deprecated_vs_variables)
  94. ; i++) {
  95. add_builtin_variable(& builtin_110_deprecated_vs_variables[i],
  96. instructions, symtab);
  97. }
  98. generate_110_uniforms(instructions, symtab);
  99. /* FINISHME: The size of this array is implementation dependent based on the
  100. * FINISHME: value of GL_MAX_TEXTURE_COORDS. GL_MAX_TEXTURE_COORDS must be
  101. * FINISHME: at least 2, so hard-code 2 for now.
  102. */
  103. const glsl_type *const vec4_type =
  104. glsl_type::get_instance(GLSL_TYPE_FLOAT, 4, 1);
  105. const glsl_type *const vec4_array_type =
  106. glsl_type::get_array_instance(vec4_type, 2);
  107. add_variable("gl_TexCoord", ir_var_out, vec4_array_type, instructions,
  108. symtab);
  109. }
  110. static void
  111. generate_120_vs_variables(exec_list *instructions,
  112. glsl_symbol_table *symtab)
  113. {
  114. /* GLSL version 1.20 did not add any built-in variables in the vertex
  115. * shader.
  116. */
  117. generate_110_vs_variables(instructions, symtab);
  118. }
  119. static void
  120. generate_130_vs_variables(exec_list *instructions,
  121. glsl_symbol_table *symtab)
  122. {
  123. generate_120_vs_variables(instructions, symtab);
  124. for (unsigned i = 0; i < Elements(builtin_130_vs_variables); i++) {
  125. add_builtin_variable(& builtin_130_vs_variables[i],
  126. instructions, symtab);
  127. }
  128. /* FINISHME: The size of this array is implementation dependent based on
  129. * FINISHME: the value of GL_MAX_CLIP_DISTANCES.
  130. */
  131. const glsl_type *const clip_distance_array_type =
  132. glsl_type::get_array_instance(glsl_type::float_type, 8);
  133. add_variable("gl_ClipDistance", ir_var_out, clip_distance_array_type,
  134. instructions, symtab);
  135. }
  136. static void
  137. initialize_vs_variables(exec_list *instructions,
  138. struct _mesa_glsl_parse_state *state)
  139. {
  140. switch (state->language_version) {
  141. case 110:
  142. generate_110_vs_variables(instructions, state->symbols);
  143. break;
  144. case 120:
  145. generate_120_vs_variables(instructions, state->symbols);
  146. break;
  147. case 130:
  148. generate_130_vs_variables(instructions, state->symbols);
  149. break;
  150. }
  151. }
  152. static void
  153. generate_110_fs_variables(exec_list *instructions,
  154. glsl_symbol_table *symtab)
  155. {
  156. for (unsigned i = 0; i < Elements(builtin_core_fs_variables); i++) {
  157. add_builtin_variable(& builtin_core_fs_variables[i],
  158. instructions, symtab);
  159. }
  160. for (unsigned i = 0
  161. ; i < Elements(builtin_110_deprecated_fs_variables)
  162. ; i++) {
  163. add_builtin_variable(& builtin_110_deprecated_fs_variables[i],
  164. instructions, symtab);
  165. }
  166. generate_110_uniforms(instructions, symtab);
  167. /* FINISHME: The size of this array is implementation dependent based on the
  168. * FINISHME: value of GL_MAX_TEXTURE_COORDS. GL_MAX_TEXTURE_COORDS must be
  169. * FINISHME: at least 2, so hard-code 2 for now.
  170. */
  171. const glsl_type *const vec4_type =
  172. glsl_type::get_instance(GLSL_TYPE_FLOAT, 4, 1);
  173. const glsl_type *const vec4_array_type =
  174. glsl_type::get_array_instance(vec4_type, 2);
  175. add_variable("gl_TexCoord", ir_var_in, vec4_array_type, instructions,
  176. symtab);
  177. }
  178. static void
  179. generate_ARB_draw_buffers_fs_variables(exec_list *instructions,
  180. glsl_symbol_table *symtab, bool warn)
  181. {
  182. /* FINISHME: The size of this array is implementation dependent based on the
  183. * FINISHME: value of GL_MAX_DRAW_BUFFERS. GL_MAX_DRAW_BUFFERS must be
  184. * FINISHME: at least 1, so hard-code 1 for now.
  185. */
  186. const glsl_type *const vec4_type =
  187. glsl_type::get_instance(GLSL_TYPE_FLOAT, 4, 1);
  188. const glsl_type *const vec4_array_type =
  189. glsl_type::get_array_instance(vec4_type, 1);
  190. ir_variable *const fd =
  191. add_variable("gl_FragData", ir_var_out, vec4_array_type, instructions,
  192. symtab);
  193. if (warn)
  194. fd->warn_extension = "GL_ARB_draw_buffers";
  195. }
  196. static void
  197. generate_120_fs_variables(exec_list *instructions,
  198. glsl_symbol_table *symtab)
  199. {
  200. generate_110_fs_variables(instructions, symtab);
  201. generate_ARB_draw_buffers_fs_variables(instructions, symtab, false);
  202. }
  203. static void
  204. generate_130_fs_variables(exec_list *instructions,
  205. glsl_symbol_table *symtab)
  206. {
  207. generate_120_fs_variables(instructions, symtab);
  208. /* FINISHME: The size of this array is implementation dependent based on
  209. * FINISHME: the value of GL_MAX_CLIP_DISTANCES.
  210. */
  211. const glsl_type *const clip_distance_array_type =
  212. glsl_type::get_array_instance(glsl_type::float_type, 8);
  213. add_variable("gl_ClipDistance", ir_var_in, clip_distance_array_type,
  214. instructions, symtab);
  215. }
  216. static void
  217. initialize_fs_variables(exec_list *instructions,
  218. struct _mesa_glsl_parse_state *state)
  219. {
  220. switch (state->language_version) {
  221. case 110:
  222. generate_110_fs_variables(instructions, state->symbols);
  223. break;
  224. case 120:
  225. generate_120_fs_variables(instructions, state->symbols);
  226. break;
  227. case 130:
  228. generate_130_fs_variables(instructions, state->symbols);
  229. break;
  230. }
  231. /* Since GL_ARB_draw_buffers is included in GLSL 1.20 and later, we
  232. * can basically ignore any extension settings for it.
  233. */
  234. if (state->language_version < 120) {
  235. if (state->ARB_draw_buffers_enable) {
  236. generate_ARB_draw_buffers_fs_variables(instructions, state->symbols,
  237. state->ARB_draw_buffers_warn);
  238. }
  239. }
  240. }
  241. void
  242. _mesa_glsl_initialize_variables(exec_list *instructions,
  243. struct _mesa_glsl_parse_state *state)
  244. {
  245. switch (state->target) {
  246. case vertex_shader:
  247. initialize_vs_variables(instructions, state);
  248. break;
  249. case geometry_shader:
  250. break;
  251. case fragment_shader:
  252. initialize_fs_variables(instructions, state);
  253. break;
  254. }
  255. }