st/mesa: inline st_free_tokens
[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/imports.h"
35 #include "main/hash.h"
36 #include "main/mtypes.h"
37 #include "program/prog_parameter.h"
38 #include "program/prog_print.h"
39 #include "program/programopt.h"
40
41 #include "pipe/p_context.h"
42 #include "pipe/p_defines.h"
43 #include "pipe/p_shader_tokens.h"
44 #include "draw/draw_context.h"
45 #include "tgsi/tgsi_dump.h"
46 #include "tgsi/tgsi_ureg.h"
47
48 #include "st_debug.h"
49 #include "st_cb_bitmap.h"
50 #include "st_cb_drawpixels.h"
51 #include "st_context.h"
52 #include "st_program.h"
53 #include "st_mesa_to_tgsi.h"
54 #include "cso_cache/cso_context.h"
55
56
57
58 /**
59 * Delete a vertex program variant. Note the caller must unlink
60 * the variant from the linked list.
61 */
62 static void
63 delete_vp_variant(struct st_context *st, struct st_vp_variant *vpv)
64 {
65 if (vpv->driver_shader)
66 cso_delete_vertex_shader(st->cso_context, vpv->driver_shader);
67
68 if (vpv->draw_shader)
69 draw_delete_vertex_shader( st->draw, vpv->draw_shader );
70
71 if (vpv->tgsi.tokens)
72 ureg_free_tokens(vpv->tgsi.tokens);
73
74 free( vpv );
75 }
76
77
78
79 /**
80 * Clean out any old compilations:
81 */
82 void
83 st_release_vp_variants( struct st_context *st,
84 struct st_vertex_program *stvp )
85 {
86 struct st_vp_variant *vpv;
87
88 for (vpv = stvp->variants; vpv; ) {
89 struct st_vp_variant *next = vpv->next;
90 delete_vp_variant(st, vpv);
91 vpv = next;
92 }
93
94 stvp->variants = NULL;
95 }
96
97
98
99 /**
100 * Delete a fragment program variant. Note the caller must unlink
101 * the variant from the linked list.
102 */
103 static void
104 delete_fp_variant(struct st_context *st, struct st_fp_variant *fpv)
105 {
106 if (fpv->driver_shader)
107 cso_delete_fragment_shader(st->cso_context, fpv->driver_shader);
108 if (fpv->parameters)
109 _mesa_free_parameter_list(fpv->parameters);
110 if (fpv->tgsi.tokens)
111 ureg_free_tokens(fpv->tgsi.tokens);
112 free(fpv);
113 }
114
115
116 /**
117 * Free all variants of a fragment program.
118 */
119 void
120 st_release_fp_variants(struct st_context *st, struct st_fragment_program *stfp)
121 {
122 struct st_fp_variant *fpv;
123
124 for (fpv = stfp->variants; fpv; ) {
125 struct st_fp_variant *next = fpv->next;
126 delete_fp_variant(st, fpv);
127 fpv = next;
128 }
129
130 stfp->variants = NULL;
131 }
132
133
134 /**
135 * Delete a geometry program variant. Note the caller must unlink
136 * the variant from the linked list.
137 */
138 static void
139 delete_gp_variant(struct st_context *st, struct st_gp_variant *gpv)
140 {
141 if (gpv->driver_shader)
142 cso_delete_geometry_shader(st->cso_context, gpv->driver_shader);
143
144 free(gpv);
145 }
146
147
148 /**
149 * Free all variants of a geometry program.
150 */
151 void
152 st_release_gp_variants(struct st_context *st, struct st_geometry_program *stgp)
153 {
154 struct st_gp_variant *gpv;
155
156 for (gpv = stgp->variants; gpv; ) {
157 struct st_gp_variant *next = gpv->next;
158 delete_gp_variant(st, gpv);
159 gpv = next;
160 }
161
162 stgp->variants = NULL;
163 }
164
165
166
167
168 /**
169 * Translate a Mesa vertex shader into a TGSI shader.
170 * \param outputMapping to map vertex program output registers (VARYING_SLOT_x)
171 * to TGSI output slots
172 * \param tokensOut destination for TGSI tokens
173 * \return pointer to cached pipe_shader object.
174 */
175 void
176 st_prepare_vertex_program(struct gl_context *ctx,
177 struct st_vertex_program *stvp)
178 {
179 struct st_context *st = st_context(ctx);
180 GLuint attr;
181
182 stvp->num_inputs = 0;
183 stvp->num_outputs = 0;
184
185 if (stvp->Base.IsPositionInvariant)
186 _mesa_insert_mvp_code(ctx, &stvp->Base);
187
188 if (!stvp->glsl_to_tgsi)
189 assert(stvp->Base.Base.NumInstructions > 1);
190
191 /*
192 * Determine number of inputs, the mappings between VERT_ATTRIB_x
193 * and TGSI generic input indexes, plus input attrib semantic info.
194 */
195 for (attr = 0; attr < VERT_ATTRIB_MAX; attr++) {
196 if ((stvp->Base.Base.InputsRead & BITFIELD64_BIT(attr)) != 0) {
197 stvp->input_to_index[attr] = stvp->num_inputs;
198 stvp->index_to_input[stvp->num_inputs] = attr;
199 stvp->num_inputs++;
200 }
201 }
202 /* bit of a hack, presetup potentially unused edgeflag input */
203 stvp->input_to_index[VERT_ATTRIB_EDGEFLAG] = stvp->num_inputs;
204 stvp->index_to_input[stvp->num_inputs] = VERT_ATTRIB_EDGEFLAG;
205
206 /* Compute mapping of vertex program outputs to slots.
207 */
208 for (attr = 0; attr < VARYING_SLOT_MAX; attr++) {
209 if ((stvp->Base.Base.OutputsWritten & BITFIELD64_BIT(attr)) == 0) {
210 stvp->result_to_output[attr] = ~0;
211 }
212 else {
213 unsigned slot = stvp->num_outputs++;
214
215 stvp->result_to_output[attr] = slot;
216
217 switch (attr) {
218 case VARYING_SLOT_POS:
219 stvp->output_semantic_name[slot] = TGSI_SEMANTIC_POSITION;
220 stvp->output_semantic_index[slot] = 0;
221 break;
222 case VARYING_SLOT_COL0:
223 stvp->output_semantic_name[slot] = TGSI_SEMANTIC_COLOR;
224 stvp->output_semantic_index[slot] = 0;
225 break;
226 case VARYING_SLOT_COL1:
227 stvp->output_semantic_name[slot] = TGSI_SEMANTIC_COLOR;
228 stvp->output_semantic_index[slot] = 1;
229 break;
230 case VARYING_SLOT_BFC0:
231 stvp->output_semantic_name[slot] = TGSI_SEMANTIC_BCOLOR;
232 stvp->output_semantic_index[slot] = 0;
233 break;
234 case VARYING_SLOT_BFC1:
235 stvp->output_semantic_name[slot] = TGSI_SEMANTIC_BCOLOR;
236 stvp->output_semantic_index[slot] = 1;
237 break;
238 case VARYING_SLOT_FOGC:
239 stvp->output_semantic_name[slot] = TGSI_SEMANTIC_FOG;
240 stvp->output_semantic_index[slot] = 0;
241 break;
242 case VARYING_SLOT_PSIZ:
243 stvp->output_semantic_name[slot] = TGSI_SEMANTIC_PSIZE;
244 stvp->output_semantic_index[slot] = 0;
245 break;
246 case VARYING_SLOT_CLIP_DIST0:
247 stvp->output_semantic_name[slot] = TGSI_SEMANTIC_CLIPDIST;
248 stvp->output_semantic_index[slot] = 0;
249 break;
250 case VARYING_SLOT_CLIP_DIST1:
251 stvp->output_semantic_name[slot] = TGSI_SEMANTIC_CLIPDIST;
252 stvp->output_semantic_index[slot] = 1;
253 break;
254 case VARYING_SLOT_EDGE:
255 assert(0);
256 break;
257 case VARYING_SLOT_CLIP_VERTEX:
258 stvp->output_semantic_name[slot] = TGSI_SEMANTIC_CLIPVERTEX;
259 stvp->output_semantic_index[slot] = 0;
260 break;
261 case VARYING_SLOT_LAYER:
262 stvp->output_semantic_name[slot] = TGSI_SEMANTIC_LAYER;
263 stvp->output_semantic_index[slot] = 0;
264 break;
265 case VARYING_SLOT_VIEWPORT:
266 stvp->output_semantic_name[slot] = TGSI_SEMANTIC_VIEWPORT_INDEX;
267 stvp->output_semantic_index[slot] = 0;
268 break;
269
270 case VARYING_SLOT_TEX0:
271 case VARYING_SLOT_TEX1:
272 case VARYING_SLOT_TEX2:
273 case VARYING_SLOT_TEX3:
274 case VARYING_SLOT_TEX4:
275 case VARYING_SLOT_TEX5:
276 case VARYING_SLOT_TEX6:
277 case VARYING_SLOT_TEX7:
278 if (st->needs_texcoord_semantic) {
279 stvp->output_semantic_name[slot] = TGSI_SEMANTIC_TEXCOORD;
280 stvp->output_semantic_index[slot] = attr - VARYING_SLOT_TEX0;
281 break;
282 }
283 /* fall through */
284 case VARYING_SLOT_VAR0:
285 default:
286 assert(attr < VARYING_SLOT_MAX);
287 stvp->output_semantic_name[slot] = TGSI_SEMANTIC_GENERIC;
288 stvp->output_semantic_index[slot] =
289 st_get_generic_varying_index(st, attr);
290 break;
291 }
292 }
293 }
294 /* similar hack to above, presetup potentially unused edgeflag output */
295 stvp->result_to_output[VARYING_SLOT_EDGE] = stvp->num_outputs;
296 stvp->output_semantic_name[stvp->num_outputs] = TGSI_SEMANTIC_EDGEFLAG;
297 stvp->output_semantic_index[stvp->num_outputs] = 0;
298 }
299
300
301 /**
302 * Translate a vertex program to create a new variant.
303 */
304 static struct st_vp_variant *
305 st_translate_vertex_program(struct st_context *st,
306 struct st_vertex_program *stvp,
307 const struct st_vp_variant_key *key)
308 {
309 struct st_vp_variant *vpv = CALLOC_STRUCT(st_vp_variant);
310 struct pipe_context *pipe = st->pipe;
311 struct ureg_program *ureg;
312 enum pipe_error error;
313 unsigned num_outputs;
314
315 st_prepare_vertex_program(st->ctx, stvp);
316
317 if (!stvp->glsl_to_tgsi)
318 {
319 _mesa_remove_output_reads(&stvp->Base.Base, PROGRAM_OUTPUT);
320 }
321
322 ureg = ureg_create( TGSI_PROCESSOR_VERTEX );
323 if (ureg == NULL) {
324 free(vpv);
325 return NULL;
326 }
327
328 vpv->key = *key;
329
330 vpv->num_inputs = stvp->num_inputs;
331 num_outputs = stvp->num_outputs;
332 if (key->passthrough_edgeflags) {
333 vpv->num_inputs++;
334 num_outputs++;
335 }
336
337 if (ST_DEBUG & DEBUG_MESA) {
338 _mesa_print_program(&stvp->Base.Base);
339 _mesa_print_program_parameters(st->ctx, &stvp->Base.Base);
340 debug_printf("\n");
341 }
342
343 if (stvp->glsl_to_tgsi)
344 error = st_translate_program(st->ctx,
345 TGSI_PROCESSOR_VERTEX,
346 ureg,
347 stvp->glsl_to_tgsi,
348 &stvp->Base.Base,
349 /* inputs */
350 vpv->num_inputs,
351 stvp->input_to_index,
352 NULL, /* input semantic name */
353 NULL, /* input semantic index */
354 NULL, /* interp mode */
355 NULL, /* interp location */
356 /* outputs */
357 num_outputs,
358 stvp->result_to_output,
359 stvp->output_semantic_name,
360 stvp->output_semantic_index,
361 key->passthrough_edgeflags,
362 key->clamp_color);
363 else
364 error = st_translate_mesa_program(st->ctx,
365 TGSI_PROCESSOR_VERTEX,
366 ureg,
367 &stvp->Base.Base,
368 /* inputs */
369 vpv->num_inputs,
370 stvp->input_to_index,
371 NULL, /* input semantic name */
372 NULL, /* input semantic index */
373 NULL,
374 /* outputs */
375 num_outputs,
376 stvp->result_to_output,
377 stvp->output_semantic_name,
378 stvp->output_semantic_index,
379 key->passthrough_edgeflags,
380 key->clamp_color);
381
382 if (error)
383 goto fail;
384
385 vpv->tgsi.tokens = ureg_get_tokens( ureg, NULL );
386 if (!vpv->tgsi.tokens)
387 goto fail;
388
389 ureg_destroy( ureg );
390
391 if (stvp->glsl_to_tgsi) {
392 st_translate_stream_output_info(stvp->glsl_to_tgsi,
393 stvp->result_to_output,
394 &vpv->tgsi.stream_output);
395 }
396
397 if (ST_DEBUG & DEBUG_TGSI) {
398 tgsi_dump(vpv->tgsi.tokens, 0);
399 debug_printf("\n");
400 }
401
402 vpv->driver_shader = pipe->create_vs_state(pipe, &vpv->tgsi);
403 return vpv;
404
405 fail:
406 debug_printf("%s: failed to translate Mesa program:\n", __FUNCTION__);
407 _mesa_print_program(&stvp->Base.Base);
408 debug_assert(0);
409
410 ureg_destroy( ureg );
411 return NULL;
412 }
413
414
415 /**
416 * Find/create a vertex program variant.
417 */
418 struct st_vp_variant *
419 st_get_vp_variant(struct st_context *st,
420 struct st_vertex_program *stvp,
421 const struct st_vp_variant_key *key)
422 {
423 struct st_vp_variant *vpv;
424
425 /* Search for existing variant */
426 for (vpv = stvp->variants; vpv; vpv = vpv->next) {
427 if (memcmp(&vpv->key, key, sizeof(*key)) == 0) {
428 break;
429 }
430 }
431
432 if (!vpv) {
433 /* create now */
434 vpv = st_translate_vertex_program(st, stvp, key);
435 if (vpv) {
436 /* insert into list */
437 vpv->next = stvp->variants;
438 stvp->variants = vpv;
439 }
440 }
441
442 return vpv;
443 }
444
445
446 static unsigned
447 st_translate_interp(enum glsl_interp_qualifier glsl_qual, bool is_color)
448 {
449 switch (glsl_qual) {
450 case INTERP_QUALIFIER_NONE:
451 if (is_color)
452 return TGSI_INTERPOLATE_COLOR;
453 return TGSI_INTERPOLATE_PERSPECTIVE;
454 case INTERP_QUALIFIER_SMOOTH:
455 return TGSI_INTERPOLATE_PERSPECTIVE;
456 case INTERP_QUALIFIER_FLAT:
457 return TGSI_INTERPOLATE_CONSTANT;
458 case INTERP_QUALIFIER_NOPERSPECTIVE:
459 return TGSI_INTERPOLATE_LINEAR;
460 default:
461 assert(0 && "unexpected interp mode in st_translate_interp()");
462 return TGSI_INTERPOLATE_PERSPECTIVE;
463 }
464 }
465
466
467 /**
468 * Translate a Mesa fragment shader into a TGSI shader using extra info in
469 * the key.
470 * \return new fragment program variant
471 */
472 static struct st_fp_variant *
473 st_translate_fragment_program(struct st_context *st,
474 struct st_fragment_program *stfp,
475 const struct st_fp_variant_key *key)
476 {
477 struct pipe_context *pipe = st->pipe;
478 struct st_fp_variant *variant = CALLOC_STRUCT(st_fp_variant);
479 GLboolean deleteFP = GL_FALSE;
480
481 GLuint outputMapping[FRAG_RESULT_MAX];
482 GLuint inputMapping[VARYING_SLOT_MAX];
483 GLuint interpMode[PIPE_MAX_SHADER_INPUTS]; /* XXX size? */
484 GLuint interpLocation[PIPE_MAX_SHADER_INPUTS];
485 GLuint attr;
486 GLbitfield64 inputsRead;
487 struct ureg_program *ureg;
488
489 GLboolean write_all = GL_FALSE;
490
491 ubyte input_semantic_name[PIPE_MAX_SHADER_INPUTS];
492 ubyte input_semantic_index[PIPE_MAX_SHADER_INPUTS];
493 uint fs_num_inputs = 0;
494
495 ubyte fs_output_semantic_name[PIPE_MAX_SHADER_OUTPUTS];
496 ubyte fs_output_semantic_index[PIPE_MAX_SHADER_OUTPUTS];
497 uint fs_num_outputs = 0;
498
499 if (!variant)
500 return NULL;
501
502 assert(!(key->bitmap && key->drawpixels));
503
504 if (key->bitmap) {
505 /* glBitmap drawing */
506 struct gl_fragment_program *fp; /* we free this temp program below */
507
508 st_make_bitmap_fragment_program(st, &stfp->Base,
509 &fp, &variant->bitmap_sampler);
510
511 variant->parameters = _mesa_clone_parameter_list(fp->Base.Parameters);
512 stfp = st_fragment_program(fp);
513 deleteFP = GL_TRUE;
514 }
515 else if (key->drawpixels) {
516 /* glDrawPixels drawing */
517 struct gl_fragment_program *fp; /* we free this temp program below */
518
519 if (key->drawpixels_z || key->drawpixels_stencil) {
520 fp = st_make_drawpix_z_stencil_program(st, key->drawpixels_z,
521 key->drawpixels_stencil);
522 }
523 else {
524 /* RGBA */
525 st_make_drawpix_fragment_program(st, &stfp->Base, &fp);
526 variant->parameters = _mesa_clone_parameter_list(fp->Base.Parameters);
527 deleteFP = GL_TRUE;
528 }
529 stfp = st_fragment_program(fp);
530 }
531
532 if (!stfp->glsl_to_tgsi)
533 _mesa_remove_output_reads(&stfp->Base.Base, PROGRAM_OUTPUT);
534
535 /*
536 * Convert Mesa program inputs to TGSI input register semantics.
537 */
538 inputsRead = stfp->Base.Base.InputsRead;
539 for (attr = 0; attr < VARYING_SLOT_MAX; attr++) {
540 if ((inputsRead & BITFIELD64_BIT(attr)) != 0) {
541 const GLuint slot = fs_num_inputs++;
542
543 inputMapping[attr] = slot;
544 if (stfp->Base.IsCentroid & BITFIELD64_BIT(attr))
545 interpLocation[slot] = TGSI_INTERPOLATE_LOC_CENTROID;
546 else if (stfp->Base.IsSample & BITFIELD64_BIT(attr))
547 interpLocation[slot] = TGSI_INTERPOLATE_LOC_SAMPLE;
548 else
549 interpLocation[slot] = TGSI_INTERPOLATE_LOC_CENTER;
550
551 if (key->persample_shading)
552 interpLocation[slot] = TGSI_INTERPOLATE_LOC_SAMPLE;
553
554 switch (attr) {
555 case VARYING_SLOT_POS:
556 input_semantic_name[slot] = TGSI_SEMANTIC_POSITION;
557 input_semantic_index[slot] = 0;
558 interpMode[slot] = TGSI_INTERPOLATE_LINEAR;
559 break;
560 case VARYING_SLOT_COL0:
561 input_semantic_name[slot] = TGSI_SEMANTIC_COLOR;
562 input_semantic_index[slot] = 0;
563 interpMode[slot] = st_translate_interp(stfp->Base.InterpQualifier[attr],
564 TRUE);
565 break;
566 case VARYING_SLOT_COL1:
567 input_semantic_name[slot] = TGSI_SEMANTIC_COLOR;
568 input_semantic_index[slot] = 1;
569 interpMode[slot] = st_translate_interp(stfp->Base.InterpQualifier[attr],
570 TRUE);
571 break;
572 case VARYING_SLOT_FOGC:
573 input_semantic_name[slot] = TGSI_SEMANTIC_FOG;
574 input_semantic_index[slot] = 0;
575 interpMode[slot] = TGSI_INTERPOLATE_PERSPECTIVE;
576 break;
577 case VARYING_SLOT_FACE:
578 input_semantic_name[slot] = TGSI_SEMANTIC_FACE;
579 input_semantic_index[slot] = 0;
580 interpMode[slot] = TGSI_INTERPOLATE_CONSTANT;
581 break;
582 case VARYING_SLOT_PRIMITIVE_ID:
583 input_semantic_name[slot] = TGSI_SEMANTIC_PRIMID;
584 input_semantic_index[slot] = 0;
585 interpMode[slot] = TGSI_INTERPOLATE_CONSTANT;
586 break;
587 case VARYING_SLOT_LAYER:
588 input_semantic_name[slot] = TGSI_SEMANTIC_LAYER;
589 input_semantic_index[slot] = 0;
590 interpMode[slot] = TGSI_INTERPOLATE_CONSTANT;
591 break;
592 case VARYING_SLOT_VIEWPORT:
593 input_semantic_name[slot] = TGSI_SEMANTIC_VIEWPORT_INDEX;
594 input_semantic_index[slot] = 0;
595 interpMode[slot] = TGSI_INTERPOLATE_CONSTANT;
596 break;
597 case VARYING_SLOT_CLIP_DIST0:
598 input_semantic_name[slot] = TGSI_SEMANTIC_CLIPDIST;
599 input_semantic_index[slot] = 0;
600 interpMode[slot] = TGSI_INTERPOLATE_PERSPECTIVE;
601 break;
602 case VARYING_SLOT_CLIP_DIST1:
603 input_semantic_name[slot] = TGSI_SEMANTIC_CLIPDIST;
604 input_semantic_index[slot] = 1;
605 interpMode[slot] = TGSI_INTERPOLATE_PERSPECTIVE;
606 break;
607 /* In most cases, there is nothing special about these
608 * inputs, so adopt a convention to use the generic
609 * semantic name and the mesa VARYING_SLOT_ number as the
610 * index.
611 *
612 * All that is required is that the vertex shader labels
613 * its own outputs similarly, and that the vertex shader
614 * generates at least every output required by the
615 * fragment shader plus fixed-function hardware (such as
616 * BFC).
617 *
618 * However, some drivers may need us to identify the PNTC and TEXi
619 * varyings if, for example, their capability to replace them with
620 * sprite coordinates is limited.
621 */
622 case VARYING_SLOT_PNTC:
623 if (st->needs_texcoord_semantic) {
624 input_semantic_name[slot] = TGSI_SEMANTIC_PCOORD;
625 input_semantic_index[slot] = 0;
626 interpMode[slot] = TGSI_INTERPOLATE_LINEAR;
627 break;
628 }
629 /* fall through */
630 case VARYING_SLOT_TEX0:
631 case VARYING_SLOT_TEX1:
632 case VARYING_SLOT_TEX2:
633 case VARYING_SLOT_TEX3:
634 case VARYING_SLOT_TEX4:
635 case VARYING_SLOT_TEX5:
636 case VARYING_SLOT_TEX6:
637 case VARYING_SLOT_TEX7:
638 if (st->needs_texcoord_semantic) {
639 input_semantic_name[slot] = TGSI_SEMANTIC_TEXCOORD;
640 input_semantic_index[slot] = attr - VARYING_SLOT_TEX0;
641 interpMode[slot] =
642 st_translate_interp(stfp->Base.InterpQualifier[attr], FALSE);
643 break;
644 }
645 /* fall through */
646 case VARYING_SLOT_VAR0:
647 default:
648 /* Semantic indices should be zero-based because drivers may choose
649 * to assign a fixed slot determined by that index.
650 * This is useful because ARB_separate_shader_objects uses location
651 * qualifiers for linkage, and if the semantic index corresponds to
652 * these locations, linkage passes in the driver become unecessary.
653 *
654 * If needs_texcoord_semantic is true, no semantic indices will be
655 * consumed for the TEXi varyings, and we can base the locations of
656 * the user varyings on VAR0. Otherwise, we use TEX0 as base index.
657 */
658 assert(attr >= VARYING_SLOT_TEX0);
659 input_semantic_name[slot] = TGSI_SEMANTIC_GENERIC;
660 input_semantic_index[slot] = st_get_generic_varying_index(st, attr);
661 if (attr == VARYING_SLOT_PNTC)
662 interpMode[slot] = TGSI_INTERPOLATE_LINEAR;
663 else
664 interpMode[slot] = st_translate_interp(stfp->Base.InterpQualifier[attr],
665 FALSE);
666 break;
667 }
668 }
669 else {
670 inputMapping[attr] = -1;
671 }
672 }
673
674 /*
675 * Semantics and mapping for outputs
676 */
677 {
678 uint numColors = 0;
679 GLbitfield64 outputsWritten = stfp->Base.Base.OutputsWritten;
680
681 /* if z is written, emit that first */
682 if (outputsWritten & BITFIELD64_BIT(FRAG_RESULT_DEPTH)) {
683 fs_output_semantic_name[fs_num_outputs] = TGSI_SEMANTIC_POSITION;
684 fs_output_semantic_index[fs_num_outputs] = 0;
685 outputMapping[FRAG_RESULT_DEPTH] = fs_num_outputs;
686 fs_num_outputs++;
687 outputsWritten &= ~(1 << FRAG_RESULT_DEPTH);
688 }
689
690 if (outputsWritten & BITFIELD64_BIT(FRAG_RESULT_STENCIL)) {
691 fs_output_semantic_name[fs_num_outputs] = TGSI_SEMANTIC_STENCIL;
692 fs_output_semantic_index[fs_num_outputs] = 0;
693 outputMapping[FRAG_RESULT_STENCIL] = fs_num_outputs;
694 fs_num_outputs++;
695 outputsWritten &= ~(1 << FRAG_RESULT_STENCIL);
696 }
697
698 if (outputsWritten & BITFIELD64_BIT(FRAG_RESULT_SAMPLE_MASK)) {
699 fs_output_semantic_name[fs_num_outputs] = TGSI_SEMANTIC_SAMPLEMASK;
700 fs_output_semantic_index[fs_num_outputs] = 0;
701 outputMapping[FRAG_RESULT_SAMPLE_MASK] = fs_num_outputs;
702 fs_num_outputs++;
703 outputsWritten &= ~(1 << FRAG_RESULT_SAMPLE_MASK);
704 }
705
706 /* handle remaining outputs (color) */
707 for (attr = 0; attr < FRAG_RESULT_MAX; attr++) {
708 if (outputsWritten & BITFIELD64_BIT(attr)) {
709 switch (attr) {
710 case FRAG_RESULT_DEPTH:
711 case FRAG_RESULT_STENCIL:
712 case FRAG_RESULT_SAMPLE_MASK:
713 /* handled above */
714 assert(0);
715 break;
716 case FRAG_RESULT_COLOR:
717 write_all = GL_TRUE; /* fallthrough */
718 default:
719 assert(attr == FRAG_RESULT_COLOR ||
720 (FRAG_RESULT_DATA0 <= attr && attr < FRAG_RESULT_MAX));
721 fs_output_semantic_name[fs_num_outputs] = TGSI_SEMANTIC_COLOR;
722 fs_output_semantic_index[fs_num_outputs] = numColors;
723 outputMapping[attr] = fs_num_outputs;
724 numColors++;
725 break;
726 }
727
728 fs_num_outputs++;
729 }
730 }
731 }
732
733 ureg = ureg_create( TGSI_PROCESSOR_FRAGMENT );
734 if (ureg == NULL) {
735 free(variant);
736 return NULL;
737 }
738
739 if (ST_DEBUG & DEBUG_MESA) {
740 _mesa_print_program(&stfp->Base.Base);
741 _mesa_print_program_parameters(st->ctx, &stfp->Base.Base);
742 debug_printf("\n");
743 }
744 if (write_all == GL_TRUE)
745 ureg_property(ureg, TGSI_PROPERTY_FS_COLOR0_WRITES_ALL_CBUFS, 1);
746
747 if (stfp->Base.FragDepthLayout != FRAG_DEPTH_LAYOUT_NONE) {
748 switch (stfp->Base.FragDepthLayout) {
749 case FRAG_DEPTH_LAYOUT_ANY:
750 ureg_property(ureg, TGSI_PROPERTY_FS_DEPTH_LAYOUT,
751 TGSI_FS_DEPTH_LAYOUT_ANY);
752 break;
753 case FRAG_DEPTH_LAYOUT_GREATER:
754 ureg_property(ureg, TGSI_PROPERTY_FS_DEPTH_LAYOUT,
755 TGSI_FS_DEPTH_LAYOUT_GREATER);
756 break;
757 case FRAG_DEPTH_LAYOUT_LESS:
758 ureg_property(ureg, TGSI_PROPERTY_FS_DEPTH_LAYOUT,
759 TGSI_FS_DEPTH_LAYOUT_LESS);
760 break;
761 case FRAG_DEPTH_LAYOUT_UNCHANGED:
762 ureg_property(ureg, TGSI_PROPERTY_FS_DEPTH_LAYOUT,
763 TGSI_FS_DEPTH_LAYOUT_UNCHANGED);
764 break;
765 default:
766 assert(0);
767 }
768 }
769
770 if (stfp->glsl_to_tgsi)
771 st_translate_program(st->ctx,
772 TGSI_PROCESSOR_FRAGMENT,
773 ureg,
774 stfp->glsl_to_tgsi,
775 &stfp->Base.Base,
776 /* inputs */
777 fs_num_inputs,
778 inputMapping,
779 input_semantic_name,
780 input_semantic_index,
781 interpMode,
782 interpLocation,
783 /* outputs */
784 fs_num_outputs,
785 outputMapping,
786 fs_output_semantic_name,
787 fs_output_semantic_index, FALSE,
788 key->clamp_color );
789 else
790 st_translate_mesa_program(st->ctx,
791 TGSI_PROCESSOR_FRAGMENT,
792 ureg,
793 &stfp->Base.Base,
794 /* inputs */
795 fs_num_inputs,
796 inputMapping,
797 input_semantic_name,
798 input_semantic_index,
799 interpMode,
800 /* outputs */
801 fs_num_outputs,
802 outputMapping,
803 fs_output_semantic_name,
804 fs_output_semantic_index, FALSE,
805 key->clamp_color);
806
807 variant->tgsi.tokens = ureg_get_tokens( ureg, NULL );
808 ureg_destroy( ureg );
809
810 if (ST_DEBUG & DEBUG_TGSI) {
811 tgsi_dump(variant->tgsi.tokens, 0/*TGSI_DUMP_VERBOSE*/);
812 debug_printf("\n");
813 }
814
815 /* fill in variant */
816 variant->driver_shader = pipe->create_fs_state(pipe, &variant->tgsi);
817 variant->key = *key;
818
819 if (deleteFP) {
820 /* Free the temporary program made above */
821 struct gl_fragment_program *fp = &stfp->Base;
822 _mesa_reference_fragprog(st->ctx, &fp, NULL);
823 }
824
825 return variant;
826 }
827
828
829 /**
830 * Translate fragment program if needed.
831 */
832 struct st_fp_variant *
833 st_get_fp_variant(struct st_context *st,
834 struct st_fragment_program *stfp,
835 const struct st_fp_variant_key *key)
836 {
837 struct st_fp_variant *fpv;
838
839 /* Search for existing variant */
840 for (fpv = stfp->variants; fpv; fpv = fpv->next) {
841 if (memcmp(&fpv->key, key, sizeof(*key)) == 0) {
842 break;
843 }
844 }
845
846 if (!fpv) {
847 /* create new */
848 fpv = st_translate_fragment_program(st, stfp, key);
849 if (fpv) {
850 /* insert into list */
851 fpv->next = stfp->variants;
852 stfp->variants = fpv;
853 }
854 }
855
856 return fpv;
857 }
858
859
860 /**
861 * Translate a geometry program to create a new variant.
862 */
863 static struct st_gp_variant *
864 st_translate_geometry_program(struct st_context *st,
865 struct st_geometry_program *stgp,
866 const struct st_gp_variant_key *key)
867 {
868 GLuint inputMapping[VARYING_SLOT_MAX];
869 GLuint outputMapping[VARYING_SLOT_MAX];
870 struct pipe_context *pipe = st->pipe;
871 GLuint attr;
872 GLbitfield64 inputsRead;
873
874 uint gs_num_inputs = 0;
875 uint gs_builtin_inputs = 0;
876 uint gs_array_offset = 0;
877
878 ubyte input_semantic_name[PIPE_MAX_SHADER_INPUTS];
879 ubyte input_semantic_index[PIPE_MAX_SHADER_INPUTS];
880
881 ubyte gs_output_semantic_name[PIPE_MAX_SHADER_OUTPUTS];
882 ubyte gs_output_semantic_index[PIPE_MAX_SHADER_OUTPUTS];
883 uint gs_num_outputs = 0;
884
885 GLint i;
886 GLuint maxSlot = 0;
887 struct ureg_program *ureg;
888 struct pipe_shader_state state = {0};
889
890 struct st_gp_variant *gpv;
891
892 gpv = CALLOC_STRUCT(st_gp_variant);
893 if (!gpv)
894 return NULL;
895
896 if (!stgp->glsl_to_tgsi) {
897 _mesa_remove_output_reads(&stgp->Base.Base, PROGRAM_OUTPUT);
898 }
899
900 ureg = ureg_create( TGSI_PROCESSOR_GEOMETRY );
901 if (ureg == NULL) {
902 free(gpv);
903 return NULL;
904 }
905
906 memset(inputMapping, 0, sizeof(inputMapping));
907 memset(outputMapping, 0, sizeof(outputMapping));
908
909 /*
910 * Convert Mesa program inputs to TGSI input register semantics.
911 */
912 inputsRead = stgp->Base.Base.InputsRead;
913 for (attr = 0; attr < VARYING_SLOT_MAX; attr++) {
914 if ((inputsRead & BITFIELD64_BIT(attr)) != 0) {
915 const GLuint slot = gs_num_inputs;
916
917 gs_num_inputs++;
918
919 inputMapping[attr] = slot;
920
921 if (attr != VARYING_SLOT_PRIMITIVE_ID) {
922 gs_array_offset += 2;
923 } else
924 ++gs_builtin_inputs;
925
926 #if 0
927 debug_printf("input map at %d = %d\n",
928 slot + gs_array_offset, stgp->input_map[slot + gs_array_offset]);
929 #endif
930
931 switch (attr) {
932 case VARYING_SLOT_PRIMITIVE_ID:
933 input_semantic_name[slot] = TGSI_SEMANTIC_PRIMID;
934 input_semantic_index[slot] = 0;
935 break;
936 case VARYING_SLOT_POS:
937 input_semantic_name[slot] = TGSI_SEMANTIC_POSITION;
938 input_semantic_index[slot] = 0;
939 break;
940 case VARYING_SLOT_COL0:
941 input_semantic_name[slot] = TGSI_SEMANTIC_COLOR;
942 input_semantic_index[slot] = 0;
943 break;
944 case VARYING_SLOT_COL1:
945 input_semantic_name[slot] = TGSI_SEMANTIC_COLOR;
946 input_semantic_index[slot] = 1;
947 break;
948 case VARYING_SLOT_FOGC:
949 input_semantic_name[slot] = TGSI_SEMANTIC_FOG;
950 input_semantic_index[slot] = 0;
951 break;
952 case VARYING_SLOT_CLIP_VERTEX:
953 input_semantic_name[slot] = TGSI_SEMANTIC_CLIPVERTEX;
954 input_semantic_index[slot] = 0;
955 break;
956 case VARYING_SLOT_CLIP_DIST0:
957 input_semantic_name[slot] = TGSI_SEMANTIC_CLIPDIST;
958 input_semantic_index[slot] = 0;
959 break;
960 case VARYING_SLOT_CLIP_DIST1:
961 input_semantic_name[slot] = TGSI_SEMANTIC_CLIPDIST;
962 input_semantic_index[slot] = 1;
963 break;
964 case VARYING_SLOT_PSIZ:
965 input_semantic_name[slot] = TGSI_SEMANTIC_PSIZE;
966 input_semantic_index[slot] = 0;
967 break;
968 case VARYING_SLOT_TEX0:
969 case VARYING_SLOT_TEX1:
970 case VARYING_SLOT_TEX2:
971 case VARYING_SLOT_TEX3:
972 case VARYING_SLOT_TEX4:
973 case VARYING_SLOT_TEX5:
974 case VARYING_SLOT_TEX6:
975 case VARYING_SLOT_TEX7:
976 if (st->needs_texcoord_semantic) {
977 input_semantic_name[slot] = TGSI_SEMANTIC_TEXCOORD;
978 input_semantic_index[slot] = attr - VARYING_SLOT_TEX0;
979 break;
980 }
981 /* fall through */
982 case VARYING_SLOT_VAR0:
983 default:
984 assert(attr >= VARYING_SLOT_VAR0 && attr < VARYING_SLOT_MAX);
985 input_semantic_name[slot] = TGSI_SEMANTIC_GENERIC;
986 input_semantic_index[slot] =
987 st_get_generic_varying_index(st, attr);
988 break;
989 }
990 }
991 }
992
993 /* initialize output semantics to defaults */
994 for (i = 0; i < PIPE_MAX_SHADER_OUTPUTS; i++) {
995 gs_output_semantic_name[i] = TGSI_SEMANTIC_GENERIC;
996 gs_output_semantic_index[i] = 0;
997 }
998
999 /*
1000 * Determine number of outputs, the (default) output register
1001 * mapping and the semantic information for each output.
1002 */
1003 for (attr = 0; attr < VARYING_SLOT_MAX; attr++) {
1004 if (stgp->Base.Base.OutputsWritten & BITFIELD64_BIT(attr)) {
1005 GLuint slot;
1006
1007 slot = gs_num_outputs;
1008 gs_num_outputs++;
1009 outputMapping[attr] = slot;
1010
1011 switch (attr) {
1012 case VARYING_SLOT_POS:
1013 assert(slot == 0);
1014 gs_output_semantic_name[slot] = TGSI_SEMANTIC_POSITION;
1015 gs_output_semantic_index[slot] = 0;
1016 break;
1017 case VARYING_SLOT_COL0:
1018 gs_output_semantic_name[slot] = TGSI_SEMANTIC_COLOR;
1019 gs_output_semantic_index[slot] = 0;
1020 break;
1021 case VARYING_SLOT_COL1:
1022 gs_output_semantic_name[slot] = TGSI_SEMANTIC_COLOR;
1023 gs_output_semantic_index[slot] = 1;
1024 break;
1025 case VARYING_SLOT_BFC0:
1026 gs_output_semantic_name[slot] = TGSI_SEMANTIC_BCOLOR;
1027 gs_output_semantic_index[slot] = 0;
1028 break;
1029 case VARYING_SLOT_BFC1:
1030 gs_output_semantic_name[slot] = TGSI_SEMANTIC_BCOLOR;
1031 gs_output_semantic_index[slot] = 1;
1032 break;
1033 case VARYING_SLOT_FOGC:
1034 gs_output_semantic_name[slot] = TGSI_SEMANTIC_FOG;
1035 gs_output_semantic_index[slot] = 0;
1036 break;
1037 case VARYING_SLOT_PSIZ:
1038 gs_output_semantic_name[slot] = TGSI_SEMANTIC_PSIZE;
1039 gs_output_semantic_index[slot] = 0;
1040 break;
1041 case VARYING_SLOT_CLIP_VERTEX:
1042 gs_output_semantic_name[slot] = TGSI_SEMANTIC_CLIPVERTEX;
1043 gs_output_semantic_index[slot] = 0;
1044 break;
1045 case VARYING_SLOT_CLIP_DIST0:
1046 gs_output_semantic_name[slot] = TGSI_SEMANTIC_CLIPDIST;
1047 gs_output_semantic_index[slot] = 0;
1048 break;
1049 case VARYING_SLOT_CLIP_DIST1:
1050 gs_output_semantic_name[slot] = TGSI_SEMANTIC_CLIPDIST;
1051 gs_output_semantic_index[slot] = 1;
1052 break;
1053 case VARYING_SLOT_LAYER:
1054 gs_output_semantic_name[slot] = TGSI_SEMANTIC_LAYER;
1055 gs_output_semantic_index[slot] = 0;
1056 break;
1057 case VARYING_SLOT_PRIMITIVE_ID:
1058 gs_output_semantic_name[slot] = TGSI_SEMANTIC_PRIMID;
1059 gs_output_semantic_index[slot] = 0;
1060 break;
1061 case VARYING_SLOT_VIEWPORT:
1062 gs_output_semantic_name[slot] = TGSI_SEMANTIC_VIEWPORT_INDEX;
1063 gs_output_semantic_index[slot] = 0;
1064 break;
1065 case VARYING_SLOT_TEX0:
1066 case VARYING_SLOT_TEX1:
1067 case VARYING_SLOT_TEX2:
1068 case VARYING_SLOT_TEX3:
1069 case VARYING_SLOT_TEX4:
1070 case VARYING_SLOT_TEX5:
1071 case VARYING_SLOT_TEX6:
1072 case VARYING_SLOT_TEX7:
1073 if (st->needs_texcoord_semantic) {
1074 gs_output_semantic_name[slot] = TGSI_SEMANTIC_TEXCOORD;
1075 gs_output_semantic_index[slot] = attr - VARYING_SLOT_TEX0;
1076 break;
1077 }
1078 /* fall through */
1079 case VARYING_SLOT_VAR0:
1080 default:
1081 assert(slot < Elements(gs_output_semantic_name));
1082 assert(attr >= VARYING_SLOT_VAR0);
1083 gs_output_semantic_name[slot] = TGSI_SEMANTIC_GENERIC;
1084 gs_output_semantic_index[slot] =
1085 st_get_generic_varying_index(st, attr);
1086 break;
1087 }
1088 }
1089 }
1090
1091 /* find max output slot referenced to compute gs_num_outputs */
1092 for (attr = 0; attr < VARYING_SLOT_MAX; attr++) {
1093 if (outputMapping[attr] != ~0U && outputMapping[attr] > maxSlot)
1094 maxSlot = outputMapping[attr];
1095 }
1096 gs_num_outputs = maxSlot + 1;
1097
1098 #if 0 /* debug */
1099 {
1100 GLuint i;
1101 printf("outputMapping? %d\n", outputMapping ? 1 : 0);
1102 if (outputMapping) {
1103 printf("attr -> slot\n");
1104 for (i = 0; i < 16; i++) {
1105 printf(" %2d %3d\n", i, outputMapping[i]);
1106 }
1107 }
1108 printf("slot sem_name sem_index\n");
1109 for (i = 0; i < gs_num_outputs; i++) {
1110 printf(" %2d %d %d\n",
1111 i,
1112 gs_output_semantic_name[i],
1113 gs_output_semantic_index[i]);
1114 }
1115 }
1116 #endif
1117
1118 ureg_property(ureg, TGSI_PROPERTY_GS_INPUT_PRIM, stgp->Base.InputType);
1119 ureg_property(ureg, TGSI_PROPERTY_GS_OUTPUT_PRIM, stgp->Base.OutputType);
1120 ureg_property(ureg, TGSI_PROPERTY_GS_MAX_OUTPUT_VERTICES,
1121 stgp->Base.VerticesOut);
1122 ureg_property(ureg, TGSI_PROPERTY_GS_INVOCATIONS, stgp->Base.Invocations);
1123
1124 if (stgp->glsl_to_tgsi)
1125 st_translate_program(st->ctx,
1126 TGSI_PROCESSOR_GEOMETRY,
1127 ureg,
1128 stgp->glsl_to_tgsi,
1129 &stgp->Base.Base,
1130 /* inputs */
1131 gs_num_inputs,
1132 inputMapping,
1133 input_semantic_name,
1134 input_semantic_index,
1135 NULL,
1136 NULL,
1137 /* outputs */
1138 gs_num_outputs,
1139 outputMapping,
1140 gs_output_semantic_name,
1141 gs_output_semantic_index,
1142 FALSE,
1143 FALSE);
1144 else
1145 st_translate_mesa_program(st->ctx,
1146 TGSI_PROCESSOR_GEOMETRY,
1147 ureg,
1148 &stgp->Base.Base,
1149 /* inputs */
1150 gs_num_inputs,
1151 inputMapping,
1152 input_semantic_name,
1153 input_semantic_index,
1154 NULL,
1155 /* outputs */
1156 gs_num_outputs,
1157 outputMapping,
1158 gs_output_semantic_name,
1159 gs_output_semantic_index,
1160 FALSE,
1161 FALSE);
1162
1163 state.tokens = ureg_get_tokens( ureg, NULL );
1164 ureg_destroy( ureg );
1165
1166 if (stgp->glsl_to_tgsi) {
1167 st_translate_stream_output_info(stgp->glsl_to_tgsi,
1168 outputMapping,
1169 &state.stream_output);
1170 }
1171
1172 if ((ST_DEBUG & DEBUG_TGSI) && (ST_DEBUG & DEBUG_MESA)) {
1173 _mesa_print_program(&stgp->Base.Base);
1174 debug_printf("\n");
1175 }
1176
1177 if (ST_DEBUG & DEBUG_TGSI) {
1178 tgsi_dump(state.tokens, 0);
1179 debug_printf("\n");
1180 }
1181
1182 /* fill in new variant */
1183 gpv->driver_shader = pipe->create_gs_state(pipe, &state);
1184 gpv->key = *key;
1185
1186 ureg_free_tokens(state.tokens);
1187 return gpv;
1188 }
1189
1190
1191 /**
1192 * Get/create geometry program variant.
1193 */
1194 struct st_gp_variant *
1195 st_get_gp_variant(struct st_context *st,
1196 struct st_geometry_program *stgp,
1197 const struct st_gp_variant_key *key)
1198 {
1199 struct st_gp_variant *gpv;
1200
1201 /* Search for existing variant */
1202 for (gpv = stgp->variants; gpv; gpv = gpv->next) {
1203 if (memcmp(&gpv->key, key, sizeof(*key)) == 0) {
1204 break;
1205 }
1206 }
1207
1208 if (!gpv) {
1209 /* create new */
1210 gpv = st_translate_geometry_program(st, stgp, key);
1211 if (gpv) {
1212 /* insert into list */
1213 gpv->next = stgp->variants;
1214 stgp->variants = gpv;
1215 }
1216 }
1217
1218 return gpv;
1219 }
1220
1221
1222
1223
1224 /**
1225 * Debug- print current shader text
1226 */
1227 void
1228 st_print_shaders(struct gl_context *ctx)
1229 {
1230 struct gl_shader_program **shProg = ctx->_Shader->CurrentProgram;
1231 unsigned j;
1232
1233 for (j = 0; j < 3; j++) {
1234 unsigned i;
1235
1236 if (shProg[j] == NULL)
1237 continue;
1238
1239 for (i = 0; i < shProg[j]->NumShaders; i++) {
1240 struct gl_shader *sh;
1241
1242 switch (shProg[j]->Shaders[i]->Type) {
1243 case GL_VERTEX_SHADER:
1244 sh = (i != 0) ? NULL : shProg[j]->Shaders[i];
1245 break;
1246 case GL_GEOMETRY_SHADER_ARB:
1247 sh = (i != 1) ? NULL : shProg[j]->Shaders[i];
1248 break;
1249 case GL_FRAGMENT_SHADER:
1250 sh = (i != 2) ? NULL : shProg[j]->Shaders[i];
1251 break;
1252 default:
1253 assert(0);
1254 sh = NULL;
1255 break;
1256 }
1257
1258 if (sh != NULL) {
1259 printf("GLSL shader %u of %u:\n", i, shProg[j]->NumShaders);
1260 printf("%s\n", sh->Source);
1261 }
1262 }
1263 }
1264 }
1265
1266
1267 /**
1268 * Vert/Geom/Frag programs have per-context variants. Free all the
1269 * variants attached to the given program which match the given context.
1270 */
1271 static void
1272 destroy_program_variants(struct st_context *st, struct gl_program *program)
1273 {
1274 if (!program || program == &_mesa_DummyProgram)
1275 return;
1276
1277 switch (program->Target) {
1278 case GL_VERTEX_PROGRAM_ARB:
1279 {
1280 struct st_vertex_program *stvp = (struct st_vertex_program *) program;
1281 struct st_vp_variant *vpv, **prevPtr = &stvp->variants;
1282
1283 for (vpv = stvp->variants; vpv; ) {
1284 struct st_vp_variant *next = vpv->next;
1285 if (vpv->key.st == st) {
1286 /* unlink from list */
1287 *prevPtr = next;
1288 /* destroy this variant */
1289 delete_vp_variant(st, vpv);
1290 }
1291 else {
1292 prevPtr = &vpv->next;
1293 }
1294 vpv = next;
1295 }
1296 }
1297 break;
1298 case GL_FRAGMENT_PROGRAM_ARB:
1299 {
1300 struct st_fragment_program *stfp =
1301 (struct st_fragment_program *) program;
1302 struct st_fp_variant *fpv, **prevPtr = &stfp->variants;
1303
1304 for (fpv = stfp->variants; fpv; ) {
1305 struct st_fp_variant *next = fpv->next;
1306 if (fpv->key.st == st) {
1307 /* unlink from list */
1308 *prevPtr = next;
1309 /* destroy this variant */
1310 delete_fp_variant(st, fpv);
1311 }
1312 else {
1313 prevPtr = &fpv->next;
1314 }
1315 fpv = next;
1316 }
1317 }
1318 break;
1319 case MESA_GEOMETRY_PROGRAM:
1320 {
1321 struct st_geometry_program *stgp =
1322 (struct st_geometry_program *) program;
1323 struct st_gp_variant *gpv, **prevPtr = &stgp->variants;
1324
1325 for (gpv = stgp->variants; gpv; ) {
1326 struct st_gp_variant *next = gpv->next;
1327 if (gpv->key.st == st) {
1328 /* unlink from list */
1329 *prevPtr = next;
1330 /* destroy this variant */
1331 delete_gp_variant(st, gpv);
1332 }
1333 else {
1334 prevPtr = &gpv->next;
1335 }
1336 gpv = next;
1337 }
1338 }
1339 break;
1340 default:
1341 _mesa_problem(NULL, "Unexpected program target 0x%x in "
1342 "destroy_program_variants_cb()", program->Target);
1343 }
1344 }
1345
1346
1347 /**
1348 * Callback for _mesa_HashWalk. Free all the shader's program variants
1349 * which match the given context.
1350 */
1351 static void
1352 destroy_shader_program_variants_cb(GLuint key, void *data, void *userData)
1353 {
1354 struct st_context *st = (struct st_context *) userData;
1355 struct gl_shader *shader = (struct gl_shader *) data;
1356
1357 switch (shader->Type) {
1358 case GL_SHADER_PROGRAM_MESA:
1359 {
1360 struct gl_shader_program *shProg = (struct gl_shader_program *) data;
1361 GLuint i;
1362
1363 for (i = 0; i < shProg->NumShaders; i++) {
1364 destroy_program_variants(st, shProg->Shaders[i]->Program);
1365 }
1366
1367 for (i = 0; i < Elements(shProg->_LinkedShaders); i++) {
1368 if (shProg->_LinkedShaders[i])
1369 destroy_program_variants(st, shProg->_LinkedShaders[i]->Program);
1370 }
1371 }
1372 break;
1373 case GL_VERTEX_SHADER:
1374 case GL_FRAGMENT_SHADER:
1375 case GL_GEOMETRY_SHADER:
1376 {
1377 destroy_program_variants(st, shader->Program);
1378 }
1379 break;
1380 default:
1381 assert(0);
1382 }
1383 }
1384
1385
1386 /**
1387 * Callback for _mesa_HashWalk. Free all the program variants which match
1388 * the given context.
1389 */
1390 static void
1391 destroy_program_variants_cb(GLuint key, void *data, void *userData)
1392 {
1393 struct st_context *st = (struct st_context *) userData;
1394 struct gl_program *program = (struct gl_program *) data;
1395 destroy_program_variants(st, program);
1396 }
1397
1398
1399 /**
1400 * Walk over all shaders and programs to delete any variants which
1401 * belong to the given context.
1402 * This is called during context tear-down.
1403 */
1404 void
1405 st_destroy_program_variants(struct st_context *st)
1406 {
1407 /* ARB vert/frag program */
1408 _mesa_HashWalk(st->ctx->Shared->Programs,
1409 destroy_program_variants_cb, st);
1410
1411 /* GLSL vert/frag/geom shaders */
1412 _mesa_HashWalk(st->ctx->Shared->ShaderObjects,
1413 destroy_shader_program_variants_cb, st);
1414 }
1415
1416
1417 /**
1418 * For debugging, print/dump the current vertex program.
1419 */
1420 void
1421 st_print_current_vertex_program(void)
1422 {
1423 GET_CURRENT_CONTEXT(ctx);
1424
1425 if (ctx->VertexProgram._Current) {
1426 struct st_vertex_program *stvp =
1427 (struct st_vertex_program *) ctx->VertexProgram._Current;
1428 struct st_vp_variant *stv;
1429
1430 debug_printf("Vertex program %u\n", stvp->Base.Base.Id);
1431
1432 for (stv = stvp->variants; stv; stv = stv->next) {
1433 debug_printf("variant %p\n", stv);
1434 tgsi_dump(stv->tgsi.tokens, 0);
1435 }
1436 }
1437 }