st_program.c revision 848b8605
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 "pipe/p_context.h" 42#include "pipe/p_defines.h" 43#include "pipe/p_shader_tokens.h" 44#include "draw/draw_context.h" 45#include "tgsi/tgsi_dump.h" 46#include "tgsi/tgsi_ureg.h" 47 48#include "st_debug.h" 49#include "st_cb_bitmap.h" 50#include "st_cb_drawpixels.h" 51#include "st_context.h" 52#include "st_program.h" 53#include "st_mesa_to_tgsi.h" 54#include "cso_cache/cso_context.h" 55 56 57 58/** 59 * Delete a vertex program variant. Note the caller must unlink 60 * the variant from the linked list. 61 */ 62static void 63delete_vp_variant(struct st_context *st, struct st_vp_variant *vpv) 64{ 65 if (vpv->driver_shader) 66 cso_delete_vertex_shader(st->cso_context, vpv->driver_shader); 67 68 if (vpv->draw_shader) 69 draw_delete_vertex_shader( st->draw, vpv->draw_shader ); 70 71 if (vpv->tgsi.tokens) 72 st_free_tokens(vpv->tgsi.tokens); 73 74 free( vpv ); 75} 76 77 78 79/** 80 * Clean out any old compilations: 81 */ 82void 83st_release_vp_variants( struct st_context *st, 84 struct st_vertex_program *stvp ) 85{ 86 struct st_vp_variant *vpv; 87 88 for (vpv = stvp->variants; vpv; ) { 89 struct st_vp_variant *next = vpv->next; 90 delete_vp_variant(st, vpv); 91 vpv = next; 92 } 93 94 stvp->variants = NULL; 95} 96 97 98 99/** 100 * Delete a fragment program variant. Note the caller must unlink 101 * the variant from the linked list. 102 */ 103static void 104delete_fp_variant(struct st_context *st, struct st_fp_variant *fpv) 105{ 106 if (fpv->driver_shader) 107 cso_delete_fragment_shader(st->cso_context, fpv->driver_shader); 108 if (fpv->parameters) 109 _mesa_free_parameter_list(fpv->parameters); 110 if (fpv->tgsi.tokens) 111 st_free_tokens(fpv->tgsi.tokens); 112 free(fpv); 113} 114 115 116/** 117 * Free all variants of a fragment program. 118 */ 119void 120st_release_fp_variants(struct st_context *st, struct st_fragment_program *stfp) 121{ 122 struct st_fp_variant *fpv; 123 124 for (fpv = stfp->variants; fpv; ) { 125 struct st_fp_variant *next = fpv->next; 126 delete_fp_variant(st, fpv); 127 fpv = next; 128 } 129 130 stfp->variants = NULL; 131} 132 133 134/** 135 * Delete a geometry program variant. Note the caller must unlink 136 * the variant from the linked list. 137 */ 138static void 139delete_gp_variant(struct st_context *st, struct st_gp_variant *gpv) 140{ 141 if (gpv->driver_shader) 142 cso_delete_geometry_shader(st->cso_context, gpv->driver_shader); 143 144 free(gpv); 145} 146 147 148/** 149 * Free all variants of a geometry program. 150 */ 151void 152st_release_gp_variants(struct st_context *st, struct st_geometry_program *stgp) 153{ 154 struct st_gp_variant *gpv; 155 156 for (gpv = stgp->variants; gpv; ) { 157 struct st_gp_variant *next = gpv->next; 158 delete_gp_variant(st, gpv); 159 gpv = next; 160 } 161 162 stgp->variants = NULL; 163} 164 165 166 167 168/** 169 * Translate a Mesa vertex shader into a TGSI shader. 170 * \param outputMapping to map vertex program output registers (VARYING_SLOT_x) 171 * to TGSI output slots 172 * \param tokensOut destination for TGSI tokens 173 * \return pointer to cached pipe_shader object. 174 */ 175void 176st_prepare_vertex_program(struct gl_context *ctx, 177 struct st_vertex_program *stvp) 178{ 179 struct st_context *st = st_context(ctx); 180 GLuint attr; 181 182 stvp->num_inputs = 0; 183 stvp->num_outputs = 0; 184 185 if (stvp->Base.IsPositionInvariant) 186 _mesa_insert_mvp_code(ctx, &stvp->Base); 187 188 if (!stvp->glsl_to_tgsi) 189 assert(stvp->Base.Base.NumInstructions > 1); 190 191 /* 192 * Determine number of inputs, the mappings between VERT_ATTRIB_x 193 * and TGSI generic input indexes, plus input attrib semantic info. 194 */ 195 for (attr = 0; attr < VERT_ATTRIB_MAX; attr++) { 196 if ((stvp->Base.Base.InputsRead & BITFIELD64_BIT(attr)) != 0) { 197 stvp->input_to_index[attr] = stvp->num_inputs; 198 stvp->index_to_input[stvp->num_inputs] = attr; 199 stvp->num_inputs++; 200 } 201 } 202 /* bit of a hack, presetup potentially unused edgeflag input */ 203 stvp->input_to_index[VERT_ATTRIB_EDGEFLAG] = stvp->num_inputs; 204 stvp->index_to_input[stvp->num_inputs] = VERT_ATTRIB_EDGEFLAG; 205 206 /* Compute mapping of vertex program outputs to slots. 207 */ 208 for (attr = 0; attr < VARYING_SLOT_MAX; attr++) { 209 if ((stvp->Base.Base.OutputsWritten & BITFIELD64_BIT(attr)) == 0) { 210 stvp->result_to_output[attr] = ~0; 211 } 212 else { 213 unsigned slot = stvp->num_outputs++; 214 215 stvp->result_to_output[attr] = slot; 216 217 switch (attr) { 218 case VARYING_SLOT_POS: 219 stvp->output_semantic_name[slot] = TGSI_SEMANTIC_POSITION; 220 stvp->output_semantic_index[slot] = 0; 221 break; 222 case VARYING_SLOT_COL0: 223 stvp->output_semantic_name[slot] = TGSI_SEMANTIC_COLOR; 224 stvp->output_semantic_index[slot] = 0; 225 break; 226 case VARYING_SLOT_COL1: 227 stvp->output_semantic_name[slot] = TGSI_SEMANTIC_COLOR; 228 stvp->output_semantic_index[slot] = 1; 229 break; 230 case VARYING_SLOT_BFC0: 231 stvp->output_semantic_name[slot] = TGSI_SEMANTIC_BCOLOR; 232 stvp->output_semantic_index[slot] = 0; 233 break; 234 case VARYING_SLOT_BFC1: 235 stvp->output_semantic_name[slot] = TGSI_SEMANTIC_BCOLOR; 236 stvp->output_semantic_index[slot] = 1; 237 break; 238 case VARYING_SLOT_FOGC: 239 stvp->output_semantic_name[slot] = TGSI_SEMANTIC_FOG; 240 stvp->output_semantic_index[slot] = 0; 241 break; 242 case VARYING_SLOT_PSIZ: 243 stvp->output_semantic_name[slot] = TGSI_SEMANTIC_PSIZE; 244 stvp->output_semantic_index[slot] = 0; 245 break; 246 case VARYING_SLOT_CLIP_DIST0: 247 stvp->output_semantic_name[slot] = TGSI_SEMANTIC_CLIPDIST; 248 stvp->output_semantic_index[slot] = 0; 249 break; 250 case VARYING_SLOT_CLIP_DIST1: 251 stvp->output_semantic_name[slot] = TGSI_SEMANTIC_CLIPDIST; 252 stvp->output_semantic_index[slot] = 1; 253 break; 254 case VARYING_SLOT_EDGE: 255 assert(0); 256 break; 257 case VARYING_SLOT_CLIP_VERTEX: 258 stvp->output_semantic_name[slot] = TGSI_SEMANTIC_CLIPVERTEX; 259 stvp->output_semantic_index[slot] = 0; 260 break; 261 case VARYING_SLOT_LAYER: 262 stvp->output_semantic_name[slot] = TGSI_SEMANTIC_LAYER; 263 stvp->output_semantic_index[slot] = 0; 264 break; 265 case VARYING_SLOT_VIEWPORT: 266 stvp->output_semantic_name[slot] = TGSI_SEMANTIC_VIEWPORT_INDEX; 267 stvp->output_semantic_index[slot] = 0; 268 break; 269 270 case VARYING_SLOT_TEX0: 271 case VARYING_SLOT_TEX1: 272 case VARYING_SLOT_TEX2: 273 case VARYING_SLOT_TEX3: 274 case VARYING_SLOT_TEX4: 275 case VARYING_SLOT_TEX5: 276 case VARYING_SLOT_TEX6: 277 case VARYING_SLOT_TEX7: 278 stvp->output_semantic_name[slot] = st->needs_texcoord_semantic ? 279 TGSI_SEMANTIC_TEXCOORD : TGSI_SEMANTIC_GENERIC; 280 stvp->output_semantic_index[slot] = attr - VARYING_SLOT_TEX0; 281 break; 282 283 case VARYING_SLOT_VAR0: 284 default: 285 assert(attr < VARYING_SLOT_MAX); 286 stvp->output_semantic_name[slot] = TGSI_SEMANTIC_GENERIC; 287 stvp->output_semantic_index[slot] = st->needs_texcoord_semantic ? 288 (attr - VARYING_SLOT_VAR0) : (attr - VARYING_SLOT_TEX0); 289 break; 290 } 291 } 292 } 293 /* similar hack to above, presetup potentially unused edgeflag output */ 294 stvp->result_to_output[VARYING_SLOT_EDGE] = stvp->num_outputs; 295 stvp->output_semantic_name[stvp->num_outputs] = TGSI_SEMANTIC_EDGEFLAG; 296 stvp->output_semantic_index[stvp->num_outputs] = 0; 297} 298 299 300/** 301 * Translate a vertex program to create a new variant. 302 */ 303static struct st_vp_variant * 304st_translate_vertex_program(struct st_context *st, 305 struct st_vertex_program *stvp, 306 const struct st_vp_variant_key *key) 307{ 308 struct st_vp_variant *vpv = CALLOC_STRUCT(st_vp_variant); 309 struct pipe_context *pipe = st->pipe; 310 struct ureg_program *ureg; 311 enum pipe_error error; 312 unsigned num_outputs; 313 314 st_prepare_vertex_program(st->ctx, stvp); 315 316 if (!stvp->glsl_to_tgsi) 317 { 318 _mesa_remove_output_reads(&stvp->Base.Base, PROGRAM_OUTPUT); 319 } 320 321 ureg = ureg_create( TGSI_PROCESSOR_VERTEX ); 322 if (ureg == NULL) { 323 free(vpv); 324 return NULL; 325 } 326 327 vpv->key = *key; 328 329 vpv->num_inputs = stvp->num_inputs; 330 num_outputs = stvp->num_outputs; 331 if (key->passthrough_edgeflags) { 332 vpv->num_inputs++; 333 num_outputs++; 334 } 335 336 if (ST_DEBUG & DEBUG_MESA) { 337 _mesa_print_program(&stvp->Base.Base); 338 _mesa_print_program_parameters(st->ctx, &stvp->Base.Base); 339 debug_printf("\n"); 340 } 341 342 if (stvp->glsl_to_tgsi) 343 error = st_translate_program(st->ctx, 344 TGSI_PROCESSOR_VERTEX, 345 ureg, 346 stvp->glsl_to_tgsi, 347 &stvp->Base.Base, 348 /* inputs */ 349 vpv->num_inputs, 350 stvp->input_to_index, 351 NULL, /* input semantic name */ 352 NULL, /* input semantic index */ 353 NULL, /* interp mode */ 354 NULL, /* interp location */ 355 /* outputs */ 356 num_outputs, 357 stvp->result_to_output, 358 stvp->output_semantic_name, 359 stvp->output_semantic_index, 360 key->passthrough_edgeflags, 361 key->clamp_color); 362 else 363 error = st_translate_mesa_program(st->ctx, 364 TGSI_PROCESSOR_VERTEX, 365 ureg, 366 &stvp->Base.Base, 367 /* inputs */ 368 vpv->num_inputs, 369 stvp->input_to_index, 370 NULL, /* input semantic name */ 371 NULL, /* input semantic index */ 372 NULL, 373 /* outputs */ 374 num_outputs, 375 stvp->result_to_output, 376 stvp->output_semantic_name, 377 stvp->output_semantic_index, 378 key->passthrough_edgeflags, 379 key->clamp_color); 380 381 if (error) 382 goto fail; 383 384 vpv->tgsi.tokens = ureg_get_tokens( ureg, NULL ); 385 if (!vpv->tgsi.tokens) 386 goto fail; 387 388 ureg_destroy( ureg ); 389 390 if (stvp->glsl_to_tgsi) { 391 st_translate_stream_output_info(stvp->glsl_to_tgsi, 392 stvp->result_to_output, 393 &vpv->tgsi.stream_output); 394 } 395 396 if (ST_DEBUG & DEBUG_TGSI) { 397 tgsi_dump(vpv->tgsi.tokens, 0); 398 debug_printf("\n"); 399 } 400 401 vpv->driver_shader = pipe->create_vs_state(pipe, &vpv->tgsi); 402 return vpv; 403 404fail: 405 debug_printf("%s: failed to translate Mesa program:\n", __FUNCTION__); 406 _mesa_print_program(&stvp->Base.Base); 407 debug_assert(0); 408 409 ureg_destroy( ureg ); 410 return NULL; 411} 412 413 414/** 415 * Find/create a vertex program variant. 416 */ 417struct st_vp_variant * 418st_get_vp_variant(struct st_context *st, 419 struct st_vertex_program *stvp, 420 const struct st_vp_variant_key *key) 421{ 422 struct st_vp_variant *vpv; 423 424 /* Search for existing variant */ 425 for (vpv = stvp->variants; vpv; vpv = vpv->next) { 426 if (memcmp(&vpv->key, key, sizeof(*key)) == 0) { 427 break; 428 } 429 } 430 431 if (!vpv) { 432 /* create now */ 433 vpv = st_translate_vertex_program(st, stvp, key); 434 if (vpv) { 435 /* insert into list */ 436 vpv->next = stvp->variants; 437 stvp->variants = vpv; 438 } 439 } 440 441 return vpv; 442} 443 444 445static unsigned 446st_translate_interp(enum glsl_interp_qualifier glsl_qual, bool is_color) 447{ 448 switch (glsl_qual) { 449 case INTERP_QUALIFIER_NONE: 450 if (is_color) 451 return TGSI_INTERPOLATE_COLOR; 452 return TGSI_INTERPOLATE_PERSPECTIVE; 453 case INTERP_QUALIFIER_SMOOTH: 454 return TGSI_INTERPOLATE_PERSPECTIVE; 455 case INTERP_QUALIFIER_FLAT: 456 return TGSI_INTERPOLATE_CONSTANT; 457 case INTERP_QUALIFIER_NOPERSPECTIVE: 458 return TGSI_INTERPOLATE_LINEAR; 459 default: 460 assert(0 && "unexpected interp mode in st_translate_interp()"); 461 return TGSI_INTERPOLATE_PERSPECTIVE; 462 } 463} 464 465 466/** 467 * Translate a Mesa fragment shader into a TGSI shader using extra info in 468 * the key. 469 * \return new fragment program variant 470 */ 471static struct st_fp_variant * 472st_translate_fragment_program(struct st_context *st, 473 struct st_fragment_program *stfp, 474 const struct st_fp_variant_key *key) 475{ 476 struct pipe_context *pipe = st->pipe; 477 struct st_fp_variant *variant = CALLOC_STRUCT(st_fp_variant); 478 GLboolean deleteFP = GL_FALSE; 479 480 GLuint outputMapping[FRAG_RESULT_MAX]; 481 GLuint inputMapping[VARYING_SLOT_MAX]; 482 GLuint interpMode[PIPE_MAX_SHADER_INPUTS]; /* XXX size? */ 483 GLuint interpLocation[PIPE_MAX_SHADER_INPUTS]; 484 GLuint attr; 485 GLbitfield64 inputsRead; 486 struct ureg_program *ureg; 487 488 GLboolean write_all = GL_FALSE; 489 490 ubyte input_semantic_name[PIPE_MAX_SHADER_INPUTS]; 491 ubyte input_semantic_index[PIPE_MAX_SHADER_INPUTS]; 492 uint fs_num_inputs = 0; 493 494 ubyte fs_output_semantic_name[PIPE_MAX_SHADER_OUTPUTS]; 495 ubyte fs_output_semantic_index[PIPE_MAX_SHADER_OUTPUTS]; 496 uint fs_num_outputs = 0; 497 498 if (!variant) 499 return NULL; 500 501 assert(!(key->bitmap && key->drawpixels)); 502 503 if (key->bitmap) { 504 /* glBitmap drawing */ 505 struct gl_fragment_program *fp; /* we free this temp program below */ 506 507 st_make_bitmap_fragment_program(st, &stfp->Base, 508 &fp, &variant->bitmap_sampler); 509 510 variant->parameters = _mesa_clone_parameter_list(fp->Base.Parameters); 511 stfp = st_fragment_program(fp); 512 deleteFP = GL_TRUE; 513 } 514 else if (key->drawpixels) { 515 /* glDrawPixels drawing */ 516 struct gl_fragment_program *fp; /* we free this temp program below */ 517 518 if (key->drawpixels_z || key->drawpixels_stencil) { 519 fp = st_make_drawpix_z_stencil_program(st, key->drawpixels_z, 520 key->drawpixels_stencil); 521 } 522 else { 523 /* RGBA */ 524 st_make_drawpix_fragment_program(st, &stfp->Base, &fp); 525 variant->parameters = _mesa_clone_parameter_list(fp->Base.Parameters); 526 deleteFP = GL_TRUE; 527 } 528 stfp = st_fragment_program(fp); 529 } 530 531 if (!stfp->glsl_to_tgsi) 532 _mesa_remove_output_reads(&stfp->Base.Base, PROGRAM_OUTPUT); 533 534 /* 535 * Convert Mesa program inputs to TGSI input register semantics. 536 */ 537 inputsRead = stfp->Base.Base.InputsRead; 538 for (attr = 0; attr < VARYING_SLOT_MAX; attr++) { 539 if ((inputsRead & BITFIELD64_BIT(attr)) != 0) { 540 const GLuint slot = fs_num_inputs++; 541 542 inputMapping[attr] = slot; 543 if (stfp->Base.IsCentroid & BITFIELD64_BIT(attr)) 544 interpLocation[slot] = TGSI_INTERPOLATE_LOC_CENTROID; 545 else if (stfp->Base.IsSample & BITFIELD64_BIT(attr)) 546 interpLocation[slot] = TGSI_INTERPOLATE_LOC_SAMPLE; 547 else 548 interpLocation[slot] = TGSI_INTERPOLATE_LOC_CENTER; 549 550 if (key->persample_shading) 551 interpLocation[slot] = TGSI_INTERPOLATE_LOC_SAMPLE; 552 553 switch (attr) { 554 case VARYING_SLOT_POS: 555 input_semantic_name[slot] = TGSI_SEMANTIC_POSITION; 556 input_semantic_index[slot] = 0; 557 interpMode[slot] = TGSI_INTERPOLATE_LINEAR; 558 break; 559 case VARYING_SLOT_COL0: 560 input_semantic_name[slot] = TGSI_SEMANTIC_COLOR; 561 input_semantic_index[slot] = 0; 562 interpMode[slot] = st_translate_interp(stfp->Base.InterpQualifier[attr], 563 TRUE); 564 break; 565 case VARYING_SLOT_COL1: 566 input_semantic_name[slot] = TGSI_SEMANTIC_COLOR; 567 input_semantic_index[slot] = 1; 568 interpMode[slot] = st_translate_interp(stfp->Base.InterpQualifier[attr], 569 TRUE); 570 break; 571 case VARYING_SLOT_FOGC: 572 input_semantic_name[slot] = TGSI_SEMANTIC_FOG; 573 input_semantic_index[slot] = 0; 574 interpMode[slot] = TGSI_INTERPOLATE_PERSPECTIVE; 575 break; 576 case VARYING_SLOT_FACE: 577 input_semantic_name[slot] = TGSI_SEMANTIC_FACE; 578 input_semantic_index[slot] = 0; 579 interpMode[slot] = TGSI_INTERPOLATE_CONSTANT; 580 break; 581 case VARYING_SLOT_PRIMITIVE_ID: 582 input_semantic_name[slot] = TGSI_SEMANTIC_PRIMID; 583 input_semantic_index[slot] = 0; 584 interpMode[slot] = TGSI_INTERPOLATE_CONSTANT; 585 break; 586 case VARYING_SLOT_LAYER: 587 input_semantic_name[slot] = TGSI_SEMANTIC_LAYER; 588 input_semantic_index[slot] = 0; 589 interpMode[slot] = TGSI_INTERPOLATE_CONSTANT; 590 break; 591 case VARYING_SLOT_VIEWPORT: 592 input_semantic_name[slot] = TGSI_SEMANTIC_VIEWPORT_INDEX; 593 input_semantic_index[slot] = 0; 594 interpMode[slot] = TGSI_INTERPOLATE_CONSTANT; 595 break; 596 case VARYING_SLOT_CLIP_DIST0: 597 input_semantic_name[slot] = TGSI_SEMANTIC_CLIPDIST; 598 input_semantic_index[slot] = 0; 599 interpMode[slot] = TGSI_INTERPOLATE_PERSPECTIVE; 600 break; 601 case VARYING_SLOT_CLIP_DIST1: 602 input_semantic_name[slot] = TGSI_SEMANTIC_CLIPDIST; 603 input_semantic_index[slot] = 1; 604 interpMode[slot] = TGSI_INTERPOLATE_PERSPECTIVE; 605 break; 606 /* In most cases, there is nothing special about these 607 * inputs, so adopt a convention to use the generic 608 * semantic name and the mesa VARYING_SLOT_ number as the 609 * index. 610 * 611 * All that is required is that the vertex shader labels 612 * its own outputs similarly, and that the vertex shader 613 * generates at least every output required by the 614 * fragment shader plus fixed-function hardware (such as 615 * BFC). 616 * 617 * However, some drivers may need us to identify the PNTC and TEXi 618 * varyings if, for example, their capability to replace them with 619 * sprite coordinates is limited. 620 */ 621 case VARYING_SLOT_PNTC: 622 if (st->needs_texcoord_semantic) { 623 input_semantic_name[slot] = TGSI_SEMANTIC_PCOORD; 624 input_semantic_index[slot] = 0; 625 interpMode[slot] = TGSI_INTERPOLATE_LINEAR; 626 break; 627 } 628 /* fall through */ 629 case VARYING_SLOT_TEX0: 630 case VARYING_SLOT_TEX1: 631 case VARYING_SLOT_TEX2: 632 case VARYING_SLOT_TEX3: 633 case VARYING_SLOT_TEX4: 634 case VARYING_SLOT_TEX5: 635 case VARYING_SLOT_TEX6: 636 case VARYING_SLOT_TEX7: 637 if (st->needs_texcoord_semantic) { 638 input_semantic_name[slot] = TGSI_SEMANTIC_TEXCOORD; 639 input_semantic_index[slot] = attr - VARYING_SLOT_TEX0; 640 interpMode[slot] = 641 st_translate_interp(stfp->Base.InterpQualifier[attr], FALSE); 642 break; 643 } 644 /* fall through */ 645 case VARYING_SLOT_VAR0: 646 default: 647 /* Semantic indices should be zero-based because drivers may choose 648 * to assign a fixed slot determined by that index. 649 * This is useful because ARB_separate_shader_objects uses location 650 * qualifiers for linkage, and if the semantic index corresponds to 651 * these locations, linkage passes in the driver become unecessary. 652 * 653 * If needs_texcoord_semantic is true, no semantic indices will be 654 * consumed for the TEXi varyings, and we can base the locations of 655 * the user varyings on VAR0. Otherwise, we use TEX0 as base index. 656 */ 657 assert(attr >= VARYING_SLOT_TEX0); 658 input_semantic_index[slot] = st->needs_texcoord_semantic ? 659 (attr - VARYING_SLOT_VAR0) : (attr - VARYING_SLOT_TEX0); 660 input_semantic_name[slot] = TGSI_SEMANTIC_GENERIC; 661 if (attr == VARYING_SLOT_PNTC) 662 interpMode[slot] = TGSI_INTERPOLATE_LINEAR; 663 else 664 interpMode[slot] = st_translate_interp(stfp->Base.InterpQualifier[attr], 665 FALSE); 666 break; 667 } 668 } 669 else { 670 inputMapping[attr] = -1; 671 } 672 } 673 674 /* 675 * Semantics and mapping for outputs 676 */ 677 { 678 uint numColors = 0; 679 GLbitfield64 outputsWritten = stfp->Base.Base.OutputsWritten; 680 681 /* if z is written, emit that first */ 682 if (outputsWritten & BITFIELD64_BIT(FRAG_RESULT_DEPTH)) { 683 fs_output_semantic_name[fs_num_outputs] = TGSI_SEMANTIC_POSITION; 684 fs_output_semantic_index[fs_num_outputs] = 0; 685 outputMapping[FRAG_RESULT_DEPTH] = fs_num_outputs; 686 fs_num_outputs++; 687 outputsWritten &= ~(1 << FRAG_RESULT_DEPTH); 688 } 689 690 if (outputsWritten & BITFIELD64_BIT(FRAG_RESULT_STENCIL)) { 691 fs_output_semantic_name[fs_num_outputs] = TGSI_SEMANTIC_STENCIL; 692 fs_output_semantic_index[fs_num_outputs] = 0; 693 outputMapping[FRAG_RESULT_STENCIL] = fs_num_outputs; 694 fs_num_outputs++; 695 outputsWritten &= ~(1 << FRAG_RESULT_STENCIL); 696 } 697 698 if (outputsWritten & BITFIELD64_BIT(FRAG_RESULT_SAMPLE_MASK)) { 699 fs_output_semantic_name[fs_num_outputs] = TGSI_SEMANTIC_SAMPLEMASK; 700 fs_output_semantic_index[fs_num_outputs] = 0; 701 outputMapping[FRAG_RESULT_SAMPLE_MASK] = fs_num_outputs; 702 fs_num_outputs++; 703 outputsWritten &= ~(1 << FRAG_RESULT_SAMPLE_MASK); 704 } 705 706 /* handle remaining outputs (color) */ 707 for (attr = 0; attr < FRAG_RESULT_MAX; attr++) { 708 if (outputsWritten & BITFIELD64_BIT(attr)) { 709 switch (attr) { 710 case FRAG_RESULT_DEPTH: 711 case FRAG_RESULT_STENCIL: 712 case FRAG_RESULT_SAMPLE_MASK: 713 /* handled above */ 714 assert(0); 715 break; 716 case FRAG_RESULT_COLOR: 717 write_all = GL_TRUE; /* fallthrough */ 718 default: 719 assert(attr == FRAG_RESULT_COLOR || 720 (FRAG_RESULT_DATA0 <= attr && attr < FRAG_RESULT_MAX)); 721 fs_output_semantic_name[fs_num_outputs] = TGSI_SEMANTIC_COLOR; 722 fs_output_semantic_index[fs_num_outputs] = numColors; 723 outputMapping[attr] = fs_num_outputs; 724 numColors++; 725 break; 726 } 727 728 fs_num_outputs++; 729 } 730 } 731 } 732 733 ureg = ureg_create( TGSI_PROCESSOR_FRAGMENT ); 734 if (ureg == NULL) { 735 free(variant); 736 return NULL; 737 } 738 739 if (ST_DEBUG & DEBUG_MESA) { 740 _mesa_print_program(&stfp->Base.Base); 741 _mesa_print_program_parameters(st->ctx, &stfp->Base.Base); 742 debug_printf("\n"); 743 } 744 if (write_all == GL_TRUE) 745 ureg_property_fs_color0_writes_all_cbufs(ureg, 1); 746 747 if (stfp->Base.FragDepthLayout != FRAG_DEPTH_LAYOUT_NONE) { 748 switch (stfp->Base.FragDepthLayout) { 749 case FRAG_DEPTH_LAYOUT_ANY: 750 ureg_property_fs_depth_layout(ureg, TGSI_FS_DEPTH_LAYOUT_ANY); 751 break; 752 case FRAG_DEPTH_LAYOUT_GREATER: 753 ureg_property_fs_depth_layout(ureg, TGSI_FS_DEPTH_LAYOUT_GREATER); 754 break; 755 case FRAG_DEPTH_LAYOUT_LESS: 756 ureg_property_fs_depth_layout(ureg, TGSI_FS_DEPTH_LAYOUT_LESS); 757 break; 758 case FRAG_DEPTH_LAYOUT_UNCHANGED: 759 ureg_property_fs_depth_layout(ureg, TGSI_FS_DEPTH_LAYOUT_UNCHANGED); 760 break; 761 default: 762 assert(0); 763 } 764 } 765 766 if (stfp->glsl_to_tgsi) 767 st_translate_program(st->ctx, 768 TGSI_PROCESSOR_FRAGMENT, 769 ureg, 770 stfp->glsl_to_tgsi, 771 &stfp->Base.Base, 772 /* inputs */ 773 fs_num_inputs, 774 inputMapping, 775 input_semantic_name, 776 input_semantic_index, 777 interpMode, 778 interpLocation, 779 /* outputs */ 780 fs_num_outputs, 781 outputMapping, 782 fs_output_semantic_name, 783 fs_output_semantic_index, FALSE, 784 key->clamp_color ); 785 else 786 st_translate_mesa_program(st->ctx, 787 TGSI_PROCESSOR_FRAGMENT, 788 ureg, 789 &stfp->Base.Base, 790 /* inputs */ 791 fs_num_inputs, 792 inputMapping, 793 input_semantic_name, 794 input_semantic_index, 795 interpMode, 796 /* outputs */ 797 fs_num_outputs, 798 outputMapping, 799 fs_output_semantic_name, 800 fs_output_semantic_index, FALSE, 801 key->clamp_color); 802 803 variant->tgsi.tokens = ureg_get_tokens( ureg, NULL ); 804 ureg_destroy( ureg ); 805 806 if (ST_DEBUG & DEBUG_TGSI) { 807 tgsi_dump(variant->tgsi.tokens, 0/*TGSI_DUMP_VERBOSE*/); 808 debug_printf("\n"); 809 } 810 811 /* fill in variant */ 812 variant->driver_shader = pipe->create_fs_state(pipe, &variant->tgsi); 813 variant->key = *key; 814 815 if (deleteFP) { 816 /* Free the temporary program made above */ 817 struct gl_fragment_program *fp = &stfp->Base; 818 _mesa_reference_fragprog(st->ctx, &fp, NULL); 819 } 820 821 return variant; 822} 823 824 825/** 826 * Translate fragment program if needed. 827 */ 828struct st_fp_variant * 829st_get_fp_variant(struct st_context *st, 830 struct st_fragment_program *stfp, 831 const struct st_fp_variant_key *key) 832{ 833 struct st_fp_variant *fpv; 834 835 /* Search for existing variant */ 836 for (fpv = stfp->variants; fpv; fpv = fpv->next) { 837 if (memcmp(&fpv->key, key, sizeof(*key)) == 0) { 838 break; 839 } 840 } 841 842 if (!fpv) { 843 /* create new */ 844 fpv = st_translate_fragment_program(st, stfp, key); 845 if (fpv) { 846 /* insert into list */ 847 fpv->next = stfp->variants; 848 stfp->variants = fpv; 849 } 850 } 851 852 return fpv; 853} 854 855 856/** 857 * Translate a geometry program to create a new variant. 858 */ 859static struct st_gp_variant * 860st_translate_geometry_program(struct st_context *st, 861 struct st_geometry_program *stgp, 862 const struct st_gp_variant_key *key) 863{ 864 GLuint inputMapping[VARYING_SLOT_MAX]; 865 GLuint outputMapping[VARYING_SLOT_MAX]; 866 struct pipe_context *pipe = st->pipe; 867 GLuint attr; 868 GLbitfield64 inputsRead; 869 GLuint vslot = 0; 870 871 uint gs_num_inputs = 0; 872 uint gs_builtin_inputs = 0; 873 uint gs_array_offset = 0; 874 875 ubyte gs_output_semantic_name[PIPE_MAX_SHADER_OUTPUTS]; 876 ubyte gs_output_semantic_index[PIPE_MAX_SHADER_OUTPUTS]; 877 uint gs_num_outputs = 0; 878 879 GLint i; 880 GLuint maxSlot = 0; 881 struct ureg_program *ureg; 882 883 struct st_gp_variant *gpv; 884 885 gpv = CALLOC_STRUCT(st_gp_variant); 886 if (!gpv) 887 return NULL; 888 889 if (!stgp->glsl_to_tgsi) { 890 _mesa_remove_output_reads(&stgp->Base.Base, PROGRAM_OUTPUT); 891 } 892 893 ureg = ureg_create( TGSI_PROCESSOR_GEOMETRY ); 894 if (ureg == NULL) { 895 free(gpv); 896 return NULL; 897 } 898 899 /* which vertex output goes to the first geometry input */ 900 vslot = 0; 901 902 memset(inputMapping, 0, sizeof(inputMapping)); 903 memset(outputMapping, 0, sizeof(outputMapping)); 904 905 /* 906 * Convert Mesa program inputs to TGSI input register semantics. 907 */ 908 inputsRead = stgp->Base.Base.InputsRead; 909 for (attr = 0; attr < VARYING_SLOT_MAX; attr++) { 910 if ((inputsRead & BITFIELD64_BIT(attr)) != 0) { 911 const GLuint slot = gs_num_inputs; 912 913 gs_num_inputs++; 914 915 inputMapping[attr] = slot; 916 917 stgp->input_map[slot + gs_array_offset] = vslot - gs_builtin_inputs; 918 stgp->input_to_index[attr] = vslot; 919 stgp->index_to_input[vslot] = attr; 920 ++vslot; 921 922 if (attr != VARYING_SLOT_PRIMITIVE_ID) { 923 gs_array_offset += 2; 924 } else 925 ++gs_builtin_inputs; 926 927#if 0 928 debug_printf("input map at %d = %d\n", 929 slot + gs_array_offset, stgp->input_map[slot + gs_array_offset]); 930#endif 931 932 switch (attr) { 933 case VARYING_SLOT_PRIMITIVE_ID: 934 stgp->input_semantic_name[slot] = TGSI_SEMANTIC_PRIMID; 935 stgp->input_semantic_index[slot] = 0; 936 break; 937 case VARYING_SLOT_POS: 938 stgp->input_semantic_name[slot] = TGSI_SEMANTIC_POSITION; 939 stgp->input_semantic_index[slot] = 0; 940 break; 941 case VARYING_SLOT_COL0: 942 stgp->input_semantic_name[slot] = TGSI_SEMANTIC_COLOR; 943 stgp->input_semantic_index[slot] = 0; 944 break; 945 case VARYING_SLOT_COL1: 946 stgp->input_semantic_name[slot] = TGSI_SEMANTIC_COLOR; 947 stgp->input_semantic_index[slot] = 1; 948 break; 949 case VARYING_SLOT_FOGC: 950 stgp->input_semantic_name[slot] = TGSI_SEMANTIC_FOG; 951 stgp->input_semantic_index[slot] = 0; 952 break; 953 case VARYING_SLOT_CLIP_VERTEX: 954 stgp->input_semantic_name[slot] = TGSI_SEMANTIC_CLIPVERTEX; 955 stgp->input_semantic_index[slot] = 0; 956 break; 957 case VARYING_SLOT_CLIP_DIST0: 958 stgp->input_semantic_name[slot] = TGSI_SEMANTIC_CLIPDIST; 959 stgp->input_semantic_index[slot] = 0; 960 break; 961 case VARYING_SLOT_CLIP_DIST1: 962 stgp->input_semantic_name[slot] = TGSI_SEMANTIC_CLIPDIST; 963 stgp->input_semantic_index[slot] = 1; 964 break; 965 case VARYING_SLOT_PSIZ: 966 stgp->input_semantic_name[slot] = TGSI_SEMANTIC_PSIZE; 967 stgp->input_semantic_index[slot] = 0; 968 break; 969 case VARYING_SLOT_TEX0: 970 case VARYING_SLOT_TEX1: 971 case VARYING_SLOT_TEX2: 972 case VARYING_SLOT_TEX3: 973 case VARYING_SLOT_TEX4: 974 case VARYING_SLOT_TEX5: 975 case VARYING_SLOT_TEX6: 976 case VARYING_SLOT_TEX7: 977 stgp->input_semantic_name[slot] = st->needs_texcoord_semantic ? 978 TGSI_SEMANTIC_TEXCOORD : TGSI_SEMANTIC_GENERIC; 979 stgp->input_semantic_index[slot] = (attr - VARYING_SLOT_TEX0); 980 break; 981 case VARYING_SLOT_VAR0: 982 default: 983 assert(attr >= VARYING_SLOT_VAR0 && attr < VARYING_SLOT_MAX); 984 stgp->input_semantic_name[slot] = TGSI_SEMANTIC_GENERIC; 985 stgp->input_semantic_index[slot] = st->needs_texcoord_semantic ? 986 (attr - VARYING_SLOT_VAR0) : (attr - VARYING_SLOT_TEX0); 987 break; 988 } 989 } 990 } 991 992 /* initialize output semantics to defaults */ 993 for (i = 0; i < PIPE_MAX_SHADER_OUTPUTS; i++) { 994 gs_output_semantic_name[i] = TGSI_SEMANTIC_GENERIC; 995 gs_output_semantic_index[i] = 0; 996 } 997 998 /* 999 * Determine number of outputs, the (default) output register 1000 * mapping and the semantic information for each output. 1001 */ 1002 for (attr = 0; attr < VARYING_SLOT_MAX; attr++) { 1003 if (stgp->Base.Base.OutputsWritten & BITFIELD64_BIT(attr)) { 1004 GLuint slot; 1005 1006 slot = gs_num_outputs; 1007 gs_num_outputs++; 1008 outputMapping[attr] = slot; 1009 1010 switch (attr) { 1011 case VARYING_SLOT_POS: 1012 assert(slot == 0); 1013 gs_output_semantic_name[slot] = TGSI_SEMANTIC_POSITION; 1014 gs_output_semantic_index[slot] = 0; 1015 break; 1016 case VARYING_SLOT_COL0: 1017 gs_output_semantic_name[slot] = TGSI_SEMANTIC_COLOR; 1018 gs_output_semantic_index[slot] = 0; 1019 break; 1020 case VARYING_SLOT_COL1: 1021 gs_output_semantic_name[slot] = TGSI_SEMANTIC_COLOR; 1022 gs_output_semantic_index[slot] = 1; 1023 break; 1024 case VARYING_SLOT_BFC0: 1025 gs_output_semantic_name[slot] = TGSI_SEMANTIC_BCOLOR; 1026 gs_output_semantic_index[slot] = 0; 1027 break; 1028 case VARYING_SLOT_BFC1: 1029 gs_output_semantic_name[slot] = TGSI_SEMANTIC_BCOLOR; 1030 gs_output_semantic_index[slot] = 1; 1031 break; 1032 case VARYING_SLOT_FOGC: 1033 gs_output_semantic_name[slot] = TGSI_SEMANTIC_FOG; 1034 gs_output_semantic_index[slot] = 0; 1035 break; 1036 case VARYING_SLOT_PSIZ: 1037 gs_output_semantic_name[slot] = TGSI_SEMANTIC_PSIZE; 1038 gs_output_semantic_index[slot] = 0; 1039 break; 1040 case VARYING_SLOT_CLIP_VERTEX: 1041 gs_output_semantic_name[slot] = TGSI_SEMANTIC_CLIPVERTEX; 1042 gs_output_semantic_index[slot] = 0; 1043 break; 1044 case VARYING_SLOT_CLIP_DIST0: 1045 gs_output_semantic_name[slot] = TGSI_SEMANTIC_CLIPDIST; 1046 gs_output_semantic_index[slot] = 0; 1047 break; 1048 case VARYING_SLOT_CLIP_DIST1: 1049 gs_output_semantic_name[slot] = TGSI_SEMANTIC_CLIPDIST; 1050 gs_output_semantic_index[slot] = 1; 1051 break; 1052 case VARYING_SLOT_LAYER: 1053 gs_output_semantic_name[slot] = TGSI_SEMANTIC_LAYER; 1054 gs_output_semantic_index[slot] = 0; 1055 break; 1056 case VARYING_SLOT_PRIMITIVE_ID: 1057 gs_output_semantic_name[slot] = TGSI_SEMANTIC_PRIMID; 1058 gs_output_semantic_index[slot] = 0; 1059 break; 1060 case VARYING_SLOT_VIEWPORT: 1061 gs_output_semantic_name[slot] = TGSI_SEMANTIC_VIEWPORT_INDEX; 1062 gs_output_semantic_index[slot] = 0; 1063 break; 1064 case VARYING_SLOT_TEX0: 1065 case VARYING_SLOT_TEX1: 1066 case VARYING_SLOT_TEX2: 1067 case VARYING_SLOT_TEX3: 1068 case VARYING_SLOT_TEX4: 1069 case VARYING_SLOT_TEX5: 1070 case VARYING_SLOT_TEX6: 1071 case VARYING_SLOT_TEX7: 1072 gs_output_semantic_name[slot] = st->needs_texcoord_semantic ? 1073 TGSI_SEMANTIC_TEXCOORD : TGSI_SEMANTIC_GENERIC; 1074 gs_output_semantic_index[slot] = (attr - VARYING_SLOT_TEX0); 1075 break; 1076 case VARYING_SLOT_VAR0: 1077 default: 1078 assert(slot < Elements(gs_output_semantic_name)); 1079 assert(attr >= VARYING_SLOT_VAR0); 1080 gs_output_semantic_name[slot] = TGSI_SEMANTIC_GENERIC; 1081 gs_output_semantic_index[slot] = st->needs_texcoord_semantic ? 1082 (attr - VARYING_SLOT_VAR0) : (attr - VARYING_SLOT_TEX0); 1083 break; 1084 } 1085 } 1086 } 1087 1088 /* find max output slot referenced to compute gs_num_outputs */ 1089 for (attr = 0; attr < VARYING_SLOT_MAX; attr++) { 1090 if (outputMapping[attr] != ~0 && outputMapping[attr] > maxSlot) 1091 maxSlot = outputMapping[attr]; 1092 } 1093 gs_num_outputs = maxSlot + 1; 1094 1095#if 0 /* debug */ 1096 { 1097 GLuint i; 1098 printf("outputMapping? %d\n", outputMapping ? 1 : 0); 1099 if (outputMapping) { 1100 printf("attr -> slot\n"); 1101 for (i = 0; i < 16; i++) { 1102 printf(" %2d %3d\n", i, outputMapping[i]); 1103 } 1104 } 1105 printf("slot sem_name sem_index\n"); 1106 for (i = 0; i < gs_num_outputs; i++) { 1107 printf(" %2d %d %d\n", 1108 i, 1109 gs_output_semantic_name[i], 1110 gs_output_semantic_index[i]); 1111 } 1112 } 1113#endif 1114 1115 /* free old shader state, if any */ 1116 if (stgp->tgsi.tokens) { 1117 st_free_tokens(stgp->tgsi.tokens); 1118 stgp->tgsi.tokens = NULL; 1119 } 1120 1121 ureg_property_gs_input_prim(ureg, stgp->Base.InputType); 1122 ureg_property_gs_output_prim(ureg, stgp->Base.OutputType); 1123 ureg_property_gs_max_vertices(ureg, stgp->Base.VerticesOut); 1124 ureg_property_gs_invocations(ureg, stgp->Base.Invocations); 1125 1126 if (stgp->glsl_to_tgsi) 1127 st_translate_program(st->ctx, 1128 TGSI_PROCESSOR_GEOMETRY, 1129 ureg, 1130 stgp->glsl_to_tgsi, 1131 &stgp->Base.Base, 1132 /* inputs */ 1133 gs_num_inputs, 1134 inputMapping, 1135 stgp->input_semantic_name, 1136 stgp->input_semantic_index, 1137 NULL, 1138 NULL, 1139 /* outputs */ 1140 gs_num_outputs, 1141 outputMapping, 1142 gs_output_semantic_name, 1143 gs_output_semantic_index, 1144 FALSE, 1145 FALSE); 1146 else 1147 st_translate_mesa_program(st->ctx, 1148 TGSI_PROCESSOR_GEOMETRY, 1149 ureg, 1150 &stgp->Base.Base, 1151 /* inputs */ 1152 gs_num_inputs, 1153 inputMapping, 1154 stgp->input_semantic_name, 1155 stgp->input_semantic_index, 1156 NULL, 1157 /* outputs */ 1158 gs_num_outputs, 1159 outputMapping, 1160 gs_output_semantic_name, 1161 gs_output_semantic_index, 1162 FALSE, 1163 FALSE); 1164 1165 stgp->num_inputs = gs_num_inputs; 1166 stgp->tgsi.tokens = ureg_get_tokens( ureg, NULL ); 1167 ureg_destroy( ureg ); 1168 1169 if (stgp->glsl_to_tgsi) { 1170 st_translate_stream_output_info(stgp->glsl_to_tgsi, 1171 outputMapping, 1172 &stgp->tgsi.stream_output); 1173 } 1174 1175 if ((ST_DEBUG & DEBUG_TGSI) && (ST_DEBUG & DEBUG_MESA)) { 1176 _mesa_print_program(&stgp->Base.Base); 1177 debug_printf("\n"); 1178 } 1179 1180 if (ST_DEBUG & DEBUG_TGSI) { 1181 tgsi_dump(stgp->tgsi.tokens, 0); 1182 debug_printf("\n"); 1183 } 1184 1185 /* fill in new variant */ 1186 gpv->driver_shader = pipe->create_gs_state(pipe, &stgp->tgsi); 1187 gpv->key = *key; 1188 return gpv; 1189} 1190 1191 1192/** 1193 * Get/create geometry program variant. 1194 */ 1195struct st_gp_variant * 1196st_get_gp_variant(struct st_context *st, 1197 struct st_geometry_program *stgp, 1198 const struct st_gp_variant_key *key) 1199{ 1200 struct st_gp_variant *gpv; 1201 1202 /* Search for existing variant */ 1203 for (gpv = stgp->variants; gpv; gpv = gpv->next) { 1204 if (memcmp(&gpv->key, key, sizeof(*key)) == 0) { 1205 break; 1206 } 1207 } 1208 1209 if (!gpv) { 1210 /* create new */ 1211 gpv = st_translate_geometry_program(st, stgp, key); 1212 if (gpv) { 1213 /* insert into list */ 1214 gpv->next = stgp->variants; 1215 stgp->variants = gpv; 1216 } 1217 } 1218 1219 return gpv; 1220} 1221 1222 1223 1224 1225/** 1226 * Debug- print current shader text 1227 */ 1228void 1229st_print_shaders(struct gl_context *ctx) 1230{ 1231 struct gl_shader_program **shProg = ctx->_Shader->CurrentProgram; 1232 unsigned j; 1233 1234 for (j = 0; j < 3; j++) { 1235 unsigned i; 1236 1237 if (shProg[j] == NULL) 1238 continue; 1239 1240 for (i = 0; i < shProg[j]->NumShaders; i++) { 1241 struct gl_shader *sh; 1242 1243 switch (shProg[j]->Shaders[i]->Type) { 1244 case GL_VERTEX_SHADER: 1245 sh = (i != 0) ? NULL : shProg[j]->Shaders[i]; 1246 break; 1247 case GL_GEOMETRY_SHADER_ARB: 1248 sh = (i != 1) ? NULL : shProg[j]->Shaders[i]; 1249 break; 1250 case GL_FRAGMENT_SHADER: 1251 sh = (i != 2) ? NULL : shProg[j]->Shaders[i]; 1252 break; 1253 default: 1254 assert(0); 1255 sh = NULL; 1256 break; 1257 } 1258 1259 if (sh != NULL) { 1260 printf("GLSL shader %u of %u:\n", i, shProg[j]->NumShaders); 1261 printf("%s\n", sh->Source); 1262 } 1263 } 1264 } 1265} 1266 1267 1268/** 1269 * Vert/Geom/Frag programs have per-context variants. Free all the 1270 * variants attached to the given program which match the given context. 1271 */ 1272static void 1273destroy_program_variants(struct st_context *st, struct gl_program *program) 1274{ 1275 if (!program || program == &_mesa_DummyProgram) 1276 return; 1277 1278 switch (program->Target) { 1279 case GL_VERTEX_PROGRAM_ARB: 1280 { 1281 struct st_vertex_program *stvp = (struct st_vertex_program *) program; 1282 struct st_vp_variant *vpv, **prevPtr = &stvp->variants; 1283 1284 for (vpv = stvp->variants; vpv; ) { 1285 struct st_vp_variant *next = vpv->next; 1286 if (vpv->key.st == st) { 1287 /* unlink from list */ 1288 *prevPtr = next; 1289 /* destroy this variant */ 1290 delete_vp_variant(st, vpv); 1291 } 1292 else { 1293 prevPtr = &vpv->next; 1294 } 1295 vpv = next; 1296 } 1297 } 1298 break; 1299 case GL_FRAGMENT_PROGRAM_ARB: 1300 { 1301 struct st_fragment_program *stfp = 1302 (struct st_fragment_program *) program; 1303 struct st_fp_variant *fpv, **prevPtr = &stfp->variants; 1304 1305 for (fpv = stfp->variants; fpv; ) { 1306 struct st_fp_variant *next = fpv->next; 1307 if (fpv->key.st == st) { 1308 /* unlink from list */ 1309 *prevPtr = next; 1310 /* destroy this variant */ 1311 delete_fp_variant(st, fpv); 1312 } 1313 else { 1314 prevPtr = &fpv->next; 1315 } 1316 fpv = next; 1317 } 1318 } 1319 break; 1320 case MESA_GEOMETRY_PROGRAM: 1321 { 1322 struct st_geometry_program *stgp = 1323 (struct st_geometry_program *) program; 1324 struct st_gp_variant *gpv, **prevPtr = &stgp->variants; 1325 1326 for (gpv = stgp->variants; gpv; ) { 1327 struct st_gp_variant *next = gpv->next; 1328 if (gpv->key.st == st) { 1329 /* unlink from list */ 1330 *prevPtr = next; 1331 /* destroy this variant */ 1332 delete_gp_variant(st, gpv); 1333 } 1334 else { 1335 prevPtr = &gpv->next; 1336 } 1337 gpv = next; 1338 } 1339 } 1340 break; 1341 default: 1342 _mesa_problem(NULL, "Unexpected program target 0x%x in " 1343 "destroy_program_variants_cb()", program->Target); 1344 } 1345} 1346 1347 1348/** 1349 * Callback for _mesa_HashWalk. Free all the shader's program variants 1350 * which match the given context. 1351 */ 1352static void 1353destroy_shader_program_variants_cb(GLuint key, void *data, void *userData) 1354{ 1355 struct st_context *st = (struct st_context *) userData; 1356 struct gl_shader *shader = (struct gl_shader *) data; 1357 1358 switch (shader->Type) { 1359 case GL_SHADER_PROGRAM_MESA: 1360 { 1361 struct gl_shader_program *shProg = (struct gl_shader_program *) data; 1362 GLuint i; 1363 1364 for (i = 0; i < shProg->NumShaders; i++) { 1365 destroy_program_variants(st, shProg->Shaders[i]->Program); 1366 } 1367 1368 for (i = 0; i < Elements(shProg->_LinkedShaders); i++) { 1369 if (shProg->_LinkedShaders[i]) 1370 destroy_program_variants(st, shProg->_LinkedShaders[i]->Program); 1371 } 1372 } 1373 break; 1374 case GL_VERTEX_SHADER: 1375 case GL_FRAGMENT_SHADER: 1376 case GL_GEOMETRY_SHADER: 1377 { 1378 destroy_program_variants(st, shader->Program); 1379 } 1380 break; 1381 default: 1382 assert(0); 1383 } 1384} 1385 1386 1387/** 1388 * Callback for _mesa_HashWalk. Free all the program variants which match 1389 * the given context. 1390 */ 1391static void 1392destroy_program_variants_cb(GLuint key, void *data, void *userData) 1393{ 1394 struct st_context *st = (struct st_context *) userData; 1395 struct gl_program *program = (struct gl_program *) data; 1396 destroy_program_variants(st, program); 1397} 1398 1399 1400/** 1401 * Walk over all shaders and programs to delete any variants which 1402 * belong to the given context. 1403 * This is called during context tear-down. 1404 */ 1405void 1406st_destroy_program_variants(struct st_context *st) 1407{ 1408 /* ARB vert/frag program */ 1409 _mesa_HashWalk(st->ctx->Shared->Programs, 1410 destroy_program_variants_cb, st); 1411 1412 /* GLSL vert/frag/geom shaders */ 1413 _mesa_HashWalk(st->ctx->Shared->ShaderObjects, 1414 destroy_shader_program_variants_cb, st); 1415} 1416 1417 1418/** 1419 * For debugging, print/dump the current vertex program. 1420 */ 1421void 1422st_print_current_vertex_program(void) 1423{ 1424 GET_CURRENT_CONTEXT(ctx); 1425 1426 if (ctx->VertexProgram._Current) { 1427 struct st_vertex_program *stvp = 1428 (struct st_vertex_program *) ctx->VertexProgram._Current; 1429 struct st_vp_variant *stv; 1430 1431 debug_printf("Vertex program %u\n", stvp->Base.Base.Id); 1432 1433 for (stv = stvp->variants; stv; stv = stv->next) { 1434 debug_printf("variant %p\n", stv); 1435 tgsi_dump(stv->tgsi.tokens, 0); 1436 } 1437 } 1438} 1439