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"
29 /* These functions are put in a "private" namespace instead of being marked
30 * static so that the unit tests can access them. See
31 * http://code.google.com/p/googletest/wiki/AdvancedGuide#Testing_Private_Code
36 get_storage(gl_uniform_storage
*storage
, unsigned num_storage
,
39 for (unsigned int i
= 0; i
< num_storage
; i
++) {
40 if (strcmp(name
, storage
[i
].name
) == 0)
48 get_uniform_block_index(const gl_shader_program
*shProg
,
49 const char *uniformBlockName
)
51 for (unsigned i
= 0; i
< shProg
->NumUniformBlocks
; i
++) {
52 if (!strcmp(shProg
->UniformBlocks
[i
].Name
, uniformBlockName
))
56 return GL_INVALID_INDEX
;
60 copy_constant_to_storage(union gl_constant_value
*storage
,
61 const ir_constant
*val
,
62 const enum glsl_base_type base_type
,
63 const unsigned int elements
)
65 for (unsigned int i
= 0; i
< elements
; i
++) {
68 storage
[i
].u
= val
->value
.u
[i
];
71 case GLSL_TYPE_SAMPLER
:
72 storage
[i
].i
= val
->value
.i
[i
];
75 storage
[i
].f
= val
->value
.f
[i
];
78 storage
[i
].b
= int(val
->value
.b
[i
]);
81 case GLSL_TYPE_STRUCT
:
83 case GLSL_TYPE_ATOMIC_UINT
:
84 case GLSL_TYPE_INTERFACE
:
87 /* All other types should have already been filtered by other
88 * paths in the caller.
90 assert(!"Should not get here.");
97 set_sampler_binding(gl_shader_program
*prog
, const char *name
, int binding
)
99 struct gl_uniform_storage
*const storage
=
100 get_storage(prog
->UniformStorage
, prog
->NumUserUniformStorage
, name
);
102 if (storage
== NULL
) {
103 assert(storage
!= NULL
);
107 const unsigned elements
= MAX2(storage
->array_elements
, 1);
109 /* Section 4.4.4 (Opaque-Uniform Layout Qualifiers) of the GLSL 4.20 spec
112 * "If the binding identifier is used with an array, the first element
113 * of the array takes the specified unit and each subsequent element
114 * takes the next consecutive unit."
116 for (unsigned int i
= 0; i
< elements
; i
++) {
117 storage
->storage
[i
].i
= binding
+ i
;
120 for (int sh
= 0; sh
< MESA_SHADER_STAGES
; sh
++) {
121 gl_shader
*shader
= prog
->_LinkedShaders
[sh
];
123 if (shader
&& storage
->sampler
[sh
].active
) {
124 for (unsigned i
= 0; i
< elements
; i
++) {
125 unsigned index
= storage
->sampler
[sh
].index
+ i
;
127 shader
->SamplerUnits
[index
] = storage
->storage
[i
].i
;
132 storage
->initialized
= true;
136 set_block_binding(gl_shader_program
*prog
, const char *block_name
, int binding
)
138 const unsigned block_index
= get_uniform_block_index(prog
, block_name
);
140 if (block_index
== GL_INVALID_INDEX
) {
141 assert(block_index
!= GL_INVALID_INDEX
);
145 /* This is a field of a UBO. val is the binding index. */
146 for (int i
= 0; i
< MESA_SHADER_STAGES
; i
++) {
147 int stage_index
= prog
->UniformBlockStageIndex
[i
][block_index
];
149 if (stage_index
!= -1) {
150 struct gl_shader
*sh
= prog
->_LinkedShaders
[i
];
151 sh
->UniformBlocks
[stage_index
].Binding
= binding
;
157 set_uniform_initializer(void *mem_ctx
, gl_shader_program
*prog
,
158 const char *name
, const glsl_type
*type
,
161 if (type
->is_record()) {
162 ir_constant
*field_constant
;
164 field_constant
= (ir_constant
*)val
->components
.get_head();
166 for (unsigned int i
= 0; i
< type
->length
; i
++) {
167 const glsl_type
*field_type
= type
->fields
.structure
[i
].type
;
168 const char *field_name
= ralloc_asprintf(mem_ctx
, "%s.%s", name
,
169 type
->fields
.structure
[i
].name
);
170 set_uniform_initializer(mem_ctx
, prog
, field_name
,
171 field_type
, field_constant
);
172 field_constant
= (ir_constant
*)field_constant
->next
;
175 } else if (type
->is_array() && type
->fields
.array
->is_record()) {
176 const glsl_type
*const element_type
= type
->fields
.array
;
178 for (unsigned int i
= 0; i
< type
->length
; i
++) {
179 const char *element_name
= ralloc_asprintf(mem_ctx
, "%s[%d]", name
, i
);
181 set_uniform_initializer(mem_ctx
, prog
, element_name
,
182 element_type
, val
->array_elements
[i
]);
187 struct gl_uniform_storage
*const storage
=
188 get_storage(prog
->UniformStorage
,
189 prog
->NumUserUniformStorage
,
191 if (storage
== NULL
) {
192 assert(storage
!= NULL
);
196 if (val
->type
->is_array()) {
197 const enum glsl_base_type base_type
=
198 val
->array_elements
[0]->type
->base_type
;
199 const unsigned int elements
= val
->array_elements
[0]->type
->components();
200 unsigned int idx
= 0;
202 assert(val
->type
->length
>= storage
->array_elements
);
203 for (unsigned int i
= 0; i
< storage
->array_elements
; i
++) {
204 copy_constant_to_storage(& storage
->storage
[idx
],
205 val
->array_elements
[i
],
212 copy_constant_to_storage(storage
->storage
,
214 val
->type
->base_type
,
215 val
->type
->components());
217 if (storage
->type
->is_sampler()) {
218 for (int sh
= 0; sh
< MESA_SHADER_STAGES
; sh
++) {
219 gl_shader
*shader
= prog
->_LinkedShaders
[sh
];
221 if (shader
&& storage
->sampler
[sh
].active
) {
222 unsigned index
= storage
->sampler
[sh
].index
;
224 shader
->SamplerUnits
[index
] = storage
->storage
[0].i
;
230 storage
->initialized
= true;
235 link_set_uniform_initializers(struct gl_shader_program
*prog
)
237 void *mem_ctx
= NULL
;
239 for (unsigned int i
= 0; i
< MESA_SHADER_STAGES
; i
++) {
240 struct gl_shader
*shader
= prog
->_LinkedShaders
[i
];
245 foreach_list(node
, shader
->ir
) {
246 ir_variable
*const var
= ((ir_instruction
*) node
)->as_variable();
248 if (!var
|| var
->data
.mode
!= ir_var_uniform
)
252 mem_ctx
= ralloc_context(NULL
);
254 if (var
->data
.explicit_binding
) {
255 const glsl_type
*const type
= var
->type
;
257 if (type
->is_sampler()
258 || (type
->is_array() && type
->fields
.array
->is_sampler())) {
259 linker::set_sampler_binding(prog
, var
->name
, var
->data
.binding
);
260 } else if (var
->is_in_uniform_block()) {
261 const glsl_type
*const iface_type
= var
->get_interface_type();
263 /* If the variable is an array and it is an interface instance,
264 * we need to set the binding for each array element. Just
265 * checking that the variable is an array is not sufficient.
266 * The variable could be an array element of a uniform block
267 * that lacks an instance name. For example:
273 * In this case "f" would pass is_in_uniform_block (above) and
274 * type->is_array(), but it will fail is_interface_instance().
276 if (var
->is_interface_instance() && var
->type
->is_array()) {
277 for (unsigned i
= 0; i
< var
->type
->length
; i
++) {
279 ralloc_asprintf(mem_ctx
, "%s[%u]", iface_type
->name
, i
);
281 /* Section 4.4.3 (Uniform Block Layout Qualifiers) of the
282 * GLSL 4.20 spec says:
284 * "If the binding identifier is used with a uniform
285 * block instanced as an array then the first element
286 * of the array takes the specified block binding and
287 * each subsequent element takes the next consecutive
288 * uniform block binding point."
290 linker::set_block_binding(prog
, name
,
291 var
->data
.binding
+ i
);
294 linker::set_block_binding(prog
, iface_type
->name
,
297 } else if (type
->contains_atomic()) {
298 /* we don't actually need to do anything. */
300 assert(!"Explicit binding not on a sampler, UBO or atomic.");
302 } else if (var
->constant_value
) {
303 linker::set_uniform_initializer(mem_ctx
, prog
, var
->name
,
304 var
->type
, var
->constant_value
);
309 ralloc_free(mem_ctx
);