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);
406 * Translate ARB (asm) program to NIR
409 st_translate_prog_to_nir(struct st_context
*st
, struct gl_program
*prog
,
410 gl_shader_stage stage
)
412 const struct gl_shader_compiler_options
*options
=
413 &st
->ctx
->Const
.ShaderCompilerOptions
[stage
];
415 /* Translate to NIR */
416 nir_shader
*nir
= prog_to_nir(prog
, options
->NirOptions
);
417 NIR_PASS_V(nir
, nir_lower_regs_to_ssa
); /* turn registers into SSA */
418 nir_validate_shader(nir
, "after st/ptn lower_regs_to_ssa");
420 NIR_PASS_V(nir
, st_nir_lower_wpos_ytransform
, prog
, st
->pipe
->screen
);
421 NIR_PASS_V(nir
, nir_lower_system_values
);
424 NIR_PASS_V(nir
, nir_opt_constant_folding
);
426 st_finalize_nir_before_variants(nir
);
427 nir_validate_shader(nir
, "after st/ptn NIR opts");
433 st_prepare_vertex_program(struct st_vertex_program
*stvp
)
435 stvp
->num_inputs
= 0;
436 memset(stvp
->input_to_index
, ~0, sizeof(stvp
->input_to_index
));
437 memset(stvp
->result_to_output
, ~0, sizeof(stvp
->result_to_output
));
439 /* Determine number of inputs, the mappings between VERT_ATTRIB_x
440 * and TGSI generic input indexes, plus input attrib semantic info.
442 for (unsigned attr
= 0; attr
< VERT_ATTRIB_MAX
; attr
++) {
443 if ((stvp
->Base
.info
.inputs_read
& BITFIELD64_BIT(attr
)) != 0) {
444 stvp
->input_to_index
[attr
] = stvp
->num_inputs
;
445 stvp
->index_to_input
[stvp
->num_inputs
] = attr
;
448 if ((stvp
->Base
.DualSlotInputs
& BITFIELD64_BIT(attr
)) != 0) {
449 /* add placeholder for second part of a double attribute */
450 stvp
->index_to_input
[stvp
->num_inputs
] = ST_DOUBLE_ATTRIB_PLACEHOLDER
;
455 /* pre-setup potentially unused edgeflag input */
456 stvp
->input_to_index
[VERT_ATTRIB_EDGEFLAG
] = stvp
->num_inputs
;
457 stvp
->index_to_input
[stvp
->num_inputs
] = VERT_ATTRIB_EDGEFLAG
;
459 /* Compute mapping of vertex program outputs to slots. */
460 unsigned num_outputs
= 0;
461 for (unsigned attr
= 0; attr
< VARYING_SLOT_MAX
; attr
++) {
462 if (stvp
->Base
.info
.outputs_written
& BITFIELD64_BIT(attr
))
463 stvp
->result_to_output
[attr
] = num_outputs
++;
465 /* pre-setup potentially unused edgeflag output */
466 stvp
->result_to_output
[VARYING_SLOT_EDGE
] = num_outputs
;
470 st_translate_stream_output_info(struct gl_program
*prog
)
472 struct gl_transform_feedback_info
*info
= prog
->sh
.LinkedTransformFeedback
;
476 /* Determine the (default) output register mapping for each output. */
477 unsigned num_outputs
= 0;
478 ubyte output_mapping
[VARYING_SLOT_TESS_MAX
];
479 memset(output_mapping
, 0, sizeof(output_mapping
));
481 for (unsigned attr
= 0; attr
< VARYING_SLOT_MAX
; attr
++) {
482 if (prog
->info
.outputs_written
& BITFIELD64_BIT(attr
))
483 output_mapping
[attr
] = num_outputs
++;
486 /* Translate stream output info. */
487 struct pipe_stream_output_info
*so_info
= NULL
;
488 if (prog
->info
.stage
== MESA_SHADER_VERTEX
)
489 so_info
= &((struct st_vertex_program
*)prog
)->state
.stream_output
;
491 so_info
= &((struct st_common_program
*)prog
)->state
.stream_output
;
493 for (unsigned i
= 0; i
< info
->NumOutputs
; i
++) {
494 so_info
->output
[i
].register_index
=
495 output_mapping
[info
->Outputs
[i
].OutputRegister
];
496 so_info
->output
[i
].start_component
= info
->Outputs
[i
].ComponentOffset
;
497 so_info
->output
[i
].num_components
= info
->Outputs
[i
].NumComponents
;
498 so_info
->output
[i
].output_buffer
= info
->Outputs
[i
].OutputBuffer
;
499 so_info
->output
[i
].dst_offset
= info
->Outputs
[i
].DstOffset
;
500 so_info
->output
[i
].stream
= info
->Outputs
[i
].StreamId
;
503 for (unsigned i
= 0; i
< PIPE_MAX_SO_BUFFERS
; i
++) {
504 so_info
->stride
[i
] = info
->Buffers
[i
].Stride
;
506 so_info
->num_outputs
= info
->NumOutputs
;
510 * Translate a vertex program.
513 st_translate_vertex_program(struct st_context
*st
,
514 struct st_vertex_program
*stvp
)
516 struct ureg_program
*ureg
;
517 enum pipe_error error
;
518 unsigned num_outputs
= 0;
520 ubyte output_semantic_name
[VARYING_SLOT_MAX
] = {0};
521 ubyte output_semantic_index
[VARYING_SLOT_MAX
] = {0};
523 if (stvp
->Base
.arb
.IsPositionInvariant
)
524 _mesa_insert_mvp_code(st
->ctx
, &stvp
->Base
);
526 st_prepare_vertex_program(stvp
);
529 if (!stvp
->glsl_to_tgsi
) {
530 _mesa_remove_output_reads(&stvp
->Base
, PROGRAM_OUTPUT
);
532 /* This determines which states will be updated when the assembly
535 stvp
->affected_states
= ST_NEW_VS_STATE
|
537 ST_NEW_VERTEX_ARRAYS
;
539 if (stvp
->Base
.Parameters
->NumParameters
)
540 stvp
->affected_states
|= ST_NEW_VS_CONSTANTS
;
542 /* No samplers are allowed in ARB_vp. */
545 /* Get semantic names and indices. */
546 for (attr
= 0; attr
< VARYING_SLOT_MAX
; attr
++) {
547 if (stvp
->Base
.info
.outputs_written
& BITFIELD64_BIT(attr
)) {
548 unsigned slot
= num_outputs
++;
549 unsigned semantic_name
, semantic_index
;
550 tgsi_get_gl_varying_semantic(attr
, st
->needs_texcoord_semantic
,
551 &semantic_name
, &semantic_index
);
552 output_semantic_name
[slot
] = semantic_name
;
553 output_semantic_index
[slot
] = semantic_index
;
556 /* pre-setup potentially unused edgeflag output */
557 output_semantic_name
[num_outputs
] = TGSI_SEMANTIC_EDGEFLAG
;
558 output_semantic_index
[num_outputs
] = 0;
560 ureg
= ureg_create_with_screen(PIPE_SHADER_VERTEX
, st
->pipe
->screen
);
564 if (stvp
->Base
.info
.clip_distance_array_size
)
565 ureg_property(ureg
, TGSI_PROPERTY_NUM_CLIPDIST_ENABLED
,
566 stvp
->Base
.info
.clip_distance_array_size
);
567 if (stvp
->Base
.info
.cull_distance_array_size
)
568 ureg_property(ureg
, TGSI_PROPERTY_NUM_CULLDIST_ENABLED
,
569 stvp
->Base
.info
.cull_distance_array_size
);
571 if (ST_DEBUG
& DEBUG_MESA
) {
572 _mesa_print_program(&stvp
->Base
);
573 _mesa_print_program_parameters(st
->ctx
, &stvp
->Base
);
577 if (stvp
->glsl_to_tgsi
) {
578 error
= st_translate_program(st
->ctx
,
585 stvp
->input_to_index
,
586 NULL
, /* inputSlotToAttr */
587 NULL
, /* input semantic name */
588 NULL
, /* input semantic index */
589 NULL
, /* interp mode */
592 stvp
->result_to_output
,
593 output_semantic_name
,
594 output_semantic_index
);
596 st_translate_stream_output_info(&stvp
->Base
);
598 free_glsl_to_tgsi_visitor(stvp
->glsl_to_tgsi
);
600 error
= st_translate_mesa_program(st
->ctx
,
606 stvp
->input_to_index
,
607 NULL
, /* input semantic name */
608 NULL
, /* input semantic index */
612 stvp
->result_to_output
,
613 output_semantic_name
,
614 output_semantic_index
);
617 debug_printf("%s: failed to translate Mesa program:\n", __func__
);
618 _mesa_print_program(&stvp
->Base
);
623 stvp
->state
.tokens
= ureg_get_tokens(ureg
, NULL
);
626 if (stvp
->glsl_to_tgsi
) {
627 stvp
->glsl_to_tgsi
= NULL
;
628 st_store_ir_in_disk_cache(st
, &stvp
->Base
, false);
633 * This must be done after the translation to TGSI is done, because
634 * we'll pass the NIR shader to the driver and the TGSI version to
635 * the draw module for the select/feedback/rasterpos code.
637 if (st
->pipe
->screen
->get_shader_param(st
->pipe
->screen
,
639 PIPE_SHADER_CAP_PREFERRED_IR
)) {
640 assert(!stvp
->glsl_to_tgsi
);
643 st_translate_prog_to_nir(st
, &stvp
->Base
, MESA_SHADER_VERTEX
);
645 if (stvp
->state
.ir
.nir
)
646 ralloc_free(stvp
->state
.ir
.nir
);
647 stvp
->state
.type
= PIPE_SHADER_IR_NIR
;
648 stvp
->state
.ir
.nir
= nir
;
649 stvp
->Base
.nir
= nir
;
653 return stvp
->state
.tokens
!= NULL
;
656 static const gl_state_index16 depth_range_state
[STATE_LENGTH
] =
657 { STATE_DEPTH_RANGE
};
659 static struct st_vp_variant
*
660 st_create_vp_variant(struct st_context
*st
,
661 struct st_vertex_program
*stvp
,
662 const struct st_common_variant_key
*key
)
664 struct st_vp_variant
*vpv
= CALLOC_STRUCT(st_vp_variant
);
665 struct pipe_context
*pipe
= st
->pipe
;
666 struct pipe_shader_state state
= {0};
668 static const gl_state_index16 point_size_state
[STATE_LENGTH
] =
669 { STATE_INTERNAL
, STATE_POINT_SIZE_CLAMPED
, 0 };
670 struct gl_program_parameter_list
*params
= stvp
->Base
.Parameters
;
673 vpv
->num_inputs
= stvp
->num_inputs
;
675 state
.stream_output
= stvp
->state
.stream_output
;
677 if (stvp
->state
.type
== PIPE_SHADER_IR_NIR
) {
678 state
.type
= PIPE_SHADER_IR_NIR
;
679 state
.ir
.nir
= nir_shader_clone(NULL
, stvp
->state
.ir
.nir
);
680 if (key
->clamp_color
)
681 NIR_PASS_V(state
.ir
.nir
, nir_lower_clamp_color_outputs
);
682 if (key
->passthrough_edgeflags
) {
683 NIR_PASS_V(state
.ir
.nir
, nir_lower_passthrough_edgeflags
);
687 if (key
->lower_point_size
) {
688 _mesa_add_state_reference(params
, point_size_state
);
689 NIR_PASS_V(state
.ir
.nir
, nir_lower_point_size_mov
,
693 if (key
->lower_ucp
) {
694 struct pipe_screen
*screen
= pipe
->screen
;
695 bool can_compact
= screen
->get_param(screen
,
696 PIPE_CAP_NIR_COMPACT_ARRAYS
);
698 bool use_eye
= st
->ctx
->_Shader
->CurrentProgram
[MESA_SHADER_VERTEX
] != NULL
;
699 gl_state_index16 clipplane_state
[MAX_CLIP_PLANES
][STATE_LENGTH
];
700 for (int i
= 0; i
< MAX_CLIP_PLANES
; ++i
) {
702 clipplane_state
[i
][0] = STATE_CLIPPLANE
;
703 clipplane_state
[i
][1] = i
;
705 clipplane_state
[i
][0] = STATE_INTERNAL
;
706 clipplane_state
[i
][1] = STATE_CLIP_INTERNAL
;
707 clipplane_state
[i
][2] = i
;
709 _mesa_add_state_reference(params
, clipplane_state
[i
]);
712 NIR_PASS_V(state
.ir
.nir
, nir_lower_clip_vs
, key
->lower_ucp
,
713 true, can_compact
, clipplane_state
);
714 NIR_PASS_V(state
.ir
.nir
, nir_lower_io_to_temporaries
,
715 nir_shader_get_entrypoint(state
.ir
.nir
), true, false);
718 st_finalize_nir(st
, &stvp
->Base
, stvp
->shader_program
,
721 vpv
->driver_shader
= pipe
->create_vs_state(pipe
, &state
);
723 /* When generating a NIR program, we usually don't have TGSI tokens.
724 * However, we do create them for ARB_vertex_program / fixed-function VS
725 * programs which we may need to use with the draw module for legacy
726 * feedback/select emulation. If they exist, copy them.
728 * TODO: Lowering for shader variants is not applied to TGSI when
729 * generating a NIR shader.
731 if (stvp
->state
.tokens
)
732 vpv
->tokens
= tgsi_dup_tokens(stvp
->state
.tokens
);
737 state
.type
= PIPE_SHADER_IR_TGSI
;
738 state
.tokens
= tgsi_dup_tokens(stvp
->state
.tokens
);
740 /* Emulate features. */
741 if (key
->clamp_color
|| key
->passthrough_edgeflags
) {
742 const struct tgsi_token
*tokens
;
744 (key
->clamp_color
? TGSI_EMU_CLAMP_COLOR_OUTPUTS
: 0) |
745 (key
->passthrough_edgeflags
? TGSI_EMU_PASSTHROUGH_EDGEFLAG
: 0);
747 tokens
= tgsi_emulate(state
.tokens
, flags
);
750 tgsi_free_tokens(state
.tokens
);
751 state
.tokens
= tokens
;
753 if (key
->passthrough_edgeflags
)
756 fprintf(stderr
, "mesa: cannot emulate deprecated features\n");
759 if (key
->lower_depth_clamp
) {
760 unsigned depth_range_const
=
761 _mesa_add_state_reference(params
, depth_range_state
);
763 const struct tgsi_token
*tokens
;
764 tokens
= st_tgsi_lower_depth_clamp(state
.tokens
, depth_range_const
,
765 key
->clip_negative_one_to_one
);
766 if (tokens
!= state
.tokens
)
767 tgsi_free_tokens(state
.tokens
);
768 state
.tokens
= tokens
;
771 if (ST_DEBUG
& DEBUG_TGSI
) {
772 tgsi_dump(state
.tokens
, 0);
776 vpv
->driver_shader
= pipe
->create_vs_state(pipe
, &state
);
777 /* Save this for selection/feedback/rasterpos. */
778 vpv
->tokens
= state
.tokens
;
784 * Find/create a vertex program variant.
786 struct st_vp_variant
*
787 st_get_vp_variant(struct st_context
*st
,
788 struct st_vertex_program
*stvp
,
789 const struct st_common_variant_key
*key
)
791 struct st_vp_variant
*vpv
;
793 /* Search for existing variant */
794 for (vpv
= stvp
->variants
; vpv
; vpv
= vpv
->next
) {
795 if (memcmp(&vpv
->key
, key
, sizeof(*key
)) == 0) {
802 vpv
= st_create_vp_variant(st
, stvp
, key
);
804 for (unsigned index
= 0; index
< vpv
->num_inputs
; ++index
) {
805 unsigned attr
= stvp
->index_to_input
[index
];
806 if (attr
== ST_DOUBLE_ATTRIB_PLACEHOLDER
)
808 vpv
->vert_attrib_mask
|= 1u << attr
;
811 /* insert into list */
812 vpv
->next
= stvp
->variants
;
813 stvp
->variants
= vpv
;
822 * Translate a Mesa fragment shader into a TGSI shader.
825 st_translate_fragment_program(struct st_context
*st
,
826 struct st_common_program
*stfp
)
828 /* Non-GLSL programs: */
829 if (!stfp
->glsl_to_tgsi
) {
830 _mesa_remove_output_reads(&stfp
->Base
, PROGRAM_OUTPUT
);
831 if (st
->ctx
->Const
.GLSLFragCoordIsSysVal
)
832 _mesa_program_fragment_position_to_sysval(&stfp
->Base
);
834 /* This determines which states will be updated when the assembly
837 * fragment.position and glDrawPixels always use constants.
839 stfp
->affected_states
= ST_NEW_FS_STATE
|
840 ST_NEW_SAMPLE_SHADING
|
844 /* Just set them for ATI_fs unconditionally. */
845 stfp
->affected_states
|= ST_NEW_FS_SAMPLER_VIEWS
|
849 if (stfp
->Base
.SamplersUsed
)
850 stfp
->affected_states
|= ST_NEW_FS_SAMPLER_VIEWS
|
854 /* Translate to NIR. */
856 st
->pipe
->screen
->get_shader_param(st
->pipe
->screen
,
857 PIPE_SHADER_FRAGMENT
,
858 PIPE_SHADER_CAP_PREFERRED_IR
)) {
860 st_translate_prog_to_nir(st
, &stfp
->Base
, MESA_SHADER_FRAGMENT
);
862 if (stfp
->state
.ir
.nir
)
863 ralloc_free(stfp
->state
.ir
.nir
);
864 stfp
->state
.type
= PIPE_SHADER_IR_NIR
;
865 stfp
->state
.ir
.nir
= nir
;
866 stfp
->Base
.nir
= nir
;
871 ubyte outputMapping
[2 * FRAG_RESULT_MAX
];
872 ubyte inputMapping
[VARYING_SLOT_MAX
];
873 ubyte inputSlotToAttr
[VARYING_SLOT_MAX
];
874 ubyte interpMode
[PIPE_MAX_SHADER_INPUTS
]; /* XXX size? */
876 GLbitfield64 inputsRead
;
877 struct ureg_program
*ureg
;
879 GLboolean write_all
= GL_FALSE
;
881 ubyte input_semantic_name
[PIPE_MAX_SHADER_INPUTS
];
882 ubyte input_semantic_index
[PIPE_MAX_SHADER_INPUTS
];
883 uint fs_num_inputs
= 0;
885 ubyte fs_output_semantic_name
[PIPE_MAX_SHADER_OUTPUTS
];
886 ubyte fs_output_semantic_index
[PIPE_MAX_SHADER_OUTPUTS
];
887 uint fs_num_outputs
= 0;
889 memset(inputSlotToAttr
, ~0, sizeof(inputSlotToAttr
));
892 * Convert Mesa program inputs to TGSI input register semantics.
894 inputsRead
= stfp
->Base
.info
.inputs_read
;
895 for (attr
= 0; attr
< VARYING_SLOT_MAX
; attr
++) {
896 if ((inputsRead
& BITFIELD64_BIT(attr
)) != 0) {
897 const GLuint slot
= fs_num_inputs
++;
899 inputMapping
[attr
] = slot
;
900 inputSlotToAttr
[slot
] = attr
;
903 case VARYING_SLOT_POS
:
904 input_semantic_name
[slot
] = TGSI_SEMANTIC_POSITION
;
905 input_semantic_index
[slot
] = 0;
906 interpMode
[slot
] = TGSI_INTERPOLATE_LINEAR
;
908 case VARYING_SLOT_COL0
:
909 input_semantic_name
[slot
] = TGSI_SEMANTIC_COLOR
;
910 input_semantic_index
[slot
] = 0;
911 interpMode
[slot
] = stfp
->glsl_to_tgsi
?
912 TGSI_INTERPOLATE_COUNT
: TGSI_INTERPOLATE_COLOR
;
914 case VARYING_SLOT_COL1
:
915 input_semantic_name
[slot
] = TGSI_SEMANTIC_COLOR
;
916 input_semantic_index
[slot
] = 1;
917 interpMode
[slot
] = stfp
->glsl_to_tgsi
?
918 TGSI_INTERPOLATE_COUNT
: TGSI_INTERPOLATE_COLOR
;
920 case VARYING_SLOT_FOGC
:
921 input_semantic_name
[slot
] = TGSI_SEMANTIC_FOG
;
922 input_semantic_index
[slot
] = 0;
923 interpMode
[slot
] = TGSI_INTERPOLATE_PERSPECTIVE
;
925 case VARYING_SLOT_FACE
:
926 input_semantic_name
[slot
] = TGSI_SEMANTIC_FACE
;
927 input_semantic_index
[slot
] = 0;
928 interpMode
[slot
] = TGSI_INTERPOLATE_CONSTANT
;
930 case VARYING_SLOT_PRIMITIVE_ID
:
931 input_semantic_name
[slot
] = TGSI_SEMANTIC_PRIMID
;
932 input_semantic_index
[slot
] = 0;
933 interpMode
[slot
] = TGSI_INTERPOLATE_CONSTANT
;
935 case VARYING_SLOT_LAYER
:
936 input_semantic_name
[slot
] = TGSI_SEMANTIC_LAYER
;
937 input_semantic_index
[slot
] = 0;
938 interpMode
[slot
] = TGSI_INTERPOLATE_CONSTANT
;
940 case VARYING_SLOT_VIEWPORT
:
941 input_semantic_name
[slot
] = TGSI_SEMANTIC_VIEWPORT_INDEX
;
942 input_semantic_index
[slot
] = 0;
943 interpMode
[slot
] = TGSI_INTERPOLATE_CONSTANT
;
945 case VARYING_SLOT_CLIP_DIST0
:
946 input_semantic_name
[slot
] = TGSI_SEMANTIC_CLIPDIST
;
947 input_semantic_index
[slot
] = 0;
948 interpMode
[slot
] = TGSI_INTERPOLATE_PERSPECTIVE
;
950 case VARYING_SLOT_CLIP_DIST1
:
951 input_semantic_name
[slot
] = TGSI_SEMANTIC_CLIPDIST
;
952 input_semantic_index
[slot
] = 1;
953 interpMode
[slot
] = TGSI_INTERPOLATE_PERSPECTIVE
;
955 case VARYING_SLOT_CULL_DIST0
:
956 case VARYING_SLOT_CULL_DIST1
:
957 /* these should have been lowered by GLSL */
960 /* In most cases, there is nothing special about these
961 * inputs, so adopt a convention to use the generic
962 * semantic name and the mesa VARYING_SLOT_ number as the
965 * All that is required is that the vertex shader labels
966 * its own outputs similarly, and that the vertex shader
967 * generates at least every output required by the
968 * fragment shader plus fixed-function hardware (such as
971 * However, some drivers may need us to identify the PNTC and TEXi
972 * varyings if, for example, their capability to replace them with
973 * sprite coordinates is limited.
975 case VARYING_SLOT_PNTC
:
976 if (st
->needs_texcoord_semantic
) {
977 input_semantic_name
[slot
] = TGSI_SEMANTIC_PCOORD
;
978 input_semantic_index
[slot
] = 0;
979 interpMode
[slot
] = TGSI_INTERPOLATE_LINEAR
;
983 case VARYING_SLOT_TEX0
:
984 case VARYING_SLOT_TEX1
:
985 case VARYING_SLOT_TEX2
:
986 case VARYING_SLOT_TEX3
:
987 case VARYING_SLOT_TEX4
:
988 case VARYING_SLOT_TEX5
:
989 case VARYING_SLOT_TEX6
:
990 case VARYING_SLOT_TEX7
:
991 if (st
->needs_texcoord_semantic
) {
992 input_semantic_name
[slot
] = TGSI_SEMANTIC_TEXCOORD
;
993 input_semantic_index
[slot
] = attr
- VARYING_SLOT_TEX0
;
994 interpMode
[slot
] = stfp
->glsl_to_tgsi
?
995 TGSI_INTERPOLATE_COUNT
: TGSI_INTERPOLATE_PERSPECTIVE
;
999 case VARYING_SLOT_VAR0
:
1001 /* Semantic indices should be zero-based because drivers may choose
1002 * to assign a fixed slot determined by that index.
1003 * This is useful because ARB_separate_shader_objects uses location
1004 * qualifiers for linkage, and if the semantic index corresponds to
1005 * these locations, linkage passes in the driver become unecessary.
1007 * If needs_texcoord_semantic is true, no semantic indices will be
1008 * consumed for the TEXi varyings, and we can base the locations of
1009 * the user varyings on VAR0. Otherwise, we use TEX0 as base index.
1011 assert(attr
>= VARYING_SLOT_VAR0
|| attr
== VARYING_SLOT_PNTC
||
1012 (attr
>= VARYING_SLOT_TEX0
&& attr
<= VARYING_SLOT_TEX7
));
1013 input_semantic_name
[slot
] = TGSI_SEMANTIC_GENERIC
;
1014 input_semantic_index
[slot
] = st_get_generic_varying_index(st
, attr
);
1015 if (attr
== VARYING_SLOT_PNTC
)
1016 interpMode
[slot
] = TGSI_INTERPOLATE_LINEAR
;
1018 interpMode
[slot
] = stfp
->glsl_to_tgsi
?
1019 TGSI_INTERPOLATE_COUNT
: TGSI_INTERPOLATE_PERSPECTIVE
;
1025 inputMapping
[attr
] = -1;
1030 * Semantics and mapping for outputs
1032 GLbitfield64 outputsWritten
= stfp
->Base
.info
.outputs_written
;
1034 /* if z is written, emit that first */
1035 if (outputsWritten
& BITFIELD64_BIT(FRAG_RESULT_DEPTH
)) {
1036 fs_output_semantic_name
[fs_num_outputs
] = TGSI_SEMANTIC_POSITION
;
1037 fs_output_semantic_index
[fs_num_outputs
] = 0;
1038 outputMapping
[FRAG_RESULT_DEPTH
] = fs_num_outputs
;
1040 outputsWritten
&= ~(1 << FRAG_RESULT_DEPTH
);
1043 if (outputsWritten
& BITFIELD64_BIT(FRAG_RESULT_STENCIL
)) {
1044 fs_output_semantic_name
[fs_num_outputs
] = TGSI_SEMANTIC_STENCIL
;
1045 fs_output_semantic_index
[fs_num_outputs
] = 0;
1046 outputMapping
[FRAG_RESULT_STENCIL
] = fs_num_outputs
;
1048 outputsWritten
&= ~(1 << FRAG_RESULT_STENCIL
);
1051 if (outputsWritten
& BITFIELD64_BIT(FRAG_RESULT_SAMPLE_MASK
)) {
1052 fs_output_semantic_name
[fs_num_outputs
] = TGSI_SEMANTIC_SAMPLEMASK
;
1053 fs_output_semantic_index
[fs_num_outputs
] = 0;
1054 outputMapping
[FRAG_RESULT_SAMPLE_MASK
] = fs_num_outputs
;
1056 outputsWritten
&= ~(1 << FRAG_RESULT_SAMPLE_MASK
);
1059 /* handle remaining outputs (color) */
1060 for (attr
= 0; attr
< ARRAY_SIZE(outputMapping
); attr
++) {
1061 const GLbitfield64 written
= attr
< FRAG_RESULT_MAX
? outputsWritten
:
1062 stfp
->Base
.SecondaryOutputsWritten
;
1063 const unsigned loc
= attr
% FRAG_RESULT_MAX
;
1065 if (written
& BITFIELD64_BIT(loc
)) {
1067 case FRAG_RESULT_DEPTH
:
1068 case FRAG_RESULT_STENCIL
:
1069 case FRAG_RESULT_SAMPLE_MASK
:
1073 case FRAG_RESULT_COLOR
:
1074 write_all
= GL_TRUE
; /* fallthrough */
1077 assert(loc
== FRAG_RESULT_COLOR
||
1078 (FRAG_RESULT_DATA0
<= loc
&& loc
< FRAG_RESULT_MAX
));
1080 index
= (loc
== FRAG_RESULT_COLOR
) ? 0 : (loc
- FRAG_RESULT_DATA0
);
1082 if (attr
>= FRAG_RESULT_MAX
) {
1083 /* Secondary color for dual source blending. */
1088 fs_output_semantic_name
[fs_num_outputs
] = TGSI_SEMANTIC_COLOR
;
1089 fs_output_semantic_index
[fs_num_outputs
] = index
;
1090 outputMapping
[attr
] = fs_num_outputs
;
1099 ureg
= ureg_create_with_screen(PIPE_SHADER_FRAGMENT
, st
->pipe
->screen
);
1103 if (ST_DEBUG
& DEBUG_MESA
) {
1104 _mesa_print_program(&stfp
->Base
);
1105 _mesa_print_program_parameters(st
->ctx
, &stfp
->Base
);
1108 if (write_all
== GL_TRUE
)
1109 ureg_property(ureg
, TGSI_PROPERTY_FS_COLOR0_WRITES_ALL_CBUFS
, 1);
1111 if (stfp
->Base
.info
.fs
.depth_layout
!= FRAG_DEPTH_LAYOUT_NONE
) {
1112 switch (stfp
->Base
.info
.fs
.depth_layout
) {
1113 case FRAG_DEPTH_LAYOUT_ANY
:
1114 ureg_property(ureg
, TGSI_PROPERTY_FS_DEPTH_LAYOUT
,
1115 TGSI_FS_DEPTH_LAYOUT_ANY
);
1117 case FRAG_DEPTH_LAYOUT_GREATER
:
1118 ureg_property(ureg
, TGSI_PROPERTY_FS_DEPTH_LAYOUT
,
1119 TGSI_FS_DEPTH_LAYOUT_GREATER
);
1121 case FRAG_DEPTH_LAYOUT_LESS
:
1122 ureg_property(ureg
, TGSI_PROPERTY_FS_DEPTH_LAYOUT
,
1123 TGSI_FS_DEPTH_LAYOUT_LESS
);
1125 case FRAG_DEPTH_LAYOUT_UNCHANGED
:
1126 ureg_property(ureg
, TGSI_PROPERTY_FS_DEPTH_LAYOUT
,
1127 TGSI_FS_DEPTH_LAYOUT_UNCHANGED
);
1134 if (stfp
->glsl_to_tgsi
) {
1135 st_translate_program(st
->ctx
,
1136 PIPE_SHADER_FRAGMENT
,
1144 input_semantic_name
,
1145 input_semantic_index
,
1150 fs_output_semantic_name
,
1151 fs_output_semantic_index
);
1153 free_glsl_to_tgsi_visitor(stfp
->glsl_to_tgsi
);
1154 } else if (stfp
->ati_fs
)
1155 st_translate_atifs_program(ureg
,
1161 input_semantic_name
,
1162 input_semantic_index
,
1167 fs_output_semantic_name
,
1168 fs_output_semantic_index
);
1170 st_translate_mesa_program(st
->ctx
,
1171 PIPE_SHADER_FRAGMENT
,
1177 input_semantic_name
,
1178 input_semantic_index
,
1183 fs_output_semantic_name
,
1184 fs_output_semantic_index
);
1186 stfp
->state
.tokens
= ureg_get_tokens(ureg
, NULL
);
1189 if (stfp
->glsl_to_tgsi
) {
1190 stfp
->glsl_to_tgsi
= NULL
;
1191 st_store_ir_in_disk_cache(st
, &stfp
->Base
, false);
1194 return stfp
->state
.tokens
!= NULL
;
1197 static struct st_fp_variant
*
1198 st_create_fp_variant(struct st_context
*st
,
1199 struct st_common_program
*stfp
,
1200 const struct st_fp_variant_key
*key
)
1202 struct pipe_context
*pipe
= st
->pipe
;
1203 struct st_fp_variant
*variant
= CALLOC_STRUCT(st_fp_variant
);
1204 struct pipe_shader_state state
= {0};
1205 struct gl_program_parameter_list
*params
= stfp
->Base
.Parameters
;
1206 static const gl_state_index16 texcoord_state
[STATE_LENGTH
] =
1207 { STATE_INTERNAL
, STATE_CURRENT_ATTRIB
, VERT_ATTRIB_TEX0
};
1208 static const gl_state_index16 scale_state
[STATE_LENGTH
] =
1209 { STATE_INTERNAL
, STATE_PT_SCALE
};
1210 static const gl_state_index16 bias_state
[STATE_LENGTH
] =
1211 { STATE_INTERNAL
, STATE_PT_BIAS
};
1212 static const gl_state_index16 alpha_ref_state
[STATE_LENGTH
] =
1213 { STATE_INTERNAL
, STATE_ALPHA_REF
};
1218 if (stfp
->state
.type
== PIPE_SHADER_IR_NIR
) {
1219 state
.type
= PIPE_SHADER_IR_NIR
;
1220 state
.ir
.nir
= nir_shader_clone(NULL
, stfp
->state
.ir
.nir
);
1222 if (key
->clamp_color
)
1223 NIR_PASS_V(state
.ir
.nir
, nir_lower_clamp_color_outputs
);
1225 if (key
->lower_flatshade
)
1226 NIR_PASS_V(state
.ir
.nir
, nir_lower_flatshade
);
1228 if (key
->lower_alpha_func
!= COMPARE_FUNC_NEVER
) {
1229 _mesa_add_state_reference(params
, alpha_ref_state
);
1230 NIR_PASS_V(state
.ir
.nir
, nir_lower_alpha_test
, key
->lower_alpha_func
,
1231 false, alpha_ref_state
);
1234 if (key
->lower_two_sided_color
)
1235 NIR_PASS_V(state
.ir
.nir
, nir_lower_two_sided_color
);
1237 if (key
->persample_shading
) {
1238 nir_shader
*shader
= state
.ir
.nir
;
1239 nir_foreach_variable(var
, &shader
->inputs
)
1240 var
->data
.sample
= true;
1243 assert(!(key
->bitmap
&& key
->drawpixels
));
1247 nir_lower_bitmap_options options
= {0};
1249 variant
->bitmap_sampler
= ffs(~stfp
->Base
.SamplersUsed
) - 1;
1250 options
.sampler
= variant
->bitmap_sampler
;
1251 options
.swizzle_xxxx
= st
->bitmap
.tex_format
== PIPE_FORMAT_R8_UNORM
;
1253 NIR_PASS_V(state
.ir
.nir
, nir_lower_bitmap
, &options
);
1256 /* glDrawPixels (color only) */
1257 if (key
->drawpixels
) {
1258 nir_lower_drawpixels_options options
= {{0}};
1259 unsigned samplers_used
= stfp
->Base
.SamplersUsed
;
1261 /* Find the first unused slot. */
1262 variant
->drawpix_sampler
= ffs(~samplers_used
) - 1;
1263 options
.drawpix_sampler
= variant
->drawpix_sampler
;
1264 samplers_used
|= (1 << variant
->drawpix_sampler
);
1266 options
.pixel_maps
= key
->pixelMaps
;
1267 if (key
->pixelMaps
) {
1268 variant
->pixelmap_sampler
= ffs(~samplers_used
) - 1;
1269 options
.pixelmap_sampler
= variant
->pixelmap_sampler
;
1272 options
.scale_and_bias
= key
->scaleAndBias
;
1273 if (key
->scaleAndBias
) {
1274 _mesa_add_state_reference(params
, scale_state
);
1275 memcpy(options
.scale_state_tokens
, scale_state
,
1276 sizeof(options
.scale_state_tokens
));
1277 _mesa_add_state_reference(params
, bias_state
);
1278 memcpy(options
.bias_state_tokens
, bias_state
,
1279 sizeof(options
.bias_state_tokens
));
1282 _mesa_add_state_reference(params
, texcoord_state
);
1283 memcpy(options
.texcoord_state_tokens
, texcoord_state
,
1284 sizeof(options
.texcoord_state_tokens
));
1286 NIR_PASS_V(state
.ir
.nir
, nir_lower_drawpixels
, &options
);
1289 if (unlikely(key
->external
.lower_nv12
|| key
->external
.lower_iyuv
||
1290 key
->external
.lower_xy_uxvx
|| key
->external
.lower_yx_xuxv
||
1291 key
->external
.lower_ayuv
|| key
->external
.lower_xyuv
)) {
1292 nir_lower_tex_options options
= {0};
1293 options
.lower_y_uv_external
= key
->external
.lower_nv12
;
1294 options
.lower_y_u_v_external
= key
->external
.lower_iyuv
;
1295 options
.lower_xy_uxvx_external
= key
->external
.lower_xy_uxvx
;
1296 options
.lower_yx_xuxv_external
= key
->external
.lower_yx_xuxv
;
1297 options
.lower_ayuv_external
= key
->external
.lower_ayuv
;
1298 options
.lower_xyuv_external
= key
->external
.lower_xyuv
;
1299 NIR_PASS_V(state
.ir
.nir
, nir_lower_tex
, &options
);
1302 st_finalize_nir(st
, &stfp
->Base
, stfp
->shader_program
, state
.ir
.nir
);
1304 if (unlikely(key
->external
.lower_nv12
|| key
->external
.lower_iyuv
||
1305 key
->external
.lower_xy_uxvx
|| key
->external
.lower_yx_xuxv
)) {
1306 /* This pass needs to happen *after* nir_lower_sampler */
1307 NIR_PASS_V(state
.ir
.nir
, st_nir_lower_tex_src_plane
,
1308 ~stfp
->Base
.SamplersUsed
,
1309 key
->external
.lower_nv12
|| key
->external
.lower_xy_uxvx
||
1310 key
->external
.lower_yx_xuxv
,
1311 key
->external
.lower_iyuv
);
1314 /* Some of the lowering above may have introduced new varyings */
1315 nir_shader_gather_info(state
.ir
.nir
,
1316 nir_shader_get_entrypoint(state
.ir
.nir
));
1318 variant
->driver_shader
= pipe
->create_fs_state(pipe
, &state
);
1319 variant
->key
= *key
;
1324 state
.tokens
= stfp
->state
.tokens
;
1326 assert(!(key
->bitmap
&& key
->drawpixels
));
1328 /* Fix texture targets and add fog for ATI_fs */
1330 const struct tgsi_token
*tokens
= st_fixup_atifs(state
.tokens
, key
);
1333 state
.tokens
= tokens
;
1335 fprintf(stderr
, "mesa: cannot post-process ATI_fs\n");
1338 /* Emulate features. */
1339 if (key
->clamp_color
|| key
->persample_shading
) {
1340 const struct tgsi_token
*tokens
;
1342 (key
->clamp_color
? TGSI_EMU_CLAMP_COLOR_OUTPUTS
: 0) |
1343 (key
->persample_shading
? TGSI_EMU_FORCE_PERSAMPLE_INTERP
: 0);
1345 tokens
= tgsi_emulate(state
.tokens
, flags
);
1348 if (state
.tokens
!= stfp
->state
.tokens
)
1349 tgsi_free_tokens(state
.tokens
);
1350 state
.tokens
= tokens
;
1352 fprintf(stderr
, "mesa: cannot emulate deprecated features\n");
1357 const struct tgsi_token
*tokens
;
1359 variant
->bitmap_sampler
= ffs(~stfp
->Base
.SamplersUsed
) - 1;
1361 tokens
= st_get_bitmap_shader(state
.tokens
,
1362 st
->internal_target
,
1363 variant
->bitmap_sampler
,
1364 st
->needs_texcoord_semantic
,
1365 st
->bitmap
.tex_format
==
1366 PIPE_FORMAT_R8_UNORM
);
1369 if (state
.tokens
!= stfp
->state
.tokens
)
1370 tgsi_free_tokens(state
.tokens
);
1371 state
.tokens
= tokens
;
1373 fprintf(stderr
, "mesa: cannot create a shader for glBitmap\n");
1376 /* glDrawPixels (color only) */
1377 if (key
->drawpixels
) {
1378 const struct tgsi_token
*tokens
;
1379 unsigned scale_const
= 0, bias_const
= 0, texcoord_const
= 0;
1381 /* Find the first unused slot. */
1382 variant
->drawpix_sampler
= ffs(~stfp
->Base
.SamplersUsed
) - 1;
1384 if (key
->pixelMaps
) {
1385 unsigned samplers_used
= stfp
->Base
.SamplersUsed
|
1386 (1 << variant
->drawpix_sampler
);
1388 variant
->pixelmap_sampler
= ffs(~samplers_used
) - 1;
1391 if (key
->scaleAndBias
) {
1392 scale_const
= _mesa_add_state_reference(params
, scale_state
);
1393 bias_const
= _mesa_add_state_reference(params
, bias_state
);
1396 texcoord_const
= _mesa_add_state_reference(params
, texcoord_state
);
1398 tokens
= st_get_drawpix_shader(state
.tokens
,
1399 st
->needs_texcoord_semantic
,
1400 key
->scaleAndBias
, scale_const
,
1401 bias_const
, key
->pixelMaps
,
1402 variant
->drawpix_sampler
,
1403 variant
->pixelmap_sampler
,
1404 texcoord_const
, st
->internal_target
);
1407 if (state
.tokens
!= stfp
->state
.tokens
)
1408 tgsi_free_tokens(state
.tokens
);
1409 state
.tokens
= tokens
;
1411 fprintf(stderr
, "mesa: cannot create a shader for glDrawPixels\n");
1414 if (unlikely(key
->external
.lower_nv12
|| key
->external
.lower_iyuv
||
1415 key
->external
.lower_xy_uxvx
|| key
->external
.lower_yx_xuxv
)) {
1416 const struct tgsi_token
*tokens
;
1418 /* samplers inserted would conflict, but this should be unpossible: */
1419 assert(!(key
->bitmap
|| key
->drawpixels
));
1421 tokens
= st_tgsi_lower_yuv(state
.tokens
,
1422 ~stfp
->Base
.SamplersUsed
,
1423 key
->external
.lower_nv12
||
1424 key
->external
.lower_xy_uxvx
||
1425 key
->external
.lower_yx_xuxv
,
1426 key
->external
.lower_iyuv
);
1428 if (state
.tokens
!= stfp
->state
.tokens
)
1429 tgsi_free_tokens(state
.tokens
);
1430 state
.tokens
= tokens
;
1432 fprintf(stderr
, "mesa: cannot create a shader for samplerExternalOES\n");
1436 if (key
->lower_depth_clamp
) {
1437 unsigned depth_range_const
= _mesa_add_state_reference(params
, depth_range_state
);
1439 const struct tgsi_token
*tokens
;
1440 tokens
= st_tgsi_lower_depth_clamp_fs(state
.tokens
, depth_range_const
);
1441 if (state
.tokens
!= stfp
->state
.tokens
)
1442 tgsi_free_tokens(state
.tokens
);
1443 state
.tokens
= tokens
;
1446 if (ST_DEBUG
& DEBUG_TGSI
) {
1447 tgsi_dump(state
.tokens
, 0);
1451 /* fill in variant */
1452 variant
->driver_shader
= pipe
->create_fs_state(pipe
, &state
);
1453 variant
->key
= *key
;
1455 if (state
.tokens
!= stfp
->state
.tokens
)
1456 tgsi_free_tokens(state
.tokens
);
1461 * Translate fragment program if needed.
1463 struct st_fp_variant
*
1464 st_get_fp_variant(struct st_context
*st
,
1465 struct st_common_program
*stfp
,
1466 const struct st_fp_variant_key
*key
)
1468 struct st_fp_variant
*fpv
;
1470 /* Search for existing variant */
1471 for (fpv
= stfp
->fp_variants
; fpv
; fpv
= fpv
->next
) {
1472 if (memcmp(&fpv
->key
, key
, sizeof(*key
)) == 0) {
1479 fpv
= st_create_fp_variant(st
, stfp
, key
);
1481 if (key
->bitmap
|| key
->drawpixels
) {
1482 /* Regular variants should always come before the
1483 * bitmap & drawpixels variants, (unless there
1484 * are no regular variants) so that
1485 * st_update_fp can take a fast path when
1486 * shader_has_one_variant is set.
1488 if (!stfp
->fp_variants
) {
1489 stfp
->fp_variants
= fpv
;
1491 /* insert into list after the first one */
1492 fpv
->next
= stfp
->fp_variants
->next
;
1493 stfp
->fp_variants
->next
= fpv
;
1496 /* insert into list */
1497 fpv
->next
= stfp
->fp_variants
;
1498 stfp
->fp_variants
= fpv
;
1507 * Translate a program. This is common code for geometry and tessellation
1511 st_translate_common_program(struct st_context
*st
,
1512 struct st_common_program
*stcp
)
1514 struct gl_program
*prog
= &stcp
->Base
;
1515 enum pipe_shader_type stage
=
1516 pipe_shader_type_from_mesa(stcp
->Base
.info
.stage
);
1517 struct ureg_program
*ureg
= ureg_create_with_screen(stage
, st
->pipe
->screen
);
1523 case PIPE_SHADER_TESS_CTRL
:
1524 ureg_property(ureg
, TGSI_PROPERTY_TCS_VERTICES_OUT
,
1525 stcp
->Base
.info
.tess
.tcs_vertices_out
);
1528 case PIPE_SHADER_TESS_EVAL
:
1529 if (stcp
->Base
.info
.tess
.primitive_mode
== GL_ISOLINES
)
1530 ureg_property(ureg
, TGSI_PROPERTY_TES_PRIM_MODE
, GL_LINES
);
1532 ureg_property(ureg
, TGSI_PROPERTY_TES_PRIM_MODE
,
1533 stcp
->Base
.info
.tess
.primitive_mode
);
1535 STATIC_ASSERT((TESS_SPACING_EQUAL
+ 1) % 3 == PIPE_TESS_SPACING_EQUAL
);
1536 STATIC_ASSERT((TESS_SPACING_FRACTIONAL_ODD
+ 1) % 3 ==
1537 PIPE_TESS_SPACING_FRACTIONAL_ODD
);
1538 STATIC_ASSERT((TESS_SPACING_FRACTIONAL_EVEN
+ 1) % 3 ==
1539 PIPE_TESS_SPACING_FRACTIONAL_EVEN
);
1541 ureg_property(ureg
, TGSI_PROPERTY_TES_SPACING
,
1542 (stcp
->Base
.info
.tess
.spacing
+ 1) % 3);
1544 ureg_property(ureg
, TGSI_PROPERTY_TES_VERTEX_ORDER_CW
,
1545 !stcp
->Base
.info
.tess
.ccw
);
1546 ureg_property(ureg
, TGSI_PROPERTY_TES_POINT_MODE
,
1547 stcp
->Base
.info
.tess
.point_mode
);
1550 case PIPE_SHADER_GEOMETRY
:
1551 ureg_property(ureg
, TGSI_PROPERTY_GS_INPUT_PRIM
,
1552 stcp
->Base
.info
.gs
.input_primitive
);
1553 ureg_property(ureg
, TGSI_PROPERTY_GS_OUTPUT_PRIM
,
1554 stcp
->Base
.info
.gs
.output_primitive
);
1555 ureg_property(ureg
, TGSI_PROPERTY_GS_MAX_OUTPUT_VERTICES
,
1556 stcp
->Base
.info
.gs
.vertices_out
);
1557 ureg_property(ureg
, TGSI_PROPERTY_GS_INVOCATIONS
,
1558 stcp
->Base
.info
.gs
.invocations
);
1565 ubyte inputSlotToAttr
[VARYING_SLOT_TESS_MAX
];
1566 ubyte inputMapping
[VARYING_SLOT_TESS_MAX
];
1567 ubyte outputMapping
[VARYING_SLOT_TESS_MAX
];
1570 ubyte input_semantic_name
[PIPE_MAX_SHADER_INPUTS
];
1571 ubyte input_semantic_index
[PIPE_MAX_SHADER_INPUTS
];
1572 uint num_inputs
= 0;
1574 ubyte output_semantic_name
[PIPE_MAX_SHADER_OUTPUTS
];
1575 ubyte output_semantic_index
[PIPE_MAX_SHADER_OUTPUTS
];
1576 uint num_outputs
= 0;
1580 memset(inputSlotToAttr
, 0, sizeof(inputSlotToAttr
));
1581 memset(inputMapping
, 0, sizeof(inputMapping
));
1582 memset(outputMapping
, 0, sizeof(outputMapping
));
1583 memset(&stcp
->state
, 0, sizeof(stcp
->state
));
1585 if (prog
->info
.clip_distance_array_size
)
1586 ureg_property(ureg
, TGSI_PROPERTY_NUM_CLIPDIST_ENABLED
,
1587 prog
->info
.clip_distance_array_size
);
1588 if (prog
->info
.cull_distance_array_size
)
1589 ureg_property(ureg
, TGSI_PROPERTY_NUM_CULLDIST_ENABLED
,
1590 prog
->info
.cull_distance_array_size
);
1593 * Convert Mesa program inputs to TGSI input register semantics.
1595 for (attr
= 0; attr
< VARYING_SLOT_MAX
; attr
++) {
1596 if ((prog
->info
.inputs_read
& BITFIELD64_BIT(attr
)) == 0)
1599 unsigned slot
= num_inputs
++;
1601 inputMapping
[attr
] = slot
;
1602 inputSlotToAttr
[slot
] = attr
;
1604 unsigned semantic_name
, semantic_index
;
1605 tgsi_get_gl_varying_semantic(attr
, st
->needs_texcoord_semantic
,
1606 &semantic_name
, &semantic_index
);
1607 input_semantic_name
[slot
] = semantic_name
;
1608 input_semantic_index
[slot
] = semantic_index
;
1611 /* Also add patch inputs. */
1612 for (attr
= 0; attr
< 32; attr
++) {
1613 if (prog
->info
.patch_inputs_read
& (1u << attr
)) {
1614 GLuint slot
= num_inputs
++;
1615 GLuint patch_attr
= VARYING_SLOT_PATCH0
+ attr
;
1617 inputMapping
[patch_attr
] = slot
;
1618 inputSlotToAttr
[slot
] = patch_attr
;
1619 input_semantic_name
[slot
] = TGSI_SEMANTIC_PATCH
;
1620 input_semantic_index
[slot
] = attr
;
1624 /* initialize output semantics to defaults */
1625 for (i
= 0; i
< PIPE_MAX_SHADER_OUTPUTS
; i
++) {
1626 output_semantic_name
[i
] = TGSI_SEMANTIC_GENERIC
;
1627 output_semantic_index
[i
] = 0;
1631 * Determine number of outputs, the (default) output register
1632 * mapping and the semantic information for each output.
1634 for (attr
= 0; attr
< VARYING_SLOT_MAX
; attr
++) {
1635 if (prog
->info
.outputs_written
& BITFIELD64_BIT(attr
)) {
1636 GLuint slot
= num_outputs
++;
1638 outputMapping
[attr
] = slot
;
1640 unsigned semantic_name
, semantic_index
;
1641 tgsi_get_gl_varying_semantic(attr
, st
->needs_texcoord_semantic
,
1642 &semantic_name
, &semantic_index
);
1643 output_semantic_name
[slot
] = semantic_name
;
1644 output_semantic_index
[slot
] = semantic_index
;
1648 /* Also add patch outputs. */
1649 for (attr
= 0; attr
< 32; attr
++) {
1650 if (prog
->info
.patch_outputs_written
& (1u << attr
)) {
1651 GLuint slot
= num_outputs
++;
1652 GLuint patch_attr
= VARYING_SLOT_PATCH0
+ attr
;
1654 outputMapping
[patch_attr
] = slot
;
1655 output_semantic_name
[slot
] = TGSI_SEMANTIC_PATCH
;
1656 output_semantic_index
[slot
] = attr
;
1660 st_translate_program(st
->ctx
,
1669 input_semantic_name
,
1670 input_semantic_index
,
1675 output_semantic_name
,
1676 output_semantic_index
);
1678 stcp
->state
.tokens
= ureg_get_tokens(ureg
, NULL
);
1682 st_translate_stream_output_info(prog
);
1684 st_store_ir_in_disk_cache(st
, prog
, false);
1686 if ((ST_DEBUG
& DEBUG_TGSI
) && (ST_DEBUG
& DEBUG_MESA
)) {
1687 _mesa_print_program(prog
);
1691 if (ST_DEBUG
& DEBUG_TGSI
) {
1692 tgsi_dump(stcp
->state
.tokens
, 0);
1696 free_glsl_to_tgsi_visitor(stcp
->glsl_to_tgsi
);
1697 stcp
->glsl_to_tgsi
= NULL
;
1703 * Get/create a basic program variant.
1705 struct st_common_variant
*
1706 st_get_common_variant(struct st_context
*st
,
1707 struct st_common_program
*prog
,
1708 const struct st_common_variant_key
*key
)
1710 struct pipe_context
*pipe
= st
->pipe
;
1711 struct st_common_variant
*v
;
1712 struct pipe_shader_state state
= {0};
1714 /* Search for existing variant */
1715 for (v
= prog
->variants
; v
; v
= v
->next
) {
1716 if (memcmp(&v
->key
, key
, sizeof(*key
)) == 0) {
1723 v
= CALLOC_STRUCT(st_common_variant
);
1726 if (prog
->state
.type
== PIPE_SHADER_IR_NIR
) {
1727 state
.type
= PIPE_SHADER_IR_NIR
;
1728 state
.ir
.nir
= nir_shader_clone(NULL
, prog
->state
.ir
.nir
);
1730 if (key
->clamp_color
)
1731 NIR_PASS_V(state
.ir
.nir
, nir_lower_clamp_color_outputs
);
1733 state
.stream_output
= prog
->state
.stream_output
;
1735 st_finalize_nir(st
, &prog
->Base
, prog
->shader_program
,
1738 if (key
->lower_depth_clamp
) {
1739 struct gl_program_parameter_list
*params
= prog
->Base
.Parameters
;
1741 unsigned depth_range_const
=
1742 _mesa_add_state_reference(params
, depth_range_state
);
1744 const struct tgsi_token
*tokens
;
1746 st_tgsi_lower_depth_clamp(prog
->state
.tokens
,
1748 key
->clip_negative_one_to_one
);
1750 if (tokens
!= prog
->state
.tokens
)
1751 tgsi_free_tokens(prog
->state
.tokens
);
1753 prog
->state
.tokens
= tokens
;
1755 state
= prog
->state
;
1757 /* fill in new variant */
1758 switch (prog
->Base
.info
.stage
) {
1759 case MESA_SHADER_TESS_CTRL
:
1760 v
->driver_shader
= pipe
->create_tcs_state(pipe
, &state
);
1762 case MESA_SHADER_TESS_EVAL
:
1763 v
->driver_shader
= pipe
->create_tes_state(pipe
, &state
);
1765 case MESA_SHADER_GEOMETRY
:
1766 v
->driver_shader
= pipe
->create_gs_state(pipe
, &state
);
1768 case MESA_SHADER_COMPUTE
: {
1769 struct pipe_compute_state cs
= {0};
1770 cs
.ir_type
= state
.type
;
1771 cs
.req_local_mem
= prog
->Base
.info
.cs
.shared_size
;
1773 if (state
.type
== PIPE_SHADER_IR_NIR
)
1774 cs
.prog
= state
.ir
.nir
;
1776 cs
.prog
= state
.tokens
;
1778 v
->driver_shader
= pipe
->create_compute_state(pipe
, &cs
);
1782 assert(!"unhandled shader type");
1789 /* insert into list */
1790 v
->next
= prog
->variants
;
1800 * Vert/Geom/Frag programs have per-context variants. Free all the
1801 * variants attached to the given program which match the given context.
1804 destroy_program_variants(struct st_context
*st
, struct gl_program
*target
)
1806 if (!target
|| target
== &_mesa_DummyProgram
)
1809 switch (target
->Target
) {
1810 case GL_VERTEX_PROGRAM_ARB
:
1812 struct st_vertex_program
*stvp
= (struct st_vertex_program
*) target
;
1813 struct st_vp_variant
*vpv
, **prevPtr
= &stvp
->variants
;
1815 for (vpv
= stvp
->variants
; vpv
; ) {
1816 struct st_vp_variant
*next
= vpv
->next
;
1817 if (vpv
->key
.st
== st
) {
1818 /* unlink from list */
1820 /* destroy this variant */
1821 delete_vp_variant(st
, vpv
);
1824 prevPtr
= &vpv
->next
;
1830 case GL_FRAGMENT_PROGRAM_ARB
:
1832 struct st_common_program
*stfp
=
1833 (struct st_common_program
*) target
;
1834 struct st_fp_variant
*fpv
, **prevPtr
= &stfp
->fp_variants
;
1836 for (fpv
= stfp
->fp_variants
; fpv
; ) {
1837 struct st_fp_variant
*next
= fpv
->next
;
1838 if (fpv
->key
.st
== st
) {
1839 /* unlink from list */
1841 /* destroy this variant */
1842 delete_fp_variant(st
, fpv
);
1845 prevPtr
= &fpv
->next
;
1851 case GL_GEOMETRY_PROGRAM_NV
:
1852 case GL_TESS_CONTROL_PROGRAM_NV
:
1853 case GL_TESS_EVALUATION_PROGRAM_NV
:
1854 case GL_COMPUTE_PROGRAM_NV
:
1856 struct st_common_program
*p
= st_common_program(target
);
1857 struct st_common_variant
*v
, **prevPtr
= &p
->variants
;
1859 for (v
= p
->variants
; v
; ) {
1860 struct st_common_variant
*next
= v
->next
;
1861 if (v
->key
.st
== st
) {
1862 /* unlink from list */
1864 /* destroy this variant */
1865 delete_basic_variant(st
, v
, target
->Target
);
1875 _mesa_problem(NULL
, "Unexpected program target 0x%x in "
1876 "destroy_program_variants_cb()", target
->Target
);
1882 * Callback for _mesa_HashWalk. Free all the shader's program variants
1883 * which match the given context.
1886 destroy_shader_program_variants_cb(GLuint key
, void *data
, void *userData
)
1888 struct st_context
*st
= (struct st_context
*) userData
;
1889 struct gl_shader
*shader
= (struct gl_shader
*) data
;
1891 switch (shader
->Type
) {
1892 case GL_SHADER_PROGRAM_MESA
:
1894 struct gl_shader_program
*shProg
= (struct gl_shader_program
*) data
;
1897 for (i
= 0; i
< ARRAY_SIZE(shProg
->_LinkedShaders
); i
++) {
1898 if (shProg
->_LinkedShaders
[i
])
1899 destroy_program_variants(st
, shProg
->_LinkedShaders
[i
]->Program
);
1903 case GL_VERTEX_SHADER
:
1904 case GL_FRAGMENT_SHADER
:
1905 case GL_GEOMETRY_SHADER
:
1906 case GL_TESS_CONTROL_SHADER
:
1907 case GL_TESS_EVALUATION_SHADER
:
1908 case GL_COMPUTE_SHADER
:
1917 * Callback for _mesa_HashWalk. Free all the program variants which match
1918 * the given context.
1921 destroy_program_variants_cb(GLuint key
, void *data
, void *userData
)
1923 struct st_context
*st
= (struct st_context
*) userData
;
1924 struct gl_program
*program
= (struct gl_program
*) data
;
1925 destroy_program_variants(st
, program
);
1930 * Walk over all shaders and programs to delete any variants which
1931 * belong to the given context.
1932 * This is called during context tear-down.
1935 st_destroy_program_variants(struct st_context
*st
)
1937 /* If shaders can be shared with other contexts, the last context will
1938 * call DeleteProgram on all shaders, releasing everything.
1940 if (st
->has_shareable_shaders
)
1943 /* ARB vert/frag program */
1944 _mesa_HashWalk(st
->ctx
->Shared
->Programs
,
1945 destroy_program_variants_cb
, st
);
1947 /* GLSL vert/frag/geom shaders */
1948 _mesa_HashWalk(st
->ctx
->Shared
->ShaderObjects
,
1949 destroy_shader_program_variants_cb
, st
);
1954 * For debugging, print/dump the current vertex program.
1957 st_print_current_vertex_program(void)
1959 GET_CURRENT_CONTEXT(ctx
);
1961 if (ctx
->VertexProgram
._Current
) {
1962 struct st_vertex_program
*stvp
=
1963 (struct st_vertex_program
*) ctx
->VertexProgram
._Current
;
1964 struct st_vp_variant
*stv
;
1966 debug_printf("Vertex program %u\n", stvp
->Base
.Id
);
1968 for (stv
= stvp
->variants
; stv
; stv
= stv
->next
) {
1969 debug_printf("variant %p\n", stv
);
1970 tgsi_dump(stv
->tokens
, 0);
1977 * Compile one shader variant.
1980 st_precompile_shader_variant(struct st_context
*st
,
1981 struct gl_program
*prog
)
1983 switch (prog
->Target
) {
1984 case GL_VERTEX_PROGRAM_ARB
: {
1985 struct st_vertex_program
*p
= (struct st_vertex_program
*)prog
;
1986 struct st_common_variant_key key
;
1988 memset(&key
, 0, sizeof(key
));
1990 key
.st
= st
->has_shareable_shaders
? NULL
: st
;
1991 st_get_vp_variant(st
, p
, &key
);
1995 case GL_FRAGMENT_PROGRAM_ARB
: {
1996 struct st_common_program
*p
= (struct st_common_program
*)prog
;
1997 struct st_fp_variant_key key
;
1999 memset(&key
, 0, sizeof(key
));
2001 key
.st
= st
->has_shareable_shaders
? NULL
: st
;
2002 st_get_fp_variant(st
, p
, &key
);
2006 case GL_TESS_CONTROL_PROGRAM_NV
:
2007 case GL_TESS_EVALUATION_PROGRAM_NV
:
2008 case GL_GEOMETRY_PROGRAM_NV
:
2009 case GL_COMPUTE_PROGRAM_NV
: {
2010 struct st_common_program
*p
= st_common_program(prog
);
2011 struct st_common_variant_key key
;
2013 memset(&key
, 0, sizeof(key
));
2015 key
.st
= st
->has_shareable_shaders
? NULL
: st
;
2016 st_get_common_variant(st
, p
, &key
);