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