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