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