Remove src/mesa and src/mesa/main from gallium source include paths.
[mesa.git] / src / gallium / drivers / i915simple / i915_fpc_emit.c
1 /**************************************************************************
2 *
3 * Copyright 2003 Tungsten Graphics, Inc., Cedar Park, Texas.
4 * All Rights Reserved.
5 *
6 * Permission is hereby granted, free of charge, to any person obtaining a
7 * 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, sub license, 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 portions
16 * of the Software.
17 *
18 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
19 * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
20 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT.
21 * IN NO EVENT SHALL TUNGSTEN GRAPHICS AND/OR ITS SUPPLIERS BE LIABLE FOR
22 * ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
23 * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
24 * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
25 *
26 **************************************************************************/
27
28 #include "i915_reg.h"
29 #include "i915_context.h"
30 #include "i915_fpc.h"
31
32
33 #define A0_DEST( reg ) (((reg)&UREG_TYPE_NR_MASK)>>UREG_A0_DEST_SHIFT_LEFT)
34 #define D0_DEST( reg ) (((reg)&UREG_TYPE_NR_MASK)>>UREG_A0_DEST_SHIFT_LEFT)
35 #define T0_DEST( reg ) (((reg)&UREG_TYPE_NR_MASK)>>UREG_A0_DEST_SHIFT_LEFT)
36 #define A0_SRC0( reg ) (((reg)&UREG_MASK)>>UREG_A0_SRC0_SHIFT_LEFT)
37 #define A1_SRC0( reg ) (((reg)&UREG_MASK)<<UREG_A1_SRC0_SHIFT_RIGHT)
38 #define A1_SRC1( reg ) (((reg)&UREG_MASK)>>UREG_A1_SRC1_SHIFT_LEFT)
39 #define A2_SRC1( reg ) (((reg)&UREG_MASK)<<UREG_A2_SRC1_SHIFT_RIGHT)
40 #define A2_SRC2( reg ) (((reg)&UREG_MASK)>>UREG_A2_SRC2_SHIFT_LEFT)
41
42 /* These are special, and don't have swizzle/negate bits.
43 */
44 #define T0_SAMPLER( reg ) (GET_UREG_NR(reg)<<T0_SAMPLER_NR_SHIFT)
45 #define T1_ADDRESS_REG( reg ) ((GET_UREG_NR(reg)<<T1_ADDRESS_REG_NR_SHIFT) | \
46 (GET_UREG_TYPE(reg)<<T1_ADDRESS_REG_TYPE_SHIFT))
47
48
49 /* Macros for translating UREG's into the various register fields used
50 * by the I915 programmable unit.
51 */
52 #define UREG_A0_DEST_SHIFT_LEFT (UREG_TYPE_SHIFT - A0_DEST_TYPE_SHIFT)
53 #define UREG_A0_SRC0_SHIFT_LEFT (UREG_TYPE_SHIFT - A0_SRC0_TYPE_SHIFT)
54 #define UREG_A1_SRC0_SHIFT_RIGHT (A1_SRC0_CHANNEL_W_SHIFT - UREG_CHANNEL_W_SHIFT)
55 #define UREG_A1_SRC1_SHIFT_LEFT (UREG_TYPE_SHIFT - A1_SRC1_TYPE_SHIFT)
56 #define UREG_A2_SRC1_SHIFT_RIGHT (A2_SRC1_CHANNEL_W_SHIFT - UREG_CHANNEL_W_SHIFT)
57 #define UREG_A2_SRC2_SHIFT_LEFT (UREG_TYPE_SHIFT - A2_SRC2_TYPE_SHIFT)
58
59 #define UREG_MASK 0xffffff00
60 #define UREG_TYPE_NR_MASK ((REG_TYPE_MASK << UREG_TYPE_SHIFT) | \
61 (REG_NR_MASK << UREG_NR_SHIFT))
62
63
64 #define I915_CONSTFLAG_PARAM 0x1f
65
66 uint
67 i915_get_temp(struct i915_fp_compile *p)
68 {
69 int bit = ffs(~p->temp_flag);
70 if (!bit) {
71 i915_program_error(p, "i915_get_temp: out of temporaries\n");
72 return 0;
73 }
74
75 p->temp_flag |= 1 << (bit - 1);
76 return UREG(REG_TYPE_R, (bit - 1));
77 }
78
79
80 uint
81 i915_get_utemp(struct i915_fp_compile * p)
82 {
83 int bit = ffs(~p->utemp_flag);
84 if (!bit) {
85 i915_program_error(p, "i915_get_utemp: out of temporaries\n");
86 return 0;
87 }
88
89 p->utemp_flag |= 1 << (bit - 1);
90 return UREG(REG_TYPE_U, (bit - 1));
91 }
92
93 void
94 i915_release_utemps(struct i915_fp_compile *p)
95 {
96 p->utemp_flag = ~0x7;
97 }
98
99
100 uint
101 i915_emit_decl(struct i915_fp_compile *p,
102 uint type, uint nr, uint d0_flags)
103 {
104 uint reg = UREG(type, nr);
105
106 if (type == REG_TYPE_T) {
107 if (p->decl_t & (1 << nr))
108 return reg;
109
110 p->decl_t |= (1 << nr);
111 }
112 else if (type == REG_TYPE_S) {
113 if (p->decl_s & (1 << nr))
114 return reg;
115
116 p->decl_s |= (1 << nr);
117 }
118 else
119 return reg;
120
121 *(p->decl++) = (D0_DCL | D0_DEST(reg) | d0_flags);
122 *(p->decl++) = D1_MBZ;
123 *(p->decl++) = D2_MBZ;
124
125 p->nr_decl_insn++;
126 return reg;
127 }
128
129 uint
130 i915_emit_arith(struct i915_fp_compile * p,
131 uint op,
132 uint dest,
133 uint mask,
134 uint saturate, uint src0, uint src1, uint src2)
135 {
136 uint c[3];
137 uint nr_const = 0;
138
139 assert(GET_UREG_TYPE(dest) != REG_TYPE_CONST);
140 dest = UREG(GET_UREG_TYPE(dest), GET_UREG_NR(dest));
141 assert(dest);
142
143 if (GET_UREG_TYPE(src0) == REG_TYPE_CONST)
144 c[nr_const++] = 0;
145 if (GET_UREG_TYPE(src1) == REG_TYPE_CONST)
146 c[nr_const++] = 1;
147 if (GET_UREG_TYPE(src2) == REG_TYPE_CONST)
148 c[nr_const++] = 2;
149
150 /* Recursively call this function to MOV additional const values
151 * into temporary registers. Use utemp registers for this -
152 * currently shouldn't be possible to run out, but keep an eye on
153 * this.
154 */
155 if (nr_const > 1) {
156 uint s[3], first, i, old_utemp_flag;
157
158 s[0] = src0;
159 s[1] = src1;
160 s[2] = src2;
161 old_utemp_flag = p->utemp_flag;
162
163 first = GET_UREG_NR(s[c[0]]);
164 for (i = 1; i < nr_const; i++) {
165 if (GET_UREG_NR(s[c[i]]) != first) {
166 uint tmp = i915_get_utemp(p);
167
168 i915_emit_arith(p, A0_MOV, tmp, A0_DEST_CHANNEL_ALL, 0,
169 s[c[i]], 0, 0);
170 s[c[i]] = tmp;
171 }
172 }
173
174 src0 = s[0];
175 src1 = s[1];
176 src2 = s[2];
177 p->utemp_flag = old_utemp_flag; /* restore */
178 }
179
180 *(p->csr++) = (op | A0_DEST(dest) | mask | saturate | A0_SRC0(src0));
181 *(p->csr++) = (A1_SRC0(src0) | A1_SRC1(src1));
182 *(p->csr++) = (A2_SRC1(src1) | A2_SRC2(src2));
183
184 p->nr_alu_insn++;
185 return dest;
186 }
187
188 uint i915_emit_texld( struct i915_fp_compile *p,
189 uint dest,
190 uint destmask,
191 uint sampler,
192 uint coord,
193 uint op )
194 {
195 uint k = UREG(GET_UREG_TYPE(coord), GET_UREG_NR(coord));
196 if (coord != k) {
197 /* No real way to work around this in the general case - need to
198 * allocate and declare a new temporary register (a utemp won't
199 * do). Will fallback for now.
200 */
201 i915_program_error(p, "Can't (yet) swizzle TEX arguments");
202 assert(0);
203 return 0;
204 }
205
206 /* Don't worry about saturate as we only support
207 */
208 if (destmask != A0_DEST_CHANNEL_ALL) {
209 uint tmp = i915_get_utemp(p);
210 i915_emit_texld( p, tmp, A0_DEST_CHANNEL_ALL, sampler, coord, op );
211 i915_emit_arith( p, A0_MOV, dest, destmask, 0, tmp, 0, 0 );
212 return dest;
213 }
214 else {
215 assert(GET_UREG_TYPE(dest) != REG_TYPE_CONST);
216 assert(dest = UREG(GET_UREG_TYPE(dest), GET_UREG_NR(dest)));
217
218 if (GET_UREG_TYPE(coord) != REG_TYPE_T) {
219 p->nr_tex_indirect++;
220 }
221
222 *(p->csr++) = (op |
223 T0_DEST( dest ) |
224 T0_SAMPLER( sampler ));
225
226 *(p->csr++) = T1_ADDRESS_REG( coord );
227 *(p->csr++) = T2_MBZ;
228
229 p->nr_tex_insn++;
230 return dest;
231 }
232 }
233
234
235 uint
236 i915_emit_const1f(struct i915_fp_compile * p, float c0)
237 {
238 unsigned reg, idx;
239
240 if (c0 == 0.0)
241 return swizzle(UREG(REG_TYPE_R, 0), ZERO, ZERO, ZERO, ZERO);
242 if (c0 == 1.0)
243 return swizzle(UREG(REG_TYPE_R, 0), ONE, ONE, ONE, ONE);
244
245 for (reg = 0; reg < I915_MAX_CONSTANT; reg++) {
246 if (p->constant_flags[reg] == I915_CONSTFLAG_PARAM)
247 continue;
248 for (idx = 0; idx < 4; idx++) {
249 if (!(p->constant_flags[reg] & (1 << idx)) ||
250 p->constants[reg][idx] == c0) {
251 p->constants[reg][idx] = c0;
252 p->constant_flags[reg] |= 1 << idx;
253 if (reg + 1 > p->num_constants)
254 p->num_constants = reg + 1;
255 return swizzle(UREG(REG_TYPE_CONST, reg), idx, ZERO, ZERO, ONE);
256 }
257 }
258 }
259
260 i915_program_error(p, "i915_emit_const1f: out of constants\n");
261 return 0;
262 }
263
264 uint
265 i915_emit_const2f(struct i915_fp_compile * p, float c0, float c1)
266 {
267 unsigned reg, idx;
268
269 if (c0 == 0.0)
270 return swizzle(i915_emit_const1f(p, c1), ZERO, X, Z, W);
271 if (c0 == 1.0)
272 return swizzle(i915_emit_const1f(p, c1), ONE, X, Z, W);
273
274 if (c1 == 0.0)
275 return swizzle(i915_emit_const1f(p, c0), X, ZERO, Z, W);
276 if (c1 == 1.0)
277 return swizzle(i915_emit_const1f(p, c0), X, ONE, Z, W);
278
279 for (reg = 0; reg < I915_MAX_CONSTANT; reg++) {
280 if (p->constant_flags[reg] == 0xf ||
281 p->constant_flags[reg] == I915_CONSTFLAG_PARAM)
282 continue;
283 for (idx = 0; idx < 3; idx++) {
284 if (!(p->constant_flags[reg] & (3 << idx))) {
285 p->constants[reg][idx + 0] = c0;
286 p->constants[reg][idx + 1] = c1;
287 p->constant_flags[reg] |= 3 << idx;
288 if (reg + 1 > p->num_constants)
289 p->num_constants = reg + 1;
290 return swizzle(UREG(REG_TYPE_CONST, reg), idx, idx + 1, ZERO, ONE);
291 }
292 }
293 }
294
295 i915_program_error(p, "i915_emit_const2f: out of constants\n");
296 return 0;
297 }
298
299
300
301 uint
302 i915_emit_const4f(struct i915_fp_compile * p,
303 float c0, float c1, float c2, float c3)
304 {
305 unsigned reg;
306
307 for (reg = 0; reg < I915_MAX_CONSTANT; reg++) {
308 if (p->constant_flags[reg] == 0xf &&
309 p->constants[reg][0] == c0 &&
310 p->constants[reg][1] == c1 &&
311 p->constants[reg][2] == c2 &&
312 p->constants[reg][3] == c3) {
313 return UREG(REG_TYPE_CONST, reg);
314 }
315 else if (p->constant_flags[reg] == 0) {
316
317 p->constants[reg][0] = c0;
318 p->constants[reg][1] = c1;
319 p->constants[reg][2] = c2;
320 p->constants[reg][3] = c3;
321 p->constant_flags[reg] = 0xf;
322 if (reg + 1 > p->num_constants)
323 p->num_constants = reg + 1;
324 return UREG(REG_TYPE_CONST, reg);
325 }
326 }
327
328 i915_program_error(p, "i915_emit_const4f: out of constants\n");
329 return 0;
330 }
331
332
333 uint
334 i915_emit_const4fv(struct i915_fp_compile * p, const float * c)
335 {
336 return i915_emit_const4f(p, c[0], c[1], c[2], c[3]);
337 }
338
339
340 #if 00000/*UNUSED*/
341 /* Reserve a slot in the constant file for a Mesa state parameter.
342 * These will later need to be tracked on statechanges, but that is
343 * done elsewhere.
344 */
345 uint
346 i915_emit_param4fv(struct i915_fp_compile * p, const float * values)
347 {
348 struct i915_fragment_program *fp = p->fp;
349 int i;
350
351 for (i = 0; i < fp->nr_params; i++) {
352 if (fp->param[i].values == values)
353 return UREG(REG_TYPE_CONST, fp->param[i].reg);
354 }
355
356 if (p->constants->nr_constants == I915_MAX_CONSTANT ||
357 fp->nr_params == I915_MAX_CONSTANT) {
358 i915_program_error(p, "i915_emit_param4fv: out of constants\n");
359 return 0;
360 }
361
362 {
363 int reg = p->constants->nr_constants++;
364 int i = fp->nr_params++;
365
366 assert (p->constant_flags[reg] == 0);
367 p->constant_flags[reg] = I915_CONSTFLAG_PARAM;
368
369 fp->param[i].values = values;
370 fp->param[i].reg = reg;
371
372 return UREG(REG_TYPE_CONST, reg);
373 }
374 }
375 #endif