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