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