• 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 "string_to_uint_map.h"
29 
30 /* These functions are put in a "private" namespace instead of being marked
31  * static so that the unit tests can access them.  See
32  * http://code.google.com/p/googletest/wiki/AdvancedGuide#Testing_Private_Code
33  */
34 namespace linker {
35 
36 static gl_uniform_storage *
get_storage(struct gl_shader_program * prog,const char * name)37 get_storage(struct gl_shader_program *prog, const char *name)
38 {
39    unsigned id;
40    if (prog->UniformHash->get(id, name))
41       return &prog->data->UniformStorage[id];
42 
43    assert(!"No uniform storage found!");
44    return NULL;
45 }
46 
47 void
copy_constant_to_storage(union gl_constant_value * storage,const ir_constant * val,const enum glsl_base_type base_type,const unsigned int elements,unsigned int boolean_true)48 copy_constant_to_storage(union gl_constant_value *storage,
49                          const ir_constant *val,
50                          const enum glsl_base_type base_type,
51                          const unsigned int elements,
52                          unsigned int boolean_true)
53 {
54    for (unsigned int i = 0; i < elements; i++) {
55       switch (base_type) {
56       case GLSL_TYPE_UINT:
57          storage[i].u = val->value.u[i];
58          break;
59       case GLSL_TYPE_INT:
60       case GLSL_TYPE_SAMPLER:
61          storage[i].i = val->value.i[i];
62          break;
63       case GLSL_TYPE_FLOAT:
64          storage[i].f = val->value.f[i];
65          break;
66       case GLSL_TYPE_DOUBLE:
67       case GLSL_TYPE_UINT64:
68       case GLSL_TYPE_INT64:
69          /* XXX need to check on big-endian */
70          memcpy(&storage[i * 2].u, &val->value.d[i], sizeof(double));
71          break;
72       case GLSL_TYPE_BOOL:
73          storage[i].b = val->value.b[i] ? boolean_true : 0;
74          break;
75       case GLSL_TYPE_ARRAY:
76       case GLSL_TYPE_STRUCT:
77       case GLSL_TYPE_IMAGE:
78       case GLSL_TYPE_ATOMIC_UINT:
79       case GLSL_TYPE_INTERFACE:
80       case GLSL_TYPE_VOID:
81       case GLSL_TYPE_SUBROUTINE:
82       case GLSL_TYPE_FUNCTION:
83       case GLSL_TYPE_ERROR:
84       case GLSL_TYPE_UINT16:
85       case GLSL_TYPE_INT16:
86       case GLSL_TYPE_FLOAT16:
87          /* All other types should have already been filtered by other
88           * paths in the caller.
89           */
90          assert(!"Should not get here.");
91          break;
92       }
93    }
94 }
95 
96 /**
97  * Initialize an opaque uniform from the value of an explicit binding
98  * qualifier specified in the shader.  Atomic counters are different because
99  * they have no storage and should be handled elsewhere.
100  */
101 static void
set_opaque_binding(void * mem_ctx,gl_shader_program * prog,const ir_variable * var,const glsl_type * type,const char * name,int * binding)102 set_opaque_binding(void *mem_ctx, gl_shader_program *prog,
103                    const ir_variable *var, const glsl_type *type,
104                    const char *name, int *binding)
105 {
106 
107    if (type->is_array() && type->fields.array->is_array()) {
108       const glsl_type *const element_type = type->fields.array;
109 
110       for (unsigned int i = 0; i < type->length; i++) {
111          const char *element_name = ralloc_asprintf(mem_ctx, "%s[%d]", name, i);
112 
113          set_opaque_binding(mem_ctx, prog, var, element_type,
114                             element_name, binding);
115       }
116    } else {
117       struct gl_uniform_storage *const storage = get_storage(prog, name);
118 
119       if (!storage)
120          return;
121 
122       const unsigned elements = MAX2(storage->array_elements, 1);
123 
124       /* Section 4.4.6 (Opaque-Uniform Layout Qualifiers) of the GLSL 4.50 spec
125        * says:
126        *
127        *     "If the binding identifier is used with an array, the first element
128        *     of the array takes the specified unit and each subsequent element
129        *     takes the next consecutive unit."
130        */
131       for (unsigned int i = 0; i < elements; i++) {
132          storage->storage[i].i = (*binding)++;
133       }
134 
135       for (int sh = 0; sh < MESA_SHADER_STAGES; sh++) {
136          gl_linked_shader *shader = prog->_LinkedShaders[sh];
137 
138          if (!shader)
139             continue;
140          if (!storage->opaque[sh].active)
141             continue;
142 
143          if (storage->type->is_sampler()) {
144             for (unsigned i = 0; i < elements; i++) {
145                const unsigned index = storage->opaque[sh].index + i;
146 
147                if (var->data.bindless) {
148                   if (index >= shader->Program->sh.NumBindlessSamplers)
149                      break;
150                   shader->Program->sh.BindlessSamplers[index].unit =
151                      storage->storage[i].i;
152                   shader->Program->sh.BindlessSamplers[index].bound = true;
153                   shader->Program->sh.HasBoundBindlessSampler = true;
154                } else {
155                   if (index >= ARRAY_SIZE(shader->Program->SamplerUnits))
156                      break;
157                   shader->Program->SamplerUnits[index] =
158                      storage->storage[i].i;
159                }
160             }
161          } else if (storage->type->is_image()) {
162             for (unsigned i = 0; i < elements; i++) {
163                const unsigned index = storage->opaque[sh].index + i;
164 
165 
166                if (var->data.bindless) {
167                   if (index >= shader->Program->sh.NumBindlessImages)
168                      break;
169                   shader->Program->sh.BindlessImages[index].unit =
170                      storage->storage[i].i;
171                   shader->Program->sh.BindlessImages[index].bound = true;
172                   shader->Program->sh.HasBoundBindlessImage = true;
173                } else {
174                   if (index >= ARRAY_SIZE(shader->Program->sh.ImageUnits))
175                      break;
176                   shader->Program->sh.ImageUnits[index] =
177                      storage->storage[i].i;
178                }
179             }
180          }
181       }
182    }
183 }
184 
185 static void
set_block_binding(gl_shader_program * prog,const char * block_name,unsigned mode,int binding)186 set_block_binding(gl_shader_program *prog, const char *block_name,
187                   unsigned mode, int binding)
188 {
189    unsigned num_blocks = mode == ir_var_uniform ?
190       prog->data->NumUniformBlocks :
191       prog->data->NumShaderStorageBlocks;
192    struct gl_uniform_block *blks = mode == ir_var_uniform ?
193       prog->data->UniformBlocks : prog->data->ShaderStorageBlocks;
194 
195    for (unsigned i = 0; i < num_blocks; i++) {
196       if (!strcmp(blks[i].Name, block_name)) {
197          blks[i].Binding = binding;
198          return;
199       }
200    }
201 
202    unreachable("Failed to initialize block binding");
203 }
204 
205 void
set_uniform_initializer(void * mem_ctx,gl_shader_program * prog,const char * name,const glsl_type * type,ir_constant * val,unsigned int boolean_true)206 set_uniform_initializer(void *mem_ctx, gl_shader_program *prog,
207                         const char *name, const glsl_type *type,
208                         ir_constant *val, unsigned int boolean_true)
209 {
210    const glsl_type *t_without_array = type->without_array();
211    if (type->is_record()) {
212       for (unsigned int i = 0; i < type->length; i++) {
213          const glsl_type *field_type = type->fields.structure[i].type;
214          const char *field_name = ralloc_asprintf(mem_ctx, "%s.%s", name,
215                                             type->fields.structure[i].name);
216          set_uniform_initializer(mem_ctx, prog, field_name,
217                                  field_type, val->get_record_field(i),
218                                  boolean_true);
219       }
220       return;
221    } else if (t_without_array->is_record() ||
222               (type->is_array() && type->fields.array->is_array())) {
223       const glsl_type *const element_type = type->fields.array;
224 
225       for (unsigned int i = 0; i < type->length; i++) {
226          const char *element_name = ralloc_asprintf(mem_ctx, "%s[%d]", name, i);
227 
228          set_uniform_initializer(mem_ctx, prog, element_name,
229                                  element_type, val->const_elements[i],
230                                  boolean_true);
231       }
232       return;
233    }
234 
235    struct gl_uniform_storage *const storage = get_storage(prog, name);
236 
237    if (!storage)
238       return;
239 
240    if (val->type->is_array()) {
241       const enum glsl_base_type base_type =
242          val->const_elements[0]->type->base_type;
243       const unsigned int elements = val->const_elements[0]->type->components();
244       unsigned int idx = 0;
245       unsigned dmul = glsl_base_type_is_64bit(base_type) ? 2 : 1;
246 
247       assert(val->type->length >= storage->array_elements);
248       for (unsigned int i = 0; i < storage->array_elements; i++) {
249          copy_constant_to_storage(& storage->storage[idx],
250                                   val->const_elements[i],
251                                   base_type,
252                                   elements,
253                                   boolean_true);
254 
255          idx += elements * dmul;
256       }
257    } else {
258       copy_constant_to_storage(storage->storage,
259                                val,
260                                val->type->base_type,
261                                val->type->components(),
262                                boolean_true);
263 
264       if (storage->type->is_sampler()) {
265          for (int sh = 0; sh < MESA_SHADER_STAGES; sh++) {
266             gl_linked_shader *shader = prog->_LinkedShaders[sh];
267 
268             if (shader && storage->opaque[sh].active) {
269                unsigned index = storage->opaque[sh].index;
270 
271                shader->Program->SamplerUnits[index] = storage->storage[0].i;
272             }
273          }
274       }
275    }
276 }
277 }
278 
279 void
link_set_uniform_initializers(struct gl_shader_program * prog,unsigned int boolean_true)280 link_set_uniform_initializers(struct gl_shader_program *prog,
281                               unsigned int boolean_true)
282 {
283    void *mem_ctx = NULL;
284 
285    for (unsigned int i = 0; i < MESA_SHADER_STAGES; i++) {
286       struct gl_linked_shader *shader = prog->_LinkedShaders[i];
287 
288       if (shader == NULL)
289          continue;
290 
291       foreach_in_list(ir_instruction, node, shader->ir) {
292          ir_variable *const var = node->as_variable();
293 
294          if (!var || (var->data.mode != ir_var_uniform &&
295              var->data.mode != ir_var_shader_storage))
296             continue;
297 
298          if (!mem_ctx)
299             mem_ctx = ralloc_context(NULL);
300 
301          if (var->data.explicit_binding) {
302             const glsl_type *const type = var->type;
303 
304             if (type->without_array()->is_sampler() ||
305                 type->without_array()->is_image()) {
306                int binding = var->data.binding;
307                linker::set_opaque_binding(mem_ctx, prog, var, var->type,
308                                           var->name, &binding);
309             } else if (var->is_in_buffer_block()) {
310                const glsl_type *const iface_type = var->get_interface_type();
311 
312                /* If the variable is an array and it is an interface instance,
313                 * we need to set the binding for each array element.  Just
314                 * checking that the variable is an array is not sufficient.
315                 * The variable could be an array element of a uniform block
316                 * that lacks an instance name.  For example:
317                 *
318                 *     uniform U {
319                 *         float f[4];
320                 *     };
321                 *
322                 * In this case "f" would pass is_in_buffer_block (above) and
323                 * type->is_array(), but it will fail is_interface_instance().
324                 */
325                if (var->is_interface_instance() && var->type->is_array()) {
326                   for (unsigned i = 0; i < var->type->length; i++) {
327                      const char *name =
328                         ralloc_asprintf(mem_ctx, "%s[%u]", iface_type->name, i);
329 
330                      /* Section 4.4.3 (Uniform Block Layout Qualifiers) of the
331                       * GLSL 4.20 spec says:
332                       *
333                       *     "If the binding identifier is used with a uniform
334                       *     block instanced as an array then the first element
335                       *     of the array takes the specified block binding and
336                       *     each subsequent element takes the next consecutive
337                       *     uniform block binding point."
338                       */
339                      linker::set_block_binding(prog, name, var->data.mode,
340                                                var->data.binding + i);
341                   }
342                } else {
343                   linker::set_block_binding(prog, iface_type->name,
344                                             var->data.mode,
345                                             var->data.binding);
346                }
347             } else if (type->contains_atomic()) {
348                /* we don't actually need to do anything. */
349             } else {
350                assert(!"Explicit binding not on a sampler, UBO or atomic.");
351             }
352          } else if (var->constant_initializer) {
353             linker::set_uniform_initializer(mem_ctx, prog, var->name,
354                                             var->type, var->constant_initializer,
355                                             boolean_true);
356          }
357       }
358    }
359 
360    memcpy(prog->data->UniformDataDefaults, prog->data->UniformDataSlots,
361           sizeof(union gl_constant_value) * prog->data->NumUniformDataSlots);
362    ralloc_free(mem_ctx);
363 }
364