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