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