st/mesa: call nir_sweep in st_finalize_nir
[mesa.git] / src / mesa / state_tracker / st_program.c
1 /**************************************************************************
2 *
3 * Copyright 2007 VMware, Inc.
4 * All Rights Reserved.
5 *
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:
13 *
14 * The above copyright notice and this permission notice (including the
15 * next paragraph) shall be included in all copies or substantial portions
16 * of the Software.
17 *
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.
25 *
26 **************************************************************************/
27 /*
28 * Authors:
29 * Keith Whitwell <keithw@vmware.com>
30 * Brian Paul
31 */
32
33
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"
42
43 #include "compiler/nir/nir.h"
44
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"
53
54 #include "st_debug.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"
63 #include "st_nir.h"
64 #include "st_shader_cache.h"
65 #include "st_util.h"
66 #include "cso_cache/cso_context.h"
67
68
69
70 static void
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,
76 uint64_t new_images,
77 uint64_t new_ubos,
78 uint64_t new_ssbos,
79 uint64_t new_atomics)
80 {
81 if (prog->Parameters->NumParameters)
82 *states |= new_constants;
83
84 if (prog->info.num_textures)
85 *states |= new_sampler_views | new_samplers;
86
87 if (prog->info.num_images)
88 *states |= new_images;
89
90 if (prog->info.num_ubos)
91 *states |= new_ubos;
92
93 if (prog->info.num_ssbos)
94 *states |= new_ssbos;
95
96 if (prog->info.num_abos)
97 *states |= new_atomics;
98 }
99
100 /**
101 * This determines which states will be updated when the shader is bound.
102 */
103 void
104 st_set_prog_affected_state_flags(struct gl_program *prog)
105 {
106 uint64_t *states;
107
108 switch (prog->info.stage) {
109 case MESA_SHADER_VERTEX:
110 states = &((struct st_program*)prog)->affected_states;
111
112 *states = ST_NEW_VS_STATE |
113 ST_NEW_RASTERIZER |
114 ST_NEW_VERTEX_ARRAYS;
115
116 set_affected_state_flags(states, prog,
117 ST_NEW_VS_CONSTANTS,
118 ST_NEW_VS_SAMPLER_VIEWS,
119 ST_NEW_VS_SAMPLERS,
120 ST_NEW_VS_IMAGES,
121 ST_NEW_VS_UBOS,
122 ST_NEW_VS_SSBOS,
123 ST_NEW_VS_ATOMICS);
124 break;
125
126 case MESA_SHADER_TESS_CTRL:
127 states = &(st_program(prog))->affected_states;
128
129 *states = ST_NEW_TCS_STATE;
130
131 set_affected_state_flags(states, prog,
132 ST_NEW_TCS_CONSTANTS,
133 ST_NEW_TCS_SAMPLER_VIEWS,
134 ST_NEW_TCS_SAMPLERS,
135 ST_NEW_TCS_IMAGES,
136 ST_NEW_TCS_UBOS,
137 ST_NEW_TCS_SSBOS,
138 ST_NEW_TCS_ATOMICS);
139 break;
140
141 case MESA_SHADER_TESS_EVAL:
142 states = &(st_program(prog))->affected_states;
143
144 *states = ST_NEW_TES_STATE |
145 ST_NEW_RASTERIZER;
146
147 set_affected_state_flags(states, prog,
148 ST_NEW_TES_CONSTANTS,
149 ST_NEW_TES_SAMPLER_VIEWS,
150 ST_NEW_TES_SAMPLERS,
151 ST_NEW_TES_IMAGES,
152 ST_NEW_TES_UBOS,
153 ST_NEW_TES_SSBOS,
154 ST_NEW_TES_ATOMICS);
155 break;
156
157 case MESA_SHADER_GEOMETRY:
158 states = &(st_program(prog))->affected_states;
159
160 *states = ST_NEW_GS_STATE |
161 ST_NEW_RASTERIZER;
162
163 set_affected_state_flags(states, prog,
164 ST_NEW_GS_CONSTANTS,
165 ST_NEW_GS_SAMPLER_VIEWS,
166 ST_NEW_GS_SAMPLERS,
167 ST_NEW_GS_IMAGES,
168 ST_NEW_GS_UBOS,
169 ST_NEW_GS_SSBOS,
170 ST_NEW_GS_ATOMICS);
171 break;
172
173 case MESA_SHADER_FRAGMENT:
174 states = &((struct st_program*)prog)->affected_states;
175
176 /* gl_FragCoord and glDrawPixels always use constants. */
177 *states = ST_NEW_FS_STATE |
178 ST_NEW_SAMPLE_SHADING |
179 ST_NEW_FS_CONSTANTS;
180
181 set_affected_state_flags(states, prog,
182 ST_NEW_FS_CONSTANTS,
183 ST_NEW_FS_SAMPLER_VIEWS,
184 ST_NEW_FS_SAMPLERS,
185 ST_NEW_FS_IMAGES,
186 ST_NEW_FS_UBOS,
187 ST_NEW_FS_SSBOS,
188 ST_NEW_FS_ATOMICS);
189 break;
190
191 case MESA_SHADER_COMPUTE:
192 states = &((struct st_program*)prog)->affected_states;
193
194 *states = ST_NEW_CS_STATE;
195
196 set_affected_state_flags(states, prog,
197 ST_NEW_CS_CONSTANTS,
198 ST_NEW_CS_SAMPLER_VIEWS,
199 ST_NEW_CS_SAMPLERS,
200 ST_NEW_CS_IMAGES,
201 ST_NEW_CS_UBOS,
202 ST_NEW_CS_SSBOS,
203 ST_NEW_CS_ATOMICS);
204 break;
205
206 default:
207 unreachable("unhandled shader stage");
208 }
209 }
210
211 static void
212 delete_ir(struct pipe_shader_state *ir)
213 {
214 if (ir->tokens) {
215 ureg_free_tokens(ir->tokens);
216 ir->tokens = NULL;
217 }
218
219 /* Note: Any setup of ->ir.nir that has had pipe->create_*_state called on
220 * it has resulted in the driver taking ownership of the NIR. Those
221 * callers should be NULLing out the nir field in any pipe_shader_state
222 * that might have this called in order to indicate that.
223 *
224 * GLSL IR and ARB programs will have set gl_program->nir to the same
225 * shader as ir->ir.nir, so it will be freed by _mesa_delete_program().
226 */
227 }
228
229 /**
230 * Delete a vertex program variant. Note the caller must unlink
231 * the variant from the linked list.
232 */
233 static void
234 delete_vp_variant(struct st_context *st, struct st_vp_variant *vpv)
235 {
236 if (vpv->driver_shader) {
237 if (st->has_shareable_shaders || vpv->key.st == st) {
238 cso_delete_vertex_shader(st->cso_context, vpv->driver_shader);
239 } else {
240 st_save_zombie_shader(vpv->key.st, PIPE_SHADER_VERTEX,
241 vpv->driver_shader);
242 }
243 }
244
245 if (vpv->draw_shader)
246 draw_delete_vertex_shader( st->draw, vpv->draw_shader );
247
248 if (vpv->tokens)
249 ureg_free_tokens(vpv->tokens);
250
251 free( vpv );
252 }
253
254
255
256 /**
257 * Clean out any old compilations:
258 */
259 void
260 st_release_vp_variants( struct st_context *st,
261 struct st_program *stvp )
262 {
263 struct st_vp_variant *vpv;
264
265 for (vpv = stvp->vp_variants; vpv; ) {
266 struct st_vp_variant *next = vpv->next;
267 delete_vp_variant(st, vpv);
268 vpv = next;
269 }
270
271 stvp->vp_variants = NULL;
272
273 delete_ir(&stvp->state);
274 }
275
276
277
278 /**
279 * Delete a fragment program variant. Note the caller must unlink
280 * the variant from the linked list.
281 */
282 static void
283 delete_fp_variant(struct st_context *st, struct st_fp_variant *fpv)
284 {
285 if (fpv->driver_shader) {
286 if (st->has_shareable_shaders || fpv->key.st == st) {
287 cso_delete_fragment_shader(st->cso_context, fpv->driver_shader);
288 } else {
289 st_save_zombie_shader(fpv->key.st, PIPE_SHADER_FRAGMENT,
290 fpv->driver_shader);
291 }
292 }
293
294 free(fpv);
295 }
296
297
298 /**
299 * Free all variants of a fragment program.
300 */
301 void
302 st_release_fp_variants(struct st_context *st, struct st_program *stfp)
303 {
304 struct st_fp_variant *fpv;
305
306 for (fpv = stfp->fp_variants; fpv; ) {
307 struct st_fp_variant *next = fpv->next;
308 delete_fp_variant(st, fpv);
309 fpv = next;
310 }
311
312 stfp->fp_variants = NULL;
313
314 delete_ir(&stfp->state);
315 }
316
317
318 /**
319 * Delete a basic program variant. Note the caller must unlink
320 * the variant from the linked list.
321 */
322 static void
323 delete_common_variant(struct st_context *st, struct st_common_variant *v,
324 GLenum target)
325 {
326 if (v->driver_shader) {
327 if (st->has_shareable_shaders || v->key.st == st) {
328 /* The shader's context matches the calling context, or we
329 * don't care.
330 */
331 switch (target) {
332 case GL_TESS_CONTROL_PROGRAM_NV:
333 cso_delete_tessctrl_shader(st->cso_context, v->driver_shader);
334 break;
335 case GL_TESS_EVALUATION_PROGRAM_NV:
336 cso_delete_tesseval_shader(st->cso_context, v->driver_shader);
337 break;
338 case GL_GEOMETRY_PROGRAM_NV:
339 cso_delete_geometry_shader(st->cso_context, v->driver_shader);
340 break;
341 case GL_COMPUTE_PROGRAM_NV:
342 cso_delete_compute_shader(st->cso_context, v->driver_shader);
343 break;
344 default:
345 unreachable("bad shader type in delete_basic_variant");
346 }
347 } else {
348 /* We can't delete a shader with a context different from the one
349 * that created it. Add it to the creating context's zombie list.
350 */
351 enum pipe_shader_type type;
352 switch (target) {
353 case GL_TESS_CONTROL_PROGRAM_NV:
354 type = PIPE_SHADER_TESS_CTRL;
355 break;
356 case GL_TESS_EVALUATION_PROGRAM_NV:
357 type = PIPE_SHADER_TESS_EVAL;
358 break;
359 case GL_GEOMETRY_PROGRAM_NV:
360 type = PIPE_SHADER_GEOMETRY;
361 break;
362 default:
363 unreachable("");
364 }
365 st_save_zombie_shader(v->key.st, type, v->driver_shader);
366 }
367 }
368
369 free(v);
370 }
371
372
373 /**
374 * Free all basic program variants.
375 */
376 void
377 st_release_common_variants(struct st_context *st, struct st_program *p)
378 {
379 struct st_common_variant *v;
380
381 for (v = p->variants; v; ) {
382 struct st_common_variant *next = v->next;
383 delete_common_variant(st, v, p->Base.Target);
384 v = next;
385 }
386
387 p->variants = NULL;
388 delete_ir(&p->state);
389 }
390
391 void
392 st_finalize_nir_before_variants(struct nir_shader *nir)
393 {
394 NIR_PASS_V(nir, nir_opt_access);
395
396 NIR_PASS_V(nir, nir_split_var_copies);
397 NIR_PASS_V(nir, nir_lower_var_copies);
398 if (nir->options->lower_all_io_to_temps ||
399 nir->options->lower_all_io_to_elements ||
400 nir->info.stage == MESA_SHADER_VERTEX ||
401 nir->info.stage == MESA_SHADER_GEOMETRY) {
402 NIR_PASS_V(nir, nir_lower_io_arrays_to_elements_no_indirects, false);
403 } else if (nir->info.stage == MESA_SHADER_FRAGMENT) {
404 NIR_PASS_V(nir, nir_lower_io_arrays_to_elements_no_indirects, true);
405 }
406
407 st_nir_assign_vs_in_locations(nir);
408 }
409
410 /**
411 * Translate ARB (asm) program to NIR
412 */
413 static nir_shader *
414 st_translate_prog_to_nir(struct st_context *st, struct gl_program *prog,
415 gl_shader_stage stage)
416 {
417 struct pipe_screen *screen = st->pipe->screen;
418 const struct gl_shader_compiler_options *options =
419 &st->ctx->Const.ShaderCompilerOptions[stage];
420
421 /* Translate to NIR */
422 nir_shader *nir = prog_to_nir(prog, options->NirOptions);
423 NIR_PASS_V(nir, nir_lower_regs_to_ssa); /* turn registers into SSA */
424 nir_validate_shader(nir, "after st/ptn lower_regs_to_ssa");
425
426 NIR_PASS_V(nir, st_nir_lower_wpos_ytransform, prog, screen);
427 NIR_PASS_V(nir, nir_lower_system_values);
428
429 /* Optimise NIR */
430 NIR_PASS_V(nir, nir_opt_constant_folding);
431 st_nir_opts(nir);
432 st_finalize_nir_before_variants(nir);
433
434 if (st->allow_st_finalize_nir_twice)
435 st_finalize_nir(st, prog, NULL, nir, true);
436
437 nir_validate_shader(nir, "after st/glsl finalize_nir");
438
439 return nir;
440 }
441
442 void
443 st_prepare_vertex_program(struct st_program *stp)
444 {
445 struct st_vertex_program *stvp = (struct st_vertex_program *)stp;
446
447 stvp->num_inputs = 0;
448 memset(stvp->input_to_index, ~0, sizeof(stvp->input_to_index));
449 memset(stvp->result_to_output, ~0, sizeof(stvp->result_to_output));
450
451 /* Determine number of inputs, the mappings between VERT_ATTRIB_x
452 * and TGSI generic input indexes, plus input attrib semantic info.
453 */
454 for (unsigned attr = 0; attr < VERT_ATTRIB_MAX; attr++) {
455 if ((stp->Base.info.inputs_read & BITFIELD64_BIT(attr)) != 0) {
456 stvp->input_to_index[attr] = stvp->num_inputs;
457 stvp->index_to_input[stvp->num_inputs] = attr;
458 stvp->num_inputs++;
459
460 if ((stp->Base.DualSlotInputs & BITFIELD64_BIT(attr)) != 0) {
461 /* add placeholder for second part of a double attribute */
462 stvp->index_to_input[stvp->num_inputs] = ST_DOUBLE_ATTRIB_PLACEHOLDER;
463 stvp->num_inputs++;
464 }
465 }
466 }
467 /* pre-setup potentially unused edgeflag input */
468 stvp->input_to_index[VERT_ATTRIB_EDGEFLAG] = stvp->num_inputs;
469 stvp->index_to_input[stvp->num_inputs] = VERT_ATTRIB_EDGEFLAG;
470
471 /* Compute mapping of vertex program outputs to slots. */
472 unsigned num_outputs = 0;
473 for (unsigned attr = 0; attr < VARYING_SLOT_MAX; attr++) {
474 if (stp->Base.info.outputs_written & BITFIELD64_BIT(attr))
475 stvp->result_to_output[attr] = num_outputs++;
476 }
477 /* pre-setup potentially unused edgeflag output */
478 stvp->result_to_output[VARYING_SLOT_EDGE] = num_outputs;
479 }
480
481 void
482 st_translate_stream_output_info(struct gl_program *prog)
483 {
484 struct gl_transform_feedback_info *info = prog->sh.LinkedTransformFeedback;
485 if (!info)
486 return;
487
488 /* Determine the (default) output register mapping for each output. */
489 unsigned num_outputs = 0;
490 ubyte output_mapping[VARYING_SLOT_TESS_MAX];
491 memset(output_mapping, 0, sizeof(output_mapping));
492
493 for (unsigned attr = 0; attr < VARYING_SLOT_MAX; attr++) {
494 if (prog->info.outputs_written & BITFIELD64_BIT(attr))
495 output_mapping[attr] = num_outputs++;
496 }
497
498 /* Translate stream output info. */
499 struct pipe_stream_output_info *so_info =
500 &((struct st_program*)prog)->state.stream_output;
501
502 for (unsigned i = 0; i < info->NumOutputs; i++) {
503 so_info->output[i].register_index =
504 output_mapping[info->Outputs[i].OutputRegister];
505 so_info->output[i].start_component = info->Outputs[i].ComponentOffset;
506 so_info->output[i].num_components = info->Outputs[i].NumComponents;
507 so_info->output[i].output_buffer = info->Outputs[i].OutputBuffer;
508 so_info->output[i].dst_offset = info->Outputs[i].DstOffset;
509 so_info->output[i].stream = info->Outputs[i].StreamId;
510 }
511
512 for (unsigned i = 0; i < PIPE_MAX_SO_BUFFERS; i++) {
513 so_info->stride[i] = info->Buffers[i].Stride;
514 }
515 so_info->num_outputs = info->NumOutputs;
516 }
517
518 /**
519 * Translate a vertex program.
520 */
521 bool
522 st_translate_vertex_program(struct st_context *st,
523 struct st_program *stp)
524 {
525 struct ureg_program *ureg;
526 enum pipe_error error;
527 unsigned num_outputs = 0;
528 unsigned attr;
529 ubyte output_semantic_name[VARYING_SLOT_MAX] = {0};
530 ubyte output_semantic_index[VARYING_SLOT_MAX] = {0};
531
532 if (stp->Base.arb.IsPositionInvariant)
533 _mesa_insert_mvp_code(st->ctx, &stp->Base);
534
535 st_prepare_vertex_program(stp);
536
537 /* ARB_vp: */
538 if (!stp->glsl_to_tgsi) {
539 _mesa_remove_output_reads(&stp->Base, PROGRAM_OUTPUT);
540
541 /* This determines which states will be updated when the assembly
542 * shader is bound.
543 */
544 stp->affected_states = ST_NEW_VS_STATE |
545 ST_NEW_RASTERIZER |
546 ST_NEW_VERTEX_ARRAYS;
547
548 if (stp->Base.Parameters->NumParameters)
549 stp->affected_states |= ST_NEW_VS_CONSTANTS;
550
551 /* No samplers are allowed in ARB_vp. */
552 }
553
554 /* Get semantic names and indices. */
555 for (attr = 0; attr < VARYING_SLOT_MAX; attr++) {
556 if (stp->Base.info.outputs_written & BITFIELD64_BIT(attr)) {
557 unsigned slot = num_outputs++;
558 unsigned semantic_name, semantic_index;
559 tgsi_get_gl_varying_semantic(attr, st->needs_texcoord_semantic,
560 &semantic_name, &semantic_index);
561 output_semantic_name[slot] = semantic_name;
562 output_semantic_index[slot] = semantic_index;
563 }
564 }
565 /* pre-setup potentially unused edgeflag output */
566 output_semantic_name[num_outputs] = TGSI_SEMANTIC_EDGEFLAG;
567 output_semantic_index[num_outputs] = 0;
568
569 ureg = ureg_create_with_screen(PIPE_SHADER_VERTEX, st->pipe->screen);
570 if (ureg == NULL)
571 return false;
572
573 if (stp->Base.info.clip_distance_array_size)
574 ureg_property(ureg, TGSI_PROPERTY_NUM_CLIPDIST_ENABLED,
575 stp->Base.info.clip_distance_array_size);
576 if (stp->Base.info.cull_distance_array_size)
577 ureg_property(ureg, TGSI_PROPERTY_NUM_CULLDIST_ENABLED,
578 stp->Base.info.cull_distance_array_size);
579
580 if (ST_DEBUG & DEBUG_MESA) {
581 _mesa_print_program(&stp->Base);
582 _mesa_print_program_parameters(st->ctx, &stp->Base);
583 debug_printf("\n");
584 }
585
586 struct st_vertex_program *stvp = (struct st_vertex_program *)stp;
587
588 if (stp->glsl_to_tgsi) {
589 error = st_translate_program(st->ctx,
590 PIPE_SHADER_VERTEX,
591 ureg,
592 stp->glsl_to_tgsi,
593 &stp->Base,
594 /* inputs */
595 stvp->num_inputs,
596 stvp->input_to_index,
597 NULL, /* inputSlotToAttr */
598 NULL, /* input semantic name */
599 NULL, /* input semantic index */
600 NULL, /* interp mode */
601 /* outputs */
602 num_outputs,
603 stvp->result_to_output,
604 output_semantic_name,
605 output_semantic_index);
606
607 st_translate_stream_output_info(&stp->Base);
608
609 free_glsl_to_tgsi_visitor(stp->glsl_to_tgsi);
610 } else
611 error = st_translate_mesa_program(st->ctx,
612 PIPE_SHADER_VERTEX,
613 ureg,
614 &stp->Base,
615 /* inputs */
616 stvp->num_inputs,
617 stvp->input_to_index,
618 NULL, /* input semantic name */
619 NULL, /* input semantic index */
620 NULL,
621 /* outputs */
622 num_outputs,
623 stvp->result_to_output,
624 output_semantic_name,
625 output_semantic_index);
626
627 if (error) {
628 debug_printf("%s: failed to translate Mesa program:\n", __func__);
629 _mesa_print_program(&stp->Base);
630 debug_assert(0);
631 return false;
632 }
633
634 stp->state.tokens = ureg_get_tokens(ureg, NULL);
635 ureg_destroy(ureg);
636
637 if (stp->glsl_to_tgsi) {
638 stp->glsl_to_tgsi = NULL;
639 st_store_ir_in_disk_cache(st, &stp->Base, false);
640 }
641
642 /* Translate to NIR.
643 *
644 * This must be done after the translation to TGSI is done, because
645 * we'll pass the NIR shader to the driver and the TGSI version to
646 * the draw module for the select/feedback/rasterpos code.
647 */
648 if (st->pipe->screen->get_shader_param(st->pipe->screen,
649 PIPE_SHADER_VERTEX,
650 PIPE_SHADER_CAP_PREFERRED_IR)) {
651 assert(!stp->glsl_to_tgsi);
652
653 nir_shader *nir =
654 st_translate_prog_to_nir(st, &stp->Base, MESA_SHADER_VERTEX);
655
656 if (stp->state.ir.nir)
657 ralloc_free(stp->state.ir.nir);
658 stp->state.type = PIPE_SHADER_IR_NIR;
659 stp->state.ir.nir = nir;
660 stp->Base.nir = nir;
661 return true;
662 }
663
664 return stp->state.tokens != NULL;
665 }
666
667 static const gl_state_index16 depth_range_state[STATE_LENGTH] =
668 { STATE_DEPTH_RANGE };
669
670 static struct st_vp_variant *
671 st_create_vp_variant(struct st_context *st,
672 struct st_program *stvp,
673 const struct st_common_variant_key *key)
674 {
675 struct st_vp_variant *vpv = CALLOC_STRUCT(st_vp_variant);
676 struct pipe_context *pipe = st->pipe;
677 struct pipe_screen *screen = pipe->screen;
678 struct pipe_shader_state state = {0};
679
680 static const gl_state_index16 point_size_state[STATE_LENGTH] =
681 { STATE_INTERNAL, STATE_POINT_SIZE_CLAMPED, 0 };
682 struct gl_program_parameter_list *params = stvp->Base.Parameters;
683
684 vpv->key = *key;
685 vpv->num_inputs = ((struct st_vertex_program*)stvp)->num_inputs;
686
687 state.stream_output = stvp->state.stream_output;
688
689 if (stvp->state.type == PIPE_SHADER_IR_NIR) {
690 bool finalize = false;
691
692 state.type = PIPE_SHADER_IR_NIR;
693 state.ir.nir = nir_shader_clone(NULL, stvp->state.ir.nir);
694 if (key->clamp_color) {
695 NIR_PASS_V(state.ir.nir, nir_lower_clamp_color_outputs);
696 finalize = true;
697 }
698 if (key->passthrough_edgeflags) {
699 NIR_PASS_V(state.ir.nir, nir_lower_passthrough_edgeflags);
700 vpv->num_inputs++;
701 finalize = true;
702 }
703
704 if (key->lower_point_size) {
705 _mesa_add_state_reference(params, point_size_state);
706 NIR_PASS_V(state.ir.nir, nir_lower_point_size_mov,
707 point_size_state);
708 finalize = true;
709 }
710
711 if (key->lower_ucp) {
712 bool can_compact = screen->get_param(screen,
713 PIPE_CAP_NIR_COMPACT_ARRAYS);
714
715 bool use_eye = st->ctx->_Shader->CurrentProgram[MESA_SHADER_VERTEX] != NULL;
716 gl_state_index16 clipplane_state[MAX_CLIP_PLANES][STATE_LENGTH];
717 for (int i = 0; i < MAX_CLIP_PLANES; ++i) {
718 if (use_eye) {
719 clipplane_state[i][0] = STATE_CLIPPLANE;
720 clipplane_state[i][1] = i;
721 } else {
722 clipplane_state[i][0] = STATE_INTERNAL;
723 clipplane_state[i][1] = STATE_CLIP_INTERNAL;
724 clipplane_state[i][2] = i;
725 }
726 _mesa_add_state_reference(params, clipplane_state[i]);
727 }
728
729 NIR_PASS_V(state.ir.nir, nir_lower_clip_vs, key->lower_ucp,
730 true, can_compact, clipplane_state);
731 NIR_PASS_V(state.ir.nir, nir_lower_io_to_temporaries,
732 nir_shader_get_entrypoint(state.ir.nir), true, false);
733 NIR_PASS_V(state.ir.nir, nir_lower_global_vars_to_local);
734 finalize = true;
735 }
736
737 if (finalize || !st->allow_st_finalize_nir_twice) {
738 st_finalize_nir(st, &stvp->Base, stvp->shader_program, state.ir.nir,
739 true);
740
741 /* Some of the lowering above may have introduced new varyings */
742 nir_shader_gather_info(state.ir.nir,
743 nir_shader_get_entrypoint(state.ir.nir));
744 }
745
746 if (ST_DEBUG & DEBUG_PRINT_IR)
747 nir_print_shader(state.ir.nir, stderr);
748
749 vpv->driver_shader = pipe->create_vs_state(pipe, &state);
750
751 /* When generating a NIR program, we usually don't have TGSI tokens.
752 * However, we do create them for ARB_vertex_program / fixed-function VS
753 * programs which we may need to use with the draw module for legacy
754 * feedback/select emulation. If they exist, copy them.
755 *
756 * TODO: Lowering for shader variants is not applied to TGSI when
757 * generating a NIR shader.
758 */
759 if (stvp->state.tokens)
760 vpv->tokens = tgsi_dup_tokens(stvp->state.tokens);
761
762 return vpv;
763 }
764
765 state.type = PIPE_SHADER_IR_TGSI;
766 state.tokens = tgsi_dup_tokens(stvp->state.tokens);
767
768 /* Emulate features. */
769 if (key->clamp_color || key->passthrough_edgeflags) {
770 const struct tgsi_token *tokens;
771 unsigned flags =
772 (key->clamp_color ? TGSI_EMU_CLAMP_COLOR_OUTPUTS : 0) |
773 (key->passthrough_edgeflags ? TGSI_EMU_PASSTHROUGH_EDGEFLAG : 0);
774
775 tokens = tgsi_emulate(state.tokens, flags);
776
777 if (tokens) {
778 tgsi_free_tokens(state.tokens);
779 state.tokens = tokens;
780
781 if (key->passthrough_edgeflags)
782 vpv->num_inputs++;
783 } else
784 fprintf(stderr, "mesa: cannot emulate deprecated features\n");
785 }
786
787 if (key->lower_depth_clamp) {
788 unsigned depth_range_const =
789 _mesa_add_state_reference(params, depth_range_state);
790
791 const struct tgsi_token *tokens;
792 tokens = st_tgsi_lower_depth_clamp(state.tokens, depth_range_const,
793 key->clip_negative_one_to_one);
794 if (tokens != state.tokens)
795 tgsi_free_tokens(state.tokens);
796 state.tokens = tokens;
797 }
798
799 if (ST_DEBUG & DEBUG_PRINT_IR)
800 tgsi_dump(state.tokens, 0);
801
802 vpv->driver_shader = pipe->create_vs_state(pipe, &state);
803 /* Save this for selection/feedback/rasterpos. */
804 vpv->tokens = state.tokens;
805 return vpv;
806 }
807
808
809 /**
810 * Find/create a vertex program variant.
811 */
812 struct st_vp_variant *
813 st_get_vp_variant(struct st_context *st,
814 struct st_program *stp,
815 const struct st_common_variant_key *key)
816 {
817 struct st_vertex_program *stvp = (struct st_vertex_program *)stp;
818 struct st_vp_variant *vpv;
819
820 /* Search for existing variant */
821 for (vpv = stp->vp_variants; vpv; vpv = vpv->next) {
822 if (memcmp(&vpv->key, key, sizeof(*key)) == 0) {
823 break;
824 }
825 }
826
827 if (!vpv) {
828 /* create now */
829 vpv = st_create_vp_variant(st, stp, key);
830 if (vpv) {
831 for (unsigned index = 0; index < vpv->num_inputs; ++index) {
832 unsigned attr = stvp->index_to_input[index];
833 if (attr == ST_DOUBLE_ATTRIB_PLACEHOLDER)
834 continue;
835 vpv->vert_attrib_mask |= 1u << attr;
836 }
837
838 /* insert into list */
839 vpv->next = stp->vp_variants;
840 stp->vp_variants = vpv;
841 }
842 }
843
844 return vpv;
845 }
846
847
848 /**
849 * Translate a Mesa fragment shader into a TGSI shader.
850 */
851 bool
852 st_translate_fragment_program(struct st_context *st,
853 struct st_program *stfp)
854 {
855 /* Non-GLSL programs: */
856 if (!stfp->glsl_to_tgsi) {
857 _mesa_remove_output_reads(&stfp->Base, PROGRAM_OUTPUT);
858 if (st->ctx->Const.GLSLFragCoordIsSysVal)
859 _mesa_program_fragment_position_to_sysval(&stfp->Base);
860
861 /* This determines which states will be updated when the assembly
862 * shader is bound.
863 *
864 * fragment.position and glDrawPixels always use constants.
865 */
866 stfp->affected_states = ST_NEW_FS_STATE |
867 ST_NEW_SAMPLE_SHADING |
868 ST_NEW_FS_CONSTANTS;
869
870 if (stfp->ati_fs) {
871 /* Just set them for ATI_fs unconditionally. */
872 stfp->affected_states |= ST_NEW_FS_SAMPLER_VIEWS |
873 ST_NEW_FS_SAMPLERS;
874 } else {
875 /* ARB_fp */
876 if (stfp->Base.SamplersUsed)
877 stfp->affected_states |= ST_NEW_FS_SAMPLER_VIEWS |
878 ST_NEW_FS_SAMPLERS;
879 }
880
881 /* Translate to NIR. */
882 if (!stfp->ati_fs &&
883 st->pipe->screen->get_shader_param(st->pipe->screen,
884 PIPE_SHADER_FRAGMENT,
885 PIPE_SHADER_CAP_PREFERRED_IR)) {
886 nir_shader *nir =
887 st_translate_prog_to_nir(st, &stfp->Base, MESA_SHADER_FRAGMENT);
888
889 if (stfp->state.ir.nir)
890 ralloc_free(stfp->state.ir.nir);
891 stfp->state.type = PIPE_SHADER_IR_NIR;
892 stfp->state.ir.nir = nir;
893 stfp->Base.nir = nir;
894 return true;
895 }
896 }
897
898 ubyte outputMapping[2 * FRAG_RESULT_MAX];
899 ubyte inputMapping[VARYING_SLOT_MAX];
900 ubyte inputSlotToAttr[VARYING_SLOT_MAX];
901 ubyte interpMode[PIPE_MAX_SHADER_INPUTS]; /* XXX size? */
902 GLuint attr;
903 GLbitfield64 inputsRead;
904 struct ureg_program *ureg;
905
906 GLboolean write_all = GL_FALSE;
907
908 ubyte input_semantic_name[PIPE_MAX_SHADER_INPUTS];
909 ubyte input_semantic_index[PIPE_MAX_SHADER_INPUTS];
910 uint fs_num_inputs = 0;
911
912 ubyte fs_output_semantic_name[PIPE_MAX_SHADER_OUTPUTS];
913 ubyte fs_output_semantic_index[PIPE_MAX_SHADER_OUTPUTS];
914 uint fs_num_outputs = 0;
915
916 memset(inputSlotToAttr, ~0, sizeof(inputSlotToAttr));
917
918 /*
919 * Convert Mesa program inputs to TGSI input register semantics.
920 */
921 inputsRead = stfp->Base.info.inputs_read;
922 for (attr = 0; attr < VARYING_SLOT_MAX; attr++) {
923 if ((inputsRead & BITFIELD64_BIT(attr)) != 0) {
924 const GLuint slot = fs_num_inputs++;
925
926 inputMapping[attr] = slot;
927 inputSlotToAttr[slot] = attr;
928
929 switch (attr) {
930 case VARYING_SLOT_POS:
931 input_semantic_name[slot] = TGSI_SEMANTIC_POSITION;
932 input_semantic_index[slot] = 0;
933 interpMode[slot] = TGSI_INTERPOLATE_LINEAR;
934 break;
935 case VARYING_SLOT_COL0:
936 input_semantic_name[slot] = TGSI_SEMANTIC_COLOR;
937 input_semantic_index[slot] = 0;
938 interpMode[slot] = stfp->glsl_to_tgsi ?
939 TGSI_INTERPOLATE_COUNT : TGSI_INTERPOLATE_COLOR;
940 break;
941 case VARYING_SLOT_COL1:
942 input_semantic_name[slot] = TGSI_SEMANTIC_COLOR;
943 input_semantic_index[slot] = 1;
944 interpMode[slot] = stfp->glsl_to_tgsi ?
945 TGSI_INTERPOLATE_COUNT : TGSI_INTERPOLATE_COLOR;
946 break;
947 case VARYING_SLOT_FOGC:
948 input_semantic_name[slot] = TGSI_SEMANTIC_FOG;
949 input_semantic_index[slot] = 0;
950 interpMode[slot] = TGSI_INTERPOLATE_PERSPECTIVE;
951 break;
952 case VARYING_SLOT_FACE:
953 input_semantic_name[slot] = TGSI_SEMANTIC_FACE;
954 input_semantic_index[slot] = 0;
955 interpMode[slot] = TGSI_INTERPOLATE_CONSTANT;
956 break;
957 case VARYING_SLOT_PRIMITIVE_ID:
958 input_semantic_name[slot] = TGSI_SEMANTIC_PRIMID;
959 input_semantic_index[slot] = 0;
960 interpMode[slot] = TGSI_INTERPOLATE_CONSTANT;
961 break;
962 case VARYING_SLOT_LAYER:
963 input_semantic_name[slot] = TGSI_SEMANTIC_LAYER;
964 input_semantic_index[slot] = 0;
965 interpMode[slot] = TGSI_INTERPOLATE_CONSTANT;
966 break;
967 case VARYING_SLOT_VIEWPORT:
968 input_semantic_name[slot] = TGSI_SEMANTIC_VIEWPORT_INDEX;
969 input_semantic_index[slot] = 0;
970 interpMode[slot] = TGSI_INTERPOLATE_CONSTANT;
971 break;
972 case VARYING_SLOT_CLIP_DIST0:
973 input_semantic_name[slot] = TGSI_SEMANTIC_CLIPDIST;
974 input_semantic_index[slot] = 0;
975 interpMode[slot] = TGSI_INTERPOLATE_PERSPECTIVE;
976 break;
977 case VARYING_SLOT_CLIP_DIST1:
978 input_semantic_name[slot] = TGSI_SEMANTIC_CLIPDIST;
979 input_semantic_index[slot] = 1;
980 interpMode[slot] = TGSI_INTERPOLATE_PERSPECTIVE;
981 break;
982 case VARYING_SLOT_CULL_DIST0:
983 case VARYING_SLOT_CULL_DIST1:
984 /* these should have been lowered by GLSL */
985 assert(0);
986 break;
987 /* In most cases, there is nothing special about these
988 * inputs, so adopt a convention to use the generic
989 * semantic name and the mesa VARYING_SLOT_ number as the
990 * index.
991 *
992 * All that is required is that the vertex shader labels
993 * its own outputs similarly, and that the vertex shader
994 * generates at least every output required by the
995 * fragment shader plus fixed-function hardware (such as
996 * BFC).
997 *
998 * However, some drivers may need us to identify the PNTC and TEXi
999 * varyings if, for example, their capability to replace them with
1000 * sprite coordinates is limited.
1001 */
1002 case VARYING_SLOT_PNTC:
1003 if (st->needs_texcoord_semantic) {
1004 input_semantic_name[slot] = TGSI_SEMANTIC_PCOORD;
1005 input_semantic_index[slot] = 0;
1006 interpMode[slot] = TGSI_INTERPOLATE_LINEAR;
1007 break;
1008 }
1009 /* fall through */
1010 case VARYING_SLOT_TEX0:
1011 case VARYING_SLOT_TEX1:
1012 case VARYING_SLOT_TEX2:
1013 case VARYING_SLOT_TEX3:
1014 case VARYING_SLOT_TEX4:
1015 case VARYING_SLOT_TEX5:
1016 case VARYING_SLOT_TEX6:
1017 case VARYING_SLOT_TEX7:
1018 if (st->needs_texcoord_semantic) {
1019 input_semantic_name[slot] = TGSI_SEMANTIC_TEXCOORD;
1020 input_semantic_index[slot] = attr - VARYING_SLOT_TEX0;
1021 interpMode[slot] = stfp->glsl_to_tgsi ?
1022 TGSI_INTERPOLATE_COUNT : TGSI_INTERPOLATE_PERSPECTIVE;
1023 break;
1024 }
1025 /* fall through */
1026 case VARYING_SLOT_VAR0:
1027 default:
1028 /* Semantic indices should be zero-based because drivers may choose
1029 * to assign a fixed slot determined by that index.
1030 * This is useful because ARB_separate_shader_objects uses location
1031 * qualifiers for linkage, and if the semantic index corresponds to
1032 * these locations, linkage passes in the driver become unecessary.
1033 *
1034 * If needs_texcoord_semantic is true, no semantic indices will be
1035 * consumed for the TEXi varyings, and we can base the locations of
1036 * the user varyings on VAR0. Otherwise, we use TEX0 as base index.
1037 */
1038 assert(attr >= VARYING_SLOT_VAR0 || attr == VARYING_SLOT_PNTC ||
1039 (attr >= VARYING_SLOT_TEX0 && attr <= VARYING_SLOT_TEX7));
1040 input_semantic_name[slot] = TGSI_SEMANTIC_GENERIC;
1041 input_semantic_index[slot] = st_get_generic_varying_index(st, attr);
1042 if (attr == VARYING_SLOT_PNTC)
1043 interpMode[slot] = TGSI_INTERPOLATE_LINEAR;
1044 else {
1045 interpMode[slot] = stfp->glsl_to_tgsi ?
1046 TGSI_INTERPOLATE_COUNT : TGSI_INTERPOLATE_PERSPECTIVE;
1047 }
1048 break;
1049 }
1050 }
1051 else {
1052 inputMapping[attr] = -1;
1053 }
1054 }
1055
1056 /*
1057 * Semantics and mapping for outputs
1058 */
1059 GLbitfield64 outputsWritten = stfp->Base.info.outputs_written;
1060
1061 /* if z is written, emit that first */
1062 if (outputsWritten & BITFIELD64_BIT(FRAG_RESULT_DEPTH)) {
1063 fs_output_semantic_name[fs_num_outputs] = TGSI_SEMANTIC_POSITION;
1064 fs_output_semantic_index[fs_num_outputs] = 0;
1065 outputMapping[FRAG_RESULT_DEPTH] = fs_num_outputs;
1066 fs_num_outputs++;
1067 outputsWritten &= ~(1 << FRAG_RESULT_DEPTH);
1068 }
1069
1070 if (outputsWritten & BITFIELD64_BIT(FRAG_RESULT_STENCIL)) {
1071 fs_output_semantic_name[fs_num_outputs] = TGSI_SEMANTIC_STENCIL;
1072 fs_output_semantic_index[fs_num_outputs] = 0;
1073 outputMapping[FRAG_RESULT_STENCIL] = fs_num_outputs;
1074 fs_num_outputs++;
1075 outputsWritten &= ~(1 << FRAG_RESULT_STENCIL);
1076 }
1077
1078 if (outputsWritten & BITFIELD64_BIT(FRAG_RESULT_SAMPLE_MASK)) {
1079 fs_output_semantic_name[fs_num_outputs] = TGSI_SEMANTIC_SAMPLEMASK;
1080 fs_output_semantic_index[fs_num_outputs] = 0;
1081 outputMapping[FRAG_RESULT_SAMPLE_MASK] = fs_num_outputs;
1082 fs_num_outputs++;
1083 outputsWritten &= ~(1 << FRAG_RESULT_SAMPLE_MASK);
1084 }
1085
1086 /* handle remaining outputs (color) */
1087 for (attr = 0; attr < ARRAY_SIZE(outputMapping); attr++) {
1088 const GLbitfield64 written = attr < FRAG_RESULT_MAX ? outputsWritten :
1089 stfp->Base.SecondaryOutputsWritten;
1090 const unsigned loc = attr % FRAG_RESULT_MAX;
1091
1092 if (written & BITFIELD64_BIT(loc)) {
1093 switch (loc) {
1094 case FRAG_RESULT_DEPTH:
1095 case FRAG_RESULT_STENCIL:
1096 case FRAG_RESULT_SAMPLE_MASK:
1097 /* handled above */
1098 assert(0);
1099 break;
1100 case FRAG_RESULT_COLOR:
1101 write_all = GL_TRUE; /* fallthrough */
1102 default: {
1103 int index;
1104 assert(loc == FRAG_RESULT_COLOR ||
1105 (FRAG_RESULT_DATA0 <= loc && loc < FRAG_RESULT_MAX));
1106
1107 index = (loc == FRAG_RESULT_COLOR) ? 0 : (loc - FRAG_RESULT_DATA0);
1108
1109 if (attr >= FRAG_RESULT_MAX) {
1110 /* Secondary color for dual source blending. */
1111 assert(index == 0);
1112 index++;
1113 }
1114
1115 fs_output_semantic_name[fs_num_outputs] = TGSI_SEMANTIC_COLOR;
1116 fs_output_semantic_index[fs_num_outputs] = index;
1117 outputMapping[attr] = fs_num_outputs;
1118 break;
1119 }
1120 }
1121
1122 fs_num_outputs++;
1123 }
1124 }
1125
1126 ureg = ureg_create_with_screen(PIPE_SHADER_FRAGMENT, st->pipe->screen);
1127 if (ureg == NULL)
1128 return false;
1129
1130 if (ST_DEBUG & DEBUG_MESA) {
1131 _mesa_print_program(&stfp->Base);
1132 _mesa_print_program_parameters(st->ctx, &stfp->Base);
1133 debug_printf("\n");
1134 }
1135 if (write_all == GL_TRUE)
1136 ureg_property(ureg, TGSI_PROPERTY_FS_COLOR0_WRITES_ALL_CBUFS, 1);
1137
1138 if (stfp->Base.info.fs.depth_layout != FRAG_DEPTH_LAYOUT_NONE) {
1139 switch (stfp->Base.info.fs.depth_layout) {
1140 case FRAG_DEPTH_LAYOUT_ANY:
1141 ureg_property(ureg, TGSI_PROPERTY_FS_DEPTH_LAYOUT,
1142 TGSI_FS_DEPTH_LAYOUT_ANY);
1143 break;
1144 case FRAG_DEPTH_LAYOUT_GREATER:
1145 ureg_property(ureg, TGSI_PROPERTY_FS_DEPTH_LAYOUT,
1146 TGSI_FS_DEPTH_LAYOUT_GREATER);
1147 break;
1148 case FRAG_DEPTH_LAYOUT_LESS:
1149 ureg_property(ureg, TGSI_PROPERTY_FS_DEPTH_LAYOUT,
1150 TGSI_FS_DEPTH_LAYOUT_LESS);
1151 break;
1152 case FRAG_DEPTH_LAYOUT_UNCHANGED:
1153 ureg_property(ureg, TGSI_PROPERTY_FS_DEPTH_LAYOUT,
1154 TGSI_FS_DEPTH_LAYOUT_UNCHANGED);
1155 break;
1156 default:
1157 assert(0);
1158 }
1159 }
1160
1161 if (stfp->glsl_to_tgsi) {
1162 st_translate_program(st->ctx,
1163 PIPE_SHADER_FRAGMENT,
1164 ureg,
1165 stfp->glsl_to_tgsi,
1166 &stfp->Base,
1167 /* inputs */
1168 fs_num_inputs,
1169 inputMapping,
1170 inputSlotToAttr,
1171 input_semantic_name,
1172 input_semantic_index,
1173 interpMode,
1174 /* outputs */
1175 fs_num_outputs,
1176 outputMapping,
1177 fs_output_semantic_name,
1178 fs_output_semantic_index);
1179
1180 free_glsl_to_tgsi_visitor(stfp->glsl_to_tgsi);
1181 } else if (stfp->ati_fs)
1182 st_translate_atifs_program(ureg,
1183 stfp->ati_fs,
1184 &stfp->Base,
1185 /* inputs */
1186 fs_num_inputs,
1187 inputMapping,
1188 input_semantic_name,
1189 input_semantic_index,
1190 interpMode,
1191 /* outputs */
1192 fs_num_outputs,
1193 outputMapping,
1194 fs_output_semantic_name,
1195 fs_output_semantic_index);
1196 else
1197 st_translate_mesa_program(st->ctx,
1198 PIPE_SHADER_FRAGMENT,
1199 ureg,
1200 &stfp->Base,
1201 /* inputs */
1202 fs_num_inputs,
1203 inputMapping,
1204 input_semantic_name,
1205 input_semantic_index,
1206 interpMode,
1207 /* outputs */
1208 fs_num_outputs,
1209 outputMapping,
1210 fs_output_semantic_name,
1211 fs_output_semantic_index);
1212
1213 stfp->state.tokens = ureg_get_tokens(ureg, NULL);
1214 ureg_destroy(ureg);
1215
1216 if (stfp->glsl_to_tgsi) {
1217 stfp->glsl_to_tgsi = NULL;
1218 st_store_ir_in_disk_cache(st, &stfp->Base, false);
1219 }
1220
1221 return stfp->state.tokens != NULL;
1222 }
1223
1224 static struct st_fp_variant *
1225 st_create_fp_variant(struct st_context *st,
1226 struct st_program *stfp,
1227 const struct st_fp_variant_key *key)
1228 {
1229 struct pipe_context *pipe = st->pipe;
1230 struct st_fp_variant *variant = CALLOC_STRUCT(st_fp_variant);
1231 struct pipe_shader_state state = {0};
1232 struct gl_program_parameter_list *params = stfp->Base.Parameters;
1233 static const gl_state_index16 texcoord_state[STATE_LENGTH] =
1234 { STATE_INTERNAL, STATE_CURRENT_ATTRIB, VERT_ATTRIB_TEX0 };
1235 static const gl_state_index16 scale_state[STATE_LENGTH] =
1236 { STATE_INTERNAL, STATE_PT_SCALE };
1237 static const gl_state_index16 bias_state[STATE_LENGTH] =
1238 { STATE_INTERNAL, STATE_PT_BIAS };
1239 static const gl_state_index16 alpha_ref_state[STATE_LENGTH] =
1240 { STATE_INTERNAL, STATE_ALPHA_REF };
1241
1242 if (!variant)
1243 return NULL;
1244
1245 if (stfp->state.type == PIPE_SHADER_IR_NIR) {
1246 bool finalize = false;
1247
1248 state.type = PIPE_SHADER_IR_NIR;
1249 state.ir.nir = nir_shader_clone(NULL, stfp->state.ir.nir);
1250
1251 if (key->clamp_color) {
1252 NIR_PASS_V(state.ir.nir, nir_lower_clamp_color_outputs);
1253 finalize = true;
1254 }
1255
1256 if (key->lower_flatshade) {
1257 NIR_PASS_V(state.ir.nir, nir_lower_flatshade);
1258 finalize = true;
1259 }
1260
1261 if (key->lower_alpha_func != COMPARE_FUNC_NEVER) {
1262 _mesa_add_state_reference(params, alpha_ref_state);
1263 NIR_PASS_V(state.ir.nir, nir_lower_alpha_test, key->lower_alpha_func,
1264 false, alpha_ref_state);
1265 finalize = true;
1266 }
1267
1268 if (key->lower_two_sided_color) {
1269 NIR_PASS_V(state.ir.nir, nir_lower_two_sided_color);
1270 finalize = true;
1271 }
1272
1273 if (key->persample_shading) {
1274 nir_shader *shader = state.ir.nir;
1275 nir_foreach_variable(var, &shader->inputs)
1276 var->data.sample = true;
1277 finalize = true;
1278 }
1279
1280 assert(!(key->bitmap && key->drawpixels));
1281
1282 /* glBitmap */
1283 if (key->bitmap) {
1284 nir_lower_bitmap_options options = {0};
1285
1286 variant->bitmap_sampler = ffs(~stfp->Base.SamplersUsed) - 1;
1287 options.sampler = variant->bitmap_sampler;
1288 options.swizzle_xxxx = st->bitmap.tex_format == PIPE_FORMAT_R8_UNORM;
1289
1290 NIR_PASS_V(state.ir.nir, nir_lower_bitmap, &options);
1291 finalize = true;
1292 }
1293
1294 /* glDrawPixels (color only) */
1295 if (key->drawpixels) {
1296 nir_lower_drawpixels_options options = {{0}};
1297 unsigned samplers_used = stfp->Base.SamplersUsed;
1298
1299 /* Find the first unused slot. */
1300 variant->drawpix_sampler = ffs(~samplers_used) - 1;
1301 options.drawpix_sampler = variant->drawpix_sampler;
1302 samplers_used |= (1 << variant->drawpix_sampler);
1303
1304 options.pixel_maps = key->pixelMaps;
1305 if (key->pixelMaps) {
1306 variant->pixelmap_sampler = ffs(~samplers_used) - 1;
1307 options.pixelmap_sampler = variant->pixelmap_sampler;
1308 }
1309
1310 options.scale_and_bias = key->scaleAndBias;
1311 if (key->scaleAndBias) {
1312 _mesa_add_state_reference(params, scale_state);
1313 memcpy(options.scale_state_tokens, scale_state,
1314 sizeof(options.scale_state_tokens));
1315 _mesa_add_state_reference(params, bias_state);
1316 memcpy(options.bias_state_tokens, bias_state,
1317 sizeof(options.bias_state_tokens));
1318 }
1319
1320 _mesa_add_state_reference(params, texcoord_state);
1321 memcpy(options.texcoord_state_tokens, texcoord_state,
1322 sizeof(options.texcoord_state_tokens));
1323
1324 NIR_PASS_V(state.ir.nir, nir_lower_drawpixels, &options);
1325 finalize = true;
1326 }
1327
1328 if (unlikely(key->external.lower_nv12 || key->external.lower_iyuv ||
1329 key->external.lower_xy_uxvx || key->external.lower_yx_xuxv ||
1330 key->external.lower_ayuv || key->external.lower_xyuv)) {
1331 nir_lower_tex_options options = {0};
1332 options.lower_y_uv_external = key->external.lower_nv12;
1333 options.lower_y_u_v_external = key->external.lower_iyuv;
1334 options.lower_xy_uxvx_external = key->external.lower_xy_uxvx;
1335 options.lower_yx_xuxv_external = key->external.lower_yx_xuxv;
1336 options.lower_ayuv_external = key->external.lower_ayuv;
1337 options.lower_xyuv_external = key->external.lower_xyuv;
1338 NIR_PASS_V(state.ir.nir, nir_lower_tex, &options);
1339 finalize = true;
1340 }
1341
1342 if (finalize || !st->allow_st_finalize_nir_twice) {
1343 st_finalize_nir(st, &stfp->Base, stfp->shader_program, state.ir.nir,
1344 false);
1345 }
1346
1347 /* This pass needs to happen *after* nir_lower_sampler */
1348 if (unlikely(key->external.lower_nv12 || key->external.lower_iyuv ||
1349 key->external.lower_xy_uxvx || key->external.lower_yx_xuxv)) {
1350 NIR_PASS_V(state.ir.nir, st_nir_lower_tex_src_plane,
1351 ~stfp->Base.SamplersUsed,
1352 key->external.lower_nv12 || key->external.lower_xy_uxvx ||
1353 key->external.lower_yx_xuxv,
1354 key->external.lower_iyuv);
1355 finalize = true;
1356 }
1357
1358 if (finalize || !st->allow_st_finalize_nir_twice) {
1359 /* Some of the lowering above may have introduced new varyings */
1360 nir_shader_gather_info(state.ir.nir,
1361 nir_shader_get_entrypoint(state.ir.nir));
1362
1363 struct pipe_screen *screen = pipe->screen;
1364 if (screen->finalize_nir)
1365 screen->finalize_nir(screen, state.ir.nir, false);
1366 }
1367
1368 if (ST_DEBUG & DEBUG_PRINT_IR)
1369 nir_print_shader(state.ir.nir, stderr);
1370
1371 variant->driver_shader = pipe->create_fs_state(pipe, &state);
1372 variant->key = *key;
1373
1374 return variant;
1375 }
1376
1377 state.tokens = stfp->state.tokens;
1378
1379 assert(!(key->bitmap && key->drawpixels));
1380
1381 /* Fix texture targets and add fog for ATI_fs */
1382 if (stfp->ati_fs) {
1383 const struct tgsi_token *tokens = st_fixup_atifs(state.tokens, key);
1384
1385 if (tokens)
1386 state.tokens = tokens;
1387 else
1388 fprintf(stderr, "mesa: cannot post-process ATI_fs\n");
1389 }
1390
1391 /* Emulate features. */
1392 if (key->clamp_color || key->persample_shading) {
1393 const struct tgsi_token *tokens;
1394 unsigned flags =
1395 (key->clamp_color ? TGSI_EMU_CLAMP_COLOR_OUTPUTS : 0) |
1396 (key->persample_shading ? TGSI_EMU_FORCE_PERSAMPLE_INTERP : 0);
1397
1398 tokens = tgsi_emulate(state.tokens, flags);
1399
1400 if (tokens) {
1401 if (state.tokens != stfp->state.tokens)
1402 tgsi_free_tokens(state.tokens);
1403 state.tokens = tokens;
1404 } else
1405 fprintf(stderr, "mesa: cannot emulate deprecated features\n");
1406 }
1407
1408 /* glBitmap */
1409 if (key->bitmap) {
1410 const struct tgsi_token *tokens;
1411
1412 variant->bitmap_sampler = ffs(~stfp->Base.SamplersUsed) - 1;
1413
1414 tokens = st_get_bitmap_shader(state.tokens,
1415 st->internal_target,
1416 variant->bitmap_sampler,
1417 st->needs_texcoord_semantic,
1418 st->bitmap.tex_format ==
1419 PIPE_FORMAT_R8_UNORM);
1420
1421 if (tokens) {
1422 if (state.tokens != stfp->state.tokens)
1423 tgsi_free_tokens(state.tokens);
1424 state.tokens = tokens;
1425 } else
1426 fprintf(stderr, "mesa: cannot create a shader for glBitmap\n");
1427 }
1428
1429 /* glDrawPixels (color only) */
1430 if (key->drawpixels) {
1431 const struct tgsi_token *tokens;
1432 unsigned scale_const = 0, bias_const = 0, texcoord_const = 0;
1433
1434 /* Find the first unused slot. */
1435 variant->drawpix_sampler = ffs(~stfp->Base.SamplersUsed) - 1;
1436
1437 if (key->pixelMaps) {
1438 unsigned samplers_used = stfp->Base.SamplersUsed |
1439 (1 << variant->drawpix_sampler);
1440
1441 variant->pixelmap_sampler = ffs(~samplers_used) - 1;
1442 }
1443
1444 if (key->scaleAndBias) {
1445 scale_const = _mesa_add_state_reference(params, scale_state);
1446 bias_const = _mesa_add_state_reference(params, bias_state);
1447 }
1448
1449 texcoord_const = _mesa_add_state_reference(params, texcoord_state);
1450
1451 tokens = st_get_drawpix_shader(state.tokens,
1452 st->needs_texcoord_semantic,
1453 key->scaleAndBias, scale_const,
1454 bias_const, key->pixelMaps,
1455 variant->drawpix_sampler,
1456 variant->pixelmap_sampler,
1457 texcoord_const, st->internal_target);
1458
1459 if (tokens) {
1460 if (state.tokens != stfp->state.tokens)
1461 tgsi_free_tokens(state.tokens);
1462 state.tokens = tokens;
1463 } else
1464 fprintf(stderr, "mesa: cannot create a shader for glDrawPixels\n");
1465 }
1466
1467 if (unlikely(key->external.lower_nv12 || key->external.lower_iyuv ||
1468 key->external.lower_xy_uxvx || key->external.lower_yx_xuxv)) {
1469 const struct tgsi_token *tokens;
1470
1471 /* samplers inserted would conflict, but this should be unpossible: */
1472 assert(!(key->bitmap || key->drawpixels));
1473
1474 tokens = st_tgsi_lower_yuv(state.tokens,
1475 ~stfp->Base.SamplersUsed,
1476 key->external.lower_nv12 ||
1477 key->external.lower_xy_uxvx ||
1478 key->external.lower_yx_xuxv,
1479 key->external.lower_iyuv);
1480 if (tokens) {
1481 if (state.tokens != stfp->state.tokens)
1482 tgsi_free_tokens(state.tokens);
1483 state.tokens = tokens;
1484 } else {
1485 fprintf(stderr, "mesa: cannot create a shader for samplerExternalOES\n");
1486 }
1487 }
1488
1489 if (key->lower_depth_clamp) {
1490 unsigned depth_range_const = _mesa_add_state_reference(params, depth_range_state);
1491
1492 const struct tgsi_token *tokens;
1493 tokens = st_tgsi_lower_depth_clamp_fs(state.tokens, depth_range_const);
1494 if (state.tokens != stfp->state.tokens)
1495 tgsi_free_tokens(state.tokens);
1496 state.tokens = tokens;
1497 }
1498
1499 if (ST_DEBUG & DEBUG_PRINT_IR)
1500 tgsi_dump(state.tokens, 0);
1501
1502 /* fill in variant */
1503 variant->driver_shader = pipe->create_fs_state(pipe, &state);
1504 variant->key = *key;
1505
1506 if (state.tokens != stfp->state.tokens)
1507 tgsi_free_tokens(state.tokens);
1508 return variant;
1509 }
1510
1511 /**
1512 * Translate fragment program if needed.
1513 */
1514 struct st_fp_variant *
1515 st_get_fp_variant(struct st_context *st,
1516 struct st_program *stfp,
1517 const struct st_fp_variant_key *key)
1518 {
1519 struct st_fp_variant *fpv;
1520
1521 /* Search for existing variant */
1522 for (fpv = stfp->fp_variants; fpv; fpv = fpv->next) {
1523 if (memcmp(&fpv->key, key, sizeof(*key)) == 0) {
1524 break;
1525 }
1526 }
1527
1528 if (!fpv) {
1529 /* create new */
1530 fpv = st_create_fp_variant(st, stfp, key);
1531 if (fpv) {
1532 if (key->bitmap || key->drawpixels) {
1533 /* Regular variants should always come before the
1534 * bitmap & drawpixels variants, (unless there
1535 * are no regular variants) so that
1536 * st_update_fp can take a fast path when
1537 * shader_has_one_variant is set.
1538 */
1539 if (!stfp->fp_variants) {
1540 stfp->fp_variants = fpv;
1541 } else {
1542 /* insert into list after the first one */
1543 fpv->next = stfp->fp_variants->next;
1544 stfp->fp_variants->next = fpv;
1545 }
1546 } else {
1547 /* insert into list */
1548 fpv->next = stfp->fp_variants;
1549 stfp->fp_variants = fpv;
1550 }
1551 }
1552 }
1553
1554 return fpv;
1555 }
1556
1557 /**
1558 * Translate a program. This is common code for geometry and tessellation
1559 * shaders.
1560 */
1561 bool
1562 st_translate_common_program(struct st_context *st,
1563 struct st_program *stp)
1564 {
1565 struct gl_program *prog = &stp->Base;
1566 enum pipe_shader_type stage =
1567 pipe_shader_type_from_mesa(stp->Base.info.stage);
1568 struct ureg_program *ureg = ureg_create_with_screen(stage, st->pipe->screen);
1569
1570 if (ureg == NULL)
1571 return false;
1572
1573 switch (stage) {
1574 case PIPE_SHADER_TESS_CTRL:
1575 ureg_property(ureg, TGSI_PROPERTY_TCS_VERTICES_OUT,
1576 stp->Base.info.tess.tcs_vertices_out);
1577 break;
1578
1579 case PIPE_SHADER_TESS_EVAL:
1580 if (stp->Base.info.tess.primitive_mode == GL_ISOLINES)
1581 ureg_property(ureg, TGSI_PROPERTY_TES_PRIM_MODE, GL_LINES);
1582 else
1583 ureg_property(ureg, TGSI_PROPERTY_TES_PRIM_MODE,
1584 stp->Base.info.tess.primitive_mode);
1585
1586 STATIC_ASSERT((TESS_SPACING_EQUAL + 1) % 3 == PIPE_TESS_SPACING_EQUAL);
1587 STATIC_ASSERT((TESS_SPACING_FRACTIONAL_ODD + 1) % 3 ==
1588 PIPE_TESS_SPACING_FRACTIONAL_ODD);
1589 STATIC_ASSERT((TESS_SPACING_FRACTIONAL_EVEN + 1) % 3 ==
1590 PIPE_TESS_SPACING_FRACTIONAL_EVEN);
1591
1592 ureg_property(ureg, TGSI_PROPERTY_TES_SPACING,
1593 (stp->Base.info.tess.spacing + 1) % 3);
1594
1595 ureg_property(ureg, TGSI_PROPERTY_TES_VERTEX_ORDER_CW,
1596 !stp->Base.info.tess.ccw);
1597 ureg_property(ureg, TGSI_PROPERTY_TES_POINT_MODE,
1598 stp->Base.info.tess.point_mode);
1599 break;
1600
1601 case PIPE_SHADER_GEOMETRY:
1602 ureg_property(ureg, TGSI_PROPERTY_GS_INPUT_PRIM,
1603 stp->Base.info.gs.input_primitive);
1604 ureg_property(ureg, TGSI_PROPERTY_GS_OUTPUT_PRIM,
1605 stp->Base.info.gs.output_primitive);
1606 ureg_property(ureg, TGSI_PROPERTY_GS_MAX_OUTPUT_VERTICES,
1607 stp->Base.info.gs.vertices_out);
1608 ureg_property(ureg, TGSI_PROPERTY_GS_INVOCATIONS,
1609 stp->Base.info.gs.invocations);
1610 break;
1611
1612 default:
1613 break;
1614 }
1615
1616 ubyte inputSlotToAttr[VARYING_SLOT_TESS_MAX];
1617 ubyte inputMapping[VARYING_SLOT_TESS_MAX];
1618 ubyte outputMapping[VARYING_SLOT_TESS_MAX];
1619 GLuint attr;
1620
1621 ubyte input_semantic_name[PIPE_MAX_SHADER_INPUTS];
1622 ubyte input_semantic_index[PIPE_MAX_SHADER_INPUTS];
1623 uint num_inputs = 0;
1624
1625 ubyte output_semantic_name[PIPE_MAX_SHADER_OUTPUTS];
1626 ubyte output_semantic_index[PIPE_MAX_SHADER_OUTPUTS];
1627 uint num_outputs = 0;
1628
1629 GLint i;
1630
1631 memset(inputSlotToAttr, 0, sizeof(inputSlotToAttr));
1632 memset(inputMapping, 0, sizeof(inputMapping));
1633 memset(outputMapping, 0, sizeof(outputMapping));
1634 memset(&stp->state, 0, sizeof(stp->state));
1635
1636 if (prog->info.clip_distance_array_size)
1637 ureg_property(ureg, TGSI_PROPERTY_NUM_CLIPDIST_ENABLED,
1638 prog->info.clip_distance_array_size);
1639 if (prog->info.cull_distance_array_size)
1640 ureg_property(ureg, TGSI_PROPERTY_NUM_CULLDIST_ENABLED,
1641 prog->info.cull_distance_array_size);
1642
1643 /*
1644 * Convert Mesa program inputs to TGSI input register semantics.
1645 */
1646 for (attr = 0; attr < VARYING_SLOT_MAX; attr++) {
1647 if ((prog->info.inputs_read & BITFIELD64_BIT(attr)) == 0)
1648 continue;
1649
1650 unsigned slot = num_inputs++;
1651
1652 inputMapping[attr] = slot;
1653 inputSlotToAttr[slot] = attr;
1654
1655 unsigned semantic_name, semantic_index;
1656 tgsi_get_gl_varying_semantic(attr, st->needs_texcoord_semantic,
1657 &semantic_name, &semantic_index);
1658 input_semantic_name[slot] = semantic_name;
1659 input_semantic_index[slot] = semantic_index;
1660 }
1661
1662 /* Also add patch inputs. */
1663 for (attr = 0; attr < 32; attr++) {
1664 if (prog->info.patch_inputs_read & (1u << attr)) {
1665 GLuint slot = num_inputs++;
1666 GLuint patch_attr = VARYING_SLOT_PATCH0 + attr;
1667
1668 inputMapping[patch_attr] = slot;
1669 inputSlotToAttr[slot] = patch_attr;
1670 input_semantic_name[slot] = TGSI_SEMANTIC_PATCH;
1671 input_semantic_index[slot] = attr;
1672 }
1673 }
1674
1675 /* initialize output semantics to defaults */
1676 for (i = 0; i < PIPE_MAX_SHADER_OUTPUTS; i++) {
1677 output_semantic_name[i] = TGSI_SEMANTIC_GENERIC;
1678 output_semantic_index[i] = 0;
1679 }
1680
1681 /*
1682 * Determine number of outputs, the (default) output register
1683 * mapping and the semantic information for each output.
1684 */
1685 for (attr = 0; attr < VARYING_SLOT_MAX; attr++) {
1686 if (prog->info.outputs_written & BITFIELD64_BIT(attr)) {
1687 GLuint slot = num_outputs++;
1688
1689 outputMapping[attr] = slot;
1690
1691 unsigned semantic_name, semantic_index;
1692 tgsi_get_gl_varying_semantic(attr, st->needs_texcoord_semantic,
1693 &semantic_name, &semantic_index);
1694 output_semantic_name[slot] = semantic_name;
1695 output_semantic_index[slot] = semantic_index;
1696 }
1697 }
1698
1699 /* Also add patch outputs. */
1700 for (attr = 0; attr < 32; attr++) {
1701 if (prog->info.patch_outputs_written & (1u << attr)) {
1702 GLuint slot = num_outputs++;
1703 GLuint patch_attr = VARYING_SLOT_PATCH0 + attr;
1704
1705 outputMapping[patch_attr] = slot;
1706 output_semantic_name[slot] = TGSI_SEMANTIC_PATCH;
1707 output_semantic_index[slot] = attr;
1708 }
1709 }
1710
1711 st_translate_program(st->ctx,
1712 stage,
1713 ureg,
1714 stp->glsl_to_tgsi,
1715 prog,
1716 /* inputs */
1717 num_inputs,
1718 inputMapping,
1719 inputSlotToAttr,
1720 input_semantic_name,
1721 input_semantic_index,
1722 NULL,
1723 /* outputs */
1724 num_outputs,
1725 outputMapping,
1726 output_semantic_name,
1727 output_semantic_index);
1728
1729 stp->state.tokens = ureg_get_tokens(ureg, NULL);
1730
1731 ureg_destroy(ureg);
1732
1733 st_translate_stream_output_info(prog);
1734
1735 st_store_ir_in_disk_cache(st, prog, false);
1736
1737 if (ST_DEBUG & DEBUG_PRINT_IR && ST_DEBUG & DEBUG_MESA)
1738 _mesa_print_program(prog);
1739
1740 free_glsl_to_tgsi_visitor(stp->glsl_to_tgsi);
1741 stp->glsl_to_tgsi = NULL;
1742 return true;
1743 }
1744
1745
1746 /**
1747 * Get/create a basic program variant.
1748 */
1749 struct st_common_variant *
1750 st_get_common_variant(struct st_context *st,
1751 struct st_program *prog,
1752 const struct st_common_variant_key *key)
1753 {
1754 struct pipe_context *pipe = st->pipe;
1755 struct st_common_variant *v;
1756 struct pipe_shader_state state = {0};
1757
1758 /* Search for existing variant */
1759 for (v = prog->variants; v; v = v->next) {
1760 if (memcmp(&v->key, key, sizeof(*key)) == 0) {
1761 break;
1762 }
1763 }
1764
1765 if (!v) {
1766 /* create new */
1767 v = CALLOC_STRUCT(st_common_variant);
1768 if (v) {
1769 if (prog->state.type == PIPE_SHADER_IR_NIR) {
1770 bool finalize = false;
1771
1772 state.type = PIPE_SHADER_IR_NIR;
1773 state.ir.nir = nir_shader_clone(NULL, prog->state.ir.nir);
1774
1775 if (key->clamp_color) {
1776 NIR_PASS_V(state.ir.nir, nir_lower_clamp_color_outputs);
1777 finalize = true;
1778 }
1779
1780 state.stream_output = prog->state.stream_output;
1781
1782 if (finalize || !st->allow_st_finalize_nir_twice) {
1783 st_finalize_nir(st, &prog->Base, prog->shader_program,
1784 state.ir.nir, true);
1785 }
1786
1787 if (ST_DEBUG & DEBUG_PRINT_IR)
1788 nir_print_shader(state.ir.nir, stderr);
1789 } else {
1790 if (key->lower_depth_clamp) {
1791 struct gl_program_parameter_list *params = prog->Base.Parameters;
1792
1793 unsigned depth_range_const =
1794 _mesa_add_state_reference(params, depth_range_state);
1795
1796 const struct tgsi_token *tokens;
1797 tokens =
1798 st_tgsi_lower_depth_clamp(prog->state.tokens,
1799 depth_range_const,
1800 key->clip_negative_one_to_one);
1801
1802 if (tokens != prog->state.tokens)
1803 tgsi_free_tokens(prog->state.tokens);
1804
1805 prog->state.tokens = tokens;
1806 }
1807 state = prog->state;
1808
1809 if (ST_DEBUG & DEBUG_PRINT_IR)
1810 tgsi_dump(state.tokens, 0);
1811 }
1812 /* fill in new variant */
1813 switch (prog->Base.info.stage) {
1814 case MESA_SHADER_TESS_CTRL:
1815 v->driver_shader = pipe->create_tcs_state(pipe, &state);
1816 break;
1817 case MESA_SHADER_TESS_EVAL:
1818 v->driver_shader = pipe->create_tes_state(pipe, &state);
1819 break;
1820 case MESA_SHADER_GEOMETRY:
1821 v->driver_shader = pipe->create_gs_state(pipe, &state);
1822 break;
1823 case MESA_SHADER_COMPUTE: {
1824 struct pipe_compute_state cs = {0};
1825 cs.ir_type = state.type;
1826 cs.req_local_mem = prog->Base.info.cs.shared_size;
1827
1828 if (state.type == PIPE_SHADER_IR_NIR)
1829 cs.prog = state.ir.nir;
1830 else
1831 cs.prog = state.tokens;
1832
1833 v->driver_shader = pipe->create_compute_state(pipe, &cs);
1834 break;
1835 }
1836 default:
1837 assert(!"unhandled shader type");
1838 free(v);
1839 return NULL;
1840 }
1841
1842 v->key = *key;
1843
1844 /* insert into list */
1845 v->next = prog->variants;
1846 prog->variants = v;
1847 }
1848 }
1849
1850 return v;
1851 }
1852
1853
1854 /**
1855 * Vert/Geom/Frag programs have per-context variants. Free all the
1856 * variants attached to the given program which match the given context.
1857 */
1858 static void
1859 destroy_program_variants(struct st_context *st, struct gl_program *target)
1860 {
1861 if (!target || target == &_mesa_DummyProgram)
1862 return;
1863
1864 switch (target->Target) {
1865 case GL_VERTEX_PROGRAM_ARB:
1866 {
1867 struct st_program *stvp = (struct st_program *) target;
1868 struct st_vp_variant *vpv, **prevPtr = &stvp->vp_variants;
1869
1870 for (vpv = stvp->vp_variants; vpv; ) {
1871 struct st_vp_variant *next = vpv->next;
1872 if (vpv->key.st == st) {
1873 /* unlink from list */
1874 *prevPtr = next;
1875 /* destroy this variant */
1876 delete_vp_variant(st, vpv);
1877 }
1878 else {
1879 prevPtr = &vpv->next;
1880 }
1881 vpv = next;
1882 }
1883 }
1884 break;
1885 case GL_FRAGMENT_PROGRAM_ARB:
1886 {
1887 struct st_program *stfp =
1888 (struct st_program *) target;
1889 struct st_fp_variant *fpv, **prevPtr = &stfp->fp_variants;
1890
1891 for (fpv = stfp->fp_variants; fpv; ) {
1892 struct st_fp_variant *next = fpv->next;
1893 if (fpv->key.st == st) {
1894 /* unlink from list */
1895 *prevPtr = next;
1896 /* destroy this variant */
1897 delete_fp_variant(st, fpv);
1898 }
1899 else {
1900 prevPtr = &fpv->next;
1901 }
1902 fpv = next;
1903 }
1904 }
1905 break;
1906 case GL_GEOMETRY_PROGRAM_NV:
1907 case GL_TESS_CONTROL_PROGRAM_NV:
1908 case GL_TESS_EVALUATION_PROGRAM_NV:
1909 case GL_COMPUTE_PROGRAM_NV:
1910 {
1911 struct st_program *p = st_program(target);
1912 struct st_common_variant *v, **prevPtr = &p->variants;
1913
1914 for (v = p->variants; v; ) {
1915 struct st_common_variant *next = v->next;
1916 if (v->key.st == st) {
1917 /* unlink from list */
1918 *prevPtr = next;
1919 /* destroy this variant */
1920 delete_common_variant(st, v, target->Target);
1921 }
1922 else {
1923 prevPtr = &v->next;
1924 }
1925 v = next;
1926 }
1927 }
1928 break;
1929 default:
1930 _mesa_problem(NULL, "Unexpected program target 0x%x in "
1931 "destroy_program_variants_cb()", target->Target);
1932 }
1933 }
1934
1935
1936 /**
1937 * Callback for _mesa_HashWalk. Free all the shader's program variants
1938 * which match the given context.
1939 */
1940 static void
1941 destroy_shader_program_variants_cb(GLuint key, void *data, void *userData)
1942 {
1943 struct st_context *st = (struct st_context *) userData;
1944 struct gl_shader *shader = (struct gl_shader *) data;
1945
1946 switch (shader->Type) {
1947 case GL_SHADER_PROGRAM_MESA:
1948 {
1949 struct gl_shader_program *shProg = (struct gl_shader_program *) data;
1950 GLuint i;
1951
1952 for (i = 0; i < ARRAY_SIZE(shProg->_LinkedShaders); i++) {
1953 if (shProg->_LinkedShaders[i])
1954 destroy_program_variants(st, shProg->_LinkedShaders[i]->Program);
1955 }
1956 }
1957 break;
1958 case GL_VERTEX_SHADER:
1959 case GL_FRAGMENT_SHADER:
1960 case GL_GEOMETRY_SHADER:
1961 case GL_TESS_CONTROL_SHADER:
1962 case GL_TESS_EVALUATION_SHADER:
1963 case GL_COMPUTE_SHADER:
1964 break;
1965 default:
1966 assert(0);
1967 }
1968 }
1969
1970
1971 /**
1972 * Callback for _mesa_HashWalk. Free all the program variants which match
1973 * the given context.
1974 */
1975 static void
1976 destroy_program_variants_cb(GLuint key, void *data, void *userData)
1977 {
1978 struct st_context *st = (struct st_context *) userData;
1979 struct gl_program *program = (struct gl_program *) data;
1980 destroy_program_variants(st, program);
1981 }
1982
1983
1984 /**
1985 * Walk over all shaders and programs to delete any variants which
1986 * belong to the given context.
1987 * This is called during context tear-down.
1988 */
1989 void
1990 st_destroy_program_variants(struct st_context *st)
1991 {
1992 /* If shaders can be shared with other contexts, the last context will
1993 * call DeleteProgram on all shaders, releasing everything.
1994 */
1995 if (st->has_shareable_shaders)
1996 return;
1997
1998 /* ARB vert/frag program */
1999 _mesa_HashWalk(st->ctx->Shared->Programs,
2000 destroy_program_variants_cb, st);
2001
2002 /* GLSL vert/frag/geom shaders */
2003 _mesa_HashWalk(st->ctx->Shared->ShaderObjects,
2004 destroy_shader_program_variants_cb, st);
2005 }
2006
2007
2008 /**
2009 * Compile one shader variant.
2010 */
2011 static void
2012 st_precompile_shader_variant(struct st_context *st,
2013 struct gl_program *prog)
2014 {
2015 switch (prog->Target) {
2016 case GL_VERTEX_PROGRAM_ARB: {
2017 struct st_program *p = (struct st_program *)prog;
2018 struct st_common_variant_key key;
2019
2020 memset(&key, 0, sizeof(key));
2021
2022 key.st = st->has_shareable_shaders ? NULL : st;
2023 st_get_vp_variant(st, p, &key);
2024 break;
2025 }
2026
2027 case GL_FRAGMENT_PROGRAM_ARB: {
2028 struct st_program *p = (struct st_program *)prog;
2029 struct st_fp_variant_key key;
2030
2031 memset(&key, 0, sizeof(key));
2032
2033 key.st = st->has_shareable_shaders ? NULL : st;
2034 st_get_fp_variant(st, p, &key);
2035 break;
2036 }
2037
2038 case GL_TESS_CONTROL_PROGRAM_NV:
2039 case GL_TESS_EVALUATION_PROGRAM_NV:
2040 case GL_GEOMETRY_PROGRAM_NV:
2041 case GL_COMPUTE_PROGRAM_NV: {
2042 struct st_program *p = st_program(prog);
2043 struct st_common_variant_key key;
2044
2045 memset(&key, 0, sizeof(key));
2046
2047 key.st = st->has_shareable_shaders ? NULL : st;
2048 st_get_common_variant(st, p, &key);
2049 break;
2050 }
2051
2052 default:
2053 assert(0);
2054 }
2055 }
2056
2057 void
2058 st_finalize_program(struct st_context *st, struct gl_program *prog)
2059 {
2060 if (st->current_program[prog->info.stage] == prog) {
2061 if (prog->info.stage == MESA_SHADER_VERTEX)
2062 st->dirty |= ST_NEW_VERTEX_PROGRAM(st, (struct st_program *)prog);
2063 else
2064 st->dirty |= ((struct st_program *)prog)->affected_states;
2065 }
2066
2067 if (prog->nir)
2068 nir_sweep(prog->nir);
2069
2070 /* Create Gallium shaders now instead of on demand. */
2071 if (ST_DEBUG & DEBUG_PRECOMPILE ||
2072 st->shader_has_one_variant[prog->info.stage])
2073 st_precompile_shader_variant(st, prog);
2074 }