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