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