Merge remote branch 'origin/master' into radeon-rewrite
[mesa.git] / src / mesa / drivers / dri / r300 / 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 "r300_reg.h"
37 #include "radeon_nqssadce.h"
38
39 #define MAKE_SWZ3(x, y, z) (MAKE_SWIZZLE4(SWIZZLE_##x, SWIZZLE_##y, SWIZZLE_##z, SWIZZLE_ZERO))
40
41 struct swizzle_data {
42 GLuint hash; /**< swizzle value this matches */
43 GLuint base; /**< base value for hw swizzle */
44 GLuint stride; /**< difference in base between arg0/1/2 */
45 };
46
47 static const struct swizzle_data native_swizzles[] = {
48 {MAKE_SWZ3(X, Y, Z), R300_ALU_ARGC_SRC0C_XYZ, 4},
49 {MAKE_SWZ3(X, X, X), R300_ALU_ARGC_SRC0C_XXX, 4},
50 {MAKE_SWZ3(Y, Y, Y), R300_ALU_ARGC_SRC0C_YYY, 4},
51 {MAKE_SWZ3(Z, Z, Z), R300_ALU_ARGC_SRC0C_ZZZ, 4},
52 {MAKE_SWZ3(W, W, W), R300_ALU_ARGC_SRC0A, 1},
53 {MAKE_SWZ3(Y, Z, X), R300_ALU_ARGC_SRC0C_YZX, 1},
54 {MAKE_SWZ3(Z, X, Y), R300_ALU_ARGC_SRC0C_ZXY, 1},
55 {MAKE_SWZ3(W, Z, Y), R300_ALU_ARGC_SRC0CA_WZY, 1},
56 {MAKE_SWZ3(ONE, ONE, ONE), R300_ALU_ARGC_ONE, 0},
57 {MAKE_SWZ3(ZERO, ZERO, ZERO), R300_ALU_ARGC_ZERO, 0}
58 };
59
60 static const int num_native_swizzles = sizeof(native_swizzles)/sizeof(native_swizzles[0]);
61
62
63 /**
64 * Find a native RGB swizzle that matches the given swizzle.
65 * Returns 0 if none found.
66 */
67 static const struct swizzle_data* lookup_native_swizzle(GLuint swizzle)
68 {
69 int i, comp;
70
71 for(i = 0; i < num_native_swizzles; ++i) {
72 const struct swizzle_data* sd = &native_swizzles[i];
73 for(comp = 0; comp < 3; ++comp) {
74 GLuint swz = GET_SWZ(swizzle, comp);
75 if (swz == SWIZZLE_NIL)
76 continue;
77 if (swz != GET_SWZ(sd->hash, comp))
78 break;
79 }
80 if (comp == 3)
81 return sd;
82 }
83
84 return 0;
85 }
86
87
88 /**
89 * Check whether the given instruction supports the swizzle and negate
90 * combinations in the given source register.
91 */
92 GLboolean r300FPIsNativeSwizzle(GLuint opcode, struct prog_src_register reg)
93 {
94 if (reg.Abs)
95 reg.NegateBase = 0;
96
97 if (opcode == OPCODE_KIL ||
98 opcode == OPCODE_TEX ||
99 opcode == OPCODE_TXB ||
100 opcode == OPCODE_TXP) {
101 int j;
102
103 if (reg.Abs || reg.NegateBase != (15*reg.NegateAbs))
104 return GL_FALSE;
105
106 for(j = 0; j < 4; ++j) {
107 GLuint swz = GET_SWZ(reg.Swizzle, j);
108 if (swz == SWIZZLE_NIL)
109 continue;
110 if (swz != j)
111 return GL_FALSE;
112 }
113
114 return GL_TRUE;
115 }
116
117 GLuint relevant = 0;
118 int j;
119
120 for(j = 0; j < 3; ++j)
121 if (GET_SWZ(reg.Swizzle, j) != SWIZZLE_NIL)
122 relevant |= 1 << j;
123
124 if ((reg.NegateBase & relevant) && (reg.NegateBase & relevant) != relevant)
125 return GL_FALSE;
126
127 if (!lookup_native_swizzle(reg.Swizzle))
128 return GL_FALSE;
129
130 return GL_TRUE;
131 }
132
133
134 /**
135 * Generate MOV dst, src using only native swizzles.
136 */
137 void r300FPBuildSwizzle(struct nqssadce_state *s, struct prog_dst_register dst, struct prog_src_register src)
138 {
139 if (src.Abs)
140 src.NegateBase = 0;
141
142 while(dst.WriteMask) {
143 const struct swizzle_data *best_swizzle = 0;
144 GLuint best_matchcount = 0;
145 GLuint best_matchmask = 0;
146 GLboolean rgbnegate;
147 int i, comp;
148
149 for(i = 0; i < num_native_swizzles; ++i) {
150 const struct swizzle_data *sd = &native_swizzles[i];
151 GLuint matchcount = 0;
152 GLuint matchmask = 0;
153 for(comp = 0; comp < 3; ++comp) {
154 if (!GET_BIT(dst.WriteMask, comp))
155 continue;
156 GLuint swz = GET_SWZ(src.Swizzle, comp);
157 if (swz == SWIZZLE_NIL)
158 continue;
159 if (swz == GET_SWZ(sd->hash, comp)) {
160 matchcount++;
161 matchmask |= 1 << comp;
162 }
163 }
164 if (matchcount > best_matchcount) {
165 best_swizzle = sd;
166 best_matchcount = matchcount;
167 best_matchmask = matchmask;
168 if (matchmask == (dst.WriteMask & WRITEMASK_XYZ))
169 break;
170 }
171 }
172
173 if ((src.NegateBase & best_matchmask) != 0) {
174 best_matchmask &= src.NegateBase;
175 rgbnegate = !src.NegateAbs;
176 } else {
177 rgbnegate = src.NegateAbs;
178 }
179
180 struct prog_instruction *inst;
181
182 _mesa_insert_instructions(s->Program, s->IP, 1);
183 inst = s->Program->Instructions + s->IP++;
184 inst->Opcode = OPCODE_MOV;
185 inst->DstReg = dst;
186 inst->DstReg.WriteMask &= (best_matchmask | WRITEMASK_W);
187 inst->SrcReg[0] = src;
188 /* Note: We rely on NqSSA/DCE to set unused swizzle components to NIL */
189
190 dst.WriteMask &= ~inst->DstReg.WriteMask;
191 }
192 }
193
194
195 /**
196 * Translate an RGB (XYZ) swizzle into the hardware code for the given
197 * instruction source.
198 */
199 GLuint r300FPTranslateRGBSwizzle(GLuint src, GLuint swizzle)
200 {
201 const struct swizzle_data* sd = lookup_native_swizzle(swizzle);
202
203 if (!sd) {
204 _mesa_printf("Not a native swizzle: %08x\n", swizzle);
205 return 0;
206 }
207
208 return sd->base + src*sd->stride;
209 }
210
211
212 /**
213 * Translate an Alpha (W) swizzle into the hardware code for the given
214 * instruction source.
215 */
216 GLuint r300FPTranslateAlphaSwizzle(GLuint src, GLuint swizzle)
217 {
218 if (swizzle < 3)
219 return swizzle + 3*src;
220
221 switch(swizzle) {
222 case SWIZZLE_W: return R300_ALU_ARGA_SRC0A + src;
223 case SWIZZLE_ONE: return R300_ALU_ARGA_ONE;
224 case SWIZZLE_ZERO: return R300_ALU_ARGA_ZERO;
225 default: return R300_ALU_ARGA_ONE;
226 }
227 }