1 /*
2 * Copyright © 2018 Intel Corporation
3 *
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:
10 *
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
13 * Software.
14 *
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 DEALINGS
21 * IN THE SOFTWARE.
22 */
23
24 #include "nir.h"
25 #include "gl_nir_linker.h"
26 #include "main/shader_types.h"
27 #include "main/consts_exts.h"
28
29 struct set_opaque_binding_closure {
30 struct gl_shader_program *shader_prog;
31 struct gl_program *prog;
32 const nir_variable *var;
33 int binding;
34 int location;
35 };
36
37 static void
set_opaque_binding(struct set_opaque_binding_closure * data,const struct glsl_type * type)38 set_opaque_binding(struct set_opaque_binding_closure *data,
39 const struct glsl_type *type)
40 {
41 if (glsl_type_is_array(type) &&
42 glsl_type_is_array(glsl_get_array_element(type))) {
43 const struct glsl_type *element_type = glsl_get_array_element(type);
44
45 for (unsigned int i = 0; i < glsl_get_length(type); i++)
46 set_opaque_binding(data, element_type);
47
48 return;
49 }
50
51 if (data->location < 0 ||
52 data->location >= data->prog->sh.data->NumUniformStorage)
53 return;
54
55 struct gl_uniform_storage *storage =
56 data->prog->sh.data->UniformStorage + data->location++;
57
58 const unsigned elements = MAX2(storage->array_elements, 1);
59
60 for (unsigned int i = 0; i < elements; i++)
61 storage->storage[i].i = data->binding++;
62
63 for (int sh = 0; sh < MESA_SHADER_STAGES; sh++) {
64 struct gl_linked_shader *shader = data->shader_prog->_LinkedShaders[sh];
65
66 if (!shader)
67 continue;
68 if (!storage->opaque[sh].active)
69 continue;
70
71 if (glsl_type_is_sampler(storage->type)) {
72 for (unsigned i = 0; i < elements; i++) {
73 const unsigned index = storage->opaque[sh].index + i;
74
75 if (storage->is_bindless) {
76 if (index >= shader->Program->sh.NumBindlessSamplers)
77 break;
78 shader->Program->sh.BindlessSamplers[index].unit =
79 storage->storage[i].i;
80 shader->Program->sh.BindlessSamplers[index].bound = true;
81 shader->Program->sh.HasBoundBindlessSampler = true;
82 } else {
83 if (index >= ARRAY_SIZE(shader->Program->SamplerUnits))
84 break;
85 shader->Program->SamplerUnits[index] =
86 storage->storage[i].i;
87 }
88 }
89 } else if (glsl_type_is_image(storage->type)) {
90 for (unsigned i = 0; i < elements; i++) {
91 const unsigned index = storage->opaque[sh].index + i;
92
93 if (storage->is_bindless) {
94 if (index >= shader->Program->sh.NumBindlessImages)
95 break;
96 shader->Program->sh.BindlessImages[index].unit =
97 storage->storage[i].i;
98 shader->Program->sh.BindlessImages[index].bound = true;
99 shader->Program->sh.HasBoundBindlessImage = true;
100 } else {
101 if (index >= ARRAY_SIZE(shader->Program->sh.ImageUnits))
102 break;
103 shader->Program->sh.ImageUnits[index] =
104 storage->storage[i].i;
105 }
106 }
107 }
108 }
109 }
110
111 static void
copy_constant_to_storage(union gl_constant_value * storage,const nir_constant * val,const struct glsl_type * type,unsigned int boolean_true)112 copy_constant_to_storage(union gl_constant_value *storage,
113 const nir_constant *val,
114 const struct glsl_type *type,
115 unsigned int boolean_true)
116 {
117 const enum glsl_base_type base_type = glsl_get_base_type(type);
118 const unsigned n_columns = glsl_get_matrix_columns(type);
119 const unsigned n_rows = glsl_get_vector_elements(type);
120 unsigned dmul = glsl_base_type_is_64bit(base_type) ? 2 : 1;
121 int i = 0;
122
123 if (n_columns > 1) {
124 const struct glsl_type *column_type = glsl_get_column_type(type);
125 for (unsigned int column = 0; column < n_columns; column++) {
126 copy_constant_to_storage(&storage[i], val->elements[column],
127 column_type, boolean_true);
128 i += n_rows * dmul;
129 }
130 } else {
131 for (unsigned int row = 0; row < n_rows; row++) {
132 switch (base_type) {
133 case GLSL_TYPE_UINT:
134 storage[i].u = val->values[row].u32;
135 break;
136 case GLSL_TYPE_INT:
137 case GLSL_TYPE_SAMPLER:
138 storage[i].i = val->values[row].i32;
139 break;
140 case GLSL_TYPE_FLOAT:
141 storage[i].f = val->values[row].f32;
142 break;
143 case GLSL_TYPE_DOUBLE:
144 case GLSL_TYPE_UINT64:
145 case GLSL_TYPE_INT64:
146 /* XXX need to check on big-endian */
147 memcpy(&storage[i].u, &val->values[row].f64, sizeof(double));
148 break;
149 case GLSL_TYPE_BOOL:
150 storage[i].b = val->values[row].u32 ? boolean_true : 0;
151 break;
152 case GLSL_TYPE_ARRAY:
153 case GLSL_TYPE_STRUCT:
154 case GLSL_TYPE_TEXTURE:
155 case GLSL_TYPE_IMAGE:
156 case GLSL_TYPE_ATOMIC_UINT:
157 case GLSL_TYPE_INTERFACE:
158 case GLSL_TYPE_VOID:
159 case GLSL_TYPE_SUBROUTINE:
160 case GLSL_TYPE_ERROR:
161 case GLSL_TYPE_UINT16:
162 case GLSL_TYPE_INT16:
163 case GLSL_TYPE_UINT8:
164 case GLSL_TYPE_INT8:
165 case GLSL_TYPE_FLOAT16:
166 /* All other types should have already been filtered by other
167 * paths in the caller.
168 */
169 assert(!"Should not get here.");
170 break;
171 case GLSL_TYPE_COOPERATIVE_MATRIX:
172 unreachable("unsupported base type cooperative matrix");
173 }
174 i += dmul;
175 }
176 }
177 }
178
179 struct set_uniform_initializer_closure {
180 struct gl_shader_program *shader_prog;
181 struct gl_program *prog;
182 const nir_variable *var;
183 int location;
184 unsigned int boolean_true;
185 };
186
187 static void
set_uniform_initializer(struct set_uniform_initializer_closure * data,const struct glsl_type * type,const nir_constant * val)188 set_uniform_initializer(struct set_uniform_initializer_closure *data,
189 const struct glsl_type *type,
190 const nir_constant *val)
191 {
192 const struct glsl_type *t_without_array = glsl_without_array(type);
193
194 if (glsl_type_is_struct_or_ifc(type)) {
195 for (unsigned int i = 0; i < glsl_get_length(type); i++) {
196 const struct glsl_type *field_type = glsl_get_struct_field(type, i);
197 set_uniform_initializer(data, field_type, val->elements[i]);
198 }
199 return;
200 }
201
202 if (glsl_type_is_struct_or_ifc(t_without_array) ||
203 (glsl_type_is_array(type) &&
204 glsl_type_is_array(glsl_get_array_element(type)))) {
205 const struct glsl_type *element_type = glsl_get_array_element(type);
206
207 for (unsigned int i = 0; i < glsl_get_length(type); i++)
208 set_uniform_initializer(data, element_type, val->elements[i]);
209
210 return;
211 }
212
213 if (data->location < 0 ||
214 data->location >= data->prog->sh.data->NumUniformStorage)
215 return;
216
217 struct gl_uniform_storage *storage =
218 data->prog->sh.data->UniformStorage + data->location++;
219
220 if (glsl_type_is_array(type)) {
221 const struct glsl_type *element_type = glsl_get_array_element(type);
222 const enum glsl_base_type base_type = glsl_get_base_type(element_type);
223 const unsigned int elements = glsl_get_components(element_type);
224 unsigned int idx = 0;
225 unsigned dmul = glsl_base_type_is_64bit(base_type) ? 2 : 1;
226
227 assert(glsl_get_length(type) >= storage->array_elements);
228 for (unsigned int i = 0; i < storage->array_elements; i++) {
229 copy_constant_to_storage(&storage->storage[idx],
230 val->elements[i],
231 element_type,
232 data->boolean_true);
233
234 idx += elements * dmul;
235 }
236 } else {
237 copy_constant_to_storage(storage->storage,
238 val,
239 type,
240 data->boolean_true);
241
242 if (glsl_type_is_sampler(storage->type)) {
243 for (int sh = 0; sh < MESA_SHADER_STAGES; sh++) {
244 struct gl_linked_shader *shader =
245 data->shader_prog->_LinkedShaders[sh];
246
247 if (shader && storage->opaque[sh].active) {
248 unsigned index = storage->opaque[sh].index;
249
250 shader->Program->SamplerUnits[index] = storage->storage[0].i;
251 }
252 }
253 }
254 }
255 }
256
257 void
gl_nir_set_uniform_initializers(const struct gl_constants * consts,struct gl_shader_program * prog)258 gl_nir_set_uniform_initializers(const struct gl_constants *consts,
259 struct gl_shader_program *prog)
260 {
261 for (unsigned i = 0; i < MESA_SHADER_STAGES; i++) {
262 struct gl_linked_shader *sh = prog->_LinkedShaders[i];
263 if (!sh)
264 continue;
265
266 nir_shader *nir = sh->Program->nir;
267 assert(nir);
268
269 nir_foreach_gl_uniform_variable(var, nir) {
270 if (var->constant_initializer) {
271 struct set_uniform_initializer_closure data = {
272 .shader_prog = prog,
273 .prog = sh->Program,
274 .var = var,
275 .location = var->data.location,
276 .boolean_true = consts->UniformBooleanTrue
277 };
278 set_uniform_initializer(&data,
279 var->type,
280 var->constant_initializer);
281 } else if (var->data.explicit_binding) {
282
283 if (nir_variable_is_in_block(var)) {
284 /* This case is handled by link_uniform_blocks */
285 continue;
286 }
287
288 const struct glsl_type *without_array =
289 glsl_without_array(var->type);
290
291 if (glsl_type_is_sampler(without_array) ||
292 glsl_type_is_image(without_array)) {
293 struct set_opaque_binding_closure data = {
294 .shader_prog = prog,
295 .prog = sh->Program,
296 .var = var,
297 .binding = var->data.binding,
298 .location = var->data.location
299 };
300 set_opaque_binding(&data, var->type);
301 }
302 }
303 }
304 }
305 memcpy(prog->data->UniformDataDefaults, prog->data->UniformDataSlots,
306 sizeof(union gl_constant_value) * prog->data->NumUniformDataSlots);
307
308 }
309