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