1 /*
2  * Copyright © 2012 Intel Corporation
3  *
4  * Permission is hereby granted, free of charge, to any person obtaining a
5  * copy of this software and associated documentation files (the "Software"),
6  * to deal in the Software without restriction, including without limitation
7  * the rights to use, copy, modify, merge, publish, distribute, sublicense,
8  * and/or sell copies of the Software, and to permit persons to whom the
9  * Software is furnished to do so, subject to the following conditions:
10  *
11  * The above copyright notice and this permission notice (including the next
12  * paragraph) shall be included in all copies or substantial portions of the
13  * Software.
14  *
15  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
18  * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
20  * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
21  * DEALINGS IN THE SOFTWARE.
22  */
23 
24 #include "main/core.h"
25 #include "ir.h"
26 #include "linker.h"
27 #include "ir_uniform.h"
28 #include "link_uniform_block_active_visitor.h"
29 #include "util/hash_table.h"
30 #include "program.h"
31 
32 namespace {
33 
34 class ubo_visitor : public program_resource_visitor {
35 public:
ubo_visitor(void * mem_ctx,gl_uniform_buffer_variable * variables,unsigned num_variables,struct gl_shader_program * prog)36    ubo_visitor(void *mem_ctx, gl_uniform_buffer_variable *variables,
37                unsigned num_variables, struct gl_shader_program *prog)
38       : index(0), offset(0), buffer_size(0), variables(variables),
39         num_variables(num_variables), mem_ctx(mem_ctx),
40         is_array_instance(false), prog(prog)
41    {
42       /* empty */
43    }
44 
process(const glsl_type * type,const char * name)45    void process(const glsl_type *type, const char *name)
46    {
47       this->offset = 0;
48       this->buffer_size = 0;
49       this->is_array_instance = strchr(name, ']') != NULL;
50       this->program_resource_visitor::process(type, name);
51    }
52 
53    unsigned index;
54    unsigned offset;
55    unsigned buffer_size;
56    gl_uniform_buffer_variable *variables;
57    unsigned num_variables;
58    void *mem_ctx;
59    bool is_array_instance;
60    struct gl_shader_program *prog;
61 
62 private:
enter_record(const glsl_type * type,const char *,bool row_major,const enum glsl_interface_packing packing)63    virtual void enter_record(const glsl_type *type, const char *,
64                              bool row_major,
65                              const enum glsl_interface_packing packing)
66    {
67       assert(type->is_record());
68       if (packing == GLSL_INTERFACE_PACKING_STD430)
69          this->offset = glsl_align(
70             this->offset, type->std430_base_alignment(row_major));
71       else
72          this->offset = glsl_align(
73             this->offset, type->std140_base_alignment(row_major));
74    }
75 
leave_record(const glsl_type * type,const char *,bool row_major,const enum glsl_interface_packing packing)76    virtual void leave_record(const glsl_type *type, const char *,
77                              bool row_major,
78                              const enum glsl_interface_packing packing)
79    {
80       assert(type->is_record());
81 
82       /* If this is the last field of a structure, apply rule #9.  The
83        * ARB_uniform_buffer_object spec says:
84        *
85        *    The structure may have padding at the end; the base offset of the
86        *    member following the sub-structure is rounded up to the next
87        *    multiple of the base alignment of the structure.
88        */
89       if (packing == GLSL_INTERFACE_PACKING_STD430)
90          this->offset = glsl_align(
91             this->offset, type->std430_base_alignment(row_major));
92       else
93          this->offset = glsl_align(
94             this->offset, type->std140_base_alignment(row_major));
95    }
96 
set_buffer_offset(unsigned offset)97    virtual void set_buffer_offset(unsigned offset)
98    {
99       this->offset = offset;
100    }
101 
visit_field(const glsl_type * type,const char * name,bool row_major,const glsl_type *,const enum glsl_interface_packing packing,bool last_field)102    virtual void visit_field(const glsl_type *type, const char *name,
103                             bool row_major, const glsl_type *,
104                             const enum glsl_interface_packing packing,
105                             bool last_field)
106    {
107       assert(this->index < this->num_variables);
108 
109       gl_uniform_buffer_variable *v = &this->variables[this->index++];
110 
111       v->Name = ralloc_strdup(mem_ctx, name);
112       v->Type = type;
113       v->RowMajor = type->without_array()->is_matrix() && row_major;
114 
115       if (this->is_array_instance) {
116          v->IndexName = ralloc_strdup(mem_ctx, name);
117 
118          char *open_bracket = strchr(v->IndexName, '[');
119          assert(open_bracket != NULL);
120 
121          char *close_bracket = strchr(open_bracket, '.') - 1;
122          assert(close_bracket != NULL);
123 
124          /* Length of the tail without the ']' but with the NUL.
125           */
126          unsigned len = strlen(close_bracket + 1) + 1;
127 
128          memmove(open_bracket, close_bracket + 1, len);
129       } else {
130          v->IndexName = v->Name;
131       }
132 
133       unsigned alignment = 0;
134       unsigned size = 0;
135 
136       /* The ARB_program_interface_query spec says:
137        *
138        *    If the final member of an active shader storage block is array
139        *    with no declared size, the minimum buffer size is computed
140        *    assuming the array was declared as an array with one element.
141        *
142        * For that reason, we use the base type of the unsized array to
143        * calculate its size. We don't need to check if the unsized array is
144        * the last member of a shader storage block (that check was already
145        * done by the parser).
146        */
147       const glsl_type *type_for_size = type;
148       if (type->is_unsized_array()) {
149          assert(last_field);
150          type_for_size = type->without_array();
151       }
152 
153       if (packing == GLSL_INTERFACE_PACKING_STD430) {
154          alignment = type->std430_base_alignment(v->RowMajor);
155          size = type_for_size->std430_size(v->RowMajor);
156       } else {
157          alignment = type->std140_base_alignment(v->RowMajor);
158          size = type_for_size->std140_size(v->RowMajor);
159       }
160 
161       this->offset = glsl_align(this->offset, alignment);
162       v->Offset = this->offset;
163 
164       this->offset += size;
165 
166       /* The ARB_uniform_buffer_object spec says:
167        *
168        *    For uniform blocks laid out according to [std140] rules, the
169        *    minimum buffer object size returned by the UNIFORM_BLOCK_DATA_SIZE
170        *    query is derived by taking the offset of the last basic machine
171        *    unit consumed by the last uniform of the uniform block (including
172        *    any end-of-array or end-of-structure padding), adding one, and
173        *    rounding up to the next multiple of the base alignment required
174        *    for a vec4.
175        */
176       this->buffer_size = glsl_align(this->offset, 16);
177    }
178 };
179 
180 class count_block_size : public program_resource_visitor {
181 public:
count_block_size()182    count_block_size() : num_active_uniforms(0)
183    {
184       /* empty */
185    }
186 
187    unsigned num_active_uniforms;
188 
189 private:
visit_field(const glsl_type *,const char *,bool,const glsl_type *,const enum glsl_interface_packing,bool)190    virtual void visit_field(const glsl_type * /* type */,
191                             const char * /* name */,
192                             bool /* row_major */,
193                             const glsl_type * /* record_type */,
194                             const enum glsl_interface_packing,
195                             bool /* last_field */)
196    {
197       this->num_active_uniforms++;
198    }
199 };
200 
201 } /* anonymous namespace */
202 
203 struct block {
204    const glsl_type *type;
205    bool has_instance_name;
206 };
207 
208 static void process_block_array_leaf(const char *name, gl_uniform_block *blocks,
209                                      ubo_visitor *parcel,
210                                      gl_uniform_buffer_variable *variables,
211                                      const struct link_uniform_block_active *const b,
212                                      unsigned *block_index,
213                                      unsigned *binding_offset,
214                                      unsigned linearized_index,
215                                      struct gl_context *ctx,
216                                      struct gl_shader_program *prog);
217 
218 /**
219  *
220  * \param first_index Value of \c block_index for the first element of the
221  *                    array.
222  */
223 static void
process_block_array(struct uniform_block_array_elements * ub_array,char ** name,size_t name_length,gl_uniform_block * blocks,ubo_visitor * parcel,gl_uniform_buffer_variable * variables,const struct link_uniform_block_active * const b,unsigned * block_index,unsigned * binding_offset,struct gl_context * ctx,struct gl_shader_program * prog,unsigned first_index)224 process_block_array(struct uniform_block_array_elements *ub_array, char **name,
225                     size_t name_length, gl_uniform_block *blocks,
226                     ubo_visitor *parcel, gl_uniform_buffer_variable *variables,
227                     const struct link_uniform_block_active *const b,
228                     unsigned *block_index, unsigned *binding_offset,
229                     struct gl_context *ctx, struct gl_shader_program *prog,
230                     unsigned first_index)
231 {
232    for (unsigned j = 0; j < ub_array->num_array_elements; j++) {
233       size_t new_length = name_length;
234 
235       /* Append the subscript to the current variable name */
236       ralloc_asprintf_rewrite_tail(name, &new_length, "[%u]",
237                                    ub_array->array_elements[j]);
238 
239       if (ub_array->array) {
240          process_block_array(ub_array->array, name, new_length, blocks,
241                              parcel, variables, b, block_index,
242                              binding_offset, ctx, prog, first_index);
243       } else {
244          process_block_array_leaf(*name, blocks,
245                                   parcel, variables, b, block_index,
246                                   binding_offset, *block_index - first_index,
247                                   ctx, prog);
248       }
249    }
250 }
251 
252 static void
process_block_array_leaf(const char * name,gl_uniform_block * blocks,ubo_visitor * parcel,gl_uniform_buffer_variable * variables,const struct link_uniform_block_active * const b,unsigned * block_index,unsigned * binding_offset,unsigned linearized_index,struct gl_context * ctx,struct gl_shader_program * prog)253 process_block_array_leaf(const char *name,
254                          gl_uniform_block *blocks,
255                          ubo_visitor *parcel, gl_uniform_buffer_variable *variables,
256                          const struct link_uniform_block_active *const b,
257                          unsigned *block_index, unsigned *binding_offset,
258                          unsigned linearized_index,
259                          struct gl_context *ctx, struct gl_shader_program *prog)
260 {
261    unsigned i = *block_index;
262    const glsl_type *type =  b->type->without_array();
263 
264    blocks[i].Name = ralloc_strdup(blocks, name);
265    blocks[i].Uniforms = &variables[(*parcel).index];
266 
267    /* The ARB_shading_language_420pack spec says:
268     *
269     *    If the binding identifier is used with a uniform block instanced as
270     *    an array then the first element of the array takes the specified
271     *    block binding and each subsequent element takes the next consecutive
272     *    uniform block binding point.
273     */
274    blocks[i].Binding = (b->has_binding) ? b->binding + *binding_offset : 0;
275 
276    blocks[i].UniformBufferSize = 0;
277    blocks[i]._Packing = gl_uniform_block_packing(type->interface_packing);
278    blocks[i]._RowMajor = type->get_interface_row_major();
279    blocks[i].linearized_array_index = linearized_index;
280 
281    parcel->process(type, b->has_instance_name ? blocks[i].Name : "");
282 
283    blocks[i].UniformBufferSize = parcel->buffer_size;
284 
285    /* Check SSBO size is lower than maximum supported size for SSBO */
286    if (b->is_shader_storage &&
287        parcel->buffer_size > ctx->Const.MaxShaderStorageBlockSize) {
288       linker_error(prog, "shader storage block `%s' has size %d, "
289                    "which is larger than than the maximum allowed (%d)",
290                    b->type->name,
291                    parcel->buffer_size,
292                    ctx->Const.MaxShaderStorageBlockSize);
293    }
294    blocks[i].NumUniforms =
295       (unsigned)(ptrdiff_t)(&variables[parcel->index] - blocks[i].Uniforms);
296 
297    *block_index = *block_index + 1;
298    *binding_offset = *binding_offset + 1;
299 }
300 
301 /* This function resizes the array types of the block so that later we can use
302  * this new size to correctly calculate the offest for indirect indexing.
303  */
304 static const glsl_type *
resize_block_array(const glsl_type * type,struct uniform_block_array_elements * ub_array)305 resize_block_array(const glsl_type *type,
306                    struct uniform_block_array_elements *ub_array)
307 {
308    if (type->is_array()) {
309       struct uniform_block_array_elements *child_array =
310          type->fields.array->is_array() ? ub_array->array : NULL;
311       const glsl_type *new_child_type =
312          resize_block_array(type->fields.array, child_array);
313 
314       const glsl_type *new_type =
315          glsl_type::get_array_instance(new_child_type,
316                                        ub_array->num_array_elements);
317       ub_array->ir->array->type = new_type;
318       return new_type;
319    } else {
320       return type;
321    }
322 }
323 
324 static void
create_buffer_blocks(void * mem_ctx,struct gl_context * ctx,struct gl_shader_program * prog,struct gl_uniform_block ** out_blks,unsigned num_blocks,struct hash_table * block_hash,unsigned num_variables,bool create_ubo_blocks)325 create_buffer_blocks(void *mem_ctx, struct gl_context *ctx,
326                      struct gl_shader_program *prog,
327                      struct gl_uniform_block **out_blks, unsigned num_blocks,
328                      struct hash_table *block_hash, unsigned num_variables,
329                      bool create_ubo_blocks)
330 {
331    if (num_blocks == 0) {
332       assert(num_variables == 0);
333       return;
334    }
335 
336    assert(num_variables != 0);
337 
338    /* Allocate storage to hold all of the information related to uniform
339     * blocks that can be queried through the API.
340     */
341    struct gl_uniform_block *blocks =
342       rzalloc_array(mem_ctx, gl_uniform_block, num_blocks);
343    gl_uniform_buffer_variable *variables =
344       ralloc_array(blocks, gl_uniform_buffer_variable, num_variables);
345 
346    /* Add each variable from each uniform block to the API tracking
347     * structures.
348     */
349    ubo_visitor parcel(blocks, variables, num_variables, prog);
350 
351    STATIC_ASSERT(unsigned(GLSL_INTERFACE_PACKING_STD140)
352                  == unsigned(ubo_packing_std140));
353    STATIC_ASSERT(unsigned(GLSL_INTERFACE_PACKING_SHARED)
354                  == unsigned(ubo_packing_shared));
355    STATIC_ASSERT(unsigned(GLSL_INTERFACE_PACKING_PACKED)
356                  == unsigned(ubo_packing_packed));
357    STATIC_ASSERT(unsigned(GLSL_INTERFACE_PACKING_STD430)
358                  == unsigned(ubo_packing_std430));
359 
360    unsigned i = 0;
361    struct hash_entry *entry;
362    hash_table_foreach (block_hash, entry) {
363       const struct link_uniform_block_active *const b =
364          (const struct link_uniform_block_active *) entry->data;
365       const glsl_type *block_type = b->type;
366 
367       if ((create_ubo_blocks && !b->is_shader_storage) ||
368           (!create_ubo_blocks && b->is_shader_storage)) {
369 
370          unsigned binding_offset = 0;
371          if (b->array != NULL) {
372             char *name = ralloc_strdup(NULL,
373                                        block_type->without_array()->name);
374             size_t name_length = strlen(name);
375 
376             assert(b->has_instance_name);
377             process_block_array(b->array, &name, name_length, blocks, &parcel,
378                                 variables, b, &i, &binding_offset, ctx, prog,
379                                 i);
380             ralloc_free(name);
381          } else {
382             process_block_array_leaf(block_type->name, blocks, &parcel,
383                                      variables, b, &i, &binding_offset,
384                                      0, ctx, prog);
385          }
386       }
387    }
388 
389    *out_blks = blocks;
390 
391    assert(parcel.index == num_variables);
392 }
393 
394 void
link_uniform_blocks(void * mem_ctx,struct gl_context * ctx,struct gl_shader_program * prog,struct gl_linked_shader * shader,struct gl_uniform_block ** ubo_blocks,unsigned * num_ubo_blocks,struct gl_uniform_block ** ssbo_blocks,unsigned * num_ssbo_blocks)395 link_uniform_blocks(void *mem_ctx,
396                     struct gl_context *ctx,
397                     struct gl_shader_program *prog,
398                     struct gl_linked_shader *shader,
399                     struct gl_uniform_block **ubo_blocks,
400                     unsigned *num_ubo_blocks,
401                     struct gl_uniform_block **ssbo_blocks,
402                     unsigned *num_ssbo_blocks)
403 {
404    /* This hash table will track all of the uniform blocks that have been
405     * encountered.  Since blocks with the same block-name must be the same,
406     * the hash is organized by block-name.
407     */
408    struct hash_table *block_hash =
409       _mesa_hash_table_create(mem_ctx, _mesa_key_hash_string,
410                               _mesa_key_string_equal);
411 
412    if (block_hash == NULL) {
413       _mesa_error_no_memory(__func__);
414       linker_error(prog, "out of memory\n");
415       return;
416    }
417 
418    /* Determine which uniform blocks are active. */
419    link_uniform_block_active_visitor v(mem_ctx, block_hash, prog);
420    visit_list_elements(&v, shader->ir);
421 
422    /* Count the number of active uniform blocks.  Count the total number of
423     * active slots in those uniform blocks.
424     */
425    unsigned num_ubo_variables = 0;
426    unsigned num_ssbo_variables = 0;
427    count_block_size block_size;
428    struct hash_entry *entry;
429 
430    hash_table_foreach (block_hash, entry) {
431       struct link_uniform_block_active *const b =
432          (struct link_uniform_block_active *) entry->data;
433 
434       assert((b->array != NULL) == b->type->is_array());
435 
436       if (b->array != NULL &&
437           (b->type->without_array()->interface_packing ==
438            GLSL_INTERFACE_PACKING_PACKED)) {
439          b->type = resize_block_array(b->type, b->array);
440          b->var->type = b->type;
441       }
442 
443       block_size.num_active_uniforms = 0;
444       block_size.process(b->type->without_array(), "");
445 
446       if (b->array != NULL) {
447          unsigned aoa_size = b->type->arrays_of_arrays_size();
448          if (b->is_shader_storage) {
449             *num_ssbo_blocks += aoa_size;
450             num_ssbo_variables += aoa_size * block_size.num_active_uniforms;
451          } else {
452             *num_ubo_blocks += aoa_size;
453             num_ubo_variables += aoa_size * block_size.num_active_uniforms;
454          }
455       } else {
456          if (b->is_shader_storage) {
457             (*num_ssbo_blocks)++;
458             num_ssbo_variables += block_size.num_active_uniforms;
459          } else {
460             (*num_ubo_blocks)++;
461             num_ubo_variables += block_size.num_active_uniforms;
462          }
463       }
464 
465    }
466 
467    create_buffer_blocks(mem_ctx, ctx, prog, ubo_blocks, *num_ubo_blocks,
468                         block_hash, num_ubo_variables, true);
469    create_buffer_blocks(mem_ctx, ctx, prog, ssbo_blocks, *num_ssbo_blocks,
470                         block_hash, num_ssbo_variables, false);
471 
472    _mesa_hash_table_destroy(block_hash, NULL);
473 }
474 
475 static bool
link_uniform_blocks_are_compatible(const gl_uniform_block * a,const gl_uniform_block * b)476 link_uniform_blocks_are_compatible(const gl_uniform_block *a,
477                                    const gl_uniform_block *b)
478 {
479    assert(strcmp(a->Name, b->Name) == 0);
480 
481    /* Page 35 (page 42 of the PDF) in section 4.3.7 of the GLSL 1.50 spec says:
482     *
483     *    Matched block names within an interface (as defined above) must match
484     *    in terms of having the same number of declarations with the same
485     *    sequence of types and the same sequence of member names, as well as
486     *    having the same member-wise layout qualification....if a matching
487     *    block is declared as an array, then the array sizes must also
488     *    match... Any mismatch will generate a link error.
489     *
490     * Arrays are not yet supported, so there is no check for that.
491     */
492    if (a->NumUniforms != b->NumUniforms)
493       return false;
494 
495    if (a->_Packing != b->_Packing)
496       return false;
497 
498    if (a->_RowMajor != b->_RowMajor)
499       return false;
500 
501    for (unsigned i = 0; i < a->NumUniforms; i++) {
502       if (strcmp(a->Uniforms[i].Name, b->Uniforms[i].Name) != 0)
503          return false;
504 
505       if (a->Uniforms[i].Type != b->Uniforms[i].Type)
506          return false;
507 
508       if (a->Uniforms[i].RowMajor != b->Uniforms[i].RowMajor)
509          return false;
510    }
511 
512    return true;
513 }
514 
515 /**
516  * Merges a uniform block into an array of uniform blocks that may or
517  * may not already contain a copy of it.
518  *
519  * Returns the index of the new block in the array.
520  */
521 int
link_cross_validate_uniform_block(void * mem_ctx,struct gl_uniform_block ** linked_blocks,unsigned int * num_linked_blocks,struct gl_uniform_block * new_block)522 link_cross_validate_uniform_block(void *mem_ctx,
523                                   struct gl_uniform_block **linked_blocks,
524                                   unsigned int *num_linked_blocks,
525                                   struct gl_uniform_block *new_block)
526 {
527    for (unsigned int i = 0; i < *num_linked_blocks; i++) {
528       struct gl_uniform_block *old_block = &(*linked_blocks)[i];
529 
530       if (strcmp(old_block->Name, new_block->Name) == 0)
531          return link_uniform_blocks_are_compatible(old_block, new_block)
532             ? i : -1;
533    }
534 
535    *linked_blocks = reralloc(mem_ctx, *linked_blocks,
536                              struct gl_uniform_block,
537                              *num_linked_blocks + 1);
538    int linked_block_index = (*num_linked_blocks)++;
539    struct gl_uniform_block *linked_block = &(*linked_blocks)[linked_block_index];
540 
541    memcpy(linked_block, new_block, sizeof(*new_block));
542    linked_block->Uniforms = ralloc_array(*linked_blocks,
543                                          struct gl_uniform_buffer_variable,
544                                          linked_block->NumUniforms);
545 
546    memcpy(linked_block->Uniforms,
547           new_block->Uniforms,
548           sizeof(*linked_block->Uniforms) * linked_block->NumUniforms);
549 
550    linked_block->Name = ralloc_strdup(*linked_blocks, linked_block->Name);
551 
552    for (unsigned int i = 0; i < linked_block->NumUniforms; i++) {
553       struct gl_uniform_buffer_variable *ubo_var =
554          &linked_block->Uniforms[i];
555 
556       if (ubo_var->Name == ubo_var->IndexName) {
557          ubo_var->Name = ralloc_strdup(*linked_blocks, ubo_var->Name);
558          ubo_var->IndexName = ubo_var->Name;
559       } else {
560          ubo_var->Name = ralloc_strdup(*linked_blocks, ubo_var->Name);
561          ubo_var->IndexName = ralloc_strdup(*linked_blocks, ubo_var->IndexName);
562       }
563    }
564 
565    return linked_block_index;
566 }
567