tgsi_ureg.c revision 8a9a37cebeff19b56afed43ae037d00950fa7594
1/************************************************************************** 2 * 3 * Copyright 2009-2010 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_build.h" 33#include "tgsi/tgsi_info.h" 34#include "tgsi/tgsi_dump.h" 35#include "tgsi/tgsi_sanity.h" 36#include "util/u_debug.h" 37#include "util/u_memory.h" 38#include "util/u_math.h" 39 40union tgsi_any_token { 41 struct tgsi_header header; 42 struct tgsi_processor processor; 43 struct tgsi_token token; 44 struct tgsi_property prop; 45 struct tgsi_property_data prop_data; 46 struct tgsi_declaration decl; 47 struct tgsi_declaration_range decl_range; 48 struct tgsi_declaration_dimension decl_dim; 49 struct tgsi_declaration_semantic decl_semantic; 50 struct tgsi_declaration_resource decl_resource; 51 struct tgsi_immediate imm; 52 union tgsi_immediate_data imm_data; 53 struct tgsi_instruction insn; 54 struct tgsi_instruction_predicate insn_predicate; 55 struct tgsi_instruction_label insn_label; 56 struct tgsi_instruction_texture insn_texture; 57 struct tgsi_texture_offset insn_texture_offset; 58 struct tgsi_src_register src; 59 struct tgsi_dimension dim; 60 struct tgsi_dst_register dst; 61 unsigned value; 62}; 63 64 65struct ureg_tokens { 66 union tgsi_any_token *tokens; 67 unsigned size; 68 unsigned order; 69 unsigned count; 70}; 71 72#define UREG_MAX_INPUT PIPE_MAX_ATTRIBS 73#define UREG_MAX_SYSTEM_VALUE PIPE_MAX_ATTRIBS 74#define UREG_MAX_OUTPUT PIPE_MAX_ATTRIBS 75#define UREG_MAX_CONSTANT_RANGE 32 76#define UREG_MAX_IMMEDIATE 256 77#define UREG_MAX_TEMP 256 78#define UREG_MAX_ADDR 2 79#define UREG_MAX_PRED 1 80 81struct const_decl { 82 struct { 83 unsigned first; 84 unsigned last; 85 } constant_range[UREG_MAX_CONSTANT_RANGE]; 86 unsigned nr_constant_ranges; 87}; 88 89#define DOMAIN_DECL 0 90#define DOMAIN_INSN 1 91 92struct ureg_program 93{ 94 unsigned processor; 95 struct pipe_context *pipe; 96 97 struct { 98 unsigned semantic_name; 99 unsigned semantic_index; 100 unsigned interp; 101 unsigned char cylindrical_wrap; 102 unsigned char centroid; 103 } fs_input[UREG_MAX_INPUT]; 104 unsigned nr_fs_inputs; 105 106 unsigned vs_inputs[UREG_MAX_INPUT/32]; 107 108 struct { 109 unsigned index; 110 unsigned semantic_name; 111 unsigned semantic_index; 112 } gs_input[UREG_MAX_INPUT]; 113 unsigned nr_gs_inputs; 114 115 struct { 116 unsigned index; 117 unsigned semantic_name; 118 unsigned semantic_index; 119 } system_value[UREG_MAX_SYSTEM_VALUE]; 120 unsigned nr_system_values; 121 122 struct { 123 unsigned semantic_name; 124 unsigned semantic_index; 125 } output[UREG_MAX_OUTPUT]; 126 unsigned nr_outputs; 127 128 struct { 129 union { 130 float f[4]; 131 unsigned u[4]; 132 int i[4]; 133 } value; 134 unsigned nr; 135 unsigned type; 136 } immediate[UREG_MAX_IMMEDIATE]; 137 unsigned nr_immediates; 138 139 struct ureg_src sampler[PIPE_MAX_SAMPLERS]; 140 unsigned nr_samplers; 141 142 struct { 143 unsigned index; 144 unsigned target; 145 unsigned return_type_x; 146 unsigned return_type_y; 147 unsigned return_type_z; 148 unsigned return_type_w; 149 } resource[PIPE_MAX_SHADER_RESOURCES]; 150 unsigned nr_resources; 151 152 unsigned temps_active[UREG_MAX_TEMP / 32]; 153 unsigned nr_temps; 154 155 struct const_decl const_decls; 156 struct const_decl const_decls2D[PIPE_MAX_CONSTANT_BUFFERS]; 157 158 unsigned property_gs_input_prim; 159 unsigned property_gs_output_prim; 160 unsigned property_gs_max_vertices; 161 unsigned char property_fs_coord_origin; /* = TGSI_FS_COORD_ORIGIN_* */ 162 unsigned char property_fs_coord_pixel_center; /* = TGSI_FS_COORD_PIXEL_CENTER_* */ 163 unsigned char property_fs_color0_writes_all_cbufs; /* = TGSI_FS_COLOR0_WRITES_ALL_CBUFS * */ 164 unsigned char property_fs_depth_layout; /* TGSI_FS_DEPTH_LAYOUT */ 165 166 unsigned nr_addrs; 167 unsigned nr_preds; 168 unsigned nr_instructions; 169 170 struct ureg_tokens domain[2]; 171}; 172 173static union tgsi_any_token error_tokens[32]; 174 175static void tokens_error( struct ureg_tokens *tokens ) 176{ 177 if (tokens->tokens && tokens->tokens != error_tokens) 178 FREE(tokens->tokens); 179 180 tokens->tokens = error_tokens; 181 tokens->size = Elements(error_tokens); 182 tokens->count = 0; 183} 184 185 186static void tokens_expand( struct ureg_tokens *tokens, 187 unsigned count ) 188{ 189 unsigned old_size = tokens->size * sizeof(unsigned); 190 191 if (tokens->tokens == error_tokens) { 192 return; 193 } 194 195 while (tokens->count + count > tokens->size) { 196 tokens->size = (1 << ++tokens->order); 197 } 198 199 tokens->tokens = REALLOC(tokens->tokens, 200 old_size, 201 tokens->size * sizeof(unsigned)); 202 if (tokens->tokens == NULL) { 203 tokens_error(tokens); 204 } 205} 206 207static void set_bad( struct ureg_program *ureg ) 208{ 209 tokens_error(&ureg->domain[0]); 210} 211 212 213 214static union tgsi_any_token *get_tokens( struct ureg_program *ureg, 215 unsigned domain, 216 unsigned count ) 217{ 218 struct ureg_tokens *tokens = &ureg->domain[domain]; 219 union tgsi_any_token *result; 220 221 if (tokens->count + count > tokens->size) 222 tokens_expand(tokens, count); 223 224 result = &tokens->tokens[tokens->count]; 225 tokens->count += count; 226 return result; 227} 228 229 230static union tgsi_any_token *retrieve_token( struct ureg_program *ureg, 231 unsigned domain, 232 unsigned nr ) 233{ 234 if (ureg->domain[domain].tokens == error_tokens) 235 return &error_tokens[0]; 236 237 return &ureg->domain[domain].tokens[nr]; 238} 239 240 241 242static INLINE struct ureg_dst 243ureg_dst_register( unsigned file, 244 unsigned index ) 245{ 246 struct ureg_dst dst; 247 248 dst.File = file; 249 dst.WriteMask = TGSI_WRITEMASK_XYZW; 250 dst.Indirect = 0; 251 dst.IndirectIndex = 0; 252 dst.IndirectSwizzle = 0; 253 dst.Saturate = 0; 254 dst.Predicate = 0; 255 dst.PredNegate = 0; 256 dst.PredSwizzleX = TGSI_SWIZZLE_X; 257 dst.PredSwizzleY = TGSI_SWIZZLE_Y; 258 dst.PredSwizzleZ = TGSI_SWIZZLE_Z; 259 dst.PredSwizzleW = TGSI_SWIZZLE_W; 260 dst.Index = index; 261 262 return dst; 263} 264 265 266void 267ureg_property_gs_input_prim(struct ureg_program *ureg, 268 unsigned input_prim) 269{ 270 ureg->property_gs_input_prim = input_prim; 271} 272 273void 274ureg_property_gs_output_prim(struct ureg_program *ureg, 275 unsigned output_prim) 276{ 277 ureg->property_gs_output_prim = output_prim; 278} 279 280void 281ureg_property_gs_max_vertices(struct ureg_program *ureg, 282 unsigned max_vertices) 283{ 284 ureg->property_gs_max_vertices = max_vertices; 285} 286 287void 288ureg_property_fs_coord_origin(struct ureg_program *ureg, 289 unsigned fs_coord_origin) 290{ 291 ureg->property_fs_coord_origin = fs_coord_origin; 292} 293 294void 295ureg_property_fs_coord_pixel_center(struct ureg_program *ureg, 296 unsigned fs_coord_pixel_center) 297{ 298 ureg->property_fs_coord_pixel_center = fs_coord_pixel_center; 299} 300 301void 302ureg_property_fs_color0_writes_all_cbufs(struct ureg_program *ureg, 303 unsigned fs_color0_writes_all_cbufs) 304{ 305 ureg->property_fs_color0_writes_all_cbufs = fs_color0_writes_all_cbufs; 306} 307 308void 309ureg_property_fs_depth_layout(struct ureg_program *ureg, 310 unsigned fs_depth_layout) 311{ 312 ureg->property_fs_depth_layout = fs_depth_layout; 313} 314 315struct ureg_src 316ureg_DECL_fs_input_cyl_centroid(struct ureg_program *ureg, 317 unsigned semantic_name, 318 unsigned semantic_index, 319 unsigned interp_mode, 320 unsigned cylindrical_wrap, 321 unsigned centroid) 322{ 323 unsigned i; 324 325 for (i = 0; i < ureg->nr_fs_inputs; i++) { 326 if (ureg->fs_input[i].semantic_name == semantic_name && 327 ureg->fs_input[i].semantic_index == semantic_index) { 328 goto out; 329 } 330 } 331 332 if (ureg->nr_fs_inputs < UREG_MAX_INPUT) { 333 ureg->fs_input[i].semantic_name = semantic_name; 334 ureg->fs_input[i].semantic_index = semantic_index; 335 ureg->fs_input[i].interp = interp_mode; 336 ureg->fs_input[i].cylindrical_wrap = cylindrical_wrap; 337 ureg->fs_input[i].centroid = centroid; 338 ureg->nr_fs_inputs++; 339 } else { 340 set_bad(ureg); 341 } 342 343out: 344 return ureg_src_register(TGSI_FILE_INPUT, i); 345} 346 347 348struct ureg_src 349ureg_DECL_vs_input( struct ureg_program *ureg, 350 unsigned index ) 351{ 352 assert(ureg->processor == TGSI_PROCESSOR_VERTEX); 353 354 ureg->vs_inputs[index/32] |= 1 << (index % 32); 355 return ureg_src_register( TGSI_FILE_INPUT, index ); 356} 357 358 359struct ureg_src 360ureg_DECL_gs_input(struct ureg_program *ureg, 361 unsigned index, 362 unsigned semantic_name, 363 unsigned semantic_index) 364{ 365 if (ureg->nr_gs_inputs < UREG_MAX_INPUT) { 366 ureg->gs_input[ureg->nr_gs_inputs].index = index; 367 ureg->gs_input[ureg->nr_gs_inputs].semantic_name = semantic_name; 368 ureg->gs_input[ureg->nr_gs_inputs].semantic_index = semantic_index; 369 ureg->nr_gs_inputs++; 370 } else { 371 set_bad(ureg); 372 } 373 374 /* XXX: Add suport for true 2D input registers. */ 375 return ureg_src_register(TGSI_FILE_INPUT, index); 376} 377 378 379struct ureg_src 380ureg_DECL_system_value(struct ureg_program *ureg, 381 unsigned index, 382 unsigned semantic_name, 383 unsigned semantic_index) 384{ 385 if (ureg->nr_system_values < UREG_MAX_SYSTEM_VALUE) { 386 ureg->system_value[ureg->nr_system_values].index = index; 387 ureg->system_value[ureg->nr_system_values].semantic_name = semantic_name; 388 ureg->system_value[ureg->nr_system_values].semantic_index = semantic_index; 389 ureg->nr_system_values++; 390 } else { 391 set_bad(ureg); 392 } 393 394 return ureg_src_register(TGSI_FILE_SYSTEM_VALUE, index); 395} 396 397 398struct ureg_dst 399ureg_DECL_output( struct ureg_program *ureg, 400 unsigned name, 401 unsigned index ) 402{ 403 unsigned i; 404 405 for (i = 0; i < ureg->nr_outputs; i++) { 406 if (ureg->output[i].semantic_name == name && 407 ureg->output[i].semantic_index == index) 408 goto out; 409 } 410 411 if (ureg->nr_outputs < UREG_MAX_OUTPUT) { 412 ureg->output[i].semantic_name = name; 413 ureg->output[i].semantic_index = index; 414 ureg->nr_outputs++; 415 } 416 else { 417 set_bad( ureg ); 418 } 419 420out: 421 return ureg_dst_register( TGSI_FILE_OUTPUT, i ); 422} 423 424 425/* Returns a new constant register. Keep track of which have been 426 * referred to so that we can emit decls later. 427 * 428 * Constant operands declared with this function must be addressed 429 * with a two-dimensional index. 430 * 431 * There is nothing in this code to bind this constant to any tracked 432 * value or manage any constant_buffer contents -- that's the 433 * resposibility of the calling code. 434 */ 435void 436ureg_DECL_constant2D(struct ureg_program *ureg, 437 unsigned first, 438 unsigned last, 439 unsigned index2D) 440{ 441 struct const_decl *decl = &ureg->const_decls2D[index2D]; 442 443 assert(index2D < PIPE_MAX_CONSTANT_BUFFERS); 444 445 if (decl->nr_constant_ranges < UREG_MAX_CONSTANT_RANGE) { 446 uint i = decl->nr_constant_ranges++; 447 448 decl->constant_range[i].first = first; 449 decl->constant_range[i].last = last; 450 } 451} 452 453 454/* A one-dimensional, depricated version of ureg_DECL_constant2D(). 455 * 456 * Constant operands declared with this function must be addressed 457 * with a one-dimensional index. 458 */ 459struct ureg_src 460ureg_DECL_constant(struct ureg_program *ureg, 461 unsigned index) 462{ 463 struct const_decl *decl = &ureg->const_decls; 464 unsigned minconst = index, maxconst = index; 465 unsigned i; 466 467 /* Inside existing range? 468 */ 469 for (i = 0; i < decl->nr_constant_ranges; i++) { 470 if (decl->constant_range[i].first <= index && 471 decl->constant_range[i].last >= index) { 472 goto out; 473 } 474 } 475 476 /* Extend existing range? 477 */ 478 for (i = 0; i < decl->nr_constant_ranges; i++) { 479 if (decl->constant_range[i].last == index - 1) { 480 decl->constant_range[i].last = index; 481 goto out; 482 } 483 484 if (decl->constant_range[i].first == index + 1) { 485 decl->constant_range[i].first = index; 486 goto out; 487 } 488 489 minconst = MIN2(minconst, decl->constant_range[i].first); 490 maxconst = MAX2(maxconst, decl->constant_range[i].last); 491 } 492 493 /* Create new range? 494 */ 495 if (decl->nr_constant_ranges < UREG_MAX_CONSTANT_RANGE) { 496 i = decl->nr_constant_ranges++; 497 decl->constant_range[i].first = index; 498 decl->constant_range[i].last = index; 499 goto out; 500 } 501 502 /* Collapse all ranges down to one: 503 */ 504 i = 0; 505 decl->constant_range[0].first = minconst; 506 decl->constant_range[0].last = maxconst; 507 decl->nr_constant_ranges = 1; 508 509out: 510 assert(i < decl->nr_constant_ranges); 511 assert(decl->constant_range[i].first <= index); 512 assert(decl->constant_range[i].last >= index); 513 return ureg_src_register(TGSI_FILE_CONSTANT, index); 514} 515 516 517/* Allocate a new temporary. Temporaries greater than UREG_MAX_TEMP 518 * are legal, but will not be released. 519 */ 520struct ureg_dst ureg_DECL_temporary( struct ureg_program *ureg ) 521{ 522 unsigned i; 523 524 for (i = 0; i < UREG_MAX_TEMP; i += 32) { 525 int bit = ffs(~ureg->temps_active[i/32]); 526 if (bit != 0) { 527 i += bit - 1; 528 goto out; 529 } 530 } 531 532 /* No reusable temps, so allocate a new one: 533 */ 534 i = ureg->nr_temps++; 535 536out: 537 if (i < UREG_MAX_TEMP) 538 ureg->temps_active[i/32] |= 1 << (i % 32); 539 540 if (i >= ureg->nr_temps) 541 ureg->nr_temps = i + 1; 542 543 return ureg_dst_register( TGSI_FILE_TEMPORARY, i ); 544} 545 546 547void ureg_release_temporary( struct ureg_program *ureg, 548 struct ureg_dst tmp ) 549{ 550 if(tmp.File == TGSI_FILE_TEMPORARY) 551 if (tmp.Index < UREG_MAX_TEMP) 552 ureg->temps_active[tmp.Index/32] &= ~(1 << (tmp.Index % 32)); 553} 554 555 556/* Allocate a new address register. 557 */ 558struct ureg_dst ureg_DECL_address( struct ureg_program *ureg ) 559{ 560 if (ureg->nr_addrs < UREG_MAX_ADDR) 561 return ureg_dst_register( TGSI_FILE_ADDRESS, ureg->nr_addrs++ ); 562 563 assert( 0 ); 564 return ureg_dst_register( TGSI_FILE_ADDRESS, 0 ); 565} 566 567/* Allocate a new predicate register. 568 */ 569struct ureg_dst 570ureg_DECL_predicate(struct ureg_program *ureg) 571{ 572 if (ureg->nr_preds < UREG_MAX_PRED) { 573 return ureg_dst_register(TGSI_FILE_PREDICATE, ureg->nr_preds++); 574 } 575 576 assert(0); 577 return ureg_dst_register(TGSI_FILE_PREDICATE, 0); 578} 579 580/* Allocate a new sampler. 581 */ 582struct ureg_src ureg_DECL_sampler( struct ureg_program *ureg, 583 unsigned nr ) 584{ 585 unsigned i; 586 587 for (i = 0; i < ureg->nr_samplers; i++) 588 if (ureg->sampler[i].Index == nr) 589 return ureg->sampler[i]; 590 591 if (i < PIPE_MAX_SAMPLERS) { 592 ureg->sampler[i] = ureg_src_register( TGSI_FILE_SAMPLER, nr ); 593 ureg->nr_samplers++; 594 return ureg->sampler[i]; 595 } 596 597 assert( 0 ); 598 return ureg->sampler[0]; 599} 600 601/* 602 * Allocate a new shader resource. 603 */ 604struct ureg_src 605ureg_DECL_resource(struct ureg_program *ureg, 606 unsigned index, 607 unsigned target, 608 unsigned return_type_x, 609 unsigned return_type_y, 610 unsigned return_type_z, 611 unsigned return_type_w) 612{ 613 struct ureg_src reg = ureg_src_register(TGSI_FILE_RESOURCE, index); 614 uint i; 615 616 for (i = 0; i < ureg->nr_resources; i++) { 617 if (ureg->resource[i].index == index) { 618 return reg; 619 } 620 } 621 622 if (i < PIPE_MAX_SHADER_RESOURCES) { 623 ureg->resource[i].index = index; 624 ureg->resource[i].target = target; 625 ureg->resource[i].return_type_x = return_type_x; 626 ureg->resource[i].return_type_y = return_type_y; 627 ureg->resource[i].return_type_z = return_type_z; 628 ureg->resource[i].return_type_w = return_type_w; 629 ureg->nr_resources++; 630 return reg; 631 } 632 633 assert(0); 634 return reg; 635} 636 637static int 638match_or_expand_immediate( const unsigned *v, 639 unsigned nr, 640 unsigned *v2, 641 unsigned *pnr2, 642 unsigned *swizzle ) 643{ 644 unsigned nr2 = *pnr2; 645 unsigned i, j; 646 647 *swizzle = 0; 648 649 for (i = 0; i < nr; i++) { 650 boolean found = FALSE; 651 652 for (j = 0; j < nr2 && !found; j++) { 653 if (v[i] == v2[j]) { 654 *swizzle |= j << (i * 2); 655 found = TRUE; 656 } 657 } 658 659 if (!found) { 660 if (nr2 >= 4) { 661 return FALSE; 662 } 663 664 v2[nr2] = v[i]; 665 *swizzle |= nr2 << (i * 2); 666 nr2++; 667 } 668 } 669 670 /* Actually expand immediate only when fully succeeded. 671 */ 672 *pnr2 = nr2; 673 return TRUE; 674} 675 676 677static struct ureg_src 678decl_immediate( struct ureg_program *ureg, 679 const unsigned *v, 680 unsigned nr, 681 unsigned type ) 682{ 683 unsigned i, j; 684 unsigned swizzle = 0; 685 686 /* Could do a first pass where we examine all existing immediates 687 * without expanding. 688 */ 689 690 for (i = 0; i < ureg->nr_immediates; i++) { 691 if (ureg->immediate[i].type != type) { 692 continue; 693 } 694 if (match_or_expand_immediate(v, 695 nr, 696 ureg->immediate[i].value.u, 697 &ureg->immediate[i].nr, 698 &swizzle)) { 699 goto out; 700 } 701 } 702 703 if (ureg->nr_immediates < UREG_MAX_IMMEDIATE) { 704 i = ureg->nr_immediates++; 705 ureg->immediate[i].type = type; 706 if (match_or_expand_immediate(v, 707 nr, 708 ureg->immediate[i].value.u, 709 &ureg->immediate[i].nr, 710 &swizzle)) { 711 goto out; 712 } 713 } 714 715 set_bad(ureg); 716 717out: 718 /* Make sure that all referenced elements are from this immediate. 719 * Has the effect of making size-one immediates into scalars. 720 */ 721 for (j = nr; j < 4; j++) { 722 swizzle |= (swizzle & 0x3) << (j * 2); 723 } 724 725 return ureg_swizzle(ureg_src_register(TGSI_FILE_IMMEDIATE, i), 726 (swizzle >> 0) & 0x3, 727 (swizzle >> 2) & 0x3, 728 (swizzle >> 4) & 0x3, 729 (swizzle >> 6) & 0x3); 730} 731 732 733struct ureg_src 734ureg_DECL_immediate( struct ureg_program *ureg, 735 const float *v, 736 unsigned nr ) 737{ 738 union { 739 float f[4]; 740 unsigned u[4]; 741 } fu; 742 unsigned int i; 743 744 for (i = 0; i < nr; i++) { 745 fu.f[i] = v[i]; 746 } 747 748 return decl_immediate(ureg, fu.u, nr, TGSI_IMM_FLOAT32); 749} 750 751 752struct ureg_src 753ureg_DECL_immediate_uint( struct ureg_program *ureg, 754 const unsigned *v, 755 unsigned nr ) 756{ 757 return decl_immediate(ureg, v, nr, TGSI_IMM_UINT32); 758} 759 760 761struct ureg_src 762ureg_DECL_immediate_block_uint( struct ureg_program *ureg, 763 const unsigned *v, 764 unsigned nr ) 765{ 766 uint index; 767 uint i; 768 769 if (ureg->nr_immediates + (nr + 3) / 4 > UREG_MAX_IMMEDIATE) { 770 set_bad(ureg); 771 return ureg_src_register(TGSI_FILE_IMMEDIATE, 0); 772 } 773 774 index = ureg->nr_immediates; 775 ureg->nr_immediates += (nr + 3) / 4; 776 777 for (i = index; i < ureg->nr_immediates; i++) { 778 ureg->immediate[i].type = TGSI_IMM_UINT32; 779 ureg->immediate[i].nr = nr > 4 ? 4 : nr; 780 memcpy(ureg->immediate[i].value.u, 781 &v[(i - index) * 4], 782 ureg->immediate[i].nr * sizeof(uint)); 783 nr -= 4; 784 } 785 786 return ureg_src_register(TGSI_FILE_IMMEDIATE, index); 787} 788 789 790struct ureg_src 791ureg_DECL_immediate_int( struct ureg_program *ureg, 792 const int *v, 793 unsigned nr ) 794{ 795 return decl_immediate(ureg, (const unsigned *)v, nr, TGSI_IMM_INT32); 796} 797 798 799void 800ureg_emit_src( struct ureg_program *ureg, 801 struct ureg_src src ) 802{ 803 unsigned size = 1 + (src.Indirect ? 1 : 0) + 804 (src.Dimension ? (src.DimIndirect ? 2 : 1) : 0); 805 806 union tgsi_any_token *out = get_tokens( ureg, DOMAIN_INSN, size ); 807 unsigned n = 0; 808 809 assert(src.File != TGSI_FILE_NULL); 810 assert(src.File < TGSI_FILE_COUNT); 811 812 out[n].value = 0; 813 out[n].src.File = src.File; 814 out[n].src.SwizzleX = src.SwizzleX; 815 out[n].src.SwizzleY = src.SwizzleY; 816 out[n].src.SwizzleZ = src.SwizzleZ; 817 out[n].src.SwizzleW = src.SwizzleW; 818 out[n].src.Index = src.Index; 819 out[n].src.Negate = src.Negate; 820 out[0].src.Absolute = src.Absolute; 821 n++; 822 823 if (src.Indirect) { 824 out[0].src.Indirect = 1; 825 out[n].value = 0; 826 out[n].src.File = src.IndirectFile; 827 out[n].src.SwizzleX = src.IndirectSwizzle; 828 out[n].src.SwizzleY = src.IndirectSwizzle; 829 out[n].src.SwizzleZ = src.IndirectSwizzle; 830 out[n].src.SwizzleW = src.IndirectSwizzle; 831 out[n].src.Index = src.IndirectIndex; 832 n++; 833 } 834 835 if (src.Dimension) { 836 if (src.DimIndirect) { 837 out[0].src.Dimension = 1; 838 out[n].dim.Indirect = 1; 839 out[n].dim.Dimension = 0; 840 out[n].dim.Padding = 0; 841 out[n].dim.Index = src.DimensionIndex; 842 n++; 843 out[n].value = 0; 844 out[n].src.File = src.DimIndFile; 845 out[n].src.SwizzleX = src.DimIndSwizzle; 846 out[n].src.SwizzleY = src.DimIndSwizzle; 847 out[n].src.SwizzleZ = src.DimIndSwizzle; 848 out[n].src.SwizzleW = src.DimIndSwizzle; 849 out[n].src.Index = src.DimIndIndex; 850 } else { 851 out[0].src.Dimension = 1; 852 out[n].dim.Indirect = 0; 853 out[n].dim.Dimension = 0; 854 out[n].dim.Padding = 0; 855 out[n].dim.Index = src.DimensionIndex; 856 } 857 n++; 858 } 859 860 assert(n == size); 861} 862 863 864void 865ureg_emit_dst( struct ureg_program *ureg, 866 struct ureg_dst dst ) 867{ 868 unsigned size = (1 + 869 (dst.Indirect ? 1 : 0)); 870 871 union tgsi_any_token *out = get_tokens( ureg, DOMAIN_INSN, size ); 872 unsigned n = 0; 873 874 assert(dst.File != TGSI_FILE_NULL); 875 assert(dst.File != TGSI_FILE_CONSTANT); 876 assert(dst.File != TGSI_FILE_INPUT); 877 assert(dst.File != TGSI_FILE_SAMPLER); 878 assert(dst.File != TGSI_FILE_RESOURCE); 879 assert(dst.File != TGSI_FILE_IMMEDIATE); 880 assert(dst.File < TGSI_FILE_COUNT); 881 882 out[n].value = 0; 883 out[n].dst.File = dst.File; 884 out[n].dst.WriteMask = dst.WriteMask; 885 out[n].dst.Indirect = dst.Indirect; 886 out[n].dst.Index = dst.Index; 887 n++; 888 889 if (dst.Indirect) { 890 out[n].value = 0; 891 out[n].src.File = TGSI_FILE_ADDRESS; 892 out[n].src.SwizzleX = dst.IndirectSwizzle; 893 out[n].src.SwizzleY = dst.IndirectSwizzle; 894 out[n].src.SwizzleZ = dst.IndirectSwizzle; 895 out[n].src.SwizzleW = dst.IndirectSwizzle; 896 out[n].src.Index = dst.IndirectIndex; 897 n++; 898 } 899 900 assert(n == size); 901} 902 903 904static void validate( unsigned opcode, 905 unsigned nr_dst, 906 unsigned nr_src ) 907{ 908#ifdef DEBUG 909 const struct tgsi_opcode_info *info = tgsi_get_opcode_info( opcode ); 910 assert(info); 911 if(info) { 912 assert(nr_dst == info->num_dst); 913 assert(nr_src == info->num_src); 914 } 915#endif 916} 917 918struct ureg_emit_insn_result 919ureg_emit_insn(struct ureg_program *ureg, 920 unsigned opcode, 921 boolean saturate, 922 boolean predicate, 923 boolean pred_negate, 924 unsigned pred_swizzle_x, 925 unsigned pred_swizzle_y, 926 unsigned pred_swizzle_z, 927 unsigned pred_swizzle_w, 928 unsigned num_dst, 929 unsigned num_src ) 930{ 931 union tgsi_any_token *out; 932 uint count = predicate ? 2 : 1; 933 struct ureg_emit_insn_result result; 934 935 validate( opcode, num_dst, num_src ); 936 937 out = get_tokens( ureg, DOMAIN_INSN, count ); 938 out[0].insn = tgsi_default_instruction(); 939 out[0].insn.Opcode = opcode; 940 out[0].insn.Saturate = saturate; 941 out[0].insn.NumDstRegs = num_dst; 942 out[0].insn.NumSrcRegs = num_src; 943 944 result.insn_token = ureg->domain[DOMAIN_INSN].count - count; 945 result.extended_token = result.insn_token; 946 947 if (predicate) { 948 out[0].insn.Predicate = 1; 949 out[1].insn_predicate = tgsi_default_instruction_predicate(); 950 out[1].insn_predicate.Negate = pred_negate; 951 out[1].insn_predicate.SwizzleX = pred_swizzle_x; 952 out[1].insn_predicate.SwizzleY = pred_swizzle_y; 953 out[1].insn_predicate.SwizzleZ = pred_swizzle_z; 954 out[1].insn_predicate.SwizzleW = pred_swizzle_w; 955 } 956 957 ureg->nr_instructions++; 958 959 return result; 960} 961 962 963void 964ureg_emit_label(struct ureg_program *ureg, 965 unsigned extended_token, 966 unsigned *label_token ) 967{ 968 union tgsi_any_token *out, *insn; 969 970 if(!label_token) 971 return; 972 973 out = get_tokens( ureg, DOMAIN_INSN, 1 ); 974 out[0].value = 0; 975 976 insn = retrieve_token( ureg, DOMAIN_INSN, extended_token ); 977 insn->insn.Label = 1; 978 979 *label_token = ureg->domain[DOMAIN_INSN].count - 1; 980} 981 982/* Will return a number which can be used in a label to point to the 983 * next instruction to be emitted. 984 */ 985unsigned 986ureg_get_instruction_number( struct ureg_program *ureg ) 987{ 988 return ureg->nr_instructions; 989} 990 991/* Patch a given label (expressed as a token number) to point to a 992 * given instruction (expressed as an instruction number). 993 */ 994void 995ureg_fixup_label(struct ureg_program *ureg, 996 unsigned label_token, 997 unsigned instruction_number ) 998{ 999 union tgsi_any_token *out = retrieve_token( ureg, DOMAIN_INSN, label_token ); 1000 1001 out->insn_label.Label = instruction_number; 1002} 1003 1004 1005void 1006ureg_emit_texture(struct ureg_program *ureg, 1007 unsigned extended_token, 1008 unsigned target, unsigned num_offsets) 1009{ 1010 union tgsi_any_token *out, *insn; 1011 1012 out = get_tokens( ureg, DOMAIN_INSN, 1 ); 1013 insn = retrieve_token( ureg, DOMAIN_INSN, extended_token ); 1014 1015 insn->insn.Texture = 1; 1016 1017 out[0].value = 0; 1018 out[0].insn_texture.Texture = target; 1019 out[0].insn_texture.NumOffsets = num_offsets; 1020} 1021 1022void 1023ureg_emit_texture_offset(struct ureg_program *ureg, 1024 const struct tgsi_texture_offset *offset) 1025{ 1026 union tgsi_any_token *out; 1027 1028 out = get_tokens( ureg, DOMAIN_INSN, 1); 1029 1030 out[0].value = 0; 1031 out[0].insn_texture_offset = *offset; 1032 1033} 1034 1035 1036void 1037ureg_fixup_insn_size(struct ureg_program *ureg, 1038 unsigned insn ) 1039{ 1040 union tgsi_any_token *out = retrieve_token( ureg, DOMAIN_INSN, insn ); 1041 1042 assert(out->insn.Type == TGSI_TOKEN_TYPE_INSTRUCTION); 1043 out->insn.NrTokens = ureg->domain[DOMAIN_INSN].count - insn - 1; 1044} 1045 1046 1047void 1048ureg_insn(struct ureg_program *ureg, 1049 unsigned opcode, 1050 const struct ureg_dst *dst, 1051 unsigned nr_dst, 1052 const struct ureg_src *src, 1053 unsigned nr_src ) 1054{ 1055 struct ureg_emit_insn_result insn; 1056 unsigned i; 1057 boolean saturate; 1058 boolean predicate; 1059 boolean negate = FALSE; 1060 unsigned swizzle[4] = { 0 }; 1061 1062 saturate = nr_dst ? dst[0].Saturate : FALSE; 1063 predicate = nr_dst ? dst[0].Predicate : FALSE; 1064 if (predicate) { 1065 negate = dst[0].PredNegate; 1066 swizzle[0] = dst[0].PredSwizzleX; 1067 swizzle[1] = dst[0].PredSwizzleY; 1068 swizzle[2] = dst[0].PredSwizzleZ; 1069 swizzle[3] = dst[0].PredSwizzleW; 1070 } 1071 1072 insn = ureg_emit_insn(ureg, 1073 opcode, 1074 saturate, 1075 predicate, 1076 negate, 1077 swizzle[0], 1078 swizzle[1], 1079 swizzle[2], 1080 swizzle[3], 1081 nr_dst, 1082 nr_src); 1083 1084 for (i = 0; i < nr_dst; i++) 1085 ureg_emit_dst( ureg, dst[i] ); 1086 1087 for (i = 0; i < nr_src; i++) 1088 ureg_emit_src( ureg, src[i] ); 1089 1090 ureg_fixup_insn_size( ureg, insn.insn_token ); 1091} 1092 1093void 1094ureg_tex_insn(struct ureg_program *ureg, 1095 unsigned opcode, 1096 const struct ureg_dst *dst, 1097 unsigned nr_dst, 1098 unsigned target, 1099 const struct tgsi_texture_offset *texoffsets, 1100 unsigned nr_offset, 1101 const struct ureg_src *src, 1102 unsigned nr_src ) 1103{ 1104 struct ureg_emit_insn_result insn; 1105 unsigned i; 1106 boolean saturate; 1107 boolean predicate; 1108 boolean negate = FALSE; 1109 unsigned swizzle[4] = { 0 }; 1110 1111 saturate = nr_dst ? dst[0].Saturate : FALSE; 1112 predicate = nr_dst ? dst[0].Predicate : FALSE; 1113 if (predicate) { 1114 negate = dst[0].PredNegate; 1115 swizzle[0] = dst[0].PredSwizzleX; 1116 swizzle[1] = dst[0].PredSwizzleY; 1117 swizzle[2] = dst[0].PredSwizzleZ; 1118 swizzle[3] = dst[0].PredSwizzleW; 1119 } 1120 1121 insn = ureg_emit_insn(ureg, 1122 opcode, 1123 saturate, 1124 predicate, 1125 negate, 1126 swizzle[0], 1127 swizzle[1], 1128 swizzle[2], 1129 swizzle[3], 1130 nr_dst, 1131 nr_src); 1132 1133 ureg_emit_texture( ureg, insn.extended_token, target, nr_offset ); 1134 1135 for (i = 0; i < nr_offset; i++) 1136 ureg_emit_texture_offset( ureg, &texoffsets[i]); 1137 1138 for (i = 0; i < nr_dst; i++) 1139 ureg_emit_dst( ureg, dst[i] ); 1140 1141 for (i = 0; i < nr_src; i++) 1142 ureg_emit_src( ureg, src[i] ); 1143 1144 ureg_fixup_insn_size( ureg, insn.insn_token ); 1145} 1146 1147 1148void 1149ureg_label_insn(struct ureg_program *ureg, 1150 unsigned opcode, 1151 const struct ureg_src *src, 1152 unsigned nr_src, 1153 unsigned *label_token ) 1154{ 1155 struct ureg_emit_insn_result insn; 1156 unsigned i; 1157 1158 insn = ureg_emit_insn(ureg, 1159 opcode, 1160 FALSE, 1161 FALSE, 1162 FALSE, 1163 TGSI_SWIZZLE_X, 1164 TGSI_SWIZZLE_Y, 1165 TGSI_SWIZZLE_Z, 1166 TGSI_SWIZZLE_W, 1167 0, 1168 nr_src); 1169 1170 ureg_emit_label( ureg, insn.extended_token, label_token ); 1171 1172 for (i = 0; i < nr_src; i++) 1173 ureg_emit_src( ureg, src[i] ); 1174 1175 ureg_fixup_insn_size( ureg, insn.insn_token ); 1176} 1177 1178 1179static void 1180emit_decl_semantic(struct ureg_program *ureg, 1181 unsigned file, 1182 unsigned index, 1183 unsigned semantic_name, 1184 unsigned semantic_index) 1185{ 1186 union tgsi_any_token *out = get_tokens(ureg, DOMAIN_DECL, 3); 1187 1188 out[0].value = 0; 1189 out[0].decl.Type = TGSI_TOKEN_TYPE_DECLARATION; 1190 out[0].decl.NrTokens = 3; 1191 out[0].decl.File = file; 1192 out[0].decl.UsageMask = TGSI_WRITEMASK_XYZW; /* FIXME! */ 1193 out[0].decl.Semantic = 1; 1194 1195 out[1].value = 0; 1196 out[1].decl_range.First = index; 1197 out[1].decl_range.Last = index; 1198 1199 out[2].value = 0; 1200 out[2].decl_semantic.Name = semantic_name; 1201 out[2].decl_semantic.Index = semantic_index; 1202} 1203 1204 1205static void 1206emit_decl_fs(struct ureg_program *ureg, 1207 unsigned file, 1208 unsigned index, 1209 unsigned semantic_name, 1210 unsigned semantic_index, 1211 unsigned interpolate, 1212 unsigned cylindrical_wrap, 1213 unsigned centroid) 1214{ 1215 union tgsi_any_token *out = get_tokens(ureg, DOMAIN_DECL, 3); 1216 1217 out[0].value = 0; 1218 out[0].decl.Type = TGSI_TOKEN_TYPE_DECLARATION; 1219 out[0].decl.NrTokens = 3; 1220 out[0].decl.File = file; 1221 out[0].decl.UsageMask = TGSI_WRITEMASK_XYZW; /* FIXME! */ 1222 out[0].decl.Interpolate = interpolate; 1223 out[0].decl.Semantic = 1; 1224 out[0].decl.CylindricalWrap = cylindrical_wrap; 1225 out[0].decl.Centroid = centroid; 1226 1227 out[1].value = 0; 1228 out[1].decl_range.First = index; 1229 out[1].decl_range.Last = index; 1230 1231 out[2].value = 0; 1232 out[2].decl_semantic.Name = semantic_name; 1233 out[2].decl_semantic.Index = semantic_index; 1234} 1235 1236 1237static void emit_decl_range( struct ureg_program *ureg, 1238 unsigned file, 1239 unsigned first, 1240 unsigned count ) 1241{ 1242 union tgsi_any_token *out = get_tokens( ureg, DOMAIN_DECL, 2 ); 1243 1244 out[0].value = 0; 1245 out[0].decl.Type = TGSI_TOKEN_TYPE_DECLARATION; 1246 out[0].decl.NrTokens = 2; 1247 out[0].decl.File = file; 1248 out[0].decl.UsageMask = TGSI_WRITEMASK_XYZW; 1249 out[0].decl.Interpolate = TGSI_INTERPOLATE_CONSTANT; 1250 out[0].decl.Semantic = 0; 1251 1252 out[1].value = 0; 1253 out[1].decl_range.First = first; 1254 out[1].decl_range.Last = first + count - 1; 1255} 1256 1257static void 1258emit_decl_range2D(struct ureg_program *ureg, 1259 unsigned file, 1260 unsigned first, 1261 unsigned last, 1262 unsigned index2D) 1263{ 1264 union tgsi_any_token *out = get_tokens(ureg, DOMAIN_DECL, 3); 1265 1266 out[0].value = 0; 1267 out[0].decl.Type = TGSI_TOKEN_TYPE_DECLARATION; 1268 out[0].decl.NrTokens = 3; 1269 out[0].decl.File = file; 1270 out[0].decl.UsageMask = TGSI_WRITEMASK_XYZW; 1271 out[0].decl.Interpolate = TGSI_INTERPOLATE_CONSTANT; 1272 out[0].decl.Dimension = 1; 1273 1274 out[1].value = 0; 1275 out[1].decl_range.First = first; 1276 out[1].decl_range.Last = last; 1277 1278 out[2].value = 0; 1279 out[2].decl_dim.Index2D = index2D; 1280} 1281 1282static void 1283emit_decl_resource(struct ureg_program *ureg, 1284 unsigned index, 1285 unsigned target, 1286 unsigned return_type_x, 1287 unsigned return_type_y, 1288 unsigned return_type_z, 1289 unsigned return_type_w ) 1290{ 1291 union tgsi_any_token *out = get_tokens(ureg, DOMAIN_DECL, 3); 1292 1293 out[0].value = 0; 1294 out[0].decl.Type = TGSI_TOKEN_TYPE_DECLARATION; 1295 out[0].decl.NrTokens = 3; 1296 out[0].decl.File = TGSI_FILE_RESOURCE; 1297 out[0].decl.UsageMask = 0xf; 1298 out[0].decl.Interpolate = TGSI_INTERPOLATE_CONSTANT; 1299 1300 out[1].value = 0; 1301 out[1].decl_range.First = index; 1302 out[1].decl_range.Last = index; 1303 1304 out[2].value = 0; 1305 out[2].decl_resource.Resource = target; 1306 out[2].decl_resource.ReturnTypeX = return_type_x; 1307 out[2].decl_resource.ReturnTypeY = return_type_y; 1308 out[2].decl_resource.ReturnTypeZ = return_type_z; 1309 out[2].decl_resource.ReturnTypeW = return_type_w; 1310} 1311 1312static void 1313emit_immediate( struct ureg_program *ureg, 1314 const unsigned *v, 1315 unsigned type ) 1316{ 1317 union tgsi_any_token *out = get_tokens( ureg, DOMAIN_DECL, 5 ); 1318 1319 out[0].value = 0; 1320 out[0].imm.Type = TGSI_TOKEN_TYPE_IMMEDIATE; 1321 out[0].imm.NrTokens = 5; 1322 out[0].imm.DataType = type; 1323 out[0].imm.Padding = 0; 1324 1325 out[1].imm_data.Uint = v[0]; 1326 out[2].imm_data.Uint = v[1]; 1327 out[3].imm_data.Uint = v[2]; 1328 out[4].imm_data.Uint = v[3]; 1329} 1330 1331static void 1332emit_property(struct ureg_program *ureg, 1333 unsigned name, 1334 unsigned data) 1335{ 1336 union tgsi_any_token *out = get_tokens(ureg, DOMAIN_DECL, 2); 1337 1338 out[0].value = 0; 1339 out[0].prop.Type = TGSI_TOKEN_TYPE_PROPERTY; 1340 out[0].prop.NrTokens = 2; 1341 out[0].prop.PropertyName = name; 1342 1343 out[1].prop_data.Data = data; 1344} 1345 1346 1347static void emit_decls( struct ureg_program *ureg ) 1348{ 1349 unsigned i; 1350 1351 if (ureg->property_gs_input_prim != ~0) { 1352 assert(ureg->processor == TGSI_PROCESSOR_GEOMETRY); 1353 1354 emit_property(ureg, 1355 TGSI_PROPERTY_GS_INPUT_PRIM, 1356 ureg->property_gs_input_prim); 1357 } 1358 1359 if (ureg->property_gs_output_prim != ~0) { 1360 assert(ureg->processor == TGSI_PROCESSOR_GEOMETRY); 1361 1362 emit_property(ureg, 1363 TGSI_PROPERTY_GS_OUTPUT_PRIM, 1364 ureg->property_gs_output_prim); 1365 } 1366 1367 if (ureg->property_gs_max_vertices != ~0) { 1368 assert(ureg->processor == TGSI_PROCESSOR_GEOMETRY); 1369 1370 emit_property(ureg, 1371 TGSI_PROPERTY_GS_MAX_OUTPUT_VERTICES, 1372 ureg->property_gs_max_vertices); 1373 } 1374 1375 if (ureg->property_fs_coord_origin) { 1376 assert(ureg->processor == TGSI_PROCESSOR_FRAGMENT); 1377 1378 emit_property(ureg, 1379 TGSI_PROPERTY_FS_COORD_ORIGIN, 1380 ureg->property_fs_coord_origin); 1381 } 1382 1383 if (ureg->property_fs_coord_pixel_center) { 1384 assert(ureg->processor == TGSI_PROCESSOR_FRAGMENT); 1385 1386 emit_property(ureg, 1387 TGSI_PROPERTY_FS_COORD_PIXEL_CENTER, 1388 ureg->property_fs_coord_pixel_center); 1389 } 1390 1391 if (ureg->property_fs_color0_writes_all_cbufs) { 1392 assert(ureg->processor == TGSI_PROCESSOR_FRAGMENT); 1393 1394 emit_property(ureg, 1395 TGSI_PROPERTY_FS_COLOR0_WRITES_ALL_CBUFS, 1396 ureg->property_fs_color0_writes_all_cbufs); 1397 } 1398 1399 if (ureg->property_fs_depth_layout) { 1400 assert(ureg->processor == TGSI_PROCESSOR_FRAGMENT); 1401 1402 emit_property(ureg, 1403 TGSI_PROPERTY_FS_DEPTH_LAYOUT, 1404 ureg->property_fs_depth_layout); 1405 } 1406 1407 if (ureg->processor == TGSI_PROCESSOR_VERTEX) { 1408 for (i = 0; i < UREG_MAX_INPUT; i++) { 1409 if (ureg->vs_inputs[i/32] & (1 << (i%32))) { 1410 emit_decl_range( ureg, TGSI_FILE_INPUT, i, 1 ); 1411 } 1412 } 1413 } else if (ureg->processor == TGSI_PROCESSOR_FRAGMENT) { 1414 for (i = 0; i < ureg->nr_fs_inputs; i++) { 1415 emit_decl_fs(ureg, 1416 TGSI_FILE_INPUT, 1417 i, 1418 ureg->fs_input[i].semantic_name, 1419 ureg->fs_input[i].semantic_index, 1420 ureg->fs_input[i].interp, 1421 ureg->fs_input[i].cylindrical_wrap, 1422 ureg->fs_input[i].centroid); 1423 } 1424 } else { 1425 for (i = 0; i < ureg->nr_gs_inputs; i++) { 1426 emit_decl_semantic(ureg, 1427 TGSI_FILE_INPUT, 1428 ureg->gs_input[i].index, 1429 ureg->gs_input[i].semantic_name, 1430 ureg->gs_input[i].semantic_index); 1431 } 1432 } 1433 1434 for (i = 0; i < ureg->nr_system_values; i++) { 1435 emit_decl_semantic(ureg, 1436 TGSI_FILE_SYSTEM_VALUE, 1437 ureg->system_value[i].index, 1438 ureg->system_value[i].semantic_name, 1439 ureg->system_value[i].semantic_index); 1440 } 1441 1442 for (i = 0; i < ureg->nr_outputs; i++) { 1443 emit_decl_semantic(ureg, 1444 TGSI_FILE_OUTPUT, 1445 i, 1446 ureg->output[i].semantic_name, 1447 ureg->output[i].semantic_index); 1448 } 1449 1450 for (i = 0; i < ureg->nr_samplers; i++) { 1451 emit_decl_range( ureg, 1452 TGSI_FILE_SAMPLER, 1453 ureg->sampler[i].Index, 1 ); 1454 } 1455 1456 for (i = 0; i < ureg->nr_resources; i++) { 1457 emit_decl_resource(ureg, 1458 ureg->resource[i].index, 1459 ureg->resource[i].target, 1460 ureg->resource[i].return_type_x, 1461 ureg->resource[i].return_type_y, 1462 ureg->resource[i].return_type_z, 1463 ureg->resource[i].return_type_w); 1464 } 1465 1466 if (ureg->const_decls.nr_constant_ranges) { 1467 for (i = 0; i < ureg->const_decls.nr_constant_ranges; i++) { 1468 emit_decl_range(ureg, 1469 TGSI_FILE_CONSTANT, 1470 ureg->const_decls.constant_range[i].first, 1471 ureg->const_decls.constant_range[i].last - ureg->const_decls.constant_range[i].first + 1); 1472 } 1473 } 1474 1475 for (i = 0; i < PIPE_MAX_CONSTANT_BUFFERS; i++) { 1476 struct const_decl *decl = &ureg->const_decls2D[i]; 1477 1478 if (decl->nr_constant_ranges) { 1479 uint j; 1480 1481 for (j = 0; j < decl->nr_constant_ranges; j++) { 1482 emit_decl_range2D(ureg, 1483 TGSI_FILE_CONSTANT, 1484 decl->constant_range[j].first, 1485 decl->constant_range[j].last, 1486 i); 1487 } 1488 } 1489 } 1490 1491 if (ureg->nr_temps) { 1492 emit_decl_range( ureg, 1493 TGSI_FILE_TEMPORARY, 1494 0, ureg->nr_temps ); 1495 } 1496 1497 if (ureg->nr_addrs) { 1498 emit_decl_range( ureg, 1499 TGSI_FILE_ADDRESS, 1500 0, ureg->nr_addrs ); 1501 } 1502 1503 if (ureg->nr_preds) { 1504 emit_decl_range(ureg, 1505 TGSI_FILE_PREDICATE, 1506 0, 1507 ureg->nr_preds); 1508 } 1509 1510 for (i = 0; i < ureg->nr_immediates; i++) { 1511 emit_immediate( ureg, 1512 ureg->immediate[i].value.u, 1513 ureg->immediate[i].type ); 1514 } 1515} 1516 1517/* Append the instruction tokens onto the declarations to build a 1518 * contiguous stream suitable to send to the driver. 1519 */ 1520static void copy_instructions( struct ureg_program *ureg ) 1521{ 1522 unsigned nr_tokens = ureg->domain[DOMAIN_INSN].count; 1523 union tgsi_any_token *out = get_tokens( ureg, 1524 DOMAIN_DECL, 1525 nr_tokens ); 1526 1527 memcpy(out, 1528 ureg->domain[DOMAIN_INSN].tokens, 1529 nr_tokens * sizeof out[0] ); 1530} 1531 1532 1533static void 1534fixup_header_size(struct ureg_program *ureg) 1535{ 1536 union tgsi_any_token *out = retrieve_token( ureg, DOMAIN_DECL, 0 ); 1537 1538 out->header.BodySize = ureg->domain[DOMAIN_DECL].count - 2; 1539} 1540 1541 1542static void 1543emit_header( struct ureg_program *ureg ) 1544{ 1545 union tgsi_any_token *out = get_tokens( ureg, DOMAIN_DECL, 2 ); 1546 1547 out[0].header.HeaderSize = 2; 1548 out[0].header.BodySize = 0; 1549 1550 out[1].processor.Processor = ureg->processor; 1551 out[1].processor.Padding = 0; 1552} 1553 1554 1555const struct tgsi_token *ureg_finalize( struct ureg_program *ureg ) 1556{ 1557 const struct tgsi_token *tokens; 1558 1559 emit_header( ureg ); 1560 emit_decls( ureg ); 1561 copy_instructions( ureg ); 1562 fixup_header_size( ureg ); 1563 1564 if (ureg->domain[0].tokens == error_tokens || 1565 ureg->domain[1].tokens == error_tokens) { 1566 debug_printf("%s: error in generated shader\n", __FUNCTION__); 1567 assert(0); 1568 return NULL; 1569 } 1570 1571 tokens = &ureg->domain[DOMAIN_DECL].tokens[0].token; 1572 1573 if (0) { 1574 debug_printf("%s: emitted shader %d tokens:\n", __FUNCTION__, 1575 ureg->domain[DOMAIN_DECL].count); 1576 tgsi_dump( tokens, 0 ); 1577 } 1578 1579#if DEBUG 1580 if (tokens && !tgsi_sanity_check(tokens)) { 1581 debug_printf("tgsi_ureg.c, sanity check failed on generated tokens:\n"); 1582 tgsi_dump(tokens, 0); 1583 assert(0); 1584 } 1585#endif 1586 1587 1588 return tokens; 1589} 1590 1591 1592void *ureg_create_shader( struct ureg_program *ureg, 1593 struct pipe_context *pipe, 1594 const struct pipe_stream_output_info *so ) 1595{ 1596 struct pipe_shader_state state; 1597 1598 state.tokens = ureg_finalize(ureg); 1599 if(!state.tokens) 1600 return NULL; 1601 1602 if (so) 1603 state.stream_output = *so; 1604 else 1605 memset(&state.stream_output, 0, sizeof(state.stream_output)); 1606 1607 if (ureg->processor == TGSI_PROCESSOR_VERTEX) 1608 return pipe->create_vs_state( pipe, &state ); 1609 else 1610 return pipe->create_fs_state( pipe, &state ); 1611} 1612 1613 1614const struct tgsi_token *ureg_get_tokens( struct ureg_program *ureg, 1615 unsigned *nr_tokens ) 1616{ 1617 const struct tgsi_token *tokens; 1618 1619 ureg_finalize(ureg); 1620 1621 tokens = &ureg->domain[DOMAIN_DECL].tokens[0].token; 1622 1623 if (nr_tokens) 1624 *nr_tokens = ureg->domain[DOMAIN_DECL].size; 1625 1626 ureg->domain[DOMAIN_DECL].tokens = 0; 1627 ureg->domain[DOMAIN_DECL].size = 0; 1628 ureg->domain[DOMAIN_DECL].order = 0; 1629 ureg->domain[DOMAIN_DECL].count = 0; 1630 1631 return tokens; 1632} 1633 1634 1635void ureg_free_tokens( const struct tgsi_token *tokens ) 1636{ 1637 FREE((struct tgsi_token *)tokens); 1638} 1639 1640 1641struct ureg_program *ureg_create( unsigned processor ) 1642{ 1643 struct ureg_program *ureg = CALLOC_STRUCT( ureg_program ); 1644 if (ureg == NULL) 1645 return NULL; 1646 1647 ureg->processor = processor; 1648 ureg->property_gs_input_prim = ~0; 1649 ureg->property_gs_output_prim = ~0; 1650 ureg->property_gs_max_vertices = ~0; 1651 return ureg; 1652} 1653 1654 1655void ureg_destroy( struct ureg_program *ureg ) 1656{ 1657 unsigned i; 1658 1659 for (i = 0; i < Elements(ureg->domain); i++) { 1660 if (ureg->domain[i].tokens && 1661 ureg->domain[i].tokens != error_tokens) 1662 FREE(ureg->domain[i].tokens); 1663 } 1664 1665 FREE(ureg); 1666} 1667