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_split_var_copies
);
394 NIR_PASS_V(nir
, nir_lower_var_copies
);
395 if (nir
->options
->lower_all_io_to_temps
||
396 nir
->options
->lower_all_io_to_elements
||
397 nir
->info
.stage
== MESA_SHADER_VERTEX
||
398 nir
->info
.stage
== MESA_SHADER_GEOMETRY
) {
399 NIR_PASS_V(nir
, nir_lower_io_arrays_to_elements_no_indirects
, false);
400 } else if (nir
->info
.stage
== MESA_SHADER_FRAGMENT
) {
401 NIR_PASS_V(nir
, nir_lower_io_arrays_to_elements_no_indirects
, true);
404 st_nir_assign_vs_in_locations(nir
);
408 * Translate ARB (asm) program to NIR
411 st_translate_prog_to_nir(struct st_context
*st
, struct gl_program
*prog
,
412 gl_shader_stage stage
)
414 const struct gl_shader_compiler_options
*options
=
415 &st
->ctx
->Const
.ShaderCompilerOptions
[stage
];
417 /* Translate to NIR */
418 nir_shader
*nir
= prog_to_nir(prog
, options
->NirOptions
);
419 NIR_PASS_V(nir
, nir_lower_regs_to_ssa
); /* turn registers into SSA */
420 nir_validate_shader(nir
, "after st/ptn lower_regs_to_ssa");
422 NIR_PASS_V(nir
, st_nir_lower_wpos_ytransform
, prog
, st
->pipe
->screen
);
423 NIR_PASS_V(nir
, nir_lower_system_values
);
426 NIR_PASS_V(nir
, nir_opt_constant_folding
);
428 st_finalize_nir_before_variants(nir
);
429 nir_validate_shader(nir
, "after st/ptn NIR opts");
435 st_prepare_vertex_program(struct st_vertex_program
*stvp
)
437 stvp
->num_inputs
= 0;
438 memset(stvp
->input_to_index
, ~0, sizeof(stvp
->input_to_index
));
439 memset(stvp
->result_to_output
, ~0, sizeof(stvp
->result_to_output
));
441 /* Determine number of inputs, the mappings between VERT_ATTRIB_x
442 * and TGSI generic input indexes, plus input attrib semantic info.
444 for (unsigned attr
= 0; attr
< VERT_ATTRIB_MAX
; attr
++) {
445 if ((stvp
->Base
.info
.inputs_read
& BITFIELD64_BIT(attr
)) != 0) {
446 stvp
->input_to_index
[attr
] = stvp
->num_inputs
;
447 stvp
->index_to_input
[stvp
->num_inputs
] = attr
;
450 if ((stvp
->Base
.DualSlotInputs
& BITFIELD64_BIT(attr
)) != 0) {
451 /* add placeholder for second part of a double attribute */
452 stvp
->index_to_input
[stvp
->num_inputs
] = ST_DOUBLE_ATTRIB_PLACEHOLDER
;
457 /* pre-setup potentially unused edgeflag input */
458 stvp
->input_to_index
[VERT_ATTRIB_EDGEFLAG
] = stvp
->num_inputs
;
459 stvp
->index_to_input
[stvp
->num_inputs
] = VERT_ATTRIB_EDGEFLAG
;
461 /* Compute mapping of vertex program outputs to slots. */
462 unsigned num_outputs
= 0;
463 for (unsigned attr
= 0; attr
< VARYING_SLOT_MAX
; attr
++) {
464 if (stvp
->Base
.info
.outputs_written
& BITFIELD64_BIT(attr
))
465 stvp
->result_to_output
[attr
] = num_outputs
++;
467 /* pre-setup potentially unused edgeflag output */
468 stvp
->result_to_output
[VARYING_SLOT_EDGE
] = num_outputs
;
472 st_translate_stream_output_info(struct gl_program
*prog
)
474 struct gl_transform_feedback_info
*info
= prog
->sh
.LinkedTransformFeedback
;
478 /* Determine the (default) output register mapping for each output. */
479 unsigned num_outputs
= 0;
480 ubyte output_mapping
[VARYING_SLOT_TESS_MAX
];
481 memset(output_mapping
, 0, sizeof(output_mapping
));
483 for (unsigned attr
= 0; attr
< VARYING_SLOT_MAX
; attr
++) {
484 if (prog
->info
.outputs_written
& BITFIELD64_BIT(attr
))
485 output_mapping
[attr
] = num_outputs
++;
488 /* Translate stream output info. */
489 struct pipe_stream_output_info
*so_info
= NULL
;
490 if (prog
->info
.stage
== MESA_SHADER_VERTEX
)
491 so_info
= &((struct st_vertex_program
*)prog
)->state
.stream_output
;
493 so_info
= &((struct st_common_program
*)prog
)->state
.stream_output
;
495 for (unsigned i
= 0; i
< info
->NumOutputs
; i
++) {
496 so_info
->output
[i
].register_index
=
497 output_mapping
[info
->Outputs
[i
].OutputRegister
];
498 so_info
->output
[i
].start_component
= info
->Outputs
[i
].ComponentOffset
;
499 so_info
->output
[i
].num_components
= info
->Outputs
[i
].NumComponents
;
500 so_info
->output
[i
].output_buffer
= info
->Outputs
[i
].OutputBuffer
;
501 so_info
->output
[i
].dst_offset
= info
->Outputs
[i
].DstOffset
;
502 so_info
->output
[i
].stream
= info
->Outputs
[i
].StreamId
;
505 for (unsigned i
= 0; i
< PIPE_MAX_SO_BUFFERS
; i
++) {
506 so_info
->stride
[i
] = info
->Buffers
[i
].Stride
;
508 so_info
->num_outputs
= info
->NumOutputs
;
512 * Translate a vertex program.
515 st_translate_vertex_program(struct st_context
*st
,
516 struct st_vertex_program
*stvp
)
518 struct ureg_program
*ureg
;
519 enum pipe_error error
;
520 unsigned num_outputs
= 0;
522 ubyte output_semantic_name
[VARYING_SLOT_MAX
] = {0};
523 ubyte output_semantic_index
[VARYING_SLOT_MAX
] = {0};
525 if (stvp
->Base
.arb
.IsPositionInvariant
)
526 _mesa_insert_mvp_code(st
->ctx
, &stvp
->Base
);
528 st_prepare_vertex_program(stvp
);
531 if (!stvp
->glsl_to_tgsi
) {
532 _mesa_remove_output_reads(&stvp
->Base
, PROGRAM_OUTPUT
);
534 /* This determines which states will be updated when the assembly
537 stvp
->affected_states
= ST_NEW_VS_STATE
|
539 ST_NEW_VERTEX_ARRAYS
;
541 if (stvp
->Base
.Parameters
->NumParameters
)
542 stvp
->affected_states
|= ST_NEW_VS_CONSTANTS
;
544 /* No samplers are allowed in ARB_vp. */
547 /* Get semantic names and indices. */
548 for (attr
= 0; attr
< VARYING_SLOT_MAX
; attr
++) {
549 if (stvp
->Base
.info
.outputs_written
& BITFIELD64_BIT(attr
)) {
550 unsigned slot
= num_outputs
++;
551 unsigned semantic_name
, semantic_index
;
552 tgsi_get_gl_varying_semantic(attr
, st
->needs_texcoord_semantic
,
553 &semantic_name
, &semantic_index
);
554 output_semantic_name
[slot
] = semantic_name
;
555 output_semantic_index
[slot
] = semantic_index
;
558 /* pre-setup potentially unused edgeflag output */
559 output_semantic_name
[num_outputs
] = TGSI_SEMANTIC_EDGEFLAG
;
560 output_semantic_index
[num_outputs
] = 0;
562 ureg
= ureg_create_with_screen(PIPE_SHADER_VERTEX
, st
->pipe
->screen
);
566 if (stvp
->Base
.info
.clip_distance_array_size
)
567 ureg_property(ureg
, TGSI_PROPERTY_NUM_CLIPDIST_ENABLED
,
568 stvp
->Base
.info
.clip_distance_array_size
);
569 if (stvp
->Base
.info
.cull_distance_array_size
)
570 ureg_property(ureg
, TGSI_PROPERTY_NUM_CULLDIST_ENABLED
,
571 stvp
->Base
.info
.cull_distance_array_size
);
573 if (ST_DEBUG
& DEBUG_MESA
) {
574 _mesa_print_program(&stvp
->Base
);
575 _mesa_print_program_parameters(st
->ctx
, &stvp
->Base
);
579 if (stvp
->glsl_to_tgsi
) {
580 error
= st_translate_program(st
->ctx
,
587 stvp
->input_to_index
,
588 NULL
, /* inputSlotToAttr */
589 NULL
, /* input semantic name */
590 NULL
, /* input semantic index */
591 NULL
, /* interp mode */
594 stvp
->result_to_output
,
595 output_semantic_name
,
596 output_semantic_index
);
598 st_translate_stream_output_info(&stvp
->Base
);
600 free_glsl_to_tgsi_visitor(stvp
->glsl_to_tgsi
);
602 error
= st_translate_mesa_program(st
->ctx
,
608 stvp
->input_to_index
,
609 NULL
, /* input semantic name */
610 NULL
, /* input semantic index */
614 stvp
->result_to_output
,
615 output_semantic_name
,
616 output_semantic_index
);
619 debug_printf("%s: failed to translate Mesa program:\n", __func__
);
620 _mesa_print_program(&stvp
->Base
);
625 stvp
->state
.tokens
= ureg_get_tokens(ureg
, NULL
);
628 if (stvp
->glsl_to_tgsi
) {
629 stvp
->glsl_to_tgsi
= NULL
;
630 st_store_ir_in_disk_cache(st
, &stvp
->Base
, false);
635 * This must be done after the translation to TGSI is done, because
636 * we'll pass the NIR shader to the driver and the TGSI version to
637 * the draw module for the select/feedback/rasterpos code.
639 if (st
->pipe
->screen
->get_shader_param(st
->pipe
->screen
,
641 PIPE_SHADER_CAP_PREFERRED_IR
)) {
642 assert(!stvp
->glsl_to_tgsi
);
645 st_translate_prog_to_nir(st
, &stvp
->Base
, MESA_SHADER_VERTEX
);
647 if (stvp
->state
.ir
.nir
)
648 ralloc_free(stvp
->state
.ir
.nir
);
649 stvp
->state
.type
= PIPE_SHADER_IR_NIR
;
650 stvp
->state
.ir
.nir
= nir
;
651 stvp
->Base
.nir
= nir
;
655 return stvp
->state
.tokens
!= NULL
;
658 static const gl_state_index16 depth_range_state
[STATE_LENGTH
] =
659 { STATE_DEPTH_RANGE
};
661 static struct st_vp_variant
*
662 st_create_vp_variant(struct st_context
*st
,
663 struct st_vertex_program
*stvp
,
664 const struct st_common_variant_key
*key
)
666 struct st_vp_variant
*vpv
= CALLOC_STRUCT(st_vp_variant
);
667 struct pipe_context
*pipe
= st
->pipe
;
668 struct pipe_shader_state state
= {0};
670 static const gl_state_index16 point_size_state
[STATE_LENGTH
] =
671 { STATE_INTERNAL
, STATE_POINT_SIZE_CLAMPED
, 0 };
672 struct gl_program_parameter_list
*params
= stvp
->Base
.Parameters
;
675 vpv
->num_inputs
= stvp
->num_inputs
;
677 state
.stream_output
= stvp
->state
.stream_output
;
679 if (stvp
->state
.type
== PIPE_SHADER_IR_NIR
) {
680 state
.type
= PIPE_SHADER_IR_NIR
;
681 state
.ir
.nir
= nir_shader_clone(NULL
, stvp
->state
.ir
.nir
);
682 if (key
->clamp_color
)
683 NIR_PASS_V(state
.ir
.nir
, nir_lower_clamp_color_outputs
);
684 if (key
->passthrough_edgeflags
) {
685 NIR_PASS_V(state
.ir
.nir
, nir_lower_passthrough_edgeflags
);
689 if (key
->lower_point_size
) {
690 _mesa_add_state_reference(params
, point_size_state
);
691 NIR_PASS_V(state
.ir
.nir
, nir_lower_point_size_mov
,
695 if (key
->lower_ucp
) {
696 struct pipe_screen
*screen
= pipe
->screen
;
697 bool can_compact
= screen
->get_param(screen
,
698 PIPE_CAP_NIR_COMPACT_ARRAYS
);
700 bool use_eye
= st
->ctx
->_Shader
->CurrentProgram
[MESA_SHADER_VERTEX
] != NULL
;
701 gl_state_index16 clipplane_state
[MAX_CLIP_PLANES
][STATE_LENGTH
];
702 for (int i
= 0; i
< MAX_CLIP_PLANES
; ++i
) {
704 clipplane_state
[i
][0] = STATE_CLIPPLANE
;
705 clipplane_state
[i
][1] = i
;
707 clipplane_state
[i
][0] = STATE_INTERNAL
;
708 clipplane_state
[i
][1] = STATE_CLIP_INTERNAL
;
709 clipplane_state
[i
][2] = i
;
711 _mesa_add_state_reference(params
, clipplane_state
[i
]);
714 NIR_PASS_V(state
.ir
.nir
, nir_lower_clip_vs
, key
->lower_ucp
,
715 true, can_compact
, clipplane_state
);
716 NIR_PASS_V(state
.ir
.nir
, nir_lower_io_to_temporaries
,
717 nir_shader_get_entrypoint(state
.ir
.nir
), true, false);
720 st_finalize_nir(st
, &stvp
->Base
, stvp
->shader_program
,
723 vpv
->driver_shader
= pipe
->create_vs_state(pipe
, &state
);
725 /* When generating a NIR program, we usually don't have TGSI tokens.
726 * However, we do create them for ARB_vertex_program / fixed-function VS
727 * programs which we may need to use with the draw module for legacy
728 * feedback/select emulation. If they exist, copy them.
730 * TODO: Lowering for shader variants is not applied to TGSI when
731 * generating a NIR shader.
733 if (stvp
->state
.tokens
)
734 vpv
->tokens
= tgsi_dup_tokens(stvp
->state
.tokens
);
739 state
.type
= PIPE_SHADER_IR_TGSI
;
740 state
.tokens
= tgsi_dup_tokens(stvp
->state
.tokens
);
742 /* Emulate features. */
743 if (key
->clamp_color
|| key
->passthrough_edgeflags
) {
744 const struct tgsi_token
*tokens
;
746 (key
->clamp_color
? TGSI_EMU_CLAMP_COLOR_OUTPUTS
: 0) |
747 (key
->passthrough_edgeflags
? TGSI_EMU_PASSTHROUGH_EDGEFLAG
: 0);
749 tokens
= tgsi_emulate(state
.tokens
, flags
);
752 tgsi_free_tokens(state
.tokens
);
753 state
.tokens
= tokens
;
755 if (key
->passthrough_edgeflags
)
758 fprintf(stderr
, "mesa: cannot emulate deprecated features\n");
761 if (key
->lower_depth_clamp
) {
762 unsigned depth_range_const
=
763 _mesa_add_state_reference(params
, depth_range_state
);
765 const struct tgsi_token
*tokens
;
766 tokens
= st_tgsi_lower_depth_clamp(state
.tokens
, depth_range_const
,
767 key
->clip_negative_one_to_one
);
768 if (tokens
!= state
.tokens
)
769 tgsi_free_tokens(state
.tokens
);
770 state
.tokens
= tokens
;
773 if (ST_DEBUG
& DEBUG_TGSI
) {
774 tgsi_dump(state
.tokens
, 0);
778 vpv
->driver_shader
= pipe
->create_vs_state(pipe
, &state
);
779 /* Save this for selection/feedback/rasterpos. */
780 vpv
->tokens
= state
.tokens
;
786 * Find/create a vertex program variant.
788 struct st_vp_variant
*
789 st_get_vp_variant(struct st_context
*st
,
790 struct st_vertex_program
*stvp
,
791 const struct st_common_variant_key
*key
)
793 struct st_vp_variant
*vpv
;
795 /* Search for existing variant */
796 for (vpv
= stvp
->variants
; vpv
; vpv
= vpv
->next
) {
797 if (memcmp(&vpv
->key
, key
, sizeof(*key
)) == 0) {
804 vpv
= st_create_vp_variant(st
, stvp
, key
);
806 for (unsigned index
= 0; index
< vpv
->num_inputs
; ++index
) {
807 unsigned attr
= stvp
->index_to_input
[index
];
808 if (attr
== ST_DOUBLE_ATTRIB_PLACEHOLDER
)
810 vpv
->vert_attrib_mask
|= 1u << attr
;
813 /* insert into list */
814 vpv
->next
= stvp
->variants
;
815 stvp
->variants
= vpv
;
824 * Translate a Mesa fragment shader into a TGSI shader.
827 st_translate_fragment_program(struct st_context
*st
,
828 struct st_common_program
*stfp
)
830 /* Non-GLSL programs: */
831 if (!stfp
->glsl_to_tgsi
) {
832 _mesa_remove_output_reads(&stfp
->Base
, PROGRAM_OUTPUT
);
833 if (st
->ctx
->Const
.GLSLFragCoordIsSysVal
)
834 _mesa_program_fragment_position_to_sysval(&stfp
->Base
);
836 /* This determines which states will be updated when the assembly
839 * fragment.position and glDrawPixels always use constants.
841 stfp
->affected_states
= ST_NEW_FS_STATE
|
842 ST_NEW_SAMPLE_SHADING
|
846 /* Just set them for ATI_fs unconditionally. */
847 stfp
->affected_states
|= ST_NEW_FS_SAMPLER_VIEWS
|
851 if (stfp
->Base
.SamplersUsed
)
852 stfp
->affected_states
|= ST_NEW_FS_SAMPLER_VIEWS
|
856 /* Translate to NIR. */
858 st
->pipe
->screen
->get_shader_param(st
->pipe
->screen
,
859 PIPE_SHADER_FRAGMENT
,
860 PIPE_SHADER_CAP_PREFERRED_IR
)) {
862 st_translate_prog_to_nir(st
, &stfp
->Base
, MESA_SHADER_FRAGMENT
);
864 if (stfp
->state
.ir
.nir
)
865 ralloc_free(stfp
->state
.ir
.nir
);
866 stfp
->state
.type
= PIPE_SHADER_IR_NIR
;
867 stfp
->state
.ir
.nir
= nir
;
868 stfp
->Base
.nir
= nir
;
873 ubyte outputMapping
[2 * FRAG_RESULT_MAX
];
874 ubyte inputMapping
[VARYING_SLOT_MAX
];
875 ubyte inputSlotToAttr
[VARYING_SLOT_MAX
];
876 ubyte interpMode
[PIPE_MAX_SHADER_INPUTS
]; /* XXX size? */
878 GLbitfield64 inputsRead
;
879 struct ureg_program
*ureg
;
881 GLboolean write_all
= GL_FALSE
;
883 ubyte input_semantic_name
[PIPE_MAX_SHADER_INPUTS
];
884 ubyte input_semantic_index
[PIPE_MAX_SHADER_INPUTS
];
885 uint fs_num_inputs
= 0;
887 ubyte fs_output_semantic_name
[PIPE_MAX_SHADER_OUTPUTS
];
888 ubyte fs_output_semantic_index
[PIPE_MAX_SHADER_OUTPUTS
];
889 uint fs_num_outputs
= 0;
891 memset(inputSlotToAttr
, ~0, sizeof(inputSlotToAttr
));
894 * Convert Mesa program inputs to TGSI input register semantics.
896 inputsRead
= stfp
->Base
.info
.inputs_read
;
897 for (attr
= 0; attr
< VARYING_SLOT_MAX
; attr
++) {
898 if ((inputsRead
& BITFIELD64_BIT(attr
)) != 0) {
899 const GLuint slot
= fs_num_inputs
++;
901 inputMapping
[attr
] = slot
;
902 inputSlotToAttr
[slot
] = attr
;
905 case VARYING_SLOT_POS
:
906 input_semantic_name
[slot
] = TGSI_SEMANTIC_POSITION
;
907 input_semantic_index
[slot
] = 0;
908 interpMode
[slot
] = TGSI_INTERPOLATE_LINEAR
;
910 case VARYING_SLOT_COL0
:
911 input_semantic_name
[slot
] = TGSI_SEMANTIC_COLOR
;
912 input_semantic_index
[slot
] = 0;
913 interpMode
[slot
] = stfp
->glsl_to_tgsi
?
914 TGSI_INTERPOLATE_COUNT
: TGSI_INTERPOLATE_COLOR
;
916 case VARYING_SLOT_COL1
:
917 input_semantic_name
[slot
] = TGSI_SEMANTIC_COLOR
;
918 input_semantic_index
[slot
] = 1;
919 interpMode
[slot
] = stfp
->glsl_to_tgsi
?
920 TGSI_INTERPOLATE_COUNT
: TGSI_INTERPOLATE_COLOR
;
922 case VARYING_SLOT_FOGC
:
923 input_semantic_name
[slot
] = TGSI_SEMANTIC_FOG
;
924 input_semantic_index
[slot
] = 0;
925 interpMode
[slot
] = TGSI_INTERPOLATE_PERSPECTIVE
;
927 case VARYING_SLOT_FACE
:
928 input_semantic_name
[slot
] = TGSI_SEMANTIC_FACE
;
929 input_semantic_index
[slot
] = 0;
930 interpMode
[slot
] = TGSI_INTERPOLATE_CONSTANT
;
932 case VARYING_SLOT_PRIMITIVE_ID
:
933 input_semantic_name
[slot
] = TGSI_SEMANTIC_PRIMID
;
934 input_semantic_index
[slot
] = 0;
935 interpMode
[slot
] = TGSI_INTERPOLATE_CONSTANT
;
937 case VARYING_SLOT_LAYER
:
938 input_semantic_name
[slot
] = TGSI_SEMANTIC_LAYER
;
939 input_semantic_index
[slot
] = 0;
940 interpMode
[slot
] = TGSI_INTERPOLATE_CONSTANT
;
942 case VARYING_SLOT_VIEWPORT
:
943 input_semantic_name
[slot
] = TGSI_SEMANTIC_VIEWPORT_INDEX
;
944 input_semantic_index
[slot
] = 0;
945 interpMode
[slot
] = TGSI_INTERPOLATE_CONSTANT
;
947 case VARYING_SLOT_CLIP_DIST0
:
948 input_semantic_name
[slot
] = TGSI_SEMANTIC_CLIPDIST
;
949 input_semantic_index
[slot
] = 0;
950 interpMode
[slot
] = TGSI_INTERPOLATE_PERSPECTIVE
;
952 case VARYING_SLOT_CLIP_DIST1
:
953 input_semantic_name
[slot
] = TGSI_SEMANTIC_CLIPDIST
;
954 input_semantic_index
[slot
] = 1;
955 interpMode
[slot
] = TGSI_INTERPOLATE_PERSPECTIVE
;
957 case VARYING_SLOT_CULL_DIST0
:
958 case VARYING_SLOT_CULL_DIST1
:
959 /* these should have been lowered by GLSL */
962 /* In most cases, there is nothing special about these
963 * inputs, so adopt a convention to use the generic
964 * semantic name and the mesa VARYING_SLOT_ number as the
967 * All that is required is that the vertex shader labels
968 * its own outputs similarly, and that the vertex shader
969 * generates at least every output required by the
970 * fragment shader plus fixed-function hardware (such as
973 * However, some drivers may need us to identify the PNTC and TEXi
974 * varyings if, for example, their capability to replace them with
975 * sprite coordinates is limited.
977 case VARYING_SLOT_PNTC
:
978 if (st
->needs_texcoord_semantic
) {
979 input_semantic_name
[slot
] = TGSI_SEMANTIC_PCOORD
;
980 input_semantic_index
[slot
] = 0;
981 interpMode
[slot
] = TGSI_INTERPOLATE_LINEAR
;
985 case VARYING_SLOT_TEX0
:
986 case VARYING_SLOT_TEX1
:
987 case VARYING_SLOT_TEX2
:
988 case VARYING_SLOT_TEX3
:
989 case VARYING_SLOT_TEX4
:
990 case VARYING_SLOT_TEX5
:
991 case VARYING_SLOT_TEX6
:
992 case VARYING_SLOT_TEX7
:
993 if (st
->needs_texcoord_semantic
) {
994 input_semantic_name
[slot
] = TGSI_SEMANTIC_TEXCOORD
;
995 input_semantic_index
[slot
] = attr
- VARYING_SLOT_TEX0
;
996 interpMode
[slot
] = stfp
->glsl_to_tgsi
?
997 TGSI_INTERPOLATE_COUNT
: TGSI_INTERPOLATE_PERSPECTIVE
;
1001 case VARYING_SLOT_VAR0
:
1003 /* Semantic indices should be zero-based because drivers may choose
1004 * to assign a fixed slot determined by that index.
1005 * This is useful because ARB_separate_shader_objects uses location
1006 * qualifiers for linkage, and if the semantic index corresponds to
1007 * these locations, linkage passes in the driver become unecessary.
1009 * If needs_texcoord_semantic is true, no semantic indices will be
1010 * consumed for the TEXi varyings, and we can base the locations of
1011 * the user varyings on VAR0. Otherwise, we use TEX0 as base index.
1013 assert(attr
>= VARYING_SLOT_VAR0
|| attr
== VARYING_SLOT_PNTC
||
1014 (attr
>= VARYING_SLOT_TEX0
&& attr
<= VARYING_SLOT_TEX7
));
1015 input_semantic_name
[slot
] = TGSI_SEMANTIC_GENERIC
;
1016 input_semantic_index
[slot
] = st_get_generic_varying_index(st
, attr
);
1017 if (attr
== VARYING_SLOT_PNTC
)
1018 interpMode
[slot
] = TGSI_INTERPOLATE_LINEAR
;
1020 interpMode
[slot
] = stfp
->glsl_to_tgsi
?
1021 TGSI_INTERPOLATE_COUNT
: TGSI_INTERPOLATE_PERSPECTIVE
;
1027 inputMapping
[attr
] = -1;
1032 * Semantics and mapping for outputs
1034 GLbitfield64 outputsWritten
= stfp
->Base
.info
.outputs_written
;
1036 /* if z is written, emit that first */
1037 if (outputsWritten
& BITFIELD64_BIT(FRAG_RESULT_DEPTH
)) {
1038 fs_output_semantic_name
[fs_num_outputs
] = TGSI_SEMANTIC_POSITION
;
1039 fs_output_semantic_index
[fs_num_outputs
] = 0;
1040 outputMapping
[FRAG_RESULT_DEPTH
] = fs_num_outputs
;
1042 outputsWritten
&= ~(1 << FRAG_RESULT_DEPTH
);
1045 if (outputsWritten
& BITFIELD64_BIT(FRAG_RESULT_STENCIL
)) {
1046 fs_output_semantic_name
[fs_num_outputs
] = TGSI_SEMANTIC_STENCIL
;
1047 fs_output_semantic_index
[fs_num_outputs
] = 0;
1048 outputMapping
[FRAG_RESULT_STENCIL
] = fs_num_outputs
;
1050 outputsWritten
&= ~(1 << FRAG_RESULT_STENCIL
);
1053 if (outputsWritten
& BITFIELD64_BIT(FRAG_RESULT_SAMPLE_MASK
)) {
1054 fs_output_semantic_name
[fs_num_outputs
] = TGSI_SEMANTIC_SAMPLEMASK
;
1055 fs_output_semantic_index
[fs_num_outputs
] = 0;
1056 outputMapping
[FRAG_RESULT_SAMPLE_MASK
] = fs_num_outputs
;
1058 outputsWritten
&= ~(1 << FRAG_RESULT_SAMPLE_MASK
);
1061 /* handle remaining outputs (color) */
1062 for (attr
= 0; attr
< ARRAY_SIZE(outputMapping
); attr
++) {
1063 const GLbitfield64 written
= attr
< FRAG_RESULT_MAX
? outputsWritten
:
1064 stfp
->Base
.SecondaryOutputsWritten
;
1065 const unsigned loc
= attr
% FRAG_RESULT_MAX
;
1067 if (written
& BITFIELD64_BIT(loc
)) {
1069 case FRAG_RESULT_DEPTH
:
1070 case FRAG_RESULT_STENCIL
:
1071 case FRAG_RESULT_SAMPLE_MASK
:
1075 case FRAG_RESULT_COLOR
:
1076 write_all
= GL_TRUE
; /* fallthrough */
1079 assert(loc
== FRAG_RESULT_COLOR
||
1080 (FRAG_RESULT_DATA0
<= loc
&& loc
< FRAG_RESULT_MAX
));
1082 index
= (loc
== FRAG_RESULT_COLOR
) ? 0 : (loc
- FRAG_RESULT_DATA0
);
1084 if (attr
>= FRAG_RESULT_MAX
) {
1085 /* Secondary color for dual source blending. */
1090 fs_output_semantic_name
[fs_num_outputs
] = TGSI_SEMANTIC_COLOR
;
1091 fs_output_semantic_index
[fs_num_outputs
] = index
;
1092 outputMapping
[attr
] = fs_num_outputs
;
1101 ureg
= ureg_create_with_screen(PIPE_SHADER_FRAGMENT
, st
->pipe
->screen
);
1105 if (ST_DEBUG
& DEBUG_MESA
) {
1106 _mesa_print_program(&stfp
->Base
);
1107 _mesa_print_program_parameters(st
->ctx
, &stfp
->Base
);
1110 if (write_all
== GL_TRUE
)
1111 ureg_property(ureg
, TGSI_PROPERTY_FS_COLOR0_WRITES_ALL_CBUFS
, 1);
1113 if (stfp
->Base
.info
.fs
.depth_layout
!= FRAG_DEPTH_LAYOUT_NONE
) {
1114 switch (stfp
->Base
.info
.fs
.depth_layout
) {
1115 case FRAG_DEPTH_LAYOUT_ANY
:
1116 ureg_property(ureg
, TGSI_PROPERTY_FS_DEPTH_LAYOUT
,
1117 TGSI_FS_DEPTH_LAYOUT_ANY
);
1119 case FRAG_DEPTH_LAYOUT_GREATER
:
1120 ureg_property(ureg
, TGSI_PROPERTY_FS_DEPTH_LAYOUT
,
1121 TGSI_FS_DEPTH_LAYOUT_GREATER
);
1123 case FRAG_DEPTH_LAYOUT_LESS
:
1124 ureg_property(ureg
, TGSI_PROPERTY_FS_DEPTH_LAYOUT
,
1125 TGSI_FS_DEPTH_LAYOUT_LESS
);
1127 case FRAG_DEPTH_LAYOUT_UNCHANGED
:
1128 ureg_property(ureg
, TGSI_PROPERTY_FS_DEPTH_LAYOUT
,
1129 TGSI_FS_DEPTH_LAYOUT_UNCHANGED
);
1136 if (stfp
->glsl_to_tgsi
) {
1137 st_translate_program(st
->ctx
,
1138 PIPE_SHADER_FRAGMENT
,
1146 input_semantic_name
,
1147 input_semantic_index
,
1152 fs_output_semantic_name
,
1153 fs_output_semantic_index
);
1155 free_glsl_to_tgsi_visitor(stfp
->glsl_to_tgsi
);
1156 } else if (stfp
->ati_fs
)
1157 st_translate_atifs_program(ureg
,
1163 input_semantic_name
,
1164 input_semantic_index
,
1169 fs_output_semantic_name
,
1170 fs_output_semantic_index
);
1172 st_translate_mesa_program(st
->ctx
,
1173 PIPE_SHADER_FRAGMENT
,
1179 input_semantic_name
,
1180 input_semantic_index
,
1185 fs_output_semantic_name
,
1186 fs_output_semantic_index
);
1188 stfp
->state
.tokens
= ureg_get_tokens(ureg
, NULL
);
1191 if (stfp
->glsl_to_tgsi
) {
1192 stfp
->glsl_to_tgsi
= NULL
;
1193 st_store_ir_in_disk_cache(st
, &stfp
->Base
, false);
1196 return stfp
->state
.tokens
!= NULL
;
1199 static struct st_fp_variant
*
1200 st_create_fp_variant(struct st_context
*st
,
1201 struct st_common_program
*stfp
,
1202 const struct st_fp_variant_key
*key
)
1204 struct pipe_context
*pipe
= st
->pipe
;
1205 struct st_fp_variant
*variant
= CALLOC_STRUCT(st_fp_variant
);
1206 struct pipe_shader_state state
= {0};
1207 struct gl_program_parameter_list
*params
= stfp
->Base
.Parameters
;
1208 static const gl_state_index16 texcoord_state
[STATE_LENGTH
] =
1209 { STATE_INTERNAL
, STATE_CURRENT_ATTRIB
, VERT_ATTRIB_TEX0
};
1210 static const gl_state_index16 scale_state
[STATE_LENGTH
] =
1211 { STATE_INTERNAL
, STATE_PT_SCALE
};
1212 static const gl_state_index16 bias_state
[STATE_LENGTH
] =
1213 { STATE_INTERNAL
, STATE_PT_BIAS
};
1214 static const gl_state_index16 alpha_ref_state
[STATE_LENGTH
] =
1215 { STATE_INTERNAL
, STATE_ALPHA_REF
};
1220 if (stfp
->state
.type
== PIPE_SHADER_IR_NIR
) {
1221 state
.type
= PIPE_SHADER_IR_NIR
;
1222 state
.ir
.nir
= nir_shader_clone(NULL
, stfp
->state
.ir
.nir
);
1224 if (key
->clamp_color
)
1225 NIR_PASS_V(state
.ir
.nir
, nir_lower_clamp_color_outputs
);
1227 if (key
->lower_flatshade
)
1228 NIR_PASS_V(state
.ir
.nir
, nir_lower_flatshade
);
1230 if (key
->lower_alpha_func
!= COMPARE_FUNC_NEVER
) {
1231 _mesa_add_state_reference(params
, alpha_ref_state
);
1232 NIR_PASS_V(state
.ir
.nir
, nir_lower_alpha_test
, key
->lower_alpha_func
,
1233 false, alpha_ref_state
);
1236 if (key
->lower_two_sided_color
)
1237 NIR_PASS_V(state
.ir
.nir
, nir_lower_two_sided_color
);
1239 if (key
->persample_shading
) {
1240 nir_shader
*shader
= state
.ir
.nir
;
1241 nir_foreach_variable(var
, &shader
->inputs
)
1242 var
->data
.sample
= true;
1245 assert(!(key
->bitmap
&& key
->drawpixels
));
1249 nir_lower_bitmap_options options
= {0};
1251 variant
->bitmap_sampler
= ffs(~stfp
->Base
.SamplersUsed
) - 1;
1252 options
.sampler
= variant
->bitmap_sampler
;
1253 options
.swizzle_xxxx
= st
->bitmap
.tex_format
== PIPE_FORMAT_R8_UNORM
;
1255 NIR_PASS_V(state
.ir
.nir
, nir_lower_bitmap
, &options
);
1258 /* glDrawPixels (color only) */
1259 if (key
->drawpixels
) {
1260 nir_lower_drawpixels_options options
= {{0}};
1261 unsigned samplers_used
= stfp
->Base
.SamplersUsed
;
1263 /* Find the first unused slot. */
1264 variant
->drawpix_sampler
= ffs(~samplers_used
) - 1;
1265 options
.drawpix_sampler
= variant
->drawpix_sampler
;
1266 samplers_used
|= (1 << variant
->drawpix_sampler
);
1268 options
.pixel_maps
= key
->pixelMaps
;
1269 if (key
->pixelMaps
) {
1270 variant
->pixelmap_sampler
= ffs(~samplers_used
) - 1;
1271 options
.pixelmap_sampler
= variant
->pixelmap_sampler
;
1274 options
.scale_and_bias
= key
->scaleAndBias
;
1275 if (key
->scaleAndBias
) {
1276 _mesa_add_state_reference(params
, scale_state
);
1277 memcpy(options
.scale_state_tokens
, scale_state
,
1278 sizeof(options
.scale_state_tokens
));
1279 _mesa_add_state_reference(params
, bias_state
);
1280 memcpy(options
.bias_state_tokens
, bias_state
,
1281 sizeof(options
.bias_state_tokens
));
1284 _mesa_add_state_reference(params
, texcoord_state
);
1285 memcpy(options
.texcoord_state_tokens
, texcoord_state
,
1286 sizeof(options
.texcoord_state_tokens
));
1288 NIR_PASS_V(state
.ir
.nir
, nir_lower_drawpixels
, &options
);
1291 if (unlikely(key
->external
.lower_nv12
|| key
->external
.lower_iyuv
||
1292 key
->external
.lower_xy_uxvx
|| key
->external
.lower_yx_xuxv
||
1293 key
->external
.lower_ayuv
|| key
->external
.lower_xyuv
)) {
1294 nir_lower_tex_options options
= {0};
1295 options
.lower_y_uv_external
= key
->external
.lower_nv12
;
1296 options
.lower_y_u_v_external
= key
->external
.lower_iyuv
;
1297 options
.lower_xy_uxvx_external
= key
->external
.lower_xy_uxvx
;
1298 options
.lower_yx_xuxv_external
= key
->external
.lower_yx_xuxv
;
1299 options
.lower_ayuv_external
= key
->external
.lower_ayuv
;
1300 options
.lower_xyuv_external
= key
->external
.lower_xyuv
;
1301 NIR_PASS_V(state
.ir
.nir
, nir_lower_tex
, &options
);
1304 st_finalize_nir(st
, &stfp
->Base
, stfp
->shader_program
, state
.ir
.nir
);
1306 if (unlikely(key
->external
.lower_nv12
|| key
->external
.lower_iyuv
||
1307 key
->external
.lower_xy_uxvx
|| key
->external
.lower_yx_xuxv
)) {
1308 /* This pass needs to happen *after* nir_lower_sampler */
1309 NIR_PASS_V(state
.ir
.nir
, st_nir_lower_tex_src_plane
,
1310 ~stfp
->Base
.SamplersUsed
,
1311 key
->external
.lower_nv12
|| key
->external
.lower_xy_uxvx
||
1312 key
->external
.lower_yx_xuxv
,
1313 key
->external
.lower_iyuv
);
1316 /* Some of the lowering above may have introduced new varyings */
1317 nir_shader_gather_info(state
.ir
.nir
,
1318 nir_shader_get_entrypoint(state
.ir
.nir
));
1320 variant
->driver_shader
= pipe
->create_fs_state(pipe
, &state
);
1321 variant
->key
= *key
;
1326 state
.tokens
= stfp
->state
.tokens
;
1328 assert(!(key
->bitmap
&& key
->drawpixels
));
1330 /* Fix texture targets and add fog for ATI_fs */
1332 const struct tgsi_token
*tokens
= st_fixup_atifs(state
.tokens
, key
);
1335 state
.tokens
= tokens
;
1337 fprintf(stderr
, "mesa: cannot post-process ATI_fs\n");
1340 /* Emulate features. */
1341 if (key
->clamp_color
|| key
->persample_shading
) {
1342 const struct tgsi_token
*tokens
;
1344 (key
->clamp_color
? TGSI_EMU_CLAMP_COLOR_OUTPUTS
: 0) |
1345 (key
->persample_shading
? TGSI_EMU_FORCE_PERSAMPLE_INTERP
: 0);
1347 tokens
= tgsi_emulate(state
.tokens
, flags
);
1350 if (state
.tokens
!= stfp
->state
.tokens
)
1351 tgsi_free_tokens(state
.tokens
);
1352 state
.tokens
= tokens
;
1354 fprintf(stderr
, "mesa: cannot emulate deprecated features\n");
1359 const struct tgsi_token
*tokens
;
1361 variant
->bitmap_sampler
= ffs(~stfp
->Base
.SamplersUsed
) - 1;
1363 tokens
= st_get_bitmap_shader(state
.tokens
,
1364 st
->internal_target
,
1365 variant
->bitmap_sampler
,
1366 st
->needs_texcoord_semantic
,
1367 st
->bitmap
.tex_format
==
1368 PIPE_FORMAT_R8_UNORM
);
1371 if (state
.tokens
!= stfp
->state
.tokens
)
1372 tgsi_free_tokens(state
.tokens
);
1373 state
.tokens
= tokens
;
1375 fprintf(stderr
, "mesa: cannot create a shader for glBitmap\n");
1378 /* glDrawPixels (color only) */
1379 if (key
->drawpixels
) {
1380 const struct tgsi_token
*tokens
;
1381 unsigned scale_const
= 0, bias_const
= 0, texcoord_const
= 0;
1383 /* Find the first unused slot. */
1384 variant
->drawpix_sampler
= ffs(~stfp
->Base
.SamplersUsed
) - 1;
1386 if (key
->pixelMaps
) {
1387 unsigned samplers_used
= stfp
->Base
.SamplersUsed
|
1388 (1 << variant
->drawpix_sampler
);
1390 variant
->pixelmap_sampler
= ffs(~samplers_used
) - 1;
1393 if (key
->scaleAndBias
) {
1394 scale_const
= _mesa_add_state_reference(params
, scale_state
);
1395 bias_const
= _mesa_add_state_reference(params
, bias_state
);
1398 texcoord_const
= _mesa_add_state_reference(params
, texcoord_state
);
1400 tokens
= st_get_drawpix_shader(state
.tokens
,
1401 st
->needs_texcoord_semantic
,
1402 key
->scaleAndBias
, scale_const
,
1403 bias_const
, key
->pixelMaps
,
1404 variant
->drawpix_sampler
,
1405 variant
->pixelmap_sampler
,
1406 texcoord_const
, st
->internal_target
);
1409 if (state
.tokens
!= stfp
->state
.tokens
)
1410 tgsi_free_tokens(state
.tokens
);
1411 state
.tokens
= tokens
;
1413 fprintf(stderr
, "mesa: cannot create a shader for glDrawPixels\n");
1416 if (unlikely(key
->external
.lower_nv12
|| key
->external
.lower_iyuv
||
1417 key
->external
.lower_xy_uxvx
|| key
->external
.lower_yx_xuxv
)) {
1418 const struct tgsi_token
*tokens
;
1420 /* samplers inserted would conflict, but this should be unpossible: */
1421 assert(!(key
->bitmap
|| key
->drawpixels
));
1423 tokens
= st_tgsi_lower_yuv(state
.tokens
,
1424 ~stfp
->Base
.SamplersUsed
,
1425 key
->external
.lower_nv12
||
1426 key
->external
.lower_xy_uxvx
||
1427 key
->external
.lower_yx_xuxv
,
1428 key
->external
.lower_iyuv
);
1430 if (state
.tokens
!= stfp
->state
.tokens
)
1431 tgsi_free_tokens(state
.tokens
);
1432 state
.tokens
= tokens
;
1434 fprintf(stderr
, "mesa: cannot create a shader for samplerExternalOES\n");
1438 if (key
->lower_depth_clamp
) {
1439 unsigned depth_range_const
= _mesa_add_state_reference(params
, depth_range_state
);
1441 const struct tgsi_token
*tokens
;
1442 tokens
= st_tgsi_lower_depth_clamp_fs(state
.tokens
, depth_range_const
);
1443 if (state
.tokens
!= stfp
->state
.tokens
)
1444 tgsi_free_tokens(state
.tokens
);
1445 state
.tokens
= tokens
;
1448 if (ST_DEBUG
& DEBUG_TGSI
) {
1449 tgsi_dump(state
.tokens
, 0);
1453 /* fill in variant */
1454 variant
->driver_shader
= pipe
->create_fs_state(pipe
, &state
);
1455 variant
->key
= *key
;
1457 if (state
.tokens
!= stfp
->state
.tokens
)
1458 tgsi_free_tokens(state
.tokens
);
1463 * Translate fragment program if needed.
1465 struct st_fp_variant
*
1466 st_get_fp_variant(struct st_context
*st
,
1467 struct st_common_program
*stfp
,
1468 const struct st_fp_variant_key
*key
)
1470 struct st_fp_variant
*fpv
;
1472 /* Search for existing variant */
1473 for (fpv
= stfp
->fp_variants
; fpv
; fpv
= fpv
->next
) {
1474 if (memcmp(&fpv
->key
, key
, sizeof(*key
)) == 0) {
1481 fpv
= st_create_fp_variant(st
, stfp
, key
);
1483 if (key
->bitmap
|| key
->drawpixels
) {
1484 /* Regular variants should always come before the
1485 * bitmap & drawpixels variants, (unless there
1486 * are no regular variants) so that
1487 * st_update_fp can take a fast path when
1488 * shader_has_one_variant is set.
1490 if (!stfp
->fp_variants
) {
1491 stfp
->fp_variants
= fpv
;
1493 /* insert into list after the first one */
1494 fpv
->next
= stfp
->fp_variants
->next
;
1495 stfp
->fp_variants
->next
= fpv
;
1498 /* insert into list */
1499 fpv
->next
= stfp
->fp_variants
;
1500 stfp
->fp_variants
= fpv
;
1509 * Translate a program. This is common code for geometry and tessellation
1513 st_translate_common_program(struct st_context
*st
,
1514 struct st_common_program
*stcp
)
1516 struct gl_program
*prog
= &stcp
->Base
;
1517 enum pipe_shader_type stage
=
1518 pipe_shader_type_from_mesa(stcp
->Base
.info
.stage
);
1519 struct ureg_program
*ureg
= ureg_create_with_screen(stage
, st
->pipe
->screen
);
1525 case PIPE_SHADER_TESS_CTRL
:
1526 ureg_property(ureg
, TGSI_PROPERTY_TCS_VERTICES_OUT
,
1527 stcp
->Base
.info
.tess
.tcs_vertices_out
);
1530 case PIPE_SHADER_TESS_EVAL
:
1531 if (stcp
->Base
.info
.tess
.primitive_mode
== GL_ISOLINES
)
1532 ureg_property(ureg
, TGSI_PROPERTY_TES_PRIM_MODE
, GL_LINES
);
1534 ureg_property(ureg
, TGSI_PROPERTY_TES_PRIM_MODE
,
1535 stcp
->Base
.info
.tess
.primitive_mode
);
1537 STATIC_ASSERT((TESS_SPACING_EQUAL
+ 1) % 3 == PIPE_TESS_SPACING_EQUAL
);
1538 STATIC_ASSERT((TESS_SPACING_FRACTIONAL_ODD
+ 1) % 3 ==
1539 PIPE_TESS_SPACING_FRACTIONAL_ODD
);
1540 STATIC_ASSERT((TESS_SPACING_FRACTIONAL_EVEN
+ 1) % 3 ==
1541 PIPE_TESS_SPACING_FRACTIONAL_EVEN
);
1543 ureg_property(ureg
, TGSI_PROPERTY_TES_SPACING
,
1544 (stcp
->Base
.info
.tess
.spacing
+ 1) % 3);
1546 ureg_property(ureg
, TGSI_PROPERTY_TES_VERTEX_ORDER_CW
,
1547 !stcp
->Base
.info
.tess
.ccw
);
1548 ureg_property(ureg
, TGSI_PROPERTY_TES_POINT_MODE
,
1549 stcp
->Base
.info
.tess
.point_mode
);
1552 case PIPE_SHADER_GEOMETRY
:
1553 ureg_property(ureg
, TGSI_PROPERTY_GS_INPUT_PRIM
,
1554 stcp
->Base
.info
.gs
.input_primitive
);
1555 ureg_property(ureg
, TGSI_PROPERTY_GS_OUTPUT_PRIM
,
1556 stcp
->Base
.info
.gs
.output_primitive
);
1557 ureg_property(ureg
, TGSI_PROPERTY_GS_MAX_OUTPUT_VERTICES
,
1558 stcp
->Base
.info
.gs
.vertices_out
);
1559 ureg_property(ureg
, TGSI_PROPERTY_GS_INVOCATIONS
,
1560 stcp
->Base
.info
.gs
.invocations
);
1567 ubyte inputSlotToAttr
[VARYING_SLOT_TESS_MAX
];
1568 ubyte inputMapping
[VARYING_SLOT_TESS_MAX
];
1569 ubyte outputMapping
[VARYING_SLOT_TESS_MAX
];
1572 ubyte input_semantic_name
[PIPE_MAX_SHADER_INPUTS
];
1573 ubyte input_semantic_index
[PIPE_MAX_SHADER_INPUTS
];
1574 uint num_inputs
= 0;
1576 ubyte output_semantic_name
[PIPE_MAX_SHADER_OUTPUTS
];
1577 ubyte output_semantic_index
[PIPE_MAX_SHADER_OUTPUTS
];
1578 uint num_outputs
= 0;
1582 memset(inputSlotToAttr
, 0, sizeof(inputSlotToAttr
));
1583 memset(inputMapping
, 0, sizeof(inputMapping
));
1584 memset(outputMapping
, 0, sizeof(outputMapping
));
1585 memset(&stcp
->state
, 0, sizeof(stcp
->state
));
1587 if (prog
->info
.clip_distance_array_size
)
1588 ureg_property(ureg
, TGSI_PROPERTY_NUM_CLIPDIST_ENABLED
,
1589 prog
->info
.clip_distance_array_size
);
1590 if (prog
->info
.cull_distance_array_size
)
1591 ureg_property(ureg
, TGSI_PROPERTY_NUM_CULLDIST_ENABLED
,
1592 prog
->info
.cull_distance_array_size
);
1595 * Convert Mesa program inputs to TGSI input register semantics.
1597 for (attr
= 0; attr
< VARYING_SLOT_MAX
; attr
++) {
1598 if ((prog
->info
.inputs_read
& BITFIELD64_BIT(attr
)) == 0)
1601 unsigned slot
= num_inputs
++;
1603 inputMapping
[attr
] = slot
;
1604 inputSlotToAttr
[slot
] = attr
;
1606 unsigned semantic_name
, semantic_index
;
1607 tgsi_get_gl_varying_semantic(attr
, st
->needs_texcoord_semantic
,
1608 &semantic_name
, &semantic_index
);
1609 input_semantic_name
[slot
] = semantic_name
;
1610 input_semantic_index
[slot
] = semantic_index
;
1613 /* Also add patch inputs. */
1614 for (attr
= 0; attr
< 32; attr
++) {
1615 if (prog
->info
.patch_inputs_read
& (1u << attr
)) {
1616 GLuint slot
= num_inputs
++;
1617 GLuint patch_attr
= VARYING_SLOT_PATCH0
+ attr
;
1619 inputMapping
[patch_attr
] = slot
;
1620 inputSlotToAttr
[slot
] = patch_attr
;
1621 input_semantic_name
[slot
] = TGSI_SEMANTIC_PATCH
;
1622 input_semantic_index
[slot
] = attr
;
1626 /* initialize output semantics to defaults */
1627 for (i
= 0; i
< PIPE_MAX_SHADER_OUTPUTS
; i
++) {
1628 output_semantic_name
[i
] = TGSI_SEMANTIC_GENERIC
;
1629 output_semantic_index
[i
] = 0;
1633 * Determine number of outputs, the (default) output register
1634 * mapping and the semantic information for each output.
1636 for (attr
= 0; attr
< VARYING_SLOT_MAX
; attr
++) {
1637 if (prog
->info
.outputs_written
& BITFIELD64_BIT(attr
)) {
1638 GLuint slot
= num_outputs
++;
1640 outputMapping
[attr
] = slot
;
1642 unsigned semantic_name
, semantic_index
;
1643 tgsi_get_gl_varying_semantic(attr
, st
->needs_texcoord_semantic
,
1644 &semantic_name
, &semantic_index
);
1645 output_semantic_name
[slot
] = semantic_name
;
1646 output_semantic_index
[slot
] = semantic_index
;
1650 /* Also add patch outputs. */
1651 for (attr
= 0; attr
< 32; attr
++) {
1652 if (prog
->info
.patch_outputs_written
& (1u << attr
)) {
1653 GLuint slot
= num_outputs
++;
1654 GLuint patch_attr
= VARYING_SLOT_PATCH0
+ attr
;
1656 outputMapping
[patch_attr
] = slot
;
1657 output_semantic_name
[slot
] = TGSI_SEMANTIC_PATCH
;
1658 output_semantic_index
[slot
] = attr
;
1662 st_translate_program(st
->ctx
,
1671 input_semantic_name
,
1672 input_semantic_index
,
1677 output_semantic_name
,
1678 output_semantic_index
);
1680 stcp
->state
.tokens
= ureg_get_tokens(ureg
, NULL
);
1684 st_translate_stream_output_info(prog
);
1686 st_store_ir_in_disk_cache(st
, prog
, false);
1688 if ((ST_DEBUG
& DEBUG_TGSI
) && (ST_DEBUG
& DEBUG_MESA
)) {
1689 _mesa_print_program(prog
);
1693 if (ST_DEBUG
& DEBUG_TGSI
) {
1694 tgsi_dump(stcp
->state
.tokens
, 0);
1698 free_glsl_to_tgsi_visitor(stcp
->glsl_to_tgsi
);
1699 stcp
->glsl_to_tgsi
= NULL
;
1705 * Get/create a basic program variant.
1707 struct st_common_variant
*
1708 st_get_common_variant(struct st_context
*st
,
1709 struct st_common_program
*prog
,
1710 const struct st_common_variant_key
*key
)
1712 struct pipe_context
*pipe
= st
->pipe
;
1713 struct st_common_variant
*v
;
1714 struct pipe_shader_state state
= {0};
1716 /* Search for existing variant */
1717 for (v
= prog
->variants
; v
; v
= v
->next
) {
1718 if (memcmp(&v
->key
, key
, sizeof(*key
)) == 0) {
1725 v
= CALLOC_STRUCT(st_common_variant
);
1728 if (prog
->state
.type
== PIPE_SHADER_IR_NIR
) {
1729 state
.type
= PIPE_SHADER_IR_NIR
;
1730 state
.ir
.nir
= nir_shader_clone(NULL
, prog
->state
.ir
.nir
);
1732 if (key
->clamp_color
)
1733 NIR_PASS_V(state
.ir
.nir
, nir_lower_clamp_color_outputs
);
1735 state
.stream_output
= prog
->state
.stream_output
;
1737 st_finalize_nir(st
, &prog
->Base
, prog
->shader_program
,
1740 if (key
->lower_depth_clamp
) {
1741 struct gl_program_parameter_list
*params
= prog
->Base
.Parameters
;
1743 unsigned depth_range_const
=
1744 _mesa_add_state_reference(params
, depth_range_state
);
1746 const struct tgsi_token
*tokens
;
1748 st_tgsi_lower_depth_clamp(prog
->state
.tokens
,
1750 key
->clip_negative_one_to_one
);
1752 if (tokens
!= prog
->state
.tokens
)
1753 tgsi_free_tokens(prog
->state
.tokens
);
1755 prog
->state
.tokens
= tokens
;
1757 state
= prog
->state
;
1759 /* fill in new variant */
1760 switch (prog
->Base
.info
.stage
) {
1761 case MESA_SHADER_TESS_CTRL
:
1762 v
->driver_shader
= pipe
->create_tcs_state(pipe
, &state
);
1764 case MESA_SHADER_TESS_EVAL
:
1765 v
->driver_shader
= pipe
->create_tes_state(pipe
, &state
);
1767 case MESA_SHADER_GEOMETRY
:
1768 v
->driver_shader
= pipe
->create_gs_state(pipe
, &state
);
1770 case MESA_SHADER_COMPUTE
: {
1771 struct pipe_compute_state cs
= {0};
1772 cs
.ir_type
= state
.type
;
1773 cs
.req_local_mem
= prog
->Base
.info
.cs
.shared_size
;
1775 if (state
.type
== PIPE_SHADER_IR_NIR
)
1776 cs
.prog
= state
.ir
.nir
;
1778 cs
.prog
= state
.tokens
;
1780 v
->driver_shader
= pipe
->create_compute_state(pipe
, &cs
);
1784 assert(!"unhandled shader type");
1791 /* insert into list */
1792 v
->next
= prog
->variants
;
1802 * Vert/Geom/Frag programs have per-context variants. Free all the
1803 * variants attached to the given program which match the given context.
1806 destroy_program_variants(struct st_context
*st
, struct gl_program
*target
)
1808 if (!target
|| target
== &_mesa_DummyProgram
)
1811 switch (target
->Target
) {
1812 case GL_VERTEX_PROGRAM_ARB
:
1814 struct st_vertex_program
*stvp
= (struct st_vertex_program
*) target
;
1815 struct st_vp_variant
*vpv
, **prevPtr
= &stvp
->variants
;
1817 for (vpv
= stvp
->variants
; vpv
; ) {
1818 struct st_vp_variant
*next
= vpv
->next
;
1819 if (vpv
->key
.st
== st
) {
1820 /* unlink from list */
1822 /* destroy this variant */
1823 delete_vp_variant(st
, vpv
);
1826 prevPtr
= &vpv
->next
;
1832 case GL_FRAGMENT_PROGRAM_ARB
:
1834 struct st_common_program
*stfp
=
1835 (struct st_common_program
*) target
;
1836 struct st_fp_variant
*fpv
, **prevPtr
= &stfp
->fp_variants
;
1838 for (fpv
= stfp
->fp_variants
; fpv
; ) {
1839 struct st_fp_variant
*next
= fpv
->next
;
1840 if (fpv
->key
.st
== st
) {
1841 /* unlink from list */
1843 /* destroy this variant */
1844 delete_fp_variant(st
, fpv
);
1847 prevPtr
= &fpv
->next
;
1853 case GL_GEOMETRY_PROGRAM_NV
:
1854 case GL_TESS_CONTROL_PROGRAM_NV
:
1855 case GL_TESS_EVALUATION_PROGRAM_NV
:
1856 case GL_COMPUTE_PROGRAM_NV
:
1858 struct st_common_program
*p
= st_common_program(target
);
1859 struct st_common_variant
*v
, **prevPtr
= &p
->variants
;
1861 for (v
= p
->variants
; v
; ) {
1862 struct st_common_variant
*next
= v
->next
;
1863 if (v
->key
.st
== st
) {
1864 /* unlink from list */
1866 /* destroy this variant */
1867 delete_basic_variant(st
, v
, target
->Target
);
1877 _mesa_problem(NULL
, "Unexpected program target 0x%x in "
1878 "destroy_program_variants_cb()", target
->Target
);
1884 * Callback for _mesa_HashWalk. Free all the shader's program variants
1885 * which match the given context.
1888 destroy_shader_program_variants_cb(GLuint key
, void *data
, void *userData
)
1890 struct st_context
*st
= (struct st_context
*) userData
;
1891 struct gl_shader
*shader
= (struct gl_shader
*) data
;
1893 switch (shader
->Type
) {
1894 case GL_SHADER_PROGRAM_MESA
:
1896 struct gl_shader_program
*shProg
= (struct gl_shader_program
*) data
;
1899 for (i
= 0; i
< ARRAY_SIZE(shProg
->_LinkedShaders
); i
++) {
1900 if (shProg
->_LinkedShaders
[i
])
1901 destroy_program_variants(st
, shProg
->_LinkedShaders
[i
]->Program
);
1905 case GL_VERTEX_SHADER
:
1906 case GL_FRAGMENT_SHADER
:
1907 case GL_GEOMETRY_SHADER
:
1908 case GL_TESS_CONTROL_SHADER
:
1909 case GL_TESS_EVALUATION_SHADER
:
1910 case GL_COMPUTE_SHADER
:
1919 * Callback for _mesa_HashWalk. Free all the program variants which match
1920 * the given context.
1923 destroy_program_variants_cb(GLuint key
, void *data
, void *userData
)
1925 struct st_context
*st
= (struct st_context
*) userData
;
1926 struct gl_program
*program
= (struct gl_program
*) data
;
1927 destroy_program_variants(st
, program
);
1932 * Walk over all shaders and programs to delete any variants which
1933 * belong to the given context.
1934 * This is called during context tear-down.
1937 st_destroy_program_variants(struct st_context
*st
)
1939 /* If shaders can be shared with other contexts, the last context will
1940 * call DeleteProgram on all shaders, releasing everything.
1942 if (st
->has_shareable_shaders
)
1945 /* ARB vert/frag program */
1946 _mesa_HashWalk(st
->ctx
->Shared
->Programs
,
1947 destroy_program_variants_cb
, st
);
1949 /* GLSL vert/frag/geom shaders */
1950 _mesa_HashWalk(st
->ctx
->Shared
->ShaderObjects
,
1951 destroy_shader_program_variants_cb
, st
);
1956 * For debugging, print/dump the current vertex program.
1959 st_print_current_vertex_program(void)
1961 GET_CURRENT_CONTEXT(ctx
);
1963 if (ctx
->VertexProgram
._Current
) {
1964 struct st_vertex_program
*stvp
=
1965 (struct st_vertex_program
*) ctx
->VertexProgram
._Current
;
1966 struct st_vp_variant
*stv
;
1968 debug_printf("Vertex program %u\n", stvp
->Base
.Id
);
1970 for (stv
= stvp
->variants
; stv
; stv
= stv
->next
) {
1971 debug_printf("variant %p\n", stv
);
1972 tgsi_dump(stv
->tokens
, 0);
1979 * Compile one shader variant.
1982 st_precompile_shader_variant(struct st_context
*st
,
1983 struct gl_program
*prog
)
1985 switch (prog
->Target
) {
1986 case GL_VERTEX_PROGRAM_ARB
: {
1987 struct st_vertex_program
*p
= (struct st_vertex_program
*)prog
;
1988 struct st_common_variant_key key
;
1990 memset(&key
, 0, sizeof(key
));
1992 key
.st
= st
->has_shareable_shaders
? NULL
: st
;
1993 st_get_vp_variant(st
, p
, &key
);
1997 case GL_FRAGMENT_PROGRAM_ARB
: {
1998 struct st_common_program
*p
= (struct st_common_program
*)prog
;
1999 struct st_fp_variant_key key
;
2001 memset(&key
, 0, sizeof(key
));
2003 key
.st
= st
->has_shareable_shaders
? NULL
: st
;
2004 st_get_fp_variant(st
, p
, &key
);
2008 case GL_TESS_CONTROL_PROGRAM_NV
:
2009 case GL_TESS_EVALUATION_PROGRAM_NV
:
2010 case GL_GEOMETRY_PROGRAM_NV
:
2011 case GL_COMPUTE_PROGRAM_NV
: {
2012 struct st_common_program
*p
= st_common_program(prog
);
2013 struct st_common_variant_key key
;
2015 memset(&key
, 0, sizeof(key
));
2017 key
.st
= st
->has_shareable_shaders
? NULL
: st
;
2018 st_get_common_variant(st
, p
, &key
);