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_ir(struct pipe_shader_state
*ir
)
213 ureg_free_tokens(ir
->tokens
);
215 /* Note: Any setup of ->ir.nir that has had pipe->create_*_state called on
216 * it has resulted in the driver taking ownership of the NIR. Those
217 * callers should be NULLing out the nir field in any pipe_shader_state
218 * that might have this called in order to indicate that.
220 * GLSL IR and ARB programs will have set gl_program->nir to the same
221 * shader as ir->ir.nir, so it will be freed by _mesa_delete_program().
226 * Delete a vertex program variant. Note the caller must unlink
227 * the variant from the linked list.
230 delete_vp_variant(struct st_context
*st
, struct st_vp_variant
*vpv
)
232 if (vpv
->driver_shader
)
233 cso_delete_vertex_shader(st
->cso_context
, vpv
->driver_shader
);
235 if (vpv
->draw_shader
)
236 draw_delete_vertex_shader( st
->draw
, vpv
->draw_shader
);
238 delete_ir(&vpv
->tgsi
);
246 * Clean out any old compilations:
249 st_release_vp_variants( struct st_context
*st
,
250 struct st_vertex_program
*stvp
)
252 struct st_vp_variant
*vpv
;
254 for (vpv
= stvp
->variants
; vpv
; ) {
255 struct st_vp_variant
*next
= vpv
->next
;
256 delete_vp_variant(st
, vpv
);
260 stvp
->variants
= NULL
;
262 delete_ir(&stvp
->tgsi
);
268 * Delete a fragment program variant. Note the caller must unlink
269 * the variant from the linked list.
272 delete_fp_variant(struct st_context
*st
, struct st_fp_variant
*fpv
)
274 if (fpv
->driver_shader
)
275 cso_delete_fragment_shader(st
->cso_context
, fpv
->driver_shader
);
281 * Free all variants of a fragment program.
284 st_release_fp_variants(struct st_context
*st
, struct st_fragment_program
*stfp
)
286 struct st_fp_variant
*fpv
;
288 for (fpv
= stfp
->variants
; fpv
; ) {
289 struct st_fp_variant
*next
= fpv
->next
;
290 delete_fp_variant(st
, fpv
);
294 stfp
->variants
= NULL
;
296 delete_ir(&stfp
->tgsi
);
301 * Delete a basic program variant. Note the caller must unlink
302 * the variant from the linked list.
305 delete_basic_variant(struct st_context
*st
, struct st_basic_variant
*v
,
308 if (v
->driver_shader
) {
310 case GL_TESS_CONTROL_PROGRAM_NV
:
311 cso_delete_tessctrl_shader(st
->cso_context
, v
->driver_shader
);
313 case GL_TESS_EVALUATION_PROGRAM_NV
:
314 cso_delete_tesseval_shader(st
->cso_context
, v
->driver_shader
);
316 case GL_GEOMETRY_PROGRAM_NV
:
317 cso_delete_geometry_shader(st
->cso_context
, v
->driver_shader
);
319 case GL_COMPUTE_PROGRAM_NV
:
320 cso_delete_compute_shader(st
->cso_context
, v
->driver_shader
);
323 assert(!"this shouldn't occur");
332 * Free all basic program variants.
335 st_release_basic_variants(struct st_context
*st
, GLenum target
,
336 struct st_basic_variant
**variants
,
337 struct pipe_shader_state
*tgsi
)
339 struct st_basic_variant
*v
;
341 for (v
= *variants
; v
; ) {
342 struct st_basic_variant
*next
= v
->next
;
343 delete_basic_variant(st
, v
, target
);
354 * Free all variants of a compute program.
357 st_release_cp_variants(struct st_context
*st
, struct st_compute_program
*stcp
)
359 struct st_basic_variant
**variants
= &stcp
->variants
;
360 struct st_basic_variant
*v
;
362 for (v
= *variants
; v
; ) {
363 struct st_basic_variant
*next
= v
->next
;
364 delete_basic_variant(st
, v
, stcp
->Base
.Target
);
370 if (stcp
->tgsi
.prog
) {
371 switch (stcp
->tgsi
.ir_type
) {
372 case PIPE_SHADER_IR_TGSI
:
373 ureg_free_tokens(stcp
->tgsi
.prog
);
374 stcp
->tgsi
.prog
= NULL
;
376 case PIPE_SHADER_IR_NIR
:
377 /* pipe driver took ownership of prog */
379 case PIPE_SHADER_IR_NATIVE
:
381 stcp
->tgsi
.prog
= NULL
;
388 * Translate ARB (asm) program to NIR
391 st_translate_prog_to_nir(struct st_context
*st
, struct gl_program
*prog
,
392 gl_shader_stage stage
)
394 enum pipe_shader_type p_stage
= stage
; /* valid for VS/FS */
395 const bool is_scalar
=
396 st
->pipe
->screen
->get_shader_param(st
->pipe
->screen
, p_stage
,
397 PIPE_SHADER_CAP_SCALAR_ISA
);
399 const struct gl_shader_compiler_options
*options
=
400 &st
->ctx
->Const
.ShaderCompilerOptions
[stage
];
402 /* Translate to NIR */
403 nir_shader
*nir
= prog_to_nir(prog
, options
->NirOptions
);
404 NIR_PASS_V(nir
, nir_lower_regs_to_ssa
); /* turn registers into SSA */
405 nir_validate_shader(nir
, "after st/ptn lower_regs_to_ssa");
407 NIR_PASS_V(nir
, st_nir_lower_wpos_ytransform
, prog
, st
->pipe
->screen
);
408 NIR_PASS_V(nir
, nir_lower_system_values
);
411 NIR_PASS_V(nir
, nir_opt_constant_folding
);
412 st_nir_opts(nir
, is_scalar
);
413 nir_validate_shader(nir
, "after st/ptn NIR opts");
419 * Translate a vertex program.
422 st_translate_vertex_program(struct st_context
*st
,
423 struct st_vertex_program
*stvp
)
425 struct ureg_program
*ureg
;
426 enum pipe_error error
;
427 unsigned num_outputs
= 0;
429 ubyte output_semantic_name
[VARYING_SLOT_MAX
] = {0};
430 ubyte output_semantic_index
[VARYING_SLOT_MAX
] = {0};
432 stvp
->num_inputs
= 0;
433 memset(stvp
->input_to_index
, ~0, sizeof(stvp
->input_to_index
));
435 if (stvp
->Base
.arb
.IsPositionInvariant
)
436 _mesa_insert_mvp_code(st
->ctx
, &stvp
->Base
);
439 * Determine number of inputs, the mappings between VERT_ATTRIB_x
440 * and TGSI generic input indexes, plus input attrib semantic info.
442 for (attr
= 0; attr
< VERT_ATTRIB_MAX
; attr
++) {
443 if ((stvp
->Base
.info
.inputs_read
& BITFIELD64_BIT(attr
)) != 0) {
444 stvp
->input_to_index
[attr
] = stvp
->num_inputs
;
445 stvp
->index_to_input
[stvp
->num_inputs
] = attr
;
447 if ((stvp
->Base
.DualSlotInputs
& BITFIELD64_BIT(attr
)) != 0) {
448 /* add placeholder for second part of a double attribute */
449 stvp
->index_to_input
[stvp
->num_inputs
] = ST_DOUBLE_ATTRIB_PLACEHOLDER
;
454 /* bit of a hack, presetup potentially unused edgeflag input */
455 stvp
->input_to_index
[VERT_ATTRIB_EDGEFLAG
] = stvp
->num_inputs
;
456 stvp
->index_to_input
[stvp
->num_inputs
] = VERT_ATTRIB_EDGEFLAG
;
458 /* Compute mapping of vertex program outputs to slots.
460 for (attr
= 0; attr
< VARYING_SLOT_MAX
; attr
++) {
461 if ((stvp
->Base
.info
.outputs_written
& BITFIELD64_BIT(attr
)) == 0) {
462 stvp
->result_to_output
[attr
] = ~0;
465 unsigned slot
= num_outputs
++;
467 stvp
->result_to_output
[attr
] = slot
;
469 unsigned semantic_name
, semantic_index
;
470 tgsi_get_gl_varying_semantic(attr
, st
->needs_texcoord_semantic
,
471 &semantic_name
, &semantic_index
);
472 output_semantic_name
[slot
] = semantic_name
;
473 output_semantic_index
[slot
] = semantic_index
;
476 /* similar hack to above, presetup potentially unused edgeflag output */
477 stvp
->result_to_output
[VARYING_SLOT_EDGE
] = num_outputs
;
478 output_semantic_name
[num_outputs
] = TGSI_SEMANTIC_EDGEFLAG
;
479 output_semantic_index
[num_outputs
] = 0;
482 if (!stvp
->glsl_to_tgsi
&& !stvp
->shader_program
) {
483 _mesa_remove_output_reads(&stvp
->Base
, PROGRAM_OUTPUT
);
485 /* This determines which states will be updated when the assembly
488 stvp
->affected_states
= ST_NEW_VS_STATE
|
490 ST_NEW_VERTEX_ARRAYS
;
492 if (stvp
->Base
.Parameters
->NumParameters
)
493 stvp
->affected_states
|= ST_NEW_VS_CONSTANTS
;
495 /* No samplers are allowed in ARB_vp. */
498 if (stvp
->shader_program
) {
499 st_translate_stream_output_info(stvp
->Base
.sh
.LinkedTransformFeedback
,
500 stvp
->result_to_output
,
501 &stvp
->tgsi
.stream_output
);
503 st_store_ir_in_disk_cache(st
, &stvp
->Base
, true);
507 ureg
= ureg_create_with_screen(PIPE_SHADER_VERTEX
, st
->pipe
->screen
);
511 if (stvp
->Base
.info
.clip_distance_array_size
)
512 ureg_property(ureg
, TGSI_PROPERTY_NUM_CLIPDIST_ENABLED
,
513 stvp
->Base
.info
.clip_distance_array_size
);
514 if (stvp
->Base
.info
.cull_distance_array_size
)
515 ureg_property(ureg
, TGSI_PROPERTY_NUM_CULLDIST_ENABLED
,
516 stvp
->Base
.info
.cull_distance_array_size
);
518 if (ST_DEBUG
& DEBUG_MESA
) {
519 _mesa_print_program(&stvp
->Base
);
520 _mesa_print_program_parameters(st
->ctx
, &stvp
->Base
);
524 if (stvp
->glsl_to_tgsi
) {
525 error
= st_translate_program(st
->ctx
,
532 stvp
->input_to_index
,
533 NULL
, /* inputSlotToAttr */
534 NULL
, /* input semantic name */
535 NULL
, /* input semantic index */
536 NULL
, /* interp mode */
539 stvp
->result_to_output
,
540 output_semantic_name
,
541 output_semantic_index
);
543 st_translate_stream_output_info(stvp
->Base
.sh
.LinkedTransformFeedback
,
544 stvp
->result_to_output
,
545 &stvp
->tgsi
.stream_output
);
547 free_glsl_to_tgsi_visitor(stvp
->glsl_to_tgsi
);
549 error
= st_translate_mesa_program(st
->ctx
,
555 stvp
->input_to_index
,
556 NULL
, /* input semantic name */
557 NULL
, /* input semantic index */
561 stvp
->result_to_output
,
562 output_semantic_name
,
563 output_semantic_index
);
566 debug_printf("%s: failed to translate Mesa program:\n", __func__
);
567 _mesa_print_program(&stvp
->Base
);
572 stvp
->tgsi
.tokens
= ureg_get_tokens(ureg
, &stvp
->num_tgsi_tokens
);
575 if (stvp
->glsl_to_tgsi
) {
576 stvp
->glsl_to_tgsi
= NULL
;
577 st_store_ir_in_disk_cache(st
, &stvp
->Base
, false);
580 bool use_nir
= PIPE_SHADER_IR_NIR
==
581 st
->pipe
->screen
->get_shader_param(st
->pipe
->screen
, PIPE_SHADER_VERTEX
,
582 PIPE_SHADER_CAP_PREFERRED_IR
);
586 st_translate_prog_to_nir(st
, &stvp
->Base
, MESA_SHADER_VERTEX
);
588 if (stvp
->tgsi
.ir
.nir
)
589 ralloc_free(stvp
->tgsi
.ir
.nir
);
590 stvp
->tgsi
.type
= PIPE_SHADER_IR_NIR
;
591 stvp
->tgsi
.ir
.nir
= nir
;
592 stvp
->Base
.nir
= nir
;
596 return stvp
->tgsi
.tokens
!= NULL
;
599 static struct st_vp_variant
*
600 st_create_vp_variant(struct st_context
*st
,
601 struct st_vertex_program
*stvp
,
602 const struct st_vp_variant_key
*key
)
604 struct st_vp_variant
*vpv
= CALLOC_STRUCT(st_vp_variant
);
605 struct pipe_context
*pipe
= st
->pipe
;
608 vpv
->tgsi
.stream_output
= stvp
->tgsi
.stream_output
;
609 vpv
->num_inputs
= stvp
->num_inputs
;
611 /* When generating a NIR program, we usually don't have TGSI tokens.
612 * However, we do create them for ARB_vertex_program / fixed-function VS
613 * programs which we may need to use with the draw module for legacy
614 * feedback/select emulation. If they exist, copy them.
616 if (stvp
->tgsi
.tokens
)
617 vpv
->tgsi
.tokens
= tgsi_dup_tokens(stvp
->tgsi
.tokens
);
619 if (stvp
->tgsi
.type
== PIPE_SHADER_IR_NIR
) {
620 vpv
->tgsi
.type
= PIPE_SHADER_IR_NIR
;
621 vpv
->tgsi
.ir
.nir
= nir_shader_clone(NULL
, stvp
->tgsi
.ir
.nir
);
622 if (key
->clamp_color
)
623 NIR_PASS_V(vpv
->tgsi
.ir
.nir
, nir_lower_clamp_color_outputs
);
624 if (key
->passthrough_edgeflags
) {
625 NIR_PASS_V(vpv
->tgsi
.ir
.nir
, nir_lower_passthrough_edgeflags
);
629 st_finalize_nir(st
, &stvp
->Base
, stvp
->shader_program
,
632 vpv
->driver_shader
= pipe
->create_vs_state(pipe
, &vpv
->tgsi
);
633 /* driver takes ownership of IR: */
634 vpv
->tgsi
.ir
.nir
= NULL
;
638 /* Emulate features. */
639 if (key
->clamp_color
|| key
->passthrough_edgeflags
) {
640 const struct tgsi_token
*tokens
;
642 (key
->clamp_color
? TGSI_EMU_CLAMP_COLOR_OUTPUTS
: 0) |
643 (key
->passthrough_edgeflags
? TGSI_EMU_PASSTHROUGH_EDGEFLAG
: 0);
645 tokens
= tgsi_emulate(vpv
->tgsi
.tokens
, flags
);
648 tgsi_free_tokens(vpv
->tgsi
.tokens
);
649 vpv
->tgsi
.tokens
= tokens
;
651 if (key
->passthrough_edgeflags
)
654 fprintf(stderr
, "mesa: cannot emulate deprecated features\n");
657 if (ST_DEBUG
& DEBUG_TGSI
) {
658 tgsi_dump(vpv
->tgsi
.tokens
, 0);
662 vpv
->driver_shader
= pipe
->create_vs_state(pipe
, &vpv
->tgsi
);
668 * Find/create a vertex program variant.
670 struct st_vp_variant
*
671 st_get_vp_variant(struct st_context
*st
,
672 struct st_vertex_program
*stvp
,
673 const struct st_vp_variant_key
*key
)
675 struct st_vp_variant
*vpv
;
677 /* Search for existing variant */
678 for (vpv
= stvp
->variants
; vpv
; vpv
= vpv
->next
) {
679 if (memcmp(&vpv
->key
, key
, sizeof(*key
)) == 0) {
686 vpv
= st_create_vp_variant(st
, stvp
, key
);
688 for (unsigned index
= 0; index
< vpv
->num_inputs
; ++index
) {
689 unsigned attr
= stvp
->index_to_input
[index
];
690 if (attr
== ST_DOUBLE_ATTRIB_PLACEHOLDER
)
692 vpv
->vert_attrib_mask
|= 1u << attr
;
695 /* insert into list */
696 vpv
->next
= stvp
->variants
;
697 stvp
->variants
= vpv
;
706 * Translate a Mesa fragment shader into a TGSI shader.
709 st_translate_fragment_program(struct st_context
*st
,
710 struct st_fragment_program
*stfp
)
712 /* We have already compiled to NIR so just return */
713 if (stfp
->shader_program
) {
714 st_store_ir_in_disk_cache(st
, &stfp
->Base
, true);
718 ubyte outputMapping
[2 * FRAG_RESULT_MAX
];
719 ubyte inputMapping
[VARYING_SLOT_MAX
];
720 ubyte inputSlotToAttr
[VARYING_SLOT_MAX
];
721 ubyte interpMode
[PIPE_MAX_SHADER_INPUTS
]; /* XXX size? */
723 GLbitfield64 inputsRead
;
724 struct ureg_program
*ureg
;
726 GLboolean write_all
= GL_FALSE
;
728 ubyte input_semantic_name
[PIPE_MAX_SHADER_INPUTS
];
729 ubyte input_semantic_index
[PIPE_MAX_SHADER_INPUTS
];
730 uint fs_num_inputs
= 0;
732 ubyte fs_output_semantic_name
[PIPE_MAX_SHADER_OUTPUTS
];
733 ubyte fs_output_semantic_index
[PIPE_MAX_SHADER_OUTPUTS
];
734 uint fs_num_outputs
= 0;
736 memset(inputSlotToAttr
, ~0, sizeof(inputSlotToAttr
));
738 /* Non-GLSL programs: */
739 if (!stfp
->glsl_to_tgsi
&& !stfp
->shader_program
) {
740 _mesa_remove_output_reads(&stfp
->Base
, PROGRAM_OUTPUT
);
741 if (st
->ctx
->Const
.GLSLFragCoordIsSysVal
)
742 _mesa_program_fragment_position_to_sysval(&stfp
->Base
);
744 /* This determines which states will be updated when the assembly
747 * fragment.position and glDrawPixels always use constants.
749 stfp
->affected_states
= ST_NEW_FS_STATE
|
750 ST_NEW_SAMPLE_SHADING
|
754 /* Just set them for ATI_fs unconditionally. */
755 stfp
->affected_states
|= ST_NEW_FS_SAMPLER_VIEWS
|
759 if (stfp
->Base
.SamplersUsed
)
760 stfp
->affected_states
|= ST_NEW_FS_SAMPLER_VIEWS
|
766 bool use_nir
= PIPE_SHADER_IR_NIR
==
767 st
->pipe
->screen
->get_shader_param(st
->pipe
->screen
,
768 PIPE_SHADER_FRAGMENT
,
769 PIPE_SHADER_CAP_PREFERRED_IR
);
771 if (use_nir
&& !stfp
->ati_fs
) {
773 st_translate_prog_to_nir(st
, &stfp
->Base
, MESA_SHADER_FRAGMENT
);
775 if (stfp
->tgsi
.ir
.nir
)
776 ralloc_free(stfp
->tgsi
.ir
.nir
);
777 stfp
->tgsi
.type
= PIPE_SHADER_IR_NIR
;
778 stfp
->tgsi
.ir
.nir
= nir
;
779 stfp
->Base
.nir
= nir
;
784 * Convert Mesa program inputs to TGSI input register semantics.
786 inputsRead
= stfp
->Base
.info
.inputs_read
;
787 for (attr
= 0; attr
< VARYING_SLOT_MAX
; attr
++) {
788 if ((inputsRead
& BITFIELD64_BIT(attr
)) != 0) {
789 const GLuint slot
= fs_num_inputs
++;
791 inputMapping
[attr
] = slot
;
792 inputSlotToAttr
[slot
] = attr
;
795 case VARYING_SLOT_POS
:
796 input_semantic_name
[slot
] = TGSI_SEMANTIC_POSITION
;
797 input_semantic_index
[slot
] = 0;
798 interpMode
[slot
] = TGSI_INTERPOLATE_LINEAR
;
800 case VARYING_SLOT_COL0
:
801 input_semantic_name
[slot
] = TGSI_SEMANTIC_COLOR
;
802 input_semantic_index
[slot
] = 0;
803 interpMode
[slot
] = stfp
->glsl_to_tgsi
?
804 TGSI_INTERPOLATE_COUNT
: TGSI_INTERPOLATE_COLOR
;
806 case VARYING_SLOT_COL1
:
807 input_semantic_name
[slot
] = TGSI_SEMANTIC_COLOR
;
808 input_semantic_index
[slot
] = 1;
809 interpMode
[slot
] = stfp
->glsl_to_tgsi
?
810 TGSI_INTERPOLATE_COUNT
: TGSI_INTERPOLATE_COLOR
;
812 case VARYING_SLOT_FOGC
:
813 input_semantic_name
[slot
] = TGSI_SEMANTIC_FOG
;
814 input_semantic_index
[slot
] = 0;
815 interpMode
[slot
] = TGSI_INTERPOLATE_PERSPECTIVE
;
817 case VARYING_SLOT_FACE
:
818 input_semantic_name
[slot
] = TGSI_SEMANTIC_FACE
;
819 input_semantic_index
[slot
] = 0;
820 interpMode
[slot
] = TGSI_INTERPOLATE_CONSTANT
;
822 case VARYING_SLOT_PRIMITIVE_ID
:
823 input_semantic_name
[slot
] = TGSI_SEMANTIC_PRIMID
;
824 input_semantic_index
[slot
] = 0;
825 interpMode
[slot
] = TGSI_INTERPOLATE_CONSTANT
;
827 case VARYING_SLOT_LAYER
:
828 input_semantic_name
[slot
] = TGSI_SEMANTIC_LAYER
;
829 input_semantic_index
[slot
] = 0;
830 interpMode
[slot
] = TGSI_INTERPOLATE_CONSTANT
;
832 case VARYING_SLOT_VIEWPORT
:
833 input_semantic_name
[slot
] = TGSI_SEMANTIC_VIEWPORT_INDEX
;
834 input_semantic_index
[slot
] = 0;
835 interpMode
[slot
] = TGSI_INTERPOLATE_CONSTANT
;
837 case VARYING_SLOT_CLIP_DIST0
:
838 input_semantic_name
[slot
] = TGSI_SEMANTIC_CLIPDIST
;
839 input_semantic_index
[slot
] = 0;
840 interpMode
[slot
] = TGSI_INTERPOLATE_PERSPECTIVE
;
842 case VARYING_SLOT_CLIP_DIST1
:
843 input_semantic_name
[slot
] = TGSI_SEMANTIC_CLIPDIST
;
844 input_semantic_index
[slot
] = 1;
845 interpMode
[slot
] = TGSI_INTERPOLATE_PERSPECTIVE
;
847 case VARYING_SLOT_CULL_DIST0
:
848 case VARYING_SLOT_CULL_DIST1
:
849 /* these should have been lowered by GLSL */
852 /* In most cases, there is nothing special about these
853 * inputs, so adopt a convention to use the generic
854 * semantic name and the mesa VARYING_SLOT_ number as the
857 * All that is required is that the vertex shader labels
858 * its own outputs similarly, and that the vertex shader
859 * generates at least every output required by the
860 * fragment shader plus fixed-function hardware (such as
863 * However, some drivers may need us to identify the PNTC and TEXi
864 * varyings if, for example, their capability to replace them with
865 * sprite coordinates is limited.
867 case VARYING_SLOT_PNTC
:
868 if (st
->needs_texcoord_semantic
) {
869 input_semantic_name
[slot
] = TGSI_SEMANTIC_PCOORD
;
870 input_semantic_index
[slot
] = 0;
871 interpMode
[slot
] = TGSI_INTERPOLATE_LINEAR
;
875 case VARYING_SLOT_TEX0
:
876 case VARYING_SLOT_TEX1
:
877 case VARYING_SLOT_TEX2
:
878 case VARYING_SLOT_TEX3
:
879 case VARYING_SLOT_TEX4
:
880 case VARYING_SLOT_TEX5
:
881 case VARYING_SLOT_TEX6
:
882 case VARYING_SLOT_TEX7
:
883 if (st
->needs_texcoord_semantic
) {
884 input_semantic_name
[slot
] = TGSI_SEMANTIC_TEXCOORD
;
885 input_semantic_index
[slot
] = attr
- VARYING_SLOT_TEX0
;
886 interpMode
[slot
] = stfp
->glsl_to_tgsi
?
887 TGSI_INTERPOLATE_COUNT
: TGSI_INTERPOLATE_PERSPECTIVE
;
891 case VARYING_SLOT_VAR0
:
893 /* Semantic indices should be zero-based because drivers may choose
894 * to assign a fixed slot determined by that index.
895 * This is useful because ARB_separate_shader_objects uses location
896 * qualifiers for linkage, and if the semantic index corresponds to
897 * these locations, linkage passes in the driver become unecessary.
899 * If needs_texcoord_semantic is true, no semantic indices will be
900 * consumed for the TEXi varyings, and we can base the locations of
901 * the user varyings on VAR0. Otherwise, we use TEX0 as base index.
903 assert(attr
>= VARYING_SLOT_VAR0
|| attr
== VARYING_SLOT_PNTC
||
904 (attr
>= VARYING_SLOT_TEX0
&& attr
<= VARYING_SLOT_TEX7
));
905 input_semantic_name
[slot
] = TGSI_SEMANTIC_GENERIC
;
906 input_semantic_index
[slot
] = st_get_generic_varying_index(st
, attr
);
907 if (attr
== VARYING_SLOT_PNTC
)
908 interpMode
[slot
] = TGSI_INTERPOLATE_LINEAR
;
910 interpMode
[slot
] = stfp
->glsl_to_tgsi
?
911 TGSI_INTERPOLATE_COUNT
: TGSI_INTERPOLATE_PERSPECTIVE
;
917 inputMapping
[attr
] = -1;
922 * Semantics and mapping for outputs
924 GLbitfield64 outputsWritten
= stfp
->Base
.info
.outputs_written
;
926 /* if z is written, emit that first */
927 if (outputsWritten
& BITFIELD64_BIT(FRAG_RESULT_DEPTH
)) {
928 fs_output_semantic_name
[fs_num_outputs
] = TGSI_SEMANTIC_POSITION
;
929 fs_output_semantic_index
[fs_num_outputs
] = 0;
930 outputMapping
[FRAG_RESULT_DEPTH
] = fs_num_outputs
;
932 outputsWritten
&= ~(1 << FRAG_RESULT_DEPTH
);
935 if (outputsWritten
& BITFIELD64_BIT(FRAG_RESULT_STENCIL
)) {
936 fs_output_semantic_name
[fs_num_outputs
] = TGSI_SEMANTIC_STENCIL
;
937 fs_output_semantic_index
[fs_num_outputs
] = 0;
938 outputMapping
[FRAG_RESULT_STENCIL
] = fs_num_outputs
;
940 outputsWritten
&= ~(1 << FRAG_RESULT_STENCIL
);
943 if (outputsWritten
& BITFIELD64_BIT(FRAG_RESULT_SAMPLE_MASK
)) {
944 fs_output_semantic_name
[fs_num_outputs
] = TGSI_SEMANTIC_SAMPLEMASK
;
945 fs_output_semantic_index
[fs_num_outputs
] = 0;
946 outputMapping
[FRAG_RESULT_SAMPLE_MASK
] = fs_num_outputs
;
948 outputsWritten
&= ~(1 << FRAG_RESULT_SAMPLE_MASK
);
951 /* handle remaining outputs (color) */
952 for (attr
= 0; attr
< ARRAY_SIZE(outputMapping
); attr
++) {
953 const GLbitfield64 written
= attr
< FRAG_RESULT_MAX
? outputsWritten
:
954 stfp
->Base
.SecondaryOutputsWritten
;
955 const unsigned loc
= attr
% FRAG_RESULT_MAX
;
957 if (written
& BITFIELD64_BIT(loc
)) {
959 case FRAG_RESULT_DEPTH
:
960 case FRAG_RESULT_STENCIL
:
961 case FRAG_RESULT_SAMPLE_MASK
:
965 case FRAG_RESULT_COLOR
:
966 write_all
= GL_TRUE
; /* fallthrough */
969 assert(loc
== FRAG_RESULT_COLOR
||
970 (FRAG_RESULT_DATA0
<= loc
&& loc
< FRAG_RESULT_MAX
));
972 index
= (loc
== FRAG_RESULT_COLOR
) ? 0 : (loc
- FRAG_RESULT_DATA0
);
974 if (attr
>= FRAG_RESULT_MAX
) {
975 /* Secondary color for dual source blending. */
980 fs_output_semantic_name
[fs_num_outputs
] = TGSI_SEMANTIC_COLOR
;
981 fs_output_semantic_index
[fs_num_outputs
] = index
;
982 outputMapping
[attr
] = fs_num_outputs
;
991 ureg
= ureg_create_with_screen(PIPE_SHADER_FRAGMENT
, st
->pipe
->screen
);
995 if (ST_DEBUG
& DEBUG_MESA
) {
996 _mesa_print_program(&stfp
->Base
);
997 _mesa_print_program_parameters(st
->ctx
, &stfp
->Base
);
1000 if (write_all
== GL_TRUE
)
1001 ureg_property(ureg
, TGSI_PROPERTY_FS_COLOR0_WRITES_ALL_CBUFS
, 1);
1003 if (stfp
->Base
.info
.fs
.depth_layout
!= FRAG_DEPTH_LAYOUT_NONE
) {
1004 switch (stfp
->Base
.info
.fs
.depth_layout
) {
1005 case FRAG_DEPTH_LAYOUT_ANY
:
1006 ureg_property(ureg
, TGSI_PROPERTY_FS_DEPTH_LAYOUT
,
1007 TGSI_FS_DEPTH_LAYOUT_ANY
);
1009 case FRAG_DEPTH_LAYOUT_GREATER
:
1010 ureg_property(ureg
, TGSI_PROPERTY_FS_DEPTH_LAYOUT
,
1011 TGSI_FS_DEPTH_LAYOUT_GREATER
);
1013 case FRAG_DEPTH_LAYOUT_LESS
:
1014 ureg_property(ureg
, TGSI_PROPERTY_FS_DEPTH_LAYOUT
,
1015 TGSI_FS_DEPTH_LAYOUT_LESS
);
1017 case FRAG_DEPTH_LAYOUT_UNCHANGED
:
1018 ureg_property(ureg
, TGSI_PROPERTY_FS_DEPTH_LAYOUT
,
1019 TGSI_FS_DEPTH_LAYOUT_UNCHANGED
);
1026 if (stfp
->glsl_to_tgsi
) {
1027 st_translate_program(st
->ctx
,
1028 PIPE_SHADER_FRAGMENT
,
1036 input_semantic_name
,
1037 input_semantic_index
,
1042 fs_output_semantic_name
,
1043 fs_output_semantic_index
);
1045 free_glsl_to_tgsi_visitor(stfp
->glsl_to_tgsi
);
1046 } else if (stfp
->ati_fs
)
1047 st_translate_atifs_program(ureg
,
1053 input_semantic_name
,
1054 input_semantic_index
,
1059 fs_output_semantic_name
,
1060 fs_output_semantic_index
);
1062 st_translate_mesa_program(st
->ctx
,
1063 PIPE_SHADER_FRAGMENT
,
1069 input_semantic_name
,
1070 input_semantic_index
,
1075 fs_output_semantic_name
,
1076 fs_output_semantic_index
);
1078 stfp
->tgsi
.tokens
= ureg_get_tokens(ureg
, &stfp
->num_tgsi_tokens
);
1081 if (stfp
->glsl_to_tgsi
) {
1082 stfp
->glsl_to_tgsi
= NULL
;
1083 st_store_ir_in_disk_cache(st
, &stfp
->Base
, false);
1086 return stfp
->tgsi
.tokens
!= NULL
;
1089 static struct st_fp_variant
*
1090 st_create_fp_variant(struct st_context
*st
,
1091 struct st_fragment_program
*stfp
,
1092 const struct st_fp_variant_key
*key
)
1094 struct pipe_context
*pipe
= st
->pipe
;
1095 struct st_fp_variant
*variant
= CALLOC_STRUCT(st_fp_variant
);
1096 struct pipe_shader_state tgsi
= {0};
1097 struct gl_program_parameter_list
*params
= stfp
->Base
.Parameters
;
1098 static const gl_state_index16 texcoord_state
[STATE_LENGTH
] =
1099 { STATE_INTERNAL
, STATE_CURRENT_ATTRIB
, VERT_ATTRIB_TEX0
};
1100 static const gl_state_index16 scale_state
[STATE_LENGTH
] =
1101 { STATE_INTERNAL
, STATE_PT_SCALE
};
1102 static const gl_state_index16 bias_state
[STATE_LENGTH
] =
1103 { STATE_INTERNAL
, STATE_PT_BIAS
};
1108 if (stfp
->tgsi
.type
== PIPE_SHADER_IR_NIR
) {
1109 tgsi
.type
= PIPE_SHADER_IR_NIR
;
1110 tgsi
.ir
.nir
= nir_shader_clone(NULL
, stfp
->tgsi
.ir
.nir
);
1112 if (key
->clamp_color
)
1113 NIR_PASS_V(tgsi
.ir
.nir
, nir_lower_clamp_color_outputs
);
1115 if (key
->persample_shading
) {
1116 nir_shader
*shader
= tgsi
.ir
.nir
;
1117 nir_foreach_variable(var
, &shader
->inputs
)
1118 var
->data
.sample
= true;
1121 assert(!(key
->bitmap
&& key
->drawpixels
));
1125 nir_lower_bitmap_options options
= {0};
1127 variant
->bitmap_sampler
= ffs(~stfp
->Base
.SamplersUsed
) - 1;
1128 options
.sampler
= variant
->bitmap_sampler
;
1129 options
.swizzle_xxxx
= (st
->bitmap
.tex_format
== PIPE_FORMAT_L8_UNORM
);
1131 NIR_PASS_V(tgsi
.ir
.nir
, nir_lower_bitmap
, &options
);
1134 /* glDrawPixels (color only) */
1135 if (key
->drawpixels
) {
1136 nir_lower_drawpixels_options options
= {{0}};
1137 unsigned samplers_used
= stfp
->Base
.SamplersUsed
;
1139 /* Find the first unused slot. */
1140 variant
->drawpix_sampler
= ffs(~samplers_used
) - 1;
1141 options
.drawpix_sampler
= variant
->drawpix_sampler
;
1142 samplers_used
|= (1 << variant
->drawpix_sampler
);
1144 options
.pixel_maps
= key
->pixelMaps
;
1145 if (key
->pixelMaps
) {
1146 variant
->pixelmap_sampler
= ffs(~samplers_used
) - 1;
1147 options
.pixelmap_sampler
= variant
->pixelmap_sampler
;
1150 options
.scale_and_bias
= key
->scaleAndBias
;
1151 if (key
->scaleAndBias
) {
1152 _mesa_add_state_reference(params
, scale_state
);
1153 memcpy(options
.scale_state_tokens
, scale_state
,
1154 sizeof(options
.scale_state_tokens
));
1155 _mesa_add_state_reference(params
, bias_state
);
1156 memcpy(options
.bias_state_tokens
, bias_state
,
1157 sizeof(options
.bias_state_tokens
));
1160 _mesa_add_state_reference(params
, texcoord_state
);
1161 memcpy(options
.texcoord_state_tokens
, texcoord_state
,
1162 sizeof(options
.texcoord_state_tokens
));
1164 NIR_PASS_V(tgsi
.ir
.nir
, nir_lower_drawpixels
, &options
);
1167 if (unlikely(key
->external
.lower_nv12
|| key
->external
.lower_iyuv
)) {
1168 nir_lower_tex_options options
= {0};
1169 options
.lower_y_uv_external
= key
->external
.lower_nv12
;
1170 options
.lower_y_u_v_external
= key
->external
.lower_iyuv
;
1171 NIR_PASS_V(tgsi
.ir
.nir
, nir_lower_tex
, &options
);
1174 st_finalize_nir(st
, &stfp
->Base
, stfp
->shader_program
, tgsi
.ir
.nir
);
1176 if (unlikely(key
->external
.lower_nv12
|| key
->external
.lower_iyuv
)) {
1177 /* This pass needs to happen *after* nir_lower_sampler */
1178 NIR_PASS_V(tgsi
.ir
.nir
, st_nir_lower_tex_src_plane
,
1179 ~stfp
->Base
.SamplersUsed
,
1180 key
->external
.lower_nv12
,
1181 key
->external
.lower_iyuv
);
1184 /* Some of the lowering above may have introduced new varyings */
1185 nir_shader_gather_info(tgsi
.ir
.nir
,
1186 nir_shader_get_entrypoint(tgsi
.ir
.nir
));
1188 variant
->driver_shader
= pipe
->create_fs_state(pipe
, &tgsi
);
1189 variant
->key
= *key
;
1194 tgsi
.tokens
= stfp
->tgsi
.tokens
;
1196 assert(!(key
->bitmap
&& key
->drawpixels
));
1198 /* Fix texture targets and add fog for ATI_fs */
1200 const struct tgsi_token
*tokens
= st_fixup_atifs(tgsi
.tokens
, key
);
1203 tgsi
.tokens
= tokens
;
1205 fprintf(stderr
, "mesa: cannot post-process ATI_fs\n");
1208 /* Emulate features. */
1209 if (key
->clamp_color
|| key
->persample_shading
) {
1210 const struct tgsi_token
*tokens
;
1212 (key
->clamp_color
? TGSI_EMU_CLAMP_COLOR_OUTPUTS
: 0) |
1213 (key
->persample_shading
? TGSI_EMU_FORCE_PERSAMPLE_INTERP
: 0);
1215 tokens
= tgsi_emulate(tgsi
.tokens
, flags
);
1218 if (tgsi
.tokens
!= stfp
->tgsi
.tokens
)
1219 tgsi_free_tokens(tgsi
.tokens
);
1220 tgsi
.tokens
= tokens
;
1222 fprintf(stderr
, "mesa: cannot emulate deprecated features\n");
1227 const struct tgsi_token
*tokens
;
1229 variant
->bitmap_sampler
= ffs(~stfp
->Base
.SamplersUsed
) - 1;
1231 tokens
= st_get_bitmap_shader(tgsi
.tokens
,
1232 st
->internal_target
,
1233 variant
->bitmap_sampler
,
1234 st
->needs_texcoord_semantic
,
1235 st
->bitmap
.tex_format
==
1236 PIPE_FORMAT_L8_UNORM
);
1239 if (tgsi
.tokens
!= stfp
->tgsi
.tokens
)
1240 tgsi_free_tokens(tgsi
.tokens
);
1241 tgsi
.tokens
= tokens
;
1243 fprintf(stderr
, "mesa: cannot create a shader for glBitmap\n");
1246 /* glDrawPixels (color only) */
1247 if (key
->drawpixels
) {
1248 const struct tgsi_token
*tokens
;
1249 unsigned scale_const
= 0, bias_const
= 0, texcoord_const
= 0;
1251 /* Find the first unused slot. */
1252 variant
->drawpix_sampler
= ffs(~stfp
->Base
.SamplersUsed
) - 1;
1254 if (key
->pixelMaps
) {
1255 unsigned samplers_used
= stfp
->Base
.SamplersUsed
|
1256 (1 << variant
->drawpix_sampler
);
1258 variant
->pixelmap_sampler
= ffs(~samplers_used
) - 1;
1261 if (key
->scaleAndBias
) {
1262 scale_const
= _mesa_add_state_reference(params
, scale_state
);
1263 bias_const
= _mesa_add_state_reference(params
, bias_state
);
1266 texcoord_const
= _mesa_add_state_reference(params
, texcoord_state
);
1268 tokens
= st_get_drawpix_shader(tgsi
.tokens
,
1269 st
->needs_texcoord_semantic
,
1270 key
->scaleAndBias
, scale_const
,
1271 bias_const
, key
->pixelMaps
,
1272 variant
->drawpix_sampler
,
1273 variant
->pixelmap_sampler
,
1274 texcoord_const
, st
->internal_target
);
1277 if (tgsi
.tokens
!= stfp
->tgsi
.tokens
)
1278 tgsi_free_tokens(tgsi
.tokens
);
1279 tgsi
.tokens
= tokens
;
1281 fprintf(stderr
, "mesa: cannot create a shader for glDrawPixels\n");
1284 if (unlikely(key
->external
.lower_nv12
|| key
->external
.lower_iyuv
)) {
1285 const struct tgsi_token
*tokens
;
1287 /* samplers inserted would conflict, but this should be unpossible: */
1288 assert(!(key
->bitmap
|| key
->drawpixels
));
1290 tokens
= st_tgsi_lower_yuv(tgsi
.tokens
,
1291 ~stfp
->Base
.SamplersUsed
,
1292 key
->external
.lower_nv12
,
1293 key
->external
.lower_iyuv
);
1295 if (tgsi
.tokens
!= stfp
->tgsi
.tokens
)
1296 tgsi_free_tokens(tgsi
.tokens
);
1297 tgsi
.tokens
= tokens
;
1299 fprintf(stderr
, "mesa: cannot create a shader for samplerExternalOES\n");
1303 if (ST_DEBUG
& DEBUG_TGSI
) {
1304 tgsi_dump(tgsi
.tokens
, 0);
1308 /* fill in variant */
1309 variant
->driver_shader
= pipe
->create_fs_state(pipe
, &tgsi
);
1310 variant
->key
= *key
;
1312 if (tgsi
.tokens
!= stfp
->tgsi
.tokens
)
1313 tgsi_free_tokens(tgsi
.tokens
);
1318 * Translate fragment program if needed.
1320 struct st_fp_variant
*
1321 st_get_fp_variant(struct st_context
*st
,
1322 struct st_fragment_program
*stfp
,
1323 const struct st_fp_variant_key
*key
)
1325 struct st_fp_variant
*fpv
;
1327 /* Search for existing variant */
1328 for (fpv
= stfp
->variants
; fpv
; fpv
= fpv
->next
) {
1329 if (memcmp(&fpv
->key
, key
, sizeof(*key
)) == 0) {
1336 fpv
= st_create_fp_variant(st
, stfp
, key
);
1338 if (key
->bitmap
|| key
->drawpixels
) {
1339 /* Regular variants should always come before the
1340 * bitmap & drawpixels variants, (unless there
1341 * are no regular variants) so that
1342 * st_update_fp can take a fast path when
1343 * shader_has_one_variant is set.
1345 if (!stfp
->variants
) {
1346 stfp
->variants
= fpv
;
1348 /* insert into list after the first one */
1349 fpv
->next
= stfp
->variants
->next
;
1350 stfp
->variants
->next
= fpv
;
1353 /* insert into list */
1354 fpv
->next
= stfp
->variants
;
1355 stfp
->variants
= fpv
;
1365 * Translate a program. This is common code for geometry and tessellation
1369 st_translate_program_common(struct st_context
*st
,
1370 struct gl_program
*prog
,
1371 struct glsl_to_tgsi_visitor
*glsl_to_tgsi
,
1372 struct ureg_program
*ureg
,
1373 unsigned tgsi_processor
,
1374 struct pipe_shader_state
*out_state
)
1376 ubyte inputSlotToAttr
[VARYING_SLOT_TESS_MAX
];
1377 ubyte inputMapping
[VARYING_SLOT_TESS_MAX
];
1378 ubyte outputMapping
[VARYING_SLOT_TESS_MAX
];
1381 ubyte input_semantic_name
[PIPE_MAX_SHADER_INPUTS
];
1382 ubyte input_semantic_index
[PIPE_MAX_SHADER_INPUTS
];
1383 uint num_inputs
= 0;
1385 ubyte output_semantic_name
[PIPE_MAX_SHADER_OUTPUTS
];
1386 ubyte output_semantic_index
[PIPE_MAX_SHADER_OUTPUTS
];
1387 uint num_outputs
= 0;
1391 memset(inputSlotToAttr
, 0, sizeof(inputSlotToAttr
));
1392 memset(inputMapping
, 0, sizeof(inputMapping
));
1393 memset(outputMapping
, 0, sizeof(outputMapping
));
1394 memset(out_state
, 0, sizeof(*out_state
));
1396 if (prog
->info
.clip_distance_array_size
)
1397 ureg_property(ureg
, TGSI_PROPERTY_NUM_CLIPDIST_ENABLED
,
1398 prog
->info
.clip_distance_array_size
);
1399 if (prog
->info
.cull_distance_array_size
)
1400 ureg_property(ureg
, TGSI_PROPERTY_NUM_CULLDIST_ENABLED
,
1401 prog
->info
.cull_distance_array_size
);
1404 * Convert Mesa program inputs to TGSI input register semantics.
1406 for (attr
= 0; attr
< VARYING_SLOT_MAX
; attr
++) {
1407 if ((prog
->info
.inputs_read
& BITFIELD64_BIT(attr
)) == 0)
1410 unsigned slot
= num_inputs
++;
1412 inputMapping
[attr
] = slot
;
1413 inputSlotToAttr
[slot
] = attr
;
1415 unsigned semantic_name
, semantic_index
;
1416 tgsi_get_gl_varying_semantic(attr
, st
->needs_texcoord_semantic
,
1417 &semantic_name
, &semantic_index
);
1418 input_semantic_name
[slot
] = semantic_name
;
1419 input_semantic_index
[slot
] = semantic_index
;
1422 /* Also add patch inputs. */
1423 for (attr
= 0; attr
< 32; attr
++) {
1424 if (prog
->info
.patch_inputs_read
& (1u << attr
)) {
1425 GLuint slot
= num_inputs
++;
1426 GLuint patch_attr
= VARYING_SLOT_PATCH0
+ attr
;
1428 inputMapping
[patch_attr
] = slot
;
1429 inputSlotToAttr
[slot
] = patch_attr
;
1430 input_semantic_name
[slot
] = TGSI_SEMANTIC_PATCH
;
1431 input_semantic_index
[slot
] = attr
;
1435 /* initialize output semantics to defaults */
1436 for (i
= 0; i
< PIPE_MAX_SHADER_OUTPUTS
; i
++) {
1437 output_semantic_name
[i
] = TGSI_SEMANTIC_GENERIC
;
1438 output_semantic_index
[i
] = 0;
1442 * Determine number of outputs, the (default) output register
1443 * mapping and the semantic information for each output.
1445 for (attr
= 0; attr
< VARYING_SLOT_MAX
; attr
++) {
1446 if (prog
->info
.outputs_written
& BITFIELD64_BIT(attr
)) {
1447 GLuint slot
= num_outputs
++;
1449 outputMapping
[attr
] = slot
;
1451 unsigned semantic_name
, semantic_index
;
1452 tgsi_get_gl_varying_semantic(attr
, st
->needs_texcoord_semantic
,
1453 &semantic_name
, &semantic_index
);
1454 output_semantic_name
[slot
] = semantic_name
;
1455 output_semantic_index
[slot
] = semantic_index
;
1459 /* Also add patch outputs. */
1460 for (attr
= 0; attr
< 32; attr
++) {
1461 if (prog
->info
.patch_outputs_written
& (1u << attr
)) {
1462 GLuint slot
= num_outputs
++;
1463 GLuint patch_attr
= VARYING_SLOT_PATCH0
+ attr
;
1465 outputMapping
[patch_attr
] = slot
;
1466 output_semantic_name
[slot
] = TGSI_SEMANTIC_PATCH
;
1467 output_semantic_index
[slot
] = attr
;
1471 st_translate_program(st
->ctx
,
1480 input_semantic_name
,
1481 input_semantic_index
,
1486 output_semantic_name
,
1487 output_semantic_index
);
1489 if (tgsi_processor
== PIPE_SHADER_COMPUTE
) {
1490 struct st_compute_program
*stcp
= (struct st_compute_program
*) prog
;
1491 out_state
->tokens
= ureg_get_tokens(ureg
, &stcp
->num_tgsi_tokens
);
1492 stcp
->tgsi
.prog
= out_state
->tokens
;
1494 struct st_common_program
*stcp
= (struct st_common_program
*) prog
;
1495 out_state
->tokens
= ureg_get_tokens(ureg
, &stcp
->num_tgsi_tokens
);
1499 st_translate_stream_output_info(prog
->sh
.LinkedTransformFeedback
,
1501 &out_state
->stream_output
);
1503 st_store_ir_in_disk_cache(st
, prog
, false);
1505 if ((ST_DEBUG
& DEBUG_TGSI
) && (ST_DEBUG
& DEBUG_MESA
)) {
1506 _mesa_print_program(prog
);
1510 if (ST_DEBUG
& DEBUG_TGSI
) {
1511 tgsi_dump(out_state
->tokens
, 0);
1517 * Update stream-output info for GS/TCS/TES. Normally this is done in
1518 * st_translate_program_common() but that is not called for glsl_to_nir
1522 st_translate_program_stream_output(struct gl_program
*prog
,
1523 struct pipe_stream_output_info
*stream_output
)
1525 if (!prog
->sh
.LinkedTransformFeedback
)
1528 ubyte outputMapping
[VARYING_SLOT_TESS_MAX
];
1530 uint num_outputs
= 0;
1532 memset(outputMapping
, 0, sizeof(outputMapping
));
1535 * Determine number of outputs, the (default) output register
1536 * mapping and the semantic information for each output.
1538 for (attr
= 0; attr
< VARYING_SLOT_MAX
; attr
++) {
1539 if (prog
->info
.outputs_written
& BITFIELD64_BIT(attr
)) {
1540 GLuint slot
= num_outputs
++;
1542 outputMapping
[attr
] = slot
;
1546 st_translate_stream_output_info(prog
->sh
.LinkedTransformFeedback
,
1552 * Translate a geometry program to create a new variant.
1555 st_translate_geometry_program(struct st_context
*st
,
1556 struct st_common_program
*stgp
)
1558 struct ureg_program
*ureg
;
1560 /* We have already compiled to NIR so just return */
1561 if (stgp
->shader_program
) {
1563 st_finalize_nir(st
, &stgp
->Base
, stgp
->shader_program
,
1565 st_translate_program_stream_output(&stgp
->Base
, &stgp
->tgsi
.stream_output
);
1566 st_store_ir_in_disk_cache(st
, &stgp
->Base
, true);
1570 ureg
= ureg_create_with_screen(PIPE_SHADER_GEOMETRY
, st
->pipe
->screen
);
1574 ureg_property(ureg
, TGSI_PROPERTY_GS_INPUT_PRIM
,
1575 stgp
->Base
.info
.gs
.input_primitive
);
1576 ureg_property(ureg
, TGSI_PROPERTY_GS_OUTPUT_PRIM
,
1577 stgp
->Base
.info
.gs
.output_primitive
);
1578 ureg_property(ureg
, TGSI_PROPERTY_GS_MAX_OUTPUT_VERTICES
,
1579 stgp
->Base
.info
.gs
.vertices_out
);
1580 ureg_property(ureg
, TGSI_PROPERTY_GS_INVOCATIONS
,
1581 stgp
->Base
.info
.gs
.invocations
);
1583 st_translate_program_common(st
, &stgp
->Base
, stgp
->glsl_to_tgsi
, ureg
,
1584 PIPE_SHADER_GEOMETRY
, &stgp
->tgsi
);
1586 free_glsl_to_tgsi_visitor(stgp
->glsl_to_tgsi
);
1587 stgp
->glsl_to_tgsi
= NULL
;
1593 * Get/create a basic program variant.
1595 struct st_basic_variant
*
1596 st_get_basic_variant(struct st_context
*st
,
1597 unsigned pipe_shader
,
1598 struct st_common_program
*prog
)
1600 struct pipe_context
*pipe
= st
->pipe
;
1601 struct st_basic_variant
*v
;
1602 struct st_basic_variant_key key
;
1603 struct pipe_shader_state tgsi
= {0};
1604 memset(&key
, 0, sizeof(key
));
1605 key
.st
= st
->has_shareable_shaders
? NULL
: st
;
1607 /* Search for existing variant */
1608 for (v
= prog
->variants
; v
; v
= v
->next
) {
1609 if (memcmp(&v
->key
, &key
, sizeof(key
)) == 0) {
1616 v
= CALLOC_STRUCT(st_basic_variant
);
1619 if (prog
->tgsi
.type
== PIPE_SHADER_IR_NIR
) {
1620 tgsi
.type
= PIPE_SHADER_IR_NIR
;
1621 tgsi
.ir
.nir
= nir_shader_clone(NULL
, prog
->tgsi
.ir
.nir
);
1622 tgsi
.stream_output
= prog
->tgsi
.stream_output
;
1625 /* fill in new variant */
1626 switch (pipe_shader
) {
1627 case PIPE_SHADER_TESS_CTRL
:
1628 v
->driver_shader
= pipe
->create_tcs_state(pipe
, &tgsi
);
1630 case PIPE_SHADER_TESS_EVAL
:
1631 v
->driver_shader
= pipe
->create_tes_state(pipe
, &tgsi
);
1633 case PIPE_SHADER_GEOMETRY
:
1634 v
->driver_shader
= pipe
->create_gs_state(pipe
, &tgsi
);
1637 assert(!"unhandled shader type");
1644 /* insert into list */
1645 v
->next
= prog
->variants
;
1655 * Translate a tessellation control program to create a new variant.
1658 st_translate_tessctrl_program(struct st_context
*st
,
1659 struct st_common_program
*sttcp
)
1661 struct ureg_program
*ureg
;
1663 /* We have already compiled to NIR so just return */
1664 if (sttcp
->shader_program
) {
1666 st_finalize_nir(st
, &sttcp
->Base
, sttcp
->shader_program
,
1667 sttcp
->tgsi
.ir
.nir
);
1668 st_store_ir_in_disk_cache(st
, &sttcp
->Base
, true);
1672 ureg
= ureg_create_with_screen(PIPE_SHADER_TESS_CTRL
, st
->pipe
->screen
);
1676 ureg_property(ureg
, TGSI_PROPERTY_TCS_VERTICES_OUT
,
1677 sttcp
->Base
.info
.tess
.tcs_vertices_out
);
1679 st_translate_program_common(st
, &sttcp
->Base
, sttcp
->glsl_to_tgsi
, ureg
,
1680 PIPE_SHADER_TESS_CTRL
, &sttcp
->tgsi
);
1682 free_glsl_to_tgsi_visitor(sttcp
->glsl_to_tgsi
);
1683 sttcp
->glsl_to_tgsi
= NULL
;
1689 * Translate a tessellation evaluation program to create a new variant.
1692 st_translate_tesseval_program(struct st_context
*st
,
1693 struct st_common_program
*sttep
)
1695 struct ureg_program
*ureg
;
1697 /* We have already compiled to NIR so just return */
1698 if (sttep
->shader_program
) {
1700 st_finalize_nir(st
, &sttep
->Base
, sttep
->shader_program
,
1701 sttep
->tgsi
.ir
.nir
);
1702 st_translate_program_stream_output(&sttep
->Base
, &sttep
->tgsi
.stream_output
);
1703 st_store_ir_in_disk_cache(st
, &sttep
->Base
, true);
1707 ureg
= ureg_create_with_screen(PIPE_SHADER_TESS_EVAL
, st
->pipe
->screen
);
1711 if (sttep
->Base
.info
.tess
.primitive_mode
== GL_ISOLINES
)
1712 ureg_property(ureg
, TGSI_PROPERTY_TES_PRIM_MODE
, GL_LINES
);
1714 ureg_property(ureg
, TGSI_PROPERTY_TES_PRIM_MODE
,
1715 sttep
->Base
.info
.tess
.primitive_mode
);
1717 STATIC_ASSERT((TESS_SPACING_EQUAL
+ 1) % 3 == PIPE_TESS_SPACING_EQUAL
);
1718 STATIC_ASSERT((TESS_SPACING_FRACTIONAL_ODD
+ 1) % 3 ==
1719 PIPE_TESS_SPACING_FRACTIONAL_ODD
);
1720 STATIC_ASSERT((TESS_SPACING_FRACTIONAL_EVEN
+ 1) % 3 ==
1721 PIPE_TESS_SPACING_FRACTIONAL_EVEN
);
1723 ureg_property(ureg
, TGSI_PROPERTY_TES_SPACING
,
1724 (sttep
->Base
.info
.tess
.spacing
+ 1) % 3);
1726 ureg_property(ureg
, TGSI_PROPERTY_TES_VERTEX_ORDER_CW
,
1727 !sttep
->Base
.info
.tess
.ccw
);
1728 ureg_property(ureg
, TGSI_PROPERTY_TES_POINT_MODE
,
1729 sttep
->Base
.info
.tess
.point_mode
);
1731 st_translate_program_common(st
, &sttep
->Base
, sttep
->glsl_to_tgsi
,
1732 ureg
, PIPE_SHADER_TESS_EVAL
, &sttep
->tgsi
);
1734 free_glsl_to_tgsi_visitor(sttep
->glsl_to_tgsi
);
1735 sttep
->glsl_to_tgsi
= NULL
;
1741 * Translate a compute program to create a new variant.
1744 st_translate_compute_program(struct st_context
*st
,
1745 struct st_compute_program
*stcp
)
1747 struct ureg_program
*ureg
;
1748 struct pipe_shader_state prog
;
1750 stcp
->tgsi
.req_local_mem
= stcp
->Base
.info
.cs
.shared_size
;
1752 if (stcp
->shader_program
) {
1753 /* no compute variants: */
1754 st_finalize_nir(st
, &stcp
->Base
, stcp
->shader_program
,
1755 (struct nir_shader
*) stcp
->tgsi
.prog
);
1756 st_store_ir_in_disk_cache(st
, &stcp
->Base
, true);
1760 ureg
= ureg_create_with_screen(PIPE_SHADER_COMPUTE
, st
->pipe
->screen
);
1764 st_translate_program_common(st
, &stcp
->Base
, stcp
->glsl_to_tgsi
, ureg
,
1765 PIPE_SHADER_COMPUTE
, &prog
);
1767 stcp
->tgsi
.ir_type
= PIPE_SHADER_IR_TGSI
;
1768 stcp
->tgsi
.req_private_mem
= 0;
1769 stcp
->tgsi
.req_input_mem
= 0;
1771 free_glsl_to_tgsi_visitor(stcp
->glsl_to_tgsi
);
1772 stcp
->glsl_to_tgsi
= NULL
;
1778 * Get/create compute program variant.
1780 struct st_basic_variant
*
1781 st_get_cp_variant(struct st_context
*st
,
1782 struct pipe_compute_state
*tgsi
,
1783 struct st_basic_variant
**variants
)
1785 struct pipe_context
*pipe
= st
->pipe
;
1786 struct st_basic_variant
*v
;
1787 struct st_basic_variant_key key
;
1789 /* use memset, not an initializer to be sure all memory is zeroed */
1790 memset(&key
, 0, sizeof(key
));
1792 key
.st
= st
->has_shareable_shaders
? NULL
: st
;
1794 /* Search for existing variant */
1795 for (v
= *variants
; v
; v
= v
->next
) {
1796 if (memcmp(&v
->key
, &key
, sizeof(key
)) == 0) {
1803 v
= CALLOC_STRUCT(st_basic_variant
);
1805 /* fill in new variant */
1806 struct pipe_compute_state cs
= *tgsi
;
1807 if (tgsi
->ir_type
== PIPE_SHADER_IR_NIR
)
1808 cs
.prog
= nir_shader_clone(NULL
, tgsi
->prog
);
1809 v
->driver_shader
= pipe
->create_compute_state(pipe
, &cs
);
1812 /* insert into list */
1813 v
->next
= *variants
;
1823 * Vert/Geom/Frag programs have per-context variants. Free all the
1824 * variants attached to the given program which match the given context.
1827 destroy_program_variants(struct st_context
*st
, struct gl_program
*target
)
1829 if (!target
|| target
== &_mesa_DummyProgram
)
1832 switch (target
->Target
) {
1833 case GL_VERTEX_PROGRAM_ARB
:
1835 struct st_vertex_program
*stvp
= (struct st_vertex_program
*) target
;
1836 struct st_vp_variant
*vpv
, **prevPtr
= &stvp
->variants
;
1838 for (vpv
= stvp
->variants
; vpv
; ) {
1839 struct st_vp_variant
*next
= vpv
->next
;
1840 if (vpv
->key
.st
== st
) {
1841 /* unlink from list */
1843 /* destroy this variant */
1844 delete_vp_variant(st
, vpv
);
1847 prevPtr
= &vpv
->next
;
1853 case GL_FRAGMENT_PROGRAM_ARB
:
1855 struct st_fragment_program
*stfp
=
1856 (struct st_fragment_program
*) target
;
1857 struct st_fp_variant
*fpv
, **prevPtr
= &stfp
->variants
;
1859 for (fpv
= stfp
->variants
; fpv
; ) {
1860 struct st_fp_variant
*next
= fpv
->next
;
1861 if (fpv
->key
.st
== st
) {
1862 /* unlink from list */
1864 /* destroy this variant */
1865 delete_fp_variant(st
, fpv
);
1868 prevPtr
= &fpv
->next
;
1874 case GL_GEOMETRY_PROGRAM_NV
:
1875 case GL_TESS_CONTROL_PROGRAM_NV
:
1876 case GL_TESS_EVALUATION_PROGRAM_NV
:
1877 case GL_COMPUTE_PROGRAM_NV
:
1879 struct st_common_program
*p
= st_common_program(target
);
1880 struct st_compute_program
*cp
= (struct st_compute_program
*)target
;
1881 struct st_basic_variant
**variants
=
1882 target
->Target
== GL_COMPUTE_PROGRAM_NV
? &cp
->variants
:
1884 struct st_basic_variant
*v
, **prevPtr
= variants
;
1886 for (v
= *variants
; v
; ) {
1887 struct st_basic_variant
*next
= v
->next
;
1888 if (v
->key
.st
== st
) {
1889 /* unlink from list */
1891 /* destroy this variant */
1892 delete_basic_variant(st
, v
, target
->Target
);
1902 _mesa_problem(NULL
, "Unexpected program target 0x%x in "
1903 "destroy_program_variants_cb()", target
->Target
);
1909 * Callback for _mesa_HashWalk. Free all the shader's program variants
1910 * which match the given context.
1913 destroy_shader_program_variants_cb(GLuint key
, void *data
, void *userData
)
1915 struct st_context
*st
= (struct st_context
*) userData
;
1916 struct gl_shader
*shader
= (struct gl_shader
*) data
;
1918 switch (shader
->Type
) {
1919 case GL_SHADER_PROGRAM_MESA
:
1921 struct gl_shader_program
*shProg
= (struct gl_shader_program
*) data
;
1924 for (i
= 0; i
< ARRAY_SIZE(shProg
->_LinkedShaders
); i
++) {
1925 if (shProg
->_LinkedShaders
[i
])
1926 destroy_program_variants(st
, shProg
->_LinkedShaders
[i
]->Program
);
1930 case GL_VERTEX_SHADER
:
1931 case GL_FRAGMENT_SHADER
:
1932 case GL_GEOMETRY_SHADER
:
1933 case GL_TESS_CONTROL_SHADER
:
1934 case GL_TESS_EVALUATION_SHADER
:
1935 case GL_COMPUTE_SHADER
:
1944 * Callback for _mesa_HashWalk. Free all the program variants which match
1945 * the given context.
1948 destroy_program_variants_cb(GLuint key
, void *data
, void *userData
)
1950 struct st_context
*st
= (struct st_context
*) userData
;
1951 struct gl_program
*program
= (struct gl_program
*) data
;
1952 destroy_program_variants(st
, program
);
1957 * Walk over all shaders and programs to delete any variants which
1958 * belong to the given context.
1959 * This is called during context tear-down.
1962 st_destroy_program_variants(struct st_context
*st
)
1964 /* If shaders can be shared with other contexts, the last context will
1965 * call DeleteProgram on all shaders, releasing everything.
1967 if (st
->has_shareable_shaders
)
1970 /* ARB vert/frag program */
1971 _mesa_HashWalk(st
->ctx
->Shared
->Programs
,
1972 destroy_program_variants_cb
, st
);
1974 /* GLSL vert/frag/geom shaders */
1975 _mesa_HashWalk(st
->ctx
->Shared
->ShaderObjects
,
1976 destroy_shader_program_variants_cb
, st
);
1981 * For debugging, print/dump the current vertex program.
1984 st_print_current_vertex_program(void)
1986 GET_CURRENT_CONTEXT(ctx
);
1988 if (ctx
->VertexProgram
._Current
) {
1989 struct st_vertex_program
*stvp
=
1990 (struct st_vertex_program
*) ctx
->VertexProgram
._Current
;
1991 struct st_vp_variant
*stv
;
1993 debug_printf("Vertex program %u\n", stvp
->Base
.Id
);
1995 for (stv
= stvp
->variants
; stv
; stv
= stv
->next
) {
1996 debug_printf("variant %p\n", stv
);
1997 tgsi_dump(stv
->tgsi
.tokens
, 0);
2004 * Compile one shader variant.
2007 st_precompile_shader_variant(struct st_context
*st
,
2008 struct gl_program
*prog
)
2010 switch (prog
->Target
) {
2011 case GL_VERTEX_PROGRAM_ARB
: {
2012 struct st_vertex_program
*p
= (struct st_vertex_program
*)prog
;
2013 struct st_vp_variant_key key
;
2015 memset(&key
, 0, sizeof(key
));
2017 key
.st
= st
->has_shareable_shaders
? NULL
: st
;
2018 st_get_vp_variant(st
, p
, &key
);
2022 case GL_TESS_CONTROL_PROGRAM_NV
: {
2023 struct st_common_program
*p
= st_common_program(prog
);
2024 st_get_basic_variant(st
, PIPE_SHADER_TESS_CTRL
, p
);
2028 case GL_TESS_EVALUATION_PROGRAM_NV
: {
2029 struct st_common_program
*p
= st_common_program(prog
);
2030 st_get_basic_variant(st
, PIPE_SHADER_TESS_EVAL
, p
);
2034 case GL_GEOMETRY_PROGRAM_NV
: {
2035 struct st_common_program
*p
= st_common_program(prog
);
2036 st_get_basic_variant(st
, PIPE_SHADER_GEOMETRY
, p
);
2040 case GL_FRAGMENT_PROGRAM_ARB
: {
2041 struct st_fragment_program
*p
= (struct st_fragment_program
*)prog
;
2042 struct st_fp_variant_key key
;
2044 memset(&key
, 0, sizeof(key
));
2046 key
.st
= st
->has_shareable_shaders
? NULL
: st
;
2047 st_get_fp_variant(st
, p
, &key
);
2051 case GL_COMPUTE_PROGRAM_NV
: {
2052 struct st_compute_program
*p
= (struct st_compute_program
*)prog
;
2053 st_get_cp_variant(st
, &p
->tgsi
, &p
->variants
);