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 10KB

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