1 /*
2 * Copyright 2008 Nicolai Haehnle.
3 * SPDX-License-Identifier: MIT
4 */
5 /**
6 * @file
7 * Utilities to deal with the somewhat odd restriction on R300 fragment
8 * program swizzles.
9 */
10
11 #include "r300_fragprog_swizzle.h"
12
13 #include <stdio.h>
14
15 #include "util/macros.h"
16
17 #include "r300_reg.h"
18 #include "radeon_compiler.h"
19
20 #define MAKE_SWZ3(x, y, z) \
21 (RC_MAKE_SWIZZLE(RC_SWIZZLE_##x, RC_SWIZZLE_##y, RC_SWIZZLE_##z, RC_SWIZZLE_ZERO))
22
23 struct swizzle_data {
24 unsigned int hash; /**< swizzle value this matches */
25 unsigned int base; /**< base value for hw swizzle */
26 unsigned int stride; /**< difference in base between arg0/1/2 */
27 unsigned int srcp_stride; /**< difference in base between arg0/scrp */
28 };
29
30 static const struct swizzle_data native_swizzles[] = {
31 {MAKE_SWZ3(X, Y, Z), R300_ALU_ARGC_SRC0C_XYZ, 4, 15},
32 {MAKE_SWZ3(X, X, X), R300_ALU_ARGC_SRC0C_XXX, 4, 15},
33 {MAKE_SWZ3(Y, Y, Y), R300_ALU_ARGC_SRC0C_YYY, 4, 15},
34 {MAKE_SWZ3(Z, Z, Z), R300_ALU_ARGC_SRC0C_ZZZ, 4, 15},
35 {MAKE_SWZ3(W, W, W), R300_ALU_ARGC_SRC0A, 1, 7},
36 {MAKE_SWZ3(Y, Z, X), R300_ALU_ARGC_SRC0C_YZX, 1, 0},
37 {MAKE_SWZ3(Z, X, Y), R300_ALU_ARGC_SRC0C_ZXY, 1, 0},
38 {MAKE_SWZ3(W, Z, Y), R300_ALU_ARGC_SRC0CA_WZY, 1, 0},
39 {MAKE_SWZ3(ONE, ONE, ONE), R300_ALU_ARGC_ONE, 0, 0},
40 {MAKE_SWZ3(ZERO, ZERO, ZERO), R300_ALU_ARGC_ZERO, 0, 0},
41 {MAKE_SWZ3(HALF, HALF, HALF), R300_ALU_ARGC_HALF, 0, 0}};
42
43 static const int num_native_swizzles = ARRAY_SIZE(native_swizzles);
44 /* Only swizzles with srcp_stride != 0 can be used for presub, so
45 * just the first five from the list. */
46 static const int num_presub_swizzles = 5;
47
48 /**
49 * Find a native RGB swizzle that matches the given swizzle.
50 * Returns 0 if none found.
51 */
52 static const struct swizzle_data *
lookup_native_swizzle(unsigned int swizzle)53 lookup_native_swizzle(unsigned int swizzle)
54 {
55 int i, comp;
56
57 for (i = 0; i < num_native_swizzles; ++i) {
58 const struct swizzle_data *sd = &native_swizzles[i];
59 for (comp = 0; comp < 3; ++comp) {
60 unsigned int swz = GET_SWZ(swizzle, comp);
61 if (swz == RC_SWIZZLE_UNUSED)
62 continue;
63 if (swz != GET_SWZ(sd->hash, comp))
64 break;
65 }
66 if (comp == 3)
67 return sd;
68 }
69
70 return NULL;
71 }
72
73 /**
74 * Determines if the given swizzle is valid for r300/r400. In most situations
75 * it is better to use r300_swizzle_is_native() which can be accessed via
76 * struct radeon_compiler *c; c->SwizzleCaps->IsNative().
77 */
78 int
r300_swizzle_is_native_basic(unsigned int swizzle)79 r300_swizzle_is_native_basic(unsigned int swizzle)
80 {
81 if (lookup_native_swizzle(swizzle))
82 return 1;
83 else
84 return 0;
85 }
86
87 /**
88 * Check whether the given instruction supports the swizzle and negate
89 * combinations in the given source register.
90 */
91 static int
r300_swizzle_is_native(rc_opcode opcode,struct rc_src_register reg)92 r300_swizzle_is_native(rc_opcode opcode, struct rc_src_register reg)
93 {
94 const struct swizzle_data *sd;
95 unsigned int relevant;
96 int j;
97
98 if (opcode == RC_OPCODE_KIL || opcode == RC_OPCODE_TEX || opcode == RC_OPCODE_TXB ||
99 opcode == RC_OPCODE_TXP) {
100 if (reg.Abs || reg.Negate)
101 return 0;
102
103 for (j = 0; j < 4; ++j) {
104 unsigned int swz = GET_SWZ(reg.Swizzle, j);
105 if (swz == RC_SWIZZLE_UNUSED)
106 continue;
107 if (swz != j)
108 return 0;
109 }
110
111 return 1;
112 }
113
114 relevant = 0;
115
116 for (j = 0; j < 3; ++j)
117 if (GET_SWZ(reg.Swizzle, j) != RC_SWIZZLE_UNUSED)
118 relevant |= 1 << j;
119
120 if ((reg.Negate & relevant) && ((reg.Negate & relevant) != relevant))
121 return 0;
122
123 sd = lookup_native_swizzle(reg.Swizzle);
124 if (!sd || (reg.File == RC_FILE_PRESUB && sd->srcp_stride == 0))
125 return 0;
126
127 return 1;
128 }
129
130 static void
r300_swizzle_split(struct rc_src_register src,unsigned int mask,struct rc_swizzle_split * split)131 r300_swizzle_split(struct rc_src_register src, unsigned int mask, struct rc_swizzle_split *split)
132 {
133 split->NumPhases = 0;
134
135 while (mask) {
136 unsigned int best_matchcount = 0;
137 unsigned int best_matchmask = 0;
138 int i, comp;
139
140 unsigned num_swizzles =
141 src.File == RC_FILE_PRESUB ? num_presub_swizzles : num_native_swizzles;
142
143 for (i = 0; i < num_swizzles; ++i) {
144 const struct swizzle_data *sd = &native_swizzles[i];
145 unsigned int matchcount = 0;
146 unsigned int matchmask = 0;
147 for (comp = 0; comp < 3; ++comp) {
148 unsigned int swz;
149 if (!GET_BIT(mask, comp))
150 continue;
151 swz = GET_SWZ(src.Swizzle, comp);
152 if (swz == RC_SWIZZLE_UNUSED)
153 continue;
154 if (swz == GET_SWZ(sd->hash, comp)) {
155 /* check if the negate bit of current component
156 * is the same for already matched components */
157 if (matchmask && (!!(src.Negate & matchmask) != !!(src.Negate & (1 << comp))))
158 continue;
159
160 matchcount++;
161 matchmask |= 1 << comp;
162 }
163 }
164 if (matchcount > best_matchcount) {
165 best_matchcount = matchcount;
166 best_matchmask = matchmask;
167 if (matchmask == (mask & RC_MASK_XYZ))
168 break;
169 }
170 }
171
172 if (mask & RC_MASK_W)
173 best_matchmask |= RC_MASK_W;
174
175 split->Phase[split->NumPhases++] = best_matchmask;
176 mask &= ~best_matchmask;
177 }
178 }
179
180 const struct rc_swizzle_caps r300_swizzle_caps = {.IsNative = r300_swizzle_is_native,
181 .Split = r300_swizzle_split};
182
183 /**
184 * Translate an RGB (XYZ) swizzle into the hardware code for the given
185 * instruction source.
186 */
187 unsigned int
r300FPTranslateRGBSwizzle(unsigned int src,unsigned int swizzle)188 r300FPTranslateRGBSwizzle(unsigned int src, unsigned int swizzle)
189 {
190 const struct swizzle_data *sd = lookup_native_swizzle(swizzle);
191
192 if (!sd || (src == RC_PAIR_PRESUB_SRC && sd->srcp_stride == 0)) {
193 fprintf(stderr, "Not a native swizzle: %08x\n", swizzle);
194 return 0;
195 }
196
197 if (src == RC_PAIR_PRESUB_SRC) {
198 return sd->base + sd->srcp_stride;
199 } else {
200 return sd->base + src * sd->stride;
201 }
202 }
203
204 /**
205 * Translate an Alpha (W) swizzle into the hardware code for the given
206 * instruction source.
207 */
208 unsigned int
r300FPTranslateAlphaSwizzle(unsigned int src,unsigned int swizzle)209 r300FPTranslateAlphaSwizzle(unsigned int src, unsigned int swizzle)
210 {
211 unsigned int swz = GET_SWZ(swizzle, 0);
212 if (src == RC_PAIR_PRESUB_SRC) {
213 return R300_ALU_ARGA_SRCP_X + swz;
214 }
215 if (swz < 3)
216 return swz + 3 * src;
217
218 switch (swz) {
219 case RC_SWIZZLE_W:
220 return R300_ALU_ARGA_SRC0A + src;
221 case RC_SWIZZLE_ONE:
222 return R300_ALU_ARGA_ONE;
223 case RC_SWIZZLE_ZERO:
224 return R300_ALU_ARGA_ZERO;
225 case RC_SWIZZLE_HALF:
226 return R300_ALU_ARGA_HALF;
227 default:
228 return R300_ALU_ARGA_ONE;
229 }
230 }
231