906d5855a756e75bef1217e8474bf986818486ed
[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 break;
668
669 case OPCODE_POW:
670 src0 = src_vector(p, &inst->SrcReg[0], program);
671 src1 = src_vector(p, &inst->SrcReg[1], program);
672 tmp = i915_get_utemp(p);
673 flags = get_result_flags(inst);
674
675 /* XXX: masking on intermediate values, here and elsewhere.
676 */
677 i915_emit_arith(p,
678 A0_LOG,
679 tmp, A0_DEST_CHANNEL_X, 0,
680 swizzle(src0, X, X, X, X), 0, 0);
681
682 i915_emit_arith(p, A0_MUL, tmp, A0_DEST_CHANNEL_X, 0, tmp, src1, 0);
683
684
685 i915_emit_arith(p,
686 A0_EXP,
687 get_result_vector(p, inst),
688 flags, 0, swizzle(tmp, X, X, X, X), 0, 0);
689
690 break;
691
692 case OPCODE_RCP:
693 src0 = src_vector(p, &inst->SrcReg[0], program);
694
695 i915_emit_arith(p,
696 A0_RCP,
697 get_result_vector(p, inst),
698 get_result_flags(inst), 0,
699 swizzle(src0, X, X, X, X), 0, 0);
700 break;
701
702 case OPCODE_RSQ:
703
704 src0 = src_vector(p, &inst->SrcReg[0], program);
705
706 i915_emit_arith(p,
707 A0_RSQ,
708 get_result_vector(p, inst),
709 get_result_flags(inst), 0,
710 swizzle(src0, X, X, X, X), 0, 0);
711 break;
712
713 case OPCODE_SCS:
714 src0 = src_vector(p, &inst->SrcReg[0], program);
715 tmp = i915_get_utemp(p);
716
717 /*
718 * t0.xy = MUL x.xx11, x.x1111 ; x^2, x, 1, 1
719 * t0 = MUL t0.xyxy t0.xx11 ; x^4, x^3, x^2, x
720 * t1 = MUL t0.xyyw t0.yz11 ; x^7 x^5 x^3 x
721 * scs.x = DP4 t1, sin_constants
722 * t1 = MUL t0.xxz1 t0.z111 ; x^6 x^4 x^2 1
723 * scs.y = DP4 t1, cos_constants
724 */
725 i915_emit_arith(p,
726 A0_MUL,
727 tmp, A0_DEST_CHANNEL_XY, 0,
728 swizzle(src0, X, X, ONE, ONE),
729 swizzle(src0, X, ONE, ONE, ONE), 0);
730
731 i915_emit_arith(p,
732 A0_MUL,
733 tmp, A0_DEST_CHANNEL_ALL, 0,
734 swizzle(tmp, X, Y, X, Y),
735 swizzle(tmp, X, X, ONE, ONE), 0);
736
737 if (inst->DstReg.WriteMask & WRITEMASK_Y) {
738 GLuint tmp1;
739
740 if (inst->DstReg.WriteMask & WRITEMASK_X)
741 tmp1 = i915_get_utemp(p);
742 else
743 tmp1 = tmp;
744
745 i915_emit_arith(p,
746 A0_MUL,
747 tmp1, A0_DEST_CHANNEL_ALL, 0,
748 swizzle(tmp, X, Y, Y, W),
749 swizzle(tmp, X, Z, ONE, ONE), 0);
750
751 i915_emit_arith(p,
752 A0_DP4,
753 get_result_vector(p, inst),
754 A0_DEST_CHANNEL_Y, 0,
755 swizzle(tmp1, W, Z, Y, X),
756 i915_emit_const4fv(p, sin_constants), 0);
757 }
758
759 if (inst->DstReg.WriteMask & WRITEMASK_X) {
760 i915_emit_arith(p,
761 A0_MUL,
762 tmp, A0_DEST_CHANNEL_XYZ, 0,
763 swizzle(tmp, X, X, Z, ONE),
764 swizzle(tmp, Z, ONE, ONE, ONE), 0);
765
766 i915_emit_arith(p,
767 A0_DP4,
768 get_result_vector(p, inst),
769 A0_DEST_CHANNEL_X, 0,
770 swizzle(tmp, ONE, Z, Y, X),
771 i915_emit_const4fv(p, cos_constants), 0);
772 }
773 break;
774
775 case OPCODE_SEQ:
776 tmp = i915_get_utemp(p);
777 flags = get_result_flags(inst);
778 dst = get_result_vector(p, inst);
779
780 /* dst = src1 >= src2 */
781 i915_emit_arith(p,
782 A0_SGE,
783 dst,
784 flags, 0,
785 src_vector(p, &inst->SrcReg[0], program),
786 src_vector(p, &inst->SrcReg[1], program),
787 0);
788 /* tmp = src1 <= src2 */
789 i915_emit_arith(p,
790 A0_SGE,
791 tmp,
792 flags, 0,
793 negate(src_vector(p, &inst->SrcReg[0], program),
794 1, 1, 1, 1),
795 negate(src_vector(p, &inst->SrcReg[1], program),
796 1, 1, 1, 1),
797 0);
798 /* dst = tmp && dst */
799 i915_emit_arith(p,
800 A0_MUL,
801 dst,
802 flags, 0,
803 dst,
804 tmp,
805 0);
806 break;
807
808 case OPCODE_SIN:
809 src0 = src_vector(p, &inst->SrcReg[0], program);
810 tmp = i915_get_utemp(p);
811 consts0 = i915_emit_const4fv(p, sin_quad_constants[0]);
812 consts1 = i915_emit_const4fv(p, sin_quad_constants[1]);
813
814 /* Reduce range from repeating about [-pi,pi] to [-1,1] */
815 i915_emit_arith(p,
816 A0_MAD,
817 tmp, A0_DEST_CHANNEL_X, 0,
818 src0,
819 swizzle(consts1, Z, ZERO, ZERO, ZERO), /* 1/(2pi) */
820 swizzle(consts0, Z, ZERO, ZERO, ZERO)); /* .5 */
821
822 i915_emit_arith(p, A0_FRC, tmp, A0_DEST_CHANNEL_X, 0, tmp, 0, 0);
823
824 i915_emit_arith(p,
825 A0_MAD,
826 tmp, A0_DEST_CHANNEL_X, 0,
827 tmp,
828 swizzle(consts0, X, ZERO, ZERO, ZERO), /* 2 */
829 swizzle(consts0, Y, ZERO, ZERO, ZERO)); /* -1 */
830
831 /* Compute sin using a quadratic and quartic. It gives continuity
832 * that repeating the Taylor series lacks every 2*pi, and has
833 * reduced error.
834 *
835 * The idea was described at:
836 * http://www.devmaster.net/forums/showthread.php?t=5784
837 */
838
839 /* tmp.y = abs(tmp.x); {x, abs(x), 0, 0} */
840 i915_emit_arith(p,
841 A0_MAX,
842 tmp, A0_DEST_CHANNEL_Y, 0,
843 swizzle(tmp, ZERO, X, ZERO, ZERO),
844 negate(swizzle(tmp, ZERO, X, ZERO, ZERO), 0, 1, 0, 0),
845 0);
846
847 /* tmp.y = tmp.y * tmp.x; {x, x * abs(x), 0, 0} */
848 i915_emit_arith(p,
849 A0_MUL,
850 tmp, A0_DEST_CHANNEL_Y, 0,
851 swizzle(tmp, ZERO, X, ZERO, ZERO),
852 tmp,
853 0);
854
855 /* tmp.x = tmp.xy DP sin_quad_constants[2].xy */
856 i915_emit_arith(p,
857 A0_DP3,
858 tmp, A0_DEST_CHANNEL_X, 0,
859 tmp,
860 swizzle(consts1, X, Y, ZERO, ZERO),
861 0);
862
863 /* tmp.x now contains a first approximation (y). Now, weight it
864 * against tmp.y**2 to get closer.
865 */
866 i915_emit_arith(p,
867 A0_MAX,
868 tmp, A0_DEST_CHANNEL_Y, 0,
869 swizzle(tmp, ZERO, X, ZERO, ZERO),
870 negate(swizzle(tmp, ZERO, X, ZERO, ZERO), 0, 1, 0, 0),
871 0);
872
873 /* tmp.y = tmp.x * tmp.y - tmp.x; {y, y * abs(y) - y, 0, 0} */
874 i915_emit_arith(p,
875 A0_MAD,
876 tmp, A0_DEST_CHANNEL_Y, 0,
877 swizzle(tmp, ZERO, X, ZERO, ZERO),
878 swizzle(tmp, ZERO, Y, ZERO, ZERO),
879 negate(swizzle(tmp, ZERO, X, ZERO, ZERO), 0, 1, 0, 0));
880
881 /* result = .2225 * tmp.y + tmp.x =.2225(y * abs(y) - y) + y= */
882 i915_emit_arith(p,
883 A0_MAD,
884 get_result_vector(p, inst),
885 get_result_flags(inst), 0,
886 swizzle(consts1, W, W, W, W),
887 swizzle(tmp, Y, Y, Y, Y),
888 swizzle(tmp, X, X, X, X));
889
890 break;
891
892 case OPCODE_SGE:
893 EMIT_2ARG_ARITH(A0_SGE);
894 break;
895
896 case OPCODE_SGT:
897 i915_emit_arith(p,
898 A0_SLT,
899 get_result_vector( p, inst ),
900 get_result_flags( inst ), 0,
901 negate(src_vector( p, &inst->SrcReg[0], program),
902 1, 1, 1, 1),
903 negate(src_vector( p, &inst->SrcReg[1], program),
904 1, 1, 1, 1),
905 0);
906 break;
907
908 case OPCODE_SLE:
909 i915_emit_arith(p,
910 A0_SGE,
911 get_result_vector( p, inst ),
912 get_result_flags( inst ), 0,
913 negate(src_vector( p, &inst->SrcReg[0], program),
914 1, 1, 1, 1),
915 negate(src_vector( p, &inst->SrcReg[1], program),
916 1, 1, 1, 1),
917 0);
918 break;
919
920 case OPCODE_SLT:
921 EMIT_2ARG_ARITH(A0_SLT);
922 break;
923
924 case OPCODE_SNE:
925 tmp = i915_get_utemp(p);
926 flags = get_result_flags(inst);
927 dst = get_result_vector(p, inst);
928
929 /* dst = src1 < src2 */
930 i915_emit_arith(p,
931 A0_SLT,
932 dst,
933 flags, 0,
934 src_vector(p, &inst->SrcReg[0], program),
935 src_vector(p, &inst->SrcReg[1], program),
936 0);
937 /* tmp = src1 > src2 */
938 i915_emit_arith(p,
939 A0_SLT,
940 tmp,
941 flags, 0,
942 negate(src_vector(p, &inst->SrcReg[0], program),
943 1, 1, 1, 1),
944 negate(src_vector(p, &inst->SrcReg[1], program),
945 1, 1, 1, 1),
946 0);
947 /* dst = tmp || dst */
948 i915_emit_arith(p,
949 A0_ADD,
950 dst,
951 flags | A0_DEST_SATURATE, 0,
952 dst,
953 tmp,
954 0);
955 break;
956
957 case OPCODE_SUB:
958 src0 = src_vector(p, &inst->SrcReg[0], program);
959 src1 = src_vector(p, &inst->SrcReg[1], program);
960
961 i915_emit_arith(p,
962 A0_ADD,
963 get_result_vector(p, inst),
964 get_result_flags(inst), 0,
965 src0, negate(src1, 1, 1, 1, 1), 0);
966 break;
967
968 case OPCODE_SWZ:
969 EMIT_1ARG_ARITH(A0_MOV); /* extended swizzle handled natively */
970 break;
971
972 case OPCODE_TEX:
973 EMIT_TEX(T0_TEXLD);
974 break;
975
976 case OPCODE_TXB:
977 EMIT_TEX(T0_TEXLDB);
978 break;
979
980 case OPCODE_TXP:
981 EMIT_TEX(T0_TEXLDP);
982 break;
983
984 case OPCODE_XPD:
985 /* Cross product:
986 * result.x = src0.y * src1.z - src0.z * src1.y;
987 * result.y = src0.z * src1.x - src0.x * src1.z;
988 * result.z = src0.x * src1.y - src0.y * src1.x;
989 * result.w = undef;
990 */
991 src0 = src_vector(p, &inst->SrcReg[0], program);
992 src1 = src_vector(p, &inst->SrcReg[1], program);
993 tmp = i915_get_utemp(p);
994
995 i915_emit_arith(p,
996 A0_MUL,
997 tmp, A0_DEST_CHANNEL_ALL, 0,
998 swizzle(src0, Z, X, Y, ONE),
999 swizzle(src1, Y, Z, X, ONE), 0);
1000
1001 i915_emit_arith(p,
1002 A0_MAD,
1003 get_result_vector(p, inst),
1004 get_result_flags(inst), 0,
1005 swizzle(src0, Y, Z, X, ONE),
1006 swizzle(src1, Z, X, Y, ONE),
1007 negate(tmp, 1, 1, 1, 0));
1008 break;
1009
1010 case OPCODE_END:
1011 return;
1012
1013 case OPCODE_BGNLOOP:
1014 case OPCODE_BGNSUB:
1015 case OPCODE_BRA:
1016 case OPCODE_BRK:
1017 case OPCODE_CAL:
1018 case OPCODE_CONT:
1019 case OPCODE_DDX:
1020 case OPCODE_DDY:
1021 case OPCODE_ELSE:
1022 case OPCODE_ENDIF:
1023 case OPCODE_ENDLOOP:
1024 case OPCODE_ENDSUB:
1025 case OPCODE_IF:
1026 case OPCODE_RET:
1027 p->error = 1;
1028 i915_program_error(p, "Unsupported opcode: %s",
1029 _mesa_opcode_string(inst->Opcode));
1030 return;
1031
1032 case OPCODE_EXP:
1033 case OPCODE_LOG:
1034 /* These opcodes are claimed as GLSL, NV_vp, and ARB_vp in
1035 * prog_instruction.h, but apparently GLSL doesn't ever emit them.
1036 * Instead, it translates to EX2 or LG2.
1037 */
1038 case OPCODE_TXD:
1039 case OPCODE_TXL:
1040 /* These opcodes are claimed by GLSL in prog_instruction.h, but
1041 * only NV_vp/fp appears to emit them.
1042 */
1043 default:
1044 i915_program_error(p, "bad opcode: %s",
1045 _mesa_opcode_string(inst->Opcode));
1046 return;
1047 }
1048
1049 inst++;
1050 i915_release_utemps(p);
1051 }
1052 }
1053
1054 /* Rather than trying to intercept and jiggle depth writes during
1055 * emit, just move the value into its correct position at the end of
1056 * the program:
1057 */
1058 static void
1059 fixup_depth_write(struct i915_fragment_program *p)
1060 {
1061 if (p->depth_written) {
1062 GLuint depth = UREG(REG_TYPE_OD, 0);
1063
1064 i915_emit_arith(p,
1065 A0_MOV,
1066 depth, A0_DEST_CHANNEL_W, 0,
1067 swizzle(depth, X, Y, Z, Z), 0, 0);
1068 }
1069 }
1070
1071
1072 static void
1073 check_wpos(struct i915_fragment_program *p)
1074 {
1075 GLuint inputs = p->FragProg.Base.InputsRead;
1076 GLint i;
1077
1078 p->wpos_tex = -1;
1079
1080 for (i = 0; i < p->ctx->Const.MaxTextureCoordUnits; i++) {
1081 if (inputs & (FRAG_BIT_TEX(i) | FRAG_BIT_VAR(i)))
1082 continue;
1083 else if (inputs & FRAG_BIT_WPOS) {
1084 p->wpos_tex = i;
1085 inputs &= ~FRAG_BIT_WPOS;
1086 }
1087 }
1088
1089 if (inputs & FRAG_BIT_WPOS) {
1090 i915_program_error(p, "No free texcoord for wpos value");
1091 }
1092 }
1093
1094
1095 static void
1096 translate_program(struct i915_fragment_program *p)
1097 {
1098 struct i915_context *i915 = I915_CONTEXT(p->ctx);
1099
1100 i915_init_program(i915, p);
1101 check_wpos(p);
1102 upload_program(p);
1103 fixup_depth_write(p);
1104 i915_fini_program(p);
1105
1106 p->translated = 1;
1107 }
1108
1109
1110 static void
1111 track_params(struct i915_fragment_program *p)
1112 {
1113 GLint i;
1114
1115 if (p->nr_params)
1116 _mesa_load_state_parameters(p->ctx, p->FragProg.Base.Parameters);
1117
1118 for (i = 0; i < p->nr_params; i++) {
1119 GLint reg = p->param[i].reg;
1120 COPY_4V(p->constant[reg], p->param[i].values);
1121 }
1122
1123 p->params_uptodate = 1;
1124 p->on_hardware = 0; /* overkill */
1125 }
1126
1127
1128 static void
1129 i915BindProgram(GLcontext * ctx, GLenum target, struct gl_program *prog)
1130 {
1131 if (target == GL_FRAGMENT_PROGRAM_ARB) {
1132 struct i915_context *i915 = I915_CONTEXT(ctx);
1133 struct i915_fragment_program *p = (struct i915_fragment_program *) prog;
1134
1135 if (i915->current_program == p)
1136 return;
1137
1138 if (i915->current_program) {
1139 i915->current_program->on_hardware = 0;
1140 i915->current_program->params_uptodate = 0;
1141 }
1142
1143 i915->current_program = p;
1144
1145 assert(p->on_hardware == 0);
1146 assert(p->params_uptodate == 0);
1147
1148 }
1149 }
1150
1151 static struct gl_program *
1152 i915NewProgram(GLcontext * ctx, GLenum target, GLuint id)
1153 {
1154 switch (target) {
1155 case GL_VERTEX_PROGRAM_ARB:
1156 return _mesa_init_vertex_program(ctx, CALLOC_STRUCT(gl_vertex_program),
1157 target, id);
1158
1159 case GL_FRAGMENT_PROGRAM_ARB:{
1160 struct i915_fragment_program *prog =
1161 CALLOC_STRUCT(i915_fragment_program);
1162 if (prog) {
1163 i915_init_program(I915_CONTEXT(ctx), prog);
1164
1165 return _mesa_init_fragment_program(ctx, &prog->FragProg,
1166 target, id);
1167 }
1168 else
1169 return NULL;
1170 }
1171
1172 default:
1173 /* Just fallback:
1174 */
1175 return _mesa_new_program(ctx, target, id);
1176 }
1177 }
1178
1179 static void
1180 i915DeleteProgram(GLcontext * ctx, struct gl_program *prog)
1181 {
1182 if (prog->Target == GL_FRAGMENT_PROGRAM_ARB) {
1183 struct i915_context *i915 = I915_CONTEXT(ctx);
1184 struct i915_fragment_program *p = (struct i915_fragment_program *) prog;
1185
1186 if (i915->current_program == p)
1187 i915->current_program = 0;
1188 }
1189
1190 _mesa_delete_program(ctx, prog);
1191 }
1192
1193
1194 static GLboolean
1195 i915IsProgramNative(GLcontext * ctx, GLenum target, struct gl_program *prog)
1196 {
1197 if (target == GL_FRAGMENT_PROGRAM_ARB) {
1198 struct i915_fragment_program *p = (struct i915_fragment_program *) prog;
1199
1200 if (!p->translated)
1201 translate_program(p);
1202
1203 return !p->error;
1204 }
1205 else
1206 return GL_TRUE;
1207 }
1208
1209 static GLboolean
1210 i915ProgramStringNotify(GLcontext * ctx,
1211 GLenum target, struct gl_program *prog)
1212 {
1213 if (target == GL_FRAGMENT_PROGRAM_ARB) {
1214 struct i915_fragment_program *p = (struct i915_fragment_program *) prog;
1215 p->translated = 0;
1216
1217 /* Hack: make sure fog is correctly enabled according to this
1218 * fragment program's fog options.
1219 */
1220 if (p->FragProg.FogOption) {
1221 /* add extra instructions to do fog, then turn off FogOption field */
1222 _mesa_append_fog_code(ctx, &p->FragProg);
1223 p->FragProg.FogOption = GL_NONE;
1224 }
1225 }
1226
1227 (void) _tnl_program_string(ctx, target, prog);
1228
1229 /* XXX check if program is legal, within limits */
1230 return GL_TRUE;
1231 }
1232
1233 void
1234 i915_update_program(GLcontext *ctx)
1235 {
1236 struct intel_context *intel = intel_context(ctx);
1237 struct i915_context *i915 = i915_context(&intel->ctx);
1238 struct i915_fragment_program *fp =
1239 (struct i915_fragment_program *) ctx->FragmentProgram._Current;
1240
1241 if (i915->current_program != fp) {
1242 if (i915->current_program) {
1243 i915->current_program->on_hardware = 0;
1244 i915->current_program->params_uptodate = 0;
1245 }
1246
1247 i915->current_program = fp;
1248 }
1249
1250 if (!fp->translated)
1251 translate_program(fp);
1252
1253 FALLBACK(&i915->intel, I915_FALLBACK_PROGRAM, fp->error);
1254 }
1255
1256 void
1257 i915ValidateFragmentProgram(struct i915_context *i915)
1258 {
1259 GLcontext *ctx = &i915->intel.ctx;
1260 struct intel_context *intel = intel_context(ctx);
1261 TNLcontext *tnl = TNL_CONTEXT(ctx);
1262 struct vertex_buffer *VB = &tnl->vb;
1263
1264 struct i915_fragment_program *p =
1265 (struct i915_fragment_program *) ctx->FragmentProgram._Current;
1266
1267 const GLuint inputsRead = p->FragProg.Base.InputsRead;
1268 GLuint s4 = i915->state.Ctx[I915_CTXREG_LIS4] & ~S4_VFMT_MASK;
1269 GLuint s2 = S2_TEXCOORD_NONE;
1270 int i, offset = 0;
1271
1272 /* Important:
1273 */
1274 VB->AttribPtr[VERT_ATTRIB_POS] = VB->NdcPtr;
1275
1276 if (!p->translated)
1277 translate_program(p);
1278
1279 intel->vertex_attr_count = 0;
1280 intel->wpos_offset = 0;
1281 intel->wpos_size = 0;
1282 intel->coloroffset = 0;
1283 intel->specoffset = 0;
1284
1285 if (inputsRead & FRAG_BITS_TEX_ANY) {
1286 EMIT_ATTR(_TNL_ATTRIB_POS, EMIT_4F_VIEWPORT, S4_VFMT_XYZW, 16);
1287 }
1288 else {
1289 EMIT_ATTR(_TNL_ATTRIB_POS, EMIT_3F_VIEWPORT, S4_VFMT_XYZ, 12);
1290 }
1291
1292 if (inputsRead & FRAG_BIT_COL0) {
1293 intel->coloroffset = offset / 4;
1294 EMIT_ATTR(_TNL_ATTRIB_COLOR0, EMIT_4UB_4F_BGRA, S4_VFMT_COLOR, 4);
1295 }
1296
1297 if (inputsRead & FRAG_BIT_COL1) {
1298 intel->specoffset = offset / 4;
1299 EMIT_ATTR(_TNL_ATTRIB_COLOR1, EMIT_4UB_4F_BGRA, S4_VFMT_SPEC_FOG, 4);
1300 }
1301
1302 if ((inputsRead & FRAG_BIT_FOGC) || i915->vertex_fog != I915_FOG_NONE) {
1303 EMIT_ATTR(_TNL_ATTRIB_FOG, EMIT_1F, S4_VFMT_FOG_PARAM, 4);
1304 }
1305
1306 for (i = 0; i < p->ctx->Const.MaxTextureCoordUnits; i++) {
1307 if (inputsRead & FRAG_BIT_TEX(i)) {
1308 int sz = VB->AttribPtr[_TNL_ATTRIB_TEX0 + i]->size;
1309
1310 s2 &= ~S2_TEXCOORD_FMT(i, S2_TEXCOORD_FMT0_MASK);
1311 s2 |= S2_TEXCOORD_FMT(i, SZ_TO_HW(sz));
1312
1313 EMIT_ATTR(_TNL_ATTRIB_TEX0 + i, EMIT_SZ(sz), 0, sz * 4);
1314 }
1315 else if (inputsRead & FRAG_BIT_VAR(i)) {
1316 int sz = VB->AttribPtr[_TNL_ATTRIB_GENERIC0 + i]->size;
1317
1318 s2 &= ~S2_TEXCOORD_FMT(i, S2_TEXCOORD_FMT0_MASK);
1319 s2 |= S2_TEXCOORD_FMT(i, SZ_TO_HW(sz));
1320
1321 EMIT_ATTR(_TNL_ATTRIB_GENERIC0 + i, EMIT_SZ(sz), 0, sz * 4);
1322 }
1323 else if (i == p->wpos_tex) {
1324
1325 /* If WPOS is required, duplicate the XYZ position data in an
1326 * unused texture coordinate:
1327 */
1328 s2 &= ~S2_TEXCOORD_FMT(i, S2_TEXCOORD_FMT0_MASK);
1329 s2 |= S2_TEXCOORD_FMT(i, SZ_TO_HW(3));
1330
1331 intel->wpos_offset = offset;
1332 intel->wpos_size = 3 * sizeof(GLuint);
1333
1334 EMIT_PAD(intel->wpos_size);
1335 }
1336 }
1337
1338 if (s2 != i915->state.Ctx[I915_CTXREG_LIS2] ||
1339 s4 != i915->state.Ctx[I915_CTXREG_LIS4]) {
1340 int k;
1341
1342 I915_STATECHANGE(i915, I915_UPLOAD_CTX);
1343
1344 /* Must do this *after* statechange, so as not to affect
1345 * buffered vertices reliant on the old state:
1346 */
1347 intel->vertex_size = _tnl_install_attrs(&intel->ctx,
1348 intel->vertex_attrs,
1349 intel->vertex_attr_count,
1350 intel->ViewportMatrix.m, 0);
1351
1352 intel->vertex_size >>= 2;
1353
1354 i915->state.Ctx[I915_CTXREG_LIS2] = s2;
1355 i915->state.Ctx[I915_CTXREG_LIS4] = s4;
1356
1357 k = intel->vtbl.check_vertex_size(intel, intel->vertex_size);
1358 assert(k);
1359 }
1360
1361 if (!p->params_uptodate)
1362 track_params(p);
1363
1364 if (!p->on_hardware)
1365 i915_upload_program(i915, p);
1366 }
1367
1368 void
1369 i915InitFragProgFuncs(struct dd_function_table *functions)
1370 {
1371 functions->BindProgram = i915BindProgram;
1372 functions->NewProgram = i915NewProgram;
1373 functions->DeleteProgram = i915DeleteProgram;
1374 functions->IsProgramNative = i915IsProgramNative;
1375 functions->ProgramStringNotify = i915ProgramStringNotify;
1376 }