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