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