st/mesa: cleanup st_translate_geometry_program
[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
873 uint gs_num_inputs = 0;
874
875 ubyte input_semantic_name[PIPE_MAX_SHADER_INPUTS];
876 ubyte input_semantic_index[PIPE_MAX_SHADER_INPUTS];
877
878 ubyte gs_output_semantic_name[PIPE_MAX_SHADER_OUTPUTS];
879 ubyte gs_output_semantic_index[PIPE_MAX_SHADER_OUTPUTS];
880 uint gs_num_outputs = 0;
881
882 GLint i;
883 struct ureg_program *ureg;
884 struct pipe_shader_state state = {0};
885 struct st_gp_variant *gpv;
886
887 gpv = CALLOC_STRUCT(st_gp_variant);
888 if (!gpv)
889 return NULL;
890
891 ureg = ureg_create(TGSI_PROCESSOR_GEOMETRY);
892 if (ureg == NULL) {
893 free(gpv);
894 return NULL;
895 }
896
897 memset(inputMapping, 0, sizeof(inputMapping));
898 memset(outputMapping, 0, sizeof(outputMapping));
899
900 /*
901 * Convert Mesa program inputs to TGSI input register semantics.
902 */
903 for (attr = 0; attr < VARYING_SLOT_MAX; attr++) {
904 if ((stgp->Base.Base.InputsRead & BITFIELD64_BIT(attr)) != 0) {
905 const GLuint slot = gs_num_inputs++;
906
907 inputMapping[attr] = slot;
908
909 switch (attr) {
910 case VARYING_SLOT_PRIMITIVE_ID:
911 input_semantic_name[slot] = TGSI_SEMANTIC_PRIMID;
912 input_semantic_index[slot] = 0;
913 break;
914 case VARYING_SLOT_POS:
915 input_semantic_name[slot] = TGSI_SEMANTIC_POSITION;
916 input_semantic_index[slot] = 0;
917 break;
918 case VARYING_SLOT_COL0:
919 input_semantic_name[slot] = TGSI_SEMANTIC_COLOR;
920 input_semantic_index[slot] = 0;
921 break;
922 case VARYING_SLOT_COL1:
923 input_semantic_name[slot] = TGSI_SEMANTIC_COLOR;
924 input_semantic_index[slot] = 1;
925 break;
926 case VARYING_SLOT_FOGC:
927 input_semantic_name[slot] = TGSI_SEMANTIC_FOG;
928 input_semantic_index[slot] = 0;
929 break;
930 case VARYING_SLOT_CLIP_VERTEX:
931 input_semantic_name[slot] = TGSI_SEMANTIC_CLIPVERTEX;
932 input_semantic_index[slot] = 0;
933 break;
934 case VARYING_SLOT_CLIP_DIST0:
935 input_semantic_name[slot] = TGSI_SEMANTIC_CLIPDIST;
936 input_semantic_index[slot] = 0;
937 break;
938 case VARYING_SLOT_CLIP_DIST1:
939 input_semantic_name[slot] = TGSI_SEMANTIC_CLIPDIST;
940 input_semantic_index[slot] = 1;
941 break;
942 case VARYING_SLOT_PSIZ:
943 input_semantic_name[slot] = TGSI_SEMANTIC_PSIZE;
944 input_semantic_index[slot] = 0;
945 break;
946 case VARYING_SLOT_TEX0:
947 case VARYING_SLOT_TEX1:
948 case VARYING_SLOT_TEX2:
949 case VARYING_SLOT_TEX3:
950 case VARYING_SLOT_TEX4:
951 case VARYING_SLOT_TEX5:
952 case VARYING_SLOT_TEX6:
953 case VARYING_SLOT_TEX7:
954 if (st->needs_texcoord_semantic) {
955 input_semantic_name[slot] = TGSI_SEMANTIC_TEXCOORD;
956 input_semantic_index[slot] = attr - VARYING_SLOT_TEX0;
957 break;
958 }
959 /* fall through */
960 case VARYING_SLOT_VAR0:
961 default:
962 assert(attr >= VARYING_SLOT_VAR0 && attr < VARYING_SLOT_MAX);
963 input_semantic_name[slot] = TGSI_SEMANTIC_GENERIC;
964 input_semantic_index[slot] =
965 st_get_generic_varying_index(st, attr);
966 break;
967 }
968 }
969 }
970
971 /* initialize output semantics to defaults */
972 for (i = 0; i < PIPE_MAX_SHADER_OUTPUTS; i++) {
973 gs_output_semantic_name[i] = TGSI_SEMANTIC_GENERIC;
974 gs_output_semantic_index[i] = 0;
975 }
976
977 /*
978 * Determine number of outputs, the (default) output register
979 * mapping and the semantic information for each output.
980 */
981 for (attr = 0; attr < VARYING_SLOT_MAX; attr++) {
982 if (stgp->Base.Base.OutputsWritten & BITFIELD64_BIT(attr)) {
983 GLuint slot = gs_num_outputs++;
984
985 outputMapping[attr] = slot;
986
987 switch (attr) {
988 case VARYING_SLOT_POS:
989 assert(slot == 0);
990 gs_output_semantic_name[slot] = TGSI_SEMANTIC_POSITION;
991 gs_output_semantic_index[slot] = 0;
992 break;
993 case VARYING_SLOT_COL0:
994 gs_output_semantic_name[slot] = TGSI_SEMANTIC_COLOR;
995 gs_output_semantic_index[slot] = 0;
996 break;
997 case VARYING_SLOT_COL1:
998 gs_output_semantic_name[slot] = TGSI_SEMANTIC_COLOR;
999 gs_output_semantic_index[slot] = 1;
1000 break;
1001 case VARYING_SLOT_BFC0:
1002 gs_output_semantic_name[slot] = TGSI_SEMANTIC_BCOLOR;
1003 gs_output_semantic_index[slot] = 0;
1004 break;
1005 case VARYING_SLOT_BFC1:
1006 gs_output_semantic_name[slot] = TGSI_SEMANTIC_BCOLOR;
1007 gs_output_semantic_index[slot] = 1;
1008 break;
1009 case VARYING_SLOT_FOGC:
1010 gs_output_semantic_name[slot] = TGSI_SEMANTIC_FOG;
1011 gs_output_semantic_index[slot] = 0;
1012 break;
1013 case VARYING_SLOT_PSIZ:
1014 gs_output_semantic_name[slot] = TGSI_SEMANTIC_PSIZE;
1015 gs_output_semantic_index[slot] = 0;
1016 break;
1017 case VARYING_SLOT_CLIP_VERTEX:
1018 gs_output_semantic_name[slot] = TGSI_SEMANTIC_CLIPVERTEX;
1019 gs_output_semantic_index[slot] = 0;
1020 break;
1021 case VARYING_SLOT_CLIP_DIST0:
1022 gs_output_semantic_name[slot] = TGSI_SEMANTIC_CLIPDIST;
1023 gs_output_semantic_index[slot] = 0;
1024 break;
1025 case VARYING_SLOT_CLIP_DIST1:
1026 gs_output_semantic_name[slot] = TGSI_SEMANTIC_CLIPDIST;
1027 gs_output_semantic_index[slot] = 1;
1028 break;
1029 case VARYING_SLOT_LAYER:
1030 gs_output_semantic_name[slot] = TGSI_SEMANTIC_LAYER;
1031 gs_output_semantic_index[slot] = 0;
1032 break;
1033 case VARYING_SLOT_PRIMITIVE_ID:
1034 gs_output_semantic_name[slot] = TGSI_SEMANTIC_PRIMID;
1035 gs_output_semantic_index[slot] = 0;
1036 break;
1037 case VARYING_SLOT_VIEWPORT:
1038 gs_output_semantic_name[slot] = TGSI_SEMANTIC_VIEWPORT_INDEX;
1039 gs_output_semantic_index[slot] = 0;
1040 break;
1041 case VARYING_SLOT_TEX0:
1042 case VARYING_SLOT_TEX1:
1043 case VARYING_SLOT_TEX2:
1044 case VARYING_SLOT_TEX3:
1045 case VARYING_SLOT_TEX4:
1046 case VARYING_SLOT_TEX5:
1047 case VARYING_SLOT_TEX6:
1048 case VARYING_SLOT_TEX7:
1049 if (st->needs_texcoord_semantic) {
1050 gs_output_semantic_name[slot] = TGSI_SEMANTIC_TEXCOORD;
1051 gs_output_semantic_index[slot] = attr - VARYING_SLOT_TEX0;
1052 break;
1053 }
1054 /* fall through */
1055 case VARYING_SLOT_VAR0:
1056 default:
1057 assert(slot < Elements(gs_output_semantic_name));
1058 assert(attr >= VARYING_SLOT_VAR0);
1059 gs_output_semantic_name[slot] = TGSI_SEMANTIC_GENERIC;
1060 gs_output_semantic_index[slot] =
1061 st_get_generic_varying_index(st, attr);
1062 break;
1063 }
1064 }
1065 }
1066
1067 ureg_property(ureg, TGSI_PROPERTY_GS_INPUT_PRIM, stgp->Base.InputType);
1068 ureg_property(ureg, TGSI_PROPERTY_GS_OUTPUT_PRIM, stgp->Base.OutputType);
1069 ureg_property(ureg, TGSI_PROPERTY_GS_MAX_OUTPUT_VERTICES,
1070 stgp->Base.VerticesOut);
1071 ureg_property(ureg, TGSI_PROPERTY_GS_INVOCATIONS, stgp->Base.Invocations);
1072
1073 st_translate_program(st->ctx,
1074 TGSI_PROCESSOR_GEOMETRY,
1075 ureg,
1076 stgp->glsl_to_tgsi,
1077 &stgp->Base.Base,
1078 /* inputs */
1079 gs_num_inputs,
1080 inputMapping,
1081 input_semantic_name,
1082 input_semantic_index,
1083 NULL,
1084 NULL,
1085 /* outputs */
1086 gs_num_outputs,
1087 outputMapping,
1088 gs_output_semantic_name,
1089 gs_output_semantic_index,
1090 FALSE,
1091 FALSE);
1092
1093 state.tokens = ureg_get_tokens(ureg, NULL);
1094 ureg_destroy(ureg);
1095
1096 st_translate_stream_output_info(stgp->glsl_to_tgsi,
1097 outputMapping,
1098 &state.stream_output);
1099
1100 if ((ST_DEBUG & DEBUG_TGSI) && (ST_DEBUG & DEBUG_MESA)) {
1101 _mesa_print_program(&stgp->Base.Base);
1102 debug_printf("\n");
1103 }
1104
1105 if (ST_DEBUG & DEBUG_TGSI) {
1106 tgsi_dump(state.tokens, 0);
1107 debug_printf("\n");
1108 }
1109
1110 /* fill in new variant */
1111 gpv->driver_shader = pipe->create_gs_state(pipe, &state);
1112 gpv->key = *key;
1113
1114 ureg_free_tokens(state.tokens);
1115 return gpv;
1116 }
1117
1118
1119 /**
1120 * Get/create geometry program variant.
1121 */
1122 struct st_gp_variant *
1123 st_get_gp_variant(struct st_context *st,
1124 struct st_geometry_program *stgp,
1125 const struct st_gp_variant_key *key)
1126 {
1127 struct st_gp_variant *gpv;
1128
1129 /* Search for existing variant */
1130 for (gpv = stgp->variants; gpv; gpv = gpv->next) {
1131 if (memcmp(&gpv->key, key, sizeof(*key)) == 0) {
1132 break;
1133 }
1134 }
1135
1136 if (!gpv) {
1137 /* create new */
1138 gpv = st_translate_geometry_program(st, stgp, key);
1139 if (gpv) {
1140 /* insert into list */
1141 gpv->next = stgp->variants;
1142 stgp->variants = gpv;
1143 }
1144 }
1145
1146 return gpv;
1147 }
1148
1149
1150
1151
1152 /**
1153 * Debug- print current shader text
1154 */
1155 void
1156 st_print_shaders(struct gl_context *ctx)
1157 {
1158 struct gl_shader_program **shProg = ctx->_Shader->CurrentProgram;
1159 unsigned j;
1160
1161 for (j = 0; j < 3; j++) {
1162 unsigned i;
1163
1164 if (shProg[j] == NULL)
1165 continue;
1166
1167 for (i = 0; i < shProg[j]->NumShaders; i++) {
1168 struct gl_shader *sh;
1169
1170 switch (shProg[j]->Shaders[i]->Type) {
1171 case GL_VERTEX_SHADER:
1172 sh = (i != 0) ? NULL : shProg[j]->Shaders[i];
1173 break;
1174 case GL_GEOMETRY_SHADER_ARB:
1175 sh = (i != 1) ? NULL : shProg[j]->Shaders[i];
1176 break;
1177 case GL_FRAGMENT_SHADER:
1178 sh = (i != 2) ? NULL : shProg[j]->Shaders[i];
1179 break;
1180 default:
1181 assert(0);
1182 sh = NULL;
1183 break;
1184 }
1185
1186 if (sh != NULL) {
1187 printf("GLSL shader %u of %u:\n", i, shProg[j]->NumShaders);
1188 printf("%s\n", sh->Source);
1189 }
1190 }
1191 }
1192 }
1193
1194
1195 /**
1196 * Vert/Geom/Frag programs have per-context variants. Free all the
1197 * variants attached to the given program which match the given context.
1198 */
1199 static void
1200 destroy_program_variants(struct st_context *st, struct gl_program *program)
1201 {
1202 if (!program || program == &_mesa_DummyProgram)
1203 return;
1204
1205 switch (program->Target) {
1206 case GL_VERTEX_PROGRAM_ARB:
1207 {
1208 struct st_vertex_program *stvp = (struct st_vertex_program *) program;
1209 struct st_vp_variant *vpv, **prevPtr = &stvp->variants;
1210
1211 for (vpv = stvp->variants; vpv; ) {
1212 struct st_vp_variant *next = vpv->next;
1213 if (vpv->key.st == st) {
1214 /* unlink from list */
1215 *prevPtr = next;
1216 /* destroy this variant */
1217 delete_vp_variant(st, vpv);
1218 }
1219 else {
1220 prevPtr = &vpv->next;
1221 }
1222 vpv = next;
1223 }
1224 }
1225 break;
1226 case GL_FRAGMENT_PROGRAM_ARB:
1227 {
1228 struct st_fragment_program *stfp =
1229 (struct st_fragment_program *) program;
1230 struct st_fp_variant *fpv, **prevPtr = &stfp->variants;
1231
1232 for (fpv = stfp->variants; fpv; ) {
1233 struct st_fp_variant *next = fpv->next;
1234 if (fpv->key.st == st) {
1235 /* unlink from list */
1236 *prevPtr = next;
1237 /* destroy this variant */
1238 delete_fp_variant(st, fpv);
1239 }
1240 else {
1241 prevPtr = &fpv->next;
1242 }
1243 fpv = next;
1244 }
1245 }
1246 break;
1247 case MESA_GEOMETRY_PROGRAM:
1248 {
1249 struct st_geometry_program *stgp =
1250 (struct st_geometry_program *) program;
1251 struct st_gp_variant *gpv, **prevPtr = &stgp->variants;
1252
1253 for (gpv = stgp->variants; gpv; ) {
1254 struct st_gp_variant *next = gpv->next;
1255 if (gpv->key.st == st) {
1256 /* unlink from list */
1257 *prevPtr = next;
1258 /* destroy this variant */
1259 delete_gp_variant(st, gpv);
1260 }
1261 else {
1262 prevPtr = &gpv->next;
1263 }
1264 gpv = next;
1265 }
1266 }
1267 break;
1268 default:
1269 _mesa_problem(NULL, "Unexpected program target 0x%x in "
1270 "destroy_program_variants_cb()", program->Target);
1271 }
1272 }
1273
1274
1275 /**
1276 * Callback for _mesa_HashWalk. Free all the shader's program variants
1277 * which match the given context.
1278 */
1279 static void
1280 destroy_shader_program_variants_cb(GLuint key, void *data, void *userData)
1281 {
1282 struct st_context *st = (struct st_context *) userData;
1283 struct gl_shader *shader = (struct gl_shader *) data;
1284
1285 switch (shader->Type) {
1286 case GL_SHADER_PROGRAM_MESA:
1287 {
1288 struct gl_shader_program *shProg = (struct gl_shader_program *) data;
1289 GLuint i;
1290
1291 for (i = 0; i < shProg->NumShaders; i++) {
1292 destroy_program_variants(st, shProg->Shaders[i]->Program);
1293 }
1294
1295 for (i = 0; i < Elements(shProg->_LinkedShaders); i++) {
1296 if (shProg->_LinkedShaders[i])
1297 destroy_program_variants(st, shProg->_LinkedShaders[i]->Program);
1298 }
1299 }
1300 break;
1301 case GL_VERTEX_SHADER:
1302 case GL_FRAGMENT_SHADER:
1303 case GL_GEOMETRY_SHADER:
1304 {
1305 destroy_program_variants(st, shader->Program);
1306 }
1307 break;
1308 default:
1309 assert(0);
1310 }
1311 }
1312
1313
1314 /**
1315 * Callback for _mesa_HashWalk. Free all the program variants which match
1316 * the given context.
1317 */
1318 static void
1319 destroy_program_variants_cb(GLuint key, void *data, void *userData)
1320 {
1321 struct st_context *st = (struct st_context *) userData;
1322 struct gl_program *program = (struct gl_program *) data;
1323 destroy_program_variants(st, program);
1324 }
1325
1326
1327 /**
1328 * Walk over all shaders and programs to delete any variants which
1329 * belong to the given context.
1330 * This is called during context tear-down.
1331 */
1332 void
1333 st_destroy_program_variants(struct st_context *st)
1334 {
1335 /* ARB vert/frag program */
1336 _mesa_HashWalk(st->ctx->Shared->Programs,
1337 destroy_program_variants_cb, st);
1338
1339 /* GLSL vert/frag/geom shaders */
1340 _mesa_HashWalk(st->ctx->Shared->ShaderObjects,
1341 destroy_shader_program_variants_cb, st);
1342 }
1343
1344
1345 /**
1346 * For debugging, print/dump the current vertex program.
1347 */
1348 void
1349 st_print_current_vertex_program(void)
1350 {
1351 GET_CURRENT_CONTEXT(ctx);
1352
1353 if (ctx->VertexProgram._Current) {
1354 struct st_vertex_program *stvp =
1355 (struct st_vertex_program *) ctx->VertexProgram._Current;
1356 struct st_vp_variant *stv;
1357
1358 debug_printf("Vertex program %u\n", stvp->Base.Base.Id);
1359
1360 for (stv = stvp->variants; stv; stv = stv->next) {
1361 debug_printf("variant %p\n", stv);
1362 tgsi_dump(stv->tgsi.tokens, 0);
1363 }
1364 }
1365 }