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
);
112 _mesa_free_parameter_list(fpv
->parameters
);
119 * Free all variants of a fragment program.
122 st_release_fp_variants(struct st_context
*st
, struct st_fragment_program
*stfp
)
124 struct st_fp_variant
*fpv
;
126 for (fpv
= stfp
->variants
; fpv
; ) {
127 struct st_fp_variant
*next
= fpv
->next
;
128 delete_fp_variant(st
, fpv
);
132 stfp
->variants
= NULL
;
137 * Delete a geometry program variant. Note the caller must unlink
138 * the variant from the linked list.
141 delete_gp_variant(struct st_context
*st
, struct st_gp_variant
*gpv
)
143 if (gpv
->driver_shader
)
144 cso_delete_geometry_shader(st
->cso_context
, gpv
->driver_shader
);
151 * Free all variants of a geometry program.
154 st_release_gp_variants(struct st_context
*st
, struct st_geometry_program
*stgp
)
156 struct st_gp_variant
*gpv
;
158 for (gpv
= stgp
->variants
; gpv
; ) {
159 struct st_gp_variant
*next
= gpv
->next
;
160 delete_gp_variant(st
, gpv
);
164 stgp
->variants
= NULL
;
171 * Translate a Mesa vertex shader into a TGSI shader.
172 * \param outputMapping to map vertex program output registers (VERT_RESULT_x)
173 * to TGSI output slots
174 * \param tokensOut destination for TGSI tokens
175 * \return pointer to cached pipe_shader object.
178 st_prepare_vertex_program(struct st_context
*st
,
179 struct st_vertex_program
*stvp
)
183 stvp
->num_inputs
= 0;
184 stvp
->num_outputs
= 0;
186 if (stvp
->Base
.IsPositionInvariant
)
187 _mesa_insert_mvp_code(st
->ctx
, &stvp
->Base
);
189 assert(stvp
->Base
.Base
.NumInstructions
> 1);
192 * Determine number of inputs, the mappings between VERT_ATTRIB_x
193 * and TGSI generic input indexes, plus input attrib semantic info.
195 for (attr
= 0; attr
< VERT_ATTRIB_MAX
; attr
++) {
196 if (stvp
->Base
.Base
.InputsRead
& (1 << attr
)) {
197 stvp
->input_to_index
[attr
] = stvp
->num_inputs
;
198 stvp
->index_to_input
[stvp
->num_inputs
] = attr
;
202 /* bit of a hack, presetup potentially unused edgeflag input */
203 stvp
->input_to_index
[VERT_ATTRIB_EDGEFLAG
] = stvp
->num_inputs
;
204 stvp
->index_to_input
[stvp
->num_inputs
] = VERT_ATTRIB_EDGEFLAG
;
206 /* Compute mapping of vertex program outputs to slots.
208 for (attr
= 0; attr
< VERT_RESULT_MAX
; attr
++) {
209 if ((stvp
->Base
.Base
.OutputsWritten
& BITFIELD64_BIT(attr
)) == 0) {
210 stvp
->result_to_output
[attr
] = ~0;
213 unsigned slot
= stvp
->num_outputs
++;
215 stvp
->result_to_output
[attr
] = slot
;
218 case VERT_RESULT_HPOS
:
219 stvp
->output_semantic_name
[slot
] = TGSI_SEMANTIC_POSITION
;
220 stvp
->output_semantic_index
[slot
] = 0;
222 case VERT_RESULT_COL0
:
223 stvp
->output_semantic_name
[slot
] = TGSI_SEMANTIC_COLOR
;
224 stvp
->output_semantic_index
[slot
] = 0;
226 case VERT_RESULT_COL1
:
227 stvp
->output_semantic_name
[slot
] = TGSI_SEMANTIC_COLOR
;
228 stvp
->output_semantic_index
[slot
] = 1;
230 case VERT_RESULT_BFC0
:
231 stvp
->output_semantic_name
[slot
] = TGSI_SEMANTIC_BCOLOR
;
232 stvp
->output_semantic_index
[slot
] = 0;
234 case VERT_RESULT_BFC1
:
235 stvp
->output_semantic_name
[slot
] = TGSI_SEMANTIC_BCOLOR
;
236 stvp
->output_semantic_index
[slot
] = 1;
238 case VERT_RESULT_FOGC
:
239 stvp
->output_semantic_name
[slot
] = TGSI_SEMANTIC_FOG
;
240 stvp
->output_semantic_index
[slot
] = 0;
242 case VERT_RESULT_PSIZ
:
243 stvp
->output_semantic_name
[slot
] = TGSI_SEMANTIC_PSIZE
;
244 stvp
->output_semantic_index
[slot
] = 0;
246 case VERT_RESULT_EDGE
:
250 case VERT_RESULT_TEX0
:
251 case VERT_RESULT_TEX1
:
252 case VERT_RESULT_TEX2
:
253 case VERT_RESULT_TEX3
:
254 case VERT_RESULT_TEX4
:
255 case VERT_RESULT_TEX5
:
256 case VERT_RESULT_TEX6
:
257 case VERT_RESULT_TEX7
:
258 stvp
->output_semantic_name
[slot
] = TGSI_SEMANTIC_GENERIC
;
259 stvp
->output_semantic_index
[slot
] = attr
- VERT_RESULT_TEX0
;
262 case VERT_RESULT_VAR0
:
264 assert(attr
< VERT_RESULT_MAX
);
265 stvp
->output_semantic_name
[slot
] = TGSI_SEMANTIC_GENERIC
;
266 stvp
->output_semantic_index
[slot
] = (FRAG_ATTRIB_VAR0
-
274 /* similar hack to above, presetup potentially unused edgeflag output */
275 stvp
->result_to_output
[VERT_RESULT_EDGE
] = stvp
->num_outputs
;
276 stvp
->output_semantic_name
[stvp
->num_outputs
] = TGSI_SEMANTIC_EDGEFLAG
;
277 stvp
->output_semantic_index
[stvp
->num_outputs
] = 0;
282 * Translate a vertex program to create a new variant.
284 static struct st_vp_variant
*
285 st_translate_vertex_program(struct st_context
*st
,
286 struct st_vertex_program
*stvp
,
287 const struct st_vp_variant_key
*key
)
289 struct st_vp_variant
*vpv
= CALLOC_STRUCT(st_vp_variant
);
290 struct pipe_context
*pipe
= st
->pipe
;
291 struct ureg_program
*ureg
;
292 enum pipe_error error
;
293 unsigned num_outputs
;
295 st_prepare_vertex_program( st
, stvp
);
297 _mesa_remove_output_reads(&stvp
->Base
.Base
, PROGRAM_OUTPUT
);
298 _mesa_remove_output_reads(&stvp
->Base
.Base
, PROGRAM_VARYING
);
300 ureg
= ureg_create( TGSI_PROCESSOR_VERTEX
);
308 vpv
->num_inputs
= stvp
->num_inputs
;
309 num_outputs
= stvp
->num_outputs
;
310 if (key
->passthrough_edgeflags
) {
315 if (ST_DEBUG
& DEBUG_MESA
) {
316 _mesa_print_program(&stvp
->Base
.Base
);
317 _mesa_print_program_parameters(st
->ctx
, &stvp
->Base
.Base
);
321 error
= st_translate_mesa_program(st
->ctx
,
322 TGSI_PROCESSOR_VERTEX
,
327 stvp
->input_to_index
,
328 NULL
, /* input semantic name */
329 NULL
, /* input semantic index */
333 stvp
->result_to_output
,
334 stvp
->output_semantic_name
,
335 stvp
->output_semantic_index
,
336 key
->passthrough_edgeflags
);
341 vpv
->tgsi
.tokens
= ureg_get_tokens( ureg
, NULL
);
342 if (!vpv
->tgsi
.tokens
)
345 ureg_destroy( ureg
);
347 vpv
->driver_shader
= pipe
->create_vs_state(pipe
, &vpv
->tgsi
);
349 if (ST_DEBUG
& DEBUG_TGSI
) {
350 tgsi_dump( vpv
->tgsi
.tokens
, 0 );
357 debug_printf("%s: failed to translate Mesa program:\n", __FUNCTION__
);
358 _mesa_print_program(&stvp
->Base
.Base
);
361 ureg_destroy( ureg
);
367 * Find/create a vertex program variant.
369 struct st_vp_variant
*
370 st_get_vp_variant(struct st_context
*st
,
371 struct st_vertex_program
*stvp
,
372 const struct st_vp_variant_key
*key
)
374 struct st_vp_variant
*vpv
;
376 /* Search for existing variant */
377 for (vpv
= stvp
->variants
; vpv
; vpv
= vpv
->next
) {
378 if (memcmp(&vpv
->key
, key
, sizeof(*key
)) == 0) {
385 vpv
= st_translate_vertex_program(st
, stvp
, key
);
387 /* insert into list */
388 vpv
->next
= stvp
->variants
;
389 stvp
->variants
= vpv
;
398 * Translate a Mesa fragment shader into a TGSI shader using extra info in
400 * \return new fragment program variant
402 static struct st_fp_variant
*
403 st_translate_fragment_program(struct st_context
*st
,
404 struct st_fragment_program
*stfp
,
405 const struct st_fp_variant_key
*key
)
407 struct pipe_context
*pipe
= st
->pipe
;
408 struct st_fp_variant
*variant
= CALLOC_STRUCT(st_fp_variant
);
409 GLboolean deleteFP
= GL_FALSE
;
414 assert(!(key
->bitmap
&& key
->drawpixels
));
418 /* glBitmap drawing */
419 struct gl_fragment_program
*fp
; /* we free this temp program below */
421 st_make_bitmap_fragment_program(st
, &stfp
->Base
,
422 &fp
, &variant
->bitmap_sampler
);
424 variant
->parameters
= _mesa_clone_parameter_list(fp
->Base
.Parameters
);
425 stfp
= st_fragment_program(fp
);
428 else if (key
->drawpixels
) {
429 /* glDrawPixels drawing */
430 struct gl_fragment_program
*fp
; /* we free this temp program below */
432 if (key
->drawpixels_z
|| key
->drawpixels_stencil
) {
433 fp
= st_make_drawpix_z_stencil_program(st
, key
->drawpixels_z
,
434 key
->drawpixels_stencil
);
438 st_make_drawpix_fragment_program(st
, &stfp
->Base
, &fp
);
439 variant
->parameters
= _mesa_clone_parameter_list(fp
->Base
.Parameters
);
442 stfp
= st_fragment_program(fp
);
446 if (!stfp
->tgsi
.tokens
) {
447 /* need to translate Mesa instructions to TGSI now */
448 GLuint outputMapping
[FRAG_RESULT_MAX
];
449 GLuint inputMapping
[FRAG_ATTRIB_MAX
];
450 GLuint interpMode
[PIPE_MAX_SHADER_INPUTS
]; /* XXX size? */
452 enum pipe_error error
;
453 const GLbitfield inputsRead
= stfp
->Base
.Base
.InputsRead
;
454 struct ureg_program
*ureg
;
455 GLboolean write_all
= GL_FALSE
;
457 ubyte input_semantic_name
[PIPE_MAX_SHADER_INPUTS
];
458 ubyte input_semantic_index
[PIPE_MAX_SHADER_INPUTS
];
459 uint fs_num_inputs
= 0;
461 ubyte fs_output_semantic_name
[PIPE_MAX_SHADER_OUTPUTS
];
462 ubyte fs_output_semantic_index
[PIPE_MAX_SHADER_OUTPUTS
];
463 uint fs_num_outputs
= 0;
466 _mesa_remove_output_reads(&stfp
->Base
.Base
, PROGRAM_OUTPUT
);
469 * Convert Mesa program inputs to TGSI input register semantics.
471 for (attr
= 0; attr
< FRAG_ATTRIB_MAX
; attr
++) {
472 if (inputsRead
& (1 << attr
)) {
473 const GLuint slot
= fs_num_inputs
++;
475 inputMapping
[attr
] = slot
;
478 case FRAG_ATTRIB_WPOS
:
479 input_semantic_name
[slot
] = TGSI_SEMANTIC_POSITION
;
480 input_semantic_index
[slot
] = 0;
481 interpMode
[slot
] = TGSI_INTERPOLATE_LINEAR
;
483 case FRAG_ATTRIB_COL0
:
484 input_semantic_name
[slot
] = TGSI_SEMANTIC_COLOR
;
485 input_semantic_index
[slot
] = 0;
486 interpMode
[slot
] = TGSI_INTERPOLATE_LINEAR
;
488 case FRAG_ATTRIB_COL1
:
489 input_semantic_name
[slot
] = TGSI_SEMANTIC_COLOR
;
490 input_semantic_index
[slot
] = 1;
491 interpMode
[slot
] = TGSI_INTERPOLATE_LINEAR
;
493 case FRAG_ATTRIB_FOGC
:
494 input_semantic_name
[slot
] = TGSI_SEMANTIC_FOG
;
495 input_semantic_index
[slot
] = 0;
496 interpMode
[slot
] = TGSI_INTERPOLATE_PERSPECTIVE
;
498 case FRAG_ATTRIB_FACE
:
499 input_semantic_name
[slot
] = TGSI_SEMANTIC_FACE
;
500 input_semantic_index
[slot
] = 0;
501 interpMode
[slot
] = TGSI_INTERPOLATE_CONSTANT
;
503 /* In most cases, there is nothing special about these
504 * inputs, so adopt a convention to use the generic
505 * semantic name and the mesa FRAG_ATTRIB_ number as the
508 * All that is required is that the vertex shader labels
509 * its own outputs similarly, and that the vertex shader
510 * generates at least every output required by the
511 * fragment shader plus fixed-function hardware (such as
514 * There is no requirement that semantic indexes start at
515 * zero or be restricted to a particular range -- nobody
516 * should be building tables based on semantic index.
518 case FRAG_ATTRIB_PNTC
:
519 case FRAG_ATTRIB_TEX0
:
520 case FRAG_ATTRIB_TEX1
:
521 case FRAG_ATTRIB_TEX2
:
522 case FRAG_ATTRIB_TEX3
:
523 case FRAG_ATTRIB_TEX4
:
524 case FRAG_ATTRIB_TEX5
:
525 case FRAG_ATTRIB_TEX6
:
526 case FRAG_ATTRIB_TEX7
:
527 case FRAG_ATTRIB_VAR0
:
529 /* Actually, let's try and zero-base this just for
530 * readability of the generated TGSI.
532 assert(attr
>= FRAG_ATTRIB_TEX0
);
533 input_semantic_index
[slot
] = (attr
- FRAG_ATTRIB_TEX0
);
534 input_semantic_name
[slot
] = TGSI_SEMANTIC_GENERIC
;
535 if (attr
== FRAG_ATTRIB_PNTC
)
536 interpMode
[slot
] = TGSI_INTERPOLATE_LINEAR
;
538 interpMode
[slot
] = TGSI_INTERPOLATE_PERSPECTIVE
;
543 inputMapping
[attr
] = -1;
548 * Semantics and mapping for outputs
552 GLbitfield64 outputsWritten
= stfp
->Base
.Base
.OutputsWritten
;
554 /* if z is written, emit that first */
555 if (outputsWritten
& BITFIELD64_BIT(FRAG_RESULT_DEPTH
)) {
556 fs_output_semantic_name
[fs_num_outputs
] = TGSI_SEMANTIC_POSITION
;
557 fs_output_semantic_index
[fs_num_outputs
] = 0;
558 outputMapping
[FRAG_RESULT_DEPTH
] = fs_num_outputs
;
560 outputsWritten
&= ~(1 << FRAG_RESULT_DEPTH
);
563 if (outputsWritten
& BITFIELD64_BIT(FRAG_RESULT_STENCIL
)) {
564 fs_output_semantic_name
[fs_num_outputs
] = TGSI_SEMANTIC_STENCIL
;
565 fs_output_semantic_index
[fs_num_outputs
] = 0;
566 outputMapping
[FRAG_RESULT_STENCIL
] = fs_num_outputs
;
568 outputsWritten
&= ~(1 << FRAG_RESULT_STENCIL
);
571 /* handle remaning outputs (color) */
572 for (attr
= 0; attr
< FRAG_RESULT_MAX
; attr
++) {
573 if (outputsWritten
& BITFIELD64_BIT(attr
)) {
575 case FRAG_RESULT_DEPTH
:
576 case FRAG_RESULT_STENCIL
:
580 case FRAG_RESULT_COLOR
:
581 write_all
= GL_TRUE
; /* fallthrough */
583 assert(attr
== FRAG_RESULT_COLOR
||
584 (FRAG_RESULT_DATA0
<= attr
&& attr
< FRAG_RESULT_MAX
));
585 fs_output_semantic_name
[fs_num_outputs
] = TGSI_SEMANTIC_COLOR
;
586 fs_output_semantic_index
[fs_num_outputs
] = numColors
;
587 outputMapping
[attr
] = fs_num_outputs
;
597 ureg
= ureg_create( TGSI_PROCESSOR_FRAGMENT
);
601 if (ST_DEBUG
& DEBUG_MESA
) {
602 _mesa_print_program(&stfp
->Base
.Base
);
603 _mesa_print_program_parameters(st
->ctx
, &stfp
->Base
.Base
);
606 if (write_all
== GL_TRUE
)
607 ureg_property_fs_color0_writes_all_cbufs(ureg
, 1);
609 error
= st_translate_mesa_program(st
->ctx
,
610 TGSI_PROCESSOR_FRAGMENT
,
617 input_semantic_index
,
622 fs_output_semantic_name
,
623 fs_output_semantic_index
, FALSE
);
624 (void)error
; /* ignored */
626 stfp
->tgsi
.tokens
= ureg_get_tokens( ureg
, NULL
);
627 ureg_destroy( ureg
);
630 /* fill in variant */
631 variant
->driver_shader
= pipe
->create_fs_state(pipe
, &stfp
->tgsi
);
634 if (ST_DEBUG
& DEBUG_TGSI
) {
635 tgsi_dump( stfp
->tgsi
.tokens
, 0/*TGSI_DUMP_VERBOSE*/ );
640 /* Free the temporary program made above */
641 struct gl_fragment_program
*fp
= &stfp
->Base
;
642 _mesa_reference_fragprog(st
->ctx
, &fp
, NULL
);
650 * Translate fragment program if needed.
652 struct st_fp_variant
*
653 st_get_fp_variant(struct st_context
*st
,
654 struct st_fragment_program
*stfp
,
655 const struct st_fp_variant_key
*key
)
657 struct st_fp_variant
*fpv
;
659 /* Search for existing variant */
660 for (fpv
= stfp
->variants
; fpv
; fpv
= fpv
->next
) {
661 if (memcmp(&fpv
->key
, key
, sizeof(*key
)) == 0) {
668 fpv
= st_translate_fragment_program(st
, stfp
, key
);
670 /* insert into list */
671 fpv
->next
= stfp
->variants
;
672 stfp
->variants
= fpv
;
681 * Translate a geometry program to create a new variant.
683 static struct st_gp_variant
*
684 st_translate_geometry_program(struct st_context
*st
,
685 struct st_geometry_program
*stgp
,
686 const struct st_gp_variant_key
*key
)
688 GLuint inputMapping
[GEOM_ATTRIB_MAX
];
689 GLuint outputMapping
[GEOM_RESULT_MAX
];
690 struct pipe_context
*pipe
= st
->pipe
;
691 enum pipe_error error
;
693 const GLbitfield inputsRead
= stgp
->Base
.Base
.InputsRead
;
695 GLuint num_generic
= 0;
697 uint gs_num_inputs
= 0;
698 uint gs_builtin_inputs
= 0;
699 uint gs_array_offset
= 0;
701 ubyte gs_output_semantic_name
[PIPE_MAX_SHADER_OUTPUTS
];
702 ubyte gs_output_semantic_index
[PIPE_MAX_SHADER_OUTPUTS
];
703 uint gs_num_outputs
= 0;
707 struct ureg_program
*ureg
;
709 struct st_gp_variant
*gpv
;
711 gpv
= CALLOC_STRUCT(st_gp_variant
);
715 _mesa_remove_output_reads(&stgp
->Base
.Base
, PROGRAM_OUTPUT
);
716 _mesa_remove_output_reads(&stgp
->Base
.Base
, PROGRAM_VARYING
);
718 ureg
= ureg_create( TGSI_PROCESSOR_GEOMETRY
);
724 /* which vertex output goes to the first geometry input */
727 memset(inputMapping
, 0, sizeof(inputMapping
));
728 memset(outputMapping
, 0, sizeof(outputMapping
));
731 * Convert Mesa program inputs to TGSI input register semantics.
733 for (attr
= 0; attr
< GEOM_ATTRIB_MAX
; attr
++) {
734 if (inputsRead
& (1 << attr
)) {
735 const GLuint slot
= gs_num_inputs
;
739 inputMapping
[attr
] = slot
;
741 stgp
->input_map
[slot
+ gs_array_offset
] = vslot
- gs_builtin_inputs
;
742 stgp
->input_to_index
[attr
] = vslot
;
743 stgp
->index_to_input
[vslot
] = attr
;
746 if (attr
!= GEOM_ATTRIB_PRIMITIVE_ID
) {
747 gs_array_offset
+= 2;
752 debug_printf("input map at %d = %d\n",
753 slot
+ gs_array_offset
, stgp
->input_map
[slot
+ gs_array_offset
]);
757 case GEOM_ATTRIB_PRIMITIVE_ID
:
758 stgp
->input_semantic_name
[slot
] = TGSI_SEMANTIC_PRIMID
;
759 stgp
->input_semantic_index
[slot
] = 0;
761 case GEOM_ATTRIB_POSITION
:
762 stgp
->input_semantic_name
[slot
] = TGSI_SEMANTIC_POSITION
;
763 stgp
->input_semantic_index
[slot
] = 0;
765 case GEOM_ATTRIB_COLOR0
:
766 stgp
->input_semantic_name
[slot
] = TGSI_SEMANTIC_COLOR
;
767 stgp
->input_semantic_index
[slot
] = 0;
769 case GEOM_ATTRIB_COLOR1
:
770 stgp
->input_semantic_name
[slot
] = TGSI_SEMANTIC_COLOR
;
771 stgp
->input_semantic_index
[slot
] = 1;
773 case GEOM_ATTRIB_FOG_FRAG_COORD
:
774 stgp
->input_semantic_name
[slot
] = TGSI_SEMANTIC_FOG
;
775 stgp
->input_semantic_index
[slot
] = 0;
777 case GEOM_ATTRIB_TEX_COORD
:
778 stgp
->input_semantic_name
[slot
] = TGSI_SEMANTIC_GENERIC
;
779 stgp
->input_semantic_index
[slot
] = num_generic
++;
781 case GEOM_ATTRIB_VAR0
:
784 stgp
->input_semantic_name
[slot
] = TGSI_SEMANTIC_GENERIC
;
785 stgp
->input_semantic_index
[slot
] = num_generic
++;
790 /* initialize output semantics to defaults */
791 for (i
= 0; i
< PIPE_MAX_SHADER_OUTPUTS
; i
++) {
792 gs_output_semantic_name
[i
] = TGSI_SEMANTIC_GENERIC
;
793 gs_output_semantic_index
[i
] = 0;
798 * Determine number of outputs, the (default) output register
799 * mapping and the semantic information for each output.
801 for (attr
= 0; attr
< GEOM_RESULT_MAX
; attr
++) {
802 if (stgp
->Base
.Base
.OutputsWritten
& BITFIELD64_BIT(attr
)) {
805 slot
= gs_num_outputs
;
807 outputMapping
[attr
] = slot
;
810 case GEOM_RESULT_POS
:
812 gs_output_semantic_name
[slot
] = TGSI_SEMANTIC_POSITION
;
813 gs_output_semantic_index
[slot
] = 0;
815 case GEOM_RESULT_COL0
:
816 gs_output_semantic_name
[slot
] = TGSI_SEMANTIC_COLOR
;
817 gs_output_semantic_index
[slot
] = 0;
819 case GEOM_RESULT_COL1
:
820 gs_output_semantic_name
[slot
] = TGSI_SEMANTIC_COLOR
;
821 gs_output_semantic_index
[slot
] = 1;
823 case GEOM_RESULT_SCOL0
:
824 gs_output_semantic_name
[slot
] = TGSI_SEMANTIC_BCOLOR
;
825 gs_output_semantic_index
[slot
] = 0;
827 case GEOM_RESULT_SCOL1
:
828 gs_output_semantic_name
[slot
] = TGSI_SEMANTIC_BCOLOR
;
829 gs_output_semantic_index
[slot
] = 1;
831 case GEOM_RESULT_FOGC
:
832 gs_output_semantic_name
[slot
] = TGSI_SEMANTIC_FOG
;
833 gs_output_semantic_index
[slot
] = 0;
835 case GEOM_RESULT_PSIZ
:
836 gs_output_semantic_name
[slot
] = TGSI_SEMANTIC_PSIZE
;
837 gs_output_semantic_index
[slot
] = 0;
839 case GEOM_RESULT_TEX0
:
840 case GEOM_RESULT_TEX1
:
841 case GEOM_RESULT_TEX2
:
842 case GEOM_RESULT_TEX3
:
843 case GEOM_RESULT_TEX4
:
844 case GEOM_RESULT_TEX5
:
845 case GEOM_RESULT_TEX6
:
846 case GEOM_RESULT_TEX7
:
848 case GEOM_RESULT_VAR0
:
851 assert(slot
< Elements(gs_output_semantic_name
));
852 /* use default semantic info */
853 gs_output_semantic_name
[slot
] = TGSI_SEMANTIC_GENERIC
;
854 gs_output_semantic_index
[slot
] = num_generic
++;
859 assert(gs_output_semantic_name
[0] == TGSI_SEMANTIC_POSITION
);
861 /* find max output slot referenced to compute gs_num_outputs */
862 for (attr
= 0; attr
< GEOM_RESULT_MAX
; attr
++) {
863 if (outputMapping
[attr
] != ~0 && outputMapping
[attr
] > maxSlot
)
864 maxSlot
= outputMapping
[attr
];
866 gs_num_outputs
= maxSlot
+ 1;
871 printf("outputMapping? %d\n", outputMapping
? 1 : 0);
873 printf("attr -> slot\n");
874 for (i
= 0; i
< 16; i
++) {
875 printf(" %2d %3d\n", i
, outputMapping
[i
]);
878 printf("slot sem_name sem_index\n");
879 for (i
= 0; i
< gs_num_outputs
; i
++) {
880 printf(" %2d %d %d\n",
882 gs_output_semantic_name
[i
],
883 gs_output_semantic_index
[i
]);
888 /* free old shader state, if any */
889 if (stgp
->tgsi
.tokens
) {
890 st_free_tokens(stgp
->tgsi
.tokens
);
891 stgp
->tgsi
.tokens
= NULL
;
894 ureg_property_gs_input_prim(ureg
, stgp
->Base
.InputType
);
895 ureg_property_gs_output_prim(ureg
, stgp
->Base
.OutputType
);
896 ureg_property_gs_max_vertices(ureg
, stgp
->Base
.VerticesOut
);
898 error
= st_translate_mesa_program(st
->ctx
,
899 TGSI_PROCESSOR_GEOMETRY
,
905 stgp
->input_semantic_name
,
906 stgp
->input_semantic_index
,
911 gs_output_semantic_name
,
912 gs_output_semantic_index
,
914 (void)error
; /* ignored */
916 stgp
->num_inputs
= gs_num_inputs
;
917 stgp
->tgsi
.tokens
= ureg_get_tokens( ureg
, NULL
);
918 ureg_destroy( ureg
);
920 /* fill in new variant */
921 gpv
->driver_shader
= pipe
->create_gs_state(pipe
, &stgp
->tgsi
);
924 if ((ST_DEBUG
& DEBUG_TGSI
) && (ST_DEBUG
& DEBUG_MESA
)) {
925 _mesa_print_program(&stgp
->Base
.Base
);
929 if (ST_DEBUG
& DEBUG_TGSI
) {
930 tgsi_dump(stgp
->tgsi
.tokens
, 0);
939 * Get/create geometry program variant.
941 struct st_gp_variant
*
942 st_get_gp_variant(struct st_context
*st
,
943 struct st_geometry_program
*stgp
,
944 const struct st_gp_variant_key
*key
)
946 struct st_gp_variant
*gpv
;
948 /* Search for existing variant */
949 for (gpv
= stgp
->variants
; gpv
; gpv
= gpv
->next
) {
950 if (memcmp(&gpv
->key
, key
, sizeof(*key
)) == 0) {
957 gpv
= st_translate_geometry_program(st
, stgp
, key
);
959 /* insert into list */
960 gpv
->next
= stgp
->variants
;
961 stgp
->variants
= gpv
;
972 * Debug- print current shader text
975 st_print_shaders(struct gl_context
*ctx
)
977 struct gl_shader_program
*shProg
[3] = {
978 ctx
->Shader
.CurrentVertexProgram
,
979 ctx
->Shader
.CurrentGeometryProgram
,
980 ctx
->Shader
.CurrentFragmentProgram
,
984 for (j
= 0; j
< 3; j
++) {
987 if (shProg
[j
] == NULL
)
990 for (i
= 0; i
< shProg
[j
]->NumShaders
; i
++) {
991 struct gl_shader
*sh
;
993 switch (shProg
[j
]->Shaders
[i
]->Type
) {
994 case GL_VERTEX_SHADER
:
995 sh
= (i
!= 0) ? NULL
: shProg
[j
]->Shaders
[i
];
997 case GL_GEOMETRY_SHADER_ARB
:
998 sh
= (i
!= 1) ? NULL
: shProg
[j
]->Shaders
[i
];
1000 case GL_FRAGMENT_SHADER
:
1001 sh
= (i
!= 2) ? NULL
: shProg
[j
]->Shaders
[i
];
1010 printf("GLSL shader %u of %u:\n", i
, shProg
[j
]->NumShaders
);
1011 printf("%s\n", sh
->Source
);
1019 * Vert/Geom/Frag programs have per-context variants. Free all the
1020 * variants attached to the given program which match the given context.
1023 destroy_program_variants(struct st_context
*st
, struct gl_program
*program
)
1028 switch (program
->Target
) {
1029 case GL_VERTEX_PROGRAM_ARB
:
1031 struct st_vertex_program
*stvp
= (struct st_vertex_program
*) program
;
1032 struct st_vp_variant
*vpv
, **prevPtr
= &stvp
->variants
;
1034 for (vpv
= stvp
->variants
; vpv
; ) {
1035 struct st_vp_variant
*next
= vpv
->next
;
1036 if (vpv
->key
.st
== st
) {
1037 /* unlink from list */
1039 /* destroy this variant */
1040 delete_vp_variant(st
, vpv
);
1043 prevPtr
= &vpv
->next
;
1049 case GL_FRAGMENT_PROGRAM_ARB
:
1051 struct st_fragment_program
*stfp
=
1052 (struct st_fragment_program
*) program
;
1053 struct st_fp_variant
*fpv
, **prevPtr
= &stfp
->variants
;
1055 for (fpv
= stfp
->variants
; fpv
; ) {
1056 struct st_fp_variant
*next
= fpv
->next
;
1057 if (fpv
->key
.st
== st
) {
1058 /* unlink from list */
1060 /* destroy this variant */
1061 delete_fp_variant(st
, fpv
);
1064 prevPtr
= &fpv
->next
;
1070 case MESA_GEOMETRY_PROGRAM
:
1072 struct st_geometry_program
*stgp
=
1073 (struct st_geometry_program
*) program
;
1074 struct st_gp_variant
*gpv
, **prevPtr
= &stgp
->variants
;
1076 for (gpv
= stgp
->variants
; gpv
; ) {
1077 struct st_gp_variant
*next
= gpv
->next
;
1078 if (gpv
->key
.st
== st
) {
1079 /* unlink from list */
1081 /* destroy this variant */
1082 delete_gp_variant(st
, gpv
);
1085 prevPtr
= &gpv
->next
;
1092 _mesa_problem(NULL
, "Unexpected program target 0x%x in "
1093 "destroy_program_variants_cb()", program
->Target
);
1099 * Callback for _mesa_HashWalk. Free all the shader's program variants
1100 * which match the given context.
1103 destroy_shader_program_variants_cb(GLuint key
, void *data
, void *userData
)
1105 struct st_context
*st
= (struct st_context
*) userData
;
1106 struct gl_shader
*shader
= (struct gl_shader
*) data
;
1108 switch (shader
->Type
) {
1109 case GL_SHADER_PROGRAM_MESA
:
1111 struct gl_shader_program
*shProg
= (struct gl_shader_program
*) data
;
1114 for (i
= 0; i
< shProg
->NumShaders
; i
++) {
1115 destroy_program_variants(st
, shProg
->Shaders
[i
]->Program
);
1118 destroy_program_variants(st
, (struct gl_program
*)
1119 shProg
->VertexProgram
);
1120 destroy_program_variants(st
, (struct gl_program
*)
1121 shProg
->FragmentProgram
);
1122 destroy_program_variants(st
, (struct gl_program
*)
1123 shProg
->GeometryProgram
);
1126 case GL_VERTEX_SHADER
:
1127 case GL_FRAGMENT_SHADER
:
1128 case GL_GEOMETRY_SHADER
:
1130 destroy_program_variants(st
, shader
->Program
);
1140 * Callback for _mesa_HashWalk. Free all the program variants which match
1141 * the given context.
1144 destroy_program_variants_cb(GLuint key
, void *data
, void *userData
)
1146 struct st_context
*st
= (struct st_context
*) userData
;
1147 struct gl_program
*program
= (struct gl_program
*) data
;
1148 destroy_program_variants(st
, program
);
1153 * Walk over all shaders and programs to delete any variants which
1154 * belong to the given context.
1155 * This is called during context tear-down.
1158 st_destroy_program_variants(struct st_context
*st
)
1160 /* ARB vert/frag program */
1161 _mesa_HashWalk(st
->ctx
->Shared
->Programs
,
1162 destroy_program_variants_cb
, st
);
1164 /* GLSL vert/frag/geom shaders */
1165 _mesa_HashWalk(st
->ctx
->Shared
->ShaderObjects
,
1166 destroy_shader_program_variants_cb
, st
);