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