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