1 /**************************************************************************
3 * Copyright 2003 Tungsten Graphics, Inc., Cedar Park, Texas.
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:
14 * The above copyright notice and this permission notice (including the
15 * next paragraph) shall be included in all copies or substantial portions
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.
26 **************************************************************************/
29 #include "i915_context.h"
31 #include "util/u_math.h"
34 #define A0_DEST( reg ) (((reg)&UREG_TYPE_NR_MASK)>>UREG_A0_DEST_SHIFT_LEFT)
35 #define D0_DEST( reg ) (((reg)&UREG_TYPE_NR_MASK)>>UREG_A0_DEST_SHIFT_LEFT)
36 #define T0_DEST( reg ) (((reg)&UREG_TYPE_NR_MASK)>>UREG_A0_DEST_SHIFT_LEFT)
37 #define A0_SRC0( reg ) (((reg)&UREG_MASK)>>UREG_A0_SRC0_SHIFT_LEFT)
38 #define A1_SRC0( reg ) (((reg)&UREG_MASK)<<UREG_A1_SRC0_SHIFT_RIGHT)
39 #define A1_SRC1( reg ) (((reg)&UREG_MASK)>>UREG_A1_SRC1_SHIFT_LEFT)
40 #define A2_SRC1( reg ) (((reg)&UREG_MASK)<<UREG_A2_SRC1_SHIFT_RIGHT)
41 #define A2_SRC2( reg ) (((reg)&UREG_MASK)>>UREG_A2_SRC2_SHIFT_LEFT)
43 /* These are special, and don't have swizzle/negate bits.
45 #define T0_SAMPLER( reg ) (GET_UREG_NR(reg)<<T0_SAMPLER_NR_SHIFT)
46 #define T1_ADDRESS_REG( reg ) ((GET_UREG_NR(reg)<<T1_ADDRESS_REG_NR_SHIFT) | \
47 (GET_UREG_TYPE(reg)<<T1_ADDRESS_REG_TYPE_SHIFT))
50 /* Macros for translating UREG's into the various register fields used
51 * by the I915 programmable unit.
53 #define UREG_A0_DEST_SHIFT_LEFT (UREG_TYPE_SHIFT - A0_DEST_TYPE_SHIFT)
54 #define UREG_A0_SRC0_SHIFT_LEFT (UREG_TYPE_SHIFT - A0_SRC0_TYPE_SHIFT)
55 #define UREG_A1_SRC0_SHIFT_RIGHT (A1_SRC0_CHANNEL_W_SHIFT - UREG_CHANNEL_W_SHIFT)
56 #define UREG_A1_SRC1_SHIFT_LEFT (UREG_TYPE_SHIFT - A1_SRC1_TYPE_SHIFT)
57 #define UREG_A2_SRC1_SHIFT_RIGHT (A2_SRC1_CHANNEL_W_SHIFT - UREG_CHANNEL_W_SHIFT)
58 #define UREG_A2_SRC2_SHIFT_LEFT (UREG_TYPE_SHIFT - A2_SRC2_TYPE_SHIFT)
60 #define UREG_MASK 0xffffff00
61 #define UREG_TYPE_NR_MASK ((REG_TYPE_MASK << UREG_TYPE_SHIFT) | \
62 (REG_NR_MASK << UREG_NR_SHIFT))
66 i915_get_temp(struct i915_fp_compile *p)
68 int bit = ffs(~p->temp_flag);
70 i915_program_error(p, "i915_get_temp: out of temporaries\n");
74 p->temp_flag |= 1 << (bit - 1);
80 i915_release_temp(struct i915_fp_compile *p, int reg)
82 p->temp_flag &= ~(1 << reg);
87 * Get unpreserved temporary, a temp whose value is not preserved between
91 i915_get_utemp(struct i915_fp_compile * p)
93 int bit = ffs(~p->utemp_flag);
95 i915_program_error(p, "i915_get_utemp: out of temporaries\n");
99 p->utemp_flag |= 1 << (bit - 1);
100 return UREG(REG_TYPE_U, (bit - 1));
104 i915_release_utemps(struct i915_fp_compile *p)
106 p->utemp_flag = ~0x7;
111 i915_emit_decl(struct i915_fp_compile *p,
112 uint type, uint nr, uint d0_flags)
114 uint reg = UREG(type, nr);
116 if (type == REG_TYPE_T) {
117 if (p->decl_t & (1 << nr))
120 p->decl_t |= (1 << nr);
122 else if (type == REG_TYPE_S) {
123 if (p->decl_s & (1 << nr))
126 p->decl_s |= (1 << nr);
131 if (p->decl< p->declarations + I915_PROGRAM_SIZE) {
132 *(p->decl++) = (D0_DCL | D0_DEST(reg) | d0_flags);
133 *(p->decl++) = D1_MBZ;
134 *(p->decl++) = D2_MBZ;
137 i915_program_error(p, "Out of declarations\n");
144 i915_emit_arith(struct i915_fp_compile * p,
148 uint saturate, uint src0, uint src1, uint src2)
153 assert(GET_UREG_TYPE(dest) != REG_TYPE_CONST);
154 dest = UREG(GET_UREG_TYPE(dest), GET_UREG_NR(dest));
157 if (GET_UREG_TYPE(src0) == REG_TYPE_CONST)
159 if (GET_UREG_TYPE(src1) == REG_TYPE_CONST)
161 if (GET_UREG_TYPE(src2) == REG_TYPE_CONST)
164 /* Recursively call this function to MOV additional const values
165 * into temporary registers. Use utemp registers for this -
166 * currently shouldn't be possible to run out, but keep an eye on
170 uint s[3], first, i, old_utemp_flag;
175 old_utemp_flag = p->utemp_flag;
177 first = GET_UREG_NR(s[c[0]]);
178 for (i = 1; i < nr_const; i++) {
179 if (GET_UREG_NR(s[c[i]]) != first) {
180 uint tmp = i915_get_utemp(p);
182 i915_emit_arith(p, A0_MOV, tmp, A0_DEST_CHANNEL_ALL, 0,
191 p->utemp_flag = old_utemp_flag; /* restore */
194 if (p->csr< p->program + I915_PROGRAM_SIZE) {
195 *(p->csr++) = (op | A0_DEST(dest) | mask | saturate | A0_SRC0(src0));
196 *(p->csr++) = (A1_SRC0(src0) | A1_SRC1(src1));
197 *(p->csr++) = (A2_SRC1(src1) | A2_SRC2(src2));
200 i915_program_error(p, "Out of instructions\n");
208 * Emit a texture load or texkill instruction.
209 * \param dest the dest i915 register
210 * \param destmask the dest register writemask
211 * \param sampler the i915 sampler register
212 * \param coord the i915 source texcoord operand
213 * \param opcode the instruction opcode
215 uint i915_emit_texld( struct i915_fp_compile *p,
222 const uint k = UREG(GET_UREG_TYPE(coord), GET_UREG_NR(coord));
226 /* texcoord is swizzled or negated. Need to allocate a new temporary
227 * register (a utemp / unpreserved temp) won't do.
231 temp = i915_get_temp(p); /* get temp reg index */
232 tempReg = UREG(REG_TYPE_R, temp); /* make i915 register */
234 i915_emit_arith( p, A0_MOV,
235 tempReg, A0_DEST_CHANNEL_ALL, /* dest reg, writemask */
237 coord, 0, 0 ); /* src0, src1, src2 */
239 /* new src texcoord is tempReg */
243 /* Don't worry about saturate as we only support
245 if (destmask != A0_DEST_CHANNEL_ALL) {
246 /* if not writing to XYZW... */
247 uint tmp = i915_get_utemp(p);
248 i915_emit_texld( p, tmp, A0_DEST_CHANNEL_ALL, sampler, coord, opcode );
249 i915_emit_arith( p, A0_MOV, dest, destmask, 0, tmp, 0, 0 );
250 /* XXX release utemp here? */
253 assert(GET_UREG_TYPE(dest) != REG_TYPE_CONST);
254 assert(dest == UREG(GET_UREG_TYPE(dest), GET_UREG_NR(dest)));
256 /* is the sampler coord a texcoord input reg? */
257 if (GET_UREG_TYPE(coord) != REG_TYPE_T) {
258 p->nr_tex_indirect++;
261 if (p->csr< p->program + I915_PROGRAM_SIZE) {
262 *(p->csr++) = (opcode |
264 T0_SAMPLER( sampler ));
266 *(p->csr++) = T1_ADDRESS_REG( coord );
267 *(p->csr++) = T2_MBZ;
270 i915_program_error(p, "Out of instructions\n");
276 i915_release_temp(p, temp);
283 i915_emit_const1f(struct i915_fp_compile * p, float c0)
285 struct i915_fragment_shader *ifs = p->shader;
289 return swizzle(UREG(REG_TYPE_R, 0), ZERO, ZERO, ZERO, ZERO);
291 return swizzle(UREG(REG_TYPE_R, 0), ONE, ONE, ONE, ONE);
293 for (reg = 0; reg < I915_MAX_CONSTANT; reg++) {
294 if (ifs->constant_flags[reg] == I915_CONSTFLAG_USER)
296 for (idx = 0; idx < 4; idx++) {
297 if (!(ifs->constant_flags[reg] & (1 << idx)) ||
298 ifs->constants[reg][idx] == c0) {
299 ifs->constants[reg][idx] = c0;
300 ifs->constant_flags[reg] |= 1 << idx;
301 if (reg + 1 > ifs->num_constants)
302 ifs->num_constants = reg + 1;
303 return swizzle(UREG(REG_TYPE_CONST, reg), idx, ZERO, ZERO, ONE);
308 i915_program_error(p, "i915_emit_const1f: out of constants\n");
313 i915_emit_const2f(struct i915_fp_compile * p, float c0, float c1)
315 struct i915_fragment_shader *ifs = p->shader;
319 return swizzle(i915_emit_const1f(p, c1), ZERO, X, Z, W);
321 return swizzle(i915_emit_const1f(p, c1), ONE, X, Z, W);
324 return swizzle(i915_emit_const1f(p, c0), X, ZERO, Z, W);
326 return swizzle(i915_emit_const1f(p, c0), X, ONE, Z, W);
328 // XXX emit swizzle here for 0, 1, -1 and any combination thereof
329 // we can use swizzle + neg for that
330 for (reg = 0; reg < I915_MAX_CONSTANT; reg++) {
331 if (ifs->constant_flags[reg] == 0xf ||
332 ifs->constant_flags[reg] == I915_CONSTFLAG_USER)
334 for (idx = 0; idx < 3; idx++) {
335 if (!(ifs->constant_flags[reg] & (3 << idx))) {
336 ifs->constants[reg][idx + 0] = c0;
337 ifs->constants[reg][idx + 1] = c1;
338 ifs->constant_flags[reg] |= 3 << idx;
339 if (reg + 1 > ifs->num_constants)
340 ifs->num_constants = reg + 1;
341 return swizzle(UREG(REG_TYPE_CONST, reg), idx, idx + 1, ZERO, ONE);
346 i915_program_error(p, "i915_emit_const2f: out of constants\n");
351 i915_emit_const4f(struct i915_fp_compile * p,
352 float c0, float c1, float c2, float c3)
354 struct i915_fragment_shader *ifs = p->shader;
357 for (reg = 0; reg < I915_MAX_CONSTANT; reg++) {
358 if (ifs->constant_flags[reg] == 0xf &&
359 ifs->constants[reg][0] == c0 &&
360 ifs->constants[reg][1] == c1 &&
361 ifs->constants[reg][2] == c2 &&
362 ifs->constants[reg][3] == c3) {
363 return UREG(REG_TYPE_CONST, reg);
365 else if (ifs->constant_flags[reg] == 0) {
367 ifs->constants[reg][0] = c0;
368 ifs->constants[reg][1] = c1;
369 ifs->constants[reg][2] = c2;
370 ifs->constants[reg][3] = c3;
371 ifs->constant_flags[reg] = 0xf;
372 if (reg + 1 > ifs->num_constants)
373 ifs->num_constants = reg + 1;
374 return UREG(REG_TYPE_CONST, reg);
378 i915_program_error(p, "i915_emit_const4f: out of constants\n");
384 i915_emit_const4fv(struct i915_fp_compile * p, const float * c)
386 return i915_emit_const4f(p, c[0], c[1], c[2], c[3]);