Merge remote branch 'main/master' into radeon-rewrite
[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_IMMEDIATE );
143 assert( interpolate <= TGSI_INTERPOLATE_PERSPECTIVE );
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.Pointer = (void *) 0;
339
340 return fullimm;
341 }
342
343 static void
344 immediate_grow(
345 struct tgsi_immediate *immediate,
346 struct tgsi_header *header )
347 {
348 assert( immediate->NrTokens < 0xFF );
349
350 immediate->NrTokens++;
351
352 header_bodysize_grow( header );
353 }
354
355 struct tgsi_immediate_float32
356 tgsi_build_immediate_float32(
357 float value,
358 struct tgsi_immediate *immediate,
359 struct tgsi_header *header )
360 {
361 struct tgsi_immediate_float32 immediate_float32;
362
363 immediate_float32.Float = value;
364
365 immediate_grow( immediate, header );
366
367 return immediate_float32;
368 }
369
370 unsigned
371 tgsi_build_full_immediate(
372 const struct tgsi_full_immediate *full_imm,
373 struct tgsi_token *tokens,
374 struct tgsi_header *header,
375 unsigned maxsize )
376 {
377 unsigned size = 0, i;
378 struct tgsi_immediate *immediate;
379
380 if( maxsize <= size )
381 return 0;
382 immediate = (struct tgsi_immediate *) &tokens[size];
383 size++;
384
385 *immediate = tgsi_build_immediate( header );
386
387 for( i = 0; i < full_imm->Immediate.NrTokens - 1; i++ ) {
388 struct tgsi_immediate_float32 *if32;
389
390 if( maxsize <= size )
391 return 0;
392 if32 = (struct tgsi_immediate_float32 *) &tokens[size];
393 size++;
394
395 *if32 = tgsi_build_immediate_float32(
396 full_imm->u.ImmediateFloat32[i].Float,
397 immediate,
398 header );
399 }
400
401 return size;
402 }
403
404 /*
405 * instruction
406 */
407
408 struct tgsi_instruction
409 tgsi_default_instruction( void )
410 {
411 struct tgsi_instruction instruction;
412
413 instruction.Type = TGSI_TOKEN_TYPE_INSTRUCTION;
414 instruction.NrTokens = 1;
415 instruction.Opcode = TGSI_OPCODE_MOV;
416 instruction.Saturate = TGSI_SAT_NONE;
417 instruction.NumDstRegs = 1;
418 instruction.NumSrcRegs = 1;
419 instruction.Padding = 0;
420 instruction.Extended = 0;
421
422 return instruction;
423 }
424
425 struct tgsi_instruction
426 tgsi_build_instruction(
427 unsigned opcode,
428 unsigned saturate,
429 unsigned num_dst_regs,
430 unsigned num_src_regs,
431 struct tgsi_header *header )
432 {
433 struct tgsi_instruction instruction;
434
435 assert (opcode <= TGSI_OPCODE_LAST);
436 assert (saturate <= TGSI_SAT_MINUS_PLUS_ONE);
437 assert (num_dst_regs <= 3);
438 assert (num_src_regs <= 15);
439
440 instruction = tgsi_default_instruction();
441 instruction.Opcode = opcode;
442 instruction.Saturate = saturate;
443 instruction.NumDstRegs = num_dst_regs;
444 instruction.NumSrcRegs = num_src_regs;
445
446 header_bodysize_grow( header );
447
448 return instruction;
449 }
450
451 static void
452 instruction_grow(
453 struct tgsi_instruction *instruction,
454 struct tgsi_header *header )
455 {
456 assert (instruction->NrTokens < 0xFF);
457
458 instruction->NrTokens++;
459
460 header_bodysize_grow( header );
461 }
462
463 struct tgsi_full_instruction
464 tgsi_default_full_instruction( void )
465 {
466 struct tgsi_full_instruction full_instruction;
467 unsigned i;
468
469 full_instruction.Instruction = tgsi_default_instruction();
470 full_instruction.InstructionExtNv = tgsi_default_instruction_ext_nv();
471 full_instruction.InstructionExtLabel = tgsi_default_instruction_ext_label();
472 full_instruction.InstructionExtTexture = tgsi_default_instruction_ext_texture();
473 for( i = 0; i < TGSI_FULL_MAX_DST_REGISTERS; i++ ) {
474 full_instruction.FullDstRegisters[i] = tgsi_default_full_dst_register();
475 }
476 for( i = 0; i < TGSI_FULL_MAX_SRC_REGISTERS; i++ ) {
477 full_instruction.FullSrcRegisters[i] = tgsi_default_full_src_register();
478 }
479
480 return full_instruction;
481 }
482
483 unsigned
484 tgsi_build_full_instruction(
485 const struct tgsi_full_instruction *full_inst,
486 struct tgsi_token *tokens,
487 struct tgsi_header *header,
488 unsigned maxsize )
489 {
490 unsigned size = 0;
491 unsigned i;
492 struct tgsi_instruction *instruction;
493 struct tgsi_token *prev_token;
494
495 if( maxsize <= size )
496 return 0;
497 instruction = (struct tgsi_instruction *) &tokens[size];
498 size++;
499
500 *instruction = tgsi_build_instruction(
501 full_inst->Instruction.Opcode,
502 full_inst->Instruction.Saturate,
503 full_inst->Instruction.NumDstRegs,
504 full_inst->Instruction.NumSrcRegs,
505 header );
506 prev_token = (struct tgsi_token *) instruction;
507
508 if( tgsi_compare_instruction_ext_nv(
509 full_inst->InstructionExtNv,
510 tgsi_default_instruction_ext_nv() ) ) {
511 struct tgsi_instruction_ext_nv *instruction_ext_nv;
512
513 if( maxsize <= size )
514 return 0;
515 instruction_ext_nv =
516 (struct tgsi_instruction_ext_nv *) &tokens[size];
517 size++;
518
519 *instruction_ext_nv = tgsi_build_instruction_ext_nv(
520 full_inst->InstructionExtNv.Precision,
521 full_inst->InstructionExtNv.CondDstIndex,
522 full_inst->InstructionExtNv.CondFlowIndex,
523 full_inst->InstructionExtNv.CondMask,
524 full_inst->InstructionExtNv.CondSwizzleX,
525 full_inst->InstructionExtNv.CondSwizzleY,
526 full_inst->InstructionExtNv.CondSwizzleZ,
527 full_inst->InstructionExtNv.CondSwizzleW,
528 full_inst->InstructionExtNv.CondDstUpdate,
529 full_inst->InstructionExtNv.CondFlowEnable,
530 prev_token,
531 instruction,
532 header );
533 prev_token = (struct tgsi_token *) instruction_ext_nv;
534 }
535
536 if( tgsi_compare_instruction_ext_label(
537 full_inst->InstructionExtLabel,
538 tgsi_default_instruction_ext_label() ) ) {
539 struct tgsi_instruction_ext_label *instruction_ext_label;
540
541 if( maxsize <= size )
542 return 0;
543 instruction_ext_label =
544 (struct tgsi_instruction_ext_label *) &tokens[size];
545 size++;
546
547 *instruction_ext_label = tgsi_build_instruction_ext_label(
548 full_inst->InstructionExtLabel.Label,
549 prev_token,
550 instruction,
551 header );
552 prev_token = (struct tgsi_token *) instruction_ext_label;
553 }
554
555 if( tgsi_compare_instruction_ext_texture(
556 full_inst->InstructionExtTexture,
557 tgsi_default_instruction_ext_texture() ) ) {
558 struct tgsi_instruction_ext_texture *instruction_ext_texture;
559
560 if( maxsize <= size )
561 return 0;
562 instruction_ext_texture =
563 (struct tgsi_instruction_ext_texture *) &tokens[size];
564 size++;
565
566 *instruction_ext_texture = tgsi_build_instruction_ext_texture(
567 full_inst->InstructionExtTexture.Texture,
568 prev_token,
569 instruction,
570 header );
571 prev_token = (struct tgsi_token *) instruction_ext_texture;
572 }
573
574 for( i = 0; i < full_inst->Instruction.NumDstRegs; i++ ) {
575 const struct tgsi_full_dst_register *reg = &full_inst->FullDstRegisters[i];
576 struct tgsi_dst_register *dst_register;
577 struct tgsi_token *prev_token;
578
579 if( maxsize <= size )
580 return 0;
581 dst_register = (struct tgsi_dst_register *) &tokens[size];
582 size++;
583
584 *dst_register = tgsi_build_dst_register(
585 reg->DstRegister.File,
586 reg->DstRegister.WriteMask,
587 reg->DstRegister.Index,
588 instruction,
589 header );
590 prev_token = (struct tgsi_token *) dst_register;
591
592 if( tgsi_compare_dst_register_ext_concode(
593 reg->DstRegisterExtConcode,
594 tgsi_default_dst_register_ext_concode() ) ) {
595 struct tgsi_dst_register_ext_concode *dst_register_ext_concode;
596
597 if( maxsize <= size )
598 return 0;
599 dst_register_ext_concode =
600 (struct tgsi_dst_register_ext_concode *) &tokens[size];
601 size++;
602
603 *dst_register_ext_concode = tgsi_build_dst_register_ext_concode(
604 reg->DstRegisterExtConcode.CondMask,
605 reg->DstRegisterExtConcode.CondSwizzleX,
606 reg->DstRegisterExtConcode.CondSwizzleY,
607 reg->DstRegisterExtConcode.CondSwizzleZ,
608 reg->DstRegisterExtConcode.CondSwizzleW,
609 reg->DstRegisterExtConcode.CondSrcIndex,
610 prev_token,
611 instruction,
612 header );
613 prev_token = (struct tgsi_token *) dst_register_ext_concode;
614 }
615
616 if( tgsi_compare_dst_register_ext_modulate(
617 reg->DstRegisterExtModulate,
618 tgsi_default_dst_register_ext_modulate() ) ) {
619 struct tgsi_dst_register_ext_modulate *dst_register_ext_modulate;
620
621 if( maxsize <= size )
622 return 0;
623 dst_register_ext_modulate =
624 (struct tgsi_dst_register_ext_modulate *) &tokens[size];
625 size++;
626
627 *dst_register_ext_modulate = tgsi_build_dst_register_ext_modulate(
628 reg->DstRegisterExtModulate.Modulate,
629 prev_token,
630 instruction,
631 header );
632 prev_token = (struct tgsi_token *) dst_register_ext_modulate;
633 }
634 }
635
636 for( i = 0; i < full_inst->Instruction.NumSrcRegs; i++ ) {
637 const struct tgsi_full_src_register *reg = &full_inst->FullSrcRegisters[i];
638 struct tgsi_src_register *src_register;
639 struct tgsi_token *prev_token;
640
641 if( maxsize <= size )
642 return 0;
643 src_register = (struct tgsi_src_register *) &tokens[size];
644 size++;
645
646 *src_register = tgsi_build_src_register(
647 reg->SrcRegister.File,
648 reg->SrcRegister.SwizzleX,
649 reg->SrcRegister.SwizzleY,
650 reg->SrcRegister.SwizzleZ,
651 reg->SrcRegister.SwizzleW,
652 reg->SrcRegister.Negate,
653 reg->SrcRegister.Indirect,
654 reg->SrcRegister.Dimension,
655 reg->SrcRegister.Index,
656 instruction,
657 header );
658 prev_token = (struct tgsi_token *) src_register;
659
660 if( tgsi_compare_src_register_ext_swz(
661 reg->SrcRegisterExtSwz,
662 tgsi_default_src_register_ext_swz() ) ) {
663 struct tgsi_src_register_ext_swz *src_register_ext_swz;
664
665 /* Use of the extended swizzle requires the simple swizzle to be identity.
666 */
667 assert( reg->SrcRegister.SwizzleX == TGSI_SWIZZLE_X );
668 assert( reg->SrcRegister.SwizzleY == TGSI_SWIZZLE_Y );
669 assert( reg->SrcRegister.SwizzleZ == TGSI_SWIZZLE_Z );
670 assert( reg->SrcRegister.SwizzleW == TGSI_SWIZZLE_W );
671 assert( reg->SrcRegister.Negate == FALSE );
672
673 if( maxsize <= size )
674 return 0;
675 src_register_ext_swz =
676 (struct tgsi_src_register_ext_swz *) &tokens[size];
677 size++;
678
679 *src_register_ext_swz = tgsi_build_src_register_ext_swz(
680 reg->SrcRegisterExtSwz.ExtSwizzleX,
681 reg->SrcRegisterExtSwz.ExtSwizzleY,
682 reg->SrcRegisterExtSwz.ExtSwizzleZ,
683 reg->SrcRegisterExtSwz.ExtSwizzleW,
684 reg->SrcRegisterExtSwz.NegateX,
685 reg->SrcRegisterExtSwz.NegateY,
686 reg->SrcRegisterExtSwz.NegateZ,
687 reg->SrcRegisterExtSwz.NegateW,
688 prev_token,
689 instruction,
690 header );
691 prev_token = (struct tgsi_token *) src_register_ext_swz;
692 }
693
694 if( tgsi_compare_src_register_ext_mod(
695 reg->SrcRegisterExtMod,
696 tgsi_default_src_register_ext_mod() ) ) {
697 struct tgsi_src_register_ext_mod *src_register_ext_mod;
698
699 if( maxsize <= size )
700 return 0;
701 src_register_ext_mod =
702 (struct tgsi_src_register_ext_mod *) &tokens[size];
703 size++;
704
705 *src_register_ext_mod = tgsi_build_src_register_ext_mod(
706 reg->SrcRegisterExtMod.Complement,
707 reg->SrcRegisterExtMod.Bias,
708 reg->SrcRegisterExtMod.Scale2X,
709 reg->SrcRegisterExtMod.Absolute,
710 reg->SrcRegisterExtMod.Negate,
711 prev_token,
712 instruction,
713 header );
714 prev_token = (struct tgsi_token *) src_register_ext_mod;
715 }
716
717 if( reg->SrcRegister.Indirect ) {
718 struct tgsi_src_register *ind;
719
720 if( maxsize <= size )
721 return 0;
722 ind = (struct tgsi_src_register *) &tokens[size];
723 size++;
724
725 *ind = tgsi_build_src_register(
726 reg->SrcRegisterInd.File,
727 reg->SrcRegisterInd.SwizzleX,
728 reg->SrcRegisterInd.SwizzleY,
729 reg->SrcRegisterInd.SwizzleZ,
730 reg->SrcRegisterInd.SwizzleW,
731 reg->SrcRegisterInd.Negate,
732 reg->SrcRegisterInd.Indirect,
733 reg->SrcRegisterInd.Dimension,
734 reg->SrcRegisterInd.Index,
735 instruction,
736 header );
737 }
738
739 if( reg->SrcRegister.Dimension ) {
740 struct tgsi_dimension *dim;
741
742 assert( !reg->SrcRegisterDim.Dimension );
743
744 if( maxsize <= size )
745 return 0;
746 dim = (struct tgsi_dimension *) &tokens[size];
747 size++;
748
749 *dim = tgsi_build_dimension(
750 reg->SrcRegisterDim.Indirect,
751 reg->SrcRegisterDim.Index,
752 instruction,
753 header );
754
755 if( reg->SrcRegisterDim.Indirect ) {
756 struct tgsi_src_register *ind;
757
758 if( maxsize <= size )
759 return 0;
760 ind = (struct tgsi_src_register *) &tokens[size];
761 size++;
762
763 *ind = tgsi_build_src_register(
764 reg->SrcRegisterDimInd.File,
765 reg->SrcRegisterDimInd.SwizzleX,
766 reg->SrcRegisterDimInd.SwizzleY,
767 reg->SrcRegisterDimInd.SwizzleZ,
768 reg->SrcRegisterDimInd.SwizzleW,
769 reg->SrcRegisterDimInd.Negate,
770 reg->SrcRegisterDimInd.Indirect,
771 reg->SrcRegisterDimInd.Dimension,
772 reg->SrcRegisterDimInd.Index,
773 instruction,
774 header );
775 }
776 }
777 }
778
779 return size;
780 }
781
782 struct tgsi_instruction_ext_nv
783 tgsi_default_instruction_ext_nv( void )
784 {
785 struct tgsi_instruction_ext_nv instruction_ext_nv;
786
787 instruction_ext_nv.Type = TGSI_INSTRUCTION_EXT_TYPE_NV;
788 instruction_ext_nv.Precision = TGSI_PRECISION_DEFAULT;
789 instruction_ext_nv.CondDstIndex = 0;
790 instruction_ext_nv.CondFlowIndex = 0;
791 instruction_ext_nv.CondMask = TGSI_CC_TR;
792 instruction_ext_nv.CondSwizzleX = TGSI_SWIZZLE_X;
793 instruction_ext_nv.CondSwizzleY = TGSI_SWIZZLE_Y;
794 instruction_ext_nv.CondSwizzleZ = TGSI_SWIZZLE_Z;
795 instruction_ext_nv.CondSwizzleW = TGSI_SWIZZLE_W;
796 instruction_ext_nv.CondDstUpdate = 0;
797 instruction_ext_nv.CondFlowEnable = 0;
798 instruction_ext_nv.Padding = 0;
799 instruction_ext_nv.Extended = 0;
800
801 return instruction_ext_nv;
802 }
803
804
805 /** test for inequality of 32-bit values pointed to by a and b */
806 static INLINE boolean
807 compare32(const void *a, const void *b)
808 {
809 return *((uint32_t *) a) != *((uint32_t *) b);
810 }
811
812
813 unsigned
814 tgsi_compare_instruction_ext_nv(
815 struct tgsi_instruction_ext_nv a,
816 struct tgsi_instruction_ext_nv b )
817 {
818 a.Padding = b.Padding = 0;
819 a.Extended = b.Extended = 0;
820 return compare32(&a, &b);
821 }
822
823 struct tgsi_instruction_ext_nv
824 tgsi_build_instruction_ext_nv(
825 unsigned precision,
826 unsigned cond_dst_index,
827 unsigned cond_flow_index,
828 unsigned cond_mask,
829 unsigned cond_swizzle_x,
830 unsigned cond_swizzle_y,
831 unsigned cond_swizzle_z,
832 unsigned cond_swizzle_w,
833 unsigned cond_dst_update,
834 unsigned cond_flow_enable,
835 struct tgsi_token *prev_token,
836 struct tgsi_instruction *instruction,
837 struct tgsi_header *header )
838 {
839 struct tgsi_instruction_ext_nv instruction_ext_nv;
840
841 instruction_ext_nv = tgsi_default_instruction_ext_nv();
842 instruction_ext_nv.Precision = precision;
843 instruction_ext_nv.CondDstIndex = cond_dst_index;
844 instruction_ext_nv.CondFlowIndex = cond_flow_index;
845 instruction_ext_nv.CondMask = cond_mask;
846 instruction_ext_nv.CondSwizzleX = cond_swizzle_x;
847 instruction_ext_nv.CondSwizzleY = cond_swizzle_y;
848 instruction_ext_nv.CondSwizzleZ = cond_swizzle_z;
849 instruction_ext_nv.CondSwizzleW = cond_swizzle_w;
850 instruction_ext_nv.CondDstUpdate = cond_dst_update;
851 instruction_ext_nv.CondFlowEnable = cond_flow_enable;
852
853 prev_token->Extended = 1;
854 instruction_grow( instruction, header );
855
856 return instruction_ext_nv;
857 }
858
859 struct tgsi_instruction_ext_label
860 tgsi_default_instruction_ext_label( void )
861 {
862 struct tgsi_instruction_ext_label instruction_ext_label;
863
864 instruction_ext_label.Type = TGSI_INSTRUCTION_EXT_TYPE_LABEL;
865 instruction_ext_label.Label = 0;
866 instruction_ext_label.Padding = 0;
867 instruction_ext_label.Extended = 0;
868
869 return instruction_ext_label;
870 }
871
872 unsigned
873 tgsi_compare_instruction_ext_label(
874 struct tgsi_instruction_ext_label a,
875 struct tgsi_instruction_ext_label b )
876 {
877 a.Padding = b.Padding = 0;
878 a.Extended = b.Extended = 0;
879 return compare32(&a, &b);
880 }
881
882 struct tgsi_instruction_ext_label
883 tgsi_build_instruction_ext_label(
884 unsigned label,
885 struct tgsi_token *prev_token,
886 struct tgsi_instruction *instruction,
887 struct tgsi_header *header )
888 {
889 struct tgsi_instruction_ext_label instruction_ext_label;
890
891 instruction_ext_label = tgsi_default_instruction_ext_label();
892 instruction_ext_label.Label = label;
893
894 prev_token->Extended = 1;
895 instruction_grow( instruction, header );
896
897 return instruction_ext_label;
898 }
899
900 struct tgsi_instruction_ext_texture
901 tgsi_default_instruction_ext_texture( void )
902 {
903 struct tgsi_instruction_ext_texture instruction_ext_texture;
904
905 instruction_ext_texture.Type = TGSI_INSTRUCTION_EXT_TYPE_TEXTURE;
906 instruction_ext_texture.Texture = TGSI_TEXTURE_UNKNOWN;
907 instruction_ext_texture.Padding = 0;
908 instruction_ext_texture.Extended = 0;
909
910 return instruction_ext_texture;
911 }
912
913 unsigned
914 tgsi_compare_instruction_ext_texture(
915 struct tgsi_instruction_ext_texture a,
916 struct tgsi_instruction_ext_texture b )
917 {
918 a.Padding = b.Padding = 0;
919 a.Extended = b.Extended = 0;
920 return compare32(&a, &b);
921 }
922
923 struct tgsi_instruction_ext_texture
924 tgsi_build_instruction_ext_texture(
925 unsigned texture,
926 struct tgsi_token *prev_token,
927 struct tgsi_instruction *instruction,
928 struct tgsi_header *header )
929 {
930 struct tgsi_instruction_ext_texture instruction_ext_texture;
931
932 instruction_ext_texture = tgsi_default_instruction_ext_texture();
933 instruction_ext_texture.Texture = texture;
934
935 prev_token->Extended = 1;
936 instruction_grow( instruction, header );
937
938 return instruction_ext_texture;
939 }
940
941 struct tgsi_src_register
942 tgsi_default_src_register( void )
943 {
944 struct tgsi_src_register src_register;
945
946 src_register.File = TGSI_FILE_NULL;
947 src_register.SwizzleX = TGSI_SWIZZLE_X;
948 src_register.SwizzleY = TGSI_SWIZZLE_Y;
949 src_register.SwizzleZ = TGSI_SWIZZLE_Z;
950 src_register.SwizzleW = TGSI_SWIZZLE_W;
951 src_register.Negate = 0;
952 src_register.Indirect = 0;
953 src_register.Dimension = 0;
954 src_register.Index = 0;
955 src_register.Extended = 0;
956
957 return src_register;
958 }
959
960 struct tgsi_src_register
961 tgsi_build_src_register(
962 unsigned file,
963 unsigned swizzle_x,
964 unsigned swizzle_y,
965 unsigned swizzle_z,
966 unsigned swizzle_w,
967 unsigned negate,
968 unsigned indirect,
969 unsigned dimension,
970 int index,
971 struct tgsi_instruction *instruction,
972 struct tgsi_header *header )
973 {
974 struct tgsi_src_register src_register;
975
976 assert( file <= TGSI_FILE_IMMEDIATE );
977 assert( swizzle_x <= TGSI_SWIZZLE_W );
978 assert( swizzle_y <= TGSI_SWIZZLE_W );
979 assert( swizzle_z <= TGSI_SWIZZLE_W );
980 assert( swizzle_w <= TGSI_SWIZZLE_W );
981 assert( negate <= 1 );
982 assert( index >= -0x8000 && index <= 0x7FFF );
983
984 src_register = tgsi_default_src_register();
985 src_register.File = file;
986 src_register.SwizzleX = swizzle_x;
987 src_register.SwizzleY = swizzle_y;
988 src_register.SwizzleZ = swizzle_z;
989 src_register.SwizzleW = swizzle_w;
990 src_register.Negate = negate;
991 src_register.Indirect = indirect;
992 src_register.Dimension = dimension;
993 src_register.Index = index;
994
995 instruction_grow( instruction, header );
996
997 return src_register;
998 }
999
1000 struct tgsi_full_src_register
1001 tgsi_default_full_src_register( void )
1002 {
1003 struct tgsi_full_src_register full_src_register;
1004
1005 full_src_register.SrcRegister = tgsi_default_src_register();
1006 full_src_register.SrcRegisterExtSwz = tgsi_default_src_register_ext_swz();
1007 full_src_register.SrcRegisterExtMod = tgsi_default_src_register_ext_mod();
1008 full_src_register.SrcRegisterInd = tgsi_default_src_register();
1009 full_src_register.SrcRegisterDim = tgsi_default_dimension();
1010 full_src_register.SrcRegisterDimInd = tgsi_default_src_register();
1011
1012 return full_src_register;
1013 }
1014
1015 struct tgsi_src_register_ext_swz
1016 tgsi_default_src_register_ext_swz( void )
1017 {
1018 struct tgsi_src_register_ext_swz src_register_ext_swz;
1019
1020 src_register_ext_swz.Type = TGSI_SRC_REGISTER_EXT_TYPE_SWZ;
1021 src_register_ext_swz.ExtSwizzleX = TGSI_EXTSWIZZLE_X;
1022 src_register_ext_swz.ExtSwizzleY = TGSI_EXTSWIZZLE_Y;
1023 src_register_ext_swz.ExtSwizzleZ = TGSI_EXTSWIZZLE_Z;
1024 src_register_ext_swz.ExtSwizzleW = TGSI_EXTSWIZZLE_W;
1025 src_register_ext_swz.NegateX = 0;
1026 src_register_ext_swz.NegateY = 0;
1027 src_register_ext_swz.NegateZ = 0;
1028 src_register_ext_swz.NegateW = 0;
1029 src_register_ext_swz.Padding = 0;
1030 src_register_ext_swz.Extended = 0;
1031
1032 return src_register_ext_swz;
1033 }
1034
1035 unsigned
1036 tgsi_compare_src_register_ext_swz(
1037 struct tgsi_src_register_ext_swz a,
1038 struct tgsi_src_register_ext_swz b )
1039 {
1040 a.Padding = b.Padding = 0;
1041 a.Extended = b.Extended = 0;
1042 return compare32(&a, &b);
1043 }
1044
1045 struct tgsi_src_register_ext_swz
1046 tgsi_build_src_register_ext_swz(
1047 unsigned ext_swizzle_x,
1048 unsigned ext_swizzle_y,
1049 unsigned ext_swizzle_z,
1050 unsigned ext_swizzle_w,
1051 unsigned negate_x,
1052 unsigned negate_y,
1053 unsigned negate_z,
1054 unsigned negate_w,
1055 struct tgsi_token *prev_token,
1056 struct tgsi_instruction *instruction,
1057 struct tgsi_header *header )
1058 {
1059 struct tgsi_src_register_ext_swz src_register_ext_swz;
1060
1061 assert( ext_swizzle_x <= TGSI_EXTSWIZZLE_ONE );
1062 assert( ext_swizzle_y <= TGSI_EXTSWIZZLE_ONE );
1063 assert( ext_swizzle_z <= TGSI_EXTSWIZZLE_ONE );
1064 assert( ext_swizzle_w <= TGSI_EXTSWIZZLE_ONE );
1065 assert( negate_x <= 1 );
1066 assert( negate_y <= 1 );
1067 assert( negate_z <= 1 );
1068 assert( negate_w <= 1 );
1069
1070 src_register_ext_swz = tgsi_default_src_register_ext_swz();
1071 src_register_ext_swz.ExtSwizzleX = ext_swizzle_x;
1072 src_register_ext_swz.ExtSwizzleY = ext_swizzle_y;
1073 src_register_ext_swz.ExtSwizzleZ = ext_swizzle_z;
1074 src_register_ext_swz.ExtSwizzleW = ext_swizzle_w;
1075 src_register_ext_swz.NegateX = negate_x;
1076 src_register_ext_swz.NegateY = negate_y;
1077 src_register_ext_swz.NegateZ = negate_z;
1078 src_register_ext_swz.NegateW = negate_w;
1079
1080 prev_token->Extended = 1;
1081 instruction_grow( instruction, header );
1082
1083 return src_register_ext_swz;
1084 }
1085
1086 struct tgsi_src_register_ext_mod
1087 tgsi_default_src_register_ext_mod( void )
1088 {
1089 struct tgsi_src_register_ext_mod src_register_ext_mod;
1090
1091 src_register_ext_mod.Type = TGSI_SRC_REGISTER_EXT_TYPE_MOD;
1092 src_register_ext_mod.Complement = 0;
1093 src_register_ext_mod.Bias = 0;
1094 src_register_ext_mod.Scale2X = 0;
1095 src_register_ext_mod.Absolute = 0;
1096 src_register_ext_mod.Negate = 0;
1097 src_register_ext_mod.Padding = 0;
1098 src_register_ext_mod.Extended = 0;
1099
1100 return src_register_ext_mod;
1101 }
1102
1103 unsigned
1104 tgsi_compare_src_register_ext_mod(
1105 struct tgsi_src_register_ext_mod a,
1106 struct tgsi_src_register_ext_mod b )
1107 {
1108 a.Padding = b.Padding = 0;
1109 a.Extended = b.Extended = 0;
1110 return compare32(&a, &b);
1111 }
1112
1113 struct tgsi_src_register_ext_mod
1114 tgsi_build_src_register_ext_mod(
1115 unsigned complement,
1116 unsigned bias,
1117 unsigned scale_2x,
1118 unsigned absolute,
1119 unsigned negate,
1120 struct tgsi_token *prev_token,
1121 struct tgsi_instruction *instruction,
1122 struct tgsi_header *header )
1123 {
1124 struct tgsi_src_register_ext_mod src_register_ext_mod;
1125
1126 assert( complement <= 1 );
1127 assert( bias <= 1 );
1128 assert( scale_2x <= 1 );
1129 assert( absolute <= 1 );
1130 assert( negate <= 1 );
1131
1132 src_register_ext_mod = tgsi_default_src_register_ext_mod();
1133 src_register_ext_mod.Complement = complement;
1134 src_register_ext_mod.Bias = bias;
1135 src_register_ext_mod.Scale2X = scale_2x;
1136 src_register_ext_mod.Absolute = absolute;
1137 src_register_ext_mod.Negate = negate;
1138
1139 prev_token->Extended = 1;
1140 instruction_grow( instruction, header );
1141
1142 return src_register_ext_mod;
1143 }
1144
1145 struct tgsi_dimension
1146 tgsi_default_dimension( void )
1147 {
1148 struct tgsi_dimension dimension;
1149
1150 dimension.Indirect = 0;
1151 dimension.Dimension = 0;
1152 dimension.Padding = 0;
1153 dimension.Index = 0;
1154 dimension.Extended = 0;
1155
1156 return dimension;
1157 }
1158
1159 struct tgsi_dimension
1160 tgsi_build_dimension(
1161 unsigned indirect,
1162 unsigned index,
1163 struct tgsi_instruction *instruction,
1164 struct tgsi_header *header )
1165 {
1166 struct tgsi_dimension dimension;
1167
1168 dimension = tgsi_default_dimension();
1169 dimension.Indirect = indirect;
1170 dimension.Index = index;
1171
1172 instruction_grow( instruction, header );
1173
1174 return dimension;
1175 }
1176
1177 struct tgsi_dst_register
1178 tgsi_default_dst_register( void )
1179 {
1180 struct tgsi_dst_register dst_register;
1181
1182 dst_register.File = TGSI_FILE_NULL;
1183 dst_register.WriteMask = TGSI_WRITEMASK_XYZW;
1184 dst_register.Indirect = 0;
1185 dst_register.Dimension = 0;
1186 dst_register.Index = 0;
1187 dst_register.Padding = 0;
1188 dst_register.Extended = 0;
1189
1190 return dst_register;
1191 }
1192
1193 struct tgsi_dst_register
1194 tgsi_build_dst_register(
1195 unsigned file,
1196 unsigned mask,
1197 int index,
1198 struct tgsi_instruction *instruction,
1199 struct tgsi_header *header )
1200 {
1201 struct tgsi_dst_register dst_register;
1202
1203 assert( file <= TGSI_FILE_IMMEDIATE );
1204 assert( mask <= TGSI_WRITEMASK_XYZW );
1205 assert( index >= -32768 && index <= 32767 );
1206
1207 dst_register = tgsi_default_dst_register();
1208 dst_register.File = file;
1209 dst_register.WriteMask = mask;
1210 dst_register.Index = index;
1211
1212 instruction_grow( instruction, header );
1213
1214 return dst_register;
1215 }
1216
1217 struct tgsi_full_dst_register
1218 tgsi_default_full_dst_register( void )
1219 {
1220 struct tgsi_full_dst_register full_dst_register;
1221
1222 full_dst_register.DstRegister = tgsi_default_dst_register();
1223 full_dst_register.DstRegisterExtConcode =
1224 tgsi_default_dst_register_ext_concode();
1225 full_dst_register.DstRegisterExtModulate =
1226 tgsi_default_dst_register_ext_modulate();
1227
1228 return full_dst_register;
1229 }
1230
1231 struct tgsi_dst_register_ext_concode
1232 tgsi_default_dst_register_ext_concode( void )
1233 {
1234 struct tgsi_dst_register_ext_concode dst_register_ext_concode;
1235
1236 dst_register_ext_concode.Type = TGSI_DST_REGISTER_EXT_TYPE_CONDCODE;
1237 dst_register_ext_concode.CondMask = TGSI_CC_TR;
1238 dst_register_ext_concode.CondSwizzleX = TGSI_SWIZZLE_X;
1239 dst_register_ext_concode.CondSwizzleY = TGSI_SWIZZLE_Y;
1240 dst_register_ext_concode.CondSwizzleZ = TGSI_SWIZZLE_Z;
1241 dst_register_ext_concode.CondSwizzleW = TGSI_SWIZZLE_W;
1242 dst_register_ext_concode.CondSrcIndex = 0;
1243 dst_register_ext_concode.Padding = 0;
1244 dst_register_ext_concode.Extended = 0;
1245
1246 return dst_register_ext_concode;
1247 }
1248
1249 unsigned
1250 tgsi_compare_dst_register_ext_concode(
1251 struct tgsi_dst_register_ext_concode a,
1252 struct tgsi_dst_register_ext_concode b )
1253 {
1254 a.Padding = b.Padding = 0;
1255 a.Extended = b.Extended = 0;
1256 return compare32(&a, &b);
1257 }
1258
1259 struct tgsi_dst_register_ext_concode
1260 tgsi_build_dst_register_ext_concode(
1261 unsigned cc,
1262 unsigned swizzle_x,
1263 unsigned swizzle_y,
1264 unsigned swizzle_z,
1265 unsigned swizzle_w,
1266 int index,
1267 struct tgsi_token *prev_token,
1268 struct tgsi_instruction *instruction,
1269 struct tgsi_header *header )
1270 {
1271 struct tgsi_dst_register_ext_concode dst_register_ext_concode;
1272
1273 assert( cc <= TGSI_CC_FL );
1274 assert( swizzle_x <= TGSI_SWIZZLE_W );
1275 assert( swizzle_y <= TGSI_SWIZZLE_W );
1276 assert( swizzle_z <= TGSI_SWIZZLE_W );
1277 assert( swizzle_w <= TGSI_SWIZZLE_W );
1278 assert( index >= -32768 && index <= 32767 );
1279
1280 dst_register_ext_concode = tgsi_default_dst_register_ext_concode();
1281 dst_register_ext_concode.CondMask = cc;
1282 dst_register_ext_concode.CondSwizzleX = swizzle_x;
1283 dst_register_ext_concode.CondSwizzleY = swizzle_y;
1284 dst_register_ext_concode.CondSwizzleZ = swizzle_z;
1285 dst_register_ext_concode.CondSwizzleW = swizzle_w;
1286 dst_register_ext_concode.CondSrcIndex = index;
1287
1288 prev_token->Extended = 1;
1289 instruction_grow( instruction, header );
1290
1291 return dst_register_ext_concode;
1292 }
1293
1294 struct tgsi_dst_register_ext_modulate
1295 tgsi_default_dst_register_ext_modulate( void )
1296 {
1297 struct tgsi_dst_register_ext_modulate dst_register_ext_modulate;
1298
1299 dst_register_ext_modulate.Type = TGSI_DST_REGISTER_EXT_TYPE_MODULATE;
1300 dst_register_ext_modulate.Modulate = TGSI_MODULATE_1X;
1301 dst_register_ext_modulate.Padding = 0;
1302 dst_register_ext_modulate.Extended = 0;
1303
1304 return dst_register_ext_modulate;
1305 }
1306
1307 unsigned
1308 tgsi_compare_dst_register_ext_modulate(
1309 struct tgsi_dst_register_ext_modulate a,
1310 struct tgsi_dst_register_ext_modulate b )
1311 {
1312 a.Padding = b.Padding = 0;
1313 a.Extended = b.Extended = 0;
1314 return compare32(&a, &b);
1315 }
1316
1317 struct tgsi_dst_register_ext_modulate
1318 tgsi_build_dst_register_ext_modulate(
1319 unsigned modulate,
1320 struct tgsi_token *prev_token,
1321 struct tgsi_instruction *instruction,
1322 struct tgsi_header *header )
1323 {
1324 struct tgsi_dst_register_ext_modulate dst_register_ext_modulate;
1325
1326 assert( modulate <= TGSI_MODULATE_EIGHTH );
1327
1328 dst_register_ext_modulate = tgsi_default_dst_register_ext_modulate();
1329 dst_register_ext_modulate.Modulate = modulate;
1330
1331 prev_token->Extended = 1;
1332 instruction_grow( instruction, header );
1333
1334 return dst_register_ext_modulate;
1335 }