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 "util/string_to_uint_map.h"
30 /* These functions are put in a "private" namespace instead of being marked
31 * static so that the unit tests can access them. See
32 * http://code.google.com/p/googletest/wiki/AdvancedGuide#Testing_Private_Code
37 get_storage(struct gl_shader_program
*prog
, const char *name
)
40 if (prog
->UniformHash
->get(id
, name
))
41 return &prog
->data
->UniformStorage
[id
];
43 assert(!"No uniform storage found!");
48 copy_constant_to_storage(union gl_constant_value
*storage
,
49 const ir_constant
*val
,
50 const enum glsl_base_type base_type
,
51 const unsigned int elements
,
52 unsigned int boolean_true
)
54 for (unsigned int i
= 0; i
< elements
; i
++) {
57 storage
[i
].u
= val
->value
.u
[i
];
60 case GLSL_TYPE_SAMPLER
:
61 storage
[i
].i
= val
->value
.i
[i
];
64 storage
[i
].f
= val
->value
.f
[i
];
66 case GLSL_TYPE_DOUBLE
:
67 /* XXX need to check on big-endian */
68 memcpy(&storage
[i
* 2].u
, &val
->value
.d
[i
], sizeof(double));
71 storage
[i
].b
= val
->value
.b
[i
] ? boolean_true
: 0;
74 case GLSL_TYPE_STRUCT
:
76 case GLSL_TYPE_ATOMIC_UINT
:
77 case GLSL_TYPE_INTERFACE
:
79 case GLSL_TYPE_SUBROUTINE
:
80 case GLSL_TYPE_FUNCTION
:
82 /* All other types should have already been filtered by other
83 * paths in the caller.
85 assert(!"Should not get here.");
92 * Initialize an opaque uniform from the value of an explicit binding
93 * qualifier specified in the shader. Atomic counters are different because
94 * they have no storage and should be handled elsewhere.
97 set_opaque_binding(void *mem_ctx
, gl_shader_program
*prog
,
98 const glsl_type
*type
, const char *name
, int *binding
)
101 if (type
->is_array() && type
->fields
.array
->is_array()) {
102 const glsl_type
*const element_type
= type
->fields
.array
;
104 for (unsigned int i
= 0; i
< type
->length
; i
++) {
105 const char *element_name
= ralloc_asprintf(mem_ctx
, "%s[%d]", name
, i
);
107 set_opaque_binding(mem_ctx
, prog
, element_type
,
108 element_name
, binding
);
111 struct gl_uniform_storage
*const storage
= get_storage(prog
, name
);
116 const unsigned elements
= MAX2(storage
->array_elements
, 1);
118 /* Section 4.4.4 (Opaque-Uniform Layout Qualifiers) of the GLSL 4.20 spec
121 * "If the binding identifier is used with an array, the first element
122 * of the array takes the specified unit and each subsequent element
123 * takes the next consecutive unit."
125 for (unsigned int i
= 0; i
< elements
; i
++) {
126 storage
->storage
[i
].i
= (*binding
)++;
129 for (int sh
= 0; sh
< MESA_SHADER_STAGES
; sh
++) {
130 gl_linked_shader
*shader
= prog
->_LinkedShaders
[sh
];
133 if (storage
->type
->base_type
== GLSL_TYPE_SAMPLER
&&
134 storage
->opaque
[sh
].active
) {
135 for (unsigned i
= 0; i
< elements
; i
++) {
136 const unsigned index
= storage
->opaque
[sh
].index
+ i
;
137 shader
->SamplerUnits
[index
] = storage
->storage
[i
].i
;
140 } else if (storage
->type
->base_type
== GLSL_TYPE_IMAGE
&&
141 storage
->opaque
[sh
].active
) {
142 for (unsigned i
= 0; i
< elements
; i
++) {
143 const unsigned index
= storage
->opaque
[sh
].index
+ i
;
144 if (index
>= ARRAY_SIZE(shader
->ImageUnits
))
146 shader
->ImageUnits
[index
] = storage
->storage
[i
].i
;
155 set_block_binding(gl_shader_program
*prog
, const char *block_name
,
156 unsigned mode
, int binding
)
158 unsigned num_blocks
= mode
== ir_var_uniform
?
159 prog
->data
->NumUniformBlocks
:
160 prog
->data
->NumShaderStorageBlocks
;
161 struct gl_uniform_block
*blks
= mode
== ir_var_uniform
?
162 prog
->data
->UniformBlocks
: prog
->data
->ShaderStorageBlocks
;
164 for (unsigned i
= 0; i
< num_blocks
; i
++) {
165 if (!strcmp(blks
[i
].Name
, block_name
)) {
166 blks
[i
].Binding
= binding
;
171 unreachable("Failed to initialize block binding");
175 set_uniform_initializer(void *mem_ctx
, gl_shader_program
*prog
,
176 const char *name
, const glsl_type
*type
,
177 ir_constant
*val
, unsigned int boolean_true
)
179 const glsl_type
*t_without_array
= type
->without_array();
180 if (type
->is_record()) {
181 ir_constant
*field_constant
;
183 field_constant
= (ir_constant
*)val
->components
.get_head();
185 for (unsigned int i
= 0; i
< type
->length
; i
++) {
186 const glsl_type
*field_type
= type
->fields
.structure
[i
].type
;
187 const char *field_name
= ralloc_asprintf(mem_ctx
, "%s.%s", name
,
188 type
->fields
.structure
[i
].name
);
189 set_uniform_initializer(mem_ctx
, prog
, field_name
,
190 field_type
, field_constant
, boolean_true
);
191 field_constant
= (ir_constant
*)field_constant
->next
;
194 } else if (t_without_array
->is_record() ||
195 (type
->is_array() && type
->fields
.array
->is_array())) {
196 const glsl_type
*const element_type
= type
->fields
.array
;
198 for (unsigned int i
= 0; i
< type
->length
; i
++) {
199 const char *element_name
= ralloc_asprintf(mem_ctx
, "%s[%d]", name
, i
);
201 set_uniform_initializer(mem_ctx
, prog
, element_name
,
202 element_type
, val
->array_elements
[i
],
208 struct gl_uniform_storage
*const storage
= get_storage(prog
, name
);
213 if (val
->type
->is_array()) {
214 const enum glsl_base_type base_type
=
215 val
->array_elements
[0]->type
->base_type
;
216 const unsigned int elements
= val
->array_elements
[0]->type
->components();
217 unsigned int idx
= 0;
218 unsigned dmul
= glsl_base_type_is_64bit(base_type
) ? 2 : 1;
220 assert(val
->type
->length
>= storage
->array_elements
);
221 for (unsigned int i
= 0; i
< storage
->array_elements
; i
++) {
222 copy_constant_to_storage(& storage
->storage
[idx
],
223 val
->array_elements
[i
],
228 idx
+= elements
* dmul
;
231 copy_constant_to_storage(storage
->storage
,
233 val
->type
->base_type
,
234 val
->type
->components(),
237 if (storage
->type
->is_sampler()) {
238 for (int sh
= 0; sh
< MESA_SHADER_STAGES
; sh
++) {
239 gl_linked_shader
*shader
= prog
->_LinkedShaders
[sh
];
241 if (shader
&& storage
->opaque
[sh
].active
) {
242 unsigned index
= storage
->opaque
[sh
].index
;
244 shader
->SamplerUnits
[index
] = storage
->storage
[0].i
;
253 link_set_uniform_initializers(struct gl_shader_program
*prog
,
254 unsigned int boolean_true
)
256 void *mem_ctx
= NULL
;
258 for (unsigned int i
= 0; i
< MESA_SHADER_STAGES
; i
++) {
259 struct gl_linked_shader
*shader
= prog
->_LinkedShaders
[i
];
264 foreach_in_list(ir_instruction
, node
, shader
->ir
) {
265 ir_variable
*const var
= node
->as_variable();
267 if (!var
|| (var
->data
.mode
!= ir_var_uniform
&&
268 var
->data
.mode
!= ir_var_shader_storage
))
272 mem_ctx
= ralloc_context(NULL
);
274 if (var
->data
.explicit_binding
) {
275 const glsl_type
*const type
= var
->type
;
277 if (type
->without_array()->is_sampler() ||
278 type
->without_array()->is_image()) {
279 int binding
= var
->data
.binding
;
280 linker::set_opaque_binding(mem_ctx
, prog
, var
->type
,
281 var
->name
, &binding
);
282 } else if (var
->is_in_buffer_block()) {
283 const glsl_type
*const iface_type
= var
->get_interface_type();
285 /* If the variable is an array and it is an interface instance,
286 * we need to set the binding for each array element. Just
287 * checking that the variable is an array is not sufficient.
288 * The variable could be an array element of a uniform block
289 * that lacks an instance name. For example:
295 * In this case "f" would pass is_in_buffer_block (above) and
296 * type->is_array(), but it will fail is_interface_instance().
298 if (var
->is_interface_instance() && var
->type
->is_array()) {
299 for (unsigned i
= 0; i
< var
->type
->length
; i
++) {
301 ralloc_asprintf(mem_ctx
, "%s[%u]", iface_type
->name
, i
);
303 /* Section 4.4.3 (Uniform Block Layout Qualifiers) of the
304 * GLSL 4.20 spec says:
306 * "If the binding identifier is used with a uniform
307 * block instanced as an array then the first element
308 * of the array takes the specified block binding and
309 * each subsequent element takes the next consecutive
310 * uniform block binding point."
312 linker::set_block_binding(prog
, name
, var
->data
.mode
,
313 var
->data
.binding
+ i
);
316 linker::set_block_binding(prog
, iface_type
->name
,
320 } else if (type
->contains_atomic()) {
321 /* we don't actually need to do anything. */
323 assert(!"Explicit binding not on a sampler, UBO or atomic.");
325 } else if (var
->constant_initializer
) {
326 linker::set_uniform_initializer(mem_ctx
, prog
, var
->name
,
327 var
->type
, var
->constant_initializer
,
333 ralloc_free(mem_ctx
);