1848b8605Smrg/**************************************************************************
2848b8605Smrg *
3848b8605Smrg * Copyright 2007 VMware, Inc.
4848b8605Smrg * All Rights Reserved.
5848b8605Smrg *
6848b8605Smrg * Permission is hereby granted, free of charge, to any person obtaining a
7848b8605Smrg * copy of this software and associated documentation files (the
8848b8605Smrg * "Software"), to deal in the Software without restriction, including
9848b8605Smrg * without limitation the rights to use, copy, modify, merge, publish,
10848b8605Smrg * distribute, sub license, and/or sell copies of the Software, and to
11848b8605Smrg * permit persons to whom the Software is furnished to do so, subject to
12848b8605Smrg * the following conditions:
13848b8605Smrg *
14848b8605Smrg * The above copyright notice and this permission notice (including the
15848b8605Smrg * next paragraph) shall be included in all copies or substantial portions
16848b8605Smrg * of the Software.
17848b8605Smrg *
18848b8605Smrg * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
19848b8605Smrg * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
20848b8605Smrg * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT.
21848b8605Smrg * IN NO EVENT SHALL VMWARE AND/OR ITS SUPPLIERS BE LIABLE FOR
22848b8605Smrg * ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
23848b8605Smrg * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
24848b8605Smrg * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
25848b8605Smrg *
26848b8605Smrg **************************************************************************/
27848b8605Smrg /*
28848b8605Smrg  * Authors:
29848b8605Smrg  *   Keith Whitwell <keithw@vmware.com>
30848b8605Smrg  *   Brian Paul
31848b8605Smrg  */
32848b8605Smrg
33848b8605Smrg
34b8e80941Smrg#include "main/errors.h"
35848b8605Smrg#include "main/imports.h"
36848b8605Smrg#include "main/hash.h"
37848b8605Smrg#include "main/mtypes.h"
38848b8605Smrg#include "program/prog_parameter.h"
39848b8605Smrg#include "program/prog_print.h"
40b8e80941Smrg#include "program/prog_to_nir.h"
41848b8605Smrg#include "program/programopt.h"
42848b8605Smrg
43b8e80941Smrg#include "compiler/nir/nir.h"
44b8e80941Smrg
45848b8605Smrg#include "pipe/p_context.h"
46848b8605Smrg#include "pipe/p_defines.h"
47848b8605Smrg#include "pipe/p_shader_tokens.h"
48848b8605Smrg#include "draw/draw_context.h"
49848b8605Smrg#include "tgsi/tgsi_dump.h"
50b8e80941Smrg#include "tgsi/tgsi_emulate.h"
51b8e80941Smrg#include "tgsi/tgsi_parse.h"
52848b8605Smrg#include "tgsi/tgsi_ureg.h"
53848b8605Smrg
54848b8605Smrg#include "st_debug.h"
55848b8605Smrg#include "st_cb_bitmap.h"
56848b8605Smrg#include "st_cb_drawpixels.h"
57848b8605Smrg#include "st_context.h"
58b8e80941Smrg#include "st_tgsi_lower_yuv.h"
59848b8605Smrg#include "st_program.h"
60848b8605Smrg#include "st_mesa_to_tgsi.h"
61b8e80941Smrg#include "st_atifs_to_tgsi.h"
62b8e80941Smrg#include "st_nir.h"
63b8e80941Smrg#include "st_shader_cache.h"
64848b8605Smrg#include "cso_cache/cso_context.h"
65848b8605Smrg
66848b8605Smrg
67848b8605Smrg
68b8e80941Smrgstatic void
69b8e80941Smrgset_affected_state_flags(uint64_t *states,
70b8e80941Smrg                         struct gl_program *prog,
71b8e80941Smrg                         uint64_t new_constants,
72b8e80941Smrg                         uint64_t new_sampler_views,
73b8e80941Smrg                         uint64_t new_samplers,
74b8e80941Smrg                         uint64_t new_images,
75b8e80941Smrg                         uint64_t new_ubos,
76b8e80941Smrg                         uint64_t new_ssbos,
77b8e80941Smrg                         uint64_t new_atomics)
78b8e80941Smrg{
79b8e80941Smrg   if (prog->Parameters->NumParameters)
80b8e80941Smrg      *states |= new_constants;
81b8e80941Smrg
82b8e80941Smrg   if (prog->info.num_textures)
83b8e80941Smrg      *states |= new_sampler_views | new_samplers;
84b8e80941Smrg
85b8e80941Smrg   if (prog->info.num_images)
86b8e80941Smrg      *states |= new_images;
87b8e80941Smrg
88b8e80941Smrg   if (prog->info.num_ubos)
89b8e80941Smrg      *states |= new_ubos;
90b8e80941Smrg
91b8e80941Smrg   if (prog->info.num_ssbos)
92b8e80941Smrg      *states |= new_ssbos;
93b8e80941Smrg
94b8e80941Smrg   if (prog->info.num_abos)
95b8e80941Smrg      *states |= new_atomics;
96b8e80941Smrg}
97b8e80941Smrg
98b8e80941Smrg/**
99b8e80941Smrg * This determines which states will be updated when the shader is bound.
100b8e80941Smrg */
101b8e80941Smrgvoid
102b8e80941Smrgst_set_prog_affected_state_flags(struct gl_program *prog)
103b8e80941Smrg{
104b8e80941Smrg   uint64_t *states;
105b8e80941Smrg
106b8e80941Smrg   switch (prog->info.stage) {
107b8e80941Smrg   case MESA_SHADER_VERTEX:
108b8e80941Smrg      states = &((struct st_vertex_program*)prog)->affected_states;
109b8e80941Smrg
110b8e80941Smrg      *states = ST_NEW_VS_STATE |
111b8e80941Smrg                ST_NEW_RASTERIZER |
112b8e80941Smrg                ST_NEW_VERTEX_ARRAYS;
113b8e80941Smrg
114b8e80941Smrg      set_affected_state_flags(states, prog,
115b8e80941Smrg                               ST_NEW_VS_CONSTANTS,
116b8e80941Smrg                               ST_NEW_VS_SAMPLER_VIEWS,
117b8e80941Smrg                               ST_NEW_VS_SAMPLERS,
118b8e80941Smrg                               ST_NEW_VS_IMAGES,
119b8e80941Smrg                               ST_NEW_VS_UBOS,
120b8e80941Smrg                               ST_NEW_VS_SSBOS,
121b8e80941Smrg                               ST_NEW_VS_ATOMICS);
122b8e80941Smrg      break;
123b8e80941Smrg
124b8e80941Smrg   case MESA_SHADER_TESS_CTRL:
125b8e80941Smrg      states = &(st_common_program(prog))->affected_states;
126b8e80941Smrg
127b8e80941Smrg      *states = ST_NEW_TCS_STATE;
128b8e80941Smrg
129b8e80941Smrg      set_affected_state_flags(states, prog,
130b8e80941Smrg                               ST_NEW_TCS_CONSTANTS,
131b8e80941Smrg                               ST_NEW_TCS_SAMPLER_VIEWS,
132b8e80941Smrg                               ST_NEW_TCS_SAMPLERS,
133b8e80941Smrg                               ST_NEW_TCS_IMAGES,
134b8e80941Smrg                               ST_NEW_TCS_UBOS,
135b8e80941Smrg                               ST_NEW_TCS_SSBOS,
136b8e80941Smrg                               ST_NEW_TCS_ATOMICS);
137b8e80941Smrg      break;
138b8e80941Smrg
139b8e80941Smrg   case MESA_SHADER_TESS_EVAL:
140b8e80941Smrg      states = &(st_common_program(prog))->affected_states;
141b8e80941Smrg
142b8e80941Smrg      *states = ST_NEW_TES_STATE |
143b8e80941Smrg                ST_NEW_RASTERIZER;
144b8e80941Smrg
145b8e80941Smrg      set_affected_state_flags(states, prog,
146b8e80941Smrg                               ST_NEW_TES_CONSTANTS,
147b8e80941Smrg                               ST_NEW_TES_SAMPLER_VIEWS,
148b8e80941Smrg                               ST_NEW_TES_SAMPLERS,
149b8e80941Smrg                               ST_NEW_TES_IMAGES,
150b8e80941Smrg                               ST_NEW_TES_UBOS,
151b8e80941Smrg                               ST_NEW_TES_SSBOS,
152b8e80941Smrg                               ST_NEW_TES_ATOMICS);
153b8e80941Smrg      break;
154b8e80941Smrg
155b8e80941Smrg   case MESA_SHADER_GEOMETRY:
156b8e80941Smrg      states = &(st_common_program(prog))->affected_states;
157b8e80941Smrg
158b8e80941Smrg      *states = ST_NEW_GS_STATE |
159b8e80941Smrg                ST_NEW_RASTERIZER;
160b8e80941Smrg
161b8e80941Smrg      set_affected_state_flags(states, prog,
162b8e80941Smrg                               ST_NEW_GS_CONSTANTS,
163b8e80941Smrg                               ST_NEW_GS_SAMPLER_VIEWS,
164b8e80941Smrg                               ST_NEW_GS_SAMPLERS,
165b8e80941Smrg                               ST_NEW_GS_IMAGES,
166b8e80941Smrg                               ST_NEW_GS_UBOS,
167b8e80941Smrg                               ST_NEW_GS_SSBOS,
168b8e80941Smrg                               ST_NEW_GS_ATOMICS);
169b8e80941Smrg      break;
170b8e80941Smrg
171b8e80941Smrg   case MESA_SHADER_FRAGMENT:
172b8e80941Smrg      states = &((struct st_fragment_program*)prog)->affected_states;
173b8e80941Smrg
174b8e80941Smrg      /* gl_FragCoord and glDrawPixels always use constants. */
175b8e80941Smrg      *states = ST_NEW_FS_STATE |
176b8e80941Smrg                ST_NEW_SAMPLE_SHADING |
177b8e80941Smrg                ST_NEW_FS_CONSTANTS;
178b8e80941Smrg
179b8e80941Smrg      set_affected_state_flags(states, prog,
180b8e80941Smrg                               ST_NEW_FS_CONSTANTS,
181b8e80941Smrg                               ST_NEW_FS_SAMPLER_VIEWS,
182b8e80941Smrg                               ST_NEW_FS_SAMPLERS,
183b8e80941Smrg                               ST_NEW_FS_IMAGES,
184b8e80941Smrg                               ST_NEW_FS_UBOS,
185b8e80941Smrg                               ST_NEW_FS_SSBOS,
186b8e80941Smrg                               ST_NEW_FS_ATOMICS);
187b8e80941Smrg      break;
188b8e80941Smrg
189b8e80941Smrg   case MESA_SHADER_COMPUTE:
190b8e80941Smrg      states = &((struct st_compute_program*)prog)->affected_states;
191b8e80941Smrg
192b8e80941Smrg      *states = ST_NEW_CS_STATE;
193b8e80941Smrg
194b8e80941Smrg      set_affected_state_flags(states, prog,
195b8e80941Smrg                               ST_NEW_CS_CONSTANTS,
196b8e80941Smrg                               ST_NEW_CS_SAMPLER_VIEWS,
197b8e80941Smrg                               ST_NEW_CS_SAMPLERS,
198b8e80941Smrg                               ST_NEW_CS_IMAGES,
199b8e80941Smrg                               ST_NEW_CS_UBOS,
200b8e80941Smrg                               ST_NEW_CS_SSBOS,
201b8e80941Smrg                               ST_NEW_CS_ATOMICS);
202b8e80941Smrg      break;
203b8e80941Smrg
204b8e80941Smrg   default:
205b8e80941Smrg      unreachable("unhandled shader stage");
206b8e80941Smrg   }
207b8e80941Smrg}
208b8e80941Smrg
209b8e80941Smrgstatic void
210b8e80941Smrgdelete_ir(struct pipe_shader_state *ir)
211b8e80941Smrg{
212b8e80941Smrg   if (ir->tokens)
213b8e80941Smrg      ureg_free_tokens(ir->tokens);
214b8e80941Smrg
215b8e80941Smrg   /* Note: Any setup of ->ir.nir that has had pipe->create_*_state called on
216b8e80941Smrg    * it has resulted in the driver taking ownership of the NIR.  Those
217b8e80941Smrg    * callers should be NULLing out the nir field in any pipe_shader_state
218b8e80941Smrg    * that might have this called in order to indicate that.
219b8e80941Smrg    *
220b8e80941Smrg    * GLSL IR and ARB programs will have set gl_program->nir to the same
221b8e80941Smrg    * shader as ir->ir.nir, so it will be freed by _mesa_delete_program().
222b8e80941Smrg    */
223b8e80941Smrg}
224b8e80941Smrg
225848b8605Smrg/**
226848b8605Smrg * Delete a vertex program variant.  Note the caller must unlink
227848b8605Smrg * the variant from the linked list.
228848b8605Smrg */
229848b8605Smrgstatic void
230848b8605Smrgdelete_vp_variant(struct st_context *st, struct st_vp_variant *vpv)
231848b8605Smrg{
232b8e80941Smrg   if (vpv->driver_shader) {
233b8e80941Smrg      if (st->has_shareable_shaders || vpv->key.st == st) {
234b8e80941Smrg         cso_delete_vertex_shader(st->cso_context, vpv->driver_shader);
235b8e80941Smrg      } else {
236b8e80941Smrg         st_save_zombie_shader(vpv->key.st, PIPE_SHADER_VERTEX,
237b8e80941Smrg                               vpv->driver_shader);
238b8e80941Smrg      }
239b8e80941Smrg   }
240b8e80941Smrg
241848b8605Smrg   if (vpv->draw_shader)
242848b8605Smrg      draw_delete_vertex_shader( st->draw, vpv->draw_shader );
243b8e80941Smrg
244b8e80941Smrg   delete_ir(&vpv->tgsi);
245b8e80941Smrg
246848b8605Smrg   free( vpv );
247848b8605Smrg}
248848b8605Smrg
249848b8605Smrg
250848b8605Smrg
251848b8605Smrg/**
252848b8605Smrg * Clean out any old compilations:
253848b8605Smrg */
254848b8605Smrgvoid
255848b8605Smrgst_release_vp_variants( struct st_context *st,
256848b8605Smrg                        struct st_vertex_program *stvp )
257848b8605Smrg{
258848b8605Smrg   struct st_vp_variant *vpv;
259848b8605Smrg
260848b8605Smrg   for (vpv = stvp->variants; vpv; ) {
261848b8605Smrg      struct st_vp_variant *next = vpv->next;
262848b8605Smrg      delete_vp_variant(st, vpv);
263848b8605Smrg      vpv = next;
264848b8605Smrg   }
265848b8605Smrg
266848b8605Smrg   stvp->variants = NULL;
267b8e80941Smrg
268b8e80941Smrg   delete_ir(&stvp->tgsi);
269848b8605Smrg}
270848b8605Smrg
271848b8605Smrg
272848b8605Smrg
273848b8605Smrg/**
274848b8605Smrg * Delete a fragment program variant.  Note the caller must unlink
275848b8605Smrg * the variant from the linked list.
276848b8605Smrg */
277848b8605Smrgstatic void
278848b8605Smrgdelete_fp_variant(struct st_context *st, struct st_fp_variant *fpv)
279848b8605Smrg{
280b8e80941Smrg   if (fpv->driver_shader) {
281b8e80941Smrg      if (st->has_shareable_shaders || fpv->key.st == st) {
282b8e80941Smrg         cso_delete_fragment_shader(st->cso_context, fpv->driver_shader);
283b8e80941Smrg      } else {
284b8e80941Smrg         st_save_zombie_shader(fpv->key.st, PIPE_SHADER_FRAGMENT,
285b8e80941Smrg                               fpv->driver_shader);
286b8e80941Smrg      }
287b8e80941Smrg   }
288b8e80941Smrg
289848b8605Smrg   free(fpv);
290848b8605Smrg}
291848b8605Smrg
292848b8605Smrg
293848b8605Smrg/**
294848b8605Smrg * Free all variants of a fragment program.
295848b8605Smrg */
296848b8605Smrgvoid
297848b8605Smrgst_release_fp_variants(struct st_context *st, struct st_fragment_program *stfp)
298848b8605Smrg{
299848b8605Smrg   struct st_fp_variant *fpv;
300848b8605Smrg
301848b8605Smrg   for (fpv = stfp->variants; fpv; ) {
302848b8605Smrg      struct st_fp_variant *next = fpv->next;
303848b8605Smrg      delete_fp_variant(st, fpv);
304848b8605Smrg      fpv = next;
305848b8605Smrg   }
306848b8605Smrg
307848b8605Smrg   stfp->variants = NULL;
308b8e80941Smrg
309b8e80941Smrg   delete_ir(&stfp->tgsi);
310848b8605Smrg}
311848b8605Smrg
312848b8605Smrg
313848b8605Smrg/**
314b8e80941Smrg * Delete a basic program variant.  Note the caller must unlink
315848b8605Smrg * the variant from the linked list.
316848b8605Smrg */
317848b8605Smrgstatic void
318b8e80941Smrgdelete_basic_variant(struct st_context *st, struct st_basic_variant *v,
319b8e80941Smrg                     GLenum target)
320b8e80941Smrg{
321b8e80941Smrg   if (v->driver_shader) {
322b8e80941Smrg      if (st->has_shareable_shaders || v->key.st == st) {
323b8e80941Smrg         /* The shader's context matches the calling context, or we
324b8e80941Smrg          * don't care.
325b8e80941Smrg          */
326b8e80941Smrg         switch (target) {
327b8e80941Smrg         case GL_TESS_CONTROL_PROGRAM_NV:
328b8e80941Smrg            cso_delete_tessctrl_shader(st->cso_context, v->driver_shader);
329b8e80941Smrg            break;
330b8e80941Smrg         case GL_TESS_EVALUATION_PROGRAM_NV:
331b8e80941Smrg            cso_delete_tesseval_shader(st->cso_context, v->driver_shader);
332b8e80941Smrg            break;
333b8e80941Smrg         case GL_GEOMETRY_PROGRAM_NV:
334b8e80941Smrg            cso_delete_geometry_shader(st->cso_context, v->driver_shader);
335b8e80941Smrg            break;
336b8e80941Smrg         case GL_COMPUTE_PROGRAM_NV:
337b8e80941Smrg            cso_delete_compute_shader(st->cso_context, v->driver_shader);
338b8e80941Smrg            break;
339b8e80941Smrg         default:
340b8e80941Smrg            unreachable("bad shader type in delete_basic_variant");
341b8e80941Smrg         }
342b8e80941Smrg      } else {
343b8e80941Smrg         /* We can't delete a shader with a context different from the one
344b8e80941Smrg          * that created it.  Add it to the creating context's zombie list.
345b8e80941Smrg          */
346b8e80941Smrg         enum pipe_shader_type type;
347b8e80941Smrg         switch (target) {
348b8e80941Smrg         case GL_TESS_CONTROL_PROGRAM_NV:
349b8e80941Smrg            type = PIPE_SHADER_TESS_CTRL;
350b8e80941Smrg            break;
351b8e80941Smrg         case GL_TESS_EVALUATION_PROGRAM_NV:
352b8e80941Smrg            type = PIPE_SHADER_TESS_EVAL;
353b8e80941Smrg            break;
354b8e80941Smrg         case GL_GEOMETRY_PROGRAM_NV:
355b8e80941Smrg            type = PIPE_SHADER_GEOMETRY;
356b8e80941Smrg            break;
357b8e80941Smrg         default:
358b8e80941Smrg            unreachable("");
359b8e80941Smrg         }
360b8e80941Smrg         st_save_zombie_shader(v->key.st, type, v->driver_shader);
361b8e80941Smrg      }
362b8e80941Smrg   }
363b8e80941Smrg
364b8e80941Smrg   free(v);
365b8e80941Smrg}
366b8e80941Smrg
367b8e80941Smrg
368b8e80941Smrg/**
369b8e80941Smrg * Free all basic program variants.
370b8e80941Smrg */
371b8e80941Smrgvoid
372b8e80941Smrgst_release_basic_variants(struct st_context *st, GLenum target,
373b8e80941Smrg                          struct st_basic_variant **variants,
374b8e80941Smrg                          struct pipe_shader_state *tgsi)
375848b8605Smrg{
376b8e80941Smrg   struct st_basic_variant *v;
377b8e80941Smrg
378b8e80941Smrg   for (v = *variants; v; ) {
379b8e80941Smrg      struct st_basic_variant *next = v->next;
380b8e80941Smrg      delete_basic_variant(st, v, target);
381b8e80941Smrg      v = next;
382b8e80941Smrg   }
383b8e80941Smrg
384b8e80941Smrg   *variants = NULL;
385b8e80941Smrg
386b8e80941Smrg   delete_ir(tgsi);
387848b8605Smrg}
388848b8605Smrg
389848b8605Smrg
390848b8605Smrg/**
391b8e80941Smrg * Free all variants of a compute program.
392848b8605Smrg */
393848b8605Smrgvoid
394b8e80941Smrgst_release_cp_variants(struct st_context *st, struct st_compute_program *stcp)
395848b8605Smrg{
396b8e80941Smrg   struct st_basic_variant **variants = &stcp->variants;
397b8e80941Smrg   struct st_basic_variant *v;
398848b8605Smrg
399b8e80941Smrg   for (v = *variants; v; ) {
400b8e80941Smrg      struct st_basic_variant *next = v->next;
401b8e80941Smrg      delete_basic_variant(st, v, stcp->Base.Target);
402b8e80941Smrg      v = next;
403848b8605Smrg   }
404848b8605Smrg
405b8e80941Smrg   *variants = NULL;
406b8e80941Smrg
407b8e80941Smrg   if (stcp->tgsi.prog) {
408b8e80941Smrg      switch (stcp->tgsi.ir_type) {
409b8e80941Smrg      case PIPE_SHADER_IR_TGSI:
410b8e80941Smrg         ureg_free_tokens(stcp->tgsi.prog);
411b8e80941Smrg         stcp->tgsi.prog = NULL;
412b8e80941Smrg         break;
413b8e80941Smrg      case PIPE_SHADER_IR_NIR:
414b8e80941Smrg         /* pipe driver took ownership of prog */
415b8e80941Smrg         break;
416b8e80941Smrg      case PIPE_SHADER_IR_NATIVE:
417b8e80941Smrg         /* ??? */
418b8e80941Smrg         stcp->tgsi.prog = NULL;
419b8e80941Smrg         break;
420b8e80941Smrg      }
421b8e80941Smrg   }
422848b8605Smrg}
423848b8605Smrg
424b8e80941Smrg/**
425b8e80941Smrg * Translate ARB (asm) program to NIR
426b8e80941Smrg */
427b8e80941Smrgstatic nir_shader *
428b8e80941Smrgst_translate_prog_to_nir(struct st_context *st, struct gl_program *prog,
429b8e80941Smrg                         gl_shader_stage stage)
430b8e80941Smrg{
431b8e80941Smrg   enum pipe_shader_type p_stage = pipe_shader_type_from_mesa(stage);
432b8e80941Smrg   const bool is_scalar =
433b8e80941Smrg      st->pipe->screen->get_shader_param(st->pipe->screen, p_stage,
434b8e80941Smrg                                         PIPE_SHADER_CAP_SCALAR_ISA);
435b8e80941Smrg
436b8e80941Smrg   const struct gl_shader_compiler_options *options =
437b8e80941Smrg      &st->ctx->Const.ShaderCompilerOptions[stage];
438848b8605Smrg
439b8e80941Smrg   /* Translate to NIR */
440b8e80941Smrg   nir_shader *nir = prog_to_nir(prog, options->NirOptions);
441b8e80941Smrg   NIR_PASS_V(nir, nir_lower_regs_to_ssa); /* turn registers into SSA */
442b8e80941Smrg   nir_validate_shader(nir, "after st/ptn lower_regs_to_ssa");
443848b8605Smrg
444b8e80941Smrg   NIR_PASS_V(nir, st_nir_lower_wpos_ytransform, prog, st->pipe->screen);
445b8e80941Smrg   NIR_PASS_V(nir, nir_lower_system_values);
446b8e80941Smrg
447b8e80941Smrg   /* Optimise NIR */
448b8e80941Smrg   NIR_PASS_V(nir, nir_opt_constant_folding);
449b8e80941Smrg   st_nir_opts(nir, is_scalar);
450b8e80941Smrg   nir_validate_shader(nir, "after st/ptn NIR opts");
451b8e80941Smrg
452b8e80941Smrg   return nir;
453b8e80941Smrg}
454848b8605Smrg
455848b8605Smrg/**
456b8e80941Smrg * Translate a vertex program.
457848b8605Smrg */
458b8e80941Smrgbool
459b8e80941Smrgst_translate_vertex_program(struct st_context *st,
460848b8605Smrg                            struct st_vertex_program *stvp)
461848b8605Smrg{
462b8e80941Smrg   struct ureg_program *ureg;
463b8e80941Smrg   enum pipe_error error;
464b8e80941Smrg   unsigned num_outputs = 0;
465b8e80941Smrg   unsigned attr;
466b8e80941Smrg   ubyte output_semantic_name[VARYING_SLOT_MAX] = {0};
467b8e80941Smrg   ubyte output_semantic_index[VARYING_SLOT_MAX] = {0};
468848b8605Smrg
469848b8605Smrg   stvp->num_inputs = 0;
470b8e80941Smrg   memset(stvp->input_to_index, ~0, sizeof(stvp->input_to_index));
471848b8605Smrg
472b8e80941Smrg   if (stvp->Base.arb.IsPositionInvariant)
473b8e80941Smrg      _mesa_insert_mvp_code(st->ctx, &stvp->Base);
474848b8605Smrg
475848b8605Smrg   /*
476848b8605Smrg    * Determine number of inputs, the mappings between VERT_ATTRIB_x
477848b8605Smrg    * and TGSI generic input indexes, plus input attrib semantic info.
478848b8605Smrg    */
479848b8605Smrg   for (attr = 0; attr < VERT_ATTRIB_MAX; attr++) {
480b8e80941Smrg      if ((stvp->Base.info.inputs_read & BITFIELD64_BIT(attr)) != 0) {
481848b8605Smrg         stvp->input_to_index[attr] = stvp->num_inputs;
482848b8605Smrg         stvp->index_to_input[stvp->num_inputs] = attr;
483848b8605Smrg         stvp->num_inputs++;
484b8e80941Smrg         if ((stvp->Base.DualSlotInputs & BITFIELD64_BIT(attr)) != 0) {
485b8e80941Smrg            /* add placeholder for second part of a double attribute */
486b8e80941Smrg            stvp->index_to_input[stvp->num_inputs] = ST_DOUBLE_ATTRIB_PLACEHOLDER;
487b8e80941Smrg            stvp->num_inputs++;
488b8e80941Smrg         }
489848b8605Smrg      }
490848b8605Smrg   }
491848b8605Smrg   /* bit of a hack, presetup potentially unused edgeflag input */
492848b8605Smrg   stvp->input_to_index[VERT_ATTRIB_EDGEFLAG] = stvp->num_inputs;
493848b8605Smrg   stvp->index_to_input[stvp->num_inputs] = VERT_ATTRIB_EDGEFLAG;
494848b8605Smrg
495848b8605Smrg   /* Compute mapping of vertex program outputs to slots.
496848b8605Smrg    */
497848b8605Smrg   for (attr = 0; attr < VARYING_SLOT_MAX; attr++) {
498b8e80941Smrg      if ((stvp->Base.info.outputs_written & BITFIELD64_BIT(attr)) == 0) {
499848b8605Smrg         stvp->result_to_output[attr] = ~0;
500848b8605Smrg      }
501848b8605Smrg      else {
502b8e80941Smrg         unsigned slot = num_outputs++;
503848b8605Smrg
504848b8605Smrg         stvp->result_to_output[attr] = slot;
505848b8605Smrg
506b8e80941Smrg         unsigned semantic_name, semantic_index;
507b8e80941Smrg         tgsi_get_gl_varying_semantic(attr, st->needs_texcoord_semantic,
508b8e80941Smrg                                      &semantic_name, &semantic_index);
509b8e80941Smrg         output_semantic_name[slot] = semantic_name;
510b8e80941Smrg         output_semantic_index[slot] = semantic_index;
511848b8605Smrg      }
512848b8605Smrg   }
513848b8605Smrg   /* similar hack to above, presetup potentially unused edgeflag output */
514b8e80941Smrg   stvp->result_to_output[VARYING_SLOT_EDGE] = num_outputs;
515b8e80941Smrg   output_semantic_name[num_outputs] = TGSI_SEMANTIC_EDGEFLAG;
516b8e80941Smrg   output_semantic_index[num_outputs] = 0;
517848b8605Smrg
518b8e80941Smrg   /* ARB_vp: */
519b8e80941Smrg   if (!stvp->glsl_to_tgsi && !stvp->shader_program) {
520b8e80941Smrg      _mesa_remove_output_reads(&stvp->Base, PROGRAM_OUTPUT);
521848b8605Smrg
522b8e80941Smrg      /* This determines which states will be updated when the assembly
523b8e80941Smrg       * shader is bound.
524b8e80941Smrg       */
525b8e80941Smrg      stvp->affected_states = ST_NEW_VS_STATE |
526b8e80941Smrg                              ST_NEW_RASTERIZER |
527b8e80941Smrg                              ST_NEW_VERTEX_ARRAYS;
528848b8605Smrg
529b8e80941Smrg      if (stvp->Base.Parameters->NumParameters)
530b8e80941Smrg         stvp->affected_states |= ST_NEW_VS_CONSTANTS;
531848b8605Smrg
532b8e80941Smrg      /* No samplers are allowed in ARB_vp. */
533848b8605Smrg   }
534848b8605Smrg
535b8e80941Smrg   if (stvp->shader_program) {
536b8e80941Smrg      st_translate_stream_output_info(stvp->Base.sh.LinkedTransformFeedback,
537b8e80941Smrg                                      stvp->result_to_output,
538b8e80941Smrg                                      &stvp->tgsi.stream_output);
539b8e80941Smrg
540b8e80941Smrg      st_store_ir_in_disk_cache(st, &stvp->Base, true);
541b8e80941Smrg      return true;
542848b8605Smrg   }
543848b8605Smrg
544b8e80941Smrg   ureg = ureg_create_with_screen(PIPE_SHADER_VERTEX, st->pipe->screen);
545b8e80941Smrg   if (ureg == NULL)
546b8e80941Smrg      return false;
547848b8605Smrg
548b8e80941Smrg   if (stvp->Base.info.clip_distance_array_size)
549b8e80941Smrg      ureg_property(ureg, TGSI_PROPERTY_NUM_CLIPDIST_ENABLED,
550b8e80941Smrg                    stvp->Base.info.clip_distance_array_size);
551b8e80941Smrg   if (stvp->Base.info.cull_distance_array_size)
552b8e80941Smrg      ureg_property(ureg, TGSI_PROPERTY_NUM_CULLDIST_ENABLED,
553b8e80941Smrg                    stvp->Base.info.cull_distance_array_size);
554848b8605Smrg
555848b8605Smrg   if (ST_DEBUG & DEBUG_MESA) {
556b8e80941Smrg      _mesa_print_program(&stvp->Base);
557b8e80941Smrg      _mesa_print_program_parameters(st->ctx, &stvp->Base);
558848b8605Smrg      debug_printf("\n");
559848b8605Smrg   }
560848b8605Smrg
561b8e80941Smrg   if (stvp->glsl_to_tgsi) {
562848b8605Smrg      error = st_translate_program(st->ctx,
563b8e80941Smrg                                   PIPE_SHADER_VERTEX,
564848b8605Smrg                                   ureg,
565848b8605Smrg                                   stvp->glsl_to_tgsi,
566b8e80941Smrg                                   &stvp->Base,
567848b8605Smrg                                   /* inputs */
568b8e80941Smrg                                   stvp->num_inputs,
569848b8605Smrg                                   stvp->input_to_index,
570b8e80941Smrg                                   NULL, /* inputSlotToAttr */
571848b8605Smrg                                   NULL, /* input semantic name */
572848b8605Smrg                                   NULL, /* input semantic index */
573848b8605Smrg                                   NULL, /* interp mode */
574848b8605Smrg                                   /* outputs */
575848b8605Smrg                                   num_outputs,
576848b8605Smrg                                   stvp->result_to_output,
577b8e80941Smrg                                   output_semantic_name,
578b8e80941Smrg                                   output_semantic_index);
579b8e80941Smrg
580b8e80941Smrg      st_translate_stream_output_info(stvp->Base.sh.LinkedTransformFeedback,
581b8e80941Smrg                                      stvp->result_to_output,
582b8e80941Smrg                                      &stvp->tgsi.stream_output);
583b8e80941Smrg
584b8e80941Smrg      free_glsl_to_tgsi_visitor(stvp->glsl_to_tgsi);
585b8e80941Smrg   } else
586848b8605Smrg      error = st_translate_mesa_program(st->ctx,
587b8e80941Smrg                                        PIPE_SHADER_VERTEX,
588848b8605Smrg                                        ureg,
589b8e80941Smrg                                        &stvp->Base,
590848b8605Smrg                                        /* inputs */
591b8e80941Smrg                                        stvp->num_inputs,
592848b8605Smrg                                        stvp->input_to_index,
593848b8605Smrg                                        NULL, /* input semantic name */
594848b8605Smrg                                        NULL, /* input semantic index */
595848b8605Smrg                                        NULL,
596848b8605Smrg                                        /* outputs */
597848b8605Smrg                                        num_outputs,
598848b8605Smrg                                        stvp->result_to_output,
599b8e80941Smrg                                        output_semantic_name,
600b8e80941Smrg                                        output_semantic_index);
601b8e80941Smrg
602b8e80941Smrg   if (error) {
603b8e80941Smrg      debug_printf("%s: failed to translate Mesa program:\n", __func__);
604b8e80941Smrg      _mesa_print_program(&stvp->Base);
605b8e80941Smrg      debug_assert(0);
606b8e80941Smrg      return false;
607b8e80941Smrg   }
608b8e80941Smrg
609b8e80941Smrg   stvp->tgsi.tokens = ureg_get_tokens(ureg, &stvp->num_tgsi_tokens);
610b8e80941Smrg   ureg_destroy(ureg);
611b8e80941Smrg
612b8e80941Smrg   if (stvp->glsl_to_tgsi) {
613b8e80941Smrg      stvp->glsl_to_tgsi = NULL;
614b8e80941Smrg      st_store_ir_in_disk_cache(st, &stvp->Base, false);
615b8e80941Smrg   }
616848b8605Smrg
617b8e80941Smrg   bool use_nir = PIPE_SHADER_IR_NIR ==
618b8e80941Smrg      st->pipe->screen->get_shader_param(st->pipe->screen, PIPE_SHADER_VERTEX,
619b8e80941Smrg                                         PIPE_SHADER_CAP_PREFERRED_IR);
620848b8605Smrg
621b8e80941Smrg   if (use_nir) {
622b8e80941Smrg      nir_shader *nir =
623b8e80941Smrg         st_translate_prog_to_nir(st, &stvp->Base, MESA_SHADER_VERTEX);
624848b8605Smrg
625b8e80941Smrg      if (stvp->tgsi.ir.nir)
626b8e80941Smrg         ralloc_free(stvp->tgsi.ir.nir);
627b8e80941Smrg      stvp->tgsi.type = PIPE_SHADER_IR_NIR;
628b8e80941Smrg      stvp->tgsi.ir.nir = nir;
629b8e80941Smrg      stvp->Base.nir = nir;
630b8e80941Smrg      return true;
631b8e80941Smrg   }
632848b8605Smrg
633b8e80941Smrg   return stvp->tgsi.tokens != NULL;
634b8e80941Smrg}
635b8e80941Smrg
636b8e80941Smrgstatic struct st_vp_variant *
637b8e80941Smrgst_create_vp_variant(struct st_context *st,
638b8e80941Smrg                     struct st_vertex_program *stvp,
639b8e80941Smrg                     const struct st_vp_variant_key *key)
640b8e80941Smrg{
641b8e80941Smrg   struct st_vp_variant *vpv = CALLOC_STRUCT(st_vp_variant);
642b8e80941Smrg   struct pipe_context *pipe = st->pipe;
643b8e80941Smrg
644b8e80941Smrg   vpv->key = *key;
645b8e80941Smrg   vpv->tgsi.stream_output = stvp->tgsi.stream_output;
646b8e80941Smrg   vpv->num_inputs = stvp->num_inputs;
647b8e80941Smrg
648b8e80941Smrg   /* When generating a NIR program, we usually don't have TGSI tokens.
649b8e80941Smrg    * However, we do create them for ARB_vertex_program / fixed-function VS
650b8e80941Smrg    * programs which we may need to use with the draw module for legacy
651b8e80941Smrg    * feedback/select emulation.  If they exist, copy them.
652b8e80941Smrg    */
653b8e80941Smrg   if (stvp->tgsi.tokens)
654b8e80941Smrg      vpv->tgsi.tokens = tgsi_dup_tokens(stvp->tgsi.tokens);
655b8e80941Smrg
656b8e80941Smrg   if (stvp->tgsi.type == PIPE_SHADER_IR_NIR) {
657b8e80941Smrg      vpv->tgsi.type = PIPE_SHADER_IR_NIR;
658b8e80941Smrg      vpv->tgsi.ir.nir = nir_shader_clone(NULL, stvp->tgsi.ir.nir);
659b8e80941Smrg      if (key->clamp_color)
660b8e80941Smrg         NIR_PASS_V(vpv->tgsi.ir.nir, nir_lower_clamp_color_outputs);
661b8e80941Smrg      if (key->passthrough_edgeflags) {
662b8e80941Smrg         NIR_PASS_V(vpv->tgsi.ir.nir, nir_lower_passthrough_edgeflags);
663b8e80941Smrg         vpv->num_inputs++;
664b8e80941Smrg      }
665b8e80941Smrg
666b8e80941Smrg      st_finalize_nir(st, &stvp->Base, stvp->shader_program,
667b8e80941Smrg                      vpv->tgsi.ir.nir);
668b8e80941Smrg
669b8e80941Smrg      vpv->driver_shader = pipe->create_vs_state(pipe, &vpv->tgsi);
670b8e80941Smrg      /* driver takes ownership of IR: */
671b8e80941Smrg      vpv->tgsi.ir.nir = NULL;
672b8e80941Smrg      return vpv;
673b8e80941Smrg   }
674b8e80941Smrg
675b8e80941Smrg   /* Emulate features. */
676b8e80941Smrg   if (key->clamp_color || key->passthrough_edgeflags) {
677b8e80941Smrg      const struct tgsi_token *tokens;
678b8e80941Smrg      unsigned flags =
679b8e80941Smrg         (key->clamp_color ? TGSI_EMU_CLAMP_COLOR_OUTPUTS : 0) |
680b8e80941Smrg         (key->passthrough_edgeflags ? TGSI_EMU_PASSTHROUGH_EDGEFLAG : 0);
681b8e80941Smrg
682b8e80941Smrg      tokens = tgsi_emulate(vpv->tgsi.tokens, flags);
683b8e80941Smrg
684b8e80941Smrg      if (tokens) {
685b8e80941Smrg         tgsi_free_tokens(vpv->tgsi.tokens);
686b8e80941Smrg         vpv->tgsi.tokens = tokens;
687b8e80941Smrg
688b8e80941Smrg         if (key->passthrough_edgeflags)
689b8e80941Smrg            vpv->num_inputs++;
690b8e80941Smrg      } else
691b8e80941Smrg         fprintf(stderr, "mesa: cannot emulate deprecated features\n");
692848b8605Smrg   }
693848b8605Smrg
694848b8605Smrg   if (ST_DEBUG & DEBUG_TGSI) {
695848b8605Smrg      tgsi_dump(vpv->tgsi.tokens, 0);
696848b8605Smrg      debug_printf("\n");
697848b8605Smrg   }
698848b8605Smrg
699848b8605Smrg   vpv->driver_shader = pipe->create_vs_state(pipe, &vpv->tgsi);
700848b8605Smrg   return vpv;
701848b8605Smrg}
702848b8605Smrg
703848b8605Smrg
704848b8605Smrg/**
705848b8605Smrg * Find/create a vertex program variant.
706848b8605Smrg */
707848b8605Smrgstruct st_vp_variant *
708848b8605Smrgst_get_vp_variant(struct st_context *st,
709848b8605Smrg                  struct st_vertex_program *stvp,
710848b8605Smrg                  const struct st_vp_variant_key *key)
711848b8605Smrg{
712848b8605Smrg   struct st_vp_variant *vpv;
713848b8605Smrg
714848b8605Smrg   /* Search for existing variant */
715848b8605Smrg   for (vpv = stvp->variants; vpv; vpv = vpv->next) {
716848b8605Smrg      if (memcmp(&vpv->key, key, sizeof(*key)) == 0) {
717848b8605Smrg         break;
718848b8605Smrg      }
719848b8605Smrg   }
720848b8605Smrg
721848b8605Smrg   if (!vpv) {
722848b8605Smrg      /* create now */
723b8e80941Smrg      vpv = st_create_vp_variant(st, stvp, key);
724848b8605Smrg      if (vpv) {
725b8e80941Smrg          for (unsigned index = 0; index < vpv->num_inputs; ++index) {
726b8e80941Smrg             unsigned attr = stvp->index_to_input[index];
727b8e80941Smrg             if (attr == ST_DOUBLE_ATTRIB_PLACEHOLDER)
728b8e80941Smrg                continue;
729b8e80941Smrg             vpv->vert_attrib_mask |= 1u << attr;
730b8e80941Smrg          }
731b8e80941Smrg
732848b8605Smrg         /* insert into list */
733848b8605Smrg         vpv->next = stvp->variants;
734848b8605Smrg         stvp->variants = vpv;
735848b8605Smrg      }
736848b8605Smrg   }
737848b8605Smrg
738848b8605Smrg   return vpv;
739848b8605Smrg}
740848b8605Smrg
741848b8605Smrg
742848b8605Smrg/**
743b8e80941Smrg * Translate a Mesa fragment shader into a TGSI shader.
744848b8605Smrg */
745b8e80941Smrgbool
746848b8605Smrgst_translate_fragment_program(struct st_context *st,
747b8e80941Smrg                              struct st_fragment_program *stfp)
748848b8605Smrg{
749b8e80941Smrg   /* We have already compiled to NIR so just return */
750b8e80941Smrg   if (stfp->shader_program) {
751b8e80941Smrg      st_store_ir_in_disk_cache(st, &stfp->Base, true);
752b8e80941Smrg      return true;
753b8e80941Smrg   }
754848b8605Smrg
755b8e80941Smrg   ubyte outputMapping[2 * FRAG_RESULT_MAX];
756b8e80941Smrg   ubyte inputMapping[VARYING_SLOT_MAX];
757b8e80941Smrg   ubyte inputSlotToAttr[VARYING_SLOT_MAX];
758b8e80941Smrg   ubyte interpMode[PIPE_MAX_SHADER_INPUTS];  /* XXX size? */
759848b8605Smrg   GLuint attr;
760848b8605Smrg   GLbitfield64 inputsRead;
761848b8605Smrg   struct ureg_program *ureg;
762848b8605Smrg
763848b8605Smrg   GLboolean write_all = GL_FALSE;
764848b8605Smrg
765848b8605Smrg   ubyte input_semantic_name[PIPE_MAX_SHADER_INPUTS];
766848b8605Smrg   ubyte input_semantic_index[PIPE_MAX_SHADER_INPUTS];
767848b8605Smrg   uint fs_num_inputs = 0;
768848b8605Smrg
769848b8605Smrg   ubyte fs_output_semantic_name[PIPE_MAX_SHADER_OUTPUTS];
770848b8605Smrg   ubyte fs_output_semantic_index[PIPE_MAX_SHADER_OUTPUTS];
771848b8605Smrg   uint fs_num_outputs = 0;
772848b8605Smrg
773b8e80941Smrg   memset(inputSlotToAttr, ~0, sizeof(inputSlotToAttr));
774b8e80941Smrg
775b8e80941Smrg   /* Non-GLSL programs: */
776b8e80941Smrg   if (!stfp->glsl_to_tgsi && !stfp->shader_program) {
777b8e80941Smrg      _mesa_remove_output_reads(&stfp->Base, PROGRAM_OUTPUT);
778b8e80941Smrg      if (st->ctx->Const.GLSLFragCoordIsSysVal)
779b8e80941Smrg         _mesa_program_fragment_position_to_sysval(&stfp->Base);
780b8e80941Smrg
781b8e80941Smrg      /* This determines which states will be updated when the assembly
782b8e80941Smrg       * shader is bound.
783b8e80941Smrg       *
784b8e80941Smrg       * fragment.position and glDrawPixels always use constants.
785b8e80941Smrg       */
786b8e80941Smrg      stfp->affected_states = ST_NEW_FS_STATE |
787b8e80941Smrg                              ST_NEW_SAMPLE_SHADING |
788b8e80941Smrg                              ST_NEW_FS_CONSTANTS;
789b8e80941Smrg
790b8e80941Smrg      if (stfp->ati_fs) {
791b8e80941Smrg         /* Just set them for ATI_fs unconditionally. */
792b8e80941Smrg         stfp->affected_states |= ST_NEW_FS_SAMPLER_VIEWS |
793b8e80941Smrg                                  ST_NEW_FS_SAMPLERS;
794b8e80941Smrg      } else {
795b8e80941Smrg         /* ARB_fp */
796b8e80941Smrg         if (stfp->Base.SamplersUsed)
797b8e80941Smrg            stfp->affected_states |= ST_NEW_FS_SAMPLER_VIEWS |
798b8e80941Smrg                                     ST_NEW_FS_SAMPLERS;
799b8e80941Smrg      }
800b8e80941Smrg   }
801848b8605Smrg
802848b8605Smrg
803b8e80941Smrg   bool use_nir = PIPE_SHADER_IR_NIR ==
804b8e80941Smrg      st->pipe->screen->get_shader_param(st->pipe->screen,
805b8e80941Smrg                                         PIPE_SHADER_FRAGMENT,
806b8e80941Smrg                                         PIPE_SHADER_CAP_PREFERRED_IR);
807848b8605Smrg
808b8e80941Smrg   if (use_nir && !stfp->ati_fs) {
809b8e80941Smrg      nir_shader *nir =
810b8e80941Smrg         st_translate_prog_to_nir(st, &stfp->Base, MESA_SHADER_FRAGMENT);
811848b8605Smrg
812b8e80941Smrg      if (stfp->tgsi.ir.nir)
813b8e80941Smrg         ralloc_free(stfp->tgsi.ir.nir);
814b8e80941Smrg      stfp->tgsi.type = PIPE_SHADER_IR_NIR;
815b8e80941Smrg      stfp->tgsi.ir.nir = nir;
816b8e80941Smrg      stfp->Base.nir = nir;
817b8e80941Smrg      return true;
818848b8605Smrg   }
819848b8605Smrg
820848b8605Smrg   /*
821848b8605Smrg    * Convert Mesa program inputs to TGSI input register semantics.
822848b8605Smrg    */
823b8e80941Smrg   inputsRead = stfp->Base.info.inputs_read;
824848b8605Smrg   for (attr = 0; attr < VARYING_SLOT_MAX; attr++) {
825848b8605Smrg      if ((inputsRead & BITFIELD64_BIT(attr)) != 0) {
826848b8605Smrg         const GLuint slot = fs_num_inputs++;
827848b8605Smrg
828848b8605Smrg         inputMapping[attr] = slot;
829b8e80941Smrg         inputSlotToAttr[slot] = attr;
830848b8605Smrg
831848b8605Smrg         switch (attr) {
832848b8605Smrg         case VARYING_SLOT_POS:
833848b8605Smrg            input_semantic_name[slot] = TGSI_SEMANTIC_POSITION;
834848b8605Smrg            input_semantic_index[slot] = 0;
835848b8605Smrg            interpMode[slot] = TGSI_INTERPOLATE_LINEAR;
836848b8605Smrg            break;
837848b8605Smrg         case VARYING_SLOT_COL0:
838848b8605Smrg            input_semantic_name[slot] = TGSI_SEMANTIC_COLOR;
839848b8605Smrg            input_semantic_index[slot] = 0;
840b8e80941Smrg            interpMode[slot] = stfp->glsl_to_tgsi ?
841b8e80941Smrg               TGSI_INTERPOLATE_COUNT : TGSI_INTERPOLATE_COLOR;
842848b8605Smrg            break;
843848b8605Smrg         case VARYING_SLOT_COL1:
844848b8605Smrg            input_semantic_name[slot] = TGSI_SEMANTIC_COLOR;
845848b8605Smrg            input_semantic_index[slot] = 1;
846b8e80941Smrg            interpMode[slot] = stfp->glsl_to_tgsi ?
847b8e80941Smrg               TGSI_INTERPOLATE_COUNT : TGSI_INTERPOLATE_COLOR;
848848b8605Smrg            break;
849848b8605Smrg         case VARYING_SLOT_FOGC:
850848b8605Smrg            input_semantic_name[slot] = TGSI_SEMANTIC_FOG;
851848b8605Smrg            input_semantic_index[slot] = 0;
852848b8605Smrg            interpMode[slot] = TGSI_INTERPOLATE_PERSPECTIVE;
853848b8605Smrg            break;
854848b8605Smrg         case VARYING_SLOT_FACE:
855848b8605Smrg            input_semantic_name[slot] = TGSI_SEMANTIC_FACE;
856848b8605Smrg            input_semantic_index[slot] = 0;
857848b8605Smrg            interpMode[slot] = TGSI_INTERPOLATE_CONSTANT;
858848b8605Smrg            break;
859848b8605Smrg         case VARYING_SLOT_PRIMITIVE_ID:
860848b8605Smrg            input_semantic_name[slot] = TGSI_SEMANTIC_PRIMID;
861848b8605Smrg            input_semantic_index[slot] = 0;
862848b8605Smrg            interpMode[slot] = TGSI_INTERPOLATE_CONSTANT;
863848b8605Smrg            break;
864848b8605Smrg         case VARYING_SLOT_LAYER:
865848b8605Smrg            input_semantic_name[slot] = TGSI_SEMANTIC_LAYER;
866848b8605Smrg            input_semantic_index[slot] = 0;
867848b8605Smrg            interpMode[slot] = TGSI_INTERPOLATE_CONSTANT;
868848b8605Smrg            break;
869848b8605Smrg         case VARYING_SLOT_VIEWPORT:
870848b8605Smrg            input_semantic_name[slot] = TGSI_SEMANTIC_VIEWPORT_INDEX;
871848b8605Smrg            input_semantic_index[slot] = 0;
872848b8605Smrg            interpMode[slot] = TGSI_INTERPOLATE_CONSTANT;
873848b8605Smrg            break;
874848b8605Smrg         case VARYING_SLOT_CLIP_DIST0:
875848b8605Smrg            input_semantic_name[slot] = TGSI_SEMANTIC_CLIPDIST;
876848b8605Smrg            input_semantic_index[slot] = 0;
877848b8605Smrg            interpMode[slot] = TGSI_INTERPOLATE_PERSPECTIVE;
878848b8605Smrg            break;
879848b8605Smrg         case VARYING_SLOT_CLIP_DIST1:
880848b8605Smrg            input_semantic_name[slot] = TGSI_SEMANTIC_CLIPDIST;
881848b8605Smrg            input_semantic_index[slot] = 1;
882848b8605Smrg            interpMode[slot] = TGSI_INTERPOLATE_PERSPECTIVE;
883848b8605Smrg            break;
884b8e80941Smrg         case VARYING_SLOT_CULL_DIST0:
885b8e80941Smrg         case VARYING_SLOT_CULL_DIST1:
886b8e80941Smrg            /* these should have been lowered by GLSL */
887b8e80941Smrg            assert(0);
888b8e80941Smrg            break;
889848b8605Smrg            /* In most cases, there is nothing special about these
890848b8605Smrg             * inputs, so adopt a convention to use the generic
891848b8605Smrg             * semantic name and the mesa VARYING_SLOT_ number as the
892848b8605Smrg             * index.
893848b8605Smrg             *
894848b8605Smrg             * All that is required is that the vertex shader labels
895848b8605Smrg             * its own outputs similarly, and that the vertex shader
896848b8605Smrg             * generates at least every output required by the
897848b8605Smrg             * fragment shader plus fixed-function hardware (such as
898848b8605Smrg             * BFC).
899848b8605Smrg             *
900848b8605Smrg             * However, some drivers may need us to identify the PNTC and TEXi
901848b8605Smrg             * varyings if, for example, their capability to replace them with
902848b8605Smrg             * sprite coordinates is limited.
903848b8605Smrg             */
904848b8605Smrg         case VARYING_SLOT_PNTC:
905848b8605Smrg            if (st->needs_texcoord_semantic) {
906848b8605Smrg               input_semantic_name[slot] = TGSI_SEMANTIC_PCOORD;
907848b8605Smrg               input_semantic_index[slot] = 0;
908848b8605Smrg               interpMode[slot] = TGSI_INTERPOLATE_LINEAR;
909848b8605Smrg               break;
910848b8605Smrg            }
911848b8605Smrg            /* fall through */
912848b8605Smrg         case VARYING_SLOT_TEX0:
913848b8605Smrg         case VARYING_SLOT_TEX1:
914848b8605Smrg         case VARYING_SLOT_TEX2:
915848b8605Smrg         case VARYING_SLOT_TEX3:
916848b8605Smrg         case VARYING_SLOT_TEX4:
917848b8605Smrg         case VARYING_SLOT_TEX5:
918848b8605Smrg         case VARYING_SLOT_TEX6:
919848b8605Smrg         case VARYING_SLOT_TEX7:
920848b8605Smrg            if (st->needs_texcoord_semantic) {
921848b8605Smrg               input_semantic_name[slot] = TGSI_SEMANTIC_TEXCOORD;
922848b8605Smrg               input_semantic_index[slot] = attr - VARYING_SLOT_TEX0;
923b8e80941Smrg               interpMode[slot] = stfp->glsl_to_tgsi ?
924b8e80941Smrg                  TGSI_INTERPOLATE_COUNT : TGSI_INTERPOLATE_PERSPECTIVE;
925848b8605Smrg               break;
926848b8605Smrg            }
927848b8605Smrg            /* fall through */
928848b8605Smrg         case VARYING_SLOT_VAR0:
929848b8605Smrg         default:
930848b8605Smrg            /* Semantic indices should be zero-based because drivers may choose
931848b8605Smrg             * to assign a fixed slot determined by that index.
932848b8605Smrg             * This is useful because ARB_separate_shader_objects uses location
933848b8605Smrg             * qualifiers for linkage, and if the semantic index corresponds to
934848b8605Smrg             * these locations, linkage passes in the driver become unecessary.
935848b8605Smrg             *
936848b8605Smrg             * If needs_texcoord_semantic is true, no semantic indices will be
937848b8605Smrg             * consumed for the TEXi varyings, and we can base the locations of
938848b8605Smrg             * the user varyings on VAR0.  Otherwise, we use TEX0 as base index.
939848b8605Smrg             */
940b8e80941Smrg            assert(attr >= VARYING_SLOT_VAR0 || attr == VARYING_SLOT_PNTC ||
941b8e80941Smrg                   (attr >= VARYING_SLOT_TEX0 && attr <= VARYING_SLOT_TEX7));
942848b8605Smrg            input_semantic_name[slot] = TGSI_SEMANTIC_GENERIC;
943b8e80941Smrg            input_semantic_index[slot] = st_get_generic_varying_index(st, attr);
944848b8605Smrg            if (attr == VARYING_SLOT_PNTC)
945848b8605Smrg               interpMode[slot] = TGSI_INTERPOLATE_LINEAR;
946b8e80941Smrg            else {
947b8e80941Smrg               interpMode[slot] = stfp->glsl_to_tgsi ?
948b8e80941Smrg                  TGSI_INTERPOLATE_COUNT : TGSI_INTERPOLATE_PERSPECTIVE;
949b8e80941Smrg            }
950848b8605Smrg            break;
951848b8605Smrg         }
952848b8605Smrg      }
953848b8605Smrg      else {
954848b8605Smrg         inputMapping[attr] = -1;
955848b8605Smrg      }
956848b8605Smrg   }
957848b8605Smrg
958848b8605Smrg   /*
959848b8605Smrg    * Semantics and mapping for outputs
960848b8605Smrg    */
961b8e80941Smrg   GLbitfield64 outputsWritten = stfp->Base.info.outputs_written;
962b8e80941Smrg
963b8e80941Smrg   /* if z is written, emit that first */
964b8e80941Smrg   if (outputsWritten & BITFIELD64_BIT(FRAG_RESULT_DEPTH)) {
965b8e80941Smrg      fs_output_semantic_name[fs_num_outputs] = TGSI_SEMANTIC_POSITION;
966b8e80941Smrg      fs_output_semantic_index[fs_num_outputs] = 0;
967b8e80941Smrg      outputMapping[FRAG_RESULT_DEPTH] = fs_num_outputs;
968b8e80941Smrg      fs_num_outputs++;
969b8e80941Smrg      outputsWritten &= ~(1 << FRAG_RESULT_DEPTH);
970b8e80941Smrg   }
971848b8605Smrg
972b8e80941Smrg   if (outputsWritten & BITFIELD64_BIT(FRAG_RESULT_STENCIL)) {
973b8e80941Smrg      fs_output_semantic_name[fs_num_outputs] = TGSI_SEMANTIC_STENCIL;
974b8e80941Smrg      fs_output_semantic_index[fs_num_outputs] = 0;
975b8e80941Smrg      outputMapping[FRAG_RESULT_STENCIL] = fs_num_outputs;
976b8e80941Smrg      fs_num_outputs++;
977b8e80941Smrg      outputsWritten &= ~(1 << FRAG_RESULT_STENCIL);
978b8e80941Smrg   }
979848b8605Smrg
980b8e80941Smrg   if (outputsWritten & BITFIELD64_BIT(FRAG_RESULT_SAMPLE_MASK)) {
981b8e80941Smrg      fs_output_semantic_name[fs_num_outputs] = TGSI_SEMANTIC_SAMPLEMASK;
982b8e80941Smrg      fs_output_semantic_index[fs_num_outputs] = 0;
983b8e80941Smrg      outputMapping[FRAG_RESULT_SAMPLE_MASK] = fs_num_outputs;
984b8e80941Smrg      fs_num_outputs++;
985b8e80941Smrg      outputsWritten &= ~(1 << FRAG_RESULT_SAMPLE_MASK);
986b8e80941Smrg   }
987848b8605Smrg
988b8e80941Smrg   /* handle remaining outputs (color) */
989b8e80941Smrg   for (attr = 0; attr < ARRAY_SIZE(outputMapping); attr++) {
990b8e80941Smrg      const GLbitfield64 written = attr < FRAG_RESULT_MAX ? outputsWritten :
991b8e80941Smrg         stfp->Base.SecondaryOutputsWritten;
992b8e80941Smrg      const unsigned loc = attr % FRAG_RESULT_MAX;
993b8e80941Smrg
994b8e80941Smrg      if (written & BITFIELD64_BIT(loc)) {
995b8e80941Smrg         switch (loc) {
996b8e80941Smrg         case FRAG_RESULT_DEPTH:
997b8e80941Smrg         case FRAG_RESULT_STENCIL:
998b8e80941Smrg         case FRAG_RESULT_SAMPLE_MASK:
999b8e80941Smrg            /* handled above */
1000b8e80941Smrg            assert(0);
1001b8e80941Smrg            break;
1002b8e80941Smrg         case FRAG_RESULT_COLOR:
1003b8e80941Smrg            write_all = GL_TRUE; /* fallthrough */
1004b8e80941Smrg         default: {
1005b8e80941Smrg            int index;
1006b8e80941Smrg            assert(loc == FRAG_RESULT_COLOR ||
1007b8e80941Smrg                   (FRAG_RESULT_DATA0 <= loc && loc < FRAG_RESULT_MAX));
1008b8e80941Smrg
1009b8e80941Smrg            index = (loc == FRAG_RESULT_COLOR) ? 0 : (loc - FRAG_RESULT_DATA0);
1010b8e80941Smrg
1011b8e80941Smrg            if (attr >= FRAG_RESULT_MAX) {
1012b8e80941Smrg               /* Secondary color for dual source blending. */
1013b8e80941Smrg               assert(index == 0);
1014b8e80941Smrg               index++;
1015848b8605Smrg            }
1016848b8605Smrg
1017b8e80941Smrg            fs_output_semantic_name[fs_num_outputs] = TGSI_SEMANTIC_COLOR;
1018b8e80941Smrg            fs_output_semantic_index[fs_num_outputs] = index;
1019b8e80941Smrg            outputMapping[attr] = fs_num_outputs;
1020b8e80941Smrg            break;
1021848b8605Smrg         }
1022b8e80941Smrg         }
1023b8e80941Smrg
1024b8e80941Smrg         fs_num_outputs++;
1025848b8605Smrg      }
1026848b8605Smrg   }
1027848b8605Smrg
1028b8e80941Smrg   ureg = ureg_create_with_screen(PIPE_SHADER_FRAGMENT, st->pipe->screen);
1029b8e80941Smrg   if (ureg == NULL)
1030b8e80941Smrg      return false;
1031848b8605Smrg
1032848b8605Smrg   if (ST_DEBUG & DEBUG_MESA) {
1033b8e80941Smrg      _mesa_print_program(&stfp->Base);
1034b8e80941Smrg      _mesa_print_program_parameters(st->ctx, &stfp->Base);
1035848b8605Smrg      debug_printf("\n");
1036848b8605Smrg   }
1037848b8605Smrg   if (write_all == GL_TRUE)
1038b8e80941Smrg      ureg_property(ureg, TGSI_PROPERTY_FS_COLOR0_WRITES_ALL_CBUFS, 1);
1039848b8605Smrg
1040b8e80941Smrg   if (stfp->Base.info.fs.depth_layout != FRAG_DEPTH_LAYOUT_NONE) {
1041b8e80941Smrg      switch (stfp->Base.info.fs.depth_layout) {
1042848b8605Smrg      case FRAG_DEPTH_LAYOUT_ANY:
1043b8e80941Smrg         ureg_property(ureg, TGSI_PROPERTY_FS_DEPTH_LAYOUT,
1044b8e80941Smrg                       TGSI_FS_DEPTH_LAYOUT_ANY);
1045848b8605Smrg         break;
1046848b8605Smrg      case FRAG_DEPTH_LAYOUT_GREATER:
1047b8e80941Smrg         ureg_property(ureg, TGSI_PROPERTY_FS_DEPTH_LAYOUT,
1048b8e80941Smrg                       TGSI_FS_DEPTH_LAYOUT_GREATER);
1049848b8605Smrg         break;
1050848b8605Smrg      case FRAG_DEPTH_LAYOUT_LESS:
1051b8e80941Smrg         ureg_property(ureg, TGSI_PROPERTY_FS_DEPTH_LAYOUT,
1052b8e80941Smrg                       TGSI_FS_DEPTH_LAYOUT_LESS);
1053848b8605Smrg         break;
1054848b8605Smrg      case FRAG_DEPTH_LAYOUT_UNCHANGED:
1055b8e80941Smrg         ureg_property(ureg, TGSI_PROPERTY_FS_DEPTH_LAYOUT,
1056b8e80941Smrg                       TGSI_FS_DEPTH_LAYOUT_UNCHANGED);
1057848b8605Smrg         break;
1058848b8605Smrg      default:
1059848b8605Smrg         assert(0);
1060848b8605Smrg      }
1061848b8605Smrg   }
1062848b8605Smrg
1063b8e80941Smrg   if (stfp->glsl_to_tgsi) {
1064848b8605Smrg      st_translate_program(st->ctx,
1065b8e80941Smrg                           PIPE_SHADER_FRAGMENT,
1066848b8605Smrg                           ureg,
1067848b8605Smrg                           stfp->glsl_to_tgsi,
1068b8e80941Smrg                           &stfp->Base,
1069848b8605Smrg                           /* inputs */
1070848b8605Smrg                           fs_num_inputs,
1071848b8605Smrg                           inputMapping,
1072b8e80941Smrg                           inputSlotToAttr,
1073848b8605Smrg                           input_semantic_name,
1074848b8605Smrg                           input_semantic_index,
1075848b8605Smrg                           interpMode,
1076848b8605Smrg                           /* outputs */
1077848b8605Smrg                           fs_num_outputs,
1078848b8605Smrg                           outputMapping,
1079848b8605Smrg                           fs_output_semantic_name,
1080b8e80941Smrg                           fs_output_semantic_index);
1081b8e80941Smrg
1082b8e80941Smrg      free_glsl_to_tgsi_visitor(stfp->glsl_to_tgsi);
1083b8e80941Smrg   } else if (stfp->ati_fs)
1084b8e80941Smrg      st_translate_atifs_program(ureg,
1085b8e80941Smrg                                 stfp->ati_fs,
1086b8e80941Smrg                                 &stfp->Base,
1087b8e80941Smrg                                 /* inputs */
1088b8e80941Smrg                                 fs_num_inputs,
1089b8e80941Smrg                                 inputMapping,
1090b8e80941Smrg                                 input_semantic_name,
1091b8e80941Smrg                                 input_semantic_index,
1092b8e80941Smrg                                 interpMode,
1093b8e80941Smrg                                 /* outputs */
1094b8e80941Smrg                                 fs_num_outputs,
1095b8e80941Smrg                                 outputMapping,
1096b8e80941Smrg                                 fs_output_semantic_name,
1097b8e80941Smrg                                 fs_output_semantic_index);
1098848b8605Smrg   else
1099848b8605Smrg      st_translate_mesa_program(st->ctx,
1100b8e80941Smrg                                PIPE_SHADER_FRAGMENT,
1101848b8605Smrg                                ureg,
1102b8e80941Smrg                                &stfp->Base,
1103848b8605Smrg                                /* inputs */
1104848b8605Smrg                                fs_num_inputs,
1105848b8605Smrg                                inputMapping,
1106848b8605Smrg                                input_semantic_name,
1107848b8605Smrg                                input_semantic_index,
1108848b8605Smrg                                interpMode,
1109848b8605Smrg                                /* outputs */
1110848b8605Smrg                                fs_num_outputs,
1111848b8605Smrg                                outputMapping,
1112848b8605Smrg                                fs_output_semantic_name,
1113b8e80941Smrg                                fs_output_semantic_index);
1114848b8605Smrg
1115b8e80941Smrg   stfp->tgsi.tokens = ureg_get_tokens(ureg, &stfp->num_tgsi_tokens);
1116b8e80941Smrg   ureg_destroy(ureg);
1117848b8605Smrg
1118b8e80941Smrg   if (stfp->glsl_to_tgsi) {
1119b8e80941Smrg      stfp->glsl_to_tgsi = NULL;
1120b8e80941Smrg      st_store_ir_in_disk_cache(st, &stfp->Base, false);
1121848b8605Smrg   }
1122848b8605Smrg
1123b8e80941Smrg   return stfp->tgsi.tokens != NULL;
1124b8e80941Smrg}
1125848b8605Smrg
1126b8e80941Smrgstatic struct st_fp_variant *
1127b8e80941Smrgst_create_fp_variant(struct st_context *st,
1128b8e80941Smrg                     struct st_fragment_program *stfp,
1129b8e80941Smrg                     const struct st_fp_variant_key *key)
1130b8e80941Smrg{
1131b8e80941Smrg   struct pipe_context *pipe = st->pipe;
1132b8e80941Smrg   struct st_fp_variant *variant = CALLOC_STRUCT(st_fp_variant);
1133b8e80941Smrg   struct pipe_shader_state tgsi = {0};
1134b8e80941Smrg   struct gl_program_parameter_list *params = stfp->Base.Parameters;
1135b8e80941Smrg   static const gl_state_index16 texcoord_state[STATE_LENGTH] =
1136b8e80941Smrg      { STATE_INTERNAL, STATE_CURRENT_ATTRIB, VERT_ATTRIB_TEX0 };
1137b8e80941Smrg   static const gl_state_index16 scale_state[STATE_LENGTH] =
1138b8e80941Smrg      { STATE_INTERNAL, STATE_PT_SCALE };
1139b8e80941Smrg   static const gl_state_index16 bias_state[STATE_LENGTH] =
1140b8e80941Smrg      { STATE_INTERNAL, STATE_PT_BIAS };
1141848b8605Smrg
1142b8e80941Smrg   if (!variant)
1143b8e80941Smrg      return NULL;
1144848b8605Smrg
1145b8e80941Smrg   if (stfp->tgsi.type == PIPE_SHADER_IR_NIR) {
1146b8e80941Smrg      tgsi.type = PIPE_SHADER_IR_NIR;
1147b8e80941Smrg      tgsi.ir.nir = nir_shader_clone(NULL, stfp->tgsi.ir.nir);
1148848b8605Smrg
1149b8e80941Smrg      if (key->clamp_color)
1150b8e80941Smrg         NIR_PASS_V(tgsi.ir.nir, nir_lower_clamp_color_outputs);
1151b8e80941Smrg
1152b8e80941Smrg      if (key->persample_shading) {
1153b8e80941Smrg          nir_shader *shader = tgsi.ir.nir;
1154b8e80941Smrg          nir_foreach_variable(var, &shader->inputs)
1155b8e80941Smrg             var->data.sample = true;
1156b8e80941Smrg      }
1157b8e80941Smrg
1158b8e80941Smrg      assert(!(key->bitmap && key->drawpixels));
1159b8e80941Smrg
1160b8e80941Smrg      /* glBitmap */
1161b8e80941Smrg      if (key->bitmap) {
1162b8e80941Smrg         nir_lower_bitmap_options options = {0};
1163b8e80941Smrg
1164b8e80941Smrg         variant->bitmap_sampler = ffs(~stfp->Base.SamplersUsed) - 1;
1165b8e80941Smrg         options.sampler = variant->bitmap_sampler;
1166b8e80941Smrg         options.swizzle_xxxx = (st->bitmap.tex_format == PIPE_FORMAT_L8_UNORM);
1167b8e80941Smrg
1168b8e80941Smrg         NIR_PASS_V(tgsi.ir.nir, nir_lower_bitmap, &options);
1169b8e80941Smrg      }
1170b8e80941Smrg
1171b8e80941Smrg      /* glDrawPixels (color only) */
1172b8e80941Smrg      if (key->drawpixels) {
1173b8e80941Smrg         nir_lower_drawpixels_options options = {{0}};
1174b8e80941Smrg         unsigned samplers_used = stfp->Base.SamplersUsed;
1175b8e80941Smrg
1176b8e80941Smrg         /* Find the first unused slot. */
1177b8e80941Smrg         variant->drawpix_sampler = ffs(~samplers_used) - 1;
1178b8e80941Smrg         options.drawpix_sampler = variant->drawpix_sampler;
1179b8e80941Smrg         samplers_used |= (1 << variant->drawpix_sampler);
1180b8e80941Smrg
1181b8e80941Smrg         options.pixel_maps = key->pixelMaps;
1182b8e80941Smrg         if (key->pixelMaps) {
1183b8e80941Smrg            variant->pixelmap_sampler = ffs(~samplers_used) - 1;
1184b8e80941Smrg            options.pixelmap_sampler = variant->pixelmap_sampler;
1185b8e80941Smrg         }
1186b8e80941Smrg
1187b8e80941Smrg         options.scale_and_bias = key->scaleAndBias;
1188b8e80941Smrg         if (key->scaleAndBias) {
1189b8e80941Smrg            _mesa_add_state_reference(params, scale_state);
1190b8e80941Smrg            memcpy(options.scale_state_tokens, scale_state,
1191b8e80941Smrg                   sizeof(options.scale_state_tokens));
1192b8e80941Smrg            _mesa_add_state_reference(params, bias_state);
1193b8e80941Smrg            memcpy(options.bias_state_tokens, bias_state,
1194b8e80941Smrg                   sizeof(options.bias_state_tokens));
1195b8e80941Smrg         }
1196b8e80941Smrg
1197b8e80941Smrg         _mesa_add_state_reference(params, texcoord_state);
1198b8e80941Smrg         memcpy(options.texcoord_state_tokens, texcoord_state,
1199b8e80941Smrg                sizeof(options.texcoord_state_tokens));
1200b8e80941Smrg
1201b8e80941Smrg         NIR_PASS_V(tgsi.ir.nir, nir_lower_drawpixels, &options);
1202b8e80941Smrg      }
1203b8e80941Smrg
1204b8e80941Smrg      if (unlikely(key->external.lower_nv12 || key->external.lower_iyuv)) {
1205b8e80941Smrg         nir_lower_tex_options options = {0};
1206b8e80941Smrg         options.lower_y_uv_external = key->external.lower_nv12;
1207b8e80941Smrg         options.lower_y_u_v_external = key->external.lower_iyuv;
1208b8e80941Smrg         NIR_PASS_V(tgsi.ir.nir, nir_lower_tex, &options);
1209b8e80941Smrg      }
1210b8e80941Smrg
1211b8e80941Smrg      st_finalize_nir(st, &stfp->Base, stfp->shader_program, tgsi.ir.nir);
1212b8e80941Smrg
1213b8e80941Smrg      if (unlikely(key->external.lower_nv12 || key->external.lower_iyuv)) {
1214b8e80941Smrg         /* This pass needs to happen *after* nir_lower_sampler */
1215b8e80941Smrg         NIR_PASS_V(tgsi.ir.nir, st_nir_lower_tex_src_plane,
1216b8e80941Smrg                    ~stfp->Base.SamplersUsed,
1217b8e80941Smrg                    key->external.lower_nv12,
1218b8e80941Smrg                    key->external.lower_iyuv);
1219b8e80941Smrg      }
1220b8e80941Smrg
1221b8e80941Smrg      /* Some of the lowering above may have introduced new varyings */
1222b8e80941Smrg      nir_shader_gather_info(tgsi.ir.nir,
1223b8e80941Smrg                             nir_shader_get_entrypoint(tgsi.ir.nir));
1224b8e80941Smrg
1225b8e80941Smrg      variant->driver_shader = pipe->create_fs_state(pipe, &tgsi);
1226b8e80941Smrg      variant->key = *key;
1227b8e80941Smrg
1228b8e80941Smrg      return variant;
1229b8e80941Smrg   }
1230b8e80941Smrg
1231b8e80941Smrg   tgsi.tokens = stfp->tgsi.tokens;
1232b8e80941Smrg
1233b8e80941Smrg   assert(!(key->bitmap && key->drawpixels));
1234b8e80941Smrg
1235b8e80941Smrg   /* Fix texture targets and add fog for ATI_fs */
1236b8e80941Smrg   if (stfp->ati_fs) {
1237b8e80941Smrg      const struct tgsi_token *tokens = st_fixup_atifs(tgsi.tokens, key);
1238b8e80941Smrg
1239b8e80941Smrg      if (tokens)
1240b8e80941Smrg         tgsi.tokens = tokens;
1241b8e80941Smrg      else
1242b8e80941Smrg         fprintf(stderr, "mesa: cannot post-process ATI_fs\n");
1243b8e80941Smrg   }
1244b8e80941Smrg
1245b8e80941Smrg   /* Emulate features. */
1246b8e80941Smrg   if (key->clamp_color || key->persample_shading) {
1247b8e80941Smrg      const struct tgsi_token *tokens;
1248b8e80941Smrg      unsigned flags =
1249b8e80941Smrg         (key->clamp_color ? TGSI_EMU_CLAMP_COLOR_OUTPUTS : 0) |
1250b8e80941Smrg         (key->persample_shading ? TGSI_EMU_FORCE_PERSAMPLE_INTERP : 0);
1251b8e80941Smrg
1252b8e80941Smrg      tokens = tgsi_emulate(tgsi.tokens, flags);
1253b8e80941Smrg
1254b8e80941Smrg      if (tokens) {
1255b8e80941Smrg         if (tgsi.tokens != stfp->tgsi.tokens)
1256b8e80941Smrg            tgsi_free_tokens(tgsi.tokens);
1257b8e80941Smrg         tgsi.tokens = tokens;
1258b8e80941Smrg      } else
1259b8e80941Smrg         fprintf(stderr, "mesa: cannot emulate deprecated features\n");
1260b8e80941Smrg   }
1261b8e80941Smrg
1262b8e80941Smrg   /* glBitmap */
1263b8e80941Smrg   if (key->bitmap) {
1264b8e80941Smrg      const struct tgsi_token *tokens;
1265b8e80941Smrg
1266b8e80941Smrg      variant->bitmap_sampler = ffs(~stfp->Base.SamplersUsed) - 1;
1267b8e80941Smrg
1268b8e80941Smrg      tokens = st_get_bitmap_shader(tgsi.tokens,
1269b8e80941Smrg                                    st->internal_target,
1270b8e80941Smrg                                    variant->bitmap_sampler,
1271b8e80941Smrg                                    st->needs_texcoord_semantic,
1272b8e80941Smrg                                    st->bitmap.tex_format ==
1273b8e80941Smrg                                    PIPE_FORMAT_L8_UNORM);
1274b8e80941Smrg
1275b8e80941Smrg      if (tokens) {
1276b8e80941Smrg         if (tgsi.tokens != stfp->tgsi.tokens)
1277b8e80941Smrg            tgsi_free_tokens(tgsi.tokens);
1278b8e80941Smrg         tgsi.tokens = tokens;
1279b8e80941Smrg      } else
1280b8e80941Smrg         fprintf(stderr, "mesa: cannot create a shader for glBitmap\n");
1281b8e80941Smrg   }
1282b8e80941Smrg
1283b8e80941Smrg   /* glDrawPixels (color only) */
1284b8e80941Smrg   if (key->drawpixels) {
1285b8e80941Smrg      const struct tgsi_token *tokens;
1286b8e80941Smrg      unsigned scale_const = 0, bias_const = 0, texcoord_const = 0;
1287b8e80941Smrg
1288b8e80941Smrg      /* Find the first unused slot. */
1289b8e80941Smrg      variant->drawpix_sampler = ffs(~stfp->Base.SamplersUsed) - 1;
1290b8e80941Smrg
1291b8e80941Smrg      if (key->pixelMaps) {
1292b8e80941Smrg         unsigned samplers_used = stfp->Base.SamplersUsed |
1293b8e80941Smrg                                  (1 << variant->drawpix_sampler);
1294b8e80941Smrg
1295b8e80941Smrg         variant->pixelmap_sampler = ffs(~samplers_used) - 1;
1296b8e80941Smrg      }
1297b8e80941Smrg
1298b8e80941Smrg      if (key->scaleAndBias) {
1299b8e80941Smrg         scale_const = _mesa_add_state_reference(params, scale_state);
1300b8e80941Smrg         bias_const = _mesa_add_state_reference(params, bias_state);
1301b8e80941Smrg      }
1302b8e80941Smrg
1303b8e80941Smrg      texcoord_const = _mesa_add_state_reference(params, texcoord_state);
1304b8e80941Smrg
1305b8e80941Smrg      tokens = st_get_drawpix_shader(tgsi.tokens,
1306b8e80941Smrg                                     st->needs_texcoord_semantic,
1307b8e80941Smrg                                     key->scaleAndBias, scale_const,
1308b8e80941Smrg                                     bias_const, key->pixelMaps,
1309b8e80941Smrg                                     variant->drawpix_sampler,
1310b8e80941Smrg                                     variant->pixelmap_sampler,
1311b8e80941Smrg                                     texcoord_const, st->internal_target);
1312b8e80941Smrg
1313b8e80941Smrg      if (tokens) {
1314b8e80941Smrg         if (tgsi.tokens != stfp->tgsi.tokens)
1315b8e80941Smrg            tgsi_free_tokens(tgsi.tokens);
1316b8e80941Smrg         tgsi.tokens = tokens;
1317b8e80941Smrg      } else
1318b8e80941Smrg         fprintf(stderr, "mesa: cannot create a shader for glDrawPixels\n");
1319b8e80941Smrg   }
1320b8e80941Smrg
1321b8e80941Smrg   if (unlikely(key->external.lower_nv12 || key->external.lower_iyuv)) {
1322b8e80941Smrg      const struct tgsi_token *tokens;
1323b8e80941Smrg
1324b8e80941Smrg      /* samplers inserted would conflict, but this should be unpossible: */
1325b8e80941Smrg      assert(!(key->bitmap || key->drawpixels));
1326b8e80941Smrg
1327b8e80941Smrg      tokens = st_tgsi_lower_yuv(tgsi.tokens,
1328b8e80941Smrg                                 ~stfp->Base.SamplersUsed,
1329b8e80941Smrg                                 key->external.lower_nv12,
1330b8e80941Smrg                                 key->external.lower_iyuv);
1331b8e80941Smrg      if (tokens) {
1332b8e80941Smrg         if (tgsi.tokens != stfp->tgsi.tokens)
1333b8e80941Smrg            tgsi_free_tokens(tgsi.tokens);
1334b8e80941Smrg         tgsi.tokens = tokens;
1335b8e80941Smrg      } else {
1336b8e80941Smrg         fprintf(stderr, "mesa: cannot create a shader for samplerExternalOES\n");
1337b8e80941Smrg      }
1338b8e80941Smrg   }
1339b8e80941Smrg
1340b8e80941Smrg   if (ST_DEBUG & DEBUG_TGSI) {
1341b8e80941Smrg      tgsi_dump(tgsi.tokens, 0);
1342b8e80941Smrg      debug_printf("\n");
1343b8e80941Smrg   }
1344b8e80941Smrg
1345b8e80941Smrg   /* fill in variant */
1346b8e80941Smrg   variant->driver_shader = pipe->create_fs_state(pipe, &tgsi);
1347b8e80941Smrg   variant->key = *key;
1348b8e80941Smrg
1349b8e80941Smrg   if (tgsi.tokens != stfp->tgsi.tokens)
1350b8e80941Smrg      tgsi_free_tokens(tgsi.tokens);
1351b8e80941Smrg   return variant;
1352b8e80941Smrg}
1353b8e80941Smrg
1354b8e80941Smrg/**
1355b8e80941Smrg * Translate fragment program if needed.
1356b8e80941Smrg */
1357b8e80941Smrgstruct st_fp_variant *
1358b8e80941Smrgst_get_fp_variant(struct st_context *st,
1359b8e80941Smrg                  struct st_fragment_program *stfp,
1360b8e80941Smrg                  const struct st_fp_variant_key *key)
1361b8e80941Smrg{
1362b8e80941Smrg   struct st_fp_variant *fpv;
1363848b8605Smrg
1364848b8605Smrg   /* Search for existing variant */
1365848b8605Smrg   for (fpv = stfp->variants; fpv; fpv = fpv->next) {
1366848b8605Smrg      if (memcmp(&fpv->key, key, sizeof(*key)) == 0) {
1367848b8605Smrg         break;
1368848b8605Smrg      }
1369848b8605Smrg   }
1370848b8605Smrg
1371848b8605Smrg   if (!fpv) {
1372848b8605Smrg      /* create new */
1373b8e80941Smrg      fpv = st_create_fp_variant(st, stfp, key);
1374848b8605Smrg      if (fpv) {
1375b8e80941Smrg         if (key->bitmap || key->drawpixels) {
1376b8e80941Smrg            /* Regular variants should always come before the
1377b8e80941Smrg             * bitmap & drawpixels variants, (unless there
1378b8e80941Smrg             * are no regular variants) so that
1379b8e80941Smrg             * st_update_fp can take a fast path when
1380b8e80941Smrg             * shader_has_one_variant is set.
1381b8e80941Smrg             */
1382b8e80941Smrg            if (!stfp->variants) {
1383b8e80941Smrg               stfp->variants = fpv;
1384b8e80941Smrg            } else {
1385b8e80941Smrg               /* insert into list after the first one */
1386b8e80941Smrg               fpv->next = stfp->variants->next;
1387b8e80941Smrg               stfp->variants->next = fpv;
1388b8e80941Smrg            }
1389b8e80941Smrg         } else {
1390b8e80941Smrg            /* insert into list */
1391b8e80941Smrg            fpv->next = stfp->variants;
1392b8e80941Smrg            stfp->variants = fpv;
1393b8e80941Smrg         }
1394848b8605Smrg      }
1395848b8605Smrg   }
1396848b8605Smrg
1397848b8605Smrg   return fpv;
1398848b8605Smrg}
1399848b8605Smrg
1400848b8605Smrg
1401848b8605Smrg/**
1402b8e80941Smrg * Translate a program. This is common code for geometry and tessellation
1403b8e80941Smrg * shaders.
1404848b8605Smrg */
1405b8e80941Smrgstatic void
1406b8e80941Smrgst_translate_program_common(struct st_context *st,
1407b8e80941Smrg                            struct gl_program *prog,
1408b8e80941Smrg                            struct glsl_to_tgsi_visitor *glsl_to_tgsi,
1409b8e80941Smrg                            struct ureg_program *ureg,
1410b8e80941Smrg                            unsigned tgsi_processor,
1411b8e80941Smrg                            struct pipe_shader_state *out_state)
1412848b8605Smrg{
1413b8e80941Smrg   ubyte inputSlotToAttr[VARYING_SLOT_TESS_MAX];
1414b8e80941Smrg   ubyte inputMapping[VARYING_SLOT_TESS_MAX];
1415b8e80941Smrg   ubyte outputMapping[VARYING_SLOT_TESS_MAX];
1416848b8605Smrg   GLuint attr;
1417848b8605Smrg
1418b8e80941Smrg   ubyte input_semantic_name[PIPE_MAX_SHADER_INPUTS];
1419b8e80941Smrg   ubyte input_semantic_index[PIPE_MAX_SHADER_INPUTS];
1420b8e80941Smrg   uint num_inputs = 0;
1421848b8605Smrg
1422b8e80941Smrg   ubyte output_semantic_name[PIPE_MAX_SHADER_OUTPUTS];
1423b8e80941Smrg   ubyte output_semantic_index[PIPE_MAX_SHADER_OUTPUTS];
1424b8e80941Smrg   uint num_outputs = 0;
1425848b8605Smrg
1426848b8605Smrg   GLint i;
1427848b8605Smrg
1428b8e80941Smrg   memset(inputSlotToAttr, 0, sizeof(inputSlotToAttr));
1429848b8605Smrg   memset(inputMapping, 0, sizeof(inputMapping));
1430848b8605Smrg   memset(outputMapping, 0, sizeof(outputMapping));
1431b8e80941Smrg   memset(out_state, 0, sizeof(*out_state));
1432b8e80941Smrg
1433b8e80941Smrg   if (prog->info.clip_distance_array_size)
1434b8e80941Smrg      ureg_property(ureg, TGSI_PROPERTY_NUM_CLIPDIST_ENABLED,
1435b8e80941Smrg                    prog->info.clip_distance_array_size);
1436b8e80941Smrg   if (prog->info.cull_distance_array_size)
1437b8e80941Smrg      ureg_property(ureg, TGSI_PROPERTY_NUM_CULLDIST_ENABLED,
1438b8e80941Smrg                    prog->info.cull_distance_array_size);
1439848b8605Smrg
1440848b8605Smrg   /*
1441848b8605Smrg    * Convert Mesa program inputs to TGSI input register semantics.
1442848b8605Smrg    */
1443848b8605Smrg   for (attr = 0; attr < VARYING_SLOT_MAX; attr++) {
1444b8e80941Smrg      if ((prog->info.inputs_read & BITFIELD64_BIT(attr)) == 0)
1445b8e80941Smrg         continue;
1446848b8605Smrg
1447b8e80941Smrg      unsigned slot = num_inputs++;
1448848b8605Smrg
1449b8e80941Smrg      inputMapping[attr] = slot;
1450b8e80941Smrg      inputSlotToAttr[slot] = attr;
1451848b8605Smrg
1452b8e80941Smrg      unsigned semantic_name, semantic_index;
1453b8e80941Smrg      tgsi_get_gl_varying_semantic(attr, st->needs_texcoord_semantic,
1454b8e80941Smrg                                   &semantic_name, &semantic_index);
1455b8e80941Smrg      input_semantic_name[slot] = semantic_name;
1456b8e80941Smrg      input_semantic_index[slot] = semantic_index;
1457b8e80941Smrg   }
1458848b8605Smrg
1459b8e80941Smrg   /* Also add patch inputs. */
1460b8e80941Smrg   for (attr = 0; attr < 32; attr++) {
1461b8e80941Smrg      if (prog->info.patch_inputs_read & (1u << attr)) {
1462b8e80941Smrg         GLuint slot = num_inputs++;
1463b8e80941Smrg         GLuint patch_attr = VARYING_SLOT_PATCH0 + attr;
1464848b8605Smrg
1465b8e80941Smrg         inputMapping[patch_attr] = slot;
1466b8e80941Smrg         inputSlotToAttr[slot] = patch_attr;
1467b8e80941Smrg         input_semantic_name[slot] = TGSI_SEMANTIC_PATCH;
1468b8e80941Smrg         input_semantic_index[slot] = attr;
1469848b8605Smrg      }
1470848b8605Smrg   }
1471848b8605Smrg
1472848b8605Smrg   /* initialize output semantics to defaults */
1473848b8605Smrg   for (i = 0; i < PIPE_MAX_SHADER_OUTPUTS; i++) {
1474b8e80941Smrg      output_semantic_name[i] = TGSI_SEMANTIC_GENERIC;
1475b8e80941Smrg      output_semantic_index[i] = 0;
1476848b8605Smrg   }
1477848b8605Smrg
1478848b8605Smrg   /*
1479848b8605Smrg    * Determine number of outputs, the (default) output register
1480848b8605Smrg    * mapping and the semantic information for each output.
1481848b8605Smrg    */
1482848b8605Smrg   for (attr = 0; attr < VARYING_SLOT_MAX; attr++) {
1483b8e80941Smrg      if (prog->info.outputs_written & BITFIELD64_BIT(attr)) {
1484b8e80941Smrg         GLuint slot = num_outputs++;
1485848b8605Smrg
1486848b8605Smrg         outputMapping[attr] = slot;
1487848b8605Smrg
1488b8e80941Smrg         unsigned semantic_name, semantic_index;
1489b8e80941Smrg         tgsi_get_gl_varying_semantic(attr, st->needs_texcoord_semantic,
1490b8e80941Smrg                                      &semantic_name, &semantic_index);
1491b8e80941Smrg         output_semantic_name[slot] = semantic_name;
1492b8e80941Smrg         output_semantic_index[slot] = semantic_index;
1493848b8605Smrg      }
1494848b8605Smrg   }
1495848b8605Smrg
1496b8e80941Smrg   /* Also add patch outputs. */
1497b8e80941Smrg   for (attr = 0; attr < 32; attr++) {
1498b8e80941Smrg      if (prog->info.patch_outputs_written & (1u << attr)) {
1499b8e80941Smrg         GLuint slot = num_outputs++;
1500b8e80941Smrg         GLuint patch_attr = VARYING_SLOT_PATCH0 + attr;
1501b8e80941Smrg
1502b8e80941Smrg         outputMapping[patch_attr] = slot;
1503b8e80941Smrg         output_semantic_name[slot] = TGSI_SEMANTIC_PATCH;
1504b8e80941Smrg         output_semantic_index[slot] = attr;
1505848b8605Smrg      }
1506848b8605Smrg   }
1507848b8605Smrg
1508b8e80941Smrg   st_translate_program(st->ctx,
1509b8e80941Smrg                        tgsi_processor,
1510b8e80941Smrg                        ureg,
1511b8e80941Smrg                        glsl_to_tgsi,
1512b8e80941Smrg                        prog,
1513b8e80941Smrg                        /* inputs */
1514b8e80941Smrg                        num_inputs,
1515b8e80941Smrg                        inputMapping,
1516b8e80941Smrg                        inputSlotToAttr,
1517b8e80941Smrg                        input_semantic_name,
1518b8e80941Smrg                        input_semantic_index,
1519b8e80941Smrg                        NULL,
1520b8e80941Smrg                        /* outputs */
1521b8e80941Smrg                        num_outputs,
1522b8e80941Smrg                        outputMapping,
1523b8e80941Smrg                        output_semantic_name,
1524b8e80941Smrg                        output_semantic_index);
1525b8e80941Smrg
1526b8e80941Smrg   if (tgsi_processor == PIPE_SHADER_COMPUTE) {
1527b8e80941Smrg      struct st_compute_program *stcp = (struct st_compute_program *) prog;
1528b8e80941Smrg      out_state->tokens = ureg_get_tokens(ureg, &stcp->num_tgsi_tokens);
1529b8e80941Smrg      stcp->tgsi.prog = out_state->tokens;
1530b8e80941Smrg   } else {
1531b8e80941Smrg      struct st_common_program *stcp = (struct st_common_program *) prog;
1532b8e80941Smrg      out_state->tokens = ureg_get_tokens(ureg, &stcp->num_tgsi_tokens);
1533848b8605Smrg   }
1534b8e80941Smrg   ureg_destroy(ureg);
1535848b8605Smrg
1536b8e80941Smrg   st_translate_stream_output_info(prog->sh.LinkedTransformFeedback,
1537b8e80941Smrg                                   outputMapping,
1538b8e80941Smrg                                   &out_state->stream_output);
1539848b8605Smrg
1540b8e80941Smrg   st_store_ir_in_disk_cache(st, prog, false);
1541848b8605Smrg
1542848b8605Smrg   if ((ST_DEBUG & DEBUG_TGSI) && (ST_DEBUG & DEBUG_MESA)) {
1543b8e80941Smrg      _mesa_print_program(prog);
1544848b8605Smrg      debug_printf("\n");
1545848b8605Smrg   }
1546848b8605Smrg
1547848b8605Smrg   if (ST_DEBUG & DEBUG_TGSI) {
1548b8e80941Smrg      tgsi_dump(out_state->tokens, 0);
1549848b8605Smrg      debug_printf("\n");
1550848b8605Smrg   }
1551b8e80941Smrg}
1552b8e80941Smrg
1553b8e80941Smrg/**
1554b8e80941Smrg * Update stream-output info for GS/TCS/TES.  Normally this is done in
1555b8e80941Smrg * st_translate_program_common() but that is not called for glsl_to_nir
1556b8e80941Smrg * case.
1557b8e80941Smrg */
1558b8e80941Smrgstatic void
1559b8e80941Smrgst_translate_program_stream_output(struct gl_program *prog,
1560b8e80941Smrg                                   struct pipe_stream_output_info *stream_output)
1561b8e80941Smrg{
1562b8e80941Smrg   if (!prog->sh.LinkedTransformFeedback)
1563b8e80941Smrg      return;
1564b8e80941Smrg
1565b8e80941Smrg   ubyte outputMapping[VARYING_SLOT_TESS_MAX];
1566b8e80941Smrg   GLuint attr;
1567b8e80941Smrg   uint num_outputs = 0;
1568848b8605Smrg
1569b8e80941Smrg   memset(outputMapping, 0, sizeof(outputMapping));
1570b8e80941Smrg
1571b8e80941Smrg   /*
1572b8e80941Smrg    * Determine number of outputs, the (default) output register
1573b8e80941Smrg    * mapping and the semantic information for each output.
1574b8e80941Smrg    */
1575b8e80941Smrg   for (attr = 0; attr < VARYING_SLOT_MAX; attr++) {
1576b8e80941Smrg      if (prog->info.outputs_written & BITFIELD64_BIT(attr)) {
1577b8e80941Smrg         GLuint slot = num_outputs++;
1578b8e80941Smrg
1579b8e80941Smrg         outputMapping[attr] = slot;
1580b8e80941Smrg      }
1581b8e80941Smrg   }
1582b8e80941Smrg
1583b8e80941Smrg   st_translate_stream_output_info(prog->sh.LinkedTransformFeedback,
1584b8e80941Smrg                                   outputMapping,
1585b8e80941Smrg                                   stream_output);
1586b8e80941Smrg}
1587b8e80941Smrg
1588b8e80941Smrg/**
1589b8e80941Smrg * Translate a geometry program to create a new variant.
1590b8e80941Smrg */
1591b8e80941Smrgbool
1592b8e80941Smrgst_translate_geometry_program(struct st_context *st,
1593b8e80941Smrg                              struct st_common_program *stgp)
1594b8e80941Smrg{
1595b8e80941Smrg   struct ureg_program *ureg;
1596b8e80941Smrg
1597b8e80941Smrg   /* We have already compiled to NIR so just return */
1598b8e80941Smrg   if (stgp->shader_program) {
1599b8e80941Smrg      /* No variants */
1600b8e80941Smrg      st_finalize_nir(st, &stgp->Base, stgp->shader_program,
1601b8e80941Smrg                      stgp->tgsi.ir.nir);
1602b8e80941Smrg      st_translate_program_stream_output(&stgp->Base, &stgp->tgsi.stream_output);
1603b8e80941Smrg      st_store_ir_in_disk_cache(st, &stgp->Base, true);
1604b8e80941Smrg      return true;
1605b8e80941Smrg   }
1606b8e80941Smrg
1607b8e80941Smrg   ureg = ureg_create_with_screen(PIPE_SHADER_GEOMETRY, st->pipe->screen);
1608b8e80941Smrg   if (ureg == NULL)
1609b8e80941Smrg      return false;
1610b8e80941Smrg
1611b8e80941Smrg   ureg_property(ureg, TGSI_PROPERTY_GS_INPUT_PRIM,
1612b8e80941Smrg                 stgp->Base.info.gs.input_primitive);
1613b8e80941Smrg   ureg_property(ureg, TGSI_PROPERTY_GS_OUTPUT_PRIM,
1614b8e80941Smrg                 stgp->Base.info.gs.output_primitive);
1615b8e80941Smrg   ureg_property(ureg, TGSI_PROPERTY_GS_MAX_OUTPUT_VERTICES,
1616b8e80941Smrg                 stgp->Base.info.gs.vertices_out);
1617b8e80941Smrg   ureg_property(ureg, TGSI_PROPERTY_GS_INVOCATIONS,
1618b8e80941Smrg                 stgp->Base.info.gs.invocations);
1619b8e80941Smrg
1620b8e80941Smrg   st_translate_program_common(st, &stgp->Base, stgp->glsl_to_tgsi, ureg,
1621b8e80941Smrg                               PIPE_SHADER_GEOMETRY, &stgp->tgsi);
1622b8e80941Smrg
1623b8e80941Smrg   free_glsl_to_tgsi_visitor(stgp->glsl_to_tgsi);
1624b8e80941Smrg   stgp->glsl_to_tgsi = NULL;
1625b8e80941Smrg   return true;
1626848b8605Smrg}
1627848b8605Smrg
1628848b8605Smrg
1629848b8605Smrg/**
1630b8e80941Smrg * Get/create a basic program variant.
1631848b8605Smrg */
1632b8e80941Smrgstruct st_basic_variant *
1633b8e80941Smrgst_get_basic_variant(struct st_context *st,
1634b8e80941Smrg                     unsigned pipe_shader,
1635b8e80941Smrg                     struct st_common_program *prog)
1636848b8605Smrg{
1637b8e80941Smrg   struct pipe_context *pipe = st->pipe;
1638b8e80941Smrg   struct st_basic_variant *v;
1639b8e80941Smrg   struct st_basic_variant_key key;
1640b8e80941Smrg   struct pipe_shader_state tgsi = {0};
1641b8e80941Smrg   memset(&key, 0, sizeof(key));
1642b8e80941Smrg   key.st = st->has_shareable_shaders ? NULL : st;
1643848b8605Smrg
1644848b8605Smrg   /* Search for existing variant */
1645b8e80941Smrg   for (v = prog->variants; v; v = v->next) {
1646b8e80941Smrg      if (memcmp(&v->key, &key, sizeof(key)) == 0) {
1647848b8605Smrg         break;
1648848b8605Smrg      }
1649848b8605Smrg   }
1650848b8605Smrg
1651b8e80941Smrg   if (!v) {
1652848b8605Smrg      /* create new */
1653b8e80941Smrg      v = CALLOC_STRUCT(st_basic_variant);
1654b8e80941Smrg      if (v) {
1655b8e80941Smrg
1656b8e80941Smrg	 if (prog->tgsi.type == PIPE_SHADER_IR_NIR) {
1657b8e80941Smrg	    tgsi.type = PIPE_SHADER_IR_NIR;
1658b8e80941Smrg	    tgsi.ir.nir = nir_shader_clone(NULL, prog->tgsi.ir.nir);
1659b8e80941Smrg            tgsi.stream_output = prog->tgsi.stream_output;
1660b8e80941Smrg	 } else
1661b8e80941Smrg	    tgsi = prog->tgsi;
1662b8e80941Smrg         /* fill in new variant */
1663b8e80941Smrg         switch (pipe_shader) {
1664b8e80941Smrg         case PIPE_SHADER_TESS_CTRL:
1665b8e80941Smrg            v->driver_shader = pipe->create_tcs_state(pipe, &tgsi);
1666b8e80941Smrg            break;
1667b8e80941Smrg         case PIPE_SHADER_TESS_EVAL:
1668b8e80941Smrg            v->driver_shader = pipe->create_tes_state(pipe, &tgsi);
1669b8e80941Smrg            break;
1670b8e80941Smrg         case PIPE_SHADER_GEOMETRY:
1671b8e80941Smrg            v->driver_shader = pipe->create_gs_state(pipe, &tgsi);
1672b8e80941Smrg            break;
1673b8e80941Smrg         default:
1674b8e80941Smrg            assert(!"unhandled shader type");
1675b8e80941Smrg            free(v);
1676b8e80941Smrg            return NULL;
1677b8e80941Smrg         }
1678b8e80941Smrg
1679b8e80941Smrg         v->key = key;
1680b8e80941Smrg
1681848b8605Smrg         /* insert into list */
1682b8e80941Smrg         v->next = prog->variants;
1683b8e80941Smrg         prog->variants = v;
1684848b8605Smrg      }
1685848b8605Smrg   }
1686848b8605Smrg
1687b8e80941Smrg   return v;
1688848b8605Smrg}
1689848b8605Smrg
1690848b8605Smrg
1691b8e80941Smrg/**
1692b8e80941Smrg * Translate a tessellation control program to create a new variant.
1693b8e80941Smrg */
1694b8e80941Smrgbool
1695b8e80941Smrgst_translate_tessctrl_program(struct st_context *st,
1696b8e80941Smrg                              struct st_common_program *sttcp)
1697b8e80941Smrg{
1698b8e80941Smrg   struct ureg_program *ureg;
1699b8e80941Smrg
1700b8e80941Smrg   /* We have already compiled to NIR so just return */
1701b8e80941Smrg   if (sttcp->shader_program) {
1702b8e80941Smrg      /* No variants */
1703b8e80941Smrg      st_finalize_nir(st, &sttcp->Base, sttcp->shader_program,
1704b8e80941Smrg                      sttcp->tgsi.ir.nir);
1705b8e80941Smrg      st_store_ir_in_disk_cache(st, &sttcp->Base, true);
1706b8e80941Smrg      return true;
1707b8e80941Smrg   }
1708b8e80941Smrg
1709b8e80941Smrg   ureg = ureg_create_with_screen(PIPE_SHADER_TESS_CTRL, st->pipe->screen);
1710b8e80941Smrg   if (ureg == NULL)
1711b8e80941Smrg      return false;
1712b8e80941Smrg
1713b8e80941Smrg   ureg_property(ureg, TGSI_PROPERTY_TCS_VERTICES_OUT,
1714b8e80941Smrg                 sttcp->Base.info.tess.tcs_vertices_out);
1715b8e80941Smrg
1716b8e80941Smrg   st_translate_program_common(st, &sttcp->Base, sttcp->glsl_to_tgsi, ureg,
1717b8e80941Smrg                               PIPE_SHADER_TESS_CTRL, &sttcp->tgsi);
1718b8e80941Smrg
1719b8e80941Smrg   free_glsl_to_tgsi_visitor(sttcp->glsl_to_tgsi);
1720b8e80941Smrg   sttcp->glsl_to_tgsi = NULL;
1721b8e80941Smrg   return true;
1722b8e80941Smrg}
1723848b8605Smrg
1724848b8605Smrg
1725848b8605Smrg/**
1726b8e80941Smrg * Translate a tessellation evaluation program to create a new variant.
1727848b8605Smrg */
1728b8e80941Smrgbool
1729b8e80941Smrgst_translate_tesseval_program(struct st_context *st,
1730b8e80941Smrg                              struct st_common_program *sttep)
1731848b8605Smrg{
1732b8e80941Smrg   struct ureg_program *ureg;
1733848b8605Smrg
1734b8e80941Smrg   /* We have already compiled to NIR so just return */
1735b8e80941Smrg   if (sttep->shader_program) {
1736b8e80941Smrg      /* No variants */
1737b8e80941Smrg      st_finalize_nir(st, &sttep->Base, sttep->shader_program,
1738b8e80941Smrg                      sttep->tgsi.ir.nir);
1739b8e80941Smrg      st_translate_program_stream_output(&sttep->Base, &sttep->tgsi.stream_output);
1740b8e80941Smrg      st_store_ir_in_disk_cache(st, &sttep->Base, true);
1741b8e80941Smrg      return true;
1742b8e80941Smrg   }
1743b8e80941Smrg
1744b8e80941Smrg   ureg = ureg_create_with_screen(PIPE_SHADER_TESS_EVAL, st->pipe->screen);
1745b8e80941Smrg   if (ureg == NULL)
1746b8e80941Smrg      return false;
1747b8e80941Smrg
1748b8e80941Smrg   if (sttep->Base.info.tess.primitive_mode == GL_ISOLINES)
1749b8e80941Smrg      ureg_property(ureg, TGSI_PROPERTY_TES_PRIM_MODE, GL_LINES);
1750b8e80941Smrg   else
1751b8e80941Smrg      ureg_property(ureg, TGSI_PROPERTY_TES_PRIM_MODE,
1752b8e80941Smrg                    sttep->Base.info.tess.primitive_mode);
1753b8e80941Smrg
1754b8e80941Smrg   STATIC_ASSERT((TESS_SPACING_EQUAL + 1) % 3 == PIPE_TESS_SPACING_EQUAL);
1755b8e80941Smrg   STATIC_ASSERT((TESS_SPACING_FRACTIONAL_ODD + 1) % 3 ==
1756b8e80941Smrg                 PIPE_TESS_SPACING_FRACTIONAL_ODD);
1757b8e80941Smrg   STATIC_ASSERT((TESS_SPACING_FRACTIONAL_EVEN + 1) % 3 ==
1758b8e80941Smrg                 PIPE_TESS_SPACING_FRACTIONAL_EVEN);
1759b8e80941Smrg
1760b8e80941Smrg   ureg_property(ureg, TGSI_PROPERTY_TES_SPACING,
1761b8e80941Smrg                 (sttep->Base.info.tess.spacing + 1) % 3);
1762b8e80941Smrg
1763b8e80941Smrg   ureg_property(ureg, TGSI_PROPERTY_TES_VERTEX_ORDER_CW,
1764b8e80941Smrg                 !sttep->Base.info.tess.ccw);
1765b8e80941Smrg   ureg_property(ureg, TGSI_PROPERTY_TES_POINT_MODE,
1766b8e80941Smrg                 sttep->Base.info.tess.point_mode);
1767b8e80941Smrg
1768b8e80941Smrg   st_translate_program_common(st, &sttep->Base, sttep->glsl_to_tgsi,
1769b8e80941Smrg                               ureg, PIPE_SHADER_TESS_EVAL, &sttep->tgsi);
1770b8e80941Smrg
1771b8e80941Smrg   free_glsl_to_tgsi_visitor(sttep->glsl_to_tgsi);
1772b8e80941Smrg   sttep->glsl_to_tgsi = NULL;
1773b8e80941Smrg   return true;
1774b8e80941Smrg}
1775b8e80941Smrg
1776b8e80941Smrg
1777b8e80941Smrg/**
1778b8e80941Smrg * Translate a compute program to create a new variant.
1779b8e80941Smrg */
1780b8e80941Smrgbool
1781b8e80941Smrgst_translate_compute_program(struct st_context *st,
1782b8e80941Smrg                             struct st_compute_program *stcp)
1783b8e80941Smrg{
1784b8e80941Smrg   struct ureg_program *ureg;
1785b8e80941Smrg   struct pipe_shader_state prog;
1786b8e80941Smrg
1787b8e80941Smrg   stcp->tgsi.req_local_mem = stcp->Base.info.cs.shared_size;
1788b8e80941Smrg
1789b8e80941Smrg   if (stcp->shader_program) {
1790b8e80941Smrg      /* no compute variants: */
1791b8e80941Smrg      st_finalize_nir(st, &stcp->Base, stcp->shader_program,
1792b8e80941Smrg                      (struct nir_shader *) stcp->tgsi.prog);
1793b8e80941Smrg      st_store_ir_in_disk_cache(st, &stcp->Base, true);
1794b8e80941Smrg      return true;
1795b8e80941Smrg   }
1796b8e80941Smrg
1797b8e80941Smrg   ureg = ureg_create_with_screen(PIPE_SHADER_COMPUTE, st->pipe->screen);
1798b8e80941Smrg   if (ureg == NULL)
1799b8e80941Smrg      return false;
1800b8e80941Smrg
1801b8e80941Smrg   st_translate_program_common(st, &stcp->Base, stcp->glsl_to_tgsi, ureg,
1802b8e80941Smrg                               PIPE_SHADER_COMPUTE, &prog);
1803b8e80941Smrg
1804b8e80941Smrg   stcp->tgsi.ir_type = PIPE_SHADER_IR_TGSI;
1805b8e80941Smrg   stcp->tgsi.req_private_mem = 0;
1806b8e80941Smrg   stcp->tgsi.req_input_mem = 0;
1807b8e80941Smrg
1808b8e80941Smrg   free_glsl_to_tgsi_visitor(stcp->glsl_to_tgsi);
1809b8e80941Smrg   stcp->glsl_to_tgsi = NULL;
1810b8e80941Smrg   return true;
1811b8e80941Smrg}
1812b8e80941Smrg
1813b8e80941Smrg
1814b8e80941Smrg/**
1815b8e80941Smrg * Get/create compute program variant.
1816b8e80941Smrg */
1817b8e80941Smrgstruct st_basic_variant *
1818b8e80941Smrgst_get_cp_variant(struct st_context *st,
1819b8e80941Smrg                  struct pipe_compute_state *tgsi,
1820b8e80941Smrg                  struct st_basic_variant **variants)
1821b8e80941Smrg{
1822b8e80941Smrg   struct pipe_context *pipe = st->pipe;
1823b8e80941Smrg   struct st_basic_variant *v;
1824b8e80941Smrg   struct st_basic_variant_key key;
1825b8e80941Smrg
1826b8e80941Smrg   /* use memset, not an initializer to be sure all memory is zeroed */
1827b8e80941Smrg   memset(&key, 0, sizeof(key));
1828b8e80941Smrg
1829b8e80941Smrg   key.st = st->has_shareable_shaders ? NULL : st;
1830b8e80941Smrg
1831b8e80941Smrg   /* Search for existing variant */
1832b8e80941Smrg   for (v = *variants; v; v = v->next) {
1833b8e80941Smrg      if (memcmp(&v->key, &key, sizeof(key)) == 0) {
1834b8e80941Smrg         break;
1835b8e80941Smrg      }
1836b8e80941Smrg   }
1837b8e80941Smrg
1838b8e80941Smrg   if (!v) {
1839b8e80941Smrg      /* create new */
1840b8e80941Smrg      v = CALLOC_STRUCT(st_basic_variant);
1841b8e80941Smrg      if (v) {
1842b8e80941Smrg         /* fill in new variant */
1843b8e80941Smrg         struct pipe_compute_state cs = *tgsi;
1844b8e80941Smrg         if (tgsi->ir_type == PIPE_SHADER_IR_NIR)
1845b8e80941Smrg            cs.prog = nir_shader_clone(NULL, tgsi->prog);
1846b8e80941Smrg         v->driver_shader = pipe->create_compute_state(pipe, &cs);
1847b8e80941Smrg         v->key = key;
1848b8e80941Smrg
1849b8e80941Smrg         /* insert into list */
1850b8e80941Smrg         v->next = *variants;
1851b8e80941Smrg         *variants = v;
1852848b8605Smrg      }
1853848b8605Smrg   }
1854b8e80941Smrg
1855b8e80941Smrg   return v;
1856848b8605Smrg}
1857848b8605Smrg
1858848b8605Smrg
1859848b8605Smrg/**
1860848b8605Smrg * Vert/Geom/Frag programs have per-context variants.  Free all the
1861848b8605Smrg * variants attached to the given program which match the given context.
1862848b8605Smrg */
1863848b8605Smrgstatic void
1864b8e80941Smrgdestroy_program_variants(struct st_context *st, struct gl_program *target)
1865848b8605Smrg{
1866b8e80941Smrg   if (!target || target == &_mesa_DummyProgram)
1867848b8605Smrg      return;
1868848b8605Smrg
1869b8e80941Smrg   switch (target->Target) {
1870848b8605Smrg   case GL_VERTEX_PROGRAM_ARB:
1871848b8605Smrg      {
1872b8e80941Smrg         struct st_vertex_program *stvp = (struct st_vertex_program *) target;
1873848b8605Smrg         struct st_vp_variant *vpv, **prevPtr = &stvp->variants;
1874848b8605Smrg
1875848b8605Smrg         for (vpv = stvp->variants; vpv; ) {
1876848b8605Smrg            struct st_vp_variant *next = vpv->next;
1877848b8605Smrg            if (vpv->key.st == st) {
1878848b8605Smrg               /* unlink from list */
1879848b8605Smrg               *prevPtr = next;
1880848b8605Smrg               /* destroy this variant */
1881848b8605Smrg               delete_vp_variant(st, vpv);
1882848b8605Smrg            }
1883848b8605Smrg            else {
1884848b8605Smrg               prevPtr = &vpv->next;
1885848b8605Smrg            }
1886848b8605Smrg            vpv = next;
1887848b8605Smrg         }
1888848b8605Smrg      }
1889848b8605Smrg      break;
1890848b8605Smrg   case GL_FRAGMENT_PROGRAM_ARB:
1891848b8605Smrg      {
1892848b8605Smrg         struct st_fragment_program *stfp =
1893b8e80941Smrg            (struct st_fragment_program *) target;
1894848b8605Smrg         struct st_fp_variant *fpv, **prevPtr = &stfp->variants;
1895848b8605Smrg
1896848b8605Smrg         for (fpv = stfp->variants; fpv; ) {
1897848b8605Smrg            struct st_fp_variant *next = fpv->next;
1898848b8605Smrg            if (fpv->key.st == st) {
1899848b8605Smrg               /* unlink from list */
1900848b8605Smrg               *prevPtr = next;
1901848b8605Smrg               /* destroy this variant */
1902848b8605Smrg               delete_fp_variant(st, fpv);
1903848b8605Smrg            }
1904848b8605Smrg            else {
1905848b8605Smrg               prevPtr = &fpv->next;
1906848b8605Smrg            }
1907848b8605Smrg            fpv = next;
1908848b8605Smrg         }
1909848b8605Smrg      }
1910848b8605Smrg      break;
1911b8e80941Smrg   case GL_GEOMETRY_PROGRAM_NV:
1912b8e80941Smrg   case GL_TESS_CONTROL_PROGRAM_NV:
1913b8e80941Smrg   case GL_TESS_EVALUATION_PROGRAM_NV:
1914b8e80941Smrg   case GL_COMPUTE_PROGRAM_NV:
1915848b8605Smrg      {
1916b8e80941Smrg         struct st_common_program *p = st_common_program(target);
1917b8e80941Smrg         struct st_compute_program *cp = (struct st_compute_program*)target;
1918b8e80941Smrg         struct st_basic_variant **variants =
1919b8e80941Smrg            target->Target == GL_COMPUTE_PROGRAM_NV ? &cp->variants :
1920b8e80941Smrg                                                      &p->variants;
1921b8e80941Smrg         struct st_basic_variant *v, **prevPtr = variants;
1922b8e80941Smrg
1923b8e80941Smrg         for (v = *variants; v; ) {
1924b8e80941Smrg            struct st_basic_variant *next = v->next;
1925b8e80941Smrg            if (v->key.st == st) {
1926848b8605Smrg               /* unlink from list */
1927848b8605Smrg               *prevPtr = next;
1928848b8605Smrg               /* destroy this variant */
1929b8e80941Smrg               delete_basic_variant(st, v, target->Target);
1930848b8605Smrg            }
1931848b8605Smrg            else {
1932b8e80941Smrg               prevPtr = &v->next;
1933848b8605Smrg            }
1934b8e80941Smrg            v = next;
1935848b8605Smrg         }
1936848b8605Smrg      }
1937848b8605Smrg      break;
1938848b8605Smrg   default:
1939848b8605Smrg      _mesa_problem(NULL, "Unexpected program target 0x%x in "
1940b8e80941Smrg                    "destroy_program_variants_cb()", target->Target);
1941848b8605Smrg   }
1942848b8605Smrg}
1943848b8605Smrg
1944848b8605Smrg
1945848b8605Smrg/**
1946848b8605Smrg * Callback for _mesa_HashWalk.  Free all the shader's program variants
1947848b8605Smrg * which match the given context.
1948848b8605Smrg */
1949848b8605Smrgstatic void
1950848b8605Smrgdestroy_shader_program_variants_cb(GLuint key, void *data, void *userData)
1951848b8605Smrg{
1952848b8605Smrg   struct st_context *st = (struct st_context *) userData;
1953848b8605Smrg   struct gl_shader *shader = (struct gl_shader *) data;
1954848b8605Smrg
1955848b8605Smrg   switch (shader->Type) {
1956848b8605Smrg   case GL_SHADER_PROGRAM_MESA:
1957848b8605Smrg      {
1958848b8605Smrg         struct gl_shader_program *shProg = (struct gl_shader_program *) data;
1959848b8605Smrg         GLuint i;
1960848b8605Smrg
1961b8e80941Smrg	 for (i = 0; i < ARRAY_SIZE(shProg->_LinkedShaders); i++) {
1962848b8605Smrg	    if (shProg->_LinkedShaders[i])
1963848b8605Smrg               destroy_program_variants(st, shProg->_LinkedShaders[i]->Program);
1964848b8605Smrg	 }
1965848b8605Smrg      }
1966848b8605Smrg      break;
1967848b8605Smrg   case GL_VERTEX_SHADER:
1968848b8605Smrg   case GL_FRAGMENT_SHADER:
1969848b8605Smrg   case GL_GEOMETRY_SHADER:
1970b8e80941Smrg   case GL_TESS_CONTROL_SHADER:
1971b8e80941Smrg   case GL_TESS_EVALUATION_SHADER:
1972b8e80941Smrg   case GL_COMPUTE_SHADER:
1973848b8605Smrg      break;
1974848b8605Smrg   default:
1975848b8605Smrg      assert(0);
1976848b8605Smrg   }
1977848b8605Smrg}
1978848b8605Smrg
1979848b8605Smrg
1980848b8605Smrg/**
1981848b8605Smrg * Callback for _mesa_HashWalk.  Free all the program variants which match
1982848b8605Smrg * the given context.
1983848b8605Smrg */
1984848b8605Smrgstatic void
1985848b8605Smrgdestroy_program_variants_cb(GLuint key, void *data, void *userData)
1986848b8605Smrg{
1987848b8605Smrg   struct st_context *st = (struct st_context *) userData;
1988848b8605Smrg   struct gl_program *program = (struct gl_program *) data;
1989848b8605Smrg   destroy_program_variants(st, program);
1990848b8605Smrg}
1991848b8605Smrg
1992848b8605Smrg
1993848b8605Smrg/**
1994848b8605Smrg * Walk over all shaders and programs to delete any variants which
1995848b8605Smrg * belong to the given context.
1996848b8605Smrg * This is called during context tear-down.
1997848b8605Smrg */
1998848b8605Smrgvoid
1999848b8605Smrgst_destroy_program_variants(struct st_context *st)
2000848b8605Smrg{
2001b8e80941Smrg   /* If shaders can be shared with other contexts, the last context will
2002b8e80941Smrg    * call DeleteProgram on all shaders, releasing everything.
2003b8e80941Smrg    */
2004b8e80941Smrg   if (st->has_shareable_shaders)
2005b8e80941Smrg      return;
2006b8e80941Smrg
2007848b8605Smrg   /* ARB vert/frag program */
2008848b8605Smrg   _mesa_HashWalk(st->ctx->Shared->Programs,
2009848b8605Smrg                  destroy_program_variants_cb, st);
2010848b8605Smrg
2011848b8605Smrg   /* GLSL vert/frag/geom shaders */
2012848b8605Smrg   _mesa_HashWalk(st->ctx->Shared->ShaderObjects,
2013848b8605Smrg                  destroy_shader_program_variants_cb, st);
2014848b8605Smrg}
2015848b8605Smrg
2016848b8605Smrg
2017848b8605Smrg/**
2018848b8605Smrg * For debugging, print/dump the current vertex program.
2019848b8605Smrg */
2020848b8605Smrgvoid
2021848b8605Smrgst_print_current_vertex_program(void)
2022848b8605Smrg{
2023848b8605Smrg   GET_CURRENT_CONTEXT(ctx);
2024848b8605Smrg
2025848b8605Smrg   if (ctx->VertexProgram._Current) {
2026848b8605Smrg      struct st_vertex_program *stvp =
2027848b8605Smrg         (struct st_vertex_program *) ctx->VertexProgram._Current;
2028848b8605Smrg      struct st_vp_variant *stv;
2029848b8605Smrg
2030b8e80941Smrg      debug_printf("Vertex program %u\n", stvp->Base.Id);
2031848b8605Smrg
2032848b8605Smrg      for (stv = stvp->variants; stv; stv = stv->next) {
2033848b8605Smrg         debug_printf("variant %p\n", stv);
2034848b8605Smrg         tgsi_dump(stv->tgsi.tokens, 0);
2035848b8605Smrg      }
2036848b8605Smrg   }
2037848b8605Smrg}
2038b8e80941Smrg
2039b8e80941Smrg
2040b8e80941Smrg/**
2041b8e80941Smrg * Compile one shader variant.
2042b8e80941Smrg */
2043b8e80941Smrgvoid
2044b8e80941Smrgst_precompile_shader_variant(struct st_context *st,
2045b8e80941Smrg                             struct gl_program *prog)
2046b8e80941Smrg{
2047b8e80941Smrg   switch (prog->Target) {
2048b8e80941Smrg   case GL_VERTEX_PROGRAM_ARB: {
2049b8e80941Smrg      struct st_vertex_program *p = (struct st_vertex_program *)prog;
2050b8e80941Smrg      struct st_vp_variant_key key;
2051b8e80941Smrg
2052b8e80941Smrg      memset(&key, 0, sizeof(key));
2053b8e80941Smrg
2054b8e80941Smrg      key.st = st->has_shareable_shaders ? NULL : st;
2055b8e80941Smrg      st_get_vp_variant(st, p, &key);
2056b8e80941Smrg      break;
2057b8e80941Smrg   }
2058b8e80941Smrg
2059b8e80941Smrg   case GL_TESS_CONTROL_PROGRAM_NV: {
2060b8e80941Smrg      struct st_common_program *p = st_common_program(prog);
2061b8e80941Smrg      st_get_basic_variant(st, PIPE_SHADER_TESS_CTRL, p);
2062b8e80941Smrg      break;
2063b8e80941Smrg   }
2064b8e80941Smrg
2065b8e80941Smrg   case GL_TESS_EVALUATION_PROGRAM_NV: {
2066b8e80941Smrg      struct st_common_program *p = st_common_program(prog);
2067b8e80941Smrg      st_get_basic_variant(st, PIPE_SHADER_TESS_EVAL, p);
2068b8e80941Smrg      break;
2069b8e80941Smrg   }
2070b8e80941Smrg
2071b8e80941Smrg   case GL_GEOMETRY_PROGRAM_NV: {
2072b8e80941Smrg      struct st_common_program *p = st_common_program(prog);
2073b8e80941Smrg      st_get_basic_variant(st, PIPE_SHADER_GEOMETRY, p);
2074b8e80941Smrg      break;
2075b8e80941Smrg   }
2076b8e80941Smrg
2077b8e80941Smrg   case GL_FRAGMENT_PROGRAM_ARB: {
2078b8e80941Smrg      struct st_fragment_program *p = (struct st_fragment_program *)prog;
2079b8e80941Smrg      struct st_fp_variant_key key;
2080b8e80941Smrg
2081b8e80941Smrg      memset(&key, 0, sizeof(key));
2082b8e80941Smrg
2083b8e80941Smrg      key.st = st->has_shareable_shaders ? NULL : st;
2084b8e80941Smrg      st_get_fp_variant(st, p, &key);
2085b8e80941Smrg      break;
2086b8e80941Smrg   }
2087b8e80941Smrg
2088b8e80941Smrg   case GL_COMPUTE_PROGRAM_NV: {
2089b8e80941Smrg      struct st_compute_program *p = (struct st_compute_program *)prog;
2090b8e80941Smrg      st_get_cp_variant(st, &p->tgsi, &p->variants);
2091b8e80941Smrg      break;
2092b8e80941Smrg   }
2093b8e80941Smrg
2094b8e80941Smrg   default:
2095b8e80941Smrg      assert(0);
2096b8e80941Smrg   }
2097b8e80941Smrg}
2098