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"
44 #include "draw/draw_context.h"
46 #include "pipe/p_context.h"
47 #include "pipe/p_defines.h"
48 #include "pipe/p_shader_tokens.h"
49 #include "draw/draw_context.h"
50 #include "tgsi/tgsi_dump.h"
51 #include "tgsi/tgsi_emulate.h"
52 #include "tgsi/tgsi_parse.h"
53 #include "tgsi/tgsi_ureg.h"
56 #include "st_cb_bitmap.h"
57 #include "st_cb_drawpixels.h"
58 #include "st_context.h"
59 #include "st_tgsi_lower_depth_clamp.h"
60 #include "st_tgsi_lower_yuv.h"
61 #include "st_program.h"
62 #include "st_mesa_to_tgsi.h"
63 #include "st_atifs_to_tgsi.h"
65 #include "st_shader_cache.h"
67 #include "cso_cache/cso_context.h"
72 set_affected_state_flags(uint64_t *states
,
73 struct gl_program
*prog
,
74 uint64_t new_constants
,
75 uint64_t new_sampler_views
,
76 uint64_t new_samplers
,
82 if (prog
->Parameters
->NumParameters
)
83 *states
|= new_constants
;
85 if (prog
->info
.num_textures
)
86 *states
|= new_sampler_views
| new_samplers
;
88 if (prog
->info
.num_images
)
89 *states
|= new_images
;
91 if (prog
->info
.num_ubos
)
94 if (prog
->info
.num_ssbos
)
97 if (prog
->info
.num_abos
)
98 *states
|= new_atomics
;
102 * This determines which states will be updated when the shader is bound.
105 st_set_prog_affected_state_flags(struct gl_program
*prog
)
109 switch (prog
->info
.stage
) {
110 case MESA_SHADER_VERTEX
:
111 states
= &((struct st_program
*)prog
)->affected_states
;
113 *states
= ST_NEW_VS_STATE
|
115 ST_NEW_VERTEX_ARRAYS
;
117 set_affected_state_flags(states
, prog
,
119 ST_NEW_VS_SAMPLER_VIEWS
,
127 case MESA_SHADER_TESS_CTRL
:
128 states
= &(st_program(prog
))->affected_states
;
130 *states
= ST_NEW_TCS_STATE
;
132 set_affected_state_flags(states
, prog
,
133 ST_NEW_TCS_CONSTANTS
,
134 ST_NEW_TCS_SAMPLER_VIEWS
,
142 case MESA_SHADER_TESS_EVAL
:
143 states
= &(st_program(prog
))->affected_states
;
145 *states
= ST_NEW_TES_STATE
|
148 set_affected_state_flags(states
, prog
,
149 ST_NEW_TES_CONSTANTS
,
150 ST_NEW_TES_SAMPLER_VIEWS
,
158 case MESA_SHADER_GEOMETRY
:
159 states
= &(st_program(prog
))->affected_states
;
161 *states
= ST_NEW_GS_STATE
|
164 set_affected_state_flags(states
, prog
,
166 ST_NEW_GS_SAMPLER_VIEWS
,
174 case MESA_SHADER_FRAGMENT
:
175 states
= &((struct st_program
*)prog
)->affected_states
;
177 /* gl_FragCoord and glDrawPixels always use constants. */
178 *states
= ST_NEW_FS_STATE
|
179 ST_NEW_SAMPLE_SHADING
|
182 set_affected_state_flags(states
, prog
,
184 ST_NEW_FS_SAMPLER_VIEWS
,
192 case MESA_SHADER_COMPUTE
:
193 states
= &((struct st_program
*)prog
)->affected_states
;
195 *states
= ST_NEW_CS_STATE
;
197 set_affected_state_flags(states
, prog
,
199 ST_NEW_CS_SAMPLER_VIEWS
,
208 unreachable("unhandled shader stage");
214 * Delete a shader variant. Note the caller must unlink the variant from
218 delete_variant(struct st_context
*st
, struct st_variant
*v
, GLenum target
)
220 if (v
->driver_shader
) {
221 if (target
== GL_VERTEX_PROGRAM_ARB
&&
222 ((struct st_common_variant
*)v
)->key
.is_draw_shader
) {
224 draw_delete_vertex_shader(st
->draw
, v
->driver_shader
);
225 } else if (st
->has_shareable_shaders
|| v
->st
== st
) {
226 /* The shader's context matches the calling context, or we
230 case GL_VERTEX_PROGRAM_ARB
:
231 st
->pipe
->delete_vs_state(st
->pipe
, v
->driver_shader
);
233 case GL_TESS_CONTROL_PROGRAM_NV
:
234 st
->pipe
->delete_tcs_state(st
->pipe
, v
->driver_shader
);
236 case GL_TESS_EVALUATION_PROGRAM_NV
:
237 st
->pipe
->delete_tes_state(st
->pipe
, v
->driver_shader
);
239 case GL_GEOMETRY_PROGRAM_NV
:
240 st
->pipe
->delete_gs_state(st
->pipe
, v
->driver_shader
);
242 case GL_FRAGMENT_PROGRAM_ARB
:
243 st
->pipe
->delete_fs_state(st
->pipe
, v
->driver_shader
);
245 case GL_COMPUTE_PROGRAM_NV
:
246 st
->pipe
->delete_compute_state(st
->pipe
, v
->driver_shader
);
249 unreachable("bad shader type in delete_basic_variant");
252 /* We can't delete a shader with a context different from the one
253 * that created it. Add it to the creating context's zombie list.
255 enum pipe_shader_type type
=
256 pipe_shader_type_from_mesa(_mesa_program_enum_to_shader_stage(target
));
258 st_save_zombie_shader(v
->st
, type
, v
->driver_shader
);
266 st_unbind_program(struct st_context
*st
, struct st_program
*p
)
268 /* Unbind the shader in cso_context and re-bind in st/mesa. */
269 switch (p
->Base
.info
.stage
) {
270 case MESA_SHADER_VERTEX
:
271 cso_set_vertex_shader_handle(st
->cso_context
, NULL
);
272 st
->dirty
|= ST_NEW_VS_STATE
;
274 case MESA_SHADER_TESS_CTRL
:
275 cso_set_tessctrl_shader_handle(st
->cso_context
, NULL
);
276 st
->dirty
|= ST_NEW_TCS_STATE
;
278 case MESA_SHADER_TESS_EVAL
:
279 cso_set_tesseval_shader_handle(st
->cso_context
, NULL
);
280 st
->dirty
|= ST_NEW_TES_STATE
;
282 case MESA_SHADER_GEOMETRY
:
283 cso_set_geometry_shader_handle(st
->cso_context
, NULL
);
284 st
->dirty
|= ST_NEW_GS_STATE
;
286 case MESA_SHADER_FRAGMENT
:
287 cso_set_fragment_shader_handle(st
->cso_context
, NULL
);
288 st
->dirty
|= ST_NEW_FS_STATE
;
290 case MESA_SHADER_COMPUTE
:
291 cso_set_compute_shader_handle(st
->cso_context
, NULL
);
292 st
->dirty
|= ST_NEW_CS_STATE
;
295 unreachable("invalid shader type");
300 * Free all basic program variants.
303 st_release_variants(struct st_context
*st
, struct st_program
*p
)
305 struct st_variant
*v
;
307 /* If we are releasing shaders, re-bind them, because we don't
308 * know which shaders are bound in the driver.
311 st_unbind_program(st
, p
);
313 for (v
= p
->variants
; v
; ) {
314 struct st_variant
*next
= v
->next
;
315 delete_variant(st
, v
, p
->Base
.Target
);
321 if (p
->state
.tokens
) {
322 ureg_free_tokens(p
->state
.tokens
);
323 p
->state
.tokens
= NULL
;
326 /* Note: Any setup of ->ir.nir that has had pipe->create_*_state called on
327 * it has resulted in the driver taking ownership of the NIR. Those
328 * callers should be NULLing out the nir field in any pipe_shader_state
329 * that might have this called in order to indicate that.
331 * GLSL IR and ARB programs will have set gl_program->nir to the same
332 * shader as ir->ir.nir, so it will be freed by _mesa_delete_program().
337 st_finalize_nir_before_variants(struct nir_shader
*nir
)
339 NIR_PASS_V(nir
, nir_opt_access
);
341 NIR_PASS_V(nir
, nir_split_var_copies
);
342 NIR_PASS_V(nir
, nir_lower_var_copies
);
343 if (nir
->options
->lower_all_io_to_temps
||
344 nir
->options
->lower_all_io_to_elements
||
345 nir
->info
.stage
== MESA_SHADER_VERTEX
||
346 nir
->info
.stage
== MESA_SHADER_GEOMETRY
) {
347 NIR_PASS_V(nir
, nir_lower_io_arrays_to_elements_no_indirects
, false);
348 } else if (nir
->info
.stage
== MESA_SHADER_FRAGMENT
) {
349 NIR_PASS_V(nir
, nir_lower_io_arrays_to_elements_no_indirects
, true);
352 st_nir_assign_vs_in_locations(nir
);
356 * Translate ARB (asm) program to NIR
359 st_translate_prog_to_nir(struct st_context
*st
, struct gl_program
*prog
,
360 gl_shader_stage stage
)
362 struct pipe_screen
*screen
= st
->pipe
->screen
;
363 const struct gl_shader_compiler_options
*options
=
364 &st
->ctx
->Const
.ShaderCompilerOptions
[stage
];
366 /* Translate to NIR */
367 nir_shader
*nir
= prog_to_nir(prog
, options
->NirOptions
);
368 NIR_PASS_V(nir
, nir_lower_regs_to_ssa
); /* turn registers into SSA */
369 nir_validate_shader(nir
, "after st/ptn lower_regs_to_ssa");
371 NIR_PASS_V(nir
, st_nir_lower_wpos_ytransform
, prog
, screen
);
372 NIR_PASS_V(nir
, nir_lower_system_values
);
375 NIR_PASS_V(nir
, nir_opt_constant_folding
);
377 st_finalize_nir_before_variants(nir
);
379 if (st
->allow_st_finalize_nir_twice
)
380 st_finalize_nir(st
, prog
, NULL
, nir
, true);
382 nir_validate_shader(nir
, "after st/glsl finalize_nir");
388 st_prepare_vertex_program(struct st_program
*stp
)
390 struct st_vertex_program
*stvp
= (struct st_vertex_program
*)stp
;
392 stvp
->num_inputs
= 0;
393 memset(stvp
->input_to_index
, ~0, sizeof(stvp
->input_to_index
));
394 memset(stvp
->result_to_output
, ~0, sizeof(stvp
->result_to_output
));
396 /* Determine number of inputs, the mappings between VERT_ATTRIB_x
397 * and TGSI generic input indexes, plus input attrib semantic info.
399 for (unsigned attr
= 0; attr
< VERT_ATTRIB_MAX
; attr
++) {
400 if ((stp
->Base
.info
.inputs_read
& BITFIELD64_BIT(attr
)) != 0) {
401 stvp
->input_to_index
[attr
] = stvp
->num_inputs
;
402 stvp
->index_to_input
[stvp
->num_inputs
] = attr
;
405 if ((stp
->Base
.DualSlotInputs
& BITFIELD64_BIT(attr
)) != 0) {
406 /* add placeholder for second part of a double attribute */
407 stvp
->index_to_input
[stvp
->num_inputs
] = ST_DOUBLE_ATTRIB_PLACEHOLDER
;
412 /* pre-setup potentially unused edgeflag input */
413 stvp
->input_to_index
[VERT_ATTRIB_EDGEFLAG
] = stvp
->num_inputs
;
414 stvp
->index_to_input
[stvp
->num_inputs
] = VERT_ATTRIB_EDGEFLAG
;
416 /* Compute mapping of vertex program outputs to slots. */
417 unsigned num_outputs
= 0;
418 for (unsigned attr
= 0; attr
< VARYING_SLOT_MAX
; attr
++) {
419 if (stp
->Base
.info
.outputs_written
& BITFIELD64_BIT(attr
))
420 stvp
->result_to_output
[attr
] = num_outputs
++;
422 /* pre-setup potentially unused edgeflag output */
423 stvp
->result_to_output
[VARYING_SLOT_EDGE
] = num_outputs
;
427 st_translate_stream_output_info(struct gl_program
*prog
)
429 struct gl_transform_feedback_info
*info
= prog
->sh
.LinkedTransformFeedback
;
433 /* Determine the (default) output register mapping for each output. */
434 unsigned num_outputs
= 0;
435 ubyte output_mapping
[VARYING_SLOT_TESS_MAX
];
436 memset(output_mapping
, 0, sizeof(output_mapping
));
438 for (unsigned attr
= 0; attr
< VARYING_SLOT_MAX
; attr
++) {
439 if (prog
->info
.outputs_written
& BITFIELD64_BIT(attr
))
440 output_mapping
[attr
] = num_outputs
++;
443 /* Translate stream output info. */
444 struct pipe_stream_output_info
*so_info
=
445 &((struct st_program
*)prog
)->state
.stream_output
;
447 for (unsigned i
= 0; i
< info
->NumOutputs
; i
++) {
448 so_info
->output
[i
].register_index
=
449 output_mapping
[info
->Outputs
[i
].OutputRegister
];
450 so_info
->output
[i
].start_component
= info
->Outputs
[i
].ComponentOffset
;
451 so_info
->output
[i
].num_components
= info
->Outputs
[i
].NumComponents
;
452 so_info
->output
[i
].output_buffer
= info
->Outputs
[i
].OutputBuffer
;
453 so_info
->output
[i
].dst_offset
= info
->Outputs
[i
].DstOffset
;
454 so_info
->output
[i
].stream
= info
->Outputs
[i
].StreamId
;
457 for (unsigned i
= 0; i
< PIPE_MAX_SO_BUFFERS
; i
++) {
458 so_info
->stride
[i
] = info
->Buffers
[i
].Stride
;
460 so_info
->num_outputs
= info
->NumOutputs
;
464 * Translate a vertex program.
467 st_translate_vertex_program(struct st_context
*st
,
468 struct st_program
*stp
)
470 struct ureg_program
*ureg
;
471 enum pipe_error error
;
472 unsigned num_outputs
= 0;
474 ubyte output_semantic_name
[VARYING_SLOT_MAX
] = {0};
475 ubyte output_semantic_index
[VARYING_SLOT_MAX
] = {0};
477 if (stp
->Base
.arb
.IsPositionInvariant
)
478 _mesa_insert_mvp_code(st
->ctx
, &stp
->Base
);
480 st_prepare_vertex_program(stp
);
483 if (!stp
->glsl_to_tgsi
) {
484 _mesa_remove_output_reads(&stp
->Base
, PROGRAM_OUTPUT
);
486 /* This determines which states will be updated when the assembly
489 stp
->affected_states
= ST_NEW_VS_STATE
|
491 ST_NEW_VERTEX_ARRAYS
;
493 if (stp
->Base
.Parameters
->NumParameters
)
494 stp
->affected_states
|= ST_NEW_VS_CONSTANTS
;
496 /* Translate to NIR if preferred. */
497 if (st
->pipe
->screen
->get_shader_param(st
->pipe
->screen
,
499 PIPE_SHADER_CAP_PREFERRED_IR
)) {
500 assert(!stp
->glsl_to_tgsi
);
503 ralloc_free(stp
->Base
.nir
);
505 stp
->state
.type
= PIPE_SHADER_IR_NIR
;
506 stp
->Base
.nir
= st_translate_prog_to_nir(st
, &stp
->Base
,
508 /* For st_draw_feedback, we need to generate TGSI too if draw doesn't
516 /* Get semantic names and indices. */
517 for (attr
= 0; attr
< VARYING_SLOT_MAX
; attr
++) {
518 if (stp
->Base
.info
.outputs_written
& BITFIELD64_BIT(attr
)) {
519 unsigned slot
= num_outputs
++;
520 unsigned semantic_name
, semantic_index
;
521 tgsi_get_gl_varying_semantic(attr
, st
->needs_texcoord_semantic
,
522 &semantic_name
, &semantic_index
);
523 output_semantic_name
[slot
] = semantic_name
;
524 output_semantic_index
[slot
] = semantic_index
;
527 /* pre-setup potentially unused edgeflag output */
528 output_semantic_name
[num_outputs
] = TGSI_SEMANTIC_EDGEFLAG
;
529 output_semantic_index
[num_outputs
] = 0;
531 ureg
= ureg_create_with_screen(PIPE_SHADER_VERTEX
, st
->pipe
->screen
);
535 if (stp
->Base
.info
.clip_distance_array_size
)
536 ureg_property(ureg
, TGSI_PROPERTY_NUM_CLIPDIST_ENABLED
,
537 stp
->Base
.info
.clip_distance_array_size
);
538 if (stp
->Base
.info
.cull_distance_array_size
)
539 ureg_property(ureg
, TGSI_PROPERTY_NUM_CULLDIST_ENABLED
,
540 stp
->Base
.info
.cull_distance_array_size
);
542 if (ST_DEBUG
& DEBUG_MESA
) {
543 _mesa_print_program(&stp
->Base
);
544 _mesa_print_program_parameters(st
->ctx
, &stp
->Base
);
548 struct st_vertex_program
*stvp
= (struct st_vertex_program
*)stp
;
550 if (stp
->glsl_to_tgsi
) {
551 error
= st_translate_program(st
->ctx
,
558 stvp
->input_to_index
,
559 NULL
, /* inputSlotToAttr */
560 NULL
, /* input semantic name */
561 NULL
, /* input semantic index */
562 NULL
, /* interp mode */
565 stvp
->result_to_output
,
566 output_semantic_name
,
567 output_semantic_index
);
569 st_translate_stream_output_info(&stp
->Base
);
571 free_glsl_to_tgsi_visitor(stp
->glsl_to_tgsi
);
573 error
= st_translate_mesa_program(st
->ctx
,
579 stvp
->input_to_index
,
580 NULL
, /* input semantic name */
581 NULL
, /* input semantic index */
585 stvp
->result_to_output
,
586 output_semantic_name
,
587 output_semantic_index
);
590 debug_printf("%s: failed to translate Mesa program:\n", __func__
);
591 _mesa_print_program(&stp
->Base
);
596 stp
->state
.tokens
= ureg_get_tokens(ureg
, NULL
);
599 if (stp
->glsl_to_tgsi
) {
600 stp
->glsl_to_tgsi
= NULL
;
601 st_store_ir_in_disk_cache(st
, &stp
->Base
, false);
604 return stp
->state
.tokens
!= NULL
;
607 static const gl_state_index16 depth_range_state
[STATE_LENGTH
] =
608 { STATE_DEPTH_RANGE
};
610 static struct st_common_variant
*
611 st_create_vp_variant(struct st_context
*st
,
612 struct st_program
*stvp
,
613 const struct st_common_variant_key
*key
)
615 struct st_common_variant
*vpv
= CALLOC_STRUCT(st_common_variant
);
616 struct pipe_context
*pipe
= st
->pipe
;
617 struct pipe_screen
*screen
= pipe
->screen
;
618 struct pipe_shader_state state
= {0};
620 static const gl_state_index16 point_size_state
[STATE_LENGTH
] =
621 { STATE_INTERNAL
, STATE_POINT_SIZE_CLAMPED
, 0 };
622 struct gl_program_parameter_list
*params
= stvp
->Base
.Parameters
;
626 state
.stream_output
= stvp
->state
.stream_output
;
628 if (stvp
->state
.type
== PIPE_SHADER_IR_NIR
&&
629 (!key
->is_draw_shader
|| draw_has_llvm())) {
630 bool finalize
= false;
632 state
.type
= PIPE_SHADER_IR_NIR
;
633 state
.ir
.nir
= nir_shader_clone(NULL
, stvp
->Base
.nir
);
634 if (key
->clamp_color
) {
635 NIR_PASS_V(state
.ir
.nir
, nir_lower_clamp_color_outputs
);
638 if (key
->passthrough_edgeflags
) {
639 NIR_PASS_V(state
.ir
.nir
, nir_lower_passthrough_edgeflags
);
643 if (key
->lower_point_size
) {
644 _mesa_add_state_reference(params
, point_size_state
);
645 NIR_PASS_V(state
.ir
.nir
, nir_lower_point_size_mov
,
650 if (key
->lower_ucp
) {
651 bool can_compact
= screen
->get_param(screen
,
652 PIPE_CAP_NIR_COMPACT_ARRAYS
);
654 bool use_eye
= st
->ctx
->_Shader
->CurrentProgram
[MESA_SHADER_VERTEX
] != NULL
;
655 gl_state_index16 clipplane_state
[MAX_CLIP_PLANES
][STATE_LENGTH
];
656 for (int i
= 0; i
< MAX_CLIP_PLANES
; ++i
) {
658 clipplane_state
[i
][0] = STATE_CLIPPLANE
;
659 clipplane_state
[i
][1] = i
;
661 clipplane_state
[i
][0] = STATE_INTERNAL
;
662 clipplane_state
[i
][1] = STATE_CLIP_INTERNAL
;
663 clipplane_state
[i
][2] = i
;
665 _mesa_add_state_reference(params
, clipplane_state
[i
]);
668 NIR_PASS_V(state
.ir
.nir
, nir_lower_clip_vs
, key
->lower_ucp
,
669 true, can_compact
, clipplane_state
);
670 NIR_PASS_V(state
.ir
.nir
, nir_lower_io_to_temporaries
,
671 nir_shader_get_entrypoint(state
.ir
.nir
), true, false);
672 NIR_PASS_V(state
.ir
.nir
, nir_lower_global_vars_to_local
);
676 if (finalize
|| !st
->allow_st_finalize_nir_twice
) {
677 st_finalize_nir(st
, &stvp
->Base
, stvp
->shader_program
, state
.ir
.nir
,
680 /* Some of the lowering above may have introduced new varyings */
681 nir_shader_gather_info(state
.ir
.nir
,
682 nir_shader_get_entrypoint(state
.ir
.nir
));
685 if (ST_DEBUG
& DEBUG_PRINT_IR
)
686 nir_print_shader(state
.ir
.nir
, stderr
);
688 if (key
->is_draw_shader
)
689 vpv
->base
.driver_shader
= draw_create_vertex_shader(st
->draw
, &state
);
691 vpv
->base
.driver_shader
= pipe
->create_vs_state(pipe
, &state
);
696 state
.type
= PIPE_SHADER_IR_TGSI
;
697 state
.tokens
= tgsi_dup_tokens(stvp
->state
.tokens
);
699 /* Emulate features. */
700 if (key
->clamp_color
|| key
->passthrough_edgeflags
) {
701 const struct tgsi_token
*tokens
;
703 (key
->clamp_color
? TGSI_EMU_CLAMP_COLOR_OUTPUTS
: 0) |
704 (key
->passthrough_edgeflags
? TGSI_EMU_PASSTHROUGH_EDGEFLAG
: 0);
706 tokens
= tgsi_emulate(state
.tokens
, flags
);
709 tgsi_free_tokens(state
.tokens
);
710 state
.tokens
= tokens
;
712 fprintf(stderr
, "mesa: cannot emulate deprecated features\n");
716 if (key
->lower_depth_clamp
) {
717 unsigned depth_range_const
=
718 _mesa_add_state_reference(params
, depth_range_state
);
720 const struct tgsi_token
*tokens
;
721 tokens
= st_tgsi_lower_depth_clamp(state
.tokens
, depth_range_const
,
722 key
->clip_negative_one_to_one
);
723 if (tokens
!= state
.tokens
)
724 tgsi_free_tokens(state
.tokens
);
725 state
.tokens
= tokens
;
728 if (ST_DEBUG
& DEBUG_PRINT_IR
)
729 tgsi_dump(state
.tokens
, 0);
731 if (key
->is_draw_shader
)
732 vpv
->base
.driver_shader
= draw_create_vertex_shader(st
->draw
, &state
);
734 vpv
->base
.driver_shader
= pipe
->create_vs_state(pipe
, &state
);
737 tgsi_free_tokens(state
.tokens
);
745 * Find/create a vertex program variant.
747 struct st_common_variant
*
748 st_get_vp_variant(struct st_context
*st
,
749 struct st_program
*stp
,
750 const struct st_common_variant_key
*key
)
752 struct st_vertex_program
*stvp
= (struct st_vertex_program
*)stp
;
753 struct st_common_variant
*vpv
;
755 /* Search for existing variant */
756 for (vpv
= st_common_variant(stp
->variants
); vpv
;
757 vpv
= st_common_variant(vpv
->base
.next
)) {
758 if (memcmp(&vpv
->key
, key
, sizeof(*key
)) == 0) {
765 vpv
= st_create_vp_variant(st
, stp
, key
);
767 vpv
->base
.st
= key
->st
;
769 unsigned num_inputs
= stvp
->num_inputs
+ key
->passthrough_edgeflags
;
770 for (unsigned index
= 0; index
< num_inputs
; ++index
) {
771 unsigned attr
= stvp
->index_to_input
[index
];
772 if (attr
== ST_DOUBLE_ATTRIB_PLACEHOLDER
)
774 vpv
->vert_attrib_mask
|= 1u << attr
;
777 /* insert into list */
778 vpv
->base
.next
= stp
->variants
;
779 stp
->variants
= &vpv
->base
;
788 * Translate a Mesa fragment shader into a TGSI shader.
791 st_translate_fragment_program(struct st_context
*st
,
792 struct st_program
*stfp
)
794 /* Non-GLSL programs: */
795 if (!stfp
->glsl_to_tgsi
) {
796 _mesa_remove_output_reads(&stfp
->Base
, PROGRAM_OUTPUT
);
797 if (st
->ctx
->Const
.GLSLFragCoordIsSysVal
)
798 _mesa_program_fragment_position_to_sysval(&stfp
->Base
);
800 /* This determines which states will be updated when the assembly
803 * fragment.position and glDrawPixels always use constants.
805 stfp
->affected_states
= ST_NEW_FS_STATE
|
806 ST_NEW_SAMPLE_SHADING
|
810 /* Just set them for ATI_fs unconditionally. */
811 stfp
->affected_states
|= ST_NEW_FS_SAMPLER_VIEWS
|
815 if (stfp
->Base
.SamplersUsed
)
816 stfp
->affected_states
|= ST_NEW_FS_SAMPLER_VIEWS
|
820 /* Translate to NIR. */
822 st
->pipe
->screen
->get_shader_param(st
->pipe
->screen
,
823 PIPE_SHADER_FRAGMENT
,
824 PIPE_SHADER_CAP_PREFERRED_IR
)) {
826 st_translate_prog_to_nir(st
, &stfp
->Base
, MESA_SHADER_FRAGMENT
);
829 ralloc_free(stfp
->Base
.nir
);
830 stfp
->state
.type
= PIPE_SHADER_IR_NIR
;
831 stfp
->Base
.nir
= nir
;
836 ubyte outputMapping
[2 * FRAG_RESULT_MAX
];
837 ubyte inputMapping
[VARYING_SLOT_MAX
];
838 ubyte inputSlotToAttr
[VARYING_SLOT_MAX
];
839 ubyte interpMode
[PIPE_MAX_SHADER_INPUTS
]; /* XXX size? */
841 GLbitfield64 inputsRead
;
842 struct ureg_program
*ureg
;
844 GLboolean write_all
= GL_FALSE
;
846 ubyte input_semantic_name
[PIPE_MAX_SHADER_INPUTS
];
847 ubyte input_semantic_index
[PIPE_MAX_SHADER_INPUTS
];
848 uint fs_num_inputs
= 0;
850 ubyte fs_output_semantic_name
[PIPE_MAX_SHADER_OUTPUTS
];
851 ubyte fs_output_semantic_index
[PIPE_MAX_SHADER_OUTPUTS
];
852 uint fs_num_outputs
= 0;
854 memset(inputSlotToAttr
, ~0, sizeof(inputSlotToAttr
));
857 * Convert Mesa program inputs to TGSI input register semantics.
859 inputsRead
= stfp
->Base
.info
.inputs_read
;
860 for (attr
= 0; attr
< VARYING_SLOT_MAX
; attr
++) {
861 if ((inputsRead
& BITFIELD64_BIT(attr
)) != 0) {
862 const GLuint slot
= fs_num_inputs
++;
864 inputMapping
[attr
] = slot
;
865 inputSlotToAttr
[slot
] = attr
;
868 case VARYING_SLOT_POS
:
869 input_semantic_name
[slot
] = TGSI_SEMANTIC_POSITION
;
870 input_semantic_index
[slot
] = 0;
871 interpMode
[slot
] = TGSI_INTERPOLATE_LINEAR
;
873 case VARYING_SLOT_COL0
:
874 input_semantic_name
[slot
] = TGSI_SEMANTIC_COLOR
;
875 input_semantic_index
[slot
] = 0;
876 interpMode
[slot
] = stfp
->glsl_to_tgsi
?
877 TGSI_INTERPOLATE_COUNT
: TGSI_INTERPOLATE_COLOR
;
879 case VARYING_SLOT_COL1
:
880 input_semantic_name
[slot
] = TGSI_SEMANTIC_COLOR
;
881 input_semantic_index
[slot
] = 1;
882 interpMode
[slot
] = stfp
->glsl_to_tgsi
?
883 TGSI_INTERPOLATE_COUNT
: TGSI_INTERPOLATE_COLOR
;
885 case VARYING_SLOT_FOGC
:
886 input_semantic_name
[slot
] = TGSI_SEMANTIC_FOG
;
887 input_semantic_index
[slot
] = 0;
888 interpMode
[slot
] = TGSI_INTERPOLATE_PERSPECTIVE
;
890 case VARYING_SLOT_FACE
:
891 input_semantic_name
[slot
] = TGSI_SEMANTIC_FACE
;
892 input_semantic_index
[slot
] = 0;
893 interpMode
[slot
] = TGSI_INTERPOLATE_CONSTANT
;
895 case VARYING_SLOT_PRIMITIVE_ID
:
896 input_semantic_name
[slot
] = TGSI_SEMANTIC_PRIMID
;
897 input_semantic_index
[slot
] = 0;
898 interpMode
[slot
] = TGSI_INTERPOLATE_CONSTANT
;
900 case VARYING_SLOT_LAYER
:
901 input_semantic_name
[slot
] = TGSI_SEMANTIC_LAYER
;
902 input_semantic_index
[slot
] = 0;
903 interpMode
[slot
] = TGSI_INTERPOLATE_CONSTANT
;
905 case VARYING_SLOT_VIEWPORT
:
906 input_semantic_name
[slot
] = TGSI_SEMANTIC_VIEWPORT_INDEX
;
907 input_semantic_index
[slot
] = 0;
908 interpMode
[slot
] = TGSI_INTERPOLATE_CONSTANT
;
910 case VARYING_SLOT_CLIP_DIST0
:
911 input_semantic_name
[slot
] = TGSI_SEMANTIC_CLIPDIST
;
912 input_semantic_index
[slot
] = 0;
913 interpMode
[slot
] = TGSI_INTERPOLATE_PERSPECTIVE
;
915 case VARYING_SLOT_CLIP_DIST1
:
916 input_semantic_name
[slot
] = TGSI_SEMANTIC_CLIPDIST
;
917 input_semantic_index
[slot
] = 1;
918 interpMode
[slot
] = TGSI_INTERPOLATE_PERSPECTIVE
;
920 case VARYING_SLOT_CULL_DIST0
:
921 case VARYING_SLOT_CULL_DIST1
:
922 /* these should have been lowered by GLSL */
925 /* In most cases, there is nothing special about these
926 * inputs, so adopt a convention to use the generic
927 * semantic name and the mesa VARYING_SLOT_ number as the
930 * All that is required is that the vertex shader labels
931 * its own outputs similarly, and that the vertex shader
932 * generates at least every output required by the
933 * fragment shader plus fixed-function hardware (such as
936 * However, some drivers may need us to identify the PNTC and TEXi
937 * varyings if, for example, their capability to replace them with
938 * sprite coordinates is limited.
940 case VARYING_SLOT_PNTC
:
941 if (st
->needs_texcoord_semantic
) {
942 input_semantic_name
[slot
] = TGSI_SEMANTIC_PCOORD
;
943 input_semantic_index
[slot
] = 0;
944 interpMode
[slot
] = TGSI_INTERPOLATE_LINEAR
;
948 case VARYING_SLOT_TEX0
:
949 case VARYING_SLOT_TEX1
:
950 case VARYING_SLOT_TEX2
:
951 case VARYING_SLOT_TEX3
:
952 case VARYING_SLOT_TEX4
:
953 case VARYING_SLOT_TEX5
:
954 case VARYING_SLOT_TEX6
:
955 case VARYING_SLOT_TEX7
:
956 if (st
->needs_texcoord_semantic
) {
957 input_semantic_name
[slot
] = TGSI_SEMANTIC_TEXCOORD
;
958 input_semantic_index
[slot
] = attr
- VARYING_SLOT_TEX0
;
959 interpMode
[slot
] = stfp
->glsl_to_tgsi
?
960 TGSI_INTERPOLATE_COUNT
: TGSI_INTERPOLATE_PERSPECTIVE
;
964 case VARYING_SLOT_VAR0
:
966 /* Semantic indices should be zero-based because drivers may choose
967 * to assign a fixed slot determined by that index.
968 * This is useful because ARB_separate_shader_objects uses location
969 * qualifiers for linkage, and if the semantic index corresponds to
970 * these locations, linkage passes in the driver become unecessary.
972 * If needs_texcoord_semantic is true, no semantic indices will be
973 * consumed for the TEXi varyings, and we can base the locations of
974 * the user varyings on VAR0. Otherwise, we use TEX0 as base index.
976 assert(attr
>= VARYING_SLOT_VAR0
|| attr
== VARYING_SLOT_PNTC
||
977 (attr
>= VARYING_SLOT_TEX0
&& attr
<= VARYING_SLOT_TEX7
));
978 input_semantic_name
[slot
] = TGSI_SEMANTIC_GENERIC
;
979 input_semantic_index
[slot
] = st_get_generic_varying_index(st
, attr
);
980 if (attr
== VARYING_SLOT_PNTC
)
981 interpMode
[slot
] = TGSI_INTERPOLATE_LINEAR
;
983 interpMode
[slot
] = stfp
->glsl_to_tgsi
?
984 TGSI_INTERPOLATE_COUNT
: TGSI_INTERPOLATE_PERSPECTIVE
;
990 inputMapping
[attr
] = -1;
995 * Semantics and mapping for outputs
997 GLbitfield64 outputsWritten
= stfp
->Base
.info
.outputs_written
;
999 /* if z is written, emit that first */
1000 if (outputsWritten
& BITFIELD64_BIT(FRAG_RESULT_DEPTH
)) {
1001 fs_output_semantic_name
[fs_num_outputs
] = TGSI_SEMANTIC_POSITION
;
1002 fs_output_semantic_index
[fs_num_outputs
] = 0;
1003 outputMapping
[FRAG_RESULT_DEPTH
] = fs_num_outputs
;
1005 outputsWritten
&= ~(1 << FRAG_RESULT_DEPTH
);
1008 if (outputsWritten
& BITFIELD64_BIT(FRAG_RESULT_STENCIL
)) {
1009 fs_output_semantic_name
[fs_num_outputs
] = TGSI_SEMANTIC_STENCIL
;
1010 fs_output_semantic_index
[fs_num_outputs
] = 0;
1011 outputMapping
[FRAG_RESULT_STENCIL
] = fs_num_outputs
;
1013 outputsWritten
&= ~(1 << FRAG_RESULT_STENCIL
);
1016 if (outputsWritten
& BITFIELD64_BIT(FRAG_RESULT_SAMPLE_MASK
)) {
1017 fs_output_semantic_name
[fs_num_outputs
] = TGSI_SEMANTIC_SAMPLEMASK
;
1018 fs_output_semantic_index
[fs_num_outputs
] = 0;
1019 outputMapping
[FRAG_RESULT_SAMPLE_MASK
] = fs_num_outputs
;
1021 outputsWritten
&= ~(1 << FRAG_RESULT_SAMPLE_MASK
);
1024 /* handle remaining outputs (color) */
1025 for (attr
= 0; attr
< ARRAY_SIZE(outputMapping
); attr
++) {
1026 const GLbitfield64 written
= attr
< FRAG_RESULT_MAX
? outputsWritten
:
1027 stfp
->Base
.SecondaryOutputsWritten
;
1028 const unsigned loc
= attr
% FRAG_RESULT_MAX
;
1030 if (written
& BITFIELD64_BIT(loc
)) {
1032 case FRAG_RESULT_DEPTH
:
1033 case FRAG_RESULT_STENCIL
:
1034 case FRAG_RESULT_SAMPLE_MASK
:
1038 case FRAG_RESULT_COLOR
:
1039 write_all
= GL_TRUE
; /* fallthrough */
1042 assert(loc
== FRAG_RESULT_COLOR
||
1043 (FRAG_RESULT_DATA0
<= loc
&& loc
< FRAG_RESULT_MAX
));
1045 index
= (loc
== FRAG_RESULT_COLOR
) ? 0 : (loc
- FRAG_RESULT_DATA0
);
1047 if (attr
>= FRAG_RESULT_MAX
) {
1048 /* Secondary color for dual source blending. */
1053 fs_output_semantic_name
[fs_num_outputs
] = TGSI_SEMANTIC_COLOR
;
1054 fs_output_semantic_index
[fs_num_outputs
] = index
;
1055 outputMapping
[attr
] = fs_num_outputs
;
1064 ureg
= ureg_create_with_screen(PIPE_SHADER_FRAGMENT
, st
->pipe
->screen
);
1068 if (ST_DEBUG
& DEBUG_MESA
) {
1069 _mesa_print_program(&stfp
->Base
);
1070 _mesa_print_program_parameters(st
->ctx
, &stfp
->Base
);
1073 if (write_all
== GL_TRUE
)
1074 ureg_property(ureg
, TGSI_PROPERTY_FS_COLOR0_WRITES_ALL_CBUFS
, 1);
1076 if (stfp
->Base
.info
.fs
.depth_layout
!= FRAG_DEPTH_LAYOUT_NONE
) {
1077 switch (stfp
->Base
.info
.fs
.depth_layout
) {
1078 case FRAG_DEPTH_LAYOUT_ANY
:
1079 ureg_property(ureg
, TGSI_PROPERTY_FS_DEPTH_LAYOUT
,
1080 TGSI_FS_DEPTH_LAYOUT_ANY
);
1082 case FRAG_DEPTH_LAYOUT_GREATER
:
1083 ureg_property(ureg
, TGSI_PROPERTY_FS_DEPTH_LAYOUT
,
1084 TGSI_FS_DEPTH_LAYOUT_GREATER
);
1086 case FRAG_DEPTH_LAYOUT_LESS
:
1087 ureg_property(ureg
, TGSI_PROPERTY_FS_DEPTH_LAYOUT
,
1088 TGSI_FS_DEPTH_LAYOUT_LESS
);
1090 case FRAG_DEPTH_LAYOUT_UNCHANGED
:
1091 ureg_property(ureg
, TGSI_PROPERTY_FS_DEPTH_LAYOUT
,
1092 TGSI_FS_DEPTH_LAYOUT_UNCHANGED
);
1099 if (stfp
->glsl_to_tgsi
) {
1100 st_translate_program(st
->ctx
,
1101 PIPE_SHADER_FRAGMENT
,
1109 input_semantic_name
,
1110 input_semantic_index
,
1115 fs_output_semantic_name
,
1116 fs_output_semantic_index
);
1118 free_glsl_to_tgsi_visitor(stfp
->glsl_to_tgsi
);
1119 } else if (stfp
->ati_fs
)
1120 st_translate_atifs_program(ureg
,
1126 input_semantic_name
,
1127 input_semantic_index
,
1132 fs_output_semantic_name
,
1133 fs_output_semantic_index
);
1135 st_translate_mesa_program(st
->ctx
,
1136 PIPE_SHADER_FRAGMENT
,
1142 input_semantic_name
,
1143 input_semantic_index
,
1148 fs_output_semantic_name
,
1149 fs_output_semantic_index
);
1151 stfp
->state
.tokens
= ureg_get_tokens(ureg
, NULL
);
1154 if (stfp
->glsl_to_tgsi
) {
1155 stfp
->glsl_to_tgsi
= NULL
;
1156 st_store_ir_in_disk_cache(st
, &stfp
->Base
, false);
1159 return stfp
->state
.tokens
!= NULL
;
1162 static struct st_fp_variant
*
1163 st_create_fp_variant(struct st_context
*st
,
1164 struct st_program
*stfp
,
1165 const struct st_fp_variant_key
*key
)
1167 struct pipe_context
*pipe
= st
->pipe
;
1168 struct st_fp_variant
*variant
= CALLOC_STRUCT(st_fp_variant
);
1169 struct pipe_shader_state state
= {0};
1170 struct gl_program_parameter_list
*params
= stfp
->Base
.Parameters
;
1171 static const gl_state_index16 texcoord_state
[STATE_LENGTH
] =
1172 { STATE_INTERNAL
, STATE_CURRENT_ATTRIB
, VERT_ATTRIB_TEX0
};
1173 static const gl_state_index16 scale_state
[STATE_LENGTH
] =
1174 { STATE_INTERNAL
, STATE_PT_SCALE
};
1175 static const gl_state_index16 bias_state
[STATE_LENGTH
] =
1176 { STATE_INTERNAL
, STATE_PT_BIAS
};
1177 static const gl_state_index16 alpha_ref_state
[STATE_LENGTH
] =
1178 { STATE_INTERNAL
, STATE_ALPHA_REF
};
1183 if (stfp
->state
.type
== PIPE_SHADER_IR_NIR
) {
1184 bool finalize
= false;
1186 state
.type
= PIPE_SHADER_IR_NIR
;
1187 state
.ir
.nir
= nir_shader_clone(NULL
, stfp
->Base
.nir
);
1189 if (key
->clamp_color
) {
1190 NIR_PASS_V(state
.ir
.nir
, nir_lower_clamp_color_outputs
);
1194 if (key
->lower_flatshade
) {
1195 NIR_PASS_V(state
.ir
.nir
, nir_lower_flatshade
);
1199 if (key
->lower_alpha_func
!= COMPARE_FUNC_NEVER
) {
1200 _mesa_add_state_reference(params
, alpha_ref_state
);
1201 NIR_PASS_V(state
.ir
.nir
, nir_lower_alpha_test
, key
->lower_alpha_func
,
1202 false, alpha_ref_state
);
1206 if (key
->lower_two_sided_color
) {
1207 NIR_PASS_V(state
.ir
.nir
, nir_lower_two_sided_color
);
1211 if (key
->persample_shading
) {
1212 nir_shader
*shader
= state
.ir
.nir
;
1213 nir_foreach_variable(var
, &shader
->inputs
)
1214 var
->data
.sample
= true;
1218 assert(!(key
->bitmap
&& key
->drawpixels
));
1222 nir_lower_bitmap_options options
= {0};
1224 variant
->bitmap_sampler
= ffs(~stfp
->Base
.SamplersUsed
) - 1;
1225 options
.sampler
= variant
->bitmap_sampler
;
1226 options
.swizzle_xxxx
= st
->bitmap
.tex_format
== PIPE_FORMAT_R8_UNORM
;
1228 NIR_PASS_V(state
.ir
.nir
, nir_lower_bitmap
, &options
);
1232 /* glDrawPixels (color only) */
1233 if (key
->drawpixels
) {
1234 nir_lower_drawpixels_options options
= {{0}};
1235 unsigned samplers_used
= stfp
->Base
.SamplersUsed
;
1237 /* Find the first unused slot. */
1238 variant
->drawpix_sampler
= ffs(~samplers_used
) - 1;
1239 options
.drawpix_sampler
= variant
->drawpix_sampler
;
1240 samplers_used
|= (1 << variant
->drawpix_sampler
);
1242 options
.pixel_maps
= key
->pixelMaps
;
1243 if (key
->pixelMaps
) {
1244 variant
->pixelmap_sampler
= ffs(~samplers_used
) - 1;
1245 options
.pixelmap_sampler
= variant
->pixelmap_sampler
;
1248 options
.scale_and_bias
= key
->scaleAndBias
;
1249 if (key
->scaleAndBias
) {
1250 _mesa_add_state_reference(params
, scale_state
);
1251 memcpy(options
.scale_state_tokens
, scale_state
,
1252 sizeof(options
.scale_state_tokens
));
1253 _mesa_add_state_reference(params
, bias_state
);
1254 memcpy(options
.bias_state_tokens
, bias_state
,
1255 sizeof(options
.bias_state_tokens
));
1258 _mesa_add_state_reference(params
, texcoord_state
);
1259 memcpy(options
.texcoord_state_tokens
, texcoord_state
,
1260 sizeof(options
.texcoord_state_tokens
));
1262 NIR_PASS_V(state
.ir
.nir
, nir_lower_drawpixels
, &options
);
1266 if (unlikely(key
->external
.lower_nv12
|| key
->external
.lower_iyuv
||
1267 key
->external
.lower_xy_uxvx
|| key
->external
.lower_yx_xuxv
||
1268 key
->external
.lower_ayuv
|| key
->external
.lower_xyuv
)) {
1270 st_nir_lower_samplers(pipe
->screen
, state
.ir
.nir
,
1271 stfp
->shader_program
, &stfp
->Base
);
1273 nir_lower_tex_options options
= {0};
1274 options
.lower_y_uv_external
= key
->external
.lower_nv12
;
1275 options
.lower_y_u_v_external
= key
->external
.lower_iyuv
;
1276 options
.lower_xy_uxvx_external
= key
->external
.lower_xy_uxvx
;
1277 options
.lower_yx_xuxv_external
= key
->external
.lower_yx_xuxv
;
1278 options
.lower_ayuv_external
= key
->external
.lower_ayuv
;
1279 options
.lower_xyuv_external
= key
->external
.lower_xyuv
;
1280 NIR_PASS_V(state
.ir
.nir
, nir_lower_tex
, &options
);
1284 if (finalize
|| !st
->allow_st_finalize_nir_twice
) {
1285 st_finalize_nir(st
, &stfp
->Base
, stfp
->shader_program
, state
.ir
.nir
,
1289 /* This pass needs to happen *after* nir_lower_sampler */
1290 if (unlikely(key
->external
.lower_nv12
|| key
->external
.lower_iyuv
||
1291 key
->external
.lower_xy_uxvx
|| key
->external
.lower_yx_xuxv
||
1292 key
->external
.lower_ayuv
|| key
->external
.lower_xyuv
)) {
1293 NIR_PASS_V(state
.ir
.nir
, st_nir_lower_tex_src_plane
,
1294 ~stfp
->Base
.SamplersUsed
,
1295 key
->external
.lower_nv12
|| key
->external
.lower_xy_uxvx
||
1296 key
->external
.lower_yx_xuxv
,
1297 key
->external
.lower_iyuv
);
1301 if (finalize
|| !st
->allow_st_finalize_nir_twice
) {
1302 /* Some of the lowering above may have introduced new varyings */
1303 nir_shader_gather_info(state
.ir
.nir
,
1304 nir_shader_get_entrypoint(state
.ir
.nir
));
1306 struct pipe_screen
*screen
= pipe
->screen
;
1307 if (screen
->finalize_nir
)
1308 screen
->finalize_nir(screen
, state
.ir
.nir
, false);
1311 if (ST_DEBUG
& DEBUG_PRINT_IR
)
1312 nir_print_shader(state
.ir
.nir
, stderr
);
1314 variant
->base
.driver_shader
= pipe
->create_fs_state(pipe
, &state
);
1315 variant
->key
= *key
;
1320 state
.tokens
= stfp
->state
.tokens
;
1322 assert(!(key
->bitmap
&& key
->drawpixels
));
1324 /* Fix texture targets and add fog for ATI_fs */
1326 const struct tgsi_token
*tokens
= st_fixup_atifs(state
.tokens
, key
);
1329 state
.tokens
= tokens
;
1331 fprintf(stderr
, "mesa: cannot post-process ATI_fs\n");
1334 /* Emulate features. */
1335 if (key
->clamp_color
|| key
->persample_shading
) {
1336 const struct tgsi_token
*tokens
;
1338 (key
->clamp_color
? TGSI_EMU_CLAMP_COLOR_OUTPUTS
: 0) |
1339 (key
->persample_shading
? TGSI_EMU_FORCE_PERSAMPLE_INTERP
: 0);
1341 tokens
= tgsi_emulate(state
.tokens
, flags
);
1344 if (state
.tokens
!= stfp
->state
.tokens
)
1345 tgsi_free_tokens(state
.tokens
);
1346 state
.tokens
= tokens
;
1348 fprintf(stderr
, "mesa: cannot emulate deprecated features\n");
1353 const struct tgsi_token
*tokens
;
1355 variant
->bitmap_sampler
= ffs(~stfp
->Base
.SamplersUsed
) - 1;
1357 tokens
= st_get_bitmap_shader(state
.tokens
,
1358 st
->internal_target
,
1359 variant
->bitmap_sampler
,
1360 st
->needs_texcoord_semantic
,
1361 st
->bitmap
.tex_format
==
1362 PIPE_FORMAT_R8_UNORM
);
1365 if (state
.tokens
!= stfp
->state
.tokens
)
1366 tgsi_free_tokens(state
.tokens
);
1367 state
.tokens
= tokens
;
1369 fprintf(stderr
, "mesa: cannot create a shader for glBitmap\n");
1372 /* glDrawPixels (color only) */
1373 if (key
->drawpixels
) {
1374 const struct tgsi_token
*tokens
;
1375 unsigned scale_const
= 0, bias_const
= 0, texcoord_const
= 0;
1377 /* Find the first unused slot. */
1378 variant
->drawpix_sampler
= ffs(~stfp
->Base
.SamplersUsed
) - 1;
1380 if (key
->pixelMaps
) {
1381 unsigned samplers_used
= stfp
->Base
.SamplersUsed
|
1382 (1 << variant
->drawpix_sampler
);
1384 variant
->pixelmap_sampler
= ffs(~samplers_used
) - 1;
1387 if (key
->scaleAndBias
) {
1388 scale_const
= _mesa_add_state_reference(params
, scale_state
);
1389 bias_const
= _mesa_add_state_reference(params
, bias_state
);
1392 texcoord_const
= _mesa_add_state_reference(params
, texcoord_state
);
1394 tokens
= st_get_drawpix_shader(state
.tokens
,
1395 st
->needs_texcoord_semantic
,
1396 key
->scaleAndBias
, scale_const
,
1397 bias_const
, key
->pixelMaps
,
1398 variant
->drawpix_sampler
,
1399 variant
->pixelmap_sampler
,
1400 texcoord_const
, st
->internal_target
);
1403 if (state
.tokens
!= stfp
->state
.tokens
)
1404 tgsi_free_tokens(state
.tokens
);
1405 state
.tokens
= tokens
;
1407 fprintf(stderr
, "mesa: cannot create a shader for glDrawPixels\n");
1410 if (unlikely(key
->external
.lower_nv12
|| key
->external
.lower_iyuv
||
1411 key
->external
.lower_xy_uxvx
|| key
->external
.lower_yx_xuxv
)) {
1412 const struct tgsi_token
*tokens
;
1414 /* samplers inserted would conflict, but this should be unpossible: */
1415 assert(!(key
->bitmap
|| key
->drawpixels
));
1417 tokens
= st_tgsi_lower_yuv(state
.tokens
,
1418 ~stfp
->Base
.SamplersUsed
,
1419 key
->external
.lower_nv12
||
1420 key
->external
.lower_xy_uxvx
||
1421 key
->external
.lower_yx_xuxv
,
1422 key
->external
.lower_iyuv
);
1424 if (state
.tokens
!= stfp
->state
.tokens
)
1425 tgsi_free_tokens(state
.tokens
);
1426 state
.tokens
= tokens
;
1428 fprintf(stderr
, "mesa: cannot create a shader for samplerExternalOES\n");
1432 if (key
->lower_depth_clamp
) {
1433 unsigned depth_range_const
= _mesa_add_state_reference(params
, depth_range_state
);
1435 const struct tgsi_token
*tokens
;
1436 tokens
= st_tgsi_lower_depth_clamp_fs(state
.tokens
, depth_range_const
);
1437 if (state
.tokens
!= stfp
->state
.tokens
)
1438 tgsi_free_tokens(state
.tokens
);
1439 state
.tokens
= tokens
;
1442 if (ST_DEBUG
& DEBUG_PRINT_IR
)
1443 tgsi_dump(state
.tokens
, 0);
1445 /* fill in variant */
1446 variant
->base
.driver_shader
= pipe
->create_fs_state(pipe
, &state
);
1447 variant
->key
= *key
;
1449 if (state
.tokens
!= stfp
->state
.tokens
)
1450 tgsi_free_tokens(state
.tokens
);
1455 * Translate fragment program if needed.
1457 struct st_fp_variant
*
1458 st_get_fp_variant(struct st_context
*st
,
1459 struct st_program
*stfp
,
1460 const struct st_fp_variant_key
*key
)
1462 struct st_fp_variant
*fpv
;
1464 /* Search for existing variant */
1465 for (fpv
= st_fp_variant(stfp
->variants
); fpv
;
1466 fpv
= st_fp_variant(fpv
->base
.next
)) {
1467 if (memcmp(&fpv
->key
, key
, sizeof(*key
)) == 0) {
1474 fpv
= st_create_fp_variant(st
, stfp
, key
);
1476 fpv
->base
.st
= key
->st
;
1478 if (key
->bitmap
|| key
->drawpixels
) {
1479 /* Regular variants should always come before the
1480 * bitmap & drawpixels variants, (unless there
1481 * are no regular variants) so that
1482 * st_update_fp can take a fast path when
1483 * shader_has_one_variant is set.
1485 if (!stfp
->variants
) {
1486 stfp
->variants
= &fpv
->base
;
1488 /* insert into list after the first one */
1489 fpv
->base
.next
= stfp
->variants
->next
;
1490 stfp
->variants
->next
= &fpv
->base
;
1493 /* insert into list */
1494 fpv
->base
.next
= stfp
->variants
;
1495 stfp
->variants
= &fpv
->base
;
1504 * Translate a program. This is common code for geometry and tessellation
1508 st_translate_common_program(struct st_context
*st
,
1509 struct st_program
*stp
)
1511 struct gl_program
*prog
= &stp
->Base
;
1512 enum pipe_shader_type stage
=
1513 pipe_shader_type_from_mesa(stp
->Base
.info
.stage
);
1514 struct ureg_program
*ureg
= ureg_create_with_screen(stage
, st
->pipe
->screen
);
1520 case PIPE_SHADER_TESS_CTRL
:
1521 ureg_property(ureg
, TGSI_PROPERTY_TCS_VERTICES_OUT
,
1522 stp
->Base
.info
.tess
.tcs_vertices_out
);
1525 case PIPE_SHADER_TESS_EVAL
:
1526 if (stp
->Base
.info
.tess
.primitive_mode
== GL_ISOLINES
)
1527 ureg_property(ureg
, TGSI_PROPERTY_TES_PRIM_MODE
, GL_LINES
);
1529 ureg_property(ureg
, TGSI_PROPERTY_TES_PRIM_MODE
,
1530 stp
->Base
.info
.tess
.primitive_mode
);
1532 STATIC_ASSERT((TESS_SPACING_EQUAL
+ 1) % 3 == PIPE_TESS_SPACING_EQUAL
);
1533 STATIC_ASSERT((TESS_SPACING_FRACTIONAL_ODD
+ 1) % 3 ==
1534 PIPE_TESS_SPACING_FRACTIONAL_ODD
);
1535 STATIC_ASSERT((TESS_SPACING_FRACTIONAL_EVEN
+ 1) % 3 ==
1536 PIPE_TESS_SPACING_FRACTIONAL_EVEN
);
1538 ureg_property(ureg
, TGSI_PROPERTY_TES_SPACING
,
1539 (stp
->Base
.info
.tess
.spacing
+ 1) % 3);
1541 ureg_property(ureg
, TGSI_PROPERTY_TES_VERTEX_ORDER_CW
,
1542 !stp
->Base
.info
.tess
.ccw
);
1543 ureg_property(ureg
, TGSI_PROPERTY_TES_POINT_MODE
,
1544 stp
->Base
.info
.tess
.point_mode
);
1547 case PIPE_SHADER_GEOMETRY
:
1548 ureg_property(ureg
, TGSI_PROPERTY_GS_INPUT_PRIM
,
1549 stp
->Base
.info
.gs
.input_primitive
);
1550 ureg_property(ureg
, TGSI_PROPERTY_GS_OUTPUT_PRIM
,
1551 stp
->Base
.info
.gs
.output_primitive
);
1552 ureg_property(ureg
, TGSI_PROPERTY_GS_MAX_OUTPUT_VERTICES
,
1553 stp
->Base
.info
.gs
.vertices_out
);
1554 ureg_property(ureg
, TGSI_PROPERTY_GS_INVOCATIONS
,
1555 stp
->Base
.info
.gs
.invocations
);
1562 ubyte inputSlotToAttr
[VARYING_SLOT_TESS_MAX
];
1563 ubyte inputMapping
[VARYING_SLOT_TESS_MAX
];
1564 ubyte outputMapping
[VARYING_SLOT_TESS_MAX
];
1567 ubyte input_semantic_name
[PIPE_MAX_SHADER_INPUTS
];
1568 ubyte input_semantic_index
[PIPE_MAX_SHADER_INPUTS
];
1569 uint num_inputs
= 0;
1571 ubyte output_semantic_name
[PIPE_MAX_SHADER_OUTPUTS
];
1572 ubyte output_semantic_index
[PIPE_MAX_SHADER_OUTPUTS
];
1573 uint num_outputs
= 0;
1577 memset(inputSlotToAttr
, 0, sizeof(inputSlotToAttr
));
1578 memset(inputMapping
, 0, sizeof(inputMapping
));
1579 memset(outputMapping
, 0, sizeof(outputMapping
));
1580 memset(&stp
->state
, 0, sizeof(stp
->state
));
1582 if (prog
->info
.clip_distance_array_size
)
1583 ureg_property(ureg
, TGSI_PROPERTY_NUM_CLIPDIST_ENABLED
,
1584 prog
->info
.clip_distance_array_size
);
1585 if (prog
->info
.cull_distance_array_size
)
1586 ureg_property(ureg
, TGSI_PROPERTY_NUM_CULLDIST_ENABLED
,
1587 prog
->info
.cull_distance_array_size
);
1590 * Convert Mesa program inputs to TGSI input register semantics.
1592 for (attr
= 0; attr
< VARYING_SLOT_MAX
; attr
++) {
1593 if ((prog
->info
.inputs_read
& BITFIELD64_BIT(attr
)) == 0)
1596 unsigned slot
= num_inputs
++;
1598 inputMapping
[attr
] = slot
;
1599 inputSlotToAttr
[slot
] = attr
;
1601 unsigned semantic_name
, semantic_index
;
1602 tgsi_get_gl_varying_semantic(attr
, st
->needs_texcoord_semantic
,
1603 &semantic_name
, &semantic_index
);
1604 input_semantic_name
[slot
] = semantic_name
;
1605 input_semantic_index
[slot
] = semantic_index
;
1608 /* Also add patch inputs. */
1609 for (attr
= 0; attr
< 32; attr
++) {
1610 if (prog
->info
.patch_inputs_read
& (1u << attr
)) {
1611 GLuint slot
= num_inputs
++;
1612 GLuint patch_attr
= VARYING_SLOT_PATCH0
+ attr
;
1614 inputMapping
[patch_attr
] = slot
;
1615 inputSlotToAttr
[slot
] = patch_attr
;
1616 input_semantic_name
[slot
] = TGSI_SEMANTIC_PATCH
;
1617 input_semantic_index
[slot
] = attr
;
1621 /* initialize output semantics to defaults */
1622 for (i
= 0; i
< PIPE_MAX_SHADER_OUTPUTS
; i
++) {
1623 output_semantic_name
[i
] = TGSI_SEMANTIC_GENERIC
;
1624 output_semantic_index
[i
] = 0;
1628 * Determine number of outputs, the (default) output register
1629 * mapping and the semantic information for each output.
1631 for (attr
= 0; attr
< VARYING_SLOT_MAX
; attr
++) {
1632 if (prog
->info
.outputs_written
& BITFIELD64_BIT(attr
)) {
1633 GLuint slot
= num_outputs
++;
1635 outputMapping
[attr
] = slot
;
1637 unsigned semantic_name
, semantic_index
;
1638 tgsi_get_gl_varying_semantic(attr
, st
->needs_texcoord_semantic
,
1639 &semantic_name
, &semantic_index
);
1640 output_semantic_name
[slot
] = semantic_name
;
1641 output_semantic_index
[slot
] = semantic_index
;
1645 /* Also add patch outputs. */
1646 for (attr
= 0; attr
< 32; attr
++) {
1647 if (prog
->info
.patch_outputs_written
& (1u << attr
)) {
1648 GLuint slot
= num_outputs
++;
1649 GLuint patch_attr
= VARYING_SLOT_PATCH0
+ attr
;
1651 outputMapping
[patch_attr
] = slot
;
1652 output_semantic_name
[slot
] = TGSI_SEMANTIC_PATCH
;
1653 output_semantic_index
[slot
] = attr
;
1657 st_translate_program(st
->ctx
,
1666 input_semantic_name
,
1667 input_semantic_index
,
1672 output_semantic_name
,
1673 output_semantic_index
);
1675 stp
->state
.tokens
= ureg_get_tokens(ureg
, NULL
);
1679 st_translate_stream_output_info(prog
);
1681 st_store_ir_in_disk_cache(st
, prog
, false);
1683 if (ST_DEBUG
& DEBUG_PRINT_IR
&& ST_DEBUG
& DEBUG_MESA
)
1684 _mesa_print_program(prog
);
1686 free_glsl_to_tgsi_visitor(stp
->glsl_to_tgsi
);
1687 stp
->glsl_to_tgsi
= NULL
;
1693 * Get/create a basic program variant.
1696 st_get_common_variant(struct st_context
*st
,
1697 struct st_program
*prog
,
1698 const struct st_common_variant_key
*key
)
1700 struct pipe_context
*pipe
= st
->pipe
;
1701 struct st_variant
*v
;
1702 struct pipe_shader_state state
= {0};
1704 /* Search for existing variant */
1705 for (v
= prog
->variants
; v
; v
= v
->next
) {
1706 if (memcmp(&st_common_variant(v
)->key
, key
, sizeof(*key
)) == 0)
1712 v
= (struct st_variant
*)CALLOC_STRUCT(st_common_variant
);
1714 if (prog
->state
.type
== PIPE_SHADER_IR_NIR
) {
1715 bool finalize
= false;
1717 state
.type
= PIPE_SHADER_IR_NIR
;
1718 state
.ir
.nir
= nir_shader_clone(NULL
, prog
->Base
.nir
);
1720 if (key
->clamp_color
) {
1721 NIR_PASS_V(state
.ir
.nir
, nir_lower_clamp_color_outputs
);
1725 state
.stream_output
= prog
->state
.stream_output
;
1727 if (finalize
|| !st
->allow_st_finalize_nir_twice
) {
1728 st_finalize_nir(st
, &prog
->Base
, prog
->shader_program
,
1729 state
.ir
.nir
, true);
1732 if (ST_DEBUG
& DEBUG_PRINT_IR
)
1733 nir_print_shader(state
.ir
.nir
, stderr
);
1735 if (key
->lower_depth_clamp
) {
1736 struct gl_program_parameter_list
*params
= prog
->Base
.Parameters
;
1738 unsigned depth_range_const
=
1739 _mesa_add_state_reference(params
, depth_range_state
);
1741 const struct tgsi_token
*tokens
;
1743 st_tgsi_lower_depth_clamp(prog
->state
.tokens
,
1745 key
->clip_negative_one_to_one
);
1747 if (tokens
!= prog
->state
.tokens
)
1748 tgsi_free_tokens(prog
->state
.tokens
);
1750 prog
->state
.tokens
= tokens
;
1752 state
= prog
->state
;
1754 if (ST_DEBUG
& DEBUG_PRINT_IR
)
1755 tgsi_dump(state
.tokens
, 0);
1757 /* fill in new variant */
1758 switch (prog
->Base
.info
.stage
) {
1759 case MESA_SHADER_TESS_CTRL
:
1760 v
->driver_shader
= pipe
->create_tcs_state(pipe
, &state
);
1762 case MESA_SHADER_TESS_EVAL
:
1763 v
->driver_shader
= pipe
->create_tes_state(pipe
, &state
);
1765 case MESA_SHADER_GEOMETRY
:
1766 v
->driver_shader
= pipe
->create_gs_state(pipe
, &state
);
1768 case MESA_SHADER_COMPUTE
: {
1769 struct pipe_compute_state cs
= {0};
1770 cs
.ir_type
= state
.type
;
1771 cs
.req_local_mem
= prog
->Base
.info
.cs
.shared_size
;
1773 if (state
.type
== PIPE_SHADER_IR_NIR
)
1774 cs
.prog
= state
.ir
.nir
;
1776 cs
.prog
= state
.tokens
;
1778 v
->driver_shader
= pipe
->create_compute_state(pipe
, &cs
);
1782 assert(!"unhandled shader type");
1787 st_common_variant(v
)->key
= *key
;
1790 /* insert into list */
1791 v
->next
= prog
->variants
;
1801 * Vert/Geom/Frag programs have per-context variants. Free all the
1802 * variants attached to the given program which match the given context.
1805 destroy_program_variants(struct st_context
*st
, struct gl_program
*target
)
1807 if (!target
|| target
== &_mesa_DummyProgram
)
1810 struct st_program
*p
= st_program(target
);
1811 struct st_variant
*v
, **prevPtr
= &p
->variants
;
1812 bool unbound
= false;
1814 for (v
= p
->variants
; v
; ) {
1815 struct st_variant
*next
= v
->next
;
1818 st_unbind_program(st
, p
);
1822 /* unlink from list */
1824 /* destroy this variant */
1825 delete_variant(st
, v
, target
->Target
);
1836 * Callback for _mesa_HashWalk. Free all the shader's program variants
1837 * which match the given context.
1840 destroy_shader_program_variants_cb(GLuint key
, void *data
, void *userData
)
1842 struct st_context
*st
= (struct st_context
*) userData
;
1843 struct gl_shader
*shader
= (struct gl_shader
*) data
;
1845 switch (shader
->Type
) {
1846 case GL_SHADER_PROGRAM_MESA
:
1848 struct gl_shader_program
*shProg
= (struct gl_shader_program
*) data
;
1851 for (i
= 0; i
< ARRAY_SIZE(shProg
->_LinkedShaders
); i
++) {
1852 if (shProg
->_LinkedShaders
[i
])
1853 destroy_program_variants(st
, shProg
->_LinkedShaders
[i
]->Program
);
1857 case GL_VERTEX_SHADER
:
1858 case GL_FRAGMENT_SHADER
:
1859 case GL_GEOMETRY_SHADER
:
1860 case GL_TESS_CONTROL_SHADER
:
1861 case GL_TESS_EVALUATION_SHADER
:
1862 case GL_COMPUTE_SHADER
:
1871 * Callback for _mesa_HashWalk. Free all the program variants which match
1872 * the given context.
1875 destroy_program_variants_cb(GLuint key
, void *data
, void *userData
)
1877 struct st_context
*st
= (struct st_context
*) userData
;
1878 struct gl_program
*program
= (struct gl_program
*) data
;
1879 destroy_program_variants(st
, program
);
1884 * Walk over all shaders and programs to delete any variants which
1885 * belong to the given context.
1886 * This is called during context tear-down.
1889 st_destroy_program_variants(struct st_context
*st
)
1891 /* If shaders can be shared with other contexts, the last context will
1892 * call DeleteProgram on all shaders, releasing everything.
1894 if (st
->has_shareable_shaders
)
1897 /* ARB vert/frag program */
1898 _mesa_HashWalk(st
->ctx
->Shared
->Programs
,
1899 destroy_program_variants_cb
, st
);
1901 /* GLSL vert/frag/geom shaders */
1902 _mesa_HashWalk(st
->ctx
->Shared
->ShaderObjects
,
1903 destroy_shader_program_variants_cb
, st
);
1908 * Compile one shader variant.
1911 st_precompile_shader_variant(struct st_context
*st
,
1912 struct gl_program
*prog
)
1914 switch (prog
->Target
) {
1915 case GL_VERTEX_PROGRAM_ARB
: {
1916 struct st_program
*p
= (struct st_program
*)prog
;
1917 struct st_common_variant_key key
;
1919 memset(&key
, 0, sizeof(key
));
1921 key
.st
= st
->has_shareable_shaders
? NULL
: st
;
1922 st_get_vp_variant(st
, p
, &key
);
1926 case GL_FRAGMENT_PROGRAM_ARB
: {
1927 struct st_program
*p
= (struct st_program
*)prog
;
1928 struct st_fp_variant_key key
;
1930 memset(&key
, 0, sizeof(key
));
1932 key
.st
= st
->has_shareable_shaders
? NULL
: st
;
1933 st_get_fp_variant(st
, p
, &key
);
1937 case GL_TESS_CONTROL_PROGRAM_NV
:
1938 case GL_TESS_EVALUATION_PROGRAM_NV
:
1939 case GL_GEOMETRY_PROGRAM_NV
:
1940 case GL_COMPUTE_PROGRAM_NV
: {
1941 struct st_program
*p
= st_program(prog
);
1942 struct st_common_variant_key key
;
1944 memset(&key
, 0, sizeof(key
));
1946 key
.st
= st
->has_shareable_shaders
? NULL
: st
;
1947 st_get_common_variant(st
, p
, &key
);
1957 st_finalize_program(struct st_context
*st
, struct gl_program
*prog
)
1959 if (st
->current_program
[prog
->info
.stage
] == prog
) {
1960 if (prog
->info
.stage
== MESA_SHADER_VERTEX
)
1961 st
->dirty
|= ST_NEW_VERTEX_PROGRAM(st
, (struct st_program
*)prog
);
1963 st
->dirty
|= ((struct st_program
*)prog
)->affected_states
;
1967 nir_sweep(prog
->nir
);
1969 /* Create Gallium shaders now instead of on demand. */
1970 if (ST_DEBUG
& DEBUG_PRECOMPILE
||
1971 st
->shader_has_one_variant
[prog
->info
.stage
])
1972 st_precompile_shader_variant(st
, prog
);