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