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.
35 #include "tgsi_transform.h"
40 emit_instruction(struct tgsi_transform_context
*ctx
,
41 const struct tgsi_full_instruction
*inst
)
45 ti
+= tgsi_build_full_instruction(inst
,
48 ctx
->max_tokens_out
- ti
);
54 emit_declaration(struct tgsi_transform_context
*ctx
,
55 const struct tgsi_full_declaration
*decl
)
59 ti
+= tgsi_build_full_declaration(decl
,
62 ctx
->max_tokens_out
- ti
);
68 emit_immediate(struct tgsi_transform_context
*ctx
,
69 const struct tgsi_full_immediate
*imm
)
73 ti
+= tgsi_build_full_immediate(imm
,
76 ctx
->max_tokens_out
- ti
);
83 * Apply user-defined transformations to the input shader to produce
85 * For example, a register search-and-replace operation could be applied
86 * by defining a transform_instruction() callback that examined and changed
87 * the instruction src/dest regs.
89 * \return number of tokens emitted
92 tgsi_transform_shader(const struct tgsi_token
*tokens_in
,
93 struct tgsi_token
*tokens_out
,
95 struct tgsi_transform_context
*ctx
)
100 struct tgsi_parse_context parse
;
103 struct tgsi_processor
*processor
;
107 ** callback context init
109 ctx
->emit_instruction
= emit_instruction
;
110 ctx
->emit_declaration
= emit_declaration
;
111 ctx
->emit_immediate
= emit_immediate
;
112 ctx
->tokens_out
= tokens_out
;
113 ctx
->max_tokens_out
= max_tokens_out
;
117 ** Setup to begin parsing input shader
119 if (tgsi_parse_init( &parse
, tokens_in
) != TGSI_PARSE_OK
) {
120 debug_printf("tgsi_parse_init() failed in tgsi_transform_shader()!\n");
123 procType
= parse
.FullHeader
.Processor
.Processor
;
124 assert(procType
== TGSI_PROCESSOR_FRAGMENT
||
125 procType
== TGSI_PROCESSOR_VERTEX
||
126 procType
== TGSI_PROCESSOR_GEOMETRY
);
130 ** Setup output shader
132 *(struct tgsi_version
*) &tokens_out
[0] = tgsi_build_version();
134 ctx
->header
= (struct tgsi_header
*) (tokens_out
+ 1);
135 *ctx
->header
= tgsi_build_header();
137 processor
= (struct tgsi_processor
*) (tokens_out
+ 2);
138 *processor
= tgsi_build_processor( procType
, ctx
->header
);
144 ** Loop over incoming program tokens/instructions
146 while( !tgsi_parse_end_of_tokens( &parse
) ) {
148 tgsi_parse_token( &parse
);
150 switch( parse
.FullToken
.Token
.Type
) {
151 case TGSI_TOKEN_TYPE_INSTRUCTION
:
153 struct tgsi_full_instruction
*fullinst
154 = &parse
.FullToken
.FullInstruction
;
156 if (ctx
->transform_instruction
)
157 ctx
->transform_instruction(ctx
, fullinst
);
159 ctx
->emit_instruction(ctx
, fullinst
);
163 case TGSI_TOKEN_TYPE_DECLARATION
:
165 struct tgsi_full_declaration
*fulldecl
166 = &parse
.FullToken
.FullDeclaration
;
168 if (ctx
->transform_declaration
)
169 ctx
->transform_declaration(ctx
, fulldecl
);
171 ctx
->emit_declaration(ctx
, fulldecl
);
175 case TGSI_TOKEN_TYPE_IMMEDIATE
:
177 struct tgsi_full_immediate
*fullimm
178 = &parse
.FullToken
.FullImmediate
;
180 if (ctx
->transform_immediate
)
181 ctx
->transform_immediate(ctx
, fullimm
);
183 ctx
->emit_immediate(ctx
, fullimm
);
196 tgsi_parse_free (&parse
);