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