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