st/mesa: print TCS/TES/GS/CS TGSI in the right place & keep disk cache enabled
[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_PRINT_IR)
795 tgsi_dump(state.tokens, 0);
796
797 vpv->driver_shader = pipe->create_vs_state(pipe, &state);
798 /* Save this for selection/feedback/rasterpos. */
799 vpv->tokens = state.tokens;
800 return vpv;
801 }
802
803
804 /**
805 * Find/create a vertex program variant.
806 */
807 struct st_vp_variant *
808 st_get_vp_variant(struct st_context *st,
809 struct st_vertex_program *stvp,
810 const struct st_common_variant_key *key)
811 {
812 struct st_vp_variant *vpv;
813
814 /* Search for existing variant */
815 for (vpv = stvp->variants; vpv; vpv = vpv->next) {
816 if (memcmp(&vpv->key, key, sizeof(*key)) == 0) {
817 break;
818 }
819 }
820
821 if (!vpv) {
822 /* create now */
823 vpv = st_create_vp_variant(st, stvp, key);
824 if (vpv) {
825 for (unsigned index = 0; index < vpv->num_inputs; ++index) {
826 unsigned attr = stvp->index_to_input[index];
827 if (attr == ST_DOUBLE_ATTRIB_PLACEHOLDER)
828 continue;
829 vpv->vert_attrib_mask |= 1u << attr;
830 }
831
832 /* insert into list */
833 vpv->next = stvp->variants;
834 stvp->variants = vpv;
835 }
836 }
837
838 return vpv;
839 }
840
841
842 /**
843 * Translate a Mesa fragment shader into a TGSI shader.
844 */
845 bool
846 st_translate_fragment_program(struct st_context *st,
847 struct st_common_program *stfp)
848 {
849 /* Non-GLSL programs: */
850 if (!stfp->glsl_to_tgsi) {
851 _mesa_remove_output_reads(&stfp->Base, PROGRAM_OUTPUT);
852 if (st->ctx->Const.GLSLFragCoordIsSysVal)
853 _mesa_program_fragment_position_to_sysval(&stfp->Base);
854
855 /* This determines which states will be updated when the assembly
856 * shader is bound.
857 *
858 * fragment.position and glDrawPixels always use constants.
859 */
860 stfp->affected_states = ST_NEW_FS_STATE |
861 ST_NEW_SAMPLE_SHADING |
862 ST_NEW_FS_CONSTANTS;
863
864 if (stfp->ati_fs) {
865 /* Just set them for ATI_fs unconditionally. */
866 stfp->affected_states |= ST_NEW_FS_SAMPLER_VIEWS |
867 ST_NEW_FS_SAMPLERS;
868 } else {
869 /* ARB_fp */
870 if (stfp->Base.SamplersUsed)
871 stfp->affected_states |= ST_NEW_FS_SAMPLER_VIEWS |
872 ST_NEW_FS_SAMPLERS;
873 }
874
875 /* Translate to NIR. */
876 if (!stfp->ati_fs &&
877 st->pipe->screen->get_shader_param(st->pipe->screen,
878 PIPE_SHADER_FRAGMENT,
879 PIPE_SHADER_CAP_PREFERRED_IR)) {
880 nir_shader *nir =
881 st_translate_prog_to_nir(st, &stfp->Base, MESA_SHADER_FRAGMENT);
882
883 if (stfp->state.ir.nir)
884 ralloc_free(stfp->state.ir.nir);
885 stfp->state.type = PIPE_SHADER_IR_NIR;
886 stfp->state.ir.nir = nir;
887 stfp->Base.nir = nir;
888 return true;
889 }
890 }
891
892 ubyte outputMapping[2 * FRAG_RESULT_MAX];
893 ubyte inputMapping[VARYING_SLOT_MAX];
894 ubyte inputSlotToAttr[VARYING_SLOT_MAX];
895 ubyte interpMode[PIPE_MAX_SHADER_INPUTS]; /* XXX size? */
896 GLuint attr;
897 GLbitfield64 inputsRead;
898 struct ureg_program *ureg;
899
900 GLboolean write_all = GL_FALSE;
901
902 ubyte input_semantic_name[PIPE_MAX_SHADER_INPUTS];
903 ubyte input_semantic_index[PIPE_MAX_SHADER_INPUTS];
904 uint fs_num_inputs = 0;
905
906 ubyte fs_output_semantic_name[PIPE_MAX_SHADER_OUTPUTS];
907 ubyte fs_output_semantic_index[PIPE_MAX_SHADER_OUTPUTS];
908 uint fs_num_outputs = 0;
909
910 memset(inputSlotToAttr, ~0, sizeof(inputSlotToAttr));
911
912 /*
913 * Convert Mesa program inputs to TGSI input register semantics.
914 */
915 inputsRead = stfp->Base.info.inputs_read;
916 for (attr = 0; attr < VARYING_SLOT_MAX; attr++) {
917 if ((inputsRead & BITFIELD64_BIT(attr)) != 0) {
918 const GLuint slot = fs_num_inputs++;
919
920 inputMapping[attr] = slot;
921 inputSlotToAttr[slot] = attr;
922
923 switch (attr) {
924 case VARYING_SLOT_POS:
925 input_semantic_name[slot] = TGSI_SEMANTIC_POSITION;
926 input_semantic_index[slot] = 0;
927 interpMode[slot] = TGSI_INTERPOLATE_LINEAR;
928 break;
929 case VARYING_SLOT_COL0:
930 input_semantic_name[slot] = TGSI_SEMANTIC_COLOR;
931 input_semantic_index[slot] = 0;
932 interpMode[slot] = stfp->glsl_to_tgsi ?
933 TGSI_INTERPOLATE_COUNT : TGSI_INTERPOLATE_COLOR;
934 break;
935 case VARYING_SLOT_COL1:
936 input_semantic_name[slot] = TGSI_SEMANTIC_COLOR;
937 input_semantic_index[slot] = 1;
938 interpMode[slot] = stfp->glsl_to_tgsi ?
939 TGSI_INTERPOLATE_COUNT : TGSI_INTERPOLATE_COLOR;
940 break;
941 case VARYING_SLOT_FOGC:
942 input_semantic_name[slot] = TGSI_SEMANTIC_FOG;
943 input_semantic_index[slot] = 0;
944 interpMode[slot] = TGSI_INTERPOLATE_PERSPECTIVE;
945 break;
946 case VARYING_SLOT_FACE:
947 input_semantic_name[slot] = TGSI_SEMANTIC_FACE;
948 input_semantic_index[slot] = 0;
949 interpMode[slot] = TGSI_INTERPOLATE_CONSTANT;
950 break;
951 case VARYING_SLOT_PRIMITIVE_ID:
952 input_semantic_name[slot] = TGSI_SEMANTIC_PRIMID;
953 input_semantic_index[slot] = 0;
954 interpMode[slot] = TGSI_INTERPOLATE_CONSTANT;
955 break;
956 case VARYING_SLOT_LAYER:
957 input_semantic_name[slot] = TGSI_SEMANTIC_LAYER;
958 input_semantic_index[slot] = 0;
959 interpMode[slot] = TGSI_INTERPOLATE_CONSTANT;
960 break;
961 case VARYING_SLOT_VIEWPORT:
962 input_semantic_name[slot] = TGSI_SEMANTIC_VIEWPORT_INDEX;
963 input_semantic_index[slot] = 0;
964 interpMode[slot] = TGSI_INTERPOLATE_CONSTANT;
965 break;
966 case VARYING_SLOT_CLIP_DIST0:
967 input_semantic_name[slot] = TGSI_SEMANTIC_CLIPDIST;
968 input_semantic_index[slot] = 0;
969 interpMode[slot] = TGSI_INTERPOLATE_PERSPECTIVE;
970 break;
971 case VARYING_SLOT_CLIP_DIST1:
972 input_semantic_name[slot] = TGSI_SEMANTIC_CLIPDIST;
973 input_semantic_index[slot] = 1;
974 interpMode[slot] = TGSI_INTERPOLATE_PERSPECTIVE;
975 break;
976 case VARYING_SLOT_CULL_DIST0:
977 case VARYING_SLOT_CULL_DIST1:
978 /* these should have been lowered by GLSL */
979 assert(0);
980 break;
981 /* In most cases, there is nothing special about these
982 * inputs, so adopt a convention to use the generic
983 * semantic name and the mesa VARYING_SLOT_ number as the
984 * index.
985 *
986 * All that is required is that the vertex shader labels
987 * its own outputs similarly, and that the vertex shader
988 * generates at least every output required by the
989 * fragment shader plus fixed-function hardware (such as
990 * BFC).
991 *
992 * However, some drivers may need us to identify the PNTC and TEXi
993 * varyings if, for example, their capability to replace them with
994 * sprite coordinates is limited.
995 */
996 case VARYING_SLOT_PNTC:
997 if (st->needs_texcoord_semantic) {
998 input_semantic_name[slot] = TGSI_SEMANTIC_PCOORD;
999 input_semantic_index[slot] = 0;
1000 interpMode[slot] = TGSI_INTERPOLATE_LINEAR;
1001 break;
1002 }
1003 /* fall through */
1004 case VARYING_SLOT_TEX0:
1005 case VARYING_SLOT_TEX1:
1006 case VARYING_SLOT_TEX2:
1007 case VARYING_SLOT_TEX3:
1008 case VARYING_SLOT_TEX4:
1009 case VARYING_SLOT_TEX5:
1010 case VARYING_SLOT_TEX6:
1011 case VARYING_SLOT_TEX7:
1012 if (st->needs_texcoord_semantic) {
1013 input_semantic_name[slot] = TGSI_SEMANTIC_TEXCOORD;
1014 input_semantic_index[slot] = attr - VARYING_SLOT_TEX0;
1015 interpMode[slot] = stfp->glsl_to_tgsi ?
1016 TGSI_INTERPOLATE_COUNT : TGSI_INTERPOLATE_PERSPECTIVE;
1017 break;
1018 }
1019 /* fall through */
1020 case VARYING_SLOT_VAR0:
1021 default:
1022 /* Semantic indices should be zero-based because drivers may choose
1023 * to assign a fixed slot determined by that index.
1024 * This is useful because ARB_separate_shader_objects uses location
1025 * qualifiers for linkage, and if the semantic index corresponds to
1026 * these locations, linkage passes in the driver become unecessary.
1027 *
1028 * If needs_texcoord_semantic is true, no semantic indices will be
1029 * consumed for the TEXi varyings, and we can base the locations of
1030 * the user varyings on VAR0. Otherwise, we use TEX0 as base index.
1031 */
1032 assert(attr >= VARYING_SLOT_VAR0 || attr == VARYING_SLOT_PNTC ||
1033 (attr >= VARYING_SLOT_TEX0 && attr <= VARYING_SLOT_TEX7));
1034 input_semantic_name[slot] = TGSI_SEMANTIC_GENERIC;
1035 input_semantic_index[slot] = st_get_generic_varying_index(st, attr);
1036 if (attr == VARYING_SLOT_PNTC)
1037 interpMode[slot] = TGSI_INTERPOLATE_LINEAR;
1038 else {
1039 interpMode[slot] = stfp->glsl_to_tgsi ?
1040 TGSI_INTERPOLATE_COUNT : TGSI_INTERPOLATE_PERSPECTIVE;
1041 }
1042 break;
1043 }
1044 }
1045 else {
1046 inputMapping[attr] = -1;
1047 }
1048 }
1049
1050 /*
1051 * Semantics and mapping for outputs
1052 */
1053 GLbitfield64 outputsWritten = stfp->Base.info.outputs_written;
1054
1055 /* if z is written, emit that first */
1056 if (outputsWritten & BITFIELD64_BIT(FRAG_RESULT_DEPTH)) {
1057 fs_output_semantic_name[fs_num_outputs] = TGSI_SEMANTIC_POSITION;
1058 fs_output_semantic_index[fs_num_outputs] = 0;
1059 outputMapping[FRAG_RESULT_DEPTH] = fs_num_outputs;
1060 fs_num_outputs++;
1061 outputsWritten &= ~(1 << FRAG_RESULT_DEPTH);
1062 }
1063
1064 if (outputsWritten & BITFIELD64_BIT(FRAG_RESULT_STENCIL)) {
1065 fs_output_semantic_name[fs_num_outputs] = TGSI_SEMANTIC_STENCIL;
1066 fs_output_semantic_index[fs_num_outputs] = 0;
1067 outputMapping[FRAG_RESULT_STENCIL] = fs_num_outputs;
1068 fs_num_outputs++;
1069 outputsWritten &= ~(1 << FRAG_RESULT_STENCIL);
1070 }
1071
1072 if (outputsWritten & BITFIELD64_BIT(FRAG_RESULT_SAMPLE_MASK)) {
1073 fs_output_semantic_name[fs_num_outputs] = TGSI_SEMANTIC_SAMPLEMASK;
1074 fs_output_semantic_index[fs_num_outputs] = 0;
1075 outputMapping[FRAG_RESULT_SAMPLE_MASK] = fs_num_outputs;
1076 fs_num_outputs++;
1077 outputsWritten &= ~(1 << FRAG_RESULT_SAMPLE_MASK);
1078 }
1079
1080 /* handle remaining outputs (color) */
1081 for (attr = 0; attr < ARRAY_SIZE(outputMapping); attr++) {
1082 const GLbitfield64 written = attr < FRAG_RESULT_MAX ? outputsWritten :
1083 stfp->Base.SecondaryOutputsWritten;
1084 const unsigned loc = attr % FRAG_RESULT_MAX;
1085
1086 if (written & BITFIELD64_BIT(loc)) {
1087 switch (loc) {
1088 case FRAG_RESULT_DEPTH:
1089 case FRAG_RESULT_STENCIL:
1090 case FRAG_RESULT_SAMPLE_MASK:
1091 /* handled above */
1092 assert(0);
1093 break;
1094 case FRAG_RESULT_COLOR:
1095 write_all = GL_TRUE; /* fallthrough */
1096 default: {
1097 int index;
1098 assert(loc == FRAG_RESULT_COLOR ||
1099 (FRAG_RESULT_DATA0 <= loc && loc < FRAG_RESULT_MAX));
1100
1101 index = (loc == FRAG_RESULT_COLOR) ? 0 : (loc - FRAG_RESULT_DATA0);
1102
1103 if (attr >= FRAG_RESULT_MAX) {
1104 /* Secondary color for dual source blending. */
1105 assert(index == 0);
1106 index++;
1107 }
1108
1109 fs_output_semantic_name[fs_num_outputs] = TGSI_SEMANTIC_COLOR;
1110 fs_output_semantic_index[fs_num_outputs] = index;
1111 outputMapping[attr] = fs_num_outputs;
1112 break;
1113 }
1114 }
1115
1116 fs_num_outputs++;
1117 }
1118 }
1119
1120 ureg = ureg_create_with_screen(PIPE_SHADER_FRAGMENT, st->pipe->screen);
1121 if (ureg == NULL)
1122 return false;
1123
1124 if (ST_DEBUG & DEBUG_MESA) {
1125 _mesa_print_program(&stfp->Base);
1126 _mesa_print_program_parameters(st->ctx, &stfp->Base);
1127 debug_printf("\n");
1128 }
1129 if (write_all == GL_TRUE)
1130 ureg_property(ureg, TGSI_PROPERTY_FS_COLOR0_WRITES_ALL_CBUFS, 1);
1131
1132 if (stfp->Base.info.fs.depth_layout != FRAG_DEPTH_LAYOUT_NONE) {
1133 switch (stfp->Base.info.fs.depth_layout) {
1134 case FRAG_DEPTH_LAYOUT_ANY:
1135 ureg_property(ureg, TGSI_PROPERTY_FS_DEPTH_LAYOUT,
1136 TGSI_FS_DEPTH_LAYOUT_ANY);
1137 break;
1138 case FRAG_DEPTH_LAYOUT_GREATER:
1139 ureg_property(ureg, TGSI_PROPERTY_FS_DEPTH_LAYOUT,
1140 TGSI_FS_DEPTH_LAYOUT_GREATER);
1141 break;
1142 case FRAG_DEPTH_LAYOUT_LESS:
1143 ureg_property(ureg, TGSI_PROPERTY_FS_DEPTH_LAYOUT,
1144 TGSI_FS_DEPTH_LAYOUT_LESS);
1145 break;
1146 case FRAG_DEPTH_LAYOUT_UNCHANGED:
1147 ureg_property(ureg, TGSI_PROPERTY_FS_DEPTH_LAYOUT,
1148 TGSI_FS_DEPTH_LAYOUT_UNCHANGED);
1149 break;
1150 default:
1151 assert(0);
1152 }
1153 }
1154
1155 if (stfp->glsl_to_tgsi) {
1156 st_translate_program(st->ctx,
1157 PIPE_SHADER_FRAGMENT,
1158 ureg,
1159 stfp->glsl_to_tgsi,
1160 &stfp->Base,
1161 /* inputs */
1162 fs_num_inputs,
1163 inputMapping,
1164 inputSlotToAttr,
1165 input_semantic_name,
1166 input_semantic_index,
1167 interpMode,
1168 /* outputs */
1169 fs_num_outputs,
1170 outputMapping,
1171 fs_output_semantic_name,
1172 fs_output_semantic_index);
1173
1174 free_glsl_to_tgsi_visitor(stfp->glsl_to_tgsi);
1175 } else if (stfp->ati_fs)
1176 st_translate_atifs_program(ureg,
1177 stfp->ati_fs,
1178 &stfp->Base,
1179 /* inputs */
1180 fs_num_inputs,
1181 inputMapping,
1182 input_semantic_name,
1183 input_semantic_index,
1184 interpMode,
1185 /* outputs */
1186 fs_num_outputs,
1187 outputMapping,
1188 fs_output_semantic_name,
1189 fs_output_semantic_index);
1190 else
1191 st_translate_mesa_program(st->ctx,
1192 PIPE_SHADER_FRAGMENT,
1193 ureg,
1194 &stfp->Base,
1195 /* inputs */
1196 fs_num_inputs,
1197 inputMapping,
1198 input_semantic_name,
1199 input_semantic_index,
1200 interpMode,
1201 /* outputs */
1202 fs_num_outputs,
1203 outputMapping,
1204 fs_output_semantic_name,
1205 fs_output_semantic_index);
1206
1207 stfp->state.tokens = ureg_get_tokens(ureg, NULL);
1208 ureg_destroy(ureg);
1209
1210 if (stfp->glsl_to_tgsi) {
1211 stfp->glsl_to_tgsi = NULL;
1212 st_store_ir_in_disk_cache(st, &stfp->Base, false);
1213 }
1214
1215 return stfp->state.tokens != NULL;
1216 }
1217
1218 static struct st_fp_variant *
1219 st_create_fp_variant(struct st_context *st,
1220 struct st_common_program *stfp,
1221 const struct st_fp_variant_key *key)
1222 {
1223 struct pipe_context *pipe = st->pipe;
1224 struct st_fp_variant *variant = CALLOC_STRUCT(st_fp_variant);
1225 struct pipe_shader_state state = {0};
1226 struct gl_program_parameter_list *params = stfp->Base.Parameters;
1227 static const gl_state_index16 texcoord_state[STATE_LENGTH] =
1228 { STATE_INTERNAL, STATE_CURRENT_ATTRIB, VERT_ATTRIB_TEX0 };
1229 static const gl_state_index16 scale_state[STATE_LENGTH] =
1230 { STATE_INTERNAL, STATE_PT_SCALE };
1231 static const gl_state_index16 bias_state[STATE_LENGTH] =
1232 { STATE_INTERNAL, STATE_PT_BIAS };
1233 static const gl_state_index16 alpha_ref_state[STATE_LENGTH] =
1234 { STATE_INTERNAL, STATE_ALPHA_REF };
1235
1236 if (!variant)
1237 return NULL;
1238
1239 if (stfp->state.type == PIPE_SHADER_IR_NIR) {
1240 bool finalize = false;
1241
1242 state.type = PIPE_SHADER_IR_NIR;
1243 state.ir.nir = nir_shader_clone(NULL, stfp->state.ir.nir);
1244
1245 if (key->clamp_color) {
1246 NIR_PASS_V(state.ir.nir, nir_lower_clamp_color_outputs);
1247 finalize = true;
1248 }
1249
1250 if (key->lower_flatshade) {
1251 NIR_PASS_V(state.ir.nir, nir_lower_flatshade);
1252 finalize = true;
1253 }
1254
1255 if (key->lower_alpha_func != COMPARE_FUNC_NEVER) {
1256 _mesa_add_state_reference(params, alpha_ref_state);
1257 NIR_PASS_V(state.ir.nir, nir_lower_alpha_test, key->lower_alpha_func,
1258 false, alpha_ref_state);
1259 finalize = true;
1260 }
1261
1262 if (key->lower_two_sided_color) {
1263 NIR_PASS_V(state.ir.nir, nir_lower_two_sided_color);
1264 finalize = true;
1265 }
1266
1267 if (key->persample_shading) {
1268 nir_shader *shader = state.ir.nir;
1269 nir_foreach_variable(var, &shader->inputs)
1270 var->data.sample = true;
1271 finalize = true;
1272 }
1273
1274 assert(!(key->bitmap && key->drawpixels));
1275
1276 /* glBitmap */
1277 if (key->bitmap) {
1278 nir_lower_bitmap_options options = {0};
1279
1280 variant->bitmap_sampler = ffs(~stfp->Base.SamplersUsed) - 1;
1281 options.sampler = variant->bitmap_sampler;
1282 options.swizzle_xxxx = st->bitmap.tex_format == PIPE_FORMAT_R8_UNORM;
1283
1284 NIR_PASS_V(state.ir.nir, nir_lower_bitmap, &options);
1285 finalize = true;
1286 }
1287
1288 /* glDrawPixels (color only) */
1289 if (key->drawpixels) {
1290 nir_lower_drawpixels_options options = {{0}};
1291 unsigned samplers_used = stfp->Base.SamplersUsed;
1292
1293 /* Find the first unused slot. */
1294 variant->drawpix_sampler = ffs(~samplers_used) - 1;
1295 options.drawpix_sampler = variant->drawpix_sampler;
1296 samplers_used |= (1 << variant->drawpix_sampler);
1297
1298 options.pixel_maps = key->pixelMaps;
1299 if (key->pixelMaps) {
1300 variant->pixelmap_sampler = ffs(~samplers_used) - 1;
1301 options.pixelmap_sampler = variant->pixelmap_sampler;
1302 }
1303
1304 options.scale_and_bias = key->scaleAndBias;
1305 if (key->scaleAndBias) {
1306 _mesa_add_state_reference(params, scale_state);
1307 memcpy(options.scale_state_tokens, scale_state,
1308 sizeof(options.scale_state_tokens));
1309 _mesa_add_state_reference(params, bias_state);
1310 memcpy(options.bias_state_tokens, bias_state,
1311 sizeof(options.bias_state_tokens));
1312 }
1313
1314 _mesa_add_state_reference(params, texcoord_state);
1315 memcpy(options.texcoord_state_tokens, texcoord_state,
1316 sizeof(options.texcoord_state_tokens));
1317
1318 NIR_PASS_V(state.ir.nir, nir_lower_drawpixels, &options);
1319 finalize = true;
1320 }
1321
1322 if (unlikely(key->external.lower_nv12 || key->external.lower_iyuv ||
1323 key->external.lower_xy_uxvx || key->external.lower_yx_xuxv ||
1324 key->external.lower_ayuv || key->external.lower_xyuv)) {
1325 nir_lower_tex_options options = {0};
1326 options.lower_y_uv_external = key->external.lower_nv12;
1327 options.lower_y_u_v_external = key->external.lower_iyuv;
1328 options.lower_xy_uxvx_external = key->external.lower_xy_uxvx;
1329 options.lower_yx_xuxv_external = key->external.lower_yx_xuxv;
1330 options.lower_ayuv_external = key->external.lower_ayuv;
1331 options.lower_xyuv_external = key->external.lower_xyuv;
1332 NIR_PASS_V(state.ir.nir, nir_lower_tex, &options);
1333 finalize = true;
1334 }
1335
1336 if (finalize || !st->allow_st_finalize_nir_twice) {
1337 st_finalize_nir(st, &stfp->Base, stfp->shader_program, state.ir.nir,
1338 false);
1339 }
1340
1341 /* This pass needs to happen *after* nir_lower_sampler */
1342 if (unlikely(key->external.lower_nv12 || key->external.lower_iyuv ||
1343 key->external.lower_xy_uxvx || key->external.lower_yx_xuxv)) {
1344 NIR_PASS_V(state.ir.nir, st_nir_lower_tex_src_plane,
1345 ~stfp->Base.SamplersUsed,
1346 key->external.lower_nv12 || key->external.lower_xy_uxvx ||
1347 key->external.lower_yx_xuxv,
1348 key->external.lower_iyuv);
1349 finalize = true;
1350 }
1351
1352 if (finalize || !st->allow_st_finalize_nir_twice) {
1353 /* Some of the lowering above may have introduced new varyings */
1354 nir_shader_gather_info(state.ir.nir,
1355 nir_shader_get_entrypoint(state.ir.nir));
1356
1357 struct pipe_screen *screen = pipe->screen;
1358 if (screen->finalize_nir)
1359 screen->finalize_nir(screen, state.ir.nir, false);
1360 }
1361
1362 variant->driver_shader = pipe->create_fs_state(pipe, &state);
1363 variant->key = *key;
1364
1365 return variant;
1366 }
1367
1368 state.tokens = stfp->state.tokens;
1369
1370 assert(!(key->bitmap && key->drawpixels));
1371
1372 /* Fix texture targets and add fog for ATI_fs */
1373 if (stfp->ati_fs) {
1374 const struct tgsi_token *tokens = st_fixup_atifs(state.tokens, key);
1375
1376 if (tokens)
1377 state.tokens = tokens;
1378 else
1379 fprintf(stderr, "mesa: cannot post-process ATI_fs\n");
1380 }
1381
1382 /* Emulate features. */
1383 if (key->clamp_color || key->persample_shading) {
1384 const struct tgsi_token *tokens;
1385 unsigned flags =
1386 (key->clamp_color ? TGSI_EMU_CLAMP_COLOR_OUTPUTS : 0) |
1387 (key->persample_shading ? TGSI_EMU_FORCE_PERSAMPLE_INTERP : 0);
1388
1389 tokens = tgsi_emulate(state.tokens, flags);
1390
1391 if (tokens) {
1392 if (state.tokens != stfp->state.tokens)
1393 tgsi_free_tokens(state.tokens);
1394 state.tokens = tokens;
1395 } else
1396 fprintf(stderr, "mesa: cannot emulate deprecated features\n");
1397 }
1398
1399 /* glBitmap */
1400 if (key->bitmap) {
1401 const struct tgsi_token *tokens;
1402
1403 variant->bitmap_sampler = ffs(~stfp->Base.SamplersUsed) - 1;
1404
1405 tokens = st_get_bitmap_shader(state.tokens,
1406 st->internal_target,
1407 variant->bitmap_sampler,
1408 st->needs_texcoord_semantic,
1409 st->bitmap.tex_format ==
1410 PIPE_FORMAT_R8_UNORM);
1411
1412 if (tokens) {
1413 if (state.tokens != stfp->state.tokens)
1414 tgsi_free_tokens(state.tokens);
1415 state.tokens = tokens;
1416 } else
1417 fprintf(stderr, "mesa: cannot create a shader for glBitmap\n");
1418 }
1419
1420 /* glDrawPixels (color only) */
1421 if (key->drawpixels) {
1422 const struct tgsi_token *tokens;
1423 unsigned scale_const = 0, bias_const = 0, texcoord_const = 0;
1424
1425 /* Find the first unused slot. */
1426 variant->drawpix_sampler = ffs(~stfp->Base.SamplersUsed) - 1;
1427
1428 if (key->pixelMaps) {
1429 unsigned samplers_used = stfp->Base.SamplersUsed |
1430 (1 << variant->drawpix_sampler);
1431
1432 variant->pixelmap_sampler = ffs(~samplers_used) - 1;
1433 }
1434
1435 if (key->scaleAndBias) {
1436 scale_const = _mesa_add_state_reference(params, scale_state);
1437 bias_const = _mesa_add_state_reference(params, bias_state);
1438 }
1439
1440 texcoord_const = _mesa_add_state_reference(params, texcoord_state);
1441
1442 tokens = st_get_drawpix_shader(state.tokens,
1443 st->needs_texcoord_semantic,
1444 key->scaleAndBias, scale_const,
1445 bias_const, key->pixelMaps,
1446 variant->drawpix_sampler,
1447 variant->pixelmap_sampler,
1448 texcoord_const, st->internal_target);
1449
1450 if (tokens) {
1451 if (state.tokens != stfp->state.tokens)
1452 tgsi_free_tokens(state.tokens);
1453 state.tokens = tokens;
1454 } else
1455 fprintf(stderr, "mesa: cannot create a shader for glDrawPixels\n");
1456 }
1457
1458 if (unlikely(key->external.lower_nv12 || key->external.lower_iyuv ||
1459 key->external.lower_xy_uxvx || key->external.lower_yx_xuxv)) {
1460 const struct tgsi_token *tokens;
1461
1462 /* samplers inserted would conflict, but this should be unpossible: */
1463 assert(!(key->bitmap || key->drawpixels));
1464
1465 tokens = st_tgsi_lower_yuv(state.tokens,
1466 ~stfp->Base.SamplersUsed,
1467 key->external.lower_nv12 ||
1468 key->external.lower_xy_uxvx ||
1469 key->external.lower_yx_xuxv,
1470 key->external.lower_iyuv);
1471 if (tokens) {
1472 if (state.tokens != stfp->state.tokens)
1473 tgsi_free_tokens(state.tokens);
1474 state.tokens = tokens;
1475 } else {
1476 fprintf(stderr, "mesa: cannot create a shader for samplerExternalOES\n");
1477 }
1478 }
1479
1480 if (key->lower_depth_clamp) {
1481 unsigned depth_range_const = _mesa_add_state_reference(params, depth_range_state);
1482
1483 const struct tgsi_token *tokens;
1484 tokens = st_tgsi_lower_depth_clamp_fs(state.tokens, depth_range_const);
1485 if (state.tokens != stfp->state.tokens)
1486 tgsi_free_tokens(state.tokens);
1487 state.tokens = tokens;
1488 }
1489
1490 if (ST_DEBUG & DEBUG_PRINT_IR)
1491 tgsi_dump(state.tokens, 0);
1492
1493 /* fill in variant */
1494 variant->driver_shader = pipe->create_fs_state(pipe, &state);
1495 variant->key = *key;
1496
1497 if (state.tokens != stfp->state.tokens)
1498 tgsi_free_tokens(state.tokens);
1499 return variant;
1500 }
1501
1502 /**
1503 * Translate fragment program if needed.
1504 */
1505 struct st_fp_variant *
1506 st_get_fp_variant(struct st_context *st,
1507 struct st_common_program *stfp,
1508 const struct st_fp_variant_key *key)
1509 {
1510 struct st_fp_variant *fpv;
1511
1512 /* Search for existing variant */
1513 for (fpv = stfp->fp_variants; fpv; fpv = fpv->next) {
1514 if (memcmp(&fpv->key, key, sizeof(*key)) == 0) {
1515 break;
1516 }
1517 }
1518
1519 if (!fpv) {
1520 /* create new */
1521 fpv = st_create_fp_variant(st, stfp, key);
1522 if (fpv) {
1523 if (key->bitmap || key->drawpixels) {
1524 /* Regular variants should always come before the
1525 * bitmap & drawpixels variants, (unless there
1526 * are no regular variants) so that
1527 * st_update_fp can take a fast path when
1528 * shader_has_one_variant is set.
1529 */
1530 if (!stfp->fp_variants) {
1531 stfp->fp_variants = fpv;
1532 } else {
1533 /* insert into list after the first one */
1534 fpv->next = stfp->fp_variants->next;
1535 stfp->fp_variants->next = fpv;
1536 }
1537 } else {
1538 /* insert into list */
1539 fpv->next = stfp->fp_variants;
1540 stfp->fp_variants = fpv;
1541 }
1542 }
1543 }
1544
1545 return fpv;
1546 }
1547
1548 /**
1549 * Translate a program. This is common code for geometry and tessellation
1550 * shaders.
1551 */
1552 bool
1553 st_translate_common_program(struct st_context *st,
1554 struct st_common_program *stcp)
1555 {
1556 struct gl_program *prog = &stcp->Base;
1557 enum pipe_shader_type stage =
1558 pipe_shader_type_from_mesa(stcp->Base.info.stage);
1559 struct ureg_program *ureg = ureg_create_with_screen(stage, st->pipe->screen);
1560
1561 if (ureg == NULL)
1562 return false;
1563
1564 switch (stage) {
1565 case PIPE_SHADER_TESS_CTRL:
1566 ureg_property(ureg, TGSI_PROPERTY_TCS_VERTICES_OUT,
1567 stcp->Base.info.tess.tcs_vertices_out);
1568 break;
1569
1570 case PIPE_SHADER_TESS_EVAL:
1571 if (stcp->Base.info.tess.primitive_mode == GL_ISOLINES)
1572 ureg_property(ureg, TGSI_PROPERTY_TES_PRIM_MODE, GL_LINES);
1573 else
1574 ureg_property(ureg, TGSI_PROPERTY_TES_PRIM_MODE,
1575 stcp->Base.info.tess.primitive_mode);
1576
1577 STATIC_ASSERT((TESS_SPACING_EQUAL + 1) % 3 == PIPE_TESS_SPACING_EQUAL);
1578 STATIC_ASSERT((TESS_SPACING_FRACTIONAL_ODD + 1) % 3 ==
1579 PIPE_TESS_SPACING_FRACTIONAL_ODD);
1580 STATIC_ASSERT((TESS_SPACING_FRACTIONAL_EVEN + 1) % 3 ==
1581 PIPE_TESS_SPACING_FRACTIONAL_EVEN);
1582
1583 ureg_property(ureg, TGSI_PROPERTY_TES_SPACING,
1584 (stcp->Base.info.tess.spacing + 1) % 3);
1585
1586 ureg_property(ureg, TGSI_PROPERTY_TES_VERTEX_ORDER_CW,
1587 !stcp->Base.info.tess.ccw);
1588 ureg_property(ureg, TGSI_PROPERTY_TES_POINT_MODE,
1589 stcp->Base.info.tess.point_mode);
1590 break;
1591
1592 case PIPE_SHADER_GEOMETRY:
1593 ureg_property(ureg, TGSI_PROPERTY_GS_INPUT_PRIM,
1594 stcp->Base.info.gs.input_primitive);
1595 ureg_property(ureg, TGSI_PROPERTY_GS_OUTPUT_PRIM,
1596 stcp->Base.info.gs.output_primitive);
1597 ureg_property(ureg, TGSI_PROPERTY_GS_MAX_OUTPUT_VERTICES,
1598 stcp->Base.info.gs.vertices_out);
1599 ureg_property(ureg, TGSI_PROPERTY_GS_INVOCATIONS,
1600 stcp->Base.info.gs.invocations);
1601 break;
1602
1603 default:
1604 break;
1605 }
1606
1607 ubyte inputSlotToAttr[VARYING_SLOT_TESS_MAX];
1608 ubyte inputMapping[VARYING_SLOT_TESS_MAX];
1609 ubyte outputMapping[VARYING_SLOT_TESS_MAX];
1610 GLuint attr;
1611
1612 ubyte input_semantic_name[PIPE_MAX_SHADER_INPUTS];
1613 ubyte input_semantic_index[PIPE_MAX_SHADER_INPUTS];
1614 uint num_inputs = 0;
1615
1616 ubyte output_semantic_name[PIPE_MAX_SHADER_OUTPUTS];
1617 ubyte output_semantic_index[PIPE_MAX_SHADER_OUTPUTS];
1618 uint num_outputs = 0;
1619
1620 GLint i;
1621
1622 memset(inputSlotToAttr, 0, sizeof(inputSlotToAttr));
1623 memset(inputMapping, 0, sizeof(inputMapping));
1624 memset(outputMapping, 0, sizeof(outputMapping));
1625 memset(&stcp->state, 0, sizeof(stcp->state));
1626
1627 if (prog->info.clip_distance_array_size)
1628 ureg_property(ureg, TGSI_PROPERTY_NUM_CLIPDIST_ENABLED,
1629 prog->info.clip_distance_array_size);
1630 if (prog->info.cull_distance_array_size)
1631 ureg_property(ureg, TGSI_PROPERTY_NUM_CULLDIST_ENABLED,
1632 prog->info.cull_distance_array_size);
1633
1634 /*
1635 * Convert Mesa program inputs to TGSI input register semantics.
1636 */
1637 for (attr = 0; attr < VARYING_SLOT_MAX; attr++) {
1638 if ((prog->info.inputs_read & BITFIELD64_BIT(attr)) == 0)
1639 continue;
1640
1641 unsigned slot = num_inputs++;
1642
1643 inputMapping[attr] = slot;
1644 inputSlotToAttr[slot] = attr;
1645
1646 unsigned semantic_name, semantic_index;
1647 tgsi_get_gl_varying_semantic(attr, st->needs_texcoord_semantic,
1648 &semantic_name, &semantic_index);
1649 input_semantic_name[slot] = semantic_name;
1650 input_semantic_index[slot] = semantic_index;
1651 }
1652
1653 /* Also add patch inputs. */
1654 for (attr = 0; attr < 32; attr++) {
1655 if (prog->info.patch_inputs_read & (1u << attr)) {
1656 GLuint slot = num_inputs++;
1657 GLuint patch_attr = VARYING_SLOT_PATCH0 + attr;
1658
1659 inputMapping[patch_attr] = slot;
1660 inputSlotToAttr[slot] = patch_attr;
1661 input_semantic_name[slot] = TGSI_SEMANTIC_PATCH;
1662 input_semantic_index[slot] = attr;
1663 }
1664 }
1665
1666 /* initialize output semantics to defaults */
1667 for (i = 0; i < PIPE_MAX_SHADER_OUTPUTS; i++) {
1668 output_semantic_name[i] = TGSI_SEMANTIC_GENERIC;
1669 output_semantic_index[i] = 0;
1670 }
1671
1672 /*
1673 * Determine number of outputs, the (default) output register
1674 * mapping and the semantic information for each output.
1675 */
1676 for (attr = 0; attr < VARYING_SLOT_MAX; attr++) {
1677 if (prog->info.outputs_written & BITFIELD64_BIT(attr)) {
1678 GLuint slot = num_outputs++;
1679
1680 outputMapping[attr] = slot;
1681
1682 unsigned semantic_name, semantic_index;
1683 tgsi_get_gl_varying_semantic(attr, st->needs_texcoord_semantic,
1684 &semantic_name, &semantic_index);
1685 output_semantic_name[slot] = semantic_name;
1686 output_semantic_index[slot] = semantic_index;
1687 }
1688 }
1689
1690 /* Also add patch outputs. */
1691 for (attr = 0; attr < 32; attr++) {
1692 if (prog->info.patch_outputs_written & (1u << attr)) {
1693 GLuint slot = num_outputs++;
1694 GLuint patch_attr = VARYING_SLOT_PATCH0 + attr;
1695
1696 outputMapping[patch_attr] = slot;
1697 output_semantic_name[slot] = TGSI_SEMANTIC_PATCH;
1698 output_semantic_index[slot] = attr;
1699 }
1700 }
1701
1702 st_translate_program(st->ctx,
1703 stage,
1704 ureg,
1705 stcp->glsl_to_tgsi,
1706 prog,
1707 /* inputs */
1708 num_inputs,
1709 inputMapping,
1710 inputSlotToAttr,
1711 input_semantic_name,
1712 input_semantic_index,
1713 NULL,
1714 /* outputs */
1715 num_outputs,
1716 outputMapping,
1717 output_semantic_name,
1718 output_semantic_index);
1719
1720 stcp->state.tokens = ureg_get_tokens(ureg, NULL);
1721
1722 ureg_destroy(ureg);
1723
1724 st_translate_stream_output_info(prog);
1725
1726 st_store_ir_in_disk_cache(st, prog, false);
1727
1728 if (ST_DEBUG & DEBUG_PRINT_IR && ST_DEBUG & DEBUG_MESA)
1729 _mesa_print_program(prog);
1730
1731 free_glsl_to_tgsi_visitor(stcp->glsl_to_tgsi);
1732 stcp->glsl_to_tgsi = NULL;
1733 return true;
1734 }
1735
1736
1737 /**
1738 * Get/create a basic program variant.
1739 */
1740 struct st_common_variant *
1741 st_get_common_variant(struct st_context *st,
1742 struct st_common_program *prog,
1743 const struct st_common_variant_key *key)
1744 {
1745 struct pipe_context *pipe = st->pipe;
1746 struct st_common_variant *v;
1747 struct pipe_shader_state state = {0};
1748
1749 /* Search for existing variant */
1750 for (v = prog->variants; v; v = v->next) {
1751 if (memcmp(&v->key, key, sizeof(*key)) == 0) {
1752 break;
1753 }
1754 }
1755
1756 if (!v) {
1757 /* create new */
1758 v = CALLOC_STRUCT(st_common_variant);
1759 if (v) {
1760 if (prog->state.type == PIPE_SHADER_IR_NIR) {
1761 bool finalize = false;
1762
1763 state.type = PIPE_SHADER_IR_NIR;
1764 state.ir.nir = nir_shader_clone(NULL, prog->state.ir.nir);
1765
1766 if (key->clamp_color) {
1767 NIR_PASS_V(state.ir.nir, nir_lower_clamp_color_outputs);
1768 finalize = true;
1769 }
1770
1771 state.stream_output = prog->state.stream_output;
1772
1773 if (finalize || !st->allow_st_finalize_nir_twice) {
1774 st_finalize_nir(st, &prog->Base, prog->shader_program,
1775 state.ir.nir, true);
1776 }
1777 } else {
1778 if (key->lower_depth_clamp) {
1779 struct gl_program_parameter_list *params = prog->Base.Parameters;
1780
1781 unsigned depth_range_const =
1782 _mesa_add_state_reference(params, depth_range_state);
1783
1784 const struct tgsi_token *tokens;
1785 tokens =
1786 st_tgsi_lower_depth_clamp(prog->state.tokens,
1787 depth_range_const,
1788 key->clip_negative_one_to_one);
1789
1790 if (tokens != prog->state.tokens)
1791 tgsi_free_tokens(prog->state.tokens);
1792
1793 prog->state.tokens = tokens;
1794 }
1795 state = prog->state;
1796
1797 if (ST_DEBUG & DEBUG_PRINT_IR)
1798 tgsi_dump(state.tokens, 0);
1799 }
1800 /* fill in new variant */
1801 switch (prog->Base.info.stage) {
1802 case MESA_SHADER_TESS_CTRL:
1803 v->driver_shader = pipe->create_tcs_state(pipe, &state);
1804 break;
1805 case MESA_SHADER_TESS_EVAL:
1806 v->driver_shader = pipe->create_tes_state(pipe, &state);
1807 break;
1808 case MESA_SHADER_GEOMETRY:
1809 v->driver_shader = pipe->create_gs_state(pipe, &state);
1810 break;
1811 case MESA_SHADER_COMPUTE: {
1812 struct pipe_compute_state cs = {0};
1813 cs.ir_type = state.type;
1814 cs.req_local_mem = prog->Base.info.cs.shared_size;
1815
1816 if (state.type == PIPE_SHADER_IR_NIR)
1817 cs.prog = state.ir.nir;
1818 else
1819 cs.prog = state.tokens;
1820
1821 v->driver_shader = pipe->create_compute_state(pipe, &cs);
1822 break;
1823 }
1824 default:
1825 assert(!"unhandled shader type");
1826 free(v);
1827 return NULL;
1828 }
1829
1830 v->key = *key;
1831
1832 /* insert into list */
1833 v->next = prog->variants;
1834 prog->variants = v;
1835 }
1836 }
1837
1838 return v;
1839 }
1840
1841
1842 /**
1843 * Vert/Geom/Frag programs have per-context variants. Free all the
1844 * variants attached to the given program which match the given context.
1845 */
1846 static void
1847 destroy_program_variants(struct st_context *st, struct gl_program *target)
1848 {
1849 if (!target || target == &_mesa_DummyProgram)
1850 return;
1851
1852 switch (target->Target) {
1853 case GL_VERTEX_PROGRAM_ARB:
1854 {
1855 struct st_vertex_program *stvp = (struct st_vertex_program *) target;
1856 struct st_vp_variant *vpv, **prevPtr = &stvp->variants;
1857
1858 for (vpv = stvp->variants; vpv; ) {
1859 struct st_vp_variant *next = vpv->next;
1860 if (vpv->key.st == st) {
1861 /* unlink from list */
1862 *prevPtr = next;
1863 /* destroy this variant */
1864 delete_vp_variant(st, vpv);
1865 }
1866 else {
1867 prevPtr = &vpv->next;
1868 }
1869 vpv = next;
1870 }
1871 }
1872 break;
1873 case GL_FRAGMENT_PROGRAM_ARB:
1874 {
1875 struct st_common_program *stfp =
1876 (struct st_common_program *) target;
1877 struct st_fp_variant *fpv, **prevPtr = &stfp->fp_variants;
1878
1879 for (fpv = stfp->fp_variants; fpv; ) {
1880 struct st_fp_variant *next = fpv->next;
1881 if (fpv->key.st == st) {
1882 /* unlink from list */
1883 *prevPtr = next;
1884 /* destroy this variant */
1885 delete_fp_variant(st, fpv);
1886 }
1887 else {
1888 prevPtr = &fpv->next;
1889 }
1890 fpv = next;
1891 }
1892 }
1893 break;
1894 case GL_GEOMETRY_PROGRAM_NV:
1895 case GL_TESS_CONTROL_PROGRAM_NV:
1896 case GL_TESS_EVALUATION_PROGRAM_NV:
1897 case GL_COMPUTE_PROGRAM_NV:
1898 {
1899 struct st_common_program *p = st_common_program(target);
1900 struct st_common_variant *v, **prevPtr = &p->variants;
1901
1902 for (v = p->variants; v; ) {
1903 struct st_common_variant *next = v->next;
1904 if (v->key.st == st) {
1905 /* unlink from list */
1906 *prevPtr = next;
1907 /* destroy this variant */
1908 delete_basic_variant(st, v, target->Target);
1909 }
1910 else {
1911 prevPtr = &v->next;
1912 }
1913 v = next;
1914 }
1915 }
1916 break;
1917 default:
1918 _mesa_problem(NULL, "Unexpected program target 0x%x in "
1919 "destroy_program_variants_cb()", target->Target);
1920 }
1921 }
1922
1923
1924 /**
1925 * Callback for _mesa_HashWalk. Free all the shader's program variants
1926 * which match the given context.
1927 */
1928 static void
1929 destroy_shader_program_variants_cb(GLuint key, void *data, void *userData)
1930 {
1931 struct st_context *st = (struct st_context *) userData;
1932 struct gl_shader *shader = (struct gl_shader *) data;
1933
1934 switch (shader->Type) {
1935 case GL_SHADER_PROGRAM_MESA:
1936 {
1937 struct gl_shader_program *shProg = (struct gl_shader_program *) data;
1938 GLuint i;
1939
1940 for (i = 0; i < ARRAY_SIZE(shProg->_LinkedShaders); i++) {
1941 if (shProg->_LinkedShaders[i])
1942 destroy_program_variants(st, shProg->_LinkedShaders[i]->Program);
1943 }
1944 }
1945 break;
1946 case GL_VERTEX_SHADER:
1947 case GL_FRAGMENT_SHADER:
1948 case GL_GEOMETRY_SHADER:
1949 case GL_TESS_CONTROL_SHADER:
1950 case GL_TESS_EVALUATION_SHADER:
1951 case GL_COMPUTE_SHADER:
1952 break;
1953 default:
1954 assert(0);
1955 }
1956 }
1957
1958
1959 /**
1960 * Callback for _mesa_HashWalk. Free all the program variants which match
1961 * the given context.
1962 */
1963 static void
1964 destroy_program_variants_cb(GLuint key, void *data, void *userData)
1965 {
1966 struct st_context *st = (struct st_context *) userData;
1967 struct gl_program *program = (struct gl_program *) data;
1968 destroy_program_variants(st, program);
1969 }
1970
1971
1972 /**
1973 * Walk over all shaders and programs to delete any variants which
1974 * belong to the given context.
1975 * This is called during context tear-down.
1976 */
1977 void
1978 st_destroy_program_variants(struct st_context *st)
1979 {
1980 /* If shaders can be shared with other contexts, the last context will
1981 * call DeleteProgram on all shaders, releasing everything.
1982 */
1983 if (st->has_shareable_shaders)
1984 return;
1985
1986 /* ARB vert/frag program */
1987 _mesa_HashWalk(st->ctx->Shared->Programs,
1988 destroy_program_variants_cb, st);
1989
1990 /* GLSL vert/frag/geom shaders */
1991 _mesa_HashWalk(st->ctx->Shared->ShaderObjects,
1992 destroy_shader_program_variants_cb, st);
1993 }
1994
1995
1996 /**
1997 * For debugging, print/dump the current vertex program.
1998 */
1999 void
2000 st_print_current_vertex_program(void)
2001 {
2002 GET_CURRENT_CONTEXT(ctx);
2003
2004 if (ctx->VertexProgram._Current) {
2005 struct st_vertex_program *stvp =
2006 (struct st_vertex_program *) ctx->VertexProgram._Current;
2007 struct st_vp_variant *stv;
2008
2009 debug_printf("Vertex program %u\n", stvp->Base.Id);
2010
2011 for (stv = stvp->variants; stv; stv = stv->next) {
2012 debug_printf("variant %p\n", stv);
2013 tgsi_dump(stv->tokens, 0);
2014 }
2015 }
2016 }
2017
2018
2019 /**
2020 * Compile one shader variant.
2021 */
2022 void
2023 st_precompile_shader_variant(struct st_context *st,
2024 struct gl_program *prog)
2025 {
2026 switch (prog->Target) {
2027 case GL_VERTEX_PROGRAM_ARB: {
2028 struct st_vertex_program *p = (struct st_vertex_program *)prog;
2029 struct st_common_variant_key key;
2030
2031 memset(&key, 0, sizeof(key));
2032
2033 key.st = st->has_shareable_shaders ? NULL : st;
2034 st_get_vp_variant(st, p, &key);
2035 break;
2036 }
2037
2038 case GL_FRAGMENT_PROGRAM_ARB: {
2039 struct st_common_program *p = (struct st_common_program *)prog;
2040 struct st_fp_variant_key key;
2041
2042 memset(&key, 0, sizeof(key));
2043
2044 key.st = st->has_shareable_shaders ? NULL : st;
2045 st_get_fp_variant(st, p, &key);
2046 break;
2047 }
2048
2049 case GL_TESS_CONTROL_PROGRAM_NV:
2050 case GL_TESS_EVALUATION_PROGRAM_NV:
2051 case GL_GEOMETRY_PROGRAM_NV:
2052 case GL_COMPUTE_PROGRAM_NV: {
2053 struct st_common_program *p = st_common_program(prog);
2054 struct st_common_variant_key key;
2055
2056 memset(&key, 0, sizeof(key));
2057
2058 key.st = st->has_shareable_shaders ? NULL : st;
2059 st_get_common_variant(st, p, &key);
2060 break;
2061 }
2062
2063 default:
2064 assert(0);
2065 }
2066 }