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_TGSI
) {
795 tgsi_dump(state
.tokens
, 0);
799 vpv
->driver_shader
= pipe
->create_vs_state(pipe
, &state
);
800 /* Save this for selection/feedback/rasterpos. */
801 vpv
->tokens
= state
.tokens
;
807 * Find/create a vertex program variant.
809 struct st_vp_variant
*
810 st_get_vp_variant(struct st_context
*st
,
811 struct st_vertex_program
*stvp
,
812 const struct st_common_variant_key
*key
)
814 struct st_vp_variant
*vpv
;
816 /* Search for existing variant */
817 for (vpv
= stvp
->variants
; vpv
; vpv
= vpv
->next
) {
818 if (memcmp(&vpv
->key
, key
, sizeof(*key
)) == 0) {
825 vpv
= st_create_vp_variant(st
, stvp
, key
);
827 for (unsigned index
= 0; index
< vpv
->num_inputs
; ++index
) {
828 unsigned attr
= stvp
->index_to_input
[index
];
829 if (attr
== ST_DOUBLE_ATTRIB_PLACEHOLDER
)
831 vpv
->vert_attrib_mask
|= 1u << attr
;
834 /* insert into list */
835 vpv
->next
= stvp
->variants
;
836 stvp
->variants
= vpv
;
845 * Translate a Mesa fragment shader into a TGSI shader.
848 st_translate_fragment_program(struct st_context
*st
,
849 struct st_common_program
*stfp
)
851 /* Non-GLSL programs: */
852 if (!stfp
->glsl_to_tgsi
) {
853 _mesa_remove_output_reads(&stfp
->Base
, PROGRAM_OUTPUT
);
854 if (st
->ctx
->Const
.GLSLFragCoordIsSysVal
)
855 _mesa_program_fragment_position_to_sysval(&stfp
->Base
);
857 /* This determines which states will be updated when the assembly
860 * fragment.position and glDrawPixels always use constants.
862 stfp
->affected_states
= ST_NEW_FS_STATE
|
863 ST_NEW_SAMPLE_SHADING
|
867 /* Just set them for ATI_fs unconditionally. */
868 stfp
->affected_states
|= ST_NEW_FS_SAMPLER_VIEWS
|
872 if (stfp
->Base
.SamplersUsed
)
873 stfp
->affected_states
|= ST_NEW_FS_SAMPLER_VIEWS
|
877 /* Translate to NIR. */
879 st
->pipe
->screen
->get_shader_param(st
->pipe
->screen
,
880 PIPE_SHADER_FRAGMENT
,
881 PIPE_SHADER_CAP_PREFERRED_IR
)) {
883 st_translate_prog_to_nir(st
, &stfp
->Base
, MESA_SHADER_FRAGMENT
);
885 if (stfp
->state
.ir
.nir
)
886 ralloc_free(stfp
->state
.ir
.nir
);
887 stfp
->state
.type
= PIPE_SHADER_IR_NIR
;
888 stfp
->state
.ir
.nir
= nir
;
889 stfp
->Base
.nir
= nir
;
894 ubyte outputMapping
[2 * FRAG_RESULT_MAX
];
895 ubyte inputMapping
[VARYING_SLOT_MAX
];
896 ubyte inputSlotToAttr
[VARYING_SLOT_MAX
];
897 ubyte interpMode
[PIPE_MAX_SHADER_INPUTS
]; /* XXX size? */
899 GLbitfield64 inputsRead
;
900 struct ureg_program
*ureg
;
902 GLboolean write_all
= GL_FALSE
;
904 ubyte input_semantic_name
[PIPE_MAX_SHADER_INPUTS
];
905 ubyte input_semantic_index
[PIPE_MAX_SHADER_INPUTS
];
906 uint fs_num_inputs
= 0;
908 ubyte fs_output_semantic_name
[PIPE_MAX_SHADER_OUTPUTS
];
909 ubyte fs_output_semantic_index
[PIPE_MAX_SHADER_OUTPUTS
];
910 uint fs_num_outputs
= 0;
912 memset(inputSlotToAttr
, ~0, sizeof(inputSlotToAttr
));
915 * Convert Mesa program inputs to TGSI input register semantics.
917 inputsRead
= stfp
->Base
.info
.inputs_read
;
918 for (attr
= 0; attr
< VARYING_SLOT_MAX
; attr
++) {
919 if ((inputsRead
& BITFIELD64_BIT(attr
)) != 0) {
920 const GLuint slot
= fs_num_inputs
++;
922 inputMapping
[attr
] = slot
;
923 inputSlotToAttr
[slot
] = attr
;
926 case VARYING_SLOT_POS
:
927 input_semantic_name
[slot
] = TGSI_SEMANTIC_POSITION
;
928 input_semantic_index
[slot
] = 0;
929 interpMode
[slot
] = TGSI_INTERPOLATE_LINEAR
;
931 case VARYING_SLOT_COL0
:
932 input_semantic_name
[slot
] = TGSI_SEMANTIC_COLOR
;
933 input_semantic_index
[slot
] = 0;
934 interpMode
[slot
] = stfp
->glsl_to_tgsi
?
935 TGSI_INTERPOLATE_COUNT
: TGSI_INTERPOLATE_COLOR
;
937 case VARYING_SLOT_COL1
:
938 input_semantic_name
[slot
] = TGSI_SEMANTIC_COLOR
;
939 input_semantic_index
[slot
] = 1;
940 interpMode
[slot
] = stfp
->glsl_to_tgsi
?
941 TGSI_INTERPOLATE_COUNT
: TGSI_INTERPOLATE_COLOR
;
943 case VARYING_SLOT_FOGC
:
944 input_semantic_name
[slot
] = TGSI_SEMANTIC_FOG
;
945 input_semantic_index
[slot
] = 0;
946 interpMode
[slot
] = TGSI_INTERPOLATE_PERSPECTIVE
;
948 case VARYING_SLOT_FACE
:
949 input_semantic_name
[slot
] = TGSI_SEMANTIC_FACE
;
950 input_semantic_index
[slot
] = 0;
951 interpMode
[slot
] = TGSI_INTERPOLATE_CONSTANT
;
953 case VARYING_SLOT_PRIMITIVE_ID
:
954 input_semantic_name
[slot
] = TGSI_SEMANTIC_PRIMID
;
955 input_semantic_index
[slot
] = 0;
956 interpMode
[slot
] = TGSI_INTERPOLATE_CONSTANT
;
958 case VARYING_SLOT_LAYER
:
959 input_semantic_name
[slot
] = TGSI_SEMANTIC_LAYER
;
960 input_semantic_index
[slot
] = 0;
961 interpMode
[slot
] = TGSI_INTERPOLATE_CONSTANT
;
963 case VARYING_SLOT_VIEWPORT
:
964 input_semantic_name
[slot
] = TGSI_SEMANTIC_VIEWPORT_INDEX
;
965 input_semantic_index
[slot
] = 0;
966 interpMode
[slot
] = TGSI_INTERPOLATE_CONSTANT
;
968 case VARYING_SLOT_CLIP_DIST0
:
969 input_semantic_name
[slot
] = TGSI_SEMANTIC_CLIPDIST
;
970 input_semantic_index
[slot
] = 0;
971 interpMode
[slot
] = TGSI_INTERPOLATE_PERSPECTIVE
;
973 case VARYING_SLOT_CLIP_DIST1
:
974 input_semantic_name
[slot
] = TGSI_SEMANTIC_CLIPDIST
;
975 input_semantic_index
[slot
] = 1;
976 interpMode
[slot
] = TGSI_INTERPOLATE_PERSPECTIVE
;
978 case VARYING_SLOT_CULL_DIST0
:
979 case VARYING_SLOT_CULL_DIST1
:
980 /* these should have been lowered by GLSL */
983 /* In most cases, there is nothing special about these
984 * inputs, so adopt a convention to use the generic
985 * semantic name and the mesa VARYING_SLOT_ number as the
988 * All that is required is that the vertex shader labels
989 * its own outputs similarly, and that the vertex shader
990 * generates at least every output required by the
991 * fragment shader plus fixed-function hardware (such as
994 * However, some drivers may need us to identify the PNTC and TEXi
995 * varyings if, for example, their capability to replace them with
996 * sprite coordinates is limited.
998 case VARYING_SLOT_PNTC
:
999 if (st
->needs_texcoord_semantic
) {
1000 input_semantic_name
[slot
] = TGSI_SEMANTIC_PCOORD
;
1001 input_semantic_index
[slot
] = 0;
1002 interpMode
[slot
] = TGSI_INTERPOLATE_LINEAR
;
1006 case VARYING_SLOT_TEX0
:
1007 case VARYING_SLOT_TEX1
:
1008 case VARYING_SLOT_TEX2
:
1009 case VARYING_SLOT_TEX3
:
1010 case VARYING_SLOT_TEX4
:
1011 case VARYING_SLOT_TEX5
:
1012 case VARYING_SLOT_TEX6
:
1013 case VARYING_SLOT_TEX7
:
1014 if (st
->needs_texcoord_semantic
) {
1015 input_semantic_name
[slot
] = TGSI_SEMANTIC_TEXCOORD
;
1016 input_semantic_index
[slot
] = attr
- VARYING_SLOT_TEX0
;
1017 interpMode
[slot
] = stfp
->glsl_to_tgsi
?
1018 TGSI_INTERPOLATE_COUNT
: TGSI_INTERPOLATE_PERSPECTIVE
;
1022 case VARYING_SLOT_VAR0
:
1024 /* Semantic indices should be zero-based because drivers may choose
1025 * to assign a fixed slot determined by that index.
1026 * This is useful because ARB_separate_shader_objects uses location
1027 * qualifiers for linkage, and if the semantic index corresponds to
1028 * these locations, linkage passes in the driver become unecessary.
1030 * If needs_texcoord_semantic is true, no semantic indices will be
1031 * consumed for the TEXi varyings, and we can base the locations of
1032 * the user varyings on VAR0. Otherwise, we use TEX0 as base index.
1034 assert(attr
>= VARYING_SLOT_VAR0
|| attr
== VARYING_SLOT_PNTC
||
1035 (attr
>= VARYING_SLOT_TEX0
&& attr
<= VARYING_SLOT_TEX7
));
1036 input_semantic_name
[slot
] = TGSI_SEMANTIC_GENERIC
;
1037 input_semantic_index
[slot
] = st_get_generic_varying_index(st
, attr
);
1038 if (attr
== VARYING_SLOT_PNTC
)
1039 interpMode
[slot
] = TGSI_INTERPOLATE_LINEAR
;
1041 interpMode
[slot
] = stfp
->glsl_to_tgsi
?
1042 TGSI_INTERPOLATE_COUNT
: TGSI_INTERPOLATE_PERSPECTIVE
;
1048 inputMapping
[attr
] = -1;
1053 * Semantics and mapping for outputs
1055 GLbitfield64 outputsWritten
= stfp
->Base
.info
.outputs_written
;
1057 /* if z is written, emit that first */
1058 if (outputsWritten
& BITFIELD64_BIT(FRAG_RESULT_DEPTH
)) {
1059 fs_output_semantic_name
[fs_num_outputs
] = TGSI_SEMANTIC_POSITION
;
1060 fs_output_semantic_index
[fs_num_outputs
] = 0;
1061 outputMapping
[FRAG_RESULT_DEPTH
] = fs_num_outputs
;
1063 outputsWritten
&= ~(1 << FRAG_RESULT_DEPTH
);
1066 if (outputsWritten
& BITFIELD64_BIT(FRAG_RESULT_STENCIL
)) {
1067 fs_output_semantic_name
[fs_num_outputs
] = TGSI_SEMANTIC_STENCIL
;
1068 fs_output_semantic_index
[fs_num_outputs
] = 0;
1069 outputMapping
[FRAG_RESULT_STENCIL
] = fs_num_outputs
;
1071 outputsWritten
&= ~(1 << FRAG_RESULT_STENCIL
);
1074 if (outputsWritten
& BITFIELD64_BIT(FRAG_RESULT_SAMPLE_MASK
)) {
1075 fs_output_semantic_name
[fs_num_outputs
] = TGSI_SEMANTIC_SAMPLEMASK
;
1076 fs_output_semantic_index
[fs_num_outputs
] = 0;
1077 outputMapping
[FRAG_RESULT_SAMPLE_MASK
] = fs_num_outputs
;
1079 outputsWritten
&= ~(1 << FRAG_RESULT_SAMPLE_MASK
);
1082 /* handle remaining outputs (color) */
1083 for (attr
= 0; attr
< ARRAY_SIZE(outputMapping
); attr
++) {
1084 const GLbitfield64 written
= attr
< FRAG_RESULT_MAX
? outputsWritten
:
1085 stfp
->Base
.SecondaryOutputsWritten
;
1086 const unsigned loc
= attr
% FRAG_RESULT_MAX
;
1088 if (written
& BITFIELD64_BIT(loc
)) {
1090 case FRAG_RESULT_DEPTH
:
1091 case FRAG_RESULT_STENCIL
:
1092 case FRAG_RESULT_SAMPLE_MASK
:
1096 case FRAG_RESULT_COLOR
:
1097 write_all
= GL_TRUE
; /* fallthrough */
1100 assert(loc
== FRAG_RESULT_COLOR
||
1101 (FRAG_RESULT_DATA0
<= loc
&& loc
< FRAG_RESULT_MAX
));
1103 index
= (loc
== FRAG_RESULT_COLOR
) ? 0 : (loc
- FRAG_RESULT_DATA0
);
1105 if (attr
>= FRAG_RESULT_MAX
) {
1106 /* Secondary color for dual source blending. */
1111 fs_output_semantic_name
[fs_num_outputs
] = TGSI_SEMANTIC_COLOR
;
1112 fs_output_semantic_index
[fs_num_outputs
] = index
;
1113 outputMapping
[attr
] = fs_num_outputs
;
1122 ureg
= ureg_create_with_screen(PIPE_SHADER_FRAGMENT
, st
->pipe
->screen
);
1126 if (ST_DEBUG
& DEBUG_MESA
) {
1127 _mesa_print_program(&stfp
->Base
);
1128 _mesa_print_program_parameters(st
->ctx
, &stfp
->Base
);
1131 if (write_all
== GL_TRUE
)
1132 ureg_property(ureg
, TGSI_PROPERTY_FS_COLOR0_WRITES_ALL_CBUFS
, 1);
1134 if (stfp
->Base
.info
.fs
.depth_layout
!= FRAG_DEPTH_LAYOUT_NONE
) {
1135 switch (stfp
->Base
.info
.fs
.depth_layout
) {
1136 case FRAG_DEPTH_LAYOUT_ANY
:
1137 ureg_property(ureg
, TGSI_PROPERTY_FS_DEPTH_LAYOUT
,
1138 TGSI_FS_DEPTH_LAYOUT_ANY
);
1140 case FRAG_DEPTH_LAYOUT_GREATER
:
1141 ureg_property(ureg
, TGSI_PROPERTY_FS_DEPTH_LAYOUT
,
1142 TGSI_FS_DEPTH_LAYOUT_GREATER
);
1144 case FRAG_DEPTH_LAYOUT_LESS
:
1145 ureg_property(ureg
, TGSI_PROPERTY_FS_DEPTH_LAYOUT
,
1146 TGSI_FS_DEPTH_LAYOUT_LESS
);
1148 case FRAG_DEPTH_LAYOUT_UNCHANGED
:
1149 ureg_property(ureg
, TGSI_PROPERTY_FS_DEPTH_LAYOUT
,
1150 TGSI_FS_DEPTH_LAYOUT_UNCHANGED
);
1157 if (stfp
->glsl_to_tgsi
) {
1158 st_translate_program(st
->ctx
,
1159 PIPE_SHADER_FRAGMENT
,
1167 input_semantic_name
,
1168 input_semantic_index
,
1173 fs_output_semantic_name
,
1174 fs_output_semantic_index
);
1176 free_glsl_to_tgsi_visitor(stfp
->glsl_to_tgsi
);
1177 } else if (stfp
->ati_fs
)
1178 st_translate_atifs_program(ureg
,
1184 input_semantic_name
,
1185 input_semantic_index
,
1190 fs_output_semantic_name
,
1191 fs_output_semantic_index
);
1193 st_translate_mesa_program(st
->ctx
,
1194 PIPE_SHADER_FRAGMENT
,
1200 input_semantic_name
,
1201 input_semantic_index
,
1206 fs_output_semantic_name
,
1207 fs_output_semantic_index
);
1209 stfp
->state
.tokens
= ureg_get_tokens(ureg
, NULL
);
1212 if (stfp
->glsl_to_tgsi
) {
1213 stfp
->glsl_to_tgsi
= NULL
;
1214 st_store_ir_in_disk_cache(st
, &stfp
->Base
, false);
1217 return stfp
->state
.tokens
!= NULL
;
1220 static struct st_fp_variant
*
1221 st_create_fp_variant(struct st_context
*st
,
1222 struct st_common_program
*stfp
,
1223 const struct st_fp_variant_key
*key
)
1225 struct pipe_context
*pipe
= st
->pipe
;
1226 struct st_fp_variant
*variant
= CALLOC_STRUCT(st_fp_variant
);
1227 struct pipe_shader_state state
= {0};
1228 struct gl_program_parameter_list
*params
= stfp
->Base
.Parameters
;
1229 static const gl_state_index16 texcoord_state
[STATE_LENGTH
] =
1230 { STATE_INTERNAL
, STATE_CURRENT_ATTRIB
, VERT_ATTRIB_TEX0
};
1231 static const gl_state_index16 scale_state
[STATE_LENGTH
] =
1232 { STATE_INTERNAL
, STATE_PT_SCALE
};
1233 static const gl_state_index16 bias_state
[STATE_LENGTH
] =
1234 { STATE_INTERNAL
, STATE_PT_BIAS
};
1235 static const gl_state_index16 alpha_ref_state
[STATE_LENGTH
] =
1236 { STATE_INTERNAL
, STATE_ALPHA_REF
};
1241 if (stfp
->state
.type
== PIPE_SHADER_IR_NIR
) {
1242 bool finalize
= false;
1244 state
.type
= PIPE_SHADER_IR_NIR
;
1245 state
.ir
.nir
= nir_shader_clone(NULL
, stfp
->state
.ir
.nir
);
1247 if (key
->clamp_color
) {
1248 NIR_PASS_V(state
.ir
.nir
, nir_lower_clamp_color_outputs
);
1252 if (key
->lower_flatshade
) {
1253 NIR_PASS_V(state
.ir
.nir
, nir_lower_flatshade
);
1257 if (key
->lower_alpha_func
!= COMPARE_FUNC_NEVER
) {
1258 _mesa_add_state_reference(params
, alpha_ref_state
);
1259 NIR_PASS_V(state
.ir
.nir
, nir_lower_alpha_test
, key
->lower_alpha_func
,
1260 false, alpha_ref_state
);
1264 if (key
->lower_two_sided_color
) {
1265 NIR_PASS_V(state
.ir
.nir
, nir_lower_two_sided_color
);
1269 if (key
->persample_shading
) {
1270 nir_shader
*shader
= state
.ir
.nir
;
1271 nir_foreach_variable(var
, &shader
->inputs
)
1272 var
->data
.sample
= true;
1276 assert(!(key
->bitmap
&& key
->drawpixels
));
1280 nir_lower_bitmap_options options
= {0};
1282 variant
->bitmap_sampler
= ffs(~stfp
->Base
.SamplersUsed
) - 1;
1283 options
.sampler
= variant
->bitmap_sampler
;
1284 options
.swizzle_xxxx
= st
->bitmap
.tex_format
== PIPE_FORMAT_R8_UNORM
;
1286 NIR_PASS_V(state
.ir
.nir
, nir_lower_bitmap
, &options
);
1290 /* glDrawPixels (color only) */
1291 if (key
->drawpixels
) {
1292 nir_lower_drawpixels_options options
= {{0}};
1293 unsigned samplers_used
= stfp
->Base
.SamplersUsed
;
1295 /* Find the first unused slot. */
1296 variant
->drawpix_sampler
= ffs(~samplers_used
) - 1;
1297 options
.drawpix_sampler
= variant
->drawpix_sampler
;
1298 samplers_used
|= (1 << variant
->drawpix_sampler
);
1300 options
.pixel_maps
= key
->pixelMaps
;
1301 if (key
->pixelMaps
) {
1302 variant
->pixelmap_sampler
= ffs(~samplers_used
) - 1;
1303 options
.pixelmap_sampler
= variant
->pixelmap_sampler
;
1306 options
.scale_and_bias
= key
->scaleAndBias
;
1307 if (key
->scaleAndBias
) {
1308 _mesa_add_state_reference(params
, scale_state
);
1309 memcpy(options
.scale_state_tokens
, scale_state
,
1310 sizeof(options
.scale_state_tokens
));
1311 _mesa_add_state_reference(params
, bias_state
);
1312 memcpy(options
.bias_state_tokens
, bias_state
,
1313 sizeof(options
.bias_state_tokens
));
1316 _mesa_add_state_reference(params
, texcoord_state
);
1317 memcpy(options
.texcoord_state_tokens
, texcoord_state
,
1318 sizeof(options
.texcoord_state_tokens
));
1320 NIR_PASS_V(state
.ir
.nir
, nir_lower_drawpixels
, &options
);
1324 if (unlikely(key
->external
.lower_nv12
|| key
->external
.lower_iyuv
||
1325 key
->external
.lower_xy_uxvx
|| key
->external
.lower_yx_xuxv
||
1326 key
->external
.lower_ayuv
|| key
->external
.lower_xyuv
)) {
1327 nir_lower_tex_options options
= {0};
1328 options
.lower_y_uv_external
= key
->external
.lower_nv12
;
1329 options
.lower_y_u_v_external
= key
->external
.lower_iyuv
;
1330 options
.lower_xy_uxvx_external
= key
->external
.lower_xy_uxvx
;
1331 options
.lower_yx_xuxv_external
= key
->external
.lower_yx_xuxv
;
1332 options
.lower_ayuv_external
= key
->external
.lower_ayuv
;
1333 options
.lower_xyuv_external
= key
->external
.lower_xyuv
;
1334 NIR_PASS_V(state
.ir
.nir
, nir_lower_tex
, &options
);
1338 if (finalize
|| !st
->allow_st_finalize_nir_twice
) {
1339 st_finalize_nir(st
, &stfp
->Base
, stfp
->shader_program
, state
.ir
.nir
,
1343 /* This pass needs to happen *after* nir_lower_sampler */
1344 if (unlikely(key
->external
.lower_nv12
|| key
->external
.lower_iyuv
||
1345 key
->external
.lower_xy_uxvx
|| key
->external
.lower_yx_xuxv
)) {
1346 NIR_PASS_V(state
.ir
.nir
, st_nir_lower_tex_src_plane
,
1347 ~stfp
->Base
.SamplersUsed
,
1348 key
->external
.lower_nv12
|| key
->external
.lower_xy_uxvx
||
1349 key
->external
.lower_yx_xuxv
,
1350 key
->external
.lower_iyuv
);
1354 if (finalize
|| !st
->allow_st_finalize_nir_twice
) {
1355 /* Some of the lowering above may have introduced new varyings */
1356 nir_shader_gather_info(state
.ir
.nir
,
1357 nir_shader_get_entrypoint(state
.ir
.nir
));
1359 struct pipe_screen
*screen
= pipe
->screen
;
1360 if (screen
->finalize_nir
)
1361 screen
->finalize_nir(screen
, state
.ir
.nir
, false);
1364 variant
->driver_shader
= pipe
->create_fs_state(pipe
, &state
);
1365 variant
->key
= *key
;
1370 state
.tokens
= stfp
->state
.tokens
;
1372 assert(!(key
->bitmap
&& key
->drawpixels
));
1374 /* Fix texture targets and add fog for ATI_fs */
1376 const struct tgsi_token
*tokens
= st_fixup_atifs(state
.tokens
, key
);
1379 state
.tokens
= tokens
;
1381 fprintf(stderr
, "mesa: cannot post-process ATI_fs\n");
1384 /* Emulate features. */
1385 if (key
->clamp_color
|| key
->persample_shading
) {
1386 const struct tgsi_token
*tokens
;
1388 (key
->clamp_color
? TGSI_EMU_CLAMP_COLOR_OUTPUTS
: 0) |
1389 (key
->persample_shading
? TGSI_EMU_FORCE_PERSAMPLE_INTERP
: 0);
1391 tokens
= tgsi_emulate(state
.tokens
, flags
);
1394 if (state
.tokens
!= stfp
->state
.tokens
)
1395 tgsi_free_tokens(state
.tokens
);
1396 state
.tokens
= tokens
;
1398 fprintf(stderr
, "mesa: cannot emulate deprecated features\n");
1403 const struct tgsi_token
*tokens
;
1405 variant
->bitmap_sampler
= ffs(~stfp
->Base
.SamplersUsed
) - 1;
1407 tokens
= st_get_bitmap_shader(state
.tokens
,
1408 st
->internal_target
,
1409 variant
->bitmap_sampler
,
1410 st
->needs_texcoord_semantic
,
1411 st
->bitmap
.tex_format
==
1412 PIPE_FORMAT_R8_UNORM
);
1415 if (state
.tokens
!= stfp
->state
.tokens
)
1416 tgsi_free_tokens(state
.tokens
);
1417 state
.tokens
= tokens
;
1419 fprintf(stderr
, "mesa: cannot create a shader for glBitmap\n");
1422 /* glDrawPixels (color only) */
1423 if (key
->drawpixels
) {
1424 const struct tgsi_token
*tokens
;
1425 unsigned scale_const
= 0, bias_const
= 0, texcoord_const
= 0;
1427 /* Find the first unused slot. */
1428 variant
->drawpix_sampler
= ffs(~stfp
->Base
.SamplersUsed
) - 1;
1430 if (key
->pixelMaps
) {
1431 unsigned samplers_used
= stfp
->Base
.SamplersUsed
|
1432 (1 << variant
->drawpix_sampler
);
1434 variant
->pixelmap_sampler
= ffs(~samplers_used
) - 1;
1437 if (key
->scaleAndBias
) {
1438 scale_const
= _mesa_add_state_reference(params
, scale_state
);
1439 bias_const
= _mesa_add_state_reference(params
, bias_state
);
1442 texcoord_const
= _mesa_add_state_reference(params
, texcoord_state
);
1444 tokens
= st_get_drawpix_shader(state
.tokens
,
1445 st
->needs_texcoord_semantic
,
1446 key
->scaleAndBias
, scale_const
,
1447 bias_const
, key
->pixelMaps
,
1448 variant
->drawpix_sampler
,
1449 variant
->pixelmap_sampler
,
1450 texcoord_const
, st
->internal_target
);
1453 if (state
.tokens
!= stfp
->state
.tokens
)
1454 tgsi_free_tokens(state
.tokens
);
1455 state
.tokens
= tokens
;
1457 fprintf(stderr
, "mesa: cannot create a shader for glDrawPixels\n");
1460 if (unlikely(key
->external
.lower_nv12
|| key
->external
.lower_iyuv
||
1461 key
->external
.lower_xy_uxvx
|| key
->external
.lower_yx_xuxv
)) {
1462 const struct tgsi_token
*tokens
;
1464 /* samplers inserted would conflict, but this should be unpossible: */
1465 assert(!(key
->bitmap
|| key
->drawpixels
));
1467 tokens
= st_tgsi_lower_yuv(state
.tokens
,
1468 ~stfp
->Base
.SamplersUsed
,
1469 key
->external
.lower_nv12
||
1470 key
->external
.lower_xy_uxvx
||
1471 key
->external
.lower_yx_xuxv
,
1472 key
->external
.lower_iyuv
);
1474 if (state
.tokens
!= stfp
->state
.tokens
)
1475 tgsi_free_tokens(state
.tokens
);
1476 state
.tokens
= tokens
;
1478 fprintf(stderr
, "mesa: cannot create a shader for samplerExternalOES\n");
1482 if (key
->lower_depth_clamp
) {
1483 unsigned depth_range_const
= _mesa_add_state_reference(params
, depth_range_state
);
1485 const struct tgsi_token
*tokens
;
1486 tokens
= st_tgsi_lower_depth_clamp_fs(state
.tokens
, depth_range_const
);
1487 if (state
.tokens
!= stfp
->state
.tokens
)
1488 tgsi_free_tokens(state
.tokens
);
1489 state
.tokens
= tokens
;
1492 if (ST_DEBUG
& DEBUG_TGSI
) {
1493 tgsi_dump(state
.tokens
, 0);
1497 /* fill in variant */
1498 variant
->driver_shader
= pipe
->create_fs_state(pipe
, &state
);
1499 variant
->key
= *key
;
1501 if (state
.tokens
!= stfp
->state
.tokens
)
1502 tgsi_free_tokens(state
.tokens
);
1507 * Translate fragment program if needed.
1509 struct st_fp_variant
*
1510 st_get_fp_variant(struct st_context
*st
,
1511 struct st_common_program
*stfp
,
1512 const struct st_fp_variant_key
*key
)
1514 struct st_fp_variant
*fpv
;
1516 /* Search for existing variant */
1517 for (fpv
= stfp
->fp_variants
; fpv
; fpv
= fpv
->next
) {
1518 if (memcmp(&fpv
->key
, key
, sizeof(*key
)) == 0) {
1525 fpv
= st_create_fp_variant(st
, stfp
, key
);
1527 if (key
->bitmap
|| key
->drawpixels
) {
1528 /* Regular variants should always come before the
1529 * bitmap & drawpixels variants, (unless there
1530 * are no regular variants) so that
1531 * st_update_fp can take a fast path when
1532 * shader_has_one_variant is set.
1534 if (!stfp
->fp_variants
) {
1535 stfp
->fp_variants
= fpv
;
1537 /* insert into list after the first one */
1538 fpv
->next
= stfp
->fp_variants
->next
;
1539 stfp
->fp_variants
->next
= fpv
;
1542 /* insert into list */
1543 fpv
->next
= stfp
->fp_variants
;
1544 stfp
->fp_variants
= fpv
;
1553 * Translate a program. This is common code for geometry and tessellation
1557 st_translate_common_program(struct st_context
*st
,
1558 struct st_common_program
*stcp
)
1560 struct gl_program
*prog
= &stcp
->Base
;
1561 enum pipe_shader_type stage
=
1562 pipe_shader_type_from_mesa(stcp
->Base
.info
.stage
);
1563 struct ureg_program
*ureg
= ureg_create_with_screen(stage
, st
->pipe
->screen
);
1569 case PIPE_SHADER_TESS_CTRL
:
1570 ureg_property(ureg
, TGSI_PROPERTY_TCS_VERTICES_OUT
,
1571 stcp
->Base
.info
.tess
.tcs_vertices_out
);
1574 case PIPE_SHADER_TESS_EVAL
:
1575 if (stcp
->Base
.info
.tess
.primitive_mode
== GL_ISOLINES
)
1576 ureg_property(ureg
, TGSI_PROPERTY_TES_PRIM_MODE
, GL_LINES
);
1578 ureg_property(ureg
, TGSI_PROPERTY_TES_PRIM_MODE
,
1579 stcp
->Base
.info
.tess
.primitive_mode
);
1581 STATIC_ASSERT((TESS_SPACING_EQUAL
+ 1) % 3 == PIPE_TESS_SPACING_EQUAL
);
1582 STATIC_ASSERT((TESS_SPACING_FRACTIONAL_ODD
+ 1) % 3 ==
1583 PIPE_TESS_SPACING_FRACTIONAL_ODD
);
1584 STATIC_ASSERT((TESS_SPACING_FRACTIONAL_EVEN
+ 1) % 3 ==
1585 PIPE_TESS_SPACING_FRACTIONAL_EVEN
);
1587 ureg_property(ureg
, TGSI_PROPERTY_TES_SPACING
,
1588 (stcp
->Base
.info
.tess
.spacing
+ 1) % 3);
1590 ureg_property(ureg
, TGSI_PROPERTY_TES_VERTEX_ORDER_CW
,
1591 !stcp
->Base
.info
.tess
.ccw
);
1592 ureg_property(ureg
, TGSI_PROPERTY_TES_POINT_MODE
,
1593 stcp
->Base
.info
.tess
.point_mode
);
1596 case PIPE_SHADER_GEOMETRY
:
1597 ureg_property(ureg
, TGSI_PROPERTY_GS_INPUT_PRIM
,
1598 stcp
->Base
.info
.gs
.input_primitive
);
1599 ureg_property(ureg
, TGSI_PROPERTY_GS_OUTPUT_PRIM
,
1600 stcp
->Base
.info
.gs
.output_primitive
);
1601 ureg_property(ureg
, TGSI_PROPERTY_GS_MAX_OUTPUT_VERTICES
,
1602 stcp
->Base
.info
.gs
.vertices_out
);
1603 ureg_property(ureg
, TGSI_PROPERTY_GS_INVOCATIONS
,
1604 stcp
->Base
.info
.gs
.invocations
);
1611 ubyte inputSlotToAttr
[VARYING_SLOT_TESS_MAX
];
1612 ubyte inputMapping
[VARYING_SLOT_TESS_MAX
];
1613 ubyte outputMapping
[VARYING_SLOT_TESS_MAX
];
1616 ubyte input_semantic_name
[PIPE_MAX_SHADER_INPUTS
];
1617 ubyte input_semantic_index
[PIPE_MAX_SHADER_INPUTS
];
1618 uint num_inputs
= 0;
1620 ubyte output_semantic_name
[PIPE_MAX_SHADER_OUTPUTS
];
1621 ubyte output_semantic_index
[PIPE_MAX_SHADER_OUTPUTS
];
1622 uint num_outputs
= 0;
1626 memset(inputSlotToAttr
, 0, sizeof(inputSlotToAttr
));
1627 memset(inputMapping
, 0, sizeof(inputMapping
));
1628 memset(outputMapping
, 0, sizeof(outputMapping
));
1629 memset(&stcp
->state
, 0, sizeof(stcp
->state
));
1631 if (prog
->info
.clip_distance_array_size
)
1632 ureg_property(ureg
, TGSI_PROPERTY_NUM_CLIPDIST_ENABLED
,
1633 prog
->info
.clip_distance_array_size
);
1634 if (prog
->info
.cull_distance_array_size
)
1635 ureg_property(ureg
, TGSI_PROPERTY_NUM_CULLDIST_ENABLED
,
1636 prog
->info
.cull_distance_array_size
);
1639 * Convert Mesa program inputs to TGSI input register semantics.
1641 for (attr
= 0; attr
< VARYING_SLOT_MAX
; attr
++) {
1642 if ((prog
->info
.inputs_read
& BITFIELD64_BIT(attr
)) == 0)
1645 unsigned slot
= num_inputs
++;
1647 inputMapping
[attr
] = slot
;
1648 inputSlotToAttr
[slot
] = attr
;
1650 unsigned semantic_name
, semantic_index
;
1651 tgsi_get_gl_varying_semantic(attr
, st
->needs_texcoord_semantic
,
1652 &semantic_name
, &semantic_index
);
1653 input_semantic_name
[slot
] = semantic_name
;
1654 input_semantic_index
[slot
] = semantic_index
;
1657 /* Also add patch inputs. */
1658 for (attr
= 0; attr
< 32; attr
++) {
1659 if (prog
->info
.patch_inputs_read
& (1u << attr
)) {
1660 GLuint slot
= num_inputs
++;
1661 GLuint patch_attr
= VARYING_SLOT_PATCH0
+ attr
;
1663 inputMapping
[patch_attr
] = slot
;
1664 inputSlotToAttr
[slot
] = patch_attr
;
1665 input_semantic_name
[slot
] = TGSI_SEMANTIC_PATCH
;
1666 input_semantic_index
[slot
] = attr
;
1670 /* initialize output semantics to defaults */
1671 for (i
= 0; i
< PIPE_MAX_SHADER_OUTPUTS
; i
++) {
1672 output_semantic_name
[i
] = TGSI_SEMANTIC_GENERIC
;
1673 output_semantic_index
[i
] = 0;
1677 * Determine number of outputs, the (default) output register
1678 * mapping and the semantic information for each output.
1680 for (attr
= 0; attr
< VARYING_SLOT_MAX
; attr
++) {
1681 if (prog
->info
.outputs_written
& BITFIELD64_BIT(attr
)) {
1682 GLuint slot
= num_outputs
++;
1684 outputMapping
[attr
] = slot
;
1686 unsigned semantic_name
, semantic_index
;
1687 tgsi_get_gl_varying_semantic(attr
, st
->needs_texcoord_semantic
,
1688 &semantic_name
, &semantic_index
);
1689 output_semantic_name
[slot
] = semantic_name
;
1690 output_semantic_index
[slot
] = semantic_index
;
1694 /* Also add patch outputs. */
1695 for (attr
= 0; attr
< 32; attr
++) {
1696 if (prog
->info
.patch_outputs_written
& (1u << attr
)) {
1697 GLuint slot
= num_outputs
++;
1698 GLuint patch_attr
= VARYING_SLOT_PATCH0
+ attr
;
1700 outputMapping
[patch_attr
] = slot
;
1701 output_semantic_name
[slot
] = TGSI_SEMANTIC_PATCH
;
1702 output_semantic_index
[slot
] = attr
;
1706 st_translate_program(st
->ctx
,
1715 input_semantic_name
,
1716 input_semantic_index
,
1721 output_semantic_name
,
1722 output_semantic_index
);
1724 stcp
->state
.tokens
= ureg_get_tokens(ureg
, NULL
);
1728 st_translate_stream_output_info(prog
);
1730 st_store_ir_in_disk_cache(st
, prog
, false);
1732 if ((ST_DEBUG
& DEBUG_TGSI
) && (ST_DEBUG
& DEBUG_MESA
)) {
1733 _mesa_print_program(prog
);
1737 if (ST_DEBUG
& DEBUG_TGSI
) {
1738 tgsi_dump(stcp
->state
.tokens
, 0);
1742 free_glsl_to_tgsi_visitor(stcp
->glsl_to_tgsi
);
1743 stcp
->glsl_to_tgsi
= NULL
;
1749 * Get/create a basic program variant.
1751 struct st_common_variant
*
1752 st_get_common_variant(struct st_context
*st
,
1753 struct st_common_program
*prog
,
1754 const struct st_common_variant_key
*key
)
1756 struct pipe_context
*pipe
= st
->pipe
;
1757 struct st_common_variant
*v
;
1758 struct pipe_shader_state state
= {0};
1760 /* Search for existing variant */
1761 for (v
= prog
->variants
; v
; v
= v
->next
) {
1762 if (memcmp(&v
->key
, key
, sizeof(*key
)) == 0) {
1769 v
= CALLOC_STRUCT(st_common_variant
);
1771 if (prog
->state
.type
== PIPE_SHADER_IR_NIR
) {
1772 bool finalize
= false;
1774 state
.type
= PIPE_SHADER_IR_NIR
;
1775 state
.ir
.nir
= nir_shader_clone(NULL
, prog
->state
.ir
.nir
);
1777 if (key
->clamp_color
) {
1778 NIR_PASS_V(state
.ir
.nir
, nir_lower_clamp_color_outputs
);
1782 state
.stream_output
= prog
->state
.stream_output
;
1784 if (finalize
|| !st
->allow_st_finalize_nir_twice
) {
1785 st_finalize_nir(st
, &prog
->Base
, prog
->shader_program
,
1786 state
.ir
.nir
, true);
1789 if (key
->lower_depth_clamp
) {
1790 struct gl_program_parameter_list
*params
= prog
->Base
.Parameters
;
1792 unsigned depth_range_const
=
1793 _mesa_add_state_reference(params
, depth_range_state
);
1795 const struct tgsi_token
*tokens
;
1797 st_tgsi_lower_depth_clamp(prog
->state
.tokens
,
1799 key
->clip_negative_one_to_one
);
1801 if (tokens
!= prog
->state
.tokens
)
1802 tgsi_free_tokens(prog
->state
.tokens
);
1804 prog
->state
.tokens
= tokens
;
1806 state
= prog
->state
;
1808 /* fill in new variant */
1809 switch (prog
->Base
.info
.stage
) {
1810 case MESA_SHADER_TESS_CTRL
:
1811 v
->driver_shader
= pipe
->create_tcs_state(pipe
, &state
);
1813 case MESA_SHADER_TESS_EVAL
:
1814 v
->driver_shader
= pipe
->create_tes_state(pipe
, &state
);
1816 case MESA_SHADER_GEOMETRY
:
1817 v
->driver_shader
= pipe
->create_gs_state(pipe
, &state
);
1819 case MESA_SHADER_COMPUTE
: {
1820 struct pipe_compute_state cs
= {0};
1821 cs
.ir_type
= state
.type
;
1822 cs
.req_local_mem
= prog
->Base
.info
.cs
.shared_size
;
1824 if (state
.type
== PIPE_SHADER_IR_NIR
)
1825 cs
.prog
= state
.ir
.nir
;
1827 cs
.prog
= state
.tokens
;
1829 v
->driver_shader
= pipe
->create_compute_state(pipe
, &cs
);
1833 assert(!"unhandled shader type");
1840 /* insert into list */
1841 v
->next
= prog
->variants
;
1851 * Vert/Geom/Frag programs have per-context variants. Free all the
1852 * variants attached to the given program which match the given context.
1855 destroy_program_variants(struct st_context
*st
, struct gl_program
*target
)
1857 if (!target
|| target
== &_mesa_DummyProgram
)
1860 switch (target
->Target
) {
1861 case GL_VERTEX_PROGRAM_ARB
:
1863 struct st_vertex_program
*stvp
= (struct st_vertex_program
*) target
;
1864 struct st_vp_variant
*vpv
, **prevPtr
= &stvp
->variants
;
1866 for (vpv
= stvp
->variants
; vpv
; ) {
1867 struct st_vp_variant
*next
= vpv
->next
;
1868 if (vpv
->key
.st
== st
) {
1869 /* unlink from list */
1871 /* destroy this variant */
1872 delete_vp_variant(st
, vpv
);
1875 prevPtr
= &vpv
->next
;
1881 case GL_FRAGMENT_PROGRAM_ARB
:
1883 struct st_common_program
*stfp
=
1884 (struct st_common_program
*) target
;
1885 struct st_fp_variant
*fpv
, **prevPtr
= &stfp
->fp_variants
;
1887 for (fpv
= stfp
->fp_variants
; fpv
; ) {
1888 struct st_fp_variant
*next
= fpv
->next
;
1889 if (fpv
->key
.st
== st
) {
1890 /* unlink from list */
1892 /* destroy this variant */
1893 delete_fp_variant(st
, fpv
);
1896 prevPtr
= &fpv
->next
;
1902 case GL_GEOMETRY_PROGRAM_NV
:
1903 case GL_TESS_CONTROL_PROGRAM_NV
:
1904 case GL_TESS_EVALUATION_PROGRAM_NV
:
1905 case GL_COMPUTE_PROGRAM_NV
:
1907 struct st_common_program
*p
= st_common_program(target
);
1908 struct st_common_variant
*v
, **prevPtr
= &p
->variants
;
1910 for (v
= p
->variants
; v
; ) {
1911 struct st_common_variant
*next
= v
->next
;
1912 if (v
->key
.st
== st
) {
1913 /* unlink from list */
1915 /* destroy this variant */
1916 delete_basic_variant(st
, v
, target
->Target
);
1926 _mesa_problem(NULL
, "Unexpected program target 0x%x in "
1927 "destroy_program_variants_cb()", target
->Target
);
1933 * Callback for _mesa_HashWalk. Free all the shader's program variants
1934 * which match the given context.
1937 destroy_shader_program_variants_cb(GLuint key
, void *data
, void *userData
)
1939 struct st_context
*st
= (struct st_context
*) userData
;
1940 struct gl_shader
*shader
= (struct gl_shader
*) data
;
1942 switch (shader
->Type
) {
1943 case GL_SHADER_PROGRAM_MESA
:
1945 struct gl_shader_program
*shProg
= (struct gl_shader_program
*) data
;
1948 for (i
= 0; i
< ARRAY_SIZE(shProg
->_LinkedShaders
); i
++) {
1949 if (shProg
->_LinkedShaders
[i
])
1950 destroy_program_variants(st
, shProg
->_LinkedShaders
[i
]->Program
);
1954 case GL_VERTEX_SHADER
:
1955 case GL_FRAGMENT_SHADER
:
1956 case GL_GEOMETRY_SHADER
:
1957 case GL_TESS_CONTROL_SHADER
:
1958 case GL_TESS_EVALUATION_SHADER
:
1959 case GL_COMPUTE_SHADER
:
1968 * Callback for _mesa_HashWalk. Free all the program variants which match
1969 * the given context.
1972 destroy_program_variants_cb(GLuint key
, void *data
, void *userData
)
1974 struct st_context
*st
= (struct st_context
*) userData
;
1975 struct gl_program
*program
= (struct gl_program
*) data
;
1976 destroy_program_variants(st
, program
);
1981 * Walk over all shaders and programs to delete any variants which
1982 * belong to the given context.
1983 * This is called during context tear-down.
1986 st_destroy_program_variants(struct st_context
*st
)
1988 /* If shaders can be shared with other contexts, the last context will
1989 * call DeleteProgram on all shaders, releasing everything.
1991 if (st
->has_shareable_shaders
)
1994 /* ARB vert/frag program */
1995 _mesa_HashWalk(st
->ctx
->Shared
->Programs
,
1996 destroy_program_variants_cb
, st
);
1998 /* GLSL vert/frag/geom shaders */
1999 _mesa_HashWalk(st
->ctx
->Shared
->ShaderObjects
,
2000 destroy_shader_program_variants_cb
, st
);
2005 * For debugging, print/dump the current vertex program.
2008 st_print_current_vertex_program(void)
2010 GET_CURRENT_CONTEXT(ctx
);
2012 if (ctx
->VertexProgram
._Current
) {
2013 struct st_vertex_program
*stvp
=
2014 (struct st_vertex_program
*) ctx
->VertexProgram
._Current
;
2015 struct st_vp_variant
*stv
;
2017 debug_printf("Vertex program %u\n", stvp
->Base
.Id
);
2019 for (stv
= stvp
->variants
; stv
; stv
= stv
->next
) {
2020 debug_printf("variant %p\n", stv
);
2021 tgsi_dump(stv
->tokens
, 0);
2028 * Compile one shader variant.
2031 st_precompile_shader_variant(struct st_context
*st
,
2032 struct gl_program
*prog
)
2034 switch (prog
->Target
) {
2035 case GL_VERTEX_PROGRAM_ARB
: {
2036 struct st_vertex_program
*p
= (struct st_vertex_program
*)prog
;
2037 struct st_common_variant_key key
;
2039 memset(&key
, 0, sizeof(key
));
2041 key
.st
= st
->has_shareable_shaders
? NULL
: st
;
2042 st_get_vp_variant(st
, p
, &key
);
2046 case GL_FRAGMENT_PROGRAM_ARB
: {
2047 struct st_common_program
*p
= (struct st_common_program
*)prog
;
2048 struct st_fp_variant_key key
;
2050 memset(&key
, 0, sizeof(key
));
2052 key
.st
= st
->has_shareable_shaders
? NULL
: st
;
2053 st_get_fp_variant(st
, p
, &key
);
2057 case GL_TESS_CONTROL_PROGRAM_NV
:
2058 case GL_TESS_EVALUATION_PROGRAM_NV
:
2059 case GL_GEOMETRY_PROGRAM_NV
:
2060 case GL_COMPUTE_PROGRAM_NV
: {
2061 struct st_common_program
*p
= st_common_program(prog
);
2062 struct st_common_variant_key key
;
2064 memset(&key
, 0, sizeof(key
));
2066 key
.st
= st
->has_shareable_shaders
? NULL
: st
;
2067 st_get_common_variant(st
, p
, &key
);