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