202f7cb711ef7f8a41c24122f2874d3290a04ba4
[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 varient. Note the caller must unlink
60 * the varient from the linked list.
61 */
62 static void
63 delete_vp_varient(struct st_context *st, struct st_vp_varient *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_vp_release_varients( struct st_context *st,
86 struct st_vertex_program *stvp )
87 {
88 struct st_vp_varient *vpv;
89
90 for (vpv = stvp->varients; vpv; ) {
91 struct st_vp_varient *next = vpv->next;
92 delete_vp_varient(st, vpv);
93 vpv = next;
94 }
95
96 stvp->varients = NULL;
97 }
98
99
100
101 /**
102 * Delete a fragment program varient. Note the caller must unlink
103 * the varient from the linked list.
104 */
105 static void
106 delete_fp_varient(struct st_context *st, struct st_fp_varient *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 varients of a fragment program.
117 */
118 void
119 st_fp_release_varients(struct st_context *st, struct st_fragment_program *stfp)
120 {
121 struct st_fp_varient *fpv;
122
123 for (fpv = stfp->varients; fpv; ) {
124 struct st_fp_varient *next = fpv->next;
125 delete_fp_varient(st, fpv);
126 fpv = next;
127 }
128
129 stfp->varients = NULL;
130 }
131
132
133 /**
134 * Delete a geometry program varient. Note the caller must unlink
135 * the varient from the linked list.
136 */
137 static void
138 delete_gp_varient(struct st_context *st, struct st_gp_varient *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 varients of a geometry program.
149 */
150 void
151 st_gp_release_varients(struct st_context *st, struct st_geometry_program *stgp)
152 {
153 struct st_gp_varient *gpv;
154
155 for (gpv = stgp->varients; gpv; ) {
156 struct st_gp_varient *next = gpv->next;
157 delete_gp_varient(st, gpv);
158 gpv = next;
159 }
160
161 stgp->varients = 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 varient.
280 */
281 static struct st_vp_varient *
282 st_translate_vertex_program(struct st_context *st,
283 struct st_vertex_program *stvp,
284 const struct st_vp_varient_key *key)
285 {
286 struct st_vp_varient *vpv = CALLOC_STRUCT(st_vp_varient);
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 =
319 st_translate_mesa_program(st->ctx,
320 TGSI_PROCESSOR_VERTEX,
321 ureg,
322 &stvp->Base.Base,
323 /* inputs */
324 vpv->num_inputs,
325 stvp->input_to_index,
326 NULL, /* input semantic name */
327 NULL, /* input semantic index */
328 NULL,
329 /* outputs */
330 num_outputs,
331 stvp->result_to_output,
332 stvp->output_semantic_name,
333 stvp->output_semantic_index,
334 key->passthrough_edgeflags );
335
336 if (error)
337 goto fail;
338
339 vpv->tgsi.tokens = ureg_get_tokens( ureg, NULL );
340 if (!vpv->tgsi.tokens)
341 goto fail;
342
343 ureg_destroy( ureg );
344
345 vpv->driver_shader = pipe->create_vs_state(pipe, &vpv->tgsi);
346
347 if (ST_DEBUG & DEBUG_TGSI) {
348 tgsi_dump( vpv->tgsi.tokens, 0 );
349 debug_printf("\n");
350 }
351
352 return vpv;
353
354 fail:
355 debug_printf("%s: failed to translate Mesa program:\n", __FUNCTION__);
356 _mesa_print_program(&stvp->Base.Base);
357 debug_assert(0);
358
359 ureg_destroy( ureg );
360 return NULL;
361 }
362
363
364 /**
365 * Find/create a vertex program varient.
366 */
367 struct st_vp_varient *
368 st_get_vp_varient(struct st_context *st,
369 struct st_vertex_program *stvp,
370 const struct st_vp_varient_key *key)
371 {
372 struct st_vp_varient *vpv;
373
374 /* Search for existing varient */
375 for (vpv = stvp->varients; vpv; vpv = vpv->next) {
376 if (memcmp(&vpv->key, key, sizeof(*key)) == 0) {
377 break;
378 }
379 }
380
381 if (!vpv) {
382 /* create now */
383 vpv = st_translate_vertex_program(st, stvp, key);
384 if (vpv) {
385 /* insert into list */
386 vpv->next = stvp->varients;
387 stvp->varients = vpv;
388 }
389 }
390
391 return vpv;
392 }
393
394
395 /**
396 * Translate a Mesa fragment shader into a TGSI shader using extra info in
397 * the key.
398 * \return new fragment program variant
399 */
400 static struct st_fp_varient *
401 st_translate_fragment_program(struct st_context *st,
402 struct st_fragment_program *stfp,
403 const struct st_fp_varient_key *key)
404 {
405 struct pipe_context *pipe = st->pipe;
406 struct st_fp_varient *varient = CALLOC_STRUCT(st_fp_varient);
407
408 if (!varient)
409 return NULL;
410
411 assert(!(key->bitmap && key->drawpixels));
412
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, &varient->bitmap_sampler);
419
420 varient->parameters = _mesa_clone_parameter_list(fp->Base.Parameters);
421 stfp = (struct 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 varient->parameters = _mesa_clone_parameter_list(fp->Base.Parameters);
435 }
436 stfp = (struct st_fragment_program *) fp;
437 }
438
439 if (!stfp->tgsi.tokens) {
440 /* need to translate Mesa instructions to TGSI now */
441 GLuint outputMapping[FRAG_RESULT_MAX];
442 GLuint inputMapping[FRAG_ATTRIB_MAX];
443 GLuint interpMode[PIPE_MAX_SHADER_INPUTS]; /* XXX size? */
444 GLuint attr;
445 enum pipe_error error;
446 const GLbitfield inputsRead = stfp->Base.Base.InputsRead;
447 struct ureg_program *ureg;
448
449 ubyte input_semantic_name[PIPE_MAX_SHADER_INPUTS];
450 ubyte input_semantic_index[PIPE_MAX_SHADER_INPUTS];
451 uint fs_num_inputs = 0;
452
453 ubyte fs_output_semantic_name[PIPE_MAX_SHADER_OUTPUTS];
454 ubyte fs_output_semantic_index[PIPE_MAX_SHADER_OUTPUTS];
455 uint fs_num_outputs = 0;
456
457
458 _mesa_remove_output_reads(&stfp->Base.Base, PROGRAM_OUTPUT);
459
460 /*
461 * Convert Mesa program inputs to TGSI input register semantics.
462 */
463 for (attr = 0; attr < FRAG_ATTRIB_MAX; attr++) {
464 if (inputsRead & (1 << attr)) {
465 const GLuint slot = fs_num_inputs++;
466
467 inputMapping[attr] = slot;
468
469 switch (attr) {
470 case FRAG_ATTRIB_WPOS:
471 input_semantic_name[slot] = TGSI_SEMANTIC_POSITION;
472 input_semantic_index[slot] = 0;
473 interpMode[slot] = TGSI_INTERPOLATE_LINEAR;
474 break;
475 case FRAG_ATTRIB_COL0:
476 input_semantic_name[slot] = TGSI_SEMANTIC_COLOR;
477 input_semantic_index[slot] = 0;
478 interpMode[slot] = TGSI_INTERPOLATE_LINEAR;
479 break;
480 case FRAG_ATTRIB_COL1:
481 input_semantic_name[slot] = TGSI_SEMANTIC_COLOR;
482 input_semantic_index[slot] = 1;
483 interpMode[slot] = TGSI_INTERPOLATE_LINEAR;
484 break;
485 case FRAG_ATTRIB_FOGC:
486 input_semantic_name[slot] = TGSI_SEMANTIC_FOG;
487 input_semantic_index[slot] = 0;
488 interpMode[slot] = TGSI_INTERPOLATE_PERSPECTIVE;
489 break;
490 case FRAG_ATTRIB_FACE:
491 input_semantic_name[slot] = TGSI_SEMANTIC_FACE;
492 input_semantic_index[slot] = 0;
493 interpMode[slot] = TGSI_INTERPOLATE_CONSTANT;
494 break;
495 /* In most cases, there is nothing special about these
496 * inputs, so adopt a convention to use the generic
497 * semantic name and the mesa FRAG_ATTRIB_ number as the
498 * index.
499 *
500 * All that is required is that the vertex shader labels
501 * its own outputs similarly, and that the vertex shader
502 * generates at least every output required by the
503 * fragment shader plus fixed-function hardware (such as
504 * BFC).
505 *
506 * There is no requirement that semantic indexes start at
507 * zero or be restricted to a particular range -- nobody
508 * should be building tables based on semantic index.
509 */
510 case FRAG_ATTRIB_PNTC:
511 case FRAG_ATTRIB_TEX0:
512 case FRAG_ATTRIB_TEX1:
513 case FRAG_ATTRIB_TEX2:
514 case FRAG_ATTRIB_TEX3:
515 case FRAG_ATTRIB_TEX4:
516 case FRAG_ATTRIB_TEX5:
517 case FRAG_ATTRIB_TEX6:
518 case FRAG_ATTRIB_TEX7:
519 case FRAG_ATTRIB_VAR0:
520 default:
521 /* Actually, let's try and zero-base this just for
522 * readability of the generated TGSI.
523 */
524 assert(attr >= FRAG_ATTRIB_TEX0);
525 input_semantic_index[slot] = (attr - FRAG_ATTRIB_TEX0);
526 input_semantic_name[slot] = TGSI_SEMANTIC_GENERIC;
527 if (attr == FRAG_ATTRIB_PNTC)
528 interpMode[slot] = TGSI_INTERPOLATE_LINEAR;
529 else
530 interpMode[slot] = TGSI_INTERPOLATE_PERSPECTIVE;
531 break;
532 }
533 }
534 else {
535 inputMapping[attr] = -1;
536 }
537 }
538
539 /*
540 * Semantics and mapping for outputs
541 */
542 {
543 uint numColors = 0;
544 GLbitfield64 outputsWritten = stfp->Base.Base.OutputsWritten;
545
546 /* if z is written, emit that first */
547 if (outputsWritten & BITFIELD64_BIT(FRAG_RESULT_DEPTH)) {
548 fs_output_semantic_name[fs_num_outputs] = TGSI_SEMANTIC_POSITION;
549 fs_output_semantic_index[fs_num_outputs] = 0;
550 outputMapping[FRAG_RESULT_DEPTH] = fs_num_outputs;
551 fs_num_outputs++;
552 outputsWritten &= ~(1 << FRAG_RESULT_DEPTH);
553 }
554
555 if (outputsWritten & BITFIELD64_BIT(FRAG_RESULT_STENCIL)) {
556 fs_output_semantic_name[fs_num_outputs] = TGSI_SEMANTIC_STENCIL;
557 fs_output_semantic_index[fs_num_outputs] = 0;
558 outputMapping[FRAG_RESULT_STENCIL] = fs_num_outputs;
559 fs_num_outputs++;
560 outputsWritten &= ~(1 << FRAG_RESULT_STENCIL);
561 }
562
563 /* handle remaning outputs (color) */
564 for (attr = 0; attr < FRAG_RESULT_MAX; attr++) {
565 if (outputsWritten & BITFIELD64_BIT(attr)) {
566 switch (attr) {
567 case FRAG_RESULT_DEPTH:
568 case FRAG_RESULT_STENCIL:
569 /* handled above */
570 assert(0);
571 break;
572 default:
573 assert(attr == FRAG_RESULT_COLOR ||
574 (FRAG_RESULT_DATA0 <= attr && attr < FRAG_RESULT_MAX));
575 fs_output_semantic_name[fs_num_outputs] = TGSI_SEMANTIC_COLOR;
576 fs_output_semantic_index[fs_num_outputs] = numColors;
577 outputMapping[attr] = fs_num_outputs;
578 numColors++;
579 break;
580 }
581
582 fs_num_outputs++;
583 }
584 }
585 }
586
587 ureg = ureg_create( TGSI_PROCESSOR_FRAGMENT );
588 if (ureg == NULL)
589 return NULL;
590
591 if (ST_DEBUG & DEBUG_MESA) {
592 _mesa_print_program(&stfp->Base.Base);
593 _mesa_print_program_parameters(st->ctx, &stfp->Base.Base);
594 debug_printf("\n");
595 }
596
597 error = st_translate_mesa_program(st->ctx,
598 TGSI_PROCESSOR_FRAGMENT,
599 ureg,
600 &stfp->Base.Base,
601 /* inputs */
602 fs_num_inputs,
603 inputMapping,
604 input_semantic_name,
605 input_semantic_index,
606 interpMode,
607 /* outputs */
608 fs_num_outputs,
609 outputMapping,
610 fs_output_semantic_name,
611 fs_output_semantic_index, FALSE );
612
613 stfp->tgsi.tokens = ureg_get_tokens( ureg, NULL );
614 ureg_destroy( ureg );
615 }
616
617 /* fill in varient */
618 varient->driver_shader = pipe->create_fs_state(pipe, &stfp->tgsi);
619 varient->key = *key;
620
621 if (ST_DEBUG & DEBUG_TGSI) {
622 tgsi_dump( stfp->tgsi.tokens, 0/*TGSI_DUMP_VERBOSE*/ );
623 debug_printf("\n");
624 }
625
626 return varient;
627 }
628
629
630 /**
631 * Translate fragment program if needed.
632 */
633 struct st_fp_varient *
634 st_get_fp_varient(struct st_context *st,
635 struct st_fragment_program *stfp,
636 const struct st_fp_varient_key *key)
637 {
638 struct st_fp_varient *fpv;
639
640 /* Search for existing varient */
641 for (fpv = stfp->varients; fpv; fpv = fpv->next) {
642 if (memcmp(&fpv->key, key, sizeof(*key)) == 0) {
643 break;
644 }
645 }
646
647 if (!fpv) {
648 /* create new */
649 fpv = st_translate_fragment_program(st, stfp, key);
650 if (fpv) {
651 /* insert into list */
652 fpv->next = stfp->varients;
653 stfp->varients = fpv;
654 }
655 }
656
657 return fpv;
658 }
659
660
661 /**
662 * Translate a geometry program to create a new varient.
663 */
664 static struct st_gp_varient *
665 st_translate_geometry_program(struct st_context *st,
666 struct st_geometry_program *stgp,
667 const struct st_gp_varient_key *key)
668 {
669 GLuint inputMapping[GEOM_ATTRIB_MAX];
670 GLuint outputMapping[GEOM_RESULT_MAX];
671 struct pipe_context *pipe = st->pipe;
672 enum pipe_error error;
673 GLuint attr;
674 const GLbitfield inputsRead = stgp->Base.Base.InputsRead;
675 GLuint vslot = 0;
676 GLuint num_generic = 0;
677
678 uint gs_num_inputs = 0;
679 uint gs_builtin_inputs = 0;
680 uint gs_array_offset = 0;
681
682 ubyte gs_output_semantic_name[PIPE_MAX_SHADER_OUTPUTS];
683 ubyte gs_output_semantic_index[PIPE_MAX_SHADER_OUTPUTS];
684 uint gs_num_outputs = 0;
685
686 GLint i;
687 GLuint maxSlot = 0;
688 struct ureg_program *ureg;
689
690 struct st_gp_varient *gpv;
691
692 gpv = CALLOC_STRUCT(st_gp_varient);
693 if (!gpv)
694 return NULL;
695
696 _mesa_remove_output_reads(&stgp->Base.Base, PROGRAM_OUTPUT);
697 _mesa_remove_output_reads(&stgp->Base.Base, PROGRAM_VARYING);
698
699 ureg = ureg_create( TGSI_PROCESSOR_GEOMETRY );
700 if (ureg == NULL) {
701 FREE(gpv);
702 return NULL;
703 }
704
705 /* which vertex output goes to the first geometry input */
706 vslot = 0;
707
708 memset(inputMapping, 0, sizeof(inputMapping));
709 memset(outputMapping, 0, sizeof(outputMapping));
710
711 /*
712 * Convert Mesa program inputs to TGSI input register semantics.
713 */
714 for (attr = 0; attr < GEOM_ATTRIB_MAX; attr++) {
715 if (inputsRead & (1 << attr)) {
716 const GLuint slot = gs_num_inputs;
717
718 gs_num_inputs++;
719
720 inputMapping[attr] = slot;
721
722 stgp->input_map[slot + gs_array_offset] = vslot - gs_builtin_inputs;
723 stgp->input_to_index[attr] = vslot;
724 stgp->index_to_input[vslot] = attr;
725 ++vslot;
726
727 if (attr != GEOM_ATTRIB_PRIMITIVE_ID) {
728 gs_array_offset += 2;
729 } else
730 ++gs_builtin_inputs;
731
732 #if 0
733 debug_printf("input map at %d = %d\n",
734 slot + gs_array_offset, stgp->input_map[slot + gs_array_offset]);
735 #endif
736
737 switch (attr) {
738 case GEOM_ATTRIB_PRIMITIVE_ID:
739 stgp->input_semantic_name[slot] = TGSI_SEMANTIC_PRIMID;
740 stgp->input_semantic_index[slot] = 0;
741 break;
742 case GEOM_ATTRIB_POSITION:
743 stgp->input_semantic_name[slot] = TGSI_SEMANTIC_POSITION;
744 stgp->input_semantic_index[slot] = 0;
745 break;
746 case GEOM_ATTRIB_COLOR0:
747 stgp->input_semantic_name[slot] = TGSI_SEMANTIC_COLOR;
748 stgp->input_semantic_index[slot] = 0;
749 break;
750 case GEOM_ATTRIB_COLOR1:
751 stgp->input_semantic_name[slot] = TGSI_SEMANTIC_COLOR;
752 stgp->input_semantic_index[slot] = 1;
753 break;
754 case GEOM_ATTRIB_FOG_FRAG_COORD:
755 stgp->input_semantic_name[slot] = TGSI_SEMANTIC_FOG;
756 stgp->input_semantic_index[slot] = 0;
757 break;
758 case GEOM_ATTRIB_TEX_COORD:
759 stgp->input_semantic_name[slot] = TGSI_SEMANTIC_GENERIC;
760 stgp->input_semantic_index[slot] = num_generic++;
761 break;
762 case GEOM_ATTRIB_VAR0:
763 /* fall-through */
764 default:
765 stgp->input_semantic_name[slot] = TGSI_SEMANTIC_GENERIC;
766 stgp->input_semantic_index[slot] = num_generic++;
767 }
768 }
769 }
770
771 /* initialize output semantics to defaults */
772 for (i = 0; i < PIPE_MAX_SHADER_OUTPUTS; i++) {
773 gs_output_semantic_name[i] = TGSI_SEMANTIC_GENERIC;
774 gs_output_semantic_index[i] = 0;
775 }
776
777 num_generic = 0;
778 /*
779 * Determine number of outputs, the (default) output register
780 * mapping and the semantic information for each output.
781 */
782 for (attr = 0; attr < GEOM_RESULT_MAX; attr++) {
783 if (stgp->Base.Base.OutputsWritten & (1 << attr)) {
784 GLuint slot;
785
786 slot = gs_num_outputs;
787 gs_num_outputs++;
788 outputMapping[attr] = slot;
789
790 switch (attr) {
791 case GEOM_RESULT_POS:
792 assert(slot == 0);
793 gs_output_semantic_name[slot] = TGSI_SEMANTIC_POSITION;
794 gs_output_semantic_index[slot] = 0;
795 break;
796 case GEOM_RESULT_COL0:
797 gs_output_semantic_name[slot] = TGSI_SEMANTIC_COLOR;
798 gs_output_semantic_index[slot] = 0;
799 break;
800 case GEOM_RESULT_COL1:
801 gs_output_semantic_name[slot] = TGSI_SEMANTIC_COLOR;
802 gs_output_semantic_index[slot] = 1;
803 break;
804 case GEOM_RESULT_SCOL0:
805 gs_output_semantic_name[slot] = TGSI_SEMANTIC_BCOLOR;
806 gs_output_semantic_index[slot] = 0;
807 break;
808 case GEOM_RESULT_SCOL1:
809 gs_output_semantic_name[slot] = TGSI_SEMANTIC_BCOLOR;
810 gs_output_semantic_index[slot] = 1;
811 break;
812 case GEOM_RESULT_FOGC:
813 gs_output_semantic_name[slot] = TGSI_SEMANTIC_FOG;
814 gs_output_semantic_index[slot] = 0;
815 break;
816 case GEOM_RESULT_PSIZ:
817 gs_output_semantic_name[slot] = TGSI_SEMANTIC_PSIZE;
818 gs_output_semantic_index[slot] = 0;
819 break;
820 case GEOM_RESULT_TEX0:
821 case GEOM_RESULT_TEX1:
822 case GEOM_RESULT_TEX2:
823 case GEOM_RESULT_TEX3:
824 case GEOM_RESULT_TEX4:
825 case GEOM_RESULT_TEX5:
826 case GEOM_RESULT_TEX6:
827 case GEOM_RESULT_TEX7:
828 /* fall-through */
829 case GEOM_RESULT_VAR0:
830 /* fall-through */
831 default:
832 assert(slot < Elements(gs_output_semantic_name));
833 /* use default semantic info */
834 gs_output_semantic_name[slot] = TGSI_SEMANTIC_GENERIC;
835 gs_output_semantic_index[slot] = num_generic++;
836 }
837 }
838 }
839
840 assert(gs_output_semantic_name[0] == TGSI_SEMANTIC_POSITION);
841
842 /* find max output slot referenced to compute gs_num_outputs */
843 for (attr = 0; attr < GEOM_RESULT_MAX; attr++) {
844 if (outputMapping[attr] != ~0 && outputMapping[attr] > maxSlot)
845 maxSlot = outputMapping[attr];
846 }
847 gs_num_outputs = maxSlot + 1;
848
849 #if 0 /* debug */
850 {
851 GLuint i;
852 printf("outputMapping? %d\n", outputMapping ? 1 : 0);
853 if (outputMapping) {
854 printf("attr -> slot\n");
855 for (i = 0; i < 16; i++) {
856 printf(" %2d %3d\n", i, outputMapping[i]);
857 }
858 }
859 printf("slot sem_name sem_index\n");
860 for (i = 0; i < gs_num_outputs; i++) {
861 printf(" %2d %d %d\n",
862 i,
863 gs_output_semantic_name[i],
864 gs_output_semantic_index[i]);
865 }
866 }
867 #endif
868
869 /* free old shader state, if any */
870 if (stgp->tgsi.tokens) {
871 st_free_tokens(stgp->tgsi.tokens);
872 stgp->tgsi.tokens = NULL;
873 }
874
875 ureg_property_gs_input_prim(ureg, stgp->Base.InputType);
876 ureg_property_gs_output_prim(ureg, stgp->Base.OutputType);
877 ureg_property_gs_max_vertices(ureg, stgp->Base.VerticesOut);
878
879 error = st_translate_mesa_program(st->ctx,
880 TGSI_PROCESSOR_GEOMETRY,
881 ureg,
882 &stgp->Base.Base,
883 /* inputs */
884 gs_num_inputs,
885 inputMapping,
886 stgp->input_semantic_name,
887 stgp->input_semantic_index,
888 NULL,
889 /* outputs */
890 gs_num_outputs,
891 outputMapping,
892 gs_output_semantic_name,
893 gs_output_semantic_index,
894 FALSE);
895
896 stgp->num_inputs = gs_num_inputs;
897 stgp->tgsi.tokens = ureg_get_tokens( ureg, NULL );
898 ureg_destroy( ureg );
899
900 /* fill in new varient */
901 gpv->driver_shader = pipe->create_gs_state(pipe, &stgp->tgsi);
902 gpv->key = *key;
903
904 if ((ST_DEBUG & DEBUG_TGSI) && (ST_DEBUG & DEBUG_MESA)) {
905 _mesa_print_program(&stgp->Base.Base);
906 debug_printf("\n");
907 }
908
909 if (ST_DEBUG & DEBUG_TGSI) {
910 tgsi_dump(stgp->tgsi.tokens, 0);
911 debug_printf("\n");
912 }
913
914 return gpv;
915 }
916
917
918 /**
919 * Get/create geometry program variant.
920 */
921 struct st_gp_varient *
922 st_get_gp_varient(struct st_context *st,
923 struct st_geometry_program *stgp,
924 const struct st_gp_varient_key *key)
925 {
926 struct st_gp_varient *gpv;
927
928 /* Search for existing varient */
929 for (gpv = stgp->varients; gpv; gpv = gpv->next) {
930 if (memcmp(&gpv->key, key, sizeof(*key)) == 0) {
931 break;
932 }
933 }
934
935 if (!gpv) {
936 /* create new */
937 gpv = st_translate_geometry_program(st, stgp, key);
938 if (gpv) {
939 /* insert into list */
940 gpv->next = stgp->varients;
941 stgp->varients = gpv;
942 }
943 }
944
945 return gpv;
946 }
947
948
949
950
951 /**
952 * Debug- print current shader text
953 */
954 void
955 st_print_shaders(struct gl_context *ctx)
956 {
957 struct gl_shader_program *shProg[3] = {
958 ctx->Shader.CurrentVertexProgram,
959 ctx->Shader.CurrentGeometryProgram,
960 ctx->Shader.CurrentFragmentProgram,
961 };
962 unsigned j;
963
964 for (j = 0; j < 3; j++) {
965 unsigned i;
966
967 if (shProg[j] == NULL)
968 continue;
969
970 for (i = 0; i < shProg[j]->NumShaders; i++) {
971 struct gl_shader *sh;
972
973 switch (shProg[j]->Shaders[i]->Type) {
974 case GL_VERTEX_SHADER:
975 sh = (i != 0) ? NULL : shProg[j]->Shaders[i];
976 break;
977 case GL_GEOMETRY_SHADER_ARB:
978 sh = (i != 1) ? NULL : shProg[j]->Shaders[i];
979 break;
980 case GL_FRAGMENT_SHADER:
981 sh = (i != 2) ? NULL : shProg[j]->Shaders[i];
982 break;
983 default:
984 assert(0);
985 sh = NULL;
986 break;
987 }
988
989 if (sh != NULL) {
990 printf("GLSL shader %u of %u:\n", i, shProg[j]->NumShaders);
991 printf("%s\n", sh->Source);
992 }
993 }
994 }
995 }
996
997
998 /**
999 * Vert/Geom/Frag programs have per-context variants. Free all the
1000 * variants attached to the given program which match the given context.
1001 */
1002 static void
1003 destroy_program_variants(struct st_context *st, struct gl_program *program)
1004 {
1005 if (!program)
1006 return;
1007
1008 switch (program->Target) {
1009 case GL_VERTEX_PROGRAM_ARB:
1010 {
1011 struct st_vertex_program *stvp = (struct st_vertex_program *) program;
1012 struct st_vp_varient *vpv, **prev = &stvp->varients;
1013
1014 for (vpv = stvp->varients; vpv; ) {
1015 struct st_vp_varient *next = vpv->next;
1016 if (vpv->key.st == st) {
1017 /* unlink from list */
1018 *prev = next;
1019 /* destroy this variant */
1020 delete_vp_varient(st, vpv);
1021 }
1022 else {
1023 prev = &vpv;
1024 }
1025 vpv = next;
1026 }
1027 }
1028 break;
1029 case GL_FRAGMENT_PROGRAM_ARB:
1030 {
1031 struct st_fragment_program *stfp =
1032 (struct st_fragment_program *) program;
1033 struct st_fp_varient *fpv, **prev = &stfp->varients;
1034
1035 for (fpv = stfp->varients; fpv; ) {
1036 struct st_fp_varient *next = fpv->next;
1037 if (fpv->key.st == st) {
1038 /* unlink from list */
1039 *prev = next;
1040 /* destroy this variant */
1041 delete_fp_varient(st, fpv);
1042 }
1043 else {
1044 prev = &fpv;
1045 }
1046 fpv = next;
1047 }
1048 }
1049 break;
1050 default:
1051 _mesa_problem(NULL, "Unexpected program target in "
1052 "destroy_program_variants_cb()");
1053 }
1054 }
1055
1056
1057 /**
1058 * Callback for _mesa_HashWalk. Free all the shader's program variants
1059 * which match the given context.
1060 */
1061 static void
1062 destroy_shader_program_variants_cb(GLuint key, void *data, void *userData)
1063 {
1064 struct st_context *st = (struct st_context *) userData;
1065 struct gl_shader *shader = (struct gl_shader *) data;
1066
1067 switch (shader->Type) {
1068 case GL_SHADER_PROGRAM_MESA:
1069 {
1070 struct gl_shader_program *shProg = (struct gl_shader_program *) data;
1071 GLuint i;
1072
1073 for (i = 0; i < shProg->NumShaders; i++) {
1074 destroy_program_variants(st, shProg->Shaders[i]->Program);
1075 }
1076
1077 destroy_program_variants(st, (struct gl_program *)
1078 shProg->VertexProgram);
1079 destroy_program_variants(st, (struct gl_program *)
1080 shProg->FragmentProgram);
1081 destroy_program_variants(st, (struct gl_program *)
1082 shProg->GeometryProgram);
1083 }
1084 break;
1085 case GL_VERTEX_SHADER:
1086 case GL_FRAGMENT_SHADER:
1087 case GL_GEOMETRY_SHADER:
1088 {
1089 destroy_program_variants(st, shader->Program);
1090 }
1091 break;
1092 default:
1093 assert(0);
1094 }
1095 }
1096
1097
1098 /**
1099 * Callback for _mesa_HashWalk. Free all the program variants which match
1100 * the given context.
1101 */
1102 static void
1103 destroy_program_variants_cb(GLuint key, void *data, void *userData)
1104 {
1105 struct st_context *st = (struct st_context *) userData;
1106 struct gl_program *program = (struct gl_program *) data;
1107 destroy_program_variants(st, program);
1108 }
1109
1110
1111 /**
1112 * Walk over all shaders and programs to delete any variants which
1113 * belong to the given context.
1114 * This is called during context tear-down.
1115 */
1116 void
1117 st_destroy_program_variants(struct st_context *st)
1118 {
1119 /* ARB vert/frag program */
1120 _mesa_HashWalk(st->ctx->Shared->Programs,
1121 destroy_program_variants_cb, st);
1122
1123 /* GLSL vert/frag/geom shaders */
1124 _mesa_HashWalk(st->ctx->Shared->ShaderObjects,
1125 destroy_shader_program_variants_cb, st);
1126 }