• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 #include "radeon_compiler_util.h"
40 #include "radeon_dataflow.h"
41 
42 #include "util/log.h"
43 
emit1(struct radeon_compiler * c,struct rc_instruction * after,rc_opcode Opcode,struct rc_sub_instruction * base,struct rc_dst_register DstReg,struct rc_src_register SrcReg)44 static struct rc_instruction *emit1(
45 	struct radeon_compiler * c, struct rc_instruction * after,
46 	rc_opcode Opcode, struct rc_sub_instruction * base,
47 	struct rc_dst_register DstReg, struct rc_src_register SrcReg)
48 {
49 	struct rc_instruction *fpi = rc_insert_new_instruction(c, after);
50 
51 	if (base) {
52 		memcpy(&fpi->U.I, base, sizeof(struct rc_sub_instruction));
53 	}
54 
55 	fpi->U.I.Opcode = Opcode;
56 	fpi->U.I.DstReg = DstReg;
57 	fpi->U.I.SrcReg[0] = SrcReg;
58 	return fpi;
59 }
60 
emit2(struct radeon_compiler * c,struct rc_instruction * after,rc_opcode Opcode,struct rc_sub_instruction * base,struct rc_dst_register DstReg,struct rc_src_register SrcReg0,struct rc_src_register SrcReg1)61 static struct rc_instruction *emit2(
62 	struct radeon_compiler * c, struct rc_instruction * after,
63 	rc_opcode Opcode, struct rc_sub_instruction * base,
64 	struct rc_dst_register DstReg,
65 	struct rc_src_register SrcReg0, struct rc_src_register SrcReg1)
66 {
67 	struct rc_instruction *fpi = rc_insert_new_instruction(c, after);
68 
69 	if (base) {
70 		memcpy(&fpi->U.I, base, sizeof(struct rc_sub_instruction));
71 	}
72 
73 	fpi->U.I.Opcode = Opcode;
74 	fpi->U.I.DstReg = DstReg;
75 	fpi->U.I.SrcReg[0] = SrcReg0;
76 	fpi->U.I.SrcReg[1] = SrcReg1;
77 	return fpi;
78 }
79 
emit3(struct radeon_compiler * c,struct rc_instruction * after,rc_opcode Opcode,struct rc_sub_instruction * base,struct rc_dst_register DstReg,struct rc_src_register SrcReg0,struct rc_src_register SrcReg1,struct rc_src_register SrcReg2)80 static struct rc_instruction *emit3(
81 	struct radeon_compiler * c, struct rc_instruction * after,
82 	rc_opcode Opcode, struct rc_sub_instruction * base,
83 	struct rc_dst_register DstReg,
84 	struct rc_src_register SrcReg0, struct rc_src_register SrcReg1,
85 	struct rc_src_register SrcReg2)
86 {
87 	struct rc_instruction *fpi = rc_insert_new_instruction(c, after);
88 
89 	if (base) {
90 		memcpy(&fpi->U.I, base, sizeof(struct rc_sub_instruction));
91 	}
92 
93 	fpi->U.I.Opcode = Opcode;
94 	fpi->U.I.DstReg = DstReg;
95 	fpi->U.I.SrcReg[0] = SrcReg0;
96 	fpi->U.I.SrcReg[1] = SrcReg1;
97 	fpi->U.I.SrcReg[2] = SrcReg2;
98 	return fpi;
99 }
100 
dstregtmpmask(int index,int mask)101 static struct rc_dst_register dstregtmpmask(int index, int mask)
102 {
103 	struct rc_dst_register dst = {0, 0, 0};
104 	dst.File = RC_FILE_TEMPORARY;
105 	dst.Index = index;
106 	dst.WriteMask = mask;
107 	return dst;
108 }
109 
110 static const struct rc_src_register builtin_one = {
111 	.File = RC_FILE_NONE,
112 	.Index = 0,
113 	.Swizzle = RC_SWIZZLE_1111
114 };
115 
116 static const struct rc_src_register srcreg_undefined = {
117 	.File = RC_FILE_NONE,
118 	.Index = 0,
119 	.Swizzle = RC_SWIZZLE_XYZW
120 };
121 
srcreg(int file,int index)122 static struct rc_src_register srcreg(int file, int index)
123 {
124 	struct rc_src_register src = srcreg_undefined;
125 	src.File = file;
126 	src.Index = index;
127 	return src;
128 }
129 
srcregswz(int file,int index,int swz)130 static struct rc_src_register srcregswz(int file, int index, int swz)
131 {
132 	struct rc_src_register src = srcreg_undefined;
133 	src.File = file;
134 	src.Index = index;
135 	src.Swizzle = swz;
136 	return src;
137 }
138 
absolute(struct rc_src_register reg)139 static struct rc_src_register absolute(struct rc_src_register reg)
140 {
141 	struct rc_src_register newreg = reg;
142 	newreg.Abs = 1;
143 	newreg.Negate = RC_MASK_NONE;
144 	return newreg;
145 }
146 
negate(struct rc_src_register reg)147 static struct rc_src_register negate(struct rc_src_register reg)
148 {
149 	struct rc_src_register newreg = reg;
150 	newreg.Negate = newreg.Negate ^ RC_MASK_XYZW;
151 	return newreg;
152 }
153 
swizzle(struct rc_src_register reg,rc_swizzle x,rc_swizzle y,rc_swizzle z,rc_swizzle w)154 static struct rc_src_register swizzle(struct rc_src_register reg,
155 		rc_swizzle x, rc_swizzle y, rc_swizzle z, rc_swizzle w)
156 {
157 	struct rc_src_register swizzled = reg;
158 	swizzled.Swizzle = combine_swizzles4(reg.Swizzle, x, y, z, w);
159 	return swizzled;
160 }
161 
swizzle_smear(struct rc_src_register reg,rc_swizzle x)162 static struct rc_src_register swizzle_smear(struct rc_src_register reg,
163 		rc_swizzle x)
164 {
165 	return swizzle(reg, x, x, x, x);
166 }
167 
swizzle_xxxx(struct rc_src_register reg)168 static struct rc_src_register swizzle_xxxx(struct rc_src_register reg)
169 {
170 	return swizzle_smear(reg, RC_SWIZZLE_X);
171 }
172 
swizzle_yyyy(struct rc_src_register reg)173 static struct rc_src_register swizzle_yyyy(struct rc_src_register reg)
174 {
175 	return swizzle_smear(reg, RC_SWIZZLE_Y);
176 }
177 
swizzle_zzzz(struct rc_src_register reg)178 static struct rc_src_register swizzle_zzzz(struct rc_src_register reg)
179 {
180 	return swizzle_smear(reg, RC_SWIZZLE_Z);
181 }
182 
swizzle_wwww(struct rc_src_register reg)183 static struct rc_src_register swizzle_wwww(struct rc_src_register reg)
184 {
185 	return swizzle_smear(reg, RC_SWIZZLE_W);
186 }
187 
new_dst_reg(struct radeon_compiler * c,struct rc_instruction * inst)188 static struct rc_dst_register new_dst_reg(struct radeon_compiler *c,
189 					       struct rc_instruction *inst)
190 {
191 	unsigned tmp = rc_find_free_temporary(c);
192 	return dstregtmpmask(tmp, inst->U.I.DstReg.WriteMask);
193 }
194 
transform_DP2(struct radeon_compiler * c,struct rc_instruction * inst)195 static void transform_DP2(struct radeon_compiler* c,
196 	struct rc_instruction* inst)
197 {
198 	struct rc_src_register src0 = inst->U.I.SrcReg[0];
199 	struct rc_src_register src1 = inst->U.I.SrcReg[1];
200 	src0.Negate &= ~(RC_MASK_Z | RC_MASK_W);
201 	src0.Swizzle &= ~(63 << (3 * 2));
202 	src0.Swizzle |= (RC_SWIZZLE_ZERO << (3 * 2)) | (RC_SWIZZLE_ZERO << (3 * 3));
203 	src1.Negate &= ~(RC_MASK_Z | RC_MASK_W);
204 	src1.Swizzle &= ~(63 << (3 * 2));
205 	src1.Swizzle |= (RC_SWIZZLE_ZERO << (3 * 2)) | (RC_SWIZZLE_ZERO << (3 * 3));
206 	emit2(c, inst->Prev, RC_OPCODE_DP3, &inst->U.I, inst->U.I.DstReg, src0, src1);
207 	rc_remove_instruction(inst);
208 }
209 
transform_RSQ(struct radeon_compiler * c,struct rc_instruction * inst)210 static void transform_RSQ(struct radeon_compiler* c,
211 	struct rc_instruction* inst)
212 {
213 	inst->U.I.SrcReg[0] = absolute(inst->U.I.SrcReg[0]);
214 }
215 
transform_KILP(struct radeon_compiler * c,struct rc_instruction * inst)216 static void transform_KILP(struct radeon_compiler * c,
217 	struct rc_instruction * inst)
218 {
219 	inst->U.I.SrcReg[0] = negate(builtin_one);
220 	inst->U.I.Opcode = RC_OPCODE_KIL;
221 }
222 
223 /**
224  * Can be used as a transformation for @ref radeonClauseLocalTransform,
225  * no userData necessary.
226  *
227  * Transforms RSQ to Radeon's native RSQ by explicitly setting
228  * absolute value.
229  *
230  * @note should be applicable to R300 and R500 fragment programs.
231  */
radeonTransformALU(struct radeon_compiler * c,struct rc_instruction * inst,void * unused)232 int radeonTransformALU(
233 	struct radeon_compiler * c,
234 	struct rc_instruction* inst,
235 	void* unused)
236 {
237 	switch(inst->U.I.Opcode) {
238 	case RC_OPCODE_DP2: transform_DP2(c, inst); return 1;
239 	case RC_OPCODE_KILP: transform_KILP(c, inst); return 1;
240 	case RC_OPCODE_RSQ: transform_RSQ(c, inst); return 1;
241 	case RC_OPCODE_SEQ: unreachable();
242 	case RC_OPCODE_SGE: unreachable();
243 	case RC_OPCODE_SLT: unreachable();
244 	case RC_OPCODE_SNE: unreachable();
245 	default:
246 		return 0;
247 	}
248 }
249 
transform_r300_vertex_CMP(struct radeon_compiler * c,struct rc_instruction * inst)250 static void transform_r300_vertex_CMP(struct radeon_compiler* c,
251 	struct rc_instruction* inst)
252 {
253 	/* R5xx has a CMP, but we can use it only if it reads from less than
254 	 * three different temps. */
255 	if (c->is_r500 && !rc_inst_has_three_diff_temp_srcs(inst))
256 		return;
257 
258 	unreachable();
259 }
260 
transform_r300_vertex_DP2(struct radeon_compiler * c,struct rc_instruction * inst)261 static void transform_r300_vertex_DP2(struct radeon_compiler* c,
262 	struct rc_instruction* inst)
263 {
264 	struct rc_instruction *next_inst = inst->Next;
265 	transform_DP2(c, inst);
266 	next_inst->Prev->U.I.Opcode = RC_OPCODE_DP4;
267 }
268 
transform_r300_vertex_DP3(struct radeon_compiler * c,struct rc_instruction * inst)269 static void transform_r300_vertex_DP3(struct radeon_compiler* c,
270 	struct rc_instruction* inst)
271 {
272 	struct rc_src_register src0 = inst->U.I.SrcReg[0];
273 	struct rc_src_register src1 = inst->U.I.SrcReg[1];
274 	src0.Negate &= ~RC_MASK_W;
275 	src0.Swizzle &= ~(7 << (3 * 3));
276 	src0.Swizzle |= RC_SWIZZLE_ZERO << (3 * 3);
277 	src1.Negate &= ~RC_MASK_W;
278 	src1.Swizzle &= ~(7 << (3 * 3));
279 	src1.Swizzle |= RC_SWIZZLE_ZERO << (3 * 3);
280 	emit2(c, inst->Prev, RC_OPCODE_DP4, &inst->U.I, inst->U.I.DstReg, src0, src1);
281 	rc_remove_instruction(inst);
282 }
283 
transform_r300_vertex_fix_LIT(struct radeon_compiler * c,struct rc_instruction * inst)284 static void transform_r300_vertex_fix_LIT(struct radeon_compiler* c,
285 	struct rc_instruction* inst)
286 {
287 	struct rc_dst_register dst = new_dst_reg(c, inst);
288 	unsigned constant_swizzle;
289 	int constant = rc_constants_add_immediate_scalar(&c->Program.Constants,
290 							 0.0000000000000000001,
291 							 &constant_swizzle);
292 
293 	/* MOV dst, src */
294 	dst.WriteMask = RC_MASK_XYZW;
295 	emit1(c, inst->Prev, RC_OPCODE_MOV, NULL,
296 		dst,
297 		inst->U.I.SrcReg[0]);
298 
299 	/* MAX dst.y, src, 0.00...001 */
300 	emit2(c, inst->Prev, RC_OPCODE_MAX, NULL,
301 		dstregtmpmask(dst.Index, RC_MASK_Y),
302 		srcreg(RC_FILE_TEMPORARY, dst.Index),
303 		srcregswz(RC_FILE_CONSTANT, constant, constant_swizzle));
304 
305 	inst->U.I.SrcReg[0] = srcreg(RC_FILE_TEMPORARY, dst.Index);
306 }
307 
transform_r300_vertex_SEQ(struct radeon_compiler * c,struct rc_instruction * inst)308 static void transform_r300_vertex_SEQ(struct radeon_compiler *c,
309 	struct rc_instruction *inst)
310 {
311 	/* x = y  <==>  x >= y && y >= x */
312 	/* x <= y */
313 	struct rc_dst_register dst0 = new_dst_reg(c, inst);
314 	emit2(c, inst->Prev, RC_OPCODE_SGE, NULL,
315 	      dst0,
316 	      inst->U.I.SrcReg[0],
317 	      inst->U.I.SrcReg[1]);
318 
319 	/* y <= x */
320 	int tmp = rc_find_free_temporary(c);
321 	emit2(c, inst->Prev, RC_OPCODE_SGE, NULL,
322 	      dstregtmpmask(tmp, inst->U.I.DstReg.WriteMask),
323 	      inst->U.I.SrcReg[1],
324 	      inst->U.I.SrcReg[0]);
325 
326 	/* x && y  =  x * y */
327 	emit2(c, inst->Prev, RC_OPCODE_MUL, NULL,
328 	      inst->U.I.DstReg,
329 	      srcreg(dst0.File, dst0.Index),
330 	      srcreg(RC_FILE_TEMPORARY, tmp));
331 
332 	rc_remove_instruction(inst);
333 }
334 
transform_r300_vertex_SNE(struct radeon_compiler * c,struct rc_instruction * inst)335 static void transform_r300_vertex_SNE(struct radeon_compiler *c,
336 	struct rc_instruction *inst)
337 {
338 	/* x != y  <==>  x < y || y < x */
339 	/* x < y */
340 	struct rc_dst_register dst0 = new_dst_reg(c, inst);
341 	emit2(c, inst->Prev, RC_OPCODE_SLT, NULL,
342 	      dst0,
343 	      inst->U.I.SrcReg[0],
344 	      inst->U.I.SrcReg[1]);
345 
346 	/* y < x */
347 	int tmp = rc_find_free_temporary(c);
348 	emit2(c, inst->Prev, RC_OPCODE_SLT, NULL,
349 	      dstregtmpmask(tmp, inst->U.I.DstReg.WriteMask),
350 	      inst->U.I.SrcReg[1],
351 	      inst->U.I.SrcReg[0]);
352 
353 	/* x || y  =  max(x, y) */
354 	emit2(c, inst->Prev, RC_OPCODE_MAX, NULL,
355 	      inst->U.I.DstReg,
356 	      srcreg(dst0.File, dst0.Index),
357 	      srcreg(RC_FILE_TEMPORARY, tmp));
358 
359 	rc_remove_instruction(inst);
360 }
361 
362 /**
363  * For use with rc_local_transform, this transforms non-native ALU
364  * instructions of the r300 up to r500 vertex engine.
365  */
r300_transform_vertex_alu(struct radeon_compiler * c,struct rc_instruction * inst,void * unused)366 int r300_transform_vertex_alu(
367 	struct radeon_compiler * c,
368 	struct rc_instruction* inst,
369 	void* unused)
370 {
371 	switch(inst->U.I.Opcode) {
372 	case RC_OPCODE_CMP: transform_r300_vertex_CMP(c, inst); return 1;
373 	case RC_OPCODE_DP2: transform_r300_vertex_DP2(c, inst); return 1;
374 	case RC_OPCODE_DP3: transform_r300_vertex_DP3(c, inst); return 1;
375 	case RC_OPCODE_LIT: transform_r300_vertex_fix_LIT(c, inst); return 1;
376 	case RC_OPCODE_SEQ:
377 		if (!c->is_r500) {
378 			transform_r300_vertex_SEQ(c, inst);
379 			return 1;
380 		}
381 		return 0;
382 	case RC_OPCODE_SNE:
383 		if (!c->is_r500) {
384 			transform_r300_vertex_SNE(c, inst);
385 			return 1;
386 		}
387 		return 0;
388 	default:
389 		return 0;
390 	}
391 }
392 
393 /**
394  * Replaces DDX/DDY instructions with MOV 0 to avoid using dummy shaders on r300/r400.
395  *
396  * @warning This explicitly changes the form of DDX and DDY!
397  */
398 
radeonStubDeriv(struct radeon_compiler * c,struct rc_instruction * inst,void * unused)399 int radeonStubDeriv(struct radeon_compiler* c,
400 	struct rc_instruction* inst,
401 	void* unused)
402 {
403 	if (inst->U.I.Opcode != RC_OPCODE_DDX && inst->U.I.Opcode != RC_OPCODE_DDY)
404 		return 0;
405 
406 	inst->U.I.Opcode = RC_OPCODE_MOV;
407 	inst->U.I.SrcReg[0].Swizzle = RC_SWIZZLE_0000;
408 
409 	mesa_logw_once("r300: WARNING: Shader is trying to use derivatives, "
410 					"but the hardware doesn't support it. "
411 					"Expect possible misrendering (it's not a bug, do not report it).");
412 
413 	return 1;
414 }
415 
416 /**
417  * Rewrite DDX/DDY instructions to properly work with r5xx shaders.
418  * The r5xx MDH/MDV instruction provides per-quad partial derivatives.
419  * It takes the form A*B+C. A and C are set by setting src0. B should be -1.
420  *
421  * @warning This explicitly changes the form of DDX and DDY!
422  */
423 
radeonTransformDeriv(struct radeon_compiler * c,struct rc_instruction * inst,void * unused)424 int radeonTransformDeriv(struct radeon_compiler* c,
425 	struct rc_instruction* inst,
426 	void* unused)
427 {
428 	if (inst->U.I.Opcode != RC_OPCODE_DDX && inst->U.I.Opcode != RC_OPCODE_DDY)
429 		return 0;
430 
431 	inst->U.I.SrcReg[1].Swizzle = RC_SWIZZLE_1111;
432 	inst->U.I.SrcReg[1].Negate = RC_MASK_XYZW;
433 
434 	return 1;
435 }
436 
rc_force_output_alpha_to_one(struct radeon_compiler * c,struct rc_instruction * inst,void * data)437 int rc_force_output_alpha_to_one(struct radeon_compiler *c,
438 				 struct rc_instruction *inst, void *data)
439 {
440 	struct r300_fragment_program_compiler *fragc = (struct r300_fragment_program_compiler*)c;
441 	const struct rc_opcode_info *info = rc_get_opcode_info(inst->U.I.Opcode);
442 	unsigned tmp;
443 
444 	if (!info->HasDstReg || inst->U.I.DstReg.File != RC_FILE_OUTPUT ||
445 	    inst->U.I.DstReg.Index == fragc->OutputDepth)
446 		return 1;
447 
448 	tmp = rc_find_free_temporary(c);
449 
450 	/* Insert MOV after inst, set alpha to 1. */
451 	emit1(c, inst, RC_OPCODE_MOV, NULL, inst->U.I.DstReg,
452 	      srcregswz(RC_FILE_TEMPORARY, tmp, RC_SWIZZLE_XYZ1));
453 
454 	/* Re-route the destination of inst to the source of mov. */
455 	inst->U.I.DstReg.File = RC_FILE_TEMPORARY;
456 	inst->U.I.DstReg.Index = tmp;
457 
458 	/* Move the saturate output modifier to the MOV instruction
459 	 * (for better copy propagation). */
460 	inst->Next->U.I.SaturateMode = inst->U.I.SaturateMode;
461 	inst->U.I.SaturateMode = RC_SATURATE_NONE;
462 	return 1;
463 }
464