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