e38b0be7ab5ad9cf579d90e9d3c36ee33e441e89
[mesa.git] / src / gallium / auxiliary / tgsi / tgsi_build.c
1 /**************************************************************************
2 *
3 * Copyright 2007 Tungsten Graphics, Inc., Cedar Park, Texas.
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 TUNGSTEN GRAPHICS 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 #include "util/u_debug.h"
29 #include "pipe/p_shader_tokens.h"
30 #include "tgsi_build.h"
31 #include "tgsi_parse.h"
32
33
34 /*
35 * header
36 */
37
38 struct tgsi_header
39 tgsi_build_header( void )
40 {
41 struct tgsi_header header;
42
43 header.HeaderSize = 1;
44 header.BodySize = 0;
45
46 return header;
47 }
48
49 static void
50 header_headersize_grow( struct tgsi_header *header )
51 {
52 assert( header->HeaderSize < 0xFF );
53 assert( header->BodySize == 0 );
54
55 header->HeaderSize++;
56 }
57
58 static void
59 header_bodysize_grow( struct tgsi_header *header )
60 {
61 assert( header->BodySize < 0xFFFFFF );
62
63 header->BodySize++;
64 }
65
66 struct tgsi_processor
67 tgsi_default_processor( void )
68 {
69 struct tgsi_processor processor;
70
71 processor.Processor = TGSI_PROCESSOR_FRAGMENT;
72 processor.Padding = 0;
73
74 return processor;
75 }
76
77 struct tgsi_processor
78 tgsi_build_processor(
79 unsigned type,
80 struct tgsi_header *header )
81 {
82 struct tgsi_processor processor;
83
84 processor = tgsi_default_processor();
85 processor.Processor = type;
86
87 header_headersize_grow( header );
88
89 return processor;
90 }
91
92 /*
93 * declaration
94 */
95
96 struct tgsi_declaration
97 tgsi_default_declaration( void )
98 {
99 struct tgsi_declaration declaration;
100
101 declaration.Type = TGSI_TOKEN_TYPE_DECLARATION;
102 declaration.NrTokens = 1;
103 declaration.File = TGSI_FILE_NULL;
104 declaration.UsageMask = TGSI_WRITEMASK_XYZW;
105 declaration.Interpolate = TGSI_INTERPOLATE_CONSTANT;
106 declaration.Dimension = 0;
107 declaration.Semantic = 0;
108 declaration.Centroid = 0;
109 declaration.Invariant = 0;
110 declaration.Padding = 0;
111
112 return declaration;
113 }
114
115 struct tgsi_declaration
116 tgsi_build_declaration(
117 unsigned file,
118 unsigned usage_mask,
119 unsigned interpolate,
120 unsigned dimension,
121 unsigned semantic,
122 unsigned centroid,
123 unsigned invariant,
124 struct tgsi_header *header )
125 {
126 struct tgsi_declaration declaration;
127
128 assert( file < TGSI_FILE_COUNT );
129 assert( interpolate < TGSI_INTERPOLATE_COUNT );
130
131 declaration = tgsi_default_declaration();
132 declaration.File = file;
133 declaration.UsageMask = usage_mask;
134 declaration.Interpolate = interpolate;
135 declaration.Dimension = dimension;
136 declaration.Semantic = semantic;
137 declaration.Centroid = centroid;
138 declaration.Invariant = invariant;
139
140 header_bodysize_grow( header );
141
142 return declaration;
143 }
144
145 static void
146 declaration_grow(
147 struct tgsi_declaration *declaration,
148 struct tgsi_header *header )
149 {
150 assert( declaration->NrTokens < 0xFF );
151
152 declaration->NrTokens++;
153
154 header_bodysize_grow( header );
155 }
156
157 struct tgsi_full_declaration
158 tgsi_default_full_declaration( void )
159 {
160 struct tgsi_full_declaration full_declaration;
161
162 full_declaration.Declaration = tgsi_default_declaration();
163 full_declaration.Range = tgsi_default_declaration_range();
164 full_declaration.Semantic = tgsi_default_declaration_semantic();
165
166 return full_declaration;
167 }
168
169 unsigned
170 tgsi_build_full_declaration(
171 const struct tgsi_full_declaration *full_decl,
172 struct tgsi_token *tokens,
173 struct tgsi_header *header,
174 unsigned maxsize )
175 {
176 unsigned size = 0;
177 struct tgsi_declaration *declaration;
178 struct tgsi_declaration_range *dr;
179
180 if( maxsize <= size )
181 return 0;
182 declaration = (struct tgsi_declaration *) &tokens[size];
183 size++;
184
185 *declaration = tgsi_build_declaration(
186 full_decl->Declaration.File,
187 full_decl->Declaration.UsageMask,
188 full_decl->Declaration.Interpolate,
189 full_decl->Declaration.Dimension,
190 full_decl->Declaration.Semantic,
191 full_decl->Declaration.Centroid,
192 full_decl->Declaration.Invariant,
193 header );
194
195 if (maxsize <= size)
196 return 0;
197 dr = (struct tgsi_declaration_range *) &tokens[size];
198 size++;
199
200 *dr = tgsi_build_declaration_range(
201 full_decl->Range.First,
202 full_decl->Range.Last,
203 declaration,
204 header );
205
206 if (full_decl->Declaration.Dimension) {
207 struct tgsi_declaration_dimension *dd;
208
209 if (maxsize <= size) {
210 return 0;
211 }
212 dd = (struct tgsi_declaration_dimension *)&tokens[size];
213 size++;
214
215 *dd = tgsi_build_declaration_dimension(full_decl->Dim.Index2D,
216 declaration,
217 header);
218 }
219
220 if( full_decl->Declaration.Semantic ) {
221 struct tgsi_declaration_semantic *ds;
222
223 if( maxsize <= size )
224 return 0;
225 ds = (struct tgsi_declaration_semantic *) &tokens[size];
226 size++;
227
228 *ds = tgsi_build_declaration_semantic(
229 full_decl->Semantic.Name,
230 full_decl->Semantic.Index,
231 declaration,
232 header );
233 }
234
235 return size;
236 }
237
238 struct tgsi_declaration_range
239 tgsi_default_declaration_range( void )
240 {
241 struct tgsi_declaration_range dr;
242
243 dr.First = 0;
244 dr.Last = 0;
245
246 return dr;
247 }
248
249 struct tgsi_declaration_range
250 tgsi_build_declaration_range(
251 unsigned first,
252 unsigned last,
253 struct tgsi_declaration *declaration,
254 struct tgsi_header *header )
255 {
256 struct tgsi_declaration_range declaration_range;
257
258 assert( last >= first );
259 assert( last <= 0xFFFF );
260
261 declaration_range = tgsi_default_declaration_range();
262 declaration_range.First = first;
263 declaration_range.Last = last;
264
265 declaration_grow( declaration, header );
266
267 return declaration_range;
268 }
269
270 struct tgsi_declaration_dimension
271 tgsi_default_declaration_dimension(void)
272 {
273 struct tgsi_declaration_dimension dd;
274
275 dd.Index2D = 0;
276 dd.Padding = 0;
277
278 return dd;
279 }
280
281 struct tgsi_declaration_dimension
282 tgsi_build_declaration_dimension(unsigned index_2d,
283 struct tgsi_declaration *declaration,
284 struct tgsi_header *header)
285 {
286 struct tgsi_declaration_dimension dd;
287
288 assert(index_2d <= 0xFFFF);
289
290 dd = tgsi_default_declaration_dimension();
291 dd.Index2D = index_2d;
292
293 declaration_grow(declaration, header);
294
295 return dd;
296 }
297
298 struct tgsi_declaration_semantic
299 tgsi_default_declaration_semantic( void )
300 {
301 struct tgsi_declaration_semantic ds;
302
303 ds.Name = TGSI_SEMANTIC_POSITION;
304 ds.Index = 0;
305 ds.Padding = 0;
306
307 return ds;
308 }
309
310 struct tgsi_declaration_semantic
311 tgsi_build_declaration_semantic(
312 unsigned semantic_name,
313 unsigned semantic_index,
314 struct tgsi_declaration *declaration,
315 struct tgsi_header *header )
316 {
317 struct tgsi_declaration_semantic ds;
318
319 assert( semantic_name <= TGSI_SEMANTIC_COUNT );
320 assert( semantic_index <= 0xFFFF );
321
322 ds = tgsi_default_declaration_semantic();
323 ds.Name = semantic_name;
324 ds.Index = semantic_index;
325
326 declaration_grow( declaration, header );
327
328 return ds;
329 }
330
331 /*
332 * immediate
333 */
334
335 struct tgsi_immediate
336 tgsi_default_immediate( void )
337 {
338 struct tgsi_immediate immediate;
339
340 immediate.Type = TGSI_TOKEN_TYPE_IMMEDIATE;
341 immediate.NrTokens = 1;
342 immediate.DataType = TGSI_IMM_FLOAT32;
343 immediate.Padding = 0;
344
345 return immediate;
346 }
347
348 struct tgsi_immediate
349 tgsi_build_immediate(
350 struct tgsi_header *header )
351 {
352 struct tgsi_immediate immediate;
353
354 immediate = tgsi_default_immediate();
355
356 header_bodysize_grow( header );
357
358 return immediate;
359 }
360
361 struct tgsi_full_immediate
362 tgsi_default_full_immediate( void )
363 {
364 struct tgsi_full_immediate fullimm;
365
366 fullimm.Immediate = tgsi_default_immediate();
367 fullimm.u[0].Float = 0.0f;
368 fullimm.u[1].Float = 0.0f;
369 fullimm.u[2].Float = 0.0f;
370 fullimm.u[3].Float = 0.0f;
371
372 return fullimm;
373 }
374
375 static void
376 immediate_grow(
377 struct tgsi_immediate *immediate,
378 struct tgsi_header *header )
379 {
380 assert( immediate->NrTokens < 0xFF );
381
382 immediate->NrTokens++;
383
384 header_bodysize_grow( header );
385 }
386
387 union tgsi_immediate_data
388 tgsi_build_immediate_float32(
389 float value,
390 struct tgsi_immediate *immediate,
391 struct tgsi_header *header )
392 {
393 union tgsi_immediate_data immediate_data;
394
395 immediate_data.Float = value;
396
397 immediate_grow( immediate, header );
398
399 return immediate_data;
400 }
401
402 unsigned
403 tgsi_build_full_immediate(
404 const struct tgsi_full_immediate *full_imm,
405 struct tgsi_token *tokens,
406 struct tgsi_header *header,
407 unsigned maxsize )
408 {
409 unsigned size = 0, i;
410 struct tgsi_immediate *immediate;
411
412 if( maxsize <= size )
413 return 0;
414 immediate = (struct tgsi_immediate *) &tokens[size];
415 size++;
416
417 *immediate = tgsi_build_immediate( header );
418
419 assert( full_imm->Immediate.NrTokens <= 4 + 1 );
420
421 for( i = 0; i < full_imm->Immediate.NrTokens - 1; i++ ) {
422 union tgsi_immediate_data *data;
423
424 if( maxsize <= size )
425 return 0;
426 data = (union tgsi_immediate_data *) &tokens[size];
427 size++;
428
429 *data = tgsi_build_immediate_float32(
430 full_imm->u[i].Float,
431 immediate,
432 header );
433 }
434
435 return size;
436 }
437
438 /*
439 * instruction
440 */
441
442 struct tgsi_instruction
443 tgsi_default_instruction( void )
444 {
445 struct tgsi_instruction instruction;
446
447 instruction.Type = TGSI_TOKEN_TYPE_INSTRUCTION;
448 instruction.NrTokens = 0;
449 instruction.Opcode = TGSI_OPCODE_MOV;
450 instruction.Saturate = TGSI_SAT_NONE;
451 instruction.Predicate = 0;
452 instruction.NumDstRegs = 1;
453 instruction.NumSrcRegs = 1;
454 instruction.Label = 0;
455 instruction.Texture = 0;
456 instruction.Padding = 0;
457
458 return instruction;
459 }
460
461 struct tgsi_instruction
462 tgsi_build_instruction(unsigned opcode,
463 unsigned saturate,
464 unsigned predicate,
465 unsigned num_dst_regs,
466 unsigned num_src_regs,
467 struct tgsi_header *header)
468 {
469 struct tgsi_instruction instruction;
470
471 assert (opcode <= TGSI_OPCODE_LAST);
472 assert (saturate <= TGSI_SAT_MINUS_PLUS_ONE);
473 assert (num_dst_regs <= 3);
474 assert (num_src_regs <= 15);
475
476 instruction = tgsi_default_instruction();
477 instruction.Opcode = opcode;
478 instruction.Saturate = saturate;
479 instruction.Predicate = predicate;
480 instruction.NumDstRegs = num_dst_regs;
481 instruction.NumSrcRegs = num_src_regs;
482
483 header_bodysize_grow( header );
484
485 return instruction;
486 }
487
488 static void
489 instruction_grow(
490 struct tgsi_instruction *instruction,
491 struct tgsi_header *header )
492 {
493 assert (instruction->NrTokens < 0xFF);
494
495 instruction->NrTokens++;
496
497 header_bodysize_grow( header );
498 }
499
500 struct tgsi_full_instruction
501 tgsi_default_full_instruction( void )
502 {
503 struct tgsi_full_instruction full_instruction;
504 unsigned i;
505
506 full_instruction.Instruction = tgsi_default_instruction();
507 full_instruction.Predicate = tgsi_default_instruction_predicate();
508 full_instruction.Label = tgsi_default_instruction_label();
509 full_instruction.Texture = tgsi_default_instruction_texture();
510 for( i = 0; i < TGSI_FULL_MAX_DST_REGISTERS; i++ ) {
511 full_instruction.Dst[i] = tgsi_default_full_dst_register();
512 }
513 for( i = 0; i < TGSI_FULL_MAX_SRC_REGISTERS; i++ ) {
514 full_instruction.Src[i] = tgsi_default_full_src_register();
515 }
516
517 return full_instruction;
518 }
519
520 unsigned
521 tgsi_build_full_instruction(
522 const struct tgsi_full_instruction *full_inst,
523 struct tgsi_token *tokens,
524 struct tgsi_header *header,
525 unsigned maxsize )
526 {
527 unsigned size = 0;
528 unsigned i;
529 struct tgsi_instruction *instruction;
530 struct tgsi_token *prev_token;
531
532 if( maxsize <= size )
533 return 0;
534 instruction = (struct tgsi_instruction *) &tokens[size];
535 size++;
536
537 *instruction = tgsi_build_instruction(full_inst->Instruction.Opcode,
538 full_inst->Instruction.Saturate,
539 full_inst->Instruction.Predicate,
540 full_inst->Instruction.NumDstRegs,
541 full_inst->Instruction.NumSrcRegs,
542 header);
543 prev_token = (struct tgsi_token *) instruction;
544
545 if (full_inst->Instruction.Predicate) {
546 struct tgsi_instruction_predicate *instruction_predicate;
547
548 if (maxsize <= size) {
549 return 0;
550 }
551 instruction_predicate = (struct tgsi_instruction_predicate *)&tokens[size];
552 size++;
553
554 *instruction_predicate =
555 tgsi_build_instruction_predicate(full_inst->Predicate.Index,
556 full_inst->Predicate.Negate,
557 full_inst->Predicate.SwizzleX,
558 full_inst->Predicate.SwizzleY,
559 full_inst->Predicate.SwizzleZ,
560 full_inst->Predicate.SwizzleW,
561 instruction,
562 header);
563 }
564
565 if (full_inst->Instruction.Label) {
566 struct tgsi_instruction_label *instruction_label;
567
568 if( maxsize <= size )
569 return 0;
570 instruction_label =
571 (struct tgsi_instruction_label *) &tokens[size];
572 size++;
573
574 *instruction_label = tgsi_build_instruction_label(
575 full_inst->Label.Label,
576 prev_token,
577 instruction,
578 header );
579 prev_token = (struct tgsi_token *) instruction_label;
580 }
581
582 if (full_inst->Instruction.Texture) {
583 struct tgsi_instruction_texture *instruction_texture;
584
585 if( maxsize <= size )
586 return 0;
587 instruction_texture =
588 (struct tgsi_instruction_texture *) &tokens[size];
589 size++;
590
591 *instruction_texture = tgsi_build_instruction_texture(
592 full_inst->Texture.Texture,
593 prev_token,
594 instruction,
595 header );
596 prev_token = (struct tgsi_token *) instruction_texture;
597 }
598
599 for( i = 0; i < full_inst->Instruction.NumDstRegs; i++ ) {
600 const struct tgsi_full_dst_register *reg = &full_inst->Dst[i];
601 struct tgsi_dst_register *dst_register;
602 struct tgsi_token *prev_token;
603
604 if( maxsize <= size )
605 return 0;
606 dst_register = (struct tgsi_dst_register *) &tokens[size];
607 size++;
608
609 *dst_register = tgsi_build_dst_register(
610 reg->Register.File,
611 reg->Register.WriteMask,
612 reg->Register.Indirect,
613 reg->Register.Index,
614 instruction,
615 header );
616 prev_token = (struct tgsi_token *) dst_register;
617
618 if( reg->Register.Indirect ) {
619 struct tgsi_src_register *ind;
620
621 if( maxsize <= size )
622 return 0;
623 ind = (struct tgsi_src_register *) &tokens[size];
624 size++;
625
626 *ind = tgsi_build_src_register(
627 reg->Indirect.File,
628 reg->Indirect.SwizzleX,
629 reg->Indirect.SwizzleY,
630 reg->Indirect.SwizzleZ,
631 reg->Indirect.SwizzleW,
632 reg->Indirect.Negate,
633 reg->Indirect.Absolute,
634 reg->Indirect.Indirect,
635 reg->Indirect.Dimension,
636 reg->Indirect.Index,
637 instruction,
638 header );
639 }
640 }
641
642 for( i = 0; i < full_inst->Instruction.NumSrcRegs; i++ ) {
643 const struct tgsi_full_src_register *reg = &full_inst->Src[i];
644 struct tgsi_src_register *src_register;
645 struct tgsi_token *prev_token;
646
647 if( maxsize <= size )
648 return 0;
649 src_register = (struct tgsi_src_register *) &tokens[size];
650 size++;
651
652 *src_register = tgsi_build_src_register(
653 reg->Register.File,
654 reg->Register.SwizzleX,
655 reg->Register.SwizzleY,
656 reg->Register.SwizzleZ,
657 reg->Register.SwizzleW,
658 reg->Register.Negate,
659 reg->Register.Absolute,
660 reg->Register.Indirect,
661 reg->Register.Dimension,
662 reg->Register.Index,
663 instruction,
664 header );
665 prev_token = (struct tgsi_token *) src_register;
666
667 if( reg->Register.Indirect ) {
668 struct tgsi_src_register *ind;
669
670 if( maxsize <= size )
671 return 0;
672 ind = (struct tgsi_src_register *) &tokens[size];
673 size++;
674
675 *ind = tgsi_build_src_register(
676 reg->Indirect.File,
677 reg->Indirect.SwizzleX,
678 reg->Indirect.SwizzleY,
679 reg->Indirect.SwizzleZ,
680 reg->Indirect.SwizzleW,
681 reg->Indirect.Negate,
682 reg->Indirect.Absolute,
683 reg->Indirect.Indirect,
684 reg->Indirect.Dimension,
685 reg->Indirect.Index,
686 instruction,
687 header );
688 }
689
690 if( reg->Register.Dimension ) {
691 struct tgsi_dimension *dim;
692
693 assert( !reg->Dimension.Dimension );
694
695 if( maxsize <= size )
696 return 0;
697 dim = (struct tgsi_dimension *) &tokens[size];
698 size++;
699
700 *dim = tgsi_build_dimension(
701 reg->Dimension.Indirect,
702 reg->Dimension.Index,
703 instruction,
704 header );
705
706 if( reg->Dimension.Indirect ) {
707 struct tgsi_src_register *ind;
708
709 if( maxsize <= size )
710 return 0;
711 ind = (struct tgsi_src_register *) &tokens[size];
712 size++;
713
714 *ind = tgsi_build_src_register(
715 reg->DimIndirect.File,
716 reg->DimIndirect.SwizzleX,
717 reg->DimIndirect.SwizzleY,
718 reg->DimIndirect.SwizzleZ,
719 reg->DimIndirect.SwizzleW,
720 reg->DimIndirect.Negate,
721 reg->DimIndirect.Absolute,
722 reg->DimIndirect.Indirect,
723 reg->DimIndirect.Dimension,
724 reg->DimIndirect.Index,
725 instruction,
726 header );
727 }
728 }
729 }
730
731 return size;
732 }
733
734 struct tgsi_instruction_predicate
735 tgsi_default_instruction_predicate(void)
736 {
737 struct tgsi_instruction_predicate instruction_predicate;
738
739 instruction_predicate.SwizzleX = TGSI_SWIZZLE_X;
740 instruction_predicate.SwizzleY = TGSI_SWIZZLE_Y;
741 instruction_predicate.SwizzleZ = TGSI_SWIZZLE_Z;
742 instruction_predicate.SwizzleW = TGSI_SWIZZLE_W;
743 instruction_predicate.Negate = 0;
744 instruction_predicate.Index = 0;
745 instruction_predicate.Padding = 0;
746
747 return instruction_predicate;
748 }
749
750 struct tgsi_instruction_predicate
751 tgsi_build_instruction_predicate(int index,
752 unsigned negate,
753 unsigned swizzleX,
754 unsigned swizzleY,
755 unsigned swizzleZ,
756 unsigned swizzleW,
757 struct tgsi_instruction *instruction,
758 struct tgsi_header *header)
759 {
760 struct tgsi_instruction_predicate instruction_predicate;
761
762 instruction_predicate = tgsi_default_instruction_predicate();
763 instruction_predicate.SwizzleX = swizzleX;
764 instruction_predicate.SwizzleY = swizzleY;
765 instruction_predicate.SwizzleZ = swizzleZ;
766 instruction_predicate.SwizzleW = swizzleW;
767 instruction_predicate.Negate = negate;
768 instruction_predicate.Index = index;
769
770 instruction_grow(instruction, header);
771
772 return instruction_predicate;
773 }
774
775 struct tgsi_instruction_label
776 tgsi_default_instruction_label( void )
777 {
778 struct tgsi_instruction_label instruction_label;
779
780 instruction_label.Label = 0;
781 instruction_label.Padding = 0;
782
783 return instruction_label;
784 }
785
786 struct tgsi_instruction_label
787 tgsi_build_instruction_label(
788 unsigned label,
789 struct tgsi_token *prev_token,
790 struct tgsi_instruction *instruction,
791 struct tgsi_header *header )
792 {
793 struct tgsi_instruction_label instruction_label;
794
795 instruction_label = tgsi_default_instruction_label();
796 instruction_label.Label = label;
797 instruction->Label = 1;
798
799 instruction_grow( instruction, header );
800
801 return instruction_label;
802 }
803
804 struct tgsi_instruction_texture
805 tgsi_default_instruction_texture( void )
806 {
807 struct tgsi_instruction_texture instruction_texture;
808
809 instruction_texture.Texture = TGSI_TEXTURE_UNKNOWN;
810 instruction_texture.Padding = 0;
811
812 return instruction_texture;
813 }
814
815 struct tgsi_instruction_texture
816 tgsi_build_instruction_texture(
817 unsigned texture,
818 struct tgsi_token *prev_token,
819 struct tgsi_instruction *instruction,
820 struct tgsi_header *header )
821 {
822 struct tgsi_instruction_texture instruction_texture;
823
824 instruction_texture = tgsi_default_instruction_texture();
825 instruction_texture.Texture = texture;
826 instruction->Texture = 1;
827
828 instruction_grow( instruction, header );
829
830 return instruction_texture;
831 }
832
833 struct tgsi_src_register
834 tgsi_default_src_register( void )
835 {
836 struct tgsi_src_register src_register;
837
838 src_register.File = TGSI_FILE_NULL;
839 src_register.SwizzleX = TGSI_SWIZZLE_X;
840 src_register.SwizzleY = TGSI_SWIZZLE_Y;
841 src_register.SwizzleZ = TGSI_SWIZZLE_Z;
842 src_register.SwizzleW = TGSI_SWIZZLE_W;
843 src_register.Negate = 0;
844 src_register.Absolute = 0;
845 src_register.Indirect = 0;
846 src_register.Dimension = 0;
847 src_register.Index = 0;
848
849 return src_register;
850 }
851
852 struct tgsi_src_register
853 tgsi_build_src_register(
854 unsigned file,
855 unsigned swizzle_x,
856 unsigned swizzle_y,
857 unsigned swizzle_z,
858 unsigned swizzle_w,
859 unsigned negate,
860 unsigned absolute,
861 unsigned indirect,
862 unsigned dimension,
863 int index,
864 struct tgsi_instruction *instruction,
865 struct tgsi_header *header )
866 {
867 struct tgsi_src_register src_register;
868
869 assert( file < TGSI_FILE_COUNT );
870 assert( swizzle_x <= TGSI_SWIZZLE_W );
871 assert( swizzle_y <= TGSI_SWIZZLE_W );
872 assert( swizzle_z <= TGSI_SWIZZLE_W );
873 assert( swizzle_w <= TGSI_SWIZZLE_W );
874 assert( negate <= 1 );
875 assert( index >= -0x8000 && index <= 0x7FFF );
876
877 src_register = tgsi_default_src_register();
878 src_register.File = file;
879 src_register.SwizzleX = swizzle_x;
880 src_register.SwizzleY = swizzle_y;
881 src_register.SwizzleZ = swizzle_z;
882 src_register.SwizzleW = swizzle_w;
883 src_register.Negate = negate;
884 src_register.Absolute = absolute;
885 src_register.Indirect = indirect;
886 src_register.Dimension = dimension;
887 src_register.Index = index;
888
889 instruction_grow( instruction, header );
890
891 return src_register;
892 }
893
894 struct tgsi_full_src_register
895 tgsi_default_full_src_register( void )
896 {
897 struct tgsi_full_src_register full_src_register;
898
899 full_src_register.Register = tgsi_default_src_register();
900 full_src_register.Indirect = tgsi_default_src_register();
901 full_src_register.Dimension = tgsi_default_dimension();
902 full_src_register.DimIndirect = tgsi_default_src_register();
903
904 return full_src_register;
905 }
906
907
908 struct tgsi_dimension
909 tgsi_default_dimension( void )
910 {
911 struct tgsi_dimension dimension;
912
913 dimension.Indirect = 0;
914 dimension.Dimension = 0;
915 dimension.Padding = 0;
916 dimension.Index = 0;
917
918 return dimension;
919 }
920
921 struct tgsi_dimension
922 tgsi_build_dimension(
923 unsigned indirect,
924 unsigned index,
925 struct tgsi_instruction *instruction,
926 struct tgsi_header *header )
927 {
928 struct tgsi_dimension dimension;
929
930 dimension = tgsi_default_dimension();
931 dimension.Indirect = indirect;
932 dimension.Index = index;
933
934 instruction_grow( instruction, header );
935
936 return dimension;
937 }
938
939 struct tgsi_dst_register
940 tgsi_default_dst_register( void )
941 {
942 struct tgsi_dst_register dst_register;
943
944 dst_register.File = TGSI_FILE_NULL;
945 dst_register.WriteMask = TGSI_WRITEMASK_XYZW;
946 dst_register.Indirect = 0;
947 dst_register.Dimension = 0;
948 dst_register.Index = 0;
949 dst_register.Padding = 0;
950
951 return dst_register;
952 }
953
954 struct tgsi_dst_register
955 tgsi_build_dst_register(
956 unsigned file,
957 unsigned mask,
958 unsigned indirect,
959 int index,
960 struct tgsi_instruction *instruction,
961 struct tgsi_header *header )
962 {
963 struct tgsi_dst_register dst_register;
964
965 assert( file < TGSI_FILE_COUNT );
966 assert( mask <= TGSI_WRITEMASK_XYZW );
967 assert( index >= -32768 && index <= 32767 );
968
969 dst_register = tgsi_default_dst_register();
970 dst_register.File = file;
971 dst_register.WriteMask = mask;
972 dst_register.Index = index;
973 dst_register.Indirect = indirect;
974
975 instruction_grow( instruction, header );
976
977 return dst_register;
978 }
979
980 struct tgsi_full_dst_register
981 tgsi_default_full_dst_register( void )
982 {
983 struct tgsi_full_dst_register full_dst_register;
984
985 full_dst_register.Register = tgsi_default_dst_register();
986 full_dst_register.Indirect = tgsi_default_src_register();
987
988 return full_dst_register;
989 }
990
991 struct tgsi_property
992 tgsi_default_property( void )
993 {
994 struct tgsi_property property;
995
996 property.Type = TGSI_TOKEN_TYPE_PROPERTY;
997 property.NrTokens = 1;
998 property.PropertyName = TGSI_PROPERTY_GS_INPUT_PRIM;
999 property.Padding = 0;
1000
1001 return property;
1002 }
1003
1004 struct tgsi_property
1005 tgsi_build_property(unsigned property_name,
1006 struct tgsi_header *header)
1007 {
1008 struct tgsi_property property;
1009
1010 property = tgsi_default_property();
1011 property.PropertyName = property_name;
1012
1013 header_bodysize_grow( header );
1014
1015 return property;
1016 }
1017
1018
1019 struct tgsi_full_property
1020 tgsi_default_full_property( void )
1021 {
1022 struct tgsi_full_property full_property;
1023
1024 full_property.Property = tgsi_default_property();
1025 memset(full_property.u, 0,
1026 sizeof(struct tgsi_property_data) * 8);
1027
1028 return full_property;
1029 }
1030
1031 static void
1032 property_grow(
1033 struct tgsi_property *property,
1034 struct tgsi_header *header )
1035 {
1036 assert( property->NrTokens < 0xFF );
1037
1038 property->NrTokens++;
1039
1040 header_bodysize_grow( header );
1041 }
1042
1043 struct tgsi_property_data
1044 tgsi_build_property_data(
1045 unsigned value,
1046 struct tgsi_property *property,
1047 struct tgsi_header *header )
1048 {
1049 struct tgsi_property_data property_data;
1050
1051 property_data.Data = value;
1052
1053 property_grow( property, header );
1054
1055 return property_data;
1056 }
1057
1058 unsigned
1059 tgsi_build_full_property(
1060 const struct tgsi_full_property *full_prop,
1061 struct tgsi_token *tokens,
1062 struct tgsi_header *header,
1063 unsigned maxsize )
1064 {
1065 unsigned size = 0, i;
1066 struct tgsi_property *property;
1067
1068 if( maxsize <= size )
1069 return 0;
1070 property = (struct tgsi_property *) &tokens[size];
1071 size++;
1072
1073 *property = tgsi_build_property(
1074 full_prop->Property.PropertyName,
1075 header );
1076
1077 assert( full_prop->Property.NrTokens <= 8 + 1 );
1078
1079 for( i = 0; i < full_prop->Property.NrTokens - 1; i++ ) {
1080 struct tgsi_property_data *data;
1081
1082 if( maxsize <= size )
1083 return 0;
1084 data = (struct tgsi_property_data *) &tokens[size];
1085 size++;
1086
1087 *data = tgsi_build_property_data(
1088 full_prop->u[i].Data,
1089 property,
1090 header );
1091 }
1092
1093 return size;
1094 }