summaryrefslogtreecommitdiffstats
path: root/src/mesa/tnl/t_vertex_sse.c
blob: 6436cbfc6ea082175ccc92d08a5c87c443af4180 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
/*
 * Copyright 2003 Tungsten Graphics, inc.
 * All Rights Reserved.
 *
 * Permission is hereby granted, free of charge, to any person obtaining a
 * copy of this software and associated documentation files (the "Software"),
 * to deal in the Software without restriction, including without limitation
 * on the rights to use, copy, modify, merge, publish, distribute, sub
 * license, and/or sell copies of the Software, and to permit persons to whom
 * the Software is furnished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice (including the next
 * paragraph) shall be included in all copies or substantial portions of the
 * Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT.  IN NO EVENT SHALL
 * TUNGSTEN GRAPHICS AND/OR THEIR SUPPLIERS BE LIABLE FOR ANY CLAIM,
 * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
 * OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE
 * USE OR OTHER DEALINGS IN THE SOFTWARE.
 *
 * Authors:
 *    Keith Whitwell <keithw@tungstengraphics.com>
 */

#include "main/glheader.h"
#include "main/context.h"
#include "main/colormac.h"
#include "main/simple_list.h"
#include "main/enums.h"
#include "t_context.h"
#include "t_vertex.h"

#if defined(USE_SSE_ASM)

#include "x86/rtasm/x86sse.h"
#include "x86/common_x86_asm.h"


/**
 * Number of bytes to allocate for generated SSE functions
 */
#define MAX_SSE_CODE_SIZE 1024


#define X    0
#define Y    1
#define Z    2
#define W    3


struct x86_program {
   struct x86_function func;

   GLcontext *ctx;
   GLboolean inputs_safe;
   GLboolean outputs_safe;
   GLboolean have_sse2;
   
   struct x86_reg identity;
   struct x86_reg chan0;
};


static struct x86_reg get_identity( struct x86_program *p )
{
   return p->identity;
}

static void emit_load4f_4( struct x86_program *p, 			   
			   struct x86_reg dest,
			   struct x86_reg arg0 )
{
   sse_movups(&p->func, dest, arg0);
}

static void emit_load4f_3( struct x86_program *p, 
			   struct x86_reg dest,
			   struct x86_reg arg0 )
{
   /* Have to jump through some hoops:
    *
    * c 0 0 0
    * c 0 0 1
    * 0 0 c 1
    * a b c 1
    */
   sse_movss(&p->func, dest, x86_make_disp(arg0, 8));
   sse_shufps(&p->func, dest, get_identity(p), SHUF(X,Y,Z,W) );
   sse_shufps(&p->func, dest, dest, SHUF(Y,Z,X,W) );
   sse_movlps(&p->func, dest, arg0);
}

static void emit_load4f_2( struct x86_program *p, 
			   struct x86_reg dest,
			   struct x86_reg arg0 )
{
   /* Initialize from identity, then pull in low two words:
    */
   sse_movups(&p->func, dest, get_identity(p));
   sse_movlps(&p->func, dest, arg0);
}

static void emit_load4f_1( struct x86_program *p, 
			   struct x86_reg dest,
			   struct x86_reg arg0 )
{
   /* Pull in low word, then swizzle in identity */
   sse_movss(&p->func, dest, arg0);
   sse_shufps(&p->func, dest, get_identity(p), SHUF(X,Y,Z,W) );
}



static void emit_load3f_3( struct x86_program *p, 			   
			   struct x86_reg dest,
			   struct x86_reg arg0 )
{
   /* Over-reads by 1 dword - potential SEGV if input is a vertex
    * array.
    */
   if (p->inputs_safe) {
      sse_movups(&p->func, dest, arg0);
   } 
   else {
      /* c 0 0 0
       * c c c c
       * a b c c 
       */
      sse_movss(&p->func, dest, x86_make_disp(arg0, 8));
      sse_shufps(&p->func, dest, dest, SHUF(X,X,X,X));
      sse_movlps(&p->func, dest, arg0);
   }
}

static void emit_load3f_2( struct x86_program *p, 
			   struct x86_reg dest,
			   struct x86_reg arg0 )
{
   emit_load4f_2(p, dest, arg0);
}

static void emit_load3f_1( struct x86_program *p, 
			   struct x86_reg dest,
			   struct x86_reg arg0 )
{
   /* Loading from memory erases the upper bits. */
   sse_movss(&p->func, dest, arg0);
}

static void emit_load2f_2( struct x86_program *p, 
			   struct x86_reg dest,
			   struct x86_reg arg0 )
{
   sse_movlps(&p->func, dest, arg0);
}

static void emit_load2f_1( struct x86_program *p, 
			   struct x86_reg dest,
			   struct x86_reg arg0 )
{
   /* Loading from memory erases the upper bits. */
   sse_movss(&p->func, dest, arg0);
}

static void emit_load1f_1( struct x86_program *p, 
			   struct x86_reg dest,
			   struct x86_reg arg0 )
{
   sse_movss(&p->func, dest, arg0);
}

static void (*load[4][4])( struct x86_program *p, 
			   struct x86_reg dest,
			   struct x86_reg arg0 ) = {
   { emit_load1f_1, 
     emit_load1f_1, 
     emit_load1f_1, 
     emit_load1f_1 },

   { emit_load2f_1, 
     emit_load2f_2, 
     emit_load2f_2, 
     emit_load2f_2 },

   { emit_load3f_1, 
     emit_load3f_2, 
     emit_load3f_3, 
     emit_load3f_3 },

   { emit_load4f_1, 
     emit_load4f_2, 
     emit_load4f_3, 
     emit_load4f_4 } 
};

static void emit_load( struct x86_program *p,
		       struct x86_reg dest,
		       GLuint sz,
		       struct x86_reg src,
		       GLuint src_sz)
{
   load[sz-1][src_sz-1](p, dest, src);
}

static void emit_store4f( struct x86_program *p, 			   
			  struct x86_reg dest,
			  struct x86_reg arg0 )
{
   sse_movups(&p->func, dest, arg0);
}

static void emit_store3f( struct x86_program *p, 
			  struct x86_reg dest,
			  struct x86_reg arg0 )
{
   if (p->outputs_safe) {
      /* Emit the extra dword anyway.  This may hurt writecombining,
       * may cause other problems.
       */
      sse_movups(&p->func, dest, arg0);
   }
   else {
      /* Alternate strategy - emit two, shuffle, emit one.
       */
      sse_movlps(&p->func, dest, arg0);
      sse_shufps(&p->func, arg0, arg0, SHUF(Z,Z,Z,Z) ); /* NOTE! destructive */
      sse_movss(&p->func, x86_make_disp(dest,8), arg0);
   }
}

static void emit_store2f( struct x86_program *p, 
			   struct x86_reg dest,
			   struct x86_reg arg0 )
{
   sse_movlps(&p->func, dest, arg0);
}

static void emit_store1f( struct x86_program *p, 
			  struct x86_reg dest,
			  struct x86_reg arg0 )
{
   sse_movss(&p->func, dest, arg0);
}


static void (*store[4])( struct x86_program *p, 
			 struct x86_reg dest,
			 struct x86_reg arg0 ) = 
{
   emit_store1f, 
   emit_store2f, 
   emit_store3f, 
   emit_store4f 
};

static void emit_store( struct x86_program *p,
			struct x86_reg dest,
			GLuint sz,
			struct x86_reg temp )

{
   store[sz-1](p, dest, temp);
}

static void emit_pack_store_4ub( struct x86_program *p,
				 struct x86_reg dest,
				 struct x86_reg temp )
{
   /* Scale by 255.0
    */
   sse_mulps(&p->func, temp, p->chan0);

   if (p->have_sse2) {
      sse2_cvtps2dq(&p->func, temp, temp);
      sse2_packssdw(&p->func, temp, temp);
      sse2_packuswb(&p->func, temp, temp);
      sse_movss(&p->func, dest, temp);
   }
   else {
      struct x86_reg mmx0 = x86_make_reg(file_MMX, 0);
      struct x86_reg mmx1 = x86_make_reg(file_MMX, 1);
      sse_cvtps2pi(&p->func, mmx0, temp);
      sse_movhlps(&p->func, temp, temp);
      sse_cvtps2pi(&p->func, mmx1, temp);
      mmx_packssdw(&p->func, mmx0, mmx1);
      mmx_packuswb(&p->func, mmx0, mmx0);
      mmx_movd(&p->func, dest, mmx0);
   }
}

static GLint get_offset( const void *a, const void *b )
{
   return (const char *)b - (const char *)a;
}

/* Not much happens here.  Eventually use this function to try and
 * avoid saving/reloading the source pointers each vertex (if some of
 * them can fit in registers).
 */
static void get_src_ptr( struct x86_program *p,
			 struct x86_reg srcREG,
			 struct x86_reg vtxREG,
			 struct tnl_clipspace_attr *a )
{
   struct tnl_clipspace *vtx = GET_VERTEX_STATE(p->ctx);
   struct x86_reg ptr_to_src = x86_make_disp(vtxREG, get_offset(vtx, &a->inputptr));

   /* Load current a[j].inputptr
    */
   x86_mov(&p->func, srcREG, ptr_to_src);
}

static void update_src_ptr( struct x86_program *p,
			 struct x86_reg srcREG,
			 struct x86_reg vtxREG,
			 struct tnl_clipspace_attr *a )
{
   if (a->inputstride) {
      struct tnl_clipspace *vtx = GET_VERTEX_STATE(p->ctx);
      struct x86_reg ptr_to_src = x86_make_disp(vtxREG, get_offset(vtx, &a->inputptr));

      /* add a[j].inputstride (hardcoded value - could just as easily
       * pull the stride value from memory each time).
       */
      x86_lea(&p->func, srcREG, x86_make_disp(srcREG, a->inputstride));
      
      /* save new value of a[j].inputptr 
       */
      x86_mov(&p->func, ptr_to_src, srcREG);
   }
}


/* Lots of hardcoding
 *
 * EAX -- pointer to current output vertex
 * ECX -- pointer to current attribute 
 * 
 */
static GLboolean build_vertex_emit( struct x86_program *p )
{
   GLcontext *ctx = p->ctx;
   TNLcontext *tnl = TNL_CONTEXT(ctx);
   struct tnl_clipspace *vtx = GET_VERTEX_STATE(ctx);
   GLuint j = 0;

   struct x86_reg vertexEAX = x86_make_reg(file_REG32, reg_AX);
   struct x86_reg srcECX = x86_make_reg(file_REG32, reg_CX);
   struct x86_reg countEBP = x86_make_reg(file_REG32, reg_BP);
   struct x86_reg vtxESI = x86_make_reg(file_REG32, reg_SI);
   struct x86_reg temp = x86_make_reg(file_XMM, 0);
   struct x86_reg vp0 = x86_make_reg(file_XMM, 1);
   struct x86_reg vp1 = x86_make_reg(file_XMM, 2);
   struct x86_reg temp2 = x86_make_reg(file_XMM, 3);
   GLubyte *fixup, *label;

   /* Push a few regs?
    */
   x86_push(&p->func, countEBP);
   x86_push(&p->func, vtxESI);


   /* Get vertex count, compare to zero
    */
   x86_xor(&p->func, srcECX, srcECX);
   x86_mov(&p->func, countEBP, x86_fn_arg(&p->func, 2));
   x86_cmp(&p->func, countEBP, srcECX);
   fixup = x86_jcc_forward(&p->func, cc_E);

   /* Initialize destination register. 
    */
   x86_mov(&p->func, vertexEAX, x86_fn_arg(&p->func, 3));

   /* Dereference ctx to get tnl, then vtx:
    */
   x86_mov(&p->func, vtxESI, x86_fn_arg(&p->func, 1));
   x86_mov(&p->func, vtxESI, x86_make_disp(vtxESI, get_offset(ctx, &ctx->swtnl_context)));
   vtxESI = x86_make_disp(vtxESI, get_offset(tnl, &tnl->clipspace));

   
   /* Possibly load vp0, vp1 for viewport calcs:
    */
   if (vtx->need_viewport) {
      sse_movups(&p->func, vp0, x86_make_disp(vtxESI, get_offset(vtx, &vtx->vp_scale[0])));
      sse_movups(&p->func, vp1, x86_make_disp(vtxESI, get_offset(vtx, &vtx->vp_xlate[0])));
   }

   /* always load, needed or not:
    */
   sse_movups(&p->func, p->chan0, x86_make_disp(vtxESI, get_offset(vtx, &vtx->chan_scale[0])));
   sse_movups(&p->func, p->identity, x86_make_disp(vtxESI, get_offset(vtx, &vtx->identity[0])));

   /* Note address for loop jump */
   label = x86_get_label(&p->func);

   /* Emit code for each of the attributes.  Currently routes
    * everything through SSE registers, even when it might be more
    * efficient to stick with regular old x86.  No optimization or
    * other tricks - enough new ground to cover here just getting
    * things working.
    */
   while (j < vtx->attr_count) {
      struct tnl_clipspace_attr *a = &vtx->attr[j];
      struct x86_reg dest = x86_make_disp(vertexEAX, a->vertoffset);

      /* Now, load an XMM reg from src, perhaps transform, then save.
       * Could be shortcircuited in specific cases:
       */
      switch (a->format) {
      case EMIT_1F:
	 get_src_ptr(p, srcECX, vtxESI, a);
	 emit_load(p, temp, 1, x86_deref(srcECX), a->inputsize);
	 emit_store(p, dest, 1, temp);
	 update_src_ptr(p, srcECX, vtxESI, a);
	 break;
      case EMIT_2F:
	 get_src_ptr(p, srcECX, vtxESI, a);
	 emit_load(p, temp, 2, x86_deref(srcECX), a->inputsize);
	 emit_store(p, dest, 2, temp);
	 update_src_ptr(p, srcECX, vtxESI, a);
	 break;
      case EMIT_3F:
	 /* Potentially the worst case - hardcode 2+1 copying:
	  */
	 if (0) {
	    get_src_ptr(p, srcECX, vtxESI, a);
	    emit_load(p, temp, 3, x86_deref(srcECX), a->inputsize);
	    emit_store(p, dest, 3, temp);
	    update_src_ptr(p, srcECX, vtxESI, a);
	 }
	 else {
	    get_src_ptr(p, srcECX, vtxESI, a);
	    emit_load(p, temp, 2, x86_deref(srcECX), a->inputsize);
	    emit_store(p, dest, 2, temp);
	    if (a->inputsize > 2) {
	       emit_load(p, temp, 1, x86_make_disp(srcECX, 8), 1);
	       emit_store(p, x86_make_disp(dest,8), 1, temp);
	    }
	    else {
	       sse_movss(&p->func, x86_make_disp(dest,8), get_identity(p));
	    }
	    update_src_ptr(p, srcECX, vtxESI, a);
	 }
	 break;
      case EMIT_4F:
	 get_src_ptr(p, srcECX, vtxESI, a);
	 emit_load(p, temp, 4, x86_deref(srcECX), a->inputsize);
	 emit_store(p, dest, 4, temp);
	 update_src_ptr(p, srcECX, vtxESI, a);
	 break;
      case EMIT_2F_VIEWPORT: 
	 get_src_ptr(p, srcECX, vtxESI, a);
	 emit_load(p, temp, 2, x86_deref(srcECX), a->inputsize);
	 sse_mulps(&p->func, temp, vp0);
	 sse_addps(&p->func, temp, vp1);
	 emit_store(p, dest, 2, temp);
	 update_src_ptr(p, srcECX, vtxESI, a);
	 break;
      case EMIT_3F_VIEWPORT: 
	 get_src_ptr(p, srcECX, vtxESI, a);
	 emit_load(p, temp, 3, x86_deref(srcECX), a->inputsize);
	 sse_mulps(&p->func, temp, vp0);
	 sse_addps(&p->func, temp, vp1);
	 emit_store(p, dest, 3, temp);
	 update_src_ptr(p, srcECX, vtxESI, a);
	 break;
      case EMIT_4F_VIEWPORT: 
	 get_src_ptr(p, srcECX, vtxESI, a);
	 emit_load(p, temp, 4, x86_deref(srcECX), a->inputsize);
	 sse_mulps(&p->func, temp, vp0);
	 sse_addps(&p->func, temp, vp1);
	 emit_store(p, dest, 4, temp);
	 update_src_ptr(p, srcECX, vtxESI, a);
	 break;
      case EMIT_3F_XYW:
	 get_src_ptr(p, srcECX, vtxESI, a);
	 emit_load(p, temp, 4, x86_deref(srcECX), a->inputsize);
	 sse_shufps(&p->func, temp, temp, SHUF(X,Y,W,Z));
	 emit_store(p, dest, 3, temp);
	 update_src_ptr(p, srcECX, vtxESI, a);
	 break;

      case EMIT_1UB_1F:	 
	 /* Test for PAD3 + 1UB:
	  */
	 if (j > 0 &&
	     a[-1].vertoffset + a[-1].vertattrsize <= a->vertoffset - 3)
	 {
	    get_src_ptr(p, srcECX, vtxESI, a);
	    emit_load(p, temp, 1, x86_deref(srcECX), a->inputsize);
	    sse_shufps(&p->func, temp, temp, SHUF(X,X,X,X));
	    emit_pack_store_4ub(p, x86_make_disp(dest, -3), temp); /* overkill! */
	    update_src_ptr(p, srcECX, vtxESI, a);
	 }
	 else {
	    _mesa_printf("Can't emit 1ub %x %x %d\n", a->vertoffset, a[-1].vertoffset, a[-1].vertattrsize );
	    return GL_FALSE;
	 }
	 break;
      case EMIT_3UB_3F_RGB:
      case EMIT_3UB_3F_BGR:
	 /* Test for 3UB + PAD1:
	  */
	 if (j == vtx->attr_count - 1 ||
	     a[1].vertoffset >= a->vertoffset + 4) {
	    get_src_ptr(p, srcECX, vtxESI, a);
	    emit_load(p, temp, 3, x86_deref(srcECX), a->inputsize);
	    if (a->format == EMIT_3UB_3F_BGR)
	       sse_shufps(&p->func, temp, temp, SHUF(Z,Y,X,W));
	    emit_pack_store_4ub(p, dest, temp);
	    update_src_ptr(p, srcECX, vtxESI, a);
	 }
	 /* Test for 3UB + 1UB:
	  */
	 else if (j < vtx->attr_count - 1 &&
		  a[1].format == EMIT_1UB_1F &&
		  a[1].vertoffset == a->vertoffset + 3) {
	    get_src_ptr(p, srcECX, vtxESI, a);
	    emit_load(p, temp, 3, x86_deref(srcECX), a->inputsize);
	    update_src_ptr(p, srcECX, vtxESI, a);

	    /* Make room for incoming value:
	     */
	    sse_shufps(&p->func, temp, temp, SHUF(W,X,Y,Z));

	    get_src_ptr(p, srcECX, vtxESI, &a[1]);
	    emit_load(p, temp2, 1, x86_deref(srcECX), a[1].inputsize);
	    sse_movss(&p->func, temp, temp2);
	    update_src_ptr(p, srcECX, vtxESI, &a[1]);

	    /* Rearrange and possibly do BGR conversion:
	     */
	    if (a->format == EMIT_3UB_3F_BGR)
	       sse_shufps(&p->func, temp, temp, SHUF(W,Z,Y,X));
	    else
	       sse_shufps(&p->func, temp, temp, SHUF(Y,Z,W,X));

	    emit_pack_store_4ub(p, dest, temp);
	    j++;		/* NOTE: two attrs consumed */
	 }
	 else {
	    _mesa_printf("Can't emit 3ub\n");
	    return GL_FALSE;	/* add this later */
	 }
	 break;

      case EMIT_4UB_4F_RGBA:
	 get_src_ptr(p, srcECX, vtxESI, a);
	 emit_load(p, temp, 4, x86_deref(srcECX), a->inputsize);
	 emit_pack_store_4ub(p, dest, temp);
	 update_src_ptr(p, srcECX, vtxESI, a);
	 break;
      case EMIT_4UB_4F_BGRA:
	 get_src_ptr(p, srcECX, vtxESI, a);
	 emit_load(p, temp, 4, x86_deref(srcECX), a->inputsize);
	 sse_shufps(&p->func, temp, temp, SHUF(Z,Y,X,W));
	 emit_pack_store_4ub(p, dest, temp);
	 update_src_ptr(p, srcECX, vtxESI, a);
	 break;
      case EMIT_4UB_4F_ARGB:
	 get_src_ptr(p, srcECX, vtxESI, a);
	 emit_load(p, temp, 4, x86_deref(srcECX), a->inputsize);
	 sse_shufps(&p->func, temp, temp, SHUF(W,X,Y,Z));
	 emit_pack_store_4ub(p, dest, temp);
	 update_src_ptr(p, srcECX, vtxESI, a);
	 break;
      case EMIT_4UB_4F_ABGR:
	 get_src_ptr(p, srcECX, vtxESI, a);
	 emit_load(p, temp, 4, x86_deref(srcECX), a->inputsize);
	 sse_shufps(&p->func, temp, temp, SHUF(W,Z,Y,X));
	 emit_pack_store_4ub(p, dest, temp);
	 update_src_ptr(p, srcECX, vtxESI, a);
	 break;
      case EMIT_4CHAN_4F_RGBA:
	 switch (CHAN_TYPE) {
	 case GL_UNSIGNED_BYTE:
	    get_src_ptr(p, srcECX, vtxESI, a);
	    emit_load(p, temp, 4, x86_deref(srcECX), a->inputsize);
	    emit_pack_store_4ub(p, dest, temp);
	    update_src_ptr(p, srcECX, vtxESI, a);
	    break;
	 case GL_FLOAT:
	    get_src_ptr(p, srcECX, vtxESI, a);
	    emit_load(p, temp, 4, x86_deref(srcECX), a->inputsize);
	    emit_store(p, dest, 4, temp);
	    update_src_ptr(p, srcECX, vtxESI, a);
	    break;
	 case GL_UNSIGNED_SHORT:
	 default:
	    _mesa_printf("unknown CHAN_TYPE %s\n", _mesa_lookup_enum_by_nr(CHAN_TYPE));
	    return GL_FALSE;
	 }
	 break;
      default:
	 _mesa_printf("unknown a[%d].format %d\n", j, a->format);
	 return GL_FALSE;	/* catch any new opcodes */
      }
      
      /* Increment j by at least 1 - may have been incremented above also:
       */
      j++;
   }

   /* Next vertex:
    */
   x86_lea(&p->func, vertexEAX, x86_make_disp(vertexEAX, vtx->vertex_size));

   /* decr count, loop if not zero
    */
   x86_dec(&p->func, countEBP);
   x86_test(&p->func, countEBP, countEBP); 
   x86_jcc(&p->func, cc_NZ, label);

   /* Exit mmx state?
    */
   if (p->func.need_emms)
      mmx_emms(&p->func);

   /* Land forward jump here:
    */
   x86_fixup_fwd_jump(&p->func, fixup);

   /* Pop regs and return
    */
   x86_pop(&p->func, x86_get_base_reg(vtxESI));
   x86_pop(&p->func, countEBP);
   x86_ret(&p->func);

   assert(!vtx->emit);
   vtx->emit = (tnl_emit_func)x86_get_func(&p->func);

   assert( (char *) p->func.csr - (char *) p->func.store <= MAX_SSE_CODE_SIZE );
   return GL_TRUE;
}



void _tnl_generate_sse_emit( GLcontext *ctx )
{
   struct tnl_clipspace *vtx = GET_VERTEX_STATE(ctx);
   struct x86_program p;   

   if (!cpu_has_xmm) {
      vtx->codegen_emit = NULL;
      return;
   }

   memset(&p, 0, sizeof(p));

   p.ctx = ctx;
   p.inputs_safe = 0;		/* for now */
   p.outputs_safe = 0;		/* for now */
   p.have_sse2 = cpu_has_xmm2;
   p.identity = x86_make_reg(file_XMM, 6);
   p.chan0 = x86_make_reg(file_XMM, 7);

   if (!x86_init_func_size(&p.func, MAX_SSE_CODE_SIZE)) {
      vtx->emit = NULL;
      return;
   }

   if (build_vertex_emit(&p)) {
      _tnl_register_fastpath( vtx, GL_TRUE );
   }
   else {
      /* Note the failure so that we don't keep trying to codegen an
       * impossible state:
       */
      _tnl_register_fastpath( vtx, GL_FALSE );
      x86_release_func(&p.func);
   }
}

#else

void _tnl_generate_sse_emit( GLcontext *ctx )
{
   /* Dummy version for when USE_SSE_ASM not defined */
}

#endif