9d6977d573a667e00f07653543599520e7fcd1ae
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 "glsl_symbol_table.h"
29 #include "program/hash_table.h"
31 /* These functions are put in a "private" namespace instead of being marked
32 * static so that the unit tests can access them. See
33 * http://code.google.com/p/googletest/wiki/AdvancedGuide#Testing_Private_Code
38 get_storage(gl_uniform_storage
*storage
, unsigned num_storage
,
41 for (unsigned int i
= 0; i
< num_storage
; i
++) {
42 if (strcmp(name
, storage
[i
].name
) == 0)
50 copy_constant_to_storage(union gl_constant_value
*storage
,
51 const ir_constant
*val
,
52 const enum glsl_base_type base_type
,
53 const unsigned int elements
)
55 for (unsigned int i
= 0; i
< elements
; i
++) {
58 storage
[i
].u
= val
->value
.u
[i
];
61 case GLSL_TYPE_SAMPLER
:
62 storage
[i
].i
= val
->value
.i
[i
];
65 storage
[i
].f
= val
->value
.f
[i
];
68 storage
[i
].b
= int(val
->value
.b
[i
]);
71 case GLSL_TYPE_STRUCT
:
73 case GLSL_TYPE_ATOMIC_UINT
:
74 case GLSL_TYPE_INTERFACE
:
77 /* All other types should have already been filtered by other
78 * paths in the caller.
80 assert(!"Should not get here.");
87 set_uniform_binding(void *mem_ctx
, gl_shader_program
*prog
,
88 const char *name
, const glsl_type
*type
, int binding
)
90 struct gl_uniform_storage
*const storage
=
91 get_storage(prog
->UniformStorage
, prog
->NumUserUniformStorage
, name
);
93 if (storage
== NULL
) {
94 assert(storage
!= NULL
);
98 if (storage
->type
->is_sampler()) {
99 unsigned elements
= MAX2(storage
->array_elements
, 1);
101 /* From section 4.4.4 of the GLSL 4.20 specification:
102 * "If the binding identifier is used with an array, the first element
103 * of the array takes the specified unit and each subsequent element
104 * takes the next consecutive unit."
106 for (unsigned int i
= 0; i
< elements
; i
++) {
107 storage
->storage
[i
].i
= binding
+ i
;
110 for (int sh
= 0; sh
< MESA_SHADER_STAGES
; sh
++) {
111 gl_shader
*shader
= prog
->_LinkedShaders
[sh
];
113 if (shader
&& storage
->sampler
[sh
].active
) {
114 for (unsigned i
= 0; i
< elements
; i
++) {
115 unsigned index
= storage
->sampler
[sh
].index
+ i
;
117 shader
->SamplerUnits
[index
] = storage
->storage
[i
].i
;
121 } else if (storage
->block_index
!= -1) {
122 /* This is a field of a UBO. val is the binding index. */
123 for (int i
= 0; i
< MESA_SHADER_STAGES
; i
++) {
124 int stage_index
= prog
->UniformBlockStageIndex
[i
][storage
->block_index
];
126 if (stage_index
!= -1) {
127 struct gl_shader
*sh
= prog
->_LinkedShaders
[i
];
128 sh
->UniformBlocks
[stage_index
].Binding
= binding
;
133 storage
->initialized
= true;
137 set_uniform_initializer(void *mem_ctx
, gl_shader_program
*prog
,
138 const char *name
, const glsl_type
*type
,
141 if (type
->is_record()) {
142 ir_constant
*field_constant
;
144 field_constant
= (ir_constant
*)val
->components
.get_head();
146 for (unsigned int i
= 0; i
< type
->length
; i
++) {
147 const glsl_type
*field_type
= type
->fields
.structure
[i
].type
;
148 const char *field_name
= ralloc_asprintf(mem_ctx
, "%s.%s", name
,
149 type
->fields
.structure
[i
].name
);
150 set_uniform_initializer(mem_ctx
, prog
, field_name
,
151 field_type
, field_constant
);
152 field_constant
= (ir_constant
*)field_constant
->next
;
155 } else if (type
->is_array() && type
->fields
.array
->is_record()) {
156 const glsl_type
*const element_type
= type
->fields
.array
;
158 for (unsigned int i
= 0; i
< type
->length
; i
++) {
159 const char *element_name
= ralloc_asprintf(mem_ctx
, "%s[%d]", name
, i
);
161 set_uniform_initializer(mem_ctx
, prog
, element_name
,
162 element_type
, val
->array_elements
[i
]);
167 struct gl_uniform_storage
*const storage
=
168 get_storage(prog
->UniformStorage
,
169 prog
->NumUserUniformStorage
,
171 if (storage
== NULL
) {
172 assert(storage
!= NULL
);
176 if (val
->type
->is_array()) {
177 const enum glsl_base_type base_type
=
178 val
->array_elements
[0]->type
->base_type
;
179 const unsigned int elements
= val
->array_elements
[0]->type
->components();
180 unsigned int idx
= 0;
182 assert(val
->type
->length
>= storage
->array_elements
);
183 for (unsigned int i
= 0; i
< storage
->array_elements
; i
++) {
184 copy_constant_to_storage(& storage
->storage
[idx
],
185 val
->array_elements
[i
],
192 copy_constant_to_storage(storage
->storage
,
194 val
->type
->base_type
,
195 val
->type
->components());
197 if (storage
->type
->is_sampler()) {
198 for (int sh
= 0; sh
< MESA_SHADER_STAGES
; sh
++) {
199 gl_shader
*shader
= prog
->_LinkedShaders
[sh
];
201 if (shader
&& storage
->sampler
[sh
].active
) {
202 unsigned index
= storage
->sampler
[sh
].index
;
204 shader
->SamplerUnits
[index
] = storage
->storage
[0].i
;
210 storage
->initialized
= true;
215 link_set_uniform_initializers(struct gl_shader_program
*prog
)
217 void *mem_ctx
= NULL
;
219 for (unsigned int i
= 0; i
< MESA_SHADER_STAGES
; i
++) {
220 struct gl_shader
*shader
= prog
->_LinkedShaders
[i
];
225 foreach_list(node
, shader
->ir
) {
226 ir_variable
*const var
= ((ir_instruction
*) node
)->as_variable();
228 if (!var
|| var
->data
.mode
!= ir_var_uniform
)
232 mem_ctx
= ralloc_context(NULL
);
234 if (var
->data
.explicit_binding
) {
235 linker::set_uniform_binding(mem_ctx
, prog
, var
->name
,
236 var
->type
, var
->data
.binding
);
237 } else if (var
->constant_value
) {
238 linker::set_uniform_initializer(mem_ctx
, prog
, var
->name
,
239 var
->type
, var
->constant_value
);
244 ralloc_free(mem_ctx
);