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