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/errors.h"
35 #include "main/imports.h"
36 #include "main/hash.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 "compiler/nir/nir.h"
44 #include "pipe/p_context.h"
45 #include "pipe/p_defines.h"
46 #include "pipe/p_shader_tokens.h"
47 #include "draw/draw_context.h"
48 #include "tgsi/tgsi_dump.h"
49 #include "tgsi/tgsi_emulate.h"
50 #include "tgsi/tgsi_parse.h"
51 #include "tgsi/tgsi_ureg.h"
54 #include "st_cb_bitmap.h"
55 #include "st_cb_drawpixels.h"
56 #include "st_context.h"
57 #include "st_tgsi_lower_yuv.h"
58 #include "st_program.h"
59 #include "st_mesa_to_tgsi.h"
60 #include "st_atifs_to_tgsi.h"
62 #include "st_shader_cache.h"
63 #include "cso_cache/cso_context.h"
68 set_affected_state_flags(uint64_t *states
,
69 struct gl_program
*prog
,
70 uint64_t new_constants
,
71 uint64_t new_sampler_views
,
72 uint64_t new_samplers
,
78 if (prog
->Parameters
->NumParameters
)
79 *states
|= new_constants
;
81 if (prog
->info
.num_textures
)
82 *states
|= new_sampler_views
| new_samplers
;
84 if (prog
->info
.num_images
)
85 *states
|= new_images
;
87 if (prog
->info
.num_ubos
)
90 if (prog
->info
.num_ssbos
)
93 if (prog
->info
.num_abos
)
94 *states
|= new_atomics
;
98 * This determines which states will be updated when the shader is bound.
101 st_set_prog_affected_state_flags(struct gl_program
*prog
)
105 switch (prog
->info
.stage
) {
106 case MESA_SHADER_VERTEX
:
107 states
= &((struct st_vertex_program
*)prog
)->affected_states
;
109 *states
= ST_NEW_VS_STATE
|
111 ST_NEW_VERTEX_ARRAYS
;
113 set_affected_state_flags(states
, prog
,
115 ST_NEW_VS_SAMPLER_VIEWS
,
123 case MESA_SHADER_TESS_CTRL
:
124 states
= &(st_common_program(prog
))->affected_states
;
126 *states
= ST_NEW_TCS_STATE
;
128 set_affected_state_flags(states
, prog
,
129 ST_NEW_TCS_CONSTANTS
,
130 ST_NEW_TCS_SAMPLER_VIEWS
,
138 case MESA_SHADER_TESS_EVAL
:
139 states
= &(st_common_program(prog
))->affected_states
;
141 *states
= ST_NEW_TES_STATE
|
144 set_affected_state_flags(states
, prog
,
145 ST_NEW_TES_CONSTANTS
,
146 ST_NEW_TES_SAMPLER_VIEWS
,
154 case MESA_SHADER_GEOMETRY
:
155 states
= &(st_common_program(prog
))->affected_states
;
157 *states
= ST_NEW_GS_STATE
|
160 set_affected_state_flags(states
, prog
,
162 ST_NEW_GS_SAMPLER_VIEWS
,
170 case MESA_SHADER_FRAGMENT
:
171 states
= &((struct st_fragment_program
*)prog
)->affected_states
;
173 /* gl_FragCoord and glDrawPixels always use constants. */
174 *states
= ST_NEW_FS_STATE
|
175 ST_NEW_SAMPLE_SHADING
|
178 set_affected_state_flags(states
, prog
,
180 ST_NEW_FS_SAMPLER_VIEWS
,
188 case MESA_SHADER_COMPUTE
:
189 states
= &((struct st_compute_program
*)prog
)->affected_states
;
191 *states
= ST_NEW_CS_STATE
;
193 set_affected_state_flags(states
, prog
,
195 ST_NEW_CS_SAMPLER_VIEWS
,
204 unreachable("unhandled shader stage");
209 * Delete a vertex program variant. Note the caller must unlink
210 * the variant from the linked list.
213 delete_vp_variant(struct st_context
*st
, struct st_vp_variant
*vpv
)
215 if (vpv
->driver_shader
)
216 cso_delete_vertex_shader(st
->cso_context
, vpv
->driver_shader
);
218 if (vpv
->draw_shader
)
219 draw_delete_vertex_shader( st
->draw
, vpv
->draw_shader
);
221 if (((vpv
->tgsi
.type
== PIPE_SHADER_IR_TGSI
)) && vpv
->tgsi
.tokens
)
222 ureg_free_tokens(vpv
->tgsi
.tokens
);
230 * Clean out any old compilations:
233 st_release_vp_variants( struct st_context
*st
,
234 struct st_vertex_program
*stvp
)
236 struct st_vp_variant
*vpv
;
238 for (vpv
= stvp
->variants
; vpv
; ) {
239 struct st_vp_variant
*next
= vpv
->next
;
240 delete_vp_variant(st
, vpv
);
244 stvp
->variants
= NULL
;
246 if ((stvp
->tgsi
.type
== PIPE_SHADER_IR_TGSI
) && stvp
->tgsi
.tokens
) {
247 tgsi_free_tokens(stvp
->tgsi
.tokens
);
248 stvp
->tgsi
.tokens
= NULL
;
255 * Delete a fragment program variant. Note the caller must unlink
256 * the variant from the linked list.
259 delete_fp_variant(struct st_context
*st
, struct st_fp_variant
*fpv
)
261 if (fpv
->driver_shader
)
262 cso_delete_fragment_shader(st
->cso_context
, fpv
->driver_shader
);
268 * Free all variants of a fragment program.
271 st_release_fp_variants(struct st_context
*st
, struct st_fragment_program
*stfp
)
273 struct st_fp_variant
*fpv
;
275 for (fpv
= stfp
->variants
; fpv
; ) {
276 struct st_fp_variant
*next
= fpv
->next
;
277 delete_fp_variant(st
, fpv
);
281 stfp
->variants
= NULL
;
283 if ((stfp
->tgsi
.type
== PIPE_SHADER_IR_TGSI
) && stfp
->tgsi
.tokens
) {
284 ureg_free_tokens(stfp
->tgsi
.tokens
);
285 stfp
->tgsi
.tokens
= NULL
;
291 * Delete a basic program variant. Note the caller must unlink
292 * the variant from the linked list.
295 delete_basic_variant(struct st_context
*st
, struct st_basic_variant
*v
,
298 if (v
->driver_shader
) {
300 case GL_TESS_CONTROL_PROGRAM_NV
:
301 cso_delete_tessctrl_shader(st
->cso_context
, v
->driver_shader
);
303 case GL_TESS_EVALUATION_PROGRAM_NV
:
304 cso_delete_tesseval_shader(st
->cso_context
, v
->driver_shader
);
306 case GL_GEOMETRY_PROGRAM_NV
:
307 cso_delete_geometry_shader(st
->cso_context
, v
->driver_shader
);
309 case GL_COMPUTE_PROGRAM_NV
:
310 cso_delete_compute_shader(st
->cso_context
, v
->driver_shader
);
313 assert(!"this shouldn't occur");
322 * Free all basic program variants.
325 st_release_basic_variants(struct st_context
*st
, GLenum target
,
326 struct st_basic_variant
**variants
,
327 struct pipe_shader_state
*tgsi
)
329 struct st_basic_variant
*v
;
331 for (v
= *variants
; v
; ) {
332 struct st_basic_variant
*next
= v
->next
;
333 delete_basic_variant(st
, v
, target
);
340 ureg_free_tokens(tgsi
->tokens
);
347 * Free all variants of a compute program.
350 st_release_cp_variants(struct st_context
*st
, struct st_compute_program
*stcp
)
352 struct st_basic_variant
**variants
= &stcp
->variants
;
353 struct st_basic_variant
*v
;
355 for (v
= *variants
; v
; ) {
356 struct st_basic_variant
*next
= v
->next
;
357 delete_basic_variant(st
, v
, stcp
->Base
.Target
);
363 if (stcp
->tgsi
.prog
) {
364 switch (stcp
->tgsi
.ir_type
) {
365 case PIPE_SHADER_IR_TGSI
:
366 ureg_free_tokens(stcp
->tgsi
.prog
);
367 stcp
->tgsi
.prog
= NULL
;
369 case PIPE_SHADER_IR_NIR
:
370 /* pipe driver took ownership of prog */
372 case PIPE_SHADER_IR_NATIVE
:
374 stcp
->tgsi
.prog
= NULL
;
381 * Translate a vertex program.
384 st_translate_vertex_program(struct st_context
*st
,
385 struct st_vertex_program
*stvp
)
387 struct ureg_program
*ureg
;
388 enum pipe_error error
;
389 unsigned num_outputs
= 0;
391 ubyte output_semantic_name
[VARYING_SLOT_MAX
] = {0};
392 ubyte output_semantic_index
[VARYING_SLOT_MAX
] = {0};
394 stvp
->num_inputs
= 0;
395 memset(stvp
->input_to_index
, ~0, sizeof(stvp
->input_to_index
));
397 if (stvp
->Base
.arb
.IsPositionInvariant
)
398 _mesa_insert_mvp_code(st
->ctx
, &stvp
->Base
);
401 * Determine number of inputs, the mappings between VERT_ATTRIB_x
402 * and TGSI generic input indexes, plus input attrib semantic info.
404 for (attr
= 0; attr
< VERT_ATTRIB_MAX
; attr
++) {
405 if ((stvp
->Base
.info
.inputs_read
& BITFIELD64_BIT(attr
)) != 0) {
406 stvp
->input_to_index
[attr
] = stvp
->num_inputs
;
407 stvp
->index_to_input
[stvp
->num_inputs
] = attr
;
409 if ((stvp
->Base
.info
.vs
.double_inputs_read
&
410 BITFIELD64_BIT(attr
)) != 0) {
411 /* add placeholder for second part of a double attribute */
412 stvp
->index_to_input
[stvp
->num_inputs
] = ST_DOUBLE_ATTRIB_PLACEHOLDER
;
417 /* bit of a hack, presetup potentially unused edgeflag input */
418 stvp
->input_to_index
[VERT_ATTRIB_EDGEFLAG
] = stvp
->num_inputs
;
419 stvp
->index_to_input
[stvp
->num_inputs
] = VERT_ATTRIB_EDGEFLAG
;
421 /* Compute mapping of vertex program outputs to slots.
423 for (attr
= 0; attr
< VARYING_SLOT_MAX
; attr
++) {
424 if ((stvp
->Base
.info
.outputs_written
& BITFIELD64_BIT(attr
)) == 0) {
425 stvp
->result_to_output
[attr
] = ~0;
428 unsigned slot
= num_outputs
++;
430 stvp
->result_to_output
[attr
] = slot
;
432 unsigned semantic_name
, semantic_index
;
433 tgsi_get_gl_varying_semantic(attr
, st
->needs_texcoord_semantic
,
434 &semantic_name
, &semantic_index
);
435 output_semantic_name
[slot
] = semantic_name
;
436 output_semantic_index
[slot
] = semantic_index
;
439 /* similar hack to above, presetup potentially unused edgeflag output */
440 stvp
->result_to_output
[VARYING_SLOT_EDGE
] = num_outputs
;
441 output_semantic_name
[num_outputs
] = TGSI_SEMANTIC_EDGEFLAG
;
442 output_semantic_index
[num_outputs
] = 0;
445 if (!stvp
->glsl_to_tgsi
&& !stvp
->shader_program
) {
446 _mesa_remove_output_reads(&stvp
->Base
, PROGRAM_OUTPUT
);
448 /* This determines which states will be updated when the assembly
451 stvp
->affected_states
= ST_NEW_VS_STATE
|
453 ST_NEW_VERTEX_ARRAYS
;
455 if (stvp
->Base
.Parameters
->NumParameters
)
456 stvp
->affected_states
|= ST_NEW_VS_CONSTANTS
;
458 /* No samplers are allowed in ARB_vp. */
461 if (stvp
->shader_program
) {
462 struct gl_program
*prog
= stvp
->shader_program
->last_vert_prog
;
464 st_translate_stream_output_info2(prog
->sh
.LinkedTransformFeedback
,
465 stvp
->result_to_output
,
466 &stvp
->tgsi
.stream_output
);
469 st_store_ir_in_disk_cache(st
, &stvp
->Base
, true);
473 ureg
= ureg_create_with_screen(PIPE_SHADER_VERTEX
, st
->pipe
->screen
);
477 if (stvp
->Base
.info
.clip_distance_array_size
)
478 ureg_property(ureg
, TGSI_PROPERTY_NUM_CLIPDIST_ENABLED
,
479 stvp
->Base
.info
.clip_distance_array_size
);
480 if (stvp
->Base
.info
.cull_distance_array_size
)
481 ureg_property(ureg
, TGSI_PROPERTY_NUM_CULLDIST_ENABLED
,
482 stvp
->Base
.info
.cull_distance_array_size
);
484 if (ST_DEBUG
& DEBUG_MESA
) {
485 _mesa_print_program(&stvp
->Base
);
486 _mesa_print_program_parameters(st
->ctx
, &stvp
->Base
);
490 if (stvp
->glsl_to_tgsi
) {
491 error
= st_translate_program(st
->ctx
,
498 stvp
->input_to_index
,
499 NULL
, /* inputSlotToAttr */
500 NULL
, /* input semantic name */
501 NULL
, /* input semantic index */
502 NULL
, /* interp mode */
505 stvp
->result_to_output
,
506 output_semantic_name
,
507 output_semantic_index
);
509 st_translate_stream_output_info(stvp
->glsl_to_tgsi
,
510 stvp
->result_to_output
,
511 &stvp
->tgsi
.stream_output
);
513 free_glsl_to_tgsi_visitor(stvp
->glsl_to_tgsi
);
515 error
= st_translate_mesa_program(st
->ctx
,
521 stvp
->input_to_index
,
522 NULL
, /* input semantic name */
523 NULL
, /* input semantic index */
527 stvp
->result_to_output
,
528 output_semantic_name
,
529 output_semantic_index
);
532 debug_printf("%s: failed to translate Mesa program:\n", __func__
);
533 _mesa_print_program(&stvp
->Base
);
538 stvp
->tgsi
.tokens
= ureg_get_tokens(ureg
, &stvp
->num_tgsi_tokens
);
541 if (stvp
->glsl_to_tgsi
) {
542 stvp
->glsl_to_tgsi
= NULL
;
543 st_store_ir_in_disk_cache(st
, &stvp
->Base
, false);
546 return stvp
->tgsi
.tokens
!= NULL
;
549 static struct st_vp_variant
*
550 st_create_vp_variant(struct st_context
*st
,
551 struct st_vertex_program
*stvp
,
552 const struct st_vp_variant_key
*key
)
554 struct st_vp_variant
*vpv
= CALLOC_STRUCT(st_vp_variant
);
555 struct pipe_context
*pipe
= st
->pipe
;
558 vpv
->tgsi
.stream_output
= stvp
->tgsi
.stream_output
;
559 vpv
->num_inputs
= stvp
->num_inputs
;
561 if (stvp
->tgsi
.type
== PIPE_SHADER_IR_NIR
) {
562 vpv
->tgsi
.type
= PIPE_SHADER_IR_NIR
;
563 vpv
->tgsi
.ir
.nir
= nir_shader_clone(NULL
, stvp
->tgsi
.ir
.nir
);
564 if (key
->clamp_color
)
565 NIR_PASS_V(vpv
->tgsi
.ir
.nir
, nir_lower_clamp_color_outputs
);
566 if (key
->passthrough_edgeflags
) {
567 NIR_PASS_V(vpv
->tgsi
.ir
.nir
, nir_lower_passthrough_edgeflags
);
571 st_finalize_nir(st
, &stvp
->Base
, stvp
->shader_program
,
574 vpv
->driver_shader
= pipe
->create_vs_state(pipe
, &vpv
->tgsi
);
575 /* driver takes ownership of IR: */
576 vpv
->tgsi
.ir
.nir
= NULL
;
580 vpv
->tgsi
.tokens
= tgsi_dup_tokens(stvp
->tgsi
.tokens
);
582 /* Emulate features. */
583 if (key
->clamp_color
|| key
->passthrough_edgeflags
) {
584 const struct tgsi_token
*tokens
;
586 (key
->clamp_color
? TGSI_EMU_CLAMP_COLOR_OUTPUTS
: 0) |
587 (key
->passthrough_edgeflags
? TGSI_EMU_PASSTHROUGH_EDGEFLAG
: 0);
589 tokens
= tgsi_emulate(vpv
->tgsi
.tokens
, flags
);
592 tgsi_free_tokens(vpv
->tgsi
.tokens
);
593 vpv
->tgsi
.tokens
= tokens
;
595 if (key
->passthrough_edgeflags
)
598 fprintf(stderr
, "mesa: cannot emulate deprecated features\n");
601 if (ST_DEBUG
& DEBUG_TGSI
) {
602 tgsi_dump(vpv
->tgsi
.tokens
, 0);
606 vpv
->driver_shader
= pipe
->create_vs_state(pipe
, &vpv
->tgsi
);
612 * Find/create a vertex program variant.
614 struct st_vp_variant
*
615 st_get_vp_variant(struct st_context
*st
,
616 struct st_vertex_program
*stvp
,
617 const struct st_vp_variant_key
*key
)
619 struct st_vp_variant
*vpv
;
621 /* Search for existing variant */
622 for (vpv
= stvp
->variants
; vpv
; vpv
= vpv
->next
) {
623 if (memcmp(&vpv
->key
, key
, sizeof(*key
)) == 0) {
630 vpv
= st_create_vp_variant(st
, stvp
, key
);
632 for (unsigned index
= 0; index
< vpv
->num_inputs
; ++index
) {
633 unsigned attr
= stvp
->index_to_input
[index
];
634 if (attr
== ST_DOUBLE_ATTRIB_PLACEHOLDER
)
636 vpv
->vert_attrib_mask
|= 1u << attr
;
639 /* insert into list */
640 vpv
->next
= stvp
->variants
;
641 stvp
->variants
= vpv
;
650 * Translate a Mesa fragment shader into a TGSI shader.
653 st_translate_fragment_program(struct st_context
*st
,
654 struct st_fragment_program
*stfp
)
656 /* We have already compiled to NIR so just return */
657 if (stfp
->shader_program
) {
658 st_store_ir_in_disk_cache(st
, &stfp
->Base
, true);
662 ubyte outputMapping
[2 * FRAG_RESULT_MAX
];
663 ubyte inputMapping
[VARYING_SLOT_MAX
];
664 ubyte inputSlotToAttr
[VARYING_SLOT_MAX
];
665 ubyte interpMode
[PIPE_MAX_SHADER_INPUTS
]; /* XXX size? */
667 GLbitfield64 inputsRead
;
668 struct ureg_program
*ureg
;
670 GLboolean write_all
= GL_FALSE
;
672 ubyte input_semantic_name
[PIPE_MAX_SHADER_INPUTS
];
673 ubyte input_semantic_index
[PIPE_MAX_SHADER_INPUTS
];
674 uint fs_num_inputs
= 0;
676 ubyte fs_output_semantic_name
[PIPE_MAX_SHADER_OUTPUTS
];
677 ubyte fs_output_semantic_index
[PIPE_MAX_SHADER_OUTPUTS
];
678 uint fs_num_outputs
= 0;
680 memset(inputSlotToAttr
, ~0, sizeof(inputSlotToAttr
));
682 /* Non-GLSL programs: */
683 if (!stfp
->glsl_to_tgsi
&& !stfp
->shader_program
) {
684 _mesa_remove_output_reads(&stfp
->Base
, PROGRAM_OUTPUT
);
685 if (st
->ctx
->Const
.GLSLFragCoordIsSysVal
)
686 _mesa_program_fragment_position_to_sysval(&stfp
->Base
);
688 /* This determines which states will be updated when the assembly
691 * fragment.position and glDrawPixels always use constants.
693 stfp
->affected_states
= ST_NEW_FS_STATE
|
694 ST_NEW_SAMPLE_SHADING
|
698 /* Just set them for ATI_fs unconditionally. */
699 stfp
->affected_states
|= ST_NEW_FS_SAMPLER_VIEWS
|
703 if (stfp
->Base
.SamplersUsed
)
704 stfp
->affected_states
|= ST_NEW_FS_SAMPLER_VIEWS
|
710 * Convert Mesa program inputs to TGSI input register semantics.
712 inputsRead
= stfp
->Base
.info
.inputs_read
;
713 for (attr
= 0; attr
< VARYING_SLOT_MAX
; attr
++) {
714 if ((inputsRead
& BITFIELD64_BIT(attr
)) != 0) {
715 const GLuint slot
= fs_num_inputs
++;
717 inputMapping
[attr
] = slot
;
718 inputSlotToAttr
[slot
] = attr
;
721 case VARYING_SLOT_POS
:
722 input_semantic_name
[slot
] = TGSI_SEMANTIC_POSITION
;
723 input_semantic_index
[slot
] = 0;
724 interpMode
[slot
] = TGSI_INTERPOLATE_LINEAR
;
726 case VARYING_SLOT_COL0
:
727 input_semantic_name
[slot
] = TGSI_SEMANTIC_COLOR
;
728 input_semantic_index
[slot
] = 0;
729 interpMode
[slot
] = stfp
->glsl_to_tgsi
?
730 TGSI_INTERPOLATE_COUNT
: TGSI_INTERPOLATE_COLOR
;
732 case VARYING_SLOT_COL1
:
733 input_semantic_name
[slot
] = TGSI_SEMANTIC_COLOR
;
734 input_semantic_index
[slot
] = 1;
735 interpMode
[slot
] = stfp
->glsl_to_tgsi
?
736 TGSI_INTERPOLATE_COUNT
: TGSI_INTERPOLATE_COLOR
;
738 case VARYING_SLOT_FOGC
:
739 input_semantic_name
[slot
] = TGSI_SEMANTIC_FOG
;
740 input_semantic_index
[slot
] = 0;
741 interpMode
[slot
] = TGSI_INTERPOLATE_PERSPECTIVE
;
743 case VARYING_SLOT_FACE
:
744 input_semantic_name
[slot
] = TGSI_SEMANTIC_FACE
;
745 input_semantic_index
[slot
] = 0;
746 interpMode
[slot
] = TGSI_INTERPOLATE_CONSTANT
;
748 case VARYING_SLOT_PRIMITIVE_ID
:
749 input_semantic_name
[slot
] = TGSI_SEMANTIC_PRIMID
;
750 input_semantic_index
[slot
] = 0;
751 interpMode
[slot
] = TGSI_INTERPOLATE_CONSTANT
;
753 case VARYING_SLOT_LAYER
:
754 input_semantic_name
[slot
] = TGSI_SEMANTIC_LAYER
;
755 input_semantic_index
[slot
] = 0;
756 interpMode
[slot
] = TGSI_INTERPOLATE_CONSTANT
;
758 case VARYING_SLOT_VIEWPORT
:
759 input_semantic_name
[slot
] = TGSI_SEMANTIC_VIEWPORT_INDEX
;
760 input_semantic_index
[slot
] = 0;
761 interpMode
[slot
] = TGSI_INTERPOLATE_CONSTANT
;
763 case VARYING_SLOT_CLIP_DIST0
:
764 input_semantic_name
[slot
] = TGSI_SEMANTIC_CLIPDIST
;
765 input_semantic_index
[slot
] = 0;
766 interpMode
[slot
] = TGSI_INTERPOLATE_PERSPECTIVE
;
768 case VARYING_SLOT_CLIP_DIST1
:
769 input_semantic_name
[slot
] = TGSI_SEMANTIC_CLIPDIST
;
770 input_semantic_index
[slot
] = 1;
771 interpMode
[slot
] = TGSI_INTERPOLATE_PERSPECTIVE
;
773 case VARYING_SLOT_CULL_DIST0
:
774 case VARYING_SLOT_CULL_DIST1
:
775 /* these should have been lowered by GLSL */
778 /* In most cases, there is nothing special about these
779 * inputs, so adopt a convention to use the generic
780 * semantic name and the mesa VARYING_SLOT_ number as the
783 * All that is required is that the vertex shader labels
784 * its own outputs similarly, and that the vertex shader
785 * generates at least every output required by the
786 * fragment shader plus fixed-function hardware (such as
789 * However, some drivers may need us to identify the PNTC and TEXi
790 * varyings if, for example, their capability to replace them with
791 * sprite coordinates is limited.
793 case VARYING_SLOT_PNTC
:
794 if (st
->needs_texcoord_semantic
) {
795 input_semantic_name
[slot
] = TGSI_SEMANTIC_PCOORD
;
796 input_semantic_index
[slot
] = 0;
797 interpMode
[slot
] = TGSI_INTERPOLATE_LINEAR
;
801 case VARYING_SLOT_TEX0
:
802 case VARYING_SLOT_TEX1
:
803 case VARYING_SLOT_TEX2
:
804 case VARYING_SLOT_TEX3
:
805 case VARYING_SLOT_TEX4
:
806 case VARYING_SLOT_TEX5
:
807 case VARYING_SLOT_TEX6
:
808 case VARYING_SLOT_TEX7
:
809 if (st
->needs_texcoord_semantic
) {
810 input_semantic_name
[slot
] = TGSI_SEMANTIC_TEXCOORD
;
811 input_semantic_index
[slot
] = attr
- VARYING_SLOT_TEX0
;
812 interpMode
[slot
] = stfp
->glsl_to_tgsi
?
813 TGSI_INTERPOLATE_COUNT
: TGSI_INTERPOLATE_PERSPECTIVE
;
817 case VARYING_SLOT_VAR0
:
819 /* Semantic indices should be zero-based because drivers may choose
820 * to assign a fixed slot determined by that index.
821 * This is useful because ARB_separate_shader_objects uses location
822 * qualifiers for linkage, and if the semantic index corresponds to
823 * these locations, linkage passes in the driver become unecessary.
825 * If needs_texcoord_semantic is true, no semantic indices will be
826 * consumed for the TEXi varyings, and we can base the locations of
827 * the user varyings on VAR0. Otherwise, we use TEX0 as base index.
829 assert(attr
>= VARYING_SLOT_VAR0
|| attr
== VARYING_SLOT_PNTC
||
830 (attr
>= VARYING_SLOT_TEX0
&& attr
<= VARYING_SLOT_TEX7
));
831 input_semantic_name
[slot
] = TGSI_SEMANTIC_GENERIC
;
832 input_semantic_index
[slot
] = st_get_generic_varying_index(st
, attr
);
833 if (attr
== VARYING_SLOT_PNTC
)
834 interpMode
[slot
] = TGSI_INTERPOLATE_LINEAR
;
836 interpMode
[slot
] = stfp
->glsl_to_tgsi
?
837 TGSI_INTERPOLATE_COUNT
: TGSI_INTERPOLATE_PERSPECTIVE
;
843 inputMapping
[attr
] = -1;
848 * Semantics and mapping for outputs
850 GLbitfield64 outputsWritten
= stfp
->Base
.info
.outputs_written
;
852 /* if z is written, emit that first */
853 if (outputsWritten
& BITFIELD64_BIT(FRAG_RESULT_DEPTH
)) {
854 fs_output_semantic_name
[fs_num_outputs
] = TGSI_SEMANTIC_POSITION
;
855 fs_output_semantic_index
[fs_num_outputs
] = 0;
856 outputMapping
[FRAG_RESULT_DEPTH
] = fs_num_outputs
;
858 outputsWritten
&= ~(1 << FRAG_RESULT_DEPTH
);
861 if (outputsWritten
& BITFIELD64_BIT(FRAG_RESULT_STENCIL
)) {
862 fs_output_semantic_name
[fs_num_outputs
] = TGSI_SEMANTIC_STENCIL
;
863 fs_output_semantic_index
[fs_num_outputs
] = 0;
864 outputMapping
[FRAG_RESULT_STENCIL
] = fs_num_outputs
;
866 outputsWritten
&= ~(1 << FRAG_RESULT_STENCIL
);
869 if (outputsWritten
& BITFIELD64_BIT(FRAG_RESULT_SAMPLE_MASK
)) {
870 fs_output_semantic_name
[fs_num_outputs
] = TGSI_SEMANTIC_SAMPLEMASK
;
871 fs_output_semantic_index
[fs_num_outputs
] = 0;
872 outputMapping
[FRAG_RESULT_SAMPLE_MASK
] = fs_num_outputs
;
874 outputsWritten
&= ~(1 << FRAG_RESULT_SAMPLE_MASK
);
877 /* handle remaining outputs (color) */
878 for (attr
= 0; attr
< ARRAY_SIZE(outputMapping
); attr
++) {
879 const GLbitfield64 written
= attr
< FRAG_RESULT_MAX
? outputsWritten
:
880 stfp
->Base
.SecondaryOutputsWritten
;
881 const unsigned loc
= attr
% FRAG_RESULT_MAX
;
883 if (written
& BITFIELD64_BIT(loc
)) {
885 case FRAG_RESULT_DEPTH
:
886 case FRAG_RESULT_STENCIL
:
887 case FRAG_RESULT_SAMPLE_MASK
:
891 case FRAG_RESULT_COLOR
:
892 write_all
= GL_TRUE
; /* fallthrough */
895 assert(loc
== FRAG_RESULT_COLOR
||
896 (FRAG_RESULT_DATA0
<= loc
&& loc
< FRAG_RESULT_MAX
));
898 index
= (loc
== FRAG_RESULT_COLOR
) ? 0 : (loc
- FRAG_RESULT_DATA0
);
900 if (attr
>= FRAG_RESULT_MAX
) {
901 /* Secondary color for dual source blending. */
906 fs_output_semantic_name
[fs_num_outputs
] = TGSI_SEMANTIC_COLOR
;
907 fs_output_semantic_index
[fs_num_outputs
] = index
;
908 outputMapping
[attr
] = fs_num_outputs
;
917 ureg
= ureg_create_with_screen(PIPE_SHADER_FRAGMENT
, st
->pipe
->screen
);
921 if (ST_DEBUG
& DEBUG_MESA
) {
922 _mesa_print_program(&stfp
->Base
);
923 _mesa_print_program_parameters(st
->ctx
, &stfp
->Base
);
926 if (write_all
== GL_TRUE
)
927 ureg_property(ureg
, TGSI_PROPERTY_FS_COLOR0_WRITES_ALL_CBUFS
, 1);
929 if (stfp
->Base
.info
.fs
.depth_layout
!= FRAG_DEPTH_LAYOUT_NONE
) {
930 switch (stfp
->Base
.info
.fs
.depth_layout
) {
931 case FRAG_DEPTH_LAYOUT_ANY
:
932 ureg_property(ureg
, TGSI_PROPERTY_FS_DEPTH_LAYOUT
,
933 TGSI_FS_DEPTH_LAYOUT_ANY
);
935 case FRAG_DEPTH_LAYOUT_GREATER
:
936 ureg_property(ureg
, TGSI_PROPERTY_FS_DEPTH_LAYOUT
,
937 TGSI_FS_DEPTH_LAYOUT_GREATER
);
939 case FRAG_DEPTH_LAYOUT_LESS
:
940 ureg_property(ureg
, TGSI_PROPERTY_FS_DEPTH_LAYOUT
,
941 TGSI_FS_DEPTH_LAYOUT_LESS
);
943 case FRAG_DEPTH_LAYOUT_UNCHANGED
:
944 ureg_property(ureg
, TGSI_PROPERTY_FS_DEPTH_LAYOUT
,
945 TGSI_FS_DEPTH_LAYOUT_UNCHANGED
);
952 if (stfp
->glsl_to_tgsi
) {
953 st_translate_program(st
->ctx
,
954 PIPE_SHADER_FRAGMENT
,
963 input_semantic_index
,
968 fs_output_semantic_name
,
969 fs_output_semantic_index
);
971 free_glsl_to_tgsi_visitor(stfp
->glsl_to_tgsi
);
972 } else if (stfp
->ati_fs
)
973 st_translate_atifs_program(ureg
,
980 input_semantic_index
,
985 fs_output_semantic_name
,
986 fs_output_semantic_index
);
988 st_translate_mesa_program(st
->ctx
,
989 PIPE_SHADER_FRAGMENT
,
996 input_semantic_index
,
1001 fs_output_semantic_name
,
1002 fs_output_semantic_index
);
1004 stfp
->tgsi
.tokens
= ureg_get_tokens(ureg
, &stfp
->num_tgsi_tokens
);
1007 if (stfp
->glsl_to_tgsi
) {
1008 stfp
->glsl_to_tgsi
= NULL
;
1009 st_store_ir_in_disk_cache(st
, &stfp
->Base
, false);
1012 return stfp
->tgsi
.tokens
!= NULL
;
1015 static struct st_fp_variant
*
1016 st_create_fp_variant(struct st_context
*st
,
1017 struct st_fragment_program
*stfp
,
1018 const struct st_fp_variant_key
*key
)
1020 struct pipe_context
*pipe
= st
->pipe
;
1021 struct st_fp_variant
*variant
= CALLOC_STRUCT(st_fp_variant
);
1022 struct pipe_shader_state tgsi
= {0};
1023 struct gl_program_parameter_list
*params
= stfp
->Base
.Parameters
;
1024 static const gl_state_index16 texcoord_state
[STATE_LENGTH
] =
1025 { STATE_INTERNAL
, STATE_CURRENT_ATTRIB
, VERT_ATTRIB_TEX0
};
1026 static const gl_state_index16 scale_state
[STATE_LENGTH
] =
1027 { STATE_INTERNAL
, STATE_PT_SCALE
};
1028 static const gl_state_index16 bias_state
[STATE_LENGTH
] =
1029 { STATE_INTERNAL
, STATE_PT_BIAS
};
1034 if (stfp
->tgsi
.type
== PIPE_SHADER_IR_NIR
) {
1035 tgsi
.type
= PIPE_SHADER_IR_NIR
;
1036 tgsi
.ir
.nir
= nir_shader_clone(NULL
, stfp
->tgsi
.ir
.nir
);
1038 if (key
->clamp_color
)
1039 NIR_PASS_V(tgsi
.ir
.nir
, nir_lower_clamp_color_outputs
);
1041 if (key
->persample_shading
) {
1042 nir_shader
*shader
= tgsi
.ir
.nir
;
1043 nir_foreach_variable(var
, &shader
->inputs
)
1044 var
->data
.sample
= true;
1047 assert(!(key
->bitmap
&& key
->drawpixels
));
1051 nir_lower_bitmap_options options
= {0};
1053 variant
->bitmap_sampler
= ffs(~stfp
->Base
.SamplersUsed
) - 1;
1054 options
.sampler
= variant
->bitmap_sampler
;
1055 options
.swizzle_xxxx
= (st
->bitmap
.tex_format
== PIPE_FORMAT_L8_UNORM
);
1057 NIR_PASS_V(tgsi
.ir
.nir
, nir_lower_bitmap
, &options
);
1060 /* glDrawPixels (color only) */
1061 if (key
->drawpixels
) {
1062 nir_lower_drawpixels_options options
= {{0}};
1063 unsigned samplers_used
= stfp
->Base
.SamplersUsed
;
1065 /* Find the first unused slot. */
1066 variant
->drawpix_sampler
= ffs(~samplers_used
) - 1;
1067 options
.drawpix_sampler
= variant
->drawpix_sampler
;
1068 samplers_used
|= (1 << variant
->drawpix_sampler
);
1070 options
.pixel_maps
= key
->pixelMaps
;
1071 if (key
->pixelMaps
) {
1072 variant
->pixelmap_sampler
= ffs(~samplers_used
) - 1;
1073 options
.pixelmap_sampler
= variant
->pixelmap_sampler
;
1076 options
.scale_and_bias
= key
->scaleAndBias
;
1077 if (key
->scaleAndBias
) {
1078 _mesa_add_state_reference(params
, scale_state
);
1079 memcpy(options
.scale_state_tokens
, scale_state
,
1080 sizeof(options
.scale_state_tokens
));
1081 _mesa_add_state_reference(params
, bias_state
);
1082 memcpy(options
.bias_state_tokens
, bias_state
,
1083 sizeof(options
.bias_state_tokens
));
1086 _mesa_add_state_reference(params
, texcoord_state
);
1087 memcpy(options
.texcoord_state_tokens
, texcoord_state
,
1088 sizeof(options
.texcoord_state_tokens
));
1090 NIR_PASS_V(tgsi
.ir
.nir
, nir_lower_drawpixels
, &options
);
1093 if (unlikely(key
->external
.lower_nv12
|| key
->external
.lower_iyuv
)) {
1094 nir_lower_tex_options options
= {0};
1095 options
.lower_y_uv_external
= key
->external
.lower_nv12
;
1096 options
.lower_y_u_v_external
= key
->external
.lower_iyuv
;
1097 NIR_PASS_V(tgsi
.ir
.nir
, nir_lower_tex
, &options
);
1100 st_finalize_nir(st
, &stfp
->Base
, stfp
->shader_program
, tgsi
.ir
.nir
);
1102 if (unlikely(key
->external
.lower_nv12
|| key
->external
.lower_iyuv
)) {
1103 /* This pass needs to happen *after* nir_lower_sampler */
1104 NIR_PASS_V(tgsi
.ir
.nir
, st_nir_lower_tex_src_plane
,
1105 ~stfp
->Base
.SamplersUsed
,
1106 key
->external
.lower_nv12
,
1107 key
->external
.lower_iyuv
);
1110 variant
->driver_shader
= pipe
->create_fs_state(pipe
, &tgsi
);
1111 variant
->key
= *key
;
1116 tgsi
.tokens
= stfp
->tgsi
.tokens
;
1118 assert(!(key
->bitmap
&& key
->drawpixels
));
1120 /* Fix texture targets and add fog for ATI_fs */
1122 const struct tgsi_token
*tokens
= st_fixup_atifs(tgsi
.tokens
, key
);
1125 tgsi
.tokens
= tokens
;
1127 fprintf(stderr
, "mesa: cannot post-process ATI_fs\n");
1130 /* Emulate features. */
1131 if (key
->clamp_color
|| key
->persample_shading
) {
1132 const struct tgsi_token
*tokens
;
1134 (key
->clamp_color
? TGSI_EMU_CLAMP_COLOR_OUTPUTS
: 0) |
1135 (key
->persample_shading
? TGSI_EMU_FORCE_PERSAMPLE_INTERP
: 0);
1137 tokens
= tgsi_emulate(tgsi
.tokens
, flags
);
1140 if (tgsi
.tokens
!= stfp
->tgsi
.tokens
)
1141 tgsi_free_tokens(tgsi
.tokens
);
1142 tgsi
.tokens
= tokens
;
1144 fprintf(stderr
, "mesa: cannot emulate deprecated features\n");
1149 const struct tgsi_token
*tokens
;
1151 variant
->bitmap_sampler
= ffs(~stfp
->Base
.SamplersUsed
) - 1;
1153 tokens
= st_get_bitmap_shader(tgsi
.tokens
,
1154 st
->internal_target
,
1155 variant
->bitmap_sampler
,
1156 st
->needs_texcoord_semantic
,
1157 st
->bitmap
.tex_format
==
1158 PIPE_FORMAT_L8_UNORM
);
1161 if (tgsi
.tokens
!= stfp
->tgsi
.tokens
)
1162 tgsi_free_tokens(tgsi
.tokens
);
1163 tgsi
.tokens
= tokens
;
1165 fprintf(stderr
, "mesa: cannot create a shader for glBitmap\n");
1168 /* glDrawPixels (color only) */
1169 if (key
->drawpixels
) {
1170 const struct tgsi_token
*tokens
;
1171 unsigned scale_const
= 0, bias_const
= 0, texcoord_const
= 0;
1173 /* Find the first unused slot. */
1174 variant
->drawpix_sampler
= ffs(~stfp
->Base
.SamplersUsed
) - 1;
1176 if (key
->pixelMaps
) {
1177 unsigned samplers_used
= stfp
->Base
.SamplersUsed
|
1178 (1 << variant
->drawpix_sampler
);
1180 variant
->pixelmap_sampler
= ffs(~samplers_used
) - 1;
1183 if (key
->scaleAndBias
) {
1184 scale_const
= _mesa_add_state_reference(params
, scale_state
);
1185 bias_const
= _mesa_add_state_reference(params
, bias_state
);
1188 texcoord_const
= _mesa_add_state_reference(params
, texcoord_state
);
1190 tokens
= st_get_drawpix_shader(tgsi
.tokens
,
1191 st
->needs_texcoord_semantic
,
1192 key
->scaleAndBias
, scale_const
,
1193 bias_const
, key
->pixelMaps
,
1194 variant
->drawpix_sampler
,
1195 variant
->pixelmap_sampler
,
1196 texcoord_const
, st
->internal_target
);
1199 if (tgsi
.tokens
!= stfp
->tgsi
.tokens
)
1200 tgsi_free_tokens(tgsi
.tokens
);
1201 tgsi
.tokens
= tokens
;
1203 fprintf(stderr
, "mesa: cannot create a shader for glDrawPixels\n");
1206 if (unlikely(key
->external
.lower_nv12
|| key
->external
.lower_iyuv
)) {
1207 const struct tgsi_token
*tokens
;
1209 /* samplers inserted would conflict, but this should be unpossible: */
1210 assert(!(key
->bitmap
|| key
->drawpixels
));
1212 tokens
= st_tgsi_lower_yuv(tgsi
.tokens
,
1213 ~stfp
->Base
.SamplersUsed
,
1214 key
->external
.lower_nv12
,
1215 key
->external
.lower_iyuv
);
1217 if (tgsi
.tokens
!= stfp
->tgsi
.tokens
)
1218 tgsi_free_tokens(tgsi
.tokens
);
1219 tgsi
.tokens
= tokens
;
1221 fprintf(stderr
, "mesa: cannot create a shader for samplerExternalOES\n");
1225 if (ST_DEBUG
& DEBUG_TGSI
) {
1226 tgsi_dump(tgsi
.tokens
, 0);
1230 /* fill in variant */
1231 variant
->driver_shader
= pipe
->create_fs_state(pipe
, &tgsi
);
1232 variant
->key
= *key
;
1234 if (tgsi
.tokens
!= stfp
->tgsi
.tokens
)
1235 tgsi_free_tokens(tgsi
.tokens
);
1240 * Translate fragment program if needed.
1242 struct st_fp_variant
*
1243 st_get_fp_variant(struct st_context
*st
,
1244 struct st_fragment_program
*stfp
,
1245 const struct st_fp_variant_key
*key
)
1247 struct st_fp_variant
*fpv
;
1249 /* Search for existing variant */
1250 for (fpv
= stfp
->variants
; fpv
; fpv
= fpv
->next
) {
1251 if (memcmp(&fpv
->key
, key
, sizeof(*key
)) == 0) {
1258 fpv
= st_create_fp_variant(st
, stfp
, key
);
1260 if (key
->bitmap
|| key
->drawpixels
) {
1261 /* Regular variants should always come before the
1262 * bitmap & drawpixels variants, (unless there
1263 * are no regular variants) so that
1264 * st_update_fp can take a fast path when
1265 * shader_has_one_variant is set.
1267 if (!stfp
->variants
) {
1268 stfp
->variants
= fpv
;
1270 /* insert into list after the first one */
1271 fpv
->next
= stfp
->variants
->next
;
1272 stfp
->variants
->next
= fpv
;
1275 /* insert into list */
1276 fpv
->next
= stfp
->variants
;
1277 stfp
->variants
= fpv
;
1287 * Translate a program. This is common code for geometry and tessellation
1291 st_translate_program_common(struct st_context
*st
,
1292 struct gl_program
*prog
,
1293 struct glsl_to_tgsi_visitor
*glsl_to_tgsi
,
1294 struct ureg_program
*ureg
,
1295 unsigned tgsi_processor
,
1296 struct pipe_shader_state
*out_state
)
1298 ubyte inputSlotToAttr
[VARYING_SLOT_TESS_MAX
];
1299 ubyte inputMapping
[VARYING_SLOT_TESS_MAX
];
1300 ubyte outputMapping
[VARYING_SLOT_TESS_MAX
];
1303 ubyte input_semantic_name
[PIPE_MAX_SHADER_INPUTS
];
1304 ubyte input_semantic_index
[PIPE_MAX_SHADER_INPUTS
];
1305 uint num_inputs
= 0;
1307 ubyte output_semantic_name
[PIPE_MAX_SHADER_OUTPUTS
];
1308 ubyte output_semantic_index
[PIPE_MAX_SHADER_OUTPUTS
];
1309 uint num_outputs
= 0;
1313 memset(inputSlotToAttr
, 0, sizeof(inputSlotToAttr
));
1314 memset(inputMapping
, 0, sizeof(inputMapping
));
1315 memset(outputMapping
, 0, sizeof(outputMapping
));
1316 memset(out_state
, 0, sizeof(*out_state
));
1318 if (prog
->info
.clip_distance_array_size
)
1319 ureg_property(ureg
, TGSI_PROPERTY_NUM_CLIPDIST_ENABLED
,
1320 prog
->info
.clip_distance_array_size
);
1321 if (prog
->info
.cull_distance_array_size
)
1322 ureg_property(ureg
, TGSI_PROPERTY_NUM_CULLDIST_ENABLED
,
1323 prog
->info
.cull_distance_array_size
);
1326 * Convert Mesa program inputs to TGSI input register semantics.
1328 for (attr
= 0; attr
< VARYING_SLOT_MAX
; attr
++) {
1329 if ((prog
->info
.inputs_read
& BITFIELD64_BIT(attr
)) == 0)
1332 unsigned slot
= num_inputs
++;
1334 inputMapping
[attr
] = slot
;
1335 inputSlotToAttr
[slot
] = attr
;
1337 unsigned semantic_name
, semantic_index
;
1338 tgsi_get_gl_varying_semantic(attr
, st
->needs_texcoord_semantic
,
1339 &semantic_name
, &semantic_index
);
1340 input_semantic_name
[slot
] = semantic_name
;
1341 input_semantic_index
[slot
] = semantic_index
;
1344 /* Also add patch inputs. */
1345 for (attr
= 0; attr
< 32; attr
++) {
1346 if (prog
->info
.patch_inputs_read
& (1u << attr
)) {
1347 GLuint slot
= num_inputs
++;
1348 GLuint patch_attr
= VARYING_SLOT_PATCH0
+ attr
;
1350 inputMapping
[patch_attr
] = slot
;
1351 inputSlotToAttr
[slot
] = patch_attr
;
1352 input_semantic_name
[slot
] = TGSI_SEMANTIC_PATCH
;
1353 input_semantic_index
[slot
] = attr
;
1357 /* initialize output semantics to defaults */
1358 for (i
= 0; i
< PIPE_MAX_SHADER_OUTPUTS
; i
++) {
1359 output_semantic_name
[i
] = TGSI_SEMANTIC_GENERIC
;
1360 output_semantic_index
[i
] = 0;
1364 * Determine number of outputs, the (default) output register
1365 * mapping and the semantic information for each output.
1367 for (attr
= 0; attr
< VARYING_SLOT_MAX
; attr
++) {
1368 if (prog
->info
.outputs_written
& BITFIELD64_BIT(attr
)) {
1369 GLuint slot
= num_outputs
++;
1371 outputMapping
[attr
] = slot
;
1373 unsigned semantic_name
, semantic_index
;
1374 tgsi_get_gl_varying_semantic(attr
, st
->needs_texcoord_semantic
,
1375 &semantic_name
, &semantic_index
);
1376 output_semantic_name
[slot
] = semantic_name
;
1377 output_semantic_index
[slot
] = semantic_index
;
1381 /* Also add patch outputs. */
1382 for (attr
= 0; attr
< 32; attr
++) {
1383 if (prog
->info
.patch_outputs_written
& (1u << attr
)) {
1384 GLuint slot
= num_outputs
++;
1385 GLuint patch_attr
= VARYING_SLOT_PATCH0
+ attr
;
1387 outputMapping
[patch_attr
] = slot
;
1388 output_semantic_name
[slot
] = TGSI_SEMANTIC_PATCH
;
1389 output_semantic_index
[slot
] = attr
;
1393 st_translate_program(st
->ctx
,
1402 input_semantic_name
,
1403 input_semantic_index
,
1408 output_semantic_name
,
1409 output_semantic_index
);
1411 if (tgsi_processor
== PIPE_SHADER_COMPUTE
) {
1412 struct st_compute_program
*stcp
= (struct st_compute_program
*) prog
;
1413 out_state
->tokens
= ureg_get_tokens(ureg
, &stcp
->num_tgsi_tokens
);
1414 stcp
->tgsi
.prog
= out_state
->tokens
;
1416 struct st_common_program
*stcp
= (struct st_common_program
*) prog
;
1417 out_state
->tokens
= ureg_get_tokens(ureg
, &stcp
->num_tgsi_tokens
);
1421 st_translate_stream_output_info(glsl_to_tgsi
,
1423 &out_state
->stream_output
);
1425 st_store_ir_in_disk_cache(st
, prog
, false);
1427 if ((ST_DEBUG
& DEBUG_TGSI
) && (ST_DEBUG
& DEBUG_MESA
)) {
1428 _mesa_print_program(prog
);
1432 if (ST_DEBUG
& DEBUG_TGSI
) {
1433 tgsi_dump(out_state
->tokens
, 0);
1439 * Update stream-output info for GS/TCS/TES. Normally this is done in
1440 * st_translate_program_common() but that is not called for glsl_to_nir
1444 st_translate_program_stream_output(struct gl_program
*prog
,
1445 struct pipe_stream_output_info
*stream_output
)
1447 if (!prog
->sh
.LinkedTransformFeedback
)
1450 ubyte outputMapping
[VARYING_SLOT_TESS_MAX
];
1452 uint num_outputs
= 0;
1454 memset(outputMapping
, 0, sizeof(outputMapping
));
1457 * Determine number of outputs, the (default) output register
1458 * mapping and the semantic information for each output.
1460 for (attr
= 0; attr
< VARYING_SLOT_MAX
; attr
++) {
1461 if (prog
->info
.outputs_written
& BITFIELD64_BIT(attr
)) {
1462 GLuint slot
= num_outputs
++;
1464 outputMapping
[attr
] = slot
;
1468 st_translate_stream_output_info2(prog
->sh
.LinkedTransformFeedback
,
1474 * Translate a geometry program to create a new variant.
1477 st_translate_geometry_program(struct st_context
*st
,
1478 struct st_common_program
*stgp
)
1480 struct ureg_program
*ureg
;
1482 /* We have already compiled to NIR so just return */
1483 if (stgp
->shader_program
) {
1485 st_finalize_nir(st
, &stgp
->Base
, stgp
->shader_program
,
1487 st_translate_program_stream_output(&stgp
->Base
, &stgp
->tgsi
.stream_output
);
1488 st_store_ir_in_disk_cache(st
, &stgp
->Base
, true);
1492 ureg
= ureg_create_with_screen(PIPE_SHADER_GEOMETRY
, st
->pipe
->screen
);
1496 ureg_property(ureg
, TGSI_PROPERTY_GS_INPUT_PRIM
,
1497 stgp
->Base
.info
.gs
.input_primitive
);
1498 ureg_property(ureg
, TGSI_PROPERTY_GS_OUTPUT_PRIM
,
1499 stgp
->Base
.info
.gs
.output_primitive
);
1500 ureg_property(ureg
, TGSI_PROPERTY_GS_MAX_OUTPUT_VERTICES
,
1501 stgp
->Base
.info
.gs
.vertices_out
);
1502 ureg_property(ureg
, TGSI_PROPERTY_GS_INVOCATIONS
,
1503 stgp
->Base
.info
.gs
.invocations
);
1505 st_translate_program_common(st
, &stgp
->Base
, stgp
->glsl_to_tgsi
, ureg
,
1506 PIPE_SHADER_GEOMETRY
, &stgp
->tgsi
);
1508 free_glsl_to_tgsi_visitor(stgp
->glsl_to_tgsi
);
1509 stgp
->glsl_to_tgsi
= NULL
;
1515 * Get/create a basic program variant.
1517 struct st_basic_variant
*
1518 st_get_basic_variant(struct st_context
*st
,
1519 unsigned pipe_shader
,
1520 struct st_common_program
*prog
)
1522 struct pipe_context
*pipe
= st
->pipe
;
1523 struct st_basic_variant
*v
;
1524 struct st_basic_variant_key key
;
1525 struct pipe_shader_state tgsi
= {0};
1526 memset(&key
, 0, sizeof(key
));
1527 key
.st
= st
->has_shareable_shaders
? NULL
: st
;
1529 /* Search for existing variant */
1530 for (v
= prog
->variants
; v
; v
= v
->next
) {
1531 if (memcmp(&v
->key
, &key
, sizeof(key
)) == 0) {
1538 v
= CALLOC_STRUCT(st_basic_variant
);
1541 if (prog
->tgsi
.type
== PIPE_SHADER_IR_NIR
) {
1542 tgsi
.type
= PIPE_SHADER_IR_NIR
;
1543 tgsi
.ir
.nir
= nir_shader_clone(NULL
, prog
->tgsi
.ir
.nir
);
1544 tgsi
.stream_output
= prog
->tgsi
.stream_output
;
1547 /* fill in new variant */
1548 switch (pipe_shader
) {
1549 case PIPE_SHADER_TESS_CTRL
:
1550 v
->driver_shader
= pipe
->create_tcs_state(pipe
, &tgsi
);
1552 case PIPE_SHADER_TESS_EVAL
:
1553 v
->driver_shader
= pipe
->create_tes_state(pipe
, &tgsi
);
1555 case PIPE_SHADER_GEOMETRY
:
1556 v
->driver_shader
= pipe
->create_gs_state(pipe
, &tgsi
);
1559 assert(!"unhandled shader type");
1566 /* insert into list */
1567 v
->next
= prog
->variants
;
1577 * Translate a tessellation control program to create a new variant.
1580 st_translate_tessctrl_program(struct st_context
*st
,
1581 struct st_common_program
*sttcp
)
1583 struct ureg_program
*ureg
;
1585 /* We have already compiled to NIR so just return */
1586 if (sttcp
->shader_program
) {
1588 st_finalize_nir(st
, &sttcp
->Base
, sttcp
->shader_program
,
1589 sttcp
->tgsi
.ir
.nir
);
1590 st_store_ir_in_disk_cache(st
, &sttcp
->Base
, true);
1594 ureg
= ureg_create_with_screen(PIPE_SHADER_TESS_CTRL
, st
->pipe
->screen
);
1598 ureg_property(ureg
, TGSI_PROPERTY_TCS_VERTICES_OUT
,
1599 sttcp
->Base
.info
.tess
.tcs_vertices_out
);
1601 st_translate_program_common(st
, &sttcp
->Base
, sttcp
->glsl_to_tgsi
, ureg
,
1602 PIPE_SHADER_TESS_CTRL
, &sttcp
->tgsi
);
1604 free_glsl_to_tgsi_visitor(sttcp
->glsl_to_tgsi
);
1605 sttcp
->glsl_to_tgsi
= NULL
;
1611 * Translate a tessellation evaluation program to create a new variant.
1614 st_translate_tesseval_program(struct st_context
*st
,
1615 struct st_common_program
*sttep
)
1617 struct ureg_program
*ureg
;
1619 /* We have already compiled to NIR so just return */
1620 if (sttep
->shader_program
) {
1622 st_finalize_nir(st
, &sttep
->Base
, sttep
->shader_program
,
1623 sttep
->tgsi
.ir
.nir
);
1624 st_translate_program_stream_output(&sttep
->Base
, &sttep
->tgsi
.stream_output
);
1625 st_store_ir_in_disk_cache(st
, &sttep
->Base
, true);
1629 ureg
= ureg_create_with_screen(PIPE_SHADER_TESS_EVAL
, st
->pipe
->screen
);
1633 if (sttep
->Base
.info
.tess
.primitive_mode
== GL_ISOLINES
)
1634 ureg_property(ureg
, TGSI_PROPERTY_TES_PRIM_MODE
, GL_LINES
);
1636 ureg_property(ureg
, TGSI_PROPERTY_TES_PRIM_MODE
,
1637 sttep
->Base
.info
.tess
.primitive_mode
);
1639 STATIC_ASSERT((TESS_SPACING_EQUAL
+ 1) % 3 == PIPE_TESS_SPACING_EQUAL
);
1640 STATIC_ASSERT((TESS_SPACING_FRACTIONAL_ODD
+ 1) % 3 ==
1641 PIPE_TESS_SPACING_FRACTIONAL_ODD
);
1642 STATIC_ASSERT((TESS_SPACING_FRACTIONAL_EVEN
+ 1) % 3 ==
1643 PIPE_TESS_SPACING_FRACTIONAL_EVEN
);
1645 ureg_property(ureg
, TGSI_PROPERTY_TES_SPACING
,
1646 (sttep
->Base
.info
.tess
.spacing
+ 1) % 3);
1648 ureg_property(ureg
, TGSI_PROPERTY_TES_VERTEX_ORDER_CW
,
1649 !sttep
->Base
.info
.tess
.ccw
);
1650 ureg_property(ureg
, TGSI_PROPERTY_TES_POINT_MODE
,
1651 sttep
->Base
.info
.tess
.point_mode
);
1653 st_translate_program_common(st
, &sttep
->Base
, sttep
->glsl_to_tgsi
,
1654 ureg
, PIPE_SHADER_TESS_EVAL
, &sttep
->tgsi
);
1656 free_glsl_to_tgsi_visitor(sttep
->glsl_to_tgsi
);
1657 sttep
->glsl_to_tgsi
= NULL
;
1663 * Translate a compute program to create a new variant.
1666 st_translate_compute_program(struct st_context
*st
,
1667 struct st_compute_program
*stcp
)
1669 struct ureg_program
*ureg
;
1670 struct pipe_shader_state prog
;
1672 stcp
->tgsi
.req_local_mem
= stcp
->Base
.info
.cs
.shared_size
;
1674 if (stcp
->shader_program
) {
1675 /* no compute variants: */
1676 st_finalize_nir(st
, &stcp
->Base
, stcp
->shader_program
,
1677 (struct nir_shader
*) stcp
->tgsi
.prog
);
1678 st_store_ir_in_disk_cache(st
, &stcp
->Base
, true);
1682 ureg
= ureg_create_with_screen(PIPE_SHADER_COMPUTE
, st
->pipe
->screen
);
1686 st_translate_program_common(st
, &stcp
->Base
, stcp
->glsl_to_tgsi
, ureg
,
1687 PIPE_SHADER_COMPUTE
, &prog
);
1689 stcp
->tgsi
.ir_type
= PIPE_SHADER_IR_TGSI
;
1690 stcp
->tgsi
.req_private_mem
= 0;
1691 stcp
->tgsi
.req_input_mem
= 0;
1693 free_glsl_to_tgsi_visitor(stcp
->glsl_to_tgsi
);
1694 stcp
->glsl_to_tgsi
= NULL
;
1700 * Get/create compute program variant.
1702 struct st_basic_variant
*
1703 st_get_cp_variant(struct st_context
*st
,
1704 struct pipe_compute_state
*tgsi
,
1705 struct st_basic_variant
**variants
)
1707 struct pipe_context
*pipe
= st
->pipe
;
1708 struct st_basic_variant
*v
;
1709 struct st_basic_variant_key key
;
1711 memset(&key
, 0, sizeof(key
));
1712 key
.st
= st
->has_shareable_shaders
? NULL
: st
;
1714 /* Search for existing variant */
1715 for (v
= *variants
; v
; v
= v
->next
) {
1716 if (memcmp(&v
->key
, &key
, sizeof(key
)) == 0) {
1723 v
= CALLOC_STRUCT(st_basic_variant
);
1725 /* fill in new variant */
1726 struct pipe_compute_state cs
= *tgsi
;
1727 if (tgsi
->ir_type
== PIPE_SHADER_IR_NIR
)
1728 cs
.prog
= nir_shader_clone(NULL
, tgsi
->prog
);
1729 v
->driver_shader
= pipe
->create_compute_state(pipe
, &cs
);
1732 /* insert into list */
1733 v
->next
= *variants
;
1743 * Vert/Geom/Frag programs have per-context variants. Free all the
1744 * variants attached to the given program which match the given context.
1747 destroy_program_variants(struct st_context
*st
, struct gl_program
*target
)
1749 if (!target
|| target
== &_mesa_DummyProgram
)
1752 switch (target
->Target
) {
1753 case GL_VERTEX_PROGRAM_ARB
:
1755 struct st_vertex_program
*stvp
= (struct st_vertex_program
*) target
;
1756 struct st_vp_variant
*vpv
, **prevPtr
= &stvp
->variants
;
1758 for (vpv
= stvp
->variants
; vpv
; ) {
1759 struct st_vp_variant
*next
= vpv
->next
;
1760 if (vpv
->key
.st
== st
) {
1761 /* unlink from list */
1763 /* destroy this variant */
1764 delete_vp_variant(st
, vpv
);
1767 prevPtr
= &vpv
->next
;
1773 case GL_FRAGMENT_PROGRAM_ARB
:
1775 struct st_fragment_program
*stfp
=
1776 (struct st_fragment_program
*) target
;
1777 struct st_fp_variant
*fpv
, **prevPtr
= &stfp
->variants
;
1779 for (fpv
= stfp
->variants
; fpv
; ) {
1780 struct st_fp_variant
*next
= fpv
->next
;
1781 if (fpv
->key
.st
== st
) {
1782 /* unlink from list */
1784 /* destroy this variant */
1785 delete_fp_variant(st
, fpv
);
1788 prevPtr
= &fpv
->next
;
1794 case GL_GEOMETRY_PROGRAM_NV
:
1795 case GL_TESS_CONTROL_PROGRAM_NV
:
1796 case GL_TESS_EVALUATION_PROGRAM_NV
:
1797 case GL_COMPUTE_PROGRAM_NV
:
1799 struct st_common_program
*p
= st_common_program(target
);
1800 struct st_compute_program
*cp
= (struct st_compute_program
*)target
;
1801 struct st_basic_variant
**variants
=
1802 target
->Target
== GL_COMPUTE_PROGRAM_NV
? &cp
->variants
:
1804 struct st_basic_variant
*v
, **prevPtr
= variants
;
1806 for (v
= *variants
; v
; ) {
1807 struct st_basic_variant
*next
= v
->next
;
1808 if (v
->key
.st
== st
) {
1809 /* unlink from list */
1811 /* destroy this variant */
1812 delete_basic_variant(st
, v
, target
->Target
);
1822 _mesa_problem(NULL
, "Unexpected program target 0x%x in "
1823 "destroy_program_variants_cb()", target
->Target
);
1829 * Callback for _mesa_HashWalk. Free all the shader's program variants
1830 * which match the given context.
1833 destroy_shader_program_variants_cb(GLuint key
, void *data
, void *userData
)
1835 struct st_context
*st
= (struct st_context
*) userData
;
1836 struct gl_shader
*shader
= (struct gl_shader
*) data
;
1838 switch (shader
->Type
) {
1839 case GL_SHADER_PROGRAM_MESA
:
1841 struct gl_shader_program
*shProg
= (struct gl_shader_program
*) data
;
1844 for (i
= 0; i
< ARRAY_SIZE(shProg
->_LinkedShaders
); i
++) {
1845 if (shProg
->_LinkedShaders
[i
])
1846 destroy_program_variants(st
, shProg
->_LinkedShaders
[i
]->Program
);
1850 case GL_VERTEX_SHADER
:
1851 case GL_FRAGMENT_SHADER
:
1852 case GL_GEOMETRY_SHADER
:
1853 case GL_TESS_CONTROL_SHADER
:
1854 case GL_TESS_EVALUATION_SHADER
:
1855 case GL_COMPUTE_SHADER
:
1864 * Callback for _mesa_HashWalk. Free all the program variants which match
1865 * the given context.
1868 destroy_program_variants_cb(GLuint key
, void *data
, void *userData
)
1870 struct st_context
*st
= (struct st_context
*) userData
;
1871 struct gl_program
*program
= (struct gl_program
*) data
;
1872 destroy_program_variants(st
, program
);
1877 * Walk over all shaders and programs to delete any variants which
1878 * belong to the given context.
1879 * This is called during context tear-down.
1882 st_destroy_program_variants(struct st_context
*st
)
1884 /* If shaders can be shared with other contexts, the last context will
1885 * call DeleteProgram on all shaders, releasing everything.
1887 if (st
->has_shareable_shaders
)
1890 /* ARB vert/frag program */
1891 _mesa_HashWalk(st
->ctx
->Shared
->Programs
,
1892 destroy_program_variants_cb
, st
);
1894 /* GLSL vert/frag/geom shaders */
1895 _mesa_HashWalk(st
->ctx
->Shared
->ShaderObjects
,
1896 destroy_shader_program_variants_cb
, st
);
1901 * For debugging, print/dump the current vertex program.
1904 st_print_current_vertex_program(void)
1906 GET_CURRENT_CONTEXT(ctx
);
1908 if (ctx
->VertexProgram
._Current
) {
1909 struct st_vertex_program
*stvp
=
1910 (struct st_vertex_program
*) ctx
->VertexProgram
._Current
;
1911 struct st_vp_variant
*stv
;
1913 debug_printf("Vertex program %u\n", stvp
->Base
.Id
);
1915 for (stv
= stvp
->variants
; stv
; stv
= stv
->next
) {
1916 debug_printf("variant %p\n", stv
);
1917 tgsi_dump(stv
->tgsi
.tokens
, 0);
1924 * Compile one shader variant.
1927 st_precompile_shader_variant(struct st_context
*st
,
1928 struct gl_program
*prog
)
1930 switch (prog
->Target
) {
1931 case GL_VERTEX_PROGRAM_ARB
: {
1932 struct st_vertex_program
*p
= (struct st_vertex_program
*)prog
;
1933 struct st_vp_variant_key key
;
1935 memset(&key
, 0, sizeof(key
));
1936 key
.st
= st
->has_shareable_shaders
? NULL
: st
;
1937 st_get_vp_variant(st
, p
, &key
);
1941 case GL_TESS_CONTROL_PROGRAM_NV
: {
1942 struct st_common_program
*p
= st_common_program(prog
);
1943 st_get_basic_variant(st
, PIPE_SHADER_TESS_CTRL
, p
);
1947 case GL_TESS_EVALUATION_PROGRAM_NV
: {
1948 struct st_common_program
*p
= st_common_program(prog
);
1949 st_get_basic_variant(st
, PIPE_SHADER_TESS_EVAL
, p
);
1953 case GL_GEOMETRY_PROGRAM_NV
: {
1954 struct st_common_program
*p
= st_common_program(prog
);
1955 st_get_basic_variant(st
, PIPE_SHADER_GEOMETRY
, p
);
1959 case GL_FRAGMENT_PROGRAM_ARB
: {
1960 struct st_fragment_program
*p
= (struct st_fragment_program
*)prog
;
1961 struct st_fp_variant_key key
;
1963 memset(&key
, 0, sizeof(key
));
1964 key
.st
= st
->has_shareable_shaders
? NULL
: st
;
1965 st_get_fp_variant(st
, p
, &key
);
1969 case GL_COMPUTE_PROGRAM_NV
: {
1970 struct st_compute_program
*p
= (struct st_compute_program
*)prog
;
1971 st_get_cp_variant(st
, &p
->tgsi
, &p
->variants
);