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_fragment_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
);
247 delete_ir(&vpv
->tgsi
);
255 * Clean out any old compilations:
258 st_release_vp_variants( struct st_context
*st
,
259 struct st_vertex_program
*stvp
)
261 struct st_vp_variant
*vpv
;
263 for (vpv
= stvp
->variants
; vpv
; ) {
264 struct st_vp_variant
*next
= vpv
->next
;
265 delete_vp_variant(st
, vpv
);
269 stvp
->variants
= NULL
;
271 delete_ir(&stvp
->tgsi
);
277 * Delete a fragment program variant. Note the caller must unlink
278 * the variant from the linked list.
281 delete_fp_variant(struct st_context
*st
, struct st_fp_variant
*fpv
)
283 if (fpv
->driver_shader
) {
284 if (st
->has_shareable_shaders
|| fpv
->key
.st
== st
) {
285 cso_delete_fragment_shader(st
->cso_context
, fpv
->driver_shader
);
287 st_save_zombie_shader(fpv
->key
.st
, PIPE_SHADER_FRAGMENT
,
297 * Free all variants of a fragment program.
300 st_release_fp_variants(struct st_context
*st
, struct st_fragment_program
*stfp
)
302 struct st_fp_variant
*fpv
;
304 for (fpv
= stfp
->variants
; fpv
; ) {
305 struct st_fp_variant
*next
= fpv
->next
;
306 delete_fp_variant(st
, fpv
);
310 stfp
->variants
= NULL
;
312 delete_ir(&stfp
->tgsi
);
317 * Delete a basic program variant. Note the caller must unlink
318 * the variant from the linked list.
321 delete_basic_variant(struct st_context
*st
, struct st_basic_variant
*v
,
324 if (v
->driver_shader
) {
325 if (st
->has_shareable_shaders
|| v
->key
.st
== st
) {
326 /* The shader's context matches the calling context, or we
330 case GL_TESS_CONTROL_PROGRAM_NV
:
331 cso_delete_tessctrl_shader(st
->cso_context
, v
->driver_shader
);
333 case GL_TESS_EVALUATION_PROGRAM_NV
:
334 cso_delete_tesseval_shader(st
->cso_context
, v
->driver_shader
);
336 case GL_GEOMETRY_PROGRAM_NV
:
337 cso_delete_geometry_shader(st
->cso_context
, v
->driver_shader
);
339 case GL_COMPUTE_PROGRAM_NV
:
340 cso_delete_compute_shader(st
->cso_context
, v
->driver_shader
);
343 unreachable("bad shader type in delete_basic_variant");
346 /* We can't delete a shader with a context different from the one
347 * that created it. Add it to the creating context's zombie list.
349 enum pipe_shader_type type
;
351 case GL_TESS_CONTROL_PROGRAM_NV
:
352 type
= PIPE_SHADER_TESS_CTRL
;
354 case GL_TESS_EVALUATION_PROGRAM_NV
:
355 type
= PIPE_SHADER_TESS_EVAL
;
357 case GL_GEOMETRY_PROGRAM_NV
:
358 type
= PIPE_SHADER_GEOMETRY
;
363 st_save_zombie_shader(v
->key
.st
, type
, v
->driver_shader
);
372 * Free all basic program variants.
375 st_release_basic_variants(struct st_context
*st
, struct st_common_program
*p
)
377 struct st_basic_variant
*v
;
379 for (v
= p
->variants
; v
; ) {
380 struct st_basic_variant
*next
= v
->next
;
381 delete_basic_variant(st
, v
, p
->Base
.Target
);
391 * Translate ARB (asm) program to NIR
394 st_translate_prog_to_nir(struct st_context
*st
, struct gl_program
*prog
,
395 gl_shader_stage stage
)
397 const struct gl_shader_compiler_options
*options
=
398 &st
->ctx
->Const
.ShaderCompilerOptions
[stage
];
400 /* Translate to NIR */
401 nir_shader
*nir
= prog_to_nir(prog
, options
->NirOptions
);
402 NIR_PASS_V(nir
, nir_lower_regs_to_ssa
); /* turn registers into SSA */
403 nir_validate_shader(nir
, "after st/ptn lower_regs_to_ssa");
405 NIR_PASS_V(nir
, st_nir_lower_wpos_ytransform
, prog
, st
->pipe
->screen
);
406 NIR_PASS_V(nir
, nir_lower_system_values
);
409 NIR_PASS_V(nir
, nir_opt_constant_folding
);
411 nir_validate_shader(nir
, "after st/ptn NIR opts");
417 st_prepare_vertex_program(struct st_vertex_program
*stvp
)
419 stvp
->num_inputs
= 0;
420 memset(stvp
->input_to_index
, ~0, sizeof(stvp
->input_to_index
));
421 memset(stvp
->result_to_output
, ~0, sizeof(stvp
->result_to_output
));
423 /* Determine number of inputs, the mappings between VERT_ATTRIB_x
424 * and TGSI generic input indexes, plus input attrib semantic info.
426 for (unsigned attr
= 0; attr
< VERT_ATTRIB_MAX
; attr
++) {
427 if ((stvp
->Base
.info
.inputs_read
& BITFIELD64_BIT(attr
)) != 0) {
428 stvp
->input_to_index
[attr
] = stvp
->num_inputs
;
429 stvp
->index_to_input
[stvp
->num_inputs
] = attr
;
432 if ((stvp
->Base
.DualSlotInputs
& BITFIELD64_BIT(attr
)) != 0) {
433 /* add placeholder for second part of a double attribute */
434 stvp
->index_to_input
[stvp
->num_inputs
] = ST_DOUBLE_ATTRIB_PLACEHOLDER
;
439 /* pre-setup potentially unused edgeflag input */
440 stvp
->input_to_index
[VERT_ATTRIB_EDGEFLAG
] = stvp
->num_inputs
;
441 stvp
->index_to_input
[stvp
->num_inputs
] = VERT_ATTRIB_EDGEFLAG
;
443 /* Compute mapping of vertex program outputs to slots. */
444 unsigned num_outputs
= 0;
445 for (unsigned attr
= 0; attr
< VARYING_SLOT_MAX
; attr
++) {
446 if (stvp
->Base
.info
.outputs_written
& BITFIELD64_BIT(attr
))
447 stvp
->result_to_output
[attr
] = num_outputs
++;
449 /* pre-setup potentially unused edgeflag output */
450 stvp
->result_to_output
[VARYING_SLOT_EDGE
] = num_outputs
;
454 st_translate_stream_output_info(struct gl_program
*prog
)
456 struct gl_transform_feedback_info
*info
= prog
->sh
.LinkedTransformFeedback
;
460 /* Determine the (default) output register mapping for each output. */
461 unsigned num_outputs
= 0;
462 ubyte output_mapping
[VARYING_SLOT_TESS_MAX
];
463 memset(output_mapping
, 0, sizeof(output_mapping
));
465 for (unsigned attr
= 0; attr
< VARYING_SLOT_MAX
; attr
++) {
466 if (prog
->info
.outputs_written
& BITFIELD64_BIT(attr
))
467 output_mapping
[attr
] = num_outputs
++;
470 /* Translate stream output info. */
471 struct pipe_stream_output_info
*so_info
= NULL
;
472 if (prog
->info
.stage
== MESA_SHADER_VERTEX
)
473 so_info
= &((struct st_vertex_program
*)prog
)->tgsi
.stream_output
;
475 so_info
= &((struct st_common_program
*)prog
)->tgsi
.stream_output
;
477 for (unsigned i
= 0; i
< info
->NumOutputs
; i
++) {
478 so_info
->output
[i
].register_index
=
479 output_mapping
[info
->Outputs
[i
].OutputRegister
];
480 so_info
->output
[i
].start_component
= info
->Outputs
[i
].ComponentOffset
;
481 so_info
->output
[i
].num_components
= info
->Outputs
[i
].NumComponents
;
482 so_info
->output
[i
].output_buffer
= info
->Outputs
[i
].OutputBuffer
;
483 so_info
->output
[i
].dst_offset
= info
->Outputs
[i
].DstOffset
;
484 so_info
->output
[i
].stream
= info
->Outputs
[i
].StreamId
;
487 for (unsigned i
= 0; i
< PIPE_MAX_SO_BUFFERS
; i
++) {
488 so_info
->stride
[i
] = info
->Buffers
[i
].Stride
;
490 so_info
->num_outputs
= info
->NumOutputs
;
494 * Translate a vertex program.
497 st_translate_vertex_program(struct st_context
*st
,
498 struct st_vertex_program
*stvp
)
500 struct ureg_program
*ureg
;
501 enum pipe_error error
;
502 unsigned num_outputs
= 0;
504 ubyte output_semantic_name
[VARYING_SLOT_MAX
] = {0};
505 ubyte output_semantic_index
[VARYING_SLOT_MAX
] = {0};
507 if (stvp
->Base
.arb
.IsPositionInvariant
)
508 _mesa_insert_mvp_code(st
->ctx
, &stvp
->Base
);
510 st_prepare_vertex_program(stvp
);
512 /* Get semantic names and indices. */
513 for (attr
= 0; attr
< VARYING_SLOT_MAX
; attr
++) {
514 if (stvp
->Base
.info
.outputs_written
& BITFIELD64_BIT(attr
)) {
515 unsigned slot
= num_outputs
++;
516 unsigned semantic_name
, semantic_index
;
517 tgsi_get_gl_varying_semantic(attr
, st
->needs_texcoord_semantic
,
518 &semantic_name
, &semantic_index
);
519 output_semantic_name
[slot
] = semantic_name
;
520 output_semantic_index
[slot
] = semantic_index
;
523 /* pre-setup potentially unused edgeflag output */
524 output_semantic_name
[num_outputs
] = TGSI_SEMANTIC_EDGEFLAG
;
525 output_semantic_index
[num_outputs
] = 0;
528 if (!stvp
->glsl_to_tgsi
) {
529 _mesa_remove_output_reads(&stvp
->Base
, PROGRAM_OUTPUT
);
531 /* This determines which states will be updated when the assembly
534 stvp
->affected_states
= ST_NEW_VS_STATE
|
536 ST_NEW_VERTEX_ARRAYS
;
538 if (stvp
->Base
.Parameters
->NumParameters
)
539 stvp
->affected_states
|= ST_NEW_VS_CONSTANTS
;
541 /* No samplers are allowed in ARB_vp. */
544 ureg
= ureg_create_with_screen(PIPE_SHADER_VERTEX
, st
->pipe
->screen
);
548 if (stvp
->Base
.info
.clip_distance_array_size
)
549 ureg_property(ureg
, TGSI_PROPERTY_NUM_CLIPDIST_ENABLED
,
550 stvp
->Base
.info
.clip_distance_array_size
);
551 if (stvp
->Base
.info
.cull_distance_array_size
)
552 ureg_property(ureg
, TGSI_PROPERTY_NUM_CULLDIST_ENABLED
,
553 stvp
->Base
.info
.cull_distance_array_size
);
555 if (ST_DEBUG
& DEBUG_MESA
) {
556 _mesa_print_program(&stvp
->Base
);
557 _mesa_print_program_parameters(st
->ctx
, &stvp
->Base
);
561 if (stvp
->glsl_to_tgsi
) {
562 error
= st_translate_program(st
->ctx
,
569 stvp
->input_to_index
,
570 NULL
, /* inputSlotToAttr */
571 NULL
, /* input semantic name */
572 NULL
, /* input semantic index */
573 NULL
, /* interp mode */
576 stvp
->result_to_output
,
577 output_semantic_name
,
578 output_semantic_index
);
580 st_translate_stream_output_info(&stvp
->Base
);
582 free_glsl_to_tgsi_visitor(stvp
->glsl_to_tgsi
);
584 error
= st_translate_mesa_program(st
->ctx
,
590 stvp
->input_to_index
,
591 NULL
, /* input semantic name */
592 NULL
, /* input semantic index */
596 stvp
->result_to_output
,
597 output_semantic_name
,
598 output_semantic_index
);
601 debug_printf("%s: failed to translate Mesa program:\n", __func__
);
602 _mesa_print_program(&stvp
->Base
);
607 stvp
->tgsi
.tokens
= ureg_get_tokens(ureg
, &stvp
->num_tgsi_tokens
);
610 if (stvp
->glsl_to_tgsi
) {
611 stvp
->glsl_to_tgsi
= NULL
;
612 st_store_ir_in_disk_cache(st
, &stvp
->Base
, false);
615 bool use_nir
= PIPE_SHADER_IR_NIR
==
616 st
->pipe
->screen
->get_shader_param(st
->pipe
->screen
, PIPE_SHADER_VERTEX
,
617 PIPE_SHADER_CAP_PREFERRED_IR
);
621 st_translate_prog_to_nir(st
, &stvp
->Base
, MESA_SHADER_VERTEX
);
623 if (stvp
->tgsi
.ir
.nir
)
624 ralloc_free(stvp
->tgsi
.ir
.nir
);
625 stvp
->tgsi
.type
= PIPE_SHADER_IR_NIR
;
626 stvp
->tgsi
.ir
.nir
= nir
;
627 stvp
->Base
.nir
= nir
;
631 return stvp
->tgsi
.tokens
!= NULL
;
634 static const gl_state_index16 depth_range_state
[STATE_LENGTH
] =
635 { STATE_DEPTH_RANGE
};
637 static struct st_vp_variant
*
638 st_create_vp_variant(struct st_context
*st
,
639 struct st_vertex_program
*stvp
,
640 const struct st_vp_variant_key
*key
)
642 struct st_vp_variant
*vpv
= CALLOC_STRUCT(st_vp_variant
);
643 struct pipe_context
*pipe
= st
->pipe
;
644 struct gl_program_parameter_list
*params
= stvp
->Base
.Parameters
;
647 vpv
->tgsi
.stream_output
= stvp
->tgsi
.stream_output
;
648 vpv
->num_inputs
= stvp
->num_inputs
;
650 /* When generating a NIR program, we usually don't have TGSI tokens.
651 * However, we do create them for ARB_vertex_program / fixed-function VS
652 * programs which we may need to use with the draw module for legacy
653 * feedback/select emulation. If they exist, copy them.
655 if (stvp
->tgsi
.tokens
)
656 vpv
->tgsi
.tokens
= tgsi_dup_tokens(stvp
->tgsi
.tokens
);
658 if (stvp
->tgsi
.type
== PIPE_SHADER_IR_NIR
) {
659 vpv
->tgsi
.type
= PIPE_SHADER_IR_NIR
;
660 vpv
->tgsi
.ir
.nir
= nir_shader_clone(NULL
, stvp
->tgsi
.ir
.nir
);
661 if (key
->clamp_color
)
662 NIR_PASS_V(vpv
->tgsi
.ir
.nir
, nir_lower_clamp_color_outputs
);
663 if (key
->passthrough_edgeflags
) {
664 NIR_PASS_V(vpv
->tgsi
.ir
.nir
, nir_lower_passthrough_edgeflags
);
668 st_finalize_nir(st
, &stvp
->Base
, stvp
->shader_program
,
671 vpv
->driver_shader
= pipe
->create_vs_state(pipe
, &vpv
->tgsi
);
672 /* driver takes ownership of IR: */
673 vpv
->tgsi
.ir
.nir
= NULL
;
677 /* Emulate features. */
678 if (key
->clamp_color
|| key
->passthrough_edgeflags
) {
679 const struct tgsi_token
*tokens
;
681 (key
->clamp_color
? TGSI_EMU_CLAMP_COLOR_OUTPUTS
: 0) |
682 (key
->passthrough_edgeflags
? TGSI_EMU_PASSTHROUGH_EDGEFLAG
: 0);
684 tokens
= tgsi_emulate(vpv
->tgsi
.tokens
, flags
);
687 tgsi_free_tokens(vpv
->tgsi
.tokens
);
688 vpv
->tgsi
.tokens
= tokens
;
690 if (key
->passthrough_edgeflags
)
693 fprintf(stderr
, "mesa: cannot emulate deprecated features\n");
696 if (key
->lower_depth_clamp
) {
697 unsigned depth_range_const
=
698 _mesa_add_state_reference(params
, depth_range_state
);
700 const struct tgsi_token
*tokens
;
701 tokens
= st_tgsi_lower_depth_clamp(vpv
->tgsi
.tokens
, depth_range_const
,
702 key
->clip_negative_one_to_one
);
703 if (tokens
!= vpv
->tgsi
.tokens
)
704 tgsi_free_tokens(vpv
->tgsi
.tokens
);
705 vpv
->tgsi
.tokens
= tokens
;
708 if (ST_DEBUG
& DEBUG_TGSI
) {
709 tgsi_dump(vpv
->tgsi
.tokens
, 0);
713 vpv
->driver_shader
= pipe
->create_vs_state(pipe
, &vpv
->tgsi
);
719 * Find/create a vertex program variant.
721 struct st_vp_variant
*
722 st_get_vp_variant(struct st_context
*st
,
723 struct st_vertex_program
*stvp
,
724 const struct st_vp_variant_key
*key
)
726 struct st_vp_variant
*vpv
;
728 /* Search for existing variant */
729 for (vpv
= stvp
->variants
; vpv
; vpv
= vpv
->next
) {
730 if (memcmp(&vpv
->key
, key
, sizeof(*key
)) == 0) {
737 vpv
= st_create_vp_variant(st
, stvp
, key
);
739 for (unsigned index
= 0; index
< vpv
->num_inputs
; ++index
) {
740 unsigned attr
= stvp
->index_to_input
[index
];
741 if (attr
== ST_DOUBLE_ATTRIB_PLACEHOLDER
)
743 vpv
->vert_attrib_mask
|= 1u << attr
;
746 /* insert into list */
747 vpv
->next
= stvp
->variants
;
748 stvp
->variants
= vpv
;
757 * Translate a Mesa fragment shader into a TGSI shader.
760 st_translate_fragment_program(struct st_context
*st
,
761 struct st_fragment_program
*stfp
)
763 /* Non-GLSL programs: */
764 if (!stfp
->glsl_to_tgsi
) {
765 _mesa_remove_output_reads(&stfp
->Base
, PROGRAM_OUTPUT
);
766 if (st
->ctx
->Const
.GLSLFragCoordIsSysVal
)
767 _mesa_program_fragment_position_to_sysval(&stfp
->Base
);
769 /* This determines which states will be updated when the assembly
772 * fragment.position and glDrawPixels always use constants.
774 stfp
->affected_states
= ST_NEW_FS_STATE
|
775 ST_NEW_SAMPLE_SHADING
|
779 /* Just set them for ATI_fs unconditionally. */
780 stfp
->affected_states
|= ST_NEW_FS_SAMPLER_VIEWS
|
784 if (stfp
->Base
.SamplersUsed
)
785 stfp
->affected_states
|= ST_NEW_FS_SAMPLER_VIEWS
|
789 /* Translate to NIR. */
791 st
->pipe
->screen
->get_shader_param(st
->pipe
->screen
,
792 PIPE_SHADER_FRAGMENT
,
793 PIPE_SHADER_CAP_PREFERRED_IR
)) {
795 st_translate_prog_to_nir(st
, &stfp
->Base
, MESA_SHADER_FRAGMENT
);
797 if (stfp
->tgsi
.ir
.nir
)
798 ralloc_free(stfp
->tgsi
.ir
.nir
);
799 stfp
->tgsi
.type
= PIPE_SHADER_IR_NIR
;
800 stfp
->tgsi
.ir
.nir
= nir
;
801 stfp
->Base
.nir
= nir
;
806 ubyte outputMapping
[2 * FRAG_RESULT_MAX
];
807 ubyte inputMapping
[VARYING_SLOT_MAX
];
808 ubyte inputSlotToAttr
[VARYING_SLOT_MAX
];
809 ubyte interpMode
[PIPE_MAX_SHADER_INPUTS
]; /* XXX size? */
811 GLbitfield64 inputsRead
;
812 struct ureg_program
*ureg
;
814 GLboolean write_all
= GL_FALSE
;
816 ubyte input_semantic_name
[PIPE_MAX_SHADER_INPUTS
];
817 ubyte input_semantic_index
[PIPE_MAX_SHADER_INPUTS
];
818 uint fs_num_inputs
= 0;
820 ubyte fs_output_semantic_name
[PIPE_MAX_SHADER_OUTPUTS
];
821 ubyte fs_output_semantic_index
[PIPE_MAX_SHADER_OUTPUTS
];
822 uint fs_num_outputs
= 0;
824 memset(inputSlotToAttr
, ~0, sizeof(inputSlotToAttr
));
827 * Convert Mesa program inputs to TGSI input register semantics.
829 inputsRead
= stfp
->Base
.info
.inputs_read
;
830 for (attr
= 0; attr
< VARYING_SLOT_MAX
; attr
++) {
831 if ((inputsRead
& BITFIELD64_BIT(attr
)) != 0) {
832 const GLuint slot
= fs_num_inputs
++;
834 inputMapping
[attr
] = slot
;
835 inputSlotToAttr
[slot
] = attr
;
838 case VARYING_SLOT_POS
:
839 input_semantic_name
[slot
] = TGSI_SEMANTIC_POSITION
;
840 input_semantic_index
[slot
] = 0;
841 interpMode
[slot
] = TGSI_INTERPOLATE_LINEAR
;
843 case VARYING_SLOT_COL0
:
844 input_semantic_name
[slot
] = TGSI_SEMANTIC_COLOR
;
845 input_semantic_index
[slot
] = 0;
846 interpMode
[slot
] = stfp
->glsl_to_tgsi
?
847 TGSI_INTERPOLATE_COUNT
: TGSI_INTERPOLATE_COLOR
;
849 case VARYING_SLOT_COL1
:
850 input_semantic_name
[slot
] = TGSI_SEMANTIC_COLOR
;
851 input_semantic_index
[slot
] = 1;
852 interpMode
[slot
] = stfp
->glsl_to_tgsi
?
853 TGSI_INTERPOLATE_COUNT
: TGSI_INTERPOLATE_COLOR
;
855 case VARYING_SLOT_FOGC
:
856 input_semantic_name
[slot
] = TGSI_SEMANTIC_FOG
;
857 input_semantic_index
[slot
] = 0;
858 interpMode
[slot
] = TGSI_INTERPOLATE_PERSPECTIVE
;
860 case VARYING_SLOT_FACE
:
861 input_semantic_name
[slot
] = TGSI_SEMANTIC_FACE
;
862 input_semantic_index
[slot
] = 0;
863 interpMode
[slot
] = TGSI_INTERPOLATE_CONSTANT
;
865 case VARYING_SLOT_PRIMITIVE_ID
:
866 input_semantic_name
[slot
] = TGSI_SEMANTIC_PRIMID
;
867 input_semantic_index
[slot
] = 0;
868 interpMode
[slot
] = TGSI_INTERPOLATE_CONSTANT
;
870 case VARYING_SLOT_LAYER
:
871 input_semantic_name
[slot
] = TGSI_SEMANTIC_LAYER
;
872 input_semantic_index
[slot
] = 0;
873 interpMode
[slot
] = TGSI_INTERPOLATE_CONSTANT
;
875 case VARYING_SLOT_VIEWPORT
:
876 input_semantic_name
[slot
] = TGSI_SEMANTIC_VIEWPORT_INDEX
;
877 input_semantic_index
[slot
] = 0;
878 interpMode
[slot
] = TGSI_INTERPOLATE_CONSTANT
;
880 case VARYING_SLOT_CLIP_DIST0
:
881 input_semantic_name
[slot
] = TGSI_SEMANTIC_CLIPDIST
;
882 input_semantic_index
[slot
] = 0;
883 interpMode
[slot
] = TGSI_INTERPOLATE_PERSPECTIVE
;
885 case VARYING_SLOT_CLIP_DIST1
:
886 input_semantic_name
[slot
] = TGSI_SEMANTIC_CLIPDIST
;
887 input_semantic_index
[slot
] = 1;
888 interpMode
[slot
] = TGSI_INTERPOLATE_PERSPECTIVE
;
890 case VARYING_SLOT_CULL_DIST0
:
891 case VARYING_SLOT_CULL_DIST1
:
892 /* these should have been lowered by GLSL */
895 /* In most cases, there is nothing special about these
896 * inputs, so adopt a convention to use the generic
897 * semantic name and the mesa VARYING_SLOT_ number as the
900 * All that is required is that the vertex shader labels
901 * its own outputs similarly, and that the vertex shader
902 * generates at least every output required by the
903 * fragment shader plus fixed-function hardware (such as
906 * However, some drivers may need us to identify the PNTC and TEXi
907 * varyings if, for example, their capability to replace them with
908 * sprite coordinates is limited.
910 case VARYING_SLOT_PNTC
:
911 if (st
->needs_texcoord_semantic
) {
912 input_semantic_name
[slot
] = TGSI_SEMANTIC_PCOORD
;
913 input_semantic_index
[slot
] = 0;
914 interpMode
[slot
] = TGSI_INTERPOLATE_LINEAR
;
918 case VARYING_SLOT_TEX0
:
919 case VARYING_SLOT_TEX1
:
920 case VARYING_SLOT_TEX2
:
921 case VARYING_SLOT_TEX3
:
922 case VARYING_SLOT_TEX4
:
923 case VARYING_SLOT_TEX5
:
924 case VARYING_SLOT_TEX6
:
925 case VARYING_SLOT_TEX7
:
926 if (st
->needs_texcoord_semantic
) {
927 input_semantic_name
[slot
] = TGSI_SEMANTIC_TEXCOORD
;
928 input_semantic_index
[slot
] = attr
- VARYING_SLOT_TEX0
;
929 interpMode
[slot
] = stfp
->glsl_to_tgsi
?
930 TGSI_INTERPOLATE_COUNT
: TGSI_INTERPOLATE_PERSPECTIVE
;
934 case VARYING_SLOT_VAR0
:
936 /* Semantic indices should be zero-based because drivers may choose
937 * to assign a fixed slot determined by that index.
938 * This is useful because ARB_separate_shader_objects uses location
939 * qualifiers for linkage, and if the semantic index corresponds to
940 * these locations, linkage passes in the driver become unecessary.
942 * If needs_texcoord_semantic is true, no semantic indices will be
943 * consumed for the TEXi varyings, and we can base the locations of
944 * the user varyings on VAR0. Otherwise, we use TEX0 as base index.
946 assert(attr
>= VARYING_SLOT_VAR0
|| attr
== VARYING_SLOT_PNTC
||
947 (attr
>= VARYING_SLOT_TEX0
&& attr
<= VARYING_SLOT_TEX7
));
948 input_semantic_name
[slot
] = TGSI_SEMANTIC_GENERIC
;
949 input_semantic_index
[slot
] = st_get_generic_varying_index(st
, attr
);
950 if (attr
== VARYING_SLOT_PNTC
)
951 interpMode
[slot
] = TGSI_INTERPOLATE_LINEAR
;
953 interpMode
[slot
] = stfp
->glsl_to_tgsi
?
954 TGSI_INTERPOLATE_COUNT
: TGSI_INTERPOLATE_PERSPECTIVE
;
960 inputMapping
[attr
] = -1;
965 * Semantics and mapping for outputs
967 GLbitfield64 outputsWritten
= stfp
->Base
.info
.outputs_written
;
969 /* if z is written, emit that first */
970 if (outputsWritten
& BITFIELD64_BIT(FRAG_RESULT_DEPTH
)) {
971 fs_output_semantic_name
[fs_num_outputs
] = TGSI_SEMANTIC_POSITION
;
972 fs_output_semantic_index
[fs_num_outputs
] = 0;
973 outputMapping
[FRAG_RESULT_DEPTH
] = fs_num_outputs
;
975 outputsWritten
&= ~(1 << FRAG_RESULT_DEPTH
);
978 if (outputsWritten
& BITFIELD64_BIT(FRAG_RESULT_STENCIL
)) {
979 fs_output_semantic_name
[fs_num_outputs
] = TGSI_SEMANTIC_STENCIL
;
980 fs_output_semantic_index
[fs_num_outputs
] = 0;
981 outputMapping
[FRAG_RESULT_STENCIL
] = fs_num_outputs
;
983 outputsWritten
&= ~(1 << FRAG_RESULT_STENCIL
);
986 if (outputsWritten
& BITFIELD64_BIT(FRAG_RESULT_SAMPLE_MASK
)) {
987 fs_output_semantic_name
[fs_num_outputs
] = TGSI_SEMANTIC_SAMPLEMASK
;
988 fs_output_semantic_index
[fs_num_outputs
] = 0;
989 outputMapping
[FRAG_RESULT_SAMPLE_MASK
] = fs_num_outputs
;
991 outputsWritten
&= ~(1 << FRAG_RESULT_SAMPLE_MASK
);
994 /* handle remaining outputs (color) */
995 for (attr
= 0; attr
< ARRAY_SIZE(outputMapping
); attr
++) {
996 const GLbitfield64 written
= attr
< FRAG_RESULT_MAX
? outputsWritten
:
997 stfp
->Base
.SecondaryOutputsWritten
;
998 const unsigned loc
= attr
% FRAG_RESULT_MAX
;
1000 if (written
& BITFIELD64_BIT(loc
)) {
1002 case FRAG_RESULT_DEPTH
:
1003 case FRAG_RESULT_STENCIL
:
1004 case FRAG_RESULT_SAMPLE_MASK
:
1008 case FRAG_RESULT_COLOR
:
1009 write_all
= GL_TRUE
; /* fallthrough */
1012 assert(loc
== FRAG_RESULT_COLOR
||
1013 (FRAG_RESULT_DATA0
<= loc
&& loc
< FRAG_RESULT_MAX
));
1015 index
= (loc
== FRAG_RESULT_COLOR
) ? 0 : (loc
- FRAG_RESULT_DATA0
);
1017 if (attr
>= FRAG_RESULT_MAX
) {
1018 /* Secondary color for dual source blending. */
1023 fs_output_semantic_name
[fs_num_outputs
] = TGSI_SEMANTIC_COLOR
;
1024 fs_output_semantic_index
[fs_num_outputs
] = index
;
1025 outputMapping
[attr
] = fs_num_outputs
;
1034 ureg
= ureg_create_with_screen(PIPE_SHADER_FRAGMENT
, st
->pipe
->screen
);
1038 if (ST_DEBUG
& DEBUG_MESA
) {
1039 _mesa_print_program(&stfp
->Base
);
1040 _mesa_print_program_parameters(st
->ctx
, &stfp
->Base
);
1043 if (write_all
== GL_TRUE
)
1044 ureg_property(ureg
, TGSI_PROPERTY_FS_COLOR0_WRITES_ALL_CBUFS
, 1);
1046 if (stfp
->Base
.info
.fs
.depth_layout
!= FRAG_DEPTH_LAYOUT_NONE
) {
1047 switch (stfp
->Base
.info
.fs
.depth_layout
) {
1048 case FRAG_DEPTH_LAYOUT_ANY
:
1049 ureg_property(ureg
, TGSI_PROPERTY_FS_DEPTH_LAYOUT
,
1050 TGSI_FS_DEPTH_LAYOUT_ANY
);
1052 case FRAG_DEPTH_LAYOUT_GREATER
:
1053 ureg_property(ureg
, TGSI_PROPERTY_FS_DEPTH_LAYOUT
,
1054 TGSI_FS_DEPTH_LAYOUT_GREATER
);
1056 case FRAG_DEPTH_LAYOUT_LESS
:
1057 ureg_property(ureg
, TGSI_PROPERTY_FS_DEPTH_LAYOUT
,
1058 TGSI_FS_DEPTH_LAYOUT_LESS
);
1060 case FRAG_DEPTH_LAYOUT_UNCHANGED
:
1061 ureg_property(ureg
, TGSI_PROPERTY_FS_DEPTH_LAYOUT
,
1062 TGSI_FS_DEPTH_LAYOUT_UNCHANGED
);
1069 if (stfp
->glsl_to_tgsi
) {
1070 st_translate_program(st
->ctx
,
1071 PIPE_SHADER_FRAGMENT
,
1079 input_semantic_name
,
1080 input_semantic_index
,
1085 fs_output_semantic_name
,
1086 fs_output_semantic_index
);
1088 free_glsl_to_tgsi_visitor(stfp
->glsl_to_tgsi
);
1089 } else if (stfp
->ati_fs
)
1090 st_translate_atifs_program(ureg
,
1096 input_semantic_name
,
1097 input_semantic_index
,
1102 fs_output_semantic_name
,
1103 fs_output_semantic_index
);
1105 st_translate_mesa_program(st
->ctx
,
1106 PIPE_SHADER_FRAGMENT
,
1112 input_semantic_name
,
1113 input_semantic_index
,
1118 fs_output_semantic_name
,
1119 fs_output_semantic_index
);
1121 stfp
->tgsi
.tokens
= ureg_get_tokens(ureg
, &stfp
->num_tgsi_tokens
);
1124 if (stfp
->glsl_to_tgsi
) {
1125 stfp
->glsl_to_tgsi
= NULL
;
1126 st_store_ir_in_disk_cache(st
, &stfp
->Base
, false);
1129 return stfp
->tgsi
.tokens
!= NULL
;
1132 static struct st_fp_variant
*
1133 st_create_fp_variant(struct st_context
*st
,
1134 struct st_fragment_program
*stfp
,
1135 const struct st_fp_variant_key
*key
)
1137 struct pipe_context
*pipe
= st
->pipe
;
1138 struct st_fp_variant
*variant
= CALLOC_STRUCT(st_fp_variant
);
1139 struct pipe_shader_state tgsi
= {0};
1140 struct gl_program_parameter_list
*params
= stfp
->Base
.Parameters
;
1141 static const gl_state_index16 texcoord_state
[STATE_LENGTH
] =
1142 { STATE_INTERNAL
, STATE_CURRENT_ATTRIB
, VERT_ATTRIB_TEX0
};
1143 static const gl_state_index16 scale_state
[STATE_LENGTH
] =
1144 { STATE_INTERNAL
, STATE_PT_SCALE
};
1145 static const gl_state_index16 bias_state
[STATE_LENGTH
] =
1146 { STATE_INTERNAL
, STATE_PT_BIAS
};
1151 if (stfp
->tgsi
.type
== PIPE_SHADER_IR_NIR
) {
1152 tgsi
.type
= PIPE_SHADER_IR_NIR
;
1153 tgsi
.ir
.nir
= nir_shader_clone(NULL
, stfp
->tgsi
.ir
.nir
);
1155 if (key
->clamp_color
)
1156 NIR_PASS_V(tgsi
.ir
.nir
, nir_lower_clamp_color_outputs
);
1158 if (key
->persample_shading
) {
1159 nir_shader
*shader
= tgsi
.ir
.nir
;
1160 nir_foreach_variable(var
, &shader
->inputs
)
1161 var
->data
.sample
= true;
1164 assert(!(key
->bitmap
&& key
->drawpixels
));
1168 nir_lower_bitmap_options options
= {0};
1170 variant
->bitmap_sampler
= ffs(~stfp
->Base
.SamplersUsed
) - 1;
1171 options
.sampler
= variant
->bitmap_sampler
;
1172 options
.swizzle_xxxx
= st
->bitmap
.tex_format
== PIPE_FORMAT_R8_UNORM
;
1174 NIR_PASS_V(tgsi
.ir
.nir
, nir_lower_bitmap
, &options
);
1177 /* glDrawPixels (color only) */
1178 if (key
->drawpixels
) {
1179 nir_lower_drawpixels_options options
= {{0}};
1180 unsigned samplers_used
= stfp
->Base
.SamplersUsed
;
1182 /* Find the first unused slot. */
1183 variant
->drawpix_sampler
= ffs(~samplers_used
) - 1;
1184 options
.drawpix_sampler
= variant
->drawpix_sampler
;
1185 samplers_used
|= (1 << variant
->drawpix_sampler
);
1187 options
.pixel_maps
= key
->pixelMaps
;
1188 if (key
->pixelMaps
) {
1189 variant
->pixelmap_sampler
= ffs(~samplers_used
) - 1;
1190 options
.pixelmap_sampler
= variant
->pixelmap_sampler
;
1193 options
.scale_and_bias
= key
->scaleAndBias
;
1194 if (key
->scaleAndBias
) {
1195 _mesa_add_state_reference(params
, scale_state
);
1196 memcpy(options
.scale_state_tokens
, scale_state
,
1197 sizeof(options
.scale_state_tokens
));
1198 _mesa_add_state_reference(params
, bias_state
);
1199 memcpy(options
.bias_state_tokens
, bias_state
,
1200 sizeof(options
.bias_state_tokens
));
1203 _mesa_add_state_reference(params
, texcoord_state
);
1204 memcpy(options
.texcoord_state_tokens
, texcoord_state
,
1205 sizeof(options
.texcoord_state_tokens
));
1207 NIR_PASS_V(tgsi
.ir
.nir
, nir_lower_drawpixels
, &options
);
1210 if (unlikely(key
->external
.lower_nv12
|| key
->external
.lower_iyuv
||
1211 key
->external
.lower_xy_uxvx
|| key
->external
.lower_yx_xuxv
||
1212 key
->external
.lower_ayuv
|| key
->external
.lower_xyuv
)) {
1213 nir_lower_tex_options options
= {0};
1214 options
.lower_y_uv_external
= key
->external
.lower_nv12
;
1215 options
.lower_y_u_v_external
= key
->external
.lower_iyuv
;
1216 options
.lower_xy_uxvx_external
= key
->external
.lower_xy_uxvx
;
1217 options
.lower_yx_xuxv_external
= key
->external
.lower_yx_xuxv
;
1218 options
.lower_ayuv_external
= key
->external
.lower_ayuv
;
1219 options
.lower_xyuv_external
= key
->external
.lower_xyuv
;
1220 NIR_PASS_V(tgsi
.ir
.nir
, nir_lower_tex
, &options
);
1223 st_finalize_nir(st
, &stfp
->Base
, stfp
->shader_program
, tgsi
.ir
.nir
);
1225 if (unlikely(key
->external
.lower_nv12
|| key
->external
.lower_iyuv
||
1226 key
->external
.lower_xy_uxvx
|| key
->external
.lower_yx_xuxv
)) {
1227 /* This pass needs to happen *after* nir_lower_sampler */
1228 NIR_PASS_V(tgsi
.ir
.nir
, st_nir_lower_tex_src_plane
,
1229 ~stfp
->Base
.SamplersUsed
,
1230 key
->external
.lower_nv12
|| key
->external
.lower_xy_uxvx
||
1231 key
->external
.lower_yx_xuxv
,
1232 key
->external
.lower_iyuv
);
1235 /* Some of the lowering above may have introduced new varyings */
1236 nir_shader_gather_info(tgsi
.ir
.nir
,
1237 nir_shader_get_entrypoint(tgsi
.ir
.nir
));
1239 variant
->driver_shader
= pipe
->create_fs_state(pipe
, &tgsi
);
1240 variant
->key
= *key
;
1245 tgsi
.tokens
= stfp
->tgsi
.tokens
;
1247 assert(!(key
->bitmap
&& key
->drawpixels
));
1249 /* Fix texture targets and add fog for ATI_fs */
1251 const struct tgsi_token
*tokens
= st_fixup_atifs(tgsi
.tokens
, key
);
1254 tgsi
.tokens
= tokens
;
1256 fprintf(stderr
, "mesa: cannot post-process ATI_fs\n");
1259 /* Emulate features. */
1260 if (key
->clamp_color
|| key
->persample_shading
) {
1261 const struct tgsi_token
*tokens
;
1263 (key
->clamp_color
? TGSI_EMU_CLAMP_COLOR_OUTPUTS
: 0) |
1264 (key
->persample_shading
? TGSI_EMU_FORCE_PERSAMPLE_INTERP
: 0);
1266 tokens
= tgsi_emulate(tgsi
.tokens
, flags
);
1269 if (tgsi
.tokens
!= stfp
->tgsi
.tokens
)
1270 tgsi_free_tokens(tgsi
.tokens
);
1271 tgsi
.tokens
= tokens
;
1273 fprintf(stderr
, "mesa: cannot emulate deprecated features\n");
1278 const struct tgsi_token
*tokens
;
1280 variant
->bitmap_sampler
= ffs(~stfp
->Base
.SamplersUsed
) - 1;
1282 tokens
= st_get_bitmap_shader(tgsi
.tokens
,
1283 st
->internal_target
,
1284 variant
->bitmap_sampler
,
1285 st
->needs_texcoord_semantic
,
1286 st
->bitmap
.tex_format
==
1287 PIPE_FORMAT_R8_UNORM
);
1290 if (tgsi
.tokens
!= stfp
->tgsi
.tokens
)
1291 tgsi_free_tokens(tgsi
.tokens
);
1292 tgsi
.tokens
= tokens
;
1294 fprintf(stderr
, "mesa: cannot create a shader for glBitmap\n");
1297 /* glDrawPixels (color only) */
1298 if (key
->drawpixels
) {
1299 const struct tgsi_token
*tokens
;
1300 unsigned scale_const
= 0, bias_const
= 0, texcoord_const
= 0;
1302 /* Find the first unused slot. */
1303 variant
->drawpix_sampler
= ffs(~stfp
->Base
.SamplersUsed
) - 1;
1305 if (key
->pixelMaps
) {
1306 unsigned samplers_used
= stfp
->Base
.SamplersUsed
|
1307 (1 << variant
->drawpix_sampler
);
1309 variant
->pixelmap_sampler
= ffs(~samplers_used
) - 1;
1312 if (key
->scaleAndBias
) {
1313 scale_const
= _mesa_add_state_reference(params
, scale_state
);
1314 bias_const
= _mesa_add_state_reference(params
, bias_state
);
1317 texcoord_const
= _mesa_add_state_reference(params
, texcoord_state
);
1319 tokens
= st_get_drawpix_shader(tgsi
.tokens
,
1320 st
->needs_texcoord_semantic
,
1321 key
->scaleAndBias
, scale_const
,
1322 bias_const
, key
->pixelMaps
,
1323 variant
->drawpix_sampler
,
1324 variant
->pixelmap_sampler
,
1325 texcoord_const
, st
->internal_target
);
1328 if (tgsi
.tokens
!= stfp
->tgsi
.tokens
)
1329 tgsi_free_tokens(tgsi
.tokens
);
1330 tgsi
.tokens
= tokens
;
1332 fprintf(stderr
, "mesa: cannot create a shader for glDrawPixels\n");
1335 if (unlikely(key
->external
.lower_nv12
|| key
->external
.lower_iyuv
||
1336 key
->external
.lower_xy_uxvx
|| key
->external
.lower_yx_xuxv
)) {
1337 const struct tgsi_token
*tokens
;
1339 /* samplers inserted would conflict, but this should be unpossible: */
1340 assert(!(key
->bitmap
|| key
->drawpixels
));
1342 tokens
= st_tgsi_lower_yuv(tgsi
.tokens
,
1343 ~stfp
->Base
.SamplersUsed
,
1344 key
->external
.lower_nv12
||
1345 key
->external
.lower_xy_uxvx
||
1346 key
->external
.lower_yx_xuxv
,
1347 key
->external
.lower_iyuv
);
1349 if (tgsi
.tokens
!= stfp
->tgsi
.tokens
)
1350 tgsi_free_tokens(tgsi
.tokens
);
1351 tgsi
.tokens
= tokens
;
1353 fprintf(stderr
, "mesa: cannot create a shader for samplerExternalOES\n");
1357 if (key
->lower_depth_clamp
) {
1358 unsigned depth_range_const
= _mesa_add_state_reference(params
, depth_range_state
);
1360 const struct tgsi_token
*tokens
;
1361 tokens
= st_tgsi_lower_depth_clamp_fs(tgsi
.tokens
, depth_range_const
);
1362 if (tgsi
.tokens
!= stfp
->tgsi
.tokens
)
1363 tgsi_free_tokens(tgsi
.tokens
);
1364 tgsi
.tokens
= tokens
;
1367 if (ST_DEBUG
& DEBUG_TGSI
) {
1368 tgsi_dump(tgsi
.tokens
, 0);
1372 /* fill in variant */
1373 variant
->driver_shader
= pipe
->create_fs_state(pipe
, &tgsi
);
1374 variant
->key
= *key
;
1376 if (tgsi
.tokens
!= stfp
->tgsi
.tokens
)
1377 tgsi_free_tokens(tgsi
.tokens
);
1382 * Translate fragment program if needed.
1384 struct st_fp_variant
*
1385 st_get_fp_variant(struct st_context
*st
,
1386 struct st_fragment_program
*stfp
,
1387 const struct st_fp_variant_key
*key
)
1389 struct st_fp_variant
*fpv
;
1391 /* Search for existing variant */
1392 for (fpv
= stfp
->variants
; fpv
; fpv
= fpv
->next
) {
1393 if (memcmp(&fpv
->key
, key
, sizeof(*key
)) == 0) {
1400 fpv
= st_create_fp_variant(st
, stfp
, key
);
1402 if (key
->bitmap
|| key
->drawpixels
) {
1403 /* Regular variants should always come before the
1404 * bitmap & drawpixels variants, (unless there
1405 * are no regular variants) so that
1406 * st_update_fp can take a fast path when
1407 * shader_has_one_variant is set.
1409 if (!stfp
->variants
) {
1410 stfp
->variants
= fpv
;
1412 /* insert into list after the first one */
1413 fpv
->next
= stfp
->variants
->next
;
1414 stfp
->variants
->next
= fpv
;
1417 /* insert into list */
1418 fpv
->next
= stfp
->variants
;
1419 stfp
->variants
= fpv
;
1428 * Translate a program. This is common code for geometry and tessellation
1432 st_translate_common_program(struct st_context
*st
,
1433 struct st_common_program
*stcp
)
1435 struct gl_program
*prog
= &stcp
->Base
;
1436 enum pipe_shader_type stage
=
1437 pipe_shader_type_from_mesa(stcp
->Base
.info
.stage
);
1438 struct ureg_program
*ureg
= ureg_create_with_screen(stage
, st
->pipe
->screen
);
1444 case PIPE_SHADER_TESS_CTRL
:
1445 ureg_property(ureg
, TGSI_PROPERTY_TCS_VERTICES_OUT
,
1446 stcp
->Base
.info
.tess
.tcs_vertices_out
);
1449 case PIPE_SHADER_TESS_EVAL
:
1450 if (stcp
->Base
.info
.tess
.primitive_mode
== GL_ISOLINES
)
1451 ureg_property(ureg
, TGSI_PROPERTY_TES_PRIM_MODE
, GL_LINES
);
1453 ureg_property(ureg
, TGSI_PROPERTY_TES_PRIM_MODE
,
1454 stcp
->Base
.info
.tess
.primitive_mode
);
1456 STATIC_ASSERT((TESS_SPACING_EQUAL
+ 1) % 3 == PIPE_TESS_SPACING_EQUAL
);
1457 STATIC_ASSERT((TESS_SPACING_FRACTIONAL_ODD
+ 1) % 3 ==
1458 PIPE_TESS_SPACING_FRACTIONAL_ODD
);
1459 STATIC_ASSERT((TESS_SPACING_FRACTIONAL_EVEN
+ 1) % 3 ==
1460 PIPE_TESS_SPACING_FRACTIONAL_EVEN
);
1462 ureg_property(ureg
, TGSI_PROPERTY_TES_SPACING
,
1463 (stcp
->Base
.info
.tess
.spacing
+ 1) % 3);
1465 ureg_property(ureg
, TGSI_PROPERTY_TES_VERTEX_ORDER_CW
,
1466 !stcp
->Base
.info
.tess
.ccw
);
1467 ureg_property(ureg
, TGSI_PROPERTY_TES_POINT_MODE
,
1468 stcp
->Base
.info
.tess
.point_mode
);
1471 case PIPE_SHADER_GEOMETRY
:
1472 ureg_property(ureg
, TGSI_PROPERTY_GS_INPUT_PRIM
,
1473 stcp
->Base
.info
.gs
.input_primitive
);
1474 ureg_property(ureg
, TGSI_PROPERTY_GS_OUTPUT_PRIM
,
1475 stcp
->Base
.info
.gs
.output_primitive
);
1476 ureg_property(ureg
, TGSI_PROPERTY_GS_MAX_OUTPUT_VERTICES
,
1477 stcp
->Base
.info
.gs
.vertices_out
);
1478 ureg_property(ureg
, TGSI_PROPERTY_GS_INVOCATIONS
,
1479 stcp
->Base
.info
.gs
.invocations
);
1486 ubyte inputSlotToAttr
[VARYING_SLOT_TESS_MAX
];
1487 ubyte inputMapping
[VARYING_SLOT_TESS_MAX
];
1488 ubyte outputMapping
[VARYING_SLOT_TESS_MAX
];
1491 ubyte input_semantic_name
[PIPE_MAX_SHADER_INPUTS
];
1492 ubyte input_semantic_index
[PIPE_MAX_SHADER_INPUTS
];
1493 uint num_inputs
= 0;
1495 ubyte output_semantic_name
[PIPE_MAX_SHADER_OUTPUTS
];
1496 ubyte output_semantic_index
[PIPE_MAX_SHADER_OUTPUTS
];
1497 uint num_outputs
= 0;
1501 memset(inputSlotToAttr
, 0, sizeof(inputSlotToAttr
));
1502 memset(inputMapping
, 0, sizeof(inputMapping
));
1503 memset(outputMapping
, 0, sizeof(outputMapping
));
1504 memset(&stcp
->tgsi
, 0, sizeof(stcp
->tgsi
));
1506 if (prog
->info
.clip_distance_array_size
)
1507 ureg_property(ureg
, TGSI_PROPERTY_NUM_CLIPDIST_ENABLED
,
1508 prog
->info
.clip_distance_array_size
);
1509 if (prog
->info
.cull_distance_array_size
)
1510 ureg_property(ureg
, TGSI_PROPERTY_NUM_CULLDIST_ENABLED
,
1511 prog
->info
.cull_distance_array_size
);
1514 * Convert Mesa program inputs to TGSI input register semantics.
1516 for (attr
= 0; attr
< VARYING_SLOT_MAX
; attr
++) {
1517 if ((prog
->info
.inputs_read
& BITFIELD64_BIT(attr
)) == 0)
1520 unsigned slot
= num_inputs
++;
1522 inputMapping
[attr
] = slot
;
1523 inputSlotToAttr
[slot
] = attr
;
1525 unsigned semantic_name
, semantic_index
;
1526 tgsi_get_gl_varying_semantic(attr
, st
->needs_texcoord_semantic
,
1527 &semantic_name
, &semantic_index
);
1528 input_semantic_name
[slot
] = semantic_name
;
1529 input_semantic_index
[slot
] = semantic_index
;
1532 /* Also add patch inputs. */
1533 for (attr
= 0; attr
< 32; attr
++) {
1534 if (prog
->info
.patch_inputs_read
& (1u << attr
)) {
1535 GLuint slot
= num_inputs
++;
1536 GLuint patch_attr
= VARYING_SLOT_PATCH0
+ attr
;
1538 inputMapping
[patch_attr
] = slot
;
1539 inputSlotToAttr
[slot
] = patch_attr
;
1540 input_semantic_name
[slot
] = TGSI_SEMANTIC_PATCH
;
1541 input_semantic_index
[slot
] = attr
;
1545 /* initialize output semantics to defaults */
1546 for (i
= 0; i
< PIPE_MAX_SHADER_OUTPUTS
; i
++) {
1547 output_semantic_name
[i
] = TGSI_SEMANTIC_GENERIC
;
1548 output_semantic_index
[i
] = 0;
1552 * Determine number of outputs, the (default) output register
1553 * mapping and the semantic information for each output.
1555 for (attr
= 0; attr
< VARYING_SLOT_MAX
; attr
++) {
1556 if (prog
->info
.outputs_written
& BITFIELD64_BIT(attr
)) {
1557 GLuint slot
= num_outputs
++;
1559 outputMapping
[attr
] = slot
;
1561 unsigned semantic_name
, semantic_index
;
1562 tgsi_get_gl_varying_semantic(attr
, st
->needs_texcoord_semantic
,
1563 &semantic_name
, &semantic_index
);
1564 output_semantic_name
[slot
] = semantic_name
;
1565 output_semantic_index
[slot
] = semantic_index
;
1569 /* Also add patch outputs. */
1570 for (attr
= 0; attr
< 32; attr
++) {
1571 if (prog
->info
.patch_outputs_written
& (1u << attr
)) {
1572 GLuint slot
= num_outputs
++;
1573 GLuint patch_attr
= VARYING_SLOT_PATCH0
+ attr
;
1575 outputMapping
[patch_attr
] = slot
;
1576 output_semantic_name
[slot
] = TGSI_SEMANTIC_PATCH
;
1577 output_semantic_index
[slot
] = attr
;
1581 st_translate_program(st
->ctx
,
1590 input_semantic_name
,
1591 input_semantic_index
,
1596 output_semantic_name
,
1597 output_semantic_index
);
1599 stcp
->tgsi
.tokens
= ureg_get_tokens(ureg
, &stcp
->num_tgsi_tokens
);
1603 st_translate_stream_output_info(prog
);
1605 st_store_ir_in_disk_cache(st
, prog
, false);
1607 if ((ST_DEBUG
& DEBUG_TGSI
) && (ST_DEBUG
& DEBUG_MESA
)) {
1608 _mesa_print_program(prog
);
1612 if (ST_DEBUG
& DEBUG_TGSI
) {
1613 tgsi_dump(stcp
->tgsi
.tokens
, 0);
1617 free_glsl_to_tgsi_visitor(stcp
->glsl_to_tgsi
);
1618 stcp
->glsl_to_tgsi
= NULL
;
1624 * Get/create a basic program variant.
1626 struct st_basic_variant
*
1627 st_get_basic_variant(struct st_context
*st
,
1628 struct st_common_program
*prog
,
1629 const struct st_basic_variant_key
*key
)
1631 struct pipe_context
*pipe
= st
->pipe
;
1632 struct st_basic_variant
*v
;
1633 struct pipe_shader_state tgsi
= {0};
1635 /* Search for existing variant */
1636 for (v
= prog
->variants
; v
; v
= v
->next
) {
1637 if (memcmp(&v
->key
, key
, sizeof(*key
)) == 0) {
1644 v
= CALLOC_STRUCT(st_basic_variant
);
1647 if (prog
->tgsi
.type
== PIPE_SHADER_IR_NIR
) {
1648 tgsi
.type
= PIPE_SHADER_IR_NIR
;
1649 tgsi
.ir
.nir
= nir_shader_clone(NULL
, prog
->tgsi
.ir
.nir
);
1651 if (key
->clamp_color
)
1652 NIR_PASS_V(tgsi
.ir
.nir
, nir_lower_clamp_color_outputs
);
1654 tgsi
.stream_output
= prog
->tgsi
.stream_output
;
1656 st_finalize_nir(st
, &prog
->Base
, prog
->shader_program
,
1659 if (key
->lower_depth_clamp
) {
1660 struct gl_program_parameter_list
*params
= prog
->Base
.Parameters
;
1662 unsigned depth_range_const
=
1663 _mesa_add_state_reference(params
, depth_range_state
);
1665 const struct tgsi_token
*tokens
;
1667 st_tgsi_lower_depth_clamp(prog
->tgsi
.tokens
,
1669 key
->clip_negative_one_to_one
);
1671 if (tokens
!= prog
->tgsi
.tokens
)
1672 tgsi_free_tokens(prog
->tgsi
.tokens
);
1674 prog
->tgsi
.tokens
= tokens
;
1675 prog
->num_tgsi_tokens
= tgsi_num_tokens(tokens
);
1679 /* fill in new variant */
1680 switch (prog
->Base
.info
.stage
) {
1681 case MESA_SHADER_TESS_CTRL
:
1682 v
->driver_shader
= pipe
->create_tcs_state(pipe
, &tgsi
);
1684 case MESA_SHADER_TESS_EVAL
:
1685 v
->driver_shader
= pipe
->create_tes_state(pipe
, &tgsi
);
1687 case MESA_SHADER_GEOMETRY
:
1688 v
->driver_shader
= pipe
->create_gs_state(pipe
, &tgsi
);
1690 case MESA_SHADER_COMPUTE
: {
1691 struct pipe_compute_state cs
= {0};
1692 cs
.ir_type
= tgsi
.type
;
1693 cs
.req_local_mem
= prog
->Base
.info
.cs
.shared_size
;
1695 if (tgsi
.type
== PIPE_SHADER_IR_NIR
)
1696 cs
.prog
= tgsi
.ir
.nir
;
1698 cs
.prog
= tgsi
.tokens
;
1700 v
->driver_shader
= pipe
->create_compute_state(pipe
, &cs
);
1704 assert(!"unhandled shader type");
1711 /* insert into list */
1712 v
->next
= prog
->variants
;
1722 * Vert/Geom/Frag programs have per-context variants. Free all the
1723 * variants attached to the given program which match the given context.
1726 destroy_program_variants(struct st_context
*st
, struct gl_program
*target
)
1728 if (!target
|| target
== &_mesa_DummyProgram
)
1731 switch (target
->Target
) {
1732 case GL_VERTEX_PROGRAM_ARB
:
1734 struct st_vertex_program
*stvp
= (struct st_vertex_program
*) target
;
1735 struct st_vp_variant
*vpv
, **prevPtr
= &stvp
->variants
;
1737 for (vpv
= stvp
->variants
; vpv
; ) {
1738 struct st_vp_variant
*next
= vpv
->next
;
1739 if (vpv
->key
.st
== st
) {
1740 /* unlink from list */
1742 /* destroy this variant */
1743 delete_vp_variant(st
, vpv
);
1746 prevPtr
= &vpv
->next
;
1752 case GL_FRAGMENT_PROGRAM_ARB
:
1754 struct st_fragment_program
*stfp
=
1755 (struct st_fragment_program
*) target
;
1756 struct st_fp_variant
*fpv
, **prevPtr
= &stfp
->variants
;
1758 for (fpv
= stfp
->variants
; fpv
; ) {
1759 struct st_fp_variant
*next
= fpv
->next
;
1760 if (fpv
->key
.st
== st
) {
1761 /* unlink from list */
1763 /* destroy this variant */
1764 delete_fp_variant(st
, fpv
);
1767 prevPtr
= &fpv
->next
;
1773 case GL_GEOMETRY_PROGRAM_NV
:
1774 case GL_TESS_CONTROL_PROGRAM_NV
:
1775 case GL_TESS_EVALUATION_PROGRAM_NV
:
1776 case GL_COMPUTE_PROGRAM_NV
:
1778 struct st_common_program
*p
= st_common_program(target
);
1779 struct st_basic_variant
*v
, **prevPtr
= &p
->variants
;
1781 for (v
= p
->variants
; v
; ) {
1782 struct st_basic_variant
*next
= v
->next
;
1783 if (v
->key
.st
== st
) {
1784 /* unlink from list */
1786 /* destroy this variant */
1787 delete_basic_variant(st
, v
, target
->Target
);
1797 _mesa_problem(NULL
, "Unexpected program target 0x%x in "
1798 "destroy_program_variants_cb()", target
->Target
);
1804 * Callback for _mesa_HashWalk. Free all the shader's program variants
1805 * which match the given context.
1808 destroy_shader_program_variants_cb(GLuint key
, void *data
, void *userData
)
1810 struct st_context
*st
= (struct st_context
*) userData
;
1811 struct gl_shader
*shader
= (struct gl_shader
*) data
;
1813 switch (shader
->Type
) {
1814 case GL_SHADER_PROGRAM_MESA
:
1816 struct gl_shader_program
*shProg
= (struct gl_shader_program
*) data
;
1819 for (i
= 0; i
< ARRAY_SIZE(shProg
->_LinkedShaders
); i
++) {
1820 if (shProg
->_LinkedShaders
[i
])
1821 destroy_program_variants(st
, shProg
->_LinkedShaders
[i
]->Program
);
1825 case GL_VERTEX_SHADER
:
1826 case GL_FRAGMENT_SHADER
:
1827 case GL_GEOMETRY_SHADER
:
1828 case GL_TESS_CONTROL_SHADER
:
1829 case GL_TESS_EVALUATION_SHADER
:
1830 case GL_COMPUTE_SHADER
:
1839 * Callback for _mesa_HashWalk. Free all the program variants which match
1840 * the given context.
1843 destroy_program_variants_cb(GLuint key
, void *data
, void *userData
)
1845 struct st_context
*st
= (struct st_context
*) userData
;
1846 struct gl_program
*program
= (struct gl_program
*) data
;
1847 destroy_program_variants(st
, program
);
1852 * Walk over all shaders and programs to delete any variants which
1853 * belong to the given context.
1854 * This is called during context tear-down.
1857 st_destroy_program_variants(struct st_context
*st
)
1859 /* If shaders can be shared with other contexts, the last context will
1860 * call DeleteProgram on all shaders, releasing everything.
1862 if (st
->has_shareable_shaders
)
1865 /* ARB vert/frag program */
1866 _mesa_HashWalk(st
->ctx
->Shared
->Programs
,
1867 destroy_program_variants_cb
, st
);
1869 /* GLSL vert/frag/geom shaders */
1870 _mesa_HashWalk(st
->ctx
->Shared
->ShaderObjects
,
1871 destroy_shader_program_variants_cb
, st
);
1876 * For debugging, print/dump the current vertex program.
1879 st_print_current_vertex_program(void)
1881 GET_CURRENT_CONTEXT(ctx
);
1883 if (ctx
->VertexProgram
._Current
) {
1884 struct st_vertex_program
*stvp
=
1885 (struct st_vertex_program
*) ctx
->VertexProgram
._Current
;
1886 struct st_vp_variant
*stv
;
1888 debug_printf("Vertex program %u\n", stvp
->Base
.Id
);
1890 for (stv
= stvp
->variants
; stv
; stv
= stv
->next
) {
1891 debug_printf("variant %p\n", stv
);
1892 tgsi_dump(stv
->tgsi
.tokens
, 0);
1899 * Compile one shader variant.
1902 st_precompile_shader_variant(struct st_context
*st
,
1903 struct gl_program
*prog
)
1905 switch (prog
->Target
) {
1906 case GL_VERTEX_PROGRAM_ARB
: {
1907 struct st_vertex_program
*p
= (struct st_vertex_program
*)prog
;
1908 struct st_vp_variant_key key
;
1910 memset(&key
, 0, sizeof(key
));
1912 key
.st
= st
->has_shareable_shaders
? NULL
: st
;
1913 st_get_vp_variant(st
, p
, &key
);
1917 case GL_FRAGMENT_PROGRAM_ARB
: {
1918 struct st_fragment_program
*p
= (struct st_fragment_program
*)prog
;
1919 struct st_fp_variant_key key
;
1921 memset(&key
, 0, sizeof(key
));
1923 key
.st
= st
->has_shareable_shaders
? NULL
: st
;
1924 st_get_fp_variant(st
, p
, &key
);
1928 case GL_TESS_CONTROL_PROGRAM_NV
:
1929 case GL_TESS_EVALUATION_PROGRAM_NV
:
1930 case GL_GEOMETRY_PROGRAM_NV
:
1931 case GL_COMPUTE_PROGRAM_NV
: {
1932 struct st_common_program
*p
= st_common_program(prog
);
1933 struct st_basic_variant_key key
;
1935 memset(&key
, 0, sizeof(key
));
1937 key
.st
= st
->has_shareable_shaders
? NULL
: st
;
1938 st_get_basic_variant(st
, p
, &key
);