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 enum pipe_shader_type p_stage
= stage
; /* valid for VS/FS */
389 const bool is_scalar
=
390 st
->pipe
->screen
->get_shader_param(st
->pipe
->screen
, p_stage
,
391 PIPE_SHADER_CAP_SCALAR_ISA
);
393 const struct gl_shader_compiler_options
*options
=
394 &st
->ctx
->Const
.ShaderCompilerOptions
[stage
];
396 /* Translate to NIR */
397 nir_shader
*nir
= prog_to_nir(prog
, options
->NirOptions
);
398 NIR_PASS_V(nir
, nir_lower_regs_to_ssa
); /* turn registers into SSA */
399 nir_validate_shader(nir
, "after st/ptn lower_regs_to_ssa");
401 NIR_PASS_V(nir
, st_nir_lower_wpos_ytransform
, prog
, st
->pipe
->screen
);
402 NIR_PASS_V(nir
, nir_lower_system_values
);
405 NIR_PASS_V(nir
, nir_opt_constant_folding
);
406 st_nir_opts(nir
, is_scalar
);
407 nir_validate_shader(nir
, "after st/ptn NIR opts");
413 * Translate a vertex program.
416 st_translate_vertex_program(struct st_context
*st
,
417 struct st_vertex_program
*stvp
)
419 struct ureg_program
*ureg
;
420 enum pipe_error error
;
421 unsigned num_outputs
= 0;
423 ubyte output_semantic_name
[VARYING_SLOT_MAX
] = {0};
424 ubyte output_semantic_index
[VARYING_SLOT_MAX
] = {0};
426 stvp
->num_inputs
= 0;
427 memset(stvp
->input_to_index
, ~0, sizeof(stvp
->input_to_index
));
429 if (stvp
->Base
.arb
.IsPositionInvariant
)
430 _mesa_insert_mvp_code(st
->ctx
, &stvp
->Base
);
433 * Determine number of inputs, the mappings between VERT_ATTRIB_x
434 * and TGSI generic input indexes, plus input attrib semantic info.
436 for (attr
= 0; attr
< VERT_ATTRIB_MAX
; attr
++) {
437 if ((stvp
->Base
.info
.inputs_read
& BITFIELD64_BIT(attr
)) != 0) {
438 stvp
->input_to_index
[attr
] = stvp
->num_inputs
;
439 stvp
->index_to_input
[stvp
->num_inputs
] = attr
;
441 if ((stvp
->Base
.DualSlotInputs
& BITFIELD64_BIT(attr
)) != 0) {
442 /* add placeholder for second part of a double attribute */
443 stvp
->index_to_input
[stvp
->num_inputs
] = ST_DOUBLE_ATTRIB_PLACEHOLDER
;
448 /* bit of a hack, presetup potentially unused edgeflag input */
449 stvp
->input_to_index
[VERT_ATTRIB_EDGEFLAG
] = stvp
->num_inputs
;
450 stvp
->index_to_input
[stvp
->num_inputs
] = VERT_ATTRIB_EDGEFLAG
;
452 /* Compute mapping of vertex program outputs to slots.
454 for (attr
= 0; attr
< VARYING_SLOT_MAX
; attr
++) {
455 if ((stvp
->Base
.info
.outputs_written
& BITFIELD64_BIT(attr
)) == 0) {
456 stvp
->result_to_output
[attr
] = ~0;
459 unsigned slot
= num_outputs
++;
461 stvp
->result_to_output
[attr
] = slot
;
463 unsigned semantic_name
, semantic_index
;
464 tgsi_get_gl_varying_semantic(attr
, st
->needs_texcoord_semantic
,
465 &semantic_name
, &semantic_index
);
466 output_semantic_name
[slot
] = semantic_name
;
467 output_semantic_index
[slot
] = semantic_index
;
470 /* similar hack to above, presetup potentially unused edgeflag output */
471 stvp
->result_to_output
[VARYING_SLOT_EDGE
] = num_outputs
;
472 output_semantic_name
[num_outputs
] = TGSI_SEMANTIC_EDGEFLAG
;
473 output_semantic_index
[num_outputs
] = 0;
476 if (!stvp
->glsl_to_tgsi
&& !stvp
->shader_program
) {
477 _mesa_remove_output_reads(&stvp
->Base
, PROGRAM_OUTPUT
);
479 /* This determines which states will be updated when the assembly
482 stvp
->affected_states
= ST_NEW_VS_STATE
|
484 ST_NEW_VERTEX_ARRAYS
;
486 if (stvp
->Base
.Parameters
->NumParameters
)
487 stvp
->affected_states
|= ST_NEW_VS_CONSTANTS
;
489 /* No samplers are allowed in ARB_vp. */
492 if (stvp
->shader_program
) {
493 st_translate_stream_output_info(stvp
->Base
.sh
.LinkedTransformFeedback
,
494 stvp
->result_to_output
,
495 &stvp
->tgsi
.stream_output
);
497 st_store_ir_in_disk_cache(st
, &stvp
->Base
, true);
501 ureg
= ureg_create_with_screen(PIPE_SHADER_VERTEX
, st
->pipe
->screen
);
505 if (stvp
->Base
.info
.clip_distance_array_size
)
506 ureg_property(ureg
, TGSI_PROPERTY_NUM_CLIPDIST_ENABLED
,
507 stvp
->Base
.info
.clip_distance_array_size
);
508 if (stvp
->Base
.info
.cull_distance_array_size
)
509 ureg_property(ureg
, TGSI_PROPERTY_NUM_CULLDIST_ENABLED
,
510 stvp
->Base
.info
.cull_distance_array_size
);
512 if (ST_DEBUG
& DEBUG_MESA
) {
513 _mesa_print_program(&stvp
->Base
);
514 _mesa_print_program_parameters(st
->ctx
, &stvp
->Base
);
518 if (stvp
->glsl_to_tgsi
) {
519 error
= st_translate_program(st
->ctx
,
526 stvp
->input_to_index
,
527 NULL
, /* inputSlotToAttr */
528 NULL
, /* input semantic name */
529 NULL
, /* input semantic index */
530 NULL
, /* interp mode */
533 stvp
->result_to_output
,
534 output_semantic_name
,
535 output_semantic_index
);
537 st_translate_stream_output_info(stvp
->Base
.sh
.LinkedTransformFeedback
,
538 stvp
->result_to_output
,
539 &stvp
->tgsi
.stream_output
);
541 free_glsl_to_tgsi_visitor(stvp
->glsl_to_tgsi
);
543 error
= st_translate_mesa_program(st
->ctx
,
549 stvp
->input_to_index
,
550 NULL
, /* input semantic name */
551 NULL
, /* input semantic index */
555 stvp
->result_to_output
,
556 output_semantic_name
,
557 output_semantic_index
);
560 debug_printf("%s: failed to translate Mesa program:\n", __func__
);
561 _mesa_print_program(&stvp
->Base
);
566 stvp
->tgsi
.tokens
= ureg_get_tokens(ureg
, &stvp
->num_tgsi_tokens
);
569 if (stvp
->glsl_to_tgsi
) {
570 stvp
->glsl_to_tgsi
= NULL
;
571 st_store_ir_in_disk_cache(st
, &stvp
->Base
, false);
574 bool use_nir
= PIPE_SHADER_IR_NIR
==
575 st
->pipe
->screen
->get_shader_param(st
->pipe
->screen
, PIPE_SHADER_VERTEX
,
576 PIPE_SHADER_CAP_PREFERRED_IR
);
580 st_translate_prog_to_nir(st
, &stvp
->Base
, MESA_SHADER_VERTEX
);
582 stvp
->tgsi
.type
= PIPE_SHADER_IR_NIR
;
583 stvp
->tgsi
.ir
.nir
= nir
;
587 return stvp
->tgsi
.tokens
!= NULL
;
590 static struct st_vp_variant
*
591 st_create_vp_variant(struct st_context
*st
,
592 struct st_vertex_program
*stvp
,
593 const struct st_vp_variant_key
*key
)
595 struct st_vp_variant
*vpv
= CALLOC_STRUCT(st_vp_variant
);
596 struct pipe_context
*pipe
= st
->pipe
;
599 vpv
->tgsi
.stream_output
= stvp
->tgsi
.stream_output
;
600 vpv
->num_inputs
= stvp
->num_inputs
;
602 /* When generating a NIR program, we usually don't have TGSI tokens.
603 * However, we do create them for ARB_vertex_program / fixed-function VS
604 * programs which we may need to use with the draw module for legacy
605 * feedback/select emulation. If they exist, copy them.
607 if (stvp
->tgsi
.tokens
)
608 vpv
->tgsi
.tokens
= tgsi_dup_tokens(stvp
->tgsi
.tokens
);
610 if (stvp
->tgsi
.type
== PIPE_SHADER_IR_NIR
) {
611 vpv
->tgsi
.type
= PIPE_SHADER_IR_NIR
;
612 vpv
->tgsi
.ir
.nir
= nir_shader_clone(NULL
, stvp
->tgsi
.ir
.nir
);
613 if (key
->clamp_color
)
614 NIR_PASS_V(vpv
->tgsi
.ir
.nir
, nir_lower_clamp_color_outputs
);
615 if (key
->passthrough_edgeflags
) {
616 NIR_PASS_V(vpv
->tgsi
.ir
.nir
, nir_lower_passthrough_edgeflags
);
620 st_finalize_nir(st
, &stvp
->Base
, stvp
->shader_program
,
623 vpv
->driver_shader
= pipe
->create_vs_state(pipe
, &vpv
->tgsi
);
624 /* driver takes ownership of IR: */
625 vpv
->tgsi
.ir
.nir
= NULL
;
629 /* Emulate features. */
630 if (key
->clamp_color
|| key
->passthrough_edgeflags
) {
631 const struct tgsi_token
*tokens
;
633 (key
->clamp_color
? TGSI_EMU_CLAMP_COLOR_OUTPUTS
: 0) |
634 (key
->passthrough_edgeflags
? TGSI_EMU_PASSTHROUGH_EDGEFLAG
: 0);
636 tokens
= tgsi_emulate(vpv
->tgsi
.tokens
, flags
);
639 tgsi_free_tokens(vpv
->tgsi
.tokens
);
640 vpv
->tgsi
.tokens
= tokens
;
642 if (key
->passthrough_edgeflags
)
645 fprintf(stderr
, "mesa: cannot emulate deprecated features\n");
648 if (ST_DEBUG
& DEBUG_TGSI
) {
649 tgsi_dump(vpv
->tgsi
.tokens
, 0);
653 vpv
->driver_shader
= pipe
->create_vs_state(pipe
, &vpv
->tgsi
);
659 * Find/create a vertex program variant.
661 struct st_vp_variant
*
662 st_get_vp_variant(struct st_context
*st
,
663 struct st_vertex_program
*stvp
,
664 const struct st_vp_variant_key
*key
)
666 struct st_vp_variant
*vpv
;
668 /* Search for existing variant */
669 for (vpv
= stvp
->variants
; vpv
; vpv
= vpv
->next
) {
670 if (memcmp(&vpv
->key
, key
, sizeof(*key
)) == 0) {
677 vpv
= st_create_vp_variant(st
, stvp
, key
);
679 for (unsigned index
= 0; index
< vpv
->num_inputs
; ++index
) {
680 unsigned attr
= stvp
->index_to_input
[index
];
681 if (attr
== ST_DOUBLE_ATTRIB_PLACEHOLDER
)
683 vpv
->vert_attrib_mask
|= 1u << attr
;
686 /* insert into list */
687 vpv
->next
= stvp
->variants
;
688 stvp
->variants
= vpv
;
697 * Translate a Mesa fragment shader into a TGSI shader.
700 st_translate_fragment_program(struct st_context
*st
,
701 struct st_fragment_program
*stfp
)
703 /* We have already compiled to NIR so just return */
704 if (stfp
->shader_program
) {
705 st_store_ir_in_disk_cache(st
, &stfp
->Base
, true);
709 ubyte outputMapping
[2 * FRAG_RESULT_MAX
];
710 ubyte inputMapping
[VARYING_SLOT_MAX
];
711 ubyte inputSlotToAttr
[VARYING_SLOT_MAX
];
712 ubyte interpMode
[PIPE_MAX_SHADER_INPUTS
]; /* XXX size? */
714 GLbitfield64 inputsRead
;
715 struct ureg_program
*ureg
;
717 GLboolean write_all
= GL_FALSE
;
719 ubyte input_semantic_name
[PIPE_MAX_SHADER_INPUTS
];
720 ubyte input_semantic_index
[PIPE_MAX_SHADER_INPUTS
];
721 uint fs_num_inputs
= 0;
723 ubyte fs_output_semantic_name
[PIPE_MAX_SHADER_OUTPUTS
];
724 ubyte fs_output_semantic_index
[PIPE_MAX_SHADER_OUTPUTS
];
725 uint fs_num_outputs
= 0;
727 memset(inputSlotToAttr
, ~0, sizeof(inputSlotToAttr
));
729 /* Non-GLSL programs: */
730 if (!stfp
->glsl_to_tgsi
&& !stfp
->shader_program
) {
731 _mesa_remove_output_reads(&stfp
->Base
, PROGRAM_OUTPUT
);
732 if (st
->ctx
->Const
.GLSLFragCoordIsSysVal
)
733 _mesa_program_fragment_position_to_sysval(&stfp
->Base
);
735 /* This determines which states will be updated when the assembly
738 * fragment.position and glDrawPixels always use constants.
740 stfp
->affected_states
= ST_NEW_FS_STATE
|
741 ST_NEW_SAMPLE_SHADING
|
745 /* Just set them for ATI_fs unconditionally. */
746 stfp
->affected_states
|= ST_NEW_FS_SAMPLER_VIEWS
|
750 if (stfp
->Base
.SamplersUsed
)
751 stfp
->affected_states
|= ST_NEW_FS_SAMPLER_VIEWS
|
757 bool use_nir
= PIPE_SHADER_IR_NIR
==
758 st
->pipe
->screen
->get_shader_param(st
->pipe
->screen
,
759 PIPE_SHADER_FRAGMENT
,
760 PIPE_SHADER_CAP_PREFERRED_IR
);
762 if (use_nir
&& !stfp
->ati_fs
) {
764 st_translate_prog_to_nir(st
, &stfp
->Base
, MESA_SHADER_FRAGMENT
);
766 stfp
->tgsi
.type
= PIPE_SHADER_IR_NIR
;
767 stfp
->tgsi
.ir
.nir
= nir
;
772 * Convert Mesa program inputs to TGSI input register semantics.
774 inputsRead
= stfp
->Base
.info
.inputs_read
;
775 for (attr
= 0; attr
< VARYING_SLOT_MAX
; attr
++) {
776 if ((inputsRead
& BITFIELD64_BIT(attr
)) != 0) {
777 const GLuint slot
= fs_num_inputs
++;
779 inputMapping
[attr
] = slot
;
780 inputSlotToAttr
[slot
] = attr
;
783 case VARYING_SLOT_POS
:
784 input_semantic_name
[slot
] = TGSI_SEMANTIC_POSITION
;
785 input_semantic_index
[slot
] = 0;
786 interpMode
[slot
] = TGSI_INTERPOLATE_LINEAR
;
788 case VARYING_SLOT_COL0
:
789 input_semantic_name
[slot
] = TGSI_SEMANTIC_COLOR
;
790 input_semantic_index
[slot
] = 0;
791 interpMode
[slot
] = stfp
->glsl_to_tgsi
?
792 TGSI_INTERPOLATE_COUNT
: TGSI_INTERPOLATE_COLOR
;
794 case VARYING_SLOT_COL1
:
795 input_semantic_name
[slot
] = TGSI_SEMANTIC_COLOR
;
796 input_semantic_index
[slot
] = 1;
797 interpMode
[slot
] = stfp
->glsl_to_tgsi
?
798 TGSI_INTERPOLATE_COUNT
: TGSI_INTERPOLATE_COLOR
;
800 case VARYING_SLOT_FOGC
:
801 input_semantic_name
[slot
] = TGSI_SEMANTIC_FOG
;
802 input_semantic_index
[slot
] = 0;
803 interpMode
[slot
] = TGSI_INTERPOLATE_PERSPECTIVE
;
805 case VARYING_SLOT_FACE
:
806 input_semantic_name
[slot
] = TGSI_SEMANTIC_FACE
;
807 input_semantic_index
[slot
] = 0;
808 interpMode
[slot
] = TGSI_INTERPOLATE_CONSTANT
;
810 case VARYING_SLOT_PRIMITIVE_ID
:
811 input_semantic_name
[slot
] = TGSI_SEMANTIC_PRIMID
;
812 input_semantic_index
[slot
] = 0;
813 interpMode
[slot
] = TGSI_INTERPOLATE_CONSTANT
;
815 case VARYING_SLOT_LAYER
:
816 input_semantic_name
[slot
] = TGSI_SEMANTIC_LAYER
;
817 input_semantic_index
[slot
] = 0;
818 interpMode
[slot
] = TGSI_INTERPOLATE_CONSTANT
;
820 case VARYING_SLOT_VIEWPORT
:
821 input_semantic_name
[slot
] = TGSI_SEMANTIC_VIEWPORT_INDEX
;
822 input_semantic_index
[slot
] = 0;
823 interpMode
[slot
] = TGSI_INTERPOLATE_CONSTANT
;
825 case VARYING_SLOT_CLIP_DIST0
:
826 input_semantic_name
[slot
] = TGSI_SEMANTIC_CLIPDIST
;
827 input_semantic_index
[slot
] = 0;
828 interpMode
[slot
] = TGSI_INTERPOLATE_PERSPECTIVE
;
830 case VARYING_SLOT_CLIP_DIST1
:
831 input_semantic_name
[slot
] = TGSI_SEMANTIC_CLIPDIST
;
832 input_semantic_index
[slot
] = 1;
833 interpMode
[slot
] = TGSI_INTERPOLATE_PERSPECTIVE
;
835 case VARYING_SLOT_CULL_DIST0
:
836 case VARYING_SLOT_CULL_DIST1
:
837 /* these should have been lowered by GLSL */
840 /* In most cases, there is nothing special about these
841 * inputs, so adopt a convention to use the generic
842 * semantic name and the mesa VARYING_SLOT_ number as the
845 * All that is required is that the vertex shader labels
846 * its own outputs similarly, and that the vertex shader
847 * generates at least every output required by the
848 * fragment shader plus fixed-function hardware (such as
851 * However, some drivers may need us to identify the PNTC and TEXi
852 * varyings if, for example, their capability to replace them with
853 * sprite coordinates is limited.
855 case VARYING_SLOT_PNTC
:
856 if (st
->needs_texcoord_semantic
) {
857 input_semantic_name
[slot
] = TGSI_SEMANTIC_PCOORD
;
858 input_semantic_index
[slot
] = 0;
859 interpMode
[slot
] = TGSI_INTERPOLATE_LINEAR
;
863 case VARYING_SLOT_TEX0
:
864 case VARYING_SLOT_TEX1
:
865 case VARYING_SLOT_TEX2
:
866 case VARYING_SLOT_TEX3
:
867 case VARYING_SLOT_TEX4
:
868 case VARYING_SLOT_TEX5
:
869 case VARYING_SLOT_TEX6
:
870 case VARYING_SLOT_TEX7
:
871 if (st
->needs_texcoord_semantic
) {
872 input_semantic_name
[slot
] = TGSI_SEMANTIC_TEXCOORD
;
873 input_semantic_index
[slot
] = attr
- VARYING_SLOT_TEX0
;
874 interpMode
[slot
] = stfp
->glsl_to_tgsi
?
875 TGSI_INTERPOLATE_COUNT
: TGSI_INTERPOLATE_PERSPECTIVE
;
879 case VARYING_SLOT_VAR0
:
881 /* Semantic indices should be zero-based because drivers may choose
882 * to assign a fixed slot determined by that index.
883 * This is useful because ARB_separate_shader_objects uses location
884 * qualifiers for linkage, and if the semantic index corresponds to
885 * these locations, linkage passes in the driver become unecessary.
887 * If needs_texcoord_semantic is true, no semantic indices will be
888 * consumed for the TEXi varyings, and we can base the locations of
889 * the user varyings on VAR0. Otherwise, we use TEX0 as base index.
891 assert(attr
>= VARYING_SLOT_VAR0
|| attr
== VARYING_SLOT_PNTC
||
892 (attr
>= VARYING_SLOT_TEX0
&& attr
<= VARYING_SLOT_TEX7
));
893 input_semantic_name
[slot
] = TGSI_SEMANTIC_GENERIC
;
894 input_semantic_index
[slot
] = st_get_generic_varying_index(st
, attr
);
895 if (attr
== VARYING_SLOT_PNTC
)
896 interpMode
[slot
] = TGSI_INTERPOLATE_LINEAR
;
898 interpMode
[slot
] = stfp
->glsl_to_tgsi
?
899 TGSI_INTERPOLATE_COUNT
: TGSI_INTERPOLATE_PERSPECTIVE
;
905 inputMapping
[attr
] = -1;
910 * Semantics and mapping for outputs
912 GLbitfield64 outputsWritten
= stfp
->Base
.info
.outputs_written
;
914 /* if z is written, emit that first */
915 if (outputsWritten
& BITFIELD64_BIT(FRAG_RESULT_DEPTH
)) {
916 fs_output_semantic_name
[fs_num_outputs
] = TGSI_SEMANTIC_POSITION
;
917 fs_output_semantic_index
[fs_num_outputs
] = 0;
918 outputMapping
[FRAG_RESULT_DEPTH
] = fs_num_outputs
;
920 outputsWritten
&= ~(1 << FRAG_RESULT_DEPTH
);
923 if (outputsWritten
& BITFIELD64_BIT(FRAG_RESULT_STENCIL
)) {
924 fs_output_semantic_name
[fs_num_outputs
] = TGSI_SEMANTIC_STENCIL
;
925 fs_output_semantic_index
[fs_num_outputs
] = 0;
926 outputMapping
[FRAG_RESULT_STENCIL
] = fs_num_outputs
;
928 outputsWritten
&= ~(1 << FRAG_RESULT_STENCIL
);
931 if (outputsWritten
& BITFIELD64_BIT(FRAG_RESULT_SAMPLE_MASK
)) {
932 fs_output_semantic_name
[fs_num_outputs
] = TGSI_SEMANTIC_SAMPLEMASK
;
933 fs_output_semantic_index
[fs_num_outputs
] = 0;
934 outputMapping
[FRAG_RESULT_SAMPLE_MASK
] = fs_num_outputs
;
936 outputsWritten
&= ~(1 << FRAG_RESULT_SAMPLE_MASK
);
939 /* handle remaining outputs (color) */
940 for (attr
= 0; attr
< ARRAY_SIZE(outputMapping
); attr
++) {
941 const GLbitfield64 written
= attr
< FRAG_RESULT_MAX
? outputsWritten
:
942 stfp
->Base
.SecondaryOutputsWritten
;
943 const unsigned loc
= attr
% FRAG_RESULT_MAX
;
945 if (written
& BITFIELD64_BIT(loc
)) {
947 case FRAG_RESULT_DEPTH
:
948 case FRAG_RESULT_STENCIL
:
949 case FRAG_RESULT_SAMPLE_MASK
:
953 case FRAG_RESULT_COLOR
:
954 write_all
= GL_TRUE
; /* fallthrough */
957 assert(loc
== FRAG_RESULT_COLOR
||
958 (FRAG_RESULT_DATA0
<= loc
&& loc
< FRAG_RESULT_MAX
));
960 index
= (loc
== FRAG_RESULT_COLOR
) ? 0 : (loc
- FRAG_RESULT_DATA0
);
962 if (attr
>= FRAG_RESULT_MAX
) {
963 /* Secondary color for dual source blending. */
968 fs_output_semantic_name
[fs_num_outputs
] = TGSI_SEMANTIC_COLOR
;
969 fs_output_semantic_index
[fs_num_outputs
] = index
;
970 outputMapping
[attr
] = fs_num_outputs
;
979 ureg
= ureg_create_with_screen(PIPE_SHADER_FRAGMENT
, st
->pipe
->screen
);
983 if (ST_DEBUG
& DEBUG_MESA
) {
984 _mesa_print_program(&stfp
->Base
);
985 _mesa_print_program_parameters(st
->ctx
, &stfp
->Base
);
988 if (write_all
== GL_TRUE
)
989 ureg_property(ureg
, TGSI_PROPERTY_FS_COLOR0_WRITES_ALL_CBUFS
, 1);
991 if (stfp
->Base
.info
.fs
.depth_layout
!= FRAG_DEPTH_LAYOUT_NONE
) {
992 switch (stfp
->Base
.info
.fs
.depth_layout
) {
993 case FRAG_DEPTH_LAYOUT_ANY
:
994 ureg_property(ureg
, TGSI_PROPERTY_FS_DEPTH_LAYOUT
,
995 TGSI_FS_DEPTH_LAYOUT_ANY
);
997 case FRAG_DEPTH_LAYOUT_GREATER
:
998 ureg_property(ureg
, TGSI_PROPERTY_FS_DEPTH_LAYOUT
,
999 TGSI_FS_DEPTH_LAYOUT_GREATER
);
1001 case FRAG_DEPTH_LAYOUT_LESS
:
1002 ureg_property(ureg
, TGSI_PROPERTY_FS_DEPTH_LAYOUT
,
1003 TGSI_FS_DEPTH_LAYOUT_LESS
);
1005 case FRAG_DEPTH_LAYOUT_UNCHANGED
:
1006 ureg_property(ureg
, TGSI_PROPERTY_FS_DEPTH_LAYOUT
,
1007 TGSI_FS_DEPTH_LAYOUT_UNCHANGED
);
1014 if (stfp
->glsl_to_tgsi
) {
1015 st_translate_program(st
->ctx
,
1016 PIPE_SHADER_FRAGMENT
,
1024 input_semantic_name
,
1025 input_semantic_index
,
1030 fs_output_semantic_name
,
1031 fs_output_semantic_index
);
1033 free_glsl_to_tgsi_visitor(stfp
->glsl_to_tgsi
);
1034 } else if (stfp
->ati_fs
)
1035 st_translate_atifs_program(ureg
,
1041 input_semantic_name
,
1042 input_semantic_index
,
1047 fs_output_semantic_name
,
1048 fs_output_semantic_index
);
1050 st_translate_mesa_program(st
->ctx
,
1051 PIPE_SHADER_FRAGMENT
,
1057 input_semantic_name
,
1058 input_semantic_index
,
1063 fs_output_semantic_name
,
1064 fs_output_semantic_index
);
1066 stfp
->tgsi
.tokens
= ureg_get_tokens(ureg
, &stfp
->num_tgsi_tokens
);
1069 if (stfp
->glsl_to_tgsi
) {
1070 stfp
->glsl_to_tgsi
= NULL
;
1071 st_store_ir_in_disk_cache(st
, &stfp
->Base
, false);
1074 return stfp
->tgsi
.tokens
!= NULL
;
1077 static struct st_fp_variant
*
1078 st_create_fp_variant(struct st_context
*st
,
1079 struct st_fragment_program
*stfp
,
1080 const struct st_fp_variant_key
*key
)
1082 struct pipe_context
*pipe
= st
->pipe
;
1083 struct st_fp_variant
*variant
= CALLOC_STRUCT(st_fp_variant
);
1084 struct pipe_shader_state tgsi
= {0};
1085 struct gl_program_parameter_list
*params
= stfp
->Base
.Parameters
;
1086 static const gl_state_index16 texcoord_state
[STATE_LENGTH
] =
1087 { STATE_INTERNAL
, STATE_CURRENT_ATTRIB
, VERT_ATTRIB_TEX0
};
1088 static const gl_state_index16 scale_state
[STATE_LENGTH
] =
1089 { STATE_INTERNAL
, STATE_PT_SCALE
};
1090 static const gl_state_index16 bias_state
[STATE_LENGTH
] =
1091 { STATE_INTERNAL
, STATE_PT_BIAS
};
1096 if (stfp
->tgsi
.type
== PIPE_SHADER_IR_NIR
) {
1097 tgsi
.type
= PIPE_SHADER_IR_NIR
;
1098 tgsi
.ir
.nir
= nir_shader_clone(NULL
, stfp
->tgsi
.ir
.nir
);
1100 if (key
->clamp_color
)
1101 NIR_PASS_V(tgsi
.ir
.nir
, nir_lower_clamp_color_outputs
);
1103 if (key
->persample_shading
) {
1104 nir_shader
*shader
= tgsi
.ir
.nir
;
1105 nir_foreach_variable(var
, &shader
->inputs
)
1106 var
->data
.sample
= true;
1109 assert(!(key
->bitmap
&& key
->drawpixels
));
1113 nir_lower_bitmap_options options
= {0};
1115 variant
->bitmap_sampler
= ffs(~stfp
->Base
.SamplersUsed
) - 1;
1116 options
.sampler
= variant
->bitmap_sampler
;
1117 options
.swizzle_xxxx
= (st
->bitmap
.tex_format
== PIPE_FORMAT_L8_UNORM
);
1119 NIR_PASS_V(tgsi
.ir
.nir
, nir_lower_bitmap
, &options
);
1122 /* glDrawPixels (color only) */
1123 if (key
->drawpixels
) {
1124 nir_lower_drawpixels_options options
= {{0}};
1125 unsigned samplers_used
= stfp
->Base
.SamplersUsed
;
1127 /* Find the first unused slot. */
1128 variant
->drawpix_sampler
= ffs(~samplers_used
) - 1;
1129 options
.drawpix_sampler
= variant
->drawpix_sampler
;
1130 samplers_used
|= (1 << variant
->drawpix_sampler
);
1132 options
.pixel_maps
= key
->pixelMaps
;
1133 if (key
->pixelMaps
) {
1134 variant
->pixelmap_sampler
= ffs(~samplers_used
) - 1;
1135 options
.pixelmap_sampler
= variant
->pixelmap_sampler
;
1138 options
.scale_and_bias
= key
->scaleAndBias
;
1139 if (key
->scaleAndBias
) {
1140 _mesa_add_state_reference(params
, scale_state
);
1141 memcpy(options
.scale_state_tokens
, scale_state
,
1142 sizeof(options
.scale_state_tokens
));
1143 _mesa_add_state_reference(params
, bias_state
);
1144 memcpy(options
.bias_state_tokens
, bias_state
,
1145 sizeof(options
.bias_state_tokens
));
1148 _mesa_add_state_reference(params
, texcoord_state
);
1149 memcpy(options
.texcoord_state_tokens
, texcoord_state
,
1150 sizeof(options
.texcoord_state_tokens
));
1152 NIR_PASS_V(tgsi
.ir
.nir
, nir_lower_drawpixels
, &options
);
1155 if (unlikely(key
->external
.lower_nv12
|| key
->external
.lower_iyuv
)) {
1156 nir_lower_tex_options options
= {0};
1157 options
.lower_y_uv_external
= key
->external
.lower_nv12
;
1158 options
.lower_y_u_v_external
= key
->external
.lower_iyuv
;
1159 NIR_PASS_V(tgsi
.ir
.nir
, nir_lower_tex
, &options
);
1162 st_finalize_nir(st
, &stfp
->Base
, stfp
->shader_program
, tgsi
.ir
.nir
);
1164 if (unlikely(key
->external
.lower_nv12
|| key
->external
.lower_iyuv
)) {
1165 /* This pass needs to happen *after* nir_lower_sampler */
1166 NIR_PASS_V(tgsi
.ir
.nir
, st_nir_lower_tex_src_plane
,
1167 ~stfp
->Base
.SamplersUsed
,
1168 key
->external
.lower_nv12
,
1169 key
->external
.lower_iyuv
);
1172 /* Some of the lowering above may have introduced new varyings */
1173 nir_shader_gather_info(tgsi
.ir
.nir
,
1174 nir_shader_get_entrypoint(tgsi
.ir
.nir
));
1176 variant
->driver_shader
= pipe
->create_fs_state(pipe
, &tgsi
);
1177 variant
->key
= *key
;
1182 tgsi
.tokens
= stfp
->tgsi
.tokens
;
1184 assert(!(key
->bitmap
&& key
->drawpixels
));
1186 /* Fix texture targets and add fog for ATI_fs */
1188 const struct tgsi_token
*tokens
= st_fixup_atifs(tgsi
.tokens
, key
);
1191 tgsi
.tokens
= tokens
;
1193 fprintf(stderr
, "mesa: cannot post-process ATI_fs\n");
1196 /* Emulate features. */
1197 if (key
->clamp_color
|| key
->persample_shading
) {
1198 const struct tgsi_token
*tokens
;
1200 (key
->clamp_color
? TGSI_EMU_CLAMP_COLOR_OUTPUTS
: 0) |
1201 (key
->persample_shading
? TGSI_EMU_FORCE_PERSAMPLE_INTERP
: 0);
1203 tokens
= tgsi_emulate(tgsi
.tokens
, flags
);
1206 if (tgsi
.tokens
!= stfp
->tgsi
.tokens
)
1207 tgsi_free_tokens(tgsi
.tokens
);
1208 tgsi
.tokens
= tokens
;
1210 fprintf(stderr
, "mesa: cannot emulate deprecated features\n");
1215 const struct tgsi_token
*tokens
;
1217 variant
->bitmap_sampler
= ffs(~stfp
->Base
.SamplersUsed
) - 1;
1219 tokens
= st_get_bitmap_shader(tgsi
.tokens
,
1220 st
->internal_target
,
1221 variant
->bitmap_sampler
,
1222 st
->needs_texcoord_semantic
,
1223 st
->bitmap
.tex_format
==
1224 PIPE_FORMAT_L8_UNORM
);
1227 if (tgsi
.tokens
!= stfp
->tgsi
.tokens
)
1228 tgsi_free_tokens(tgsi
.tokens
);
1229 tgsi
.tokens
= tokens
;
1231 fprintf(stderr
, "mesa: cannot create a shader for glBitmap\n");
1234 /* glDrawPixels (color only) */
1235 if (key
->drawpixels
) {
1236 const struct tgsi_token
*tokens
;
1237 unsigned scale_const
= 0, bias_const
= 0, texcoord_const
= 0;
1239 /* Find the first unused slot. */
1240 variant
->drawpix_sampler
= ffs(~stfp
->Base
.SamplersUsed
) - 1;
1242 if (key
->pixelMaps
) {
1243 unsigned samplers_used
= stfp
->Base
.SamplersUsed
|
1244 (1 << variant
->drawpix_sampler
);
1246 variant
->pixelmap_sampler
= ffs(~samplers_used
) - 1;
1249 if (key
->scaleAndBias
) {
1250 scale_const
= _mesa_add_state_reference(params
, scale_state
);
1251 bias_const
= _mesa_add_state_reference(params
, bias_state
);
1254 texcoord_const
= _mesa_add_state_reference(params
, texcoord_state
);
1256 tokens
= st_get_drawpix_shader(tgsi
.tokens
,
1257 st
->needs_texcoord_semantic
,
1258 key
->scaleAndBias
, scale_const
,
1259 bias_const
, key
->pixelMaps
,
1260 variant
->drawpix_sampler
,
1261 variant
->pixelmap_sampler
,
1262 texcoord_const
, st
->internal_target
);
1265 if (tgsi
.tokens
!= stfp
->tgsi
.tokens
)
1266 tgsi_free_tokens(tgsi
.tokens
);
1267 tgsi
.tokens
= tokens
;
1269 fprintf(stderr
, "mesa: cannot create a shader for glDrawPixels\n");
1272 if (unlikely(key
->external
.lower_nv12
|| key
->external
.lower_iyuv
)) {
1273 const struct tgsi_token
*tokens
;
1275 /* samplers inserted would conflict, but this should be unpossible: */
1276 assert(!(key
->bitmap
|| key
->drawpixels
));
1278 tokens
= st_tgsi_lower_yuv(tgsi
.tokens
,
1279 ~stfp
->Base
.SamplersUsed
,
1280 key
->external
.lower_nv12
,
1281 key
->external
.lower_iyuv
);
1283 if (tgsi
.tokens
!= stfp
->tgsi
.tokens
)
1284 tgsi_free_tokens(tgsi
.tokens
);
1285 tgsi
.tokens
= tokens
;
1287 fprintf(stderr
, "mesa: cannot create a shader for samplerExternalOES\n");
1291 if (ST_DEBUG
& DEBUG_TGSI
) {
1292 tgsi_dump(tgsi
.tokens
, 0);
1296 /* fill in variant */
1297 variant
->driver_shader
= pipe
->create_fs_state(pipe
, &tgsi
);
1298 variant
->key
= *key
;
1300 if (tgsi
.tokens
!= stfp
->tgsi
.tokens
)
1301 tgsi_free_tokens(tgsi
.tokens
);
1306 * Translate fragment program if needed.
1308 struct st_fp_variant
*
1309 st_get_fp_variant(struct st_context
*st
,
1310 struct st_fragment_program
*stfp
,
1311 const struct st_fp_variant_key
*key
)
1313 struct st_fp_variant
*fpv
;
1315 /* Search for existing variant */
1316 for (fpv
= stfp
->variants
; fpv
; fpv
= fpv
->next
) {
1317 if (memcmp(&fpv
->key
, key
, sizeof(*key
)) == 0) {
1324 fpv
= st_create_fp_variant(st
, stfp
, key
);
1326 if (key
->bitmap
|| key
->drawpixels
) {
1327 /* Regular variants should always come before the
1328 * bitmap & drawpixels variants, (unless there
1329 * are no regular variants) so that
1330 * st_update_fp can take a fast path when
1331 * shader_has_one_variant is set.
1333 if (!stfp
->variants
) {
1334 stfp
->variants
= fpv
;
1336 /* insert into list after the first one */
1337 fpv
->next
= stfp
->variants
->next
;
1338 stfp
->variants
->next
= fpv
;
1341 /* insert into list */
1342 fpv
->next
= stfp
->variants
;
1343 stfp
->variants
= fpv
;
1353 * Translate a program. This is common code for geometry and tessellation
1357 st_translate_program_common(struct st_context
*st
,
1358 struct gl_program
*prog
,
1359 struct glsl_to_tgsi_visitor
*glsl_to_tgsi
,
1360 struct ureg_program
*ureg
,
1361 unsigned tgsi_processor
,
1362 struct pipe_shader_state
*out_state
)
1364 ubyte inputSlotToAttr
[VARYING_SLOT_TESS_MAX
];
1365 ubyte inputMapping
[VARYING_SLOT_TESS_MAX
];
1366 ubyte outputMapping
[VARYING_SLOT_TESS_MAX
];
1369 ubyte input_semantic_name
[PIPE_MAX_SHADER_INPUTS
];
1370 ubyte input_semantic_index
[PIPE_MAX_SHADER_INPUTS
];
1371 uint num_inputs
= 0;
1373 ubyte output_semantic_name
[PIPE_MAX_SHADER_OUTPUTS
];
1374 ubyte output_semantic_index
[PIPE_MAX_SHADER_OUTPUTS
];
1375 uint num_outputs
= 0;
1379 memset(inputSlotToAttr
, 0, sizeof(inputSlotToAttr
));
1380 memset(inputMapping
, 0, sizeof(inputMapping
));
1381 memset(outputMapping
, 0, sizeof(outputMapping
));
1382 memset(out_state
, 0, sizeof(*out_state
));
1384 if (prog
->info
.clip_distance_array_size
)
1385 ureg_property(ureg
, TGSI_PROPERTY_NUM_CLIPDIST_ENABLED
,
1386 prog
->info
.clip_distance_array_size
);
1387 if (prog
->info
.cull_distance_array_size
)
1388 ureg_property(ureg
, TGSI_PROPERTY_NUM_CULLDIST_ENABLED
,
1389 prog
->info
.cull_distance_array_size
);
1392 * Convert Mesa program inputs to TGSI input register semantics.
1394 for (attr
= 0; attr
< VARYING_SLOT_MAX
; attr
++) {
1395 if ((prog
->info
.inputs_read
& BITFIELD64_BIT(attr
)) == 0)
1398 unsigned slot
= num_inputs
++;
1400 inputMapping
[attr
] = slot
;
1401 inputSlotToAttr
[slot
] = attr
;
1403 unsigned semantic_name
, semantic_index
;
1404 tgsi_get_gl_varying_semantic(attr
, st
->needs_texcoord_semantic
,
1405 &semantic_name
, &semantic_index
);
1406 input_semantic_name
[slot
] = semantic_name
;
1407 input_semantic_index
[slot
] = semantic_index
;
1410 /* Also add patch inputs. */
1411 for (attr
= 0; attr
< 32; attr
++) {
1412 if (prog
->info
.patch_inputs_read
& (1u << attr
)) {
1413 GLuint slot
= num_inputs
++;
1414 GLuint patch_attr
= VARYING_SLOT_PATCH0
+ attr
;
1416 inputMapping
[patch_attr
] = slot
;
1417 inputSlotToAttr
[slot
] = patch_attr
;
1418 input_semantic_name
[slot
] = TGSI_SEMANTIC_PATCH
;
1419 input_semantic_index
[slot
] = attr
;
1423 /* initialize output semantics to defaults */
1424 for (i
= 0; i
< PIPE_MAX_SHADER_OUTPUTS
; i
++) {
1425 output_semantic_name
[i
] = TGSI_SEMANTIC_GENERIC
;
1426 output_semantic_index
[i
] = 0;
1430 * Determine number of outputs, the (default) output register
1431 * mapping and the semantic information for each output.
1433 for (attr
= 0; attr
< VARYING_SLOT_MAX
; attr
++) {
1434 if (prog
->info
.outputs_written
& BITFIELD64_BIT(attr
)) {
1435 GLuint slot
= num_outputs
++;
1437 outputMapping
[attr
] = slot
;
1439 unsigned semantic_name
, semantic_index
;
1440 tgsi_get_gl_varying_semantic(attr
, st
->needs_texcoord_semantic
,
1441 &semantic_name
, &semantic_index
);
1442 output_semantic_name
[slot
] = semantic_name
;
1443 output_semantic_index
[slot
] = semantic_index
;
1447 /* Also add patch outputs. */
1448 for (attr
= 0; attr
< 32; attr
++) {
1449 if (prog
->info
.patch_outputs_written
& (1u << attr
)) {
1450 GLuint slot
= num_outputs
++;
1451 GLuint patch_attr
= VARYING_SLOT_PATCH0
+ attr
;
1453 outputMapping
[patch_attr
] = slot
;
1454 output_semantic_name
[slot
] = TGSI_SEMANTIC_PATCH
;
1455 output_semantic_index
[slot
] = attr
;
1459 st_translate_program(st
->ctx
,
1468 input_semantic_name
,
1469 input_semantic_index
,
1474 output_semantic_name
,
1475 output_semantic_index
);
1477 if (tgsi_processor
== PIPE_SHADER_COMPUTE
) {
1478 struct st_compute_program
*stcp
= (struct st_compute_program
*) prog
;
1479 out_state
->tokens
= ureg_get_tokens(ureg
, &stcp
->num_tgsi_tokens
);
1480 stcp
->tgsi
.prog
= out_state
->tokens
;
1482 struct st_common_program
*stcp
= (struct st_common_program
*) prog
;
1483 out_state
->tokens
= ureg_get_tokens(ureg
, &stcp
->num_tgsi_tokens
);
1487 st_translate_stream_output_info(prog
->sh
.LinkedTransformFeedback
,
1489 &out_state
->stream_output
);
1491 st_store_ir_in_disk_cache(st
, prog
, false);
1493 if ((ST_DEBUG
& DEBUG_TGSI
) && (ST_DEBUG
& DEBUG_MESA
)) {
1494 _mesa_print_program(prog
);
1498 if (ST_DEBUG
& DEBUG_TGSI
) {
1499 tgsi_dump(out_state
->tokens
, 0);
1505 * Update stream-output info for GS/TCS/TES. Normally this is done in
1506 * st_translate_program_common() but that is not called for glsl_to_nir
1510 st_translate_program_stream_output(struct gl_program
*prog
,
1511 struct pipe_stream_output_info
*stream_output
)
1513 if (!prog
->sh
.LinkedTransformFeedback
)
1516 ubyte outputMapping
[VARYING_SLOT_TESS_MAX
];
1518 uint num_outputs
= 0;
1520 memset(outputMapping
, 0, sizeof(outputMapping
));
1523 * Determine number of outputs, the (default) output register
1524 * mapping and the semantic information for each output.
1526 for (attr
= 0; attr
< VARYING_SLOT_MAX
; attr
++) {
1527 if (prog
->info
.outputs_written
& BITFIELD64_BIT(attr
)) {
1528 GLuint slot
= num_outputs
++;
1530 outputMapping
[attr
] = slot
;
1534 st_translate_stream_output_info(prog
->sh
.LinkedTransformFeedback
,
1540 * Translate a geometry program to create a new variant.
1543 st_translate_geometry_program(struct st_context
*st
,
1544 struct st_common_program
*stgp
)
1546 struct ureg_program
*ureg
;
1548 /* We have already compiled to NIR so just return */
1549 if (stgp
->shader_program
) {
1551 st_finalize_nir(st
, &stgp
->Base
, stgp
->shader_program
,
1553 st_translate_program_stream_output(&stgp
->Base
, &stgp
->tgsi
.stream_output
);
1554 st_store_ir_in_disk_cache(st
, &stgp
->Base
, true);
1558 ureg
= ureg_create_with_screen(PIPE_SHADER_GEOMETRY
, st
->pipe
->screen
);
1562 ureg_property(ureg
, TGSI_PROPERTY_GS_INPUT_PRIM
,
1563 stgp
->Base
.info
.gs
.input_primitive
);
1564 ureg_property(ureg
, TGSI_PROPERTY_GS_OUTPUT_PRIM
,
1565 stgp
->Base
.info
.gs
.output_primitive
);
1566 ureg_property(ureg
, TGSI_PROPERTY_GS_MAX_OUTPUT_VERTICES
,
1567 stgp
->Base
.info
.gs
.vertices_out
);
1568 ureg_property(ureg
, TGSI_PROPERTY_GS_INVOCATIONS
,
1569 stgp
->Base
.info
.gs
.invocations
);
1571 st_translate_program_common(st
, &stgp
->Base
, stgp
->glsl_to_tgsi
, ureg
,
1572 PIPE_SHADER_GEOMETRY
, &stgp
->tgsi
);
1574 free_glsl_to_tgsi_visitor(stgp
->glsl_to_tgsi
);
1575 stgp
->glsl_to_tgsi
= NULL
;
1581 * Get/create a basic program variant.
1583 struct st_basic_variant
*
1584 st_get_basic_variant(struct st_context
*st
,
1585 unsigned pipe_shader
,
1586 struct st_common_program
*prog
)
1588 struct pipe_context
*pipe
= st
->pipe
;
1589 struct st_basic_variant
*v
;
1590 struct st_basic_variant_key key
;
1591 struct pipe_shader_state tgsi
= {0};
1592 memset(&key
, 0, sizeof(key
));
1593 key
.st
= st
->has_shareable_shaders
? NULL
: st
;
1595 /* Search for existing variant */
1596 for (v
= prog
->variants
; v
; v
= v
->next
) {
1597 if (memcmp(&v
->key
, &key
, sizeof(key
)) == 0) {
1604 v
= CALLOC_STRUCT(st_basic_variant
);
1607 if (prog
->tgsi
.type
== PIPE_SHADER_IR_NIR
) {
1608 tgsi
.type
= PIPE_SHADER_IR_NIR
;
1609 tgsi
.ir
.nir
= nir_shader_clone(NULL
, prog
->tgsi
.ir
.nir
);
1610 tgsi
.stream_output
= prog
->tgsi
.stream_output
;
1613 /* fill in new variant */
1614 switch (pipe_shader
) {
1615 case PIPE_SHADER_TESS_CTRL
:
1616 v
->driver_shader
= pipe
->create_tcs_state(pipe
, &tgsi
);
1618 case PIPE_SHADER_TESS_EVAL
:
1619 v
->driver_shader
= pipe
->create_tes_state(pipe
, &tgsi
);
1621 case PIPE_SHADER_GEOMETRY
:
1622 v
->driver_shader
= pipe
->create_gs_state(pipe
, &tgsi
);
1625 assert(!"unhandled shader type");
1632 /* insert into list */
1633 v
->next
= prog
->variants
;
1643 * Translate a tessellation control program to create a new variant.
1646 st_translate_tessctrl_program(struct st_context
*st
,
1647 struct st_common_program
*sttcp
)
1649 struct ureg_program
*ureg
;
1651 /* We have already compiled to NIR so just return */
1652 if (sttcp
->shader_program
) {
1654 st_finalize_nir(st
, &sttcp
->Base
, sttcp
->shader_program
,
1655 sttcp
->tgsi
.ir
.nir
);
1656 st_store_ir_in_disk_cache(st
, &sttcp
->Base
, true);
1660 ureg
= ureg_create_with_screen(PIPE_SHADER_TESS_CTRL
, st
->pipe
->screen
);
1664 ureg_property(ureg
, TGSI_PROPERTY_TCS_VERTICES_OUT
,
1665 sttcp
->Base
.info
.tess
.tcs_vertices_out
);
1667 st_translate_program_common(st
, &sttcp
->Base
, sttcp
->glsl_to_tgsi
, ureg
,
1668 PIPE_SHADER_TESS_CTRL
, &sttcp
->tgsi
);
1670 free_glsl_to_tgsi_visitor(sttcp
->glsl_to_tgsi
);
1671 sttcp
->glsl_to_tgsi
= NULL
;
1677 * Translate a tessellation evaluation program to create a new variant.
1680 st_translate_tesseval_program(struct st_context
*st
,
1681 struct st_common_program
*sttep
)
1683 struct ureg_program
*ureg
;
1685 /* We have already compiled to NIR so just return */
1686 if (sttep
->shader_program
) {
1688 st_finalize_nir(st
, &sttep
->Base
, sttep
->shader_program
,
1689 sttep
->tgsi
.ir
.nir
);
1690 st_translate_program_stream_output(&sttep
->Base
, &sttep
->tgsi
.stream_output
);
1691 st_store_ir_in_disk_cache(st
, &sttep
->Base
, true);
1695 ureg
= ureg_create_with_screen(PIPE_SHADER_TESS_EVAL
, st
->pipe
->screen
);
1699 if (sttep
->Base
.info
.tess
.primitive_mode
== GL_ISOLINES
)
1700 ureg_property(ureg
, TGSI_PROPERTY_TES_PRIM_MODE
, GL_LINES
);
1702 ureg_property(ureg
, TGSI_PROPERTY_TES_PRIM_MODE
,
1703 sttep
->Base
.info
.tess
.primitive_mode
);
1705 STATIC_ASSERT((TESS_SPACING_EQUAL
+ 1) % 3 == PIPE_TESS_SPACING_EQUAL
);
1706 STATIC_ASSERT((TESS_SPACING_FRACTIONAL_ODD
+ 1) % 3 ==
1707 PIPE_TESS_SPACING_FRACTIONAL_ODD
);
1708 STATIC_ASSERT((TESS_SPACING_FRACTIONAL_EVEN
+ 1) % 3 ==
1709 PIPE_TESS_SPACING_FRACTIONAL_EVEN
);
1711 ureg_property(ureg
, TGSI_PROPERTY_TES_SPACING
,
1712 (sttep
->Base
.info
.tess
.spacing
+ 1) % 3);
1714 ureg_property(ureg
, TGSI_PROPERTY_TES_VERTEX_ORDER_CW
,
1715 !sttep
->Base
.info
.tess
.ccw
);
1716 ureg_property(ureg
, TGSI_PROPERTY_TES_POINT_MODE
,
1717 sttep
->Base
.info
.tess
.point_mode
);
1719 st_translate_program_common(st
, &sttep
->Base
, sttep
->glsl_to_tgsi
,
1720 ureg
, PIPE_SHADER_TESS_EVAL
, &sttep
->tgsi
);
1722 free_glsl_to_tgsi_visitor(sttep
->glsl_to_tgsi
);
1723 sttep
->glsl_to_tgsi
= NULL
;
1729 * Translate a compute program to create a new variant.
1732 st_translate_compute_program(struct st_context
*st
,
1733 struct st_compute_program
*stcp
)
1735 struct ureg_program
*ureg
;
1736 struct pipe_shader_state prog
;
1738 stcp
->tgsi
.req_local_mem
= stcp
->Base
.info
.cs
.shared_size
;
1740 if (stcp
->shader_program
) {
1741 /* no compute variants: */
1742 st_finalize_nir(st
, &stcp
->Base
, stcp
->shader_program
,
1743 (struct nir_shader
*) stcp
->tgsi
.prog
);
1744 st_store_ir_in_disk_cache(st
, &stcp
->Base
, true);
1748 ureg
= ureg_create_with_screen(PIPE_SHADER_COMPUTE
, st
->pipe
->screen
);
1752 st_translate_program_common(st
, &stcp
->Base
, stcp
->glsl_to_tgsi
, ureg
,
1753 PIPE_SHADER_COMPUTE
, &prog
);
1755 stcp
->tgsi
.ir_type
= PIPE_SHADER_IR_TGSI
;
1756 stcp
->tgsi
.req_private_mem
= 0;
1757 stcp
->tgsi
.req_input_mem
= 0;
1759 free_glsl_to_tgsi_visitor(stcp
->glsl_to_tgsi
);
1760 stcp
->glsl_to_tgsi
= NULL
;
1766 * Get/create compute program variant.
1768 struct st_basic_variant
*
1769 st_get_cp_variant(struct st_context
*st
,
1770 struct pipe_compute_state
*tgsi
,
1771 struct st_basic_variant
**variants
)
1773 struct pipe_context
*pipe
= st
->pipe
;
1774 struct st_basic_variant
*v
;
1775 struct st_basic_variant_key key
= {0};
1777 key
.st
= st
->has_shareable_shaders
? NULL
: st
;
1779 /* Search for existing variant */
1780 for (v
= *variants
; v
; v
= v
->next
) {
1781 if (memcmp(&v
->key
, &key
, sizeof(key
)) == 0) {
1788 v
= CALLOC_STRUCT(st_basic_variant
);
1790 /* fill in new variant */
1791 struct pipe_compute_state cs
= *tgsi
;
1792 if (tgsi
->ir_type
== PIPE_SHADER_IR_NIR
)
1793 cs
.prog
= nir_shader_clone(NULL
, tgsi
->prog
);
1794 v
->driver_shader
= pipe
->create_compute_state(pipe
, &cs
);
1797 /* insert into list */
1798 v
->next
= *variants
;
1808 * Vert/Geom/Frag programs have per-context variants. Free all the
1809 * variants attached to the given program which match the given context.
1812 destroy_program_variants(struct st_context
*st
, struct gl_program
*target
)
1814 if (!target
|| target
== &_mesa_DummyProgram
)
1817 switch (target
->Target
) {
1818 case GL_VERTEX_PROGRAM_ARB
:
1820 struct st_vertex_program
*stvp
= (struct st_vertex_program
*) target
;
1821 struct st_vp_variant
*vpv
, **prevPtr
= &stvp
->variants
;
1823 for (vpv
= stvp
->variants
; vpv
; ) {
1824 struct st_vp_variant
*next
= vpv
->next
;
1825 if (vpv
->key
.st
== st
) {
1826 /* unlink from list */
1828 /* destroy this variant */
1829 delete_vp_variant(st
, vpv
);
1832 prevPtr
= &vpv
->next
;
1838 case GL_FRAGMENT_PROGRAM_ARB
:
1840 struct st_fragment_program
*stfp
=
1841 (struct st_fragment_program
*) target
;
1842 struct st_fp_variant
*fpv
, **prevPtr
= &stfp
->variants
;
1844 for (fpv
= stfp
->variants
; fpv
; ) {
1845 struct st_fp_variant
*next
= fpv
->next
;
1846 if (fpv
->key
.st
== st
) {
1847 /* unlink from list */
1849 /* destroy this variant */
1850 delete_fp_variant(st
, fpv
);
1853 prevPtr
= &fpv
->next
;
1859 case GL_GEOMETRY_PROGRAM_NV
:
1860 case GL_TESS_CONTROL_PROGRAM_NV
:
1861 case GL_TESS_EVALUATION_PROGRAM_NV
:
1862 case GL_COMPUTE_PROGRAM_NV
:
1864 struct st_common_program
*p
= st_common_program(target
);
1865 struct st_compute_program
*cp
= (struct st_compute_program
*)target
;
1866 struct st_basic_variant
**variants
=
1867 target
->Target
== GL_COMPUTE_PROGRAM_NV
? &cp
->variants
:
1869 struct st_basic_variant
*v
, **prevPtr
= variants
;
1871 for (v
= *variants
; v
; ) {
1872 struct st_basic_variant
*next
= v
->next
;
1873 if (v
->key
.st
== st
) {
1874 /* unlink from list */
1876 /* destroy this variant */
1877 delete_basic_variant(st
, v
, target
->Target
);
1887 _mesa_problem(NULL
, "Unexpected program target 0x%x in "
1888 "destroy_program_variants_cb()", target
->Target
);
1894 * Callback for _mesa_HashWalk. Free all the shader's program variants
1895 * which match the given context.
1898 destroy_shader_program_variants_cb(GLuint key
, void *data
, void *userData
)
1900 struct st_context
*st
= (struct st_context
*) userData
;
1901 struct gl_shader
*shader
= (struct gl_shader
*) data
;
1903 switch (shader
->Type
) {
1904 case GL_SHADER_PROGRAM_MESA
:
1906 struct gl_shader_program
*shProg
= (struct gl_shader_program
*) data
;
1909 for (i
= 0; i
< ARRAY_SIZE(shProg
->_LinkedShaders
); i
++) {
1910 if (shProg
->_LinkedShaders
[i
])
1911 destroy_program_variants(st
, shProg
->_LinkedShaders
[i
]->Program
);
1915 case GL_VERTEX_SHADER
:
1916 case GL_FRAGMENT_SHADER
:
1917 case GL_GEOMETRY_SHADER
:
1918 case GL_TESS_CONTROL_SHADER
:
1919 case GL_TESS_EVALUATION_SHADER
:
1920 case GL_COMPUTE_SHADER
:
1929 * Callback for _mesa_HashWalk. Free all the program variants which match
1930 * the given context.
1933 destroy_program_variants_cb(GLuint key
, void *data
, void *userData
)
1935 struct st_context
*st
= (struct st_context
*) userData
;
1936 struct gl_program
*program
= (struct gl_program
*) data
;
1937 destroy_program_variants(st
, program
);
1942 * Walk over all shaders and programs to delete any variants which
1943 * belong to the given context.
1944 * This is called during context tear-down.
1947 st_destroy_program_variants(struct st_context
*st
)
1949 /* If shaders can be shared with other contexts, the last context will
1950 * call DeleteProgram on all shaders, releasing everything.
1952 if (st
->has_shareable_shaders
)
1955 /* ARB vert/frag program */
1956 _mesa_HashWalk(st
->ctx
->Shared
->Programs
,
1957 destroy_program_variants_cb
, st
);
1959 /* GLSL vert/frag/geom shaders */
1960 _mesa_HashWalk(st
->ctx
->Shared
->ShaderObjects
,
1961 destroy_shader_program_variants_cb
, st
);
1966 * For debugging, print/dump the current vertex program.
1969 st_print_current_vertex_program(void)
1971 GET_CURRENT_CONTEXT(ctx
);
1973 if (ctx
->VertexProgram
._Current
) {
1974 struct st_vertex_program
*stvp
=
1975 (struct st_vertex_program
*) ctx
->VertexProgram
._Current
;
1976 struct st_vp_variant
*stv
;
1978 debug_printf("Vertex program %u\n", stvp
->Base
.Id
);
1980 for (stv
= stvp
->variants
; stv
; stv
= stv
->next
) {
1981 debug_printf("variant %p\n", stv
);
1982 tgsi_dump(stv
->tgsi
.tokens
, 0);
1989 * Compile one shader variant.
1992 st_precompile_shader_variant(struct st_context
*st
,
1993 struct gl_program
*prog
)
1995 switch (prog
->Target
) {
1996 case GL_VERTEX_PROGRAM_ARB
: {
1997 struct st_vertex_program
*p
= (struct st_vertex_program
*)prog
;
1998 struct st_vp_variant_key key
= {0};
2000 key
.st
= st
->has_shareable_shaders
? NULL
: st
;
2001 st_get_vp_variant(st
, p
, &key
);
2005 case GL_TESS_CONTROL_PROGRAM_NV
: {
2006 struct st_common_program
*p
= st_common_program(prog
);
2007 st_get_basic_variant(st
, PIPE_SHADER_TESS_CTRL
, p
);
2011 case GL_TESS_EVALUATION_PROGRAM_NV
: {
2012 struct st_common_program
*p
= st_common_program(prog
);
2013 st_get_basic_variant(st
, PIPE_SHADER_TESS_EVAL
, p
);
2017 case GL_GEOMETRY_PROGRAM_NV
: {
2018 struct st_common_program
*p
= st_common_program(prog
);
2019 st_get_basic_variant(st
, PIPE_SHADER_GEOMETRY
, p
);
2023 case GL_FRAGMENT_PROGRAM_ARB
: {
2024 struct st_fragment_program
*p
= (struct st_fragment_program
*)prog
;
2025 struct st_fp_variant_key key
= {0};
2027 key
.st
= st
->has_shareable_shaders
? NULL
: st
;
2028 st_get_fp_variant(st
, p
, &key
);
2032 case GL_COMPUTE_PROGRAM_NV
: {
2033 struct st_compute_program
*p
= (struct st_compute_program
*)prog
;
2034 st_get_cp_variant(st
, &p
->tgsi
, &p
->variants
);