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