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
);
513 if (!stvp
->glsl_to_tgsi
) {
514 _mesa_remove_output_reads(&stvp
->Base
, PROGRAM_OUTPUT
);
516 /* This determines which states will be updated when the assembly
519 stvp
->affected_states
= ST_NEW_VS_STATE
|
521 ST_NEW_VERTEX_ARRAYS
;
523 if (stvp
->Base
.Parameters
->NumParameters
)
524 stvp
->affected_states
|= ST_NEW_VS_CONSTANTS
;
526 /* No samplers are allowed in ARB_vp. */
529 /* Get semantic names and indices. */
530 for (attr
= 0; attr
< VARYING_SLOT_MAX
; attr
++) {
531 if (stvp
->Base
.info
.outputs_written
& BITFIELD64_BIT(attr
)) {
532 unsigned slot
= num_outputs
++;
533 unsigned semantic_name
, semantic_index
;
534 tgsi_get_gl_varying_semantic(attr
, st
->needs_texcoord_semantic
,
535 &semantic_name
, &semantic_index
);
536 output_semantic_name
[slot
] = semantic_name
;
537 output_semantic_index
[slot
] = semantic_index
;
540 /* pre-setup potentially unused edgeflag output */
541 output_semantic_name
[num_outputs
] = TGSI_SEMANTIC_EDGEFLAG
;
542 output_semantic_index
[num_outputs
] = 0;
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);
617 * This must be done after the translation to TGSI is done, because
618 * we'll pass the NIR shader to the driver and the TGSI version to
619 * the draw module for the select/feedback/rasterpos code.
621 if (st
->pipe
->screen
->get_shader_param(st
->pipe
->screen
,
623 PIPE_SHADER_CAP_PREFERRED_IR
)) {
624 assert(!stvp
->glsl_to_tgsi
);
627 st_translate_prog_to_nir(st
, &stvp
->Base
, MESA_SHADER_VERTEX
);
629 if (stvp
->tgsi
.ir
.nir
)
630 ralloc_free(stvp
->tgsi
.ir
.nir
);
631 stvp
->tgsi
.type
= PIPE_SHADER_IR_NIR
;
632 stvp
->tgsi
.ir
.nir
= nir
;
633 stvp
->Base
.nir
= nir
;
637 return stvp
->tgsi
.tokens
!= NULL
;
640 static const gl_state_index16 depth_range_state
[STATE_LENGTH
] =
641 { STATE_DEPTH_RANGE
};
643 static struct st_vp_variant
*
644 st_create_vp_variant(struct st_context
*st
,
645 struct st_vertex_program
*stvp
,
646 const struct st_vp_variant_key
*key
)
648 struct st_vp_variant
*vpv
= CALLOC_STRUCT(st_vp_variant
);
649 struct pipe_context
*pipe
= st
->pipe
;
650 struct gl_program_parameter_list
*params
= stvp
->Base
.Parameters
;
653 vpv
->tgsi
.stream_output
= stvp
->tgsi
.stream_output
;
654 vpv
->num_inputs
= stvp
->num_inputs
;
656 /* When generating a NIR program, we usually don't have TGSI tokens.
657 * However, we do create them for ARB_vertex_program / fixed-function VS
658 * programs which we may need to use with the draw module for legacy
659 * feedback/select emulation. If they exist, copy them.
661 if (stvp
->tgsi
.tokens
)
662 vpv
->tgsi
.tokens
= tgsi_dup_tokens(stvp
->tgsi
.tokens
);
664 if (stvp
->tgsi
.type
== PIPE_SHADER_IR_NIR
) {
665 vpv
->tgsi
.type
= PIPE_SHADER_IR_NIR
;
666 vpv
->tgsi
.ir
.nir
= nir_shader_clone(NULL
, stvp
->tgsi
.ir
.nir
);
667 if (key
->clamp_color
)
668 NIR_PASS_V(vpv
->tgsi
.ir
.nir
, nir_lower_clamp_color_outputs
);
669 if (key
->passthrough_edgeflags
) {
670 NIR_PASS_V(vpv
->tgsi
.ir
.nir
, nir_lower_passthrough_edgeflags
);
674 st_finalize_nir(st
, &stvp
->Base
, stvp
->shader_program
,
677 vpv
->driver_shader
= pipe
->create_vs_state(pipe
, &vpv
->tgsi
);
678 /* driver takes ownership of IR: */
679 vpv
->tgsi
.ir
.nir
= NULL
;
683 /* Emulate features. */
684 if (key
->clamp_color
|| key
->passthrough_edgeflags
) {
685 const struct tgsi_token
*tokens
;
687 (key
->clamp_color
? TGSI_EMU_CLAMP_COLOR_OUTPUTS
: 0) |
688 (key
->passthrough_edgeflags
? TGSI_EMU_PASSTHROUGH_EDGEFLAG
: 0);
690 tokens
= tgsi_emulate(vpv
->tgsi
.tokens
, flags
);
693 tgsi_free_tokens(vpv
->tgsi
.tokens
);
694 vpv
->tgsi
.tokens
= tokens
;
696 if (key
->passthrough_edgeflags
)
699 fprintf(stderr
, "mesa: cannot emulate deprecated features\n");
702 if (key
->lower_depth_clamp
) {
703 unsigned depth_range_const
=
704 _mesa_add_state_reference(params
, depth_range_state
);
706 const struct tgsi_token
*tokens
;
707 tokens
= st_tgsi_lower_depth_clamp(vpv
->tgsi
.tokens
, depth_range_const
,
708 key
->clip_negative_one_to_one
);
709 if (tokens
!= vpv
->tgsi
.tokens
)
710 tgsi_free_tokens(vpv
->tgsi
.tokens
);
711 vpv
->tgsi
.tokens
= tokens
;
714 if (ST_DEBUG
& DEBUG_TGSI
) {
715 tgsi_dump(vpv
->tgsi
.tokens
, 0);
719 vpv
->driver_shader
= pipe
->create_vs_state(pipe
, &vpv
->tgsi
);
725 * Find/create a vertex program variant.
727 struct st_vp_variant
*
728 st_get_vp_variant(struct st_context
*st
,
729 struct st_vertex_program
*stvp
,
730 const struct st_vp_variant_key
*key
)
732 struct st_vp_variant
*vpv
;
734 /* Search for existing variant */
735 for (vpv
= stvp
->variants
; vpv
; vpv
= vpv
->next
) {
736 if (memcmp(&vpv
->key
, key
, sizeof(*key
)) == 0) {
743 vpv
= st_create_vp_variant(st
, stvp
, key
);
745 for (unsigned index
= 0; index
< vpv
->num_inputs
; ++index
) {
746 unsigned attr
= stvp
->index_to_input
[index
];
747 if (attr
== ST_DOUBLE_ATTRIB_PLACEHOLDER
)
749 vpv
->vert_attrib_mask
|= 1u << attr
;
752 /* insert into list */
753 vpv
->next
= stvp
->variants
;
754 stvp
->variants
= vpv
;
763 * Translate a Mesa fragment shader into a TGSI shader.
766 st_translate_fragment_program(struct st_context
*st
,
767 struct st_fragment_program
*stfp
)
769 /* Non-GLSL programs: */
770 if (!stfp
->glsl_to_tgsi
) {
771 _mesa_remove_output_reads(&stfp
->Base
, PROGRAM_OUTPUT
);
772 if (st
->ctx
->Const
.GLSLFragCoordIsSysVal
)
773 _mesa_program_fragment_position_to_sysval(&stfp
->Base
);
775 /* This determines which states will be updated when the assembly
778 * fragment.position and glDrawPixels always use constants.
780 stfp
->affected_states
= ST_NEW_FS_STATE
|
781 ST_NEW_SAMPLE_SHADING
|
785 /* Just set them for ATI_fs unconditionally. */
786 stfp
->affected_states
|= ST_NEW_FS_SAMPLER_VIEWS
|
790 if (stfp
->Base
.SamplersUsed
)
791 stfp
->affected_states
|= ST_NEW_FS_SAMPLER_VIEWS
|
795 /* Translate to NIR. */
797 st
->pipe
->screen
->get_shader_param(st
->pipe
->screen
,
798 PIPE_SHADER_FRAGMENT
,
799 PIPE_SHADER_CAP_PREFERRED_IR
)) {
801 st_translate_prog_to_nir(st
, &stfp
->Base
, MESA_SHADER_FRAGMENT
);
803 if (stfp
->tgsi
.ir
.nir
)
804 ralloc_free(stfp
->tgsi
.ir
.nir
);
805 stfp
->tgsi
.type
= PIPE_SHADER_IR_NIR
;
806 stfp
->tgsi
.ir
.nir
= nir
;
807 stfp
->Base
.nir
= nir
;
812 ubyte outputMapping
[2 * FRAG_RESULT_MAX
];
813 ubyte inputMapping
[VARYING_SLOT_MAX
];
814 ubyte inputSlotToAttr
[VARYING_SLOT_MAX
];
815 ubyte interpMode
[PIPE_MAX_SHADER_INPUTS
]; /* XXX size? */
817 GLbitfield64 inputsRead
;
818 struct ureg_program
*ureg
;
820 GLboolean write_all
= GL_FALSE
;
822 ubyte input_semantic_name
[PIPE_MAX_SHADER_INPUTS
];
823 ubyte input_semantic_index
[PIPE_MAX_SHADER_INPUTS
];
824 uint fs_num_inputs
= 0;
826 ubyte fs_output_semantic_name
[PIPE_MAX_SHADER_OUTPUTS
];
827 ubyte fs_output_semantic_index
[PIPE_MAX_SHADER_OUTPUTS
];
828 uint fs_num_outputs
= 0;
830 memset(inputSlotToAttr
, ~0, sizeof(inputSlotToAttr
));
833 * Convert Mesa program inputs to TGSI input register semantics.
835 inputsRead
= stfp
->Base
.info
.inputs_read
;
836 for (attr
= 0; attr
< VARYING_SLOT_MAX
; attr
++) {
837 if ((inputsRead
& BITFIELD64_BIT(attr
)) != 0) {
838 const GLuint slot
= fs_num_inputs
++;
840 inputMapping
[attr
] = slot
;
841 inputSlotToAttr
[slot
] = attr
;
844 case VARYING_SLOT_POS
:
845 input_semantic_name
[slot
] = TGSI_SEMANTIC_POSITION
;
846 input_semantic_index
[slot
] = 0;
847 interpMode
[slot
] = TGSI_INTERPOLATE_LINEAR
;
849 case VARYING_SLOT_COL0
:
850 input_semantic_name
[slot
] = TGSI_SEMANTIC_COLOR
;
851 input_semantic_index
[slot
] = 0;
852 interpMode
[slot
] = stfp
->glsl_to_tgsi
?
853 TGSI_INTERPOLATE_COUNT
: TGSI_INTERPOLATE_COLOR
;
855 case VARYING_SLOT_COL1
:
856 input_semantic_name
[slot
] = TGSI_SEMANTIC_COLOR
;
857 input_semantic_index
[slot
] = 1;
858 interpMode
[slot
] = stfp
->glsl_to_tgsi
?
859 TGSI_INTERPOLATE_COUNT
: TGSI_INTERPOLATE_COLOR
;
861 case VARYING_SLOT_FOGC
:
862 input_semantic_name
[slot
] = TGSI_SEMANTIC_FOG
;
863 input_semantic_index
[slot
] = 0;
864 interpMode
[slot
] = TGSI_INTERPOLATE_PERSPECTIVE
;
866 case VARYING_SLOT_FACE
:
867 input_semantic_name
[slot
] = TGSI_SEMANTIC_FACE
;
868 input_semantic_index
[slot
] = 0;
869 interpMode
[slot
] = TGSI_INTERPOLATE_CONSTANT
;
871 case VARYING_SLOT_PRIMITIVE_ID
:
872 input_semantic_name
[slot
] = TGSI_SEMANTIC_PRIMID
;
873 input_semantic_index
[slot
] = 0;
874 interpMode
[slot
] = TGSI_INTERPOLATE_CONSTANT
;
876 case VARYING_SLOT_LAYER
:
877 input_semantic_name
[slot
] = TGSI_SEMANTIC_LAYER
;
878 input_semantic_index
[slot
] = 0;
879 interpMode
[slot
] = TGSI_INTERPOLATE_CONSTANT
;
881 case VARYING_SLOT_VIEWPORT
:
882 input_semantic_name
[slot
] = TGSI_SEMANTIC_VIEWPORT_INDEX
;
883 input_semantic_index
[slot
] = 0;
884 interpMode
[slot
] = TGSI_INTERPOLATE_CONSTANT
;
886 case VARYING_SLOT_CLIP_DIST0
:
887 input_semantic_name
[slot
] = TGSI_SEMANTIC_CLIPDIST
;
888 input_semantic_index
[slot
] = 0;
889 interpMode
[slot
] = TGSI_INTERPOLATE_PERSPECTIVE
;
891 case VARYING_SLOT_CLIP_DIST1
:
892 input_semantic_name
[slot
] = TGSI_SEMANTIC_CLIPDIST
;
893 input_semantic_index
[slot
] = 1;
894 interpMode
[slot
] = TGSI_INTERPOLATE_PERSPECTIVE
;
896 case VARYING_SLOT_CULL_DIST0
:
897 case VARYING_SLOT_CULL_DIST1
:
898 /* these should have been lowered by GLSL */
901 /* In most cases, there is nothing special about these
902 * inputs, so adopt a convention to use the generic
903 * semantic name and the mesa VARYING_SLOT_ number as the
906 * All that is required is that the vertex shader labels
907 * its own outputs similarly, and that the vertex shader
908 * generates at least every output required by the
909 * fragment shader plus fixed-function hardware (such as
912 * However, some drivers may need us to identify the PNTC and TEXi
913 * varyings if, for example, their capability to replace them with
914 * sprite coordinates is limited.
916 case VARYING_SLOT_PNTC
:
917 if (st
->needs_texcoord_semantic
) {
918 input_semantic_name
[slot
] = TGSI_SEMANTIC_PCOORD
;
919 input_semantic_index
[slot
] = 0;
920 interpMode
[slot
] = TGSI_INTERPOLATE_LINEAR
;
924 case VARYING_SLOT_TEX0
:
925 case VARYING_SLOT_TEX1
:
926 case VARYING_SLOT_TEX2
:
927 case VARYING_SLOT_TEX3
:
928 case VARYING_SLOT_TEX4
:
929 case VARYING_SLOT_TEX5
:
930 case VARYING_SLOT_TEX6
:
931 case VARYING_SLOT_TEX7
:
932 if (st
->needs_texcoord_semantic
) {
933 input_semantic_name
[slot
] = TGSI_SEMANTIC_TEXCOORD
;
934 input_semantic_index
[slot
] = attr
- VARYING_SLOT_TEX0
;
935 interpMode
[slot
] = stfp
->glsl_to_tgsi
?
936 TGSI_INTERPOLATE_COUNT
: TGSI_INTERPOLATE_PERSPECTIVE
;
940 case VARYING_SLOT_VAR0
:
942 /* Semantic indices should be zero-based because drivers may choose
943 * to assign a fixed slot determined by that index.
944 * This is useful because ARB_separate_shader_objects uses location
945 * qualifiers for linkage, and if the semantic index corresponds to
946 * these locations, linkage passes in the driver become unecessary.
948 * If needs_texcoord_semantic is true, no semantic indices will be
949 * consumed for the TEXi varyings, and we can base the locations of
950 * the user varyings on VAR0. Otherwise, we use TEX0 as base index.
952 assert(attr
>= VARYING_SLOT_VAR0
|| attr
== VARYING_SLOT_PNTC
||
953 (attr
>= VARYING_SLOT_TEX0
&& attr
<= VARYING_SLOT_TEX7
));
954 input_semantic_name
[slot
] = TGSI_SEMANTIC_GENERIC
;
955 input_semantic_index
[slot
] = st_get_generic_varying_index(st
, attr
);
956 if (attr
== VARYING_SLOT_PNTC
)
957 interpMode
[slot
] = TGSI_INTERPOLATE_LINEAR
;
959 interpMode
[slot
] = stfp
->glsl_to_tgsi
?
960 TGSI_INTERPOLATE_COUNT
: TGSI_INTERPOLATE_PERSPECTIVE
;
966 inputMapping
[attr
] = -1;
971 * Semantics and mapping for outputs
973 GLbitfield64 outputsWritten
= stfp
->Base
.info
.outputs_written
;
975 /* if z is written, emit that first */
976 if (outputsWritten
& BITFIELD64_BIT(FRAG_RESULT_DEPTH
)) {
977 fs_output_semantic_name
[fs_num_outputs
] = TGSI_SEMANTIC_POSITION
;
978 fs_output_semantic_index
[fs_num_outputs
] = 0;
979 outputMapping
[FRAG_RESULT_DEPTH
] = fs_num_outputs
;
981 outputsWritten
&= ~(1 << FRAG_RESULT_DEPTH
);
984 if (outputsWritten
& BITFIELD64_BIT(FRAG_RESULT_STENCIL
)) {
985 fs_output_semantic_name
[fs_num_outputs
] = TGSI_SEMANTIC_STENCIL
;
986 fs_output_semantic_index
[fs_num_outputs
] = 0;
987 outputMapping
[FRAG_RESULT_STENCIL
] = fs_num_outputs
;
989 outputsWritten
&= ~(1 << FRAG_RESULT_STENCIL
);
992 if (outputsWritten
& BITFIELD64_BIT(FRAG_RESULT_SAMPLE_MASK
)) {
993 fs_output_semantic_name
[fs_num_outputs
] = TGSI_SEMANTIC_SAMPLEMASK
;
994 fs_output_semantic_index
[fs_num_outputs
] = 0;
995 outputMapping
[FRAG_RESULT_SAMPLE_MASK
] = fs_num_outputs
;
997 outputsWritten
&= ~(1 << FRAG_RESULT_SAMPLE_MASK
);
1000 /* handle remaining outputs (color) */
1001 for (attr
= 0; attr
< ARRAY_SIZE(outputMapping
); attr
++) {
1002 const GLbitfield64 written
= attr
< FRAG_RESULT_MAX
? outputsWritten
:
1003 stfp
->Base
.SecondaryOutputsWritten
;
1004 const unsigned loc
= attr
% FRAG_RESULT_MAX
;
1006 if (written
& BITFIELD64_BIT(loc
)) {
1008 case FRAG_RESULT_DEPTH
:
1009 case FRAG_RESULT_STENCIL
:
1010 case FRAG_RESULT_SAMPLE_MASK
:
1014 case FRAG_RESULT_COLOR
:
1015 write_all
= GL_TRUE
; /* fallthrough */
1018 assert(loc
== FRAG_RESULT_COLOR
||
1019 (FRAG_RESULT_DATA0
<= loc
&& loc
< FRAG_RESULT_MAX
));
1021 index
= (loc
== FRAG_RESULT_COLOR
) ? 0 : (loc
- FRAG_RESULT_DATA0
);
1023 if (attr
>= FRAG_RESULT_MAX
) {
1024 /* Secondary color for dual source blending. */
1029 fs_output_semantic_name
[fs_num_outputs
] = TGSI_SEMANTIC_COLOR
;
1030 fs_output_semantic_index
[fs_num_outputs
] = index
;
1031 outputMapping
[attr
] = fs_num_outputs
;
1040 ureg
= ureg_create_with_screen(PIPE_SHADER_FRAGMENT
, st
->pipe
->screen
);
1044 if (ST_DEBUG
& DEBUG_MESA
) {
1045 _mesa_print_program(&stfp
->Base
);
1046 _mesa_print_program_parameters(st
->ctx
, &stfp
->Base
);
1049 if (write_all
== GL_TRUE
)
1050 ureg_property(ureg
, TGSI_PROPERTY_FS_COLOR0_WRITES_ALL_CBUFS
, 1);
1052 if (stfp
->Base
.info
.fs
.depth_layout
!= FRAG_DEPTH_LAYOUT_NONE
) {
1053 switch (stfp
->Base
.info
.fs
.depth_layout
) {
1054 case FRAG_DEPTH_LAYOUT_ANY
:
1055 ureg_property(ureg
, TGSI_PROPERTY_FS_DEPTH_LAYOUT
,
1056 TGSI_FS_DEPTH_LAYOUT_ANY
);
1058 case FRAG_DEPTH_LAYOUT_GREATER
:
1059 ureg_property(ureg
, TGSI_PROPERTY_FS_DEPTH_LAYOUT
,
1060 TGSI_FS_DEPTH_LAYOUT_GREATER
);
1062 case FRAG_DEPTH_LAYOUT_LESS
:
1063 ureg_property(ureg
, TGSI_PROPERTY_FS_DEPTH_LAYOUT
,
1064 TGSI_FS_DEPTH_LAYOUT_LESS
);
1066 case FRAG_DEPTH_LAYOUT_UNCHANGED
:
1067 ureg_property(ureg
, TGSI_PROPERTY_FS_DEPTH_LAYOUT
,
1068 TGSI_FS_DEPTH_LAYOUT_UNCHANGED
);
1075 if (stfp
->glsl_to_tgsi
) {
1076 st_translate_program(st
->ctx
,
1077 PIPE_SHADER_FRAGMENT
,
1085 input_semantic_name
,
1086 input_semantic_index
,
1091 fs_output_semantic_name
,
1092 fs_output_semantic_index
);
1094 free_glsl_to_tgsi_visitor(stfp
->glsl_to_tgsi
);
1095 } else if (stfp
->ati_fs
)
1096 st_translate_atifs_program(ureg
,
1102 input_semantic_name
,
1103 input_semantic_index
,
1108 fs_output_semantic_name
,
1109 fs_output_semantic_index
);
1111 st_translate_mesa_program(st
->ctx
,
1112 PIPE_SHADER_FRAGMENT
,
1118 input_semantic_name
,
1119 input_semantic_index
,
1124 fs_output_semantic_name
,
1125 fs_output_semantic_index
);
1127 stfp
->tgsi
.tokens
= ureg_get_tokens(ureg
, &stfp
->num_tgsi_tokens
);
1130 if (stfp
->glsl_to_tgsi
) {
1131 stfp
->glsl_to_tgsi
= NULL
;
1132 st_store_ir_in_disk_cache(st
, &stfp
->Base
, false);
1135 return stfp
->tgsi
.tokens
!= NULL
;
1138 static struct st_fp_variant
*
1139 st_create_fp_variant(struct st_context
*st
,
1140 struct st_fragment_program
*stfp
,
1141 const struct st_fp_variant_key
*key
)
1143 struct pipe_context
*pipe
= st
->pipe
;
1144 struct st_fp_variant
*variant
= CALLOC_STRUCT(st_fp_variant
);
1145 struct pipe_shader_state tgsi
= {0};
1146 struct gl_program_parameter_list
*params
= stfp
->Base
.Parameters
;
1147 static const gl_state_index16 texcoord_state
[STATE_LENGTH
] =
1148 { STATE_INTERNAL
, STATE_CURRENT_ATTRIB
, VERT_ATTRIB_TEX0
};
1149 static const gl_state_index16 scale_state
[STATE_LENGTH
] =
1150 { STATE_INTERNAL
, STATE_PT_SCALE
};
1151 static const gl_state_index16 bias_state
[STATE_LENGTH
] =
1152 { STATE_INTERNAL
, STATE_PT_BIAS
};
1153 static const gl_state_index16 alpha_ref_state
[STATE_LENGTH
] =
1154 { STATE_INTERNAL
, STATE_ALPHA_REF
};
1159 if (stfp
->tgsi
.type
== PIPE_SHADER_IR_NIR
) {
1160 tgsi
.type
= PIPE_SHADER_IR_NIR
;
1161 tgsi
.ir
.nir
= nir_shader_clone(NULL
, stfp
->tgsi
.ir
.nir
);
1163 if (key
->clamp_color
)
1164 NIR_PASS_V(tgsi
.ir
.nir
, nir_lower_clamp_color_outputs
);
1166 if (key
->lower_flatshade
)
1167 NIR_PASS_V(tgsi
.ir
.nir
, nir_lower_flatshade
);
1169 if (key
->lower_alpha_func
!= COMPARE_FUNC_NEVER
) {
1170 _mesa_add_state_reference(params
, alpha_ref_state
);
1171 NIR_PASS_V(tgsi
.ir
.nir
, nir_lower_alpha_test
, key
->lower_alpha_func
,
1172 false, alpha_ref_state
);
1175 if (key
->persample_shading
) {
1176 nir_shader
*shader
= tgsi
.ir
.nir
;
1177 nir_foreach_variable(var
, &shader
->inputs
)
1178 var
->data
.sample
= true;
1181 assert(!(key
->bitmap
&& key
->drawpixels
));
1185 nir_lower_bitmap_options options
= {0};
1187 variant
->bitmap_sampler
= ffs(~stfp
->Base
.SamplersUsed
) - 1;
1188 options
.sampler
= variant
->bitmap_sampler
;
1189 options
.swizzle_xxxx
= st
->bitmap
.tex_format
== PIPE_FORMAT_R8_UNORM
;
1191 NIR_PASS_V(tgsi
.ir
.nir
, nir_lower_bitmap
, &options
);
1194 /* glDrawPixels (color only) */
1195 if (key
->drawpixels
) {
1196 nir_lower_drawpixels_options options
= {{0}};
1197 unsigned samplers_used
= stfp
->Base
.SamplersUsed
;
1199 /* Find the first unused slot. */
1200 variant
->drawpix_sampler
= ffs(~samplers_used
) - 1;
1201 options
.drawpix_sampler
= variant
->drawpix_sampler
;
1202 samplers_used
|= (1 << variant
->drawpix_sampler
);
1204 options
.pixel_maps
= key
->pixelMaps
;
1205 if (key
->pixelMaps
) {
1206 variant
->pixelmap_sampler
= ffs(~samplers_used
) - 1;
1207 options
.pixelmap_sampler
= variant
->pixelmap_sampler
;
1210 options
.scale_and_bias
= key
->scaleAndBias
;
1211 if (key
->scaleAndBias
) {
1212 _mesa_add_state_reference(params
, scale_state
);
1213 memcpy(options
.scale_state_tokens
, scale_state
,
1214 sizeof(options
.scale_state_tokens
));
1215 _mesa_add_state_reference(params
, bias_state
);
1216 memcpy(options
.bias_state_tokens
, bias_state
,
1217 sizeof(options
.bias_state_tokens
));
1220 _mesa_add_state_reference(params
, texcoord_state
);
1221 memcpy(options
.texcoord_state_tokens
, texcoord_state
,
1222 sizeof(options
.texcoord_state_tokens
));
1224 NIR_PASS_V(tgsi
.ir
.nir
, nir_lower_drawpixels
, &options
);
1227 if (unlikely(key
->external
.lower_nv12
|| key
->external
.lower_iyuv
||
1228 key
->external
.lower_xy_uxvx
|| key
->external
.lower_yx_xuxv
||
1229 key
->external
.lower_ayuv
|| key
->external
.lower_xyuv
)) {
1230 nir_lower_tex_options options
= {0};
1231 options
.lower_y_uv_external
= key
->external
.lower_nv12
;
1232 options
.lower_y_u_v_external
= key
->external
.lower_iyuv
;
1233 options
.lower_xy_uxvx_external
= key
->external
.lower_xy_uxvx
;
1234 options
.lower_yx_xuxv_external
= key
->external
.lower_yx_xuxv
;
1235 options
.lower_ayuv_external
= key
->external
.lower_ayuv
;
1236 options
.lower_xyuv_external
= key
->external
.lower_xyuv
;
1237 NIR_PASS_V(tgsi
.ir
.nir
, nir_lower_tex
, &options
);
1240 st_finalize_nir(st
, &stfp
->Base
, stfp
->shader_program
, tgsi
.ir
.nir
);
1242 if (unlikely(key
->external
.lower_nv12
|| key
->external
.lower_iyuv
||
1243 key
->external
.lower_xy_uxvx
|| key
->external
.lower_yx_xuxv
)) {
1244 /* This pass needs to happen *after* nir_lower_sampler */
1245 NIR_PASS_V(tgsi
.ir
.nir
, st_nir_lower_tex_src_plane
,
1246 ~stfp
->Base
.SamplersUsed
,
1247 key
->external
.lower_nv12
|| key
->external
.lower_xy_uxvx
||
1248 key
->external
.lower_yx_xuxv
,
1249 key
->external
.lower_iyuv
);
1252 /* Some of the lowering above may have introduced new varyings */
1253 nir_shader_gather_info(tgsi
.ir
.nir
,
1254 nir_shader_get_entrypoint(tgsi
.ir
.nir
));
1256 variant
->driver_shader
= pipe
->create_fs_state(pipe
, &tgsi
);
1257 variant
->key
= *key
;
1262 tgsi
.tokens
= stfp
->tgsi
.tokens
;
1264 assert(!(key
->bitmap
&& key
->drawpixels
));
1266 /* Fix texture targets and add fog for ATI_fs */
1268 const struct tgsi_token
*tokens
= st_fixup_atifs(tgsi
.tokens
, key
);
1271 tgsi
.tokens
= tokens
;
1273 fprintf(stderr
, "mesa: cannot post-process ATI_fs\n");
1276 /* Emulate features. */
1277 if (key
->clamp_color
|| key
->persample_shading
) {
1278 const struct tgsi_token
*tokens
;
1280 (key
->clamp_color
? TGSI_EMU_CLAMP_COLOR_OUTPUTS
: 0) |
1281 (key
->persample_shading
? TGSI_EMU_FORCE_PERSAMPLE_INTERP
: 0);
1283 tokens
= tgsi_emulate(tgsi
.tokens
, flags
);
1286 if (tgsi
.tokens
!= stfp
->tgsi
.tokens
)
1287 tgsi_free_tokens(tgsi
.tokens
);
1288 tgsi
.tokens
= tokens
;
1290 fprintf(stderr
, "mesa: cannot emulate deprecated features\n");
1295 const struct tgsi_token
*tokens
;
1297 variant
->bitmap_sampler
= ffs(~stfp
->Base
.SamplersUsed
) - 1;
1299 tokens
= st_get_bitmap_shader(tgsi
.tokens
,
1300 st
->internal_target
,
1301 variant
->bitmap_sampler
,
1302 st
->needs_texcoord_semantic
,
1303 st
->bitmap
.tex_format
==
1304 PIPE_FORMAT_R8_UNORM
);
1307 if (tgsi
.tokens
!= stfp
->tgsi
.tokens
)
1308 tgsi_free_tokens(tgsi
.tokens
);
1309 tgsi
.tokens
= tokens
;
1311 fprintf(stderr
, "mesa: cannot create a shader for glBitmap\n");
1314 /* glDrawPixels (color only) */
1315 if (key
->drawpixels
) {
1316 const struct tgsi_token
*tokens
;
1317 unsigned scale_const
= 0, bias_const
= 0, texcoord_const
= 0;
1319 /* Find the first unused slot. */
1320 variant
->drawpix_sampler
= ffs(~stfp
->Base
.SamplersUsed
) - 1;
1322 if (key
->pixelMaps
) {
1323 unsigned samplers_used
= stfp
->Base
.SamplersUsed
|
1324 (1 << variant
->drawpix_sampler
);
1326 variant
->pixelmap_sampler
= ffs(~samplers_used
) - 1;
1329 if (key
->scaleAndBias
) {
1330 scale_const
= _mesa_add_state_reference(params
, scale_state
);
1331 bias_const
= _mesa_add_state_reference(params
, bias_state
);
1334 texcoord_const
= _mesa_add_state_reference(params
, texcoord_state
);
1336 tokens
= st_get_drawpix_shader(tgsi
.tokens
,
1337 st
->needs_texcoord_semantic
,
1338 key
->scaleAndBias
, scale_const
,
1339 bias_const
, key
->pixelMaps
,
1340 variant
->drawpix_sampler
,
1341 variant
->pixelmap_sampler
,
1342 texcoord_const
, st
->internal_target
);
1345 if (tgsi
.tokens
!= stfp
->tgsi
.tokens
)
1346 tgsi_free_tokens(tgsi
.tokens
);
1347 tgsi
.tokens
= tokens
;
1349 fprintf(stderr
, "mesa: cannot create a shader for glDrawPixels\n");
1352 if (unlikely(key
->external
.lower_nv12
|| key
->external
.lower_iyuv
||
1353 key
->external
.lower_xy_uxvx
|| key
->external
.lower_yx_xuxv
)) {
1354 const struct tgsi_token
*tokens
;
1356 /* samplers inserted would conflict, but this should be unpossible: */
1357 assert(!(key
->bitmap
|| key
->drawpixels
));
1359 tokens
= st_tgsi_lower_yuv(tgsi
.tokens
,
1360 ~stfp
->Base
.SamplersUsed
,
1361 key
->external
.lower_nv12
||
1362 key
->external
.lower_xy_uxvx
||
1363 key
->external
.lower_yx_xuxv
,
1364 key
->external
.lower_iyuv
);
1366 if (tgsi
.tokens
!= stfp
->tgsi
.tokens
)
1367 tgsi_free_tokens(tgsi
.tokens
);
1368 tgsi
.tokens
= tokens
;
1370 fprintf(stderr
, "mesa: cannot create a shader for samplerExternalOES\n");
1374 if (key
->lower_depth_clamp
) {
1375 unsigned depth_range_const
= _mesa_add_state_reference(params
, depth_range_state
);
1377 const struct tgsi_token
*tokens
;
1378 tokens
= st_tgsi_lower_depth_clamp_fs(tgsi
.tokens
, depth_range_const
);
1379 if (tgsi
.tokens
!= stfp
->tgsi
.tokens
)
1380 tgsi_free_tokens(tgsi
.tokens
);
1381 tgsi
.tokens
= tokens
;
1384 if (ST_DEBUG
& DEBUG_TGSI
) {
1385 tgsi_dump(tgsi
.tokens
, 0);
1389 /* fill in variant */
1390 variant
->driver_shader
= pipe
->create_fs_state(pipe
, &tgsi
);
1391 variant
->key
= *key
;
1393 if (tgsi
.tokens
!= stfp
->tgsi
.tokens
)
1394 tgsi_free_tokens(tgsi
.tokens
);
1399 * Translate fragment program if needed.
1401 struct st_fp_variant
*
1402 st_get_fp_variant(struct st_context
*st
,
1403 struct st_fragment_program
*stfp
,
1404 const struct st_fp_variant_key
*key
)
1406 struct st_fp_variant
*fpv
;
1408 /* Search for existing variant */
1409 for (fpv
= stfp
->variants
; fpv
; fpv
= fpv
->next
) {
1410 if (memcmp(&fpv
->key
, key
, sizeof(*key
)) == 0) {
1417 fpv
= st_create_fp_variant(st
, stfp
, key
);
1419 if (key
->bitmap
|| key
->drawpixels
) {
1420 /* Regular variants should always come before the
1421 * bitmap & drawpixels variants, (unless there
1422 * are no regular variants) so that
1423 * st_update_fp can take a fast path when
1424 * shader_has_one_variant is set.
1426 if (!stfp
->variants
) {
1427 stfp
->variants
= fpv
;
1429 /* insert into list after the first one */
1430 fpv
->next
= stfp
->variants
->next
;
1431 stfp
->variants
->next
= fpv
;
1434 /* insert into list */
1435 fpv
->next
= stfp
->variants
;
1436 stfp
->variants
= fpv
;
1445 * Translate a program. This is common code for geometry and tessellation
1449 st_translate_common_program(struct st_context
*st
,
1450 struct st_common_program
*stcp
)
1452 struct gl_program
*prog
= &stcp
->Base
;
1453 enum pipe_shader_type stage
=
1454 pipe_shader_type_from_mesa(stcp
->Base
.info
.stage
);
1455 struct ureg_program
*ureg
= ureg_create_with_screen(stage
, st
->pipe
->screen
);
1461 case PIPE_SHADER_TESS_CTRL
:
1462 ureg_property(ureg
, TGSI_PROPERTY_TCS_VERTICES_OUT
,
1463 stcp
->Base
.info
.tess
.tcs_vertices_out
);
1466 case PIPE_SHADER_TESS_EVAL
:
1467 if (stcp
->Base
.info
.tess
.primitive_mode
== GL_ISOLINES
)
1468 ureg_property(ureg
, TGSI_PROPERTY_TES_PRIM_MODE
, GL_LINES
);
1470 ureg_property(ureg
, TGSI_PROPERTY_TES_PRIM_MODE
,
1471 stcp
->Base
.info
.tess
.primitive_mode
);
1473 STATIC_ASSERT((TESS_SPACING_EQUAL
+ 1) % 3 == PIPE_TESS_SPACING_EQUAL
);
1474 STATIC_ASSERT((TESS_SPACING_FRACTIONAL_ODD
+ 1) % 3 ==
1475 PIPE_TESS_SPACING_FRACTIONAL_ODD
);
1476 STATIC_ASSERT((TESS_SPACING_FRACTIONAL_EVEN
+ 1) % 3 ==
1477 PIPE_TESS_SPACING_FRACTIONAL_EVEN
);
1479 ureg_property(ureg
, TGSI_PROPERTY_TES_SPACING
,
1480 (stcp
->Base
.info
.tess
.spacing
+ 1) % 3);
1482 ureg_property(ureg
, TGSI_PROPERTY_TES_VERTEX_ORDER_CW
,
1483 !stcp
->Base
.info
.tess
.ccw
);
1484 ureg_property(ureg
, TGSI_PROPERTY_TES_POINT_MODE
,
1485 stcp
->Base
.info
.tess
.point_mode
);
1488 case PIPE_SHADER_GEOMETRY
:
1489 ureg_property(ureg
, TGSI_PROPERTY_GS_INPUT_PRIM
,
1490 stcp
->Base
.info
.gs
.input_primitive
);
1491 ureg_property(ureg
, TGSI_PROPERTY_GS_OUTPUT_PRIM
,
1492 stcp
->Base
.info
.gs
.output_primitive
);
1493 ureg_property(ureg
, TGSI_PROPERTY_GS_MAX_OUTPUT_VERTICES
,
1494 stcp
->Base
.info
.gs
.vertices_out
);
1495 ureg_property(ureg
, TGSI_PROPERTY_GS_INVOCATIONS
,
1496 stcp
->Base
.info
.gs
.invocations
);
1503 ubyte inputSlotToAttr
[VARYING_SLOT_TESS_MAX
];
1504 ubyte inputMapping
[VARYING_SLOT_TESS_MAX
];
1505 ubyte outputMapping
[VARYING_SLOT_TESS_MAX
];
1508 ubyte input_semantic_name
[PIPE_MAX_SHADER_INPUTS
];
1509 ubyte input_semantic_index
[PIPE_MAX_SHADER_INPUTS
];
1510 uint num_inputs
= 0;
1512 ubyte output_semantic_name
[PIPE_MAX_SHADER_OUTPUTS
];
1513 ubyte output_semantic_index
[PIPE_MAX_SHADER_OUTPUTS
];
1514 uint num_outputs
= 0;
1518 memset(inputSlotToAttr
, 0, sizeof(inputSlotToAttr
));
1519 memset(inputMapping
, 0, sizeof(inputMapping
));
1520 memset(outputMapping
, 0, sizeof(outputMapping
));
1521 memset(&stcp
->tgsi
, 0, sizeof(stcp
->tgsi
));
1523 if (prog
->info
.clip_distance_array_size
)
1524 ureg_property(ureg
, TGSI_PROPERTY_NUM_CLIPDIST_ENABLED
,
1525 prog
->info
.clip_distance_array_size
);
1526 if (prog
->info
.cull_distance_array_size
)
1527 ureg_property(ureg
, TGSI_PROPERTY_NUM_CULLDIST_ENABLED
,
1528 prog
->info
.cull_distance_array_size
);
1531 * Convert Mesa program inputs to TGSI input register semantics.
1533 for (attr
= 0; attr
< VARYING_SLOT_MAX
; attr
++) {
1534 if ((prog
->info
.inputs_read
& BITFIELD64_BIT(attr
)) == 0)
1537 unsigned slot
= num_inputs
++;
1539 inputMapping
[attr
] = slot
;
1540 inputSlotToAttr
[slot
] = attr
;
1542 unsigned semantic_name
, semantic_index
;
1543 tgsi_get_gl_varying_semantic(attr
, st
->needs_texcoord_semantic
,
1544 &semantic_name
, &semantic_index
);
1545 input_semantic_name
[slot
] = semantic_name
;
1546 input_semantic_index
[slot
] = semantic_index
;
1549 /* Also add patch inputs. */
1550 for (attr
= 0; attr
< 32; attr
++) {
1551 if (prog
->info
.patch_inputs_read
& (1u << attr
)) {
1552 GLuint slot
= num_inputs
++;
1553 GLuint patch_attr
= VARYING_SLOT_PATCH0
+ attr
;
1555 inputMapping
[patch_attr
] = slot
;
1556 inputSlotToAttr
[slot
] = patch_attr
;
1557 input_semantic_name
[slot
] = TGSI_SEMANTIC_PATCH
;
1558 input_semantic_index
[slot
] = attr
;
1562 /* initialize output semantics to defaults */
1563 for (i
= 0; i
< PIPE_MAX_SHADER_OUTPUTS
; i
++) {
1564 output_semantic_name
[i
] = TGSI_SEMANTIC_GENERIC
;
1565 output_semantic_index
[i
] = 0;
1569 * Determine number of outputs, the (default) output register
1570 * mapping and the semantic information for each output.
1572 for (attr
= 0; attr
< VARYING_SLOT_MAX
; attr
++) {
1573 if (prog
->info
.outputs_written
& BITFIELD64_BIT(attr
)) {
1574 GLuint slot
= num_outputs
++;
1576 outputMapping
[attr
] = slot
;
1578 unsigned semantic_name
, semantic_index
;
1579 tgsi_get_gl_varying_semantic(attr
, st
->needs_texcoord_semantic
,
1580 &semantic_name
, &semantic_index
);
1581 output_semantic_name
[slot
] = semantic_name
;
1582 output_semantic_index
[slot
] = semantic_index
;
1586 /* Also add patch outputs. */
1587 for (attr
= 0; attr
< 32; attr
++) {
1588 if (prog
->info
.patch_outputs_written
& (1u << attr
)) {
1589 GLuint slot
= num_outputs
++;
1590 GLuint patch_attr
= VARYING_SLOT_PATCH0
+ attr
;
1592 outputMapping
[patch_attr
] = slot
;
1593 output_semantic_name
[slot
] = TGSI_SEMANTIC_PATCH
;
1594 output_semantic_index
[slot
] = attr
;
1598 st_translate_program(st
->ctx
,
1607 input_semantic_name
,
1608 input_semantic_index
,
1613 output_semantic_name
,
1614 output_semantic_index
);
1616 stcp
->tgsi
.tokens
= ureg_get_tokens(ureg
, &stcp
->num_tgsi_tokens
);
1620 st_translate_stream_output_info(prog
);
1622 st_store_ir_in_disk_cache(st
, prog
, false);
1624 if ((ST_DEBUG
& DEBUG_TGSI
) && (ST_DEBUG
& DEBUG_MESA
)) {
1625 _mesa_print_program(prog
);
1629 if (ST_DEBUG
& DEBUG_TGSI
) {
1630 tgsi_dump(stcp
->tgsi
.tokens
, 0);
1634 free_glsl_to_tgsi_visitor(stcp
->glsl_to_tgsi
);
1635 stcp
->glsl_to_tgsi
= NULL
;
1641 * Get/create a basic program variant.
1643 struct st_basic_variant
*
1644 st_get_basic_variant(struct st_context
*st
,
1645 struct st_common_program
*prog
,
1646 const struct st_basic_variant_key
*key
)
1648 struct pipe_context
*pipe
= st
->pipe
;
1649 struct st_basic_variant
*v
;
1650 struct pipe_shader_state tgsi
= {0};
1652 /* Search for existing variant */
1653 for (v
= prog
->variants
; v
; v
= v
->next
) {
1654 if (memcmp(&v
->key
, key
, sizeof(*key
)) == 0) {
1661 v
= CALLOC_STRUCT(st_basic_variant
);
1664 if (prog
->tgsi
.type
== PIPE_SHADER_IR_NIR
) {
1665 tgsi
.type
= PIPE_SHADER_IR_NIR
;
1666 tgsi
.ir
.nir
= nir_shader_clone(NULL
, prog
->tgsi
.ir
.nir
);
1668 if (key
->clamp_color
)
1669 NIR_PASS_V(tgsi
.ir
.nir
, nir_lower_clamp_color_outputs
);
1671 tgsi
.stream_output
= prog
->tgsi
.stream_output
;
1673 st_finalize_nir(st
, &prog
->Base
, prog
->shader_program
,
1676 if (key
->lower_depth_clamp
) {
1677 struct gl_program_parameter_list
*params
= prog
->Base
.Parameters
;
1679 unsigned depth_range_const
=
1680 _mesa_add_state_reference(params
, depth_range_state
);
1682 const struct tgsi_token
*tokens
;
1684 st_tgsi_lower_depth_clamp(prog
->tgsi
.tokens
,
1686 key
->clip_negative_one_to_one
);
1688 if (tokens
!= prog
->tgsi
.tokens
)
1689 tgsi_free_tokens(prog
->tgsi
.tokens
);
1691 prog
->tgsi
.tokens
= tokens
;
1692 prog
->num_tgsi_tokens
= tgsi_num_tokens(tokens
);
1696 /* fill in new variant */
1697 switch (prog
->Base
.info
.stage
) {
1698 case MESA_SHADER_TESS_CTRL
:
1699 v
->driver_shader
= pipe
->create_tcs_state(pipe
, &tgsi
);
1701 case MESA_SHADER_TESS_EVAL
:
1702 v
->driver_shader
= pipe
->create_tes_state(pipe
, &tgsi
);
1704 case MESA_SHADER_GEOMETRY
:
1705 v
->driver_shader
= pipe
->create_gs_state(pipe
, &tgsi
);
1707 case MESA_SHADER_COMPUTE
: {
1708 struct pipe_compute_state cs
= {0};
1709 cs
.ir_type
= tgsi
.type
;
1710 cs
.req_local_mem
= prog
->Base
.info
.cs
.shared_size
;
1712 if (tgsi
.type
== PIPE_SHADER_IR_NIR
)
1713 cs
.prog
= tgsi
.ir
.nir
;
1715 cs
.prog
= tgsi
.tokens
;
1717 v
->driver_shader
= pipe
->create_compute_state(pipe
, &cs
);
1721 assert(!"unhandled shader type");
1728 /* insert into list */
1729 v
->next
= prog
->variants
;
1739 * Vert/Geom/Frag programs have per-context variants. Free all the
1740 * variants attached to the given program which match the given context.
1743 destroy_program_variants(struct st_context
*st
, struct gl_program
*target
)
1745 if (!target
|| target
== &_mesa_DummyProgram
)
1748 switch (target
->Target
) {
1749 case GL_VERTEX_PROGRAM_ARB
:
1751 struct st_vertex_program
*stvp
= (struct st_vertex_program
*) target
;
1752 struct st_vp_variant
*vpv
, **prevPtr
= &stvp
->variants
;
1754 for (vpv
= stvp
->variants
; vpv
; ) {
1755 struct st_vp_variant
*next
= vpv
->next
;
1756 if (vpv
->key
.st
== st
) {
1757 /* unlink from list */
1759 /* destroy this variant */
1760 delete_vp_variant(st
, vpv
);
1763 prevPtr
= &vpv
->next
;
1769 case GL_FRAGMENT_PROGRAM_ARB
:
1771 struct st_fragment_program
*stfp
=
1772 (struct st_fragment_program
*) target
;
1773 struct st_fp_variant
*fpv
, **prevPtr
= &stfp
->variants
;
1775 for (fpv
= stfp
->variants
; fpv
; ) {
1776 struct st_fp_variant
*next
= fpv
->next
;
1777 if (fpv
->key
.st
== st
) {
1778 /* unlink from list */
1780 /* destroy this variant */
1781 delete_fp_variant(st
, fpv
);
1784 prevPtr
= &fpv
->next
;
1790 case GL_GEOMETRY_PROGRAM_NV
:
1791 case GL_TESS_CONTROL_PROGRAM_NV
:
1792 case GL_TESS_EVALUATION_PROGRAM_NV
:
1793 case GL_COMPUTE_PROGRAM_NV
:
1795 struct st_common_program
*p
= st_common_program(target
);
1796 struct st_basic_variant
*v
, **prevPtr
= &p
->variants
;
1798 for (v
= p
->variants
; v
; ) {
1799 struct st_basic_variant
*next
= v
->next
;
1800 if (v
->key
.st
== st
) {
1801 /* unlink from list */
1803 /* destroy this variant */
1804 delete_basic_variant(st
, v
, target
->Target
);
1814 _mesa_problem(NULL
, "Unexpected program target 0x%x in "
1815 "destroy_program_variants_cb()", target
->Target
);
1821 * Callback for _mesa_HashWalk. Free all the shader's program variants
1822 * which match the given context.
1825 destroy_shader_program_variants_cb(GLuint key
, void *data
, void *userData
)
1827 struct st_context
*st
= (struct st_context
*) userData
;
1828 struct gl_shader
*shader
= (struct gl_shader
*) data
;
1830 switch (shader
->Type
) {
1831 case GL_SHADER_PROGRAM_MESA
:
1833 struct gl_shader_program
*shProg
= (struct gl_shader_program
*) data
;
1836 for (i
= 0; i
< ARRAY_SIZE(shProg
->_LinkedShaders
); i
++) {
1837 if (shProg
->_LinkedShaders
[i
])
1838 destroy_program_variants(st
, shProg
->_LinkedShaders
[i
]->Program
);
1842 case GL_VERTEX_SHADER
:
1843 case GL_FRAGMENT_SHADER
:
1844 case GL_GEOMETRY_SHADER
:
1845 case GL_TESS_CONTROL_SHADER
:
1846 case GL_TESS_EVALUATION_SHADER
:
1847 case GL_COMPUTE_SHADER
:
1856 * Callback for _mesa_HashWalk. Free all the program variants which match
1857 * the given context.
1860 destroy_program_variants_cb(GLuint key
, void *data
, void *userData
)
1862 struct st_context
*st
= (struct st_context
*) userData
;
1863 struct gl_program
*program
= (struct gl_program
*) data
;
1864 destroy_program_variants(st
, program
);
1869 * Walk over all shaders and programs to delete any variants which
1870 * belong to the given context.
1871 * This is called during context tear-down.
1874 st_destroy_program_variants(struct st_context
*st
)
1876 /* If shaders can be shared with other contexts, the last context will
1877 * call DeleteProgram on all shaders, releasing everything.
1879 if (st
->has_shareable_shaders
)
1882 /* ARB vert/frag program */
1883 _mesa_HashWalk(st
->ctx
->Shared
->Programs
,
1884 destroy_program_variants_cb
, st
);
1886 /* GLSL vert/frag/geom shaders */
1887 _mesa_HashWalk(st
->ctx
->Shared
->ShaderObjects
,
1888 destroy_shader_program_variants_cb
, st
);
1893 * For debugging, print/dump the current vertex program.
1896 st_print_current_vertex_program(void)
1898 GET_CURRENT_CONTEXT(ctx
);
1900 if (ctx
->VertexProgram
._Current
) {
1901 struct st_vertex_program
*stvp
=
1902 (struct st_vertex_program
*) ctx
->VertexProgram
._Current
;
1903 struct st_vp_variant
*stv
;
1905 debug_printf("Vertex program %u\n", stvp
->Base
.Id
);
1907 for (stv
= stvp
->variants
; stv
; stv
= stv
->next
) {
1908 debug_printf("variant %p\n", stv
);
1909 tgsi_dump(stv
->tgsi
.tokens
, 0);
1916 * Compile one shader variant.
1919 st_precompile_shader_variant(struct st_context
*st
,
1920 struct gl_program
*prog
)
1922 switch (prog
->Target
) {
1923 case GL_VERTEX_PROGRAM_ARB
: {
1924 struct st_vertex_program
*p
= (struct st_vertex_program
*)prog
;
1925 struct st_vp_variant_key key
;
1927 memset(&key
, 0, sizeof(key
));
1929 key
.st
= st
->has_shareable_shaders
? NULL
: st
;
1930 st_get_vp_variant(st
, p
, &key
);
1934 case GL_FRAGMENT_PROGRAM_ARB
: {
1935 struct st_fragment_program
*p
= (struct st_fragment_program
*)prog
;
1936 struct st_fp_variant_key key
;
1938 memset(&key
, 0, sizeof(key
));
1940 key
.st
= st
->has_shareable_shaders
? NULL
: st
;
1941 st_get_fp_variant(st
, p
, &key
);
1945 case GL_TESS_CONTROL_PROGRAM_NV
:
1946 case GL_TESS_EVALUATION_PROGRAM_NV
:
1947 case GL_GEOMETRY_PROGRAM_NV
:
1948 case GL_COMPUTE_PROGRAM_NV
: {
1949 struct st_common_program
*p
= st_common_program(prog
);
1950 struct st_basic_variant_key key
;
1952 memset(&key
, 0, sizeof(key
));
1954 key
.st
= st
->has_shareable_shaders
? NULL
: st
;
1955 st_get_basic_variant(st
, p
, &key
);