Merge commit 'origin/master' into gallium-0.2
[mesa.git] / src / gallium / auxiliary / tgsi / tgsi_transform.c
1 /**************************************************************************
2 *
3 * Copyright 2008 Tungsten Graphics, Inc., Cedar Park, Texas.
4 * All Rights Reserved.
5 *
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:
13 *
14 * The above copyright notice and this permission notice (including the
15 * next paragraph) shall be included in all copies or substantial portions
16 * of the Software.
17 *
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.
25 *
26 **************************************************************************/
27
28 /**
29 * TGSI program transformation utility.
30 *
31 * Authors: Brian Paul
32 */
33
34 #include "pipe/p_debug.h"
35
36 #include "tgsi_transform.h"
37
38
39
40 static void
41 emit_instruction(struct tgsi_transform_context *ctx,
42 const struct tgsi_full_instruction *inst)
43 {
44 uint ti = ctx->ti;
45
46 ti += tgsi_build_full_instruction(inst,
47 ctx->tokens_out + ti,
48 ctx->header,
49 ctx->max_tokens_out - ti);
50 ctx->ti = ti;
51 }
52
53
54 static void
55 emit_declaration(struct tgsi_transform_context *ctx,
56 const struct tgsi_full_declaration *decl)
57 {
58 uint ti = ctx->ti;
59
60 ti += tgsi_build_full_declaration(decl,
61 ctx->tokens_out + ti,
62 ctx->header,
63 ctx->max_tokens_out - ti);
64 ctx->ti = ti;
65 }
66
67
68 static void
69 emit_immediate(struct tgsi_transform_context *ctx,
70 const struct tgsi_full_immediate *imm)
71 {
72 uint ti = ctx->ti;
73
74 ti += tgsi_build_full_immediate(imm,
75 ctx->tokens_out + ti,
76 ctx->header,
77 ctx->max_tokens_out - ti);
78 ctx->ti = ti;
79 }
80
81
82
83 /**
84 * Apply user-defined transformations to the input shader to produce
85 * the output shader.
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.
89 *
90 * \return number of tokens emitted
91 */
92 int
93 tgsi_transform_shader(const struct tgsi_token *tokens_in,
94 struct tgsi_token *tokens_out,
95 uint max_tokens_out,
96 struct tgsi_transform_context *ctx)
97 {
98 uint procType;
99
100 /* input shader */
101 struct tgsi_parse_context parse;
102
103 /* output shader */
104 struct tgsi_processor *processor;
105
106
107 /**
108 ** callback context init
109 **/
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;
115
116
117 /**
118 ** Setup to begin parsing input shader
119 **/
120 if (tgsi_parse_init( &parse, tokens_in ) != TGSI_PARSE_OK) {
121 debug_printf("tgsi_parse_init() failed in tgsi_transform_shader()!\n");
122 return -1;
123 }
124 procType = parse.FullHeader.Processor.Processor;
125 assert(procType == TGSI_PROCESSOR_FRAGMENT ||
126 procType == TGSI_PROCESSOR_VERTEX ||
127 procType == TGSI_PROCESSOR_GEOMETRY);
128
129
130 /**
131 ** Setup output shader
132 **/
133 *(struct tgsi_version *) &tokens_out[0] = tgsi_build_version();
134
135 ctx->header = (struct tgsi_header *) (tokens_out + 1);
136 *ctx->header = tgsi_build_header();
137
138 processor = (struct tgsi_processor *) (tokens_out + 2);
139 *processor = tgsi_build_processor( procType, ctx->header );
140
141 ctx->ti = 3;
142
143
144 /**
145 ** Loop over incoming program tokens/instructions
146 */
147 while( !tgsi_parse_end_of_tokens( &parse ) ) {
148
149 tgsi_parse_token( &parse );
150
151 switch( parse.FullToken.Token.Type ) {
152 case TGSI_TOKEN_TYPE_INSTRUCTION:
153 {
154 struct tgsi_full_instruction *fullinst
155 = &parse.FullToken.FullInstruction;
156
157 if (ctx->transform_instruction)
158 ctx->transform_instruction(ctx, fullinst);
159 else
160 ctx->emit_instruction(ctx, fullinst);
161 }
162 break;
163
164 case TGSI_TOKEN_TYPE_DECLARATION:
165 {
166 struct tgsi_full_declaration *fulldecl
167 = &parse.FullToken.FullDeclaration;
168
169 if (ctx->transform_declaration)
170 ctx->transform_declaration(ctx, fulldecl);
171 else
172 ctx->emit_declaration(ctx, fulldecl);
173 }
174 break;
175
176 case TGSI_TOKEN_TYPE_IMMEDIATE:
177 {
178 struct tgsi_full_immediate *fullimm
179 = &parse.FullToken.FullImmediate;
180
181 if (ctx->transform_immediate)
182 ctx->transform_immediate(ctx, fullimm);
183 else
184 ctx->emit_immediate(ctx, fullimm);
185 }
186 break;
187
188 default:
189 assert( 0 );
190 }
191 }
192
193 if (ctx->epilog) {
194 ctx->epilog(ctx);
195 }
196
197 tgsi_parse_free (&parse);
198
199 return ctx->ti;
200 }