Paul Berry | 4b11b57 | 2012-12-17 14:20:35 -0800 | [diff] [blame] | 1 | /* |
| 2 | * Copyright © 2012 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 link_varyings.cpp |
| 26 | * |
| 27 | * Linker functions related specifically to linking varyings between shader |
| 28 | * stages. |
| 29 | */ |
| 30 | |
| 31 | |
| 32 | #include "main/mtypes.h" |
| 33 | #include "glsl_symbol_table.h" |
| 34 | #include "ir_optimization.h" |
| 35 | #include "linker.h" |
| 36 | #include "link_varyings.h" |
| 37 | #include "main/macros.h" |
Paul Berry | 99b7833 | 2013-01-29 07:34:52 -0800 | [diff] [blame] | 38 | #include "program/hash_table.h" |
Paul Berry | 53febac | 2013-01-29 05:52:47 -0800 | [diff] [blame] | 39 | #include "program.h" |
Paul Berry | 4b11b57 | 2012-12-17 14:20:35 -0800 | [diff] [blame] | 40 | |
| 41 | |
| 42 | /** |
| 43 | * Validate that outputs from one stage match inputs of another |
| 44 | */ |
| 45 | bool |
| 46 | cross_validate_outputs_to_inputs(struct gl_shader_program *prog, |
| 47 | gl_shader *producer, gl_shader *consumer) |
| 48 | { |
| 49 | glsl_symbol_table parameters; |
| 50 | /* FINISHME: Figure these out dynamically. */ |
| 51 | const char *const producer_stage = "vertex"; |
| 52 | const char *const consumer_stage = "fragment"; |
| 53 | |
| 54 | /* Find all shader outputs in the "producer" stage. |
| 55 | */ |
| 56 | foreach_list(node, producer->ir) { |
| 57 | ir_variable *const var = ((ir_instruction *) node)->as_variable(); |
| 58 | |
Paul Berry | 42a29d8 | 2013-01-11 14:39:32 -0800 | [diff] [blame] | 59 | if ((var == NULL) || (var->mode != ir_var_shader_out)) |
Paul Berry | 4b11b57 | 2012-12-17 14:20:35 -0800 | [diff] [blame] | 60 | continue; |
| 61 | |
| 62 | parameters.add_variable(var); |
| 63 | } |
| 64 | |
| 65 | |
| 66 | /* Find all shader inputs in the "consumer" stage. Any variables that have |
| 67 | * matching outputs already in the symbol table must have the same type and |
| 68 | * qualifiers. |
| 69 | */ |
| 70 | foreach_list(node, consumer->ir) { |
| 71 | ir_variable *const input = ((ir_instruction *) node)->as_variable(); |
| 72 | |
Paul Berry | 42a29d8 | 2013-01-11 14:39:32 -0800 | [diff] [blame] | 73 | if ((input == NULL) || (input->mode != ir_var_shader_in)) |
Paul Berry | 4b11b57 | 2012-12-17 14:20:35 -0800 | [diff] [blame] | 74 | continue; |
| 75 | |
| 76 | ir_variable *const output = parameters.get_variable(input->name); |
| 77 | if (output != NULL) { |
| 78 | /* Check that the types match between stages. |
| 79 | */ |
| 80 | if (input->type != output->type) { |
| 81 | /* There is a bit of a special case for gl_TexCoord. This |
| 82 | * built-in is unsized by default. Applications that variable |
| 83 | * access it must redeclare it with a size. There is some |
| 84 | * language in the GLSL spec that implies the fragment shader |
| 85 | * and vertex shader do not have to agree on this size. Other |
| 86 | * driver behave this way, and one or two applications seem to |
| 87 | * rely on it. |
| 88 | * |
| 89 | * Neither declaration needs to be modified here because the array |
| 90 | * sizes are fixed later when update_array_sizes is called. |
| 91 | * |
| 92 | * From page 48 (page 54 of the PDF) of the GLSL 1.10 spec: |
| 93 | * |
| 94 | * "Unlike user-defined varying variables, the built-in |
| 95 | * varying variables don't have a strict one-to-one |
| 96 | * correspondence between the vertex language and the |
| 97 | * fragment language." |
| 98 | */ |
| 99 | if (!output->type->is_array() |
| 100 | || (strncmp("gl_", output->name, 3) != 0)) { |
| 101 | linker_error(prog, |
| 102 | "%s shader output `%s' declared as type `%s', " |
| 103 | "but %s shader input declared as type `%s'\n", |
| 104 | producer_stage, output->name, |
| 105 | output->type->name, |
| 106 | consumer_stage, input->type->name); |
| 107 | return false; |
| 108 | } |
| 109 | } |
| 110 | |
| 111 | /* Check that all of the qualifiers match between stages. |
| 112 | */ |
| 113 | if (input->centroid != output->centroid) { |
| 114 | linker_error(prog, |
| 115 | "%s shader output `%s' %s centroid qualifier, " |
| 116 | "but %s shader input %s centroid qualifier\n", |
| 117 | producer_stage, |
| 118 | output->name, |
| 119 | (output->centroid) ? "has" : "lacks", |
| 120 | consumer_stage, |
| 121 | (input->centroid) ? "has" : "lacks"); |
| 122 | return false; |
| 123 | } |
| 124 | |
| 125 | if (input->invariant != output->invariant) { |
| 126 | linker_error(prog, |
| 127 | "%s shader output `%s' %s invariant qualifier, " |
| 128 | "but %s shader input %s invariant qualifier\n", |
| 129 | producer_stage, |
| 130 | output->name, |
| 131 | (output->invariant) ? "has" : "lacks", |
| 132 | consumer_stage, |
| 133 | (input->invariant) ? "has" : "lacks"); |
| 134 | return false; |
| 135 | } |
| 136 | |
| 137 | if (input->interpolation != output->interpolation) { |
| 138 | linker_error(prog, |
| 139 | "%s shader output `%s' specifies %s " |
| 140 | "interpolation qualifier, " |
| 141 | "but %s shader input specifies %s " |
| 142 | "interpolation qualifier\n", |
| 143 | producer_stage, |
| 144 | output->name, |
| 145 | output->interpolation_string(), |
| 146 | consumer_stage, |
| 147 | input->interpolation_string()); |
| 148 | return false; |
| 149 | } |
| 150 | } |
| 151 | } |
| 152 | |
| 153 | return true; |
| 154 | } |
| 155 | |
| 156 | |
| 157 | /** |
| 158 | * Initialize this object based on a string that was passed to |
Paul Berry | 53febac | 2013-01-29 05:52:47 -0800 | [diff] [blame] | 159 | * glTransformFeedbackVaryings. |
| 160 | * |
| 161 | * If the input is mal-formed, this call still succeeds, but it sets |
| 162 | * this->var_name to a mal-formed input, so tfeedback_decl::find_output_var() |
| 163 | * will fail to find any matching variable. |
Paul Berry | 4b11b57 | 2012-12-17 14:20:35 -0800 | [diff] [blame] | 164 | */ |
Paul Berry | 53febac | 2013-01-29 05:52:47 -0800 | [diff] [blame] | 165 | void |
Paul Berry | 4b11b57 | 2012-12-17 14:20:35 -0800 | [diff] [blame] | 166 | tfeedback_decl::init(struct gl_context *ctx, struct gl_shader_program *prog, |
| 167 | const void *mem_ctx, const char *input) |
| 168 | { |
| 169 | /* We don't have to be pedantic about what is a valid GLSL variable name, |
| 170 | * because any variable with an invalid name can't exist in the IR anyway. |
| 171 | */ |
| 172 | |
| 173 | this->location = -1; |
| 174 | this->orig_name = input; |
| 175 | this->is_clip_distance_mesa = false; |
| 176 | this->skip_components = 0; |
| 177 | this->next_buffer_separator = false; |
Paul Berry | 99b7833 | 2013-01-29 07:34:52 -0800 | [diff] [blame] | 178 | this->matched_candidate = NULL; |
Paul Berry | 4b11b57 | 2012-12-17 14:20:35 -0800 | [diff] [blame] | 179 | |
| 180 | if (ctx->Extensions.ARB_transform_feedback3) { |
| 181 | /* Parse gl_NextBuffer. */ |
| 182 | if (strcmp(input, "gl_NextBuffer") == 0) { |
| 183 | this->next_buffer_separator = true; |
Paul Berry | 53febac | 2013-01-29 05:52:47 -0800 | [diff] [blame] | 184 | return; |
Paul Berry | 4b11b57 | 2012-12-17 14:20:35 -0800 | [diff] [blame] | 185 | } |
| 186 | |
| 187 | /* Parse gl_SkipComponents. */ |
| 188 | if (strcmp(input, "gl_SkipComponents1") == 0) |
| 189 | this->skip_components = 1; |
| 190 | else if (strcmp(input, "gl_SkipComponents2") == 0) |
| 191 | this->skip_components = 2; |
| 192 | else if (strcmp(input, "gl_SkipComponents3") == 0) |
| 193 | this->skip_components = 3; |
| 194 | else if (strcmp(input, "gl_SkipComponents4") == 0) |
| 195 | this->skip_components = 4; |
| 196 | |
| 197 | if (this->skip_components) |
Paul Berry | 53febac | 2013-01-29 05:52:47 -0800 | [diff] [blame] | 198 | return; |
Paul Berry | 4b11b57 | 2012-12-17 14:20:35 -0800 | [diff] [blame] | 199 | } |
| 200 | |
| 201 | /* Parse a declaration. */ |
Paul Berry | 53febac | 2013-01-29 05:52:47 -0800 | [diff] [blame] | 202 | const char *base_name_end; |
| 203 | long subscript = parse_program_resource_name(input, &base_name_end); |
| 204 | this->var_name = ralloc_strndup(mem_ctx, input, base_name_end - input); |
| 205 | if (subscript >= 0) { |
| 206 | this->array_subscript = subscript; |
Paul Berry | 4b11b57 | 2012-12-17 14:20:35 -0800 | [diff] [blame] | 207 | this->is_subscripted = true; |
| 208 | } else { |
Paul Berry | 4b11b57 | 2012-12-17 14:20:35 -0800 | [diff] [blame] | 209 | this->is_subscripted = false; |
| 210 | } |
| 211 | |
| 212 | /* For drivers that lower gl_ClipDistance to gl_ClipDistanceMESA, this |
| 213 | * class must behave specially to account for the fact that gl_ClipDistance |
| 214 | * is converted from a float[8] to a vec4[2]. |
| 215 | */ |
| 216 | if (ctx->ShaderCompilerOptions[MESA_SHADER_VERTEX].LowerClipDistance && |
| 217 | strcmp(this->var_name, "gl_ClipDistance") == 0) { |
| 218 | this->is_clip_distance_mesa = true; |
| 219 | } |
Paul Berry | 4b11b57 | 2012-12-17 14:20:35 -0800 | [diff] [blame] | 220 | } |
| 221 | |
| 222 | |
| 223 | /** |
| 224 | * Determine whether two tfeedback_decl objects refer to the same variable and |
| 225 | * array index (if applicable). |
| 226 | */ |
| 227 | bool |
| 228 | tfeedback_decl::is_same(const tfeedback_decl &x, const tfeedback_decl &y) |
| 229 | { |
| 230 | assert(x.is_varying() && y.is_varying()); |
| 231 | |
| 232 | if (strcmp(x.var_name, y.var_name) != 0) |
| 233 | return false; |
| 234 | if (x.is_subscripted != y.is_subscripted) |
| 235 | return false; |
| 236 | if (x.is_subscripted && x.array_subscript != y.array_subscript) |
| 237 | return false; |
| 238 | return true; |
| 239 | } |
| 240 | |
| 241 | |
| 242 | /** |
Paul Berry | 99b7833 | 2013-01-29 07:34:52 -0800 | [diff] [blame] | 243 | * Assign a location for this tfeedback_decl object based on the transform |
| 244 | * feedback candidate found by find_candidate. |
Paul Berry | 4b11b57 | 2012-12-17 14:20:35 -0800 | [diff] [blame] | 245 | * |
| 246 | * If an error occurs, the error is reported through linker_error() and false |
| 247 | * is returned. |
| 248 | */ |
| 249 | bool |
| 250 | tfeedback_decl::assign_location(struct gl_context *ctx, |
Paul Berry | 99b7833 | 2013-01-29 07:34:52 -0800 | [diff] [blame] | 251 | struct gl_shader_program *prog) |
Paul Berry | 4b11b57 | 2012-12-17 14:20:35 -0800 | [diff] [blame] | 252 | { |
| 253 | assert(this->is_varying()); |
| 254 | |
Paul Berry | 99b7833 | 2013-01-29 07:34:52 -0800 | [diff] [blame] | 255 | unsigned fine_location |
| 256 | = this->matched_candidate->toplevel_var->location * 4 |
| 257 | + this->matched_candidate->toplevel_var->location_frac |
| 258 | + this->matched_candidate->offset; |
| 259 | |
| 260 | if (this->matched_candidate->type->is_array()) { |
Paul Berry | 4b11b57 | 2012-12-17 14:20:35 -0800 | [diff] [blame] | 261 | /* Array variable */ |
| 262 | const unsigned matrix_cols = |
Paul Berry | 99b7833 | 2013-01-29 07:34:52 -0800 | [diff] [blame] | 263 | this->matched_candidate->type->fields.array->matrix_columns; |
Paul Berry | 4b11b57 | 2012-12-17 14:20:35 -0800 | [diff] [blame] | 264 | const unsigned vector_elements = |
Paul Berry | 99b7833 | 2013-01-29 07:34:52 -0800 | [diff] [blame] | 265 | this->matched_candidate->type->fields.array->vector_elements; |
Paul Berry | 4b11b57 | 2012-12-17 14:20:35 -0800 | [diff] [blame] | 266 | unsigned actual_array_size = this->is_clip_distance_mesa ? |
Paul Berry | 99b7833 | 2013-01-29 07:34:52 -0800 | [diff] [blame] | 267 | prog->Vert.ClipDistanceArraySize : |
| 268 | this->matched_candidate->type->array_size(); |
Paul Berry | 4b11b57 | 2012-12-17 14:20:35 -0800 | [diff] [blame] | 269 | |
| 270 | if (this->is_subscripted) { |
| 271 | /* Check array bounds. */ |
| 272 | if (this->array_subscript >= actual_array_size) { |
| 273 | linker_error(prog, "Transform feedback varying %s has index " |
| 274 | "%i, but the array size is %u.", |
| 275 | this->orig_name, this->array_subscript, |
| 276 | actual_array_size); |
| 277 | return false; |
| 278 | } |
Paul Berry | 99b7833 | 2013-01-29 07:34:52 -0800 | [diff] [blame] | 279 | unsigned array_elem_size = this->is_clip_distance_mesa ? |
| 280 | 1 : vector_elements * matrix_cols; |
| 281 | fine_location += array_elem_size * this->array_subscript; |
Paul Berry | 4b11b57 | 2012-12-17 14:20:35 -0800 | [diff] [blame] | 282 | this->size = 1; |
| 283 | } else { |
Paul Berry | 4b11b57 | 2012-12-17 14:20:35 -0800 | [diff] [blame] | 284 | this->size = actual_array_size; |
| 285 | } |
| 286 | this->vector_elements = vector_elements; |
| 287 | this->matrix_columns = matrix_cols; |
| 288 | if (this->is_clip_distance_mesa) |
| 289 | this->type = GL_FLOAT; |
| 290 | else |
Paul Berry | 99b7833 | 2013-01-29 07:34:52 -0800 | [diff] [blame] | 291 | this->type = this->matched_candidate->type->fields.array->gl_type; |
Paul Berry | 4b11b57 | 2012-12-17 14:20:35 -0800 | [diff] [blame] | 292 | } else { |
| 293 | /* Regular variable (scalar, vector, or matrix) */ |
| 294 | if (this->is_subscripted) { |
| 295 | linker_error(prog, "Transform feedback varying %s requested, " |
| 296 | "but %s is not an array.", |
| 297 | this->orig_name, this->var_name); |
| 298 | return false; |
| 299 | } |
Paul Berry | 4b11b57 | 2012-12-17 14:20:35 -0800 | [diff] [blame] | 300 | this->size = 1; |
Paul Berry | 99b7833 | 2013-01-29 07:34:52 -0800 | [diff] [blame] | 301 | this->vector_elements = this->matched_candidate->type->vector_elements; |
| 302 | this->matrix_columns = this->matched_candidate->type->matrix_columns; |
| 303 | this->type = this->matched_candidate->type->gl_type; |
Paul Berry | 4b11b57 | 2012-12-17 14:20:35 -0800 | [diff] [blame] | 304 | } |
Paul Berry | 99b7833 | 2013-01-29 07:34:52 -0800 | [diff] [blame] | 305 | this->location = fine_location / 4; |
| 306 | this->location_frac = fine_location % 4; |
Paul Berry | 4b11b57 | 2012-12-17 14:20:35 -0800 | [diff] [blame] | 307 | |
| 308 | /* From GL_EXT_transform_feedback: |
| 309 | * A program will fail to link if: |
| 310 | * |
| 311 | * * the total number of components to capture in any varying |
| 312 | * variable in <varyings> is greater than the constant |
| 313 | * MAX_TRANSFORM_FEEDBACK_SEPARATE_COMPONENTS_EXT and the |
| 314 | * buffer mode is SEPARATE_ATTRIBS_EXT; |
| 315 | */ |
| 316 | if (prog->TransformFeedback.BufferMode == GL_SEPARATE_ATTRIBS && |
| 317 | this->num_components() > |
| 318 | ctx->Const.MaxTransformFeedbackSeparateComponents) { |
| 319 | linker_error(prog, "Transform feedback varying %s exceeds " |
| 320 | "MAX_TRANSFORM_FEEDBACK_SEPARATE_COMPONENTS.", |
| 321 | this->orig_name); |
| 322 | return false; |
| 323 | } |
| 324 | |
| 325 | return true; |
| 326 | } |
| 327 | |
| 328 | |
| 329 | unsigned |
| 330 | tfeedback_decl::get_num_outputs() const |
| 331 | { |
| 332 | if (!this->is_varying()) { |
| 333 | return 0; |
| 334 | } |
| 335 | |
| 336 | return (this->num_components() + this->location_frac + 3)/4; |
| 337 | } |
| 338 | |
| 339 | |
| 340 | /** |
| 341 | * Update gl_transform_feedback_info to reflect this tfeedback_decl. |
| 342 | * |
| 343 | * If an error occurs, the error is reported through linker_error() and false |
| 344 | * is returned. |
| 345 | */ |
| 346 | bool |
| 347 | tfeedback_decl::store(struct gl_context *ctx, struct gl_shader_program *prog, |
| 348 | struct gl_transform_feedback_info *info, |
| 349 | unsigned buffer, const unsigned max_outputs) const |
| 350 | { |
| 351 | assert(!this->next_buffer_separator); |
| 352 | |
| 353 | /* Handle gl_SkipComponents. */ |
| 354 | if (this->skip_components) { |
| 355 | info->BufferStride[buffer] += this->skip_components; |
| 356 | return true; |
| 357 | } |
| 358 | |
| 359 | /* From GL_EXT_transform_feedback: |
| 360 | * A program will fail to link if: |
| 361 | * |
| 362 | * * the total number of components to capture is greater than |
| 363 | * the constant MAX_TRANSFORM_FEEDBACK_INTERLEAVED_COMPONENTS_EXT |
| 364 | * and the buffer mode is INTERLEAVED_ATTRIBS_EXT. |
| 365 | */ |
| 366 | if (prog->TransformFeedback.BufferMode == GL_INTERLEAVED_ATTRIBS && |
| 367 | info->BufferStride[buffer] + this->num_components() > |
| 368 | ctx->Const.MaxTransformFeedbackInterleavedComponents) { |
| 369 | linker_error(prog, "The MAX_TRANSFORM_FEEDBACK_INTERLEAVED_COMPONENTS " |
| 370 | "limit has been exceeded."); |
| 371 | return false; |
| 372 | } |
| 373 | |
| 374 | unsigned location = this->location; |
| 375 | unsigned location_frac = this->location_frac; |
| 376 | unsigned num_components = this->num_components(); |
| 377 | while (num_components > 0) { |
| 378 | unsigned output_size = MIN2(num_components, 4 - location_frac); |
| 379 | assert(info->NumOutputs < max_outputs); |
| 380 | info->Outputs[info->NumOutputs].ComponentOffset = location_frac; |
| 381 | info->Outputs[info->NumOutputs].OutputRegister = location; |
| 382 | info->Outputs[info->NumOutputs].NumComponents = output_size; |
| 383 | info->Outputs[info->NumOutputs].OutputBuffer = buffer; |
| 384 | info->Outputs[info->NumOutputs].DstOffset = info->BufferStride[buffer]; |
| 385 | ++info->NumOutputs; |
| 386 | info->BufferStride[buffer] += output_size; |
| 387 | num_components -= output_size; |
| 388 | location++; |
| 389 | location_frac = 0; |
| 390 | } |
| 391 | |
| 392 | info->Varyings[info->NumVarying].Name = ralloc_strdup(prog, this->orig_name); |
| 393 | info->Varyings[info->NumVarying].Type = this->type; |
| 394 | info->Varyings[info->NumVarying].Size = this->size; |
| 395 | info->NumVarying++; |
| 396 | |
| 397 | return true; |
| 398 | } |
| 399 | |
| 400 | |
Paul Berry | 99b7833 | 2013-01-29 07:34:52 -0800 | [diff] [blame] | 401 | const tfeedback_candidate * |
| 402 | tfeedback_decl::find_candidate(gl_shader_program *prog, |
| 403 | hash_table *tfeedback_candidates) |
Paul Berry | 4b11b57 | 2012-12-17 14:20:35 -0800 | [diff] [blame] | 404 | { |
| 405 | const char *name = this->is_clip_distance_mesa |
| 406 | ? "gl_ClipDistanceMESA" : this->var_name; |
Paul Berry | 99b7833 | 2013-01-29 07:34:52 -0800 | [diff] [blame] | 407 | this->matched_candidate = (const tfeedback_candidate *) |
| 408 | hash_table_find(tfeedback_candidates, name); |
| 409 | if (!this->matched_candidate) { |
| 410 | /* From GL_EXT_transform_feedback: |
| 411 | * A program will fail to link if: |
| 412 | * |
| 413 | * * any variable name specified in the <varyings> array is not |
| 414 | * declared as an output in the geometry shader (if present) or |
| 415 | * the vertex shader (if no geometry shader is present); |
| 416 | */ |
| 417 | linker_error(prog, "Transform feedback varying %s undeclared.", |
| 418 | this->orig_name); |
Paul Berry | cd53457 | 2013-01-17 13:45:30 -0800 | [diff] [blame] | 419 | } |
Paul Berry | 99b7833 | 2013-01-29 07:34:52 -0800 | [diff] [blame] | 420 | return this->matched_candidate; |
Paul Berry | 4b11b57 | 2012-12-17 14:20:35 -0800 | [diff] [blame] | 421 | } |
| 422 | |
| 423 | |
| 424 | /** |
| 425 | * Parse all the transform feedback declarations that were passed to |
| 426 | * glTransformFeedbackVaryings() and store them in tfeedback_decl objects. |
| 427 | * |
| 428 | * If an error occurs, the error is reported through linker_error() and false |
| 429 | * is returned. |
| 430 | */ |
| 431 | bool |
| 432 | parse_tfeedback_decls(struct gl_context *ctx, struct gl_shader_program *prog, |
| 433 | const void *mem_ctx, unsigned num_names, |
| 434 | char **varying_names, tfeedback_decl *decls) |
| 435 | { |
| 436 | for (unsigned i = 0; i < num_names; ++i) { |
Paul Berry | 53febac | 2013-01-29 05:52:47 -0800 | [diff] [blame] | 437 | decls[i].init(ctx, prog, mem_ctx, varying_names[i]); |
Paul Berry | 4b11b57 | 2012-12-17 14:20:35 -0800 | [diff] [blame] | 438 | |
| 439 | if (!decls[i].is_varying()) |
| 440 | continue; |
| 441 | |
| 442 | /* From GL_EXT_transform_feedback: |
| 443 | * A program will fail to link if: |
| 444 | * |
| 445 | * * any two entries in the <varyings> array specify the same varying |
| 446 | * variable; |
| 447 | * |
| 448 | * We interpret this to mean "any two entries in the <varyings> array |
| 449 | * specify the same varying variable and array index", since transform |
| 450 | * feedback of arrays would be useless otherwise. |
| 451 | */ |
| 452 | for (unsigned j = 0; j < i; ++j) { |
| 453 | if (!decls[j].is_varying()) |
| 454 | continue; |
| 455 | |
| 456 | if (tfeedback_decl::is_same(decls[i], decls[j])) { |
| 457 | linker_error(prog, "Transform feedback varying %s specified " |
| 458 | "more than once.", varying_names[i]); |
| 459 | return false; |
| 460 | } |
| 461 | } |
| 462 | } |
| 463 | return true; |
| 464 | } |
| 465 | |
| 466 | |
| 467 | /** |
| 468 | * Store transform feedback location assignments into |
| 469 | * prog->LinkedTransformFeedback based on the data stored in tfeedback_decls. |
| 470 | * |
| 471 | * If an error occurs, the error is reported through linker_error() and false |
| 472 | * is returned. |
| 473 | */ |
| 474 | bool |
| 475 | store_tfeedback_info(struct gl_context *ctx, struct gl_shader_program *prog, |
| 476 | unsigned num_tfeedback_decls, |
| 477 | tfeedback_decl *tfeedback_decls) |
| 478 | { |
| 479 | bool separate_attribs_mode = |
| 480 | prog->TransformFeedback.BufferMode == GL_SEPARATE_ATTRIBS; |
| 481 | |
| 482 | ralloc_free(prog->LinkedTransformFeedback.Varyings); |
| 483 | ralloc_free(prog->LinkedTransformFeedback.Outputs); |
| 484 | |
| 485 | memset(&prog->LinkedTransformFeedback, 0, |
| 486 | sizeof(prog->LinkedTransformFeedback)); |
| 487 | |
| 488 | prog->LinkedTransformFeedback.Varyings = |
| 489 | rzalloc_array(prog, |
| 490 | struct gl_transform_feedback_varying_info, |
| 491 | num_tfeedback_decls); |
| 492 | |
| 493 | unsigned num_outputs = 0; |
| 494 | for (unsigned i = 0; i < num_tfeedback_decls; ++i) |
| 495 | num_outputs += tfeedback_decls[i].get_num_outputs(); |
| 496 | |
| 497 | prog->LinkedTransformFeedback.Outputs = |
| 498 | rzalloc_array(prog, |
| 499 | struct gl_transform_feedback_output, |
| 500 | num_outputs); |
| 501 | |
| 502 | unsigned num_buffers = 0; |
| 503 | |
| 504 | if (separate_attribs_mode) { |
| 505 | /* GL_SEPARATE_ATTRIBS */ |
| 506 | for (unsigned i = 0; i < num_tfeedback_decls; ++i) { |
| 507 | if (!tfeedback_decls[i].store(ctx, prog, &prog->LinkedTransformFeedback, |
| 508 | num_buffers, num_outputs)) |
| 509 | return false; |
| 510 | |
| 511 | num_buffers++; |
| 512 | } |
| 513 | } |
| 514 | else { |
| 515 | /* GL_INVERLEAVED_ATTRIBS */ |
| 516 | for (unsigned i = 0; i < num_tfeedback_decls; ++i) { |
| 517 | if (tfeedback_decls[i].is_next_buffer_separator()) { |
| 518 | num_buffers++; |
| 519 | continue; |
| 520 | } |
| 521 | |
| 522 | if (!tfeedback_decls[i].store(ctx, prog, |
| 523 | &prog->LinkedTransformFeedback, |
| 524 | num_buffers, num_outputs)) |
| 525 | return false; |
| 526 | } |
| 527 | num_buffers++; |
| 528 | } |
| 529 | |
| 530 | assert(prog->LinkedTransformFeedback.NumOutputs == num_outputs); |
| 531 | |
| 532 | prog->LinkedTransformFeedback.NumBuffers = num_buffers; |
| 533 | return true; |
| 534 | } |
| 535 | |
| 536 | |
| 537 | /** |
| 538 | * Data structure recording the relationship between outputs of one shader |
| 539 | * stage (the "producer") and inputs of another (the "consumer"). |
| 540 | */ |
| 541 | class varying_matches |
| 542 | { |
| 543 | public: |
| 544 | varying_matches(bool disable_varying_packing); |
| 545 | ~varying_matches(); |
| 546 | void record(ir_variable *producer_var, ir_variable *consumer_var); |
| 547 | unsigned assign_locations(); |
| 548 | void store_locations(unsigned producer_base, unsigned consumer_base) const; |
| 549 | |
| 550 | private: |
| 551 | /** |
| 552 | * If true, this driver disables varying packing, so all varyings need to |
| 553 | * be aligned on slot boundaries, and take up a number of slots equal to |
| 554 | * their number of matrix columns times their array size. |
| 555 | */ |
| 556 | const bool disable_varying_packing; |
| 557 | |
| 558 | /** |
| 559 | * Enum representing the order in which varyings are packed within a |
| 560 | * packing class. |
| 561 | * |
| 562 | * Currently we pack vec4's first, then vec2's, then scalar values, then |
| 563 | * vec3's. This order ensures that the only vectors that are at risk of |
| 564 | * having to be "double parked" (split between two adjacent varying slots) |
| 565 | * are the vec3's. |
| 566 | */ |
| 567 | enum packing_order_enum { |
| 568 | PACKING_ORDER_VEC4, |
| 569 | PACKING_ORDER_VEC2, |
| 570 | PACKING_ORDER_SCALAR, |
| 571 | PACKING_ORDER_VEC3, |
| 572 | }; |
| 573 | |
| 574 | static unsigned compute_packing_class(ir_variable *var); |
| 575 | static packing_order_enum compute_packing_order(ir_variable *var); |
| 576 | static int match_comparator(const void *x_generic, const void *y_generic); |
| 577 | |
| 578 | /** |
| 579 | * Structure recording the relationship between a single producer output |
| 580 | * and a single consumer input. |
| 581 | */ |
| 582 | struct match { |
| 583 | /** |
| 584 | * Packing class for this varying, computed by compute_packing_class(). |
| 585 | */ |
| 586 | unsigned packing_class; |
| 587 | |
| 588 | /** |
| 589 | * Packing order for this varying, computed by compute_packing_order(). |
| 590 | */ |
| 591 | packing_order_enum packing_order; |
| 592 | unsigned num_components; |
| 593 | |
| 594 | /** |
| 595 | * The output variable in the producer stage. |
| 596 | */ |
| 597 | ir_variable *producer_var; |
| 598 | |
| 599 | /** |
| 600 | * The input variable in the consumer stage. |
| 601 | */ |
| 602 | ir_variable *consumer_var; |
| 603 | |
| 604 | /** |
| 605 | * The location which has been assigned for this varying. This is |
| 606 | * expressed in multiples of a float, with the first generic varying |
| 607 | * (i.e. the one referred to by VERT_RESULT_VAR0 or FRAG_ATTRIB_VAR0) |
| 608 | * represented by the value 0. |
| 609 | */ |
| 610 | unsigned generic_location; |
| 611 | } *matches; |
| 612 | |
| 613 | /** |
| 614 | * The number of elements in the \c matches array that are currently in |
| 615 | * use. |
| 616 | */ |
| 617 | unsigned num_matches; |
| 618 | |
| 619 | /** |
| 620 | * The number of elements that were set aside for the \c matches array when |
| 621 | * it was allocated. |
| 622 | */ |
| 623 | unsigned matches_capacity; |
| 624 | }; |
| 625 | |
| 626 | |
| 627 | varying_matches::varying_matches(bool disable_varying_packing) |
| 628 | : disable_varying_packing(disable_varying_packing) |
| 629 | { |
| 630 | /* Note: this initial capacity is rather arbitrarily chosen to be large |
| 631 | * enough for many cases without wasting an unreasonable amount of space. |
| 632 | * varying_matches::record() will resize the array if there are more than |
| 633 | * this number of varyings. |
| 634 | */ |
| 635 | this->matches_capacity = 8; |
| 636 | this->matches = (match *) |
| 637 | malloc(sizeof(*this->matches) * this->matches_capacity); |
| 638 | this->num_matches = 0; |
| 639 | } |
| 640 | |
| 641 | |
| 642 | varying_matches::~varying_matches() |
| 643 | { |
| 644 | free(this->matches); |
| 645 | } |
| 646 | |
| 647 | |
| 648 | /** |
| 649 | * Record the given producer/consumer variable pair in the list of variables |
| 650 | * that should later be assigned locations. |
| 651 | * |
| 652 | * It is permissible for \c consumer_var to be NULL (this happens if a |
| 653 | * variable is output by the producer and consumed by transform feedback, but |
| 654 | * not consumed by the consumer). |
| 655 | * |
| 656 | * If \c producer_var has already been paired up with a consumer_var, or |
| 657 | * producer_var is part of fixed pipeline functionality (and hence already has |
| 658 | * a location assigned), this function has no effect. |
| 659 | */ |
| 660 | void |
| 661 | varying_matches::record(ir_variable *producer_var, ir_variable *consumer_var) |
| 662 | { |
| 663 | if (!producer_var->is_unmatched_generic_inout) { |
| 664 | /* Either a location already exists for this variable (since it is part |
| 665 | * of fixed functionality), or it has already been recorded as part of a |
| 666 | * previous match. |
| 667 | */ |
| 668 | return; |
| 669 | } |
| 670 | |
| 671 | if (this->num_matches == this->matches_capacity) { |
| 672 | this->matches_capacity *= 2; |
| 673 | this->matches = (match *) |
| 674 | realloc(this->matches, |
| 675 | sizeof(*this->matches) * this->matches_capacity); |
| 676 | } |
| 677 | this->matches[this->num_matches].packing_class |
| 678 | = this->compute_packing_class(producer_var); |
| 679 | this->matches[this->num_matches].packing_order |
| 680 | = this->compute_packing_order(producer_var); |
| 681 | if (this->disable_varying_packing) { |
| 682 | unsigned slots = producer_var->type->is_array() |
| 683 | ? (producer_var->type->length |
| 684 | * producer_var->type->fields.array->matrix_columns) |
| 685 | : producer_var->type->matrix_columns; |
| 686 | this->matches[this->num_matches].num_components = 4 * slots; |
| 687 | } else { |
| 688 | this->matches[this->num_matches].num_components |
| 689 | = producer_var->type->component_slots(); |
| 690 | } |
| 691 | this->matches[this->num_matches].producer_var = producer_var; |
| 692 | this->matches[this->num_matches].consumer_var = consumer_var; |
| 693 | this->num_matches++; |
| 694 | producer_var->is_unmatched_generic_inout = 0; |
| 695 | if (consumer_var) |
| 696 | consumer_var->is_unmatched_generic_inout = 0; |
| 697 | } |
| 698 | |
| 699 | |
| 700 | /** |
| 701 | * Choose locations for all of the variable matches that were previously |
| 702 | * passed to varying_matches::record(). |
| 703 | */ |
| 704 | unsigned |
| 705 | varying_matches::assign_locations() |
| 706 | { |
| 707 | /* Sort varying matches into an order that makes them easy to pack. */ |
| 708 | qsort(this->matches, this->num_matches, sizeof(*this->matches), |
| 709 | &varying_matches::match_comparator); |
| 710 | |
| 711 | unsigned generic_location = 0; |
| 712 | |
| 713 | for (unsigned i = 0; i < this->num_matches; i++) { |
| 714 | /* Advance to the next slot if this varying has a different packing |
| 715 | * class than the previous one, and we're not already on a slot |
| 716 | * boundary. |
| 717 | */ |
| 718 | if (i > 0 && |
| 719 | this->matches[i - 1].packing_class |
| 720 | != this->matches[i].packing_class) { |
| 721 | generic_location = ALIGN(generic_location, 4); |
| 722 | } |
| 723 | |
| 724 | this->matches[i].generic_location = generic_location; |
| 725 | |
| 726 | generic_location += this->matches[i].num_components; |
| 727 | } |
| 728 | |
| 729 | return (generic_location + 3) / 4; |
| 730 | } |
| 731 | |
| 732 | |
| 733 | /** |
| 734 | * Update the producer and consumer shaders to reflect the locations |
| 735 | * assignments that were made by varying_matches::assign_locations(). |
| 736 | */ |
| 737 | void |
| 738 | varying_matches::store_locations(unsigned producer_base, |
| 739 | unsigned consumer_base) const |
| 740 | { |
| 741 | for (unsigned i = 0; i < this->num_matches; i++) { |
| 742 | ir_variable *producer_var = this->matches[i].producer_var; |
| 743 | ir_variable *consumer_var = this->matches[i].consumer_var; |
| 744 | unsigned generic_location = this->matches[i].generic_location; |
| 745 | unsigned slot = generic_location / 4; |
| 746 | unsigned offset = generic_location % 4; |
| 747 | |
| 748 | producer_var->location = producer_base + slot; |
| 749 | producer_var->location_frac = offset; |
| 750 | if (consumer_var) { |
| 751 | assert(consumer_var->location == -1); |
| 752 | consumer_var->location = consumer_base + slot; |
| 753 | consumer_var->location_frac = offset; |
| 754 | } |
| 755 | } |
| 756 | } |
| 757 | |
| 758 | |
| 759 | /** |
| 760 | * Compute the "packing class" of the given varying. This is an unsigned |
| 761 | * integer with the property that two variables in the same packing class can |
| 762 | * be safely backed into the same vec4. |
| 763 | */ |
| 764 | unsigned |
| 765 | varying_matches::compute_packing_class(ir_variable *var) |
| 766 | { |
Paul Berry | c35abcd | 2012-12-18 16:37:52 -0800 | [diff] [blame] | 767 | /* Without help from the back-end, there is no way to pack together |
| 768 | * variables with different interpolation types, because |
| 769 | * lower_packed_varyings must choose exactly one interpolation type for |
| 770 | * each packed varying it creates. |
Paul Berry | 4b11b57 | 2012-12-17 14:20:35 -0800 | [diff] [blame] | 771 | * |
Paul Berry | c35abcd | 2012-12-18 16:37:52 -0800 | [diff] [blame] | 772 | * However, we can safely pack together floats, ints, and uints, because: |
| 773 | * |
| 774 | * - varyings of base type "int" and "uint" must use the "flat" |
| 775 | * interpolation type, which can only occur in GLSL 1.30 and above. |
| 776 | * |
| 777 | * - On platforms that support GLSL 1.30 and above, lower_packed_varyings |
| 778 | * can store flat floats as ints without losing any information (using |
| 779 | * the ir_unop_bitcast_* opcodes). |
| 780 | * |
| 781 | * Therefore, the packing class depends only on the interpolation type. |
Paul Berry | 4b11b57 | 2012-12-17 14:20:35 -0800 | [diff] [blame] | 782 | */ |
| 783 | unsigned packing_class = var->centroid ? 1 : 0; |
| 784 | packing_class *= 4; |
| 785 | packing_class += var->interpolation; |
Paul Berry | 4b11b57 | 2012-12-17 14:20:35 -0800 | [diff] [blame] | 786 | return packing_class; |
| 787 | } |
| 788 | |
| 789 | |
| 790 | /** |
| 791 | * Compute the "packing order" of the given varying. This is a sort key we |
| 792 | * use to determine when to attempt to pack the given varying relative to |
| 793 | * other varyings in the same packing class. |
| 794 | */ |
| 795 | varying_matches::packing_order_enum |
| 796 | varying_matches::compute_packing_order(ir_variable *var) |
| 797 | { |
| 798 | const glsl_type *element_type = var->type; |
| 799 | |
Paul Berry | 4b11b57 | 2012-12-17 14:20:35 -0800 | [diff] [blame] | 800 | while (element_type->base_type == GLSL_TYPE_ARRAY) { |
| 801 | element_type = element_type->fields.array; |
| 802 | } |
| 803 | |
Paul Berry | 88e4bfd | 2013-01-11 12:56:03 -0800 | [diff] [blame] | 804 | switch (element_type->component_slots() % 4) { |
Paul Berry | 4b11b57 | 2012-12-17 14:20:35 -0800 | [diff] [blame] | 805 | case 1: return PACKING_ORDER_SCALAR; |
| 806 | case 2: return PACKING_ORDER_VEC2; |
| 807 | case 3: return PACKING_ORDER_VEC3; |
Paul Berry | 88e4bfd | 2013-01-11 12:56:03 -0800 | [diff] [blame] | 808 | case 0: return PACKING_ORDER_VEC4; |
Paul Berry | 4b11b57 | 2012-12-17 14:20:35 -0800 | [diff] [blame] | 809 | default: |
| 810 | assert(!"Unexpected value of vector_elements"); |
| 811 | return PACKING_ORDER_VEC4; |
| 812 | } |
| 813 | } |
| 814 | |
| 815 | |
| 816 | /** |
| 817 | * Comparison function passed to qsort() to sort varyings by packing_class and |
| 818 | * then by packing_order. |
| 819 | */ |
| 820 | int |
| 821 | varying_matches::match_comparator(const void *x_generic, const void *y_generic) |
| 822 | { |
| 823 | const match *x = (const match *) x_generic; |
| 824 | const match *y = (const match *) y_generic; |
| 825 | |
| 826 | if (x->packing_class != y->packing_class) |
| 827 | return x->packing_class - y->packing_class; |
| 828 | return x->packing_order - y->packing_order; |
| 829 | } |
| 830 | |
| 831 | |
| 832 | /** |
| 833 | * Is the given variable a varying variable to be counted against the |
| 834 | * limit in ctx->Const.MaxVarying? |
| 835 | * This includes variables such as texcoords, colors and generic |
| 836 | * varyings, but excludes variables such as gl_FrontFacing and gl_FragCoord. |
| 837 | */ |
| 838 | static bool |
| 839 | is_varying_var(GLenum shaderType, const ir_variable *var) |
| 840 | { |
| 841 | /* Only fragment shaders will take a varying variable as an input */ |
| 842 | if (shaderType == GL_FRAGMENT_SHADER && |
Paul Berry | 42a29d8 | 2013-01-11 14:39:32 -0800 | [diff] [blame] | 843 | var->mode == ir_var_shader_in) { |
Paul Berry | 4b11b57 | 2012-12-17 14:20:35 -0800 | [diff] [blame] | 844 | switch (var->location) { |
| 845 | case FRAG_ATTRIB_WPOS: |
| 846 | case FRAG_ATTRIB_FACE: |
| 847 | case FRAG_ATTRIB_PNTC: |
| 848 | return false; |
| 849 | default: |
| 850 | return true; |
| 851 | } |
| 852 | } |
| 853 | return false; |
| 854 | } |
| 855 | |
| 856 | |
| 857 | /** |
Paul Berry | 99b7833 | 2013-01-29 07:34:52 -0800 | [diff] [blame] | 858 | * Visitor class that generates tfeedback_candidate structs describing all |
| 859 | * possible targets of transform feedback. |
| 860 | * |
| 861 | * tfeedback_candidate structs are stored in the hash table |
| 862 | * tfeedback_candidates, which is passed to the constructor. This hash table |
| 863 | * maps varying names to instances of the tfeedback_candidate struct. |
| 864 | */ |
| 865 | class tfeedback_candidate_generator : public program_resource_visitor |
| 866 | { |
| 867 | public: |
| 868 | tfeedback_candidate_generator(void *mem_ctx, |
| 869 | hash_table *tfeedback_candidates) |
| 870 | : mem_ctx(mem_ctx), |
Vinson Lee | b681ed6 | 2013-02-05 22:53:20 -0800 | [diff] [blame] | 871 | tfeedback_candidates(tfeedback_candidates), |
| 872 | toplevel_var(NULL), |
| 873 | varying_floats(0) |
Paul Berry | 99b7833 | 2013-01-29 07:34:52 -0800 | [diff] [blame] | 874 | { |
| 875 | } |
| 876 | |
| 877 | void process(ir_variable *var) |
| 878 | { |
| 879 | this->toplevel_var = var; |
| 880 | this->varying_floats = 0; |
| 881 | if (var->is_interface_instance()) |
| 882 | program_resource_visitor::process(var->interface_type, |
| 883 | var->interface_type->name); |
| 884 | else |
| 885 | program_resource_visitor::process(var); |
| 886 | } |
| 887 | |
| 888 | private: |
| 889 | virtual void visit_field(const glsl_type *type, const char *name, |
| 890 | bool row_major) |
| 891 | { |
| 892 | assert(!type->is_record()); |
| 893 | assert(!(type->is_array() && type->fields.array->is_record())); |
| 894 | assert(!type->is_interface()); |
| 895 | assert(!(type->is_array() && type->fields.array->is_interface())); |
| 896 | |
| 897 | (void) row_major; |
| 898 | |
| 899 | tfeedback_candidate *candidate |
| 900 | = rzalloc(this->mem_ctx, tfeedback_candidate); |
| 901 | candidate->toplevel_var = this->toplevel_var; |
| 902 | candidate->type = type; |
| 903 | candidate->offset = this->varying_floats; |
| 904 | hash_table_insert(this->tfeedback_candidates, candidate, |
| 905 | ralloc_strdup(this->mem_ctx, name)); |
| 906 | this->varying_floats += type->component_slots(); |
| 907 | } |
| 908 | |
| 909 | /** |
| 910 | * Memory context used to allocate hash table keys and values. |
| 911 | */ |
| 912 | void * const mem_ctx; |
| 913 | |
| 914 | /** |
| 915 | * Hash table in which tfeedback_candidate objects should be stored. |
| 916 | */ |
| 917 | hash_table * const tfeedback_candidates; |
| 918 | |
| 919 | /** |
| 920 | * Pointer to the toplevel variable that is being traversed. |
| 921 | */ |
| 922 | ir_variable *toplevel_var; |
| 923 | |
| 924 | /** |
| 925 | * Total number of varying floats that have been visited so far. This is |
| 926 | * used to determine the offset to each varying within the toplevel |
| 927 | * variable. |
| 928 | */ |
| 929 | unsigned varying_floats; |
| 930 | }; |
| 931 | |
| 932 | |
| 933 | /** |
Paul Berry | 4b11b57 | 2012-12-17 14:20:35 -0800 | [diff] [blame] | 934 | * Assign locations for all variables that are produced in one pipeline stage |
| 935 | * (the "producer") and consumed in the next stage (the "consumer"). |
| 936 | * |
| 937 | * Variables produced by the producer may also be consumed by transform |
| 938 | * feedback. |
| 939 | * |
| 940 | * \param num_tfeedback_decls is the number of declarations indicating |
| 941 | * variables that may be consumed by transform feedback. |
| 942 | * |
| 943 | * \param tfeedback_decls is a pointer to an array of tfeedback_decl objects |
| 944 | * representing the result of parsing the strings passed to |
| 945 | * glTransformFeedbackVaryings(). assign_location() will be called for |
| 946 | * each of these objects that matches one of the outputs of the |
| 947 | * producer. |
| 948 | * |
| 949 | * When num_tfeedback_decls is nonzero, it is permissible for the consumer to |
| 950 | * be NULL. In this case, varying locations are assigned solely based on the |
| 951 | * requirements of transform feedback. |
| 952 | */ |
| 953 | bool |
| 954 | assign_varying_locations(struct gl_context *ctx, |
| 955 | void *mem_ctx, |
| 956 | struct gl_shader_program *prog, |
| 957 | gl_shader *producer, gl_shader *consumer, |
| 958 | unsigned num_tfeedback_decls, |
| 959 | tfeedback_decl *tfeedback_decls) |
| 960 | { |
| 961 | /* FINISHME: Set dynamically when geometry shader support is added. */ |
| 962 | const unsigned producer_base = VERT_RESULT_VAR0; |
| 963 | const unsigned consumer_base = FRAG_ATTRIB_VAR0; |
| 964 | varying_matches matches(ctx->Const.DisableVaryingPacking); |
Paul Berry | 99b7833 | 2013-01-29 07:34:52 -0800 | [diff] [blame] | 965 | hash_table *tfeedback_candidates |
| 966 | = hash_table_ctor(0, hash_table_string_hash, hash_table_string_compare); |
Jordan Justen | 0486d50 | 2013-02-12 16:12:31 -0800 | [diff] [blame^] | 967 | hash_table *consumer_inputs |
| 968 | = hash_table_ctor(0, hash_table_string_hash, hash_table_string_compare); |
Paul Berry | 4b11b57 | 2012-12-17 14:20:35 -0800 | [diff] [blame] | 969 | |
| 970 | /* Operate in a total of three passes. |
| 971 | * |
| 972 | * 1. Assign locations for any matching inputs and outputs. |
| 973 | * |
| 974 | * 2. Mark output variables in the producer that do not have locations as |
| 975 | * not being outputs. This lets the optimizer eliminate them. |
| 976 | * |
| 977 | * 3. Mark input variables in the consumer that do not have locations as |
| 978 | * not being inputs. This lets the optimizer eliminate them. |
| 979 | */ |
| 980 | |
Jordan Justen | 0486d50 | 2013-02-12 16:12:31 -0800 | [diff] [blame^] | 981 | if (consumer) { |
| 982 | foreach_list(node, consumer->ir) { |
| 983 | ir_variable *const input_var = |
| 984 | ((ir_instruction *) node)->as_variable(); |
| 985 | |
| 986 | if ((input_var != NULL) && (input_var->mode == ir_var_shader_in)) { |
| 987 | hash_table_insert(consumer_inputs, input_var, |
| 988 | ralloc_strdup(mem_ctx, input_var->name)); |
| 989 | } |
| 990 | } |
| 991 | } |
| 992 | |
Paul Berry | 4b11b57 | 2012-12-17 14:20:35 -0800 | [diff] [blame] | 993 | foreach_list(node, producer->ir) { |
| 994 | ir_variable *const output_var = ((ir_instruction *) node)->as_variable(); |
| 995 | |
Paul Berry | 42a29d8 | 2013-01-11 14:39:32 -0800 | [diff] [blame] | 996 | if ((output_var == NULL) || (output_var->mode != ir_var_shader_out)) |
Paul Berry | 4b11b57 | 2012-12-17 14:20:35 -0800 | [diff] [blame] | 997 | continue; |
| 998 | |
Paul Berry | 99b7833 | 2013-01-29 07:34:52 -0800 | [diff] [blame] | 999 | tfeedback_candidate_generator g(mem_ctx, tfeedback_candidates); |
| 1000 | g.process(output_var); |
| 1001 | |
Paul Berry | 4b11b57 | 2012-12-17 14:20:35 -0800 | [diff] [blame] | 1002 | ir_variable *input_var = |
Jordan Justen | 0486d50 | 2013-02-12 16:12:31 -0800 | [diff] [blame^] | 1003 | (ir_variable *) hash_table_find(consumer_inputs, output_var->name); |
Paul Berry | 4b11b57 | 2012-12-17 14:20:35 -0800 | [diff] [blame] | 1004 | |
Paul Berry | 42a29d8 | 2013-01-11 14:39:32 -0800 | [diff] [blame] | 1005 | if (input_var && input_var->mode != ir_var_shader_in) |
Paul Berry | 4b11b57 | 2012-12-17 14:20:35 -0800 | [diff] [blame] | 1006 | input_var = NULL; |
| 1007 | |
| 1008 | if (input_var) { |
| 1009 | matches.record(output_var, input_var); |
| 1010 | } |
| 1011 | } |
| 1012 | |
| 1013 | for (unsigned i = 0; i < num_tfeedback_decls; ++i) { |
| 1014 | if (!tfeedback_decls[i].is_varying()) |
| 1015 | continue; |
| 1016 | |
Paul Berry | 99b7833 | 2013-01-29 07:34:52 -0800 | [diff] [blame] | 1017 | const tfeedback_candidate *matched_candidate |
| 1018 | = tfeedback_decls[i].find_candidate(prog, tfeedback_candidates); |
Paul Berry | 4b11b57 | 2012-12-17 14:20:35 -0800 | [diff] [blame] | 1019 | |
Paul Berry | 99b7833 | 2013-01-29 07:34:52 -0800 | [diff] [blame] | 1020 | if (matched_candidate == NULL) { |
| 1021 | hash_table_dtor(tfeedback_candidates); |
Jordan Justen | 0486d50 | 2013-02-12 16:12:31 -0800 | [diff] [blame^] | 1022 | hash_table_dtor(consumer_inputs); |
Paul Berry | 4b11b57 | 2012-12-17 14:20:35 -0800 | [diff] [blame] | 1023 | return false; |
Paul Berry | 4b11b57 | 2012-12-17 14:20:35 -0800 | [diff] [blame] | 1024 | } |
Paul Berry | 99b7833 | 2013-01-29 07:34:52 -0800 | [diff] [blame] | 1025 | |
| 1026 | if (matched_candidate->toplevel_var->is_unmatched_generic_inout) |
| 1027 | matches.record(matched_candidate->toplevel_var, NULL); |
Paul Berry | 4b11b57 | 2012-12-17 14:20:35 -0800 | [diff] [blame] | 1028 | } |
| 1029 | |
| 1030 | const unsigned slots_used = matches.assign_locations(); |
| 1031 | matches.store_locations(producer_base, consumer_base); |
| 1032 | |
| 1033 | for (unsigned i = 0; i < num_tfeedback_decls; ++i) { |
| 1034 | if (!tfeedback_decls[i].is_varying()) |
| 1035 | continue; |
| 1036 | |
Paul Berry | 99b7833 | 2013-01-29 07:34:52 -0800 | [diff] [blame] | 1037 | if (!tfeedback_decls[i].assign_location(ctx, prog)) { |
| 1038 | hash_table_dtor(tfeedback_candidates); |
Jordan Justen | 0486d50 | 2013-02-12 16:12:31 -0800 | [diff] [blame^] | 1039 | hash_table_dtor(consumer_inputs); |
Paul Berry | 4b11b57 | 2012-12-17 14:20:35 -0800 | [diff] [blame] | 1040 | return false; |
Paul Berry | 99b7833 | 2013-01-29 07:34:52 -0800 | [diff] [blame] | 1041 | } |
Paul Berry | 4b11b57 | 2012-12-17 14:20:35 -0800 | [diff] [blame] | 1042 | } |
| 1043 | |
Paul Berry | 99b7833 | 2013-01-29 07:34:52 -0800 | [diff] [blame] | 1044 | hash_table_dtor(tfeedback_candidates); |
Jordan Justen | 0486d50 | 2013-02-12 16:12:31 -0800 | [diff] [blame^] | 1045 | hash_table_dtor(consumer_inputs); |
Paul Berry | 99b7833 | 2013-01-29 07:34:52 -0800 | [diff] [blame] | 1046 | |
Paul Berry | 4b11b57 | 2012-12-17 14:20:35 -0800 | [diff] [blame] | 1047 | if (ctx->Const.DisableVaryingPacking) { |
| 1048 | /* Transform feedback code assumes varyings are packed, so if the driver |
| 1049 | * has disabled varying packing, make sure it does not support transform |
| 1050 | * feedback. |
| 1051 | */ |
| 1052 | assert(!ctx->Extensions.EXT_transform_feedback); |
| 1053 | } else { |
Paul Berry | 42a29d8 | 2013-01-11 14:39:32 -0800 | [diff] [blame] | 1054 | lower_packed_varyings(mem_ctx, producer_base, slots_used, |
| 1055 | ir_var_shader_out, producer); |
Paul Berry | 4b11b57 | 2012-12-17 14:20:35 -0800 | [diff] [blame] | 1056 | if (consumer) { |
Paul Berry | 42a29d8 | 2013-01-11 14:39:32 -0800 | [diff] [blame] | 1057 | lower_packed_varyings(mem_ctx, consumer_base, slots_used, |
| 1058 | ir_var_shader_in, consumer); |
Paul Berry | 4b11b57 | 2012-12-17 14:20:35 -0800 | [diff] [blame] | 1059 | } |
| 1060 | } |
| 1061 | |
| 1062 | unsigned varying_vectors = 0; |
| 1063 | |
| 1064 | if (consumer) { |
| 1065 | foreach_list(node, consumer->ir) { |
| 1066 | ir_variable *const var = ((ir_instruction *) node)->as_variable(); |
| 1067 | |
Paul Berry | 42a29d8 | 2013-01-11 14:39:32 -0800 | [diff] [blame] | 1068 | if ((var == NULL) || (var->mode != ir_var_shader_in)) |
Paul Berry | 4b11b57 | 2012-12-17 14:20:35 -0800 | [diff] [blame] | 1069 | continue; |
| 1070 | |
| 1071 | if (var->is_unmatched_generic_inout) { |
| 1072 | if (prog->Version <= 120) { |
| 1073 | /* On page 25 (page 31 of the PDF) of the GLSL 1.20 spec: |
| 1074 | * |
| 1075 | * Only those varying variables used (i.e. read) in |
| 1076 | * the fragment shader executable must be written to |
| 1077 | * by the vertex shader executable; declaring |
| 1078 | * superfluous varying variables in a vertex shader is |
| 1079 | * permissible. |
| 1080 | * |
| 1081 | * We interpret this text as meaning that the VS must |
| 1082 | * write the variable for the FS to read it. See |
| 1083 | * "glsl1-varying read but not written" in piglit. |
| 1084 | */ |
| 1085 | |
| 1086 | linker_error(prog, "fragment shader varying %s not written " |
| 1087 | "by vertex shader\n.", var->name); |
| 1088 | } |
| 1089 | |
| 1090 | /* An 'in' variable is only really a shader input if its |
| 1091 | * value is written by the previous stage. |
| 1092 | */ |
| 1093 | var->mode = ir_var_auto; |
| 1094 | } else if (is_varying_var(consumer->Type, var)) { |
| 1095 | /* The packing rules are used for vertex shader inputs are also |
| 1096 | * used for fragment shader inputs. |
| 1097 | */ |
| 1098 | varying_vectors += count_attribute_slots(var->type); |
| 1099 | } |
| 1100 | } |
| 1101 | } |
| 1102 | |
| 1103 | if (ctx->API == API_OPENGLES2 || prog->IsES) { |
| 1104 | if (varying_vectors > ctx->Const.MaxVarying) { |
| 1105 | if (ctx->Const.GLSLSkipStrictMaxVaryingLimitCheck) { |
| 1106 | linker_warning(prog, "shader uses too many varying vectors " |
| 1107 | "(%u > %u), but the driver will try to optimize " |
| 1108 | "them out; this is non-portable out-of-spec " |
| 1109 | "behavior\n", |
| 1110 | varying_vectors, ctx->Const.MaxVarying); |
| 1111 | } else { |
| 1112 | linker_error(prog, "shader uses too many varying vectors " |
| 1113 | "(%u > %u)\n", |
| 1114 | varying_vectors, ctx->Const.MaxVarying); |
| 1115 | return false; |
| 1116 | } |
| 1117 | } |
| 1118 | } else { |
| 1119 | const unsigned float_components = varying_vectors * 4; |
| 1120 | if (float_components > ctx->Const.MaxVarying * 4) { |
| 1121 | if (ctx->Const.GLSLSkipStrictMaxVaryingLimitCheck) { |
| 1122 | linker_warning(prog, "shader uses too many varying components " |
| 1123 | "(%u > %u), but the driver will try to optimize " |
| 1124 | "them out; this is non-portable out-of-spec " |
| 1125 | "behavior\n", |
| 1126 | float_components, ctx->Const.MaxVarying * 4); |
| 1127 | } else { |
| 1128 | linker_error(prog, "shader uses too many varying components " |
| 1129 | "(%u > %u)\n", |
| 1130 | float_components, ctx->Const.MaxVarying * 4); |
| 1131 | return false; |
| 1132 | } |
| 1133 | } |
| 1134 | } |
| 1135 | |
| 1136 | return true; |
| 1137 | } |