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 <strings.h>
29
30#include "main/glheader.h"
31#include "main/macros.h"
32#include "main/enums.h"
33
34#include "tnl/t_context.h"
35#include "intel_batchbuffer.h"
36
37#include "i915_reg.h"
38#include "i915_context.h"
39#include "i915_program.h"
40
41
42#define A0_DEST( reg ) (((reg)&UREG_TYPE_NR_MASK)>>UREG_A0_DEST_SHIFT_LEFT)
43#define D0_DEST( reg ) (((reg)&UREG_TYPE_NR_MASK)>>UREG_A0_DEST_SHIFT_LEFT)
44#define T0_DEST( reg ) (((reg)&UREG_TYPE_NR_MASK)>>UREG_A0_DEST_SHIFT_LEFT)
45#define A0_SRC0( reg ) (((reg)&UREG_MASK)>>UREG_A0_SRC0_SHIFT_LEFT)
46#define A1_SRC0( reg ) (((reg)&UREG_MASK)<<UREG_A1_SRC0_SHIFT_RIGHT)
47#define A1_SRC1( reg ) (((reg)&UREG_MASK)>>UREG_A1_SRC1_SHIFT_LEFT)
48#define A2_SRC1( reg ) (((reg)&UREG_MASK)<<UREG_A2_SRC1_SHIFT_RIGHT)
49#define A2_SRC2( reg ) (((reg)&UREG_MASK)>>UREG_A2_SRC2_SHIFT_LEFT)
50
51/* These are special, and don't have swizzle/negate bits.
52 */
53#define T0_SAMPLER( reg )     (GET_UREG_NR(reg)<<T0_SAMPLER_NR_SHIFT)
54#define T1_ADDRESS_REG( reg ) ((GET_UREG_NR(reg)<<T1_ADDRESS_REG_NR_SHIFT) | \
55			       (GET_UREG_TYPE(reg)<<T1_ADDRESS_REG_TYPE_SHIFT))
56
57
58/* Macros for translating UREG's into the various register fields used
59 * by the I915 programmable unit.
60 */
61#define UREG_A0_DEST_SHIFT_LEFT  (UREG_TYPE_SHIFT - A0_DEST_TYPE_SHIFT)
62#define UREG_A0_SRC0_SHIFT_LEFT  (UREG_TYPE_SHIFT - A0_SRC0_TYPE_SHIFT)
63#define UREG_A1_SRC0_SHIFT_RIGHT (A1_SRC0_CHANNEL_W_SHIFT - UREG_CHANNEL_W_SHIFT)
64#define UREG_A1_SRC1_SHIFT_LEFT  (UREG_TYPE_SHIFT - A1_SRC1_TYPE_SHIFT)
65#define UREG_A2_SRC1_SHIFT_RIGHT (A2_SRC1_CHANNEL_W_SHIFT - UREG_CHANNEL_W_SHIFT)
66#define UREG_A2_SRC2_SHIFT_LEFT  (UREG_TYPE_SHIFT - A2_SRC2_TYPE_SHIFT)
67
68#define UREG_MASK         0xffffff00
69#define UREG_TYPE_NR_MASK ((REG_TYPE_MASK << UREG_TYPE_SHIFT) | \
70  			   (REG_NR_MASK << UREG_NR_SHIFT))
71
72
73#define I915_CONSTFLAG_PARAM 0x1f
74
75GLuint
76i915_get_temp(struct i915_fragment_program *p)
77{
78   int bit = ffs(~p->temp_flag);
79   if (!bit) {
80      fprintf(stderr, "%s: out of temporaries\n", __FILE__);
81      exit(1);
82   }
83
84   p->temp_flag |= 1 << (bit - 1);
85   return UREG(REG_TYPE_R, (bit - 1));
86}
87
88
89GLuint
90i915_get_utemp(struct i915_fragment_program * p)
91{
92   int bit = ffs(~p->utemp_flag);
93   if (!bit) {
94      fprintf(stderr, "%s: out of temporaries\n", __FILE__);
95      exit(1);
96   }
97
98   p->utemp_flag |= 1 << (bit - 1);
99   return UREG(REG_TYPE_U, (bit - 1));
100}
101
102void
103i915_release_utemps(struct i915_fragment_program *p)
104{
105   p->utemp_flag = ~0x7;
106}
107
108
109GLuint
110i915_emit_decl(struct i915_fragment_program *p,
111               GLuint type, GLuint nr, GLuint d0_flags)
112{
113   GLuint reg = UREG(type, nr);
114
115   if (type == REG_TYPE_T) {
116      if (p->decl_t & (1 << nr))
117         return reg;
118
119      p->decl_t |= (1 << nr);
120   }
121   else if (type == REG_TYPE_S) {
122      if (p->decl_s & (1 << nr))
123         return reg;
124
125      p->decl_s |= (1 << nr);
126   }
127   else
128      return reg;
129
130   *(p->decl++) = (D0_DCL | D0_DEST(reg) | d0_flags);
131   *(p->decl++) = D1_MBZ;
132   *(p->decl++) = D2_MBZ;
133   assert(p->decl <= p->declarations + ARRAY_SIZE(p->declarations));
134
135   p->nr_decl_insn++;
136   return reg;
137}
138
139GLuint
140i915_emit_arith(struct i915_fragment_program * p,
141                GLuint op,
142                GLuint dest,
143                GLuint mask,
144                GLuint saturate, GLuint src0, GLuint src1, GLuint src2)
145{
146   GLuint c[3];
147   GLuint nr_const = 0;
148
149   assert(GET_UREG_TYPE(dest) != REG_TYPE_CONST);
150   dest = UREG(GET_UREG_TYPE(dest), GET_UREG_NR(dest));
151   assert(dest);
152
153   if (GET_UREG_TYPE(src0) == REG_TYPE_CONST)
154      c[nr_const++] = 0;
155   if (GET_UREG_TYPE(src1) == REG_TYPE_CONST)
156      c[nr_const++] = 1;
157   if (GET_UREG_TYPE(src2) == REG_TYPE_CONST)
158      c[nr_const++] = 2;
159
160   /* Recursively call this function to MOV additional const values
161    * into temporary registers.  Use utemp registers for this -
162    * currently shouldn't be possible to run out, but keep an eye on
163    * this.
164    */
165   if (nr_const > 1) {
166      GLuint s[3], first, i, old_utemp_flag;
167
168      s[0] = src0;
169      s[1] = src1;
170      s[2] = src2;
171      old_utemp_flag = p->utemp_flag;
172
173      first = GET_UREG_NR(s[c[0]]);
174      for (i = 1; i < nr_const; i++) {
175         if (GET_UREG_NR(s[c[i]]) != first) {
176            GLuint tmp = i915_get_utemp(p);
177
178            i915_emit_arith(p, A0_MOV, tmp, A0_DEST_CHANNEL_ALL, 0,
179                            s[c[i]], 0, 0);
180            s[c[i]] = tmp;
181         }
182      }
183
184      src0 = s[0];
185      src1 = s[1];
186      src2 = s[2];
187      p->utemp_flag = old_utemp_flag;   /* restore */
188   }
189
190   if (p->csr >= p->program + ARRAY_SIZE(p->program)) {
191      i915_program_error(p, "Program contains too many instructions");
192      return UREG_BAD;
193   }
194
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));
198
199   if (GET_UREG_TYPE(dest) == REG_TYPE_R)
200      p->register_phases[GET_UREG_NR(dest)] = p->nr_tex_indirect;
201
202   p->nr_alu_insn++;
203   return dest;
204}
205
206static GLuint get_free_rreg (struct i915_fragment_program *p,
207                             GLuint live_regs)
208{
209    int bit = ffs(~live_regs);
210    if (!bit) {
211        i915_program_error(p, "Can't find free R reg");
212        return UREG_BAD;
213    }
214    return UREG(REG_TYPE_R, bit - 1);
215}
216
217GLuint i915_emit_texld( struct i915_fragment_program *p,
218			GLuint live_regs,
219			GLuint dest,
220			GLuint destmask,
221			GLuint sampler,
222			GLuint coord,
223			GLuint op )
224{
225    if (coord != UREG(GET_UREG_TYPE(coord), GET_UREG_NR(coord))) {
226        /* With the help of the "needed registers" table created earlier, pick
227         * a register we can MOV the swizzled TC to (since TEX doesn't support
228         * swizzled sources) */
229        GLuint swizCoord = get_free_rreg(p, live_regs);
230        if (swizCoord == UREG_BAD)
231            return 0;
232
233        i915_emit_arith( p, A0_MOV, swizCoord, A0_DEST_CHANNEL_ALL, 0, coord, 0, 0 );
234        coord = swizCoord;
235    }
236
237   /* Don't worry about saturate as we only support texture formats
238    * that are always in the 0..1 range.
239    */
240   if (destmask != A0_DEST_CHANNEL_ALL) {
241      GLuint tmp = i915_get_utemp(p);
242      i915_emit_texld( p, 0, tmp, A0_DEST_CHANNEL_ALL, sampler, coord, op );
243      i915_emit_arith( p, A0_MOV, dest, destmask, 0, tmp, 0, 0 );
244      return dest;
245   }
246   else {
247      assert(GET_UREG_TYPE(dest) != REG_TYPE_CONST);
248      assert(dest == UREG(GET_UREG_TYPE(dest), GET_UREG_NR(dest)));
249      /* Can't use unsaved temps for coords, as the phase boundary would result
250       * in the contents becoming undefined.
251       */
252      assert(GET_UREG_TYPE(coord) != REG_TYPE_U);
253
254      if ((GET_UREG_TYPE(coord) != REG_TYPE_R) &&
255          (GET_UREG_TYPE(coord) != REG_TYPE_OC) &&
256          (GET_UREG_TYPE(coord) != REG_TYPE_OD) &&
257          (GET_UREG_TYPE(coord) != REG_TYPE_T)) {
258          GLuint  tmpCoord = get_free_rreg(p, live_regs);
259
260          if (tmpCoord == UREG_BAD)
261              return 0;
262
263          i915_emit_arith(p, A0_MOV, tmpCoord, A0_DEST_CHANNEL_ALL, 0, coord, 0, 0);
264          coord = tmpCoord;
265      }
266
267      /* Output register being oC or oD defines a phase boundary */
268      if (GET_UREG_TYPE(dest) == REG_TYPE_OC ||
269	  GET_UREG_TYPE(dest) == REG_TYPE_OD)
270	 p->nr_tex_indirect++;
271
272      /* Reading from an r# register whose contents depend on output of the
273       * current phase defines a phase boundary.
274       */
275      if (GET_UREG_TYPE(coord) == REG_TYPE_R &&
276	  p->register_phases[GET_UREG_NR(coord)] == p->nr_tex_indirect)
277	 p->nr_tex_indirect++;
278
279      if (p->csr >= p->program + ARRAY_SIZE(p->program)) {
280	 i915_program_error(p, "Program contains too many instructions");
281	 return UREG_BAD;
282      }
283
284      *(p->csr++) = (op |
285		     T0_DEST( dest ) |
286		     T0_SAMPLER( sampler ));
287
288      *(p->csr++) = T1_ADDRESS_REG( coord );
289      *(p->csr++) = T2_MBZ;
290
291      if (GET_UREG_TYPE(dest) == REG_TYPE_R)
292	 p->register_phases[GET_UREG_NR(dest)] = p->nr_tex_indirect;
293
294      p->nr_tex_insn++;
295      return dest;
296   }
297}
298
299
300GLuint
301i915_emit_const1f(struct i915_fragment_program * p, GLfloat c0)
302{
303   GLint reg, idx;
304
305   if (c0 == 0.0)
306      return swizzle(UREG(REG_TYPE_R, 0), ZERO, ZERO, ZERO, ZERO);
307   if (c0 == 1.0)
308      return swizzle(UREG(REG_TYPE_R, 0), ONE, ONE, ONE, ONE);
309
310   for (reg = 0; reg < I915_MAX_CONSTANT; reg++) {
311      if (p->constant_flags[reg] == I915_CONSTFLAG_PARAM)
312         continue;
313      for (idx = 0; idx < 4; idx++) {
314         if (!(p->constant_flags[reg] & (1 << idx)) ||
315             p->constant[reg][idx] == c0) {
316            p->constant[reg][idx] = c0;
317            p->constant_flags[reg] |= 1 << idx;
318            if (reg + 1 > p->nr_constants)
319               p->nr_constants = reg + 1;
320            return swizzle(UREG(REG_TYPE_CONST, reg), idx, ZERO, ZERO, ONE);
321         }
322      }
323   }
324
325   fprintf(stderr, "%s: out of constants\n", __FUNCTION__);
326   p->error = 1;
327   return 0;
328}
329
330GLuint
331i915_emit_const2f(struct i915_fragment_program * p, GLfloat c0, GLfloat c1)
332{
333   GLint reg, idx;
334
335   if (c0 == 0.0)
336      return swizzle(i915_emit_const1f(p, c1), ZERO, X, Z, W);
337   if (c0 == 1.0)
338      return swizzle(i915_emit_const1f(p, c1), ONE, X, Z, W);
339
340   if (c1 == 0.0)
341      return swizzle(i915_emit_const1f(p, c0), X, ZERO, Z, W);
342   if (c1 == 1.0)
343      return swizzle(i915_emit_const1f(p, c0), X, ONE, Z, W);
344
345   for (reg = 0; reg < I915_MAX_CONSTANT; reg++) {
346      if (p->constant_flags[reg] == 0xf ||
347          p->constant_flags[reg] == I915_CONSTFLAG_PARAM)
348         continue;
349      for (idx = 0; idx < 3; idx++) {
350         if (!(p->constant_flags[reg] & (3 << idx))) {
351            p->constant[reg][idx] = c0;
352            p->constant[reg][idx + 1] = c1;
353            p->constant_flags[reg] |= 3 << idx;
354            if (reg + 1 > p->nr_constants)
355               p->nr_constants = reg + 1;
356            return swizzle(UREG(REG_TYPE_CONST, reg), idx, idx + 1, ZERO,
357                           ONE);
358         }
359      }
360   }
361
362   fprintf(stderr, "%s: out of constants\n", __FUNCTION__);
363   p->error = 1;
364   return 0;
365}
366
367
368
369GLuint
370i915_emit_const4f(struct i915_fragment_program * p,
371                  GLfloat c0, GLfloat c1, GLfloat c2, GLfloat c3)
372{
373   GLint reg;
374
375   for (reg = 0; reg < I915_MAX_CONSTANT; reg++) {
376      if (p->constant_flags[reg] == 0xf &&
377          p->constant[reg][0] == c0 &&
378          p->constant[reg][1] == c1 &&
379          p->constant[reg][2] == c2 && p->constant[reg][3] == c3) {
380         return UREG(REG_TYPE_CONST, reg);
381      }
382      else if (p->constant_flags[reg] == 0) {
383         p->constant[reg][0] = c0;
384         p->constant[reg][1] = c1;
385         p->constant[reg][2] = c2;
386         p->constant[reg][3] = c3;
387         p->constant_flags[reg] = 0xf;
388         if (reg + 1 > p->nr_constants)
389            p->nr_constants = reg + 1;
390         return UREG(REG_TYPE_CONST, reg);
391      }
392   }
393
394   fprintf(stderr, "%s: out of constants\n", __FUNCTION__);
395   p->error = 1;
396   return 0;
397}
398
399
400GLuint
401i915_emit_const4fv(struct i915_fragment_program * p, const GLfloat * c)
402{
403   return i915_emit_const4f(p, c[0], c[1], c[2], c[3]);
404}
405
406
407GLuint
408i915_emit_param4fv(struct i915_fragment_program * p, const GLfloat * values)
409{
410   GLint reg, i;
411
412   for (i = 0; i < p->nr_params; i++) {
413      if (p->param[i].values == values)
414         return UREG(REG_TYPE_CONST, p->param[i].reg);
415   }
416
417
418   for (reg = 0; reg < I915_MAX_CONSTANT; reg++) {
419      if (p->constant_flags[reg] == 0) {
420         p->constant_flags[reg] = I915_CONSTFLAG_PARAM;
421         i = p->nr_params++;
422
423         p->param[i].values = values;
424         p->param[i].reg = reg;
425         p->params_uptodate = 0;
426
427         if (reg + 1 > p->nr_constants)
428            p->nr_constants = reg + 1;
429         return UREG(REG_TYPE_CONST, reg);
430      }
431   }
432
433   fprintf(stderr, "%s: out of constants\n", __FUNCTION__);
434   p->error = 1;
435   return 0;
436}
437
438/* Warning the user about program errors seems to be quite valuable, from
439 * our bug reports.  It unfortunately means piglit reporting errors
440 * when we fall back to software due to an unsupportable program, though.
441 */
442void
443i915_program_error(struct i915_fragment_program *p, const char *fmt, ...)
444{
445   if (unlikely((INTEL_DEBUG & (DEBUG_WM | DEBUG_PERF)) != 0)) {
446      va_list args;
447
448      fprintf(stderr, "i915_program_error: ");
449      va_start(args, fmt);
450      vfprintf(stderr, fmt, args);
451      va_end(args);
452
453      fprintf(stderr, "\n");
454   }
455   p->error = 1;
456}
457
458
459void
460i915_init_program(struct i915_context *i915, struct i915_fragment_program *p)
461{
462   struct gl_context *ctx = &i915->intel.ctx;
463
464   p->translated = 0;
465   p->params_uptodate = 0;
466   p->on_hardware = 0;
467   p->error = 0;
468
469   memset(&p->register_phases, 0, sizeof(p->register_phases));
470   p->nr_tex_indirect = 1;
471   p->nr_tex_insn = 0;
472   p->nr_alu_insn = 0;
473   p->nr_decl_insn = 0;
474
475   p->ctx = ctx;
476   memset(p->constant_flags, 0, sizeof(p->constant_flags));
477
478   p->nr_constants = 0;
479   p->csr = p->program;
480   p->decl = p->declarations;
481   p->decl_s = 0;
482   p->decl_t = 0;
483   p->temp_flag = 0xffff000;
484   p->utemp_flag = ~0x7;
485   p->wpos_tex = -1;
486   p->depth_written = 0;
487   p->nr_params = 0;
488
489   *(p->decl++) = _3DSTATE_PIXEL_SHADER_PROGRAM;
490}
491
492
493void
494i915_fini_program(struct i915_fragment_program *p)
495{
496   GLuint program_size = p->csr - p->program;
497   GLuint decl_size = p->decl - p->declarations;
498
499   if (p->nr_tex_indirect > I915_MAX_TEX_INDIRECT) {
500      i915_program_error(p, "Exceeded max nr indirect texture lookups "
501			 "(%d out of %d)",
502			 p->nr_tex_indirect, I915_MAX_TEX_INDIRECT);
503   }
504
505   if (p->nr_tex_insn > I915_MAX_TEX_INSN) {
506      i915_program_error(p, "Exceeded max TEX instructions (%d out of %d)",
507			 p->nr_tex_insn, I915_MAX_TEX_INSN);
508   }
509
510   if (p->nr_alu_insn > I915_MAX_ALU_INSN)
511      i915_program_error(p, "Exceeded max ALU instructions (%d out of %d)",
512			 p->nr_alu_insn, I915_MAX_ALU_INSN);
513
514   if (p->nr_decl_insn > I915_MAX_DECL_INSN) {
515      i915_program_error(p, "Exceeded max DECL instructions (%d out of %d)",
516			 p->nr_decl_insn, I915_MAX_DECL_INSN);
517   }
518
519   if (p->error) {
520      p->FragProg.Base.NumNativeInstructions = 0;
521      p->FragProg.Base.NumNativeAluInstructions = 0;
522      p->FragProg.Base.NumNativeTexInstructions = 0;
523      p->FragProg.Base.NumNativeTexIndirections = 0;
524   }
525   else {
526      p->FragProg.Base.NumNativeInstructions = (p->nr_alu_insn +
527                                                p->nr_tex_insn +
528                                                p->nr_decl_insn);
529      p->FragProg.Base.NumNativeAluInstructions = p->nr_alu_insn;
530      p->FragProg.Base.NumNativeTexInstructions = p->nr_tex_insn;
531      p->FragProg.Base.NumNativeTexIndirections = p->nr_tex_indirect;
532   }
533
534   p->declarations[0] |= program_size + decl_size - 2;
535}
536
537void
538i915_upload_program(struct i915_context *i915,
539                    struct i915_fragment_program *p)
540{
541   GLuint program_size = p->csr - p->program;
542   GLuint decl_size = p->decl - p->declarations;
543
544   if (p->error)
545      return;
546
547   /* Could just go straight to the batchbuffer from here:
548    */
549   if (i915->state.ProgramSize != (program_size + decl_size) ||
550       memcmp(i915->state.Program + decl_size, p->program,
551              program_size * sizeof(int)) != 0) {
552      I915_STATECHANGE(i915, I915_UPLOAD_PROGRAM);
553      memcpy(i915->state.Program, p->declarations, decl_size * sizeof(int));
554      memcpy(i915->state.Program + decl_size, p->program,
555             program_size * sizeof(int));
556      i915->state.ProgramSize = decl_size + program_size;
557   }
558
559   /* Always seemed to get a failure if I used memcmp() to
560    * shortcircuit this state upload.  Needs further investigation?
561    */
562   if (p->nr_constants) {
563      GLuint nr = p->nr_constants;
564
565      I915_ACTIVESTATE(i915, I915_UPLOAD_CONSTANTS, 1);
566      I915_STATECHANGE(i915, I915_UPLOAD_CONSTANTS);
567
568      i915->state.Constant[0] = _3DSTATE_PIXEL_SHADER_CONSTANTS | ((nr) * 4);
569      i915->state.Constant[1] = (1 << (nr - 1)) | ((1 << (nr - 1)) - 1);
570
571      memcpy(&i915->state.Constant[2], p->constant, 4 * sizeof(int) * (nr));
572      i915->state.ConstantSize = 2 + (nr) * 4;
573
574      if (0) {
575         GLuint i;
576         for (i = 0; i < nr; i++) {
577            fprintf(stderr, "const[%d]: %f %f %f %f\n", i,
578                    p->constant[i][0],
579                    p->constant[i][1], p->constant[i][2], p->constant[i][3]);
580         }
581      }
582   }
583   else {
584      I915_ACTIVESTATE(i915, I915_UPLOAD_CONSTANTS, 0);
585   }
586
587   p->on_hardware = 1;
588}
589