| 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 <[email protected]> |
| 65 |
*/ |
| 66 |
|
| 67 |
#include <ctype.h> |
| 68 |
#include "util/strndup.h" |
| 69 |
#include "glsl_symbol_table.h" |
| 70 |
#include "glsl_parser_extras.h" |
| 71 |
#include "ir.h" |
| 72 |
#include "program.h" |
| 73 |
#include "program/prog_instruction.h" |
| 74 |
#include "program/program.h" |
| 75 |
#include "util/mesa-sha1.h" |
| 76 |
#include "util/set.h" |
| 77 |
#include "string_to_uint_map.h" |
| 78 |
#include "linker.h" |
| 79 |
#include "linker_util.h" |
| 80 |
#include "link_varyings.h" |
| 81 |
#include "ir_optimization.h" |
| 82 |
#include "ir_rvalue_visitor.h" |
| 83 |
#include "ir_uniform.h" |
| 84 |
#include "builtin_functions.h" |
| 85 |
#include "shader_cache.h" |
| 86 |
#include "util/u_string.h" |
| 87 |
#include "util/u_math.h" |
| 88 |
|
| 89 |
|
| 90 |
#include "main/shaderobj.h" |
| 91 |
#include "main/enums.h" |
| 92 |
#include "main/mtypes.h" |
| 93 |
|
| 94 |
|
| 95 |
namespace { |
| 96 |
|
| 97 |
struct find_variable { |
| 98 |
const char *name; |
| 99 |
bool found; |
| 100 |
|
| 101 |
find_variable(const char *name) : name(name), found(false) {} |
| 102 |
}; |
| 103 |
|
| 104 |
/** |
| 105 |
* Visitor that determines whether or not a variable is ever written. |
| 106 |
* |
| 107 |
* Use \ref find_assignments for convenience. |
| 108 |
*/ |
| 109 |
class find_assignment_visitor : public ir_hierarchical_visitor { |
| 110 |
public: |
| 111 |
find_assignment_visitor(unsigned num_vars, |
| 112 |
find_variable * const *vars) |
| 113 |
: num_variables(num_vars), num_found(0), variables(vars) |
| 114 |
{ |
| 115 |
} |
| 116 |
|
| 117 |
virtual ir_visitor_status visit_enter(ir_assignment *ir) |
| 118 |
{ |
| 119 |
ir_variable *const var = ir->lhs->variable_referenced(); |
| 120 |
|
| 121 |
return check_variable_name(var->name); |
| 122 |
} |
| 123 |
|
| 124 |
virtual ir_visitor_status visit_enter(ir_call *ir) |
| 125 |
{ |
| 126 |
foreach_two_lists(formal_node, &ir->callee->parameters, |
| 127 |
actual_node, &ir->actual_parameters) { |
| 128 |
ir_rvalue *param_rval = (ir_rvalue *) actual_node; |
| 129 |
ir_variable *sig_param = (ir_variable *) formal_node; |
| 130 |
|
| 131 |
if (sig_param->data.mode == ir_var_function_out || |
| 132 |
sig_param->data.mode == ir_var_function_inout) { |
| 133 |
ir_variable *var = param_rval->variable_referenced(); |
| 134 |
if (var && check_variable_name(var->name) == visit_stop) |
| 135 |
return visit_stop; |
| 136 |
} |
| 137 |
} |
| 138 |
|
| 139 |
if (ir->return_deref != NULL) { |
| 140 |
ir_variable *const var = ir->return_deref->variable_referenced(); |
| 141 |
|
| 142 |
if (check_variable_name(var->name) == visit_stop) |
| 143 |
return visit_stop; |
| 144 |
} |
| 145 |
|
| 146 |
return visit_continue_with_parent; |
| 147 |
} |
| 148 |
|
| 149 |
private: |
| 150 |
ir_visitor_status check_variable_name(const char *name) |
| 151 |
{ |
| 152 |
for (unsigned i = 0; i < num_variables; ++i) { |
| 153 |
if (strcmp(variables[i]->name, name) == 0) { |
| 154 |
if (!variables[i]->found) { |
| 155 |
variables[i]->found = true; |
| 156 |
|
| 157 |
assert(num_found < num_variables); |
| 158 |
if (++num_found == num_variables) |
| 159 |
return visit_stop; |
| 160 |
} |
| 161 |
break; |
| 162 |
} |
| 163 |
} |
| 164 |
|
| 165 |
return visit_continue_with_parent; |
| 166 |
} |
| 167 |
|
| 168 |
private: |
| 169 |
unsigned num_variables; /**< Number of variables to find */ |
| 170 |
unsigned num_found; /**< Number of variables already found */ |
| 171 |
find_variable * const *variables; /**< Variables to find */ |
| 172 |
}; |
| 173 |
|
| 174 |
/** |
| 175 |
* Determine whether or not any of NULL-terminated list of variables is ever |
| 176 |
* written to. |
| 177 |
*/ |
| 178 |
static void |
| 179 |
find_assignments(exec_list *ir, find_variable * const *vars) |
| 180 |
{ |
| 181 |
unsigned num_variables = 0; |
| 182 |
|
| 183 |
for (find_variable * const *v = vars; *v; ++v) |
| 184 |
num_variables++; |
| 185 |
|
| 186 |
find_assignment_visitor visitor(num_variables, vars); |
| 187 |
visitor.run(ir); |
| 188 |
} |
| 189 |
|
| 190 |
/** |
| 191 |
* Determine whether or not the given variable is ever written to. |
| 192 |
*/ |
| 193 |
static void |
| 194 |
find_assignments(exec_list *ir, find_variable *var) |
| 195 |
{ |
| 196 |
find_assignment_visitor visitor(1, &var); |
| 197 |
visitor.run(ir); |
| 198 |
} |
| 199 |
|
| 200 |
/** |
| 201 |
* Visitor that determines whether or not a variable is ever read. |
| 202 |
*/ |
| 203 |
class find_deref_visitor : public ir_hierarchical_visitor { |
| 204 |
public: |
| 205 |
find_deref_visitor(const char *name) |
| 206 |
: name(name), found(false) |
| 207 |
{ |
| 208 |
/* empty */ |
| 209 |
} |
| 210 |
|
| 211 |
virtual ir_visitor_status visit(ir_dereference_variable *ir) |
| 212 |
{ |
| 213 |
if (strcmp(this->name, ir->var->name) == 0) { |
| 214 |
this->found = true; |
| 215 |
return visit_stop; |
| 216 |
} |
| 217 |
|
| 218 |
return visit_continue; |
| 219 |
} |
| 220 |
|
| 221 |
bool variable_found() const |
| 222 |
{ |
| 223 |
return this->found; |
| 224 |
} |
| 225 |
|
| 226 |
private: |
| 227 |
const char *name; /**< Find writes to a variable with this name. */ |
| 228 |
bool found; /**< Was a write to the variable found? */ |
| 229 |
}; |
| 230 |
|
| 231 |
|
| 232 |
/** |
| 233 |
* A visitor helper that provides methods for updating the types of |
| 234 |
* ir_dereferences. Classes that update variable types (say, updating |
| 235 |
* array sizes) will want to use this so that dereference types stay in sync. |
| 236 |
*/ |
| 237 |
class deref_type_updater : public ir_hierarchical_visitor { |
| 238 |
public: |
| 239 |
virtual ir_visitor_status visit(ir_dereference_variable *ir) |
| 240 |
{ |
| 241 |
ir->type = ir->var->type; |
| 242 |
return visit_continue; |
| 243 |
} |
| 244 |
|
| 245 |
virtual ir_visitor_status visit_leave(ir_dereference_array *ir) |
| 246 |
{ |
| 247 |
const glsl_type *const vt = ir->array->type; |
| 248 |
if (vt->is_array()) |
| 249 |
ir->type = vt->fields.array; |
| 250 |
return visit_continue; |
| 251 |
} |
| 252 |
|
| 253 |
virtual ir_visitor_status visit_leave(ir_dereference_record *ir) |
| 254 |
{ |
| 255 |
ir->type = ir->record->type->fields.structure[ir->field_idx].type; |
| 256 |
return visit_continue; |
| 257 |
} |
| 258 |
}; |
| 259 |
|
| 260 |
|
| 261 |
class array_resize_visitor : public deref_type_updater { |
| 262 |
public: |
| 263 |
using deref_type_updater::visit; |
| 264 |
|
| 265 |
unsigned num_vertices; |
| 266 |
gl_shader_program *prog; |
| 267 |
gl_shader_stage stage; |
| 268 |
|
| 269 |
array_resize_visitor(unsigned num_vertices, |
| 270 |
gl_shader_program *prog, |
| 271 |
gl_shader_stage stage) |
| 272 |
{ |
| 273 |
this->num_vertices = num_vertices; |
| 274 |
this->prog = prog; |
| 275 |
this->stage = stage; |
| 276 |
} |
| 277 |
|
| 278 |
virtual ~array_resize_visitor() |
| 279 |
{ |
| 280 |
/* empty */ |
| 281 |
} |
| 282 |
|
| 283 |
virtual ir_visitor_status visit(ir_variable *var) |
| 284 |
{ |
| 285 |
if (!var->type->is_array() || var->data.mode != ir_var_shader_in || |
| 286 |
var->data.patch) |
| 287 |
return visit_continue; |
| 288 |
|
| 289 |
unsigned size = var->type->length; |
| 290 |
|
| 291 |
if (stage == MESA_SHADER_GEOMETRY) { |
| 292 |
/* Generate a link error if the shader has declared this array with |
| 293 |
* an incorrect size. |
| 294 |
*/ |
| 295 |
if (!var->data.implicit_sized_array && |
| 296 |
size && size != this->num_vertices) { |
| 297 |
linker_error(this->prog, "size of array %s declared as %u, " |
| 298 |
"but number of input vertices is %u\n", |
| 299 |
var->name, size, this->num_vertices); |
| 300 |
return visit_continue; |
| 301 |
} |
| 302 |
|
| 303 |
/* Generate a link error if the shader attempts to access an input |
| 304 |
* array using an index too large for its actual size assigned at |
| 305 |
* link time. |
| 306 |
*/ |
| 307 |
if (var->data.max_array_access >= (int)this->num_vertices) { |
| 308 |
linker_error(this->prog, "%s shader accesses element %i of " |
| 309 |
"%s, but only %i input vertices\n", |
| 310 |
_mesa_shader_stage_to_string(this->stage), |
| 311 |
var->data.max_array_access, var->name, this->num_vertices); |
| 312 |
return visit_continue; |
| 313 |
} |
| 314 |
} |
| 315 |
|
| 316 |
var->type = glsl_type::get_array_instance(var->type->fields.array, |
| 317 |
this->num_vertices); |
| 318 |
var->data.max_array_access = this->num_vertices - 1; |
| 319 |
|
| 320 |
return visit_continue; |
| 321 |
} |
| 322 |
}; |
| 323 |
|
| 324 |
/** |
| 325 |
* Visitor that determines the highest stream id to which a (geometry) shader |
| 326 |
* emits vertices. It also checks whether End{Stream}Primitive is ever called. |
| 327 |
*/ |
| 328 |
class find_emit_vertex_visitor : public ir_hierarchical_visitor { |
| 329 |
public: |
| 330 |
find_emit_vertex_visitor(int max_allowed) |
| 331 |
: max_stream_allowed(max_allowed), |
| 332 |
invalid_stream_id(0), |
| 333 |
invalid_stream_id_from_emit_vertex(false), |
| 334 |
end_primitive_found(false), |
| 335 |
uses_non_zero_stream(false) |
| 336 |
{ |
| 337 |
/* empty */ |
| 338 |
} |
| 339 |
|
| 340 |
virtual ir_visitor_status visit_leave(ir_emit_vertex *ir) |
| 341 |
{ |
| 342 |
int stream_id = ir->stream_id(); |
| 343 |
|
| 344 |
if (stream_id < 0) { |
| 345 |
invalid_stream_id = stream_id; |
| 346 |
invalid_stream_id_from_emit_vertex = true; |
| 347 |
return visit_stop; |
| 348 |
} |
| 349 |
|
| 350 |
if (stream_id > max_stream_allowed) { |
| 351 |
invalid_stream_id = stream_id; |
| 352 |
invalid_stream_id_from_emit_vertex = true; |
| 353 |
return visit_stop; |
| 354 |
} |
| 355 |
|
| 356 |
if (stream_id != 0) |
| 357 |
uses_non_zero_stream = true; |
| 358 |
|
| 359 |
return visit_continue; |
| 360 |
} |
| 361 |
|
| 362 |
virtual ir_visitor_status visit_leave(ir_end_primitive *ir) |
| 363 |
{ |
| 364 |
end_primitive_found = true; |
| 365 |
|
| 366 |
int stream_id = ir->stream_id(); |
| 367 |
|
| 368 |
if (stream_id < 0) { |
| 369 |
invalid_stream_id = stream_id; |
| 370 |
invalid_stream_id_from_emit_vertex = false; |
| 371 |
return visit_stop; |
| 372 |
} |
| 373 |
|
| 374 |
if (stream_id > max_stream_allowed) { |
| 375 |
invalid_stream_id = stream_id; |
| 376 |
invalid_stream_id_from_emit_vertex = false; |
| 377 |
return visit_stop; |
| 378 |
} |
| 379 |
|
| 380 |
if (stream_id != 0) |
| 381 |
uses_non_zero_stream = true; |
| 382 |
|
| 383 |
return visit_continue; |
| 384 |
} |
| 385 |
|
| 386 |
bool error() |
| 387 |
{ |
| 388 |
return invalid_stream_id != 0; |
| 389 |
} |
| 390 |
|
| 391 |
const char *error_func() |
| 392 |
{ |
| 393 |
return invalid_stream_id_from_emit_vertex ? |
| 394 |
"EmitStreamVertex" : "EndStreamPrimitive"; |
| 395 |
} |
| 396 |
|
| 397 |
int error_stream() |
| 398 |
{ |
| 399 |
return invalid_stream_id; |
| 400 |
} |
| 401 |
|
| 402 |
bool uses_streams() |
| 403 |
{ |
| 404 |
return uses_non_zero_stream; |
| 405 |
} |
| 406 |
|
| 407 |
bool uses_end_primitive() |
| 408 |
{ |
| 409 |
return end_primitive_found; |
| 410 |
} |
| 411 |
|
| 412 |
private: |
| 413 |
int max_stream_allowed; |
| 414 |
int invalid_stream_id; |
| 415 |
bool invalid_stream_id_from_emit_vertex; |
| 416 |
bool end_primitive_found; |
| 417 |
bool uses_non_zero_stream; |
| 418 |
}; |
| 419 |
|
| 420 |
/* Class that finds array derefs and check if indexes are dynamic. */ |
| 421 |
class dynamic_sampler_array_indexing_visitor : public ir_hierarchical_visitor |
| 422 |
{ |
| 423 |
public: |
| 424 |
dynamic_sampler_array_indexing_visitor() : |
| 425 |
dynamic_sampler_array_indexing(false) |
| 426 |
{ |
| 427 |
} |
| 428 |
|
| 429 |
ir_visitor_status visit_enter(ir_dereference_array *ir) |
| 430 |
{ |
| 431 |
if (!ir->variable_referenced()) |
| 432 |
return visit_continue; |
| 433 |
|
| 434 |
if (!ir->variable_referenced()->type->contains_sampler()) |
| 435 |
return visit_continue; |
| 436 |
|
| 437 |
if (!ir->array_index->constant_expression_value(ralloc_parent(ir))) { |
| 438 |
dynamic_sampler_array_indexing = true; |
| 439 |
return visit_stop; |
| 440 |
} |
| 441 |
return visit_continue; |
| 442 |
} |
| 443 |
|
| 444 |
bool uses_dynamic_sampler_array_indexing() |
| 445 |
{ |
| 446 |
return dynamic_sampler_array_indexing; |
| 447 |
} |
| 448 |
|
| 449 |
private: |
| 450 |
bool dynamic_sampler_array_indexing; |
| 451 |
}; |
| 452 |
|
| 453 |
} /* anonymous namespace */ |
| 454 |
|
| 455 |
void |
| 456 |
linker_error(gl_shader_program *prog, const char *fmt, ...) |
| 457 |
{ |
| 458 |
va_list ap; |
| 459 |
|
| 460 |
ralloc_strcat(&prog->data->InfoLog, "error: "); |
| 461 |
va_start(ap, fmt); |
| 462 |
ralloc_vasprintf_append(&prog->data->InfoLog, fmt, ap); |
| 463 |
va_end(ap); |
| 464 |
|
| 465 |
prog->data->LinkStatus = LINKING_FAILURE; |
| 466 |
} |
| 467 |
|
| 468 |
|
| 469 |
void |
| 470 |
linker_warning(gl_shader_program *prog, const char *fmt, ...) |
| 471 |
{ |
| 472 |
va_list ap; |
| 473 |
|
| 474 |
ralloc_strcat(&prog->data->InfoLog, "warning: "); |
| 475 |
va_start(ap, fmt); |
| 476 |
ralloc_vasprintf_append(&prog->data->InfoLog, fmt, ap); |
| 477 |
va_end(ap); |
| 478 |
|
| 479 |
} |
| 480 |
|
| 481 |
|
| 482 |
/** |
| 483 |
* Given a string identifying a program resource, break it into a base name |
| 484 |
* and an optional array index in square brackets. |
| 485 |
* |
| 486 |
* If an array index is present, \c out_base_name_end is set to point to the |
| 487 |
* "[" that precedes the array index, and the array index itself is returned |
| 488 |
* as a long. |
| 489 |
* |
| 490 |
* If no array index is present (or if the array index is negative or |
| 491 |
* mal-formed), \c out_base_name_end, is set to point to the null terminator |
| 492 |
* at the end of the input string, and -1 is returned. |
| 493 |
* |
| 494 |
* Only the final array index is parsed; if the string contains other array |
| 495 |
* indices (or structure field accesses), they are left in the base name. |
| 496 |
* |
| 497 |
* No attempt is made to check that the base name is properly formed; |
| 498 |
* typically the caller will look up the base name in a hash table, so |
| 499 |
* ill-formed base names simply turn into hash table lookup failures. |
| 500 |
*/ |
| 501 |
long |
| 502 |
parse_program_resource_name(const GLchar *name, |
| 503 |
const GLchar **out_base_name_end) |
| 504 |
{ |
| 505 |
/* Section 7.3.1 ("Program Interfaces") of the OpenGL 4.3 spec says: |
| 506 |
* |
| 507 |
* "When an integer array element or block instance number is part of |
| 508 |
* the name string, it will be specified in decimal form without a "+" |
| 509 |
* or "-" sign or any extra leading zeroes. Additionally, the name |
| 510 |
* string will not include white space anywhere in the string." |
| 511 |
*/ |
| 512 |
|
| 513 |
const size_t len = strlen(name); |
| 514 |
*out_base_name_end = name + len; |
| 515 |
|
| 516 |
if (len == 0 || name[len-1] != ']') |
| 517 |
return -1; |
| 518 |
|
| 519 |
/* Walk backwards over the string looking for a non-digit character. This |
| 520 |
* had better be the opening bracket for an array index. |
| 521 |
* |
| 522 |
* Initially, i specifies the location of the ']'. Since the string may |
| 523 |
* contain only the ']' charcater, walk backwards very carefully. |
| 524 |
*/ |
| 525 |
unsigned i; |
| 526 |
for (i = len - 1; (i > 0) && isdigit(name[i-1]); --i) |
| 527 |
/* empty */ ; |
| 528 |
|
| 529 |
if ((i == 0) || name[i-1] != '[') |
| 530 |
return -1; |
| 531 |
|
| 532 |
long array_index = strtol(&name[i], NULL, 10); |
| 533 |
if (array_index < 0) |
| 534 |
return -1; |
| 535 |
|
| 536 |
/* Check for leading zero */ |
| 537 |
if (name[i] == '0' && name[i+1] != ']') |
| 538 |
return -1; |
| 539 |
|
| 540 |
*out_base_name_end = name + (i - 1); |
| 541 |
return array_index; |
| 542 |
} |
| 543 |
|
| 544 |
|
| 545 |
void |
| 546 |
link_invalidate_variable_locations(exec_list *ir) |
| 547 |
{ |
| 548 |
foreach_in_list(ir_instruction, node, ir) { |
| 549 |
ir_variable *const var = node->as_variable(); |
| 550 |
|
| 551 |
if (var == NULL) |
| 552 |
continue; |
| 553 |
|
| 554 |
/* Only assign locations for variables that lack an explicit location. |
| 555 |
* Explicit locations are set for all built-in variables, generic vertex |
| 556 |
* shader inputs (via layout(location=...)), and generic fragment shader |
| 557 |
* outputs (also via layout(location=...)). |
| 558 |
*/ |
| 559 |
if (!var->data.explicit_location) { |
| 560 |
var->data.location = -1; |
| 561 |
var->data.location_frac = 0; |
| 562 |
} |
| 563 |
|
| 564 |
/* ir_variable::is_unmatched_generic_inout is used by the linker while |
| 565 |
* connecting outputs from one stage to inputs of the next stage. |
| 566 |
*/ |
| 567 |
if (var->data.explicit_location && |
| 568 |
var->data.location < VARYING_SLOT_VAR0) { |
| 569 |
var->data.is_unmatched_generic_inout = 0; |
| 570 |
} else { |
| 571 |
var->data.is_unmatched_generic_inout = 1; |
| 572 |
} |
| 573 |
} |
| 574 |
} |
| 575 |
|
| 576 |
|
| 577 |
/** |
| 578 |
* Set clip_distance_array_size based and cull_distance_array_size on the given |
| 579 |
* shader. |
| 580 |
* |
| 581 |
* Also check for errors based on incorrect usage of gl_ClipVertex and |
| 582 |
* gl_ClipDistance and gl_CullDistance. |
| 583 |
* Additionally test whether the arrays gl_ClipDistance and gl_CullDistance |
| 584 |
* exceed the maximum size defined by gl_MaxCombinedClipAndCullDistances. |
| 585 |
* |
| 586 |
* Return false if an error was reported. |
| 587 |
*/ |
| 588 |
static void |
| 589 |
analyze_clip_cull_usage(struct gl_shader_program *prog, |
| 590 |
struct gl_linked_shader *shader, |
| 591 |
struct gl_context *ctx, |
| 592 |
struct shader_info *info) |
| 593 |
{ |
| 594 |
info->clip_distance_array_size = 0; |
| 595 |
info->cull_distance_array_size = 0; |
| 596 |
|
| 597 |
if (prog->data->Version >= (prog->IsES ? 300 : 130)) { |
| 598 |
/* From section 7.1 (Vertex Shader Special Variables) of the |
| 599 |
* GLSL 1.30 spec: |
| 600 |
* |
| 601 |
* "It is an error for a shader to statically write both |
| 602 |
* gl_ClipVertex and gl_ClipDistance." |
| 603 |
* |
| 604 |
* This does not apply to GLSL ES shaders, since GLSL ES defines neither |
| 605 |
* gl_ClipVertex nor gl_ClipDistance. However with |
| 606 |
* GL_EXT_clip_cull_distance, this functionality is exposed in ES 3.0. |
| 607 |
*/ |
| 608 |
find_variable gl_ClipDistance("gl_ClipDistance"); |
| 609 |
find_variable gl_CullDistance("gl_CullDistance"); |
| 610 |
find_variable gl_ClipVertex("gl_ClipVertex"); |
| 611 |
find_variable * const variables[] = { |
| 612 |
&gl_ClipDistance, |
| 613 |
&gl_CullDistance, |
| 614 |
!prog->IsES ? &gl_ClipVertex : NULL, |
| 615 |
NULL |
| 616 |
}; |
| 617 |
find_assignments(shader->ir, variables); |
| 618 |
|
| 619 |
/* From the ARB_cull_distance spec: |
| 620 |
* |
| 621 |
* It is a compile-time or link-time error for the set of shaders forming |
| 622 |
* a program to statically read or write both gl_ClipVertex and either |
| 623 |
* gl_ClipDistance or gl_CullDistance. |
| 624 |
* |
| 625 |
* This does not apply to GLSL ES shaders, since GLSL ES doesn't define |
| 626 |
* gl_ClipVertex. |
| 627 |
*/ |
| 628 |
if (!prog->IsES) { |
| 629 |
if (gl_ClipVertex.found && gl_ClipDistance.found) { |
| 630 |
linker_error(prog, "%s shader writes to both `gl_ClipVertex' " |
| 631 |
"and `gl_ClipDistance'\n", |
| 632 |
_mesa_shader_stage_to_string(shader->Stage)); |
| 633 |
return; |
| 634 |
} |
| 635 |
if (gl_ClipVertex.found && gl_CullDistance.found) { |
| 636 |
linker_error(prog, "%s shader writes to both `gl_ClipVertex' " |
| 637 |
"and `gl_CullDistance'\n", |
| 638 |
_mesa_shader_stage_to_string(shader->Stage)); |
| 639 |
return; |
| 640 |
} |
| 641 |
} |
| 642 |
|
| 643 |
if (gl_ClipDistance.found) { |
| 644 |
ir_variable *clip_distance_var = |
| 645 |
shader->symbols->get_variable("gl_ClipDistance"); |
| 646 |
assert(clip_distance_var); |
| 647 |
info->clip_distance_array_size = clip_distance_var->type->length; |
| 648 |
} |
| 649 |
if (gl_CullDistance.found) { |
| 650 |
ir_variable *cull_distance_var = |
| 651 |
shader->symbols->get_variable("gl_CullDistance"); |
| 652 |
assert(cull_distance_var); |
| 653 |
info->cull_distance_array_size = cull_distance_var->type->length; |
| 654 |
} |
| 655 |
/* From the ARB_cull_distance spec: |
| 656 |
* |
| 657 |
* It is a compile-time or link-time error for the set of shaders forming |
| 658 |
* a program to have the sum of the sizes of the gl_ClipDistance and |
| 659 |
* gl_CullDistance arrays to be larger than |
| 660 |
* gl_MaxCombinedClipAndCullDistances. |
| 661 |
*/ |
| 662 |
if ((uint32_t)(info->clip_distance_array_size + info->cull_distance_array_size) > |
| 663 |
ctx->Const.MaxClipPlanes) { |
| 664 |
linker_error(prog, "%s shader: the combined size of " |
| 665 |
"'gl_ClipDistance' and 'gl_CullDistance' size cannot " |
| 666 |
"be larger than " |
| 667 |
"gl_MaxCombinedClipAndCullDistances (%u)", |
| 668 |
_mesa_shader_stage_to_string(shader->Stage), |
| 669 |
ctx->Const.MaxClipPlanes); |
| 670 |
} |
| 671 |
} |
| 672 |
} |
| 673 |
|
| 674 |
|
| 675 |
/** |
| 676 |
* Verify that a vertex shader executable meets all semantic requirements. |
| 677 |
* |
| 678 |
* Also sets info.clip_distance_array_size and |
| 679 |
* info.cull_distance_array_size as a side effect. |
| 680 |
* |
| 681 |
* \param shader Vertex shader executable to be verified |
| 682 |
*/ |
| 683 |
static void |
| 684 |
validate_vertex_shader_executable(struct gl_shader_program *prog, |
| 685 |
struct gl_linked_shader *shader, |
| 686 |
struct gl_context *ctx) |
| 687 |
{ |
| 688 |
if (shader == NULL) |
| 689 |
return; |
| 690 |
|
| 691 |
/* From the GLSL 1.10 spec, page 48: |
| 692 |
* |
| 693 |
* "The variable gl_Position is available only in the vertex |
| 694 |
* language and is intended for writing the homogeneous vertex |
| 695 |
* position. All executions of a well-formed vertex shader |
| 696 |
* executable must write a value into this variable. [...] The |
| 697 |
* variable gl_Position is available only in the vertex |
| 698 |
* language and is intended for writing the homogeneous vertex |
| 699 |
* position. All executions of a well-formed vertex shader |
| 700 |
* executable must write a value into this variable." |
| 701 |
* |
| 702 |
* while in GLSL 1.40 this text is changed to: |
| 703 |
* |
| 704 |
* "The variable gl_Position is available only in the vertex |
| 705 |
* language and is intended for writing the homogeneous vertex |
| 706 |
* position. It can be written at any time during shader |
| 707 |
* execution. It may also be read back by a vertex shader |
| 708 |
* after being written. This value will be used by primitive |
| 709 |
* assembly, clipping, culling, and other fixed functionality |
| 710 |
* operations, if present, that operate on primitives after |
| 711 |
* vertex processing has occurred. Its value is undefined if |
| 712 |
* the vertex shader executable does not write gl_Position." |
| 713 |
* |
| 714 |
* All GLSL ES Versions are similar to GLSL 1.40--failing to write to |
| 715 |
* gl_Position is not an error. |
| 716 |
*/ |
| 717 |
if (prog->data->Version < (prog->IsES ? 300 : 140)) { |
| 718 |
find_variable gl_Position("gl_Position"); |
| 719 |
find_assignments(shader->ir, &gl_Position); |
| 720 |
if (!gl_Position.found) { |
| 721 |
if (prog->IsES) { |
| 722 |
linker_warning(prog, |
| 723 |
"vertex shader does not write to `gl_Position'. " |
| 724 |
"Its value is undefined. \n"); |
| 725 |
} else { |
| 726 |
linker_error(prog, |
| 727 |
"vertex shader does not write to `gl_Position'. \n"); |
| 728 |
} |
| 729 |
return; |
| 730 |
} |
| 731 |
} |
| 732 |
|
| 733 |
analyze_clip_cull_usage(prog, shader, ctx, &shader->Program->info); |
| 734 |
} |
| 735 |
|
| 736 |
static void |
| 737 |
validate_tess_eval_shader_executable(struct gl_shader_program *prog, |
| 738 |
struct gl_linked_shader *shader, |
| 739 |
struct gl_context *ctx) |
| 740 |
{ |
| 741 |
if (shader == NULL) |
| 742 |
return; |
| 743 |
|
| 744 |
analyze_clip_cull_usage(prog, shader, ctx, &shader->Program->info); |
| 745 |
} |
| 746 |
|
| 747 |
|
| 748 |
/** |
| 749 |
* Verify that a fragment shader executable meets all semantic requirements |
| 750 |
* |
| 751 |
* \param shader Fragment shader executable to be verified |
| 752 |
*/ |
| 753 |
static void |
| 754 |
validate_fragment_shader_executable(struct gl_shader_program *prog, |
| 755 |
struct gl_linked_shader *shader) |
| 756 |
{ |
| 757 |
if (shader == NULL) |
| 758 |
return; |
| 759 |
|
| 760 |
find_variable gl_FragColor("gl_FragColor"); |
| 761 |
find_variable gl_FragData("gl_FragData"); |
| 762 |
find_variable * const variables[] = { &gl_FragColor, &gl_FragData, NULL }; |
| 763 |
find_assignments(shader->ir, variables); |
| 764 |
|
| 765 |
if (gl_FragColor.found && gl_FragData.found) { |
| 766 |
linker_error(prog, "fragment shader writes to both " |
| 767 |
"`gl_FragColor' and `gl_FragData'\n"); |
| 768 |
} |
| 769 |
} |
| 770 |
|
| 771 |
/** |
| 772 |
* Verify that a geometry shader executable meets all semantic requirements |
| 773 |
* |
| 774 |
* Also sets prog->Geom.VerticesIn, and info.clip_distance_array_sizeand |
| 775 |
* info.cull_distance_array_size as a side effect. |
| 776 |
* |
| 777 |
* \param shader Geometry shader executable to be verified |
| 778 |
*/ |
| 779 |
static void |
| 780 |
validate_geometry_shader_executable(struct gl_shader_program *prog, |
| 781 |
struct gl_linked_shader *shader, |
| 782 |
struct gl_context *ctx) |
| 783 |
{ |
| 784 |
if (shader == NULL) |
| 785 |
return; |
| 786 |
|
| 787 |
unsigned num_vertices = |
| 788 |
vertices_per_prim(shader->Program->info.gs.input_primitive); |
| 789 |
prog->Geom.VerticesIn = num_vertices; |
| 790 |
|
| 791 |
analyze_clip_cull_usage(prog, shader, ctx, &shader->Program->info); |
| 792 |
} |
| 793 |
|
| 794 |
/** |
| 795 |
* Check if geometry shaders emit to non-zero streams and do corresponding |
| 796 |
* validations. |
| 797 |
*/ |
| 798 |
static void |
| 799 |
validate_geometry_shader_emissions(struct gl_context *ctx, |
| 800 |
struct gl_shader_program *prog) |
| 801 |
{ |
| 802 |
struct gl_linked_shader *sh = prog->_LinkedShaders[MESA_SHADER_GEOMETRY]; |
| 803 |
|
| 804 |
if (sh != NULL) { |
| 805 |
find_emit_vertex_visitor emit_vertex(ctx->Const.MaxVertexStreams - 1); |
| 806 |
emit_vertex.run(sh->ir); |
| 807 |
if (emit_vertex.error()) { |
| 808 |
linker_error(prog, "Invalid call %s(%d). Accepted values for the " |
| 809 |
"stream parameter are in the range [0, %d].\n", |
| 810 |
emit_vertex.error_func(), |
| 811 |
emit_vertex.error_stream(), |
| 812 |
ctx->Const.MaxVertexStreams - 1); |
| 813 |
} |
| 814 |
prog->Geom.UsesStreams = emit_vertex.uses_streams(); |
| 815 |
prog->Geom.UsesEndPrimitive = emit_vertex.uses_end_primitive(); |
| 816 |
|
| 817 |
/* From the ARB_gpu_shader5 spec: |
| 818 |
* |
| 819 |
* "Multiple vertex streams are supported only if the output primitive |
| 820 |
* type is declared to be "points". A program will fail to link if it |
| 821 |
* contains a geometry shader calling EmitStreamVertex() or |
| 822 |
* EndStreamPrimitive() if its output primitive type is not "points". |
| 823 |
* |
| 824 |
* However, in the same spec: |
| 825 |
* |
| 826 |
* "The function EmitVertex() is equivalent to calling EmitStreamVertex() |
| 827 |
* with <stream> set to zero." |
| 828 |
* |
| 829 |
* And: |
| 830 |
* |
| 831 |
* "The function EndPrimitive() is equivalent to calling |
| 832 |
* EndStreamPrimitive() with <stream> set to zero." |
| 833 |
* |
| 834 |
* Since we can call EmitVertex() and EndPrimitive() when we output |
| 835 |
* primitives other than points, calling EmitStreamVertex(0) or |
| 836 |
* EmitEndPrimitive(0) should not produce errors. This it also what Nvidia |
| 837 |
* does. Currently we only set prog->Geom.UsesStreams to TRUE when |
| 838 |
* EmitStreamVertex() or EmitEndPrimitive() are called with a non-zero |
| 839 |
* stream. |
| 840 |
*/ |
| 841 |
if (prog->Geom.UsesStreams && |
| 842 |
sh->Program->info.gs.output_primitive != GL_POINTS) { |
| 843 |
linker_error(prog, "EmitStreamVertex(n) and EndStreamPrimitive(n) " |
| 844 |
"with n>0 requires point output\n"); |
| 845 |
} |
| 846 |
} |
| 847 |
} |
| 848 |
|
| 849 |
bool |
| 850 |
validate_intrastage_arrays(struct gl_shader_program *prog, |
| 851 |
ir_variable *const var, |
| 852 |
ir_variable *const existing, |
| 853 |
bool match_precision) |
| 854 |
{ |
| 855 |
/* Consider the types to be "the same" if both types are arrays |
| 856 |
* of the same type and one of the arrays is implicitly sized. |
| 857 |
* In addition, set the type of the linked variable to the |
| 858 |
* explicitly sized array. |
| 859 |
*/ |
| 860 |
if (var->type->is_array() && existing->type->is_array()) { |
| 861 |
const glsl_type *no_array_var = var->type->fields.array; |
| 862 |
const glsl_type *no_array_existing = existing->type->fields.array; |
| 863 |
bool type_matches; |
| 864 |
|
| 865 |
type_matches = (match_precision ? |
| 866 |
no_array_var == no_array_existing : |
| 867 |
no_array_var->compare_no_precision(no_array_existing)); |
| 868 |
|
| 869 |
if (type_matches && |
| 870 |
((var->type->length == 0)|| (existing->type->length == 0))) { |
| 871 |
if (var->type->length != 0) { |
| 872 |
if ((int)var->type->length <= existing->data.max_array_access) { |
| 873 |
linker_error(prog, "%s `%s' declared as type " |
| 874 |
"`%s' but outermost dimension has an index" |
| 875 |
" of `%i'\n", |
| 876 |
mode_string(var), |
| 877 |
var->name, var->type->name, |
| 878 |
existing->data.max_array_access); |
| 879 |
} |
| 880 |
existing->type = var->type; |
| 881 |
return true; |
| 882 |
} else if (existing->type->length != 0) { |
| 883 |
if((int)existing->type->length <= var->data.max_array_access && |
| 884 |
!existing->data.from_ssbo_unsized_array) { |
| 885 |
linker_error(prog, "%s `%s' declared as type " |
| 886 |
"`%s' but outermost dimension has an index" |
| 887 |
" of `%i'\n", |
| 888 |
mode_string(var), |
| 889 |
var->name, existing->type->name, |
| 890 |
var->data.max_array_access); |
| 891 |
} |
| 892 |
return true; |
| 893 |
} |
| 894 |
} |
| 895 |
} |
| 896 |
return false; |
| 897 |
} |
| 898 |
|
| 899 |
|
| 900 |
/** |
| 901 |
* Perform validation of global variables used across multiple shaders |
| 902 |
*/ |
| 903 |
static void |
| 904 |
cross_validate_globals(struct gl_context *ctx, struct gl_shader_program *prog, |
| 905 |
struct exec_list *ir, glsl_symbol_table *variables, |
| 906 |
bool uniforms_only) |
| 907 |
{ |
| 908 |
foreach_in_list(ir_instruction, node, ir) { |
| 909 |
ir_variable *const var = node->as_variable(); |
| 910 |
|
| 911 |
if (var == NULL) |
| 912 |
continue; |
| 913 |
|
| 914 |
if (uniforms_only && (var->data.mode != ir_var_uniform && var->data.mode != ir_var_shader_storage)) |
| 915 |
continue; |
| 916 |
|
| 917 |
/* don't cross validate subroutine uniforms */ |
| 918 |
if (var->type->contains_subroutine()) |
| 919 |
continue; |
| 920 |
|
| 921 |
/* Don't cross validate interface instances. These are only relevant |
| 922 |
* inside a shader. The cross validation is done at the Interface Block |
| 923 |
* name level. |
| 924 |
*/ |
| 925 |
if (var->is_interface_instance()) |
| 926 |
continue; |
| 927 |
|
| 928 |
/* Don't cross validate temporaries that are at global scope. These |
| 929 |
* will eventually get pulled into the shaders 'main'. |
| 930 |
*/ |
| 931 |
if (var->data.mode == ir_var_temporary) |
| 932 |
continue; |
| 933 |
|
| 934 |
/* If a global with this name has already been seen, verify that the |
| 935 |
* new instance has the same type. In addition, if the globals have |
| 936 |
* initializers, the values of the initializers must be the same. |
| 937 |
*/ |
| 938 |
ir_variable *const existing = variables->get_variable(var->name); |
| 939 |
if (existing != NULL) { |
| 940 |
/* Check if types match. */ |
| 941 |
if (var->type != existing->type) { |
| 942 |
if (!validate_intrastage_arrays(prog, var, existing)) { |
| 943 |
/* If it is an unsized array in a Shader Storage Block, |
| 944 |
* two different shaders can access to different elements. |
| 945 |
* Because of that, they might be converted to different |
| 946 |
* sized arrays, then check that they are compatible but |
| 947 |
* ignore the array size. |
| 948 |
*/ |
| 949 |
if (!(var->data.mode == ir_var_shader_storage && |
| 950 |
var->data.from_ssbo_unsized_array && |
| 951 |
existing->data.mode == ir_var_shader_storage && |
| 952 |
existing->data.from_ssbo_unsized_array && |
| 953 |
var->type->gl_type == existing->type->gl_type)) { |
| 954 |
linker_error(prog, "%s `%s' declared as type " |
| 955 |
"`%s' and type `%s'\n", |
| 956 |
mode_string(var), |
| 957 |
var->name, var->type->name, |
| 958 |
existing->type->name); |
| 959 |
return; |
| 960 |
} |
| 961 |
} |
| 962 |
} |
| 963 |
|
| 964 |
if (var->data.explicit_location) { |
| 965 |
if (existing->data.explicit_location |
| 966 |
&& (var->data.location != existing->data.location)) { |
| 967 |
linker_error(prog, "explicit locations for %s " |
| 968 |
"`%s' have differing values\n", |
| 969 |
mode_string(var), var->name); |
| 970 |
return; |
| 971 |
} |
| 972 |
|
| 973 |
if (var->data.location_frac != existing->data.location_frac) { |
| 974 |
linker_error(prog, "explicit components for %s `%s' have " |
| 975 |
"differing values\n", mode_string(var), var->name); |
| 976 |
return; |
| 977 |
} |
| 978 |
|
| 979 |
existing->data.location = var->data.location; |
| 980 |
existing->data.explicit_location = true; |
| 981 |
} else { |
| 982 |
/* Check if uniform with implicit location was marked explicit |
| 983 |
* by earlier shader stage. If so, mark it explicit in this stage |
| 984 |
* too to make sure later processing does not treat it as |
| 985 |
* implicit one. |
| 986 |
*/ |
| 987 |
if (existing->data.explicit_location) { |
| 988 |
var->data.location = existing->data.location; |
| 989 |
var->data.explicit_location = true; |
| 990 |
} |
| 991 |
} |
| 992 |
|
| 993 |
/* From the GLSL 4.20 specification: |
| 994 |
* "A link error will result if two compilation units in a program |
| 995 |
* specify different integer-constant bindings for the same |
| 996 |
* opaque-uniform name. However, it is not an error to specify a |
| 997 |
* binding on some but not all declarations for the same name" |
| 998 |
*/ |
| 999 |
if (var->data.explicit_binding) { |
| 1000 |
if (existing->data.explicit_binding && |
| 1001 |
var->data.binding != existing->data.binding) { |
| 1002 |
linker_error(prog, "explicit bindings for %s " |
| 1003 |
"`%s' have differing values\n", |
| 1004 |
mode_string(var), var->name); |
| 1005 |
return; |
| 1006 |
} |
| 1007 |
|
| 1008 |
existing->data.binding = var->data.binding; |
| 1009 |
existing->data.explicit_binding = true; |
| 1010 |
} |
| 1011 |
|
| 1012 |
if (var->type->contains_atomic() && |
| 1013 |
var->data.offset != existing->data.offset) { |
| 1014 |
linker_error(prog, "offset specifications for %s " |
| 1015 |
"`%s' have differing values\n", |
| 1016 |
mode_string(var), var->name); |
| 1017 |
return; |
| 1018 |
} |
| 1019 |
|
| 1020 |
/* Validate layout qualifiers for gl_FragDepth. |
| 1021 |
* |
| 1022 |
* From the AMD/ARB_conservative_depth specs: |
| 1023 |
* |
| 1024 |
* "If gl_FragDepth is redeclared in any fragment shader in a |
| 1025 |
* program, it must be redeclared in all fragment shaders in |
| 1026 |
* that program that have static assignments to |
| 1027 |
* gl_FragDepth. All redeclarations of gl_FragDepth in all |
| 1028 |
* fragment shaders in a single program must have the same set |
| 1029 |
* of qualifiers." |
| 1030 |
*/ |
| 1031 |
if (strcmp(var->name, "gl_FragDepth") == 0) { |
| 1032 |
bool layout_declared = var->data.depth_layout != ir_depth_layout_none; |
| 1033 |
bool layout_differs = |
| 1034 |
var->data.depth_layout != existing->data.depth_layout; |
| 1035 |
|
| 1036 |
if (layout_declared && layout_differs) { |
| 1037 |
linker_error(prog, |
| 1038 |
"All redeclarations of gl_FragDepth in all " |
| 1039 |
"fragment shaders in a single program must have " |
| 1040 |
"the same set of qualifiers.\n"); |
| 1041 |
} |
| 1042 |
|
| 1043 |
if (var->data.used && layout_differs) { |
| 1044 |
linker_error(prog, |
| 1045 |
"If gl_FragDepth is redeclared with a layout " |
| 1046 |
"qualifier in any fragment shader, it must be " |
| 1047 |
"redeclared with the same layout qualifier in " |
| 1048 |
"all fragment shaders that have assignments to " |
| 1049 |
"gl_FragDepth\n"); |
| 1050 |
} |
| 1051 |
} |
| 1052 |
|
| 1053 |
/* Page 35 (page 41 of the PDF) of the GLSL 4.20 spec says: |
| 1054 |
* |
| 1055 |
* "If a shared global has multiple initializers, the |
| 1056 |
* initializers must all be constant expressions, and they |
| 1057 |
* must all have the same value. Otherwise, a link error will |
| 1058 |
* result. (A shared global having only one initializer does |
| 1059 |
* not require that initializer to be a constant expression.)" |
| 1060 |
* |
| 1061 |
* Previous to 4.20 the GLSL spec simply said that initializers |
| 1062 |
* must have the same value. In this case of non-constant |
| 1063 |
* initializers, this was impossible to determine. As a result, |
| 1064 |
* no vendor actually implemented that behavior. The 4.20 |
| 1065 |
* behavior matches the implemented behavior of at least one other |
| 1066 |
* vendor, so we'll implement that for all GLSL versions. |
| 1067 |
*/ |
| 1068 |
if (var->constant_initializer != NULL) { |
| 1069 |
if (existing->constant_initializer != NULL) { |
| 1070 |
if (!var->constant_initializer->has_value(existing->constant_initializer)) { |
| 1071 |
linker_error(prog, "initializers for %s " |
| 1072 |
"`%s' have differing values\n", |
| 1073 |
mode_string(var), var->name); |
| 1074 |
return; |
| 1075 |
} |
| 1076 |
} else { |
| 1077 |
/* If the first-seen instance of a particular uniform did |
| 1078 |
* not have an initializer but a later instance does, |
| 1079 |
* replace the former with the later. |
| 1080 |
*/ |
| 1081 |
variables->replace_variable(existing->name, var); |
| 1082 |
} |
| 1083 |
} |
| 1084 |
|
| 1085 |
if (var->data.has_initializer) { |
| 1086 |
if (existing->data.has_initializer |
| 1087 |
&& (var->constant_initializer == NULL |
| 1088 |
|| existing->constant_initializer == NULL)) { |
| 1089 |
linker_error(prog, |
| 1090 |
"shared global variable `%s' has multiple " |
| 1091 |
"non-constant initializers.\n", |
| 1092 |
var->name); |
| 1093 |
return; |
| 1094 |
} |
| 1095 |
} |
| 1096 |
|
| 1097 |
if (existing->data.explicit_invariant != var->data.explicit_invariant) { |
| 1098 |
linker_error(prog, "declarations for %s `%s' have " |
| 1099 |
"mismatching invariant qualifiers\n", |
| 1100 |
mode_string(var), var->name); |
| 1101 |
return; |
| 1102 |
} |
| 1103 |
if (existing->data.centroid != var->data.centroid) { |
| 1104 |
linker_error(prog, "declarations for %s `%s' have " |
| 1105 |
"mismatching centroid qualifiers\n", |
| 1106 |
mode_string(var), var->name); |
| 1107 |
return; |
| 1108 |
} |
| 1109 |
if (existing->data.sample != var->data.sample) { |
| 1110 |
linker_error(prog, "declarations for %s `%s` have " |
| 1111 |
"mismatching sample qualifiers\n", |
| 1112 |
mode_string(var), var->name); |
| 1113 |
return; |
| 1114 |
} |
| 1115 |
if (existing->data.image_format != var->data.image_format) { |
| 1116 |
linker_error(prog, "declarations for %s `%s` have " |
| 1117 |
"mismatching image format qualifiers\n", |
| 1118 |
mode_string(var), var->name); |
| 1119 |
return; |
| 1120 |
} |
| 1121 |
|
| 1122 |
/* Check the precision qualifier matches for uniform variables on |
| 1123 |
* GLSL ES. |
| 1124 |
*/ |
| 1125 |
if (!ctx->Const.AllowGLSLRelaxedES && |
| 1126 |
prog->IsES && !var->get_interface_type() && |
| 1127 |
existing->data.precision != var->data.precision) { |
| 1128 |
if ((existing->data.used && var->data.used) || prog->data->Version >= 300) { |
| 1129 |
linker_error(prog, "declarations for %s `%s` have " |
| 1130 |
"mismatching precision qualifiers\n", |
| 1131 |
mode_string(var), var->name); |
| 1132 |
return; |
| 1133 |
} else { |
| 1134 |
linker_warning(prog, "declarations for %s `%s` have " |
| 1135 |
"mismatching precision qualifiers\n", |
| 1136 |
mode_string(var), var->name); |
| 1137 |
} |
| 1138 |
} |
| 1139 |
|
| 1140 |
/* In OpenGL GLSL 3.20 spec, section 4.3.9: |
| 1141 |
* |
| 1142 |
* "It is a link-time error if any particular shader interface |
| 1143 |
* contains: |
| 1144 |
* |
| 1145 |
* - two different blocks, each having no instance name, and each |
| 1146 |
* having a member of the same name, or |
| 1147 |
* |
| 1148 |
* - a variable outside a block, and a block with no instance name, |
| 1149 |
* where the variable has the same name as a member in the block." |
| 1150 |
*/ |
| 1151 |
const glsl_type *var_itype = var->get_interface_type(); |
| 1152 |
const glsl_type *existing_itype = existing->get_interface_type(); |
| 1153 |
if (var_itype != existing_itype) { |
| 1154 |
if (!var_itype || !existing_itype) { |
| 1155 |
linker_error(prog, "declarations for %s `%s` are inside block " |
| 1156 |
"`%s` and outside a block", |
| 1157 |
mode_string(var), var->name, |
| 1158 |
var_itype ? var_itype->name : existing_itype->name); |
| 1159 |
return; |
| 1160 |
} else if (strcmp(var_itype->name, existing_itype->name) != 0) { |
| 1161 |
linker_error(prog, "declarations for %s `%s` are inside blocks " |
| 1162 |
"`%s` and `%s`", |
| 1163 |
mode_string(var), var->name, |
| 1164 |
existing_itype->name, |
| 1165 |
var_itype->name); |
| 1166 |
return; |
| 1167 |
} |
| 1168 |
} |
| 1169 |
} else |
| 1170 |
variables->add_variable(var); |
| 1171 |
} |
| 1172 |
} |
| 1173 |
|
| 1174 |
|
| 1175 |
/** |
| 1176 |
* Perform validation of uniforms used across multiple shader stages |
| 1177 |
*/ |
| 1178 |
static void |
| 1179 |
cross_validate_uniforms(struct gl_context *ctx, |
| 1180 |
struct gl_shader_program *prog) |
| 1181 |
{ |
| 1182 |
glsl_symbol_table variables; |
| 1183 |
for (unsigned i = 0; i < MESA_SHADER_STAGES; i++) { |
| 1184 |
if (prog->_LinkedShaders[i] == NULL) |
| 1185 |
continue; |
| 1186 |
|
| 1187 |
cross_validate_globals(ctx, prog, prog->_LinkedShaders[i]->ir, |
| 1188 |
&variables, true); |
| 1189 |
} |
| 1190 |
} |
| 1191 |
|
| 1192 |
/** |
| 1193 |
* Accumulates the array of buffer blocks and checks that all definitions of |
| 1194 |
* blocks agree on their contents. |
| 1195 |
*/ |
| 1196 |
static bool |
| 1197 |
interstage_cross_validate_uniform_blocks(struct gl_shader_program *prog, |
| 1198 |
bool validate_ssbo) |
| 1199 |
{ |
| 1200 |
int *InterfaceBlockStageIndex[MESA_SHADER_STAGES]; |
| 1201 |
struct gl_uniform_block *blks = NULL; |
| 1202 |
unsigned *num_blks = validate_ssbo ? &prog->data->NumShaderStorageBlocks : |
| 1203 |
&prog->data->NumUniformBlocks; |
| 1204 |
|
| 1205 |
unsigned max_num_buffer_blocks = 0; |
| 1206 |
for (unsigned i = 0; i < MESA_SHADER_STAGES; i++) { |
| 1207 |
if (prog->_LinkedShaders[i]) { |
| 1208 |
if (validate_ssbo) { |
| 1209 |
max_num_buffer_blocks += |
| 1210 |
prog->_LinkedShaders[i]->Program->info.num_ssbos; |
| 1211 |
} else { |
| 1212 |
max_num_buffer_blocks += |
| 1213 |
prog->_LinkedShaders[i]->Program->info.num_ubos; |
| 1214 |
} |
| 1215 |
} |
| 1216 |
} |
| 1217 |
|
| 1218 |
for (unsigned i = 0; i < MESA_SHADER_STAGES; i++) { |
| 1219 |
struct gl_linked_shader *sh = prog->_LinkedShaders[i]; |
| 1220 |
|
| 1221 |
InterfaceBlockStageIndex[i] = new int[max_num_buffer_blocks]; |
| 1222 |
for (unsigned int j = 0; j < max_num_buffer_blocks; j++) |
| 1223 |
InterfaceBlockStageIndex[i][j] = -1; |
| 1224 |
|
| 1225 |
if (sh == NULL) |
| 1226 |
continue; |
| 1227 |
|
| 1228 |
unsigned sh_num_blocks; |
| 1229 |
struct gl_uniform_block **sh_blks; |
| 1230 |
if (validate_ssbo) { |
| 1231 |
sh_num_blocks = prog->_LinkedShaders[i]->Program->info.num_ssbos; |
| 1232 |
sh_blks = sh->Program->sh.ShaderStorageBlocks; |
| 1233 |
} else { |
| 1234 |
sh_num_blocks = prog->_LinkedShaders[i]->Program->info.num_ubos; |
| 1235 |
sh_blks = sh->Program->sh.UniformBlocks; |
| 1236 |
} |
| 1237 |
|
| 1238 |
for (unsigned int j = 0; j < sh_num_blocks; j++) { |
| 1239 |
int index = link_cross_validate_uniform_block(prog->data, &blks, |
| 1240 |
num_blks, sh_blks[j]); |
| 1241 |
|
| 1242 |
if (index == -1) { |
| 1243 |
linker_error(prog, "buffer block `%s' has mismatching " |
| 1244 |
"definitions\n", sh_blks[j]->Name); |
| 1245 |
|
| 1246 |
for (unsigned k = 0; k <= i; k++) { |
| 1247 |
delete[] InterfaceBlockStageIndex[k]; |
| 1248 |
} |
| 1249 |
|
| 1250 |
/* Reset the block count. This will help avoid various segfaults |
| 1251 |
* from api calls that assume the array exists due to the count |
| 1252 |
* being non-zero. |
| 1253 |
*/ |
| 1254 |
*num_blks = 0; |
| 1255 |
return false; |
| 1256 |
} |
| 1257 |
|
| 1258 |
InterfaceBlockStageIndex[i][index] = j; |
| 1259 |
} |
| 1260 |
} |
| 1261 |
|
| 1262 |
/* Update per stage block pointers to point to the program list. |
| 1263 |
* FIXME: We should be able to free the per stage blocks here. |
| 1264 |
*/ |
| 1265 |
for (unsigned i = 0; i < MESA_SHADER_STAGES; i++) { |
| 1266 |
for (unsigned j = 0; j < *num_blks; j++) { |
| 1267 |
int stage_index = InterfaceBlockStageIndex[i][j]; |
| 1268 |
|
| 1269 |
if (stage_index != -1) { |
| 1270 |
struct gl_linked_shader *sh = prog->_LinkedShaders[i]; |
| 1271 |
|
| 1272 |
struct gl_uniform_block **sh_blks = validate_ssbo ? |
| 1273 |
sh->Program->sh.ShaderStorageBlocks : |
| 1274 |
sh->Program->sh.UniformBlocks; |
| 1275 |
|
| 1276 |
blks[j].stageref |= sh_blks[stage_index]->stageref; |
| 1277 |
sh_blks[stage_index] = &blks[j]; |
| 1278 |
} |
| 1279 |
} |
| 1280 |
} |
| 1281 |
|
| 1282 |
for (unsigned i = 0; i < MESA_SHADER_STAGES; i++) { |
| 1283 |
delete[] InterfaceBlockStageIndex[i]; |
| 1284 |
} |
| 1285 |
|
| 1286 |
if (validate_ssbo) |
| 1287 |
prog->data->ShaderStorageBlocks = blks; |
| 1288 |
else |
| 1289 |
prog->data->UniformBlocks = blks; |
| 1290 |
|
| 1291 |
return true; |
| 1292 |
} |
| 1293 |
|
| 1294 |
/** |
| 1295 |
* Verifies the invariance of built-in special variables. |
| 1296 |
*/ |
| 1297 |
static bool |
| 1298 |
validate_invariant_builtins(struct gl_shader_program *prog, |
| 1299 |
const gl_linked_shader *vert, |
| 1300 |
const gl_linked_shader *frag) |
| 1301 |
{ |
| 1302 |
const ir_variable *var_vert; |
| 1303 |
const ir_variable *var_frag; |
| 1304 |
|
| 1305 |
if (!vert || !frag) |
| 1306 |
return true; |
| 1307 |
|
| 1308 |
/* |
| 1309 |
* From OpenGL ES Shading Language 1.0 specification |
| 1310 |
* (4.6.4 Invariance and Linkage): |
| 1311 |
* "The invariance of varyings that are declared in both the vertex and |
| 1312 |
* fragment shaders must match. For the built-in special variables, |
| 1313 |
* gl_FragCoord can only be declared invariant if and only if |
| 1314 |
* gl_Position is declared invariant. Similarly gl_PointCoord can only |
| 1315 |
* be declared invariant if and only if gl_PointSize is declared |
| 1316 |
* invariant. It is an error to declare gl_FrontFacing as invariant. |
| 1317 |
* The invariance of gl_FrontFacing is the same as the invariance of |
| 1318 |
* gl_Position." |
| 1319 |
*/ |
| 1320 |
var_frag = frag->symbols->get_variable("gl_FragCoord"); |
| 1321 |
if (var_frag && var_frag->data.invariant) { |
| 1322 |
var_vert = vert->symbols->get_variable("gl_Position"); |
| 1323 |
if (var_vert && !var_vert->data.invariant) { |
| 1324 |
linker_error(prog, |
| 1325 |
"fragment shader built-in `%s' has invariant qualifier, " |
| 1326 |
"but vertex shader built-in `%s' lacks invariant qualifier\n", |
| 1327 |
var_frag->name, var_vert->name); |
| 1328 |
return false; |
| 1329 |
} |
| 1330 |
} |
| 1331 |
|
| 1332 |
var_frag = frag->symbols->get_variable("gl_PointCoord"); |
| 1333 |
if (var_frag && var_frag->data.invariant) { |
| 1334 |
var_vert = vert->symbols->get_variable("gl_PointSize"); |
| 1335 |
if (var_vert && !var_vert->data.invariant) { |
| 1336 |
linker_error(prog, |
| 1337 |
"fragment shader built-in `%s' has invariant qualifier, " |
| 1338 |
"but vertex shader built-in `%s' lacks invariant qualifier\n", |
| 1339 |
var_frag->name, var_vert->name); |
| 1340 |
return false; |
| 1341 |
} |
| 1342 |
} |
| 1343 |
|
| 1344 |
var_frag = frag->symbols->get_variable("gl_FrontFacing"); |
| 1345 |
if (var_frag && var_frag->data.invariant) { |
| 1346 |
linker_error(prog, |
| 1347 |
"fragment shader built-in `%s' can not be declared as invariant\n", |
| 1348 |
var_frag->name); |
| 1349 |
return false; |
| 1350 |
} |
| 1351 |
|
| 1352 |
return true; |
| 1353 |
} |
| 1354 |
|
| 1355 |
/** |
| 1356 |
* Populates a shaders symbol table with all global declarations |
| 1357 |
*/ |
| 1358 |
static void |
| 1359 |
populate_symbol_table(gl_linked_shader *sh, glsl_symbol_table *symbols) |
| 1360 |
{ |
| 1361 |
sh->symbols = new(sh) glsl_symbol_table; |
| 1362 |
|
| 1363 |
_mesa_glsl_copy_symbols_from_table(sh->ir, symbols, sh->symbols); |
| 1364 |
} |
| 1365 |
|
| 1366 |
|
| 1367 |
/** |
| 1368 |
* Remap variables referenced in an instruction tree |
| 1369 |
* |
| 1370 |
* This is used when instruction trees are cloned from one shader and placed in |
| 1371 |
* another. These trees will contain references to \c ir_variable nodes that |
| 1372 |
* do not exist in the target shader. This function finds these \c ir_variable |
| 1373 |
* references and replaces the references with matching variables in the target |
| 1374 |
* shader. |
| 1375 |
* |
| 1376 |
* If there is no matching variable in the target shader, a clone of the |
| 1377 |
* \c ir_variable is made and added to the target shader. The new variable is |
| 1378 |
* added to \b both the instruction stream and the symbol table. |
| 1379 |
* |
| 1380 |
* \param inst IR tree that is to be processed. |
| 1381 |
* \param symbols Symbol table containing global scope symbols in the |
| 1382 |
* linked shader. |
| 1383 |
* \param instructions Instruction stream where new variable declarations |
| 1384 |
* should be added. |
| 1385 |
*/ |
| 1386 |
static void |
| 1387 |
remap_variables(ir_instruction *inst, struct gl_linked_shader *target, |
| 1388 |
hash_table *temps) |
| 1389 |
{ |
| 1390 |
class remap_visitor : public ir_hierarchical_visitor { |
| 1391 |
public: |
| 1392 |
remap_visitor(struct gl_linked_shader *target, hash_table *temps) |
| 1393 |
{ |
| 1394 |
this->target = target; |
| 1395 |
this->symbols = target->symbols; |
| 1396 |
this->instructions = target->ir; |
| 1397 |
this->temps = temps; |
| 1398 |
} |
| 1399 |
|
| 1400 |
virtual ir_visitor_status visit(ir_dereference_variable *ir) |
| 1401 |
{ |
| 1402 |
if (ir->var->data.mode == ir_var_temporary) { |
| 1403 |
hash_entry *entry = _mesa_hash_table_search(temps, ir->var); |
| 1404 |
ir_variable *var = entry ? (ir_variable *) entry->data : NULL; |
| 1405 |
|
| 1406 |
assert(var != NULL); |
| 1407 |
ir->var = var; |
| 1408 |
return visit_continue; |
| 1409 |
} |
| 1410 |
|
| 1411 |
ir_variable *const existing = |
| 1412 |
this->symbols->get_variable(ir->var->name); |
| 1413 |
if (existing != NULL) |
| 1414 |
ir->var = existing; |
| 1415 |
else { |
| 1416 |
ir_variable *copy = ir->var->clone(this->target, NULL); |
| 1417 |
|
| 1418 |
this->symbols->add_variable(copy); |
| 1419 |
this->instructions->push_head(copy); |
| 1420 |
ir->var = copy; |
| 1421 |
} |
| 1422 |
|
| 1423 |
return visit_continue; |
| 1424 |
} |
| 1425 |
|
| 1426 |
private: |
| 1427 |
struct gl_linked_shader *target; |
| 1428 |
glsl_symbol_table *symbols; |
| 1429 |
exec_list *instructions; |
| 1430 |
hash_table *temps; |
| 1431 |
}; |
| 1432 |
|
| 1433 |
remap_visitor v(target, temps); |
| 1434 |
|
| 1435 |
inst->accept(&v); |
| 1436 |
} |
| 1437 |
|
| 1438 |
|
| 1439 |
/** |
| 1440 |
* Move non-declarations from one instruction stream to another |
| 1441 |
* |
| 1442 |
* The intended usage pattern of this function is to pass the pointer to the |
| 1443 |
* head sentinel of a list (i.e., a pointer to the list cast to an \c exec_node |
| 1444 |
* pointer) for \c last and \c false for \c make_copies on the first |
| 1445 |
* call. Successive calls pass the return value of the previous call for |
| 1446 |
* \c last and \c true for \c make_copies. |
| 1447 |
* |
| 1448 |
* \param instructions Source instruction stream |
| 1449 |
* \param last Instruction after which new instructions should be |
| 1450 |
* inserted in the target instruction stream |
| 1451 |
* \param make_copies Flag selecting whether instructions in \c instructions |
| 1452 |
* should be copied (via \c ir_instruction::clone) into the |
| 1453 |
* target list or moved. |
| 1454 |
* |
| 1455 |
* \return |
| 1456 |
* The new "last" instruction in the target instruction stream. This pointer |
| 1457 |
* is suitable for use as the \c last parameter of a later call to this |
| 1458 |
* function. |
| 1459 |
*/ |
| 1460 |
static exec_node * |
| 1461 |
move_non_declarations(exec_list *instructions, exec_node *last, |
| 1462 |
bool make_copies, gl_linked_shader *target) |
| 1463 |
{ |
| 1464 |
hash_table *temps = NULL; |
| 1465 |
|
| 1466 |
if (make_copies) |
| 1467 |
temps = _mesa_pointer_hash_table_create(NULL); |
| 1468 |
|
| 1469 |
foreach_in_list_safe(ir_instruction, inst, instructions) { |
| 1470 |
if (inst->as_function()) |
| 1471 |
continue; |
| 1472 |
|
| 1473 |
if (inst->ir_type == ir_type_precision) |
| 1474 |
continue; |
| 1475 |
if (inst->ir_type == ir_type_typedecl) |
| 1476 |
continue; |
| 1477 |
|
| 1478 |
ir_variable *var = inst->as_variable(); |
| 1479 |
if ((var != NULL) && (var->data.mode != ir_var_temporary)) |
| 1480 |
continue; |
| 1481 |
|
| 1482 |
assert(inst->as_assignment() |
| 1483 |
|| inst->as_call() |
| 1484 |
|| inst->as_if() /* for initializers with the ?: operator */ |
| 1485 |
|| ((var != NULL) && (var->data.mode == ir_var_temporary))); |
| 1486 |
|
| 1487 |
if (make_copies) { |
| 1488 |
inst = inst->clone(target, NULL); |
| 1489 |
|
| 1490 |
if (var != NULL) |
| 1491 |
_mesa_hash_table_insert(temps, var, inst); |
| 1492 |
else |
| 1493 |
remap_variables(inst, target, temps); |
| 1494 |
} else { |
| 1495 |
inst->remove(); |
| 1496 |
} |
| 1497 |
|
| 1498 |
last->insert_after(inst); |
| 1499 |
last = inst; |
| 1500 |
} |
| 1501 |
|
| 1502 |
if (make_copies) |
| 1503 |
_mesa_hash_table_destroy(temps, NULL); |
| 1504 |
|
| 1505 |
return last; |
| 1506 |
} |
| 1507 |
|
| 1508 |
|
| 1509 |
/** |
| 1510 |
* This class is only used in link_intrastage_shaders() below but declaring |
| 1511 |
* it inside that function leads to compiler warnings with some versions of |
| 1512 |
* gcc. |
| 1513 |
*/ |
| 1514 |
class array_sizing_visitor : public deref_type_updater { |
| 1515 |
public: |
| 1516 |
using deref_type_updater::visit; |
| 1517 |
|
| 1518 |
array_sizing_visitor() |
| 1519 |
: mem_ctx(ralloc_context(NULL)), |
| 1520 |
unnamed_interfaces(_mesa_pointer_hash_table_create(NULL)) |
| 1521 |
{ |
| 1522 |
} |
| 1523 |
|
| 1524 |
~array_sizing_visitor() |
| 1525 |
{ |
| 1526 |
_mesa_hash_table_destroy(this->unnamed_interfaces, NULL); |
| 1527 |
ralloc_free(this->mem_ctx); |
| 1528 |
} |
| 1529 |
|
| 1530 |
virtual ir_visitor_status visit(ir_variable *var) |
| 1531 |
{ |
| 1532 |
const glsl_type *type_without_array; |
| 1533 |
bool implicit_sized_array = var->data.implicit_sized_array; |
| 1534 |
fixup_type(&var->type, var->data.max_array_access, |
| 1535 |
var->data.from_ssbo_unsized_array, |
| 1536 |
&implicit_sized_array); |
| 1537 |
var->data.implicit_sized_array = implicit_sized_array; |
| 1538 |
type_without_array = var->type->without_array(); |
| 1539 |
if (var->type->is_interface()) { |
| 1540 |
if (interface_contains_unsized_arrays(var->type)) { |
| 1541 |
const glsl_type *new_type = |
| 1542 |
resize_interface_members(var->type, |
| 1543 |
var->get_max_ifc_array_access(), |
| 1544 |
var->is_in_shader_storage_block()); |
| 1545 |
var->type = new_type; |
| 1546 |
var->change_interface_type(new_type); |
| 1547 |
} |
| 1548 |
} else if (type_without_array->is_interface()) { |
| 1549 |
if (interface_contains_unsized_arrays(type_without_array)) { |
| 1550 |
const glsl_type *new_type = |
| 1551 |
resize_interface_members(type_without_array, |
| 1552 |
var->get_max_ifc_array_access(), |
| 1553 |
var->is_in_shader_storage_block()); |
| 1554 |
var->change_interface_type(new_type); |
| 1555 |
var->type = update_interface_members_array(var->type, new_type); |
| 1556 |
} |
| 1557 |
} else if (const glsl_type *ifc_type = var->get_interface_type()) { |
| 1558 |
/* Store a pointer to the variable in the unnamed_interfaces |
| 1559 |
* hashtable. |
| 1560 |
*/ |
| 1561 |
hash_entry *entry = |
| 1562 |
_mesa_hash_table_search(this->unnamed_interfaces, |
| 1563 |
ifc_type); |
| 1564 |
|
| 1565 |
ir_variable **interface_vars = entry ? (ir_variable **) entry->data : NULL; |
| 1566 |
|
| 1567 |
if (interface_vars == NULL) { |
| 1568 |
interface_vars = rzalloc_array(mem_ctx, ir_variable *, |
| 1569 |
ifc_type->length); |
| 1570 |
_mesa_hash_table_insert(this->unnamed_interfaces, ifc_type, |
| 1571 |
interface_vars); |
| 1572 |
} |
| 1573 |
unsigned index = ifc_type->field_index(var->name); |
| 1574 |
assert(index < ifc_type->length); |
| 1575 |
assert(interface_vars[index] == NULL); |
| 1576 |
interface_vars[index] = var; |
| 1577 |
} |
| 1578 |
return visit_continue; |
| 1579 |
} |
| 1580 |
|
| 1581 |
/** |
| 1582 |
* For each unnamed interface block that was discovered while running the |
| 1583 |
* visitor, adjust the interface type to reflect the newly assigned array |
| 1584 |
* sizes, and fix up the ir_variable nodes to point to the new interface |
| 1585 |
* type. |
| 1586 |
*/ |
| 1587 |
void fixup_unnamed_interface_types() |
| 1588 |
{ |
| 1589 |
hash_table_call_foreach(this->unnamed_interfaces, |
| 1590 |
fixup_unnamed_interface_type, NULL); |
| 1591 |
} |
| 1592 |
|
| 1593 |
private: |
| 1594 |
/** |
| 1595 |
* If the type pointed to by \c type represents an unsized array, replace |
| 1596 |
* it with a sized array whose size is determined by max_array_access. |
| 1597 |
*/ |
| 1598 |
static void fixup_type(const glsl_type **type, unsigned max_array_access, |
| 1599 |
bool from_ssbo_unsized_array, bool *implicit_sized) |
| 1600 |
{ |
| 1601 |
if (!from_ssbo_unsized_array && (*type)->is_unsized_array()) { |
| 1602 |
*type = glsl_type::get_array_instance((*type)->fields.array, |
| 1603 |
max_array_access + 1); |
| 1604 |
*implicit_sized = true; |
| 1605 |
assert(*type != NULL); |
| 1606 |
} |
| 1607 |
} |
| 1608 |
|
| 1609 |
static const glsl_type * |
| 1610 |
update_interface_members_array(const glsl_type *type, |
| 1611 |
const glsl_type *new_interface_type) |
| 1612 |
{ |
| 1613 |
const glsl_type *element_type = type->fields.array; |
| 1614 |
if (element_type->is_array()) { |
| 1615 |
const glsl_type *new_array_type = |
| 1616 |
update_interface_members_array(element_type, new_interface_type); |
| 1617 |
return glsl_type::get_array_instance(new_array_type, type->length); |
| 1618 |
} else { |
| 1619 |
return glsl_type::get_array_instance(new_interface_type, |
| 1620 |
type->length); |
| 1621 |
} |
| 1622 |
} |
| 1623 |
|
| 1624 |
/** |
| 1625 |
* Determine whether the given interface type contains unsized arrays (if |
| 1626 |
* it doesn't, array_sizing_visitor doesn't need to process it). |
| 1627 |
*/ |
| 1628 |
static bool interface_contains_unsized_arrays(const glsl_type *type) |
| 1629 |
{ |
| 1630 |
for (unsigned i = 0; i < type->length; i++) { |
| 1631 |
const glsl_type *elem_type = type->fields.structure[i].type; |
| 1632 |
if (elem_type->is_unsized_array()) |
| 1633 |
return true; |
| 1634 |
} |
| 1635 |
return false; |
| 1636 |
} |
| 1637 |
|
| 1638 |
/** |
| 1639 |
* Create a new interface type based on the given type, with unsized arrays |
| 1640 |
* replaced by sized arrays whose size is determined by |
| 1641 |
* max_ifc_array_access. |
| 1642 |
*/ |
| 1643 |
static const glsl_type * |
| 1644 |
resize_interface_members(const glsl_type *type, |
| 1645 |
const int *max_ifc_array_access, |
| 1646 |
bool is_ssbo) |
| 1647 |
{ |
| 1648 |
unsigned num_fields = type->length; |
| 1649 |
glsl_struct_field *fields = new glsl_struct_field[num_fields]; |
| 1650 |
memcpy(fields, type->fields.structure, |
| 1651 |
num_fields * sizeof(*fields)); |
| 1652 |
for (unsigned i = 0; i < num_fields; i++) { |
| 1653 |
bool implicit_sized_array = fields[i].implicit_sized_array; |
| 1654 |
/* If SSBO last member is unsized array, we don't replace it by a sized |
| 1655 |
* array. |
| 1656 |
*/ |
| 1657 |
if (is_ssbo && i == (num_fields - 1)) |
| 1658 |
fixup_type(&fields[i].type, max_ifc_array_access[i], |
| 1659 |
true, &implicit_sized_array); |
| 1660 |
else |
| 1661 |
fixup_type(&fields[i].type, max_ifc_array_access[i], |
| 1662 |
false, &implicit_sized_array); |
| 1663 |
fields[i].implicit_sized_array = implicit_sized_array; |
| 1664 |
} |
| 1665 |
glsl_interface_packing packing = |
| 1666 |
(glsl_interface_packing) type->interface_packing; |
| 1667 |
bool row_major = (bool) type->interface_row_major; |
| 1668 |
const glsl_type *new_ifc_type = |
| 1669 |
glsl_type::get_interface_instance(fields, num_fields, |
| 1670 |
packing, row_major, type->name); |
| 1671 |
delete [] fields; |
| 1672 |
return new_ifc_type; |
| 1673 |
} |
| 1674 |
|
| 1675 |
static void fixup_unnamed_interface_type(const void *key, void *data, |
| 1676 |
void *) |
| 1677 |
{ |
| 1678 |
const glsl_type *ifc_type = (const glsl_type *) key; |
| 1679 |
ir_variable **interface_vars = (ir_variable **) data; |
| 1680 |
unsigned num_fields = ifc_type->length; |
| 1681 |
glsl_struct_field *fields = new glsl_struct_field[num_fields]; |
| 1682 |
memcpy(fields, ifc_type->fields.structure, |
| 1683 |
num_fields * sizeof(*fields)); |
| 1684 |
bool interface_type_changed = false; |
| 1685 |
for (unsigned i = 0; i < num_fields; i++) { |
| 1686 |
if (interface_vars[i] != NULL && |
| 1687 |
fields[i].type != interface_vars[i]->type) { |
| 1688 |
fields[i].type = interface_vars[i]->type; |
| 1689 |
interface_type_changed = true; |
| 1690 |
} |
| 1691 |
} |
| 1692 |
if (!interface_type_changed) { |
| 1693 |
delete [] fields; |
| 1694 |
return; |
| 1695 |
} |
| 1696 |
glsl_interface_packing packing = |
| 1697 |
(glsl_interface_packing) ifc_type->interface_packing; |
| 1698 |
bool row_major = (bool) ifc_type->interface_row_major; |
| 1699 |
const glsl_type *new_ifc_type = |
| 1700 |
glsl_type::get_interface_instance(fields, num_fields, packing, |
| 1701 |
row_major, ifc_type->name); |
| 1702 |
delete [] fields; |
| 1703 |
for (unsigned i = 0; i < num_fields; i++) { |
| 1704 |
if (interface_vars[i] != NULL) |
| 1705 |
interface_vars[i]->change_interface_type(new_ifc_type); |
| 1706 |
} |
| 1707 |
} |
| 1708 |
|
| 1709 |
/** |
| 1710 |
* Memory context used to allocate the data in \c unnamed_interfaces. |
| 1711 |
*/ |
| 1712 |
void *mem_ctx; |
| 1713 |
|
| 1714 |
/** |
| 1715 |
* Hash table from const glsl_type * to an array of ir_variable *'s |
| 1716 |
* pointing to the ir_variables constituting each unnamed interface block. |
| 1717 |
*/ |
| 1718 |
hash_table *unnamed_interfaces; |
| 1719 |
}; |
| 1720 |
|
| 1721 |
static bool |
| 1722 |
validate_xfb_buffer_stride(struct gl_context *ctx, unsigned idx, |
| 1723 |
struct gl_shader_program *prog) |
| 1724 |
{ |
| 1725 |
/* We will validate doubles at a later stage */ |
| 1726 |
if (prog->TransformFeedback.BufferStride[idx] % 4) { |
| 1727 |
linker_error(prog, "invalid qualifier xfb_stride=%d must be a " |
| 1728 |
"multiple of 4 or if its applied to a type that is " |
| 1729 |
"or contains a double a multiple of 8.", |
| 1730 |
prog->TransformFeedback.BufferStride[idx]); |
| 1731 |
return false; |
| 1732 |
} |
| 1733 |
|
| 1734 |
if (prog->TransformFeedback.BufferStride[idx] / 4 > |
| 1735 |
ctx->Const.MaxTransformFeedbackInterleavedComponents) { |
| 1736 |
linker_error(prog, "The MAX_TRANSFORM_FEEDBACK_INTERLEAVED_COMPONENTS " |
| 1737 |
"limit has been exceeded."); |
| 1738 |
return false; |
| 1739 |
} |
| 1740 |
|
| 1741 |
return true; |
| 1742 |
} |
| 1743 |
|
| 1744 |
/** |
| 1745 |
* Check for conflicting xfb_stride default qualifiers and store buffer stride |
| 1746 |
* for later use. |
| 1747 |
*/ |
| 1748 |
static void |
| 1749 |
link_xfb_stride_layout_qualifiers(struct gl_context *ctx, |
| 1750 |
struct gl_shader_program *prog, |
| 1751 |
struct gl_shader **shader_list, |
| 1752 |
unsigned num_shaders) |
| 1753 |
{ |
| 1754 |
for (unsigned i = 0; i < MAX_FEEDBACK_BUFFERS; i++) { |
| 1755 |
prog->TransformFeedback.BufferStride[i] = 0; |
| 1756 |
} |
| 1757 |
|
| 1758 |
for (unsigned i = 0; i < num_shaders; i++) { |
| 1759 |
struct gl_shader *shader = shader_list[i]; |
| 1760 |
|
| 1761 |
for (unsigned j = 0; j < MAX_FEEDBACK_BUFFERS; j++) { |
| 1762 |
if (shader->TransformFeedbackBufferStride[j]) { |
| 1763 |
if (prog->TransformFeedback.BufferStride[j] == 0) { |
| 1764 |
prog->TransformFeedback.BufferStride[j] = |
| 1765 |
shader->TransformFeedbackBufferStride[j]; |
| 1766 |
if (!validate_xfb_buffer_stride(ctx, j, prog)) |
| 1767 |
return; |
| 1768 |
} else if (prog->TransformFeedback.BufferStride[j] != |
| 1769 |
shader->TransformFeedbackBufferStride[j]){ |
| 1770 |
linker_error(prog, |
| 1771 |
"intrastage shaders defined with conflicting " |
| 1772 |
"xfb_stride for buffer %d (%d and %d)\n", j, |
| 1773 |
prog->TransformFeedback.BufferStride[j], |
| 1774 |
shader->TransformFeedbackBufferStride[j]); |
| 1775 |
return; |
| 1776 |
} |
| 1777 |
} |
| 1778 |
} |
| 1779 |
} |
| 1780 |
} |
| 1781 |
|
| 1782 |
/** |
| 1783 |
* Check for conflicting bindless/bound sampler/image layout qualifiers at |
| 1784 |
* global scope. |
| 1785 |
*/ |
| 1786 |
static void |
| 1787 |
link_bindless_layout_qualifiers(struct gl_shader_program *prog, |
| 1788 |
struct gl_shader **shader_list, |
| 1789 |
unsigned num_shaders) |
| 1790 |
{ |
| 1791 |
bool bindless_sampler, bindless_image; |
| 1792 |
bool bound_sampler, bound_image; |
| 1793 |
|
| 1794 |
bindless_sampler = bindless_image = false; |
| 1795 |
bound_sampler = bound_image = false; |
| 1796 |
|
| 1797 |
for (unsigned i = 0; i < num_shaders; i++) { |
| 1798 |
struct gl_shader *shader = shader_list[i]; |
| 1799 |
|
| 1800 |
if (shader->bindless_sampler) |
| 1801 |
bindless_sampler = true; |
| 1802 |
if (shader->bindless_image) |
| 1803 |
bindless_image = true; |
| 1804 |
if (shader->bound_sampler) |
| 1805 |
bound_sampler = true; |
| 1806 |
if (shader->bound_image) |
| 1807 |
bound_image = true; |
| 1808 |
|
| 1809 |
if ((bindless_sampler && bound_sampler) || |
| 1810 |
(bindless_image && bound_image)) { |
| 1811 |
/* From section 4.4.6 of the ARB_bindless_texture spec: |
| 1812 |
* |
| 1813 |
* "If both bindless_sampler and bound_sampler, or bindless_image |
| 1814 |
* and bound_image, are declared at global scope in any |
| 1815 |
* compilation unit, a link- time error will be generated." |
| 1816 |
*/ |
| 1817 |
linker_error(prog, "both bindless_sampler and bound_sampler, or " |
| 1818 |
"bindless_image and bound_image, can't be declared at " |
| 1819 |
"global scope"); |
| 1820 |
} |
| 1821 |
} |
| 1822 |
} |
| 1823 |
|
| 1824 |
/** |
| 1825 |
* Check for conflicting viewport_relative settings across shaders, and sets |
| 1826 |
* the value for the linked shader. |
| 1827 |
*/ |
| 1828 |
static void |
| 1829 |
link_layer_viewport_relative_qualifier(struct gl_shader_program *prog, |
| 1830 |
struct gl_program *gl_prog, |
| 1831 |
struct gl_shader **shader_list, |
| 1832 |
unsigned num_shaders) |
| 1833 |
{ |
| 1834 |
unsigned i; |
| 1835 |
|
| 1836 |
/* Find first shader with explicit layer declaration */ |
| 1837 |
for (i = 0; i < num_shaders; i++) { |
| 1838 |
if (shader_list[i]->redeclares_gl_layer) { |
| 1839 |
gl_prog->info.layer_viewport_relative = |
| 1840 |
shader_list[i]->layer_viewport_relative; |
| 1841 |
break; |
| 1842 |
} |
| 1843 |
} |
| 1844 |
|
| 1845 |
/* Now make sure that each subsequent shader's explicit layer declaration |
| 1846 |
* matches the first one's. |
| 1847 |
*/ |
| 1848 |
for (; i < num_shaders; i++) { |
| 1849 |
if (shader_list[i]->redeclares_gl_layer && |
| 1850 |
shader_list[i]->layer_viewport_relative != |
| 1851 |
gl_prog->info.layer_viewport_relative) { |
| 1852 |
linker_error(prog, "all gl_Layer redeclarations must have identical " |
| 1853 |
"viewport_relative settings"); |
| 1854 |
} |
| 1855 |
} |
| 1856 |
} |
| 1857 |
|
| 1858 |
/** |
| 1859 |
* Performs the cross-validation of tessellation control shader vertices and |
| 1860 |
* layout qualifiers for the attached tessellation control shaders, |
| 1861 |
* and propagates them to the linked TCS and linked shader program. |
| 1862 |
*/ |
| 1863 |
static void |
| 1864 |
link_tcs_out_layout_qualifiers(struct gl_shader_program *prog, |
| 1865 |
struct gl_program *gl_prog, |
| 1866 |
struct gl_shader **shader_list, |
| 1867 |
unsigned num_shaders) |
| 1868 |
{ |
| 1869 |
if (gl_prog->info.stage != MESA_SHADER_TESS_CTRL) |
| 1870 |
return; |
| 1871 |
|
| 1872 |
gl_prog->info.tess.tcs_vertices_out = 0; |
| 1873 |
|
| 1874 |
/* From the GLSL 4.0 spec (chapter 4.3.8.2): |
| 1875 |
* |
| 1876 |
* "All tessellation control shader layout declarations in a program |
| 1877 |
* must specify the same output patch vertex count. There must be at |
| 1878 |
* least one layout qualifier specifying an output patch vertex count |
| 1879 |
* in any program containing tessellation control shaders; however, |
| 1880 |
* such a declaration is not required in all tessellation control |
| 1881 |
* shaders." |
| 1882 |
*/ |
| 1883 |
|
| 1884 |
for (unsigned i = 0; i < num_shaders; i++) { |
| 1885 |
struct gl_shader *shader = shader_list[i]; |
| 1886 |
|
| 1887 |
if (shader->info.TessCtrl.VerticesOut != 0) { |
| 1888 |
if (gl_prog->info.tess.tcs_vertices_out != 0 && |
| 1889 |
gl_prog->info.tess.tcs_vertices_out != |
| 1890 |
(unsigned) shader->info.TessCtrl.VerticesOut) { |
| 1891 |
linker_error(prog, "tessellation control shader defined with " |
| 1892 |
"conflicting output vertex count (%d and %d)\n", |
| 1893 |
gl_prog->info.tess.tcs_vertices_out, |
| 1894 |
shader->info.TessCtrl.VerticesOut); |
| 1895 |
return; |
| 1896 |
} |
| 1897 |
gl_prog->info.tess.tcs_vertices_out = |
| 1898 |
shader->info.TessCtrl.VerticesOut; |
| 1899 |
} |
| 1900 |
} |
| 1901 |
|
| 1902 |
/* Just do the intrastage -> interstage propagation right now, |
| 1903 |
* since we already know we're in the right type of shader program |
| 1904 |
* for doing it. |
| 1905 |
*/ |
| 1906 |
if (gl_prog->info.tess.tcs_vertices_out == 0) { |
| 1907 |
linker_error(prog, "tessellation control shader didn't declare " |
| 1908 |
"vertices out layout qualifier\n"); |
| 1909 |
return; |
| 1910 |
} |
| 1911 |
} |
| 1912 |
|
| 1913 |
|
| 1914 |
/** |
| 1915 |
* Performs the cross-validation of tessellation evaluation shader |
| 1916 |
* primitive type, vertex spacing, ordering and point_mode layout qualifiers |
| 1917 |
* for the attached tessellation evaluation shaders, and propagates them |
| 1918 |
* to the linked TES and linked shader program. |
| 1919 |
*/ |
| 1920 |
static void |
| 1921 |
link_tes_in_layout_qualifiers(struct gl_shader_program *prog, |
| 1922 |
struct gl_program *gl_prog, |
| 1923 |
struct gl_shader **shader_list, |
| 1924 |
unsigned num_shaders) |
| 1925 |
{ |
| 1926 |
if (gl_prog->info.stage != MESA_SHADER_TESS_EVAL) |
| 1927 |
return; |
| 1928 |
|
| 1929 |
int point_mode = -1; |
| 1930 |
unsigned vertex_order = 0; |
| 1931 |
|
| 1932 |
gl_prog->info.tess.primitive_mode = PRIM_UNKNOWN; |
| 1933 |
gl_prog->info.tess.spacing = TESS_SPACING_UNSPECIFIED; |
| 1934 |
|
| 1935 |
/* From the GLSL 4.0 spec (chapter 4.3.8.1): |
| 1936 |
* |
| 1937 |
* "At least one tessellation evaluation shader (compilation unit) in |
| 1938 |
* a program must declare a primitive mode in its input layout. |
| 1939 |
* Declaration vertex spacing, ordering, and point mode identifiers is |
| 1940 |
* optional. It is not required that all tessellation evaluation |
| 1941 |
* shaders in a program declare a primitive mode. If spacing or |
| 1942 |
* vertex ordering declarations are omitted, the tessellation |
| 1943 |
* primitive generator will use equal spacing or counter-clockwise |
| 1944 |
* vertex ordering, respectively. If a point mode declaration is |
| 1945 |
* omitted, the tessellation primitive generator will produce lines or |
| 1946 |
* triangles according to the primitive mode." |
| 1947 |
*/ |
| 1948 |
|
| 1949 |
for (unsigned i = 0; i < num_shaders; i++) { |
| 1950 |
struct gl_shader *shader = shader_list[i]; |
| 1951 |
|
| 1952 |
if (shader->info.TessEval.PrimitiveMode != PRIM_UNKNOWN) { |
| 1953 |
if (gl_prog->info.tess.primitive_mode != PRIM_UNKNOWN && |
| 1954 |
gl_prog->info.tess.primitive_mode != |
| 1955 |
shader->info.TessEval.PrimitiveMode) { |
| 1956 |
linker_error(prog, "tessellation evaluation shader defined with " |
| 1957 |
"conflicting input primitive modes.\n"); |
| 1958 |
return; |
| 1959 |
} |
| 1960 |
gl_prog->info.tess.primitive_mode = |
| 1961 |
shader->info.TessEval.PrimitiveMode; |
| 1962 |
} |
| 1963 |
|
| 1964 |
if (shader->info.TessEval.Spacing != 0) { |
| 1965 |
if (gl_prog->info.tess.spacing != 0 && gl_prog->info.tess.spacing != |
| 1966 |
shader->info.TessEval.Spacing) { |
| 1967 |
linker_error(prog, "tessellation evaluation shader defined with " |
| 1968 |
"conflicting vertex spacing.\n"); |
| 1969 |
return; |
| 1970 |
} |
| 1971 |
gl_prog->info.tess.spacing = shader->info.TessEval.Spacing; |
| 1972 |
} |
| 1973 |
|
| 1974 |
if (shader->info.TessEval.VertexOrder != 0) { |
| 1975 |
if (vertex_order != 0 && |
| 1976 |
vertex_order != shader->info.TessEval.VertexOrder) { |
| 1977 |
linker_error(prog, "tessellation evaluation shader defined with " |
| 1978 |
"conflicting ordering.\n"); |
| 1979 |
return; |
| 1980 |
} |
| 1981 |
vertex_order = shader->info.TessEval.VertexOrder; |
| 1982 |
} |
| 1983 |
|
| 1984 |
if (shader->info.TessEval.PointMode != -1) { |
| 1985 |
if (point_mode != -1 && |
| 1986 |
point_mode != shader->info.TessEval.PointMode) { |
| 1987 |
linker_error(prog, "tessellation evaluation shader defined with " |
| 1988 |
"conflicting point modes.\n"); |
| 1989 |
return; |
| 1990 |
} |
| 1991 |
point_mode = shader->info.TessEval.PointMode; |
| 1992 |
} |
| 1993 |
|
| 1994 |
} |
| 1995 |
|
| 1996 |
/* Just do the intrastage -> interstage propagation right now, |
| 1997 |
* since we already know we're in the right type of shader program |
| 1998 |
* for doing it. |
| 1999 |
*/ |
| 2000 |
if (gl_prog->info.tess.primitive_mode == PRIM_UNKNOWN) { |
| 2001 |
linker_error(prog, |
| 2002 |
"tessellation evaluation shader didn't declare input " |
| 2003 |
"primitive modes.\n"); |
| 2004 |
return; |
| 2005 |
} |
| 2006 |
|
| 2007 |
if (gl_prog->info.tess.spacing == TESS_SPACING_UNSPECIFIED) |
| 2008 |
gl_prog->info.tess.spacing = TESS_SPACING_EQUAL; |
| 2009 |
|
| 2010 |
if (vertex_order == 0 || vertex_order == GL_CCW) |
| 2011 |
gl_prog->info.tess.ccw = true; |
| 2012 |
else |
| 2013 |
gl_prog->info.tess.ccw = false; |
| 2014 |
|
| 2015 |
|
| 2016 |
if (point_mode == -1 || point_mode == GL_FALSE) |
| 2017 |
gl_prog->info.tess.point_mode = false; |
| 2018 |
else |
| 2019 |
gl_prog->info.tess.point_mode = true; |
| 2020 |
} |
| 2021 |
|
| 2022 |
|
| 2023 |
/** |
| 2024 |
* Performs the cross-validation of layout qualifiers specified in |
| 2025 |
* redeclaration of gl_FragCoord for the attached fragment shaders, |
| 2026 |
* and propagates them to the linked FS and linked shader program. |
| 2027 |
*/ |
| 2028 |
static void |
| 2029 |
link_fs_inout_layout_qualifiers(struct gl_shader_program *prog, |
| 2030 |
struct gl_linked_shader *linked_shader, |
| 2031 |
struct gl_shader **shader_list, |
| 2032 |
unsigned num_shaders) |
| 2033 |
{ |
| 2034 |
bool redeclares_gl_fragcoord = false; |
| 2035 |
bool uses_gl_fragcoord = false; |
| 2036 |
bool origin_upper_left = false; |
| 2037 |
bool pixel_center_integer = false; |
| 2038 |
|
| 2039 |
if (linked_shader->Stage != MESA_SHADER_FRAGMENT || |
| 2040 |
(prog->data->Version < 150 && |
| 2041 |
!prog->ARB_fragment_coord_conventions_enable)) |
| 2042 |
return; |
| 2043 |
|
| 2044 |
for (unsigned i = 0; i < num_shaders; i++) { |
| 2045 |
struct gl_shader *shader = shader_list[i]; |
| 2046 |
/* From the GLSL 1.50 spec, page 39: |
| 2047 |
* |
| 2048 |
* "If gl_FragCoord is redeclared in any fragment shader in a program, |
| 2049 |
* it must be redeclared in all the fragment shaders in that program |
| 2050 |
* that have a static use gl_FragCoord." |
| 2051 |
*/ |
| 2052 |
if ((redeclares_gl_fragcoord && !shader->redeclares_gl_fragcoord && |
| 2053 |
shader->uses_gl_fragcoord) |
| 2054 |
|| (shader->redeclares_gl_fragcoord && !redeclares_gl_fragcoord && |
| 2055 |
uses_gl_fragcoord)) { |
| 2056 |
linker_error(prog, "fragment shader defined with conflicting " |
| 2057 |
"layout qualifiers for gl_FragCoord\n"); |
| 2058 |
} |
| 2059 |
|
| 2060 |
/* From the GLSL 1.50 spec, page 39: |
| 2061 |
* |
| 2062 |
* "All redeclarations of gl_FragCoord in all fragment shaders in a |
| 2063 |
* single program must have the same set of qualifiers." |
| 2064 |
*/ |
| 2065 |
if (redeclares_gl_fragcoord && shader->redeclares_gl_fragcoord && |
| 2066 |
(shader->origin_upper_left != origin_upper_left || |
| 2067 |
shader->pixel_center_integer != pixel_center_integer)) { |
| 2068 |
linker_error(prog, "fragment shader defined with conflicting " |
| 2069 |
"layout qualifiers for gl_FragCoord\n"); |
| 2070 |
} |
| 2071 |
|
| 2072 |
/* Update the linked shader state. Note that uses_gl_fragcoord should |
| 2073 |
* accumulate the results. The other values should replace. If there |
| 2074 |
* are multiple redeclarations, all the fields except uses_gl_fragcoord |
| 2075 |
* are already known to be the same. |
| 2076 |
*/ |
| 2077 |
if (shader->redeclares_gl_fragcoord || shader->uses_gl_fragcoord) { |
| 2078 |
redeclares_gl_fragcoord = shader->redeclares_gl_fragcoord; |
| 2079 |
uses_gl_fragcoord |= shader->uses_gl_fragcoord; |
| 2080 |
origin_upper_left = shader->origin_upper_left; |
| 2081 |
pixel_center_integer = shader->pixel_center_integer; |
| 2082 |
} |
| 2083 |
|
| 2084 |
linked_shader->Program->info.fs.early_fragment_tests |= |
| 2085 |
shader->EarlyFragmentTests || shader->PostDepthCoverage; |
| 2086 |
linked_shader->Program->info.fs.inner_coverage |= shader->InnerCoverage; |
| 2087 |
linked_shader->Program->info.fs.post_depth_coverage |= |
| 2088 |
shader->PostDepthCoverage; |
| 2089 |
linked_shader->Program->info.fs.pixel_interlock_ordered |= |
| 2090 |
shader->PixelInterlockOrdered; |
| 2091 |
linked_shader->Program->info.fs.pixel_interlock_unordered |= |
| 2092 |
shader->PixelInterlockUnordered; |
| 2093 |
linked_shader->Program->info.fs.sample_interlock_ordered |= |
| 2094 |
shader->SampleInterlockOrdered; |
| 2095 |
linked_shader->Program->info.fs.sample_interlock_unordered |= |
| 2096 |
shader->SampleInterlockUnordered; |
| 2097 |
linked_shader->Program->sh.fs.BlendSupport |= shader->BlendSupport; |
| 2098 |
} |
| 2099 |
|
| 2100 |
linked_shader->Program->info.fs.pixel_center_integer = pixel_center_integer; |
| 2101 |
linked_shader->Program->info.fs.origin_upper_left = origin_upper_left; |
| 2102 |
} |
| 2103 |
|
| 2104 |
/** |
| 2105 |
* Performs the cross-validation of geometry shader max_vertices and |
| 2106 |
* primitive type layout qualifiers for the attached geometry shaders, |
| 2107 |
* and propagates them to the linked GS and linked shader program. |
| 2108 |
*/ |
| 2109 |
static void |
| 2110 |
link_gs_inout_layout_qualifiers(struct gl_shader_program *prog, |
| 2111 |
struct gl_program *gl_prog, |
| 2112 |
struct gl_shader **shader_list, |
| 2113 |
unsigned num_shaders) |
| 2114 |
{ |
| 2115 |
/* No in/out qualifiers defined for anything but GLSL 1.50+ |
| 2116 |
* geometry shaders so far. |
| 2117 |
*/ |
| 2118 |
if (gl_prog->info.stage != MESA_SHADER_GEOMETRY || |
| 2119 |
prog->data->Version < 150) |
| 2120 |
return; |
| 2121 |
|
| 2122 |
int vertices_out = -1; |
| 2123 |
|
| 2124 |
gl_prog->info.gs.invocations = 0; |
| 2125 |
gl_prog->info.gs.input_primitive = PRIM_UNKNOWN; |
| 2126 |
gl_prog->info.gs.output_primitive = PRIM_UNKNOWN; |
| 2127 |
|
| 2128 |
/* From the GLSL 1.50 spec, page 46: |
| 2129 |
* |
| 2130 |
* "All geometry shader output layout declarations in a program |
| 2131 |
* must declare the same layout and same value for |
| 2132 |
* max_vertices. There must be at least one geometry output |
| 2133 |
* layout declaration somewhere in a program, but not all |
| 2134 |
* geometry shaders (compilation units) are required to |
| 2135 |
* declare it." |
| 2136 |
*/ |
| 2137 |
|
| 2138 |
for (unsigned i = 0; i < num_shaders; i++) { |
| 2139 |
struct gl_shader *shader = shader_list[i]; |
| 2140 |
|
| 2141 |
if (shader->info.Geom.InputType != PRIM_UNKNOWN) { |
| 2142 |
if (gl_prog->info.gs.input_primitive != PRIM_UNKNOWN && |
| 2143 |
gl_prog->info.gs.input_primitive != |
| 2144 |
shader->info.Geom.InputType) { |
| 2145 |
linker_error(prog, "geometry shader defined with conflicting " |
| 2146 |
"input types\n"); |
| 2147 |
return; |
| 2148 |
} |
| 2149 |
gl_prog->info.gs.input_primitive = shader->info.Geom.InputType; |
| 2150 |
} |
| 2151 |
|
| 2152 |
if (shader->info.Geom.OutputType != PRIM_UNKNOWN) { |
| 2153 |
if (gl_prog->info.gs.output_primitive != PRIM_UNKNOWN && |
| 2154 |
gl_prog->info.gs.output_primitive != |
| 2155 |
shader->info.Geom.OutputType) { |
| 2156 |
linker_error(prog, "geometry shader defined with conflicting " |
| 2157 |
"output types\n"); |
| 2158 |
return; |
| 2159 |
} |
| 2160 |
gl_prog->info.gs.output_primitive = shader->info.Geom.OutputType; |
| 2161 |
} |
| 2162 |
|
| 2163 |
if (shader->info.Geom.VerticesOut != -1) { |
| 2164 |
if (vertices_out != -1 && |
| 2165 |
vertices_out != shader->info.Geom.VerticesOut) { |
| 2166 |
linker_error(prog, "geometry shader defined with conflicting " |
| 2167 |
"output vertex count (%d and %d)\n", |
| 2168 |
vertices_out, shader->info.Geom.VerticesOut); |
| 2169 |
return; |
| 2170 |
} |
| 2171 |
vertices_out = shader->info.Geom.VerticesOut; |
| 2172 |
} |
| 2173 |
|
| 2174 |
if (shader->info.Geom.Invocations != 0) { |
| 2175 |
if (gl_prog->info.gs.invocations != 0 && |
| 2176 |
gl_prog->info.gs.invocations != |
| 2177 |
(unsigned) shader->info.Geom.Invocations) { |
| 2178 |
linker_error(prog, "geometry shader defined with conflicting " |
| 2179 |
"invocation count (%d and %d)\n", |
| 2180 |
gl_prog->info.gs.invocations, |
| 2181 |
shader->info.Geom.Invocations); |
| 2182 |
return; |
| 2183 |
} |
| 2184 |
gl_prog->info.gs.invocations = shader->info.Geom.Invocations; |
| 2185 |
} |
| 2186 |
} |
| 2187 |
|
| 2188 |
/* Just do the intrastage -> interstage propagation right now, |
| 2189 |
* since we already know we're in the right type of shader program |
| 2190 |
* for doing it. |
| 2191 |
*/ |
| 2192 |
if (gl_prog->info.gs.input_primitive == PRIM_UNKNOWN) { |
| 2193 |
linker_error(prog, |
| 2194 |
"geometry shader didn't declare primitive input type\n"); |
| 2195 |
return; |
| 2196 |
} |
| 2197 |
|
| 2198 |
if (gl_prog->info.gs.output_primitive == PRIM_UNKNOWN) { |
| 2199 |
linker_error(prog, |
| 2200 |
"geometry shader didn't declare primitive output type\n"); |
| 2201 |
return; |
| 2202 |
} |
| 2203 |
|
| 2204 |
if (vertices_out == -1) { |
| 2205 |
linker_error(prog, |
| 2206 |
"geometry shader didn't declare max_vertices\n"); |
| 2207 |
return; |
| 2208 |
} else { |
| 2209 |
gl_prog->info.gs.vertices_out = vertices_out; |
| 2210 |
} |
| 2211 |
|
| 2212 |
if (gl_prog->info.gs.invocations == 0) |
| 2213 |
gl_prog->info.gs.invocations = 1; |
| 2214 |
} |
| 2215 |
|
| 2216 |
|
| 2217 |
/** |
| 2218 |
* Perform cross-validation of compute shader local_size_{x,y,z} layout and |
| 2219 |
* derivative arrangement qualifiers for the attached compute shaders, and |
| 2220 |
* propagate them to the linked CS and linked shader program. |
| 2221 |
*/ |
| 2222 |
static void |
| 2223 |
link_cs_input_layout_qualifiers(struct gl_shader_program *prog, |
| 2224 |
struct gl_program *gl_prog, |
| 2225 |
struct gl_shader **shader_list, |
| 2226 |
unsigned num_shaders) |
| 2227 |
{ |
| 2228 |
/* This function is called for all shader stages, but it only has an effect |
| 2229 |
* for compute shaders. |
| 2230 |
*/ |
| 2231 |
if (gl_prog->info.stage != MESA_SHADER_COMPUTE) |
| 2232 |
return; |
| 2233 |
|
| 2234 |
for (int i = 0; i < 3; i++) |
| 2235 |
gl_prog->info.cs.local_size[i] = 0; |
| 2236 |
|
| 2237 |
gl_prog->info.cs.local_size_variable = false; |
| 2238 |
|
| 2239 |
gl_prog->info.cs.derivative_group = DERIVATIVE_GROUP_NONE; |
| 2240 |
|
| 2241 |
/* From the ARB_compute_shader spec, in the section describing local size |
| 2242 |
* declarations: |
| 2243 |
* |
| 2244 |
* If multiple compute shaders attached to a single program object |
| 2245 |
* declare local work-group size, the declarations must be identical; |
| 2246 |
* otherwise a link-time error results. Furthermore, if a program |
| 2247 |
* object contains any compute shaders, at least one must contain an |
| 2248 |
* input layout qualifier specifying the local work sizes of the |
| 2249 |
* program, or a link-time error will occur. |
| 2250 |
*/ |
| 2251 |
for (unsigned sh = 0; sh < num_shaders; sh++) { |
| 2252 |
struct gl_shader *shader = shader_list[sh]; |
| 2253 |
|
| 2254 |
if (shader->info.Comp.LocalSize[0] != 0) { |
| 2255 |
if (gl_prog->info.cs.local_size[0] != 0) { |
| 2256 |
for (int i = 0; i < 3; i++) { |
| 2257 |
if (gl_prog->info.cs.local_size[i] != |
| 2258 |
shader->info.Comp.LocalSize[i]) { |
| 2259 |
linker_error(prog, "compute shader defined with conflicting " |
| 2260 |
"local sizes\n"); |
| 2261 |
return; |
| 2262 |
} |
| 2263 |
} |
| 2264 |
} |
| 2265 |
for (int i = 0; i < 3; i++) { |
| 2266 |
gl_prog->info.cs.local_size[i] = |
| 2267 |
shader->info.Comp.LocalSize[i]; |
| 2268 |
} |
| 2269 |
} else if (shader->info.Comp.LocalSizeVariable) { |
| 2270 |
if (gl_prog->info.cs.local_size[0] != 0) { |
| 2271 |
/* The ARB_compute_variable_group_size spec says: |
| 2272 |
* |
| 2273 |
* If one compute shader attached to a program declares a |
| 2274 |
* variable local group size and a second compute shader |
| 2275 |
* attached to the same program declares a fixed local group |
| 2276 |
* size, a link-time error results. |
| 2277 |
*/ |
| 2278 |
linker_error(prog, "compute shader defined with both fixed and " |
| 2279 |
"variable local group size\n"); |
| 2280 |
return; |
| 2281 |
} |
| 2282 |
gl_prog->info.cs.local_size_variable = true; |
| 2283 |
} |
| 2284 |
|
| 2285 |
enum gl_derivative_group group = shader->info.Comp.DerivativeGroup; |
| 2286 |
if (group != DERIVATIVE_GROUP_NONE) { |
| 2287 |
if (gl_prog->info.cs.derivative_group != DERIVATIVE_GROUP_NONE && |
| 2288 |
gl_prog->info.cs.derivative_group != group) { |
| 2289 |
linker_error(prog, "compute shader defined with conflicting " |
| 2290 |
"derivative groups\n"); |
| 2291 |
return; |
| 2292 |
} |
| 2293 |
gl_prog->info.cs.derivative_group = group; |
| 2294 |
} |
| 2295 |
} |
| 2296 |
|
| 2297 |
/* Just do the intrastage -> interstage propagation right now, |
| 2298 |
* since we already know we're in the right type of shader program |
| 2299 |
* for doing it. |
| 2300 |
*/ |
| 2301 |
if (gl_prog->info.cs.local_size[0] == 0 && |
| 2302 |
!gl_prog->info.cs.local_size_variable) { |
| 2303 |
linker_error(prog, "compute shader must contain a fixed or a variable " |
| 2304 |
"local group size\n"); |
| 2305 |
return; |
| 2306 |
} |
| 2307 |
|
| 2308 |
if (gl_prog->info.cs.derivative_group == DERIVATIVE_GROUP_QUADS) { |
| 2309 |
if (gl_prog->info.cs.local_size[0] % 2 != 0) { |
| 2310 |
linker_error(prog, "derivative_group_quadsNV must be used with a " |
| 2311 |
"local group size whose first dimension " |
| 2312 |
"is a multiple of 2\n"); |
| 2313 |
return; |
| 2314 |
} |
| 2315 |
if (gl_prog->info.cs.local_size[1] % 2 != 0) { |
| 2316 |
linker_error(prog, "derivative_group_quadsNV must be used with a local" |
| 2317 |
"group size whose second dimension " |
| 2318 |
"is a multiple of 2\n"); |
| 2319 |
return; |
| 2320 |
} |
| 2321 |
} else if (gl_prog->info.cs.derivative_group == DERIVATIVE_GROUP_LINEAR) { |
| 2322 |
if ((gl_prog->info.cs.local_size[0] * |
| 2323 |
gl_prog->info.cs.local_size[1] * |
| 2324 |
gl_prog->info.cs.local_size[2]) % 4 != 0) { |
| 2325 |
linker_error(prog, "derivative_group_linearNV must be used with a " |
| 2326 |
"local group size whose total number of invocations " |
| 2327 |
"is a multiple of 4\n"); |
| 2328 |
return; |
| 2329 |
} |
| 2330 |
} |
| 2331 |
} |
| 2332 |
|
| 2333 |
/** |
| 2334 |
* Link all out variables on a single stage which are not |
| 2335 |
* directly used in a shader with the main function. |
| 2336 |
*/ |
| 2337 |
static void |
| 2338 |
link_output_variables(struct gl_linked_shader *linked_shader, |
| 2339 |
struct gl_shader **shader_list, |
| 2340 |
unsigned num_shaders) |
| 2341 |
{ |
| 2342 |
struct glsl_symbol_table *symbols = linked_shader->symbols; |
| 2343 |
|
| 2344 |
for (unsigned i = 0; i < num_shaders; i++) { |
| 2345 |
|
| 2346 |
/* Skip shader object with main function */ |
| 2347 |
if (shader_list[i]->symbols->get_function("main")) |
| 2348 |
continue; |
| 2349 |
|
| 2350 |
foreach_in_list(ir_instruction, ir, shader_list[i]->ir) { |
| 2351 |
if (ir->ir_type != ir_type_variable) |
| 2352 |
continue; |
| 2353 |
|
| 2354 |
ir_variable *var = (ir_variable *) ir; |
| 2355 |
|
| 2356 |
if (var->data.mode == ir_var_shader_out && |
| 2357 |
!symbols->get_variable(var->name)) { |
| 2358 |
var = var->clone(linked_shader, NULL); |
| 2359 |
symbols->add_variable(var); |
| 2360 |
linked_shader->ir->push_head(var); |
| 2361 |
} |
| 2362 |
} |
| 2363 |
} |
| 2364 |
|
| 2365 |
return; |
| 2366 |
} |
| 2367 |
|
| 2368 |
|
| 2369 |
/** |
| 2370 |
* Combine a group of shaders for a single stage to generate a linked shader |
| 2371 |
* |
| 2372 |
* \note |
| 2373 |
* If this function is supplied a single shader, it is cloned, and the new |
| 2374 |
* shader is returned. |
| 2375 |
*/ |
| 2376 |
struct gl_linked_shader * |
| 2377 |
link_intrastage_shaders(void *mem_ctx, |
| 2378 |
struct gl_context *ctx, |
| 2379 |
struct gl_shader_program *prog, |
| 2380 |
struct gl_shader **shader_list, |
| 2381 |
unsigned num_shaders, |
| 2382 |
bool allow_missing_main) |
| 2383 |
{ |
| 2384 |
struct gl_uniform_block *ubo_blocks = NULL; |
| 2385 |
struct gl_uniform_block *ssbo_blocks = NULL; |
| 2386 |
unsigned num_ubo_blocks = 0; |
| 2387 |
unsigned num_ssbo_blocks = 0; |
| 2388 |
|
| 2389 |
/* Check that global variables defined in multiple shaders are consistent. |
| 2390 |
*/ |
| 2391 |
glsl_symbol_table variables; |
| 2392 |
for (unsigned i = 0; i < num_shaders; i++) { |
| 2393 |
if (shader_list[i] == NULL) |
| 2394 |
continue; |
| 2395 |
cross_validate_globals(ctx, prog, shader_list[i]->ir, &variables, |
| 2396 |
false); |
| 2397 |
} |
| 2398 |
|
| 2399 |
if (!prog->data->LinkStatus) |
| 2400 |
return NULL; |
| 2401 |
|
| 2402 |
/* Check that interface blocks defined in multiple shaders are consistent. |
| 2403 |
*/ |
| 2404 |
validate_intrastage_interface_blocks(prog, (const gl_shader **)shader_list, |
| 2405 |
num_shaders); |
| 2406 |
if (!prog->data->LinkStatus) |
| 2407 |
return NULL; |
| 2408 |
|
| 2409 |
/* Check that there is only a single definition of each function signature |
| 2410 |
* across all shaders. |
| 2411 |
*/ |
| 2412 |
for (unsigned i = 0; i < (num_shaders - 1); i++) { |
| 2413 |
foreach_in_list(ir_instruction, node, shader_list[i]->ir) { |
| 2414 |
ir_function *const f = node->as_function(); |
| 2415 |
|
| 2416 |
if (f == NULL) |
| 2417 |
continue; |
| 2418 |
|
| 2419 |
for (unsigned j = i + 1; j < num_shaders; j++) { |
| 2420 |
ir_function *const other = |
| 2421 |
shader_list[j]->symbols->get_function(f->name); |
| 2422 |
|
| 2423 |
/* If the other shader has no function (and therefore no function |
| 2424 |
* signatures) with the same name, skip to the next shader. |
| 2425 |
*/ |
| 2426 |
if (other == NULL) |
| 2427 |
continue; |
| 2428 |
|
| 2429 |
foreach_in_list(ir_function_signature, sig, &f->signatures) { |
| 2430 |
if (!sig->is_defined) |
| 2431 |
continue; |
| 2432 |
|
| 2433 |
ir_function_signature *other_sig = |
| 2434 |
other->exact_matching_signature(NULL, &sig->parameters); |
| 2435 |
|
| 2436 |
if (other_sig != NULL && other_sig->is_defined) { |
| 2437 |
linker_error(prog, "function `%s' is multiply defined\n", |
| 2438 |
f->name); |
| 2439 |
return NULL; |
| 2440 |
} |
| 2441 |
} |
| 2442 |
} |
| 2443 |
} |
| 2444 |
} |
| 2445 |
|
| 2446 |
/* Find the shader that defines main, and make a clone of it. |
| 2447 |
* |
| 2448 |
* Starting with the clone, search for undefined references. If one is |
| 2449 |
* found, find the shader that defines it. Clone the reference and add |
| 2450 |
* it to the shader. Repeat until there are no undefined references or |
| 2451 |
* until a reference cannot be resolved. |
| 2452 |
*/ |
| 2453 |
gl_shader *main = NULL; |
| 2454 |
for (unsigned i = 0; i < num_shaders; i++) { |
| 2455 |
if (_mesa_get_main_function_signature(shader_list[i]->symbols)) { |
| 2456 |
main = shader_list[i]; |
| 2457 |
break; |
| 2458 |
} |
| 2459 |
} |
| 2460 |
|
| 2461 |
if (main == NULL && allow_missing_main) |
| 2462 |
main = shader_list[0]; |
| 2463 |
|
| 2464 |
if (main == NULL) { |
| 2465 |
linker_error(prog, "%s shader lacks `main'\n", |
| 2466 |
_mesa_shader_stage_to_string(shader_list[0]->Stage)); |
| 2467 |
return NULL; |
| 2468 |
} |
| 2469 |
|
| 2470 |
gl_linked_shader *linked = rzalloc(NULL, struct gl_linked_shader); |
| 2471 |
linked->Stage = shader_list[0]->Stage; |
| 2472 |
|
| 2473 |
/* Create program and attach it to the linked shader */ |
| 2474 |
struct gl_program *gl_prog = |
| 2475 |
ctx->Driver.NewProgram(ctx, shader_list[0]->Stage, prog->Name, false); |
| 2476 |
if (!gl_prog) { |
| 2477 |
prog->data->LinkStatus = LINKING_FAILURE; |
| 2478 |
_mesa_delete_linked_shader(ctx, linked); |
| 2479 |
return NULL; |
| 2480 |
} |
| 2481 |
|
| 2482 |
_mesa_reference_shader_program_data(ctx, &gl_prog->sh.data, prog->data); |
| 2483 |
|
| 2484 |
/* Don't use _mesa_reference_program() just take ownership */ |
| 2485 |
linked->Program = gl_prog; |
| 2486 |
|
| 2487 |
linked->ir = new(linked) exec_list; |
| 2488 |
clone_ir_list(mem_ctx, linked->ir, main->ir); |
| 2489 |
|
| 2490 |
link_fs_inout_layout_qualifiers(prog, linked, shader_list, num_shaders); |
| 2491 |
link_tcs_out_layout_qualifiers(prog, gl_prog, shader_list, num_shaders); |
| 2492 |
link_tes_in_layout_qualifiers(prog, gl_prog, shader_list, num_shaders); |
| 2493 |
link_gs_inout_layout_qualifiers(prog, gl_prog, shader_list, num_shaders); |
| 2494 |
link_cs_input_layout_qualifiers(prog, gl_prog, shader_list, num_shaders); |
| 2495 |
|
| 2496 |
if (linked->Stage != MESA_SHADER_FRAGMENT) |
| 2497 |
link_xfb_stride_layout_qualifiers(ctx, prog, shader_list, num_shaders); |
| 2498 |
|
| 2499 |
link_bindless_layout_qualifiers(prog, shader_list, num_shaders); |
| 2500 |
|
| 2501 |
link_layer_viewport_relative_qualifier(prog, gl_prog, shader_list, num_shaders); |
| 2502 |
|
| 2503 |
populate_symbol_table(linked, shader_list[0]->symbols); |
| 2504 |
|
| 2505 |
/* The pointer to the main function in the final linked shader (i.e., the |
| 2506 |
* copy of the original shader that contained the main function). |
| 2507 |
*/ |
| 2508 |
ir_function_signature *const main_sig = |
| 2509 |
_mesa_get_main_function_signature(linked->symbols); |
| 2510 |
|
| 2511 |
/* Move any instructions other than variable declarations or function |
| 2512 |
* declarations into main. |
| 2513 |
*/ |
| 2514 |
if (main_sig != NULL) { |
| 2515 |
exec_node *insertion_point = |
| 2516 |
move_non_declarations(linked->ir, (exec_node *) &main_sig->body, false, |
| 2517 |
linked); |
| 2518 |
|
| 2519 |
for (unsigned i = 0; i < num_shaders; i++) { |
| 2520 |
if (shader_list[i] == main) |
| 2521 |
continue; |
| 2522 |
|
| 2523 |
insertion_point = move_non_declarations(shader_list[i]->ir, |
| 2524 |
insertion_point, true, linked); |
| 2525 |
} |
| 2526 |
} |
| 2527 |
|
| 2528 |
if (!link_function_calls(prog, linked, shader_list, num_shaders)) { |
| 2529 |
_mesa_delete_linked_shader(ctx, linked); |
| 2530 |
return NULL; |
| 2531 |
} |
| 2532 |
|
| 2533 |
if (linked->Stage != MESA_SHADER_FRAGMENT) |
| 2534 |
link_output_variables(linked, shader_list, num_shaders); |
| 2535 |
|
| 2536 |
/* Make a pass over all variable declarations to ensure that arrays with |
| 2537 |
* unspecified sizes have a size specified. The size is inferred from the |
| 2538 |
* max_array_access field. |
| 2539 |
*/ |
| 2540 |
array_sizing_visitor v; |
| 2541 |
v.run(linked->ir); |
| 2542 |
v.fixup_unnamed_interface_types(); |
| 2543 |
|
| 2544 |
/* Link up uniform blocks defined within this stage. */ |
| 2545 |
link_uniform_blocks(mem_ctx, ctx, prog, linked, &ubo_blocks, |
| 2546 |
&num_ubo_blocks, &ssbo_blocks, &num_ssbo_blocks); |
| 2547 |
|
| 2548 |
const unsigned max_uniform_blocks = |
| 2549 |
ctx->Const.Program[linked->Stage].MaxUniformBlocks; |
| 2550 |
if (num_ubo_blocks > max_uniform_blocks) { |
| 2551 |
linker_error(prog, "Too many %s uniform blocks (%d/%d)\n", |
| 2552 |
_mesa_shader_stage_to_string(linked->Stage), |
| 2553 |
num_ubo_blocks, max_uniform_blocks); |
| 2554 |
} |
| 2555 |
|
| 2556 |
const unsigned max_shader_storage_blocks = |
| 2557 |
ctx->Const.Program[linked->Stage].MaxShaderStorageBlocks; |
| 2558 |
if (num_ssbo_blocks > max_shader_storage_blocks) { |
| 2559 |
linker_error(prog, "Too many %s shader storage blocks (%d/%d)\n", |
| 2560 |
_mesa_shader_stage_to_string(linked->Stage), |
| 2561 |
num_ssbo_blocks, max_shader_storage_blocks); |
| 2562 |
} |
| 2563 |
|
| 2564 |
if (!prog->data->LinkStatus) { |
| 2565 |
_mesa_delete_linked_shader(ctx, linked); |
| 2566 |
return NULL; |
| 2567 |
} |
| 2568 |
|
| 2569 |
/* Copy ubo blocks to linked shader list */ |
| 2570 |
linked->Program->sh.UniformBlocks = |
| 2571 |
ralloc_array(linked, gl_uniform_block *, num_ubo_blocks); |
| 2572 |
ralloc_steal(linked, ubo_blocks); |
| 2573 |
for (unsigned i = 0; i < num_ubo_blocks; i++) { |
| 2574 |
linked->Program->sh.UniformBlocks[i] = &ubo_blocks[i]; |
| 2575 |
} |
| 2576 |
linked->Program->info.num_ubos = num_ubo_blocks; |
| 2577 |
|
| 2578 |
/* Copy ssbo blocks to linked shader list */ |
| 2579 |
linked->Program->sh.ShaderStorageBlocks = |
| 2580 |
ralloc_array(linked, gl_uniform_block *, num_ssbo_blocks); |
| 2581 |
ralloc_steal(linked, ssbo_blocks); |
| 2582 |
for (unsigned i = 0; i < num_ssbo_blocks; i++) { |
| 2583 |
linked->Program->sh.ShaderStorageBlocks[i] = &ssbo_blocks[i]; |
| 2584 |
} |
| 2585 |
linked->Program->info.num_ssbos = num_ssbo_blocks; |
| 2586 |
|
| 2587 |
/* At this point linked should contain all of the linked IR, so |
| 2588 |
* validate it to make sure nothing went wrong. |
| 2589 |
*/ |
| 2590 |
validate_ir_tree(linked->ir); |
| 2591 |
|
| 2592 |
/* Set the size of geometry shader input arrays */ |
| 2593 |
if (linked->Stage == MESA_SHADER_GEOMETRY) { |
| 2594 |
unsigned num_vertices = |
| 2595 |
vertices_per_prim(gl_prog->info.gs.input_primitive); |
| 2596 |
array_resize_visitor input_resize_visitor(num_vertices, prog, |
| 2597 |
MESA_SHADER_GEOMETRY); |
| 2598 |
foreach_in_list(ir_instruction, ir, linked->ir) { |
| 2599 |
ir->accept(&input_resize_visitor); |
| 2600 |
} |
| 2601 |
} |
| 2602 |
|
| 2603 |
if (ctx->Const.VertexID_is_zero_based) |
| 2604 |
lower_vertex_id(linked); |
| 2605 |
|
| 2606 |
if (ctx->Const.LowerCsDerivedVariables) |
| 2607 |
lower_cs_derived(linked); |
| 2608 |
|
| 2609 |
#ifdef DEBUG |
| 2610 |
/* Compute the source checksum. */ |
| 2611 |
linked->SourceChecksum = 0; |
| 2612 |
for (unsigned i = 0; i < num_shaders; i++) { |
| 2613 |
if (shader_list[i] == NULL) |
| 2614 |
continue; |
| 2615 |
linked->SourceChecksum ^= shader_list[i]->SourceChecksum; |
| 2616 |
} |
| 2617 |
#endif |
| 2618 |
|
| 2619 |
return linked; |
| 2620 |
} |
| 2621 |
|
| 2622 |
/** |
| 2623 |
* Update the sizes of linked shader uniform arrays to the maximum |
| 2624 |
* array index used. |
| 2625 |
* |
| 2626 |
* From page 81 (page 95 of the PDF) of the OpenGL 2.1 spec: |
| 2627 |
* |
| 2628 |
* If one or more elements of an array are active, |
| 2629 |
* GetActiveUniform will return the name of the array in name, |
| 2630 |
* subject to the restrictions listed above. The type of the array |
| 2631 |
* is returned in type. The size parameter contains the highest |
| 2632 |
* array element index used, plus one. The compiler or linker |
| 2633 |
* determines the highest index used. There will be only one |
| 2634 |
* active uniform reported by the GL per uniform array. |
| 2635 |
|
| 2636 |
*/ |
| 2637 |
static void |
| 2638 |
update_array_sizes(struct gl_shader_program *prog) |
| 2639 |
{ |
| 2640 |
for (unsigned i = 0; i < MESA_SHADER_STAGES; i++) { |
| 2641 |
if (prog->_LinkedShaders[i] == NULL) |
| 2642 |
continue; |
| 2643 |
|
| 2644 |
bool types_were_updated = false; |
| 2645 |
|
| 2646 |
foreach_in_list(ir_instruction, node, prog->_LinkedShaders[i]->ir) { |
| 2647 |
ir_variable *const var = node->as_variable(); |
| 2648 |
|
| 2649 |
if ((var == NULL) || (var->data.mode != ir_var_uniform) || |
| 2650 |
!var->type->is_array()) |
| 2651 |
continue; |
| 2652 |
|
| 2653 |
/* GL_ARB_uniform_buffer_object says that std140 uniforms |
| 2654 |
* will not be eliminated. Since we always do std140, just |
| 2655 |
* don't resize arrays in UBOs. |
| 2656 |
* |
| 2657 |
* Atomic counters are supposed to get deterministic |
| 2658 |
* locations assigned based on the declaration ordering and |
| 2659 |
* sizes, array compaction would mess that up. |
| 2660 |
* |
| 2661 |
* Subroutine uniforms are not removed. |
| 2662 |
*/ |
| 2663 |
if (var->is_in_buffer_block() || var->type->contains_atomic() || |
| 2664 |
var->type->contains_subroutine() || var->constant_initializer) |
| 2665 |
continue; |
| 2666 |
|
| 2667 |
int size = var->data.max_array_access; |
| 2668 |
for (unsigned j = 0; j < MESA_SHADER_STAGES; j++) { |
| 2669 |
if (prog->_LinkedShaders[j] == NULL) |
| 2670 |
continue; |
| 2671 |
|
| 2672 |
foreach_in_list(ir_instruction, node2, prog->_LinkedShaders[j]->ir) { |
| 2673 |
ir_variable *other_var = node2->as_variable(); |
| 2674 |
if (!other_var) |
| 2675 |
continue; |
| 2676 |
|
| 2677 |
if (strcmp(var->name, other_var->name) == 0 && |
| 2678 |
other_var->data.max_array_access > size) { |
| 2679 |
size = other_var->data.max_array_access; |
| 2680 |
} |
| 2681 |
} |
| 2682 |
} |
| 2683 |
|
| 2684 |
if (size + 1 != (int)var->type->length) { |
| 2685 |
/* If this is a built-in uniform (i.e., it's backed by some |
| 2686 |
* fixed-function state), adjust the number of state slots to |
| 2687 |
* match the new array size. The number of slots per array entry |
| 2688 |
* is not known. It seems safe to assume that the total number of |
| 2689 |
* slots is an integer multiple of the number of array elements. |
| 2690 |
* Determine the number of slots per array element by dividing by |
| 2691 |
* the old (total) size. |
| 2692 |
*/ |
| 2693 |
const unsigned num_slots = var->get_num_state_slots(); |
| 2694 |
if (num_slots > 0) { |
| 2695 |
var->set_num_state_slots((size + 1) |
| 2696 |
* (num_slots / var->type->length)); |
| 2697 |
} |
| 2698 |
|
| 2699 |
var->type = glsl_type::get_array_instance(var->type->fields.array, |
| 2700 |
size + 1); |
| 2701 |
types_were_updated = true; |
| 2702 |
} |
| 2703 |
} |
| 2704 |
|
| 2705 |
/* Update the types of dereferences in case we changed any. */ |
| 2706 |
if (types_were_updated) { |
| 2707 |
deref_type_updater v; |
| 2708 |
v.run(prog->_LinkedShaders[i]->ir); |
| 2709 |
} |
| 2710 |
} |
| 2711 |
} |
| 2712 |
|
| 2713 |
/** |
| 2714 |
* Resize tessellation evaluation per-vertex inputs to the size of |
| 2715 |
* tessellation control per-vertex outputs. |
| 2716 |
*/ |
| 2717 |
static void |
| 2718 |
resize_tes_inputs(struct gl_context *ctx, |
| 2719 |
struct gl_shader_program *prog) |
| 2720 |
{ |
| 2721 |
if (prog->_LinkedShaders[MESA_SHADER_TESS_EVAL] == NULL) |
| 2722 |
return; |
| 2723 |
|
| 2724 |
gl_linked_shader *const tcs = prog->_LinkedShaders[MESA_SHADER_TESS_CTRL]; |
| 2725 |
gl_linked_shader *const tes = prog->_LinkedShaders[MESA_SHADER_TESS_EVAL]; |
| 2726 |
|
| 2727 |
/* If no control shader is present, then the TES inputs are statically |
| 2728 |
* sized to MaxPatchVertices; the actual size of the arrays won't be |
| 2729 |
* known until draw time. |
| 2730 |
*/ |
| 2731 |
const int num_vertices = tcs |
| 2732 |
? tcs->Program->info.tess.tcs_vertices_out |
| 2733 |
: ctx->Const.MaxPatchVertices; |
| 2734 |
|
| 2735 |
array_resize_visitor input_resize_visitor(num_vertices, prog, |
| 2736 |
MESA_SHADER_TESS_EVAL); |
| 2737 |
foreach_in_list(ir_instruction, ir, tes->ir) { |
| 2738 |
ir->accept(&input_resize_visitor); |
| 2739 |
} |
| 2740 |
|
| 2741 |
if (tcs) { |
| 2742 |
/* Convert the gl_PatchVerticesIn system value into a constant, since |
| 2743 |
* the value is known at this point. |
| 2744 |
*/ |
| 2745 |
foreach_in_list(ir_instruction, ir, tes->ir) { |
| 2746 |
ir_variable *var = ir->as_variable(); |
| 2747 |
if (var && var->data.mode == ir_var_system_value && |
| 2748 |
var->data.location == SYSTEM_VALUE_VERTICES_IN) { |
| 2749 |
void *mem_ctx = ralloc_parent(var); |
| 2750 |
var->data.location = 0; |
| 2751 |
var->data.explicit_location = false; |
| 2752 |
var->data.mode = ir_var_auto; |
| 2753 |
var->constant_value = new(mem_ctx) ir_constant(num_vertices); |
| 2754 |
} |
| 2755 |
} |
| 2756 |
} |
| 2757 |
} |
| 2758 |
|
| 2759 |
/** |
| 2760 |
* Find a contiguous set of available bits in a bitmask. |
| 2761 |
* |
| 2762 |
* \param used_mask Bits representing used (1) and unused (0) locations |
| 2763 |
* \param needed_count Number of contiguous bits needed. |
| 2764 |
* |
| 2765 |
* \return |
| 2766 |
* Base location of the available bits on success or -1 on failure. |
| 2767 |
*/ |
| 2768 |
static int |
| 2769 |
find_available_slots(unsigned used_mask, unsigned needed_count) |
| 2770 |
{ |
| 2771 |
unsigned needed_mask = (1 << needed_count) - 1; |
| 2772 |
const int max_bit_to_test = (8 * sizeof(used_mask)) - needed_count; |
| 2773 |
|
| 2774 |
/* The comparison to 32 is redundant, but without it GCC emits "warning: |
| 2775 |
* cannot optimize possibly infinite loops" for the loop below. |
| 2776 |
*/ |
| 2777 |
if ((needed_count == 0) || (max_bit_to_test < 0) || (max_bit_to_test > 32)) |
| 2778 |
return -1; |
| 2779 |
|
| 2780 |
for (int i = 0; i <= max_bit_to_test; i++) { |
| 2781 |
if ((needed_mask & ~used_mask) == needed_mask) |
| 2782 |
return i; |
| 2783 |
|
| 2784 |
needed_mask <<= 1; |
| 2785 |
} |
| 2786 |
|
| 2787 |
return -1; |
| 2788 |
} |
| 2789 |
|
| 2790 |
|
| 2791 |
#define SAFE_MASK_FROM_INDEX(i) (((i) >= 32) ? ~0 : ((1 << (i)) - 1)) |
| 2792 |
|
| 2793 |
/** |
| 2794 |
* Assign locations for either VS inputs or FS outputs. |
| 2795 |
* |
| 2796 |
* \param mem_ctx Temporary ralloc context used for linking. |
| 2797 |
* \param prog Shader program whose variables need locations |
| 2798 |
* assigned. |
| 2799 |
* \param constants Driver specific constant values for the program. |
| 2800 |
* \param target_index Selector for the program target to receive location |
| 2801 |
* assignmnets. Must be either \c MESA_SHADER_VERTEX or |
| 2802 |
* \c MESA_SHADER_FRAGMENT. |
| 2803 |
* \param do_assignment Whether we are actually marking the assignment or we |
| 2804 |
* are just doing a dry-run checking. |
| 2805 |
* |
| 2806 |
* \return |
| 2807 |
* If locations are (or can be, in case of dry-running) successfully assigned, |
| 2808 |
* true is returned. Otherwise an error is emitted to the shader link log and |
| 2809 |
* false is returned. |
| 2810 |
*/ |
| 2811 |
static bool |
| 2812 |
assign_attribute_or_color_locations(void *mem_ctx, |
| 2813 |
gl_shader_program *prog, |
| 2814 |
struct gl_constants *constants, |
| 2815 |
unsigned target_index, |
| 2816 |
bool do_assignment) |
| 2817 |
{ |
| 2818 |
/* Maximum number of generic locations. This corresponds to either the |
| 2819 |
* maximum number of draw buffers or the maximum number of generic |
| 2820 |
* attributes. |
| 2821 |
*/ |
| 2822 |
unsigned max_index = (target_index == MESA_SHADER_VERTEX) ? |
| 2823 |
constants->Program[target_index].MaxAttribs : |
| 2824 |
MAX2(constants->MaxDrawBuffers, constants->MaxDualSourceDrawBuffers); |
| 2825 |
|
| 2826 |
/* Mark invalid locations as being used. |
| 2827 |
*/ |
| 2828 |
unsigned used_locations = ~SAFE_MASK_FROM_INDEX(max_index); |
| 2829 |
unsigned double_storage_locations = 0; |
| 2830 |
|
| 2831 |
assert((target_index == MESA_SHADER_VERTEX) |
| 2832 |
|| (target_index == MESA_SHADER_FRAGMENT)); |
| 2833 |
|
| 2834 |
gl_linked_shader *const sh = prog->_LinkedShaders[target_index]; |
| 2835 |
if (sh == NULL) |
| 2836 |
return true; |
| 2837 |
|
| 2838 |
/* Operate in a total of four passes. |
| 2839 |
* |
| 2840 |
* 1. Invalidate the location assignments for all vertex shader inputs. |
| 2841 |
* |
| 2842 |
* 2. Assign locations for inputs that have user-defined (via |
| 2843 |
* glBindVertexAttribLocation) locations and outputs that have |
| 2844 |
* user-defined locations (via glBindFragDataLocation). |
| 2845 |
* |
| 2846 |
* 3. Sort the attributes without assigned locations by number of slots |
| 2847 |
* required in decreasing order. Fragmentation caused by attribute |
| 2848 |
* locations assigned by the application may prevent large attributes |
| 2849 |
* from having enough contiguous space. |
| 2850 |
* |
| 2851 |
* 4. Assign locations to any inputs without assigned locations. |
| 2852 |
*/ |
| 2853 |
|
| 2854 |
const int generic_base = (target_index == MESA_SHADER_VERTEX) |
| 2855 |
? (int) VERT_ATTRIB_GENERIC0 : (int) FRAG_RESULT_DATA0; |
| 2856 |
|
| 2857 |
const enum ir_variable_mode direction = |
| 2858 |
(target_index == MESA_SHADER_VERTEX) |
| 2859 |
? ir_var_shader_in : ir_var_shader_out; |
| 2860 |
|
| 2861 |
|
| 2862 |
/* Temporary storage for the set of attributes that need locations assigned. |
| 2863 |
*/ |
| 2864 |
struct temp_attr { |
| 2865 |
unsigned slots; |
| 2866 |
ir_variable *var; |
| 2867 |
|
| 2868 |
/* Used below in the call to qsort. */ |
| 2869 |
static int compare(const void *a, const void *b) |
| 2870 |
{ |
| 2871 |
const temp_attr *const l = (const temp_attr *) a; |
| 2872 |
const temp_attr *const r = (const temp_attr *) b; |
| 2873 |
|
| 2874 |
/* Reversed because we want a descending order sort below. */ |
| 2875 |
return r->slots - l->slots; |
| 2876 |
} |
| 2877 |
} to_assign[32]; |
| 2878 |
assert(max_index <= 32); |
| 2879 |
|
| 2880 |
/* Temporary array for the set of attributes that have locations assigned, |
| 2881 |
* for the purpose of checking overlapping slots/components of (non-ES) |
| 2882 |
* fragment shader outputs. |
| 2883 |
*/ |
| 2884 |
ir_variable *assigned[12 * 4]; /* (max # of FS outputs) * # components */ |
| 2885 |
unsigned assigned_attr = 0; |
| 2886 |
|
| 2887 |
unsigned num_attr = 0; |
| 2888 |
|
| 2889 |
foreach_in_list(ir_instruction, node, sh->ir) { |
| 2890 |
ir_variable *const var = node->as_variable(); |
| 2891 |
|
| 2892 |
if ((var == NULL) || (var->data.mode != (unsigned) direction)) |
| 2893 |
continue; |
| 2894 |
|
| 2895 |
if (var->data.explicit_location) { |
| 2896 |
var->data.is_unmatched_generic_inout = 0; |
| 2897 |
if ((var->data.location >= (int)(max_index + generic_base)) |
| 2898 |
|| (var->data.location < 0)) { |
| 2899 |
linker_error(prog, |
| 2900 |
"invalid explicit location %d specified for `%s'\n", |
| 2901 |
(var->data.location < 0) |
| 2902 |
? var->data.location |
| 2903 |
: var->data.location - generic_base, |
| 2904 |
var->name); |
| 2905 |
return false; |
| 2906 |
} |
| 2907 |
} else if (target_index == MESA_SHADER_VERTEX) { |
| 2908 |
unsigned binding; |
| 2909 |
|
| 2910 |
if (prog->AttributeBindings->get(binding, var->name)) { |
| 2911 |
assert(binding >= VERT_ATTRIB_GENERIC0); |
| 2912 |
var->data.location = binding; |
| 2913 |
var->data.is_unmatched_generic_inout = 0; |
| 2914 |
} |
| 2915 |
} else if (target_index == MESA_SHADER_FRAGMENT) { |
| 2916 |
unsigned binding; |
| 2917 |
unsigned index; |
| 2918 |
const char *name = var->name; |
| 2919 |
const glsl_type *type = var->type; |
| 2920 |
|
| 2921 |
while (type) { |
| 2922 |
/* Check if there's a binding for the variable name */ |
| 2923 |
if (prog->FragDataBindings->get(binding, name)) { |
| 2924 |
assert(binding >= FRAG_RESULT_DATA0); |
| 2925 |
var->data.location = binding; |
| 2926 |
var->data.is_unmatched_generic_inout = 0; |
| 2927 |
|
| 2928 |
if (prog->FragDataIndexBindings->get(index, name)) { |
| 2929 |
var->data.index = index; |
| 2930 |
} |
| 2931 |
break; |
| 2932 |
} |
| 2933 |
|
| 2934 |
/* If not, but it's an array type, look for name[0] */ |
| 2935 |
if (type->is_array()) { |
| 2936 |
name = ralloc_asprintf(mem_ctx, "%s[0]", name); |
| 2937 |
type = type->fields.array; |
| 2938 |
continue; |
| 2939 |
} |
| 2940 |
|
| 2941 |
break; |
| 2942 |
} |
| 2943 |
} |
| 2944 |
|
| 2945 |
if (strcmp(var->name, "gl_LastFragData") == 0) |
| 2946 |
continue; |
| 2947 |
|
| 2948 |
/* From GL4.5 core spec, section 15.2 (Shader Execution): |
| 2949 |
* |
| 2950 |
* "Output binding assignments will cause LinkProgram to fail: |
| 2951 |
* ... |
| 2952 |
* If the program has an active output assigned to a location greater |
| 2953 |
* than or equal to the value of MAX_DUAL_SOURCE_DRAW_BUFFERS and has |
| 2954 |
* an active output assigned an index greater than or equal to one;" |
| 2955 |
*/ |
| 2956 |
if (target_index == MESA_SHADER_FRAGMENT && var->data.index >= 1 && |
| 2957 |
var->data.location - generic_base >= |
| 2958 |
(int) constants->MaxDualSourceDrawBuffers) { |
| 2959 |
linker_error(prog, |
| 2960 |
"output location %d >= GL_MAX_DUAL_SOURCE_DRAW_BUFFERS " |
| 2961 |
"with index %u for %s\n", |
| 2962 |
var->data.location - generic_base, var->data.index, |
| 2963 |
var->name); |
| 2964 |
return false; |
| 2965 |
} |
| 2966 |
|
| 2967 |
const unsigned slots = var->type->count_attribute_slots(target_index == MESA_SHADER_VERTEX); |
| 2968 |
|
| 2969 |
/* If the variable is not a built-in and has a location statically |
| 2970 |
* assigned in the shader (presumably via a layout qualifier), make sure |
| 2971 |
* that it doesn't collide with other assigned locations. Otherwise, |
| 2972 |
* add it to the list of variables that need linker-assigned locations. |
| 2973 |
*/ |
| 2974 |
if (var->data.location != -1) { |
| 2975 |
if (var->data.location >= generic_base && var->data.index < 1) { |
| 2976 |
/* From page 61 of the OpenGL 4.0 spec: |
| 2977 |
* |
| 2978 |
* "LinkProgram will fail if the attribute bindings assigned |
| 2979 |
* by BindAttribLocation do not leave not enough space to |
| 2980 |
* assign a location for an active matrix attribute or an |
| 2981 |
* active attribute array, both of which require multiple |
| 2982 |
* contiguous generic attributes." |
| 2983 |
* |
| 2984 |
* I think above text prohibits the aliasing of explicit and |
| 2985 |
* automatic assignments. But, aliasing is allowed in manual |
| 2986 |
* assignments of attribute locations. See below comments for |
| 2987 |
* the details. |
| 2988 |
* |
| 2989 |
* From OpenGL 4.0 spec, page 61: |
| 2990 |
* |
| 2991 |
* "It is possible for an application to bind more than one |
| 2992 |
* attribute name to the same location. This is referred to as |
| 2993 |
* aliasing. This will only work if only one of the aliased |
| 2994 |
* attributes is active in the executable program, or if no |
| 2995 |
* path through the shader consumes more than one attribute of |
| 2996 |
* a set of attributes aliased to the same location. A link |
| 2997 |
* error can occur if the linker determines that every path |
| 2998 |
* through the shader consumes multiple aliased attributes, |
| 2999 |
* but implementations are not required to generate an error |
| 3000 |
* in this case." |
| 3001 |
* |
| 3002 |
* From GLSL 4.30 spec, page 54: |
| 3003 |
* |
| 3004 |
* "A program will fail to link if any two non-vertex shader |
| 3005 |
* input variables are assigned to the same location. For |
| 3006 |
* vertex shaders, multiple input variables may be assigned |
| 3007 |
* to the same location using either layout qualifiers or via |
| 3008 |
* the OpenGL API. However, such aliasing is intended only to |
| 3009 |
* support vertex shaders where each execution path accesses |
| 3010 |
* at most one input per each location. Implementations are |
| 3011 |
* permitted, but not required, to generate link-time errors |
| 3012 |
* if they detect that every path through the vertex shader |
| 3013 |
* executable accesses multiple inputs assigned to any single |
| 3014 |
* location. For all shader types, a program will fail to link |
| 3015 |
* if explicit location assignments leave the linker unable |
| 3016 |
* to find space for other variables without explicit |
| 3017 |
* assignments." |
| 3018 |
* |
| 3019 |
* From OpenGL ES 3.0 spec, page 56: |
| 3020 |
* |
| 3021 |
* "Binding more than one attribute name to the same location |
| 3022 |
* is referred to as aliasing, and is not permitted in OpenGL |
| 3023 |
* ES Shading Language 3.00 vertex shaders. LinkProgram will |
| 3024 |
* fail when this condition exists. However, aliasing is |
| 3025 |
* possible in OpenGL ES Shading Language 1.00 vertex shaders. |
| 3026 |
* This will only work if only one of the aliased attributes |
| 3027 |
* is active in the executable program, or if no path through |
| 3028 |
* the shader consumes more than one attribute of a set of |
| 3029 |
* attributes aliased to the same location. A link error can |
| 3030 |
* occur if the linker determines that every path through the |
| 3031 |
* shader consumes multiple aliased attributes, but implemen- |
| 3032 |
* tations are not required to generate an error in this case." |
| 3033 |
* |
| 3034 |
* After looking at above references from OpenGL, OpenGL ES and |
| 3035 |
* GLSL specifications, we allow aliasing of vertex input variables |
| 3036 |
* in: OpenGL 2.0 (and above) and OpenGL ES 2.0. |
| 3037 |
* |
| 3038 |
* NOTE: This is not required by the spec but its worth mentioning |
| 3039 |
* here that we're not doing anything to make sure that no path |
| 3040 |
* through the vertex shader executable accesses multiple inputs |
| 3041 |
* assigned to any single location. |
| 3042 |
*/ |
| 3043 |
|
| 3044 |
/* Mask representing the contiguous slots that will be used by |
| 3045 |
* this attribute. |
| 3046 |
*/ |
| 3047 |
const unsigned attr = var->data.location - generic_base; |
| 3048 |
const unsigned use_mask = (1 << slots) - 1; |
| 3049 |
const char *const string = (target_index == MESA_SHADER_VERTEX) |
| 3050 |
? "vertex shader input" : "fragment shader output"; |
| 3051 |
|
| 3052 |
/* Generate a link error if the requested locations for this |
| 3053 |
* attribute exceed the maximum allowed attribute location. |
| 3054 |
*/ |
| 3055 |
if (attr + slots > max_index) { |
| 3056 |
linker_error(prog, |
| 3057 |
"insufficient contiguous locations " |
| 3058 |
"available for %s `%s' %d %d %d\n", string, |
| 3059 |
var->name, used_locations, use_mask, attr); |
| 3060 |
return false; |
| 3061 |
} |
| 3062 |
|
| 3063 |
/* Generate a link error if the set of bits requested for this |
| 3064 |
* attribute overlaps any previously allocated bits. |
| 3065 |
*/ |
| 3066 |
if ((~(use_mask << attr) & used_locations) != used_locations) { |
| 3067 |
if (target_index == MESA_SHADER_FRAGMENT && !prog->IsES) { |
| 3068 |
/* From section 4.4.2 (Output Layout Qualifiers) of the GLSL |
| 3069 |
* 4.40 spec: |
| 3070 |
* |
| 3071 |
* "Additionally, for fragment shader outputs, if two |
| 3072 |
* variables are placed within the same location, they |
| 3073 |
* must have the same underlying type (floating-point or |
| 3074 |
* integer). No component aliasing of output variables or |
| 3075 |
* members is allowed. |
| 3076 |
*/ |
| 3077 |
for (unsigned i = 0; i < assigned_attr; i++) { |
| 3078 |
unsigned assigned_slots = |
| 3079 |
assigned[i]->type->count_attribute_slots(false); |
| 3080 |
unsigned assig_attr = |
| 3081 |
assigned[i]->data.location - generic_base; |
| 3082 |
unsigned assigned_use_mask = (1 << assigned_slots) - 1; |
| 3083 |
|
| 3084 |
if ((assigned_use_mask << assig_attr) & |
| 3085 |
(use_mask << attr)) { |
| 3086 |
|
| 3087 |
const glsl_type *assigned_type = |
| 3088 |
assigned[i]->type->without_array(); |
| 3089 |
const glsl_type *type = var->type->without_array(); |
| 3090 |
if (assigned_type->base_type != type->base_type) { |
| 3091 |
linker_error(prog, "types do not match for aliased" |
| 3092 |
" %ss %s and %s\n", string, |
| 3093 |
assigned[i]->name, var->name); |
| 3094 |
return false; |
| 3095 |
} |
| 3096 |
|
| 3097 |
unsigned assigned_component_mask = |
| 3098 |
((1 << assigned_type->vector_elements) - 1) << |
| 3099 |
assigned[i]->data.location_frac; |
| 3100 |
unsigned component_mask = |
| 3101 |
((1 << type->vector_elements) - 1) << |
| 3102 |
var->data.location_frac; |
| 3103 |
if (assigned_component_mask & component_mask) { |
| 3104 |
linker_error(prog, "overlapping component is " |
| 3105 |
"assigned to %ss %s and %s " |
| 3106 |
"(component=%d)\n", |
| 3107 |
string, assigned[i]->name, var->name, |
| 3108 |
var->data.location_frac); |
| 3109 |
return false; |
| 3110 |
} |
| 3111 |
} |
| 3112 |
} |
| 3113 |
} else if (target_index == MESA_SHADER_FRAGMENT || |
| 3114 |
(prog->IsES && prog->data->Version >= 300)) { |
| 3115 |
linker_error(prog, "overlapping location is assigned " |
| 3116 |
"to %s `%s' %d %d %d\n", string, var->name, |
| 3117 |
used_locations, use_mask, attr); |
| 3118 |
return false; |
| 3119 |
} else { |
| 3120 |
linker_warning(prog, "overlapping location is assigned " |
| 3121 |
"to %s `%s' %d %d %d\n", string, var->name, |
| 3122 |
used_locations, use_mask, attr); |
| 3123 |
} |
| 3124 |
} |
| 3125 |
|
| 3126 |
if (target_index == MESA_SHADER_FRAGMENT && !prog->IsES) { |
| 3127 |
/* Only track assigned variables for non-ES fragment shaders |
| 3128 |
* to avoid overflowing the array. |
| 3129 |
* |
| 3130 |
* At most one variable per fragment output component should |
| 3131 |
* reach this. |
| 3132 |
*/ |
| 3133 |
assert(assigned_attr < ARRAY_SIZE(assigned)); |
| 3134 |
assigned[assigned_attr] = var; |
| 3135 |
assigned_attr++; |
| 3136 |
} |
| 3137 |
|
| 3138 |
used_locations |= (use_mask << attr); |
| 3139 |
|
| 3140 |
/* From the GL 4.5 core spec, section 11.1.1 (Vertex Attributes): |
| 3141 |
* |
| 3142 |
* "A program with more than the value of MAX_VERTEX_ATTRIBS |
| 3143 |
* active attribute variables may fail to link, unless |
| 3144 |
* device-dependent optimizations are able to make the program |
| 3145 |
* fit within available hardware resources. For the purposes |
| 3146 |
* of this test, attribute variables of the type dvec3, dvec4, |
| 3147 |
* dmat2x3, dmat2x4, dmat3, dmat3x4, dmat4x3, and dmat4 may |
| 3148 |
* count as consuming twice as many attributes as equivalent |
| 3149 |
* single-precision types. While these types use the same number |
| 3150 |
* of generic attributes as their single-precision equivalents, |
| 3151 |
* implementations are permitted to consume two single-precision |
| 3152 |
* vectors of internal storage for each three- or four-component |
| 3153 |
* double-precision vector." |
| 3154 |
* |
| 3155 |
* Mark this attribute slot as taking up twice as much space |
| 3156 |
* so we can count it properly against limits. According to |
| 3157 |
* issue (3) of the GL_ARB_vertex_attrib_64bit behavior, this |
| 3158 |
* is optional behavior, but it seems preferable. |
| 3159 |
*/ |
| 3160 |
if (var->type->without_array()->is_dual_slot()) |
| 3161 |
double_storage_locations |= (use_mask << attr); |
| 3162 |
} |
| 3163 |
|
| 3164 |
continue; |
| 3165 |
} |
| 3166 |
|
| 3167 |
if (num_attr >= max_index) { |
| 3168 |
linker_error(prog, "too many %s (max %u)", |
| 3169 |
target_index == MESA_SHADER_VERTEX ? |
| 3170 |
"vertex shader inputs" : "fragment shader outputs", |
| 3171 |
max_index); |
| 3172 |
return false; |
| 3173 |
} |
| 3174 |
to_assign[num_attr].slots = slots; |
| 3175 |
to_assign[num_attr].var = var; |
| 3176 |
num_attr++; |
| 3177 |
} |
| 3178 |
|
| 3179 |
if (!do_assignment) |
| 3180 |
return true; |
| 3181 |
|
| 3182 |
if (target_index == MESA_SHADER_VERTEX) { |
| 3183 |
unsigned total_attribs_size = |
| 3184 |
util_bitcount(used_locations & SAFE_MASK_FROM_INDEX(max_index)) + |
| 3185 |
util_bitcount(double_storage_locations); |
| 3186 |
if (total_attribs_size > max_index) { |
| 3187 |
linker_error(prog, |
| 3188 |
"attempt to use %d vertex attribute slots only %d available ", |
| 3189 |
total_attribs_size, max_index); |
| 3190 |
return false; |
| 3191 |
} |
| 3192 |
} |
| 3193 |
|
| 3194 |
/* If all of the attributes were assigned locations by the application (or |
| 3195 |
* are built-in attributes with fixed locations), return early. This should |
| 3196 |
* be the common case. |
| 3197 |
*/ |
| 3198 |
if (num_attr == 0) |
| 3199 |
return true; |
| 3200 |
|
| 3201 |
qsort(to_assign, num_attr, sizeof(to_assign[0]), temp_attr::compare); |
| 3202 |
|
| 3203 |
if (target_index == MESA_SHADER_VERTEX) { |
| 3204 |
/* VERT_ATTRIB_GENERIC0 is a pseudo-alias for VERT_ATTRIB_POS. It can |
| 3205 |
* only be explicitly assigned by via glBindAttribLocation. Mark it as |
| 3206 |
* reserved to prevent it from being automatically allocated below. |
| 3207 |
*/ |
| 3208 |
find_deref_visitor find("gl_Vertex"); |
| 3209 |
find.run(sh->ir); |
| 3210 |
if (find.variable_found()) |
| 3211 |
used_locations |= (1 << 0); |
| 3212 |
} |
| 3213 |
|
| 3214 |
for (unsigned i = 0; i < num_attr; i++) { |
| 3215 |
/* Mask representing the contiguous slots that will be used by this |
| 3216 |
* attribute. |
| 3217 |
*/ |
| 3218 |
const unsigned use_mask = (1 << to_assign[i].slots) - 1; |
| 3219 |
|
| 3220 |
int location = find_available_slots(used_locations, to_assign[i].slots); |
| 3221 |
|
| 3222 |
if (location < 0) { |
| 3223 |
const char *const string = (target_index == MESA_SHADER_VERTEX) |
| 3224 |
? "vertex shader input" : "fragment shader output"; |
| 3225 |
|
| 3226 |
linker_error(prog, |
| 3227 |
"insufficient contiguous locations " |
| 3228 |
"available for %s `%s'\n", |
| 3229 |
string, to_assign[i].var->name); |
| 3230 |
return false; |
| 3231 |
} |
| 3232 |
|
| 3233 |
to_assign[i].var->data.location = generic_base + location; |
| 3234 |
to_assign[i].var->data.is_unmatched_generic_inout = 0; |
| 3235 |
used_locations |= (use_mask << location); |
| 3236 |
|
| 3237 |
if (to_assign[i].var->type->without_array()->is_dual_slot()) |
| 3238 |
double_storage_locations |= (use_mask << location); |
| 3239 |
} |
| 3240 |
|
| 3241 |
/* Now that we have all the locations, from the GL 4.5 core spec, section |
| 3242 |
* 11.1.1 (Vertex Attributes), dvec3, dvec4, dmat2x3, dmat2x4, dmat3, |
| 3243 |
* dmat3x4, dmat4x3, and dmat4 count as consuming twice as many attributes |
| 3244 |
* as equivalent single-precision types. |
| 3245 |
*/ |
| 3246 |
if (target_index == MESA_SHADER_VERTEX) { |
| 3247 |
unsigned total_attribs_size = |
| 3248 |
util_bitcount(used_locations & SAFE_MASK_FROM_INDEX(max_index)) + |
| 3249 |
util_bitcount(double_storage_locations); |
| 3250 |
if (total_attribs_size > max_index) { |
| 3251 |
linker_error(prog, |
| 3252 |
"attempt to use %d vertex attribute slots only %d available ", |
| 3253 |
total_attribs_size, max_index); |
| 3254 |
return false; |
| 3255 |
} |
| 3256 |
} |
| 3257 |
|
| 3258 |
return true; |
| 3259 |
} |
| 3260 |
|
| 3261 |
/** |
| 3262 |
* Match explicit locations of outputs to inputs and deactivate the |
| 3263 |
* unmatch flag if found so we don't optimise them away. |
| 3264 |
*/ |
| 3265 |
static void |
| 3266 |
match_explicit_outputs_to_inputs(gl_linked_shader *producer, |
| 3267 |
gl_linked_shader *consumer) |
| 3268 |
{ |
| 3269 |
glsl_symbol_table parameters; |
| 3270 |
ir_variable *explicit_locations[MAX_VARYINGS_INCL_PATCH][4] = |
| 3271 |
{ {NULL, NULL} }; |
| 3272 |
|
| 3273 |
/* Find all shader outputs in the "producer" stage. |
| 3274 |
*/ |
| 3275 |
foreach_in_list(ir_instruction, node, producer->ir) { |
| 3276 |
ir_variable *const var = node->as_variable(); |
| 3277 |
|
| 3278 |
if ((var == NULL) || (var->data.mode != ir_var_shader_out)) |
| 3279 |
continue; |
| 3280 |
|
| 3281 |
if (var->data.explicit_location && |
| 3282 |
var->data.location >= VARYING_SLOT_VAR0) { |
| 3283 |
const unsigned idx = var->data.location - VARYING_SLOT_VAR0; |
| 3284 |
if (explicit_locations[idx][var->data.location_frac] == NULL) |
| 3285 |
explicit_locations[idx][var->data.location_frac] = var; |
| 3286 |
|
| 3287 |
/* Always match TCS outputs. They are shared by all invocations |
| 3288 |
* within a patch and can be used as shared memory. |
| 3289 |
*/ |
| 3290 |
if (producer->Stage == MESA_SHADER_TESS_CTRL) |
| 3291 |
var->data.is_unmatched_generic_inout = 0; |
| 3292 |
} |
| 3293 |
} |
| 3294 |
|
| 3295 |
/* Match inputs to outputs */ |
| 3296 |
foreach_in_list(ir_instruction, node, consumer->ir) { |
| 3297 |
ir_variable *const input = node->as_variable(); |
| 3298 |
|
| 3299 |
if ((input == NULL) || (input->data.mode != ir_var_shader_in)) |
| 3300 |
continue; |
| 3301 |
|
| 3302 |
ir_variable *output = NULL; |
| 3303 |
if (input->data.explicit_location |
| 3304 |
&& input->data.location >= VARYING_SLOT_VAR0) { |
| 3305 |
output = explicit_locations[input->data.location - VARYING_SLOT_VAR0] |
| 3306 |
[input->data.location_frac]; |
| 3307 |
|
| 3308 |
if (output != NULL){ |
| 3309 |
input->data.is_unmatched_generic_inout = 0; |
| 3310 |
output->data.is_unmatched_generic_inout = 0; |
| 3311 |
} |
| 3312 |
} |
| 3313 |
} |
| 3314 |
} |
| 3315 |
|
| 3316 |
/** |
| 3317 |
* Store the gl_FragDepth layout in the gl_shader_program struct. |
| 3318 |
*/ |
| 3319 |
static void |
| 3320 |
store_fragdepth_layout(struct gl_shader_program *prog) |
| 3321 |
{ |
| 3322 |
if (prog->_LinkedShaders[MESA_SHADER_FRAGMENT] == NULL) { |
| 3323 |
return; |
| 3324 |
} |
| 3325 |
|
| 3326 |
struct exec_list *ir = prog->_LinkedShaders[MESA_SHADER_FRAGMENT]->ir; |
| 3327 |
|
| 3328 |
/* We don't look up the gl_FragDepth symbol directly because if |
| 3329 |
* gl_FragDepth is not used in the shader, it's removed from the IR. |
| 3330 |
* However, the symbol won't be removed from the symbol table. |
| 3331 |
* |
| 3332 |
* We're only interested in the cases where the variable is NOT removed |
| 3333 |
* from the IR. |
| 3334 |
*/ |
| 3335 |
foreach_in_list(ir_instruction, node, ir) { |
| 3336 |
ir_variable *const var = node->as_variable(); |
| 3337 |
|
| 3338 |
if (var == NULL || var->data.mode != ir_var_shader_out) { |
| 3339 |
continue; |
| 3340 |
} |
| 3341 |
|
| 3342 |
if (strcmp(var->name, "gl_FragDepth") == 0) { |
| 3343 |
switch (var->data.depth_layout) { |
| 3344 |
case ir_depth_layout_none: |
| 3345 |
prog->FragDepthLayout = FRAG_DEPTH_LAYOUT_NONE; |
| 3346 |
return; |
| 3347 |
case ir_depth_layout_any: |
| 3348 |
prog->FragDepthLayout = FRAG_DEPTH_LAYOUT_ANY; |
| 3349 |
return; |
| 3350 |
case ir_depth_layout_greater: |
| 3351 |
prog->FragDepthLayout = FRAG_DEPTH_LAYOUT_GREATER; |
| 3352 |
return; |
| 3353 |
case ir_depth_layout_less: |
| 3354 |
prog->FragDepthLayout = FRAG_DEPTH_LAYOUT_LESS; |
| 3355 |
return; |
| 3356 |
case ir_depth_layout_unchanged: |
| 3357 |
prog->FragDepthLayout = FRAG_DEPTH_LAYOUT_UNCHANGED; |
| 3358 |
return; |
| 3359 |
default: |
| 3360 |
assert(0); |
| 3361 |
return; |
| 3362 |
} |
| 3363 |
} |
| 3364 |
} |
| 3365 |
} |
| 3366 |
|
| 3367 |
/** |
| 3368 |
* Validate shader image resources. |
| 3369 |
*/ |
| 3370 |
static void |
| 3371 |
check_image_resources(struct gl_context *ctx, struct gl_shader_program *prog) |
| 3372 |
{ |
| 3373 |
unsigned total_image_units = 0; |
| 3374 |
unsigned fragment_outputs = 0; |
| 3375 |
unsigned total_shader_storage_blocks = 0; |
| 3376 |
|
| 3377 |
if (!ctx->Extensions.ARB_shader_image_load_store) |
| 3378 |
return; |
| 3379 |
|
| 3380 |
for (unsigned i = 0; i < MESA_SHADER_STAGES; i++) { |
| 3381 |
struct gl_linked_shader *sh = prog->_LinkedShaders[i]; |
| 3382 |
|
| 3383 |
if (sh) { |
| 3384 |
total_image_units += sh->Program->info.num_images; |
| 3385 |
total_shader_storage_blocks += sh->Program->info.num_ssbos; |
| 3386 |
|
| 3387 |
if (i == MESA_SHADER_FRAGMENT) { |
| 3388 |
foreach_in_list(ir_instruction, node, sh->ir) { |
| 3389 |
ir_variable *var = node->as_variable(); |
| 3390 |
if (var && var->data.mode == ir_var_shader_out) |
| 3391 |
/* since there are no double fs outputs - pass false */ |
| 3392 |
fragment_outputs += var->type->count_attribute_slots(false); |
| 3393 |
} |
| 3394 |
} |
| 3395 |
} |
| 3396 |
} |
| 3397 |
|
| 3398 |
if (total_image_units > ctx->Const.MaxCombinedImageUniforms) |
| 3399 |
linker_error(prog, "Too many combined image uniforms\n"); |
| 3400 |
|
| 3401 |
if (total_image_units + fragment_outputs + total_shader_storage_blocks > |
| 3402 |
ctx->Const.MaxCombinedShaderOutputResources) |
| 3403 |
linker_error(prog, "Too many combined image uniforms, shader storage " |
| 3404 |
" buffers and fragment outputs\n"); |
| 3405 |
} |
| 3406 |
|
| 3407 |
|
| 3408 |
/** |
| 3409 |
* Initializes explicit location slots to INACTIVE_UNIFORM_EXPLICIT_LOCATION |
| 3410 |
* for a variable, checks for overlaps between other uniforms using explicit |
| 3411 |
* locations. |
| 3412 |
*/ |
| 3413 |
static int |
| 3414 |
reserve_explicit_locations(struct gl_shader_program *prog, |
| 3415 |
string_to_uint_map *map, ir_variable *var) |
| 3416 |
{ |
| 3417 |
unsigned slots = var->type->uniform_locations(); |
| 3418 |
unsigned max_loc = var->data.location + slots - 1; |
| 3419 |
unsigned return_value = slots; |
| 3420 |
|
| 3421 |
/* Resize remap table if locations do not fit in the current one. */ |
| 3422 |
if (max_loc + 1 > prog->NumUniformRemapTable) { |
| 3423 |
prog->UniformRemapTable = |
| 3424 |
reralloc(prog, prog->UniformRemapTable, |
| 3425 |
gl_uniform_storage *, |
| 3426 |
max_loc + 1); |
| 3427 |
|
| 3428 |
if (!prog->UniformRemapTable) { |
| 3429 |
linker_error(prog, "Out of memory during linking.\n"); |
| 3430 |
return -1; |
| 3431 |
} |
| 3432 |
|
| 3433 |
/* Initialize allocated space. */ |
| 3434 |
for (unsigned i = prog->NumUniformRemapTable; i < max_loc + 1; i++) |
| 3435 |
prog->UniformRemapTable[i] = NULL; |
| 3436 |
|
| 3437 |
prog->NumUniformRemapTable = max_loc + 1; |
| 3438 |
} |
| 3439 |
|
| 3440 |
for (unsigned i = 0; i < slots; i++) { |
| 3441 |
unsigned loc = var->data.location + i; |
| 3442 |
|
| 3443 |
/* Check if location is already used. */ |
| 3444 |
if (prog->UniformRemapTable[loc] == INACTIVE_UNIFORM_EXPLICIT_LOCATION) { |
| 3445 |
|
| 3446 |
/* Possibly same uniform from a different stage, this is ok. */ |
| 3447 |
unsigned hash_loc; |
| 3448 |
if (map->get(hash_loc, var->name) && hash_loc == loc - i) { |
| 3449 |
return_value = 0; |
| 3450 |
continue; |
| 3451 |
} |
| 3452 |
|
| 3453 |
/* ARB_explicit_uniform_location specification states: |
| 3454 |
* |
| 3455 |
* "No two default-block uniform variables in the program can have |
| 3456 |
* the same location, even if they are unused, otherwise a compiler |
| 3457 |
* or linker error will be generated." |
| 3458 |
*/ |
| 3459 |
linker_error(prog, |
| 3460 |
"location qualifier for uniform %s overlaps " |
| 3461 |
"previously used location\n", |
| 3462 |
var->name); |
| 3463 |
return -1; |
| 3464 |
} |
| 3465 |
|
| 3466 |
/* Initialize location as inactive before optimization |
| 3467 |
* rounds and location assignment. |
| 3468 |
*/ |
| 3469 |
prog->UniformRemapTable[loc] = INACTIVE_UNIFORM_EXPLICIT_LOCATION; |
| 3470 |
} |
| 3471 |
|
| 3472 |
/* Note, base location used for arrays. */ |
| 3473 |
map->put(var->data.location, var->name); |
| 3474 |
|
| 3475 |
return return_value; |
| 3476 |
} |
| 3477 |
|
| 3478 |
static bool |
| 3479 |
reserve_subroutine_explicit_locations(struct gl_shader_program *prog, |
| 3480 |
struct gl_program *p, |
| 3481 |
ir_variable *var) |
| 3482 |
{ |
| 3483 |
unsigned slots = var->type->uniform_locations(); |
| 3484 |
unsigned max_loc = var->data.location + slots - 1; |
| 3485 |
|
| 3486 |
/* Resize remap table if locations do not fit in the current one. */ |
| 3487 |
if (max_loc + 1 > p->sh.NumSubroutineUniformRemapTable) { |
| 3488 |
p->sh.SubroutineUniformRemapTable = |
| 3489 |
reralloc(p, p->sh.SubroutineUniformRemapTable, |
| 3490 |
gl_uniform_storage *, |
| 3491 |
max_loc + 1); |
| 3492 |
|
| 3493 |
if (!p->sh.SubroutineUniformRemapTable) { |
| 3494 |
linker_error(prog, "Out of memory during linking.\n"); |
| 3495 |
return false; |
| 3496 |
} |
| 3497 |
|
| 3498 |
/* Initialize allocated space. */ |
| 3499 |
for (unsigned i = p->sh.NumSubroutineUniformRemapTable; i < max_loc + 1; i++) |
| 3500 |
p->sh.SubroutineUniformRemapTable[i] = NULL; |
| 3501 |
|
| 3502 |
p->sh.NumSubroutineUniformRemapTable = max_loc + 1; |
| 3503 |
} |
| 3504 |
|
| 3505 |
for (unsigned i = 0; i < slots; i++) { |
| 3506 |
unsigned loc = var->data.location + i; |
| 3507 |
|
| 3508 |
/* Check if location is already used. */ |
| 3509 |
if (p->sh.SubroutineUniformRemapTable[loc] == INACTIVE_UNIFORM_EXPLICIT_LOCATION) { |
| 3510 |
|
| 3511 |
/* ARB_explicit_uniform_location specification states: |
| 3512 |
* "No two subroutine uniform variables can have the same location |
| 3513 |
* in the same shader stage, otherwise a compiler or linker error |
| 3514 |
* will be generated." |
| 3515 |
*/ |
| 3516 |
linker_error(prog, |
| 3517 |
"location qualifier for uniform %s overlaps " |
| 3518 |
"previously used location\n", |
| 3519 |
var->name); |
| 3520 |
return false; |
| 3521 |
} |
| 3522 |
|
| 3523 |
/* Initialize location as inactive before optimization |
| 3524 |
* rounds and location assignment. |
| 3525 |
*/ |
| 3526 |
p->sh.SubroutineUniformRemapTable[loc] = INACTIVE_UNIFORM_EXPLICIT_LOCATION; |
| 3527 |
} |
| 3528 |
|
| 3529 |
return true; |
| 3530 |
} |
| 3531 |
/** |
| 3532 |
* Check and reserve all explicit uniform locations, called before |
| 3533 |
* any optimizations happen to handle also inactive uniforms and |
| 3534 |
* inactive array elements that may get trimmed away. |
| 3535 |
*/ |
| 3536 |
static void |
| 3537 |
check_explicit_uniform_locations(struct gl_context *ctx, |
| 3538 |
struct gl_shader_program *prog) |
| 3539 |
{ |
| 3540 |
prog->NumExplicitUniformLocations = 0; |
| 3541 |
|
| 3542 |
if (!ctx->Extensions.ARB_explicit_uniform_location) |
| 3543 |
return; |
| 3544 |
|
| 3545 |
/* This map is used to detect if overlapping explicit locations |
| 3546 |
* occur with the same uniform (from different stage) or a different one. |
| 3547 |
*/ |
| 3548 |
string_to_uint_map *uniform_map = new string_to_uint_map; |
| 3549 |
|
| 3550 |
if (!uniform_map) { |
| 3551 |
linker_error(prog, "Out of memory during linking.\n"); |
| 3552 |
return; |
| 3553 |
} |
| 3554 |
|
| 3555 |
unsigned entries_total = 0; |
| 3556 |
unsigned mask = prog->data->linked_stages; |
| 3557 |
while (mask) { |
| 3558 |
const int i = u_bit_scan(&mask); |
| 3559 |
struct gl_program *p = prog->_LinkedShaders[i]->Program; |
| 3560 |
|
| 3561 |
foreach_in_list(ir_instruction, node, prog->_LinkedShaders[i]->ir) { |
| 3562 |
ir_variable *var = node->as_variable(); |
| 3563 |
if (!var || var->data.mode != ir_var_uniform) |
| 3564 |
continue; |
| 3565 |
|
| 3566 |
if (var->data.explicit_location) { |
| 3567 |
bool ret = false; |
| 3568 |
if (var->type->without_array()->is_subroutine()) |
| 3569 |
ret = reserve_subroutine_explicit_locations(prog, p, var); |
| 3570 |
else { |
| 3571 |
int slots = reserve_explicit_locations(prog, uniform_map, |
| 3572 |
var); |
| 3573 |
if (slots != -1) { |
| 3574 |
ret = true; |
| 3575 |
entries_total += slots; |
| 3576 |
} |
| 3577 |
} |
| 3578 |
if (!ret) { |
| 3579 |
delete uniform_map; |
| 3580 |
return; |
| 3581 |
} |
| 3582 |
} |
| 3583 |
} |
| 3584 |
} |
| 3585 |
|
| 3586 |
link_util_update_empty_uniform_locations(prog); |
| 3587 |
|
| 3588 |
delete uniform_map; |
| 3589 |
prog->NumExplicitUniformLocations = entries_total; |
| 3590 |
} |
| 3591 |
|
| 3592 |
/* Function checks if a variable var is a packed varying and |
| 3593 |
* if given name is part of packed varying's list. |
| 3594 |
* |
| 3595 |
* If a variable is a packed varying, it has a name like |
| 3596 |
* 'packed:a,b,c' where a, b and c are separate variables. |
| 3597 |
*/ |
| 3598 |
static bool |
| 3599 |
included_in_packed_varying(ir_variable *var, const char *name) |
| 3600 |
{ |
| 3601 |
if (strncmp(var->name, "packed:", 7) != 0) |
| 3602 |
return false; |
| 3603 |
|
| 3604 |
char *list = strdup(var->name + 7); |
| 3605 |
assert(list); |
| 3606 |
|
| 3607 |
bool found = false; |
| 3608 |
char *saveptr; |
| 3609 |
char *token = strtok_r(list, ",", &saveptr); |
| 3610 |
while (token) { |
| 3611 |
if (strcmp(token, name) == 0) { |
| 3612 |
found = true; |
| 3613 |
break; |
| 3614 |
} |
| 3615 |
token = strtok_r(NULL, ",", &saveptr); |
| 3616 |
} |
| 3617 |
free(list); |
| 3618 |
return found; |
| 3619 |
} |
| 3620 |
|
| 3621 |
/** |
| 3622 |
* Function builds a stage reference bitmask from variable name. |
| 3623 |
*/ |
| 3624 |
static uint8_t |
| 3625 |
build_stageref(struct gl_shader_program *shProg, const char *name, |
| 3626 |
unsigned mode) |
| 3627 |
{ |
| 3628 |
uint8_t stages = 0; |
| 3629 |
|
| 3630 |
/* Note, that we assume MAX 8 stages, if there will be more stages, type |
| 3631 |
* used for reference mask in gl_program_resource will need to be changed. |
| 3632 |
*/ |
| 3633 |
assert(MESA_SHADER_STAGES < 8); |
| 3634 |
|
| 3635 |
for (unsigned i = 0; i < MESA_SHADER_STAGES; i++) { |
| 3636 |
struct gl_linked_shader *sh = shProg->_LinkedShaders[i]; |
| 3637 |
if (!sh) |
| 3638 |
continue; |
| 3639 |
|
| 3640 |
/* Shader symbol table may contain variables that have |
| 3641 |
* been optimized away. Search IR for the variable instead. |
| 3642 |
*/ |
| 3643 |
foreach_in_list(ir_instruction, node, sh->ir) { |
| 3644 |
ir_variable *var = node->as_variable(); |
| 3645 |
if (var) { |
| 3646 |
unsigned baselen = strlen(var->name); |
| 3647 |
|
| 3648 |
if (included_in_packed_varying(var, name)) { |
| 3649 |
stages |= (1 << i); |
| 3650 |
break; |
| 3651 |
} |
| 3652 |
|
| 3653 |
/* Type needs to match if specified, otherwise we might |
| 3654 |
* pick a variable with same name but different interface. |
| 3655 |
*/ |
| 3656 |
if (var->data.mode != mode) |
| 3657 |
continue; |
| 3658 |
|
| 3659 |
if (strncmp(var->name, name, baselen) == 0) { |
| 3660 |
/* Check for exact name matches but also check for arrays and |
| 3661 |
* structs. |
| 3662 |
*/ |
| 3663 |
if (name[baselen] == '\0' || |
| 3664 |
name[baselen] == '[' || |
| 3665 |
name[baselen] == '.') { |
| 3666 |
stages |= (1 << i); |
| 3667 |
break; |
| 3668 |
} |
| 3669 |
} |
| 3670 |
} |
| 3671 |
} |
| 3672 |
} |
| 3673 |
return stages; |
| 3674 |
} |
| 3675 |
|
| 3676 |
/** |
| 3677 |
* Create gl_shader_variable from ir_variable class. |
| 3678 |
*/ |
| 3679 |
static gl_shader_variable * |
| 3680 |
create_shader_variable(struct gl_shader_program *shProg, |
| 3681 |
const ir_variable *in, |
| 3682 |
const char *name, const glsl_type *type, |
| 3683 |
const glsl_type *interface_type, |
| 3684 |
bool use_implicit_location, int location, |
| 3685 |
const glsl_type *outermost_struct_type) |
| 3686 |
{ |
| 3687 |
/* Allocate zero-initialized memory to ensure that bitfield padding |
| 3688 |
* is zero. |
| 3689 |
*/ |
| 3690 |
gl_shader_variable *out = rzalloc(shProg, struct gl_shader_variable); |
| 3691 |
if (!out) |
| 3692 |
return NULL; |
| 3693 |
|
| 3694 |
/* Since gl_VertexID may be lowered to gl_VertexIDMESA, but applications |
| 3695 |
* expect to see gl_VertexID in the program resource list. Pretend. |
| 3696 |
*/ |
| 3697 |
if (in->data.mode == ir_var_system_value && |
| 3698 |
in->data.location == SYSTEM_VALUE_VERTEX_ID_ZERO_BASE) { |
| 3699 |
out->name = ralloc_strdup(shProg, "gl_VertexID"); |
| 3700 |
} else if ((in->data.mode == ir_var_shader_out && |
| 3701 |
in->data.location == VARYING_SLOT_TESS_LEVEL_OUTER) || |
| 3702 |
(in->data.mode == ir_var_system_value && |
| 3703 |
in->data.location == SYSTEM_VALUE_TESS_LEVEL_OUTER)) { |
| 3704 |
out->name = ralloc_strdup(shProg, "gl_TessLevelOuter"); |
| 3705 |
type = glsl_type::get_array_instance(glsl_type::float_type, 4); |
| 3706 |
} else if ((in->data.mode == ir_var_shader_out && |
| 3707 |
in->data.location == VARYING_SLOT_TESS_LEVEL_INNER) || |
| 3708 |
(in->data.mode == ir_var_system_value && |
| 3709 |
in->data.location == SYSTEM_VALUE_TESS_LEVEL_INNER)) { |
| 3710 |
out->name = ralloc_strdup(shProg, "gl_TessLevelInner"); |
| 3711 |
type = glsl_type::get_array_instance(glsl_type::float_type, 2); |
| 3712 |
} else { |
| 3713 |
out->name = ralloc_strdup(shProg, name); |
| 3714 |
} |
| 3715 |
|
| 3716 |
if (!out->name) |
| 3717 |
return NULL; |
| 3718 |
|
| 3719 |
/* The ARB_program_interface_query spec says: |
| 3720 |
* |
| 3721 |
* "Not all active variables are assigned valid locations; the |
| 3722 |
* following variables will have an effective location of -1: |
| 3723 |
* |
| 3724 |
* * uniforms declared as atomic counters; |
| 3725 |
* |
| 3726 |
* * members of a uniform block; |
| 3727 |
* |
| 3728 |
* * built-in inputs, outputs, and uniforms (starting with "gl_"); and |
| 3729 |
* |
| 3730 |
* * inputs or outputs not declared with a "location" layout |
| 3731 |
* qualifier, except for vertex shader inputs and fragment shader |
| 3732 |
* outputs." |
| 3733 |
*/ |
| 3734 |
if (in->type->is_atomic_uint() || is_gl_identifier(in->name) || |
| 3735 |
!(in->data.explicit_location || use_implicit_location)) { |
| 3736 |
out->location = -1; |
| 3737 |
} else { |
| 3738 |
out->location = location; |
| 3739 |
} |
| 3740 |
|
| 3741 |
out->type = type; |
| 3742 |
out->outermost_struct_type = outermost_struct_type; |
| 3743 |
out->interface_type = interface_type; |
| 3744 |
out->component = in->data.location_frac; |
| 3745 |
out->index = in->data.index; |
| 3746 |
out->patch = in->data.patch; |
| 3747 |
out->mode = in->data.mode; |
| 3748 |
out->interpolation = in->data.interpolation; |
| 3749 |
out->explicit_location = in->data.explicit_location; |
| 3750 |
out->precision = in->data.precision; |
| 3751 |
|
| 3752 |
return out; |
| 3753 |
} |
| 3754 |
|
| 3755 |
static bool |
| 3756 |
add_shader_variable(const struct gl_context *ctx, |
| 3757 |
struct gl_shader_program *shProg, |
| 3758 |
struct set *resource_set, |
| 3759 |
unsigned stage_mask, |
| 3760 |
GLenum programInterface, ir_variable *var, |
| 3761 |
const char *name, const glsl_type *type, |
| 3762 |
bool use_implicit_location, int location, |
| 3763 |
bool inouts_share_location, |
| 3764 |
const glsl_type *outermost_struct_type = NULL) |
| 3765 |
{ |
| 3766 |
const glsl_type *interface_type = var->get_interface_type(); |
| 3767 |
|
| 3768 |
if (outermost_struct_type == NULL) { |
| 3769 |
if (var->data.from_named_ifc_block) { |
| 3770 |
const char *interface_name = interface_type->name; |
| 3771 |
|
| 3772 |
if (interface_type->is_array()) { |
| 3773 |
/* Issue #16 of the ARB_program_interface_query spec says: |
| 3774 |
* |
| 3775 |
* "* If a variable is a member of an interface block without an |
| 3776 |
* instance name, it is enumerated using just the variable name. |
| 3777 |
* |
| 3778 |
* * If a variable is a member of an interface block with an |
| 3779 |
* instance name, it is enumerated as "BlockName.Member", where |
| 3780 |
* "BlockName" is the name of the interface block (not the |
| 3781 |
* instance name) and "Member" is the name of the variable." |
| 3782 |
* |
| 3783 |
* In particular, it indicates that it should be "BlockName", |
| 3784 |
* not "BlockName[array length]". The conformance suite and |
| 3785 |
* dEQP both require this behavior. |
| 3786 |
* |
| 3787 |
* Here, we unwrap the extra array level added by named interface |
| 3788 |
* block array lowering so we have the correct variable type. We |
| 3789 |
* also unwrap the interface type when constructing the name. |
| 3790 |
* |
| 3791 |
* We leave interface_type the same so that ES 3.x SSO pipeline |
| 3792 |
* validation can enforce the rules requiring array length to |
| 3793 |
* match on interface blocks. |
| 3794 |
*/ |
| 3795 |
type = type->fields.array; |
| 3796 |
|
| 3797 |
interface_name = interface_type->fields.array->name; |
| 3798 |
} |
| 3799 |
|
| 3800 |
name = ralloc_asprintf(shProg, "%s.%s", interface_name, name); |
| 3801 |
} |
| 3802 |
} |
| 3803 |
|
| 3804 |
switch (type->base_type) { |
| 3805 |
case GLSL_TYPE_STRUCT: { |
| 3806 |
/* The ARB_program_interface_query spec says: |
| 3807 |
* |
| 3808 |
* "For an active variable declared as a structure, a separate entry |
| 3809 |
* will be generated for each active structure member. The name of |
| 3810 |
* each entry is formed by concatenating the name of the structure, |
| 3811 |
* the "." character, and the name of the structure member. If a |
| 3812 |
* structure member to enumerate is itself a structure or array, |
| 3813 |
* these enumeration rules are applied recursively." |
| 3814 |
*/ |
| 3815 |
if (outermost_struct_type == NULL) |
| 3816 |
outermost_struct_type = type; |
| 3817 |
|
| 3818 |
unsigned field_location = location; |
| 3819 |
for (unsigned i = 0; i < type->length; i++) { |
| 3820 |
const struct glsl_struct_field *field = &type->fields.structure[i]; |
| 3821 |
char *field_name = ralloc_asprintf(shProg, "%s.%s", name, field->name); |
| 3822 |
if (!add_shader_variable(ctx, shProg, resource_set, |
| 3823 |
stage_mask, programInterface, |
| 3824 |
var, field_name, field->type, |
| 3825 |
use_implicit_location, field_location, |
| 3826 |
false, outermost_struct_type)) |
| 3827 |
return false; |
| 3828 |
|
| 3829 |
field_location += field->type->count_attribute_slots(false); |
| 3830 |
} |
| 3831 |
return true; |
| 3832 |
} |
| 3833 |
|
| 3834 |
case GLSL_TYPE_ARRAY: { |
| 3835 |
/* The ARB_program_interface_query spec says: |
| 3836 |
* |
| 3837 |
* "For an active variable declared as an array of basic types, a |
| 3838 |
* single entry will be generated, with its name string formed by |
| 3839 |
* concatenating the name of the array and the string "[0]"." |
| 3840 |
* |
| 3841 |
* "For an active variable declared as an array of an aggregate data |
| 3842 |
* type (structures or arrays), a separate entry will be generated |
| 3843 |
* for each active array element, unless noted immediately below. |
| 3844 |
* The name of each entry is formed by concatenating the name of |
| 3845 |
* the array, the "[" character, an integer identifying the element |
| 3846 |
* number, and the "]" character. These enumeration rules are |
| 3847 |
* applied recursively, treating each enumerated array element as a |
| 3848 |
* separate active variable." |
| 3849 |
*/ |
| 3850 |
const struct glsl_type *array_type = type->fields.array; |
| 3851 |
if (array_type->base_type == GLSL_TYPE_STRUCT || |
| 3852 |
array_type->base_type == GLSL_TYPE_ARRAY) { |
| 3853 |
unsigned elem_location = location; |
| 3854 |
unsigned stride = inouts_share_location ? 0 : |
| 3855 |
array_type->count_attribute_slots(false); |
| 3856 |
for (unsigned i = 0; i < type->length; i++) { |
| 3857 |
char *elem = ralloc_asprintf(shProg, "%s[%d]", name, i); |
| 3858 |
if (!add_shader_variable(ctx, shProg, resource_set, |
| 3859 |
stage_mask, programInterface, |
| 3860 |
var, elem, array_type, |
| 3861 |
use_implicit_location, elem_location, |
| 3862 |
false, outermost_struct_type)) |
| 3863 |
return false; |
| 3864 |
elem_location += stride; |
| 3865 |
} |
| 3866 |
return true; |
| 3867 |
} |
| 3868 |
/* fallthrough */ |
| 3869 |
} |
| 3870 |
|
| 3871 |
default: { |
| 3872 |
/* The ARB_program_interface_query spec says: |
| 3873 |
* |
| 3874 |
* "For an active variable declared as a single instance of a basic |
| 3875 |
* type, a single entry will be generated, using the variable name |
| 3876 |
* from the shader source." |
| 3877 |
*/ |
| 3878 |
gl_shader_variable *sha_v = |
| 3879 |
create_shader_variable(shProg, var, name, type, interface_type, |
| 3880 |
use_implicit_location, location, |
| 3881 |
outermost_struct_type); |
| 3882 |
if (!sha_v) |
| 3883 |
return false; |
| 3884 |
|
| 3885 |
return link_util_add_program_resource(shProg, resource_set, |
| 3886 |
programInterface, sha_v, stage_mask); |
| 3887 |
} |
| 3888 |
} |
| 3889 |
} |
| 3890 |
|
| 3891 |
static bool |
| 3892 |
inout_has_same_location(const ir_variable *var, unsigned stage) |
| 3893 |
{ |
| 3894 |
if (!var->data.patch && |
| 3895 |
((var->data.mode == ir_var_shader_out && |
| 3896 |
stage == MESA_SHADER_TESS_CTRL) || |
| 3897 |
(var->data.mode == ir_var_shader_in && |
| 3898 |
(stage == MESA_SHADER_TESS_CTRL || stage == MESA_SHADER_TESS_EVAL || |
| 3899 |
stage == MESA_SHADER_GEOMETRY)))) |
| 3900 |
return true; |
| 3901 |
else |
| 3902 |
return false; |
| 3903 |
} |
| 3904 |
|
| 3905 |
static bool |
| 3906 |
add_interface_variables(const struct gl_context *ctx, |
| 3907 |
struct gl_shader_program *shProg, |
| 3908 |
struct set *resource_set, |
| 3909 |
unsigned stage, GLenum programInterface) |
| 3910 |
{ |
| 3911 |
exec_list *ir = shProg->_LinkedShaders[stage]->ir; |
| 3912 |
|
| 3913 |
foreach_in_list(ir_instruction, node, ir) { |
| 3914 |
ir_variable *var = node->as_variable(); |
| 3915 |
|
| 3916 |
if (!var || var->data.how_declared == ir_var_hidden) |
| 3917 |
continue; |
| 3918 |
|
| 3919 |
int loc_bias; |
| 3920 |
|
| 3921 |
switch (var->data.mode) { |
| 3922 |
case ir_var_system_value: |
| 3923 |
case ir_var_shader_in: |
| 3924 |
if (programInterface != GL_PROGRAM_INPUT) |
| 3925 |
continue; |
| 3926 |
loc_bias = (stage == MESA_SHADER_VERTEX) ? int(VERT_ATTRIB_GENERIC0) |
| 3927 |
: int(VARYING_SLOT_VAR0); |
| 3928 |
break; |
| 3929 |
case ir_var_shader_out: |
| 3930 |
if (programInterface != GL_PROGRAM_OUTPUT) |
| 3931 |
continue; |
| 3932 |
loc_bias = (stage == MESA_SHADER_FRAGMENT) ? int(FRAG_RESULT_DATA0) |
| 3933 |
: int(VARYING_SLOT_VAR0); |
| 3934 |
break; |
| 3935 |
default: |
| 3936 |
continue; |
| 3937 |
}; |
| 3938 |
|
| 3939 |
if (var->data.patch) |
| 3940 |
loc_bias = int(VARYING_SLOT_PATCH0); |
| 3941 |
|
| 3942 |
/* Skip packed varyings, packed varyings are handled separately |
| 3943 |
* by add_packed_varyings. |
| 3944 |
*/ |
| 3945 |
if (strncmp(var->name, "packed:", 7) == 0) |
| 3946 |
continue; |
| 3947 |
|
| 3948 |
/* Skip fragdata arrays, these are handled separately |
| 3949 |
* by add_fragdata_arrays. |
| 3950 |
*/ |
| 3951 |
if (strncmp(var->name, "gl_out_FragData", 15) == 0) |
| 3952 |
continue; |
| 3953 |
|
| 3954 |
const bool vs_input_or_fs_output = |
| 3955 |
(stage == MESA_SHADER_VERTEX && var->data.mode == ir_var_shader_in) || |
| 3956 |
(stage == MESA_SHADER_FRAGMENT && var->data.mode == ir_var_shader_out); |
| 3957 |
|
| 3958 |
if (!add_shader_variable(ctx, shProg, resource_set, |
| 3959 |
1 << stage, programInterface, |
| 3960 |
var, var->name, var->type, vs_input_or_fs_output, |
| 3961 |
var->data.location - loc_bias, |
| 3962 |
inout_has_same_location(var, stage))) |
| 3963 |
return false; |
| 3964 |
} |
| 3965 |
return true; |
| 3966 |
} |
| 3967 |
|
| 3968 |
static bool |
| 3969 |
add_packed_varyings(const struct gl_context *ctx, |
| 3970 |
struct gl_shader_program *shProg, |
| 3971 |
struct set *resource_set, |
| 3972 |
int stage, GLenum type) |
| 3973 |
{ |
| 3974 |
struct gl_linked_shader *sh = shProg->_LinkedShaders[stage]; |
| 3975 |
GLenum iface; |
| 3976 |
|
| 3977 |
if (!sh || !sh->packed_varyings) |
| 3978 |
return true; |
| 3979 |
|
| 3980 |
foreach_in_list(ir_instruction, node, sh->packed_varyings) { |
| 3981 |
ir_variable *var = node->as_variable(); |
| 3982 |
if (var) { |
| 3983 |
switch (var->data.mode) { |
| 3984 |
case ir_var_shader_in: |
| 3985 |
iface = GL_PROGRAM_INPUT; |
| 3986 |
break; |
| 3987 |
case ir_var_shader_out: |
| 3988 |
iface = GL_PROGRAM_OUTPUT; |
| 3989 |
break; |
| 3990 |
default: |
| 3991 |
unreachable("unexpected type"); |
| 3992 |
} |
| 3993 |
|
| 3994 |
if (type == iface) { |
| 3995 |
const int stage_mask = |
| 3996 |
build_stageref(shProg, var->name, var->data.mode); |
| 3997 |
if (!add_shader_variable(ctx, shProg, resource_set, |
| 3998 |
stage_mask, |
| 3999 |
iface, var, var->name, var->type, false, |
| 4000 |
var->data.location - VARYING_SLOT_VAR0, |
| 4001 |
inout_has_same_location(var, stage))) |
| 4002 |
return false; |
| 4003 |
} |
| 4004 |
} |
| 4005 |
} |
| 4006 |
return true; |
| 4007 |
} |
| 4008 |
|
| 4009 |
static bool |
| 4010 |
add_fragdata_arrays(const struct gl_context *ctx, |
| 4011 |
struct gl_shader_program *shProg, |
| 4012 |
struct set *resource_set) |
| 4013 |
{ |
| 4014 |
struct gl_linked_shader *sh = shProg->_LinkedShaders[MESA_SHADER_FRAGMENT]; |
| 4015 |
|
| 4016 |
if (!sh || !sh->fragdata_arrays) |
| 4017 |
return true; |
| 4018 |
|
| 4019 |
foreach_in_list(ir_instruction, node, sh->fragdata_arrays) { |
| 4020 |
ir_variable *var = node->as_variable(); |
| 4021 |
if (var) { |
| 4022 |
assert(var->data.mode == ir_var_shader_out); |
| 4023 |
|
| 4024 |
if (!add_shader_variable(ctx, shProg, resource_set, |
| 4025 |
1 << MESA_SHADER_FRAGMENT, |
| 4026 |
GL_PROGRAM_OUTPUT, var, var->name, var->type, |
| 4027 |
true, var->data.location - FRAG_RESULT_DATA0, |
| 4028 |
false)) |
| 4029 |
return false; |
| 4030 |
} |
| 4031 |
} |
| 4032 |
return true; |
| 4033 |
} |
| 4034 |
|
| 4035 |
/** |
| 4036 |
* Builds up a list of program resources that point to existing |
| 4037 |
* resource data. |
| 4038 |
*/ |
| 4039 |
void |
| 4040 |
build_program_resource_list(struct gl_context *ctx, |
| 4041 |
struct gl_shader_program *shProg, |
| 4042 |
bool add_packed_varyings_only) |
| 4043 |
{ |
| 4044 |
/* Rebuild resource list. */ |
| 4045 |
if (shProg->data->ProgramResourceList) { |
| 4046 |
ralloc_free(shProg->data->ProgramResourceList); |
| 4047 |
shProg->data->ProgramResourceList = NULL; |
| 4048 |
shProg->data->NumProgramResourceList = 0; |
| 4049 |
} |
| 4050 |
|
| 4051 |
int input_stage = MESA_SHADER_STAGES, output_stage = 0; |
| 4052 |
|
| 4053 |
/* Determine first input and final output stage. These are used to |
| 4054 |
* detect which variables should be enumerated in the resource list |
| 4055 |
* for GL_PROGRAM_INPUT and GL_PROGRAM_OUTPUT. |
| 4056 |
*/ |
| 4057 |
for (unsigned i = 0; i < MESA_SHADER_STAGES; i++) { |
| 4058 |
if (!shProg->_LinkedShaders[i]) |
| 4059 |
continue; |
| 4060 |
if (input_stage == MESA_SHADER_STAGES) |
| 4061 |
input_stage = i; |
| 4062 |
output_stage = i; |
| 4063 |
} |
| 4064 |
|
| 4065 |
/* Empty shader, no resources. */ |
| 4066 |
if (input_stage == MESA_SHADER_STAGES && output_stage == 0) |
| 4067 |
return; |
| 4068 |
|
| 4069 |
struct set *resource_set = _mesa_pointer_set_create(NULL); |
| 4070 |
|
| 4071 |
/* Program interface needs to expose varyings in case of SSO. */ |
| 4072 |
if (shProg->SeparateShader) { |
| 4073 |
if (!add_packed_varyings(ctx, shProg, resource_set, |
| 4074 |
input_stage, GL_PROGRAM_INPUT)) |
| 4075 |
return; |
| 4076 |
|
| 4077 |
if (!add_packed_varyings(ctx, shProg, resource_set, |
| 4078 |
output_stage, GL_PROGRAM_OUTPUT)) |
| 4079 |
return; |
| 4080 |
} |
| 4081 |
|
| 4082 |
if (add_packed_varyings_only) { |
| 4083 |
_mesa_set_destroy(resource_set, NULL); |
| 4084 |
return; |
| 4085 |
} |
| 4086 |
|
| 4087 |
if (!add_fragdata_arrays(ctx, shProg, resource_set)) |
| 4088 |
return; |
| 4089 |
|
| 4090 |
/* Add inputs and outputs to the resource list. */ |
| 4091 |
if (!add_interface_variables(ctx, shProg, resource_set, |
| 4092 |
input_stage, GL_PROGRAM_INPUT)) |
| 4093 |
return; |
| 4094 |
|
| 4095 |
if (!add_interface_variables(ctx, shProg, resource_set, |
| 4096 |
output_stage, GL_PROGRAM_OUTPUT)) |
| 4097 |
return; |
| 4098 |
|
| 4099 |
if (shProg->last_vert_prog) { |
| 4100 |
struct gl_transform_feedback_info *linked_xfb = |
| 4101 |
shProg->last_vert_prog->sh.LinkedTransformFeedback; |
| 4102 |
|
| 4103 |
/* Add transform feedback varyings. */ |
| 4104 |
if (linked_xfb->NumVarying > 0) { |
| 4105 |
for (int i = 0; i < linked_xfb->NumVarying; i++) { |
| 4106 |
if (!link_util_add_program_resource(shProg, resource_set, |
| 4107 |
GL_TRANSFORM_FEEDBACK_VARYING, |
| 4108 |
&linked_xfb->Varyings[i], 0)) |
| 4109 |
return; |
| 4110 |
} |
| 4111 |
} |
| 4112 |
|
| 4113 |
/* Add transform feedback buffers. */ |
| 4114 |
for (unsigned i = 0; i < ctx->Const.MaxTransformFeedbackBuffers; i++) { |
| 4115 |
if ((linked_xfb->ActiveBuffers >> i) & 1) { |
| 4116 |
linked_xfb->Buffers[i].Binding = i; |
| 4117 |
if (!link_util_add_program_resource(shProg, resource_set, |
| 4118 |
GL_TRANSFORM_FEEDBACK_BUFFER, |
| 4119 |
&linked_xfb->Buffers[i], 0)) |
| 4120 |
return; |
| 4121 |
} |
| 4122 |
} |
| 4123 |
} |
| 4124 |
|
| 4125 |
int top_level_array_base_offset = -1; |
| 4126 |
int top_level_array_size_in_bytes = -1; |
| 4127 |
int second_element_offset = -1; |
| 4128 |
int buffer_block_index = -1; |
| 4129 |
|
| 4130 |
/* Add uniforms from uniform storage. */ |
| 4131 |
for (unsigned i = 0; i < shProg->data->NumUniformStorage; i++) { |
| 4132 |
/* Do not add uniforms internally used by Mesa. */ |
| 4133 |
if (shProg->data->UniformStorage[i].hidden) |
| 4134 |
continue; |
| 4135 |
|
| 4136 |
bool is_shader_storage = |
| 4137 |
shProg->data->UniformStorage[i].is_shader_storage; |
| 4138 |
GLenum type = is_shader_storage ? GL_BUFFER_VARIABLE : GL_UNIFORM; |
| 4139 |
if (!link_util_should_add_buffer_variable(shProg, |
| 4140 |
&shProg->data->UniformStorage[i], |
| 4141 |
top_level_array_base_offset, |
| 4142 |
top_level_array_size_in_bytes, |
| 4143 |
second_element_offset, |
| 4144 |
buffer_block_index)) |
| 4145 |
continue; |
| 4146 |
|
| 4147 |
if (is_shader_storage) { |
| 4148 |
/* From the OpenGL 4.6 specification, 7.3.1.1 Naming Active Resources: |
| 4149 |
* |
| 4150 |
* "For an active shader storage block member declared as an array |
| 4151 |
* of an aggregate type, an entry will be generated only for the |
| 4152 |
* first array element, regardless of its type. Such block members |
| 4153 |
* are referred to as top-level arrays. If the block member is an |
| 4154 |
* aggregate type, the enumeration rules are then applied |
| 4155 |
* recursively." |
| 4156 |
* |
| 4157 |
* Below we update our tracking values used by |
| 4158 |
* link_util_should_add_buffer_variable(). We only want to reset the |
| 4159 |
* offsets once we have moved past the first element. |
| 4160 |
*/ |
| 4161 |
if (shProg->data->UniformStorage[i].offset >= second_element_offset) { |
| 4162 |
top_level_array_base_offset = |
| 4163 |
shProg->data->UniformStorage[i].offset; |
| 4164 |
|
| 4165 |
top_level_array_size_in_bytes = |
| 4166 |
shProg->data->UniformStorage[i].top_level_array_size * |
| 4167 |
shProg->data->UniformStorage[i].top_level_array_stride; |
| 4168 |
|
| 4169 |
/* Set or reset the second element offset. For non arrays this |
| 4170 |
* will be set to -1. |
| 4171 |
*/ |
| 4172 |
second_element_offset = top_level_array_size_in_bytes ? |
| 4173 |
top_level_array_base_offset + |
| 4174 |
shProg->data->UniformStorage[i].top_level_array_stride : -1; |
| 4175 |
} |
| 4176 |
|
| 4177 |
buffer_block_index = shProg->data->UniformStorage[i].block_index; |
| 4178 |
} |
| 4179 |
|
| 4180 |
uint8_t stageref = shProg->data->UniformStorage[i].active_shader_mask; |
| 4181 |
if (!link_util_add_program_resource(shProg, resource_set, type, |
| 4182 |
&shProg->data->UniformStorage[i], stageref)) |
| 4183 |
return; |
| 4184 |
} |
| 4185 |
|
| 4186 |
/* Add program uniform blocks. */ |
| 4187 |
for (unsigned i = 0; i < shProg->data->NumUniformBlocks; i++) { |
| 4188 |
if (!link_util_add_program_resource(shProg, resource_set, GL_UNIFORM_BLOCK, |
| 4189 |
&shProg->data->UniformBlocks[i], 0)) |
| 4190 |
return; |
| 4191 |
} |
| 4192 |
|
| 4193 |
/* Add program shader storage blocks. */ |
| 4194 |
for (unsigned i = 0; i < shProg->data->NumShaderStorageBlocks; i++) { |
| 4195 |
if (!link_util_add_program_resource(shProg, resource_set, GL_SHADER_STORAGE_BLOCK, |
| 4196 |
&shProg->data->ShaderStorageBlocks[i], 0)) |
| 4197 |
return; |
| 4198 |
} |
| 4199 |
|
| 4200 |
/* Add atomic counter buffers. */ |
| 4201 |
for (unsigned i = 0; i < shProg->data->NumAtomicBuffers; i++) { |
| 4202 |
if (!link_util_add_program_resource(shProg, resource_set, GL_ATOMIC_COUNTER_BUFFER, |
| 4203 |
&shProg->data->AtomicBuffers[i], 0)) |
| 4204 |
return; |
| 4205 |
} |
| 4206 |
|
| 4207 |
for (unsigned i = 0; i < shProg->data->NumUniformStorage; i++) { |
| 4208 |
GLenum type; |
| 4209 |
if (!shProg->data->UniformStorage[i].hidden) |
| 4210 |
continue; |
| 4211 |
|
| 4212 |
for (int j = MESA_SHADER_VERTEX; j < MESA_SHADER_STAGES; j++) { |
| 4213 |
if (!shProg->data->UniformStorage[i].opaque[j].active || |
| 4214 |
!shProg->data->UniformStorage[i].type->is_subroutine()) |
| 4215 |
continue; |
| 4216 |
|
| 4217 |
type = _mesa_shader_stage_to_subroutine_uniform((gl_shader_stage)j); |
| 4218 |
/* add shader subroutines */ |
| 4219 |
if (!link_util_add_program_resource(shProg, resource_set, |
| 4220 |
type, &shProg->data->UniformStorage[i], 0)) |
| 4221 |
return; |
| 4222 |
} |
| 4223 |
} |
| 4224 |
|
| 4225 |
unsigned mask = shProg->data->linked_stages; |
| 4226 |
while (mask) { |
| 4227 |
const int i = u_bit_scan(&mask); |
| 4228 |
struct gl_program *p = shProg->_LinkedShaders[i]->Program; |
| 4229 |
|
| 4230 |
GLuint type = _mesa_shader_stage_to_subroutine((gl_shader_stage)i); |
| 4231 |
for (unsigned j = 0; j < p->sh.NumSubroutineFunctions; j++) { |
| 4232 |
if (!link_util_add_program_resource(shProg, resource_set, |
| 4233 |
type, &p->sh.SubroutineFunctions[j], 0)) |
| 4234 |
return; |
| 4235 |
} |
| 4236 |
} |
| 4237 |
|
| 4238 |
_mesa_set_destroy(resource_set, NULL); |
| 4239 |
} |
| 4240 |
|
| 4241 |
/** |
| 4242 |
* This check is done to make sure we allow only constant expression |
| 4243 |
* indexing and "constant-index-expression" (indexing with an expression |
| 4244 |
* that includes loop induction variable). |
| 4245 |
*/ |
| 4246 |
static bool |
| 4247 |
validate_sampler_array_indexing(struct gl_context *ctx, |
| 4248 |
struct gl_shader_program *prog) |
| 4249 |
{ |
| 4250 |
dynamic_sampler_array_indexing_visitor v; |
| 4251 |
for (unsigned i = 0; i < MESA_SHADER_STAGES; i++) { |
| 4252 |
if (prog->_LinkedShaders[i] == NULL) |
| 4253 |
continue; |
| 4254 |
|
| 4255 |
bool no_dynamic_indexing = |
| 4256 |
ctx->Const.ShaderCompilerOptions[i].EmitNoIndirectSampler; |
| 4257 |
|
| 4258 |
/* Search for array derefs in shader. */ |
| 4259 |
v.run(prog->_LinkedShaders[i]->ir); |
| 4260 |
if (v.uses_dynamic_sampler_array_indexing()) { |
| 4261 |
const char *msg = "sampler arrays indexed with non-constant " |
| 4262 |
"expressions is forbidden in GLSL %s %u"; |
| 4263 |
/* Backend has indicated that it has no dynamic indexing support. */ |
| 4264 |
if (no_dynamic_indexing) { |
| 4265 |
linker_error(prog, msg, prog->IsES ? "ES" : "", |
| 4266 |
prog->data->Version); |
| 4267 |
return false; |
| 4268 |
} else { |
| 4269 |
linker_warning(prog, msg, prog->IsES ? "ES" : "", |
| 4270 |
prog->data->Version); |
| 4271 |
} |
| 4272 |
} |
| 4273 |
} |
| 4274 |
return true; |
| 4275 |
} |
| 4276 |
|
| 4277 |
static void |
| 4278 |
link_assign_subroutine_types(struct gl_shader_program *prog) |
| 4279 |
{ |
| 4280 |
unsigned mask = prog->data->linked_stages; |
| 4281 |
while (mask) { |
| 4282 |
const int i = u_bit_scan(&mask); |
| 4283 |
gl_program *p = prog->_LinkedShaders[i]->Program; |
| 4284 |
|
| 4285 |
p->sh.MaxSubroutineFunctionIndex = 0; |
| 4286 |
foreach_in_list(ir_instruction, node, prog->_LinkedShaders[i]->ir) { |
| 4287 |
ir_function *fn = node->as_function(); |
| 4288 |
if (!fn) |
| 4289 |
continue; |
| 4290 |
|
| 4291 |
if (fn->is_subroutine) |
| 4292 |
p->sh.NumSubroutineUniformTypes++; |
| 4293 |
|
| 4294 |
if (!fn->num_subroutine_types) |
| 4295 |
continue; |
| 4296 |
|
| 4297 |
/* these should have been calculated earlier. */ |
| 4298 |
assert(fn->subroutine_index != -1); |
| 4299 |
if (p->sh.NumSubroutineFunctions + 1 > MAX_SUBROUTINES) { |
| 4300 |
linker_error(prog, "Too many subroutine functions declared.\n"); |
| 4301 |
return; |
| 4302 |
} |
| 4303 |
p->sh.SubroutineFunctions = reralloc(p, p->sh.SubroutineFunctions, |
| 4304 |
struct gl_subroutine_function, |
| 4305 |
p->sh.NumSubroutineFunctions + 1); |
| 4306 |
p->sh.SubroutineFunctions[p->sh.NumSubroutineFunctions].name = ralloc_strdup(p, fn->name); |
| 4307 |
p->sh.SubroutineFunctions[p->sh.NumSubroutineFunctions].num_compat_types = fn->num_subroutine_types; |
| 4308 |
p->sh.SubroutineFunctions[p->sh.NumSubroutineFunctions].types = |
| 4309 |
ralloc_array(p, const struct glsl_type *, |
| 4310 |
fn->num_subroutine_types); |
| 4311 |
|
| 4312 |
/* From Section 4.4.4(Subroutine Function Layout Qualifiers) of the |
| 4313 |
* GLSL 4.5 spec: |
| 4314 |
* |
| 4315 |
* "Each subroutine with an index qualifier in the shader must be |
| 4316 |
* given a unique index, otherwise a compile or link error will be |
| 4317 |
* generated." |
| 4318 |
*/ |
| 4319 |
for (unsigned j = 0; j < p->sh.NumSubroutineFunctions; j++) { |
| 4320 |
if (p->sh.SubroutineFunctions[j].index != -1 && |
| 4321 |
p->sh.SubroutineFunctions[j].index == fn->subroutine_index) { |
| 4322 |
linker_error(prog, "each subroutine index qualifier in the " |
| 4323 |
"shader must be unique\n"); |
| 4324 |
return; |
| 4325 |
} |
| 4326 |
} |
| 4327 |
p->sh.SubroutineFunctions[p->sh.NumSubroutineFunctions].index = |
| 4328 |
fn->subroutine_index; |
| 4329 |
|
| 4330 |
if (fn->subroutine_index > (int)p->sh.MaxSubroutineFunctionIndex) |
| 4331 |
p->sh.MaxSubroutineFunctionIndex = fn->subroutine_index; |
| 4332 |
|
| 4333 |
for (int j = 0; j < fn->num_subroutine_types; j++) |
| 4334 |
p->sh.SubroutineFunctions[p->sh.NumSubroutineFunctions].types[j] = fn->subroutine_types[j]; |
| 4335 |
p->sh.NumSubroutineFunctions++; |
| 4336 |
} |
| 4337 |
} |
| 4338 |
} |
| 4339 |
|
| 4340 |
static void |
| 4341 |
verify_subroutine_associated_funcs(struct gl_shader_program *prog) |
| 4342 |
{ |
| 4343 |
unsigned mask = prog->data->linked_stages; |
| 4344 |
while (mask) { |
| 4345 |
const int i = u_bit_scan(&mask); |
| 4346 |
gl_program *p = prog->_LinkedShaders[i]->Program; |
| 4347 |
glsl_symbol_table *symbols = prog->_LinkedShaders[i]->symbols; |
| 4348 |
|
| 4349 |
/* Section 6.1.2 (Subroutines) of the GLSL 4.00 spec says: |
| 4350 |
* |
| 4351 |
* "A program will fail to compile or link if any shader |
| 4352 |
* or stage contains two or more functions with the same |
| 4353 |
* name if the name is associated with a subroutine type." |
| 4354 |
*/ |
| 4355 |
for (unsigned j = 0; j < p->sh.NumSubroutineFunctions; j++) { |
| 4356 |
unsigned definitions = 0; |
| 4357 |
char *name = p->sh.SubroutineFunctions[j].name; |
| 4358 |
ir_function *fn = symbols->get_function(name); |
| 4359 |
|
| 4360 |
/* Calculate number of function definitions with the same name */ |
| 4361 |
foreach_in_list(ir_function_signature, sig, &fn->signatures) { |
| 4362 |
if (sig->is_defined) { |
| 4363 |
if (++definitions > 1) { |
| 4364 |
linker_error(prog, "%s shader contains two or more function " |
| 4365 |
"definitions with name `%s', which is " |
| 4366 |
"associated with a subroutine type.\n", |
| 4367 |
_mesa_shader_stage_to_string(i), |
| 4368 |
fn->name); |
| 4369 |
return; |
| 4370 |
} |
| 4371 |
} |
| 4372 |
} |
| 4373 |
} |
| 4374 |
} |
| 4375 |
} |
| 4376 |
|
| 4377 |
|
| 4378 |
static void |
| 4379 |
set_always_active_io(exec_list *ir, ir_variable_mode io_mode) |
| 4380 |
{ |
| 4381 |
assert(io_mode == ir_var_shader_in || io_mode == ir_var_shader_out); |
| 4382 |
|
| 4383 |
foreach_in_list(ir_instruction, node, ir) { |
| 4384 |
ir_variable *const var = node->as_variable(); |
| 4385 |
|
| 4386 |
if (var == NULL || var->data.mode != io_mode) |
| 4387 |
continue; |
| 4388 |
|
| 4389 |
/* Don't set always active on builtins that haven't been redeclared */ |
| 4390 |
if (var->data.how_declared == ir_var_declared_implicitly) |
| 4391 |
continue; |
| 4392 |
|
| 4393 |
var->data.always_active_io = true; |
| 4394 |
} |
| 4395 |
} |
| 4396 |
|
| 4397 |
/** |
| 4398 |
* When separate shader programs are enabled, only input/outputs between |
| 4399 |
* the stages of a multi-stage separate program can be safely removed |
| 4400 |
* from the shader interface. Other inputs/outputs must remain active. |
| 4401 |
*/ |
| 4402 |
static void |
| 4403 |
disable_varying_optimizations_for_sso(struct gl_shader_program *prog) |
| 4404 |
{ |
| 4405 |
unsigned first, last; |
| 4406 |
assert(prog->SeparateShader); |
| 4407 |
|
| 4408 |
first = MESA_SHADER_STAGES; |
| 4409 |
last = 0; |
| 4410 |
|
| 4411 |
/* Determine first and last stage. Excluding the compute stage */ |
| 4412 |
for (unsigned i = 0; i < MESA_SHADER_COMPUTE; i++) { |
| 4413 |
if (!prog->_LinkedShaders[i]) |
| 4414 |
continue; |
| 4415 |
if (first == MESA_SHADER_STAGES) |
| 4416 |
first = i; |
| 4417 |
last = i; |
| 4418 |
} |
| 4419 |
|
| 4420 |
if (first == MESA_SHADER_STAGES) |
| 4421 |
return; |
| 4422 |
|
| 4423 |
for (unsigned stage = 0; stage < MESA_SHADER_STAGES; stage++) { |
| 4424 |
gl_linked_shader *sh = prog->_LinkedShaders[stage]; |
| 4425 |
if (!sh) |
| 4426 |
continue; |
| 4427 |
|
| 4428 |
/* Prevent the removal of inputs to the first and outputs from the last |
| 4429 |
* stage, unless they are the initial pipeline inputs or final pipeline |
| 4430 |
* outputs, respectively. |
| 4431 |
* |
| 4432 |
* The removal of IO between shaders in the same program is always |
| 4433 |
* allowed. |
| 4434 |
*/ |
| 4435 |
if (stage == first && stage != MESA_SHADER_VERTEX) |
| 4436 |
set_always_active_io(sh->ir, ir_var_shader_in); |
| 4437 |
if (stage == last && stage != MESA_SHADER_FRAGMENT) |
| 4438 |
set_always_active_io(sh->ir, ir_var_shader_out); |
| 4439 |
} |
| 4440 |
} |
| 4441 |
|
| 4442 |
static void |
| 4443 |
link_and_validate_uniforms(struct gl_context *ctx, |
| 4444 |
struct gl_shader_program *prog) |
| 4445 |
{ |
| 4446 |
update_array_sizes(prog); |
| 4447 |
|
| 4448 |
if (!ctx->Const.UseNIRGLSLLinker) { |
| 4449 |
link_assign_uniform_locations(prog, ctx); |
| 4450 |
|
| 4451 |
if (prog->data->LinkStatus == LINKING_FAILURE) |
| 4452 |
return; |
| 4453 |
|
| 4454 |
link_util_calculate_subroutine_compat(prog); |
| 4455 |
link_util_check_uniform_resources(ctx, prog); |
| 4456 |
link_util_check_subroutine_resources(prog); |
| 4457 |
check_image_resources(ctx, prog); |
| 4458 |
link_assign_atomic_counter_resources(ctx, prog); |
| 4459 |
link_check_atomic_counter_resources(ctx, prog); |
| 4460 |
} |
| 4461 |
} |
| 4462 |
|
| 4463 |
static bool |
| 4464 |
link_varyings_and_uniforms(unsigned first, unsigned last, |
| 4465 |
struct gl_context *ctx, |
| 4466 |
struct gl_shader_program *prog, void *mem_ctx) |
| 4467 |
{ |
| 4468 |
/* Mark all generic shader inputs and outputs as unpaired. */ |
| 4469 |
for (unsigned i = MESA_SHADER_VERTEX; i <= MESA_SHADER_FRAGMENT; i++) { |
| 4470 |
if (prog->_LinkedShaders[i] != NULL) { |
| 4471 |
link_invalidate_variable_locations(prog->_LinkedShaders[i]->ir); |
| 4472 |
} |
| 4473 |
} |
| 4474 |
|
| 4475 |
unsigned prev = first; |
| 4476 |
for (unsigned i = prev + 1; i <= MESA_SHADER_FRAGMENT; i++) { |
| 4477 |
if (prog->_LinkedShaders[i] == NULL) |
| 4478 |
continue; |
| 4479 |
|
| 4480 |
match_explicit_outputs_to_inputs(prog->_LinkedShaders[prev], |
| 4481 |
prog->_LinkedShaders[i]); |
| 4482 |
prev = i; |
| 4483 |
} |
| 4484 |
|
| 4485 |
if (!assign_attribute_or_color_locations(mem_ctx, prog, &ctx->Const, |
| 4486 |
MESA_SHADER_VERTEX, true)) { |
| 4487 |
return false; |
| 4488 |
} |
| 4489 |
|
| 4490 |
if (!assign_attribute_or_color_locations(mem_ctx, prog, &ctx->Const, |
| 4491 |
MESA_SHADER_FRAGMENT, true)) { |
| 4492 |
return false; |
| 4493 |
} |
| 4494 |
|
| 4495 |
prog->last_vert_prog = NULL; |
| 4496 |
for (int i = MESA_SHADER_GEOMETRY; i >= MESA_SHADER_VERTEX; i--) { |
| 4497 |
if (prog->_LinkedShaders[i] == NULL) |
| 4498 |
continue; |
| 4499 |
|
| 4500 |
prog->last_vert_prog = prog->_LinkedShaders[i]->Program; |
| 4501 |
break; |
| 4502 |
} |
| 4503 |
|
| 4504 |
if (!link_varyings(prog, first, last, ctx, mem_ctx)) |
| 4505 |
return false; |
| 4506 |
|
| 4507 |
link_and_validate_uniforms(ctx, prog); |
| 4508 |
|
| 4509 |
if (!prog->data->LinkStatus) |
| 4510 |
return false; |
| 4511 |
|
| 4512 |
for (unsigned i = 0; i < MESA_SHADER_STAGES; i++) { |
| 4513 |
if (prog->_LinkedShaders[i] == NULL) |
| 4514 |
continue; |
| 4515 |
|
| 4516 |
const struct gl_shader_compiler_options *options = |
| 4517 |
&ctx->Const.ShaderCompilerOptions[i]; |
| 4518 |
|
| 4519 |
if (options->LowerBufferInterfaceBlocks) |
| 4520 |
lower_ubo_reference(prog->_LinkedShaders[i], |
| 4521 |
options->ClampBlockIndicesToArrayBounds, |
| 4522 |
ctx->Const.UseSTD430AsDefaultPacking); |
| 4523 |
|
| 4524 |
if (i == MESA_SHADER_COMPUTE) |
| 4525 |
lower_shared_reference(ctx, prog, prog->_LinkedShaders[i]); |
| 4526 |
|
| 4527 |
lower_vector_derefs(prog->_LinkedShaders[i]); |
| 4528 |
do_vec_index_to_swizzle(prog->_LinkedShaders[i]->ir); |
| 4529 |
} |
| 4530 |
|
| 4531 |
return true; |
| 4532 |
} |
| 4533 |
|
| 4534 |
static void |
| 4535 |
linker_optimisation_loop(struct gl_context *ctx, exec_list *ir, |
| 4536 |
unsigned stage) |
| 4537 |
{ |
| 4538 |
if (ctx->Const.GLSLOptimizeConservatively) { |
| 4539 |
/* Run it just once. */ |
| 4540 |
do_common_optimization(ir, true, false, |
| 4541 |
&ctx->Const.ShaderCompilerOptions[stage], |
| 4542 |
ctx->Const.NativeIntegers); |
| 4543 |
} else { |
| 4544 |
/* Repeat it until it stops making changes. */ |
| 4545 |
while (do_common_optimization(ir, true, false, |
| 4546 |
&ctx->Const.ShaderCompilerOptions[stage], |
| 4547 |
ctx->Const.NativeIntegers)) |
| 4548 |
; |
| 4549 |
} |
| 4550 |
} |
| 4551 |
|
| 4552 |
void |
| 4553 |
link_shaders(struct gl_context *ctx, struct gl_shader_program *prog) |
| 4554 |
{ |
| 4555 |
prog->data->LinkStatus = LINKING_SUCCESS; /* All error paths will set this to false */ |
| 4556 |
prog->data->Validated = false; |
| 4557 |
|
| 4558 |
/* Section 7.3 (Program Objects) of the OpenGL 4.5 Core Profile spec says: |
| 4559 |
* |
| 4560 |
* "Linking can fail for a variety of reasons as specified in the |
| 4561 |
* OpenGL Shading Language Specification, as well as any of the |
| 4562 |
* following reasons: |
| 4563 |
* |
| 4564 |
* - No shader objects are attached to program." |
| 4565 |
* |
| 4566 |
* The Compatibility Profile specification does not list the error. In |
| 4567 |
* Compatibility Profile missing shader stages are replaced by |
| 4568 |
* fixed-function. This applies to the case where all stages are |
| 4569 |
* missing. |
| 4570 |
*/ |
| 4571 |
if (prog->NumShaders == 0) { |
| 4572 |
if (ctx->API != API_OPENGL_COMPAT) |
| 4573 |
linker_error(prog, "no shaders attached to the program\n"); |
| 4574 |
return; |
| 4575 |
} |
| 4576 |
|
| 4577 |
#ifdef ENABLE_SHADER_CACHE |
| 4578 |
if (shader_cache_read_program_metadata(ctx, prog)) |
| 4579 |
return; |
| 4580 |
#endif |
| 4581 |
|
| 4582 |
void *mem_ctx = ralloc_context(NULL); // temporary linker context |
| 4583 |
|
| 4584 |
prog->ARB_fragment_coord_conventions_enable = false; |
| 4585 |
|
| 4586 |
/* Separate the shaders into groups based on their type. |
| 4587 |
*/ |
| 4588 |
struct gl_shader **shader_list[MESA_SHADER_STAGES]; |
| 4589 |
unsigned num_shaders[MESA_SHADER_STAGES]; |
| 4590 |
|
| 4591 |
for (int i = 0; i < MESA_SHADER_STAGES; i++) { |
| 4592 |
shader_list[i] = (struct gl_shader **) |
| 4593 |
calloc(prog->NumShaders, sizeof(struct gl_shader *)); |
| 4594 |
num_shaders[i] = 0; |
| 4595 |
} |
| 4596 |
|
| 4597 |
unsigned min_version = UINT_MAX; |
| 4598 |
unsigned max_version = 0; |
| 4599 |
for (unsigned i = 0; i < prog->NumShaders; i++) { |
| 4600 |
min_version = MIN2(min_version, prog->Shaders[i]->Version); |
| 4601 |
max_version = MAX2(max_version, prog->Shaders[i]->Version); |
| 4602 |
|
| 4603 |
if (!ctx->Const.AllowGLSLRelaxedES && |
| 4604 |
prog->Shaders[i]->IsES != prog->Shaders[0]->IsES) { |
| 4605 |
linker_error(prog, "all shaders must use same shading " |
| 4606 |
"language version\n"); |
| 4607 |
goto done; |
| 4608 |
} |
| 4609 |
|
| 4610 |
if (prog->Shaders[i]->ARB_fragment_coord_conventions_enable) { |
| 4611 |
prog->ARB_fragment_coord_conventions_enable = true; |
| 4612 |
} |
| 4613 |
|
| 4614 |
gl_shader_stage shader_type = prog->Shaders[i]->Stage; |
| 4615 |
shader_list[shader_type][num_shaders[shader_type]] = prog->Shaders[i]; |
| 4616 |
num_shaders[shader_type]++; |
| 4617 |
} |
| 4618 |
|
| 4619 |
/* In desktop GLSL, different shader versions may be linked together. In |
| 4620 |
* GLSL ES, all shader versions must be the same. |
| 4621 |
*/ |
| 4622 |
if (!ctx->Const.AllowGLSLRelaxedES && prog->Shaders[0]->IsES && |
| 4623 |
min_version != max_version) { |
| 4624 |
linker_error(prog, "all shaders must use same shading " |
| 4625 |
"language version\n"); |
| 4626 |
goto done; |
| 4627 |
} |
| 4628 |
|
| 4629 |
prog->data->Version = max_version; |
| 4630 |
prog->IsES = prog->Shaders[0]->IsES; |
| 4631 |
|
| 4632 |
/* Some shaders have to be linked with some other shaders present. |
| 4633 |
*/ |
| 4634 |
if (!prog->SeparateShader) { |
| 4635 |
if (num_shaders[MESA_SHADER_GEOMETRY] > 0 && |
| 4636 |
num_shaders[MESA_SHADER_VERTEX] == 0) { |
| 4637 |
linker_error(prog, "Geometry shader must be linked with " |
| 4638 |
"vertex shader\n"); |
| 4639 |
goto done; |
| 4640 |
} |
| 4641 |
if (num_shaders[MESA_SHADER_TESS_EVAL] > 0 && |
| 4642 |
num_shaders[MESA_SHADER_VERTEX] == 0) { |
| 4643 |
linker_error(prog, "Tessellation evaluation shader must be linked " |
| 4644 |
"with vertex shader\n"); |
| 4645 |
goto done; |
| 4646 |
} |
| 4647 |
if (num_shaders[MESA_SHADER_TESS_CTRL] > 0 && |
| 4648 |
num_shaders[MESA_SHADER_VERTEX] == 0) { |
| 4649 |
linker_error(prog, "Tessellation control shader must be linked with " |
| 4650 |
"vertex shader\n"); |
| 4651 |
goto done; |
| 4652 |
} |
| 4653 |
|
| 4654 |
/* Section 7.3 of the OpenGL ES 3.2 specification says: |
| 4655 |
* |
| 4656 |
* "Linking can fail for [...] any of the following reasons: |
| 4657 |
* |
| 4658 |
* * program contains an object to form a tessellation control |
| 4659 |
* shader [...] and [...] the program is not separable and |
| 4660 |
* contains no object to form a tessellation evaluation shader" |
| 4661 |
* |
| 4662 |
* The OpenGL spec is contradictory. It allows linking without a tess |
| 4663 |
* eval shader, but that can only be used with transform feedback and |
| 4664 |
* rasterization disabled. However, transform feedback isn't allowed |
| 4665 |
* with GL_PATCHES, so it can't be used. |
| 4666 |
* |
| 4667 |
* More investigation showed that the idea of transform feedback after |
| 4668 |
* a tess control shader was dropped, because some hw vendors couldn't |
| 4669 |
* support tessellation without a tess eval shader, but the linker |
| 4670 |
* section wasn't updated to reflect that. |
| 4671 |
* |
| 4672 |
* All specifications (ARB_tessellation_shader, GL 4.0-4.5) have this |
| 4673 |
* spec bug. |
| 4674 |
* |
| 4675 |
* Do what's reasonable and always require a tess eval shader if a tess |
| 4676 |
* control shader is present. |
| 4677 |
*/ |
| 4678 |
if (num_shaders[MESA_SHADER_TESS_CTRL] > 0 && |
| 4679 |
num_shaders[MESA_SHADER_TESS_EVAL] == 0) { |
| 4680 |
linker_error(prog, "Tessellation control shader must be linked with " |
| 4681 |
"tessellation evaluation shader\n"); |
| 4682 |
goto done; |
| 4683 |
} |
| 4684 |
|
| 4685 |
if (prog->IsES) { |
| 4686 |
if (num_shaders[MESA_SHADER_TESS_EVAL] > 0 && |
| 4687 |
num_shaders[MESA_SHADER_TESS_CTRL] == 0) { |
| 4688 |
linker_error(prog, "GLSL ES requires non-separable programs " |
| 4689 |
"containing a tessellation evaluation shader to also " |
| 4690 |
"be linked with a tessellation control shader\n"); |
| 4691 |
goto done; |
| 4692 |
} |
| 4693 |
} |
| 4694 |
} |
| 4695 |
|
| 4696 |
/* Compute shaders have additional restrictions. */ |
| 4697 |
if (num_shaders[MESA_SHADER_COMPUTE] > 0 && |
| 4698 |
num_shaders[MESA_SHADER_COMPUTE] != prog->NumShaders) { |
| 4699 |
linker_error(prog, "Compute shaders may not be linked with any other " |
| 4700 |
"type of shader\n"); |
| 4701 |
} |
| 4702 |
|
| 4703 |
/* Link all shaders for a particular stage and validate the result. |
| 4704 |
*/ |
| 4705 |
for (int stage = 0; stage < MESA_SHADER_STAGES; stage++) { |
| 4706 |
if (num_shaders[stage] > 0) { |
| 4707 |
gl_linked_shader *const sh = |
| 4708 |
link_intrastage_shaders(mem_ctx, ctx, prog, shader_list[stage], |
| 4709 |
num_shaders[stage], false); |
| 4710 |
|
| 4711 |
if (!prog->data->LinkStatus) { |
| 4712 |
if (sh) |
| 4713 |
_mesa_delete_linked_shader(ctx, sh); |
| 4714 |
goto done; |
| 4715 |
} |
| 4716 |
|
| 4717 |
switch (stage) { |
| 4718 |
case MESA_SHADER_VERTEX: |
| 4719 |
validate_vertex_shader_executable(prog, sh, ctx); |
| 4720 |
break; |
| 4721 |
case MESA_SHADER_TESS_CTRL: |
| 4722 |
/* nothing to be done */ |
| 4723 |
break; |
| 4724 |
case MESA_SHADER_TESS_EVAL: |
| 4725 |
validate_tess_eval_shader_executable(prog, sh, ctx); |
| 4726 |
break; |
| 4727 |
case MESA_SHADER_GEOMETRY: |
| 4728 |
validate_geometry_shader_executable(prog, sh, ctx); |
| 4729 |
break; |
| 4730 |
case MESA_SHADER_FRAGMENT: |
| 4731 |
validate_fragment_shader_executable(prog, sh); |
| 4732 |
break; |
| 4733 |
} |
| 4734 |
if (!prog->data->LinkStatus) { |
| 4735 |
if (sh) |
| 4736 |
_mesa_delete_linked_shader(ctx, sh); |
| 4737 |
goto done; |
| 4738 |
} |
| 4739 |
|
| 4740 |
prog->_LinkedShaders[stage] = sh; |
| 4741 |
prog->data->linked_stages |= 1 << stage; |
| 4742 |
} |
| 4743 |
} |
| 4744 |
|
| 4745 |
/* Here begins the inter-stage linking phase. Some initial validation is |
| 4746 |
* performed, then locations are assigned for uniforms, attributes, and |
| 4747 |
* varyings. |
| 4748 |
*/ |
| 4749 |
cross_validate_uniforms(ctx, prog); |
| 4750 |
if (!prog->data->LinkStatus) |
| 4751 |
goto done; |
| 4752 |
|
| 4753 |
unsigned first, last, prev; |
| 4754 |
|
| 4755 |
first = MESA_SHADER_STAGES; |
| 4756 |
last = 0; |
| 4757 |
|
| 4758 |
/* Determine first and last stage. */ |
| 4759 |
for (unsigned i = 0; i < MESA_SHADER_STAGES; i++) { |
| 4760 |
if (!prog->_LinkedShaders[i]) |
| 4761 |
continue; |
| 4762 |
if (first == MESA_SHADER_STAGES) |
| 4763 |
first = i; |
| 4764 |
last = i; |
| 4765 |
} |
| 4766 |
|
| 4767 |
check_explicit_uniform_locations(ctx, prog); |
| 4768 |
link_assign_subroutine_types(prog); |
| 4769 |
verify_subroutine_associated_funcs(prog); |
| 4770 |
|
| 4771 |
if (!prog->data->LinkStatus) |
| 4772 |
goto done; |
| 4773 |
|
| 4774 |
resize_tes_inputs(ctx, prog); |
| 4775 |
|
| 4776 |
/* Validate the inputs of each stage with the output of the preceding |
| 4777 |
* stage. |
| 4778 |
*/ |
| 4779 |
prev = first; |
| 4780 |
for (unsigned i = prev + 1; i <= MESA_SHADER_FRAGMENT; i++) { |
| 4781 |
if (prog->_LinkedShaders[i] == NULL) |
| 4782 |
continue; |
| 4783 |
|
| 4784 |
validate_interstage_inout_blocks(prog, prog->_LinkedShaders[prev], |
| 4785 |
prog->_LinkedShaders[i]); |
| 4786 |
if (!prog->data->LinkStatus) |
| 4787 |
goto done; |
| 4788 |
|
| 4789 |
cross_validate_outputs_to_inputs(ctx, prog, |
| 4790 |
prog->_LinkedShaders[prev], |
| 4791 |
prog->_LinkedShaders[i]); |
| 4792 |
if (!prog->data->LinkStatus) |
| 4793 |
goto done; |
| 4794 |
|
| 4795 |
prev = i; |
| 4796 |
} |
| 4797 |
|
| 4798 |
/* The cross validation of outputs/inputs above validates interstage |
| 4799 |
* explicit locations. We need to do this also for the inputs in the first |
| 4800 |
* stage and outputs of the last stage included in the program, since there |
| 4801 |
* is no cross validation for these. |
| 4802 |
*/ |
| 4803 |
validate_first_and_last_interface_explicit_locations(ctx, prog, |
| 4804 |
(gl_shader_stage) first, |
| 4805 |
(gl_shader_stage) last); |
| 4806 |
|
| 4807 |
/* Cross-validate uniform blocks between shader stages */ |
| 4808 |
validate_interstage_uniform_blocks(prog, prog->_LinkedShaders); |
| 4809 |
if (!prog->data->LinkStatus) |
| 4810 |
goto done; |
| 4811 |
|
| 4812 |
for (unsigned int i = 0; i < MESA_SHADER_STAGES; i++) { |
| 4813 |
if (prog->_LinkedShaders[i] != NULL) |
| 4814 |
lower_named_interface_blocks(mem_ctx, prog->_LinkedShaders[i]); |
| 4815 |
} |
| 4816 |
|
| 4817 |
if (prog->IsES && prog->data->Version == 100) |
| 4818 |
if (!validate_invariant_builtins(prog, |
| 4819 |
prog->_LinkedShaders[MESA_SHADER_VERTEX], |
| 4820 |
prog->_LinkedShaders[MESA_SHADER_FRAGMENT])) |
| 4821 |
goto done; |
| 4822 |
|
| 4823 |
/* Implement the GLSL 1.30+ rule for discard vs infinite loops Do |
| 4824 |
* it before optimization because we want most of the checks to get |
| 4825 |
* dropped thanks to constant propagation. |
| 4826 |
* |
| 4827 |
* This rule also applies to GLSL ES 3.00. |
| 4828 |
*/ |
| 4829 |
if (max_version >= (prog->IsES ? 300 : 130)) { |
| 4830 |
struct gl_linked_shader *sh = prog->_LinkedShaders[MESA_SHADER_FRAGMENT]; |
| 4831 |
if (sh) { |
| 4832 |
lower_discard_flow(sh->ir); |
| 4833 |
} |
| 4834 |
} |
| 4835 |
|
| 4836 |
if (prog->SeparateShader) |
| 4837 |
disable_varying_optimizations_for_sso(prog); |
| 4838 |
|
| 4839 |
/* Process UBOs */ |
| 4840 |
if (!interstage_cross_validate_uniform_blocks(prog, false)) |
| 4841 |
goto done; |
| 4842 |
|
| 4843 |
/* Process SSBOs */ |
| 4844 |
if (!interstage_cross_validate_uniform_blocks(prog, true)) |
| 4845 |
goto done; |
| 4846 |
|
| 4847 |
/* Do common optimization before assigning storage for attributes, |
| 4848 |
* uniforms, and varyings. Later optimization could possibly make |
| 4849 |
* some of that unused. |
| 4850 |
*/ |
| 4851 |
for (unsigned i = 0; i < MESA_SHADER_STAGES; i++) { |
| 4852 |
if (prog->_LinkedShaders[i] == NULL) |
| 4853 |
continue; |
| 4854 |
|
| 4855 |
detect_recursion_linked(prog, prog->_LinkedShaders[i]->ir); |
| 4856 |
if (!prog->data->LinkStatus) |
| 4857 |
goto done; |
| 4858 |
|
| 4859 |
if (ctx->Const.ShaderCompilerOptions[i].LowerCombinedClipCullDistance) { |
| 4860 |
lower_clip_cull_distance(prog, prog->_LinkedShaders[i]); |
| 4861 |
} |
| 4862 |
|
| 4863 |
if (ctx->Const.LowerTessLevel) { |
| 4864 |
lower_tess_level(prog->_LinkedShaders[i]); |
| 4865 |
} |
| 4866 |
|
| 4867 |
/* Section 13.46 (Vertex Attribute Aliasing) of the OpenGL ES 3.2 |
| 4868 |
* specification says: |
| 4869 |
* |
| 4870 |
* "In general, the behavior of GLSL ES should not depend on compiler |
| 4871 |
* optimizations which might be implementation-dependent. Name matching |
| 4872 |
* rules in most languages, including C++ from which GLSL ES is derived, |
| 4873 |
* are based on declarations rather than use. |
| 4874 |
* |
| 4875 |
* RESOLUTION: The existence of aliasing is determined by declarations |
| 4876 |
* present after preprocessing." |
| 4877 |
* |
| 4878 |
* Because of this rule, we do a 'dry-run' of attribute assignment for |
| 4879 |
* vertex shader inputs here. |
| 4880 |
*/ |
| 4881 |
if (prog->IsES && i == MESA_SHADER_VERTEX) { |
| 4882 |
if (!assign_attribute_or_color_locations(mem_ctx, prog, &ctx->Const, |
| 4883 |
MESA_SHADER_VERTEX, false)) { |
| 4884 |
goto done; |
| 4885 |
} |
| 4886 |
} |
| 4887 |
|
| 4888 |
/* Call opts before lowering const arrays to uniforms so we can const |
| 4889 |
* propagate any elements accessed directly. |
| 4890 |
*/ |
| 4891 |
linker_optimisation_loop(ctx, prog->_LinkedShaders[i]->ir, i); |
| 4892 |
|
| 4893 |
/* Call opts after lowering const arrays to copy propagate things. */ |
| 4894 |
if (ctx->Const.GLSLLowerConstArrays && |
| 4895 |
lower_const_arrays_to_uniforms(prog->_LinkedShaders[i]->ir, i, |
| 4896 |
ctx->Const.Program[i].MaxUniformComponents)) |
| 4897 |
linker_optimisation_loop(ctx, prog->_LinkedShaders[i]->ir, i); |
| 4898 |
|
| 4899 |
} |
| 4900 |
|
| 4901 |
/* Validation for special cases where we allow sampler array indexing |
| 4902 |
* with loop induction variable. This check emits a warning or error |
| 4903 |
* depending if backend can handle dynamic indexing. |
| 4904 |
*/ |
| 4905 |
if ((!prog->IsES && prog->data->Version < 130) || |
| 4906 |
(prog->IsES && prog->data->Version < 300)) { |
| 4907 |
if (!validate_sampler_array_indexing(ctx, prog)) |
| 4908 |
goto done; |
| 4909 |
} |
| 4910 |
|
| 4911 |
/* Check and validate stream emissions in geometry shaders */ |
| 4912 |
validate_geometry_shader_emissions(ctx, prog); |
| 4913 |
|
| 4914 |
store_fragdepth_layout(prog); |
| 4915 |
|
| 4916 |
if(!link_varyings_and_uniforms(first, last, ctx, prog, mem_ctx)) |
| 4917 |
goto done; |
| 4918 |
|
| 4919 |
/* Linking varyings can cause some extra, useless swizzles to be generated |
| 4920 |
* due to packing and unpacking. |
| 4921 |
*/ |
| 4922 |
for (unsigned i = 0; i < MESA_SHADER_STAGES; i++) { |
| 4923 |
if (prog->_LinkedShaders[i] == NULL) |
| 4924 |
continue; |
| 4925 |
|
| 4926 |
optimize_swizzles(prog->_LinkedShaders[i]->ir); |
| 4927 |
} |
| 4928 |
|
| 4929 |
/* OpenGL ES < 3.1 requires that a vertex shader and a fragment shader both |
| 4930 |
* be present in a linked program. GL_ARB_ES2_compatibility doesn't say |
| 4931 |
* anything about shader linking when one of the shaders (vertex or |
| 4932 |
* fragment shader) is absent. So, the extension shouldn't change the |
| 4933 |
* behavior specified in GLSL specification. |
| 4934 |
* |
| 4935 |
* From OpenGL ES 3.1 specification (7.3 Program Objects): |
| 4936 |
* "Linking can fail for a variety of reasons as specified in the |
| 4937 |
* OpenGL ES Shading Language Specification, as well as any of the |
| 4938 |
* following reasons: |
| 4939 |
* |
| 4940 |
* ... |
| 4941 |
* |
| 4942 |
* * program contains objects to form either a vertex shader or |
| 4943 |
* fragment shader, and program is not separable, and does not |
| 4944 |
* contain objects to form both a vertex shader and fragment |
| 4945 |
* shader." |
| 4946 |
* |
| 4947 |
* However, the only scenario in 3.1+ where we don't require them both is |
| 4948 |
* when we have a compute shader. For example: |
| 4949 |
* |
| 4950 |
* - No shaders is a link error. |
| 4951 |
* - Geom or Tess without a Vertex shader is a link error which means we |
| 4952 |
* always require a Vertex shader and hence a Fragment shader. |
| 4953 |
* - Finally a Compute shader linked with any other stage is a link error. |
| 4954 |
*/ |
| 4955 |
if (!prog->SeparateShader && ctx->API == API_OPENGLES2 && |
| 4956 |
num_shaders[MESA_SHADER_COMPUTE] == 0) { |
| 4957 |
if (prog->_LinkedShaders[MESA_SHADER_VERTEX] == NULL) { |
| 4958 |
linker_error(prog, "program lacks a vertex shader\n"); |
| 4959 |
} else if (prog->_LinkedShaders[MESA_SHADER_FRAGMENT] == NULL) { |
| 4960 |
linker_error(prog, "program lacks a fragment shader\n"); |
| 4961 |
} |
| 4962 |
} |
| 4963 |
|
| 4964 |
done: |
| 4965 |
for (unsigned i = 0; i < MESA_SHADER_STAGES; i++) { |
| 4966 |
free(shader_list[i]); |
| 4967 |
if (prog->_LinkedShaders[i] == NULL) |
| 4968 |
continue; |
| 4969 |
|
| 4970 |
/* Do a final validation step to make sure that the IR wasn't |
| 4971 |
* invalidated by any modifications performed after intrastage linking. |
| 4972 |
*/ |
| 4973 |
validate_ir_tree(prog->_LinkedShaders[i]->ir); |
| 4974 |
|
| 4975 |
/* Retain any live IR, but trash the rest. */ |
| 4976 |
reparent_ir(prog->_LinkedShaders[i]->ir, prog->_LinkedShaders[i]->ir); |
| 4977 |
|
| 4978 |
/* The symbol table in the linked shaders may contain references to |
| 4979 |
* variables that were removed (e.g., unused uniforms). Since it may |
| 4980 |
* contain junk, there is no possible valid use. Delete it and set the |
| 4981 |
* pointer to NULL. |
| 4982 |
*/ |
| 4983 |
delete prog->_LinkedShaders[i]->symbols; |
| 4984 |
prog->_LinkedShaders[i]->symbols = NULL; |
| 4985 |
} |
| 4986 |
|
| 4987 |
ralloc_free(mem_ctx); |
| 4988 |
} |