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/prog_to_nir.h"
41 #include "program/programopt.h"
43 #include "compiler/nir/nir.h"
45 #include "pipe/p_context.h"
46 #include "pipe/p_defines.h"
47 #include "pipe/p_shader_tokens.h"
48 #include "draw/draw_context.h"
49 #include "tgsi/tgsi_dump.h"
50 #include "tgsi/tgsi_emulate.h"
51 #include "tgsi/tgsi_parse.h"
52 #include "tgsi/tgsi_ureg.h"
55 #include "st_cb_bitmap.h"
56 #include "st_cb_drawpixels.h"
57 #include "st_context.h"
58 #include "st_tgsi_lower_yuv.h"
59 #include "st_program.h"
60 #include "st_mesa_to_tgsi.h"
61 #include "st_atifs_to_tgsi.h"
63 #include "st_shader_cache.h"
64 #include "cso_cache/cso_context.h"
69 set_affected_state_flags(uint64_t *states
,
70 struct gl_program
*prog
,
71 uint64_t new_constants
,
72 uint64_t new_sampler_views
,
73 uint64_t new_samplers
,
79 if (prog
->Parameters
->NumParameters
)
80 *states
|= new_constants
;
82 if (prog
->info
.num_textures
)
83 *states
|= new_sampler_views
| new_samplers
;
85 if (prog
->info
.num_images
)
86 *states
|= new_images
;
88 if (prog
->info
.num_ubos
)
91 if (prog
->info
.num_ssbos
)
94 if (prog
->info
.num_abos
)
95 *states
|= new_atomics
;
99 * This determines which states will be updated when the shader is bound.
102 st_set_prog_affected_state_flags(struct gl_program
*prog
)
106 switch (prog
->info
.stage
) {
107 case MESA_SHADER_VERTEX
:
108 states
= &((struct st_vertex_program
*)prog
)->affected_states
;
110 *states
= ST_NEW_VS_STATE
|
112 ST_NEW_VERTEX_ARRAYS
;
114 set_affected_state_flags(states
, prog
,
116 ST_NEW_VS_SAMPLER_VIEWS
,
124 case MESA_SHADER_TESS_CTRL
:
125 states
= &(st_common_program(prog
))->affected_states
;
127 *states
= ST_NEW_TCS_STATE
;
129 set_affected_state_flags(states
, prog
,
130 ST_NEW_TCS_CONSTANTS
,
131 ST_NEW_TCS_SAMPLER_VIEWS
,
139 case MESA_SHADER_TESS_EVAL
:
140 states
= &(st_common_program(prog
))->affected_states
;
142 *states
= ST_NEW_TES_STATE
|
145 set_affected_state_flags(states
, prog
,
146 ST_NEW_TES_CONSTANTS
,
147 ST_NEW_TES_SAMPLER_VIEWS
,
155 case MESA_SHADER_GEOMETRY
:
156 states
= &(st_common_program(prog
))->affected_states
;
158 *states
= ST_NEW_GS_STATE
|
161 set_affected_state_flags(states
, prog
,
163 ST_NEW_GS_SAMPLER_VIEWS
,
171 case MESA_SHADER_FRAGMENT
:
172 states
= &((struct st_fragment_program
*)prog
)->affected_states
;
174 /* gl_FragCoord and glDrawPixels always use constants. */
175 *states
= ST_NEW_FS_STATE
|
176 ST_NEW_SAMPLE_SHADING
|
179 set_affected_state_flags(states
, prog
,
181 ST_NEW_FS_SAMPLER_VIEWS
,
189 case MESA_SHADER_COMPUTE
:
190 states
= &((struct st_compute_program
*)prog
)->affected_states
;
192 *states
= ST_NEW_CS_STATE
;
194 set_affected_state_flags(states
, prog
,
196 ST_NEW_CS_SAMPLER_VIEWS
,
205 unreachable("unhandled shader stage");
210 * Delete a vertex program variant. Note the caller must unlink
211 * the variant from the linked list.
214 delete_vp_variant(struct st_context
*st
, struct st_vp_variant
*vpv
)
216 if (vpv
->driver_shader
)
217 cso_delete_vertex_shader(st
->cso_context
, vpv
->driver_shader
);
219 if (vpv
->draw_shader
)
220 draw_delete_vertex_shader( st
->draw
, vpv
->draw_shader
);
222 if (((vpv
->tgsi
.type
== PIPE_SHADER_IR_TGSI
)) && vpv
->tgsi
.tokens
)
223 ureg_free_tokens(vpv
->tgsi
.tokens
);
231 * Clean out any old compilations:
234 st_release_vp_variants( struct st_context
*st
,
235 struct st_vertex_program
*stvp
)
237 struct st_vp_variant
*vpv
;
239 for (vpv
= stvp
->variants
; vpv
; ) {
240 struct st_vp_variant
*next
= vpv
->next
;
241 delete_vp_variant(st
, vpv
);
245 stvp
->variants
= NULL
;
247 if ((stvp
->tgsi
.type
== PIPE_SHADER_IR_TGSI
) && stvp
->tgsi
.tokens
) {
248 tgsi_free_tokens(stvp
->tgsi
.tokens
);
249 stvp
->tgsi
.tokens
= NULL
;
256 * Delete a fragment program variant. Note the caller must unlink
257 * the variant from the linked list.
260 delete_fp_variant(struct st_context
*st
, struct st_fp_variant
*fpv
)
262 if (fpv
->driver_shader
)
263 cso_delete_fragment_shader(st
->cso_context
, fpv
->driver_shader
);
269 * Free all variants of a fragment program.
272 st_release_fp_variants(struct st_context
*st
, struct st_fragment_program
*stfp
)
274 struct st_fp_variant
*fpv
;
276 for (fpv
= stfp
->variants
; fpv
; ) {
277 struct st_fp_variant
*next
= fpv
->next
;
278 delete_fp_variant(st
, fpv
);
282 stfp
->variants
= NULL
;
284 if ((stfp
->tgsi
.type
== PIPE_SHADER_IR_TGSI
) && stfp
->tgsi
.tokens
) {
285 ureg_free_tokens(stfp
->tgsi
.tokens
);
286 stfp
->tgsi
.tokens
= NULL
;
292 * Delete a basic program variant. Note the caller must unlink
293 * the variant from the linked list.
296 delete_basic_variant(struct st_context
*st
, struct st_basic_variant
*v
,
299 if (v
->driver_shader
) {
301 case GL_TESS_CONTROL_PROGRAM_NV
:
302 cso_delete_tessctrl_shader(st
->cso_context
, v
->driver_shader
);
304 case GL_TESS_EVALUATION_PROGRAM_NV
:
305 cso_delete_tesseval_shader(st
->cso_context
, v
->driver_shader
);
307 case GL_GEOMETRY_PROGRAM_NV
:
308 cso_delete_geometry_shader(st
->cso_context
, v
->driver_shader
);
310 case GL_COMPUTE_PROGRAM_NV
:
311 cso_delete_compute_shader(st
->cso_context
, v
->driver_shader
);
314 assert(!"this shouldn't occur");
323 * Free all basic program variants.
326 st_release_basic_variants(struct st_context
*st
, GLenum target
,
327 struct st_basic_variant
**variants
,
328 struct pipe_shader_state
*tgsi
)
330 struct st_basic_variant
*v
;
332 for (v
= *variants
; v
; ) {
333 struct st_basic_variant
*next
= v
->next
;
334 delete_basic_variant(st
, v
, target
);
341 ureg_free_tokens(tgsi
->tokens
);
348 * Free all variants of a compute program.
351 st_release_cp_variants(struct st_context
*st
, struct st_compute_program
*stcp
)
353 struct st_basic_variant
**variants
= &stcp
->variants
;
354 struct st_basic_variant
*v
;
356 for (v
= *variants
; v
; ) {
357 struct st_basic_variant
*next
= v
->next
;
358 delete_basic_variant(st
, v
, stcp
->Base
.Target
);
364 if (stcp
->tgsi
.prog
) {
365 switch (stcp
->tgsi
.ir_type
) {
366 case PIPE_SHADER_IR_TGSI
:
367 ureg_free_tokens(stcp
->tgsi
.prog
);
368 stcp
->tgsi
.prog
= NULL
;
370 case PIPE_SHADER_IR_NIR
:
371 /* pipe driver took ownership of prog */
373 case PIPE_SHADER_IR_NATIVE
:
375 stcp
->tgsi
.prog
= NULL
;
382 * Translate ARB (asm) program to NIR
385 st_translate_prog_to_nir(struct st_context
*st
, struct gl_program
*prog
,
386 gl_shader_stage stage
)
388 const struct gl_shader_compiler_options
*options
=
389 &st
->ctx
->Const
.ShaderCompilerOptions
[stage
];
391 /* Translate to NIR */
392 nir_shader
*nir
= prog_to_nir(prog
, options
->NirOptions
);
393 NIR_PASS_V(nir
, nir_lower_regs_to_ssa
); /* turn registers into SSA */
394 nir_validate_shader(nir
);
398 nir_validate_shader(nir
);
404 * Translate a vertex program.
407 st_translate_vertex_program(struct st_context
*st
,
408 struct st_vertex_program
*stvp
)
410 struct ureg_program
*ureg
;
411 enum pipe_error error
;
412 unsigned num_outputs
= 0;
414 ubyte output_semantic_name
[VARYING_SLOT_MAX
] = {0};
415 ubyte output_semantic_index
[VARYING_SLOT_MAX
] = {0};
417 stvp
->num_inputs
= 0;
418 memset(stvp
->input_to_index
, ~0, sizeof(stvp
->input_to_index
));
420 if (stvp
->Base
.arb
.IsPositionInvariant
)
421 _mesa_insert_mvp_code(st
->ctx
, &stvp
->Base
);
424 * Determine number of inputs, the mappings between VERT_ATTRIB_x
425 * and TGSI generic input indexes, plus input attrib semantic info.
427 for (attr
= 0; attr
< VERT_ATTRIB_MAX
; attr
++) {
428 if ((stvp
->Base
.info
.inputs_read
& BITFIELD64_BIT(attr
)) != 0) {
429 stvp
->input_to_index
[attr
] = stvp
->num_inputs
;
430 stvp
->index_to_input
[stvp
->num_inputs
] = attr
;
432 if ((stvp
->Base
.info
.vs
.double_inputs_read
&
433 BITFIELD64_BIT(attr
)) != 0) {
434 /* add placeholder for second part of a double attribute */
435 stvp
->index_to_input
[stvp
->num_inputs
] = ST_DOUBLE_ATTRIB_PLACEHOLDER
;
440 /* bit of a hack, presetup potentially unused edgeflag input */
441 stvp
->input_to_index
[VERT_ATTRIB_EDGEFLAG
] = stvp
->num_inputs
;
442 stvp
->index_to_input
[stvp
->num_inputs
] = VERT_ATTRIB_EDGEFLAG
;
444 /* Compute mapping of vertex program outputs to slots.
446 for (attr
= 0; attr
< VARYING_SLOT_MAX
; attr
++) {
447 if ((stvp
->Base
.info
.outputs_written
& BITFIELD64_BIT(attr
)) == 0) {
448 stvp
->result_to_output
[attr
] = ~0;
451 unsigned slot
= num_outputs
++;
453 stvp
->result_to_output
[attr
] = slot
;
455 unsigned semantic_name
, semantic_index
;
456 tgsi_get_gl_varying_semantic(attr
, st
->needs_texcoord_semantic
,
457 &semantic_name
, &semantic_index
);
458 output_semantic_name
[slot
] = semantic_name
;
459 output_semantic_index
[slot
] = semantic_index
;
462 /* similar hack to above, presetup potentially unused edgeflag output */
463 stvp
->result_to_output
[VARYING_SLOT_EDGE
] = num_outputs
;
464 output_semantic_name
[num_outputs
] = TGSI_SEMANTIC_EDGEFLAG
;
465 output_semantic_index
[num_outputs
] = 0;
468 if (!stvp
->glsl_to_tgsi
&& !stvp
->shader_program
) {
469 _mesa_remove_output_reads(&stvp
->Base
, PROGRAM_OUTPUT
);
471 /* This determines which states will be updated when the assembly
474 stvp
->affected_states
= ST_NEW_VS_STATE
|
476 ST_NEW_VERTEX_ARRAYS
;
478 if (stvp
->Base
.Parameters
->NumParameters
)
479 stvp
->affected_states
|= ST_NEW_VS_CONSTANTS
;
481 /* No samplers are allowed in ARB_vp. */
484 enum pipe_shader_ir preferred_ir
= (enum pipe_shader_ir
)
485 st
->pipe
->screen
->get_shader_param(st
->pipe
->screen
, PIPE_SHADER_VERTEX
,
486 PIPE_SHADER_CAP_PREFERRED_IR
);
488 if (preferred_ir
== PIPE_SHADER_IR_NIR
) {
489 if (stvp
->shader_program
) {
490 struct gl_program
*prog
= stvp
->shader_program
->last_vert_prog
;
492 st_translate_stream_output_info2(prog
->sh
.LinkedTransformFeedback
,
493 stvp
->result_to_output
,
494 &stvp
->tgsi
.stream_output
);
497 st_store_ir_in_disk_cache(st
, &stvp
->Base
, true);
499 nir_shader
*nir
= st_translate_prog_to_nir(st
, &stvp
->Base
,
502 stvp
->tgsi
.type
= PIPE_SHADER_IR_NIR
;
503 stvp
->tgsi
.ir
.nir
= nir
;
509 ureg
= ureg_create_with_screen(PIPE_SHADER_VERTEX
, st
->pipe
->screen
);
513 if (stvp
->Base
.info
.clip_distance_array_size
)
514 ureg_property(ureg
, TGSI_PROPERTY_NUM_CLIPDIST_ENABLED
,
515 stvp
->Base
.info
.clip_distance_array_size
);
516 if (stvp
->Base
.info
.cull_distance_array_size
)
517 ureg_property(ureg
, TGSI_PROPERTY_NUM_CULLDIST_ENABLED
,
518 stvp
->Base
.info
.cull_distance_array_size
);
520 if (ST_DEBUG
& DEBUG_MESA
) {
521 _mesa_print_program(&stvp
->Base
);
522 _mesa_print_program_parameters(st
->ctx
, &stvp
->Base
);
526 if (stvp
->glsl_to_tgsi
) {
527 error
= st_translate_program(st
->ctx
,
534 stvp
->input_to_index
,
535 NULL
, /* inputSlotToAttr */
536 NULL
, /* input semantic name */
537 NULL
, /* input semantic index */
538 NULL
, /* interp mode */
541 stvp
->result_to_output
,
542 output_semantic_name
,
543 output_semantic_index
);
545 st_translate_stream_output_info(stvp
->glsl_to_tgsi
,
546 stvp
->result_to_output
,
547 &stvp
->tgsi
.stream_output
);
549 free_glsl_to_tgsi_visitor(stvp
->glsl_to_tgsi
);
551 error
= st_translate_mesa_program(st
->ctx
,
557 stvp
->input_to_index
,
558 NULL
, /* input semantic name */
559 NULL
, /* input semantic index */
563 stvp
->result_to_output
,
564 output_semantic_name
,
565 output_semantic_index
);
568 debug_printf("%s: failed to translate Mesa program:\n", __func__
);
569 _mesa_print_program(&stvp
->Base
);
574 stvp
->tgsi
.tokens
= ureg_get_tokens(ureg
, &stvp
->num_tgsi_tokens
);
577 if (stvp
->glsl_to_tgsi
) {
578 stvp
->glsl_to_tgsi
= NULL
;
579 st_store_ir_in_disk_cache(st
, &stvp
->Base
, false);
582 return stvp
->tgsi
.tokens
!= NULL
;
585 static struct st_vp_variant
*
586 st_create_vp_variant(struct st_context
*st
,
587 struct st_vertex_program
*stvp
,
588 const struct st_vp_variant_key
*key
)
590 struct st_vp_variant
*vpv
= CALLOC_STRUCT(st_vp_variant
);
591 struct pipe_context
*pipe
= st
->pipe
;
594 vpv
->tgsi
.stream_output
= stvp
->tgsi
.stream_output
;
595 vpv
->num_inputs
= stvp
->num_inputs
;
597 if (stvp
->tgsi
.type
== PIPE_SHADER_IR_NIR
) {
598 vpv
->tgsi
.type
= PIPE_SHADER_IR_NIR
;
599 vpv
->tgsi
.ir
.nir
= nir_shader_clone(NULL
, stvp
->tgsi
.ir
.nir
);
600 if (key
->clamp_color
)
601 NIR_PASS_V(vpv
->tgsi
.ir
.nir
, nir_lower_clamp_color_outputs
);
602 if (key
->passthrough_edgeflags
) {
603 NIR_PASS_V(vpv
->tgsi
.ir
.nir
, nir_lower_passthrough_edgeflags
);
607 st_finalize_nir(st
, &stvp
->Base
, stvp
->shader_program
,
610 vpv
->driver_shader
= pipe
->create_vs_state(pipe
, &vpv
->tgsi
);
611 /* driver takes ownership of IR: */
612 vpv
->tgsi
.ir
.nir
= NULL
;
616 vpv
->tgsi
.tokens
= tgsi_dup_tokens(stvp
->tgsi
.tokens
);
618 /* Emulate features. */
619 if (key
->clamp_color
|| key
->passthrough_edgeflags
) {
620 const struct tgsi_token
*tokens
;
622 (key
->clamp_color
? TGSI_EMU_CLAMP_COLOR_OUTPUTS
: 0) |
623 (key
->passthrough_edgeflags
? TGSI_EMU_PASSTHROUGH_EDGEFLAG
: 0);
625 tokens
= tgsi_emulate(vpv
->tgsi
.tokens
, flags
);
628 tgsi_free_tokens(vpv
->tgsi
.tokens
);
629 vpv
->tgsi
.tokens
= tokens
;
631 if (key
->passthrough_edgeflags
)
634 fprintf(stderr
, "mesa: cannot emulate deprecated features\n");
637 if (ST_DEBUG
& DEBUG_TGSI
) {
638 tgsi_dump(vpv
->tgsi
.tokens
, 0);
642 vpv
->driver_shader
= pipe
->create_vs_state(pipe
, &vpv
->tgsi
);
648 * Find/create a vertex program variant.
650 struct st_vp_variant
*
651 st_get_vp_variant(struct st_context
*st
,
652 struct st_vertex_program
*stvp
,
653 const struct st_vp_variant_key
*key
)
655 struct st_vp_variant
*vpv
;
657 /* Search for existing variant */
658 for (vpv
= stvp
->variants
; vpv
; vpv
= vpv
->next
) {
659 if (memcmp(&vpv
->key
, key
, sizeof(*key
)) == 0) {
666 vpv
= st_create_vp_variant(st
, stvp
, key
);
668 for (unsigned index
= 0; index
< vpv
->num_inputs
; ++index
) {
669 unsigned attr
= stvp
->index_to_input
[index
];
670 if (attr
== ST_DOUBLE_ATTRIB_PLACEHOLDER
)
672 vpv
->vert_attrib_mask
|= 1u << attr
;
675 /* insert into list */
676 vpv
->next
= stvp
->variants
;
677 stvp
->variants
= vpv
;
686 * Translate a Mesa fragment shader into a TGSI shader.
689 st_translate_fragment_program(struct st_context
*st
,
690 struct st_fragment_program
*stfp
)
692 /* We have already compiled to NIR so just return */
693 if (stfp
->shader_program
) {
694 st_store_ir_in_disk_cache(st
, &stfp
->Base
, true);
698 ubyte outputMapping
[2 * FRAG_RESULT_MAX
];
699 ubyte inputMapping
[VARYING_SLOT_MAX
];
700 ubyte inputSlotToAttr
[VARYING_SLOT_MAX
];
701 ubyte interpMode
[PIPE_MAX_SHADER_INPUTS
]; /* XXX size? */
703 GLbitfield64 inputsRead
;
704 struct ureg_program
*ureg
;
706 GLboolean write_all
= GL_FALSE
;
708 ubyte input_semantic_name
[PIPE_MAX_SHADER_INPUTS
];
709 ubyte input_semantic_index
[PIPE_MAX_SHADER_INPUTS
];
710 uint fs_num_inputs
= 0;
712 ubyte fs_output_semantic_name
[PIPE_MAX_SHADER_OUTPUTS
];
713 ubyte fs_output_semantic_index
[PIPE_MAX_SHADER_OUTPUTS
];
714 uint fs_num_outputs
= 0;
716 memset(inputSlotToAttr
, ~0, sizeof(inputSlotToAttr
));
718 /* Non-GLSL programs: */
719 if (!stfp
->glsl_to_tgsi
&& !stfp
->shader_program
) {
720 _mesa_remove_output_reads(&stfp
->Base
, PROGRAM_OUTPUT
);
721 if (st
->ctx
->Const
.GLSLFragCoordIsSysVal
)
722 _mesa_program_fragment_position_to_sysval(&stfp
->Base
);
724 /* This determines which states will be updated when the assembly
727 * fragment.position and glDrawPixels always use constants.
729 stfp
->affected_states
= ST_NEW_FS_STATE
|
730 ST_NEW_SAMPLE_SHADING
|
734 /* Just set them for ATI_fs unconditionally. */
735 stfp
->affected_states
|= ST_NEW_FS_SAMPLER_VIEWS
|
739 if (stfp
->Base
.SamplersUsed
)
740 stfp
->affected_states
|= ST_NEW_FS_SAMPLER_VIEWS
|
745 enum pipe_shader_ir preferred_ir
= (enum pipe_shader_ir
)
746 st
->pipe
->screen
->get_shader_param(st
->pipe
->screen
,
747 PIPE_SHADER_FRAGMENT
,
748 PIPE_SHADER_CAP_PREFERRED_IR
);
750 if (preferred_ir
== PIPE_SHADER_IR_NIR
) {
751 nir_shader
*nir
= st_translate_prog_to_nir(st
, &stfp
->Base
,
752 MESA_SHADER_FRAGMENT
);
754 stfp
->tgsi
.type
= PIPE_SHADER_IR_NIR
;
755 stfp
->tgsi
.ir
.nir
= nir
;
761 * Convert Mesa program inputs to TGSI input register semantics.
763 inputsRead
= stfp
->Base
.info
.inputs_read
;
764 for (attr
= 0; attr
< VARYING_SLOT_MAX
; attr
++) {
765 if ((inputsRead
& BITFIELD64_BIT(attr
)) != 0) {
766 const GLuint slot
= fs_num_inputs
++;
768 inputMapping
[attr
] = slot
;
769 inputSlotToAttr
[slot
] = attr
;
772 case VARYING_SLOT_POS
:
773 input_semantic_name
[slot
] = TGSI_SEMANTIC_POSITION
;
774 input_semantic_index
[slot
] = 0;
775 interpMode
[slot
] = TGSI_INTERPOLATE_LINEAR
;
777 case VARYING_SLOT_COL0
:
778 input_semantic_name
[slot
] = TGSI_SEMANTIC_COLOR
;
779 input_semantic_index
[slot
] = 0;
780 interpMode
[slot
] = stfp
->glsl_to_tgsi
?
781 TGSI_INTERPOLATE_COUNT
: TGSI_INTERPOLATE_COLOR
;
783 case VARYING_SLOT_COL1
:
784 input_semantic_name
[slot
] = TGSI_SEMANTIC_COLOR
;
785 input_semantic_index
[slot
] = 1;
786 interpMode
[slot
] = stfp
->glsl_to_tgsi
?
787 TGSI_INTERPOLATE_COUNT
: TGSI_INTERPOLATE_COLOR
;
789 case VARYING_SLOT_FOGC
:
790 input_semantic_name
[slot
] = TGSI_SEMANTIC_FOG
;
791 input_semantic_index
[slot
] = 0;
792 interpMode
[slot
] = TGSI_INTERPOLATE_PERSPECTIVE
;
794 case VARYING_SLOT_FACE
:
795 input_semantic_name
[slot
] = TGSI_SEMANTIC_FACE
;
796 input_semantic_index
[slot
] = 0;
797 interpMode
[slot
] = TGSI_INTERPOLATE_CONSTANT
;
799 case VARYING_SLOT_PRIMITIVE_ID
:
800 input_semantic_name
[slot
] = TGSI_SEMANTIC_PRIMID
;
801 input_semantic_index
[slot
] = 0;
802 interpMode
[slot
] = TGSI_INTERPOLATE_CONSTANT
;
804 case VARYING_SLOT_LAYER
:
805 input_semantic_name
[slot
] = TGSI_SEMANTIC_LAYER
;
806 input_semantic_index
[slot
] = 0;
807 interpMode
[slot
] = TGSI_INTERPOLATE_CONSTANT
;
809 case VARYING_SLOT_VIEWPORT
:
810 input_semantic_name
[slot
] = TGSI_SEMANTIC_VIEWPORT_INDEX
;
811 input_semantic_index
[slot
] = 0;
812 interpMode
[slot
] = TGSI_INTERPOLATE_CONSTANT
;
814 case VARYING_SLOT_CLIP_DIST0
:
815 input_semantic_name
[slot
] = TGSI_SEMANTIC_CLIPDIST
;
816 input_semantic_index
[slot
] = 0;
817 interpMode
[slot
] = TGSI_INTERPOLATE_PERSPECTIVE
;
819 case VARYING_SLOT_CLIP_DIST1
:
820 input_semantic_name
[slot
] = TGSI_SEMANTIC_CLIPDIST
;
821 input_semantic_index
[slot
] = 1;
822 interpMode
[slot
] = TGSI_INTERPOLATE_PERSPECTIVE
;
824 case VARYING_SLOT_CULL_DIST0
:
825 case VARYING_SLOT_CULL_DIST1
:
826 /* these should have been lowered by GLSL */
829 /* In most cases, there is nothing special about these
830 * inputs, so adopt a convention to use the generic
831 * semantic name and the mesa VARYING_SLOT_ number as the
834 * All that is required is that the vertex shader labels
835 * its own outputs similarly, and that the vertex shader
836 * generates at least every output required by the
837 * fragment shader plus fixed-function hardware (such as
840 * However, some drivers may need us to identify the PNTC and TEXi
841 * varyings if, for example, their capability to replace them with
842 * sprite coordinates is limited.
844 case VARYING_SLOT_PNTC
:
845 if (st
->needs_texcoord_semantic
) {
846 input_semantic_name
[slot
] = TGSI_SEMANTIC_PCOORD
;
847 input_semantic_index
[slot
] = 0;
848 interpMode
[slot
] = TGSI_INTERPOLATE_LINEAR
;
852 case VARYING_SLOT_TEX0
:
853 case VARYING_SLOT_TEX1
:
854 case VARYING_SLOT_TEX2
:
855 case VARYING_SLOT_TEX3
:
856 case VARYING_SLOT_TEX4
:
857 case VARYING_SLOT_TEX5
:
858 case VARYING_SLOT_TEX6
:
859 case VARYING_SLOT_TEX7
:
860 if (st
->needs_texcoord_semantic
) {
861 input_semantic_name
[slot
] = TGSI_SEMANTIC_TEXCOORD
;
862 input_semantic_index
[slot
] = attr
- VARYING_SLOT_TEX0
;
863 interpMode
[slot
] = stfp
->glsl_to_tgsi
?
864 TGSI_INTERPOLATE_COUNT
: TGSI_INTERPOLATE_PERSPECTIVE
;
868 case VARYING_SLOT_VAR0
:
870 /* Semantic indices should be zero-based because drivers may choose
871 * to assign a fixed slot determined by that index.
872 * This is useful because ARB_separate_shader_objects uses location
873 * qualifiers for linkage, and if the semantic index corresponds to
874 * these locations, linkage passes in the driver become unecessary.
876 * If needs_texcoord_semantic is true, no semantic indices will be
877 * consumed for the TEXi varyings, and we can base the locations of
878 * the user varyings on VAR0. Otherwise, we use TEX0 as base index.
880 assert(attr
>= VARYING_SLOT_VAR0
|| attr
== VARYING_SLOT_PNTC
||
881 (attr
>= VARYING_SLOT_TEX0
&& attr
<= VARYING_SLOT_TEX7
));
882 input_semantic_name
[slot
] = TGSI_SEMANTIC_GENERIC
;
883 input_semantic_index
[slot
] = st_get_generic_varying_index(st
, attr
);
884 if (attr
== VARYING_SLOT_PNTC
)
885 interpMode
[slot
] = TGSI_INTERPOLATE_LINEAR
;
887 interpMode
[slot
] = stfp
->glsl_to_tgsi
?
888 TGSI_INTERPOLATE_COUNT
: TGSI_INTERPOLATE_PERSPECTIVE
;
894 inputMapping
[attr
] = -1;
899 * Semantics and mapping for outputs
901 GLbitfield64 outputsWritten
= stfp
->Base
.info
.outputs_written
;
903 /* if z is written, emit that first */
904 if (outputsWritten
& BITFIELD64_BIT(FRAG_RESULT_DEPTH
)) {
905 fs_output_semantic_name
[fs_num_outputs
] = TGSI_SEMANTIC_POSITION
;
906 fs_output_semantic_index
[fs_num_outputs
] = 0;
907 outputMapping
[FRAG_RESULT_DEPTH
] = fs_num_outputs
;
909 outputsWritten
&= ~(1 << FRAG_RESULT_DEPTH
);
912 if (outputsWritten
& BITFIELD64_BIT(FRAG_RESULT_STENCIL
)) {
913 fs_output_semantic_name
[fs_num_outputs
] = TGSI_SEMANTIC_STENCIL
;
914 fs_output_semantic_index
[fs_num_outputs
] = 0;
915 outputMapping
[FRAG_RESULT_STENCIL
] = fs_num_outputs
;
917 outputsWritten
&= ~(1 << FRAG_RESULT_STENCIL
);
920 if (outputsWritten
& BITFIELD64_BIT(FRAG_RESULT_SAMPLE_MASK
)) {
921 fs_output_semantic_name
[fs_num_outputs
] = TGSI_SEMANTIC_SAMPLEMASK
;
922 fs_output_semantic_index
[fs_num_outputs
] = 0;
923 outputMapping
[FRAG_RESULT_SAMPLE_MASK
] = fs_num_outputs
;
925 outputsWritten
&= ~(1 << FRAG_RESULT_SAMPLE_MASK
);
928 /* handle remaining outputs (color) */
929 for (attr
= 0; attr
< ARRAY_SIZE(outputMapping
); attr
++) {
930 const GLbitfield64 written
= attr
< FRAG_RESULT_MAX
? outputsWritten
:
931 stfp
->Base
.SecondaryOutputsWritten
;
932 const unsigned loc
= attr
% FRAG_RESULT_MAX
;
934 if (written
& BITFIELD64_BIT(loc
)) {
936 case FRAG_RESULT_DEPTH
:
937 case FRAG_RESULT_STENCIL
:
938 case FRAG_RESULT_SAMPLE_MASK
:
942 case FRAG_RESULT_COLOR
:
943 write_all
= GL_TRUE
; /* fallthrough */
946 assert(loc
== FRAG_RESULT_COLOR
||
947 (FRAG_RESULT_DATA0
<= loc
&& loc
< FRAG_RESULT_MAX
));
949 index
= (loc
== FRAG_RESULT_COLOR
) ? 0 : (loc
- FRAG_RESULT_DATA0
);
951 if (attr
>= FRAG_RESULT_MAX
) {
952 /* Secondary color for dual source blending. */
957 fs_output_semantic_name
[fs_num_outputs
] = TGSI_SEMANTIC_COLOR
;
958 fs_output_semantic_index
[fs_num_outputs
] = index
;
959 outputMapping
[attr
] = fs_num_outputs
;
968 ureg
= ureg_create_with_screen(PIPE_SHADER_FRAGMENT
, st
->pipe
->screen
);
972 if (ST_DEBUG
& DEBUG_MESA
) {
973 _mesa_print_program(&stfp
->Base
);
974 _mesa_print_program_parameters(st
->ctx
, &stfp
->Base
);
977 if (write_all
== GL_TRUE
)
978 ureg_property(ureg
, TGSI_PROPERTY_FS_COLOR0_WRITES_ALL_CBUFS
, 1);
980 if (stfp
->Base
.info
.fs
.depth_layout
!= FRAG_DEPTH_LAYOUT_NONE
) {
981 switch (stfp
->Base
.info
.fs
.depth_layout
) {
982 case FRAG_DEPTH_LAYOUT_ANY
:
983 ureg_property(ureg
, TGSI_PROPERTY_FS_DEPTH_LAYOUT
,
984 TGSI_FS_DEPTH_LAYOUT_ANY
);
986 case FRAG_DEPTH_LAYOUT_GREATER
:
987 ureg_property(ureg
, TGSI_PROPERTY_FS_DEPTH_LAYOUT
,
988 TGSI_FS_DEPTH_LAYOUT_GREATER
);
990 case FRAG_DEPTH_LAYOUT_LESS
:
991 ureg_property(ureg
, TGSI_PROPERTY_FS_DEPTH_LAYOUT
,
992 TGSI_FS_DEPTH_LAYOUT_LESS
);
994 case FRAG_DEPTH_LAYOUT_UNCHANGED
:
995 ureg_property(ureg
, TGSI_PROPERTY_FS_DEPTH_LAYOUT
,
996 TGSI_FS_DEPTH_LAYOUT_UNCHANGED
);
1003 if (stfp
->glsl_to_tgsi
) {
1004 st_translate_program(st
->ctx
,
1005 PIPE_SHADER_FRAGMENT
,
1013 input_semantic_name
,
1014 input_semantic_index
,
1019 fs_output_semantic_name
,
1020 fs_output_semantic_index
);
1022 free_glsl_to_tgsi_visitor(stfp
->glsl_to_tgsi
);
1023 } else if (stfp
->ati_fs
)
1024 st_translate_atifs_program(ureg
,
1030 input_semantic_name
,
1031 input_semantic_index
,
1036 fs_output_semantic_name
,
1037 fs_output_semantic_index
);
1039 st_translate_mesa_program(st
->ctx
,
1040 PIPE_SHADER_FRAGMENT
,
1046 input_semantic_name
,
1047 input_semantic_index
,
1052 fs_output_semantic_name
,
1053 fs_output_semantic_index
);
1055 stfp
->tgsi
.tokens
= ureg_get_tokens(ureg
, &stfp
->num_tgsi_tokens
);
1058 if (stfp
->glsl_to_tgsi
) {
1059 stfp
->glsl_to_tgsi
= NULL
;
1060 st_store_ir_in_disk_cache(st
, &stfp
->Base
, false);
1063 return stfp
->tgsi
.tokens
!= NULL
;
1066 static struct st_fp_variant
*
1067 st_create_fp_variant(struct st_context
*st
,
1068 struct st_fragment_program
*stfp
,
1069 const struct st_fp_variant_key
*key
)
1071 struct pipe_context
*pipe
= st
->pipe
;
1072 struct st_fp_variant
*variant
= CALLOC_STRUCT(st_fp_variant
);
1073 struct pipe_shader_state tgsi
= {0};
1074 struct gl_program_parameter_list
*params
= stfp
->Base
.Parameters
;
1075 static const gl_state_index16 texcoord_state
[STATE_LENGTH
] =
1076 { STATE_INTERNAL
, STATE_CURRENT_ATTRIB
, VERT_ATTRIB_TEX0
};
1077 static const gl_state_index16 scale_state
[STATE_LENGTH
] =
1078 { STATE_INTERNAL
, STATE_PT_SCALE
};
1079 static const gl_state_index16 bias_state
[STATE_LENGTH
] =
1080 { STATE_INTERNAL
, STATE_PT_BIAS
};
1085 if (stfp
->tgsi
.type
== PIPE_SHADER_IR_NIR
) {
1086 tgsi
.type
= PIPE_SHADER_IR_NIR
;
1087 tgsi
.ir
.nir
= nir_shader_clone(NULL
, stfp
->tgsi
.ir
.nir
);
1089 if (key
->clamp_color
)
1090 NIR_PASS_V(tgsi
.ir
.nir
, nir_lower_clamp_color_outputs
);
1092 if (key
->persample_shading
) {
1093 nir_shader
*shader
= tgsi
.ir
.nir
;
1094 nir_foreach_variable(var
, &shader
->inputs
)
1095 var
->data
.sample
= true;
1098 assert(!(key
->bitmap
&& key
->drawpixels
));
1102 nir_lower_bitmap_options options
= {0};
1104 variant
->bitmap_sampler
= ffs(~stfp
->Base
.SamplersUsed
) - 1;
1105 options
.sampler
= variant
->bitmap_sampler
;
1106 options
.swizzle_xxxx
= (st
->bitmap
.tex_format
== PIPE_FORMAT_L8_UNORM
);
1108 NIR_PASS_V(tgsi
.ir
.nir
, nir_lower_bitmap
, &options
);
1111 /* glDrawPixels (color only) */
1112 if (key
->drawpixels
) {
1113 nir_lower_drawpixels_options options
= {{0}};
1114 unsigned samplers_used
= stfp
->Base
.SamplersUsed
;
1116 /* Find the first unused slot. */
1117 variant
->drawpix_sampler
= ffs(~samplers_used
) - 1;
1118 options
.drawpix_sampler
= variant
->drawpix_sampler
;
1119 samplers_used
|= (1 << variant
->drawpix_sampler
);
1121 options
.pixel_maps
= key
->pixelMaps
;
1122 if (key
->pixelMaps
) {
1123 variant
->pixelmap_sampler
= ffs(~samplers_used
) - 1;
1124 options
.pixelmap_sampler
= variant
->pixelmap_sampler
;
1127 options
.scale_and_bias
= key
->scaleAndBias
;
1128 if (key
->scaleAndBias
) {
1129 _mesa_add_state_reference(params
, scale_state
);
1130 memcpy(options
.scale_state_tokens
, scale_state
,
1131 sizeof(options
.scale_state_tokens
));
1132 _mesa_add_state_reference(params
, bias_state
);
1133 memcpy(options
.bias_state_tokens
, bias_state
,
1134 sizeof(options
.bias_state_tokens
));
1137 _mesa_add_state_reference(params
, texcoord_state
);
1138 memcpy(options
.texcoord_state_tokens
, texcoord_state
,
1139 sizeof(options
.texcoord_state_tokens
));
1141 NIR_PASS_V(tgsi
.ir
.nir
, nir_lower_drawpixels
, &options
);
1144 if (unlikely(key
->external
.lower_nv12
|| key
->external
.lower_iyuv
)) {
1145 nir_lower_tex_options options
= {0};
1146 options
.lower_y_uv_external
= key
->external
.lower_nv12
;
1147 options
.lower_y_u_v_external
= key
->external
.lower_iyuv
;
1148 NIR_PASS_V(tgsi
.ir
.nir
, nir_lower_tex
, &options
);
1151 st_finalize_nir(st
, &stfp
->Base
, stfp
->shader_program
, tgsi
.ir
.nir
);
1153 if (unlikely(key
->external
.lower_nv12
|| key
->external
.lower_iyuv
)) {
1154 /* This pass needs to happen *after* nir_lower_sampler */
1155 NIR_PASS_V(tgsi
.ir
.nir
, st_nir_lower_tex_src_plane
,
1156 ~stfp
->Base
.SamplersUsed
,
1157 key
->external
.lower_nv12
,
1158 key
->external
.lower_iyuv
);
1161 variant
->driver_shader
= pipe
->create_fs_state(pipe
, &tgsi
);
1162 variant
->key
= *key
;
1167 tgsi
.tokens
= stfp
->tgsi
.tokens
;
1169 assert(!(key
->bitmap
&& key
->drawpixels
));
1171 /* Fix texture targets and add fog for ATI_fs */
1173 const struct tgsi_token
*tokens
= st_fixup_atifs(tgsi
.tokens
, key
);
1176 tgsi
.tokens
= tokens
;
1178 fprintf(stderr
, "mesa: cannot post-process ATI_fs\n");
1181 /* Emulate features. */
1182 if (key
->clamp_color
|| key
->persample_shading
) {
1183 const struct tgsi_token
*tokens
;
1185 (key
->clamp_color
? TGSI_EMU_CLAMP_COLOR_OUTPUTS
: 0) |
1186 (key
->persample_shading
? TGSI_EMU_FORCE_PERSAMPLE_INTERP
: 0);
1188 tokens
= tgsi_emulate(tgsi
.tokens
, flags
);
1191 if (tgsi
.tokens
!= stfp
->tgsi
.tokens
)
1192 tgsi_free_tokens(tgsi
.tokens
);
1193 tgsi
.tokens
= tokens
;
1195 fprintf(stderr
, "mesa: cannot emulate deprecated features\n");
1200 const struct tgsi_token
*tokens
;
1202 variant
->bitmap_sampler
= ffs(~stfp
->Base
.SamplersUsed
) - 1;
1204 tokens
= st_get_bitmap_shader(tgsi
.tokens
,
1205 st
->internal_target
,
1206 variant
->bitmap_sampler
,
1207 st
->needs_texcoord_semantic
,
1208 st
->bitmap
.tex_format
==
1209 PIPE_FORMAT_L8_UNORM
);
1212 if (tgsi
.tokens
!= stfp
->tgsi
.tokens
)
1213 tgsi_free_tokens(tgsi
.tokens
);
1214 tgsi
.tokens
= tokens
;
1216 fprintf(stderr
, "mesa: cannot create a shader for glBitmap\n");
1219 /* glDrawPixels (color only) */
1220 if (key
->drawpixels
) {
1221 const struct tgsi_token
*tokens
;
1222 unsigned scale_const
= 0, bias_const
= 0, texcoord_const
= 0;
1224 /* Find the first unused slot. */
1225 variant
->drawpix_sampler
= ffs(~stfp
->Base
.SamplersUsed
) - 1;
1227 if (key
->pixelMaps
) {
1228 unsigned samplers_used
= stfp
->Base
.SamplersUsed
|
1229 (1 << variant
->drawpix_sampler
);
1231 variant
->pixelmap_sampler
= ffs(~samplers_used
) - 1;
1234 if (key
->scaleAndBias
) {
1235 scale_const
= _mesa_add_state_reference(params
, scale_state
);
1236 bias_const
= _mesa_add_state_reference(params
, bias_state
);
1239 texcoord_const
= _mesa_add_state_reference(params
, texcoord_state
);
1241 tokens
= st_get_drawpix_shader(tgsi
.tokens
,
1242 st
->needs_texcoord_semantic
,
1243 key
->scaleAndBias
, scale_const
,
1244 bias_const
, key
->pixelMaps
,
1245 variant
->drawpix_sampler
,
1246 variant
->pixelmap_sampler
,
1247 texcoord_const
, st
->internal_target
);
1250 if (tgsi
.tokens
!= stfp
->tgsi
.tokens
)
1251 tgsi_free_tokens(tgsi
.tokens
);
1252 tgsi
.tokens
= tokens
;
1254 fprintf(stderr
, "mesa: cannot create a shader for glDrawPixels\n");
1257 if (unlikely(key
->external
.lower_nv12
|| key
->external
.lower_iyuv
)) {
1258 const struct tgsi_token
*tokens
;
1260 /* samplers inserted would conflict, but this should be unpossible: */
1261 assert(!(key
->bitmap
|| key
->drawpixels
));
1263 tokens
= st_tgsi_lower_yuv(tgsi
.tokens
,
1264 ~stfp
->Base
.SamplersUsed
,
1265 key
->external
.lower_nv12
,
1266 key
->external
.lower_iyuv
);
1268 if (tgsi
.tokens
!= stfp
->tgsi
.tokens
)
1269 tgsi_free_tokens(tgsi
.tokens
);
1270 tgsi
.tokens
= tokens
;
1272 fprintf(stderr
, "mesa: cannot create a shader for samplerExternalOES\n");
1276 if (ST_DEBUG
& DEBUG_TGSI
) {
1277 tgsi_dump(tgsi
.tokens
, 0);
1281 /* fill in variant */
1282 variant
->driver_shader
= pipe
->create_fs_state(pipe
, &tgsi
);
1283 variant
->key
= *key
;
1285 if (tgsi
.tokens
!= stfp
->tgsi
.tokens
)
1286 tgsi_free_tokens(tgsi
.tokens
);
1291 * Translate fragment program if needed.
1293 struct st_fp_variant
*
1294 st_get_fp_variant(struct st_context
*st
,
1295 struct st_fragment_program
*stfp
,
1296 const struct st_fp_variant_key
*key
)
1298 struct st_fp_variant
*fpv
;
1300 /* Search for existing variant */
1301 for (fpv
= stfp
->variants
; fpv
; fpv
= fpv
->next
) {
1302 if (memcmp(&fpv
->key
, key
, sizeof(*key
)) == 0) {
1309 fpv
= st_create_fp_variant(st
, stfp
, key
);
1311 if (key
->bitmap
|| key
->drawpixels
) {
1312 /* Regular variants should always come before the
1313 * bitmap & drawpixels variants, (unless there
1314 * are no regular variants) so that
1315 * st_update_fp can take a fast path when
1316 * shader_has_one_variant is set.
1318 if (!stfp
->variants
) {
1319 stfp
->variants
= fpv
;
1321 /* insert into list after the first one */
1322 fpv
->next
= stfp
->variants
->next
;
1323 stfp
->variants
->next
= fpv
;
1326 /* insert into list */
1327 fpv
->next
= stfp
->variants
;
1328 stfp
->variants
= fpv
;
1338 * Translate a program. This is common code for geometry and tessellation
1342 st_translate_program_common(struct st_context
*st
,
1343 struct gl_program
*prog
,
1344 struct glsl_to_tgsi_visitor
*glsl_to_tgsi
,
1345 struct ureg_program
*ureg
,
1346 unsigned tgsi_processor
,
1347 struct pipe_shader_state
*out_state
)
1349 ubyte inputSlotToAttr
[VARYING_SLOT_TESS_MAX
];
1350 ubyte inputMapping
[VARYING_SLOT_TESS_MAX
];
1351 ubyte outputMapping
[VARYING_SLOT_TESS_MAX
];
1354 ubyte input_semantic_name
[PIPE_MAX_SHADER_INPUTS
];
1355 ubyte input_semantic_index
[PIPE_MAX_SHADER_INPUTS
];
1356 uint num_inputs
= 0;
1358 ubyte output_semantic_name
[PIPE_MAX_SHADER_OUTPUTS
];
1359 ubyte output_semantic_index
[PIPE_MAX_SHADER_OUTPUTS
];
1360 uint num_outputs
= 0;
1364 memset(inputSlotToAttr
, 0, sizeof(inputSlotToAttr
));
1365 memset(inputMapping
, 0, sizeof(inputMapping
));
1366 memset(outputMapping
, 0, sizeof(outputMapping
));
1367 memset(out_state
, 0, sizeof(*out_state
));
1369 if (prog
->info
.clip_distance_array_size
)
1370 ureg_property(ureg
, TGSI_PROPERTY_NUM_CLIPDIST_ENABLED
,
1371 prog
->info
.clip_distance_array_size
);
1372 if (prog
->info
.cull_distance_array_size
)
1373 ureg_property(ureg
, TGSI_PROPERTY_NUM_CULLDIST_ENABLED
,
1374 prog
->info
.cull_distance_array_size
);
1377 * Convert Mesa program inputs to TGSI input register semantics.
1379 for (attr
= 0; attr
< VARYING_SLOT_MAX
; attr
++) {
1380 if ((prog
->info
.inputs_read
& BITFIELD64_BIT(attr
)) == 0)
1383 unsigned slot
= num_inputs
++;
1385 inputMapping
[attr
] = slot
;
1386 inputSlotToAttr
[slot
] = attr
;
1388 unsigned semantic_name
, semantic_index
;
1389 tgsi_get_gl_varying_semantic(attr
, st
->needs_texcoord_semantic
,
1390 &semantic_name
, &semantic_index
);
1391 input_semantic_name
[slot
] = semantic_name
;
1392 input_semantic_index
[slot
] = semantic_index
;
1395 /* Also add patch inputs. */
1396 for (attr
= 0; attr
< 32; attr
++) {
1397 if (prog
->info
.patch_inputs_read
& (1u << attr
)) {
1398 GLuint slot
= num_inputs
++;
1399 GLuint patch_attr
= VARYING_SLOT_PATCH0
+ attr
;
1401 inputMapping
[patch_attr
] = slot
;
1402 inputSlotToAttr
[slot
] = patch_attr
;
1403 input_semantic_name
[slot
] = TGSI_SEMANTIC_PATCH
;
1404 input_semantic_index
[slot
] = attr
;
1408 /* initialize output semantics to defaults */
1409 for (i
= 0; i
< PIPE_MAX_SHADER_OUTPUTS
; i
++) {
1410 output_semantic_name
[i
] = TGSI_SEMANTIC_GENERIC
;
1411 output_semantic_index
[i
] = 0;
1415 * Determine number of outputs, the (default) output register
1416 * mapping and the semantic information for each output.
1418 for (attr
= 0; attr
< VARYING_SLOT_MAX
; attr
++) {
1419 if (prog
->info
.outputs_written
& BITFIELD64_BIT(attr
)) {
1420 GLuint slot
= num_outputs
++;
1422 outputMapping
[attr
] = slot
;
1424 unsigned semantic_name
, semantic_index
;
1425 tgsi_get_gl_varying_semantic(attr
, st
->needs_texcoord_semantic
,
1426 &semantic_name
, &semantic_index
);
1427 output_semantic_name
[slot
] = semantic_name
;
1428 output_semantic_index
[slot
] = semantic_index
;
1432 /* Also add patch outputs. */
1433 for (attr
= 0; attr
< 32; attr
++) {
1434 if (prog
->info
.patch_outputs_written
& (1u << attr
)) {
1435 GLuint slot
= num_outputs
++;
1436 GLuint patch_attr
= VARYING_SLOT_PATCH0
+ attr
;
1438 outputMapping
[patch_attr
] = slot
;
1439 output_semantic_name
[slot
] = TGSI_SEMANTIC_PATCH
;
1440 output_semantic_index
[slot
] = attr
;
1444 st_translate_program(st
->ctx
,
1453 input_semantic_name
,
1454 input_semantic_index
,
1459 output_semantic_name
,
1460 output_semantic_index
);
1462 if (tgsi_processor
== PIPE_SHADER_COMPUTE
) {
1463 struct st_compute_program
*stcp
= (struct st_compute_program
*) prog
;
1464 out_state
->tokens
= ureg_get_tokens(ureg
, &stcp
->num_tgsi_tokens
);
1465 stcp
->tgsi
.prog
= out_state
->tokens
;
1467 struct st_common_program
*stcp
= (struct st_common_program
*) prog
;
1468 out_state
->tokens
= ureg_get_tokens(ureg
, &stcp
->num_tgsi_tokens
);
1472 st_translate_stream_output_info(glsl_to_tgsi
,
1474 &out_state
->stream_output
);
1476 st_store_ir_in_disk_cache(st
, prog
, false);
1478 if ((ST_DEBUG
& DEBUG_TGSI
) && (ST_DEBUG
& DEBUG_MESA
)) {
1479 _mesa_print_program(prog
);
1483 if (ST_DEBUG
& DEBUG_TGSI
) {
1484 tgsi_dump(out_state
->tokens
, 0);
1490 * Update stream-output info for GS/TCS/TES. Normally this is done in
1491 * st_translate_program_common() but that is not called for glsl_to_nir
1495 st_translate_program_stream_output(struct gl_program
*prog
,
1496 struct pipe_stream_output_info
*stream_output
)
1498 if (!prog
->sh
.LinkedTransformFeedback
)
1501 ubyte outputMapping
[VARYING_SLOT_TESS_MAX
];
1503 uint num_outputs
= 0;
1505 memset(outputMapping
, 0, sizeof(outputMapping
));
1508 * Determine number of outputs, the (default) output register
1509 * mapping and the semantic information for each output.
1511 for (attr
= 0; attr
< VARYING_SLOT_MAX
; attr
++) {
1512 if (prog
->info
.outputs_written
& BITFIELD64_BIT(attr
)) {
1513 GLuint slot
= num_outputs
++;
1515 outputMapping
[attr
] = slot
;
1519 st_translate_stream_output_info2(prog
->sh
.LinkedTransformFeedback
,
1525 * Translate a geometry program to create a new variant.
1528 st_translate_geometry_program(struct st_context
*st
,
1529 struct st_common_program
*stgp
)
1531 struct ureg_program
*ureg
;
1533 /* We have already compiled to NIR so just return */
1534 if (stgp
->shader_program
) {
1536 st_finalize_nir(st
, &stgp
->Base
, stgp
->shader_program
,
1538 st_translate_program_stream_output(&stgp
->Base
, &stgp
->tgsi
.stream_output
);
1539 st_store_ir_in_disk_cache(st
, &stgp
->Base
, true);
1543 ureg
= ureg_create_with_screen(PIPE_SHADER_GEOMETRY
, st
->pipe
->screen
);
1547 ureg_property(ureg
, TGSI_PROPERTY_GS_INPUT_PRIM
,
1548 stgp
->Base
.info
.gs
.input_primitive
);
1549 ureg_property(ureg
, TGSI_PROPERTY_GS_OUTPUT_PRIM
,
1550 stgp
->Base
.info
.gs
.output_primitive
);
1551 ureg_property(ureg
, TGSI_PROPERTY_GS_MAX_OUTPUT_VERTICES
,
1552 stgp
->Base
.info
.gs
.vertices_out
);
1553 ureg_property(ureg
, TGSI_PROPERTY_GS_INVOCATIONS
,
1554 stgp
->Base
.info
.gs
.invocations
);
1556 st_translate_program_common(st
, &stgp
->Base
, stgp
->glsl_to_tgsi
, ureg
,
1557 PIPE_SHADER_GEOMETRY
, &stgp
->tgsi
);
1559 free_glsl_to_tgsi_visitor(stgp
->glsl_to_tgsi
);
1560 stgp
->glsl_to_tgsi
= NULL
;
1566 * Get/create a basic program variant.
1568 struct st_basic_variant
*
1569 st_get_basic_variant(struct st_context
*st
,
1570 unsigned pipe_shader
,
1571 struct st_common_program
*prog
)
1573 struct pipe_context
*pipe
= st
->pipe
;
1574 struct st_basic_variant
*v
;
1575 struct st_basic_variant_key key
;
1576 struct pipe_shader_state tgsi
= {0};
1577 memset(&key
, 0, sizeof(key
));
1578 key
.st
= st
->has_shareable_shaders
? NULL
: st
;
1580 /* Search for existing variant */
1581 for (v
= prog
->variants
; v
; v
= v
->next
) {
1582 if (memcmp(&v
->key
, &key
, sizeof(key
)) == 0) {
1589 v
= CALLOC_STRUCT(st_basic_variant
);
1592 if (prog
->tgsi
.type
== PIPE_SHADER_IR_NIR
) {
1593 tgsi
.type
= PIPE_SHADER_IR_NIR
;
1594 tgsi
.ir
.nir
= nir_shader_clone(NULL
, prog
->tgsi
.ir
.nir
);
1595 tgsi
.stream_output
= prog
->tgsi
.stream_output
;
1598 /* fill in new variant */
1599 switch (pipe_shader
) {
1600 case PIPE_SHADER_TESS_CTRL
:
1601 v
->driver_shader
= pipe
->create_tcs_state(pipe
, &tgsi
);
1603 case PIPE_SHADER_TESS_EVAL
:
1604 v
->driver_shader
= pipe
->create_tes_state(pipe
, &tgsi
);
1606 case PIPE_SHADER_GEOMETRY
:
1607 v
->driver_shader
= pipe
->create_gs_state(pipe
, &tgsi
);
1610 assert(!"unhandled shader type");
1617 /* insert into list */
1618 v
->next
= prog
->variants
;
1628 * Translate a tessellation control program to create a new variant.
1631 st_translate_tessctrl_program(struct st_context
*st
,
1632 struct st_common_program
*sttcp
)
1634 struct ureg_program
*ureg
;
1636 /* We have already compiled to NIR so just return */
1637 if (sttcp
->shader_program
) {
1639 st_finalize_nir(st
, &sttcp
->Base
, sttcp
->shader_program
,
1640 sttcp
->tgsi
.ir
.nir
);
1641 st_store_ir_in_disk_cache(st
, &sttcp
->Base
, true);
1645 ureg
= ureg_create_with_screen(PIPE_SHADER_TESS_CTRL
, st
->pipe
->screen
);
1649 ureg_property(ureg
, TGSI_PROPERTY_TCS_VERTICES_OUT
,
1650 sttcp
->Base
.info
.tess
.tcs_vertices_out
);
1652 st_translate_program_common(st
, &sttcp
->Base
, sttcp
->glsl_to_tgsi
, ureg
,
1653 PIPE_SHADER_TESS_CTRL
, &sttcp
->tgsi
);
1655 free_glsl_to_tgsi_visitor(sttcp
->glsl_to_tgsi
);
1656 sttcp
->glsl_to_tgsi
= NULL
;
1662 * Translate a tessellation evaluation program to create a new variant.
1665 st_translate_tesseval_program(struct st_context
*st
,
1666 struct st_common_program
*sttep
)
1668 struct ureg_program
*ureg
;
1670 /* We have already compiled to NIR so just return */
1671 if (sttep
->shader_program
) {
1673 st_finalize_nir(st
, &sttep
->Base
, sttep
->shader_program
,
1674 sttep
->tgsi
.ir
.nir
);
1675 st_translate_program_stream_output(&sttep
->Base
, &sttep
->tgsi
.stream_output
);
1676 st_store_ir_in_disk_cache(st
, &sttep
->Base
, true);
1680 ureg
= ureg_create_with_screen(PIPE_SHADER_TESS_EVAL
, st
->pipe
->screen
);
1684 if (sttep
->Base
.info
.tess
.primitive_mode
== GL_ISOLINES
)
1685 ureg_property(ureg
, TGSI_PROPERTY_TES_PRIM_MODE
, GL_LINES
);
1687 ureg_property(ureg
, TGSI_PROPERTY_TES_PRIM_MODE
,
1688 sttep
->Base
.info
.tess
.primitive_mode
);
1690 STATIC_ASSERT((TESS_SPACING_EQUAL
+ 1) % 3 == PIPE_TESS_SPACING_EQUAL
);
1691 STATIC_ASSERT((TESS_SPACING_FRACTIONAL_ODD
+ 1) % 3 ==
1692 PIPE_TESS_SPACING_FRACTIONAL_ODD
);
1693 STATIC_ASSERT((TESS_SPACING_FRACTIONAL_EVEN
+ 1) % 3 ==
1694 PIPE_TESS_SPACING_FRACTIONAL_EVEN
);
1696 ureg_property(ureg
, TGSI_PROPERTY_TES_SPACING
,
1697 (sttep
->Base
.info
.tess
.spacing
+ 1) % 3);
1699 ureg_property(ureg
, TGSI_PROPERTY_TES_VERTEX_ORDER_CW
,
1700 !sttep
->Base
.info
.tess
.ccw
);
1701 ureg_property(ureg
, TGSI_PROPERTY_TES_POINT_MODE
,
1702 sttep
->Base
.info
.tess
.point_mode
);
1704 st_translate_program_common(st
, &sttep
->Base
, sttep
->glsl_to_tgsi
,
1705 ureg
, PIPE_SHADER_TESS_EVAL
, &sttep
->tgsi
);
1707 free_glsl_to_tgsi_visitor(sttep
->glsl_to_tgsi
);
1708 sttep
->glsl_to_tgsi
= NULL
;
1714 * Translate a compute program to create a new variant.
1717 st_translate_compute_program(struct st_context
*st
,
1718 struct st_compute_program
*stcp
)
1720 struct ureg_program
*ureg
;
1721 struct pipe_shader_state prog
;
1723 stcp
->tgsi
.req_local_mem
= stcp
->Base
.info
.cs
.shared_size
;
1725 if (stcp
->shader_program
) {
1726 /* no compute variants: */
1727 st_finalize_nir(st
, &stcp
->Base
, stcp
->shader_program
,
1728 (struct nir_shader
*) stcp
->tgsi
.prog
);
1729 st_store_ir_in_disk_cache(st
, &stcp
->Base
, true);
1733 ureg
= ureg_create_with_screen(PIPE_SHADER_COMPUTE
, st
->pipe
->screen
);
1737 st_translate_program_common(st
, &stcp
->Base
, stcp
->glsl_to_tgsi
, ureg
,
1738 PIPE_SHADER_COMPUTE
, &prog
);
1740 stcp
->tgsi
.ir_type
= PIPE_SHADER_IR_TGSI
;
1741 stcp
->tgsi
.req_private_mem
= 0;
1742 stcp
->tgsi
.req_input_mem
= 0;
1744 free_glsl_to_tgsi_visitor(stcp
->glsl_to_tgsi
);
1745 stcp
->glsl_to_tgsi
= NULL
;
1751 * Get/create compute program variant.
1753 struct st_basic_variant
*
1754 st_get_cp_variant(struct st_context
*st
,
1755 struct pipe_compute_state
*tgsi
,
1756 struct st_basic_variant
**variants
)
1758 struct pipe_context
*pipe
= st
->pipe
;
1759 struct st_basic_variant
*v
;
1760 struct st_basic_variant_key key
;
1762 memset(&key
, 0, sizeof(key
));
1763 key
.st
= st
->has_shareable_shaders
? NULL
: st
;
1765 /* Search for existing variant */
1766 for (v
= *variants
; v
; v
= v
->next
) {
1767 if (memcmp(&v
->key
, &key
, sizeof(key
)) == 0) {
1774 v
= CALLOC_STRUCT(st_basic_variant
);
1776 /* fill in new variant */
1777 struct pipe_compute_state cs
= *tgsi
;
1778 if (tgsi
->ir_type
== PIPE_SHADER_IR_NIR
)
1779 cs
.prog
= nir_shader_clone(NULL
, tgsi
->prog
);
1780 v
->driver_shader
= pipe
->create_compute_state(pipe
, &cs
);
1783 /* insert into list */
1784 v
->next
= *variants
;
1794 * Vert/Geom/Frag programs have per-context variants. Free all the
1795 * variants attached to the given program which match the given context.
1798 destroy_program_variants(struct st_context
*st
, struct gl_program
*target
)
1800 if (!target
|| target
== &_mesa_DummyProgram
)
1803 switch (target
->Target
) {
1804 case GL_VERTEX_PROGRAM_ARB
:
1806 struct st_vertex_program
*stvp
= (struct st_vertex_program
*) target
;
1807 struct st_vp_variant
*vpv
, **prevPtr
= &stvp
->variants
;
1809 for (vpv
= stvp
->variants
; vpv
; ) {
1810 struct st_vp_variant
*next
= vpv
->next
;
1811 if (vpv
->key
.st
== st
) {
1812 /* unlink from list */
1814 /* destroy this variant */
1815 delete_vp_variant(st
, vpv
);
1818 prevPtr
= &vpv
->next
;
1824 case GL_FRAGMENT_PROGRAM_ARB
:
1826 struct st_fragment_program
*stfp
=
1827 (struct st_fragment_program
*) target
;
1828 struct st_fp_variant
*fpv
, **prevPtr
= &stfp
->variants
;
1830 for (fpv
= stfp
->variants
; fpv
; ) {
1831 struct st_fp_variant
*next
= fpv
->next
;
1832 if (fpv
->key
.st
== st
) {
1833 /* unlink from list */
1835 /* destroy this variant */
1836 delete_fp_variant(st
, fpv
);
1839 prevPtr
= &fpv
->next
;
1845 case GL_GEOMETRY_PROGRAM_NV
:
1846 case GL_TESS_CONTROL_PROGRAM_NV
:
1847 case GL_TESS_EVALUATION_PROGRAM_NV
:
1848 case GL_COMPUTE_PROGRAM_NV
:
1850 struct st_common_program
*p
= st_common_program(target
);
1851 struct st_compute_program
*cp
= (struct st_compute_program
*)target
;
1852 struct st_basic_variant
**variants
=
1853 target
->Target
== GL_COMPUTE_PROGRAM_NV
? &cp
->variants
:
1855 struct st_basic_variant
*v
, **prevPtr
= variants
;
1857 for (v
= *variants
; v
; ) {
1858 struct st_basic_variant
*next
= v
->next
;
1859 if (v
->key
.st
== st
) {
1860 /* unlink from list */
1862 /* destroy this variant */
1863 delete_basic_variant(st
, v
, target
->Target
);
1873 _mesa_problem(NULL
, "Unexpected program target 0x%x in "
1874 "destroy_program_variants_cb()", target
->Target
);
1880 * Callback for _mesa_HashWalk. Free all the shader's program variants
1881 * which match the given context.
1884 destroy_shader_program_variants_cb(GLuint key
, void *data
, void *userData
)
1886 struct st_context
*st
= (struct st_context
*) userData
;
1887 struct gl_shader
*shader
= (struct gl_shader
*) data
;
1889 switch (shader
->Type
) {
1890 case GL_SHADER_PROGRAM_MESA
:
1892 struct gl_shader_program
*shProg
= (struct gl_shader_program
*) data
;
1895 for (i
= 0; i
< ARRAY_SIZE(shProg
->_LinkedShaders
); i
++) {
1896 if (shProg
->_LinkedShaders
[i
])
1897 destroy_program_variants(st
, shProg
->_LinkedShaders
[i
]->Program
);
1901 case GL_VERTEX_SHADER
:
1902 case GL_FRAGMENT_SHADER
:
1903 case GL_GEOMETRY_SHADER
:
1904 case GL_TESS_CONTROL_SHADER
:
1905 case GL_TESS_EVALUATION_SHADER
:
1906 case GL_COMPUTE_SHADER
:
1915 * Callback for _mesa_HashWalk. Free all the program variants which match
1916 * the given context.
1919 destroy_program_variants_cb(GLuint key
, void *data
, void *userData
)
1921 struct st_context
*st
= (struct st_context
*) userData
;
1922 struct gl_program
*program
= (struct gl_program
*) data
;
1923 destroy_program_variants(st
, program
);
1928 * Walk over all shaders and programs to delete any variants which
1929 * belong to the given context.
1930 * This is called during context tear-down.
1933 st_destroy_program_variants(struct st_context
*st
)
1935 /* If shaders can be shared with other contexts, the last context will
1936 * call DeleteProgram on all shaders, releasing everything.
1938 if (st
->has_shareable_shaders
)
1941 /* ARB vert/frag program */
1942 _mesa_HashWalk(st
->ctx
->Shared
->Programs
,
1943 destroy_program_variants_cb
, st
);
1945 /* GLSL vert/frag/geom shaders */
1946 _mesa_HashWalk(st
->ctx
->Shared
->ShaderObjects
,
1947 destroy_shader_program_variants_cb
, st
);
1952 * For debugging, print/dump the current vertex program.
1955 st_print_current_vertex_program(void)
1957 GET_CURRENT_CONTEXT(ctx
);
1959 if (ctx
->VertexProgram
._Current
) {
1960 struct st_vertex_program
*stvp
=
1961 (struct st_vertex_program
*) ctx
->VertexProgram
._Current
;
1962 struct st_vp_variant
*stv
;
1964 debug_printf("Vertex program %u\n", stvp
->Base
.Id
);
1966 for (stv
= stvp
->variants
; stv
; stv
= stv
->next
) {
1967 debug_printf("variant %p\n", stv
);
1968 tgsi_dump(stv
->tgsi
.tokens
, 0);
1975 * Compile one shader variant.
1978 st_precompile_shader_variant(struct st_context
*st
,
1979 struct gl_program
*prog
)
1981 switch (prog
->Target
) {
1982 case GL_VERTEX_PROGRAM_ARB
: {
1983 struct st_vertex_program
*p
= (struct st_vertex_program
*)prog
;
1984 struct st_vp_variant_key key
;
1986 memset(&key
, 0, sizeof(key
));
1987 key
.st
= st
->has_shareable_shaders
? NULL
: st
;
1988 st_get_vp_variant(st
, p
, &key
);
1992 case GL_TESS_CONTROL_PROGRAM_NV
: {
1993 struct st_common_program
*p
= st_common_program(prog
);
1994 st_get_basic_variant(st
, PIPE_SHADER_TESS_CTRL
, p
);
1998 case GL_TESS_EVALUATION_PROGRAM_NV
: {
1999 struct st_common_program
*p
= st_common_program(prog
);
2000 st_get_basic_variant(st
, PIPE_SHADER_TESS_EVAL
, p
);
2004 case GL_GEOMETRY_PROGRAM_NV
: {
2005 struct st_common_program
*p
= st_common_program(prog
);
2006 st_get_basic_variant(st
, PIPE_SHADER_GEOMETRY
, p
);
2010 case GL_FRAGMENT_PROGRAM_ARB
: {
2011 struct st_fragment_program
*p
= (struct st_fragment_program
*)prog
;
2012 struct st_fp_variant_key key
;
2014 memset(&key
, 0, sizeof(key
));
2015 key
.st
= st
->has_shareable_shaders
? NULL
: st
;
2016 st_get_fp_variant(st
, p
, &key
);
2020 case GL_COMPUTE_PROGRAM_NV
: {
2021 struct st_compute_program
*p
= (struct st_compute_program
*)prog
;
2022 st_get_cp_variant(st
, &p
->tgsi
, &p
->variants
);