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