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