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