1 /**************************************************************************
3 * Copyright 2008 Tungsten Graphics, Inc., Cedar Park, Texas.
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 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.
26 **************************************************************************/
29 * TGSI program scan utility.
30 * Used to determine which registers and instructions are used by a shader.
36 #include "util/u_math.h"
37 #include "tgsi/tgsi_build.h"
38 #include "tgsi/tgsi_parse.h"
39 #include "tgsi/tgsi_scan.h"
47 tgsi_scan_shader(const struct tgsi_token
*tokens
,
48 struct tgsi_shader_info
*info
)
51 struct tgsi_parse_context parse
;
53 memset(info
, 0, sizeof(*info
));
54 for (i
= 0; i
< TGSI_FILE_COUNT
; i
++)
55 info
->file_max
[i
] = -1;
58 ** Setup to begin parsing input shader
60 if (tgsi_parse_init( &parse
, tokens
) != TGSI_PARSE_OK
) {
61 debug_printf("tgsi_parse_init() failed in tgsi_scan_shader()!\n");
64 procType
= parse
.FullHeader
.Processor
.Processor
;
65 assert(procType
== TGSI_PROCESSOR_FRAGMENT
||
66 procType
== TGSI_PROCESSOR_VERTEX
||
67 procType
== TGSI_PROCESSOR_GEOMETRY
);
71 ** Loop over incoming program tokens/instructions
73 while( !tgsi_parse_end_of_tokens( &parse
) ) {
77 tgsi_parse_token( &parse
);
79 switch( parse
.FullToken
.Token
.Type
) {
80 case TGSI_TOKEN_TYPE_INSTRUCTION
:
82 struct tgsi_full_instruction
*fullinst
83 = &parse
.FullToken
.FullInstruction
;
85 assert(fullinst
->Instruction
.Opcode
< TGSI_OPCODE_LAST
);
86 info
->opcode_count
[fullinst
->Instruction
.Opcode
]++;
90 case TGSI_TOKEN_TYPE_DECLARATION
:
92 struct tgsi_full_declaration
*fulldecl
93 = &parse
.FullToken
.FullDeclaration
;
94 uint file
= fulldecl
->Declaration
.File
;
96 for (i
= fulldecl
->DeclarationRange
.First
;
97 i
<= fulldecl
->DeclarationRange
.Last
;
100 /* only first 32 regs will appear in this bitfield */
101 info
->file_mask
[file
] |= (1 << i
);
102 info
->file_count
[file
]++;
103 info
->file_max
[file
] = MAX2(info
->file_max
[file
], (int)i
);
105 if (file
== TGSI_FILE_INPUT
) {
106 info
->input_semantic_name
[i
] = (ubyte
)fulldecl
->Semantic
.SemanticName
;
107 info
->input_semantic_index
[i
] = (ubyte
)fulldecl
->Semantic
.SemanticIndex
;
111 if (file
== TGSI_FILE_OUTPUT
) {
112 info
->output_semantic_name
[i
] = (ubyte
)fulldecl
->Semantic
.SemanticName
;
113 info
->output_semantic_index
[i
] = (ubyte
)fulldecl
->Semantic
.SemanticIndex
;
118 if (procType
== TGSI_PROCESSOR_FRAGMENT
&&
119 file
== TGSI_FILE_OUTPUT
&&
120 fulldecl
->Semantic
.SemanticName
== TGSI_SEMANTIC_POSITION
) {
121 info
->writes_z
= TRUE
;
127 case TGSI_TOKEN_TYPE_IMMEDIATE
:
128 info
->immediate_count
++;
136 assert( info
->file_max
[TGSI_FILE_INPUT
] + 1 == info
->num_inputs
);
137 assert( info
->file_max
[TGSI_FILE_OUTPUT
] + 1 == info
->num_outputs
);
139 info
->uses_kill
= (info
->opcode_count
[TGSI_OPCODE_KIL
] ||
140 info
->opcode_count
[TGSI_OPCODE_KILP
]);
142 tgsi_parse_free (&parse
);
148 * Check if the given shader is a "passthrough" shader consisting of only
149 * MOV instructions of the form: MOV OUT[n], IN[n]
153 tgsi_is_passthrough_shader(const struct tgsi_token
*tokens
)
155 struct tgsi_parse_context parse
;
158 ** Setup to begin parsing input shader
160 if (tgsi_parse_init(&parse
, tokens
) != TGSI_PARSE_OK
) {
161 debug_printf("tgsi_parse_init() failed in tgsi_is_passthrough_shader()!\n");
166 ** Loop over incoming program tokens/instructions
168 while (!tgsi_parse_end_of_tokens(&parse
)) {
170 tgsi_parse_token(&parse
);
172 switch (parse
.FullToken
.Token
.Type
) {
173 case TGSI_TOKEN_TYPE_INSTRUCTION
:
175 struct tgsi_full_instruction
*fullinst
=
176 &parse
.FullToken
.FullInstruction
;
177 const struct tgsi_full_src_register
*src
=
178 &fullinst
->FullSrcRegisters
[0];
179 const struct tgsi_full_dst_register
*dst
=
180 &fullinst
->FullDstRegisters
[0];
182 /* Do a whole bunch of checks for a simple move */
183 if (fullinst
->Instruction
.Opcode
!= TGSI_OPCODE_MOV
||
184 src
->SrcRegister
.File
!= TGSI_FILE_INPUT
||
185 dst
->DstRegister
.File
!= TGSI_FILE_OUTPUT
||
186 src
->SrcRegister
.Index
!= dst
->DstRegister
.Index
||
188 src
->SrcRegister
.Negate
||
189 src
->SrcRegisterExtMod
.Negate
||
190 src
->SrcRegisterExtMod
.Absolute
||
191 src
->SrcRegisterExtMod
.Scale2X
||
192 src
->SrcRegisterExtMod
.Bias
||
193 src
->SrcRegisterExtMod
.Complement
||
195 src
->SrcRegister
.SwizzleX
!= TGSI_SWIZZLE_X
||
196 src
->SrcRegister
.SwizzleY
!= TGSI_SWIZZLE_Y
||
197 src
->SrcRegister
.SwizzleZ
!= TGSI_SWIZZLE_Z
||
198 src
->SrcRegister
.SwizzleW
!= TGSI_SWIZZLE_W
||
200 src
->SrcRegisterExtSwz
.ExtSwizzleX
!= TGSI_EXTSWIZZLE_X
||
201 src
->SrcRegisterExtSwz
.ExtSwizzleY
!= TGSI_EXTSWIZZLE_Y
||
202 src
->SrcRegisterExtSwz
.ExtSwizzleZ
!= TGSI_EXTSWIZZLE_Z
||
203 src
->SrcRegisterExtSwz
.ExtSwizzleW
!= TGSI_EXTSWIZZLE_W
||
205 dst
->DstRegister
.WriteMask
!= TGSI_WRITEMASK_XYZW
)
207 tgsi_parse_free(&parse
);
213 case TGSI_TOKEN_TYPE_DECLARATION
:
215 case TGSI_TOKEN_TYPE_IMMEDIATE
:
222 tgsi_parse_free(&parse
);
224 /* if we get here, it's a pass-through shader */