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 "main/context.h"
34 #include "main/mtypes.h"
35 #include "main/errors.h"
36 #include "main/glspirv.h"
37 #include "main/shaderapi.h"
38 #include "main/uniforms.h"
39
40 #include "main/shaderobj.h"
41 #include "st_context.h"
42 #include "st_program.h"
43 #include "st_shader_cache.h"
44
45 #include "compiler/nir/nir.h"
46 #include "compiler/nir/nir_builder.h"
47 #include "compiler/glsl_types.h"
48 #include "compiler/glsl/glsl_to_nir.h"
49 #include "compiler/glsl/gl_nir.h"
50 #include "compiler/glsl/gl_nir_linker.h"
51 #include "compiler/glsl/ir.h"
52 #include "compiler/glsl/ir_optimization.h"
53 #include "compiler/glsl/linker_util.h"
54 #include "compiler/glsl/program.h"
55 #include "compiler/glsl/shader_cache.h"
56 #include "compiler/glsl/string_to_uint_map.h"
57
58 static int
type_size(const struct glsl_type * type)59 type_size(const struct glsl_type *type)
60 {
61 return glsl_count_attribute_slots(type, false);
62 }
63
64 /* Depending on PIPE_CAP_TGSI_TEXCOORD (st->needs_texcoord_semantic) we
65 * may need to fix up varying slots so the glsl->nir path is aligned
66 * with the anything->tgsi->nir path.
67 */
68 static void
st_nir_fixup_varying_slots(struct st_context * st,nir_shader * shader,nir_variable_mode mode)69 st_nir_fixup_varying_slots(struct st_context *st, nir_shader *shader,
70 nir_variable_mode mode)
71 {
72 if (st->needs_texcoord_semantic)
73 return;
74
75 /* This is called from finalize, but we don't want to do this adjustment twice. */
76 assert(!st->allow_st_finalize_nir_twice);
77
78 nir_foreach_variable_with_modes(var, shader, mode) {
79 if (var->data.location >= VARYING_SLOT_VAR0 && var->data.location < VARYING_SLOT_PATCH0) {
80 var->data.location += 9;
81 } else if (var->data.location == VARYING_SLOT_PNTC) {
82 var->data.location = VARYING_SLOT_VAR8;
83 } else if ((var->data.location >= VARYING_SLOT_TEX0) &&
84 (var->data.location <= VARYING_SLOT_TEX7)) {
85 var->data.location += VARYING_SLOT_VAR0 - VARYING_SLOT_TEX0;
86 }
87 }
88 }
89
90 /* input location assignment for VS inputs must be handled specially, so
91 * that it is aligned w/ st's vbo state.
92 * (This isn't the case with, for ex, FS inputs, which only need to agree
93 * on varying-slot w/ the VS outputs)
94 */
95 void
st_nir_assign_vs_in_locations(struct nir_shader * nir)96 st_nir_assign_vs_in_locations(struct nir_shader *nir)
97 {
98 if (nir->info.stage != MESA_SHADER_VERTEX || nir->info.io_lowered)
99 return;
100
101 nir->num_inputs = util_bitcount64(nir->info.inputs_read);
102
103 bool removed_inputs = false;
104
105 nir_foreach_shader_in_variable_safe(var, nir) {
106 /* NIR already assigns dual-slot inputs to two locations so all we have
107 * to do is compact everything down.
108 */
109 if (nir->info.inputs_read & BITFIELD64_BIT(var->data.location)) {
110 var->data.driver_location =
111 util_bitcount64(nir->info.inputs_read &
112 BITFIELD64_MASK(var->data.location));
113 } else {
114 /* Convert unused input variables to shader_temp (with no
115 * initialization), to avoid confusing drivers looking through the
116 * inputs array and expecting to find inputs with a driver_location
117 * set.
118 */
119 var->data.mode = nir_var_shader_temp;
120 removed_inputs = true;
121 }
122 }
123
124 /* Re-lower global vars, to deal with any dead VS inputs. */
125 if (removed_inputs)
126 NIR_PASS(_, nir, nir_lower_global_vars_to_local);
127 }
128
129 static int
st_nir_lookup_parameter_index(struct gl_program * prog,nir_variable * var)130 st_nir_lookup_parameter_index(struct gl_program *prog, nir_variable *var)
131 {
132 struct gl_program_parameter_list *params = prog->Parameters;
133
134 /* Lookup the first parameter that the uniform storage that match the
135 * variable location.
136 */
137 for (unsigned i = 0; i < params->NumParameters; i++) {
138 int index = params->Parameters[i].MainUniformStorageIndex;
139 if (index == var->data.location)
140 return i;
141 }
142
143 /* TODO: Handle this fallback for SPIR-V. We need this for GLSL e.g. in
144 * dEQP-GLES2.functional.uniform_api.random.3
145 */
146
147 /* is there a better way to do this? If we have something like:
148 *
149 * struct S {
150 * float f;
151 * vec4 v;
152 * };
153 * uniform S color;
154 *
155 * Then what we get in prog->Parameters looks like:
156 *
157 * 0: Name=color.f, Type=6, DataType=1406, Size=1
158 * 1: Name=color.v, Type=6, DataType=8b52, Size=4
159 *
160 * So the name doesn't match up and _mesa_lookup_parameter_index()
161 * fails. In this case just find the first matching "color.*"..
162 *
163 * Note for arrays you could end up w/ color[n].f, for example.
164 */
165 if (!prog->sh.data->spirv) {
166 int namelen = strlen(var->name);
167 for (unsigned i = 0; i < params->NumParameters; i++) {
168 struct gl_program_parameter *p = ¶ms->Parameters[i];
169 if ((strncmp(p->Name, var->name, namelen) == 0) &&
170 ((p->Name[namelen] == '.') || (p->Name[namelen] == '['))) {
171 return i;
172 }
173 }
174 }
175
176 return -1;
177 }
178
179 static void
st_nir_assign_uniform_locations(struct gl_context * ctx,struct gl_program * prog,nir_shader * nir)180 st_nir_assign_uniform_locations(struct gl_context *ctx,
181 struct gl_program *prog,
182 nir_shader *nir)
183 {
184 int shaderidx = 0;
185 int imageidx = 0;
186
187 nir_foreach_variable_with_modes(uniform, nir, nir_var_uniform |
188 nir_var_image) {
189 int loc;
190
191 const struct glsl_type *type = glsl_without_array(uniform->type);
192 if (!uniform->data.bindless && (glsl_type_is_sampler(type) || glsl_type_is_image(type))) {
193 if (glsl_type_is_sampler(type)) {
194 loc = shaderidx;
195 shaderidx += type_size(uniform->type);
196 } else {
197 loc = imageidx;
198 imageidx += type_size(uniform->type);
199 }
200 } else if (uniform->state_slots) {
201 const gl_state_index16 *const stateTokens = uniform->state_slots[0].tokens;
202
203 unsigned comps;
204 if (glsl_type_is_struct_or_ifc(type)) {
205 comps = 4;
206 } else {
207 comps = glsl_get_vector_elements(type);
208 }
209
210 if (ctx->Const.PackedDriverUniformStorage) {
211 loc = _mesa_add_sized_state_reference(prog->Parameters,
212 stateTokens, comps, false);
213 loc = prog->Parameters->Parameters[loc].ValueOffset;
214 } else {
215 loc = _mesa_add_state_reference(prog->Parameters, stateTokens);
216 }
217 } else {
218 loc = st_nir_lookup_parameter_index(prog, uniform);
219
220 /* We need to check that loc is not -1 here before accessing the
221 * array. It can be negative for example when we have a struct that
222 * only contains opaque types.
223 */
224 if (loc >= 0 && ctx->Const.PackedDriverUniformStorage) {
225 loc = prog->Parameters->Parameters[loc].ValueOffset;
226 }
227 }
228
229 uniform->data.driver_location = loc;
230 }
231 }
232
233 static bool
def_is_64bit(nir_def * def,void * state)234 def_is_64bit(nir_def *def, void *state)
235 {
236 bool *lower = (bool *)state;
237 if (def && (def->bit_size == 64)) {
238 *lower = true;
239 return false;
240 }
241 return true;
242 }
243
244 static bool
src_is_64bit(nir_src * src,void * state)245 src_is_64bit(nir_src *src, void *state)
246 {
247 bool *lower = (bool *)state;
248 if (src && (nir_src_bit_size(*src) == 64)) {
249 *lower = true;
250 return false;
251 }
252 return true;
253 }
254
255 static bool
filter_64_bit_instr(const nir_instr * const_instr,UNUSED const void * data)256 filter_64_bit_instr(const nir_instr *const_instr, UNUSED const void *data)
257 {
258 bool lower = false;
259 /* lower_alu_to_scalar required nir_instr to be const, but nir_foreach_*
260 * doesn't have const variants, so do the ugly const_cast here. */
261 nir_instr *instr = const_cast<nir_instr *>(const_instr);
262
263 nir_foreach_def(instr, def_is_64bit, &lower);
264 if (lower)
265 return true;
266 nir_foreach_src(instr, src_is_64bit, &lower);
267 return lower;
268 }
269
270 /* Second third of converting glsl_to_nir. This creates uniforms, gathers
271 * info on varyings, etc after NIR link time opts have been applied.
272 */
273 static char *
st_glsl_to_nir_post_opts(struct st_context * st,struct gl_program * prog,struct gl_shader_program * shader_program)274 st_glsl_to_nir_post_opts(struct st_context *st, struct gl_program *prog,
275 struct gl_shader_program *shader_program)
276 {
277 nir_shader *nir = prog->nir;
278 struct pipe_screen *screen = st->screen;
279
280 /* Make a pass over the IR to add state references for any built-in
281 * uniforms that are used. This has to be done now (during linking).
282 * Code generation doesn't happen until the first time this shader is
283 * used for rendering. Waiting until then to generate the parameters is
284 * too late. At that point, the values for the built-in uniforms won't
285 * get sent to the shader.
286 */
287 nir_foreach_uniform_variable(var, nir) {
288 const nir_state_slot *const slots = var->state_slots;
289 if (slots != NULL) {
290 const struct glsl_type *type = glsl_without_array(var->type);
291 for (unsigned int i = 0; i < var->num_state_slots; i++) {
292 unsigned comps;
293 if (glsl_type_is_struct_or_ifc(type)) {
294 comps = _mesa_program_state_value_size(slots[i].tokens);
295 } else {
296 comps = glsl_get_vector_elements(type);
297 }
298
299 if (st->ctx->Const.PackedDriverUniformStorage) {
300 _mesa_add_sized_state_reference(prog->Parameters,
301 slots[i].tokens,
302 comps, false);
303 } else {
304 _mesa_add_state_reference(prog->Parameters,
305 slots[i].tokens);
306 }
307 }
308 }
309 }
310
311 /* Avoid reallocation of the program parameter list, because the uniform
312 * storage is only associated with the original parameter list.
313 * This should be enough for Bitmap and DrawPixels constants.
314 */
315 _mesa_ensure_and_associate_uniform_storage(st->ctx, shader_program, prog, 28);
316
317 /* None of the builtins being lowered here can be produced by SPIR-V. See
318 * _mesa_builtin_uniform_desc. Also drivers that support packed uniform
319 * storage don't need to lower builtins.
320 */
321 if (!shader_program->data->spirv &&
322 !st->ctx->Const.PackedDriverUniformStorage)
323 NIR_PASS(_, nir, st_nir_lower_builtin);
324
325 if (!screen->get_param(screen, PIPE_CAP_NIR_ATOMICS_AS_DEREF))
326 NIR_PASS(_, nir, gl_nir_lower_atomics, shader_program, true);
327
328 NIR_PASS(_, nir, nir_opt_intrinsics);
329 NIR_PASS(_, nir, nir_opt_fragdepth);
330
331 /* Lower 64-bit ops. */
332 if (nir->options->lower_int64_options ||
333 nir->options->lower_doubles_options) {
334 bool lowered_64bit_ops = false;
335 bool revectorize = false;
336
337 if (nir->options->lower_doubles_options) {
338 /* nir_lower_doubles is not prepared for vector ops, so if the backend doesn't
339 * request lower_alu_to_scalar until now, lower all 64 bit ops, and try to
340 * vectorize them afterwards again */
341 if (!nir->options->lower_to_scalar) {
342 NIR_PASS(revectorize, nir, nir_lower_alu_to_scalar, filter_64_bit_instr, nullptr);
343 NIR_PASS(revectorize, nir, nir_lower_phis_to_scalar, false);
344 }
345 /* doubles lowering requires frexp to be lowered first if it will be,
346 * since the pass generates other 64-bit ops. Most backends lower
347 * frexp, and using doubles is rare, and using frexp is even more rare
348 * (no instances in shader-db), so we're not too worried about
349 * accidentally lowering a 32-bit frexp here.
350 */
351 NIR_PASS(lowered_64bit_ops, nir, nir_lower_frexp);
352
353 NIR_PASS(lowered_64bit_ops, nir, nir_lower_doubles,
354 st->ctx->SoftFP64, nir->options->lower_doubles_options);
355 }
356 if (nir->options->lower_int64_options)
357 NIR_PASS(lowered_64bit_ops, nir, nir_lower_int64);
358
359 if (revectorize && !nir->options->vectorize_vec2_16bit)
360 NIR_PASS(_, nir, nir_opt_vectorize, nullptr, nullptr);
361
362 if (revectorize || lowered_64bit_ops)
363 gl_nir_opts(nir);
364 }
365
366 nir_variable_mode mask =
367 nir_var_shader_in | nir_var_shader_out | nir_var_function_temp;
368 nir_remove_dead_variables(nir, mask, NULL);
369
370 if (!st->has_hw_atomics && !screen->get_param(screen, PIPE_CAP_NIR_ATOMICS_AS_DEREF)) {
371 unsigned align_offset_state = 0;
372 if (st->ctx->Const.ShaderStorageBufferOffsetAlignment > 4) {
373 struct gl_program_parameter_list *params = prog->Parameters;
374 for (unsigned i = 0; i < shader_program->data->NumAtomicBuffers; i++) {
375 gl_state_index16 state[STATE_LENGTH] = { STATE_ATOMIC_COUNTER_OFFSET, (short)shader_program->data->AtomicBuffers[i].Binding };
376 _mesa_add_state_reference(params, state);
377 }
378 align_offset_state = STATE_ATOMIC_COUNTER_OFFSET;
379 }
380 NIR_PASS(_, nir, nir_lower_atomics_to_ssbo, align_offset_state);
381 }
382
383 st_set_prog_affected_state_flags(prog);
384
385 st_finalize_nir_before_variants(nir);
386
387 char *msg = NULL;
388 if (st->allow_st_finalize_nir_twice)
389 msg = st_finalize_nir(st, prog, shader_program, nir, true, true);
390
391 if (st->ctx->_Shader->Flags & GLSL_DUMP) {
392 _mesa_log("\n");
393 _mesa_log("NIR IR for linked %s program %d:\n",
394 _mesa_shader_stage_to_string(prog->info.stage),
395 shader_program->Name);
396 nir_print_shader(nir, _mesa_get_log_file());
397 _mesa_log("\n\n");
398 }
399
400 return msg;
401 }
402
403 static void
st_nir_vectorize_io(nir_shader * producer,nir_shader * consumer)404 st_nir_vectorize_io(nir_shader *producer, nir_shader *consumer)
405 {
406 if (consumer)
407 NIR_PASS(_, consumer, nir_lower_io_to_vector, nir_var_shader_in);
408
409 if (!producer)
410 return;
411
412 NIR_PASS(_, producer, nir_lower_io_to_vector, nir_var_shader_out);
413
414 if (producer->info.stage == MESA_SHADER_TESS_CTRL &&
415 producer->options->vectorize_tess_levels)
416 NIR_PASS(_, producer, nir_vectorize_tess_levels);
417
418 NIR_PASS(_, producer, nir_opt_combine_stores, nir_var_shader_out);
419
420 if ((producer)->info.stage != MESA_SHADER_TESS_CTRL) {
421 /* Calling lower_io_to_vector creates output variable writes with
422 * write-masks. We only support these for TCS outputs, so for other
423 * stages, we need to call nir_lower_io_to_temporaries to get rid of
424 * them. This, in turn, creates temporary variables and extra
425 * copy_deref intrinsics that we need to clean up.
426 */
427 NIR_PASS(_, producer, nir_lower_io_to_temporaries,
428 nir_shader_get_entrypoint(producer), true, false);
429 NIR_PASS(_, producer, nir_lower_global_vars_to_local);
430 NIR_PASS(_, producer, nir_split_var_copies);
431 NIR_PASS(_, producer, nir_lower_var_copies);
432 }
433
434 /* Undef scalar store_deref intrinsics are not ignored by nir_lower_io,
435 * so they must be removed before that. These passes remove them.
436 */
437 NIR_PASS(_, producer, nir_lower_vars_to_ssa);
438 NIR_PASS(_, producer, nir_opt_undef);
439 NIR_PASS(_, producer, nir_opt_dce);
440 }
441
442 extern "C" {
443
444 bool
st_nir_lower_wpos_ytransform(struct nir_shader * nir,struct gl_program * prog,struct pipe_screen * pscreen)445 st_nir_lower_wpos_ytransform(struct nir_shader *nir,
446 struct gl_program *prog,
447 struct pipe_screen *pscreen)
448 {
449 bool progress = false;
450
451 if (nir->info.stage != MESA_SHADER_FRAGMENT) {
452 nir_shader_preserve_all_metadata(nir);
453 return progress;
454 }
455
456 static const gl_state_index16 wposTransformState[STATE_LENGTH] = {
457 STATE_FB_WPOS_Y_TRANSFORM
458 };
459 nir_lower_wpos_ytransform_options wpos_options = { { 0 } };
460
461 memcpy(wpos_options.state_tokens, wposTransformState,
462 sizeof(wpos_options.state_tokens));
463 wpos_options.fs_coord_origin_upper_left =
464 pscreen->get_param(pscreen,
465 PIPE_CAP_FS_COORD_ORIGIN_UPPER_LEFT);
466 wpos_options.fs_coord_origin_lower_left =
467 pscreen->get_param(pscreen,
468 PIPE_CAP_FS_COORD_ORIGIN_LOWER_LEFT);
469 wpos_options.fs_coord_pixel_center_integer =
470 pscreen->get_param(pscreen,
471 PIPE_CAP_FS_COORD_PIXEL_CENTER_INTEGER);
472 wpos_options.fs_coord_pixel_center_half_integer =
473 pscreen->get_param(pscreen,
474 PIPE_CAP_FS_COORD_PIXEL_CENTER_HALF_INTEGER);
475
476 if (nir_lower_wpos_ytransform(nir, &wpos_options)) {
477 _mesa_add_state_reference(prog->Parameters, wposTransformState);
478 progress = true;
479 }
480
481 static const gl_state_index16 pntcTransformState[STATE_LENGTH] = {
482 STATE_FB_PNTC_Y_TRANSFORM
483 };
484
485 if (nir_lower_pntc_ytransform(nir, &pntcTransformState)) {
486 _mesa_add_state_reference(prog->Parameters, pntcTransformState);
487 progress = true;
488 }
489
490 return progress;
491 }
492
493 static bool
st_link_glsl_to_nir(struct gl_context * ctx,struct gl_shader_program * shader_program)494 st_link_glsl_to_nir(struct gl_context *ctx,
495 struct gl_shader_program *shader_program)
496 {
497 struct st_context *st = st_context(ctx);
498 struct gl_linked_shader *linked_shader[MESA_SHADER_STAGES];
499 unsigned num_shaders = 0;
500
501 /* Return early if we are loading the shader from on-disk cache */
502 if (st_load_nir_from_disk_cache(ctx, shader_program)) {
503 return GL_TRUE;
504 }
505
506 MESA_TRACE_FUNC();
507
508 assert(shader_program->data->LinkStatus);
509
510 for (unsigned i = 0; i < MESA_SHADER_STAGES; i++) {
511 if (shader_program->_LinkedShaders[i])
512 linked_shader[num_shaders++] = shader_program->_LinkedShaders[i];
513 }
514
515 for (unsigned i = 0; i < num_shaders; i++) {
516 struct gl_linked_shader *shader = linked_shader[i];
517 const nir_shader_compiler_options *options =
518 st->ctx->Const.ShaderCompilerOptions[shader->Stage].NirOptions;
519 struct gl_program *prog = shader->Program;
520
521 _mesa_copy_linked_program_data(shader_program, shader);
522
523 assert(!prog->nir);
524 prog->shader_program = shader_program;
525 prog->state.type = PIPE_SHADER_IR_NIR;
526
527 /* Parameters will be filled during NIR linking. */
528 prog->Parameters = _mesa_new_parameter_list();
529
530 if (shader_program->data->spirv) {
531 prog->nir = _mesa_spirv_to_nir(ctx, shader_program, shader->Stage, options);
532 } else {
533 if (ctx->_Shader->Flags & GLSL_DUMP) {
534 _mesa_log("\n");
535 _mesa_log("GLSL IR for linked %s program %d:\n",
536 _mesa_shader_stage_to_string(shader->Stage),
537 shader_program->Name);
538 _mesa_print_ir(_mesa_get_log_file(), shader->ir, NULL);
539 _mesa_log("\n\n");
540 }
541
542 prog->nir = glsl_to_nir(&st->ctx->Const, shader_program, shader->Stage, options);
543 }
544
545 memcpy(prog->nir->info.source_sha1, shader->linked_source_sha1,
546 SHA1_DIGEST_LENGTH);
547
548 nir_shader_gather_info(prog->nir, nir_shader_get_entrypoint(prog->nir));
549 if (!st->ctx->SoftFP64 && ((prog->nir->info.bit_sizes_int | prog->nir->info.bit_sizes_float) & 64) &&
550 (options->lower_doubles_options & nir_lower_fp64_full_software) != 0) {
551
552 /* It's not possible to use float64 on GLSL ES, so don't bother trying to
553 * build the support code. The support code depends on higher versions of
554 * desktop GLSL, so it will fail to compile (below) anyway.
555 */
556 if (_mesa_is_desktop_gl(st->ctx) && st->ctx->Const.GLSLVersion >= 400)
557 st->ctx->SoftFP64 = glsl_float64_funcs_to_nir(st->ctx, options);
558 }
559 }
560
561 if (shader_program->data->spirv) {
562 static const gl_nir_linker_options opts = {
563 true /*fill_parameters */
564 };
565 if (!gl_nir_link_spirv(&ctx->Const, &ctx->Extensions, shader_program,
566 &opts))
567 return GL_FALSE;
568 } else {
569 if (!gl_nir_link_glsl(&ctx->Const, &ctx->Extensions, ctx->API,
570 shader_program))
571 return GL_FALSE;
572 }
573
574 for (unsigned i = 0; i < num_shaders; i++) {
575 struct gl_program *prog = linked_shader[i]->Program;
576 prog->ExternalSamplersUsed = gl_external_samplers(prog);
577 _mesa_update_shader_textures_used(shader_program, prog);
578 }
579
580 nir_build_program_resource_list(&ctx->Const, shader_program,
581 shader_program->data->spirv);
582
583 for (unsigned i = 0; i < num_shaders; i++) {
584 struct gl_linked_shader *shader = linked_shader[i];
585 nir_shader *nir = shader->Program->nir;
586 gl_shader_stage stage = shader->Stage;
587 const struct gl_shader_compiler_options *options =
588 &ctx->Const.ShaderCompilerOptions[stage];
589
590 /* If there are forms of indirect addressing that the driver
591 * cannot handle, perform the lowering pass.
592 */
593 if (options->EmitNoIndirectInput || options->EmitNoIndirectOutput ||
594 options->EmitNoIndirectTemp || options->EmitNoIndirectUniform) {
595 nir_variable_mode mode = options->EmitNoIndirectInput ?
596 nir_var_shader_in : (nir_variable_mode)0;
597 mode |= options->EmitNoIndirectOutput ?
598 nir_var_shader_out : (nir_variable_mode)0;
599 mode |= options->EmitNoIndirectTemp ?
600 nir_var_function_temp : (nir_variable_mode)0;
601 mode |= options->EmitNoIndirectUniform ?
602 nir_var_uniform | nir_var_mem_ubo | nir_var_mem_ssbo :
603 (nir_variable_mode)0;
604
605 nir_lower_indirect_derefs(nir, mode, UINT32_MAX);
606 }
607
608 /* This needs to run after the initial pass of nir_lower_vars_to_ssa, so
609 * that the buffer indices are constants in nir where they where
610 * constants in GLSL. */
611 NIR_PASS(_, nir, gl_nir_lower_buffers, shader_program);
612
613 /* Remap the locations to slots so those requiring two slots will occupy
614 * two locations. For instance, if we have in the IR code a dvec3 attr0 in
615 * location 0 and vec4 attr1 in location 1, in NIR attr0 will use
616 * locations/slots 0 and 1, and attr1 will use location/slot 2
617 */
618 if (nir->info.stage == MESA_SHADER_VERTEX && !shader_program->data->spirv)
619 nir_remap_dual_slot_attributes(nir, &shader->Program->DualSlotInputs);
620
621 NIR_PASS(_, nir, st_nir_lower_wpos_ytransform, shader->Program,
622 st->screen);
623
624 NIR_PASS(_, nir, nir_lower_system_values);
625 NIR_PASS(_, nir, nir_lower_compute_system_values, NULL);
626
627 if (i >= 1) {
628 struct gl_program *prev_shader = linked_shader[i - 1]->Program;
629
630 /* We can't use nir_compact_varyings with transform feedback, since
631 * the pipe_stream_output->output_register field is based on the
632 * pre-compacted driver_locations.
633 */
634 if (!(prev_shader->sh.LinkedTransformFeedback &&
635 prev_shader->sh.LinkedTransformFeedback->NumVarying > 0))
636 nir_compact_varyings(prev_shader->nir,
637 nir, ctx->API != API_OPENGL_COMPAT);
638
639 if (ctx->Const.ShaderCompilerOptions[shader->Stage].NirOptions->vectorize_io)
640 st_nir_vectorize_io(prev_shader->nir, nir);
641 }
642 }
643
644 /* If the program is a separate shader program check if we need to vectorise
645 * the first and last program interfaces too.
646 */
647 if (shader_program->SeparateShader && num_shaders > 0) {
648 struct gl_linked_shader *first_shader = linked_shader[0];
649 struct gl_linked_shader *last_shader = linked_shader[num_shaders - 1];
650 if (first_shader->Stage != MESA_SHADER_COMPUTE) {
651 if (ctx->Const.ShaderCompilerOptions[first_shader->Stage].NirOptions->vectorize_io &&
652 first_shader->Stage > MESA_SHADER_VERTEX)
653 st_nir_vectorize_io(NULL, first_shader->Program->nir);
654
655 if (ctx->Const.ShaderCompilerOptions[last_shader->Stage].NirOptions->vectorize_io &&
656 last_shader->Stage < MESA_SHADER_FRAGMENT)
657 st_nir_vectorize_io(last_shader->Program->nir, NULL);
658 }
659 }
660
661 struct shader_info *prev_info = NULL;
662
663 for (unsigned i = 0; i < num_shaders; i++) {
664 struct gl_linked_shader *shader = linked_shader[i];
665 struct shader_info *info = &shader->Program->nir->info;
666
667 char *msg = st_glsl_to_nir_post_opts(st, shader->Program, shader_program);
668 if (msg) {
669 linker_error(shader_program, msg);
670 return false;
671 }
672
673 if (prev_info &&
674 ctx->Const.ShaderCompilerOptions[shader->Stage].NirOptions->unify_interfaces) {
675 prev_info->outputs_written |= info->inputs_read &
676 ~(VARYING_BIT_TESS_LEVEL_INNER | VARYING_BIT_TESS_LEVEL_OUTER);
677 info->inputs_read |= prev_info->outputs_written &
678 ~(VARYING_BIT_TESS_LEVEL_INNER | VARYING_BIT_TESS_LEVEL_OUTER);
679
680 prev_info->patch_outputs_written |= info->patch_inputs_read;
681 info->patch_inputs_read |= prev_info->patch_outputs_written;
682 }
683 prev_info = info;
684 }
685
686 for (unsigned i = 0; i < num_shaders; i++) {
687 struct gl_linked_shader *shader = linked_shader[i];
688 struct gl_program *prog = shader->Program;
689
690 /* Make sure that prog->info is in sync with nir->info, but st/mesa
691 * expects some of the values to be from before lowering.
692 */
693 shader_info old_info = prog->info;
694 prog->info = prog->nir->info;
695 prog->info.name = old_info.name;
696 prog->info.label = old_info.label;
697 prog->info.num_ssbos = old_info.num_ssbos;
698 prog->info.num_ubos = old_info.num_ubos;
699 prog->info.num_abos = old_info.num_abos;
700
701 if (prog->info.stage == MESA_SHADER_VERTEX) {
702 /* NIR expands dual-slot inputs out to two locations. We need to
703 * compact things back down GL-style single-slot inputs to avoid
704 * confusing the state tracker.
705 */
706 prog->info.inputs_read =
707 nir_get_single_slot_attribs_mask(prog->nir->info.inputs_read,
708 prog->DualSlotInputs);
709
710 /* Initialize st_vertex_program members. */
711 st_prepare_vertex_program(prog);
712 }
713
714 /* Get pipe_stream_output_info. */
715 if (shader->Stage == MESA_SHADER_VERTEX ||
716 shader->Stage == MESA_SHADER_TESS_EVAL ||
717 shader->Stage == MESA_SHADER_GEOMETRY)
718 st_translate_stream_output_info(prog);
719
720 st_store_nir_in_disk_cache(st, prog);
721
722 st_release_variants(st, prog);
723 st_finalize_program(st, prog);
724 }
725
726 struct pipe_context *pctx = st_context(ctx)->pipe;
727 if (pctx->link_shader) {
728 void *driver_handles[PIPE_SHADER_TYPES];
729 memset(driver_handles, 0, sizeof(driver_handles));
730
731 for (uint32_t i = 0; i < MESA_SHADER_STAGES; ++i) {
732 struct gl_linked_shader *shader = shader_program->_LinkedShaders[i];
733 if (shader) {
734 struct gl_program *p = shader->Program;
735 if (p && p->variants) {
736 enum pipe_shader_type type = pipe_shader_type_from_mesa(shader->Stage);
737 driver_handles[type] = p->variants->driver_shader;
738 }
739 }
740 }
741
742 pctx->link_shader(pctx, driver_handles);
743 }
744
745 return true;
746 }
747
748 void
st_nir_assign_varying_locations(struct st_context * st,nir_shader * nir)749 st_nir_assign_varying_locations(struct st_context *st, nir_shader *nir)
750 {
751 if (nir->info.stage == MESA_SHADER_VERTEX) {
752 nir_assign_io_var_locations(nir, nir_var_shader_out,
753 &nir->num_outputs,
754 nir->info.stage);
755 st_nir_fixup_varying_slots(st, nir, nir_var_shader_out);
756 } else if (nir->info.stage == MESA_SHADER_GEOMETRY ||
757 nir->info.stage == MESA_SHADER_TESS_CTRL ||
758 nir->info.stage == MESA_SHADER_TESS_EVAL) {
759 nir_assign_io_var_locations(nir, nir_var_shader_in,
760 &nir->num_inputs,
761 nir->info.stage);
762 st_nir_fixup_varying_slots(st, nir, nir_var_shader_in);
763
764 nir_assign_io_var_locations(nir, nir_var_shader_out,
765 &nir->num_outputs,
766 nir->info.stage);
767 st_nir_fixup_varying_slots(st, nir, nir_var_shader_out);
768 } else if (nir->info.stage == MESA_SHADER_FRAGMENT) {
769 nir_assign_io_var_locations(nir, nir_var_shader_in,
770 &nir->num_inputs,
771 nir->info.stage);
772 st_nir_fixup_varying_slots(st, nir, nir_var_shader_in);
773 nir_assign_io_var_locations(nir, nir_var_shader_out,
774 &nir->num_outputs,
775 nir->info.stage);
776 } else if (nir->info.stage == MESA_SHADER_COMPUTE) {
777 /* TODO? */
778 } else {
779 unreachable("invalid shader type");
780 }
781 }
782
783 void
st_nir_lower_samplers(struct pipe_screen * screen,nir_shader * nir,struct gl_shader_program * shader_program,struct gl_program * prog)784 st_nir_lower_samplers(struct pipe_screen *screen, nir_shader *nir,
785 struct gl_shader_program *shader_program,
786 struct gl_program *prog)
787 {
788 if (screen->get_param(screen, PIPE_CAP_NIR_SAMPLERS_AS_DEREF))
789 NIR_PASS(_, nir, gl_nir_lower_samplers_as_deref, shader_program);
790 else
791 NIR_PASS(_, nir, gl_nir_lower_samplers, shader_program);
792
793 if (prog) {
794 BITSET_COPY(prog->info.textures_used, nir->info.textures_used);
795 BITSET_COPY(prog->info.textures_used_by_txf, nir->info.textures_used_by_txf);
796 BITSET_COPY(prog->info.samplers_used, nir->info.samplers_used);
797 BITSET_COPY(prog->info.images_used, nir->info.images_used);
798 BITSET_COPY(prog->info.image_buffers, nir->info.image_buffers);
799 BITSET_COPY(prog->info.msaa_images, nir->info.msaa_images);
800 }
801 }
802
803 static int
st_packed_uniforms_type_size(const struct glsl_type * type,bool bindless)804 st_packed_uniforms_type_size(const struct glsl_type *type, bool bindless)
805 {
806 return glsl_count_dword_slots(type, bindless);
807 }
808
809 static int
st_unpacked_uniforms_type_size(const struct glsl_type * type,bool bindless)810 st_unpacked_uniforms_type_size(const struct glsl_type *type, bool bindless)
811 {
812 return glsl_count_vec4_slots(type, false, bindless);
813 }
814
815 void
st_nir_lower_uniforms(struct st_context * st,nir_shader * nir)816 st_nir_lower_uniforms(struct st_context *st, nir_shader *nir)
817 {
818 if (st->ctx->Const.PackedDriverUniformStorage) {
819 NIR_PASS(_, nir, nir_lower_io, nir_var_uniform,
820 st_packed_uniforms_type_size,
821 (nir_lower_io_options)0);
822 } else {
823 NIR_PASS(_, nir, nir_lower_io, nir_var_uniform,
824 st_unpacked_uniforms_type_size,
825 (nir_lower_io_options)0);
826 }
827
828 if (nir->options->lower_uniforms_to_ubo)
829 NIR_PASS(_, nir, nir_lower_uniforms_to_ubo,
830 st->ctx->Const.PackedDriverUniformStorage,
831 !st->ctx->Const.NativeIntegers);
832 }
833
834 /* Last third of preparing nir from glsl, which happens after shader
835 * variant lowering.
836 */
837 char *
st_finalize_nir(struct st_context * st,struct gl_program * prog,struct gl_shader_program * shader_program,nir_shader * nir,bool finalize_by_driver,bool is_before_variants)838 st_finalize_nir(struct st_context *st, struct gl_program *prog,
839 struct gl_shader_program *shader_program,
840 nir_shader *nir, bool finalize_by_driver,
841 bool is_before_variants)
842 {
843 struct pipe_screen *screen = st->screen;
844
845 MESA_TRACE_FUNC();
846
847 NIR_PASS(_, nir, nir_split_var_copies);
848 NIR_PASS(_, nir, nir_lower_var_copies);
849
850 const bool lower_tg4_offsets =
851 !st->screen->get_param(screen, PIPE_CAP_TEXTURE_GATHER_OFFSETS);
852
853 if (st->lower_rect_tex || lower_tg4_offsets) {
854 struct nir_lower_tex_options opts = {0};
855 opts.lower_rect = !!st->lower_rect_tex;
856 opts.lower_tg4_offsets = lower_tg4_offsets;
857
858 NIR_PASS(_, nir, nir_lower_tex, &opts);
859 }
860
861 st_nir_assign_varying_locations(st, nir);
862 st_nir_assign_uniform_locations(st->ctx, prog, nir);
863
864 /* Lower load_deref/store_deref of inputs and outputs.
865 * This depends on st_nir_assign_varying_locations.
866 */
867 if (nir->options->io_options & nir_io_glsl_lower_derefs) {
868 nir_lower_io_passes(nir, false);
869 NIR_PASS(_, nir, nir_remove_dead_variables,
870 nir_var_shader_in | nir_var_shader_out, NULL);
871 }
872
873 /* Set num_uniforms in number of attribute slots (vec4s) */
874 nir->num_uniforms = DIV_ROUND_UP(prog->Parameters->NumParameterValues, 4);
875
876 st_nir_lower_uniforms(st, nir);
877
878 if (is_before_variants && nir->options->lower_uniforms_to_ubo) {
879 /* This must be done after uniforms are lowered to UBO and all
880 * nir_var_uniform variables are removed from NIR to prevent conflicts
881 * between state parameter merging and shader variant generation.
882 */
883 _mesa_optimize_state_parameters(&st->ctx->Const, prog->Parameters);
884 }
885
886 st_nir_lower_samplers(screen, nir, shader_program, prog);
887 if (!screen->get_param(screen, PIPE_CAP_NIR_IMAGES_AS_DEREF))
888 NIR_PASS(_, nir, gl_nir_lower_images, false);
889
890 char *msg = NULL;
891 if (finalize_by_driver && screen->finalize_nir)
892 msg = screen->finalize_nir(screen, nir);
893
894 return msg;
895 }
896
897 /**
898 * Link a GLSL shader program. Called via glLinkProgram().
899 */
900 void
st_link_shader(struct gl_context * ctx,struct gl_shader_program * prog)901 st_link_shader(struct gl_context *ctx, struct gl_shader_program *prog)
902 {
903 unsigned int i;
904 bool spirv = false;
905
906 MESA_TRACE_FUNC();
907
908 _mesa_clear_shader_program_data(ctx, prog);
909
910 prog->data = _mesa_create_shader_program_data();
911
912 prog->data->LinkStatus = LINKING_SUCCESS;
913
914 for (i = 0; i < prog->NumShaders; i++) {
915 if (!prog->Shaders[i]->CompileStatus) {
916 linker_error(prog, "linking with uncompiled/unspecialized shader");
917 }
918
919 if (!i) {
920 spirv = (prog->Shaders[i]->spirv_data != NULL);
921 } else if (spirv && !prog->Shaders[i]->spirv_data) {
922 /* The GL_ARB_gl_spirv spec adds a new bullet point to the list of
923 * reasons LinkProgram can fail:
924 *
925 * "All the shader objects attached to <program> do not have the
926 * same value for the SPIR_V_BINARY_ARB state."
927 */
928 linker_error(prog,
929 "not all attached shaders have the same "
930 "SPIR_V_BINARY_ARB state");
931 }
932 }
933 prog->data->spirv = spirv;
934
935 if (prog->data->LinkStatus) {
936 if (!spirv)
937 link_shaders(ctx, prog);
938 else
939 _mesa_spirv_link_shaders(ctx, prog);
940 }
941
942 /* If LinkStatus is LINKING_SUCCESS, then reset sampler validated to true.
943 * Validation happens via the LinkShader call below. If LinkStatus is
944 * LINKING_SKIPPED, then SamplersValidated will have been restored from the
945 * shader cache.
946 */
947 if (prog->data->LinkStatus == LINKING_SUCCESS) {
948 prog->SamplersValidated = GL_TRUE;
949 }
950
951 if (prog->data->LinkStatus && !st_link_glsl_to_nir(ctx, prog)) {
952 prog->data->LinkStatus = LINKING_FAILURE;
953 }
954
955 if (prog->data->LinkStatus != LINKING_FAILURE)
956 _mesa_create_program_resource_hash(prog);
957
958 /* Return early if we are loading the shader from on-disk cache */
959 if (prog->data->LinkStatus == LINKING_SKIPPED)
960 return;
961
962 if (ctx->_Shader->Flags & GLSL_DUMP) {
963 if (!prog->data->LinkStatus) {
964 fprintf(stderr, "GLSL shader program %d failed to link\n", prog->Name);
965 }
966
967 if (prog->data->InfoLog && prog->data->InfoLog[0] != 0) {
968 fprintf(stderr, "GLSL shader program %d info log:\n", prog->Name);
969 fprintf(stderr, "%s\n", prog->data->InfoLog);
970 }
971 }
972
973 #ifdef ENABLE_SHADER_CACHE
974 if (prog->data->LinkStatus)
975 shader_cache_write_program_metadata(ctx, prog);
976 #endif
977 }
978
979 } /* extern "C" */
980