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