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