2 * Copyright 2009 Nicolai Hähnle <nhaehnle@gmail.com>
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:
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
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. */
23 #include "r300_tgsi_to_rc.h"
25 #include "radeon_compiler.h"
26 #include "radeon_program.h"
28 #include "tgsi/tgsi_info.h"
29 #include "tgsi/tgsi_parse.h"
30 #include "tgsi/tgsi_scan.h"
31 #include "tgsi/tgsi_util.h"
33 static unsigned translate_opcode(unsigned 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_SUB
: return RC_OPCODE_SUB
;
54 case TGSI_OPCODE_LRP
: return RC_OPCODE_LRP
;
55 /* case TGSI_OPCODE_CND: return RC_OPCODE_CND; */
56 /* case TGSI_OPCODE_CND0: return RC_OPCODE_CND0; */
57 /* case TGSI_OPCODE_DP2A: return RC_OPCODE_DP2A; */
59 case TGSI_OPCODE_FRC
: return RC_OPCODE_FRC
;
60 /* case TGSI_OPCODE_CLAMP: return RC_OPCODE_CLAMP; */
61 case TGSI_OPCODE_FLR
: return RC_OPCODE_FLR
;
62 /* case TGSI_OPCODE_ROUND: return RC_OPCODE_ROUND; */
63 case TGSI_OPCODE_EX2
: return RC_OPCODE_EX2
;
64 case TGSI_OPCODE_LG2
: return RC_OPCODE_LG2
;
65 case TGSI_OPCODE_POW
: return RC_OPCODE_POW
;
66 case TGSI_OPCODE_XPD
: return RC_OPCODE_XPD
;
68 case TGSI_OPCODE_ABS
: return RC_OPCODE_ABS
;
69 /* case TGSI_OPCODE_RCC: return RC_OPCODE_RCC; */
70 case TGSI_OPCODE_DPH
: return RC_OPCODE_DPH
;
71 case TGSI_OPCODE_COS
: return RC_OPCODE_COS
;
72 case TGSI_OPCODE_DDX
: return RC_OPCODE_DDX
;
73 case TGSI_OPCODE_DDY
: return RC_OPCODE_DDY
;
74 /* case TGSI_OPCODE_KILP: return RC_OPCODE_KILP; */
75 /* case TGSI_OPCODE_PK2H: return RC_OPCODE_PK2H; */
76 /* case TGSI_OPCODE_PK2US: return RC_OPCODE_PK2US; */
77 /* case TGSI_OPCODE_PK4B: return RC_OPCODE_PK4B; */
78 /* case TGSI_OPCODE_PK4UB: return RC_OPCODE_PK4UB; */
79 /* case TGSI_OPCODE_RFL: return RC_OPCODE_RFL; */
80 case TGSI_OPCODE_SEQ
: return RC_OPCODE_SEQ
;
81 case TGSI_OPCODE_SFL
: return RC_OPCODE_SFL
;
82 case TGSI_OPCODE_SGT
: return RC_OPCODE_SGT
;
83 case TGSI_OPCODE_SIN
: return RC_OPCODE_SIN
;
84 case TGSI_OPCODE_SLE
: return RC_OPCODE_SLE
;
85 case TGSI_OPCODE_SNE
: return RC_OPCODE_SNE
;
86 /* case TGSI_OPCODE_STR: return RC_OPCODE_STR; */
87 case TGSI_OPCODE_TEX
: return RC_OPCODE_TEX
;
88 case TGSI_OPCODE_TXD
: return RC_OPCODE_TXD
;
89 case TGSI_OPCODE_TXP
: return RC_OPCODE_TXP
;
90 /* case TGSI_OPCODE_UP2H: return RC_OPCODE_UP2H; */
91 /* case TGSI_OPCODE_UP2US: return RC_OPCODE_UP2US; */
92 /* case TGSI_OPCODE_UP4B: return RC_OPCODE_UP4B; */
93 /* case TGSI_OPCODE_UP4UB: return RC_OPCODE_UP4UB; */
94 /* case TGSI_OPCODE_X2D: return RC_OPCODE_X2D; */
95 /* case TGSI_OPCODE_ARA: return RC_OPCODE_ARA; */
96 /* case TGSI_OPCODE_ARR: return RC_OPCODE_ARR; */
97 /* case TGSI_OPCODE_BRA: return RC_OPCODE_BRA; */
98 /* case TGSI_OPCODE_CAL: return RC_OPCODE_CAL; */
99 /* case TGSI_OPCODE_RET: return RC_OPCODE_RET; */
100 /* case TGSI_OPCODE_SSG: return RC_OPCODE_SSG; */
101 case TGSI_OPCODE_CMP
: return RC_OPCODE_CMP
;
102 case TGSI_OPCODE_SCS
: return RC_OPCODE_SCS
;
103 case TGSI_OPCODE_TXB
: return RC_OPCODE_TXB
;
104 /* case TGSI_OPCODE_NRM: return RC_OPCODE_NRM; */
105 /* case TGSI_OPCODE_DIV: return RC_OPCODE_DIV; */
106 /* case TGSI_OPCODE_DP2: return RC_OPCODE_DP2; */
107 case TGSI_OPCODE_TXL
: return RC_OPCODE_TXL
;
108 case TGSI_OPCODE_BRK
: return RC_OPCODE_BRK
;
109 case TGSI_OPCODE_IF
: return RC_OPCODE_IF
;
110 case TGSI_OPCODE_BGNLOOP
: return RC_OPCODE_BGNLOOP
;
111 case TGSI_OPCODE_ELSE
: return RC_OPCODE_ELSE
;
112 case TGSI_OPCODE_ENDIF
: return RC_OPCODE_ENDIF
;
113 case TGSI_OPCODE_ENDLOOP
: return RC_OPCODE_ENDLOOP
;
114 /* case TGSI_OPCODE_PUSHA: return RC_OPCODE_PUSHA; */
115 /* case TGSI_OPCODE_POPA: return RC_OPCODE_POPA; */
116 case TGSI_OPCODE_CEIL
: return RC_OPCODE_CEIL
;
117 /* case TGSI_OPCODE_I2F: return RC_OPCODE_I2F; */
118 /* case TGSI_OPCODE_NOT: return RC_OPCODE_NOT; */
119 case TGSI_OPCODE_TRUNC
: return RC_OPCODE_FLR
;
120 /* case TGSI_OPCODE_SHL: return RC_OPCODE_SHL; */
121 /* case TGSI_OPCODE_ISHR: return RC_OPCODE_SHR; */
122 /* case TGSI_OPCODE_AND: return RC_OPCODE_AND; */
123 /* case TGSI_OPCODE_OR: return RC_OPCODE_OR; */
124 /* case TGSI_OPCODE_MOD: return RC_OPCODE_MOD; */
125 /* case TGSI_OPCODE_XOR: return RC_OPCODE_XOR; */
126 /* case TGSI_OPCODE_SAD: return RC_OPCODE_SAD; */
127 /* case TGSI_OPCODE_TXF: return RC_OPCODE_TXF; */
128 /* case TGSI_OPCODE_TXQ: return RC_OPCODE_TXQ; */
129 /* case TGSI_OPCODE_CONT: return RC_OPCODE_CONT; */
130 /* case TGSI_OPCODE_EMIT: return RC_OPCODE_EMIT; */
131 /* case TGSI_OPCODE_ENDPRIM: return RC_OPCODE_ENDPRIM; */
132 /* case TGSI_OPCODE_BGNLOOP2: return RC_OPCODE_BGNLOOP2; */
133 /* case TGSI_OPCODE_BGNSUB: return RC_OPCODE_BGNSUB; */
134 /* case TGSI_OPCODE_ENDLOOP2: return RC_OPCODE_ENDLOOP2; */
135 /* case TGSI_OPCODE_ENDSUB: return RC_OPCODE_ENDSUB; */
136 case TGSI_OPCODE_NOP
: return RC_OPCODE_NOP
;
138 /* case TGSI_OPCODE_NRM4: return RC_OPCODE_NRM4; */
139 /* case TGSI_OPCODE_CALLNZ: return RC_OPCODE_CALLNZ; */
140 /* case TGSI_OPCODE_IFC: return RC_OPCODE_IFC; */
141 /* case TGSI_OPCODE_BREAKC: return RC_OPCODE_BREAKC; */
142 case TGSI_OPCODE_KIL
: return RC_OPCODE_KIL
;
145 fprintf(stderr
, "r300: Unknown TGSI/RC opcode: %s\n", tgsi_get_opcode_name(opcode
));
146 return RC_OPCODE_ILLEGAL_OPCODE
;
149 static unsigned translate_saturate(unsigned saturate
)
153 fprintf(stderr
, "Unknown saturate mode: %i\n", saturate
);
155 case TGSI_SAT_NONE
: return RC_SATURATE_NONE
;
156 case TGSI_SAT_ZERO_ONE
: return RC_SATURATE_ZERO_ONE
;
160 static unsigned translate_register_file(unsigned file
)
163 case TGSI_FILE_CONSTANT
: return RC_FILE_CONSTANT
;
164 case TGSI_FILE_IMMEDIATE
: return RC_FILE_CONSTANT
;
165 case TGSI_FILE_INPUT
: return RC_FILE_INPUT
;
166 case TGSI_FILE_OUTPUT
: return RC_FILE_OUTPUT
;
168 fprintf(stderr
, "Unhandled register file: %i\n", file
);
170 case TGSI_FILE_TEMPORARY
: return RC_FILE_TEMPORARY
;
171 case TGSI_FILE_ADDRESS
: return RC_FILE_ADDRESS
;
175 static int translate_register_index(
176 struct tgsi_to_rc
* ttr
,
180 if (file
== TGSI_FILE_IMMEDIATE
)
181 return ttr
->immediate_offset
+ index
;
186 static void transform_dstreg(
187 struct tgsi_to_rc
* ttr
,
188 struct rc_dst_register
* dst
,
189 struct tgsi_full_dst_register
* src
)
191 dst
->File
= translate_register_file(src
->Register
.File
);
192 dst
->Index
= translate_register_index(ttr
, src
->Register
.File
, src
->Register
.Index
);
193 dst
->WriteMask
= src
->Register
.WriteMask
;
194 dst
->RelAddr
= src
->Register
.Indirect
;
197 static void transform_srcreg(
198 struct tgsi_to_rc
* ttr
,
199 struct rc_src_register
* dst
,
200 struct tgsi_full_src_register
* src
)
204 dst
->File
= translate_register_file(src
->Register
.File
);
205 dst
->Index
= translate_register_index(ttr
, src
->Register
.File
, src
->Register
.Index
);
206 dst
->RelAddr
= src
->Register
.Indirect
;
207 dst
->Swizzle
= tgsi_util_get_full_src_register_swizzle(src
, 0);
208 dst
->Swizzle
|= tgsi_util_get_full_src_register_swizzle(src
, 1) << 3;
209 dst
->Swizzle
|= tgsi_util_get_full_src_register_swizzle(src
, 2) << 6;
210 dst
->Swizzle
|= tgsi_util_get_full_src_register_swizzle(src
, 3) << 9;
211 dst
->Abs
= src
->Register
.Absolute
;
212 dst
->Negate
= src
->Register
.Negate
? RC_MASK_XYZW
: 0;
214 if (src
->Register
.File
== TGSI_FILE_IMMEDIATE
) {
215 for (i
= 0; i
< ttr
->imms_to_swizzle_count
; i
++) {
216 if (ttr
->imms_to_swizzle
[i
].index
== src
->Register
.Index
) {
217 dst
->File
= RC_FILE_TEMPORARY
;
220 for (j
= 0; j
< 4; j
++) {
221 dst
->Swizzle
|= GET_SWZ(ttr
->imms_to_swizzle
[i
].swizzle
,
222 tgsi_util_get_full_src_register_swizzle(src
, j
)) << (j
* 3);
230 static void transform_texture(struct rc_instruction
* dst
, struct tgsi_instruction_texture src
,
231 uint32_t *shadowSamplers
)
233 switch(src
.Texture
) {
234 case TGSI_TEXTURE_1D
:
235 dst
->U
.I
.TexSrcTarget
= RC_TEXTURE_1D
;
237 case TGSI_TEXTURE_2D
:
238 dst
->U
.I
.TexSrcTarget
= RC_TEXTURE_2D
;
240 case TGSI_TEXTURE_3D
:
241 dst
->U
.I
.TexSrcTarget
= RC_TEXTURE_3D
;
243 case TGSI_TEXTURE_CUBE
:
244 dst
->U
.I
.TexSrcTarget
= RC_TEXTURE_CUBE
;
246 case TGSI_TEXTURE_RECT
:
247 dst
->U
.I
.TexSrcTarget
= RC_TEXTURE_RECT
;
249 case TGSI_TEXTURE_SHADOW1D
:
250 dst
->U
.I
.TexSrcTarget
= RC_TEXTURE_1D
;
251 dst
->U
.I
.TexShadow
= 1;
252 *shadowSamplers
|= 1 << dst
->U
.I
.TexSrcUnit
;
254 case TGSI_TEXTURE_SHADOW2D
:
255 dst
->U
.I
.TexSrcTarget
= RC_TEXTURE_2D
;
256 dst
->U
.I
.TexShadow
= 1;
257 *shadowSamplers
|= 1 << dst
->U
.I
.TexSrcUnit
;
259 case TGSI_TEXTURE_SHADOWRECT
:
260 dst
->U
.I
.TexSrcTarget
= RC_TEXTURE_RECT
;
261 dst
->U
.I
.TexShadow
= 1;
262 *shadowSamplers
|= 1 << dst
->U
.I
.TexSrcUnit
;
267 static void transform_instruction(struct tgsi_to_rc
* ttr
, struct tgsi_full_instruction
* src
)
269 struct rc_instruction
* dst
;
272 dst
= rc_insert_new_instruction(ttr
->compiler
, ttr
->compiler
->Program
.Instructions
.Prev
);
273 dst
->U
.I
.Opcode
= translate_opcode(src
->Instruction
.Opcode
);
274 dst
->U
.I
.SaturateMode
= translate_saturate(src
->Instruction
.Saturate
);
276 if (src
->Instruction
.NumDstRegs
)
277 transform_dstreg(ttr
, &dst
->U
.I
.DstReg
, &src
->Dst
[0]);
279 for(i
= 0; i
< src
->Instruction
.NumSrcRegs
; ++i
) {
280 if (src
->Src
[i
].Register
.File
== TGSI_FILE_SAMPLER
)
281 dst
->U
.I
.TexSrcUnit
= src
->Src
[i
].Register
.Index
;
283 transform_srcreg(ttr
, &dst
->U
.I
.SrcReg
[i
], &src
->Src
[i
]);
287 if (src
->Instruction
.Texture
)
288 transform_texture(dst
, src
->Texture
,
289 &ttr
->compiler
->Program
.ShadowSamplers
);
292 static void handle_immediate(struct tgsi_to_rc
* ttr
,
293 struct tgsi_full_immediate
* imm
,
296 struct rc_constant constant
;
297 unsigned swizzle
= 0;
298 boolean can_swizzle
= TRUE
;
301 for (i
= 0; i
< 4; i
++) {
302 if (imm
->u
[i
].Float
== 0.0f
) {
303 swizzle
|= RC_SWIZZLE_ZERO
<< (i
* 3);
304 } else if (imm
->u
[i
].Float
== 0.5f
&& ttr
->use_half_swizzles
) {
305 swizzle
|= RC_SWIZZLE_HALF
<< (i
* 3);
306 } else if (imm
->u
[i
].Float
== 1.0f
) {
307 swizzle
|= RC_SWIZZLE_ONE
<< (i
* 3);
315 ttr
->imms_to_swizzle
[ttr
->imms_to_swizzle_count
].index
= index
;
316 ttr
->imms_to_swizzle
[ttr
->imms_to_swizzle_count
].swizzle
= swizzle
;
317 ttr
->imms_to_swizzle_count
++;
319 constant
.Type
= RC_CONSTANT_IMMEDIATE
;
321 for(i
= 0; i
< 4; ++i
)
322 constant
.u
.Immediate
[i
] = imm
->u
[i
].Float
;
323 rc_constants_add(&ttr
->compiler
->Program
.Constants
, &constant
);
327 void r300_tgsi_to_rc(struct tgsi_to_rc
* ttr
,
328 const struct tgsi_token
* tokens
)
330 struct tgsi_full_instruction
*inst
;
331 struct tgsi_parse_context parser
;
332 unsigned imm_index
= 0;
335 /* Allocate constants placeholders.
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
;
343 constant
.u
.External
= i
;
344 rc_constants_add(&ttr
->compiler
->Program
.Constants
, &constant
);
347 ttr
->immediate_offset
= ttr
->compiler
->Program
.Constants
.Count
;
349 ttr
->imms_to_swizzle
= malloc(ttr
->info
->immediate_count
* sizeof(struct swizzled_imms
));
350 ttr
->imms_to_swizzle_count
= 0;
352 tgsi_parse_init(&parser
, tokens
);
354 while (!tgsi_parse_end_of_tokens(&parser
)) {
355 tgsi_parse_token(&parser
);
357 switch (parser
.FullToken
.Token
.Type
) {
358 case TGSI_TOKEN_TYPE_DECLARATION
:
360 case TGSI_TOKEN_TYPE_IMMEDIATE
:
361 handle_immediate(ttr
, &parser
.FullToken
.FullImmediate
, imm_index
);
364 case TGSI_TOKEN_TYPE_INSTRUCTION
:
365 inst
= &parser
.FullToken
.FullInstruction
;
366 /* This hack with the RET opcode woudn't work with
368 if (inst
->Instruction
.Opcode
== TGSI_OPCODE_END
||
369 inst
->Instruction
.Opcode
== TGSI_OPCODE_RET
) {
373 transform_instruction(ttr
, inst
);
378 tgsi_parse_free(&parser
);
380 free(ttr
->imms_to_swizzle
);
382 rc_calculate_inputs_outputs(ttr
->compiler
);