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_depth_clamp.h"
59 #include "st_tgsi_lower_yuv.h"
60 #include "st_program.h"
61 #include "st_mesa_to_tgsi.h"
62 #include "st_atifs_to_tgsi.h"
64 #include "st_shader_cache.h"
65 #include "cso_cache/cso_context.h"
70 set_affected_state_flags(uint64_t *states
,
71 struct gl_program
*prog
,
72 uint64_t new_constants
,
73 uint64_t new_sampler_views
,
74 uint64_t new_samplers
,
80 if (prog
->Parameters
->NumParameters
)
81 *states
|= new_constants
;
83 if (prog
->info
.num_textures
)
84 *states
|= new_sampler_views
| new_samplers
;
86 if (prog
->info
.num_images
)
87 *states
|= new_images
;
89 if (prog
->info
.num_ubos
)
92 if (prog
->info
.num_ssbos
)
95 if (prog
->info
.num_abos
)
96 *states
|= new_atomics
;
100 * This determines which states will be updated when the shader is bound.
103 st_set_prog_affected_state_flags(struct gl_program
*prog
)
107 switch (prog
->info
.stage
) {
108 case MESA_SHADER_VERTEX
:
109 states
= &((struct st_vertex_program
*)prog
)->affected_states
;
111 *states
= ST_NEW_VS_STATE
|
113 ST_NEW_VERTEX_ARRAYS
;
115 set_affected_state_flags(states
, prog
,
117 ST_NEW_VS_SAMPLER_VIEWS
,
125 case MESA_SHADER_TESS_CTRL
:
126 states
= &(st_common_program(prog
))->affected_states
;
128 *states
= ST_NEW_TCS_STATE
;
130 set_affected_state_flags(states
, prog
,
131 ST_NEW_TCS_CONSTANTS
,
132 ST_NEW_TCS_SAMPLER_VIEWS
,
140 case MESA_SHADER_TESS_EVAL
:
141 states
= &(st_common_program(prog
))->affected_states
;
143 *states
= ST_NEW_TES_STATE
|
146 set_affected_state_flags(states
, prog
,
147 ST_NEW_TES_CONSTANTS
,
148 ST_NEW_TES_SAMPLER_VIEWS
,
156 case MESA_SHADER_GEOMETRY
:
157 states
= &(st_common_program(prog
))->affected_states
;
159 *states
= ST_NEW_GS_STATE
|
162 set_affected_state_flags(states
, prog
,
164 ST_NEW_GS_SAMPLER_VIEWS
,
172 case MESA_SHADER_FRAGMENT
:
173 states
= &((struct st_common_program
*)prog
)->affected_states
;
175 /* gl_FragCoord and glDrawPixels always use constants. */
176 *states
= ST_NEW_FS_STATE
|
177 ST_NEW_SAMPLE_SHADING
|
180 set_affected_state_flags(states
, prog
,
182 ST_NEW_FS_SAMPLER_VIEWS
,
190 case MESA_SHADER_COMPUTE
:
191 states
= &((struct st_common_program
*)prog
)->affected_states
;
193 *states
= ST_NEW_CS_STATE
;
195 set_affected_state_flags(states
, prog
,
197 ST_NEW_CS_SAMPLER_VIEWS
,
206 unreachable("unhandled shader stage");
211 delete_ir(struct pipe_shader_state
*ir
)
214 ureg_free_tokens(ir
->tokens
);
218 /* Note: Any setup of ->ir.nir that has had pipe->create_*_state called on
219 * it has resulted in the driver taking ownership of the NIR. Those
220 * callers should be NULLing out the nir field in any pipe_shader_state
221 * that might have this called in order to indicate that.
223 * GLSL IR and ARB programs will have set gl_program->nir to the same
224 * shader as ir->ir.nir, so it will be freed by _mesa_delete_program().
229 * Delete a vertex program variant. Note the caller must unlink
230 * the variant from the linked list.
233 delete_vp_variant(struct st_context
*st
, struct st_vp_variant
*vpv
)
235 if (vpv
->driver_shader
) {
236 if (st
->has_shareable_shaders
|| vpv
->key
.st
== st
) {
237 cso_delete_vertex_shader(st
->cso_context
, vpv
->driver_shader
);
239 st_save_zombie_shader(vpv
->key
.st
, PIPE_SHADER_VERTEX
,
244 if (vpv
->draw_shader
)
245 draw_delete_vertex_shader( st
->draw
, vpv
->draw_shader
);
248 ureg_free_tokens(vpv
->tokens
);
256 * Clean out any old compilations:
259 st_release_vp_variants( struct st_context
*st
,
260 struct st_vertex_program
*stvp
)
262 struct st_vp_variant
*vpv
;
264 for (vpv
= stvp
->variants
; vpv
; ) {
265 struct st_vp_variant
*next
= vpv
->next
;
266 delete_vp_variant(st
, vpv
);
270 stvp
->variants
= NULL
;
272 delete_ir(&stvp
->state
);
278 * Delete a fragment program variant. Note the caller must unlink
279 * the variant from the linked list.
282 delete_fp_variant(struct st_context
*st
, struct st_fp_variant
*fpv
)
284 if (fpv
->driver_shader
) {
285 if (st
->has_shareable_shaders
|| fpv
->key
.st
== st
) {
286 cso_delete_fragment_shader(st
->cso_context
, fpv
->driver_shader
);
288 st_save_zombie_shader(fpv
->key
.st
, PIPE_SHADER_FRAGMENT
,
298 * Free all variants of a fragment program.
301 st_release_fp_variants(struct st_context
*st
, struct st_common_program
*stfp
)
303 struct st_fp_variant
*fpv
;
305 for (fpv
= stfp
->fp_variants
; fpv
; ) {
306 struct st_fp_variant
*next
= fpv
->next
;
307 delete_fp_variant(st
, fpv
);
311 stfp
->fp_variants
= NULL
;
313 delete_ir(&stfp
->state
);
318 * Delete a basic program variant. Note the caller must unlink
319 * the variant from the linked list.
322 delete_basic_variant(struct st_context
*st
, struct st_common_variant
*v
,
325 if (v
->driver_shader
) {
326 if (st
->has_shareable_shaders
|| v
->key
.st
== st
) {
327 /* The shader's context matches the calling context, or we
331 case GL_TESS_CONTROL_PROGRAM_NV
:
332 cso_delete_tessctrl_shader(st
->cso_context
, v
->driver_shader
);
334 case GL_TESS_EVALUATION_PROGRAM_NV
:
335 cso_delete_tesseval_shader(st
->cso_context
, v
->driver_shader
);
337 case GL_GEOMETRY_PROGRAM_NV
:
338 cso_delete_geometry_shader(st
->cso_context
, v
->driver_shader
);
340 case GL_COMPUTE_PROGRAM_NV
:
341 cso_delete_compute_shader(st
->cso_context
, v
->driver_shader
);
344 unreachable("bad shader type in delete_basic_variant");
347 /* We can't delete a shader with a context different from the one
348 * that created it. Add it to the creating context's zombie list.
350 enum pipe_shader_type type
;
352 case GL_TESS_CONTROL_PROGRAM_NV
:
353 type
= PIPE_SHADER_TESS_CTRL
;
355 case GL_TESS_EVALUATION_PROGRAM_NV
:
356 type
= PIPE_SHADER_TESS_EVAL
;
358 case GL_GEOMETRY_PROGRAM_NV
:
359 type
= PIPE_SHADER_GEOMETRY
;
364 st_save_zombie_shader(v
->key
.st
, type
, v
->driver_shader
);
373 * Free all basic program variants.
376 st_release_common_variants(struct st_context
*st
, struct st_common_program
*p
)
378 struct st_common_variant
*v
;
380 for (v
= p
->variants
; v
; ) {
381 struct st_common_variant
*next
= v
->next
;
382 delete_basic_variant(st
, v
, p
->Base
.Target
);
387 delete_ir(&p
->state
);
391 st_finalize_nir_before_variants(struct nir_shader
*nir
)
393 NIR_PASS_V(nir
, nir_opt_access
);
395 NIR_PASS_V(nir
, nir_split_var_copies
);
396 NIR_PASS_V(nir
, nir_lower_var_copies
);
397 if (nir
->options
->lower_all_io_to_temps
||
398 nir
->options
->lower_all_io_to_elements
||
399 nir
->info
.stage
== MESA_SHADER_VERTEX
||
400 nir
->info
.stage
== MESA_SHADER_GEOMETRY
) {
401 NIR_PASS_V(nir
, nir_lower_io_arrays_to_elements_no_indirects
, false);
402 } else if (nir
->info
.stage
== MESA_SHADER_FRAGMENT
) {
403 NIR_PASS_V(nir
, nir_lower_io_arrays_to_elements_no_indirects
, true);
406 st_nir_assign_vs_in_locations(nir
);
410 * Translate ARB (asm) program to NIR
413 st_translate_prog_to_nir(struct st_context
*st
, struct gl_program
*prog
,
414 gl_shader_stage stage
)
416 struct pipe_screen
*screen
= st
->pipe
->screen
;
417 const struct gl_shader_compiler_options
*options
=
418 &st
->ctx
->Const
.ShaderCompilerOptions
[stage
];
420 /* Translate to NIR */
421 nir_shader
*nir
= prog_to_nir(prog
, options
->NirOptions
);
422 NIR_PASS_V(nir
, nir_lower_regs_to_ssa
); /* turn registers into SSA */
423 nir_validate_shader(nir
, "after st/ptn lower_regs_to_ssa");
425 NIR_PASS_V(nir
, st_nir_lower_wpos_ytransform
, prog
, screen
);
426 NIR_PASS_V(nir
, nir_lower_system_values
);
429 NIR_PASS_V(nir
, nir_opt_constant_folding
);
431 st_finalize_nir_before_variants(nir
);
433 if (st
->allow_st_finalize_nir_twice
)
434 st_finalize_nir(st
, prog
, NULL
, nir
, true);
436 nir_validate_shader(nir
, "after st/glsl finalize_nir");
442 st_prepare_vertex_program(struct st_vertex_program
*stvp
)
444 stvp
->num_inputs
= 0;
445 memset(stvp
->input_to_index
, ~0, sizeof(stvp
->input_to_index
));
446 memset(stvp
->result_to_output
, ~0, sizeof(stvp
->result_to_output
));
448 /* Determine number of inputs, the mappings between VERT_ATTRIB_x
449 * and TGSI generic input indexes, plus input attrib semantic info.
451 for (unsigned attr
= 0; attr
< VERT_ATTRIB_MAX
; attr
++) {
452 if ((stvp
->Base
.info
.inputs_read
& BITFIELD64_BIT(attr
)) != 0) {
453 stvp
->input_to_index
[attr
] = stvp
->num_inputs
;
454 stvp
->index_to_input
[stvp
->num_inputs
] = attr
;
457 if ((stvp
->Base
.DualSlotInputs
& BITFIELD64_BIT(attr
)) != 0) {
458 /* add placeholder for second part of a double attribute */
459 stvp
->index_to_input
[stvp
->num_inputs
] = ST_DOUBLE_ATTRIB_PLACEHOLDER
;
464 /* pre-setup potentially unused edgeflag input */
465 stvp
->input_to_index
[VERT_ATTRIB_EDGEFLAG
] = stvp
->num_inputs
;
466 stvp
->index_to_input
[stvp
->num_inputs
] = VERT_ATTRIB_EDGEFLAG
;
468 /* Compute mapping of vertex program outputs to slots. */
469 unsigned num_outputs
= 0;
470 for (unsigned attr
= 0; attr
< VARYING_SLOT_MAX
; attr
++) {
471 if (stvp
->Base
.info
.outputs_written
& BITFIELD64_BIT(attr
))
472 stvp
->result_to_output
[attr
] = num_outputs
++;
474 /* pre-setup potentially unused edgeflag output */
475 stvp
->result_to_output
[VARYING_SLOT_EDGE
] = num_outputs
;
479 st_translate_stream_output_info(struct gl_program
*prog
)
481 struct gl_transform_feedback_info
*info
= prog
->sh
.LinkedTransformFeedback
;
485 /* Determine the (default) output register mapping for each output. */
486 unsigned num_outputs
= 0;
487 ubyte output_mapping
[VARYING_SLOT_TESS_MAX
];
488 memset(output_mapping
, 0, sizeof(output_mapping
));
490 for (unsigned attr
= 0; attr
< VARYING_SLOT_MAX
; attr
++) {
491 if (prog
->info
.outputs_written
& BITFIELD64_BIT(attr
))
492 output_mapping
[attr
] = num_outputs
++;
495 /* Translate stream output info. */
496 struct pipe_stream_output_info
*so_info
= NULL
;
497 if (prog
->info
.stage
== MESA_SHADER_VERTEX
)
498 so_info
= &((struct st_vertex_program
*)prog
)->state
.stream_output
;
500 so_info
= &((struct st_common_program
*)prog
)->state
.stream_output
;
502 for (unsigned i
= 0; i
< info
->NumOutputs
; i
++) {
503 so_info
->output
[i
].register_index
=
504 output_mapping
[info
->Outputs
[i
].OutputRegister
];
505 so_info
->output
[i
].start_component
= info
->Outputs
[i
].ComponentOffset
;
506 so_info
->output
[i
].num_components
= info
->Outputs
[i
].NumComponents
;
507 so_info
->output
[i
].output_buffer
= info
->Outputs
[i
].OutputBuffer
;
508 so_info
->output
[i
].dst_offset
= info
->Outputs
[i
].DstOffset
;
509 so_info
->output
[i
].stream
= info
->Outputs
[i
].StreamId
;
512 for (unsigned i
= 0; i
< PIPE_MAX_SO_BUFFERS
; i
++) {
513 so_info
->stride
[i
] = info
->Buffers
[i
].Stride
;
515 so_info
->num_outputs
= info
->NumOutputs
;
519 * Translate a vertex program.
522 st_translate_vertex_program(struct st_context
*st
,
523 struct st_vertex_program
*stvp
)
525 struct ureg_program
*ureg
;
526 enum pipe_error error
;
527 unsigned num_outputs
= 0;
529 ubyte output_semantic_name
[VARYING_SLOT_MAX
] = {0};
530 ubyte output_semantic_index
[VARYING_SLOT_MAX
] = {0};
532 if (stvp
->Base
.arb
.IsPositionInvariant
)
533 _mesa_insert_mvp_code(st
->ctx
, &stvp
->Base
);
535 st_prepare_vertex_program(stvp
);
538 if (!stvp
->glsl_to_tgsi
) {
539 _mesa_remove_output_reads(&stvp
->Base
, PROGRAM_OUTPUT
);
541 /* This determines which states will be updated when the assembly
544 stvp
->affected_states
= ST_NEW_VS_STATE
|
546 ST_NEW_VERTEX_ARRAYS
;
548 if (stvp
->Base
.Parameters
->NumParameters
)
549 stvp
->affected_states
|= ST_NEW_VS_CONSTANTS
;
551 /* No samplers are allowed in ARB_vp. */
554 /* Get semantic names and indices. */
555 for (attr
= 0; attr
< VARYING_SLOT_MAX
; attr
++) {
556 if (stvp
->Base
.info
.outputs_written
& BITFIELD64_BIT(attr
)) {
557 unsigned slot
= num_outputs
++;
558 unsigned semantic_name
, semantic_index
;
559 tgsi_get_gl_varying_semantic(attr
, st
->needs_texcoord_semantic
,
560 &semantic_name
, &semantic_index
);
561 output_semantic_name
[slot
] = semantic_name
;
562 output_semantic_index
[slot
] = semantic_index
;
565 /* pre-setup potentially unused edgeflag output */
566 output_semantic_name
[num_outputs
] = TGSI_SEMANTIC_EDGEFLAG
;
567 output_semantic_index
[num_outputs
] = 0;
569 ureg
= ureg_create_with_screen(PIPE_SHADER_VERTEX
, st
->pipe
->screen
);
573 if (stvp
->Base
.info
.clip_distance_array_size
)
574 ureg_property(ureg
, TGSI_PROPERTY_NUM_CLIPDIST_ENABLED
,
575 stvp
->Base
.info
.clip_distance_array_size
);
576 if (stvp
->Base
.info
.cull_distance_array_size
)
577 ureg_property(ureg
, TGSI_PROPERTY_NUM_CULLDIST_ENABLED
,
578 stvp
->Base
.info
.cull_distance_array_size
);
580 if (ST_DEBUG
& DEBUG_MESA
) {
581 _mesa_print_program(&stvp
->Base
);
582 _mesa_print_program_parameters(st
->ctx
, &stvp
->Base
);
586 if (stvp
->glsl_to_tgsi
) {
587 error
= st_translate_program(st
->ctx
,
594 stvp
->input_to_index
,
595 NULL
, /* inputSlotToAttr */
596 NULL
, /* input semantic name */
597 NULL
, /* input semantic index */
598 NULL
, /* interp mode */
601 stvp
->result_to_output
,
602 output_semantic_name
,
603 output_semantic_index
);
605 st_translate_stream_output_info(&stvp
->Base
);
607 free_glsl_to_tgsi_visitor(stvp
->glsl_to_tgsi
);
609 error
= st_translate_mesa_program(st
->ctx
,
615 stvp
->input_to_index
,
616 NULL
, /* input semantic name */
617 NULL
, /* input semantic index */
621 stvp
->result_to_output
,
622 output_semantic_name
,
623 output_semantic_index
);
626 debug_printf("%s: failed to translate Mesa program:\n", __func__
);
627 _mesa_print_program(&stvp
->Base
);
632 stvp
->state
.tokens
= ureg_get_tokens(ureg
, NULL
);
635 if (stvp
->glsl_to_tgsi
) {
636 stvp
->glsl_to_tgsi
= NULL
;
637 st_store_ir_in_disk_cache(st
, &stvp
->Base
, false);
642 * This must be done after the translation to TGSI is done, because
643 * we'll pass the NIR shader to the driver and the TGSI version to
644 * the draw module for the select/feedback/rasterpos code.
646 if (st
->pipe
->screen
->get_shader_param(st
->pipe
->screen
,
648 PIPE_SHADER_CAP_PREFERRED_IR
)) {
649 assert(!stvp
->glsl_to_tgsi
);
652 st_translate_prog_to_nir(st
, &stvp
->Base
, MESA_SHADER_VERTEX
);
654 if (stvp
->state
.ir
.nir
)
655 ralloc_free(stvp
->state
.ir
.nir
);
656 stvp
->state
.type
= PIPE_SHADER_IR_NIR
;
657 stvp
->state
.ir
.nir
= nir
;
658 stvp
->Base
.nir
= nir
;
662 return stvp
->state
.tokens
!= NULL
;
665 static const gl_state_index16 depth_range_state
[STATE_LENGTH
] =
666 { STATE_DEPTH_RANGE
};
668 static struct st_vp_variant
*
669 st_create_vp_variant(struct st_context
*st
,
670 struct st_vertex_program
*stvp
,
671 const struct st_common_variant_key
*key
)
673 struct st_vp_variant
*vpv
= CALLOC_STRUCT(st_vp_variant
);
674 struct pipe_context
*pipe
= st
->pipe
;
675 struct pipe_screen
*screen
= pipe
->screen
;
676 struct pipe_shader_state state
= {0};
678 static const gl_state_index16 point_size_state
[STATE_LENGTH
] =
679 { STATE_INTERNAL
, STATE_POINT_SIZE_CLAMPED
, 0 };
680 struct gl_program_parameter_list
*params
= stvp
->Base
.Parameters
;
683 vpv
->num_inputs
= stvp
->num_inputs
;
685 state
.stream_output
= stvp
->state
.stream_output
;
687 if (stvp
->state
.type
== PIPE_SHADER_IR_NIR
) {
688 bool finalize
= false;
690 state
.type
= PIPE_SHADER_IR_NIR
;
691 state
.ir
.nir
= nir_shader_clone(NULL
, stvp
->state
.ir
.nir
);
692 if (key
->clamp_color
) {
693 NIR_PASS_V(state
.ir
.nir
, nir_lower_clamp_color_outputs
);
696 if (key
->passthrough_edgeflags
) {
697 NIR_PASS_V(state
.ir
.nir
, nir_lower_passthrough_edgeflags
);
702 if (key
->lower_point_size
) {
703 _mesa_add_state_reference(params
, point_size_state
);
704 NIR_PASS_V(state
.ir
.nir
, nir_lower_point_size_mov
,
709 if (key
->lower_ucp
) {
710 bool can_compact
= screen
->get_param(screen
,
711 PIPE_CAP_NIR_COMPACT_ARRAYS
);
713 bool use_eye
= st
->ctx
->_Shader
->CurrentProgram
[MESA_SHADER_VERTEX
] != NULL
;
714 gl_state_index16 clipplane_state
[MAX_CLIP_PLANES
][STATE_LENGTH
];
715 for (int i
= 0; i
< MAX_CLIP_PLANES
; ++i
) {
717 clipplane_state
[i
][0] = STATE_CLIPPLANE
;
718 clipplane_state
[i
][1] = i
;
720 clipplane_state
[i
][0] = STATE_INTERNAL
;
721 clipplane_state
[i
][1] = STATE_CLIP_INTERNAL
;
722 clipplane_state
[i
][2] = i
;
724 _mesa_add_state_reference(params
, clipplane_state
[i
]);
727 NIR_PASS_V(state
.ir
.nir
, nir_lower_clip_vs
, key
->lower_ucp
,
728 true, can_compact
, clipplane_state
);
729 NIR_PASS_V(state
.ir
.nir
, nir_lower_io_to_temporaries
,
730 nir_shader_get_entrypoint(state
.ir
.nir
), true, false);
731 NIR_PASS_V(state
.ir
.nir
, nir_lower_global_vars_to_local
);
735 if (finalize
|| !st
->allow_st_finalize_nir_twice
) {
736 st_finalize_nir(st
, &stvp
->Base
, stvp
->shader_program
, state
.ir
.nir
,
739 /* Some of the lowering above may have introduced new varyings */
740 nir_shader_gather_info(state
.ir
.nir
,
741 nir_shader_get_entrypoint(state
.ir
.nir
));
744 vpv
->driver_shader
= pipe
->create_vs_state(pipe
, &state
);
746 /* When generating a NIR program, we usually don't have TGSI tokens.
747 * However, we do create them for ARB_vertex_program / fixed-function VS
748 * programs which we may need to use with the draw module for legacy
749 * feedback/select emulation. If they exist, copy them.
751 * TODO: Lowering for shader variants is not applied to TGSI when
752 * generating a NIR shader.
754 if (stvp
->state
.tokens
)
755 vpv
->tokens
= tgsi_dup_tokens(stvp
->state
.tokens
);
760 state
.type
= PIPE_SHADER_IR_TGSI
;
761 state
.tokens
= tgsi_dup_tokens(stvp
->state
.tokens
);
763 /* Emulate features. */
764 if (key
->clamp_color
|| key
->passthrough_edgeflags
) {
765 const struct tgsi_token
*tokens
;
767 (key
->clamp_color
? TGSI_EMU_CLAMP_COLOR_OUTPUTS
: 0) |
768 (key
->passthrough_edgeflags
? TGSI_EMU_PASSTHROUGH_EDGEFLAG
: 0);
770 tokens
= tgsi_emulate(state
.tokens
, flags
);
773 tgsi_free_tokens(state
.tokens
);
774 state
.tokens
= tokens
;
776 if (key
->passthrough_edgeflags
)
779 fprintf(stderr
, "mesa: cannot emulate deprecated features\n");
782 if (key
->lower_depth_clamp
) {
783 unsigned depth_range_const
=
784 _mesa_add_state_reference(params
, depth_range_state
);
786 const struct tgsi_token
*tokens
;
787 tokens
= st_tgsi_lower_depth_clamp(state
.tokens
, depth_range_const
,
788 key
->clip_negative_one_to_one
);
789 if (tokens
!= state
.tokens
)
790 tgsi_free_tokens(state
.tokens
);
791 state
.tokens
= tokens
;
794 if (ST_DEBUG
& DEBUG_PRINT_IR
)
795 tgsi_dump(state
.tokens
, 0);
797 vpv
->driver_shader
= pipe
->create_vs_state(pipe
, &state
);
798 /* Save this for selection/feedback/rasterpos. */
799 vpv
->tokens
= state
.tokens
;
805 * Find/create a vertex program variant.
807 struct st_vp_variant
*
808 st_get_vp_variant(struct st_context
*st
,
809 struct st_vertex_program
*stvp
,
810 const struct st_common_variant_key
*key
)
812 struct st_vp_variant
*vpv
;
814 /* Search for existing variant */
815 for (vpv
= stvp
->variants
; vpv
; vpv
= vpv
->next
) {
816 if (memcmp(&vpv
->key
, key
, sizeof(*key
)) == 0) {
823 vpv
= st_create_vp_variant(st
, stvp
, key
);
825 for (unsigned index
= 0; index
< vpv
->num_inputs
; ++index
) {
826 unsigned attr
= stvp
->index_to_input
[index
];
827 if (attr
== ST_DOUBLE_ATTRIB_PLACEHOLDER
)
829 vpv
->vert_attrib_mask
|= 1u << attr
;
832 /* insert into list */
833 vpv
->next
= stvp
->variants
;
834 stvp
->variants
= vpv
;
843 * Translate a Mesa fragment shader into a TGSI shader.
846 st_translate_fragment_program(struct st_context
*st
,
847 struct st_common_program
*stfp
)
849 /* Non-GLSL programs: */
850 if (!stfp
->glsl_to_tgsi
) {
851 _mesa_remove_output_reads(&stfp
->Base
, PROGRAM_OUTPUT
);
852 if (st
->ctx
->Const
.GLSLFragCoordIsSysVal
)
853 _mesa_program_fragment_position_to_sysval(&stfp
->Base
);
855 /* This determines which states will be updated when the assembly
858 * fragment.position and glDrawPixels always use constants.
860 stfp
->affected_states
= ST_NEW_FS_STATE
|
861 ST_NEW_SAMPLE_SHADING
|
865 /* Just set them for ATI_fs unconditionally. */
866 stfp
->affected_states
|= ST_NEW_FS_SAMPLER_VIEWS
|
870 if (stfp
->Base
.SamplersUsed
)
871 stfp
->affected_states
|= ST_NEW_FS_SAMPLER_VIEWS
|
875 /* Translate to NIR. */
877 st
->pipe
->screen
->get_shader_param(st
->pipe
->screen
,
878 PIPE_SHADER_FRAGMENT
,
879 PIPE_SHADER_CAP_PREFERRED_IR
)) {
881 st_translate_prog_to_nir(st
, &stfp
->Base
, MESA_SHADER_FRAGMENT
);
883 if (stfp
->state
.ir
.nir
)
884 ralloc_free(stfp
->state
.ir
.nir
);
885 stfp
->state
.type
= PIPE_SHADER_IR_NIR
;
886 stfp
->state
.ir
.nir
= nir
;
887 stfp
->Base
.nir
= nir
;
892 ubyte outputMapping
[2 * FRAG_RESULT_MAX
];
893 ubyte inputMapping
[VARYING_SLOT_MAX
];
894 ubyte inputSlotToAttr
[VARYING_SLOT_MAX
];
895 ubyte interpMode
[PIPE_MAX_SHADER_INPUTS
]; /* XXX size? */
897 GLbitfield64 inputsRead
;
898 struct ureg_program
*ureg
;
900 GLboolean write_all
= GL_FALSE
;
902 ubyte input_semantic_name
[PIPE_MAX_SHADER_INPUTS
];
903 ubyte input_semantic_index
[PIPE_MAX_SHADER_INPUTS
];
904 uint fs_num_inputs
= 0;
906 ubyte fs_output_semantic_name
[PIPE_MAX_SHADER_OUTPUTS
];
907 ubyte fs_output_semantic_index
[PIPE_MAX_SHADER_OUTPUTS
];
908 uint fs_num_outputs
= 0;
910 memset(inputSlotToAttr
, ~0, sizeof(inputSlotToAttr
));
913 * Convert Mesa program inputs to TGSI input register semantics.
915 inputsRead
= stfp
->Base
.info
.inputs_read
;
916 for (attr
= 0; attr
< VARYING_SLOT_MAX
; attr
++) {
917 if ((inputsRead
& BITFIELD64_BIT(attr
)) != 0) {
918 const GLuint slot
= fs_num_inputs
++;
920 inputMapping
[attr
] = slot
;
921 inputSlotToAttr
[slot
] = attr
;
924 case VARYING_SLOT_POS
:
925 input_semantic_name
[slot
] = TGSI_SEMANTIC_POSITION
;
926 input_semantic_index
[slot
] = 0;
927 interpMode
[slot
] = TGSI_INTERPOLATE_LINEAR
;
929 case VARYING_SLOT_COL0
:
930 input_semantic_name
[slot
] = TGSI_SEMANTIC_COLOR
;
931 input_semantic_index
[slot
] = 0;
932 interpMode
[slot
] = stfp
->glsl_to_tgsi
?
933 TGSI_INTERPOLATE_COUNT
: TGSI_INTERPOLATE_COLOR
;
935 case VARYING_SLOT_COL1
:
936 input_semantic_name
[slot
] = TGSI_SEMANTIC_COLOR
;
937 input_semantic_index
[slot
] = 1;
938 interpMode
[slot
] = stfp
->glsl_to_tgsi
?
939 TGSI_INTERPOLATE_COUNT
: TGSI_INTERPOLATE_COLOR
;
941 case VARYING_SLOT_FOGC
:
942 input_semantic_name
[slot
] = TGSI_SEMANTIC_FOG
;
943 input_semantic_index
[slot
] = 0;
944 interpMode
[slot
] = TGSI_INTERPOLATE_PERSPECTIVE
;
946 case VARYING_SLOT_FACE
:
947 input_semantic_name
[slot
] = TGSI_SEMANTIC_FACE
;
948 input_semantic_index
[slot
] = 0;
949 interpMode
[slot
] = TGSI_INTERPOLATE_CONSTANT
;
951 case VARYING_SLOT_PRIMITIVE_ID
:
952 input_semantic_name
[slot
] = TGSI_SEMANTIC_PRIMID
;
953 input_semantic_index
[slot
] = 0;
954 interpMode
[slot
] = TGSI_INTERPOLATE_CONSTANT
;
956 case VARYING_SLOT_LAYER
:
957 input_semantic_name
[slot
] = TGSI_SEMANTIC_LAYER
;
958 input_semantic_index
[slot
] = 0;
959 interpMode
[slot
] = TGSI_INTERPOLATE_CONSTANT
;
961 case VARYING_SLOT_VIEWPORT
:
962 input_semantic_name
[slot
] = TGSI_SEMANTIC_VIEWPORT_INDEX
;
963 input_semantic_index
[slot
] = 0;
964 interpMode
[slot
] = TGSI_INTERPOLATE_CONSTANT
;
966 case VARYING_SLOT_CLIP_DIST0
:
967 input_semantic_name
[slot
] = TGSI_SEMANTIC_CLIPDIST
;
968 input_semantic_index
[slot
] = 0;
969 interpMode
[slot
] = TGSI_INTERPOLATE_PERSPECTIVE
;
971 case VARYING_SLOT_CLIP_DIST1
:
972 input_semantic_name
[slot
] = TGSI_SEMANTIC_CLIPDIST
;
973 input_semantic_index
[slot
] = 1;
974 interpMode
[slot
] = TGSI_INTERPOLATE_PERSPECTIVE
;
976 case VARYING_SLOT_CULL_DIST0
:
977 case VARYING_SLOT_CULL_DIST1
:
978 /* these should have been lowered by GLSL */
981 /* In most cases, there is nothing special about these
982 * inputs, so adopt a convention to use the generic
983 * semantic name and the mesa VARYING_SLOT_ number as the
986 * All that is required is that the vertex shader labels
987 * its own outputs similarly, and that the vertex shader
988 * generates at least every output required by the
989 * fragment shader plus fixed-function hardware (such as
992 * However, some drivers may need us to identify the PNTC and TEXi
993 * varyings if, for example, their capability to replace them with
994 * sprite coordinates is limited.
996 case VARYING_SLOT_PNTC
:
997 if (st
->needs_texcoord_semantic
) {
998 input_semantic_name
[slot
] = TGSI_SEMANTIC_PCOORD
;
999 input_semantic_index
[slot
] = 0;
1000 interpMode
[slot
] = TGSI_INTERPOLATE_LINEAR
;
1004 case VARYING_SLOT_TEX0
:
1005 case VARYING_SLOT_TEX1
:
1006 case VARYING_SLOT_TEX2
:
1007 case VARYING_SLOT_TEX3
:
1008 case VARYING_SLOT_TEX4
:
1009 case VARYING_SLOT_TEX5
:
1010 case VARYING_SLOT_TEX6
:
1011 case VARYING_SLOT_TEX7
:
1012 if (st
->needs_texcoord_semantic
) {
1013 input_semantic_name
[slot
] = TGSI_SEMANTIC_TEXCOORD
;
1014 input_semantic_index
[slot
] = attr
- VARYING_SLOT_TEX0
;
1015 interpMode
[slot
] = stfp
->glsl_to_tgsi
?
1016 TGSI_INTERPOLATE_COUNT
: TGSI_INTERPOLATE_PERSPECTIVE
;
1020 case VARYING_SLOT_VAR0
:
1022 /* Semantic indices should be zero-based because drivers may choose
1023 * to assign a fixed slot determined by that index.
1024 * This is useful because ARB_separate_shader_objects uses location
1025 * qualifiers for linkage, and if the semantic index corresponds to
1026 * these locations, linkage passes in the driver become unecessary.
1028 * If needs_texcoord_semantic is true, no semantic indices will be
1029 * consumed for the TEXi varyings, and we can base the locations of
1030 * the user varyings on VAR0. Otherwise, we use TEX0 as base index.
1032 assert(attr
>= VARYING_SLOT_VAR0
|| attr
== VARYING_SLOT_PNTC
||
1033 (attr
>= VARYING_SLOT_TEX0
&& attr
<= VARYING_SLOT_TEX7
));
1034 input_semantic_name
[slot
] = TGSI_SEMANTIC_GENERIC
;
1035 input_semantic_index
[slot
] = st_get_generic_varying_index(st
, attr
);
1036 if (attr
== VARYING_SLOT_PNTC
)
1037 interpMode
[slot
] = TGSI_INTERPOLATE_LINEAR
;
1039 interpMode
[slot
] = stfp
->glsl_to_tgsi
?
1040 TGSI_INTERPOLATE_COUNT
: TGSI_INTERPOLATE_PERSPECTIVE
;
1046 inputMapping
[attr
] = -1;
1051 * Semantics and mapping for outputs
1053 GLbitfield64 outputsWritten
= stfp
->Base
.info
.outputs_written
;
1055 /* if z is written, emit that first */
1056 if (outputsWritten
& BITFIELD64_BIT(FRAG_RESULT_DEPTH
)) {
1057 fs_output_semantic_name
[fs_num_outputs
] = TGSI_SEMANTIC_POSITION
;
1058 fs_output_semantic_index
[fs_num_outputs
] = 0;
1059 outputMapping
[FRAG_RESULT_DEPTH
] = fs_num_outputs
;
1061 outputsWritten
&= ~(1 << FRAG_RESULT_DEPTH
);
1064 if (outputsWritten
& BITFIELD64_BIT(FRAG_RESULT_STENCIL
)) {
1065 fs_output_semantic_name
[fs_num_outputs
] = TGSI_SEMANTIC_STENCIL
;
1066 fs_output_semantic_index
[fs_num_outputs
] = 0;
1067 outputMapping
[FRAG_RESULT_STENCIL
] = fs_num_outputs
;
1069 outputsWritten
&= ~(1 << FRAG_RESULT_STENCIL
);
1072 if (outputsWritten
& BITFIELD64_BIT(FRAG_RESULT_SAMPLE_MASK
)) {
1073 fs_output_semantic_name
[fs_num_outputs
] = TGSI_SEMANTIC_SAMPLEMASK
;
1074 fs_output_semantic_index
[fs_num_outputs
] = 0;
1075 outputMapping
[FRAG_RESULT_SAMPLE_MASK
] = fs_num_outputs
;
1077 outputsWritten
&= ~(1 << FRAG_RESULT_SAMPLE_MASK
);
1080 /* handle remaining outputs (color) */
1081 for (attr
= 0; attr
< ARRAY_SIZE(outputMapping
); attr
++) {
1082 const GLbitfield64 written
= attr
< FRAG_RESULT_MAX
? outputsWritten
:
1083 stfp
->Base
.SecondaryOutputsWritten
;
1084 const unsigned loc
= attr
% FRAG_RESULT_MAX
;
1086 if (written
& BITFIELD64_BIT(loc
)) {
1088 case FRAG_RESULT_DEPTH
:
1089 case FRAG_RESULT_STENCIL
:
1090 case FRAG_RESULT_SAMPLE_MASK
:
1094 case FRAG_RESULT_COLOR
:
1095 write_all
= GL_TRUE
; /* fallthrough */
1098 assert(loc
== FRAG_RESULT_COLOR
||
1099 (FRAG_RESULT_DATA0
<= loc
&& loc
< FRAG_RESULT_MAX
));
1101 index
= (loc
== FRAG_RESULT_COLOR
) ? 0 : (loc
- FRAG_RESULT_DATA0
);
1103 if (attr
>= FRAG_RESULT_MAX
) {
1104 /* Secondary color for dual source blending. */
1109 fs_output_semantic_name
[fs_num_outputs
] = TGSI_SEMANTIC_COLOR
;
1110 fs_output_semantic_index
[fs_num_outputs
] = index
;
1111 outputMapping
[attr
] = fs_num_outputs
;
1120 ureg
= ureg_create_with_screen(PIPE_SHADER_FRAGMENT
, st
->pipe
->screen
);
1124 if (ST_DEBUG
& DEBUG_MESA
) {
1125 _mesa_print_program(&stfp
->Base
);
1126 _mesa_print_program_parameters(st
->ctx
, &stfp
->Base
);
1129 if (write_all
== GL_TRUE
)
1130 ureg_property(ureg
, TGSI_PROPERTY_FS_COLOR0_WRITES_ALL_CBUFS
, 1);
1132 if (stfp
->Base
.info
.fs
.depth_layout
!= FRAG_DEPTH_LAYOUT_NONE
) {
1133 switch (stfp
->Base
.info
.fs
.depth_layout
) {
1134 case FRAG_DEPTH_LAYOUT_ANY
:
1135 ureg_property(ureg
, TGSI_PROPERTY_FS_DEPTH_LAYOUT
,
1136 TGSI_FS_DEPTH_LAYOUT_ANY
);
1138 case FRAG_DEPTH_LAYOUT_GREATER
:
1139 ureg_property(ureg
, TGSI_PROPERTY_FS_DEPTH_LAYOUT
,
1140 TGSI_FS_DEPTH_LAYOUT_GREATER
);
1142 case FRAG_DEPTH_LAYOUT_LESS
:
1143 ureg_property(ureg
, TGSI_PROPERTY_FS_DEPTH_LAYOUT
,
1144 TGSI_FS_DEPTH_LAYOUT_LESS
);
1146 case FRAG_DEPTH_LAYOUT_UNCHANGED
:
1147 ureg_property(ureg
, TGSI_PROPERTY_FS_DEPTH_LAYOUT
,
1148 TGSI_FS_DEPTH_LAYOUT_UNCHANGED
);
1155 if (stfp
->glsl_to_tgsi
) {
1156 st_translate_program(st
->ctx
,
1157 PIPE_SHADER_FRAGMENT
,
1165 input_semantic_name
,
1166 input_semantic_index
,
1171 fs_output_semantic_name
,
1172 fs_output_semantic_index
);
1174 free_glsl_to_tgsi_visitor(stfp
->glsl_to_tgsi
);
1175 } else if (stfp
->ati_fs
)
1176 st_translate_atifs_program(ureg
,
1182 input_semantic_name
,
1183 input_semantic_index
,
1188 fs_output_semantic_name
,
1189 fs_output_semantic_index
);
1191 st_translate_mesa_program(st
->ctx
,
1192 PIPE_SHADER_FRAGMENT
,
1198 input_semantic_name
,
1199 input_semantic_index
,
1204 fs_output_semantic_name
,
1205 fs_output_semantic_index
);
1207 stfp
->state
.tokens
= ureg_get_tokens(ureg
, NULL
);
1210 if (stfp
->glsl_to_tgsi
) {
1211 stfp
->glsl_to_tgsi
= NULL
;
1212 st_store_ir_in_disk_cache(st
, &stfp
->Base
, false);
1215 return stfp
->state
.tokens
!= NULL
;
1218 static struct st_fp_variant
*
1219 st_create_fp_variant(struct st_context
*st
,
1220 struct st_common_program
*stfp
,
1221 const struct st_fp_variant_key
*key
)
1223 struct pipe_context
*pipe
= st
->pipe
;
1224 struct st_fp_variant
*variant
= CALLOC_STRUCT(st_fp_variant
);
1225 struct pipe_shader_state state
= {0};
1226 struct gl_program_parameter_list
*params
= stfp
->Base
.Parameters
;
1227 static const gl_state_index16 texcoord_state
[STATE_LENGTH
] =
1228 { STATE_INTERNAL
, STATE_CURRENT_ATTRIB
, VERT_ATTRIB_TEX0
};
1229 static const gl_state_index16 scale_state
[STATE_LENGTH
] =
1230 { STATE_INTERNAL
, STATE_PT_SCALE
};
1231 static const gl_state_index16 bias_state
[STATE_LENGTH
] =
1232 { STATE_INTERNAL
, STATE_PT_BIAS
};
1233 static const gl_state_index16 alpha_ref_state
[STATE_LENGTH
] =
1234 { STATE_INTERNAL
, STATE_ALPHA_REF
};
1239 if (stfp
->state
.type
== PIPE_SHADER_IR_NIR
) {
1240 bool finalize
= false;
1242 state
.type
= PIPE_SHADER_IR_NIR
;
1243 state
.ir
.nir
= nir_shader_clone(NULL
, stfp
->state
.ir
.nir
);
1245 if (key
->clamp_color
) {
1246 NIR_PASS_V(state
.ir
.nir
, nir_lower_clamp_color_outputs
);
1250 if (key
->lower_flatshade
) {
1251 NIR_PASS_V(state
.ir
.nir
, nir_lower_flatshade
);
1255 if (key
->lower_alpha_func
!= COMPARE_FUNC_NEVER
) {
1256 _mesa_add_state_reference(params
, alpha_ref_state
);
1257 NIR_PASS_V(state
.ir
.nir
, nir_lower_alpha_test
, key
->lower_alpha_func
,
1258 false, alpha_ref_state
);
1262 if (key
->lower_two_sided_color
) {
1263 NIR_PASS_V(state
.ir
.nir
, nir_lower_two_sided_color
);
1267 if (key
->persample_shading
) {
1268 nir_shader
*shader
= state
.ir
.nir
;
1269 nir_foreach_variable(var
, &shader
->inputs
)
1270 var
->data
.sample
= true;
1274 assert(!(key
->bitmap
&& key
->drawpixels
));
1278 nir_lower_bitmap_options options
= {0};
1280 variant
->bitmap_sampler
= ffs(~stfp
->Base
.SamplersUsed
) - 1;
1281 options
.sampler
= variant
->bitmap_sampler
;
1282 options
.swizzle_xxxx
= st
->bitmap
.tex_format
== PIPE_FORMAT_R8_UNORM
;
1284 NIR_PASS_V(state
.ir
.nir
, nir_lower_bitmap
, &options
);
1288 /* glDrawPixels (color only) */
1289 if (key
->drawpixels
) {
1290 nir_lower_drawpixels_options options
= {{0}};
1291 unsigned samplers_used
= stfp
->Base
.SamplersUsed
;
1293 /* Find the first unused slot. */
1294 variant
->drawpix_sampler
= ffs(~samplers_used
) - 1;
1295 options
.drawpix_sampler
= variant
->drawpix_sampler
;
1296 samplers_used
|= (1 << variant
->drawpix_sampler
);
1298 options
.pixel_maps
= key
->pixelMaps
;
1299 if (key
->pixelMaps
) {
1300 variant
->pixelmap_sampler
= ffs(~samplers_used
) - 1;
1301 options
.pixelmap_sampler
= variant
->pixelmap_sampler
;
1304 options
.scale_and_bias
= key
->scaleAndBias
;
1305 if (key
->scaleAndBias
) {
1306 _mesa_add_state_reference(params
, scale_state
);
1307 memcpy(options
.scale_state_tokens
, scale_state
,
1308 sizeof(options
.scale_state_tokens
));
1309 _mesa_add_state_reference(params
, bias_state
);
1310 memcpy(options
.bias_state_tokens
, bias_state
,
1311 sizeof(options
.bias_state_tokens
));
1314 _mesa_add_state_reference(params
, texcoord_state
);
1315 memcpy(options
.texcoord_state_tokens
, texcoord_state
,
1316 sizeof(options
.texcoord_state_tokens
));
1318 NIR_PASS_V(state
.ir
.nir
, nir_lower_drawpixels
, &options
);
1322 if (unlikely(key
->external
.lower_nv12
|| key
->external
.lower_iyuv
||
1323 key
->external
.lower_xy_uxvx
|| key
->external
.lower_yx_xuxv
||
1324 key
->external
.lower_ayuv
|| key
->external
.lower_xyuv
)) {
1325 nir_lower_tex_options options
= {0};
1326 options
.lower_y_uv_external
= key
->external
.lower_nv12
;
1327 options
.lower_y_u_v_external
= key
->external
.lower_iyuv
;
1328 options
.lower_xy_uxvx_external
= key
->external
.lower_xy_uxvx
;
1329 options
.lower_yx_xuxv_external
= key
->external
.lower_yx_xuxv
;
1330 options
.lower_ayuv_external
= key
->external
.lower_ayuv
;
1331 options
.lower_xyuv_external
= key
->external
.lower_xyuv
;
1332 NIR_PASS_V(state
.ir
.nir
, nir_lower_tex
, &options
);
1336 if (finalize
|| !st
->allow_st_finalize_nir_twice
) {
1337 st_finalize_nir(st
, &stfp
->Base
, stfp
->shader_program
, state
.ir
.nir
,
1341 /* This pass needs to happen *after* nir_lower_sampler */
1342 if (unlikely(key
->external
.lower_nv12
|| key
->external
.lower_iyuv
||
1343 key
->external
.lower_xy_uxvx
|| key
->external
.lower_yx_xuxv
)) {
1344 NIR_PASS_V(state
.ir
.nir
, st_nir_lower_tex_src_plane
,
1345 ~stfp
->Base
.SamplersUsed
,
1346 key
->external
.lower_nv12
|| key
->external
.lower_xy_uxvx
||
1347 key
->external
.lower_yx_xuxv
,
1348 key
->external
.lower_iyuv
);
1352 if (finalize
|| !st
->allow_st_finalize_nir_twice
) {
1353 /* Some of the lowering above may have introduced new varyings */
1354 nir_shader_gather_info(state
.ir
.nir
,
1355 nir_shader_get_entrypoint(state
.ir
.nir
));
1357 struct pipe_screen
*screen
= pipe
->screen
;
1358 if (screen
->finalize_nir
)
1359 screen
->finalize_nir(screen
, state
.ir
.nir
, false);
1362 variant
->driver_shader
= pipe
->create_fs_state(pipe
, &state
);
1363 variant
->key
= *key
;
1368 state
.tokens
= stfp
->state
.tokens
;
1370 assert(!(key
->bitmap
&& key
->drawpixels
));
1372 /* Fix texture targets and add fog for ATI_fs */
1374 const struct tgsi_token
*tokens
= st_fixup_atifs(state
.tokens
, key
);
1377 state
.tokens
= tokens
;
1379 fprintf(stderr
, "mesa: cannot post-process ATI_fs\n");
1382 /* Emulate features. */
1383 if (key
->clamp_color
|| key
->persample_shading
) {
1384 const struct tgsi_token
*tokens
;
1386 (key
->clamp_color
? TGSI_EMU_CLAMP_COLOR_OUTPUTS
: 0) |
1387 (key
->persample_shading
? TGSI_EMU_FORCE_PERSAMPLE_INTERP
: 0);
1389 tokens
= tgsi_emulate(state
.tokens
, flags
);
1392 if (state
.tokens
!= stfp
->state
.tokens
)
1393 tgsi_free_tokens(state
.tokens
);
1394 state
.tokens
= tokens
;
1396 fprintf(stderr
, "mesa: cannot emulate deprecated features\n");
1401 const struct tgsi_token
*tokens
;
1403 variant
->bitmap_sampler
= ffs(~stfp
->Base
.SamplersUsed
) - 1;
1405 tokens
= st_get_bitmap_shader(state
.tokens
,
1406 st
->internal_target
,
1407 variant
->bitmap_sampler
,
1408 st
->needs_texcoord_semantic
,
1409 st
->bitmap
.tex_format
==
1410 PIPE_FORMAT_R8_UNORM
);
1413 if (state
.tokens
!= stfp
->state
.tokens
)
1414 tgsi_free_tokens(state
.tokens
);
1415 state
.tokens
= tokens
;
1417 fprintf(stderr
, "mesa: cannot create a shader for glBitmap\n");
1420 /* glDrawPixels (color only) */
1421 if (key
->drawpixels
) {
1422 const struct tgsi_token
*tokens
;
1423 unsigned scale_const
= 0, bias_const
= 0, texcoord_const
= 0;
1425 /* Find the first unused slot. */
1426 variant
->drawpix_sampler
= ffs(~stfp
->Base
.SamplersUsed
) - 1;
1428 if (key
->pixelMaps
) {
1429 unsigned samplers_used
= stfp
->Base
.SamplersUsed
|
1430 (1 << variant
->drawpix_sampler
);
1432 variant
->pixelmap_sampler
= ffs(~samplers_used
) - 1;
1435 if (key
->scaleAndBias
) {
1436 scale_const
= _mesa_add_state_reference(params
, scale_state
);
1437 bias_const
= _mesa_add_state_reference(params
, bias_state
);
1440 texcoord_const
= _mesa_add_state_reference(params
, texcoord_state
);
1442 tokens
= st_get_drawpix_shader(state
.tokens
,
1443 st
->needs_texcoord_semantic
,
1444 key
->scaleAndBias
, scale_const
,
1445 bias_const
, key
->pixelMaps
,
1446 variant
->drawpix_sampler
,
1447 variant
->pixelmap_sampler
,
1448 texcoord_const
, st
->internal_target
);
1451 if (state
.tokens
!= stfp
->state
.tokens
)
1452 tgsi_free_tokens(state
.tokens
);
1453 state
.tokens
= tokens
;
1455 fprintf(stderr
, "mesa: cannot create a shader for glDrawPixels\n");
1458 if (unlikely(key
->external
.lower_nv12
|| key
->external
.lower_iyuv
||
1459 key
->external
.lower_xy_uxvx
|| key
->external
.lower_yx_xuxv
)) {
1460 const struct tgsi_token
*tokens
;
1462 /* samplers inserted would conflict, but this should be unpossible: */
1463 assert(!(key
->bitmap
|| key
->drawpixels
));
1465 tokens
= st_tgsi_lower_yuv(state
.tokens
,
1466 ~stfp
->Base
.SamplersUsed
,
1467 key
->external
.lower_nv12
||
1468 key
->external
.lower_xy_uxvx
||
1469 key
->external
.lower_yx_xuxv
,
1470 key
->external
.lower_iyuv
);
1472 if (state
.tokens
!= stfp
->state
.tokens
)
1473 tgsi_free_tokens(state
.tokens
);
1474 state
.tokens
= tokens
;
1476 fprintf(stderr
, "mesa: cannot create a shader for samplerExternalOES\n");
1480 if (key
->lower_depth_clamp
) {
1481 unsigned depth_range_const
= _mesa_add_state_reference(params
, depth_range_state
);
1483 const struct tgsi_token
*tokens
;
1484 tokens
= st_tgsi_lower_depth_clamp_fs(state
.tokens
, depth_range_const
);
1485 if (state
.tokens
!= stfp
->state
.tokens
)
1486 tgsi_free_tokens(state
.tokens
);
1487 state
.tokens
= tokens
;
1490 if (ST_DEBUG
& DEBUG_PRINT_IR
)
1491 tgsi_dump(state
.tokens
, 0);
1493 /* fill in variant */
1494 variant
->driver_shader
= pipe
->create_fs_state(pipe
, &state
);
1495 variant
->key
= *key
;
1497 if (state
.tokens
!= stfp
->state
.tokens
)
1498 tgsi_free_tokens(state
.tokens
);
1503 * Translate fragment program if needed.
1505 struct st_fp_variant
*
1506 st_get_fp_variant(struct st_context
*st
,
1507 struct st_common_program
*stfp
,
1508 const struct st_fp_variant_key
*key
)
1510 struct st_fp_variant
*fpv
;
1512 /* Search for existing variant */
1513 for (fpv
= stfp
->fp_variants
; fpv
; fpv
= fpv
->next
) {
1514 if (memcmp(&fpv
->key
, key
, sizeof(*key
)) == 0) {
1521 fpv
= st_create_fp_variant(st
, stfp
, key
);
1523 if (key
->bitmap
|| key
->drawpixels
) {
1524 /* Regular variants should always come before the
1525 * bitmap & drawpixels variants, (unless there
1526 * are no regular variants) so that
1527 * st_update_fp can take a fast path when
1528 * shader_has_one_variant is set.
1530 if (!stfp
->fp_variants
) {
1531 stfp
->fp_variants
= fpv
;
1533 /* insert into list after the first one */
1534 fpv
->next
= stfp
->fp_variants
->next
;
1535 stfp
->fp_variants
->next
= fpv
;
1538 /* insert into list */
1539 fpv
->next
= stfp
->fp_variants
;
1540 stfp
->fp_variants
= fpv
;
1549 * Translate a program. This is common code for geometry and tessellation
1553 st_translate_common_program(struct st_context
*st
,
1554 struct st_common_program
*stcp
)
1556 struct gl_program
*prog
= &stcp
->Base
;
1557 enum pipe_shader_type stage
=
1558 pipe_shader_type_from_mesa(stcp
->Base
.info
.stage
);
1559 struct ureg_program
*ureg
= ureg_create_with_screen(stage
, st
->pipe
->screen
);
1565 case PIPE_SHADER_TESS_CTRL
:
1566 ureg_property(ureg
, TGSI_PROPERTY_TCS_VERTICES_OUT
,
1567 stcp
->Base
.info
.tess
.tcs_vertices_out
);
1570 case PIPE_SHADER_TESS_EVAL
:
1571 if (stcp
->Base
.info
.tess
.primitive_mode
== GL_ISOLINES
)
1572 ureg_property(ureg
, TGSI_PROPERTY_TES_PRIM_MODE
, GL_LINES
);
1574 ureg_property(ureg
, TGSI_PROPERTY_TES_PRIM_MODE
,
1575 stcp
->Base
.info
.tess
.primitive_mode
);
1577 STATIC_ASSERT((TESS_SPACING_EQUAL
+ 1) % 3 == PIPE_TESS_SPACING_EQUAL
);
1578 STATIC_ASSERT((TESS_SPACING_FRACTIONAL_ODD
+ 1) % 3 ==
1579 PIPE_TESS_SPACING_FRACTIONAL_ODD
);
1580 STATIC_ASSERT((TESS_SPACING_FRACTIONAL_EVEN
+ 1) % 3 ==
1581 PIPE_TESS_SPACING_FRACTIONAL_EVEN
);
1583 ureg_property(ureg
, TGSI_PROPERTY_TES_SPACING
,
1584 (stcp
->Base
.info
.tess
.spacing
+ 1) % 3);
1586 ureg_property(ureg
, TGSI_PROPERTY_TES_VERTEX_ORDER_CW
,
1587 !stcp
->Base
.info
.tess
.ccw
);
1588 ureg_property(ureg
, TGSI_PROPERTY_TES_POINT_MODE
,
1589 stcp
->Base
.info
.tess
.point_mode
);
1592 case PIPE_SHADER_GEOMETRY
:
1593 ureg_property(ureg
, TGSI_PROPERTY_GS_INPUT_PRIM
,
1594 stcp
->Base
.info
.gs
.input_primitive
);
1595 ureg_property(ureg
, TGSI_PROPERTY_GS_OUTPUT_PRIM
,
1596 stcp
->Base
.info
.gs
.output_primitive
);
1597 ureg_property(ureg
, TGSI_PROPERTY_GS_MAX_OUTPUT_VERTICES
,
1598 stcp
->Base
.info
.gs
.vertices_out
);
1599 ureg_property(ureg
, TGSI_PROPERTY_GS_INVOCATIONS
,
1600 stcp
->Base
.info
.gs
.invocations
);
1607 ubyte inputSlotToAttr
[VARYING_SLOT_TESS_MAX
];
1608 ubyte inputMapping
[VARYING_SLOT_TESS_MAX
];
1609 ubyte outputMapping
[VARYING_SLOT_TESS_MAX
];
1612 ubyte input_semantic_name
[PIPE_MAX_SHADER_INPUTS
];
1613 ubyte input_semantic_index
[PIPE_MAX_SHADER_INPUTS
];
1614 uint num_inputs
= 0;
1616 ubyte output_semantic_name
[PIPE_MAX_SHADER_OUTPUTS
];
1617 ubyte output_semantic_index
[PIPE_MAX_SHADER_OUTPUTS
];
1618 uint num_outputs
= 0;
1622 memset(inputSlotToAttr
, 0, sizeof(inputSlotToAttr
));
1623 memset(inputMapping
, 0, sizeof(inputMapping
));
1624 memset(outputMapping
, 0, sizeof(outputMapping
));
1625 memset(&stcp
->state
, 0, sizeof(stcp
->state
));
1627 if (prog
->info
.clip_distance_array_size
)
1628 ureg_property(ureg
, TGSI_PROPERTY_NUM_CLIPDIST_ENABLED
,
1629 prog
->info
.clip_distance_array_size
);
1630 if (prog
->info
.cull_distance_array_size
)
1631 ureg_property(ureg
, TGSI_PROPERTY_NUM_CULLDIST_ENABLED
,
1632 prog
->info
.cull_distance_array_size
);
1635 * Convert Mesa program inputs to TGSI input register semantics.
1637 for (attr
= 0; attr
< VARYING_SLOT_MAX
; attr
++) {
1638 if ((prog
->info
.inputs_read
& BITFIELD64_BIT(attr
)) == 0)
1641 unsigned slot
= num_inputs
++;
1643 inputMapping
[attr
] = slot
;
1644 inputSlotToAttr
[slot
] = attr
;
1646 unsigned semantic_name
, semantic_index
;
1647 tgsi_get_gl_varying_semantic(attr
, st
->needs_texcoord_semantic
,
1648 &semantic_name
, &semantic_index
);
1649 input_semantic_name
[slot
] = semantic_name
;
1650 input_semantic_index
[slot
] = semantic_index
;
1653 /* Also add patch inputs. */
1654 for (attr
= 0; attr
< 32; attr
++) {
1655 if (prog
->info
.patch_inputs_read
& (1u << attr
)) {
1656 GLuint slot
= num_inputs
++;
1657 GLuint patch_attr
= VARYING_SLOT_PATCH0
+ attr
;
1659 inputMapping
[patch_attr
] = slot
;
1660 inputSlotToAttr
[slot
] = patch_attr
;
1661 input_semantic_name
[slot
] = TGSI_SEMANTIC_PATCH
;
1662 input_semantic_index
[slot
] = attr
;
1666 /* initialize output semantics to defaults */
1667 for (i
= 0; i
< PIPE_MAX_SHADER_OUTPUTS
; i
++) {
1668 output_semantic_name
[i
] = TGSI_SEMANTIC_GENERIC
;
1669 output_semantic_index
[i
] = 0;
1673 * Determine number of outputs, the (default) output register
1674 * mapping and the semantic information for each output.
1676 for (attr
= 0; attr
< VARYING_SLOT_MAX
; attr
++) {
1677 if (prog
->info
.outputs_written
& BITFIELD64_BIT(attr
)) {
1678 GLuint slot
= num_outputs
++;
1680 outputMapping
[attr
] = slot
;
1682 unsigned semantic_name
, semantic_index
;
1683 tgsi_get_gl_varying_semantic(attr
, st
->needs_texcoord_semantic
,
1684 &semantic_name
, &semantic_index
);
1685 output_semantic_name
[slot
] = semantic_name
;
1686 output_semantic_index
[slot
] = semantic_index
;
1690 /* Also add patch outputs. */
1691 for (attr
= 0; attr
< 32; attr
++) {
1692 if (prog
->info
.patch_outputs_written
& (1u << attr
)) {
1693 GLuint slot
= num_outputs
++;
1694 GLuint patch_attr
= VARYING_SLOT_PATCH0
+ attr
;
1696 outputMapping
[patch_attr
] = slot
;
1697 output_semantic_name
[slot
] = TGSI_SEMANTIC_PATCH
;
1698 output_semantic_index
[slot
] = attr
;
1702 st_translate_program(st
->ctx
,
1711 input_semantic_name
,
1712 input_semantic_index
,
1717 output_semantic_name
,
1718 output_semantic_index
);
1720 stcp
->state
.tokens
= ureg_get_tokens(ureg
, NULL
);
1724 st_translate_stream_output_info(prog
);
1726 st_store_ir_in_disk_cache(st
, prog
, false);
1728 if (ST_DEBUG
& DEBUG_PRINT_IR
&& ST_DEBUG
& DEBUG_MESA
)
1729 _mesa_print_program(prog
);
1731 if (ST_DEBUG
& DEBUG_PRINT_IR
)
1732 tgsi_dump(stcp
->state
.tokens
, 0);
1734 free_glsl_to_tgsi_visitor(stcp
->glsl_to_tgsi
);
1735 stcp
->glsl_to_tgsi
= NULL
;
1741 * Get/create a basic program variant.
1743 struct st_common_variant
*
1744 st_get_common_variant(struct st_context
*st
,
1745 struct st_common_program
*prog
,
1746 const struct st_common_variant_key
*key
)
1748 struct pipe_context
*pipe
= st
->pipe
;
1749 struct st_common_variant
*v
;
1750 struct pipe_shader_state state
= {0};
1752 /* Search for existing variant */
1753 for (v
= prog
->variants
; v
; v
= v
->next
) {
1754 if (memcmp(&v
->key
, key
, sizeof(*key
)) == 0) {
1761 v
= CALLOC_STRUCT(st_common_variant
);
1763 if (prog
->state
.type
== PIPE_SHADER_IR_NIR
) {
1764 bool finalize
= false;
1766 state
.type
= PIPE_SHADER_IR_NIR
;
1767 state
.ir
.nir
= nir_shader_clone(NULL
, prog
->state
.ir
.nir
);
1769 if (key
->clamp_color
) {
1770 NIR_PASS_V(state
.ir
.nir
, nir_lower_clamp_color_outputs
);
1774 state
.stream_output
= prog
->state
.stream_output
;
1776 if (finalize
|| !st
->allow_st_finalize_nir_twice
) {
1777 st_finalize_nir(st
, &prog
->Base
, prog
->shader_program
,
1778 state
.ir
.nir
, true);
1781 if (key
->lower_depth_clamp
) {
1782 struct gl_program_parameter_list
*params
= prog
->Base
.Parameters
;
1784 unsigned depth_range_const
=
1785 _mesa_add_state_reference(params
, depth_range_state
);
1787 const struct tgsi_token
*tokens
;
1789 st_tgsi_lower_depth_clamp(prog
->state
.tokens
,
1791 key
->clip_negative_one_to_one
);
1793 if (tokens
!= prog
->state
.tokens
)
1794 tgsi_free_tokens(prog
->state
.tokens
);
1796 prog
->state
.tokens
= tokens
;
1798 state
= prog
->state
;
1800 /* fill in new variant */
1801 switch (prog
->Base
.info
.stage
) {
1802 case MESA_SHADER_TESS_CTRL
:
1803 v
->driver_shader
= pipe
->create_tcs_state(pipe
, &state
);
1805 case MESA_SHADER_TESS_EVAL
:
1806 v
->driver_shader
= pipe
->create_tes_state(pipe
, &state
);
1808 case MESA_SHADER_GEOMETRY
:
1809 v
->driver_shader
= pipe
->create_gs_state(pipe
, &state
);
1811 case MESA_SHADER_COMPUTE
: {
1812 struct pipe_compute_state cs
= {0};
1813 cs
.ir_type
= state
.type
;
1814 cs
.req_local_mem
= prog
->Base
.info
.cs
.shared_size
;
1816 if (state
.type
== PIPE_SHADER_IR_NIR
)
1817 cs
.prog
= state
.ir
.nir
;
1819 cs
.prog
= state
.tokens
;
1821 v
->driver_shader
= pipe
->create_compute_state(pipe
, &cs
);
1825 assert(!"unhandled shader type");
1832 /* insert into list */
1833 v
->next
= prog
->variants
;
1843 * Vert/Geom/Frag programs have per-context variants. Free all the
1844 * variants attached to the given program which match the given context.
1847 destroy_program_variants(struct st_context
*st
, struct gl_program
*target
)
1849 if (!target
|| target
== &_mesa_DummyProgram
)
1852 switch (target
->Target
) {
1853 case GL_VERTEX_PROGRAM_ARB
:
1855 struct st_vertex_program
*stvp
= (struct st_vertex_program
*) target
;
1856 struct st_vp_variant
*vpv
, **prevPtr
= &stvp
->variants
;
1858 for (vpv
= stvp
->variants
; vpv
; ) {
1859 struct st_vp_variant
*next
= vpv
->next
;
1860 if (vpv
->key
.st
== st
) {
1861 /* unlink from list */
1863 /* destroy this variant */
1864 delete_vp_variant(st
, vpv
);
1867 prevPtr
= &vpv
->next
;
1873 case GL_FRAGMENT_PROGRAM_ARB
:
1875 struct st_common_program
*stfp
=
1876 (struct st_common_program
*) target
;
1877 struct st_fp_variant
*fpv
, **prevPtr
= &stfp
->fp_variants
;
1879 for (fpv
= stfp
->fp_variants
; fpv
; ) {
1880 struct st_fp_variant
*next
= fpv
->next
;
1881 if (fpv
->key
.st
== st
) {
1882 /* unlink from list */
1884 /* destroy this variant */
1885 delete_fp_variant(st
, fpv
);
1888 prevPtr
= &fpv
->next
;
1894 case GL_GEOMETRY_PROGRAM_NV
:
1895 case GL_TESS_CONTROL_PROGRAM_NV
:
1896 case GL_TESS_EVALUATION_PROGRAM_NV
:
1897 case GL_COMPUTE_PROGRAM_NV
:
1899 struct st_common_program
*p
= st_common_program(target
);
1900 struct st_common_variant
*v
, **prevPtr
= &p
->variants
;
1902 for (v
= p
->variants
; v
; ) {
1903 struct st_common_variant
*next
= v
->next
;
1904 if (v
->key
.st
== st
) {
1905 /* unlink from list */
1907 /* destroy this variant */
1908 delete_basic_variant(st
, v
, target
->Target
);
1918 _mesa_problem(NULL
, "Unexpected program target 0x%x in "
1919 "destroy_program_variants_cb()", target
->Target
);
1925 * Callback for _mesa_HashWalk. Free all the shader's program variants
1926 * which match the given context.
1929 destroy_shader_program_variants_cb(GLuint key
, void *data
, void *userData
)
1931 struct st_context
*st
= (struct st_context
*) userData
;
1932 struct gl_shader
*shader
= (struct gl_shader
*) data
;
1934 switch (shader
->Type
) {
1935 case GL_SHADER_PROGRAM_MESA
:
1937 struct gl_shader_program
*shProg
= (struct gl_shader_program
*) data
;
1940 for (i
= 0; i
< ARRAY_SIZE(shProg
->_LinkedShaders
); i
++) {
1941 if (shProg
->_LinkedShaders
[i
])
1942 destroy_program_variants(st
, shProg
->_LinkedShaders
[i
]->Program
);
1946 case GL_VERTEX_SHADER
:
1947 case GL_FRAGMENT_SHADER
:
1948 case GL_GEOMETRY_SHADER
:
1949 case GL_TESS_CONTROL_SHADER
:
1950 case GL_TESS_EVALUATION_SHADER
:
1951 case GL_COMPUTE_SHADER
:
1960 * Callback for _mesa_HashWalk. Free all the program variants which match
1961 * the given context.
1964 destroy_program_variants_cb(GLuint key
, void *data
, void *userData
)
1966 struct st_context
*st
= (struct st_context
*) userData
;
1967 struct gl_program
*program
= (struct gl_program
*) data
;
1968 destroy_program_variants(st
, program
);
1973 * Walk over all shaders and programs to delete any variants which
1974 * belong to the given context.
1975 * This is called during context tear-down.
1978 st_destroy_program_variants(struct st_context
*st
)
1980 /* If shaders can be shared with other contexts, the last context will
1981 * call DeleteProgram on all shaders, releasing everything.
1983 if (st
->has_shareable_shaders
)
1986 /* ARB vert/frag program */
1987 _mesa_HashWalk(st
->ctx
->Shared
->Programs
,
1988 destroy_program_variants_cb
, st
);
1990 /* GLSL vert/frag/geom shaders */
1991 _mesa_HashWalk(st
->ctx
->Shared
->ShaderObjects
,
1992 destroy_shader_program_variants_cb
, st
);
1997 * For debugging, print/dump the current vertex program.
2000 st_print_current_vertex_program(void)
2002 GET_CURRENT_CONTEXT(ctx
);
2004 if (ctx
->VertexProgram
._Current
) {
2005 struct st_vertex_program
*stvp
=
2006 (struct st_vertex_program
*) ctx
->VertexProgram
._Current
;
2007 struct st_vp_variant
*stv
;
2009 debug_printf("Vertex program %u\n", stvp
->Base
.Id
);
2011 for (stv
= stvp
->variants
; stv
; stv
= stv
->next
) {
2012 debug_printf("variant %p\n", stv
);
2013 tgsi_dump(stv
->tokens
, 0);
2020 * Compile one shader variant.
2023 st_precompile_shader_variant(struct st_context
*st
,
2024 struct gl_program
*prog
)
2026 switch (prog
->Target
) {
2027 case GL_VERTEX_PROGRAM_ARB
: {
2028 struct st_vertex_program
*p
= (struct st_vertex_program
*)prog
;
2029 struct st_common_variant_key key
;
2031 memset(&key
, 0, sizeof(key
));
2033 key
.st
= st
->has_shareable_shaders
? NULL
: st
;
2034 st_get_vp_variant(st
, p
, &key
);
2038 case GL_FRAGMENT_PROGRAM_ARB
: {
2039 struct st_common_program
*p
= (struct st_common_program
*)prog
;
2040 struct st_fp_variant_key key
;
2042 memset(&key
, 0, sizeof(key
));
2044 key
.st
= st
->has_shareable_shaders
? NULL
: st
;
2045 st_get_fp_variant(st
, p
, &key
);
2049 case GL_TESS_CONTROL_PROGRAM_NV
:
2050 case GL_TESS_EVALUATION_PROGRAM_NV
:
2051 case GL_GEOMETRY_PROGRAM_NV
:
2052 case GL_COMPUTE_PROGRAM_NV
: {
2053 struct st_common_program
*p
= st_common_program(prog
);
2054 struct st_common_variant_key key
;
2056 memset(&key
, 0, sizeof(key
));
2058 key
.st
= st
->has_shareable_shaders
? NULL
: st
;
2059 st_get_common_variant(st
, p
, &key
);