summaryrefslogtreecommitdiff
path: root/src/glsl/link_interface_blocks.cpp
blob: a7fceb9bad63437d39d0c3b44d8e27ec33d3527c (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
/*
 * Copyright © 2013 Intel Corporation
 *
 * Permission is hereby granted, free of charge, to any person obtaining a
 * copy of this software and associated documentation files (the "Software"),
 * to deal in the Software without restriction, including without limitation
 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
 * and/or sell copies of the Software, and to permit persons to whom the
 * Software is furnished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice (including the next
 * paragraph) shall be included in all copies or substantial portions of the
 * Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
 * DEALINGS IN THE SOFTWARE.
 */

/**
 * \file link_interface_blocks.cpp
 * Linker support for GLSL's interface blocks.
 */

#include "ir.h"
#include "glsl_symbol_table.h"
#include "linker.h"
#include "main/macros.h"
#include "program/hash_table.h"


namespace {

/**
 * Information about a single interface block definition that we need to keep
 * track of in order to check linkage rules.
 *
 * Note: this class is expected to be short lived, so it doesn't make copies
 * of the strings it references; it simply borrows the pointers from the
 * ir_variable class.
 */
struct interface_block_definition
{
   /**
    * Extract an interface block definition from an ir_variable that
    * represents either the interface instance (for named interfaces), or a
    * member of the interface (for unnamed interfaces).
    */
   explicit interface_block_definition(const ir_variable *var)
      : type(var->get_interface_type()),
        instance_name(NULL),
        array_size(-1)
   {
      if (var->is_interface_instance()) {
         instance_name = var->name;
         if (var->type->is_array())
            array_size = var->type->length;
      }
   }

   /**
    * Interface block type
    */
   const glsl_type *type;

   /**
    * For a named interface block, the instance name.  Otherwise NULL.
    */
   const char *instance_name;

   /**
    * For an interface block array, the array size (or 0 if unsized).
    * Otherwise -1.
    */
   int array_size;
};


/**
 * Check if two interfaces match, according to intrastage interface matching
 * rules.  If they do, and the first interface uses an unsized array, it will
 * be updated to reflect the array size declared in the second interface.
 */
bool
intrastage_match(interface_block_definition *a,
                 const interface_block_definition *b,
                 ir_variable_mode mode)
{
   /* Types must match. */
   if (a->type != b->type)
      return false;

   /* Presence/absence of interface names must match. */
   if ((a->instance_name == NULL) != (b->instance_name == NULL))
      return false;

   /* For uniforms, instance names need not match.  For shader ins/outs,
    * it's not clear from the spec whether they need to match, but
    * Mesa's implementation relies on them matching.
    */
   if (a->instance_name != NULL && mode != ir_var_uniform &&
       strcmp(a->instance_name, b->instance_name) != 0) {
      return false;
   }

   /* Array vs. nonarray must be consistent, and sizes must be
    * consistent, with the exception that unsized arrays match sized
    * arrays.
    */
   if ((a->array_size == -1) != (b->array_size == -1))
      return false;
   if (b->array_size != 0) {
      if (a->array_size == 0)
         a->array_size = b->array_size;
      else if (a->array_size != b->array_size)
         return false;
   }

   return true;
}


/**
 * Check if two interfaces match, according to interstage (in/out) interface
 * matching rules.
 *
 * If \c extra_array_level is true, then vertex-to-geometry shader matching
 * rules are enforced (i.e. a successful match requires the consumer interface
 * to be an array and the producer interface to be a non-array).
 */
bool
interstage_match(const interface_block_definition *producer,
                 const interface_block_definition *consumer,
                 bool extra_array_level)
{
   /* Unsized arrays should not occur during interstage linking.  They
    * should have all been assigned a size by link_intrastage_shaders.
    */
   assert(consumer->array_size != 0);
   assert(producer->array_size != 0);

   /* Types must match. */
   if (consumer->type != producer->type)
      return false;
   if (extra_array_level) {
      /* Consumer must be an array, and producer must not. */
      if (consumer->array_size == -1)
         return false;
      if (producer->array_size != -1)
         return false;
   } else {
      /* Array vs. nonarray must be consistent, and sizes must be consistent.
       * Since unsized arrays have been ruled out, we can check this by just
       * making sure the sizes are equal.
       */
      if (consumer->array_size != producer->array_size)
         return false;
   }
   return true;
}


/**
 * This class keeps track of a mapping from an interface block name to the
 * necessary information about that interface block to determine whether to
 * generate a link error.
 *
 * Note: this class is expected to be short lived, so it doesn't make copies
 * of the strings it references; it simply borrows the pointers from the
 * ir_variable class.
 */
class interface_block_definitions
{
public:
   interface_block_definitions()
      : mem_ctx(ralloc_context(NULL)),
        ht(hash_table_ctor(0, hash_table_string_hash,
                           hash_table_string_compare))
   {
   }

   ~interface_block_definitions()
   {
      hash_table_dtor(ht);
      ralloc_free(mem_ctx);
   }

   /**
    * Lookup the interface definition having the given block name.  Return
    * NULL if none is found.
    */
   interface_block_definition *lookup(const char *block_name)
   {
      return (interface_block_definition *) hash_table_find(ht, block_name);
   }

   /**
    * Add a new interface definition.
    */
   void store(const interface_block_definition &def)
   {
      interface_block_definition *hash_entry =
         rzalloc(mem_ctx, interface_block_definition);
      *hash_entry = def;
      hash_table_insert(ht, hash_entry, def.type->name);
   }

private:
   /**
    * Ralloc context for data structures allocated by this class.
    */
   void *mem_ctx;

   /**
    * Hash table mapping interface block name to an \c
    * interface_block_definition struct.  interface_block_definition structs
    * are allocated using \c mem_ctx.
    */
   hash_table *ht;
};


}; /* anonymous namespace */


void
validate_intrastage_interface_blocks(struct gl_shader_program *prog,
                                     const gl_shader **shader_list,
                                     unsigned num_shaders)
{
   interface_block_definitions in_interfaces;
   interface_block_definitions out_interfaces;
   interface_block_definitions uniform_interfaces;

   for (unsigned int i = 0; i < num_shaders; i++) {
      if (shader_list[i] == NULL)
         continue;

      foreach_list(node, shader_list[i]->ir) {
         ir_variable *var = ((ir_instruction *) node)->as_variable();
         if (!var)
            continue;

         const glsl_type *iface_type = var->get_interface_type();

         if (iface_type == NULL)
            continue;

         interface_block_definitions *definitions;
         switch (var->mode) {
         case ir_var_shader_in:
            definitions = &in_interfaces;
            break;
         case ir_var_shader_out:
            definitions = &out_interfaces;
            break;
         case ir_var_uniform:
            definitions = &uniform_interfaces;
            break;
         default:
            /* Only in, out, and uniform interfaces are legal, so we should
             * never get here.
             */
            assert(!"illegal interface type");
            continue;
         }

         const interface_block_definition def(var);
         interface_block_definition *prev_def =
            definitions->lookup(iface_type->name);

         if (prev_def == NULL) {
            /* This is the first time we've seen the interface, so save
             * it into the appropriate data structure.
             */
            definitions->store(def);
         } else if (!intrastage_match(prev_def, &def,
                                      (ir_variable_mode) var->mode)) {
            linker_error(prog, "definitions of interface block `%s' do not"
                         " match\n", iface_type->name);
            return;
         }
      }
   }
}

void
validate_interstage_interface_blocks(struct gl_shader_program *prog,
                                     const gl_shader *producer,
                                     const gl_shader *consumer)
{
   interface_block_definitions inout_interfaces;
   interface_block_definitions uniform_interfaces;
   const bool extra_array_level = consumer->Type == GL_GEOMETRY_SHADER;

   /* Add non-output interfaces from the consumer to the symbol table. */
   foreach_list(node, consumer->ir) {
      ir_variable *var = ((ir_instruction *) node)->as_variable();
      if (!var || !var->get_interface_type() || var->mode == ir_var_shader_out)
         continue;

      interface_block_definitions *definitions = var->mode == ir_var_uniform ?
         &uniform_interfaces : &inout_interfaces;
      definitions->store(interface_block_definition(var));
   }

   /* Verify that the producer's interfaces match. */
   foreach_list(node, producer->ir) {
      ir_variable *var = ((ir_instruction *) node)->as_variable();
      if (!var || !var->get_interface_type() || var->mode == ir_var_shader_in)
         continue;

      interface_block_definitions *definitions = var->mode == ir_var_uniform ?
         &uniform_interfaces : &inout_interfaces;
      interface_block_definition *consumer_def =
         definitions->lookup(var->get_interface_type()->name);

      /* The consumer doesn't use this output block.  Ignore it. */
      if (consumer_def == NULL)
         continue;

      const interface_block_definition producer_def(var);
      bool match;
      if (var->mode == ir_var_uniform) {
         /* Uniform matching rules are the same for interstage and intrastage
          * linking.
          */
         match = intrastage_match(consumer_def, &producer_def,
                                  (ir_variable_mode) var->mode);
      } else {
         match = interstage_match(&producer_def, consumer_def,
                                  extra_array_level);
      }

      if (!match) {
         linker_error(prog, "definitions of interface block `%s' do not "
                      "match\n", var->get_interface_type()->name);
         return;
      }
   }
}