1 /*
2 * Copyright 2009 Nicolai Hähnle <nhaehnle@gmail.com>
3 *
4 * Permission is hereby granted, free of charge, to any person obtaining a
5 * copy of this software and associated documentation files (the "Software"),
6 * to deal in the Software without restriction, including without limitation
7 * on the rights to use, copy, modify, merge, publish, distribute, sub
8 * license, and/or sell copies of the Software, and to permit persons to whom
9 * the Software is furnished to do so, subject to the following conditions:
10 *
11 * The above copyright notice and this permission notice (including the next
12 * paragraph) shall be included in all copies or substantial portions of the
13 * Software.
14 *
15 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17 * FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. IN NO EVENT SHALL
18 * THE COPYRIGHT HOLDER(S) AND/OR THEIR SUPPLIERS BE LIABLE FOR ANY CLAIM,
19 * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
20 * OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE
21 * USE OR OTHER DEALINGS IN THE SOFTWARE. */
22
23 #include <stdio.h>
24 #include "r300_tgsi_to_rc.h"
25
26 #include "compiler/radeon_compiler.h"
27
28 #include "tgsi/tgsi_info.h"
29 #include "tgsi/tgsi_parse.h"
30 #include "tgsi/tgsi_scan.h"
31 #include "tgsi/tgsi_util.h"
32
translate_opcode(unsigned opcode)33 static unsigned translate_opcode(unsigned opcode)
34 {
35 switch(opcode) {
36 case TGSI_OPCODE_ARL: return RC_OPCODE_ARL;
37 case TGSI_OPCODE_MOV: return RC_OPCODE_MOV;
38 case TGSI_OPCODE_LIT: return RC_OPCODE_LIT;
39 case TGSI_OPCODE_RCP: return RC_OPCODE_RCP;
40 case TGSI_OPCODE_RSQ: return RC_OPCODE_RSQ;
41 case TGSI_OPCODE_EXP: return RC_OPCODE_EXP;
42 case TGSI_OPCODE_LOG: return RC_OPCODE_LOG;
43 case TGSI_OPCODE_MUL: return RC_OPCODE_MUL;
44 case TGSI_OPCODE_ADD: return RC_OPCODE_ADD;
45 case TGSI_OPCODE_DP3: return RC_OPCODE_DP3;
46 case TGSI_OPCODE_DP4: return RC_OPCODE_DP4;
47 case TGSI_OPCODE_DST: return RC_OPCODE_DST;
48 case TGSI_OPCODE_MIN: return RC_OPCODE_MIN;
49 case TGSI_OPCODE_MAX: return RC_OPCODE_MAX;
50 case TGSI_OPCODE_SLT: return RC_OPCODE_SLT;
51 case TGSI_OPCODE_SGE: return RC_OPCODE_SGE;
52 case TGSI_OPCODE_MAD: return RC_OPCODE_MAD;
53 case TGSI_OPCODE_LRP: return RC_OPCODE_LRP;
54 case TGSI_OPCODE_FRC: return RC_OPCODE_FRC;
55 case TGSI_OPCODE_FLR: return RC_OPCODE_FLR;
56 case TGSI_OPCODE_ROUND: return RC_OPCODE_ROUND;
57 case TGSI_OPCODE_EX2: return RC_OPCODE_EX2;
58 case TGSI_OPCODE_LG2: return RC_OPCODE_LG2;
59 case TGSI_OPCODE_POW: return RC_OPCODE_POW;
60 case TGSI_OPCODE_COS: return RC_OPCODE_COS;
61 case TGSI_OPCODE_DDX: return RC_OPCODE_DDX;
62 case TGSI_OPCODE_DDY: return RC_OPCODE_DDY;
63 case TGSI_OPCODE_KILL: return RC_OPCODE_KILP;
64 /* case TGSI_OPCODE_PK2H: return RC_OPCODE_PK2H; */
65 /* case TGSI_OPCODE_PK2US: return RC_OPCODE_PK2US; */
66 /* case TGSI_OPCODE_PK4B: return RC_OPCODE_PK4B; */
67 /* case TGSI_OPCODE_PK4UB: return RC_OPCODE_PK4UB; */
68 case TGSI_OPCODE_SEQ: return RC_OPCODE_SEQ;
69 case TGSI_OPCODE_SGT: return RC_OPCODE_SGT;
70 case TGSI_OPCODE_SIN: return RC_OPCODE_SIN;
71 case TGSI_OPCODE_SLE: return RC_OPCODE_SLE;
72 case TGSI_OPCODE_SNE: return RC_OPCODE_SNE;
73 case TGSI_OPCODE_TEX: return RC_OPCODE_TEX;
74 case TGSI_OPCODE_TXD: return RC_OPCODE_TXD;
75 case TGSI_OPCODE_TXP: return RC_OPCODE_TXP;
76 /* case TGSI_OPCODE_UP2H: return RC_OPCODE_UP2H; */
77 /* case TGSI_OPCODE_UP2US: return RC_OPCODE_UP2US; */
78 /* case TGSI_OPCODE_UP4B: return RC_OPCODE_UP4B; */
79 /* case TGSI_OPCODE_UP4UB: return RC_OPCODE_UP4UB; */
80 case TGSI_OPCODE_ARR: return RC_OPCODE_ARR;
81 /* case TGSI_OPCODE_CAL: return RC_OPCODE_CAL; */
82 /* case TGSI_OPCODE_RET: return RC_OPCODE_RET; */
83 case TGSI_OPCODE_SSG: return RC_OPCODE_SSG;
84 case TGSI_OPCODE_CMP: return RC_OPCODE_CMP;
85 case TGSI_OPCODE_TXB: return RC_OPCODE_TXB;
86 /* case TGSI_OPCODE_DIV: return RC_OPCODE_DIV; */
87 case TGSI_OPCODE_DP2: return RC_OPCODE_DP2;
88 case TGSI_OPCODE_TXL: return RC_OPCODE_TXL;
89 case TGSI_OPCODE_BRK: return RC_OPCODE_BRK;
90 case TGSI_OPCODE_IF: return RC_OPCODE_IF;
91 case TGSI_OPCODE_BGNLOOP: return RC_OPCODE_BGNLOOP;
92 case TGSI_OPCODE_ELSE: return RC_OPCODE_ELSE;
93 case TGSI_OPCODE_ENDIF: return RC_OPCODE_ENDIF;
94 case TGSI_OPCODE_ENDLOOP: return RC_OPCODE_ENDLOOP;
95 /* case TGSI_OPCODE_PUSHA: return RC_OPCODE_PUSHA; */
96 /* case TGSI_OPCODE_POPA: return RC_OPCODE_POPA; */
97 case TGSI_OPCODE_CEIL: return RC_OPCODE_CEIL;
98 /* case TGSI_OPCODE_I2F: return RC_OPCODE_I2F; */
99 /* case TGSI_OPCODE_NOT: return RC_OPCODE_NOT; */
100 case TGSI_OPCODE_TRUNC: return RC_OPCODE_TRUNC;
101 /* case TGSI_OPCODE_SHL: return RC_OPCODE_SHL; */
102 /* case TGSI_OPCODE_ISHR: return RC_OPCODE_SHR; */
103 /* case TGSI_OPCODE_AND: return RC_OPCODE_AND; */
104 /* case TGSI_OPCODE_OR: return RC_OPCODE_OR; */
105 /* case TGSI_OPCODE_MOD: return RC_OPCODE_MOD; */
106 /* case TGSI_OPCODE_XOR: return RC_OPCODE_XOR; */
107 /* case TGSI_OPCODE_TXF: return RC_OPCODE_TXF; */
108 /* case TGSI_OPCODE_TXQ: return RC_OPCODE_TXQ; */
109 case TGSI_OPCODE_CONT: return RC_OPCODE_CONT;
110 /* case TGSI_OPCODE_EMIT: return RC_OPCODE_EMIT; */
111 /* case TGSI_OPCODE_ENDPRIM: return RC_OPCODE_ENDPRIM; */
112 /* case TGSI_OPCODE_BGNLOOP2: return RC_OPCODE_BGNLOOP2; */
113 /* case TGSI_OPCODE_BGNSUB: return RC_OPCODE_BGNSUB; */
114 /* case TGSI_OPCODE_ENDLOOP2: return RC_OPCODE_ENDLOOP2; */
115 /* case TGSI_OPCODE_ENDSUB: return RC_OPCODE_ENDSUB; */
116 case TGSI_OPCODE_NOP: return RC_OPCODE_NOP;
117 case TGSI_OPCODE_KILL_IF: return RC_OPCODE_KIL;
118 }
119
120 fprintf(stderr, "r300: Unknown TGSI/RC opcode: %s\n", tgsi_get_opcode_name(opcode));
121 return RC_OPCODE_ILLEGAL_OPCODE;
122 }
123
translate_saturate(unsigned saturate)124 static unsigned translate_saturate(unsigned saturate)
125 {
126 return saturate ? RC_SATURATE_ZERO_ONE : RC_SATURATE_NONE;
127 }
128
translate_register_file(unsigned file)129 static unsigned translate_register_file(unsigned file)
130 {
131 switch(file) {
132 case TGSI_FILE_CONSTANT: return RC_FILE_CONSTANT;
133 case TGSI_FILE_IMMEDIATE: return RC_FILE_CONSTANT;
134 case TGSI_FILE_INPUT: return RC_FILE_INPUT;
135 case TGSI_FILE_OUTPUT: return RC_FILE_OUTPUT;
136 default:
137 fprintf(stderr, "Unhandled register file: %i\n", file);
138 /* fall-through */
139 case TGSI_FILE_TEMPORARY: return RC_FILE_TEMPORARY;
140 case TGSI_FILE_ADDRESS: return RC_FILE_ADDRESS;
141 }
142 }
143
translate_register_index(struct tgsi_to_rc * ttr,unsigned file,int index)144 static int translate_register_index(
145 struct tgsi_to_rc * ttr,
146 unsigned file,
147 int index)
148 {
149 if (file == TGSI_FILE_IMMEDIATE)
150 return ttr->immediate_offset + index;
151
152 return index;
153 }
154
transform_dstreg(struct tgsi_to_rc * ttr,struct rc_dst_register * dst,struct tgsi_full_dst_register * src)155 static void transform_dstreg(
156 struct tgsi_to_rc * ttr,
157 struct rc_dst_register * dst,
158 struct tgsi_full_dst_register * src)
159 {
160 dst->File = translate_register_file(src->Register.File);
161 dst->Index = translate_register_index(ttr, src->Register.File, src->Register.Index);
162 dst->WriteMask = src->Register.WriteMask;
163
164 if (src->Register.Indirect) {
165 ttr->error = TRUE;
166 fprintf(stderr, "r300: Relative addressing of destination operands "
167 "is unsupported.\n");
168 }
169 }
170
transform_srcreg(struct tgsi_to_rc * ttr,struct rc_src_register * dst,struct tgsi_full_src_register * src)171 static void transform_srcreg(
172 struct tgsi_to_rc * ttr,
173 struct rc_src_register * dst,
174 struct tgsi_full_src_register * src)
175 {
176 unsigned i, j;
177
178 dst->File = translate_register_file(src->Register.File);
179 dst->Index = translate_register_index(ttr, src->Register.File, src->Register.Index);
180 dst->RelAddr = src->Register.Indirect;
181 dst->Swizzle = tgsi_util_get_full_src_register_swizzle(src, 0);
182 dst->Swizzle |= tgsi_util_get_full_src_register_swizzle(src, 1) << 3;
183 dst->Swizzle |= tgsi_util_get_full_src_register_swizzle(src, 2) << 6;
184 dst->Swizzle |= tgsi_util_get_full_src_register_swizzle(src, 3) << 9;
185 dst->Abs = src->Register.Absolute;
186 dst->Negate = src->Register.Negate ? RC_MASK_XYZW : 0;
187
188 if (src->Register.File == TGSI_FILE_IMMEDIATE) {
189 for (i = 0; i < ttr->imms_to_swizzle_count; i++) {
190 if (ttr->imms_to_swizzle[i].index == src->Register.Index) {
191 dst->File = RC_FILE_TEMPORARY;
192 dst->Index = 0;
193 dst->Swizzle = 0;
194 for (j = 0; j < 4; j++) {
195 dst->Swizzle |= GET_SWZ(ttr->imms_to_swizzle[i].swizzle,
196 tgsi_util_get_full_src_register_swizzle(src, j)) << (j * 3);
197 }
198 break;
199 }
200 }
201 }
202 }
203
transform_texture(struct rc_instruction * dst,struct tgsi_instruction_texture src,uint32_t * shadowSamplers)204 static void transform_texture(struct rc_instruction * dst, struct tgsi_instruction_texture src,
205 uint32_t *shadowSamplers)
206 {
207 switch(src.Texture) {
208 case TGSI_TEXTURE_1D:
209 dst->U.I.TexSrcTarget = RC_TEXTURE_1D;
210 break;
211 case TGSI_TEXTURE_2D:
212 dst->U.I.TexSrcTarget = RC_TEXTURE_2D;
213 break;
214 case TGSI_TEXTURE_3D:
215 dst->U.I.TexSrcTarget = RC_TEXTURE_3D;
216 break;
217 case TGSI_TEXTURE_CUBE:
218 dst->U.I.TexSrcTarget = RC_TEXTURE_CUBE;
219 break;
220 case TGSI_TEXTURE_RECT:
221 dst->U.I.TexSrcTarget = RC_TEXTURE_RECT;
222 break;
223 case TGSI_TEXTURE_SHADOW1D:
224 dst->U.I.TexSrcTarget = RC_TEXTURE_1D;
225 dst->U.I.TexShadow = 1;
226 *shadowSamplers |= 1 << dst->U.I.TexSrcUnit;
227 break;
228 case TGSI_TEXTURE_SHADOW2D:
229 dst->U.I.TexSrcTarget = RC_TEXTURE_2D;
230 dst->U.I.TexShadow = 1;
231 *shadowSamplers |= 1 << dst->U.I.TexSrcUnit;
232 break;
233 case TGSI_TEXTURE_SHADOWRECT:
234 dst->U.I.TexSrcTarget = RC_TEXTURE_RECT;
235 dst->U.I.TexShadow = 1;
236 *shadowSamplers |= 1 << dst->U.I.TexSrcUnit;
237 break;
238 }
239 dst->U.I.TexSwizzle = RC_SWIZZLE_XYZW;
240 }
241
transform_instruction(struct tgsi_to_rc * ttr,struct tgsi_full_instruction * src)242 static void transform_instruction(struct tgsi_to_rc * ttr, struct tgsi_full_instruction * src)
243 {
244 struct rc_instruction * dst;
245 int i;
246
247 dst = rc_insert_new_instruction(ttr->compiler, ttr->compiler->Program.Instructions.Prev);
248 dst->U.I.Opcode = translate_opcode(src->Instruction.Opcode);
249 dst->U.I.SaturateMode = translate_saturate(src->Instruction.Saturate);
250
251 if (src->Instruction.NumDstRegs)
252 transform_dstreg(ttr, &dst->U.I.DstReg, &src->Dst[0]);
253
254 for(i = 0; i < src->Instruction.NumSrcRegs; ++i) {
255 if (src->Src[i].Register.File == TGSI_FILE_SAMPLER)
256 dst->U.I.TexSrcUnit = src->Src[i].Register.Index;
257 else
258 transform_srcreg(ttr, &dst->U.I.SrcReg[i], &src->Src[i]);
259 }
260
261 /* Texturing. */
262 if (src->Instruction.Texture)
263 transform_texture(dst, src->Texture,
264 &ttr->compiler->Program.ShadowSamplers);
265 }
266
handle_immediate(struct tgsi_to_rc * ttr,struct tgsi_full_immediate * imm,unsigned index)267 static void handle_immediate(struct tgsi_to_rc * ttr,
268 struct tgsi_full_immediate * imm,
269 unsigned index)
270 {
271 struct rc_constant constant;
272 unsigned swizzle = 0;
273 boolean can_swizzle = TRUE;
274 unsigned i;
275
276 for (i = 0; i < 4; i++) {
277 if (imm->u[i].Float == 0.0f) {
278 swizzle |= RC_SWIZZLE_ZERO << (i * 3);
279 } else if (imm->u[i].Float == 0.5f && ttr->use_half_swizzles) {
280 swizzle |= RC_SWIZZLE_HALF << (i * 3);
281 } else if (imm->u[i].Float == 1.0f) {
282 swizzle |= RC_SWIZZLE_ONE << (i * 3);
283 } else {
284 can_swizzle = FALSE;
285 break;
286 }
287 }
288
289 if (can_swizzle) {
290 ttr->imms_to_swizzle[ttr->imms_to_swizzle_count].index = index;
291 ttr->imms_to_swizzle[ttr->imms_to_swizzle_count].swizzle = swizzle;
292 ttr->imms_to_swizzle_count++;
293 } else {
294 constant.Type = RC_CONSTANT_IMMEDIATE;
295 constant.Size = 4;
296 for(i = 0; i < 4; ++i)
297 constant.u.Immediate[i] = imm->u[i].Float;
298 rc_constants_add(&ttr->compiler->Program.Constants, &constant);
299 }
300 }
301
r300_tgsi_to_rc(struct tgsi_to_rc * ttr,const struct tgsi_token * tokens)302 void r300_tgsi_to_rc(struct tgsi_to_rc * ttr,
303 const struct tgsi_token * tokens)
304 {
305 struct tgsi_full_instruction *inst;
306 struct tgsi_parse_context parser;
307 unsigned imm_index = 0;
308 int i;
309
310 ttr->error = FALSE;
311
312 /* Allocate constants placeholders.
313 *
314 * Note: What if declared constants are not contiguous? */
315 for(i = 0; i <= ttr->info->file_max[TGSI_FILE_CONSTANT]; ++i) {
316 struct rc_constant constant;
317 memset(&constant, 0, sizeof(constant));
318 constant.Type = RC_CONSTANT_EXTERNAL;
319 constant.Size = 4;
320 constant.u.External = i;
321 rc_constants_add(&ttr->compiler->Program.Constants, &constant);
322 }
323
324 ttr->immediate_offset = ttr->compiler->Program.Constants.Count;
325
326 ttr->imms_to_swizzle = malloc(ttr->info->immediate_count * sizeof(struct swizzled_imms));
327 ttr->imms_to_swizzle_count = 0;
328
329 tgsi_parse_init(&parser, tokens);
330
331 while (!tgsi_parse_end_of_tokens(&parser)) {
332 tgsi_parse_token(&parser);
333
334 switch (parser.FullToken.Token.Type) {
335 case TGSI_TOKEN_TYPE_DECLARATION:
336 break;
337 case TGSI_TOKEN_TYPE_IMMEDIATE:
338 handle_immediate(ttr, &parser.FullToken.FullImmediate, imm_index);
339 imm_index++;
340 break;
341 case TGSI_TOKEN_TYPE_INSTRUCTION:
342 inst = &parser.FullToken.FullInstruction;
343 if (inst->Instruction.Opcode == TGSI_OPCODE_END) {
344 break;
345 }
346
347 transform_instruction(ttr, inst);
348 break;
349 }
350 }
351
352 tgsi_parse_free(&parser);
353
354 free(ttr->imms_to_swizzle);
355
356 rc_calculate_inputs_outputs(ttr->compiler);
357 }
358