OSDN Git Service

mesa: fix active subroutine uniforms properly
[android-x86/external-mesa.git] / src / compiler / glsl / link_uniforms.cpp
1 /*
2  * Copyright © 2011 Intel Corporation
3  *
4  * Permission is hereby granted, free of charge, to any person obtaining a
5  * copy of this software and associated documentation files (the "Software"),
6  * to deal in the Software without restriction, including without limitation
7  * the rights to use, copy, modify, merge, publish, distribute, sublicense,
8  * and/or sell copies of the Software, and to permit persons to whom the
9  * Software is furnished to do so, subject to the following conditions:
10  *
11  * The above copyright notice and this permission notice (including the next
12  * paragraph) shall be included in all copies or substantial portions of the
13  * Software.
14  *
15  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
18  * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
20  * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
21  * DEALINGS IN THE SOFTWARE.
22  */
23
24 #include "main/core.h"
25 #include "ir.h"
26 #include "linker.h"
27 #include "ir_uniform.h"
28 #include "glsl_symbol_table.h"
29 #include "program.h"
30 #include "util/string_to_uint_map.h"
31 #include "ir_variable_refcount.h"
32
33 /**
34  * \file link_uniforms.cpp
35  * Assign locations for GLSL uniforms.
36  *
37  * \author Ian Romanick <ian.d.romanick@intel.com>
38  */
39
40 /**
41  * Used by linker to indicate uniforms that have no location set.
42  */
43 #define UNMAPPED_UNIFORM_LOC ~0u
44
45 /**
46  * Count the backing storage requirements for a type
47  */
48 static unsigned
49 values_for_type(const glsl_type *type)
50 {
51    if (type->is_sampler()) {
52       return 1;
53    } else if (type->is_array() && type->fields.array->is_sampler()) {
54       return type->array_size();
55    } else {
56       return type->component_slots();
57    }
58 }
59
60 void
61 program_resource_visitor::process(const glsl_type *type, const char *name)
62 {
63    assert(type->without_array()->is_record()
64           || type->without_array()->is_interface());
65
66    unsigned record_array_count = 1;
67    char *name_copy = ralloc_strdup(NULL, name);
68    enum glsl_interface_packing packing = type->get_interface_packing();
69
70    recursion(type, &name_copy, strlen(name), false, NULL, packing, false,
71              record_array_count, NULL);
72    ralloc_free(name_copy);
73 }
74
75 void
76 program_resource_visitor::process(ir_variable *var)
77 {
78    unsigned record_array_count = 1;
79    const bool row_major =
80       var->data.matrix_layout == GLSL_MATRIX_LAYOUT_ROW_MAJOR;
81
82    const enum glsl_interface_packing packing = var->get_interface_type() ?
83       var->get_interface_type_packing() :
84       var->type->get_interface_packing();
85
86    const glsl_type *t =
87       var->data.from_named_ifc_block ? var->get_interface_type() : var->type;
88    const glsl_type *t_without_array = t->without_array();
89
90    /* false is always passed for the row_major parameter to the other
91     * processing functions because no information is available to do
92     * otherwise.  See the warning in linker.h.
93     */
94    if (t_without_array->is_record() ||
95               (t->is_array() && t->fields.array->is_array())) {
96       char *name = ralloc_strdup(NULL, var->name);
97       recursion(var->type, &name, strlen(name), row_major, NULL, packing,
98                 false, record_array_count, NULL);
99       ralloc_free(name);
100    } else if (t_without_array->is_interface()) {
101       char *name = ralloc_strdup(NULL, t_without_array->name);
102       const glsl_struct_field *ifc_member = var->data.from_named_ifc_block ?
103          &t_without_array->
104             fields.structure[t_without_array->field_index(var->name)] : NULL;
105
106       recursion(t, &name, strlen(name), row_major, NULL, packing,
107                 false, record_array_count, ifc_member);
108       ralloc_free(name);
109    } else {
110       this->set_record_array_count(record_array_count);
111       this->visit_field(t, var->name, row_major, NULL, packing, false);
112    }
113 }
114
115 void
116 program_resource_visitor::recursion(const glsl_type *t, char **name,
117                                     size_t name_length, bool row_major,
118                                     const glsl_type *record_type,
119                                     const enum glsl_interface_packing packing,
120                                     bool last_field,
121                                     unsigned record_array_count,
122                                     const glsl_struct_field *named_ifc_member)
123 {
124    /* Records need to have each field processed individually.
125     *
126     * Arrays of records need to have each array element processed
127     * individually, then each field of the resulting array elements processed
128     * individually.
129     */
130    if (t->is_interface() && named_ifc_member) {
131       ralloc_asprintf_rewrite_tail(name, &name_length, ".%s",
132                                    named_ifc_member->name);
133       recursion(named_ifc_member->type, name, name_length, row_major, NULL,
134                 packing, false, record_array_count, NULL);
135    } else if (t->is_record() || t->is_interface()) {
136       if (record_type == NULL && t->is_record())
137          record_type = t;
138
139       if (t->is_record())
140          this->enter_record(t, *name, row_major, packing);
141
142       for (unsigned i = 0; i < t->length; i++) {
143          const char *field = t->fields.structure[i].name;
144          size_t new_length = name_length;
145
146          if (t->fields.structure[i].type->is_record())
147             this->visit_field(&t->fields.structure[i]);
148
149          if (t->is_interface() && t->fields.structure[i].offset != -1)
150             this->set_buffer_offset(t->fields.structure[i].offset);
151
152          /* Append '.field' to the current variable name. */
153          if (name_length == 0) {
154             ralloc_asprintf_rewrite_tail(name, &new_length, "%s", field);
155          } else {
156             ralloc_asprintf_rewrite_tail(name, &new_length, ".%s", field);
157          }
158
159          /* The layout of structures at the top level of the block is set
160           * during parsing.  For matrices contained in multiple levels of
161           * structures in the block, the inner structures have no layout.
162           * These cases must potentially inherit the layout from the outer
163           * levels.
164           */
165          bool field_row_major = row_major;
166          const enum glsl_matrix_layout matrix_layout =
167             glsl_matrix_layout(t->fields.structure[i].matrix_layout);
168          if (matrix_layout == GLSL_MATRIX_LAYOUT_ROW_MAJOR) {
169             field_row_major = true;
170          } else if (matrix_layout == GLSL_MATRIX_LAYOUT_COLUMN_MAJOR) {
171             field_row_major = false;
172          }
173
174          recursion(t->fields.structure[i].type, name, new_length,
175                    field_row_major,
176                    record_type,
177                    packing,
178                    (i + 1) == t->length, record_array_count, NULL);
179
180          /* Only the first leaf-field of the record gets called with the
181           * record type pointer.
182           */
183          record_type = NULL;
184       }
185
186       if (t->is_record()) {
187          (*name)[name_length] = '\0';
188          this->leave_record(t, *name, row_major, packing);
189       }
190    } else if (t->without_array()->is_record() ||
191               t->without_array()->is_interface() ||
192               (t->is_array() && t->fields.array->is_array())) {
193       if (record_type == NULL && t->fields.array->is_record())
194          record_type = t->fields.array;
195
196       unsigned length = t->length;
197       /* Shader storage block unsized arrays: add subscript [0] to variable
198        * names */
199       if (t->is_unsized_array())
200          length = 1;
201
202       record_array_count *= length;
203
204       for (unsigned i = 0; i < length; i++) {
205          size_t new_length = name_length;
206
207          /* Append the subscript to the current variable name */
208          ralloc_asprintf_rewrite_tail(name, &new_length, "[%u]", i);
209
210          recursion(t->fields.array, name, new_length, row_major,
211                    record_type,
212                    packing,
213                    (i + 1) == t->length, record_array_count,
214                    named_ifc_member);
215
216          /* Only the first leaf-field of the record gets called with the
217           * record type pointer.
218           */
219          record_type = NULL;
220       }
221    } else {
222       this->set_record_array_count(record_array_count);
223       this->visit_field(t, *name, row_major, record_type, packing, last_field);
224    }
225 }
226
227 void
228 program_resource_visitor::visit_field(const glsl_struct_field *)
229 {
230 }
231
232 void
233 program_resource_visitor::enter_record(const glsl_type *, const char *, bool,
234                                        const enum glsl_interface_packing)
235 {
236 }
237
238 void
239 program_resource_visitor::leave_record(const glsl_type *, const char *, bool,
240                                        const enum glsl_interface_packing)
241 {
242 }
243
244 void
245 program_resource_visitor::set_buffer_offset(unsigned)
246 {
247 }
248
249 void
250 program_resource_visitor::set_record_array_count(unsigned)
251 {
252 }
253
254 namespace {
255
256 /**
257  * Class to help calculate the storage requirements for a set of uniforms
258  *
259  * As uniforms are added to the active set the number of active uniforms and
260  * the storage requirements for those uniforms are accumulated.  The active
261  * uniforms are added to the hash table supplied to the constructor.
262  *
263  * If the same uniform is added multiple times (i.e., once for each shader
264  * target), it will only be accounted once.
265  */
266 class count_uniform_size : public program_resource_visitor {
267 public:
268    count_uniform_size(struct string_to_uint_map *map,
269                       struct string_to_uint_map *hidden_map)
270       : num_active_uniforms(0), num_hidden_uniforms(0), num_values(0),
271         num_shader_samplers(0), num_shader_images(0),
272         num_shader_uniform_components(0), num_shader_subroutines(0),
273         is_buffer_block(false), is_shader_storage(false), map(map),
274         hidden_map(hidden_map)
275    {
276       /* empty */
277    }
278
279    void start_shader()
280    {
281       this->num_shader_samplers = 0;
282       this->num_shader_images = 0;
283       this->num_shader_uniform_components = 0;
284       this->num_shader_subroutines = 0;
285    }
286
287    void process(ir_variable *var)
288    {
289       this->current_var = var;
290       this->is_buffer_block = var->is_in_buffer_block();
291       this->is_shader_storage = var->is_in_shader_storage_block();
292       if (var->is_interface_instance())
293          program_resource_visitor::process(var->get_interface_type(),
294                                            var->get_interface_type()->name);
295       else
296          program_resource_visitor::process(var);
297    }
298
299    /**
300     * Total number of active uniforms counted
301     */
302    unsigned num_active_uniforms;
303
304    unsigned num_hidden_uniforms;
305
306    /**
307     * Number of data values required to back the storage for the active uniforms
308     */
309    unsigned num_values;
310
311    /**
312     * Number of samplers used
313     */
314    unsigned num_shader_samplers;
315
316    /**
317     * Number of images used
318     */
319    unsigned num_shader_images;
320
321    /**
322     * Number of uniforms used in the current shader
323     */
324    unsigned num_shader_uniform_components;
325
326    /**
327     * Number of subroutine uniforms used
328     */
329    unsigned num_shader_subroutines;
330
331    bool is_buffer_block;
332    bool is_shader_storage;
333
334    struct string_to_uint_map *map;
335
336 private:
337    virtual void visit_field(const glsl_type *type, const char *name,
338                             bool /* row_major */,
339                             const glsl_type * /* record_type */,
340                             const enum glsl_interface_packing,
341                             bool /* last_field */)
342    {
343       assert(!type->without_array()->is_record());
344       assert(!type->without_array()->is_interface());
345       assert(!(type->is_array() && type->fields.array->is_array()));
346
347       /* Count the number of samplers regardless of whether the uniform is
348        * already in the hash table.  The hash table prevents adding the same
349        * uniform for multiple shader targets, but in this case we want to
350        * count it for each shader target.
351        */
352       const unsigned values = values_for_type(type);
353       if (type->contains_subroutine()) {
354          this->num_shader_subroutines += values;
355       } else if (type->contains_sampler()) {
356          this->num_shader_samplers += values;
357       } else if (type->contains_image()) {
358          this->num_shader_images += values;
359
360          /* As drivers are likely to represent image uniforms as
361           * scalar indices, count them against the limit of uniform
362           * components in the default block.  The spec allows image
363           * uniforms to use up no more than one scalar slot.
364           */
365          if (!is_shader_storage)
366             this->num_shader_uniform_components += values;
367       } else {
368          /* Accumulate the total number of uniform slots used by this shader.
369           * Note that samplers do not count against this limit because they
370           * don't use any storage on current hardware.
371           */
372          if (!is_buffer_block)
373             this->num_shader_uniform_components += values;
374       }
375
376       /* If the uniform is already in the map, there's nothing more to do.
377        */
378       unsigned id;
379       if (this->map->get(id, name))
380          return;
381
382       if (this->current_var->data.how_declared == ir_var_hidden) {
383          this->hidden_map->put(this->num_hidden_uniforms, name);
384          this->num_hidden_uniforms++;
385       } else {
386          this->map->put(this->num_active_uniforms-this->num_hidden_uniforms,
387                         name);
388       }
389
390       /* Each leaf uniform occupies one entry in the list of active
391        * uniforms.
392        */
393       this->num_active_uniforms++;
394
395       if(!is_gl_identifier(name) && !is_shader_storage && !is_buffer_block)
396          this->num_values += values;
397    }
398
399    struct string_to_uint_map *hidden_map;
400
401    /**
402     * Current variable being processed.
403     */
404    ir_variable *current_var;
405 };
406
407 } /* anonymous namespace */
408
409 /**
410  * Class to help parcel out pieces of backing storage to uniforms
411  *
412  * Each uniform processed has some range of the \c gl_constant_value
413  * structures associated with it.  The association is done by finding
414  * the uniform in the \c string_to_uint_map and using the value from
415  * the map to connect that slot in the \c gl_uniform_storage table
416  * with the next available slot in the \c gl_constant_value array.
417  *
418  * \warning
419  * This class assumes that every uniform that will be processed is
420  * already in the \c string_to_uint_map.  In addition, it assumes that
421  * the \c gl_uniform_storage and \c gl_constant_value arrays are "big
422  * enough."
423  */
424 class parcel_out_uniform_storage : public program_resource_visitor {
425 public:
426    parcel_out_uniform_storage(struct gl_shader_program *prog,
427                               struct string_to_uint_map *map,
428                               struct gl_uniform_storage *uniforms,
429                               union gl_constant_value *values)
430       : prog(prog), map(map), uniforms(uniforms), values(values)
431    {
432    }
433
434    void start_shader(gl_shader_stage shader_type)
435    {
436       assert(shader_type < MESA_SHADER_STAGES);
437       this->shader_type = shader_type;
438
439       this->shader_samplers_used = 0;
440       this->shader_shadow_samplers = 0;
441       this->next_sampler = 0;
442       this->next_image = 0;
443       this->next_subroutine = 0;
444       this->record_array_count = 1;
445       memset(this->targets, 0, sizeof(this->targets));
446    }
447
448    void set_and_process(ir_variable *var)
449    {
450       current_var = var;
451       field_counter = 0;
452       this->record_next_sampler = new string_to_uint_map;
453
454       buffer_block_index = -1;
455       if (var->is_in_buffer_block()) {
456          struct gl_uniform_block *blks = var->is_in_shader_storage_block() ?
457             prog->data->ShaderStorageBlocks : prog->data->UniformBlocks;
458          unsigned num_blks = var->is_in_shader_storage_block() ?
459             prog->data->NumShaderStorageBlocks : prog->data->NumUniformBlocks;
460
461          if (var->is_interface_instance() && var->type->is_array()) {
462             unsigned l = strlen(var->get_interface_type()->name);
463
464             for (unsigned i = 0; i < num_blks; i++) {
465                if (strncmp(var->get_interface_type()->name, blks[i].Name, l)
466                    == 0 && blks[i].Name[l] == '[') {
467                   buffer_block_index = i;
468                   break;
469                }
470             }
471          } else {
472             for (unsigned i = 0; i < num_blks; i++) {
473                if (strcmp(var->get_interface_type()->name, blks[i].Name) ==
474                    0) {
475                   buffer_block_index = i;
476                   break;
477                }
478             }
479          }
480          assert(buffer_block_index != -1);
481
482          /* Uniform blocks that were specified with an instance name must be
483           * handled a little bit differently.  The name of the variable is the
484           * name used to reference the uniform block instead of being the name
485           * of a variable within the block.  Therefore, searching for the name
486           * within the block will fail.
487           */
488          if (var->is_interface_instance()) {
489             ubo_byte_offset = 0;
490             process(var->get_interface_type(),
491                     var->get_interface_type()->name);
492          } else {
493             const struct gl_uniform_block *const block =
494                &blks[buffer_block_index];
495
496             assert(var->data.location != -1);
497
498             const struct gl_uniform_buffer_variable *const ubo_var =
499                &block->Uniforms[var->data.location];
500
501             ubo_byte_offset = ubo_var->Offset;
502             process(var);
503          }
504       } else {
505          /* Store any explicit location and reset data location so we can
506           * reuse this variable for storing the uniform slot number.
507           */
508          this->explicit_location = current_var->data.location;
509          current_var->data.location = -1;
510
511          process(var);
512       }
513       delete this->record_next_sampler;
514    }
515
516    int buffer_block_index;
517    int ubo_byte_offset;
518    gl_shader_stage shader_type;
519
520 private:
521    void handle_samplers(const glsl_type *base_type,
522                         struct gl_uniform_storage *uniform, const char *name)
523    {
524       if (base_type->is_sampler()) {
525          uniform->opaque[shader_type].active = true;
526
527          /* Handle multiple samplers inside struct arrays */
528          if (this->record_array_count > 1) {
529             unsigned inner_array_size = MAX2(1, uniform->array_elements);
530             char *name_copy = ralloc_strdup(NULL, name);
531
532             /* Remove all array subscripts from the sampler name */
533             char *str_start;
534             const char *str_end;
535             while((str_start = strchr(name_copy, '[')) &&
536                   (str_end = strchr(name_copy, ']'))) {
537                memmove(str_start, str_end + 1, 1 + strlen(str_end));
538             }
539
540             unsigned index = 0;
541             if (this->record_next_sampler->get(index, name_copy)) {
542                /* In this case, we've already seen this uniform so we just use
543                 * the next sampler index recorded the last time we visited.
544                 */
545                uniform->opaque[shader_type].index = index;
546                index = inner_array_size + uniform->opaque[shader_type].index;
547                this->record_next_sampler->put(index, name_copy);
548
549                ralloc_free(name_copy);
550                /* Return as everything else has already been initialised in a
551                 * previous pass.
552                 */
553                return;
554             } else {
555                /* We've never seen this uniform before so we need to allocate
556                 * enough indices to store it.
557                 *
558                 * Nested struct arrays behave like arrays of arrays so we need
559                 * to increase the index by the total number of elements of the
560                 * sampler in case there is more than one sampler inside the
561                 * structs. This allows the offset to be easily calculated for
562                 * indirect indexing.
563                 */
564                uniform->opaque[shader_type].index = this->next_sampler;
565                this->next_sampler +=
566                   inner_array_size * this->record_array_count;
567
568                /* Store the next index for future passes over the struct array
569                 */
570                index = uniform->opaque[shader_type].index + inner_array_size;
571                this->record_next_sampler->put(index, name_copy);
572                ralloc_free(name_copy);
573             }
574          } else {
575             /* Increment the sampler by 1 for non-arrays and by the number of
576              * array elements for arrays.
577              */
578             uniform->opaque[shader_type].index = this->next_sampler;
579             this->next_sampler += MAX2(1, uniform->array_elements);
580          }
581
582          const gl_texture_index target = base_type->sampler_index();
583          const unsigned shadow = base_type->sampler_shadow;
584          for (unsigned i = uniform->opaque[shader_type].index;
585               i < MIN2(this->next_sampler, MAX_SAMPLERS);
586               i++) {
587             this->targets[i] = target;
588             this->shader_samplers_used |= 1U << i;
589             this->shader_shadow_samplers |= shadow << i;
590          }
591       }
592    }
593
594    void handle_images(const glsl_type *base_type,
595                       struct gl_uniform_storage *uniform)
596    {
597       if (base_type->is_image()) {
598          uniform->opaque[shader_type].index = this->next_image;
599          uniform->opaque[shader_type].active = true;
600
601          /* Set image access qualifiers */
602          const GLenum access =
603             (current_var->data.image_read_only ? GL_READ_ONLY :
604              current_var->data.image_write_only ? GL_WRITE_ONLY :
605                 GL_READ_WRITE);
606
607          const unsigned first = this->next_image;
608
609          /* Increment the image index by 1 for non-arrays and by the
610           * number of array elements for arrays.
611           */
612          this->next_image += MAX2(1, uniform->array_elements);
613
614          for (unsigned i = first; i < MIN2(next_image, MAX_IMAGE_UNIFORMS); i++)
615             prog->_LinkedShaders[shader_type]->ImageAccess[i] = access;
616       }
617    }
618
619    void handle_subroutines(const glsl_type *base_type,
620                            struct gl_uniform_storage *uniform)
621    {
622       if (base_type->is_subroutine()) {
623          uniform->opaque[shader_type].index = this->next_subroutine;
624          uniform->opaque[shader_type].active = true;
625
626          prog->_LinkedShaders[shader_type]->NumSubroutineUniforms++;
627
628          /* Increment the subroutine index by 1 for non-arrays and by the
629           * number of array elements for arrays.
630           */
631          this->next_subroutine += MAX2(1, uniform->array_elements);
632
633       }
634    }
635
636    virtual void set_buffer_offset(unsigned offset)
637    {
638       this->ubo_byte_offset = offset;
639    }
640
641    virtual void set_record_array_count(unsigned record_array_count)
642    {
643       this->record_array_count = record_array_count;
644    }
645
646    virtual void enter_record(const glsl_type *type, const char *,
647                              bool row_major,
648                              const enum glsl_interface_packing packing)
649    {
650       assert(type->is_record());
651       if (this->buffer_block_index == -1)
652          return;
653       if (packing == GLSL_INTERFACE_PACKING_STD430)
654          this->ubo_byte_offset = glsl_align(
655             this->ubo_byte_offset, type->std430_base_alignment(row_major));
656       else
657          this->ubo_byte_offset = glsl_align(
658             this->ubo_byte_offset, type->std140_base_alignment(row_major));
659    }
660
661    virtual void leave_record(const glsl_type *type, const char *,
662                              bool row_major,
663                              const enum glsl_interface_packing packing)
664    {
665       assert(type->is_record());
666       if (this->buffer_block_index == -1)
667          return;
668       if (packing == GLSL_INTERFACE_PACKING_STD430)
669          this->ubo_byte_offset = glsl_align(
670             this->ubo_byte_offset, type->std430_base_alignment(row_major));
671       else
672          this->ubo_byte_offset = glsl_align(
673             this->ubo_byte_offset, type->std140_base_alignment(row_major));
674    }
675
676    virtual void visit_field(const glsl_type *type, const char *name,
677                             bool row_major, const glsl_type * /* record_type */,
678                             const enum glsl_interface_packing packing,
679                             bool /* last_field */)
680    {
681       assert(!type->without_array()->is_record());
682       assert(!type->without_array()->is_interface());
683       assert(!(type->is_array() && type->fields.array->is_array()));
684
685       unsigned id;
686       bool found = this->map->get(id, name);
687       assert(found);
688
689       if (!found)
690          return;
691
692       const glsl_type *base_type;
693       if (type->is_array()) {
694          this->uniforms[id].array_elements = type->length;
695          base_type = type->fields.array;
696       } else {
697          this->uniforms[id].array_elements = 0;
698          base_type = type;
699       }
700
701       /* Initialise opaque data */
702       this->uniforms[id].opaque[shader_type].index = ~0;
703       this->uniforms[id].opaque[shader_type].active = false;
704
705       /* This assigns uniform indices to sampler and image uniforms. */
706       handle_samplers(base_type, &this->uniforms[id], name);
707       handle_images(base_type, &this->uniforms[id]);
708       handle_subroutines(base_type, &this->uniforms[id]);
709
710       /* For array of arrays or struct arrays the base location may have
711        * already been set so don't set it again.
712        */
713       if (buffer_block_index == -1 && current_var->data.location == -1) {
714          current_var->data.location = id;
715       }
716
717       /* If there is already storage associated with this uniform or if the
718        * uniform is set as builtin, it means that it was set while processing
719        * an earlier shader stage.  For example, we may be processing the
720        * uniform in the fragment shader, but the uniform was already processed
721        * in the vertex shader.
722        */
723       if (this->uniforms[id].storage != NULL || this->uniforms[id].builtin) {
724          return;
725       }
726
727       /* Assign explicit locations. */
728       if (current_var->data.explicit_location) {
729          /* Set sequential locations for struct fields. */
730          if (current_var->type->without_array()->is_record() ||
731              current_var->type->is_array_of_arrays()) {
732             const unsigned entries = MAX2(1, this->uniforms[id].array_elements);
733             this->uniforms[id].remap_location =
734                this->explicit_location + field_counter;
735             field_counter += entries;
736          } else {
737             this->uniforms[id].remap_location = this->explicit_location;
738          }
739       } else {
740          /* Initialize to to indicate that no location is set */
741          this->uniforms[id].remap_location = UNMAPPED_UNIFORM_LOC;
742       }
743
744       this->uniforms[id].name = ralloc_strdup(this->uniforms, name);
745       this->uniforms[id].type = base_type;
746       this->uniforms[id].num_driver_storage = 0;
747       this->uniforms[id].driver_storage = NULL;
748       this->uniforms[id].atomic_buffer_index = -1;
749       this->uniforms[id].hidden =
750          current_var->data.how_declared == ir_var_hidden;
751       this->uniforms[id].builtin = is_gl_identifier(name);
752
753       this->uniforms[id].is_shader_storage =
754          current_var->is_in_shader_storage_block();
755
756       /* Do not assign storage if the uniform is a builtin or buffer object */
757       if (!this->uniforms[id].builtin &&
758           !this->uniforms[id].is_shader_storage &&
759           this->buffer_block_index == -1)
760          this->uniforms[id].storage = this->values;
761
762       if (this->buffer_block_index != -1) {
763          this->uniforms[id].block_index = this->buffer_block_index;
764
765          unsigned alignment = type->std140_base_alignment(row_major);
766          if (packing == GLSL_INTERFACE_PACKING_STD430)
767             alignment = type->std430_base_alignment(row_major);
768          this->ubo_byte_offset = glsl_align(this->ubo_byte_offset, alignment);
769          this->uniforms[id].offset = this->ubo_byte_offset;
770          if (packing == GLSL_INTERFACE_PACKING_STD430)
771             this->ubo_byte_offset += type->std430_size(row_major);
772          else
773             this->ubo_byte_offset += type->std140_size(row_major);
774
775          if (type->is_array()) {
776             if (packing == GLSL_INTERFACE_PACKING_STD430)
777                this->uniforms[id].array_stride =
778                   type->without_array()->std430_array_stride(row_major);
779             else
780                this->uniforms[id].array_stride =
781                   glsl_align(type->without_array()->std140_size(row_major),
782                              16);
783          } else {
784             this->uniforms[id].array_stride = 0;
785          }
786
787          if (type->without_array()->is_matrix()) {
788             const glsl_type *matrix = type->without_array();
789             const unsigned N = matrix->base_type == GLSL_TYPE_DOUBLE ? 8 : 4;
790             const unsigned items =
791                row_major ? matrix->matrix_columns : matrix->vector_elements;
792
793             assert(items <= 4);
794             if (packing == GLSL_INTERFACE_PACKING_STD430)
795                this->uniforms[id].matrix_stride = items < 3 ? items * N :
796                                                     glsl_align(items * N, 16);
797             else
798                this->uniforms[id].matrix_stride = glsl_align(items * N, 16);
799             this->uniforms[id].row_major = row_major;
800          } else {
801             this->uniforms[id].matrix_stride = 0;
802             this->uniforms[id].row_major = false;
803          }
804       } else {
805          this->uniforms[id].block_index = -1;
806          this->uniforms[id].offset = -1;
807          this->uniforms[id].array_stride = -1;
808          this->uniforms[id].matrix_stride = -1;
809          this->uniforms[id].row_major = false;
810       }
811
812       if (!this->uniforms[id].builtin &&
813           !this->uniforms[id].is_shader_storage &&
814           this->buffer_block_index == -1)
815          this->values += values_for_type(type);
816    }
817
818    /**
819     * Current program being processed.
820     */
821    struct gl_shader_program *prog;
822
823    struct string_to_uint_map *map;
824
825    struct gl_uniform_storage *uniforms;
826    unsigned next_sampler;
827    unsigned next_image;
828    unsigned next_subroutine;
829
830    /**
831     * Field counter is used to take care that uniform structures
832     * with explicit locations get sequential locations.
833     */
834    unsigned field_counter;
835
836    /**
837     * Current variable being processed.
838     */
839    ir_variable *current_var;
840
841    /* Used to store the explicit location from current_var so that we can
842     * reuse the location field for storing the uniform slot id.
843     */
844    int explicit_location;
845
846    /* Stores total struct array elements including nested structs */
847    unsigned record_array_count;
848
849    /* Map for temporarily storing next sampler index when handling samplers in
850     * struct arrays.
851     */
852    struct string_to_uint_map *record_next_sampler;
853
854 public:
855    union gl_constant_value *values;
856
857    gl_texture_index targets[MAX_SAMPLERS];
858
859    /**
860     * Mask of samplers used by the current shader stage.
861     */
862    unsigned shader_samplers_used;
863
864    /**
865     * Mask of samplers used by the current shader stage for shadows.
866     */
867    unsigned shader_shadow_samplers;
868 };
869
870 static bool
871 variable_is_referenced(ir_variable_refcount_visitor &v, ir_variable *var)
872 {
873    ir_variable_refcount_entry *const entry = v.get_variable_entry(var);
874
875    return entry->referenced_count > 0;
876
877 }
878
879 /**
880  * Walks the IR and update the references to uniform blocks in the
881  * ir_variables to point at linked shader's list (previously, they
882  * would point at the uniform block list in one of the pre-linked
883  * shaders).
884  */
885 static void
886 link_update_uniform_buffer_variables(struct gl_linked_shader *shader,
887                                      unsigned stage)
888 {
889    ir_variable_refcount_visitor v;
890
891    v.run(shader->ir);
892
893    foreach_in_list(ir_instruction, node, shader->ir) {
894       ir_variable *const var = node->as_variable();
895
896       if (var == NULL || !var->is_in_buffer_block())
897          continue;
898
899       assert(var->data.mode == ir_var_uniform ||
900              var->data.mode == ir_var_shader_storage);
901
902       unsigned num_blocks = var->data.mode == ir_var_uniform ?
903          shader->NumUniformBlocks : shader->NumShaderStorageBlocks;
904       struct gl_uniform_block **blks = var->data.mode == ir_var_uniform ?
905          shader->UniformBlocks : shader->ShaderStorageBlocks;
906
907       if (var->is_interface_instance()) {
908          if (variable_is_referenced(v, var)) {
909             /* Since this is an interface instance, the instance type will be
910              * same as the array-stripped variable type.  If the variable type
911              * is an array, then the block names will be suffixed with [0]
912              * through [n-1].  Unlike for non-interface instances, there will
913              * not be structure types here, so the only name sentinel that we
914              * have to worry about is [.
915              */
916             assert(var->type->without_array() == var->get_interface_type());
917             const char sentinel = var->type->is_array() ? '[' : '\0';
918
919             const ptrdiff_t len = strlen(var->get_interface_type()->name);
920             for (unsigned i = 0; i < num_blocks; i++) {
921                const char *const begin = blks[i]->Name;
922                const char *const end = strchr(begin, sentinel);
923
924                if (end == NULL)
925                   continue;
926
927                if (len != (end - begin))
928                   continue;
929
930                /* Even when a match is found, do not "break" here.  This could
931                 * be an array of instances, and all elements of the array need
932                 * to be marked as referenced.
933                 */
934                if (strncmp(begin, var->get_interface_type()->name, len) == 0) {
935                   blks[i]->stageref |= 1U << stage;
936                }
937             }
938          }
939
940          var->data.location = 0;
941          continue;
942       }
943
944       bool found = false;
945       char sentinel = '\0';
946
947       if (var->type->is_record()) {
948          sentinel = '.';
949       } else if (var->type->is_array() && (var->type->fields.array->is_array()
950                  || var->type->without_array()->is_record())) {
951          sentinel = '[';
952       }
953
954       const unsigned l = strlen(var->name);
955       for (unsigned i = 0; i < num_blocks; i++) {
956          for (unsigned j = 0; j < blks[i]->NumUniforms; j++) {
957             if (sentinel) {
958                const char *begin = blks[i]->Uniforms[j].Name;
959                const char *end = strchr(begin, sentinel);
960
961                if (end == NULL)
962                   continue;
963
964                if ((ptrdiff_t) l != (end - begin))
965                   continue;
966
967                found = strncmp(var->name, begin, l) == 0;
968             } else {
969                found = strcmp(var->name, blks[i]->Uniforms[j].Name) == 0;
970             }
971
972             if (found) {
973                var->data.location = j;
974
975                if (variable_is_referenced(v, var))
976                   blks[i]->stageref |= 1U << stage;
977
978                break;
979             }
980          }
981
982          if (found)
983             break;
984       }
985       assert(found);
986    }
987 }
988
989 /**
990  * Combine the hidden uniform hash map with the uniform hash map so that the
991  * hidden uniforms will be given indicies at the end of the uniform storage
992  * array.
993  */
994 static void
995 assign_hidden_uniform_slot_id(const char *name, unsigned hidden_id,
996                               void *closure)
997 {
998    count_uniform_size *uniform_size = (count_uniform_size *) closure;
999    unsigned hidden_uniform_start = uniform_size->num_active_uniforms -
1000       uniform_size->num_hidden_uniforms;
1001
1002    uniform_size->map->put(hidden_uniform_start + hidden_id, name);
1003 }
1004
1005 /**
1006  * Search through the list of empty blocks to find one that fits the current
1007  * uniform.
1008  */
1009 static int
1010 find_empty_block(struct gl_shader_program *prog,
1011                  struct gl_uniform_storage *uniform)
1012 {
1013    const unsigned entries = MAX2(1, uniform->array_elements);
1014
1015    foreach_list_typed(struct empty_uniform_block, block, link,
1016                       &prog->EmptyUniformLocations) {
1017       /* Found a block with enough slots to fit the uniform */
1018       if (block->slots == entries) {
1019          unsigned start = block->start;
1020          exec_node_remove(&block->link);
1021          ralloc_free(block);
1022
1023          return start;
1024       /* Found a block with more slots than needed. It can still be used. */
1025       } else if (block->slots > entries) {
1026          unsigned start = block->start;
1027          block->start += entries;
1028          block->slots -= entries;
1029
1030          return start;
1031       }
1032    }
1033
1034    return -1;
1035 }
1036
1037 static void
1038 link_setup_uniform_remap_tables(struct gl_context *ctx,
1039                                 struct gl_shader_program *prog,
1040                                 unsigned num_explicit_uniform_locs)
1041 {
1042    unsigned total_entries = num_explicit_uniform_locs;
1043    unsigned empty_locs =
1044       prog->NumUniformRemapTable - num_explicit_uniform_locs;
1045
1046    /* Reserve all the explicit locations of the active uniforms. */
1047    for (unsigned i = 0; i < prog->data->NumUniformStorage; i++) {
1048       if (prog->data->UniformStorage[i].type->is_subroutine() ||
1049           prog->data->UniformStorage[i].is_shader_storage)
1050          continue;
1051
1052       if (prog->data->UniformStorage[i].remap_location !=
1053           UNMAPPED_UNIFORM_LOC) {
1054          /* How many new entries for this uniform? */
1055          const unsigned entries =
1056             MAX2(1, prog->data->UniformStorage[i].array_elements);
1057
1058          /* Set remap table entries point to correct gl_uniform_storage. */
1059          for (unsigned j = 0; j < entries; j++) {
1060             unsigned element_loc =
1061                prog->data->UniformStorage[i].remap_location + j;
1062             assert(prog->UniformRemapTable[element_loc] ==
1063                    INACTIVE_UNIFORM_EXPLICIT_LOCATION);
1064             prog->UniformRemapTable[element_loc] =
1065                &prog->data->UniformStorage[i];
1066          }
1067       }
1068    }
1069
1070    /* Reserve locations for rest of the uniforms. */
1071    for (unsigned i = 0; i < prog->data->NumUniformStorage; i++) {
1072
1073       if (prog->data->UniformStorage[i].type->is_subroutine() ||
1074           prog->data->UniformStorage[i].is_shader_storage)
1075          continue;
1076
1077       /* Built-in uniforms should not get any location. */
1078       if (prog->data->UniformStorage[i].builtin)
1079          continue;
1080
1081       /* Explicit ones have been set already. */
1082       if (prog->data->UniformStorage[i].remap_location != UNMAPPED_UNIFORM_LOC)
1083          continue;
1084
1085       /* how many new entries for this uniform? */
1086       const unsigned entries =
1087          MAX2(1, prog->data->UniformStorage[i].array_elements);
1088
1089       /* Find UniformRemapTable for empty blocks where we can fit this uniform. */
1090       int chosen_location = -1;
1091
1092       if (empty_locs)
1093          chosen_location = find_empty_block(prog, &prog->data->UniformStorage[i]);
1094
1095       /* Add new entries to the total amount of entries. */
1096       total_entries += entries;
1097
1098       if (chosen_location != -1) {
1099          empty_locs -= entries;
1100       } else {
1101          chosen_location = prog->NumUniformRemapTable;
1102
1103          /* resize remap table to fit new entries */
1104          prog->UniformRemapTable =
1105             reralloc(prog,
1106                      prog->UniformRemapTable,
1107                      gl_uniform_storage *,
1108                      prog->NumUniformRemapTable + entries);
1109          prog->NumUniformRemapTable += entries;
1110       }
1111
1112       /* set pointers for this uniform */
1113       for (unsigned j = 0; j < entries; j++)
1114          prog->UniformRemapTable[chosen_location + j] =
1115             &prog->data->UniformStorage[i];
1116
1117       /* set the base location in remap table for the uniform */
1118       prog->data->UniformStorage[i].remap_location = chosen_location;
1119    }
1120
1121    /* Verify that total amount of entries for explicit and implicit locations
1122     * is less than MAX_UNIFORM_LOCATIONS.
1123     */
1124
1125    if (total_entries > ctx->Const.MaxUserAssignableUniformLocations) {
1126       linker_error(prog, "count of uniform locations > MAX_UNIFORM_LOCATIONS"
1127                    "(%u > %u)", total_entries,
1128                    ctx->Const.MaxUserAssignableUniformLocations);
1129    }
1130
1131    /* Reserve all the explicit locations of the active subroutine uniforms. */
1132    for (unsigned i = 0; i < prog->data->NumUniformStorage; i++) {
1133       if (!prog->data->UniformStorage[i].type->is_subroutine())
1134          continue;
1135
1136       if (prog->data->UniformStorage[i].remap_location == UNMAPPED_UNIFORM_LOC)
1137          continue;
1138
1139       /* How many new entries for this uniform? */
1140       const unsigned entries =
1141          MAX2(1, prog->data->UniformStorage[i].array_elements);
1142
1143       for (unsigned j = 0; j < MESA_SHADER_STAGES; j++) {
1144          struct gl_linked_shader *sh = prog->_LinkedShaders[j];
1145          if (!sh)
1146             continue;
1147
1148          if (!prog->data->UniformStorage[i].opaque[j].active)
1149             continue;
1150
1151          /* Set remap table entries point to correct gl_uniform_storage. */
1152          for (unsigned k = 0; k < entries; k++) {
1153             unsigned element_loc =
1154                prog->data->UniformStorage[i].remap_location + k;
1155             assert(sh->SubroutineUniformRemapTable[element_loc] ==
1156                    INACTIVE_UNIFORM_EXPLICIT_LOCATION);
1157             sh->SubroutineUniformRemapTable[element_loc] =
1158                &prog->data->UniformStorage[i];
1159          }
1160       }
1161    }
1162
1163    /* reserve subroutine locations */
1164    for (unsigned i = 0; i < prog->data->NumUniformStorage; i++) {
1165       if (!prog->data->UniformStorage[i].type->is_subroutine())
1166          continue;
1167
1168       if (prog->data->UniformStorage[i].remap_location !=
1169           UNMAPPED_UNIFORM_LOC)
1170          continue;
1171
1172       const unsigned entries =
1173          MAX2(1, prog->data->UniformStorage[i].array_elements);
1174
1175       for (unsigned j = 0; j < MESA_SHADER_STAGES; j++) {
1176          struct gl_linked_shader *sh = prog->_LinkedShaders[j];
1177          if (!sh)
1178             continue;
1179
1180          if (!prog->data->UniformStorage[i].opaque[j].active)
1181             continue;
1182
1183          sh->SubroutineUniformRemapTable =
1184             reralloc(sh,
1185                      sh->SubroutineUniformRemapTable,
1186                      gl_uniform_storage *,
1187                      sh->NumSubroutineUniformRemapTable + entries);
1188
1189          for (unsigned k = 0; k < entries; k++) {
1190             sh->SubroutineUniformRemapTable[sh->NumSubroutineUniformRemapTable + k] =
1191                &prog->data->UniformStorage[i];
1192          }
1193          prog->data->UniformStorage[i].remap_location =
1194             sh->NumSubroutineUniformRemapTable;
1195          sh->NumSubroutineUniformRemapTable += entries;
1196       }
1197    }
1198 }
1199
1200 static void
1201 link_assign_uniform_storage(struct gl_context *ctx,
1202                             struct gl_shader_program *prog,
1203                             const unsigned num_data_slots,
1204                             unsigned num_explicit_uniform_locs)
1205 {
1206    /* On the outside chance that there were no uniforms, bail out.
1207     */
1208    if (prog->data->NumUniformStorage == 0)
1209       return;
1210
1211    unsigned int boolean_true = ctx->Const.UniformBooleanTrue;
1212
1213    prog->data->UniformStorage = rzalloc_array(prog, struct gl_uniform_storage,
1214                                               prog->data->NumUniformStorage);
1215    union gl_constant_value *data = rzalloc_array(prog->data->UniformStorage,
1216                                                  union gl_constant_value,
1217                                                  num_data_slots);
1218 #ifndef NDEBUG
1219    union gl_constant_value *data_end = &data[num_data_slots];
1220 #endif
1221
1222    parcel_out_uniform_storage parcel(prog, prog->UniformHash,
1223                                      prog->data->UniformStorage, data);
1224
1225    for (unsigned i = 0; i < MESA_SHADER_STAGES; i++) {
1226       if (prog->_LinkedShaders[i] == NULL)
1227          continue;
1228
1229       parcel.start_shader((gl_shader_stage)i);
1230
1231       foreach_in_list(ir_instruction, node, prog->_LinkedShaders[i]->ir) {
1232          ir_variable *const var = node->as_variable();
1233
1234          if ((var == NULL) || (var->data.mode != ir_var_uniform &&
1235                                var->data.mode != ir_var_shader_storage))
1236             continue;
1237
1238          parcel.set_and_process(var);
1239       }
1240
1241       prog->_LinkedShaders[i]->active_samplers = parcel.shader_samplers_used;
1242       prog->_LinkedShaders[i]->shadow_samplers = parcel.shader_shadow_samplers;
1243
1244       STATIC_ASSERT(sizeof(prog->_LinkedShaders[i]->SamplerTargets) ==
1245                     sizeof(parcel.targets));
1246       memcpy(prog->_LinkedShaders[i]->SamplerTargets, parcel.targets,
1247              sizeof(prog->_LinkedShaders[i]->SamplerTargets));
1248    }
1249
1250 #ifndef NDEBUG
1251    for (unsigned i = 0; i < prog->data->NumUniformStorage; i++) {
1252       assert(prog->data->UniformStorage[i].storage != NULL ||
1253              prog->data->UniformStorage[i].builtin ||
1254              prog->data->UniformStorage[i].is_shader_storage ||
1255              prog->data->UniformStorage[i].block_index != -1);
1256    }
1257
1258    assert(parcel.values == data_end);
1259 #endif
1260
1261    link_setup_uniform_remap_tables(ctx, prog, num_explicit_uniform_locs);
1262
1263    link_set_uniform_initializers(prog, boolean_true);
1264 }
1265
1266 void
1267 link_assign_uniform_locations(struct gl_shader_program *prog,
1268                               struct gl_context *ctx,
1269                               unsigned int num_explicit_uniform_locs)
1270 {
1271    ralloc_free(prog->data->UniformStorage);
1272    prog->data->UniformStorage = NULL;
1273    prog->data->NumUniformStorage = 0;
1274
1275    if (prog->UniformHash != NULL) {
1276       prog->UniformHash->clear();
1277    } else {
1278       prog->UniformHash = new string_to_uint_map;
1279    }
1280
1281    /* First pass: Count the uniform resources used by the user-defined
1282     * uniforms.  While this happens, each active uniform will have an index
1283     * assigned to it.
1284     *
1285     * Note: this is *NOT* the index that is returned to the application by
1286     * glGetUniformLocation.
1287     */
1288    struct string_to_uint_map *hiddenUniforms = new string_to_uint_map;
1289    count_uniform_size uniform_size(prog->UniformHash, hiddenUniforms);
1290    for (unsigned i = 0; i < MESA_SHADER_STAGES; i++) {
1291       struct gl_linked_shader *sh = prog->_LinkedShaders[i];
1292
1293       if (sh == NULL)
1294          continue;
1295
1296       /* Uniforms that lack an initializer in the shader code have an initial
1297        * value of zero.  This includes sampler uniforms.
1298        *
1299        * Page 24 (page 30 of the PDF) of the GLSL 1.20 spec says:
1300        *
1301        *     "The link time initial value is either the value of the variable's
1302        *     initializer, if present, or 0 if no initializer is present. Sampler
1303        *     types cannot have initializers."
1304        */
1305       memset(sh->SamplerUnits, 0, sizeof(sh->SamplerUnits));
1306       memset(sh->ImageUnits, 0, sizeof(sh->ImageUnits));
1307
1308       link_update_uniform_buffer_variables(sh, i);
1309
1310       /* Reset various per-shader target counts.
1311        */
1312       uniform_size.start_shader();
1313
1314       foreach_in_list(ir_instruction, node, sh->ir) {
1315          ir_variable *const var = node->as_variable();
1316
1317          if ((var == NULL) || (var->data.mode != ir_var_uniform &&
1318                                var->data.mode != ir_var_shader_storage))
1319             continue;
1320
1321          uniform_size.process(var);
1322       }
1323
1324       sh->num_samplers = uniform_size.num_shader_samplers;
1325       sh->NumImages = uniform_size.num_shader_images;
1326       sh->num_uniform_components = uniform_size.num_shader_uniform_components;
1327       sh->num_combined_uniform_components = sh->num_uniform_components;
1328
1329       for (unsigned i = 0; i < sh->NumUniformBlocks; i++) {
1330          sh->num_combined_uniform_components +=
1331             sh->UniformBlocks[i]->UniformBufferSize / 4;
1332       }
1333    }
1334
1335    prog->data->NumUniformStorage = uniform_size.num_active_uniforms;
1336    prog->data->NumHiddenUniforms = uniform_size.num_hidden_uniforms;
1337
1338    /* assign hidden uniforms a slot id */
1339    hiddenUniforms->iterate(assign_hidden_uniform_slot_id, &uniform_size);
1340    delete hiddenUniforms;
1341
1342    link_assign_uniform_storage(ctx, prog, uniform_size.num_values,
1343                                num_explicit_uniform_locs);
1344 }