a5158de9456f65b13a775b0bd1ecc2880ce8e1bf
[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->Negate)
166 src = negate(src,
167 GET_BIT(source->Negate, 0),
168 GET_BIT(source->Negate, 1),
169 GET_BIT(source->Negate, 2),
170 GET_BIT(source->Negate, 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 /* _mesa_debug_fp_inst(program->Base.NumInstructions, inst); */
327
328 /* Is this a parse-failed program? Ensure a valid program is
329 * loaded, as the flagging of an error isn't sufficient to stop
330 * this being uploaded to hardware.
331 */
332 if (inst[0].Opcode == OPCODE_END) {
333 GLuint tmp = i915_get_utemp(p);
334 i915_emit_arith(p,
335 A0_MOV,
336 UREG(REG_TYPE_OC, 0),
337 A0_DEST_CHANNEL_ALL, 0,
338 swizzle(tmp, ONE, ZERO, ONE, ONE), 0, 0);
339 return;
340 }
341
342 if (program->Base.NumInstructions > I915_MAX_INSN) {
343 i915_program_error( p, "Exceeded max instructions" );
344 return;
345 }
346
347 /* Not always needed:
348 */
349 calc_live_regs(p);
350
351 while (1) {
352 GLuint src0, src1, src2, flags;
353 GLuint tmp = 0, consts0 = 0, consts1 = 0;
354
355 switch (inst->Opcode) {
356 case OPCODE_ABS:
357 src0 = src_vector(p, &inst->SrcReg[0], program);
358 i915_emit_arith(p,
359 A0_MAX,
360 get_result_vector(p, inst),
361 get_result_flags(inst), 0,
362 src0, negate(src0, 1, 1, 1, 1), 0);
363 break;
364
365 case OPCODE_ADD:
366 EMIT_2ARG_ARITH(A0_ADD);
367 break;
368
369 case OPCODE_CMP:
370 src0 = src_vector(p, &inst->SrcReg[0], program);
371 src1 = src_vector(p, &inst->SrcReg[1], program);
372 src2 = src_vector(p, &inst->SrcReg[2], program);
373 i915_emit_arith(p, A0_CMP, get_result_vector(p, inst), get_result_flags(inst), 0, src0, src2, src1); /* NOTE: order of src2, src1 */
374 break;
375
376 case OPCODE_COS:
377 src0 = src_vector(p, &inst->SrcReg[0], program);
378 tmp = i915_get_utemp(p);
379 consts0 = i915_emit_const4fv(p, sin_quad_constants[0]);
380 consts1 = i915_emit_const4fv(p, sin_quad_constants[1]);
381
382 /* Reduce range from repeating about [-pi,pi] to [-1,1] */
383 i915_emit_arith(p,
384 A0_MAD,
385 tmp, A0_DEST_CHANNEL_X, 0,
386 src0,
387 swizzle(consts1, Z, ZERO, ZERO, ZERO), /* 1/(2pi) */
388 swizzle(consts0, W, ZERO, ZERO, ZERO)); /* .75 */
389
390 i915_emit_arith(p, A0_FRC, tmp, A0_DEST_CHANNEL_X, 0, tmp, 0, 0);
391
392 i915_emit_arith(p,
393 A0_MAD,
394 tmp, A0_DEST_CHANNEL_X, 0,
395 tmp,
396 swizzle(consts0, X, ZERO, ZERO, ZERO), /* 2 */
397 swizzle(consts0, Y, ZERO, ZERO, ZERO)); /* -1 */
398
399 /* Compute COS with the same calculation used for SIN, but a
400 * different source range has been mapped to [-1,1] this time.
401 */
402
403 /* tmp.y = abs(tmp.x); {x, abs(x), 0, 0} */
404 i915_emit_arith(p,
405 A0_MAX,
406 tmp, A0_DEST_CHANNEL_Y, 0,
407 swizzle(tmp, ZERO, X, ZERO, ZERO),
408 negate(swizzle(tmp, ZERO, X, ZERO, ZERO), 0, 1, 0, 0),
409 0);
410
411 /* tmp.y = tmp.y * tmp.x; {x, x * abs(x), 0, 0} */
412 i915_emit_arith(p,
413 A0_MUL,
414 tmp, A0_DEST_CHANNEL_Y, 0,
415 swizzle(tmp, ZERO, X, ZERO, ZERO),
416 tmp,
417 0);
418
419 /* tmp.x = tmp.xy DP sin_quad_constants[2].xy */
420 i915_emit_arith(p,
421 A0_DP3,
422 tmp, A0_DEST_CHANNEL_X, 0,
423 tmp,
424 swizzle(consts1, X, Y, ZERO, ZERO),
425 0);
426
427 /* tmp.x now contains a first approximation (y). Now, weight it
428 * against tmp.y**2 to get closer.
429 */
430 i915_emit_arith(p,
431 A0_MAX,
432 tmp, A0_DEST_CHANNEL_Y, 0,
433 swizzle(tmp, ZERO, X, ZERO, ZERO),
434 negate(swizzle(tmp, ZERO, X, ZERO, ZERO), 0, 1, 0, 0),
435 0);
436
437 /* tmp.y = tmp.x * tmp.y - tmp.x; {y, y * abs(y) - y, 0, 0} */
438 i915_emit_arith(p,
439 A0_MAD,
440 tmp, A0_DEST_CHANNEL_Y, 0,
441 swizzle(tmp, ZERO, X, ZERO, ZERO),
442 swizzle(tmp, ZERO, Y, ZERO, ZERO),
443 negate(swizzle(tmp, ZERO, X, ZERO, ZERO), 0, 1, 0, 0));
444
445 /* result = .2225 * tmp.y + tmp.x =.2225(y * abs(y) - y) + y= */
446 i915_emit_arith(p,
447 A0_MAD,
448 get_result_vector(p, inst),
449 get_result_flags(inst), 0,
450 swizzle(consts1, W, W, W, W),
451 swizzle(tmp, Y, Y, Y, Y),
452 swizzle(tmp, X, X, X, X));
453 break;
454
455 case OPCODE_DP3:
456 EMIT_2ARG_ARITH(A0_DP3);
457 break;
458
459 case OPCODE_DP4:
460 EMIT_2ARG_ARITH(A0_DP4);
461 break;
462
463 case OPCODE_DPH:
464 src0 = src_vector(p, &inst->SrcReg[0], program);
465 src1 = src_vector(p, &inst->SrcReg[1], program);
466
467 i915_emit_arith(p,
468 A0_DP4,
469 get_result_vector(p, inst),
470 get_result_flags(inst), 0,
471 swizzle(src0, X, Y, Z, ONE), src1, 0);
472 break;
473
474 case OPCODE_DST:
475 src0 = src_vector(p, &inst->SrcReg[0], program);
476 src1 = src_vector(p, &inst->SrcReg[1], program);
477
478 /* result[0] = 1 * 1;
479 * result[1] = a[1] * b[1];
480 * result[2] = a[2] * 1;
481 * result[3] = 1 * b[3];
482 */
483 i915_emit_arith(p,
484 A0_MUL,
485 get_result_vector(p, inst),
486 get_result_flags(inst), 0,
487 swizzle(src0, ONE, Y, Z, ONE),
488 swizzle(src1, ONE, Y, ONE, W), 0);
489 break;
490
491 case OPCODE_EX2:
492 src0 = src_vector(p, &inst->SrcReg[0], program);
493
494 i915_emit_arith(p,
495 A0_EXP,
496 get_result_vector(p, inst),
497 get_result_flags(inst), 0,
498 swizzle(src0, X, X, X, X), 0, 0);
499 break;
500
501 case OPCODE_FLR:
502 EMIT_1ARG_ARITH(A0_FLR);
503 break;
504
505 case OPCODE_FRC:
506 EMIT_1ARG_ARITH(A0_FRC);
507 break;
508
509 case OPCODE_KIL:
510 src0 = src_vector(p, &inst->SrcReg[0], program);
511 tmp = i915_get_utemp(p);
512
513 i915_emit_texld(p, get_live_regs(p, inst),
514 tmp, A0_DEST_CHANNEL_ALL, /* use a dummy dest reg */
515 0, src0, T0_TEXKILL);
516 break;
517
518 case OPCODE_LG2:
519 src0 = src_vector(p, &inst->SrcReg[0], program);
520
521 i915_emit_arith(p,
522 A0_LOG,
523 get_result_vector(p, inst),
524 get_result_flags(inst), 0,
525 swizzle(src0, X, X, X, X), 0, 0);
526 break;
527
528 case OPCODE_LIT:
529 src0 = src_vector(p, &inst->SrcReg[0], program);
530 tmp = i915_get_utemp(p);
531
532 /* tmp = max( a.xyzw, a.00zw )
533 * XXX: Clamp tmp.w to -128..128
534 * tmp.y = log(tmp.y)
535 * tmp.y = tmp.w * tmp.y
536 * tmp.y = exp(tmp.y)
537 * result = cmp (a.11-x1, a.1x01, a.1xy1 )
538 */
539 i915_emit_arith(p, A0_MAX, tmp, A0_DEST_CHANNEL_ALL, 0,
540 src0, swizzle(src0, ZERO, ZERO, Z, W), 0);
541
542 i915_emit_arith(p, A0_LOG, tmp, A0_DEST_CHANNEL_Y, 0,
543 swizzle(tmp, Y, Y, Y, Y), 0, 0);
544
545 i915_emit_arith(p, A0_MUL, tmp, A0_DEST_CHANNEL_Y, 0,
546 swizzle(tmp, ZERO, Y, ZERO, ZERO),
547 swizzle(tmp, ZERO, W, ZERO, ZERO), 0);
548
549 i915_emit_arith(p, A0_EXP, tmp, A0_DEST_CHANNEL_Y, 0,
550 swizzle(tmp, Y, Y, Y, Y), 0, 0);
551
552 i915_emit_arith(p, A0_CMP,
553 get_result_vector(p, inst),
554 get_result_flags(inst), 0,
555 negate(swizzle(tmp, ONE, ONE, X, ONE), 0, 0, 1, 0),
556 swizzle(tmp, ONE, X, ZERO, ONE),
557 swizzle(tmp, ONE, X, Y, ONE));
558
559 break;
560
561 case OPCODE_LRP:
562 src0 = src_vector(p, &inst->SrcReg[0], program);
563 src1 = src_vector(p, &inst->SrcReg[1], program);
564 src2 = src_vector(p, &inst->SrcReg[2], program);
565 flags = get_result_flags(inst);
566 tmp = i915_get_utemp(p);
567
568 /* b*a + c*(1-a)
569 *
570 * b*a + c - ca
571 *
572 * tmp = b*a + c,
573 * result = (-c)*a + tmp
574 */
575 i915_emit_arith(p, A0_MAD, tmp,
576 flags & A0_DEST_CHANNEL_ALL, 0, src1, src0, src2);
577
578 i915_emit_arith(p, A0_MAD,
579 get_result_vector(p, inst),
580 flags, 0, negate(src2, 1, 1, 1, 1), src0, tmp);
581 break;
582
583 case OPCODE_MAD:
584 EMIT_3ARG_ARITH(A0_MAD);
585 break;
586
587 case OPCODE_MAX:
588 EMIT_2ARG_ARITH(A0_MAX);
589 break;
590
591 case OPCODE_MIN:
592 src0 = src_vector(p, &inst->SrcReg[0], program);
593 src1 = src_vector(p, &inst->SrcReg[1], program);
594 tmp = i915_get_utemp(p);
595 flags = get_result_flags(inst);
596
597 i915_emit_arith(p,
598 A0_MAX,
599 tmp, flags & A0_DEST_CHANNEL_ALL, 0,
600 negate(src0, 1, 1, 1, 1),
601 negate(src1, 1, 1, 1, 1), 0);
602
603 i915_emit_arith(p,
604 A0_MOV,
605 get_result_vector(p, inst),
606 flags, 0, negate(tmp, 1, 1, 1, 1), 0, 0);
607 break;
608
609 case OPCODE_MOV:
610 EMIT_1ARG_ARITH(A0_MOV);
611 break;
612
613 case OPCODE_MUL:
614 EMIT_2ARG_ARITH(A0_MUL);
615 break;
616
617 case OPCODE_POW:
618 src0 = src_vector(p, &inst->SrcReg[0], program);
619 src1 = src_vector(p, &inst->SrcReg[1], program);
620 tmp = i915_get_utemp(p);
621 flags = get_result_flags(inst);
622
623 /* XXX: masking on intermediate values, here and elsewhere.
624 */
625 i915_emit_arith(p,
626 A0_LOG,
627 tmp, A0_DEST_CHANNEL_X, 0,
628 swizzle(src0, X, X, X, X), 0, 0);
629
630 i915_emit_arith(p, A0_MUL, tmp, A0_DEST_CHANNEL_X, 0, tmp, src1, 0);
631
632
633 i915_emit_arith(p,
634 A0_EXP,
635 get_result_vector(p, inst),
636 flags, 0, swizzle(tmp, X, X, X, X), 0, 0);
637
638 break;
639
640 case OPCODE_RCP:
641 src0 = src_vector(p, &inst->SrcReg[0], program);
642
643 i915_emit_arith(p,
644 A0_RCP,
645 get_result_vector(p, inst),
646 get_result_flags(inst), 0,
647 swizzle(src0, X, X, X, X), 0, 0);
648 break;
649
650 case OPCODE_RSQ:
651
652 src0 = src_vector(p, &inst->SrcReg[0], program);
653
654 i915_emit_arith(p,
655 A0_RSQ,
656 get_result_vector(p, inst),
657 get_result_flags(inst), 0,
658 swizzle(src0, X, X, X, X), 0, 0);
659 break;
660
661 case OPCODE_SCS:
662 src0 = src_vector(p, &inst->SrcReg[0], program);
663 tmp = i915_get_utemp(p);
664
665 /*
666 * t0.xy = MUL x.xx11, x.x1111 ; x^2, x, 1, 1
667 * t0 = MUL t0.xyxy t0.xx11 ; x^4, x^3, x^2, x
668 * t1 = MUL t0.xyyw t0.yz11 ; x^7 x^5 x^3 x
669 * scs.x = DP4 t1, sin_constants
670 * t1 = MUL t0.xxz1 t0.z111 ; x^6 x^4 x^2 1
671 * scs.y = DP4 t1, cos_constants
672 */
673 i915_emit_arith(p,
674 A0_MUL,
675 tmp, A0_DEST_CHANNEL_XY, 0,
676 swizzle(src0, X, X, ONE, ONE),
677 swizzle(src0, X, ONE, ONE, ONE), 0);
678
679 i915_emit_arith(p,
680 A0_MUL,
681 tmp, A0_DEST_CHANNEL_ALL, 0,
682 swizzle(tmp, X, Y, X, Y),
683 swizzle(tmp, X, X, ONE, ONE), 0);
684
685 if (inst->DstReg.WriteMask & WRITEMASK_Y) {
686 GLuint tmp1;
687
688 if (inst->DstReg.WriteMask & WRITEMASK_X)
689 tmp1 = i915_get_utemp(p);
690 else
691 tmp1 = tmp;
692
693 i915_emit_arith(p,
694 A0_MUL,
695 tmp1, A0_DEST_CHANNEL_ALL, 0,
696 swizzle(tmp, X, Y, Y, W),
697 swizzle(tmp, X, Z, ONE, ONE), 0);
698
699 i915_emit_arith(p,
700 A0_DP4,
701 get_result_vector(p, inst),
702 A0_DEST_CHANNEL_Y, 0,
703 swizzle(tmp1, W, Z, Y, X),
704 i915_emit_const4fv(p, sin_constants), 0);
705 }
706
707 if (inst->DstReg.WriteMask & WRITEMASK_X) {
708 i915_emit_arith(p,
709 A0_MUL,
710 tmp, A0_DEST_CHANNEL_XYZ, 0,
711 swizzle(tmp, X, X, Z, ONE),
712 swizzle(tmp, Z, ONE, ONE, ONE), 0);
713
714 i915_emit_arith(p,
715 A0_DP4,
716 get_result_vector(p, inst),
717 A0_DEST_CHANNEL_X, 0,
718 swizzle(tmp, ONE, Z, Y, X),
719 i915_emit_const4fv(p, cos_constants), 0);
720 }
721 break;
722
723 case OPCODE_SGE:
724 EMIT_2ARG_ARITH(A0_SGE);
725 break;
726
727 case OPCODE_SIN:
728 src0 = src_vector(p, &inst->SrcReg[0], program);
729 tmp = i915_get_utemp(p);
730 consts0 = i915_emit_const4fv(p, sin_quad_constants[0]);
731 consts1 = i915_emit_const4fv(p, sin_quad_constants[1]);
732
733 /* Reduce range from repeating about [-pi,pi] to [-1,1] */
734 i915_emit_arith(p,
735 A0_MAD,
736 tmp, A0_DEST_CHANNEL_X, 0,
737 src0,
738 swizzle(consts1, Z, ZERO, ZERO, ZERO), /* 1/(2pi) */
739 swizzle(consts0, Z, ZERO, ZERO, ZERO)); /* .5 */
740
741 i915_emit_arith(p, A0_FRC, tmp, A0_DEST_CHANNEL_X, 0, tmp, 0, 0);
742
743 i915_emit_arith(p,
744 A0_MAD,
745 tmp, A0_DEST_CHANNEL_X, 0,
746 tmp,
747 swizzle(consts0, X, ZERO, ZERO, ZERO), /* 2 */
748 swizzle(consts0, Y, ZERO, ZERO, ZERO)); /* -1 */
749
750 /* Compute sin using a quadratic and quartic. It gives continuity
751 * that repeating the Taylor series lacks every 2*pi, and has
752 * reduced error.
753 *
754 * The idea was described at:
755 * http://www.devmaster.net/forums/showthread.php?t=5784
756 */
757
758 /* tmp.y = abs(tmp.x); {x, abs(x), 0, 0} */
759 i915_emit_arith(p,
760 A0_MAX,
761 tmp, A0_DEST_CHANNEL_Y, 0,
762 swizzle(tmp, ZERO, X, ZERO, ZERO),
763 negate(swizzle(tmp, ZERO, X, ZERO, ZERO), 0, 1, 0, 0),
764 0);
765
766 /* tmp.y = tmp.y * tmp.x; {x, x * abs(x), 0, 0} */
767 i915_emit_arith(p,
768 A0_MUL,
769 tmp, A0_DEST_CHANNEL_Y, 0,
770 swizzle(tmp, ZERO, X, ZERO, ZERO),
771 tmp,
772 0);
773
774 /* tmp.x = tmp.xy DP sin_quad_constants[2].xy */
775 i915_emit_arith(p,
776 A0_DP3,
777 tmp, A0_DEST_CHANNEL_X, 0,
778 tmp,
779 swizzle(consts1, X, Y, ZERO, ZERO),
780 0);
781
782 /* tmp.x now contains a first approximation (y). Now, weight it
783 * against tmp.y**2 to get closer.
784 */
785 i915_emit_arith(p,
786 A0_MAX,
787 tmp, A0_DEST_CHANNEL_Y, 0,
788 swizzle(tmp, ZERO, X, ZERO, ZERO),
789 negate(swizzle(tmp, ZERO, X, ZERO, ZERO), 0, 1, 0, 0),
790 0);
791
792 /* tmp.y = tmp.x * tmp.y - tmp.x; {y, y * abs(y) - y, 0, 0} */
793 i915_emit_arith(p,
794 A0_MAD,
795 tmp, A0_DEST_CHANNEL_Y, 0,
796 swizzle(tmp, ZERO, X, ZERO, ZERO),
797 swizzle(tmp, ZERO, Y, ZERO, ZERO),
798 negate(swizzle(tmp, ZERO, X, ZERO, ZERO), 0, 1, 0, 0));
799
800 /* result = .2225 * tmp.y + tmp.x =.2225(y * abs(y) - y) + y= */
801 i915_emit_arith(p,
802 A0_MAD,
803 get_result_vector(p, inst),
804 get_result_flags(inst), 0,
805 swizzle(consts1, W, W, W, W),
806 swizzle(tmp, Y, Y, Y, Y),
807 swizzle(tmp, X, X, X, X));
808
809 break;
810
811 case OPCODE_SLT:
812 EMIT_2ARG_ARITH(A0_SLT);
813 break;
814
815 case OPCODE_SUB:
816 src0 = src_vector(p, &inst->SrcReg[0], program);
817 src1 = src_vector(p, &inst->SrcReg[1], program);
818
819 i915_emit_arith(p,
820 A0_ADD,
821 get_result_vector(p, inst),
822 get_result_flags(inst), 0,
823 src0, negate(src1, 1, 1, 1, 1), 0);
824 break;
825
826 case OPCODE_SWZ:
827 EMIT_1ARG_ARITH(A0_MOV); /* extended swizzle handled natively */
828 break;
829
830 case OPCODE_TEX:
831 EMIT_TEX(T0_TEXLD);
832 break;
833
834 case OPCODE_TXB:
835 EMIT_TEX(T0_TEXLDB);
836 break;
837
838 case OPCODE_TXP:
839 EMIT_TEX(T0_TEXLDP);
840 break;
841
842 case OPCODE_XPD:
843 /* Cross product:
844 * result.x = src0.y * src1.z - src0.z * src1.y;
845 * result.y = src0.z * src1.x - src0.x * src1.z;
846 * result.z = src0.x * src1.y - src0.y * src1.x;
847 * result.w = undef;
848 */
849 src0 = src_vector(p, &inst->SrcReg[0], program);
850 src1 = src_vector(p, &inst->SrcReg[1], program);
851 tmp = i915_get_utemp(p);
852
853 i915_emit_arith(p,
854 A0_MUL,
855 tmp, A0_DEST_CHANNEL_ALL, 0,
856 swizzle(src0, Z, X, Y, ONE),
857 swizzle(src1, Y, Z, X, ONE), 0);
858
859 i915_emit_arith(p,
860 A0_MAD,
861 get_result_vector(p, inst),
862 get_result_flags(inst), 0,
863 swizzle(src0, Y, Z, X, ONE),
864 swizzle(src1, Z, X, Y, ONE),
865 negate(tmp, 1, 1, 1, 0));
866 break;
867
868 case OPCODE_END:
869 return;
870
871 default:
872 i915_program_error(p, "bad opcode");
873 return;
874 }
875
876 inst++;
877 i915_release_utemps(p);
878 }
879 }
880
881 /* Rather than trying to intercept and jiggle depth writes during
882 * emit, just move the value into its correct position at the end of
883 * the program:
884 */
885 static void
886 fixup_depth_write(struct i915_fragment_program *p)
887 {
888 if (p->depth_written) {
889 GLuint depth = UREG(REG_TYPE_OD, 0);
890
891 i915_emit_arith(p,
892 A0_MOV,
893 depth, A0_DEST_CHANNEL_W, 0,
894 swizzle(depth, X, Y, Z, Z), 0, 0);
895 }
896 }
897
898
899 static void
900 check_wpos(struct i915_fragment_program *p)
901 {
902 GLuint inputs = p->FragProg.Base.InputsRead;
903 GLint i;
904
905 p->wpos_tex = -1;
906
907 for (i = 0; i < p->ctx->Const.MaxTextureCoordUnits; i++) {
908 if (inputs & FRAG_BIT_TEX(i))
909 continue;
910 else if (inputs & FRAG_BIT_WPOS) {
911 p->wpos_tex = i;
912 inputs &= ~FRAG_BIT_WPOS;
913 }
914 }
915
916 if (inputs & FRAG_BIT_WPOS) {
917 i915_program_error(p, "No free texcoord for wpos value");
918 }
919 }
920
921
922 static void
923 translate_program(struct i915_fragment_program *p)
924 {
925 struct i915_context *i915 = I915_CONTEXT(p->ctx);
926
927 i915_init_program(i915, p);
928 check_wpos(p);
929 upload_program(p);
930 fixup_depth_write(p);
931 i915_fini_program(p);
932
933 p->translated = 1;
934 }
935
936
937 static void
938 track_params(struct i915_fragment_program *p)
939 {
940 GLint i;
941
942 if (p->nr_params)
943 _mesa_load_state_parameters(p->ctx, p->FragProg.Base.Parameters);
944
945 for (i = 0; i < p->nr_params; i++) {
946 GLint reg = p->param[i].reg;
947 COPY_4V(p->constant[reg], p->param[i].values);
948 }
949
950 p->params_uptodate = 1;
951 p->on_hardware = 0; /* overkill */
952 }
953
954
955 static void
956 i915BindProgram(GLcontext * ctx, GLenum target, struct gl_program *prog)
957 {
958 if (target == GL_FRAGMENT_PROGRAM_ARB) {
959 struct i915_context *i915 = I915_CONTEXT(ctx);
960 struct i915_fragment_program *p = (struct i915_fragment_program *) prog;
961
962 if (i915->current_program == p)
963 return;
964
965 if (i915->current_program) {
966 i915->current_program->on_hardware = 0;
967 i915->current_program->params_uptodate = 0;
968 }
969
970 i915->current_program = p;
971
972 assert(p->on_hardware == 0);
973 assert(p->params_uptodate == 0);
974
975 }
976 }
977
978 static struct gl_program *
979 i915NewProgram(GLcontext * ctx, GLenum target, GLuint id)
980 {
981 switch (target) {
982 case GL_VERTEX_PROGRAM_ARB:
983 return _mesa_init_vertex_program(ctx, CALLOC_STRUCT(gl_vertex_program),
984 target, id);
985
986 case GL_FRAGMENT_PROGRAM_ARB:{
987 struct i915_fragment_program *prog =
988 CALLOC_STRUCT(i915_fragment_program);
989 if (prog) {
990 i915_init_program(I915_CONTEXT(ctx), prog);
991
992 return _mesa_init_fragment_program(ctx, &prog->FragProg,
993 target, id);
994 }
995 else
996 return NULL;
997 }
998
999 default:
1000 /* Just fallback:
1001 */
1002 return _mesa_new_program(ctx, target, id);
1003 }
1004 }
1005
1006 static void
1007 i915DeleteProgram(GLcontext * ctx, struct gl_program *prog)
1008 {
1009 if (prog->Target == GL_FRAGMENT_PROGRAM_ARB) {
1010 struct i915_context *i915 = I915_CONTEXT(ctx);
1011 struct i915_fragment_program *p = (struct i915_fragment_program *) prog;
1012
1013 if (i915->current_program == p)
1014 i915->current_program = 0;
1015 }
1016
1017 _mesa_delete_program(ctx, prog);
1018 }
1019
1020
1021 static GLboolean
1022 i915IsProgramNative(GLcontext * ctx, GLenum target, struct gl_program *prog)
1023 {
1024 if (target == GL_FRAGMENT_PROGRAM_ARB) {
1025 struct i915_fragment_program *p = (struct i915_fragment_program *) prog;
1026
1027 if (!p->translated)
1028 translate_program(p);
1029
1030 return !p->error;
1031 }
1032 else
1033 return GL_TRUE;
1034 }
1035
1036 static void
1037 i915ProgramStringNotify(GLcontext * ctx,
1038 GLenum target, struct gl_program *prog)
1039 {
1040 if (target == GL_FRAGMENT_PROGRAM_ARB) {
1041 struct i915_fragment_program *p = (struct i915_fragment_program *) prog;
1042 p->translated = 0;
1043
1044 /* Hack: make sure fog is correctly enabled according to this
1045 * fragment program's fog options.
1046 */
1047 if (p->FragProg.FogOption) {
1048 /* add extra instructions to do fog, then turn off FogOption field */
1049 _mesa_append_fog_code(ctx, &p->FragProg);
1050 p->FragProg.FogOption = GL_NONE;
1051 }
1052
1053 if (INTEL_DEBUG & DEBUG_STATE)
1054 _mesa_print_program(prog);
1055 }
1056
1057 _tnl_program_string(ctx, target, prog);
1058 }
1059
1060
1061 void
1062 i915ValidateFragmentProgram(struct i915_context *i915)
1063 {
1064 GLcontext *ctx = &i915->intel.ctx;
1065 struct intel_context *intel = intel_context(ctx);
1066 TNLcontext *tnl = TNL_CONTEXT(ctx);
1067 struct vertex_buffer *VB = &tnl->vb;
1068
1069 struct i915_fragment_program *p =
1070 (struct i915_fragment_program *) ctx->FragmentProgram._Current;
1071
1072 const GLuint inputsRead = p->FragProg.Base.InputsRead;
1073 GLuint s4 = i915->state.Ctx[I915_CTXREG_LIS4] & ~S4_VFMT_MASK;
1074 GLuint s2 = S2_TEXCOORD_NONE;
1075 int i, offset = 0;
1076
1077 if (i915->current_program != p) {
1078 if (i915->current_program) {
1079 i915->current_program->on_hardware = 0;
1080 i915->current_program->params_uptodate = 0;
1081 }
1082
1083 i915->current_program = p;
1084 }
1085
1086
1087 /* Important:
1088 */
1089 VB->AttribPtr[VERT_ATTRIB_POS] = VB->NdcPtr;
1090
1091 if (!p->translated)
1092 translate_program(p);
1093
1094 intel->vertex_attr_count = 0;
1095 intel->wpos_offset = 0;
1096 intel->wpos_size = 0;
1097 intel->coloroffset = 0;
1098 intel->specoffset = 0;
1099
1100 if (inputsRead & FRAG_BITS_TEX_ANY) {
1101 EMIT_ATTR(_TNL_ATTRIB_POS, EMIT_4F_VIEWPORT, S4_VFMT_XYZW, 16);
1102 }
1103 else {
1104 EMIT_ATTR(_TNL_ATTRIB_POS, EMIT_3F_VIEWPORT, S4_VFMT_XYZ, 12);
1105 }
1106
1107 if (inputsRead & FRAG_BIT_COL0) {
1108 intel->coloroffset = offset / 4;
1109 EMIT_ATTR(_TNL_ATTRIB_COLOR0, EMIT_4UB_4F_BGRA, S4_VFMT_COLOR, 4);
1110 }
1111
1112 if (inputsRead & FRAG_BIT_COL1) {
1113 intel->specoffset = offset / 4;
1114 EMIT_ATTR(_TNL_ATTRIB_COLOR1, EMIT_4UB_4F_BGRA, S4_VFMT_SPEC_FOG, 4);
1115 }
1116
1117 if ((inputsRead & FRAG_BIT_FOGC) || i915->vertex_fog != I915_FOG_NONE) {
1118 EMIT_ATTR(_TNL_ATTRIB_FOG, EMIT_1F, S4_VFMT_FOG_PARAM, 4);
1119 }
1120
1121 for (i = 0; i < p->ctx->Const.MaxTextureCoordUnits; i++) {
1122 if (inputsRead & FRAG_BIT_TEX(i)) {
1123 int sz = VB->TexCoordPtr[i]->size;
1124
1125 s2 &= ~S2_TEXCOORD_FMT(i, S2_TEXCOORD_FMT0_MASK);
1126 s2 |= S2_TEXCOORD_FMT(i, SZ_TO_HW(sz));
1127
1128 EMIT_ATTR(_TNL_ATTRIB_TEX0 + i, EMIT_SZ(sz), 0, sz * 4);
1129 }
1130 else if (i == p->wpos_tex) {
1131
1132 /* If WPOS is required, duplicate the XYZ position data in an
1133 * unused texture coordinate:
1134 */
1135 s2 &= ~S2_TEXCOORD_FMT(i, S2_TEXCOORD_FMT0_MASK);
1136 s2 |= S2_TEXCOORD_FMT(i, SZ_TO_HW(3));
1137
1138 intel->wpos_offset = offset;
1139 intel->wpos_size = 3 * sizeof(GLuint);
1140
1141 EMIT_PAD(intel->wpos_size);
1142 }
1143 }
1144
1145 if (s2 != i915->state.Ctx[I915_CTXREG_LIS2] ||
1146 s4 != i915->state.Ctx[I915_CTXREG_LIS4]) {
1147 int k;
1148
1149 I915_STATECHANGE(i915, I915_UPLOAD_CTX);
1150
1151 /* Must do this *after* statechange, so as not to affect
1152 * buffered vertices reliant on the old state:
1153 */
1154 intel->vertex_size = _tnl_install_attrs(&intel->ctx,
1155 intel->vertex_attrs,
1156 intel->vertex_attr_count,
1157 intel->ViewportMatrix.m, 0);
1158
1159 intel->vertex_size >>= 2;
1160
1161 i915->state.Ctx[I915_CTXREG_LIS2] = s2;
1162 i915->state.Ctx[I915_CTXREG_LIS4] = s4;
1163
1164 k = intel->vtbl.check_vertex_size(intel, intel->vertex_size);
1165 assert(k);
1166 }
1167
1168 if (!p->params_uptodate)
1169 track_params(p);
1170
1171 if (!p->on_hardware)
1172 i915_upload_program(i915, p);
1173 }
1174
1175 void
1176 i915InitFragProgFuncs(struct dd_function_table *functions)
1177 {
1178 functions->BindProgram = i915BindProgram;
1179 functions->NewProgram = i915NewProgram;
1180 functions->DeleteProgram = i915DeleteProgram;
1181 functions->IsProgramNative = i915IsProgramNative;
1182 functions->ProgramStringNotify = i915ProgramStringNotify;
1183 }