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