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
.MemType
= TGSI_MEMORY_TYPE_GLOBAL
;
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
.MemType
= mem_type
;
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
.MemType
,
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(
796 struct tgsi_token
*prev_token
,
797 struct tgsi_instruction
*instruction
,
798 struct tgsi_header
*header
)
800 struct tgsi_instruction_memory instruction_memory
;
802 instruction_memory
.Qualifier
= qualifier
;
803 instruction_memory
.Texture
= texture
;
804 instruction_memory
.Format
= format
;
805 instruction_memory
.Padding
= 0;
806 instruction
->Memory
= 1;
808 instruction_grow( instruction
, header
);
810 return instruction_memory
;
813 static struct tgsi_texture_offset
814 tgsi_default_texture_offset( void )
816 struct tgsi_texture_offset texture_offset
;
818 texture_offset
.Index
= 0;
819 texture_offset
.File
= 0;
820 texture_offset
.SwizzleX
= 0;
821 texture_offset
.SwizzleY
= 0;
822 texture_offset
.SwizzleZ
= 0;
823 texture_offset
.Padding
= 0;
825 return texture_offset
;
828 static struct tgsi_texture_offset
829 tgsi_build_texture_offset(
830 int index
, int file
, int swizzle_x
, int swizzle_y
, int swizzle_z
,
831 struct tgsi_token
*prev_token
,
832 struct tgsi_instruction
*instruction
,
833 struct tgsi_header
*header
)
835 struct tgsi_texture_offset texture_offset
;
837 texture_offset
.Index
= index
;
838 texture_offset
.File
= file
;
839 texture_offset
.SwizzleX
= swizzle_x
;
840 texture_offset
.SwizzleY
= swizzle_y
;
841 texture_offset
.SwizzleZ
= swizzle_z
;
842 texture_offset
.Padding
= 0;
844 instruction_grow( instruction
, header
);
846 return texture_offset
;
849 static struct tgsi_src_register
850 tgsi_default_src_register( void )
852 struct tgsi_src_register src_register
;
854 src_register
.File
= TGSI_FILE_NULL
;
855 src_register
.SwizzleX
= TGSI_SWIZZLE_X
;
856 src_register
.SwizzleY
= TGSI_SWIZZLE_Y
;
857 src_register
.SwizzleZ
= TGSI_SWIZZLE_Z
;
858 src_register
.SwizzleW
= TGSI_SWIZZLE_W
;
859 src_register
.Negate
= 0;
860 src_register
.Absolute
= 0;
861 src_register
.Indirect
= 0;
862 src_register
.Dimension
= 0;
863 src_register
.Index
= 0;
868 static struct tgsi_src_register
869 tgsi_build_src_register(
880 struct tgsi_instruction
*instruction
,
881 struct tgsi_header
*header
)
883 struct tgsi_src_register src_register
;
885 assert( file
< TGSI_FILE_COUNT
);
886 assert( swizzle_x
<= TGSI_SWIZZLE_W
);
887 assert( swizzle_y
<= TGSI_SWIZZLE_W
);
888 assert( swizzle_z
<= TGSI_SWIZZLE_W
);
889 assert( swizzle_w
<= TGSI_SWIZZLE_W
);
890 assert( negate
<= 1 );
891 assert( index
>= -0x8000 && index
<= 0x7FFF );
893 src_register
.File
= file
;
894 src_register
.SwizzleX
= swizzle_x
;
895 src_register
.SwizzleY
= swizzle_y
;
896 src_register
.SwizzleZ
= swizzle_z
;
897 src_register
.SwizzleW
= swizzle_w
;
898 src_register
.Negate
= negate
;
899 src_register
.Absolute
= absolute
;
900 src_register
.Indirect
= indirect
;
901 src_register
.Dimension
= dimension
;
902 src_register
.Index
= index
;
904 instruction_grow( instruction
, header
);
909 static struct tgsi_ind_register
910 tgsi_default_ind_register( void )
912 struct tgsi_ind_register ind_register
;
914 ind_register
.File
= TGSI_FILE_NULL
;
915 ind_register
.Index
= 0;
916 ind_register
.Swizzle
= TGSI_SWIZZLE_X
;
917 ind_register
.ArrayID
= 0;
922 static struct tgsi_ind_register
923 tgsi_build_ind_register(
928 struct tgsi_instruction
*instruction
,
929 struct tgsi_header
*header
)
931 struct tgsi_ind_register ind_register
;
933 assert( file
< TGSI_FILE_COUNT
);
934 assert( swizzle
<= TGSI_SWIZZLE_W
);
935 assert( index
>= -0x8000 && index
<= 0x7FFF );
937 ind_register
.File
= file
;
938 ind_register
.Swizzle
= swizzle
;
939 ind_register
.Index
= index
;
940 ind_register
.ArrayID
= arrayid
;
942 instruction_grow( instruction
, header
);
947 static struct tgsi_dimension
948 tgsi_default_dimension( void )
950 struct tgsi_dimension dimension
;
952 dimension
.Indirect
= 0;
953 dimension
.Dimension
= 0;
954 dimension
.Padding
= 0;
960 static struct tgsi_full_src_register
961 tgsi_default_full_src_register( void )
963 struct tgsi_full_src_register full_src_register
;
965 full_src_register
.Register
= tgsi_default_src_register();
966 full_src_register
.Indirect
= tgsi_default_ind_register();
967 full_src_register
.Dimension
= tgsi_default_dimension();
968 full_src_register
.DimIndirect
= tgsi_default_ind_register();
970 return full_src_register
;
973 static struct tgsi_dimension
974 tgsi_build_dimension(
977 struct tgsi_instruction
*instruction
,
978 struct tgsi_header
*header
)
980 struct tgsi_dimension dimension
;
982 dimension
.Indirect
= indirect
;
983 dimension
.Dimension
= 0;
984 dimension
.Padding
= 0;
985 dimension
.Index
= index
;
987 instruction_grow( instruction
, header
);
992 static struct tgsi_dst_register
993 tgsi_default_dst_register( void )
995 struct tgsi_dst_register dst_register
;
997 dst_register
.File
= TGSI_FILE_NULL
;
998 dst_register
.WriteMask
= TGSI_WRITEMASK_XYZW
;
999 dst_register
.Indirect
= 0;
1000 dst_register
.Dimension
= 0;
1001 dst_register
.Index
= 0;
1002 dst_register
.Padding
= 0;
1004 return dst_register
;
1007 static struct tgsi_dst_register
1008 tgsi_build_dst_register(
1014 struct tgsi_instruction
*instruction
,
1015 struct tgsi_header
*header
)
1017 struct tgsi_dst_register dst_register
;
1019 assert( file
< TGSI_FILE_COUNT
);
1020 assert( mask
<= TGSI_WRITEMASK_XYZW
);
1021 assert( index
>= -32768 && index
<= 32767 );
1023 dst_register
.File
= file
;
1024 dst_register
.WriteMask
= mask
;
1025 dst_register
.Indirect
= indirect
;
1026 dst_register
.Dimension
= dimension
;
1027 dst_register
.Index
= index
;
1028 dst_register
.Padding
= 0;
1030 instruction_grow( instruction
, header
);
1032 return dst_register
;
1035 static struct tgsi_full_dst_register
1036 tgsi_default_full_dst_register( void )
1038 struct tgsi_full_dst_register full_dst_register
;
1040 full_dst_register
.Register
= tgsi_default_dst_register();
1041 full_dst_register
.Indirect
= tgsi_default_ind_register();
1042 full_dst_register
.Dimension
= tgsi_default_dimension();
1043 full_dst_register
.DimIndirect
= tgsi_default_ind_register();
1045 return full_dst_register
;
1048 struct tgsi_full_instruction
1049 tgsi_default_full_instruction( void )
1051 struct tgsi_full_instruction full_instruction
;
1054 full_instruction
.Instruction
= tgsi_default_instruction();
1055 full_instruction
.Predicate
= tgsi_default_instruction_predicate();
1056 full_instruction
.Label
= tgsi_default_instruction_label();
1057 full_instruction
.Texture
= tgsi_default_instruction_texture();
1058 full_instruction
.Memory
= tgsi_default_instruction_memory();
1059 for( i
= 0; i
< TGSI_FULL_MAX_TEX_OFFSETS
; i
++ ) {
1060 full_instruction
.TexOffsets
[i
] = tgsi_default_texture_offset();
1062 for( i
= 0; i
< TGSI_FULL_MAX_DST_REGISTERS
; i
++ ) {
1063 full_instruction
.Dst
[i
] = tgsi_default_full_dst_register();
1065 for( i
= 0; i
< TGSI_FULL_MAX_SRC_REGISTERS
; i
++ ) {
1066 full_instruction
.Src
[i
] = tgsi_default_full_src_register();
1069 return full_instruction
;
1073 tgsi_build_full_instruction(
1074 const struct tgsi_full_instruction
*full_inst
,
1075 struct tgsi_token
*tokens
,
1076 struct tgsi_header
*header
,
1081 struct tgsi_instruction
*instruction
;
1082 struct tgsi_token
*prev_token
;
1084 if( maxsize
<= size
)
1086 instruction
= (struct tgsi_instruction
*) &tokens
[size
];
1089 *instruction
= tgsi_build_instruction(full_inst
->Instruction
.Opcode
,
1090 full_inst
->Instruction
.Saturate
,
1091 full_inst
->Instruction
.Predicate
,
1092 full_inst
->Instruction
.NumDstRegs
,
1093 full_inst
->Instruction
.NumSrcRegs
,
1095 prev_token
= (struct tgsi_token
*) instruction
;
1097 if (full_inst
->Instruction
.Predicate
) {
1098 struct tgsi_instruction_predicate
*instruction_predicate
;
1100 if (maxsize
<= size
) {
1103 instruction_predicate
= (struct tgsi_instruction_predicate
*)&tokens
[size
];
1106 *instruction_predicate
=
1107 tgsi_build_instruction_predicate(full_inst
->Predicate
.Index
,
1108 full_inst
->Predicate
.Negate
,
1109 full_inst
->Predicate
.SwizzleX
,
1110 full_inst
->Predicate
.SwizzleY
,
1111 full_inst
->Predicate
.SwizzleZ
,
1112 full_inst
->Predicate
.SwizzleW
,
1117 if (full_inst
->Instruction
.Label
) {
1118 struct tgsi_instruction_label
*instruction_label
;
1120 if( maxsize
<= size
)
1123 (struct tgsi_instruction_label
*) &tokens
[size
];
1126 *instruction_label
= tgsi_build_instruction_label(
1127 full_inst
->Label
.Label
,
1131 prev_token
= (struct tgsi_token
*) instruction_label
;
1134 if (full_inst
->Instruction
.Texture
) {
1135 struct tgsi_instruction_texture
*instruction_texture
;
1137 if( maxsize
<= size
)
1139 instruction_texture
=
1140 (struct tgsi_instruction_texture
*) &tokens
[size
];
1143 *instruction_texture
= tgsi_build_instruction_texture(
1144 full_inst
->Texture
.Texture
,
1145 full_inst
->Texture
.NumOffsets
,
1149 prev_token
= (struct tgsi_token
*) instruction_texture
;
1151 for (i
= 0; i
< full_inst
->Texture
.NumOffsets
; i
++) {
1152 struct tgsi_texture_offset
*texture_offset
;
1154 if ( maxsize
<= size
)
1156 texture_offset
= (struct tgsi_texture_offset
*)&tokens
[size
];
1158 *texture_offset
= tgsi_build_texture_offset(
1159 full_inst
->TexOffsets
[i
].Index
,
1160 full_inst
->TexOffsets
[i
].File
,
1161 full_inst
->TexOffsets
[i
].SwizzleX
,
1162 full_inst
->TexOffsets
[i
].SwizzleY
,
1163 full_inst
->TexOffsets
[i
].SwizzleZ
,
1167 prev_token
= (struct tgsi_token
*) texture_offset
;
1171 if (full_inst
->Instruction
.Memory
) {
1172 struct tgsi_instruction_memory
*instruction_memory
;
1174 if( maxsize
<= size
)
1176 instruction_memory
=
1177 (struct tgsi_instruction_memory
*) &tokens
[size
];
1180 *instruction_memory
= tgsi_build_instruction_memory(
1181 full_inst
->Memory
.Qualifier
,
1182 full_inst
->Memory
.Texture
,
1183 full_inst
->Memory
.Format
,
1187 prev_token
= (struct tgsi_token
*) instruction_memory
;
1190 for( i
= 0; i
< full_inst
->Instruction
.NumDstRegs
; i
++ ) {
1191 const struct tgsi_full_dst_register
*reg
= &full_inst
->Dst
[i
];
1192 struct tgsi_dst_register
*dst_register
;
1194 if( maxsize
<= size
)
1196 dst_register
= (struct tgsi_dst_register
*) &tokens
[size
];
1199 *dst_register
= tgsi_build_dst_register(
1201 reg
->Register
.WriteMask
,
1202 reg
->Register
.Indirect
,
1203 reg
->Register
.Dimension
,
1204 reg
->Register
.Index
,
1208 if( reg
->Register
.Indirect
) {
1209 struct tgsi_ind_register
*ind
;
1211 if( maxsize
<= size
)
1213 ind
= (struct tgsi_ind_register
*) &tokens
[size
];
1216 *ind
= tgsi_build_ind_register(
1218 reg
->Indirect
.Swizzle
,
1219 reg
->Indirect
.Index
,
1220 reg
->Indirect
.ArrayID
,
1225 if( reg
->Register
.Dimension
) {
1226 struct tgsi_dimension
*dim
;
1228 assert( !reg
->Dimension
.Dimension
);
1230 if( maxsize
<= size
)
1232 dim
= (struct tgsi_dimension
*) &tokens
[size
];
1235 *dim
= tgsi_build_dimension(
1236 reg
->Dimension
.Indirect
,
1237 reg
->Dimension
.Index
,
1241 if( reg
->Dimension
.Indirect
) {
1242 struct tgsi_ind_register
*ind
;
1244 if( maxsize
<= size
)
1246 ind
= (struct tgsi_ind_register
*) &tokens
[size
];
1249 *ind
= tgsi_build_ind_register(
1250 reg
->DimIndirect
.File
,
1251 reg
->DimIndirect
.Swizzle
,
1252 reg
->DimIndirect
.Index
,
1253 reg
->DimIndirect
.ArrayID
,
1260 for( i
= 0; i
< full_inst
->Instruction
.NumSrcRegs
; i
++ ) {
1261 const struct tgsi_full_src_register
*reg
= &full_inst
->Src
[i
];
1262 struct tgsi_src_register
*src_register
;
1264 if( maxsize
<= size
)
1266 src_register
= (struct tgsi_src_register
*) &tokens
[size
];
1269 *src_register
= tgsi_build_src_register(
1271 reg
->Register
.SwizzleX
,
1272 reg
->Register
.SwizzleY
,
1273 reg
->Register
.SwizzleZ
,
1274 reg
->Register
.SwizzleW
,
1275 reg
->Register
.Negate
,
1276 reg
->Register
.Absolute
,
1277 reg
->Register
.Indirect
,
1278 reg
->Register
.Dimension
,
1279 reg
->Register
.Index
,
1283 if( reg
->Register
.Indirect
) {
1284 struct tgsi_ind_register
*ind
;
1286 if( maxsize
<= size
)
1288 ind
= (struct tgsi_ind_register
*) &tokens
[size
];
1291 *ind
= tgsi_build_ind_register(
1293 reg
->Indirect
.Swizzle
,
1294 reg
->Indirect
.Index
,
1295 reg
->Indirect
.ArrayID
,
1300 if( reg
->Register
.Dimension
) {
1301 struct tgsi_dimension
*dim
;
1303 assert( !reg
->Dimension
.Dimension
);
1305 if( maxsize
<= size
)
1307 dim
= (struct tgsi_dimension
*) &tokens
[size
];
1310 *dim
= tgsi_build_dimension(
1311 reg
->Dimension
.Indirect
,
1312 reg
->Dimension
.Index
,
1316 if( reg
->Dimension
.Indirect
) {
1317 struct tgsi_ind_register
*ind
;
1319 if( maxsize
<= size
)
1321 ind
= (struct tgsi_ind_register
*) &tokens
[size
];
1324 *ind
= tgsi_build_ind_register(
1325 reg
->DimIndirect
.File
,
1326 reg
->DimIndirect
.Swizzle
,
1327 reg
->DimIndirect
.Index
,
1328 reg
->DimIndirect
.ArrayID
,
1338 static struct tgsi_property
1339 tgsi_default_property( void )
1341 struct tgsi_property property
;
1343 property
.Type
= TGSI_TOKEN_TYPE_PROPERTY
;
1344 property
.NrTokens
= 1;
1345 property
.PropertyName
= TGSI_PROPERTY_GS_INPUT_PRIM
;
1346 property
.Padding
= 0;
1351 static struct tgsi_property
1352 tgsi_build_property(unsigned property_name
,
1353 struct tgsi_header
*header
)
1355 struct tgsi_property property
;
1357 property
= tgsi_default_property();
1358 property
.PropertyName
= property_name
;
1360 header_bodysize_grow( header
);
1366 struct tgsi_full_property
1367 tgsi_default_full_property( void )
1369 struct tgsi_full_property full_property
;
1371 full_property
.Property
= tgsi_default_property();
1372 memset(full_property
.u
, 0,
1373 sizeof(struct tgsi_property_data
) * 8);
1375 return full_property
;
1380 struct tgsi_property
*property
,
1381 struct tgsi_header
*header
)
1383 assert( property
->NrTokens
< 0xFF );
1385 property
->NrTokens
++;
1387 header_bodysize_grow( header
);
1390 static struct tgsi_property_data
1391 tgsi_build_property_data(
1393 struct tgsi_property
*property
,
1394 struct tgsi_header
*header
)
1396 struct tgsi_property_data property_data
;
1398 property_data
.Data
= value
;
1400 property_grow( property
, header
);
1402 return property_data
;
1406 tgsi_build_full_property(
1407 const struct tgsi_full_property
*full_prop
,
1408 struct tgsi_token
*tokens
,
1409 struct tgsi_header
*header
,
1412 unsigned size
= 0, i
;
1413 struct tgsi_property
*property
;
1415 if( maxsize
<= size
)
1417 property
= (struct tgsi_property
*) &tokens
[size
];
1420 *property
= tgsi_build_property(
1421 full_prop
->Property
.PropertyName
,
1424 assert( full_prop
->Property
.NrTokens
<= 8 + 1 );
1426 for( i
= 0; i
< full_prop
->Property
.NrTokens
- 1; i
++ ) {
1427 struct tgsi_property_data
*data
;
1429 if( maxsize
<= size
)
1431 data
= (struct tgsi_property_data
*) &tokens
[size
];
1434 *data
= tgsi_build_property_data(
1435 full_prop
->u
[i
].Data
,
1443 struct tgsi_full_src_register
1444 tgsi_full_src_register_from_dst(const struct tgsi_full_dst_register
*dst
)
1446 struct tgsi_full_src_register src
;
1447 src
.Register
= tgsi_default_src_register();
1448 src
.Register
.File
= dst
->Register
.File
;
1449 src
.Register
.Indirect
= dst
->Register
.Indirect
;
1450 src
.Register
.Dimension
= dst
->Register
.Dimension
;
1451 src
.Register
.Index
= dst
->Register
.Index
;
1452 src
.Indirect
= dst
->Indirect
;
1453 src
.Dimension
= dst
->Dimension
;
1454 src
.DimIndirect
= dst
->DimIndirect
;