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