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
);
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 varient.
366 struct st_vp_varient
*
367 st_get_vp_varient(struct st_context
*st
,
368 struct st_vertex_program
*stvp
,
369 const struct st_vp_varient_key
*key
)
371 struct st_vp_varient
*vpv
;
373 /* Search for existing varient */
374 for (vpv
= stvp
->varients
; 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
->varients
;
386 stvp
->varients
= 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_varient
*
400 st_translate_fragment_program(struct st_context
*st
,
401 struct st_fragment_program
*stfp
,
402 const struct st_fp_varient_key
*key
)
404 struct pipe_context
*pipe
= st
->pipe
;
405 struct st_fp_varient
*varient
= CALLOC_STRUCT(st_fp_varient
);
410 assert(!(key
->bitmap
&& key
->drawpixels
));
413 /* glBitmap drawing */
414 struct gl_fragment_program
*fp
;
416 st_make_bitmap_fragment_program(st
, &stfp
->Base
,
417 &fp
, &varient
->bitmap_sampler
);
419 varient
->parameters
= _mesa_clone_parameter_list(fp
->Base
.Parameters
);
420 stfp
= st_fragment_program(fp
);
422 else if (key
->drawpixels
) {
423 /* glDrawPixels drawing */
424 struct gl_fragment_program
*fp
;
426 if (key
->drawpixels_z
|| key
->drawpixels_stencil
) {
427 fp
= st_make_drawpix_z_stencil_program(st
, key
->drawpixels_z
,
428 key
->drawpixels_stencil
);
432 st_make_drawpix_fragment_program(st
, &stfp
->Base
, &fp
);
433 varient
->parameters
= _mesa_clone_parameter_list(fp
->Base
.Parameters
);
435 stfp
= st_fragment_program(fp
);
438 if (!stfp
->tgsi
.tokens
) {
439 /* need to translate Mesa instructions to TGSI now */
440 GLuint outputMapping
[FRAG_RESULT_MAX
];
441 GLuint inputMapping
[FRAG_ATTRIB_MAX
];
442 GLuint interpMode
[PIPE_MAX_SHADER_INPUTS
]; /* XXX size? */
444 enum pipe_error error
;
445 const GLbitfield inputsRead
= stfp
->Base
.Base
.InputsRead
;
446 struct ureg_program
*ureg
;
448 ubyte input_semantic_name
[PIPE_MAX_SHADER_INPUTS
];
449 ubyte input_semantic_index
[PIPE_MAX_SHADER_INPUTS
];
450 uint fs_num_inputs
= 0;
452 ubyte fs_output_semantic_name
[PIPE_MAX_SHADER_OUTPUTS
];
453 ubyte fs_output_semantic_index
[PIPE_MAX_SHADER_OUTPUTS
];
454 uint fs_num_outputs
= 0;
457 _mesa_remove_output_reads(&stfp
->Base
.Base
, PROGRAM_OUTPUT
);
460 * Convert Mesa program inputs to TGSI input register semantics.
462 for (attr
= 0; attr
< FRAG_ATTRIB_MAX
; attr
++) {
463 if (inputsRead
& (1 << attr
)) {
464 const GLuint slot
= fs_num_inputs
++;
466 inputMapping
[attr
] = slot
;
469 case FRAG_ATTRIB_WPOS
:
470 input_semantic_name
[slot
] = TGSI_SEMANTIC_POSITION
;
471 input_semantic_index
[slot
] = 0;
472 interpMode
[slot
] = TGSI_INTERPOLATE_LINEAR
;
474 case FRAG_ATTRIB_COL0
:
475 input_semantic_name
[slot
] = TGSI_SEMANTIC_COLOR
;
476 input_semantic_index
[slot
] = 0;
477 interpMode
[slot
] = TGSI_INTERPOLATE_LINEAR
;
479 case FRAG_ATTRIB_COL1
:
480 input_semantic_name
[slot
] = TGSI_SEMANTIC_COLOR
;
481 input_semantic_index
[slot
] = 1;
482 interpMode
[slot
] = TGSI_INTERPOLATE_LINEAR
;
484 case FRAG_ATTRIB_FOGC
:
485 input_semantic_name
[slot
] = TGSI_SEMANTIC_FOG
;
486 input_semantic_index
[slot
] = 0;
487 interpMode
[slot
] = TGSI_INTERPOLATE_PERSPECTIVE
;
489 case FRAG_ATTRIB_FACE
:
490 input_semantic_name
[slot
] = TGSI_SEMANTIC_FACE
;
491 input_semantic_index
[slot
] = 0;
492 interpMode
[slot
] = TGSI_INTERPOLATE_CONSTANT
;
494 /* In most cases, there is nothing special about these
495 * inputs, so adopt a convention to use the generic
496 * semantic name and the mesa FRAG_ATTRIB_ number as the
499 * All that is required is that the vertex shader labels
500 * its own outputs similarly, and that the vertex shader
501 * generates at least every output required by the
502 * fragment shader plus fixed-function hardware (such as
505 * There is no requirement that semantic indexes start at
506 * zero or be restricted to a particular range -- nobody
507 * should be building tables based on semantic index.
509 case FRAG_ATTRIB_PNTC
:
510 case FRAG_ATTRIB_TEX0
:
511 case FRAG_ATTRIB_TEX1
:
512 case FRAG_ATTRIB_TEX2
:
513 case FRAG_ATTRIB_TEX3
:
514 case FRAG_ATTRIB_TEX4
:
515 case FRAG_ATTRIB_TEX5
:
516 case FRAG_ATTRIB_TEX6
:
517 case FRAG_ATTRIB_TEX7
:
518 case FRAG_ATTRIB_VAR0
:
520 /* Actually, let's try and zero-base this just for
521 * readability of the generated TGSI.
523 assert(attr
>= FRAG_ATTRIB_TEX0
);
524 input_semantic_index
[slot
] = (attr
- FRAG_ATTRIB_TEX0
);
525 input_semantic_name
[slot
] = TGSI_SEMANTIC_GENERIC
;
526 if (attr
== FRAG_ATTRIB_PNTC
)
527 interpMode
[slot
] = TGSI_INTERPOLATE_LINEAR
;
529 interpMode
[slot
] = TGSI_INTERPOLATE_PERSPECTIVE
;
534 inputMapping
[attr
] = -1;
539 * Semantics and mapping for outputs
543 GLbitfield64 outputsWritten
= stfp
->Base
.Base
.OutputsWritten
;
545 /* if z is written, emit that first */
546 if (outputsWritten
& BITFIELD64_BIT(FRAG_RESULT_DEPTH
)) {
547 fs_output_semantic_name
[fs_num_outputs
] = TGSI_SEMANTIC_POSITION
;
548 fs_output_semantic_index
[fs_num_outputs
] = 0;
549 outputMapping
[FRAG_RESULT_DEPTH
] = fs_num_outputs
;
551 outputsWritten
&= ~(1 << FRAG_RESULT_DEPTH
);
554 if (outputsWritten
& BITFIELD64_BIT(FRAG_RESULT_STENCIL
)) {
555 fs_output_semantic_name
[fs_num_outputs
] = TGSI_SEMANTIC_STENCIL
;
556 fs_output_semantic_index
[fs_num_outputs
] = 0;
557 outputMapping
[FRAG_RESULT_STENCIL
] = fs_num_outputs
;
559 outputsWritten
&= ~(1 << FRAG_RESULT_STENCIL
);
562 /* handle remaning outputs (color) */
563 for (attr
= 0; attr
< FRAG_RESULT_MAX
; attr
++) {
564 if (outputsWritten
& BITFIELD64_BIT(attr
)) {
566 case FRAG_RESULT_DEPTH
:
567 case FRAG_RESULT_STENCIL
:
572 assert(attr
== FRAG_RESULT_COLOR
||
573 (FRAG_RESULT_DATA0
<= attr
&& attr
< FRAG_RESULT_MAX
));
574 fs_output_semantic_name
[fs_num_outputs
] = TGSI_SEMANTIC_COLOR
;
575 fs_output_semantic_index
[fs_num_outputs
] = numColors
;
576 outputMapping
[attr
] = fs_num_outputs
;
586 ureg
= ureg_create( TGSI_PROCESSOR_FRAGMENT
);
590 if (ST_DEBUG
& DEBUG_MESA
) {
591 _mesa_print_program(&stfp
->Base
.Base
);
592 _mesa_print_program_parameters(st
->ctx
, &stfp
->Base
.Base
);
596 error
= st_translate_mesa_program(st
->ctx
,
597 TGSI_PROCESSOR_FRAGMENT
,
604 input_semantic_index
,
609 fs_output_semantic_name
,
610 fs_output_semantic_index
, FALSE
);
612 stfp
->tgsi
.tokens
= ureg_get_tokens( ureg
, NULL
);
613 ureg_destroy( ureg
);
616 /* fill in varient */
617 varient
->driver_shader
= pipe
->create_fs_state(pipe
, &stfp
->tgsi
);
620 if (ST_DEBUG
& DEBUG_TGSI
) {
621 tgsi_dump( stfp
->tgsi
.tokens
, 0/*TGSI_DUMP_VERBOSE*/ );
630 * Translate fragment program if needed.
632 struct st_fp_varient
*
633 st_get_fp_varient(struct st_context
*st
,
634 struct st_fragment_program
*stfp
,
635 const struct st_fp_varient_key
*key
)
637 struct st_fp_varient
*fpv
;
639 /* Search for existing varient */
640 for (fpv
= stfp
->varients
; fpv
; fpv
= fpv
->next
) {
641 if (memcmp(&fpv
->key
, key
, sizeof(*key
)) == 0) {
648 fpv
= st_translate_fragment_program(st
, stfp
, key
);
650 /* insert into list */
651 fpv
->next
= stfp
->varients
;
652 stfp
->varients
= fpv
;
661 * Translate a geometry program to create a new varient.
663 static struct st_gp_varient
*
664 st_translate_geometry_program(struct st_context
*st
,
665 struct st_geometry_program
*stgp
,
666 const struct st_gp_varient_key
*key
)
668 GLuint inputMapping
[GEOM_ATTRIB_MAX
];
669 GLuint outputMapping
[GEOM_RESULT_MAX
];
670 struct pipe_context
*pipe
= st
->pipe
;
671 enum pipe_error error
;
673 const GLbitfield inputsRead
= stgp
->Base
.Base
.InputsRead
;
675 GLuint num_generic
= 0;
677 uint gs_num_inputs
= 0;
678 uint gs_builtin_inputs
= 0;
679 uint gs_array_offset
= 0;
681 ubyte gs_output_semantic_name
[PIPE_MAX_SHADER_OUTPUTS
];
682 ubyte gs_output_semantic_index
[PIPE_MAX_SHADER_OUTPUTS
];
683 uint gs_num_outputs
= 0;
687 struct ureg_program
*ureg
;
689 struct st_gp_varient
*gpv
;
691 gpv
= CALLOC_STRUCT(st_gp_varient
);
695 _mesa_remove_output_reads(&stgp
->Base
.Base
, PROGRAM_OUTPUT
);
696 _mesa_remove_output_reads(&stgp
->Base
.Base
, PROGRAM_VARYING
);
698 ureg
= ureg_create( TGSI_PROCESSOR_GEOMETRY
);
704 /* which vertex output goes to the first geometry input */
707 memset(inputMapping
, 0, sizeof(inputMapping
));
708 memset(outputMapping
, 0, sizeof(outputMapping
));
711 * Convert Mesa program inputs to TGSI input register semantics.
713 for (attr
= 0; attr
< GEOM_ATTRIB_MAX
; attr
++) {
714 if (inputsRead
& (1 << attr
)) {
715 const GLuint slot
= gs_num_inputs
;
719 inputMapping
[attr
] = slot
;
721 stgp
->input_map
[slot
+ gs_array_offset
] = vslot
- gs_builtin_inputs
;
722 stgp
->input_to_index
[attr
] = vslot
;
723 stgp
->index_to_input
[vslot
] = attr
;
726 if (attr
!= GEOM_ATTRIB_PRIMITIVE_ID
) {
727 gs_array_offset
+= 2;
732 debug_printf("input map at %d = %d\n",
733 slot
+ gs_array_offset
, stgp
->input_map
[slot
+ gs_array_offset
]);
737 case GEOM_ATTRIB_PRIMITIVE_ID
:
738 stgp
->input_semantic_name
[slot
] = TGSI_SEMANTIC_PRIMID
;
739 stgp
->input_semantic_index
[slot
] = 0;
741 case GEOM_ATTRIB_POSITION
:
742 stgp
->input_semantic_name
[slot
] = TGSI_SEMANTIC_POSITION
;
743 stgp
->input_semantic_index
[slot
] = 0;
745 case GEOM_ATTRIB_COLOR0
:
746 stgp
->input_semantic_name
[slot
] = TGSI_SEMANTIC_COLOR
;
747 stgp
->input_semantic_index
[slot
] = 0;
749 case GEOM_ATTRIB_COLOR1
:
750 stgp
->input_semantic_name
[slot
] = TGSI_SEMANTIC_COLOR
;
751 stgp
->input_semantic_index
[slot
] = 1;
753 case GEOM_ATTRIB_FOG_FRAG_COORD
:
754 stgp
->input_semantic_name
[slot
] = TGSI_SEMANTIC_FOG
;
755 stgp
->input_semantic_index
[slot
] = 0;
757 case GEOM_ATTRIB_TEX_COORD
:
758 stgp
->input_semantic_name
[slot
] = TGSI_SEMANTIC_GENERIC
;
759 stgp
->input_semantic_index
[slot
] = num_generic
++;
761 case GEOM_ATTRIB_VAR0
:
764 stgp
->input_semantic_name
[slot
] = TGSI_SEMANTIC_GENERIC
;
765 stgp
->input_semantic_index
[slot
] = num_generic
++;
770 /* initialize output semantics to defaults */
771 for (i
= 0; i
< PIPE_MAX_SHADER_OUTPUTS
; i
++) {
772 gs_output_semantic_name
[i
] = TGSI_SEMANTIC_GENERIC
;
773 gs_output_semantic_index
[i
] = 0;
778 * Determine number of outputs, the (default) output register
779 * mapping and the semantic information for each output.
781 for (attr
= 0; attr
< GEOM_RESULT_MAX
; attr
++) {
782 if (stgp
->Base
.Base
.OutputsWritten
& (1 << attr
)) {
785 slot
= gs_num_outputs
;
787 outputMapping
[attr
] = slot
;
790 case GEOM_RESULT_POS
:
792 gs_output_semantic_name
[slot
] = TGSI_SEMANTIC_POSITION
;
793 gs_output_semantic_index
[slot
] = 0;
795 case GEOM_RESULT_COL0
:
796 gs_output_semantic_name
[slot
] = TGSI_SEMANTIC_COLOR
;
797 gs_output_semantic_index
[slot
] = 0;
799 case GEOM_RESULT_COL1
:
800 gs_output_semantic_name
[slot
] = TGSI_SEMANTIC_COLOR
;
801 gs_output_semantic_index
[slot
] = 1;
803 case GEOM_RESULT_SCOL0
:
804 gs_output_semantic_name
[slot
] = TGSI_SEMANTIC_BCOLOR
;
805 gs_output_semantic_index
[slot
] = 0;
807 case GEOM_RESULT_SCOL1
:
808 gs_output_semantic_name
[slot
] = TGSI_SEMANTIC_BCOLOR
;
809 gs_output_semantic_index
[slot
] = 1;
811 case GEOM_RESULT_FOGC
:
812 gs_output_semantic_name
[slot
] = TGSI_SEMANTIC_FOG
;
813 gs_output_semantic_index
[slot
] = 0;
815 case GEOM_RESULT_PSIZ
:
816 gs_output_semantic_name
[slot
] = TGSI_SEMANTIC_PSIZE
;
817 gs_output_semantic_index
[slot
] = 0;
819 case GEOM_RESULT_TEX0
:
820 case GEOM_RESULT_TEX1
:
821 case GEOM_RESULT_TEX2
:
822 case GEOM_RESULT_TEX3
:
823 case GEOM_RESULT_TEX4
:
824 case GEOM_RESULT_TEX5
:
825 case GEOM_RESULT_TEX6
:
826 case GEOM_RESULT_TEX7
:
828 case GEOM_RESULT_VAR0
:
831 assert(slot
< Elements(gs_output_semantic_name
));
832 /* use default semantic info */
833 gs_output_semantic_name
[slot
] = TGSI_SEMANTIC_GENERIC
;
834 gs_output_semantic_index
[slot
] = num_generic
++;
839 assert(gs_output_semantic_name
[0] == TGSI_SEMANTIC_POSITION
);
841 /* find max output slot referenced to compute gs_num_outputs */
842 for (attr
= 0; attr
< GEOM_RESULT_MAX
; attr
++) {
843 if (outputMapping
[attr
] != ~0 && outputMapping
[attr
] > maxSlot
)
844 maxSlot
= outputMapping
[attr
];
846 gs_num_outputs
= maxSlot
+ 1;
851 printf("outputMapping? %d\n", outputMapping
? 1 : 0);
853 printf("attr -> slot\n");
854 for (i
= 0; i
< 16; i
++) {
855 printf(" %2d %3d\n", i
, outputMapping
[i
]);
858 printf("slot sem_name sem_index\n");
859 for (i
= 0; i
< gs_num_outputs
; i
++) {
860 printf(" %2d %d %d\n",
862 gs_output_semantic_name
[i
],
863 gs_output_semantic_index
[i
]);
868 /* free old shader state, if any */
869 if (stgp
->tgsi
.tokens
) {
870 st_free_tokens(stgp
->tgsi
.tokens
);
871 stgp
->tgsi
.tokens
= NULL
;
874 ureg_property_gs_input_prim(ureg
, stgp
->Base
.InputType
);
875 ureg_property_gs_output_prim(ureg
, stgp
->Base
.OutputType
);
876 ureg_property_gs_max_vertices(ureg
, stgp
->Base
.VerticesOut
);
878 error
= st_translate_mesa_program(st
->ctx
,
879 TGSI_PROCESSOR_GEOMETRY
,
885 stgp
->input_semantic_name
,
886 stgp
->input_semantic_index
,
891 gs_output_semantic_name
,
892 gs_output_semantic_index
,
895 stgp
->num_inputs
= gs_num_inputs
;
896 stgp
->tgsi
.tokens
= ureg_get_tokens( ureg
, NULL
);
897 ureg_destroy( ureg
);
899 /* fill in new varient */
900 gpv
->driver_shader
= pipe
->create_gs_state(pipe
, &stgp
->tgsi
);
903 if ((ST_DEBUG
& DEBUG_TGSI
) && (ST_DEBUG
& DEBUG_MESA
)) {
904 _mesa_print_program(&stgp
->Base
.Base
);
908 if (ST_DEBUG
& DEBUG_TGSI
) {
909 tgsi_dump(stgp
->tgsi
.tokens
, 0);
918 * Get/create geometry program variant.
920 struct st_gp_varient
*
921 st_get_gp_varient(struct st_context
*st
,
922 struct st_geometry_program
*stgp
,
923 const struct st_gp_varient_key
*key
)
925 struct st_gp_varient
*gpv
;
927 /* Search for existing varient */
928 for (gpv
= stgp
->varients
; gpv
; gpv
= gpv
->next
) {
929 if (memcmp(&gpv
->key
, key
, sizeof(*key
)) == 0) {
936 gpv
= st_translate_geometry_program(st
, stgp
, key
);
938 /* insert into list */
939 gpv
->next
= stgp
->varients
;
940 stgp
->varients
= gpv
;
951 * Debug- print current shader text
954 st_print_shaders(struct gl_context
*ctx
)
956 struct gl_shader_program
*shProg
[3] = {
957 ctx
->Shader
.CurrentVertexProgram
,
958 ctx
->Shader
.CurrentGeometryProgram
,
959 ctx
->Shader
.CurrentFragmentProgram
,
963 for (j
= 0; j
< 3; j
++) {
966 if (shProg
[j
] == NULL
)
969 for (i
= 0; i
< shProg
[j
]->NumShaders
; i
++) {
970 struct gl_shader
*sh
;
972 switch (shProg
[j
]->Shaders
[i
]->Type
) {
973 case GL_VERTEX_SHADER
:
974 sh
= (i
!= 0) ? NULL
: shProg
[j
]->Shaders
[i
];
976 case GL_GEOMETRY_SHADER_ARB
:
977 sh
= (i
!= 1) ? NULL
: shProg
[j
]->Shaders
[i
];
979 case GL_FRAGMENT_SHADER
:
980 sh
= (i
!= 2) ? NULL
: shProg
[j
]->Shaders
[i
];
989 printf("GLSL shader %u of %u:\n", i
, shProg
[j
]->NumShaders
);
990 printf("%s\n", sh
->Source
);
998 * Vert/Geom/Frag programs have per-context variants. Free all the
999 * variants attached to the given program which match the given context.
1002 destroy_program_variants(struct st_context
*st
, struct gl_program
*program
)
1007 switch (program
->Target
) {
1008 case GL_VERTEX_PROGRAM_ARB
:
1010 struct st_vertex_program
*stvp
= (struct st_vertex_program
*) program
;
1011 struct st_vp_varient
*vpv
, **prev
= &stvp
->varients
;
1013 for (vpv
= stvp
->varients
; vpv
; ) {
1014 struct st_vp_varient
*next
= vpv
->next
;
1015 if (vpv
->key
.st
== st
) {
1016 /* unlink from list */
1018 /* destroy this variant */
1019 delete_vp_varient(st
, vpv
);
1028 case GL_FRAGMENT_PROGRAM_ARB
:
1030 struct st_fragment_program
*stfp
=
1031 (struct st_fragment_program
*) program
;
1032 struct st_fp_varient
*fpv
, **prev
= &stfp
->varients
;
1034 for (fpv
= stfp
->varients
; fpv
; ) {
1035 struct st_fp_varient
*next
= fpv
->next
;
1036 if (fpv
->key
.st
== st
) {
1037 /* unlink from list */
1039 /* destroy this variant */
1040 delete_fp_varient(st
, fpv
);
1050 _mesa_problem(NULL
, "Unexpected program target in "
1051 "destroy_program_variants_cb()");
1057 * Callback for _mesa_HashWalk. Free all the shader's program variants
1058 * which match the given context.
1061 destroy_shader_program_variants_cb(GLuint key
, void *data
, void *userData
)
1063 struct st_context
*st
= (struct st_context
*) userData
;
1064 struct gl_shader
*shader
= (struct gl_shader
*) data
;
1066 switch (shader
->Type
) {
1067 case GL_SHADER_PROGRAM_MESA
:
1069 struct gl_shader_program
*shProg
= (struct gl_shader_program
*) data
;
1072 for (i
= 0; i
< shProg
->NumShaders
; i
++) {
1073 destroy_program_variants(st
, shProg
->Shaders
[i
]->Program
);
1076 destroy_program_variants(st
, (struct gl_program
*)
1077 shProg
->VertexProgram
);
1078 destroy_program_variants(st
, (struct gl_program
*)
1079 shProg
->FragmentProgram
);
1080 destroy_program_variants(st
, (struct gl_program
*)
1081 shProg
->GeometryProgram
);
1084 case GL_VERTEX_SHADER
:
1085 case GL_FRAGMENT_SHADER
:
1086 case GL_GEOMETRY_SHADER
:
1088 destroy_program_variants(st
, shader
->Program
);
1098 * Callback for _mesa_HashWalk. Free all the program variants which match
1099 * the given context.
1102 destroy_program_variants_cb(GLuint key
, void *data
, void *userData
)
1104 struct st_context
*st
= (struct st_context
*) userData
;
1105 struct gl_program
*program
= (struct gl_program
*) data
;
1106 destroy_program_variants(st
, program
);
1111 * Walk over all shaders and programs to delete any variants which
1112 * belong to the given context.
1113 * This is called during context tear-down.
1116 st_destroy_program_variants(struct st_context
*st
)
1118 /* ARB vert/frag program */
1119 _mesa_HashWalk(st
->ctx
->Shared
->Programs
,
1120 destroy_program_variants_cb
, st
);
1122 /* GLSL vert/frag/geom shaders */
1123 _mesa_HashWalk(st
->ctx
->Shared
->ShaderObjects
,
1124 destroy_shader_program_variants_cb
, st
);