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