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