1 /**************************************************************************
3 * Copyright 2007 VMware, Inc.
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 VMWARE 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 <keithw@vmware.com>
34 #include "main/imports.h"
35 #include "main/hash.h"
36 #include "main/mtypes.h"
37 #include "program/prog_parameter.h"
38 #include "program/prog_print.h"
39 #include "program/programopt.h"
41 #include "pipe/p_context.h"
42 #include "pipe/p_defines.h"
43 #include "pipe/p_shader_tokens.h"
44 #include "draw/draw_context.h"
45 #include "tgsi/tgsi_dump.h"
46 #include "tgsi/tgsi_ureg.h"
49 #include "st_cb_bitmap.h"
50 #include "st_cb_drawpixels.h"
51 #include "st_context.h"
52 #include "st_program.h"
53 #include "st_mesa_to_tgsi.h"
54 #include "cso_cache/cso_context.h"
59 * Delete a vertex program variant. Note the caller must unlink
60 * the variant from the linked list.
63 delete_vp_variant(struct st_context
*st
, struct st_vp_variant
*vpv
)
65 if (vpv
->driver_shader
)
66 cso_delete_vertex_shader(st
->cso_context
, vpv
->driver_shader
);
69 draw_delete_vertex_shader( st
->draw
, vpv
->draw_shader
);
72 ureg_free_tokens(vpv
->tgsi
.tokens
);
80 * Clean out any old compilations:
83 st_release_vp_variants( struct st_context
*st
,
84 struct st_vertex_program
*stvp
)
86 struct st_vp_variant
*vpv
;
88 for (vpv
= stvp
->variants
; vpv
; ) {
89 struct st_vp_variant
*next
= vpv
->next
;
90 delete_vp_variant(st
, vpv
);
94 stvp
->variants
= NULL
;
100 * Delete a fragment program variant. Note the caller must unlink
101 * the variant from the linked list.
104 delete_fp_variant(struct st_context
*st
, struct st_fp_variant
*fpv
)
106 if (fpv
->driver_shader
)
107 cso_delete_fragment_shader(st
->cso_context
, fpv
->driver_shader
);
109 _mesa_free_parameter_list(fpv
->parameters
);
110 if (fpv
->tgsi
.tokens
)
111 ureg_free_tokens(fpv
->tgsi
.tokens
);
117 * Free all variants of a fragment program.
120 st_release_fp_variants(struct st_context
*st
, struct st_fragment_program
*stfp
)
122 struct st_fp_variant
*fpv
;
124 for (fpv
= stfp
->variants
; fpv
; ) {
125 struct st_fp_variant
*next
= fpv
->next
;
126 delete_fp_variant(st
, fpv
);
130 stfp
->variants
= NULL
;
135 * Delete a geometry program variant. Note the caller must unlink
136 * the variant from the linked list.
139 delete_gp_variant(struct st_context
*st
, struct st_gp_variant
*gpv
)
141 if (gpv
->driver_shader
)
142 cso_delete_geometry_shader(st
->cso_context
, gpv
->driver_shader
);
149 * Free all variants of a geometry program.
152 st_release_gp_variants(struct st_context
*st
, struct st_geometry_program
*stgp
)
154 struct st_gp_variant
*gpv
;
156 for (gpv
= stgp
->variants
; gpv
; ) {
157 struct st_gp_variant
*next
= gpv
->next
;
158 delete_gp_variant(st
, gpv
);
162 stgp
->variants
= NULL
;
169 * Translate a Mesa vertex shader into a TGSI shader.
170 * \param outputMapping to map vertex program output registers (VARYING_SLOT_x)
171 * to TGSI output slots
172 * \param tokensOut destination for TGSI tokens
173 * \return pointer to cached pipe_shader object.
176 st_prepare_vertex_program(struct gl_context
*ctx
,
177 struct st_vertex_program
*stvp
)
179 struct st_context
*st
= st_context(ctx
);
182 stvp
->num_inputs
= 0;
183 stvp
->num_outputs
= 0;
185 if (stvp
->Base
.IsPositionInvariant
)
186 _mesa_insert_mvp_code(ctx
, &stvp
->Base
);
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
& BITFIELD64_BIT(attr
)) != 0) {
194 stvp
->input_to_index
[attr
] = stvp
->num_inputs
;
195 stvp
->index_to_input
[stvp
->num_inputs
] = attr
;
197 if ((stvp
->Base
.Base
.DoubleInputsRead
& BITFIELD64_BIT(attr
)) != 0) {
198 /* add placeholder for second part of a double attribute */
199 stvp
->index_to_input
[stvp
->num_inputs
] = ST_DOUBLE_ATTRIB_PLACEHOLDER
;
204 /* bit of a hack, presetup potentially unused edgeflag input */
205 stvp
->input_to_index
[VERT_ATTRIB_EDGEFLAG
] = stvp
->num_inputs
;
206 stvp
->index_to_input
[stvp
->num_inputs
] = VERT_ATTRIB_EDGEFLAG
;
208 /* Compute mapping of vertex program outputs to slots.
210 for (attr
= 0; attr
< VARYING_SLOT_MAX
; attr
++) {
211 if ((stvp
->Base
.Base
.OutputsWritten
& BITFIELD64_BIT(attr
)) == 0) {
212 stvp
->result_to_output
[attr
] = ~0;
215 unsigned slot
= stvp
->num_outputs
++;
217 stvp
->result_to_output
[attr
] = slot
;
220 case VARYING_SLOT_POS
:
221 stvp
->output_semantic_name
[slot
] = TGSI_SEMANTIC_POSITION
;
222 stvp
->output_semantic_index
[slot
] = 0;
224 case VARYING_SLOT_COL0
:
225 stvp
->output_semantic_name
[slot
] = TGSI_SEMANTIC_COLOR
;
226 stvp
->output_semantic_index
[slot
] = 0;
228 case VARYING_SLOT_COL1
:
229 stvp
->output_semantic_name
[slot
] = TGSI_SEMANTIC_COLOR
;
230 stvp
->output_semantic_index
[slot
] = 1;
232 case VARYING_SLOT_BFC0
:
233 stvp
->output_semantic_name
[slot
] = TGSI_SEMANTIC_BCOLOR
;
234 stvp
->output_semantic_index
[slot
] = 0;
236 case VARYING_SLOT_BFC1
:
237 stvp
->output_semantic_name
[slot
] = TGSI_SEMANTIC_BCOLOR
;
238 stvp
->output_semantic_index
[slot
] = 1;
240 case VARYING_SLOT_FOGC
:
241 stvp
->output_semantic_name
[slot
] = TGSI_SEMANTIC_FOG
;
242 stvp
->output_semantic_index
[slot
] = 0;
244 case VARYING_SLOT_PSIZ
:
245 stvp
->output_semantic_name
[slot
] = TGSI_SEMANTIC_PSIZE
;
246 stvp
->output_semantic_index
[slot
] = 0;
248 case VARYING_SLOT_CLIP_DIST0
:
249 stvp
->output_semantic_name
[slot
] = TGSI_SEMANTIC_CLIPDIST
;
250 stvp
->output_semantic_index
[slot
] = 0;
252 case VARYING_SLOT_CLIP_DIST1
:
253 stvp
->output_semantic_name
[slot
] = TGSI_SEMANTIC_CLIPDIST
;
254 stvp
->output_semantic_index
[slot
] = 1;
256 case VARYING_SLOT_EDGE
:
259 case VARYING_SLOT_CLIP_VERTEX
:
260 stvp
->output_semantic_name
[slot
] = TGSI_SEMANTIC_CLIPVERTEX
;
261 stvp
->output_semantic_index
[slot
] = 0;
263 case VARYING_SLOT_LAYER
:
264 stvp
->output_semantic_name
[slot
] = TGSI_SEMANTIC_LAYER
;
265 stvp
->output_semantic_index
[slot
] = 0;
267 case VARYING_SLOT_VIEWPORT
:
268 stvp
->output_semantic_name
[slot
] = TGSI_SEMANTIC_VIEWPORT_INDEX
;
269 stvp
->output_semantic_index
[slot
] = 0;
272 case VARYING_SLOT_TEX0
:
273 case VARYING_SLOT_TEX1
:
274 case VARYING_SLOT_TEX2
:
275 case VARYING_SLOT_TEX3
:
276 case VARYING_SLOT_TEX4
:
277 case VARYING_SLOT_TEX5
:
278 case VARYING_SLOT_TEX6
:
279 case VARYING_SLOT_TEX7
:
280 if (st
->needs_texcoord_semantic
) {
281 stvp
->output_semantic_name
[slot
] = TGSI_SEMANTIC_TEXCOORD
;
282 stvp
->output_semantic_index
[slot
] = attr
- VARYING_SLOT_TEX0
;
286 case VARYING_SLOT_VAR0
:
288 assert(attr
< VARYING_SLOT_MAX
);
289 stvp
->output_semantic_name
[slot
] = TGSI_SEMANTIC_GENERIC
;
290 stvp
->output_semantic_index
[slot
] =
291 st_get_generic_varying_index(st
, attr
);
296 /* similar hack to above, presetup potentially unused edgeflag output */
297 stvp
->result_to_output
[VARYING_SLOT_EDGE
] = stvp
->num_outputs
;
298 stvp
->output_semantic_name
[stvp
->num_outputs
] = TGSI_SEMANTIC_EDGEFLAG
;
299 stvp
->output_semantic_index
[stvp
->num_outputs
] = 0;
304 * Translate a vertex program to create a new variant.
306 static struct st_vp_variant
*
307 st_translate_vertex_program(struct st_context
*st
,
308 struct st_vertex_program
*stvp
,
309 const struct st_vp_variant_key
*key
)
311 struct st_vp_variant
*vpv
= CALLOC_STRUCT(st_vp_variant
);
312 struct pipe_context
*pipe
= st
->pipe
;
313 struct ureg_program
*ureg
;
314 enum pipe_error error
;
315 unsigned num_outputs
;
317 st_prepare_vertex_program(st
->ctx
, stvp
);
319 if (!stvp
->glsl_to_tgsi
)
321 _mesa_remove_output_reads(&stvp
->Base
.Base
, PROGRAM_OUTPUT
);
324 ureg
= ureg_create( TGSI_PROCESSOR_VERTEX
);
332 vpv
->num_inputs
= stvp
->num_inputs
;
333 num_outputs
= stvp
->num_outputs
;
334 if (key
->passthrough_edgeflags
) {
339 if (ST_DEBUG
& DEBUG_MESA
) {
340 _mesa_print_program(&stvp
->Base
.Base
);
341 _mesa_print_program_parameters(st
->ctx
, &stvp
->Base
.Base
);
345 if (stvp
->glsl_to_tgsi
)
346 error
= st_translate_program(st
->ctx
,
347 TGSI_PROCESSOR_VERTEX
,
353 stvp
->input_to_index
,
354 NULL
, /* input semantic name */
355 NULL
, /* input semantic index */
356 NULL
, /* interp mode */
357 NULL
, /* interp location */
360 stvp
->result_to_output
,
361 stvp
->output_semantic_name
,
362 stvp
->output_semantic_index
,
363 key
->passthrough_edgeflags
,
366 error
= st_translate_mesa_program(st
->ctx
,
367 TGSI_PROCESSOR_VERTEX
,
372 stvp
->input_to_index
,
373 NULL
, /* input semantic name */
374 NULL
, /* input semantic index */
378 stvp
->result_to_output
,
379 stvp
->output_semantic_name
,
380 stvp
->output_semantic_index
,
381 key
->passthrough_edgeflags
,
387 vpv
->tgsi
.tokens
= ureg_get_tokens( ureg
, NULL
);
388 if (!vpv
->tgsi
.tokens
)
391 ureg_destroy( ureg
);
393 if (stvp
->glsl_to_tgsi
) {
394 st_translate_stream_output_info(stvp
->glsl_to_tgsi
,
395 stvp
->result_to_output
,
396 &vpv
->tgsi
.stream_output
);
399 if (ST_DEBUG
& DEBUG_TGSI
) {
400 tgsi_dump(vpv
->tgsi
.tokens
, 0);
404 vpv
->driver_shader
= pipe
->create_vs_state(pipe
, &vpv
->tgsi
);
408 debug_printf("%s: failed to translate Mesa program:\n", __func__
);
409 _mesa_print_program(&stvp
->Base
.Base
);
412 ureg_destroy( ureg
);
418 * Find/create a vertex program variant.
420 struct st_vp_variant
*
421 st_get_vp_variant(struct st_context
*st
,
422 struct st_vertex_program
*stvp
,
423 const struct st_vp_variant_key
*key
)
425 struct st_vp_variant
*vpv
;
427 /* Search for existing variant */
428 for (vpv
= stvp
->variants
; vpv
; vpv
= vpv
->next
) {
429 if (memcmp(&vpv
->key
, key
, sizeof(*key
)) == 0) {
436 vpv
= st_translate_vertex_program(st
, stvp
, key
);
438 /* insert into list */
439 vpv
->next
= stvp
->variants
;
440 stvp
->variants
= vpv
;
449 st_translate_interp(enum glsl_interp_qualifier glsl_qual
, bool is_color
)
452 case INTERP_QUALIFIER_NONE
:
454 return TGSI_INTERPOLATE_COLOR
;
455 return TGSI_INTERPOLATE_PERSPECTIVE
;
456 case INTERP_QUALIFIER_SMOOTH
:
457 return TGSI_INTERPOLATE_PERSPECTIVE
;
458 case INTERP_QUALIFIER_FLAT
:
459 return TGSI_INTERPOLATE_CONSTANT
;
460 case INTERP_QUALIFIER_NOPERSPECTIVE
:
461 return TGSI_INTERPOLATE_LINEAR
;
463 assert(0 && "unexpected interp mode in st_translate_interp()");
464 return TGSI_INTERPOLATE_PERSPECTIVE
;
470 * Translate a Mesa fragment shader into a TGSI shader using extra info in
472 * \return new fragment program variant
474 static struct st_fp_variant
*
475 st_translate_fragment_program(struct st_context
*st
,
476 struct st_fragment_program
*stfp
,
477 const struct st_fp_variant_key
*key
)
479 struct pipe_context
*pipe
= st
->pipe
;
480 struct st_fp_variant
*variant
= CALLOC_STRUCT(st_fp_variant
);
481 GLboolean deleteFP
= GL_FALSE
;
483 GLuint outputMapping
[FRAG_RESULT_MAX
];
484 GLuint inputMapping
[VARYING_SLOT_MAX
];
485 GLuint interpMode
[PIPE_MAX_SHADER_INPUTS
]; /* XXX size? */
486 GLuint interpLocation
[PIPE_MAX_SHADER_INPUTS
];
488 GLbitfield64 inputsRead
;
489 struct ureg_program
*ureg
;
491 GLboolean write_all
= GL_FALSE
;
493 ubyte input_semantic_name
[PIPE_MAX_SHADER_INPUTS
];
494 ubyte input_semantic_index
[PIPE_MAX_SHADER_INPUTS
];
495 uint fs_num_inputs
= 0;
497 ubyte fs_output_semantic_name
[PIPE_MAX_SHADER_OUTPUTS
];
498 ubyte fs_output_semantic_index
[PIPE_MAX_SHADER_OUTPUTS
];
499 uint fs_num_outputs
= 0;
504 assert(!(key
->bitmap
&& key
->drawpixels
));
507 /* glBitmap drawing */
508 struct gl_fragment_program
*fp
; /* we free this temp program below */
510 st_make_bitmap_fragment_program(st
, &stfp
->Base
,
511 &fp
, &variant
->bitmap_sampler
);
513 variant
->parameters
= _mesa_clone_parameter_list(fp
->Base
.Parameters
);
514 stfp
= st_fragment_program(fp
);
517 else if (key
->drawpixels
) {
518 /* glDrawPixels drawing */
519 struct gl_fragment_program
*fp
; /* we free this temp program below */
521 if (key
->drawpixels_z
|| key
->drawpixels_stencil
) {
522 fp
= st_make_drawpix_z_stencil_program(st
, key
->drawpixels_z
,
523 key
->drawpixels_stencil
);
527 st_make_drawpix_fragment_program(st
, &stfp
->Base
, &fp
);
528 variant
->parameters
= _mesa_clone_parameter_list(fp
->Base
.Parameters
);
531 stfp
= st_fragment_program(fp
);
534 if (!stfp
->glsl_to_tgsi
)
535 _mesa_remove_output_reads(&stfp
->Base
.Base
, PROGRAM_OUTPUT
);
538 * Convert Mesa program inputs to TGSI input register semantics.
540 inputsRead
= stfp
->Base
.Base
.InputsRead
;
541 for (attr
= 0; attr
< VARYING_SLOT_MAX
; attr
++) {
542 if ((inputsRead
& BITFIELD64_BIT(attr
)) != 0) {
543 const GLuint slot
= fs_num_inputs
++;
545 inputMapping
[attr
] = slot
;
546 if (stfp
->Base
.IsCentroid
& BITFIELD64_BIT(attr
))
547 interpLocation
[slot
] = TGSI_INTERPOLATE_LOC_CENTROID
;
548 else if (stfp
->Base
.IsSample
& BITFIELD64_BIT(attr
))
549 interpLocation
[slot
] = TGSI_INTERPOLATE_LOC_SAMPLE
;
551 interpLocation
[slot
] = TGSI_INTERPOLATE_LOC_CENTER
;
553 if (key
->persample_shading
)
554 interpLocation
[slot
] = TGSI_INTERPOLATE_LOC_SAMPLE
;
557 case VARYING_SLOT_POS
:
558 input_semantic_name
[slot
] = TGSI_SEMANTIC_POSITION
;
559 input_semantic_index
[slot
] = 0;
560 interpMode
[slot
] = TGSI_INTERPOLATE_LINEAR
;
562 case VARYING_SLOT_COL0
:
563 input_semantic_name
[slot
] = TGSI_SEMANTIC_COLOR
;
564 input_semantic_index
[slot
] = 0;
565 interpMode
[slot
] = st_translate_interp(stfp
->Base
.InterpQualifier
[attr
],
568 case VARYING_SLOT_COL1
:
569 input_semantic_name
[slot
] = TGSI_SEMANTIC_COLOR
;
570 input_semantic_index
[slot
] = 1;
571 interpMode
[slot
] = st_translate_interp(stfp
->Base
.InterpQualifier
[attr
],
574 case VARYING_SLOT_FOGC
:
575 input_semantic_name
[slot
] = TGSI_SEMANTIC_FOG
;
576 input_semantic_index
[slot
] = 0;
577 interpMode
[slot
] = TGSI_INTERPOLATE_PERSPECTIVE
;
579 case VARYING_SLOT_FACE
:
580 input_semantic_name
[slot
] = TGSI_SEMANTIC_FACE
;
581 input_semantic_index
[slot
] = 0;
582 interpMode
[slot
] = TGSI_INTERPOLATE_CONSTANT
;
584 case VARYING_SLOT_PRIMITIVE_ID
:
585 input_semantic_name
[slot
] = TGSI_SEMANTIC_PRIMID
;
586 input_semantic_index
[slot
] = 0;
587 interpMode
[slot
] = TGSI_INTERPOLATE_CONSTANT
;
589 case VARYING_SLOT_LAYER
:
590 input_semantic_name
[slot
] = TGSI_SEMANTIC_LAYER
;
591 input_semantic_index
[slot
] = 0;
592 interpMode
[slot
] = TGSI_INTERPOLATE_CONSTANT
;
594 case VARYING_SLOT_VIEWPORT
:
595 input_semantic_name
[slot
] = TGSI_SEMANTIC_VIEWPORT_INDEX
;
596 input_semantic_index
[slot
] = 0;
597 interpMode
[slot
] = TGSI_INTERPOLATE_CONSTANT
;
599 case VARYING_SLOT_CLIP_DIST0
:
600 input_semantic_name
[slot
] = TGSI_SEMANTIC_CLIPDIST
;
601 input_semantic_index
[slot
] = 0;
602 interpMode
[slot
] = TGSI_INTERPOLATE_PERSPECTIVE
;
604 case VARYING_SLOT_CLIP_DIST1
:
605 input_semantic_name
[slot
] = TGSI_SEMANTIC_CLIPDIST
;
606 input_semantic_index
[slot
] = 1;
607 interpMode
[slot
] = TGSI_INTERPOLATE_PERSPECTIVE
;
609 /* In most cases, there is nothing special about these
610 * inputs, so adopt a convention to use the generic
611 * semantic name and the mesa VARYING_SLOT_ number as the
614 * All that is required is that the vertex shader labels
615 * its own outputs similarly, and that the vertex shader
616 * generates at least every output required by the
617 * fragment shader plus fixed-function hardware (such as
620 * However, some drivers may need us to identify the PNTC and TEXi
621 * varyings if, for example, their capability to replace them with
622 * sprite coordinates is limited.
624 case VARYING_SLOT_PNTC
:
625 if (st
->needs_texcoord_semantic
) {
626 input_semantic_name
[slot
] = TGSI_SEMANTIC_PCOORD
;
627 input_semantic_index
[slot
] = 0;
628 interpMode
[slot
] = TGSI_INTERPOLATE_LINEAR
;
632 case VARYING_SLOT_TEX0
:
633 case VARYING_SLOT_TEX1
:
634 case VARYING_SLOT_TEX2
:
635 case VARYING_SLOT_TEX3
:
636 case VARYING_SLOT_TEX4
:
637 case VARYING_SLOT_TEX5
:
638 case VARYING_SLOT_TEX6
:
639 case VARYING_SLOT_TEX7
:
640 if (st
->needs_texcoord_semantic
) {
641 input_semantic_name
[slot
] = TGSI_SEMANTIC_TEXCOORD
;
642 input_semantic_index
[slot
] = attr
- VARYING_SLOT_TEX0
;
644 st_translate_interp(stfp
->Base
.InterpQualifier
[attr
], FALSE
);
648 case VARYING_SLOT_VAR0
:
650 /* Semantic indices should be zero-based because drivers may choose
651 * to assign a fixed slot determined by that index.
652 * This is useful because ARB_separate_shader_objects uses location
653 * qualifiers for linkage, and if the semantic index corresponds to
654 * these locations, linkage passes in the driver become unecessary.
656 * If needs_texcoord_semantic is true, no semantic indices will be
657 * consumed for the TEXi varyings, and we can base the locations of
658 * the user varyings on VAR0. Otherwise, we use TEX0 as base index.
660 assert(attr
>= VARYING_SLOT_TEX0
);
661 input_semantic_name
[slot
] = TGSI_SEMANTIC_GENERIC
;
662 input_semantic_index
[slot
] = st_get_generic_varying_index(st
, attr
);
663 if (attr
== VARYING_SLOT_PNTC
)
664 interpMode
[slot
] = TGSI_INTERPOLATE_LINEAR
;
666 interpMode
[slot
] = st_translate_interp(stfp
->Base
.InterpQualifier
[attr
],
672 inputMapping
[attr
] = -1;
677 * Semantics and mapping for outputs
681 GLbitfield64 outputsWritten
= stfp
->Base
.Base
.OutputsWritten
;
683 /* if z is written, emit that first */
684 if (outputsWritten
& BITFIELD64_BIT(FRAG_RESULT_DEPTH
)) {
685 fs_output_semantic_name
[fs_num_outputs
] = TGSI_SEMANTIC_POSITION
;
686 fs_output_semantic_index
[fs_num_outputs
] = 0;
687 outputMapping
[FRAG_RESULT_DEPTH
] = fs_num_outputs
;
689 outputsWritten
&= ~(1 << FRAG_RESULT_DEPTH
);
692 if (outputsWritten
& BITFIELD64_BIT(FRAG_RESULT_STENCIL
)) {
693 fs_output_semantic_name
[fs_num_outputs
] = TGSI_SEMANTIC_STENCIL
;
694 fs_output_semantic_index
[fs_num_outputs
] = 0;
695 outputMapping
[FRAG_RESULT_STENCIL
] = fs_num_outputs
;
697 outputsWritten
&= ~(1 << FRAG_RESULT_STENCIL
);
700 if (outputsWritten
& BITFIELD64_BIT(FRAG_RESULT_SAMPLE_MASK
)) {
701 fs_output_semantic_name
[fs_num_outputs
] = TGSI_SEMANTIC_SAMPLEMASK
;
702 fs_output_semantic_index
[fs_num_outputs
] = 0;
703 outputMapping
[FRAG_RESULT_SAMPLE_MASK
] = fs_num_outputs
;
705 outputsWritten
&= ~(1 << FRAG_RESULT_SAMPLE_MASK
);
708 /* handle remaining outputs (color) */
709 for (attr
= 0; attr
< FRAG_RESULT_MAX
; attr
++) {
710 if (outputsWritten
& BITFIELD64_BIT(attr
)) {
712 case FRAG_RESULT_DEPTH
:
713 case FRAG_RESULT_STENCIL
:
714 case FRAG_RESULT_SAMPLE_MASK
:
718 case FRAG_RESULT_COLOR
:
719 write_all
= GL_TRUE
; /* fallthrough */
721 assert(attr
== FRAG_RESULT_COLOR
||
722 (FRAG_RESULT_DATA0
<= attr
&& attr
< FRAG_RESULT_MAX
));
723 fs_output_semantic_name
[fs_num_outputs
] = TGSI_SEMANTIC_COLOR
;
724 fs_output_semantic_index
[fs_num_outputs
] = numColors
;
725 outputMapping
[attr
] = fs_num_outputs
;
735 ureg
= ureg_create( TGSI_PROCESSOR_FRAGMENT
);
741 if (ST_DEBUG
& DEBUG_MESA
) {
742 _mesa_print_program(&stfp
->Base
.Base
);
743 _mesa_print_program_parameters(st
->ctx
, &stfp
->Base
.Base
);
746 if (write_all
== GL_TRUE
)
747 ureg_property(ureg
, TGSI_PROPERTY_FS_COLOR0_WRITES_ALL_CBUFS
, 1);
749 if (stfp
->Base
.FragDepthLayout
!= FRAG_DEPTH_LAYOUT_NONE
) {
750 switch (stfp
->Base
.FragDepthLayout
) {
751 case FRAG_DEPTH_LAYOUT_ANY
:
752 ureg_property(ureg
, TGSI_PROPERTY_FS_DEPTH_LAYOUT
,
753 TGSI_FS_DEPTH_LAYOUT_ANY
);
755 case FRAG_DEPTH_LAYOUT_GREATER
:
756 ureg_property(ureg
, TGSI_PROPERTY_FS_DEPTH_LAYOUT
,
757 TGSI_FS_DEPTH_LAYOUT_GREATER
);
759 case FRAG_DEPTH_LAYOUT_LESS
:
760 ureg_property(ureg
, TGSI_PROPERTY_FS_DEPTH_LAYOUT
,
761 TGSI_FS_DEPTH_LAYOUT_LESS
);
763 case FRAG_DEPTH_LAYOUT_UNCHANGED
:
764 ureg_property(ureg
, TGSI_PROPERTY_FS_DEPTH_LAYOUT
,
765 TGSI_FS_DEPTH_LAYOUT_UNCHANGED
);
772 if (stfp
->glsl_to_tgsi
)
773 st_translate_program(st
->ctx
,
774 TGSI_PROCESSOR_FRAGMENT
,
782 input_semantic_index
,
788 fs_output_semantic_name
,
789 fs_output_semantic_index
, FALSE
,
792 st_translate_mesa_program(st
->ctx
,
793 TGSI_PROCESSOR_FRAGMENT
,
800 input_semantic_index
,
805 fs_output_semantic_name
,
806 fs_output_semantic_index
, FALSE
,
809 variant
->tgsi
.tokens
= ureg_get_tokens( ureg
, NULL
);
810 ureg_destroy( ureg
);
812 if (ST_DEBUG
& DEBUG_TGSI
) {
813 tgsi_dump(variant
->tgsi
.tokens
, 0/*TGSI_DUMP_VERBOSE*/);
817 /* fill in variant */
818 variant
->driver_shader
= pipe
->create_fs_state(pipe
, &variant
->tgsi
);
822 /* Free the temporary program made above */
823 struct gl_fragment_program
*fp
= &stfp
->Base
;
824 _mesa_reference_fragprog(st
->ctx
, &fp
, NULL
);
832 * Translate fragment program if needed.
834 struct st_fp_variant
*
835 st_get_fp_variant(struct st_context
*st
,
836 struct st_fragment_program
*stfp
,
837 const struct st_fp_variant_key
*key
)
839 struct st_fp_variant
*fpv
;
841 /* Search for existing variant */
842 for (fpv
= stfp
->variants
; fpv
; fpv
= fpv
->next
) {
843 if (memcmp(&fpv
->key
, key
, sizeof(*key
)) == 0) {
850 fpv
= st_translate_fragment_program(st
, stfp
, key
);
852 /* insert into list */
853 fpv
->next
= stfp
->variants
;
854 stfp
->variants
= fpv
;
863 * Translate a geometry program to create a new variant.
865 static struct st_gp_variant
*
866 st_translate_geometry_program(struct st_context
*st
,
867 struct st_geometry_program
*stgp
,
868 const struct st_gp_variant_key
*key
)
870 GLuint inputMapping
[VARYING_SLOT_MAX
];
871 GLuint outputMapping
[VARYING_SLOT_MAX
];
872 struct pipe_context
*pipe
= st
->pipe
;
875 uint gs_num_inputs
= 0;
877 ubyte input_semantic_name
[PIPE_MAX_SHADER_INPUTS
];
878 ubyte input_semantic_index
[PIPE_MAX_SHADER_INPUTS
];
880 ubyte gs_output_semantic_name
[PIPE_MAX_SHADER_OUTPUTS
];
881 ubyte gs_output_semantic_index
[PIPE_MAX_SHADER_OUTPUTS
];
882 uint gs_num_outputs
= 0;
885 struct ureg_program
*ureg
;
886 struct pipe_shader_state state
= {0};
887 struct st_gp_variant
*gpv
;
889 gpv
= CALLOC_STRUCT(st_gp_variant
);
893 ureg
= ureg_create(TGSI_PROCESSOR_GEOMETRY
);
899 memset(inputMapping
, 0, sizeof(inputMapping
));
900 memset(outputMapping
, 0, sizeof(outputMapping
));
903 * Convert Mesa program inputs to TGSI input register semantics.
905 for (attr
= 0; attr
< VARYING_SLOT_MAX
; attr
++) {
906 if ((stgp
->Base
.Base
.InputsRead
& BITFIELD64_BIT(attr
)) != 0) {
907 const GLuint slot
= gs_num_inputs
++;
909 inputMapping
[attr
] = slot
;
912 case VARYING_SLOT_PRIMITIVE_ID
:
913 input_semantic_name
[slot
] = TGSI_SEMANTIC_PRIMID
;
914 input_semantic_index
[slot
] = 0;
916 case VARYING_SLOT_POS
:
917 input_semantic_name
[slot
] = TGSI_SEMANTIC_POSITION
;
918 input_semantic_index
[slot
] = 0;
920 case VARYING_SLOT_COL0
:
921 input_semantic_name
[slot
] = TGSI_SEMANTIC_COLOR
;
922 input_semantic_index
[slot
] = 0;
924 case VARYING_SLOT_COL1
:
925 input_semantic_name
[slot
] = TGSI_SEMANTIC_COLOR
;
926 input_semantic_index
[slot
] = 1;
928 case VARYING_SLOT_FOGC
:
929 input_semantic_name
[slot
] = TGSI_SEMANTIC_FOG
;
930 input_semantic_index
[slot
] = 0;
932 case VARYING_SLOT_CLIP_VERTEX
:
933 input_semantic_name
[slot
] = TGSI_SEMANTIC_CLIPVERTEX
;
934 input_semantic_index
[slot
] = 0;
936 case VARYING_SLOT_CLIP_DIST0
:
937 input_semantic_name
[slot
] = TGSI_SEMANTIC_CLIPDIST
;
938 input_semantic_index
[slot
] = 0;
940 case VARYING_SLOT_CLIP_DIST1
:
941 input_semantic_name
[slot
] = TGSI_SEMANTIC_CLIPDIST
;
942 input_semantic_index
[slot
] = 1;
944 case VARYING_SLOT_PSIZ
:
945 input_semantic_name
[slot
] = TGSI_SEMANTIC_PSIZE
;
946 input_semantic_index
[slot
] = 0;
948 case VARYING_SLOT_TEX0
:
949 case VARYING_SLOT_TEX1
:
950 case VARYING_SLOT_TEX2
:
951 case VARYING_SLOT_TEX3
:
952 case VARYING_SLOT_TEX4
:
953 case VARYING_SLOT_TEX5
:
954 case VARYING_SLOT_TEX6
:
955 case VARYING_SLOT_TEX7
:
956 if (st
->needs_texcoord_semantic
) {
957 input_semantic_name
[slot
] = TGSI_SEMANTIC_TEXCOORD
;
958 input_semantic_index
[slot
] = attr
- VARYING_SLOT_TEX0
;
962 case VARYING_SLOT_VAR0
:
964 assert(attr
>= VARYING_SLOT_VAR0
&& attr
< VARYING_SLOT_MAX
);
965 input_semantic_name
[slot
] = TGSI_SEMANTIC_GENERIC
;
966 input_semantic_index
[slot
] =
967 st_get_generic_varying_index(st
, attr
);
973 /* initialize output semantics to defaults */
974 for (i
= 0; i
< PIPE_MAX_SHADER_OUTPUTS
; i
++) {
975 gs_output_semantic_name
[i
] = TGSI_SEMANTIC_GENERIC
;
976 gs_output_semantic_index
[i
] = 0;
980 * Determine number of outputs, the (default) output register
981 * mapping and the semantic information for each output.
983 for (attr
= 0; attr
< VARYING_SLOT_MAX
; attr
++) {
984 if (stgp
->Base
.Base
.OutputsWritten
& BITFIELD64_BIT(attr
)) {
985 GLuint slot
= gs_num_outputs
++;
987 outputMapping
[attr
] = slot
;
990 case VARYING_SLOT_POS
:
992 gs_output_semantic_name
[slot
] = TGSI_SEMANTIC_POSITION
;
993 gs_output_semantic_index
[slot
] = 0;
995 case VARYING_SLOT_COL0
:
996 gs_output_semantic_name
[slot
] = TGSI_SEMANTIC_COLOR
;
997 gs_output_semantic_index
[slot
] = 0;
999 case VARYING_SLOT_COL1
:
1000 gs_output_semantic_name
[slot
] = TGSI_SEMANTIC_COLOR
;
1001 gs_output_semantic_index
[slot
] = 1;
1003 case VARYING_SLOT_BFC0
:
1004 gs_output_semantic_name
[slot
] = TGSI_SEMANTIC_BCOLOR
;
1005 gs_output_semantic_index
[slot
] = 0;
1007 case VARYING_SLOT_BFC1
:
1008 gs_output_semantic_name
[slot
] = TGSI_SEMANTIC_BCOLOR
;
1009 gs_output_semantic_index
[slot
] = 1;
1011 case VARYING_SLOT_FOGC
:
1012 gs_output_semantic_name
[slot
] = TGSI_SEMANTIC_FOG
;
1013 gs_output_semantic_index
[slot
] = 0;
1015 case VARYING_SLOT_PSIZ
:
1016 gs_output_semantic_name
[slot
] = TGSI_SEMANTIC_PSIZE
;
1017 gs_output_semantic_index
[slot
] = 0;
1019 case VARYING_SLOT_CLIP_VERTEX
:
1020 gs_output_semantic_name
[slot
] = TGSI_SEMANTIC_CLIPVERTEX
;
1021 gs_output_semantic_index
[slot
] = 0;
1023 case VARYING_SLOT_CLIP_DIST0
:
1024 gs_output_semantic_name
[slot
] = TGSI_SEMANTIC_CLIPDIST
;
1025 gs_output_semantic_index
[slot
] = 0;
1027 case VARYING_SLOT_CLIP_DIST1
:
1028 gs_output_semantic_name
[slot
] = TGSI_SEMANTIC_CLIPDIST
;
1029 gs_output_semantic_index
[slot
] = 1;
1031 case VARYING_SLOT_LAYER
:
1032 gs_output_semantic_name
[slot
] = TGSI_SEMANTIC_LAYER
;
1033 gs_output_semantic_index
[slot
] = 0;
1035 case VARYING_SLOT_PRIMITIVE_ID
:
1036 gs_output_semantic_name
[slot
] = TGSI_SEMANTIC_PRIMID
;
1037 gs_output_semantic_index
[slot
] = 0;
1039 case VARYING_SLOT_VIEWPORT
:
1040 gs_output_semantic_name
[slot
] = TGSI_SEMANTIC_VIEWPORT_INDEX
;
1041 gs_output_semantic_index
[slot
] = 0;
1043 case VARYING_SLOT_TEX0
:
1044 case VARYING_SLOT_TEX1
:
1045 case VARYING_SLOT_TEX2
:
1046 case VARYING_SLOT_TEX3
:
1047 case VARYING_SLOT_TEX4
:
1048 case VARYING_SLOT_TEX5
:
1049 case VARYING_SLOT_TEX6
:
1050 case VARYING_SLOT_TEX7
:
1051 if (st
->needs_texcoord_semantic
) {
1052 gs_output_semantic_name
[slot
] = TGSI_SEMANTIC_TEXCOORD
;
1053 gs_output_semantic_index
[slot
] = attr
- VARYING_SLOT_TEX0
;
1057 case VARYING_SLOT_VAR0
:
1059 assert(slot
< ARRAY_SIZE(gs_output_semantic_name
));
1060 assert(attr
>= VARYING_SLOT_VAR0
);
1061 gs_output_semantic_name
[slot
] = TGSI_SEMANTIC_GENERIC
;
1062 gs_output_semantic_index
[slot
] =
1063 st_get_generic_varying_index(st
, attr
);
1069 ureg_property(ureg
, TGSI_PROPERTY_GS_INPUT_PRIM
, stgp
->Base
.InputType
);
1070 ureg_property(ureg
, TGSI_PROPERTY_GS_OUTPUT_PRIM
, stgp
->Base
.OutputType
);
1071 ureg_property(ureg
, TGSI_PROPERTY_GS_MAX_OUTPUT_VERTICES
,
1072 stgp
->Base
.VerticesOut
);
1073 ureg_property(ureg
, TGSI_PROPERTY_GS_INVOCATIONS
, stgp
->Base
.Invocations
);
1075 st_translate_program(st
->ctx
,
1076 TGSI_PROCESSOR_GEOMETRY
,
1083 input_semantic_name
,
1084 input_semantic_index
,
1090 gs_output_semantic_name
,
1091 gs_output_semantic_index
,
1095 state
.tokens
= ureg_get_tokens(ureg
, NULL
);
1098 st_translate_stream_output_info(stgp
->glsl_to_tgsi
,
1100 &state
.stream_output
);
1102 if ((ST_DEBUG
& DEBUG_TGSI
) && (ST_DEBUG
& DEBUG_MESA
)) {
1103 _mesa_print_program(&stgp
->Base
.Base
);
1107 if (ST_DEBUG
& DEBUG_TGSI
) {
1108 tgsi_dump(state
.tokens
, 0);
1112 /* fill in new variant */
1113 gpv
->driver_shader
= pipe
->create_gs_state(pipe
, &state
);
1116 ureg_free_tokens(state
.tokens
);
1122 * Get/create geometry program variant.
1124 struct st_gp_variant
*
1125 st_get_gp_variant(struct st_context
*st
,
1126 struct st_geometry_program
*stgp
,
1127 const struct st_gp_variant_key
*key
)
1129 struct st_gp_variant
*gpv
;
1131 /* Search for existing variant */
1132 for (gpv
= stgp
->variants
; gpv
; gpv
= gpv
->next
) {
1133 if (memcmp(&gpv
->key
, key
, sizeof(*key
)) == 0) {
1140 gpv
= st_translate_geometry_program(st
, stgp
, key
);
1142 /* insert into list */
1143 gpv
->next
= stgp
->variants
;
1144 stgp
->variants
= gpv
;
1153 * Vert/Geom/Frag programs have per-context variants. Free all the
1154 * variants attached to the given program which match the given context.
1157 destroy_program_variants(struct st_context
*st
, struct gl_program
*program
)
1159 if (!program
|| program
== &_mesa_DummyProgram
)
1162 switch (program
->Target
) {
1163 case GL_VERTEX_PROGRAM_ARB
:
1165 struct st_vertex_program
*stvp
= (struct st_vertex_program
*) program
;
1166 struct st_vp_variant
*vpv
, **prevPtr
= &stvp
->variants
;
1168 for (vpv
= stvp
->variants
; vpv
; ) {
1169 struct st_vp_variant
*next
= vpv
->next
;
1170 if (vpv
->key
.st
== st
) {
1171 /* unlink from list */
1173 /* destroy this variant */
1174 delete_vp_variant(st
, vpv
);
1177 prevPtr
= &vpv
->next
;
1183 case GL_FRAGMENT_PROGRAM_ARB
:
1185 struct st_fragment_program
*stfp
=
1186 (struct st_fragment_program
*) program
;
1187 struct st_fp_variant
*fpv
, **prevPtr
= &stfp
->variants
;
1189 for (fpv
= stfp
->variants
; fpv
; ) {
1190 struct st_fp_variant
*next
= fpv
->next
;
1191 if (fpv
->key
.st
== st
) {
1192 /* unlink from list */
1194 /* destroy this variant */
1195 delete_fp_variant(st
, fpv
);
1198 prevPtr
= &fpv
->next
;
1204 case MESA_GEOMETRY_PROGRAM
:
1206 struct st_geometry_program
*stgp
=
1207 (struct st_geometry_program
*) program
;
1208 struct st_gp_variant
*gpv
, **prevPtr
= &stgp
->variants
;
1210 for (gpv
= stgp
->variants
; gpv
; ) {
1211 struct st_gp_variant
*next
= gpv
->next
;
1212 if (gpv
->key
.st
== st
) {
1213 /* unlink from list */
1215 /* destroy this variant */
1216 delete_gp_variant(st
, gpv
);
1219 prevPtr
= &gpv
->next
;
1226 _mesa_problem(NULL
, "Unexpected program target 0x%x in "
1227 "destroy_program_variants_cb()", program
->Target
);
1233 * Callback for _mesa_HashWalk. Free all the shader's program variants
1234 * which match the given context.
1237 destroy_shader_program_variants_cb(GLuint key
, void *data
, void *userData
)
1239 struct st_context
*st
= (struct st_context
*) userData
;
1240 struct gl_shader
*shader
= (struct gl_shader
*) data
;
1242 switch (shader
->Type
) {
1243 case GL_SHADER_PROGRAM_MESA
:
1245 struct gl_shader_program
*shProg
= (struct gl_shader_program
*) data
;
1248 for (i
= 0; i
< shProg
->NumShaders
; i
++) {
1249 destroy_program_variants(st
, shProg
->Shaders
[i
]->Program
);
1252 for (i
= 0; i
< ARRAY_SIZE(shProg
->_LinkedShaders
); i
++) {
1253 if (shProg
->_LinkedShaders
[i
])
1254 destroy_program_variants(st
, shProg
->_LinkedShaders
[i
]->Program
);
1258 case GL_VERTEX_SHADER
:
1259 case GL_FRAGMENT_SHADER
:
1260 case GL_GEOMETRY_SHADER
:
1262 destroy_program_variants(st
, shader
->Program
);
1272 * Callback for _mesa_HashWalk. Free all the program variants which match
1273 * the given context.
1276 destroy_program_variants_cb(GLuint key
, void *data
, void *userData
)
1278 struct st_context
*st
= (struct st_context
*) userData
;
1279 struct gl_program
*program
= (struct gl_program
*) data
;
1280 destroy_program_variants(st
, program
);
1285 * Walk over all shaders and programs to delete any variants which
1286 * belong to the given context.
1287 * This is called during context tear-down.
1290 st_destroy_program_variants(struct st_context
*st
)
1292 /* ARB vert/frag program */
1293 _mesa_HashWalk(st
->ctx
->Shared
->Programs
,
1294 destroy_program_variants_cb
, st
);
1296 /* GLSL vert/frag/geom shaders */
1297 _mesa_HashWalk(st
->ctx
->Shared
->ShaderObjects
,
1298 destroy_shader_program_variants_cb
, st
);
1303 * For debugging, print/dump the current vertex program.
1306 st_print_current_vertex_program(void)
1308 GET_CURRENT_CONTEXT(ctx
);
1310 if (ctx
->VertexProgram
._Current
) {
1311 struct st_vertex_program
*stvp
=
1312 (struct st_vertex_program
*) ctx
->VertexProgram
._Current
;
1313 struct st_vp_variant
*stv
;
1315 debug_printf("Vertex program %u\n", stvp
->Base
.Base
.Id
);
1317 for (stv
= stvp
->variants
; stv
; stv
= stv
->next
) {
1318 debug_printf("variant %p\n", stv
);
1319 tgsi_dump(stv
->tgsi
.tokens
, 0);
1326 * Compile one shader variant.
1329 st_precompile_shader_variant(struct st_context
*st
,
1330 struct gl_program
*prog
)
1332 switch (prog
->Target
) {
1333 case GL_VERTEX_PROGRAM_ARB
: {
1334 struct st_vertex_program
*p
= (struct st_vertex_program
*)prog
;
1335 struct st_vp_variant_key key
;
1337 memset(&key
, 0, sizeof(key
));
1339 st_get_vp_variant(st
, p
, &key
);
1343 case GL_GEOMETRY_PROGRAM_NV
: {
1344 struct st_geometry_program
*p
= (struct st_geometry_program
*)prog
;
1345 struct st_gp_variant_key key
;
1347 memset(&key
, 0, sizeof(key
));
1349 st_get_gp_variant(st
, p
, &key
);
1353 case GL_FRAGMENT_PROGRAM_ARB
: {
1354 struct st_fragment_program
*p
= (struct st_fragment_program
*)prog
;
1355 struct st_fp_variant_key key
;
1357 memset(&key
, 0, sizeof(key
));
1359 st_get_fp_variant(st
, p
, &key
);