Revert "st/nir: use NIR for asm programs"
[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.info.vs.double_inputs_read &
410 BITFIELD64_BIT(attr)) != 0) {
411 /* add placeholder for second part of a double attribute */
412 stvp->index_to_input[stvp->num_inputs] = ST_DOUBLE_ATTRIB_PLACEHOLDER;
413 stvp->num_inputs++;
414 }
415 }
416 }
417 /* bit of a hack, presetup potentially unused edgeflag input */
418 stvp->input_to_index[VERT_ATTRIB_EDGEFLAG] = stvp->num_inputs;
419 stvp->index_to_input[stvp->num_inputs] = VERT_ATTRIB_EDGEFLAG;
420
421 /* Compute mapping of vertex program outputs to slots.
422 */
423 for (attr = 0; attr < VARYING_SLOT_MAX; attr++) {
424 if ((stvp->Base.info.outputs_written & BITFIELD64_BIT(attr)) == 0) {
425 stvp->result_to_output[attr] = ~0;
426 }
427 else {
428 unsigned slot = num_outputs++;
429
430 stvp->result_to_output[attr] = slot;
431
432 unsigned semantic_name, semantic_index;
433 tgsi_get_gl_varying_semantic(attr, st->needs_texcoord_semantic,
434 &semantic_name, &semantic_index);
435 output_semantic_name[slot] = semantic_name;
436 output_semantic_index[slot] = semantic_index;
437 }
438 }
439 /* similar hack to above, presetup potentially unused edgeflag output */
440 stvp->result_to_output[VARYING_SLOT_EDGE] = num_outputs;
441 output_semantic_name[num_outputs] = TGSI_SEMANTIC_EDGEFLAG;
442 output_semantic_index[num_outputs] = 0;
443
444 /* ARB_vp: */
445 if (!stvp->glsl_to_tgsi && !stvp->shader_program) {
446 _mesa_remove_output_reads(&stvp->Base, PROGRAM_OUTPUT);
447
448 /* This determines which states will be updated when the assembly
449 * shader is bound.
450 */
451 stvp->affected_states = ST_NEW_VS_STATE |
452 ST_NEW_RASTERIZER |
453 ST_NEW_VERTEX_ARRAYS;
454
455 if (stvp->Base.Parameters->NumParameters)
456 stvp->affected_states |= ST_NEW_VS_CONSTANTS;
457
458 /* No samplers are allowed in ARB_vp. */
459 }
460
461 if (stvp->shader_program) {
462 struct gl_program *prog = stvp->shader_program->last_vert_prog;
463 if (prog) {
464 st_translate_stream_output_info2(prog->sh.LinkedTransformFeedback,
465 stvp->result_to_output,
466 &stvp->tgsi.stream_output);
467 }
468
469 st_store_ir_in_disk_cache(st, &stvp->Base, true);
470 return true;
471 }
472
473 ureg = ureg_create_with_screen(PIPE_SHADER_VERTEX, st->pipe->screen);
474 if (ureg == NULL)
475 return false;
476
477 if (stvp->Base.info.clip_distance_array_size)
478 ureg_property(ureg, TGSI_PROPERTY_NUM_CLIPDIST_ENABLED,
479 stvp->Base.info.clip_distance_array_size);
480 if (stvp->Base.info.cull_distance_array_size)
481 ureg_property(ureg, TGSI_PROPERTY_NUM_CULLDIST_ENABLED,
482 stvp->Base.info.cull_distance_array_size);
483
484 if (ST_DEBUG & DEBUG_MESA) {
485 _mesa_print_program(&stvp->Base);
486 _mesa_print_program_parameters(st->ctx, &stvp->Base);
487 debug_printf("\n");
488 }
489
490 if (stvp->glsl_to_tgsi) {
491 error = st_translate_program(st->ctx,
492 PIPE_SHADER_VERTEX,
493 ureg,
494 stvp->glsl_to_tgsi,
495 &stvp->Base,
496 /* inputs */
497 stvp->num_inputs,
498 stvp->input_to_index,
499 NULL, /* inputSlotToAttr */
500 NULL, /* input semantic name */
501 NULL, /* input semantic index */
502 NULL, /* interp mode */
503 /* outputs */
504 num_outputs,
505 stvp->result_to_output,
506 output_semantic_name,
507 output_semantic_index);
508
509 st_translate_stream_output_info(stvp->glsl_to_tgsi,
510 stvp->result_to_output,
511 &stvp->tgsi.stream_output);
512
513 free_glsl_to_tgsi_visitor(stvp->glsl_to_tgsi);
514 } else
515 error = st_translate_mesa_program(st->ctx,
516 PIPE_SHADER_VERTEX,
517 ureg,
518 &stvp->Base,
519 /* inputs */
520 stvp->num_inputs,
521 stvp->input_to_index,
522 NULL, /* input semantic name */
523 NULL, /* input semantic index */
524 NULL,
525 /* outputs */
526 num_outputs,
527 stvp->result_to_output,
528 output_semantic_name,
529 output_semantic_index);
530
531 if (error) {
532 debug_printf("%s: failed to translate Mesa program:\n", __func__);
533 _mesa_print_program(&stvp->Base);
534 debug_assert(0);
535 return false;
536 }
537
538 stvp->tgsi.tokens = ureg_get_tokens(ureg, &stvp->num_tgsi_tokens);
539 ureg_destroy(ureg);
540
541 if (stvp->glsl_to_tgsi) {
542 stvp->glsl_to_tgsi = NULL;
543 st_store_ir_in_disk_cache(st, &stvp->Base, false);
544 }
545
546 return stvp->tgsi.tokens != NULL;
547 }
548
549 static struct st_vp_variant *
550 st_create_vp_variant(struct st_context *st,
551 struct st_vertex_program *stvp,
552 const struct st_vp_variant_key *key)
553 {
554 struct st_vp_variant *vpv = CALLOC_STRUCT(st_vp_variant);
555 struct pipe_context *pipe = st->pipe;
556
557 vpv->key = *key;
558 vpv->tgsi.stream_output = stvp->tgsi.stream_output;
559 vpv->num_inputs = stvp->num_inputs;
560
561 if (stvp->tgsi.type == PIPE_SHADER_IR_NIR) {
562 vpv->tgsi.type = PIPE_SHADER_IR_NIR;
563 vpv->tgsi.ir.nir = nir_shader_clone(NULL, stvp->tgsi.ir.nir);
564 if (key->clamp_color)
565 NIR_PASS_V(vpv->tgsi.ir.nir, nir_lower_clamp_color_outputs);
566 if (key->passthrough_edgeflags) {
567 NIR_PASS_V(vpv->tgsi.ir.nir, nir_lower_passthrough_edgeflags);
568 vpv->num_inputs++;
569 }
570
571 st_finalize_nir(st, &stvp->Base, stvp->shader_program,
572 vpv->tgsi.ir.nir);
573
574 vpv->driver_shader = pipe->create_vs_state(pipe, &vpv->tgsi);
575 /* driver takes ownership of IR: */
576 vpv->tgsi.ir.nir = NULL;
577 return vpv;
578 }
579
580 vpv->tgsi.tokens = tgsi_dup_tokens(stvp->tgsi.tokens);
581
582 /* Emulate features. */
583 if (key->clamp_color || key->passthrough_edgeflags) {
584 const struct tgsi_token *tokens;
585 unsigned flags =
586 (key->clamp_color ? TGSI_EMU_CLAMP_COLOR_OUTPUTS : 0) |
587 (key->passthrough_edgeflags ? TGSI_EMU_PASSTHROUGH_EDGEFLAG : 0);
588
589 tokens = tgsi_emulate(vpv->tgsi.tokens, flags);
590
591 if (tokens) {
592 tgsi_free_tokens(vpv->tgsi.tokens);
593 vpv->tgsi.tokens = tokens;
594
595 if (key->passthrough_edgeflags)
596 vpv->num_inputs++;
597 } else
598 fprintf(stderr, "mesa: cannot emulate deprecated features\n");
599 }
600
601 if (ST_DEBUG & DEBUG_TGSI) {
602 tgsi_dump(vpv->tgsi.tokens, 0);
603 debug_printf("\n");
604 }
605
606 vpv->driver_shader = pipe->create_vs_state(pipe, &vpv->tgsi);
607 return vpv;
608 }
609
610
611 /**
612 * Find/create a vertex program variant.
613 */
614 struct st_vp_variant *
615 st_get_vp_variant(struct st_context *st,
616 struct st_vertex_program *stvp,
617 const struct st_vp_variant_key *key)
618 {
619 struct st_vp_variant *vpv;
620
621 /* Search for existing variant */
622 for (vpv = stvp->variants; vpv; vpv = vpv->next) {
623 if (memcmp(&vpv->key, key, sizeof(*key)) == 0) {
624 break;
625 }
626 }
627
628 if (!vpv) {
629 /* create now */
630 vpv = st_create_vp_variant(st, stvp, key);
631 if (vpv) {
632 for (unsigned index = 0; index < vpv->num_inputs; ++index) {
633 unsigned attr = stvp->index_to_input[index];
634 if (attr == ST_DOUBLE_ATTRIB_PLACEHOLDER)
635 continue;
636 vpv->vert_attrib_mask |= 1u << attr;
637 }
638
639 /* insert into list */
640 vpv->next = stvp->variants;
641 stvp->variants = vpv;
642 }
643 }
644
645 return vpv;
646 }
647
648
649 /**
650 * Translate a Mesa fragment shader into a TGSI shader.
651 */
652 bool
653 st_translate_fragment_program(struct st_context *st,
654 struct st_fragment_program *stfp)
655 {
656 /* We have already compiled to NIR so just return */
657 if (stfp->shader_program) {
658 st_store_ir_in_disk_cache(st, &stfp->Base, true);
659 return true;
660 }
661
662 ubyte outputMapping[2 * FRAG_RESULT_MAX];
663 ubyte inputMapping[VARYING_SLOT_MAX];
664 ubyte inputSlotToAttr[VARYING_SLOT_MAX];
665 ubyte interpMode[PIPE_MAX_SHADER_INPUTS]; /* XXX size? */
666 GLuint attr;
667 GLbitfield64 inputsRead;
668 struct ureg_program *ureg;
669
670 GLboolean write_all = GL_FALSE;
671
672 ubyte input_semantic_name[PIPE_MAX_SHADER_INPUTS];
673 ubyte input_semantic_index[PIPE_MAX_SHADER_INPUTS];
674 uint fs_num_inputs = 0;
675
676 ubyte fs_output_semantic_name[PIPE_MAX_SHADER_OUTPUTS];
677 ubyte fs_output_semantic_index[PIPE_MAX_SHADER_OUTPUTS];
678 uint fs_num_outputs = 0;
679
680 memset(inputSlotToAttr, ~0, sizeof(inputSlotToAttr));
681
682 /* Non-GLSL programs: */
683 if (!stfp->glsl_to_tgsi && !stfp->shader_program) {
684 _mesa_remove_output_reads(&stfp->Base, PROGRAM_OUTPUT);
685 if (st->ctx->Const.GLSLFragCoordIsSysVal)
686 _mesa_program_fragment_position_to_sysval(&stfp->Base);
687
688 /* This determines which states will be updated when the assembly
689 * shader is bound.
690 *
691 * fragment.position and glDrawPixels always use constants.
692 */
693 stfp->affected_states = ST_NEW_FS_STATE |
694 ST_NEW_SAMPLE_SHADING |
695 ST_NEW_FS_CONSTANTS;
696
697 if (stfp->ati_fs) {
698 /* Just set them for ATI_fs unconditionally. */
699 stfp->affected_states |= ST_NEW_FS_SAMPLER_VIEWS |
700 ST_NEW_FS_SAMPLERS;
701 } else {
702 /* ARB_fp */
703 if (stfp->Base.SamplersUsed)
704 stfp->affected_states |= ST_NEW_FS_SAMPLER_VIEWS |
705 ST_NEW_FS_SAMPLERS;
706 }
707 }
708
709 /*
710 * Convert Mesa program inputs to TGSI input register semantics.
711 */
712 inputsRead = stfp->Base.info.inputs_read;
713 for (attr = 0; attr < VARYING_SLOT_MAX; attr++) {
714 if ((inputsRead & BITFIELD64_BIT(attr)) != 0) {
715 const GLuint slot = fs_num_inputs++;
716
717 inputMapping[attr] = slot;
718 inputSlotToAttr[slot] = attr;
719
720 switch (attr) {
721 case VARYING_SLOT_POS:
722 input_semantic_name[slot] = TGSI_SEMANTIC_POSITION;
723 input_semantic_index[slot] = 0;
724 interpMode[slot] = TGSI_INTERPOLATE_LINEAR;
725 break;
726 case VARYING_SLOT_COL0:
727 input_semantic_name[slot] = TGSI_SEMANTIC_COLOR;
728 input_semantic_index[slot] = 0;
729 interpMode[slot] = stfp->glsl_to_tgsi ?
730 TGSI_INTERPOLATE_COUNT : TGSI_INTERPOLATE_COLOR;
731 break;
732 case VARYING_SLOT_COL1:
733 input_semantic_name[slot] = TGSI_SEMANTIC_COLOR;
734 input_semantic_index[slot] = 1;
735 interpMode[slot] = stfp->glsl_to_tgsi ?
736 TGSI_INTERPOLATE_COUNT : TGSI_INTERPOLATE_COLOR;
737 break;
738 case VARYING_SLOT_FOGC:
739 input_semantic_name[slot] = TGSI_SEMANTIC_FOG;
740 input_semantic_index[slot] = 0;
741 interpMode[slot] = TGSI_INTERPOLATE_PERSPECTIVE;
742 break;
743 case VARYING_SLOT_FACE:
744 input_semantic_name[slot] = TGSI_SEMANTIC_FACE;
745 input_semantic_index[slot] = 0;
746 interpMode[slot] = TGSI_INTERPOLATE_CONSTANT;
747 break;
748 case VARYING_SLOT_PRIMITIVE_ID:
749 input_semantic_name[slot] = TGSI_SEMANTIC_PRIMID;
750 input_semantic_index[slot] = 0;
751 interpMode[slot] = TGSI_INTERPOLATE_CONSTANT;
752 break;
753 case VARYING_SLOT_LAYER:
754 input_semantic_name[slot] = TGSI_SEMANTIC_LAYER;
755 input_semantic_index[slot] = 0;
756 interpMode[slot] = TGSI_INTERPOLATE_CONSTANT;
757 break;
758 case VARYING_SLOT_VIEWPORT:
759 input_semantic_name[slot] = TGSI_SEMANTIC_VIEWPORT_INDEX;
760 input_semantic_index[slot] = 0;
761 interpMode[slot] = TGSI_INTERPOLATE_CONSTANT;
762 break;
763 case VARYING_SLOT_CLIP_DIST0:
764 input_semantic_name[slot] = TGSI_SEMANTIC_CLIPDIST;
765 input_semantic_index[slot] = 0;
766 interpMode[slot] = TGSI_INTERPOLATE_PERSPECTIVE;
767 break;
768 case VARYING_SLOT_CLIP_DIST1:
769 input_semantic_name[slot] = TGSI_SEMANTIC_CLIPDIST;
770 input_semantic_index[slot] = 1;
771 interpMode[slot] = TGSI_INTERPOLATE_PERSPECTIVE;
772 break;
773 case VARYING_SLOT_CULL_DIST0:
774 case VARYING_SLOT_CULL_DIST1:
775 /* these should have been lowered by GLSL */
776 assert(0);
777 break;
778 /* In most cases, there is nothing special about these
779 * inputs, so adopt a convention to use the generic
780 * semantic name and the mesa VARYING_SLOT_ number as the
781 * index.
782 *
783 * All that is required is that the vertex shader labels
784 * its own outputs similarly, and that the vertex shader
785 * generates at least every output required by the
786 * fragment shader plus fixed-function hardware (such as
787 * BFC).
788 *
789 * However, some drivers may need us to identify the PNTC and TEXi
790 * varyings if, for example, their capability to replace them with
791 * sprite coordinates is limited.
792 */
793 case VARYING_SLOT_PNTC:
794 if (st->needs_texcoord_semantic) {
795 input_semantic_name[slot] = TGSI_SEMANTIC_PCOORD;
796 input_semantic_index[slot] = 0;
797 interpMode[slot] = TGSI_INTERPOLATE_LINEAR;
798 break;
799 }
800 /* fall through */
801 case VARYING_SLOT_TEX0:
802 case VARYING_SLOT_TEX1:
803 case VARYING_SLOT_TEX2:
804 case VARYING_SLOT_TEX3:
805 case VARYING_SLOT_TEX4:
806 case VARYING_SLOT_TEX5:
807 case VARYING_SLOT_TEX6:
808 case VARYING_SLOT_TEX7:
809 if (st->needs_texcoord_semantic) {
810 input_semantic_name[slot] = TGSI_SEMANTIC_TEXCOORD;
811 input_semantic_index[slot] = attr - VARYING_SLOT_TEX0;
812 interpMode[slot] = stfp->glsl_to_tgsi ?
813 TGSI_INTERPOLATE_COUNT : TGSI_INTERPOLATE_PERSPECTIVE;
814 break;
815 }
816 /* fall through */
817 case VARYING_SLOT_VAR0:
818 default:
819 /* Semantic indices should be zero-based because drivers may choose
820 * to assign a fixed slot determined by that index.
821 * This is useful because ARB_separate_shader_objects uses location
822 * qualifiers for linkage, and if the semantic index corresponds to
823 * these locations, linkage passes in the driver become unecessary.
824 *
825 * If needs_texcoord_semantic is true, no semantic indices will be
826 * consumed for the TEXi varyings, and we can base the locations of
827 * the user varyings on VAR0. Otherwise, we use TEX0 as base index.
828 */
829 assert(attr >= VARYING_SLOT_VAR0 || attr == VARYING_SLOT_PNTC ||
830 (attr >= VARYING_SLOT_TEX0 && attr <= VARYING_SLOT_TEX7));
831 input_semantic_name[slot] = TGSI_SEMANTIC_GENERIC;
832 input_semantic_index[slot] = st_get_generic_varying_index(st, attr);
833 if (attr == VARYING_SLOT_PNTC)
834 interpMode[slot] = TGSI_INTERPOLATE_LINEAR;
835 else {
836 interpMode[slot] = stfp->glsl_to_tgsi ?
837 TGSI_INTERPOLATE_COUNT : TGSI_INTERPOLATE_PERSPECTIVE;
838 }
839 break;
840 }
841 }
842 else {
843 inputMapping[attr] = -1;
844 }
845 }
846
847 /*
848 * Semantics and mapping for outputs
849 */
850 GLbitfield64 outputsWritten = stfp->Base.info.outputs_written;
851
852 /* if z is written, emit that first */
853 if (outputsWritten & BITFIELD64_BIT(FRAG_RESULT_DEPTH)) {
854 fs_output_semantic_name[fs_num_outputs] = TGSI_SEMANTIC_POSITION;
855 fs_output_semantic_index[fs_num_outputs] = 0;
856 outputMapping[FRAG_RESULT_DEPTH] = fs_num_outputs;
857 fs_num_outputs++;
858 outputsWritten &= ~(1 << FRAG_RESULT_DEPTH);
859 }
860
861 if (outputsWritten & BITFIELD64_BIT(FRAG_RESULT_STENCIL)) {
862 fs_output_semantic_name[fs_num_outputs] = TGSI_SEMANTIC_STENCIL;
863 fs_output_semantic_index[fs_num_outputs] = 0;
864 outputMapping[FRAG_RESULT_STENCIL] = fs_num_outputs;
865 fs_num_outputs++;
866 outputsWritten &= ~(1 << FRAG_RESULT_STENCIL);
867 }
868
869 if (outputsWritten & BITFIELD64_BIT(FRAG_RESULT_SAMPLE_MASK)) {
870 fs_output_semantic_name[fs_num_outputs] = TGSI_SEMANTIC_SAMPLEMASK;
871 fs_output_semantic_index[fs_num_outputs] = 0;
872 outputMapping[FRAG_RESULT_SAMPLE_MASK] = fs_num_outputs;
873 fs_num_outputs++;
874 outputsWritten &= ~(1 << FRAG_RESULT_SAMPLE_MASK);
875 }
876
877 /* handle remaining outputs (color) */
878 for (attr = 0; attr < ARRAY_SIZE(outputMapping); attr++) {
879 const GLbitfield64 written = attr < FRAG_RESULT_MAX ? outputsWritten :
880 stfp->Base.SecondaryOutputsWritten;
881 const unsigned loc = attr % FRAG_RESULT_MAX;
882
883 if (written & BITFIELD64_BIT(loc)) {
884 switch (loc) {
885 case FRAG_RESULT_DEPTH:
886 case FRAG_RESULT_STENCIL:
887 case FRAG_RESULT_SAMPLE_MASK:
888 /* handled above */
889 assert(0);
890 break;
891 case FRAG_RESULT_COLOR:
892 write_all = GL_TRUE; /* fallthrough */
893 default: {
894 int index;
895 assert(loc == FRAG_RESULT_COLOR ||
896 (FRAG_RESULT_DATA0 <= loc && loc < FRAG_RESULT_MAX));
897
898 index = (loc == FRAG_RESULT_COLOR) ? 0 : (loc - FRAG_RESULT_DATA0);
899
900 if (attr >= FRAG_RESULT_MAX) {
901 /* Secondary color for dual source blending. */
902 assert(index == 0);
903 index++;
904 }
905
906 fs_output_semantic_name[fs_num_outputs] = TGSI_SEMANTIC_COLOR;
907 fs_output_semantic_index[fs_num_outputs] = index;
908 outputMapping[attr] = fs_num_outputs;
909 break;
910 }
911 }
912
913 fs_num_outputs++;
914 }
915 }
916
917 ureg = ureg_create_with_screen(PIPE_SHADER_FRAGMENT, st->pipe->screen);
918 if (ureg == NULL)
919 return false;
920
921 if (ST_DEBUG & DEBUG_MESA) {
922 _mesa_print_program(&stfp->Base);
923 _mesa_print_program_parameters(st->ctx, &stfp->Base);
924 debug_printf("\n");
925 }
926 if (write_all == GL_TRUE)
927 ureg_property(ureg, TGSI_PROPERTY_FS_COLOR0_WRITES_ALL_CBUFS, 1);
928
929 if (stfp->Base.info.fs.depth_layout != FRAG_DEPTH_LAYOUT_NONE) {
930 switch (stfp->Base.info.fs.depth_layout) {
931 case FRAG_DEPTH_LAYOUT_ANY:
932 ureg_property(ureg, TGSI_PROPERTY_FS_DEPTH_LAYOUT,
933 TGSI_FS_DEPTH_LAYOUT_ANY);
934 break;
935 case FRAG_DEPTH_LAYOUT_GREATER:
936 ureg_property(ureg, TGSI_PROPERTY_FS_DEPTH_LAYOUT,
937 TGSI_FS_DEPTH_LAYOUT_GREATER);
938 break;
939 case FRAG_DEPTH_LAYOUT_LESS:
940 ureg_property(ureg, TGSI_PROPERTY_FS_DEPTH_LAYOUT,
941 TGSI_FS_DEPTH_LAYOUT_LESS);
942 break;
943 case FRAG_DEPTH_LAYOUT_UNCHANGED:
944 ureg_property(ureg, TGSI_PROPERTY_FS_DEPTH_LAYOUT,
945 TGSI_FS_DEPTH_LAYOUT_UNCHANGED);
946 break;
947 default:
948 assert(0);
949 }
950 }
951
952 if (stfp->glsl_to_tgsi) {
953 st_translate_program(st->ctx,
954 PIPE_SHADER_FRAGMENT,
955 ureg,
956 stfp->glsl_to_tgsi,
957 &stfp->Base,
958 /* inputs */
959 fs_num_inputs,
960 inputMapping,
961 inputSlotToAttr,
962 input_semantic_name,
963 input_semantic_index,
964 interpMode,
965 /* outputs */
966 fs_num_outputs,
967 outputMapping,
968 fs_output_semantic_name,
969 fs_output_semantic_index);
970
971 free_glsl_to_tgsi_visitor(stfp->glsl_to_tgsi);
972 } else if (stfp->ati_fs)
973 st_translate_atifs_program(ureg,
974 stfp->ati_fs,
975 &stfp->Base,
976 /* inputs */
977 fs_num_inputs,
978 inputMapping,
979 input_semantic_name,
980 input_semantic_index,
981 interpMode,
982 /* outputs */
983 fs_num_outputs,
984 outputMapping,
985 fs_output_semantic_name,
986 fs_output_semantic_index);
987 else
988 st_translate_mesa_program(st->ctx,
989 PIPE_SHADER_FRAGMENT,
990 ureg,
991 &stfp->Base,
992 /* inputs */
993 fs_num_inputs,
994 inputMapping,
995 input_semantic_name,
996 input_semantic_index,
997 interpMode,
998 /* outputs */
999 fs_num_outputs,
1000 outputMapping,
1001 fs_output_semantic_name,
1002 fs_output_semantic_index);
1003
1004 stfp->tgsi.tokens = ureg_get_tokens(ureg, &stfp->num_tgsi_tokens);
1005 ureg_destroy(ureg);
1006
1007 if (stfp->glsl_to_tgsi) {
1008 stfp->glsl_to_tgsi = NULL;
1009 st_store_ir_in_disk_cache(st, &stfp->Base, false);
1010 }
1011
1012 return stfp->tgsi.tokens != NULL;
1013 }
1014
1015 static struct st_fp_variant *
1016 st_create_fp_variant(struct st_context *st,
1017 struct st_fragment_program *stfp,
1018 const struct st_fp_variant_key *key)
1019 {
1020 struct pipe_context *pipe = st->pipe;
1021 struct st_fp_variant *variant = CALLOC_STRUCT(st_fp_variant);
1022 struct pipe_shader_state tgsi = {0};
1023 struct gl_program_parameter_list *params = stfp->Base.Parameters;
1024 static const gl_state_index16 texcoord_state[STATE_LENGTH] =
1025 { STATE_INTERNAL, STATE_CURRENT_ATTRIB, VERT_ATTRIB_TEX0 };
1026 static const gl_state_index16 scale_state[STATE_LENGTH] =
1027 { STATE_INTERNAL, STATE_PT_SCALE };
1028 static const gl_state_index16 bias_state[STATE_LENGTH] =
1029 { STATE_INTERNAL, STATE_PT_BIAS };
1030
1031 if (!variant)
1032 return NULL;
1033
1034 if (stfp->tgsi.type == PIPE_SHADER_IR_NIR) {
1035 tgsi.type = PIPE_SHADER_IR_NIR;
1036 tgsi.ir.nir = nir_shader_clone(NULL, stfp->tgsi.ir.nir);
1037
1038 if (key->clamp_color)
1039 NIR_PASS_V(tgsi.ir.nir, nir_lower_clamp_color_outputs);
1040
1041 if (key->persample_shading) {
1042 nir_shader *shader = tgsi.ir.nir;
1043 nir_foreach_variable(var, &shader->inputs)
1044 var->data.sample = true;
1045 }
1046
1047 assert(!(key->bitmap && key->drawpixels));
1048
1049 /* glBitmap */
1050 if (key->bitmap) {
1051 nir_lower_bitmap_options options = {0};
1052
1053 variant->bitmap_sampler = ffs(~stfp->Base.SamplersUsed) - 1;
1054 options.sampler = variant->bitmap_sampler;
1055 options.swizzle_xxxx = (st->bitmap.tex_format == PIPE_FORMAT_L8_UNORM);
1056
1057 NIR_PASS_V(tgsi.ir.nir, nir_lower_bitmap, &options);
1058 }
1059
1060 /* glDrawPixels (color only) */
1061 if (key->drawpixels) {
1062 nir_lower_drawpixels_options options = {{0}};
1063 unsigned samplers_used = stfp->Base.SamplersUsed;
1064
1065 /* Find the first unused slot. */
1066 variant->drawpix_sampler = ffs(~samplers_used) - 1;
1067 options.drawpix_sampler = variant->drawpix_sampler;
1068 samplers_used |= (1 << variant->drawpix_sampler);
1069
1070 options.pixel_maps = key->pixelMaps;
1071 if (key->pixelMaps) {
1072 variant->pixelmap_sampler = ffs(~samplers_used) - 1;
1073 options.pixelmap_sampler = variant->pixelmap_sampler;
1074 }
1075
1076 options.scale_and_bias = key->scaleAndBias;
1077 if (key->scaleAndBias) {
1078 _mesa_add_state_reference(params, scale_state);
1079 memcpy(options.scale_state_tokens, scale_state,
1080 sizeof(options.scale_state_tokens));
1081 _mesa_add_state_reference(params, bias_state);
1082 memcpy(options.bias_state_tokens, bias_state,
1083 sizeof(options.bias_state_tokens));
1084 }
1085
1086 _mesa_add_state_reference(params, texcoord_state);
1087 memcpy(options.texcoord_state_tokens, texcoord_state,
1088 sizeof(options.texcoord_state_tokens));
1089
1090 NIR_PASS_V(tgsi.ir.nir, nir_lower_drawpixels, &options);
1091 }
1092
1093 if (unlikely(key->external.lower_nv12 || key->external.lower_iyuv)) {
1094 nir_lower_tex_options options = {0};
1095 options.lower_y_uv_external = key->external.lower_nv12;
1096 options.lower_y_u_v_external = key->external.lower_iyuv;
1097 NIR_PASS_V(tgsi.ir.nir, nir_lower_tex, &options);
1098 }
1099
1100 st_finalize_nir(st, &stfp->Base, stfp->shader_program, tgsi.ir.nir);
1101
1102 if (unlikely(key->external.lower_nv12 || key->external.lower_iyuv)) {
1103 /* This pass needs to happen *after* nir_lower_sampler */
1104 NIR_PASS_V(tgsi.ir.nir, st_nir_lower_tex_src_plane,
1105 ~stfp->Base.SamplersUsed,
1106 key->external.lower_nv12,
1107 key->external.lower_iyuv);
1108 }
1109
1110 variant->driver_shader = pipe->create_fs_state(pipe, &tgsi);
1111 variant->key = *key;
1112
1113 return variant;
1114 }
1115
1116 tgsi.tokens = stfp->tgsi.tokens;
1117
1118 assert(!(key->bitmap && key->drawpixels));
1119
1120 /* Fix texture targets and add fog for ATI_fs */
1121 if (stfp->ati_fs) {
1122 const struct tgsi_token *tokens = st_fixup_atifs(tgsi.tokens, key);
1123
1124 if (tokens)
1125 tgsi.tokens = tokens;
1126 else
1127 fprintf(stderr, "mesa: cannot post-process ATI_fs\n");
1128 }
1129
1130 /* Emulate features. */
1131 if (key->clamp_color || key->persample_shading) {
1132 const struct tgsi_token *tokens;
1133 unsigned flags =
1134 (key->clamp_color ? TGSI_EMU_CLAMP_COLOR_OUTPUTS : 0) |
1135 (key->persample_shading ? TGSI_EMU_FORCE_PERSAMPLE_INTERP : 0);
1136
1137 tokens = tgsi_emulate(tgsi.tokens, flags);
1138
1139 if (tokens) {
1140 if (tgsi.tokens != stfp->tgsi.tokens)
1141 tgsi_free_tokens(tgsi.tokens);
1142 tgsi.tokens = tokens;
1143 } else
1144 fprintf(stderr, "mesa: cannot emulate deprecated features\n");
1145 }
1146
1147 /* glBitmap */
1148 if (key->bitmap) {
1149 const struct tgsi_token *tokens;
1150
1151 variant->bitmap_sampler = ffs(~stfp->Base.SamplersUsed) - 1;
1152
1153 tokens = st_get_bitmap_shader(tgsi.tokens,
1154 st->internal_target,
1155 variant->bitmap_sampler,
1156 st->needs_texcoord_semantic,
1157 st->bitmap.tex_format ==
1158 PIPE_FORMAT_L8_UNORM);
1159
1160 if (tokens) {
1161 if (tgsi.tokens != stfp->tgsi.tokens)
1162 tgsi_free_tokens(tgsi.tokens);
1163 tgsi.tokens = tokens;
1164 } else
1165 fprintf(stderr, "mesa: cannot create a shader for glBitmap\n");
1166 }
1167
1168 /* glDrawPixels (color only) */
1169 if (key->drawpixels) {
1170 const struct tgsi_token *tokens;
1171 unsigned scale_const = 0, bias_const = 0, texcoord_const = 0;
1172
1173 /* Find the first unused slot. */
1174 variant->drawpix_sampler = ffs(~stfp->Base.SamplersUsed) - 1;
1175
1176 if (key->pixelMaps) {
1177 unsigned samplers_used = stfp->Base.SamplersUsed |
1178 (1 << variant->drawpix_sampler);
1179
1180 variant->pixelmap_sampler = ffs(~samplers_used) - 1;
1181 }
1182
1183 if (key->scaleAndBias) {
1184 scale_const = _mesa_add_state_reference(params, scale_state);
1185 bias_const = _mesa_add_state_reference(params, bias_state);
1186 }
1187
1188 texcoord_const = _mesa_add_state_reference(params, texcoord_state);
1189
1190 tokens = st_get_drawpix_shader(tgsi.tokens,
1191 st->needs_texcoord_semantic,
1192 key->scaleAndBias, scale_const,
1193 bias_const, key->pixelMaps,
1194 variant->drawpix_sampler,
1195 variant->pixelmap_sampler,
1196 texcoord_const, st->internal_target);
1197
1198 if (tokens) {
1199 if (tgsi.tokens != stfp->tgsi.tokens)
1200 tgsi_free_tokens(tgsi.tokens);
1201 tgsi.tokens = tokens;
1202 } else
1203 fprintf(stderr, "mesa: cannot create a shader for glDrawPixels\n");
1204 }
1205
1206 if (unlikely(key->external.lower_nv12 || key->external.lower_iyuv)) {
1207 const struct tgsi_token *tokens;
1208
1209 /* samplers inserted would conflict, but this should be unpossible: */
1210 assert(!(key->bitmap || key->drawpixels));
1211
1212 tokens = st_tgsi_lower_yuv(tgsi.tokens,
1213 ~stfp->Base.SamplersUsed,
1214 key->external.lower_nv12,
1215 key->external.lower_iyuv);
1216 if (tokens) {
1217 if (tgsi.tokens != stfp->tgsi.tokens)
1218 tgsi_free_tokens(tgsi.tokens);
1219 tgsi.tokens = tokens;
1220 } else {
1221 fprintf(stderr, "mesa: cannot create a shader for samplerExternalOES\n");
1222 }
1223 }
1224
1225 if (ST_DEBUG & DEBUG_TGSI) {
1226 tgsi_dump(tgsi.tokens, 0);
1227 debug_printf("\n");
1228 }
1229
1230 /* fill in variant */
1231 variant->driver_shader = pipe->create_fs_state(pipe, &tgsi);
1232 variant->key = *key;
1233
1234 if (tgsi.tokens != stfp->tgsi.tokens)
1235 tgsi_free_tokens(tgsi.tokens);
1236 return variant;
1237 }
1238
1239 /**
1240 * Translate fragment program if needed.
1241 */
1242 struct st_fp_variant *
1243 st_get_fp_variant(struct st_context *st,
1244 struct st_fragment_program *stfp,
1245 const struct st_fp_variant_key *key)
1246 {
1247 struct st_fp_variant *fpv;
1248
1249 /* Search for existing variant */
1250 for (fpv = stfp->variants; fpv; fpv = fpv->next) {
1251 if (memcmp(&fpv->key, key, sizeof(*key)) == 0) {
1252 break;
1253 }
1254 }
1255
1256 if (!fpv) {
1257 /* create new */
1258 fpv = st_create_fp_variant(st, stfp, key);
1259 if (fpv) {
1260 if (key->bitmap || key->drawpixels) {
1261 /* Regular variants should always come before the
1262 * bitmap & drawpixels variants, (unless there
1263 * are no regular variants) so that
1264 * st_update_fp can take a fast path when
1265 * shader_has_one_variant is set.
1266 */
1267 if (!stfp->variants) {
1268 stfp->variants = fpv;
1269 } else {
1270 /* insert into list after the first one */
1271 fpv->next = stfp->variants->next;
1272 stfp->variants->next = fpv;
1273 }
1274 } else {
1275 /* insert into list */
1276 fpv->next = stfp->variants;
1277 stfp->variants = fpv;
1278 }
1279 }
1280 }
1281
1282 return fpv;
1283 }
1284
1285
1286 /**
1287 * Translate a program. This is common code for geometry and tessellation
1288 * shaders.
1289 */
1290 static void
1291 st_translate_program_common(struct st_context *st,
1292 struct gl_program *prog,
1293 struct glsl_to_tgsi_visitor *glsl_to_tgsi,
1294 struct ureg_program *ureg,
1295 unsigned tgsi_processor,
1296 struct pipe_shader_state *out_state)
1297 {
1298 ubyte inputSlotToAttr[VARYING_SLOT_TESS_MAX];
1299 ubyte inputMapping[VARYING_SLOT_TESS_MAX];
1300 ubyte outputMapping[VARYING_SLOT_TESS_MAX];
1301 GLuint attr;
1302
1303 ubyte input_semantic_name[PIPE_MAX_SHADER_INPUTS];
1304 ubyte input_semantic_index[PIPE_MAX_SHADER_INPUTS];
1305 uint num_inputs = 0;
1306
1307 ubyte output_semantic_name[PIPE_MAX_SHADER_OUTPUTS];
1308 ubyte output_semantic_index[PIPE_MAX_SHADER_OUTPUTS];
1309 uint num_outputs = 0;
1310
1311 GLint i;
1312
1313 memset(inputSlotToAttr, 0, sizeof(inputSlotToAttr));
1314 memset(inputMapping, 0, sizeof(inputMapping));
1315 memset(outputMapping, 0, sizeof(outputMapping));
1316 memset(out_state, 0, sizeof(*out_state));
1317
1318 if (prog->info.clip_distance_array_size)
1319 ureg_property(ureg, TGSI_PROPERTY_NUM_CLIPDIST_ENABLED,
1320 prog->info.clip_distance_array_size);
1321 if (prog->info.cull_distance_array_size)
1322 ureg_property(ureg, TGSI_PROPERTY_NUM_CULLDIST_ENABLED,
1323 prog->info.cull_distance_array_size);
1324
1325 /*
1326 * Convert Mesa program inputs to TGSI input register semantics.
1327 */
1328 for (attr = 0; attr < VARYING_SLOT_MAX; attr++) {
1329 if ((prog->info.inputs_read & BITFIELD64_BIT(attr)) == 0)
1330 continue;
1331
1332 unsigned slot = num_inputs++;
1333
1334 inputMapping[attr] = slot;
1335 inputSlotToAttr[slot] = attr;
1336
1337 unsigned semantic_name, semantic_index;
1338 tgsi_get_gl_varying_semantic(attr, st->needs_texcoord_semantic,
1339 &semantic_name, &semantic_index);
1340 input_semantic_name[slot] = semantic_name;
1341 input_semantic_index[slot] = semantic_index;
1342 }
1343
1344 /* Also add patch inputs. */
1345 for (attr = 0; attr < 32; attr++) {
1346 if (prog->info.patch_inputs_read & (1u << attr)) {
1347 GLuint slot = num_inputs++;
1348 GLuint patch_attr = VARYING_SLOT_PATCH0 + attr;
1349
1350 inputMapping[patch_attr] = slot;
1351 inputSlotToAttr[slot] = patch_attr;
1352 input_semantic_name[slot] = TGSI_SEMANTIC_PATCH;
1353 input_semantic_index[slot] = attr;
1354 }
1355 }
1356
1357 /* initialize output semantics to defaults */
1358 for (i = 0; i < PIPE_MAX_SHADER_OUTPUTS; i++) {
1359 output_semantic_name[i] = TGSI_SEMANTIC_GENERIC;
1360 output_semantic_index[i] = 0;
1361 }
1362
1363 /*
1364 * Determine number of outputs, the (default) output register
1365 * mapping and the semantic information for each output.
1366 */
1367 for (attr = 0; attr < VARYING_SLOT_MAX; attr++) {
1368 if (prog->info.outputs_written & BITFIELD64_BIT(attr)) {
1369 GLuint slot = num_outputs++;
1370
1371 outputMapping[attr] = slot;
1372
1373 unsigned semantic_name, semantic_index;
1374 tgsi_get_gl_varying_semantic(attr, st->needs_texcoord_semantic,
1375 &semantic_name, &semantic_index);
1376 output_semantic_name[slot] = semantic_name;
1377 output_semantic_index[slot] = semantic_index;
1378 }
1379 }
1380
1381 /* Also add patch outputs. */
1382 for (attr = 0; attr < 32; attr++) {
1383 if (prog->info.patch_outputs_written & (1u << attr)) {
1384 GLuint slot = num_outputs++;
1385 GLuint patch_attr = VARYING_SLOT_PATCH0 + attr;
1386
1387 outputMapping[patch_attr] = slot;
1388 output_semantic_name[slot] = TGSI_SEMANTIC_PATCH;
1389 output_semantic_index[slot] = attr;
1390 }
1391 }
1392
1393 st_translate_program(st->ctx,
1394 tgsi_processor,
1395 ureg,
1396 glsl_to_tgsi,
1397 prog,
1398 /* inputs */
1399 num_inputs,
1400 inputMapping,
1401 inputSlotToAttr,
1402 input_semantic_name,
1403 input_semantic_index,
1404 NULL,
1405 /* outputs */
1406 num_outputs,
1407 outputMapping,
1408 output_semantic_name,
1409 output_semantic_index);
1410
1411 if (tgsi_processor == PIPE_SHADER_COMPUTE) {
1412 struct st_compute_program *stcp = (struct st_compute_program *) prog;
1413 out_state->tokens = ureg_get_tokens(ureg, &stcp->num_tgsi_tokens);
1414 stcp->tgsi.prog = out_state->tokens;
1415 } else {
1416 struct st_common_program *stcp = (struct st_common_program *) prog;
1417 out_state->tokens = ureg_get_tokens(ureg, &stcp->num_tgsi_tokens);
1418 }
1419 ureg_destroy(ureg);
1420
1421 st_translate_stream_output_info(glsl_to_tgsi,
1422 outputMapping,
1423 &out_state->stream_output);
1424
1425 st_store_ir_in_disk_cache(st, prog, false);
1426
1427 if ((ST_DEBUG & DEBUG_TGSI) && (ST_DEBUG & DEBUG_MESA)) {
1428 _mesa_print_program(prog);
1429 debug_printf("\n");
1430 }
1431
1432 if (ST_DEBUG & DEBUG_TGSI) {
1433 tgsi_dump(out_state->tokens, 0);
1434 debug_printf("\n");
1435 }
1436 }
1437
1438 /**
1439 * Update stream-output info for GS/TCS/TES. Normally this is done in
1440 * st_translate_program_common() but that is not called for glsl_to_nir
1441 * case.
1442 */
1443 static void
1444 st_translate_program_stream_output(struct gl_program *prog,
1445 struct pipe_stream_output_info *stream_output)
1446 {
1447 if (!prog->sh.LinkedTransformFeedback)
1448 return;
1449
1450 ubyte outputMapping[VARYING_SLOT_TESS_MAX];
1451 GLuint attr;
1452 uint num_outputs = 0;
1453
1454 memset(outputMapping, 0, sizeof(outputMapping));
1455
1456 /*
1457 * Determine number of outputs, the (default) output register
1458 * mapping and the semantic information for each output.
1459 */
1460 for (attr = 0; attr < VARYING_SLOT_MAX; attr++) {
1461 if (prog->info.outputs_written & BITFIELD64_BIT(attr)) {
1462 GLuint slot = num_outputs++;
1463
1464 outputMapping[attr] = slot;
1465 }
1466 }
1467
1468 st_translate_stream_output_info2(prog->sh.LinkedTransformFeedback,
1469 outputMapping,
1470 stream_output);
1471 }
1472
1473 /**
1474 * Translate a geometry program to create a new variant.
1475 */
1476 bool
1477 st_translate_geometry_program(struct st_context *st,
1478 struct st_common_program *stgp)
1479 {
1480 struct ureg_program *ureg;
1481
1482 /* We have already compiled to NIR so just return */
1483 if (stgp->shader_program) {
1484 /* No variants */
1485 st_finalize_nir(st, &stgp->Base, stgp->shader_program,
1486 stgp->tgsi.ir.nir);
1487 st_translate_program_stream_output(&stgp->Base, &stgp->tgsi.stream_output);
1488 st_store_ir_in_disk_cache(st, &stgp->Base, true);
1489 return true;
1490 }
1491
1492 ureg = ureg_create_with_screen(PIPE_SHADER_GEOMETRY, st->pipe->screen);
1493 if (ureg == NULL)
1494 return false;
1495
1496 ureg_property(ureg, TGSI_PROPERTY_GS_INPUT_PRIM,
1497 stgp->Base.info.gs.input_primitive);
1498 ureg_property(ureg, TGSI_PROPERTY_GS_OUTPUT_PRIM,
1499 stgp->Base.info.gs.output_primitive);
1500 ureg_property(ureg, TGSI_PROPERTY_GS_MAX_OUTPUT_VERTICES,
1501 stgp->Base.info.gs.vertices_out);
1502 ureg_property(ureg, TGSI_PROPERTY_GS_INVOCATIONS,
1503 stgp->Base.info.gs.invocations);
1504
1505 st_translate_program_common(st, &stgp->Base, stgp->glsl_to_tgsi, ureg,
1506 PIPE_SHADER_GEOMETRY, &stgp->tgsi);
1507
1508 free_glsl_to_tgsi_visitor(stgp->glsl_to_tgsi);
1509 stgp->glsl_to_tgsi = NULL;
1510 return true;
1511 }
1512
1513
1514 /**
1515 * Get/create a basic program variant.
1516 */
1517 struct st_basic_variant *
1518 st_get_basic_variant(struct st_context *st,
1519 unsigned pipe_shader,
1520 struct st_common_program *prog)
1521 {
1522 struct pipe_context *pipe = st->pipe;
1523 struct st_basic_variant *v;
1524 struct st_basic_variant_key key;
1525 struct pipe_shader_state tgsi = {0};
1526 memset(&key, 0, sizeof(key));
1527 key.st = st->has_shareable_shaders ? NULL : st;
1528
1529 /* Search for existing variant */
1530 for (v = prog->variants; v; v = v->next) {
1531 if (memcmp(&v->key, &key, sizeof(key)) == 0) {
1532 break;
1533 }
1534 }
1535
1536 if (!v) {
1537 /* create new */
1538 v = CALLOC_STRUCT(st_basic_variant);
1539 if (v) {
1540
1541 if (prog->tgsi.type == PIPE_SHADER_IR_NIR) {
1542 tgsi.type = PIPE_SHADER_IR_NIR;
1543 tgsi.ir.nir = nir_shader_clone(NULL, prog->tgsi.ir.nir);
1544 tgsi.stream_output = prog->tgsi.stream_output;
1545 } else
1546 tgsi = prog->tgsi;
1547 /* fill in new variant */
1548 switch (pipe_shader) {
1549 case PIPE_SHADER_TESS_CTRL:
1550 v->driver_shader = pipe->create_tcs_state(pipe, &tgsi);
1551 break;
1552 case PIPE_SHADER_TESS_EVAL:
1553 v->driver_shader = pipe->create_tes_state(pipe, &tgsi);
1554 break;
1555 case PIPE_SHADER_GEOMETRY:
1556 v->driver_shader = pipe->create_gs_state(pipe, &tgsi);
1557 break;
1558 default:
1559 assert(!"unhandled shader type");
1560 free(v);
1561 return NULL;
1562 }
1563
1564 v->key = key;
1565
1566 /* insert into list */
1567 v->next = prog->variants;
1568 prog->variants = v;
1569 }
1570 }
1571
1572 return v;
1573 }
1574
1575
1576 /**
1577 * Translate a tessellation control program to create a new variant.
1578 */
1579 bool
1580 st_translate_tessctrl_program(struct st_context *st,
1581 struct st_common_program *sttcp)
1582 {
1583 struct ureg_program *ureg;
1584
1585 /* We have already compiled to NIR so just return */
1586 if (sttcp->shader_program) {
1587 /* No variants */
1588 st_finalize_nir(st, &sttcp->Base, sttcp->shader_program,
1589 sttcp->tgsi.ir.nir);
1590 st_store_ir_in_disk_cache(st, &sttcp->Base, true);
1591 return true;
1592 }
1593
1594 ureg = ureg_create_with_screen(PIPE_SHADER_TESS_CTRL, st->pipe->screen);
1595 if (ureg == NULL)
1596 return false;
1597
1598 ureg_property(ureg, TGSI_PROPERTY_TCS_VERTICES_OUT,
1599 sttcp->Base.info.tess.tcs_vertices_out);
1600
1601 st_translate_program_common(st, &sttcp->Base, sttcp->glsl_to_tgsi, ureg,
1602 PIPE_SHADER_TESS_CTRL, &sttcp->tgsi);
1603
1604 free_glsl_to_tgsi_visitor(sttcp->glsl_to_tgsi);
1605 sttcp->glsl_to_tgsi = NULL;
1606 return true;
1607 }
1608
1609
1610 /**
1611 * Translate a tessellation evaluation program to create a new variant.
1612 */
1613 bool
1614 st_translate_tesseval_program(struct st_context *st,
1615 struct st_common_program *sttep)
1616 {
1617 struct ureg_program *ureg;
1618
1619 /* We have already compiled to NIR so just return */
1620 if (sttep->shader_program) {
1621 /* No variants */
1622 st_finalize_nir(st, &sttep->Base, sttep->shader_program,
1623 sttep->tgsi.ir.nir);
1624 st_translate_program_stream_output(&sttep->Base, &sttep->tgsi.stream_output);
1625 st_store_ir_in_disk_cache(st, &sttep->Base, true);
1626 return true;
1627 }
1628
1629 ureg = ureg_create_with_screen(PIPE_SHADER_TESS_EVAL, st->pipe->screen);
1630 if (ureg == NULL)
1631 return false;
1632
1633 if (sttep->Base.info.tess.primitive_mode == GL_ISOLINES)
1634 ureg_property(ureg, TGSI_PROPERTY_TES_PRIM_MODE, GL_LINES);
1635 else
1636 ureg_property(ureg, TGSI_PROPERTY_TES_PRIM_MODE,
1637 sttep->Base.info.tess.primitive_mode);
1638
1639 STATIC_ASSERT((TESS_SPACING_EQUAL + 1) % 3 == PIPE_TESS_SPACING_EQUAL);
1640 STATIC_ASSERT((TESS_SPACING_FRACTIONAL_ODD + 1) % 3 ==
1641 PIPE_TESS_SPACING_FRACTIONAL_ODD);
1642 STATIC_ASSERT((TESS_SPACING_FRACTIONAL_EVEN + 1) % 3 ==
1643 PIPE_TESS_SPACING_FRACTIONAL_EVEN);
1644
1645 ureg_property(ureg, TGSI_PROPERTY_TES_SPACING,
1646 (sttep->Base.info.tess.spacing + 1) % 3);
1647
1648 ureg_property(ureg, TGSI_PROPERTY_TES_VERTEX_ORDER_CW,
1649 !sttep->Base.info.tess.ccw);
1650 ureg_property(ureg, TGSI_PROPERTY_TES_POINT_MODE,
1651 sttep->Base.info.tess.point_mode);
1652
1653 st_translate_program_common(st, &sttep->Base, sttep->glsl_to_tgsi,
1654 ureg, PIPE_SHADER_TESS_EVAL, &sttep->tgsi);
1655
1656 free_glsl_to_tgsi_visitor(sttep->glsl_to_tgsi);
1657 sttep->glsl_to_tgsi = NULL;
1658 return true;
1659 }
1660
1661
1662 /**
1663 * Translate a compute program to create a new variant.
1664 */
1665 bool
1666 st_translate_compute_program(struct st_context *st,
1667 struct st_compute_program *stcp)
1668 {
1669 struct ureg_program *ureg;
1670 struct pipe_shader_state prog;
1671
1672 stcp->tgsi.req_local_mem = stcp->Base.info.cs.shared_size;
1673
1674 if (stcp->shader_program) {
1675 /* no compute variants: */
1676 st_finalize_nir(st, &stcp->Base, stcp->shader_program,
1677 (struct nir_shader *) stcp->tgsi.prog);
1678 st_store_ir_in_disk_cache(st, &stcp->Base, true);
1679 return true;
1680 }
1681
1682 ureg = ureg_create_with_screen(PIPE_SHADER_COMPUTE, st->pipe->screen);
1683 if (ureg == NULL)
1684 return false;
1685
1686 st_translate_program_common(st, &stcp->Base, stcp->glsl_to_tgsi, ureg,
1687 PIPE_SHADER_COMPUTE, &prog);
1688
1689 stcp->tgsi.ir_type = PIPE_SHADER_IR_TGSI;
1690 stcp->tgsi.req_private_mem = 0;
1691 stcp->tgsi.req_input_mem = 0;
1692
1693 free_glsl_to_tgsi_visitor(stcp->glsl_to_tgsi);
1694 stcp->glsl_to_tgsi = NULL;
1695 return true;
1696 }
1697
1698
1699 /**
1700 * Get/create compute program variant.
1701 */
1702 struct st_basic_variant *
1703 st_get_cp_variant(struct st_context *st,
1704 struct pipe_compute_state *tgsi,
1705 struct st_basic_variant **variants)
1706 {
1707 struct pipe_context *pipe = st->pipe;
1708 struct st_basic_variant *v;
1709 struct st_basic_variant_key key;
1710
1711 memset(&key, 0, sizeof(key));
1712 key.st = st->has_shareable_shaders ? NULL : st;
1713
1714 /* Search for existing variant */
1715 for (v = *variants; v; v = v->next) {
1716 if (memcmp(&v->key, &key, sizeof(key)) == 0) {
1717 break;
1718 }
1719 }
1720
1721 if (!v) {
1722 /* create new */
1723 v = CALLOC_STRUCT(st_basic_variant);
1724 if (v) {
1725 /* fill in new variant */
1726 struct pipe_compute_state cs = *tgsi;
1727 if (tgsi->ir_type == PIPE_SHADER_IR_NIR)
1728 cs.prog = nir_shader_clone(NULL, tgsi->prog);
1729 v->driver_shader = pipe->create_compute_state(pipe, &cs);
1730 v->key = key;
1731
1732 /* insert into list */
1733 v->next = *variants;
1734 *variants = v;
1735 }
1736 }
1737
1738 return v;
1739 }
1740
1741
1742 /**
1743 * Vert/Geom/Frag programs have per-context variants. Free all the
1744 * variants attached to the given program which match the given context.
1745 */
1746 static void
1747 destroy_program_variants(struct st_context *st, struct gl_program *target)
1748 {
1749 if (!target || target == &_mesa_DummyProgram)
1750 return;
1751
1752 switch (target->Target) {
1753 case GL_VERTEX_PROGRAM_ARB:
1754 {
1755 struct st_vertex_program *stvp = (struct st_vertex_program *) target;
1756 struct st_vp_variant *vpv, **prevPtr = &stvp->variants;
1757
1758 for (vpv = stvp->variants; vpv; ) {
1759 struct st_vp_variant *next = vpv->next;
1760 if (vpv->key.st == st) {
1761 /* unlink from list */
1762 *prevPtr = next;
1763 /* destroy this variant */
1764 delete_vp_variant(st, vpv);
1765 }
1766 else {
1767 prevPtr = &vpv->next;
1768 }
1769 vpv = next;
1770 }
1771 }
1772 break;
1773 case GL_FRAGMENT_PROGRAM_ARB:
1774 {
1775 struct st_fragment_program *stfp =
1776 (struct st_fragment_program *) target;
1777 struct st_fp_variant *fpv, **prevPtr = &stfp->variants;
1778
1779 for (fpv = stfp->variants; fpv; ) {
1780 struct st_fp_variant *next = fpv->next;
1781 if (fpv->key.st == st) {
1782 /* unlink from list */
1783 *prevPtr = next;
1784 /* destroy this variant */
1785 delete_fp_variant(st, fpv);
1786 }
1787 else {
1788 prevPtr = &fpv->next;
1789 }
1790 fpv = next;
1791 }
1792 }
1793 break;
1794 case GL_GEOMETRY_PROGRAM_NV:
1795 case GL_TESS_CONTROL_PROGRAM_NV:
1796 case GL_TESS_EVALUATION_PROGRAM_NV:
1797 case GL_COMPUTE_PROGRAM_NV:
1798 {
1799 struct st_common_program *p = st_common_program(target);
1800 struct st_compute_program *cp = (struct st_compute_program*)target;
1801 struct st_basic_variant **variants =
1802 target->Target == GL_COMPUTE_PROGRAM_NV ? &cp->variants :
1803 &p->variants;
1804 struct st_basic_variant *v, **prevPtr = variants;
1805
1806 for (v = *variants; v; ) {
1807 struct st_basic_variant *next = v->next;
1808 if (v->key.st == st) {
1809 /* unlink from list */
1810 *prevPtr = next;
1811 /* destroy this variant */
1812 delete_basic_variant(st, v, target->Target);
1813 }
1814 else {
1815 prevPtr = &v->next;
1816 }
1817 v = next;
1818 }
1819 }
1820 break;
1821 default:
1822 _mesa_problem(NULL, "Unexpected program target 0x%x in "
1823 "destroy_program_variants_cb()", target->Target);
1824 }
1825 }
1826
1827
1828 /**
1829 * Callback for _mesa_HashWalk. Free all the shader's program variants
1830 * which match the given context.
1831 */
1832 static void
1833 destroy_shader_program_variants_cb(GLuint key, void *data, void *userData)
1834 {
1835 struct st_context *st = (struct st_context *) userData;
1836 struct gl_shader *shader = (struct gl_shader *) data;
1837
1838 switch (shader->Type) {
1839 case GL_SHADER_PROGRAM_MESA:
1840 {
1841 struct gl_shader_program *shProg = (struct gl_shader_program *) data;
1842 GLuint i;
1843
1844 for (i = 0; i < ARRAY_SIZE(shProg->_LinkedShaders); i++) {
1845 if (shProg->_LinkedShaders[i])
1846 destroy_program_variants(st, shProg->_LinkedShaders[i]->Program);
1847 }
1848 }
1849 break;
1850 case GL_VERTEX_SHADER:
1851 case GL_FRAGMENT_SHADER:
1852 case GL_GEOMETRY_SHADER:
1853 case GL_TESS_CONTROL_SHADER:
1854 case GL_TESS_EVALUATION_SHADER:
1855 case GL_COMPUTE_SHADER:
1856 break;
1857 default:
1858 assert(0);
1859 }
1860 }
1861
1862
1863 /**
1864 * Callback for _mesa_HashWalk. Free all the program variants which match
1865 * the given context.
1866 */
1867 static void
1868 destroy_program_variants_cb(GLuint key, void *data, void *userData)
1869 {
1870 struct st_context *st = (struct st_context *) userData;
1871 struct gl_program *program = (struct gl_program *) data;
1872 destroy_program_variants(st, program);
1873 }
1874
1875
1876 /**
1877 * Walk over all shaders and programs to delete any variants which
1878 * belong to the given context.
1879 * This is called during context tear-down.
1880 */
1881 void
1882 st_destroy_program_variants(struct st_context *st)
1883 {
1884 /* If shaders can be shared with other contexts, the last context will
1885 * call DeleteProgram on all shaders, releasing everything.
1886 */
1887 if (st->has_shareable_shaders)
1888 return;
1889
1890 /* ARB vert/frag program */
1891 _mesa_HashWalk(st->ctx->Shared->Programs,
1892 destroy_program_variants_cb, st);
1893
1894 /* GLSL vert/frag/geom shaders */
1895 _mesa_HashWalk(st->ctx->Shared->ShaderObjects,
1896 destroy_shader_program_variants_cb, st);
1897 }
1898
1899
1900 /**
1901 * For debugging, print/dump the current vertex program.
1902 */
1903 void
1904 st_print_current_vertex_program(void)
1905 {
1906 GET_CURRENT_CONTEXT(ctx);
1907
1908 if (ctx->VertexProgram._Current) {
1909 struct st_vertex_program *stvp =
1910 (struct st_vertex_program *) ctx->VertexProgram._Current;
1911 struct st_vp_variant *stv;
1912
1913 debug_printf("Vertex program %u\n", stvp->Base.Id);
1914
1915 for (stv = stvp->variants; stv; stv = stv->next) {
1916 debug_printf("variant %p\n", stv);
1917 tgsi_dump(stv->tgsi.tokens, 0);
1918 }
1919 }
1920 }
1921
1922
1923 /**
1924 * Compile one shader variant.
1925 */
1926 void
1927 st_precompile_shader_variant(struct st_context *st,
1928 struct gl_program *prog)
1929 {
1930 switch (prog->Target) {
1931 case GL_VERTEX_PROGRAM_ARB: {
1932 struct st_vertex_program *p = (struct st_vertex_program *)prog;
1933 struct st_vp_variant_key key;
1934
1935 memset(&key, 0, sizeof(key));
1936 key.st = st->has_shareable_shaders ? NULL : st;
1937 st_get_vp_variant(st, p, &key);
1938 break;
1939 }
1940
1941 case GL_TESS_CONTROL_PROGRAM_NV: {
1942 struct st_common_program *p = st_common_program(prog);
1943 st_get_basic_variant(st, PIPE_SHADER_TESS_CTRL, p);
1944 break;
1945 }
1946
1947 case GL_TESS_EVALUATION_PROGRAM_NV: {
1948 struct st_common_program *p = st_common_program(prog);
1949 st_get_basic_variant(st, PIPE_SHADER_TESS_EVAL, p);
1950 break;
1951 }
1952
1953 case GL_GEOMETRY_PROGRAM_NV: {
1954 struct st_common_program *p = st_common_program(prog);
1955 st_get_basic_variant(st, PIPE_SHADER_GEOMETRY, p);
1956 break;
1957 }
1958
1959 case GL_FRAGMENT_PROGRAM_ARB: {
1960 struct st_fragment_program *p = (struct st_fragment_program *)prog;
1961 struct st_fp_variant_key key;
1962
1963 memset(&key, 0, sizeof(key));
1964 key.st = st->has_shareable_shaders ? NULL : st;
1965 st_get_fp_variant(st, p, &key);
1966 break;
1967 }
1968
1969 case GL_COMPUTE_PROGRAM_NV: {
1970 struct st_compute_program *p = (struct st_compute_program *)prog;
1971 st_get_cp_variant(st, &p->tgsi, &p->variants);
1972 break;
1973 }
1974
1975 default:
1976 assert(0);
1977 }
1978 }