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