1 /**************************************************************************
3 * Copyright 2007 Tungsten Graphics, Inc., Cedar Park, Texas.
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:
14 * The above copyright notice and this permission notice (including the
15 * next paragraph) shall be included in all copies or substantial portions
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.
26 **************************************************************************/
29 * Keith Whitwell <keith@tungstengraphics.com>
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"
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"
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"
60 * Delete a vertex program variant. Note the caller must unlink
61 * the variant from the linked list.
64 delete_vp_variant(struct st_context
*st
, struct st_vp_variant
*vpv
)
66 if (vpv
->driver_shader
)
67 cso_delete_vertex_shader(st
->cso_context
, vpv
->driver_shader
);
69 #if FEATURE_feedback || FEATURE_rastpos
71 draw_delete_vertex_shader( st
->draw
, vpv
->draw_shader
);
75 st_free_tokens(vpv
->tgsi
.tokens
);
83 * Clean out any old compilations:
86 st_release_vp_variants( struct st_context
*st
,
87 struct st_vertex_program
*stvp
)
89 struct st_vp_variant
*vpv
;
91 for (vpv
= stvp
->variants
; vpv
; ) {
92 struct st_vp_variant
*next
= vpv
->next
;
93 delete_vp_variant(st
, vpv
);
97 stvp
->variants
= NULL
;
103 * Delete a fragment program variant. Note the caller must unlink
104 * the variant from the linked list.
107 delete_fp_variant(struct st_context
*st
, struct st_fp_variant
*fpv
)
109 if (fpv
->driver_shader
)
110 cso_delete_fragment_shader(st
->cso_context
, fpv
->driver_shader
);
117 * Free all variants of a fragment program.
120 st_release_fp_variants(struct st_context
*st
, struct st_fragment_program
*stfp
)
122 struct st_fp_variant
*fpv
;
124 for (fpv
= stfp
->variants
; fpv
; ) {
125 struct st_fp_variant
*next
= fpv
->next
;
126 delete_fp_variant(st
, fpv
);
130 stfp
->variants
= NULL
;
135 * Delete a geometry program variant. Note the caller must unlink
136 * the variant from the linked list.
139 delete_gp_variant(struct st_context
*st
, struct st_gp_variant
*gpv
)
141 if (gpv
->driver_shader
)
142 cso_delete_geometry_shader(st
->cso_context
, gpv
->driver_shader
);
149 * Free all variants of a geometry program.
152 st_release_gp_variants(struct st_context
*st
, struct st_geometry_program
*stgp
)
154 struct st_gp_variant
*gpv
;
156 for (gpv
= stgp
->variants
; gpv
; ) {
157 struct st_gp_variant
*next
= gpv
->next
;
158 delete_gp_variant(st
, gpv
);
162 stgp
->variants
= NULL
;
169 * Translate a Mesa vertex shader into a TGSI shader.
170 * \param outputMapping to map vertex program output registers (VERT_RESULT_x)
171 * to TGSI output slots
172 * \param tokensOut destination for TGSI tokens
173 * \return pointer to cached pipe_shader object.
176 st_prepare_vertex_program(struct st_context
*st
,
177 struct st_vertex_program
*stvp
)
181 stvp
->num_inputs
= 0;
182 stvp
->num_outputs
= 0;
184 if (stvp
->Base
.IsPositionInvariant
)
185 _mesa_insert_mvp_code(st
->ctx
, &stvp
->Base
);
187 assert(stvp
->Base
.Base
.NumInstructions
> 1);
190 * Determine number of inputs, the mappings between VERT_ATTRIB_x
191 * and TGSI generic input indexes, plus input attrib semantic info.
193 for (attr
= 0; attr
< VERT_ATTRIB_MAX
; attr
++) {
194 if (stvp
->Base
.Base
.InputsRead
& (1 << attr
)) {
195 stvp
->input_to_index
[attr
] = stvp
->num_inputs
;
196 stvp
->index_to_input
[stvp
->num_inputs
] = attr
;
200 /* bit of a hack, presetup potentially unused edgeflag input */
201 stvp
->input_to_index
[VERT_ATTRIB_EDGEFLAG
] = stvp
->num_inputs
;
202 stvp
->index_to_input
[stvp
->num_inputs
] = VERT_ATTRIB_EDGEFLAG
;
204 /* Compute mapping of vertex program outputs to slots.
206 for (attr
= 0; attr
< VERT_RESULT_MAX
; attr
++) {
207 if ((stvp
->Base
.Base
.OutputsWritten
& BITFIELD64_BIT(attr
)) == 0) {
208 stvp
->result_to_output
[attr
] = ~0;
211 unsigned slot
= stvp
->num_outputs
++;
213 stvp
->result_to_output
[attr
] = slot
;
216 case VERT_RESULT_HPOS
:
217 stvp
->output_semantic_name
[slot
] = TGSI_SEMANTIC_POSITION
;
218 stvp
->output_semantic_index
[slot
] = 0;
220 case VERT_RESULT_COL0
:
221 stvp
->output_semantic_name
[slot
] = TGSI_SEMANTIC_COLOR
;
222 stvp
->output_semantic_index
[slot
] = 0;
224 case VERT_RESULT_COL1
:
225 stvp
->output_semantic_name
[slot
] = TGSI_SEMANTIC_COLOR
;
226 stvp
->output_semantic_index
[slot
] = 1;
228 case VERT_RESULT_BFC0
:
229 stvp
->output_semantic_name
[slot
] = TGSI_SEMANTIC_BCOLOR
;
230 stvp
->output_semantic_index
[slot
] = 0;
232 case VERT_RESULT_BFC1
:
233 stvp
->output_semantic_name
[slot
] = TGSI_SEMANTIC_BCOLOR
;
234 stvp
->output_semantic_index
[slot
] = 1;
236 case VERT_RESULT_FOGC
:
237 stvp
->output_semantic_name
[slot
] = TGSI_SEMANTIC_FOG
;
238 stvp
->output_semantic_index
[slot
] = 0;
240 case VERT_RESULT_PSIZ
:
241 stvp
->output_semantic_name
[slot
] = TGSI_SEMANTIC_PSIZE
;
242 stvp
->output_semantic_index
[slot
] = 0;
244 case VERT_RESULT_EDGE
:
248 case VERT_RESULT_TEX0
:
249 case VERT_RESULT_TEX1
:
250 case VERT_RESULT_TEX2
:
251 case VERT_RESULT_TEX3
:
252 case VERT_RESULT_TEX4
:
253 case VERT_RESULT_TEX5
:
254 case VERT_RESULT_TEX6
:
255 case VERT_RESULT_TEX7
:
256 stvp
->output_semantic_name
[slot
] = TGSI_SEMANTIC_GENERIC
;
257 stvp
->output_semantic_index
[slot
] = attr
- VERT_RESULT_TEX0
;
260 case VERT_RESULT_VAR0
:
262 assert(attr
< VERT_RESULT_MAX
);
263 stvp
->output_semantic_name
[slot
] = TGSI_SEMANTIC_GENERIC
;
264 stvp
->output_semantic_index
[slot
] = (FRAG_ATTRIB_VAR0
-
272 /* similar hack to above, presetup potentially unused edgeflag output */
273 stvp
->result_to_output
[VERT_RESULT_EDGE
] = stvp
->num_outputs
;
274 stvp
->output_semantic_name
[stvp
->num_outputs
] = TGSI_SEMANTIC_EDGEFLAG
;
275 stvp
->output_semantic_index
[stvp
->num_outputs
] = 0;
280 * Translate a vertex program to create a new variant.
282 static struct st_vp_variant
*
283 st_translate_vertex_program(struct st_context
*st
,
284 struct st_vertex_program
*stvp
,
285 const struct st_vp_variant_key
*key
)
287 struct st_vp_variant
*vpv
= CALLOC_STRUCT(st_vp_variant
);
288 struct pipe_context
*pipe
= st
->pipe
;
289 struct ureg_program
*ureg
;
290 enum pipe_error error
;
291 unsigned num_outputs
;
293 st_prepare_vertex_program( st
, stvp
);
295 _mesa_remove_output_reads(&stvp
->Base
.Base
, PROGRAM_OUTPUT
);
296 _mesa_remove_output_reads(&stvp
->Base
.Base
, PROGRAM_VARYING
);
298 ureg
= ureg_create( TGSI_PROCESSOR_VERTEX
);
306 vpv
->num_inputs
= stvp
->num_inputs
;
307 num_outputs
= stvp
->num_outputs
;
308 if (key
->passthrough_edgeflags
) {
313 if (ST_DEBUG
& DEBUG_MESA
) {
314 _mesa_print_program(&stvp
->Base
.Base
);
315 _mesa_print_program_parameters(st
->ctx
, &stvp
->Base
.Base
);
319 error
= st_translate_mesa_program(st
->ctx
,
320 TGSI_PROCESSOR_VERTEX
,
325 stvp
->input_to_index
,
326 NULL
, /* input semantic name */
327 NULL
, /* input semantic index */
331 stvp
->result_to_output
,
332 stvp
->output_semantic_name
,
333 stvp
->output_semantic_index
,
334 key
->passthrough_edgeflags
);
339 vpv
->tgsi
.tokens
= ureg_get_tokens( ureg
, NULL
);
340 if (!vpv
->tgsi
.tokens
)
343 ureg_destroy( ureg
);
345 vpv
->driver_shader
= pipe
->create_vs_state(pipe
, &vpv
->tgsi
);
347 if (ST_DEBUG
& DEBUG_TGSI
) {
348 tgsi_dump( vpv
->tgsi
.tokens
, 0 );
355 debug_printf("%s: failed to translate Mesa program:\n", __FUNCTION__
);
356 _mesa_print_program(&stvp
->Base
.Base
);
359 ureg_destroy( ureg
);
365 * Find/create a vertex program variant.
367 struct st_vp_variant
*
368 st_get_vp_variant(struct st_context
*st
,
369 struct st_vertex_program
*stvp
,
370 const struct st_vp_variant_key
*key
)
372 struct st_vp_variant
*vpv
;
374 /* Search for existing variant */
375 for (vpv
= stvp
->variants
; vpv
; vpv
= vpv
->next
) {
376 if (memcmp(&vpv
->key
, key
, sizeof(*key
)) == 0) {
383 vpv
= st_translate_vertex_program(st
, stvp
, key
);
385 /* insert into list */
386 vpv
->next
= stvp
->variants
;
387 stvp
->variants
= vpv
;
396 * Translate a Mesa fragment shader into a TGSI shader using extra info in
398 * \return new fragment program variant
400 static struct st_fp_variant
*
401 st_translate_fragment_program(struct st_context
*st
,
402 struct st_fragment_program
*stfp
,
403 const struct st_fp_variant_key
*key
)
405 struct pipe_context
*pipe
= st
->pipe
;
406 struct st_fp_variant
*variant
= CALLOC_STRUCT(st_fp_variant
);
411 assert(!(key
->bitmap
&& key
->drawpixels
));
415 /* glBitmap drawing */
416 struct gl_fragment_program
*fp
;
418 st_make_bitmap_fragment_program(st
, &stfp
->Base
,
419 &fp
, &variant
->bitmap_sampler
);
421 variant
->parameters
= _mesa_clone_parameter_list(fp
->Base
.Parameters
);
422 stfp
= st_fragment_program(fp
);
424 else if (key
->drawpixels
) {
425 /* glDrawPixels drawing */
426 struct gl_fragment_program
*fp
;
428 if (key
->drawpixels_z
|| key
->drawpixels_stencil
) {
429 fp
= st_make_drawpix_z_stencil_program(st
, key
->drawpixels_z
,
430 key
->drawpixels_stencil
);
434 st_make_drawpix_fragment_program(st
, &stfp
->Base
, &fp
);
435 variant
->parameters
= _mesa_clone_parameter_list(fp
->Base
.Parameters
);
437 stfp
= st_fragment_program(fp
);
441 if (!stfp
->tgsi
.tokens
) {
442 /* need to translate Mesa instructions to TGSI now */
443 GLuint outputMapping
[FRAG_RESULT_MAX
];
444 GLuint inputMapping
[FRAG_ATTRIB_MAX
];
445 GLuint interpMode
[PIPE_MAX_SHADER_INPUTS
]; /* XXX size? */
447 enum pipe_error error
;
448 const GLbitfield inputsRead
= stfp
->Base
.Base
.InputsRead
;
449 struct ureg_program
*ureg
;
450 GLboolean write_all
= GL_FALSE
;
452 ubyte input_semantic_name
[PIPE_MAX_SHADER_INPUTS
];
453 ubyte input_semantic_index
[PIPE_MAX_SHADER_INPUTS
];
454 uint fs_num_inputs
= 0;
456 ubyte fs_output_semantic_name
[PIPE_MAX_SHADER_OUTPUTS
];
457 ubyte fs_output_semantic_index
[PIPE_MAX_SHADER_OUTPUTS
];
458 uint fs_num_outputs
= 0;
461 _mesa_remove_output_reads(&stfp
->Base
.Base
, PROGRAM_OUTPUT
);
464 * Convert Mesa program inputs to TGSI input register semantics.
466 for (attr
= 0; attr
< FRAG_ATTRIB_MAX
; attr
++) {
467 if (inputsRead
& (1 << attr
)) {
468 const GLuint slot
= fs_num_inputs
++;
470 inputMapping
[attr
] = slot
;
473 case FRAG_ATTRIB_WPOS
:
474 input_semantic_name
[slot
] = TGSI_SEMANTIC_POSITION
;
475 input_semantic_index
[slot
] = 0;
476 interpMode
[slot
] = TGSI_INTERPOLATE_LINEAR
;
478 case FRAG_ATTRIB_COL0
:
479 input_semantic_name
[slot
] = TGSI_SEMANTIC_COLOR
;
480 input_semantic_index
[slot
] = 0;
481 interpMode
[slot
] = TGSI_INTERPOLATE_LINEAR
;
483 case FRAG_ATTRIB_COL1
:
484 input_semantic_name
[slot
] = TGSI_SEMANTIC_COLOR
;
485 input_semantic_index
[slot
] = 1;
486 interpMode
[slot
] = TGSI_INTERPOLATE_LINEAR
;
488 case FRAG_ATTRIB_FOGC
:
489 input_semantic_name
[slot
] = TGSI_SEMANTIC_FOG
;
490 input_semantic_index
[slot
] = 0;
491 interpMode
[slot
] = TGSI_INTERPOLATE_PERSPECTIVE
;
493 case FRAG_ATTRIB_FACE
:
494 input_semantic_name
[slot
] = TGSI_SEMANTIC_FACE
;
495 input_semantic_index
[slot
] = 0;
496 interpMode
[slot
] = TGSI_INTERPOLATE_CONSTANT
;
498 /* In most cases, there is nothing special about these
499 * inputs, so adopt a convention to use the generic
500 * semantic name and the mesa FRAG_ATTRIB_ number as the
503 * All that is required is that the vertex shader labels
504 * its own outputs similarly, and that the vertex shader
505 * generates at least every output required by the
506 * fragment shader plus fixed-function hardware (such as
509 * There is no requirement that semantic indexes start at
510 * zero or be restricted to a particular range -- nobody
511 * should be building tables based on semantic index.
513 case FRAG_ATTRIB_PNTC
:
514 case FRAG_ATTRIB_TEX0
:
515 case FRAG_ATTRIB_TEX1
:
516 case FRAG_ATTRIB_TEX2
:
517 case FRAG_ATTRIB_TEX3
:
518 case FRAG_ATTRIB_TEX4
:
519 case FRAG_ATTRIB_TEX5
:
520 case FRAG_ATTRIB_TEX6
:
521 case FRAG_ATTRIB_TEX7
:
522 case FRAG_ATTRIB_VAR0
:
524 /* Actually, let's try and zero-base this just for
525 * readability of the generated TGSI.
527 assert(attr
>= FRAG_ATTRIB_TEX0
);
528 input_semantic_index
[slot
] = (attr
- FRAG_ATTRIB_TEX0
);
529 input_semantic_name
[slot
] = TGSI_SEMANTIC_GENERIC
;
530 if (attr
== FRAG_ATTRIB_PNTC
)
531 interpMode
[slot
] = TGSI_INTERPOLATE_LINEAR
;
533 interpMode
[slot
] = TGSI_INTERPOLATE_PERSPECTIVE
;
538 inputMapping
[attr
] = -1;
543 * Semantics and mapping for outputs
547 GLbitfield64 outputsWritten
= stfp
->Base
.Base
.OutputsWritten
;
549 /* if z is written, emit that first */
550 if (outputsWritten
& BITFIELD64_BIT(FRAG_RESULT_DEPTH
)) {
551 fs_output_semantic_name
[fs_num_outputs
] = TGSI_SEMANTIC_POSITION
;
552 fs_output_semantic_index
[fs_num_outputs
] = 0;
553 outputMapping
[FRAG_RESULT_DEPTH
] = fs_num_outputs
;
555 outputsWritten
&= ~(1 << FRAG_RESULT_DEPTH
);
558 if (outputsWritten
& BITFIELD64_BIT(FRAG_RESULT_STENCIL
)) {
559 fs_output_semantic_name
[fs_num_outputs
] = TGSI_SEMANTIC_STENCIL
;
560 fs_output_semantic_index
[fs_num_outputs
] = 0;
561 outputMapping
[FRAG_RESULT_STENCIL
] = fs_num_outputs
;
563 outputsWritten
&= ~(1 << FRAG_RESULT_STENCIL
);
566 /* handle remaning outputs (color) */
567 for (attr
= 0; attr
< FRAG_RESULT_MAX
; attr
++) {
568 if (outputsWritten
& BITFIELD64_BIT(attr
)) {
570 case FRAG_RESULT_DEPTH
:
571 case FRAG_RESULT_STENCIL
:
575 case FRAG_RESULT_COLOR
:
576 write_all
= GL_TRUE
; /* fallthrough */
578 assert(attr
== FRAG_RESULT_COLOR
||
579 (FRAG_RESULT_DATA0
<= attr
&& attr
< FRAG_RESULT_MAX
));
580 fs_output_semantic_name
[fs_num_outputs
] = TGSI_SEMANTIC_COLOR
;
581 fs_output_semantic_index
[fs_num_outputs
] = numColors
;
582 outputMapping
[attr
] = fs_num_outputs
;
592 ureg
= ureg_create( TGSI_PROCESSOR_FRAGMENT
);
596 if (ST_DEBUG
& DEBUG_MESA
) {
597 _mesa_print_program(&stfp
->Base
.Base
);
598 _mesa_print_program_parameters(st
->ctx
, &stfp
->Base
.Base
);
601 if (write_all
== GL_TRUE
)
602 ureg_property_fs_color0_writes_all_cbufs(ureg
, 1);
604 error
= st_translate_mesa_program(st
->ctx
,
605 TGSI_PROCESSOR_FRAGMENT
,
612 input_semantic_index
,
617 fs_output_semantic_name
,
618 fs_output_semantic_index
, FALSE
);
620 stfp
->tgsi
.tokens
= ureg_get_tokens( ureg
, NULL
);
621 ureg_destroy( ureg
);
624 /* fill in variant */
625 variant
->driver_shader
= pipe
->create_fs_state(pipe
, &stfp
->tgsi
);
628 if (ST_DEBUG
& DEBUG_TGSI
) {
629 tgsi_dump( stfp
->tgsi
.tokens
, 0/*TGSI_DUMP_VERBOSE*/ );
638 * Translate fragment program if needed.
640 struct st_fp_variant
*
641 st_get_fp_variant(struct st_context
*st
,
642 struct st_fragment_program
*stfp
,
643 const struct st_fp_variant_key
*key
)
645 struct st_fp_variant
*fpv
;
647 /* Search for existing variant */
648 for (fpv
= stfp
->variants
; fpv
; fpv
= fpv
->next
) {
649 if (memcmp(&fpv
->key
, key
, sizeof(*key
)) == 0) {
656 fpv
= st_translate_fragment_program(st
, stfp
, key
);
658 /* insert into list */
659 fpv
->next
= stfp
->variants
;
660 stfp
->variants
= fpv
;
669 * Translate a geometry program to create a new variant.
671 static struct st_gp_variant
*
672 st_translate_geometry_program(struct st_context
*st
,
673 struct st_geometry_program
*stgp
,
674 const struct st_gp_variant_key
*key
)
676 GLuint inputMapping
[GEOM_ATTRIB_MAX
];
677 GLuint outputMapping
[GEOM_RESULT_MAX
];
678 struct pipe_context
*pipe
= st
->pipe
;
679 enum pipe_error error
;
681 const GLbitfield inputsRead
= stgp
->Base
.Base
.InputsRead
;
683 GLuint num_generic
= 0;
685 uint gs_num_inputs
= 0;
686 uint gs_builtin_inputs
= 0;
687 uint gs_array_offset
= 0;
689 ubyte gs_output_semantic_name
[PIPE_MAX_SHADER_OUTPUTS
];
690 ubyte gs_output_semantic_index
[PIPE_MAX_SHADER_OUTPUTS
];
691 uint gs_num_outputs
= 0;
695 struct ureg_program
*ureg
;
697 struct st_gp_variant
*gpv
;
699 gpv
= CALLOC_STRUCT(st_gp_variant
);
703 _mesa_remove_output_reads(&stgp
->Base
.Base
, PROGRAM_OUTPUT
);
704 _mesa_remove_output_reads(&stgp
->Base
.Base
, PROGRAM_VARYING
);
706 ureg
= ureg_create( TGSI_PROCESSOR_GEOMETRY
);
712 /* which vertex output goes to the first geometry input */
715 memset(inputMapping
, 0, sizeof(inputMapping
));
716 memset(outputMapping
, 0, sizeof(outputMapping
));
719 * Convert Mesa program inputs to TGSI input register semantics.
721 for (attr
= 0; attr
< GEOM_ATTRIB_MAX
; attr
++) {
722 if (inputsRead
& (1 << attr
)) {
723 const GLuint slot
= gs_num_inputs
;
727 inputMapping
[attr
] = slot
;
729 stgp
->input_map
[slot
+ gs_array_offset
] = vslot
- gs_builtin_inputs
;
730 stgp
->input_to_index
[attr
] = vslot
;
731 stgp
->index_to_input
[vslot
] = attr
;
734 if (attr
!= GEOM_ATTRIB_PRIMITIVE_ID
) {
735 gs_array_offset
+= 2;
740 debug_printf("input map at %d = %d\n",
741 slot
+ gs_array_offset
, stgp
->input_map
[slot
+ gs_array_offset
]);
745 case GEOM_ATTRIB_PRIMITIVE_ID
:
746 stgp
->input_semantic_name
[slot
] = TGSI_SEMANTIC_PRIMID
;
747 stgp
->input_semantic_index
[slot
] = 0;
749 case GEOM_ATTRIB_POSITION
:
750 stgp
->input_semantic_name
[slot
] = TGSI_SEMANTIC_POSITION
;
751 stgp
->input_semantic_index
[slot
] = 0;
753 case GEOM_ATTRIB_COLOR0
:
754 stgp
->input_semantic_name
[slot
] = TGSI_SEMANTIC_COLOR
;
755 stgp
->input_semantic_index
[slot
] = 0;
757 case GEOM_ATTRIB_COLOR1
:
758 stgp
->input_semantic_name
[slot
] = TGSI_SEMANTIC_COLOR
;
759 stgp
->input_semantic_index
[slot
] = 1;
761 case GEOM_ATTRIB_FOG_FRAG_COORD
:
762 stgp
->input_semantic_name
[slot
] = TGSI_SEMANTIC_FOG
;
763 stgp
->input_semantic_index
[slot
] = 0;
765 case GEOM_ATTRIB_TEX_COORD
:
766 stgp
->input_semantic_name
[slot
] = TGSI_SEMANTIC_GENERIC
;
767 stgp
->input_semantic_index
[slot
] = num_generic
++;
769 case GEOM_ATTRIB_VAR0
:
772 stgp
->input_semantic_name
[slot
] = TGSI_SEMANTIC_GENERIC
;
773 stgp
->input_semantic_index
[slot
] = num_generic
++;
778 /* initialize output semantics to defaults */
779 for (i
= 0; i
< PIPE_MAX_SHADER_OUTPUTS
; i
++) {
780 gs_output_semantic_name
[i
] = TGSI_SEMANTIC_GENERIC
;
781 gs_output_semantic_index
[i
] = 0;
786 * Determine number of outputs, the (default) output register
787 * mapping and the semantic information for each output.
789 for (attr
= 0; attr
< GEOM_RESULT_MAX
; attr
++) {
790 if (stgp
->Base
.Base
.OutputsWritten
& (1 << attr
)) {
793 slot
= gs_num_outputs
;
795 outputMapping
[attr
] = slot
;
798 case GEOM_RESULT_POS
:
800 gs_output_semantic_name
[slot
] = TGSI_SEMANTIC_POSITION
;
801 gs_output_semantic_index
[slot
] = 0;
803 case GEOM_RESULT_COL0
:
804 gs_output_semantic_name
[slot
] = TGSI_SEMANTIC_COLOR
;
805 gs_output_semantic_index
[slot
] = 0;
807 case GEOM_RESULT_COL1
:
808 gs_output_semantic_name
[slot
] = TGSI_SEMANTIC_COLOR
;
809 gs_output_semantic_index
[slot
] = 1;
811 case GEOM_RESULT_SCOL0
:
812 gs_output_semantic_name
[slot
] = TGSI_SEMANTIC_BCOLOR
;
813 gs_output_semantic_index
[slot
] = 0;
815 case GEOM_RESULT_SCOL1
:
816 gs_output_semantic_name
[slot
] = TGSI_SEMANTIC_BCOLOR
;
817 gs_output_semantic_index
[slot
] = 1;
819 case GEOM_RESULT_FOGC
:
820 gs_output_semantic_name
[slot
] = TGSI_SEMANTIC_FOG
;
821 gs_output_semantic_index
[slot
] = 0;
823 case GEOM_RESULT_PSIZ
:
824 gs_output_semantic_name
[slot
] = TGSI_SEMANTIC_PSIZE
;
825 gs_output_semantic_index
[slot
] = 0;
827 case GEOM_RESULT_TEX0
:
828 case GEOM_RESULT_TEX1
:
829 case GEOM_RESULT_TEX2
:
830 case GEOM_RESULT_TEX3
:
831 case GEOM_RESULT_TEX4
:
832 case GEOM_RESULT_TEX5
:
833 case GEOM_RESULT_TEX6
:
834 case GEOM_RESULT_TEX7
:
836 case GEOM_RESULT_VAR0
:
839 assert(slot
< Elements(gs_output_semantic_name
));
840 /* use default semantic info */
841 gs_output_semantic_name
[slot
] = TGSI_SEMANTIC_GENERIC
;
842 gs_output_semantic_index
[slot
] = num_generic
++;
847 assert(gs_output_semantic_name
[0] == TGSI_SEMANTIC_POSITION
);
849 /* find max output slot referenced to compute gs_num_outputs */
850 for (attr
= 0; attr
< GEOM_RESULT_MAX
; attr
++) {
851 if (outputMapping
[attr
] != ~0 && outputMapping
[attr
] > maxSlot
)
852 maxSlot
= outputMapping
[attr
];
854 gs_num_outputs
= maxSlot
+ 1;
859 printf("outputMapping? %d\n", outputMapping
? 1 : 0);
861 printf("attr -> slot\n");
862 for (i
= 0; i
< 16; i
++) {
863 printf(" %2d %3d\n", i
, outputMapping
[i
]);
866 printf("slot sem_name sem_index\n");
867 for (i
= 0; i
< gs_num_outputs
; i
++) {
868 printf(" %2d %d %d\n",
870 gs_output_semantic_name
[i
],
871 gs_output_semantic_index
[i
]);
876 /* free old shader state, if any */
877 if (stgp
->tgsi
.tokens
) {
878 st_free_tokens(stgp
->tgsi
.tokens
);
879 stgp
->tgsi
.tokens
= NULL
;
882 ureg_property_gs_input_prim(ureg
, stgp
->Base
.InputType
);
883 ureg_property_gs_output_prim(ureg
, stgp
->Base
.OutputType
);
884 ureg_property_gs_max_vertices(ureg
, stgp
->Base
.VerticesOut
);
886 error
= st_translate_mesa_program(st
->ctx
,
887 TGSI_PROCESSOR_GEOMETRY
,
893 stgp
->input_semantic_name
,
894 stgp
->input_semantic_index
,
899 gs_output_semantic_name
,
900 gs_output_semantic_index
,
903 stgp
->num_inputs
= gs_num_inputs
;
904 stgp
->tgsi
.tokens
= ureg_get_tokens( ureg
, NULL
);
905 ureg_destroy( ureg
);
907 /* fill in new variant */
908 gpv
->driver_shader
= pipe
->create_gs_state(pipe
, &stgp
->tgsi
);
911 if ((ST_DEBUG
& DEBUG_TGSI
) && (ST_DEBUG
& DEBUG_MESA
)) {
912 _mesa_print_program(&stgp
->Base
.Base
);
916 if (ST_DEBUG
& DEBUG_TGSI
) {
917 tgsi_dump(stgp
->tgsi
.tokens
, 0);
926 * Get/create geometry program variant.
928 struct st_gp_variant
*
929 st_get_gp_variant(struct st_context
*st
,
930 struct st_geometry_program
*stgp
,
931 const struct st_gp_variant_key
*key
)
933 struct st_gp_variant
*gpv
;
935 /* Search for existing variant */
936 for (gpv
= stgp
->variants
; gpv
; gpv
= gpv
->next
) {
937 if (memcmp(&gpv
->key
, key
, sizeof(*key
)) == 0) {
944 gpv
= st_translate_geometry_program(st
, stgp
, key
);
946 /* insert into list */
947 gpv
->next
= stgp
->variants
;
948 stgp
->variants
= gpv
;
959 * Debug- print current shader text
962 st_print_shaders(struct gl_context
*ctx
)
964 struct gl_shader_program
*shProg
[3] = {
965 ctx
->Shader
.CurrentVertexProgram
,
966 ctx
->Shader
.CurrentGeometryProgram
,
967 ctx
->Shader
.CurrentFragmentProgram
,
971 for (j
= 0; j
< 3; j
++) {
974 if (shProg
[j
] == NULL
)
977 for (i
= 0; i
< shProg
[j
]->NumShaders
; i
++) {
978 struct gl_shader
*sh
;
980 switch (shProg
[j
]->Shaders
[i
]->Type
) {
981 case GL_VERTEX_SHADER
:
982 sh
= (i
!= 0) ? NULL
: shProg
[j
]->Shaders
[i
];
984 case GL_GEOMETRY_SHADER_ARB
:
985 sh
= (i
!= 1) ? NULL
: shProg
[j
]->Shaders
[i
];
987 case GL_FRAGMENT_SHADER
:
988 sh
= (i
!= 2) ? NULL
: shProg
[j
]->Shaders
[i
];
997 printf("GLSL shader %u of %u:\n", i
, shProg
[j
]->NumShaders
);
998 printf("%s\n", sh
->Source
);
1006 * Vert/Geom/Frag programs have per-context variants. Free all the
1007 * variants attached to the given program which match the given context.
1010 destroy_program_variants(struct st_context
*st
, struct gl_program
*program
)
1015 switch (program
->Target
) {
1016 case GL_VERTEX_PROGRAM_ARB
:
1018 struct st_vertex_program
*stvp
= (struct st_vertex_program
*) program
;
1019 struct st_vp_variant
*vpv
, **prevPtr
= &stvp
->variants
;
1021 for (vpv
= stvp
->variants
; vpv
; ) {
1022 struct st_vp_variant
*next
= vpv
->next
;
1023 if (vpv
->key
.st
== st
) {
1024 /* unlink from list */
1026 /* destroy this variant */
1027 delete_vp_variant(st
, vpv
);
1030 prevPtr
= &vpv
->next
;
1036 case GL_FRAGMENT_PROGRAM_ARB
:
1038 struct st_fragment_program
*stfp
=
1039 (struct st_fragment_program
*) program
;
1040 struct st_fp_variant
*fpv
, **prevPtr
= &stfp
->variants
;
1042 for (fpv
= stfp
->variants
; fpv
; ) {
1043 struct st_fp_variant
*next
= fpv
->next
;
1044 if (fpv
->key
.st
== st
) {
1045 /* unlink from list */
1047 /* destroy this variant */
1048 delete_fp_variant(st
, fpv
);
1051 prevPtr
= &fpv
->next
;
1057 case MESA_GEOMETRY_PROGRAM
:
1059 struct st_geometry_program
*stgp
=
1060 (struct st_geometry_program
*) program
;
1061 struct st_gp_variant
*gpv
, **prevPtr
= &stgp
->variants
;
1063 for (gpv
= stgp
->variants
; gpv
; ) {
1064 struct st_gp_variant
*next
= gpv
->next
;
1065 if (gpv
->key
.st
== st
) {
1066 /* unlink from list */
1068 /* destroy this variant */
1069 delete_gp_variant(st
, gpv
);
1072 prevPtr
= &gpv
->next
;
1079 _mesa_problem(NULL
, "Unexpected program target in "
1080 "destroy_program_variants_cb()");
1086 * Callback for _mesa_HashWalk. Free all the shader's program variants
1087 * which match the given context.
1090 destroy_shader_program_variants_cb(GLuint key
, void *data
, void *userData
)
1092 struct st_context
*st
= (struct st_context
*) userData
;
1093 struct gl_shader
*shader
= (struct gl_shader
*) data
;
1095 switch (shader
->Type
) {
1096 case GL_SHADER_PROGRAM_MESA
:
1098 struct gl_shader_program
*shProg
= (struct gl_shader_program
*) data
;
1101 for (i
= 0; i
< shProg
->NumShaders
; i
++) {
1102 destroy_program_variants(st
, shProg
->Shaders
[i
]->Program
);
1105 destroy_program_variants(st
, (struct gl_program
*)
1106 shProg
->VertexProgram
);
1107 destroy_program_variants(st
, (struct gl_program
*)
1108 shProg
->FragmentProgram
);
1109 destroy_program_variants(st
, (struct gl_program
*)
1110 shProg
->GeometryProgram
);
1113 case GL_VERTEX_SHADER
:
1114 case GL_FRAGMENT_SHADER
:
1115 case GL_GEOMETRY_SHADER
:
1117 destroy_program_variants(st
, shader
->Program
);
1127 * Callback for _mesa_HashWalk. Free all the program variants which match
1128 * the given context.
1131 destroy_program_variants_cb(GLuint key
, void *data
, void *userData
)
1133 struct st_context
*st
= (struct st_context
*) userData
;
1134 struct gl_program
*program
= (struct gl_program
*) data
;
1135 destroy_program_variants(st
, program
);
1140 * Walk over all shaders and programs to delete any variants which
1141 * belong to the given context.
1142 * This is called during context tear-down.
1145 st_destroy_program_variants(struct st_context
*st
)
1147 /* ARB vert/frag program */
1148 _mesa_HashWalk(st
->ctx
->Shared
->Programs
,
1149 destroy_program_variants_cb
, st
);
1151 /* GLSL vert/frag/geom shaders */
1152 _mesa_HashWalk(st
->ctx
->Shared
->ShaderObjects
,
1153 destroy_shader_program_variants_cb
, st
);