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