Clone of mesa.
Vous ne pouvez pas sélectionner plus de 25 sujets Les noms de sujets doivent commencer par une lettre ou un nombre, peuvent contenir des tirets ('-') et peuvent comporter jusqu'à 35 caractères.

ir_variable.cpp 10KB

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