1 /**************************************************************************
3 * Copyright 2007-2008 Tungsten Graphics, Inc., Cedar Park, Texas.
5 * Copyright 2009-2010 VMware, Inc. All rights Reserved.
7 * Permission is hereby granted, free of charge, to any person obtaining a
8 * copy of this software and associated documentation files (the
9 * "Software"), to deal in the Software without restriction, including
10 * without limitation the rights to use, copy, modify, merge, publish,
11 * distribute, sub license, and/or sell copies of the Software, and to
12 * permit persons to whom the Software is furnished to do so, subject to
13 * the following conditions:
15 * The above copyright notice and this permission notice (including the
16 * next paragraph) shall be included in all copies or substantial portions
19 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
20 * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
21 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT.
22 * IN NO EVENT SHALL TUNGSTEN GRAPHICS AND/OR ITS SUPPLIERS BE LIABLE FOR
23 * ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
24 * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
25 * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
27 **************************************************************************/
29 #include "pipe/p_config.h"
31 #include "tgsi/tgsi_sse2.h"
33 #if defined(PIPE_ARCH_X86) && 0 /* See FIXME notes below */
35 #include "util/u_debug.h"
36 #include "pipe/p_shader_tokens.h"
37 #include "util/u_math.h"
38 #include "util/u_memory.h"
39 #if defined(PIPE_ARCH_SSE)
40 #include "util/u_sse.h"
42 #include "tgsi/tgsi_info.h"
43 #include "tgsi/tgsi_parse.h"
44 #include "tgsi/tgsi_util.h"
45 #include "tgsi/tgsi_dump.h"
46 #include "tgsi/tgsi_exec.h"
48 #include "rtasm/rtasm_x86sse.h"
52 * This costs about 100fps (close to 10%) in gears:
54 #define HIGH_PRECISION 1
59 #define FOR_EACH_CHANNEL( CHAN )\
60 for (CHAN = 0; CHAN < NUM_CHANNELS; CHAN++)
62 #define IS_DST0_CHANNEL_ENABLED( INST, CHAN )\
63 ((INST).Dst[0].Register.WriteMask & (1 << (CHAN)))
65 #define IF_IS_DST0_CHANNEL_ENABLED( INST, CHAN )\
66 if (IS_DST0_CHANNEL_ENABLED( INST, CHAN ))
68 #define FOR_EACH_DST0_ENABLED_CHANNEL( INST, CHAN )\
69 FOR_EACH_CHANNEL( CHAN )\
70 IF_IS_DST0_CHANNEL_ENABLED( INST, CHAN )
77 #define TEMP_ONE_I TGSI_EXEC_TEMP_ONE_I
78 #define TEMP_ONE_C TGSI_EXEC_TEMP_ONE_C
80 #define TEMP_R0 TGSI_EXEC_TEMP_R0
81 #define TEMP_ADDR TGSI_EXEC_TEMP_ADDR
82 #define TEMP_EXEC_MASK_I TGSI_EXEC_MASK_I
83 #define TEMP_EXEC_MASK_C TGSI_EXEC_MASK_C
87 * X86 utility functions.
96 (enum x86_reg_name
) xmm
);
100 * X86 register mapping helpers.
103 static struct x86_reg
104 get_const_base( void )
111 static struct x86_reg
112 get_machine_base( void )
119 static struct x86_reg
120 get_input_base( void )
122 /* FIXME: tgsi_exec_machine::Inputs is a pointer now! */
123 return x86_make_disp(
125 Offset(struct tgsi_exec_machine
, Inputs
) );
128 static struct x86_reg
129 get_output_base( void )
131 /* FIXME: tgsi_exec_machine::Ouputs is a pointer now! */
132 return x86_make_disp(
134 Offset(struct tgsi_exec_machine
, Outputs
) );
137 static struct x86_reg
138 get_temp_base( void )
140 return x86_make_disp(
142 Offset(struct tgsi_exec_machine
, Temps
) );
145 static struct x86_reg
146 get_coef_base( void )
153 static struct x86_reg
154 get_sampler_base( void )
161 static struct x86_reg
162 get_immediate_base( void )
169 static struct x86_reg
170 get_system_value_base( void )
172 return x86_make_disp(
174 Offset(struct tgsi_exec_machine
, SystemValue
) );
179 * Data access helpers.
183 static struct x86_reg
188 return x86_make_disp(
189 get_immediate_base(),
190 (vec
* 4 + chan
) * 4 );
193 static struct x86_reg
198 return x86_make_disp(
200 (vec
* 4 + chan
) * 4 );
203 static struct x86_reg
207 return x86_make_disp(
209 unit
* sizeof( struct tgsi_sampler
* ) );
212 static struct x86_reg
217 return x86_make_disp(
219 (vec
* 4 + chan
) * 16 );
222 static struct x86_reg
227 return x86_make_disp(
229 (vec
* 4 + chan
) * 16 );
232 static struct x86_reg
237 return x86_make_disp(
239 (vec
* 4 + chan
) * 16 );
242 static struct x86_reg
247 return x86_make_disp(
248 get_system_value_base(), /* base */
249 (vec
* 4 + chan
) * 4 ); /* byte offset from base */
252 static struct x86_reg
258 return x86_make_disp(
260 ((vec
* 3 + member
) * 4 + chan
) * 4 );
266 struct x86_function
*func
)
273 * Data fetch helpers.
277 * Copy a shader constant to xmm register
278 * \param xmm the destination xmm register
279 * \param vec the src const buffer index
280 * \param chan src channel to fetch (X, Y, Z or W)
284 struct x86_function
*func
,
293 /* 'vec' is the offset from the address register's value.
294 * We're loading CONST[ADDR+vec] into an xmm register.
296 struct x86_reg r0
= get_immediate_base();
297 struct x86_reg r1
= get_coef_base();
300 assert( indirectFile
== TGSI_FILE_ADDRESS
);
301 assert( indirectIndex
== 0 );
302 assert( r0
.mod
== mod_REG
);
303 assert( r1
.mod
== mod_REG
);
305 x86_push( func
, r0
);
306 x86_push( func
, r1
);
309 * Loop over the four pixels or vertices in the quad.
310 * Get the value of the address (offset) register for pixel/vertex[i],
311 * add it to the src offset and index into the constant buffer.
312 * Note that we're working on SOA data.
313 * If any of the pixel/vertex execution channels are unused their
314 * values will be garbage. It's very important that we don't use
315 * those garbage values as indexes into the constant buffer since
316 * that'll cause segfaults.
317 * The solution is to bitwise-AND the offset with the execution mask
318 * register whose values are either 0 or ~0.
319 * The caller must setup the execution mask register to indicate
320 * which channels are valid/alive before running the shader.
321 * The execution mask will also figure into loops and conditionals
324 for (i
= 0; i
< QUAD_SIZE
; i
++) {
325 /* r1 = address register[i] */
326 x86_mov( func
, r1
, x86_make_disp( get_temp( TEMP_ADDR
, CHAN_X
), i
* 4 ) );
327 /* r0 = execution mask[i] */
328 x86_mov( func
, r0
, x86_make_disp( get_temp( TEMP_EXEC_MASK_I
, TEMP_EXEC_MASK_C
), i
* 4 ) );
330 x86_and( func
, r1
, r0
);
331 /* r0 = 'vec', the offset */
332 x86_lea( func
, r0
, get_const( vec
, chan
) );
334 /* Quick hack to multiply r1 by 16 -- need to add SHL to rtasm.
336 x86_add( func
, r1
, r1
);
337 x86_add( func
, r1
, r1
);
338 x86_add( func
, r1
, r1
);
339 x86_add( func
, r1
, r1
);
341 x86_add( func
, r0
, r1
); /* r0 = r0 + r1 */
342 x86_mov( func
, r1
, x86_deref( r0
) );
343 x86_mov( func
, x86_make_disp( get_temp( TEMP_R0
, CHAN_X
), i
* 4 ), r1
);
352 get_temp( TEMP_R0
, CHAN_X
) );
355 /* 'vec' is the index into the src register file, such as TEMP[vec] */
361 get_const( vec
, chan
) );
366 SHUF( 0, 0, 0, 0 ) );
372 struct x86_function
*func
,
380 get_immediate( vec
, chan
) );
385 SHUF( 0, 0, 0, 0 ) );
390 * Copy a shader input to xmm register
391 * \param xmm the destination xmm register
392 * \param vec the src input attrib
393 * \param chan src channel to fetch (X, Y, Z or W)
397 struct x86_function
*func
,
405 get_input( vec
, chan
) );
409 * Store an xmm register to a shader output
410 * \param xmm the source xmm register
411 * \param vec the dest output attrib
412 * \param chan src dest channel to store (X, Y, Z or W)
416 struct x86_function
*func
,
423 get_output( vec
, chan
),
428 * Copy a shader temporary to xmm register
429 * \param xmm the destination xmm register
430 * \param vec the src temp register
431 * \param chan src channel to fetch (X, Y, Z or W)
435 struct x86_function
*func
,
443 get_temp( vec
, chan
) );
447 * Copy a system value to xmm register
448 * \param xmm the destination xmm register
449 * \param vec the source system value register
450 * \param chan src channel to fetch (X, Y, Z or W)
454 struct x86_function
*func
,
462 get_system_value( vec
, chan
) );
467 SHUF( 0, 0, 0, 0 ) );
471 * Load an xmm register with an input attrib coefficient (a0, dadx or dady)
472 * \param xmm the destination xmm register
473 * \param vec the src input/attribute coefficient index
474 * \param chan src channel to fetch (X, Y, Z or W)
475 * \param member 0=a0, 1=dadx, 2=dady
479 struct x86_function
*func
,
488 get_coef( vec
, chan
, member
) );
493 SHUF( 0, 0, 0, 0 ) );
497 * Data store helpers.
502 struct x86_function
*func
,
509 get_input( vec
, chan
),
515 struct x86_function
*func
,
522 get_temp( vec
, chan
),
528 struct x86_function
*func
,
538 vec
+ TGSI_EXEC_TEMP_ADDR
,
543 * Coefficent fetch helpers.
548 struct x86_function
*func
,
563 struct x86_function
*func
,
578 struct x86_function
*func
,
592 * Function call helpers.
596 * NOTE: In gcc, if the destination uses the SSE intrinsics, then it must be
597 * defined with __attribute__((force_align_arg_pointer)), as we do not guarantee
598 * that the stack pointer is 16 byte aligned, as expected.
602 struct x86_function
*func
,
603 unsigned xmm_save_mask
,
604 const struct x86_reg
*arg
,
606 void (PIPE_CDECL
*code
)() )
608 struct x86_reg ecx
= x86_make_reg( file_REG32
, reg_CX
);
613 x86_make_reg( file_REG32
, reg_AX
) );
616 x86_make_reg( file_REG32
, reg_CX
) );
619 x86_make_reg( file_REG32
, reg_DX
) );
621 /* Store XMM regs to the stack
623 for(i
= 0, n
= 0; i
< 8; ++i
)
624 if(xmm_save_mask
& (1 << i
))
629 x86_make_reg( file_REG32
, reg_SP
),
632 for(i
= 0, n
= 0; i
< 8; ++i
)
633 if(xmm_save_mask
& (1 << i
)) {
636 x86_make_disp( x86_make_reg( file_REG32
, reg_SP
), n
*16 ),
641 for (i
= 0; i
< nr_args
; i
++) {
642 /* Load the address of the buffer we use for passing arguments and
650 /* Push actual function arguments (currently just the pointer to
651 * the buffer above), and call the function:
653 x86_push( func
, ecx
);
656 x86_mov_reg_imm( func
, ecx
, (unsigned long) code
);
657 x86_call( func
, ecx
);
659 /* Pop the arguments (or just add an immediate to esp)
661 for (i
= 0; i
< nr_args
; i
++) {
665 /* Pop the saved XMM regs:
667 for(i
= 0, n
= 0; i
< 8; ++i
)
668 if(xmm_save_mask
& (1 << i
)) {
672 x86_make_disp( x86_make_reg( file_REG32
, reg_SP
), n
*16 ) );
678 x86_make_reg( file_REG32
, reg_SP
),
681 /* Restore GP registers in a reverse order.
685 x86_make_reg( file_REG32
, reg_DX
) );
688 x86_make_reg( file_REG32
, reg_CX
) );
691 x86_make_reg( file_REG32
, reg_AX
) );
695 emit_func_call_dst_src1(
696 struct x86_function
*func
,
700 void (PIPE_CDECL
*code
)() )
702 struct x86_reg store
= get_temp( TEMP_R0
, 0 );
703 unsigned xmm_mask
= ((1 << xmm_save
) - 1) & ~(1 << xmm_dst
);
705 /* Store our input parameters (in xmm regs) to the buffer we use
706 * for passing arguments. We will pass a pointer to this buffer as
707 * the actual function argument.
712 make_xmm( xmm_src0
) );
714 emit_func_call( func
,
728 emit_func_call_dst_src2(
729 struct x86_function
*func
,
734 void (PIPE_CDECL
*code
)() )
736 struct x86_reg store
= get_temp( TEMP_R0
, 0 );
737 unsigned xmm_mask
= ((1 << xmm_save
) - 1) & ~(1 << xmm_dst
);
739 /* Store two inputs to parameter buffer.
744 make_xmm( xmm_src0
) );
748 x86_make_disp( store
, 4 * sizeof(float) ),
749 make_xmm( xmm_src1
) );
754 emit_func_call( func
,
760 /* Retrieve the results:
772 #if defined(PIPE_ARCH_SSE)
775 * Fast SSE2 implementation of special math functions.
778 #define POLY0(x, c0) _mm_set1_ps(c0)
779 #define POLY1(x, c0, c1) _mm_add_ps(_mm_mul_ps(POLY0(x, c1), x), _mm_set1_ps(c0))
780 #define POLY2(x, c0, c1, c2) _mm_add_ps(_mm_mul_ps(POLY1(x, c1, c2), x), _mm_set1_ps(c0))
781 #define POLY3(x, c0, c1, c2, c3) _mm_add_ps(_mm_mul_ps(POLY2(x, c1, c2, c3), x), _mm_set1_ps(c0))
782 #define POLY4(x, c0, c1, c2, c3, c4) _mm_add_ps(_mm_mul_ps(POLY3(x, c1, c2, c3, c4), x), _mm_set1_ps(c0))
783 #define POLY5(x, c0, c1, c2, c3, c4, c5) _mm_add_ps(_mm_mul_ps(POLY4(x, c1, c2, c3, c4, c5), x), _mm_set1_ps(c0))
785 #define EXP_POLY_DEGREE 3
786 #define LOG_POLY_DEGREE 5
789 * See http://www.devmaster.net/forums/showthread.php?p=43580
795 __m128 fpart
, expipart
, expfpart
;
797 x
= _mm_min_ps(x
, _mm_set1_ps( 129.00000f
));
798 x
= _mm_max_ps(x
, _mm_set1_ps(-126.99999f
));
800 /* ipart = int(x - 0.5) */
801 ipart
= _mm_cvtps_epi32(_mm_sub_ps(x
, _mm_set1_ps(0.5f
)));
803 /* fpart = x - ipart */
804 fpart
= _mm_sub_ps(x
, _mm_cvtepi32_ps(ipart
));
806 /* expipart = (float) (1 << ipart) */
807 expipart
= _mm_castsi128_ps(_mm_slli_epi32(_mm_add_epi32(ipart
, _mm_set1_epi32(127)), 23));
809 /* minimax polynomial fit of 2**x, in range [-0.5, 0.5[ */
810 #if EXP_POLY_DEGREE == 5
811 expfpart
= POLY5(fpart
, 9.9999994e-1f
, 6.9315308e-1f
, 2.4015361e-1f
, 5.5826318e-2f
, 8.9893397e-3f
, 1.8775767e-3f
);
812 #elif EXP_POLY_DEGREE == 4
813 expfpart
= POLY4(fpart
, 1.0000026f
, 6.9300383e-1f
, 2.4144275e-1f
, 5.2011464e-2f
, 1.3534167e-2f
);
814 #elif EXP_POLY_DEGREE == 3
815 expfpart
= POLY3(fpart
, 9.9992520e-1f
, 6.9583356e-1f
, 2.2606716e-1f
, 7.8024521e-2f
);
816 #elif EXP_POLY_DEGREE == 2
817 expfpart
= POLY2(fpart
, 1.0017247f
, 6.5763628e-1f
, 3.3718944e-1f
);
822 return _mm_mul_ps(expipart
, expfpart
);
827 * See http://www.devmaster.net/forums/showthread.php?p=43580
832 __m128i expmask
= _mm_set1_epi32(0x7f800000);
833 __m128i mantmask
= _mm_set1_epi32(0x007fffff);
834 __m128 one
= _mm_set1_ps(1.0f
);
836 __m128i i
= _mm_castps_si128(x
);
838 /* exp = (float) exponent(x) */
839 __m128 exp
= _mm_cvtepi32_ps(_mm_sub_epi32(_mm_srli_epi32(_mm_and_si128(i
, expmask
), 23), _mm_set1_epi32(127)));
841 /* mant = (float) mantissa(x) */
842 __m128 mant
= _mm_or_ps(_mm_castsi128_ps(_mm_and_si128(i
, mantmask
)), one
);
846 /* Minimax polynomial fit of log2(x)/(x - 1), for x in range [1, 2[
847 * These coefficients can be generate with
848 * http://www.boost.org/doc/libs/1_36_0/libs/math/doc/sf_and_dist/html/math_toolkit/toolkit/internals2/minimax.html
850 #if LOG_POLY_DEGREE == 6
851 logmant
= POLY5(mant
, 3.11578814719469302614f
, -3.32419399085241980044f
, 2.59883907202499966007f
, -1.23152682416275988241f
, 0.318212422185251071475f
, -0.0344359067839062357313f
);
852 #elif LOG_POLY_DEGREE == 5
853 logmant
= POLY4(mant
, 2.8882704548164776201f
, -2.52074962577807006663f
, 1.48116647521213171641f
, -0.465725644288844778798f
, 0.0596515482674574969533f
);
854 #elif LOG_POLY_DEGREE == 4
855 logmant
= POLY3(mant
, 2.61761038894603480148f
, -1.75647175389045657003f
, 0.688243882994381274313f
, -0.107254423828329604454f
);
856 #elif LOG_POLY_DEGREE == 3
857 logmant
= POLY2(mant
, 2.28330284476918490682f
, -1.04913055217340124191f
, 0.204446009836232697516f
);
862 /* This effectively increases the polynomial degree by one, but ensures that log2(1) == 0*/
863 logmant
= _mm_mul_ps(logmant
, _mm_sub_ps(mant
, one
));
865 return _mm_add_ps(logmant
, exp
);
870 powf4(__m128 x
, __m128 y
)
872 return exp2f4(_mm_mul_ps(log2f4(x
), y
));
875 #endif /* PIPE_ARCH_SSE */
880 * Low-level instruction translators.
885 struct x86_function
*func
,
892 TGSI_EXEC_TEMP_7FFFFFFF_I
,
893 TGSI_EXEC_TEMP_7FFFFFFF_C
) );
898 struct x86_function
*func
,
905 make_xmm( xmm_src
) );
908 static void PIPE_CDECL
912 store
[0] = cosf( store
[0] );
913 store
[1] = cosf( store
[1] );
914 store
[2] = cosf( store
[2] );
915 store
[3] = cosf( store
[3] );
920 struct x86_function
*func
,
924 emit_func_call_dst_src1(
932 static void PIPE_CDECL
933 #if defined(PIPE_CC_GCC) && defined(PIPE_ARCH_SSE)
934 __attribute__((force_align_arg_pointer
))
939 #if defined(PIPE_ARCH_SSE)
940 _mm_store_ps(&store
[0], exp2f4( _mm_load_ps(&store
[0]) ));
942 store
[0] = util_fast_exp2( store
[0] );
943 store
[1] = util_fast_exp2( store
[1] );
944 store
[2] = util_fast_exp2( store
[2] );
945 store
[3] = util_fast_exp2( store
[3] );
951 struct x86_function
*func
,
955 emit_func_call_dst_src1(
965 struct x86_function
*func
,
976 struct x86_function
*func
,
985 static void PIPE_CDECL
989 store
[0] = floorf( store
[0] );
990 store
[1] = floorf( store
[1] );
991 store
[2] = floorf( store
[2] );
992 store
[3] = floorf( store
[3] );
997 struct x86_function
*func
,
1001 emit_func_call_dst_src1(
1009 static void PIPE_CDECL
1013 store
[0] -= floorf( store
[0] );
1014 store
[1] -= floorf( store
[1] );
1015 store
[2] -= floorf( store
[2] );
1016 store
[3] -= floorf( store
[3] );
1021 struct x86_function
*func
,
1025 emit_func_call_dst_src1(
1033 static void PIPE_CDECL
1034 #if defined(PIPE_CC_GCC) && defined(PIPE_ARCH_SSE)
1035 __attribute__((force_align_arg_pointer
))
1040 #if defined(PIPE_ARCH_SSE)
1041 _mm_store_ps(&store
[0], log2f4( _mm_load_ps(&store
[0]) ));
1043 store
[0] = util_fast_log2( store
[0] );
1044 store
[1] = util_fast_log2( store
[1] );
1045 store
[2] = util_fast_log2( store
[2] );
1046 store
[3] = util_fast_log2( store
[3] );
1052 struct x86_function
*func
,
1056 emit_func_call_dst_src1(
1066 struct x86_function
*func
,
1072 make_xmm( xmm_dst
),
1073 make_xmm( xmm_src
) );
1077 emit_mul (struct x86_function
*func
,
1083 make_xmm( xmm_dst
),
1084 make_xmm( xmm_src
) );
1089 struct x86_function
*func
,
1096 TGSI_EXEC_TEMP_80000000_I
,
1097 TGSI_EXEC_TEMP_80000000_C
) );
1100 static void PIPE_CDECL
1101 #if defined(PIPE_CC_GCC) && defined(PIPE_ARCH_SSE)
1102 __attribute__((force_align_arg_pointer
))
1107 #if defined(PIPE_ARCH_SSE)
1108 _mm_store_ps(&store
[0], powf4( _mm_load_ps(&store
[0]), _mm_load_ps(&store
[4]) ));
1110 store
[0] = util_fast_pow( store
[0], store
[4] );
1111 store
[1] = util_fast_pow( store
[1], store
[5] );
1112 store
[2] = util_fast_pow( store
[2], store
[6] );
1113 store
[3] = util_fast_pow( store
[3], store
[7] );
1119 struct x86_function
*func
,
1125 emit_func_call_dst_src2(
1136 struct x86_function
*func
,
1140 /* On Intel CPUs at least, this is only accurate to 12 bits -- not
1141 * good enough. Need to either emit a proper divide or use the
1142 * iterative technique described below in emit_rsqrt().
1146 make_xmm( xmm_dst
),
1147 make_xmm( xmm_src
) );
1150 static void PIPE_CDECL
1154 store
[0] = floorf( store
[0] + 0.5f
);
1155 store
[1] = floorf( store
[1] + 0.5f
);
1156 store
[2] = floorf( store
[2] + 0.5f
);
1157 store
[3] = floorf( store
[3] + 0.5f
);
1162 struct x86_function
*func
,
1166 emit_func_call_dst_src1(
1176 struct x86_function
*func
,
1181 /* Although rsqrtps() and rcpps() are low precision on some/all SSE
1182 * implementations, it is possible to improve its precision at
1183 * fairly low cost, using a newton/raphson step, as below:
1185 * x1 = 2 * rcpps(a) - a * rcpps(a) * rcpps(a)
1186 * x1 = 0.5 * rsqrtps(a) * [3.0 - (a * rsqrtps(a))* rsqrtps(a)]
1188 * See: http://softwarecommunity.intel.com/articles/eng/1818.htm
1191 struct x86_reg dst
= make_xmm( xmm_dst
);
1192 struct x86_reg src
= make_xmm( xmm_src
);
1193 struct x86_reg tmp0
= make_xmm( 2 );
1194 struct x86_reg tmp1
= make_xmm( 3 );
1196 assert( xmm_dst
!= xmm_src
);
1197 assert( xmm_dst
!= 2 && xmm_dst
!= 3 );
1198 assert( xmm_src
!= 2 && xmm_src
!= 3 );
1200 sse_movaps( func
, dst
, get_temp( TGSI_EXEC_TEMP_HALF_I
, TGSI_EXEC_TEMP_HALF_C
) );
1201 sse_movaps( func
, tmp0
, get_temp( TGSI_EXEC_TEMP_THREE_I
, TGSI_EXEC_TEMP_THREE_C
) );
1202 sse_rsqrtps( func
, tmp1
, src
);
1203 sse_mulps( func
, src
, tmp1
);
1204 sse_mulps( func
, dst
, tmp1
);
1205 sse_mulps( func
, src
, tmp1
);
1206 sse_subps( func
, tmp0
, src
);
1207 sse_mulps( func
, dst
, tmp0
);
1210 /* On Intel CPUs at least, this is only accurate to 12 bits -- not
1215 make_xmm( xmm_dst
),
1216 make_xmm( xmm_src
) );
1222 struct x86_function
*func
,
1229 TGSI_EXEC_TEMP_80000000_I
,
1230 TGSI_EXEC_TEMP_80000000_C
) );
1233 static void PIPE_CDECL
1237 store
[0] = store
[0] < 0.0f
? -1.0f
: store
[0] > 0.0f
? 1.0f
: 0.0f
;
1238 store
[1] = store
[1] < 0.0f
? -1.0f
: store
[1] > 0.0f
? 1.0f
: 0.0f
;
1239 store
[2] = store
[2] < 0.0f
? -1.0f
: store
[2] > 0.0f
? 1.0f
: 0.0f
;
1240 store
[3] = store
[3] < 0.0f
? -1.0f
: store
[3] > 0.0f
? 1.0f
: 0.0f
;
1245 struct x86_function
*func
,
1249 emit_func_call_dst_src1(
1257 static void PIPE_CDECL
1261 store
[0] = sinf( store
[0] );
1262 store
[1] = sinf( store
[1] );
1263 store
[2] = sinf( store
[2] );
1264 store
[3] = sinf( store
[3] );
1268 emit_sin (struct x86_function
*func
,
1272 emit_func_call_dst_src1(
1282 struct x86_function
*func
,
1288 make_xmm( xmm_dst
),
1289 make_xmm( xmm_src
) );
1297 struct x86_function
*func
,
1299 const struct tgsi_full_src_register
*reg
,
1300 const unsigned chan_index
)
1302 unsigned swizzle
= tgsi_util_get_full_src_register_swizzle( reg
, chan_index
);
1305 case TGSI_SWIZZLE_X
:
1306 case TGSI_SWIZZLE_Y
:
1307 case TGSI_SWIZZLE_Z
:
1308 case TGSI_SWIZZLE_W
:
1309 switch (reg
->Register
.File
) {
1310 case TGSI_FILE_CONSTANT
:
1314 reg
->Register
.Index
,
1316 reg
->Register
.Indirect
,
1318 reg
->Indirect
.Index
);
1321 case TGSI_FILE_IMMEDIATE
:
1325 reg
->Register
.Index
,
1329 case TGSI_FILE_SYSTEM_VALUE
:
1333 reg
->Register
.Index
,
1337 case TGSI_FILE_INPUT
:
1341 reg
->Register
.Index
,
1345 case TGSI_FILE_TEMPORARY
:
1349 reg
->Register
.Index
,
1362 switch( tgsi_util_get_full_src_register_sign_mode( reg
, chan_index
) ) {
1363 case TGSI_UTIL_SIGN_CLEAR
:
1364 emit_abs( func
, xmm
);
1367 case TGSI_UTIL_SIGN_SET
:
1368 emit_setsign( func
, xmm
);
1371 case TGSI_UTIL_SIGN_TOGGLE
:
1372 emit_neg( func
, xmm
);
1375 case TGSI_UTIL_SIGN_KEEP
:
1380 #define FETCH( FUNC, INST, XMM, INDEX, CHAN )\
1381 emit_fetch( FUNC, XMM, &(INST).Src[INDEX], CHAN )
1388 struct x86_function
*func
,
1390 const struct tgsi_full_dst_register
*reg
,
1391 const struct tgsi_full_instruction
*inst
,
1392 unsigned chan_index
)
1394 switch( inst
->Instruction
.Saturate
) {
1398 case TGSI_SAT_ZERO_ONE
:
1403 TGSI_EXEC_TEMP_00000000_I
,
1404 TGSI_EXEC_TEMP_00000000_C
) );
1410 TGSI_EXEC_TEMP_ONE_I
,
1411 TGSI_EXEC_TEMP_ONE_C
) );
1414 case TGSI_SAT_MINUS_PLUS_ONE
:
1420 switch( reg
->Register
.File
) {
1421 case TGSI_FILE_OUTPUT
:
1425 reg
->Register
.Index
,
1429 case TGSI_FILE_TEMPORARY
:
1433 reg
->Register
.Index
,
1437 case TGSI_FILE_ADDRESS
:
1441 reg
->Register
.Index
,
1450 #define STORE( FUNC, INST, XMM, INDEX, CHAN )\
1451 emit_store( FUNC, XMM, &(INST).Dst[INDEX], &(INST), CHAN )
1454 static void PIPE_CDECL
1455 fetch_texel( struct tgsi_sampler
**sampler
,
1461 debug_printf("%s sampler: %p (%p) store: %p\n",
1466 for (j
= 0; j
< 4; j
++)
1467 debug_printf("sample %d texcoord %f %f %f lodbias %f\n",
1476 float rgba
[NUM_CHANNELS
][QUAD_SIZE
];
1477 (*sampler
)->get_samples(*sampler
,
1481 &store
[12], /* lodbias */
1482 tgsi_sampler_lod_bias
,
1483 rgba
); /* results */
1485 memcpy( store
, rgba
, 16 * sizeof(float));
1489 for (j
= 0; j
< 4; j
++)
1490 debug_printf("sample %d result %f %f %f %f\n",
1500 * High-level instruction translators.
1503 emit_tex( struct x86_function
*func
,
1504 const struct tgsi_full_instruction
*inst
,
1508 const uint unit
= inst
->Src
[1].Register
.Index
;
1509 struct x86_reg args
[2];
1513 assert(inst
->Instruction
.Texture
);
1514 switch (inst
->Texture
.Texture
) {
1515 case TGSI_TEXTURE_1D
:
1518 case TGSI_TEXTURE_2D
:
1519 case TGSI_TEXTURE_RECT
:
1520 case TGSI_TEXTURE_1D_ARRAY
:
1523 case TGSI_TEXTURE_SHADOW1D
:
1524 case TGSI_TEXTURE_SHADOW2D
:
1525 case TGSI_TEXTURE_SHADOWRECT
:
1526 case TGSI_TEXTURE_3D
:
1527 case TGSI_TEXTURE_CUBE
:
1528 case TGSI_TEXTURE_2D_ARRAY
:
1537 FETCH( func
, *inst
, 3, 0, 3 );
1543 TGSI_EXEC_TEMP_00000000_I
,
1544 TGSI_EXEC_TEMP_00000000_C
);
1548 /* store lodbias whether enabled or not -- fetch_texel currently
1549 * respects it always.
1552 get_temp( TEMP_R0
, 3 ),
1556 FETCH( func
, *inst
, 3, 0, 3 );
1558 emit_rcp( func
, 3, 3 );
1561 for (i
= 0; i
< count
; i
++) {
1562 FETCH( func
, *inst
, i
, 0, i
);
1571 /* Store in the argument buffer:
1575 get_temp( TEMP_R0
, i
),
1579 args
[0] = get_temp( TEMP_R0
, 0 );
1580 args
[1] = get_sampler_ptr( unit
);
1582 emit_func_call( func
,
1588 /* If all four channels are enabled, could use a pointer to
1589 * dst[0].x instead of TEMP_R0 for store?
1591 FOR_EACH_DST0_ENABLED_CHANNEL( *inst
, i
) {
1596 get_temp( TEMP_R0
, i
) );
1598 STORE( func
, *inst
, 0, 0, i
);
1605 struct x86_function
*func
,
1606 const struct tgsi_full_src_register
*reg
)
1608 unsigned uniquemask
;
1609 unsigned unique_count
= 0;
1610 unsigned chan_index
;
1613 /* This mask stores component bits that were already tested. Note that
1614 * we test if the value is less than zero, so 1.0 and 0.0 need not to be
1619 FOR_EACH_CHANNEL( chan_index
) {
1622 /* unswizzle channel */
1623 swizzle
= tgsi_util_get_full_src_register_swizzle(
1627 /* check if the component has not been already tested */
1628 if( !(uniquemask
& (1 << swizzle
)) ) {
1629 uniquemask
|= 1 << swizzle
;
1631 /* allocate register */
1642 x86_make_reg( file_REG32
, reg_AX
) );
1645 x86_make_reg( file_REG32
, reg_DX
) );
1647 for (i
= 0 ; i
< unique_count
; i
++ ) {
1648 struct x86_reg dataXMM
= make_xmm(i
);
1654 TGSI_EXEC_TEMP_00000000_I
,
1655 TGSI_EXEC_TEMP_00000000_C
),
1661 x86_make_reg( file_REG32
, reg_AX
),
1667 x86_make_reg( file_REG32
, reg_DX
),
1671 x86_make_reg( file_REG32
, reg_AX
),
1672 x86_make_reg( file_REG32
, reg_DX
) );
1679 TGSI_EXEC_TEMP_KILMASK_I
,
1680 TGSI_EXEC_TEMP_KILMASK_C
),
1681 x86_make_reg( file_REG32
, reg_AX
) );
1685 x86_make_reg( file_REG32
, reg_DX
) );
1688 x86_make_reg( file_REG32
, reg_AX
) );
1694 struct x86_function
*func
)
1696 /* XXX todo / fix me */
1702 struct x86_function
*func
,
1703 struct tgsi_full_instruction
*inst
,
1706 unsigned chan_index
;
1708 FOR_EACH_DST0_ENABLED_CHANNEL( *inst
, chan_index
) {
1709 FETCH( func
, *inst
, 0, 0, chan_index
);
1710 FETCH( func
, *inst
, 1, 1, chan_index
);
1722 STORE( func
, *inst
, 0, 0, chan_index
);
1728 struct x86_function
*func
,
1729 struct tgsi_full_instruction
*inst
)
1731 unsigned chan_index
;
1733 FOR_EACH_DST0_ENABLED_CHANNEL( *inst
, chan_index
) {
1734 FETCH( func
, *inst
, 0, 0, chan_index
);
1735 FETCH( func
, *inst
, 1, 1, chan_index
);
1736 FETCH( func
, *inst
, 2, 2, chan_index
);
1741 TGSI_EXEC_TEMP_00000000_I
,
1742 TGSI_EXEC_TEMP_00000000_C
),
1756 STORE( func
, *inst
, 0, 0, chan_index
);
1762 * Check if inst src/dest regs use indirect addressing into temporary,
1763 * input or output register files.
1766 indirect_reg_reference(const struct tgsi_full_instruction
*inst
)
1769 for (i
= 0; i
< inst
->Instruction
.NumSrcRegs
; i
++) {
1770 const struct tgsi_full_src_register
*reg
= &inst
->Src
[i
];
1771 if ((reg
->Register
.File
== TGSI_FILE_TEMPORARY
||
1772 reg
->Register
.File
== TGSI_FILE_INPUT
||
1773 reg
->Register
.File
== TGSI_FILE_OUTPUT
) &&
1774 reg
->Register
.Indirect
)
1777 for (i
= 0; i
< inst
->Instruction
.NumDstRegs
; i
++) {
1778 const struct tgsi_full_dst_register
*reg
= &inst
->Dst
[i
];
1779 if ((reg
->Register
.File
== TGSI_FILE_TEMPORARY
||
1780 reg
->Register
.File
== TGSI_FILE_INPUT
||
1781 reg
->Register
.File
== TGSI_FILE_OUTPUT
) &&
1782 reg
->Register
.Indirect
)
1791 struct x86_function
*func
,
1792 struct tgsi_full_instruction
*inst
)
1794 unsigned chan_index
;
1796 /* we can't handle indirect addressing into temp register file yet */
1797 if (indirect_reg_reference(inst
))
1800 switch (inst
->Instruction
.Opcode
) {
1801 case TGSI_OPCODE_ARL
:
1802 FOR_EACH_DST0_ENABLED_CHANNEL( *inst
, chan_index
) {
1803 FETCH( func
, *inst
, 0, 0, chan_index
);
1804 emit_flr(func
, 0, 0);
1805 emit_f2it( func
, 0 );
1806 STORE( func
, *inst
, 0, 0, chan_index
);
1810 case TGSI_OPCODE_MOV
:
1811 FOR_EACH_DST0_ENABLED_CHANNEL( *inst
, chan_index
) {
1812 FETCH( func
, *inst
, 4 + chan_index
, 0, chan_index
);
1814 FOR_EACH_DST0_ENABLED_CHANNEL( *inst
, chan_index
) {
1815 STORE( func
, *inst
, 4 + chan_index
, 0, chan_index
);
1819 case TGSI_OPCODE_LIT
:
1820 if( IS_DST0_CHANNEL_ENABLED( *inst
, CHAN_X
) ||
1821 IS_DST0_CHANNEL_ENABLED( *inst
, CHAN_W
) ) {
1827 if( IS_DST0_CHANNEL_ENABLED( *inst
, CHAN_X
) ) {
1828 STORE( func
, *inst
, 0, 0, CHAN_X
);
1830 if( IS_DST0_CHANNEL_ENABLED( *inst
, CHAN_W
) ) {
1831 STORE( func
, *inst
, 0, 0, CHAN_W
);
1834 if( IS_DST0_CHANNEL_ENABLED( *inst
, CHAN_Y
) ||
1835 IS_DST0_CHANNEL_ENABLED( *inst
, CHAN_Z
) ) {
1836 if( IS_DST0_CHANNEL_ENABLED( *inst
, CHAN_Y
) ) {
1837 FETCH( func
, *inst
, 0, 0, CHAN_X
);
1842 TGSI_EXEC_TEMP_00000000_I
,
1843 TGSI_EXEC_TEMP_00000000_C
) );
1844 STORE( func
, *inst
, 0, 0, CHAN_Y
);
1846 if( IS_DST0_CHANNEL_ENABLED( *inst
, CHAN_Z
) ) {
1847 /* XMM[1] = SrcReg[0].yyyy */
1848 FETCH( func
, *inst
, 1, 0, CHAN_Y
);
1849 /* XMM[1] = max(XMM[1], 0) */
1854 TGSI_EXEC_TEMP_00000000_I
,
1855 TGSI_EXEC_TEMP_00000000_C
) );
1856 /* XMM[2] = SrcReg[0].wwww */
1857 FETCH( func
, *inst
, 2, 0, CHAN_W
);
1858 /* XMM[2] = min(XMM[2], 128.0) */
1863 TGSI_EXEC_TEMP_128_I
,
1864 TGSI_EXEC_TEMP_128_C
) );
1865 /* XMM[2] = max(XMM[2], -128.0) */
1870 TGSI_EXEC_TEMP_MINUS_128_I
,
1871 TGSI_EXEC_TEMP_MINUS_128_C
) );
1872 emit_pow( func
, 3, 1, 1, 2 );
1873 FETCH( func
, *inst
, 0, 0, CHAN_X
);
1887 STORE( func
, *inst
, 2, 0, CHAN_Z
);
1892 case TGSI_OPCODE_RCP
:
1893 FETCH( func
, *inst
, 0, 0, CHAN_X
);
1894 emit_rcp( func
, 0, 0 );
1895 FOR_EACH_DST0_ENABLED_CHANNEL( *inst
, chan_index
) {
1896 STORE( func
, *inst
, 0, 0, chan_index
);
1900 case TGSI_OPCODE_RSQ
:
1901 FETCH( func
, *inst
, 0, 0, CHAN_X
);
1902 emit_abs( func
, 0 );
1903 emit_rsqrt( func
, 1, 0 );
1904 FOR_EACH_DST0_ENABLED_CHANNEL( *inst
, chan_index
) {
1905 STORE( func
, *inst
, 1, 0, chan_index
);
1909 case TGSI_OPCODE_EXP
:
1910 if (IS_DST0_CHANNEL_ENABLED( *inst
, CHAN_X
) ||
1911 IS_DST0_CHANNEL_ENABLED( *inst
, CHAN_Y
) ||
1912 IS_DST0_CHANNEL_ENABLED( *inst
, CHAN_Z
)) {
1913 FETCH( func
, *inst
, 0, 0, CHAN_X
);
1914 if (IS_DST0_CHANNEL_ENABLED( *inst
, CHAN_X
) ||
1915 IS_DST0_CHANNEL_ENABLED( *inst
, CHAN_Y
)) {
1916 emit_MOV( func
, 1, 0 );
1917 emit_flr( func
, 2, 1 );
1918 /* dst.x = ex2(floor(src.x)) */
1919 if (IS_DST0_CHANNEL_ENABLED( *inst
, CHAN_X
)) {
1920 emit_MOV( func
, 2, 1 );
1921 emit_ex2( func
, 3, 2 );
1922 STORE( func
, *inst
, 2, 0, CHAN_X
);
1924 /* dst.y = src.x - floor(src.x) */
1925 if (IS_DST0_CHANNEL_ENABLED( *inst
, CHAN_Y
)) {
1926 emit_MOV( func
, 2, 0 );
1927 emit_sub( func
, 2, 1 );
1928 STORE( func
, *inst
, 2, 0, CHAN_Y
);
1931 /* dst.z = ex2(src.x) */
1932 if (IS_DST0_CHANNEL_ENABLED( *inst
, CHAN_Z
)) {
1933 emit_ex2( func
, 3, 0 );
1934 STORE( func
, *inst
, 0, 0, CHAN_Z
);
1938 if (IS_DST0_CHANNEL_ENABLED( *inst
, CHAN_W
)) {
1939 emit_tempf( func
, 0, TEMP_ONE_I
, TEMP_ONE_C
);
1940 STORE( func
, *inst
, 0, 0, CHAN_W
);
1944 case TGSI_OPCODE_LOG
:
1945 if (IS_DST0_CHANNEL_ENABLED( *inst
, CHAN_X
) ||
1946 IS_DST0_CHANNEL_ENABLED( *inst
, CHAN_Y
) ||
1947 IS_DST0_CHANNEL_ENABLED( *inst
, CHAN_Z
)) {
1948 FETCH( func
, *inst
, 0, 0, CHAN_X
);
1949 emit_abs( func
, 0 );
1950 emit_MOV( func
, 1, 0 );
1951 emit_lg2( func
, 2, 1 );
1952 /* dst.z = lg2(abs(src.x)) */
1953 if (IS_DST0_CHANNEL_ENABLED( *inst
, CHAN_Z
)) {
1954 STORE( func
, *inst
, 1, 0, CHAN_Z
);
1956 if (IS_DST0_CHANNEL_ENABLED( *inst
, CHAN_X
) ||
1957 IS_DST0_CHANNEL_ENABLED( *inst
, CHAN_Y
)) {
1958 emit_flr( func
, 2, 1 );
1959 /* dst.x = floor(lg2(abs(src.x))) */
1960 if (IS_DST0_CHANNEL_ENABLED( *inst
, CHAN_X
)) {
1961 STORE( func
, *inst
, 1, 0, CHAN_X
);
1963 /* dst.x = abs(src)/ex2(floor(lg2(abs(src.x)))) */
1964 if (IS_DST0_CHANNEL_ENABLED( *inst
, CHAN_Y
)) {
1965 emit_ex2( func
, 2, 1 );
1966 emit_rcp( func
, 1, 1 );
1967 emit_mul( func
, 0, 1 );
1968 STORE( func
, *inst
, 0, 0, CHAN_Y
);
1973 if (IS_DST0_CHANNEL_ENABLED( *inst
, CHAN_W
)) {
1974 emit_tempf( func
, 0, TEMP_ONE_I
, TEMP_ONE_C
);
1975 STORE( func
, *inst
, 0, 0, CHAN_W
);
1979 case TGSI_OPCODE_MUL
:
1980 /* do all fetches and adds, storing results in temp regs */
1981 FOR_EACH_DST0_ENABLED_CHANNEL( *inst
, chan_index
) {
1982 int r
= chan_index
+ 1;
1983 FETCH( func
, *inst
, 0, 0, chan_index
); /* load xmm[0] */
1984 FETCH( func
, *inst
, r
, 1, chan_index
); /* load xmm[r] */
1985 emit_mul( func
, r
, 0 ); /* xmm[r] = xmm[r] * xmm[0] */
1987 /* do all stores of the temp regs */
1988 FOR_EACH_DST0_ENABLED_CHANNEL( *inst
, chan_index
) {
1989 int r
= chan_index
+ 1;
1990 STORE( func
, *inst
, r
, 0, chan_index
); /* store xmm[r] */
1994 case TGSI_OPCODE_ADD
:
1995 /* do all fetches and adds, storing results in temp regs */
1996 FOR_EACH_DST0_ENABLED_CHANNEL( *inst
, chan_index
) {
1997 int r
= chan_index
+ 1;
1998 FETCH( func
, *inst
, 0, 0, chan_index
); /* load xmm[0] */
1999 FETCH( func
, *inst
, r
, 1, chan_index
); /* load xmm[r] */
2000 emit_add( func
, r
, 0 ); /* xmm[r] = xmm[r] + xmm[0] */
2002 /* do all stores of the temp regs */
2003 FOR_EACH_DST0_ENABLED_CHANNEL( *inst
, chan_index
) {
2004 int r
= chan_index
+ 1;
2005 STORE( func
, *inst
, r
, 0, chan_index
); /* store xmm[r] */
2009 case TGSI_OPCODE_DP3
:
2010 FETCH( func
, *inst
, 0, 0, CHAN_X
);
2011 FETCH( func
, *inst
, 1, 1, CHAN_X
);
2012 emit_mul( func
, 0, 1 );
2013 FETCH( func
, *inst
, 1, 0, CHAN_Y
);
2014 FETCH( func
, *inst
, 2, 1, CHAN_Y
);
2015 emit_mul( func
, 1, 2 );
2016 emit_add( func
, 0, 1 );
2017 FETCH( func
, *inst
, 1, 0, CHAN_Z
);
2018 FETCH( func
, *inst
, 2, 1, CHAN_Z
);
2019 emit_mul( func
, 1, 2 );
2020 emit_add( func
, 0, 1 );
2021 FOR_EACH_DST0_ENABLED_CHANNEL( *inst
, chan_index
) {
2022 STORE( func
, *inst
, 0, 0, chan_index
);
2026 case TGSI_OPCODE_DP4
:
2027 FETCH( func
, *inst
, 0, 0, CHAN_X
);
2028 FETCH( func
, *inst
, 1, 1, CHAN_X
);
2029 emit_mul( func
, 0, 1 );
2030 FETCH( func
, *inst
, 1, 0, CHAN_Y
);
2031 FETCH( func
, *inst
, 2, 1, CHAN_Y
);
2032 emit_mul( func
, 1, 2 );
2033 emit_add( func
, 0, 1 );
2034 FETCH( func
, *inst
, 1, 0, CHAN_Z
);
2035 FETCH( func
, *inst
, 2, 1, CHAN_Z
);
2036 emit_mul(func
, 1, 2 );
2037 emit_add(func
, 0, 1 );
2038 FETCH( func
, *inst
, 1, 0, CHAN_W
);
2039 FETCH( func
, *inst
, 2, 1, CHAN_W
);
2040 emit_mul( func
, 1, 2 );
2041 emit_add( func
, 0, 1 );
2042 FOR_EACH_DST0_ENABLED_CHANNEL( *inst
, chan_index
) {
2043 STORE( func
, *inst
, 0, 0, chan_index
);
2047 case TGSI_OPCODE_DST
:
2048 IF_IS_DST0_CHANNEL_ENABLED( *inst
, CHAN_X
) {
2054 STORE( func
, *inst
, 0, 0, CHAN_X
);
2056 IF_IS_DST0_CHANNEL_ENABLED( *inst
, CHAN_Y
) {
2057 FETCH( func
, *inst
, 0, 0, CHAN_Y
);
2058 FETCH( func
, *inst
, 1, 1, CHAN_Y
);
2059 emit_mul( func
, 0, 1 );
2060 STORE( func
, *inst
, 0, 0, CHAN_Y
);
2062 IF_IS_DST0_CHANNEL_ENABLED( *inst
, CHAN_Z
) {
2063 FETCH( func
, *inst
, 0, 0, CHAN_Z
);
2064 STORE( func
, *inst
, 0, 0, CHAN_Z
);
2066 IF_IS_DST0_CHANNEL_ENABLED( *inst
, CHAN_W
) {
2067 FETCH( func
, *inst
, 0, 1, CHAN_W
);
2068 STORE( func
, *inst
, 0, 0, CHAN_W
);
2072 case TGSI_OPCODE_MIN
:
2073 FOR_EACH_DST0_ENABLED_CHANNEL( *inst
, chan_index
) {
2074 FETCH( func
, *inst
, 0, 0, chan_index
);
2075 FETCH( func
, *inst
, 1, 1, chan_index
);
2080 STORE( func
, *inst
, 0, 0, chan_index
);
2084 case TGSI_OPCODE_MAX
:
2085 FOR_EACH_DST0_ENABLED_CHANNEL( *inst
, chan_index
) {
2086 FETCH( func
, *inst
, 0, 0, chan_index
);
2087 FETCH( func
, *inst
, 1, 1, chan_index
);
2092 STORE( func
, *inst
, 0, 0, chan_index
);
2096 case TGSI_OPCODE_SLT
:
2097 emit_setcc( func
, inst
, cc_LessThan
);
2100 case TGSI_OPCODE_SGE
:
2101 emit_setcc( func
, inst
, cc_NotLessThan
);
2104 case TGSI_OPCODE_MAD
:
2105 FOR_EACH_DST0_ENABLED_CHANNEL( *inst
, chan_index
) {
2106 FETCH( func
, *inst
, 0, 0, chan_index
);
2107 FETCH( func
, *inst
, 1, 1, chan_index
);
2108 FETCH( func
, *inst
, 2, 2, chan_index
);
2109 emit_mul( func
, 0, 1 );
2110 emit_add( func
, 0, 2 );
2111 STORE( func
, *inst
, 0, 0, chan_index
);
2115 case TGSI_OPCODE_SUB
:
2116 FOR_EACH_DST0_ENABLED_CHANNEL( *inst
, chan_index
) {
2117 FETCH( func
, *inst
, 0, 0, chan_index
);
2118 FETCH( func
, *inst
, 1, 1, chan_index
);
2119 emit_sub( func
, 0, 1 );
2120 STORE( func
, *inst
, 0, 0, chan_index
);
2124 case TGSI_OPCODE_LRP
:
2125 FOR_EACH_DST0_ENABLED_CHANNEL( *inst
, chan_index
) {
2126 FETCH( func
, *inst
, 0, 0, chan_index
);
2127 FETCH( func
, *inst
, 1, 1, chan_index
);
2128 FETCH( func
, *inst
, 2, 2, chan_index
);
2129 emit_sub( func
, 1, 2 );
2130 emit_mul( func
, 0, 1 );
2131 emit_add( func
, 0, 2 );
2132 STORE( func
, *inst
, 0, 0, chan_index
);
2136 case TGSI_OPCODE_CND
:
2140 case TGSI_OPCODE_DP2A
:
2141 FETCH( func
, *inst
, 0, 0, CHAN_X
); /* xmm0 = src[0].x */
2142 FETCH( func
, *inst
, 1, 1, CHAN_X
); /* xmm1 = src[1].x */
2143 emit_mul( func
, 0, 1 ); /* xmm0 = xmm0 * xmm1 */
2144 FETCH( func
, *inst
, 1, 0, CHAN_Y
); /* xmm1 = src[0].y */
2145 FETCH( func
, *inst
, 2, 1, CHAN_Y
); /* xmm2 = src[1].y */
2146 emit_mul( func
, 1, 2 ); /* xmm1 = xmm1 * xmm2 */
2147 emit_add( func
, 0, 1 ); /* xmm0 = xmm0 + xmm1 */
2148 FETCH( func
, *inst
, 1, 2, CHAN_X
); /* xmm1 = src[2].x */
2149 emit_add( func
, 0, 1 ); /* xmm0 = xmm0 + xmm1 */
2150 FOR_EACH_DST0_ENABLED_CHANNEL( *inst
, chan_index
) {
2151 STORE( func
, *inst
, 0, 0, chan_index
); /* dest[ch] = xmm0 */
2155 case TGSI_OPCODE_FRC
:
2156 FOR_EACH_DST0_ENABLED_CHANNEL( *inst
, chan_index
) {
2157 FETCH( func
, *inst
, 0, 0, chan_index
);
2158 emit_frc( func
, 0, 0 );
2159 STORE( func
, *inst
, 0, 0, chan_index
);
2163 case TGSI_OPCODE_CLAMP
:
2167 case TGSI_OPCODE_FLR
:
2168 FOR_EACH_DST0_ENABLED_CHANNEL( *inst
, chan_index
) {
2169 FETCH( func
, *inst
, 0, 0, chan_index
);
2170 emit_flr( func
, 0, 0 );
2171 STORE( func
, *inst
, 0, 0, chan_index
);
2175 case TGSI_OPCODE_ROUND
:
2176 FOR_EACH_DST0_ENABLED_CHANNEL( *inst
, chan_index
) {
2177 FETCH( func
, *inst
, 0, 0, chan_index
);
2178 emit_rnd( func
, 0, 0 );
2179 STORE( func
, *inst
, 0, 0, chan_index
);
2183 case TGSI_OPCODE_EX2
:
2184 FETCH( func
, *inst
, 0, 0, CHAN_X
);
2185 emit_ex2( func
, 0, 0 );
2186 FOR_EACH_DST0_ENABLED_CHANNEL( *inst
, chan_index
) {
2187 STORE( func
, *inst
, 0, 0, chan_index
);
2191 case TGSI_OPCODE_LG2
:
2192 FETCH( func
, *inst
, 0, 0, CHAN_X
);
2193 emit_lg2( func
, 0, 0 );
2194 FOR_EACH_DST0_ENABLED_CHANNEL( *inst
, chan_index
) {
2195 STORE( func
, *inst
, 0, 0, chan_index
);
2199 case TGSI_OPCODE_POW
:
2200 FETCH( func
, *inst
, 0, 0, CHAN_X
);
2201 FETCH( func
, *inst
, 1, 1, CHAN_X
);
2202 emit_pow( func
, 0, 0, 0, 1 );
2203 FOR_EACH_DST0_ENABLED_CHANNEL( *inst
, chan_index
) {
2204 STORE( func
, *inst
, 0, 0, chan_index
);
2208 case TGSI_OPCODE_XPD
:
2209 /* Note: we do all stores after all operands have been fetched
2210 * to avoid src/dst register aliasing issues for an instruction
2211 * such as: XPD TEMP[2].xyz, TEMP[0], TEMP[2];
2213 if( IS_DST0_CHANNEL_ENABLED( *inst
, CHAN_X
) ||
2214 IS_DST0_CHANNEL_ENABLED( *inst
, CHAN_Y
) ) {
2215 FETCH( func
, *inst
, 1, 1, CHAN_Z
); /* xmm[1] = src[1].z */
2216 FETCH( func
, *inst
, 3, 0, CHAN_Z
); /* xmm[3] = src[0].z */
2218 if( IS_DST0_CHANNEL_ENABLED( *inst
, CHAN_X
) ||
2219 IS_DST0_CHANNEL_ENABLED( *inst
, CHAN_Z
) ) {
2220 FETCH( func
, *inst
, 0, 0, CHAN_Y
); /* xmm[0] = src[0].y */
2221 FETCH( func
, *inst
, 4, 1, CHAN_Y
); /* xmm[4] = src[1].y */
2223 IF_IS_DST0_CHANNEL_ENABLED( *inst
, CHAN_X
) {
2224 emit_MOV( func
, 7, 0 ); /* xmm[7] = xmm[0] */
2225 emit_mul( func
, 7, 1 ); /* xmm[7] = xmm[2] * xmm[1] */
2226 emit_MOV( func
, 5, 3 ); /* xmm[5] = xmm[3] */
2227 emit_mul( func
, 5, 4 ); /* xmm[5] = xmm[5] * xmm[4] */
2228 emit_sub( func
, 7, 5 ); /* xmm[7] = xmm[2] - xmm[5] */
2229 /* store xmm[7] in dst.x below */
2231 if( IS_DST0_CHANNEL_ENABLED( *inst
, CHAN_Y
) ||
2232 IS_DST0_CHANNEL_ENABLED( *inst
, CHAN_Z
) ) {
2233 FETCH( func
, *inst
, 2, 1, CHAN_X
); /* xmm[2] = src[1].x */
2234 FETCH( func
, *inst
, 5, 0, CHAN_X
); /* xmm[5] = src[0].x */
2236 IF_IS_DST0_CHANNEL_ENABLED( *inst
, CHAN_Y
) {
2237 emit_mul( func
, 3, 2 ); /* xmm[3] = xmm[3] * xmm[2] */
2238 emit_mul( func
, 1, 5 ); /* xmm[1] = xmm[1] * xmm[5] */
2239 emit_sub( func
, 3, 1 ); /* xmm[3] = xmm[3] - xmm[1] */
2240 /* store xmm[3] in dst.y below */
2242 IF_IS_DST0_CHANNEL_ENABLED( *inst
, CHAN_Z
) {
2243 emit_mul( func
, 5, 4 ); /* xmm[5] = xmm[5] * xmm[4] */
2244 emit_mul( func
, 0, 2 ); /* xmm[0] = xmm[0] * xmm[2] */
2245 emit_sub( func
, 5, 0 ); /* xmm[5] = xmm[5] - xmm[0] */
2246 STORE( func
, *inst
, 5, 0, CHAN_Z
); /* dst.z = xmm[5] */
2248 IF_IS_DST0_CHANNEL_ENABLED( *inst
, CHAN_X
) {
2249 STORE( func
, *inst
, 7, 0, CHAN_X
); /* dst.x = xmm[7] */
2251 IF_IS_DST0_CHANNEL_ENABLED( *inst
, CHAN_Y
) {
2252 STORE( func
, *inst
, 3, 0, CHAN_Y
); /* dst.y = xmm[3] */
2254 IF_IS_DST0_CHANNEL_ENABLED( *inst
, CHAN_W
) {
2260 STORE( func
, *inst
, 0, 0, CHAN_W
);
2264 case TGSI_OPCODE_ABS
:
2265 FOR_EACH_DST0_ENABLED_CHANNEL( *inst
, chan_index
) {
2266 FETCH( func
, *inst
, 0, 0, chan_index
);
2267 emit_abs( func
, 0) ;
2269 STORE( func
, *inst
, 0, 0, chan_index
);
2273 case TGSI_OPCODE_RCC
:
2277 case TGSI_OPCODE_DPH
:
2278 FETCH( func
, *inst
, 0, 0, CHAN_X
);
2279 FETCH( func
, *inst
, 1, 1, CHAN_X
);
2280 emit_mul( func
, 0, 1 );
2281 FETCH( func
, *inst
, 1, 0, CHAN_Y
);
2282 FETCH( func
, *inst
, 2, 1, CHAN_Y
);
2283 emit_mul( func
, 1, 2 );
2284 emit_add( func
, 0, 1 );
2285 FETCH( func
, *inst
, 1, 0, CHAN_Z
);
2286 FETCH( func
, *inst
, 2, 1, CHAN_Z
);
2287 emit_mul( func
, 1, 2 );
2288 emit_add( func
, 0, 1 );
2289 FETCH( func
, *inst
, 1, 1, CHAN_W
);
2290 emit_add( func
, 0, 1 );
2291 FOR_EACH_DST0_ENABLED_CHANNEL( *inst
, chan_index
) {
2292 STORE( func
, *inst
, 0, 0, chan_index
);
2296 case TGSI_OPCODE_COS
:
2297 FETCH( func
, *inst
, 0, 0, CHAN_X
);
2298 emit_cos( func
, 0, 0 );
2299 FOR_EACH_DST0_ENABLED_CHANNEL( *inst
, chan_index
) {
2300 STORE( func
, *inst
, 0, 0, chan_index
);
2304 case TGSI_OPCODE_DDX
:
2308 case TGSI_OPCODE_DDY
:
2312 case TGSI_OPCODE_KILP
:
2313 /* predicated kill */
2315 return 0; /* XXX fix me */
2318 case TGSI_OPCODE_KIL
:
2319 /* conditional kill */
2320 emit_kil( func
, &inst
->Src
[0] );
2323 case TGSI_OPCODE_PK2H
:
2327 case TGSI_OPCODE_PK2US
:
2331 case TGSI_OPCODE_PK4B
:
2335 case TGSI_OPCODE_PK4UB
:
2339 case TGSI_OPCODE_RFL
:
2343 case TGSI_OPCODE_SEQ
:
2344 emit_setcc( func
, inst
, cc_Equal
);
2347 case TGSI_OPCODE_SFL
:
2351 case TGSI_OPCODE_SGT
:
2352 emit_setcc( func
, inst
, cc_NotLessThanEqual
);
2355 case TGSI_OPCODE_SIN
:
2356 FETCH( func
, *inst
, 0, 0, CHAN_X
);
2357 emit_sin( func
, 0, 0 );
2358 FOR_EACH_DST0_ENABLED_CHANNEL( *inst
, chan_index
) {
2359 STORE( func
, *inst
, 0, 0, chan_index
);
2363 case TGSI_OPCODE_SLE
:
2364 emit_setcc( func
, inst
, cc_LessThanEqual
);
2367 case TGSI_OPCODE_SNE
:
2368 emit_setcc( func
, inst
, cc_NotEqual
);
2371 case TGSI_OPCODE_STR
:
2375 case TGSI_OPCODE_TEX
:
2376 emit_tex( func
, inst
, FALSE
, FALSE
);
2379 case TGSI_OPCODE_TXD
:
2383 case TGSI_OPCODE_UP2H
:
2387 case TGSI_OPCODE_UP2US
:
2391 case TGSI_OPCODE_UP4B
:
2395 case TGSI_OPCODE_UP4UB
:
2399 case TGSI_OPCODE_X2D
:
2403 case TGSI_OPCODE_ARA
:
2407 case TGSI_OPCODE_ARR
:
2408 FOR_EACH_DST0_ENABLED_CHANNEL( *inst
, chan_index
) {
2409 FETCH( func
, *inst
, 0, 0, chan_index
);
2410 emit_rnd( func
, 0, 0 );
2411 emit_f2it( func
, 0 );
2412 STORE( func
, *inst
, 0, 0, chan_index
);
2416 case TGSI_OPCODE_BRA
:
2420 case TGSI_OPCODE_CAL
:
2424 case TGSI_OPCODE_RET
:
2428 case TGSI_OPCODE_END
:
2431 case TGSI_OPCODE_SSG
:
2432 FOR_EACH_DST0_ENABLED_CHANNEL( *inst
, chan_index
) {
2433 FETCH( func
, *inst
, 0, 0, chan_index
);
2434 emit_sgn( func
, 0, 0 );
2435 STORE( func
, *inst
, 0, 0, chan_index
);
2439 case TGSI_OPCODE_CMP
:
2440 emit_cmp (func
, inst
);
2443 case TGSI_OPCODE_SCS
:
2444 IF_IS_DST0_CHANNEL_ENABLED( *inst
, CHAN_X
) {
2445 FETCH( func
, *inst
, 0, 0, CHAN_X
);
2446 emit_cos( func
, 0, 0 );
2447 STORE( func
, *inst
, 0, 0, CHAN_X
);
2449 IF_IS_DST0_CHANNEL_ENABLED( *inst
, CHAN_Y
) {
2450 FETCH( func
, *inst
, 0, 0, CHAN_X
);
2451 emit_sin( func
, 0, 0 );
2452 STORE( func
, *inst
, 0, 0, CHAN_Y
);
2454 IF_IS_DST0_CHANNEL_ENABLED( *inst
, CHAN_Z
) {
2458 TGSI_EXEC_TEMP_00000000_I
,
2459 TGSI_EXEC_TEMP_00000000_C
);
2460 STORE( func
, *inst
, 0, 0, CHAN_Z
);
2462 IF_IS_DST0_CHANNEL_ENABLED( *inst
, CHAN_W
) {
2468 STORE( func
, *inst
, 0, 0, CHAN_W
);
2472 case TGSI_OPCODE_TXB
:
2473 emit_tex( func
, inst
, TRUE
, FALSE
);
2476 case TGSI_OPCODE_NRM
:
2478 case TGSI_OPCODE_NRM4
:
2479 /* 3 or 4-component normalization */
2481 uint dims
= (inst
->Instruction
.Opcode
== TGSI_OPCODE_NRM
) ? 3 : 4;
2483 if (IS_DST0_CHANNEL_ENABLED(*inst
, CHAN_X
) ||
2484 IS_DST0_CHANNEL_ENABLED(*inst
, CHAN_Y
) ||
2485 IS_DST0_CHANNEL_ENABLED(*inst
, CHAN_Z
) ||
2486 (IS_DST0_CHANNEL_ENABLED(*inst
, CHAN_W
) && dims
== 4)) {
2488 /* NOTE: Cannot use xmm regs 2/3 here (see emit_rsqrt() above). */
2491 /* xmm0 = src.x * src.x */
2492 FETCH(func
, *inst
, 0, 0, CHAN_X
);
2493 if (IS_DST0_CHANNEL_ENABLED(*inst
, CHAN_X
)) {
2494 emit_MOV(func
, 4, 0);
2496 emit_mul(func
, 0, 0);
2499 /* xmm0 = xmm0 + src.y * src.y */
2500 FETCH(func
, *inst
, 1, 0, CHAN_Y
);
2501 if (IS_DST0_CHANNEL_ENABLED(*inst
, CHAN_Y
)) {
2502 emit_MOV(func
, 5, 1);
2504 emit_mul(func
, 1, 1);
2505 emit_add(func
, 0, 1);
2508 /* xmm0 = xmm0 + src.z * src.z */
2509 FETCH(func
, *inst
, 1, 0, CHAN_Z
);
2510 if (IS_DST0_CHANNEL_ENABLED(*inst
, CHAN_Z
)) {
2511 emit_MOV(func
, 6, 1);
2513 emit_mul(func
, 1, 1);
2514 emit_add(func
, 0, 1);
2518 /* xmm0 = xmm0 + src.w * src.w */
2519 FETCH(func
, *inst
, 1, 0, CHAN_W
);
2520 if (IS_DST0_CHANNEL_ENABLED(*inst
, CHAN_W
)) {
2521 emit_MOV(func
, 7, 1);
2523 emit_mul(func
, 1, 1);
2524 emit_add(func
, 0, 1);
2527 /* xmm1 = 1 / sqrt(xmm0) */
2528 emit_rsqrt(func
, 1, 0);
2530 /* dst.x = xmm1 * src.x */
2531 if (IS_DST0_CHANNEL_ENABLED(*inst
, CHAN_X
)) {
2532 emit_mul(func
, 4, 1);
2533 STORE(func
, *inst
, 4, 0, CHAN_X
);
2536 /* dst.y = xmm1 * src.y */
2537 if (IS_DST0_CHANNEL_ENABLED(*inst
, CHAN_Y
)) {
2538 emit_mul(func
, 5, 1);
2539 STORE(func
, *inst
, 5, 0, CHAN_Y
);
2542 /* dst.z = xmm1 * src.z */
2543 if (IS_DST0_CHANNEL_ENABLED(*inst
, CHAN_Z
)) {
2544 emit_mul(func
, 6, 1);
2545 STORE(func
, *inst
, 6, 0, CHAN_Z
);
2548 /* dst.w = xmm1 * src.w */
2549 if (IS_DST0_CHANNEL_ENABLED(*inst
, CHAN_X
) && dims
== 4) {
2550 emit_mul(func
, 7, 1);
2551 STORE(func
, *inst
, 7, 0, CHAN_W
);
2556 if (IS_DST0_CHANNEL_ENABLED(*inst
, CHAN_W
) && dims
== 3) {
2557 emit_tempf(func
, 0, TEMP_ONE_I
, TEMP_ONE_C
);
2558 STORE(func
, *inst
, 0, 0, CHAN_W
);
2563 case TGSI_OPCODE_DIV
:
2567 case TGSI_OPCODE_DP2
:
2568 FETCH( func
, *inst
, 0, 0, CHAN_X
); /* xmm0 = src[0].x */
2569 FETCH( func
, *inst
, 1, 1, CHAN_X
); /* xmm1 = src[1].x */
2570 emit_mul( func
, 0, 1 ); /* xmm0 = xmm0 * xmm1 */
2571 FETCH( func
, *inst
, 1, 0, CHAN_Y
); /* xmm1 = src[0].y */
2572 FETCH( func
, *inst
, 2, 1, CHAN_Y
); /* xmm2 = src[1].y */
2573 emit_mul( func
, 1, 2 ); /* xmm1 = xmm1 * xmm2 */
2574 emit_add( func
, 0, 1 ); /* xmm0 = xmm0 + xmm1 */
2575 FOR_EACH_DST0_ENABLED_CHANNEL( *inst
, chan_index
) {
2576 STORE( func
, *inst
, 0, 0, chan_index
); /* dest[ch] = xmm0 */
2580 case TGSI_OPCODE_TXL
:
2584 case TGSI_OPCODE_TXP
:
2585 emit_tex( func
, inst
, FALSE
, TRUE
);
2588 case TGSI_OPCODE_BRK
:
2592 case TGSI_OPCODE_IF
:
2596 case TGSI_OPCODE_ELSE
:
2600 case TGSI_OPCODE_ENDIF
:
2604 case TGSI_OPCODE_PUSHA
:
2608 case TGSI_OPCODE_POPA
:
2612 case TGSI_OPCODE_CEIL
:
2616 case TGSI_OPCODE_I2F
:
2620 case TGSI_OPCODE_NOT
:
2624 case TGSI_OPCODE_TRUNC
:
2625 FOR_EACH_DST0_ENABLED_CHANNEL( *inst
, chan_index
) {
2626 FETCH( func
, *inst
, 0, 0, chan_index
);
2627 emit_f2it( func
, 0 );
2628 emit_i2f( func
, 0 );
2629 STORE( func
, *inst
, 0, 0, chan_index
);
2633 case TGSI_OPCODE_SHL
:
2637 case TGSI_OPCODE_ISHR
:
2641 case TGSI_OPCODE_AND
:
2645 case TGSI_OPCODE_OR
:
2649 case TGSI_OPCODE_MOD
:
2653 case TGSI_OPCODE_XOR
:
2657 case TGSI_OPCODE_SAD
:
2661 case TGSI_OPCODE_TXF
:
2665 case TGSI_OPCODE_TXQ
:
2669 case TGSI_OPCODE_CONT
:
2673 case TGSI_OPCODE_EMIT
:
2677 case TGSI_OPCODE_ENDPRIM
:
2690 struct x86_function
*func
,
2691 struct tgsi_full_declaration
*decl
)
2693 if( decl
->Declaration
.File
== TGSI_FILE_INPUT
) {
2694 unsigned first
, last
, mask
;
2697 first
= decl
->Range
.First
;
2698 last
= decl
->Range
.Last
;
2699 mask
= decl
->Declaration
.UsageMask
;
2701 for( i
= first
; i
<= last
; i
++ ) {
2702 for( j
= 0; j
< NUM_CHANNELS
; j
++ ) {
2703 if( mask
& (1 << j
) ) {
2704 switch( decl
->Declaration
.Interpolate
) {
2705 case TGSI_INTERPOLATE_CONSTANT
:
2706 emit_coef_a0( func
, 0, i
, j
);
2707 emit_inputs( func
, 0, i
, j
);
2710 case TGSI_INTERPOLATE_LINEAR
:
2711 emit_tempf( func
, 0, 0, TGSI_SWIZZLE_X
);
2712 emit_coef_dadx( func
, 1, i
, j
);
2713 emit_tempf( func
, 2, 0, TGSI_SWIZZLE_Y
);
2714 emit_coef_dady( func
, 3, i
, j
);
2715 emit_mul( func
, 0, 1 ); /* x * dadx */
2716 emit_coef_a0( func
, 4, i
, j
);
2717 emit_mul( func
, 2, 3 ); /* y * dady */
2718 emit_add( func
, 0, 4 ); /* x * dadx + a0 */
2719 emit_add( func
, 0, 2 ); /* x * dadx + y * dady + a0 */
2720 emit_inputs( func
, 0, i
, j
);
2723 case TGSI_INTERPOLATE_PERSPECTIVE
:
2724 emit_tempf( func
, 0, 0, TGSI_SWIZZLE_X
);
2725 emit_coef_dadx( func
, 1, i
, j
);
2726 emit_tempf( func
, 2, 0, TGSI_SWIZZLE_Y
);
2727 emit_coef_dady( func
, 3, i
, j
);
2728 emit_mul( func
, 0, 1 ); /* x * dadx */
2729 emit_tempf( func
, 4, 0, TGSI_SWIZZLE_W
);
2730 emit_coef_a0( func
, 5, i
, j
);
2731 emit_rcp( func
, 4, 4 ); /* 1.0 / w */
2732 emit_mul( func
, 2, 3 ); /* y * dady */
2733 emit_add( func
, 0, 5 ); /* x * dadx + a0 */
2734 emit_add( func
, 0, 2 ); /* x * dadx + y * dady + a0 */
2735 emit_mul( func
, 0, 4 ); /* (x * dadx + y * dady + a0) / w */
2736 emit_inputs( func
, 0, i
, j
);
2749 static void aos_to_soa( struct x86_function
*func
,
2755 struct x86_reg soa_input
= x86_make_reg( file_REG32
, reg_AX
);
2756 struct x86_reg aos_input
= x86_make_reg( file_REG32
, reg_BX
);
2757 struct x86_reg num_inputs
= x86_make_reg( file_REG32
, reg_CX
);
2758 struct x86_reg stride
= x86_make_reg( file_REG32
, reg_DX
);
2759 int loop_top
, loop_exit_fixup
;
2762 x86_push( func
, x86_make_reg( file_REG32
, reg_BX
) );
2764 x86_mov( func
, aos_input
, x86_fn_arg( func
, arg_aos
) );
2765 x86_mov( func
, soa_input
, x86_fn_arg( func
, arg_machine
) );
2766 /* FIXME: tgsi_exec_machine::Inputs is a pointer now! */
2767 x86_lea( func
, soa_input
,
2768 x86_make_disp( soa_input
,
2769 Offset(struct tgsi_exec_machine
, Inputs
) ) );
2770 x86_mov( func
, num_inputs
, x86_fn_arg( func
, arg_num
) );
2771 x86_mov( func
, stride
, x86_fn_arg( func
, arg_stride
) );
2773 /* while (num_inputs != 0) */
2774 loop_top
= x86_get_label( func
);
2775 x86_cmp_imm( func
, num_inputs
, 0 );
2776 loop_exit_fixup
= x86_jcc_forward( func
, cc_E
);
2779 x86_push( func
, aos_input
);
2780 sse_movlps( func
, make_xmm( 0 ), x86_make_disp( aos_input
, 0 ) );
2781 sse_movlps( func
, make_xmm( 3 ), x86_make_disp( aos_input
, 8 ) );
2782 x86_add( func
, aos_input
, stride
);
2783 sse_movhps( func
, make_xmm( 0 ), x86_make_disp( aos_input
, 0 ) );
2784 sse_movhps( func
, make_xmm( 3 ), x86_make_disp( aos_input
, 8 ) );
2785 x86_add( func
, aos_input
, stride
);
2786 sse_movlps( func
, make_xmm( 1 ), x86_make_disp( aos_input
, 0 ) );
2787 sse_movlps( func
, make_xmm( 4 ), x86_make_disp( aos_input
, 8 ) );
2788 x86_add( func
, aos_input
, stride
);
2789 sse_movhps( func
, make_xmm( 1 ), x86_make_disp( aos_input
, 0 ) );
2790 sse_movhps( func
, make_xmm( 4 ), x86_make_disp( aos_input
, 8 ) );
2791 x86_pop( func
, aos_input
);
2793 sse_movaps( func
, make_xmm( 2 ), make_xmm( 0 ) );
2794 sse_movaps( func
, make_xmm( 5 ), make_xmm( 3 ) );
2795 sse_shufps( func
, make_xmm( 0 ), make_xmm( 1 ), 0x88 );
2796 sse_shufps( func
, make_xmm( 2 ), make_xmm( 1 ), 0xdd );
2797 sse_shufps( func
, make_xmm( 3 ), make_xmm( 4 ), 0x88 );
2798 sse_shufps( func
, make_xmm( 5 ), make_xmm( 4 ), 0xdd );
2800 sse_movups( func
, x86_make_disp( soa_input
, 0 ), make_xmm( 0 ) );
2801 sse_movups( func
, x86_make_disp( soa_input
, 16 ), make_xmm( 2 ) );
2802 sse_movups( func
, x86_make_disp( soa_input
, 32 ), make_xmm( 3 ) );
2803 sse_movups( func
, x86_make_disp( soa_input
, 48 ), make_xmm( 5 ) );
2805 /* Advance to next input */
2806 x86_lea( func
, aos_input
, x86_make_disp(aos_input
, 16) );
2807 x86_lea( func
, soa_input
, x86_make_disp(soa_input
, 64) );
2810 x86_dec( func
, num_inputs
);
2811 x86_jmp( func
, loop_top
);
2812 x86_fixup_fwd_jump( func
, loop_exit_fixup
);
2815 x86_pop( func
, x86_make_reg( file_REG32
, reg_BX
) );
2818 static void soa_to_aos( struct x86_function
*func
,
2824 struct x86_reg soa_output
= x86_make_reg( file_REG32
, reg_AX
);
2825 struct x86_reg aos_output
= x86_make_reg( file_REG32
, reg_BX
);
2826 struct x86_reg num_outputs
= x86_make_reg( file_REG32
, reg_CX
);
2827 struct x86_reg temp
= x86_make_reg( file_REG32
, reg_DX
);
2831 x86_push( func
, x86_make_reg( file_REG32
, reg_BX
) );
2833 x86_mov( func
, aos_output
, x86_fn_arg( func
, arg_aos
) );
2834 x86_mov( func
, soa_output
, x86_fn_arg( func
, arg_machine
) );
2835 /* FIXME: tgsi_exec_machine::Ouputs is a pointer now! */
2836 x86_lea( func
, soa_output
,
2837 x86_make_disp( soa_output
,
2838 Offset(struct tgsi_exec_machine
, Outputs
) ) );
2839 x86_mov( func
, num_outputs
, x86_fn_arg( func
, arg_num
) );
2842 inner_loop
= x86_get_label( func
);
2844 sse_movups( func
, make_xmm( 0 ), x86_make_disp( soa_output
, 0 ) );
2845 sse_movups( func
, make_xmm( 1 ), x86_make_disp( soa_output
, 16 ) );
2846 sse_movups( func
, make_xmm( 3 ), x86_make_disp( soa_output
, 32 ) );
2847 sse_movups( func
, make_xmm( 4 ), x86_make_disp( soa_output
, 48 ) );
2849 sse_movaps( func
, make_xmm( 2 ), make_xmm( 0 ) );
2850 sse_movaps( func
, make_xmm( 5 ), make_xmm( 3 ) );
2851 sse_unpcklps( func
, make_xmm( 0 ), make_xmm( 1 ) );
2852 sse_unpckhps( func
, make_xmm( 2 ), make_xmm( 1 ) );
2853 sse_unpcklps( func
, make_xmm( 3 ), make_xmm( 4 ) );
2854 sse_unpckhps( func
, make_xmm( 5 ), make_xmm( 4 ) );
2856 x86_mov( func
, temp
, x86_fn_arg( func
, arg_stride
) );
2857 x86_push( func
, aos_output
);
2858 sse_movlps( func
, x86_make_disp( aos_output
, 0 ), make_xmm( 0 ) );
2859 sse_movlps( func
, x86_make_disp( aos_output
, 8 ), make_xmm( 3 ) );
2860 x86_add( func
, aos_output
, temp
);
2861 sse_movhps( func
, x86_make_disp( aos_output
, 0 ), make_xmm( 0 ) );
2862 sse_movhps( func
, x86_make_disp( aos_output
, 8 ), make_xmm( 3 ) );
2863 x86_add( func
, aos_output
, temp
);
2864 sse_movlps( func
, x86_make_disp( aos_output
, 0 ), make_xmm( 2 ) );
2865 sse_movlps( func
, x86_make_disp( aos_output
, 8 ), make_xmm( 5 ) );
2866 x86_add( func
, aos_output
, temp
);
2867 sse_movhps( func
, x86_make_disp( aos_output
, 0 ), make_xmm( 2 ) );
2868 sse_movhps( func
, x86_make_disp( aos_output
, 8 ), make_xmm( 5 ) );
2869 x86_pop( func
, aos_output
);
2871 /* Advance to next output */
2872 x86_lea( func
, aos_output
, x86_make_disp(aos_output
, 16) );
2873 x86_lea( func
, soa_output
, x86_make_disp(soa_output
, 64) );
2875 /* while --num_outputs */
2876 x86_dec( func
, num_outputs
);
2877 x86_jcc( func
, cc_NE
, inner_loop
);
2880 x86_pop( func
, x86_make_reg( file_REG32
, reg_BX
) );
2885 * Check if the instructions dst register is the same as any src
2886 * register and warn if there's a posible SOA dependency.
2889 check_soa_dependencies(const struct tgsi_full_instruction
*inst
)
2891 uint opcode
= inst
->Instruction
.Opcode
;
2893 /* XXX: we only handle src/dst aliasing in a few opcodes currently.
2894 * Need to use an additional temporay to hold the result in the
2895 * cases where the code is too opaque to fix.
2899 case TGSI_OPCODE_ADD
:
2900 case TGSI_OPCODE_MOV
:
2901 case TGSI_OPCODE_MUL
:
2902 case TGSI_OPCODE_RCP
:
2903 case TGSI_OPCODE_RSQ
:
2904 case TGSI_OPCODE_EXP
:
2905 case TGSI_OPCODE_LOG
:
2906 case TGSI_OPCODE_DP3
:
2907 case TGSI_OPCODE_DP4
:
2908 case TGSI_OPCODE_DP2A
:
2909 case TGSI_OPCODE_EX2
:
2910 case TGSI_OPCODE_LG2
:
2911 case TGSI_OPCODE_POW
:
2912 case TGSI_OPCODE_XPD
:
2913 case TGSI_OPCODE_DPH
:
2914 case TGSI_OPCODE_COS
:
2915 case TGSI_OPCODE_SIN
:
2916 case TGSI_OPCODE_TEX
:
2917 case TGSI_OPCODE_TXB
:
2918 case TGSI_OPCODE_TXP
:
2919 case TGSI_OPCODE_NRM
:
2920 case TGSI_OPCODE_NRM4
:
2921 case TGSI_OPCODE_DP2
:
2922 /* OK - these opcodes correctly handle SOA dependencies */
2925 if (!tgsi_check_soa_dependencies(inst
))
2928 debug_printf("Warning: src/dst aliasing in instruction"
2929 " is not handled:\n");
2930 debug_printf("Warning: ");
2931 tgsi_dump_instruction(inst
, 1);
2939 * Translate a TGSI vertex/fragment shader to SSE2 code.
2940 * Slightly different things are done for vertex vs. fragment shaders.
2942 * \param tokens the TGSI input shader
2943 * \param func the output SSE code/function
2944 * \param immediates buffer to place immediates, later passed to SSE func
2945 * \param return 1 for success, 0 if translation failed
2949 const struct tgsi_token
*tokens
,
2950 struct x86_function
*func
,
2951 float (*immediates
)[4],
2952 boolean do_swizzles
)
2954 struct tgsi_parse_context parse
;
2956 uint num_immediates
= 0;
2960 func
->csr
= func
->store
;
2962 tgsi_parse_init( &parse
, tokens
);
2964 /* Can't just use EDI, EBX without save/restoring them:
2966 x86_push( func
, x86_make_reg( file_REG32
, reg_BX
) );
2967 x86_push( func
, x86_make_reg( file_REG32
, reg_DI
) );
2970 * Different function args for vertex/fragment shaders:
2972 if (parse
.FullHeader
.Processor
.Processor
== TGSI_PROCESSOR_VERTEX
) {
2978 6 ); /* input_stride */
2984 x86_fn_arg( func
, 1 ) );
2988 x86_fn_arg( func
, 2 ) );
2991 get_immediate_base(),
2992 x86_fn_arg( func
, 3 ) );
2994 if (parse
.FullHeader
.Processor
.Processor
== TGSI_PROCESSOR_FRAGMENT
) {
2998 x86_fn_arg( func
, 4 ) );
3004 x86_make_disp( get_machine_base(),
3005 Offset( struct tgsi_exec_machine
, Samplers
) ) );
3007 while( !tgsi_parse_end_of_tokens( &parse
) && ok
) {
3008 tgsi_parse_token( &parse
);
3010 switch( parse
.FullToken
.Token
.Type
) {
3011 case TGSI_TOKEN_TYPE_DECLARATION
:
3012 if (parse
.FullHeader
.Processor
.Processor
== TGSI_PROCESSOR_FRAGMENT
) {
3015 &parse
.FullToken
.FullDeclaration
);
3019 case TGSI_TOKEN_TYPE_INSTRUCTION
:
3020 ok
= emit_instruction(
3022 &parse
.FullToken
.FullInstruction
);
3025 uint opcode
= parse
.FullToken
.FullInstruction
.Instruction
.Opcode
;
3026 uint proc
= parse
.FullHeader
.Processor
.Processor
;
3027 debug_printf("failed to translate tgsi opcode %d (%s) to SSE (%s)\n",
3029 tgsi_get_opcode_name(opcode
),
3030 tgsi_get_processor_name(proc
));
3034 ok
= check_soa_dependencies(&parse
.FullToken
.FullInstruction
);
3037 case TGSI_TOKEN_TYPE_IMMEDIATE
:
3038 /* simply copy the immediate values into the next immediates[] slot */
3040 const uint size
= parse
.FullToken
.FullImmediate
.Immediate
.NrTokens
- 1;
3043 assert(num_immediates
< TGSI_EXEC_NUM_IMMEDIATES
);
3044 for( i
= 0; i
< size
; i
++ ) {
3045 immediates
[num_immediates
][i
] =
3046 parse
.FullToken
.FullImmediate
.u
[i
].Float
;
3049 debug_printf("SSE FS immediate[%d] = %f %f %f %f\n",
3051 immediates
[num_immediates
][0],
3052 immediates
[num_immediates
][1],
3053 immediates
[num_immediates
][2],
3054 immediates
[num_immediates
][3]);
3059 case TGSI_TOKEN_TYPE_PROPERTY
:
3060 /* we just ignore them for now */
3069 if (parse
.FullHeader
.Processor
.Processor
== TGSI_PROCESSOR_VERTEX
) {
3074 8, /* num_outputs */
3075 9 ); /* output_stride */
3078 /* Can't just use EBX, EDI without save/restoring them:
3080 x86_pop( func
, x86_make_reg( file_REG32
, reg_DI
) );
3081 x86_pop( func
, x86_make_reg( file_REG32
, reg_BX
) );
3085 tgsi_parse_free( &parse
);
3090 #else /* !PIPE_ARCH_X86 */
3094 const struct tgsi_token
*tokens
,
3095 struct x86_function
*func
,
3096 float (*immediates
)[4],
3097 boolean do_swizzles
)
3102 #endif /* !PIPE_ARCH_X86 */