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");
213 * Delete a shader variant. Note the caller must unlink the variant from
217 delete_variant(struct st_context
*st
, struct st_variant
*v
, GLenum target
)
219 if (v
->driver_shader
) {
220 if (st
->has_shareable_shaders
|| v
->st
== st
) {
221 /* The shader's context matches the calling context, or we
225 case GL_VERTEX_PROGRAM_ARB
:
226 cso_delete_vertex_shader(st
->cso_context
, v
->driver_shader
);
228 case GL_TESS_CONTROL_PROGRAM_NV
:
229 cso_delete_tessctrl_shader(st
->cso_context
, v
->driver_shader
);
231 case GL_TESS_EVALUATION_PROGRAM_NV
:
232 cso_delete_tesseval_shader(st
->cso_context
, v
->driver_shader
);
234 case GL_GEOMETRY_PROGRAM_NV
:
235 cso_delete_geometry_shader(st
->cso_context
, v
->driver_shader
);
237 case GL_FRAGMENT_PROGRAM_ARB
:
238 cso_delete_fragment_shader(st
->cso_context
, v
->driver_shader
);
240 case GL_COMPUTE_PROGRAM_NV
:
241 cso_delete_compute_shader(st
->cso_context
, v
->driver_shader
);
244 unreachable("bad shader type in delete_basic_variant");
247 /* We can't delete a shader with a context different from the one
248 * that created it. Add it to the creating context's zombie list.
250 enum pipe_shader_type type
=
251 pipe_shader_type_from_mesa(_mesa_program_enum_to_shader_stage(target
));
253 st_save_zombie_shader(v
->st
, type
, v
->driver_shader
);
257 if (target
== GL_VERTEX_PROGRAM_ARB
) {
258 struct st_vp_variant
*vpv
= (struct st_vp_variant
*)v
;
260 if (vpv
->draw_shader
)
261 draw_delete_vertex_shader( st
->draw
, vpv
->draw_shader
);
264 ureg_free_tokens(vpv
->tokens
);
272 * Free all basic program variants.
275 st_release_variants(struct st_context
*st
, struct st_program
*p
)
277 struct st_variant
*v
;
279 for (v
= p
->variants
; v
; ) {
280 struct st_variant
*next
= v
->next
;
281 delete_variant(st
, v
, p
->Base
.Target
);
287 if (p
->state
.tokens
) {
288 ureg_free_tokens(p
->state
.tokens
);
289 p
->state
.tokens
= NULL
;
292 /* Note: Any setup of ->ir.nir that has had pipe->create_*_state called on
293 * it has resulted in the driver taking ownership of the NIR. Those
294 * callers should be NULLing out the nir field in any pipe_shader_state
295 * that might have this called in order to indicate that.
297 * GLSL IR and ARB programs will have set gl_program->nir to the same
298 * shader as ir->ir.nir, so it will be freed by _mesa_delete_program().
303 st_finalize_nir_before_variants(struct nir_shader
*nir
)
305 NIR_PASS_V(nir
, nir_opt_access
);
307 NIR_PASS_V(nir
, nir_split_var_copies
);
308 NIR_PASS_V(nir
, nir_lower_var_copies
);
309 if (nir
->options
->lower_all_io_to_temps
||
310 nir
->options
->lower_all_io_to_elements
||
311 nir
->info
.stage
== MESA_SHADER_VERTEX
||
312 nir
->info
.stage
== MESA_SHADER_GEOMETRY
) {
313 NIR_PASS_V(nir
, nir_lower_io_arrays_to_elements_no_indirects
, false);
314 } else if (nir
->info
.stage
== MESA_SHADER_FRAGMENT
) {
315 NIR_PASS_V(nir
, nir_lower_io_arrays_to_elements_no_indirects
, true);
318 st_nir_assign_vs_in_locations(nir
);
322 * Translate ARB (asm) program to NIR
325 st_translate_prog_to_nir(struct st_context
*st
, struct gl_program
*prog
,
326 gl_shader_stage stage
)
328 struct pipe_screen
*screen
= st
->pipe
->screen
;
329 const struct gl_shader_compiler_options
*options
=
330 &st
->ctx
->Const
.ShaderCompilerOptions
[stage
];
332 /* Translate to NIR */
333 nir_shader
*nir
= prog_to_nir(prog
, options
->NirOptions
);
334 NIR_PASS_V(nir
, nir_lower_regs_to_ssa
); /* turn registers into SSA */
335 nir_validate_shader(nir
, "after st/ptn lower_regs_to_ssa");
337 NIR_PASS_V(nir
, st_nir_lower_wpos_ytransform
, prog
, screen
);
338 NIR_PASS_V(nir
, nir_lower_system_values
);
341 NIR_PASS_V(nir
, nir_opt_constant_folding
);
343 st_finalize_nir_before_variants(nir
);
345 if (st
->allow_st_finalize_nir_twice
)
346 st_finalize_nir(st
, prog
, NULL
, nir
, true);
348 nir_validate_shader(nir
, "after st/glsl finalize_nir");
354 st_prepare_vertex_program(struct st_program
*stp
)
356 struct st_vertex_program
*stvp
= (struct st_vertex_program
*)stp
;
358 stvp
->num_inputs
= 0;
359 memset(stvp
->input_to_index
, ~0, sizeof(stvp
->input_to_index
));
360 memset(stvp
->result_to_output
, ~0, sizeof(stvp
->result_to_output
));
362 /* Determine number of inputs, the mappings between VERT_ATTRIB_x
363 * and TGSI generic input indexes, plus input attrib semantic info.
365 for (unsigned attr
= 0; attr
< VERT_ATTRIB_MAX
; attr
++) {
366 if ((stp
->Base
.info
.inputs_read
& BITFIELD64_BIT(attr
)) != 0) {
367 stvp
->input_to_index
[attr
] = stvp
->num_inputs
;
368 stvp
->index_to_input
[stvp
->num_inputs
] = attr
;
371 if ((stp
->Base
.DualSlotInputs
& BITFIELD64_BIT(attr
)) != 0) {
372 /* add placeholder for second part of a double attribute */
373 stvp
->index_to_input
[stvp
->num_inputs
] = ST_DOUBLE_ATTRIB_PLACEHOLDER
;
378 /* pre-setup potentially unused edgeflag input */
379 stvp
->input_to_index
[VERT_ATTRIB_EDGEFLAG
] = stvp
->num_inputs
;
380 stvp
->index_to_input
[stvp
->num_inputs
] = VERT_ATTRIB_EDGEFLAG
;
382 /* Compute mapping of vertex program outputs to slots. */
383 unsigned num_outputs
= 0;
384 for (unsigned attr
= 0; attr
< VARYING_SLOT_MAX
; attr
++) {
385 if (stp
->Base
.info
.outputs_written
& BITFIELD64_BIT(attr
))
386 stvp
->result_to_output
[attr
] = num_outputs
++;
388 /* pre-setup potentially unused edgeflag output */
389 stvp
->result_to_output
[VARYING_SLOT_EDGE
] = num_outputs
;
393 st_translate_stream_output_info(struct gl_program
*prog
)
395 struct gl_transform_feedback_info
*info
= prog
->sh
.LinkedTransformFeedback
;
399 /* Determine the (default) output register mapping for each output. */
400 unsigned num_outputs
= 0;
401 ubyte output_mapping
[VARYING_SLOT_TESS_MAX
];
402 memset(output_mapping
, 0, sizeof(output_mapping
));
404 for (unsigned attr
= 0; attr
< VARYING_SLOT_MAX
; attr
++) {
405 if (prog
->info
.outputs_written
& BITFIELD64_BIT(attr
))
406 output_mapping
[attr
] = num_outputs
++;
409 /* Translate stream output info. */
410 struct pipe_stream_output_info
*so_info
=
411 &((struct st_program
*)prog
)->state
.stream_output
;
413 for (unsigned i
= 0; i
< info
->NumOutputs
; i
++) {
414 so_info
->output
[i
].register_index
=
415 output_mapping
[info
->Outputs
[i
].OutputRegister
];
416 so_info
->output
[i
].start_component
= info
->Outputs
[i
].ComponentOffset
;
417 so_info
->output
[i
].num_components
= info
->Outputs
[i
].NumComponents
;
418 so_info
->output
[i
].output_buffer
= info
->Outputs
[i
].OutputBuffer
;
419 so_info
->output
[i
].dst_offset
= info
->Outputs
[i
].DstOffset
;
420 so_info
->output
[i
].stream
= info
->Outputs
[i
].StreamId
;
423 for (unsigned i
= 0; i
< PIPE_MAX_SO_BUFFERS
; i
++) {
424 so_info
->stride
[i
] = info
->Buffers
[i
].Stride
;
426 so_info
->num_outputs
= info
->NumOutputs
;
430 * Translate a vertex program.
433 st_translate_vertex_program(struct st_context
*st
,
434 struct st_program
*stp
)
436 struct ureg_program
*ureg
;
437 enum pipe_error error
;
438 unsigned num_outputs
= 0;
440 ubyte output_semantic_name
[VARYING_SLOT_MAX
] = {0};
441 ubyte output_semantic_index
[VARYING_SLOT_MAX
] = {0};
443 if (stp
->Base
.arb
.IsPositionInvariant
)
444 _mesa_insert_mvp_code(st
->ctx
, &stp
->Base
);
446 st_prepare_vertex_program(stp
);
449 if (!stp
->glsl_to_tgsi
) {
450 _mesa_remove_output_reads(&stp
->Base
, PROGRAM_OUTPUT
);
452 /* This determines which states will be updated when the assembly
455 stp
->affected_states
= ST_NEW_VS_STATE
|
457 ST_NEW_VERTEX_ARRAYS
;
459 if (stp
->Base
.Parameters
->NumParameters
)
460 stp
->affected_states
|= ST_NEW_VS_CONSTANTS
;
462 /* No samplers are allowed in ARB_vp. */
465 /* Get semantic names and indices. */
466 for (attr
= 0; attr
< VARYING_SLOT_MAX
; attr
++) {
467 if (stp
->Base
.info
.outputs_written
& BITFIELD64_BIT(attr
)) {
468 unsigned slot
= num_outputs
++;
469 unsigned semantic_name
, semantic_index
;
470 tgsi_get_gl_varying_semantic(attr
, st
->needs_texcoord_semantic
,
471 &semantic_name
, &semantic_index
);
472 output_semantic_name
[slot
] = semantic_name
;
473 output_semantic_index
[slot
] = semantic_index
;
476 /* pre-setup potentially unused edgeflag output */
477 output_semantic_name
[num_outputs
] = TGSI_SEMANTIC_EDGEFLAG
;
478 output_semantic_index
[num_outputs
] = 0;
480 ureg
= ureg_create_with_screen(PIPE_SHADER_VERTEX
, st
->pipe
->screen
);
484 if (stp
->Base
.info
.clip_distance_array_size
)
485 ureg_property(ureg
, TGSI_PROPERTY_NUM_CLIPDIST_ENABLED
,
486 stp
->Base
.info
.clip_distance_array_size
);
487 if (stp
->Base
.info
.cull_distance_array_size
)
488 ureg_property(ureg
, TGSI_PROPERTY_NUM_CULLDIST_ENABLED
,
489 stp
->Base
.info
.cull_distance_array_size
);
491 if (ST_DEBUG
& DEBUG_MESA
) {
492 _mesa_print_program(&stp
->Base
);
493 _mesa_print_program_parameters(st
->ctx
, &stp
->Base
);
497 struct st_vertex_program
*stvp
= (struct st_vertex_program
*)stp
;
499 if (stp
->glsl_to_tgsi
) {
500 error
= st_translate_program(st
->ctx
,
507 stvp
->input_to_index
,
508 NULL
, /* inputSlotToAttr */
509 NULL
, /* input semantic name */
510 NULL
, /* input semantic index */
511 NULL
, /* interp mode */
514 stvp
->result_to_output
,
515 output_semantic_name
,
516 output_semantic_index
);
518 st_translate_stream_output_info(&stp
->Base
);
520 free_glsl_to_tgsi_visitor(stp
->glsl_to_tgsi
);
522 error
= st_translate_mesa_program(st
->ctx
,
528 stvp
->input_to_index
,
529 NULL
, /* input semantic name */
530 NULL
, /* input semantic index */
534 stvp
->result_to_output
,
535 output_semantic_name
,
536 output_semantic_index
);
539 debug_printf("%s: failed to translate Mesa program:\n", __func__
);
540 _mesa_print_program(&stp
->Base
);
545 stp
->state
.tokens
= ureg_get_tokens(ureg
, NULL
);
548 if (stp
->glsl_to_tgsi
) {
549 stp
->glsl_to_tgsi
= NULL
;
550 st_store_ir_in_disk_cache(st
, &stp
->Base
, false);
555 * This must be done after the translation to TGSI is done, because
556 * we'll pass the NIR shader to the driver and the TGSI version to
557 * the draw module for the select/feedback/rasterpos code.
559 if (st
->pipe
->screen
->get_shader_param(st
->pipe
->screen
,
561 PIPE_SHADER_CAP_PREFERRED_IR
)) {
562 assert(!stp
->glsl_to_tgsi
);
565 st_translate_prog_to_nir(st
, &stp
->Base
, MESA_SHADER_VERTEX
);
568 ralloc_free(stp
->Base
.nir
);
569 stp
->state
.type
= PIPE_SHADER_IR_NIR
;
574 return stp
->state
.tokens
!= NULL
;
577 static const gl_state_index16 depth_range_state
[STATE_LENGTH
] =
578 { STATE_DEPTH_RANGE
};
580 static struct st_vp_variant
*
581 st_create_vp_variant(struct st_context
*st
,
582 struct st_program
*stvp
,
583 const struct st_common_variant_key
*key
)
585 struct st_vp_variant
*vpv
= CALLOC_STRUCT(st_vp_variant
);
586 struct pipe_context
*pipe
= st
->pipe
;
587 struct pipe_screen
*screen
= pipe
->screen
;
588 struct pipe_shader_state state
= {0};
590 static const gl_state_index16 point_size_state
[STATE_LENGTH
] =
591 { STATE_INTERNAL
, STATE_POINT_SIZE_CLAMPED
, 0 };
592 struct gl_program_parameter_list
*params
= stvp
->Base
.Parameters
;
595 vpv
->num_inputs
= ((struct st_vertex_program
*)stvp
)->num_inputs
;
597 state
.stream_output
= stvp
->state
.stream_output
;
599 if (stvp
->state
.type
== PIPE_SHADER_IR_NIR
) {
600 bool finalize
= false;
602 state
.type
= PIPE_SHADER_IR_NIR
;
603 state
.ir
.nir
= nir_shader_clone(NULL
, stvp
->Base
.nir
);
604 if (key
->clamp_color
) {
605 NIR_PASS_V(state
.ir
.nir
, nir_lower_clamp_color_outputs
);
608 if (key
->passthrough_edgeflags
) {
609 NIR_PASS_V(state
.ir
.nir
, nir_lower_passthrough_edgeflags
);
614 if (key
->lower_point_size
) {
615 _mesa_add_state_reference(params
, point_size_state
);
616 NIR_PASS_V(state
.ir
.nir
, nir_lower_point_size_mov
,
621 if (key
->lower_ucp
) {
622 bool can_compact
= screen
->get_param(screen
,
623 PIPE_CAP_NIR_COMPACT_ARRAYS
);
625 bool use_eye
= st
->ctx
->_Shader
->CurrentProgram
[MESA_SHADER_VERTEX
] != NULL
;
626 gl_state_index16 clipplane_state
[MAX_CLIP_PLANES
][STATE_LENGTH
];
627 for (int i
= 0; i
< MAX_CLIP_PLANES
; ++i
) {
629 clipplane_state
[i
][0] = STATE_CLIPPLANE
;
630 clipplane_state
[i
][1] = i
;
632 clipplane_state
[i
][0] = STATE_INTERNAL
;
633 clipplane_state
[i
][1] = STATE_CLIP_INTERNAL
;
634 clipplane_state
[i
][2] = i
;
636 _mesa_add_state_reference(params
, clipplane_state
[i
]);
639 NIR_PASS_V(state
.ir
.nir
, nir_lower_clip_vs
, key
->lower_ucp
,
640 true, can_compact
, clipplane_state
);
641 NIR_PASS_V(state
.ir
.nir
, nir_lower_io_to_temporaries
,
642 nir_shader_get_entrypoint(state
.ir
.nir
), true, false);
643 NIR_PASS_V(state
.ir
.nir
, nir_lower_global_vars_to_local
);
647 if (finalize
|| !st
->allow_st_finalize_nir_twice
) {
648 st_finalize_nir(st
, &stvp
->Base
, stvp
->shader_program
, state
.ir
.nir
,
651 /* Some of the lowering above may have introduced new varyings */
652 nir_shader_gather_info(state
.ir
.nir
,
653 nir_shader_get_entrypoint(state
.ir
.nir
));
656 if (ST_DEBUG
& DEBUG_PRINT_IR
)
657 nir_print_shader(state
.ir
.nir
, stderr
);
659 vpv
->base
.driver_shader
= pipe
->create_vs_state(pipe
, &state
);
661 /* When generating a NIR program, we usually don't have TGSI tokens.
662 * However, we do create them for ARB_vertex_program / fixed-function VS
663 * programs which we may need to use with the draw module for legacy
664 * feedback/select emulation. If they exist, copy them.
666 * TODO: Lowering for shader variants is not applied to TGSI when
667 * generating a NIR shader.
669 if (stvp
->state
.tokens
)
670 vpv
->tokens
= tgsi_dup_tokens(stvp
->state
.tokens
);
675 state
.type
= PIPE_SHADER_IR_TGSI
;
676 state
.tokens
= tgsi_dup_tokens(stvp
->state
.tokens
);
678 /* Emulate features. */
679 if (key
->clamp_color
|| key
->passthrough_edgeflags
) {
680 const struct tgsi_token
*tokens
;
682 (key
->clamp_color
? TGSI_EMU_CLAMP_COLOR_OUTPUTS
: 0) |
683 (key
->passthrough_edgeflags
? TGSI_EMU_PASSTHROUGH_EDGEFLAG
: 0);
685 tokens
= tgsi_emulate(state
.tokens
, flags
);
688 tgsi_free_tokens(state
.tokens
);
689 state
.tokens
= tokens
;
691 if (key
->passthrough_edgeflags
)
694 fprintf(stderr
, "mesa: cannot emulate deprecated features\n");
697 if (key
->lower_depth_clamp
) {
698 unsigned depth_range_const
=
699 _mesa_add_state_reference(params
, depth_range_state
);
701 const struct tgsi_token
*tokens
;
702 tokens
= st_tgsi_lower_depth_clamp(state
.tokens
, depth_range_const
,
703 key
->clip_negative_one_to_one
);
704 if (tokens
!= state
.tokens
)
705 tgsi_free_tokens(state
.tokens
);
706 state
.tokens
= tokens
;
709 if (ST_DEBUG
& DEBUG_PRINT_IR
)
710 tgsi_dump(state
.tokens
, 0);
712 vpv
->base
.driver_shader
= pipe
->create_vs_state(pipe
, &state
);
713 /* Save this for selection/feedback/rasterpos. */
714 vpv
->tokens
= state
.tokens
;
720 * Find/create a vertex program variant.
722 struct st_vp_variant
*
723 st_get_vp_variant(struct st_context
*st
,
724 struct st_program
*stp
,
725 const struct st_common_variant_key
*key
)
727 struct st_vertex_program
*stvp
= (struct st_vertex_program
*)stp
;
728 struct st_vp_variant
*vpv
;
730 /* Search for existing variant */
731 for (vpv
= st_vp_variant(stp
->variants
); vpv
;
732 vpv
= st_vp_variant(vpv
->base
.next
)) {
733 if (memcmp(&vpv
->key
, key
, sizeof(*key
)) == 0) {
740 vpv
= st_create_vp_variant(st
, stp
, key
);
742 vpv
->base
.st
= key
->st
;
744 for (unsigned index
= 0; index
< vpv
->num_inputs
; ++index
) {
745 unsigned attr
= stvp
->index_to_input
[index
];
746 if (attr
== ST_DOUBLE_ATTRIB_PLACEHOLDER
)
748 vpv
->vert_attrib_mask
|= 1u << attr
;
751 /* insert into list */
752 vpv
->base
.next
= stp
->variants
;
753 stp
->variants
= &vpv
->base
;
762 * Translate a Mesa fragment shader into a TGSI shader.
765 st_translate_fragment_program(struct st_context
*st
,
766 struct st_program
*stfp
)
768 /* Non-GLSL programs: */
769 if (!stfp
->glsl_to_tgsi
) {
770 _mesa_remove_output_reads(&stfp
->Base
, PROGRAM_OUTPUT
);
771 if (st
->ctx
->Const
.GLSLFragCoordIsSysVal
)
772 _mesa_program_fragment_position_to_sysval(&stfp
->Base
);
774 /* This determines which states will be updated when the assembly
777 * fragment.position and glDrawPixels always use constants.
779 stfp
->affected_states
= ST_NEW_FS_STATE
|
780 ST_NEW_SAMPLE_SHADING
|
784 /* Just set them for ATI_fs unconditionally. */
785 stfp
->affected_states
|= ST_NEW_FS_SAMPLER_VIEWS
|
789 if (stfp
->Base
.SamplersUsed
)
790 stfp
->affected_states
|= ST_NEW_FS_SAMPLER_VIEWS
|
794 /* Translate to NIR. */
796 st
->pipe
->screen
->get_shader_param(st
->pipe
->screen
,
797 PIPE_SHADER_FRAGMENT
,
798 PIPE_SHADER_CAP_PREFERRED_IR
)) {
800 st_translate_prog_to_nir(st
, &stfp
->Base
, MESA_SHADER_FRAGMENT
);
803 ralloc_free(stfp
->Base
.nir
);
804 stfp
->state
.type
= PIPE_SHADER_IR_NIR
;
805 stfp
->Base
.nir
= nir
;
810 ubyte outputMapping
[2 * FRAG_RESULT_MAX
];
811 ubyte inputMapping
[VARYING_SLOT_MAX
];
812 ubyte inputSlotToAttr
[VARYING_SLOT_MAX
];
813 ubyte interpMode
[PIPE_MAX_SHADER_INPUTS
]; /* XXX size? */
815 GLbitfield64 inputsRead
;
816 struct ureg_program
*ureg
;
818 GLboolean write_all
= GL_FALSE
;
820 ubyte input_semantic_name
[PIPE_MAX_SHADER_INPUTS
];
821 ubyte input_semantic_index
[PIPE_MAX_SHADER_INPUTS
];
822 uint fs_num_inputs
= 0;
824 ubyte fs_output_semantic_name
[PIPE_MAX_SHADER_OUTPUTS
];
825 ubyte fs_output_semantic_index
[PIPE_MAX_SHADER_OUTPUTS
];
826 uint fs_num_outputs
= 0;
828 memset(inputSlotToAttr
, ~0, sizeof(inputSlotToAttr
));
831 * Convert Mesa program inputs to TGSI input register semantics.
833 inputsRead
= stfp
->Base
.info
.inputs_read
;
834 for (attr
= 0; attr
< VARYING_SLOT_MAX
; attr
++) {
835 if ((inputsRead
& BITFIELD64_BIT(attr
)) != 0) {
836 const GLuint slot
= fs_num_inputs
++;
838 inputMapping
[attr
] = slot
;
839 inputSlotToAttr
[slot
] = attr
;
842 case VARYING_SLOT_POS
:
843 input_semantic_name
[slot
] = TGSI_SEMANTIC_POSITION
;
844 input_semantic_index
[slot
] = 0;
845 interpMode
[slot
] = TGSI_INTERPOLATE_LINEAR
;
847 case VARYING_SLOT_COL0
:
848 input_semantic_name
[slot
] = TGSI_SEMANTIC_COLOR
;
849 input_semantic_index
[slot
] = 0;
850 interpMode
[slot
] = stfp
->glsl_to_tgsi
?
851 TGSI_INTERPOLATE_COUNT
: TGSI_INTERPOLATE_COLOR
;
853 case VARYING_SLOT_COL1
:
854 input_semantic_name
[slot
] = TGSI_SEMANTIC_COLOR
;
855 input_semantic_index
[slot
] = 1;
856 interpMode
[slot
] = stfp
->glsl_to_tgsi
?
857 TGSI_INTERPOLATE_COUNT
: TGSI_INTERPOLATE_COLOR
;
859 case VARYING_SLOT_FOGC
:
860 input_semantic_name
[slot
] = TGSI_SEMANTIC_FOG
;
861 input_semantic_index
[slot
] = 0;
862 interpMode
[slot
] = TGSI_INTERPOLATE_PERSPECTIVE
;
864 case VARYING_SLOT_FACE
:
865 input_semantic_name
[slot
] = TGSI_SEMANTIC_FACE
;
866 input_semantic_index
[slot
] = 0;
867 interpMode
[slot
] = TGSI_INTERPOLATE_CONSTANT
;
869 case VARYING_SLOT_PRIMITIVE_ID
:
870 input_semantic_name
[slot
] = TGSI_SEMANTIC_PRIMID
;
871 input_semantic_index
[slot
] = 0;
872 interpMode
[slot
] = TGSI_INTERPOLATE_CONSTANT
;
874 case VARYING_SLOT_LAYER
:
875 input_semantic_name
[slot
] = TGSI_SEMANTIC_LAYER
;
876 input_semantic_index
[slot
] = 0;
877 interpMode
[slot
] = TGSI_INTERPOLATE_CONSTANT
;
879 case VARYING_SLOT_VIEWPORT
:
880 input_semantic_name
[slot
] = TGSI_SEMANTIC_VIEWPORT_INDEX
;
881 input_semantic_index
[slot
] = 0;
882 interpMode
[slot
] = TGSI_INTERPOLATE_CONSTANT
;
884 case VARYING_SLOT_CLIP_DIST0
:
885 input_semantic_name
[slot
] = TGSI_SEMANTIC_CLIPDIST
;
886 input_semantic_index
[slot
] = 0;
887 interpMode
[slot
] = TGSI_INTERPOLATE_PERSPECTIVE
;
889 case VARYING_SLOT_CLIP_DIST1
:
890 input_semantic_name
[slot
] = TGSI_SEMANTIC_CLIPDIST
;
891 input_semantic_index
[slot
] = 1;
892 interpMode
[slot
] = TGSI_INTERPOLATE_PERSPECTIVE
;
894 case VARYING_SLOT_CULL_DIST0
:
895 case VARYING_SLOT_CULL_DIST1
:
896 /* these should have been lowered by GLSL */
899 /* In most cases, there is nothing special about these
900 * inputs, so adopt a convention to use the generic
901 * semantic name and the mesa VARYING_SLOT_ number as the
904 * All that is required is that the vertex shader labels
905 * its own outputs similarly, and that the vertex shader
906 * generates at least every output required by the
907 * fragment shader plus fixed-function hardware (such as
910 * However, some drivers may need us to identify the PNTC and TEXi
911 * varyings if, for example, their capability to replace them with
912 * sprite coordinates is limited.
914 case VARYING_SLOT_PNTC
:
915 if (st
->needs_texcoord_semantic
) {
916 input_semantic_name
[slot
] = TGSI_SEMANTIC_PCOORD
;
917 input_semantic_index
[slot
] = 0;
918 interpMode
[slot
] = TGSI_INTERPOLATE_LINEAR
;
922 case VARYING_SLOT_TEX0
:
923 case VARYING_SLOT_TEX1
:
924 case VARYING_SLOT_TEX2
:
925 case VARYING_SLOT_TEX3
:
926 case VARYING_SLOT_TEX4
:
927 case VARYING_SLOT_TEX5
:
928 case VARYING_SLOT_TEX6
:
929 case VARYING_SLOT_TEX7
:
930 if (st
->needs_texcoord_semantic
) {
931 input_semantic_name
[slot
] = TGSI_SEMANTIC_TEXCOORD
;
932 input_semantic_index
[slot
] = attr
- VARYING_SLOT_TEX0
;
933 interpMode
[slot
] = stfp
->glsl_to_tgsi
?
934 TGSI_INTERPOLATE_COUNT
: TGSI_INTERPOLATE_PERSPECTIVE
;
938 case VARYING_SLOT_VAR0
:
940 /* Semantic indices should be zero-based because drivers may choose
941 * to assign a fixed slot determined by that index.
942 * This is useful because ARB_separate_shader_objects uses location
943 * qualifiers for linkage, and if the semantic index corresponds to
944 * these locations, linkage passes in the driver become unecessary.
946 * If needs_texcoord_semantic is true, no semantic indices will be
947 * consumed for the TEXi varyings, and we can base the locations of
948 * the user varyings on VAR0. Otherwise, we use TEX0 as base index.
950 assert(attr
>= VARYING_SLOT_VAR0
|| attr
== VARYING_SLOT_PNTC
||
951 (attr
>= VARYING_SLOT_TEX0
&& attr
<= VARYING_SLOT_TEX7
));
952 input_semantic_name
[slot
] = TGSI_SEMANTIC_GENERIC
;
953 input_semantic_index
[slot
] = st_get_generic_varying_index(st
, attr
);
954 if (attr
== VARYING_SLOT_PNTC
)
955 interpMode
[slot
] = TGSI_INTERPOLATE_LINEAR
;
957 interpMode
[slot
] = stfp
->glsl_to_tgsi
?
958 TGSI_INTERPOLATE_COUNT
: TGSI_INTERPOLATE_PERSPECTIVE
;
964 inputMapping
[attr
] = -1;
969 * Semantics and mapping for outputs
971 GLbitfield64 outputsWritten
= stfp
->Base
.info
.outputs_written
;
973 /* if z is written, emit that first */
974 if (outputsWritten
& BITFIELD64_BIT(FRAG_RESULT_DEPTH
)) {
975 fs_output_semantic_name
[fs_num_outputs
] = TGSI_SEMANTIC_POSITION
;
976 fs_output_semantic_index
[fs_num_outputs
] = 0;
977 outputMapping
[FRAG_RESULT_DEPTH
] = fs_num_outputs
;
979 outputsWritten
&= ~(1 << FRAG_RESULT_DEPTH
);
982 if (outputsWritten
& BITFIELD64_BIT(FRAG_RESULT_STENCIL
)) {
983 fs_output_semantic_name
[fs_num_outputs
] = TGSI_SEMANTIC_STENCIL
;
984 fs_output_semantic_index
[fs_num_outputs
] = 0;
985 outputMapping
[FRAG_RESULT_STENCIL
] = fs_num_outputs
;
987 outputsWritten
&= ~(1 << FRAG_RESULT_STENCIL
);
990 if (outputsWritten
& BITFIELD64_BIT(FRAG_RESULT_SAMPLE_MASK
)) {
991 fs_output_semantic_name
[fs_num_outputs
] = TGSI_SEMANTIC_SAMPLEMASK
;
992 fs_output_semantic_index
[fs_num_outputs
] = 0;
993 outputMapping
[FRAG_RESULT_SAMPLE_MASK
] = fs_num_outputs
;
995 outputsWritten
&= ~(1 << FRAG_RESULT_SAMPLE_MASK
);
998 /* handle remaining outputs (color) */
999 for (attr
= 0; attr
< ARRAY_SIZE(outputMapping
); attr
++) {
1000 const GLbitfield64 written
= attr
< FRAG_RESULT_MAX
? outputsWritten
:
1001 stfp
->Base
.SecondaryOutputsWritten
;
1002 const unsigned loc
= attr
% FRAG_RESULT_MAX
;
1004 if (written
& BITFIELD64_BIT(loc
)) {
1006 case FRAG_RESULT_DEPTH
:
1007 case FRAG_RESULT_STENCIL
:
1008 case FRAG_RESULT_SAMPLE_MASK
:
1012 case FRAG_RESULT_COLOR
:
1013 write_all
= GL_TRUE
; /* fallthrough */
1016 assert(loc
== FRAG_RESULT_COLOR
||
1017 (FRAG_RESULT_DATA0
<= loc
&& loc
< FRAG_RESULT_MAX
));
1019 index
= (loc
== FRAG_RESULT_COLOR
) ? 0 : (loc
- FRAG_RESULT_DATA0
);
1021 if (attr
>= FRAG_RESULT_MAX
) {
1022 /* Secondary color for dual source blending. */
1027 fs_output_semantic_name
[fs_num_outputs
] = TGSI_SEMANTIC_COLOR
;
1028 fs_output_semantic_index
[fs_num_outputs
] = index
;
1029 outputMapping
[attr
] = fs_num_outputs
;
1038 ureg
= ureg_create_with_screen(PIPE_SHADER_FRAGMENT
, st
->pipe
->screen
);
1042 if (ST_DEBUG
& DEBUG_MESA
) {
1043 _mesa_print_program(&stfp
->Base
);
1044 _mesa_print_program_parameters(st
->ctx
, &stfp
->Base
);
1047 if (write_all
== GL_TRUE
)
1048 ureg_property(ureg
, TGSI_PROPERTY_FS_COLOR0_WRITES_ALL_CBUFS
, 1);
1050 if (stfp
->Base
.info
.fs
.depth_layout
!= FRAG_DEPTH_LAYOUT_NONE
) {
1051 switch (stfp
->Base
.info
.fs
.depth_layout
) {
1052 case FRAG_DEPTH_LAYOUT_ANY
:
1053 ureg_property(ureg
, TGSI_PROPERTY_FS_DEPTH_LAYOUT
,
1054 TGSI_FS_DEPTH_LAYOUT_ANY
);
1056 case FRAG_DEPTH_LAYOUT_GREATER
:
1057 ureg_property(ureg
, TGSI_PROPERTY_FS_DEPTH_LAYOUT
,
1058 TGSI_FS_DEPTH_LAYOUT_GREATER
);
1060 case FRAG_DEPTH_LAYOUT_LESS
:
1061 ureg_property(ureg
, TGSI_PROPERTY_FS_DEPTH_LAYOUT
,
1062 TGSI_FS_DEPTH_LAYOUT_LESS
);
1064 case FRAG_DEPTH_LAYOUT_UNCHANGED
:
1065 ureg_property(ureg
, TGSI_PROPERTY_FS_DEPTH_LAYOUT
,
1066 TGSI_FS_DEPTH_LAYOUT_UNCHANGED
);
1073 if (stfp
->glsl_to_tgsi
) {
1074 st_translate_program(st
->ctx
,
1075 PIPE_SHADER_FRAGMENT
,
1083 input_semantic_name
,
1084 input_semantic_index
,
1089 fs_output_semantic_name
,
1090 fs_output_semantic_index
);
1092 free_glsl_to_tgsi_visitor(stfp
->glsl_to_tgsi
);
1093 } else if (stfp
->ati_fs
)
1094 st_translate_atifs_program(ureg
,
1100 input_semantic_name
,
1101 input_semantic_index
,
1106 fs_output_semantic_name
,
1107 fs_output_semantic_index
);
1109 st_translate_mesa_program(st
->ctx
,
1110 PIPE_SHADER_FRAGMENT
,
1116 input_semantic_name
,
1117 input_semantic_index
,
1122 fs_output_semantic_name
,
1123 fs_output_semantic_index
);
1125 stfp
->state
.tokens
= ureg_get_tokens(ureg
, NULL
);
1128 if (stfp
->glsl_to_tgsi
) {
1129 stfp
->glsl_to_tgsi
= NULL
;
1130 st_store_ir_in_disk_cache(st
, &stfp
->Base
, false);
1133 return stfp
->state
.tokens
!= NULL
;
1136 static struct st_fp_variant
*
1137 st_create_fp_variant(struct st_context
*st
,
1138 struct st_program
*stfp
,
1139 const struct st_fp_variant_key
*key
)
1141 struct pipe_context
*pipe
= st
->pipe
;
1142 struct st_fp_variant
*variant
= CALLOC_STRUCT(st_fp_variant
);
1143 struct pipe_shader_state state
= {0};
1144 struct gl_program_parameter_list
*params
= stfp
->Base
.Parameters
;
1145 static const gl_state_index16 texcoord_state
[STATE_LENGTH
] =
1146 { STATE_INTERNAL
, STATE_CURRENT_ATTRIB
, VERT_ATTRIB_TEX0
};
1147 static const gl_state_index16 scale_state
[STATE_LENGTH
] =
1148 { STATE_INTERNAL
, STATE_PT_SCALE
};
1149 static const gl_state_index16 bias_state
[STATE_LENGTH
] =
1150 { STATE_INTERNAL
, STATE_PT_BIAS
};
1151 static const gl_state_index16 alpha_ref_state
[STATE_LENGTH
] =
1152 { STATE_INTERNAL
, STATE_ALPHA_REF
};
1157 if (stfp
->state
.type
== PIPE_SHADER_IR_NIR
) {
1158 bool finalize
= false;
1160 state
.type
= PIPE_SHADER_IR_NIR
;
1161 state
.ir
.nir
= nir_shader_clone(NULL
, stfp
->Base
.nir
);
1163 if (key
->clamp_color
) {
1164 NIR_PASS_V(state
.ir
.nir
, nir_lower_clamp_color_outputs
);
1168 if (key
->lower_flatshade
) {
1169 NIR_PASS_V(state
.ir
.nir
, nir_lower_flatshade
);
1173 if (key
->lower_alpha_func
!= COMPARE_FUNC_NEVER
) {
1174 _mesa_add_state_reference(params
, alpha_ref_state
);
1175 NIR_PASS_V(state
.ir
.nir
, nir_lower_alpha_test
, key
->lower_alpha_func
,
1176 false, alpha_ref_state
);
1180 if (key
->lower_two_sided_color
) {
1181 NIR_PASS_V(state
.ir
.nir
, nir_lower_two_sided_color
);
1185 if (key
->persample_shading
) {
1186 nir_shader
*shader
= state
.ir
.nir
;
1187 nir_foreach_variable(var
, &shader
->inputs
)
1188 var
->data
.sample
= true;
1192 assert(!(key
->bitmap
&& key
->drawpixels
));
1196 nir_lower_bitmap_options options
= {0};
1198 variant
->bitmap_sampler
= ffs(~stfp
->Base
.SamplersUsed
) - 1;
1199 options
.sampler
= variant
->bitmap_sampler
;
1200 options
.swizzle_xxxx
= st
->bitmap
.tex_format
== PIPE_FORMAT_R8_UNORM
;
1202 NIR_PASS_V(state
.ir
.nir
, nir_lower_bitmap
, &options
);
1206 /* glDrawPixels (color only) */
1207 if (key
->drawpixels
) {
1208 nir_lower_drawpixels_options options
= {{0}};
1209 unsigned samplers_used
= stfp
->Base
.SamplersUsed
;
1211 /* Find the first unused slot. */
1212 variant
->drawpix_sampler
= ffs(~samplers_used
) - 1;
1213 options
.drawpix_sampler
= variant
->drawpix_sampler
;
1214 samplers_used
|= (1 << variant
->drawpix_sampler
);
1216 options
.pixel_maps
= key
->pixelMaps
;
1217 if (key
->pixelMaps
) {
1218 variant
->pixelmap_sampler
= ffs(~samplers_used
) - 1;
1219 options
.pixelmap_sampler
= variant
->pixelmap_sampler
;
1222 options
.scale_and_bias
= key
->scaleAndBias
;
1223 if (key
->scaleAndBias
) {
1224 _mesa_add_state_reference(params
, scale_state
);
1225 memcpy(options
.scale_state_tokens
, scale_state
,
1226 sizeof(options
.scale_state_tokens
));
1227 _mesa_add_state_reference(params
, bias_state
);
1228 memcpy(options
.bias_state_tokens
, bias_state
,
1229 sizeof(options
.bias_state_tokens
));
1232 _mesa_add_state_reference(params
, texcoord_state
);
1233 memcpy(options
.texcoord_state_tokens
, texcoord_state
,
1234 sizeof(options
.texcoord_state_tokens
));
1236 NIR_PASS_V(state
.ir
.nir
, nir_lower_drawpixels
, &options
);
1240 if (unlikely(key
->external
.lower_nv12
|| key
->external
.lower_iyuv
||
1241 key
->external
.lower_xy_uxvx
|| key
->external
.lower_yx_xuxv
||
1242 key
->external
.lower_ayuv
|| key
->external
.lower_xyuv
)) {
1243 nir_lower_tex_options options
= {0};
1244 options
.lower_y_uv_external
= key
->external
.lower_nv12
;
1245 options
.lower_y_u_v_external
= key
->external
.lower_iyuv
;
1246 options
.lower_xy_uxvx_external
= key
->external
.lower_xy_uxvx
;
1247 options
.lower_yx_xuxv_external
= key
->external
.lower_yx_xuxv
;
1248 options
.lower_ayuv_external
= key
->external
.lower_ayuv
;
1249 options
.lower_xyuv_external
= key
->external
.lower_xyuv
;
1250 NIR_PASS_V(state
.ir
.nir
, nir_lower_tex
, &options
);
1254 if (finalize
|| !st
->allow_st_finalize_nir_twice
) {
1255 st_finalize_nir(st
, &stfp
->Base
, stfp
->shader_program
, state
.ir
.nir
,
1259 /* This pass needs to happen *after* nir_lower_sampler */
1260 if (unlikely(key
->external
.lower_nv12
|| key
->external
.lower_iyuv
||
1261 key
->external
.lower_xy_uxvx
|| key
->external
.lower_yx_xuxv
)) {
1262 NIR_PASS_V(state
.ir
.nir
, st_nir_lower_tex_src_plane
,
1263 ~stfp
->Base
.SamplersUsed
,
1264 key
->external
.lower_nv12
|| key
->external
.lower_xy_uxvx
||
1265 key
->external
.lower_yx_xuxv
,
1266 key
->external
.lower_iyuv
);
1270 if (finalize
|| !st
->allow_st_finalize_nir_twice
) {
1271 /* Some of the lowering above may have introduced new varyings */
1272 nir_shader_gather_info(state
.ir
.nir
,
1273 nir_shader_get_entrypoint(state
.ir
.nir
));
1275 struct pipe_screen
*screen
= pipe
->screen
;
1276 if (screen
->finalize_nir
)
1277 screen
->finalize_nir(screen
, state
.ir
.nir
, false);
1280 if (ST_DEBUG
& DEBUG_PRINT_IR
)
1281 nir_print_shader(state
.ir
.nir
, stderr
);
1283 variant
->base
.driver_shader
= pipe
->create_fs_state(pipe
, &state
);
1284 variant
->key
= *key
;
1289 state
.tokens
= stfp
->state
.tokens
;
1291 assert(!(key
->bitmap
&& key
->drawpixels
));
1293 /* Fix texture targets and add fog for ATI_fs */
1295 const struct tgsi_token
*tokens
= st_fixup_atifs(state
.tokens
, key
);
1298 state
.tokens
= tokens
;
1300 fprintf(stderr
, "mesa: cannot post-process ATI_fs\n");
1303 /* Emulate features. */
1304 if (key
->clamp_color
|| key
->persample_shading
) {
1305 const struct tgsi_token
*tokens
;
1307 (key
->clamp_color
? TGSI_EMU_CLAMP_COLOR_OUTPUTS
: 0) |
1308 (key
->persample_shading
? TGSI_EMU_FORCE_PERSAMPLE_INTERP
: 0);
1310 tokens
= tgsi_emulate(state
.tokens
, flags
);
1313 if (state
.tokens
!= stfp
->state
.tokens
)
1314 tgsi_free_tokens(state
.tokens
);
1315 state
.tokens
= tokens
;
1317 fprintf(stderr
, "mesa: cannot emulate deprecated features\n");
1322 const struct tgsi_token
*tokens
;
1324 variant
->bitmap_sampler
= ffs(~stfp
->Base
.SamplersUsed
) - 1;
1326 tokens
= st_get_bitmap_shader(state
.tokens
,
1327 st
->internal_target
,
1328 variant
->bitmap_sampler
,
1329 st
->needs_texcoord_semantic
,
1330 st
->bitmap
.tex_format
==
1331 PIPE_FORMAT_R8_UNORM
);
1334 if (state
.tokens
!= stfp
->state
.tokens
)
1335 tgsi_free_tokens(state
.tokens
);
1336 state
.tokens
= tokens
;
1338 fprintf(stderr
, "mesa: cannot create a shader for glBitmap\n");
1341 /* glDrawPixels (color only) */
1342 if (key
->drawpixels
) {
1343 const struct tgsi_token
*tokens
;
1344 unsigned scale_const
= 0, bias_const
= 0, texcoord_const
= 0;
1346 /* Find the first unused slot. */
1347 variant
->drawpix_sampler
= ffs(~stfp
->Base
.SamplersUsed
) - 1;
1349 if (key
->pixelMaps
) {
1350 unsigned samplers_used
= stfp
->Base
.SamplersUsed
|
1351 (1 << variant
->drawpix_sampler
);
1353 variant
->pixelmap_sampler
= ffs(~samplers_used
) - 1;
1356 if (key
->scaleAndBias
) {
1357 scale_const
= _mesa_add_state_reference(params
, scale_state
);
1358 bias_const
= _mesa_add_state_reference(params
, bias_state
);
1361 texcoord_const
= _mesa_add_state_reference(params
, texcoord_state
);
1363 tokens
= st_get_drawpix_shader(state
.tokens
,
1364 st
->needs_texcoord_semantic
,
1365 key
->scaleAndBias
, scale_const
,
1366 bias_const
, key
->pixelMaps
,
1367 variant
->drawpix_sampler
,
1368 variant
->pixelmap_sampler
,
1369 texcoord_const
, st
->internal_target
);
1372 if (state
.tokens
!= stfp
->state
.tokens
)
1373 tgsi_free_tokens(state
.tokens
);
1374 state
.tokens
= tokens
;
1376 fprintf(stderr
, "mesa: cannot create a shader for glDrawPixels\n");
1379 if (unlikely(key
->external
.lower_nv12
|| key
->external
.lower_iyuv
||
1380 key
->external
.lower_xy_uxvx
|| key
->external
.lower_yx_xuxv
)) {
1381 const struct tgsi_token
*tokens
;
1383 /* samplers inserted would conflict, but this should be unpossible: */
1384 assert(!(key
->bitmap
|| key
->drawpixels
));
1386 tokens
= st_tgsi_lower_yuv(state
.tokens
,
1387 ~stfp
->Base
.SamplersUsed
,
1388 key
->external
.lower_nv12
||
1389 key
->external
.lower_xy_uxvx
||
1390 key
->external
.lower_yx_xuxv
,
1391 key
->external
.lower_iyuv
);
1393 if (state
.tokens
!= stfp
->state
.tokens
)
1394 tgsi_free_tokens(state
.tokens
);
1395 state
.tokens
= tokens
;
1397 fprintf(stderr
, "mesa: cannot create a shader for samplerExternalOES\n");
1401 if (key
->lower_depth_clamp
) {
1402 unsigned depth_range_const
= _mesa_add_state_reference(params
, depth_range_state
);
1404 const struct tgsi_token
*tokens
;
1405 tokens
= st_tgsi_lower_depth_clamp_fs(state
.tokens
, depth_range_const
);
1406 if (state
.tokens
!= stfp
->state
.tokens
)
1407 tgsi_free_tokens(state
.tokens
);
1408 state
.tokens
= tokens
;
1411 if (ST_DEBUG
& DEBUG_PRINT_IR
)
1412 tgsi_dump(state
.tokens
, 0);
1414 /* fill in variant */
1415 variant
->base
.driver_shader
= pipe
->create_fs_state(pipe
, &state
);
1416 variant
->key
= *key
;
1418 if (state
.tokens
!= stfp
->state
.tokens
)
1419 tgsi_free_tokens(state
.tokens
);
1424 * Translate fragment program if needed.
1426 struct st_fp_variant
*
1427 st_get_fp_variant(struct st_context
*st
,
1428 struct st_program
*stfp
,
1429 const struct st_fp_variant_key
*key
)
1431 struct st_fp_variant
*fpv
;
1433 /* Search for existing variant */
1434 for (fpv
= st_fp_variant(stfp
->variants
); fpv
;
1435 fpv
= st_fp_variant(fpv
->base
.next
)) {
1436 if (memcmp(&fpv
->key
, key
, sizeof(*key
)) == 0) {
1443 fpv
= st_create_fp_variant(st
, stfp
, key
);
1445 fpv
->base
.st
= key
->st
;
1447 if (key
->bitmap
|| key
->drawpixels
) {
1448 /* Regular variants should always come before the
1449 * bitmap & drawpixels variants, (unless there
1450 * are no regular variants) so that
1451 * st_update_fp can take a fast path when
1452 * shader_has_one_variant is set.
1454 if (!stfp
->variants
) {
1455 stfp
->variants
= &fpv
->base
;
1457 /* insert into list after the first one */
1458 fpv
->base
.next
= stfp
->variants
->next
;
1459 stfp
->variants
->next
= &fpv
->base
;
1462 /* insert into list */
1463 fpv
->base
.next
= stfp
->variants
;
1464 stfp
->variants
= &fpv
->base
;
1473 * Translate a program. This is common code for geometry and tessellation
1477 st_translate_common_program(struct st_context
*st
,
1478 struct st_program
*stp
)
1480 struct gl_program
*prog
= &stp
->Base
;
1481 enum pipe_shader_type stage
=
1482 pipe_shader_type_from_mesa(stp
->Base
.info
.stage
);
1483 struct ureg_program
*ureg
= ureg_create_with_screen(stage
, st
->pipe
->screen
);
1489 case PIPE_SHADER_TESS_CTRL
:
1490 ureg_property(ureg
, TGSI_PROPERTY_TCS_VERTICES_OUT
,
1491 stp
->Base
.info
.tess
.tcs_vertices_out
);
1494 case PIPE_SHADER_TESS_EVAL
:
1495 if (stp
->Base
.info
.tess
.primitive_mode
== GL_ISOLINES
)
1496 ureg_property(ureg
, TGSI_PROPERTY_TES_PRIM_MODE
, GL_LINES
);
1498 ureg_property(ureg
, TGSI_PROPERTY_TES_PRIM_MODE
,
1499 stp
->Base
.info
.tess
.primitive_mode
);
1501 STATIC_ASSERT((TESS_SPACING_EQUAL
+ 1) % 3 == PIPE_TESS_SPACING_EQUAL
);
1502 STATIC_ASSERT((TESS_SPACING_FRACTIONAL_ODD
+ 1) % 3 ==
1503 PIPE_TESS_SPACING_FRACTIONAL_ODD
);
1504 STATIC_ASSERT((TESS_SPACING_FRACTIONAL_EVEN
+ 1) % 3 ==
1505 PIPE_TESS_SPACING_FRACTIONAL_EVEN
);
1507 ureg_property(ureg
, TGSI_PROPERTY_TES_SPACING
,
1508 (stp
->Base
.info
.tess
.spacing
+ 1) % 3);
1510 ureg_property(ureg
, TGSI_PROPERTY_TES_VERTEX_ORDER_CW
,
1511 !stp
->Base
.info
.tess
.ccw
);
1512 ureg_property(ureg
, TGSI_PROPERTY_TES_POINT_MODE
,
1513 stp
->Base
.info
.tess
.point_mode
);
1516 case PIPE_SHADER_GEOMETRY
:
1517 ureg_property(ureg
, TGSI_PROPERTY_GS_INPUT_PRIM
,
1518 stp
->Base
.info
.gs
.input_primitive
);
1519 ureg_property(ureg
, TGSI_PROPERTY_GS_OUTPUT_PRIM
,
1520 stp
->Base
.info
.gs
.output_primitive
);
1521 ureg_property(ureg
, TGSI_PROPERTY_GS_MAX_OUTPUT_VERTICES
,
1522 stp
->Base
.info
.gs
.vertices_out
);
1523 ureg_property(ureg
, TGSI_PROPERTY_GS_INVOCATIONS
,
1524 stp
->Base
.info
.gs
.invocations
);
1531 ubyte inputSlotToAttr
[VARYING_SLOT_TESS_MAX
];
1532 ubyte inputMapping
[VARYING_SLOT_TESS_MAX
];
1533 ubyte outputMapping
[VARYING_SLOT_TESS_MAX
];
1536 ubyte input_semantic_name
[PIPE_MAX_SHADER_INPUTS
];
1537 ubyte input_semantic_index
[PIPE_MAX_SHADER_INPUTS
];
1538 uint num_inputs
= 0;
1540 ubyte output_semantic_name
[PIPE_MAX_SHADER_OUTPUTS
];
1541 ubyte output_semantic_index
[PIPE_MAX_SHADER_OUTPUTS
];
1542 uint num_outputs
= 0;
1546 memset(inputSlotToAttr
, 0, sizeof(inputSlotToAttr
));
1547 memset(inputMapping
, 0, sizeof(inputMapping
));
1548 memset(outputMapping
, 0, sizeof(outputMapping
));
1549 memset(&stp
->state
, 0, sizeof(stp
->state
));
1551 if (prog
->info
.clip_distance_array_size
)
1552 ureg_property(ureg
, TGSI_PROPERTY_NUM_CLIPDIST_ENABLED
,
1553 prog
->info
.clip_distance_array_size
);
1554 if (prog
->info
.cull_distance_array_size
)
1555 ureg_property(ureg
, TGSI_PROPERTY_NUM_CULLDIST_ENABLED
,
1556 prog
->info
.cull_distance_array_size
);
1559 * Convert Mesa program inputs to TGSI input register semantics.
1561 for (attr
= 0; attr
< VARYING_SLOT_MAX
; attr
++) {
1562 if ((prog
->info
.inputs_read
& BITFIELD64_BIT(attr
)) == 0)
1565 unsigned slot
= num_inputs
++;
1567 inputMapping
[attr
] = slot
;
1568 inputSlotToAttr
[slot
] = attr
;
1570 unsigned semantic_name
, semantic_index
;
1571 tgsi_get_gl_varying_semantic(attr
, st
->needs_texcoord_semantic
,
1572 &semantic_name
, &semantic_index
);
1573 input_semantic_name
[slot
] = semantic_name
;
1574 input_semantic_index
[slot
] = semantic_index
;
1577 /* Also add patch inputs. */
1578 for (attr
= 0; attr
< 32; attr
++) {
1579 if (prog
->info
.patch_inputs_read
& (1u << attr
)) {
1580 GLuint slot
= num_inputs
++;
1581 GLuint patch_attr
= VARYING_SLOT_PATCH0
+ attr
;
1583 inputMapping
[patch_attr
] = slot
;
1584 inputSlotToAttr
[slot
] = patch_attr
;
1585 input_semantic_name
[slot
] = TGSI_SEMANTIC_PATCH
;
1586 input_semantic_index
[slot
] = attr
;
1590 /* initialize output semantics to defaults */
1591 for (i
= 0; i
< PIPE_MAX_SHADER_OUTPUTS
; i
++) {
1592 output_semantic_name
[i
] = TGSI_SEMANTIC_GENERIC
;
1593 output_semantic_index
[i
] = 0;
1597 * Determine number of outputs, the (default) output register
1598 * mapping and the semantic information for each output.
1600 for (attr
= 0; attr
< VARYING_SLOT_MAX
; attr
++) {
1601 if (prog
->info
.outputs_written
& BITFIELD64_BIT(attr
)) {
1602 GLuint slot
= num_outputs
++;
1604 outputMapping
[attr
] = slot
;
1606 unsigned semantic_name
, semantic_index
;
1607 tgsi_get_gl_varying_semantic(attr
, st
->needs_texcoord_semantic
,
1608 &semantic_name
, &semantic_index
);
1609 output_semantic_name
[slot
] = semantic_name
;
1610 output_semantic_index
[slot
] = semantic_index
;
1614 /* Also add patch outputs. */
1615 for (attr
= 0; attr
< 32; attr
++) {
1616 if (prog
->info
.patch_outputs_written
& (1u << attr
)) {
1617 GLuint slot
= num_outputs
++;
1618 GLuint patch_attr
= VARYING_SLOT_PATCH0
+ attr
;
1620 outputMapping
[patch_attr
] = slot
;
1621 output_semantic_name
[slot
] = TGSI_SEMANTIC_PATCH
;
1622 output_semantic_index
[slot
] = attr
;
1626 st_translate_program(st
->ctx
,
1635 input_semantic_name
,
1636 input_semantic_index
,
1641 output_semantic_name
,
1642 output_semantic_index
);
1644 stp
->state
.tokens
= ureg_get_tokens(ureg
, NULL
);
1648 st_translate_stream_output_info(prog
);
1650 st_store_ir_in_disk_cache(st
, prog
, false);
1652 if (ST_DEBUG
& DEBUG_PRINT_IR
&& ST_DEBUG
& DEBUG_MESA
)
1653 _mesa_print_program(prog
);
1655 free_glsl_to_tgsi_visitor(stp
->glsl_to_tgsi
);
1656 stp
->glsl_to_tgsi
= NULL
;
1662 * Get/create a basic program variant.
1665 st_get_common_variant(struct st_context
*st
,
1666 struct st_program
*prog
,
1667 const struct st_common_variant_key
*key
)
1669 struct pipe_context
*pipe
= st
->pipe
;
1670 struct st_variant
*v
;
1671 struct pipe_shader_state state
= {0};
1673 /* Search for existing variant */
1674 for (v
= prog
->variants
; v
; v
= v
->next
) {
1675 if (memcmp(&st_common_variant(v
)->key
, key
, sizeof(*key
)) == 0)
1681 v
= (struct st_variant
*)CALLOC_STRUCT(st_common_variant
);
1683 if (prog
->state
.type
== PIPE_SHADER_IR_NIR
) {
1684 bool finalize
= false;
1686 state
.type
= PIPE_SHADER_IR_NIR
;
1687 state
.ir
.nir
= nir_shader_clone(NULL
, prog
->Base
.nir
);
1689 if (key
->clamp_color
) {
1690 NIR_PASS_V(state
.ir
.nir
, nir_lower_clamp_color_outputs
);
1694 state
.stream_output
= prog
->state
.stream_output
;
1696 if (finalize
|| !st
->allow_st_finalize_nir_twice
) {
1697 st_finalize_nir(st
, &prog
->Base
, prog
->shader_program
,
1698 state
.ir
.nir
, true);
1701 if (ST_DEBUG
& DEBUG_PRINT_IR
)
1702 nir_print_shader(state
.ir
.nir
, stderr
);
1704 if (key
->lower_depth_clamp
) {
1705 struct gl_program_parameter_list
*params
= prog
->Base
.Parameters
;
1707 unsigned depth_range_const
=
1708 _mesa_add_state_reference(params
, depth_range_state
);
1710 const struct tgsi_token
*tokens
;
1712 st_tgsi_lower_depth_clamp(prog
->state
.tokens
,
1714 key
->clip_negative_one_to_one
);
1716 if (tokens
!= prog
->state
.tokens
)
1717 tgsi_free_tokens(prog
->state
.tokens
);
1719 prog
->state
.tokens
= tokens
;
1721 state
= prog
->state
;
1723 if (ST_DEBUG
& DEBUG_PRINT_IR
)
1724 tgsi_dump(state
.tokens
, 0);
1726 /* fill in new variant */
1727 switch (prog
->Base
.info
.stage
) {
1728 case MESA_SHADER_TESS_CTRL
:
1729 v
->driver_shader
= pipe
->create_tcs_state(pipe
, &state
);
1731 case MESA_SHADER_TESS_EVAL
:
1732 v
->driver_shader
= pipe
->create_tes_state(pipe
, &state
);
1734 case MESA_SHADER_GEOMETRY
:
1735 v
->driver_shader
= pipe
->create_gs_state(pipe
, &state
);
1737 case MESA_SHADER_COMPUTE
: {
1738 struct pipe_compute_state cs
= {0};
1739 cs
.ir_type
= state
.type
;
1740 cs
.req_local_mem
= prog
->Base
.info
.cs
.shared_size
;
1742 if (state
.type
== PIPE_SHADER_IR_NIR
)
1743 cs
.prog
= state
.ir
.nir
;
1745 cs
.prog
= state
.tokens
;
1747 v
->driver_shader
= pipe
->create_compute_state(pipe
, &cs
);
1751 assert(!"unhandled shader type");
1756 st_common_variant(v
)->key
= *key
;
1759 /* insert into list */
1760 v
->next
= prog
->variants
;
1770 * Vert/Geom/Frag programs have per-context variants. Free all the
1771 * variants attached to the given program which match the given context.
1774 destroy_program_variants(struct st_context
*st
, struct gl_program
*target
)
1776 if (!target
|| target
== &_mesa_DummyProgram
)
1779 struct st_program
*p
= st_program(target
);
1780 struct st_variant
*v
, **prevPtr
= &p
->variants
;
1782 for (v
= p
->variants
; v
; ) {
1783 struct st_variant
*next
= v
->next
;
1785 /* unlink from list */
1787 /* destroy this variant */
1788 delete_variant(st
, v
, target
->Target
);
1799 * Callback for _mesa_HashWalk. Free all the shader's program variants
1800 * which match the given context.
1803 destroy_shader_program_variants_cb(GLuint key
, void *data
, void *userData
)
1805 struct st_context
*st
= (struct st_context
*) userData
;
1806 struct gl_shader
*shader
= (struct gl_shader
*) data
;
1808 switch (shader
->Type
) {
1809 case GL_SHADER_PROGRAM_MESA
:
1811 struct gl_shader_program
*shProg
= (struct gl_shader_program
*) data
;
1814 for (i
= 0; i
< ARRAY_SIZE(shProg
->_LinkedShaders
); i
++) {
1815 if (shProg
->_LinkedShaders
[i
])
1816 destroy_program_variants(st
, shProg
->_LinkedShaders
[i
]->Program
);
1820 case GL_VERTEX_SHADER
:
1821 case GL_FRAGMENT_SHADER
:
1822 case GL_GEOMETRY_SHADER
:
1823 case GL_TESS_CONTROL_SHADER
:
1824 case GL_TESS_EVALUATION_SHADER
:
1825 case GL_COMPUTE_SHADER
:
1834 * Callback for _mesa_HashWalk. Free all the program variants which match
1835 * the given context.
1838 destroy_program_variants_cb(GLuint key
, void *data
, void *userData
)
1840 struct st_context
*st
= (struct st_context
*) userData
;
1841 struct gl_program
*program
= (struct gl_program
*) data
;
1842 destroy_program_variants(st
, program
);
1847 * Walk over all shaders and programs to delete any variants which
1848 * belong to the given context.
1849 * This is called during context tear-down.
1852 st_destroy_program_variants(struct st_context
*st
)
1854 /* If shaders can be shared with other contexts, the last context will
1855 * call DeleteProgram on all shaders, releasing everything.
1857 if (st
->has_shareable_shaders
)
1860 /* ARB vert/frag program */
1861 _mesa_HashWalk(st
->ctx
->Shared
->Programs
,
1862 destroy_program_variants_cb
, st
);
1864 /* GLSL vert/frag/geom shaders */
1865 _mesa_HashWalk(st
->ctx
->Shared
->ShaderObjects
,
1866 destroy_shader_program_variants_cb
, st
);
1871 * Compile one shader variant.
1874 st_precompile_shader_variant(struct st_context
*st
,
1875 struct gl_program
*prog
)
1877 switch (prog
->Target
) {
1878 case GL_VERTEX_PROGRAM_ARB
: {
1879 struct st_program
*p
= (struct st_program
*)prog
;
1880 struct st_common_variant_key key
;
1882 memset(&key
, 0, sizeof(key
));
1884 key
.st
= st
->has_shareable_shaders
? NULL
: st
;
1885 st_get_vp_variant(st
, p
, &key
);
1889 case GL_FRAGMENT_PROGRAM_ARB
: {
1890 struct st_program
*p
= (struct st_program
*)prog
;
1891 struct st_fp_variant_key key
;
1893 memset(&key
, 0, sizeof(key
));
1895 key
.st
= st
->has_shareable_shaders
? NULL
: st
;
1896 st_get_fp_variant(st
, p
, &key
);
1900 case GL_TESS_CONTROL_PROGRAM_NV
:
1901 case GL_TESS_EVALUATION_PROGRAM_NV
:
1902 case GL_GEOMETRY_PROGRAM_NV
:
1903 case GL_COMPUTE_PROGRAM_NV
: {
1904 struct st_program
*p
= st_program(prog
);
1905 struct st_common_variant_key key
;
1907 memset(&key
, 0, sizeof(key
));
1909 key
.st
= st
->has_shareable_shaders
? NULL
: st
;
1910 st_get_common_variant(st
, p
, &key
);
1920 st_finalize_program(struct st_context
*st
, struct gl_program
*prog
)
1922 if (st
->current_program
[prog
->info
.stage
] == prog
) {
1923 if (prog
->info
.stage
== MESA_SHADER_VERTEX
)
1924 st
->dirty
|= ST_NEW_VERTEX_PROGRAM(st
, (struct st_program
*)prog
);
1926 st
->dirty
|= ((struct st_program
*)prog
)->affected_states
;
1930 nir_sweep(prog
->nir
);
1932 /* Create Gallium shaders now instead of on demand. */
1933 if (ST_DEBUG
& DEBUG_PRECOMPILE
||
1934 st
->shader_has_one_variant
[prog
->info
.stage
])
1935 st_precompile_shader_variant(st
, prog
);