1 /**************************************************************************
2 *
3 * Copyright 2003 VMware, Inc.
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 VMWARE 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 "util/u_math.h"
29 #include "i915_context.h"
30 #include "i915_fpc.h"
31 #include "i915_reg.h"
32
33 uint32_t
i915_get_temp(struct i915_fp_compile * p)34 i915_get_temp(struct i915_fp_compile *p)
35 {
36 int bit = ffs(~p->temp_flag);
37 if (!bit) {
38 i915_program_error(p, "i915_get_temp: out of temporaries");
39 return 0;
40 }
41
42 p->temp_flag |= 1 << (bit - 1);
43 return bit - 1;
44 }
45
46 static void
i915_release_temp(struct i915_fp_compile * p,int reg)47 i915_release_temp(struct i915_fp_compile *p, int reg)
48 {
49 p->temp_flag &= ~(1 << reg);
50 }
51
52 /**
53 * Get unpreserved temporary, a temp whose value is not preserved between
54 * PS program phases.
55 */
56 uint32_t
i915_get_utemp(struct i915_fp_compile * p)57 i915_get_utemp(struct i915_fp_compile *p)
58 {
59 int bit = ffs(~p->utemp_flag);
60 if (!bit) {
61 i915_program_error(p, "i915_get_utemp: out of temporaries");
62 return 0;
63 }
64
65 p->utemp_flag |= 1 << (bit - 1);
66 return UREG(REG_TYPE_U, (bit - 1));
67 }
68
69 void
i915_release_utemps(struct i915_fp_compile * p)70 i915_release_utemps(struct i915_fp_compile *p)
71 {
72 p->utemp_flag = ~0x7;
73 }
74
75 uint32_t
i915_emit_decl(struct i915_fp_compile * p,uint32_t type,uint32_t nr,uint32_t d0_flags)76 i915_emit_decl(struct i915_fp_compile *p, uint32_t type, uint32_t nr,
77 uint32_t d0_flags)
78 {
79 uint32_t reg = UREG(type, nr);
80
81 if (type == REG_TYPE_T) {
82 if (p->decl_t & (1 << nr))
83 return reg;
84
85 p->decl_t |= (1 << nr);
86 } else if (type == REG_TYPE_S) {
87 if (p->decl_s & (1 << nr))
88 return reg;
89
90 p->decl_s |= (1 << nr);
91 } else
92 return reg;
93
94 if (p->decl < p->declarations + I915_PROGRAM_SIZE) {
95 *(p->decl++) = (D0_DCL | D0_DEST(reg) | d0_flags);
96 *(p->decl++) = D1_MBZ;
97 *(p->decl++) = D2_MBZ;
98 } else
99 i915_program_error(p, "Out of declarations");
100
101 p->nr_decl_insn++;
102 return reg;
103 }
104
105 uint32_t
i915_emit_arith(struct i915_fp_compile * p,uint32_t op,uint32_t dest,uint32_t mask,uint32_t saturate,uint32_t src0,uint32_t src1,uint32_t src2)106 i915_emit_arith(struct i915_fp_compile *p, uint32_t op, uint32_t dest,
107 uint32_t mask, uint32_t saturate, uint32_t src0, uint32_t src1,
108 uint32_t src2)
109 {
110 uint32_t c[3];
111 uint32_t nr_const = 0;
112
113 assert(GET_UREG_TYPE(dest) != REG_TYPE_CONST);
114 dest = UREG(GET_UREG_TYPE(dest), GET_UREG_NR(dest));
115 assert(dest);
116
117 if (GET_UREG_TYPE(src0) == REG_TYPE_CONST)
118 c[nr_const++] = 0;
119 if (GET_UREG_TYPE(src1) == REG_TYPE_CONST)
120 c[nr_const++] = 1;
121 if (GET_UREG_TYPE(src2) == REG_TYPE_CONST)
122 c[nr_const++] = 2;
123
124 /* Recursively call this function to MOV additional const values
125 * into temporary registers. Use utemp registers for this -
126 * currently shouldn't be possible to run out, but keep an eye on
127 * this.
128 */
129 if (nr_const > 1) {
130 uint32_t s[3], first, i, old_utemp_flag;
131
132 s[0] = src0;
133 s[1] = src1;
134 s[2] = src2;
135 old_utemp_flag = p->utemp_flag;
136
137 first = GET_UREG_NR(s[c[0]]);
138 for (i = 1; i < nr_const; i++) {
139 if (GET_UREG_NR(s[c[i]]) != first) {
140 uint32_t tmp = i915_get_utemp(p);
141
142 i915_emit_arith(p, A0_MOV, tmp, A0_DEST_CHANNEL_ALL, 0, s[c[i]], 0,
143 0);
144 s[c[i]] = tmp;
145 }
146 }
147
148 src0 = s[0];
149 src1 = s[1];
150 src2 = s[2];
151 p->utemp_flag = old_utemp_flag; /* restore */
152 }
153
154 if (p->csr < p->program + I915_PROGRAM_SIZE) {
155 *(p->csr++) = (op | A0_DEST(dest) | mask | saturate | A0_SRC0(src0));
156 *(p->csr++) = (A1_SRC0(src0) | A1_SRC1(src1));
157 *(p->csr++) = (A2_SRC1(src1) | A2_SRC2(src2));
158 }
159
160 if (GET_UREG_TYPE(dest) == REG_TYPE_R)
161 p->register_phases[GET_UREG_NR(dest)] = p->nr_tex_indirect;
162
163 p->nr_alu_insn++;
164 return dest;
165 }
166
167 /**
168 * Emit a texture load or texkill instruction.
169 * \param dest the dest i915 register
170 * \param destmask the dest register writemask
171 * \param sampler the i915 sampler register
172 * \param coord the i915 source texcoord operand
173 * \param opcode the instruction opcode
174 */
175 uint32_t
i915_emit_texld(struct i915_fp_compile * p,uint32_t dest,uint32_t destmask,uint32_t sampler,uint32_t coord,uint32_t opcode,uint32_t coord_mask)176 i915_emit_texld(struct i915_fp_compile *p, uint32_t dest, uint32_t destmask,
177 uint32_t sampler, uint32_t coord, uint32_t opcode,
178 uint32_t coord_mask)
179 {
180 const uint32_t k = UREG(GET_UREG_TYPE(coord), GET_UREG_NR(coord));
181
182 int temp = -1;
183
184 uint32_t coord_used = 0xf << UREG_CHANNEL_X_SHIFT;
185 if (coord_mask & TGSI_WRITEMASK_Y)
186 coord_used |= 0xf << UREG_CHANNEL_Y_SHIFT;
187 if (coord_mask & TGSI_WRITEMASK_Z)
188 coord_used |= 0xf << UREG_CHANNEL_Z_SHIFT;
189 if (coord_mask & TGSI_WRITEMASK_W)
190 coord_used |= 0xf << UREG_CHANNEL_W_SHIFT;
191
192 if ((coord & coord_used) != (k & coord_used) ||
193 GET_UREG_TYPE(coord) == REG_TYPE_CONST) {
194 /* texcoord is swizzled or negated. Need to allocate a new temporary
195 * register (a utemp / unpreserved temp) won't do.
196 */
197 uint32_t tempReg;
198
199 temp = i915_get_temp(p); /* get temp reg index */
200 tempReg = UREG(REG_TYPE_R, temp); /* make i915 register */
201
202 i915_emit_arith(p, A0_MOV, tempReg,
203 A0_DEST_CHANNEL_ALL, /* dest reg, writemask */
204 0, /* saturate */
205 coord, 0, 0); /* src0, src1, src2 */
206
207 /* new src texcoord is tempReg */
208 coord = tempReg;
209 }
210
211 /* Don't worry about saturate as we only support
212 */
213 if (destmask != A0_DEST_CHANNEL_ALL) {
214 /* if not writing to XYZW... */
215 uint32_t tmp = i915_get_utemp(p);
216 i915_emit_texld(p, tmp, A0_DEST_CHANNEL_ALL, sampler, coord, opcode,
217 coord_mask);
218 i915_emit_arith(p, A0_MOV, dest, destmask, 0, tmp, 0, 0);
219 /* XXX release utemp here? */
220 } else {
221 assert(GET_UREG_TYPE(dest) != REG_TYPE_CONST);
222 assert(dest == UREG(GET_UREG_TYPE(dest), GET_UREG_NR(dest)));
223
224 /* Output register being oC or oD defines a phase boundary */
225 if (GET_UREG_TYPE(dest) == REG_TYPE_OC ||
226 GET_UREG_TYPE(dest) == REG_TYPE_OD)
227 p->nr_tex_indirect++;
228
229 /* Reading from an r# register whose contents depend on output of the
230 * current phase defines a phase boundary.
231 */
232 if (GET_UREG_TYPE(coord) == REG_TYPE_R &&
233 p->register_phases[GET_UREG_NR(coord)] == p->nr_tex_indirect)
234 p->nr_tex_indirect++;
235
236 if (p->csr < p->program + I915_PROGRAM_SIZE) {
237 *(p->csr++) = (opcode | T0_DEST(dest) | T0_SAMPLER(sampler));
238
239 *(p->csr++) = T1_ADDRESS_REG(coord);
240 *(p->csr++) = T2_MBZ;
241 }
242
243 if (GET_UREG_TYPE(dest) == REG_TYPE_R)
244 p->register_phases[GET_UREG_NR(dest)] = p->nr_tex_indirect;
245
246 p->nr_tex_insn++;
247 }
248
249 if (temp >= 0)
250 i915_release_temp(p, temp);
251
252 return dest;
253 }
254
255 uint32_t
i915_emit_const1f(struct i915_fp_compile * p,float c0)256 i915_emit_const1f(struct i915_fp_compile *p, float c0)
257 {
258 struct i915_fragment_shader *ifs = p->shader;
259 unsigned reg, idx;
260
261 if (c0 == 0.0)
262 return swizzle(UREG(REG_TYPE_R, 0), ZERO, ZERO, ZERO, ZERO);
263 if (c0 == 1.0)
264 return swizzle(UREG(REG_TYPE_R, 0), ONE, ONE, ONE, ONE);
265
266 for (reg = 0; reg < I915_MAX_CONSTANT; reg++) {
267 if (ifs->constant_flags[reg] == I915_CONSTFLAG_USER)
268 continue;
269 for (idx = 0; idx < 4; idx++) {
270 if (!(ifs->constant_flags[reg] & (1 << idx)) ||
271 ifs->constants[reg][idx] == c0) {
272 ifs->constants[reg][idx] = c0;
273 ifs->constant_flags[reg] |= 1 << idx;
274 if (reg + 1 > ifs->num_constants)
275 ifs->num_constants = reg + 1;
276 return swizzle(UREG(REG_TYPE_CONST, reg), idx, ZERO, ZERO, ONE);
277 }
278 }
279 }
280
281 i915_program_error(p, "i915_emit_const1f: out of constants");
282 return 0;
283 }
284
285 uint32_t
i915_emit_const2f(struct i915_fp_compile * p,float c0,float c1)286 i915_emit_const2f(struct i915_fp_compile *p, float c0, float c1)
287 {
288 struct i915_fragment_shader *ifs = p->shader;
289 unsigned reg, idx;
290
291 if (c0 == 0.0)
292 return swizzle(i915_emit_const1f(p, c1), ZERO, X, Z, W);
293 if (c0 == 1.0)
294 return swizzle(i915_emit_const1f(p, c1), ONE, X, Z, W);
295
296 if (c1 == 0.0)
297 return swizzle(i915_emit_const1f(p, c0), X, ZERO, Z, W);
298 if (c1 == 1.0)
299 return swizzle(i915_emit_const1f(p, c0), X, ONE, Z, W);
300
301 // XXX emit swizzle here for 0, 1, -1 and any combination thereof
302 // we can use swizzle + neg for that
303 for (reg = 0; reg < I915_MAX_CONSTANT; reg++) {
304 if (ifs->constant_flags[reg] == 0xf ||
305 ifs->constant_flags[reg] == I915_CONSTFLAG_USER)
306 continue;
307 for (idx = 0; idx < 3; idx++) {
308 if (!(ifs->constant_flags[reg] & (3 << idx))) {
309 ifs->constants[reg][idx + 0] = c0;
310 ifs->constants[reg][idx + 1] = c1;
311 ifs->constant_flags[reg] |= 3 << idx;
312 if (reg + 1 > ifs->num_constants)
313 ifs->num_constants = reg + 1;
314 return swizzle(UREG(REG_TYPE_CONST, reg), idx, idx + 1, ZERO, ONE);
315 }
316 }
317 }
318
319 i915_program_error(p, "i915_emit_const2f: out of constants");
320 return 0;
321 }
322
323 uint32_t
i915_emit_const4f(struct i915_fp_compile * p,float c0,float c1,float c2,float c3)324 i915_emit_const4f(struct i915_fp_compile *p, float c0, float c1, float c2,
325 float c3)
326 {
327 struct i915_fragment_shader *ifs = p->shader;
328 unsigned reg;
329
330 // XXX emit swizzle here for 0, 1, -1 and any combination thereof
331 // we can use swizzle + neg for that
332 for (reg = 0; reg < I915_MAX_CONSTANT; reg++) {
333 if (ifs->constant_flags[reg] == 0xf && ifs->constants[reg][0] == c0 &&
334 ifs->constants[reg][1] == c1 && ifs->constants[reg][2] == c2 &&
335 ifs->constants[reg][3] == c3) {
336 return UREG(REG_TYPE_CONST, reg);
337 } else if (ifs->constant_flags[reg] == 0) {
338
339 ifs->constants[reg][0] = c0;
340 ifs->constants[reg][1] = c1;
341 ifs->constants[reg][2] = c2;
342 ifs->constants[reg][3] = c3;
343 ifs->constant_flags[reg] = 0xf;
344 if (reg + 1 > ifs->num_constants)
345 ifs->num_constants = reg + 1;
346 return UREG(REG_TYPE_CONST, reg);
347 }
348 }
349
350 i915_program_error(p, "i915_emit_const4f: out of constants");
351 return 0;
352 }
353
354 uint32_t
i915_emit_const4fv(struct i915_fp_compile * p,const float * c)355 i915_emit_const4fv(struct i915_fp_compile *p, const float *c)
356 {
357 return i915_emit_const4f(p, c[0], c[1], c[2], c[3]);
358 }
359