Clone of mesa.
Ви не можете вибрати більше 25 тем Теми мають розпочинатися з літери або цифри, можуть містити дефіси (-) і не повинні перевищувати 35 символів.

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225
  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. * \file linker.cpp
  25. * GLSL linker implementation
  26. *
  27. * Given a set of shaders that are to be linked to generate a final program,
  28. * there are three distinct stages.
  29. *
  30. * In the first stage shaders are partitioned into groups based on the shader
  31. * type. All shaders of a particular type (e.g., vertex shaders) are linked
  32. * together.
  33. *
  34. * - Undefined references in each shader are resolve to definitions in
  35. * another shader.
  36. * - Types and qualifiers of uniforms, outputs, and global variables defined
  37. * in multiple shaders with the same name are verified to be the same.
  38. * - Initializers for uniforms and global variables defined
  39. * in multiple shaders with the same name are verified to be the same.
  40. *
  41. * The result, in the terminology of the GLSL spec, is a set of shader
  42. * executables for each processing unit.
  43. *
  44. * After the first stage is complete, a series of semantic checks are performed
  45. * on each of the shader executables.
  46. *
  47. * - Each shader executable must define a \c main function.
  48. * - Each vertex shader executable must write to \c gl_Position.
  49. * - Each fragment shader executable must write to either \c gl_FragData or
  50. * \c gl_FragColor.
  51. *
  52. * In the final stage individual shader executables are linked to create a
  53. * complete exectuable.
  54. *
  55. * - Types of uniforms defined in multiple shader stages with the same name
  56. * are verified to be the same.
  57. * - Initializers for uniforms defined in multiple shader stages with the
  58. * same name are verified to be the same.
  59. * - Types and qualifiers of outputs defined in one stage are verified to
  60. * be the same as the types and qualifiers of inputs defined with the same
  61. * name in a later stage.
  62. *
  63. * \author Ian Romanick <ian.d.romanick@intel.com>
  64. */
  65. #include <cstdlib>
  66. #include <cstdio>
  67. #include "glsl_symbol_table.h"
  68. #include "glsl_parser_extras.h"
  69. #include "ir.h"
  70. #include "program.h"
  71. /**
  72. * Visitor that determines whether or not a variable is ever written.
  73. */
  74. class find_assignment_visitor : public ir_hierarchical_visitor {
  75. public:
  76. find_assignment_visitor(const char *name)
  77. : name(name), found(false)
  78. {
  79. /* empty */
  80. }
  81. virtual ir_visitor_status visit_enter(ir_assignment *ir)
  82. {
  83. ir_variable *const var = ir->lhs->variable_referenced();
  84. if (strcmp(name, var->name) == 0) {
  85. found = true;
  86. return visit_stop;
  87. }
  88. return visit_continue_with_parent;
  89. }
  90. bool variable_found()
  91. {
  92. return found;
  93. }
  94. private:
  95. const char *name; /**< Find writes to a variable with this name. */
  96. bool found; /**< Was a write to the variable found? */
  97. };
  98. /**
  99. * Verify that a vertex shader executable meets all semantic requirements
  100. *
  101. * \param shader Vertex shader executable to be verified
  102. */
  103. bool
  104. validate_vertex_shader_executable(struct glsl_shader *shader)
  105. {
  106. if (shader == NULL)
  107. return true;
  108. if (!shader->symbols->get_function("main")) {
  109. printf("error: vertex shader lacks `main'\n");
  110. return false;
  111. }
  112. find_assignment_visitor find("gl_Position");
  113. find.run(&shader->ir);
  114. if (!find.variable_found()) {
  115. printf("error: vertex shader does not write to `gl_Position'\n");
  116. return false;
  117. }
  118. return true;
  119. }
  120. /**
  121. * Verify that a fragment shader executable meets all semantic requirements
  122. *
  123. * \param shader Fragment shader executable to be verified
  124. */
  125. bool
  126. validate_fragment_shader_executable(struct glsl_shader *shader)
  127. {
  128. if (shader == NULL)
  129. return true;
  130. if (!shader->symbols->get_function("main")) {
  131. printf("error: fragment shader lacks `main'\n");
  132. return false;
  133. }
  134. find_assignment_visitor frag_color("gl_FragColor");
  135. find_assignment_visitor frag_data("gl_FragData");
  136. frag_color.run(&shader->ir);
  137. frag_data.run(&shader->ir);
  138. if (!frag_color.variable_found() && !frag_data.variable_found()) {
  139. printf("error: fragment shader does not write to `gl_FragColor' or "
  140. "`gl_FragData'\n");
  141. return false;
  142. }
  143. if (frag_color.variable_found() && frag_data.variable_found()) {
  144. printf("error: fragment shader write to both `gl_FragColor' and "
  145. "`gl_FragData'\n");
  146. return false;
  147. }
  148. return true;
  149. }
  150. void
  151. link_shaders(struct glsl_program *prog)
  152. {
  153. prog->LinkStatus = false;
  154. prog->Validated = false;
  155. prog->_Used = false;
  156. /* Separate the shaders into groups based on their type.
  157. */
  158. struct glsl_shader **vert_shader_list;
  159. unsigned num_vert_shaders = 0;
  160. struct glsl_shader **frag_shader_list;
  161. unsigned num_frag_shaders = 0;
  162. vert_shader_list = (struct glsl_shader **)
  163. calloc(2 * prog->NumShaders, sizeof(struct glsl_shader *));
  164. frag_shader_list = &vert_shader_list[prog->NumShaders];
  165. for (unsigned i = 0; i < prog->NumShaders; i++) {
  166. switch (prog->Shaders[i]->Type) {
  167. case GL_VERTEX_SHADER:
  168. vert_shader_list[num_vert_shaders] = prog->Shaders[i];
  169. num_vert_shaders++;
  170. break;
  171. case GL_FRAGMENT_SHADER:
  172. frag_shader_list[num_frag_shaders] = prog->Shaders[i];
  173. num_frag_shaders++;
  174. break;
  175. case GL_GEOMETRY_SHADER:
  176. /* FINISHME: Support geometry shaders. */
  177. assert(prog->Shaders[i]->Type != GL_GEOMETRY_SHADER);
  178. break;
  179. }
  180. }
  181. /* FINISHME: Implement intra-stage linking. */
  182. assert(num_vert_shaders <= 1);
  183. assert(num_frag_shaders <= 1);
  184. /* Verify that each of the per-target executables is valid.
  185. */
  186. if (!validate_vertex_shader_executable(vert_shader_list[0])
  187. || !validate_fragment_shader_executable(frag_shader_list[0]))
  188. goto done;
  189. /* FINISHME: Perform inter-stage linking. */
  190. prog->LinkStatus = true;
  191. done:
  192. free(vert_shader_list);
  193. }