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