summaryrefslogtreecommitdiffstats
path: root/src/mesa/drivers/dri/sis/sis_tris.c
blob: 21ec99c878f6e2043b7c8aa6c30c5d6a676277f9 (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
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
/* $XFree86*/ /* -*- c-basic-offset: 3 -*- */
/**************************************************************************

Copyright 2000 Silicon Integrated Systems Corp, Inc., HsinChu, Taiwan.
Copyright 2003 Eric Anholt
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
ERIC ANHOLT OR SILICON INTEGRATED SYSTEMS CORP 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:
 *   Sung-Ching Lin <sclin@sis.com.tw>
 *   Eric Anholt <anholt@FreeBSD.org>
 */

#include "glheader.h"
#include "mtypes.h"
#include "colormac.h"
#include "macros.h"

#include "swrast/swrast.h"
#include "swrast_setup/swrast_setup.h"
#include "tnl/tnl.h"
#include "tnl/t_context.h"
#include "tnl/t_pipeline.h"

#include "sis_tris.h"
#include "sis_state.h"
#include "sis_vb.h"
#include "sis_lock.h"

static const GLuint hw_prim[GL_POLYGON+1] = {
   OP_3D_POINT_DRAW,		/* GL_POINTS */
   OP_3D_LINE_DRAW,		/* GL_LINES */
   OP_3D_LINE_DRAW,		/* GL_LINE_LOOP */
   OP_3D_LINE_DRAW,		/* GL_LINE_STRIP */
   OP_3D_TRIANGLE_DRAW,		/* GL_TRIANGLES */
   OP_3D_TRIANGLE_DRAW,		/* GL_TRIANGLE_STRIP */
   OP_3D_TRIANGLE_DRAW,		/* GL_TRIANGLE_FAN */
   OP_3D_TRIANGLE_DRAW,		/* GL_QUADS */
   OP_3D_TRIANGLE_DRAW,		/* GL_QUAD_STRIP */
   OP_3D_TRIANGLE_DRAW		/* GL_POLYGON */
};

static const GLuint hw_prim_mmio_fire[OP_3D_TRIANGLE_DRAW+1] = {
   OP_3D_FIRE_TSARGBa,
   OP_3D_FIRE_TSARGBb,
   OP_3D_FIRE_TSARGBc
};

static const GLuint hw_prim_mmio_shade[OP_3D_TRIANGLE_DRAW+1] = {
   SHADE_FLAT_VertexA,
   SHADE_FLAT_VertexB,
   SHADE_FLAT_VertexC
};

static const GLuint hw_prim_agp_type[OP_3D_TRIANGLE_DRAW+1] = {
   MASK_PsPointList,
   MASK_PsLineList,
   MASK_PsTriangleList
};

static const GLuint hw_prim_agp_shade[OP_3D_TRIANGLE_DRAW+1] = {
   MASK_PsShadingFlatA,
   MASK_PsShadingFlatB,
   MASK_PsShadingFlatC
};

static void sisRasterPrimitive( GLcontext *ctx, GLuint hwprim );
static void sisRenderPrimitive( GLcontext *ctx, GLenum prim );
static void sisMakeRoomAGP( sisContextPtr smesa, GLint num );
static void sisUpdateAGP( sisContextPtr smesa );
static void sisFireVertsAGP( sisContextPtr smesa );

static float *AGP_StartPtr;
static float *AGP_WritePtr;		/* Current write position */
static float *AGP_ReadPtr;		/* Last known engine readposition */
static long AGP_SpaceLeft;		/* Last known engine readposition */

/***********************************************************************
 *                    Emit primitives as inline vertices               *
 ***********************************************************************/

/* Future optimizations:
 *
 * The previous code only emitted W when fog or textures were enabled.
 */

#define SIS_MMIO_WRITE_VERTEX(_v, i, lastvert)			\
do {								\
   MMIOBase[(REG_3D_TSXa+(i)*0x30)/4] = _v->v.x;		\
   MMIOBase[(REG_3D_TSYa+(i)*0x30)/4] = _v->v.y;	        \
   MMIOBase[(REG_3D_TSZa+(i)*0x30)/4] = _v->v.z;		\
   MMIOBase[(REG_3D_TSWGa+(i)*0x30)/4] = _v->v.w;		\
   /*((GLint *) MMIOBase)[(REG_3D_TSFSa+(i)*0x30)/4] = _v->ui[5];*/ \
   if (SIS_STATES & SIS_VERT_TEX0) {				\
      MMIOBase[(REG_3D_TSUAa+(i)*0x30)/4] = _v->v.u0;		\
      MMIOBase[(REG_3D_TSVAa+(i)*0x30)/4] = _v->v.v0;		\
   }								\
   if (SIS_STATES & SIS_VERT_TEX1) {				\
      MMIOBase[(REG_3D_TSUBa+(i)*0x30)/4] = _v->v.u1;		\
      MMIOBase[(REG_3D_TSVBa+(i)*0x30)/4] = _v->v.v1;		\
   }								\
   /*MMIOBase[(REG_3D_TSUCa+(i)*0x30)/4] = _v->v.u2;		\
   MMIOBase[(REG_3D_TSVCa+(i)*0x30)/4] = _v->v.v2;*/		\
   /* the ARGB write of the last vertex of the primitive fires the 3d engine*/ \
   if (lastvert || (SIS_STATES & SIS_VERT_SMOOTH))		\
      ((GLint *) MMIOBase)[(REG_3D_TSARGBa+(i)*0x30)/4] = _v->ui[4]; \
} while (0);

#define SIS_AGP_WRITE_VERTEX(_v)				\
do {								\
   AGP_WritePtr[0] = _v->v.x;					\
   AGP_WritePtr[1] = _v->v.y;					\
   AGP_WritePtr[2] = _v->v.z;					\
   AGP_WritePtr[3] = _v->v.w;					\
   ((GLint *)AGP_WritePtr)[4] = _v->ui[4];			\
   AGP_WritePtr += 5;						\
   if (SIS_STATES & SIS_VERT_TEX0) {				\
      AGP_WritePtr[0] = _v->v.u0;				\
      AGP_WritePtr[1] = _v->v.v0;				\
      AGP_WritePtr += 2;					\
   }								\
   if (SIS_STATES & SIS_VERT_TEX1) {				\
      AGP_WritePtr[0] = _v->v.u1;				\
      AGP_WritePtr[1] = _v->v.v1;				\
      AGP_WritePtr += 2;					\
   }								\
} while(0)

#define MMIO_VERT_REG_COUNT 10

#define SIS_VERT_SMOOTH	0x01
#define SIS_VERT_TEX0	0x02
#define SIS_VERT_TEX1	0x04

static sis_quad_func sis_quad_func_agp[8];
static sis_tri_func sis_tri_func_agp[8];
static sis_line_func sis_line_func_agp[8];
static sis_point_func sis_point_func_agp[8];
static sis_quad_func sis_quad_func_mmio[8];
static sis_tri_func sis_tri_func_mmio[8];
static sis_line_func sis_line_func_mmio[8];
static sis_point_func sis_point_func_mmio[8];

/* XXX: These definitions look questionable */
#define USE_XYZ		MASK_PsVertex_HAS_RHW
#define USE_W		MASK_PsVertex_HAS_NORMALXYZ
#define USE_RGB		MASK_PsVertex_HAS_SPECULAR
#define USE_UV1		MASK_PsVertex_HAS_UVSet2
#define USE_UV2		MASK_PsVertex_HAS_UVSet3

static GLint AGPParsingValues[8] = {
  (5 << 28) | USE_XYZ | USE_W | USE_RGB,
  (5 << 28) | USE_XYZ | USE_W | USE_RGB,
  (7 << 28) | USE_XYZ | USE_W | USE_RGB | USE_UV1,
  (7 << 28) | USE_XYZ | USE_W | USE_RGB | USE_UV1,
  (7 << 28) | USE_XYZ | USE_W | USE_RGB | USE_UV2,
  (7 << 28) | USE_XYZ | USE_W | USE_RGB | USE_UV2,
  (9 << 28) | USE_XYZ | USE_W | USE_RGB | USE_UV1 | USE_UV2,
  (9 << 28) | USE_XYZ | USE_W | USE_RGB | USE_UV1 | USE_UV2,
};

#define SIS_STATES (0)
#define TAG(x) x##_none
#include "sis_tritmp.h"

#define SIS_STATES (SIS_VERT_SMOOTH)
#define TAG(x) x##_s
#include "sis_tritmp.h"

#define SIS_STATES (SIS_VERT_TEX0)
#define TAG(x) x##_t0
#include "sis_tritmp.h"

#define SIS_STATES (SIS_VERT_SMOOTH | SIS_VERT_TEX0)
#define TAG(x) x##_st0
#include "sis_tritmp.h"

#define SIS_STATES (SIS_VERT_TEX1)
#define TAG(x) x##_t1
#include "sis_tritmp.h"

#define SIS_STATES (SIS_VERT_SMOOTH | SIS_VERT_TEX1)
#define TAG(x) x##_st1
#include "sis_tritmp.h"

#define SIS_STATES (SIS_VERT_TEX0 | SIS_VERT_TEX1)
#define TAG(x) x##_t0t1
#include "sis_tritmp.h"

#define SIS_STATES (SIS_VERT_SMOOTH | SIS_VERT_TEX0 | SIS_VERT_TEX1)
#define TAG(x) x##_st0t1
#include "sis_tritmp.h"

/***********************************************************************
 *          Macros for t_dd_tritmp.h to draw basic primitives          *
 ***********************************************************************/

#define POINT( v0 ) smesa->draw_point( smesa, v0 )
#define LINE( v0, v1 ) smesa->draw_line( smesa, v0, v1 )
#define TRI( a, b, c ) smesa->draw_tri( smesa, a, b, c )
#define QUAD( a, b, c, d ) smesa->draw_quad( smesa, a, b, c, d )

/***********************************************************************
 *              Build render functions from dd templates               *
 ***********************************************************************/

#define SIS_OFFSET_BIT		0x01
#define SIS_TWOSIDE_BIT		0x02
#define SIS_UNFILLED_BIT	0x04
#define SIS_FALLBACK_BIT	0x08
#define SIS_MAX_TRIFUNC		0x10


static struct {
   points_func	        points;
   line_func		line;
   triangle_func	triangle;
   quad_func		quad;
} rast_tab[SIS_MAX_TRIFUNC];


#define DO_FALLBACK (IND & SIS_FALLBACK_BIT)
#define DO_OFFSET   (IND & SIS_OFFSET_BIT)
#define DO_UNFILLED (IND & SIS_UNFILLED_BIT)
#define DO_TWOSIDE  (IND & SIS_TWOSIDE_BIT)
#define DO_FLAT      0
#define DO_TRI       1
#define DO_QUAD      1
#define DO_LINE      1
#define DO_POINTS    1
#define DO_FULL_QUAD 1

#define HAVE_RGBA   1
#define HAVE_SPEC   1
#define HAVE_BACK_COLORS  0
#define HAVE_HW_FLATSHADE 1
#define VERTEX sisVertex
#define TAB rast_tab

#define DEPTH_SCALE 1.0
#define UNFILLED_TRI unfilled_tri
#define UNFILLED_QUAD unfilled_quad
#define VERT_X(_v) _v->v.x
#define VERT_Y(_v) _v->v.y
#define VERT_Z(_v) _v->v.z
#define AREA_IS_CCW( a ) (a > 0)
#define GET_VERTEX(e) (smesa->verts + (e * smesa->vertex_size * sizeof(int)))

#define VERT_SET_RGBA( v, c )  					\
do {								\
   sis_color_t *color = (sis_color_t *)&((v)->ui[coloroffset]);	\
   UNCLAMPED_FLOAT_TO_UBYTE(color->red, (c)[0]);		\
   UNCLAMPED_FLOAT_TO_UBYTE(color->green, (c)[1]);		\
   UNCLAMPED_FLOAT_TO_UBYTE(color->blue, (c)[2]);		\
   UNCLAMPED_FLOAT_TO_UBYTE(color->alpha, (c)[3]);		\
} while (0)

#define VERT_COPY_RGBA( v0, v1 ) v0->ui[coloroffset] = v1->ui[coloroffset]

#define VERT_SET_SPEC( v0, c )					\
do {								\
   if (havespec) {						\
      UNCLAMPED_FLOAT_TO_UBYTE(v0->v.specular.red, (c)[0]);	\
      UNCLAMPED_FLOAT_TO_UBYTE(v0->v.specular.green, (c)[1]);	\
      UNCLAMPED_FLOAT_TO_UBYTE(v0->v.specular.blue, (c)[2]);	\
   }								\
} while (0)
#define VERT_COPY_SPEC( v0, v1 )			\
do {							\
   if (havespec) {					\
      v0->v.specular.red   = v1->v.specular.red;	\
      v0->v.specular.green = v1->v.specular.green;	\
      v0->v.specular.blue  = v1->v.specular.blue; 	\
   }							\
} while (0)

#define VERT_SAVE_RGBA( idx )    color[idx] = v[idx]->ui[coloroffset]
#define VERT_RESTORE_RGBA( idx ) v[idx]->ui[coloroffset] = color[idx]
#define VERT_SAVE_SPEC( idx )    if (havespec) spec[idx] = v[idx]->ui[5]
#define VERT_RESTORE_SPEC( idx ) if (havespec) v[idx]->ui[5] = spec[idx]

#define LOCAL_VARS(n)						\
   sisContextPtr smesa = SIS_CONTEXT(ctx);			\
   GLuint color[n], spec[n];					\
   GLuint coloroffset = (smesa->vertex_size == 4 ? 3 : 4);	\
   GLboolean havespec = (smesa->vertex_size == 4 ? 0 : 1);	\
   (void) color; (void) spec; (void) coloroffset; (void) havespec;

/***********************************************************************
 *                Helpers for rendering unfilled primitives            *
 ***********************************************************************/

#define RASTERIZE(x) if (smesa->hw_primitive != hw_prim[x]) \
                        sisRasterPrimitive( ctx, hw_prim[x] )
#define RENDER_PRIMITIVE smesa->render_primitive
#define IND SIS_FALLBACK_BIT
#define TAG(x) x
#include "tnl_dd/t_dd_unfilled.h"
#undef IND


/***********************************************************************
 *                      Generate GL render functions                   *
 ***********************************************************************/


#define IND (0)
#define TAG(x) x
#include "tnl_dd/t_dd_tritmp.h"

#define IND (SIS_OFFSET_BIT)
#define TAG(x) x##_offset
#include "tnl_dd/t_dd_tritmp.h"

#define IND (SIS_TWOSIDE_BIT)
#define TAG(x) x##_twoside
#include "tnl_dd/t_dd_tritmp.h"

#define IND (SIS_TWOSIDE_BIT | SIS_OFFSET_BIT)
#define TAG(x) x##_twoside_offset
#include "tnl_dd/t_dd_tritmp.h"

#define IND (SIS_UNFILLED_BIT)
#define TAG(x) x##_unfilled
#include "tnl_dd/t_dd_tritmp.h"

#define IND (SIS_OFFSET_BIT | SIS_UNFILLED_BIT)
#define TAG(x) x##_offset_unfilled
#include "tnl_dd/t_dd_tritmp.h"

#define IND (SIS_TWOSIDE_BIT | SIS_UNFILLED_BIT)
#define TAG(x) x##_twoside_unfilled
#include "tnl_dd/t_dd_tritmp.h"

#define IND (SIS_TWOSIDE_BIT | SIS_OFFSET_BIT | SIS_UNFILLED_BIT)
#define TAG(x) x##_twoside_offset_unfilled
#include "tnl_dd/t_dd_tritmp.h"

#define IND (SIS_FALLBACK_BIT)
#define TAG(x) x##_fallback
#include "tnl_dd/t_dd_tritmp.h"

#define IND (SIS_OFFSET_BIT | SIS_FALLBACK_BIT)
#define TAG(x) x##_offset_fallback
#include "tnl_dd/t_dd_tritmp.h"

#define IND (SIS_TWOSIDE_BIT | SIS_FALLBACK_BIT)
#define TAG(x) x##_twoside_fallback
#include "tnl_dd/t_dd_tritmp.h"

#define IND (SIS_TWOSIDE_BIT | SIS_OFFSET_BIT | SIS_FALLBACK_BIT)
#define TAG(x) x##_twoside_offset_fallback
#include "tnl_dd/t_dd_tritmp.h"

#define IND (SIS_UNFILLED_BIT | SIS_FALLBACK_BIT)
#define TAG(x) x##_unfilled_fallback
#include "tnl_dd/t_dd_tritmp.h"

#define IND (SIS_OFFSET_BIT | SIS_UNFILLED_BIT | SIS_FALLBACK_BIT)
#define TAG(x) x##_offset_unfilled_fallback
#include "tnl_dd/t_dd_tritmp.h"

#define IND (SIS_TWOSIDE_BIT | SIS_UNFILLED_BIT | SIS_FALLBACK_BIT)
#define TAG(x) x##_twoside_unfilled_fallback
#include "tnl_dd/t_dd_tritmp.h"

#define IND (SIS_TWOSIDE_BIT | SIS_OFFSET_BIT | SIS_UNFILLED_BIT |  \
	     SIS_FALLBACK_BIT)
#define TAG(x) x##_twoside_offset_unfilled_fallback
#include "tnl_dd/t_dd_tritmp.h"


static void init_rast_tab( void )
{
   init();
   init_offset();
   init_twoside();
   init_twoside_offset();
   init_unfilled();
   init_offset_unfilled();
   init_twoside_unfilled();
   init_twoside_offset_unfilled();
   init_fallback();
   init_offset_fallback();
   init_twoside_fallback();
   init_twoside_offset_fallback();
   init_unfilled_fallback();
   init_offset_unfilled_fallback();
   init_twoside_unfilled_fallback();
   init_twoside_offset_unfilled_fallback();
}



/***********************************************************************
 *                    Rasterization fallback helpers                   *
 ***********************************************************************/


/* This code is hit only when a mix of accelerated and unaccelerated
 * primitives are being drawn, and only for the unaccelerated
 * primitives.
 */
static void
sis_fallback_quad( sisContextPtr smesa,
		   sisVertex *v0,
		   sisVertex *v1,
		   sisVertex *v2,
		   sisVertex *v3 )
{
   GLcontext *ctx = smesa->glCtx;
   SWvertex v[4];
   sis_translate_vertex( ctx, v0, &v[0] );
   sis_translate_vertex( ctx, v1, &v[1] );
   sis_translate_vertex( ctx, v2, &v[2] );
   sis_translate_vertex( ctx, v3, &v[3] );
   _swrast_Triangle( ctx, &v[0], &v[1], &v[3] );
   _swrast_Triangle( ctx, &v[1], &v[2], &v[3] );
}

static void
sis_fallback_tri( sisContextPtr smesa,
		  sisVertex *v0,
		  sisVertex *v1,
		  sisVertex *v2 )
{
   GLcontext *ctx = smesa->glCtx;
   SWvertex v[3];
   sis_translate_vertex( ctx, v0, &v[0] );
   sis_translate_vertex( ctx, v1, &v[1] );
   sis_translate_vertex( ctx, v2, &v[2] );
   _swrast_Triangle( ctx, &v[0], &v[1], &v[2] );
}


static void
sis_fallback_line( sisContextPtr smesa,
		   sisVertex *v0,
		   sisVertex *v1 )
{
   GLcontext *ctx = smesa->glCtx;
   SWvertex v[2];
   sis_translate_vertex( ctx, v0, &v[0] );
   sis_translate_vertex( ctx, v1, &v[1] );
   _swrast_Line( ctx, &v[0], &v[1] );
}


static void
sis_fallback_point( sisContextPtr smesa,
		    sisVertex *v0 )
{
   GLcontext *ctx = smesa->glCtx;
   SWvertex v[1];
   sis_translate_vertex( ctx, v0, &v[0] );
   _swrast_Point( ctx, &v[0] );
}



/**********************************************************************/
/*               Render unclipped begin/end objects                   */
/**********************************************************************/

#define VERT(x) (sisVertex *)(sisverts + (x * vertsize * sizeof(int)))
#define RENDER_POINTS( start, count )		\
   for ( ; start < count ; start++)		\
      smesa->draw_point( smesa, VERT(start) )
#define RENDER_LINE( v0, v1 ) smesa->draw_line( smesa, VERT(v0), VERT(v1) )
#define RENDER_TRI( v0, v1, v2 ) smesa->draw_tri( smesa, VERT(v0), VERT(v1), \
   VERT(v2) )
#define RENDER_QUAD( v0, v1, v2, v3 ) smesa->draw_quad( smesa, VERT(v0), \
   VERT(v1), VERT(v2), VERT(v3))
#define INIT(x) sisRenderPrimitive( ctx, x )
#undef LOCAL_VARS
#define LOCAL_VARS				\
    sisContextPtr smesa = SIS_CONTEXT(ctx);	\
    const GLuint vertsize = smesa->vertex_size;		\
    const char *sisverts = (char *)smesa->verts;		\
    const GLuint * const elt = TNL_CONTEXT(ctx)->vb.Elts;	\
    (void) elt;
#define RESET_STIPPLE
#define RESET_OCCLUSION
#define PRESERVE_VB_DEFS
#define ELT(x) (x)
#define TAG(x) sis_##x##_verts
#include "tnl/t_vb_rendertmp.h"
#undef ELT
#undef TAG
#define TAG(x) sis_##x##_elts
#define ELT(x) elt[x]
#include "tnl/t_vb_rendertmp.h"


/**********************************************************************/
/*                    Render clipped primitives                       */
/**********************************************************************/

static void sisRenderClippedPoly( GLcontext *ctx, const GLuint *elts, GLuint n )
{
   TNLcontext *tnl = TNL_CONTEXT(ctx);
   struct vertex_buffer *VB = &TNL_CONTEXT(ctx)->vb;

   /* Render the new vertices as an unclipped polygon.
    */
   {
      GLuint *tmp = VB->Elts;
      VB->Elts = (GLuint *)elts;
      tnl->Driver.Render.PrimTabElts[GL_POLYGON]( ctx, 0, n, PRIM_BEGIN|PRIM_END );
      VB->Elts = tmp;
   }
}

static void sisRenderClippedLine( GLcontext *ctx, GLuint ii, GLuint jj )
{
   TNLcontext *tnl = TNL_CONTEXT(ctx);
   tnl->Driver.Render.Line( ctx, ii, jj );
}

#if 0
static void sisFastRenderClippedPoly( GLcontext *ctx, const GLuint *elts,
				      GLuint n )
{
   sisContextPtr smesa = SIS_CONTEXT( ctx );
   GLuint vertsize = smesa->vertex_size;
   GLuint *vb = r128AllocDmaLow( rmesa, (n-2) * 3 * 4 * vertsize );
   GLubyte *sisverts = (GLubyte *)smesa->verts;
   const GLuint *start = (const GLuint *)VERT(elts[0]);
   int i,j;

   smesa->num_verts += (n-2) * 3;

   for (i = 2 ; i < n ; i++) {
      COPY_DWORDS( j, vb, vertsize, (sisVertexPtr) VERT(elts[i-1]) );
      COPY_DWORDS( j, vb, vertsize, (sisVertexPtr) VERT(elts[i]) );
      COPY_DWORDS( j, vb, vertsize, (sisVertexPtr) start );
   }
}
#endif




/**********************************************************************/
/*                    Choose render functions                         */
/**********************************************************************/

#define _SIS_NEW_RENDER_STATE (_DD_NEW_LINE_STIPPLE |	\
			          _DD_NEW_LINE_SMOOTH |		\
			          _DD_NEW_POINT_SMOOTH |	\
			          _DD_NEW_TRI_SMOOTH |		\
			          _DD_NEW_TRI_UNFILLED |	\
			          _DD_NEW_TRI_LIGHT_TWOSIDE |	\
			          _DD_NEW_TRI_OFFSET)		\


#define POINT_FALLBACK (DD_POINT_SMOOTH)
#define LINE_FALLBACK (DD_LINE_STIPPLE|DD_LINE_SMOOTH)
#define TRI_FALLBACK (DD_TRI_SMOOTH)
#define ANY_FALLBACK_FLAGS (POINT_FALLBACK|LINE_FALLBACK|TRI_FALLBACK)
#define ANY_RASTER_FLAGS (DD_TRI_LIGHT_TWOSIDE|DD_TRI_OFFSET|DD_TRI_UNFILLED)


static void sisChooseRenderState(GLcontext *ctx)
{
   sisContextPtr smesa = SIS_CONTEXT( ctx );
   GLuint flags = ctx->_TriangleCaps;
   GLuint index = 0;
   GLuint vertindex = 0;
   
   if (ctx->Texture.Unit[0]._ReallyEnabled)
      vertindex |= SIS_VERT_TEX0;
   if (ctx->Texture.Unit[1]._ReallyEnabled)
      vertindex |= SIS_VERT_TEX1;
   if (ctx->Light.ShadeModel == GL_SMOOTH)
      vertindex |= SIS_VERT_SMOOTH;

   if (smesa->AGPCmdModeEnabled) {
      smesa->draw_quad = sis_quad_func_agp[vertindex];
      smesa->draw_tri = sis_tri_func_agp[vertindex];
      smesa->draw_line = sis_line_func_agp[vertindex];
      smesa->draw_point = sis_point_func_agp[vertindex];
   } else {
      smesa->draw_quad = sis_quad_func_mmio[vertindex];
      smesa->draw_tri = sis_tri_func_mmio[vertindex];
      smesa->draw_line = sis_line_func_mmio[vertindex];
      smesa->draw_point = sis_point_func_mmio[vertindex];
   }
   smesa->AGPParseSet &= ~(MASK_VertexDWSize | MASK_VertexDataFormat);
   smesa->AGPParseSet |= AGPParsingValues[vertindex];

   if (flags & (ANY_RASTER_FLAGS|ANY_FALLBACK_FLAGS)) {

      if (flags & ANY_RASTER_FLAGS) {
	 if (flags & DD_TRI_LIGHT_TWOSIDE) index |= SIS_TWOSIDE_BIT;
	 if (flags & DD_TRI_OFFSET)        index |= SIS_OFFSET_BIT;
	 if (flags & DD_TRI_UNFILLED)      index |= SIS_UNFILLED_BIT;
      }

      /* Hook in fallbacks for specific primitives.
       */
      if (flags & ANY_FALLBACK_FLAGS) {
	 if (flags & POINT_FALLBACK)
            smesa->draw_point = sis_fallback_point;
	 if (flags & LINE_FALLBACK)
            smesa->draw_line = sis_fallback_line;
	 if (flags & TRI_FALLBACK) {
            smesa->draw_quad = sis_fallback_quad;
            smesa->draw_tri = sis_fallback_tri;
         }
	 index |= SIS_FALLBACK_BIT;
      }
   }

   if (index != smesa->RenderIndex) {
      TNLcontext *tnl = TNL_CONTEXT(ctx);
      tnl->Driver.Render.Points = rast_tab[index].points;
      tnl->Driver.Render.Line = rast_tab[index].line;
      tnl->Driver.Render.Triangle = rast_tab[index].triangle;
      tnl->Driver.Render.Quad = rast_tab[index].quad;

      if (index == 0) {
	 tnl->Driver.Render.PrimTabVerts = sis_render_tab_verts;
	 tnl->Driver.Render.PrimTabElts = sis_render_tab_elts;
	 tnl->Driver.Render.ClippedLine = rast_tab[index].line;
         /*XXX: sisFastRenderClippedPoly*/
	 tnl->Driver.Render.ClippedPolygon = sisRenderClippedPoly;
      } else {
	 tnl->Driver.Render.PrimTabVerts = _tnl_render_tab_verts;
	 tnl->Driver.Render.PrimTabElts = _tnl_render_tab_elts;
	 tnl->Driver.Render.ClippedLine = sisRenderClippedLine;
	 tnl->Driver.Render.ClippedPolygon = sisRenderClippedPoly;
      }

      smesa->RenderIndex = index;
   }
}

/**********************************************************************/
/*                Multipass rendering for front buffering             */
/**********************************************************************/
static GLboolean multipass_cliprect( GLcontext *ctx, GLuint pass )
{
   sisContextPtr smesa = SIS_CONTEXT( ctx );

   if (pass >= smesa->driDrawable->numClipRects) {
      return GL_FALSE;
   } else {
      GLint x1, y1, x2, y2;

      x1 = smesa->driDrawable->pClipRects[pass].x1 - smesa->driDrawable->x;
      y1 = smesa->driDrawable->pClipRects[pass].y1 - smesa->driDrawable->y;
      x2 = smesa->driDrawable->pClipRects[pass].x2 - smesa->driDrawable->x;
      y2 = smesa->driDrawable->pClipRects[pass].y2 - smesa->driDrawable->y;

      if (ctx->Scissor.Enabled) {
         GLint scisy1 = Y_FLIP(ctx->Scissor.Y + ctx->Scissor.Height - 1);
         GLint scisy2 = Y_FLIP(ctx->Scissor.Y);

         if (ctx->Scissor.X > x1)
            x1 = ctx->Scissor.X;
         if (scisy1 > y1)
            y1 = scisy1;
         if (ctx->Scissor.X + ctx->Scissor.Width - 1 < x2)
            x2 = ctx->Scissor.X + ctx->Scissor.Width - 1;
         if (scisy2 < y2)
            y2 = scisy2;
      }

      MMIO(REG_3D_ClipTopBottom, y1 << 13 | y2);
      MMIO(REG_3D_ClipLeftRight, x1 << 13 | x2);
      /* Mark that we clobbered these registers */
      smesa->GlobalFlag |= GFLAG_CLIPPING;
      return GL_TRUE;
   }
}



/**********************************************************************/
/*                 Validate state at pipeline start                   */
/**********************************************************************/

static void sisRunPipeline( GLcontext *ctx )
{
   sisContextPtr smesa = SIS_CONTEXT( ctx );

   LOCK_HARDWARE();
   sisUpdateHWState( ctx );

   if (smesa->AGPCmdModeEnabled) {
      AGP_WritePtr = (GLfloat *)smesa->AGPCmdBufBase + *smesa->pAGPCmdBufNext;
      AGP_StartPtr = AGP_WritePtr;
      AGP_ReadPtr = (GLfloat *)((long)MMIO_READ(REG_3D_AGPCmBase) -
         (long)smesa->AGPCmdBufAddr + (long)smesa->AGPCmdBufBase);
      sisUpdateAGP( smesa );
   }

   if (!smesa->Fallback && smesa->NewGLState) {
      if (smesa->NewGLState & _SIS_NEW_VERTEX_STATE)
	 sisChooseVertexState( ctx );

      if (smesa->NewGLState & (_SIS_NEW_RENDER_STATE | _NEW_TEXTURE))
	 sisChooseRenderState( ctx );

      smesa->NewGLState = 0;
   }

   _tnl_run_pipeline( ctx );

   if (smesa->AGPCmdModeEnabled)
      sisFireVertsAGP( smesa );
   else
      mEndPrimitive();
   UNLOCK_HARDWARE();
}

/**********************************************************************/
/*                 High level hooks for t_vb_render.c                 */
/**********************************************************************/

/* This is called when Mesa switches between rendering triangle
 * primitives (such as GL_POLYGON, GL_QUADS, GL_TRIANGLE_STRIP, etc),
 * and lines, points and bitmaps.
 */

static void sisRasterPrimitive( GLcontext *ctx, GLuint hwprim )
{
   sisContextPtr smesa = SIS_CONTEXT(ctx);
   if (smesa->hw_primitive != hwprim) {
      if (smesa->AGPCmdModeEnabled) {
         sisFireVertsAGP( smesa );
         smesa->AGPParseSet &= ~(MASK_PsDataType | MASK_PsShadingMode);
         smesa->AGPParseSet |= hw_prim_agp_type[hwprim];
         if (ctx->Light.ShadeModel == GL_FLAT)
            smesa->AGPParseSet |= hw_prim_agp_shade[hwprim];
         else
            smesa->AGPParseSet |= MASK_PsShadingSmooth;
      } else {
         mEndPrimitive();
         smesa->dwPrimitiveSet &= ~(MASK_DrawPrimitiveCommand | 
            MASK_SetFirePosition | MASK_ShadingMode);
         smesa->dwPrimitiveSet |= hwprim | hw_prim_mmio_fire[hwprim];
         if (ctx->Light.ShadeModel == GL_FLAT)
            smesa->dwPrimitiveSet |= hw_prim_mmio_shade[hwprim];
         else
            smesa->dwPrimitiveSet |= SHADE_GOURAUD;
      }
   }
   smesa->hw_primitive = hwprim;
}

static void sisRenderPrimitive( GLcontext *ctx, GLenum prim )
{
   sisContextPtr smesa = SIS_CONTEXT(ctx);

   smesa->render_primitive = prim;
   if (prim >= GL_TRIANGLES && (ctx->_TriangleCaps & DD_TRI_UNFILLED))
      return;
   sisRasterPrimitive( ctx, hw_prim[prim] );
}


static void sisRenderStart( GLcontext *ctx )
{
   TNLcontext *tnl = TNL_CONTEXT(ctx);
   sisContextPtr smesa = SIS_CONTEXT(ctx);

   /* Check for projective texturing.  Make sure all texcoord
    * pointers point to something.  (fix in mesa?)
    */
   sisCheckTexSizes( ctx );

   if (ctx->Color._DrawDestMask == DD_FRONT_LEFT_BIT && 
      smesa->driDrawable->numClipRects != 0)
   {
      multipass_cliprect(ctx, 0);
      if (smesa->driDrawable->numClipRects > 1)
         tnl->Driver.Render.Multipass = multipass_cliprect;
      else
         tnl->Driver.Render.Multipass = NULL;
   } else {
      tnl->Driver.Render.Multipass = NULL;
   }
}

static void sisRenderFinish( GLcontext *ctx )
{
}

/* Update SpaceLeft after an engine or current write pointer update */
static void sisUpdateAGP( sisContextPtr smesa )
{
   /* ReadPtr == WritePtr is the empty case */
   if (AGP_ReadPtr <= AGP_WritePtr)
      AGP_SpaceLeft = (long)smesa->AGPCmdBufBase + (long)smesa->AGPCmdBufSize - 
         (long)AGP_WritePtr;
   else
      AGP_SpaceLeft = AGP_ReadPtr - AGP_WritePtr - 4;
}

/* Fires a set of vertices that have been written from AGP_StartPtr to
 * AGP_WritePtr, using the smesa->AGPParseSet format.
 */
void
sisFireVertsAGP( sisContextPtr smesa )
{
   if (AGP_WritePtr == AGP_StartPtr)
      return;

   mWait3DCmdQueue(5);
   mEndPrimitive();
   MMIO(REG_3D_AGPCmBase, (long)AGP_StartPtr - (long)smesa->AGPCmdBufBase +
      (long)smesa->AGPCmdBufAddr);
   MMIO(REG_3D_AGPTtDwNum, (((long)AGP_WritePtr - (long)AGP_StartPtr) >> 2) |
      0x50000000);
   MMIO(REG_3D_ParsingSet, smesa->AGPParseSet);
   
   MMIO(REG_3D_AGPCmFire, (GLint)(-1));
   mEndPrimitive();

   *(smesa->pAGPCmdBufNext) = (((long)AGP_WritePtr -
      (long)smesa->AGPCmdBufBase) + 0xf) & ~0xf;
   AGP_StartPtr = AGP_WritePtr;
   sisUpdateAGP( smesa );
}

/* Make sure there are more than num dwords left in the AGP queue. */
static void
sisMakeRoomAGP( sisContextPtr smesa, GLint num )
{
   int size = num * 4;
   
   if (size <= AGP_SpaceLeft) {
      AGP_SpaceLeft -= size;
      return;
   }
   /* Wrapping */
   if (AGP_WritePtr + num > (GLfloat *)(smesa->AGPCmdBufBase +
      smesa->AGPCmdBufSize))
   {
      sisFireVertsAGP( smesa );
      AGP_WritePtr = (GLfloat *)smesa->AGPCmdBufBase;
      AGP_StartPtr = AGP_WritePtr;
      sisUpdateAGP( smesa );
      WaitEngIdle( smesa ); /* XXX Why is this necessary? */
   }

   if (size > AGP_SpaceLeft) {
      /* Update the cached engine read pointer */
      AGP_ReadPtr = (GLfloat *)((long)MMIO_READ(REG_3D_AGPCmBase) -
         (long)smesa->AGPCmdBufAddr + (long)smesa->AGPCmdBufBase);
      sisUpdateAGP( smesa );
      while (size > AGP_SpaceLeft) {
         /* Spin until space is available. */
         AGP_ReadPtr = (GLfloat *)((long)MMIO_READ(REG_3D_AGPCmBase) -
            (long)smesa->AGPCmdBufAddr + (long)smesa->AGPCmdBufBase);
         sisUpdateAGP( smesa );
      }
   }
   AGP_SpaceLeft -= size;
}

/**********************************************************************/
/*           Transition to/from hardware rasterization.               */
/**********************************************************************/

void sisFallback( GLcontext *ctx, GLuint bit, GLboolean mode )
{
   TNLcontext *tnl = TNL_CONTEXT(ctx);
   sisContextPtr smesa = SIS_CONTEXT(ctx);
   GLuint oldfallback = smesa->Fallback;

   if (mode) {
      smesa->Fallback |= bit;
      if (oldfallback == 0) {
	 _swsetup_Wakeup( ctx );
	 smesa->RenderIndex = ~0;
      }
   }
   else {
      smesa->Fallback &= ~bit;
      if (oldfallback == bit) {
	 _swrast_flush( ctx );
	 tnl->Driver.Render.Start = sisRenderStart;
	 tnl->Driver.Render.PrimitiveNotify = sisRenderPrimitive;
	 tnl->Driver.Render.Finish = sisRenderFinish;
	 tnl->Driver.Render.BuildVertices = sisBuildVertices;
	 smesa->NewGLState |= (_SIS_NEW_RENDER_STATE|
			       _SIS_NEW_VERTEX_STATE);
      }
   }
}


/**********************************************************************/
/*                            Initialization.                         */
/**********************************************************************/

void sisInitTriFuncs( GLcontext *ctx )
{
   sisContextPtr smesa = SIS_CONTEXT(ctx);
   TNLcontext *tnl = TNL_CONTEXT(ctx);
   static int firsttime = 1;

   if (firsttime) {
      init_rast_tab();
      firsttime = 0;

      sis_vert_init_none();
      sis_vert_init_s();
      sis_vert_init_t0();
      sis_vert_init_st0();
      sis_vert_init_t1();
      sis_vert_init_st1();
      sis_vert_init_t0t1();
      sis_vert_init_st0t1();
   }

   tnl->Driver.RunPipeline = sisRunPipeline;
   tnl->Driver.Render.Start = sisRenderStart;
   tnl->Driver.Render.Finish = sisRenderFinish;
   tnl->Driver.Render.PrimitiveNotify = sisRenderPrimitive;
   tnl->Driver.Render.ResetLineStipple = _swrast_ResetLineStipple;
   tnl->Driver.Render.BuildVertices = sisBuildVertices;
   tnl->Driver.Render.Multipass		= NULL;

   if (driQueryOptionb(&smesa->optionCache, "fallback_force"))
      sisFallback(ctx, SIS_FALLBACK_FORCE, 1);
   else
      sisFallback(ctx, SIS_FALLBACK_FORCE, 0);

   smesa->RenderIndex = ~0;
   smesa->NewGLState |= (_SIS_NEW_RENDER_STATE|
			 _SIS_NEW_VERTEX_STATE);
}