Merge branch 'mesa_7_5_branch'
[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.Negate = NEGATE_NONE;
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.Negate)
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.Negate & relevant) && ((reg.Negate & 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.Negate = NEGATE_NONE;
141
142 while(dst.WriteMask) {
143 const struct swizzle_data *best_swizzle = 0;
144 GLuint best_matchcount = 0;
145 GLuint best_matchmask = 0;
146 int i, comp;
147
148 for(i = 0; i < num_native_swizzles; ++i) {
149 const struct swizzle_data *sd = &native_swizzles[i];
150 GLuint matchcount = 0;
151 GLuint matchmask = 0;
152 for(comp = 0; comp < 3; ++comp) {
153 if (!GET_BIT(dst.WriteMask, comp))
154 continue;
155 GLuint swz = GET_SWZ(src.Swizzle, comp);
156 if (swz == SWIZZLE_NIL)
157 continue;
158 if (swz == GET_SWZ(sd->hash, comp)) {
159 /* check if the negate bit of current component
160 * is the same for already matched components */
161 if (matchmask && (!!(src.Negate & matchmask) != !!(src.Negate & (1 << comp))))
162 continue;
163
164 matchcount++;
165 matchmask |= 1 << comp;
166 }
167 }
168 if (matchcount > best_matchcount) {
169 best_swizzle = sd;
170 best_matchcount = matchcount;
171 best_matchmask = matchmask;
172 if (matchmask == (dst.WriteMask & WRITEMASK_XYZ))
173 break;
174 }
175 }
176
177 struct prog_instruction *inst;
178
179 _mesa_insert_instructions(s->Program, s->IP, 1);
180 inst = s->Program->Instructions + s->IP++;
181 inst->Opcode = OPCODE_MOV;
182 inst->DstReg = dst;
183 inst->DstReg.WriteMask &= (best_matchmask | WRITEMASK_W);
184 inst->SrcReg[0] = src;
185 inst->SrcReg[0].Negate = (best_matchmask & src.Negate) ? NEGATE_XYZW : NEGATE_NONE;
186 /* Note: We rely on NqSSA/DCE to set unused swizzle components to NIL */
187
188 dst.WriteMask &= ~inst->DstReg.WriteMask;
189 }
190 }
191
192
193 /**
194 * Translate an RGB (XYZ) swizzle into the hardware code for the given
195 * instruction source.
196 */
197 GLuint r300FPTranslateRGBSwizzle(GLuint src, GLuint swizzle)
198 {
199 const struct swizzle_data* sd = lookup_native_swizzle(swizzle);
200
201 if (!sd) {
202 _mesa_printf("Not a native swizzle: %08x\n", swizzle);
203 return 0;
204 }
205
206 return sd->base + src*sd->stride;
207 }
208
209
210 /**
211 * Translate an Alpha (W) swizzle into the hardware code for the given
212 * instruction source.
213 */
214 GLuint r300FPTranslateAlphaSwizzle(GLuint src, GLuint swizzle)
215 {
216 if (swizzle < 3)
217 return swizzle + 3*src;
218
219 switch(swizzle) {
220 case SWIZZLE_W: return R300_ALU_ARGA_SRC0A + src;
221 case SWIZZLE_ONE: return R300_ALU_ARGA_ONE;
222 case SWIZZLE_ZERO: return R300_ALU_ARGA_ZERO;
223 default: return R300_ALU_ARGA_ONE;
224 }
225 }