1 /**************************************************************************
3 * Copyright 2007 Tungsten Graphics, Inc., Cedar Park, Texas.
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:
14 * The above copyright notice and this permission notice (including the
15 * next paragraph) shall be included in all copies or substantial portions
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.
26 **************************************************************************/
32 #include "i915_context.h"
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"
42 #include "draw/draw_vertex.h"
45 #define M_PI 3.14159265358979323846
49 * Simple pass-through fragment shader to use when we don't have
50 * a real shader (or it fails to compile for some reason).
52 static unsigned passthrough
[] =
54 _3DSTATE_PIXEL_SHADER_PROGRAM
| ((2*3)-1),
56 /* declare input color:
59 (REG_TYPE_T
<< D0_TYPE_SHIFT
) |
60 (T_DIFFUSE
<< D0_NR_SHIFT
) |
65 /* move to output color:
68 (REG_TYPE_OC
<< A0_DEST_TYPE_SHIFT
) |
70 (REG_TYPE_T
<< A0_SRC0_TYPE_SHIFT
) |
71 (T_DIFFUSE
<< A0_SRC0_NR_SHIFT
)),
72 0x01230000, /* .xyzw */
77 /* 1, -1/3!, 1/5!, -1/7! */
78 static const float scs_sin_constants
[4] = { 1.0,
80 1.0f
/ (5 * 4 * 3 * 2 * 1),
81 -1.0f
/ (7 * 6 * 5 * 4 * 3 * 2 * 1)
84 /* 1, -1/2!, 1/4!, -1/6! */
85 static const float scs_cos_constants
[4] = { 1.0,
87 1.0f
/ (4 * 3 * 2 * 1),
88 -1.0f
/ (6 * 5 * 4 * 3 * 2 * 1)
91 /* 2*pi, -(2*pi)^3/3!, (2*pi)^5/5!, -(2*pi)^7/7! */
92 static const float sin_constants
[4] = { 2.0 * M_PI
,
93 -8.0f
* M_PI
* M_PI
* M_PI
/ (3 * 2 * 1),
94 32.0f
* M_PI
* M_PI
* M_PI
* M_PI
* M_PI
/ (5 * 4 * 3 * 2 * 1),
95 -128.0f
* M_PI
* M_PI
* M_PI
* M_PI
* M_PI
* M_PI
* M_PI
/ (7 * 6 * 5 * 4 * 3 * 2 * 1)
98 /* 1, -(2*pi)^2/2!, (2*pi)^4/4!, -(2*pi)^6/6! */
99 static const float cos_constants
[4] = { 1.0,
100 -4.0f
* M_PI
* M_PI
/ (2 * 1),
101 16.0f
* M_PI
* M_PI
* M_PI
* M_PI
/ (4 * 3 * 2 * 1),
102 -64.0f
* M_PI
* M_PI
* M_PI
* M_PI
* M_PI
* M_PI
/ (6 * 5 * 4 * 3 * 2 * 1)
108 * component-wise negation of ureg
111 negate(int reg
, int x
, int y
, int z
, int w
)
113 /* Another neat thing about the UREG representation */
114 return reg
^ (((x
& 1) << UREG_CHANNEL_X_NEGATE_SHIFT
) |
115 ((y
& 1) << UREG_CHANNEL_Y_NEGATE_SHIFT
) |
116 ((z
& 1) << UREG_CHANNEL_Z_NEGATE_SHIFT
) |
117 ((w
& 1) << UREG_CHANNEL_W_NEGATE_SHIFT
));
122 * In the event of a translation failure, we'll generate a simple color
123 * pass-through program.
126 i915_use_passthrough_shader(struct i915_fragment_shader
*fs
)
128 fs
->program
= (uint
*) MALLOC(sizeof(passthrough
));
130 memcpy(fs
->program
, passthrough
, sizeof(passthrough
));
131 fs
->program_len
= Elements(passthrough
);
133 fs
->num_constants
= 0;
138 i915_program_error(struct i915_fp_compile
*p
, const char *msg
, ...)
143 debug_printf("i915_program_error: ");
144 va_start( args
, msg
);
145 util_vsnprintf( buffer
, sizeof(buffer
), msg
, args
);
147 debug_printf("%s", buffer
);
153 static uint
get_mapping(struct i915_fragment_shader
* fs
, int unit
)
156 for (i
= 0; i
< I915_TEX_UNITS
; i
++)
158 if (fs
->generic_mapping
[i
] == -1) {
159 fs
->generic_mapping
[i
] = unit
;
162 if (fs
->generic_mapping
[i
] == unit
)
165 debug_printf("Exceeded max generics\n");
170 * Construct a ureg for the given source register. Will emit
171 * constants, apply swizzling and negation as needed.
174 src_vector(struct i915_fp_compile
*p
,
175 const struct i915_full_src_register
*source
,
176 struct i915_fragment_shader
* fs
)
178 uint index
= source
->Register
.Index
;
179 uint src
= 0, sem_name
, sem_ind
;
181 switch (source
->Register
.File
) {
182 case TGSI_FILE_TEMPORARY
:
183 if (source
->Register
.Index
>= I915_MAX_TEMPORARY
) {
184 i915_program_error(p
, "Exceeded max temporary reg");
187 src
= UREG(REG_TYPE_R
, index
);
189 case TGSI_FILE_INPUT
:
190 /* XXX: Packing COL1, FOGC into a single attribute works for
191 * texenv programs, but will fail for real fragment programs
192 * that use these attributes and expect them to be a full 4
193 * components wide. Could use a texcoord to pass these
194 * attributes if necessary, but that won't work in the general
197 * We also use a texture coordinate to pass wpos when possible.
200 sem_name
= p
->shader
->info
.input_semantic_name
[index
];
201 sem_ind
= p
->shader
->info
.input_semantic_index
[index
];
204 case TGSI_SEMANTIC_POSITION
:
207 int real_tex_unit
= get_mapping(fs
, I915_SEMANTIC_POS
);
208 src
= i915_emit_decl(p
, REG_TYPE_T
, T_TEX0
+ real_tex_unit
, D0_CHANNEL_ALL
);
211 case TGSI_SEMANTIC_COLOR
:
213 src
= i915_emit_decl(p
, REG_TYPE_T
, T_DIFFUSE
, D0_CHANNEL_ALL
);
216 /* secondary color */
217 assert(sem_ind
== 1);
218 src
= i915_emit_decl(p
, REG_TYPE_T
, T_SPECULAR
, D0_CHANNEL_XYZ
);
219 src
= swizzle(src
, X
, Y
, Z
, ONE
);
222 case TGSI_SEMANTIC_FOG
:
223 src
= i915_emit_decl(p
, REG_TYPE_T
, T_FOG_W
, D0_CHANNEL_W
);
224 src
= swizzle(src
, W
, W
, W
, W
);
226 case TGSI_SEMANTIC_GENERIC
:
228 int real_tex_unit
= get_mapping(fs
, sem_ind
);
229 src
= i915_emit_decl(p
, REG_TYPE_T
, T_TEX0
+ real_tex_unit
, D0_CHANNEL_ALL
);
232 case TGSI_SEMANTIC_FACE
:
234 /* for back/front faces */
235 int real_tex_unit
= get_mapping(fs
, I915_SEMANTIC_FACE
);
236 src
= i915_emit_decl(p
, REG_TYPE_T
, T_TEX0
+ real_tex_unit
, D0_CHANNEL_X
);
240 i915_program_error(p
, "Bad source->Index");
245 case TGSI_FILE_IMMEDIATE
:
246 assert(index
< p
->num_immediates
);
247 index
= p
->immediates_map
[index
];
249 case TGSI_FILE_CONSTANT
:
250 src
= UREG(REG_TYPE_CONST
, index
);
254 i915_program_error(p
, "Bad source->File");
259 source
->Register
.SwizzleX
,
260 source
->Register
.SwizzleY
,
261 source
->Register
.SwizzleZ
,
262 source
->Register
.SwizzleW
);
264 /* There's both negate-all-components and per-component negation.
265 * Try to handle both here.
268 int n
= source
->Register
.Negate
;
269 src
= negate(src
, n
, n
, n
, n
);
274 /* XXX assertions disabled to allow arbfplight.c to run */
275 /* XXX enable these assertions, or fix things */
276 assert(!source
->Register
.Absolute
);
278 if (source
->Register
.Absolute
)
279 debug_printf("Unhandled absolute value\n");
286 * Construct a ureg for a destination register.
289 get_result_vector(struct i915_fp_compile
*p
,
290 const struct i915_full_dst_register
*dest
)
292 switch (dest
->Register
.File
) {
293 case TGSI_FILE_OUTPUT
:
295 uint sem_name
= p
->shader
->info
.output_semantic_name
[dest
->Register
.Index
];
297 case TGSI_SEMANTIC_POSITION
:
298 return UREG(REG_TYPE_OD
, 0);
299 case TGSI_SEMANTIC_COLOR
:
300 return UREG(REG_TYPE_OC
, 0);
302 i915_program_error(p
, "Bad inst->DstReg.Index/semantics");
306 case TGSI_FILE_TEMPORARY
:
307 return UREG(REG_TYPE_R
, dest
->Register
.Index
);
309 i915_program_error(p
, "Bad inst->DstReg.File");
316 * Compute flags for saturation and writemask.
319 get_result_flags(const struct i915_full_instruction
*inst
)
322 = inst
->Dst
[0].Register
.WriteMask
;
325 if (inst
->Instruction
.Saturate
== TGSI_SAT_ZERO_ONE
)
326 flags
|= A0_DEST_SATURATE
;
328 if (writeMask
& TGSI_WRITEMASK_X
)
329 flags
|= A0_DEST_CHANNEL_X
;
330 if (writeMask
& TGSI_WRITEMASK_Y
)
331 flags
|= A0_DEST_CHANNEL_Y
;
332 if (writeMask
& TGSI_WRITEMASK_Z
)
333 flags
|= A0_DEST_CHANNEL_Z
;
334 if (writeMask
& TGSI_WRITEMASK_W
)
335 flags
|= A0_DEST_CHANNEL_W
;
342 * Convert TGSI_TEXTURE_x token to DO_SAMPLE_TYPE_x token
345 translate_tex_src_target(struct i915_fp_compile
*p
, uint tex
)
348 case TGSI_TEXTURE_SHADOW1D
:
350 case TGSI_TEXTURE_1D
:
351 return D0_SAMPLE_TYPE_2D
;
353 case TGSI_TEXTURE_SHADOW2D
:
355 case TGSI_TEXTURE_2D
:
356 return D0_SAMPLE_TYPE_2D
;
358 case TGSI_TEXTURE_SHADOWRECT
:
360 case TGSI_TEXTURE_RECT
:
361 return D0_SAMPLE_TYPE_2D
;
363 case TGSI_TEXTURE_3D
:
364 return D0_SAMPLE_TYPE_VOLUME
;
366 case TGSI_TEXTURE_CUBE
:
367 return D0_SAMPLE_TYPE_CUBE
;
370 i915_program_error(p
, "TexSrc type");
376 * Return the number of coords needed to access a given TGSI_TEXTURE_*
379 texture_num_coords(struct i915_fp_compile
*p
, uint tex
)
382 case TGSI_TEXTURE_SHADOW1D
:
383 case TGSI_TEXTURE_1D
:
386 case TGSI_TEXTURE_SHADOW2D
:
387 case TGSI_TEXTURE_2D
:
388 case TGSI_TEXTURE_SHADOWRECT
:
389 case TGSI_TEXTURE_RECT
:
392 case TGSI_TEXTURE_3D
:
393 case TGSI_TEXTURE_CUBE
:
397 i915_program_error(p
, "Num coords");
404 * Generate texel lookup instruction.
407 emit_tex(struct i915_fp_compile
*p
,
408 const struct i915_full_instruction
*inst
,
410 struct i915_fragment_shader
* fs
)
412 uint texture
= inst
->Texture
.Texture
;
413 uint unit
= inst
->Src
[1].Register
.Index
;
414 uint tex
= translate_tex_src_target( p
, texture
);
415 uint sampler
= i915_emit_decl(p
, REG_TYPE_S
, unit
, tex
);
416 uint coord
= src_vector( p
, &inst
->Src
[0], fs
);
419 get_result_vector( p
, &inst
->Dst
[0] ),
420 get_result_flags( inst
),
424 texture_num_coords(p
, texture
) );
429 * Generate a simple arithmetic instruction
430 * \param opcode the i915 opcode
431 * \param numArgs the number of input/src arguments
434 emit_simple_arith(struct i915_fp_compile
*p
,
435 const struct i915_full_instruction
*inst
,
436 uint opcode
, uint numArgs
,
437 struct i915_fragment_shader
* fs
)
439 uint arg1
, arg2
, arg3
;
441 assert(numArgs
<= 3);
443 arg1
= (numArgs
< 1) ? 0 : src_vector( p
, &inst
->Src
[0], fs
);
444 arg2
= (numArgs
< 2) ? 0 : src_vector( p
, &inst
->Src
[1], fs
);
445 arg3
= (numArgs
< 3) ? 0 : src_vector( p
, &inst
->Src
[2], fs
);
449 get_result_vector( p
, &inst
->Dst
[0]),
450 get_result_flags( inst
), 0,
457 /** As above, but swap the first two src regs */
459 emit_simple_arith_swap2(struct i915_fp_compile
*p
,
460 const struct i915_full_instruction
*inst
,
461 uint opcode
, uint numArgs
,
462 struct i915_fragment_shader
* fs
)
464 struct i915_full_instruction inst2
;
466 assert(numArgs
== 2);
468 /* transpose first two registers */
470 inst2
.Src
[0] = inst
->Src
[1];
471 inst2
.Src
[1] = inst
->Src
[0];
473 emit_simple_arith(p
, &inst2
, opcode
, numArgs
, fs
);
477 * Translate TGSI instruction to i915 instruction.
481 * DDX, DDY -- return 0
482 * SIN, COS -- could use another taylor step?
483 * LIT -- results seem a little different to sw mesa
484 * LOG -- different to mesa on negative numbers, but this is conformant.
487 i915_translate_instruction(struct i915_fp_compile
*p
,
488 const struct i915_full_instruction
*inst
,
489 struct i915_fragment_shader
*fs
)
492 uint src0
, src1
, src2
, flags
;
495 switch (inst
->Instruction
.Opcode
) {
496 case TGSI_OPCODE_ABS
:
497 src0
= src_vector(p
, &inst
->Src
[0], fs
);
500 get_result_vector(p
, &inst
->Dst
[0]),
501 get_result_flags(inst
), 0,
502 src0
, negate(src0
, 1, 1, 1, 1), 0);
505 case TGSI_OPCODE_ADD
:
506 emit_simple_arith(p
, inst
, A0_ADD
, 2, fs
);
509 case TGSI_OPCODE_CMP
:
510 src0
= src_vector(p
, &inst
->Src
[0], fs
);
511 src1
= src_vector(p
, &inst
->Src
[1], fs
);
512 src2
= src_vector(p
, &inst
->Src
[2], fs
);
513 i915_emit_arith(p
, A0_CMP
,
514 get_result_vector(p
, &inst
->Dst
[0]),
515 get_result_flags(inst
),
516 0, src0
, src2
, src1
); /* NOTE: order of src2, src1 */
519 case TGSI_OPCODE_COS
:
520 src0
= src_vector(p
, &inst
->Src
[0], fs
);
521 tmp
= i915_get_utemp(p
);
525 tmp
, A0_DEST_CHANNEL_X
, 0,
526 src0
, i915_emit_const1f(p
, 1.0f
/ (float) (M_PI
* 2.0)), 0);
528 i915_emit_arith(p
, A0_MOD
, tmp
, A0_DEST_CHANNEL_X
, 0, tmp
, 0, 0);
531 * t0.xy = MUL x.xx11, x.x111 ; x^2, x, 1, 1
532 * t0 = MUL t0.xyxy t0.xx11 ; x^4, x^3, x^2, 1
533 * t0 = MUL t0.xxz1 t0.z111 ; x^6 x^4 x^2 1
534 * result = DP4 t0, cos_constants
538 tmp
, A0_DEST_CHANNEL_XY
, 0,
539 swizzle(tmp
, X
, X
, ONE
, ONE
),
540 swizzle(tmp
, X
, ONE
, ONE
, ONE
), 0);
544 tmp
, A0_DEST_CHANNEL_XYZ
, 0,
545 swizzle(tmp
, X
, Y
, X
, ONE
),
546 swizzle(tmp
, X
, X
, ONE
, ONE
), 0);
550 tmp
, A0_DEST_CHANNEL_XYZ
, 0,
551 swizzle(tmp
, X
, X
, Z
, ONE
),
552 swizzle(tmp
, Z
, ONE
, ONE
, ONE
), 0);
556 get_result_vector(p
, &inst
->Dst
[0]),
557 get_result_flags(inst
), 0,
558 swizzle(tmp
, ONE
, Z
, Y
, X
),
559 i915_emit_const4fv(p
, cos_constants
), 0);
562 case TGSI_OPCODE_DDX
:
563 case TGSI_OPCODE_DDY
:
564 /* XXX We just output 0 here */
565 debug_printf("Punting DDX/DDX\n");
566 src0
= get_result_vector(p
, &inst
->Dst
[0]);
569 get_result_vector(p
, &inst
->Dst
[0]),
570 get_result_flags(inst
), 0,
571 swizzle(src0
, ZERO
, ZERO
, ZERO
, ZERO
), 0, 0);
574 case TGSI_OPCODE_DP2
:
575 src0
= src_vector(p
, &inst
->Src
[0], fs
);
576 src1
= src_vector(p
, &inst
->Src
[1], fs
);
580 get_result_vector(p
, &inst
->Dst
[0]),
581 get_result_flags(inst
), 0,
582 swizzle(src0
, X
, Y
, ZERO
, ZERO
), src1
, 0);
585 case TGSI_OPCODE_DP3
:
586 emit_simple_arith(p
, inst
, A0_DP3
, 2, fs
);
589 case TGSI_OPCODE_DP4
:
590 emit_simple_arith(p
, inst
, A0_DP4
, 2, fs
);
593 case TGSI_OPCODE_DPH
:
594 src0
= src_vector(p
, &inst
->Src
[0], fs
);
595 src1
= src_vector(p
, &inst
->Src
[1], fs
);
599 get_result_vector(p
, &inst
->Dst
[0]),
600 get_result_flags(inst
), 0,
601 swizzle(src0
, X
, Y
, Z
, ONE
), src1
, 0);
604 case TGSI_OPCODE_DST
:
605 src0
= src_vector(p
, &inst
->Src
[0], fs
);
606 src1
= src_vector(p
, &inst
->Src
[1], fs
);
608 /* result[0] = 1 * 1;
609 * result[1] = a[1] * b[1];
610 * result[2] = a[2] * 1;
611 * result[3] = 1 * b[3];
615 get_result_vector(p
, &inst
->Dst
[0]),
616 get_result_flags(inst
), 0,
617 swizzle(src0
, ONE
, Y
, Z
, ONE
),
618 swizzle(src1
, ONE
, Y
, ONE
, W
), 0);
621 case TGSI_OPCODE_END
:
625 case TGSI_OPCODE_EX2
:
626 src0
= src_vector(p
, &inst
->Src
[0], fs
);
630 get_result_vector(p
, &inst
->Dst
[0]),
631 get_result_flags(inst
), 0,
632 swizzle(src0
, X
, X
, X
, X
), 0, 0);
635 case TGSI_OPCODE_FLR
:
636 emit_simple_arith(p
, inst
, A0_FLR
, 1, fs
);
639 case TGSI_OPCODE_FRC
:
640 emit_simple_arith(p
, inst
, A0_FRC
, 1, fs
);
643 case TGSI_OPCODE_KIL
:
644 /* kill if src[0].x < 0 || src[0].y < 0 ... */
645 src0
= src_vector(p
, &inst
->Src
[0], fs
);
646 tmp
= i915_get_utemp(p
);
649 tmp
, /* dest reg: a dummy reg */
650 A0_DEST_CHANNEL_ALL
, /* dest writemask */
653 T0_TEXKILL
, /* opcode */
657 case TGSI_OPCODE_KILP
:
658 /* We emit an unconditional kill; we may want to revisit
659 * if we ever implement conditionals.
661 tmp
= i915_get_utemp(p
);
664 tmp
, /* dest reg: a dummy reg */
665 A0_DEST_CHANNEL_ALL
, /* dest writemask */
667 negate(swizzle(0, ONE
, ONE
, ONE
, ONE
), 1, 1, 1, 1), /* coord */
668 T0_TEXKILL
, /* opcode */
672 case TGSI_OPCODE_LG2
:
673 src0
= src_vector(p
, &inst
->Src
[0], fs
);
677 get_result_vector(p
, &inst
->Dst
[0]),
678 get_result_flags(inst
), 0,
679 swizzle(src0
, X
, X
, X
, X
), 0, 0);
682 case TGSI_OPCODE_LIT
:
683 src0
= src_vector(p
, &inst
->Src
[0], fs
);
684 tmp
= i915_get_utemp(p
);
686 /* tmp = max( a.xyzw, a.00zw )
687 * XXX: Clamp tmp.w to -128..128
689 * tmp.y = tmp.w * tmp.y
691 * result = cmp (a.11-x1, a.1x01, a.1xy1 )
693 i915_emit_arith(p
, A0_MAX
, tmp
, A0_DEST_CHANNEL_ALL
, 0,
694 src0
, swizzle(src0
, ZERO
, ZERO
, Z
, W
), 0);
696 i915_emit_arith(p
, A0_LOG
, tmp
, A0_DEST_CHANNEL_Y
, 0,
697 swizzle(tmp
, Y
, Y
, Y
, Y
), 0, 0);
699 i915_emit_arith(p
, A0_MUL
, tmp
, A0_DEST_CHANNEL_Y
, 0,
700 swizzle(tmp
, ZERO
, Y
, ZERO
, ZERO
),
701 swizzle(tmp
, ZERO
, W
, ZERO
, ZERO
), 0);
703 i915_emit_arith(p
, A0_EXP
, tmp
, A0_DEST_CHANNEL_Y
, 0,
704 swizzle(tmp
, Y
, Y
, Y
, Y
), 0, 0);
706 i915_emit_arith(p
, A0_CMP
,
707 get_result_vector(p
, &inst
->Dst
[0]),
708 get_result_flags(inst
), 0,
709 negate(swizzle(tmp
, ONE
, ONE
, X
, ONE
), 0, 0, 1, 0),
710 swizzle(tmp
, ONE
, X
, ZERO
, ONE
),
711 swizzle(tmp
, ONE
, X
, Y
, ONE
));
715 case TGSI_OPCODE_LRP
:
716 src0
= src_vector(p
, &inst
->Src
[0], fs
);
717 src1
= src_vector(p
, &inst
->Src
[1], fs
);
718 src2
= src_vector(p
, &inst
->Src
[2], fs
);
719 flags
= get_result_flags(inst
);
720 tmp
= i915_get_utemp(p
);
727 * result = (-c)*a + tmp
729 i915_emit_arith(p
, A0_MAD
, tmp
,
730 flags
& A0_DEST_CHANNEL_ALL
, 0, src1
, src0
, src2
);
732 i915_emit_arith(p
, A0_MAD
,
733 get_result_vector(p
, &inst
->Dst
[0]),
734 flags
, 0, negate(src2
, 1, 1, 1, 1), src0
, tmp
);
737 case TGSI_OPCODE_MAD
:
738 emit_simple_arith(p
, inst
, A0_MAD
, 3, fs
);
741 case TGSI_OPCODE_MAX
:
742 emit_simple_arith(p
, inst
, A0_MAX
, 2, fs
);
745 case TGSI_OPCODE_MIN
:
746 src0
= src_vector(p
, &inst
->Src
[0], fs
);
747 src1
= src_vector(p
, &inst
->Src
[1], fs
);
748 tmp
= i915_get_utemp(p
);
749 flags
= get_result_flags(inst
);
753 tmp
, flags
& A0_DEST_CHANNEL_ALL
, 0,
754 negate(src0
, 1, 1, 1, 1),
755 negate(src1
, 1, 1, 1, 1), 0);
759 get_result_vector(p
, &inst
->Dst
[0]),
760 flags
, 0, negate(tmp
, 1, 1, 1, 1), 0, 0);
763 case TGSI_OPCODE_MOV
:
764 emit_simple_arith(p
, inst
, A0_MOV
, 1, fs
);
767 case TGSI_OPCODE_MUL
:
768 emit_simple_arith(p
, inst
, A0_MUL
, 2, fs
);
771 case TGSI_OPCODE_NOP
:
774 case TGSI_OPCODE_POW
:
775 src0
= src_vector(p
, &inst
->Src
[0], fs
);
776 src1
= src_vector(p
, &inst
->Src
[1], fs
);
777 tmp
= i915_get_utemp(p
);
778 flags
= get_result_flags(inst
);
780 /* XXX: masking on intermediate values, here and elsewhere.
784 tmp
, A0_DEST_CHANNEL_X
, 0,
785 swizzle(src0
, X
, X
, X
, X
), 0, 0);
787 i915_emit_arith(p
, A0_MUL
, tmp
, A0_DEST_CHANNEL_X
, 0, tmp
, src1
, 0);
791 get_result_vector(p
, &inst
->Dst
[0]),
792 flags
, 0, swizzle(tmp
, X
, X
, X
, X
), 0, 0);
795 case TGSI_OPCODE_RET
:
799 case TGSI_OPCODE_RCP
:
800 src0
= src_vector(p
, &inst
->Src
[0], fs
);
804 get_result_vector(p
, &inst
->Dst
[0]),
805 get_result_flags(inst
), 0,
806 swizzle(src0
, X
, X
, X
, X
), 0, 0);
809 case TGSI_OPCODE_RSQ
:
810 src0
= src_vector(p
, &inst
->Src
[0], fs
);
814 get_result_vector(p
, &inst
->Dst
[0]),
815 get_result_flags(inst
), 0,
816 swizzle(src0
, X
, X
, X
, X
), 0, 0);
819 case TGSI_OPCODE_SCS
:
820 src0
= src_vector(p
, &inst
->Src
[0], fs
);
821 tmp
= i915_get_utemp(p
);
824 * t0.xy = MUL x.xx11, x.x1111 ; x^2, x, 1, 1
825 * t0 = MUL t0.xyxy t0.xx11 ; x^4, x^3, x^2, x
826 * t1 = MUL t0.xyyw t0.yz11 ; x^7 x^5 x^3 x
827 * scs.x = DP4 t1, scs_sin_constants
828 * t1 = MUL t0.xxz1 t0.z111 ; x^6 x^4 x^2 1
829 * scs.y = DP4 t1, scs_cos_constants
833 tmp
, A0_DEST_CHANNEL_XY
, 0,
834 swizzle(src0
, X
, X
, ONE
, ONE
),
835 swizzle(src0
, X
, ONE
, ONE
, ONE
), 0);
839 tmp
, A0_DEST_CHANNEL_ALL
, 0,
840 swizzle(tmp
, X
, Y
, X
, Y
),
841 swizzle(tmp
, X
, X
, ONE
, ONE
), 0);
843 writemask
= inst
->Dst
[0].Register
.WriteMask
;
845 if (writemask
& TGSI_WRITEMASK_Y
) {
848 if (writemask
& TGSI_WRITEMASK_X
)
849 tmp1
= i915_get_utemp(p
);
855 tmp1
, A0_DEST_CHANNEL_ALL
, 0,
856 swizzle(tmp
, X
, Y
, Y
, W
),
857 swizzle(tmp
, X
, Z
, ONE
, ONE
), 0);
861 get_result_vector(p
, &inst
->Dst
[0]),
862 A0_DEST_CHANNEL_Y
, 0,
863 swizzle(tmp1
, W
, Z
, Y
, X
),
864 i915_emit_const4fv(p
, scs_sin_constants
), 0);
867 if (writemask
& TGSI_WRITEMASK_X
) {
870 tmp
, A0_DEST_CHANNEL_XYZ
, 0,
871 swizzle(tmp
, X
, X
, Z
, ONE
),
872 swizzle(tmp
, Z
, ONE
, ONE
, ONE
), 0);
876 get_result_vector(p
, &inst
->Dst
[0]),
877 A0_DEST_CHANNEL_X
, 0,
878 swizzle(tmp
, ONE
, Z
, Y
, X
),
879 i915_emit_const4fv(p
, scs_cos_constants
), 0);
883 case TGSI_OPCODE_SEQ
:
884 /* if we're both >= and <= then we're == */
885 src0
= src_vector(p
, &inst
->Src
[0], fs
);
886 src1
= src_vector(p
, &inst
->Src
[1], fs
);
887 tmp
= i915_get_utemp(p
);
891 tmp
, A0_DEST_CHANNEL_ALL
, 0,
897 get_result_vector(p
, &inst
->Dst
[0]),
898 A0_DEST_CHANNEL_ALL
, 0,
904 get_result_vector(p
, &inst
->Dst
[0]),
905 A0_DEST_CHANNEL_ALL
, 0,
906 get_result_vector(p
, &inst
->Dst
[0]),
911 case TGSI_OPCODE_SGE
:
912 emit_simple_arith(p
, inst
, A0_SGE
, 2, fs
);
915 case TGSI_OPCODE_SIN
:
916 src0
= src_vector(p
, &inst
->Src
[0], fs
);
917 tmp
= i915_get_utemp(p
);
921 tmp
, A0_DEST_CHANNEL_X
, 0,
922 src0
, i915_emit_const1f(p
, 1.0f
/ (float) (M_PI
* 2.0)), 0);
924 i915_emit_arith(p
, A0_MOD
, tmp
, A0_DEST_CHANNEL_X
, 0, tmp
, 0, 0);
927 * t0.xy = MUL x.xx11, x.x1111 ; x^2, x, 1, 1
928 * t0 = MUL t0.xyxy t0.xx11 ; x^4, x^3, x^2, x
929 * t1 = MUL t0.xyyw t0.yz11 ; x^7 x^5 x^3 x
930 * result = DP4 t1.wzyx, sin_constants
934 tmp
, A0_DEST_CHANNEL_XY
, 0,
935 swizzle(tmp
, X
, X
, ONE
, ONE
),
936 swizzle(tmp
, X
, ONE
, ONE
, ONE
), 0);
940 tmp
, A0_DEST_CHANNEL_ALL
, 0,
941 swizzle(tmp
, X
, Y
, X
, Y
),
942 swizzle(tmp
, X
, X
, ONE
, ONE
), 0);
946 tmp
, A0_DEST_CHANNEL_ALL
, 0,
947 swizzle(tmp
, X
, Y
, Y
, W
),
948 swizzle(tmp
, X
, Z
, ONE
, ONE
), 0);
952 get_result_vector(p
, &inst
->Dst
[0]),
953 get_result_flags(inst
), 0,
954 swizzle(tmp
, W
, Z
, Y
, X
),
955 i915_emit_const4fv(p
, sin_constants
), 0);
958 case TGSI_OPCODE_SLE
:
959 /* like SGE, but swap reg0, reg1 */
960 emit_simple_arith_swap2(p
, inst
, A0_SGE
, 2, fs
);
963 case TGSI_OPCODE_SLT
:
964 emit_simple_arith(p
, inst
, A0_SLT
, 2, fs
);
967 case TGSI_OPCODE_SGT
:
968 /* like SLT, but swap reg0, reg1 */
969 emit_simple_arith_swap2(p
, inst
, A0_SLT
, 2, fs
);
972 case TGSI_OPCODE_SNE
:
973 /* if we're < or > then we're != */
974 src0
= src_vector(p
, &inst
->Src
[0], fs
);
975 src1
= src_vector(p
, &inst
->Src
[1], fs
);
976 tmp
= i915_get_utemp(p
);
981 A0_DEST_CHANNEL_ALL
, 0,
987 get_result_vector(p
, &inst
->Dst
[0]),
988 A0_DEST_CHANNEL_ALL
, 0,
994 get_result_vector(p
, &inst
->Dst
[0]),
995 A0_DEST_CHANNEL_ALL
, 0,
996 get_result_vector(p
, &inst
->Dst
[0]),
1000 case TGSI_OPCODE_SSG
:
1001 /* compute (src>0) - (src<0) */
1002 src0
= src_vector(p
, &inst
->Src
[0], fs
);
1003 tmp
= i915_get_utemp(p
);
1008 A0_DEST_CHANNEL_ALL
, 0,
1010 swizzle(src0
, ZERO
, ZERO
, ZERO
, ZERO
), 0);
1014 get_result_vector(p
, &inst
->Dst
[0]),
1015 A0_DEST_CHANNEL_ALL
, 0,
1016 swizzle(src0
, ZERO
, ZERO
, ZERO
, ZERO
),
1021 get_result_vector(p
, &inst
->Dst
[0]),
1022 A0_DEST_CHANNEL_ALL
, 0,
1023 get_result_vector(p
, &inst
->Dst
[0]),
1024 negate(tmp
, 1, 1, 1, 1), 0);
1027 case TGSI_OPCODE_SUB
:
1028 src0
= src_vector(p
, &inst
->Src
[0], fs
);
1029 src1
= src_vector(p
, &inst
->Src
[1], fs
);
1033 get_result_vector(p
, &inst
->Dst
[0]),
1034 get_result_flags(inst
), 0,
1035 src0
, negate(src1
, 1, 1, 1, 1), 0);
1038 case TGSI_OPCODE_TEX
:
1039 emit_tex(p
, inst
, T0_TEXLD
, fs
);
1042 case TGSI_OPCODE_TRUNC
:
1043 emit_simple_arith(p
, inst
, A0_TRC
, 1, fs
);
1046 case TGSI_OPCODE_TXB
:
1047 emit_tex(p
, inst
, T0_TEXLDB
, fs
);
1050 case TGSI_OPCODE_TXP
:
1051 emit_tex(p
, inst
, T0_TEXLDP
, fs
);
1054 case TGSI_OPCODE_XPD
:
1056 * result.x = src0.y * src1.z - src0.z * src1.y;
1057 * result.y = src0.z * src1.x - src0.x * src1.z;
1058 * result.z = src0.x * src1.y - src0.y * src1.x;
1061 src0
= src_vector(p
, &inst
->Src
[0], fs
);
1062 src1
= src_vector(p
, &inst
->Src
[1], fs
);
1063 tmp
= i915_get_utemp(p
);
1067 tmp
, A0_DEST_CHANNEL_ALL
, 0,
1068 swizzle(src0
, Z
, X
, Y
, ONE
),
1069 swizzle(src1
, Y
, Z
, X
, ONE
), 0);
1073 get_result_vector(p
, &inst
->Dst
[0]),
1074 get_result_flags(inst
), 0,
1075 swizzle(src0
, Y
, Z
, X
, ONE
),
1076 swizzle(src1
, Z
, X
, Y
, ONE
),
1077 negate(tmp
, 1, 1, 1, 0));
1081 i915_program_error(p
, "bad opcode %d", inst
->Instruction
.Opcode
);
1086 i915_release_utemps(p
);
1090 static void i915_translate_token(struct i915_fp_compile
*p
,
1091 const union i915_full_token
* token
,
1092 struct i915_fragment_shader
*fs
)
1094 struct i915_fragment_shader
*ifs
= p
->shader
;
1095 switch( token
->Token
.Type
) {
1096 case TGSI_TOKEN_TYPE_PROPERTY
:
1098 * We only support one cbuf, but we still need to ignore the property
1099 * correctly so we don't hit the assert at the end of the switch case.
1101 assert(token
->FullProperty
.Property
.PropertyName
==
1102 TGSI_PROPERTY_FS_COLOR0_WRITES_ALL_CBUFS
);
1105 case TGSI_TOKEN_TYPE_DECLARATION
:
1106 if (token
->FullDeclaration
.Declaration
.File
1107 == TGSI_FILE_CONSTANT
) {
1109 for (i
= token
->FullDeclaration
.Range
.First
;
1110 i
<= token
->FullDeclaration
.Range
.Last
;
1112 assert(ifs
->constant_flags
[i
] == 0x0);
1113 ifs
->constant_flags
[i
] = I915_CONSTFLAG_USER
;
1114 ifs
->num_constants
= MAX2(ifs
->num_constants
, i
+ 1);
1117 else if (token
->FullDeclaration
.Declaration
.File
1118 == TGSI_FILE_TEMPORARY
) {
1120 for (i
= token
->FullDeclaration
.Range
.First
;
1121 i
<= token
->FullDeclaration
.Range
.Last
;
1123 if (i
>= I915_MAX_TEMPORARY
)
1124 debug_printf("Too many temps (%d)\n",i
);
1126 /* XXX just use shader->info->file_mask[TGSI_FILE_TEMPORARY] */
1127 p
->temp_flag
|= (1 << i
); /* mark temp as used */
1132 case TGSI_TOKEN_TYPE_IMMEDIATE
:
1134 const struct tgsi_full_immediate
*imm
1135 = &token
->FullImmediate
;
1136 const uint pos
= p
->num_immediates
++;
1138 assert( imm
->Immediate
.NrTokens
<= 4 + 1 );
1139 for (j
= 0; j
< imm
->Immediate
.NrTokens
- 1; j
++) {
1140 p
->immediates
[pos
][j
] = imm
->u
[j
].Float
;
1145 case TGSI_TOKEN_TYPE_INSTRUCTION
:
1146 if (p
->first_instruction
) {
1147 /* resolve location of immediates */
1149 for (i
= 0; i
< p
->num_immediates
; i
++) {
1150 /* find constant slot for this immediate */
1151 for (j
= 0; j
< I915_MAX_CONSTANT
; j
++) {
1152 if (ifs
->constant_flags
[j
] == 0x0) {
1153 memcpy(ifs
->constants
[j
],
1156 /*printf("immediate %d maps to const %d\n", i, j);*/
1157 ifs
->constant_flags
[j
] = 0xf; /* all four comps used */
1158 p
->immediates_map
[i
] = j
;
1159 ifs
->num_constants
= MAX2(ifs
->num_constants
, j
+ 1);
1165 p
->first_instruction
= FALSE
;
1168 i915_translate_instruction(p
, &token
->FullInstruction
, fs
);
1178 * Translate TGSI fragment shader into i915 hardware instructions.
1179 * \param p the translation state
1180 * \param tokens the TGSI token array
1183 i915_translate_instructions(struct i915_fp_compile
*p
,
1184 const struct i915_token_list
*tokens
,
1185 struct i915_fragment_shader
*fs
)
1188 for(i
= 0; i
<tokens
->NumTokens
; i
++) {
1189 i915_translate_token(p
, &tokens
->Tokens
[i
], fs
);
1194 static struct i915_fp_compile
*
1195 i915_init_compile(struct i915_context
*i915
,
1196 struct i915_fragment_shader
*ifs
)
1198 struct i915_fp_compile
*p
= CALLOC_STRUCT(i915_fp_compile
);
1203 /* Put new constants at end of const buffer, growing downward.
1204 * The problem is we don't know how many user-defined constants might
1205 * be specified with pipe->set_constant_buffer().
1206 * Should pre-scan the user's program to determine the highest-numbered
1207 * constant referenced.
1209 ifs
->num_constants
= 0;
1210 memset(ifs
->constant_flags
, 0, sizeof(ifs
->constant_flags
));
1212 memset(&p
->register_phases
, 0, sizeof(p
->register_phases
));
1214 for (i
= 0; i
< I915_TEX_UNITS
; i
++)
1215 ifs
->generic_mapping
[i
] = -1;
1217 p
->first_instruction
= TRUE
;
1219 p
->nr_tex_indirect
= 1; /* correct? */
1222 p
->nr_decl_insn
= 0;
1224 p
->csr
= p
->program
;
1225 p
->decl
= p
->declarations
;
1228 p
->temp_flag
= ~0x0 << I915_MAX_TEMPORARY
;
1229 p
->utemp_flag
= ~0x7;
1231 /* initialize the first program word */
1232 *(p
->decl
++) = _3DSTATE_PIXEL_SHADER_PROGRAM
;
1238 /* Copy compile results to the fragment program struct and destroy the
1239 * compilation context.
1242 i915_fini_compile(struct i915_context
*i915
, struct i915_fp_compile
*p
)
1244 struct i915_fragment_shader
*ifs
= p
->shader
;
1245 unsigned long program_size
= (unsigned long) (p
->csr
- p
->program
);
1246 unsigned long decl_size
= (unsigned long) (p
->decl
- p
->declarations
);
1248 if (p
->nr_tex_indirect
> I915_MAX_TEX_INDIRECT
)
1249 debug_printf("Exceeded max nr indirect texture lookups\n");
1251 if (p
->nr_tex_insn
> I915_MAX_TEX_INSN
)
1252 i915_program_error(p
, "Exceeded max TEX instructions");
1254 if (p
->nr_alu_insn
> I915_MAX_ALU_INSN
)
1255 i915_program_error(p
, "Exceeded max ALU instructions");
1257 if (p
->nr_decl_insn
> I915_MAX_DECL_INSN
)
1258 i915_program_error(p
, "Exceeded max DECL instructions");
1261 p
->NumNativeInstructions
= 0;
1262 p
->NumNativeAluInstructions
= 0;
1263 p
->NumNativeTexInstructions
= 0;
1264 p
->NumNativeTexIndirections
= 0;
1266 i915_use_passthrough_shader(ifs
);
1269 p
->NumNativeInstructions
1270 = p
->nr_alu_insn
+ p
->nr_tex_insn
+ p
->nr_decl_insn
;
1271 p
->NumNativeAluInstructions
= p
->nr_alu_insn
;
1272 p
->NumNativeTexInstructions
= p
->nr_tex_insn
;
1273 p
->NumNativeTexIndirections
= p
->nr_tex_indirect
;
1275 /* patch in the program length */
1276 p
->declarations
[0] |= program_size
+ decl_size
- 2;
1278 /* Copy compilation results to fragment program struct:
1281 assert(!ifs
->program
);
1284 = (uint
*) MALLOC(decl_size
* sizeof(uint
));
1286 = (uint
*) MALLOC(program_size
* sizeof(uint
));
1289 ifs
->decl_len
= decl_size
;
1293 decl_size
* sizeof(uint
));
1297 ifs
->program_len
= program_size
;
1299 memcpy(ifs
->program
,
1301 program_size
* sizeof(uint
));
1305 /* Release the compilation struct:
1315 * Rather than trying to intercept and jiggle depth writes during
1316 * emit, just move the value into its correct position at the end of
1320 i915_fixup_depth_write(struct i915_fp_compile
*p
)
1322 /* XXX assuming pos/depth is always in output[0] */
1323 if (p
->shader
->info
.output_semantic_name
[0] == TGSI_SEMANTIC_POSITION
) {
1324 const uint depth
= UREG(REG_TYPE_OD
, 0);
1327 A0_MOV
, /* opcode */
1328 depth
, /* dest reg */
1329 A0_DEST_CHANNEL_W
, /* write mask */
1331 swizzle(depth
, X
, Y
, Z
, Z
), /* src0 */
1332 0, 0 /* src1, src2 */);
1338 i915_translate_fragment_program( struct i915_context
*i915
,
1339 struct i915_fragment_shader
*fs
)
1341 struct i915_fp_compile
*p
;
1342 const struct tgsi_token
*tokens
= fs
->state
.tokens
;
1343 struct i915_token_list
* i_tokens
;
1346 tgsi_dump(tokens
, 0);
1349 /* hw doesn't seem to like empty frag programs, even when the depth write
1350 * fixup gets emitted below - may that one is fishy, too? */
1351 if (fs
->info
.num_instructions
== 1) {
1352 i915_use_passthrough_shader(fs
);
1357 p
= i915_init_compile(i915
, fs
);
1359 i_tokens
= i915_optimize(tokens
);
1360 i915_translate_instructions(p
, i_tokens
, fs
);
1361 i915_fixup_depth_write(p
);
1363 i915_fini_compile(i915
, p
);
1364 i915_optimize_free(i_tokens
);
1367 i915_disassemble_program(NULL
, fs
->program
, fs
->program_len
);