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