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