Merge commit 'origin/master' into gallium-0.2
[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 "pipe/p_debug.h"
29 #include "pipe/p_shader_tokens.h"
30 #include "tgsi_build.h"
31 #include "tgsi_parse.h"
32
33 /*
34 * version
35 */
36
37 struct tgsi_version
38 tgsi_build_version( void )
39 {
40 struct tgsi_version version;
41
42 version.MajorVersion = 1;
43 version.MinorVersion = 1;
44 version.Padding = 0;
45
46 return version;
47 }
48
49 /*
50 * header
51 */
52
53 struct tgsi_header
54 tgsi_build_header( void )
55 {
56 struct tgsi_header header;
57
58 header.HeaderSize = 1;
59 header.BodySize = 0;
60
61 return header;
62 }
63
64 static void
65 header_headersize_grow( struct tgsi_header *header )
66 {
67 assert( header->HeaderSize < 0xFF );
68 assert( header->BodySize == 0 );
69
70 header->HeaderSize++;
71 }
72
73 static void
74 header_bodysize_grow( struct tgsi_header *header )
75 {
76 assert( header->BodySize < 0xFFFFFF );
77
78 header->BodySize++;
79 }
80
81 struct tgsi_processor
82 tgsi_default_processor( void )
83 {
84 struct tgsi_processor processor;
85
86 processor.Processor = TGSI_PROCESSOR_FRAGMENT;
87 processor.Padding = 0;
88
89 return processor;
90 }
91
92 struct tgsi_processor
93 tgsi_build_processor(
94 unsigned type,
95 struct tgsi_header *header )
96 {
97 struct tgsi_processor processor;
98
99 processor = tgsi_default_processor();
100 processor.Processor = type;
101
102 header_headersize_grow( header );
103
104 return processor;
105 }
106
107 /*
108 * declaration
109 */
110
111 struct tgsi_declaration
112 tgsi_default_declaration( void )
113 {
114 struct tgsi_declaration declaration;
115
116 declaration.Type = TGSI_TOKEN_TYPE_DECLARATION;
117 declaration.Size = 1;
118 declaration.File = TGSI_FILE_NULL;
119 declaration.UsageMask = TGSI_WRITEMASK_XYZW;
120 declaration.Interpolate = TGSI_INTERPOLATE_CONSTANT;
121 declaration.Semantic = 0;
122 declaration.Padding = 0;
123 declaration.Extended = 0;
124
125 return declaration;
126 }
127
128 struct tgsi_declaration
129 tgsi_build_declaration(
130 unsigned file,
131 unsigned usage_mask,
132 unsigned interpolate,
133 unsigned semantic,
134 struct tgsi_header *header )
135 {
136 struct tgsi_declaration declaration;
137
138 assert( file <= TGSI_FILE_IMMEDIATE );
139 assert( interpolate <= TGSI_INTERPOLATE_PERSPECTIVE );
140
141 declaration = tgsi_default_declaration();
142 declaration.File = file;
143 declaration.UsageMask = usage_mask;
144 declaration.Interpolate = interpolate;
145 declaration.Semantic = semantic;
146
147 header_bodysize_grow( header );
148
149 return declaration;
150 }
151
152 static void
153 declaration_grow(
154 struct tgsi_declaration *declaration,
155 struct tgsi_header *header )
156 {
157 assert( declaration->Size < 0xFF );
158
159 declaration->Size++;
160
161 header_bodysize_grow( header );
162 }
163
164 struct tgsi_full_declaration
165 tgsi_default_full_declaration( void )
166 {
167 struct tgsi_full_declaration full_declaration;
168
169 full_declaration.Declaration = tgsi_default_declaration();
170 full_declaration.DeclarationRange = tgsi_default_declaration_range();
171 full_declaration.Semantic = tgsi_default_declaration_semantic();
172
173 return full_declaration;
174 }
175
176 unsigned
177 tgsi_build_full_declaration(
178 const struct tgsi_full_declaration *full_decl,
179 struct tgsi_token *tokens,
180 struct tgsi_header *header,
181 unsigned maxsize )
182 {
183 unsigned size = 0;
184 struct tgsi_declaration *declaration;
185 struct tgsi_declaration_range *dr;
186
187 if( maxsize <= size )
188 return 0;
189 declaration = (struct tgsi_declaration *) &tokens[size];
190 size++;
191
192 *declaration = tgsi_build_declaration(
193 full_decl->Declaration.File,
194 full_decl->Declaration.UsageMask,
195 full_decl->Declaration.Interpolate,
196 full_decl->Declaration.Semantic,
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->DeclarationRange.First,
206 full_decl->DeclarationRange.Last,
207 declaration,
208 header );
209
210 if( full_decl->Declaration.Semantic ) {
211 struct tgsi_declaration_semantic *ds;
212
213 if( maxsize <= size )
214 return 0;
215 ds = (struct tgsi_declaration_semantic *) &tokens[size];
216 size++;
217
218 *ds = tgsi_build_declaration_semantic(
219 full_decl->Semantic.SemanticName,
220 full_decl->Semantic.SemanticIndex,
221 declaration,
222 header );
223 }
224
225 return size;
226 }
227
228 struct tgsi_declaration_range
229 tgsi_default_declaration_range( void )
230 {
231 struct tgsi_declaration_range dr;
232
233 dr.First = 0;
234 dr.Last = 0;
235
236 return dr;
237 }
238
239 struct tgsi_declaration_range
240 tgsi_build_declaration_range(
241 unsigned first,
242 unsigned last,
243 struct tgsi_declaration *declaration,
244 struct tgsi_header *header )
245 {
246 struct tgsi_declaration_range declaration_range;
247
248 assert( last >= first );
249 assert( last <= 0xFFFF );
250
251 declaration_range = tgsi_default_declaration_range();
252 declaration_range.First = first;
253 declaration_range.Last = last;
254
255 declaration_grow( declaration, header );
256
257 return declaration_range;
258 }
259
260 struct tgsi_declaration_semantic
261 tgsi_default_declaration_semantic( void )
262 {
263 struct tgsi_declaration_semantic ds;
264
265 ds.SemanticName = TGSI_SEMANTIC_POSITION;
266 ds.SemanticIndex = 0;
267 ds.Padding = 0;
268
269 return ds;
270 }
271
272 struct tgsi_declaration_semantic
273 tgsi_build_declaration_semantic(
274 unsigned semantic_name,
275 unsigned semantic_index,
276 struct tgsi_declaration *declaration,
277 struct tgsi_header *header )
278 {
279 struct tgsi_declaration_semantic ds;
280
281 assert( semantic_name <= TGSI_SEMANTIC_COUNT );
282 assert( semantic_index <= 0xFFFF );
283
284 ds = tgsi_default_declaration_semantic();
285 ds.SemanticName = semantic_name;
286 ds.SemanticIndex = semantic_index;
287
288 declaration_grow( declaration, header );
289
290 return ds;
291 }
292
293 /*
294 * immediate
295 */
296
297 struct tgsi_immediate
298 tgsi_default_immediate( void )
299 {
300 struct tgsi_immediate immediate;
301
302 immediate.Type = TGSI_TOKEN_TYPE_IMMEDIATE;
303 immediate.Size = 1;
304 immediate.DataType = TGSI_IMM_FLOAT32;
305 immediate.Padding = 0;
306 immediate.Extended = 0;
307
308 return immediate;
309 }
310
311 struct tgsi_immediate
312 tgsi_build_immediate(
313 struct tgsi_header *header )
314 {
315 struct tgsi_immediate immediate;
316
317 immediate = tgsi_default_immediate();
318
319 header_bodysize_grow( header );
320
321 return immediate;
322 }
323
324 struct tgsi_full_immediate
325 tgsi_default_full_immediate( void )
326 {
327 struct tgsi_full_immediate fullimm;
328
329 fullimm.Immediate = tgsi_default_immediate();
330 fullimm.u.Pointer = (void *) 0;
331
332 return fullimm;
333 }
334
335 static void
336 immediate_grow(
337 struct tgsi_immediate *immediate,
338 struct tgsi_header *header )
339 {
340 assert( immediate->Size < 0xFF );
341
342 immediate->Size++;
343
344 header_bodysize_grow( header );
345 }
346
347 struct tgsi_immediate_float32
348 tgsi_build_immediate_float32(
349 float value,
350 struct tgsi_immediate *immediate,
351 struct tgsi_header *header )
352 {
353 struct tgsi_immediate_float32 immediate_float32;
354
355 immediate_float32.Float = value;
356
357 immediate_grow( immediate, header );
358
359 return immediate_float32;
360 }
361
362 unsigned
363 tgsi_build_full_immediate(
364 const struct tgsi_full_immediate *full_imm,
365 struct tgsi_token *tokens,
366 struct tgsi_header *header,
367 unsigned maxsize )
368 {
369 unsigned size = 0, i;
370 struct tgsi_immediate *immediate;
371
372 if( maxsize <= size )
373 return 0;
374 immediate = (struct tgsi_immediate *) &tokens[size];
375 size++;
376
377 *immediate = tgsi_build_immediate( header );
378
379 for( i = 0; i < full_imm->Immediate.Size - 1; i++ ) {
380 struct tgsi_immediate_float32 *if32;
381
382 if( maxsize <= size )
383 return 0;
384 if32 = (struct tgsi_immediate_float32 *) &tokens[size];
385 size++;
386
387 *if32 = tgsi_build_immediate_float32(
388 full_imm->u.ImmediateFloat32[i].Float,
389 immediate,
390 header );
391 }
392
393 return size;
394 }
395
396 /*
397 * instruction
398 */
399
400 struct tgsi_instruction
401 tgsi_default_instruction( void )
402 {
403 struct tgsi_instruction instruction;
404
405 instruction.Type = TGSI_TOKEN_TYPE_INSTRUCTION;
406 instruction.Size = 1;
407 instruction.Opcode = TGSI_OPCODE_MOV;
408 instruction.Saturate = TGSI_SAT_NONE;
409 instruction.NumDstRegs = 1;
410 instruction.NumSrcRegs = 1;
411 instruction.Padding = 0;
412 instruction.Extended = 0;
413
414 return instruction;
415 }
416
417 struct tgsi_instruction
418 tgsi_build_instruction(
419 unsigned opcode,
420 unsigned saturate,
421 unsigned num_dst_regs,
422 unsigned num_src_regs,
423 struct tgsi_header *header )
424 {
425 struct tgsi_instruction instruction;
426
427 assert (opcode <= TGSI_OPCODE_LAST);
428 assert (saturate <= TGSI_SAT_MINUS_PLUS_ONE);
429 assert (num_dst_regs <= 3);
430 assert (num_src_regs <= 15);
431
432 instruction = tgsi_default_instruction();
433 instruction.Opcode = opcode;
434 instruction.Saturate = saturate;
435 instruction.NumDstRegs = num_dst_regs;
436 instruction.NumSrcRegs = num_src_regs;
437
438 header_bodysize_grow( header );
439
440 return instruction;
441 }
442
443 static void
444 instruction_grow(
445 struct tgsi_instruction *instruction,
446 struct tgsi_header *header )
447 {
448 assert (instruction->Size < 0xFF);
449
450 instruction->Size++;
451
452 header_bodysize_grow( header );
453 }
454
455 struct tgsi_full_instruction
456 tgsi_default_full_instruction( void )
457 {
458 struct tgsi_full_instruction full_instruction;
459 unsigned i;
460
461 full_instruction.Instruction = tgsi_default_instruction();
462 full_instruction.InstructionExtNv = tgsi_default_instruction_ext_nv();
463 full_instruction.InstructionExtLabel = tgsi_default_instruction_ext_label();
464 full_instruction.InstructionExtTexture = tgsi_default_instruction_ext_texture();
465 for( i = 0; i < TGSI_FULL_MAX_DST_REGISTERS; i++ ) {
466 full_instruction.FullDstRegisters[i] = tgsi_default_full_dst_register();
467 }
468 for( i = 0; i < TGSI_FULL_MAX_SRC_REGISTERS; i++ ) {
469 full_instruction.FullSrcRegisters[i] = tgsi_default_full_src_register();
470 }
471
472 return full_instruction;
473 }
474
475 unsigned
476 tgsi_build_full_instruction(
477 const struct tgsi_full_instruction *full_inst,
478 struct tgsi_token *tokens,
479 struct tgsi_header *header,
480 unsigned maxsize )
481 {
482 unsigned size = 0;
483 unsigned i;
484 struct tgsi_instruction *instruction;
485 struct tgsi_token *prev_token;
486
487 if( maxsize <= size )
488 return 0;
489 instruction = (struct tgsi_instruction *) &tokens[size];
490 size++;
491
492 *instruction = tgsi_build_instruction(
493 full_inst->Instruction.Opcode,
494 full_inst->Instruction.Saturate,
495 full_inst->Instruction.NumDstRegs,
496 full_inst->Instruction.NumSrcRegs,
497 header );
498 prev_token = (struct tgsi_token *) instruction;
499
500 if( tgsi_compare_instruction_ext_nv(
501 full_inst->InstructionExtNv,
502 tgsi_default_instruction_ext_nv() ) ) {
503 struct tgsi_instruction_ext_nv *instruction_ext_nv;
504
505 if( maxsize <= size )
506 return 0;
507 instruction_ext_nv =
508 (struct tgsi_instruction_ext_nv *) &tokens[size];
509 size++;
510
511 *instruction_ext_nv = tgsi_build_instruction_ext_nv(
512 full_inst->InstructionExtNv.Precision,
513 full_inst->InstructionExtNv.CondDstIndex,
514 full_inst->InstructionExtNv.CondFlowIndex,
515 full_inst->InstructionExtNv.CondMask,
516 full_inst->InstructionExtNv.CondSwizzleX,
517 full_inst->InstructionExtNv.CondSwizzleY,
518 full_inst->InstructionExtNv.CondSwizzleZ,
519 full_inst->InstructionExtNv.CondSwizzleW,
520 full_inst->InstructionExtNv.CondDstUpdate,
521 full_inst->InstructionExtNv.CondFlowEnable,
522 prev_token,
523 instruction,
524 header );
525 prev_token = (struct tgsi_token *) instruction_ext_nv;
526 }
527
528 if( tgsi_compare_instruction_ext_label(
529 full_inst->InstructionExtLabel,
530 tgsi_default_instruction_ext_label() ) ) {
531 struct tgsi_instruction_ext_label *instruction_ext_label;
532
533 if( maxsize <= size )
534 return 0;
535 instruction_ext_label =
536 (struct tgsi_instruction_ext_label *) &tokens[size];
537 size++;
538
539 *instruction_ext_label = tgsi_build_instruction_ext_label(
540 full_inst->InstructionExtLabel.Label,
541 prev_token,
542 instruction,
543 header );
544 prev_token = (struct tgsi_token *) instruction_ext_label;
545 }
546
547 if( tgsi_compare_instruction_ext_texture(
548 full_inst->InstructionExtTexture,
549 tgsi_default_instruction_ext_texture() ) ) {
550 struct tgsi_instruction_ext_texture *instruction_ext_texture;
551
552 if( maxsize <= size )
553 return 0;
554 instruction_ext_texture =
555 (struct tgsi_instruction_ext_texture *) &tokens[size];
556 size++;
557
558 *instruction_ext_texture = tgsi_build_instruction_ext_texture(
559 full_inst->InstructionExtTexture.Texture,
560 prev_token,
561 instruction,
562 header );
563 prev_token = (struct tgsi_token *) instruction_ext_texture;
564 }
565
566 for( i = 0; i < full_inst->Instruction.NumDstRegs; i++ ) {
567 const struct tgsi_full_dst_register *reg = &full_inst->FullDstRegisters[i];
568 struct tgsi_dst_register *dst_register;
569 struct tgsi_token *prev_token;
570
571 if( maxsize <= size )
572 return 0;
573 dst_register = (struct tgsi_dst_register *) &tokens[size];
574 size++;
575
576 *dst_register = tgsi_build_dst_register(
577 reg->DstRegister.File,
578 reg->DstRegister.WriteMask,
579 reg->DstRegister.Index,
580 instruction,
581 header );
582 prev_token = (struct tgsi_token *) dst_register;
583
584 if( tgsi_compare_dst_register_ext_concode(
585 reg->DstRegisterExtConcode,
586 tgsi_default_dst_register_ext_concode() ) ) {
587 struct tgsi_dst_register_ext_concode *dst_register_ext_concode;
588
589 if( maxsize <= size )
590 return 0;
591 dst_register_ext_concode =
592 (struct tgsi_dst_register_ext_concode *) &tokens[size];
593 size++;
594
595 *dst_register_ext_concode = tgsi_build_dst_register_ext_concode(
596 reg->DstRegisterExtConcode.CondMask,
597 reg->DstRegisterExtConcode.CondSwizzleX,
598 reg->DstRegisterExtConcode.CondSwizzleY,
599 reg->DstRegisterExtConcode.CondSwizzleZ,
600 reg->DstRegisterExtConcode.CondSwizzleW,
601 reg->DstRegisterExtConcode.CondSrcIndex,
602 prev_token,
603 instruction,
604 header );
605 prev_token = (struct tgsi_token *) dst_register_ext_concode;
606 }
607
608 if( tgsi_compare_dst_register_ext_modulate(
609 reg->DstRegisterExtModulate,
610 tgsi_default_dst_register_ext_modulate() ) ) {
611 struct tgsi_dst_register_ext_modulate *dst_register_ext_modulate;
612
613 if( maxsize <= size )
614 return 0;
615 dst_register_ext_modulate =
616 (struct tgsi_dst_register_ext_modulate *) &tokens[size];
617 size++;
618
619 *dst_register_ext_modulate = tgsi_build_dst_register_ext_modulate(
620 reg->DstRegisterExtModulate.Modulate,
621 prev_token,
622 instruction,
623 header );
624 prev_token = (struct tgsi_token *) dst_register_ext_modulate;
625 }
626 }
627
628 for( i = 0; i < full_inst->Instruction.NumSrcRegs; i++ ) {
629 const struct tgsi_full_src_register *reg = &full_inst->FullSrcRegisters[i];
630 struct tgsi_src_register *src_register;
631 struct tgsi_token *prev_token;
632
633 if( maxsize <= size )
634 return 0;
635 src_register = (struct tgsi_src_register *) &tokens[size];
636 size++;
637
638 *src_register = tgsi_build_src_register(
639 reg->SrcRegister.File,
640 reg->SrcRegister.SwizzleX,
641 reg->SrcRegister.SwizzleY,
642 reg->SrcRegister.SwizzleZ,
643 reg->SrcRegister.SwizzleW,
644 reg->SrcRegister.Negate,
645 reg->SrcRegister.Indirect,
646 reg->SrcRegister.Dimension,
647 reg->SrcRegister.Index,
648 instruction,
649 header );
650 prev_token = (struct tgsi_token *) src_register;
651
652 if( tgsi_compare_src_register_ext_swz(
653 reg->SrcRegisterExtSwz,
654 tgsi_default_src_register_ext_swz() ) ) {
655 struct tgsi_src_register_ext_swz *src_register_ext_swz;
656
657 /* Use of the extended swizzle requires the simple swizzle to be identity.
658 */
659 assert( reg->SrcRegister.SwizzleX == TGSI_SWIZZLE_X );
660 assert( reg->SrcRegister.SwizzleY == TGSI_SWIZZLE_Y );
661 assert( reg->SrcRegister.SwizzleZ == TGSI_SWIZZLE_Z );
662 assert( reg->SrcRegister.SwizzleW == TGSI_SWIZZLE_W );
663 assert( reg->SrcRegister.Negate == FALSE );
664
665 if( maxsize <= size )
666 return 0;
667 src_register_ext_swz =
668 (struct tgsi_src_register_ext_swz *) &tokens[size];
669 size++;
670
671 *src_register_ext_swz = tgsi_build_src_register_ext_swz(
672 reg->SrcRegisterExtSwz.ExtSwizzleX,
673 reg->SrcRegisterExtSwz.ExtSwizzleY,
674 reg->SrcRegisterExtSwz.ExtSwizzleZ,
675 reg->SrcRegisterExtSwz.ExtSwizzleW,
676 reg->SrcRegisterExtSwz.NegateX,
677 reg->SrcRegisterExtSwz.NegateY,
678 reg->SrcRegisterExtSwz.NegateZ,
679 reg->SrcRegisterExtSwz.NegateW,
680 prev_token,
681 instruction,
682 header );
683 prev_token = (struct tgsi_token *) src_register_ext_swz;
684 }
685
686 if( tgsi_compare_src_register_ext_mod(
687 reg->SrcRegisterExtMod,
688 tgsi_default_src_register_ext_mod() ) ) {
689 struct tgsi_src_register_ext_mod *src_register_ext_mod;
690
691 if( maxsize <= size )
692 return 0;
693 src_register_ext_mod =
694 (struct tgsi_src_register_ext_mod *) &tokens[size];
695 size++;
696
697 *src_register_ext_mod = tgsi_build_src_register_ext_mod(
698 reg->SrcRegisterExtMod.Complement,
699 reg->SrcRegisterExtMod.Bias,
700 reg->SrcRegisterExtMod.Scale2X,
701 reg->SrcRegisterExtMod.Absolute,
702 reg->SrcRegisterExtMod.Negate,
703 prev_token,
704 instruction,
705 header );
706 prev_token = (struct tgsi_token *) src_register_ext_mod;
707 }
708
709 if( reg->SrcRegister.Indirect ) {
710 struct tgsi_src_register *ind;
711
712 if( maxsize <= size )
713 return 0;
714 ind = (struct tgsi_src_register *) &tokens[size];
715 size++;
716
717 *ind = tgsi_build_src_register(
718 reg->SrcRegisterInd.File,
719 reg->SrcRegisterInd.SwizzleX,
720 reg->SrcRegisterInd.SwizzleY,
721 reg->SrcRegisterInd.SwizzleZ,
722 reg->SrcRegisterInd.SwizzleW,
723 reg->SrcRegisterInd.Negate,
724 reg->SrcRegisterInd.Indirect,
725 reg->SrcRegisterInd.Dimension,
726 reg->SrcRegisterInd.Index,
727 instruction,
728 header );
729 }
730
731 if( reg->SrcRegister.Dimension ) {
732 struct tgsi_dimension *dim;
733
734 assert( !reg->SrcRegisterDim.Dimension );
735
736 if( maxsize <= size )
737 return 0;
738 dim = (struct tgsi_dimension *) &tokens[size];
739 size++;
740
741 *dim = tgsi_build_dimension(
742 reg->SrcRegisterDim.Indirect,
743 reg->SrcRegisterDim.Index,
744 instruction,
745 header );
746
747 if( reg->SrcRegisterDim.Indirect ) {
748 struct tgsi_src_register *ind;
749
750 if( maxsize <= size )
751 return 0;
752 ind = (struct tgsi_src_register *) &tokens[size];
753 size++;
754
755 *ind = tgsi_build_src_register(
756 reg->SrcRegisterDimInd.File,
757 reg->SrcRegisterDimInd.SwizzleX,
758 reg->SrcRegisterDimInd.SwizzleY,
759 reg->SrcRegisterDimInd.SwizzleZ,
760 reg->SrcRegisterDimInd.SwizzleW,
761 reg->SrcRegisterDimInd.Negate,
762 reg->SrcRegisterDimInd.Indirect,
763 reg->SrcRegisterDimInd.Dimension,
764 reg->SrcRegisterDimInd.Index,
765 instruction,
766 header );
767 }
768 }
769 }
770
771 return size;
772 }
773
774 struct tgsi_instruction_ext_nv
775 tgsi_default_instruction_ext_nv( void )
776 {
777 struct tgsi_instruction_ext_nv instruction_ext_nv;
778
779 instruction_ext_nv.Type = TGSI_INSTRUCTION_EXT_TYPE_NV;
780 instruction_ext_nv.Precision = TGSI_PRECISION_DEFAULT;
781 instruction_ext_nv.CondDstIndex = 0;
782 instruction_ext_nv.CondFlowIndex = 0;
783 instruction_ext_nv.CondMask = TGSI_CC_TR;
784 instruction_ext_nv.CondSwizzleX = TGSI_SWIZZLE_X;
785 instruction_ext_nv.CondSwizzleY = TGSI_SWIZZLE_Y;
786 instruction_ext_nv.CondSwizzleZ = TGSI_SWIZZLE_Z;
787 instruction_ext_nv.CondSwizzleW = TGSI_SWIZZLE_W;
788 instruction_ext_nv.CondDstUpdate = 0;
789 instruction_ext_nv.CondFlowEnable = 0;
790 instruction_ext_nv.Padding = 0;
791 instruction_ext_nv.Extended = 0;
792
793 return instruction_ext_nv;
794 }
795
796
797 /** test for inequality of 32-bit values pointed to by a and b */
798 static INLINE boolean
799 compare32(const void *a, const void *b)
800 {
801 return *((uint32_t *) a) != *((uint32_t *) b);
802 }
803
804
805 unsigned
806 tgsi_compare_instruction_ext_nv(
807 struct tgsi_instruction_ext_nv a,
808 struct tgsi_instruction_ext_nv b )
809 {
810 a.Padding = b.Padding = 0;
811 a.Extended = b.Extended = 0;
812 return compare32(&a, &b);
813 }
814
815 struct tgsi_instruction_ext_nv
816 tgsi_build_instruction_ext_nv(
817 unsigned precision,
818 unsigned cond_dst_index,
819 unsigned cond_flow_index,
820 unsigned cond_mask,
821 unsigned cond_swizzle_x,
822 unsigned cond_swizzle_y,
823 unsigned cond_swizzle_z,
824 unsigned cond_swizzle_w,
825 unsigned cond_dst_update,
826 unsigned cond_flow_enable,
827 struct tgsi_token *prev_token,
828 struct tgsi_instruction *instruction,
829 struct tgsi_header *header )
830 {
831 struct tgsi_instruction_ext_nv instruction_ext_nv;
832
833 instruction_ext_nv = tgsi_default_instruction_ext_nv();
834 instruction_ext_nv.Precision = precision;
835 instruction_ext_nv.CondDstIndex = cond_dst_index;
836 instruction_ext_nv.CondFlowIndex = cond_flow_index;
837 instruction_ext_nv.CondMask = cond_mask;
838 instruction_ext_nv.CondSwizzleX = cond_swizzle_x;
839 instruction_ext_nv.CondSwizzleY = cond_swizzle_y;
840 instruction_ext_nv.CondSwizzleZ = cond_swizzle_z;
841 instruction_ext_nv.CondSwizzleW = cond_swizzle_w;
842 instruction_ext_nv.CondDstUpdate = cond_dst_update;
843 instruction_ext_nv.CondFlowEnable = cond_flow_enable;
844
845 prev_token->Extended = 1;
846 instruction_grow( instruction, header );
847
848 return instruction_ext_nv;
849 }
850
851 struct tgsi_instruction_ext_label
852 tgsi_default_instruction_ext_label( void )
853 {
854 struct tgsi_instruction_ext_label instruction_ext_label;
855
856 instruction_ext_label.Type = TGSI_INSTRUCTION_EXT_TYPE_LABEL;
857 instruction_ext_label.Label = 0;
858 instruction_ext_label.Padding = 0;
859 instruction_ext_label.Extended = 0;
860
861 return instruction_ext_label;
862 }
863
864 unsigned
865 tgsi_compare_instruction_ext_label(
866 struct tgsi_instruction_ext_label a,
867 struct tgsi_instruction_ext_label b )
868 {
869 a.Padding = b.Padding = 0;
870 a.Extended = b.Extended = 0;
871 return compare32(&a, &b);
872 }
873
874 struct tgsi_instruction_ext_label
875 tgsi_build_instruction_ext_label(
876 unsigned label,
877 struct tgsi_token *prev_token,
878 struct tgsi_instruction *instruction,
879 struct tgsi_header *header )
880 {
881 struct tgsi_instruction_ext_label instruction_ext_label;
882
883 instruction_ext_label = tgsi_default_instruction_ext_label();
884 instruction_ext_label.Label = label;
885
886 prev_token->Extended = 1;
887 instruction_grow( instruction, header );
888
889 return instruction_ext_label;
890 }
891
892 struct tgsi_instruction_ext_texture
893 tgsi_default_instruction_ext_texture( void )
894 {
895 struct tgsi_instruction_ext_texture instruction_ext_texture;
896
897 instruction_ext_texture.Type = TGSI_INSTRUCTION_EXT_TYPE_TEXTURE;
898 instruction_ext_texture.Texture = TGSI_TEXTURE_UNKNOWN;
899 instruction_ext_texture.Padding = 0;
900 instruction_ext_texture.Extended = 0;
901
902 return instruction_ext_texture;
903 }
904
905 unsigned
906 tgsi_compare_instruction_ext_texture(
907 struct tgsi_instruction_ext_texture a,
908 struct tgsi_instruction_ext_texture b )
909 {
910 a.Padding = b.Padding = 0;
911 a.Extended = b.Extended = 0;
912 return compare32(&a, &b);
913 }
914
915 struct tgsi_instruction_ext_texture
916 tgsi_build_instruction_ext_texture(
917 unsigned texture,
918 struct tgsi_token *prev_token,
919 struct tgsi_instruction *instruction,
920 struct tgsi_header *header )
921 {
922 struct tgsi_instruction_ext_texture instruction_ext_texture;
923
924 instruction_ext_texture = tgsi_default_instruction_ext_texture();
925 instruction_ext_texture.Texture = texture;
926
927 prev_token->Extended = 1;
928 instruction_grow( instruction, header );
929
930 return instruction_ext_texture;
931 }
932
933 struct tgsi_src_register
934 tgsi_default_src_register( void )
935 {
936 struct tgsi_src_register src_register;
937
938 src_register.File = TGSI_FILE_NULL;
939 src_register.SwizzleX = TGSI_SWIZZLE_X;
940 src_register.SwizzleY = TGSI_SWIZZLE_Y;
941 src_register.SwizzleZ = TGSI_SWIZZLE_Z;
942 src_register.SwizzleW = TGSI_SWIZZLE_W;
943 src_register.Negate = 0;
944 src_register.Indirect = 0;
945 src_register.Dimension = 0;
946 src_register.Index = 0;
947 src_register.Extended = 0;
948
949 return src_register;
950 }
951
952 struct tgsi_src_register
953 tgsi_build_src_register(
954 unsigned file,
955 unsigned swizzle_x,
956 unsigned swizzle_y,
957 unsigned swizzle_z,
958 unsigned swizzle_w,
959 unsigned negate,
960 unsigned indirect,
961 unsigned dimension,
962 int index,
963 struct tgsi_instruction *instruction,
964 struct tgsi_header *header )
965 {
966 struct tgsi_src_register src_register;
967
968 assert( file <= TGSI_FILE_IMMEDIATE );
969 assert( swizzle_x <= TGSI_SWIZZLE_W );
970 assert( swizzle_y <= TGSI_SWIZZLE_W );
971 assert( swizzle_z <= TGSI_SWIZZLE_W );
972 assert( swizzle_w <= TGSI_SWIZZLE_W );
973 assert( negate <= 1 );
974 assert( index >= -0x8000 && index <= 0x7FFF );
975
976 src_register = tgsi_default_src_register();
977 src_register.File = file;
978 src_register.SwizzleX = swizzle_x;
979 src_register.SwizzleY = swizzle_y;
980 src_register.SwizzleZ = swizzle_z;
981 src_register.SwizzleW = swizzle_w;
982 src_register.Negate = negate;
983 src_register.Indirect = indirect;
984 src_register.Dimension = dimension;
985 src_register.Index = index;
986
987 instruction_grow( instruction, header );
988
989 return src_register;
990 }
991
992 struct tgsi_full_src_register
993 tgsi_default_full_src_register( void )
994 {
995 struct tgsi_full_src_register full_src_register;
996
997 full_src_register.SrcRegister = tgsi_default_src_register();
998 full_src_register.SrcRegisterExtSwz = tgsi_default_src_register_ext_swz();
999 full_src_register.SrcRegisterExtMod = tgsi_default_src_register_ext_mod();
1000 full_src_register.SrcRegisterInd = tgsi_default_src_register();
1001 full_src_register.SrcRegisterDim = tgsi_default_dimension();
1002 full_src_register.SrcRegisterDimInd = tgsi_default_src_register();
1003
1004 return full_src_register;
1005 }
1006
1007 struct tgsi_src_register_ext_swz
1008 tgsi_default_src_register_ext_swz( void )
1009 {
1010 struct tgsi_src_register_ext_swz src_register_ext_swz;
1011
1012 src_register_ext_swz.Type = TGSI_SRC_REGISTER_EXT_TYPE_SWZ;
1013 src_register_ext_swz.ExtSwizzleX = TGSI_EXTSWIZZLE_X;
1014 src_register_ext_swz.ExtSwizzleY = TGSI_EXTSWIZZLE_Y;
1015 src_register_ext_swz.ExtSwizzleZ = TGSI_EXTSWIZZLE_Z;
1016 src_register_ext_swz.ExtSwizzleW = TGSI_EXTSWIZZLE_W;
1017 src_register_ext_swz.NegateX = 0;
1018 src_register_ext_swz.NegateY = 0;
1019 src_register_ext_swz.NegateZ = 0;
1020 src_register_ext_swz.NegateW = 0;
1021 src_register_ext_swz.Padding = 0;
1022 src_register_ext_swz.Extended = 0;
1023
1024 return src_register_ext_swz;
1025 }
1026
1027 unsigned
1028 tgsi_compare_src_register_ext_swz(
1029 struct tgsi_src_register_ext_swz a,
1030 struct tgsi_src_register_ext_swz b )
1031 {
1032 a.Padding = b.Padding = 0;
1033 a.Extended = b.Extended = 0;
1034 return compare32(&a, &b);
1035 }
1036
1037 struct tgsi_src_register_ext_swz
1038 tgsi_build_src_register_ext_swz(
1039 unsigned ext_swizzle_x,
1040 unsigned ext_swizzle_y,
1041 unsigned ext_swizzle_z,
1042 unsigned ext_swizzle_w,
1043 unsigned negate_x,
1044 unsigned negate_y,
1045 unsigned negate_z,
1046 unsigned negate_w,
1047 struct tgsi_token *prev_token,
1048 struct tgsi_instruction *instruction,
1049 struct tgsi_header *header )
1050 {
1051 struct tgsi_src_register_ext_swz src_register_ext_swz;
1052
1053 assert( ext_swizzle_x <= TGSI_EXTSWIZZLE_ONE );
1054 assert( ext_swizzle_y <= TGSI_EXTSWIZZLE_ONE );
1055 assert( ext_swizzle_z <= TGSI_EXTSWIZZLE_ONE );
1056 assert( ext_swizzle_w <= TGSI_EXTSWIZZLE_ONE );
1057 assert( negate_x <= 1 );
1058 assert( negate_y <= 1 );
1059 assert( negate_z <= 1 );
1060 assert( negate_w <= 1 );
1061
1062 src_register_ext_swz = tgsi_default_src_register_ext_swz();
1063 src_register_ext_swz.ExtSwizzleX = ext_swizzle_x;
1064 src_register_ext_swz.ExtSwizzleY = ext_swizzle_y;
1065 src_register_ext_swz.ExtSwizzleZ = ext_swizzle_z;
1066 src_register_ext_swz.ExtSwizzleW = ext_swizzle_w;
1067 src_register_ext_swz.NegateX = negate_x;
1068 src_register_ext_swz.NegateY = negate_y;
1069 src_register_ext_swz.NegateZ = negate_z;
1070 src_register_ext_swz.NegateW = negate_w;
1071
1072 prev_token->Extended = 1;
1073 instruction_grow( instruction, header );
1074
1075 return src_register_ext_swz;
1076 }
1077
1078 struct tgsi_src_register_ext_mod
1079 tgsi_default_src_register_ext_mod( void )
1080 {
1081 struct tgsi_src_register_ext_mod src_register_ext_mod;
1082
1083 src_register_ext_mod.Type = TGSI_SRC_REGISTER_EXT_TYPE_MOD;
1084 src_register_ext_mod.Complement = 0;
1085 src_register_ext_mod.Bias = 0;
1086 src_register_ext_mod.Scale2X = 0;
1087 src_register_ext_mod.Absolute = 0;
1088 src_register_ext_mod.Negate = 0;
1089 src_register_ext_mod.Padding = 0;
1090 src_register_ext_mod.Extended = 0;
1091
1092 return src_register_ext_mod;
1093 }
1094
1095 unsigned
1096 tgsi_compare_src_register_ext_mod(
1097 struct tgsi_src_register_ext_mod a,
1098 struct tgsi_src_register_ext_mod b )
1099 {
1100 a.Padding = b.Padding = 0;
1101 a.Extended = b.Extended = 0;
1102 return compare32(&a, &b);
1103 }
1104
1105 struct tgsi_src_register_ext_mod
1106 tgsi_build_src_register_ext_mod(
1107 unsigned complement,
1108 unsigned bias,
1109 unsigned scale_2x,
1110 unsigned absolute,
1111 unsigned negate,
1112 struct tgsi_token *prev_token,
1113 struct tgsi_instruction *instruction,
1114 struct tgsi_header *header )
1115 {
1116 struct tgsi_src_register_ext_mod src_register_ext_mod;
1117
1118 assert( complement <= 1 );
1119 assert( bias <= 1 );
1120 assert( scale_2x <= 1 );
1121 assert( absolute <= 1 );
1122 assert( negate <= 1 );
1123
1124 src_register_ext_mod = tgsi_default_src_register_ext_mod();
1125 src_register_ext_mod.Complement = complement;
1126 src_register_ext_mod.Bias = bias;
1127 src_register_ext_mod.Scale2X = scale_2x;
1128 src_register_ext_mod.Absolute = absolute;
1129 src_register_ext_mod.Negate = negate;
1130
1131 prev_token->Extended = 1;
1132 instruction_grow( instruction, header );
1133
1134 return src_register_ext_mod;
1135 }
1136
1137 struct tgsi_dimension
1138 tgsi_default_dimension( void )
1139 {
1140 struct tgsi_dimension dimension;
1141
1142 dimension.Indirect = 0;
1143 dimension.Dimension = 0;
1144 dimension.Padding = 0;
1145 dimension.Index = 0;
1146 dimension.Extended = 0;
1147
1148 return dimension;
1149 }
1150
1151 struct tgsi_dimension
1152 tgsi_build_dimension(
1153 unsigned indirect,
1154 unsigned index,
1155 struct tgsi_instruction *instruction,
1156 struct tgsi_header *header )
1157 {
1158 struct tgsi_dimension dimension;
1159
1160 dimension = tgsi_default_dimension();
1161 dimension.Indirect = indirect;
1162 dimension.Index = index;
1163
1164 instruction_grow( instruction, header );
1165
1166 return dimension;
1167 }
1168
1169 struct tgsi_dst_register
1170 tgsi_default_dst_register( void )
1171 {
1172 struct tgsi_dst_register dst_register;
1173
1174 dst_register.File = TGSI_FILE_NULL;
1175 dst_register.WriteMask = TGSI_WRITEMASK_XYZW;
1176 dst_register.Indirect = 0;
1177 dst_register.Dimension = 0;
1178 dst_register.Index = 0;
1179 dst_register.Padding = 0;
1180 dst_register.Extended = 0;
1181
1182 return dst_register;
1183 }
1184
1185 struct tgsi_dst_register
1186 tgsi_build_dst_register(
1187 unsigned file,
1188 unsigned mask,
1189 int index,
1190 struct tgsi_instruction *instruction,
1191 struct tgsi_header *header )
1192 {
1193 struct tgsi_dst_register dst_register;
1194
1195 assert( file <= TGSI_FILE_IMMEDIATE );
1196 assert( mask <= TGSI_WRITEMASK_XYZW );
1197 assert( index >= -32768 && index <= 32767 );
1198
1199 dst_register = tgsi_default_dst_register();
1200 dst_register.File = file;
1201 dst_register.WriteMask = mask;
1202 dst_register.Index = index;
1203
1204 instruction_grow( instruction, header );
1205
1206 return dst_register;
1207 }
1208
1209 struct tgsi_full_dst_register
1210 tgsi_default_full_dst_register( void )
1211 {
1212 struct tgsi_full_dst_register full_dst_register;
1213
1214 full_dst_register.DstRegister = tgsi_default_dst_register();
1215 full_dst_register.DstRegisterExtConcode =
1216 tgsi_default_dst_register_ext_concode();
1217 full_dst_register.DstRegisterExtModulate =
1218 tgsi_default_dst_register_ext_modulate();
1219
1220 return full_dst_register;
1221 }
1222
1223 struct tgsi_dst_register_ext_concode
1224 tgsi_default_dst_register_ext_concode( void )
1225 {
1226 struct tgsi_dst_register_ext_concode dst_register_ext_concode;
1227
1228 dst_register_ext_concode.Type = TGSI_DST_REGISTER_EXT_TYPE_CONDCODE;
1229 dst_register_ext_concode.CondMask = TGSI_CC_TR;
1230 dst_register_ext_concode.CondSwizzleX = TGSI_SWIZZLE_X;
1231 dst_register_ext_concode.CondSwizzleY = TGSI_SWIZZLE_Y;
1232 dst_register_ext_concode.CondSwizzleZ = TGSI_SWIZZLE_Z;
1233 dst_register_ext_concode.CondSwizzleW = TGSI_SWIZZLE_W;
1234 dst_register_ext_concode.CondSrcIndex = 0;
1235 dst_register_ext_concode.Padding = 0;
1236 dst_register_ext_concode.Extended = 0;
1237
1238 return dst_register_ext_concode;
1239 }
1240
1241 unsigned
1242 tgsi_compare_dst_register_ext_concode(
1243 struct tgsi_dst_register_ext_concode a,
1244 struct tgsi_dst_register_ext_concode b )
1245 {
1246 a.Padding = b.Padding = 0;
1247 a.Extended = b.Extended = 0;
1248 return compare32(&a, &b);
1249 }
1250
1251 struct tgsi_dst_register_ext_concode
1252 tgsi_build_dst_register_ext_concode(
1253 unsigned cc,
1254 unsigned swizzle_x,
1255 unsigned swizzle_y,
1256 unsigned swizzle_z,
1257 unsigned swizzle_w,
1258 int index,
1259 struct tgsi_token *prev_token,
1260 struct tgsi_instruction *instruction,
1261 struct tgsi_header *header )
1262 {
1263 struct tgsi_dst_register_ext_concode dst_register_ext_concode;
1264
1265 assert( cc <= TGSI_CC_FL );
1266 assert( swizzle_x <= TGSI_SWIZZLE_W );
1267 assert( swizzle_y <= TGSI_SWIZZLE_W );
1268 assert( swizzle_z <= TGSI_SWIZZLE_W );
1269 assert( swizzle_w <= TGSI_SWIZZLE_W );
1270 assert( index >= -32768 && index <= 32767 );
1271
1272 dst_register_ext_concode = tgsi_default_dst_register_ext_concode();
1273 dst_register_ext_concode.CondMask = cc;
1274 dst_register_ext_concode.CondSwizzleX = swizzle_x;
1275 dst_register_ext_concode.CondSwizzleY = swizzle_y;
1276 dst_register_ext_concode.CondSwizzleZ = swizzle_z;
1277 dst_register_ext_concode.CondSwizzleW = swizzle_w;
1278 dst_register_ext_concode.CondSrcIndex = index;
1279
1280 prev_token->Extended = 1;
1281 instruction_grow( instruction, header );
1282
1283 return dst_register_ext_concode;
1284 }
1285
1286 struct tgsi_dst_register_ext_modulate
1287 tgsi_default_dst_register_ext_modulate( void )
1288 {
1289 struct tgsi_dst_register_ext_modulate dst_register_ext_modulate;
1290
1291 dst_register_ext_modulate.Type = TGSI_DST_REGISTER_EXT_TYPE_MODULATE;
1292 dst_register_ext_modulate.Modulate = TGSI_MODULATE_1X;
1293 dst_register_ext_modulate.Padding = 0;
1294 dst_register_ext_modulate.Extended = 0;
1295
1296 return dst_register_ext_modulate;
1297 }
1298
1299 unsigned
1300 tgsi_compare_dst_register_ext_modulate(
1301 struct tgsi_dst_register_ext_modulate a,
1302 struct tgsi_dst_register_ext_modulate b )
1303 {
1304 a.Padding = b.Padding = 0;
1305 a.Extended = b.Extended = 0;
1306 return compare32(&a, &b);
1307 }
1308
1309 struct tgsi_dst_register_ext_modulate
1310 tgsi_build_dst_register_ext_modulate(
1311 unsigned modulate,
1312 struct tgsi_token *prev_token,
1313 struct tgsi_instruction *instruction,
1314 struct tgsi_header *header )
1315 {
1316 struct tgsi_dst_register_ext_modulate dst_register_ext_modulate;
1317
1318 assert( modulate <= TGSI_MODULATE_EIGHTH );
1319
1320 dst_register_ext_modulate = tgsi_default_dst_register_ext_modulate();
1321 dst_register_ext_modulate.Modulate = modulate;
1322
1323 prev_token->Extended = 1;
1324 instruction_grow( instruction, header );
1325
1326 return dst_register_ext_modulate;
1327 }