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 /* Some of the lowering above may have introduced new varyings */
724 nir_shader_gather_info(state
.ir
.nir
,
725 nir_shader_get_entrypoint(state
.ir
.nir
));
727 vpv
->driver_shader
= pipe
->create_vs_state(pipe
, &state
);
729 /* When generating a NIR program, we usually don't have TGSI tokens.
730 * However, we do create them for ARB_vertex_program / fixed-function VS
731 * programs which we may need to use with the draw module for legacy
732 * feedback/select emulation. If they exist, copy them.
734 * TODO: Lowering for shader variants is not applied to TGSI when
735 * generating a NIR shader.
737 if (stvp
->state
.tokens
)
738 vpv
->tokens
= tgsi_dup_tokens(stvp
->state
.tokens
);
743 state
.type
= PIPE_SHADER_IR_TGSI
;
744 state
.tokens
= tgsi_dup_tokens(stvp
->state
.tokens
);
746 /* Emulate features. */
747 if (key
->clamp_color
|| key
->passthrough_edgeflags
) {
748 const struct tgsi_token
*tokens
;
750 (key
->clamp_color
? TGSI_EMU_CLAMP_COLOR_OUTPUTS
: 0) |
751 (key
->passthrough_edgeflags
? TGSI_EMU_PASSTHROUGH_EDGEFLAG
: 0);
753 tokens
= tgsi_emulate(state
.tokens
, flags
);
756 tgsi_free_tokens(state
.tokens
);
757 state
.tokens
= tokens
;
759 if (key
->passthrough_edgeflags
)
762 fprintf(stderr
, "mesa: cannot emulate deprecated features\n");
765 if (key
->lower_depth_clamp
) {
766 unsigned depth_range_const
=
767 _mesa_add_state_reference(params
, depth_range_state
);
769 const struct tgsi_token
*tokens
;
770 tokens
= st_tgsi_lower_depth_clamp(state
.tokens
, depth_range_const
,
771 key
->clip_negative_one_to_one
);
772 if (tokens
!= state
.tokens
)
773 tgsi_free_tokens(state
.tokens
);
774 state
.tokens
= tokens
;
777 if (ST_DEBUG
& DEBUG_TGSI
) {
778 tgsi_dump(state
.tokens
, 0);
782 vpv
->driver_shader
= pipe
->create_vs_state(pipe
, &state
);
783 /* Save this for selection/feedback/rasterpos. */
784 vpv
->tokens
= state
.tokens
;
790 * Find/create a vertex program variant.
792 struct st_vp_variant
*
793 st_get_vp_variant(struct st_context
*st
,
794 struct st_vertex_program
*stvp
,
795 const struct st_common_variant_key
*key
)
797 struct st_vp_variant
*vpv
;
799 /* Search for existing variant */
800 for (vpv
= stvp
->variants
; vpv
; vpv
= vpv
->next
) {
801 if (memcmp(&vpv
->key
, key
, sizeof(*key
)) == 0) {
808 vpv
= st_create_vp_variant(st
, stvp
, key
);
810 for (unsigned index
= 0; index
< vpv
->num_inputs
; ++index
) {
811 unsigned attr
= stvp
->index_to_input
[index
];
812 if (attr
== ST_DOUBLE_ATTRIB_PLACEHOLDER
)
814 vpv
->vert_attrib_mask
|= 1u << attr
;
817 /* insert into list */
818 vpv
->next
= stvp
->variants
;
819 stvp
->variants
= vpv
;
828 * Translate a Mesa fragment shader into a TGSI shader.
831 st_translate_fragment_program(struct st_context
*st
,
832 struct st_common_program
*stfp
)
834 /* Non-GLSL programs: */
835 if (!stfp
->glsl_to_tgsi
) {
836 _mesa_remove_output_reads(&stfp
->Base
, PROGRAM_OUTPUT
);
837 if (st
->ctx
->Const
.GLSLFragCoordIsSysVal
)
838 _mesa_program_fragment_position_to_sysval(&stfp
->Base
);
840 /* This determines which states will be updated when the assembly
843 * fragment.position and glDrawPixels always use constants.
845 stfp
->affected_states
= ST_NEW_FS_STATE
|
846 ST_NEW_SAMPLE_SHADING
|
850 /* Just set them for ATI_fs unconditionally. */
851 stfp
->affected_states
|= ST_NEW_FS_SAMPLER_VIEWS
|
855 if (stfp
->Base
.SamplersUsed
)
856 stfp
->affected_states
|= ST_NEW_FS_SAMPLER_VIEWS
|
860 /* Translate to NIR. */
862 st
->pipe
->screen
->get_shader_param(st
->pipe
->screen
,
863 PIPE_SHADER_FRAGMENT
,
864 PIPE_SHADER_CAP_PREFERRED_IR
)) {
866 st_translate_prog_to_nir(st
, &stfp
->Base
, MESA_SHADER_FRAGMENT
);
868 if (stfp
->state
.ir
.nir
)
869 ralloc_free(stfp
->state
.ir
.nir
);
870 stfp
->state
.type
= PIPE_SHADER_IR_NIR
;
871 stfp
->state
.ir
.nir
= nir
;
872 stfp
->Base
.nir
= nir
;
877 ubyte outputMapping
[2 * FRAG_RESULT_MAX
];
878 ubyte inputMapping
[VARYING_SLOT_MAX
];
879 ubyte inputSlotToAttr
[VARYING_SLOT_MAX
];
880 ubyte interpMode
[PIPE_MAX_SHADER_INPUTS
]; /* XXX size? */
882 GLbitfield64 inputsRead
;
883 struct ureg_program
*ureg
;
885 GLboolean write_all
= GL_FALSE
;
887 ubyte input_semantic_name
[PIPE_MAX_SHADER_INPUTS
];
888 ubyte input_semantic_index
[PIPE_MAX_SHADER_INPUTS
];
889 uint fs_num_inputs
= 0;
891 ubyte fs_output_semantic_name
[PIPE_MAX_SHADER_OUTPUTS
];
892 ubyte fs_output_semantic_index
[PIPE_MAX_SHADER_OUTPUTS
];
893 uint fs_num_outputs
= 0;
895 memset(inputSlotToAttr
, ~0, sizeof(inputSlotToAttr
));
898 * Convert Mesa program inputs to TGSI input register semantics.
900 inputsRead
= stfp
->Base
.info
.inputs_read
;
901 for (attr
= 0; attr
< VARYING_SLOT_MAX
; attr
++) {
902 if ((inputsRead
& BITFIELD64_BIT(attr
)) != 0) {
903 const GLuint slot
= fs_num_inputs
++;
905 inputMapping
[attr
] = slot
;
906 inputSlotToAttr
[slot
] = attr
;
909 case VARYING_SLOT_POS
:
910 input_semantic_name
[slot
] = TGSI_SEMANTIC_POSITION
;
911 input_semantic_index
[slot
] = 0;
912 interpMode
[slot
] = TGSI_INTERPOLATE_LINEAR
;
914 case VARYING_SLOT_COL0
:
915 input_semantic_name
[slot
] = TGSI_SEMANTIC_COLOR
;
916 input_semantic_index
[slot
] = 0;
917 interpMode
[slot
] = stfp
->glsl_to_tgsi
?
918 TGSI_INTERPOLATE_COUNT
: TGSI_INTERPOLATE_COLOR
;
920 case VARYING_SLOT_COL1
:
921 input_semantic_name
[slot
] = TGSI_SEMANTIC_COLOR
;
922 input_semantic_index
[slot
] = 1;
923 interpMode
[slot
] = stfp
->glsl_to_tgsi
?
924 TGSI_INTERPOLATE_COUNT
: TGSI_INTERPOLATE_COLOR
;
926 case VARYING_SLOT_FOGC
:
927 input_semantic_name
[slot
] = TGSI_SEMANTIC_FOG
;
928 input_semantic_index
[slot
] = 0;
929 interpMode
[slot
] = TGSI_INTERPOLATE_PERSPECTIVE
;
931 case VARYING_SLOT_FACE
:
932 input_semantic_name
[slot
] = TGSI_SEMANTIC_FACE
;
933 input_semantic_index
[slot
] = 0;
934 interpMode
[slot
] = TGSI_INTERPOLATE_CONSTANT
;
936 case VARYING_SLOT_PRIMITIVE_ID
:
937 input_semantic_name
[slot
] = TGSI_SEMANTIC_PRIMID
;
938 input_semantic_index
[slot
] = 0;
939 interpMode
[slot
] = TGSI_INTERPOLATE_CONSTANT
;
941 case VARYING_SLOT_LAYER
:
942 input_semantic_name
[slot
] = TGSI_SEMANTIC_LAYER
;
943 input_semantic_index
[slot
] = 0;
944 interpMode
[slot
] = TGSI_INTERPOLATE_CONSTANT
;
946 case VARYING_SLOT_VIEWPORT
:
947 input_semantic_name
[slot
] = TGSI_SEMANTIC_VIEWPORT_INDEX
;
948 input_semantic_index
[slot
] = 0;
949 interpMode
[slot
] = TGSI_INTERPOLATE_CONSTANT
;
951 case VARYING_SLOT_CLIP_DIST0
:
952 input_semantic_name
[slot
] = TGSI_SEMANTIC_CLIPDIST
;
953 input_semantic_index
[slot
] = 0;
954 interpMode
[slot
] = TGSI_INTERPOLATE_PERSPECTIVE
;
956 case VARYING_SLOT_CLIP_DIST1
:
957 input_semantic_name
[slot
] = TGSI_SEMANTIC_CLIPDIST
;
958 input_semantic_index
[slot
] = 1;
959 interpMode
[slot
] = TGSI_INTERPOLATE_PERSPECTIVE
;
961 case VARYING_SLOT_CULL_DIST0
:
962 case VARYING_SLOT_CULL_DIST1
:
963 /* these should have been lowered by GLSL */
966 /* In most cases, there is nothing special about these
967 * inputs, so adopt a convention to use the generic
968 * semantic name and the mesa VARYING_SLOT_ number as the
971 * All that is required is that the vertex shader labels
972 * its own outputs similarly, and that the vertex shader
973 * generates at least every output required by the
974 * fragment shader plus fixed-function hardware (such as
977 * However, some drivers may need us to identify the PNTC and TEXi
978 * varyings if, for example, their capability to replace them with
979 * sprite coordinates is limited.
981 case VARYING_SLOT_PNTC
:
982 if (st
->needs_texcoord_semantic
) {
983 input_semantic_name
[slot
] = TGSI_SEMANTIC_PCOORD
;
984 input_semantic_index
[slot
] = 0;
985 interpMode
[slot
] = TGSI_INTERPOLATE_LINEAR
;
989 case VARYING_SLOT_TEX0
:
990 case VARYING_SLOT_TEX1
:
991 case VARYING_SLOT_TEX2
:
992 case VARYING_SLOT_TEX3
:
993 case VARYING_SLOT_TEX4
:
994 case VARYING_SLOT_TEX5
:
995 case VARYING_SLOT_TEX6
:
996 case VARYING_SLOT_TEX7
:
997 if (st
->needs_texcoord_semantic
) {
998 input_semantic_name
[slot
] = TGSI_SEMANTIC_TEXCOORD
;
999 input_semantic_index
[slot
] = attr
- VARYING_SLOT_TEX0
;
1000 interpMode
[slot
] = stfp
->glsl_to_tgsi
?
1001 TGSI_INTERPOLATE_COUNT
: TGSI_INTERPOLATE_PERSPECTIVE
;
1005 case VARYING_SLOT_VAR0
:
1007 /* Semantic indices should be zero-based because drivers may choose
1008 * to assign a fixed slot determined by that index.
1009 * This is useful because ARB_separate_shader_objects uses location
1010 * qualifiers for linkage, and if the semantic index corresponds to
1011 * these locations, linkage passes in the driver become unecessary.
1013 * If needs_texcoord_semantic is true, no semantic indices will be
1014 * consumed for the TEXi varyings, and we can base the locations of
1015 * the user varyings on VAR0. Otherwise, we use TEX0 as base index.
1017 assert(attr
>= VARYING_SLOT_VAR0
|| attr
== VARYING_SLOT_PNTC
||
1018 (attr
>= VARYING_SLOT_TEX0
&& attr
<= VARYING_SLOT_TEX7
));
1019 input_semantic_name
[slot
] = TGSI_SEMANTIC_GENERIC
;
1020 input_semantic_index
[slot
] = st_get_generic_varying_index(st
, attr
);
1021 if (attr
== VARYING_SLOT_PNTC
)
1022 interpMode
[slot
] = TGSI_INTERPOLATE_LINEAR
;
1024 interpMode
[slot
] = stfp
->glsl_to_tgsi
?
1025 TGSI_INTERPOLATE_COUNT
: TGSI_INTERPOLATE_PERSPECTIVE
;
1031 inputMapping
[attr
] = -1;
1036 * Semantics and mapping for outputs
1038 GLbitfield64 outputsWritten
= stfp
->Base
.info
.outputs_written
;
1040 /* if z is written, emit that first */
1041 if (outputsWritten
& BITFIELD64_BIT(FRAG_RESULT_DEPTH
)) {
1042 fs_output_semantic_name
[fs_num_outputs
] = TGSI_SEMANTIC_POSITION
;
1043 fs_output_semantic_index
[fs_num_outputs
] = 0;
1044 outputMapping
[FRAG_RESULT_DEPTH
] = fs_num_outputs
;
1046 outputsWritten
&= ~(1 << FRAG_RESULT_DEPTH
);
1049 if (outputsWritten
& BITFIELD64_BIT(FRAG_RESULT_STENCIL
)) {
1050 fs_output_semantic_name
[fs_num_outputs
] = TGSI_SEMANTIC_STENCIL
;
1051 fs_output_semantic_index
[fs_num_outputs
] = 0;
1052 outputMapping
[FRAG_RESULT_STENCIL
] = fs_num_outputs
;
1054 outputsWritten
&= ~(1 << FRAG_RESULT_STENCIL
);
1057 if (outputsWritten
& BITFIELD64_BIT(FRAG_RESULT_SAMPLE_MASK
)) {
1058 fs_output_semantic_name
[fs_num_outputs
] = TGSI_SEMANTIC_SAMPLEMASK
;
1059 fs_output_semantic_index
[fs_num_outputs
] = 0;
1060 outputMapping
[FRAG_RESULT_SAMPLE_MASK
] = fs_num_outputs
;
1062 outputsWritten
&= ~(1 << FRAG_RESULT_SAMPLE_MASK
);
1065 /* handle remaining outputs (color) */
1066 for (attr
= 0; attr
< ARRAY_SIZE(outputMapping
); attr
++) {
1067 const GLbitfield64 written
= attr
< FRAG_RESULT_MAX
? outputsWritten
:
1068 stfp
->Base
.SecondaryOutputsWritten
;
1069 const unsigned loc
= attr
% FRAG_RESULT_MAX
;
1071 if (written
& BITFIELD64_BIT(loc
)) {
1073 case FRAG_RESULT_DEPTH
:
1074 case FRAG_RESULT_STENCIL
:
1075 case FRAG_RESULT_SAMPLE_MASK
:
1079 case FRAG_RESULT_COLOR
:
1080 write_all
= GL_TRUE
; /* fallthrough */
1083 assert(loc
== FRAG_RESULT_COLOR
||
1084 (FRAG_RESULT_DATA0
<= loc
&& loc
< FRAG_RESULT_MAX
));
1086 index
= (loc
== FRAG_RESULT_COLOR
) ? 0 : (loc
- FRAG_RESULT_DATA0
);
1088 if (attr
>= FRAG_RESULT_MAX
) {
1089 /* Secondary color for dual source blending. */
1094 fs_output_semantic_name
[fs_num_outputs
] = TGSI_SEMANTIC_COLOR
;
1095 fs_output_semantic_index
[fs_num_outputs
] = index
;
1096 outputMapping
[attr
] = fs_num_outputs
;
1105 ureg
= ureg_create_with_screen(PIPE_SHADER_FRAGMENT
, st
->pipe
->screen
);
1109 if (ST_DEBUG
& DEBUG_MESA
) {
1110 _mesa_print_program(&stfp
->Base
);
1111 _mesa_print_program_parameters(st
->ctx
, &stfp
->Base
);
1114 if (write_all
== GL_TRUE
)
1115 ureg_property(ureg
, TGSI_PROPERTY_FS_COLOR0_WRITES_ALL_CBUFS
, 1);
1117 if (stfp
->Base
.info
.fs
.depth_layout
!= FRAG_DEPTH_LAYOUT_NONE
) {
1118 switch (stfp
->Base
.info
.fs
.depth_layout
) {
1119 case FRAG_DEPTH_LAYOUT_ANY
:
1120 ureg_property(ureg
, TGSI_PROPERTY_FS_DEPTH_LAYOUT
,
1121 TGSI_FS_DEPTH_LAYOUT_ANY
);
1123 case FRAG_DEPTH_LAYOUT_GREATER
:
1124 ureg_property(ureg
, TGSI_PROPERTY_FS_DEPTH_LAYOUT
,
1125 TGSI_FS_DEPTH_LAYOUT_GREATER
);
1127 case FRAG_DEPTH_LAYOUT_LESS
:
1128 ureg_property(ureg
, TGSI_PROPERTY_FS_DEPTH_LAYOUT
,
1129 TGSI_FS_DEPTH_LAYOUT_LESS
);
1131 case FRAG_DEPTH_LAYOUT_UNCHANGED
:
1132 ureg_property(ureg
, TGSI_PROPERTY_FS_DEPTH_LAYOUT
,
1133 TGSI_FS_DEPTH_LAYOUT_UNCHANGED
);
1140 if (stfp
->glsl_to_tgsi
) {
1141 st_translate_program(st
->ctx
,
1142 PIPE_SHADER_FRAGMENT
,
1150 input_semantic_name
,
1151 input_semantic_index
,
1156 fs_output_semantic_name
,
1157 fs_output_semantic_index
);
1159 free_glsl_to_tgsi_visitor(stfp
->glsl_to_tgsi
);
1160 } else if (stfp
->ati_fs
)
1161 st_translate_atifs_program(ureg
,
1167 input_semantic_name
,
1168 input_semantic_index
,
1173 fs_output_semantic_name
,
1174 fs_output_semantic_index
);
1176 st_translate_mesa_program(st
->ctx
,
1177 PIPE_SHADER_FRAGMENT
,
1183 input_semantic_name
,
1184 input_semantic_index
,
1189 fs_output_semantic_name
,
1190 fs_output_semantic_index
);
1192 stfp
->state
.tokens
= ureg_get_tokens(ureg
, NULL
);
1195 if (stfp
->glsl_to_tgsi
) {
1196 stfp
->glsl_to_tgsi
= NULL
;
1197 st_store_ir_in_disk_cache(st
, &stfp
->Base
, false);
1200 return stfp
->state
.tokens
!= NULL
;
1203 static struct st_fp_variant
*
1204 st_create_fp_variant(struct st_context
*st
,
1205 struct st_common_program
*stfp
,
1206 const struct st_fp_variant_key
*key
)
1208 struct pipe_context
*pipe
= st
->pipe
;
1209 struct st_fp_variant
*variant
= CALLOC_STRUCT(st_fp_variant
);
1210 struct pipe_shader_state state
= {0};
1211 struct gl_program_parameter_list
*params
= stfp
->Base
.Parameters
;
1212 static const gl_state_index16 texcoord_state
[STATE_LENGTH
] =
1213 { STATE_INTERNAL
, STATE_CURRENT_ATTRIB
, VERT_ATTRIB_TEX0
};
1214 static const gl_state_index16 scale_state
[STATE_LENGTH
] =
1215 { STATE_INTERNAL
, STATE_PT_SCALE
};
1216 static const gl_state_index16 bias_state
[STATE_LENGTH
] =
1217 { STATE_INTERNAL
, STATE_PT_BIAS
};
1218 static const gl_state_index16 alpha_ref_state
[STATE_LENGTH
] =
1219 { STATE_INTERNAL
, STATE_ALPHA_REF
};
1224 if (stfp
->state
.type
== PIPE_SHADER_IR_NIR
) {
1225 state
.type
= PIPE_SHADER_IR_NIR
;
1226 state
.ir
.nir
= nir_shader_clone(NULL
, stfp
->state
.ir
.nir
);
1228 if (key
->clamp_color
)
1229 NIR_PASS_V(state
.ir
.nir
, nir_lower_clamp_color_outputs
);
1231 if (key
->lower_flatshade
)
1232 NIR_PASS_V(state
.ir
.nir
, nir_lower_flatshade
);
1234 if (key
->lower_alpha_func
!= COMPARE_FUNC_NEVER
) {
1235 _mesa_add_state_reference(params
, alpha_ref_state
);
1236 NIR_PASS_V(state
.ir
.nir
, nir_lower_alpha_test
, key
->lower_alpha_func
,
1237 false, alpha_ref_state
);
1240 if (key
->lower_two_sided_color
)
1241 NIR_PASS_V(state
.ir
.nir
, nir_lower_two_sided_color
);
1243 if (key
->persample_shading
) {
1244 nir_shader
*shader
= state
.ir
.nir
;
1245 nir_foreach_variable(var
, &shader
->inputs
)
1246 var
->data
.sample
= true;
1249 assert(!(key
->bitmap
&& key
->drawpixels
));
1253 nir_lower_bitmap_options options
= {0};
1255 variant
->bitmap_sampler
= ffs(~stfp
->Base
.SamplersUsed
) - 1;
1256 options
.sampler
= variant
->bitmap_sampler
;
1257 options
.swizzle_xxxx
= st
->bitmap
.tex_format
== PIPE_FORMAT_R8_UNORM
;
1259 NIR_PASS_V(state
.ir
.nir
, nir_lower_bitmap
, &options
);
1262 /* glDrawPixels (color only) */
1263 if (key
->drawpixels
) {
1264 nir_lower_drawpixels_options options
= {{0}};
1265 unsigned samplers_used
= stfp
->Base
.SamplersUsed
;
1267 /* Find the first unused slot. */
1268 variant
->drawpix_sampler
= ffs(~samplers_used
) - 1;
1269 options
.drawpix_sampler
= variant
->drawpix_sampler
;
1270 samplers_used
|= (1 << variant
->drawpix_sampler
);
1272 options
.pixel_maps
= key
->pixelMaps
;
1273 if (key
->pixelMaps
) {
1274 variant
->pixelmap_sampler
= ffs(~samplers_used
) - 1;
1275 options
.pixelmap_sampler
= variant
->pixelmap_sampler
;
1278 options
.scale_and_bias
= key
->scaleAndBias
;
1279 if (key
->scaleAndBias
) {
1280 _mesa_add_state_reference(params
, scale_state
);
1281 memcpy(options
.scale_state_tokens
, scale_state
,
1282 sizeof(options
.scale_state_tokens
));
1283 _mesa_add_state_reference(params
, bias_state
);
1284 memcpy(options
.bias_state_tokens
, bias_state
,
1285 sizeof(options
.bias_state_tokens
));
1288 _mesa_add_state_reference(params
, texcoord_state
);
1289 memcpy(options
.texcoord_state_tokens
, texcoord_state
,
1290 sizeof(options
.texcoord_state_tokens
));
1292 NIR_PASS_V(state
.ir
.nir
, nir_lower_drawpixels
, &options
);
1295 if (unlikely(key
->external
.lower_nv12
|| key
->external
.lower_iyuv
||
1296 key
->external
.lower_xy_uxvx
|| key
->external
.lower_yx_xuxv
||
1297 key
->external
.lower_ayuv
|| key
->external
.lower_xyuv
)) {
1298 nir_lower_tex_options options
= {0};
1299 options
.lower_y_uv_external
= key
->external
.lower_nv12
;
1300 options
.lower_y_u_v_external
= key
->external
.lower_iyuv
;
1301 options
.lower_xy_uxvx_external
= key
->external
.lower_xy_uxvx
;
1302 options
.lower_yx_xuxv_external
= key
->external
.lower_yx_xuxv
;
1303 options
.lower_ayuv_external
= key
->external
.lower_ayuv
;
1304 options
.lower_xyuv_external
= key
->external
.lower_xyuv
;
1305 NIR_PASS_V(state
.ir
.nir
, nir_lower_tex
, &options
);
1308 st_finalize_nir(st
, &stfp
->Base
, stfp
->shader_program
, state
.ir
.nir
);
1310 if (unlikely(key
->external
.lower_nv12
|| key
->external
.lower_iyuv
||
1311 key
->external
.lower_xy_uxvx
|| key
->external
.lower_yx_xuxv
)) {
1312 /* This pass needs to happen *after* nir_lower_sampler */
1313 NIR_PASS_V(state
.ir
.nir
, st_nir_lower_tex_src_plane
,
1314 ~stfp
->Base
.SamplersUsed
,
1315 key
->external
.lower_nv12
|| key
->external
.lower_xy_uxvx
||
1316 key
->external
.lower_yx_xuxv
,
1317 key
->external
.lower_iyuv
);
1320 /* Some of the lowering above may have introduced new varyings */
1321 nir_shader_gather_info(state
.ir
.nir
,
1322 nir_shader_get_entrypoint(state
.ir
.nir
));
1324 variant
->driver_shader
= pipe
->create_fs_state(pipe
, &state
);
1325 variant
->key
= *key
;
1330 state
.tokens
= stfp
->state
.tokens
;
1332 assert(!(key
->bitmap
&& key
->drawpixels
));
1334 /* Fix texture targets and add fog for ATI_fs */
1336 const struct tgsi_token
*tokens
= st_fixup_atifs(state
.tokens
, key
);
1339 state
.tokens
= tokens
;
1341 fprintf(stderr
, "mesa: cannot post-process ATI_fs\n");
1344 /* Emulate features. */
1345 if (key
->clamp_color
|| key
->persample_shading
) {
1346 const struct tgsi_token
*tokens
;
1348 (key
->clamp_color
? TGSI_EMU_CLAMP_COLOR_OUTPUTS
: 0) |
1349 (key
->persample_shading
? TGSI_EMU_FORCE_PERSAMPLE_INTERP
: 0);
1351 tokens
= tgsi_emulate(state
.tokens
, flags
);
1354 if (state
.tokens
!= stfp
->state
.tokens
)
1355 tgsi_free_tokens(state
.tokens
);
1356 state
.tokens
= tokens
;
1358 fprintf(stderr
, "mesa: cannot emulate deprecated features\n");
1363 const struct tgsi_token
*tokens
;
1365 variant
->bitmap_sampler
= ffs(~stfp
->Base
.SamplersUsed
) - 1;
1367 tokens
= st_get_bitmap_shader(state
.tokens
,
1368 st
->internal_target
,
1369 variant
->bitmap_sampler
,
1370 st
->needs_texcoord_semantic
,
1371 st
->bitmap
.tex_format
==
1372 PIPE_FORMAT_R8_UNORM
);
1375 if (state
.tokens
!= stfp
->state
.tokens
)
1376 tgsi_free_tokens(state
.tokens
);
1377 state
.tokens
= tokens
;
1379 fprintf(stderr
, "mesa: cannot create a shader for glBitmap\n");
1382 /* glDrawPixels (color only) */
1383 if (key
->drawpixels
) {
1384 const struct tgsi_token
*tokens
;
1385 unsigned scale_const
= 0, bias_const
= 0, texcoord_const
= 0;
1387 /* Find the first unused slot. */
1388 variant
->drawpix_sampler
= ffs(~stfp
->Base
.SamplersUsed
) - 1;
1390 if (key
->pixelMaps
) {
1391 unsigned samplers_used
= stfp
->Base
.SamplersUsed
|
1392 (1 << variant
->drawpix_sampler
);
1394 variant
->pixelmap_sampler
= ffs(~samplers_used
) - 1;
1397 if (key
->scaleAndBias
) {
1398 scale_const
= _mesa_add_state_reference(params
, scale_state
);
1399 bias_const
= _mesa_add_state_reference(params
, bias_state
);
1402 texcoord_const
= _mesa_add_state_reference(params
, texcoord_state
);
1404 tokens
= st_get_drawpix_shader(state
.tokens
,
1405 st
->needs_texcoord_semantic
,
1406 key
->scaleAndBias
, scale_const
,
1407 bias_const
, key
->pixelMaps
,
1408 variant
->drawpix_sampler
,
1409 variant
->pixelmap_sampler
,
1410 texcoord_const
, st
->internal_target
);
1413 if (state
.tokens
!= stfp
->state
.tokens
)
1414 tgsi_free_tokens(state
.tokens
);
1415 state
.tokens
= tokens
;
1417 fprintf(stderr
, "mesa: cannot create a shader for glDrawPixels\n");
1420 if (unlikely(key
->external
.lower_nv12
|| key
->external
.lower_iyuv
||
1421 key
->external
.lower_xy_uxvx
|| key
->external
.lower_yx_xuxv
)) {
1422 const struct tgsi_token
*tokens
;
1424 /* samplers inserted would conflict, but this should be unpossible: */
1425 assert(!(key
->bitmap
|| key
->drawpixels
));
1427 tokens
= st_tgsi_lower_yuv(state
.tokens
,
1428 ~stfp
->Base
.SamplersUsed
,
1429 key
->external
.lower_nv12
||
1430 key
->external
.lower_xy_uxvx
||
1431 key
->external
.lower_yx_xuxv
,
1432 key
->external
.lower_iyuv
);
1434 if (state
.tokens
!= stfp
->state
.tokens
)
1435 tgsi_free_tokens(state
.tokens
);
1436 state
.tokens
= tokens
;
1438 fprintf(stderr
, "mesa: cannot create a shader for samplerExternalOES\n");
1442 if (key
->lower_depth_clamp
) {
1443 unsigned depth_range_const
= _mesa_add_state_reference(params
, depth_range_state
);
1445 const struct tgsi_token
*tokens
;
1446 tokens
= st_tgsi_lower_depth_clamp_fs(state
.tokens
, depth_range_const
);
1447 if (state
.tokens
!= stfp
->state
.tokens
)
1448 tgsi_free_tokens(state
.tokens
);
1449 state
.tokens
= tokens
;
1452 if (ST_DEBUG
& DEBUG_TGSI
) {
1453 tgsi_dump(state
.tokens
, 0);
1457 /* fill in variant */
1458 variant
->driver_shader
= pipe
->create_fs_state(pipe
, &state
);
1459 variant
->key
= *key
;
1461 if (state
.tokens
!= stfp
->state
.tokens
)
1462 tgsi_free_tokens(state
.tokens
);
1467 * Translate fragment program if needed.
1469 struct st_fp_variant
*
1470 st_get_fp_variant(struct st_context
*st
,
1471 struct st_common_program
*stfp
,
1472 const struct st_fp_variant_key
*key
)
1474 struct st_fp_variant
*fpv
;
1476 /* Search for existing variant */
1477 for (fpv
= stfp
->fp_variants
; fpv
; fpv
= fpv
->next
) {
1478 if (memcmp(&fpv
->key
, key
, sizeof(*key
)) == 0) {
1485 fpv
= st_create_fp_variant(st
, stfp
, key
);
1487 if (key
->bitmap
|| key
->drawpixels
) {
1488 /* Regular variants should always come before the
1489 * bitmap & drawpixels variants, (unless there
1490 * are no regular variants) so that
1491 * st_update_fp can take a fast path when
1492 * shader_has_one_variant is set.
1494 if (!stfp
->fp_variants
) {
1495 stfp
->fp_variants
= fpv
;
1497 /* insert into list after the first one */
1498 fpv
->next
= stfp
->fp_variants
->next
;
1499 stfp
->fp_variants
->next
= fpv
;
1502 /* insert into list */
1503 fpv
->next
= stfp
->fp_variants
;
1504 stfp
->fp_variants
= fpv
;
1513 * Translate a program. This is common code for geometry and tessellation
1517 st_translate_common_program(struct st_context
*st
,
1518 struct st_common_program
*stcp
)
1520 struct gl_program
*prog
= &stcp
->Base
;
1521 enum pipe_shader_type stage
=
1522 pipe_shader_type_from_mesa(stcp
->Base
.info
.stage
);
1523 struct ureg_program
*ureg
= ureg_create_with_screen(stage
, st
->pipe
->screen
);
1529 case PIPE_SHADER_TESS_CTRL
:
1530 ureg_property(ureg
, TGSI_PROPERTY_TCS_VERTICES_OUT
,
1531 stcp
->Base
.info
.tess
.tcs_vertices_out
);
1534 case PIPE_SHADER_TESS_EVAL
:
1535 if (stcp
->Base
.info
.tess
.primitive_mode
== GL_ISOLINES
)
1536 ureg_property(ureg
, TGSI_PROPERTY_TES_PRIM_MODE
, GL_LINES
);
1538 ureg_property(ureg
, TGSI_PROPERTY_TES_PRIM_MODE
,
1539 stcp
->Base
.info
.tess
.primitive_mode
);
1541 STATIC_ASSERT((TESS_SPACING_EQUAL
+ 1) % 3 == PIPE_TESS_SPACING_EQUAL
);
1542 STATIC_ASSERT((TESS_SPACING_FRACTIONAL_ODD
+ 1) % 3 ==
1543 PIPE_TESS_SPACING_FRACTIONAL_ODD
);
1544 STATIC_ASSERT((TESS_SPACING_FRACTIONAL_EVEN
+ 1) % 3 ==
1545 PIPE_TESS_SPACING_FRACTIONAL_EVEN
);
1547 ureg_property(ureg
, TGSI_PROPERTY_TES_SPACING
,
1548 (stcp
->Base
.info
.tess
.spacing
+ 1) % 3);
1550 ureg_property(ureg
, TGSI_PROPERTY_TES_VERTEX_ORDER_CW
,
1551 !stcp
->Base
.info
.tess
.ccw
);
1552 ureg_property(ureg
, TGSI_PROPERTY_TES_POINT_MODE
,
1553 stcp
->Base
.info
.tess
.point_mode
);
1556 case PIPE_SHADER_GEOMETRY
:
1557 ureg_property(ureg
, TGSI_PROPERTY_GS_INPUT_PRIM
,
1558 stcp
->Base
.info
.gs
.input_primitive
);
1559 ureg_property(ureg
, TGSI_PROPERTY_GS_OUTPUT_PRIM
,
1560 stcp
->Base
.info
.gs
.output_primitive
);
1561 ureg_property(ureg
, TGSI_PROPERTY_GS_MAX_OUTPUT_VERTICES
,
1562 stcp
->Base
.info
.gs
.vertices_out
);
1563 ureg_property(ureg
, TGSI_PROPERTY_GS_INVOCATIONS
,
1564 stcp
->Base
.info
.gs
.invocations
);
1571 ubyte inputSlotToAttr
[VARYING_SLOT_TESS_MAX
];
1572 ubyte inputMapping
[VARYING_SLOT_TESS_MAX
];
1573 ubyte outputMapping
[VARYING_SLOT_TESS_MAX
];
1576 ubyte input_semantic_name
[PIPE_MAX_SHADER_INPUTS
];
1577 ubyte input_semantic_index
[PIPE_MAX_SHADER_INPUTS
];
1578 uint num_inputs
= 0;
1580 ubyte output_semantic_name
[PIPE_MAX_SHADER_OUTPUTS
];
1581 ubyte output_semantic_index
[PIPE_MAX_SHADER_OUTPUTS
];
1582 uint num_outputs
= 0;
1586 memset(inputSlotToAttr
, 0, sizeof(inputSlotToAttr
));
1587 memset(inputMapping
, 0, sizeof(inputMapping
));
1588 memset(outputMapping
, 0, sizeof(outputMapping
));
1589 memset(&stcp
->state
, 0, sizeof(stcp
->state
));
1591 if (prog
->info
.clip_distance_array_size
)
1592 ureg_property(ureg
, TGSI_PROPERTY_NUM_CLIPDIST_ENABLED
,
1593 prog
->info
.clip_distance_array_size
);
1594 if (prog
->info
.cull_distance_array_size
)
1595 ureg_property(ureg
, TGSI_PROPERTY_NUM_CULLDIST_ENABLED
,
1596 prog
->info
.cull_distance_array_size
);
1599 * Convert Mesa program inputs to TGSI input register semantics.
1601 for (attr
= 0; attr
< VARYING_SLOT_MAX
; attr
++) {
1602 if ((prog
->info
.inputs_read
& BITFIELD64_BIT(attr
)) == 0)
1605 unsigned slot
= num_inputs
++;
1607 inputMapping
[attr
] = slot
;
1608 inputSlotToAttr
[slot
] = attr
;
1610 unsigned semantic_name
, semantic_index
;
1611 tgsi_get_gl_varying_semantic(attr
, st
->needs_texcoord_semantic
,
1612 &semantic_name
, &semantic_index
);
1613 input_semantic_name
[slot
] = semantic_name
;
1614 input_semantic_index
[slot
] = semantic_index
;
1617 /* Also add patch inputs. */
1618 for (attr
= 0; attr
< 32; attr
++) {
1619 if (prog
->info
.patch_inputs_read
& (1u << attr
)) {
1620 GLuint slot
= num_inputs
++;
1621 GLuint patch_attr
= VARYING_SLOT_PATCH0
+ attr
;
1623 inputMapping
[patch_attr
] = slot
;
1624 inputSlotToAttr
[slot
] = patch_attr
;
1625 input_semantic_name
[slot
] = TGSI_SEMANTIC_PATCH
;
1626 input_semantic_index
[slot
] = attr
;
1630 /* initialize output semantics to defaults */
1631 for (i
= 0; i
< PIPE_MAX_SHADER_OUTPUTS
; i
++) {
1632 output_semantic_name
[i
] = TGSI_SEMANTIC_GENERIC
;
1633 output_semantic_index
[i
] = 0;
1637 * Determine number of outputs, the (default) output register
1638 * mapping and the semantic information for each output.
1640 for (attr
= 0; attr
< VARYING_SLOT_MAX
; attr
++) {
1641 if (prog
->info
.outputs_written
& BITFIELD64_BIT(attr
)) {
1642 GLuint slot
= num_outputs
++;
1644 outputMapping
[attr
] = slot
;
1646 unsigned semantic_name
, semantic_index
;
1647 tgsi_get_gl_varying_semantic(attr
, st
->needs_texcoord_semantic
,
1648 &semantic_name
, &semantic_index
);
1649 output_semantic_name
[slot
] = semantic_name
;
1650 output_semantic_index
[slot
] = semantic_index
;
1654 /* Also add patch outputs. */
1655 for (attr
= 0; attr
< 32; attr
++) {
1656 if (prog
->info
.patch_outputs_written
& (1u << attr
)) {
1657 GLuint slot
= num_outputs
++;
1658 GLuint patch_attr
= VARYING_SLOT_PATCH0
+ attr
;
1660 outputMapping
[patch_attr
] = slot
;
1661 output_semantic_name
[slot
] = TGSI_SEMANTIC_PATCH
;
1662 output_semantic_index
[slot
] = attr
;
1666 st_translate_program(st
->ctx
,
1675 input_semantic_name
,
1676 input_semantic_index
,
1681 output_semantic_name
,
1682 output_semantic_index
);
1684 stcp
->state
.tokens
= ureg_get_tokens(ureg
, NULL
);
1688 st_translate_stream_output_info(prog
);
1690 st_store_ir_in_disk_cache(st
, prog
, false);
1692 if ((ST_DEBUG
& DEBUG_TGSI
) && (ST_DEBUG
& DEBUG_MESA
)) {
1693 _mesa_print_program(prog
);
1697 if (ST_DEBUG
& DEBUG_TGSI
) {
1698 tgsi_dump(stcp
->state
.tokens
, 0);
1702 free_glsl_to_tgsi_visitor(stcp
->glsl_to_tgsi
);
1703 stcp
->glsl_to_tgsi
= NULL
;
1709 * Get/create a basic program variant.
1711 struct st_common_variant
*
1712 st_get_common_variant(struct st_context
*st
,
1713 struct st_common_program
*prog
,
1714 const struct st_common_variant_key
*key
)
1716 struct pipe_context
*pipe
= st
->pipe
;
1717 struct st_common_variant
*v
;
1718 struct pipe_shader_state state
= {0};
1720 /* Search for existing variant */
1721 for (v
= prog
->variants
; v
; v
= v
->next
) {
1722 if (memcmp(&v
->key
, key
, sizeof(*key
)) == 0) {
1729 v
= CALLOC_STRUCT(st_common_variant
);
1732 if (prog
->state
.type
== PIPE_SHADER_IR_NIR
) {
1733 state
.type
= PIPE_SHADER_IR_NIR
;
1734 state
.ir
.nir
= nir_shader_clone(NULL
, prog
->state
.ir
.nir
);
1736 if (key
->clamp_color
)
1737 NIR_PASS_V(state
.ir
.nir
, nir_lower_clamp_color_outputs
);
1739 state
.stream_output
= prog
->state
.stream_output
;
1741 st_finalize_nir(st
, &prog
->Base
, prog
->shader_program
,
1744 if (key
->lower_depth_clamp
) {
1745 struct gl_program_parameter_list
*params
= prog
->Base
.Parameters
;
1747 unsigned depth_range_const
=
1748 _mesa_add_state_reference(params
, depth_range_state
);
1750 const struct tgsi_token
*tokens
;
1752 st_tgsi_lower_depth_clamp(prog
->state
.tokens
,
1754 key
->clip_negative_one_to_one
);
1756 if (tokens
!= prog
->state
.tokens
)
1757 tgsi_free_tokens(prog
->state
.tokens
);
1759 prog
->state
.tokens
= tokens
;
1761 state
= prog
->state
;
1763 /* fill in new variant */
1764 switch (prog
->Base
.info
.stage
) {
1765 case MESA_SHADER_TESS_CTRL
:
1766 v
->driver_shader
= pipe
->create_tcs_state(pipe
, &state
);
1768 case MESA_SHADER_TESS_EVAL
:
1769 v
->driver_shader
= pipe
->create_tes_state(pipe
, &state
);
1771 case MESA_SHADER_GEOMETRY
:
1772 v
->driver_shader
= pipe
->create_gs_state(pipe
, &state
);
1774 case MESA_SHADER_COMPUTE
: {
1775 struct pipe_compute_state cs
= {0};
1776 cs
.ir_type
= state
.type
;
1777 cs
.req_local_mem
= prog
->Base
.info
.cs
.shared_size
;
1779 if (state
.type
== PIPE_SHADER_IR_NIR
)
1780 cs
.prog
= state
.ir
.nir
;
1782 cs
.prog
= state
.tokens
;
1784 v
->driver_shader
= pipe
->create_compute_state(pipe
, &cs
);
1788 assert(!"unhandled shader type");
1795 /* insert into list */
1796 v
->next
= prog
->variants
;
1806 * Vert/Geom/Frag programs have per-context variants. Free all the
1807 * variants attached to the given program which match the given context.
1810 destroy_program_variants(struct st_context
*st
, struct gl_program
*target
)
1812 if (!target
|| target
== &_mesa_DummyProgram
)
1815 switch (target
->Target
) {
1816 case GL_VERTEX_PROGRAM_ARB
:
1818 struct st_vertex_program
*stvp
= (struct st_vertex_program
*) target
;
1819 struct st_vp_variant
*vpv
, **prevPtr
= &stvp
->variants
;
1821 for (vpv
= stvp
->variants
; vpv
; ) {
1822 struct st_vp_variant
*next
= vpv
->next
;
1823 if (vpv
->key
.st
== st
) {
1824 /* unlink from list */
1826 /* destroy this variant */
1827 delete_vp_variant(st
, vpv
);
1830 prevPtr
= &vpv
->next
;
1836 case GL_FRAGMENT_PROGRAM_ARB
:
1838 struct st_common_program
*stfp
=
1839 (struct st_common_program
*) target
;
1840 struct st_fp_variant
*fpv
, **prevPtr
= &stfp
->fp_variants
;
1842 for (fpv
= stfp
->fp_variants
; fpv
; ) {
1843 struct st_fp_variant
*next
= fpv
->next
;
1844 if (fpv
->key
.st
== st
) {
1845 /* unlink from list */
1847 /* destroy this variant */
1848 delete_fp_variant(st
, fpv
);
1851 prevPtr
= &fpv
->next
;
1857 case GL_GEOMETRY_PROGRAM_NV
:
1858 case GL_TESS_CONTROL_PROGRAM_NV
:
1859 case GL_TESS_EVALUATION_PROGRAM_NV
:
1860 case GL_COMPUTE_PROGRAM_NV
:
1862 struct st_common_program
*p
= st_common_program(target
);
1863 struct st_common_variant
*v
, **prevPtr
= &p
->variants
;
1865 for (v
= p
->variants
; v
; ) {
1866 struct st_common_variant
*next
= v
->next
;
1867 if (v
->key
.st
== st
) {
1868 /* unlink from list */
1870 /* destroy this variant */
1871 delete_basic_variant(st
, v
, target
->Target
);
1881 _mesa_problem(NULL
, "Unexpected program target 0x%x in "
1882 "destroy_program_variants_cb()", target
->Target
);
1888 * Callback for _mesa_HashWalk. Free all the shader's program variants
1889 * which match the given context.
1892 destroy_shader_program_variants_cb(GLuint key
, void *data
, void *userData
)
1894 struct st_context
*st
= (struct st_context
*) userData
;
1895 struct gl_shader
*shader
= (struct gl_shader
*) data
;
1897 switch (shader
->Type
) {
1898 case GL_SHADER_PROGRAM_MESA
:
1900 struct gl_shader_program
*shProg
= (struct gl_shader_program
*) data
;
1903 for (i
= 0; i
< ARRAY_SIZE(shProg
->_LinkedShaders
); i
++) {
1904 if (shProg
->_LinkedShaders
[i
])
1905 destroy_program_variants(st
, shProg
->_LinkedShaders
[i
]->Program
);
1909 case GL_VERTEX_SHADER
:
1910 case GL_FRAGMENT_SHADER
:
1911 case GL_GEOMETRY_SHADER
:
1912 case GL_TESS_CONTROL_SHADER
:
1913 case GL_TESS_EVALUATION_SHADER
:
1914 case GL_COMPUTE_SHADER
:
1923 * Callback for _mesa_HashWalk. Free all the program variants which match
1924 * the given context.
1927 destroy_program_variants_cb(GLuint key
, void *data
, void *userData
)
1929 struct st_context
*st
= (struct st_context
*) userData
;
1930 struct gl_program
*program
= (struct gl_program
*) data
;
1931 destroy_program_variants(st
, program
);
1936 * Walk over all shaders and programs to delete any variants which
1937 * belong to the given context.
1938 * This is called during context tear-down.
1941 st_destroy_program_variants(struct st_context
*st
)
1943 /* If shaders can be shared with other contexts, the last context will
1944 * call DeleteProgram on all shaders, releasing everything.
1946 if (st
->has_shareable_shaders
)
1949 /* ARB vert/frag program */
1950 _mesa_HashWalk(st
->ctx
->Shared
->Programs
,
1951 destroy_program_variants_cb
, st
);
1953 /* GLSL vert/frag/geom shaders */
1954 _mesa_HashWalk(st
->ctx
->Shared
->ShaderObjects
,
1955 destroy_shader_program_variants_cb
, st
);
1960 * For debugging, print/dump the current vertex program.
1963 st_print_current_vertex_program(void)
1965 GET_CURRENT_CONTEXT(ctx
);
1967 if (ctx
->VertexProgram
._Current
) {
1968 struct st_vertex_program
*stvp
=
1969 (struct st_vertex_program
*) ctx
->VertexProgram
._Current
;
1970 struct st_vp_variant
*stv
;
1972 debug_printf("Vertex program %u\n", stvp
->Base
.Id
);
1974 for (stv
= stvp
->variants
; stv
; stv
= stv
->next
) {
1975 debug_printf("variant %p\n", stv
);
1976 tgsi_dump(stv
->tokens
, 0);
1983 * Compile one shader variant.
1986 st_precompile_shader_variant(struct st_context
*st
,
1987 struct gl_program
*prog
)
1989 switch (prog
->Target
) {
1990 case GL_VERTEX_PROGRAM_ARB
: {
1991 struct st_vertex_program
*p
= (struct st_vertex_program
*)prog
;
1992 struct st_common_variant_key key
;
1994 memset(&key
, 0, sizeof(key
));
1996 key
.st
= st
->has_shareable_shaders
? NULL
: st
;
1997 st_get_vp_variant(st
, p
, &key
);
2001 case GL_FRAGMENT_PROGRAM_ARB
: {
2002 struct st_common_program
*p
= (struct st_common_program
*)prog
;
2003 struct st_fp_variant_key key
;
2005 memset(&key
, 0, sizeof(key
));
2007 key
.st
= st
->has_shareable_shaders
? NULL
: st
;
2008 st_get_fp_variant(st
, p
, &key
);
2012 case GL_TESS_CONTROL_PROGRAM_NV
:
2013 case GL_TESS_EVALUATION_PROGRAM_NV
:
2014 case GL_GEOMETRY_PROGRAM_NV
:
2015 case GL_COMPUTE_PROGRAM_NV
: {
2016 struct st_common_program
*p
= st_common_program(prog
);
2017 struct st_common_variant_key key
;
2019 memset(&key
, 0, sizeof(key
));
2021 key
.st
= st
->has_shareable_shaders
? NULL
: st
;
2022 st_get_common_variant(st
, p
, &key
);