1 /**************************************************************************
3 * Copyright 2007 VMware, Inc.
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:
14 * The above copyright notice and this permission notice (including the
15 * next paragraph) shall be included in all copies or substantial portions
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 VMWARE 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.
26 **************************************************************************/
28 #include "util/u_debug.h"
29 #include "pipe/p_format.h"
30 #include "pipe/p_shader_tokens.h"
31 #include "tgsi_build.h"
32 #include "tgsi_parse.h"
40 tgsi_build_header( void )
42 struct tgsi_header header
;
44 header
.HeaderSize
= 1;
51 header_headersize_grow( struct tgsi_header
*header
)
53 assert( header
->HeaderSize
< 0xFF );
54 assert( header
->BodySize
== 0 );
60 header_bodysize_grow( struct tgsi_header
*header
)
62 assert( header
->BodySize
< 0xFFFFFF );
70 struct tgsi_header
*header
)
72 struct tgsi_processor processor
;
74 processor
.Processor
= type
;
75 processor
.Padding
= 0;
77 header_headersize_grow( header
);
88 struct tgsi_declaration
*declaration
,
89 struct tgsi_header
*header
)
91 assert( declaration
->NrTokens
< 0xFF );
93 declaration
->NrTokens
++;
95 header_bodysize_grow( header
);
98 static struct tgsi_declaration
99 tgsi_default_declaration( void )
101 struct tgsi_declaration declaration
;
103 declaration
.Type
= TGSI_TOKEN_TYPE_DECLARATION
;
104 declaration
.NrTokens
= 1;
105 declaration
.File
= TGSI_FILE_NULL
;
106 declaration
.UsageMask
= TGSI_WRITEMASK_XYZW
;
107 declaration
.Interpolate
= 0;
108 declaration
.Dimension
= 0;
109 declaration
.Semantic
= 0;
110 declaration
.Invariant
= 0;
111 declaration
.Local
= 0;
112 declaration
.Array
= 0;
113 declaration
.Atomic
= 0;
114 declaration
.Shared
= 0;
115 declaration
.Padding
= 0;
120 static struct tgsi_declaration
121 tgsi_build_declaration(
124 unsigned interpolate
,
132 struct tgsi_header
*header
)
134 struct tgsi_declaration declaration
;
136 assert( file
< TGSI_FILE_COUNT
);
137 assert( interpolate
< TGSI_INTERPOLATE_COUNT
);
139 declaration
= tgsi_default_declaration();
140 declaration
.File
= file
;
141 declaration
.UsageMask
= usage_mask
;
142 declaration
.Interpolate
= interpolate
;
143 declaration
.Dimension
= dimension
;
144 declaration
.Semantic
= semantic
;
145 declaration
.Invariant
= invariant
;
146 declaration
.Local
= local
;
147 declaration
.Array
= array
;
148 declaration
.Atomic
= atomic
;
149 declaration
.Shared
= shared
;
150 header_bodysize_grow( header
);
155 static struct tgsi_declaration_range
156 tgsi_default_declaration_range( void )
158 struct tgsi_declaration_range dr
;
166 static struct tgsi_declaration_range
167 tgsi_build_declaration_range(
170 struct tgsi_declaration
*declaration
,
171 struct tgsi_header
*header
)
173 struct tgsi_declaration_range declaration_range
;
175 assert( last
>= first
);
176 assert( last
<= 0xFFFF );
178 declaration_range
.First
= first
;
179 declaration_range
.Last
= last
;
181 declaration_grow( declaration
, header
);
183 return declaration_range
;
186 static struct tgsi_declaration_dimension
187 tgsi_build_declaration_dimension(unsigned index_2d
,
188 struct tgsi_declaration
*declaration
,
189 struct tgsi_header
*header
)
191 struct tgsi_declaration_dimension dd
;
193 assert(index_2d
<= 0xFFFF);
195 dd
.Index2D
= index_2d
;
198 declaration_grow(declaration
, header
);
203 static struct tgsi_declaration_interp
204 tgsi_default_declaration_interp( void )
206 struct tgsi_declaration_interp di
;
208 di
.Interpolate
= TGSI_INTERPOLATE_CONSTANT
;
209 di
.Location
= TGSI_INTERPOLATE_LOC_CENTER
;
210 di
.CylindricalWrap
= 0;
216 static struct tgsi_declaration_interp
217 tgsi_build_declaration_interp(unsigned interpolate
,
218 unsigned interpolate_location
,
219 unsigned cylindrical_wrap
,
220 struct tgsi_declaration
*declaration
,
221 struct tgsi_header
*header
)
223 struct tgsi_declaration_interp di
;
225 di
.Interpolate
= interpolate
;
226 di
.Location
= interpolate_location
;
227 di
.CylindricalWrap
= cylindrical_wrap
;
230 declaration_grow(declaration
, header
);
235 static struct tgsi_declaration_semantic
236 tgsi_default_declaration_semantic( void )
238 struct tgsi_declaration_semantic ds
;
240 ds
.Name
= TGSI_SEMANTIC_POSITION
;
247 static struct tgsi_declaration_semantic
248 tgsi_build_declaration_semantic(
249 unsigned semantic_name
,
250 unsigned semantic_index
,
251 struct tgsi_declaration
*declaration
,
252 struct tgsi_header
*header
)
254 struct tgsi_declaration_semantic ds
;
256 assert( semantic_name
<= TGSI_SEMANTIC_COUNT
);
257 assert( semantic_index
<= 0xFFFF );
259 ds
.Name
= semantic_name
;
260 ds
.Index
= semantic_index
;
263 declaration_grow( declaration
, header
);
268 static struct tgsi_declaration_image
269 tgsi_default_declaration_image(void)
271 struct tgsi_declaration_image di
;
273 di
.Resource
= TGSI_TEXTURE_BUFFER
;
282 static struct tgsi_declaration_image
283 tgsi_build_declaration_image(unsigned texture
,
287 struct tgsi_declaration
*declaration
,
288 struct tgsi_header
*header
)
290 struct tgsi_declaration_image di
;
292 di
= tgsi_default_declaration_image();
293 di
.Resource
= texture
;
296 di
.Writable
= writable
;
298 declaration_grow(declaration
, header
);
303 static struct tgsi_declaration_sampler_view
304 tgsi_default_declaration_sampler_view(void)
306 struct tgsi_declaration_sampler_view dsv
;
308 dsv
.Resource
= TGSI_TEXTURE_BUFFER
;
309 dsv
.ReturnTypeX
= TGSI_RETURN_TYPE_UNORM
;
310 dsv
.ReturnTypeY
= TGSI_RETURN_TYPE_UNORM
;
311 dsv
.ReturnTypeZ
= TGSI_RETURN_TYPE_UNORM
;
312 dsv
.ReturnTypeW
= TGSI_RETURN_TYPE_UNORM
;
317 static struct tgsi_declaration_sampler_view
318 tgsi_build_declaration_sampler_view(unsigned texture
,
319 unsigned return_type_x
,
320 unsigned return_type_y
,
321 unsigned return_type_z
,
322 unsigned return_type_w
,
323 struct tgsi_declaration
*declaration
,
324 struct tgsi_header
*header
)
326 struct tgsi_declaration_sampler_view dsv
;
328 dsv
= tgsi_default_declaration_sampler_view();
329 dsv
.Resource
= texture
;
330 dsv
.ReturnTypeX
= return_type_x
;
331 dsv
.ReturnTypeY
= return_type_y
;
332 dsv
.ReturnTypeZ
= return_type_z
;
333 dsv
.ReturnTypeW
= return_type_w
;
335 declaration_grow(declaration
, header
);
341 static struct tgsi_declaration_array
342 tgsi_default_declaration_array( void )
344 struct tgsi_declaration_array a
;
352 static struct tgsi_declaration_array
353 tgsi_build_declaration_array(unsigned arrayid
,
354 struct tgsi_declaration
*declaration
,
355 struct tgsi_header
*header
)
357 struct tgsi_declaration_array da
;
359 da
= tgsi_default_declaration_array();
360 da
.ArrayID
= arrayid
;
362 declaration_grow(declaration
, header
);
367 struct tgsi_full_declaration
368 tgsi_default_full_declaration( void )
370 struct tgsi_full_declaration full_declaration
;
372 full_declaration
.Declaration
= tgsi_default_declaration();
373 full_declaration
.Range
= tgsi_default_declaration_range();
374 full_declaration
.Semantic
= tgsi_default_declaration_semantic();
375 full_declaration
.Interp
= tgsi_default_declaration_interp();
376 full_declaration
.Image
= tgsi_default_declaration_image();
377 full_declaration
.SamplerView
= tgsi_default_declaration_sampler_view();
378 full_declaration
.Array
= tgsi_default_declaration_array();
380 return full_declaration
;
384 tgsi_build_full_declaration(
385 const struct tgsi_full_declaration
*full_decl
,
386 struct tgsi_token
*tokens
,
387 struct tgsi_header
*header
,
391 struct tgsi_declaration
*declaration
;
392 struct tgsi_declaration_range
*dr
;
394 if( maxsize
<= size
)
396 declaration
= (struct tgsi_declaration
*) &tokens
[size
];
399 *declaration
= tgsi_build_declaration(
400 full_decl
->Declaration
.File
,
401 full_decl
->Declaration
.UsageMask
,
402 full_decl
->Declaration
.Interpolate
,
403 full_decl
->Declaration
.Dimension
,
404 full_decl
->Declaration
.Semantic
,
405 full_decl
->Declaration
.Invariant
,
406 full_decl
->Declaration
.Local
,
407 full_decl
->Declaration
.Array
,
408 full_decl
->Declaration
.Atomic
,
409 full_decl
->Declaration
.Shared
,
414 dr
= (struct tgsi_declaration_range
*) &tokens
[size
];
417 *dr
= tgsi_build_declaration_range(
418 full_decl
->Range
.First
,
419 full_decl
->Range
.Last
,
423 if (full_decl
->Declaration
.Dimension
) {
424 struct tgsi_declaration_dimension
*dd
;
426 if (maxsize
<= size
) {
429 dd
= (struct tgsi_declaration_dimension
*)&tokens
[size
];
432 *dd
= tgsi_build_declaration_dimension(full_decl
->Dim
.Index2D
,
437 if (full_decl
->Declaration
.Interpolate
) {
438 struct tgsi_declaration_interp
*di
;
440 if (maxsize
<= size
) {
443 di
= (struct tgsi_declaration_interp
*)&tokens
[size
];
446 *di
= tgsi_build_declaration_interp(full_decl
->Interp
.Interpolate
,
447 full_decl
->Interp
.Location
,
448 full_decl
->Interp
.CylindricalWrap
,
453 if( full_decl
->Declaration
.Semantic
) {
454 struct tgsi_declaration_semantic
*ds
;
456 if( maxsize
<= size
)
458 ds
= (struct tgsi_declaration_semantic
*) &tokens
[size
];
461 *ds
= tgsi_build_declaration_semantic(
462 full_decl
->Semantic
.Name
,
463 full_decl
->Semantic
.Index
,
468 if (full_decl
->Declaration
.File
== TGSI_FILE_IMAGE
) {
469 struct tgsi_declaration_image
*di
;
471 if (maxsize
<= size
) {
474 di
= (struct tgsi_declaration_image
*)&tokens
[size
];
477 *di
= tgsi_build_declaration_image(full_decl
->Image
.Resource
,
478 full_decl
->Image
.Format
,
479 full_decl
->Image
.Raw
,
480 full_decl
->Image
.Writable
,
485 if (full_decl
->Declaration
.File
== TGSI_FILE_SAMPLER_VIEW
) {
486 struct tgsi_declaration_sampler_view
*dsv
;
488 if (maxsize
<= size
) {
491 dsv
= (struct tgsi_declaration_sampler_view
*)&tokens
[size
];
494 *dsv
= tgsi_build_declaration_sampler_view(
495 full_decl
->SamplerView
.Resource
,
496 full_decl
->SamplerView
.ReturnTypeX
,
497 full_decl
->SamplerView
.ReturnTypeY
,
498 full_decl
->SamplerView
.ReturnTypeZ
,
499 full_decl
->SamplerView
.ReturnTypeW
,
504 if (full_decl
->Declaration
.Array
) {
505 struct tgsi_declaration_array
*da
;
507 if (maxsize
<= size
) {
510 da
= (struct tgsi_declaration_array
*)&tokens
[size
];
512 *da
= tgsi_build_declaration_array(
513 full_decl
->Array
.ArrayID
,
524 static struct tgsi_immediate
525 tgsi_default_immediate( void )
527 struct tgsi_immediate immediate
;
529 immediate
.Type
= TGSI_TOKEN_TYPE_IMMEDIATE
;
530 immediate
.NrTokens
= 1;
531 immediate
.DataType
= TGSI_IMM_FLOAT32
;
532 immediate
.Padding
= 0;
537 static struct tgsi_immediate
538 tgsi_build_immediate(
539 struct tgsi_header
*header
,
542 struct tgsi_immediate immediate
;
544 immediate
= tgsi_default_immediate();
545 immediate
.DataType
= type
;
547 header_bodysize_grow( header
);
552 struct tgsi_full_immediate
553 tgsi_default_full_immediate( void )
555 struct tgsi_full_immediate fullimm
;
557 fullimm
.Immediate
= tgsi_default_immediate();
558 fullimm
.u
[0].Float
= 0.0f
;
559 fullimm
.u
[1].Float
= 0.0f
;
560 fullimm
.u
[2].Float
= 0.0f
;
561 fullimm
.u
[3].Float
= 0.0f
;
568 struct tgsi_immediate
*immediate
,
569 struct tgsi_header
*header
)
571 assert( immediate
->NrTokens
< 0xFF );
573 immediate
->NrTokens
++;
575 header_bodysize_grow( header
);
579 tgsi_build_full_immediate(
580 const struct tgsi_full_immediate
*full_imm
,
581 struct tgsi_token
*tokens
,
582 struct tgsi_header
*header
,
585 unsigned size
= 0, i
;
586 struct tgsi_immediate
*immediate
;
588 if( maxsize
<= size
)
590 immediate
= (struct tgsi_immediate
*) &tokens
[size
];
593 *immediate
= tgsi_build_immediate( header
, full_imm
->Immediate
.DataType
);
595 assert( full_imm
->Immediate
.NrTokens
<= 4 + 1 );
597 for( i
= 0; i
< full_imm
->Immediate
.NrTokens
- 1; i
++ ) {
598 union tgsi_immediate_data
*data
;
600 if( maxsize
<= size
)
603 data
= (union tgsi_immediate_data
*) &tokens
[size
];
604 *data
= full_imm
->u
[i
];
606 immediate_grow( immediate
, header
);
617 struct tgsi_instruction
618 tgsi_default_instruction( void )
620 struct tgsi_instruction instruction
;
622 instruction
.Type
= TGSI_TOKEN_TYPE_INSTRUCTION
;
623 instruction
.NrTokens
= 0;
624 instruction
.Opcode
= TGSI_OPCODE_MOV
;
625 instruction
.Saturate
= 0;
626 instruction
.Predicate
= 0;
627 instruction
.NumDstRegs
= 1;
628 instruction
.NumSrcRegs
= 1;
629 instruction
.Label
= 0;
630 instruction
.Texture
= 0;
631 instruction
.Memory
= 0;
632 instruction
.Padding
= 0;
637 static struct tgsi_instruction
638 tgsi_build_instruction(unsigned opcode
,
641 unsigned num_dst_regs
,
642 unsigned num_src_regs
,
643 struct tgsi_header
*header
)
645 struct tgsi_instruction instruction
;
647 assert (opcode
<= TGSI_OPCODE_LAST
);
648 assert (saturate
<= 1);
649 assert (num_dst_regs
<= 3);
650 assert (num_src_regs
<= 15);
652 instruction
= tgsi_default_instruction();
653 instruction
.Opcode
= opcode
;
654 instruction
.Saturate
= saturate
;
655 instruction
.Predicate
= predicate
;
656 instruction
.NumDstRegs
= num_dst_regs
;
657 instruction
.NumSrcRegs
= num_src_regs
;
659 header_bodysize_grow( header
);
666 struct tgsi_instruction
*instruction
,
667 struct tgsi_header
*header
)
669 assert (instruction
->NrTokens
< 0xFF);
671 instruction
->NrTokens
++;
673 header_bodysize_grow( header
);
676 struct tgsi_instruction_predicate
677 tgsi_default_instruction_predicate(void)
679 struct tgsi_instruction_predicate instruction_predicate
;
681 instruction_predicate
.SwizzleX
= TGSI_SWIZZLE_X
;
682 instruction_predicate
.SwizzleY
= TGSI_SWIZZLE_Y
;
683 instruction_predicate
.SwizzleZ
= TGSI_SWIZZLE_Z
;
684 instruction_predicate
.SwizzleW
= TGSI_SWIZZLE_W
;
685 instruction_predicate
.Negate
= 0;
686 instruction_predicate
.Index
= 0;
687 instruction_predicate
.Padding
= 0;
689 return instruction_predicate
;
692 static struct tgsi_instruction_predicate
693 tgsi_build_instruction_predicate(int index
,
699 struct tgsi_instruction
*instruction
,
700 struct tgsi_header
*header
)
702 struct tgsi_instruction_predicate instruction_predicate
;
704 instruction_predicate
= tgsi_default_instruction_predicate();
705 instruction_predicate
.SwizzleX
= swizzleX
;
706 instruction_predicate
.SwizzleY
= swizzleY
;
707 instruction_predicate
.SwizzleZ
= swizzleZ
;
708 instruction_predicate
.SwizzleW
= swizzleW
;
709 instruction_predicate
.Negate
= negate
;
710 instruction_predicate
.Index
= index
;
712 instruction_grow(instruction
, header
);
714 return instruction_predicate
;
717 static struct tgsi_instruction_label
718 tgsi_default_instruction_label( void )
720 struct tgsi_instruction_label instruction_label
;
722 instruction_label
.Label
= 0;
723 instruction_label
.Padding
= 0;
725 return instruction_label
;
728 static struct tgsi_instruction_label
729 tgsi_build_instruction_label(
731 struct tgsi_token
*prev_token
,
732 struct tgsi_instruction
*instruction
,
733 struct tgsi_header
*header
)
735 struct tgsi_instruction_label instruction_label
;
737 instruction_label
.Label
= label
;
738 instruction_label
.Padding
= 0;
739 instruction
->Label
= 1;
741 instruction_grow( instruction
, header
);
743 return instruction_label
;
746 static struct tgsi_instruction_texture
747 tgsi_default_instruction_texture( void )
749 struct tgsi_instruction_texture instruction_texture
;
751 instruction_texture
.Texture
= TGSI_TEXTURE_UNKNOWN
;
752 instruction_texture
.NumOffsets
= 0;
753 instruction_texture
.Padding
= 0;
755 return instruction_texture
;
758 static struct tgsi_instruction_texture
759 tgsi_build_instruction_texture(
761 unsigned num_offsets
,
762 struct tgsi_token
*prev_token
,
763 struct tgsi_instruction
*instruction
,
764 struct tgsi_header
*header
)
766 struct tgsi_instruction_texture instruction_texture
;
768 instruction_texture
.Texture
= texture
;
769 instruction_texture
.NumOffsets
= num_offsets
;
770 instruction_texture
.Padding
= 0;
771 instruction
->Texture
= 1;
773 instruction_grow( instruction
, header
);
775 return instruction_texture
;
778 static struct tgsi_instruction_memory
779 tgsi_default_instruction_memory( void )
781 struct tgsi_instruction_memory instruction_memory
;
783 instruction_memory
.Qualifier
= 0;
784 instruction_memory
.Texture
= 0;
785 instruction_memory
.Format
= 0;
786 instruction_memory
.Padding
= 0;
788 return instruction_memory
;
791 static struct tgsi_instruction_memory
792 tgsi_build_instruction_memory(
794 struct tgsi_token
*prev_token
,
795 struct tgsi_instruction
*instruction
,
796 struct tgsi_header
*header
)
798 struct tgsi_instruction_memory instruction_memory
;
800 instruction_memory
.Qualifier
= qualifier
;
801 instruction_memory
.Texture
= 0;
802 instruction_memory
.Format
= 0;
803 instruction_memory
.Padding
= 0;
804 instruction
->Memory
= 1;
806 instruction_grow( instruction
, header
);
808 return instruction_memory
;
811 static struct tgsi_texture_offset
812 tgsi_default_texture_offset( void )
814 struct tgsi_texture_offset texture_offset
;
816 texture_offset
.Index
= 0;
817 texture_offset
.File
= 0;
818 texture_offset
.SwizzleX
= 0;
819 texture_offset
.SwizzleY
= 0;
820 texture_offset
.SwizzleZ
= 0;
821 texture_offset
.Padding
= 0;
823 return texture_offset
;
826 static struct tgsi_texture_offset
827 tgsi_build_texture_offset(
828 int index
, int file
, int swizzle_x
, int swizzle_y
, int swizzle_z
,
829 struct tgsi_token
*prev_token
,
830 struct tgsi_instruction
*instruction
,
831 struct tgsi_header
*header
)
833 struct tgsi_texture_offset texture_offset
;
835 texture_offset
.Index
= index
;
836 texture_offset
.File
= file
;
837 texture_offset
.SwizzleX
= swizzle_x
;
838 texture_offset
.SwizzleY
= swizzle_y
;
839 texture_offset
.SwizzleZ
= swizzle_z
;
840 texture_offset
.Padding
= 0;
842 instruction_grow( instruction
, header
);
844 return texture_offset
;
847 static struct tgsi_src_register
848 tgsi_default_src_register( void )
850 struct tgsi_src_register src_register
;
852 src_register
.File
= TGSI_FILE_NULL
;
853 src_register
.SwizzleX
= TGSI_SWIZZLE_X
;
854 src_register
.SwizzleY
= TGSI_SWIZZLE_Y
;
855 src_register
.SwizzleZ
= TGSI_SWIZZLE_Z
;
856 src_register
.SwizzleW
= TGSI_SWIZZLE_W
;
857 src_register
.Negate
= 0;
858 src_register
.Absolute
= 0;
859 src_register
.Indirect
= 0;
860 src_register
.Dimension
= 0;
861 src_register
.Index
= 0;
866 static struct tgsi_src_register
867 tgsi_build_src_register(
878 struct tgsi_instruction
*instruction
,
879 struct tgsi_header
*header
)
881 struct tgsi_src_register src_register
;
883 assert( file
< TGSI_FILE_COUNT
);
884 assert( swizzle_x
<= TGSI_SWIZZLE_W
);
885 assert( swizzle_y
<= TGSI_SWIZZLE_W
);
886 assert( swizzle_z
<= TGSI_SWIZZLE_W
);
887 assert( swizzle_w
<= TGSI_SWIZZLE_W
);
888 assert( negate
<= 1 );
889 assert( index
>= -0x8000 && index
<= 0x7FFF );
891 src_register
.File
= file
;
892 src_register
.SwizzleX
= swizzle_x
;
893 src_register
.SwizzleY
= swizzle_y
;
894 src_register
.SwizzleZ
= swizzle_z
;
895 src_register
.SwizzleW
= swizzle_w
;
896 src_register
.Negate
= negate
;
897 src_register
.Absolute
= absolute
;
898 src_register
.Indirect
= indirect
;
899 src_register
.Dimension
= dimension
;
900 src_register
.Index
= index
;
902 instruction_grow( instruction
, header
);
907 static struct tgsi_ind_register
908 tgsi_default_ind_register( void )
910 struct tgsi_ind_register ind_register
;
912 ind_register
.File
= TGSI_FILE_NULL
;
913 ind_register
.Index
= 0;
914 ind_register
.Swizzle
= TGSI_SWIZZLE_X
;
915 ind_register
.ArrayID
= 0;
920 static struct tgsi_ind_register
921 tgsi_build_ind_register(
926 struct tgsi_instruction
*instruction
,
927 struct tgsi_header
*header
)
929 struct tgsi_ind_register ind_register
;
931 assert( file
< TGSI_FILE_COUNT
);
932 assert( swizzle
<= TGSI_SWIZZLE_W
);
933 assert( index
>= -0x8000 && index
<= 0x7FFF );
935 ind_register
.File
= file
;
936 ind_register
.Swizzle
= swizzle
;
937 ind_register
.Index
= index
;
938 ind_register
.ArrayID
= arrayid
;
940 instruction_grow( instruction
, header
);
945 static struct tgsi_dimension
946 tgsi_default_dimension( void )
948 struct tgsi_dimension dimension
;
950 dimension
.Indirect
= 0;
951 dimension
.Dimension
= 0;
952 dimension
.Padding
= 0;
958 static struct tgsi_full_src_register
959 tgsi_default_full_src_register( void )
961 struct tgsi_full_src_register full_src_register
;
963 full_src_register
.Register
= tgsi_default_src_register();
964 full_src_register
.Indirect
= tgsi_default_ind_register();
965 full_src_register
.Dimension
= tgsi_default_dimension();
966 full_src_register
.DimIndirect
= tgsi_default_ind_register();
968 return full_src_register
;
971 static struct tgsi_dimension
972 tgsi_build_dimension(
975 struct tgsi_instruction
*instruction
,
976 struct tgsi_header
*header
)
978 struct tgsi_dimension dimension
;
980 dimension
.Indirect
= indirect
;
981 dimension
.Dimension
= 0;
982 dimension
.Padding
= 0;
983 dimension
.Index
= index
;
985 instruction_grow( instruction
, header
);
990 static struct tgsi_dst_register
991 tgsi_default_dst_register( void )
993 struct tgsi_dst_register dst_register
;
995 dst_register
.File
= TGSI_FILE_NULL
;
996 dst_register
.WriteMask
= TGSI_WRITEMASK_XYZW
;
997 dst_register
.Indirect
= 0;
998 dst_register
.Dimension
= 0;
999 dst_register
.Index
= 0;
1000 dst_register
.Padding
= 0;
1002 return dst_register
;
1005 static struct tgsi_dst_register
1006 tgsi_build_dst_register(
1012 struct tgsi_instruction
*instruction
,
1013 struct tgsi_header
*header
)
1015 struct tgsi_dst_register dst_register
;
1017 assert( file
< TGSI_FILE_COUNT
);
1018 assert( mask
<= TGSI_WRITEMASK_XYZW
);
1019 assert( index
>= -32768 && index
<= 32767 );
1021 dst_register
.File
= file
;
1022 dst_register
.WriteMask
= mask
;
1023 dst_register
.Indirect
= indirect
;
1024 dst_register
.Dimension
= dimension
;
1025 dst_register
.Index
= index
;
1026 dst_register
.Padding
= 0;
1028 instruction_grow( instruction
, header
);
1030 return dst_register
;
1033 static struct tgsi_full_dst_register
1034 tgsi_default_full_dst_register( void )
1036 struct tgsi_full_dst_register full_dst_register
;
1038 full_dst_register
.Register
= tgsi_default_dst_register();
1039 full_dst_register
.Indirect
= tgsi_default_ind_register();
1040 full_dst_register
.Dimension
= tgsi_default_dimension();
1041 full_dst_register
.DimIndirect
= tgsi_default_ind_register();
1043 return full_dst_register
;
1046 struct tgsi_full_instruction
1047 tgsi_default_full_instruction( void )
1049 struct tgsi_full_instruction full_instruction
;
1052 full_instruction
.Instruction
= tgsi_default_instruction();
1053 full_instruction
.Predicate
= tgsi_default_instruction_predicate();
1054 full_instruction
.Label
= tgsi_default_instruction_label();
1055 full_instruction
.Texture
= tgsi_default_instruction_texture();
1056 full_instruction
.Memory
= tgsi_default_instruction_memory();
1057 for( i
= 0; i
< TGSI_FULL_MAX_TEX_OFFSETS
; i
++ ) {
1058 full_instruction
.TexOffsets
[i
] = tgsi_default_texture_offset();
1060 for( i
= 0; i
< TGSI_FULL_MAX_DST_REGISTERS
; i
++ ) {
1061 full_instruction
.Dst
[i
] = tgsi_default_full_dst_register();
1063 for( i
= 0; i
< TGSI_FULL_MAX_SRC_REGISTERS
; i
++ ) {
1064 full_instruction
.Src
[i
] = tgsi_default_full_src_register();
1067 return full_instruction
;
1071 tgsi_build_full_instruction(
1072 const struct tgsi_full_instruction
*full_inst
,
1073 struct tgsi_token
*tokens
,
1074 struct tgsi_header
*header
,
1079 struct tgsi_instruction
*instruction
;
1080 struct tgsi_token
*prev_token
;
1082 if( maxsize
<= size
)
1084 instruction
= (struct tgsi_instruction
*) &tokens
[size
];
1087 *instruction
= tgsi_build_instruction(full_inst
->Instruction
.Opcode
,
1088 full_inst
->Instruction
.Saturate
,
1089 full_inst
->Instruction
.Predicate
,
1090 full_inst
->Instruction
.NumDstRegs
,
1091 full_inst
->Instruction
.NumSrcRegs
,
1093 prev_token
= (struct tgsi_token
*) instruction
;
1095 if (full_inst
->Instruction
.Predicate
) {
1096 struct tgsi_instruction_predicate
*instruction_predicate
;
1098 if (maxsize
<= size
) {
1101 instruction_predicate
= (struct tgsi_instruction_predicate
*)&tokens
[size
];
1104 *instruction_predicate
=
1105 tgsi_build_instruction_predicate(full_inst
->Predicate
.Index
,
1106 full_inst
->Predicate
.Negate
,
1107 full_inst
->Predicate
.SwizzleX
,
1108 full_inst
->Predicate
.SwizzleY
,
1109 full_inst
->Predicate
.SwizzleZ
,
1110 full_inst
->Predicate
.SwizzleW
,
1115 if (full_inst
->Instruction
.Label
) {
1116 struct tgsi_instruction_label
*instruction_label
;
1118 if( maxsize
<= size
)
1121 (struct tgsi_instruction_label
*) &tokens
[size
];
1124 *instruction_label
= tgsi_build_instruction_label(
1125 full_inst
->Label
.Label
,
1129 prev_token
= (struct tgsi_token
*) instruction_label
;
1132 if (full_inst
->Instruction
.Texture
) {
1133 struct tgsi_instruction_texture
*instruction_texture
;
1135 if( maxsize
<= size
)
1137 instruction_texture
=
1138 (struct tgsi_instruction_texture
*) &tokens
[size
];
1141 *instruction_texture
= tgsi_build_instruction_texture(
1142 full_inst
->Texture
.Texture
,
1143 full_inst
->Texture
.NumOffsets
,
1147 prev_token
= (struct tgsi_token
*) instruction_texture
;
1149 for (i
= 0; i
< full_inst
->Texture
.NumOffsets
; i
++) {
1150 struct tgsi_texture_offset
*texture_offset
;
1152 if ( maxsize
<= size
)
1154 texture_offset
= (struct tgsi_texture_offset
*)&tokens
[size
];
1156 *texture_offset
= tgsi_build_texture_offset(
1157 full_inst
->TexOffsets
[i
].Index
,
1158 full_inst
->TexOffsets
[i
].File
,
1159 full_inst
->TexOffsets
[i
].SwizzleX
,
1160 full_inst
->TexOffsets
[i
].SwizzleY
,
1161 full_inst
->TexOffsets
[i
].SwizzleZ
,
1165 prev_token
= (struct tgsi_token
*) texture_offset
;
1169 if (full_inst
->Instruction
.Memory
) {
1170 struct tgsi_instruction_memory
*instruction_memory
;
1172 if( maxsize
<= size
)
1174 instruction_memory
=
1175 (struct tgsi_instruction_memory
*) &tokens
[size
];
1178 *instruction_memory
= tgsi_build_instruction_memory(
1179 full_inst
->Memory
.Qualifier
,
1183 prev_token
= (struct tgsi_token
*) instruction_memory
;
1186 for( i
= 0; i
< full_inst
->Instruction
.NumDstRegs
; i
++ ) {
1187 const struct tgsi_full_dst_register
*reg
= &full_inst
->Dst
[i
];
1188 struct tgsi_dst_register
*dst_register
;
1190 if( maxsize
<= size
)
1192 dst_register
= (struct tgsi_dst_register
*) &tokens
[size
];
1195 *dst_register
= tgsi_build_dst_register(
1197 reg
->Register
.WriteMask
,
1198 reg
->Register
.Indirect
,
1199 reg
->Register
.Dimension
,
1200 reg
->Register
.Index
,
1204 if( reg
->Register
.Indirect
) {
1205 struct tgsi_ind_register
*ind
;
1207 if( maxsize
<= size
)
1209 ind
= (struct tgsi_ind_register
*) &tokens
[size
];
1212 *ind
= tgsi_build_ind_register(
1214 reg
->Indirect
.Swizzle
,
1215 reg
->Indirect
.Index
,
1216 reg
->Indirect
.ArrayID
,
1221 if( reg
->Register
.Dimension
) {
1222 struct tgsi_dimension
*dim
;
1224 assert( !reg
->Dimension
.Dimension
);
1226 if( maxsize
<= size
)
1228 dim
= (struct tgsi_dimension
*) &tokens
[size
];
1231 *dim
= tgsi_build_dimension(
1232 reg
->Dimension
.Indirect
,
1233 reg
->Dimension
.Index
,
1237 if( reg
->Dimension
.Indirect
) {
1238 struct tgsi_ind_register
*ind
;
1240 if( maxsize
<= size
)
1242 ind
= (struct tgsi_ind_register
*) &tokens
[size
];
1245 *ind
= tgsi_build_ind_register(
1246 reg
->DimIndirect
.File
,
1247 reg
->DimIndirect
.Swizzle
,
1248 reg
->DimIndirect
.Index
,
1249 reg
->DimIndirect
.ArrayID
,
1256 for( i
= 0; i
< full_inst
->Instruction
.NumSrcRegs
; i
++ ) {
1257 const struct tgsi_full_src_register
*reg
= &full_inst
->Src
[i
];
1258 struct tgsi_src_register
*src_register
;
1260 if( maxsize
<= size
)
1262 src_register
= (struct tgsi_src_register
*) &tokens
[size
];
1265 *src_register
= tgsi_build_src_register(
1267 reg
->Register
.SwizzleX
,
1268 reg
->Register
.SwizzleY
,
1269 reg
->Register
.SwizzleZ
,
1270 reg
->Register
.SwizzleW
,
1271 reg
->Register
.Negate
,
1272 reg
->Register
.Absolute
,
1273 reg
->Register
.Indirect
,
1274 reg
->Register
.Dimension
,
1275 reg
->Register
.Index
,
1279 if( reg
->Register
.Indirect
) {
1280 struct tgsi_ind_register
*ind
;
1282 if( maxsize
<= size
)
1284 ind
= (struct tgsi_ind_register
*) &tokens
[size
];
1287 *ind
= tgsi_build_ind_register(
1289 reg
->Indirect
.Swizzle
,
1290 reg
->Indirect
.Index
,
1291 reg
->Indirect
.ArrayID
,
1296 if( reg
->Register
.Dimension
) {
1297 struct tgsi_dimension
*dim
;
1299 assert( !reg
->Dimension
.Dimension
);
1301 if( maxsize
<= size
)
1303 dim
= (struct tgsi_dimension
*) &tokens
[size
];
1306 *dim
= tgsi_build_dimension(
1307 reg
->Dimension
.Indirect
,
1308 reg
->Dimension
.Index
,
1312 if( reg
->Dimension
.Indirect
) {
1313 struct tgsi_ind_register
*ind
;
1315 if( maxsize
<= size
)
1317 ind
= (struct tgsi_ind_register
*) &tokens
[size
];
1320 *ind
= tgsi_build_ind_register(
1321 reg
->DimIndirect
.File
,
1322 reg
->DimIndirect
.Swizzle
,
1323 reg
->DimIndirect
.Index
,
1324 reg
->DimIndirect
.ArrayID
,
1334 static struct tgsi_property
1335 tgsi_default_property( void )
1337 struct tgsi_property property
;
1339 property
.Type
= TGSI_TOKEN_TYPE_PROPERTY
;
1340 property
.NrTokens
= 1;
1341 property
.PropertyName
= TGSI_PROPERTY_GS_INPUT_PRIM
;
1342 property
.Padding
= 0;
1347 static struct tgsi_property
1348 tgsi_build_property(unsigned property_name
,
1349 struct tgsi_header
*header
)
1351 struct tgsi_property property
;
1353 property
= tgsi_default_property();
1354 property
.PropertyName
= property_name
;
1356 header_bodysize_grow( header
);
1362 struct tgsi_full_property
1363 tgsi_default_full_property( void )
1365 struct tgsi_full_property full_property
;
1367 full_property
.Property
= tgsi_default_property();
1368 memset(full_property
.u
, 0,
1369 sizeof(struct tgsi_property_data
) * 8);
1371 return full_property
;
1376 struct tgsi_property
*property
,
1377 struct tgsi_header
*header
)
1379 assert( property
->NrTokens
< 0xFF );
1381 property
->NrTokens
++;
1383 header_bodysize_grow( header
);
1386 static struct tgsi_property_data
1387 tgsi_build_property_data(
1389 struct tgsi_property
*property
,
1390 struct tgsi_header
*header
)
1392 struct tgsi_property_data property_data
;
1394 property_data
.Data
= value
;
1396 property_grow( property
, header
);
1398 return property_data
;
1402 tgsi_build_full_property(
1403 const struct tgsi_full_property
*full_prop
,
1404 struct tgsi_token
*tokens
,
1405 struct tgsi_header
*header
,
1408 unsigned size
= 0, i
;
1409 struct tgsi_property
*property
;
1411 if( maxsize
<= size
)
1413 property
= (struct tgsi_property
*) &tokens
[size
];
1416 *property
= tgsi_build_property(
1417 full_prop
->Property
.PropertyName
,
1420 assert( full_prop
->Property
.NrTokens
<= 8 + 1 );
1422 for( i
= 0; i
< full_prop
->Property
.NrTokens
- 1; i
++ ) {
1423 struct tgsi_property_data
*data
;
1425 if( maxsize
<= size
)
1427 data
= (struct tgsi_property_data
*) &tokens
[size
];
1430 *data
= tgsi_build_property_data(
1431 full_prop
->u
[i
].Data
,
1439 struct tgsi_full_src_register
1440 tgsi_full_src_register_from_dst(const struct tgsi_full_dst_register
*dst
)
1442 struct tgsi_full_src_register src
;
1443 src
.Register
= tgsi_default_src_register();
1444 src
.Register
.File
= dst
->Register
.File
;
1445 src
.Register
.Indirect
= dst
->Register
.Indirect
;
1446 src
.Register
.Dimension
= dst
->Register
.Dimension
;
1447 src
.Register
.Index
= dst
->Register
.Index
;
1448 src
.Indirect
= dst
->Indirect
;
1449 src
.Dimension
= dst
->Dimension
;
1450 src
.DimIndirect
= dst
->DimIndirect
;