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
,
64 unsigned int boolean_true
)
66 for (unsigned int i
= 0; i
< elements
; i
++) {
69 storage
[i
].u
= val
->value
.u
[i
];
72 case GLSL_TYPE_SAMPLER
:
73 storage
[i
].i
= val
->value
.i
[i
];
76 storage
[i
].f
= val
->value
.f
[i
];
78 case GLSL_TYPE_DOUBLE
:
79 /* XXX need to check on big-endian */
80 storage
[i
* 2].u
= *(uint32_t *)&val
->value
.d
[i
];
81 storage
[i
* 2 + 1].u
= *(((uint32_t *)&val
->value
.d
[i
]) + 1);
84 storage
[i
].b
= val
->value
.b
[i
] ? boolean_true
: 0;
87 case GLSL_TYPE_STRUCT
:
89 case GLSL_TYPE_ATOMIC_UINT
:
90 case GLSL_TYPE_INTERFACE
:
91 case GLSL_TYPE_FUNCTION
:
93 case GLSL_TYPE_SUBROUTINE
:
95 /* All other types should have already been filtered by other
96 * paths in the caller.
98 assert(!"Should not get here.");
105 set_sampler_binding(gl_shader_program
*prog
, const char *name
, int binding
)
107 struct gl_uniform_storage
*const storage
=
108 get_storage(prog
->UniformStorage
, prog
->NumUniformStorage
, name
);
110 if (storage
== NULL
) {
111 assert(storage
!= NULL
);
115 const unsigned elements
= MAX2(storage
->array_elements
, 1);
117 /* Section 4.4.4 (Opaque-Uniform Layout Qualifiers) of the GLSL 4.20 spec
120 * "If the binding identifier is used with an array, the first element
121 * of the array takes the specified unit and each subsequent element
122 * takes the next consecutive unit."
124 for (unsigned int i
= 0; i
< elements
; i
++) {
125 storage
->storage
[i
].i
= binding
+ i
;
128 for (int sh
= 0; sh
< MESA_SHADER_STAGES
; sh
++) {
129 gl_shader
*shader
= prog
->_LinkedShaders
[sh
];
131 if (shader
&& storage
->sampler
[sh
].active
) {
132 for (unsigned i
= 0; i
< elements
; i
++) {
133 unsigned index
= storage
->sampler
[sh
].index
+ i
;
135 shader
->SamplerUnits
[index
] = storage
->storage
[i
].i
;
140 storage
->initialized
= true;
144 set_block_binding(gl_shader_program
*prog
, const char *block_name
, int binding
)
146 const unsigned block_index
= get_uniform_block_index(prog
, block_name
);
148 if (block_index
== GL_INVALID_INDEX
) {
149 assert(block_index
!= GL_INVALID_INDEX
);
153 /* This is a field of a UBO. val is the binding index. */
154 for (int i
= 0; i
< MESA_SHADER_STAGES
; i
++) {
155 int stage_index
= prog
->UniformBlockStageIndex
[i
][block_index
];
157 if (stage_index
!= -1) {
158 struct gl_shader
*sh
= prog
->_LinkedShaders
[i
];
159 sh
->UniformBlocks
[stage_index
].Binding
= binding
;
165 set_uniform_initializer(void *mem_ctx
, gl_shader_program
*prog
,
166 const char *name
, const glsl_type
*type
,
167 ir_constant
*val
, unsigned int boolean_true
)
169 if (type
->is_record()) {
170 ir_constant
*field_constant
;
172 field_constant
= (ir_constant
*)val
->components
.get_head();
174 for (unsigned int i
= 0; i
< type
->length
; i
++) {
175 const glsl_type
*field_type
= type
->fields
.structure
[i
].type
;
176 const char *field_name
= ralloc_asprintf(mem_ctx
, "%s.%s", name
,
177 type
->fields
.structure
[i
].name
);
178 set_uniform_initializer(mem_ctx
, prog
, field_name
,
179 field_type
, field_constant
, boolean_true
);
180 field_constant
= (ir_constant
*)field_constant
->next
;
183 } else if (type
->is_array() && type
->fields
.array
->is_record()) {
184 const glsl_type
*const element_type
= type
->fields
.array
;
186 for (unsigned int i
= 0; i
< type
->length
; i
++) {
187 const char *element_name
= ralloc_asprintf(mem_ctx
, "%s[%d]", name
, i
);
189 set_uniform_initializer(mem_ctx
, prog
, element_name
,
190 element_type
, val
->array_elements
[i
],
196 struct gl_uniform_storage
*const storage
=
197 get_storage(prog
->UniformStorage
,
198 prog
->NumUniformStorage
,
200 if (storage
== NULL
) {
201 assert(storage
!= NULL
);
205 if (val
->type
->is_array()) {
206 const enum glsl_base_type base_type
=
207 val
->array_elements
[0]->type
->base_type
;
208 const unsigned int elements
= val
->array_elements
[0]->type
->components();
209 unsigned int idx
= 0;
210 unsigned dmul
= (base_type
== GLSL_TYPE_DOUBLE
) ? 2 : 1;
212 assert(val
->type
->length
>= storage
->array_elements
);
213 for (unsigned int i
= 0; i
< storage
->array_elements
; i
++) {
214 copy_constant_to_storage(& storage
->storage
[idx
],
215 val
->array_elements
[i
],
220 idx
+= elements
* dmul
;
223 copy_constant_to_storage(storage
->storage
,
225 val
->type
->base_type
,
226 val
->type
->components(),
229 if (storage
->type
->is_sampler()) {
230 for (int sh
= 0; sh
< MESA_SHADER_STAGES
; sh
++) {
231 gl_shader
*shader
= prog
->_LinkedShaders
[sh
];
233 if (shader
&& storage
->sampler
[sh
].active
) {
234 unsigned index
= storage
->sampler
[sh
].index
;
236 shader
->SamplerUnits
[index
] = storage
->storage
[0].i
;
242 storage
->initialized
= true;
247 link_set_uniform_initializers(struct gl_shader_program
*prog
,
248 unsigned int boolean_true
)
250 void *mem_ctx
= NULL
;
252 for (unsigned int i
= 0; i
< MESA_SHADER_STAGES
; i
++) {
253 struct gl_shader
*shader
= prog
->_LinkedShaders
[i
];
258 foreach_in_list(ir_instruction
, node
, shader
->ir
) {
259 ir_variable
*const var
= node
->as_variable();
261 if (!var
|| (var
->data
.mode
!= ir_var_uniform
&&
262 var
->data
.mode
!= ir_var_shader_storage
))
266 mem_ctx
= ralloc_context(NULL
);
268 if (var
->data
.explicit_binding
) {
269 const glsl_type
*const type
= var
->type
;
271 if (type
->without_array()->is_sampler()) {
272 linker::set_sampler_binding(prog
, var
->name
, var
->data
.binding
);
273 } else if (var
->is_in_buffer_block()) {
274 const glsl_type
*const iface_type
= var
->get_interface_type();
276 /* If the variable is an array and it is an interface instance,
277 * we need to set the binding for each array element. Just
278 * checking that the variable is an array is not sufficient.
279 * The variable could be an array element of a uniform block
280 * that lacks an instance name. For example:
286 * In this case "f" would pass is_in_buffer_block (above) and
287 * type->is_array(), but it will fail is_interface_instance().
289 if (var
->is_interface_instance() && var
->type
->is_array()) {
290 for (unsigned i
= 0; i
< var
->type
->length
; i
++) {
292 ralloc_asprintf(mem_ctx
, "%s[%u]", iface_type
->name
, i
);
294 /* Section 4.4.3 (Uniform Block Layout Qualifiers) of the
295 * GLSL 4.20 spec says:
297 * "If the binding identifier is used with a uniform
298 * block instanced as an array then the first element
299 * of the array takes the specified block binding and
300 * each subsequent element takes the next consecutive
301 * uniform block binding point."
303 linker::set_block_binding(prog
, name
,
304 var
->data
.binding
+ i
);
307 linker::set_block_binding(prog
, iface_type
->name
,
310 } else if (type
->contains_atomic()) {
311 /* we don't actually need to do anything. */
313 assert(!"Explicit binding not on a sampler, UBO or atomic.");
315 } else if (var
->constant_value
) {
316 linker::set_uniform_initializer(mem_ctx
, prog
, var
->name
,
317 var
->type
, var
->constant_value
,
323 ralloc_free(mem_ctx
);