tgsi_ureg.c revision 65079afcde1be96d980b56e86bdd023d8e5a338b
1/************************************************************************** 2 * 3 * Copyright 2009 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, INC 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 29#include "pipe/p_context.h" 30#include "pipe/p_state.h" 31#include "tgsi/tgsi_ureg.h" 32#include "tgsi/tgsi_dump.h" 33#include "util/u_memory.h" 34 35union tgsi_any_token { 36 struct tgsi_version version; 37 struct tgsi_header header; 38 struct tgsi_processor processor; 39 struct tgsi_token token; 40 struct tgsi_declaration decl; 41 struct tgsi_declaration_range decl_range; 42 struct tgsi_declaration_semantic decl_semantic; 43 struct tgsi_immediate imm; 44 union tgsi_immediate_data imm_data; 45 struct tgsi_instruction insn; 46 struct tgsi_instruction_ext_nv insn_ext_nv; 47 struct tgsi_instruction_ext_label insn_ext_label; 48 struct tgsi_instruction_ext_texture insn_ext_texture; 49 struct tgsi_instruction_ext_predicate insn_ext_predicate; 50 struct tgsi_src_register src; 51 struct tgsi_src_register_ext_swz src_ext_swz; 52 struct tgsi_src_register_ext_mod src_ext_mod; 53 struct tgsi_dimension dim; 54 struct tgsi_dst_register dst; 55 struct tgsi_dst_register_ext_concode dst_ext_code; 56 struct tgsi_dst_register_ext_modulate dst_ext_mod; 57 struct tgsi_dst_register_ext_predicate dst_ext_pred; 58 unsigned value; 59}; 60 61 62struct ureg_tokens { 63 union tgsi_any_token *tokens; 64 unsigned size; 65 unsigned order; 66 unsigned count; 67}; 68 69#define UREG_MAX_INPUT PIPE_MAX_ATTRIBS 70#define UREG_MAX_OUTPUT PIPE_MAX_ATTRIBS 71#define UREG_MAX_IMMEDIATE 32 72#define UREG_MAX_TEMP 256 73 74#define DOMAIN_DECL 0 75#define DOMAIN_INSN 1 76 77struct ureg_program 78{ 79 unsigned processor; 80 struct pipe_context *pipe; 81 82 struct { 83 unsigned semantic_name; 84 unsigned semantic_index; 85 unsigned interp; 86 } input[UREG_MAX_INPUT]; 87 unsigned nr_inputs; 88 89 struct { 90 unsigned semantic_name; 91 unsigned semantic_index; 92 } output[UREG_MAX_OUTPUT]; 93 unsigned nr_outputs; 94 95 struct { 96 float v[4]; 97 unsigned nr; 98 } immediate[UREG_MAX_IMMEDIATE]; 99 unsigned nr_immediates; 100 101 unsigned temps_active[UREG_MAX_TEMP / 32]; 102 unsigned nr_temps; 103 104 unsigned nr_constants; 105 unsigned nr_samplers; 106 unsigned nr_instructions; 107 108 struct ureg_tokens domain[2]; 109}; 110 111static union tgsi_any_token error_tokens[32]; 112 113static void tokens_error( struct ureg_tokens *tokens ) 114{ 115 tokens->tokens = error_tokens; 116 tokens->size = Elements(error_tokens); 117 tokens->count = 0; 118} 119 120 121static void tokens_expand( struct ureg_tokens *tokens, 122 unsigned count ) 123{ 124 unsigned old_size = tokens->size * sizeof(unsigned); 125 126 if (tokens->tokens == error_tokens) 127 goto fail; 128 129 while (tokens->count + count > tokens->size) { 130 tokens->size = (1 << ++tokens->order); 131 } 132 133 tokens->tokens = REALLOC(tokens->tokens, 134 old_size, 135 tokens->size * sizeof(unsigned)); 136 if (tokens->tokens == NULL) 137 goto fail; 138 139 return; 140 141fail: 142 tokens_error(tokens); 143} 144 145static void set_bad( struct ureg_program *ureg ) 146{ 147 tokens_error(&ureg->domain[0]); 148} 149 150 151 152static union tgsi_any_token *get_tokens( struct ureg_program *ureg, 153 unsigned domain, 154 unsigned count ) 155{ 156 struct ureg_tokens *tokens = &ureg->domain[domain]; 157 union tgsi_any_token *result; 158 159 if (tokens->count + count > tokens->size) 160 tokens_expand(tokens, count); 161 162 result = &tokens->tokens[tokens->count]; 163 tokens->count += count; 164 return result; 165} 166 167 168static union tgsi_any_token *retrieve_token( struct ureg_program *ureg, 169 unsigned domain, 170 unsigned nr ) 171{ 172 if (ureg->domain[domain].tokens == error_tokens) 173 return &error_tokens[0]; 174 175 return &ureg->domain[domain].tokens[nr]; 176} 177 178 179 180static INLINE struct ureg_dst 181ureg_dst_register( unsigned file, 182 unsigned index ) 183{ 184 struct ureg_dst dst; 185 186 dst.File = file; 187 dst.WriteMask = TGSI_WRITEMASK_XYZW; 188 dst.Indirect = 0; 189 dst.Saturate = 0; 190 dst.Index = index; 191 dst.Pad1 = 0; 192 dst.Pad2 = 0; 193 194 return dst; 195} 196 197static INLINE struct ureg_src 198ureg_src_register( unsigned file, 199 unsigned index ) 200{ 201 struct ureg_src src; 202 203 src.File = file; 204 src.SwizzleX = TGSI_SWIZZLE_X; 205 src.SwizzleY = TGSI_SWIZZLE_Y; 206 src.SwizzleZ = TGSI_SWIZZLE_Z; 207 src.SwizzleW = TGSI_SWIZZLE_W; 208 src.Pad = 0; 209 src.Indirect = 0; 210 src.Absolute = 0; 211 src.Index = index; 212 src.Negate = 0; 213 214 return src; 215} 216 217 218 219 220static struct ureg_src 221ureg_DECL_input( struct ureg_program *ureg, 222 unsigned name, 223 unsigned index, 224 unsigned interp_mode ) 225{ 226 unsigned i; 227 228 for (i = 0; i < ureg->nr_inputs; i++) { 229 if (ureg->input[i].semantic_name == name && 230 ureg->input[i].semantic_index == index) 231 goto out; 232 } 233 234 if (ureg->nr_inputs < UREG_MAX_INPUT) { 235 ureg->input[i].semantic_name = name; 236 ureg->input[i].semantic_index = index; 237 ureg->input[i].interp = interp_mode; 238 ureg->nr_inputs++; 239 } 240 else { 241 set_bad( ureg ); 242 } 243 244out: 245 return ureg_src_register( TGSI_FILE_INPUT, i ); 246} 247 248 249 250struct ureg_src 251ureg_DECL_fs_input( struct ureg_program *ureg, 252 unsigned name, 253 unsigned index, 254 unsigned interp ) 255{ 256 return ureg_DECL_input( ureg, name, index, interp ); 257} 258 259 260struct ureg_src 261ureg_DECL_vs_input( struct ureg_program *ureg, 262 unsigned name, 263 unsigned index ) 264{ 265 return ureg_DECL_input( ureg, name, index, TGSI_INTERPOLATE_CONSTANT ); 266} 267 268 269struct ureg_dst 270ureg_DECL_output( struct ureg_program *ureg, 271 unsigned name, 272 unsigned index ) 273{ 274 unsigned i; 275 276 for (i = 0; i < ureg->nr_outputs; i++) { 277 if (ureg->output[i].semantic_name == name && 278 ureg->output[i].semantic_index == index) 279 goto out; 280 } 281 282 if (ureg->nr_outputs < UREG_MAX_OUTPUT) { 283 ureg->output[i].semantic_name = name; 284 ureg->output[i].semantic_index = index; 285 ureg->nr_outputs++; 286 } 287 else { 288 set_bad( ureg ); 289 } 290 291out: 292 return ureg_dst_register( TGSI_FILE_OUTPUT, i ); 293} 294 295 296/* Returns a new constant register. Keep track of which have been 297 * referred to so that we can emit decls later. 298 * 299 * There is nothing in this code to bind this constant to any tracked 300 * value or manage any constant_buffer contents -- that's the 301 * resposibility of the calling code. 302 */ 303struct ureg_src ureg_DECL_constant(struct ureg_program *ureg ) 304{ 305 return ureg_src_register( TGSI_FILE_TEMPORARY, ureg->nr_constants++ ); 306} 307 308 309/* Allocate a new temporary. Temporaries greater than UREG_MAX_TEMP 310 * are legal, but will not be released. 311 */ 312struct ureg_dst ureg_DECL_temporary( struct ureg_program *ureg ) 313{ 314 unsigned i; 315 316 for (i = 0; i < UREG_MAX_TEMP; i += 32) { 317 int bit = ffs(~ureg->temps_active[i/32]); 318 if (bit != 0) { 319 i += bit - 1; 320 goto out; 321 } 322 } 323 324 /* No reusable temps, so allocate a new one: 325 */ 326 i = ureg->nr_temps++; 327 328out: 329 if (i < UREG_MAX_TEMP) 330 ureg->temps_active[i/32] |= 1 << (i % 32); 331 332 if (i >= ureg->nr_temps) 333 ureg->nr_temps = i + 1; 334 335 return ureg_dst_register( TGSI_FILE_TEMPORARY, i ); 336} 337 338 339void ureg_release_temporary( struct ureg_program *ureg, 340 struct ureg_dst tmp ) 341{ 342 if (tmp.Index < UREG_MAX_TEMP) 343 ureg->temps_active[tmp.Index/32] &= ~(1 << (tmp.Index % 32)); 344} 345 346 347/* Allocate a new sampler. 348 */ 349struct ureg_src ureg_DECL_sampler( struct ureg_program *ureg ) 350{ 351 return ureg_src_register( TGSI_FILE_SAMPLER, ureg->nr_samplers++ ); 352} 353 354 355 356 357static int match_or_expand_immediate( const float *v, 358 unsigned nr, 359 float *v2, 360 unsigned *nr2, 361 unsigned *swizzle ) 362{ 363 unsigned i, j; 364 365 for (i = 0; i < nr; i++) { 366 boolean found = FALSE; 367 368 for (j = 0; j < *nr2 && !found; j++) { 369 if (v[i] == v2[j]) { 370 *swizzle |= j << (i * 2); 371 found = TRUE; 372 } 373 } 374 375 if (!found) { 376 if (*nr2 >= 4) 377 return FALSE; 378 379 v2[*nr2] = v[i]; 380 *swizzle |= *nr2 << (i * 2); 381 (*nr2)++; 382 } 383 } 384 385 return TRUE; 386} 387 388 389 390 391struct ureg_src ureg_DECL_immediate( struct ureg_program *ureg, 392 const float *v, 393 unsigned nr ) 394{ 395 unsigned i; 396 unsigned swizzle = 0; 397 398 /* Could do a first pass where we examine all existing immediates 399 * without expanding. 400 */ 401 402 for (i = 0; i < ureg->nr_immediates; i++) { 403 if (match_or_expand_immediate( v, 404 nr, 405 ureg->immediate[i].v, 406 &ureg->immediate[i].nr, 407 &swizzle )) 408 goto out; 409 } 410 411 if (ureg->nr_immediates < UREG_MAX_IMMEDIATE) { 412 i = ureg->nr_immediates++; 413 if (match_or_expand_immediate( v, 414 nr, 415 ureg->immediate[i].v, 416 &ureg->immediate[i].nr, 417 &swizzle )) 418 goto out; 419 } 420 421 set_bad( ureg ); 422 423out: 424 return ureg_swizzle( ureg_src_register( TGSI_FILE_IMMEDIATE, i ), 425 (swizzle >> 0) & 0x3, 426 (swizzle >> 2) & 0x3, 427 (swizzle >> 4) & 0x3, 428 (swizzle >> 6) & 0x3); 429} 430 431 432void 433ureg_emit_src( struct ureg_program *ureg, 434 struct ureg_src src ) 435{ 436 unsigned size = (1 + 437 (src.Absolute ? 1 : 0) + 438 (src.Indirect ? 1 : 0)); 439 440 union tgsi_any_token *out = get_tokens( ureg, DOMAIN_INSN, size ); 441 unsigned n = 0; 442 443 out[n].value = 0; 444 out[n].src.File = src.File; 445 out[n].src.SwizzleX = src.SwizzleX; 446 out[n].src.SwizzleY = src.SwizzleY; 447 out[n].src.SwizzleZ = src.SwizzleZ; 448 out[n].src.SwizzleW = src.SwizzleW; 449 out[n].src.Indirect = src.Indirect; 450 out[n].src.Index = src.Index; 451 n++; 452 453 if (src.Absolute) { 454 out[n].value = 0; 455 out[n].src_ext_mod.Absolute = 1; 456 n++; 457 } 458 459 if (src.Indirect) { 460 out[n].value = 0; 461 out[n].src.File = TGSI_FILE_ADDRESS; 462 out[n].src.SwizzleX = TGSI_SWIZZLE_X; 463 out[n].src.SwizzleY = TGSI_SWIZZLE_X; 464 out[n].src.SwizzleZ = TGSI_SWIZZLE_X; 465 out[n].src.SwizzleW = TGSI_SWIZZLE_X; 466 out[n].src.Indirect = 0; 467 out[n].src.Index = 0; 468 n++; 469 } 470 471 assert(n == size); 472} 473 474 475void 476ureg_emit_dst( struct ureg_program *ureg, 477 struct ureg_dst dst ) 478{ 479 unsigned size = (1 + 480 (dst.Indirect ? 1 : 0)); 481 482 union tgsi_any_token *out = get_tokens( ureg, DOMAIN_INSN, size ); 483 unsigned n = 0; 484 485 out[n].value = 0; 486 out[n].dst.File = dst.File; 487 out[n].dst.WriteMask = dst.WriteMask; 488 out[n].dst.Indirect = dst.Indirect; 489 out[n].dst.Index = dst.Index; 490 n++; 491 492 if (dst.Indirect) { 493 out[n].value = 0; 494 out[n].src.File = TGSI_FILE_ADDRESS; 495 out[n].src.SwizzleX = TGSI_SWIZZLE_X; 496 out[n].src.SwizzleY = TGSI_SWIZZLE_X; 497 out[n].src.SwizzleZ = TGSI_SWIZZLE_X; 498 out[n].src.SwizzleW = TGSI_SWIZZLE_X; 499 out[n].src.Indirect = 0; 500 out[n].src.Index = 0; 501 n++; 502 } 503 504 assert(n == size); 505} 506 507 508 509unsigned 510ureg_emit_insn(struct ureg_program *ureg, 511 unsigned opcode, 512 boolean saturate, 513 unsigned num_dst, 514 unsigned num_src ) 515{ 516 union tgsi_any_token *out; 517 518 out = get_tokens( ureg, DOMAIN_INSN, 1 ); 519 out[0].value = 0; 520 out[0].insn.Type = TGSI_TOKEN_TYPE_INSTRUCTION; 521 out[0].insn.NrTokens = 0; 522 out[0].insn.Opcode = opcode; 523 out[0].insn.Saturate = saturate; 524 out[0].insn.NrTokens = 0; 525 out[0].insn.NumDstRegs = num_dst; 526 out[0].insn.NumSrcRegs = num_src; 527 out[0].insn.Padding = 0; 528 out[0].insn.Extended = 0; 529 530 ureg->nr_instructions++; 531 532 return ureg->domain[DOMAIN_INSN].count - 1; 533} 534 535 536void 537ureg_emit_label(struct ureg_program *ureg, 538 unsigned insn_token, 539 unsigned *label_token ) 540{ 541 union tgsi_any_token *out, *insn; 542 543 out = get_tokens( ureg, DOMAIN_INSN, 1 ); 544 insn = retrieve_token( ureg, DOMAIN_INSN, insn_token ); 545 546 insn->insn.Extended = 1; 547 548 out[0].value = 0; 549 out[0].insn_ext_label.Type = TGSI_INSTRUCTION_EXT_TYPE_LABEL; 550 551 *label_token = ureg->domain[DOMAIN_INSN].count - 1; 552} 553 554/* Will return a number which can be used in a label to point to the 555 * next instruction to be emitted. 556 */ 557unsigned 558ureg_get_instruction_number( struct ureg_program *ureg ) 559{ 560 return ureg->nr_instructions; 561} 562 563/* Patch a given label (expressed as a token number) to point to a 564 * given instruction (expressed as an instruction number). 565 */ 566void 567ureg_fixup_label(struct ureg_program *ureg, 568 unsigned label_token, 569 unsigned instruction_number ) 570{ 571 union tgsi_any_token *out = retrieve_token( ureg, DOMAIN_INSN, label_token ); 572 573 assert(out->insn_ext_label.Type == TGSI_INSTRUCTION_EXT_TYPE_LABEL); 574 out->insn_ext_label.Label = instruction_number; 575} 576 577 578void 579ureg_emit_texture(struct ureg_program *ureg, 580 unsigned insn_token, 581 unsigned target ) 582{ 583 union tgsi_any_token *out, *insn; 584 585 out = get_tokens( ureg, DOMAIN_INSN, 1 ); 586 insn = retrieve_token( ureg, DOMAIN_INSN, insn_token ); 587 588 insn->insn.Extended = 1; 589 590 out[0].value = 0; 591 out[0].insn_ext_texture.Type = TGSI_INSTRUCTION_EXT_TYPE_TEXTURE; 592 out[0].insn_ext_texture.Texture = target; 593} 594 595 596void 597ureg_fixup_insn_size(struct ureg_program *ureg, 598 unsigned insn ) 599{ 600 union tgsi_any_token *out = retrieve_token( ureg, DOMAIN_INSN, insn ); 601 602 assert(out->insn.Type == TGSI_TOKEN_TYPE_INSTRUCTION); 603 out->insn.NrTokens = ureg->domain[DOMAIN_INSN].count - insn - 1; 604} 605 606 607 608 609 610static void emit_decl( struct ureg_program *ureg, 611 unsigned file, 612 unsigned index, 613 unsigned semantic_name, 614 unsigned semantic_index, 615 unsigned interp ) 616{ 617 union tgsi_any_token *out = get_tokens( ureg, DOMAIN_DECL, 3 ); 618 619 out[0].value = 0; 620 out[0].decl.Type = TGSI_TOKEN_TYPE_DECLARATION; 621 out[0].decl.NrTokens = 3; 622 out[0].decl.File = file; 623 out[0].decl.UsageMask = TGSI_WRITEMASK_XYZW; /* FIXME! */ 624 out[0].decl.Interpolate = interp; 625 out[0].decl.Semantic = 1; 626 627 out[1].value = 0; 628 out[1].decl_range.First = 629 out[1].decl_range.Last = index; 630 631 out[2].value = 0; 632 out[2].decl_semantic.SemanticName = semantic_name; 633 out[2].decl_semantic.SemanticIndex = semantic_index; 634 635} 636 637 638static void emit_decl_range( struct ureg_program *ureg, 639 unsigned file, 640 unsigned first, 641 unsigned count ) 642{ 643 union tgsi_any_token *out = get_tokens( ureg, DOMAIN_DECL, 2 ); 644 645 out[0].value = 0; 646 out[0].decl.Type = TGSI_TOKEN_TYPE_DECLARATION; 647 out[0].decl.NrTokens = 2; 648 out[0].decl.File = file; 649 out[0].decl.UsageMask = 0xf; 650 out[0].decl.Interpolate = TGSI_INTERPOLATE_CONSTANT; 651 out[0].decl.Semantic = 0; 652 653 out[1].value = 0; 654 out[1].decl_range.First = first; 655 out[1].decl_range.Last = first + count - 1; 656} 657 658static void emit_immediate( struct ureg_program *ureg, 659 const float *v ) 660{ 661 union tgsi_any_token *out = get_tokens( ureg, DOMAIN_DECL, 5 ); 662 663 out[0].value = 0; 664 out[0].imm.Type = TGSI_TOKEN_TYPE_IMMEDIATE; 665 out[0].imm.NrTokens = 5; 666 out[0].imm.DataType = TGSI_IMM_FLOAT32; 667 out[0].imm.Padding = 0; 668 out[0].imm.Extended = 0; 669 670 out[1].imm_data.Float = v[0]; 671 out[2].imm_data.Float = v[1]; 672 out[3].imm_data.Float = v[2]; 673 out[4].imm_data.Float = v[3]; 674} 675 676 677 678 679static void emit_decls( struct ureg_program *ureg ) 680{ 681 unsigned i; 682 683 for (i = 0; i < ureg->nr_inputs; i++) { 684 emit_decl( ureg, 685 TGSI_FILE_INPUT, 686 i, 687 ureg->input[i].semantic_name, 688 ureg->input[i].semantic_index, 689 ureg->input[i].interp ); 690 } 691 692 for (i = 0; i < ureg->nr_outputs; i++) { 693 emit_decl( ureg, 694 TGSI_FILE_OUTPUT, 695 i, 696 ureg->output[i].semantic_name, 697 ureg->output[i].semantic_index, 698 TGSI_INTERPOLATE_CONSTANT ); 699 } 700 701 if (ureg->nr_samplers) { 702 emit_decl_range( ureg, 703 TGSI_FILE_SAMPLER, 704 0, ureg->nr_samplers ); 705 } 706 707 if (ureg->nr_constants) { 708 emit_decl_range( ureg, 709 TGSI_FILE_CONSTANT, 710 0, ureg->nr_constants ); 711 } 712 713 if (ureg->nr_temps) { 714 emit_decl_range( ureg, 715 TGSI_FILE_TEMPORARY, 716 0, ureg->nr_temps ); 717 } 718 719 for (i = 0; i < ureg->nr_immediates; i++) { 720 emit_immediate( ureg, 721 ureg->immediate[i].v ); 722 } 723} 724 725/* Append the instruction tokens onto the declarations to build a 726 * contiguous stream suitable to send to the driver. 727 */ 728static void copy_instructions( struct ureg_program *ureg ) 729{ 730 unsigned nr_tokens = ureg->domain[DOMAIN_INSN].count; 731 union tgsi_any_token *out = get_tokens( ureg, 732 DOMAIN_DECL, 733 nr_tokens ); 734 735 memcpy(out, 736 ureg->domain[DOMAIN_INSN].tokens, 737 nr_tokens * sizeof out[0] ); 738} 739 740 741static void 742fixup_header_size(struct ureg_program *ureg ) 743{ 744 union tgsi_any_token *out = retrieve_token( ureg, DOMAIN_DECL, 1 ); 745 746 out->header.BodySize = ureg->domain[DOMAIN_DECL].count - 3; 747} 748 749 750static void 751emit_header( struct ureg_program *ureg ) 752{ 753 union tgsi_any_token *out = get_tokens( ureg, DOMAIN_DECL, 3 ); 754 755 out[0].version.MajorVersion = 1; 756 out[0].version.MinorVersion = 1; 757 out[0].version.Padding = 0; 758 759 out[1].header.HeaderSize = 2; 760 out[1].header.BodySize = 0; 761 762 out[2].processor.Processor = ureg->processor; 763 out[2].processor.Padding = 0; 764} 765 766 767void *ureg_create_shader( struct ureg_program *ureg ) 768{ 769 struct pipe_shader_state state; 770 771 emit_header( ureg ); 772 emit_decls( ureg ); 773 copy_instructions( ureg ); 774 fixup_header_size( ureg ); 775 776 if (ureg->domain[0].tokens == error_tokens || 777 ureg->domain[1].tokens == error_tokens) { 778 debug_printf("%s: error in generated shader\n", __FUNCTION__); 779 assert(0); 780 return NULL; 781 } 782 783 state.tokens = (const struct tgsi_token *)ureg->domain[DOMAIN_DECL].tokens; 784 785 if (0) { 786 debug_printf("%s: emitted shader %d tokens:\n", __FUNCTION__, 787 ureg->domain[DOMAIN_DECL].count); 788 tgsi_dump( state.tokens, 0 ); 789 } 790 791 if (ureg->processor == TGSI_PROCESSOR_VERTEX) 792 return ureg->pipe->create_vs_state( ureg->pipe, &state ); 793 else 794 return ureg->pipe->create_fs_state( ureg->pipe, &state ); 795} 796 797 798 799 800struct ureg_program *ureg_create( struct pipe_context *pipe, 801 unsigned processor ) 802{ 803 struct ureg_program *ureg = CALLOC_STRUCT( ureg_program ); 804 if (ureg == NULL) 805 return NULL; 806 807 ureg->pipe = pipe; 808 ureg->processor = processor; 809 return ureg; 810} 811 812 813void ureg_destroy( struct ureg_program *ureg ) 814{ 815 unsigned i; 816 817 for (i = 0; i < Elements(ureg->domain); i++) { 818 if (ureg->domain[i].tokens && 819 ureg->domain[i].tokens != error_tokens) 820 FREE(ureg->domain[i].tokens); 821 } 822 823 FREE(ureg); 824} 825