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
->NumBufferInterfaceBlocks
; i
++) {
52 if (!strcmp(shProg
->BufferInterfaceBlocks
[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
:
92 case GLSL_TYPE_SUBROUTINE
:
93 case GLSL_TYPE_FUNCTION
:
95 /* All other types should have already been filtered by other
96 * paths in the caller.
98 assert(!"Should not get here.");
105 * Initialize an opaque uniform from the value of an explicit binding
106 * qualifier specified in the shader. Atomic counters are different because
107 * they have no storage and should be handled elsewhere.
110 set_opaque_binding(void *mem_ctx
, gl_shader_program
*prog
,
111 const glsl_type
*type
, const char *name
, int *binding
)
114 if (type
->is_array() && type
->fields
.array
->is_array()) {
115 const glsl_type
*const element_type
= type
->fields
.array
;
117 for (unsigned int i
= 0; i
< type
->length
; i
++) {
118 const char *element_name
= ralloc_asprintf(mem_ctx
, "%s[%d]", name
, i
);
120 set_opaque_binding(mem_ctx
, prog
, element_type
,
121 element_name
, binding
);
124 struct gl_uniform_storage
*const storage
=
125 get_storage(prog
->UniformStorage
, prog
->NumUniformStorage
, name
);
127 if (storage
== NULL
) {
128 assert(storage
!= NULL
);
132 const unsigned elements
= MAX2(storage
->array_elements
, 1);
134 /* Section 4.4.4 (Opaque-Uniform Layout Qualifiers) of the GLSL 4.20 spec
137 * "If the binding identifier is used with an array, the first element
138 * of the array takes the specified unit and each subsequent element
139 * takes the next consecutive unit."
141 for (unsigned int i
= 0; i
< elements
; i
++) {
142 storage
->storage
[i
].i
= (*binding
)++;
145 for (int sh
= 0; sh
< MESA_SHADER_STAGES
; sh
++) {
146 gl_shader
*shader
= prog
->_LinkedShaders
[sh
];
149 if (storage
->type
->base_type
== GLSL_TYPE_SAMPLER
&&
150 storage
->opaque
[sh
].active
) {
151 for (unsigned i
= 0; i
< elements
; i
++) {
152 const unsigned index
= storage
->opaque
[sh
].index
+ i
;
153 shader
->SamplerUnits
[index
] = storage
->storage
[i
].i
;
156 } else if (storage
->type
->base_type
== GLSL_TYPE_IMAGE
&&
157 storage
->opaque
[sh
].active
) {
158 for (unsigned i
= 0; i
< elements
; i
++) {
159 const unsigned index
= storage
->opaque
[sh
].index
+ i
;
160 shader
->ImageUnits
[index
] = storage
->storage
[i
].i
;
169 set_block_binding(gl_shader_program
*prog
, const char *block_name
, int binding
)
171 const unsigned block_index
= get_uniform_block_index(prog
, block_name
);
173 if (block_index
== GL_INVALID_INDEX
) {
174 assert(block_index
!= GL_INVALID_INDEX
);
178 /* This is a field of a UBO. val is the binding index. */
179 for (int i
= 0; i
< MESA_SHADER_STAGES
; i
++) {
180 int stage_index
= prog
->InterfaceBlockStageIndex
[i
][block_index
];
182 if (stage_index
!= -1) {
183 struct gl_shader
*sh
= prog
->_LinkedShaders
[i
];
184 sh
->BufferInterfaceBlocks
[stage_index
]->Binding
= binding
;
190 set_uniform_initializer(void *mem_ctx
, gl_shader_program
*prog
,
191 const char *name
, const glsl_type
*type
,
192 ir_constant
*val
, unsigned int boolean_true
)
194 const glsl_type
*t_without_array
= type
->without_array();
195 if (type
->is_record()) {
196 ir_constant
*field_constant
;
198 field_constant
= (ir_constant
*)val
->components
.get_head();
200 for (unsigned int i
= 0; i
< type
->length
; i
++) {
201 const glsl_type
*field_type
= type
->fields
.structure
[i
].type
;
202 const char *field_name
= ralloc_asprintf(mem_ctx
, "%s.%s", name
,
203 type
->fields
.structure
[i
].name
);
204 set_uniform_initializer(mem_ctx
, prog
, field_name
,
205 field_type
, field_constant
, boolean_true
);
206 field_constant
= (ir_constant
*)field_constant
->next
;
209 } else if (t_without_array
->is_record() ||
210 (type
->is_array() && type
->fields
.array
->is_array())) {
211 const glsl_type
*const element_type
= type
->fields
.array
;
213 for (unsigned int i
= 0; i
< type
->length
; i
++) {
214 const char *element_name
= ralloc_asprintf(mem_ctx
, "%s[%d]", name
, i
);
216 set_uniform_initializer(mem_ctx
, prog
, element_name
,
217 element_type
, val
->array_elements
[i
],
223 struct gl_uniform_storage
*const storage
=
224 get_storage(prog
->UniformStorage
,
225 prog
->NumUniformStorage
,
227 if (storage
== NULL
) {
228 assert(storage
!= NULL
);
232 if (val
->type
->is_array()) {
233 const enum glsl_base_type base_type
=
234 val
->array_elements
[0]->type
->base_type
;
235 const unsigned int elements
= val
->array_elements
[0]->type
->components();
236 unsigned int idx
= 0;
237 unsigned dmul
= (base_type
== GLSL_TYPE_DOUBLE
) ? 2 : 1;
239 assert(val
->type
->length
>= storage
->array_elements
);
240 for (unsigned int i
= 0; i
< storage
->array_elements
; i
++) {
241 copy_constant_to_storage(& storage
->storage
[idx
],
242 val
->array_elements
[i
],
247 idx
+= elements
* dmul
;
250 copy_constant_to_storage(storage
->storage
,
252 val
->type
->base_type
,
253 val
->type
->components(),
256 if (storage
->type
->is_sampler()) {
257 for (int sh
= 0; sh
< MESA_SHADER_STAGES
; sh
++) {
258 gl_shader
*shader
= prog
->_LinkedShaders
[sh
];
260 if (shader
&& storage
->opaque
[sh
].active
) {
261 unsigned index
= storage
->opaque
[sh
].index
;
263 shader
->SamplerUnits
[index
] = storage
->storage
[0].i
;
272 link_set_uniform_initializers(struct gl_shader_program
*prog
,
273 unsigned int boolean_true
)
275 void *mem_ctx
= NULL
;
277 for (unsigned int i
= 0; i
< MESA_SHADER_STAGES
; i
++) {
278 struct gl_shader
*shader
= prog
->_LinkedShaders
[i
];
283 foreach_in_list(ir_instruction
, node
, shader
->ir
) {
284 ir_variable
*const var
= node
->as_variable();
286 if (!var
|| (var
->data
.mode
!= ir_var_uniform
&&
287 var
->data
.mode
!= ir_var_shader_storage
))
291 mem_ctx
= ralloc_context(NULL
);
293 if (var
->data
.explicit_binding
) {
294 const glsl_type
*const type
= var
->type
;
296 if (type
->without_array()->is_sampler() ||
297 type
->without_array()->is_image()) {
298 int binding
= var
->data
.binding
;
299 linker::set_opaque_binding(mem_ctx
, prog
, var
->type
,
300 var
->name
, &binding
);
301 } else if (var
->is_in_buffer_block()) {
302 const glsl_type
*const iface_type
= var
->get_interface_type();
304 /* If the variable is an array and it is an interface instance,
305 * we need to set the binding for each array element. Just
306 * checking that the variable is an array is not sufficient.
307 * The variable could be an array element of a uniform block
308 * that lacks an instance name. For example:
314 * In this case "f" would pass is_in_buffer_block (above) and
315 * type->is_array(), but it will fail is_interface_instance().
317 if (var
->is_interface_instance() && var
->type
->is_array()) {
318 for (unsigned i
= 0; i
< var
->type
->length
; i
++) {
320 ralloc_asprintf(mem_ctx
, "%s[%u]", iface_type
->name
, i
);
322 /* Section 4.4.3 (Uniform Block Layout Qualifiers) of the
323 * GLSL 4.20 spec says:
325 * "If the binding identifier is used with a uniform
326 * block instanced as an array then the first element
327 * of the array takes the specified block binding and
328 * each subsequent element takes the next consecutive
329 * uniform block binding point."
331 linker::set_block_binding(prog
, name
,
332 var
->data
.binding
+ i
);
335 linker::set_block_binding(prog
, iface_type
->name
,
338 } else if (type
->contains_atomic()) {
339 /* we don't actually need to do anything. */
341 assert(!"Explicit binding not on a sampler, UBO or atomic.");
343 } else if (var
->constant_initializer
) {
344 linker::set_uniform_initializer(mem_ctx
, prog
, var
->name
,
345 var
->type
, var
->constant_initializer
,
351 ralloc_free(mem_ctx
);