1 /**************************************************************************
3 * Copyright 2008 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 **************************************************************************/
29 * TGSI program transformation utility.
34 #include "util/u_debug.h"
36 #include "tgsi_transform.h"
41 emit_instruction(struct tgsi_transform_context
*ctx
,
42 const struct tgsi_full_instruction
*inst
)
46 ti
+= tgsi_build_full_instruction(inst
,
49 ctx
->max_tokens_out
- ti
);
55 emit_declaration(struct tgsi_transform_context
*ctx
,
56 const struct tgsi_full_declaration
*decl
)
60 ti
+= tgsi_build_full_declaration(decl
,
63 ctx
->max_tokens_out
- ti
);
69 emit_immediate(struct tgsi_transform_context
*ctx
,
70 const struct tgsi_full_immediate
*imm
)
74 ti
+= tgsi_build_full_immediate(imm
,
77 ctx
->max_tokens_out
- ti
);
83 emit_property(struct tgsi_transform_context
*ctx
,
84 const struct tgsi_full_property
*prop
)
88 ti
+= tgsi_build_full_property(prop
,
91 ctx
->max_tokens_out
- ti
);
97 * Apply user-defined transformations to the input shader to produce
99 * For example, a register search-and-replace operation could be applied
100 * by defining a transform_instruction() callback that examined and changed
101 * the instruction src/dest regs.
103 * \return number of tokens emitted
106 tgsi_transform_shader(const struct tgsi_token
*tokens_in
,
107 struct tgsi_token
*tokens_out
,
109 struct tgsi_transform_context
*ctx
)
112 boolean first_instruction
= TRUE
;
113 boolean epilog_emitted
= FALSE
;
118 struct tgsi_parse_context parse
;
121 struct tgsi_processor
*processor
;
125 ** callback context init
127 ctx
->emit_instruction
= emit_instruction
;
128 ctx
->emit_declaration
= emit_declaration
;
129 ctx
->emit_immediate
= emit_immediate
;
130 ctx
->emit_property
= emit_property
;
131 ctx
->tokens_out
= tokens_out
;
132 ctx
->max_tokens_out
= max_tokens_out
;
136 ** Setup to begin parsing input shader
138 if (tgsi_parse_init( &parse
, tokens_in
) != TGSI_PARSE_OK
) {
139 debug_printf("tgsi_parse_init() failed in tgsi_transform_shader()!\n");
142 procType
= parse
.FullHeader
.Processor
.Processor
;
143 assert(procType
== PIPE_SHADER_FRAGMENT
||
144 procType
== PIPE_SHADER_VERTEX
||
145 procType
== PIPE_SHADER_GEOMETRY
);
149 ** Setup output shader
151 ctx
->header
= (struct tgsi_header
*)tokens_out
;
152 *ctx
->header
= tgsi_build_header();
154 processor
= (struct tgsi_processor
*) (tokens_out
+ 1);
155 *processor
= tgsi_build_processor( procType
, ctx
->header
);
161 ** Loop over incoming program tokens/instructions
163 while( !tgsi_parse_end_of_tokens( &parse
) ) {
165 tgsi_parse_token( &parse
);
167 switch( parse
.FullToken
.Token
.Type
) {
168 case TGSI_TOKEN_TYPE_INSTRUCTION
:
170 struct tgsi_full_instruction
*fullinst
171 = &parse
.FullToken
.FullInstruction
;
172 enum tgsi_opcode opcode
= fullinst
->Instruction
.Opcode
;
174 if (first_instruction
&& ctx
->prolog
) {
179 * XXX Note: we handle the case of ret in main.
180 * However, the output redirections done by transform
181 * have their limits with control flow and will generally
182 * not work correctly. e.g.
189 * If the color output is redirected to a temp and modified
190 * by a transform, this will not work (the oColor assignment
191 * in the conditional will never make it to the actual output).
193 if ((opcode
== TGSI_OPCODE_END
|| opcode
== TGSI_OPCODE_RET
) &&
194 call_stack
== 0 && ctx
->epilog
&& !epilog_emitted
) {
195 if (opcode
== TGSI_OPCODE_RET
&& cond_stack
!= 0) {
196 assert(!"transform ignoring RET in main");
198 assert(cond_stack
== 0);
199 /* Emit caller's epilog */
201 epilog_emitted
= TRUE
;
203 /* Emit END (or RET) */
204 ctx
->emit_instruction(ctx
, fullinst
);
209 case TGSI_OPCODE_UIF
:
210 case TGSI_OPCODE_SWITCH
:
211 case TGSI_OPCODE_BGNLOOP
:
214 case TGSI_OPCODE_CAL
:
217 case TGSI_OPCODE_ENDIF
:
218 case TGSI_OPCODE_ENDSWITCH
:
219 case TGSI_OPCODE_ENDLOOP
:
220 assert(cond_stack
> 0);
223 case TGSI_OPCODE_ENDSUB
:
224 assert(call_stack
> 0);
227 case TGSI_OPCODE_BGNSUB
:
228 case TGSI_OPCODE_RET
:
232 if (ctx
->transform_instruction
)
233 ctx
->transform_instruction(ctx
, fullinst
);
235 ctx
->emit_instruction(ctx
, fullinst
);
238 first_instruction
= FALSE
;
242 case TGSI_TOKEN_TYPE_DECLARATION
:
244 struct tgsi_full_declaration
*fulldecl
245 = &parse
.FullToken
.FullDeclaration
;
247 if (ctx
->transform_declaration
)
248 ctx
->transform_declaration(ctx
, fulldecl
);
250 ctx
->emit_declaration(ctx
, fulldecl
);
254 case TGSI_TOKEN_TYPE_IMMEDIATE
:
256 struct tgsi_full_immediate
*fullimm
257 = &parse
.FullToken
.FullImmediate
;
259 if (ctx
->transform_immediate
)
260 ctx
->transform_immediate(ctx
, fullimm
);
262 ctx
->emit_immediate(ctx
, fullimm
);
265 case TGSI_TOKEN_TYPE_PROPERTY
:
267 struct tgsi_full_property
*fullprop
268 = &parse
.FullToken
.FullProperty
;
270 if (ctx
->transform_property
)
271 ctx
->transform_property(ctx
, fullprop
);
273 ctx
->emit_property(ctx
, fullprop
);
281 assert(call_stack
== 0);
283 tgsi_parse_free (&parse
);
289 #include "tgsi_text.h"
291 extern int tgsi_transform_foo( struct tgsi_token
*tokens_out
,
292 uint max_tokens_out
);
294 /* This function exists only so that tgsi_text_translate() doesn't get
295 * magic-ed out of the libtgsi.a archive by the build system. Don't
296 * remove unless you know this has been fixed - check on mingw/scons
300 tgsi_transform_foo( struct tgsi_token
*tokens_out
,
301 uint max_tokens_out
)
305 "DCL IN[0], COLOR, CONSTANT\n"
306 "DCL OUT[0], COLOR\n"
307 " 0: MOV OUT[0], IN[0]\n"
310 return tgsi_text_translate( text
,