i915g: Fix comment about sin/cos constants.
[mesa.git] / src / gallium / drivers / i915 / i915_fpc_translate.c
1 /**************************************************************************
2 *
3 * Copyright 2007 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
29 #include <stdarg.h>
30
31 #include "i915_reg.h"
32 #include "i915_context.h"
33 #include "i915_fpc.h"
34
35 #include "pipe/p_shader_tokens.h"
36 #include "util/u_math.h"
37 #include "util/u_memory.h"
38 #include "util/u_string.h"
39 #include "tgsi/tgsi_parse.h"
40 #include "tgsi/tgsi_dump.h"
41
42 #include "draw/draw_vertex.h"
43
44
45 /**
46 * Simple pass-through fragment shader to use when we don't have
47 * a real shader (or it fails to compile for some reason).
48 */
49 static unsigned passthrough[] =
50 {
51 _3DSTATE_PIXEL_SHADER_PROGRAM | ((2*3)-1),
52
53 /* declare input color:
54 */
55 (D0_DCL |
56 (REG_TYPE_T << D0_TYPE_SHIFT) |
57 (T_DIFFUSE << D0_NR_SHIFT) |
58 D0_CHANNEL_ALL),
59 0,
60 0,
61
62 /* move to output color:
63 */
64 (A0_MOV |
65 (REG_TYPE_OC << A0_DEST_TYPE_SHIFT) |
66 A0_DEST_CHANNEL_ALL |
67 (REG_TYPE_T << A0_SRC0_TYPE_SHIFT) |
68 (T_DIFFUSE << A0_SRC0_NR_SHIFT)),
69 0x01230000, /* .xyzw */
70 0
71 };
72
73
74 /* 1, -1/3!, 1/5!, -1/7! */
75 static const float scs_sin_constants[4] = { 1.0,
76 -1.0f / (3 * 2 * 1),
77 1.0f / (5 * 4 * 3 * 2 * 1),
78 -1.0f / (7 * 6 * 5 * 4 * 3 * 2 * 1)
79 };
80
81 /* 1, -1/2!, 1/4!, -1/6! */
82 static const float scs_cos_constants[4] = { 1.0,
83 -1.0f / (2 * 1),
84 1.0f / (4 * 3 * 2 * 1),
85 -1.0f / (6 * 5 * 4 * 3 * 2 * 1)
86 };
87
88 /* 2*pi, -(2*pi)^3/3!, (2*pi)^5/5!, -(2*pi)^7/7! */
89 static const float sin_constants[4] = { 2.0 * M_PI,
90 -8.0f * M_PI * M_PI * M_PI / (3 * 2 * 1),
91 32.0f * M_PI * M_PI * M_PI * M_PI * M_PI / (5 * 4 * 3 * 2 * 1),
92 -128.0f * M_PI * M_PI * M_PI * M_PI * M_PI * M_PI * M_PI / (7 * 6 * 5 * 4 * 3 * 2 * 1)
93 };
94
95 /* 1, -(2*pi)^2/2!, (2*pi)^4/4!, -(2*pi)^6/6! */
96 static const float cos_constants[4] = { 1.0,
97 -4.0f * M_PI * M_PI / (2 * 1),
98 16.0f * M_PI * M_PI * M_PI * M_PI / (4 * 3 * 2 * 1),
99 -64.0f * M_PI * M_PI * M_PI * M_PI * M_PI * M_PI / (6 * 5 * 4 * 3 * 2 * 1)
100 };
101
102
103
104 /**
105 * component-wise negation of ureg
106 */
107 static INLINE int
108 negate(int reg, int x, int y, int z, int w)
109 {
110 /* Another neat thing about the UREG representation */
111 return reg ^ (((x & 1) << UREG_CHANNEL_X_NEGATE_SHIFT) |
112 ((y & 1) << UREG_CHANNEL_Y_NEGATE_SHIFT) |
113 ((z & 1) << UREG_CHANNEL_Z_NEGATE_SHIFT) |
114 ((w & 1) << UREG_CHANNEL_W_NEGATE_SHIFT));
115 }
116
117
118 /**
119 * In the event of a translation failure, we'll generate a simple color
120 * pass-through program.
121 */
122 static void
123 i915_use_passthrough_shader(struct i915_fragment_shader *fs)
124 {
125 fs->program = (uint *) MALLOC(sizeof(passthrough));
126 if (fs->program) {
127 memcpy(fs->program, passthrough, sizeof(passthrough));
128 fs->program_len = Elements(passthrough);
129 }
130 fs->num_constants = 0;
131 }
132
133
134 void
135 i915_program_error(struct i915_fp_compile *p, const char *msg, ...)
136 {
137 va_list args;
138 char buffer[1024];
139
140 debug_printf("i915_program_error: ");
141 va_start( args, msg );
142 util_vsnprintf( buffer, sizeof(buffer), msg, args );
143 va_end( args );
144 debug_printf("%s", buffer);
145 debug_printf("\n");
146
147 p->error = 1;
148 }
149
150 static uint get_mapping(struct i915_fragment_shader* fs, int unit)
151 {
152 int i;
153 for (i = 0; i < I915_TEX_UNITS; i++)
154 {
155 if (fs->generic_mapping[i] == -1) {
156 fs->generic_mapping[i] = unit;
157 return i;
158 }
159 if (fs->generic_mapping[i] == unit)
160 return i;
161 }
162 debug_printf("Exceeded max generics\n");
163 return 0;
164 }
165
166 /**
167 * Construct a ureg for the given source register. Will emit
168 * constants, apply swizzling and negation as needed.
169 */
170 static uint
171 src_vector(struct i915_fp_compile *p,
172 const struct tgsi_full_src_register *source,
173 struct i915_fragment_shader* fs)
174 {
175 uint index = source->Register.Index;
176 uint src = 0, sem_name, sem_ind;
177
178 switch (source->Register.File) {
179 case TGSI_FILE_TEMPORARY:
180 if (source->Register.Index >= I915_MAX_TEMPORARY) {
181 i915_program_error(p, "Exceeded max temporary reg");
182 return 0;
183 }
184 src = UREG(REG_TYPE_R, index);
185 break;
186 case TGSI_FILE_INPUT:
187 /* XXX: Packing COL1, FOGC into a single attribute works for
188 * texenv programs, but will fail for real fragment programs
189 * that use these attributes and expect them to be a full 4
190 * components wide. Could use a texcoord to pass these
191 * attributes if necessary, but that won't work in the general
192 * case.
193 *
194 * We also use a texture coordinate to pass wpos when possible.
195 */
196
197 sem_name = p->shader->info.input_semantic_name[index];
198 sem_ind = p->shader->info.input_semantic_index[index];
199
200 switch (sem_name) {
201 case TGSI_SEMANTIC_POSITION:
202 {
203 /* for fragcoord */
204 int real_tex_unit = get_mapping(fs, I915_SEMANTIC_POS);
205 src = i915_emit_decl(p, REG_TYPE_T, T_TEX0 + real_tex_unit, D0_CHANNEL_ALL);
206 break;
207 }
208 case TGSI_SEMANTIC_COLOR:
209 if (sem_ind == 0) {
210 src = i915_emit_decl(p, REG_TYPE_T, T_DIFFUSE, D0_CHANNEL_ALL);
211 }
212 else {
213 /* secondary color */
214 assert(sem_ind == 1);
215 src = i915_emit_decl(p, REG_TYPE_T, T_SPECULAR, D0_CHANNEL_XYZ);
216 src = swizzle(src, X, Y, Z, ONE);
217 }
218 break;
219 case TGSI_SEMANTIC_FOG:
220 src = i915_emit_decl(p, REG_TYPE_T, T_FOG_W, D0_CHANNEL_W);
221 src = swizzle(src, W, W, W, W);
222 break;
223 case TGSI_SEMANTIC_GENERIC:
224 {
225 int real_tex_unit = get_mapping(fs, sem_ind);
226 src = i915_emit_decl(p, REG_TYPE_T, T_TEX0 + real_tex_unit, D0_CHANNEL_ALL);
227 break;
228 }
229 case TGSI_SEMANTIC_FACE:
230 {
231 /* for back/front faces */
232 int real_tex_unit = get_mapping(fs, I915_SEMANTIC_FACE);
233 src = i915_emit_decl(p, REG_TYPE_T, T_TEX0 + real_tex_unit, D0_CHANNEL_X);
234 break;
235 }
236 default:
237 i915_program_error(p, "Bad source->Index");
238 return 0;
239 }
240 break;
241
242 case TGSI_FILE_IMMEDIATE:
243 assert(index < p->num_immediates);
244 index = p->immediates_map[index];
245 /* fall-through */
246 case TGSI_FILE_CONSTANT:
247 src = UREG(REG_TYPE_CONST, index);
248 break;
249
250 default:
251 i915_program_error(p, "Bad source->File");
252 return 0;
253 }
254
255 src = swizzle(src,
256 source->Register.SwizzleX,
257 source->Register.SwizzleY,
258 source->Register.SwizzleZ,
259 source->Register.SwizzleW);
260
261 /* There's both negate-all-components and per-component negation.
262 * Try to handle both here.
263 */
264 {
265 int n = source->Register.Negate;
266 src = negate(src, n, n, n, n);
267 }
268
269 /* no abs() */
270 #if 0
271 /* XXX assertions disabled to allow arbfplight.c to run */
272 /* XXX enable these assertions, or fix things */
273 assert(!source->Register.Absolute);
274 #endif
275 if (source->Register.Absolute)
276 debug_printf("Exceeded max generics\n");
277
278 return src;
279 }
280
281
282 /**
283 * Construct a ureg for a destination register.
284 */
285 static uint
286 get_result_vector(struct i915_fp_compile *p,
287 const struct tgsi_full_dst_register *dest)
288 {
289 switch (dest->Register.File) {
290 case TGSI_FILE_OUTPUT:
291 {
292 uint sem_name = p->shader->info.output_semantic_name[dest->Register.Index];
293 switch (sem_name) {
294 case TGSI_SEMANTIC_POSITION:
295 return UREG(REG_TYPE_OD, 0);
296 case TGSI_SEMANTIC_COLOR:
297 return UREG(REG_TYPE_OC, 0);
298 default:
299 i915_program_error(p, "Bad inst->DstReg.Index/semantics");
300 return 0;
301 }
302 }
303 case TGSI_FILE_TEMPORARY:
304 return UREG(REG_TYPE_R, dest->Register.Index);
305 default:
306 i915_program_error(p, "Bad inst->DstReg.File");
307 return 0;
308 }
309 }
310
311
312 /**
313 * Compute flags for saturation and writemask.
314 */
315 static uint
316 get_result_flags(const struct tgsi_full_instruction *inst)
317 {
318 const uint writeMask
319 = inst->Dst[0].Register.WriteMask;
320 uint flags = 0x0;
321
322 if (inst->Instruction.Saturate == TGSI_SAT_ZERO_ONE)
323 flags |= A0_DEST_SATURATE;
324
325 if (writeMask & TGSI_WRITEMASK_X)
326 flags |= A0_DEST_CHANNEL_X;
327 if (writeMask & TGSI_WRITEMASK_Y)
328 flags |= A0_DEST_CHANNEL_Y;
329 if (writeMask & TGSI_WRITEMASK_Z)
330 flags |= A0_DEST_CHANNEL_Z;
331 if (writeMask & TGSI_WRITEMASK_W)
332 flags |= A0_DEST_CHANNEL_W;
333
334 return flags;
335 }
336
337
338 /**
339 * Convert TGSI_TEXTURE_x token to DO_SAMPLE_TYPE_x token
340 */
341 static uint
342 translate_tex_src_target(struct i915_fp_compile *p, uint tex)
343 {
344 switch (tex) {
345 case TGSI_TEXTURE_SHADOW1D:
346 /* fall-through */
347 case TGSI_TEXTURE_1D:
348 return D0_SAMPLE_TYPE_2D;
349
350 case TGSI_TEXTURE_SHADOW2D:
351 /* fall-through */
352 case TGSI_TEXTURE_2D:
353 return D0_SAMPLE_TYPE_2D;
354
355 case TGSI_TEXTURE_SHADOWRECT:
356 /* fall-through */
357 case TGSI_TEXTURE_RECT:
358 return D0_SAMPLE_TYPE_2D;
359
360 case TGSI_TEXTURE_3D:
361 return D0_SAMPLE_TYPE_VOLUME;
362
363 case TGSI_TEXTURE_CUBE:
364 return D0_SAMPLE_TYPE_CUBE;
365
366 default:
367 i915_program_error(p, "TexSrc type");
368 return 0;
369 }
370 }
371
372
373 /**
374 * Generate texel lookup instruction.
375 */
376 static void
377 emit_tex(struct i915_fp_compile *p,
378 const struct tgsi_full_instruction *inst,
379 uint opcode,
380 struct i915_fragment_shader* fs)
381 {
382 uint texture = inst->Texture.Texture;
383 uint unit = inst->Src[1].Register.Index;
384 uint tex = translate_tex_src_target( p, texture );
385 uint sampler = i915_emit_decl(p, REG_TYPE_S, unit, tex);
386 uint coord = src_vector( p, &inst->Src[0], fs);
387
388 i915_emit_texld( p,
389 get_result_vector( p, &inst->Dst[0] ),
390 get_result_flags( inst ),
391 sampler,
392 coord,
393 opcode);
394 }
395
396
397 /**
398 * Generate a simple arithmetic instruction
399 * \param opcode the i915 opcode
400 * \param numArgs the number of input/src arguments
401 */
402 static void
403 emit_simple_arith(struct i915_fp_compile *p,
404 const struct tgsi_full_instruction *inst,
405 uint opcode, uint numArgs,
406 struct i915_fragment_shader* fs)
407 {
408 uint arg1, arg2, arg3;
409
410 assert(numArgs <= 3);
411
412 arg1 = (numArgs < 1) ? 0 : src_vector( p, &inst->Src[0], fs );
413 arg2 = (numArgs < 2) ? 0 : src_vector( p, &inst->Src[1], fs );
414 arg3 = (numArgs < 3) ? 0 : src_vector( p, &inst->Src[2], fs );
415
416 i915_emit_arith( p,
417 opcode,
418 get_result_vector( p, &inst->Dst[0]),
419 get_result_flags( inst ), 0,
420 arg1,
421 arg2,
422 arg3 );
423 }
424
425
426 /** As above, but swap the first two src regs */
427 static void
428 emit_simple_arith_swap2(struct i915_fp_compile *p,
429 const struct tgsi_full_instruction *inst,
430 uint opcode, uint numArgs,
431 struct i915_fragment_shader* fs)
432 {
433 struct tgsi_full_instruction inst2;
434
435 assert(numArgs == 2);
436
437 /* transpose first two registers */
438 inst2 = *inst;
439 inst2.Src[0] = inst->Src[1];
440 inst2.Src[1] = inst->Src[0];
441
442 emit_simple_arith(p, &inst2, opcode, numArgs, fs);
443 }
444
445
446 #ifndef M_PI
447 #define M_PI 3.14159265358979323846
448 #endif
449
450 /*
451 * Translate TGSI instruction to i915 instruction.
452 *
453 * Possible concerns:
454 *
455 * SIN, COS -- could use another taylor step?
456 * LIT -- results seem a little different to sw mesa
457 * LOG -- different to mesa on negative numbers, but this is conformant.
458 */
459 static void
460 i915_translate_instruction(struct i915_fp_compile *p,
461 const struct tgsi_full_instruction *inst,
462 struct i915_fragment_shader *fs)
463 {
464 uint writemask;
465 uint src0, src1, src2, flags;
466 uint tmp = 0;
467
468 switch (inst->Instruction.Opcode) {
469 case TGSI_OPCODE_ABS:
470 src0 = src_vector(p, &inst->Src[0], fs);
471 i915_emit_arith(p,
472 A0_MAX,
473 get_result_vector(p, &inst->Dst[0]),
474 get_result_flags(inst), 0,
475 src0, negate(src0, 1, 1, 1, 1), 0);
476 break;
477
478 case TGSI_OPCODE_ADD:
479 emit_simple_arith(p, inst, A0_ADD, 2, fs);
480 break;
481
482 case TGSI_OPCODE_CMP:
483 src0 = src_vector(p, &inst->Src[0], fs);
484 src1 = src_vector(p, &inst->Src[1], fs);
485 src2 = src_vector(p, &inst->Src[2], fs);
486 i915_emit_arith(p, A0_CMP,
487 get_result_vector(p, &inst->Dst[0]),
488 get_result_flags(inst),
489 0, src0, src2, src1); /* NOTE: order of src2, src1 */
490 break;
491
492 case TGSI_OPCODE_COS:
493 src0 = src_vector(p, &inst->Src[0], fs);
494 tmp = i915_get_utemp(p);
495
496 i915_emit_arith(p,
497 A0_MUL,
498 tmp, A0_DEST_CHANNEL_X, 0,
499 src0, i915_emit_const1f(p, 1.0f / (float) (M_PI * 2.0)), 0);
500
501 i915_emit_arith(p, A0_MOD, tmp, A0_DEST_CHANNEL_X, 0, tmp, 0, 0);
502
503 /*
504 * t0.xy = MUL x.xx11, x.x1111 ; x^2, x, 1, 1
505 * t0 = MUL t0.xyxy t0.xx11 ; x^4, x^3, x^2, 1
506 * t0 = MUL t0.xxz1 t0.z111 ; x^6 x^4 x^2 1
507 * result = DP4 t0, cos_constants
508 */
509 i915_emit_arith(p,
510 A0_MUL,
511 tmp, A0_DEST_CHANNEL_XY, 0,
512 swizzle(tmp, X, X, ONE, ONE),
513 swizzle(tmp, X, ONE, ONE, ONE), 0);
514
515 i915_emit_arith(p,
516 A0_MUL,
517 tmp, A0_DEST_CHANNEL_XYZ, 0,
518 swizzle(tmp, X, Y, X, ONE),
519 swizzle(tmp, X, X, ONE, ONE), 0);
520
521 i915_emit_arith(p,
522 A0_MUL,
523 tmp, A0_DEST_CHANNEL_XYZ, 0,
524 swizzle(tmp, X, X, Z, ONE),
525 swizzle(tmp, Z, ONE, ONE, ONE), 0);
526
527 i915_emit_arith(p,
528 A0_DP4,
529 get_result_vector(p, &inst->Dst[0]),
530 get_result_flags(inst), 0,
531 swizzle(tmp, ONE, Z, Y, X),
532 i915_emit_const4fv(p, cos_constants), 0);
533 break;
534
535 case TGSI_OPCODE_DDX:
536 case TGSI_OPCODE_DDY:
537 /* XXX We just output 0 here */
538 debug_printf("Punting DDX/DDX\n");
539 src0 = get_result_vector(p, &inst->Dst[0]);
540 i915_emit_arith(p,
541 A0_MOV,
542 get_result_vector(p, &inst->Dst[0]),
543 get_result_flags(inst), 0,
544 swizzle(src0, ZERO, ZERO, ZERO, ZERO), 0, 0);
545 break;
546
547 case TGSI_OPCODE_DP2:
548 src0 = src_vector(p, &inst->Src[0], fs);
549 src1 = src_vector(p, &inst->Src[1], fs);
550
551 i915_emit_arith(p,
552 A0_DP3,
553 get_result_vector(p, &inst->Dst[0]),
554 get_result_flags(inst), 0,
555 swizzle(src0, X, Y, ZERO, ZERO), src1, 0);
556 break;
557
558 case TGSI_OPCODE_DP3:
559 emit_simple_arith(p, inst, A0_DP3, 2, fs);
560 break;
561
562 case TGSI_OPCODE_DP4:
563 emit_simple_arith(p, inst, A0_DP4, 2, fs);
564 break;
565
566 case TGSI_OPCODE_DPH:
567 src0 = src_vector(p, &inst->Src[0], fs);
568 src1 = src_vector(p, &inst->Src[1], fs);
569
570 i915_emit_arith(p,
571 A0_DP4,
572 get_result_vector(p, &inst->Dst[0]),
573 get_result_flags(inst), 0,
574 swizzle(src0, X, Y, Z, ONE), src1, 0);
575 break;
576
577 case TGSI_OPCODE_DST:
578 src0 = src_vector(p, &inst->Src[0], fs);
579 src1 = src_vector(p, &inst->Src[1], fs);
580
581 /* result[0] = 1 * 1;
582 * result[1] = a[1] * b[1];
583 * result[2] = a[2] * 1;
584 * result[3] = 1 * b[3];
585 */
586 i915_emit_arith(p,
587 A0_MUL,
588 get_result_vector(p, &inst->Dst[0]),
589 get_result_flags(inst), 0,
590 swizzle(src0, ONE, Y, Z, ONE),
591 swizzle(src1, ONE, Y, ONE, W), 0);
592 break;
593
594 case TGSI_OPCODE_END:
595 /* no-op */
596 break;
597
598 case TGSI_OPCODE_EX2:
599 src0 = src_vector(p, &inst->Src[0], fs);
600
601 i915_emit_arith(p,
602 A0_EXP,
603 get_result_vector(p, &inst->Dst[0]),
604 get_result_flags(inst), 0,
605 swizzle(src0, X, X, X, X), 0, 0);
606 break;
607
608 case TGSI_OPCODE_FLR:
609 emit_simple_arith(p, inst, A0_FLR, 1, fs);
610 break;
611
612 case TGSI_OPCODE_FRC:
613 emit_simple_arith(p, inst, A0_FRC, 1, fs);
614 break;
615
616 case TGSI_OPCODE_KIL:
617 /* kill if src[0].x < 0 || src[0].y < 0 ... */
618 src0 = src_vector(p, &inst->Src[0], fs);
619 tmp = i915_get_utemp(p);
620
621 i915_emit_texld(p,
622 tmp, /* dest reg: a dummy reg */
623 A0_DEST_CHANNEL_ALL, /* dest writemask */
624 0, /* sampler */
625 src0, /* coord*/
626 T0_TEXKILL); /* opcode */
627 break;
628
629 case TGSI_OPCODE_KILP:
630 assert(0); /* not tested yet */
631 break;
632
633 case TGSI_OPCODE_LG2:
634 src0 = src_vector(p, &inst->Src[0], fs);
635
636 i915_emit_arith(p,
637 A0_LOG,
638 get_result_vector(p, &inst->Dst[0]),
639 get_result_flags(inst), 0,
640 swizzle(src0, X, X, X, X), 0, 0);
641 break;
642
643 case TGSI_OPCODE_LIT:
644 src0 = src_vector(p, &inst->Src[0], fs);
645 tmp = i915_get_utemp(p);
646
647 /* tmp = max( a.xyzw, a.00zw )
648 * XXX: Clamp tmp.w to -128..128
649 * tmp.y = log(tmp.y)
650 * tmp.y = tmp.w * tmp.y
651 * tmp.y = exp(tmp.y)
652 * result = cmp (a.11-x1, a.1x01, a.1xy1 )
653 */
654 i915_emit_arith(p, A0_MAX, tmp, A0_DEST_CHANNEL_ALL, 0,
655 src0, swizzle(src0, ZERO, ZERO, Z, W), 0);
656
657 i915_emit_arith(p, A0_LOG, tmp, A0_DEST_CHANNEL_Y, 0,
658 swizzle(tmp, Y, Y, Y, Y), 0, 0);
659
660 i915_emit_arith(p, A0_MUL, tmp, A0_DEST_CHANNEL_Y, 0,
661 swizzle(tmp, ZERO, Y, ZERO, ZERO),
662 swizzle(tmp, ZERO, W, ZERO, ZERO), 0);
663
664 i915_emit_arith(p, A0_EXP, tmp, A0_DEST_CHANNEL_Y, 0,
665 swizzle(tmp, Y, Y, Y, Y), 0, 0);
666
667 i915_emit_arith(p, A0_CMP,
668 get_result_vector(p, &inst->Dst[0]),
669 get_result_flags(inst), 0,
670 negate(swizzle(tmp, ONE, ONE, X, ONE), 0, 0, 1, 0),
671 swizzle(tmp, ONE, X, ZERO, ONE),
672 swizzle(tmp, ONE, X, Y, ONE));
673
674 break;
675
676 case TGSI_OPCODE_LRP:
677 src0 = src_vector(p, &inst->Src[0], fs);
678 src1 = src_vector(p, &inst->Src[1], fs);
679 src2 = src_vector(p, &inst->Src[2], fs);
680 flags = get_result_flags(inst);
681 tmp = i915_get_utemp(p);
682
683 /* b*a + c*(1-a)
684 *
685 * b*a + c - ca
686 *
687 * tmp = b*a + c,
688 * result = (-c)*a + tmp
689 */
690 i915_emit_arith(p, A0_MAD, tmp,
691 flags & A0_DEST_CHANNEL_ALL, 0, src1, src0, src2);
692
693 i915_emit_arith(p, A0_MAD,
694 get_result_vector(p, &inst->Dst[0]),
695 flags, 0, negate(src2, 1, 1, 1, 1), src0, tmp);
696 break;
697
698 case TGSI_OPCODE_MAD:
699 emit_simple_arith(p, inst, A0_MAD, 3, fs);
700 break;
701
702 case TGSI_OPCODE_MAX:
703 emit_simple_arith(p, inst, A0_MAX, 2, fs);
704 break;
705
706 case TGSI_OPCODE_MIN:
707 src0 = src_vector(p, &inst->Src[0], fs);
708 src1 = src_vector(p, &inst->Src[1], fs);
709 tmp = i915_get_utemp(p);
710 flags = get_result_flags(inst);
711
712 i915_emit_arith(p,
713 A0_MAX,
714 tmp, flags & A0_DEST_CHANNEL_ALL, 0,
715 negate(src0, 1, 1, 1, 1),
716 negate(src1, 1, 1, 1, 1), 0);
717
718 i915_emit_arith(p,
719 A0_MOV,
720 get_result_vector(p, &inst->Dst[0]),
721 flags, 0, negate(tmp, 1, 1, 1, 1), 0, 0);
722 break;
723
724 case TGSI_OPCODE_MOV:
725 emit_simple_arith(p, inst, A0_MOV, 1, fs);
726 break;
727
728 case TGSI_OPCODE_MUL:
729 emit_simple_arith(p, inst, A0_MUL, 2, fs);
730 break;
731
732 case TGSI_OPCODE_POW:
733 src0 = src_vector(p, &inst->Src[0], fs);
734 src1 = src_vector(p, &inst->Src[1], fs);
735 tmp = i915_get_utemp(p);
736 flags = get_result_flags(inst);
737
738 /* XXX: masking on intermediate values, here and elsewhere.
739 */
740 i915_emit_arith(p,
741 A0_LOG,
742 tmp, A0_DEST_CHANNEL_X, 0,
743 swizzle(src0, X, X, X, X), 0, 0);
744
745 i915_emit_arith(p, A0_MUL, tmp, A0_DEST_CHANNEL_X, 0, tmp, src1, 0);
746
747 i915_emit_arith(p,
748 A0_EXP,
749 get_result_vector(p, &inst->Dst[0]),
750 flags, 0, swizzle(tmp, X, X, X, X), 0, 0);
751 break;
752
753 case TGSI_OPCODE_RET:
754 /* XXX: no-op? */
755 break;
756
757 case TGSI_OPCODE_RCP:
758 src0 = src_vector(p, &inst->Src[0], fs);
759
760 i915_emit_arith(p,
761 A0_RCP,
762 get_result_vector(p, &inst->Dst[0]),
763 get_result_flags(inst), 0,
764 swizzle(src0, X, X, X, X), 0, 0);
765 break;
766
767 case TGSI_OPCODE_RSQ:
768 src0 = src_vector(p, &inst->Src[0], fs);
769
770 i915_emit_arith(p,
771 A0_RSQ,
772 get_result_vector(p, &inst->Dst[0]),
773 get_result_flags(inst), 0,
774 swizzle(src0, X, X, X, X), 0, 0);
775 break;
776
777 case TGSI_OPCODE_SCS:
778 src0 = src_vector(p, &inst->Src[0], fs);
779 tmp = i915_get_utemp(p);
780
781 /*
782 * t0.xy = MUL x.xx11, x.x1111 ; x^2, x, 1, 1
783 * t0 = MUL t0.xyxy t0.xx11 ; x^4, x^3, x^2, x
784 * t1 = MUL t0.xyyw t0.yz11 ; x^7 x^5 x^3 x
785 * scs.x = DP4 t1, scs_sin_constants
786 * t1 = MUL t0.xxz1 t0.z111 ; x^6 x^4 x^2 1
787 * scs.y = DP4 t1, scs_cos_constants
788 */
789 i915_emit_arith(p,
790 A0_MUL,
791 tmp, A0_DEST_CHANNEL_XY, 0,
792 swizzle(src0, X, X, ONE, ONE),
793 swizzle(src0, X, ONE, ONE, ONE), 0);
794
795 i915_emit_arith(p,
796 A0_MUL,
797 tmp, A0_DEST_CHANNEL_ALL, 0,
798 swizzle(tmp, X, Y, X, Y),
799 swizzle(tmp, X, X, ONE, ONE), 0);
800
801 writemask = inst->Dst[0].Register.WriteMask;
802
803 if (writemask & TGSI_WRITEMASK_Y) {
804 uint tmp1;
805
806 if (writemask & TGSI_WRITEMASK_X)
807 tmp1 = i915_get_utemp(p);
808 else
809 tmp1 = tmp;
810
811 i915_emit_arith(p,
812 A0_MUL,
813 tmp1, A0_DEST_CHANNEL_ALL, 0,
814 swizzle(tmp, X, Y, Y, W),
815 swizzle(tmp, X, Z, ONE, ONE), 0);
816
817 i915_emit_arith(p,
818 A0_DP4,
819 get_result_vector(p, &inst->Dst[0]),
820 A0_DEST_CHANNEL_Y, 0,
821 swizzle(tmp1, W, Z, Y, X),
822 i915_emit_const4fv(p, scs_sin_constants), 0);
823 }
824
825 if (writemask & TGSI_WRITEMASK_X) {
826 i915_emit_arith(p,
827 A0_MUL,
828 tmp, A0_DEST_CHANNEL_XYZ, 0,
829 swizzle(tmp, X, X, Z, ONE),
830 swizzle(tmp, Z, ONE, ONE, ONE), 0);
831
832 i915_emit_arith(p,
833 A0_DP4,
834 get_result_vector(p, &inst->Dst[0]),
835 A0_DEST_CHANNEL_X, 0,
836 swizzle(tmp, ONE, Z, Y, X),
837 i915_emit_const4fv(p, scs_cos_constants), 0);
838 }
839 break;
840
841 case TGSI_OPCODE_SEQ:
842 /* if we're both >= and <= then we're == */
843 src0 = src_vector(p, &inst->Src[0], fs);
844 src1 = src_vector(p, &inst->Src[1], fs);
845 tmp = i915_get_utemp(p);
846
847 i915_emit_arith(p,
848 A0_SGE,
849 tmp, A0_DEST_CHANNEL_ALL, 0,
850 src0,
851 src1, 0);
852
853 i915_emit_arith(p,
854 A0_SGE,
855 get_result_vector(p, &inst->Dst[0]),
856 A0_DEST_CHANNEL_ALL, 0,
857 src1,
858 src0, 0);
859
860 i915_emit_arith(p,
861 A0_MUL,
862 get_result_vector(p, &inst->Dst[0]),
863 A0_DEST_CHANNEL_ALL, 0,
864 get_result_vector(p, &inst->Dst[0]),
865 tmp, 0);
866
867 break;
868
869 case TGSI_OPCODE_SGE:
870 emit_simple_arith(p, inst, A0_SGE, 2, fs);
871 break;
872
873 case TGSI_OPCODE_SIN:
874 src0 = src_vector(p, &inst->Src[0], fs);
875 tmp = i915_get_utemp(p);
876
877 i915_emit_arith(p,
878 A0_MUL,
879 tmp, A0_DEST_CHANNEL_X, 0,
880 src0, i915_emit_const1f(p, 1.0f / (float) (M_PI * 2.0)), 0);
881
882 i915_emit_arith(p, A0_MOD, tmp, A0_DEST_CHANNEL_X, 0, tmp, 0, 0);
883
884 /*
885 * t0.xy = MUL x.xx11, x.x1111 ; x^2, x, 1, 1
886 * t0 = MUL t0.xyxy t0.xx11 ; x^4, x^3, x^2, x
887 * t1 = MUL t0.xyyw t0.yz11 ; x^7 x^5 x^3 x
888 * result = DP4 t1.wzyx, sin_constants
889 */
890 i915_emit_arith(p,
891 A0_MUL,
892 tmp, A0_DEST_CHANNEL_XY, 0,
893 swizzle(tmp, X, X, ONE, ONE),
894 swizzle(tmp, X, ONE, ONE, ONE), 0);
895
896 i915_emit_arith(p,
897 A0_MUL,
898 tmp, A0_DEST_CHANNEL_ALL, 0,
899 swizzle(tmp, X, Y, X, Y),
900 swizzle(tmp, X, X, ONE, ONE), 0);
901
902 i915_emit_arith(p,
903 A0_MUL,
904 tmp, A0_DEST_CHANNEL_ALL, 0,
905 swizzle(tmp, X, Y, Y, W),
906 swizzle(tmp, X, Z, ONE, ONE), 0);
907
908 i915_emit_arith(p,
909 A0_DP4,
910 get_result_vector(p, &inst->Dst[0]),
911 get_result_flags(inst), 0,
912 swizzle(tmp, W, Z, Y, X),
913 i915_emit_const4fv(p, sin_constants), 0);
914 break;
915
916 case TGSI_OPCODE_SLE:
917 /* like SGE, but swap reg0, reg1 */
918 emit_simple_arith_swap2(p, inst, A0_SGE, 2, fs);
919 break;
920
921 case TGSI_OPCODE_SLT:
922 emit_simple_arith(p, inst, A0_SLT, 2, fs);
923 break;
924
925 case TGSI_OPCODE_SGT:
926 /* like SLT, but swap reg0, reg1 */
927 emit_simple_arith_swap2(p, inst, A0_SLT, 2, fs);
928 break;
929
930 case TGSI_OPCODE_SNE:
931 /* if we're < or > then we're != */
932 src0 = src_vector(p, &inst->Src[0], fs);
933 src1 = src_vector(p, &inst->Src[1], fs);
934 tmp = i915_get_utemp(p);
935
936 i915_emit_arith(p,
937 A0_SLT,
938 tmp,
939 A0_DEST_CHANNEL_ALL, 0,
940 src0,
941 src1, 0);
942
943 i915_emit_arith(p,
944 A0_SLT,
945 get_result_vector(p, &inst->Dst[0]),
946 A0_DEST_CHANNEL_ALL, 0,
947 src1,
948 src0, 0);
949
950 i915_emit_arith(p,
951 A0_ADD,
952 get_result_vector(p, &inst->Dst[0]),
953 A0_DEST_CHANNEL_ALL, 0,
954 get_result_vector(p, &inst->Dst[0]),
955 tmp, 0);
956 break;
957
958 case TGSI_OPCODE_SSG:
959 /* compute (src>0) - (src<0) */
960 src0 = src_vector(p, &inst->Src[0], fs);
961 tmp = i915_get_utemp(p);
962
963 i915_emit_arith(p,
964 A0_SLT,
965 tmp,
966 A0_DEST_CHANNEL_ALL, 0,
967 src0,
968 swizzle(src0, ZERO, ZERO, ZERO, ZERO), 0);
969
970 i915_emit_arith(p,
971 A0_SLT,
972 get_result_vector(p, &inst->Dst[0]),
973 A0_DEST_CHANNEL_ALL, 0,
974 swizzle(src0, ZERO, ZERO, ZERO, ZERO),
975 src0, 0);
976
977 i915_emit_arith(p,
978 A0_ADD,
979 get_result_vector(p, &inst->Dst[0]),
980 A0_DEST_CHANNEL_ALL, 0,
981 get_result_vector(p, &inst->Dst[0]),
982 negate(tmp, 1, 1, 1, 1), 0);
983 break;
984
985 case TGSI_OPCODE_SUB:
986 src0 = src_vector(p, &inst->Src[0], fs);
987 src1 = src_vector(p, &inst->Src[1], fs);
988
989 i915_emit_arith(p,
990 A0_ADD,
991 get_result_vector(p, &inst->Dst[0]),
992 get_result_flags(inst), 0,
993 src0, negate(src1, 1, 1, 1, 1), 0);
994 break;
995
996 case TGSI_OPCODE_TEX:
997 emit_tex(p, inst, T0_TEXLD, fs);
998 break;
999
1000 case TGSI_OPCODE_TRUNC:
1001 emit_simple_arith(p, inst, A0_TRC, 1, fs);
1002 break;
1003
1004 case TGSI_OPCODE_TXB:
1005 emit_tex(p, inst, T0_TEXLDB, fs);
1006 break;
1007
1008 case TGSI_OPCODE_TXP:
1009 emit_tex(p, inst, T0_TEXLDP, fs);
1010 break;
1011
1012 case TGSI_OPCODE_XPD:
1013 /* Cross product:
1014 * result.x = src0.y * src1.z - src0.z * src1.y;
1015 * result.y = src0.z * src1.x - src0.x * src1.z;
1016 * result.z = src0.x * src1.y - src0.y * src1.x;
1017 * result.w = undef;
1018 */
1019 src0 = src_vector(p, &inst->Src[0], fs);
1020 src1 = src_vector(p, &inst->Src[1], fs);
1021 tmp = i915_get_utemp(p);
1022
1023 i915_emit_arith(p,
1024 A0_MUL,
1025 tmp, A0_DEST_CHANNEL_ALL, 0,
1026 swizzle(src0, Z, X, Y, ONE),
1027 swizzle(src1, Y, Z, X, ONE), 0);
1028
1029 i915_emit_arith(p,
1030 A0_MAD,
1031 get_result_vector(p, &inst->Dst[0]),
1032 get_result_flags(inst), 0,
1033 swizzle(src0, Y, Z, X, ONE),
1034 swizzle(src1, Z, X, Y, ONE),
1035 negate(tmp, 1, 1, 1, 0));
1036 break;
1037
1038 default:
1039 i915_program_error(p, "bad opcode %d", inst->Instruction.Opcode);
1040 p->error = 1;
1041 return;
1042 }
1043
1044 i915_release_utemps(p);
1045 }
1046
1047
1048 /**
1049 * Translate TGSI fragment shader into i915 hardware instructions.
1050 * \param p the translation state
1051 * \param tokens the TGSI token array
1052 */
1053 static void
1054 i915_translate_instructions(struct i915_fp_compile *p,
1055 const struct tgsi_token *tokens,
1056 struct i915_fragment_shader *fs)
1057 {
1058 struct i915_fragment_shader *ifs = p->shader;
1059 struct tgsi_parse_context parse;
1060
1061 tgsi_parse_init( &parse, tokens );
1062
1063 while( !tgsi_parse_end_of_tokens( &parse ) ) {
1064
1065 tgsi_parse_token( &parse );
1066
1067 switch( parse.FullToken.Token.Type ) {
1068 case TGSI_TOKEN_TYPE_PROPERTY:
1069 /*
1070 * We only support one cbuf, but we still need to ignore the property
1071 * correctly so we don't hit the assert at the end of the switch case.
1072 */
1073 assert(parse.FullToken.FullProperty.Property.PropertyName ==
1074 TGSI_PROPERTY_FS_COLOR0_WRITES_ALL_CBUFS);
1075 break;
1076 case TGSI_TOKEN_TYPE_DECLARATION:
1077 if (parse.FullToken.FullDeclaration.Declaration.File
1078 == TGSI_FILE_CONSTANT) {
1079 uint i;
1080 for (i = parse.FullToken.FullDeclaration.Range.First;
1081 i <= parse.FullToken.FullDeclaration.Range.Last;
1082 i++) {
1083 assert(ifs->constant_flags[i] == 0x0);
1084 ifs->constant_flags[i] = I915_CONSTFLAG_USER;
1085 ifs->num_constants = MAX2(ifs->num_constants, i + 1);
1086 }
1087 }
1088 else if (parse.FullToken.FullDeclaration.Declaration.File
1089 == TGSI_FILE_TEMPORARY) {
1090 uint i;
1091 for (i = parse.FullToken.FullDeclaration.Range.First;
1092 i <= parse.FullToken.FullDeclaration.Range.Last;
1093 i++) {
1094 assert(i < I915_MAX_TEMPORARY);
1095 /* XXX just use shader->info->file_mask[TGSI_FILE_TEMPORARY] */
1096 p->temp_flag |= (1 << i); /* mark temp as used */
1097 }
1098 }
1099 break;
1100
1101 case TGSI_TOKEN_TYPE_IMMEDIATE:
1102 {
1103 const struct tgsi_full_immediate *imm
1104 = &parse.FullToken.FullImmediate;
1105 const uint pos = p->num_immediates++;
1106 uint j;
1107 assert( imm->Immediate.NrTokens <= 4 + 1 );
1108 for (j = 0; j < imm->Immediate.NrTokens - 1; j++) {
1109 p->immediates[pos][j] = imm->u[j].Float;
1110 }
1111 }
1112 break;
1113
1114 case TGSI_TOKEN_TYPE_INSTRUCTION:
1115 if (p->first_instruction) {
1116 /* resolve location of immediates */
1117 uint i, j;
1118 for (i = 0; i < p->num_immediates; i++) {
1119 /* find constant slot for this immediate */
1120 for (j = 0; j < I915_MAX_CONSTANT; j++) {
1121 if (ifs->constant_flags[j] == 0x0) {
1122 memcpy(ifs->constants[j],
1123 p->immediates[i],
1124 4 * sizeof(float));
1125 /*printf("immediate %d maps to const %d\n", i, j);*/
1126 ifs->constant_flags[j] = 0xf; /* all four comps used */
1127 p->immediates_map[i] = j;
1128 ifs->num_constants = MAX2(ifs->num_constants, j + 1);
1129 break;
1130 }
1131 }
1132 }
1133
1134 p->first_instruction = FALSE;
1135 }
1136
1137 i915_translate_instruction(p, &parse.FullToken.FullInstruction, fs);
1138 break;
1139
1140 default:
1141 assert( 0 );
1142 }
1143
1144 } /* while */
1145
1146 tgsi_parse_free (&parse);
1147 }
1148
1149
1150 static struct i915_fp_compile *
1151 i915_init_compile(struct i915_context *i915,
1152 struct i915_fragment_shader *ifs)
1153 {
1154 struct i915_fp_compile *p = CALLOC_STRUCT(i915_fp_compile);
1155 int i;
1156
1157 p->shader = ifs;
1158
1159 /* Put new constants at end of const buffer, growing downward.
1160 * The problem is we don't know how many user-defined constants might
1161 * be specified with pipe->set_constant_buffer().
1162 * Should pre-scan the user's program to determine the highest-numbered
1163 * constant referenced.
1164 */
1165 ifs->num_constants = 0;
1166 memset(ifs->constant_flags, 0, sizeof(ifs->constant_flags));
1167
1168 for (i = 0; i < I915_TEX_UNITS; i++)
1169 ifs->generic_mapping[i] = -1;
1170
1171 p->first_instruction = TRUE;
1172
1173 p->nr_tex_indirect = 1; /* correct? */
1174 p->nr_tex_insn = 0;
1175 p->nr_alu_insn = 0;
1176 p->nr_decl_insn = 0;
1177
1178 p->csr = p->program;
1179 p->decl = p->declarations;
1180 p->decl_s = 0;
1181 p->decl_t = 0;
1182 p->temp_flag = ~0x0 << I915_MAX_TEMPORARY;
1183 p->utemp_flag = ~0x7;
1184
1185 /* initialize the first program word */
1186 *(p->decl++) = _3DSTATE_PIXEL_SHADER_PROGRAM;
1187
1188 return p;
1189 }
1190
1191
1192 /* Copy compile results to the fragment program struct and destroy the
1193 * compilation context.
1194 */
1195 static void
1196 i915_fini_compile(struct i915_context *i915, struct i915_fp_compile *p)
1197 {
1198 struct i915_fragment_shader *ifs = p->shader;
1199 unsigned long program_size = (unsigned long) (p->csr - p->program);
1200 unsigned long decl_size = (unsigned long) (p->decl - p->declarations);
1201
1202 if (p->nr_tex_indirect > I915_MAX_TEX_INDIRECT)
1203 i915_program_error(p, "Exceeded max nr indirect texture lookups");
1204
1205 if (p->nr_tex_insn > I915_MAX_TEX_INSN)
1206 i915_program_error(p, "Exceeded max TEX instructions");
1207
1208 if (p->nr_alu_insn > I915_MAX_ALU_INSN)
1209 i915_program_error(p, "Exceeded max ALU instructions");
1210
1211 if (p->nr_decl_insn > I915_MAX_DECL_INSN)
1212 i915_program_error(p, "Exceeded max DECL instructions");
1213
1214 if (p->error) {
1215 p->NumNativeInstructions = 0;
1216 p->NumNativeAluInstructions = 0;
1217 p->NumNativeTexInstructions = 0;
1218 p->NumNativeTexIndirections = 0;
1219
1220 i915_use_passthrough_shader(ifs);
1221 }
1222 else {
1223 p->NumNativeInstructions
1224 = p->nr_alu_insn + p->nr_tex_insn + p->nr_decl_insn;
1225 p->NumNativeAluInstructions = p->nr_alu_insn;
1226 p->NumNativeTexInstructions = p->nr_tex_insn;
1227 p->NumNativeTexIndirections = p->nr_tex_indirect;
1228
1229 /* patch in the program length */
1230 p->declarations[0] |= program_size + decl_size - 2;
1231
1232 /* Copy compilation results to fragment program struct:
1233 */
1234 assert(!ifs->program);
1235 ifs->program
1236 = (uint *) MALLOC((program_size + decl_size) * sizeof(uint));
1237 if (ifs->program) {
1238 ifs->program_len = program_size + decl_size;
1239
1240 memcpy(ifs->program,
1241 p->declarations,
1242 decl_size * sizeof(uint));
1243
1244 memcpy(ifs->program + decl_size,
1245 p->program,
1246 program_size * sizeof(uint));
1247 }
1248 }
1249
1250 /* Release the compilation struct:
1251 */
1252 FREE(p);
1253 }
1254
1255
1256
1257
1258
1259 /**
1260 * Rather than trying to intercept and jiggle depth writes during
1261 * emit, just move the value into its correct position at the end of
1262 * the program:
1263 */
1264 static void
1265 i915_fixup_depth_write(struct i915_fp_compile *p)
1266 {
1267 /* XXX assuming pos/depth is always in output[0] */
1268 if (p->shader->info.output_semantic_name[0] == TGSI_SEMANTIC_POSITION) {
1269 const uint depth = UREG(REG_TYPE_OD, 0);
1270
1271 i915_emit_arith(p,
1272 A0_MOV, /* opcode */
1273 depth, /* dest reg */
1274 A0_DEST_CHANNEL_W, /* write mask */
1275 0, /* saturate? */
1276 swizzle(depth, X, Y, Z, Z), /* src0 */
1277 0, 0 /* src1, src2 */);
1278 }
1279 }
1280
1281
1282 void
1283 i915_translate_fragment_program( struct i915_context *i915,
1284 struct i915_fragment_shader *fs)
1285 {
1286 struct i915_fp_compile *p;
1287 const struct tgsi_token *tokens = fs->state.tokens;
1288
1289 #if 0
1290 tgsi_dump(tokens, 0);
1291 #endif
1292
1293 /* hw doesn't seem to like empty frag programs, even when the depth write
1294 * fixup gets emitted below - may that one is fishy, too? */
1295 if (fs->info.num_instructions == 1) {
1296 i915_use_passthrough_shader(fs);
1297
1298 return;
1299 }
1300
1301 p = i915_init_compile(i915, fs);
1302
1303 i915_translate_instructions(p, tokens, fs);
1304 i915_fixup_depth_write(p);
1305
1306 i915_fini_compile(i915, p);
1307 }