r300/compiler: fix LIT instruction case 0^0 = 1
[mesa.git] / src / mesa / drivers / dri / r300 / compiler / radeon_program_alu.c
1 /*
2 * Copyright (C) 2008 Nicolai Haehnle.
3 *
4 * All Rights Reserved.
5 *
6 * Permission is hereby granted, free of charge, to any person obtaining
7 * a 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, sublicense, 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
16 * portions of the Software.
17 *
18 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
19 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
20 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
21 * IN NO EVENT SHALL THE COPYRIGHT OWNER(S) AND/OR ITS SUPPLIERS BE
22 * LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
23 * OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
24 * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
25 *
26 */
27
28 /**
29 * @file
30 *
31 * Shareable transformations that transform "special" ALU instructions
32 * into ALU instructions that are supported by hardware.
33 *
34 */
35
36 #include "radeon_program_alu.h"
37
38 #include "radeon_compiler.h"
39
40
41 static struct rc_instruction *emit1(
42 struct radeon_compiler * c, struct rc_instruction * after,
43 rc_opcode Opcode, rc_saturate_mode Saturate, struct rc_dst_register DstReg,
44 struct rc_src_register SrcReg)
45 {
46 struct rc_instruction *fpi = rc_insert_new_instruction(c, after);
47
48 fpi->U.I.Opcode = Opcode;
49 fpi->U.I.SaturateMode = Saturate;
50 fpi->U.I.DstReg = DstReg;
51 fpi->U.I.SrcReg[0] = SrcReg;
52 return fpi;
53 }
54
55 static struct rc_instruction *emit2(
56 struct radeon_compiler * c, struct rc_instruction * after,
57 rc_opcode Opcode, rc_saturate_mode Saturate, struct rc_dst_register DstReg,
58 struct rc_src_register SrcReg0, struct rc_src_register SrcReg1)
59 {
60 struct rc_instruction *fpi = rc_insert_new_instruction(c, after);
61
62 fpi->U.I.Opcode = Opcode;
63 fpi->U.I.SaturateMode = Saturate;
64 fpi->U.I.DstReg = DstReg;
65 fpi->U.I.SrcReg[0] = SrcReg0;
66 fpi->U.I.SrcReg[1] = SrcReg1;
67 return fpi;
68 }
69
70 static struct rc_instruction *emit3(
71 struct radeon_compiler * c, struct rc_instruction * after,
72 rc_opcode Opcode, rc_saturate_mode Saturate, struct rc_dst_register DstReg,
73 struct rc_src_register SrcReg0, struct rc_src_register SrcReg1,
74 struct rc_src_register SrcReg2)
75 {
76 struct rc_instruction *fpi = rc_insert_new_instruction(c, after);
77
78 fpi->U.I.Opcode = Opcode;
79 fpi->U.I.SaturateMode = Saturate;
80 fpi->U.I.DstReg = DstReg;
81 fpi->U.I.SrcReg[0] = SrcReg0;
82 fpi->U.I.SrcReg[1] = SrcReg1;
83 fpi->U.I.SrcReg[2] = SrcReg2;
84 return fpi;
85 }
86
87 static struct rc_dst_register dstreg(int file, int index)
88 {
89 struct rc_dst_register dst;
90 dst.File = file;
91 dst.Index = index;
92 dst.WriteMask = RC_MASK_XYZW;
93 dst.RelAddr = 0;
94 return dst;
95 }
96
97 static struct rc_dst_register dstregtmpmask(int index, int mask)
98 {
99 struct rc_dst_register dst = {0};
100 dst.File = RC_FILE_TEMPORARY;
101 dst.Index = index;
102 dst.WriteMask = mask;
103 dst.RelAddr = 0;
104 return dst;
105 }
106
107 static const struct rc_src_register builtin_zero = {
108 .File = RC_FILE_NONE,
109 .Index = 0,
110 .Swizzle = RC_SWIZZLE_0000
111 };
112 static const struct rc_src_register builtin_one = {
113 .File = RC_FILE_NONE,
114 .Index = 0,
115 .Swizzle = RC_SWIZZLE_1111
116 };
117 static const struct rc_src_register srcreg_undefined = {
118 .File = RC_FILE_NONE,
119 .Index = 0,
120 .Swizzle = RC_SWIZZLE_XYZW
121 };
122
123 static struct rc_src_register srcreg(int file, int index)
124 {
125 struct rc_src_register src = srcreg_undefined;
126 src.File = file;
127 src.Index = index;
128 return src;
129 }
130
131 static struct rc_src_register srcregswz(int file, int index, int swz)
132 {
133 struct rc_src_register src = srcreg_undefined;
134 src.File = file;
135 src.Index = index;
136 src.Swizzle = swz;
137 return src;
138 }
139
140 static struct rc_src_register absolute(struct rc_src_register reg)
141 {
142 struct rc_src_register newreg = reg;
143 newreg.Abs = 1;
144 newreg.Negate = RC_MASK_NONE;
145 return newreg;
146 }
147
148 static struct rc_src_register negate(struct rc_src_register reg)
149 {
150 struct rc_src_register newreg = reg;
151 newreg.Negate = newreg.Negate ^ RC_MASK_XYZW;
152 return newreg;
153 }
154
155 static struct rc_src_register swizzle(struct rc_src_register reg,
156 rc_swizzle x, rc_swizzle y, rc_swizzle z, rc_swizzle w)
157 {
158 struct rc_src_register swizzled = reg;
159 swizzled.Swizzle = combine_swizzles4(reg.Swizzle, x, y, z, w);
160 return swizzled;
161 }
162
163 static struct rc_src_register scalar(struct rc_src_register reg)
164 {
165 return swizzle(reg, RC_SWIZZLE_X, RC_SWIZZLE_X, RC_SWIZZLE_X, RC_SWIZZLE_X);
166 }
167
168 static void transform_ABS(struct radeon_compiler* c,
169 struct rc_instruction* inst)
170 {
171 struct rc_src_register src = inst->U.I.SrcReg[0];
172 src.Abs = 1;
173 src.Negate = RC_MASK_NONE;
174 emit1(c, inst->Prev, RC_OPCODE_MOV, inst->U.I.SaturateMode, inst->U.I.DstReg, src);
175 rc_remove_instruction(inst);
176 }
177
178 static void transform_CEIL(struct radeon_compiler* c,
179 struct rc_instruction* inst)
180 {
181 /* Assuming:
182 * ceil(x) = -floor(-x)
183 *
184 * After inlining floor:
185 * ceil(x) = -(-x-frac(-x))
186 *
187 * After simplification:
188 * ceil(x) = x+frac(-x)
189 */
190
191 int tempreg = rc_find_free_temporary(c);
192 emit1(c, inst->Prev, RC_OPCODE_FRC, 0, dstreg(RC_FILE_TEMPORARY, tempreg), negate(inst->U.I.SrcReg[0]));
193 emit2(c, inst->Prev, RC_OPCODE_ADD, inst->U.I.SaturateMode, inst->U.I.DstReg,
194 inst->U.I.SrcReg[0], srcreg(RC_FILE_TEMPORARY, tempreg));
195 rc_remove_instruction(inst);
196 }
197
198 static void transform_DP3(struct radeon_compiler* c,
199 struct rc_instruction* inst)
200 {
201 struct rc_src_register src0 = inst->U.I.SrcReg[0];
202 struct rc_src_register src1 = inst->U.I.SrcReg[1];
203 src0.Negate &= ~RC_MASK_W;
204 src0.Swizzle &= ~(7 << (3 * 3));
205 src0.Swizzle |= RC_SWIZZLE_ZERO << (3 * 3);
206 src1.Negate &= ~RC_MASK_W;
207 src1.Swizzle &= ~(7 << (3 * 3));
208 src1.Swizzle |= RC_SWIZZLE_ZERO << (3 * 3);
209 emit2(c, inst->Prev, RC_OPCODE_DP4, inst->U.I.SaturateMode, inst->U.I.DstReg, src0, src1);
210 rc_remove_instruction(inst);
211 }
212
213 static void transform_DPH(struct radeon_compiler* c,
214 struct rc_instruction* inst)
215 {
216 struct rc_src_register src0 = inst->U.I.SrcReg[0];
217 src0.Negate &= ~RC_MASK_W;
218 src0.Swizzle &= ~(7 << (3 * 3));
219 src0.Swizzle |= RC_SWIZZLE_ONE << (3 * 3);
220 emit2(c, inst->Prev, RC_OPCODE_DP4, inst->U.I.SaturateMode, inst->U.I.DstReg, src0, inst->U.I.SrcReg[1]);
221 rc_remove_instruction(inst);
222 }
223
224 /**
225 * [1, src0.y*src1.y, src0.z, src1.w]
226 * So basically MUL with lotsa swizzling.
227 */
228 static void transform_DST(struct radeon_compiler* c,
229 struct rc_instruction* inst)
230 {
231 emit2(c, inst->Prev, RC_OPCODE_MUL, inst->U.I.SaturateMode, inst->U.I.DstReg,
232 swizzle(inst->U.I.SrcReg[0], RC_SWIZZLE_ONE, RC_SWIZZLE_Y, RC_SWIZZLE_Z, RC_SWIZZLE_ONE),
233 swizzle(inst->U.I.SrcReg[1], RC_SWIZZLE_ONE, RC_SWIZZLE_Y, RC_SWIZZLE_ONE, RC_SWIZZLE_W));
234 rc_remove_instruction(inst);
235 }
236
237 static void transform_FLR(struct radeon_compiler* c,
238 struct rc_instruction* inst)
239 {
240 int tempreg = rc_find_free_temporary(c);
241 emit1(c, inst->Prev, RC_OPCODE_FRC, 0, dstreg(RC_FILE_TEMPORARY, tempreg), inst->U.I.SrcReg[0]);
242 emit2(c, inst->Prev, RC_OPCODE_ADD, inst->U.I.SaturateMode, inst->U.I.DstReg,
243 inst->U.I.SrcReg[0], negate(srcreg(RC_FILE_TEMPORARY, tempreg)));
244 rc_remove_instruction(inst);
245 }
246
247 /**
248 * Definition of LIT (from ARB_fragment_program):
249 *
250 * tmp = VectorLoad(op0);
251 * if (tmp.x < 0) tmp.x = 0;
252 * if (tmp.y < 0) tmp.y = 0;
253 * if (tmp.w < -(128.0-epsilon)) tmp.w = -(128.0-epsilon);
254 * else if (tmp.w > 128-epsilon) tmp.w = 128-epsilon;
255 * result.x = 1.0;
256 * result.y = tmp.x;
257 * result.z = (tmp.x > 0) ? RoughApproxPower(tmp.y, tmp.w) : 0.0;
258 * result.w = 1.0;
259 *
260 * The longest path of computation is the one leading to result.z,
261 * consisting of 5 operations. This implementation of LIT takes
262 * 5 slots, if the subsequent optimization passes are clever enough
263 * to pair instructions correctly.
264 */
265 static void transform_LIT(struct radeon_compiler* c,
266 struct rc_instruction* inst)
267 {
268 unsigned int constant;
269 unsigned int constant_swizzle;
270 unsigned int temp;
271 struct rc_src_register srctemp;
272
273 constant = rc_constants_add_immediate_scalar(&c->Program.Constants, -127.999999, &constant_swizzle);
274
275 if (inst->U.I.DstReg.WriteMask != RC_MASK_XYZW || inst->U.I.DstReg.File != RC_FILE_TEMPORARY) {
276 struct rc_instruction * inst_mov;
277
278 inst_mov = emit1(c, inst,
279 RC_OPCODE_MOV, 0, inst->U.I.DstReg,
280 srcreg(RC_FILE_TEMPORARY, rc_find_free_temporary(c)));
281
282 inst->U.I.DstReg.File = RC_FILE_TEMPORARY;
283 inst->U.I.DstReg.Index = inst_mov->U.I.SrcReg[0].Index;
284 inst->U.I.DstReg.WriteMask = RC_MASK_XYZW;
285 }
286
287 temp = inst->U.I.DstReg.Index;
288 srctemp = srcreg(RC_FILE_TEMPORARY, temp);
289
290 /* tmp.x = max(0.0, Src.x); */
291 /* tmp.y = max(0.0, Src.y); */
292 /* tmp.w = clamp(Src.z, -128+eps, 128-eps); */
293 emit2(c, inst->Prev, RC_OPCODE_MAX, 0,
294 dstregtmpmask(temp, RC_MASK_XYW),
295 inst->U.I.SrcReg[0],
296 swizzle(srcreg(RC_FILE_CONSTANT, constant),
297 RC_SWIZZLE_ZERO, RC_SWIZZLE_ZERO, RC_SWIZZLE_ZERO, constant_swizzle&3));
298 emit2(c, inst->Prev, RC_OPCODE_MIN, 0,
299 dstregtmpmask(temp, RC_MASK_Z),
300 swizzle(srctemp, RC_SWIZZLE_W, RC_SWIZZLE_W, RC_SWIZZLE_W, RC_SWIZZLE_W),
301 negate(srcregswz(RC_FILE_CONSTANT, constant, constant_swizzle)));
302
303 /* tmp.w = Pow(tmp.y, tmp.w) */
304 emit1(c, inst->Prev, RC_OPCODE_LG2, 0,
305 dstregtmpmask(temp, RC_MASK_W),
306 swizzle(srctemp, RC_SWIZZLE_Y, RC_SWIZZLE_Y, RC_SWIZZLE_Y, RC_SWIZZLE_Y));
307 emit2(c, inst->Prev, RC_OPCODE_MUL, 0,
308 dstregtmpmask(temp, RC_MASK_W),
309 swizzle(srctemp, RC_SWIZZLE_W, RC_SWIZZLE_W, RC_SWIZZLE_W, RC_SWIZZLE_W),
310 swizzle(srctemp, RC_SWIZZLE_Z, RC_SWIZZLE_Z, RC_SWIZZLE_Z, RC_SWIZZLE_Z));
311 emit1(c, inst->Prev, RC_OPCODE_EX2, 0,
312 dstregtmpmask(temp, RC_MASK_W),
313 swizzle(srctemp, RC_SWIZZLE_W, RC_SWIZZLE_W, RC_SWIZZLE_W, RC_SWIZZLE_W));
314
315 /* tmp.z = (tmp.x > 0) ? tmp.w : 0.0 */
316 emit3(c, inst->Prev, RC_OPCODE_CMP, inst->U.I.SaturateMode,
317 dstregtmpmask(temp, RC_MASK_Z),
318 negate(swizzle(srctemp, RC_SWIZZLE_X, RC_SWIZZLE_X, RC_SWIZZLE_X, RC_SWIZZLE_X)),
319 swizzle(srctemp, RC_SWIZZLE_W, RC_SWIZZLE_W, RC_SWIZZLE_W, RC_SWIZZLE_W),
320 builtin_zero);
321
322 /* tmp.x, tmp.y, tmp.w = 1.0, tmp.x, 1.0 */
323 emit1(c, inst->Prev, RC_OPCODE_MOV, inst->U.I.SaturateMode,
324 dstregtmpmask(temp, RC_MASK_XYW),
325 swizzle(srctemp, RC_SWIZZLE_ONE, RC_SWIZZLE_X, RC_SWIZZLE_ONE, RC_SWIZZLE_ONE));
326
327 rc_remove_instruction(inst);
328 }
329
330 static void transform_LRP(struct radeon_compiler* c,
331 struct rc_instruction* inst)
332 {
333 int tempreg = rc_find_free_temporary(c);
334
335 emit2(c, inst->Prev, RC_OPCODE_ADD, 0,
336 dstreg(RC_FILE_TEMPORARY, tempreg),
337 inst->U.I.SrcReg[1], negate(inst->U.I.SrcReg[2]));
338 emit3(c, inst->Prev, RC_OPCODE_MAD, inst->U.I.SaturateMode,
339 inst->U.I.DstReg,
340 inst->U.I.SrcReg[0], srcreg(RC_FILE_TEMPORARY, tempreg), inst->U.I.SrcReg[2]);
341
342 rc_remove_instruction(inst);
343 }
344
345 static void transform_POW(struct radeon_compiler* c,
346 struct rc_instruction* inst)
347 {
348 int tempreg = rc_find_free_temporary(c);
349 struct rc_dst_register tempdst = dstreg(RC_FILE_TEMPORARY, tempreg);
350 struct rc_src_register tempsrc = srcreg(RC_FILE_TEMPORARY, tempreg);
351 tempdst.WriteMask = RC_MASK_W;
352 tempsrc.Swizzle = RC_SWIZZLE_WWWW;
353
354 emit1(c, inst->Prev, RC_OPCODE_LG2, 0, tempdst, scalar(inst->U.I.SrcReg[0]));
355 emit2(c, inst->Prev, RC_OPCODE_MUL, 0, tempdst, tempsrc, scalar(inst->U.I.SrcReg[1]));
356 emit1(c, inst->Prev, RC_OPCODE_EX2, inst->U.I.SaturateMode, inst->U.I.DstReg, tempsrc);
357
358 rc_remove_instruction(inst);
359 }
360
361 static void transform_RSQ(struct radeon_compiler* c,
362 struct rc_instruction* inst)
363 {
364 inst->U.I.SrcReg[0] = absolute(inst->U.I.SrcReg[0]);
365 }
366
367 static void transform_SEQ(struct radeon_compiler* c,
368 struct rc_instruction* inst)
369 {
370 int tempreg = rc_find_free_temporary(c);
371
372 emit2(c, inst->Prev, RC_OPCODE_ADD, 0, dstreg(RC_FILE_TEMPORARY, tempreg), inst->U.I.SrcReg[0], negate(inst->U.I.SrcReg[1]));
373 emit3(c, inst->Prev, RC_OPCODE_CMP, inst->U.I.SaturateMode, inst->U.I.DstReg,
374 negate(absolute(srcreg(RC_FILE_TEMPORARY, tempreg))), builtin_zero, builtin_one);
375
376 rc_remove_instruction(inst);
377 }
378
379 static void transform_SFL(struct radeon_compiler* c,
380 struct rc_instruction* inst)
381 {
382 emit1(c, inst->Prev, RC_OPCODE_MOV, inst->U.I.SaturateMode, inst->U.I.DstReg, builtin_zero);
383 rc_remove_instruction(inst);
384 }
385
386 static void transform_SGE(struct radeon_compiler* c,
387 struct rc_instruction* inst)
388 {
389 int tempreg = rc_find_free_temporary(c);
390
391 emit2(c, inst->Prev, RC_OPCODE_ADD, 0, dstreg(RC_FILE_TEMPORARY, tempreg), inst->U.I.SrcReg[0], negate(inst->U.I.SrcReg[1]));
392 emit3(c, inst->Prev, RC_OPCODE_CMP, inst->U.I.SaturateMode, inst->U.I.DstReg,
393 srcreg(RC_FILE_TEMPORARY, tempreg), builtin_zero, builtin_one);
394
395 rc_remove_instruction(inst);
396 }
397
398 static void transform_SGT(struct radeon_compiler* c,
399 struct rc_instruction* inst)
400 {
401 int tempreg = rc_find_free_temporary(c);
402
403 emit2(c, inst->Prev, RC_OPCODE_ADD, 0, dstreg(RC_FILE_TEMPORARY, tempreg), negate(inst->U.I.SrcReg[0]), inst->U.I.SrcReg[1]);
404 emit3(c, inst->Prev, RC_OPCODE_CMP, inst->U.I.SaturateMode, inst->U.I.DstReg,
405 srcreg(RC_FILE_TEMPORARY, tempreg), builtin_one, builtin_zero);
406
407 rc_remove_instruction(inst);
408 }
409
410 static void transform_SLE(struct radeon_compiler* c,
411 struct rc_instruction* inst)
412 {
413 int tempreg = rc_find_free_temporary(c);
414
415 emit2(c, inst->Prev, RC_OPCODE_ADD, 0, dstreg(RC_FILE_TEMPORARY, tempreg), negate(inst->U.I.SrcReg[0]), inst->U.I.SrcReg[1]);
416 emit3(c, inst->Prev, RC_OPCODE_CMP, inst->U.I.SaturateMode, inst->U.I.DstReg,
417 srcreg(RC_FILE_TEMPORARY, tempreg), builtin_zero, builtin_one);
418
419 rc_remove_instruction(inst);
420 }
421
422 static void transform_SLT(struct radeon_compiler* c,
423 struct rc_instruction* inst)
424 {
425 int tempreg = rc_find_free_temporary(c);
426
427 emit2(c, inst->Prev, RC_OPCODE_ADD, 0, dstreg(RC_FILE_TEMPORARY, tempreg), inst->U.I.SrcReg[0], negate(inst->U.I.SrcReg[1]));
428 emit3(c, inst->Prev, RC_OPCODE_CMP, inst->U.I.SaturateMode, inst->U.I.DstReg,
429 srcreg(RC_FILE_TEMPORARY, tempreg), builtin_one, builtin_zero);
430
431 rc_remove_instruction(inst);
432 }
433
434 static void transform_SNE(struct radeon_compiler* c,
435 struct rc_instruction* inst)
436 {
437 int tempreg = rc_find_free_temporary(c);
438
439 emit2(c, inst->Prev, RC_OPCODE_ADD, 0, dstreg(RC_FILE_TEMPORARY, tempreg), inst->U.I.SrcReg[0], negate(inst->U.I.SrcReg[1]));
440 emit3(c, inst->Prev, RC_OPCODE_CMP, inst->U.I.SaturateMode, inst->U.I.DstReg,
441 negate(absolute(srcreg(RC_FILE_TEMPORARY, tempreg))), builtin_one, builtin_zero);
442
443 rc_remove_instruction(inst);
444 }
445
446 static void transform_SUB(struct radeon_compiler* c,
447 struct rc_instruction* inst)
448 {
449 inst->U.I.Opcode = RC_OPCODE_ADD;
450 inst->U.I.SrcReg[1] = negate(inst->U.I.SrcReg[1]);
451 }
452
453 static void transform_SWZ(struct radeon_compiler* c,
454 struct rc_instruction* inst)
455 {
456 inst->U.I.Opcode = RC_OPCODE_MOV;
457 }
458
459 static void transform_XPD(struct radeon_compiler* c,
460 struct rc_instruction* inst)
461 {
462 int tempreg = rc_find_free_temporary(c);
463
464 emit2(c, inst->Prev, RC_OPCODE_MUL, 0, dstreg(RC_FILE_TEMPORARY, tempreg),
465 swizzle(inst->U.I.SrcReg[0], RC_SWIZZLE_Z, RC_SWIZZLE_X, RC_SWIZZLE_Y, RC_SWIZZLE_W),
466 swizzle(inst->U.I.SrcReg[1], RC_SWIZZLE_Y, RC_SWIZZLE_Z, RC_SWIZZLE_X, RC_SWIZZLE_W));
467 emit3(c, inst->Prev, RC_OPCODE_MAD, inst->U.I.SaturateMode, inst->U.I.DstReg,
468 swizzle(inst->U.I.SrcReg[0], RC_SWIZZLE_Y, RC_SWIZZLE_Z, RC_SWIZZLE_X, RC_SWIZZLE_W),
469 swizzle(inst->U.I.SrcReg[1], RC_SWIZZLE_Z, RC_SWIZZLE_X, RC_SWIZZLE_Y, RC_SWIZZLE_W),
470 negate(srcreg(RC_FILE_TEMPORARY, tempreg)));
471
472 rc_remove_instruction(inst);
473 }
474
475
476 /**
477 * Can be used as a transformation for @ref radeonClauseLocalTransform,
478 * no userData necessary.
479 *
480 * Eliminates the following ALU instructions:
481 * ABS, CEIL, DPH, DST, FLR, LIT, LRP, POW, SEQ, SFL, SGE, SGT, SLE, SLT, SNE, SUB, SWZ, XPD
482 * using:
483 * MOV, ADD, MUL, MAD, FRC, DP3, LG2, EX2, CMP
484 *
485 * Transforms RSQ to Radeon's native RSQ by explicitly setting
486 * absolute value.
487 *
488 * @note should be applicable to R300 and R500 fragment programs.
489 */
490 int radeonTransformALU(
491 struct radeon_compiler * c,
492 struct rc_instruction* inst,
493 void* unused)
494 {
495 switch(inst->U.I.Opcode) {
496 case RC_OPCODE_ABS: transform_ABS(c, inst); return 1;
497 case RC_OPCODE_CEIL: transform_CEIL(c, inst); return 1;
498 case RC_OPCODE_DPH: transform_DPH(c, inst); return 1;
499 case RC_OPCODE_DST: transform_DST(c, inst); return 1;
500 case RC_OPCODE_FLR: transform_FLR(c, inst); return 1;
501 case RC_OPCODE_LIT: transform_LIT(c, inst); return 1;
502 case RC_OPCODE_LRP: transform_LRP(c, inst); return 1;
503 case RC_OPCODE_POW: transform_POW(c, inst); return 1;
504 case RC_OPCODE_RSQ: transform_RSQ(c, inst); return 1;
505 case RC_OPCODE_SEQ: transform_SEQ(c, inst); return 1;
506 case RC_OPCODE_SFL: transform_SFL(c, inst); return 1;
507 case RC_OPCODE_SGE: transform_SGE(c, inst); return 1;
508 case RC_OPCODE_SGT: transform_SGT(c, inst); return 1;
509 case RC_OPCODE_SLE: transform_SLE(c, inst); return 1;
510 case RC_OPCODE_SLT: transform_SLT(c, inst); return 1;
511 case RC_OPCODE_SNE: transform_SNE(c, inst); return 1;
512 case RC_OPCODE_SUB: transform_SUB(c, inst); return 1;
513 case RC_OPCODE_SWZ: transform_SWZ(c, inst); return 1;
514 case RC_OPCODE_XPD: transform_XPD(c, inst); return 1;
515 default:
516 return 0;
517 }
518 }
519
520
521 static void transform_r300_vertex_ABS(struct radeon_compiler* c,
522 struct rc_instruction* inst)
523 {
524 /* Note: r500 can take absolute values, but r300 cannot. */
525 inst->U.I.Opcode = RC_OPCODE_MAX;
526 inst->U.I.SrcReg[1] = inst->U.I.SrcReg[0];
527 inst->U.I.SrcReg[1].Negate ^= RC_MASK_XYZW;
528 }
529
530 static void transform_r300_vertex_CMP(struct radeon_compiler* c,
531 struct rc_instruction* inst)
532 {
533 /* There is no decent CMP available, so let's rig one up.
534 * CMP is defined as dst = src0 < 0.0 ? src1 : src2
535 * The following sequence consumes two temps and two extra slots
536 * (the second temp and the second slot is consumed by transform_LRP),
537 * but should be equivalent:
538 *
539 * SLT tmp0, src0, 0.0
540 * LRP dst, tmp0, src1, src2
541 *
542 * Yes, I know, I'm a mad scientist. ~ C. & M. */
543 int tempreg0 = rc_find_free_temporary(c);
544
545 /* SLT tmp0, src0, 0.0 */
546 emit2(c, inst->Prev, RC_OPCODE_SLT, 0,
547 dstreg(RC_FILE_TEMPORARY, tempreg0),
548 inst->U.I.SrcReg[0], builtin_zero);
549
550 /* LRP dst, tmp0, src1, src2 */
551 transform_LRP(c,
552 emit3(c, inst->Prev, RC_OPCODE_LRP, 0,
553 inst->U.I.DstReg,
554 srcreg(RC_FILE_TEMPORARY, tempreg0), inst->U.I.SrcReg[1], inst->U.I.SrcReg[2]));
555
556 rc_remove_instruction(inst);
557 }
558
559 static void transform_r300_vertex_fix_LIT(struct radeon_compiler* c,
560 struct rc_instruction* inst)
561 {
562 int tempreg = rc_find_free_temporary(c);
563 unsigned constant_swizzle;
564 int constant = rc_constants_add_immediate_scalar(&c->Program.Constants,
565 0.0000000000000000001,
566 &constant_swizzle);
567
568 /* MOV dst, src */
569 emit1(c, inst->Prev, RC_OPCODE_MOV, 0,
570 dstreg(RC_FILE_TEMPORARY, tempreg),
571 inst->U.I.SrcReg[0]);
572
573 /* MAX dst.z, src, 0.00...001 */
574 emit2(c, inst->Prev, RC_OPCODE_MAX, 0,
575 dstregtmpmask(tempreg, RC_MASK_Y),
576 srcreg(RC_FILE_TEMPORARY, tempreg),
577 srcregswz(RC_FILE_CONSTANT, constant, constant_swizzle));
578
579 inst->U.I.SrcReg[0] = srcreg(RC_FILE_TEMPORARY, tempreg);
580 }
581
582 /**
583 * For use with radeonLocalTransform, this transforms non-native ALU
584 * instructions of the r300 up to r500 vertex engine.
585 */
586 int r300_transform_vertex_alu(
587 struct radeon_compiler * c,
588 struct rc_instruction* inst,
589 void* unused)
590 {
591 switch(inst->U.I.Opcode) {
592 case RC_OPCODE_ABS: transform_r300_vertex_ABS(c, inst); return 1;
593 case RC_OPCODE_CEIL: transform_CEIL(c, inst); return 1;
594 case RC_OPCODE_CMP: transform_r300_vertex_CMP(c, inst); return 1;
595 case RC_OPCODE_DP3: transform_DP3(c, inst); return 1;
596 case RC_OPCODE_DPH: transform_DPH(c, inst); return 1;
597 case RC_OPCODE_FLR: transform_FLR(c, inst); return 1;
598 case RC_OPCODE_LIT: transform_r300_vertex_fix_LIT(c, inst); return 1;
599 case RC_OPCODE_LRP: transform_LRP(c, inst); return 1;
600 case RC_OPCODE_SUB: transform_SUB(c, inst); return 1;
601 case RC_OPCODE_SWZ: transform_SWZ(c, inst); return 1;
602 case RC_OPCODE_XPD: transform_XPD(c, inst); return 1;
603 default:
604 return 0;
605 }
606 }
607
608 static void sincos_constants(struct radeon_compiler* c, unsigned int *constants)
609 {
610 static const float SinCosConsts[2][4] = {
611 {
612 1.273239545, /* 4/PI */
613 -0.405284735, /* -4/(PI*PI) */
614 3.141592654, /* PI */
615 0.2225 /* weight */
616 },
617 {
618 0.75,
619 0.5,
620 0.159154943, /* 1/(2*PI) */
621 6.283185307 /* 2*PI */
622 }
623 };
624 int i;
625
626 for(i = 0; i < 2; ++i)
627 constants[i] = rc_constants_add_immediate_vec4(&c->Program.Constants, SinCosConsts[i]);
628 }
629
630 /**
631 * Approximate sin(x), where x is clamped to (-pi/2, pi/2).
632 *
633 * MUL tmp.xy, src, { 4/PI, -4/(PI^2) }
634 * MAD tmp.x, tmp.y, |src|, tmp.x
635 * MAD tmp.y, tmp.x, |tmp.x|, -tmp.x
636 * MAD dest, tmp.y, weight, tmp.x
637 */
638 static void sin_approx(
639 struct radeon_compiler* c, struct rc_instruction * inst,
640 struct rc_dst_register dst, struct rc_src_register src, const unsigned int* constants)
641 {
642 unsigned int tempreg = rc_find_free_temporary(c);
643
644 emit2(c, inst->Prev, RC_OPCODE_MUL, 0, dstregtmpmask(tempreg, RC_MASK_XY),
645 swizzle(src, RC_SWIZZLE_X, RC_SWIZZLE_X, RC_SWIZZLE_X, RC_SWIZZLE_X),
646 srcreg(RC_FILE_CONSTANT, constants[0]));
647 emit3(c, inst->Prev, RC_OPCODE_MAD, 0, dstregtmpmask(tempreg, RC_MASK_X),
648 swizzle(srcreg(RC_FILE_TEMPORARY, tempreg), RC_SWIZZLE_Y, RC_SWIZZLE_Y, RC_SWIZZLE_Y, RC_SWIZZLE_Y),
649 absolute(swizzle(src, RC_SWIZZLE_X, RC_SWIZZLE_X, RC_SWIZZLE_X, RC_SWIZZLE_X)),
650 swizzle(srcreg(RC_FILE_TEMPORARY, tempreg), RC_SWIZZLE_X, RC_SWIZZLE_X, RC_SWIZZLE_X, RC_SWIZZLE_X));
651 emit3(c, inst->Prev, RC_OPCODE_MAD, 0, dstregtmpmask(tempreg, RC_MASK_Y),
652 swizzle(srcreg(RC_FILE_TEMPORARY, tempreg), RC_SWIZZLE_X, RC_SWIZZLE_X, RC_SWIZZLE_X, RC_SWIZZLE_X),
653 absolute(swizzle(srcreg(RC_FILE_TEMPORARY, tempreg), RC_SWIZZLE_X, RC_SWIZZLE_X, RC_SWIZZLE_X, RC_SWIZZLE_X)),
654 negate(swizzle(srcreg(RC_FILE_TEMPORARY, tempreg), RC_SWIZZLE_X, RC_SWIZZLE_X, RC_SWIZZLE_X, RC_SWIZZLE_X)));
655 emit3(c, inst->Prev, RC_OPCODE_MAD, 0, dst,
656 swizzle(srcreg(RC_FILE_TEMPORARY, tempreg), RC_SWIZZLE_Y, RC_SWIZZLE_Y, RC_SWIZZLE_Y, RC_SWIZZLE_Y),
657 swizzle(srcreg(RC_FILE_CONSTANT, constants[0]), RC_SWIZZLE_W, RC_SWIZZLE_W, RC_SWIZZLE_W, RC_SWIZZLE_W),
658 swizzle(srcreg(RC_FILE_TEMPORARY, tempreg), RC_SWIZZLE_X, RC_SWIZZLE_X, RC_SWIZZLE_X, RC_SWIZZLE_X));
659 }
660
661 /**
662 * Translate the trigonometric functions COS, SIN, and SCS
663 * using only the basic instructions
664 * MOV, ADD, MUL, MAD, FRC
665 */
666 int radeonTransformTrigSimple(struct radeon_compiler* c,
667 struct rc_instruction* inst,
668 void* unused)
669 {
670 if (inst->U.I.Opcode != RC_OPCODE_COS &&
671 inst->U.I.Opcode != RC_OPCODE_SIN &&
672 inst->U.I.Opcode != RC_OPCODE_SCS)
673 return 0;
674
675 unsigned int constants[2];
676 unsigned int tempreg = rc_find_free_temporary(c);
677
678 sincos_constants(c, constants);
679
680 if (inst->U.I.Opcode == RC_OPCODE_COS) {
681 /* MAD tmp.x, src, 1/(2*PI), 0.75 */
682 /* FRC tmp.x, tmp.x */
683 /* MAD tmp.z, tmp.x, 2*PI, -PI */
684 emit3(c, inst->Prev, RC_OPCODE_MAD, 0, dstregtmpmask(tempreg, RC_MASK_W),
685 swizzle(inst->U.I.SrcReg[0], RC_SWIZZLE_X, RC_SWIZZLE_X, RC_SWIZZLE_X, RC_SWIZZLE_X),
686 swizzle(srcreg(RC_FILE_CONSTANT, constants[1]), RC_SWIZZLE_Z, RC_SWIZZLE_Z, RC_SWIZZLE_Z, RC_SWIZZLE_Z),
687 swizzle(srcreg(RC_FILE_CONSTANT, constants[1]), RC_SWIZZLE_X, RC_SWIZZLE_X, RC_SWIZZLE_X, RC_SWIZZLE_X));
688 emit1(c, inst->Prev, RC_OPCODE_FRC, 0, dstregtmpmask(tempreg, RC_MASK_W),
689 swizzle(srcreg(RC_FILE_TEMPORARY, tempreg), RC_SWIZZLE_W, RC_SWIZZLE_W, RC_SWIZZLE_W, RC_SWIZZLE_W));
690 emit3(c, inst->Prev, RC_OPCODE_MAD, 0, dstregtmpmask(tempreg, RC_MASK_W),
691 swizzle(srcreg(RC_FILE_TEMPORARY, tempreg), RC_SWIZZLE_W, RC_SWIZZLE_W, RC_SWIZZLE_W, RC_SWIZZLE_W),
692 swizzle(srcreg(RC_FILE_CONSTANT, constants[1]), RC_SWIZZLE_W, RC_SWIZZLE_W, RC_SWIZZLE_W, RC_SWIZZLE_W),
693 negate(swizzle(srcreg(RC_FILE_CONSTANT, constants[0]), RC_SWIZZLE_Z, RC_SWIZZLE_Z, RC_SWIZZLE_Z, RC_SWIZZLE_Z)));
694
695 sin_approx(c, inst, inst->U.I.DstReg,
696 swizzle(srcreg(RC_FILE_TEMPORARY, tempreg), RC_SWIZZLE_W, RC_SWIZZLE_W, RC_SWIZZLE_W, RC_SWIZZLE_W),
697 constants);
698 } else if (inst->U.I.Opcode == RC_OPCODE_SIN) {
699 emit3(c, inst->Prev, RC_OPCODE_MAD, 0, dstregtmpmask(tempreg, RC_MASK_W),
700 swizzle(inst->U.I.SrcReg[0], RC_SWIZZLE_X, RC_SWIZZLE_X, RC_SWIZZLE_X, RC_SWIZZLE_X),
701 swizzle(srcreg(RC_FILE_CONSTANT, constants[1]), RC_SWIZZLE_Z, RC_SWIZZLE_Z, RC_SWIZZLE_Z, RC_SWIZZLE_Z),
702 swizzle(srcreg(RC_FILE_CONSTANT, constants[1]), RC_SWIZZLE_Y, RC_SWIZZLE_Y, RC_SWIZZLE_Y, RC_SWIZZLE_Y));
703 emit1(c, inst->Prev, RC_OPCODE_FRC, 0, dstregtmpmask(tempreg, RC_MASK_W),
704 swizzle(srcreg(RC_FILE_TEMPORARY, tempreg), RC_SWIZZLE_W, RC_SWIZZLE_W, RC_SWIZZLE_W, RC_SWIZZLE_W));
705 emit3(c, inst->Prev, RC_OPCODE_MAD, 0, dstregtmpmask(tempreg, RC_MASK_W),
706 swizzle(srcreg(RC_FILE_TEMPORARY, tempreg), RC_SWIZZLE_W, RC_SWIZZLE_W, RC_SWIZZLE_W, RC_SWIZZLE_W),
707 swizzle(srcreg(RC_FILE_CONSTANT, constants[1]), RC_SWIZZLE_W, RC_SWIZZLE_W, RC_SWIZZLE_W, RC_SWIZZLE_W),
708 negate(swizzle(srcreg(RC_FILE_CONSTANT, constants[0]), RC_SWIZZLE_Z, RC_SWIZZLE_Z, RC_SWIZZLE_Z, RC_SWIZZLE_Z)));
709
710 sin_approx(c, inst, inst->U.I.DstReg,
711 swizzle(srcreg(RC_FILE_TEMPORARY, tempreg), RC_SWIZZLE_W, RC_SWIZZLE_W, RC_SWIZZLE_W, RC_SWIZZLE_W),
712 constants);
713 } else {
714 emit3(c, inst->Prev, RC_OPCODE_MAD, 0, dstregtmpmask(tempreg, RC_MASK_XY),
715 swizzle(inst->U.I.SrcReg[0], RC_SWIZZLE_X, RC_SWIZZLE_X, RC_SWIZZLE_X, RC_SWIZZLE_X),
716 swizzle(srcreg(RC_FILE_CONSTANT, constants[1]), RC_SWIZZLE_Z, RC_SWIZZLE_Z, RC_SWIZZLE_Z, RC_SWIZZLE_Z),
717 swizzle(srcreg(RC_FILE_CONSTANT, constants[1]), RC_SWIZZLE_X, RC_SWIZZLE_Y, RC_SWIZZLE_Z, RC_SWIZZLE_W));
718 emit1(c, inst->Prev, RC_OPCODE_FRC, 0, dstregtmpmask(tempreg, RC_MASK_XY),
719 srcreg(RC_FILE_TEMPORARY, tempreg));
720 emit3(c, inst->Prev, RC_OPCODE_MAD, 0, dstregtmpmask(tempreg, RC_MASK_XY),
721 srcreg(RC_FILE_TEMPORARY, tempreg),
722 swizzle(srcreg(RC_FILE_CONSTANT, constants[1]), RC_SWIZZLE_W, RC_SWIZZLE_W, RC_SWIZZLE_W, RC_SWIZZLE_W),
723 negate(swizzle(srcreg(RC_FILE_CONSTANT, constants[0]), RC_SWIZZLE_Z, RC_SWIZZLE_Z, RC_SWIZZLE_Z, RC_SWIZZLE_Z)));
724
725 struct rc_dst_register dst = inst->U.I.DstReg;
726
727 dst.WriteMask = inst->U.I.DstReg.WriteMask & RC_MASK_X;
728 sin_approx(c, inst, dst,
729 swizzle(srcreg(RC_FILE_TEMPORARY, tempreg), RC_SWIZZLE_X, RC_SWIZZLE_X, RC_SWIZZLE_X, RC_SWIZZLE_X),
730 constants);
731
732 dst.WriteMask = inst->U.I.DstReg.WriteMask & RC_MASK_Y;
733 sin_approx(c, inst, dst,
734 swizzle(srcreg(RC_FILE_TEMPORARY, tempreg), RC_SWIZZLE_Y, RC_SWIZZLE_Y, RC_SWIZZLE_Y, RC_SWIZZLE_Y),
735 constants);
736 }
737
738 rc_remove_instruction(inst);
739
740 return 1;
741 }
742
743
744 /**
745 * Transform the trigonometric functions COS, SIN, and SCS
746 * to include pre-scaling by 1/(2*PI) and taking the fractional
747 * part, so that the input to COS and SIN is always in the range [0,1).
748 * SCS is replaced by one COS and one SIN instruction.
749 *
750 * @warning This transformation implicitly changes the semantics of SIN and COS!
751 */
752 int radeonTransformTrigScale(struct radeon_compiler* c,
753 struct rc_instruction* inst,
754 void* unused)
755 {
756 if (inst->U.I.Opcode != RC_OPCODE_COS &&
757 inst->U.I.Opcode != RC_OPCODE_SIN &&
758 inst->U.I.Opcode != RC_OPCODE_SCS)
759 return 0;
760
761 static const float RCP_2PI = 0.15915494309189535;
762 unsigned int temp;
763 unsigned int constant;
764 unsigned int constant_swizzle;
765
766 temp = rc_find_free_temporary(c);
767 constant = rc_constants_add_immediate_scalar(&c->Program.Constants, RCP_2PI, &constant_swizzle);
768
769 emit2(c, inst->Prev, RC_OPCODE_MUL, 0, dstregtmpmask(temp, RC_MASK_W),
770 swizzle(inst->U.I.SrcReg[0], RC_SWIZZLE_X, RC_SWIZZLE_X, RC_SWIZZLE_X, RC_SWIZZLE_X),
771 srcregswz(RC_FILE_CONSTANT, constant, constant_swizzle));
772 emit1(c, inst->Prev, RC_OPCODE_FRC, 0, dstregtmpmask(temp, RC_MASK_W),
773 srcreg(RC_FILE_TEMPORARY, temp));
774
775 if (inst->U.I.Opcode == RC_OPCODE_COS) {
776 emit1(c, inst->Prev, RC_OPCODE_COS, inst->U.I.SaturateMode, inst->U.I.DstReg,
777 srcregswz(RC_FILE_TEMPORARY, temp, RC_SWIZZLE_WWWW));
778 } else if (inst->U.I.Opcode == RC_OPCODE_SIN) {
779 emit1(c, inst->Prev, RC_OPCODE_SIN, inst->U.I.SaturateMode,
780 inst->U.I.DstReg, srcregswz(RC_FILE_TEMPORARY, temp, RC_SWIZZLE_WWWW));
781 } else if (inst->U.I.Opcode == RC_OPCODE_SCS) {
782 struct rc_dst_register moddst = inst->U.I.DstReg;
783
784 if (inst->U.I.DstReg.WriteMask & RC_MASK_X) {
785 moddst.WriteMask = RC_MASK_X;
786 emit1(c, inst->Prev, RC_OPCODE_COS, inst->U.I.SaturateMode, moddst,
787 srcregswz(RC_FILE_TEMPORARY, temp, RC_SWIZZLE_WWWW));
788 }
789 if (inst->U.I.DstReg.WriteMask & RC_MASK_Y) {
790 moddst.WriteMask = RC_MASK_Y;
791 emit1(c, inst->Prev, RC_OPCODE_SIN, inst->U.I.SaturateMode, moddst,
792 srcregswz(RC_FILE_TEMPORARY, temp, RC_SWIZZLE_WWWW));
793 }
794 }
795
796 rc_remove_instruction(inst);
797
798 return 1;
799 }
800
801 /**
802 * Rewrite DDX/DDY instructions to properly work with r5xx shaders.
803 * The r5xx MDH/MDV instruction provides per-quad partial derivatives.
804 * It takes the form A*B+C. A and C are set by setting src0. B should be -1.
805 *
806 * @warning This explicitly changes the form of DDX and DDY!
807 */
808
809 int radeonTransformDeriv(struct radeon_compiler* c,
810 struct rc_instruction* inst,
811 void* unused)
812 {
813 if (inst->U.I.Opcode != RC_OPCODE_DDX && inst->U.I.Opcode != RC_OPCODE_DDY)
814 return 0;
815
816 inst->U.I.SrcReg[1].Swizzle = RC_MAKE_SWIZZLE(RC_SWIZZLE_ONE, RC_SWIZZLE_ONE, RC_SWIZZLE_ONE, RC_SWIZZLE_ONE);
817 inst->U.I.SrcReg[1].Negate = RC_MASK_XYZW;
818
819 return 1;
820 }