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