97861cc404e7b9a52b66724e7a532369935e594b
[mesa.git] / src / mesa / state_tracker / st_program.c
1 /**************************************************************************
2 *
3 * Copyright 2007 VMware, Inc.
4 * All Rights Reserved.
5 *
6 * Permission is hereby granted, free of charge, to any person obtaining a
7 * copy of this software and associated documentation files (the
8 * "Software"), to deal in the Software without restriction, including
9 * without limitation the rights to use, copy, modify, merge, publish,
10 * distribute, sub license, and/or sell copies of the Software, and to
11 * permit persons to whom the Software is furnished to do so, subject to
12 * the following conditions:
13 *
14 * The above copyright notice and this permission notice (including the
15 * next paragraph) shall be included in all copies or substantial portions
16 * of the Software.
17 *
18 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
19 * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
20 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT.
21 * IN NO EVENT SHALL VMWARE AND/OR ITS SUPPLIERS BE LIABLE FOR
22 * ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
23 * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
24 * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
25 *
26 **************************************************************************/
27 /*
28 * Authors:
29 * Keith Whitwell <keithw@vmware.com>
30 * Brian Paul
31 */
32
33
34 #include "main/errors.h"
35
36 #include "main/hash.h"
37 #include "main/mtypes.h"
38 #include "program/prog_parameter.h"
39 #include "program/prog_print.h"
40 #include "program/prog_to_nir.h"
41 #include "program/programopt.h"
42
43 #include "compiler/nir/nir.h"
44 #include "compiler/nir/nir_serialize.h"
45 #include "draw/draw_context.h"
46
47 #include "pipe/p_context.h"
48 #include "pipe/p_defines.h"
49 #include "pipe/p_shader_tokens.h"
50 #include "draw/draw_context.h"
51 #include "tgsi/tgsi_dump.h"
52 #include "tgsi/tgsi_emulate.h"
53 #include "tgsi/tgsi_parse.h"
54 #include "tgsi/tgsi_ureg.h"
55
56 #include "util/u_memory.h"
57
58 #include "st_debug.h"
59 #include "st_cb_bitmap.h"
60 #include "st_cb_drawpixels.h"
61 #include "st_context.h"
62 #include "st_tgsi_lower_depth_clamp.h"
63 #include "st_tgsi_lower_yuv.h"
64 #include "st_program.h"
65 #include "st_mesa_to_tgsi.h"
66 #include "st_atifs_to_tgsi.h"
67 #include "st_nir.h"
68 #include "st_shader_cache.h"
69 #include "st_util.h"
70 #include "cso_cache/cso_context.h"
71
72
73 static void
74 destroy_program_variants(struct st_context *st, struct gl_program *target);
75
76 static void
77 set_affected_state_flags(uint64_t *states,
78 struct gl_program *prog,
79 uint64_t new_constants,
80 uint64_t new_sampler_views,
81 uint64_t new_samplers,
82 uint64_t new_images,
83 uint64_t new_ubos,
84 uint64_t new_ssbos,
85 uint64_t new_atomics)
86 {
87 if (prog->Parameters->NumParameters)
88 *states |= new_constants;
89
90 if (prog->info.num_textures)
91 *states |= new_sampler_views | new_samplers;
92
93 if (prog->info.num_images)
94 *states |= new_images;
95
96 if (prog->info.num_ubos)
97 *states |= new_ubos;
98
99 if (prog->info.num_ssbos)
100 *states |= new_ssbos;
101
102 if (prog->info.num_abos)
103 *states |= new_atomics;
104 }
105
106 /**
107 * This determines which states will be updated when the shader is bound.
108 */
109 void
110 st_set_prog_affected_state_flags(struct gl_program *prog)
111 {
112 uint64_t *states;
113
114 switch (prog->info.stage) {
115 case MESA_SHADER_VERTEX:
116 states = &((struct st_program*)prog)->affected_states;
117
118 *states = ST_NEW_VS_STATE |
119 ST_NEW_RASTERIZER |
120 ST_NEW_VERTEX_ARRAYS;
121
122 set_affected_state_flags(states, prog,
123 ST_NEW_VS_CONSTANTS,
124 ST_NEW_VS_SAMPLER_VIEWS,
125 ST_NEW_VS_SAMPLERS,
126 ST_NEW_VS_IMAGES,
127 ST_NEW_VS_UBOS,
128 ST_NEW_VS_SSBOS,
129 ST_NEW_VS_ATOMICS);
130 break;
131
132 case MESA_SHADER_TESS_CTRL:
133 states = &(st_program(prog))->affected_states;
134
135 *states = ST_NEW_TCS_STATE;
136
137 set_affected_state_flags(states, prog,
138 ST_NEW_TCS_CONSTANTS,
139 ST_NEW_TCS_SAMPLER_VIEWS,
140 ST_NEW_TCS_SAMPLERS,
141 ST_NEW_TCS_IMAGES,
142 ST_NEW_TCS_UBOS,
143 ST_NEW_TCS_SSBOS,
144 ST_NEW_TCS_ATOMICS);
145 break;
146
147 case MESA_SHADER_TESS_EVAL:
148 states = &(st_program(prog))->affected_states;
149
150 *states = ST_NEW_TES_STATE |
151 ST_NEW_RASTERIZER;
152
153 set_affected_state_flags(states, prog,
154 ST_NEW_TES_CONSTANTS,
155 ST_NEW_TES_SAMPLER_VIEWS,
156 ST_NEW_TES_SAMPLERS,
157 ST_NEW_TES_IMAGES,
158 ST_NEW_TES_UBOS,
159 ST_NEW_TES_SSBOS,
160 ST_NEW_TES_ATOMICS);
161 break;
162
163 case MESA_SHADER_GEOMETRY:
164 states = &(st_program(prog))->affected_states;
165
166 *states = ST_NEW_GS_STATE |
167 ST_NEW_RASTERIZER;
168
169 set_affected_state_flags(states, prog,
170 ST_NEW_GS_CONSTANTS,
171 ST_NEW_GS_SAMPLER_VIEWS,
172 ST_NEW_GS_SAMPLERS,
173 ST_NEW_GS_IMAGES,
174 ST_NEW_GS_UBOS,
175 ST_NEW_GS_SSBOS,
176 ST_NEW_GS_ATOMICS);
177 break;
178
179 case MESA_SHADER_FRAGMENT:
180 states = &((struct st_program*)prog)->affected_states;
181
182 /* gl_FragCoord and glDrawPixels always use constants. */
183 *states = ST_NEW_FS_STATE |
184 ST_NEW_SAMPLE_SHADING |
185 ST_NEW_FS_CONSTANTS;
186
187 set_affected_state_flags(states, prog,
188 ST_NEW_FS_CONSTANTS,
189 ST_NEW_FS_SAMPLER_VIEWS,
190 ST_NEW_FS_SAMPLERS,
191 ST_NEW_FS_IMAGES,
192 ST_NEW_FS_UBOS,
193 ST_NEW_FS_SSBOS,
194 ST_NEW_FS_ATOMICS);
195 break;
196
197 case MESA_SHADER_COMPUTE:
198 states = &((struct st_program*)prog)->affected_states;
199
200 *states = ST_NEW_CS_STATE;
201
202 set_affected_state_flags(states, prog,
203 ST_NEW_CS_CONSTANTS,
204 ST_NEW_CS_SAMPLER_VIEWS,
205 ST_NEW_CS_SAMPLERS,
206 ST_NEW_CS_IMAGES,
207 ST_NEW_CS_UBOS,
208 ST_NEW_CS_SSBOS,
209 ST_NEW_CS_ATOMICS);
210 break;
211
212 default:
213 unreachable("unhandled shader stage");
214 }
215 }
216
217
218 /**
219 * Delete a shader variant. Note the caller must unlink the variant from
220 * the linked list.
221 */
222 static void
223 delete_variant(struct st_context *st, struct st_variant *v, GLenum target)
224 {
225 if (v->driver_shader) {
226 if (target == GL_VERTEX_PROGRAM_ARB &&
227 ((struct st_common_variant*)v)->key.is_draw_shader) {
228 /* Draw shader. */
229 draw_delete_vertex_shader(st->draw, v->driver_shader);
230 } else if (st->has_shareable_shaders || v->st == st) {
231 /* The shader's context matches the calling context, or we
232 * don't care.
233 */
234 switch (target) {
235 case GL_VERTEX_PROGRAM_ARB:
236 st->pipe->delete_vs_state(st->pipe, v->driver_shader);
237 break;
238 case GL_TESS_CONTROL_PROGRAM_NV:
239 st->pipe->delete_tcs_state(st->pipe, v->driver_shader);
240 break;
241 case GL_TESS_EVALUATION_PROGRAM_NV:
242 st->pipe->delete_tes_state(st->pipe, v->driver_shader);
243 break;
244 case GL_GEOMETRY_PROGRAM_NV:
245 st->pipe->delete_gs_state(st->pipe, v->driver_shader);
246 break;
247 case GL_FRAGMENT_PROGRAM_ARB:
248 st->pipe->delete_fs_state(st->pipe, v->driver_shader);
249 break;
250 case GL_COMPUTE_PROGRAM_NV:
251 st->pipe->delete_compute_state(st->pipe, v->driver_shader);
252 break;
253 default:
254 unreachable("bad shader type in delete_basic_variant");
255 }
256 } else {
257 /* We can't delete a shader with a context different from the one
258 * that created it. Add it to the creating context's zombie list.
259 */
260 enum pipe_shader_type type =
261 pipe_shader_type_from_mesa(_mesa_program_enum_to_shader_stage(target));
262
263 st_save_zombie_shader(v->st, type, v->driver_shader);
264 }
265 }
266
267 free(v);
268 }
269
270 static void
271 st_unbind_program(struct st_context *st, struct st_program *p)
272 {
273 /* Unbind the shader in cso_context and re-bind in st/mesa. */
274 switch (p->Base.info.stage) {
275 case MESA_SHADER_VERTEX:
276 cso_set_vertex_shader_handle(st->cso_context, NULL);
277 st->dirty |= ST_NEW_VS_STATE;
278 break;
279 case MESA_SHADER_TESS_CTRL:
280 cso_set_tessctrl_shader_handle(st->cso_context, NULL);
281 st->dirty |= ST_NEW_TCS_STATE;
282 break;
283 case MESA_SHADER_TESS_EVAL:
284 cso_set_tesseval_shader_handle(st->cso_context, NULL);
285 st->dirty |= ST_NEW_TES_STATE;
286 break;
287 case MESA_SHADER_GEOMETRY:
288 cso_set_geometry_shader_handle(st->cso_context, NULL);
289 st->dirty |= ST_NEW_GS_STATE;
290 break;
291 case MESA_SHADER_FRAGMENT:
292 cso_set_fragment_shader_handle(st->cso_context, NULL);
293 st->dirty |= ST_NEW_FS_STATE;
294 break;
295 case MESA_SHADER_COMPUTE:
296 cso_set_compute_shader_handle(st->cso_context, NULL);
297 st->dirty |= ST_NEW_CS_STATE;
298 break;
299 default:
300 unreachable("invalid shader type");
301 }
302 }
303
304 /**
305 * Free all basic program variants.
306 */
307 void
308 st_release_variants(struct st_context *st, struct st_program *p)
309 {
310 struct st_variant *v;
311
312 /* If we are releasing shaders, re-bind them, because we don't
313 * know which shaders are bound in the driver.
314 */
315 if (p->variants)
316 st_unbind_program(st, p);
317
318 for (v = p->variants; v; ) {
319 struct st_variant *next = v->next;
320 delete_variant(st, v, p->Base.Target);
321 v = next;
322 }
323
324 p->variants = NULL;
325
326 if (p->state.tokens) {
327 ureg_free_tokens(p->state.tokens);
328 p->state.tokens = NULL;
329 }
330
331 /* Note: Any setup of ->ir.nir that has had pipe->create_*_state called on
332 * it has resulted in the driver taking ownership of the NIR. Those
333 * callers should be NULLing out the nir field in any pipe_shader_state
334 * that might have this called in order to indicate that.
335 *
336 * GLSL IR and ARB programs will have set gl_program->nir to the same
337 * shader as ir->ir.nir, so it will be freed by _mesa_delete_program().
338 */
339 }
340
341 /**
342 * Free all basic program variants and unref program.
343 */
344 void
345 st_release_program(struct st_context *st, struct st_program **p)
346 {
347 if (!*p)
348 return;
349
350 destroy_program_variants(st, &((*p)->Base));
351 st_reference_prog(st, p, NULL);
352 }
353
354 void
355 st_finalize_nir_before_variants(struct nir_shader *nir)
356 {
357 NIR_PASS_V(nir, nir_opt_access);
358
359 NIR_PASS_V(nir, nir_split_var_copies);
360 NIR_PASS_V(nir, nir_lower_var_copies);
361 if (nir->options->lower_all_io_to_temps ||
362 nir->options->lower_all_io_to_elements ||
363 nir->info.stage == MESA_SHADER_VERTEX ||
364 nir->info.stage == MESA_SHADER_GEOMETRY) {
365 NIR_PASS_V(nir, nir_lower_io_arrays_to_elements_no_indirects, false);
366 } else if (nir->info.stage == MESA_SHADER_FRAGMENT) {
367 NIR_PASS_V(nir, nir_lower_io_arrays_to_elements_no_indirects, true);
368 }
369
370 st_nir_assign_vs_in_locations(nir);
371 }
372
373 /**
374 * Translate ARB (asm) program to NIR
375 */
376 static nir_shader *
377 st_translate_prog_to_nir(struct st_context *st, struct gl_program *prog,
378 gl_shader_stage stage)
379 {
380 struct pipe_screen *screen = st->pipe->screen;
381 const struct gl_shader_compiler_options *options =
382 &st->ctx->Const.ShaderCompilerOptions[stage];
383
384 /* Translate to NIR */
385 nir_shader *nir = prog_to_nir(prog, options->NirOptions);
386 NIR_PASS_V(nir, nir_lower_regs_to_ssa); /* turn registers into SSA */
387 nir_validate_shader(nir, "after st/ptn lower_regs_to_ssa");
388
389 NIR_PASS_V(nir, st_nir_lower_wpos_ytransform, prog, screen);
390 NIR_PASS_V(nir, nir_lower_system_values);
391
392 /* Optimise NIR */
393 NIR_PASS_V(nir, nir_opt_constant_folding);
394 st_nir_opts(nir);
395 st_finalize_nir_before_variants(nir);
396
397 if (st->allow_st_finalize_nir_twice)
398 st_finalize_nir(st, prog, NULL, nir, true);
399
400 nir_validate_shader(nir, "after st/glsl finalize_nir");
401
402 return nir;
403 }
404
405 void
406 st_prepare_vertex_program(struct st_program *stp)
407 {
408 struct st_vertex_program *stvp = (struct st_vertex_program *)stp;
409
410 stvp->num_inputs = 0;
411 memset(stvp->input_to_index, ~0, sizeof(stvp->input_to_index));
412 memset(stvp->result_to_output, ~0, sizeof(stvp->result_to_output));
413
414 /* Determine number of inputs, the mappings between VERT_ATTRIB_x
415 * and TGSI generic input indexes, plus input attrib semantic info.
416 */
417 for (unsigned attr = 0; attr < VERT_ATTRIB_MAX; attr++) {
418 if ((stp->Base.info.inputs_read & BITFIELD64_BIT(attr)) != 0) {
419 stvp->input_to_index[attr] = stvp->num_inputs;
420 stvp->index_to_input[stvp->num_inputs] = attr;
421 stvp->num_inputs++;
422
423 if ((stp->Base.DualSlotInputs & BITFIELD64_BIT(attr)) != 0) {
424 /* add placeholder for second part of a double attribute */
425 stvp->index_to_input[stvp->num_inputs] = ST_DOUBLE_ATTRIB_PLACEHOLDER;
426 stvp->num_inputs++;
427 }
428 }
429 }
430 /* pre-setup potentially unused edgeflag input */
431 stvp->input_to_index[VERT_ATTRIB_EDGEFLAG] = stvp->num_inputs;
432 stvp->index_to_input[stvp->num_inputs] = VERT_ATTRIB_EDGEFLAG;
433
434 /* Compute mapping of vertex program outputs to slots. */
435 unsigned num_outputs = 0;
436 for (unsigned attr = 0; attr < VARYING_SLOT_MAX; attr++) {
437 if (stp->Base.info.outputs_written & BITFIELD64_BIT(attr))
438 stvp->result_to_output[attr] = num_outputs++;
439 }
440 /* pre-setup potentially unused edgeflag output */
441 stvp->result_to_output[VARYING_SLOT_EDGE] = num_outputs;
442 }
443
444 void
445 st_translate_stream_output_info(struct gl_program *prog)
446 {
447 struct gl_transform_feedback_info *info = prog->sh.LinkedTransformFeedback;
448 if (!info)
449 return;
450
451 /* Determine the (default) output register mapping for each output. */
452 unsigned num_outputs = 0;
453 ubyte output_mapping[VARYING_SLOT_TESS_MAX];
454 memset(output_mapping, 0, sizeof(output_mapping));
455
456 for (unsigned attr = 0; attr < VARYING_SLOT_MAX; attr++) {
457 if (prog->info.outputs_written & BITFIELD64_BIT(attr))
458 output_mapping[attr] = num_outputs++;
459 }
460
461 /* Translate stream output info. */
462 struct pipe_stream_output_info *so_info =
463 &((struct st_program*)prog)->state.stream_output;
464
465 for (unsigned i = 0; i < info->NumOutputs; i++) {
466 so_info->output[i].register_index =
467 output_mapping[info->Outputs[i].OutputRegister];
468 so_info->output[i].start_component = info->Outputs[i].ComponentOffset;
469 so_info->output[i].num_components = info->Outputs[i].NumComponents;
470 so_info->output[i].output_buffer = info->Outputs[i].OutputBuffer;
471 so_info->output[i].dst_offset = info->Outputs[i].DstOffset;
472 so_info->output[i].stream = info->Outputs[i].StreamId;
473 }
474
475 for (unsigned i = 0; i < PIPE_MAX_SO_BUFFERS; i++) {
476 so_info->stride[i] = info->Buffers[i].Stride;
477 }
478 so_info->num_outputs = info->NumOutputs;
479 }
480
481 /**
482 * Translate a vertex program.
483 */
484 bool
485 st_translate_vertex_program(struct st_context *st,
486 struct st_program *stp)
487 {
488 struct ureg_program *ureg;
489 enum pipe_error error;
490 unsigned num_outputs = 0;
491 unsigned attr;
492 ubyte output_semantic_name[VARYING_SLOT_MAX] = {0};
493 ubyte output_semantic_index[VARYING_SLOT_MAX] = {0};
494
495 if (stp->Base.arb.IsPositionInvariant)
496 _mesa_insert_mvp_code(st->ctx, &stp->Base);
497
498 /* ARB_vp: */
499 if (!stp->glsl_to_tgsi) {
500 _mesa_remove_output_reads(&stp->Base, PROGRAM_OUTPUT);
501
502 /* This determines which states will be updated when the assembly
503 * shader is bound.
504 */
505 stp->affected_states = ST_NEW_VS_STATE |
506 ST_NEW_RASTERIZER |
507 ST_NEW_VERTEX_ARRAYS;
508
509 if (stp->Base.Parameters->NumParameters)
510 stp->affected_states |= ST_NEW_VS_CONSTANTS;
511
512 /* Translate to NIR if preferred. */
513 if (st->pipe->screen->get_shader_param(st->pipe->screen,
514 PIPE_SHADER_VERTEX,
515 PIPE_SHADER_CAP_PREFERRED_IR)) {
516 assert(!stp->glsl_to_tgsi);
517
518 if (stp->Base.nir)
519 ralloc_free(stp->Base.nir);
520
521 if (stp->serialized_nir) {
522 free(stp->serialized_nir);
523 stp->serialized_nir = NULL;
524 }
525
526 stp->state.type = PIPE_SHADER_IR_NIR;
527 stp->Base.nir = st_translate_prog_to_nir(st, &stp->Base,
528 MESA_SHADER_VERTEX);
529
530 /* We must update stp->Base.info after translation and before
531 * st_prepare_vertex_program is called, because inputs_read
532 * may become outdated after NIR optimization passes.
533 *
534 * For ffvp/ARB_vp inputs_read is populated based
535 * on declared attributes without taking their usage into
536 * consideration. When creating shader variants we expect
537 * that their inputs_read would match the base ones for
538 * input mapping to work properly.
539 */
540 nir_shader_gather_info(stp->Base.nir,
541 nir_shader_get_entrypoint(stp->Base.nir));
542 st_nir_assign_vs_in_locations(stp->Base.nir);
543 stp->Base.info = stp->Base.nir->info;
544
545 /* For st_draw_feedback, we need to generate TGSI too if draw doesn't
546 * use LLVM.
547 */
548 if (draw_has_llvm()) {
549 st_prepare_vertex_program(stp);
550 return true;
551 }
552 }
553 }
554
555 st_prepare_vertex_program(stp);
556
557 /* Get semantic names and indices. */
558 for (attr = 0; attr < VARYING_SLOT_MAX; attr++) {
559 if (stp->Base.info.outputs_written & BITFIELD64_BIT(attr)) {
560 unsigned slot = num_outputs++;
561 unsigned semantic_name, semantic_index;
562 tgsi_get_gl_varying_semantic(attr, st->needs_texcoord_semantic,
563 &semantic_name, &semantic_index);
564 output_semantic_name[slot] = semantic_name;
565 output_semantic_index[slot] = semantic_index;
566 }
567 }
568 /* pre-setup potentially unused edgeflag output */
569 output_semantic_name[num_outputs] = TGSI_SEMANTIC_EDGEFLAG;
570 output_semantic_index[num_outputs] = 0;
571
572 ureg = ureg_create_with_screen(PIPE_SHADER_VERTEX, st->pipe->screen);
573 if (ureg == NULL)
574 return false;
575
576 if (stp->Base.info.clip_distance_array_size)
577 ureg_property(ureg, TGSI_PROPERTY_NUM_CLIPDIST_ENABLED,
578 stp->Base.info.clip_distance_array_size);
579 if (stp->Base.info.cull_distance_array_size)
580 ureg_property(ureg, TGSI_PROPERTY_NUM_CULLDIST_ENABLED,
581 stp->Base.info.cull_distance_array_size);
582
583 if (ST_DEBUG & DEBUG_MESA) {
584 _mesa_print_program(&stp->Base);
585 _mesa_print_program_parameters(st->ctx, &stp->Base);
586 debug_printf("\n");
587 }
588
589 struct st_vertex_program *stvp = (struct st_vertex_program *)stp;
590
591 if (stp->glsl_to_tgsi) {
592 error = st_translate_program(st->ctx,
593 PIPE_SHADER_VERTEX,
594 ureg,
595 stp->glsl_to_tgsi,
596 &stp->Base,
597 /* inputs */
598 stvp->num_inputs,
599 stvp->input_to_index,
600 NULL, /* inputSlotToAttr */
601 NULL, /* input semantic name */
602 NULL, /* input semantic index */
603 NULL, /* interp mode */
604 /* outputs */
605 num_outputs,
606 stvp->result_to_output,
607 output_semantic_name,
608 output_semantic_index);
609
610 st_translate_stream_output_info(&stp->Base);
611
612 free_glsl_to_tgsi_visitor(stp->glsl_to_tgsi);
613 } else
614 error = st_translate_mesa_program(st->ctx,
615 PIPE_SHADER_VERTEX,
616 ureg,
617 &stp->Base,
618 /* inputs */
619 stvp->num_inputs,
620 stvp->input_to_index,
621 NULL, /* input semantic name */
622 NULL, /* input semantic index */
623 NULL,
624 /* outputs */
625 num_outputs,
626 stvp->result_to_output,
627 output_semantic_name,
628 output_semantic_index);
629
630 if (error) {
631 debug_printf("%s: failed to translate Mesa program:\n", __func__);
632 _mesa_print_program(&stp->Base);
633 debug_assert(0);
634 return false;
635 }
636
637 stp->state.tokens = ureg_get_tokens(ureg, NULL);
638 ureg_destroy(ureg);
639
640 if (stp->glsl_to_tgsi) {
641 stp->glsl_to_tgsi = NULL;
642 st_store_ir_in_disk_cache(st, &stp->Base, false);
643 }
644
645 return stp->state.tokens != NULL;
646 }
647
648 static struct nir_shader *
649 get_nir_shader(struct st_context *st, struct st_program *stp)
650 {
651 if (stp->Base.nir) {
652 nir_shader *nir = stp->Base.nir;
653
654 /* The first shader variant takes ownership of NIR, so that there is
655 * no cloning. Additional shader variants are always generated from
656 * serialized NIR to save memory.
657 */
658 stp->Base.nir = NULL;
659 assert(stp->serialized_nir && stp->serialized_nir_size);
660 return nir;
661 }
662
663 struct blob_reader blob_reader;
664 const struct nir_shader_compiler_options *options =
665 st->ctx->Const.ShaderCompilerOptions[stp->Base.info.stage].NirOptions;
666
667 blob_reader_init(&blob_reader, stp->serialized_nir, stp->serialized_nir_size);
668 return nir_deserialize(NULL, options, &blob_reader);
669 }
670
671 static const gl_state_index16 depth_range_state[STATE_LENGTH] =
672 { STATE_DEPTH_RANGE };
673
674 static struct st_common_variant *
675 st_create_vp_variant(struct st_context *st,
676 struct st_program *stvp,
677 const struct st_common_variant_key *key)
678 {
679 struct st_common_variant *vpv = CALLOC_STRUCT(st_common_variant);
680 struct pipe_context *pipe = st->pipe;
681 struct pipe_screen *screen = pipe->screen;
682 struct pipe_shader_state state = {0};
683
684 static const gl_state_index16 point_size_state[STATE_LENGTH] =
685 { STATE_INTERNAL, STATE_POINT_SIZE_CLAMPED, 0 };
686 struct gl_program_parameter_list *params = stvp->Base.Parameters;
687
688 vpv->key = *key;
689
690 state.stream_output = stvp->state.stream_output;
691
692 if (stvp->state.type == PIPE_SHADER_IR_NIR &&
693 (!key->is_draw_shader || draw_has_llvm())) {
694 bool finalize = false;
695
696 state.type = PIPE_SHADER_IR_NIR;
697 state.ir.nir = get_nir_shader(st, stvp);
698 if (key->clamp_color) {
699 NIR_PASS_V(state.ir.nir, nir_lower_clamp_color_outputs);
700 finalize = true;
701 }
702 if (key->passthrough_edgeflags) {
703 NIR_PASS_V(state.ir.nir, nir_lower_passthrough_edgeflags);
704 finalize = true;
705 }
706
707 if (key->lower_point_size) {
708 _mesa_add_state_reference(params, point_size_state);
709 NIR_PASS_V(state.ir.nir, nir_lower_point_size_mov,
710 point_size_state);
711 finalize = true;
712 }
713
714 if (key->lower_ucp) {
715 bool can_compact = screen->get_param(screen,
716 PIPE_CAP_NIR_COMPACT_ARRAYS);
717
718 bool use_eye = st->ctx->_Shader->CurrentProgram[MESA_SHADER_VERTEX] != NULL;
719 struct nir_shader *nir = state.ir.nir;
720
721 if (nir->info.outputs_written & VARYING_BIT_CLIP_DIST0)
722 NIR_PASS_V(state.ir.nir, nir_lower_clip_disable, key->lower_ucp);
723 else {
724 gl_state_index16 clipplane_state[MAX_CLIP_PLANES][STATE_LENGTH];
725 for (int i = 0; i < MAX_CLIP_PLANES; ++i) {
726 if (use_eye) {
727 clipplane_state[i][0] = STATE_CLIPPLANE;
728 clipplane_state[i][1] = i;
729 } else {
730 clipplane_state[i][0] = STATE_INTERNAL;
731 clipplane_state[i][1] = STATE_CLIP_INTERNAL;
732 clipplane_state[i][2] = i;
733 }
734 _mesa_add_state_reference(params, clipplane_state[i]);
735 }
736
737 NIR_PASS_V(state.ir.nir, nir_lower_clip_vs, key->lower_ucp,
738 true, can_compact, clipplane_state);
739 NIR_PASS_V(state.ir.nir, nir_lower_io_to_temporaries,
740 nir_shader_get_entrypoint(state.ir.nir), true, false);
741 NIR_PASS_V(state.ir.nir, nir_lower_global_vars_to_local);
742 }
743 finalize = true;
744 }
745
746 if (finalize || !st->allow_st_finalize_nir_twice) {
747 st_finalize_nir(st, &stvp->Base, stvp->shader_program, state.ir.nir,
748 true);
749
750 /* Some of the lowering above may have introduced new varyings */
751 nir_shader_gather_info(state.ir.nir,
752 nir_shader_get_entrypoint(state.ir.nir));
753 }
754
755 if (ST_DEBUG & DEBUG_PRINT_IR)
756 nir_print_shader(state.ir.nir, stderr);
757
758 if (key->is_draw_shader)
759 vpv->base.driver_shader = draw_create_vertex_shader(st->draw, &state);
760 else
761 vpv->base.driver_shader = pipe->create_vs_state(pipe, &state);
762
763 return vpv;
764 }
765
766 state.type = PIPE_SHADER_IR_TGSI;
767 state.tokens = tgsi_dup_tokens(stvp->state.tokens);
768
769 /* Emulate features. */
770 if (key->clamp_color || key->passthrough_edgeflags) {
771 const struct tgsi_token *tokens;
772 unsigned flags =
773 (key->clamp_color ? TGSI_EMU_CLAMP_COLOR_OUTPUTS : 0) |
774 (key->passthrough_edgeflags ? TGSI_EMU_PASSTHROUGH_EDGEFLAG : 0);
775
776 tokens = tgsi_emulate(state.tokens, flags);
777
778 if (tokens) {
779 tgsi_free_tokens(state.tokens);
780 state.tokens = tokens;
781 } else {
782 fprintf(stderr, "mesa: cannot emulate deprecated features\n");
783 }
784 }
785
786 if (key->lower_depth_clamp) {
787 unsigned depth_range_const =
788 _mesa_add_state_reference(params, depth_range_state);
789
790 const struct tgsi_token *tokens;
791 tokens = st_tgsi_lower_depth_clamp(state.tokens, depth_range_const,
792 key->clip_negative_one_to_one);
793 if (tokens != state.tokens)
794 tgsi_free_tokens(state.tokens);
795 state.tokens = tokens;
796 }
797
798 if (ST_DEBUG & DEBUG_PRINT_IR)
799 tgsi_dump(state.tokens, 0);
800
801 if (key->is_draw_shader)
802 vpv->base.driver_shader = draw_create_vertex_shader(st->draw, &state);
803 else
804 vpv->base.driver_shader = pipe->create_vs_state(pipe, &state);
805
806 if (state.tokens) {
807 tgsi_free_tokens(state.tokens);
808 }
809
810 return vpv;
811 }
812
813
814 /**
815 * Find/create a vertex program variant.
816 */
817 struct st_common_variant *
818 st_get_vp_variant(struct st_context *st,
819 struct st_program *stp,
820 const struct st_common_variant_key *key)
821 {
822 struct st_vertex_program *stvp = (struct st_vertex_program *)stp;
823 struct st_common_variant *vpv;
824
825 /* Search for existing variant */
826 for (vpv = st_common_variant(stp->variants); vpv;
827 vpv = st_common_variant(vpv->base.next)) {
828 if (memcmp(&vpv->key, key, sizeof(*key)) == 0) {
829 break;
830 }
831 }
832
833 if (!vpv) {
834 /* create now */
835 vpv = st_create_vp_variant(st, stp, key);
836 if (vpv) {
837 vpv->base.st = key->st;
838
839 unsigned num_inputs = stvp->num_inputs + key->passthrough_edgeflags;
840 for (unsigned index = 0; index < num_inputs; ++index) {
841 unsigned attr = stvp->index_to_input[index];
842 if (attr == ST_DOUBLE_ATTRIB_PLACEHOLDER)
843 continue;
844 vpv->vert_attrib_mask |= 1u << attr;
845 }
846
847 /* insert into list */
848 vpv->base.next = stp->variants;
849 stp->variants = &vpv->base;
850 }
851 }
852
853 return vpv;
854 }
855
856
857 /**
858 * Translate a Mesa fragment shader into a TGSI shader.
859 */
860 bool
861 st_translate_fragment_program(struct st_context *st,
862 struct st_program *stfp)
863 {
864 /* Non-GLSL programs: */
865 if (!stfp->glsl_to_tgsi) {
866 _mesa_remove_output_reads(&stfp->Base, PROGRAM_OUTPUT);
867 if (st->ctx->Const.GLSLFragCoordIsSysVal)
868 _mesa_program_fragment_position_to_sysval(&stfp->Base);
869
870 /* This determines which states will be updated when the assembly
871 * shader is bound.
872 *
873 * fragment.position and glDrawPixels always use constants.
874 */
875 stfp->affected_states = ST_NEW_FS_STATE |
876 ST_NEW_SAMPLE_SHADING |
877 ST_NEW_FS_CONSTANTS;
878
879 if (stfp->ati_fs) {
880 /* Just set them for ATI_fs unconditionally. */
881 stfp->affected_states |= ST_NEW_FS_SAMPLER_VIEWS |
882 ST_NEW_FS_SAMPLERS;
883 } else {
884 /* ARB_fp */
885 if (stfp->Base.SamplersUsed)
886 stfp->affected_states |= ST_NEW_FS_SAMPLER_VIEWS |
887 ST_NEW_FS_SAMPLERS;
888 }
889
890 /* Translate to NIR. */
891 if (!stfp->ati_fs &&
892 st->pipe->screen->get_shader_param(st->pipe->screen,
893 PIPE_SHADER_FRAGMENT,
894 PIPE_SHADER_CAP_PREFERRED_IR)) {
895 nir_shader *nir =
896 st_translate_prog_to_nir(st, &stfp->Base, MESA_SHADER_FRAGMENT);
897
898 if (stfp->Base.nir)
899 ralloc_free(stfp->Base.nir);
900 if (stfp->serialized_nir) {
901 free(stfp->serialized_nir);
902 stfp->serialized_nir = NULL;
903 }
904 stfp->state.type = PIPE_SHADER_IR_NIR;
905 stfp->Base.nir = nir;
906 return true;
907 }
908 }
909
910 ubyte outputMapping[2 * FRAG_RESULT_MAX];
911 ubyte inputMapping[VARYING_SLOT_MAX];
912 ubyte inputSlotToAttr[VARYING_SLOT_MAX];
913 ubyte interpMode[PIPE_MAX_SHADER_INPUTS]; /* XXX size? */
914 GLuint attr;
915 GLbitfield64 inputsRead;
916 struct ureg_program *ureg;
917
918 GLboolean write_all = GL_FALSE;
919
920 ubyte input_semantic_name[PIPE_MAX_SHADER_INPUTS];
921 ubyte input_semantic_index[PIPE_MAX_SHADER_INPUTS];
922 uint fs_num_inputs = 0;
923
924 ubyte fs_output_semantic_name[PIPE_MAX_SHADER_OUTPUTS];
925 ubyte fs_output_semantic_index[PIPE_MAX_SHADER_OUTPUTS];
926 uint fs_num_outputs = 0;
927
928 memset(inputSlotToAttr, ~0, sizeof(inputSlotToAttr));
929
930 /*
931 * Convert Mesa program inputs to TGSI input register semantics.
932 */
933 inputsRead = stfp->Base.info.inputs_read;
934 for (attr = 0; attr < VARYING_SLOT_MAX; attr++) {
935 if ((inputsRead & BITFIELD64_BIT(attr)) != 0) {
936 const GLuint slot = fs_num_inputs++;
937
938 inputMapping[attr] = slot;
939 inputSlotToAttr[slot] = attr;
940
941 switch (attr) {
942 case VARYING_SLOT_POS:
943 input_semantic_name[slot] = TGSI_SEMANTIC_POSITION;
944 input_semantic_index[slot] = 0;
945 interpMode[slot] = TGSI_INTERPOLATE_LINEAR;
946 break;
947 case VARYING_SLOT_COL0:
948 input_semantic_name[slot] = TGSI_SEMANTIC_COLOR;
949 input_semantic_index[slot] = 0;
950 interpMode[slot] = stfp->glsl_to_tgsi ?
951 TGSI_INTERPOLATE_COUNT : TGSI_INTERPOLATE_COLOR;
952 break;
953 case VARYING_SLOT_COL1:
954 input_semantic_name[slot] = TGSI_SEMANTIC_COLOR;
955 input_semantic_index[slot] = 1;
956 interpMode[slot] = stfp->glsl_to_tgsi ?
957 TGSI_INTERPOLATE_COUNT : TGSI_INTERPOLATE_COLOR;
958 break;
959 case VARYING_SLOT_FOGC:
960 input_semantic_name[slot] = TGSI_SEMANTIC_FOG;
961 input_semantic_index[slot] = 0;
962 interpMode[slot] = TGSI_INTERPOLATE_PERSPECTIVE;
963 break;
964 case VARYING_SLOT_FACE:
965 input_semantic_name[slot] = TGSI_SEMANTIC_FACE;
966 input_semantic_index[slot] = 0;
967 interpMode[slot] = TGSI_INTERPOLATE_CONSTANT;
968 break;
969 case VARYING_SLOT_PRIMITIVE_ID:
970 input_semantic_name[slot] = TGSI_SEMANTIC_PRIMID;
971 input_semantic_index[slot] = 0;
972 interpMode[slot] = TGSI_INTERPOLATE_CONSTANT;
973 break;
974 case VARYING_SLOT_LAYER:
975 input_semantic_name[slot] = TGSI_SEMANTIC_LAYER;
976 input_semantic_index[slot] = 0;
977 interpMode[slot] = TGSI_INTERPOLATE_CONSTANT;
978 break;
979 case VARYING_SLOT_VIEWPORT:
980 input_semantic_name[slot] = TGSI_SEMANTIC_VIEWPORT_INDEX;
981 input_semantic_index[slot] = 0;
982 interpMode[slot] = TGSI_INTERPOLATE_CONSTANT;
983 break;
984 case VARYING_SLOT_CLIP_DIST0:
985 input_semantic_name[slot] = TGSI_SEMANTIC_CLIPDIST;
986 input_semantic_index[slot] = 0;
987 interpMode[slot] = TGSI_INTERPOLATE_PERSPECTIVE;
988 break;
989 case VARYING_SLOT_CLIP_DIST1:
990 input_semantic_name[slot] = TGSI_SEMANTIC_CLIPDIST;
991 input_semantic_index[slot] = 1;
992 interpMode[slot] = TGSI_INTERPOLATE_PERSPECTIVE;
993 break;
994 case VARYING_SLOT_CULL_DIST0:
995 case VARYING_SLOT_CULL_DIST1:
996 /* these should have been lowered by GLSL */
997 assert(0);
998 break;
999 /* In most cases, there is nothing special about these
1000 * inputs, so adopt a convention to use the generic
1001 * semantic name and the mesa VARYING_SLOT_ number as the
1002 * index.
1003 *
1004 * All that is required is that the vertex shader labels
1005 * its own outputs similarly, and that the vertex shader
1006 * generates at least every output required by the
1007 * fragment shader plus fixed-function hardware (such as
1008 * BFC).
1009 *
1010 * However, some drivers may need us to identify the PNTC and TEXi
1011 * varyings if, for example, their capability to replace them with
1012 * sprite coordinates is limited.
1013 */
1014 case VARYING_SLOT_PNTC:
1015 if (st->needs_texcoord_semantic) {
1016 input_semantic_name[slot] = TGSI_SEMANTIC_PCOORD;
1017 input_semantic_index[slot] = 0;
1018 interpMode[slot] = TGSI_INTERPOLATE_LINEAR;
1019 break;
1020 }
1021 /* fall through */
1022 case VARYING_SLOT_TEX0:
1023 case VARYING_SLOT_TEX1:
1024 case VARYING_SLOT_TEX2:
1025 case VARYING_SLOT_TEX3:
1026 case VARYING_SLOT_TEX4:
1027 case VARYING_SLOT_TEX5:
1028 case VARYING_SLOT_TEX6:
1029 case VARYING_SLOT_TEX7:
1030 if (st->needs_texcoord_semantic) {
1031 input_semantic_name[slot] = TGSI_SEMANTIC_TEXCOORD;
1032 input_semantic_index[slot] = attr - VARYING_SLOT_TEX0;
1033 interpMode[slot] = stfp->glsl_to_tgsi ?
1034 TGSI_INTERPOLATE_COUNT : TGSI_INTERPOLATE_PERSPECTIVE;
1035 break;
1036 }
1037 /* fall through */
1038 case VARYING_SLOT_VAR0:
1039 default:
1040 /* Semantic indices should be zero-based because drivers may choose
1041 * to assign a fixed slot determined by that index.
1042 * This is useful because ARB_separate_shader_objects uses location
1043 * qualifiers for linkage, and if the semantic index corresponds to
1044 * these locations, linkage passes in the driver become unecessary.
1045 *
1046 * If needs_texcoord_semantic is true, no semantic indices will be
1047 * consumed for the TEXi varyings, and we can base the locations of
1048 * the user varyings on VAR0. Otherwise, we use TEX0 as base index.
1049 */
1050 assert(attr >= VARYING_SLOT_VAR0 || attr == VARYING_SLOT_PNTC ||
1051 (attr >= VARYING_SLOT_TEX0 && attr <= VARYING_SLOT_TEX7));
1052 input_semantic_name[slot] = TGSI_SEMANTIC_GENERIC;
1053 input_semantic_index[slot] = st_get_generic_varying_index(st, attr);
1054 if (attr == VARYING_SLOT_PNTC)
1055 interpMode[slot] = TGSI_INTERPOLATE_LINEAR;
1056 else {
1057 interpMode[slot] = stfp->glsl_to_tgsi ?
1058 TGSI_INTERPOLATE_COUNT : TGSI_INTERPOLATE_PERSPECTIVE;
1059 }
1060 break;
1061 }
1062 }
1063 else {
1064 inputMapping[attr] = -1;
1065 }
1066 }
1067
1068 /*
1069 * Semantics and mapping for outputs
1070 */
1071 GLbitfield64 outputsWritten = stfp->Base.info.outputs_written;
1072
1073 /* if z is written, emit that first */
1074 if (outputsWritten & BITFIELD64_BIT(FRAG_RESULT_DEPTH)) {
1075 fs_output_semantic_name[fs_num_outputs] = TGSI_SEMANTIC_POSITION;
1076 fs_output_semantic_index[fs_num_outputs] = 0;
1077 outputMapping[FRAG_RESULT_DEPTH] = fs_num_outputs;
1078 fs_num_outputs++;
1079 outputsWritten &= ~(1 << FRAG_RESULT_DEPTH);
1080 }
1081
1082 if (outputsWritten & BITFIELD64_BIT(FRAG_RESULT_STENCIL)) {
1083 fs_output_semantic_name[fs_num_outputs] = TGSI_SEMANTIC_STENCIL;
1084 fs_output_semantic_index[fs_num_outputs] = 0;
1085 outputMapping[FRAG_RESULT_STENCIL] = fs_num_outputs;
1086 fs_num_outputs++;
1087 outputsWritten &= ~(1 << FRAG_RESULT_STENCIL);
1088 }
1089
1090 if (outputsWritten & BITFIELD64_BIT(FRAG_RESULT_SAMPLE_MASK)) {
1091 fs_output_semantic_name[fs_num_outputs] = TGSI_SEMANTIC_SAMPLEMASK;
1092 fs_output_semantic_index[fs_num_outputs] = 0;
1093 outputMapping[FRAG_RESULT_SAMPLE_MASK] = fs_num_outputs;
1094 fs_num_outputs++;
1095 outputsWritten &= ~(1 << FRAG_RESULT_SAMPLE_MASK);
1096 }
1097
1098 /* handle remaining outputs (color) */
1099 for (attr = 0; attr < ARRAY_SIZE(outputMapping); attr++) {
1100 const GLbitfield64 written = attr < FRAG_RESULT_MAX ? outputsWritten :
1101 stfp->Base.SecondaryOutputsWritten;
1102 const unsigned loc = attr % FRAG_RESULT_MAX;
1103
1104 if (written & BITFIELD64_BIT(loc)) {
1105 switch (loc) {
1106 case FRAG_RESULT_DEPTH:
1107 case FRAG_RESULT_STENCIL:
1108 case FRAG_RESULT_SAMPLE_MASK:
1109 /* handled above */
1110 assert(0);
1111 break;
1112 case FRAG_RESULT_COLOR:
1113 write_all = GL_TRUE; /* fallthrough */
1114 default: {
1115 int index;
1116 assert(loc == FRAG_RESULT_COLOR ||
1117 (FRAG_RESULT_DATA0 <= loc && loc < FRAG_RESULT_MAX));
1118
1119 index = (loc == FRAG_RESULT_COLOR) ? 0 : (loc - FRAG_RESULT_DATA0);
1120
1121 if (attr >= FRAG_RESULT_MAX) {
1122 /* Secondary color for dual source blending. */
1123 assert(index == 0);
1124 index++;
1125 }
1126
1127 fs_output_semantic_name[fs_num_outputs] = TGSI_SEMANTIC_COLOR;
1128 fs_output_semantic_index[fs_num_outputs] = index;
1129 outputMapping[attr] = fs_num_outputs;
1130 break;
1131 }
1132 }
1133
1134 fs_num_outputs++;
1135 }
1136 }
1137
1138 ureg = ureg_create_with_screen(PIPE_SHADER_FRAGMENT, st->pipe->screen);
1139 if (ureg == NULL)
1140 return false;
1141
1142 if (ST_DEBUG & DEBUG_MESA) {
1143 _mesa_print_program(&stfp->Base);
1144 _mesa_print_program_parameters(st->ctx, &stfp->Base);
1145 debug_printf("\n");
1146 }
1147 if (write_all == GL_TRUE)
1148 ureg_property(ureg, TGSI_PROPERTY_FS_COLOR0_WRITES_ALL_CBUFS, 1);
1149
1150 if (stfp->Base.info.fs.depth_layout != FRAG_DEPTH_LAYOUT_NONE) {
1151 switch (stfp->Base.info.fs.depth_layout) {
1152 case FRAG_DEPTH_LAYOUT_ANY:
1153 ureg_property(ureg, TGSI_PROPERTY_FS_DEPTH_LAYOUT,
1154 TGSI_FS_DEPTH_LAYOUT_ANY);
1155 break;
1156 case FRAG_DEPTH_LAYOUT_GREATER:
1157 ureg_property(ureg, TGSI_PROPERTY_FS_DEPTH_LAYOUT,
1158 TGSI_FS_DEPTH_LAYOUT_GREATER);
1159 break;
1160 case FRAG_DEPTH_LAYOUT_LESS:
1161 ureg_property(ureg, TGSI_PROPERTY_FS_DEPTH_LAYOUT,
1162 TGSI_FS_DEPTH_LAYOUT_LESS);
1163 break;
1164 case FRAG_DEPTH_LAYOUT_UNCHANGED:
1165 ureg_property(ureg, TGSI_PROPERTY_FS_DEPTH_LAYOUT,
1166 TGSI_FS_DEPTH_LAYOUT_UNCHANGED);
1167 break;
1168 default:
1169 assert(0);
1170 }
1171 }
1172
1173 if (stfp->glsl_to_tgsi) {
1174 st_translate_program(st->ctx,
1175 PIPE_SHADER_FRAGMENT,
1176 ureg,
1177 stfp->glsl_to_tgsi,
1178 &stfp->Base,
1179 /* inputs */
1180 fs_num_inputs,
1181 inputMapping,
1182 inputSlotToAttr,
1183 input_semantic_name,
1184 input_semantic_index,
1185 interpMode,
1186 /* outputs */
1187 fs_num_outputs,
1188 outputMapping,
1189 fs_output_semantic_name,
1190 fs_output_semantic_index);
1191
1192 free_glsl_to_tgsi_visitor(stfp->glsl_to_tgsi);
1193 } else if (stfp->ati_fs)
1194 st_translate_atifs_program(ureg,
1195 stfp->ati_fs,
1196 &stfp->Base,
1197 /* inputs */
1198 fs_num_inputs,
1199 inputMapping,
1200 input_semantic_name,
1201 input_semantic_index,
1202 interpMode,
1203 /* outputs */
1204 fs_num_outputs,
1205 outputMapping,
1206 fs_output_semantic_name,
1207 fs_output_semantic_index);
1208 else
1209 st_translate_mesa_program(st->ctx,
1210 PIPE_SHADER_FRAGMENT,
1211 ureg,
1212 &stfp->Base,
1213 /* inputs */
1214 fs_num_inputs,
1215 inputMapping,
1216 input_semantic_name,
1217 input_semantic_index,
1218 interpMode,
1219 /* outputs */
1220 fs_num_outputs,
1221 outputMapping,
1222 fs_output_semantic_name,
1223 fs_output_semantic_index);
1224
1225 stfp->state.tokens = ureg_get_tokens(ureg, NULL);
1226 ureg_destroy(ureg);
1227
1228 if (stfp->glsl_to_tgsi) {
1229 stfp->glsl_to_tgsi = NULL;
1230 st_store_ir_in_disk_cache(st, &stfp->Base, false);
1231 }
1232
1233 return stfp->state.tokens != NULL;
1234 }
1235
1236 static struct st_fp_variant *
1237 st_create_fp_variant(struct st_context *st,
1238 struct st_program *stfp,
1239 const struct st_fp_variant_key *key)
1240 {
1241 struct pipe_context *pipe = st->pipe;
1242 struct st_fp_variant *variant = CALLOC_STRUCT(st_fp_variant);
1243 struct pipe_shader_state state = {0};
1244 struct gl_program_parameter_list *params = stfp->Base.Parameters;
1245 static const gl_state_index16 texcoord_state[STATE_LENGTH] =
1246 { STATE_INTERNAL, STATE_CURRENT_ATTRIB, VERT_ATTRIB_TEX0 };
1247 static const gl_state_index16 scale_state[STATE_LENGTH] =
1248 { STATE_INTERNAL, STATE_PT_SCALE };
1249 static const gl_state_index16 bias_state[STATE_LENGTH] =
1250 { STATE_INTERNAL, STATE_PT_BIAS };
1251 static const gl_state_index16 alpha_ref_state[STATE_LENGTH] =
1252 { STATE_INTERNAL, STATE_ALPHA_REF };
1253
1254 if (!variant)
1255 return NULL;
1256
1257 if (stfp->state.type == PIPE_SHADER_IR_NIR) {
1258 bool finalize = false;
1259
1260 state.type = PIPE_SHADER_IR_NIR;
1261 state.ir.nir = get_nir_shader(st, stfp);
1262
1263 if (key->clamp_color) {
1264 NIR_PASS_V(state.ir.nir, nir_lower_clamp_color_outputs);
1265 finalize = true;
1266 }
1267
1268 if (key->lower_flatshade) {
1269 NIR_PASS_V(state.ir.nir, nir_lower_flatshade);
1270 finalize = true;
1271 }
1272
1273 if (key->lower_alpha_func != COMPARE_FUNC_NEVER) {
1274 _mesa_add_state_reference(params, alpha_ref_state);
1275 NIR_PASS_V(state.ir.nir, nir_lower_alpha_test, key->lower_alpha_func,
1276 false, alpha_ref_state);
1277 finalize = true;
1278 }
1279
1280 if (key->lower_two_sided_color) {
1281 bool face_sysval = st->ctx->Const.GLSLFrontFacingIsSysVal;
1282 NIR_PASS_V(state.ir.nir, nir_lower_two_sided_color, face_sysval);
1283 finalize = true;
1284 }
1285
1286 if (key->persample_shading) {
1287 nir_shader *shader = state.ir.nir;
1288 nir_foreach_variable(var, &shader->inputs)
1289 var->data.sample = true;
1290 finalize = true;
1291 }
1292
1293 assert(!(key->bitmap && key->drawpixels));
1294
1295 /* glBitmap */
1296 if (key->bitmap) {
1297 nir_lower_bitmap_options options = {0};
1298
1299 variant->bitmap_sampler = ffs(~stfp->Base.SamplersUsed) - 1;
1300 options.sampler = variant->bitmap_sampler;
1301 options.swizzle_xxxx = st->bitmap.tex_format == PIPE_FORMAT_R8_UNORM;
1302
1303 NIR_PASS_V(state.ir.nir, nir_lower_bitmap, &options);
1304 finalize = true;
1305 }
1306
1307 /* glDrawPixels (color only) */
1308 if (key->drawpixels) {
1309 nir_lower_drawpixels_options options = {{0}};
1310 unsigned samplers_used = stfp->Base.SamplersUsed;
1311
1312 /* Find the first unused slot. */
1313 variant->drawpix_sampler = ffs(~samplers_used) - 1;
1314 options.drawpix_sampler = variant->drawpix_sampler;
1315 samplers_used |= (1 << variant->drawpix_sampler);
1316
1317 options.pixel_maps = key->pixelMaps;
1318 if (key->pixelMaps) {
1319 variant->pixelmap_sampler = ffs(~samplers_used) - 1;
1320 options.pixelmap_sampler = variant->pixelmap_sampler;
1321 }
1322
1323 options.scale_and_bias = key->scaleAndBias;
1324 if (key->scaleAndBias) {
1325 _mesa_add_state_reference(params, scale_state);
1326 memcpy(options.scale_state_tokens, scale_state,
1327 sizeof(options.scale_state_tokens));
1328 _mesa_add_state_reference(params, bias_state);
1329 memcpy(options.bias_state_tokens, bias_state,
1330 sizeof(options.bias_state_tokens));
1331 }
1332
1333 _mesa_add_state_reference(params, texcoord_state);
1334 memcpy(options.texcoord_state_tokens, texcoord_state,
1335 sizeof(options.texcoord_state_tokens));
1336
1337 NIR_PASS_V(state.ir.nir, nir_lower_drawpixels, &options);
1338 finalize = true;
1339 }
1340
1341 if (unlikely(key->external.lower_nv12 || key->external.lower_iyuv ||
1342 key->external.lower_xy_uxvx || key->external.lower_yx_xuxv ||
1343 key->external.lower_ayuv || key->external.lower_xyuv)) {
1344
1345 st_nir_lower_samplers(pipe->screen, state.ir.nir,
1346 stfp->shader_program, &stfp->Base);
1347
1348 nir_lower_tex_options options = {0};
1349 options.lower_y_uv_external = key->external.lower_nv12;
1350 options.lower_y_u_v_external = key->external.lower_iyuv;
1351 options.lower_xy_uxvx_external = key->external.lower_xy_uxvx;
1352 options.lower_yx_xuxv_external = key->external.lower_yx_xuxv;
1353 options.lower_ayuv_external = key->external.lower_ayuv;
1354 options.lower_xyuv_external = key->external.lower_xyuv;
1355 NIR_PASS_V(state.ir.nir, nir_lower_tex, &options);
1356 finalize = true;
1357 }
1358
1359 if (finalize || !st->allow_st_finalize_nir_twice) {
1360 st_finalize_nir(st, &stfp->Base, stfp->shader_program, state.ir.nir,
1361 false);
1362 }
1363
1364 /* This pass needs to happen *after* nir_lower_sampler */
1365 if (unlikely(key->external.lower_nv12 || key->external.lower_iyuv ||
1366 key->external.lower_xy_uxvx || key->external.lower_yx_xuxv ||
1367 key->external.lower_ayuv || key->external.lower_xyuv)) {
1368 NIR_PASS_V(state.ir.nir, st_nir_lower_tex_src_plane,
1369 ~stfp->Base.SamplersUsed,
1370 key->external.lower_nv12 || key->external.lower_xy_uxvx ||
1371 key->external.lower_yx_xuxv,
1372 key->external.lower_iyuv);
1373 finalize = true;
1374 }
1375
1376 if (finalize || !st->allow_st_finalize_nir_twice) {
1377 /* Some of the lowering above may have introduced new varyings */
1378 nir_shader_gather_info(state.ir.nir,
1379 nir_shader_get_entrypoint(state.ir.nir));
1380
1381 struct pipe_screen *screen = pipe->screen;
1382 if (screen->finalize_nir)
1383 screen->finalize_nir(screen, state.ir.nir, false);
1384 }
1385
1386 if (ST_DEBUG & DEBUG_PRINT_IR)
1387 nir_print_shader(state.ir.nir, stderr);
1388
1389 variant->base.driver_shader = pipe->create_fs_state(pipe, &state);
1390 variant->key = *key;
1391
1392 return variant;
1393 }
1394
1395 state.tokens = stfp->state.tokens;
1396
1397 assert(!(key->bitmap && key->drawpixels));
1398
1399 /* Fix texture targets and add fog for ATI_fs */
1400 if (stfp->ati_fs) {
1401 const struct tgsi_token *tokens = st_fixup_atifs(state.tokens, key);
1402
1403 if (tokens)
1404 state.tokens = tokens;
1405 else
1406 fprintf(stderr, "mesa: cannot post-process ATI_fs\n");
1407 }
1408
1409 /* Emulate features. */
1410 if (key->clamp_color || key->persample_shading) {
1411 const struct tgsi_token *tokens;
1412 unsigned flags =
1413 (key->clamp_color ? TGSI_EMU_CLAMP_COLOR_OUTPUTS : 0) |
1414 (key->persample_shading ? TGSI_EMU_FORCE_PERSAMPLE_INTERP : 0);
1415
1416 tokens = tgsi_emulate(state.tokens, flags);
1417
1418 if (tokens) {
1419 if (state.tokens != stfp->state.tokens)
1420 tgsi_free_tokens(state.tokens);
1421 state.tokens = tokens;
1422 } else
1423 fprintf(stderr, "mesa: cannot emulate deprecated features\n");
1424 }
1425
1426 /* glBitmap */
1427 if (key->bitmap) {
1428 const struct tgsi_token *tokens;
1429
1430 variant->bitmap_sampler = ffs(~stfp->Base.SamplersUsed) - 1;
1431
1432 tokens = st_get_bitmap_shader(state.tokens,
1433 st->internal_target,
1434 variant->bitmap_sampler,
1435 st->needs_texcoord_semantic,
1436 st->bitmap.tex_format ==
1437 PIPE_FORMAT_R8_UNORM);
1438
1439 if (tokens) {
1440 if (state.tokens != stfp->state.tokens)
1441 tgsi_free_tokens(state.tokens);
1442 state.tokens = tokens;
1443 } else
1444 fprintf(stderr, "mesa: cannot create a shader for glBitmap\n");
1445 }
1446
1447 /* glDrawPixels (color only) */
1448 if (key->drawpixels) {
1449 const struct tgsi_token *tokens;
1450 unsigned scale_const = 0, bias_const = 0, texcoord_const = 0;
1451
1452 /* Find the first unused slot. */
1453 variant->drawpix_sampler = ffs(~stfp->Base.SamplersUsed) - 1;
1454
1455 if (key->pixelMaps) {
1456 unsigned samplers_used = stfp->Base.SamplersUsed |
1457 (1 << variant->drawpix_sampler);
1458
1459 variant->pixelmap_sampler = ffs(~samplers_used) - 1;
1460 }
1461
1462 if (key->scaleAndBias) {
1463 scale_const = _mesa_add_state_reference(params, scale_state);
1464 bias_const = _mesa_add_state_reference(params, bias_state);
1465 }
1466
1467 texcoord_const = _mesa_add_state_reference(params, texcoord_state);
1468
1469 tokens = st_get_drawpix_shader(state.tokens,
1470 st->needs_texcoord_semantic,
1471 key->scaleAndBias, scale_const,
1472 bias_const, key->pixelMaps,
1473 variant->drawpix_sampler,
1474 variant->pixelmap_sampler,
1475 texcoord_const, st->internal_target);
1476
1477 if (tokens) {
1478 if (state.tokens != stfp->state.tokens)
1479 tgsi_free_tokens(state.tokens);
1480 state.tokens = tokens;
1481 } else
1482 fprintf(stderr, "mesa: cannot create a shader for glDrawPixels\n");
1483 }
1484
1485 if (unlikely(key->external.lower_nv12 || key->external.lower_iyuv ||
1486 key->external.lower_xy_uxvx || key->external.lower_yx_xuxv)) {
1487 const struct tgsi_token *tokens;
1488
1489 /* samplers inserted would conflict, but this should be unpossible: */
1490 assert(!(key->bitmap || key->drawpixels));
1491
1492 tokens = st_tgsi_lower_yuv(state.tokens,
1493 ~stfp->Base.SamplersUsed,
1494 key->external.lower_nv12 ||
1495 key->external.lower_xy_uxvx ||
1496 key->external.lower_yx_xuxv,
1497 key->external.lower_iyuv);
1498 if (tokens) {
1499 if (state.tokens != stfp->state.tokens)
1500 tgsi_free_tokens(state.tokens);
1501 state.tokens = tokens;
1502 } else {
1503 fprintf(stderr, "mesa: cannot create a shader for samplerExternalOES\n");
1504 }
1505 }
1506
1507 if (key->lower_depth_clamp) {
1508 unsigned depth_range_const = _mesa_add_state_reference(params, depth_range_state);
1509
1510 const struct tgsi_token *tokens;
1511 tokens = st_tgsi_lower_depth_clamp_fs(state.tokens, depth_range_const);
1512 if (state.tokens != stfp->state.tokens)
1513 tgsi_free_tokens(state.tokens);
1514 state.tokens = tokens;
1515 }
1516
1517 if (ST_DEBUG & DEBUG_PRINT_IR)
1518 tgsi_dump(state.tokens, 0);
1519
1520 /* fill in variant */
1521 variant->base.driver_shader = pipe->create_fs_state(pipe, &state);
1522 variant->key = *key;
1523
1524 if (state.tokens != stfp->state.tokens)
1525 tgsi_free_tokens(state.tokens);
1526 return variant;
1527 }
1528
1529 /**
1530 * Translate fragment program if needed.
1531 */
1532 struct st_fp_variant *
1533 st_get_fp_variant(struct st_context *st,
1534 struct st_program *stfp,
1535 const struct st_fp_variant_key *key)
1536 {
1537 struct st_fp_variant *fpv;
1538
1539 /* Search for existing variant */
1540 for (fpv = st_fp_variant(stfp->variants); fpv;
1541 fpv = st_fp_variant(fpv->base.next)) {
1542 if (memcmp(&fpv->key, key, sizeof(*key)) == 0) {
1543 break;
1544 }
1545 }
1546
1547 if (!fpv) {
1548 /* create new */
1549 fpv = st_create_fp_variant(st, stfp, key);
1550 if (fpv) {
1551 fpv->base.st = key->st;
1552
1553 if (key->bitmap || key->drawpixels) {
1554 /* Regular variants should always come before the
1555 * bitmap & drawpixels variants, (unless there
1556 * are no regular variants) so that
1557 * st_update_fp can take a fast path when
1558 * shader_has_one_variant is set.
1559 */
1560 if (!stfp->variants) {
1561 stfp->variants = &fpv->base;
1562 } else {
1563 /* insert into list after the first one */
1564 fpv->base.next = stfp->variants->next;
1565 stfp->variants->next = &fpv->base;
1566 }
1567 } else {
1568 /* insert into list */
1569 fpv->base.next = stfp->variants;
1570 stfp->variants = &fpv->base;
1571 }
1572 }
1573 }
1574
1575 return fpv;
1576 }
1577
1578 /**
1579 * Translate a program. This is common code for geometry and tessellation
1580 * shaders.
1581 */
1582 bool
1583 st_translate_common_program(struct st_context *st,
1584 struct st_program *stp)
1585 {
1586 struct gl_program *prog = &stp->Base;
1587 enum pipe_shader_type stage =
1588 pipe_shader_type_from_mesa(stp->Base.info.stage);
1589 struct ureg_program *ureg = ureg_create_with_screen(stage, st->pipe->screen);
1590
1591 if (ureg == NULL)
1592 return false;
1593
1594 switch (stage) {
1595 case PIPE_SHADER_TESS_CTRL:
1596 ureg_property(ureg, TGSI_PROPERTY_TCS_VERTICES_OUT,
1597 stp->Base.info.tess.tcs_vertices_out);
1598 break;
1599
1600 case PIPE_SHADER_TESS_EVAL:
1601 if (stp->Base.info.tess.primitive_mode == GL_ISOLINES)
1602 ureg_property(ureg, TGSI_PROPERTY_TES_PRIM_MODE, GL_LINES);
1603 else
1604 ureg_property(ureg, TGSI_PROPERTY_TES_PRIM_MODE,
1605 stp->Base.info.tess.primitive_mode);
1606
1607 STATIC_ASSERT((TESS_SPACING_EQUAL + 1) % 3 == PIPE_TESS_SPACING_EQUAL);
1608 STATIC_ASSERT((TESS_SPACING_FRACTIONAL_ODD + 1) % 3 ==
1609 PIPE_TESS_SPACING_FRACTIONAL_ODD);
1610 STATIC_ASSERT((TESS_SPACING_FRACTIONAL_EVEN + 1) % 3 ==
1611 PIPE_TESS_SPACING_FRACTIONAL_EVEN);
1612
1613 ureg_property(ureg, TGSI_PROPERTY_TES_SPACING,
1614 (stp->Base.info.tess.spacing + 1) % 3);
1615
1616 ureg_property(ureg, TGSI_PROPERTY_TES_VERTEX_ORDER_CW,
1617 !stp->Base.info.tess.ccw);
1618 ureg_property(ureg, TGSI_PROPERTY_TES_POINT_MODE,
1619 stp->Base.info.tess.point_mode);
1620 break;
1621
1622 case PIPE_SHADER_GEOMETRY:
1623 ureg_property(ureg, TGSI_PROPERTY_GS_INPUT_PRIM,
1624 stp->Base.info.gs.input_primitive);
1625 ureg_property(ureg, TGSI_PROPERTY_GS_OUTPUT_PRIM,
1626 stp->Base.info.gs.output_primitive);
1627 ureg_property(ureg, TGSI_PROPERTY_GS_MAX_OUTPUT_VERTICES,
1628 stp->Base.info.gs.vertices_out);
1629 ureg_property(ureg, TGSI_PROPERTY_GS_INVOCATIONS,
1630 stp->Base.info.gs.invocations);
1631 break;
1632
1633 default:
1634 break;
1635 }
1636
1637 ubyte inputSlotToAttr[VARYING_SLOT_TESS_MAX];
1638 ubyte inputMapping[VARYING_SLOT_TESS_MAX];
1639 ubyte outputMapping[VARYING_SLOT_TESS_MAX];
1640 GLuint attr;
1641
1642 ubyte input_semantic_name[PIPE_MAX_SHADER_INPUTS];
1643 ubyte input_semantic_index[PIPE_MAX_SHADER_INPUTS];
1644 uint num_inputs = 0;
1645
1646 ubyte output_semantic_name[PIPE_MAX_SHADER_OUTPUTS];
1647 ubyte output_semantic_index[PIPE_MAX_SHADER_OUTPUTS];
1648 uint num_outputs = 0;
1649
1650 GLint i;
1651
1652 memset(inputSlotToAttr, 0, sizeof(inputSlotToAttr));
1653 memset(inputMapping, 0, sizeof(inputMapping));
1654 memset(outputMapping, 0, sizeof(outputMapping));
1655 memset(&stp->state, 0, sizeof(stp->state));
1656
1657 if (prog->info.clip_distance_array_size)
1658 ureg_property(ureg, TGSI_PROPERTY_NUM_CLIPDIST_ENABLED,
1659 prog->info.clip_distance_array_size);
1660 if (prog->info.cull_distance_array_size)
1661 ureg_property(ureg, TGSI_PROPERTY_NUM_CULLDIST_ENABLED,
1662 prog->info.cull_distance_array_size);
1663
1664 /*
1665 * Convert Mesa program inputs to TGSI input register semantics.
1666 */
1667 for (attr = 0; attr < VARYING_SLOT_MAX; attr++) {
1668 if ((prog->info.inputs_read & BITFIELD64_BIT(attr)) == 0)
1669 continue;
1670
1671 unsigned slot = num_inputs++;
1672
1673 inputMapping[attr] = slot;
1674 inputSlotToAttr[slot] = attr;
1675
1676 unsigned semantic_name, semantic_index;
1677 tgsi_get_gl_varying_semantic(attr, st->needs_texcoord_semantic,
1678 &semantic_name, &semantic_index);
1679 input_semantic_name[slot] = semantic_name;
1680 input_semantic_index[slot] = semantic_index;
1681 }
1682
1683 /* Also add patch inputs. */
1684 for (attr = 0; attr < 32; attr++) {
1685 if (prog->info.patch_inputs_read & (1u << attr)) {
1686 GLuint slot = num_inputs++;
1687 GLuint patch_attr = VARYING_SLOT_PATCH0 + attr;
1688
1689 inputMapping[patch_attr] = slot;
1690 inputSlotToAttr[slot] = patch_attr;
1691 input_semantic_name[slot] = TGSI_SEMANTIC_PATCH;
1692 input_semantic_index[slot] = attr;
1693 }
1694 }
1695
1696 /* initialize output semantics to defaults */
1697 for (i = 0; i < PIPE_MAX_SHADER_OUTPUTS; i++) {
1698 output_semantic_name[i] = TGSI_SEMANTIC_GENERIC;
1699 output_semantic_index[i] = 0;
1700 }
1701
1702 /*
1703 * Determine number of outputs, the (default) output register
1704 * mapping and the semantic information for each output.
1705 */
1706 for (attr = 0; attr < VARYING_SLOT_MAX; attr++) {
1707 if (prog->info.outputs_written & BITFIELD64_BIT(attr)) {
1708 GLuint slot = num_outputs++;
1709
1710 outputMapping[attr] = slot;
1711
1712 unsigned semantic_name, semantic_index;
1713 tgsi_get_gl_varying_semantic(attr, st->needs_texcoord_semantic,
1714 &semantic_name, &semantic_index);
1715 output_semantic_name[slot] = semantic_name;
1716 output_semantic_index[slot] = semantic_index;
1717 }
1718 }
1719
1720 /* Also add patch outputs. */
1721 for (attr = 0; attr < 32; attr++) {
1722 if (prog->info.patch_outputs_written & (1u << attr)) {
1723 GLuint slot = num_outputs++;
1724 GLuint patch_attr = VARYING_SLOT_PATCH0 + attr;
1725
1726 outputMapping[patch_attr] = slot;
1727 output_semantic_name[slot] = TGSI_SEMANTIC_PATCH;
1728 output_semantic_index[slot] = attr;
1729 }
1730 }
1731
1732 st_translate_program(st->ctx,
1733 stage,
1734 ureg,
1735 stp->glsl_to_tgsi,
1736 prog,
1737 /* inputs */
1738 num_inputs,
1739 inputMapping,
1740 inputSlotToAttr,
1741 input_semantic_name,
1742 input_semantic_index,
1743 NULL,
1744 /* outputs */
1745 num_outputs,
1746 outputMapping,
1747 output_semantic_name,
1748 output_semantic_index);
1749
1750 stp->state.tokens = ureg_get_tokens(ureg, NULL);
1751
1752 ureg_destroy(ureg);
1753
1754 st_translate_stream_output_info(prog);
1755
1756 st_store_ir_in_disk_cache(st, prog, false);
1757
1758 if (ST_DEBUG & DEBUG_PRINT_IR && ST_DEBUG & DEBUG_MESA)
1759 _mesa_print_program(prog);
1760
1761 free_glsl_to_tgsi_visitor(stp->glsl_to_tgsi);
1762 stp->glsl_to_tgsi = NULL;
1763 return true;
1764 }
1765
1766
1767 /**
1768 * Get/create a basic program variant.
1769 */
1770 struct st_variant *
1771 st_get_common_variant(struct st_context *st,
1772 struct st_program *prog,
1773 const struct st_common_variant_key *key)
1774 {
1775 struct pipe_context *pipe = st->pipe;
1776 struct st_variant *v;
1777 struct pipe_shader_state state = {0};
1778
1779 /* Search for existing variant */
1780 for (v = prog->variants; v; v = v->next) {
1781 if (memcmp(&st_common_variant(v)->key, key, sizeof(*key)) == 0)
1782 break;
1783 }
1784
1785 if (!v) {
1786 /* create new */
1787 v = (struct st_variant*)CALLOC_STRUCT(st_common_variant);
1788 if (v) {
1789 if (prog->state.type == PIPE_SHADER_IR_NIR) {
1790 bool finalize = false;
1791
1792 state.type = PIPE_SHADER_IR_NIR;
1793 state.ir.nir = get_nir_shader(st, prog);
1794
1795 if (key->clamp_color) {
1796 NIR_PASS_V(state.ir.nir, nir_lower_clamp_color_outputs);
1797 finalize = true;
1798 }
1799
1800 state.stream_output = prog->state.stream_output;
1801
1802 if (finalize || !st->allow_st_finalize_nir_twice) {
1803 st_finalize_nir(st, &prog->Base, prog->shader_program,
1804 state.ir.nir, true);
1805 }
1806
1807 if (ST_DEBUG & DEBUG_PRINT_IR)
1808 nir_print_shader(state.ir.nir, stderr);
1809 } else {
1810 if (key->lower_depth_clamp) {
1811 struct gl_program_parameter_list *params = prog->Base.Parameters;
1812
1813 unsigned depth_range_const =
1814 _mesa_add_state_reference(params, depth_range_state);
1815
1816 const struct tgsi_token *tokens;
1817 tokens =
1818 st_tgsi_lower_depth_clamp(prog->state.tokens,
1819 depth_range_const,
1820 key->clip_negative_one_to_one);
1821
1822 if (tokens != prog->state.tokens)
1823 tgsi_free_tokens(prog->state.tokens);
1824
1825 prog->state.tokens = tokens;
1826 }
1827 state = prog->state;
1828
1829 if (ST_DEBUG & DEBUG_PRINT_IR)
1830 tgsi_dump(state.tokens, 0);
1831 }
1832 /* fill in new variant */
1833 switch (prog->Base.info.stage) {
1834 case MESA_SHADER_TESS_CTRL:
1835 v->driver_shader = pipe->create_tcs_state(pipe, &state);
1836 break;
1837 case MESA_SHADER_TESS_EVAL:
1838 v->driver_shader = pipe->create_tes_state(pipe, &state);
1839 break;
1840 case MESA_SHADER_GEOMETRY:
1841 v->driver_shader = pipe->create_gs_state(pipe, &state);
1842 break;
1843 case MESA_SHADER_COMPUTE: {
1844 struct pipe_compute_state cs = {0};
1845 cs.ir_type = state.type;
1846 cs.req_local_mem = prog->Base.info.cs.shared_size;
1847
1848 if (state.type == PIPE_SHADER_IR_NIR)
1849 cs.prog = state.ir.nir;
1850 else
1851 cs.prog = state.tokens;
1852
1853 v->driver_shader = pipe->create_compute_state(pipe, &cs);
1854 break;
1855 }
1856 default:
1857 assert(!"unhandled shader type");
1858 free(v);
1859 return NULL;
1860 }
1861
1862 st_common_variant(v)->key = *key;
1863 v->st = key->st;
1864
1865 /* insert into list */
1866 v->next = prog->variants;
1867 prog->variants = v;
1868 }
1869 }
1870
1871 return v;
1872 }
1873
1874
1875 /**
1876 * Vert/Geom/Frag programs have per-context variants. Free all the
1877 * variants attached to the given program which match the given context.
1878 */
1879 static void
1880 destroy_program_variants(struct st_context *st, struct gl_program *target)
1881 {
1882 if (!target || target == &_mesa_DummyProgram)
1883 return;
1884
1885 struct st_program *p = st_program(target);
1886 struct st_variant *v, **prevPtr = &p->variants;
1887 bool unbound = false;
1888
1889 for (v = p->variants; v; ) {
1890 struct st_variant *next = v->next;
1891 if (v->st == st) {
1892 if (!unbound) {
1893 st_unbind_program(st, p);
1894 unbound = true;
1895 }
1896
1897 /* unlink from list */
1898 *prevPtr = next;
1899 /* destroy this variant */
1900 delete_variant(st, v, target->Target);
1901 }
1902 else {
1903 prevPtr = &v->next;
1904 }
1905 v = next;
1906 }
1907 }
1908
1909
1910 /**
1911 * Callback for _mesa_HashWalk. Free all the shader's program variants
1912 * which match the given context.
1913 */
1914 static void
1915 destroy_shader_program_variants_cb(GLuint key, void *data, void *userData)
1916 {
1917 struct st_context *st = (struct st_context *) userData;
1918 struct gl_shader *shader = (struct gl_shader *) data;
1919
1920 switch (shader->Type) {
1921 case GL_SHADER_PROGRAM_MESA:
1922 {
1923 struct gl_shader_program *shProg = (struct gl_shader_program *) data;
1924 GLuint i;
1925
1926 for (i = 0; i < ARRAY_SIZE(shProg->_LinkedShaders); i++) {
1927 if (shProg->_LinkedShaders[i])
1928 destroy_program_variants(st, shProg->_LinkedShaders[i]->Program);
1929 }
1930 }
1931 break;
1932 case GL_VERTEX_SHADER:
1933 case GL_FRAGMENT_SHADER:
1934 case GL_GEOMETRY_SHADER:
1935 case GL_TESS_CONTROL_SHADER:
1936 case GL_TESS_EVALUATION_SHADER:
1937 case GL_COMPUTE_SHADER:
1938 break;
1939 default:
1940 assert(0);
1941 }
1942 }
1943
1944
1945 /**
1946 * Callback for _mesa_HashWalk. Free all the program variants which match
1947 * the given context.
1948 */
1949 static void
1950 destroy_program_variants_cb(GLuint key, void *data, void *userData)
1951 {
1952 struct st_context *st = (struct st_context *) userData;
1953 struct gl_program *program = (struct gl_program *) data;
1954 destroy_program_variants(st, program);
1955 }
1956
1957
1958 /**
1959 * Walk over all shaders and programs to delete any variants which
1960 * belong to the given context.
1961 * This is called during context tear-down.
1962 */
1963 void
1964 st_destroy_program_variants(struct st_context *st)
1965 {
1966 /* If shaders can be shared with other contexts, the last context will
1967 * call DeleteProgram on all shaders, releasing everything.
1968 */
1969 if (st->has_shareable_shaders)
1970 return;
1971
1972 /* ARB vert/frag program */
1973 _mesa_HashWalk(st->ctx->Shared->Programs,
1974 destroy_program_variants_cb, st);
1975
1976 /* GLSL vert/frag/geom shaders */
1977 _mesa_HashWalk(st->ctx->Shared->ShaderObjects,
1978 destroy_shader_program_variants_cb, st);
1979 }
1980
1981
1982 /**
1983 * Compile one shader variant.
1984 */
1985 static void
1986 st_precompile_shader_variant(struct st_context *st,
1987 struct gl_program *prog)
1988 {
1989 switch (prog->Target) {
1990 case GL_VERTEX_PROGRAM_ARB: {
1991 struct st_program *p = (struct st_program *)prog;
1992 struct st_common_variant_key key;
1993
1994 memset(&key, 0, sizeof(key));
1995
1996 key.st = st->has_shareable_shaders ? NULL : st;
1997 st_get_vp_variant(st, p, &key);
1998 break;
1999 }
2000
2001 case GL_FRAGMENT_PROGRAM_ARB: {
2002 struct st_program *p = (struct st_program *)prog;
2003 struct st_fp_variant_key key;
2004
2005 memset(&key, 0, sizeof(key));
2006
2007 key.st = st->has_shareable_shaders ? NULL : st;
2008 st_get_fp_variant(st, p, &key);
2009 break;
2010 }
2011
2012 case GL_TESS_CONTROL_PROGRAM_NV:
2013 case GL_TESS_EVALUATION_PROGRAM_NV:
2014 case GL_GEOMETRY_PROGRAM_NV:
2015 case GL_COMPUTE_PROGRAM_NV: {
2016 struct st_program *p = st_program(prog);
2017 struct st_common_variant_key key;
2018
2019 memset(&key, 0, sizeof(key));
2020
2021 key.st = st->has_shareable_shaders ? NULL : st;
2022 st_get_common_variant(st, p, &key);
2023 break;
2024 }
2025
2026 default:
2027 assert(0);
2028 }
2029 }
2030
2031 void
2032 st_serialize_nir(struct st_program *stp)
2033 {
2034 if (!stp->serialized_nir) {
2035 struct blob blob;
2036 size_t size;
2037
2038 blob_init(&blob);
2039 nir_serialize(&blob, stp->Base.nir, false);
2040 blob_finish_get_buffer(&blob, &stp->serialized_nir, &size);
2041 stp->serialized_nir_size = size;
2042 }
2043 }
2044
2045 void
2046 st_finalize_program(struct st_context *st, struct gl_program *prog)
2047 {
2048 if (st->current_program[prog->info.stage] == prog) {
2049 if (prog->info.stage == MESA_SHADER_VERTEX)
2050 st->dirty |= ST_NEW_VERTEX_PROGRAM(st, (struct st_program *)prog);
2051 else
2052 st->dirty |= ((struct st_program *)prog)->affected_states;
2053 }
2054
2055 if (prog->nir) {
2056 nir_sweep(prog->nir);
2057
2058 /* This is only needed for ARB_vp/fp programs and when the disk cache
2059 * is disabled. If the disk cache is enabled, GLSL programs are
2060 * serialized in write_nir_to_cache.
2061 */
2062 st_serialize_nir(st_program(prog));
2063 }
2064
2065 /* Create Gallium shaders now instead of on demand. */
2066 if (ST_DEBUG & DEBUG_PRECOMPILE ||
2067 st->shader_has_one_variant[prog->info.stage])
2068 st_precompile_shader_variant(st, prog);
2069 }