Merge remote branch 'origin/master' into nv50-compiler
[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/mtypes.h"
36 #include "program/prog_print.h"
37 #include "program/programopt.h"
38
39 #include "pipe/p_context.h"
40 #include "pipe/p_defines.h"
41 #include "pipe/p_shader_tokens.h"
42 #include "draw/draw_context.h"
43 #include "tgsi/tgsi_dump.h"
44 #include "tgsi/tgsi_ureg.h"
45
46 #include "st_debug.h"
47 #include "st_context.h"
48 #include "st_program.h"
49 #include "st_mesa_to_tgsi.h"
50 #include "cso_cache/cso_context.h"
51
52
53
54 /**
55 * Clean out any old compilations:
56 */
57 void
58 st_vp_release_varients( struct st_context *st,
59 struct st_vertex_program *stvp )
60 {
61 struct st_vp_varient *vpv;
62
63 for (vpv = stvp->varients; vpv; ) {
64 struct st_vp_varient *next = vpv->next;
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 vpv = next;
80 }
81
82 stvp->varients = NULL;
83 }
84
85
86
87
88 /**
89 * Translate a Mesa vertex shader into a TGSI shader.
90 * \param outputMapping to map vertex program output registers (VERT_RESULT_x)
91 * to TGSI output slots
92 * \param tokensOut destination for TGSI tokens
93 * \return pointer to cached pipe_shader object.
94 */
95 void
96 st_prepare_vertex_program(struct st_context *st,
97 struct st_vertex_program *stvp)
98 {
99 GLuint attr;
100
101 stvp->num_inputs = 0;
102 stvp->num_outputs = 0;
103
104 if (stvp->Base.IsPositionInvariant)
105 _mesa_insert_mvp_code(st->ctx, &stvp->Base);
106
107 assert(stvp->Base.Base.NumInstructions > 1);
108
109 /*
110 * Determine number of inputs, the mappings between VERT_ATTRIB_x
111 * and TGSI generic input indexes, plus input attrib semantic info.
112 */
113 for (attr = 0; attr < VERT_ATTRIB_MAX; attr++) {
114 if (stvp->Base.Base.InputsRead & (1 << attr)) {
115 stvp->input_to_index[attr] = stvp->num_inputs;
116 stvp->index_to_input[stvp->num_inputs] = attr;
117 stvp->num_inputs++;
118 }
119 }
120 /* bit of a hack, presetup potentially unused edgeflag input */
121 stvp->input_to_index[VERT_ATTRIB_EDGEFLAG] = stvp->num_inputs;
122 stvp->index_to_input[stvp->num_inputs] = VERT_ATTRIB_EDGEFLAG;
123
124 /* Compute mapping of vertex program outputs to slots.
125 */
126 for (attr = 0; attr < VERT_RESULT_MAX; attr++) {
127 if ((stvp->Base.Base.OutputsWritten & BITFIELD64_BIT(attr)) == 0) {
128 stvp->result_to_output[attr] = ~0;
129 }
130 else {
131 unsigned slot = stvp->num_outputs++;
132
133 stvp->result_to_output[attr] = slot;
134
135 switch (attr) {
136 case VERT_RESULT_HPOS:
137 stvp->output_semantic_name[slot] = TGSI_SEMANTIC_POSITION;
138 stvp->output_semantic_index[slot] = 0;
139 break;
140 case VERT_RESULT_COL0:
141 stvp->output_semantic_name[slot] = TGSI_SEMANTIC_COLOR;
142 stvp->output_semantic_index[slot] = 0;
143 break;
144 case VERT_RESULT_COL1:
145 stvp->output_semantic_name[slot] = TGSI_SEMANTIC_COLOR;
146 stvp->output_semantic_index[slot] = 1;
147 break;
148 case VERT_RESULT_BFC0:
149 stvp->output_semantic_name[slot] = TGSI_SEMANTIC_BCOLOR;
150 stvp->output_semantic_index[slot] = 0;
151 break;
152 case VERT_RESULT_BFC1:
153 stvp->output_semantic_name[slot] = TGSI_SEMANTIC_BCOLOR;
154 stvp->output_semantic_index[slot] = 1;
155 break;
156 case VERT_RESULT_FOGC:
157 stvp->output_semantic_name[slot] = TGSI_SEMANTIC_FOG;
158 stvp->output_semantic_index[slot] = 0;
159 break;
160 case VERT_RESULT_PSIZ:
161 stvp->output_semantic_name[slot] = TGSI_SEMANTIC_PSIZE;
162 stvp->output_semantic_index[slot] = 0;
163 break;
164 case VERT_RESULT_EDGE:
165 assert(0);
166 break;
167
168 case VERT_RESULT_TEX0:
169 case VERT_RESULT_TEX1:
170 case VERT_RESULT_TEX2:
171 case VERT_RESULT_TEX3:
172 case VERT_RESULT_TEX4:
173 case VERT_RESULT_TEX5:
174 case VERT_RESULT_TEX6:
175 case VERT_RESULT_TEX7:
176 stvp->output_semantic_name[slot] = TGSI_SEMANTIC_GENERIC;
177 stvp->output_semantic_index[slot] = attr - VERT_RESULT_TEX0;
178 break;
179
180 case VERT_RESULT_VAR0:
181 default:
182 assert(attr < VERT_RESULT_MAX);
183 stvp->output_semantic_name[slot] = TGSI_SEMANTIC_GENERIC;
184 stvp->output_semantic_index[slot] = (FRAG_ATTRIB_VAR0 -
185 FRAG_ATTRIB_TEX0 +
186 attr -
187 VERT_RESULT_VAR0);
188 break;
189 }
190 }
191 }
192 /* similar hack to above, presetup potentially unused edgeflag output */
193 stvp->result_to_output[VERT_RESULT_EDGE] = stvp->num_outputs;
194 stvp->output_semantic_name[stvp->num_outputs] = TGSI_SEMANTIC_EDGEFLAG;
195 stvp->output_semantic_index[stvp->num_outputs] = 0;
196 }
197
198
199 struct st_vp_varient *
200 st_translate_vertex_program(struct st_context *st,
201 struct st_vertex_program *stvp,
202 const struct st_vp_varient_key *key)
203 {
204 struct st_vp_varient *vpv = CALLOC_STRUCT(st_vp_varient);
205 struct pipe_context *pipe = st->pipe;
206 struct ureg_program *ureg;
207 enum pipe_error error;
208 unsigned num_outputs;
209
210 _mesa_remove_output_reads(&stvp->Base.Base, PROGRAM_OUTPUT);
211 _mesa_remove_output_reads(&stvp->Base.Base, PROGRAM_VARYING);
212
213 ureg = ureg_create( TGSI_PROCESSOR_VERTEX );
214 if (ureg == NULL) {
215 FREE(vpv);
216 return NULL;
217 }
218
219 vpv->num_inputs = stvp->num_inputs;
220 num_outputs = stvp->num_outputs;
221 if (key->passthrough_edgeflags) {
222 vpv->num_inputs++;
223 num_outputs++;
224 }
225
226 if (ST_DEBUG & DEBUG_MESA) {
227 _mesa_print_program(&stvp->Base.Base);
228 _mesa_print_program_parameters(st->ctx, &stvp->Base.Base);
229 debug_printf("\n");
230 }
231
232 error =
233 st_translate_mesa_program(st->ctx,
234 TGSI_PROCESSOR_VERTEX,
235 ureg,
236 &stvp->Base.Base,
237 /* inputs */
238 vpv->num_inputs,
239 stvp->input_to_index,
240 NULL, /* input semantic name */
241 NULL, /* input semantic index */
242 NULL,
243 /* outputs */
244 num_outputs,
245 stvp->result_to_output,
246 stvp->output_semantic_name,
247 stvp->output_semantic_index,
248 key->passthrough_edgeflags );
249
250 if (error)
251 goto fail;
252
253 vpv->tgsi.tokens = ureg_get_tokens( ureg, NULL );
254 if (!vpv->tgsi.tokens)
255 goto fail;
256
257 ureg_destroy( ureg );
258
259 vpv->driver_shader = pipe->create_vs_state(pipe, &vpv->tgsi);
260
261 if (ST_DEBUG & DEBUG_TGSI) {
262 tgsi_dump( vpv->tgsi.tokens, 0 );
263 debug_printf("\n");
264 }
265
266 return vpv;
267
268 fail:
269 debug_printf("%s: failed to translate Mesa program:\n", __FUNCTION__);
270 _mesa_print_program(&stvp->Base.Base);
271 debug_assert(0);
272
273 ureg_destroy( ureg );
274 return NULL;
275 }
276
277
278
279 /**
280 * Translate a Mesa fragment shader into a TGSI shader.
281 * \return pointer to cached pipe_shader object.
282 */
283 void
284 st_translate_fragment_program(struct st_context *st,
285 struct st_fragment_program *stfp )
286 {
287 struct pipe_context *pipe = st->pipe;
288 GLuint outputMapping[FRAG_RESULT_MAX];
289 GLuint inputMapping[FRAG_ATTRIB_MAX];
290 GLuint interpMode[PIPE_MAX_SHADER_INPUTS]; /* XXX size? */
291 GLuint attr;
292 enum pipe_error error;
293 const GLbitfield inputsRead = stfp->Base.Base.InputsRead;
294 struct ureg_program *ureg;
295
296 ubyte input_semantic_name[PIPE_MAX_SHADER_INPUTS];
297 ubyte input_semantic_index[PIPE_MAX_SHADER_INPUTS];
298 uint fs_num_inputs = 0;
299
300 ubyte fs_output_semantic_name[PIPE_MAX_SHADER_OUTPUTS];
301 ubyte fs_output_semantic_index[PIPE_MAX_SHADER_OUTPUTS];
302 uint fs_num_outputs = 0;
303
304 _mesa_remove_output_reads(&stfp->Base.Base, PROGRAM_OUTPUT);
305
306 /*
307 * Convert Mesa program inputs to TGSI input register semantics.
308 */
309 for (attr = 0; attr < FRAG_ATTRIB_MAX; attr++) {
310 if (inputsRead & (1 << attr)) {
311 const GLuint slot = fs_num_inputs++;
312
313 inputMapping[attr] = slot;
314
315 switch (attr) {
316 case FRAG_ATTRIB_WPOS:
317 input_semantic_name[slot] = TGSI_SEMANTIC_POSITION;
318 input_semantic_index[slot] = 0;
319 interpMode[slot] = TGSI_INTERPOLATE_LINEAR;
320 break;
321 case FRAG_ATTRIB_COL0:
322 input_semantic_name[slot] = TGSI_SEMANTIC_COLOR;
323 input_semantic_index[slot] = 0;
324 interpMode[slot] = TGSI_INTERPOLATE_LINEAR;
325 break;
326 case FRAG_ATTRIB_COL1:
327 input_semantic_name[slot] = TGSI_SEMANTIC_COLOR;
328 input_semantic_index[slot] = 1;
329 interpMode[slot] = TGSI_INTERPOLATE_LINEAR;
330 break;
331 case FRAG_ATTRIB_FOGC:
332 input_semantic_name[slot] = TGSI_SEMANTIC_FOG;
333 input_semantic_index[slot] = 0;
334 interpMode[slot] = TGSI_INTERPOLATE_PERSPECTIVE;
335 break;
336 case FRAG_ATTRIB_FACE:
337 input_semantic_name[slot] = TGSI_SEMANTIC_FACE;
338 input_semantic_index[slot] = 0;
339 interpMode[slot] = TGSI_INTERPOLATE_CONSTANT;
340 break;
341 case FRAG_ATTRIB_PNTC:
342 /* This is a hack. We really need a new semantic label for
343 * point coord. The draw module needs to know which fragment
344 * shader input is the point coord attribute so that it can set
345 * up the right vertex attribute values.
346 */
347 input_semantic_name[slot] = TGSI_SEMANTIC_GENERIC;
348 input_semantic_index[slot] = 0;
349 interpMode[slot] = TGSI_INTERPOLATE_PERSPECTIVE;
350 break;
351
352 /* In most cases, there is nothing special about these
353 * inputs, so adopt a convention to use the generic
354 * semantic name and the mesa FRAG_ATTRIB_ number as the
355 * index.
356 *
357 * All that is required is that the vertex shader labels
358 * its own outputs similarly, and that the vertex shader
359 * generates at least every output required by the
360 * fragment shader plus fixed-function hardware (such as
361 * BFC).
362 *
363 * There is no requirement that semantic indexes start at
364 * zero or be restricted to a particular range -- nobody
365 * should be building tables based on semantic index.
366 */
367 case FRAG_ATTRIB_TEX0:
368 case FRAG_ATTRIB_TEX1:
369 case FRAG_ATTRIB_TEX2:
370 case FRAG_ATTRIB_TEX3:
371 case FRAG_ATTRIB_TEX4:
372 case FRAG_ATTRIB_TEX5:
373 case FRAG_ATTRIB_TEX6:
374 case FRAG_ATTRIB_TEX7:
375 case FRAG_ATTRIB_VAR0:
376 default:
377 /* Actually, let's try and zero-base this just for
378 * readability of the generated TGSI.
379 */
380 assert(attr >= FRAG_ATTRIB_TEX0);
381 input_semantic_index[slot] = (attr - FRAG_ATTRIB_TEX0);
382 input_semantic_name[slot] = TGSI_SEMANTIC_GENERIC;
383 interpMode[slot] = TGSI_INTERPOLATE_PERSPECTIVE;
384 break;
385 }
386 }
387 else {
388 inputMapping[attr] = -1;
389 }
390 }
391
392 /*
393 * Semantics and mapping for outputs
394 */
395 {
396 uint numColors = 0;
397 GLbitfield64 outputsWritten = stfp->Base.Base.OutputsWritten;
398
399 /* if z is written, emit that first */
400 if (outputsWritten & BITFIELD64_BIT(FRAG_RESULT_DEPTH)) {
401 fs_output_semantic_name[fs_num_outputs] = TGSI_SEMANTIC_POSITION;
402 fs_output_semantic_index[fs_num_outputs] = 0;
403 outputMapping[FRAG_RESULT_DEPTH] = fs_num_outputs;
404 fs_num_outputs++;
405 outputsWritten &= ~(1 << FRAG_RESULT_DEPTH);
406 }
407
408 /* handle remaning outputs (color) */
409 for (attr = 0; attr < FRAG_RESULT_MAX; attr++) {
410 if (outputsWritten & BITFIELD64_BIT(attr)) {
411 switch (attr) {
412 case FRAG_RESULT_DEPTH:
413 /* handled above */
414 assert(0);
415 break;
416 default:
417 assert(attr == FRAG_RESULT_COLOR ||
418 (FRAG_RESULT_DATA0 <= attr && attr < FRAG_RESULT_MAX));
419 fs_output_semantic_name[fs_num_outputs] = TGSI_SEMANTIC_COLOR;
420 fs_output_semantic_index[fs_num_outputs] = numColors;
421 outputMapping[attr] = fs_num_outputs;
422 numColors++;
423 break;
424 }
425
426 fs_num_outputs++;
427 }
428 }
429 }
430
431 ureg = ureg_create( TGSI_PROCESSOR_FRAGMENT );
432 if (ureg == NULL)
433 return;
434
435 if (ST_DEBUG & DEBUG_MESA) {
436 _mesa_print_program(&stfp->Base.Base);
437 _mesa_print_program_parameters(st->ctx, &stfp->Base.Base);
438 debug_printf("\n");
439 }
440
441 error =
442 st_translate_mesa_program(st->ctx,
443 TGSI_PROCESSOR_FRAGMENT,
444 ureg,
445 &stfp->Base.Base,
446 /* inputs */
447 fs_num_inputs,
448 inputMapping,
449 input_semantic_name,
450 input_semantic_index,
451 interpMode,
452 /* outputs */
453 fs_num_outputs,
454 outputMapping,
455 fs_output_semantic_name,
456 fs_output_semantic_index, FALSE );
457
458 stfp->tgsi.tokens = ureg_get_tokens( ureg, NULL );
459 ureg_destroy( ureg );
460 stfp->driver_shader = pipe->create_fs_state(pipe, &stfp->tgsi);
461
462 if (ST_DEBUG & DEBUG_TGSI) {
463 tgsi_dump( stfp->tgsi.tokens, 0/*TGSI_DUMP_VERBOSE*/ );
464 debug_printf("\n");
465 }
466 }
467
468 void
469 st_translate_geometry_program(struct st_context *st,
470 struct st_geometry_program *stgp)
471 {
472 GLuint inputMapping[GEOM_ATTRIB_MAX];
473 GLuint outputMapping[GEOM_RESULT_MAX];
474 struct pipe_context *pipe = st->pipe;
475 enum pipe_error error;
476 GLuint attr;
477 const GLbitfield inputsRead = stgp->Base.Base.InputsRead;
478 GLuint vslot = 0;
479 GLuint num_generic = 0;
480
481 uint gs_num_inputs = 0;
482 uint gs_builtin_inputs = 0;
483 uint gs_array_offset = 0;
484
485 ubyte gs_output_semantic_name[PIPE_MAX_SHADER_OUTPUTS];
486 ubyte gs_output_semantic_index[PIPE_MAX_SHADER_OUTPUTS];
487 uint gs_num_outputs = 0;
488
489 GLint i;
490 GLuint maxSlot = 0;
491 struct ureg_program *ureg;
492
493 _mesa_remove_output_reads(&stgp->Base.Base, PROGRAM_OUTPUT);
494 _mesa_remove_output_reads(&stgp->Base.Base, PROGRAM_VARYING);
495
496 ureg = ureg_create( TGSI_PROCESSOR_GEOMETRY );
497 if (ureg == NULL) {
498 return;
499 }
500
501 /* which vertex output goes to the first geometry input */
502 vslot = 0;
503
504 memset(inputMapping, 0, sizeof(inputMapping));
505 memset(outputMapping, 0, sizeof(outputMapping));
506
507 /*
508 * Convert Mesa program inputs to TGSI input register semantics.
509 */
510 for (attr = 0; attr < GEOM_ATTRIB_MAX; attr++) {
511 if (inputsRead & (1 << attr)) {
512 const GLuint slot = gs_num_inputs;
513
514 gs_num_inputs++;
515
516 inputMapping[attr] = slot;
517
518 stgp->input_map[slot + gs_array_offset] = vslot - gs_builtin_inputs;
519 stgp->input_to_index[attr] = vslot;
520 stgp->index_to_input[vslot] = attr;
521 ++vslot;
522
523 if (attr != GEOM_ATTRIB_PRIMITIVE_ID) {
524 gs_array_offset += 2;
525 } else
526 ++gs_builtin_inputs;
527
528 #if 1
529 debug_printf("input map at %d = %d\n",
530 slot + gs_array_offset, stgp->input_map[slot + gs_array_offset]);
531 #endif
532
533 switch (attr) {
534 case GEOM_ATTRIB_PRIMITIVE_ID:
535 stgp->input_semantic_name[slot] = TGSI_SEMANTIC_PRIMID;
536 stgp->input_semantic_index[slot] = 0;
537 break;
538 case GEOM_ATTRIB_POSITION:
539 stgp->input_semantic_name[slot] = TGSI_SEMANTIC_POSITION;
540 stgp->input_semantic_index[slot] = 0;
541 break;
542 case GEOM_ATTRIB_COLOR0:
543 stgp->input_semantic_name[slot] = TGSI_SEMANTIC_COLOR;
544 stgp->input_semantic_index[slot] = 0;
545 break;
546 case GEOM_ATTRIB_COLOR1:
547 stgp->input_semantic_name[slot] = TGSI_SEMANTIC_COLOR;
548 stgp->input_semantic_index[slot] = 1;
549 break;
550 case GEOM_ATTRIB_FOG_FRAG_COORD:
551 stgp->input_semantic_name[slot] = TGSI_SEMANTIC_FOG;
552 stgp->input_semantic_index[slot] = 0;
553 break;
554 case GEOM_ATTRIB_TEX_COORD:
555 stgp->input_semantic_name[slot] = TGSI_SEMANTIC_GENERIC;
556 stgp->input_semantic_index[slot] = num_generic++;
557 break;
558 case GEOM_ATTRIB_VAR0:
559 /* fall-through */
560 default:
561 stgp->input_semantic_name[slot] = TGSI_SEMANTIC_GENERIC;
562 stgp->input_semantic_index[slot] = num_generic++;
563 }
564 }
565 }
566
567 /* initialize output semantics to defaults */
568 for (i = 0; i < PIPE_MAX_SHADER_OUTPUTS; i++) {
569 gs_output_semantic_name[i] = TGSI_SEMANTIC_GENERIC;
570 gs_output_semantic_index[i] = 0;
571 }
572
573 num_generic = 0;
574 /*
575 * Determine number of outputs, the (default) output register
576 * mapping and the semantic information for each output.
577 */
578 for (attr = 0; attr < GEOM_RESULT_MAX; attr++) {
579 if (stgp->Base.Base.OutputsWritten & (1 << attr)) {
580 GLuint slot;
581
582 slot = gs_num_outputs;
583 gs_num_outputs++;
584 outputMapping[attr] = slot;
585
586 switch (attr) {
587 case GEOM_RESULT_POS:
588 assert(slot == 0);
589 gs_output_semantic_name[slot] = TGSI_SEMANTIC_POSITION;
590 gs_output_semantic_index[slot] = 0;
591 break;
592 case GEOM_RESULT_COL0:
593 gs_output_semantic_name[slot] = TGSI_SEMANTIC_COLOR;
594 gs_output_semantic_index[slot] = 0;
595 break;
596 case GEOM_RESULT_COL1:
597 gs_output_semantic_name[slot] = TGSI_SEMANTIC_COLOR;
598 gs_output_semantic_index[slot] = 1;
599 break;
600 case GEOM_RESULT_SCOL0:
601 gs_output_semantic_name[slot] = TGSI_SEMANTIC_BCOLOR;
602 gs_output_semantic_index[slot] = 0;
603 break;
604 case GEOM_RESULT_SCOL1:
605 gs_output_semantic_name[slot] = TGSI_SEMANTIC_BCOLOR;
606 gs_output_semantic_index[slot] = 1;
607 break;
608 case GEOM_RESULT_FOGC:
609 gs_output_semantic_name[slot] = TGSI_SEMANTIC_FOG;
610 gs_output_semantic_index[slot] = 0;
611 break;
612 case GEOM_RESULT_PSIZ:
613 gs_output_semantic_name[slot] = TGSI_SEMANTIC_PSIZE;
614 gs_output_semantic_index[slot] = 0;
615 break;
616 case GEOM_RESULT_TEX0:
617 case GEOM_RESULT_TEX1:
618 case GEOM_RESULT_TEX2:
619 case GEOM_RESULT_TEX3:
620 case GEOM_RESULT_TEX4:
621 case GEOM_RESULT_TEX5:
622 case GEOM_RESULT_TEX6:
623 case GEOM_RESULT_TEX7:
624 /* fall-through */
625 case GEOM_RESULT_VAR0:
626 /* fall-through */
627 default:
628 assert(slot < Elements(gs_output_semantic_name));
629 /* use default semantic info */
630 gs_output_semantic_name[slot] = TGSI_SEMANTIC_GENERIC;
631 gs_output_semantic_index[slot] = num_generic++;
632 }
633 }
634 }
635
636 assert(gs_output_semantic_name[0] == TGSI_SEMANTIC_POSITION);
637
638 /* find max output slot referenced to compute gs_num_outputs */
639 for (attr = 0; attr < GEOM_RESULT_MAX; attr++) {
640 if (outputMapping[attr] != ~0 && outputMapping[attr] > maxSlot)
641 maxSlot = outputMapping[attr];
642 }
643 gs_num_outputs = maxSlot + 1;
644
645 #if 0 /* debug */
646 {
647 GLuint i;
648 printf("outputMapping? %d\n", outputMapping ? 1 : 0);
649 if (outputMapping) {
650 printf("attr -> slot\n");
651 for (i = 0; i < 16; i++) {
652 printf(" %2d %3d\n", i, outputMapping[i]);
653 }
654 }
655 printf("slot sem_name sem_index\n");
656 for (i = 0; i < gs_num_outputs; i++) {
657 printf(" %2d %d %d\n",
658 i,
659 gs_output_semantic_name[i],
660 gs_output_semantic_index[i]);
661 }
662 }
663 #endif
664
665 /* free old shader state, if any */
666 if (stgp->tgsi.tokens) {
667 st_free_tokens(stgp->tgsi.tokens);
668 stgp->tgsi.tokens = NULL;
669 }
670 if (stgp->driver_shader) {
671 cso_delete_geometry_shader(st->cso_context, stgp->driver_shader);
672 stgp->driver_shader = NULL;
673 }
674
675 ureg_property_gs_input_prim(ureg, stgp->Base.InputType);
676 ureg_property_gs_output_prim(ureg, stgp->Base.OutputType);
677 ureg_property_gs_max_vertices(ureg, stgp->Base.VerticesOut);
678
679 error = st_translate_mesa_program(st->ctx,
680 TGSI_PROCESSOR_GEOMETRY,
681 ureg,
682 &stgp->Base.Base,
683 /* inputs */
684 gs_num_inputs,
685 inputMapping,
686 stgp->input_semantic_name,
687 stgp->input_semantic_index,
688 NULL,
689 /* outputs */
690 gs_num_outputs,
691 outputMapping,
692 gs_output_semantic_name,
693 gs_output_semantic_index,
694 FALSE);
695
696
697 stgp->num_inputs = gs_num_inputs;
698 stgp->tgsi.tokens = ureg_get_tokens( ureg, NULL );
699 ureg_destroy( ureg );
700 stgp->driver_shader = pipe->create_gs_state(pipe, &stgp->tgsi);
701
702 if ((ST_DEBUG & DEBUG_TGSI) && (ST_DEBUG & DEBUG_MESA)) {
703 _mesa_print_program(&stgp->Base.Base);
704 debug_printf("\n");
705 }
706
707 if (ST_DEBUG & DEBUG_TGSI) {
708 tgsi_dump(stgp->tgsi.tokens, 0);
709 debug_printf("\n");
710 }
711 }
712
713 /**
714 * Debug- print current shader text
715 */
716 void
717 st_print_shaders(GLcontext *ctx)
718 {
719 struct gl_shader_program *shProg = ctx->Shader.CurrentProgram;
720 if (shProg) {
721 GLuint i;
722 for (i = 0; i < shProg->NumShaders; i++) {
723 printf("GLSL shader %u of %u:\n", i, shProg->NumShaders);
724 printf("%s\n", shProg->Shaders[i]->Source);
725 }
726 }
727 }