st/glsl_to_nir: fix the case where NIR clone testing is enabled
[mesa.git] / src / mesa / state_tracker / st_glsl_to_nir.cpp
1 /*
2 * Copyright © 2015 Red Hat
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 FROM,
20 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21 * SOFTWARE.
22 */
23
24 #include "st_nir.h"
25
26 #include "pipe/p_defines.h"
27 #include "pipe/p_screen.h"
28 #include "pipe/p_context.h"
29
30 #include "program/program.h"
31 #include "program/prog_statevars.h"
32 #include "program/prog_parameter.h"
33 #include "program/ir_to_mesa.h"
34 #include "main/mtypes.h"
35 #include "main/errors.h"
36 #include "main/shaderapi.h"
37 #include "main/uniforms.h"
38 #include "util/string_to_uint_map.h"
39
40 #include "st_context.h"
41 #include "st_program.h"
42 #include "st_glsl_types.h"
43
44 #include "compiler/nir/nir.h"
45 #include "compiler/glsl_types.h"
46 #include "compiler/glsl/glsl_to_nir.h"
47 #include "compiler/glsl/ir.h"
48
49
50 /* Depending on PIPE_CAP_TGSI_TEXCOORD (st->needs_texcoord_semantic) we
51 * may need to fix up varying slots so the glsl->nir path is aligned
52 * with the anything->tgsi->nir path.
53 */
54 static void
55 st_nir_fixup_varying_slots(struct st_context *st, struct exec_list *var_list)
56 {
57 if (st->needs_texcoord_semantic)
58 return;
59
60 nir_foreach_variable(var, var_list) {
61 if (var->data.location >= VARYING_SLOT_VAR0) {
62 var->data.location += 9;
63 } else if ((var->data.location >= VARYING_SLOT_TEX0) &&
64 (var->data.location <= VARYING_SLOT_TEX7)) {
65 var->data.location += VARYING_SLOT_VAR0 - VARYING_SLOT_TEX0;
66 }
67 }
68 }
69
70 /* input location assignment for VS inputs must be handled specially, so
71 * that it is aligned w/ st's vbo state.
72 * (This isn't the case with, for ex, FS inputs, which only need to agree
73 * on varying-slot w/ the VS outputs)
74 */
75 static void
76 st_nir_assign_vs_in_locations(struct gl_program *prog, nir_shader *nir)
77 {
78 unsigned attr, num_inputs = 0;
79 unsigned input_to_index[VERT_ATTRIB_MAX] = {0};
80
81 /* TODO de-duplicate w/ similar code in st_translate_vertex_program()? */
82 for (attr = 0; attr < VERT_ATTRIB_MAX; attr++) {
83 if ((prog->info.inputs_read & BITFIELD64_BIT(attr)) != 0) {
84 input_to_index[attr] = num_inputs;
85 num_inputs++;
86 if ((prog->info.double_inputs_read & BITFIELD64_BIT(attr)) != 0) {
87 /* add placeholder for second part of a double attribute */
88 num_inputs++;
89 }
90 } else {
91 input_to_index[attr] = ~0;
92 }
93 }
94
95 /* bit of a hack, mirroring st_translate_vertex_program */
96 input_to_index[VERT_ATTRIB_EDGEFLAG] = num_inputs;
97
98 nir->num_inputs = 0;
99 nir_foreach_variable_safe(var, &nir->inputs) {
100 attr = var->data.location;
101 assert(attr < ARRAY_SIZE(input_to_index));
102
103 if (input_to_index[attr] != ~0u) {
104 var->data.driver_location = input_to_index[attr];
105 nir->num_inputs++;
106 } else {
107 /* Move unused input variables to the globals list (with no
108 * initialization), to avoid confusing drivers looking through the
109 * inputs array and expecting to find inputs with a driver_location
110 * set.
111 */
112 exec_node_remove(&var->node);
113 var->data.mode = nir_var_global;
114 exec_list_push_tail(&nir->globals, &var->node);
115 }
116 }
117 }
118
119 static int
120 st_nir_lookup_parameter_index(const struct gl_program_parameter_list *params,
121 const char *name)
122 {
123 int loc = _mesa_lookup_parameter_index(params, name);
124
125 /* is there a better way to do this? If we have something like:
126 *
127 * struct S {
128 * float f;
129 * vec4 v;
130 * };
131 * uniform S color;
132 *
133 * Then what we get in prog->Parameters looks like:
134 *
135 * 0: Name=color.f, Type=6, DataType=1406, Size=1
136 * 1: Name=color.v, Type=6, DataType=8b52, Size=4
137 *
138 * So the name doesn't match up and _mesa_lookup_parameter_index()
139 * fails. In this case just find the first matching "color.*"..
140 *
141 * Note for arrays you could end up w/ color[n].f, for example.
142 *
143 * glsl_to_tgsi works slightly differently in this regard. It is
144 * emitting something more low level, so it just translates the
145 * params list 1:1 to CONST[] regs. Going from GLSL IR to TGSI,
146 * it just calculates the additional offset of struct field members
147 * in glsl_to_tgsi_visitor::visit(ir_dereference_record *ir) or
148 * glsl_to_tgsi_visitor::visit(ir_dereference_array *ir). It never
149 * needs to work backwards to get base var loc from the param-list
150 * which already has them separated out.
151 */
152 if (loc < 0) {
153 int namelen = strlen(name);
154 for (unsigned i = 0; i < params->NumParameters; i++) {
155 struct gl_program_parameter *p = &params->Parameters[i];
156 if ((strncmp(p->Name, name, namelen) == 0) &&
157 ((p->Name[namelen] == '.') || (p->Name[namelen] == '['))) {
158 loc = i;
159 break;
160 }
161 }
162 }
163
164 return loc;
165 }
166
167 static void
168 st_nir_assign_uniform_locations(struct gl_program *prog,
169 struct gl_shader_program *shader_program,
170 struct exec_list *uniform_list, unsigned *size)
171 {
172 int max = 0;
173 int shaderidx = 0;
174
175 nir_foreach_variable(uniform, uniform_list) {
176 int loc;
177
178 /*
179 * UBO's have their own address spaces, so don't count them towards the
180 * number of global uniforms
181 */
182 if ((uniform->data.mode == nir_var_uniform || uniform->data.mode == nir_var_shader_storage) &&
183 uniform->interface_type != NULL)
184 continue;
185
186 if (uniform->type->is_sampler()) {
187 unsigned val = 0;
188 bool found = shader_program->UniformHash->get(val, uniform->name);
189 loc = shaderidx++;
190 assert(found);
191 (void) found; /* silence unused var warning */
192 /* this ensure that nir_lower_samplers looks at the correct
193 * shader_program->UniformStorage[location]:
194 */
195 uniform->data.location = val;
196 } else if (strncmp(uniform->name, "gl_", 3) == 0) {
197 const gl_state_index *const stateTokens = (gl_state_index *)uniform->state_slots[0].tokens;
198 /* This state reference has already been setup by ir_to_mesa, but we'll
199 * get the same index back here.
200 */
201 loc = _mesa_add_state_reference(prog->Parameters, stateTokens);
202 } else {
203 loc = st_nir_lookup_parameter_index(prog->Parameters, uniform->name);
204 }
205
206 uniform->data.driver_location = loc;
207
208 max = MAX2(max, loc + st_glsl_type_size(uniform->type));
209 }
210 *size = max;
211 }
212
213 extern "C" {
214
215 /* First half of converting glsl_to_nir.. this leaves things in a pre-
216 * nir_lower_io state, so that shader variants can more easily insert/
217 * replace variables, etc.
218 */
219 nir_shader *
220 st_glsl_to_nir(struct st_context *st, struct gl_program *prog,
221 struct gl_shader_program *shader_program,
222 gl_shader_stage stage)
223 {
224 struct pipe_screen *pscreen = st->pipe->screen;
225 enum pipe_shader_type ptarget = st_shader_stage_to_ptarget(stage);
226 const nir_shader_compiler_options *options;
227 nir_shader *nir;
228
229 assert(pscreen->get_compiler_options); /* drivers using NIR must implement this */
230
231 options = (const nir_shader_compiler_options *)
232 pscreen->get_compiler_options(pscreen, PIPE_SHADER_IR_NIR, ptarget);
233 assert(options);
234
235 if (prog->nir)
236 return prog->nir;
237
238 nir = glsl_to_nir(shader_program, stage, options);
239
240 NIR_PASS_V(nir, nir_lower_io_to_temporaries,
241 nir_shader_get_entrypoint(nir),
242 true, true);
243 NIR_PASS_V(nir, nir_lower_global_vars_to_local);
244 NIR_PASS_V(nir, nir_split_var_copies);
245 NIR_PASS_V(nir, nir_lower_var_copies);
246 NIR_PASS_V(nir, st_nir_lower_builtin);
247 NIR_PASS_V(nir, nir_lower_atomics, shader_program);
248
249 /* fragment shaders may need : */
250 if (stage == MESA_SHADER_FRAGMENT) {
251 static const gl_state_index wposTransformState[STATE_LENGTH] = {
252 STATE_INTERNAL, STATE_FB_WPOS_Y_TRANSFORM
253 };
254 nir_lower_wpos_ytransform_options wpos_options = { { 0 } };
255 struct pipe_screen *pscreen = st->pipe->screen;
256
257 memcpy(wpos_options.state_tokens, wposTransformState,
258 sizeof(wpos_options.state_tokens));
259 wpos_options.fs_coord_origin_upper_left =
260 pscreen->get_param(pscreen, PIPE_CAP_TGSI_FS_COORD_ORIGIN_UPPER_LEFT);
261 wpos_options.fs_coord_origin_lower_left =
262 pscreen->get_param(pscreen, PIPE_CAP_TGSI_FS_COORD_ORIGIN_LOWER_LEFT);
263 wpos_options.fs_coord_pixel_center_integer =
264 pscreen->get_param(pscreen, PIPE_CAP_TGSI_FS_COORD_PIXEL_CENTER_INTEGER);
265 wpos_options.fs_coord_pixel_center_half_integer =
266 pscreen->get_param(pscreen, PIPE_CAP_TGSI_FS_COORD_PIXEL_CENTER_HALF_INTEGER);
267
268 if (nir_lower_wpos_ytransform(nir, &wpos_options)) {
269 nir_validate_shader(nir);
270 _mesa_add_state_reference(prog->Parameters, wposTransformState);
271 }
272 }
273
274 if (st->ctx->_Shader->Flags & GLSL_DUMP) {
275 _mesa_log("\n");
276 _mesa_log("NIR IR for linked %s program %d:\n",
277 _mesa_shader_stage_to_string(stage),
278 shader_program->Name);
279 nir_print_shader(nir, _mesa_get_log_file());
280 _mesa_log("\n\n");
281 }
282
283 prog->nir = nir;
284
285 return nir;
286 }
287
288 /* TODO any better helper somewhere to sort a list? */
289
290 static void
291 insert_sorted(struct exec_list *var_list, nir_variable *new_var)
292 {
293 nir_foreach_variable(var, var_list) {
294 if (var->data.location > new_var->data.location) {
295 exec_node_insert_node_before(&var->node, &new_var->node);
296 return;
297 }
298 }
299 exec_list_push_tail(var_list, &new_var->node);
300 }
301
302 static void
303 sort_varyings(struct exec_list *var_list)
304 {
305 struct exec_list new_list;
306 exec_list_make_empty(&new_list);
307 nir_foreach_variable_safe(var, var_list) {
308 exec_node_remove(&var->node);
309 insert_sorted(&new_list, var);
310 }
311 exec_list_move_nodes_to(&new_list, var_list);
312 }
313
314 /* Second half of preparing nir from glsl, which happens after shader
315 * variant lowering.
316 */
317 void
318 st_finalize_nir(struct st_context *st, struct gl_program *prog, nir_shader *nir)
319 {
320 NIR_PASS_V(nir, nir_split_var_copies);
321 NIR_PASS_V(nir, nir_lower_var_copies);
322 NIR_PASS_V(nir, nir_lower_io_types);
323
324 if (nir->stage == MESA_SHADER_VERTEX) {
325 /* Needs special handling so drvloc matches the vbo state: */
326 st_nir_assign_vs_in_locations(prog, nir);
327 /* Re-lower global vars, to deal with any dead VS inputs. */
328 NIR_PASS_V(nir, nir_lower_global_vars_to_local);
329
330 sort_varyings(&nir->outputs);
331 nir_assign_var_locations(&nir->outputs,
332 &nir->num_outputs,
333 st_glsl_type_size);
334 st_nir_fixup_varying_slots(st, &nir->outputs);
335 } else if (nir->stage == MESA_SHADER_FRAGMENT) {
336 sort_varyings(&nir->inputs);
337 nir_assign_var_locations(&nir->inputs,
338 &nir->num_inputs,
339 st_glsl_type_size);
340 st_nir_fixup_varying_slots(st, &nir->inputs);
341 nir_assign_var_locations(&nir->outputs,
342 &nir->num_outputs,
343 st_glsl_type_size);
344 } else if (nir->stage == MESA_SHADER_COMPUTE) {
345 /* TODO? */
346 } else {
347 unreachable("invalid shader type for tgsi bypass\n");
348 }
349
350 struct gl_shader_program *shader_program;
351 switch (nir->stage) {
352 case MESA_SHADER_VERTEX:
353 shader_program = ((struct st_vertex_program *)prog)->shader_program;
354 break;
355 case MESA_SHADER_FRAGMENT:
356 shader_program = ((struct st_fragment_program *)prog)->shader_program;
357 break;
358 case MESA_SHADER_COMPUTE:
359 shader_program = ((struct st_compute_program *)prog)->shader_program;
360 break;
361 default:
362 assert(!"should not be reached");
363 return;
364 }
365
366 NIR_PASS_V(nir, nir_lower_atomics_to_ssbo,
367 st->ctx->Const.Program[nir->stage].MaxAtomicBuffers);
368
369 st_nir_assign_uniform_locations(prog, shader_program,
370 &nir->uniforms, &nir->num_uniforms);
371
372 NIR_PASS_V(nir, nir_lower_system_values);
373 NIR_PASS_V(nir, nir_lower_io, nir_var_all, st_glsl_type_size,
374 (nir_lower_io_options)0);
375 NIR_PASS_V(nir, nir_lower_samplers, shader_program);
376 }
377
378 struct gl_program *
379 st_nir_get_mesa_program(struct gl_context *ctx,
380 struct gl_shader_program *shader_program,
381 struct gl_linked_shader *shader)
382 {
383 struct gl_program *prog;
384
385 validate_ir_tree(shader->ir);
386
387 prog = shader->Program;
388
389 prog->Parameters = _mesa_new_parameter_list();
390
391 do_set_program_inouts(shader->ir, prog, shader->Stage);
392
393 _mesa_copy_linked_program_data(shader_program, shader);
394 _mesa_generate_parameters_list_for_uniforms(shader_program, shader,
395 prog->Parameters);
396
397 /* Make a pass over the IR to add state references for any built-in
398 * uniforms that are used. This has to be done now (during linking).
399 * Code generation doesn't happen until the first time this shader is
400 * used for rendering. Waiting until then to generate the parameters is
401 * too late. At that point, the values for the built-in uniforms won't
402 * get sent to the shader.
403 */
404 foreach_in_list(ir_instruction, node, shader->ir) {
405 ir_variable *var = node->as_variable();
406
407 if ((var == NULL) || (var->data.mode != ir_var_uniform) ||
408 (strncmp(var->name, "gl_", 3) != 0))
409 continue;
410
411 const ir_state_slot *const slots = var->get_state_slots();
412 assert(slots != NULL);
413
414 for (unsigned int i = 0; i < var->get_num_state_slots(); i++) {
415 _mesa_add_state_reference(prog->Parameters,
416 (gl_state_index *) slots[i].tokens);
417 }
418 }
419
420 if (ctx->_Shader->Flags & GLSL_DUMP) {
421 _mesa_log("\n");
422 _mesa_log("GLSL IR for linked %s program %d:\n",
423 _mesa_shader_stage_to_string(shader->Stage),
424 shader_program->Name);
425 _mesa_print_ir(_mesa_get_log_file(), shader->ir, NULL);
426 _mesa_log("\n\n");
427 }
428
429 prog->ShadowSamplers = shader->shadow_samplers;
430 prog->ExternalSamplersUsed = gl_external_samplers(prog);
431 _mesa_update_shader_textures_used(shader_program, prog);
432
433 /* Avoid reallocation of the program parameter list, because the uniform
434 * storage is only associated with the original parameter list.
435 * This should be enough for Bitmap and DrawPixels constants.
436 */
437 _mesa_reserve_parameter_storage(prog->Parameters, 8);
438
439 /* This has to be done last. Any operation the can cause
440 * prog->ParameterValues to get reallocated (e.g., anything that adds a
441 * program constant) has to happen before creating this linkage.
442 */
443 _mesa_associate_uniform_storage(ctx, shader_program, prog, true);
444
445 struct st_vertex_program *stvp;
446 struct st_fragment_program *stfp;
447 struct st_compute_program *stcp;
448
449 switch (shader->Stage) {
450 case MESA_SHADER_VERTEX:
451 stvp = (struct st_vertex_program *)prog;
452 stvp->shader_program = shader_program;
453 break;
454 case MESA_SHADER_FRAGMENT:
455 stfp = (struct st_fragment_program *)prog;
456 stfp->shader_program = shader_program;
457 break;
458 case MESA_SHADER_COMPUTE:
459 stcp = (struct st_compute_program *)prog;
460 stcp->shader_program = shader_program;
461 break;
462 default:
463 assert(!"should not be reached");
464 return NULL;
465 }
466
467 return prog;
468 }
469
470 } /* extern "C" */