OSDN Git Service

Merge branch 'mesa_7_7_branch'
[android-x86/external-mesa.git] / src / mesa / drivers / dri / i915 / i915_fragprog.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 "main/glheader.h"
29 #include "main/macros.h"
30 #include "main/enums.h"
31
32 #include "shader/prog_instruction.h"
33 #include "shader/prog_parameter.h"
34 #include "shader/program.h"
35 #include "shader/programopt.h"
36 #include "shader/prog_print.h"
37
38 #include "tnl/tnl.h"
39 #include "tnl/t_context.h"
40
41 #include "intel_batchbuffer.h"
42
43 #include "i915_reg.h"
44 #include "i915_context.h"
45 #include "i915_program.h"
46
47 static const GLfloat sin_quad_constants[2][4] = {
48    {
49       2.0,
50       -1.0,
51       .5,
52       .75
53    },
54    {
55       4.0,
56       -4.0,
57       1.0 / (2.0 * M_PI),
58       .2225
59    }
60 };
61
62 static const GLfloat sin_constants[4] = { 1.0,
63    -1.0 / (3 * 2 * 1),
64    1.0 / (5 * 4 * 3 * 2 * 1),
65    -1.0 / (7 * 6 * 5 * 4 * 3 * 2 * 1)
66 };
67
68 /* 1, -1/2!, 1/4!, -1/6! */
69 static const GLfloat cos_constants[4] = { 1.0,
70    -1.0 / (2 * 1),
71    1.0 / (4 * 3 * 2 * 1),
72    -1.0 / (6 * 5 * 4 * 3 * 2 * 1)
73 };
74
75 /**
76  * Retrieve a ureg for the given source register.  Will emit
77  * constants, apply swizzling and negation as needed.
78  */
79 static GLuint
80 src_vector(struct i915_fragment_program *p,
81            const struct prog_src_register *source,
82            const struct gl_fragment_program *program)
83 {
84    GLuint src;
85
86    switch (source->File) {
87
88       /* Registers:
89        */
90    case PROGRAM_TEMPORARY:
91       if (source->Index >= I915_MAX_TEMPORARY) {
92          i915_program_error(p, "Exceeded max temporary reg: %d/%d",
93                             source->Index, I915_MAX_TEMPORARY);
94          return 0;
95       }
96       src = UREG(REG_TYPE_R, source->Index);
97       break;
98    case PROGRAM_INPUT:
99       switch (source->Index) {
100       case FRAG_ATTRIB_WPOS:
101          src = i915_emit_decl(p, REG_TYPE_T, p->wpos_tex, D0_CHANNEL_ALL);
102          break;
103       case FRAG_ATTRIB_COL0:
104          src = i915_emit_decl(p, REG_TYPE_T, T_DIFFUSE, D0_CHANNEL_ALL);
105          break;
106       case FRAG_ATTRIB_COL1:
107          src = i915_emit_decl(p, REG_TYPE_T, T_SPECULAR, D0_CHANNEL_XYZ);
108          src = swizzle(src, X, Y, Z, ONE);
109          break;
110       case FRAG_ATTRIB_FOGC:
111          src = i915_emit_decl(p, REG_TYPE_T, T_FOG_W, D0_CHANNEL_W);
112          src = swizzle(src, W, ZERO, ZERO, ONE);
113          break;
114       case FRAG_ATTRIB_TEX0:
115       case FRAG_ATTRIB_TEX1:
116       case FRAG_ATTRIB_TEX2:
117       case FRAG_ATTRIB_TEX3:
118       case FRAG_ATTRIB_TEX4:
119       case FRAG_ATTRIB_TEX5:
120       case FRAG_ATTRIB_TEX6:
121       case FRAG_ATTRIB_TEX7:
122          src = i915_emit_decl(p, REG_TYPE_T,
123                               T_TEX0 + (source->Index - FRAG_ATTRIB_TEX0),
124                               D0_CHANNEL_ALL);
125          break;
126
127       case FRAG_ATTRIB_VAR0:
128       case FRAG_ATTRIB_VAR0 + 1:
129       case FRAG_ATTRIB_VAR0 + 2:
130       case FRAG_ATTRIB_VAR0 + 3:
131       case FRAG_ATTRIB_VAR0 + 4:
132       case FRAG_ATTRIB_VAR0 + 5:
133       case FRAG_ATTRIB_VAR0 + 6:
134       case FRAG_ATTRIB_VAR0 + 7:
135          src = i915_emit_decl(p, REG_TYPE_T,
136                               T_TEX0 + (source->Index - FRAG_ATTRIB_VAR0),
137                               D0_CHANNEL_ALL);
138          break;
139
140       default:
141          i915_program_error(p, "Bad source->Index: %d", source->Index);
142          return 0;
143       }
144       break;
145
146       /* Various paramters and env values.  All emitted to
147        * hardware as program constants.
148        */
149    case PROGRAM_LOCAL_PARAM:
150       src = i915_emit_param4fv(p, program->Base.LocalParams[source->Index]);
151       break;
152
153    case PROGRAM_ENV_PARAM:
154       src =
155          i915_emit_param4fv(p,
156                             p->ctx->FragmentProgram.Parameters[source->
157                                                                Index]);
158       break;
159
160    case PROGRAM_CONSTANT:
161    case PROGRAM_STATE_VAR:
162    case PROGRAM_NAMED_PARAM:
163    case PROGRAM_UNIFORM:
164       src =
165          i915_emit_param4fv(p,
166                             program->Base.Parameters->ParameterValues[source->
167                                                                       Index]);
168       break;
169
170    default:
171       i915_program_error(p, "Bad source->File: %d", source->File);
172       return 0;
173    }
174
175    src = swizzle(src,
176                  GET_SWZ(source->Swizzle, 0),
177                  GET_SWZ(source->Swizzle, 1),
178                  GET_SWZ(source->Swizzle, 2), GET_SWZ(source->Swizzle, 3));
179
180    if (source->Negate)
181       src = negate(src,
182                    GET_BIT(source->Negate, 0),
183                    GET_BIT(source->Negate, 1),
184                    GET_BIT(source->Negate, 2),
185                    GET_BIT(source->Negate, 3));
186
187    return src;
188 }
189
190
191 static GLuint
192 get_result_vector(struct i915_fragment_program *p,
193                   const struct prog_instruction *inst)
194 {
195    switch (inst->DstReg.File) {
196    case PROGRAM_OUTPUT:
197       switch (inst->DstReg.Index) {
198       case FRAG_RESULT_COLOR:
199          return UREG(REG_TYPE_OC, 0);
200       case FRAG_RESULT_DEPTH:
201          p->depth_written = 1;
202          return UREG(REG_TYPE_OD, 0);
203       default:
204          i915_program_error(p, "Bad inst->DstReg.Index: %d",
205                             inst->DstReg.Index);
206          return 0;
207       }
208    case PROGRAM_TEMPORARY:
209       return UREG(REG_TYPE_R, inst->DstReg.Index);
210    default:
211       i915_program_error(p, "Bad inst->DstReg.File: %d", inst->DstReg.File);
212       return 0;
213    }
214 }
215
216 static GLuint
217 get_result_flags(const struct prog_instruction *inst)
218 {
219    GLuint flags = 0;
220
221    if (inst->SaturateMode == SATURATE_ZERO_ONE)
222       flags |= A0_DEST_SATURATE;
223    if (inst->DstReg.WriteMask & WRITEMASK_X)
224       flags |= A0_DEST_CHANNEL_X;
225    if (inst->DstReg.WriteMask & WRITEMASK_Y)
226       flags |= A0_DEST_CHANNEL_Y;
227    if (inst->DstReg.WriteMask & WRITEMASK_Z)
228       flags |= A0_DEST_CHANNEL_Z;
229    if (inst->DstReg.WriteMask & WRITEMASK_W)
230       flags |= A0_DEST_CHANNEL_W;
231
232    return flags;
233 }
234
235 static GLuint
236 translate_tex_src_target(struct i915_fragment_program *p, GLubyte bit)
237 {
238    switch (bit) {
239    case TEXTURE_1D_INDEX:
240       return D0_SAMPLE_TYPE_2D;
241    case TEXTURE_2D_INDEX:
242       return D0_SAMPLE_TYPE_2D;
243    case TEXTURE_RECT_INDEX:
244       return D0_SAMPLE_TYPE_2D;
245    case TEXTURE_3D_INDEX:
246       return D0_SAMPLE_TYPE_VOLUME;
247    case TEXTURE_CUBE_INDEX:
248       return D0_SAMPLE_TYPE_CUBE;
249    default:
250       i915_program_error(p, "TexSrcBit: %d", bit);
251       return 0;
252    }
253 }
254
255 #define EMIT_TEX( OP )                                          \
256 do {                                                            \
257    GLuint dim = translate_tex_src_target( p, inst->TexSrcTarget );      \
258    GLuint sampler = i915_emit_decl(p, REG_TYPE_S,               \
259                                   inst->TexSrcUnit, dim);       \
260    GLuint coord = src_vector( p, &inst->SrcReg[0], program);    \
261    /* Texel lookup */                                           \
262                                                                 \
263    i915_emit_texld( p, get_live_regs(p, inst),                                          \
264                get_result_vector( p, inst ),                    \
265                get_result_flags( inst ),                        \
266                sampler,                                         \
267                coord,                                           \
268                OP);                                             \
269 } while (0)
270
271 #define EMIT_ARITH( OP, N )                                             \
272 do {                                                                    \
273    i915_emit_arith( p,                                                  \
274                OP,                                                      \
275                get_result_vector( p, inst ),                            \
276                get_result_flags( inst ), 0,                     \
277                (N<1)?0:src_vector( p, &inst->SrcReg[0], program),       \
278                (N<2)?0:src_vector( p, &inst->SrcReg[1], program),       \
279                (N<3)?0:src_vector( p, &inst->SrcReg[2], program));      \
280 } while (0)
281
282 #define EMIT_1ARG_ARITH( OP ) EMIT_ARITH( OP, 1 )
283 #define EMIT_2ARG_ARITH( OP ) EMIT_ARITH( OP, 2 )
284 #define EMIT_3ARG_ARITH( OP ) EMIT_ARITH( OP, 3 )
285
286 /* 
287  * TODO: consider moving this into core 
288  */
289 static void calc_live_regs( struct i915_fragment_program *p )
290 {
291     const struct gl_fragment_program *program = p->ctx->FragmentProgram._Current;
292     GLuint regsUsed = 0xffff0000;
293     GLint i;
294    
295     for (i = program->Base.NumInstructions - 1; i >= 0; i--) {
296         struct prog_instruction *inst = &program->Base.Instructions[i];
297         int opArgs = _mesa_num_inst_src_regs(inst->Opcode);
298         int a;
299
300         /* Register is written to: unmark as live for this and preceeding ops */ 
301         if (inst->DstReg.File == PROGRAM_TEMPORARY)
302             regsUsed &= ~(1 << inst->DstReg.Index);
303
304         for (a = 0; a < opArgs; a++) {
305             /* Register is read from: mark as live for this and preceeding ops */ 
306             if (inst->SrcReg[a].File == PROGRAM_TEMPORARY)
307                 regsUsed |= 1 << inst->SrcReg[a].Index;
308         }
309
310         p->usedRegs[i] = regsUsed;
311     }
312 }
313
314 static GLuint get_live_regs( struct i915_fragment_program *p, 
315                              const struct prog_instruction *inst )
316 {
317     const struct gl_fragment_program *program = p->ctx->FragmentProgram._Current;
318     GLuint nr = inst - program->Base.Instructions;
319
320     return p->usedRegs[nr];
321 }
322  
323
324 /* Possible concerns:
325  *
326  * SIN, COS -- could use another taylor step?
327  * LIT      -- results seem a little different to sw mesa
328  * LOG      -- different to mesa on negative numbers, but this is conformant.
329  * 
330  * Parse failures -- Mesa doesn't currently give a good indication
331  * internally whether a particular program string parsed or not.  This
332  * can lead to confusion -- hopefully we cope with it ok now.
333  *
334  */
335 static void
336 upload_program(struct i915_fragment_program *p)
337 {
338    const struct gl_fragment_program *program =
339       p->ctx->FragmentProgram._Current;
340    const struct prog_instruction *inst = program->Base.Instructions;
341
342    if (INTEL_DEBUG & DEBUG_WM)
343       _mesa_print_program(&program->Base);
344
345    /* Is this a parse-failed program?  Ensure a valid program is
346     * loaded, as the flagging of an error isn't sufficient to stop
347     * this being uploaded to hardware.
348     */
349    if (inst[0].Opcode == OPCODE_END) {
350       GLuint tmp = i915_get_utemp(p);
351       i915_emit_arith(p,
352                       A0_MOV,
353                       UREG(REG_TYPE_OC, 0),
354                       A0_DEST_CHANNEL_ALL, 0,
355                       swizzle(tmp, ONE, ZERO, ONE, ONE), 0, 0);
356       return;
357    }
358
359    if (program->Base.NumInstructions > I915_MAX_INSN) {
360        i915_program_error( p, "Exceeded max instructions" );
361        return;
362     }
363
364    /* Not always needed:
365     */
366    calc_live_regs(p);
367
368    while (1) {
369       GLuint src0, src1, src2, flags;
370       GLuint tmp = 0, dst, consts0 = 0, consts1 = 0;
371
372       switch (inst->Opcode) {
373       case OPCODE_ABS:
374          src0 = src_vector(p, &inst->SrcReg[0], program);
375          i915_emit_arith(p,
376                          A0_MAX,
377                          get_result_vector(p, inst),
378                          get_result_flags(inst), 0,
379                          src0, negate(src0, 1, 1, 1, 1), 0);
380          break;
381
382       case OPCODE_ADD:
383          EMIT_2ARG_ARITH(A0_ADD);
384          break;
385
386       case OPCODE_CMP:
387          src0 = src_vector(p, &inst->SrcReg[0], program);
388          src1 = src_vector(p, &inst->SrcReg[1], program);
389          src2 = src_vector(p, &inst->SrcReg[2], program);
390          i915_emit_arith(p, A0_CMP, get_result_vector(p, inst), get_result_flags(inst), 0, src0, src2, src1);   /* NOTE: order of src2, src1 */
391          break;
392
393       case OPCODE_COS:
394          src0 = src_vector(p, &inst->SrcReg[0], program);
395          tmp = i915_get_utemp(p);
396          consts0 = i915_emit_const4fv(p, sin_quad_constants[0]);
397          consts1 = i915_emit_const4fv(p, sin_quad_constants[1]);
398
399          /* Reduce range from repeating about [-pi,pi] to [-1,1] */
400          i915_emit_arith(p,
401                          A0_MAD,
402                          tmp, A0_DEST_CHANNEL_X, 0,
403                          src0,
404                          swizzle(consts1, Z, ZERO, ZERO, ZERO), /* 1/(2pi) */
405                          swizzle(consts0, W, ZERO, ZERO, ZERO)); /* .75 */
406
407          i915_emit_arith(p, A0_FRC, tmp, A0_DEST_CHANNEL_X, 0, tmp, 0, 0);
408
409          i915_emit_arith(p,
410                          A0_MAD,
411                          tmp, A0_DEST_CHANNEL_X, 0,
412                          tmp,
413                          swizzle(consts0, X, ZERO, ZERO, ZERO), /* 2 */
414                          swizzle(consts0, Y, ZERO, ZERO, ZERO)); /* -1 */
415
416          /* Compute COS with the same calculation used for SIN, but a
417           * different source range has been mapped to [-1,1] this time.
418           */
419
420          /* tmp.y = abs(tmp.x); {x, abs(x), 0, 0} */
421          i915_emit_arith(p,
422                          A0_MAX,
423                          tmp, A0_DEST_CHANNEL_Y, 0,
424                          swizzle(tmp, ZERO, X, ZERO, ZERO),
425                          negate(swizzle(tmp, ZERO, X, ZERO, ZERO), 0, 1, 0, 0),
426                          0);
427
428          /* tmp.y = tmp.y * tmp.x; {x, x * abs(x), 0, 0} */
429          i915_emit_arith(p,
430                          A0_MUL,
431                          tmp, A0_DEST_CHANNEL_Y, 0,
432                          swizzle(tmp, ZERO, X, ZERO, ZERO),
433                          tmp,
434                          0);
435
436          /* tmp.x = tmp.xy DP sin_quad_constants[2].xy */
437          i915_emit_arith(p,
438                          A0_DP3,
439                          tmp, A0_DEST_CHANNEL_X, 0,
440                          tmp,
441                          swizzle(consts1, X, Y, ZERO, ZERO),
442                          0);
443
444          /* tmp.x now contains a first approximation (y).  Now, weight it
445           * against tmp.y**2 to get closer.
446           */
447          i915_emit_arith(p,
448                          A0_MAX,
449                          tmp, A0_DEST_CHANNEL_Y, 0,
450                          swizzle(tmp, ZERO, X, ZERO, ZERO),
451                          negate(swizzle(tmp, ZERO, X, ZERO, ZERO), 0, 1, 0, 0),
452                          0);
453
454          /* tmp.y = tmp.x * tmp.y - tmp.x; {y, y * abs(y) - y, 0, 0} */
455          i915_emit_arith(p,
456                          A0_MAD,
457                          tmp, A0_DEST_CHANNEL_Y, 0,
458                          swizzle(tmp, ZERO, X, ZERO, ZERO),
459                          swizzle(tmp, ZERO, Y, ZERO, ZERO),
460                          negate(swizzle(tmp, ZERO, X, ZERO, ZERO), 0, 1, 0, 0));
461
462          /* result = .2225 * tmp.y + tmp.x =.2225(y * abs(y) - y) + y= */
463          i915_emit_arith(p,
464                          A0_MAD,
465                          get_result_vector(p, inst),
466                          get_result_flags(inst), 0,
467                          swizzle(consts1, W, W, W, W),
468                          swizzle(tmp, Y, Y, Y, Y),
469                          swizzle(tmp, X, X, X, X));
470          break;
471
472       case OPCODE_DP3:
473          EMIT_2ARG_ARITH(A0_DP3);
474          break;
475
476       case OPCODE_DP4:
477          EMIT_2ARG_ARITH(A0_DP4);
478          break;
479
480       case OPCODE_DPH:
481          src0 = src_vector(p, &inst->SrcReg[0], program);
482          src1 = src_vector(p, &inst->SrcReg[1], program);
483
484          i915_emit_arith(p,
485                          A0_DP4,
486                          get_result_vector(p, inst),
487                          get_result_flags(inst), 0,
488                          swizzle(src0, X, Y, Z, ONE), src1, 0);
489          break;
490
491       case OPCODE_DST:
492          src0 = src_vector(p, &inst->SrcReg[0], program);
493          src1 = src_vector(p, &inst->SrcReg[1], program);
494
495          /* result[0] = 1    * 1;
496           * result[1] = a[1] * b[1];
497           * result[2] = a[2] * 1;
498           * result[3] = 1    * b[3];
499           */
500          i915_emit_arith(p,
501                          A0_MUL,
502                          get_result_vector(p, inst),
503                          get_result_flags(inst), 0,
504                          swizzle(src0, ONE, Y, Z, ONE),
505                          swizzle(src1, ONE, Y, ONE, W), 0);
506          break;
507
508       case OPCODE_EX2:
509          src0 = src_vector(p, &inst->SrcReg[0], program);
510
511          i915_emit_arith(p,
512                          A0_EXP,
513                          get_result_vector(p, inst),
514                          get_result_flags(inst), 0,
515                          swizzle(src0, X, X, X, X), 0, 0);
516          break;
517
518       case OPCODE_FLR:
519          EMIT_1ARG_ARITH(A0_FLR);
520          break;
521
522       case OPCODE_TRUNC:
523          EMIT_1ARG_ARITH(A0_TRC);
524          break;
525
526       case OPCODE_FRC:
527          EMIT_1ARG_ARITH(A0_FRC);
528          break;
529
530       case OPCODE_KIL:
531          src0 = src_vector(p, &inst->SrcReg[0], program);
532          tmp = i915_get_utemp(p);
533
534          i915_emit_texld(p, get_live_regs(p, inst),
535                          tmp, A0_DEST_CHANNEL_ALL,   /* use a dummy dest reg */
536                          0, src0, T0_TEXKILL);
537          break;
538
539       case OPCODE_KIL_NV:
540          if (inst->DstReg.CondMask == COND_TR) {
541             tmp = i915_get_utemp(p);
542
543             i915_emit_texld(p, get_live_regs(p, inst),
544                             tmp, A0_DEST_CHANNEL_ALL,
545                             0, /* use a dummy dest reg */
546                             swizzle(tmp, ONE, ONE, ONE, ONE), /* always */
547                             T0_TEXKILL);
548          } else {
549             p->error = 1;
550             i915_program_error(p, "Unsupported KIL_NV condition code: %d",
551                                inst->DstReg.CondMask);
552          }
553          break;
554
555       case OPCODE_LG2:
556          src0 = src_vector(p, &inst->SrcReg[0], program);
557
558          i915_emit_arith(p,
559                          A0_LOG,
560                          get_result_vector(p, inst),
561                          get_result_flags(inst), 0,
562                          swizzle(src0, X, X, X, X), 0, 0);
563          break;
564
565       case OPCODE_LIT:
566          src0 = src_vector(p, &inst->SrcReg[0], program);
567          tmp = i915_get_utemp(p);
568
569          /* tmp = max( a.xyzw, a.00zw )
570           * XXX: Clamp tmp.w to -128..128
571           * tmp.y = log(tmp.y)
572           * tmp.y = tmp.w * tmp.y
573           * tmp.y = exp(tmp.y)
574           * result = cmp (a.11-x1, a.1x01, a.1xy1 )
575           */
576          i915_emit_arith(p, A0_MAX, tmp, A0_DEST_CHANNEL_ALL, 0,
577                          src0, swizzle(src0, ZERO, ZERO, Z, W), 0);
578
579          i915_emit_arith(p, A0_LOG, tmp, A0_DEST_CHANNEL_Y, 0,
580                          swizzle(tmp, Y, Y, Y, Y), 0, 0);
581
582          i915_emit_arith(p, A0_MUL, tmp, A0_DEST_CHANNEL_Y, 0,
583                          swizzle(tmp, ZERO, Y, ZERO, ZERO),
584                          swizzle(tmp, ZERO, W, ZERO, ZERO), 0);
585
586          i915_emit_arith(p, A0_EXP, tmp, A0_DEST_CHANNEL_Y, 0,
587                          swizzle(tmp, Y, Y, Y, Y), 0, 0);
588
589          i915_emit_arith(p, A0_CMP,
590                          get_result_vector(p, inst),
591                          get_result_flags(inst), 0,
592                          negate(swizzle(tmp, ONE, ONE, X, ONE), 0, 0, 1, 0),
593                          swizzle(tmp, ONE, X, ZERO, ONE),
594                          swizzle(tmp, ONE, X, Y, ONE));
595
596          break;
597
598       case OPCODE_LRP:
599          src0 = src_vector(p, &inst->SrcReg[0], program);
600          src1 = src_vector(p, &inst->SrcReg[1], program);
601          src2 = src_vector(p, &inst->SrcReg[2], program);
602          flags = get_result_flags(inst);
603          tmp = i915_get_utemp(p);
604
605          /* b*a + c*(1-a)
606           *
607           * b*a + c - ca 
608           *
609           * tmp = b*a + c, 
610           * result = (-c)*a + tmp 
611           */
612          i915_emit_arith(p, A0_MAD, tmp,
613                          flags & A0_DEST_CHANNEL_ALL, 0, src1, src0, src2);
614
615          i915_emit_arith(p, A0_MAD,
616                          get_result_vector(p, inst),
617                          flags, 0, negate(src2, 1, 1, 1, 1), src0, tmp);
618          break;
619
620       case OPCODE_MAD:
621          EMIT_3ARG_ARITH(A0_MAD);
622          break;
623
624       case OPCODE_MAX:
625          EMIT_2ARG_ARITH(A0_MAX);
626          break;
627
628       case OPCODE_MIN:
629          src0 = src_vector(p, &inst->SrcReg[0], program);
630          src1 = src_vector(p, &inst->SrcReg[1], program);
631          tmp = i915_get_utemp(p);
632          flags = get_result_flags(inst);
633
634          i915_emit_arith(p,
635                          A0_MAX,
636                          tmp, flags & A0_DEST_CHANNEL_ALL, 0,
637                          negate(src0, 1, 1, 1, 1),
638                          negate(src1, 1, 1, 1, 1), 0);
639
640          i915_emit_arith(p,
641                          A0_MOV,
642                          get_result_vector(p, inst),
643                          flags, 0, negate(tmp, 1, 1, 1, 1), 0, 0);
644          break;
645
646       case OPCODE_MOV:
647          EMIT_1ARG_ARITH(A0_MOV);
648          break;
649
650       case OPCODE_MUL:
651          EMIT_2ARG_ARITH(A0_MUL);
652          break;
653
654       case OPCODE_NOISE1:
655       case OPCODE_NOISE2:
656       case OPCODE_NOISE3:
657       case OPCODE_NOISE4:
658          /* Don't implement noise because we just don't have the instructions
659           * to spare.  We aren't the first vendor to do so.
660           */
661          i915_program_error(p, "Stubbed-out noise functions");
662          i915_emit_arith(p,
663                          A0_MOV,
664                          get_result_vector(p, inst),
665                          get_result_flags(inst), 0,
666                          swizzle(tmp, ZERO, ZERO, ZERO, ZERO), 0, 0);
667
668       case OPCODE_POW:
669          src0 = src_vector(p, &inst->SrcReg[0], program);
670          src1 = src_vector(p, &inst->SrcReg[1], program);
671          tmp = i915_get_utemp(p);
672          flags = get_result_flags(inst);
673
674          /* XXX: masking on intermediate values, here and elsewhere.
675           */
676          i915_emit_arith(p,
677                          A0_LOG,
678                          tmp, A0_DEST_CHANNEL_X, 0,
679                          swizzle(src0, X, X, X, X), 0, 0);
680
681          i915_emit_arith(p, A0_MUL, tmp, A0_DEST_CHANNEL_X, 0, tmp, src1, 0);
682
683
684          i915_emit_arith(p,
685                          A0_EXP,
686                          get_result_vector(p, inst),
687                          flags, 0, swizzle(tmp, X, X, X, X), 0, 0);
688
689          break;
690
691       case OPCODE_RCP:
692          src0 = src_vector(p, &inst->SrcReg[0], program);
693
694          i915_emit_arith(p,
695                          A0_RCP,
696                          get_result_vector(p, inst),
697                          get_result_flags(inst), 0,
698                          swizzle(src0, X, X, X, X), 0, 0);
699          break;
700
701       case OPCODE_RSQ:
702
703          src0 = src_vector(p, &inst->SrcReg[0], program);
704
705          i915_emit_arith(p,
706                          A0_RSQ,
707                          get_result_vector(p, inst),
708                          get_result_flags(inst), 0,
709                          swizzle(src0, X, X, X, X), 0, 0);
710          break;
711
712       case OPCODE_SCS:
713          src0 = src_vector(p, &inst->SrcReg[0], program);
714          tmp = i915_get_utemp(p);
715
716          /* 
717           * t0.xy = MUL x.xx11, x.x1111  ; x^2, x, 1, 1
718           * t0 = MUL t0.xyxy t0.xx11 ; x^4, x^3, x^2, x
719           * t1 = MUL t0.xyyw t0.yz11    ; x^7 x^5 x^3 x
720           * scs.x = DP4 t1, sin_constants
721           * t1 = MUL t0.xxz1 t0.z111    ; x^6 x^4 x^2 1
722           * scs.y = DP4 t1, cos_constants
723           */
724          i915_emit_arith(p,
725                          A0_MUL,
726                          tmp, A0_DEST_CHANNEL_XY, 0,
727                          swizzle(src0, X, X, ONE, ONE),
728                          swizzle(src0, X, ONE, ONE, ONE), 0);
729
730          i915_emit_arith(p,
731                          A0_MUL,
732                          tmp, A0_DEST_CHANNEL_ALL, 0,
733                          swizzle(tmp, X, Y, X, Y),
734                          swizzle(tmp, X, X, ONE, ONE), 0);
735
736          if (inst->DstReg.WriteMask & WRITEMASK_Y) {
737             GLuint tmp1;
738
739             if (inst->DstReg.WriteMask & WRITEMASK_X)
740                tmp1 = i915_get_utemp(p);
741             else
742                tmp1 = tmp;
743
744             i915_emit_arith(p,
745                             A0_MUL,
746                             tmp1, A0_DEST_CHANNEL_ALL, 0,
747                             swizzle(tmp, X, Y, Y, W),
748                             swizzle(tmp, X, Z, ONE, ONE), 0);
749
750             i915_emit_arith(p,
751                             A0_DP4,
752                             get_result_vector(p, inst),
753                             A0_DEST_CHANNEL_Y, 0,
754                             swizzle(tmp1, W, Z, Y, X),
755                             i915_emit_const4fv(p, sin_constants), 0);
756          }
757
758          if (inst->DstReg.WriteMask & WRITEMASK_X) {
759             i915_emit_arith(p,
760                             A0_MUL,
761                             tmp, A0_DEST_CHANNEL_XYZ, 0,
762                             swizzle(tmp, X, X, Z, ONE),
763                             swizzle(tmp, Z, ONE, ONE, ONE), 0);
764
765             i915_emit_arith(p,
766                             A0_DP4,
767                             get_result_vector(p, inst),
768                             A0_DEST_CHANNEL_X, 0,
769                             swizzle(tmp, ONE, Z, Y, X),
770                             i915_emit_const4fv(p, cos_constants), 0);
771          }
772          break;
773
774       case OPCODE_SEQ:
775          tmp = i915_get_utemp(p);
776          flags = get_result_flags(inst);
777          dst = get_result_vector(p, inst);
778
779          /* dst = src1 >= src2 */
780          i915_emit_arith(p,
781                          A0_SGE,
782                          dst,
783                          flags, 0,
784                          src_vector(p, &inst->SrcReg[0], program),
785                          src_vector(p, &inst->SrcReg[1], program),
786                          0);
787          /* tmp = src1 <= src2 */
788          i915_emit_arith(p,
789                          A0_SGE,
790                          tmp,
791                          flags, 0,
792                          negate(src_vector(p, &inst->SrcReg[0], program),
793                                 1, 1, 1, 1),
794                          negate(src_vector(p, &inst->SrcReg[1], program),
795                                 1, 1, 1, 1),
796                          0);
797          /* dst = tmp && dst */
798          i915_emit_arith(p,
799                          A0_MUL,
800                          dst,
801                          flags, 0,
802                          dst,
803                          tmp,
804                          0);
805          break;
806
807       case OPCODE_SIN:
808          src0 = src_vector(p, &inst->SrcReg[0], program);
809          tmp = i915_get_utemp(p);
810          consts0 = i915_emit_const4fv(p, sin_quad_constants[0]);
811          consts1 = i915_emit_const4fv(p, sin_quad_constants[1]);
812
813          /* Reduce range from repeating about [-pi,pi] to [-1,1] */
814          i915_emit_arith(p,
815                          A0_MAD,
816                          tmp, A0_DEST_CHANNEL_X, 0,
817                          src0,
818                          swizzle(consts1, Z, ZERO, ZERO, ZERO), /* 1/(2pi) */
819                          swizzle(consts0, Z, ZERO, ZERO, ZERO)); /* .5 */
820
821          i915_emit_arith(p, A0_FRC, tmp, A0_DEST_CHANNEL_X, 0, tmp, 0, 0);
822
823          i915_emit_arith(p,
824                          A0_MAD,
825                          tmp, A0_DEST_CHANNEL_X, 0,
826                          tmp,
827                          swizzle(consts0, X, ZERO, ZERO, ZERO), /* 2 */
828                          swizzle(consts0, Y, ZERO, ZERO, ZERO)); /* -1 */
829
830          /* Compute sin using a quadratic and quartic.  It gives continuity
831           * that repeating the Taylor series lacks every 2*pi, and has
832           * reduced error.
833           *
834           * The idea was described at:
835           * http://www.devmaster.net/forums/showthread.php?t=5784
836           */
837
838          /* tmp.y = abs(tmp.x); {x, abs(x), 0, 0} */
839          i915_emit_arith(p,
840                          A0_MAX,
841                          tmp, A0_DEST_CHANNEL_Y, 0,
842                          swizzle(tmp, ZERO, X, ZERO, ZERO),
843                          negate(swizzle(tmp, ZERO, X, ZERO, ZERO), 0, 1, 0, 0),
844                          0);
845
846          /* tmp.y = tmp.y * tmp.x; {x, x * abs(x), 0, 0} */
847          i915_emit_arith(p,
848                          A0_MUL,
849                          tmp, A0_DEST_CHANNEL_Y, 0,
850                          swizzle(tmp, ZERO, X, ZERO, ZERO),
851                          tmp,
852                          0);
853
854          /* tmp.x = tmp.xy DP sin_quad_constants[2].xy */
855          i915_emit_arith(p,
856                          A0_DP3,
857                          tmp, A0_DEST_CHANNEL_X, 0,
858                          tmp,
859                          swizzle(consts1, X, Y, ZERO, ZERO),
860                          0);
861
862          /* tmp.x now contains a first approximation (y).  Now, weight it
863           * against tmp.y**2 to get closer.
864           */
865          i915_emit_arith(p,
866                          A0_MAX,
867                          tmp, A0_DEST_CHANNEL_Y, 0,
868                          swizzle(tmp, ZERO, X, ZERO, ZERO),
869                          negate(swizzle(tmp, ZERO, X, ZERO, ZERO), 0, 1, 0, 0),
870                          0);
871
872          /* tmp.y = tmp.x * tmp.y - tmp.x; {y, y * abs(y) - y, 0, 0} */
873          i915_emit_arith(p,
874                          A0_MAD,
875                          tmp, A0_DEST_CHANNEL_Y, 0,
876                          swizzle(tmp, ZERO, X, ZERO, ZERO),
877                          swizzle(tmp, ZERO, Y, ZERO, ZERO),
878                          negate(swizzle(tmp, ZERO, X, ZERO, ZERO), 0, 1, 0, 0));
879
880          /* result = .2225 * tmp.y + tmp.x =.2225(y * abs(y) - y) + y= */
881          i915_emit_arith(p,
882                          A0_MAD,
883                          get_result_vector(p, inst),
884                          get_result_flags(inst), 0,
885                          swizzle(consts1, W, W, W, W),
886                          swizzle(tmp, Y, Y, Y, Y),
887                          swizzle(tmp, X, X, X, X));
888
889          break;
890
891       case OPCODE_SGE:
892          EMIT_2ARG_ARITH(A0_SGE);
893          break;
894
895       case OPCODE_SGT:
896          i915_emit_arith(p,
897                          A0_SLT,
898                          get_result_vector( p, inst ),
899                          get_result_flags( inst ), 0,
900                          negate(src_vector( p, &inst->SrcReg[0], program),
901                                 1, 1, 1, 1),
902                          negate(src_vector( p, &inst->SrcReg[1], program),
903                                 1, 1, 1, 1),
904                          0);
905          break;
906
907       case OPCODE_SLE:
908          i915_emit_arith(p,
909                          A0_SGE,
910                          get_result_vector( p, inst ),
911                          get_result_flags( inst ), 0,
912                          negate(src_vector( p, &inst->SrcReg[0], program),
913                                 1, 1, 1, 1),
914                          negate(src_vector( p, &inst->SrcReg[1], program),
915                                 1, 1, 1, 1),
916                          0);
917          break;
918
919       case OPCODE_SLT:
920          EMIT_2ARG_ARITH(A0_SLT);
921          break;
922
923       case OPCODE_SNE:
924          tmp = i915_get_utemp(p);
925          flags = get_result_flags(inst);
926          dst = get_result_vector(p, inst);
927
928          /* dst = src1 < src2 */
929          i915_emit_arith(p,
930                          A0_SLT,
931                          dst,
932                          flags, 0,
933                          src_vector(p, &inst->SrcReg[0], program),
934                          src_vector(p, &inst->SrcReg[1], program),
935                          0);
936          /* tmp = src1 > src2 */
937          i915_emit_arith(p,
938                          A0_SLT,
939                          tmp,
940                          flags, 0,
941                          negate(src_vector(p, &inst->SrcReg[0], program),
942                                 1, 1, 1, 1),
943                          negate(src_vector(p, &inst->SrcReg[1], program),
944                                 1, 1, 1, 1),
945                          0);
946          /* dst = tmp || dst */
947          i915_emit_arith(p,
948                          A0_ADD,
949                          dst,
950                          flags | A0_DEST_SATURATE, 0,
951                          dst,
952                          tmp,
953                          0);
954          break;
955
956       case OPCODE_SUB:
957          src0 = src_vector(p, &inst->SrcReg[0], program);
958          src1 = src_vector(p, &inst->SrcReg[1], program);
959
960          i915_emit_arith(p,
961                          A0_ADD,
962                          get_result_vector(p, inst),
963                          get_result_flags(inst), 0,
964                          src0, negate(src1, 1, 1, 1, 1), 0);
965          break;
966
967       case OPCODE_SWZ:
968          EMIT_1ARG_ARITH(A0_MOV);       /* extended swizzle handled natively */
969          break;
970
971       case OPCODE_TEX:
972          EMIT_TEX(T0_TEXLD);
973          break;
974
975       case OPCODE_TXB:
976          EMIT_TEX(T0_TEXLDB);
977          break;
978
979       case OPCODE_TXP:
980          EMIT_TEX(T0_TEXLDP);
981          break;
982
983       case OPCODE_XPD:
984          /* Cross product:
985           *      result.x = src0.y * src1.z - src0.z * src1.y;
986           *      result.y = src0.z * src1.x - src0.x * src1.z;
987           *      result.z = src0.x * src1.y - src0.y * src1.x;
988           *      result.w = undef;
989           */
990          src0 = src_vector(p, &inst->SrcReg[0], program);
991          src1 = src_vector(p, &inst->SrcReg[1], program);
992          tmp = i915_get_utemp(p);
993
994          i915_emit_arith(p,
995                          A0_MUL,
996                          tmp, A0_DEST_CHANNEL_ALL, 0,
997                          swizzle(src0, Z, X, Y, ONE),
998                          swizzle(src1, Y, Z, X, ONE), 0);
999
1000          i915_emit_arith(p,
1001                          A0_MAD,
1002                          get_result_vector(p, inst),
1003                          get_result_flags(inst), 0,
1004                          swizzle(src0, Y, Z, X, ONE),
1005                          swizzle(src1, Z, X, Y, ONE),
1006                          negate(tmp, 1, 1, 1, 0));
1007          break;
1008
1009       case OPCODE_END:
1010          return;
1011
1012       case OPCODE_BGNLOOP:
1013       case OPCODE_BGNSUB:
1014       case OPCODE_BRA:
1015       case OPCODE_BRK:
1016       case OPCODE_CAL:
1017       case OPCODE_CONT:
1018       case OPCODE_DDX:
1019       case OPCODE_DDY:
1020       case OPCODE_ELSE:
1021       case OPCODE_ENDIF:
1022       case OPCODE_ENDLOOP:
1023       case OPCODE_ENDSUB:
1024       case OPCODE_IF:
1025       case OPCODE_RET:
1026          p->error = 1;
1027          i915_program_error(p, "Unsupported opcode: %s",
1028                             _mesa_opcode_string(inst->Opcode));
1029          return;
1030
1031       case OPCODE_EXP:
1032       case OPCODE_LOG:
1033          /* These opcodes are claimed as GLSL, NV_vp, and ARB_vp in
1034           * prog_instruction.h, but apparently GLSL doesn't ever emit them.
1035           * Instead, it translates to EX2 or LG2.
1036           */
1037       case OPCODE_TXD:
1038       case OPCODE_TXL:
1039          /* These opcodes are claimed by GLSL in prog_instruction.h, but
1040           * only NV_vp/fp appears to emit them.
1041           */
1042       default:
1043          i915_program_error(p, "bad opcode: %s",
1044                             _mesa_opcode_string(inst->Opcode));
1045          return;
1046       }
1047
1048       inst++;
1049       i915_release_utemps(p);
1050    }
1051 }
1052
1053 /* Rather than trying to intercept and jiggle depth writes during
1054  * emit, just move the value into its correct position at the end of
1055  * the program:
1056  */
1057 static void
1058 fixup_depth_write(struct i915_fragment_program *p)
1059 {
1060    if (p->depth_written) {
1061       GLuint depth = UREG(REG_TYPE_OD, 0);
1062
1063       i915_emit_arith(p,
1064                       A0_MOV,
1065                       depth, A0_DEST_CHANNEL_W, 0,
1066                       swizzle(depth, X, Y, Z, Z), 0, 0);
1067    }
1068 }
1069
1070
1071 static void
1072 check_wpos(struct i915_fragment_program *p)
1073 {
1074    GLuint inputs = p->FragProg.Base.InputsRead;
1075    GLint i;
1076
1077    p->wpos_tex = -1;
1078
1079    for (i = 0; i < p->ctx->Const.MaxTextureCoordUnits; i++) {
1080       if (inputs & (FRAG_BIT_TEX(i) | FRAG_BIT_VAR(i)))
1081          continue;
1082       else if (inputs & FRAG_BIT_WPOS) {
1083          p->wpos_tex = i;
1084          inputs &= ~FRAG_BIT_WPOS;
1085       }
1086    }
1087
1088    if (inputs & FRAG_BIT_WPOS) {
1089       i915_program_error(p, "No free texcoord for wpos value");
1090    }
1091 }
1092
1093
1094 static void
1095 translate_program(struct i915_fragment_program *p)
1096 {
1097    struct i915_context *i915 = I915_CONTEXT(p->ctx);
1098
1099    i915_init_program(i915, p);
1100    check_wpos(p);
1101    upload_program(p);
1102    fixup_depth_write(p);
1103    i915_fini_program(p);
1104
1105    p->translated = 1;
1106 }
1107
1108
1109 static void
1110 track_params(struct i915_fragment_program *p)
1111 {
1112    GLint i;
1113
1114    if (p->nr_params)
1115       _mesa_load_state_parameters(p->ctx, p->FragProg.Base.Parameters);
1116
1117    for (i = 0; i < p->nr_params; i++) {
1118       GLint reg = p->param[i].reg;
1119       COPY_4V(p->constant[reg], p->param[i].values);
1120    }
1121
1122    p->params_uptodate = 1;
1123    p->on_hardware = 0;          /* overkill */
1124 }
1125
1126
1127 static void
1128 i915BindProgram(GLcontext * ctx, GLenum target, struct gl_program *prog)
1129 {
1130    if (target == GL_FRAGMENT_PROGRAM_ARB) {
1131       struct i915_context *i915 = I915_CONTEXT(ctx);
1132       struct i915_fragment_program *p = (struct i915_fragment_program *) prog;
1133
1134       if (i915->current_program == p)
1135          return;
1136
1137       if (i915->current_program) {
1138          i915->current_program->on_hardware = 0;
1139          i915->current_program->params_uptodate = 0;
1140       }
1141
1142       i915->current_program = p;
1143
1144       assert(p->on_hardware == 0);
1145       assert(p->params_uptodate == 0);
1146
1147    }
1148 }
1149
1150 static struct gl_program *
1151 i915NewProgram(GLcontext * ctx, GLenum target, GLuint id)
1152 {
1153    switch (target) {
1154    case GL_VERTEX_PROGRAM_ARB:
1155       return _mesa_init_vertex_program(ctx, CALLOC_STRUCT(gl_vertex_program),
1156                                        target, id);
1157
1158    case GL_FRAGMENT_PROGRAM_ARB:{
1159          struct i915_fragment_program *prog =
1160             CALLOC_STRUCT(i915_fragment_program);
1161          if (prog) {
1162             i915_init_program(I915_CONTEXT(ctx), prog);
1163
1164             return _mesa_init_fragment_program(ctx, &prog->FragProg,
1165                                                target, id);
1166          }
1167          else
1168             return NULL;
1169       }
1170
1171    default:
1172       /* Just fallback:
1173        */
1174       return _mesa_new_program(ctx, target, id);
1175    }
1176 }
1177
1178 static void
1179 i915DeleteProgram(GLcontext * ctx, struct gl_program *prog)
1180 {
1181    if (prog->Target == GL_FRAGMENT_PROGRAM_ARB) {
1182       struct i915_context *i915 = I915_CONTEXT(ctx);
1183       struct i915_fragment_program *p = (struct i915_fragment_program *) prog;
1184
1185       if (i915->current_program == p)
1186          i915->current_program = 0;
1187    }
1188
1189    _mesa_delete_program(ctx, prog);
1190 }
1191
1192
1193 static GLboolean
1194 i915IsProgramNative(GLcontext * ctx, GLenum target, struct gl_program *prog)
1195 {
1196    if (target == GL_FRAGMENT_PROGRAM_ARB) {
1197       struct i915_fragment_program *p = (struct i915_fragment_program *) prog;
1198
1199       if (!p->translated)
1200          translate_program(p);
1201
1202       return !p->error;
1203    }
1204    else
1205       return GL_TRUE;
1206 }
1207
1208 static void
1209 i915ProgramStringNotify(GLcontext * ctx,
1210                         GLenum target, struct gl_program *prog)
1211 {
1212    if (target == GL_FRAGMENT_PROGRAM_ARB) {
1213       struct i915_fragment_program *p = (struct i915_fragment_program *) prog;
1214       p->translated = 0;
1215
1216       /* Hack: make sure fog is correctly enabled according to this
1217        * fragment program's fog options.
1218        */
1219       if (p->FragProg.FogOption) {
1220          /* add extra instructions to do fog, then turn off FogOption field */
1221          _mesa_append_fog_code(ctx, &p->FragProg);
1222          p->FragProg.FogOption = GL_NONE;
1223       }
1224    }
1225
1226    _tnl_program_string(ctx, target, prog);
1227 }
1228
1229 void
1230 i915_update_program(GLcontext *ctx)
1231 {
1232    struct intel_context *intel = intel_context(ctx);
1233    struct i915_context *i915 = i915_context(&intel->ctx);
1234    struct i915_fragment_program *fp =
1235       (struct i915_fragment_program *) ctx->FragmentProgram._Current;
1236
1237    if (i915->current_program != fp) {
1238       if (i915->current_program) {
1239          i915->current_program->on_hardware = 0;
1240          i915->current_program->params_uptodate = 0;
1241       }
1242
1243       i915->current_program = fp;
1244    }
1245
1246    if (!fp->translated)
1247       translate_program(fp);
1248
1249    FALLBACK(&i915->intel, I915_FALLBACK_PROGRAM, fp->error);
1250 }
1251
1252 void
1253 i915ValidateFragmentProgram(struct i915_context *i915)
1254 {
1255    GLcontext *ctx = &i915->intel.ctx;
1256    struct intel_context *intel = intel_context(ctx);
1257    TNLcontext *tnl = TNL_CONTEXT(ctx);
1258    struct vertex_buffer *VB = &tnl->vb;
1259
1260    struct i915_fragment_program *p =
1261       (struct i915_fragment_program *) ctx->FragmentProgram._Current;
1262
1263    const GLuint inputsRead = p->FragProg.Base.InputsRead;
1264    GLuint s4 = i915->state.Ctx[I915_CTXREG_LIS4] & ~S4_VFMT_MASK;
1265    GLuint s2 = S2_TEXCOORD_NONE;
1266    int i, offset = 0;
1267
1268    /* Important:
1269     */
1270    VB->AttribPtr[VERT_ATTRIB_POS] = VB->NdcPtr;
1271
1272    if (!p->translated)
1273       translate_program(p);
1274
1275    intel->vertex_attr_count = 0;
1276    intel->wpos_offset = 0;
1277    intel->wpos_size = 0;
1278    intel->coloroffset = 0;
1279    intel->specoffset = 0;
1280
1281    if (inputsRead & FRAG_BITS_TEX_ANY) {
1282       EMIT_ATTR(_TNL_ATTRIB_POS, EMIT_4F_VIEWPORT, S4_VFMT_XYZW, 16);
1283    }
1284    else {
1285       EMIT_ATTR(_TNL_ATTRIB_POS, EMIT_3F_VIEWPORT, S4_VFMT_XYZ, 12);
1286    }
1287
1288    if (inputsRead & FRAG_BIT_COL0) {
1289       intel->coloroffset = offset / 4;
1290       EMIT_ATTR(_TNL_ATTRIB_COLOR0, EMIT_4UB_4F_BGRA, S4_VFMT_COLOR, 4);
1291    }
1292
1293    if (inputsRead & FRAG_BIT_COL1) {
1294        intel->specoffset = offset / 4;
1295        EMIT_ATTR(_TNL_ATTRIB_COLOR1, EMIT_4UB_4F_BGRA, S4_VFMT_SPEC_FOG, 4);
1296    }
1297
1298    if ((inputsRead & FRAG_BIT_FOGC) || i915->vertex_fog != I915_FOG_NONE) {
1299       EMIT_ATTR(_TNL_ATTRIB_FOG, EMIT_1F, S4_VFMT_FOG_PARAM, 4);
1300    }
1301
1302    for (i = 0; i < p->ctx->Const.MaxTextureCoordUnits; i++) {
1303       if (inputsRead & FRAG_BIT_TEX(i)) {
1304          int sz = VB->AttribPtr[_TNL_ATTRIB_TEX0 + i]->size;
1305
1306          s2 &= ~S2_TEXCOORD_FMT(i, S2_TEXCOORD_FMT0_MASK);
1307          s2 |= S2_TEXCOORD_FMT(i, SZ_TO_HW(sz));
1308
1309          EMIT_ATTR(_TNL_ATTRIB_TEX0 + i, EMIT_SZ(sz), 0, sz * 4);
1310       }
1311       else if (inputsRead & FRAG_BIT_VAR(i)) {
1312          int sz = VB->AttribPtr[_TNL_ATTRIB_GENERIC0 + i]->size;
1313
1314          s2 &= ~S2_TEXCOORD_FMT(i, S2_TEXCOORD_FMT0_MASK);
1315          s2 |= S2_TEXCOORD_FMT(i, SZ_TO_HW(sz));
1316
1317          EMIT_ATTR(_TNL_ATTRIB_GENERIC0 + i, EMIT_SZ(sz), 0, sz * 4);
1318       }
1319       else if (i == p->wpos_tex) {
1320
1321          /* If WPOS is required, duplicate the XYZ position data in an
1322           * unused texture coordinate:
1323           */
1324          s2 &= ~S2_TEXCOORD_FMT(i, S2_TEXCOORD_FMT0_MASK);
1325          s2 |= S2_TEXCOORD_FMT(i, SZ_TO_HW(3));
1326
1327          intel->wpos_offset = offset;
1328          intel->wpos_size = 3 * sizeof(GLuint);
1329
1330          EMIT_PAD(intel->wpos_size);
1331       }
1332    }
1333
1334    if (s2 != i915->state.Ctx[I915_CTXREG_LIS2] ||
1335        s4 != i915->state.Ctx[I915_CTXREG_LIS4]) {
1336       int k;
1337
1338       I915_STATECHANGE(i915, I915_UPLOAD_CTX);
1339
1340       /* Must do this *after* statechange, so as not to affect
1341        * buffered vertices reliant on the old state:
1342        */
1343       intel->vertex_size = _tnl_install_attrs(&intel->ctx,
1344                                               intel->vertex_attrs,
1345                                               intel->vertex_attr_count,
1346                                               intel->ViewportMatrix.m, 0);
1347
1348       intel->vertex_size >>= 2;
1349
1350       i915->state.Ctx[I915_CTXREG_LIS2] = s2;
1351       i915->state.Ctx[I915_CTXREG_LIS4] = s4;
1352
1353       k = intel->vtbl.check_vertex_size(intel, intel->vertex_size);
1354       assert(k);
1355    }
1356
1357    if (!p->params_uptodate)
1358       track_params(p);
1359
1360    if (!p->on_hardware)
1361       i915_upload_program(i915, p);
1362 }
1363
1364 void
1365 i915InitFragProgFuncs(struct dd_function_table *functions)
1366 {
1367    functions->BindProgram = i915BindProgram;
1368    functions->NewProgram = i915NewProgram;
1369    functions->DeleteProgram = i915DeleteProgram;
1370    functions->IsProgramNative = i915IsProgramNative;
1371    functions->ProgramStringNotify = i915ProgramStringNotify;
1372 }