2 * Copyright 2003 Tungsten Graphics, inc.
5 * Permission is hereby granted, free of charge, to any person obtaining a
6 * copy of this software and associated documentation files (the "Software"),
7 * to deal in the Software without restriction, including without limitation
8 * on the rights to use, copy, modify, merge, publish, distribute, sub
9 * license, and/or sell copies of the Software, and to permit persons to whom
10 * the Software is furnished to do so, subject to the following conditions:
12 * The above copyright notice and this permission notice (including the next
13 * paragraph) shall be included in all copies or substantial portions of the
16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 * FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. IN NO EVENT SHALL
19 * TUNGSTEN GRAPHICS AND/OR THEIR SUPPLIERS BE LIABLE FOR ANY CLAIM,
20 * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
21 * OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE
22 * USE OR OTHER DEALINGS IN THE SOFTWARE.
25 * Keith Whitwell <keithw@tungstengraphics.com>
31 #include "t_context.h"
33 #include "simple_list.h"
36 #if defined(USE_SSE_ASM)
38 #include "x86/rtasm/x86sse.h"
39 #include "x86/common_x86_asm.h"
49 struct x86_function func
;
52 GLboolean inputs_safe
;
53 GLboolean outputs_safe
;
56 struct x86_reg identity
;
61 static struct x86_reg
get_identity( struct x86_program
*p
)
66 static void emit_load4f_4( struct x86_program
*p
,
70 sse_movups(&p
->func
, dest
, arg0
);
73 static void emit_load4f_3( struct x86_program
*p
,
77 /* Have to jump through some hoops:
84 sse_movss(&p
->func
, dest
, x86_make_disp(arg0
, 8));
85 sse_shufps(&p
->func
, dest
, get_identity(p
), SHUF(X
,Y
,Z
,W
) );
86 sse_shufps(&p
->func
, dest
, dest
, SHUF(Y
,Z
,X
,W
) );
87 sse_movlps(&p
->func
, dest
, arg0
);
90 static void emit_load4f_2( struct x86_program
*p
,
94 /* Initialize from identity, then pull in low two words:
96 sse_movups(&p
->func
, dest
, get_identity(p
));
97 sse_movlps(&p
->func
, dest
, arg0
);
100 static void emit_load4f_1( struct x86_program
*p
,
102 struct x86_reg arg0
)
104 /* Pull in low word, then swizzle in identity */
105 sse_movss(&p
->func
, dest
, arg0
);
106 sse_shufps(&p
->func
, dest
, get_identity(p
), SHUF(X
,Y
,Z
,W
) );
111 static void emit_load3f_3( struct x86_program
*p
,
113 struct x86_reg arg0
)
115 /* Over-reads by 1 dword - potential SEGV if input is a vertex
118 if (p
->inputs_safe
) {
119 sse_movups(&p
->func
, dest
, arg0
);
126 sse_movss(&p
->func
, dest
, x86_make_disp(arg0
, 8));
127 sse_shufps(&p
->func
, dest
, dest
, SHUF(X
,X
,X
,X
));
128 sse_movlps(&p
->func
, dest
, arg0
);
132 static void emit_load3f_2( struct x86_program
*p
,
134 struct x86_reg arg0
)
136 emit_load4f_2(p
, dest
, arg0
);
139 static void emit_load3f_1( struct x86_program
*p
,
141 struct x86_reg arg0
)
143 emit_load4f_1(p
, dest
, arg0
);
146 static void emit_load2f_2( struct x86_program
*p
,
148 struct x86_reg arg0
)
150 sse_movlps(&p
->func
, dest
, arg0
);
153 static void emit_load2f_1( struct x86_program
*p
,
155 struct x86_reg arg0
)
157 emit_load4f_1(p
, dest
, arg0
);
160 static void emit_load1f_1( struct x86_program
*p
,
162 struct x86_reg arg0
)
164 sse_movss(&p
->func
, dest
, arg0
);
167 static void (*load
[4][4])( struct x86_program
*p
,
169 struct x86_reg arg0
) = {
191 static void emit_load( struct x86_program
*p
,
197 load
[sz
-1][src_sz
-1](p
, dest
, src
);
200 static void emit_store4f( struct x86_program
*p
,
202 struct x86_reg arg0
)
204 sse_movups(&p
->func
, dest
, arg0
);
207 static void emit_store3f( struct x86_program
*p
,
209 struct x86_reg arg0
)
211 if (p
->outputs_safe
) {
212 /* Emit the extra dword anyway. This may hurt writecombining,
213 * may cause other problems.
215 sse_movups(&p
->func
, dest
, arg0
);
218 /* Alternate strategy - emit two, shuffle, emit one.
220 sse_movlps(&p
->func
, dest
, arg0
);
221 sse_shufps(&p
->func
, arg0
, arg0
, SHUF(Z
,Z
,Z
,Z
) ); /* NOTE! destructive */
222 sse_movss(&p
->func
, x86_make_disp(dest
,8), arg0
);
226 static void emit_store2f( struct x86_program
*p
,
228 struct x86_reg arg0
)
230 sse_movlps(&p
->func
, dest
, arg0
);
233 static void emit_store1f( struct x86_program
*p
,
235 struct x86_reg arg0
)
237 sse_movss(&p
->func
, dest
, arg0
);
241 static void (*store
[4])( struct x86_program
*p
,
243 struct x86_reg arg0
) =
251 static void emit_store( struct x86_program
*p
,
254 struct x86_reg temp
)
257 store
[sz
-1](p
, dest
, temp
);
260 static void emit_pack_store_4ub( struct x86_program
*p
,
262 struct x86_reg temp
)
266 sse_mulps(&p
->func
, temp
, p
->chan0
);
269 sse2_cvtps2dq(&p
->func
, temp
, temp
);
270 sse2_packssdw(&p
->func
, temp
, temp
);
271 sse2_packuswb(&p
->func
, temp
, temp
);
272 sse_movss(&p
->func
, dest
, temp
);
275 struct x86_reg mmx0
= x86_make_reg(file_MMX
, 0);
276 struct x86_reg mmx1
= x86_make_reg(file_MMX
, 1);
277 sse_cvtps2pi(&p
->func
, mmx0
, temp
);
278 sse_movhlps(&p
->func
, temp
, temp
);
279 sse_cvtps2pi(&p
->func
, mmx1
, temp
);
280 mmx_packssdw(&p
->func
, mmx0
, mmx1
);
281 mmx_packuswb(&p
->func
, mmx0
, mmx0
);
282 mmx_movd(&p
->func
, dest
, mmx0
);
286 static GLint
get_offset( const void *a
, const void *b
)
288 return (const char *)b
- (const char *)a
;
291 /* Not much happens here. Eventually use this function to try and
292 * avoid saving/reloading the source pointers each vertex (if some of
293 * them can fit in registers).
295 static void get_src_ptr( struct x86_program
*p
,
296 struct x86_reg srcREG
,
297 struct x86_reg vtxREG
,
298 struct tnl_clipspace_attr
*a
)
300 struct tnl_clipspace
*vtx
= GET_VERTEX_STATE(p
->ctx
);
301 struct x86_reg ptr_to_src
= x86_make_disp(vtxREG
, get_offset(vtx
, &a
->inputptr
));
303 /* Load current a[j].inputptr
305 x86_mov(&p
->func
, srcREG
, ptr_to_src
);
308 static void update_src_ptr( struct x86_program
*p
,
309 struct x86_reg srcREG
,
310 struct x86_reg vtxREG
,
311 struct tnl_clipspace_attr
*a
)
313 if (a
->inputstride
) {
314 struct tnl_clipspace
*vtx
= GET_VERTEX_STATE(p
->ctx
);
315 struct x86_reg ptr_to_src
= x86_make_disp(vtxREG
, get_offset(vtx
, &a
->inputptr
));
317 /* add a[j].inputstride (hardcoded value - could just as easily
318 * pull the stride value from memory each time).
320 x86_lea(&p
->func
, srcREG
, x86_make_disp(srcREG
, a
->inputstride
));
322 /* save new value of a[j].inputptr
324 x86_mov(&p
->func
, ptr_to_src
, srcREG
);
329 /* Lots of hardcoding
331 * EAX -- pointer to current output vertex
332 * ECX -- pointer to current attribute
335 static GLboolean
build_vertex_emit( struct x86_program
*p
)
337 GLcontext
*ctx
= p
->ctx
;
338 TNLcontext
*tnl
= TNL_CONTEXT(ctx
);
339 struct tnl_clipspace
*vtx
= GET_VERTEX_STATE(ctx
);
342 struct x86_reg vertexEAX
= x86_make_reg(file_REG32
, reg_AX
);
343 struct x86_reg srcECX
= x86_make_reg(file_REG32
, reg_CX
);
344 struct x86_reg countEBP
= x86_make_reg(file_REG32
, reg_BP
);
345 struct x86_reg vtxESI
= x86_make_reg(file_REG32
, reg_SI
);
346 struct x86_reg temp
= x86_make_reg(file_XMM
, 0);
347 struct x86_reg vp0
= x86_make_reg(file_XMM
, 1);
348 struct x86_reg vp1
= x86_make_reg(file_XMM
, 2);
349 GLubyte
*fixup
, *label
;
351 x86_init_func(&p
->func
);
355 x86_push(&p
->func
, countEBP
);
356 x86_push(&p
->func
, vtxESI
);
359 /* Get vertex count, compare to zero
361 x86_xor(&p
->func
, srcECX
, srcECX
);
362 x86_mov(&p
->func
, countEBP
, x86_fn_arg(&p
->func
, 2));
363 x86_cmp(&p
->func
, countEBP
, srcECX
);
364 fixup
= x86_jcc_forward(&p
->func
, cc_E
);
366 /* Initialize destination register.
368 x86_mov(&p
->func
, vertexEAX
, x86_fn_arg(&p
->func
, 3));
370 /* Dereference ctx to get tnl, then vtx:
372 x86_mov(&p
->func
, vtxESI
, x86_fn_arg(&p
->func
, 1));
373 x86_mov(&p
->func
, vtxESI
, x86_make_disp(vtxESI
, get_offset(ctx
, &ctx
->swtnl_context
)));
374 vtxESI
= x86_make_disp(vtxESI
, get_offset(tnl
, &tnl
->clipspace
));
377 /* Possibly load vp0, vp1 for viewport calcs:
379 if (vtx
->need_viewport
) {
380 sse_movups(&p
->func
, vp0
, x86_make_disp(vtxESI
, get_offset(vtx
, &vtx
->vp_scale
[0])));
381 sse_movups(&p
->func
, vp1
, x86_make_disp(vtxESI
, get_offset(vtx
, &vtx
->vp_xlate
[0])));
384 /* always load, needed or not:
386 sse_movups(&p
->func
, p
->chan0
, x86_make_disp(vtxESI
, get_offset(vtx
, &vtx
->chan_scale
[0])));
387 sse_movups(&p
->func
, p
->identity
, x86_make_disp(vtxESI
, get_offset(vtx
, &vtx
->identity
[0])));
389 /* Note address for loop jump */
390 label
= x86_get_label(&p
->func
);
392 /* Emit code for each of the attributes. Currently routes
393 * everything through SSE registers, even when it might be more
394 * efficient to stick with regular old x86. No optimization or
395 * other tricks - enough new ground to cover here just getting
398 while (j
< vtx
->attr_count
) {
399 struct tnl_clipspace_attr
*a
= &vtx
->attr
[j
];
400 struct x86_reg dest
= x86_make_disp(vertexEAX
, a
->vertoffset
);
402 /* Now, load an XMM reg from src, perhaps transform, then save.
403 * Could be shortcircuited in specific cases:
407 get_src_ptr(p
, srcECX
, vtxESI
, a
);
408 emit_load(p
, temp
, 1, x86_deref(srcECX
), a
->inputsize
);
409 emit_store(p
, dest
, 1, temp
);
410 update_src_ptr(p
, srcECX
, vtxESI
, a
);
413 get_src_ptr(p
, srcECX
, vtxESI
, a
);
414 emit_load(p
, temp
, 2, x86_deref(srcECX
), a
->inputsize
);
415 emit_store(p
, dest
, 2, temp
);
416 update_src_ptr(p
, srcECX
, vtxESI
, a
);
419 /* Potentially the worst case - hardcode 2+1 copying:
422 get_src_ptr(p
, srcECX
, vtxESI
, a
);
423 emit_load(p
, temp
, 3, x86_deref(srcECX
), a
->inputsize
);
424 emit_store(p
, dest
, 3, temp
);
425 update_src_ptr(p
, srcECX
, vtxESI
, a
);
428 get_src_ptr(p
, srcECX
, vtxESI
, a
);
429 emit_load(p
, temp
, 2, x86_deref(srcECX
), a
->inputsize
);
430 emit_store(p
, dest
, 2, temp
);
431 if (a
->inputsize
> 2) {
432 emit_load(p
, temp
, 1, x86_make_disp(srcECX
, 8), 1);
433 emit_store(p
, x86_make_disp(dest
,8), 1, temp
);
436 sse_movss(&p
->func
, x86_make_disp(dest
,8), get_identity(p
));
438 update_src_ptr(p
, srcECX
, vtxESI
, a
);
442 get_src_ptr(p
, srcECX
, vtxESI
, a
);
443 emit_load(p
, temp
, 4, x86_deref(srcECX
), a
->inputsize
);
444 emit_store(p
, dest
, 4, temp
);
445 update_src_ptr(p
, srcECX
, vtxESI
, a
);
447 case EMIT_2F_VIEWPORT
:
448 get_src_ptr(p
, srcECX
, vtxESI
, a
);
449 emit_load(p
, temp
, 2, x86_deref(srcECX
), a
->inputsize
);
450 sse_mulps(&p
->func
, temp
, vp0
);
451 sse_addps(&p
->func
, temp
, vp1
);
452 emit_store(p
, dest
, 2, temp
);
453 update_src_ptr(p
, srcECX
, vtxESI
, a
);
455 case EMIT_3F_VIEWPORT
:
456 get_src_ptr(p
, srcECX
, vtxESI
, a
);
457 emit_load(p
, temp
, 3, x86_deref(srcECX
), a
->inputsize
);
458 sse_mulps(&p
->func
, temp
, vp0
);
459 sse_addps(&p
->func
, temp
, vp1
);
460 emit_store(p
, dest
, 3, temp
);
461 update_src_ptr(p
, srcECX
, vtxESI
, a
);
463 case EMIT_4F_VIEWPORT
:
464 get_src_ptr(p
, srcECX
, vtxESI
, a
);
465 emit_load(p
, temp
, 4, x86_deref(srcECX
), a
->inputsize
);
466 sse_mulps(&p
->func
, temp
, vp0
);
467 sse_addps(&p
->func
, temp
, vp1
);
468 emit_store(p
, dest
, 4, temp
);
469 update_src_ptr(p
, srcECX
, vtxESI
, a
);
472 get_src_ptr(p
, srcECX
, vtxESI
, a
);
473 emit_load(p
, temp
, 4, x86_deref(srcECX
), a
->inputsize
);
474 sse_shufps(&p
->func
, temp
, temp
, SHUF(X
,Y
,W
,Z
));
475 emit_store(p
, dest
, 3, temp
);
476 update_src_ptr(p
, srcECX
, vtxESI
, a
);
480 /* Test for PAD3 + 1UB:
483 a
[-1].vertoffset
+ a
[-1].vertattrsize
<= a
->vertoffset
- 3)
485 get_src_ptr(p
, srcECX
, vtxESI
, a
);
486 emit_load(p
, temp
, 1, x86_deref(srcECX
), a
->inputsize
);
487 sse_shufps(&p
->func
, temp
, temp
, SHUF(X
,X
,X
,X
));
488 emit_pack_store_4ub(p
, x86_make_disp(dest
, -3), temp
); /* overkill! */
489 update_src_ptr(p
, srcECX
, vtxESI
, a
);
492 _mesa_printf("Can't emit 1ub %x %x %d\n", a
->vertoffset
, a
[-1].vertoffset
, a
[-1].vertattrsize
);
496 case EMIT_3UB_3F_RGB
:
497 case EMIT_3UB_3F_BGR
:
498 /* Test for 3UB + PAD1:
500 if (j
== vtx
->attr_count
- 1 ||
501 a
[1].vertoffset
>= a
->vertoffset
+ 4) {
502 get_src_ptr(p
, srcECX
, vtxESI
, a
);
503 emit_load(p
, temp
, 3, x86_deref(srcECX
), a
->inputsize
);
504 if (a
->format
== EMIT_3UB_3F_BGR
)
505 sse_shufps(&p
->func
, temp
, temp
, SHUF(Z
,Y
,X
,W
));
506 emit_pack_store_4ub(p
, dest
, temp
);
507 update_src_ptr(p
, srcECX
, vtxESI
, a
);
509 /* Test for 3UB + 1UB:
511 else if (j
< vtx
->attr_count
- 1 &&
512 a
[1].format
== EMIT_1UB_1F
&&
513 a
[1].vertoffset
== a
->vertoffset
+ 3) {
514 get_src_ptr(p
, srcECX
, vtxESI
, a
);
515 emit_load(p
, temp
, 3, x86_deref(srcECX
), a
->inputsize
);
516 update_src_ptr(p
, srcECX
, vtxESI
, a
);
518 /* Make room for incoming value:
520 sse_shufps(&p
->func
, temp
, temp
, SHUF(W
,X
,Y
,Z
));
522 get_src_ptr(p
, srcECX
, vtxESI
, &a
[1]);
523 emit_load(p
, temp
, 1, x86_deref(srcECX
), a
[1].inputsize
);
524 update_src_ptr(p
, srcECX
, vtxESI
, &a
[1]);
526 /* Rearrange and possibly do BGR conversion:
528 if (a
->format
== EMIT_3UB_3F_BGR
)
529 sse_shufps(&p
->func
, temp
, temp
, SHUF(W
,Z
,Y
,X
));
531 sse_shufps(&p
->func
, temp
, temp
, SHUF(Y
,Z
,W
,X
));
533 emit_pack_store_4ub(p
, dest
, temp
);
534 j
++; /* NOTE: two attrs consumed */
537 _mesa_printf("Can't emit 3ub\n");
539 return GL_FALSE
; /* add this later */
542 case EMIT_4UB_4F_RGBA
:
543 get_src_ptr(p
, srcECX
, vtxESI
, a
);
544 emit_load(p
, temp
, 4, x86_deref(srcECX
), a
->inputsize
);
545 emit_pack_store_4ub(p
, dest
, temp
);
546 update_src_ptr(p
, srcECX
, vtxESI
, a
);
548 case EMIT_4UB_4F_BGRA
:
549 get_src_ptr(p
, srcECX
, vtxESI
, a
);
550 emit_load(p
, temp
, 4, x86_deref(srcECX
), a
->inputsize
);
551 sse_shufps(&p
->func
, temp
, temp
, SHUF(Z
,Y
,X
,W
));
552 emit_pack_store_4ub(p
, dest
, temp
);
553 update_src_ptr(p
, srcECX
, vtxESI
, a
);
555 case EMIT_4UB_4F_ARGB
:
556 get_src_ptr(p
, srcECX
, vtxESI
, a
);
557 emit_load(p
, temp
, 4, x86_deref(srcECX
), a
->inputsize
);
558 sse_shufps(&p
->func
, temp
, temp
, SHUF(W
,X
,Y
,Z
));
559 emit_pack_store_4ub(p
, dest
, temp
);
560 update_src_ptr(p
, srcECX
, vtxESI
, a
);
562 case EMIT_4UB_4F_ABGR
:
563 get_src_ptr(p
, srcECX
, vtxESI
, a
);
564 emit_load(p
, temp
, 4, x86_deref(srcECX
), a
->inputsize
);
565 sse_shufps(&p
->func
, temp
, temp
, SHUF(W
,Z
,Y
,X
));
566 emit_pack_store_4ub(p
, dest
, temp
);
567 update_src_ptr(p
, srcECX
, vtxESI
, a
);
569 case EMIT_4CHAN_4F_RGBA
:
571 case GL_UNSIGNED_BYTE
:
572 get_src_ptr(p
, srcECX
, vtxESI
, a
);
573 emit_load(p
, temp
, 4, x86_deref(srcECX
), a
->inputsize
);
574 emit_pack_store_4ub(p
, dest
, temp
);
575 update_src_ptr(p
, srcECX
, vtxESI
, a
);
578 get_src_ptr(p
, srcECX
, vtxESI
, a
);
579 emit_load(p
, temp
, 4, x86_deref(srcECX
), a
->inputsize
);
580 emit_store(p
, dest
, 4, temp
);
581 update_src_ptr(p
, srcECX
, vtxESI
, a
);
583 case GL_UNSIGNED_SHORT
:
585 _mesa_printf("unknown CHAN_TYPE %s\n", _mesa_lookup_enum_by_nr(CHAN_TYPE
));
590 _mesa_printf("unknown a[%d].format %d\n", j
, a
->format
);
591 return GL_FALSE
; /* catch any new opcodes */
594 /* Increment j by at least 1 - may have been incremented above also:
601 x86_lea(&p
->func
, vertexEAX
, x86_make_disp(vertexEAX
, vtx
->vertex_size
));
603 /* decr count, loop if not zero
605 x86_dec(&p
->func
, countEBP
);
606 x86_test(&p
->func
, countEBP
, countEBP
);
607 x86_jcc(&p
->func
, cc_NZ
, label
);
611 if (p
->func
.need_emms
)
614 /* Land forward jump here:
616 x86_fixup_fwd_jump(&p
->func
, fixup
);
618 /* Pop regs and return
620 x86_pop(&p
->func
, x86_get_base_reg(vtxESI
));
621 x86_pop(&p
->func
, countEBP
);
624 vtx
->emit
= (tnl_emit_func
)x86_get_func(&p
->func
);
630 void _tnl_generate_sse_emit( GLcontext
*ctx
)
632 struct tnl_clipspace
*vtx
= GET_VERTEX_STATE(ctx
);
633 struct x86_program p
;
636 vtx
->codegen_emit
= NULL
;
640 _mesa_memset(&p
, 0, sizeof(p
));
643 p
.inputs_safe
= 0; /* for now */
644 p
.outputs_safe
= 1; /* for now */
645 p
.have_sse2
= cpu_has_xmm2
;
646 p
.identity
= x86_make_reg(file_XMM
, 6);
647 p
.chan0
= x86_make_reg(file_XMM
, 7);
649 x86_init_func(&p
.func
);
651 if (build_vertex_emit(&p
)) {
652 _tnl_register_fastpath( vtx
, GL_TRUE
);
655 /* Note the failure so that we don't keep trying to codegen an
658 _tnl_register_fastpath( vtx
, GL_FALSE
);
659 x86_release_func(&p
.func
);
665 void _tnl_generate_sse_emit( GLcontext
*ctx
)
667 /* Dummy version for when USE_SSE_ASM not defined */