2 * Copyright © 2012 Intel Corporation
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:
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
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.
24 #include "main/core.h"
27 #include "ir_uniform.h"
28 #include "link_uniform_block_active_visitor.h"
29 #include "main/hash_table.h"
32 class ubo_visitor
: public program_resource_visitor
{
34 ubo_visitor(void *mem_ctx
, gl_uniform_buffer_variable
*variables
,
35 unsigned num_variables
)
36 : index(0), offset(0), buffer_size(0), variables(variables
),
37 num_variables(num_variables
), mem_ctx(mem_ctx
), is_array_instance(false)
42 void process(const glsl_type
*type
, const char *name
)
45 this->buffer_size
= 0;
46 this->is_array_instance
= strchr(name
, ']') != NULL
;
47 this->program_resource_visitor::process(type
, name
);
53 gl_uniform_buffer_variable
*variables
;
54 unsigned num_variables
;
56 bool is_array_instance
;
59 virtual void visit_field(const glsl_type
*type
, const char *name
,
62 assert(this->index
< this->num_variables
);
64 gl_uniform_buffer_variable
*v
= &this->variables
[this->index
++];
66 v
->Name
= ralloc_strdup(mem_ctx
, name
);
68 v
->RowMajor
= row_major
;
70 if (this->is_array_instance
) {
71 v
->IndexName
= ralloc_strdup(mem_ctx
, name
);
73 char *open_bracket
= strchr(v
->IndexName
, '[');
74 assert(open_bracket
!= NULL
);
76 char *close_bracket
= strchr(open_bracket
, ']');
77 assert(close_bracket
!= NULL
);
79 /* Length of the tail without the ']' but with the NUL.
81 unsigned len
= strlen(close_bracket
+ 1) + 1;
83 memmove(open_bracket
, close_bracket
+ 1, len
);
85 v
->IndexName
= v
->Name
;
88 unsigned alignment
= type
->std140_base_alignment(v
->RowMajor
);
89 unsigned size
= type
->std140_size(v
->RowMajor
);
91 this->offset
= glsl_align(this->offset
, alignment
);
92 v
->Offset
= this->offset
;
95 /* From the GL_ARB_uniform_buffer_object spec:
97 * "For uniform blocks laid out according to [std140] rules, the
98 * minimum buffer object size returned by the
99 * UNIFORM_BLOCK_DATA_SIZE query is derived by taking the offset of
100 * the last basic machine unit consumed by the last uniform of the
101 * uniform block (including any end-of-array or end-of-structure
102 * padding), adding one, and rounding up to the next multiple of
103 * the base alignment required for a vec4."
105 this->buffer_size
= glsl_align(this->offset
, 16);
108 virtual void visit_field(const glsl_struct_field
*field
)
110 this->offset
= glsl_align(this->offset
,
111 field
->type
->std140_base_alignment(false));
115 class count_block_size
: public program_resource_visitor
{
117 count_block_size() : num_active_uniforms(0)
122 unsigned num_active_uniforms
;
125 virtual void visit_field(const glsl_type
*type
, const char *name
,
131 this->num_active_uniforms
++;
136 const glsl_type
*type
;
137 bool has_instance_name
;
141 link_uniform_blocks(void *mem_ctx
,
142 struct gl_shader_program
*prog
,
143 struct gl_shader
**shader_list
,
144 unsigned num_shaders
,
145 struct gl_uniform_block
**blocks_ret
)
147 /* This hash table will track all of the uniform blocks that have been
148 * encountered. Since blocks with the same block-name must be the same,
149 * the hash is organized by block-name.
151 struct hash_table
*block_hash
=
152 _mesa_hash_table_create(mem_ctx
, _mesa_key_string_equal
);
154 /* Determine which uniform blocks are active.
156 link_uniform_block_active_visitor
v(mem_ctx
, block_hash
, prog
);
157 for (unsigned i
= 0; i
< num_shaders
; i
++) {
158 visit_list_elements(&v
, shader_list
[i
]->ir
);
161 /* Count the number of active uniform blocks. Count the total number of
162 * active slots in those uniform blocks.
164 unsigned num_blocks
= 0;
165 unsigned num_variables
= 0;
166 count_block_size block_size
;
167 struct hash_entry
*entry
;
169 hash_table_foreach (block_hash
, entry
) {
170 const struct link_uniform_block_active
*const b
=
171 (const struct link_uniform_block_active
*) entry
->data
;
173 const glsl_type
*const block_type
=
174 b
->type
->is_array() ? b
->type
->fields
.array
: b
->type
;
176 assert((b
->num_array_elements
> 0) == b
->type
->is_array());
178 block_size
.num_active_uniforms
= 0;
179 block_size
.process(block_type
, "");
181 if (b
->num_array_elements
> 0) {
182 num_blocks
+= b
->num_array_elements
;
183 num_variables
+= b
->num_array_elements
184 * block_size
.num_active_uniforms
;
187 num_variables
+= block_size
.num_active_uniforms
;
192 if (num_blocks
== 0) {
193 assert(num_variables
== 0);
194 _mesa_hash_table_destroy(block_hash
, NULL
);
198 assert(num_variables
!= 0);
200 /* Allocate storage to hold all of the informatation related to uniform
201 * blocks that can be queried through the API.
203 gl_uniform_block
*blocks
=
204 ralloc_array(mem_ctx
, gl_uniform_block
, num_blocks
);
205 gl_uniform_buffer_variable
*variables
=
206 ralloc_array(blocks
, gl_uniform_buffer_variable
, num_variables
);
208 /* Add each variable from each uniform block to the API tracking
212 ubo_visitor
parcel(blocks
, variables
, num_variables
);
214 STATIC_ASSERT(unsigned(GLSL_INTERFACE_PACKING_STD140
)
215 == unsigned(ubo_packing_std140
));
216 STATIC_ASSERT(unsigned(GLSL_INTERFACE_PACKING_SHARED
)
217 == unsigned(ubo_packing_shared
));
218 STATIC_ASSERT(unsigned(GLSL_INTERFACE_PACKING_PACKED
)
219 == unsigned(ubo_packing_packed
));
222 hash_table_foreach (block_hash
, entry
) {
223 const struct link_uniform_block_active
*const b
=
224 (const struct link_uniform_block_active
*) entry
->data
;
225 const glsl_type
*block_type
= b
->type
;
227 if (b
->num_array_elements
> 0) {
228 const char *const name
= block_type
->fields
.array
->name
;
230 assert(b
->has_instance_name
);
231 for (unsigned j
= 0; j
< b
->num_array_elements
; j
++) {
232 blocks
[i
].Name
= ralloc_asprintf(blocks
, "%s[%u]", name
,
233 b
->array_elements
[j
]);
234 blocks
[i
].Uniforms
= &variables
[parcel
.index
];
235 blocks
[i
].Binding
= 0;
236 blocks
[i
].UniformBufferSize
= 0;
238 gl_uniform_block_packing(block_type
->interface_packing
);
240 parcel
.process(block_type
->fields
.array
,
243 blocks
[i
].UniformBufferSize
= parcel
.buffer_size
;
245 blocks
[i
].NumUniforms
=
246 (unsigned)(ptrdiff_t)(&variables
[parcel
.index
] - blocks
[i
].Uniforms
);
251 blocks
[i
].Name
= ralloc_strdup(blocks
, block_type
->name
);
252 blocks
[i
].Uniforms
= &variables
[parcel
.index
];
253 blocks
[i
].Binding
= 0;
254 blocks
[i
].UniformBufferSize
= 0;
256 gl_uniform_block_packing(block_type
->interface_packing
);
258 parcel
.process(block_type
,
259 b
->has_instance_name
? block_type
->name
: "");
261 blocks
[i
].UniformBufferSize
= parcel
.buffer_size
;
263 blocks
[i
].NumUniforms
=
264 (unsigned)(ptrdiff_t)(&variables
[parcel
.index
] - blocks
[i
].Uniforms
);
270 assert(parcel
.index
== num_variables
);
272 _mesa_hash_table_destroy(block_hash
, NULL
);
274 *blocks_ret
= blocks
;
279 link_uniform_blocks_are_compatible(const gl_uniform_block
*a
,
280 const gl_uniform_block
*b
)
282 assert(strcmp(a
->Name
, b
->Name
) == 0);
284 /* Page 35 (page 42 of the PDF) in section 4.3.7 of the GLSL 1.50 spec says:
286 * "Matched block names within an interface (as defined above) must
287 * match in terms of having the same number of declarations with the
288 * same sequence of types and the same sequence of member names, as
289 * well as having the same member-wise layout qualification....if a
290 * matching block is declared as an array, then the array sizes must
291 * also match... Any mismatch will generate a link error."
293 * Arrays are not yet supported, so there is no check for that.
295 if (a
->NumUniforms
!= b
->NumUniforms
)
298 if (a
->_Packing
!= b
->_Packing
)
301 for (unsigned i
= 0; i
< a
->NumUniforms
; i
++) {
302 if (strcmp(a
->Uniforms
[i
].Name
, b
->Uniforms
[i
].Name
) != 0)
305 if (a
->Uniforms
[i
].Type
!= b
->Uniforms
[i
].Type
)
308 if (a
->Uniforms
[i
].RowMajor
!= b
->Uniforms
[i
].RowMajor
)