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