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