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 case GLSL_TYPE_UINT64
:
69 /* XXX need to check on big-endian */
70 memcpy(&storage
[i
* 2].u
, &val
->value
.d
[i
], sizeof(double));
73 storage
[i
].b
= val
->value
.b
[i
] ? boolean_true
: 0;
76 case GLSL_TYPE_STRUCT
:
78 case GLSL_TYPE_ATOMIC_UINT
:
79 case GLSL_TYPE_INTERFACE
:
81 case GLSL_TYPE_SUBROUTINE
:
82 case GLSL_TYPE_FUNCTION
:
84 /* All other types should have already been filtered by other
85 * paths in the caller.
87 assert(!"Should not get here.");
94 * Initialize an opaque uniform from the value of an explicit binding
95 * qualifier specified in the shader. Atomic counters are different because
96 * they have no storage and should be handled elsewhere.
99 set_opaque_binding(void *mem_ctx
, gl_shader_program
*prog
,
100 const ir_variable
*var
, const glsl_type
*type
,
101 const char *name
, int *binding
)
104 if (type
->is_array() && type
->fields
.array
->is_array()) {
105 const glsl_type
*const element_type
= type
->fields
.array
;
107 for (unsigned int i
= 0; i
< type
->length
; i
++) {
108 const char *element_name
= ralloc_asprintf(mem_ctx
, "%s[%d]", name
, i
);
110 set_opaque_binding(mem_ctx
, prog
, var
, element_type
,
111 element_name
, binding
);
114 struct gl_uniform_storage
*const storage
= get_storage(prog
, name
);
119 const unsigned elements
= MAX2(storage
->array_elements
, 1);
121 /* Section 4.4.6 (Opaque-Uniform Layout Qualifiers) of the GLSL 4.50 spec
124 * "If the binding identifier is used with an array, the first element
125 * of the array takes the specified unit and each subsequent element
126 * takes the next consecutive unit."
128 for (unsigned int i
= 0; i
< elements
; i
++) {
129 storage
->storage
[i
].i
= (*binding
)++;
132 for (int sh
= 0; sh
< MESA_SHADER_STAGES
; sh
++) {
133 gl_linked_shader
*shader
= prog
->_LinkedShaders
[sh
];
137 if (!storage
->opaque
[sh
].active
)
140 if (storage
->type
->is_sampler()) {
141 for (unsigned i
= 0; i
< elements
; i
++) {
142 const unsigned index
= storage
->opaque
[sh
].index
+ i
;
144 if (var
->data
.bindless
) {
145 if (index
>= shader
->Program
->sh
.NumBindlessSamplers
)
147 shader
->Program
->sh
.BindlessSamplers
[index
].unit
=
148 storage
->storage
[i
].i
;
149 shader
->Program
->sh
.BindlessSamplers
[index
].bound
= true;
150 shader
->Program
->sh
.HasBoundBindlessSampler
= true;
152 if (index
>= ARRAY_SIZE(shader
->Program
->SamplerUnits
))
154 shader
->Program
->SamplerUnits
[index
] =
155 storage
->storage
[i
].i
;
158 } else if (storage
->type
->is_image()) {
159 for (unsigned i
= 0; i
< elements
; i
++) {
160 const unsigned index
= storage
->opaque
[sh
].index
+ i
;
163 if (var
->data
.bindless
) {
164 if (index
>= shader
->Program
->sh
.NumBindlessImages
)
166 shader
->Program
->sh
.BindlessImages
[index
].unit
=
167 storage
->storage
[i
].i
;
168 shader
->Program
->sh
.BindlessImages
[index
].bound
= true;
169 shader
->Program
->sh
.HasBoundBindlessImage
= true;
171 if (index
>= ARRAY_SIZE(shader
->Program
->sh
.ImageUnits
))
173 shader
->Program
->sh
.ImageUnits
[index
] =
174 storage
->storage
[i
].i
;
183 set_block_binding(gl_shader_program
*prog
, const char *block_name
,
184 unsigned mode
, int binding
)
186 unsigned num_blocks
= mode
== ir_var_uniform
?
187 prog
->data
->NumUniformBlocks
:
188 prog
->data
->NumShaderStorageBlocks
;
189 struct gl_uniform_block
*blks
= mode
== ir_var_uniform
?
190 prog
->data
->UniformBlocks
: prog
->data
->ShaderStorageBlocks
;
192 for (unsigned i
= 0; i
< num_blocks
; i
++) {
193 if (!strcmp(blks
[i
].Name
, block_name
)) {
194 blks
[i
].Binding
= binding
;
199 unreachable("Failed to initialize block binding");
203 set_uniform_initializer(void *mem_ctx
, gl_shader_program
*prog
,
204 const char *name
, const glsl_type
*type
,
205 ir_constant
*val
, unsigned int boolean_true
)
207 const glsl_type
*t_without_array
= type
->without_array();
208 if (type
->is_record()) {
209 ir_constant
*field_constant
;
211 field_constant
= (ir_constant
*)val
->components
.get_head();
213 for (unsigned int i
= 0; i
< type
->length
; i
++) {
214 const glsl_type
*field_type
= type
->fields
.structure
[i
].type
;
215 const char *field_name
= ralloc_asprintf(mem_ctx
, "%s.%s", name
,
216 type
->fields
.structure
[i
].name
);
217 set_uniform_initializer(mem_ctx
, prog
, field_name
,
218 field_type
, field_constant
, boolean_true
);
219 field_constant
= (ir_constant
*)field_constant
->next
;
222 } else if (t_without_array
->is_record() ||
223 (type
->is_array() && type
->fields
.array
->is_array())) {
224 const glsl_type
*const element_type
= type
->fields
.array
;
226 for (unsigned int i
= 0; i
< type
->length
; i
++) {
227 const char *element_name
= ralloc_asprintf(mem_ctx
, "%s[%d]", name
, i
);
229 set_uniform_initializer(mem_ctx
, prog
, element_name
,
230 element_type
, val
->array_elements
[i
],
236 struct gl_uniform_storage
*const storage
= get_storage(prog
, name
);
241 if (val
->type
->is_array()) {
242 const enum glsl_base_type base_type
=
243 val
->array_elements
[0]->type
->base_type
;
244 const unsigned int elements
= val
->array_elements
[0]->type
->components();
245 unsigned int idx
= 0;
246 unsigned dmul
= glsl_base_type_is_64bit(base_type
) ? 2 : 1;
248 assert(val
->type
->length
>= storage
->array_elements
);
249 for (unsigned int i
= 0; i
< storage
->array_elements
; i
++) {
250 copy_constant_to_storage(& storage
->storage
[idx
],
251 val
->array_elements
[i
],
256 idx
+= elements
* dmul
;
259 copy_constant_to_storage(storage
->storage
,
261 val
->type
->base_type
,
262 val
->type
->components(),
265 if (storage
->type
->is_sampler()) {
266 for (int sh
= 0; sh
< MESA_SHADER_STAGES
; sh
++) {
267 gl_linked_shader
*shader
= prog
->_LinkedShaders
[sh
];
269 if (shader
&& storage
->opaque
[sh
].active
) {
270 unsigned index
= storage
->opaque
[sh
].index
;
272 shader
->Program
->SamplerUnits
[index
] = storage
->storage
[0].i
;
281 link_set_uniform_initializers(struct gl_shader_program
*prog
,
282 unsigned int boolean_true
)
284 void *mem_ctx
= NULL
;
286 for (unsigned int i
= 0; i
< MESA_SHADER_STAGES
; i
++) {
287 struct gl_linked_shader
*shader
= prog
->_LinkedShaders
[i
];
292 foreach_in_list(ir_instruction
, node
, shader
->ir
) {
293 ir_variable
*const var
= node
->as_variable();
295 if (!var
|| (var
->data
.mode
!= ir_var_uniform
&&
296 var
->data
.mode
!= ir_var_shader_storage
))
300 mem_ctx
= ralloc_context(NULL
);
302 if (var
->data
.explicit_binding
) {
303 const glsl_type
*const type
= var
->type
;
305 if (type
->without_array()->is_sampler() ||
306 type
->without_array()->is_image()) {
307 int binding
= var
->data
.binding
;
308 linker::set_opaque_binding(mem_ctx
, prog
, var
, var
->type
,
309 var
->name
, &binding
);
310 } else if (var
->is_in_buffer_block()) {
311 const glsl_type
*const iface_type
= var
->get_interface_type();
313 /* If the variable is an array and it is an interface instance,
314 * we need to set the binding for each array element. Just
315 * checking that the variable is an array is not sufficient.
316 * The variable could be an array element of a uniform block
317 * that lacks an instance name. For example:
323 * In this case "f" would pass is_in_buffer_block (above) and
324 * type->is_array(), but it will fail is_interface_instance().
326 if (var
->is_interface_instance() && var
->type
->is_array()) {
327 for (unsigned i
= 0; i
< var
->type
->length
; i
++) {
329 ralloc_asprintf(mem_ctx
, "%s[%u]", iface_type
->name
, i
);
331 /* Section 4.4.3 (Uniform Block Layout Qualifiers) of the
332 * GLSL 4.20 spec says:
334 * "If the binding identifier is used with a uniform
335 * block instanced as an array then the first element
336 * of the array takes the specified block binding and
337 * each subsequent element takes the next consecutive
338 * uniform block binding point."
340 linker::set_block_binding(prog
, name
, var
->data
.mode
,
341 var
->data
.binding
+ i
);
344 linker::set_block_binding(prog
, iface_type
->name
,
348 } else if (type
->contains_atomic()) {
349 /* we don't actually need to do anything. */
351 assert(!"Explicit binding not on a sampler, UBO or atomic.");
353 } else if (var
->constant_initializer
) {
354 linker::set_uniform_initializer(mem_ctx
, prog
, var
->name
,
355 var
->type
, var
->constant_initializer
,
361 ralloc_free(mem_ctx
);