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 varient. Note the caller must unlink
60 * the varient from the linked list.
63 delete_vp_varient(struct st_context
*st
, struct st_vp_varient
*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_vp_release_varients( struct st_context
*st
,
86 struct st_vertex_program
*stvp
)
88 struct st_vp_varient
*vpv
;
90 for (vpv
= stvp
->varients
; vpv
; ) {
91 struct st_vp_varient
*next
= vpv
->next
;
92 delete_vp_varient(st
, vpv
);
96 stvp
->varients
= NULL
;
102 * Delete a fragment program varient. Note the caller must unlink
103 * the varient from the linked list.
106 delete_fp_varient(struct st_context
*st
, struct st_fp_varient
*fpv
)
108 if (fpv
->driver_shader
)
109 cso_delete_fragment_shader(st
->cso_context
, fpv
->driver_shader
);
116 * Free all varients of a fragment program.
119 st_fp_release_varients(struct st_context
*st
, struct st_fragment_program
*stfp
)
121 struct st_fp_varient
*fpv
;
123 for (fpv
= stfp
->varients
; fpv
; ) {
124 struct st_fp_varient
*next
= fpv
->next
;
125 delete_fp_varient(st
, fpv
);
129 stfp
->varients
= NULL
;
134 * Delete a geometry program varient. Note the caller must unlink
135 * the varient from the linked list.
138 delete_gp_varient(struct st_context
*st
, struct st_gp_varient
*gpv
)
140 if (gpv
->driver_shader
)
141 cso_delete_geometry_shader(st
->cso_context
, gpv
->driver_shader
);
148 * Free all varients of a geometry program.
151 st_gp_release_varients(struct st_context
*st
, struct st_geometry_program
*stgp
)
153 struct st_gp_varient
*gpv
;
155 for (gpv
= stgp
->varients
; gpv
; ) {
156 struct st_gp_varient
*next
= gpv
->next
;
157 delete_gp_varient(st
, gpv
);
161 stgp
->varients
= 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 varient.
281 static struct st_vp_varient
*
282 st_translate_vertex_program(struct st_context
*st
,
283 struct st_vertex_program
*stvp
,
284 const struct st_vp_varient_key
*key
)
286 struct st_vp_varient
*vpv
= CALLOC_STRUCT(st_vp_varient
);
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
);
319 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 varient.
367 struct st_vp_varient
*
368 st_get_vp_varient(struct st_context
*st
,
369 struct st_vertex_program
*stvp
,
370 const struct st_vp_varient_key
*key
)
372 struct st_vp_varient
*vpv
;
374 /* Search for existing varient */
375 for (vpv
= stvp
->varients
; 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
->varients
;
387 stvp
->varients
= 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_varient
*
401 st_translate_fragment_program(struct st_context
*st
,
402 struct st_fragment_program
*stfp
,
403 const struct st_fp_varient_key
*key
)
405 struct pipe_context
*pipe
= st
->pipe
;
406 struct st_fp_varient
*varient
= CALLOC_STRUCT(st_fp_varient
);
411 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
, &varient
->bitmap_sampler
);
420 varient
->parameters
= _mesa_clone_parameter_list(fp
->Base
.Parameters
);
421 stfp
= (struct 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 varient
->parameters
= _mesa_clone_parameter_list(fp
->Base
.Parameters
);
436 stfp
= (struct st_fragment_program
*) fp
;
439 if (!stfp
->tgsi
.tokens
) {
440 /* need to translate Mesa instructions to TGSI now */
441 GLuint outputMapping
[FRAG_RESULT_MAX
];
442 GLuint inputMapping
[FRAG_ATTRIB_MAX
];
443 GLuint interpMode
[PIPE_MAX_SHADER_INPUTS
]; /* XXX size? */
445 enum pipe_error error
;
446 const GLbitfield inputsRead
= stfp
->Base
.Base
.InputsRead
;
447 struct ureg_program
*ureg
;
449 ubyte input_semantic_name
[PIPE_MAX_SHADER_INPUTS
];
450 ubyte input_semantic_index
[PIPE_MAX_SHADER_INPUTS
];
451 uint fs_num_inputs
= 0;
453 ubyte fs_output_semantic_name
[PIPE_MAX_SHADER_OUTPUTS
];
454 ubyte fs_output_semantic_index
[PIPE_MAX_SHADER_OUTPUTS
];
455 uint fs_num_outputs
= 0;
458 _mesa_remove_output_reads(&stfp
->Base
.Base
, PROGRAM_OUTPUT
);
461 * Convert Mesa program inputs to TGSI input register semantics.
463 for (attr
= 0; attr
< FRAG_ATTRIB_MAX
; attr
++) {
464 if (inputsRead
& (1 << attr
)) {
465 const GLuint slot
= fs_num_inputs
++;
467 inputMapping
[attr
] = slot
;
470 case FRAG_ATTRIB_WPOS
:
471 input_semantic_name
[slot
] = TGSI_SEMANTIC_POSITION
;
472 input_semantic_index
[slot
] = 0;
473 interpMode
[slot
] = TGSI_INTERPOLATE_LINEAR
;
475 case FRAG_ATTRIB_COL0
:
476 input_semantic_name
[slot
] = TGSI_SEMANTIC_COLOR
;
477 input_semantic_index
[slot
] = 0;
478 interpMode
[slot
] = TGSI_INTERPOLATE_LINEAR
;
480 case FRAG_ATTRIB_COL1
:
481 input_semantic_name
[slot
] = TGSI_SEMANTIC_COLOR
;
482 input_semantic_index
[slot
] = 1;
483 interpMode
[slot
] = TGSI_INTERPOLATE_LINEAR
;
485 case FRAG_ATTRIB_FOGC
:
486 input_semantic_name
[slot
] = TGSI_SEMANTIC_FOG
;
487 input_semantic_index
[slot
] = 0;
488 interpMode
[slot
] = TGSI_INTERPOLATE_PERSPECTIVE
;
490 case FRAG_ATTRIB_FACE
:
491 input_semantic_name
[slot
] = TGSI_SEMANTIC_FACE
;
492 input_semantic_index
[slot
] = 0;
493 interpMode
[slot
] = TGSI_INTERPOLATE_CONSTANT
;
495 /* In most cases, there is nothing special about these
496 * inputs, so adopt a convention to use the generic
497 * semantic name and the mesa FRAG_ATTRIB_ number as the
500 * All that is required is that the vertex shader labels
501 * its own outputs similarly, and that the vertex shader
502 * generates at least every output required by the
503 * fragment shader plus fixed-function hardware (such as
506 * There is no requirement that semantic indexes start at
507 * zero or be restricted to a particular range -- nobody
508 * should be building tables based on semantic index.
510 case FRAG_ATTRIB_PNTC
:
511 case FRAG_ATTRIB_TEX0
:
512 case FRAG_ATTRIB_TEX1
:
513 case FRAG_ATTRIB_TEX2
:
514 case FRAG_ATTRIB_TEX3
:
515 case FRAG_ATTRIB_TEX4
:
516 case FRAG_ATTRIB_TEX5
:
517 case FRAG_ATTRIB_TEX6
:
518 case FRAG_ATTRIB_TEX7
:
519 case FRAG_ATTRIB_VAR0
:
521 /* Actually, let's try and zero-base this just for
522 * readability of the generated TGSI.
524 assert(attr
>= FRAG_ATTRIB_TEX0
);
525 input_semantic_index
[slot
] = (attr
- FRAG_ATTRIB_TEX0
);
526 input_semantic_name
[slot
] = TGSI_SEMANTIC_GENERIC
;
527 if (attr
== FRAG_ATTRIB_PNTC
)
528 interpMode
[slot
] = TGSI_INTERPOLATE_LINEAR
;
530 interpMode
[slot
] = TGSI_INTERPOLATE_PERSPECTIVE
;
535 inputMapping
[attr
] = -1;
540 * Semantics and mapping for outputs
544 GLbitfield64 outputsWritten
= stfp
->Base
.Base
.OutputsWritten
;
546 /* if z is written, emit that first */
547 if (outputsWritten
& BITFIELD64_BIT(FRAG_RESULT_DEPTH
)) {
548 fs_output_semantic_name
[fs_num_outputs
] = TGSI_SEMANTIC_POSITION
;
549 fs_output_semantic_index
[fs_num_outputs
] = 0;
550 outputMapping
[FRAG_RESULT_DEPTH
] = fs_num_outputs
;
552 outputsWritten
&= ~(1 << FRAG_RESULT_DEPTH
);
555 if (outputsWritten
& BITFIELD64_BIT(FRAG_RESULT_STENCIL
)) {
556 fs_output_semantic_name
[fs_num_outputs
] = TGSI_SEMANTIC_STENCIL
;
557 fs_output_semantic_index
[fs_num_outputs
] = 0;
558 outputMapping
[FRAG_RESULT_STENCIL
] = fs_num_outputs
;
560 outputsWritten
&= ~(1 << FRAG_RESULT_STENCIL
);
563 /* handle remaning outputs (color) */
564 for (attr
= 0; attr
< FRAG_RESULT_MAX
; attr
++) {
565 if (outputsWritten
& BITFIELD64_BIT(attr
)) {
567 case FRAG_RESULT_DEPTH
:
568 case FRAG_RESULT_STENCIL
:
573 assert(attr
== FRAG_RESULT_COLOR
||
574 (FRAG_RESULT_DATA0
<= attr
&& attr
< FRAG_RESULT_MAX
));
575 fs_output_semantic_name
[fs_num_outputs
] = TGSI_SEMANTIC_COLOR
;
576 fs_output_semantic_index
[fs_num_outputs
] = numColors
;
577 outputMapping
[attr
] = fs_num_outputs
;
587 ureg
= ureg_create( TGSI_PROCESSOR_FRAGMENT
);
591 if (ST_DEBUG
& DEBUG_MESA
) {
592 _mesa_print_program(&stfp
->Base
.Base
);
593 _mesa_print_program_parameters(st
->ctx
, &stfp
->Base
.Base
);
597 error
= st_translate_mesa_program(st
->ctx
,
598 TGSI_PROCESSOR_FRAGMENT
,
605 input_semantic_index
,
610 fs_output_semantic_name
,
611 fs_output_semantic_index
, FALSE
);
613 stfp
->tgsi
.tokens
= ureg_get_tokens( ureg
, NULL
);
614 ureg_destroy( ureg
);
617 /* fill in varient */
618 varient
->driver_shader
= pipe
->create_fs_state(pipe
, &stfp
->tgsi
);
621 if (ST_DEBUG
& DEBUG_TGSI
) {
622 tgsi_dump( stfp
->tgsi
.tokens
, 0/*TGSI_DUMP_VERBOSE*/ );
631 * Translate fragment program if needed.
633 struct st_fp_varient
*
634 st_get_fp_varient(struct st_context
*st
,
635 struct st_fragment_program
*stfp
,
636 const struct st_fp_varient_key
*key
)
638 struct st_fp_varient
*fpv
;
640 /* Search for existing varient */
641 for (fpv
= stfp
->varients
; fpv
; fpv
= fpv
->next
) {
642 if (memcmp(&fpv
->key
, key
, sizeof(*key
)) == 0) {
649 fpv
= st_translate_fragment_program(st
, stfp
, key
);
651 /* insert into list */
652 fpv
->next
= stfp
->varients
;
653 stfp
->varients
= fpv
;
662 * Translate a geometry program to create a new varient.
664 static struct st_gp_varient
*
665 st_translate_geometry_program(struct st_context
*st
,
666 struct st_geometry_program
*stgp
,
667 const struct st_gp_varient_key
*key
)
669 GLuint inputMapping
[GEOM_ATTRIB_MAX
];
670 GLuint outputMapping
[GEOM_RESULT_MAX
];
671 struct pipe_context
*pipe
= st
->pipe
;
672 enum pipe_error error
;
674 const GLbitfield inputsRead
= stgp
->Base
.Base
.InputsRead
;
676 GLuint num_generic
= 0;
678 uint gs_num_inputs
= 0;
679 uint gs_builtin_inputs
= 0;
680 uint gs_array_offset
= 0;
682 ubyte gs_output_semantic_name
[PIPE_MAX_SHADER_OUTPUTS
];
683 ubyte gs_output_semantic_index
[PIPE_MAX_SHADER_OUTPUTS
];
684 uint gs_num_outputs
= 0;
688 struct ureg_program
*ureg
;
690 struct st_gp_varient
*gpv
;
692 gpv
= CALLOC_STRUCT(st_gp_varient
);
696 _mesa_remove_output_reads(&stgp
->Base
.Base
, PROGRAM_OUTPUT
);
697 _mesa_remove_output_reads(&stgp
->Base
.Base
, PROGRAM_VARYING
);
699 ureg
= ureg_create( TGSI_PROCESSOR_GEOMETRY
);
705 /* which vertex output goes to the first geometry input */
708 memset(inputMapping
, 0, sizeof(inputMapping
));
709 memset(outputMapping
, 0, sizeof(outputMapping
));
712 * Convert Mesa program inputs to TGSI input register semantics.
714 for (attr
= 0; attr
< GEOM_ATTRIB_MAX
; attr
++) {
715 if (inputsRead
& (1 << attr
)) {
716 const GLuint slot
= gs_num_inputs
;
720 inputMapping
[attr
] = slot
;
722 stgp
->input_map
[slot
+ gs_array_offset
] = vslot
- gs_builtin_inputs
;
723 stgp
->input_to_index
[attr
] = vslot
;
724 stgp
->index_to_input
[vslot
] = attr
;
727 if (attr
!= GEOM_ATTRIB_PRIMITIVE_ID
) {
728 gs_array_offset
+= 2;
733 debug_printf("input map at %d = %d\n",
734 slot
+ gs_array_offset
, stgp
->input_map
[slot
+ gs_array_offset
]);
738 case GEOM_ATTRIB_PRIMITIVE_ID
:
739 stgp
->input_semantic_name
[slot
] = TGSI_SEMANTIC_PRIMID
;
740 stgp
->input_semantic_index
[slot
] = 0;
742 case GEOM_ATTRIB_POSITION
:
743 stgp
->input_semantic_name
[slot
] = TGSI_SEMANTIC_POSITION
;
744 stgp
->input_semantic_index
[slot
] = 0;
746 case GEOM_ATTRIB_COLOR0
:
747 stgp
->input_semantic_name
[slot
] = TGSI_SEMANTIC_COLOR
;
748 stgp
->input_semantic_index
[slot
] = 0;
750 case GEOM_ATTRIB_COLOR1
:
751 stgp
->input_semantic_name
[slot
] = TGSI_SEMANTIC_COLOR
;
752 stgp
->input_semantic_index
[slot
] = 1;
754 case GEOM_ATTRIB_FOG_FRAG_COORD
:
755 stgp
->input_semantic_name
[slot
] = TGSI_SEMANTIC_FOG
;
756 stgp
->input_semantic_index
[slot
] = 0;
758 case GEOM_ATTRIB_TEX_COORD
:
759 stgp
->input_semantic_name
[slot
] = TGSI_SEMANTIC_GENERIC
;
760 stgp
->input_semantic_index
[slot
] = num_generic
++;
762 case GEOM_ATTRIB_VAR0
:
765 stgp
->input_semantic_name
[slot
] = TGSI_SEMANTIC_GENERIC
;
766 stgp
->input_semantic_index
[slot
] = num_generic
++;
771 /* initialize output semantics to defaults */
772 for (i
= 0; i
< PIPE_MAX_SHADER_OUTPUTS
; i
++) {
773 gs_output_semantic_name
[i
] = TGSI_SEMANTIC_GENERIC
;
774 gs_output_semantic_index
[i
] = 0;
779 * Determine number of outputs, the (default) output register
780 * mapping and the semantic information for each output.
782 for (attr
= 0; attr
< GEOM_RESULT_MAX
; attr
++) {
783 if (stgp
->Base
.Base
.OutputsWritten
& (1 << attr
)) {
786 slot
= gs_num_outputs
;
788 outputMapping
[attr
] = slot
;
791 case GEOM_RESULT_POS
:
793 gs_output_semantic_name
[slot
] = TGSI_SEMANTIC_POSITION
;
794 gs_output_semantic_index
[slot
] = 0;
796 case GEOM_RESULT_COL0
:
797 gs_output_semantic_name
[slot
] = TGSI_SEMANTIC_COLOR
;
798 gs_output_semantic_index
[slot
] = 0;
800 case GEOM_RESULT_COL1
:
801 gs_output_semantic_name
[slot
] = TGSI_SEMANTIC_COLOR
;
802 gs_output_semantic_index
[slot
] = 1;
804 case GEOM_RESULT_SCOL0
:
805 gs_output_semantic_name
[slot
] = TGSI_SEMANTIC_BCOLOR
;
806 gs_output_semantic_index
[slot
] = 0;
808 case GEOM_RESULT_SCOL1
:
809 gs_output_semantic_name
[slot
] = TGSI_SEMANTIC_BCOLOR
;
810 gs_output_semantic_index
[slot
] = 1;
812 case GEOM_RESULT_FOGC
:
813 gs_output_semantic_name
[slot
] = TGSI_SEMANTIC_FOG
;
814 gs_output_semantic_index
[slot
] = 0;
816 case GEOM_RESULT_PSIZ
:
817 gs_output_semantic_name
[slot
] = TGSI_SEMANTIC_PSIZE
;
818 gs_output_semantic_index
[slot
] = 0;
820 case GEOM_RESULT_TEX0
:
821 case GEOM_RESULT_TEX1
:
822 case GEOM_RESULT_TEX2
:
823 case GEOM_RESULT_TEX3
:
824 case GEOM_RESULT_TEX4
:
825 case GEOM_RESULT_TEX5
:
826 case GEOM_RESULT_TEX6
:
827 case GEOM_RESULT_TEX7
:
829 case GEOM_RESULT_VAR0
:
832 assert(slot
< Elements(gs_output_semantic_name
));
833 /* use default semantic info */
834 gs_output_semantic_name
[slot
] = TGSI_SEMANTIC_GENERIC
;
835 gs_output_semantic_index
[slot
] = num_generic
++;
840 assert(gs_output_semantic_name
[0] == TGSI_SEMANTIC_POSITION
);
842 /* find max output slot referenced to compute gs_num_outputs */
843 for (attr
= 0; attr
< GEOM_RESULT_MAX
; attr
++) {
844 if (outputMapping
[attr
] != ~0 && outputMapping
[attr
] > maxSlot
)
845 maxSlot
= outputMapping
[attr
];
847 gs_num_outputs
= maxSlot
+ 1;
852 printf("outputMapping? %d\n", outputMapping
? 1 : 0);
854 printf("attr -> slot\n");
855 for (i
= 0; i
< 16; i
++) {
856 printf(" %2d %3d\n", i
, outputMapping
[i
]);
859 printf("slot sem_name sem_index\n");
860 for (i
= 0; i
< gs_num_outputs
; i
++) {
861 printf(" %2d %d %d\n",
863 gs_output_semantic_name
[i
],
864 gs_output_semantic_index
[i
]);
869 /* free old shader state, if any */
870 if (stgp
->tgsi
.tokens
) {
871 st_free_tokens(stgp
->tgsi
.tokens
);
872 stgp
->tgsi
.tokens
= NULL
;
875 ureg_property_gs_input_prim(ureg
, stgp
->Base
.InputType
);
876 ureg_property_gs_output_prim(ureg
, stgp
->Base
.OutputType
);
877 ureg_property_gs_max_vertices(ureg
, stgp
->Base
.VerticesOut
);
879 error
= st_translate_mesa_program(st
->ctx
,
880 TGSI_PROCESSOR_GEOMETRY
,
886 stgp
->input_semantic_name
,
887 stgp
->input_semantic_index
,
892 gs_output_semantic_name
,
893 gs_output_semantic_index
,
896 stgp
->num_inputs
= gs_num_inputs
;
897 stgp
->tgsi
.tokens
= ureg_get_tokens( ureg
, NULL
);
898 ureg_destroy( ureg
);
900 /* fill in new varient */
901 gpv
->driver_shader
= pipe
->create_gs_state(pipe
, &stgp
->tgsi
);
904 if ((ST_DEBUG
& DEBUG_TGSI
) && (ST_DEBUG
& DEBUG_MESA
)) {
905 _mesa_print_program(&stgp
->Base
.Base
);
909 if (ST_DEBUG
& DEBUG_TGSI
) {
910 tgsi_dump(stgp
->tgsi
.tokens
, 0);
919 * Get/create geometry program variant.
921 struct st_gp_varient
*
922 st_get_gp_varient(struct st_context
*st
,
923 struct st_geometry_program
*stgp
,
924 const struct st_gp_varient_key
*key
)
926 struct st_gp_varient
*gpv
;
928 /* Search for existing varient */
929 for (gpv
= stgp
->varients
; gpv
; gpv
= gpv
->next
) {
930 if (memcmp(&gpv
->key
, key
, sizeof(*key
)) == 0) {
937 gpv
= st_translate_geometry_program(st
, stgp
, key
);
939 /* insert into list */
940 gpv
->next
= stgp
->varients
;
941 stgp
->varients
= gpv
;
952 * Debug- print current shader text
955 st_print_shaders(struct gl_context
*ctx
)
957 struct gl_shader_program
*shProg
[3] = {
958 ctx
->Shader
.CurrentVertexProgram
,
959 ctx
->Shader
.CurrentGeometryProgram
,
960 ctx
->Shader
.CurrentFragmentProgram
,
964 for (j
= 0; j
< 3; j
++) {
967 if (shProg
[j
] == NULL
)
970 for (i
= 0; i
< shProg
[j
]->NumShaders
; i
++) {
971 struct gl_shader
*sh
;
973 switch (shProg
[j
]->Shaders
[i
]->Type
) {
974 case GL_VERTEX_SHADER
:
975 sh
= (i
!= 0) ? NULL
: shProg
[j
]->Shaders
[i
];
977 case GL_GEOMETRY_SHADER_ARB
:
978 sh
= (i
!= 1) ? NULL
: shProg
[j
]->Shaders
[i
];
980 case GL_FRAGMENT_SHADER
:
981 sh
= (i
!= 2) ? NULL
: shProg
[j
]->Shaders
[i
];
990 printf("GLSL shader %u of %u:\n", i
, shProg
[j
]->NumShaders
);
991 printf("%s\n", sh
->Source
);
999 * Vert/Geom/Frag programs have per-context variants. Free all the
1000 * variants attached to the given program which match the given context.
1003 destroy_program_variants(struct st_context
*st
, struct gl_program
*program
)
1008 switch (program
->Target
) {
1009 case GL_VERTEX_PROGRAM_ARB
:
1011 struct st_vertex_program
*stvp
= (struct st_vertex_program
*) program
;
1012 struct st_vp_varient
*vpv
, **prev
= &stvp
->varients
;
1014 for (vpv
= stvp
->varients
; vpv
; ) {
1015 struct st_vp_varient
*next
= vpv
->next
;
1016 if (vpv
->key
.st
== st
) {
1017 /* unlink from list */
1019 /* destroy this variant */
1020 delete_vp_varient(st
, vpv
);
1029 case GL_FRAGMENT_PROGRAM_ARB
:
1031 struct st_fragment_program
*stfp
=
1032 (struct st_fragment_program
*) program
;
1033 struct st_fp_varient
*fpv
, **prev
= &stfp
->varients
;
1035 for (fpv
= stfp
->varients
; fpv
; ) {
1036 struct st_fp_varient
*next
= fpv
->next
;
1037 if (fpv
->key
.st
== st
) {
1038 /* unlink from list */
1040 /* destroy this variant */
1041 delete_fp_varient(st
, fpv
);
1051 _mesa_problem(NULL
, "Unexpected program target in "
1052 "destroy_program_variants_cb()");
1058 * Callback for _mesa_HashWalk. Free all the shader's program variants
1059 * which match the given context.
1062 destroy_shader_program_variants_cb(GLuint key
, void *data
, void *userData
)
1064 struct st_context
*st
= (struct st_context
*) userData
;
1065 struct gl_shader
*shader
= (struct gl_shader
*) data
;
1067 switch (shader
->Type
) {
1068 case GL_SHADER_PROGRAM_MESA
:
1070 struct gl_shader_program
*shProg
= (struct gl_shader_program
*) data
;
1073 for (i
= 0; i
< shProg
->NumShaders
; i
++) {
1074 destroy_program_variants(st
, shProg
->Shaders
[i
]->Program
);
1077 destroy_program_variants(st
, (struct gl_program
*)
1078 shProg
->VertexProgram
);
1079 destroy_program_variants(st
, (struct gl_program
*)
1080 shProg
->FragmentProgram
);
1081 destroy_program_variants(st
, (struct gl_program
*)
1082 shProg
->GeometryProgram
);
1085 case GL_VERTEX_SHADER
:
1086 case GL_FRAGMENT_SHADER
:
1087 case GL_GEOMETRY_SHADER
:
1089 destroy_program_variants(st
, shader
->Program
);
1099 * Callback for _mesa_HashWalk. Free all the program variants which match
1100 * the given context.
1103 destroy_program_variants_cb(GLuint key
, void *data
, void *userData
)
1105 struct st_context
*st
= (struct st_context
*) userData
;
1106 struct gl_program
*program
= (struct gl_program
*) data
;
1107 destroy_program_variants(st
, program
);
1112 * Walk over all shaders and programs to delete any variants which
1113 * belong to the given context.
1114 * This is called during context tear-down.
1117 st_destroy_program_variants(struct st_context
*st
)
1119 /* ARB vert/frag program */
1120 _mesa_HashWalk(st
->ctx
->Shared
->Programs
,
1121 destroy_program_variants_cb
, st
);
1123 /* GLSL vert/frag/geom shaders */
1124 _mesa_HashWalk(st
->ctx
->Shared
->ShaderObjects
,
1125 destroy_shader_program_variants_cb
, st
);