SQUASH: Fix comments from eric
[mesa.git] / src / glsl / link_uniform_blocks.cpp
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:
36 ubo_visitor(void *mem_ctx, gl_uniform_buffer_variable *variables,
37 unsigned num_variables)
38 : index(0), offset(0), buffer_size(0), variables(variables),
39 num_variables(num_variables), mem_ctx(mem_ctx), is_array_instance(false)
40 {
41 /* empty */
42 }
43
44 void process(const glsl_type *type, const char *name)
45 {
46 this->offset = 0;
47 this->buffer_size = 0;
48 this->is_array_instance = strchr(name, ']') != NULL;
49 this->program_resource_visitor::process(type, name);
50 }
51
52 unsigned index;
53 unsigned offset;
54 unsigned buffer_size;
55 gl_uniform_buffer_variable *variables;
56 unsigned num_variables;
57 void *mem_ctx;
58 bool is_array_instance;
59
60 private:
61 virtual void visit_field(const glsl_type *type, const char *name,
62 bool row_major)
63 {
64 (void) type;
65 (void) name;
66 (void) row_major;
67 assert(!"Should not get here.");
68 }
69
70 virtual void visit_field(const glsl_type *type, const char *name,
71 bool row_major, const glsl_type *record_type,
72 bool last_field)
73 {
74 assert(this->index < this->num_variables);
75
76 gl_uniform_buffer_variable *v = &this->variables[this->index++];
77
78 v->Name = ralloc_strdup(mem_ctx, name);
79 v->Type = type;
80 v->RowMajor = type->without_array()->is_matrix() && row_major;
81
82 if (this->is_array_instance) {
83 v->IndexName = ralloc_strdup(mem_ctx, name);
84
85 char *open_bracket = strchr(v->IndexName, '[');
86 assert(open_bracket != NULL);
87
88 char *close_bracket = strchr(open_bracket, ']');
89 assert(close_bracket != NULL);
90
91 /* Length of the tail without the ']' but with the NUL.
92 */
93 unsigned len = strlen(close_bracket + 1) + 1;
94
95 memmove(open_bracket, close_bracket + 1, len);
96 } else {
97 v->IndexName = v->Name;
98 }
99
100 const unsigned alignment = record_type
101 ? record_type->std140_base_alignment(v->RowMajor)
102 : type->std140_base_alignment(v->RowMajor);
103 unsigned size = type->std140_size(v->RowMajor);
104
105 this->offset = glsl_align(this->offset, alignment);
106 v->Offset = this->offset;
107
108 /* If this is the last field of a structure, apply rule #9. The
109 * GL_ARB_uniform_buffer_object spec says:
110 *
111 * "The structure may have padding at the end; the base offset of
112 * the member following the sub-structure is rounded up to the next
113 * multiple of the base alignment of the structure."
114 *
115 * last_field won't be set if this is the last field of a UBO that is
116 * not a named instance.
117 */
118 this->offset += size;
119 if (last_field)
120 this->offset = glsl_align(this->offset, 16);
121
122 /* From the GL_ARB_uniform_buffer_object spec:
123 *
124 * "For uniform blocks laid out according to [std140] rules, the
125 * minimum buffer object size returned by the
126 * UNIFORM_BLOCK_DATA_SIZE query is derived by taking the offset of
127 * the last basic machine unit consumed by the last uniform of the
128 * uniform block (including any end-of-array or end-of-structure
129 * padding), adding one, and rounding up to the next multiple of
130 * the base alignment required for a vec4."
131 */
132 this->buffer_size = glsl_align(this->offset, 16);
133 }
134
135 virtual void visit_field(const glsl_struct_field *field)
136 {
137 /* FINISHME: When support for doubles (dvec4, etc.) is added to the
138 * FINISHME: compiler, this may be incorrect for a structure in a UBO
139 * FINISHME: like struct s { struct { float f } s1; dvec4 v; };.
140 */
141 this->offset = glsl_align(this->offset,
142 field->type->std140_base_alignment(false));
143 }
144 };
145
146 class count_block_size : public program_resource_visitor {
147 public:
148 count_block_size() : num_active_uniforms(0)
149 {
150 /* empty */
151 }
152
153 unsigned num_active_uniforms;
154
155 private:
156 virtual void visit_field(const glsl_type *type, const char *name,
157 bool row_major)
158 {
159 (void) type;
160 (void) name;
161 (void) row_major;
162 this->num_active_uniforms++;
163 }
164 };
165
166 } /* anonymous namespace */
167
168 struct block {
169 const glsl_type *type;
170 bool has_instance_name;
171 };
172
173 unsigned
174 link_uniform_blocks(void *mem_ctx,
175 struct gl_shader_program *prog,
176 struct gl_shader **shader_list,
177 unsigned num_shaders,
178 struct gl_uniform_block **blocks_ret)
179 {
180 /* This hash table will track all of the uniform blocks that have been
181 * encountered. Since blocks with the same block-name must be the same,
182 * the hash is organized by block-name.
183 */
184 struct hash_table *block_hash =
185 _mesa_hash_table_create(mem_ctx, _mesa_key_hash_string,
186 _mesa_key_string_equal);
187
188 if (block_hash == NULL) {
189 _mesa_error_no_memory(__func__);
190 linker_error(prog, "out of memory\n");
191 return 0;
192 }
193
194 /* Determine which uniform blocks are active.
195 */
196 link_uniform_block_active_visitor v(mem_ctx, block_hash, prog);
197 for (unsigned i = 0; i < num_shaders; i++) {
198 visit_list_elements(&v, shader_list[i]->ir);
199 }
200
201 /* Count the number of active uniform blocks. Count the total number of
202 * active slots in those uniform blocks.
203 */
204 unsigned num_blocks = 0;
205 unsigned num_variables = 0;
206 count_block_size block_size;
207 struct hash_entry *entry;
208
209 hash_table_foreach (block_hash, entry) {
210 const struct link_uniform_block_active *const b =
211 (const struct link_uniform_block_active *) entry->data;
212
213 const glsl_type *const block_type =
214 b->type->is_array() ? b->type->fields.array : b->type;
215
216 assert((b->num_array_elements > 0) == b->type->is_array());
217
218 block_size.num_active_uniforms = 0;
219 block_size.process(block_type, "");
220
221 if (b->num_array_elements > 0) {
222 num_blocks += b->num_array_elements;
223 num_variables += b->num_array_elements
224 * block_size.num_active_uniforms;
225 } else {
226 num_blocks++;
227 num_variables += block_size.num_active_uniforms;
228 }
229
230 }
231
232 if (num_blocks == 0) {
233 assert(num_variables == 0);
234 _mesa_hash_table_destroy(block_hash, NULL);
235 return 0;
236 }
237
238 assert(num_variables != 0);
239
240 /* Allocate storage to hold all of the informatation related to uniform
241 * blocks that can be queried through the API.
242 */
243 gl_uniform_block *blocks =
244 ralloc_array(mem_ctx, gl_uniform_block, num_blocks);
245 gl_uniform_buffer_variable *variables =
246 ralloc_array(blocks, gl_uniform_buffer_variable, num_variables);
247
248 /* Add each variable from each uniform block to the API tracking
249 * structures.
250 */
251 unsigned i = 0;
252 ubo_visitor parcel(blocks, variables, num_variables);
253
254 STATIC_ASSERT(unsigned(GLSL_INTERFACE_PACKING_STD140)
255 == unsigned(ubo_packing_std140));
256 STATIC_ASSERT(unsigned(GLSL_INTERFACE_PACKING_SHARED)
257 == unsigned(ubo_packing_shared));
258 STATIC_ASSERT(unsigned(GLSL_INTERFACE_PACKING_PACKED)
259 == unsigned(ubo_packing_packed));
260
261
262 hash_table_foreach (block_hash, entry) {
263 const struct link_uniform_block_active *const b =
264 (const struct link_uniform_block_active *) entry->data;
265 const glsl_type *block_type = b->type;
266
267 if (b->num_array_elements > 0) {
268 const char *const name = block_type->fields.array->name;
269
270 assert(b->has_instance_name);
271 for (unsigned j = 0; j < b->num_array_elements; j++) {
272 blocks[i].Name = ralloc_asprintf(blocks, "%s[%u]", name,
273 b->array_elements[j]);
274 blocks[i].Uniforms = &variables[parcel.index];
275
276 /* The GL_ARB_shading_language_420pack spec says:
277 *
278 * "If the binding identifier is used with a uniform block
279 * instanced as an array then the first element of the array
280 * takes the specified block binding and each subsequent
281 * element takes the next consecutive uniform block binding
282 * point."
283 */
284 blocks[i].Binding = (b->has_binding) ? b->binding + j : 0;
285
286 blocks[i].UniformBufferSize = 0;
287 blocks[i]._Packing =
288 gl_uniform_block_packing(block_type->interface_packing);
289
290 parcel.process(block_type->fields.array,
291 blocks[i].Name);
292
293 blocks[i].UniformBufferSize = parcel.buffer_size;
294
295 blocks[i].NumUniforms =
296 (unsigned)(ptrdiff_t)(&variables[parcel.index] - blocks[i].Uniforms);
297
298 i++;
299 }
300 } else {
301 blocks[i].Name = ralloc_strdup(blocks, block_type->name);
302 blocks[i].Uniforms = &variables[parcel.index];
303 blocks[i].Binding = (b->has_binding) ? b->binding : 0;
304 blocks[i].UniformBufferSize = 0;
305 blocks[i]._Packing =
306 gl_uniform_block_packing(block_type->interface_packing);
307
308 parcel.process(block_type,
309 b->has_instance_name ? block_type->name : "");
310
311 blocks[i].UniformBufferSize = parcel.buffer_size;
312
313 blocks[i].NumUniforms =
314 (unsigned)(ptrdiff_t)(&variables[parcel.index] - blocks[i].Uniforms);
315
316 i++;
317 }
318 }
319
320 assert(parcel.index == num_variables);
321
322 _mesa_hash_table_destroy(block_hash, NULL);
323
324 *blocks_ret = blocks;
325 return num_blocks;
326 }
327
328 bool
329 link_uniform_blocks_are_compatible(const gl_uniform_block *a,
330 const gl_uniform_block *b)
331 {
332 assert(strcmp(a->Name, b->Name) == 0);
333
334 /* Page 35 (page 42 of the PDF) in section 4.3.7 of the GLSL 1.50 spec says:
335 *
336 * "Matched block names within an interface (as defined above) must
337 * match in terms of having the same number of declarations with the
338 * same sequence of types and the same sequence of member names, as
339 * well as having the same member-wise layout qualification....if a
340 * matching block is declared as an array, then the array sizes must
341 * also match... Any mismatch will generate a link error."
342 *
343 * Arrays are not yet supported, so there is no check for that.
344 */
345 if (a->NumUniforms != b->NumUniforms)
346 return false;
347
348 if (a->_Packing != b->_Packing)
349 return false;
350
351 for (unsigned i = 0; i < a->NumUniforms; i++) {
352 if (strcmp(a->Uniforms[i].Name, b->Uniforms[i].Name) != 0)
353 return false;
354
355 if (a->Uniforms[i].Type != b->Uniforms[i].Type)
356 return false;
357
358 if (a->Uniforms[i].RowMajor != b->Uniforms[i].RowMajor)
359 return false;
360 }
361
362 return true;
363 }