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
33 #include "util/compiler.h"
34
translate_opcode(unsigned opcode)35 static unsigned translate_opcode(unsigned opcode)
36 {
37 switch(opcode) {
38 case TGSI_OPCODE_ARL: return RC_OPCODE_ARL;
39 case TGSI_OPCODE_MOV: return RC_OPCODE_MOV;
40 case TGSI_OPCODE_LIT: return RC_OPCODE_LIT;
41 case TGSI_OPCODE_RCP: return RC_OPCODE_RCP;
42 case TGSI_OPCODE_RSQ: return RC_OPCODE_RSQ;
43 case TGSI_OPCODE_EXP: return RC_OPCODE_EXP;
44 case TGSI_OPCODE_LOG: return RC_OPCODE_LOG;
45 case TGSI_OPCODE_MUL: return RC_OPCODE_MUL;
46 case TGSI_OPCODE_ADD: return RC_OPCODE_ADD;
47 case TGSI_OPCODE_DP3: return RC_OPCODE_DP3;
48 case TGSI_OPCODE_DP4: return RC_OPCODE_DP4;
49 case TGSI_OPCODE_DST: return RC_OPCODE_DST;
50 case TGSI_OPCODE_MIN: return RC_OPCODE_MIN;
51 case TGSI_OPCODE_MAX: return RC_OPCODE_MAX;
52 case TGSI_OPCODE_SLT: return RC_OPCODE_SLT;
53 case TGSI_OPCODE_SGE: return RC_OPCODE_SGE;
54 case TGSI_OPCODE_MAD: return RC_OPCODE_MAD;
55 case TGSI_OPCODE_LRP: return RC_OPCODE_LRP;
56 case TGSI_OPCODE_FRC: return RC_OPCODE_FRC;
57 case TGSI_OPCODE_FLR: return RC_OPCODE_FLR;
58 case TGSI_OPCODE_ROUND: return RC_OPCODE_ROUND;
59 case TGSI_OPCODE_EX2: return RC_OPCODE_EX2;
60 case TGSI_OPCODE_LG2: return RC_OPCODE_LG2;
61 case TGSI_OPCODE_POW: return RC_OPCODE_POW;
62 case TGSI_OPCODE_COS: return RC_OPCODE_COS;
63 case TGSI_OPCODE_DDX: return RC_OPCODE_DDX;
64 case TGSI_OPCODE_DDY: return RC_OPCODE_DDY;
65 case TGSI_OPCODE_KILL: return RC_OPCODE_KILP;
66 /* case TGSI_OPCODE_PK2H: return RC_OPCODE_PK2H; */
67 /* case TGSI_OPCODE_PK2US: return RC_OPCODE_PK2US; */
68 /* case TGSI_OPCODE_PK4B: return RC_OPCODE_PK4B; */
69 /* case TGSI_OPCODE_PK4UB: return RC_OPCODE_PK4UB; */
70 case TGSI_OPCODE_SEQ: return RC_OPCODE_SEQ;
71 case TGSI_OPCODE_SGT: return RC_OPCODE_SGT;
72 case TGSI_OPCODE_SIN: return RC_OPCODE_SIN;
73 case TGSI_OPCODE_SLE: return RC_OPCODE_SLE;
74 case TGSI_OPCODE_SNE: return RC_OPCODE_SNE;
75 case TGSI_OPCODE_TEX: return RC_OPCODE_TEX;
76 case TGSI_OPCODE_TXD: return RC_OPCODE_TXD;
77 case TGSI_OPCODE_TXP: return RC_OPCODE_TXP;
78 /* case TGSI_OPCODE_UP2H: return RC_OPCODE_UP2H; */
79 /* case TGSI_OPCODE_UP2US: return RC_OPCODE_UP2US; */
80 /* case TGSI_OPCODE_UP4B: return RC_OPCODE_UP4B; */
81 /* case TGSI_OPCODE_UP4UB: return RC_OPCODE_UP4UB; */
82 case TGSI_OPCODE_ARR: return RC_OPCODE_ARR;
83 /* case TGSI_OPCODE_CAL: return RC_OPCODE_CAL; */
84 /* case TGSI_OPCODE_RET: return RC_OPCODE_RET; */
85 case TGSI_OPCODE_SSG: return RC_OPCODE_SSG;
86 case TGSI_OPCODE_CMP: return RC_OPCODE_CMP;
87 case TGSI_OPCODE_TXB: return RC_OPCODE_TXB;
88 /* case TGSI_OPCODE_DIV: return RC_OPCODE_DIV; */
89 case TGSI_OPCODE_DP2: return RC_OPCODE_DP2;
90 case TGSI_OPCODE_TXL: return RC_OPCODE_TXL;
91 case TGSI_OPCODE_BRK: return RC_OPCODE_BRK;
92 case TGSI_OPCODE_IF: return RC_OPCODE_IF;
93 case TGSI_OPCODE_BGNLOOP: return RC_OPCODE_BGNLOOP;
94 case TGSI_OPCODE_ELSE: return RC_OPCODE_ELSE;
95 case TGSI_OPCODE_ENDIF: return RC_OPCODE_ENDIF;
96 case TGSI_OPCODE_ENDLOOP: return RC_OPCODE_ENDLOOP;
97 /* case TGSI_OPCODE_PUSHA: return RC_OPCODE_PUSHA; */
98 /* case TGSI_OPCODE_POPA: return RC_OPCODE_POPA; */
99 case TGSI_OPCODE_CEIL: return RC_OPCODE_CEIL;
100 /* case TGSI_OPCODE_I2F: return RC_OPCODE_I2F; */
101 /* case TGSI_OPCODE_NOT: return RC_OPCODE_NOT; */
102 case TGSI_OPCODE_TRUNC: return RC_OPCODE_TRUNC;
103 /* case TGSI_OPCODE_SHL: return RC_OPCODE_SHL; */
104 /* case TGSI_OPCODE_ISHR: return RC_OPCODE_SHR; */
105 /* case TGSI_OPCODE_AND: return RC_OPCODE_AND; */
106 /* case TGSI_OPCODE_OR: return RC_OPCODE_OR; */
107 /* case TGSI_OPCODE_MOD: return RC_OPCODE_MOD; */
108 /* case TGSI_OPCODE_XOR: return RC_OPCODE_XOR; */
109 /* case TGSI_OPCODE_TXF: return RC_OPCODE_TXF; */
110 /* case TGSI_OPCODE_TXQ: return RC_OPCODE_TXQ; */
111 case TGSI_OPCODE_CONT: return RC_OPCODE_CONT;
112 /* case TGSI_OPCODE_EMIT: return RC_OPCODE_EMIT; */
113 /* case TGSI_OPCODE_ENDPRIM: return RC_OPCODE_ENDPRIM; */
114 /* case TGSI_OPCODE_BGNLOOP2: return RC_OPCODE_BGNLOOP2; */
115 /* case TGSI_OPCODE_BGNSUB: return RC_OPCODE_BGNSUB; */
116 /* case TGSI_OPCODE_ENDLOOP2: return RC_OPCODE_ENDLOOP2; */
117 /* case TGSI_OPCODE_ENDSUB: return RC_OPCODE_ENDSUB; */
118 case TGSI_OPCODE_NOP: return RC_OPCODE_NOP;
119 case TGSI_OPCODE_KILL_IF: return RC_OPCODE_KIL;
120 }
121
122 fprintf(stderr, "r300: Unknown TGSI/RC opcode: %s\n", tgsi_get_opcode_name(opcode));
123 return RC_OPCODE_ILLEGAL_OPCODE;
124 }
125
translate_saturate(unsigned saturate)126 static unsigned translate_saturate(unsigned saturate)
127 {
128 return saturate ? RC_SATURATE_ZERO_ONE : RC_SATURATE_NONE;
129 }
130
translate_register_file(unsigned file)131 static unsigned translate_register_file(unsigned file)
132 {
133 switch(file) {
134 case TGSI_FILE_CONSTANT: return RC_FILE_CONSTANT;
135 case TGSI_FILE_IMMEDIATE: return RC_FILE_CONSTANT;
136 case TGSI_FILE_INPUT: return RC_FILE_INPUT;
137 case TGSI_FILE_OUTPUT: return RC_FILE_OUTPUT;
138 default:
139 fprintf(stderr, "Unhandled register file: %i\n", file);
140 FALLTHROUGH;
141 case TGSI_FILE_TEMPORARY: return RC_FILE_TEMPORARY;
142 case TGSI_FILE_ADDRESS: return RC_FILE_ADDRESS;
143 }
144 }
145
translate_register_index(struct tgsi_to_rc * ttr,unsigned file,int index)146 static int translate_register_index(
147 struct tgsi_to_rc * ttr,
148 unsigned file,
149 int index)
150 {
151 if (file == TGSI_FILE_IMMEDIATE)
152 return ttr->immediate_offset + index;
153
154 return index;
155 }
156
transform_dstreg(struct tgsi_to_rc * ttr,struct rc_dst_register * dst,struct tgsi_full_dst_register * src)157 static void transform_dstreg(
158 struct tgsi_to_rc * ttr,
159 struct rc_dst_register * dst,
160 struct tgsi_full_dst_register * src)
161 {
162 dst->File = translate_register_file(src->Register.File);
163 dst->Index = translate_register_index(ttr, src->Register.File, src->Register.Index);
164 dst->WriteMask = src->Register.WriteMask;
165
166 if (src->Register.Indirect) {
167 ttr->error = TRUE;
168 fprintf(stderr, "r300: Relative addressing of destination operands "
169 "is unsupported.\n");
170 }
171 }
172
transform_srcreg(struct tgsi_to_rc * ttr,struct rc_src_register * dst,struct tgsi_full_src_register * src)173 static void transform_srcreg(
174 struct tgsi_to_rc * ttr,
175 struct rc_src_register * dst,
176 struct tgsi_full_src_register * src)
177 {
178 unsigned i, j;
179
180 dst->File = translate_register_file(src->Register.File);
181 dst->Index = translate_register_index(ttr, src->Register.File, src->Register.Index);
182 dst->RelAddr = src->Register.Indirect;
183 dst->Swizzle = tgsi_util_get_full_src_register_swizzle(src, 0);
184 dst->Swizzle |= tgsi_util_get_full_src_register_swizzle(src, 1) << 3;
185 dst->Swizzle |= tgsi_util_get_full_src_register_swizzle(src, 2) << 6;
186 dst->Swizzle |= tgsi_util_get_full_src_register_swizzle(src, 3) << 9;
187 dst->Abs = src->Register.Absolute;
188 dst->Negate = src->Register.Negate ? RC_MASK_XYZW : 0;
189
190 if (src->Register.File == TGSI_FILE_IMMEDIATE) {
191 for (i = 0; i < ttr->imms_to_swizzle_count; i++) {
192 if (ttr->imms_to_swizzle[i].index == src->Register.Index) {
193 dst->File = RC_FILE_TEMPORARY;
194 dst->Index = 0;
195 dst->Swizzle = 0;
196 for (j = 0; j < 4; j++) {
197 dst->Swizzle |= GET_SWZ(ttr->imms_to_swizzle[i].swizzle,
198 tgsi_util_get_full_src_register_swizzle(src, j)) << (j * 3);
199 }
200 break;
201 }
202 }
203 }
204 }
205
transform_texture(struct rc_instruction * dst,struct tgsi_instruction_texture src,uint32_t * shadowSamplers)206 static void transform_texture(struct rc_instruction * dst, struct tgsi_instruction_texture src,
207 uint32_t *shadowSamplers)
208 {
209 switch(src.Texture) {
210 case TGSI_TEXTURE_1D:
211 dst->U.I.TexSrcTarget = RC_TEXTURE_1D;
212 break;
213 case TGSI_TEXTURE_2D:
214 dst->U.I.TexSrcTarget = RC_TEXTURE_2D;
215 break;
216 case TGSI_TEXTURE_3D:
217 dst->U.I.TexSrcTarget = RC_TEXTURE_3D;
218 break;
219 case TGSI_TEXTURE_CUBE:
220 dst->U.I.TexSrcTarget = RC_TEXTURE_CUBE;
221 break;
222 case TGSI_TEXTURE_RECT:
223 dst->U.I.TexSrcTarget = RC_TEXTURE_RECT;
224 break;
225 case TGSI_TEXTURE_SHADOW1D:
226 dst->U.I.TexSrcTarget = RC_TEXTURE_1D;
227 dst->U.I.TexShadow = 1;
228 *shadowSamplers |= 1U << dst->U.I.TexSrcUnit;
229 break;
230 case TGSI_TEXTURE_SHADOW2D:
231 dst->U.I.TexSrcTarget = RC_TEXTURE_2D;
232 dst->U.I.TexShadow = 1;
233 *shadowSamplers |= 1U << dst->U.I.TexSrcUnit;
234 break;
235 case TGSI_TEXTURE_SHADOWRECT:
236 dst->U.I.TexSrcTarget = RC_TEXTURE_RECT;
237 dst->U.I.TexShadow = 1;
238 *shadowSamplers |= 1U << dst->U.I.TexSrcUnit;
239 break;
240 case TGSI_TEXTURE_1D_ARRAY:
241 dst->U.I.TexSrcTarget = RC_TEXTURE_1D_ARRAY;
242 break;
243 case TGSI_TEXTURE_2D_ARRAY:
244 dst->U.I.TexSrcTarget = RC_TEXTURE_2D_ARRAY;
245 break;
246 case TGSI_TEXTURE_SHADOW1D_ARRAY:
247 dst->U.I.TexSrcTarget = RC_TEXTURE_1D_ARRAY;
248 dst->U.I.TexShadow = 1;
249 *shadowSamplers |= 1U << dst->U.I.TexSrcUnit;
250 break;
251 case TGSI_TEXTURE_SHADOW2D_ARRAY:
252 dst->U.I.TexSrcTarget = RC_TEXTURE_2D_ARRAY;
253 dst->U.I.TexShadow = 1;
254 *shadowSamplers |= 1U << dst->U.I.TexSrcUnit;
255 break;
256 case TGSI_TEXTURE_SHADOWCUBE:
257 dst->U.I.TexSrcTarget = RC_TEXTURE_CUBE;
258 dst->U.I.TexShadow = 1;
259 *shadowSamplers |= 1U << dst->U.I.TexSrcUnit;
260 break;
261 }
262 dst->U.I.TexSwizzle = RC_SWIZZLE_XYZW;
263 }
264
transform_instruction(struct tgsi_to_rc * ttr,struct tgsi_full_instruction * src)265 static void transform_instruction(struct tgsi_to_rc * ttr, struct tgsi_full_instruction * src)
266 {
267 struct rc_instruction * dst;
268 int i;
269
270 dst = rc_insert_new_instruction(ttr->compiler, ttr->compiler->Program.Instructions.Prev);
271 dst->U.I.Opcode = translate_opcode(src->Instruction.Opcode);
272 dst->U.I.SaturateMode = translate_saturate(src->Instruction.Saturate);
273
274 if (src->Instruction.NumDstRegs)
275 transform_dstreg(ttr, &dst->U.I.DstReg, &src->Dst[0]);
276
277 for(i = 0; i < src->Instruction.NumSrcRegs; ++i) {
278 if (src->Src[i].Register.File == TGSI_FILE_SAMPLER)
279 dst->U.I.TexSrcUnit = src->Src[i].Register.Index;
280 else
281 transform_srcreg(ttr, &dst->U.I.SrcReg[i], &src->Src[i]);
282 }
283
284 /* Texturing. */
285 if (src->Instruction.Texture)
286 transform_texture(dst, src->Texture,
287 &ttr->compiler->Program.ShadowSamplers);
288 }
289
handle_immediate(struct tgsi_to_rc * ttr,struct tgsi_full_immediate * imm,unsigned index)290 static void handle_immediate(struct tgsi_to_rc * ttr,
291 struct tgsi_full_immediate * imm,
292 unsigned index)
293 {
294 struct rc_constant constant;
295 unsigned swizzle = 0;
296 boolean can_swizzle = TRUE;
297 unsigned i;
298
299 for (i = 0; i < 4; i++) {
300 if (imm->u[i].Float == 0.0f) {
301 swizzle |= RC_SWIZZLE_ZERO << (i * 3);
302 } else if (imm->u[i].Float == 0.5f && ttr->use_half_swizzles) {
303 swizzle |= RC_SWIZZLE_HALF << (i * 3);
304 } else if (imm->u[i].Float == 1.0f) {
305 swizzle |= RC_SWIZZLE_ONE << (i * 3);
306 } else {
307 can_swizzle = FALSE;
308 break;
309 }
310 }
311
312 if (can_swizzle) {
313 ttr->imms_to_swizzle[ttr->imms_to_swizzle_count].index = index;
314 ttr->imms_to_swizzle[ttr->imms_to_swizzle_count].swizzle = swizzle;
315 ttr->imms_to_swizzle_count++;
316 } else {
317 constant.Type = RC_CONSTANT_IMMEDIATE;
318 constant.Size = 4;
319 for(i = 0; i < 4; ++i)
320 constant.u.Immediate[i] = imm->u[i].Float;
321 rc_constants_add(&ttr->compiler->Program.Constants, &constant);
322 }
323 }
324
r300_tgsi_to_rc(struct tgsi_to_rc * ttr,const struct tgsi_token * tokens)325 void r300_tgsi_to_rc(struct tgsi_to_rc * ttr,
326 const struct tgsi_token * tokens)
327 {
328 struct tgsi_full_instruction *inst;
329 struct tgsi_parse_context parser;
330 unsigned imm_index = 0;
331 int i;
332
333 ttr->error = FALSE;
334
335 /* Allocate constants placeholders.
336 *
337 * Note: What if declared constants are not contiguous? */
338 for(i = 0; i <= ttr->info->file_max[TGSI_FILE_CONSTANT]; ++i) {
339 struct rc_constant constant;
340 memset(&constant, 0, sizeof(constant));
341 constant.Type = RC_CONSTANT_EXTERNAL;
342 constant.Size = 4;
343 constant.u.External = i;
344 rc_constants_add(&ttr->compiler->Program.Constants, &constant);
345 }
346
347 ttr->immediate_offset = ttr->compiler->Program.Constants.Count;
348
349 ttr->imms_to_swizzle = malloc(ttr->info->immediate_count * sizeof(struct swizzled_imms));
350 ttr->imms_to_swizzle_count = 0;
351
352 tgsi_parse_init(&parser, tokens);
353
354 while (!tgsi_parse_end_of_tokens(&parser)) {
355 tgsi_parse_token(&parser);
356
357 switch (parser.FullToken.Token.Type) {
358 case TGSI_TOKEN_TYPE_DECLARATION:
359 break;
360 case TGSI_TOKEN_TYPE_IMMEDIATE:
361 handle_immediate(ttr, &parser.FullToken.FullImmediate, imm_index);
362 imm_index++;
363 break;
364 case TGSI_TOKEN_TYPE_INSTRUCTION:
365 inst = &parser.FullToken.FullInstruction;
366 if (inst->Instruction.Opcode == TGSI_OPCODE_END) {
367 break;
368 }
369
370 transform_instruction(ttr, inst);
371 break;
372 }
373 }
374
375 tgsi_parse_free(&parser);
376
377 free(ttr->imms_to_swizzle);
378
379 rc_calculate_inputs_outputs(ttr->compiler);
380 }
381