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