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"
66 #include "cso_cache/cso_context.h"
71 set_affected_state_flags(uint64_t *states
,
72 struct gl_program
*prog
,
73 uint64_t new_constants
,
74 uint64_t new_sampler_views
,
75 uint64_t new_samplers
,
81 if (prog
->Parameters
->NumParameters
)
82 *states
|= new_constants
;
84 if (prog
->info
.num_textures
)
85 *states
|= new_sampler_views
| new_samplers
;
87 if (prog
->info
.num_images
)
88 *states
|= new_images
;
90 if (prog
->info
.num_ubos
)
93 if (prog
->info
.num_ssbos
)
96 if (prog
->info
.num_abos
)
97 *states
|= new_atomics
;
101 * This determines which states will be updated when the shader is bound.
104 st_set_prog_affected_state_flags(struct gl_program
*prog
)
108 switch (prog
->info
.stage
) {
109 case MESA_SHADER_VERTEX
:
110 states
= &((struct st_program
*)prog
)->affected_states
;
112 *states
= ST_NEW_VS_STATE
|
114 ST_NEW_VERTEX_ARRAYS
;
116 set_affected_state_flags(states
, prog
,
118 ST_NEW_VS_SAMPLER_VIEWS
,
126 case MESA_SHADER_TESS_CTRL
:
127 states
= &(st_program(prog
))->affected_states
;
129 *states
= ST_NEW_TCS_STATE
;
131 set_affected_state_flags(states
, prog
,
132 ST_NEW_TCS_CONSTANTS
,
133 ST_NEW_TCS_SAMPLER_VIEWS
,
141 case MESA_SHADER_TESS_EVAL
:
142 states
= &(st_program(prog
))->affected_states
;
144 *states
= ST_NEW_TES_STATE
|
147 set_affected_state_flags(states
, prog
,
148 ST_NEW_TES_CONSTANTS
,
149 ST_NEW_TES_SAMPLER_VIEWS
,
157 case MESA_SHADER_GEOMETRY
:
158 states
= &(st_program(prog
))->affected_states
;
160 *states
= ST_NEW_GS_STATE
|
163 set_affected_state_flags(states
, prog
,
165 ST_NEW_GS_SAMPLER_VIEWS
,
173 case MESA_SHADER_FRAGMENT
:
174 states
= &((struct st_program
*)prog
)->affected_states
;
176 /* gl_FragCoord and glDrawPixels always use constants. */
177 *states
= ST_NEW_FS_STATE
|
178 ST_NEW_SAMPLE_SHADING
|
181 set_affected_state_flags(states
, prog
,
183 ST_NEW_FS_SAMPLER_VIEWS
,
191 case MESA_SHADER_COMPUTE
:
192 states
= &((struct st_program
*)prog
)->affected_states
;
194 *states
= ST_NEW_CS_STATE
;
196 set_affected_state_flags(states
, prog
,
198 ST_NEW_CS_SAMPLER_VIEWS
,
207 unreachable("unhandled shader stage");
212 delete_ir(struct pipe_shader_state
*ir
)
215 ureg_free_tokens(ir
->tokens
);
219 /* Note: Any setup of ->ir.nir that has had pipe->create_*_state called on
220 * it has resulted in the driver taking ownership of the NIR. Those
221 * callers should be NULLing out the nir field in any pipe_shader_state
222 * that might have this called in order to indicate that.
224 * GLSL IR and ARB programs will have set gl_program->nir to the same
225 * shader as ir->ir.nir, so it will be freed by _mesa_delete_program().
230 * Delete a vertex program variant. Note the caller must unlink
231 * the variant from the linked list.
234 delete_vp_variant(struct st_context
*st
, struct st_vp_variant
*vpv
)
236 if (vpv
->driver_shader
) {
237 if (st
->has_shareable_shaders
|| vpv
->key
.st
== st
) {
238 cso_delete_vertex_shader(st
->cso_context
, vpv
->driver_shader
);
240 st_save_zombie_shader(vpv
->key
.st
, PIPE_SHADER_VERTEX
,
245 if (vpv
->draw_shader
)
246 draw_delete_vertex_shader( st
->draw
, vpv
->draw_shader
);
249 ureg_free_tokens(vpv
->tokens
);
257 * Clean out any old compilations:
260 st_release_vp_variants( struct st_context
*st
,
261 struct st_program
*stvp
)
263 struct st_vp_variant
*vpv
;
265 for (vpv
= stvp
->vp_variants
; vpv
; ) {
266 struct st_vp_variant
*next
= vpv
->next
;
267 delete_vp_variant(st
, vpv
);
271 stvp
->vp_variants
= NULL
;
273 delete_ir(&stvp
->state
);
279 * Delete a fragment program variant. Note the caller must unlink
280 * the variant from the linked list.
283 delete_fp_variant(struct st_context
*st
, struct st_fp_variant
*fpv
)
285 if (fpv
->driver_shader
) {
286 if (st
->has_shareable_shaders
|| fpv
->key
.st
== st
) {
287 cso_delete_fragment_shader(st
->cso_context
, fpv
->driver_shader
);
289 st_save_zombie_shader(fpv
->key
.st
, PIPE_SHADER_FRAGMENT
,
299 * Free all variants of a fragment program.
302 st_release_fp_variants(struct st_context
*st
, struct st_program
*stfp
)
304 struct st_fp_variant
*fpv
;
306 for (fpv
= stfp
->fp_variants
; fpv
; ) {
307 struct st_fp_variant
*next
= fpv
->next
;
308 delete_fp_variant(st
, fpv
);
312 stfp
->fp_variants
= NULL
;
314 delete_ir(&stfp
->state
);
319 * Delete a basic program variant. Note the caller must unlink
320 * the variant from the linked list.
323 delete_common_variant(struct st_context
*st
, struct st_common_variant
*v
,
326 if (v
->driver_shader
) {
327 if (st
->has_shareable_shaders
|| v
->key
.st
== st
) {
328 /* The shader's context matches the calling context, or we
332 case GL_TESS_CONTROL_PROGRAM_NV
:
333 cso_delete_tessctrl_shader(st
->cso_context
, v
->driver_shader
);
335 case GL_TESS_EVALUATION_PROGRAM_NV
:
336 cso_delete_tesseval_shader(st
->cso_context
, v
->driver_shader
);
338 case GL_GEOMETRY_PROGRAM_NV
:
339 cso_delete_geometry_shader(st
->cso_context
, v
->driver_shader
);
341 case GL_COMPUTE_PROGRAM_NV
:
342 cso_delete_compute_shader(st
->cso_context
, v
->driver_shader
);
345 unreachable("bad shader type in delete_basic_variant");
348 /* We can't delete a shader with a context different from the one
349 * that created it. Add it to the creating context's zombie list.
351 enum pipe_shader_type type
;
353 case GL_TESS_CONTROL_PROGRAM_NV
:
354 type
= PIPE_SHADER_TESS_CTRL
;
356 case GL_TESS_EVALUATION_PROGRAM_NV
:
357 type
= PIPE_SHADER_TESS_EVAL
;
359 case GL_GEOMETRY_PROGRAM_NV
:
360 type
= PIPE_SHADER_GEOMETRY
;
365 st_save_zombie_shader(v
->key
.st
, type
, v
->driver_shader
);
374 * Free all basic program variants.
377 st_release_common_variants(struct st_context
*st
, struct st_program
*p
)
379 struct st_common_variant
*v
;
381 for (v
= p
->variants
; v
; ) {
382 struct st_common_variant
*next
= v
->next
;
383 delete_common_variant(st
, v
, p
->Base
.Target
);
388 delete_ir(&p
->state
);
392 st_finalize_nir_before_variants(struct nir_shader
*nir
)
394 NIR_PASS_V(nir
, nir_opt_access
);
396 NIR_PASS_V(nir
, nir_split_var_copies
);
397 NIR_PASS_V(nir
, nir_lower_var_copies
);
398 if (nir
->options
->lower_all_io_to_temps
||
399 nir
->options
->lower_all_io_to_elements
||
400 nir
->info
.stage
== MESA_SHADER_VERTEX
||
401 nir
->info
.stage
== MESA_SHADER_GEOMETRY
) {
402 NIR_PASS_V(nir
, nir_lower_io_arrays_to_elements_no_indirects
, false);
403 } else if (nir
->info
.stage
== MESA_SHADER_FRAGMENT
) {
404 NIR_PASS_V(nir
, nir_lower_io_arrays_to_elements_no_indirects
, true);
407 st_nir_assign_vs_in_locations(nir
);
411 * Translate ARB (asm) program to NIR
414 st_translate_prog_to_nir(struct st_context
*st
, struct gl_program
*prog
,
415 gl_shader_stage stage
)
417 struct pipe_screen
*screen
= st
->pipe
->screen
;
418 const struct gl_shader_compiler_options
*options
=
419 &st
->ctx
->Const
.ShaderCompilerOptions
[stage
];
421 /* Translate to NIR */
422 nir_shader
*nir
= prog_to_nir(prog
, options
->NirOptions
);
423 NIR_PASS_V(nir
, nir_lower_regs_to_ssa
); /* turn registers into SSA */
424 nir_validate_shader(nir
, "after st/ptn lower_regs_to_ssa");
426 NIR_PASS_V(nir
, st_nir_lower_wpos_ytransform
, prog
, screen
);
427 NIR_PASS_V(nir
, nir_lower_system_values
);
430 NIR_PASS_V(nir
, nir_opt_constant_folding
);
432 st_finalize_nir_before_variants(nir
);
434 if (st
->allow_st_finalize_nir_twice
)
435 st_finalize_nir(st
, prog
, NULL
, nir
, true);
437 nir_validate_shader(nir
, "after st/glsl finalize_nir");
443 st_prepare_vertex_program(struct st_program
*stp
)
445 struct st_vertex_program
*stvp
= (struct st_vertex_program
*)stp
;
447 stvp
->num_inputs
= 0;
448 memset(stvp
->input_to_index
, ~0, sizeof(stvp
->input_to_index
));
449 memset(stvp
->result_to_output
, ~0, sizeof(stvp
->result_to_output
));
451 /* Determine number of inputs, the mappings between VERT_ATTRIB_x
452 * and TGSI generic input indexes, plus input attrib semantic info.
454 for (unsigned attr
= 0; attr
< VERT_ATTRIB_MAX
; attr
++) {
455 if ((stp
->Base
.info
.inputs_read
& BITFIELD64_BIT(attr
)) != 0) {
456 stvp
->input_to_index
[attr
] = stvp
->num_inputs
;
457 stvp
->index_to_input
[stvp
->num_inputs
] = attr
;
460 if ((stp
->Base
.DualSlotInputs
& BITFIELD64_BIT(attr
)) != 0) {
461 /* add placeholder for second part of a double attribute */
462 stvp
->index_to_input
[stvp
->num_inputs
] = ST_DOUBLE_ATTRIB_PLACEHOLDER
;
467 /* pre-setup potentially unused edgeflag input */
468 stvp
->input_to_index
[VERT_ATTRIB_EDGEFLAG
] = stvp
->num_inputs
;
469 stvp
->index_to_input
[stvp
->num_inputs
] = VERT_ATTRIB_EDGEFLAG
;
471 /* Compute mapping of vertex program outputs to slots. */
472 unsigned num_outputs
= 0;
473 for (unsigned attr
= 0; attr
< VARYING_SLOT_MAX
; attr
++) {
474 if (stp
->Base
.info
.outputs_written
& BITFIELD64_BIT(attr
))
475 stvp
->result_to_output
[attr
] = num_outputs
++;
477 /* pre-setup potentially unused edgeflag output */
478 stvp
->result_to_output
[VARYING_SLOT_EDGE
] = num_outputs
;
482 st_translate_stream_output_info(struct gl_program
*prog
)
484 struct gl_transform_feedback_info
*info
= prog
->sh
.LinkedTransformFeedback
;
488 /* Determine the (default) output register mapping for each output. */
489 unsigned num_outputs
= 0;
490 ubyte output_mapping
[VARYING_SLOT_TESS_MAX
];
491 memset(output_mapping
, 0, sizeof(output_mapping
));
493 for (unsigned attr
= 0; attr
< VARYING_SLOT_MAX
; attr
++) {
494 if (prog
->info
.outputs_written
& BITFIELD64_BIT(attr
))
495 output_mapping
[attr
] = num_outputs
++;
498 /* Translate stream output info. */
499 struct pipe_stream_output_info
*so_info
=
500 &((struct st_program
*)prog
)->state
.stream_output
;
502 for (unsigned i
= 0; i
< info
->NumOutputs
; i
++) {
503 so_info
->output
[i
].register_index
=
504 output_mapping
[info
->Outputs
[i
].OutputRegister
];
505 so_info
->output
[i
].start_component
= info
->Outputs
[i
].ComponentOffset
;
506 so_info
->output
[i
].num_components
= info
->Outputs
[i
].NumComponents
;
507 so_info
->output
[i
].output_buffer
= info
->Outputs
[i
].OutputBuffer
;
508 so_info
->output
[i
].dst_offset
= info
->Outputs
[i
].DstOffset
;
509 so_info
->output
[i
].stream
= info
->Outputs
[i
].StreamId
;
512 for (unsigned i
= 0; i
< PIPE_MAX_SO_BUFFERS
; i
++) {
513 so_info
->stride
[i
] = info
->Buffers
[i
].Stride
;
515 so_info
->num_outputs
= info
->NumOutputs
;
519 * Translate a vertex program.
522 st_translate_vertex_program(struct st_context
*st
,
523 struct st_program
*stp
)
525 struct ureg_program
*ureg
;
526 enum pipe_error error
;
527 unsigned num_outputs
= 0;
529 ubyte output_semantic_name
[VARYING_SLOT_MAX
] = {0};
530 ubyte output_semantic_index
[VARYING_SLOT_MAX
] = {0};
532 if (stp
->Base
.arb
.IsPositionInvariant
)
533 _mesa_insert_mvp_code(st
->ctx
, &stp
->Base
);
535 st_prepare_vertex_program(stp
);
538 if (!stp
->glsl_to_tgsi
) {
539 _mesa_remove_output_reads(&stp
->Base
, PROGRAM_OUTPUT
);
541 /* This determines which states will be updated when the assembly
544 stp
->affected_states
= ST_NEW_VS_STATE
|
546 ST_NEW_VERTEX_ARRAYS
;
548 if (stp
->Base
.Parameters
->NumParameters
)
549 stp
->affected_states
|= ST_NEW_VS_CONSTANTS
;
551 /* No samplers are allowed in ARB_vp. */
554 /* Get semantic names and indices. */
555 for (attr
= 0; attr
< VARYING_SLOT_MAX
; attr
++) {
556 if (stp
->Base
.info
.outputs_written
& BITFIELD64_BIT(attr
)) {
557 unsigned slot
= num_outputs
++;
558 unsigned semantic_name
, semantic_index
;
559 tgsi_get_gl_varying_semantic(attr
, st
->needs_texcoord_semantic
,
560 &semantic_name
, &semantic_index
);
561 output_semantic_name
[slot
] = semantic_name
;
562 output_semantic_index
[slot
] = semantic_index
;
565 /* pre-setup potentially unused edgeflag output */
566 output_semantic_name
[num_outputs
] = TGSI_SEMANTIC_EDGEFLAG
;
567 output_semantic_index
[num_outputs
] = 0;
569 ureg
= ureg_create_with_screen(PIPE_SHADER_VERTEX
, st
->pipe
->screen
);
573 if (stp
->Base
.info
.clip_distance_array_size
)
574 ureg_property(ureg
, TGSI_PROPERTY_NUM_CLIPDIST_ENABLED
,
575 stp
->Base
.info
.clip_distance_array_size
);
576 if (stp
->Base
.info
.cull_distance_array_size
)
577 ureg_property(ureg
, TGSI_PROPERTY_NUM_CULLDIST_ENABLED
,
578 stp
->Base
.info
.cull_distance_array_size
);
580 if (ST_DEBUG
& DEBUG_MESA
) {
581 _mesa_print_program(&stp
->Base
);
582 _mesa_print_program_parameters(st
->ctx
, &stp
->Base
);
586 struct st_vertex_program
*stvp
= (struct st_vertex_program
*)stp
;
588 if (stp
->glsl_to_tgsi
) {
589 error
= st_translate_program(st
->ctx
,
596 stvp
->input_to_index
,
597 NULL
, /* inputSlotToAttr */
598 NULL
, /* input semantic name */
599 NULL
, /* input semantic index */
600 NULL
, /* interp mode */
603 stvp
->result_to_output
,
604 output_semantic_name
,
605 output_semantic_index
);
607 st_translate_stream_output_info(&stp
->Base
);
609 free_glsl_to_tgsi_visitor(stp
->glsl_to_tgsi
);
611 error
= st_translate_mesa_program(st
->ctx
,
617 stvp
->input_to_index
,
618 NULL
, /* input semantic name */
619 NULL
, /* input semantic index */
623 stvp
->result_to_output
,
624 output_semantic_name
,
625 output_semantic_index
);
628 debug_printf("%s: failed to translate Mesa program:\n", __func__
);
629 _mesa_print_program(&stp
->Base
);
634 stp
->state
.tokens
= ureg_get_tokens(ureg
, NULL
);
637 if (stp
->glsl_to_tgsi
) {
638 stp
->glsl_to_tgsi
= NULL
;
639 st_store_ir_in_disk_cache(st
, &stp
->Base
, false);
644 * This must be done after the translation to TGSI is done, because
645 * we'll pass the NIR shader to the driver and the TGSI version to
646 * the draw module for the select/feedback/rasterpos code.
648 if (st
->pipe
->screen
->get_shader_param(st
->pipe
->screen
,
650 PIPE_SHADER_CAP_PREFERRED_IR
)) {
651 assert(!stp
->glsl_to_tgsi
);
654 st_translate_prog_to_nir(st
, &stp
->Base
, MESA_SHADER_VERTEX
);
656 if (stp
->state
.ir
.nir
)
657 ralloc_free(stp
->state
.ir
.nir
);
658 stp
->state
.type
= PIPE_SHADER_IR_NIR
;
659 stp
->state
.ir
.nir
= nir
;
664 return stp
->state
.tokens
!= NULL
;
667 static const gl_state_index16 depth_range_state
[STATE_LENGTH
] =
668 { STATE_DEPTH_RANGE
};
670 static struct st_vp_variant
*
671 st_create_vp_variant(struct st_context
*st
,
672 struct st_program
*stvp
,
673 const struct st_common_variant_key
*key
)
675 struct st_vp_variant
*vpv
= CALLOC_STRUCT(st_vp_variant
);
676 struct pipe_context
*pipe
= st
->pipe
;
677 struct pipe_screen
*screen
= pipe
->screen
;
678 struct pipe_shader_state state
= {0};
680 static const gl_state_index16 point_size_state
[STATE_LENGTH
] =
681 { STATE_INTERNAL
, STATE_POINT_SIZE_CLAMPED
, 0 };
682 struct gl_program_parameter_list
*params
= stvp
->Base
.Parameters
;
685 vpv
->num_inputs
= ((struct st_vertex_program
*)stvp
)->num_inputs
;
687 state
.stream_output
= stvp
->state
.stream_output
;
689 if (stvp
->state
.type
== PIPE_SHADER_IR_NIR
) {
690 bool finalize
= false;
692 state
.type
= PIPE_SHADER_IR_NIR
;
693 state
.ir
.nir
= nir_shader_clone(NULL
, stvp
->state
.ir
.nir
);
694 if (key
->clamp_color
) {
695 NIR_PASS_V(state
.ir
.nir
, nir_lower_clamp_color_outputs
);
698 if (key
->passthrough_edgeflags
) {
699 NIR_PASS_V(state
.ir
.nir
, nir_lower_passthrough_edgeflags
);
704 if (key
->lower_point_size
) {
705 _mesa_add_state_reference(params
, point_size_state
);
706 NIR_PASS_V(state
.ir
.nir
, nir_lower_point_size_mov
,
711 if (key
->lower_ucp
) {
712 bool can_compact
= screen
->get_param(screen
,
713 PIPE_CAP_NIR_COMPACT_ARRAYS
);
715 bool use_eye
= st
->ctx
->_Shader
->CurrentProgram
[MESA_SHADER_VERTEX
] != NULL
;
716 gl_state_index16 clipplane_state
[MAX_CLIP_PLANES
][STATE_LENGTH
];
717 for (int i
= 0; i
< MAX_CLIP_PLANES
; ++i
) {
719 clipplane_state
[i
][0] = STATE_CLIPPLANE
;
720 clipplane_state
[i
][1] = i
;
722 clipplane_state
[i
][0] = STATE_INTERNAL
;
723 clipplane_state
[i
][1] = STATE_CLIP_INTERNAL
;
724 clipplane_state
[i
][2] = i
;
726 _mesa_add_state_reference(params
, clipplane_state
[i
]);
729 NIR_PASS_V(state
.ir
.nir
, nir_lower_clip_vs
, key
->lower_ucp
,
730 true, can_compact
, clipplane_state
);
731 NIR_PASS_V(state
.ir
.nir
, nir_lower_io_to_temporaries
,
732 nir_shader_get_entrypoint(state
.ir
.nir
), true, false);
733 NIR_PASS_V(state
.ir
.nir
, nir_lower_global_vars_to_local
);
737 if (finalize
|| !st
->allow_st_finalize_nir_twice
) {
738 st_finalize_nir(st
, &stvp
->Base
, stvp
->shader_program
, state
.ir
.nir
,
741 /* Some of the lowering above may have introduced new varyings */
742 nir_shader_gather_info(state
.ir
.nir
,
743 nir_shader_get_entrypoint(state
.ir
.nir
));
746 if (ST_DEBUG
& DEBUG_PRINT_IR
)
747 nir_print_shader(state
.ir
.nir
, stderr
);
749 vpv
->driver_shader
= pipe
->create_vs_state(pipe
, &state
);
751 /* When generating a NIR program, we usually don't have TGSI tokens.
752 * However, we do create them for ARB_vertex_program / fixed-function VS
753 * programs which we may need to use with the draw module for legacy
754 * feedback/select emulation. If they exist, copy them.
756 * TODO: Lowering for shader variants is not applied to TGSI when
757 * generating a NIR shader.
759 if (stvp
->state
.tokens
)
760 vpv
->tokens
= tgsi_dup_tokens(stvp
->state
.tokens
);
765 state
.type
= PIPE_SHADER_IR_TGSI
;
766 state
.tokens
= tgsi_dup_tokens(stvp
->state
.tokens
);
768 /* Emulate features. */
769 if (key
->clamp_color
|| key
->passthrough_edgeflags
) {
770 const struct tgsi_token
*tokens
;
772 (key
->clamp_color
? TGSI_EMU_CLAMP_COLOR_OUTPUTS
: 0) |
773 (key
->passthrough_edgeflags
? TGSI_EMU_PASSTHROUGH_EDGEFLAG
: 0);
775 tokens
= tgsi_emulate(state
.tokens
, flags
);
778 tgsi_free_tokens(state
.tokens
);
779 state
.tokens
= tokens
;
781 if (key
->passthrough_edgeflags
)
784 fprintf(stderr
, "mesa: cannot emulate deprecated features\n");
787 if (key
->lower_depth_clamp
) {
788 unsigned depth_range_const
=
789 _mesa_add_state_reference(params
, depth_range_state
);
791 const struct tgsi_token
*tokens
;
792 tokens
= st_tgsi_lower_depth_clamp(state
.tokens
, depth_range_const
,
793 key
->clip_negative_one_to_one
);
794 if (tokens
!= state
.tokens
)
795 tgsi_free_tokens(state
.tokens
);
796 state
.tokens
= tokens
;
799 if (ST_DEBUG
& DEBUG_PRINT_IR
)
800 tgsi_dump(state
.tokens
, 0);
802 vpv
->driver_shader
= pipe
->create_vs_state(pipe
, &state
);
803 /* Save this for selection/feedback/rasterpos. */
804 vpv
->tokens
= state
.tokens
;
810 * Find/create a vertex program variant.
812 struct st_vp_variant
*
813 st_get_vp_variant(struct st_context
*st
,
814 struct st_program
*stp
,
815 const struct st_common_variant_key
*key
)
817 struct st_vertex_program
*stvp
= (struct st_vertex_program
*)stp
;
818 struct st_vp_variant
*vpv
;
820 /* Search for existing variant */
821 for (vpv
= stp
->vp_variants
; vpv
; vpv
= vpv
->next
) {
822 if (memcmp(&vpv
->key
, key
, sizeof(*key
)) == 0) {
829 vpv
= st_create_vp_variant(st
, stp
, key
);
831 for (unsigned index
= 0; index
< vpv
->num_inputs
; ++index
) {
832 unsigned attr
= stvp
->index_to_input
[index
];
833 if (attr
== ST_DOUBLE_ATTRIB_PLACEHOLDER
)
835 vpv
->vert_attrib_mask
|= 1u << attr
;
838 /* insert into list */
839 vpv
->next
= stp
->vp_variants
;
840 stp
->vp_variants
= vpv
;
849 * Translate a Mesa fragment shader into a TGSI shader.
852 st_translate_fragment_program(struct st_context
*st
,
853 struct st_program
*stfp
)
855 /* Non-GLSL programs: */
856 if (!stfp
->glsl_to_tgsi
) {
857 _mesa_remove_output_reads(&stfp
->Base
, PROGRAM_OUTPUT
);
858 if (st
->ctx
->Const
.GLSLFragCoordIsSysVal
)
859 _mesa_program_fragment_position_to_sysval(&stfp
->Base
);
861 /* This determines which states will be updated when the assembly
864 * fragment.position and glDrawPixels always use constants.
866 stfp
->affected_states
= ST_NEW_FS_STATE
|
867 ST_NEW_SAMPLE_SHADING
|
871 /* Just set them for ATI_fs unconditionally. */
872 stfp
->affected_states
|= ST_NEW_FS_SAMPLER_VIEWS
|
876 if (stfp
->Base
.SamplersUsed
)
877 stfp
->affected_states
|= ST_NEW_FS_SAMPLER_VIEWS
|
881 /* Translate to NIR. */
883 st
->pipe
->screen
->get_shader_param(st
->pipe
->screen
,
884 PIPE_SHADER_FRAGMENT
,
885 PIPE_SHADER_CAP_PREFERRED_IR
)) {
887 st_translate_prog_to_nir(st
, &stfp
->Base
, MESA_SHADER_FRAGMENT
);
889 if (stfp
->state
.ir
.nir
)
890 ralloc_free(stfp
->state
.ir
.nir
);
891 stfp
->state
.type
= PIPE_SHADER_IR_NIR
;
892 stfp
->state
.ir
.nir
= nir
;
893 stfp
->Base
.nir
= nir
;
898 ubyte outputMapping
[2 * FRAG_RESULT_MAX
];
899 ubyte inputMapping
[VARYING_SLOT_MAX
];
900 ubyte inputSlotToAttr
[VARYING_SLOT_MAX
];
901 ubyte interpMode
[PIPE_MAX_SHADER_INPUTS
]; /* XXX size? */
903 GLbitfield64 inputsRead
;
904 struct ureg_program
*ureg
;
906 GLboolean write_all
= GL_FALSE
;
908 ubyte input_semantic_name
[PIPE_MAX_SHADER_INPUTS
];
909 ubyte input_semantic_index
[PIPE_MAX_SHADER_INPUTS
];
910 uint fs_num_inputs
= 0;
912 ubyte fs_output_semantic_name
[PIPE_MAX_SHADER_OUTPUTS
];
913 ubyte fs_output_semantic_index
[PIPE_MAX_SHADER_OUTPUTS
];
914 uint fs_num_outputs
= 0;
916 memset(inputSlotToAttr
, ~0, sizeof(inputSlotToAttr
));
919 * Convert Mesa program inputs to TGSI input register semantics.
921 inputsRead
= stfp
->Base
.info
.inputs_read
;
922 for (attr
= 0; attr
< VARYING_SLOT_MAX
; attr
++) {
923 if ((inputsRead
& BITFIELD64_BIT(attr
)) != 0) {
924 const GLuint slot
= fs_num_inputs
++;
926 inputMapping
[attr
] = slot
;
927 inputSlotToAttr
[slot
] = attr
;
930 case VARYING_SLOT_POS
:
931 input_semantic_name
[slot
] = TGSI_SEMANTIC_POSITION
;
932 input_semantic_index
[slot
] = 0;
933 interpMode
[slot
] = TGSI_INTERPOLATE_LINEAR
;
935 case VARYING_SLOT_COL0
:
936 input_semantic_name
[slot
] = TGSI_SEMANTIC_COLOR
;
937 input_semantic_index
[slot
] = 0;
938 interpMode
[slot
] = stfp
->glsl_to_tgsi
?
939 TGSI_INTERPOLATE_COUNT
: TGSI_INTERPOLATE_COLOR
;
941 case VARYING_SLOT_COL1
:
942 input_semantic_name
[slot
] = TGSI_SEMANTIC_COLOR
;
943 input_semantic_index
[slot
] = 1;
944 interpMode
[slot
] = stfp
->glsl_to_tgsi
?
945 TGSI_INTERPOLATE_COUNT
: TGSI_INTERPOLATE_COLOR
;
947 case VARYING_SLOT_FOGC
:
948 input_semantic_name
[slot
] = TGSI_SEMANTIC_FOG
;
949 input_semantic_index
[slot
] = 0;
950 interpMode
[slot
] = TGSI_INTERPOLATE_PERSPECTIVE
;
952 case VARYING_SLOT_FACE
:
953 input_semantic_name
[slot
] = TGSI_SEMANTIC_FACE
;
954 input_semantic_index
[slot
] = 0;
955 interpMode
[slot
] = TGSI_INTERPOLATE_CONSTANT
;
957 case VARYING_SLOT_PRIMITIVE_ID
:
958 input_semantic_name
[slot
] = TGSI_SEMANTIC_PRIMID
;
959 input_semantic_index
[slot
] = 0;
960 interpMode
[slot
] = TGSI_INTERPOLATE_CONSTANT
;
962 case VARYING_SLOT_LAYER
:
963 input_semantic_name
[slot
] = TGSI_SEMANTIC_LAYER
;
964 input_semantic_index
[slot
] = 0;
965 interpMode
[slot
] = TGSI_INTERPOLATE_CONSTANT
;
967 case VARYING_SLOT_VIEWPORT
:
968 input_semantic_name
[slot
] = TGSI_SEMANTIC_VIEWPORT_INDEX
;
969 input_semantic_index
[slot
] = 0;
970 interpMode
[slot
] = TGSI_INTERPOLATE_CONSTANT
;
972 case VARYING_SLOT_CLIP_DIST0
:
973 input_semantic_name
[slot
] = TGSI_SEMANTIC_CLIPDIST
;
974 input_semantic_index
[slot
] = 0;
975 interpMode
[slot
] = TGSI_INTERPOLATE_PERSPECTIVE
;
977 case VARYING_SLOT_CLIP_DIST1
:
978 input_semantic_name
[slot
] = TGSI_SEMANTIC_CLIPDIST
;
979 input_semantic_index
[slot
] = 1;
980 interpMode
[slot
] = TGSI_INTERPOLATE_PERSPECTIVE
;
982 case VARYING_SLOT_CULL_DIST0
:
983 case VARYING_SLOT_CULL_DIST1
:
984 /* these should have been lowered by GLSL */
987 /* In most cases, there is nothing special about these
988 * inputs, so adopt a convention to use the generic
989 * semantic name and the mesa VARYING_SLOT_ number as the
992 * All that is required is that the vertex shader labels
993 * its own outputs similarly, and that the vertex shader
994 * generates at least every output required by the
995 * fragment shader plus fixed-function hardware (such as
998 * However, some drivers may need us to identify the PNTC and TEXi
999 * varyings if, for example, their capability to replace them with
1000 * sprite coordinates is limited.
1002 case VARYING_SLOT_PNTC
:
1003 if (st
->needs_texcoord_semantic
) {
1004 input_semantic_name
[slot
] = TGSI_SEMANTIC_PCOORD
;
1005 input_semantic_index
[slot
] = 0;
1006 interpMode
[slot
] = TGSI_INTERPOLATE_LINEAR
;
1010 case VARYING_SLOT_TEX0
:
1011 case VARYING_SLOT_TEX1
:
1012 case VARYING_SLOT_TEX2
:
1013 case VARYING_SLOT_TEX3
:
1014 case VARYING_SLOT_TEX4
:
1015 case VARYING_SLOT_TEX5
:
1016 case VARYING_SLOT_TEX6
:
1017 case VARYING_SLOT_TEX7
:
1018 if (st
->needs_texcoord_semantic
) {
1019 input_semantic_name
[slot
] = TGSI_SEMANTIC_TEXCOORD
;
1020 input_semantic_index
[slot
] = attr
- VARYING_SLOT_TEX0
;
1021 interpMode
[slot
] = stfp
->glsl_to_tgsi
?
1022 TGSI_INTERPOLATE_COUNT
: TGSI_INTERPOLATE_PERSPECTIVE
;
1026 case VARYING_SLOT_VAR0
:
1028 /* Semantic indices should be zero-based because drivers may choose
1029 * to assign a fixed slot determined by that index.
1030 * This is useful because ARB_separate_shader_objects uses location
1031 * qualifiers for linkage, and if the semantic index corresponds to
1032 * these locations, linkage passes in the driver become unecessary.
1034 * If needs_texcoord_semantic is true, no semantic indices will be
1035 * consumed for the TEXi varyings, and we can base the locations of
1036 * the user varyings on VAR0. Otherwise, we use TEX0 as base index.
1038 assert(attr
>= VARYING_SLOT_VAR0
|| attr
== VARYING_SLOT_PNTC
||
1039 (attr
>= VARYING_SLOT_TEX0
&& attr
<= VARYING_SLOT_TEX7
));
1040 input_semantic_name
[slot
] = TGSI_SEMANTIC_GENERIC
;
1041 input_semantic_index
[slot
] = st_get_generic_varying_index(st
, attr
);
1042 if (attr
== VARYING_SLOT_PNTC
)
1043 interpMode
[slot
] = TGSI_INTERPOLATE_LINEAR
;
1045 interpMode
[slot
] = stfp
->glsl_to_tgsi
?
1046 TGSI_INTERPOLATE_COUNT
: TGSI_INTERPOLATE_PERSPECTIVE
;
1052 inputMapping
[attr
] = -1;
1057 * Semantics and mapping for outputs
1059 GLbitfield64 outputsWritten
= stfp
->Base
.info
.outputs_written
;
1061 /* if z is written, emit that first */
1062 if (outputsWritten
& BITFIELD64_BIT(FRAG_RESULT_DEPTH
)) {
1063 fs_output_semantic_name
[fs_num_outputs
] = TGSI_SEMANTIC_POSITION
;
1064 fs_output_semantic_index
[fs_num_outputs
] = 0;
1065 outputMapping
[FRAG_RESULT_DEPTH
] = fs_num_outputs
;
1067 outputsWritten
&= ~(1 << FRAG_RESULT_DEPTH
);
1070 if (outputsWritten
& BITFIELD64_BIT(FRAG_RESULT_STENCIL
)) {
1071 fs_output_semantic_name
[fs_num_outputs
] = TGSI_SEMANTIC_STENCIL
;
1072 fs_output_semantic_index
[fs_num_outputs
] = 0;
1073 outputMapping
[FRAG_RESULT_STENCIL
] = fs_num_outputs
;
1075 outputsWritten
&= ~(1 << FRAG_RESULT_STENCIL
);
1078 if (outputsWritten
& BITFIELD64_BIT(FRAG_RESULT_SAMPLE_MASK
)) {
1079 fs_output_semantic_name
[fs_num_outputs
] = TGSI_SEMANTIC_SAMPLEMASK
;
1080 fs_output_semantic_index
[fs_num_outputs
] = 0;
1081 outputMapping
[FRAG_RESULT_SAMPLE_MASK
] = fs_num_outputs
;
1083 outputsWritten
&= ~(1 << FRAG_RESULT_SAMPLE_MASK
);
1086 /* handle remaining outputs (color) */
1087 for (attr
= 0; attr
< ARRAY_SIZE(outputMapping
); attr
++) {
1088 const GLbitfield64 written
= attr
< FRAG_RESULT_MAX
? outputsWritten
:
1089 stfp
->Base
.SecondaryOutputsWritten
;
1090 const unsigned loc
= attr
% FRAG_RESULT_MAX
;
1092 if (written
& BITFIELD64_BIT(loc
)) {
1094 case FRAG_RESULT_DEPTH
:
1095 case FRAG_RESULT_STENCIL
:
1096 case FRAG_RESULT_SAMPLE_MASK
:
1100 case FRAG_RESULT_COLOR
:
1101 write_all
= GL_TRUE
; /* fallthrough */
1104 assert(loc
== FRAG_RESULT_COLOR
||
1105 (FRAG_RESULT_DATA0
<= loc
&& loc
< FRAG_RESULT_MAX
));
1107 index
= (loc
== FRAG_RESULT_COLOR
) ? 0 : (loc
- FRAG_RESULT_DATA0
);
1109 if (attr
>= FRAG_RESULT_MAX
) {
1110 /* Secondary color for dual source blending. */
1115 fs_output_semantic_name
[fs_num_outputs
] = TGSI_SEMANTIC_COLOR
;
1116 fs_output_semantic_index
[fs_num_outputs
] = index
;
1117 outputMapping
[attr
] = fs_num_outputs
;
1126 ureg
= ureg_create_with_screen(PIPE_SHADER_FRAGMENT
, st
->pipe
->screen
);
1130 if (ST_DEBUG
& DEBUG_MESA
) {
1131 _mesa_print_program(&stfp
->Base
);
1132 _mesa_print_program_parameters(st
->ctx
, &stfp
->Base
);
1135 if (write_all
== GL_TRUE
)
1136 ureg_property(ureg
, TGSI_PROPERTY_FS_COLOR0_WRITES_ALL_CBUFS
, 1);
1138 if (stfp
->Base
.info
.fs
.depth_layout
!= FRAG_DEPTH_LAYOUT_NONE
) {
1139 switch (stfp
->Base
.info
.fs
.depth_layout
) {
1140 case FRAG_DEPTH_LAYOUT_ANY
:
1141 ureg_property(ureg
, TGSI_PROPERTY_FS_DEPTH_LAYOUT
,
1142 TGSI_FS_DEPTH_LAYOUT_ANY
);
1144 case FRAG_DEPTH_LAYOUT_GREATER
:
1145 ureg_property(ureg
, TGSI_PROPERTY_FS_DEPTH_LAYOUT
,
1146 TGSI_FS_DEPTH_LAYOUT_GREATER
);
1148 case FRAG_DEPTH_LAYOUT_LESS
:
1149 ureg_property(ureg
, TGSI_PROPERTY_FS_DEPTH_LAYOUT
,
1150 TGSI_FS_DEPTH_LAYOUT_LESS
);
1152 case FRAG_DEPTH_LAYOUT_UNCHANGED
:
1153 ureg_property(ureg
, TGSI_PROPERTY_FS_DEPTH_LAYOUT
,
1154 TGSI_FS_DEPTH_LAYOUT_UNCHANGED
);
1161 if (stfp
->glsl_to_tgsi
) {
1162 st_translate_program(st
->ctx
,
1163 PIPE_SHADER_FRAGMENT
,
1171 input_semantic_name
,
1172 input_semantic_index
,
1177 fs_output_semantic_name
,
1178 fs_output_semantic_index
);
1180 free_glsl_to_tgsi_visitor(stfp
->glsl_to_tgsi
);
1181 } else if (stfp
->ati_fs
)
1182 st_translate_atifs_program(ureg
,
1188 input_semantic_name
,
1189 input_semantic_index
,
1194 fs_output_semantic_name
,
1195 fs_output_semantic_index
);
1197 st_translate_mesa_program(st
->ctx
,
1198 PIPE_SHADER_FRAGMENT
,
1204 input_semantic_name
,
1205 input_semantic_index
,
1210 fs_output_semantic_name
,
1211 fs_output_semantic_index
);
1213 stfp
->state
.tokens
= ureg_get_tokens(ureg
, NULL
);
1216 if (stfp
->glsl_to_tgsi
) {
1217 stfp
->glsl_to_tgsi
= NULL
;
1218 st_store_ir_in_disk_cache(st
, &stfp
->Base
, false);
1221 return stfp
->state
.tokens
!= NULL
;
1224 static struct st_fp_variant
*
1225 st_create_fp_variant(struct st_context
*st
,
1226 struct st_program
*stfp
,
1227 const struct st_fp_variant_key
*key
)
1229 struct pipe_context
*pipe
= st
->pipe
;
1230 struct st_fp_variant
*variant
= CALLOC_STRUCT(st_fp_variant
);
1231 struct pipe_shader_state state
= {0};
1232 struct gl_program_parameter_list
*params
= stfp
->Base
.Parameters
;
1233 static const gl_state_index16 texcoord_state
[STATE_LENGTH
] =
1234 { STATE_INTERNAL
, STATE_CURRENT_ATTRIB
, VERT_ATTRIB_TEX0
};
1235 static const gl_state_index16 scale_state
[STATE_LENGTH
] =
1236 { STATE_INTERNAL
, STATE_PT_SCALE
};
1237 static const gl_state_index16 bias_state
[STATE_LENGTH
] =
1238 { STATE_INTERNAL
, STATE_PT_BIAS
};
1239 static const gl_state_index16 alpha_ref_state
[STATE_LENGTH
] =
1240 { STATE_INTERNAL
, STATE_ALPHA_REF
};
1245 if (stfp
->state
.type
== PIPE_SHADER_IR_NIR
) {
1246 bool finalize
= false;
1248 state
.type
= PIPE_SHADER_IR_NIR
;
1249 state
.ir
.nir
= nir_shader_clone(NULL
, stfp
->state
.ir
.nir
);
1251 if (key
->clamp_color
) {
1252 NIR_PASS_V(state
.ir
.nir
, nir_lower_clamp_color_outputs
);
1256 if (key
->lower_flatshade
) {
1257 NIR_PASS_V(state
.ir
.nir
, nir_lower_flatshade
);
1261 if (key
->lower_alpha_func
!= COMPARE_FUNC_NEVER
) {
1262 _mesa_add_state_reference(params
, alpha_ref_state
);
1263 NIR_PASS_V(state
.ir
.nir
, nir_lower_alpha_test
, key
->lower_alpha_func
,
1264 false, alpha_ref_state
);
1268 if (key
->lower_two_sided_color
) {
1269 NIR_PASS_V(state
.ir
.nir
, nir_lower_two_sided_color
);
1273 if (key
->persample_shading
) {
1274 nir_shader
*shader
= state
.ir
.nir
;
1275 nir_foreach_variable(var
, &shader
->inputs
)
1276 var
->data
.sample
= true;
1280 assert(!(key
->bitmap
&& key
->drawpixels
));
1284 nir_lower_bitmap_options options
= {0};
1286 variant
->bitmap_sampler
= ffs(~stfp
->Base
.SamplersUsed
) - 1;
1287 options
.sampler
= variant
->bitmap_sampler
;
1288 options
.swizzle_xxxx
= st
->bitmap
.tex_format
== PIPE_FORMAT_R8_UNORM
;
1290 NIR_PASS_V(state
.ir
.nir
, nir_lower_bitmap
, &options
);
1294 /* glDrawPixels (color only) */
1295 if (key
->drawpixels
) {
1296 nir_lower_drawpixels_options options
= {{0}};
1297 unsigned samplers_used
= stfp
->Base
.SamplersUsed
;
1299 /* Find the first unused slot. */
1300 variant
->drawpix_sampler
= ffs(~samplers_used
) - 1;
1301 options
.drawpix_sampler
= variant
->drawpix_sampler
;
1302 samplers_used
|= (1 << variant
->drawpix_sampler
);
1304 options
.pixel_maps
= key
->pixelMaps
;
1305 if (key
->pixelMaps
) {
1306 variant
->pixelmap_sampler
= ffs(~samplers_used
) - 1;
1307 options
.pixelmap_sampler
= variant
->pixelmap_sampler
;
1310 options
.scale_and_bias
= key
->scaleAndBias
;
1311 if (key
->scaleAndBias
) {
1312 _mesa_add_state_reference(params
, scale_state
);
1313 memcpy(options
.scale_state_tokens
, scale_state
,
1314 sizeof(options
.scale_state_tokens
));
1315 _mesa_add_state_reference(params
, bias_state
);
1316 memcpy(options
.bias_state_tokens
, bias_state
,
1317 sizeof(options
.bias_state_tokens
));
1320 _mesa_add_state_reference(params
, texcoord_state
);
1321 memcpy(options
.texcoord_state_tokens
, texcoord_state
,
1322 sizeof(options
.texcoord_state_tokens
));
1324 NIR_PASS_V(state
.ir
.nir
, nir_lower_drawpixels
, &options
);
1328 if (unlikely(key
->external
.lower_nv12
|| key
->external
.lower_iyuv
||
1329 key
->external
.lower_xy_uxvx
|| key
->external
.lower_yx_xuxv
||
1330 key
->external
.lower_ayuv
|| key
->external
.lower_xyuv
)) {
1331 nir_lower_tex_options options
= {0};
1332 options
.lower_y_uv_external
= key
->external
.lower_nv12
;
1333 options
.lower_y_u_v_external
= key
->external
.lower_iyuv
;
1334 options
.lower_xy_uxvx_external
= key
->external
.lower_xy_uxvx
;
1335 options
.lower_yx_xuxv_external
= key
->external
.lower_yx_xuxv
;
1336 options
.lower_ayuv_external
= key
->external
.lower_ayuv
;
1337 options
.lower_xyuv_external
= key
->external
.lower_xyuv
;
1338 NIR_PASS_V(state
.ir
.nir
, nir_lower_tex
, &options
);
1342 if (finalize
|| !st
->allow_st_finalize_nir_twice
) {
1343 st_finalize_nir(st
, &stfp
->Base
, stfp
->shader_program
, state
.ir
.nir
,
1347 /* This pass needs to happen *after* nir_lower_sampler */
1348 if (unlikely(key
->external
.lower_nv12
|| key
->external
.lower_iyuv
||
1349 key
->external
.lower_xy_uxvx
|| key
->external
.lower_yx_xuxv
)) {
1350 NIR_PASS_V(state
.ir
.nir
, st_nir_lower_tex_src_plane
,
1351 ~stfp
->Base
.SamplersUsed
,
1352 key
->external
.lower_nv12
|| key
->external
.lower_xy_uxvx
||
1353 key
->external
.lower_yx_xuxv
,
1354 key
->external
.lower_iyuv
);
1358 if (finalize
|| !st
->allow_st_finalize_nir_twice
) {
1359 /* Some of the lowering above may have introduced new varyings */
1360 nir_shader_gather_info(state
.ir
.nir
,
1361 nir_shader_get_entrypoint(state
.ir
.nir
));
1363 struct pipe_screen
*screen
= pipe
->screen
;
1364 if (screen
->finalize_nir
)
1365 screen
->finalize_nir(screen
, state
.ir
.nir
, false);
1368 if (ST_DEBUG
& DEBUG_PRINT_IR
)
1369 nir_print_shader(state
.ir
.nir
, stderr
);
1371 variant
->driver_shader
= pipe
->create_fs_state(pipe
, &state
);
1372 variant
->key
= *key
;
1377 state
.tokens
= stfp
->state
.tokens
;
1379 assert(!(key
->bitmap
&& key
->drawpixels
));
1381 /* Fix texture targets and add fog for ATI_fs */
1383 const struct tgsi_token
*tokens
= st_fixup_atifs(state
.tokens
, key
);
1386 state
.tokens
= tokens
;
1388 fprintf(stderr
, "mesa: cannot post-process ATI_fs\n");
1391 /* Emulate features. */
1392 if (key
->clamp_color
|| key
->persample_shading
) {
1393 const struct tgsi_token
*tokens
;
1395 (key
->clamp_color
? TGSI_EMU_CLAMP_COLOR_OUTPUTS
: 0) |
1396 (key
->persample_shading
? TGSI_EMU_FORCE_PERSAMPLE_INTERP
: 0);
1398 tokens
= tgsi_emulate(state
.tokens
, flags
);
1401 if (state
.tokens
!= stfp
->state
.tokens
)
1402 tgsi_free_tokens(state
.tokens
);
1403 state
.tokens
= tokens
;
1405 fprintf(stderr
, "mesa: cannot emulate deprecated features\n");
1410 const struct tgsi_token
*tokens
;
1412 variant
->bitmap_sampler
= ffs(~stfp
->Base
.SamplersUsed
) - 1;
1414 tokens
= st_get_bitmap_shader(state
.tokens
,
1415 st
->internal_target
,
1416 variant
->bitmap_sampler
,
1417 st
->needs_texcoord_semantic
,
1418 st
->bitmap
.tex_format
==
1419 PIPE_FORMAT_R8_UNORM
);
1422 if (state
.tokens
!= stfp
->state
.tokens
)
1423 tgsi_free_tokens(state
.tokens
);
1424 state
.tokens
= tokens
;
1426 fprintf(stderr
, "mesa: cannot create a shader for glBitmap\n");
1429 /* glDrawPixels (color only) */
1430 if (key
->drawpixels
) {
1431 const struct tgsi_token
*tokens
;
1432 unsigned scale_const
= 0, bias_const
= 0, texcoord_const
= 0;
1434 /* Find the first unused slot. */
1435 variant
->drawpix_sampler
= ffs(~stfp
->Base
.SamplersUsed
) - 1;
1437 if (key
->pixelMaps
) {
1438 unsigned samplers_used
= stfp
->Base
.SamplersUsed
|
1439 (1 << variant
->drawpix_sampler
);
1441 variant
->pixelmap_sampler
= ffs(~samplers_used
) - 1;
1444 if (key
->scaleAndBias
) {
1445 scale_const
= _mesa_add_state_reference(params
, scale_state
);
1446 bias_const
= _mesa_add_state_reference(params
, bias_state
);
1449 texcoord_const
= _mesa_add_state_reference(params
, texcoord_state
);
1451 tokens
= st_get_drawpix_shader(state
.tokens
,
1452 st
->needs_texcoord_semantic
,
1453 key
->scaleAndBias
, scale_const
,
1454 bias_const
, key
->pixelMaps
,
1455 variant
->drawpix_sampler
,
1456 variant
->pixelmap_sampler
,
1457 texcoord_const
, st
->internal_target
);
1460 if (state
.tokens
!= stfp
->state
.tokens
)
1461 tgsi_free_tokens(state
.tokens
);
1462 state
.tokens
= tokens
;
1464 fprintf(stderr
, "mesa: cannot create a shader for glDrawPixels\n");
1467 if (unlikely(key
->external
.lower_nv12
|| key
->external
.lower_iyuv
||
1468 key
->external
.lower_xy_uxvx
|| key
->external
.lower_yx_xuxv
)) {
1469 const struct tgsi_token
*tokens
;
1471 /* samplers inserted would conflict, but this should be unpossible: */
1472 assert(!(key
->bitmap
|| key
->drawpixels
));
1474 tokens
= st_tgsi_lower_yuv(state
.tokens
,
1475 ~stfp
->Base
.SamplersUsed
,
1476 key
->external
.lower_nv12
||
1477 key
->external
.lower_xy_uxvx
||
1478 key
->external
.lower_yx_xuxv
,
1479 key
->external
.lower_iyuv
);
1481 if (state
.tokens
!= stfp
->state
.tokens
)
1482 tgsi_free_tokens(state
.tokens
);
1483 state
.tokens
= tokens
;
1485 fprintf(stderr
, "mesa: cannot create a shader for samplerExternalOES\n");
1489 if (key
->lower_depth_clamp
) {
1490 unsigned depth_range_const
= _mesa_add_state_reference(params
, depth_range_state
);
1492 const struct tgsi_token
*tokens
;
1493 tokens
= st_tgsi_lower_depth_clamp_fs(state
.tokens
, depth_range_const
);
1494 if (state
.tokens
!= stfp
->state
.tokens
)
1495 tgsi_free_tokens(state
.tokens
);
1496 state
.tokens
= tokens
;
1499 if (ST_DEBUG
& DEBUG_PRINT_IR
)
1500 tgsi_dump(state
.tokens
, 0);
1502 /* fill in variant */
1503 variant
->driver_shader
= pipe
->create_fs_state(pipe
, &state
);
1504 variant
->key
= *key
;
1506 if (state
.tokens
!= stfp
->state
.tokens
)
1507 tgsi_free_tokens(state
.tokens
);
1512 * Translate fragment program if needed.
1514 struct st_fp_variant
*
1515 st_get_fp_variant(struct st_context
*st
,
1516 struct st_program
*stfp
,
1517 const struct st_fp_variant_key
*key
)
1519 struct st_fp_variant
*fpv
;
1521 /* Search for existing variant */
1522 for (fpv
= stfp
->fp_variants
; fpv
; fpv
= fpv
->next
) {
1523 if (memcmp(&fpv
->key
, key
, sizeof(*key
)) == 0) {
1530 fpv
= st_create_fp_variant(st
, stfp
, key
);
1532 if (key
->bitmap
|| key
->drawpixels
) {
1533 /* Regular variants should always come before the
1534 * bitmap & drawpixels variants, (unless there
1535 * are no regular variants) so that
1536 * st_update_fp can take a fast path when
1537 * shader_has_one_variant is set.
1539 if (!stfp
->fp_variants
) {
1540 stfp
->fp_variants
= fpv
;
1542 /* insert into list after the first one */
1543 fpv
->next
= stfp
->fp_variants
->next
;
1544 stfp
->fp_variants
->next
= fpv
;
1547 /* insert into list */
1548 fpv
->next
= stfp
->fp_variants
;
1549 stfp
->fp_variants
= fpv
;
1558 * Translate a program. This is common code for geometry and tessellation
1562 st_translate_common_program(struct st_context
*st
,
1563 struct st_program
*stp
)
1565 struct gl_program
*prog
= &stp
->Base
;
1566 enum pipe_shader_type stage
=
1567 pipe_shader_type_from_mesa(stp
->Base
.info
.stage
);
1568 struct ureg_program
*ureg
= ureg_create_with_screen(stage
, st
->pipe
->screen
);
1574 case PIPE_SHADER_TESS_CTRL
:
1575 ureg_property(ureg
, TGSI_PROPERTY_TCS_VERTICES_OUT
,
1576 stp
->Base
.info
.tess
.tcs_vertices_out
);
1579 case PIPE_SHADER_TESS_EVAL
:
1580 if (stp
->Base
.info
.tess
.primitive_mode
== GL_ISOLINES
)
1581 ureg_property(ureg
, TGSI_PROPERTY_TES_PRIM_MODE
, GL_LINES
);
1583 ureg_property(ureg
, TGSI_PROPERTY_TES_PRIM_MODE
,
1584 stp
->Base
.info
.tess
.primitive_mode
);
1586 STATIC_ASSERT((TESS_SPACING_EQUAL
+ 1) % 3 == PIPE_TESS_SPACING_EQUAL
);
1587 STATIC_ASSERT((TESS_SPACING_FRACTIONAL_ODD
+ 1) % 3 ==
1588 PIPE_TESS_SPACING_FRACTIONAL_ODD
);
1589 STATIC_ASSERT((TESS_SPACING_FRACTIONAL_EVEN
+ 1) % 3 ==
1590 PIPE_TESS_SPACING_FRACTIONAL_EVEN
);
1592 ureg_property(ureg
, TGSI_PROPERTY_TES_SPACING
,
1593 (stp
->Base
.info
.tess
.spacing
+ 1) % 3);
1595 ureg_property(ureg
, TGSI_PROPERTY_TES_VERTEX_ORDER_CW
,
1596 !stp
->Base
.info
.tess
.ccw
);
1597 ureg_property(ureg
, TGSI_PROPERTY_TES_POINT_MODE
,
1598 stp
->Base
.info
.tess
.point_mode
);
1601 case PIPE_SHADER_GEOMETRY
:
1602 ureg_property(ureg
, TGSI_PROPERTY_GS_INPUT_PRIM
,
1603 stp
->Base
.info
.gs
.input_primitive
);
1604 ureg_property(ureg
, TGSI_PROPERTY_GS_OUTPUT_PRIM
,
1605 stp
->Base
.info
.gs
.output_primitive
);
1606 ureg_property(ureg
, TGSI_PROPERTY_GS_MAX_OUTPUT_VERTICES
,
1607 stp
->Base
.info
.gs
.vertices_out
);
1608 ureg_property(ureg
, TGSI_PROPERTY_GS_INVOCATIONS
,
1609 stp
->Base
.info
.gs
.invocations
);
1616 ubyte inputSlotToAttr
[VARYING_SLOT_TESS_MAX
];
1617 ubyte inputMapping
[VARYING_SLOT_TESS_MAX
];
1618 ubyte outputMapping
[VARYING_SLOT_TESS_MAX
];
1621 ubyte input_semantic_name
[PIPE_MAX_SHADER_INPUTS
];
1622 ubyte input_semantic_index
[PIPE_MAX_SHADER_INPUTS
];
1623 uint num_inputs
= 0;
1625 ubyte output_semantic_name
[PIPE_MAX_SHADER_OUTPUTS
];
1626 ubyte output_semantic_index
[PIPE_MAX_SHADER_OUTPUTS
];
1627 uint num_outputs
= 0;
1631 memset(inputSlotToAttr
, 0, sizeof(inputSlotToAttr
));
1632 memset(inputMapping
, 0, sizeof(inputMapping
));
1633 memset(outputMapping
, 0, sizeof(outputMapping
));
1634 memset(&stp
->state
, 0, sizeof(stp
->state
));
1636 if (prog
->info
.clip_distance_array_size
)
1637 ureg_property(ureg
, TGSI_PROPERTY_NUM_CLIPDIST_ENABLED
,
1638 prog
->info
.clip_distance_array_size
);
1639 if (prog
->info
.cull_distance_array_size
)
1640 ureg_property(ureg
, TGSI_PROPERTY_NUM_CULLDIST_ENABLED
,
1641 prog
->info
.cull_distance_array_size
);
1644 * Convert Mesa program inputs to TGSI input register semantics.
1646 for (attr
= 0; attr
< VARYING_SLOT_MAX
; attr
++) {
1647 if ((prog
->info
.inputs_read
& BITFIELD64_BIT(attr
)) == 0)
1650 unsigned slot
= num_inputs
++;
1652 inputMapping
[attr
] = slot
;
1653 inputSlotToAttr
[slot
] = attr
;
1655 unsigned semantic_name
, semantic_index
;
1656 tgsi_get_gl_varying_semantic(attr
, st
->needs_texcoord_semantic
,
1657 &semantic_name
, &semantic_index
);
1658 input_semantic_name
[slot
] = semantic_name
;
1659 input_semantic_index
[slot
] = semantic_index
;
1662 /* Also add patch inputs. */
1663 for (attr
= 0; attr
< 32; attr
++) {
1664 if (prog
->info
.patch_inputs_read
& (1u << attr
)) {
1665 GLuint slot
= num_inputs
++;
1666 GLuint patch_attr
= VARYING_SLOT_PATCH0
+ attr
;
1668 inputMapping
[patch_attr
] = slot
;
1669 inputSlotToAttr
[slot
] = patch_attr
;
1670 input_semantic_name
[slot
] = TGSI_SEMANTIC_PATCH
;
1671 input_semantic_index
[slot
] = attr
;
1675 /* initialize output semantics to defaults */
1676 for (i
= 0; i
< PIPE_MAX_SHADER_OUTPUTS
; i
++) {
1677 output_semantic_name
[i
] = TGSI_SEMANTIC_GENERIC
;
1678 output_semantic_index
[i
] = 0;
1682 * Determine number of outputs, the (default) output register
1683 * mapping and the semantic information for each output.
1685 for (attr
= 0; attr
< VARYING_SLOT_MAX
; attr
++) {
1686 if (prog
->info
.outputs_written
& BITFIELD64_BIT(attr
)) {
1687 GLuint slot
= num_outputs
++;
1689 outputMapping
[attr
] = slot
;
1691 unsigned semantic_name
, semantic_index
;
1692 tgsi_get_gl_varying_semantic(attr
, st
->needs_texcoord_semantic
,
1693 &semantic_name
, &semantic_index
);
1694 output_semantic_name
[slot
] = semantic_name
;
1695 output_semantic_index
[slot
] = semantic_index
;
1699 /* Also add patch outputs. */
1700 for (attr
= 0; attr
< 32; attr
++) {
1701 if (prog
->info
.patch_outputs_written
& (1u << attr
)) {
1702 GLuint slot
= num_outputs
++;
1703 GLuint patch_attr
= VARYING_SLOT_PATCH0
+ attr
;
1705 outputMapping
[patch_attr
] = slot
;
1706 output_semantic_name
[slot
] = TGSI_SEMANTIC_PATCH
;
1707 output_semantic_index
[slot
] = attr
;
1711 st_translate_program(st
->ctx
,
1720 input_semantic_name
,
1721 input_semantic_index
,
1726 output_semantic_name
,
1727 output_semantic_index
);
1729 stp
->state
.tokens
= ureg_get_tokens(ureg
, NULL
);
1733 st_translate_stream_output_info(prog
);
1735 st_store_ir_in_disk_cache(st
, prog
, false);
1737 if (ST_DEBUG
& DEBUG_PRINT_IR
&& ST_DEBUG
& DEBUG_MESA
)
1738 _mesa_print_program(prog
);
1740 free_glsl_to_tgsi_visitor(stp
->glsl_to_tgsi
);
1741 stp
->glsl_to_tgsi
= NULL
;
1747 * Get/create a basic program variant.
1749 struct st_common_variant
*
1750 st_get_common_variant(struct st_context
*st
,
1751 struct st_program
*prog
,
1752 const struct st_common_variant_key
*key
)
1754 struct pipe_context
*pipe
= st
->pipe
;
1755 struct st_common_variant
*v
;
1756 struct pipe_shader_state state
= {0};
1758 /* Search for existing variant */
1759 for (v
= prog
->variants
; v
; v
= v
->next
) {
1760 if (memcmp(&v
->key
, key
, sizeof(*key
)) == 0) {
1767 v
= CALLOC_STRUCT(st_common_variant
);
1769 if (prog
->state
.type
== PIPE_SHADER_IR_NIR
) {
1770 bool finalize
= false;
1772 state
.type
= PIPE_SHADER_IR_NIR
;
1773 state
.ir
.nir
= nir_shader_clone(NULL
, prog
->state
.ir
.nir
);
1775 if (key
->clamp_color
) {
1776 NIR_PASS_V(state
.ir
.nir
, nir_lower_clamp_color_outputs
);
1780 state
.stream_output
= prog
->state
.stream_output
;
1782 if (finalize
|| !st
->allow_st_finalize_nir_twice
) {
1783 st_finalize_nir(st
, &prog
->Base
, prog
->shader_program
,
1784 state
.ir
.nir
, true);
1787 if (ST_DEBUG
& DEBUG_PRINT_IR
)
1788 nir_print_shader(state
.ir
.nir
, stderr
);
1790 if (key
->lower_depth_clamp
) {
1791 struct gl_program_parameter_list
*params
= prog
->Base
.Parameters
;
1793 unsigned depth_range_const
=
1794 _mesa_add_state_reference(params
, depth_range_state
);
1796 const struct tgsi_token
*tokens
;
1798 st_tgsi_lower_depth_clamp(prog
->state
.tokens
,
1800 key
->clip_negative_one_to_one
);
1802 if (tokens
!= prog
->state
.tokens
)
1803 tgsi_free_tokens(prog
->state
.tokens
);
1805 prog
->state
.tokens
= tokens
;
1807 state
= prog
->state
;
1809 if (ST_DEBUG
& DEBUG_PRINT_IR
)
1810 tgsi_dump(state
.tokens
, 0);
1812 /* fill in new variant */
1813 switch (prog
->Base
.info
.stage
) {
1814 case MESA_SHADER_TESS_CTRL
:
1815 v
->driver_shader
= pipe
->create_tcs_state(pipe
, &state
);
1817 case MESA_SHADER_TESS_EVAL
:
1818 v
->driver_shader
= pipe
->create_tes_state(pipe
, &state
);
1820 case MESA_SHADER_GEOMETRY
:
1821 v
->driver_shader
= pipe
->create_gs_state(pipe
, &state
);
1823 case MESA_SHADER_COMPUTE
: {
1824 struct pipe_compute_state cs
= {0};
1825 cs
.ir_type
= state
.type
;
1826 cs
.req_local_mem
= prog
->Base
.info
.cs
.shared_size
;
1828 if (state
.type
== PIPE_SHADER_IR_NIR
)
1829 cs
.prog
= state
.ir
.nir
;
1831 cs
.prog
= state
.tokens
;
1833 v
->driver_shader
= pipe
->create_compute_state(pipe
, &cs
);
1837 assert(!"unhandled shader type");
1844 /* insert into list */
1845 v
->next
= prog
->variants
;
1855 * Vert/Geom/Frag programs have per-context variants. Free all the
1856 * variants attached to the given program which match the given context.
1859 destroy_program_variants(struct st_context
*st
, struct gl_program
*target
)
1861 if (!target
|| target
== &_mesa_DummyProgram
)
1864 switch (target
->Target
) {
1865 case GL_VERTEX_PROGRAM_ARB
:
1867 struct st_program
*stvp
= (struct st_program
*) target
;
1868 struct st_vp_variant
*vpv
, **prevPtr
= &stvp
->vp_variants
;
1870 for (vpv
= stvp
->vp_variants
; vpv
; ) {
1871 struct st_vp_variant
*next
= vpv
->next
;
1872 if (vpv
->key
.st
== st
) {
1873 /* unlink from list */
1875 /* destroy this variant */
1876 delete_vp_variant(st
, vpv
);
1879 prevPtr
= &vpv
->next
;
1885 case GL_FRAGMENT_PROGRAM_ARB
:
1887 struct st_program
*stfp
=
1888 (struct st_program
*) target
;
1889 struct st_fp_variant
*fpv
, **prevPtr
= &stfp
->fp_variants
;
1891 for (fpv
= stfp
->fp_variants
; fpv
; ) {
1892 struct st_fp_variant
*next
= fpv
->next
;
1893 if (fpv
->key
.st
== st
) {
1894 /* unlink from list */
1896 /* destroy this variant */
1897 delete_fp_variant(st
, fpv
);
1900 prevPtr
= &fpv
->next
;
1906 case GL_GEOMETRY_PROGRAM_NV
:
1907 case GL_TESS_CONTROL_PROGRAM_NV
:
1908 case GL_TESS_EVALUATION_PROGRAM_NV
:
1909 case GL_COMPUTE_PROGRAM_NV
:
1911 struct st_program
*p
= st_program(target
);
1912 struct st_common_variant
*v
, **prevPtr
= &p
->variants
;
1914 for (v
= p
->variants
; v
; ) {
1915 struct st_common_variant
*next
= v
->next
;
1916 if (v
->key
.st
== st
) {
1917 /* unlink from list */
1919 /* destroy this variant */
1920 delete_common_variant(st
, v
, target
->Target
);
1930 _mesa_problem(NULL
, "Unexpected program target 0x%x in "
1931 "destroy_program_variants_cb()", target
->Target
);
1937 * Callback for _mesa_HashWalk. Free all the shader's program variants
1938 * which match the given context.
1941 destroy_shader_program_variants_cb(GLuint key
, void *data
, void *userData
)
1943 struct st_context
*st
= (struct st_context
*) userData
;
1944 struct gl_shader
*shader
= (struct gl_shader
*) data
;
1946 switch (shader
->Type
) {
1947 case GL_SHADER_PROGRAM_MESA
:
1949 struct gl_shader_program
*shProg
= (struct gl_shader_program
*) data
;
1952 for (i
= 0; i
< ARRAY_SIZE(shProg
->_LinkedShaders
); i
++) {
1953 if (shProg
->_LinkedShaders
[i
])
1954 destroy_program_variants(st
, shProg
->_LinkedShaders
[i
]->Program
);
1958 case GL_VERTEX_SHADER
:
1959 case GL_FRAGMENT_SHADER
:
1960 case GL_GEOMETRY_SHADER
:
1961 case GL_TESS_CONTROL_SHADER
:
1962 case GL_TESS_EVALUATION_SHADER
:
1963 case GL_COMPUTE_SHADER
:
1972 * Callback for _mesa_HashWalk. Free all the program variants which match
1973 * the given context.
1976 destroy_program_variants_cb(GLuint key
, void *data
, void *userData
)
1978 struct st_context
*st
= (struct st_context
*) userData
;
1979 struct gl_program
*program
= (struct gl_program
*) data
;
1980 destroy_program_variants(st
, program
);
1985 * Walk over all shaders and programs to delete any variants which
1986 * belong to the given context.
1987 * This is called during context tear-down.
1990 st_destroy_program_variants(struct st_context
*st
)
1992 /* If shaders can be shared with other contexts, the last context will
1993 * call DeleteProgram on all shaders, releasing everything.
1995 if (st
->has_shareable_shaders
)
1998 /* ARB vert/frag program */
1999 _mesa_HashWalk(st
->ctx
->Shared
->Programs
,
2000 destroy_program_variants_cb
, st
);
2002 /* GLSL vert/frag/geom shaders */
2003 _mesa_HashWalk(st
->ctx
->Shared
->ShaderObjects
,
2004 destroy_shader_program_variants_cb
, st
);
2009 * Compile one shader variant.
2012 st_precompile_shader_variant(struct st_context
*st
,
2013 struct gl_program
*prog
)
2015 switch (prog
->Target
) {
2016 case GL_VERTEX_PROGRAM_ARB
: {
2017 struct st_program
*p
= (struct st_program
*)prog
;
2018 struct st_common_variant_key key
;
2020 memset(&key
, 0, sizeof(key
));
2022 key
.st
= st
->has_shareable_shaders
? NULL
: st
;
2023 st_get_vp_variant(st
, p
, &key
);
2027 case GL_FRAGMENT_PROGRAM_ARB
: {
2028 struct st_program
*p
= (struct st_program
*)prog
;
2029 struct st_fp_variant_key key
;
2031 memset(&key
, 0, sizeof(key
));
2033 key
.st
= st
->has_shareable_shaders
? NULL
: st
;
2034 st_get_fp_variant(st
, p
, &key
);
2038 case GL_TESS_CONTROL_PROGRAM_NV
:
2039 case GL_TESS_EVALUATION_PROGRAM_NV
:
2040 case GL_GEOMETRY_PROGRAM_NV
:
2041 case GL_COMPUTE_PROGRAM_NV
: {
2042 struct st_program
*p
= st_program(prog
);
2043 struct st_common_variant_key key
;
2045 memset(&key
, 0, sizeof(key
));
2047 key
.st
= st
->has_shareable_shaders
? NULL
: st
;
2048 st_get_common_variant(st
, p
, &key
);
2058 st_finalize_program(struct st_context
*st
, struct gl_program
*prog
)
2060 if (st
->current_program
[prog
->info
.stage
] == prog
) {
2061 if (prog
->info
.stage
== MESA_SHADER_VERTEX
)
2062 st
->dirty
|= ST_NEW_VERTEX_PROGRAM(st
, (struct st_program
*)prog
);
2064 st
->dirty
|= ((struct st_program
*)prog
)->affected_states
;
2068 nir_sweep(prog
->nir
);
2070 /* Create Gallium shaders now instead of on demand. */
2071 if (ST_DEBUG
& DEBUG_PRECOMPILE
||
2072 st
->shader_has_one_variant
[prog
->info
.stage
])
2073 st_precompile_shader_variant(st
, prog
);