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 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
);
84 * Apply user-defined transformations to the input shader to produce
86 * For example, a register search-and-replace operation could be applied
87 * by defining a transform_instruction() callback that examined and changed
88 * the instruction src/dest regs.
90 * \return number of tokens emitted
93 tgsi_transform_shader(const struct tgsi_token
*tokens_in
,
94 struct tgsi_token
*tokens_out
,
96 struct tgsi_transform_context
*ctx
)
101 struct tgsi_parse_context parse
;
104 struct tgsi_processor
*processor
;
108 ** callback context init
110 ctx
->emit_instruction
= emit_instruction
;
111 ctx
->emit_declaration
= emit_declaration
;
112 ctx
->emit_immediate
= emit_immediate
;
113 ctx
->tokens_out
= tokens_out
;
114 ctx
->max_tokens_out
= max_tokens_out
;
118 ** Setup to begin parsing input shader
120 if (tgsi_parse_init( &parse
, tokens_in
) != TGSI_PARSE_OK
) {
121 debug_printf("tgsi_parse_init() failed in tgsi_transform_shader()!\n");
124 procType
= parse
.FullHeader
.Processor
.Processor
;
125 assert(procType
== TGSI_PROCESSOR_FRAGMENT
||
126 procType
== TGSI_PROCESSOR_VERTEX
||
127 procType
== TGSI_PROCESSOR_GEOMETRY
);
131 ** Setup output shader
133 ctx
->header
= (struct tgsi_header
*)tokens_out
;
134 *ctx
->header
= tgsi_build_header();
136 processor
= (struct tgsi_processor
*) (tokens_out
+ 1);
137 *processor
= tgsi_build_processor( procType
, ctx
->header
);
143 ** Loop over incoming program tokens/instructions
145 while( !tgsi_parse_end_of_tokens( &parse
) ) {
147 tgsi_parse_token( &parse
);
149 switch( parse
.FullToken
.Token
.Type
) {
150 case TGSI_TOKEN_TYPE_INSTRUCTION
:
152 struct tgsi_full_instruction
*fullinst
153 = &parse
.FullToken
.FullInstruction
;
155 if (ctx
->transform_instruction
)
156 ctx
->transform_instruction(ctx
, fullinst
);
158 ctx
->emit_instruction(ctx
, fullinst
);
162 case TGSI_TOKEN_TYPE_DECLARATION
:
164 struct tgsi_full_declaration
*fulldecl
165 = &parse
.FullToken
.FullDeclaration
;
167 if (ctx
->transform_declaration
)
168 ctx
->transform_declaration(ctx
, fulldecl
);
170 ctx
->emit_declaration(ctx
, fulldecl
);
174 case TGSI_TOKEN_TYPE_IMMEDIATE
:
176 struct tgsi_full_immediate
*fullimm
177 = &parse
.FullToken
.FullImmediate
;
179 if (ctx
->transform_immediate
)
180 ctx
->transform_immediate(ctx
, fullimm
);
182 ctx
->emit_immediate(ctx
, fullimm
);
195 tgsi_parse_free (&parse
);
201 #include "tgsi_text.h"
203 extern int tgsi_transform_foo( struct tgsi_token
*tokens_out
,
204 uint max_tokens_out
);
206 /* This function exists only so that tgsi_text_translate() doesn't get
207 * magic-ed out of the libtgsi.a archive by the build system. Don't
208 * remove unless you know this has been fixed - check on mingw/scons
212 tgsi_transform_foo( struct tgsi_token
*tokens_out
,
213 uint max_tokens_out
)
217 "DCL IN[0], COLOR, CONSTANT\n"
218 "DCL OUT[0], COLOR\n"
219 " 0: MOV OUT[0], IN[0]\n"
222 return tgsi_text_translate( text
,