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
);
188 if (!stvp
->glsl_to_tgsi
)
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
& BITFIELD64_BIT(attr
)) != 0) {
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
< VARYING_SLOT_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 VARYING_SLOT_POS
:
219 stvp
->output_semantic_name
[slot
] = TGSI_SEMANTIC_POSITION
;
220 stvp
->output_semantic_index
[slot
] = 0;
222 case VARYING_SLOT_COL0
:
223 stvp
->output_semantic_name
[slot
] = TGSI_SEMANTIC_COLOR
;
224 stvp
->output_semantic_index
[slot
] = 0;
226 case VARYING_SLOT_COL1
:
227 stvp
->output_semantic_name
[slot
] = TGSI_SEMANTIC_COLOR
;
228 stvp
->output_semantic_index
[slot
] = 1;
230 case VARYING_SLOT_BFC0
:
231 stvp
->output_semantic_name
[slot
] = TGSI_SEMANTIC_BCOLOR
;
232 stvp
->output_semantic_index
[slot
] = 0;
234 case VARYING_SLOT_BFC1
:
235 stvp
->output_semantic_name
[slot
] = TGSI_SEMANTIC_BCOLOR
;
236 stvp
->output_semantic_index
[slot
] = 1;
238 case VARYING_SLOT_FOGC
:
239 stvp
->output_semantic_name
[slot
] = TGSI_SEMANTIC_FOG
;
240 stvp
->output_semantic_index
[slot
] = 0;
242 case VARYING_SLOT_PSIZ
:
243 stvp
->output_semantic_name
[slot
] = TGSI_SEMANTIC_PSIZE
;
244 stvp
->output_semantic_index
[slot
] = 0;
246 case VARYING_SLOT_CLIP_DIST0
:
247 stvp
->output_semantic_name
[slot
] = TGSI_SEMANTIC_CLIPDIST
;
248 stvp
->output_semantic_index
[slot
] = 0;
250 case VARYING_SLOT_CLIP_DIST1
:
251 stvp
->output_semantic_name
[slot
] = TGSI_SEMANTIC_CLIPDIST
;
252 stvp
->output_semantic_index
[slot
] = 1;
254 case VARYING_SLOT_EDGE
:
257 case VARYING_SLOT_CLIP_VERTEX
:
258 stvp
->output_semantic_name
[slot
] = TGSI_SEMANTIC_CLIPVERTEX
;
259 stvp
->output_semantic_index
[slot
] = 0;
261 case VARYING_SLOT_LAYER
:
262 stvp
->output_semantic_name
[slot
] = TGSI_SEMANTIC_LAYER
;
263 stvp
->output_semantic_index
[slot
] = 0;
265 case VARYING_SLOT_VIEWPORT
:
266 stvp
->output_semantic_name
[slot
] = TGSI_SEMANTIC_VIEWPORT_INDEX
;
267 stvp
->output_semantic_index
[slot
] = 0;
270 case VARYING_SLOT_TEX0
:
271 case VARYING_SLOT_TEX1
:
272 case VARYING_SLOT_TEX2
:
273 case VARYING_SLOT_TEX3
:
274 case VARYING_SLOT_TEX4
:
275 case VARYING_SLOT_TEX5
:
276 case VARYING_SLOT_TEX6
:
277 case VARYING_SLOT_TEX7
:
278 if (st
->needs_texcoord_semantic
) {
279 stvp
->output_semantic_name
[slot
] = TGSI_SEMANTIC_TEXCOORD
;
280 stvp
->output_semantic_index
[slot
] = attr
- VARYING_SLOT_TEX0
;
284 case VARYING_SLOT_VAR0
:
286 assert(attr
< VARYING_SLOT_MAX
);
287 stvp
->output_semantic_name
[slot
] = TGSI_SEMANTIC_GENERIC
;
288 stvp
->output_semantic_index
[slot
] =
289 st_get_generic_varying_index(st
, attr
);
294 /* similar hack to above, presetup potentially unused edgeflag output */
295 stvp
->result_to_output
[VARYING_SLOT_EDGE
] = stvp
->num_outputs
;
296 stvp
->output_semantic_name
[stvp
->num_outputs
] = TGSI_SEMANTIC_EDGEFLAG
;
297 stvp
->output_semantic_index
[stvp
->num_outputs
] = 0;
302 * Translate a vertex program to create a new variant.
304 static struct st_vp_variant
*
305 st_translate_vertex_program(struct st_context
*st
,
306 struct st_vertex_program
*stvp
,
307 const struct st_vp_variant_key
*key
)
309 struct st_vp_variant
*vpv
= CALLOC_STRUCT(st_vp_variant
);
310 struct pipe_context
*pipe
= st
->pipe
;
311 struct ureg_program
*ureg
;
312 enum pipe_error error
;
313 unsigned num_outputs
;
315 st_prepare_vertex_program(st
->ctx
, stvp
);
317 if (!stvp
->glsl_to_tgsi
)
319 _mesa_remove_output_reads(&stvp
->Base
.Base
, PROGRAM_OUTPUT
);
322 ureg
= ureg_create( TGSI_PROCESSOR_VERTEX
);
330 vpv
->num_inputs
= stvp
->num_inputs
;
331 num_outputs
= stvp
->num_outputs
;
332 if (key
->passthrough_edgeflags
) {
337 if (ST_DEBUG
& DEBUG_MESA
) {
338 _mesa_print_program(&stvp
->Base
.Base
);
339 _mesa_print_program_parameters(st
->ctx
, &stvp
->Base
.Base
);
343 if (stvp
->glsl_to_tgsi
)
344 error
= st_translate_program(st
->ctx
,
345 TGSI_PROCESSOR_VERTEX
,
351 stvp
->input_to_index
,
352 NULL
, /* input semantic name */
353 NULL
, /* input semantic index */
354 NULL
, /* interp mode */
355 NULL
, /* interp location */
358 stvp
->result_to_output
,
359 stvp
->output_semantic_name
,
360 stvp
->output_semantic_index
,
361 key
->passthrough_edgeflags
,
364 error
= st_translate_mesa_program(st
->ctx
,
365 TGSI_PROCESSOR_VERTEX
,
370 stvp
->input_to_index
,
371 NULL
, /* input semantic name */
372 NULL
, /* input semantic index */
376 stvp
->result_to_output
,
377 stvp
->output_semantic_name
,
378 stvp
->output_semantic_index
,
379 key
->passthrough_edgeflags
,
385 vpv
->tgsi
.tokens
= ureg_get_tokens( ureg
, NULL
);
386 if (!vpv
->tgsi
.tokens
)
389 ureg_destroy( ureg
);
391 if (stvp
->glsl_to_tgsi
) {
392 st_translate_stream_output_info(stvp
->glsl_to_tgsi
,
393 stvp
->result_to_output
,
394 &vpv
->tgsi
.stream_output
);
397 if (ST_DEBUG
& DEBUG_TGSI
) {
398 tgsi_dump(vpv
->tgsi
.tokens
, 0);
402 vpv
->driver_shader
= pipe
->create_vs_state(pipe
, &vpv
->tgsi
);
406 debug_printf("%s: failed to translate Mesa program:\n", __FUNCTION__
);
407 _mesa_print_program(&stvp
->Base
.Base
);
410 ureg_destroy( ureg
);
416 * Find/create a vertex program variant.
418 struct st_vp_variant
*
419 st_get_vp_variant(struct st_context
*st
,
420 struct st_vertex_program
*stvp
,
421 const struct st_vp_variant_key
*key
)
423 struct st_vp_variant
*vpv
;
425 /* Search for existing variant */
426 for (vpv
= stvp
->variants
; vpv
; vpv
= vpv
->next
) {
427 if (memcmp(&vpv
->key
, key
, sizeof(*key
)) == 0) {
434 vpv
= st_translate_vertex_program(st
, stvp
, key
);
436 /* insert into list */
437 vpv
->next
= stvp
->variants
;
438 stvp
->variants
= vpv
;
447 st_translate_interp(enum glsl_interp_qualifier glsl_qual
, bool is_color
)
450 case INTERP_QUALIFIER_NONE
:
452 return TGSI_INTERPOLATE_COLOR
;
453 return TGSI_INTERPOLATE_PERSPECTIVE
;
454 case INTERP_QUALIFIER_SMOOTH
:
455 return TGSI_INTERPOLATE_PERSPECTIVE
;
456 case INTERP_QUALIFIER_FLAT
:
457 return TGSI_INTERPOLATE_CONSTANT
;
458 case INTERP_QUALIFIER_NOPERSPECTIVE
:
459 return TGSI_INTERPOLATE_LINEAR
;
461 assert(0 && "unexpected interp mode in st_translate_interp()");
462 return TGSI_INTERPOLATE_PERSPECTIVE
;
468 * Translate a Mesa fragment shader into a TGSI shader using extra info in
470 * \return new fragment program variant
472 static struct st_fp_variant
*
473 st_translate_fragment_program(struct st_context
*st
,
474 struct st_fragment_program
*stfp
,
475 const struct st_fp_variant_key
*key
)
477 struct pipe_context
*pipe
= st
->pipe
;
478 struct st_fp_variant
*variant
= CALLOC_STRUCT(st_fp_variant
);
479 GLboolean deleteFP
= GL_FALSE
;
481 GLuint outputMapping
[FRAG_RESULT_MAX
];
482 GLuint inputMapping
[VARYING_SLOT_MAX
];
483 GLuint interpMode
[PIPE_MAX_SHADER_INPUTS
]; /* XXX size? */
484 GLuint interpLocation
[PIPE_MAX_SHADER_INPUTS
];
486 GLbitfield64 inputsRead
;
487 struct ureg_program
*ureg
;
489 GLboolean write_all
= GL_FALSE
;
491 ubyte input_semantic_name
[PIPE_MAX_SHADER_INPUTS
];
492 ubyte input_semantic_index
[PIPE_MAX_SHADER_INPUTS
];
493 uint fs_num_inputs
= 0;
495 ubyte fs_output_semantic_name
[PIPE_MAX_SHADER_OUTPUTS
];
496 ubyte fs_output_semantic_index
[PIPE_MAX_SHADER_OUTPUTS
];
497 uint fs_num_outputs
= 0;
502 assert(!(key
->bitmap
&& key
->drawpixels
));
505 /* glBitmap drawing */
506 struct gl_fragment_program
*fp
; /* we free this temp program below */
508 st_make_bitmap_fragment_program(st
, &stfp
->Base
,
509 &fp
, &variant
->bitmap_sampler
);
511 variant
->parameters
= _mesa_clone_parameter_list(fp
->Base
.Parameters
);
512 stfp
= st_fragment_program(fp
);
515 else if (key
->drawpixels
) {
516 /* glDrawPixels drawing */
517 struct gl_fragment_program
*fp
; /* we free this temp program below */
519 if (key
->drawpixels_z
|| key
->drawpixels_stencil
) {
520 fp
= st_make_drawpix_z_stencil_program(st
, key
->drawpixels_z
,
521 key
->drawpixels_stencil
);
525 st_make_drawpix_fragment_program(st
, &stfp
->Base
, &fp
);
526 variant
->parameters
= _mesa_clone_parameter_list(fp
->Base
.Parameters
);
529 stfp
= st_fragment_program(fp
);
532 if (!stfp
->glsl_to_tgsi
)
533 _mesa_remove_output_reads(&stfp
->Base
.Base
, PROGRAM_OUTPUT
);
536 * Convert Mesa program inputs to TGSI input register semantics.
538 inputsRead
= stfp
->Base
.Base
.InputsRead
;
539 for (attr
= 0; attr
< VARYING_SLOT_MAX
; attr
++) {
540 if ((inputsRead
& BITFIELD64_BIT(attr
)) != 0) {
541 const GLuint slot
= fs_num_inputs
++;
543 inputMapping
[attr
] = slot
;
544 if (stfp
->Base
.IsCentroid
& BITFIELD64_BIT(attr
))
545 interpLocation
[slot
] = TGSI_INTERPOLATE_LOC_CENTROID
;
546 else if (stfp
->Base
.IsSample
& BITFIELD64_BIT(attr
))
547 interpLocation
[slot
] = TGSI_INTERPOLATE_LOC_SAMPLE
;
549 interpLocation
[slot
] = TGSI_INTERPOLATE_LOC_CENTER
;
551 if (key
->persample_shading
)
552 interpLocation
[slot
] = TGSI_INTERPOLATE_LOC_SAMPLE
;
555 case VARYING_SLOT_POS
:
556 input_semantic_name
[slot
] = TGSI_SEMANTIC_POSITION
;
557 input_semantic_index
[slot
] = 0;
558 interpMode
[slot
] = TGSI_INTERPOLATE_LINEAR
;
560 case VARYING_SLOT_COL0
:
561 input_semantic_name
[slot
] = TGSI_SEMANTIC_COLOR
;
562 input_semantic_index
[slot
] = 0;
563 interpMode
[slot
] = st_translate_interp(stfp
->Base
.InterpQualifier
[attr
],
566 case VARYING_SLOT_COL1
:
567 input_semantic_name
[slot
] = TGSI_SEMANTIC_COLOR
;
568 input_semantic_index
[slot
] = 1;
569 interpMode
[slot
] = st_translate_interp(stfp
->Base
.InterpQualifier
[attr
],
572 case VARYING_SLOT_FOGC
:
573 input_semantic_name
[slot
] = TGSI_SEMANTIC_FOG
;
574 input_semantic_index
[slot
] = 0;
575 interpMode
[slot
] = TGSI_INTERPOLATE_PERSPECTIVE
;
577 case VARYING_SLOT_FACE
:
578 input_semantic_name
[slot
] = TGSI_SEMANTIC_FACE
;
579 input_semantic_index
[slot
] = 0;
580 interpMode
[slot
] = TGSI_INTERPOLATE_CONSTANT
;
582 case VARYING_SLOT_PRIMITIVE_ID
:
583 input_semantic_name
[slot
] = TGSI_SEMANTIC_PRIMID
;
584 input_semantic_index
[slot
] = 0;
585 interpMode
[slot
] = TGSI_INTERPOLATE_CONSTANT
;
587 case VARYING_SLOT_LAYER
:
588 input_semantic_name
[slot
] = TGSI_SEMANTIC_LAYER
;
589 input_semantic_index
[slot
] = 0;
590 interpMode
[slot
] = TGSI_INTERPOLATE_CONSTANT
;
592 case VARYING_SLOT_VIEWPORT
:
593 input_semantic_name
[slot
] = TGSI_SEMANTIC_VIEWPORT_INDEX
;
594 input_semantic_index
[slot
] = 0;
595 interpMode
[slot
] = TGSI_INTERPOLATE_CONSTANT
;
597 case VARYING_SLOT_CLIP_DIST0
:
598 input_semantic_name
[slot
] = TGSI_SEMANTIC_CLIPDIST
;
599 input_semantic_index
[slot
] = 0;
600 interpMode
[slot
] = TGSI_INTERPOLATE_PERSPECTIVE
;
602 case VARYING_SLOT_CLIP_DIST1
:
603 input_semantic_name
[slot
] = TGSI_SEMANTIC_CLIPDIST
;
604 input_semantic_index
[slot
] = 1;
605 interpMode
[slot
] = TGSI_INTERPOLATE_PERSPECTIVE
;
607 /* In most cases, there is nothing special about these
608 * inputs, so adopt a convention to use the generic
609 * semantic name and the mesa VARYING_SLOT_ number as the
612 * All that is required is that the vertex shader labels
613 * its own outputs similarly, and that the vertex shader
614 * generates at least every output required by the
615 * fragment shader plus fixed-function hardware (such as
618 * However, some drivers may need us to identify the PNTC and TEXi
619 * varyings if, for example, their capability to replace them with
620 * sprite coordinates is limited.
622 case VARYING_SLOT_PNTC
:
623 if (st
->needs_texcoord_semantic
) {
624 input_semantic_name
[slot
] = TGSI_SEMANTIC_PCOORD
;
625 input_semantic_index
[slot
] = 0;
626 interpMode
[slot
] = TGSI_INTERPOLATE_LINEAR
;
630 case VARYING_SLOT_TEX0
:
631 case VARYING_SLOT_TEX1
:
632 case VARYING_SLOT_TEX2
:
633 case VARYING_SLOT_TEX3
:
634 case VARYING_SLOT_TEX4
:
635 case VARYING_SLOT_TEX5
:
636 case VARYING_SLOT_TEX6
:
637 case VARYING_SLOT_TEX7
:
638 if (st
->needs_texcoord_semantic
) {
639 input_semantic_name
[slot
] = TGSI_SEMANTIC_TEXCOORD
;
640 input_semantic_index
[slot
] = attr
- VARYING_SLOT_TEX0
;
642 st_translate_interp(stfp
->Base
.InterpQualifier
[attr
], FALSE
);
646 case VARYING_SLOT_VAR0
:
648 /* Semantic indices should be zero-based because drivers may choose
649 * to assign a fixed slot determined by that index.
650 * This is useful because ARB_separate_shader_objects uses location
651 * qualifiers for linkage, and if the semantic index corresponds to
652 * these locations, linkage passes in the driver become unecessary.
654 * If needs_texcoord_semantic is true, no semantic indices will be
655 * consumed for the TEXi varyings, and we can base the locations of
656 * the user varyings on VAR0. Otherwise, we use TEX0 as base index.
658 assert(attr
>= VARYING_SLOT_TEX0
);
659 input_semantic_name
[slot
] = TGSI_SEMANTIC_GENERIC
;
660 input_semantic_index
[slot
] = st_get_generic_varying_index(st
, attr
);
661 if (attr
== VARYING_SLOT_PNTC
)
662 interpMode
[slot
] = TGSI_INTERPOLATE_LINEAR
;
664 interpMode
[slot
] = st_translate_interp(stfp
->Base
.InterpQualifier
[attr
],
670 inputMapping
[attr
] = -1;
675 * Semantics and mapping for outputs
679 GLbitfield64 outputsWritten
= stfp
->Base
.Base
.OutputsWritten
;
681 /* if z is written, emit that first */
682 if (outputsWritten
& BITFIELD64_BIT(FRAG_RESULT_DEPTH
)) {
683 fs_output_semantic_name
[fs_num_outputs
] = TGSI_SEMANTIC_POSITION
;
684 fs_output_semantic_index
[fs_num_outputs
] = 0;
685 outputMapping
[FRAG_RESULT_DEPTH
] = fs_num_outputs
;
687 outputsWritten
&= ~(1 << FRAG_RESULT_DEPTH
);
690 if (outputsWritten
& BITFIELD64_BIT(FRAG_RESULT_STENCIL
)) {
691 fs_output_semantic_name
[fs_num_outputs
] = TGSI_SEMANTIC_STENCIL
;
692 fs_output_semantic_index
[fs_num_outputs
] = 0;
693 outputMapping
[FRAG_RESULT_STENCIL
] = fs_num_outputs
;
695 outputsWritten
&= ~(1 << FRAG_RESULT_STENCIL
);
698 if (outputsWritten
& BITFIELD64_BIT(FRAG_RESULT_SAMPLE_MASK
)) {
699 fs_output_semantic_name
[fs_num_outputs
] = TGSI_SEMANTIC_SAMPLEMASK
;
700 fs_output_semantic_index
[fs_num_outputs
] = 0;
701 outputMapping
[FRAG_RESULT_SAMPLE_MASK
] = fs_num_outputs
;
703 outputsWritten
&= ~(1 << FRAG_RESULT_SAMPLE_MASK
);
706 /* handle remaining outputs (color) */
707 for (attr
= 0; attr
< FRAG_RESULT_MAX
; attr
++) {
708 if (outputsWritten
& BITFIELD64_BIT(attr
)) {
710 case FRAG_RESULT_DEPTH
:
711 case FRAG_RESULT_STENCIL
:
712 case FRAG_RESULT_SAMPLE_MASK
:
716 case FRAG_RESULT_COLOR
:
717 write_all
= GL_TRUE
; /* fallthrough */
719 assert(attr
== FRAG_RESULT_COLOR
||
720 (FRAG_RESULT_DATA0
<= attr
&& attr
< FRAG_RESULT_MAX
));
721 fs_output_semantic_name
[fs_num_outputs
] = TGSI_SEMANTIC_COLOR
;
722 fs_output_semantic_index
[fs_num_outputs
] = numColors
;
723 outputMapping
[attr
] = fs_num_outputs
;
733 ureg
= ureg_create( TGSI_PROCESSOR_FRAGMENT
);
739 if (ST_DEBUG
& DEBUG_MESA
) {
740 _mesa_print_program(&stfp
->Base
.Base
);
741 _mesa_print_program_parameters(st
->ctx
, &stfp
->Base
.Base
);
744 if (write_all
== GL_TRUE
)
745 ureg_property(ureg
, TGSI_PROPERTY_FS_COLOR0_WRITES_ALL_CBUFS
, 1);
747 if (stfp
->Base
.FragDepthLayout
!= FRAG_DEPTH_LAYOUT_NONE
) {
748 switch (stfp
->Base
.FragDepthLayout
) {
749 case FRAG_DEPTH_LAYOUT_ANY
:
750 ureg_property(ureg
, TGSI_PROPERTY_FS_DEPTH_LAYOUT
,
751 TGSI_FS_DEPTH_LAYOUT_ANY
);
753 case FRAG_DEPTH_LAYOUT_GREATER
:
754 ureg_property(ureg
, TGSI_PROPERTY_FS_DEPTH_LAYOUT
,
755 TGSI_FS_DEPTH_LAYOUT_GREATER
);
757 case FRAG_DEPTH_LAYOUT_LESS
:
758 ureg_property(ureg
, TGSI_PROPERTY_FS_DEPTH_LAYOUT
,
759 TGSI_FS_DEPTH_LAYOUT_LESS
);
761 case FRAG_DEPTH_LAYOUT_UNCHANGED
:
762 ureg_property(ureg
, TGSI_PROPERTY_FS_DEPTH_LAYOUT
,
763 TGSI_FS_DEPTH_LAYOUT_UNCHANGED
);
770 if (stfp
->glsl_to_tgsi
)
771 st_translate_program(st
->ctx
,
772 TGSI_PROCESSOR_FRAGMENT
,
780 input_semantic_index
,
786 fs_output_semantic_name
,
787 fs_output_semantic_index
, FALSE
,
790 st_translate_mesa_program(st
->ctx
,
791 TGSI_PROCESSOR_FRAGMENT
,
798 input_semantic_index
,
803 fs_output_semantic_name
,
804 fs_output_semantic_index
, FALSE
,
807 variant
->tgsi
.tokens
= ureg_get_tokens( ureg
, NULL
);
808 ureg_destroy( ureg
);
810 if (ST_DEBUG
& DEBUG_TGSI
) {
811 tgsi_dump(variant
->tgsi
.tokens
, 0/*TGSI_DUMP_VERBOSE*/);
815 /* fill in variant */
816 variant
->driver_shader
= pipe
->create_fs_state(pipe
, &variant
->tgsi
);
820 /* Free the temporary program made above */
821 struct gl_fragment_program
*fp
= &stfp
->Base
;
822 _mesa_reference_fragprog(st
->ctx
, &fp
, NULL
);
830 * Translate fragment program if needed.
832 struct st_fp_variant
*
833 st_get_fp_variant(struct st_context
*st
,
834 struct st_fragment_program
*stfp
,
835 const struct st_fp_variant_key
*key
)
837 struct st_fp_variant
*fpv
;
839 /* Search for existing variant */
840 for (fpv
= stfp
->variants
; fpv
; fpv
= fpv
->next
) {
841 if (memcmp(&fpv
->key
, key
, sizeof(*key
)) == 0) {
848 fpv
= st_translate_fragment_program(st
, stfp
, key
);
850 /* insert into list */
851 fpv
->next
= stfp
->variants
;
852 stfp
->variants
= fpv
;
861 * Translate a geometry program to create a new variant.
863 static struct st_gp_variant
*
864 st_translate_geometry_program(struct st_context
*st
,
865 struct st_geometry_program
*stgp
,
866 const struct st_gp_variant_key
*key
)
868 GLuint inputMapping
[VARYING_SLOT_MAX
];
869 GLuint outputMapping
[VARYING_SLOT_MAX
];
870 struct pipe_context
*pipe
= st
->pipe
;
872 GLbitfield64 inputsRead
;
874 uint gs_num_inputs
= 0;
875 uint gs_builtin_inputs
= 0;
876 uint gs_array_offset
= 0;
878 ubyte input_semantic_name
[PIPE_MAX_SHADER_INPUTS
];
879 ubyte input_semantic_index
[PIPE_MAX_SHADER_INPUTS
];
881 ubyte gs_output_semantic_name
[PIPE_MAX_SHADER_OUTPUTS
];
882 ubyte gs_output_semantic_index
[PIPE_MAX_SHADER_OUTPUTS
];
883 uint gs_num_outputs
= 0;
887 struct ureg_program
*ureg
;
888 struct pipe_shader_state state
= {0};
890 struct st_gp_variant
*gpv
;
892 gpv
= CALLOC_STRUCT(st_gp_variant
);
896 if (!stgp
->glsl_to_tgsi
) {
897 _mesa_remove_output_reads(&stgp
->Base
.Base
, PROGRAM_OUTPUT
);
900 ureg
= ureg_create( TGSI_PROCESSOR_GEOMETRY
);
906 memset(inputMapping
, 0, sizeof(inputMapping
));
907 memset(outputMapping
, 0, sizeof(outputMapping
));
910 * Convert Mesa program inputs to TGSI input register semantics.
912 inputsRead
= stgp
->Base
.Base
.InputsRead
;
913 for (attr
= 0; attr
< VARYING_SLOT_MAX
; attr
++) {
914 if ((inputsRead
& BITFIELD64_BIT(attr
)) != 0) {
915 const GLuint slot
= gs_num_inputs
;
919 inputMapping
[attr
] = slot
;
921 if (attr
!= VARYING_SLOT_PRIMITIVE_ID
) {
922 gs_array_offset
+= 2;
927 debug_printf("input map at %d = %d\n",
928 slot
+ gs_array_offset
, stgp
->input_map
[slot
+ gs_array_offset
]);
932 case VARYING_SLOT_PRIMITIVE_ID
:
933 input_semantic_name
[slot
] = TGSI_SEMANTIC_PRIMID
;
934 input_semantic_index
[slot
] = 0;
936 case VARYING_SLOT_POS
:
937 input_semantic_name
[slot
] = TGSI_SEMANTIC_POSITION
;
938 input_semantic_index
[slot
] = 0;
940 case VARYING_SLOT_COL0
:
941 input_semantic_name
[slot
] = TGSI_SEMANTIC_COLOR
;
942 input_semantic_index
[slot
] = 0;
944 case VARYING_SLOT_COL1
:
945 input_semantic_name
[slot
] = TGSI_SEMANTIC_COLOR
;
946 input_semantic_index
[slot
] = 1;
948 case VARYING_SLOT_FOGC
:
949 input_semantic_name
[slot
] = TGSI_SEMANTIC_FOG
;
950 input_semantic_index
[slot
] = 0;
952 case VARYING_SLOT_CLIP_VERTEX
:
953 input_semantic_name
[slot
] = TGSI_SEMANTIC_CLIPVERTEX
;
954 input_semantic_index
[slot
] = 0;
956 case VARYING_SLOT_CLIP_DIST0
:
957 input_semantic_name
[slot
] = TGSI_SEMANTIC_CLIPDIST
;
958 input_semantic_index
[slot
] = 0;
960 case VARYING_SLOT_CLIP_DIST1
:
961 input_semantic_name
[slot
] = TGSI_SEMANTIC_CLIPDIST
;
962 input_semantic_index
[slot
] = 1;
964 case VARYING_SLOT_PSIZ
:
965 input_semantic_name
[slot
] = TGSI_SEMANTIC_PSIZE
;
966 input_semantic_index
[slot
] = 0;
968 case VARYING_SLOT_TEX0
:
969 case VARYING_SLOT_TEX1
:
970 case VARYING_SLOT_TEX2
:
971 case VARYING_SLOT_TEX3
:
972 case VARYING_SLOT_TEX4
:
973 case VARYING_SLOT_TEX5
:
974 case VARYING_SLOT_TEX6
:
975 case VARYING_SLOT_TEX7
:
976 if (st
->needs_texcoord_semantic
) {
977 input_semantic_name
[slot
] = TGSI_SEMANTIC_TEXCOORD
;
978 input_semantic_index
[slot
] = attr
- VARYING_SLOT_TEX0
;
982 case VARYING_SLOT_VAR0
:
984 assert(attr
>= VARYING_SLOT_VAR0
&& attr
< VARYING_SLOT_MAX
);
985 input_semantic_name
[slot
] = TGSI_SEMANTIC_GENERIC
;
986 input_semantic_index
[slot
] =
987 st_get_generic_varying_index(st
, attr
);
993 /* initialize output semantics to defaults */
994 for (i
= 0; i
< PIPE_MAX_SHADER_OUTPUTS
; i
++) {
995 gs_output_semantic_name
[i
] = TGSI_SEMANTIC_GENERIC
;
996 gs_output_semantic_index
[i
] = 0;
1000 * Determine number of outputs, the (default) output register
1001 * mapping and the semantic information for each output.
1003 for (attr
= 0; attr
< VARYING_SLOT_MAX
; attr
++) {
1004 if (stgp
->Base
.Base
.OutputsWritten
& BITFIELD64_BIT(attr
)) {
1007 slot
= gs_num_outputs
;
1009 outputMapping
[attr
] = slot
;
1012 case VARYING_SLOT_POS
:
1014 gs_output_semantic_name
[slot
] = TGSI_SEMANTIC_POSITION
;
1015 gs_output_semantic_index
[slot
] = 0;
1017 case VARYING_SLOT_COL0
:
1018 gs_output_semantic_name
[slot
] = TGSI_SEMANTIC_COLOR
;
1019 gs_output_semantic_index
[slot
] = 0;
1021 case VARYING_SLOT_COL1
:
1022 gs_output_semantic_name
[slot
] = TGSI_SEMANTIC_COLOR
;
1023 gs_output_semantic_index
[slot
] = 1;
1025 case VARYING_SLOT_BFC0
:
1026 gs_output_semantic_name
[slot
] = TGSI_SEMANTIC_BCOLOR
;
1027 gs_output_semantic_index
[slot
] = 0;
1029 case VARYING_SLOT_BFC1
:
1030 gs_output_semantic_name
[slot
] = TGSI_SEMANTIC_BCOLOR
;
1031 gs_output_semantic_index
[slot
] = 1;
1033 case VARYING_SLOT_FOGC
:
1034 gs_output_semantic_name
[slot
] = TGSI_SEMANTIC_FOG
;
1035 gs_output_semantic_index
[slot
] = 0;
1037 case VARYING_SLOT_PSIZ
:
1038 gs_output_semantic_name
[slot
] = TGSI_SEMANTIC_PSIZE
;
1039 gs_output_semantic_index
[slot
] = 0;
1041 case VARYING_SLOT_CLIP_VERTEX
:
1042 gs_output_semantic_name
[slot
] = TGSI_SEMANTIC_CLIPVERTEX
;
1043 gs_output_semantic_index
[slot
] = 0;
1045 case VARYING_SLOT_CLIP_DIST0
:
1046 gs_output_semantic_name
[slot
] = TGSI_SEMANTIC_CLIPDIST
;
1047 gs_output_semantic_index
[slot
] = 0;
1049 case VARYING_SLOT_CLIP_DIST1
:
1050 gs_output_semantic_name
[slot
] = TGSI_SEMANTIC_CLIPDIST
;
1051 gs_output_semantic_index
[slot
] = 1;
1053 case VARYING_SLOT_LAYER
:
1054 gs_output_semantic_name
[slot
] = TGSI_SEMANTIC_LAYER
;
1055 gs_output_semantic_index
[slot
] = 0;
1057 case VARYING_SLOT_PRIMITIVE_ID
:
1058 gs_output_semantic_name
[slot
] = TGSI_SEMANTIC_PRIMID
;
1059 gs_output_semantic_index
[slot
] = 0;
1061 case VARYING_SLOT_VIEWPORT
:
1062 gs_output_semantic_name
[slot
] = TGSI_SEMANTIC_VIEWPORT_INDEX
;
1063 gs_output_semantic_index
[slot
] = 0;
1065 case VARYING_SLOT_TEX0
:
1066 case VARYING_SLOT_TEX1
:
1067 case VARYING_SLOT_TEX2
:
1068 case VARYING_SLOT_TEX3
:
1069 case VARYING_SLOT_TEX4
:
1070 case VARYING_SLOT_TEX5
:
1071 case VARYING_SLOT_TEX6
:
1072 case VARYING_SLOT_TEX7
:
1073 if (st
->needs_texcoord_semantic
) {
1074 gs_output_semantic_name
[slot
] = TGSI_SEMANTIC_TEXCOORD
;
1075 gs_output_semantic_index
[slot
] = attr
- VARYING_SLOT_TEX0
;
1079 case VARYING_SLOT_VAR0
:
1081 assert(slot
< Elements(gs_output_semantic_name
));
1082 assert(attr
>= VARYING_SLOT_VAR0
);
1083 gs_output_semantic_name
[slot
] = TGSI_SEMANTIC_GENERIC
;
1084 gs_output_semantic_index
[slot
] =
1085 st_get_generic_varying_index(st
, attr
);
1091 /* find max output slot referenced to compute gs_num_outputs */
1092 for (attr
= 0; attr
< VARYING_SLOT_MAX
; attr
++) {
1093 if (outputMapping
[attr
] != ~0U && outputMapping
[attr
] > maxSlot
)
1094 maxSlot
= outputMapping
[attr
];
1096 gs_num_outputs
= maxSlot
+ 1;
1101 printf("outputMapping? %d\n", outputMapping
? 1 : 0);
1102 if (outputMapping
) {
1103 printf("attr -> slot\n");
1104 for (i
= 0; i
< 16; i
++) {
1105 printf(" %2d %3d\n", i
, outputMapping
[i
]);
1108 printf("slot sem_name sem_index\n");
1109 for (i
= 0; i
< gs_num_outputs
; i
++) {
1110 printf(" %2d %d %d\n",
1112 gs_output_semantic_name
[i
],
1113 gs_output_semantic_index
[i
]);
1118 ureg_property(ureg
, TGSI_PROPERTY_GS_INPUT_PRIM
, stgp
->Base
.InputType
);
1119 ureg_property(ureg
, TGSI_PROPERTY_GS_OUTPUT_PRIM
, stgp
->Base
.OutputType
);
1120 ureg_property(ureg
, TGSI_PROPERTY_GS_MAX_OUTPUT_VERTICES
,
1121 stgp
->Base
.VerticesOut
);
1122 ureg_property(ureg
, TGSI_PROPERTY_GS_INVOCATIONS
, stgp
->Base
.Invocations
);
1124 if (stgp
->glsl_to_tgsi
)
1125 st_translate_program(st
->ctx
,
1126 TGSI_PROCESSOR_GEOMETRY
,
1133 input_semantic_name
,
1134 input_semantic_index
,
1140 gs_output_semantic_name
,
1141 gs_output_semantic_index
,
1145 st_translate_mesa_program(st
->ctx
,
1146 TGSI_PROCESSOR_GEOMETRY
,
1152 input_semantic_name
,
1153 input_semantic_index
,
1158 gs_output_semantic_name
,
1159 gs_output_semantic_index
,
1163 state
.tokens
= ureg_get_tokens( ureg
, NULL
);
1164 ureg_destroy( ureg
);
1166 if (stgp
->glsl_to_tgsi
) {
1167 st_translate_stream_output_info(stgp
->glsl_to_tgsi
,
1169 &state
.stream_output
);
1172 if ((ST_DEBUG
& DEBUG_TGSI
) && (ST_DEBUG
& DEBUG_MESA
)) {
1173 _mesa_print_program(&stgp
->Base
.Base
);
1177 if (ST_DEBUG
& DEBUG_TGSI
) {
1178 tgsi_dump(state
.tokens
, 0);
1182 /* fill in new variant */
1183 gpv
->driver_shader
= pipe
->create_gs_state(pipe
, &state
);
1186 ureg_free_tokens(state
.tokens
);
1192 * Get/create geometry program variant.
1194 struct st_gp_variant
*
1195 st_get_gp_variant(struct st_context
*st
,
1196 struct st_geometry_program
*stgp
,
1197 const struct st_gp_variant_key
*key
)
1199 struct st_gp_variant
*gpv
;
1201 /* Search for existing variant */
1202 for (gpv
= stgp
->variants
; gpv
; gpv
= gpv
->next
) {
1203 if (memcmp(&gpv
->key
, key
, sizeof(*key
)) == 0) {
1210 gpv
= st_translate_geometry_program(st
, stgp
, key
);
1212 /* insert into list */
1213 gpv
->next
= stgp
->variants
;
1214 stgp
->variants
= gpv
;
1225 * Debug- print current shader text
1228 st_print_shaders(struct gl_context
*ctx
)
1230 struct gl_shader_program
**shProg
= ctx
->_Shader
->CurrentProgram
;
1233 for (j
= 0; j
< 3; j
++) {
1236 if (shProg
[j
] == NULL
)
1239 for (i
= 0; i
< shProg
[j
]->NumShaders
; i
++) {
1240 struct gl_shader
*sh
;
1242 switch (shProg
[j
]->Shaders
[i
]->Type
) {
1243 case GL_VERTEX_SHADER
:
1244 sh
= (i
!= 0) ? NULL
: shProg
[j
]->Shaders
[i
];
1246 case GL_GEOMETRY_SHADER_ARB
:
1247 sh
= (i
!= 1) ? NULL
: shProg
[j
]->Shaders
[i
];
1249 case GL_FRAGMENT_SHADER
:
1250 sh
= (i
!= 2) ? NULL
: shProg
[j
]->Shaders
[i
];
1259 printf("GLSL shader %u of %u:\n", i
, shProg
[j
]->NumShaders
);
1260 printf("%s\n", sh
->Source
);
1268 * Vert/Geom/Frag programs have per-context variants. Free all the
1269 * variants attached to the given program which match the given context.
1272 destroy_program_variants(struct st_context
*st
, struct gl_program
*program
)
1274 if (!program
|| program
== &_mesa_DummyProgram
)
1277 switch (program
->Target
) {
1278 case GL_VERTEX_PROGRAM_ARB
:
1280 struct st_vertex_program
*stvp
= (struct st_vertex_program
*) program
;
1281 struct st_vp_variant
*vpv
, **prevPtr
= &stvp
->variants
;
1283 for (vpv
= stvp
->variants
; vpv
; ) {
1284 struct st_vp_variant
*next
= vpv
->next
;
1285 if (vpv
->key
.st
== st
) {
1286 /* unlink from list */
1288 /* destroy this variant */
1289 delete_vp_variant(st
, vpv
);
1292 prevPtr
= &vpv
->next
;
1298 case GL_FRAGMENT_PROGRAM_ARB
:
1300 struct st_fragment_program
*stfp
=
1301 (struct st_fragment_program
*) program
;
1302 struct st_fp_variant
*fpv
, **prevPtr
= &stfp
->variants
;
1304 for (fpv
= stfp
->variants
; fpv
; ) {
1305 struct st_fp_variant
*next
= fpv
->next
;
1306 if (fpv
->key
.st
== st
) {
1307 /* unlink from list */
1309 /* destroy this variant */
1310 delete_fp_variant(st
, fpv
);
1313 prevPtr
= &fpv
->next
;
1319 case MESA_GEOMETRY_PROGRAM
:
1321 struct st_geometry_program
*stgp
=
1322 (struct st_geometry_program
*) program
;
1323 struct st_gp_variant
*gpv
, **prevPtr
= &stgp
->variants
;
1325 for (gpv
= stgp
->variants
; gpv
; ) {
1326 struct st_gp_variant
*next
= gpv
->next
;
1327 if (gpv
->key
.st
== st
) {
1328 /* unlink from list */
1330 /* destroy this variant */
1331 delete_gp_variant(st
, gpv
);
1334 prevPtr
= &gpv
->next
;
1341 _mesa_problem(NULL
, "Unexpected program target 0x%x in "
1342 "destroy_program_variants_cb()", program
->Target
);
1348 * Callback for _mesa_HashWalk. Free all the shader's program variants
1349 * which match the given context.
1352 destroy_shader_program_variants_cb(GLuint key
, void *data
, void *userData
)
1354 struct st_context
*st
= (struct st_context
*) userData
;
1355 struct gl_shader
*shader
= (struct gl_shader
*) data
;
1357 switch (shader
->Type
) {
1358 case GL_SHADER_PROGRAM_MESA
:
1360 struct gl_shader_program
*shProg
= (struct gl_shader_program
*) data
;
1363 for (i
= 0; i
< shProg
->NumShaders
; i
++) {
1364 destroy_program_variants(st
, shProg
->Shaders
[i
]->Program
);
1367 for (i
= 0; i
< Elements(shProg
->_LinkedShaders
); i
++) {
1368 if (shProg
->_LinkedShaders
[i
])
1369 destroy_program_variants(st
, shProg
->_LinkedShaders
[i
]->Program
);
1373 case GL_VERTEX_SHADER
:
1374 case GL_FRAGMENT_SHADER
:
1375 case GL_GEOMETRY_SHADER
:
1377 destroy_program_variants(st
, shader
->Program
);
1387 * Callback for _mesa_HashWalk. Free all the program variants which match
1388 * the given context.
1391 destroy_program_variants_cb(GLuint key
, void *data
, void *userData
)
1393 struct st_context
*st
= (struct st_context
*) userData
;
1394 struct gl_program
*program
= (struct gl_program
*) data
;
1395 destroy_program_variants(st
, program
);
1400 * Walk over all shaders and programs to delete any variants which
1401 * belong to the given context.
1402 * This is called during context tear-down.
1405 st_destroy_program_variants(struct st_context
*st
)
1407 /* ARB vert/frag program */
1408 _mesa_HashWalk(st
->ctx
->Shared
->Programs
,
1409 destroy_program_variants_cb
, st
);
1411 /* GLSL vert/frag/geom shaders */
1412 _mesa_HashWalk(st
->ctx
->Shared
->ShaderObjects
,
1413 destroy_shader_program_variants_cb
, st
);
1418 * For debugging, print/dump the current vertex program.
1421 st_print_current_vertex_program(void)
1423 GET_CURRENT_CONTEXT(ctx
);
1425 if (ctx
->VertexProgram
._Current
) {
1426 struct st_vertex_program
*stvp
=
1427 (struct st_vertex_program
*) ctx
->VertexProgram
._Current
;
1428 struct st_vp_variant
*stv
;
1430 debug_printf("Vertex program %u\n", stvp
->Base
.Base
.Id
);
1432 for (stv
= stvp
->variants
; stv
; stv
= stv
->next
) {
1433 debug_printf("variant %p\n", stv
);
1434 tgsi_dump(stv
->tgsi
.tokens
, 0);