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