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 gl_context
*ctx
,
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(ctx
, &stvp
->Base
);
189 if (!stvp
->glsl_to_tgsi
)
190 assert(stvp
->Base
.Base
.NumInstructions
> 1);
193 * Determine number of inputs, the mappings between VERT_ATTRIB_x
194 * and TGSI generic input indexes, plus input attrib semantic info.
196 for (attr
= 0; attr
< VERT_ATTRIB_MAX
; attr
++) {
197 if (stvp
->Base
.Base
.InputsRead
& (1 << attr
)) {
198 stvp
->input_to_index
[attr
] = stvp
->num_inputs
;
199 stvp
->index_to_input
[stvp
->num_inputs
] = attr
;
203 /* bit of a hack, presetup potentially unused edgeflag input */
204 stvp
->input_to_index
[VERT_ATTRIB_EDGEFLAG
] = stvp
->num_inputs
;
205 stvp
->index_to_input
[stvp
->num_inputs
] = VERT_ATTRIB_EDGEFLAG
;
207 /* Compute mapping of vertex program outputs to slots.
209 for (attr
= 0; attr
< VERT_RESULT_MAX
; attr
++) {
210 if ((stvp
->Base
.Base
.OutputsWritten
& BITFIELD64_BIT(attr
)) == 0) {
211 stvp
->result_to_output
[attr
] = ~0;
214 unsigned slot
= stvp
->num_outputs
++;
216 stvp
->result_to_output
[attr
] = slot
;
219 case VERT_RESULT_HPOS
:
220 stvp
->output_semantic_name
[slot
] = TGSI_SEMANTIC_POSITION
;
221 stvp
->output_semantic_index
[slot
] = 0;
223 case VERT_RESULT_COL0
:
224 stvp
->output_semantic_name
[slot
] = TGSI_SEMANTIC_COLOR
;
225 stvp
->output_semantic_index
[slot
] = 0;
227 case VERT_RESULT_COL1
:
228 stvp
->output_semantic_name
[slot
] = TGSI_SEMANTIC_COLOR
;
229 stvp
->output_semantic_index
[slot
] = 1;
231 case VERT_RESULT_BFC0
:
232 stvp
->output_semantic_name
[slot
] = TGSI_SEMANTIC_BCOLOR
;
233 stvp
->output_semantic_index
[slot
] = 0;
235 case VERT_RESULT_BFC1
:
236 stvp
->output_semantic_name
[slot
] = TGSI_SEMANTIC_BCOLOR
;
237 stvp
->output_semantic_index
[slot
] = 1;
239 case VERT_RESULT_FOGC
:
240 stvp
->output_semantic_name
[slot
] = TGSI_SEMANTIC_FOG
;
241 stvp
->output_semantic_index
[slot
] = 0;
243 case VERT_RESULT_PSIZ
:
244 stvp
->output_semantic_name
[slot
] = TGSI_SEMANTIC_PSIZE
;
245 stvp
->output_semantic_index
[slot
] = 0;
247 case VERT_RESULT_EDGE
:
251 case VERT_RESULT_TEX0
:
252 case VERT_RESULT_TEX1
:
253 case VERT_RESULT_TEX2
:
254 case VERT_RESULT_TEX3
:
255 case VERT_RESULT_TEX4
:
256 case VERT_RESULT_TEX5
:
257 case VERT_RESULT_TEX6
:
258 case VERT_RESULT_TEX7
:
259 stvp
->output_semantic_name
[slot
] = TGSI_SEMANTIC_GENERIC
;
260 stvp
->output_semantic_index
[slot
] = attr
- VERT_RESULT_TEX0
;
263 case VERT_RESULT_VAR0
:
265 assert(attr
< VERT_RESULT_MAX
);
266 stvp
->output_semantic_name
[slot
] = TGSI_SEMANTIC_GENERIC
;
267 stvp
->output_semantic_index
[slot
] = (FRAG_ATTRIB_VAR0
-
275 /* similar hack to above, presetup potentially unused edgeflag output */
276 stvp
->result_to_output
[VERT_RESULT_EDGE
] = stvp
->num_outputs
;
277 stvp
->output_semantic_name
[stvp
->num_outputs
] = TGSI_SEMANTIC_EDGEFLAG
;
278 stvp
->output_semantic_index
[stvp
->num_outputs
] = 0;
283 * Translate a vertex program to create a new variant.
285 static struct st_vp_variant
*
286 st_translate_vertex_program(struct st_context
*st
,
287 struct st_vertex_program
*stvp
,
288 const struct st_vp_variant_key
*key
)
290 struct st_vp_variant
*vpv
= CALLOC_STRUCT(st_vp_variant
);
291 struct pipe_context
*pipe
= st
->pipe
;
292 struct ureg_program
*ureg
;
293 enum pipe_error error
;
294 unsigned num_outputs
;
296 st_prepare_vertex_program(st
->ctx
, stvp
);
298 if (!stvp
->glsl_to_tgsi
)
300 _mesa_remove_output_reads(&stvp
->Base
.Base
, PROGRAM_OUTPUT
);
301 _mesa_remove_output_reads(&stvp
->Base
.Base
, PROGRAM_VARYING
);
304 ureg
= ureg_create( TGSI_PROCESSOR_VERTEX
);
312 vpv
->num_inputs
= stvp
->num_inputs
;
313 num_outputs
= stvp
->num_outputs
;
314 if (key
->passthrough_edgeflags
) {
319 if (ST_DEBUG
& DEBUG_MESA
) {
320 _mesa_print_program(&stvp
->Base
.Base
);
321 _mesa_print_program_parameters(st
->ctx
, &stvp
->Base
.Base
);
325 if (stvp
->glsl_to_tgsi
)
326 error
= st_translate_program(st
->ctx
,
327 TGSI_PROCESSOR_VERTEX
,
333 stvp
->input_to_index
,
334 NULL
, /* input semantic name */
335 NULL
, /* input semantic index */
336 NULL
, /* interp mode */
339 stvp
->result_to_output
,
340 stvp
->output_semantic_name
,
341 stvp
->output_semantic_index
,
342 key
->passthrough_edgeflags
);
344 error
= st_translate_mesa_program(st
->ctx
,
345 TGSI_PROCESSOR_VERTEX
,
350 stvp
->input_to_index
,
351 NULL
, /* input semantic name */
352 NULL
, /* input semantic index */
356 stvp
->result_to_output
,
357 stvp
->output_semantic_name
,
358 stvp
->output_semantic_index
,
359 key
->passthrough_edgeflags
);
364 vpv
->tgsi
.tokens
= ureg_get_tokens( ureg
, NULL
);
365 if (!vpv
->tgsi
.tokens
)
368 ureg_destroy( ureg
);
370 vpv
->driver_shader
= pipe
->create_vs_state(pipe
, &vpv
->tgsi
);
372 if (ST_DEBUG
& DEBUG_TGSI
) {
373 tgsi_dump( vpv
->tgsi
.tokens
, 0 );
380 debug_printf("%s: failed to translate Mesa program:\n", __FUNCTION__
);
381 _mesa_print_program(&stvp
->Base
.Base
);
384 ureg_destroy( ureg
);
390 * Find/create a vertex program variant.
392 struct st_vp_variant
*
393 st_get_vp_variant(struct st_context
*st
,
394 struct st_vertex_program
*stvp
,
395 const struct st_vp_variant_key
*key
)
397 struct st_vp_variant
*vpv
;
399 /* Search for existing variant */
400 for (vpv
= stvp
->variants
; vpv
; vpv
= vpv
->next
) {
401 if (memcmp(&vpv
->key
, key
, sizeof(*key
)) == 0) {
408 vpv
= st_translate_vertex_program(st
, stvp
, key
);
410 /* insert into list */
411 vpv
->next
= stvp
->variants
;
412 stvp
->variants
= vpv
;
420 * Translate Mesa fragment shader attributes to TGSI attributes.
421 * \return GL_TRUE if color output should be written to all render targets,
425 st_prepare_fragment_program(struct gl_context
*ctx
,
426 struct st_fragment_program
*stfp
)
429 const GLbitfield inputsRead
= stfp
->Base
.Base
.InputsRead
;
430 GLboolean write_all
= GL_FALSE
;
433 * Convert Mesa program inputs to TGSI input register semantics.
435 for (attr
= 0; attr
< FRAG_ATTRIB_MAX
; attr
++) {
436 if (inputsRead
& (1 << attr
)) {
437 const GLuint slot
= stfp
->num_inputs
++;
439 stfp
->input_to_index
[attr
] = slot
;
442 case FRAG_ATTRIB_WPOS
:
443 stfp
->input_semantic_name
[slot
] = TGSI_SEMANTIC_POSITION
;
444 stfp
->input_semantic_index
[slot
] = 0;
445 stfp
->interp_mode
[slot
] = TGSI_INTERPOLATE_LINEAR
;
447 case FRAG_ATTRIB_COL0
:
448 stfp
->input_semantic_name
[slot
] = TGSI_SEMANTIC_COLOR
;
449 stfp
->input_semantic_index
[slot
] = 0;
450 stfp
->interp_mode
[slot
] = TGSI_INTERPOLATE_LINEAR
;
452 case FRAG_ATTRIB_COL1
:
453 stfp
->input_semantic_name
[slot
] = TGSI_SEMANTIC_COLOR
;
454 stfp
->input_semantic_index
[slot
] = 1;
455 stfp
->interp_mode
[slot
] = TGSI_INTERPOLATE_LINEAR
;
457 case FRAG_ATTRIB_FOGC
:
458 stfp
->input_semantic_name
[slot
] = TGSI_SEMANTIC_FOG
;
459 stfp
->input_semantic_index
[slot
] = 0;
460 stfp
->interp_mode
[slot
] = TGSI_INTERPOLATE_PERSPECTIVE
;
462 case FRAG_ATTRIB_FACE
:
463 stfp
->input_semantic_name
[slot
] = TGSI_SEMANTIC_FACE
;
464 stfp
->input_semantic_index
[slot
] = 0;
465 stfp
->interp_mode
[slot
] = TGSI_INTERPOLATE_CONSTANT
;
467 /* In most cases, there is nothing special about these
468 * inputs, so adopt a convention to use the generic
469 * semantic name and the mesa FRAG_ATTRIB_ number as the
472 * All that is required is that the vertex shader labels
473 * its own outputs similarly, and that the vertex shader
474 * generates at least every output required by the
475 * fragment shader plus fixed-function hardware (such as
478 * There is no requirement that semantic indexes start at
479 * zero or be restricted to a particular range -- nobody
480 * should be building tables based on semantic index.
482 case FRAG_ATTRIB_PNTC
:
483 case FRAG_ATTRIB_TEX0
:
484 case FRAG_ATTRIB_TEX1
:
485 case FRAG_ATTRIB_TEX2
:
486 case FRAG_ATTRIB_TEX3
:
487 case FRAG_ATTRIB_TEX4
:
488 case FRAG_ATTRIB_TEX5
:
489 case FRAG_ATTRIB_TEX6
:
490 case FRAG_ATTRIB_TEX7
:
491 case FRAG_ATTRIB_VAR0
:
493 /* Actually, let's try and zero-base this just for
494 * readability of the generated TGSI.
496 assert(attr
>= FRAG_ATTRIB_TEX0
);
497 stfp
->input_semantic_index
[slot
] = (attr
- FRAG_ATTRIB_TEX0
);
498 stfp
->input_semantic_name
[slot
] = TGSI_SEMANTIC_GENERIC
;
499 if (attr
== FRAG_ATTRIB_PNTC
)
500 stfp
->interp_mode
[slot
] = TGSI_INTERPOLATE_LINEAR
;
502 stfp
->interp_mode
[slot
] = TGSI_INTERPOLATE_PERSPECTIVE
;
507 stfp
->input_to_index
[attr
] = -1;
512 * Semantics and mapping for outputs
516 GLbitfield64 outputsWritten
= stfp
->Base
.Base
.OutputsWritten
;
518 /* if z is written, emit that first */
519 if (outputsWritten
& BITFIELD64_BIT(FRAG_RESULT_DEPTH
)) {
520 stfp
->output_semantic_name
[stfp
->num_outputs
] = TGSI_SEMANTIC_POSITION
;
521 stfp
->output_semantic_index
[stfp
->num_outputs
] = 0;
522 stfp
->result_to_output
[FRAG_RESULT_DEPTH
] = stfp
->num_outputs
;
524 outputsWritten
&= ~(1 << FRAG_RESULT_DEPTH
);
527 if (outputsWritten
& BITFIELD64_BIT(FRAG_RESULT_STENCIL
)) {
528 stfp
->output_semantic_name
[stfp
->num_outputs
] = TGSI_SEMANTIC_STENCIL
;
529 stfp
->output_semantic_index
[stfp
->num_outputs
] = 0;
530 stfp
->result_to_output
[FRAG_RESULT_STENCIL
] = stfp
->num_outputs
;
532 outputsWritten
&= ~(1 << FRAG_RESULT_STENCIL
);
535 /* handle remaning outputs (color) */
536 for (attr
= 0; attr
< FRAG_RESULT_MAX
; attr
++) {
537 if (outputsWritten
& BITFIELD64_BIT(attr
)) {
539 case FRAG_RESULT_DEPTH
:
540 case FRAG_RESULT_STENCIL
:
544 case FRAG_RESULT_COLOR
:
545 write_all
= GL_TRUE
; /* fallthrough */
547 assert(attr
== FRAG_RESULT_COLOR
||
548 (FRAG_RESULT_DATA0
<= attr
&& attr
< FRAG_RESULT_MAX
));
549 stfp
->output_semantic_name
[stfp
->num_outputs
] = TGSI_SEMANTIC_COLOR
;
550 stfp
->output_semantic_index
[stfp
->num_outputs
] = numColors
;
551 stfp
->result_to_output
[attr
] = stfp
->num_outputs
;
566 * Translate a Mesa fragment shader into a TGSI shader using extra info in
568 * \return new fragment program variant
570 static struct st_fp_variant
*
571 st_translate_fragment_program(struct st_context
*st
,
572 struct st_fragment_program
*stfp
,
573 const struct st_fp_variant_key
*key
)
575 struct pipe_context
*pipe
= st
->pipe
;
576 struct st_fp_variant
*variant
= CALLOC_STRUCT(st_fp_variant
);
577 GLboolean deleteFP
= GL_FALSE
;
582 assert(!(key
->bitmap
&& key
->drawpixels
));
586 /* glBitmap drawing */
587 struct gl_fragment_program
*fp
; /* we free this temp program below */
589 st_make_bitmap_fragment_program(st
, &stfp
->Base
,
590 &fp
, &variant
->bitmap_sampler
);
592 variant
->parameters
= _mesa_clone_parameter_list(fp
->Base
.Parameters
);
593 stfp
= st_fragment_program(fp
);
596 else if (key
->drawpixels
) {
597 /* glDrawPixels drawing */
598 struct gl_fragment_program
*fp
; /* we free this temp program below */
600 if (key
->drawpixels_z
|| key
->drawpixels_stencil
) {
601 fp
= st_make_drawpix_z_stencil_program(st
, key
->drawpixels_z
,
602 key
->drawpixels_stencil
);
606 st_make_drawpix_fragment_program(st
, &stfp
->Base
, &fp
);
607 variant
->parameters
= _mesa_clone_parameter_list(fp
->Base
.Parameters
);
610 stfp
= st_fragment_program(fp
);
614 if (!stfp
->tgsi
.tokens
) {
615 /* need to translate Mesa instructions to TGSI now */
616 enum pipe_error error
;
617 struct ureg_program
*ureg
;
618 GLboolean write_all
= st_prepare_fragment_program(st
->ctx
, stfp
);
620 if (!stfp
->glsl_to_tgsi
)
621 _mesa_remove_output_reads(&stfp
->Base
.Base
, PROGRAM_OUTPUT
);
623 ureg
= ureg_create( TGSI_PROCESSOR_FRAGMENT
);
627 if (ST_DEBUG
& DEBUG_MESA
) {
628 _mesa_print_program(&stfp
->Base
.Base
);
629 _mesa_print_program_parameters(st
->ctx
, &stfp
->Base
.Base
);
632 if (write_all
== GL_TRUE
)
633 ureg_property_fs_color0_writes_all_cbufs(ureg
, 1);
635 if (stfp
->glsl_to_tgsi
)
636 error
= st_translate_program(st
->ctx
,
637 TGSI_PROCESSOR_FRAGMENT
,
643 stfp
->input_to_index
,
644 stfp
->input_semantic_name
,
645 stfp
->input_semantic_index
,
649 stfp
->result_to_output
,
650 stfp
->output_semantic_name
,
651 stfp
->output_semantic_index
, FALSE
);
653 error
= st_translate_mesa_program(st
->ctx
,
654 TGSI_PROCESSOR_FRAGMENT
,
659 stfp
->input_to_index
,
660 stfp
->input_semantic_name
,
661 stfp
->input_semantic_index
,
665 stfp
->result_to_output
,
666 stfp
->output_semantic_name
,
667 stfp
->output_semantic_index
, FALSE
);
669 stfp
->tgsi
.tokens
= ureg_get_tokens( ureg
, NULL
);
670 ureg_destroy( ureg
);
673 /* fill in variant */
674 variant
->driver_shader
= pipe
->create_fs_state(pipe
, &stfp
->tgsi
);
677 if (ST_DEBUG
& DEBUG_TGSI
) {
678 tgsi_dump( stfp
->tgsi
.tokens
, 0/*TGSI_DUMP_VERBOSE*/ );
683 /* Free the temporary program made above */
684 struct gl_fragment_program
*fp
= &stfp
->Base
;
685 _mesa_reference_fragprog(st
->ctx
, &fp
, NULL
);
693 * Translate fragment program if needed.
695 struct st_fp_variant
*
696 st_get_fp_variant(struct st_context
*st
,
697 struct st_fragment_program
*stfp
,
698 const struct st_fp_variant_key
*key
)
700 struct st_fp_variant
*fpv
;
702 /* Search for existing variant */
703 for (fpv
= stfp
->variants
; fpv
; fpv
= fpv
->next
) {
704 if (memcmp(&fpv
->key
, key
, sizeof(*key
)) == 0) {
711 fpv
= st_translate_fragment_program(st
, stfp
, key
);
713 /* insert into list */
714 fpv
->next
= stfp
->variants
;
715 stfp
->variants
= fpv
;
724 * Translate a geometry program to create a new variant.
726 static struct st_gp_variant
*
727 st_translate_geometry_program(struct st_context
*st
,
728 struct st_geometry_program
*stgp
,
729 const struct st_gp_variant_key
*key
)
731 GLuint inputMapping
[GEOM_ATTRIB_MAX
];
732 GLuint outputMapping
[GEOM_RESULT_MAX
];
733 struct pipe_context
*pipe
= st
->pipe
;
734 enum pipe_error error
;
736 const GLbitfield inputsRead
= stgp
->Base
.Base
.InputsRead
;
738 GLuint num_generic
= 0;
740 uint gs_num_inputs
= 0;
741 uint gs_builtin_inputs
= 0;
742 uint gs_array_offset
= 0;
744 ubyte gs_output_semantic_name
[PIPE_MAX_SHADER_OUTPUTS
];
745 ubyte gs_output_semantic_index
[PIPE_MAX_SHADER_OUTPUTS
];
746 uint gs_num_outputs
= 0;
750 struct ureg_program
*ureg
;
752 struct st_gp_variant
*gpv
;
754 gpv
= CALLOC_STRUCT(st_gp_variant
);
758 _mesa_remove_output_reads(&stgp
->Base
.Base
, PROGRAM_OUTPUT
);
759 _mesa_remove_output_reads(&stgp
->Base
.Base
, PROGRAM_VARYING
);
761 ureg
= ureg_create( TGSI_PROCESSOR_GEOMETRY
);
767 /* which vertex output goes to the first geometry input */
770 memset(inputMapping
, 0, sizeof(inputMapping
));
771 memset(outputMapping
, 0, sizeof(outputMapping
));
774 * Convert Mesa program inputs to TGSI input register semantics.
776 for (attr
= 0; attr
< GEOM_ATTRIB_MAX
; attr
++) {
777 if (inputsRead
& (1 << attr
)) {
778 const GLuint slot
= gs_num_inputs
;
782 inputMapping
[attr
] = slot
;
784 stgp
->input_map
[slot
+ gs_array_offset
] = vslot
- gs_builtin_inputs
;
785 stgp
->input_to_index
[attr
] = vslot
;
786 stgp
->index_to_input
[vslot
] = attr
;
789 if (attr
!= GEOM_ATTRIB_PRIMITIVE_ID
) {
790 gs_array_offset
+= 2;
795 debug_printf("input map at %d = %d\n",
796 slot
+ gs_array_offset
, stgp
->input_map
[slot
+ gs_array_offset
]);
800 case GEOM_ATTRIB_PRIMITIVE_ID
:
801 stgp
->input_semantic_name
[slot
] = TGSI_SEMANTIC_PRIMID
;
802 stgp
->input_semantic_index
[slot
] = 0;
804 case GEOM_ATTRIB_POSITION
:
805 stgp
->input_semantic_name
[slot
] = TGSI_SEMANTIC_POSITION
;
806 stgp
->input_semantic_index
[slot
] = 0;
808 case GEOM_ATTRIB_COLOR0
:
809 stgp
->input_semantic_name
[slot
] = TGSI_SEMANTIC_COLOR
;
810 stgp
->input_semantic_index
[slot
] = 0;
812 case GEOM_ATTRIB_COLOR1
:
813 stgp
->input_semantic_name
[slot
] = TGSI_SEMANTIC_COLOR
;
814 stgp
->input_semantic_index
[slot
] = 1;
816 case GEOM_ATTRIB_FOG_FRAG_COORD
:
817 stgp
->input_semantic_name
[slot
] = TGSI_SEMANTIC_FOG
;
818 stgp
->input_semantic_index
[slot
] = 0;
820 case GEOM_ATTRIB_TEX_COORD
:
821 stgp
->input_semantic_name
[slot
] = TGSI_SEMANTIC_GENERIC
;
822 stgp
->input_semantic_index
[slot
] = num_generic
++;
824 case GEOM_ATTRIB_VAR0
:
827 stgp
->input_semantic_name
[slot
] = TGSI_SEMANTIC_GENERIC
;
828 stgp
->input_semantic_index
[slot
] = num_generic
++;
833 /* initialize output semantics to defaults */
834 for (i
= 0; i
< PIPE_MAX_SHADER_OUTPUTS
; i
++) {
835 gs_output_semantic_name
[i
] = TGSI_SEMANTIC_GENERIC
;
836 gs_output_semantic_index
[i
] = 0;
841 * Determine number of outputs, the (default) output register
842 * mapping and the semantic information for each output.
844 for (attr
= 0; attr
< GEOM_RESULT_MAX
; attr
++) {
845 if (stgp
->Base
.Base
.OutputsWritten
& BITFIELD64_BIT(attr
)) {
848 slot
= gs_num_outputs
;
850 outputMapping
[attr
] = slot
;
853 case GEOM_RESULT_POS
:
855 gs_output_semantic_name
[slot
] = TGSI_SEMANTIC_POSITION
;
856 gs_output_semantic_index
[slot
] = 0;
858 case GEOM_RESULT_COL0
:
859 gs_output_semantic_name
[slot
] = TGSI_SEMANTIC_COLOR
;
860 gs_output_semantic_index
[slot
] = 0;
862 case GEOM_RESULT_COL1
:
863 gs_output_semantic_name
[slot
] = TGSI_SEMANTIC_COLOR
;
864 gs_output_semantic_index
[slot
] = 1;
866 case GEOM_RESULT_SCOL0
:
867 gs_output_semantic_name
[slot
] = TGSI_SEMANTIC_BCOLOR
;
868 gs_output_semantic_index
[slot
] = 0;
870 case GEOM_RESULT_SCOL1
:
871 gs_output_semantic_name
[slot
] = TGSI_SEMANTIC_BCOLOR
;
872 gs_output_semantic_index
[slot
] = 1;
874 case GEOM_RESULT_FOGC
:
875 gs_output_semantic_name
[slot
] = TGSI_SEMANTIC_FOG
;
876 gs_output_semantic_index
[slot
] = 0;
878 case GEOM_RESULT_PSIZ
:
879 gs_output_semantic_name
[slot
] = TGSI_SEMANTIC_PSIZE
;
880 gs_output_semantic_index
[slot
] = 0;
882 case GEOM_RESULT_TEX0
:
883 case GEOM_RESULT_TEX1
:
884 case GEOM_RESULT_TEX2
:
885 case GEOM_RESULT_TEX3
:
886 case GEOM_RESULT_TEX4
:
887 case GEOM_RESULT_TEX5
:
888 case GEOM_RESULT_TEX6
:
889 case GEOM_RESULT_TEX7
:
891 case GEOM_RESULT_VAR0
:
894 assert(slot
< Elements(gs_output_semantic_name
));
895 /* use default semantic info */
896 gs_output_semantic_name
[slot
] = TGSI_SEMANTIC_GENERIC
;
897 gs_output_semantic_index
[slot
] = num_generic
++;
902 assert(gs_output_semantic_name
[0] == TGSI_SEMANTIC_POSITION
);
904 /* find max output slot referenced to compute gs_num_outputs */
905 for (attr
= 0; attr
< GEOM_RESULT_MAX
; attr
++) {
906 if (outputMapping
[attr
] != ~0 && outputMapping
[attr
] > maxSlot
)
907 maxSlot
= outputMapping
[attr
];
909 gs_num_outputs
= maxSlot
+ 1;
914 printf("outputMapping? %d\n", outputMapping
? 1 : 0);
916 printf("attr -> slot\n");
917 for (i
= 0; i
< 16; i
++) {
918 printf(" %2d %3d\n", i
, outputMapping
[i
]);
921 printf("slot sem_name sem_index\n");
922 for (i
= 0; i
< gs_num_outputs
; i
++) {
923 printf(" %2d %d %d\n",
925 gs_output_semantic_name
[i
],
926 gs_output_semantic_index
[i
]);
931 /* free old shader state, if any */
932 if (stgp
->tgsi
.tokens
) {
933 st_free_tokens(stgp
->tgsi
.tokens
);
934 stgp
->tgsi
.tokens
= NULL
;
937 ureg_property_gs_input_prim(ureg
, stgp
->Base
.InputType
);
938 ureg_property_gs_output_prim(ureg
, stgp
->Base
.OutputType
);
939 ureg_property_gs_max_vertices(ureg
, stgp
->Base
.VerticesOut
);
941 error
= st_translate_mesa_program(st
->ctx
,
942 TGSI_PROCESSOR_GEOMETRY
,
948 stgp
->input_semantic_name
,
949 stgp
->input_semantic_index
,
954 gs_output_semantic_name
,
955 gs_output_semantic_index
,
958 stgp
->num_inputs
= gs_num_inputs
;
959 stgp
->tgsi
.tokens
= ureg_get_tokens( ureg
, NULL
);
960 ureg_destroy( ureg
);
962 /* fill in new variant */
963 gpv
->driver_shader
= pipe
->create_gs_state(pipe
, &stgp
->tgsi
);
966 if ((ST_DEBUG
& DEBUG_TGSI
) && (ST_DEBUG
& DEBUG_MESA
)) {
967 _mesa_print_program(&stgp
->Base
.Base
);
971 if (ST_DEBUG
& DEBUG_TGSI
) {
972 tgsi_dump(stgp
->tgsi
.tokens
, 0);
981 * Get/create geometry program variant.
983 struct st_gp_variant
*
984 st_get_gp_variant(struct st_context
*st
,
985 struct st_geometry_program
*stgp
,
986 const struct st_gp_variant_key
*key
)
988 struct st_gp_variant
*gpv
;
990 /* Search for existing variant */
991 for (gpv
= stgp
->variants
; gpv
; gpv
= gpv
->next
) {
992 if (memcmp(&gpv
->key
, key
, sizeof(*key
)) == 0) {
999 gpv
= st_translate_geometry_program(st
, stgp
, key
);
1001 /* insert into list */
1002 gpv
->next
= stgp
->variants
;
1003 stgp
->variants
= gpv
;
1014 * Debug- print current shader text
1017 st_print_shaders(struct gl_context
*ctx
)
1019 struct gl_shader_program
*shProg
[3] = {
1020 ctx
->Shader
.CurrentVertexProgram
,
1021 ctx
->Shader
.CurrentGeometryProgram
,
1022 ctx
->Shader
.CurrentFragmentProgram
,
1026 for (j
= 0; j
< 3; j
++) {
1029 if (shProg
[j
] == NULL
)
1032 for (i
= 0; i
< shProg
[j
]->NumShaders
; i
++) {
1033 struct gl_shader
*sh
;
1035 switch (shProg
[j
]->Shaders
[i
]->Type
) {
1036 case GL_VERTEX_SHADER
:
1037 sh
= (i
!= 0) ? NULL
: shProg
[j
]->Shaders
[i
];
1039 case GL_GEOMETRY_SHADER_ARB
:
1040 sh
= (i
!= 1) ? NULL
: shProg
[j
]->Shaders
[i
];
1042 case GL_FRAGMENT_SHADER
:
1043 sh
= (i
!= 2) ? NULL
: shProg
[j
]->Shaders
[i
];
1052 printf("GLSL shader %u of %u:\n", i
, shProg
[j
]->NumShaders
);
1053 printf("%s\n", sh
->Source
);
1061 * Vert/Geom/Frag programs have per-context variants. Free all the
1062 * variants attached to the given program which match the given context.
1065 destroy_program_variants(struct st_context
*st
, struct gl_program
*program
)
1070 switch (program
->Target
) {
1071 case GL_VERTEX_PROGRAM_ARB
:
1073 struct st_vertex_program
*stvp
= (struct st_vertex_program
*) program
;
1074 struct st_vp_variant
*vpv
, **prevPtr
= &stvp
->variants
;
1076 for (vpv
= stvp
->variants
; vpv
; ) {
1077 struct st_vp_variant
*next
= vpv
->next
;
1078 if (vpv
->key
.st
== st
) {
1079 /* unlink from list */
1081 /* destroy this variant */
1082 delete_vp_variant(st
, vpv
);
1085 prevPtr
= &vpv
->next
;
1091 case GL_FRAGMENT_PROGRAM_ARB
:
1093 struct st_fragment_program
*stfp
=
1094 (struct st_fragment_program
*) program
;
1095 struct st_fp_variant
*fpv
, **prevPtr
= &stfp
->variants
;
1097 for (fpv
= stfp
->variants
; fpv
; ) {
1098 struct st_fp_variant
*next
= fpv
->next
;
1099 if (fpv
->key
.st
== st
) {
1100 /* unlink from list */
1102 /* destroy this variant */
1103 delete_fp_variant(st
, fpv
);
1106 prevPtr
= &fpv
->next
;
1112 case MESA_GEOMETRY_PROGRAM
:
1114 struct st_geometry_program
*stgp
=
1115 (struct st_geometry_program
*) program
;
1116 struct st_gp_variant
*gpv
, **prevPtr
= &stgp
->variants
;
1118 for (gpv
= stgp
->variants
; gpv
; ) {
1119 struct st_gp_variant
*next
= gpv
->next
;
1120 if (gpv
->key
.st
== st
) {
1121 /* unlink from list */
1123 /* destroy this variant */
1124 delete_gp_variant(st
, gpv
);
1127 prevPtr
= &gpv
->next
;
1134 _mesa_problem(NULL
, "Unexpected program target 0x%x in "
1135 "destroy_program_variants_cb()", program
->Target
);
1141 * Callback for _mesa_HashWalk. Free all the shader's program variants
1142 * which match the given context.
1145 destroy_shader_program_variants_cb(GLuint key
, void *data
, void *userData
)
1147 struct st_context
*st
= (struct st_context
*) userData
;
1148 struct gl_shader
*shader
= (struct gl_shader
*) data
;
1150 switch (shader
->Type
) {
1151 case GL_SHADER_PROGRAM_MESA
:
1153 struct gl_shader_program
*shProg
= (struct gl_shader_program
*) data
;
1156 for (i
= 0; i
< shProg
->NumShaders
; i
++) {
1157 destroy_program_variants(st
, shProg
->Shaders
[i
]->Program
);
1160 destroy_program_variants(st
, (struct gl_program
*)
1161 shProg
->VertexProgram
);
1162 destroy_program_variants(st
, (struct gl_program
*)
1163 shProg
->FragmentProgram
);
1164 destroy_program_variants(st
, (struct gl_program
*)
1165 shProg
->GeometryProgram
);
1168 case GL_VERTEX_SHADER
:
1169 case GL_FRAGMENT_SHADER
:
1170 case GL_GEOMETRY_SHADER
:
1172 destroy_program_variants(st
, shader
->Program
);
1182 * Callback for _mesa_HashWalk. Free all the program variants which match
1183 * the given context.
1186 destroy_program_variants_cb(GLuint key
, void *data
, void *userData
)
1188 struct st_context
*st
= (struct st_context
*) userData
;
1189 struct gl_program
*program
= (struct gl_program
*) data
;
1190 destroy_program_variants(st
, program
);
1195 * Walk over all shaders and programs to delete any variants which
1196 * belong to the given context.
1197 * This is called during context tear-down.
1200 st_destroy_program_variants(struct st_context
*st
)
1202 /* ARB vert/frag program */
1203 _mesa_HashWalk(st
->ctx
->Shared
->Programs
,
1204 destroy_program_variants_cb
, st
);
1206 /* GLSL vert/frag/geom shaders */
1207 _mesa_HashWalk(st
->ctx
->Shared
->ShaderObjects
,
1208 destroy_shader_program_variants_cb
, st
);