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 if (st
->has_shareable_shaders
|| vpv
->key
.st
== st
) {
234 cso_delete_vertex_shader(st
->cso_context
, vpv
->driver_shader
);
236 st_save_zombie_shader(vpv
->key
.st
, PIPE_SHADER_VERTEX
,
241 if (vpv
->draw_shader
)
242 draw_delete_vertex_shader( st
->draw
, vpv
->draw_shader
);
244 delete_ir(&vpv
->tgsi
);
252 * Clean out any old compilations:
255 st_release_vp_variants( struct st_context
*st
,
256 struct st_vertex_program
*stvp
)
258 struct st_vp_variant
*vpv
;
260 for (vpv
= stvp
->variants
; vpv
; ) {
261 struct st_vp_variant
*next
= vpv
->next
;
262 delete_vp_variant(st
, vpv
);
266 stvp
->variants
= NULL
;
268 delete_ir(&stvp
->tgsi
);
274 * Delete a fragment program variant. Note the caller must unlink
275 * the variant from the linked list.
278 delete_fp_variant(struct st_context
*st
, struct st_fp_variant
*fpv
)
280 if (fpv
->driver_shader
) {
281 if (st
->has_shareable_shaders
|| fpv
->key
.st
== st
) {
282 cso_delete_fragment_shader(st
->cso_context
, fpv
->driver_shader
);
284 st_save_zombie_shader(fpv
->key
.st
, PIPE_SHADER_FRAGMENT
,
294 * Free all variants of a fragment program.
297 st_release_fp_variants(struct st_context
*st
, struct st_fragment_program
*stfp
)
299 struct st_fp_variant
*fpv
;
301 for (fpv
= stfp
->variants
; fpv
; ) {
302 struct st_fp_variant
*next
= fpv
->next
;
303 delete_fp_variant(st
, fpv
);
307 stfp
->variants
= NULL
;
309 delete_ir(&stfp
->tgsi
);
314 * Delete a basic program variant. Note the caller must unlink
315 * the variant from the linked list.
318 delete_basic_variant(struct st_context
*st
, struct st_basic_variant
*v
,
321 if (v
->driver_shader
) {
322 if (st
->has_shareable_shaders
|| v
->key
.st
== st
) {
323 /* The shader's context matches the calling context, or we
327 case GL_TESS_CONTROL_PROGRAM_NV
:
328 cso_delete_tessctrl_shader(st
->cso_context
, v
->driver_shader
);
330 case GL_TESS_EVALUATION_PROGRAM_NV
:
331 cso_delete_tesseval_shader(st
->cso_context
, v
->driver_shader
);
333 case GL_GEOMETRY_PROGRAM_NV
:
334 cso_delete_geometry_shader(st
->cso_context
, v
->driver_shader
);
336 case GL_COMPUTE_PROGRAM_NV
:
337 cso_delete_compute_shader(st
->cso_context
, v
->driver_shader
);
340 unreachable("bad shader type in delete_basic_variant");
343 /* We can't delete a shader with a context different from the one
344 * that created it. Add it to the creating context's zombie list.
346 enum pipe_shader_type type
;
348 case GL_TESS_CONTROL_PROGRAM_NV
:
349 type
= PIPE_SHADER_TESS_CTRL
;
351 case GL_TESS_EVALUATION_PROGRAM_NV
:
352 type
= PIPE_SHADER_TESS_EVAL
;
354 case GL_GEOMETRY_PROGRAM_NV
:
355 type
= PIPE_SHADER_GEOMETRY
;
360 st_save_zombie_shader(v
->key
.st
, type
, v
->driver_shader
);
369 * Free all basic program variants.
372 st_release_basic_variants(struct st_context
*st
, GLenum target
,
373 struct st_basic_variant
**variants
,
374 struct pipe_shader_state
*tgsi
)
376 struct st_basic_variant
*v
;
378 for (v
= *variants
; v
; ) {
379 struct st_basic_variant
*next
= v
->next
;
380 delete_basic_variant(st
, v
, target
);
391 * Free all variants of a compute program.
394 st_release_cp_variants(struct st_context
*st
, struct st_compute_program
*stcp
)
396 struct st_basic_variant
**variants
= &stcp
->variants
;
397 struct st_basic_variant
*v
;
399 for (v
= *variants
; v
; ) {
400 struct st_basic_variant
*next
= v
->next
;
401 delete_basic_variant(st
, v
, stcp
->Base
.Target
);
407 if (stcp
->tgsi
.prog
) {
408 switch (stcp
->tgsi
.ir_type
) {
409 case PIPE_SHADER_IR_TGSI
:
410 ureg_free_tokens(stcp
->tgsi
.prog
);
411 stcp
->tgsi
.prog
= NULL
;
413 case PIPE_SHADER_IR_NIR
:
414 /* pipe driver took ownership of prog */
416 case PIPE_SHADER_IR_NATIVE
:
418 stcp
->tgsi
.prog
= NULL
;
425 * Translate ARB (asm) program to NIR
428 st_translate_prog_to_nir(struct st_context
*st
, struct gl_program
*prog
,
429 gl_shader_stage stage
)
431 enum pipe_shader_type p_stage
= stage
; /* valid for VS/FS */
432 const bool is_scalar
=
433 st
->pipe
->screen
->get_shader_param(st
->pipe
->screen
, p_stage
,
434 PIPE_SHADER_CAP_SCALAR_ISA
);
436 const struct gl_shader_compiler_options
*options
=
437 &st
->ctx
->Const
.ShaderCompilerOptions
[stage
];
439 /* Translate to NIR */
440 nir_shader
*nir
= prog_to_nir(prog
, options
->NirOptions
);
441 NIR_PASS_V(nir
, nir_lower_regs_to_ssa
); /* turn registers into SSA */
442 nir_validate_shader(nir
, "after st/ptn lower_regs_to_ssa");
444 NIR_PASS_V(nir
, st_nir_lower_wpos_ytransform
, prog
, st
->pipe
->screen
);
445 NIR_PASS_V(nir
, nir_lower_system_values
);
448 NIR_PASS_V(nir
, nir_opt_constant_folding
);
449 st_nir_opts(nir
, is_scalar
);
450 nir_validate_shader(nir
, "after st/ptn NIR opts");
456 * Translate a vertex program.
459 st_translate_vertex_program(struct st_context
*st
,
460 struct st_vertex_program
*stvp
)
462 struct ureg_program
*ureg
;
463 enum pipe_error error
;
464 unsigned num_outputs
= 0;
466 ubyte output_semantic_name
[VARYING_SLOT_MAX
] = {0};
467 ubyte output_semantic_index
[VARYING_SLOT_MAX
] = {0};
469 stvp
->num_inputs
= 0;
470 memset(stvp
->input_to_index
, ~0, sizeof(stvp
->input_to_index
));
472 if (stvp
->Base
.arb
.IsPositionInvariant
)
473 _mesa_insert_mvp_code(st
->ctx
, &stvp
->Base
);
476 * Determine number of inputs, the mappings between VERT_ATTRIB_x
477 * and TGSI generic input indexes, plus input attrib semantic info.
479 for (attr
= 0; attr
< VERT_ATTRIB_MAX
; attr
++) {
480 if ((stvp
->Base
.info
.inputs_read
& BITFIELD64_BIT(attr
)) != 0) {
481 stvp
->input_to_index
[attr
] = stvp
->num_inputs
;
482 stvp
->index_to_input
[stvp
->num_inputs
] = attr
;
484 if ((stvp
->Base
.DualSlotInputs
& BITFIELD64_BIT(attr
)) != 0) {
485 /* add placeholder for second part of a double attribute */
486 stvp
->index_to_input
[stvp
->num_inputs
] = ST_DOUBLE_ATTRIB_PLACEHOLDER
;
491 /* bit of a hack, presetup potentially unused edgeflag input */
492 stvp
->input_to_index
[VERT_ATTRIB_EDGEFLAG
] = stvp
->num_inputs
;
493 stvp
->index_to_input
[stvp
->num_inputs
] = VERT_ATTRIB_EDGEFLAG
;
495 /* Compute mapping of vertex program outputs to slots.
497 for (attr
= 0; attr
< VARYING_SLOT_MAX
; attr
++) {
498 if ((stvp
->Base
.info
.outputs_written
& BITFIELD64_BIT(attr
)) == 0) {
499 stvp
->result_to_output
[attr
] = ~0;
502 unsigned slot
= num_outputs
++;
504 stvp
->result_to_output
[attr
] = slot
;
506 unsigned semantic_name
, semantic_index
;
507 tgsi_get_gl_varying_semantic(attr
, st
->needs_texcoord_semantic
,
508 &semantic_name
, &semantic_index
);
509 output_semantic_name
[slot
] = semantic_name
;
510 output_semantic_index
[slot
] = semantic_index
;
513 /* similar hack to above, presetup potentially unused edgeflag output */
514 stvp
->result_to_output
[VARYING_SLOT_EDGE
] = num_outputs
;
515 output_semantic_name
[num_outputs
] = TGSI_SEMANTIC_EDGEFLAG
;
516 output_semantic_index
[num_outputs
] = 0;
519 if (!stvp
->glsl_to_tgsi
&& !stvp
->shader_program
) {
520 _mesa_remove_output_reads(&stvp
->Base
, PROGRAM_OUTPUT
);
522 /* This determines which states will be updated when the assembly
525 stvp
->affected_states
= ST_NEW_VS_STATE
|
527 ST_NEW_VERTEX_ARRAYS
;
529 if (stvp
->Base
.Parameters
->NumParameters
)
530 stvp
->affected_states
|= ST_NEW_VS_CONSTANTS
;
532 /* No samplers are allowed in ARB_vp. */
535 if (stvp
->shader_program
) {
536 st_translate_stream_output_info(stvp
->Base
.sh
.LinkedTransformFeedback
,
537 stvp
->result_to_output
,
538 &stvp
->tgsi
.stream_output
);
540 st_store_ir_in_disk_cache(st
, &stvp
->Base
, true);
544 ureg
= ureg_create_with_screen(PIPE_SHADER_VERTEX
, st
->pipe
->screen
);
548 if (stvp
->Base
.info
.clip_distance_array_size
)
549 ureg_property(ureg
, TGSI_PROPERTY_NUM_CLIPDIST_ENABLED
,
550 stvp
->Base
.info
.clip_distance_array_size
);
551 if (stvp
->Base
.info
.cull_distance_array_size
)
552 ureg_property(ureg
, TGSI_PROPERTY_NUM_CULLDIST_ENABLED
,
553 stvp
->Base
.info
.cull_distance_array_size
);
555 if (ST_DEBUG
& DEBUG_MESA
) {
556 _mesa_print_program(&stvp
->Base
);
557 _mesa_print_program_parameters(st
->ctx
, &stvp
->Base
);
561 if (stvp
->glsl_to_tgsi
) {
562 error
= st_translate_program(st
->ctx
,
569 stvp
->input_to_index
,
570 NULL
, /* inputSlotToAttr */
571 NULL
, /* input semantic name */
572 NULL
, /* input semantic index */
573 NULL
, /* interp mode */
576 stvp
->result_to_output
,
577 output_semantic_name
,
578 output_semantic_index
);
580 st_translate_stream_output_info(stvp
->Base
.sh
.LinkedTransformFeedback
,
581 stvp
->result_to_output
,
582 &stvp
->tgsi
.stream_output
);
584 free_glsl_to_tgsi_visitor(stvp
->glsl_to_tgsi
);
586 error
= st_translate_mesa_program(st
->ctx
,
592 stvp
->input_to_index
,
593 NULL
, /* input semantic name */
594 NULL
, /* input semantic index */
598 stvp
->result_to_output
,
599 output_semantic_name
,
600 output_semantic_index
);
603 debug_printf("%s: failed to translate Mesa program:\n", __func__
);
604 _mesa_print_program(&stvp
->Base
);
609 stvp
->tgsi
.tokens
= ureg_get_tokens(ureg
, &stvp
->num_tgsi_tokens
);
612 if (stvp
->glsl_to_tgsi
) {
613 stvp
->glsl_to_tgsi
= NULL
;
614 st_store_ir_in_disk_cache(st
, &stvp
->Base
, false);
617 bool use_nir
= PIPE_SHADER_IR_NIR
==
618 st
->pipe
->screen
->get_shader_param(st
->pipe
->screen
, PIPE_SHADER_VERTEX
,
619 PIPE_SHADER_CAP_PREFERRED_IR
);
623 st_translate_prog_to_nir(st
, &stvp
->Base
, MESA_SHADER_VERTEX
);
625 if (stvp
->tgsi
.ir
.nir
)
626 ralloc_free(stvp
->tgsi
.ir
.nir
);
627 stvp
->tgsi
.type
= PIPE_SHADER_IR_NIR
;
628 stvp
->tgsi
.ir
.nir
= nir
;
629 stvp
->Base
.nir
= nir
;
633 return stvp
->tgsi
.tokens
!= NULL
;
636 static struct st_vp_variant
*
637 st_create_vp_variant(struct st_context
*st
,
638 struct st_vertex_program
*stvp
,
639 const struct st_vp_variant_key
*key
)
641 struct st_vp_variant
*vpv
= CALLOC_STRUCT(st_vp_variant
);
642 struct pipe_context
*pipe
= st
->pipe
;
645 vpv
->tgsi
.stream_output
= stvp
->tgsi
.stream_output
;
646 vpv
->num_inputs
= stvp
->num_inputs
;
648 /* When generating a NIR program, we usually don't have TGSI tokens.
649 * However, we do create them for ARB_vertex_program / fixed-function VS
650 * programs which we may need to use with the draw module for legacy
651 * feedback/select emulation. If they exist, copy them.
653 if (stvp
->tgsi
.tokens
)
654 vpv
->tgsi
.tokens
= tgsi_dup_tokens(stvp
->tgsi
.tokens
);
656 if (stvp
->tgsi
.type
== PIPE_SHADER_IR_NIR
) {
657 vpv
->tgsi
.type
= PIPE_SHADER_IR_NIR
;
658 vpv
->tgsi
.ir
.nir
= nir_shader_clone(NULL
, stvp
->tgsi
.ir
.nir
);
659 if (key
->clamp_color
)
660 NIR_PASS_V(vpv
->tgsi
.ir
.nir
, nir_lower_clamp_color_outputs
);
661 if (key
->passthrough_edgeflags
) {
662 NIR_PASS_V(vpv
->tgsi
.ir
.nir
, nir_lower_passthrough_edgeflags
);
666 st_finalize_nir(st
, &stvp
->Base
, stvp
->shader_program
,
669 vpv
->driver_shader
= pipe
->create_vs_state(pipe
, &vpv
->tgsi
);
670 /* driver takes ownership of IR: */
671 vpv
->tgsi
.ir
.nir
= NULL
;
675 /* Emulate features. */
676 if (key
->clamp_color
|| key
->passthrough_edgeflags
) {
677 const struct tgsi_token
*tokens
;
679 (key
->clamp_color
? TGSI_EMU_CLAMP_COLOR_OUTPUTS
: 0) |
680 (key
->passthrough_edgeflags
? TGSI_EMU_PASSTHROUGH_EDGEFLAG
: 0);
682 tokens
= tgsi_emulate(vpv
->tgsi
.tokens
, flags
);
685 tgsi_free_tokens(vpv
->tgsi
.tokens
);
686 vpv
->tgsi
.tokens
= tokens
;
688 if (key
->passthrough_edgeflags
)
691 fprintf(stderr
, "mesa: cannot emulate deprecated features\n");
694 if (ST_DEBUG
& DEBUG_TGSI
) {
695 tgsi_dump(vpv
->tgsi
.tokens
, 0);
699 vpv
->driver_shader
= pipe
->create_vs_state(pipe
, &vpv
->tgsi
);
705 * Find/create a vertex program variant.
707 struct st_vp_variant
*
708 st_get_vp_variant(struct st_context
*st
,
709 struct st_vertex_program
*stvp
,
710 const struct st_vp_variant_key
*key
)
712 struct st_vp_variant
*vpv
;
714 /* Search for existing variant */
715 for (vpv
= stvp
->variants
; vpv
; vpv
= vpv
->next
) {
716 if (memcmp(&vpv
->key
, key
, sizeof(*key
)) == 0) {
723 vpv
= st_create_vp_variant(st
, stvp
, key
);
725 for (unsigned index
= 0; index
< vpv
->num_inputs
; ++index
) {
726 unsigned attr
= stvp
->index_to_input
[index
];
727 if (attr
== ST_DOUBLE_ATTRIB_PLACEHOLDER
)
729 vpv
->vert_attrib_mask
|= 1u << attr
;
732 /* insert into list */
733 vpv
->next
= stvp
->variants
;
734 stvp
->variants
= vpv
;
743 * Translate a Mesa fragment shader into a TGSI shader.
746 st_translate_fragment_program(struct st_context
*st
,
747 struct st_fragment_program
*stfp
)
749 /* We have already compiled to NIR so just return */
750 if (stfp
->shader_program
) {
751 st_store_ir_in_disk_cache(st
, &stfp
->Base
, true);
755 ubyte outputMapping
[2 * FRAG_RESULT_MAX
];
756 ubyte inputMapping
[VARYING_SLOT_MAX
];
757 ubyte inputSlotToAttr
[VARYING_SLOT_MAX
];
758 ubyte interpMode
[PIPE_MAX_SHADER_INPUTS
]; /* XXX size? */
760 GLbitfield64 inputsRead
;
761 struct ureg_program
*ureg
;
763 GLboolean write_all
= GL_FALSE
;
765 ubyte input_semantic_name
[PIPE_MAX_SHADER_INPUTS
];
766 ubyte input_semantic_index
[PIPE_MAX_SHADER_INPUTS
];
767 uint fs_num_inputs
= 0;
769 ubyte fs_output_semantic_name
[PIPE_MAX_SHADER_OUTPUTS
];
770 ubyte fs_output_semantic_index
[PIPE_MAX_SHADER_OUTPUTS
];
771 uint fs_num_outputs
= 0;
773 memset(inputSlotToAttr
, ~0, sizeof(inputSlotToAttr
));
775 /* Non-GLSL programs: */
776 if (!stfp
->glsl_to_tgsi
&& !stfp
->shader_program
) {
777 _mesa_remove_output_reads(&stfp
->Base
, PROGRAM_OUTPUT
);
778 if (st
->ctx
->Const
.GLSLFragCoordIsSysVal
)
779 _mesa_program_fragment_position_to_sysval(&stfp
->Base
);
781 /* This determines which states will be updated when the assembly
784 * fragment.position and glDrawPixels always use constants.
786 stfp
->affected_states
= ST_NEW_FS_STATE
|
787 ST_NEW_SAMPLE_SHADING
|
791 /* Just set them for ATI_fs unconditionally. */
792 stfp
->affected_states
|= ST_NEW_FS_SAMPLER_VIEWS
|
796 if (stfp
->Base
.SamplersUsed
)
797 stfp
->affected_states
|= ST_NEW_FS_SAMPLER_VIEWS
|
803 bool use_nir
= PIPE_SHADER_IR_NIR
==
804 st
->pipe
->screen
->get_shader_param(st
->pipe
->screen
,
805 PIPE_SHADER_FRAGMENT
,
806 PIPE_SHADER_CAP_PREFERRED_IR
);
808 if (use_nir
&& !stfp
->ati_fs
) {
810 st_translate_prog_to_nir(st
, &stfp
->Base
, MESA_SHADER_FRAGMENT
);
812 if (stfp
->tgsi
.ir
.nir
)
813 ralloc_free(stfp
->tgsi
.ir
.nir
);
814 stfp
->tgsi
.type
= PIPE_SHADER_IR_NIR
;
815 stfp
->tgsi
.ir
.nir
= nir
;
816 stfp
->Base
.nir
= nir
;
821 * Convert Mesa program inputs to TGSI input register semantics.
823 inputsRead
= stfp
->Base
.info
.inputs_read
;
824 for (attr
= 0; attr
< VARYING_SLOT_MAX
; attr
++) {
825 if ((inputsRead
& BITFIELD64_BIT(attr
)) != 0) {
826 const GLuint slot
= fs_num_inputs
++;
828 inputMapping
[attr
] = slot
;
829 inputSlotToAttr
[slot
] = attr
;
832 case VARYING_SLOT_POS
:
833 input_semantic_name
[slot
] = TGSI_SEMANTIC_POSITION
;
834 input_semantic_index
[slot
] = 0;
835 interpMode
[slot
] = TGSI_INTERPOLATE_LINEAR
;
837 case VARYING_SLOT_COL0
:
838 input_semantic_name
[slot
] = TGSI_SEMANTIC_COLOR
;
839 input_semantic_index
[slot
] = 0;
840 interpMode
[slot
] = stfp
->glsl_to_tgsi
?
841 TGSI_INTERPOLATE_COUNT
: TGSI_INTERPOLATE_COLOR
;
843 case VARYING_SLOT_COL1
:
844 input_semantic_name
[slot
] = TGSI_SEMANTIC_COLOR
;
845 input_semantic_index
[slot
] = 1;
846 interpMode
[slot
] = stfp
->glsl_to_tgsi
?
847 TGSI_INTERPOLATE_COUNT
: TGSI_INTERPOLATE_COLOR
;
849 case VARYING_SLOT_FOGC
:
850 input_semantic_name
[slot
] = TGSI_SEMANTIC_FOG
;
851 input_semantic_index
[slot
] = 0;
852 interpMode
[slot
] = TGSI_INTERPOLATE_PERSPECTIVE
;
854 case VARYING_SLOT_FACE
:
855 input_semantic_name
[slot
] = TGSI_SEMANTIC_FACE
;
856 input_semantic_index
[slot
] = 0;
857 interpMode
[slot
] = TGSI_INTERPOLATE_CONSTANT
;
859 case VARYING_SLOT_PRIMITIVE_ID
:
860 input_semantic_name
[slot
] = TGSI_SEMANTIC_PRIMID
;
861 input_semantic_index
[slot
] = 0;
862 interpMode
[slot
] = TGSI_INTERPOLATE_CONSTANT
;
864 case VARYING_SLOT_LAYER
:
865 input_semantic_name
[slot
] = TGSI_SEMANTIC_LAYER
;
866 input_semantic_index
[slot
] = 0;
867 interpMode
[slot
] = TGSI_INTERPOLATE_CONSTANT
;
869 case VARYING_SLOT_VIEWPORT
:
870 input_semantic_name
[slot
] = TGSI_SEMANTIC_VIEWPORT_INDEX
;
871 input_semantic_index
[slot
] = 0;
872 interpMode
[slot
] = TGSI_INTERPOLATE_CONSTANT
;
874 case VARYING_SLOT_CLIP_DIST0
:
875 input_semantic_name
[slot
] = TGSI_SEMANTIC_CLIPDIST
;
876 input_semantic_index
[slot
] = 0;
877 interpMode
[slot
] = TGSI_INTERPOLATE_PERSPECTIVE
;
879 case VARYING_SLOT_CLIP_DIST1
:
880 input_semantic_name
[slot
] = TGSI_SEMANTIC_CLIPDIST
;
881 input_semantic_index
[slot
] = 1;
882 interpMode
[slot
] = TGSI_INTERPOLATE_PERSPECTIVE
;
884 case VARYING_SLOT_CULL_DIST0
:
885 case VARYING_SLOT_CULL_DIST1
:
886 /* these should have been lowered by GLSL */
889 /* In most cases, there is nothing special about these
890 * inputs, so adopt a convention to use the generic
891 * semantic name and the mesa VARYING_SLOT_ number as the
894 * All that is required is that the vertex shader labels
895 * its own outputs similarly, and that the vertex shader
896 * generates at least every output required by the
897 * fragment shader plus fixed-function hardware (such as
900 * However, some drivers may need us to identify the PNTC and TEXi
901 * varyings if, for example, their capability to replace them with
902 * sprite coordinates is limited.
904 case VARYING_SLOT_PNTC
:
905 if (st
->needs_texcoord_semantic
) {
906 input_semantic_name
[slot
] = TGSI_SEMANTIC_PCOORD
;
907 input_semantic_index
[slot
] = 0;
908 interpMode
[slot
] = TGSI_INTERPOLATE_LINEAR
;
912 case VARYING_SLOT_TEX0
:
913 case VARYING_SLOT_TEX1
:
914 case VARYING_SLOT_TEX2
:
915 case VARYING_SLOT_TEX3
:
916 case VARYING_SLOT_TEX4
:
917 case VARYING_SLOT_TEX5
:
918 case VARYING_SLOT_TEX6
:
919 case VARYING_SLOT_TEX7
:
920 if (st
->needs_texcoord_semantic
) {
921 input_semantic_name
[slot
] = TGSI_SEMANTIC_TEXCOORD
;
922 input_semantic_index
[slot
] = attr
- VARYING_SLOT_TEX0
;
923 interpMode
[slot
] = stfp
->glsl_to_tgsi
?
924 TGSI_INTERPOLATE_COUNT
: TGSI_INTERPOLATE_PERSPECTIVE
;
928 case VARYING_SLOT_VAR0
:
930 /* Semantic indices should be zero-based because drivers may choose
931 * to assign a fixed slot determined by that index.
932 * This is useful because ARB_separate_shader_objects uses location
933 * qualifiers for linkage, and if the semantic index corresponds to
934 * these locations, linkage passes in the driver become unecessary.
936 * If needs_texcoord_semantic is true, no semantic indices will be
937 * consumed for the TEXi varyings, and we can base the locations of
938 * the user varyings on VAR0. Otherwise, we use TEX0 as base index.
940 assert(attr
>= VARYING_SLOT_VAR0
|| attr
== VARYING_SLOT_PNTC
||
941 (attr
>= VARYING_SLOT_TEX0
&& attr
<= VARYING_SLOT_TEX7
));
942 input_semantic_name
[slot
] = TGSI_SEMANTIC_GENERIC
;
943 input_semantic_index
[slot
] = st_get_generic_varying_index(st
, attr
);
944 if (attr
== VARYING_SLOT_PNTC
)
945 interpMode
[slot
] = TGSI_INTERPOLATE_LINEAR
;
947 interpMode
[slot
] = stfp
->glsl_to_tgsi
?
948 TGSI_INTERPOLATE_COUNT
: TGSI_INTERPOLATE_PERSPECTIVE
;
954 inputMapping
[attr
] = -1;
959 * Semantics and mapping for outputs
961 GLbitfield64 outputsWritten
= stfp
->Base
.info
.outputs_written
;
963 /* if z is written, emit that first */
964 if (outputsWritten
& BITFIELD64_BIT(FRAG_RESULT_DEPTH
)) {
965 fs_output_semantic_name
[fs_num_outputs
] = TGSI_SEMANTIC_POSITION
;
966 fs_output_semantic_index
[fs_num_outputs
] = 0;
967 outputMapping
[FRAG_RESULT_DEPTH
] = fs_num_outputs
;
969 outputsWritten
&= ~(1 << FRAG_RESULT_DEPTH
);
972 if (outputsWritten
& BITFIELD64_BIT(FRAG_RESULT_STENCIL
)) {
973 fs_output_semantic_name
[fs_num_outputs
] = TGSI_SEMANTIC_STENCIL
;
974 fs_output_semantic_index
[fs_num_outputs
] = 0;
975 outputMapping
[FRAG_RESULT_STENCIL
] = fs_num_outputs
;
977 outputsWritten
&= ~(1 << FRAG_RESULT_STENCIL
);
980 if (outputsWritten
& BITFIELD64_BIT(FRAG_RESULT_SAMPLE_MASK
)) {
981 fs_output_semantic_name
[fs_num_outputs
] = TGSI_SEMANTIC_SAMPLEMASK
;
982 fs_output_semantic_index
[fs_num_outputs
] = 0;
983 outputMapping
[FRAG_RESULT_SAMPLE_MASK
] = fs_num_outputs
;
985 outputsWritten
&= ~(1 << FRAG_RESULT_SAMPLE_MASK
);
988 /* handle remaining outputs (color) */
989 for (attr
= 0; attr
< ARRAY_SIZE(outputMapping
); attr
++) {
990 const GLbitfield64 written
= attr
< FRAG_RESULT_MAX
? outputsWritten
:
991 stfp
->Base
.SecondaryOutputsWritten
;
992 const unsigned loc
= attr
% FRAG_RESULT_MAX
;
994 if (written
& BITFIELD64_BIT(loc
)) {
996 case FRAG_RESULT_DEPTH
:
997 case FRAG_RESULT_STENCIL
:
998 case FRAG_RESULT_SAMPLE_MASK
:
1002 case FRAG_RESULT_COLOR
:
1003 write_all
= GL_TRUE
; /* fallthrough */
1006 assert(loc
== FRAG_RESULT_COLOR
||
1007 (FRAG_RESULT_DATA0
<= loc
&& loc
< FRAG_RESULT_MAX
));
1009 index
= (loc
== FRAG_RESULT_COLOR
) ? 0 : (loc
- FRAG_RESULT_DATA0
);
1011 if (attr
>= FRAG_RESULT_MAX
) {
1012 /* Secondary color for dual source blending. */
1017 fs_output_semantic_name
[fs_num_outputs
] = TGSI_SEMANTIC_COLOR
;
1018 fs_output_semantic_index
[fs_num_outputs
] = index
;
1019 outputMapping
[attr
] = fs_num_outputs
;
1028 ureg
= ureg_create_with_screen(PIPE_SHADER_FRAGMENT
, st
->pipe
->screen
);
1032 if (ST_DEBUG
& DEBUG_MESA
) {
1033 _mesa_print_program(&stfp
->Base
);
1034 _mesa_print_program_parameters(st
->ctx
, &stfp
->Base
);
1037 if (write_all
== GL_TRUE
)
1038 ureg_property(ureg
, TGSI_PROPERTY_FS_COLOR0_WRITES_ALL_CBUFS
, 1);
1040 if (stfp
->Base
.info
.fs
.depth_layout
!= FRAG_DEPTH_LAYOUT_NONE
) {
1041 switch (stfp
->Base
.info
.fs
.depth_layout
) {
1042 case FRAG_DEPTH_LAYOUT_ANY
:
1043 ureg_property(ureg
, TGSI_PROPERTY_FS_DEPTH_LAYOUT
,
1044 TGSI_FS_DEPTH_LAYOUT_ANY
);
1046 case FRAG_DEPTH_LAYOUT_GREATER
:
1047 ureg_property(ureg
, TGSI_PROPERTY_FS_DEPTH_LAYOUT
,
1048 TGSI_FS_DEPTH_LAYOUT_GREATER
);
1050 case FRAG_DEPTH_LAYOUT_LESS
:
1051 ureg_property(ureg
, TGSI_PROPERTY_FS_DEPTH_LAYOUT
,
1052 TGSI_FS_DEPTH_LAYOUT_LESS
);
1054 case FRAG_DEPTH_LAYOUT_UNCHANGED
:
1055 ureg_property(ureg
, TGSI_PROPERTY_FS_DEPTH_LAYOUT
,
1056 TGSI_FS_DEPTH_LAYOUT_UNCHANGED
);
1063 if (stfp
->glsl_to_tgsi
) {
1064 st_translate_program(st
->ctx
,
1065 PIPE_SHADER_FRAGMENT
,
1073 input_semantic_name
,
1074 input_semantic_index
,
1079 fs_output_semantic_name
,
1080 fs_output_semantic_index
);
1082 free_glsl_to_tgsi_visitor(stfp
->glsl_to_tgsi
);
1083 } else if (stfp
->ati_fs
)
1084 st_translate_atifs_program(ureg
,
1090 input_semantic_name
,
1091 input_semantic_index
,
1096 fs_output_semantic_name
,
1097 fs_output_semantic_index
);
1099 st_translate_mesa_program(st
->ctx
,
1100 PIPE_SHADER_FRAGMENT
,
1106 input_semantic_name
,
1107 input_semantic_index
,
1112 fs_output_semantic_name
,
1113 fs_output_semantic_index
);
1115 stfp
->tgsi
.tokens
= ureg_get_tokens(ureg
, &stfp
->num_tgsi_tokens
);
1118 if (stfp
->glsl_to_tgsi
) {
1119 stfp
->glsl_to_tgsi
= NULL
;
1120 st_store_ir_in_disk_cache(st
, &stfp
->Base
, false);
1123 return stfp
->tgsi
.tokens
!= NULL
;
1126 static struct st_fp_variant
*
1127 st_create_fp_variant(struct st_context
*st
,
1128 struct st_fragment_program
*stfp
,
1129 const struct st_fp_variant_key
*key
)
1131 struct pipe_context
*pipe
= st
->pipe
;
1132 struct st_fp_variant
*variant
= CALLOC_STRUCT(st_fp_variant
);
1133 struct pipe_shader_state tgsi
= {0};
1134 struct gl_program_parameter_list
*params
= stfp
->Base
.Parameters
;
1135 static const gl_state_index16 texcoord_state
[STATE_LENGTH
] =
1136 { STATE_INTERNAL
, STATE_CURRENT_ATTRIB
, VERT_ATTRIB_TEX0
};
1137 static const gl_state_index16 scale_state
[STATE_LENGTH
] =
1138 { STATE_INTERNAL
, STATE_PT_SCALE
};
1139 static const gl_state_index16 bias_state
[STATE_LENGTH
] =
1140 { STATE_INTERNAL
, STATE_PT_BIAS
};
1145 if (stfp
->tgsi
.type
== PIPE_SHADER_IR_NIR
) {
1146 tgsi
.type
= PIPE_SHADER_IR_NIR
;
1147 tgsi
.ir
.nir
= nir_shader_clone(NULL
, stfp
->tgsi
.ir
.nir
);
1149 if (key
->clamp_color
)
1150 NIR_PASS_V(tgsi
.ir
.nir
, nir_lower_clamp_color_outputs
);
1152 if (key
->persample_shading
) {
1153 nir_shader
*shader
= tgsi
.ir
.nir
;
1154 nir_foreach_variable(var
, &shader
->inputs
)
1155 var
->data
.sample
= true;
1158 assert(!(key
->bitmap
&& key
->drawpixels
));
1162 nir_lower_bitmap_options options
= {0};
1164 variant
->bitmap_sampler
= ffs(~stfp
->Base
.SamplersUsed
) - 1;
1165 options
.sampler
= variant
->bitmap_sampler
;
1166 options
.swizzle_xxxx
= (st
->bitmap
.tex_format
== PIPE_FORMAT_L8_UNORM
);
1168 NIR_PASS_V(tgsi
.ir
.nir
, nir_lower_bitmap
, &options
);
1171 /* glDrawPixels (color only) */
1172 if (key
->drawpixels
) {
1173 nir_lower_drawpixels_options options
= {{0}};
1174 unsigned samplers_used
= stfp
->Base
.SamplersUsed
;
1176 /* Find the first unused slot. */
1177 variant
->drawpix_sampler
= ffs(~samplers_used
) - 1;
1178 options
.drawpix_sampler
= variant
->drawpix_sampler
;
1179 samplers_used
|= (1 << variant
->drawpix_sampler
);
1181 options
.pixel_maps
= key
->pixelMaps
;
1182 if (key
->pixelMaps
) {
1183 variant
->pixelmap_sampler
= ffs(~samplers_used
) - 1;
1184 options
.pixelmap_sampler
= variant
->pixelmap_sampler
;
1187 options
.scale_and_bias
= key
->scaleAndBias
;
1188 if (key
->scaleAndBias
) {
1189 _mesa_add_state_reference(params
, scale_state
);
1190 memcpy(options
.scale_state_tokens
, scale_state
,
1191 sizeof(options
.scale_state_tokens
));
1192 _mesa_add_state_reference(params
, bias_state
);
1193 memcpy(options
.bias_state_tokens
, bias_state
,
1194 sizeof(options
.bias_state_tokens
));
1197 _mesa_add_state_reference(params
, texcoord_state
);
1198 memcpy(options
.texcoord_state_tokens
, texcoord_state
,
1199 sizeof(options
.texcoord_state_tokens
));
1201 NIR_PASS_V(tgsi
.ir
.nir
, nir_lower_drawpixels
, &options
);
1204 if (unlikely(key
->external
.lower_nv12
|| key
->external
.lower_iyuv
)) {
1205 nir_lower_tex_options options
= {0};
1206 options
.lower_y_uv_external
= key
->external
.lower_nv12
;
1207 options
.lower_y_u_v_external
= key
->external
.lower_iyuv
;
1208 NIR_PASS_V(tgsi
.ir
.nir
, nir_lower_tex
, &options
);
1211 st_finalize_nir(st
, &stfp
->Base
, stfp
->shader_program
, tgsi
.ir
.nir
);
1213 if (unlikely(key
->external
.lower_nv12
|| key
->external
.lower_iyuv
)) {
1214 /* This pass needs to happen *after* nir_lower_sampler */
1215 NIR_PASS_V(tgsi
.ir
.nir
, st_nir_lower_tex_src_plane
,
1216 ~stfp
->Base
.SamplersUsed
,
1217 key
->external
.lower_nv12
,
1218 key
->external
.lower_iyuv
);
1221 /* Some of the lowering above may have introduced new varyings */
1222 nir_shader_gather_info(tgsi
.ir
.nir
,
1223 nir_shader_get_entrypoint(tgsi
.ir
.nir
));
1225 variant
->driver_shader
= pipe
->create_fs_state(pipe
, &tgsi
);
1226 variant
->key
= *key
;
1231 tgsi
.tokens
= stfp
->tgsi
.tokens
;
1233 assert(!(key
->bitmap
&& key
->drawpixels
));
1235 /* Fix texture targets and add fog for ATI_fs */
1237 const struct tgsi_token
*tokens
= st_fixup_atifs(tgsi
.tokens
, key
);
1240 tgsi
.tokens
= tokens
;
1242 fprintf(stderr
, "mesa: cannot post-process ATI_fs\n");
1245 /* Emulate features. */
1246 if (key
->clamp_color
|| key
->persample_shading
) {
1247 const struct tgsi_token
*tokens
;
1249 (key
->clamp_color
? TGSI_EMU_CLAMP_COLOR_OUTPUTS
: 0) |
1250 (key
->persample_shading
? TGSI_EMU_FORCE_PERSAMPLE_INTERP
: 0);
1252 tokens
= tgsi_emulate(tgsi
.tokens
, flags
);
1255 if (tgsi
.tokens
!= stfp
->tgsi
.tokens
)
1256 tgsi_free_tokens(tgsi
.tokens
);
1257 tgsi
.tokens
= tokens
;
1259 fprintf(stderr
, "mesa: cannot emulate deprecated features\n");
1264 const struct tgsi_token
*tokens
;
1266 variant
->bitmap_sampler
= ffs(~stfp
->Base
.SamplersUsed
) - 1;
1268 tokens
= st_get_bitmap_shader(tgsi
.tokens
,
1269 st
->internal_target
,
1270 variant
->bitmap_sampler
,
1271 st
->needs_texcoord_semantic
,
1272 st
->bitmap
.tex_format
==
1273 PIPE_FORMAT_L8_UNORM
);
1276 if (tgsi
.tokens
!= stfp
->tgsi
.tokens
)
1277 tgsi_free_tokens(tgsi
.tokens
);
1278 tgsi
.tokens
= tokens
;
1280 fprintf(stderr
, "mesa: cannot create a shader for glBitmap\n");
1283 /* glDrawPixels (color only) */
1284 if (key
->drawpixels
) {
1285 const struct tgsi_token
*tokens
;
1286 unsigned scale_const
= 0, bias_const
= 0, texcoord_const
= 0;
1288 /* Find the first unused slot. */
1289 variant
->drawpix_sampler
= ffs(~stfp
->Base
.SamplersUsed
) - 1;
1291 if (key
->pixelMaps
) {
1292 unsigned samplers_used
= stfp
->Base
.SamplersUsed
|
1293 (1 << variant
->drawpix_sampler
);
1295 variant
->pixelmap_sampler
= ffs(~samplers_used
) - 1;
1298 if (key
->scaleAndBias
) {
1299 scale_const
= _mesa_add_state_reference(params
, scale_state
);
1300 bias_const
= _mesa_add_state_reference(params
, bias_state
);
1303 texcoord_const
= _mesa_add_state_reference(params
, texcoord_state
);
1305 tokens
= st_get_drawpix_shader(tgsi
.tokens
,
1306 st
->needs_texcoord_semantic
,
1307 key
->scaleAndBias
, scale_const
,
1308 bias_const
, key
->pixelMaps
,
1309 variant
->drawpix_sampler
,
1310 variant
->pixelmap_sampler
,
1311 texcoord_const
, st
->internal_target
);
1314 if (tgsi
.tokens
!= stfp
->tgsi
.tokens
)
1315 tgsi_free_tokens(tgsi
.tokens
);
1316 tgsi
.tokens
= tokens
;
1318 fprintf(stderr
, "mesa: cannot create a shader for glDrawPixels\n");
1321 if (unlikely(key
->external
.lower_nv12
|| key
->external
.lower_iyuv
)) {
1322 const struct tgsi_token
*tokens
;
1324 /* samplers inserted would conflict, but this should be unpossible: */
1325 assert(!(key
->bitmap
|| key
->drawpixels
));
1327 tokens
= st_tgsi_lower_yuv(tgsi
.tokens
,
1328 ~stfp
->Base
.SamplersUsed
,
1329 key
->external
.lower_nv12
,
1330 key
->external
.lower_iyuv
);
1332 if (tgsi
.tokens
!= stfp
->tgsi
.tokens
)
1333 tgsi_free_tokens(tgsi
.tokens
);
1334 tgsi
.tokens
= tokens
;
1336 fprintf(stderr
, "mesa: cannot create a shader for samplerExternalOES\n");
1340 if (ST_DEBUG
& DEBUG_TGSI
) {
1341 tgsi_dump(tgsi
.tokens
, 0);
1345 /* fill in variant */
1346 variant
->driver_shader
= pipe
->create_fs_state(pipe
, &tgsi
);
1347 variant
->key
= *key
;
1349 if (tgsi
.tokens
!= stfp
->tgsi
.tokens
)
1350 tgsi_free_tokens(tgsi
.tokens
);
1355 * Translate fragment program if needed.
1357 struct st_fp_variant
*
1358 st_get_fp_variant(struct st_context
*st
,
1359 struct st_fragment_program
*stfp
,
1360 const struct st_fp_variant_key
*key
)
1362 struct st_fp_variant
*fpv
;
1364 /* Search for existing variant */
1365 for (fpv
= stfp
->variants
; fpv
; fpv
= fpv
->next
) {
1366 if (memcmp(&fpv
->key
, key
, sizeof(*key
)) == 0) {
1373 fpv
= st_create_fp_variant(st
, stfp
, key
);
1375 if (key
->bitmap
|| key
->drawpixels
) {
1376 /* Regular variants should always come before the
1377 * bitmap & drawpixels variants, (unless there
1378 * are no regular variants) so that
1379 * st_update_fp can take a fast path when
1380 * shader_has_one_variant is set.
1382 if (!stfp
->variants
) {
1383 stfp
->variants
= fpv
;
1385 /* insert into list after the first one */
1386 fpv
->next
= stfp
->variants
->next
;
1387 stfp
->variants
->next
= fpv
;
1390 /* insert into list */
1391 fpv
->next
= stfp
->variants
;
1392 stfp
->variants
= fpv
;
1402 * Translate a program. This is common code for geometry and tessellation
1406 st_translate_program_common(struct st_context
*st
,
1407 struct gl_program
*prog
,
1408 struct glsl_to_tgsi_visitor
*glsl_to_tgsi
,
1409 struct ureg_program
*ureg
,
1410 unsigned tgsi_processor
,
1411 struct pipe_shader_state
*out_state
)
1413 ubyte inputSlotToAttr
[VARYING_SLOT_TESS_MAX
];
1414 ubyte inputMapping
[VARYING_SLOT_TESS_MAX
];
1415 ubyte outputMapping
[VARYING_SLOT_TESS_MAX
];
1418 ubyte input_semantic_name
[PIPE_MAX_SHADER_INPUTS
];
1419 ubyte input_semantic_index
[PIPE_MAX_SHADER_INPUTS
];
1420 uint num_inputs
= 0;
1422 ubyte output_semantic_name
[PIPE_MAX_SHADER_OUTPUTS
];
1423 ubyte output_semantic_index
[PIPE_MAX_SHADER_OUTPUTS
];
1424 uint num_outputs
= 0;
1428 memset(inputSlotToAttr
, 0, sizeof(inputSlotToAttr
));
1429 memset(inputMapping
, 0, sizeof(inputMapping
));
1430 memset(outputMapping
, 0, sizeof(outputMapping
));
1431 memset(out_state
, 0, sizeof(*out_state
));
1433 if (prog
->info
.clip_distance_array_size
)
1434 ureg_property(ureg
, TGSI_PROPERTY_NUM_CLIPDIST_ENABLED
,
1435 prog
->info
.clip_distance_array_size
);
1436 if (prog
->info
.cull_distance_array_size
)
1437 ureg_property(ureg
, TGSI_PROPERTY_NUM_CULLDIST_ENABLED
,
1438 prog
->info
.cull_distance_array_size
);
1441 * Convert Mesa program inputs to TGSI input register semantics.
1443 for (attr
= 0; attr
< VARYING_SLOT_MAX
; attr
++) {
1444 if ((prog
->info
.inputs_read
& BITFIELD64_BIT(attr
)) == 0)
1447 unsigned slot
= num_inputs
++;
1449 inputMapping
[attr
] = slot
;
1450 inputSlotToAttr
[slot
] = attr
;
1452 unsigned semantic_name
, semantic_index
;
1453 tgsi_get_gl_varying_semantic(attr
, st
->needs_texcoord_semantic
,
1454 &semantic_name
, &semantic_index
);
1455 input_semantic_name
[slot
] = semantic_name
;
1456 input_semantic_index
[slot
] = semantic_index
;
1459 /* Also add patch inputs. */
1460 for (attr
= 0; attr
< 32; attr
++) {
1461 if (prog
->info
.patch_inputs_read
& (1u << attr
)) {
1462 GLuint slot
= num_inputs
++;
1463 GLuint patch_attr
= VARYING_SLOT_PATCH0
+ attr
;
1465 inputMapping
[patch_attr
] = slot
;
1466 inputSlotToAttr
[slot
] = patch_attr
;
1467 input_semantic_name
[slot
] = TGSI_SEMANTIC_PATCH
;
1468 input_semantic_index
[slot
] = attr
;
1472 /* initialize output semantics to defaults */
1473 for (i
= 0; i
< PIPE_MAX_SHADER_OUTPUTS
; i
++) {
1474 output_semantic_name
[i
] = TGSI_SEMANTIC_GENERIC
;
1475 output_semantic_index
[i
] = 0;
1479 * Determine number of outputs, the (default) output register
1480 * mapping and the semantic information for each output.
1482 for (attr
= 0; attr
< VARYING_SLOT_MAX
; attr
++) {
1483 if (prog
->info
.outputs_written
& BITFIELD64_BIT(attr
)) {
1484 GLuint slot
= num_outputs
++;
1486 outputMapping
[attr
] = slot
;
1488 unsigned semantic_name
, semantic_index
;
1489 tgsi_get_gl_varying_semantic(attr
, st
->needs_texcoord_semantic
,
1490 &semantic_name
, &semantic_index
);
1491 output_semantic_name
[slot
] = semantic_name
;
1492 output_semantic_index
[slot
] = semantic_index
;
1496 /* Also add patch outputs. */
1497 for (attr
= 0; attr
< 32; attr
++) {
1498 if (prog
->info
.patch_outputs_written
& (1u << attr
)) {
1499 GLuint slot
= num_outputs
++;
1500 GLuint patch_attr
= VARYING_SLOT_PATCH0
+ attr
;
1502 outputMapping
[patch_attr
] = slot
;
1503 output_semantic_name
[slot
] = TGSI_SEMANTIC_PATCH
;
1504 output_semantic_index
[slot
] = attr
;
1508 st_translate_program(st
->ctx
,
1517 input_semantic_name
,
1518 input_semantic_index
,
1523 output_semantic_name
,
1524 output_semantic_index
);
1526 if (tgsi_processor
== PIPE_SHADER_COMPUTE
) {
1527 struct st_compute_program
*stcp
= (struct st_compute_program
*) prog
;
1528 out_state
->tokens
= ureg_get_tokens(ureg
, &stcp
->num_tgsi_tokens
);
1529 stcp
->tgsi
.prog
= out_state
->tokens
;
1531 struct st_common_program
*stcp
= (struct st_common_program
*) prog
;
1532 out_state
->tokens
= ureg_get_tokens(ureg
, &stcp
->num_tgsi_tokens
);
1536 st_translate_stream_output_info(prog
->sh
.LinkedTransformFeedback
,
1538 &out_state
->stream_output
);
1540 st_store_ir_in_disk_cache(st
, prog
, false);
1542 if ((ST_DEBUG
& DEBUG_TGSI
) && (ST_DEBUG
& DEBUG_MESA
)) {
1543 _mesa_print_program(prog
);
1547 if (ST_DEBUG
& DEBUG_TGSI
) {
1548 tgsi_dump(out_state
->tokens
, 0);
1554 * Update stream-output info for GS/TCS/TES. Normally this is done in
1555 * st_translate_program_common() but that is not called for glsl_to_nir
1559 st_translate_program_stream_output(struct gl_program
*prog
,
1560 struct pipe_stream_output_info
*stream_output
)
1562 if (!prog
->sh
.LinkedTransformFeedback
)
1565 ubyte outputMapping
[VARYING_SLOT_TESS_MAX
];
1567 uint num_outputs
= 0;
1569 memset(outputMapping
, 0, sizeof(outputMapping
));
1572 * Determine number of outputs, the (default) output register
1573 * mapping and the semantic information for each output.
1575 for (attr
= 0; attr
< VARYING_SLOT_MAX
; attr
++) {
1576 if (prog
->info
.outputs_written
& BITFIELD64_BIT(attr
)) {
1577 GLuint slot
= num_outputs
++;
1579 outputMapping
[attr
] = slot
;
1583 st_translate_stream_output_info(prog
->sh
.LinkedTransformFeedback
,
1589 * Translate a geometry program to create a new variant.
1592 st_translate_geometry_program(struct st_context
*st
,
1593 struct st_common_program
*stgp
)
1595 struct ureg_program
*ureg
;
1597 /* We have already compiled to NIR so just return */
1598 if (stgp
->shader_program
) {
1600 st_finalize_nir(st
, &stgp
->Base
, stgp
->shader_program
,
1602 st_translate_program_stream_output(&stgp
->Base
, &stgp
->tgsi
.stream_output
);
1603 st_store_ir_in_disk_cache(st
, &stgp
->Base
, true);
1607 ureg
= ureg_create_with_screen(PIPE_SHADER_GEOMETRY
, st
->pipe
->screen
);
1611 ureg_property(ureg
, TGSI_PROPERTY_GS_INPUT_PRIM
,
1612 stgp
->Base
.info
.gs
.input_primitive
);
1613 ureg_property(ureg
, TGSI_PROPERTY_GS_OUTPUT_PRIM
,
1614 stgp
->Base
.info
.gs
.output_primitive
);
1615 ureg_property(ureg
, TGSI_PROPERTY_GS_MAX_OUTPUT_VERTICES
,
1616 stgp
->Base
.info
.gs
.vertices_out
);
1617 ureg_property(ureg
, TGSI_PROPERTY_GS_INVOCATIONS
,
1618 stgp
->Base
.info
.gs
.invocations
);
1620 st_translate_program_common(st
, &stgp
->Base
, stgp
->glsl_to_tgsi
, ureg
,
1621 PIPE_SHADER_GEOMETRY
, &stgp
->tgsi
);
1623 free_glsl_to_tgsi_visitor(stgp
->glsl_to_tgsi
);
1624 stgp
->glsl_to_tgsi
= NULL
;
1630 * Get/create a basic program variant.
1632 struct st_basic_variant
*
1633 st_get_basic_variant(struct st_context
*st
,
1634 unsigned pipe_shader
,
1635 struct st_common_program
*prog
)
1637 struct pipe_context
*pipe
= st
->pipe
;
1638 struct st_basic_variant
*v
;
1639 struct st_basic_variant_key key
;
1640 struct pipe_shader_state tgsi
= {0};
1641 memset(&key
, 0, sizeof(key
));
1642 key
.st
= st
->has_shareable_shaders
? NULL
: st
;
1644 /* Search for existing variant */
1645 for (v
= prog
->variants
; v
; v
= v
->next
) {
1646 if (memcmp(&v
->key
, &key
, sizeof(key
)) == 0) {
1653 v
= CALLOC_STRUCT(st_basic_variant
);
1656 if (prog
->tgsi
.type
== PIPE_SHADER_IR_NIR
) {
1657 tgsi
.type
= PIPE_SHADER_IR_NIR
;
1658 tgsi
.ir
.nir
= nir_shader_clone(NULL
, prog
->tgsi
.ir
.nir
);
1659 tgsi
.stream_output
= prog
->tgsi
.stream_output
;
1662 /* fill in new variant */
1663 switch (pipe_shader
) {
1664 case PIPE_SHADER_TESS_CTRL
:
1665 v
->driver_shader
= pipe
->create_tcs_state(pipe
, &tgsi
);
1667 case PIPE_SHADER_TESS_EVAL
:
1668 v
->driver_shader
= pipe
->create_tes_state(pipe
, &tgsi
);
1670 case PIPE_SHADER_GEOMETRY
:
1671 v
->driver_shader
= pipe
->create_gs_state(pipe
, &tgsi
);
1674 assert(!"unhandled shader type");
1681 /* insert into list */
1682 v
->next
= prog
->variants
;
1692 * Translate a tessellation control program to create a new variant.
1695 st_translate_tessctrl_program(struct st_context
*st
,
1696 struct st_common_program
*sttcp
)
1698 struct ureg_program
*ureg
;
1700 /* We have already compiled to NIR so just return */
1701 if (sttcp
->shader_program
) {
1703 st_finalize_nir(st
, &sttcp
->Base
, sttcp
->shader_program
,
1704 sttcp
->tgsi
.ir
.nir
);
1705 st_store_ir_in_disk_cache(st
, &sttcp
->Base
, true);
1709 ureg
= ureg_create_with_screen(PIPE_SHADER_TESS_CTRL
, st
->pipe
->screen
);
1713 ureg_property(ureg
, TGSI_PROPERTY_TCS_VERTICES_OUT
,
1714 sttcp
->Base
.info
.tess
.tcs_vertices_out
);
1716 st_translate_program_common(st
, &sttcp
->Base
, sttcp
->glsl_to_tgsi
, ureg
,
1717 PIPE_SHADER_TESS_CTRL
, &sttcp
->tgsi
);
1719 free_glsl_to_tgsi_visitor(sttcp
->glsl_to_tgsi
);
1720 sttcp
->glsl_to_tgsi
= NULL
;
1726 * Translate a tessellation evaluation program to create a new variant.
1729 st_translate_tesseval_program(struct st_context
*st
,
1730 struct st_common_program
*sttep
)
1732 struct ureg_program
*ureg
;
1734 /* We have already compiled to NIR so just return */
1735 if (sttep
->shader_program
) {
1737 st_finalize_nir(st
, &sttep
->Base
, sttep
->shader_program
,
1738 sttep
->tgsi
.ir
.nir
);
1739 st_translate_program_stream_output(&sttep
->Base
, &sttep
->tgsi
.stream_output
);
1740 st_store_ir_in_disk_cache(st
, &sttep
->Base
, true);
1744 ureg
= ureg_create_with_screen(PIPE_SHADER_TESS_EVAL
, st
->pipe
->screen
);
1748 if (sttep
->Base
.info
.tess
.primitive_mode
== GL_ISOLINES
)
1749 ureg_property(ureg
, TGSI_PROPERTY_TES_PRIM_MODE
, GL_LINES
);
1751 ureg_property(ureg
, TGSI_PROPERTY_TES_PRIM_MODE
,
1752 sttep
->Base
.info
.tess
.primitive_mode
);
1754 STATIC_ASSERT((TESS_SPACING_EQUAL
+ 1) % 3 == PIPE_TESS_SPACING_EQUAL
);
1755 STATIC_ASSERT((TESS_SPACING_FRACTIONAL_ODD
+ 1) % 3 ==
1756 PIPE_TESS_SPACING_FRACTIONAL_ODD
);
1757 STATIC_ASSERT((TESS_SPACING_FRACTIONAL_EVEN
+ 1) % 3 ==
1758 PIPE_TESS_SPACING_FRACTIONAL_EVEN
);
1760 ureg_property(ureg
, TGSI_PROPERTY_TES_SPACING
,
1761 (sttep
->Base
.info
.tess
.spacing
+ 1) % 3);
1763 ureg_property(ureg
, TGSI_PROPERTY_TES_VERTEX_ORDER_CW
,
1764 !sttep
->Base
.info
.tess
.ccw
);
1765 ureg_property(ureg
, TGSI_PROPERTY_TES_POINT_MODE
,
1766 sttep
->Base
.info
.tess
.point_mode
);
1768 st_translate_program_common(st
, &sttep
->Base
, sttep
->glsl_to_tgsi
,
1769 ureg
, PIPE_SHADER_TESS_EVAL
, &sttep
->tgsi
);
1771 free_glsl_to_tgsi_visitor(sttep
->glsl_to_tgsi
);
1772 sttep
->glsl_to_tgsi
= NULL
;
1778 * Translate a compute program to create a new variant.
1781 st_translate_compute_program(struct st_context
*st
,
1782 struct st_compute_program
*stcp
)
1784 struct ureg_program
*ureg
;
1785 struct pipe_shader_state prog
;
1787 stcp
->tgsi
.req_local_mem
= stcp
->Base
.info
.cs
.shared_size
;
1789 if (stcp
->shader_program
) {
1790 /* no compute variants: */
1791 st_finalize_nir(st
, &stcp
->Base
, stcp
->shader_program
,
1792 (struct nir_shader
*) stcp
->tgsi
.prog
);
1793 st_store_ir_in_disk_cache(st
, &stcp
->Base
, true);
1797 ureg
= ureg_create_with_screen(PIPE_SHADER_COMPUTE
, st
->pipe
->screen
);
1801 st_translate_program_common(st
, &stcp
->Base
, stcp
->glsl_to_tgsi
, ureg
,
1802 PIPE_SHADER_COMPUTE
, &prog
);
1804 stcp
->tgsi
.ir_type
= PIPE_SHADER_IR_TGSI
;
1805 stcp
->tgsi
.req_private_mem
= 0;
1806 stcp
->tgsi
.req_input_mem
= 0;
1808 free_glsl_to_tgsi_visitor(stcp
->glsl_to_tgsi
);
1809 stcp
->glsl_to_tgsi
= NULL
;
1815 * Get/create compute program variant.
1817 struct st_basic_variant
*
1818 st_get_cp_variant(struct st_context
*st
,
1819 struct pipe_compute_state
*tgsi
,
1820 struct st_basic_variant
**variants
)
1822 struct pipe_context
*pipe
= st
->pipe
;
1823 struct st_basic_variant
*v
;
1824 struct st_basic_variant_key key
;
1826 /* use memset, not an initializer to be sure all memory is zeroed */
1827 memset(&key
, 0, sizeof(key
));
1829 key
.st
= st
->has_shareable_shaders
? NULL
: st
;
1831 /* Search for existing variant */
1832 for (v
= *variants
; v
; v
= v
->next
) {
1833 if (memcmp(&v
->key
, &key
, sizeof(key
)) == 0) {
1840 v
= CALLOC_STRUCT(st_basic_variant
);
1842 /* fill in new variant */
1843 struct pipe_compute_state cs
= *tgsi
;
1844 if (tgsi
->ir_type
== PIPE_SHADER_IR_NIR
)
1845 cs
.prog
= nir_shader_clone(NULL
, tgsi
->prog
);
1846 v
->driver_shader
= pipe
->create_compute_state(pipe
, &cs
);
1849 /* insert into list */
1850 v
->next
= *variants
;
1860 * Vert/Geom/Frag programs have per-context variants. Free all the
1861 * variants attached to the given program which match the given context.
1864 destroy_program_variants(struct st_context
*st
, struct gl_program
*target
)
1866 if (!target
|| target
== &_mesa_DummyProgram
)
1869 switch (target
->Target
) {
1870 case GL_VERTEX_PROGRAM_ARB
:
1872 struct st_vertex_program
*stvp
= (struct st_vertex_program
*) target
;
1873 struct st_vp_variant
*vpv
, **prevPtr
= &stvp
->variants
;
1875 for (vpv
= stvp
->variants
; vpv
; ) {
1876 struct st_vp_variant
*next
= vpv
->next
;
1877 if (vpv
->key
.st
== st
) {
1878 /* unlink from list */
1880 /* destroy this variant */
1881 delete_vp_variant(st
, vpv
);
1884 prevPtr
= &vpv
->next
;
1890 case GL_FRAGMENT_PROGRAM_ARB
:
1892 struct st_fragment_program
*stfp
=
1893 (struct st_fragment_program
*) target
;
1894 struct st_fp_variant
*fpv
, **prevPtr
= &stfp
->variants
;
1896 for (fpv
= stfp
->variants
; fpv
; ) {
1897 struct st_fp_variant
*next
= fpv
->next
;
1898 if (fpv
->key
.st
== st
) {
1899 /* unlink from list */
1901 /* destroy this variant */
1902 delete_fp_variant(st
, fpv
);
1905 prevPtr
= &fpv
->next
;
1911 case GL_GEOMETRY_PROGRAM_NV
:
1912 case GL_TESS_CONTROL_PROGRAM_NV
:
1913 case GL_TESS_EVALUATION_PROGRAM_NV
:
1914 case GL_COMPUTE_PROGRAM_NV
:
1916 struct st_common_program
*p
= st_common_program(target
);
1917 struct st_compute_program
*cp
= (struct st_compute_program
*)target
;
1918 struct st_basic_variant
**variants
=
1919 target
->Target
== GL_COMPUTE_PROGRAM_NV
? &cp
->variants
:
1921 struct st_basic_variant
*v
, **prevPtr
= variants
;
1923 for (v
= *variants
; v
; ) {
1924 struct st_basic_variant
*next
= v
->next
;
1925 if (v
->key
.st
== st
) {
1926 /* unlink from list */
1928 /* destroy this variant */
1929 delete_basic_variant(st
, v
, target
->Target
);
1939 _mesa_problem(NULL
, "Unexpected program target 0x%x in "
1940 "destroy_program_variants_cb()", target
->Target
);
1946 * Callback for _mesa_HashWalk. Free all the shader's program variants
1947 * which match the given context.
1950 destroy_shader_program_variants_cb(GLuint key
, void *data
, void *userData
)
1952 struct st_context
*st
= (struct st_context
*) userData
;
1953 struct gl_shader
*shader
= (struct gl_shader
*) data
;
1955 switch (shader
->Type
) {
1956 case GL_SHADER_PROGRAM_MESA
:
1958 struct gl_shader_program
*shProg
= (struct gl_shader_program
*) data
;
1961 for (i
= 0; i
< ARRAY_SIZE(shProg
->_LinkedShaders
); i
++) {
1962 if (shProg
->_LinkedShaders
[i
])
1963 destroy_program_variants(st
, shProg
->_LinkedShaders
[i
]->Program
);
1967 case GL_VERTEX_SHADER
:
1968 case GL_FRAGMENT_SHADER
:
1969 case GL_GEOMETRY_SHADER
:
1970 case GL_TESS_CONTROL_SHADER
:
1971 case GL_TESS_EVALUATION_SHADER
:
1972 case GL_COMPUTE_SHADER
:
1981 * Callback for _mesa_HashWalk. Free all the program variants which match
1982 * the given context.
1985 destroy_program_variants_cb(GLuint key
, void *data
, void *userData
)
1987 struct st_context
*st
= (struct st_context
*) userData
;
1988 struct gl_program
*program
= (struct gl_program
*) data
;
1989 destroy_program_variants(st
, program
);
1994 * Walk over all shaders and programs to delete any variants which
1995 * belong to the given context.
1996 * This is called during context tear-down.
1999 st_destroy_program_variants(struct st_context
*st
)
2001 /* If shaders can be shared with other contexts, the last context will
2002 * call DeleteProgram on all shaders, releasing everything.
2004 if (st
->has_shareable_shaders
)
2007 /* ARB vert/frag program */
2008 _mesa_HashWalk(st
->ctx
->Shared
->Programs
,
2009 destroy_program_variants_cb
, st
);
2011 /* GLSL vert/frag/geom shaders */
2012 _mesa_HashWalk(st
->ctx
->Shared
->ShaderObjects
,
2013 destroy_shader_program_variants_cb
, st
);
2018 * For debugging, print/dump the current vertex program.
2021 st_print_current_vertex_program(void)
2023 GET_CURRENT_CONTEXT(ctx
);
2025 if (ctx
->VertexProgram
._Current
) {
2026 struct st_vertex_program
*stvp
=
2027 (struct st_vertex_program
*) ctx
->VertexProgram
._Current
;
2028 struct st_vp_variant
*stv
;
2030 debug_printf("Vertex program %u\n", stvp
->Base
.Id
);
2032 for (stv
= stvp
->variants
; stv
; stv
= stv
->next
) {
2033 debug_printf("variant %p\n", stv
);
2034 tgsi_dump(stv
->tgsi
.tokens
, 0);
2041 * Compile one shader variant.
2044 st_precompile_shader_variant(struct st_context
*st
,
2045 struct gl_program
*prog
)
2047 switch (prog
->Target
) {
2048 case GL_VERTEX_PROGRAM_ARB
: {
2049 struct st_vertex_program
*p
= (struct st_vertex_program
*)prog
;
2050 struct st_vp_variant_key key
;
2052 memset(&key
, 0, sizeof(key
));
2054 key
.st
= st
->has_shareable_shaders
? NULL
: st
;
2055 st_get_vp_variant(st
, p
, &key
);
2059 case GL_TESS_CONTROL_PROGRAM_NV
: {
2060 struct st_common_program
*p
= st_common_program(prog
);
2061 st_get_basic_variant(st
, PIPE_SHADER_TESS_CTRL
, p
);
2065 case GL_TESS_EVALUATION_PROGRAM_NV
: {
2066 struct st_common_program
*p
= st_common_program(prog
);
2067 st_get_basic_variant(st
, PIPE_SHADER_TESS_EVAL
, p
);
2071 case GL_GEOMETRY_PROGRAM_NV
: {
2072 struct st_common_program
*p
= st_common_program(prog
);
2073 st_get_basic_variant(st
, PIPE_SHADER_GEOMETRY
, p
);
2077 case GL_FRAGMENT_PROGRAM_ARB
: {
2078 struct st_fragment_program
*p
= (struct st_fragment_program
*)prog
;
2079 struct st_fp_variant_key key
;
2081 memset(&key
, 0, sizeof(key
));
2083 key
.st
= st
->has_shareable_shaders
? NULL
: st
;
2084 st_get_fp_variant(st
, p
, &key
);
2088 case GL_COMPUTE_PROGRAM_NV
: {
2089 struct st_compute_program
*p
= (struct st_compute_program
*)prog
;
2090 st_get_cp_variant(st
, &p
->tgsi
, &p
->variants
);