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/mtypes.h"
37 #include "program/prog_parameter.h"
38 #include "program/prog_print.h"
39 #include "program/programopt.h"
41 #include "pipe/p_context.h"
42 #include "pipe/p_defines.h"
43 #include "pipe/p_shader_tokens.h"
44 #include "draw/draw_context.h"
45 #include "tgsi/tgsi_dump.h"
46 #include "tgsi/tgsi_ureg.h"
49 #include "st_cb_bitmap.h"
50 #include "st_cb_drawpixels.h"
51 #include "st_context.h"
52 #include "st_program.h"
53 #include "st_mesa_to_tgsi.h"
54 #include "cso_cache/cso_context.h"
59 * Delete a vertex program variant. Note the caller must unlink
60 * the variant from the linked list.
63 delete_vp_variant(struct st_context
*st
, struct st_vp_variant
*vpv
)
65 if (vpv
->driver_shader
)
66 cso_delete_vertex_shader(st
->cso_context
, vpv
->driver_shader
);
68 #if FEATURE_feedback || FEATURE_rastpos
70 draw_delete_vertex_shader( st
->draw
, vpv
->draw_shader
);
74 st_free_tokens(vpv
->tgsi
.tokens
);
82 * Clean out any old compilations:
85 st_release_vp_variants( struct st_context
*st
,
86 struct st_vertex_program
*stvp
)
88 struct st_vp_variant
*vpv
;
90 for (vpv
= stvp
->variants
; vpv
; ) {
91 struct st_vp_variant
*next
= vpv
->next
;
92 delete_vp_variant(st
, vpv
);
96 stvp
->variants
= NULL
;
102 * Delete a fragment program variant. Note the caller must unlink
103 * the variant from the linked list.
106 delete_fp_variant(struct st_context
*st
, struct st_fp_variant
*fpv
)
108 if (fpv
->driver_shader
)
109 cso_delete_fragment_shader(st
->cso_context
, fpv
->driver_shader
);
116 * Free all variants of a fragment program.
119 st_release_fp_variants(struct st_context
*st
, struct st_fragment_program
*stfp
)
121 struct st_fp_variant
*fpv
;
123 for (fpv
= stfp
->variants
; fpv
; ) {
124 struct st_fp_variant
*next
= fpv
->next
;
125 delete_fp_variant(st
, fpv
);
129 stfp
->variants
= NULL
;
134 * Delete a geometry program variant. Note the caller must unlink
135 * the variant from the linked list.
138 delete_gp_variant(struct st_context
*st
, struct st_gp_variant
*gpv
)
140 if (gpv
->driver_shader
)
141 cso_delete_geometry_shader(st
->cso_context
, gpv
->driver_shader
);
148 * Free all variants of a geometry program.
151 st_release_gp_variants(struct st_context
*st
, struct st_geometry_program
*stgp
)
153 struct st_gp_variant
*gpv
;
155 for (gpv
= stgp
->variants
; gpv
; ) {
156 struct st_gp_variant
*next
= gpv
->next
;
157 delete_gp_variant(st
, gpv
);
161 stgp
->variants
= NULL
;
168 * Translate a Mesa vertex shader into a TGSI shader.
169 * \param outputMapping to map vertex program output registers (VERT_RESULT_x)
170 * to TGSI output slots
171 * \param tokensOut destination for TGSI tokens
172 * \return pointer to cached pipe_shader object.
175 st_prepare_vertex_program(struct st_context
*st
,
176 struct st_vertex_program
*stvp
)
180 stvp
->num_inputs
= 0;
181 stvp
->num_outputs
= 0;
183 if (stvp
->Base
.IsPositionInvariant
)
184 _mesa_insert_mvp_code(st
->ctx
, &stvp
->Base
);
186 assert(stvp
->Base
.Base
.NumInstructions
> 1);
189 * Determine number of inputs, the mappings between VERT_ATTRIB_x
190 * and TGSI generic input indexes, plus input attrib semantic info.
192 for (attr
= 0; attr
< VERT_ATTRIB_MAX
; attr
++) {
193 if (stvp
->Base
.Base
.InputsRead
& (1 << attr
)) {
194 stvp
->input_to_index
[attr
] = stvp
->num_inputs
;
195 stvp
->index_to_input
[stvp
->num_inputs
] = attr
;
199 /* bit of a hack, presetup potentially unused edgeflag input */
200 stvp
->input_to_index
[VERT_ATTRIB_EDGEFLAG
] = stvp
->num_inputs
;
201 stvp
->index_to_input
[stvp
->num_inputs
] = VERT_ATTRIB_EDGEFLAG
;
203 /* Compute mapping of vertex program outputs to slots.
205 for (attr
= 0; attr
< VERT_RESULT_MAX
; attr
++) {
206 if ((stvp
->Base
.Base
.OutputsWritten
& BITFIELD64_BIT(attr
)) == 0) {
207 stvp
->result_to_output
[attr
] = ~0;
210 unsigned slot
= stvp
->num_outputs
++;
212 stvp
->result_to_output
[attr
] = slot
;
215 case VERT_RESULT_HPOS
:
216 stvp
->output_semantic_name
[slot
] = TGSI_SEMANTIC_POSITION
;
217 stvp
->output_semantic_index
[slot
] = 0;
219 case VERT_RESULT_COL0
:
220 stvp
->output_semantic_name
[slot
] = TGSI_SEMANTIC_COLOR
;
221 stvp
->output_semantic_index
[slot
] = 0;
223 case VERT_RESULT_COL1
:
224 stvp
->output_semantic_name
[slot
] = TGSI_SEMANTIC_COLOR
;
225 stvp
->output_semantic_index
[slot
] = 1;
227 case VERT_RESULT_BFC0
:
228 stvp
->output_semantic_name
[slot
] = TGSI_SEMANTIC_BCOLOR
;
229 stvp
->output_semantic_index
[slot
] = 0;
231 case VERT_RESULT_BFC1
:
232 stvp
->output_semantic_name
[slot
] = TGSI_SEMANTIC_BCOLOR
;
233 stvp
->output_semantic_index
[slot
] = 1;
235 case VERT_RESULT_FOGC
:
236 stvp
->output_semantic_name
[slot
] = TGSI_SEMANTIC_FOG
;
237 stvp
->output_semantic_index
[slot
] = 0;
239 case VERT_RESULT_PSIZ
:
240 stvp
->output_semantic_name
[slot
] = TGSI_SEMANTIC_PSIZE
;
241 stvp
->output_semantic_index
[slot
] = 0;
243 case VERT_RESULT_EDGE
:
247 case VERT_RESULT_TEX0
:
248 case VERT_RESULT_TEX1
:
249 case VERT_RESULT_TEX2
:
250 case VERT_RESULT_TEX3
:
251 case VERT_RESULT_TEX4
:
252 case VERT_RESULT_TEX5
:
253 case VERT_RESULT_TEX6
:
254 case VERT_RESULT_TEX7
:
255 stvp
->output_semantic_name
[slot
] = TGSI_SEMANTIC_GENERIC
;
256 stvp
->output_semantic_index
[slot
] = attr
- VERT_RESULT_TEX0
;
259 case VERT_RESULT_VAR0
:
261 assert(attr
< VERT_RESULT_MAX
);
262 stvp
->output_semantic_name
[slot
] = TGSI_SEMANTIC_GENERIC
;
263 stvp
->output_semantic_index
[slot
] = (FRAG_ATTRIB_VAR0
-
271 /* similar hack to above, presetup potentially unused edgeflag output */
272 stvp
->result_to_output
[VERT_RESULT_EDGE
] = stvp
->num_outputs
;
273 stvp
->output_semantic_name
[stvp
->num_outputs
] = TGSI_SEMANTIC_EDGEFLAG
;
274 stvp
->output_semantic_index
[stvp
->num_outputs
] = 0;
279 * Translate a vertex program to create a new variant.
281 static struct st_vp_variant
*
282 st_translate_vertex_program(struct st_context
*st
,
283 struct st_vertex_program
*stvp
,
284 const struct st_vp_variant_key
*key
)
286 struct st_vp_variant
*vpv
= CALLOC_STRUCT(st_vp_variant
);
287 struct pipe_context
*pipe
= st
->pipe
;
288 struct ureg_program
*ureg
;
289 enum pipe_error error
;
290 unsigned num_outputs
;
292 st_prepare_vertex_program( st
, stvp
);
294 _mesa_remove_output_reads(&stvp
->Base
.Base
, PROGRAM_OUTPUT
);
295 _mesa_remove_output_reads(&stvp
->Base
.Base
, PROGRAM_VARYING
);
297 ureg
= ureg_create( TGSI_PROCESSOR_VERTEX
);
305 vpv
->num_inputs
= stvp
->num_inputs
;
306 num_outputs
= stvp
->num_outputs
;
307 if (key
->passthrough_edgeflags
) {
312 if (ST_DEBUG
& DEBUG_MESA
) {
313 _mesa_print_program(&stvp
->Base
.Base
);
314 _mesa_print_program_parameters(st
->ctx
, &stvp
->Base
.Base
);
318 error
= st_translate_mesa_program(st
->ctx
,
319 TGSI_PROCESSOR_VERTEX
,
324 stvp
->input_to_index
,
325 NULL
, /* input semantic name */
326 NULL
, /* input semantic index */
330 stvp
->result_to_output
,
331 stvp
->output_semantic_name
,
332 stvp
->output_semantic_index
,
333 key
->passthrough_edgeflags
);
338 vpv
->tgsi
.tokens
= ureg_get_tokens( ureg
, NULL
);
339 if (!vpv
->tgsi
.tokens
)
342 ureg_destroy( ureg
);
344 vpv
->driver_shader
= pipe
->create_vs_state(pipe
, &vpv
->tgsi
);
346 if (ST_DEBUG
& DEBUG_TGSI
) {
347 tgsi_dump( vpv
->tgsi
.tokens
, 0 );
354 debug_printf("%s: failed to translate Mesa program:\n", __FUNCTION__
);
355 _mesa_print_program(&stvp
->Base
.Base
);
358 ureg_destroy( ureg
);
364 * Find/create a vertex program variant.
366 struct st_vp_variant
*
367 st_get_vp_variant(struct st_context
*st
,
368 struct st_vertex_program
*stvp
,
369 const struct st_vp_variant_key
*key
)
371 struct st_vp_variant
*vpv
;
373 /* Search for existing variant */
374 for (vpv
= stvp
->variants
; vpv
; vpv
= vpv
->next
) {
375 if (memcmp(&vpv
->key
, key
, sizeof(*key
)) == 0) {
382 vpv
= st_translate_vertex_program(st
, stvp
, key
);
384 /* insert into list */
385 vpv
->next
= stvp
->variants
;
386 stvp
->variants
= vpv
;
395 * Translate a Mesa fragment shader into a TGSI shader using extra info in
397 * \return new fragment program variant
399 static struct st_fp_variant
*
400 st_translate_fragment_program(struct st_context
*st
,
401 struct st_fragment_program
*stfp
,
402 const struct st_fp_variant_key
*key
)
404 struct pipe_context
*pipe
= st
->pipe
;
405 struct st_fp_variant
*variant
= CALLOC_STRUCT(st_fp_variant
);
410 assert(!(key
->bitmap
&& key
->drawpixels
));
414 /* glBitmap drawing */
415 struct gl_fragment_program
*fp
;
417 st_make_bitmap_fragment_program(st
, &stfp
->Base
,
418 &fp
, &variant
->bitmap_sampler
);
420 variant
->parameters
= _mesa_clone_parameter_list(fp
->Base
.Parameters
);
421 stfp
= st_fragment_program(fp
);
423 else if (key
->drawpixels
) {
424 /* glDrawPixels drawing */
425 struct gl_fragment_program
*fp
;
427 if (key
->drawpixels_z
|| key
->drawpixels_stencil
) {
428 fp
= st_make_drawpix_z_stencil_program(st
, key
->drawpixels_z
,
429 key
->drawpixels_stencil
);
433 st_make_drawpix_fragment_program(st
, &stfp
->Base
, &fp
);
434 variant
->parameters
= _mesa_clone_parameter_list(fp
->Base
.Parameters
);
436 stfp
= st_fragment_program(fp
);
440 if (!stfp
->tgsi
.tokens
) {
441 /* need to translate Mesa instructions to TGSI now */
442 GLuint outputMapping
[FRAG_RESULT_MAX
];
443 GLuint inputMapping
[FRAG_ATTRIB_MAX
];
444 GLuint interpMode
[PIPE_MAX_SHADER_INPUTS
]; /* XXX size? */
446 enum pipe_error error
;
447 const GLbitfield inputsRead
= stfp
->Base
.Base
.InputsRead
;
448 struct ureg_program
*ureg
;
449 GLboolean write_all
= GL_FALSE
;
451 ubyte input_semantic_name
[PIPE_MAX_SHADER_INPUTS
];
452 ubyte input_semantic_index
[PIPE_MAX_SHADER_INPUTS
];
453 uint fs_num_inputs
= 0;
455 ubyte fs_output_semantic_name
[PIPE_MAX_SHADER_OUTPUTS
];
456 ubyte fs_output_semantic_index
[PIPE_MAX_SHADER_OUTPUTS
];
457 uint fs_num_outputs
= 0;
460 _mesa_remove_output_reads(&stfp
->Base
.Base
, PROGRAM_OUTPUT
);
463 * Convert Mesa program inputs to TGSI input register semantics.
465 for (attr
= 0; attr
< FRAG_ATTRIB_MAX
; attr
++) {
466 if (inputsRead
& (1 << attr
)) {
467 const GLuint slot
= fs_num_inputs
++;
469 inputMapping
[attr
] = slot
;
472 case FRAG_ATTRIB_WPOS
:
473 input_semantic_name
[slot
] = TGSI_SEMANTIC_POSITION
;
474 input_semantic_index
[slot
] = 0;
475 interpMode
[slot
] = TGSI_INTERPOLATE_LINEAR
;
477 case FRAG_ATTRIB_COL0
:
478 input_semantic_name
[slot
] = TGSI_SEMANTIC_COLOR
;
479 input_semantic_index
[slot
] = 0;
480 interpMode
[slot
] = TGSI_INTERPOLATE_LINEAR
;
482 case FRAG_ATTRIB_COL1
:
483 input_semantic_name
[slot
] = TGSI_SEMANTIC_COLOR
;
484 input_semantic_index
[slot
] = 1;
485 interpMode
[slot
] = TGSI_INTERPOLATE_LINEAR
;
487 case FRAG_ATTRIB_FOGC
:
488 input_semantic_name
[slot
] = TGSI_SEMANTIC_FOG
;
489 input_semantic_index
[slot
] = 0;
490 interpMode
[slot
] = TGSI_INTERPOLATE_PERSPECTIVE
;
492 case FRAG_ATTRIB_FACE
:
493 input_semantic_name
[slot
] = TGSI_SEMANTIC_FACE
;
494 input_semantic_index
[slot
] = 0;
495 interpMode
[slot
] = TGSI_INTERPOLATE_CONSTANT
;
497 /* In most cases, there is nothing special about these
498 * inputs, so adopt a convention to use the generic
499 * semantic name and the mesa FRAG_ATTRIB_ number as the
502 * All that is required is that the vertex shader labels
503 * its own outputs similarly, and that the vertex shader
504 * generates at least every output required by the
505 * fragment shader plus fixed-function hardware (such as
508 * There is no requirement that semantic indexes start at
509 * zero or be restricted to a particular range -- nobody
510 * should be building tables based on semantic index.
512 case FRAG_ATTRIB_PNTC
:
513 case FRAG_ATTRIB_TEX0
:
514 case FRAG_ATTRIB_TEX1
:
515 case FRAG_ATTRIB_TEX2
:
516 case FRAG_ATTRIB_TEX3
:
517 case FRAG_ATTRIB_TEX4
:
518 case FRAG_ATTRIB_TEX5
:
519 case FRAG_ATTRIB_TEX6
:
520 case FRAG_ATTRIB_TEX7
:
521 case FRAG_ATTRIB_VAR0
:
523 /* Actually, let's try and zero-base this just for
524 * readability of the generated TGSI.
526 assert(attr
>= FRAG_ATTRIB_TEX0
);
527 input_semantic_index
[slot
] = (attr
- FRAG_ATTRIB_TEX0
);
528 input_semantic_name
[slot
] = TGSI_SEMANTIC_GENERIC
;
529 if (attr
== FRAG_ATTRIB_PNTC
)
530 interpMode
[slot
] = TGSI_INTERPOLATE_LINEAR
;
532 interpMode
[slot
] = TGSI_INTERPOLATE_PERSPECTIVE
;
537 inputMapping
[attr
] = -1;
542 * Semantics and mapping for outputs
546 GLbitfield64 outputsWritten
= stfp
->Base
.Base
.OutputsWritten
;
548 /* if z is written, emit that first */
549 if (outputsWritten
& BITFIELD64_BIT(FRAG_RESULT_DEPTH
)) {
550 fs_output_semantic_name
[fs_num_outputs
] = TGSI_SEMANTIC_POSITION
;
551 fs_output_semantic_index
[fs_num_outputs
] = 0;
552 outputMapping
[FRAG_RESULT_DEPTH
] = fs_num_outputs
;
554 outputsWritten
&= ~(1 << FRAG_RESULT_DEPTH
);
557 if (outputsWritten
& BITFIELD64_BIT(FRAG_RESULT_STENCIL
)) {
558 fs_output_semantic_name
[fs_num_outputs
] = TGSI_SEMANTIC_STENCIL
;
559 fs_output_semantic_index
[fs_num_outputs
] = 0;
560 outputMapping
[FRAG_RESULT_STENCIL
] = fs_num_outputs
;
562 outputsWritten
&= ~(1 << FRAG_RESULT_STENCIL
);
565 /* handle remaning outputs (color) */
566 for (attr
= 0; attr
< FRAG_RESULT_MAX
; attr
++) {
567 if (outputsWritten
& BITFIELD64_BIT(attr
)) {
569 case FRAG_RESULT_DEPTH
:
570 case FRAG_RESULT_STENCIL
:
574 case FRAG_RESULT_COLOR
:
575 write_all
= GL_TRUE
; /* fallthrough */
577 assert(attr
== FRAG_RESULT_COLOR
||
578 (FRAG_RESULT_DATA0
<= attr
&& attr
< FRAG_RESULT_MAX
));
579 fs_output_semantic_name
[fs_num_outputs
] = TGSI_SEMANTIC_COLOR
;
580 fs_output_semantic_index
[fs_num_outputs
] = numColors
;
581 outputMapping
[attr
] = fs_num_outputs
;
591 ureg
= ureg_create( TGSI_PROCESSOR_FRAGMENT
);
595 if (ST_DEBUG
& DEBUG_MESA
) {
596 _mesa_print_program(&stfp
->Base
.Base
);
597 _mesa_print_program_parameters(st
->ctx
, &stfp
->Base
.Base
);
600 if (write_all
== GL_TRUE
)
601 ureg_property_fs_color0_writes_all_cbufs(ureg
, 1);
603 error
= st_translate_mesa_program(st
->ctx
,
604 TGSI_PROCESSOR_FRAGMENT
,
611 input_semantic_index
,
616 fs_output_semantic_name
,
617 fs_output_semantic_index
, FALSE
);
619 stfp
->tgsi
.tokens
= ureg_get_tokens( ureg
, NULL
);
620 ureg_destroy( ureg
);
623 /* fill in variant */
624 variant
->driver_shader
= pipe
->create_fs_state(pipe
, &stfp
->tgsi
);
627 if (ST_DEBUG
& DEBUG_TGSI
) {
628 tgsi_dump( stfp
->tgsi
.tokens
, 0/*TGSI_DUMP_VERBOSE*/ );
637 * Translate fragment program if needed.
639 struct st_fp_variant
*
640 st_get_fp_variant(struct st_context
*st
,
641 struct st_fragment_program
*stfp
,
642 const struct st_fp_variant_key
*key
)
644 struct st_fp_variant
*fpv
;
646 /* Search for existing variant */
647 for (fpv
= stfp
->variants
; fpv
; fpv
= fpv
->next
) {
648 if (memcmp(&fpv
->key
, key
, sizeof(*key
)) == 0) {
655 fpv
= st_translate_fragment_program(st
, stfp
, key
);
657 /* insert into list */
658 fpv
->next
= stfp
->variants
;
659 stfp
->variants
= fpv
;
668 * Translate a geometry program to create a new variant.
670 static struct st_gp_variant
*
671 st_translate_geometry_program(struct st_context
*st
,
672 struct st_geometry_program
*stgp
,
673 const struct st_gp_variant_key
*key
)
675 GLuint inputMapping
[GEOM_ATTRIB_MAX
];
676 GLuint outputMapping
[GEOM_RESULT_MAX
];
677 struct pipe_context
*pipe
= st
->pipe
;
678 enum pipe_error error
;
680 const GLbitfield inputsRead
= stgp
->Base
.Base
.InputsRead
;
682 GLuint num_generic
= 0;
684 uint gs_num_inputs
= 0;
685 uint gs_builtin_inputs
= 0;
686 uint gs_array_offset
= 0;
688 ubyte gs_output_semantic_name
[PIPE_MAX_SHADER_OUTPUTS
];
689 ubyte gs_output_semantic_index
[PIPE_MAX_SHADER_OUTPUTS
];
690 uint gs_num_outputs
= 0;
694 struct ureg_program
*ureg
;
696 struct st_gp_variant
*gpv
;
698 gpv
= CALLOC_STRUCT(st_gp_variant
);
702 _mesa_remove_output_reads(&stgp
->Base
.Base
, PROGRAM_OUTPUT
);
703 _mesa_remove_output_reads(&stgp
->Base
.Base
, PROGRAM_VARYING
);
705 ureg
= ureg_create( TGSI_PROCESSOR_GEOMETRY
);
711 /* which vertex output goes to the first geometry input */
714 memset(inputMapping
, 0, sizeof(inputMapping
));
715 memset(outputMapping
, 0, sizeof(outputMapping
));
718 * Convert Mesa program inputs to TGSI input register semantics.
720 for (attr
= 0; attr
< GEOM_ATTRIB_MAX
; attr
++) {
721 if (inputsRead
& (1 << attr
)) {
722 const GLuint slot
= gs_num_inputs
;
726 inputMapping
[attr
] = slot
;
728 stgp
->input_map
[slot
+ gs_array_offset
] = vslot
- gs_builtin_inputs
;
729 stgp
->input_to_index
[attr
] = vslot
;
730 stgp
->index_to_input
[vslot
] = attr
;
733 if (attr
!= GEOM_ATTRIB_PRIMITIVE_ID
) {
734 gs_array_offset
+= 2;
739 debug_printf("input map at %d = %d\n",
740 slot
+ gs_array_offset
, stgp
->input_map
[slot
+ gs_array_offset
]);
744 case GEOM_ATTRIB_PRIMITIVE_ID
:
745 stgp
->input_semantic_name
[slot
] = TGSI_SEMANTIC_PRIMID
;
746 stgp
->input_semantic_index
[slot
] = 0;
748 case GEOM_ATTRIB_POSITION
:
749 stgp
->input_semantic_name
[slot
] = TGSI_SEMANTIC_POSITION
;
750 stgp
->input_semantic_index
[slot
] = 0;
752 case GEOM_ATTRIB_COLOR0
:
753 stgp
->input_semantic_name
[slot
] = TGSI_SEMANTIC_COLOR
;
754 stgp
->input_semantic_index
[slot
] = 0;
756 case GEOM_ATTRIB_COLOR1
:
757 stgp
->input_semantic_name
[slot
] = TGSI_SEMANTIC_COLOR
;
758 stgp
->input_semantic_index
[slot
] = 1;
760 case GEOM_ATTRIB_FOG_FRAG_COORD
:
761 stgp
->input_semantic_name
[slot
] = TGSI_SEMANTIC_FOG
;
762 stgp
->input_semantic_index
[slot
] = 0;
764 case GEOM_ATTRIB_TEX_COORD
:
765 stgp
->input_semantic_name
[slot
] = TGSI_SEMANTIC_GENERIC
;
766 stgp
->input_semantic_index
[slot
] = num_generic
++;
768 case GEOM_ATTRIB_VAR0
:
771 stgp
->input_semantic_name
[slot
] = TGSI_SEMANTIC_GENERIC
;
772 stgp
->input_semantic_index
[slot
] = num_generic
++;
777 /* initialize output semantics to defaults */
778 for (i
= 0; i
< PIPE_MAX_SHADER_OUTPUTS
; i
++) {
779 gs_output_semantic_name
[i
] = TGSI_SEMANTIC_GENERIC
;
780 gs_output_semantic_index
[i
] = 0;
785 * Determine number of outputs, the (default) output register
786 * mapping and the semantic information for each output.
788 for (attr
= 0; attr
< GEOM_RESULT_MAX
; attr
++) {
789 if (stgp
->Base
.Base
.OutputsWritten
& (1 << attr
)) {
792 slot
= gs_num_outputs
;
794 outputMapping
[attr
] = slot
;
797 case GEOM_RESULT_POS
:
799 gs_output_semantic_name
[slot
] = TGSI_SEMANTIC_POSITION
;
800 gs_output_semantic_index
[slot
] = 0;
802 case GEOM_RESULT_COL0
:
803 gs_output_semantic_name
[slot
] = TGSI_SEMANTIC_COLOR
;
804 gs_output_semantic_index
[slot
] = 0;
806 case GEOM_RESULT_COL1
:
807 gs_output_semantic_name
[slot
] = TGSI_SEMANTIC_COLOR
;
808 gs_output_semantic_index
[slot
] = 1;
810 case GEOM_RESULT_SCOL0
:
811 gs_output_semantic_name
[slot
] = TGSI_SEMANTIC_BCOLOR
;
812 gs_output_semantic_index
[slot
] = 0;
814 case GEOM_RESULT_SCOL1
:
815 gs_output_semantic_name
[slot
] = TGSI_SEMANTIC_BCOLOR
;
816 gs_output_semantic_index
[slot
] = 1;
818 case GEOM_RESULT_FOGC
:
819 gs_output_semantic_name
[slot
] = TGSI_SEMANTIC_FOG
;
820 gs_output_semantic_index
[slot
] = 0;
822 case GEOM_RESULT_PSIZ
:
823 gs_output_semantic_name
[slot
] = TGSI_SEMANTIC_PSIZE
;
824 gs_output_semantic_index
[slot
] = 0;
826 case GEOM_RESULT_TEX0
:
827 case GEOM_RESULT_TEX1
:
828 case GEOM_RESULT_TEX2
:
829 case GEOM_RESULT_TEX3
:
830 case GEOM_RESULT_TEX4
:
831 case GEOM_RESULT_TEX5
:
832 case GEOM_RESULT_TEX6
:
833 case GEOM_RESULT_TEX7
:
835 case GEOM_RESULT_VAR0
:
838 assert(slot
< Elements(gs_output_semantic_name
));
839 /* use default semantic info */
840 gs_output_semantic_name
[slot
] = TGSI_SEMANTIC_GENERIC
;
841 gs_output_semantic_index
[slot
] = num_generic
++;
846 assert(gs_output_semantic_name
[0] == TGSI_SEMANTIC_POSITION
);
848 /* find max output slot referenced to compute gs_num_outputs */
849 for (attr
= 0; attr
< GEOM_RESULT_MAX
; attr
++) {
850 if (outputMapping
[attr
] != ~0 && outputMapping
[attr
] > maxSlot
)
851 maxSlot
= outputMapping
[attr
];
853 gs_num_outputs
= maxSlot
+ 1;
858 printf("outputMapping? %d\n", outputMapping
? 1 : 0);
860 printf("attr -> slot\n");
861 for (i
= 0; i
< 16; i
++) {
862 printf(" %2d %3d\n", i
, outputMapping
[i
]);
865 printf("slot sem_name sem_index\n");
866 for (i
= 0; i
< gs_num_outputs
; i
++) {
867 printf(" %2d %d %d\n",
869 gs_output_semantic_name
[i
],
870 gs_output_semantic_index
[i
]);
875 /* free old shader state, if any */
876 if (stgp
->tgsi
.tokens
) {
877 st_free_tokens(stgp
->tgsi
.tokens
);
878 stgp
->tgsi
.tokens
= NULL
;
881 ureg_property_gs_input_prim(ureg
, stgp
->Base
.InputType
);
882 ureg_property_gs_output_prim(ureg
, stgp
->Base
.OutputType
);
883 ureg_property_gs_max_vertices(ureg
, stgp
->Base
.VerticesOut
);
885 error
= st_translate_mesa_program(st
->ctx
,
886 TGSI_PROCESSOR_GEOMETRY
,
892 stgp
->input_semantic_name
,
893 stgp
->input_semantic_index
,
898 gs_output_semantic_name
,
899 gs_output_semantic_index
,
902 stgp
->num_inputs
= gs_num_inputs
;
903 stgp
->tgsi
.tokens
= ureg_get_tokens( ureg
, NULL
);
904 ureg_destroy( ureg
);
906 /* fill in new variant */
907 gpv
->driver_shader
= pipe
->create_gs_state(pipe
, &stgp
->tgsi
);
910 if ((ST_DEBUG
& DEBUG_TGSI
) && (ST_DEBUG
& DEBUG_MESA
)) {
911 _mesa_print_program(&stgp
->Base
.Base
);
915 if (ST_DEBUG
& DEBUG_TGSI
) {
916 tgsi_dump(stgp
->tgsi
.tokens
, 0);
925 * Get/create geometry program variant.
927 struct st_gp_variant
*
928 st_get_gp_variant(struct st_context
*st
,
929 struct st_geometry_program
*stgp
,
930 const struct st_gp_variant_key
*key
)
932 struct st_gp_variant
*gpv
;
934 /* Search for existing variant */
935 for (gpv
= stgp
->variants
; gpv
; gpv
= gpv
->next
) {
936 if (memcmp(&gpv
->key
, key
, sizeof(*key
)) == 0) {
943 gpv
= st_translate_geometry_program(st
, stgp
, key
);
945 /* insert into list */
946 gpv
->next
= stgp
->variants
;
947 stgp
->variants
= gpv
;
958 * Debug- print current shader text
961 st_print_shaders(struct gl_context
*ctx
)
963 struct gl_shader_program
*shProg
[3] = {
964 ctx
->Shader
.CurrentVertexProgram
,
965 ctx
->Shader
.CurrentGeometryProgram
,
966 ctx
->Shader
.CurrentFragmentProgram
,
970 for (j
= 0; j
< 3; j
++) {
973 if (shProg
[j
] == NULL
)
976 for (i
= 0; i
< shProg
[j
]->NumShaders
; i
++) {
977 struct gl_shader
*sh
;
979 switch (shProg
[j
]->Shaders
[i
]->Type
) {
980 case GL_VERTEX_SHADER
:
981 sh
= (i
!= 0) ? NULL
: shProg
[j
]->Shaders
[i
];
983 case GL_GEOMETRY_SHADER_ARB
:
984 sh
= (i
!= 1) ? NULL
: shProg
[j
]->Shaders
[i
];
986 case GL_FRAGMENT_SHADER
:
987 sh
= (i
!= 2) ? NULL
: shProg
[j
]->Shaders
[i
];
996 printf("GLSL shader %u of %u:\n", i
, shProg
[j
]->NumShaders
);
997 printf("%s\n", sh
->Source
);
1005 * Vert/Geom/Frag programs have per-context variants. Free all the
1006 * variants attached to the given program which match the given context.
1009 destroy_program_variants(struct st_context
*st
, struct gl_program
*program
)
1014 switch (program
->Target
) {
1015 case GL_VERTEX_PROGRAM_ARB
:
1017 struct st_vertex_program
*stvp
= (struct st_vertex_program
*) program
;
1018 struct st_vp_variant
*vpv
, **prevPtr
= &stvp
->variants
;
1020 for (vpv
= stvp
->variants
; vpv
; ) {
1021 struct st_vp_variant
*next
= vpv
->next
;
1022 if (vpv
->key
.st
== st
) {
1023 /* unlink from list */
1025 /* destroy this variant */
1026 delete_vp_variant(st
, vpv
);
1029 prevPtr
= &vpv
->next
;
1035 case GL_FRAGMENT_PROGRAM_ARB
:
1037 struct st_fragment_program
*stfp
=
1038 (struct st_fragment_program
*) program
;
1039 struct st_fp_variant
*fpv
, **prevPtr
= &stfp
->variants
;
1041 for (fpv
= stfp
->variants
; fpv
; ) {
1042 struct st_fp_variant
*next
= fpv
->next
;
1043 if (fpv
->key
.st
== st
) {
1044 /* unlink from list */
1046 /* destroy this variant */
1047 delete_fp_variant(st
, fpv
);
1050 prevPtr
= &fpv
->next
;
1056 case MESA_GEOMETRY_PROGRAM
:
1058 struct st_geometry_program
*stgp
=
1059 (struct st_geometry_program
*) program
;
1060 struct st_gp_variant
*gpv
, **prevPtr
= &stgp
->variants
;
1062 for (gpv
= stgp
->variants
; gpv
; ) {
1063 struct st_gp_variant
*next
= gpv
->next
;
1064 if (gpv
->key
.st
== st
) {
1065 /* unlink from list */
1067 /* destroy this variant */
1068 delete_gp_variant(st
, gpv
);
1071 prevPtr
= &gpv
->next
;
1078 _mesa_problem(NULL
, "Unexpected program target in "
1079 "destroy_program_variants_cb()");
1085 * Callback for _mesa_HashWalk. Free all the shader's program variants
1086 * which match the given context.
1089 destroy_shader_program_variants_cb(GLuint key
, void *data
, void *userData
)
1091 struct st_context
*st
= (struct st_context
*) userData
;
1092 struct gl_shader
*shader
= (struct gl_shader
*) data
;
1094 switch (shader
->Type
) {
1095 case GL_SHADER_PROGRAM_MESA
:
1097 struct gl_shader_program
*shProg
= (struct gl_shader_program
*) data
;
1100 for (i
= 0; i
< shProg
->NumShaders
; i
++) {
1101 destroy_program_variants(st
, shProg
->Shaders
[i
]->Program
);
1104 destroy_program_variants(st
, (struct gl_program
*)
1105 shProg
->VertexProgram
);
1106 destroy_program_variants(st
, (struct gl_program
*)
1107 shProg
->FragmentProgram
);
1108 destroy_program_variants(st
, (struct gl_program
*)
1109 shProg
->GeometryProgram
);
1112 case GL_VERTEX_SHADER
:
1113 case GL_FRAGMENT_SHADER
:
1114 case GL_GEOMETRY_SHADER
:
1116 destroy_program_variants(st
, shader
->Program
);
1126 * Callback for _mesa_HashWalk. Free all the program variants which match
1127 * the given context.
1130 destroy_program_variants_cb(GLuint key
, void *data
, void *userData
)
1132 struct st_context
*st
= (struct st_context
*) userData
;
1133 struct gl_program
*program
= (struct gl_program
*) data
;
1134 destroy_program_variants(st
, program
);
1139 * Walk over all shaders and programs to delete any variants which
1140 * belong to the given context.
1141 * This is called during context tear-down.
1144 st_destroy_program_variants(struct st_context
*st
)
1146 /* ARB vert/frag program */
1147 _mesa_HashWalk(st
->ctx
->Shared
->Programs
,
1148 destroy_program_variants_cb
, st
);
1150 /* GLSL vert/frag/geom shaders */
1151 _mesa_HashWalk(st
->ctx
->Shared
->ShaderObjects
,
1152 destroy_shader_program_variants_cb
, st
);