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 "glsl_symbol_table.h"
29 #include "program/hash_table.h"
31 /* These functions are put in a "private" namespace instead of being marked
32 * static so that the unit tests can access them. See
33 * http://code.google.com/p/googletest/wiki/AdvancedGuide#Testing_Private_Code
38 get_storage(gl_uniform_storage
*storage
, unsigned num_storage
,
41 for (unsigned int i
= 0; i
< num_storage
; i
++) {
42 if (strcmp(name
, storage
[i
].name
) == 0)
50 get_uniform_block_index(const gl_shader_program
*shProg
,
51 const char *uniformBlockName
)
53 for (unsigned i
= 0; i
< shProg
->NumUniformBlocks
; i
++) {
54 if (!strcmp(shProg
->UniformBlocks
[i
].Name
, uniformBlockName
))
58 return GL_INVALID_INDEX
;
62 copy_constant_to_storage(union gl_constant_value
*storage
,
63 const ir_constant
*val
,
64 const enum glsl_base_type base_type
,
65 const unsigned int elements
)
67 for (unsigned int i
= 0; i
< elements
; i
++) {
70 storage
[i
].u
= val
->value
.u
[i
];
73 case GLSL_TYPE_SAMPLER
:
74 storage
[i
].i
= val
->value
.i
[i
];
77 storage
[i
].f
= val
->value
.f
[i
];
80 storage
[i
].b
= int(val
->value
.b
[i
]);
83 case GLSL_TYPE_STRUCT
:
85 case GLSL_TYPE_ATOMIC_UINT
:
86 case GLSL_TYPE_INTERFACE
:
89 /* All other types should have already been filtered by other
90 * paths in the caller.
92 assert(!"Should not get here.");
99 set_sampler_binding(gl_shader_program
*prog
, const char *name
, int binding
)
101 struct gl_uniform_storage
*const storage
=
102 get_storage(prog
->UniformStorage
, prog
->NumUserUniformStorage
, name
);
104 if (storage
== NULL
) {
105 assert(storage
!= NULL
);
109 const unsigned elements
= MAX2(storage
->array_elements
, 1);
111 /* Section 4.4.4 (Opaque-Uniform Layout Qualifiers) of the GLSL 4.20 spec
114 * "If the binding identifier is used with an array, the first element
115 * of the array takes the specified unit and each subsequent element
116 * takes the next consecutive unit."
118 for (unsigned int i
= 0; i
< elements
; i
++) {
119 storage
->storage
[i
].i
= binding
+ i
;
122 for (int sh
= 0; sh
< MESA_SHADER_STAGES
; sh
++) {
123 gl_shader
*shader
= prog
->_LinkedShaders
[sh
];
125 if (shader
&& storage
->sampler
[sh
].active
) {
126 for (unsigned i
= 0; i
< elements
; i
++) {
127 unsigned index
= storage
->sampler
[sh
].index
+ i
;
129 shader
->SamplerUnits
[index
] = storage
->storage
[i
].i
;
134 storage
->initialized
= true;
138 set_block_binding(gl_shader_program
*prog
, const char *block_name
, int binding
)
140 const unsigned block_index
= get_uniform_block_index(prog
, block_name
);
142 if (block_index
== GL_INVALID_INDEX
) {
143 assert(block_index
!= GL_INVALID_INDEX
);
147 /* This is a field of a UBO. val is the binding index. */
148 for (int i
= 0; i
< MESA_SHADER_STAGES
; i
++) {
149 int stage_index
= prog
->UniformBlockStageIndex
[i
][block_index
];
151 if (stage_index
!= -1) {
152 struct gl_shader
*sh
= prog
->_LinkedShaders
[i
];
153 sh
->UniformBlocks
[stage_index
].Binding
= binding
;
159 set_uniform_initializer(void *mem_ctx
, gl_shader_program
*prog
,
160 const char *name
, const glsl_type
*type
,
163 if (type
->is_record()) {
164 ir_constant
*field_constant
;
166 field_constant
= (ir_constant
*)val
->components
.get_head();
168 for (unsigned int i
= 0; i
< type
->length
; i
++) {
169 const glsl_type
*field_type
= type
->fields
.structure
[i
].type
;
170 const char *field_name
= ralloc_asprintf(mem_ctx
, "%s.%s", name
,
171 type
->fields
.structure
[i
].name
);
172 set_uniform_initializer(mem_ctx
, prog
, field_name
,
173 field_type
, field_constant
);
174 field_constant
= (ir_constant
*)field_constant
->next
;
177 } else if (type
->is_array() && type
->fields
.array
->is_record()) {
178 const glsl_type
*const element_type
= type
->fields
.array
;
180 for (unsigned int i
= 0; i
< type
->length
; i
++) {
181 const char *element_name
= ralloc_asprintf(mem_ctx
, "%s[%d]", name
, i
);
183 set_uniform_initializer(mem_ctx
, prog
, element_name
,
184 element_type
, val
->array_elements
[i
]);
189 struct gl_uniform_storage
*const storage
=
190 get_storage(prog
->UniformStorage
,
191 prog
->NumUserUniformStorage
,
193 if (storage
== NULL
) {
194 assert(storage
!= NULL
);
198 if (val
->type
->is_array()) {
199 const enum glsl_base_type base_type
=
200 val
->array_elements
[0]->type
->base_type
;
201 const unsigned int elements
= val
->array_elements
[0]->type
->components();
202 unsigned int idx
= 0;
204 assert(val
->type
->length
>= storage
->array_elements
);
205 for (unsigned int i
= 0; i
< storage
->array_elements
; i
++) {
206 copy_constant_to_storage(& storage
->storage
[idx
],
207 val
->array_elements
[i
],
214 copy_constant_to_storage(storage
->storage
,
216 val
->type
->base_type
,
217 val
->type
->components());
219 if (storage
->type
->is_sampler()) {
220 for (int sh
= 0; sh
< MESA_SHADER_STAGES
; sh
++) {
221 gl_shader
*shader
= prog
->_LinkedShaders
[sh
];
223 if (shader
&& storage
->sampler
[sh
].active
) {
224 unsigned index
= storage
->sampler
[sh
].index
;
226 shader
->SamplerUnits
[index
] = storage
->storage
[0].i
;
232 storage
->initialized
= true;
237 link_set_uniform_initializers(struct gl_shader_program
*prog
)
239 void *mem_ctx
= NULL
;
241 for (unsigned int i
= 0; i
< MESA_SHADER_STAGES
; i
++) {
242 struct gl_shader
*shader
= prog
->_LinkedShaders
[i
];
247 foreach_list(node
, shader
->ir
) {
248 ir_variable
*const var
= ((ir_instruction
*) node
)->as_variable();
250 if (!var
|| var
->data
.mode
!= ir_var_uniform
)
254 mem_ctx
= ralloc_context(NULL
);
256 if (var
->data
.explicit_binding
) {
257 const glsl_type
*const type
= var
->type
;
259 if (type
->is_sampler()
260 || (type
->is_array() && type
->fields
.array
->is_sampler())) {
261 linker::set_sampler_binding(prog
, var
->name
, var
->data
.binding
);
262 } else if (var
->is_in_uniform_block()) {
263 const glsl_type
*const iface_type
= var
->get_interface_type();
265 /* If the variable is an array and it is an interface instance,
266 * we need to set the binding for each array element. Just
267 * checking that the variable is an array is not sufficient.
268 * The variable could be an array element of a uniform block
269 * that lacks an instance name. For example:
275 * In this case "f" would pass is_in_uniform_block (above) and
276 * type->is_array(), but it will fail is_interface_instance().
278 if (var
->is_interface_instance() && var
->type
->is_array()) {
279 for (unsigned i
= 0; i
< var
->type
->length
; i
++) {
281 ralloc_asprintf(mem_ctx
, "%s[%u]", iface_type
->name
, i
);
283 /* Section 4.4.3 (Uniform Block Layout Qualifiers) of the
284 * GLSL 4.20 spec says:
286 * "If the binding identifier is used with a uniform
287 * block instanced as an array then the first element
288 * of the array takes the specified block binding and
289 * each subsequent element takes the next consecutive
290 * uniform block binding point."
292 linker::set_block_binding(prog
, name
,
293 var
->data
.binding
+ i
);
296 linker::set_block_binding(prog
, iface_type
->name
,
299 } else if (type
->contains_atomic()) {
300 /* we don't actually need to do anything. */
302 assert(!"Explicit binding not on a sampler, UBO or atomic.");
304 } else if (var
->constant_value
) {
305 linker::set_uniform_initializer(mem_ctx
, prog
, var
->name
,
306 var
->type
, var
->constant_value
);
311 ralloc_free(mem_ctx
);