1 /**************************************************************************
3 * Copyright 2007 VMware, Inc.
6 * Permission is hereby granted, free of charge, to any person obtaining a
7 * copy of this software and associated documentation files (the
8 * "Software"), to deal in the Software without restriction, including
9 * without limitation the rights to use, copy, modify, merge, publish,
10 * distribute, sub license, and/or sell copies of the Software, and to
11 * permit persons to whom the Software is furnished to do so, subject to
12 * the following conditions:
14 * The above copyright notice and this permission notice (including the
15 * next paragraph) shall be included in all copies or substantial portions
18 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
19 * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
20 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT.
21 * IN NO EVENT SHALL VMWARE AND/OR ITS SUPPLIERS BE LIABLE FOR
22 * ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
23 * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
24 * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
26 **************************************************************************/
29 * Keith Whitwell <keithw@vmware.com>
34 #include "main/errors.h"
35 #include "main/imports.h"
36 #include "main/hash.h"
37 #include "main/mtypes.h"
38 #include "program/prog_parameter.h"
39 #include "program/prog_print.h"
40 #include "program/prog_to_nir.h"
41 #include "program/programopt.h"
43 #include "compiler/nir/nir.h"
45 #include "pipe/p_context.h"
46 #include "pipe/p_defines.h"
47 #include "pipe/p_shader_tokens.h"
48 #include "draw/draw_context.h"
49 #include "tgsi/tgsi_dump.h"
50 #include "tgsi/tgsi_emulate.h"
51 #include "tgsi/tgsi_parse.h"
52 #include "tgsi/tgsi_ureg.h"
55 #include "st_cb_bitmap.h"
56 #include "st_cb_drawpixels.h"
57 #include "st_context.h"
58 #include "st_tgsi_lower_depth_clamp.h"
59 #include "st_tgsi_lower_yuv.h"
60 #include "st_program.h"
61 #include "st_mesa_to_tgsi.h"
62 #include "st_atifs_to_tgsi.h"
64 #include "st_shader_cache.h"
65 #include "cso_cache/cso_context.h"
70 set_affected_state_flags(uint64_t *states
,
71 struct gl_program
*prog
,
72 uint64_t new_constants
,
73 uint64_t new_sampler_views
,
74 uint64_t new_samplers
,
80 if (prog
->Parameters
->NumParameters
)
81 *states
|= new_constants
;
83 if (prog
->info
.num_textures
)
84 *states
|= new_sampler_views
| new_samplers
;
86 if (prog
->info
.num_images
)
87 *states
|= new_images
;
89 if (prog
->info
.num_ubos
)
92 if (prog
->info
.num_ssbos
)
95 if (prog
->info
.num_abos
)
96 *states
|= new_atomics
;
100 * This determines which states will be updated when the shader is bound.
103 st_set_prog_affected_state_flags(struct gl_program
*prog
)
107 switch (prog
->info
.stage
) {
108 case MESA_SHADER_VERTEX
:
109 states
= &((struct st_vertex_program
*)prog
)->affected_states
;
111 *states
= ST_NEW_VS_STATE
|
113 ST_NEW_VERTEX_ARRAYS
;
115 set_affected_state_flags(states
, prog
,
117 ST_NEW_VS_SAMPLER_VIEWS
,
125 case MESA_SHADER_TESS_CTRL
:
126 states
= &(st_common_program(prog
))->affected_states
;
128 *states
= ST_NEW_TCS_STATE
;
130 set_affected_state_flags(states
, prog
,
131 ST_NEW_TCS_CONSTANTS
,
132 ST_NEW_TCS_SAMPLER_VIEWS
,
140 case MESA_SHADER_TESS_EVAL
:
141 states
= &(st_common_program(prog
))->affected_states
;
143 *states
= ST_NEW_TES_STATE
|
146 set_affected_state_flags(states
, prog
,
147 ST_NEW_TES_CONSTANTS
,
148 ST_NEW_TES_SAMPLER_VIEWS
,
156 case MESA_SHADER_GEOMETRY
:
157 states
= &(st_common_program(prog
))->affected_states
;
159 *states
= ST_NEW_GS_STATE
|
162 set_affected_state_flags(states
, prog
,
164 ST_NEW_GS_SAMPLER_VIEWS
,
172 case MESA_SHADER_FRAGMENT
:
173 states
= &((struct st_common_program
*)prog
)->affected_states
;
175 /* gl_FragCoord and glDrawPixels always use constants. */
176 *states
= ST_NEW_FS_STATE
|
177 ST_NEW_SAMPLE_SHADING
|
180 set_affected_state_flags(states
, prog
,
182 ST_NEW_FS_SAMPLER_VIEWS
,
190 case MESA_SHADER_COMPUTE
:
191 states
= &((struct st_common_program
*)prog
)->affected_states
;
193 *states
= ST_NEW_CS_STATE
;
195 set_affected_state_flags(states
, prog
,
197 ST_NEW_CS_SAMPLER_VIEWS
,
206 unreachable("unhandled shader stage");
211 delete_ir(struct pipe_shader_state
*ir
)
214 ureg_free_tokens(ir
->tokens
);
218 /* Note: Any setup of ->ir.nir that has had pipe->create_*_state called on
219 * it has resulted in the driver taking ownership of the NIR. Those
220 * callers should be NULLing out the nir field in any pipe_shader_state
221 * that might have this called in order to indicate that.
223 * GLSL IR and ARB programs will have set gl_program->nir to the same
224 * shader as ir->ir.nir, so it will be freed by _mesa_delete_program().
229 * Delete a vertex program variant. Note the caller must unlink
230 * the variant from the linked list.
233 delete_vp_variant(struct st_context
*st
, struct st_vp_variant
*vpv
)
235 if (vpv
->driver_shader
) {
236 if (st
->has_shareable_shaders
|| vpv
->key
.st
== st
) {
237 cso_delete_vertex_shader(st
->cso_context
, vpv
->driver_shader
);
239 st_save_zombie_shader(vpv
->key
.st
, PIPE_SHADER_VERTEX
,
244 if (vpv
->draw_shader
)
245 draw_delete_vertex_shader( st
->draw
, vpv
->draw_shader
);
248 ureg_free_tokens(vpv
->tokens
);
256 * Clean out any old compilations:
259 st_release_vp_variants( struct st_context
*st
,
260 struct st_vertex_program
*stvp
)
262 struct st_vp_variant
*vpv
;
264 for (vpv
= stvp
->variants
; vpv
; ) {
265 struct st_vp_variant
*next
= vpv
->next
;
266 delete_vp_variant(st
, vpv
);
270 stvp
->variants
= NULL
;
272 delete_ir(&stvp
->state
);
278 * Delete a fragment program variant. Note the caller must unlink
279 * the variant from the linked list.
282 delete_fp_variant(struct st_context
*st
, struct st_fp_variant
*fpv
)
284 if (fpv
->driver_shader
) {
285 if (st
->has_shareable_shaders
|| fpv
->key
.st
== st
) {
286 cso_delete_fragment_shader(st
->cso_context
, fpv
->driver_shader
);
288 st_save_zombie_shader(fpv
->key
.st
, PIPE_SHADER_FRAGMENT
,
298 * Free all variants of a fragment program.
301 st_release_fp_variants(struct st_context
*st
, struct st_common_program
*stfp
)
303 struct st_fp_variant
*fpv
;
305 for (fpv
= stfp
->fp_variants
; fpv
; ) {
306 struct st_fp_variant
*next
= fpv
->next
;
307 delete_fp_variant(st
, fpv
);
311 stfp
->fp_variants
= NULL
;
313 delete_ir(&stfp
->state
);
318 * Delete a basic program variant. Note the caller must unlink
319 * the variant from the linked list.
322 delete_basic_variant(struct st_context
*st
, struct st_common_variant
*v
,
325 if (v
->driver_shader
) {
326 if (st
->has_shareable_shaders
|| v
->key
.st
== st
) {
327 /* The shader's context matches the calling context, or we
331 case GL_TESS_CONTROL_PROGRAM_NV
:
332 cso_delete_tessctrl_shader(st
->cso_context
, v
->driver_shader
);
334 case GL_TESS_EVALUATION_PROGRAM_NV
:
335 cso_delete_tesseval_shader(st
->cso_context
, v
->driver_shader
);
337 case GL_GEOMETRY_PROGRAM_NV
:
338 cso_delete_geometry_shader(st
->cso_context
, v
->driver_shader
);
340 case GL_COMPUTE_PROGRAM_NV
:
341 cso_delete_compute_shader(st
->cso_context
, v
->driver_shader
);
344 unreachable("bad shader type in delete_basic_variant");
347 /* We can't delete a shader with a context different from the one
348 * that created it. Add it to the creating context's zombie list.
350 enum pipe_shader_type type
;
352 case GL_TESS_CONTROL_PROGRAM_NV
:
353 type
= PIPE_SHADER_TESS_CTRL
;
355 case GL_TESS_EVALUATION_PROGRAM_NV
:
356 type
= PIPE_SHADER_TESS_EVAL
;
358 case GL_GEOMETRY_PROGRAM_NV
:
359 type
= PIPE_SHADER_GEOMETRY
;
364 st_save_zombie_shader(v
->key
.st
, type
, v
->driver_shader
);
373 * Free all basic program variants.
376 st_release_common_variants(struct st_context
*st
, struct st_common_program
*p
)
378 struct st_common_variant
*v
;
380 for (v
= p
->variants
; v
; ) {
381 struct st_common_variant
*next
= v
->next
;
382 delete_basic_variant(st
, v
, p
->Base
.Target
);
387 delete_ir(&p
->state
);
392 * Translate ARB (asm) program to NIR
395 st_translate_prog_to_nir(struct st_context
*st
, struct gl_program
*prog
,
396 gl_shader_stage stage
)
398 const struct gl_shader_compiler_options
*options
=
399 &st
->ctx
->Const
.ShaderCompilerOptions
[stage
];
401 /* Translate to NIR */
402 nir_shader
*nir
= prog_to_nir(prog
, options
->NirOptions
);
403 NIR_PASS_V(nir
, nir_lower_regs_to_ssa
); /* turn registers into SSA */
404 nir_validate_shader(nir
, "after st/ptn lower_regs_to_ssa");
406 NIR_PASS_V(nir
, st_nir_lower_wpos_ytransform
, prog
, st
->pipe
->screen
);
407 NIR_PASS_V(nir
, nir_lower_system_values
);
410 NIR_PASS_V(nir
, nir_opt_constant_folding
);
412 nir_validate_shader(nir
, "after st/ptn NIR opts");
418 st_prepare_vertex_program(struct st_vertex_program
*stvp
)
420 stvp
->num_inputs
= 0;
421 memset(stvp
->input_to_index
, ~0, sizeof(stvp
->input_to_index
));
422 memset(stvp
->result_to_output
, ~0, sizeof(stvp
->result_to_output
));
424 /* Determine number of inputs, the mappings between VERT_ATTRIB_x
425 * and TGSI generic input indexes, plus input attrib semantic info.
427 for (unsigned attr
= 0; attr
< VERT_ATTRIB_MAX
; attr
++) {
428 if ((stvp
->Base
.info
.inputs_read
& BITFIELD64_BIT(attr
)) != 0) {
429 stvp
->input_to_index
[attr
] = stvp
->num_inputs
;
430 stvp
->index_to_input
[stvp
->num_inputs
] = attr
;
433 if ((stvp
->Base
.DualSlotInputs
& BITFIELD64_BIT(attr
)) != 0) {
434 /* add placeholder for second part of a double attribute */
435 stvp
->index_to_input
[stvp
->num_inputs
] = ST_DOUBLE_ATTRIB_PLACEHOLDER
;
440 /* pre-setup potentially unused edgeflag input */
441 stvp
->input_to_index
[VERT_ATTRIB_EDGEFLAG
] = stvp
->num_inputs
;
442 stvp
->index_to_input
[stvp
->num_inputs
] = VERT_ATTRIB_EDGEFLAG
;
444 /* Compute mapping of vertex program outputs to slots. */
445 unsigned num_outputs
= 0;
446 for (unsigned attr
= 0; attr
< VARYING_SLOT_MAX
; attr
++) {
447 if (stvp
->Base
.info
.outputs_written
& BITFIELD64_BIT(attr
))
448 stvp
->result_to_output
[attr
] = num_outputs
++;
450 /* pre-setup potentially unused edgeflag output */
451 stvp
->result_to_output
[VARYING_SLOT_EDGE
] = num_outputs
;
455 st_translate_stream_output_info(struct gl_program
*prog
)
457 struct gl_transform_feedback_info
*info
= prog
->sh
.LinkedTransformFeedback
;
461 /* Determine the (default) output register mapping for each output. */
462 unsigned num_outputs
= 0;
463 ubyte output_mapping
[VARYING_SLOT_TESS_MAX
];
464 memset(output_mapping
, 0, sizeof(output_mapping
));
466 for (unsigned attr
= 0; attr
< VARYING_SLOT_MAX
; attr
++) {
467 if (prog
->info
.outputs_written
& BITFIELD64_BIT(attr
))
468 output_mapping
[attr
] = num_outputs
++;
471 /* Translate stream output info. */
472 struct pipe_stream_output_info
*so_info
= NULL
;
473 if (prog
->info
.stage
== MESA_SHADER_VERTEX
)
474 so_info
= &((struct st_vertex_program
*)prog
)->state
.stream_output
;
476 so_info
= &((struct st_common_program
*)prog
)->state
.stream_output
;
478 for (unsigned i
= 0; i
< info
->NumOutputs
; i
++) {
479 so_info
->output
[i
].register_index
=
480 output_mapping
[info
->Outputs
[i
].OutputRegister
];
481 so_info
->output
[i
].start_component
= info
->Outputs
[i
].ComponentOffset
;
482 so_info
->output
[i
].num_components
= info
->Outputs
[i
].NumComponents
;
483 so_info
->output
[i
].output_buffer
= info
->Outputs
[i
].OutputBuffer
;
484 so_info
->output
[i
].dst_offset
= info
->Outputs
[i
].DstOffset
;
485 so_info
->output
[i
].stream
= info
->Outputs
[i
].StreamId
;
488 for (unsigned i
= 0; i
< PIPE_MAX_SO_BUFFERS
; i
++) {
489 so_info
->stride
[i
] = info
->Buffers
[i
].Stride
;
491 so_info
->num_outputs
= info
->NumOutputs
;
495 * Translate a vertex program.
498 st_translate_vertex_program(struct st_context
*st
,
499 struct st_vertex_program
*stvp
)
501 struct ureg_program
*ureg
;
502 enum pipe_error error
;
503 unsigned num_outputs
= 0;
505 ubyte output_semantic_name
[VARYING_SLOT_MAX
] = {0};
506 ubyte output_semantic_index
[VARYING_SLOT_MAX
] = {0};
508 if (stvp
->Base
.arb
.IsPositionInvariant
)
509 _mesa_insert_mvp_code(st
->ctx
, &stvp
->Base
);
511 st_prepare_vertex_program(stvp
);
514 if (!stvp
->glsl_to_tgsi
) {
515 _mesa_remove_output_reads(&stvp
->Base
, PROGRAM_OUTPUT
);
517 /* This determines which states will be updated when the assembly
520 stvp
->affected_states
= ST_NEW_VS_STATE
|
522 ST_NEW_VERTEX_ARRAYS
;
524 if (stvp
->Base
.Parameters
->NumParameters
)
525 stvp
->affected_states
|= ST_NEW_VS_CONSTANTS
;
527 /* No samplers are allowed in ARB_vp. */
530 /* Get semantic names and indices. */
531 for (attr
= 0; attr
< VARYING_SLOT_MAX
; attr
++) {
532 if (stvp
->Base
.info
.outputs_written
& BITFIELD64_BIT(attr
)) {
533 unsigned slot
= num_outputs
++;
534 unsigned semantic_name
, semantic_index
;
535 tgsi_get_gl_varying_semantic(attr
, st
->needs_texcoord_semantic
,
536 &semantic_name
, &semantic_index
);
537 output_semantic_name
[slot
] = semantic_name
;
538 output_semantic_index
[slot
] = semantic_index
;
541 /* pre-setup potentially unused edgeflag output */
542 output_semantic_name
[num_outputs
] = TGSI_SEMANTIC_EDGEFLAG
;
543 output_semantic_index
[num_outputs
] = 0;
545 ureg
= ureg_create_with_screen(PIPE_SHADER_VERTEX
, st
->pipe
->screen
);
549 if (stvp
->Base
.info
.clip_distance_array_size
)
550 ureg_property(ureg
, TGSI_PROPERTY_NUM_CLIPDIST_ENABLED
,
551 stvp
->Base
.info
.clip_distance_array_size
);
552 if (stvp
->Base
.info
.cull_distance_array_size
)
553 ureg_property(ureg
, TGSI_PROPERTY_NUM_CULLDIST_ENABLED
,
554 stvp
->Base
.info
.cull_distance_array_size
);
556 if (ST_DEBUG
& DEBUG_MESA
) {
557 _mesa_print_program(&stvp
->Base
);
558 _mesa_print_program_parameters(st
->ctx
, &stvp
->Base
);
562 if (stvp
->glsl_to_tgsi
) {
563 error
= st_translate_program(st
->ctx
,
570 stvp
->input_to_index
,
571 NULL
, /* inputSlotToAttr */
572 NULL
, /* input semantic name */
573 NULL
, /* input semantic index */
574 NULL
, /* interp mode */
577 stvp
->result_to_output
,
578 output_semantic_name
,
579 output_semantic_index
);
581 st_translate_stream_output_info(&stvp
->Base
);
583 free_glsl_to_tgsi_visitor(stvp
->glsl_to_tgsi
);
585 error
= st_translate_mesa_program(st
->ctx
,
591 stvp
->input_to_index
,
592 NULL
, /* input semantic name */
593 NULL
, /* input semantic index */
597 stvp
->result_to_output
,
598 output_semantic_name
,
599 output_semantic_index
);
602 debug_printf("%s: failed to translate Mesa program:\n", __func__
);
603 _mesa_print_program(&stvp
->Base
);
608 stvp
->state
.tokens
= ureg_get_tokens(ureg
, NULL
);
611 if (stvp
->glsl_to_tgsi
) {
612 stvp
->glsl_to_tgsi
= NULL
;
613 st_store_ir_in_disk_cache(st
, &stvp
->Base
, false);
618 * This must be done after the translation to TGSI is done, because
619 * we'll pass the NIR shader to the driver and the TGSI version to
620 * the draw module for the select/feedback/rasterpos code.
622 if (st
->pipe
->screen
->get_shader_param(st
->pipe
->screen
,
624 PIPE_SHADER_CAP_PREFERRED_IR
)) {
625 assert(!stvp
->glsl_to_tgsi
);
628 st_translate_prog_to_nir(st
, &stvp
->Base
, MESA_SHADER_VERTEX
);
630 if (stvp
->state
.ir
.nir
)
631 ralloc_free(stvp
->state
.ir
.nir
);
632 stvp
->state
.type
= PIPE_SHADER_IR_NIR
;
633 stvp
->state
.ir
.nir
= nir
;
634 stvp
->Base
.nir
= nir
;
638 return stvp
->state
.tokens
!= NULL
;
641 static const gl_state_index16 depth_range_state
[STATE_LENGTH
] =
642 { STATE_DEPTH_RANGE
};
644 static struct st_vp_variant
*
645 st_create_vp_variant(struct st_context
*st
,
646 struct st_vertex_program
*stvp
,
647 const struct st_common_variant_key
*key
)
649 struct st_vp_variant
*vpv
= CALLOC_STRUCT(st_vp_variant
);
650 struct pipe_context
*pipe
= st
->pipe
;
651 struct pipe_shader_state state
= {0};
653 static const gl_state_index16 point_size_state
[STATE_LENGTH
] =
654 { STATE_INTERNAL
, STATE_POINT_SIZE_CLAMPED
, 0 };
655 struct gl_program_parameter_list
*params
= stvp
->Base
.Parameters
;
658 vpv
->num_inputs
= stvp
->num_inputs
;
660 state
.stream_output
= stvp
->state
.stream_output
;
662 if (stvp
->state
.type
== PIPE_SHADER_IR_NIR
) {
663 state
.type
= PIPE_SHADER_IR_NIR
;
664 state
.ir
.nir
= nir_shader_clone(NULL
, stvp
->state
.ir
.nir
);
665 if (key
->clamp_color
)
666 NIR_PASS_V(state
.ir
.nir
, nir_lower_clamp_color_outputs
);
667 if (key
->passthrough_edgeflags
) {
668 NIR_PASS_V(state
.ir
.nir
, nir_lower_passthrough_edgeflags
);
672 if (key
->lower_point_size
) {
673 _mesa_add_state_reference(params
, point_size_state
);
674 NIR_PASS_V(state
.ir
.nir
, nir_lower_point_size_mov
,
678 if (key
->lower_ucp
) {
679 struct pipe_screen
*screen
= pipe
->screen
;
680 bool can_compact
= screen
->get_param(screen
,
681 PIPE_CAP_NIR_COMPACT_ARRAYS
);
683 bool use_eye
= st
->ctx
->_Shader
->CurrentProgram
[MESA_SHADER_VERTEX
] != NULL
;
684 gl_state_index16 clipplane_state
[MAX_CLIP_PLANES
][STATE_LENGTH
];
685 for (int i
= 0; i
< MAX_CLIP_PLANES
; ++i
) {
687 clipplane_state
[i
][0] = STATE_CLIPPLANE
;
688 clipplane_state
[i
][1] = i
;
690 clipplane_state
[i
][0] = STATE_INTERNAL
;
691 clipplane_state
[i
][1] = STATE_CLIP_INTERNAL
;
692 clipplane_state
[i
][2] = i
;
694 _mesa_add_state_reference(params
, clipplane_state
[i
]);
697 NIR_PASS_V(state
.ir
.nir
, nir_lower_clip_vs
, key
->lower_ucp
,
698 true, can_compact
, clipplane_state
);
699 NIR_PASS_V(state
.ir
.nir
, nir_lower_io_to_temporaries
,
700 nir_shader_get_entrypoint(state
.ir
.nir
), true, false);
703 st_finalize_nir(st
, &stvp
->Base
, stvp
->shader_program
,
706 vpv
->driver_shader
= pipe
->create_vs_state(pipe
, &state
);
708 /* When generating a NIR program, we usually don't have TGSI tokens.
709 * However, we do create them for ARB_vertex_program / fixed-function VS
710 * programs which we may need to use with the draw module for legacy
711 * feedback/select emulation. If they exist, copy them.
713 * TODO: Lowering for shader variants is not applied to TGSI when
714 * generating a NIR shader.
716 if (stvp
->state
.tokens
)
717 vpv
->tokens
= tgsi_dup_tokens(stvp
->state
.tokens
);
722 state
.type
= PIPE_SHADER_IR_TGSI
;
723 state
.tokens
= tgsi_dup_tokens(stvp
->state
.tokens
);
725 /* Emulate features. */
726 if (key
->clamp_color
|| key
->passthrough_edgeflags
) {
727 const struct tgsi_token
*tokens
;
729 (key
->clamp_color
? TGSI_EMU_CLAMP_COLOR_OUTPUTS
: 0) |
730 (key
->passthrough_edgeflags
? TGSI_EMU_PASSTHROUGH_EDGEFLAG
: 0);
732 tokens
= tgsi_emulate(state
.tokens
, flags
);
735 tgsi_free_tokens(state
.tokens
);
736 state
.tokens
= tokens
;
738 if (key
->passthrough_edgeflags
)
741 fprintf(stderr
, "mesa: cannot emulate deprecated features\n");
744 if (key
->lower_depth_clamp
) {
745 unsigned depth_range_const
=
746 _mesa_add_state_reference(params
, depth_range_state
);
748 const struct tgsi_token
*tokens
;
749 tokens
= st_tgsi_lower_depth_clamp(state
.tokens
, depth_range_const
,
750 key
->clip_negative_one_to_one
);
751 if (tokens
!= state
.tokens
)
752 tgsi_free_tokens(state
.tokens
);
753 state
.tokens
= tokens
;
756 if (ST_DEBUG
& DEBUG_TGSI
) {
757 tgsi_dump(state
.tokens
, 0);
761 vpv
->driver_shader
= pipe
->create_vs_state(pipe
, &state
);
762 /* Save this for selection/feedback/rasterpos. */
763 vpv
->tokens
= state
.tokens
;
769 * Find/create a vertex program variant.
771 struct st_vp_variant
*
772 st_get_vp_variant(struct st_context
*st
,
773 struct st_vertex_program
*stvp
,
774 const struct st_common_variant_key
*key
)
776 struct st_vp_variant
*vpv
;
778 /* Search for existing variant */
779 for (vpv
= stvp
->variants
; vpv
; vpv
= vpv
->next
) {
780 if (memcmp(&vpv
->key
, key
, sizeof(*key
)) == 0) {
787 vpv
= st_create_vp_variant(st
, stvp
, key
);
789 for (unsigned index
= 0; index
< vpv
->num_inputs
; ++index
) {
790 unsigned attr
= stvp
->index_to_input
[index
];
791 if (attr
== ST_DOUBLE_ATTRIB_PLACEHOLDER
)
793 vpv
->vert_attrib_mask
|= 1u << attr
;
796 /* insert into list */
797 vpv
->next
= stvp
->variants
;
798 stvp
->variants
= vpv
;
807 * Translate a Mesa fragment shader into a TGSI shader.
810 st_translate_fragment_program(struct st_context
*st
,
811 struct st_common_program
*stfp
)
813 /* Non-GLSL programs: */
814 if (!stfp
->glsl_to_tgsi
) {
815 _mesa_remove_output_reads(&stfp
->Base
, PROGRAM_OUTPUT
);
816 if (st
->ctx
->Const
.GLSLFragCoordIsSysVal
)
817 _mesa_program_fragment_position_to_sysval(&stfp
->Base
);
819 /* This determines which states will be updated when the assembly
822 * fragment.position and glDrawPixels always use constants.
824 stfp
->affected_states
= ST_NEW_FS_STATE
|
825 ST_NEW_SAMPLE_SHADING
|
829 /* Just set them for ATI_fs unconditionally. */
830 stfp
->affected_states
|= ST_NEW_FS_SAMPLER_VIEWS
|
834 if (stfp
->Base
.SamplersUsed
)
835 stfp
->affected_states
|= ST_NEW_FS_SAMPLER_VIEWS
|
839 /* Translate to NIR. */
841 st
->pipe
->screen
->get_shader_param(st
->pipe
->screen
,
842 PIPE_SHADER_FRAGMENT
,
843 PIPE_SHADER_CAP_PREFERRED_IR
)) {
845 st_translate_prog_to_nir(st
, &stfp
->Base
, MESA_SHADER_FRAGMENT
);
847 if (stfp
->state
.ir
.nir
)
848 ralloc_free(stfp
->state
.ir
.nir
);
849 stfp
->state
.type
= PIPE_SHADER_IR_NIR
;
850 stfp
->state
.ir
.nir
= nir
;
851 stfp
->Base
.nir
= nir
;
856 ubyte outputMapping
[2 * FRAG_RESULT_MAX
];
857 ubyte inputMapping
[VARYING_SLOT_MAX
];
858 ubyte inputSlotToAttr
[VARYING_SLOT_MAX
];
859 ubyte interpMode
[PIPE_MAX_SHADER_INPUTS
]; /* XXX size? */
861 GLbitfield64 inputsRead
;
862 struct ureg_program
*ureg
;
864 GLboolean write_all
= GL_FALSE
;
866 ubyte input_semantic_name
[PIPE_MAX_SHADER_INPUTS
];
867 ubyte input_semantic_index
[PIPE_MAX_SHADER_INPUTS
];
868 uint fs_num_inputs
= 0;
870 ubyte fs_output_semantic_name
[PIPE_MAX_SHADER_OUTPUTS
];
871 ubyte fs_output_semantic_index
[PIPE_MAX_SHADER_OUTPUTS
];
872 uint fs_num_outputs
= 0;
874 memset(inputSlotToAttr
, ~0, sizeof(inputSlotToAttr
));
877 * Convert Mesa program inputs to TGSI input register semantics.
879 inputsRead
= stfp
->Base
.info
.inputs_read
;
880 for (attr
= 0; attr
< VARYING_SLOT_MAX
; attr
++) {
881 if ((inputsRead
& BITFIELD64_BIT(attr
)) != 0) {
882 const GLuint slot
= fs_num_inputs
++;
884 inputMapping
[attr
] = slot
;
885 inputSlotToAttr
[slot
] = attr
;
888 case VARYING_SLOT_POS
:
889 input_semantic_name
[slot
] = TGSI_SEMANTIC_POSITION
;
890 input_semantic_index
[slot
] = 0;
891 interpMode
[slot
] = TGSI_INTERPOLATE_LINEAR
;
893 case VARYING_SLOT_COL0
:
894 input_semantic_name
[slot
] = TGSI_SEMANTIC_COLOR
;
895 input_semantic_index
[slot
] = 0;
896 interpMode
[slot
] = stfp
->glsl_to_tgsi
?
897 TGSI_INTERPOLATE_COUNT
: TGSI_INTERPOLATE_COLOR
;
899 case VARYING_SLOT_COL1
:
900 input_semantic_name
[slot
] = TGSI_SEMANTIC_COLOR
;
901 input_semantic_index
[slot
] = 1;
902 interpMode
[slot
] = stfp
->glsl_to_tgsi
?
903 TGSI_INTERPOLATE_COUNT
: TGSI_INTERPOLATE_COLOR
;
905 case VARYING_SLOT_FOGC
:
906 input_semantic_name
[slot
] = TGSI_SEMANTIC_FOG
;
907 input_semantic_index
[slot
] = 0;
908 interpMode
[slot
] = TGSI_INTERPOLATE_PERSPECTIVE
;
910 case VARYING_SLOT_FACE
:
911 input_semantic_name
[slot
] = TGSI_SEMANTIC_FACE
;
912 input_semantic_index
[slot
] = 0;
913 interpMode
[slot
] = TGSI_INTERPOLATE_CONSTANT
;
915 case VARYING_SLOT_PRIMITIVE_ID
:
916 input_semantic_name
[slot
] = TGSI_SEMANTIC_PRIMID
;
917 input_semantic_index
[slot
] = 0;
918 interpMode
[slot
] = TGSI_INTERPOLATE_CONSTANT
;
920 case VARYING_SLOT_LAYER
:
921 input_semantic_name
[slot
] = TGSI_SEMANTIC_LAYER
;
922 input_semantic_index
[slot
] = 0;
923 interpMode
[slot
] = TGSI_INTERPOLATE_CONSTANT
;
925 case VARYING_SLOT_VIEWPORT
:
926 input_semantic_name
[slot
] = TGSI_SEMANTIC_VIEWPORT_INDEX
;
927 input_semantic_index
[slot
] = 0;
928 interpMode
[slot
] = TGSI_INTERPOLATE_CONSTANT
;
930 case VARYING_SLOT_CLIP_DIST0
:
931 input_semantic_name
[slot
] = TGSI_SEMANTIC_CLIPDIST
;
932 input_semantic_index
[slot
] = 0;
933 interpMode
[slot
] = TGSI_INTERPOLATE_PERSPECTIVE
;
935 case VARYING_SLOT_CLIP_DIST1
:
936 input_semantic_name
[slot
] = TGSI_SEMANTIC_CLIPDIST
;
937 input_semantic_index
[slot
] = 1;
938 interpMode
[slot
] = TGSI_INTERPOLATE_PERSPECTIVE
;
940 case VARYING_SLOT_CULL_DIST0
:
941 case VARYING_SLOT_CULL_DIST1
:
942 /* these should have been lowered by GLSL */
945 /* In most cases, there is nothing special about these
946 * inputs, so adopt a convention to use the generic
947 * semantic name and the mesa VARYING_SLOT_ number as the
950 * All that is required is that the vertex shader labels
951 * its own outputs similarly, and that the vertex shader
952 * generates at least every output required by the
953 * fragment shader plus fixed-function hardware (such as
956 * However, some drivers may need us to identify the PNTC and TEXi
957 * varyings if, for example, their capability to replace them with
958 * sprite coordinates is limited.
960 case VARYING_SLOT_PNTC
:
961 if (st
->needs_texcoord_semantic
) {
962 input_semantic_name
[slot
] = TGSI_SEMANTIC_PCOORD
;
963 input_semantic_index
[slot
] = 0;
964 interpMode
[slot
] = TGSI_INTERPOLATE_LINEAR
;
968 case VARYING_SLOT_TEX0
:
969 case VARYING_SLOT_TEX1
:
970 case VARYING_SLOT_TEX2
:
971 case VARYING_SLOT_TEX3
:
972 case VARYING_SLOT_TEX4
:
973 case VARYING_SLOT_TEX5
:
974 case VARYING_SLOT_TEX6
:
975 case VARYING_SLOT_TEX7
:
976 if (st
->needs_texcoord_semantic
) {
977 input_semantic_name
[slot
] = TGSI_SEMANTIC_TEXCOORD
;
978 input_semantic_index
[slot
] = attr
- VARYING_SLOT_TEX0
;
979 interpMode
[slot
] = stfp
->glsl_to_tgsi
?
980 TGSI_INTERPOLATE_COUNT
: TGSI_INTERPOLATE_PERSPECTIVE
;
984 case VARYING_SLOT_VAR0
:
986 /* Semantic indices should be zero-based because drivers may choose
987 * to assign a fixed slot determined by that index.
988 * This is useful because ARB_separate_shader_objects uses location
989 * qualifiers for linkage, and if the semantic index corresponds to
990 * these locations, linkage passes in the driver become unecessary.
992 * If needs_texcoord_semantic is true, no semantic indices will be
993 * consumed for the TEXi varyings, and we can base the locations of
994 * the user varyings on VAR0. Otherwise, we use TEX0 as base index.
996 assert(attr
>= VARYING_SLOT_VAR0
|| attr
== VARYING_SLOT_PNTC
||
997 (attr
>= VARYING_SLOT_TEX0
&& attr
<= VARYING_SLOT_TEX7
));
998 input_semantic_name
[slot
] = TGSI_SEMANTIC_GENERIC
;
999 input_semantic_index
[slot
] = st_get_generic_varying_index(st
, attr
);
1000 if (attr
== VARYING_SLOT_PNTC
)
1001 interpMode
[slot
] = TGSI_INTERPOLATE_LINEAR
;
1003 interpMode
[slot
] = stfp
->glsl_to_tgsi
?
1004 TGSI_INTERPOLATE_COUNT
: TGSI_INTERPOLATE_PERSPECTIVE
;
1010 inputMapping
[attr
] = -1;
1015 * Semantics and mapping for outputs
1017 GLbitfield64 outputsWritten
= stfp
->Base
.info
.outputs_written
;
1019 /* if z is written, emit that first */
1020 if (outputsWritten
& BITFIELD64_BIT(FRAG_RESULT_DEPTH
)) {
1021 fs_output_semantic_name
[fs_num_outputs
] = TGSI_SEMANTIC_POSITION
;
1022 fs_output_semantic_index
[fs_num_outputs
] = 0;
1023 outputMapping
[FRAG_RESULT_DEPTH
] = fs_num_outputs
;
1025 outputsWritten
&= ~(1 << FRAG_RESULT_DEPTH
);
1028 if (outputsWritten
& BITFIELD64_BIT(FRAG_RESULT_STENCIL
)) {
1029 fs_output_semantic_name
[fs_num_outputs
] = TGSI_SEMANTIC_STENCIL
;
1030 fs_output_semantic_index
[fs_num_outputs
] = 0;
1031 outputMapping
[FRAG_RESULT_STENCIL
] = fs_num_outputs
;
1033 outputsWritten
&= ~(1 << FRAG_RESULT_STENCIL
);
1036 if (outputsWritten
& BITFIELD64_BIT(FRAG_RESULT_SAMPLE_MASK
)) {
1037 fs_output_semantic_name
[fs_num_outputs
] = TGSI_SEMANTIC_SAMPLEMASK
;
1038 fs_output_semantic_index
[fs_num_outputs
] = 0;
1039 outputMapping
[FRAG_RESULT_SAMPLE_MASK
] = fs_num_outputs
;
1041 outputsWritten
&= ~(1 << FRAG_RESULT_SAMPLE_MASK
);
1044 /* handle remaining outputs (color) */
1045 for (attr
= 0; attr
< ARRAY_SIZE(outputMapping
); attr
++) {
1046 const GLbitfield64 written
= attr
< FRAG_RESULT_MAX
? outputsWritten
:
1047 stfp
->Base
.SecondaryOutputsWritten
;
1048 const unsigned loc
= attr
% FRAG_RESULT_MAX
;
1050 if (written
& BITFIELD64_BIT(loc
)) {
1052 case FRAG_RESULT_DEPTH
:
1053 case FRAG_RESULT_STENCIL
:
1054 case FRAG_RESULT_SAMPLE_MASK
:
1058 case FRAG_RESULT_COLOR
:
1059 write_all
= GL_TRUE
; /* fallthrough */
1062 assert(loc
== FRAG_RESULT_COLOR
||
1063 (FRAG_RESULT_DATA0
<= loc
&& loc
< FRAG_RESULT_MAX
));
1065 index
= (loc
== FRAG_RESULT_COLOR
) ? 0 : (loc
- FRAG_RESULT_DATA0
);
1067 if (attr
>= FRAG_RESULT_MAX
) {
1068 /* Secondary color for dual source blending. */
1073 fs_output_semantic_name
[fs_num_outputs
] = TGSI_SEMANTIC_COLOR
;
1074 fs_output_semantic_index
[fs_num_outputs
] = index
;
1075 outputMapping
[attr
] = fs_num_outputs
;
1084 ureg
= ureg_create_with_screen(PIPE_SHADER_FRAGMENT
, st
->pipe
->screen
);
1088 if (ST_DEBUG
& DEBUG_MESA
) {
1089 _mesa_print_program(&stfp
->Base
);
1090 _mesa_print_program_parameters(st
->ctx
, &stfp
->Base
);
1093 if (write_all
== GL_TRUE
)
1094 ureg_property(ureg
, TGSI_PROPERTY_FS_COLOR0_WRITES_ALL_CBUFS
, 1);
1096 if (stfp
->Base
.info
.fs
.depth_layout
!= FRAG_DEPTH_LAYOUT_NONE
) {
1097 switch (stfp
->Base
.info
.fs
.depth_layout
) {
1098 case FRAG_DEPTH_LAYOUT_ANY
:
1099 ureg_property(ureg
, TGSI_PROPERTY_FS_DEPTH_LAYOUT
,
1100 TGSI_FS_DEPTH_LAYOUT_ANY
);
1102 case FRAG_DEPTH_LAYOUT_GREATER
:
1103 ureg_property(ureg
, TGSI_PROPERTY_FS_DEPTH_LAYOUT
,
1104 TGSI_FS_DEPTH_LAYOUT_GREATER
);
1106 case FRAG_DEPTH_LAYOUT_LESS
:
1107 ureg_property(ureg
, TGSI_PROPERTY_FS_DEPTH_LAYOUT
,
1108 TGSI_FS_DEPTH_LAYOUT_LESS
);
1110 case FRAG_DEPTH_LAYOUT_UNCHANGED
:
1111 ureg_property(ureg
, TGSI_PROPERTY_FS_DEPTH_LAYOUT
,
1112 TGSI_FS_DEPTH_LAYOUT_UNCHANGED
);
1119 if (stfp
->glsl_to_tgsi
) {
1120 st_translate_program(st
->ctx
,
1121 PIPE_SHADER_FRAGMENT
,
1129 input_semantic_name
,
1130 input_semantic_index
,
1135 fs_output_semantic_name
,
1136 fs_output_semantic_index
);
1138 free_glsl_to_tgsi_visitor(stfp
->glsl_to_tgsi
);
1139 } else if (stfp
->ati_fs
)
1140 st_translate_atifs_program(ureg
,
1146 input_semantic_name
,
1147 input_semantic_index
,
1152 fs_output_semantic_name
,
1153 fs_output_semantic_index
);
1155 st_translate_mesa_program(st
->ctx
,
1156 PIPE_SHADER_FRAGMENT
,
1162 input_semantic_name
,
1163 input_semantic_index
,
1168 fs_output_semantic_name
,
1169 fs_output_semantic_index
);
1171 stfp
->state
.tokens
= ureg_get_tokens(ureg
, NULL
);
1174 if (stfp
->glsl_to_tgsi
) {
1175 stfp
->glsl_to_tgsi
= NULL
;
1176 st_store_ir_in_disk_cache(st
, &stfp
->Base
, false);
1179 return stfp
->state
.tokens
!= NULL
;
1182 static struct st_fp_variant
*
1183 st_create_fp_variant(struct st_context
*st
,
1184 struct st_common_program
*stfp
,
1185 const struct st_fp_variant_key
*key
)
1187 struct pipe_context
*pipe
= st
->pipe
;
1188 struct st_fp_variant
*variant
= CALLOC_STRUCT(st_fp_variant
);
1189 struct pipe_shader_state state
= {0};
1190 struct gl_program_parameter_list
*params
= stfp
->Base
.Parameters
;
1191 static const gl_state_index16 texcoord_state
[STATE_LENGTH
] =
1192 { STATE_INTERNAL
, STATE_CURRENT_ATTRIB
, VERT_ATTRIB_TEX0
};
1193 static const gl_state_index16 scale_state
[STATE_LENGTH
] =
1194 { STATE_INTERNAL
, STATE_PT_SCALE
};
1195 static const gl_state_index16 bias_state
[STATE_LENGTH
] =
1196 { STATE_INTERNAL
, STATE_PT_BIAS
};
1197 static const gl_state_index16 alpha_ref_state
[STATE_LENGTH
] =
1198 { STATE_INTERNAL
, STATE_ALPHA_REF
};
1203 if (stfp
->state
.type
== PIPE_SHADER_IR_NIR
) {
1204 state
.type
= PIPE_SHADER_IR_NIR
;
1205 state
.ir
.nir
= nir_shader_clone(NULL
, stfp
->state
.ir
.nir
);
1207 if (key
->clamp_color
)
1208 NIR_PASS_V(state
.ir
.nir
, nir_lower_clamp_color_outputs
);
1210 if (key
->lower_flatshade
)
1211 NIR_PASS_V(state
.ir
.nir
, nir_lower_flatshade
);
1213 if (key
->lower_alpha_func
!= COMPARE_FUNC_NEVER
) {
1214 _mesa_add_state_reference(params
, alpha_ref_state
);
1215 NIR_PASS_V(state
.ir
.nir
, nir_lower_alpha_test
, key
->lower_alpha_func
,
1216 false, alpha_ref_state
);
1219 if (key
->lower_two_sided_color
)
1220 NIR_PASS_V(state
.ir
.nir
, nir_lower_two_sided_color
);
1222 if (key
->persample_shading
) {
1223 nir_shader
*shader
= state
.ir
.nir
;
1224 nir_foreach_variable(var
, &shader
->inputs
)
1225 var
->data
.sample
= true;
1228 assert(!(key
->bitmap
&& key
->drawpixels
));
1232 nir_lower_bitmap_options options
= {0};
1234 variant
->bitmap_sampler
= ffs(~stfp
->Base
.SamplersUsed
) - 1;
1235 options
.sampler
= variant
->bitmap_sampler
;
1236 options
.swizzle_xxxx
= st
->bitmap
.tex_format
== PIPE_FORMAT_R8_UNORM
;
1238 NIR_PASS_V(state
.ir
.nir
, nir_lower_bitmap
, &options
);
1241 /* glDrawPixels (color only) */
1242 if (key
->drawpixels
) {
1243 nir_lower_drawpixels_options options
= {{0}};
1244 unsigned samplers_used
= stfp
->Base
.SamplersUsed
;
1246 /* Find the first unused slot. */
1247 variant
->drawpix_sampler
= ffs(~samplers_used
) - 1;
1248 options
.drawpix_sampler
= variant
->drawpix_sampler
;
1249 samplers_used
|= (1 << variant
->drawpix_sampler
);
1251 options
.pixel_maps
= key
->pixelMaps
;
1252 if (key
->pixelMaps
) {
1253 variant
->pixelmap_sampler
= ffs(~samplers_used
) - 1;
1254 options
.pixelmap_sampler
= variant
->pixelmap_sampler
;
1257 options
.scale_and_bias
= key
->scaleAndBias
;
1258 if (key
->scaleAndBias
) {
1259 _mesa_add_state_reference(params
, scale_state
);
1260 memcpy(options
.scale_state_tokens
, scale_state
,
1261 sizeof(options
.scale_state_tokens
));
1262 _mesa_add_state_reference(params
, bias_state
);
1263 memcpy(options
.bias_state_tokens
, bias_state
,
1264 sizeof(options
.bias_state_tokens
));
1267 _mesa_add_state_reference(params
, texcoord_state
);
1268 memcpy(options
.texcoord_state_tokens
, texcoord_state
,
1269 sizeof(options
.texcoord_state_tokens
));
1271 NIR_PASS_V(state
.ir
.nir
, nir_lower_drawpixels
, &options
);
1274 if (unlikely(key
->external
.lower_nv12
|| key
->external
.lower_iyuv
||
1275 key
->external
.lower_xy_uxvx
|| key
->external
.lower_yx_xuxv
||
1276 key
->external
.lower_ayuv
|| key
->external
.lower_xyuv
)) {
1277 nir_lower_tex_options options
= {0};
1278 options
.lower_y_uv_external
= key
->external
.lower_nv12
;
1279 options
.lower_y_u_v_external
= key
->external
.lower_iyuv
;
1280 options
.lower_xy_uxvx_external
= key
->external
.lower_xy_uxvx
;
1281 options
.lower_yx_xuxv_external
= key
->external
.lower_yx_xuxv
;
1282 options
.lower_ayuv_external
= key
->external
.lower_ayuv
;
1283 options
.lower_xyuv_external
= key
->external
.lower_xyuv
;
1284 NIR_PASS_V(state
.ir
.nir
, nir_lower_tex
, &options
);
1287 st_finalize_nir(st
, &stfp
->Base
, stfp
->shader_program
, state
.ir
.nir
);
1289 if (unlikely(key
->external
.lower_nv12
|| key
->external
.lower_iyuv
||
1290 key
->external
.lower_xy_uxvx
|| key
->external
.lower_yx_xuxv
)) {
1291 /* This pass needs to happen *after* nir_lower_sampler */
1292 NIR_PASS_V(state
.ir
.nir
, st_nir_lower_tex_src_plane
,
1293 ~stfp
->Base
.SamplersUsed
,
1294 key
->external
.lower_nv12
|| key
->external
.lower_xy_uxvx
||
1295 key
->external
.lower_yx_xuxv
,
1296 key
->external
.lower_iyuv
);
1299 /* Some of the lowering above may have introduced new varyings */
1300 nir_shader_gather_info(state
.ir
.nir
,
1301 nir_shader_get_entrypoint(state
.ir
.nir
));
1303 variant
->driver_shader
= pipe
->create_fs_state(pipe
, &state
);
1304 variant
->key
= *key
;
1309 state
.tokens
= stfp
->state
.tokens
;
1311 assert(!(key
->bitmap
&& key
->drawpixels
));
1313 /* Fix texture targets and add fog for ATI_fs */
1315 const struct tgsi_token
*tokens
= st_fixup_atifs(state
.tokens
, key
);
1318 state
.tokens
= tokens
;
1320 fprintf(stderr
, "mesa: cannot post-process ATI_fs\n");
1323 /* Emulate features. */
1324 if (key
->clamp_color
|| key
->persample_shading
) {
1325 const struct tgsi_token
*tokens
;
1327 (key
->clamp_color
? TGSI_EMU_CLAMP_COLOR_OUTPUTS
: 0) |
1328 (key
->persample_shading
? TGSI_EMU_FORCE_PERSAMPLE_INTERP
: 0);
1330 tokens
= tgsi_emulate(state
.tokens
, flags
);
1333 if (state
.tokens
!= stfp
->state
.tokens
)
1334 tgsi_free_tokens(state
.tokens
);
1335 state
.tokens
= tokens
;
1337 fprintf(stderr
, "mesa: cannot emulate deprecated features\n");
1342 const struct tgsi_token
*tokens
;
1344 variant
->bitmap_sampler
= ffs(~stfp
->Base
.SamplersUsed
) - 1;
1346 tokens
= st_get_bitmap_shader(state
.tokens
,
1347 st
->internal_target
,
1348 variant
->bitmap_sampler
,
1349 st
->needs_texcoord_semantic
,
1350 st
->bitmap
.tex_format
==
1351 PIPE_FORMAT_R8_UNORM
);
1354 if (state
.tokens
!= stfp
->state
.tokens
)
1355 tgsi_free_tokens(state
.tokens
);
1356 state
.tokens
= tokens
;
1358 fprintf(stderr
, "mesa: cannot create a shader for glBitmap\n");
1361 /* glDrawPixels (color only) */
1362 if (key
->drawpixels
) {
1363 const struct tgsi_token
*tokens
;
1364 unsigned scale_const
= 0, bias_const
= 0, texcoord_const
= 0;
1366 /* Find the first unused slot. */
1367 variant
->drawpix_sampler
= ffs(~stfp
->Base
.SamplersUsed
) - 1;
1369 if (key
->pixelMaps
) {
1370 unsigned samplers_used
= stfp
->Base
.SamplersUsed
|
1371 (1 << variant
->drawpix_sampler
);
1373 variant
->pixelmap_sampler
= ffs(~samplers_used
) - 1;
1376 if (key
->scaleAndBias
) {
1377 scale_const
= _mesa_add_state_reference(params
, scale_state
);
1378 bias_const
= _mesa_add_state_reference(params
, bias_state
);
1381 texcoord_const
= _mesa_add_state_reference(params
, texcoord_state
);
1383 tokens
= st_get_drawpix_shader(state
.tokens
,
1384 st
->needs_texcoord_semantic
,
1385 key
->scaleAndBias
, scale_const
,
1386 bias_const
, key
->pixelMaps
,
1387 variant
->drawpix_sampler
,
1388 variant
->pixelmap_sampler
,
1389 texcoord_const
, st
->internal_target
);
1392 if (state
.tokens
!= stfp
->state
.tokens
)
1393 tgsi_free_tokens(state
.tokens
);
1394 state
.tokens
= tokens
;
1396 fprintf(stderr
, "mesa: cannot create a shader for glDrawPixels\n");
1399 if (unlikely(key
->external
.lower_nv12
|| key
->external
.lower_iyuv
||
1400 key
->external
.lower_xy_uxvx
|| key
->external
.lower_yx_xuxv
)) {
1401 const struct tgsi_token
*tokens
;
1403 /* samplers inserted would conflict, but this should be unpossible: */
1404 assert(!(key
->bitmap
|| key
->drawpixels
));
1406 tokens
= st_tgsi_lower_yuv(state
.tokens
,
1407 ~stfp
->Base
.SamplersUsed
,
1408 key
->external
.lower_nv12
||
1409 key
->external
.lower_xy_uxvx
||
1410 key
->external
.lower_yx_xuxv
,
1411 key
->external
.lower_iyuv
);
1413 if (state
.tokens
!= stfp
->state
.tokens
)
1414 tgsi_free_tokens(state
.tokens
);
1415 state
.tokens
= tokens
;
1417 fprintf(stderr
, "mesa: cannot create a shader for samplerExternalOES\n");
1421 if (key
->lower_depth_clamp
) {
1422 unsigned depth_range_const
= _mesa_add_state_reference(params
, depth_range_state
);
1424 const struct tgsi_token
*tokens
;
1425 tokens
= st_tgsi_lower_depth_clamp_fs(state
.tokens
, depth_range_const
);
1426 if (state
.tokens
!= stfp
->state
.tokens
)
1427 tgsi_free_tokens(state
.tokens
);
1428 state
.tokens
= tokens
;
1431 if (ST_DEBUG
& DEBUG_TGSI
) {
1432 tgsi_dump(state
.tokens
, 0);
1436 /* fill in variant */
1437 variant
->driver_shader
= pipe
->create_fs_state(pipe
, &state
);
1438 variant
->key
= *key
;
1440 if (state
.tokens
!= stfp
->state
.tokens
)
1441 tgsi_free_tokens(state
.tokens
);
1446 * Translate fragment program if needed.
1448 struct st_fp_variant
*
1449 st_get_fp_variant(struct st_context
*st
,
1450 struct st_common_program
*stfp
,
1451 const struct st_fp_variant_key
*key
)
1453 struct st_fp_variant
*fpv
;
1455 /* Search for existing variant */
1456 for (fpv
= stfp
->fp_variants
; fpv
; fpv
= fpv
->next
) {
1457 if (memcmp(&fpv
->key
, key
, sizeof(*key
)) == 0) {
1464 fpv
= st_create_fp_variant(st
, stfp
, key
);
1466 if (key
->bitmap
|| key
->drawpixels
) {
1467 /* Regular variants should always come before the
1468 * bitmap & drawpixels variants, (unless there
1469 * are no regular variants) so that
1470 * st_update_fp can take a fast path when
1471 * shader_has_one_variant is set.
1473 if (!stfp
->fp_variants
) {
1474 stfp
->fp_variants
= fpv
;
1476 /* insert into list after the first one */
1477 fpv
->next
= stfp
->fp_variants
->next
;
1478 stfp
->fp_variants
->next
= fpv
;
1481 /* insert into list */
1482 fpv
->next
= stfp
->fp_variants
;
1483 stfp
->fp_variants
= fpv
;
1492 * Translate a program. This is common code for geometry and tessellation
1496 st_translate_common_program(struct st_context
*st
,
1497 struct st_common_program
*stcp
)
1499 struct gl_program
*prog
= &stcp
->Base
;
1500 enum pipe_shader_type stage
=
1501 pipe_shader_type_from_mesa(stcp
->Base
.info
.stage
);
1502 struct ureg_program
*ureg
= ureg_create_with_screen(stage
, st
->pipe
->screen
);
1508 case PIPE_SHADER_TESS_CTRL
:
1509 ureg_property(ureg
, TGSI_PROPERTY_TCS_VERTICES_OUT
,
1510 stcp
->Base
.info
.tess
.tcs_vertices_out
);
1513 case PIPE_SHADER_TESS_EVAL
:
1514 if (stcp
->Base
.info
.tess
.primitive_mode
== GL_ISOLINES
)
1515 ureg_property(ureg
, TGSI_PROPERTY_TES_PRIM_MODE
, GL_LINES
);
1517 ureg_property(ureg
, TGSI_PROPERTY_TES_PRIM_MODE
,
1518 stcp
->Base
.info
.tess
.primitive_mode
);
1520 STATIC_ASSERT((TESS_SPACING_EQUAL
+ 1) % 3 == PIPE_TESS_SPACING_EQUAL
);
1521 STATIC_ASSERT((TESS_SPACING_FRACTIONAL_ODD
+ 1) % 3 ==
1522 PIPE_TESS_SPACING_FRACTIONAL_ODD
);
1523 STATIC_ASSERT((TESS_SPACING_FRACTIONAL_EVEN
+ 1) % 3 ==
1524 PIPE_TESS_SPACING_FRACTIONAL_EVEN
);
1526 ureg_property(ureg
, TGSI_PROPERTY_TES_SPACING
,
1527 (stcp
->Base
.info
.tess
.spacing
+ 1) % 3);
1529 ureg_property(ureg
, TGSI_PROPERTY_TES_VERTEX_ORDER_CW
,
1530 !stcp
->Base
.info
.tess
.ccw
);
1531 ureg_property(ureg
, TGSI_PROPERTY_TES_POINT_MODE
,
1532 stcp
->Base
.info
.tess
.point_mode
);
1535 case PIPE_SHADER_GEOMETRY
:
1536 ureg_property(ureg
, TGSI_PROPERTY_GS_INPUT_PRIM
,
1537 stcp
->Base
.info
.gs
.input_primitive
);
1538 ureg_property(ureg
, TGSI_PROPERTY_GS_OUTPUT_PRIM
,
1539 stcp
->Base
.info
.gs
.output_primitive
);
1540 ureg_property(ureg
, TGSI_PROPERTY_GS_MAX_OUTPUT_VERTICES
,
1541 stcp
->Base
.info
.gs
.vertices_out
);
1542 ureg_property(ureg
, TGSI_PROPERTY_GS_INVOCATIONS
,
1543 stcp
->Base
.info
.gs
.invocations
);
1550 ubyte inputSlotToAttr
[VARYING_SLOT_TESS_MAX
];
1551 ubyte inputMapping
[VARYING_SLOT_TESS_MAX
];
1552 ubyte outputMapping
[VARYING_SLOT_TESS_MAX
];
1555 ubyte input_semantic_name
[PIPE_MAX_SHADER_INPUTS
];
1556 ubyte input_semantic_index
[PIPE_MAX_SHADER_INPUTS
];
1557 uint num_inputs
= 0;
1559 ubyte output_semantic_name
[PIPE_MAX_SHADER_OUTPUTS
];
1560 ubyte output_semantic_index
[PIPE_MAX_SHADER_OUTPUTS
];
1561 uint num_outputs
= 0;
1565 memset(inputSlotToAttr
, 0, sizeof(inputSlotToAttr
));
1566 memset(inputMapping
, 0, sizeof(inputMapping
));
1567 memset(outputMapping
, 0, sizeof(outputMapping
));
1568 memset(&stcp
->state
, 0, sizeof(stcp
->state
));
1570 if (prog
->info
.clip_distance_array_size
)
1571 ureg_property(ureg
, TGSI_PROPERTY_NUM_CLIPDIST_ENABLED
,
1572 prog
->info
.clip_distance_array_size
);
1573 if (prog
->info
.cull_distance_array_size
)
1574 ureg_property(ureg
, TGSI_PROPERTY_NUM_CULLDIST_ENABLED
,
1575 prog
->info
.cull_distance_array_size
);
1578 * Convert Mesa program inputs to TGSI input register semantics.
1580 for (attr
= 0; attr
< VARYING_SLOT_MAX
; attr
++) {
1581 if ((prog
->info
.inputs_read
& BITFIELD64_BIT(attr
)) == 0)
1584 unsigned slot
= num_inputs
++;
1586 inputMapping
[attr
] = slot
;
1587 inputSlotToAttr
[slot
] = attr
;
1589 unsigned semantic_name
, semantic_index
;
1590 tgsi_get_gl_varying_semantic(attr
, st
->needs_texcoord_semantic
,
1591 &semantic_name
, &semantic_index
);
1592 input_semantic_name
[slot
] = semantic_name
;
1593 input_semantic_index
[slot
] = semantic_index
;
1596 /* Also add patch inputs. */
1597 for (attr
= 0; attr
< 32; attr
++) {
1598 if (prog
->info
.patch_inputs_read
& (1u << attr
)) {
1599 GLuint slot
= num_inputs
++;
1600 GLuint patch_attr
= VARYING_SLOT_PATCH0
+ attr
;
1602 inputMapping
[patch_attr
] = slot
;
1603 inputSlotToAttr
[slot
] = patch_attr
;
1604 input_semantic_name
[slot
] = TGSI_SEMANTIC_PATCH
;
1605 input_semantic_index
[slot
] = attr
;
1609 /* initialize output semantics to defaults */
1610 for (i
= 0; i
< PIPE_MAX_SHADER_OUTPUTS
; i
++) {
1611 output_semantic_name
[i
] = TGSI_SEMANTIC_GENERIC
;
1612 output_semantic_index
[i
] = 0;
1616 * Determine number of outputs, the (default) output register
1617 * mapping and the semantic information for each output.
1619 for (attr
= 0; attr
< VARYING_SLOT_MAX
; attr
++) {
1620 if (prog
->info
.outputs_written
& BITFIELD64_BIT(attr
)) {
1621 GLuint slot
= num_outputs
++;
1623 outputMapping
[attr
] = slot
;
1625 unsigned semantic_name
, semantic_index
;
1626 tgsi_get_gl_varying_semantic(attr
, st
->needs_texcoord_semantic
,
1627 &semantic_name
, &semantic_index
);
1628 output_semantic_name
[slot
] = semantic_name
;
1629 output_semantic_index
[slot
] = semantic_index
;
1633 /* Also add patch outputs. */
1634 for (attr
= 0; attr
< 32; attr
++) {
1635 if (prog
->info
.patch_outputs_written
& (1u << attr
)) {
1636 GLuint slot
= num_outputs
++;
1637 GLuint patch_attr
= VARYING_SLOT_PATCH0
+ attr
;
1639 outputMapping
[patch_attr
] = slot
;
1640 output_semantic_name
[slot
] = TGSI_SEMANTIC_PATCH
;
1641 output_semantic_index
[slot
] = attr
;
1645 st_translate_program(st
->ctx
,
1654 input_semantic_name
,
1655 input_semantic_index
,
1660 output_semantic_name
,
1661 output_semantic_index
);
1663 stcp
->state
.tokens
= ureg_get_tokens(ureg
, NULL
);
1667 st_translate_stream_output_info(prog
);
1669 st_store_ir_in_disk_cache(st
, prog
, false);
1671 if ((ST_DEBUG
& DEBUG_TGSI
) && (ST_DEBUG
& DEBUG_MESA
)) {
1672 _mesa_print_program(prog
);
1676 if (ST_DEBUG
& DEBUG_TGSI
) {
1677 tgsi_dump(stcp
->state
.tokens
, 0);
1681 free_glsl_to_tgsi_visitor(stcp
->glsl_to_tgsi
);
1682 stcp
->glsl_to_tgsi
= NULL
;
1688 * Get/create a basic program variant.
1690 struct st_common_variant
*
1691 st_get_common_variant(struct st_context
*st
,
1692 struct st_common_program
*prog
,
1693 const struct st_common_variant_key
*key
)
1695 struct pipe_context
*pipe
= st
->pipe
;
1696 struct st_common_variant
*v
;
1697 struct pipe_shader_state state
= {0};
1699 /* Search for existing variant */
1700 for (v
= prog
->variants
; v
; v
= v
->next
) {
1701 if (memcmp(&v
->key
, key
, sizeof(*key
)) == 0) {
1708 v
= CALLOC_STRUCT(st_common_variant
);
1711 if (prog
->state
.type
== PIPE_SHADER_IR_NIR
) {
1712 state
.type
= PIPE_SHADER_IR_NIR
;
1713 state
.ir
.nir
= nir_shader_clone(NULL
, prog
->state
.ir
.nir
);
1715 if (key
->clamp_color
)
1716 NIR_PASS_V(state
.ir
.nir
, nir_lower_clamp_color_outputs
);
1718 state
.stream_output
= prog
->state
.stream_output
;
1720 st_finalize_nir(st
, &prog
->Base
, prog
->shader_program
,
1723 if (key
->lower_depth_clamp
) {
1724 struct gl_program_parameter_list
*params
= prog
->Base
.Parameters
;
1726 unsigned depth_range_const
=
1727 _mesa_add_state_reference(params
, depth_range_state
);
1729 const struct tgsi_token
*tokens
;
1731 st_tgsi_lower_depth_clamp(prog
->state
.tokens
,
1733 key
->clip_negative_one_to_one
);
1735 if (tokens
!= prog
->state
.tokens
)
1736 tgsi_free_tokens(prog
->state
.tokens
);
1738 prog
->state
.tokens
= tokens
;
1740 state
= prog
->state
;
1742 /* fill in new variant */
1743 switch (prog
->Base
.info
.stage
) {
1744 case MESA_SHADER_TESS_CTRL
:
1745 v
->driver_shader
= pipe
->create_tcs_state(pipe
, &state
);
1747 case MESA_SHADER_TESS_EVAL
:
1748 v
->driver_shader
= pipe
->create_tes_state(pipe
, &state
);
1750 case MESA_SHADER_GEOMETRY
:
1751 v
->driver_shader
= pipe
->create_gs_state(pipe
, &state
);
1753 case MESA_SHADER_COMPUTE
: {
1754 struct pipe_compute_state cs
= {0};
1755 cs
.ir_type
= state
.type
;
1756 cs
.req_local_mem
= prog
->Base
.info
.cs
.shared_size
;
1758 if (state
.type
== PIPE_SHADER_IR_NIR
)
1759 cs
.prog
= state
.ir
.nir
;
1761 cs
.prog
= state
.tokens
;
1763 v
->driver_shader
= pipe
->create_compute_state(pipe
, &cs
);
1767 assert(!"unhandled shader type");
1774 /* insert into list */
1775 v
->next
= prog
->variants
;
1785 * Vert/Geom/Frag programs have per-context variants. Free all the
1786 * variants attached to the given program which match the given context.
1789 destroy_program_variants(struct st_context
*st
, struct gl_program
*target
)
1791 if (!target
|| target
== &_mesa_DummyProgram
)
1794 switch (target
->Target
) {
1795 case GL_VERTEX_PROGRAM_ARB
:
1797 struct st_vertex_program
*stvp
= (struct st_vertex_program
*) target
;
1798 struct st_vp_variant
*vpv
, **prevPtr
= &stvp
->variants
;
1800 for (vpv
= stvp
->variants
; vpv
; ) {
1801 struct st_vp_variant
*next
= vpv
->next
;
1802 if (vpv
->key
.st
== st
) {
1803 /* unlink from list */
1805 /* destroy this variant */
1806 delete_vp_variant(st
, vpv
);
1809 prevPtr
= &vpv
->next
;
1815 case GL_FRAGMENT_PROGRAM_ARB
:
1817 struct st_common_program
*stfp
=
1818 (struct st_common_program
*) target
;
1819 struct st_fp_variant
*fpv
, **prevPtr
= &stfp
->fp_variants
;
1821 for (fpv
= stfp
->fp_variants
; fpv
; ) {
1822 struct st_fp_variant
*next
= fpv
->next
;
1823 if (fpv
->key
.st
== st
) {
1824 /* unlink from list */
1826 /* destroy this variant */
1827 delete_fp_variant(st
, fpv
);
1830 prevPtr
= &fpv
->next
;
1836 case GL_GEOMETRY_PROGRAM_NV
:
1837 case GL_TESS_CONTROL_PROGRAM_NV
:
1838 case GL_TESS_EVALUATION_PROGRAM_NV
:
1839 case GL_COMPUTE_PROGRAM_NV
:
1841 struct st_common_program
*p
= st_common_program(target
);
1842 struct st_common_variant
*v
, **prevPtr
= &p
->variants
;
1844 for (v
= p
->variants
; v
; ) {
1845 struct st_common_variant
*next
= v
->next
;
1846 if (v
->key
.st
== st
) {
1847 /* unlink from list */
1849 /* destroy this variant */
1850 delete_basic_variant(st
, v
, target
->Target
);
1860 _mesa_problem(NULL
, "Unexpected program target 0x%x in "
1861 "destroy_program_variants_cb()", target
->Target
);
1867 * Callback for _mesa_HashWalk. Free all the shader's program variants
1868 * which match the given context.
1871 destroy_shader_program_variants_cb(GLuint key
, void *data
, void *userData
)
1873 struct st_context
*st
= (struct st_context
*) userData
;
1874 struct gl_shader
*shader
= (struct gl_shader
*) data
;
1876 switch (shader
->Type
) {
1877 case GL_SHADER_PROGRAM_MESA
:
1879 struct gl_shader_program
*shProg
= (struct gl_shader_program
*) data
;
1882 for (i
= 0; i
< ARRAY_SIZE(shProg
->_LinkedShaders
); i
++) {
1883 if (shProg
->_LinkedShaders
[i
])
1884 destroy_program_variants(st
, shProg
->_LinkedShaders
[i
]->Program
);
1888 case GL_VERTEX_SHADER
:
1889 case GL_FRAGMENT_SHADER
:
1890 case GL_GEOMETRY_SHADER
:
1891 case GL_TESS_CONTROL_SHADER
:
1892 case GL_TESS_EVALUATION_SHADER
:
1893 case GL_COMPUTE_SHADER
:
1902 * Callback for _mesa_HashWalk. Free all the program variants which match
1903 * the given context.
1906 destroy_program_variants_cb(GLuint key
, void *data
, void *userData
)
1908 struct st_context
*st
= (struct st_context
*) userData
;
1909 struct gl_program
*program
= (struct gl_program
*) data
;
1910 destroy_program_variants(st
, program
);
1915 * Walk over all shaders and programs to delete any variants which
1916 * belong to the given context.
1917 * This is called during context tear-down.
1920 st_destroy_program_variants(struct st_context
*st
)
1922 /* If shaders can be shared with other contexts, the last context will
1923 * call DeleteProgram on all shaders, releasing everything.
1925 if (st
->has_shareable_shaders
)
1928 /* ARB vert/frag program */
1929 _mesa_HashWalk(st
->ctx
->Shared
->Programs
,
1930 destroy_program_variants_cb
, st
);
1932 /* GLSL vert/frag/geom shaders */
1933 _mesa_HashWalk(st
->ctx
->Shared
->ShaderObjects
,
1934 destroy_shader_program_variants_cb
, st
);
1939 * For debugging, print/dump the current vertex program.
1942 st_print_current_vertex_program(void)
1944 GET_CURRENT_CONTEXT(ctx
);
1946 if (ctx
->VertexProgram
._Current
) {
1947 struct st_vertex_program
*stvp
=
1948 (struct st_vertex_program
*) ctx
->VertexProgram
._Current
;
1949 struct st_vp_variant
*stv
;
1951 debug_printf("Vertex program %u\n", stvp
->Base
.Id
);
1953 for (stv
= stvp
->variants
; stv
; stv
= stv
->next
) {
1954 debug_printf("variant %p\n", stv
);
1955 tgsi_dump(stv
->tokens
, 0);
1962 * Compile one shader variant.
1965 st_precompile_shader_variant(struct st_context
*st
,
1966 struct gl_program
*prog
)
1968 switch (prog
->Target
) {
1969 case GL_VERTEX_PROGRAM_ARB
: {
1970 struct st_vertex_program
*p
= (struct st_vertex_program
*)prog
;
1971 struct st_common_variant_key key
;
1973 memset(&key
, 0, sizeof(key
));
1975 key
.st
= st
->has_shareable_shaders
? NULL
: st
;
1976 st_get_vp_variant(st
, p
, &key
);
1980 case GL_FRAGMENT_PROGRAM_ARB
: {
1981 struct st_common_program
*p
= (struct st_common_program
*)prog
;
1982 struct st_fp_variant_key key
;
1984 memset(&key
, 0, sizeof(key
));
1986 key
.st
= st
->has_shareable_shaders
? NULL
: st
;
1987 st_get_fp_variant(st
, p
, &key
);
1991 case GL_TESS_CONTROL_PROGRAM_NV
:
1992 case GL_TESS_EVALUATION_PROGRAM_NV
:
1993 case GL_GEOMETRY_PROGRAM_NV
:
1994 case GL_COMPUTE_PROGRAM_NV
: {
1995 struct st_common_program
*p
= st_common_program(prog
);
1996 struct st_common_variant_key key
;
1998 memset(&key
, 0, sizeof(key
));
2000 key
.st
= st
->has_shareable_shaders
? NULL
: st
;
2001 st_get_common_variant(st
, p
, &key
);