tgsi: fix regression in indexed const lookups
[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.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.Indirect,
588 reg->DstRegister.Index,
589 instruction,
590 header );
591 prev_token = (struct tgsi_token *) dst_register;
592
593 if( tgsi_compare_dst_register_ext_concode(
594 reg->DstRegisterExtConcode,
595 tgsi_default_dst_register_ext_concode() ) ) {
596 struct tgsi_dst_register_ext_concode *dst_register_ext_concode;
597
598 if( maxsize <= size )
599 return 0;
600 dst_register_ext_concode =
601 (struct tgsi_dst_register_ext_concode *) &tokens[size];
602 size++;
603
604 *dst_register_ext_concode = tgsi_build_dst_register_ext_concode(
605 reg->DstRegisterExtConcode.CondMask,
606 reg->DstRegisterExtConcode.CondSwizzleX,
607 reg->DstRegisterExtConcode.CondSwizzleY,
608 reg->DstRegisterExtConcode.CondSwizzleZ,
609 reg->DstRegisterExtConcode.CondSwizzleW,
610 reg->DstRegisterExtConcode.CondSrcIndex,
611 prev_token,
612 instruction,
613 header );
614 prev_token = (struct tgsi_token *) dst_register_ext_concode;
615 }
616
617 if( tgsi_compare_dst_register_ext_modulate(
618 reg->DstRegisterExtModulate,
619 tgsi_default_dst_register_ext_modulate() ) ) {
620 struct tgsi_dst_register_ext_modulate *dst_register_ext_modulate;
621
622 if( maxsize <= size )
623 return 0;
624 dst_register_ext_modulate =
625 (struct tgsi_dst_register_ext_modulate *) &tokens[size];
626 size++;
627
628 *dst_register_ext_modulate = tgsi_build_dst_register_ext_modulate(
629 reg->DstRegisterExtModulate.Modulate,
630 prev_token,
631 instruction,
632 header );
633 prev_token = (struct tgsi_token *) dst_register_ext_modulate;
634 }
635
636 if( reg->DstRegister.Indirect ) {
637 struct tgsi_src_register *ind;
638
639 if( maxsize <= size )
640 return 0;
641 ind = (struct tgsi_src_register *) &tokens[size];
642 size++;
643
644 *ind = tgsi_build_src_register(
645 reg->DstRegisterInd.File,
646 reg->DstRegisterInd.SwizzleX,
647 reg->DstRegisterInd.SwizzleY,
648 reg->DstRegisterInd.SwizzleZ,
649 reg->DstRegisterInd.SwizzleW,
650 reg->DstRegisterInd.Negate,
651 reg->DstRegisterInd.Indirect,
652 reg->DstRegisterInd.Dimension,
653 reg->DstRegisterInd.Index,
654 instruction,
655 header );
656 }
657 }
658
659 for( i = 0; i < full_inst->Instruction.NumSrcRegs; i++ ) {
660 const struct tgsi_full_src_register *reg = &full_inst->FullSrcRegisters[i];
661 struct tgsi_src_register *src_register;
662 struct tgsi_token *prev_token;
663
664 if( maxsize <= size )
665 return 0;
666 src_register = (struct tgsi_src_register *) &tokens[size];
667 size++;
668
669 *src_register = tgsi_build_src_register(
670 reg->SrcRegister.File,
671 reg->SrcRegister.SwizzleX,
672 reg->SrcRegister.SwizzleY,
673 reg->SrcRegister.SwizzleZ,
674 reg->SrcRegister.SwizzleW,
675 reg->SrcRegister.Negate,
676 reg->SrcRegister.Indirect,
677 reg->SrcRegister.Dimension,
678 reg->SrcRegister.Index,
679 instruction,
680 header );
681 prev_token = (struct tgsi_token *) src_register;
682
683 if( tgsi_compare_src_register_ext_swz(
684 reg->SrcRegisterExtSwz,
685 tgsi_default_src_register_ext_swz() ) ) {
686 struct tgsi_src_register_ext_swz *src_register_ext_swz;
687
688 /* Use of the extended swizzle requires the simple swizzle to be identity.
689 */
690 assert( reg->SrcRegister.SwizzleX == TGSI_SWIZZLE_X );
691 assert( reg->SrcRegister.SwizzleY == TGSI_SWIZZLE_Y );
692 assert( reg->SrcRegister.SwizzleZ == TGSI_SWIZZLE_Z );
693 assert( reg->SrcRegister.SwizzleW == TGSI_SWIZZLE_W );
694 assert( reg->SrcRegister.Negate == FALSE );
695
696 if( maxsize <= size )
697 return 0;
698 src_register_ext_swz =
699 (struct tgsi_src_register_ext_swz *) &tokens[size];
700 size++;
701
702 *src_register_ext_swz = tgsi_build_src_register_ext_swz(
703 reg->SrcRegisterExtSwz.ExtSwizzleX,
704 reg->SrcRegisterExtSwz.ExtSwizzleY,
705 reg->SrcRegisterExtSwz.ExtSwizzleZ,
706 reg->SrcRegisterExtSwz.ExtSwizzleW,
707 reg->SrcRegisterExtSwz.NegateX,
708 reg->SrcRegisterExtSwz.NegateY,
709 reg->SrcRegisterExtSwz.NegateZ,
710 reg->SrcRegisterExtSwz.NegateW,
711 prev_token,
712 instruction,
713 header );
714 prev_token = (struct tgsi_token *) src_register_ext_swz;
715 }
716
717 if( tgsi_compare_src_register_ext_mod(
718 reg->SrcRegisterExtMod,
719 tgsi_default_src_register_ext_mod() ) ) {
720 struct tgsi_src_register_ext_mod *src_register_ext_mod;
721
722 if( maxsize <= size )
723 return 0;
724 src_register_ext_mod =
725 (struct tgsi_src_register_ext_mod *) &tokens[size];
726 size++;
727
728 *src_register_ext_mod = tgsi_build_src_register_ext_mod(
729 reg->SrcRegisterExtMod.Complement,
730 reg->SrcRegisterExtMod.Bias,
731 reg->SrcRegisterExtMod.Scale2X,
732 reg->SrcRegisterExtMod.Absolute,
733 reg->SrcRegisterExtMod.Negate,
734 prev_token,
735 instruction,
736 header );
737 prev_token = (struct tgsi_token *) src_register_ext_mod;
738 }
739
740 if( reg->SrcRegister.Indirect ) {
741 struct tgsi_src_register *ind;
742
743 if( maxsize <= size )
744 return 0;
745 ind = (struct tgsi_src_register *) &tokens[size];
746 size++;
747
748 *ind = tgsi_build_src_register(
749 reg->SrcRegisterInd.File,
750 reg->SrcRegisterInd.SwizzleX,
751 reg->SrcRegisterInd.SwizzleY,
752 reg->SrcRegisterInd.SwizzleZ,
753 reg->SrcRegisterInd.SwizzleW,
754 reg->SrcRegisterInd.Negate,
755 reg->SrcRegisterInd.Indirect,
756 reg->SrcRegisterInd.Dimension,
757 reg->SrcRegisterInd.Index,
758 instruction,
759 header );
760 }
761
762 if( reg->SrcRegister.Dimension ) {
763 struct tgsi_dimension *dim;
764
765 assert( !reg->SrcRegisterDim.Dimension );
766
767 if( maxsize <= size )
768 return 0;
769 dim = (struct tgsi_dimension *) &tokens[size];
770 size++;
771
772 *dim = tgsi_build_dimension(
773 reg->SrcRegisterDim.Indirect,
774 reg->SrcRegisterDim.Index,
775 instruction,
776 header );
777
778 if( reg->SrcRegisterDim.Indirect ) {
779 struct tgsi_src_register *ind;
780
781 if( maxsize <= size )
782 return 0;
783 ind = (struct tgsi_src_register *) &tokens[size];
784 size++;
785
786 *ind = tgsi_build_src_register(
787 reg->SrcRegisterDimInd.File,
788 reg->SrcRegisterDimInd.SwizzleX,
789 reg->SrcRegisterDimInd.SwizzleY,
790 reg->SrcRegisterDimInd.SwizzleZ,
791 reg->SrcRegisterDimInd.SwizzleW,
792 reg->SrcRegisterDimInd.Negate,
793 reg->SrcRegisterDimInd.Indirect,
794 reg->SrcRegisterDimInd.Dimension,
795 reg->SrcRegisterDimInd.Index,
796 instruction,
797 header );
798 }
799 }
800 }
801
802 return size;
803 }
804
805 struct tgsi_instruction_ext_nv
806 tgsi_default_instruction_ext_nv( void )
807 {
808 struct tgsi_instruction_ext_nv instruction_ext_nv;
809
810 instruction_ext_nv.Type = TGSI_INSTRUCTION_EXT_TYPE_NV;
811 instruction_ext_nv.Precision = TGSI_PRECISION_DEFAULT;
812 instruction_ext_nv.CondDstIndex = 0;
813 instruction_ext_nv.CondFlowIndex = 0;
814 instruction_ext_nv.CondMask = TGSI_CC_TR;
815 instruction_ext_nv.CondSwizzleX = TGSI_SWIZZLE_X;
816 instruction_ext_nv.CondSwizzleY = TGSI_SWIZZLE_Y;
817 instruction_ext_nv.CondSwizzleZ = TGSI_SWIZZLE_Z;
818 instruction_ext_nv.CondSwizzleW = TGSI_SWIZZLE_W;
819 instruction_ext_nv.CondDstUpdate = 0;
820 instruction_ext_nv.CondFlowEnable = 0;
821 instruction_ext_nv.Padding = 0;
822 instruction_ext_nv.Extended = 0;
823
824 return instruction_ext_nv;
825 }
826
827
828 /** test for inequality of 32-bit values pointed to by a and b */
829 static INLINE boolean
830 compare32(const void *a, const void *b)
831 {
832 return *((uint32_t *) a) != *((uint32_t *) b);
833 }
834
835
836 unsigned
837 tgsi_compare_instruction_ext_nv(
838 struct tgsi_instruction_ext_nv a,
839 struct tgsi_instruction_ext_nv b )
840 {
841 a.Padding = b.Padding = 0;
842 a.Extended = b.Extended = 0;
843 return compare32(&a, &b);
844 }
845
846 struct tgsi_instruction_ext_nv
847 tgsi_build_instruction_ext_nv(
848 unsigned precision,
849 unsigned cond_dst_index,
850 unsigned cond_flow_index,
851 unsigned cond_mask,
852 unsigned cond_swizzle_x,
853 unsigned cond_swizzle_y,
854 unsigned cond_swizzle_z,
855 unsigned cond_swizzle_w,
856 unsigned cond_dst_update,
857 unsigned cond_flow_enable,
858 struct tgsi_token *prev_token,
859 struct tgsi_instruction *instruction,
860 struct tgsi_header *header )
861 {
862 struct tgsi_instruction_ext_nv instruction_ext_nv;
863
864 instruction_ext_nv = tgsi_default_instruction_ext_nv();
865 instruction_ext_nv.Precision = precision;
866 instruction_ext_nv.CondDstIndex = cond_dst_index;
867 instruction_ext_nv.CondFlowIndex = cond_flow_index;
868 instruction_ext_nv.CondMask = cond_mask;
869 instruction_ext_nv.CondSwizzleX = cond_swizzle_x;
870 instruction_ext_nv.CondSwizzleY = cond_swizzle_y;
871 instruction_ext_nv.CondSwizzleZ = cond_swizzle_z;
872 instruction_ext_nv.CondSwizzleW = cond_swizzle_w;
873 instruction_ext_nv.CondDstUpdate = cond_dst_update;
874 instruction_ext_nv.CondFlowEnable = cond_flow_enable;
875
876 prev_token->Extended = 1;
877 instruction_grow( instruction, header );
878
879 return instruction_ext_nv;
880 }
881
882 struct tgsi_instruction_ext_label
883 tgsi_default_instruction_ext_label( void )
884 {
885 struct tgsi_instruction_ext_label instruction_ext_label;
886
887 instruction_ext_label.Type = TGSI_INSTRUCTION_EXT_TYPE_LABEL;
888 instruction_ext_label.Label = 0;
889 instruction_ext_label.Padding = 0;
890 instruction_ext_label.Extended = 0;
891
892 return instruction_ext_label;
893 }
894
895 unsigned
896 tgsi_compare_instruction_ext_label(
897 struct tgsi_instruction_ext_label a,
898 struct tgsi_instruction_ext_label b )
899 {
900 a.Padding = b.Padding = 0;
901 a.Extended = b.Extended = 0;
902 return compare32(&a, &b);
903 }
904
905 struct tgsi_instruction_ext_label
906 tgsi_build_instruction_ext_label(
907 unsigned label,
908 struct tgsi_token *prev_token,
909 struct tgsi_instruction *instruction,
910 struct tgsi_header *header )
911 {
912 struct tgsi_instruction_ext_label instruction_ext_label;
913
914 instruction_ext_label = tgsi_default_instruction_ext_label();
915 instruction_ext_label.Label = label;
916
917 prev_token->Extended = 1;
918 instruction_grow( instruction, header );
919
920 return instruction_ext_label;
921 }
922
923 struct tgsi_instruction_ext_texture
924 tgsi_default_instruction_ext_texture( void )
925 {
926 struct tgsi_instruction_ext_texture instruction_ext_texture;
927
928 instruction_ext_texture.Type = TGSI_INSTRUCTION_EXT_TYPE_TEXTURE;
929 instruction_ext_texture.Texture = TGSI_TEXTURE_UNKNOWN;
930 instruction_ext_texture.Padding = 0;
931 instruction_ext_texture.Extended = 0;
932
933 return instruction_ext_texture;
934 }
935
936 unsigned
937 tgsi_compare_instruction_ext_texture(
938 struct tgsi_instruction_ext_texture a,
939 struct tgsi_instruction_ext_texture b )
940 {
941 a.Padding = b.Padding = 0;
942 a.Extended = b.Extended = 0;
943 return compare32(&a, &b);
944 }
945
946 struct tgsi_instruction_ext_texture
947 tgsi_build_instruction_ext_texture(
948 unsigned texture,
949 struct tgsi_token *prev_token,
950 struct tgsi_instruction *instruction,
951 struct tgsi_header *header )
952 {
953 struct tgsi_instruction_ext_texture instruction_ext_texture;
954
955 instruction_ext_texture = tgsi_default_instruction_ext_texture();
956 instruction_ext_texture.Texture = texture;
957
958 prev_token->Extended = 1;
959 instruction_grow( instruction, header );
960
961 return instruction_ext_texture;
962 }
963
964 struct tgsi_src_register
965 tgsi_default_src_register( void )
966 {
967 struct tgsi_src_register src_register;
968
969 src_register.File = TGSI_FILE_NULL;
970 src_register.SwizzleX = TGSI_SWIZZLE_X;
971 src_register.SwizzleY = TGSI_SWIZZLE_Y;
972 src_register.SwizzleZ = TGSI_SWIZZLE_Z;
973 src_register.SwizzleW = TGSI_SWIZZLE_W;
974 src_register.Negate = 0;
975 src_register.Indirect = 0;
976 src_register.Dimension = 0;
977 src_register.Index = 0;
978 src_register.Extended = 0;
979
980 return src_register;
981 }
982
983 struct tgsi_src_register
984 tgsi_build_src_register(
985 unsigned file,
986 unsigned swizzle_x,
987 unsigned swizzle_y,
988 unsigned swizzle_z,
989 unsigned swizzle_w,
990 unsigned negate,
991 unsigned indirect,
992 unsigned dimension,
993 int index,
994 struct tgsi_instruction *instruction,
995 struct tgsi_header *header )
996 {
997 struct tgsi_src_register src_register;
998
999 assert( file < TGSI_FILE_COUNT );
1000 assert( swizzle_x <= TGSI_SWIZZLE_W );
1001 assert( swizzle_y <= TGSI_SWIZZLE_W );
1002 assert( swizzle_z <= TGSI_SWIZZLE_W );
1003 assert( swizzle_w <= TGSI_SWIZZLE_W );
1004 assert( negate <= 1 );
1005 assert( index >= -0x8000 && index <= 0x7FFF );
1006
1007 src_register = tgsi_default_src_register();
1008 src_register.File = file;
1009 src_register.SwizzleX = swizzle_x;
1010 src_register.SwizzleY = swizzle_y;
1011 src_register.SwizzleZ = swizzle_z;
1012 src_register.SwizzleW = swizzle_w;
1013 src_register.Negate = negate;
1014 src_register.Indirect = indirect;
1015 src_register.Dimension = dimension;
1016 src_register.Index = index;
1017
1018 instruction_grow( instruction, header );
1019
1020 return src_register;
1021 }
1022
1023 struct tgsi_full_src_register
1024 tgsi_default_full_src_register( void )
1025 {
1026 struct tgsi_full_src_register full_src_register;
1027
1028 full_src_register.SrcRegister = tgsi_default_src_register();
1029 full_src_register.SrcRegisterExtSwz = tgsi_default_src_register_ext_swz();
1030 full_src_register.SrcRegisterExtMod = tgsi_default_src_register_ext_mod();
1031 full_src_register.SrcRegisterInd = tgsi_default_src_register();
1032 full_src_register.SrcRegisterDim = tgsi_default_dimension();
1033 full_src_register.SrcRegisterDimInd = tgsi_default_src_register();
1034
1035 return full_src_register;
1036 }
1037
1038 struct tgsi_src_register_ext_swz
1039 tgsi_default_src_register_ext_swz( void )
1040 {
1041 struct tgsi_src_register_ext_swz src_register_ext_swz;
1042
1043 src_register_ext_swz.Type = TGSI_SRC_REGISTER_EXT_TYPE_SWZ;
1044 src_register_ext_swz.ExtSwizzleX = TGSI_EXTSWIZZLE_X;
1045 src_register_ext_swz.ExtSwizzleY = TGSI_EXTSWIZZLE_Y;
1046 src_register_ext_swz.ExtSwizzleZ = TGSI_EXTSWIZZLE_Z;
1047 src_register_ext_swz.ExtSwizzleW = TGSI_EXTSWIZZLE_W;
1048 src_register_ext_swz.NegateX = 0;
1049 src_register_ext_swz.NegateY = 0;
1050 src_register_ext_swz.NegateZ = 0;
1051 src_register_ext_swz.NegateW = 0;
1052 src_register_ext_swz.Padding = 0;
1053 src_register_ext_swz.Extended = 0;
1054
1055 return src_register_ext_swz;
1056 }
1057
1058 unsigned
1059 tgsi_compare_src_register_ext_swz(
1060 struct tgsi_src_register_ext_swz a,
1061 struct tgsi_src_register_ext_swz b )
1062 {
1063 a.Padding = b.Padding = 0;
1064 a.Extended = b.Extended = 0;
1065 return compare32(&a, &b);
1066 }
1067
1068 struct tgsi_src_register_ext_swz
1069 tgsi_build_src_register_ext_swz(
1070 unsigned ext_swizzle_x,
1071 unsigned ext_swizzle_y,
1072 unsigned ext_swizzle_z,
1073 unsigned ext_swizzle_w,
1074 unsigned negate_x,
1075 unsigned negate_y,
1076 unsigned negate_z,
1077 unsigned negate_w,
1078 struct tgsi_token *prev_token,
1079 struct tgsi_instruction *instruction,
1080 struct tgsi_header *header )
1081 {
1082 struct tgsi_src_register_ext_swz src_register_ext_swz;
1083
1084 assert( ext_swizzle_x <= TGSI_EXTSWIZZLE_ONE );
1085 assert( ext_swizzle_y <= TGSI_EXTSWIZZLE_ONE );
1086 assert( ext_swizzle_z <= TGSI_EXTSWIZZLE_ONE );
1087 assert( ext_swizzle_w <= TGSI_EXTSWIZZLE_ONE );
1088 assert( negate_x <= 1 );
1089 assert( negate_y <= 1 );
1090 assert( negate_z <= 1 );
1091 assert( negate_w <= 1 );
1092
1093 src_register_ext_swz = tgsi_default_src_register_ext_swz();
1094 src_register_ext_swz.ExtSwizzleX = ext_swizzle_x;
1095 src_register_ext_swz.ExtSwizzleY = ext_swizzle_y;
1096 src_register_ext_swz.ExtSwizzleZ = ext_swizzle_z;
1097 src_register_ext_swz.ExtSwizzleW = ext_swizzle_w;
1098 src_register_ext_swz.NegateX = negate_x;
1099 src_register_ext_swz.NegateY = negate_y;
1100 src_register_ext_swz.NegateZ = negate_z;
1101 src_register_ext_swz.NegateW = negate_w;
1102
1103 prev_token->Extended = 1;
1104 instruction_grow( instruction, header );
1105
1106 return src_register_ext_swz;
1107 }
1108
1109 struct tgsi_src_register_ext_mod
1110 tgsi_default_src_register_ext_mod( void )
1111 {
1112 struct tgsi_src_register_ext_mod src_register_ext_mod;
1113
1114 src_register_ext_mod.Type = TGSI_SRC_REGISTER_EXT_TYPE_MOD;
1115 src_register_ext_mod.Complement = 0;
1116 src_register_ext_mod.Bias = 0;
1117 src_register_ext_mod.Scale2X = 0;
1118 src_register_ext_mod.Absolute = 0;
1119 src_register_ext_mod.Negate = 0;
1120 src_register_ext_mod.Padding = 0;
1121 src_register_ext_mod.Extended = 0;
1122
1123 return src_register_ext_mod;
1124 }
1125
1126 unsigned
1127 tgsi_compare_src_register_ext_mod(
1128 struct tgsi_src_register_ext_mod a,
1129 struct tgsi_src_register_ext_mod b )
1130 {
1131 a.Padding = b.Padding = 0;
1132 a.Extended = b.Extended = 0;
1133 return compare32(&a, &b);
1134 }
1135
1136 struct tgsi_src_register_ext_mod
1137 tgsi_build_src_register_ext_mod(
1138 unsigned complement,
1139 unsigned bias,
1140 unsigned scale_2x,
1141 unsigned absolute,
1142 unsigned negate,
1143 struct tgsi_token *prev_token,
1144 struct tgsi_instruction *instruction,
1145 struct tgsi_header *header )
1146 {
1147 struct tgsi_src_register_ext_mod src_register_ext_mod;
1148
1149 assert( complement <= 1 );
1150 assert( bias <= 1 );
1151 assert( scale_2x <= 1 );
1152 assert( absolute <= 1 );
1153 assert( negate <= 1 );
1154
1155 src_register_ext_mod = tgsi_default_src_register_ext_mod();
1156 src_register_ext_mod.Complement = complement;
1157 src_register_ext_mod.Bias = bias;
1158 src_register_ext_mod.Scale2X = scale_2x;
1159 src_register_ext_mod.Absolute = absolute;
1160 src_register_ext_mod.Negate = negate;
1161
1162 prev_token->Extended = 1;
1163 instruction_grow( instruction, header );
1164
1165 return src_register_ext_mod;
1166 }
1167
1168 struct tgsi_dimension
1169 tgsi_default_dimension( void )
1170 {
1171 struct tgsi_dimension dimension;
1172
1173 dimension.Indirect = 0;
1174 dimension.Dimension = 0;
1175 dimension.Padding = 0;
1176 dimension.Index = 0;
1177 dimension.Extended = 0;
1178
1179 return dimension;
1180 }
1181
1182 struct tgsi_dimension
1183 tgsi_build_dimension(
1184 unsigned indirect,
1185 unsigned index,
1186 struct tgsi_instruction *instruction,
1187 struct tgsi_header *header )
1188 {
1189 struct tgsi_dimension dimension;
1190
1191 dimension = tgsi_default_dimension();
1192 dimension.Indirect = indirect;
1193 dimension.Index = index;
1194
1195 instruction_grow( instruction, header );
1196
1197 return dimension;
1198 }
1199
1200 struct tgsi_dst_register
1201 tgsi_default_dst_register( void )
1202 {
1203 struct tgsi_dst_register dst_register;
1204
1205 dst_register.File = TGSI_FILE_NULL;
1206 dst_register.WriteMask = TGSI_WRITEMASK_XYZW;
1207 dst_register.Indirect = 0;
1208 dst_register.Dimension = 0;
1209 dst_register.Index = 0;
1210 dst_register.Padding = 0;
1211 dst_register.Extended = 0;
1212
1213 return dst_register;
1214 }
1215
1216 struct tgsi_dst_register
1217 tgsi_build_dst_register(
1218 unsigned file,
1219 unsigned mask,
1220 unsigned indirect,
1221 int index,
1222 struct tgsi_instruction *instruction,
1223 struct tgsi_header *header )
1224 {
1225 struct tgsi_dst_register dst_register;
1226
1227 assert( file < TGSI_FILE_COUNT );
1228 assert( mask <= TGSI_WRITEMASK_XYZW );
1229 assert( index >= -32768 && index <= 32767 );
1230
1231 dst_register = tgsi_default_dst_register();
1232 dst_register.File = file;
1233 dst_register.WriteMask = mask;
1234 dst_register.Index = index;
1235 dst_register.Indirect = indirect;
1236
1237 instruction_grow( instruction, header );
1238
1239 return dst_register;
1240 }
1241
1242 struct tgsi_full_dst_register
1243 tgsi_default_full_dst_register( void )
1244 {
1245 struct tgsi_full_dst_register full_dst_register;
1246
1247 full_dst_register.DstRegister = tgsi_default_dst_register();
1248 full_dst_register.DstRegisterInd = tgsi_default_src_register();
1249 full_dst_register.DstRegisterExtConcode =
1250 tgsi_default_dst_register_ext_concode();
1251 full_dst_register.DstRegisterExtModulate =
1252 tgsi_default_dst_register_ext_modulate();
1253
1254 return full_dst_register;
1255 }
1256
1257 struct tgsi_dst_register_ext_concode
1258 tgsi_default_dst_register_ext_concode( void )
1259 {
1260 struct tgsi_dst_register_ext_concode dst_register_ext_concode;
1261
1262 dst_register_ext_concode.Type = TGSI_DST_REGISTER_EXT_TYPE_CONDCODE;
1263 dst_register_ext_concode.CondMask = TGSI_CC_TR;
1264 dst_register_ext_concode.CondSwizzleX = TGSI_SWIZZLE_X;
1265 dst_register_ext_concode.CondSwizzleY = TGSI_SWIZZLE_Y;
1266 dst_register_ext_concode.CondSwizzleZ = TGSI_SWIZZLE_Z;
1267 dst_register_ext_concode.CondSwizzleW = TGSI_SWIZZLE_W;
1268 dst_register_ext_concode.CondSrcIndex = 0;
1269 dst_register_ext_concode.Padding = 0;
1270 dst_register_ext_concode.Extended = 0;
1271
1272 return dst_register_ext_concode;
1273 }
1274
1275 unsigned
1276 tgsi_compare_dst_register_ext_concode(
1277 struct tgsi_dst_register_ext_concode a,
1278 struct tgsi_dst_register_ext_concode b )
1279 {
1280 a.Padding = b.Padding = 0;
1281 a.Extended = b.Extended = 0;
1282 return compare32(&a, &b);
1283 }
1284
1285 struct tgsi_dst_register_ext_concode
1286 tgsi_build_dst_register_ext_concode(
1287 unsigned cc,
1288 unsigned swizzle_x,
1289 unsigned swizzle_y,
1290 unsigned swizzle_z,
1291 unsigned swizzle_w,
1292 int index,
1293 struct tgsi_token *prev_token,
1294 struct tgsi_instruction *instruction,
1295 struct tgsi_header *header )
1296 {
1297 struct tgsi_dst_register_ext_concode dst_register_ext_concode;
1298
1299 assert( cc <= TGSI_CC_FL );
1300 assert( swizzle_x <= TGSI_SWIZZLE_W );
1301 assert( swizzle_y <= TGSI_SWIZZLE_W );
1302 assert( swizzle_z <= TGSI_SWIZZLE_W );
1303 assert( swizzle_w <= TGSI_SWIZZLE_W );
1304 assert( index >= -32768 && index <= 32767 );
1305
1306 dst_register_ext_concode = tgsi_default_dst_register_ext_concode();
1307 dst_register_ext_concode.CondMask = cc;
1308 dst_register_ext_concode.CondSwizzleX = swizzle_x;
1309 dst_register_ext_concode.CondSwizzleY = swizzle_y;
1310 dst_register_ext_concode.CondSwizzleZ = swizzle_z;
1311 dst_register_ext_concode.CondSwizzleW = swizzle_w;
1312 dst_register_ext_concode.CondSrcIndex = index;
1313
1314 prev_token->Extended = 1;
1315 instruction_grow( instruction, header );
1316
1317 return dst_register_ext_concode;
1318 }
1319
1320 struct tgsi_dst_register_ext_modulate
1321 tgsi_default_dst_register_ext_modulate( void )
1322 {
1323 struct tgsi_dst_register_ext_modulate dst_register_ext_modulate;
1324
1325 dst_register_ext_modulate.Type = TGSI_DST_REGISTER_EXT_TYPE_MODULATE;
1326 dst_register_ext_modulate.Modulate = TGSI_MODULATE_1X;
1327 dst_register_ext_modulate.Padding = 0;
1328 dst_register_ext_modulate.Extended = 0;
1329
1330 return dst_register_ext_modulate;
1331 }
1332
1333 unsigned
1334 tgsi_compare_dst_register_ext_modulate(
1335 struct tgsi_dst_register_ext_modulate a,
1336 struct tgsi_dst_register_ext_modulate b )
1337 {
1338 a.Padding = b.Padding = 0;
1339 a.Extended = b.Extended = 0;
1340 return compare32(&a, &b);
1341 }
1342
1343 struct tgsi_dst_register_ext_modulate
1344 tgsi_build_dst_register_ext_modulate(
1345 unsigned modulate,
1346 struct tgsi_token *prev_token,
1347 struct tgsi_instruction *instruction,
1348 struct tgsi_header *header )
1349 {
1350 struct tgsi_dst_register_ext_modulate dst_register_ext_modulate;
1351
1352 assert( modulate <= TGSI_MODULATE_EIGHTH );
1353
1354 dst_register_ext_modulate = tgsi_default_dst_register_ext_modulate();
1355 dst_register_ext_modulate.Modulate = modulate;
1356
1357 prev_token->Extended = 1;
1358 instruction_grow( instruction, header );
1359
1360 return dst_register_ext_modulate;
1361 }