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